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