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