]> git.proxmox.com Git - mirror_frr.git/blob - bgpd/bgp_nexthop.c
Marker merge for 'RE-0.99.17.6'
[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
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. */
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
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. */
41
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 */
46 \f
47 /* Only one BGP scan thread are activated at the same time. */
48 static struct thread *bgp_scan_thread = NULL;
49
50 /* BGP import thread */
51 static struct thread *bgp_import_thread = NULL;
52
53 /* BGP scan interval. */
54 static int bgp_scan_interval;
55
56 /* BGP import interval. */
57 static int bgp_import_interval;
58
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];
63
64 /* Route table for connected route. */
65 static struct bgp_table *bgp_connected_table[AFI_MAX];
66
67 /* BGP nexthop lookup query client. */
68 struct zclient *zlookup = NULL;
69 \f
70 /* Add nexthop to the end of the list. */
71 static void
72 bnc_nexthop_add (struct bgp_nexthop_cache *bnc, struct nexthop *nexthop)
73 {
74 struct nexthop *last;
75
76 for (last = bnc->nexthop; last && last->next; last = last->next)
77 ;
78 if (last)
79 last->next = nexthop;
80 else
81 bnc->nexthop = nexthop;
82 nexthop->prev = last;
83 }
84
85 static void
86 bnc_nexthop_free (struct bgp_nexthop_cache *bnc)
87 {
88 struct nexthop *nexthop;
89 struct nexthop *next = NULL;
90
91 for (nexthop = bnc->nexthop; nexthop; nexthop = next)
92 {
93 next = nexthop->next;
94 XFREE (MTYPE_NEXTHOP, nexthop);
95 }
96 }
97
98 static struct bgp_nexthop_cache *
99 bnc_new (void)
100 {
101 return XCALLOC (MTYPE_BGP_NEXTHOP_CACHE, sizeof (struct bgp_nexthop_cache));
102 }
103
104 static void
105 bnc_free (struct bgp_nexthop_cache *bnc)
106 {
107 bnc_nexthop_free (bnc);
108 XFREE (MTYPE_BGP_NEXTHOP_CACHE, bnc);
109 }
110 \f
111 static int
112 bgp_nexthop_same (struct nexthop *next1, struct nexthop *next2)
113 {
114 if (next1->type != next2->type)
115 return 0;
116
117 switch (next1->type)
118 {
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;
144 }
145 return 1;
146 }
147
148 static int
149 bgp_nexthop_cache_different (struct bgp_nexthop_cache *bnc1,
150 struct bgp_nexthop_cache *bnc2)
151 {
152 int i;
153 struct nexthop *next1, *next2;
154
155 if (bnc1->nexthop_num != bnc2->nexthop_num)
156 return 1;
157
158 next1 = bnc1->nexthop;
159 next2 = bnc2->nexthop;
160
161 for (i = 0; i < bnc1->nexthop_num; i++)
162 {
163 if (! bgp_nexthop_same (next1, next2))
164 return 1;
165
166 next1 = next1->next;
167 next2 = next2->next;
168 }
169 return 0;
170 }
171
172 /* If nexthop exists on connected network return 1. */
173 int
174 bgp_nexthop_onlink (afi_t afi, struct attr *attr)
175 {
176 struct bgp_node *rn;
177
178 /* Lookup the address is onlink or not. */
179 if (afi == AFI_IP)
180 {
181 rn = bgp_node_match_ipv4 (bgp_connected_table[AFI_IP], &attr->nexthop);
182 if (rn)
183 {
184 bgp_unlock_node (rn);
185 return 1;
186 }
187 }
188 #ifdef HAVE_IPV6
189 else if (afi == AFI_IP6)
190 {
191 if (attr->extra->mp_nexthop_len == 32)
192 return 1;
193 else if (attr->extra->mp_nexthop_len == 16)
194 {
195 if (IN6_IS_ADDR_LINKLOCAL (&attr->extra->mp_nexthop_global))
196 return 1;
197
198 rn = bgp_node_match_ipv6 (bgp_connected_table[AFI_IP6],
199 &attr->extra->mp_nexthop_global);
200 if (rn)
201 {
202 bgp_unlock_node (rn);
203 return 1;
204 }
205 }
206 }
207 #endif /* HAVE_IPV6 */
208 return 0;
209 }
210
211 #ifdef HAVE_IPV6
212 /* Check specified next-hop is reachable or not. */
213 static int
214 bgp_nexthop_lookup_ipv6 (struct peer *peer, struct bgp_info *ri, int *changed,
215 int *metricchanged)
216 {
217 struct bgp_node *rn;
218 struct prefix p;
219 struct bgp_nexthop_cache *bnc;
220 struct attr *attr;
221
222 /* Only check IPv6 global address only nexthop. */
223 attr = ri->attr;
224
225 if (attr->extra->mp_nexthop_len != 16
226 || IN6_IS_ADDR_LINKLOCAL (&attr->extra->mp_nexthop_global))
227 return 1;
228
229 memset (&p, 0, sizeof (struct prefix));
230 p.family = AF_INET6;
231 p.prefixlen = IPV6_MAX_BITLEN;
232 p.u.prefix6 = attr->extra->mp_nexthop_global;
233
234 /* IBGP or ebgp-multihop */
235 rn = bgp_node_get (bgp_nexthop_cache_table[AFI_IP6], &p);
236
237 if (rn->info)
238 {
239 bnc = rn->info;
240 bgp_unlock_node (rn);
241 }
242 else
243 {
244 if (NULL == (bnc = zlookup_query_ipv6 (&attr->extra->mp_nexthop_global)))
245 bnc = bnc_new ();
246 else
247 {
248 if (changed)
249 {
250 struct bgp_table *old;
251 struct bgp_node *oldrn;
252
253 if (bgp_nexthop_cache_table[AFI_IP6] == cache1_table[AFI_IP6])
254 old = cache2_table[AFI_IP6];
255 else
256 old = cache1_table[AFI_IP6];
257
258 oldrn = bgp_node_lookup (old, &p);
259 if (oldrn)
260 {
261 struct bgp_nexthop_cache *oldbnc = oldrn->info;
262
263 bnc->changed = bgp_nexthop_cache_different (bnc, oldbnc);
264
265 if (bnc->metric != oldbnc->metric)
266 bnc->metricchanged = 1;
267
268 bgp_unlock_node (oldrn);
269 }
270 }
271 }
272 rn->info = bnc;
273 }
274
275 if (changed)
276 *changed = bnc->changed;
277
278 if (metricchanged)
279 *metricchanged = bnc->metricchanged;
280
281 if (bnc->valid && bnc->metric)
282 (bgp_info_extra_get (ri))->igpmetric = bnc->metric;
283 else if (ri->extra)
284 ri->extra->igpmetric = 0;
285
286 return bnc->valid;
287 }
288 #endif /* HAVE_IPV6 */
289
290 /* Check specified next-hop is reachable or not. */
291 int
292 bgp_nexthop_lookup (afi_t afi, struct peer *peer, struct bgp_info *ri,
293 int *changed, int *metricchanged)
294 {
295 struct bgp_node *rn;
296 struct prefix p;
297 struct bgp_nexthop_cache *bnc;
298 struct in_addr addr;
299
300 #ifdef HAVE_IPV6
301 if (afi == AFI_IP6)
302 return bgp_nexthop_lookup_ipv6 (peer, ri, changed, metricchanged);
303 #endif /* HAVE_IPV6 */
304
305 addr = ri->attr->nexthop;
306
307 memset (&p, 0, sizeof (struct prefix));
308 p.family = AF_INET;
309 p.prefixlen = IPV4_MAX_BITLEN;
310 p.u.prefix4 = addr;
311
312 /* IBGP or ebgp-multihop */
313 rn = bgp_node_get (bgp_nexthop_cache_table[AFI_IP], &p);
314
315 if (rn->info)
316 {
317 bnc = rn->info;
318 bgp_unlock_node (rn);
319 }
320 else
321 {
322 if (NULL == (bnc = zlookup_query (addr)))
323 bnc = bnc_new ();
324 else
325 {
326 if (changed)
327 {
328 struct bgp_table *old;
329 struct bgp_node *oldrn;
330
331 if (bgp_nexthop_cache_table[AFI_IP] == cache1_table[AFI_IP])
332 old = cache2_table[AFI_IP];
333 else
334 old = cache1_table[AFI_IP];
335
336 oldrn = bgp_node_lookup (old, &p);
337 if (oldrn)
338 {
339 struct bgp_nexthop_cache *oldbnc = oldrn->info;
340
341 bnc->changed = bgp_nexthop_cache_different (bnc, oldbnc);
342
343 if (bnc->metric != oldbnc->metric)
344 bnc->metricchanged = 1;
345
346 bgp_unlock_node (oldrn);
347 }
348 }
349 }
350 rn->info = bnc;
351 }
352
353 if (changed)
354 *changed = bnc->changed;
355
356 if (metricchanged)
357 *metricchanged = bnc->metricchanged;
358
359 if (bnc->valid && bnc->metric)
360 (bgp_info_extra_get(ri))->igpmetric = bnc->metric;
361 else if (ri->extra)
362 ri->extra->igpmetric = 0;
363
364 return bnc->valid;
365 }
366
367 /* Reset and free all BGP nexthop cache. */
368 static void
369 bgp_nexthop_cache_reset (struct bgp_table *table)
370 {
371 struct bgp_node *rn;
372 struct bgp_nexthop_cache *bnc;
373
374 for (rn = bgp_table_top (table); rn; rn = bgp_route_next (rn))
375 if ((bnc = rn->info) != NULL)
376 {
377 bnc_free (bnc);
378 rn->info = NULL;
379 bgp_unlock_node (rn);
380 }
381 }
382
383 static void
384 bgp_scan (afi_t afi, safi_t safi)
385 {
386 struct bgp_node *rn;
387 struct bgp *bgp;
388 struct bgp_info *bi;
389 struct bgp_info *next;
390 struct peer *peer;
391 struct listnode *node, *nnode;
392 int valid;
393 int current;
394 int changed;
395 int metricchanged;
396
397 /* Change cache. */
398 if (bgp_nexthop_cache_table[afi] == cache1_table[afi])
399 bgp_nexthop_cache_table[afi] = cache2_table[afi];
400 else
401 bgp_nexthop_cache_table[afi] = cache1_table[afi];
402
403 /* Get default bgp. */
404 bgp = bgp_get_default ();
405 if (bgp == NULL)
406 return;
407
408 /* Maximum prefix check */
409 for (ALL_LIST_ELEMENTS (bgp->peer, node, nnode, peer))
410 {
411 if (peer->status != Established)
412 continue;
413
414 if (peer->afc[afi][SAFI_UNICAST])
415 bgp_maximum_prefix_overflow (peer, afi, SAFI_UNICAST, 1);
416 if (peer->afc[afi][SAFI_MULTICAST])
417 bgp_maximum_prefix_overflow (peer, afi, SAFI_MULTICAST, 1);
418 if (peer->afc[afi][SAFI_MPLS_VPN])
419 bgp_maximum_prefix_overflow (peer, afi, SAFI_MPLS_VPN, 1);
420 }
421
422 for (rn = bgp_table_top (bgp->rib[afi][SAFI_UNICAST]); rn;
423 rn = bgp_route_next (rn))
424 {
425 for (bi = rn->info; bi; bi = next)
426 {
427 next = bi->next;
428
429 if (bi->type == ZEBRA_ROUTE_BGP && bi->sub_type == BGP_ROUTE_NORMAL)
430 {
431 changed = 0;
432 metricchanged = 0;
433
434 if (peer_sort (bi->peer) == BGP_PEER_EBGP && bi->peer->ttl == 1)
435 valid = bgp_nexthop_onlink (afi, bi->attr);
436 else
437 valid = bgp_nexthop_lookup (afi, bi->peer, bi,
438 &changed, &metricchanged);
439
440 current = CHECK_FLAG (bi->flags, BGP_INFO_VALID) ? 1 : 0;
441
442 if (changed)
443 SET_FLAG (bi->flags, BGP_INFO_IGP_CHANGED);
444 else
445 UNSET_FLAG (bi->flags, BGP_INFO_IGP_CHANGED);
446
447 if (valid != current)
448 {
449 if (CHECK_FLAG (bi->flags, BGP_INFO_VALID))
450 {
451 bgp_aggregate_decrement (bgp, &rn->p, bi,
452 afi, SAFI_UNICAST);
453 bgp_info_unset_flag (rn, bi, BGP_INFO_VALID);
454 }
455 else
456 {
457 bgp_info_set_flag (rn, bi, BGP_INFO_VALID);
458 bgp_aggregate_increment (bgp, &rn->p, bi,
459 afi, SAFI_UNICAST);
460 }
461 }
462
463 if (CHECK_FLAG (bgp->af_flags[afi][SAFI_UNICAST],
464 BGP_CONFIG_DAMPENING)
465 && bi->extra && bi->extra->damp_info )
466 if (bgp_damp_scan (bi, afi, SAFI_UNICAST))
467 bgp_aggregate_increment (bgp, &rn->p, bi,
468 afi, SAFI_UNICAST);
469 }
470 }
471 bgp_process (bgp, rn, afi, SAFI_UNICAST);
472 }
473
474 /* Flash old cache. */
475 if (bgp_nexthop_cache_table[afi] == cache1_table[afi])
476 bgp_nexthop_cache_reset (cache2_table[afi]);
477 else
478 bgp_nexthop_cache_reset (cache1_table[afi]);
479
480 if (BGP_DEBUG (events, EVENTS))
481 {
482 if (afi == AFI_IP)
483 zlog_debug ("scanning IPv4 Unicast routing tables");
484 else if (afi == AFI_IP6)
485 zlog_debug ("scanning IPv6 Unicast routing tables");
486 }
487 }
488
489 /* BGP scan thread. This thread check nexthop reachability. */
490 static int
491 bgp_scan_timer (struct thread *t)
492 {
493 bgp_scan_thread =
494 thread_add_timer (master, bgp_scan_timer, NULL, bgp_scan_interval);
495
496 if (BGP_DEBUG (events, EVENTS))
497 zlog_debug ("Performing BGP general scanning");
498
499 bgp_scan (AFI_IP, SAFI_UNICAST);
500
501 #ifdef HAVE_IPV6
502 bgp_scan (AFI_IP6, SAFI_UNICAST);
503 #endif /* HAVE_IPV6 */
504
505 return 0;
506 }
507 \f
508 struct bgp_connected_ref
509 {
510 unsigned int refcnt;
511 };
512
513 void
514 bgp_connected_add (struct connected *ifc)
515 {
516 struct prefix p;
517 struct prefix *addr;
518 struct interface *ifp;
519 struct bgp_node *rn;
520 struct bgp_connected_ref *bc;
521
522 ifp = ifc->ifp;
523
524 if (! ifp)
525 return;
526
527 if (if_is_loopback (ifp))
528 return;
529
530 addr = ifc->address;
531
532 if (addr->family == AF_INET)
533 {
534 PREFIX_COPY_IPV4(&p, CONNECTED_PREFIX(ifc));
535 apply_mask_ipv4 ((struct prefix_ipv4 *) &p);
536
537 if (prefix_ipv4_any ((struct prefix_ipv4 *) &p))
538 return;
539
540 rn = bgp_node_get (bgp_connected_table[AFI_IP], (struct prefix *) &p);
541 if (rn->info)
542 {
543 bc = rn->info;
544 bc->refcnt++;
545 }
546 else
547 {
548 bc = XCALLOC (MTYPE_BGP_CONN, sizeof (struct bgp_connected_ref));
549 bc->refcnt = 1;
550 rn->info = bc;
551 }
552 }
553 #ifdef HAVE_IPV6
554 else if (addr->family == AF_INET6)
555 {
556 PREFIX_COPY_IPV6(&p, CONNECTED_PREFIX(ifc));
557 apply_mask_ipv6 ((struct prefix_ipv6 *) &p);
558
559 if (IN6_IS_ADDR_UNSPECIFIED (&p.u.prefix6))
560 return;
561
562 if (IN6_IS_ADDR_LINKLOCAL (&p.u.prefix6))
563 return;
564
565 rn = bgp_node_get (bgp_connected_table[AFI_IP6], (struct prefix *) &p);
566 if (rn->info)
567 {
568 bc = rn->info;
569 bc->refcnt++;
570 }
571 else
572 {
573 bc = XCALLOC (MTYPE_BGP_CONN, sizeof (struct bgp_connected_ref));
574 bc->refcnt = 1;
575 rn->info = bc;
576 }
577 }
578 #endif /* HAVE_IPV6 */
579 }
580
581 void
582 bgp_connected_delete (struct connected *ifc)
583 {
584 struct prefix p;
585 struct prefix *addr;
586 struct interface *ifp;
587 struct bgp_node *rn;
588 struct bgp_connected_ref *bc;
589
590 ifp = ifc->ifp;
591
592 if (if_is_loopback (ifp))
593 return;
594
595 addr = ifc->address;
596
597 if (addr->family == AF_INET)
598 {
599 PREFIX_COPY_IPV4(&p, CONNECTED_PREFIX(ifc));
600 apply_mask_ipv4 ((struct prefix_ipv4 *) &p);
601
602 if (prefix_ipv4_any ((struct prefix_ipv4 *) &p))
603 return;
604
605 rn = bgp_node_lookup (bgp_connected_table[AFI_IP], &p);
606 if (! rn)
607 return;
608
609 bc = rn->info;
610 bc->refcnt--;
611 if (bc->refcnt == 0)
612 {
613 XFREE (MTYPE_BGP_CONN, bc);
614 rn->info = NULL;
615 }
616 bgp_unlock_node (rn);
617 bgp_unlock_node (rn);
618 }
619 #ifdef HAVE_IPV6
620 else if (addr->family == AF_INET6)
621 {
622 PREFIX_COPY_IPV6(&p, CONNECTED_PREFIX(ifc));
623 apply_mask_ipv6 ((struct prefix_ipv6 *) &p);
624
625 if (IN6_IS_ADDR_UNSPECIFIED (&p.u.prefix6))
626 return;
627
628 if (IN6_IS_ADDR_LINKLOCAL (&p.u.prefix6))
629 return;
630
631 rn = bgp_node_lookup (bgp_connected_table[AFI_IP6], (struct prefix *) &p);
632 if (! rn)
633 return;
634
635 bc = rn->info;
636 bc->refcnt--;
637 if (bc->refcnt == 0)
638 {
639 XFREE (MTYPE_BGP_CONN, bc);
640 rn->info = NULL;
641 }
642 bgp_unlock_node (rn);
643 bgp_unlock_node (rn);
644 }
645 #endif /* HAVE_IPV6 */
646 }
647
648 int
649 bgp_nexthop_self (afi_t afi, struct attr *attr)
650 {
651 struct listnode *node;
652 struct listnode *node2;
653 struct interface *ifp;
654 struct connected *ifc;
655 struct prefix *p;
656
657 for (ALL_LIST_ELEMENTS_RO (iflist, node, ifp))
658 {
659 for (ALL_LIST_ELEMENTS_RO (ifp->connected, node2, ifc))
660 {
661 p = ifc->address;
662
663 if (p && p->family == AF_INET
664 && IPV4_ADDR_SAME (&p->u.prefix4, &attr->nexthop))
665 return 1;
666 }
667 }
668 return 0;
669 }
670 \f
671 static struct bgp_nexthop_cache *
672 zlookup_read (void)
673 {
674 struct stream *s;
675 uint16_t length;
676 u_char marker;
677 u_char version;
678 uint16_t command;
679 int nbytes;
680 struct in_addr raddr;
681 uint32_t metric;
682 int i;
683 u_char nexthop_num;
684 struct nexthop *nexthop;
685 struct bgp_nexthop_cache *bnc;
686
687 s = zlookup->ibuf;
688 stream_reset (s);
689
690 nbytes = stream_read (s, zlookup->sock, 2);
691 length = stream_getw (s);
692
693 nbytes = stream_read (s, zlookup->sock, length - 2);
694 marker = stream_getc (s);
695 version = stream_getc (s);
696
697 if (version != ZSERV_VERSION || marker != ZEBRA_HEADER_MARKER)
698 {
699 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
700 __func__, zlookup->sock, marker, version);
701 return NULL;
702 }
703
704 command = stream_getw (s);
705
706 raddr.s_addr = stream_get_ipv4 (s);
707 metric = stream_getl (s);
708 nexthop_num = stream_getc (s);
709
710 if (nexthop_num)
711 {
712 bnc = bnc_new ();
713 bnc->valid = 1;
714 bnc->metric = metric;
715 bnc->nexthop_num = nexthop_num;
716
717 for (i = 0; i < nexthop_num; i++)
718 {
719 nexthop = XCALLOC (MTYPE_NEXTHOP, sizeof (struct nexthop));
720 nexthop->type = stream_getc (s);
721 switch (nexthop->type)
722 {
723 case ZEBRA_NEXTHOP_IPV4:
724 nexthop->gate.ipv4.s_addr = stream_get_ipv4 (s);
725 break;
726 case ZEBRA_NEXTHOP_IFINDEX:
727 case ZEBRA_NEXTHOP_IFNAME:
728 nexthop->ifindex = stream_getl (s);
729 break;
730 default:
731 /* do nothing */
732 break;
733 }
734 bnc_nexthop_add (bnc, nexthop);
735 }
736 }
737 else
738 return NULL;
739
740 return bnc;
741 }
742
743 struct bgp_nexthop_cache *
744 zlookup_query (struct in_addr addr)
745 {
746 int ret;
747 struct stream *s;
748
749 /* Check socket. */
750 if (zlookup->sock < 0)
751 return NULL;
752
753 s = zlookup->obuf;
754 stream_reset (s);
755 zclient_create_header (s, ZEBRA_IPV4_NEXTHOP_LOOKUP);
756 stream_put_in_addr (s, &addr);
757
758 stream_putw_at (s, 0, stream_get_endp (s));
759
760 ret = writen (zlookup->sock, s->data, stream_get_endp (s));
761 if (ret < 0)
762 {
763 zlog_err ("can't write to zlookup->sock");
764 close (zlookup->sock);
765 zlookup->sock = -1;
766 return NULL;
767 }
768 if (ret == 0)
769 {
770 zlog_err ("zlookup->sock connection closed");
771 close (zlookup->sock);
772 zlookup->sock = -1;
773 return NULL;
774 }
775
776 return zlookup_read ();
777 }
778
779 #ifdef HAVE_IPV6
780 static struct bgp_nexthop_cache *
781 zlookup_read_ipv6 (void)
782 {
783 struct stream *s;
784 uint16_t length;
785 u_char version, marker;
786 uint16_t command;
787 int nbytes;
788 struct in6_addr raddr;
789 uint32_t metric;
790 int i;
791 u_char nexthop_num;
792 struct nexthop *nexthop;
793 struct bgp_nexthop_cache *bnc;
794
795 s = zlookup->ibuf;
796 stream_reset (s);
797
798 nbytes = stream_read (s, zlookup->sock, 2);
799 length = stream_getw (s);
800
801 nbytes = stream_read (s, zlookup->sock, length - 2);
802 marker = stream_getc (s);
803 version = stream_getc (s);
804
805 if (version != ZSERV_VERSION || marker != ZEBRA_HEADER_MARKER)
806 {
807 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
808 __func__, zlookup->sock, marker, version);
809 return NULL;
810 }
811
812 command = stream_getw (s);
813
814 stream_get (&raddr, s, 16);
815
816 metric = stream_getl (s);
817 nexthop_num = stream_getc (s);
818
819 if (nexthop_num)
820 {
821 bnc = bnc_new ();
822 bnc->valid = 1;
823 bnc->metric = metric;
824 bnc->nexthop_num = nexthop_num;
825
826 for (i = 0; i < nexthop_num; i++)
827 {
828 nexthop = XCALLOC (MTYPE_NEXTHOP, sizeof (struct nexthop));
829 nexthop->type = stream_getc (s);
830 switch (nexthop->type)
831 {
832 case ZEBRA_NEXTHOP_IPV6:
833 stream_get (&nexthop->gate.ipv6, s, 16);
834 break;
835 case ZEBRA_NEXTHOP_IPV6_IFINDEX:
836 case ZEBRA_NEXTHOP_IPV6_IFNAME:
837 stream_get (&nexthop->gate.ipv6, s, 16);
838 nexthop->ifindex = stream_getl (s);
839 break;
840 case ZEBRA_NEXTHOP_IFINDEX:
841 case ZEBRA_NEXTHOP_IFNAME:
842 nexthop->ifindex = stream_getl (s);
843 break;
844 default:
845 /* do nothing */
846 break;
847 }
848 bnc_nexthop_add (bnc, nexthop);
849 }
850 }
851 else
852 return NULL;
853
854 return bnc;
855 }
856
857 struct bgp_nexthop_cache *
858 zlookup_query_ipv6 (struct in6_addr *addr)
859 {
860 int ret;
861 struct stream *s;
862
863 /* Check socket. */
864 if (zlookup->sock < 0)
865 return NULL;
866
867 s = zlookup->obuf;
868 stream_reset (s);
869 zclient_create_header (s, ZEBRA_IPV6_NEXTHOP_LOOKUP);
870 stream_put (s, addr, 16);
871 stream_putw_at (s, 0, stream_get_endp (s));
872
873 ret = writen (zlookup->sock, s->data, stream_get_endp (s));
874 if (ret < 0)
875 {
876 zlog_err ("can't write to zlookup->sock");
877 close (zlookup->sock);
878 zlookup->sock = -1;
879 return NULL;
880 }
881 if (ret == 0)
882 {
883 zlog_err ("zlookup->sock connection closed");
884 close (zlookup->sock);
885 zlookup->sock = -1;
886 return NULL;
887 }
888
889 return zlookup_read_ipv6 ();
890 }
891 #endif /* HAVE_IPV6 */
892
893 static int
894 bgp_import_check (struct prefix *p, u_int32_t *igpmetric,
895 struct in_addr *igpnexthop)
896 {
897 struct stream *s;
898 int ret;
899 u_int16_t length, command;
900 u_char version, marker;
901 int nbytes;
902 struct in_addr addr;
903 struct in_addr nexthop;
904 u_int32_t metric = 0;
905 u_char nexthop_num;
906 u_char nexthop_type;
907
908 /* If lookup connection is not available return valid. */
909 if (zlookup->sock < 0)
910 {
911 if (igpmetric)
912 *igpmetric = 0;
913 return 1;
914 }
915
916 /* Send query to the lookup connection */
917 s = zlookup->obuf;
918 stream_reset (s);
919 zclient_create_header (s, ZEBRA_IPV4_IMPORT_LOOKUP);
920
921 stream_putc (s, p->prefixlen);
922 stream_put_in_addr (s, &p->u.prefix4);
923
924 stream_putw_at (s, 0, stream_get_endp (s));
925
926 /* Write the packet. */
927 ret = writen (zlookup->sock, s->data, stream_get_endp (s));
928
929 if (ret < 0)
930 {
931 zlog_err ("can't write to zlookup->sock");
932 close (zlookup->sock);
933 zlookup->sock = -1;
934 return 1;
935 }
936 if (ret == 0)
937 {
938 zlog_err ("zlookup->sock connection closed");
939 close (zlookup->sock);
940 zlookup->sock = -1;
941 return 1;
942 }
943
944 /* Get result. */
945 stream_reset (s);
946
947 /* Fetch length. */
948 nbytes = stream_read (s, zlookup->sock, 2);
949 length = stream_getw (s);
950
951 /* Fetch whole data. */
952 nbytes = stream_read (s, zlookup->sock, length - 2);
953 marker = stream_getc (s);
954 version = stream_getc (s);
955
956 if (version != ZSERV_VERSION || marker != ZEBRA_HEADER_MARKER)
957 {
958 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
959 __func__, zlookup->sock, marker, version);
960 return 0;
961 }
962
963 command = stream_getw (s);
964
965 addr.s_addr = stream_get_ipv4 (s);
966 metric = stream_getl (s);
967 nexthop_num = stream_getc (s);
968
969 /* Set IGP metric value. */
970 if (igpmetric)
971 *igpmetric = metric;
972
973 /* If there is nexthop then this is active route. */
974 if (nexthop_num)
975 {
976 nexthop.s_addr = 0;
977 nexthop_type = stream_getc (s);
978 if (nexthop_type == ZEBRA_NEXTHOP_IPV4)
979 {
980 nexthop.s_addr = stream_get_ipv4 (s);
981 if (igpnexthop)
982 *igpnexthop = nexthop;
983 }
984 else
985 *igpnexthop = nexthop;
986
987 return 1;
988 }
989 else
990 return 0;
991 }
992
993 /* Scan all configured BGP route then check the route exists in IGP or
994 not. */
995 static int
996 bgp_import (struct thread *t)
997 {
998 struct bgp *bgp;
999 struct bgp_node *rn;
1000 struct bgp_static *bgp_static;
1001 struct listnode *node, *nnode;
1002 int valid;
1003 u_int32_t metric;
1004 struct in_addr nexthop;
1005 afi_t afi;
1006 safi_t safi;
1007
1008 bgp_import_thread =
1009 thread_add_timer (master, bgp_import, NULL, bgp_import_interval);
1010
1011 if (BGP_DEBUG (events, EVENTS))
1012 zlog_debug ("Import timer expired.");
1013
1014 for (ALL_LIST_ELEMENTS (bm->bgp, node, nnode, bgp))
1015 {
1016 for (afi = AFI_IP; afi < AFI_MAX; afi++)
1017 for (safi = SAFI_UNICAST; safi < SAFI_MPLS_VPN; safi++)
1018 for (rn = bgp_table_top (bgp->route[afi][safi]); rn;
1019 rn = bgp_route_next (rn))
1020 if ((bgp_static = rn->info) != NULL)
1021 {
1022 if (bgp_static->backdoor)
1023 continue;
1024
1025 valid = bgp_static->valid;
1026 metric = bgp_static->igpmetric;
1027 nexthop = bgp_static->igpnexthop;
1028
1029 if (bgp_flag_check (bgp, BGP_FLAG_IMPORT_CHECK)
1030 && afi == AFI_IP && safi == SAFI_UNICAST)
1031 bgp_static->valid = bgp_import_check (&rn->p, &bgp_static->igpmetric,
1032 &bgp_static->igpnexthop);
1033 else
1034 {
1035 bgp_static->valid = 1;
1036 bgp_static->igpmetric = 0;
1037 bgp_static->igpnexthop.s_addr = 0;
1038 }
1039
1040 if (bgp_static->valid != valid)
1041 {
1042 if (bgp_static->valid)
1043 bgp_static_update (bgp, &rn->p, bgp_static, afi, safi);
1044 else
1045 bgp_static_withdraw (bgp, &rn->p, afi, safi);
1046 }
1047 else if (bgp_static->valid)
1048 {
1049 if (bgp_static->igpmetric != metric
1050 || bgp_static->igpnexthop.s_addr != nexthop.s_addr
1051 || bgp_static->rmap.name)
1052 bgp_static_update (bgp, &rn->p, bgp_static, afi, safi);
1053 }
1054 }
1055 }
1056 return 0;
1057 }
1058
1059 /* Connect to zebra for nexthop lookup. */
1060 static int
1061 zlookup_connect (struct thread *t)
1062 {
1063 struct zclient *zlookup;
1064
1065 zlookup = THREAD_ARG (t);
1066 zlookup->t_connect = NULL;
1067
1068 if (zlookup->sock != -1)
1069 return 0;
1070
1071 if (zclient_socket_connect (zlookup) < 0)
1072 return -1;
1073
1074 return 0;
1075 }
1076
1077 /* Check specified multiaccess next-hop. */
1078 int
1079 bgp_multiaccess_check_v4 (struct in_addr nexthop, char *peer)
1080 {
1081 struct bgp_node *rn1;
1082 struct bgp_node *rn2;
1083 struct prefix p1;
1084 struct prefix p2;
1085 struct in_addr addr;
1086 int ret;
1087
1088 ret = inet_aton (peer, &addr);
1089 if (! ret)
1090 return 0;
1091
1092 memset (&p1, 0, sizeof (struct prefix));
1093 p1.family = AF_INET;
1094 p1.prefixlen = IPV4_MAX_BITLEN;
1095 p1.u.prefix4 = nexthop;
1096 memset (&p2, 0, sizeof (struct prefix));
1097 p2.family = AF_INET;
1098 p2.prefixlen = IPV4_MAX_BITLEN;
1099 p2.u.prefix4 = addr;
1100
1101 /* If bgp scan is not enabled, return invalid. */
1102 if (zlookup->sock < 0)
1103 return 0;
1104
1105 rn1 = bgp_node_match (bgp_connected_table[AFI_IP], &p1);
1106 if (! rn1)
1107 return 0;
1108 bgp_unlock_node (rn1);
1109
1110 rn2 = bgp_node_match (bgp_connected_table[AFI_IP], &p2);
1111 if (! rn2)
1112 return 0;
1113 bgp_unlock_node (rn2);
1114
1115 /* This is safe, even with above unlocks, since we are just
1116 comparing pointers to the objects, not the objects themselves. */
1117 if (rn1 == rn2)
1118 return 1;
1119
1120 return 0;
1121 }
1122 \f
1123 DEFUN (bgp_scan_time,
1124 bgp_scan_time_cmd,
1125 "bgp scan-time <5-60>",
1126 "BGP specific commands\n"
1127 "Configure background scanner interval\n"
1128 "Scanner interval (seconds)\n")
1129 {
1130 bgp_scan_interval = atoi (argv[0]);
1131
1132 if (bgp_scan_thread)
1133 {
1134 thread_cancel (bgp_scan_thread);
1135 bgp_scan_thread =
1136 thread_add_timer (master, bgp_scan_timer, NULL, bgp_scan_interval);
1137 }
1138
1139 return CMD_SUCCESS;
1140 }
1141
1142 DEFUN (no_bgp_scan_time,
1143 no_bgp_scan_time_cmd,
1144 "no bgp scan-time",
1145 NO_STR
1146 "BGP specific commands\n"
1147 "Configure background scanner interval\n")
1148 {
1149 bgp_scan_interval = BGP_SCAN_INTERVAL_DEFAULT;
1150
1151 if (bgp_scan_thread)
1152 {
1153 thread_cancel (bgp_scan_thread);
1154 bgp_scan_thread =
1155 thread_add_timer (master, bgp_scan_timer, NULL, bgp_scan_interval);
1156 }
1157
1158 return CMD_SUCCESS;
1159 }
1160
1161 ALIAS (no_bgp_scan_time,
1162 no_bgp_scan_time_val_cmd,
1163 "no bgp scan-time <5-60>",
1164 NO_STR
1165 "BGP specific commands\n"
1166 "Configure background scanner interval\n"
1167 "Scanner interval (seconds)\n")
1168
1169 static int
1170 show_ip_bgp_scan_tables (struct vty *vty, const char detail)
1171 {
1172 struct bgp_node *rn;
1173 struct bgp_nexthop_cache *bnc;
1174 char buf[INET6_ADDRSTRLEN];
1175 u_char i;
1176
1177 if (bgp_scan_thread)
1178 vty_out (vty, "BGP scan is running%s", VTY_NEWLINE);
1179 else
1180 vty_out (vty, "BGP scan is not running%s", VTY_NEWLINE);
1181 vty_out (vty, "BGP scan interval is %d%s", bgp_scan_interval, VTY_NEWLINE);
1182
1183 vty_out (vty, "Current BGP nexthop cache:%s", VTY_NEWLINE);
1184 for (rn = bgp_table_top (bgp_nexthop_cache_table[AFI_IP]); rn; rn = bgp_route_next (rn))
1185 if ((bnc = rn->info) != NULL)
1186 {
1187 if (bnc->valid)
1188 {
1189 vty_out (vty, " %s valid [IGP metric %d]%s",
1190 inet_ntop (AF_INET, &rn->p.u.prefix4, buf, INET6_ADDRSTRLEN), bnc->metric, VTY_NEWLINE);
1191 if (detail)
1192 for (i = 0; i < bnc->nexthop_num; i++)
1193 switch (bnc->nexthop[i].type)
1194 {
1195 case NEXTHOP_TYPE_IPV4:
1196 vty_out (vty, " gate %s%s", inet_ntop (AF_INET, &bnc->nexthop[i].gate.ipv4, buf, INET6_ADDRSTRLEN), VTY_NEWLINE);
1197 break;
1198 case NEXTHOP_TYPE_IFINDEX:
1199 vty_out (vty, " ifidx %u%s", bnc->nexthop[i].ifindex, VTY_NEWLINE);
1200 break;
1201 default:
1202 vty_out (vty, " invalid nexthop type %u%s", bnc->nexthop[i].type, VTY_NEWLINE);
1203 }
1204 }
1205 else
1206 vty_out (vty, " %s invalid%s",
1207 inet_ntop (AF_INET, &rn->p.u.prefix4, buf, INET6_ADDRSTRLEN), VTY_NEWLINE);
1208 }
1209
1210 #ifdef HAVE_IPV6
1211 {
1212 for (rn = bgp_table_top (bgp_nexthop_cache_table[AFI_IP6]);
1213 rn;
1214 rn = bgp_route_next (rn))
1215 if ((bnc = rn->info) != NULL)
1216 {
1217 if (bnc->valid)
1218 {
1219 vty_out (vty, " %s valid [IGP metric %d]%s",
1220 inet_ntop (AF_INET6, &rn->p.u.prefix6, buf, INET6_ADDRSTRLEN),
1221 bnc->metric, VTY_NEWLINE);
1222 if (detail)
1223 for (i = 0; i < bnc->nexthop_num; i++)
1224 switch (bnc->nexthop[i].type)
1225 {
1226 case NEXTHOP_TYPE_IPV6:
1227 vty_out (vty, " gate %s%s", inet_ntop (AF_INET6, &bnc->nexthop[i].gate.ipv6, buf, INET6_ADDRSTRLEN), VTY_NEWLINE);
1228 break;
1229 case NEXTHOP_TYPE_IFINDEX:
1230 vty_out (vty, " ifidx %u%s", bnc->nexthop[i].ifindex, VTY_NEWLINE);
1231 break;
1232 default:
1233 vty_out (vty, " invalid nexthop type %u%s", bnc->nexthop[i].type, VTY_NEWLINE);
1234 }
1235 }
1236 else
1237 vty_out (vty, " %s invalid%s",
1238 inet_ntop (AF_INET6, &rn->p.u.prefix6, buf, INET6_ADDRSTRLEN),
1239 VTY_NEWLINE);
1240 }
1241 }
1242 #endif /* HAVE_IPV6 */
1243
1244 vty_out (vty, "BGP connected route:%s", VTY_NEWLINE);
1245 for (rn = bgp_table_top (bgp_connected_table[AFI_IP]);
1246 rn;
1247 rn = bgp_route_next (rn))
1248 if (rn->info != NULL)
1249 vty_out (vty, " %s/%d%s", inet_ntoa (rn->p.u.prefix4), rn->p.prefixlen,
1250 VTY_NEWLINE);
1251
1252 #ifdef HAVE_IPV6
1253 {
1254 for (rn = bgp_table_top (bgp_connected_table[AFI_IP6]);
1255 rn;
1256 rn = bgp_route_next (rn))
1257 if (rn->info != NULL)
1258 vty_out (vty, " %s/%d%s",
1259 inet_ntop (AF_INET6, &rn->p.u.prefix6, buf, INET6_ADDRSTRLEN),
1260 rn->p.prefixlen,
1261 VTY_NEWLINE);
1262 }
1263 #endif /* HAVE_IPV6 */
1264
1265 return CMD_SUCCESS;
1266 }
1267
1268 DEFUN (show_ip_bgp_scan,
1269 show_ip_bgp_scan_cmd,
1270 "show ip bgp scan",
1271 SHOW_STR
1272 IP_STR
1273 BGP_STR
1274 "BGP scan status\n")
1275 {
1276 return show_ip_bgp_scan_tables (vty, 0);
1277 }
1278
1279 DEFUN (show_ip_bgp_scan_detail,
1280 show_ip_bgp_scan_detail_cmd,
1281 "show ip bgp scan detail",
1282 SHOW_STR
1283 IP_STR
1284 BGP_STR
1285 "BGP scan status\n"
1286 "More detailed output\n")
1287 {
1288 return show_ip_bgp_scan_tables (vty, 1);
1289 }
1290
1291 int
1292 bgp_config_write_scan_time (struct vty *vty)
1293 {
1294 if (bgp_scan_interval != BGP_SCAN_INTERVAL_DEFAULT)
1295 vty_out (vty, " bgp scan-time %d%s", bgp_scan_interval, VTY_NEWLINE);
1296 return CMD_SUCCESS;
1297 }
1298
1299 void
1300 bgp_scan_init (void)
1301 {
1302 zlookup = zclient_new ();
1303 zlookup->sock = -1;
1304 zlookup->t_connect = thread_add_event (master, zlookup_connect, zlookup, 0);
1305
1306 bgp_scan_interval = BGP_SCAN_INTERVAL_DEFAULT;
1307 bgp_import_interval = BGP_IMPORT_INTERVAL_DEFAULT;
1308
1309 cache1_table[AFI_IP] = bgp_table_init (AFI_IP, SAFI_UNICAST);
1310 cache2_table[AFI_IP] = bgp_table_init (AFI_IP, SAFI_UNICAST);
1311 bgp_nexthop_cache_table[AFI_IP] = cache1_table[AFI_IP];
1312
1313 bgp_connected_table[AFI_IP] = bgp_table_init (AFI_IP, SAFI_UNICAST);
1314
1315 #ifdef HAVE_IPV6
1316 cache1_table[AFI_IP6] = bgp_table_init (AFI_IP6, SAFI_UNICAST);
1317 cache2_table[AFI_IP6] = bgp_table_init (AFI_IP6, SAFI_UNICAST);
1318 bgp_nexthop_cache_table[AFI_IP6] = cache1_table[AFI_IP6];
1319 bgp_connected_table[AFI_IP6] = bgp_table_init (AFI_IP6, SAFI_UNICAST);
1320 #endif /* HAVE_IPV6 */
1321
1322 /* Make BGP scan thread. */
1323 bgp_scan_thread = thread_add_timer (master, bgp_scan_timer,
1324 NULL, bgp_scan_interval);
1325 /* Make BGP import there. */
1326 bgp_import_thread = thread_add_timer (master, bgp_import, NULL, 0);
1327
1328 install_element (BGP_NODE, &bgp_scan_time_cmd);
1329 install_element (BGP_NODE, &no_bgp_scan_time_cmd);
1330 install_element (BGP_NODE, &no_bgp_scan_time_val_cmd);
1331 install_element (VIEW_NODE, &show_ip_bgp_scan_cmd);
1332 install_element (VIEW_NODE, &show_ip_bgp_scan_detail_cmd);
1333 install_element (RESTRICTED_NODE, &show_ip_bgp_scan_cmd);
1334 install_element (ENABLE_NODE, &show_ip_bgp_scan_cmd);
1335 install_element (ENABLE_NODE, &show_ip_bgp_scan_detail_cmd);
1336 }
1337
1338 void
1339 bgp_scan_finish (void)
1340 {
1341 /* Only the current one needs to be reset. */
1342 bgp_nexthop_cache_reset (bgp_nexthop_cache_table[AFI_IP]);
1343
1344 bgp_table_unlock (cache1_table[AFI_IP]);
1345 cache1_table[AFI_IP] = NULL;
1346
1347 bgp_table_unlock (cache2_table[AFI_IP]);
1348 cache2_table[AFI_IP] = NULL;
1349
1350 bgp_table_unlock (bgp_connected_table[AFI_IP]);
1351 bgp_connected_table[AFI_IP] = NULL;
1352
1353 #ifdef HAVE_IPV6
1354 /* Only the current one needs to be reset. */
1355 bgp_nexthop_cache_reset (bgp_nexthop_cache_table[AFI_IP6]);
1356
1357 bgp_table_unlock (cache1_table[AFI_IP6]);
1358 cache1_table[AFI_IP6] = NULL;
1359
1360 bgp_table_unlock (cache2_table[AFI_IP6]);
1361 cache2_table[AFI_IP6] = NULL;
1362
1363 bgp_table_unlock (bgp_connected_table[AFI_IP6]);
1364 bgp_connected_table[AFI_IP6] = NULL;
1365 #endif /* HAVE_IPV6 */
1366 }