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]); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 510 | } |
| 511 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 512 | /* BGP scan thread. This thread check nexthop reachability. */ |
| 513 | int |
paul | 5932020 | 2004-11-09 01:54:03 +0000 | [diff] [blame] | 514 | bgp_scan_timer (struct thread *t) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 515 | { |
| 516 | bgp_scan_thread = |
paul | 5932020 | 2004-11-09 01:54:03 +0000 | [diff] [blame] | 517 | thread_add_timer (master, bgp_scan_timer, NULL, bgp_scan_interval); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 518 | |
| 519 | if (BGP_DEBUG (normal, NORMAL)) |
ajs | 478ba05 | 2004-12-08 20:41:23 +0000 | [diff] [blame] | 520 | zlog_debug ("Performing BGP general scanning"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 521 | |
paul | 5932020 | 2004-11-09 01:54:03 +0000 | [diff] [blame] | 522 | bgp_scan (AFI_IP, SAFI_UNICAST); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 523 | |
| 524 | #ifdef HAVE_IPV6 |
paul | 5932020 | 2004-11-09 01:54:03 +0000 | [diff] [blame] | 525 | bgp_scan (AFI_IP6, SAFI_UNICAST); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 526 | #endif /* HAVE_IPV6 */ |
| 527 | |
| 528 | return 0; |
| 529 | } |
| 530 | |
paul | 5932020 | 2004-11-09 01:54:03 +0000 | [diff] [blame] | 531 | struct bgp_connected_ref |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 532 | { |
| 533 | unsigned int refcnt; |
| 534 | }; |
| 535 | |
| 536 | void |
| 537 | bgp_connected_add (struct connected *ifc) |
| 538 | { |
| 539 | struct prefix p; |
| 540 | struct prefix *addr; |
| 541 | struct prefix *dest; |
| 542 | struct interface *ifp; |
| 543 | struct bgp_node *rn; |
paul | 5932020 | 2004-11-09 01:54:03 +0000 | [diff] [blame] | 544 | struct bgp_connected_ref *bc; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 545 | |
| 546 | ifp = ifc->ifp; |
| 547 | |
| 548 | if (! ifp) |
| 549 | return; |
| 550 | |
| 551 | if (if_is_loopback (ifp)) |
| 552 | return; |
| 553 | |
| 554 | addr = ifc->address; |
| 555 | dest = ifc->destination; |
| 556 | |
| 557 | if (addr->family == AF_INET) |
| 558 | { |
| 559 | memset (&p, 0, sizeof (struct prefix)); |
| 560 | p.family = AF_INET; |
| 561 | p.prefixlen = addr->prefixlen; |
| 562 | |
hasso | 3fb9cd6 | 2004-10-19 19:44:43 +0000 | [diff] [blame] | 563 | if (CONNECTED_POINTOPOINT_HOST(ifc)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 564 | p.u.prefix4 = dest->u.prefix4; |
| 565 | else |
| 566 | p.u.prefix4 = addr->u.prefix4; |
| 567 | |
| 568 | apply_mask_ipv4 ((struct prefix_ipv4 *) &p); |
| 569 | |
| 570 | if (prefix_ipv4_any ((struct prefix_ipv4 *) &p)) |
| 571 | return; |
| 572 | |
paul | 5932020 | 2004-11-09 01:54:03 +0000 | [diff] [blame] | 573 | rn = bgp_node_get (bgp_connected_table[AFI_IP], (struct prefix *) &p); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 574 | if (rn->info) |
| 575 | { |
| 576 | bc = rn->info; |
| 577 | bc->refcnt++; |
| 578 | } |
| 579 | else |
| 580 | { |
paul | 5932020 | 2004-11-09 01:54:03 +0000 | [diff] [blame] | 581 | bc = XMALLOC (0, sizeof (struct bgp_connected_ref)); |
| 582 | memset (bc, 0, sizeof (struct bgp_connected_ref)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 583 | bc->refcnt = 1; |
| 584 | rn->info = bc; |
| 585 | } |
| 586 | } |
| 587 | #ifdef HAVE_IPV6 |
| 588 | if (addr->family == AF_INET6) |
| 589 | { |
| 590 | memset (&p, 0, sizeof (struct prefix)); |
| 591 | p.family = AF_INET6; |
| 592 | p.prefixlen = addr->prefixlen; |
| 593 | |
hasso | 3fb9cd6 | 2004-10-19 19:44:43 +0000 | [diff] [blame] | 594 | if (if_is_pointopoint (ifp) && dest) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 595 | p.u.prefix6 = dest->u.prefix6; |
| 596 | else |
| 597 | p.u.prefix6 = addr->u.prefix6; |
| 598 | |
| 599 | apply_mask_ipv6 ((struct prefix_ipv6 *) &p); |
| 600 | |
| 601 | if (IN6_IS_ADDR_UNSPECIFIED (&p.u.prefix6)) |
| 602 | return; |
| 603 | |
| 604 | if (IN6_IS_ADDR_LINKLOCAL (&p.u.prefix6)) |
| 605 | return; |
| 606 | |
paul | 5932020 | 2004-11-09 01:54:03 +0000 | [diff] [blame] | 607 | rn = bgp_node_get (bgp_connected_table[AFI_IP6], (struct prefix *) &p); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 608 | if (rn->info) |
| 609 | { |
| 610 | bc = rn->info; |
| 611 | bc->refcnt++; |
| 612 | } |
| 613 | else |
| 614 | { |
paul | 5932020 | 2004-11-09 01:54:03 +0000 | [diff] [blame] | 615 | bc = XMALLOC (0, sizeof (struct bgp_connected_ref)); |
| 616 | memset (bc, 0, sizeof (struct bgp_connected_ref)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 617 | bc->refcnt = 1; |
| 618 | rn->info = bc; |
| 619 | } |
| 620 | } |
| 621 | #endif /* HAVE_IPV6 */ |
| 622 | } |
| 623 | |
| 624 | void |
| 625 | bgp_connected_delete (struct connected *ifc) |
| 626 | { |
| 627 | struct prefix p; |
| 628 | struct prefix *addr; |
| 629 | struct prefix *dest; |
| 630 | struct interface *ifp; |
| 631 | struct bgp_node *rn; |
paul | 5932020 | 2004-11-09 01:54:03 +0000 | [diff] [blame] | 632 | struct bgp_connected_ref *bc; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 633 | |
| 634 | ifp = ifc->ifp; |
| 635 | |
| 636 | if (if_is_loopback (ifp)) |
| 637 | return; |
| 638 | |
| 639 | addr = ifc->address; |
| 640 | dest = ifc->destination; |
| 641 | |
| 642 | if (addr->family == AF_INET) |
| 643 | { |
| 644 | memset (&p, 0, sizeof (struct prefix)); |
| 645 | p.family = AF_INET; |
| 646 | p.prefixlen = addr->prefixlen; |
| 647 | |
hasso | 3fb9cd6 | 2004-10-19 19:44:43 +0000 | [diff] [blame] | 648 | if (CONNECTED_POINTOPOINT_HOST(ifc)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 649 | p.u.prefix4 = dest->u.prefix4; |
| 650 | else |
| 651 | p.u.prefix4 = addr->u.prefix4; |
| 652 | |
| 653 | apply_mask_ipv4 ((struct prefix_ipv4 *) &p); |
| 654 | |
| 655 | if (prefix_ipv4_any ((struct prefix_ipv4 *) &p)) |
| 656 | return; |
| 657 | |
paul | 5932020 | 2004-11-09 01:54:03 +0000 | [diff] [blame] | 658 | rn = bgp_node_lookup (bgp_connected_table[AFI_IP], &p); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 659 | if (! rn) |
| 660 | return; |
| 661 | |
| 662 | bc = rn->info; |
| 663 | bc->refcnt--; |
| 664 | if (bc->refcnt == 0) |
| 665 | { |
| 666 | XFREE (0, bc); |
| 667 | rn->info = NULL; |
| 668 | } |
| 669 | bgp_unlock_node (rn); |
| 670 | bgp_unlock_node (rn); |
| 671 | } |
| 672 | #ifdef HAVE_IPV6 |
| 673 | else if (addr->family == AF_INET6) |
| 674 | { |
| 675 | memset (&p, 0, sizeof (struct prefix)); |
| 676 | p.family = AF_INET6; |
| 677 | p.prefixlen = addr->prefixlen; |
| 678 | |
hasso | 3fb9cd6 | 2004-10-19 19:44:43 +0000 | [diff] [blame] | 679 | if (if_is_pointopoint (ifp) && dest) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 680 | p.u.prefix6 = dest->u.prefix6; |
| 681 | else |
| 682 | p.u.prefix6 = addr->u.prefix6; |
| 683 | |
| 684 | apply_mask_ipv6 ((struct prefix_ipv6 *) &p); |
| 685 | |
| 686 | if (IN6_IS_ADDR_UNSPECIFIED (&p.u.prefix6)) |
| 687 | return; |
| 688 | |
| 689 | if (IN6_IS_ADDR_LINKLOCAL (&p.u.prefix6)) |
| 690 | return; |
| 691 | |
paul | 5932020 | 2004-11-09 01:54:03 +0000 | [diff] [blame] | 692 | rn = bgp_node_lookup (bgp_connected_table[AFI_IP6], (struct prefix *) &p); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 693 | if (! rn) |
| 694 | return; |
| 695 | |
| 696 | bc = rn->info; |
| 697 | bc->refcnt--; |
| 698 | if (bc->refcnt == 0) |
| 699 | { |
| 700 | XFREE (0, bc); |
| 701 | rn->info = NULL; |
| 702 | } |
| 703 | bgp_unlock_node (rn); |
| 704 | bgp_unlock_node (rn); |
| 705 | } |
| 706 | #endif /* HAVE_IPV6 */ |
| 707 | } |
| 708 | |
| 709 | int |
| 710 | bgp_nexthop_self (afi_t afi, struct attr *attr) |
| 711 | { |
hasso | 52dc7ee | 2004-09-23 19:18:23 +0000 | [diff] [blame] | 712 | struct listnode *node; |
| 713 | struct listnode *node2; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 714 | struct interface *ifp; |
| 715 | struct connected *ifc; |
| 716 | struct prefix *p; |
| 717 | |
| 718 | for (node = listhead (iflist); node; nextnode (node)) |
| 719 | { |
| 720 | ifp = getdata (node); |
| 721 | |
| 722 | for (node2 = listhead (ifp->connected); node2; nextnode (node2)) |
| 723 | { |
| 724 | ifc = getdata (node2); |
| 725 | p = ifc->address; |
| 726 | |
| 727 | if (p && p->family == AF_INET |
| 728 | && IPV4_ADDR_SAME (&p->u.prefix4, &attr->nexthop)) |
| 729 | return 1; |
| 730 | } |
| 731 | } |
| 732 | return 0; |
| 733 | } |
| 734 | |
| 735 | struct bgp_nexthop_cache * |
| 736 | zlookup_read () |
| 737 | { |
| 738 | struct stream *s; |
| 739 | u_int16_t length; |
| 740 | u_char command; |
| 741 | int nbytes; |
| 742 | struct in_addr raddr; |
| 743 | u_int32_t metric; |
| 744 | int i; |
| 745 | u_char nexthop_num; |
| 746 | struct nexthop *nexthop; |
| 747 | struct bgp_nexthop_cache *bnc; |
| 748 | |
| 749 | s = zlookup->ibuf; |
| 750 | stream_reset (s); |
| 751 | |
| 752 | nbytes = stream_read (s, zlookup->sock, 2); |
| 753 | length = stream_getw (s); |
| 754 | |
| 755 | nbytes = stream_read (s, zlookup->sock, length - 2); |
| 756 | command = stream_getc (s); |
| 757 | raddr.s_addr = stream_get_ipv4 (s); |
| 758 | metric = stream_getl (s); |
| 759 | nexthop_num = stream_getc (s); |
| 760 | |
| 761 | if (nexthop_num) |
| 762 | { |
| 763 | bnc = bnc_new (); |
| 764 | bnc->valid = 1; |
| 765 | bnc->metric = metric; |
| 766 | bnc->nexthop_num = nexthop_num; |
| 767 | |
| 768 | for (i = 0; i < nexthop_num; i++) |
| 769 | { |
| 770 | nexthop = XMALLOC (MTYPE_NEXTHOP, sizeof (struct nexthop)); |
| 771 | memset (nexthop, 0, sizeof (struct nexthop)); |
| 772 | nexthop->type = stream_getc (s); |
| 773 | switch (nexthop->type) |
| 774 | { |
| 775 | case ZEBRA_NEXTHOP_IPV4: |
| 776 | nexthop->gate.ipv4.s_addr = stream_get_ipv4 (s); |
| 777 | break; |
| 778 | case ZEBRA_NEXTHOP_IFINDEX: |
| 779 | case ZEBRA_NEXTHOP_IFNAME: |
| 780 | nexthop->ifindex = stream_getl (s); |
| 781 | break; |
hasso | fa2b17e | 2004-03-04 17:45:00 +0000 | [diff] [blame] | 782 | default: |
| 783 | /* do nothing */ |
| 784 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 785 | } |
| 786 | bnc_nexthop_add (bnc, nexthop); |
| 787 | } |
| 788 | } |
| 789 | else |
| 790 | return NULL; |
| 791 | |
| 792 | return bnc; |
| 793 | } |
| 794 | |
| 795 | struct bgp_nexthop_cache * |
| 796 | zlookup_query (struct in_addr addr) |
| 797 | { |
| 798 | int ret; |
| 799 | struct stream *s; |
| 800 | |
| 801 | /* Check socket. */ |
| 802 | if (zlookup->sock < 0) |
| 803 | return NULL; |
| 804 | |
| 805 | s = zlookup->obuf; |
| 806 | stream_reset (s); |
| 807 | stream_putw (s, 7); |
| 808 | stream_putc (s, ZEBRA_IPV4_NEXTHOP_LOOKUP); |
| 809 | stream_put_in_addr (s, &addr); |
| 810 | |
| 811 | ret = writen (zlookup->sock, s->data, 7); |
| 812 | if (ret < 0) |
| 813 | { |
| 814 | zlog_err ("can't write to zlookup->sock"); |
| 815 | close (zlookup->sock); |
| 816 | zlookup->sock = -1; |
| 817 | return NULL; |
| 818 | } |
| 819 | if (ret == 0) |
| 820 | { |
| 821 | zlog_err ("zlookup->sock connection closed"); |
| 822 | close (zlookup->sock); |
| 823 | zlookup->sock = -1; |
| 824 | return NULL; |
| 825 | } |
| 826 | |
| 827 | return zlookup_read (); |
| 828 | } |
| 829 | |
| 830 | #ifdef HAVE_IPV6 |
| 831 | struct bgp_nexthop_cache * |
| 832 | zlookup_read_ipv6 () |
| 833 | { |
| 834 | struct stream *s; |
| 835 | u_int16_t length; |
| 836 | u_char command; |
| 837 | int nbytes; |
| 838 | struct in6_addr raddr; |
| 839 | u_int32_t metric; |
| 840 | int i; |
| 841 | u_char nexthop_num; |
| 842 | struct nexthop *nexthop; |
| 843 | struct bgp_nexthop_cache *bnc; |
| 844 | |
| 845 | s = zlookup->ibuf; |
| 846 | stream_reset (s); |
| 847 | |
| 848 | nbytes = stream_read (s, zlookup->sock, 2); |
| 849 | length = stream_getw (s); |
| 850 | |
| 851 | nbytes = stream_read (s, zlookup->sock, length - 2); |
| 852 | command = stream_getc (s); |
| 853 | |
| 854 | stream_get (&raddr, s, 16); |
| 855 | |
| 856 | metric = stream_getl (s); |
| 857 | nexthop_num = stream_getc (s); |
| 858 | |
| 859 | if (nexthop_num) |
| 860 | { |
| 861 | bnc = bnc_new (); |
| 862 | bnc->valid = 1; |
| 863 | bnc->metric = metric; |
| 864 | bnc->nexthop_num = nexthop_num; |
| 865 | |
| 866 | for (i = 0; i < nexthop_num; i++) |
| 867 | { |
| 868 | nexthop = XMALLOC (MTYPE_NEXTHOP, sizeof (struct nexthop)); |
| 869 | memset (nexthop, 0, sizeof (struct nexthop)); |
| 870 | nexthop->type = stream_getc (s); |
| 871 | switch (nexthop->type) |
| 872 | { |
| 873 | case ZEBRA_NEXTHOP_IPV6: |
| 874 | stream_get (&nexthop->gate.ipv6, s, 16); |
| 875 | break; |
| 876 | case ZEBRA_NEXTHOP_IPV6_IFINDEX: |
| 877 | case ZEBRA_NEXTHOP_IPV6_IFNAME: |
| 878 | stream_get (&nexthop->gate.ipv6, s, 16); |
| 879 | nexthop->ifindex = stream_getl (s); |
| 880 | break; |
| 881 | case ZEBRA_NEXTHOP_IFINDEX: |
| 882 | case ZEBRA_NEXTHOP_IFNAME: |
| 883 | nexthop->ifindex = stream_getl (s); |
| 884 | break; |
hasso | fa2b17e | 2004-03-04 17:45:00 +0000 | [diff] [blame] | 885 | default: |
| 886 | /* do nothing */ |
| 887 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 888 | } |
| 889 | bnc_nexthop_add (bnc, nexthop); |
| 890 | } |
| 891 | } |
| 892 | else |
| 893 | return NULL; |
| 894 | |
| 895 | return bnc; |
| 896 | } |
| 897 | |
| 898 | struct bgp_nexthop_cache * |
| 899 | zlookup_query_ipv6 (struct in6_addr *addr) |
| 900 | { |
| 901 | int ret; |
| 902 | struct stream *s; |
| 903 | |
| 904 | /* Check socket. */ |
| 905 | if (zlookup->sock < 0) |
| 906 | return NULL; |
| 907 | |
| 908 | s = zlookup->obuf; |
| 909 | stream_reset (s); |
| 910 | stream_putw (s, 19); |
| 911 | stream_putc (s, ZEBRA_IPV6_NEXTHOP_LOOKUP); |
| 912 | stream_put (s, addr, 16); |
| 913 | |
| 914 | ret = writen (zlookup->sock, s->data, 19); |
| 915 | if (ret < 0) |
| 916 | { |
| 917 | zlog_err ("can't write to zlookup->sock"); |
| 918 | close (zlookup->sock); |
| 919 | zlookup->sock = -1; |
| 920 | return NULL; |
| 921 | } |
| 922 | if (ret == 0) |
| 923 | { |
| 924 | zlog_err ("zlookup->sock connection closed"); |
| 925 | close (zlookup->sock); |
| 926 | zlookup->sock = -1; |
| 927 | return NULL; |
| 928 | } |
| 929 | |
| 930 | return zlookup_read_ipv6 (); |
| 931 | } |
| 932 | #endif /* HAVE_IPV6 */ |
| 933 | |
| 934 | int |
| 935 | bgp_import_check (struct prefix *p, u_int32_t *igpmetric, struct in_addr *igpnexthop) |
| 936 | { |
| 937 | struct stream *s; |
| 938 | int ret; |
| 939 | u_int16_t length; |
| 940 | u_char command; |
| 941 | int nbytes; |
| 942 | struct in_addr addr; |
| 943 | struct in_addr nexthop; |
| 944 | u_int32_t metric = 0; |
| 945 | u_char nexthop_num; |
| 946 | u_char nexthop_type; |
| 947 | |
| 948 | /* If lookup connection is not available return valid. */ |
| 949 | if (zlookup->sock < 0) |
| 950 | { |
| 951 | if (igpmetric) |
| 952 | *igpmetric = 0; |
| 953 | return 1; |
| 954 | } |
| 955 | |
| 956 | /* Send query to the lookup connection */ |
| 957 | s = zlookup->obuf; |
| 958 | stream_reset (s); |
| 959 | stream_putw (s, 8); |
| 960 | stream_putc (s, ZEBRA_IPV4_IMPORT_LOOKUP); |
| 961 | stream_putc (s, p->prefixlen); |
| 962 | stream_put_in_addr (s, &p->u.prefix4); |
| 963 | |
| 964 | /* Write the packet. */ |
| 965 | ret = writen (zlookup->sock, s->data, 8); |
| 966 | |
| 967 | if (ret < 0) |
| 968 | { |
| 969 | zlog_err ("can't write to zlookup->sock"); |
| 970 | close (zlookup->sock); |
| 971 | zlookup->sock = -1; |
| 972 | return 1; |
| 973 | } |
| 974 | if (ret == 0) |
| 975 | { |
| 976 | zlog_err ("zlookup->sock connection closed"); |
| 977 | close (zlookup->sock); |
| 978 | zlookup->sock = -1; |
| 979 | return 1; |
| 980 | } |
| 981 | |
| 982 | /* Get result. */ |
| 983 | stream_reset (s); |
| 984 | |
| 985 | /* Fetch length. */ |
| 986 | nbytes = stream_read (s, zlookup->sock, 2); |
| 987 | length = stream_getw (s); |
| 988 | |
| 989 | /* Fetch whole data. */ |
| 990 | nbytes = stream_read (s, zlookup->sock, length - 2); |
| 991 | command = stream_getc (s); |
| 992 | addr.s_addr = stream_get_ipv4 (s); |
| 993 | metric = stream_getl (s); |
| 994 | nexthop_num = stream_getc (s); |
| 995 | |
| 996 | /* Set IGP metric value. */ |
| 997 | if (igpmetric) |
| 998 | *igpmetric = metric; |
| 999 | |
| 1000 | /* If there is nexthop then this is active route. */ |
| 1001 | if (nexthop_num) |
| 1002 | { |
| 1003 | nexthop.s_addr = 0; |
| 1004 | nexthop_type = stream_getc (s); |
| 1005 | if (nexthop_type == ZEBRA_NEXTHOP_IPV4) |
| 1006 | { |
| 1007 | nexthop.s_addr = stream_get_ipv4 (s); |
| 1008 | if (igpnexthop) |
| 1009 | *igpnexthop = nexthop; |
| 1010 | } |
| 1011 | else |
| 1012 | *igpnexthop = nexthop; |
| 1013 | |
| 1014 | return 1; |
| 1015 | } |
| 1016 | else |
| 1017 | return 0; |
| 1018 | } |
| 1019 | |
| 1020 | /* Scan all configured BGP route then check the route exists in IGP or |
| 1021 | not. */ |
| 1022 | int |
| 1023 | bgp_import (struct thread *t) |
| 1024 | { |
paul | 6cbbc3c | 2003-04-28 17:11:02 +0000 | [diff] [blame] | 1025 | struct bgp_master *bm; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1026 | struct bgp *bgp; |
| 1027 | struct bgp_node *rn; |
| 1028 | struct bgp_static *bgp_static; |
paul | 6cbbc3c | 2003-04-28 17:11:02 +0000 | [diff] [blame] | 1029 | struct listnode *nn; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1030 | int valid; |
| 1031 | u_int32_t metric; |
| 1032 | struct in_addr nexthop; |
| 1033 | afi_t afi; |
| 1034 | safi_t safi; |
| 1035 | |
| 1036 | bgp_import_thread = |
| 1037 | thread_add_timer (master, bgp_import, NULL, bgp_import_interval); |
| 1038 | |
paul | 6cbbc3c | 2003-04-28 17:11:02 +0000 | [diff] [blame] | 1039 | bm = bgp_get_master (); |
| 1040 | if (! bm) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1041 | return 0; |
| 1042 | |
paul | 6cbbc3c | 2003-04-28 17:11:02 +0000 | [diff] [blame] | 1043 | LIST_LOOP (bm->bgp, bgp, nn) |
| 1044 | { |
| 1045 | for (afi = AFI_IP; afi < AFI_MAX; afi++) |
| 1046 | for (safi = SAFI_UNICAST; safi < SAFI_MPLS_VPN; safi++) |
| 1047 | for (rn = bgp_table_top (bgp->route[afi][safi]); rn; |
| 1048 | rn = bgp_route_next (rn)) |
| 1049 | if ((bgp_static = rn->info) != NULL) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1050 | { |
paul | 6cbbc3c | 2003-04-28 17:11:02 +0000 | [diff] [blame] | 1051 | if (bgp_static->backdoor) |
| 1052 | continue; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1053 | |
paul | 6cbbc3c | 2003-04-28 17:11:02 +0000 | [diff] [blame] | 1054 | valid = bgp_static->valid; |
| 1055 | metric = bgp_static->igpmetric; |
| 1056 | nexthop = bgp_static->igpnexthop; |
| 1057 | |
| 1058 | if (bgp_flag_check (bgp, BGP_FLAG_IMPORT_CHECK) |
| 1059 | && afi == AFI_IP && safi == SAFI_UNICAST) |
| 1060 | bgp_static->valid = bgp_import_check (&rn->p, &bgp_static->igpmetric, |
| 1061 | &bgp_static->igpnexthop); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1062 | else |
paul | 6cbbc3c | 2003-04-28 17:11:02 +0000 | [diff] [blame] | 1063 | { |
| 1064 | bgp_static->valid = 1; |
| 1065 | bgp_static->igpmetric = 0; |
| 1066 | bgp_static->igpnexthop.s_addr = 0; |
| 1067 | } |
| 1068 | |
| 1069 | if (bgp_static->valid != valid) |
| 1070 | { |
| 1071 | if (bgp_static->valid) |
| 1072 | bgp_static_update (bgp, &rn->p, bgp_static, afi, safi); |
| 1073 | else |
| 1074 | bgp_static_withdraw (bgp, &rn->p, afi, safi); |
| 1075 | } |
| 1076 | else if (bgp_static->valid) |
| 1077 | { |
| 1078 | if (bgp_static->igpmetric != metric |
| 1079 | || bgp_static->igpnexthop.s_addr != nexthop.s_addr |
| 1080 | || bgp_static->rmap.name) |
| 1081 | bgp_static_update (bgp, &rn->p, bgp_static, afi, safi); |
| 1082 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1083 | } |
paul | 6cbbc3c | 2003-04-28 17:11:02 +0000 | [diff] [blame] | 1084 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1085 | return 0; |
| 1086 | } |
| 1087 | |
| 1088 | /* Connect to zebra for nexthop lookup. */ |
| 1089 | int |
| 1090 | zlookup_connect (struct thread *t) |
| 1091 | { |
| 1092 | struct zclient *zlookup; |
| 1093 | |
| 1094 | zlookup = THREAD_ARG (t); |
| 1095 | zlookup->t_connect = NULL; |
| 1096 | |
| 1097 | if (zlookup->sock != -1) |
| 1098 | return 0; |
| 1099 | |
| 1100 | #ifdef HAVE_TCP_ZEBRA |
| 1101 | zlookup->sock = zclient_socket (); |
| 1102 | #else |
| 1103 | zlookup->sock = zclient_socket_un (ZEBRA_SERV_PATH); |
| 1104 | #endif /* HAVE_TCP_ZEBRA */ |
| 1105 | if (zlookup->sock < 0) |
| 1106 | return -1; |
| 1107 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1108 | return 0; |
| 1109 | } |
| 1110 | |
| 1111 | /* Check specified multiaccess next-hop. */ |
| 1112 | int |
| 1113 | bgp_multiaccess_check_v4 (struct in_addr nexthop, char *peer) |
| 1114 | { |
| 1115 | struct bgp_node *rn1; |
| 1116 | struct bgp_node *rn2; |
| 1117 | struct prefix p1; |
| 1118 | struct prefix p2; |
| 1119 | struct in_addr addr; |
| 1120 | int ret; |
| 1121 | |
| 1122 | ret = inet_aton (peer, &addr); |
| 1123 | if (! ret) |
| 1124 | return 0; |
| 1125 | |
| 1126 | memset (&p1, 0, sizeof (struct prefix)); |
| 1127 | p1.family = AF_INET; |
| 1128 | p1.prefixlen = IPV4_MAX_BITLEN; |
| 1129 | p1.u.prefix4 = nexthop; |
| 1130 | memset (&p2, 0, sizeof (struct prefix)); |
| 1131 | p2.family = AF_INET; |
| 1132 | p2.prefixlen = IPV4_MAX_BITLEN; |
| 1133 | p2.u.prefix4 = addr; |
| 1134 | |
| 1135 | /* If bgp scan is not enabled, return invalid. */ |
| 1136 | if (zlookup->sock < 0) |
| 1137 | return 0; |
| 1138 | |
paul | 5932020 | 2004-11-09 01:54:03 +0000 | [diff] [blame] | 1139 | rn1 = bgp_node_match (bgp_connected_table[AFI_IP], &p1); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1140 | if (! rn1) |
| 1141 | return 0; |
| 1142 | |
paul | 5932020 | 2004-11-09 01:54:03 +0000 | [diff] [blame] | 1143 | rn2 = bgp_node_match (bgp_connected_table[AFI_IP], &p2); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1144 | if (! rn2) |
| 1145 | return 0; |
| 1146 | |
| 1147 | if (rn1 == rn2) |
| 1148 | return 1; |
| 1149 | |
| 1150 | return 0; |
| 1151 | } |
| 1152 | |
| 1153 | DEFUN (bgp_scan_time, |
| 1154 | bgp_scan_time_cmd, |
| 1155 | "bgp scan-time <5-60>", |
| 1156 | "BGP specific commands\n" |
| 1157 | "Configure background scanner interval\n" |
| 1158 | "Scanner interval (seconds)\n") |
| 1159 | { |
| 1160 | bgp_scan_interval = atoi (argv[0]); |
| 1161 | |
| 1162 | if (bgp_scan_thread) |
| 1163 | { |
| 1164 | thread_cancel (bgp_scan_thread); |
| 1165 | bgp_scan_thread = |
paul | 5932020 | 2004-11-09 01:54:03 +0000 | [diff] [blame] | 1166 | thread_add_timer (master, bgp_scan_timer, NULL, bgp_scan_interval); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1167 | } |
| 1168 | |
| 1169 | return CMD_SUCCESS; |
| 1170 | } |
| 1171 | |
| 1172 | DEFUN (no_bgp_scan_time, |
| 1173 | no_bgp_scan_time_cmd, |
| 1174 | "no bgp scan-time", |
| 1175 | NO_STR |
| 1176 | "BGP specific commands\n" |
| 1177 | "Configure background scanner interval\n") |
| 1178 | { |
| 1179 | bgp_scan_interval = BGP_SCAN_INTERVAL_DEFAULT; |
| 1180 | |
| 1181 | if (bgp_scan_thread) |
| 1182 | { |
| 1183 | thread_cancel (bgp_scan_thread); |
| 1184 | bgp_scan_thread = |
paul | 5932020 | 2004-11-09 01:54:03 +0000 | [diff] [blame] | 1185 | thread_add_timer (master, bgp_scan_timer, NULL, bgp_scan_interval); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1186 | } |
| 1187 | |
| 1188 | return CMD_SUCCESS; |
| 1189 | } |
| 1190 | |
| 1191 | ALIAS (no_bgp_scan_time, |
| 1192 | no_bgp_scan_time_val_cmd, |
| 1193 | "no bgp scan-time <5-60>", |
| 1194 | NO_STR |
| 1195 | "BGP specific commands\n" |
| 1196 | "Configure background scanner interval\n" |
| 1197 | "Scanner interval (seconds)\n") |
| 1198 | |
| 1199 | DEFUN (show_ip_bgp_scan, |
| 1200 | show_ip_bgp_scan_cmd, |
| 1201 | "show ip bgp scan", |
| 1202 | SHOW_STR |
| 1203 | IP_STR |
| 1204 | BGP_STR |
| 1205 | "BGP scan status\n") |
| 1206 | { |
| 1207 | struct bgp_node *rn; |
| 1208 | struct bgp_nexthop_cache *bnc; |
| 1209 | |
| 1210 | if (bgp_scan_thread) |
| 1211 | vty_out (vty, "BGP scan is running%s", VTY_NEWLINE); |
| 1212 | else |
| 1213 | vty_out (vty, "BGP scan is not running%s", VTY_NEWLINE); |
| 1214 | vty_out (vty, "BGP scan interval is %d%s", bgp_scan_interval, VTY_NEWLINE); |
| 1215 | |
| 1216 | vty_out (vty, "Current BGP nexthop cache:%s", VTY_NEWLINE); |
paul | 5932020 | 2004-11-09 01:54:03 +0000 | [diff] [blame] | 1217 | 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] | 1218 | if ((bnc = rn->info) != NULL) |
| 1219 | { |
| 1220 | if (bnc->valid) |
| 1221 | vty_out (vty, " %s valid [IGP metric %d]%s", |
| 1222 | inet_ntoa (rn->p.u.prefix4), bnc->metric, VTY_NEWLINE); |
| 1223 | else |
| 1224 | vty_out (vty, " %s invalid%s", |
| 1225 | inet_ntoa (rn->p.u.prefix4), VTY_NEWLINE); |
| 1226 | } |
| 1227 | |
| 1228 | #ifdef HAVE_IPV6 |
| 1229 | { |
| 1230 | char buf[BUFSIZ]; |
paul | 5932020 | 2004-11-09 01:54:03 +0000 | [diff] [blame] | 1231 | for (rn = bgp_table_top (bgp_nexthop_cache_table[AFI_IP6]); |
| 1232 | rn; |
| 1233 | rn = bgp_route_next (rn)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1234 | if ((bnc = rn->info) != NULL) |
| 1235 | { |
| 1236 | if (bnc->valid) |
| 1237 | vty_out (vty, " %s valid [IGP metric %d]%s", |
| 1238 | inet_ntop (AF_INET6, &rn->p.u.prefix6, buf, BUFSIZ), |
| 1239 | bnc->metric, VTY_NEWLINE); |
| 1240 | else |
| 1241 | vty_out (vty, " %s invalid%s", |
| 1242 | inet_ntop (AF_INET6, &rn->p.u.prefix6, buf, BUFSIZ), |
| 1243 | VTY_NEWLINE); |
| 1244 | } |
| 1245 | } |
| 1246 | #endif /* HAVE_IPV6 */ |
| 1247 | |
| 1248 | vty_out (vty, "BGP connected route:%s", VTY_NEWLINE); |
paul | 5932020 | 2004-11-09 01:54:03 +0000 | [diff] [blame] | 1249 | for (rn = bgp_table_top (bgp_connected_table[AFI_IP]); |
| 1250 | rn; |
| 1251 | rn = bgp_route_next (rn)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1252 | if (rn->info != NULL) |
| 1253 | vty_out (vty, " %s/%d%s", inet_ntoa (rn->p.u.prefix4), rn->p.prefixlen, |
| 1254 | VTY_NEWLINE); |
| 1255 | |
| 1256 | #ifdef HAVE_IPV6 |
| 1257 | { |
| 1258 | char buf[BUFSIZ]; |
| 1259 | |
paul | 5932020 | 2004-11-09 01:54:03 +0000 | [diff] [blame] | 1260 | for (rn = bgp_table_top (bgp_connected_table[AFI_IP6]); |
| 1261 | rn; |
| 1262 | rn = bgp_route_next (rn)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1263 | if (rn->info != NULL) |
| 1264 | vty_out (vty, " %s/%d%s", |
| 1265 | inet_ntop (AF_INET6, &rn->p.u.prefix6, buf, BUFSIZ), |
| 1266 | rn->p.prefixlen, |
| 1267 | VTY_NEWLINE); |
| 1268 | } |
| 1269 | #endif /* HAVE_IPV6 */ |
| 1270 | |
| 1271 | return CMD_SUCCESS; |
| 1272 | } |
| 1273 | |
| 1274 | int |
| 1275 | bgp_config_write_scan_time (struct vty *vty) |
| 1276 | { |
| 1277 | if (bgp_scan_interval != BGP_SCAN_INTERVAL_DEFAULT) |
| 1278 | vty_out (vty, " bgp scan-time %d%s", bgp_scan_interval, VTY_NEWLINE); |
| 1279 | return CMD_SUCCESS; |
| 1280 | } |
| 1281 | |
| 1282 | void |
| 1283 | bgp_scan_init () |
| 1284 | { |
| 1285 | zlookup = zclient_new (); |
| 1286 | zlookup->sock = -1; |
| 1287 | zlookup->ibuf = stream_new (ZEBRA_MAX_PACKET_SIZ); |
| 1288 | zlookup->obuf = stream_new (ZEBRA_MAX_PACKET_SIZ); |
| 1289 | zlookup->t_connect = thread_add_event (master, zlookup_connect, zlookup, 0); |
| 1290 | |
| 1291 | bgp_scan_interval = BGP_SCAN_INTERVAL_DEFAULT; |
| 1292 | bgp_import_interval = BGP_IMPORT_INTERVAL_DEFAULT; |
| 1293 | |
paul | 5932020 | 2004-11-09 01:54:03 +0000 | [diff] [blame] | 1294 | cache1_table[AFI_IP] = bgp_table_init (); |
| 1295 | cache2_table[AFI_IP] = bgp_table_init (); |
| 1296 | bgp_nexthop_cache_table[AFI_IP] = cache1_table[AFI_IP]; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1297 | |
paul | 5932020 | 2004-11-09 01:54:03 +0000 | [diff] [blame] | 1298 | bgp_connected_table[AFI_IP] = bgp_table_init (); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1299 | |
| 1300 | #ifdef HAVE_IPV6 |
paul | 5932020 | 2004-11-09 01:54:03 +0000 | [diff] [blame] | 1301 | cache1_table[AFI_IP6] = bgp_table_init (); |
| 1302 | cache2_table[AFI_IP6] = bgp_table_init (); |
| 1303 | bgp_nexthop_cache_table[AFI_IP6] = cache1_table[AFI_IP6]; |
| 1304 | bgp_connected_table[AFI_IP6] = bgp_table_init (); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1305 | #endif /* HAVE_IPV6 */ |
| 1306 | |
| 1307 | /* Make BGP scan thread. */ |
paul | 5932020 | 2004-11-09 01:54:03 +0000 | [diff] [blame] | 1308 | bgp_scan_thread = thread_add_timer (master, bgp_scan_timer, |
| 1309 | NULL, bgp_scan_interval); |
paul | 6cbbc3c | 2003-04-28 17:11:02 +0000 | [diff] [blame] | 1310 | /* Make BGP import there. */ |
| 1311 | bgp_import_thread = thread_add_timer (master, bgp_import, NULL, 0); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1312 | |
| 1313 | install_element (BGP_NODE, &bgp_scan_time_cmd); |
| 1314 | install_element (BGP_NODE, &no_bgp_scan_time_cmd); |
| 1315 | install_element (BGP_NODE, &no_bgp_scan_time_val_cmd); |
| 1316 | install_element (VIEW_NODE, &show_ip_bgp_scan_cmd); |
| 1317 | install_element (ENABLE_NODE, &show_ip_bgp_scan_cmd); |
| 1318 | } |