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