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