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