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