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