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