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