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; |
Chris Caputo | 6c88b44 | 2010-07-27 16:28:55 +0000 | [diff] [blame] | 279 | |
| 280 | bgp_unlock_node (oldrn); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 281 | } |
| 282 | } |
| 283 | } |
| 284 | else |
| 285 | { |
| 286 | bnc = bnc_new (); |
| 287 | bnc->valid = 0; |
| 288 | } |
| 289 | rn->info = bnc; |
| 290 | } |
| 291 | |
| 292 | if (changed) |
| 293 | *changed = bnc->changed; |
| 294 | |
| 295 | if (metricchanged) |
| 296 | *metricchanged = bnc->metricchanged; |
| 297 | |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 298 | if (bnc->valid && bnc->metric) |
| 299 | (bgp_info_extra_get (ri))->igpmetric = bnc->metric; |
| 300 | else if (ri->extra) |
| 301 | ri->extra->igpmetric = 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 302 | |
| 303 | return bnc->valid; |
| 304 | } |
| 305 | #endif /* HAVE_IPV6 */ |
| 306 | |
| 307 | /* Check specified next-hop is reachable or not. */ |
| 308 | int |
| 309 | bgp_nexthop_lookup (afi_t afi, struct peer *peer, struct bgp_info *ri, |
| 310 | int *changed, int *metricchanged) |
| 311 | { |
| 312 | struct bgp_node *rn; |
| 313 | struct prefix p; |
| 314 | struct bgp_nexthop_cache *bnc; |
| 315 | struct in_addr addr; |
| 316 | |
| 317 | /* If lookup is not enabled, return valid. */ |
| 318 | if (zlookup->sock < 0) |
| 319 | { |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 320 | if (ri->extra) |
| 321 | ri->extra->igpmetric = 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 322 | return 1; |
| 323 | } |
| 324 | |
| 325 | #ifdef HAVE_IPV6 |
| 326 | if (afi == AFI_IP6) |
| 327 | return bgp_nexthop_lookup_ipv6 (peer, ri, changed, metricchanged); |
| 328 | #endif /* HAVE_IPV6 */ |
| 329 | |
| 330 | addr = ri->attr->nexthop; |
| 331 | |
| 332 | memset (&p, 0, sizeof (struct prefix)); |
| 333 | p.family = AF_INET; |
| 334 | p.prefixlen = IPV4_MAX_BITLEN; |
| 335 | p.u.prefix4 = addr; |
| 336 | |
| 337 | /* IBGP or ebgp-multihop */ |
paul | 5932020 | 2004-11-09 01:54:03 +0000 | [diff] [blame] | 338 | rn = bgp_node_get (bgp_nexthop_cache_table[AFI_IP], &p); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 339 | |
| 340 | if (rn->info) |
| 341 | { |
| 342 | bnc = rn->info; |
| 343 | bgp_unlock_node (rn); |
| 344 | } |
| 345 | else |
| 346 | { |
| 347 | bnc = zlookup_query (addr); |
| 348 | if (bnc) |
| 349 | { |
| 350 | struct bgp_table *old; |
| 351 | struct bgp_node *oldrn; |
| 352 | struct bgp_nexthop_cache *oldbnc; |
| 353 | |
| 354 | if (changed) |
| 355 | { |
paul | 5932020 | 2004-11-09 01:54:03 +0000 | [diff] [blame] | 356 | if (bgp_nexthop_cache_table[AFI_IP] == cache1_table[AFI_IP]) |
| 357 | old = cache2_table[AFI_IP]; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 358 | else |
paul | 5932020 | 2004-11-09 01:54:03 +0000 | [diff] [blame] | 359 | old = cache1_table[AFI_IP]; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 360 | |
| 361 | oldrn = bgp_node_lookup (old, &p); |
| 362 | if (oldrn) |
| 363 | { |
| 364 | oldbnc = oldrn->info; |
| 365 | |
| 366 | bnc->changed = bgp_nexthop_cache_changed (bnc, oldbnc); |
| 367 | |
| 368 | if (bnc->metric != oldbnc->metric) |
| 369 | bnc->metricchanged = 1; |
Chris Caputo | 6c88b44 | 2010-07-27 16:28:55 +0000 | [diff] [blame] | 370 | |
| 371 | bgp_unlock_node (oldrn); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 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 | { |
Chris Caputo | 6c88b44 | 2010-07-27 16:28:55 +0000 | [diff] [blame] | 578 | bc = XCALLOC (MTYPE_BGP_CONN, sizeof (struct bgp_connected_ref)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 579 | bc->refcnt = 1; |
| 580 | rn->info = bc; |
| 581 | } |
| 582 | } |
| 583 | #ifdef HAVE_IPV6 |
Andrew J. Schorr | e452963 | 2006-12-12 19:18:21 +0000 | [diff] [blame] | 584 | else if (addr->family == AF_INET6) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 585 | { |
Andrew J. Schorr | e452963 | 2006-12-12 19:18:21 +0000 | [diff] [blame] | 586 | PREFIX_COPY_IPV6(&p, CONNECTED_PREFIX(ifc)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 587 | apply_mask_ipv6 ((struct prefix_ipv6 *) &p); |
| 588 | |
| 589 | if (IN6_IS_ADDR_UNSPECIFIED (&p.u.prefix6)) |
| 590 | return; |
| 591 | |
| 592 | if (IN6_IS_ADDR_LINKLOCAL (&p.u.prefix6)) |
| 593 | return; |
| 594 | |
paul | 5932020 | 2004-11-09 01:54:03 +0000 | [diff] [blame] | 595 | rn = bgp_node_get (bgp_connected_table[AFI_IP6], (struct prefix *) &p); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 596 | if (rn->info) |
| 597 | { |
| 598 | bc = rn->info; |
| 599 | bc->refcnt++; |
| 600 | } |
| 601 | else |
| 602 | { |
Chris Caputo | 6c88b44 | 2010-07-27 16:28:55 +0000 | [diff] [blame] | 603 | bc = XCALLOC (MTYPE_BGP_CONN, sizeof (struct bgp_connected_ref)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 604 | bc->refcnt = 1; |
| 605 | rn->info = bc; |
| 606 | } |
| 607 | } |
| 608 | #endif /* HAVE_IPV6 */ |
| 609 | } |
| 610 | |
| 611 | void |
| 612 | bgp_connected_delete (struct connected *ifc) |
| 613 | { |
| 614 | struct prefix p; |
| 615 | struct prefix *addr; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 616 | struct interface *ifp; |
| 617 | struct bgp_node *rn; |
paul | 5932020 | 2004-11-09 01:54:03 +0000 | [diff] [blame] | 618 | struct bgp_connected_ref *bc; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 619 | |
| 620 | ifp = ifc->ifp; |
| 621 | |
| 622 | if (if_is_loopback (ifp)) |
| 623 | return; |
| 624 | |
| 625 | addr = ifc->address; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 626 | |
| 627 | if (addr->family == AF_INET) |
| 628 | { |
Andrew J. Schorr | e452963 | 2006-12-12 19:18:21 +0000 | [diff] [blame] | 629 | PREFIX_COPY_IPV4(&p, CONNECTED_PREFIX(ifc)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 630 | apply_mask_ipv4 ((struct prefix_ipv4 *) &p); |
| 631 | |
| 632 | if (prefix_ipv4_any ((struct prefix_ipv4 *) &p)) |
| 633 | return; |
| 634 | |
paul | 5932020 | 2004-11-09 01:54:03 +0000 | [diff] [blame] | 635 | rn = bgp_node_lookup (bgp_connected_table[AFI_IP], &p); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 636 | if (! rn) |
| 637 | return; |
| 638 | |
| 639 | bc = rn->info; |
| 640 | bc->refcnt--; |
| 641 | if (bc->refcnt == 0) |
| 642 | { |
Chris Caputo | 6c88b44 | 2010-07-27 16:28:55 +0000 | [diff] [blame] | 643 | XFREE (MTYPE_BGP_CONN, bc); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 644 | rn->info = NULL; |
| 645 | } |
| 646 | bgp_unlock_node (rn); |
| 647 | bgp_unlock_node (rn); |
| 648 | } |
| 649 | #ifdef HAVE_IPV6 |
| 650 | else if (addr->family == AF_INET6) |
| 651 | { |
Andrew J. Schorr | e452963 | 2006-12-12 19:18:21 +0000 | [diff] [blame] | 652 | PREFIX_COPY_IPV6(&p, CONNECTED_PREFIX(ifc)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 653 | apply_mask_ipv6 ((struct prefix_ipv6 *) &p); |
| 654 | |
| 655 | if (IN6_IS_ADDR_UNSPECIFIED (&p.u.prefix6)) |
| 656 | return; |
| 657 | |
| 658 | if (IN6_IS_ADDR_LINKLOCAL (&p.u.prefix6)) |
| 659 | return; |
| 660 | |
paul | 5932020 | 2004-11-09 01:54:03 +0000 | [diff] [blame] | 661 | rn = bgp_node_lookup (bgp_connected_table[AFI_IP6], (struct prefix *) &p); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 662 | if (! rn) |
| 663 | return; |
| 664 | |
| 665 | bc = rn->info; |
| 666 | bc->refcnt--; |
| 667 | if (bc->refcnt == 0) |
| 668 | { |
Chris Caputo | 6c88b44 | 2010-07-27 16:28:55 +0000 | [diff] [blame] | 669 | XFREE (MTYPE_BGP_CONN, bc); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 670 | rn->info = NULL; |
| 671 | } |
| 672 | bgp_unlock_node (rn); |
| 673 | bgp_unlock_node (rn); |
| 674 | } |
| 675 | #endif /* HAVE_IPV6 */ |
| 676 | } |
| 677 | |
| 678 | int |
| 679 | bgp_nexthop_self (afi_t afi, struct attr *attr) |
| 680 | { |
hasso | 52dc7ee | 2004-09-23 19:18:23 +0000 | [diff] [blame] | 681 | struct listnode *node; |
| 682 | struct listnode *node2; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 683 | struct interface *ifp; |
| 684 | struct connected *ifc; |
| 685 | struct prefix *p; |
| 686 | |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 687 | for (ALL_LIST_ELEMENTS_RO (iflist, node, ifp)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 688 | { |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 689 | for (ALL_LIST_ELEMENTS_RO (ifp->connected, node2, ifc)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 690 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 691 | p = ifc->address; |
| 692 | |
| 693 | if (p && p->family == AF_INET |
| 694 | && IPV4_ADDR_SAME (&p->u.prefix4, &attr->nexthop)) |
| 695 | return 1; |
| 696 | } |
| 697 | } |
| 698 | return 0; |
| 699 | } |
| 700 | |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 701 | static struct bgp_nexthop_cache * |
Stephen Hemminger | 66e5cd8 | 2009-02-09 10:14:16 -0800 | [diff] [blame] | 702 | zlookup_read (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 703 | { |
| 704 | struct stream *s; |
paul | d3092e7 | 2006-01-17 17:33:46 +0000 | [diff] [blame] | 705 | uint16_t length; |
| 706 | u_char marker; |
| 707 | u_char version; |
| 708 | uint16_t command; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 709 | int nbytes; |
| 710 | struct in_addr raddr; |
paul | d3092e7 | 2006-01-17 17:33:46 +0000 | [diff] [blame] | 711 | uint32_t metric; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 712 | int i; |
| 713 | u_char nexthop_num; |
| 714 | struct nexthop *nexthop; |
| 715 | struct bgp_nexthop_cache *bnc; |
| 716 | |
| 717 | s = zlookup->ibuf; |
| 718 | stream_reset (s); |
| 719 | |
| 720 | nbytes = stream_read (s, zlookup->sock, 2); |
| 721 | length = stream_getw (s); |
| 722 | |
| 723 | nbytes = stream_read (s, zlookup->sock, length - 2); |
paul | d3092e7 | 2006-01-17 17:33:46 +0000 | [diff] [blame] | 724 | marker = stream_getc (s); |
| 725 | version = stream_getc (s); |
| 726 | |
| 727 | if (version != ZSERV_VERSION || marker != ZEBRA_HEADER_MARKER) |
| 728 | { |
| 729 | zlog_err("%s: socket %d version mismatch, marker %d, version %d", |
| 730 | __func__, zlookup->sock, marker, version); |
| 731 | return NULL; |
| 732 | } |
| 733 | |
| 734 | command = stream_getw (s); |
| 735 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 736 | raddr.s_addr = stream_get_ipv4 (s); |
| 737 | metric = stream_getl (s); |
| 738 | nexthop_num = stream_getc (s); |
| 739 | |
| 740 | if (nexthop_num) |
| 741 | { |
| 742 | bnc = bnc_new (); |
| 743 | bnc->valid = 1; |
| 744 | bnc->metric = metric; |
| 745 | bnc->nexthop_num = nexthop_num; |
| 746 | |
| 747 | for (i = 0; i < nexthop_num; i++) |
| 748 | { |
Stephen Hemminger | 393deb9 | 2008-08-18 14:13:29 -0700 | [diff] [blame] | 749 | nexthop = XCALLOC (MTYPE_NEXTHOP, sizeof (struct nexthop)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 750 | nexthop->type = stream_getc (s); |
| 751 | switch (nexthop->type) |
| 752 | { |
| 753 | case ZEBRA_NEXTHOP_IPV4: |
| 754 | nexthop->gate.ipv4.s_addr = stream_get_ipv4 (s); |
| 755 | break; |
| 756 | case ZEBRA_NEXTHOP_IFINDEX: |
| 757 | case ZEBRA_NEXTHOP_IFNAME: |
| 758 | nexthop->ifindex = stream_getl (s); |
| 759 | break; |
hasso | fa2b17e | 2004-03-04 17:45:00 +0000 | [diff] [blame] | 760 | default: |
| 761 | /* do nothing */ |
| 762 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 763 | } |
| 764 | bnc_nexthop_add (bnc, nexthop); |
| 765 | } |
| 766 | } |
| 767 | else |
| 768 | return NULL; |
| 769 | |
| 770 | return bnc; |
| 771 | } |
| 772 | |
| 773 | struct bgp_nexthop_cache * |
| 774 | zlookup_query (struct in_addr addr) |
| 775 | { |
| 776 | int ret; |
| 777 | struct stream *s; |
| 778 | |
| 779 | /* Check socket. */ |
| 780 | if (zlookup->sock < 0) |
| 781 | return NULL; |
| 782 | |
| 783 | s = zlookup->obuf; |
| 784 | stream_reset (s); |
paul | d3092e7 | 2006-01-17 17:33:46 +0000 | [diff] [blame] | 785 | zclient_create_header (s, ZEBRA_IPV4_NEXTHOP_LOOKUP); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 786 | stream_put_in_addr (s, &addr); |
paul | d3092e7 | 2006-01-17 17:33:46 +0000 | [diff] [blame] | 787 | |
| 788 | stream_putw_at (s, 0, stream_get_endp (s)); |
| 789 | |
| 790 | ret = writen (zlookup->sock, s->data, stream_get_endp (s)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 791 | if (ret < 0) |
| 792 | { |
| 793 | zlog_err ("can't write to zlookup->sock"); |
| 794 | close (zlookup->sock); |
| 795 | zlookup->sock = -1; |
| 796 | return NULL; |
| 797 | } |
| 798 | if (ret == 0) |
| 799 | { |
| 800 | zlog_err ("zlookup->sock connection closed"); |
| 801 | close (zlookup->sock); |
| 802 | zlookup->sock = -1; |
| 803 | return NULL; |
| 804 | } |
| 805 | |
| 806 | return zlookup_read (); |
| 807 | } |
| 808 | |
| 809 | #ifdef HAVE_IPV6 |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 810 | static struct bgp_nexthop_cache * |
Stephen Hemminger | 66e5cd8 | 2009-02-09 10:14:16 -0800 | [diff] [blame] | 811 | zlookup_read_ipv6 (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 812 | { |
| 813 | struct stream *s; |
paul | d3092e7 | 2006-01-17 17:33:46 +0000 | [diff] [blame] | 814 | uint16_t length; |
| 815 | u_char version, marker; |
| 816 | uint16_t command; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 817 | int nbytes; |
| 818 | struct in6_addr raddr; |
paul | d3092e7 | 2006-01-17 17:33:46 +0000 | [diff] [blame] | 819 | uint32_t metric; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 820 | int i; |
| 821 | u_char nexthop_num; |
| 822 | struct nexthop *nexthop; |
| 823 | struct bgp_nexthop_cache *bnc; |
| 824 | |
| 825 | s = zlookup->ibuf; |
| 826 | stream_reset (s); |
| 827 | |
| 828 | nbytes = stream_read (s, zlookup->sock, 2); |
| 829 | length = stream_getw (s); |
| 830 | |
| 831 | nbytes = stream_read (s, zlookup->sock, length - 2); |
paul | d3092e7 | 2006-01-17 17:33:46 +0000 | [diff] [blame] | 832 | marker = stream_getc (s); |
| 833 | version = stream_getc (s); |
| 834 | |
| 835 | if (version != ZSERV_VERSION || marker != ZEBRA_HEADER_MARKER) |
| 836 | { |
| 837 | zlog_err("%s: socket %d version mismatch, marker %d, version %d", |
| 838 | __func__, zlookup->sock, marker, version); |
| 839 | return NULL; |
| 840 | } |
| 841 | |
| 842 | command = stream_getw (s); |
| 843 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 844 | stream_get (&raddr, s, 16); |
| 845 | |
| 846 | metric = stream_getl (s); |
| 847 | nexthop_num = stream_getc (s); |
| 848 | |
| 849 | if (nexthop_num) |
| 850 | { |
| 851 | bnc = bnc_new (); |
| 852 | bnc->valid = 1; |
| 853 | bnc->metric = metric; |
| 854 | bnc->nexthop_num = nexthop_num; |
| 855 | |
| 856 | for (i = 0; i < nexthop_num; i++) |
| 857 | { |
Stephen Hemminger | 393deb9 | 2008-08-18 14:13:29 -0700 | [diff] [blame] | 858 | nexthop = XCALLOC (MTYPE_NEXTHOP, sizeof (struct nexthop)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 859 | nexthop->type = stream_getc (s); |
| 860 | switch (nexthop->type) |
| 861 | { |
| 862 | case ZEBRA_NEXTHOP_IPV6: |
| 863 | stream_get (&nexthop->gate.ipv6, s, 16); |
| 864 | break; |
| 865 | case ZEBRA_NEXTHOP_IPV6_IFINDEX: |
| 866 | case ZEBRA_NEXTHOP_IPV6_IFNAME: |
| 867 | stream_get (&nexthop->gate.ipv6, s, 16); |
| 868 | nexthop->ifindex = stream_getl (s); |
| 869 | break; |
| 870 | case ZEBRA_NEXTHOP_IFINDEX: |
| 871 | case ZEBRA_NEXTHOP_IFNAME: |
| 872 | nexthop->ifindex = stream_getl (s); |
| 873 | break; |
hasso | fa2b17e | 2004-03-04 17:45:00 +0000 | [diff] [blame] | 874 | default: |
| 875 | /* do nothing */ |
| 876 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 877 | } |
| 878 | bnc_nexthop_add (bnc, nexthop); |
| 879 | } |
| 880 | } |
| 881 | else |
| 882 | return NULL; |
| 883 | |
| 884 | return bnc; |
| 885 | } |
| 886 | |
| 887 | struct bgp_nexthop_cache * |
| 888 | zlookup_query_ipv6 (struct in6_addr *addr) |
| 889 | { |
| 890 | int ret; |
| 891 | struct stream *s; |
| 892 | |
| 893 | /* Check socket. */ |
| 894 | if (zlookup->sock < 0) |
| 895 | return NULL; |
| 896 | |
| 897 | s = zlookup->obuf; |
| 898 | stream_reset (s); |
paul | d3092e7 | 2006-01-17 17:33:46 +0000 | [diff] [blame] | 899 | zclient_create_header (s, ZEBRA_IPV6_NEXTHOP_LOOKUP); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 900 | stream_put (s, addr, 16); |
paul | d3092e7 | 2006-01-17 17:33:46 +0000 | [diff] [blame] | 901 | stream_putw_at (s, 0, stream_get_endp (s)); |
| 902 | |
| 903 | ret = writen (zlookup->sock, s->data, stream_get_endp (s)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 904 | if (ret < 0) |
| 905 | { |
| 906 | zlog_err ("can't write to zlookup->sock"); |
| 907 | close (zlookup->sock); |
| 908 | zlookup->sock = -1; |
| 909 | return NULL; |
| 910 | } |
| 911 | if (ret == 0) |
| 912 | { |
| 913 | zlog_err ("zlookup->sock connection closed"); |
| 914 | close (zlookup->sock); |
| 915 | zlookup->sock = -1; |
| 916 | return NULL; |
| 917 | } |
| 918 | |
| 919 | return zlookup_read_ipv6 (); |
| 920 | } |
| 921 | #endif /* HAVE_IPV6 */ |
| 922 | |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 923 | static int |
| 924 | bgp_import_check (struct prefix *p, u_int32_t *igpmetric, |
| 925 | struct in_addr *igpnexthop) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 926 | { |
| 927 | struct stream *s; |
| 928 | int ret; |
paul | d3092e7 | 2006-01-17 17:33:46 +0000 | [diff] [blame] | 929 | u_int16_t length, command; |
| 930 | u_char version, marker; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 931 | int nbytes; |
| 932 | struct in_addr addr; |
| 933 | struct in_addr nexthop; |
| 934 | u_int32_t metric = 0; |
| 935 | u_char nexthop_num; |
| 936 | u_char nexthop_type; |
| 937 | |
| 938 | /* If lookup connection is not available return valid. */ |
| 939 | if (zlookup->sock < 0) |
| 940 | { |
| 941 | if (igpmetric) |
| 942 | *igpmetric = 0; |
| 943 | return 1; |
| 944 | } |
| 945 | |
| 946 | /* Send query to the lookup connection */ |
| 947 | s = zlookup->obuf; |
| 948 | stream_reset (s); |
paul | d3092e7 | 2006-01-17 17:33:46 +0000 | [diff] [blame] | 949 | zclient_create_header (s, ZEBRA_IPV4_IMPORT_LOOKUP); |
| 950 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 951 | stream_putc (s, p->prefixlen); |
| 952 | stream_put_in_addr (s, &p->u.prefix4); |
paul | d3092e7 | 2006-01-17 17:33:46 +0000 | [diff] [blame] | 953 | |
| 954 | stream_putw_at (s, 0, stream_get_endp (s)); |
| 955 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 956 | /* Write the packet. */ |
paul | d3092e7 | 2006-01-17 17:33:46 +0000 | [diff] [blame] | 957 | ret = writen (zlookup->sock, s->data, stream_get_endp (s)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 958 | |
| 959 | if (ret < 0) |
| 960 | { |
| 961 | zlog_err ("can't write to zlookup->sock"); |
| 962 | close (zlookup->sock); |
| 963 | zlookup->sock = -1; |
| 964 | return 1; |
| 965 | } |
| 966 | if (ret == 0) |
| 967 | { |
| 968 | zlog_err ("zlookup->sock connection closed"); |
| 969 | close (zlookup->sock); |
| 970 | zlookup->sock = -1; |
| 971 | return 1; |
| 972 | } |
| 973 | |
| 974 | /* Get result. */ |
| 975 | stream_reset (s); |
| 976 | |
| 977 | /* Fetch length. */ |
| 978 | nbytes = stream_read (s, zlookup->sock, 2); |
| 979 | length = stream_getw (s); |
| 980 | |
| 981 | /* Fetch whole data. */ |
| 982 | nbytes = stream_read (s, zlookup->sock, length - 2); |
paul | d3092e7 | 2006-01-17 17:33:46 +0000 | [diff] [blame] | 983 | marker = stream_getc (s); |
| 984 | version = stream_getc (s); |
| 985 | |
| 986 | if (version != ZSERV_VERSION || marker != ZEBRA_HEADER_MARKER) |
| 987 | { |
| 988 | zlog_err("%s: socket %d version mismatch, marker %d, version %d", |
| 989 | __func__, zlookup->sock, marker, version); |
| 990 | return 0; |
| 991 | } |
| 992 | |
| 993 | command = stream_getw (s); |
| 994 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 995 | addr.s_addr = stream_get_ipv4 (s); |
| 996 | metric = stream_getl (s); |
| 997 | nexthop_num = stream_getc (s); |
| 998 | |
| 999 | /* Set IGP metric value. */ |
| 1000 | if (igpmetric) |
| 1001 | *igpmetric = metric; |
| 1002 | |
| 1003 | /* If there is nexthop then this is active route. */ |
| 1004 | if (nexthop_num) |
| 1005 | { |
| 1006 | nexthop.s_addr = 0; |
| 1007 | nexthop_type = stream_getc (s); |
| 1008 | if (nexthop_type == ZEBRA_NEXTHOP_IPV4) |
| 1009 | { |
| 1010 | nexthop.s_addr = stream_get_ipv4 (s); |
| 1011 | if (igpnexthop) |
| 1012 | *igpnexthop = nexthop; |
| 1013 | } |
| 1014 | else |
| 1015 | *igpnexthop = nexthop; |
| 1016 | |
| 1017 | return 1; |
| 1018 | } |
| 1019 | else |
| 1020 | return 0; |
| 1021 | } |
| 1022 | |
| 1023 | /* Scan all configured BGP route then check the route exists in IGP or |
| 1024 | not. */ |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 1025 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1026 | bgp_import (struct thread *t) |
| 1027 | { |
| 1028 | struct bgp *bgp; |
| 1029 | struct bgp_node *rn; |
| 1030 | struct bgp_static *bgp_static; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 1031 | struct listnode *node, *nnode; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1032 | int valid; |
| 1033 | u_int32_t metric; |
| 1034 | struct in_addr nexthop; |
| 1035 | afi_t afi; |
| 1036 | safi_t safi; |
| 1037 | |
| 1038 | bgp_import_thread = |
| 1039 | thread_add_timer (master, bgp_import, NULL, bgp_import_interval); |
| 1040 | |
hasso | f418446 | 2005-02-01 20:13:16 +0000 | [diff] [blame] | 1041 | if (BGP_DEBUG (events, EVENTS)) |
| 1042 | zlog_debug ("Import timer expired."); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1043 | |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 1044 | for (ALL_LIST_ELEMENTS (bm->bgp, node, nnode, bgp)) |
paul | 6cbbc3c | 2003-04-28 17:11:02 +0000 | [diff] [blame] | 1045 | { |
| 1046 | for (afi = AFI_IP; afi < AFI_MAX; afi++) |
| 1047 | for (safi = SAFI_UNICAST; safi < SAFI_MPLS_VPN; safi++) |
| 1048 | for (rn = bgp_table_top (bgp->route[afi][safi]); rn; |
| 1049 | rn = bgp_route_next (rn)) |
| 1050 | if ((bgp_static = rn->info) != NULL) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1051 | { |
paul | 6cbbc3c | 2003-04-28 17:11:02 +0000 | [diff] [blame] | 1052 | if (bgp_static->backdoor) |
| 1053 | continue; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1054 | |
paul | 6cbbc3c | 2003-04-28 17:11:02 +0000 | [diff] [blame] | 1055 | valid = bgp_static->valid; |
| 1056 | metric = bgp_static->igpmetric; |
| 1057 | nexthop = bgp_static->igpnexthop; |
| 1058 | |
| 1059 | if (bgp_flag_check (bgp, BGP_FLAG_IMPORT_CHECK) |
| 1060 | && afi == AFI_IP && safi == SAFI_UNICAST) |
| 1061 | bgp_static->valid = bgp_import_check (&rn->p, &bgp_static->igpmetric, |
| 1062 | &bgp_static->igpnexthop); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1063 | else |
paul | 6cbbc3c | 2003-04-28 17:11:02 +0000 | [diff] [blame] | 1064 | { |
| 1065 | bgp_static->valid = 1; |
| 1066 | bgp_static->igpmetric = 0; |
| 1067 | bgp_static->igpnexthop.s_addr = 0; |
| 1068 | } |
| 1069 | |
| 1070 | if (bgp_static->valid != valid) |
| 1071 | { |
| 1072 | if (bgp_static->valid) |
| 1073 | bgp_static_update (bgp, &rn->p, bgp_static, afi, safi); |
| 1074 | else |
| 1075 | bgp_static_withdraw (bgp, &rn->p, afi, safi); |
| 1076 | } |
| 1077 | else if (bgp_static->valid) |
| 1078 | { |
| 1079 | if (bgp_static->igpmetric != metric |
| 1080 | || bgp_static->igpnexthop.s_addr != nexthop.s_addr |
| 1081 | || bgp_static->rmap.name) |
| 1082 | bgp_static_update (bgp, &rn->p, bgp_static, afi, safi); |
| 1083 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1084 | } |
paul | 6cbbc3c | 2003-04-28 17:11:02 +0000 | [diff] [blame] | 1085 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1086 | return 0; |
| 1087 | } |
| 1088 | |
| 1089 | /* Connect to zebra for nexthop lookup. */ |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 1090 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1091 | zlookup_connect (struct thread *t) |
| 1092 | { |
| 1093 | struct zclient *zlookup; |
| 1094 | |
| 1095 | zlookup = THREAD_ARG (t); |
| 1096 | zlookup->t_connect = NULL; |
| 1097 | |
| 1098 | if (zlookup->sock != -1) |
| 1099 | return 0; |
| 1100 | |
Vyacheslav Trushkin | b511468 | 2011-11-25 18:51:48 +0400 | [diff] [blame] | 1101 | if (zclient_socket_connect (zlookup) < 0) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1102 | return -1; |
| 1103 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1104 | return 0; |
| 1105 | } |
| 1106 | |
| 1107 | /* Check specified multiaccess next-hop. */ |
| 1108 | int |
| 1109 | bgp_multiaccess_check_v4 (struct in_addr nexthop, char *peer) |
| 1110 | { |
| 1111 | struct bgp_node *rn1; |
| 1112 | struct bgp_node *rn2; |
| 1113 | struct prefix p1; |
| 1114 | struct prefix p2; |
| 1115 | struct in_addr addr; |
| 1116 | int ret; |
| 1117 | |
| 1118 | ret = inet_aton (peer, &addr); |
| 1119 | if (! ret) |
| 1120 | return 0; |
| 1121 | |
| 1122 | memset (&p1, 0, sizeof (struct prefix)); |
| 1123 | p1.family = AF_INET; |
| 1124 | p1.prefixlen = IPV4_MAX_BITLEN; |
| 1125 | p1.u.prefix4 = nexthop; |
| 1126 | memset (&p2, 0, sizeof (struct prefix)); |
| 1127 | p2.family = AF_INET; |
| 1128 | p2.prefixlen = IPV4_MAX_BITLEN; |
| 1129 | p2.u.prefix4 = addr; |
| 1130 | |
| 1131 | /* If bgp scan is not enabled, return invalid. */ |
| 1132 | if (zlookup->sock < 0) |
| 1133 | return 0; |
| 1134 | |
paul | 5932020 | 2004-11-09 01:54:03 +0000 | [diff] [blame] | 1135 | rn1 = bgp_node_match (bgp_connected_table[AFI_IP], &p1); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1136 | if (! rn1) |
| 1137 | return 0; |
Chris Caputo | 6c88b44 | 2010-07-27 16:28:55 +0000 | [diff] [blame] | 1138 | bgp_unlock_node (rn1); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 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; |
Chris Caputo | 6c88b44 | 2010-07-27 16:28:55 +0000 | [diff] [blame] | 1143 | bgp_unlock_node (rn2); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1144 | |
Chris Caputo | 6c88b44 | 2010-07-27 16:28:55 +0000 | [diff] [blame] | 1145 | /* This is safe, even with above unlocks, since we are just |
| 1146 | comparing pointers to the objects, not the objects themselves. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1147 | if (rn1 == rn2) |
| 1148 | return 1; |
| 1149 | |
| 1150 | return 0; |
| 1151 | } |
| 1152 | |
| 1153 | DEFUN (bgp_scan_time, |
| 1154 | bgp_scan_time_cmd, |
| 1155 | "bgp scan-time <5-60>", |
| 1156 | "BGP specific commands\n" |
| 1157 | "Configure background scanner interval\n" |
| 1158 | "Scanner interval (seconds)\n") |
| 1159 | { |
| 1160 | bgp_scan_interval = atoi (argv[0]); |
| 1161 | |
| 1162 | if (bgp_scan_thread) |
| 1163 | { |
| 1164 | thread_cancel (bgp_scan_thread); |
| 1165 | bgp_scan_thread = |
paul | 5932020 | 2004-11-09 01:54:03 +0000 | [diff] [blame] | 1166 | thread_add_timer (master, bgp_scan_timer, NULL, bgp_scan_interval); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1167 | } |
| 1168 | |
| 1169 | return CMD_SUCCESS; |
| 1170 | } |
| 1171 | |
| 1172 | DEFUN (no_bgp_scan_time, |
| 1173 | no_bgp_scan_time_cmd, |
| 1174 | "no bgp scan-time", |
| 1175 | NO_STR |
| 1176 | "BGP specific commands\n" |
| 1177 | "Configure background scanner interval\n") |
| 1178 | { |
| 1179 | bgp_scan_interval = BGP_SCAN_INTERVAL_DEFAULT; |
| 1180 | |
| 1181 | if (bgp_scan_thread) |
| 1182 | { |
| 1183 | thread_cancel (bgp_scan_thread); |
| 1184 | bgp_scan_thread = |
paul | 5932020 | 2004-11-09 01:54:03 +0000 | [diff] [blame] | 1185 | thread_add_timer (master, bgp_scan_timer, NULL, bgp_scan_interval); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1186 | } |
| 1187 | |
| 1188 | return CMD_SUCCESS; |
| 1189 | } |
| 1190 | |
| 1191 | ALIAS (no_bgp_scan_time, |
| 1192 | no_bgp_scan_time_val_cmd, |
| 1193 | "no bgp scan-time <5-60>", |
| 1194 | NO_STR |
| 1195 | "BGP specific commands\n" |
| 1196 | "Configure background scanner interval\n" |
| 1197 | "Scanner interval (seconds)\n") |
| 1198 | |
| 1199 | DEFUN (show_ip_bgp_scan, |
| 1200 | show_ip_bgp_scan_cmd, |
| 1201 | "show ip bgp scan", |
| 1202 | SHOW_STR |
| 1203 | IP_STR |
| 1204 | BGP_STR |
| 1205 | "BGP scan status\n") |
| 1206 | { |
| 1207 | struct bgp_node *rn; |
| 1208 | struct bgp_nexthop_cache *bnc; |
| 1209 | |
| 1210 | if (bgp_scan_thread) |
| 1211 | vty_out (vty, "BGP scan is running%s", VTY_NEWLINE); |
| 1212 | else |
| 1213 | vty_out (vty, "BGP scan is not running%s", VTY_NEWLINE); |
| 1214 | vty_out (vty, "BGP scan interval is %d%s", bgp_scan_interval, VTY_NEWLINE); |
| 1215 | |
| 1216 | vty_out (vty, "Current BGP nexthop cache:%s", VTY_NEWLINE); |
paul | 5932020 | 2004-11-09 01:54:03 +0000 | [diff] [blame] | 1217 | for (rn = bgp_table_top (bgp_nexthop_cache_table[AFI_IP]); rn; rn = bgp_route_next (rn)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1218 | if ((bnc = rn->info) != NULL) |
| 1219 | { |
| 1220 | if (bnc->valid) |
| 1221 | vty_out (vty, " %s valid [IGP metric %d]%s", |
| 1222 | inet_ntoa (rn->p.u.prefix4), bnc->metric, VTY_NEWLINE); |
| 1223 | else |
| 1224 | vty_out (vty, " %s invalid%s", |
| 1225 | inet_ntoa (rn->p.u.prefix4), VTY_NEWLINE); |
| 1226 | } |
| 1227 | |
| 1228 | #ifdef HAVE_IPV6 |
| 1229 | { |
| 1230 | char buf[BUFSIZ]; |
paul | 5932020 | 2004-11-09 01:54:03 +0000 | [diff] [blame] | 1231 | for (rn = bgp_table_top (bgp_nexthop_cache_table[AFI_IP6]); |
| 1232 | rn; |
| 1233 | rn = bgp_route_next (rn)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1234 | if ((bnc = rn->info) != NULL) |
| 1235 | { |
| 1236 | if (bnc->valid) |
| 1237 | vty_out (vty, " %s valid [IGP metric %d]%s", |
| 1238 | inet_ntop (AF_INET6, &rn->p.u.prefix6, buf, BUFSIZ), |
| 1239 | bnc->metric, VTY_NEWLINE); |
| 1240 | else |
| 1241 | vty_out (vty, " %s invalid%s", |
| 1242 | inet_ntop (AF_INET6, &rn->p.u.prefix6, buf, BUFSIZ), |
| 1243 | VTY_NEWLINE); |
| 1244 | } |
| 1245 | } |
| 1246 | #endif /* HAVE_IPV6 */ |
| 1247 | |
| 1248 | vty_out (vty, "BGP connected route:%s", VTY_NEWLINE); |
paul | 5932020 | 2004-11-09 01:54:03 +0000 | [diff] [blame] | 1249 | for (rn = bgp_table_top (bgp_connected_table[AFI_IP]); |
| 1250 | rn; |
| 1251 | rn = bgp_route_next (rn)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1252 | if (rn->info != NULL) |
| 1253 | vty_out (vty, " %s/%d%s", inet_ntoa (rn->p.u.prefix4), rn->p.prefixlen, |
| 1254 | VTY_NEWLINE); |
| 1255 | |
| 1256 | #ifdef HAVE_IPV6 |
| 1257 | { |
| 1258 | char buf[BUFSIZ]; |
| 1259 | |
paul | 5932020 | 2004-11-09 01:54:03 +0000 | [diff] [blame] | 1260 | for (rn = bgp_table_top (bgp_connected_table[AFI_IP6]); |
| 1261 | rn; |
| 1262 | rn = bgp_route_next (rn)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1263 | if (rn->info != NULL) |
| 1264 | vty_out (vty, " %s/%d%s", |
| 1265 | inet_ntop (AF_INET6, &rn->p.u.prefix6, buf, BUFSIZ), |
| 1266 | rn->p.prefixlen, |
| 1267 | VTY_NEWLINE); |
| 1268 | } |
| 1269 | #endif /* HAVE_IPV6 */ |
| 1270 | |
| 1271 | return CMD_SUCCESS; |
| 1272 | } |
| 1273 | |
| 1274 | int |
| 1275 | bgp_config_write_scan_time (struct vty *vty) |
| 1276 | { |
| 1277 | if (bgp_scan_interval != BGP_SCAN_INTERVAL_DEFAULT) |
| 1278 | vty_out (vty, " bgp scan-time %d%s", bgp_scan_interval, VTY_NEWLINE); |
| 1279 | return CMD_SUCCESS; |
| 1280 | } |
| 1281 | |
| 1282 | void |
Stephen Hemminger | 66e5cd8 | 2009-02-09 10:14:16 -0800 | [diff] [blame] | 1283 | bgp_scan_init (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1284 | { |
| 1285 | zlookup = zclient_new (); |
| 1286 | zlookup->sock = -1; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1287 | zlookup->t_connect = thread_add_event (master, zlookup_connect, zlookup, 0); |
| 1288 | |
| 1289 | bgp_scan_interval = BGP_SCAN_INTERVAL_DEFAULT; |
| 1290 | bgp_import_interval = BGP_IMPORT_INTERVAL_DEFAULT; |
| 1291 | |
Paul Jakma | 64e580a | 2006-02-21 01:09:01 +0000 | [diff] [blame] | 1292 | cache1_table[AFI_IP] = bgp_table_init (AFI_IP, SAFI_UNICAST); |
| 1293 | cache2_table[AFI_IP] = bgp_table_init (AFI_IP, SAFI_UNICAST); |
paul | 5932020 | 2004-11-09 01:54:03 +0000 | [diff] [blame] | 1294 | bgp_nexthop_cache_table[AFI_IP] = cache1_table[AFI_IP]; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1295 | |
Paul Jakma | 64e580a | 2006-02-21 01:09:01 +0000 | [diff] [blame] | 1296 | bgp_connected_table[AFI_IP] = bgp_table_init (AFI_IP, SAFI_UNICAST); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1297 | |
| 1298 | #ifdef HAVE_IPV6 |
Paul Jakma | 64e580a | 2006-02-21 01:09:01 +0000 | [diff] [blame] | 1299 | cache1_table[AFI_IP6] = bgp_table_init (AFI_IP6, SAFI_UNICAST); |
| 1300 | cache2_table[AFI_IP6] = bgp_table_init (AFI_IP6, SAFI_UNICAST); |
paul | 5932020 | 2004-11-09 01:54:03 +0000 | [diff] [blame] | 1301 | bgp_nexthop_cache_table[AFI_IP6] = cache1_table[AFI_IP6]; |
Paul Jakma | 64e580a | 2006-02-21 01:09:01 +0000 | [diff] [blame] | 1302 | bgp_connected_table[AFI_IP6] = bgp_table_init (AFI_IP6, SAFI_UNICAST); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1303 | #endif /* HAVE_IPV6 */ |
| 1304 | |
| 1305 | /* Make BGP scan thread. */ |
paul | 5932020 | 2004-11-09 01:54:03 +0000 | [diff] [blame] | 1306 | bgp_scan_thread = thread_add_timer (master, bgp_scan_timer, |
| 1307 | NULL, bgp_scan_interval); |
paul | 6cbbc3c | 2003-04-28 17:11:02 +0000 | [diff] [blame] | 1308 | /* Make BGP import there. */ |
| 1309 | bgp_import_thread = thread_add_timer (master, bgp_import, NULL, 0); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1310 | |
| 1311 | install_element (BGP_NODE, &bgp_scan_time_cmd); |
| 1312 | install_element (BGP_NODE, &no_bgp_scan_time_cmd); |
| 1313 | install_element (BGP_NODE, &no_bgp_scan_time_val_cmd); |
| 1314 | install_element (VIEW_NODE, &show_ip_bgp_scan_cmd); |
Paul Jakma | 62687ff | 2008-08-23 14:27:06 +0100 | [diff] [blame] | 1315 | install_element (RESTRICTED_NODE, &show_ip_bgp_scan_cmd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1316 | install_element (ENABLE_NODE, &show_ip_bgp_scan_cmd); |
| 1317 | } |
Chris Caputo | 228da42 | 2009-07-18 05:44:03 +0000 | [diff] [blame] | 1318 | |
| 1319 | void |
| 1320 | bgp_scan_finish (void) |
| 1321 | { |
Chris Caputo | 6c88b44 | 2010-07-27 16:28:55 +0000 | [diff] [blame] | 1322 | /* Only the current one needs to be reset. */ |
| 1323 | bgp_nexthop_cache_reset (bgp_nexthop_cache_table[AFI_IP]); |
| 1324 | |
Chris Caputo | 228da42 | 2009-07-18 05:44:03 +0000 | [diff] [blame] | 1325 | bgp_table_unlock (cache1_table[AFI_IP]); |
| 1326 | cache1_table[AFI_IP] = NULL; |
| 1327 | |
| 1328 | bgp_table_unlock (cache2_table[AFI_IP]); |
| 1329 | cache2_table[AFI_IP] = NULL; |
| 1330 | |
| 1331 | bgp_table_unlock (bgp_connected_table[AFI_IP]); |
| 1332 | bgp_connected_table[AFI_IP] = NULL; |
| 1333 | |
| 1334 | #ifdef HAVE_IPV6 |
Chris Caputo | 6c88b44 | 2010-07-27 16:28:55 +0000 | [diff] [blame] | 1335 | /* Only the current one needs to be reset. */ |
| 1336 | bgp_nexthop_cache_reset (bgp_nexthop_cache_table[AFI_IP6]); |
| 1337 | |
Chris Caputo | 228da42 | 2009-07-18 05:44:03 +0000 | [diff] [blame] | 1338 | bgp_table_unlock (cache1_table[AFI_IP6]); |
| 1339 | cache1_table[AFI_IP6] = NULL; |
| 1340 | |
| 1341 | bgp_table_unlock (cache2_table[AFI_IP6]); |
| 1342 | cache2_table[AFI_IP6] = NULL; |
| 1343 | |
| 1344 | bgp_table_unlock (bgp_connected_table[AFI_IP6]); |
| 1345 | bgp_connected_table[AFI_IP6] = NULL; |
| 1346 | #endif /* HAVE_IPV6 */ |
| 1347 | } |