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