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 | |
Christian Franke | a0f6ce5 | 2013-04-11 08:24:30 +0000 | [diff] [blame] | 456 | if (bi->peer->sort == BGP_PEER_EBGP && bi->peer->ttl == 1 |
| 457 | && !CHECK_FLAG(bi->peer->flags, PEER_FLAG_DISABLE_CONNECTED_CHECK)) |
Denis Ovsienko | 8e80bdf | 2011-08-05 18:52:52 +0400 | [diff] [blame] | 458 | valid = bgp_nexthop_onlink (afi, bi->attr); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 459 | else |
paul | 5932020 | 2004-11-09 01:54:03 +0000 | [diff] [blame] | 460 | valid = bgp_nexthop_lookup (afi, bi->peer, bi, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 461 | &changed, &metricchanged); |
| 462 | |
| 463 | current = CHECK_FLAG (bi->flags, BGP_INFO_VALID) ? 1 : 0; |
| 464 | |
| 465 | if (changed) |
| 466 | SET_FLAG (bi->flags, BGP_INFO_IGP_CHANGED); |
| 467 | else |
| 468 | UNSET_FLAG (bi->flags, BGP_INFO_IGP_CHANGED); |
| 469 | |
| 470 | if (valid != current) |
| 471 | { |
| 472 | if (CHECK_FLAG (bi->flags, BGP_INFO_VALID)) |
| 473 | { |
| 474 | bgp_aggregate_decrement (bgp, &rn->p, bi, |
paul | 5932020 | 2004-11-09 01:54:03 +0000 | [diff] [blame] | 475 | afi, SAFI_UNICAST); |
Paul Jakma | 1a392d4 | 2006-09-07 00:24:49 +0000 | [diff] [blame] | 476 | bgp_info_unset_flag (rn, bi, BGP_INFO_VALID); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 477 | } |
| 478 | else |
| 479 | { |
Paul Jakma | 1a392d4 | 2006-09-07 00:24:49 +0000 | [diff] [blame] | 480 | bgp_info_set_flag (rn, bi, BGP_INFO_VALID); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 481 | bgp_aggregate_increment (bgp, &rn->p, bi, |
paul | 5932020 | 2004-11-09 01:54:03 +0000 | [diff] [blame] | 482 | afi, SAFI_UNICAST); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 483 | } |
| 484 | } |
| 485 | |
paul | 5932020 | 2004-11-09 01:54:03 +0000 | [diff] [blame] | 486 | if (CHECK_FLAG (bgp->af_flags[afi][SAFI_UNICAST], |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 487 | BGP_CONFIG_DAMPENING) |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 488 | && bi->extra && bi->extra->damp_info ) |
paul | 5932020 | 2004-11-09 01:54:03 +0000 | [diff] [blame] | 489 | if (bgp_damp_scan (bi, afi, SAFI_UNICAST)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 490 | bgp_aggregate_increment (bgp, &rn->p, bi, |
paul | 5932020 | 2004-11-09 01:54:03 +0000 | [diff] [blame] | 491 | afi, SAFI_UNICAST); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 492 | } |
| 493 | } |
paul | 5932020 | 2004-11-09 01:54:03 +0000 | [diff] [blame] | 494 | bgp_process (bgp, rn, afi, SAFI_UNICAST); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | /* Flash old cache. */ |
paul | 5932020 | 2004-11-09 01:54:03 +0000 | [diff] [blame] | 498 | if (bgp_nexthop_cache_table[afi] == cache1_table[afi]) |
| 499 | bgp_nexthop_cache_reset (cache2_table[afi]); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 500 | else |
paul | 5932020 | 2004-11-09 01:54:03 +0000 | [diff] [blame] | 501 | bgp_nexthop_cache_reset (cache1_table[afi]); |
hasso | f418446 | 2005-02-01 20:13:16 +0000 | [diff] [blame] | 502 | |
| 503 | if (BGP_DEBUG (events, EVENTS)) |
| 504 | { |
| 505 | if (afi == AFI_IP) |
| 506 | zlog_debug ("scanning IPv4 Unicast routing tables"); |
| 507 | else if (afi == AFI_IP6) |
| 508 | zlog_debug ("scanning IPv6 Unicast routing tables"); |
| 509 | } |
Christian Franke | dcab1bb | 2012-12-07 16:45:52 +0000 | [diff] [blame] | 510 | |
| 511 | /* Reevaluate default-originate route-maps and announce/withdraw |
| 512 | * default route if neccesary. */ |
| 513 | for (ALL_LIST_ELEMENTS (bgp->peer, node, nnode, peer)) |
| 514 | { |
| 515 | if (peer->status == Established |
| 516 | && CHECK_FLAG(peer->af_flags[afi][safi], PEER_FLAG_DEFAULT_ORIGINATE) |
| 517 | && peer->default_rmap[afi][safi].name) |
| 518 | bgp_default_originate (peer, afi, safi, 0); |
| 519 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 520 | } |
| 521 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 522 | /* BGP scan thread. This thread check nexthop reachability. */ |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 523 | static int |
paul | 5932020 | 2004-11-09 01:54:03 +0000 | [diff] [blame] | 524 | bgp_scan_timer (struct thread *t) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 525 | { |
| 526 | bgp_scan_thread = |
paul | 5932020 | 2004-11-09 01:54:03 +0000 | [diff] [blame] | 527 | thread_add_timer (master, bgp_scan_timer, NULL, bgp_scan_interval); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 528 | |
hasso | f418446 | 2005-02-01 20:13:16 +0000 | [diff] [blame] | 529 | if (BGP_DEBUG (events, EVENTS)) |
ajs | 478ba05 | 2004-12-08 20:41:23 +0000 | [diff] [blame] | 530 | zlog_debug ("Performing BGP general scanning"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 531 | |
paul | 5932020 | 2004-11-09 01:54:03 +0000 | [diff] [blame] | 532 | bgp_scan (AFI_IP, SAFI_UNICAST); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 533 | |
| 534 | #ifdef HAVE_IPV6 |
paul | 5932020 | 2004-11-09 01:54:03 +0000 | [diff] [blame] | 535 | bgp_scan (AFI_IP6, SAFI_UNICAST); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 536 | #endif /* HAVE_IPV6 */ |
| 537 | |
| 538 | return 0; |
| 539 | } |
Jorge Boncompte [DTI2] | 10f9bf3 | 2012-05-07 16:52:52 +0000 | [diff] [blame] | 540 | |
| 541 | /* BGP own address structure */ |
| 542 | struct bgp_addr |
| 543 | { |
| 544 | struct in_addr addr; |
| 545 | int refcnt; |
| 546 | }; |
| 547 | |
| 548 | static struct hash *bgp_address_hash; |
| 549 | |
| 550 | static void * |
| 551 | bgp_address_hash_alloc (void *p) |
| 552 | { |
| 553 | struct in_addr *val = p; |
| 554 | struct bgp_addr *addr; |
| 555 | |
| 556 | addr = XMALLOC (MTYPE_BGP_ADDR, sizeof (struct bgp_addr)); |
| 557 | addr->refcnt = 0; |
| 558 | addr->addr.s_addr = val->s_addr; |
| 559 | |
| 560 | return addr; |
| 561 | } |
| 562 | |
| 563 | static unsigned int |
| 564 | bgp_address_hash_key_make (void *p) |
| 565 | { |
| 566 | const struct bgp_addr *addr = p; |
| 567 | |
| 568 | return jhash_1word(addr->addr.s_addr, 0); |
| 569 | } |
| 570 | |
| 571 | static int |
| 572 | bgp_address_hash_cmp (const void *p1, const void *p2) |
| 573 | { |
| 574 | const struct bgp_addr *addr1 = p1; |
| 575 | const struct bgp_addr *addr2 = p2; |
| 576 | |
| 577 | return addr1->addr.s_addr == addr2->addr.s_addr; |
| 578 | } |
| 579 | |
| 580 | void |
| 581 | bgp_address_init (void) |
| 582 | { |
| 583 | bgp_address_hash = hash_create (bgp_address_hash_key_make, |
| 584 | bgp_address_hash_cmp); |
| 585 | } |
| 586 | |
| 587 | static void |
| 588 | bgp_address_add (struct prefix *p) |
| 589 | { |
| 590 | struct bgp_addr tmp; |
| 591 | struct bgp_addr *addr; |
| 592 | |
| 593 | tmp.addr = p->u.prefix4; |
| 594 | |
| 595 | addr = hash_get (bgp_address_hash, &tmp, bgp_address_hash_alloc); |
| 596 | addr->refcnt++; |
| 597 | } |
| 598 | |
| 599 | static void |
| 600 | bgp_address_del (struct prefix *p) |
| 601 | { |
| 602 | struct bgp_addr tmp; |
| 603 | struct bgp_addr *addr; |
| 604 | |
| 605 | tmp.addr = p->u.prefix4; |
| 606 | |
| 607 | addr = hash_lookup (bgp_address_hash, &tmp); |
Rakesh Garimella | 9e47abd | 2013-03-11 12:38:31 +0000 | [diff] [blame] | 608 | /* may have been deleted earlier by bgp_interface_down() */ |
| 609 | if (addr == NULL) |
| 610 | return; |
| 611 | |
Jorge Boncompte [DTI2] | 10f9bf3 | 2012-05-07 16:52:52 +0000 | [diff] [blame] | 612 | addr->refcnt--; |
| 613 | |
| 614 | if (addr->refcnt == 0) |
| 615 | { |
| 616 | hash_release (bgp_address_hash, addr); |
| 617 | XFREE (MTYPE_BGP_ADDR, addr); |
| 618 | } |
| 619 | } |
| 620 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 621 | |
paul | 5932020 | 2004-11-09 01:54:03 +0000 | [diff] [blame] | 622 | struct bgp_connected_ref |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 623 | { |
| 624 | unsigned int refcnt; |
| 625 | }; |
| 626 | |
| 627 | void |
| 628 | bgp_connected_add (struct connected *ifc) |
| 629 | { |
| 630 | struct prefix p; |
| 631 | struct prefix *addr; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 632 | struct interface *ifp; |
| 633 | struct bgp_node *rn; |
paul | 5932020 | 2004-11-09 01:54:03 +0000 | [diff] [blame] | 634 | struct bgp_connected_ref *bc; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 635 | |
| 636 | ifp = ifc->ifp; |
| 637 | |
| 638 | if (! ifp) |
| 639 | return; |
| 640 | |
| 641 | if (if_is_loopback (ifp)) |
| 642 | return; |
| 643 | |
| 644 | addr = ifc->address; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 645 | |
| 646 | if (addr->family == AF_INET) |
| 647 | { |
Andrew J. Schorr | e452963 | 2006-12-12 19:18:21 +0000 | [diff] [blame] | 648 | PREFIX_COPY_IPV4(&p, CONNECTED_PREFIX(ifc)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 649 | apply_mask_ipv4 ((struct prefix_ipv4 *) &p); |
| 650 | |
| 651 | if (prefix_ipv4_any ((struct prefix_ipv4 *) &p)) |
| 652 | return; |
| 653 | |
Jorge Boncompte [DTI2] | 10f9bf3 | 2012-05-07 16:52:52 +0000 | [diff] [blame] | 654 | bgp_address_add (addr); |
| 655 | |
paul | 5932020 | 2004-11-09 01:54:03 +0000 | [diff] [blame] | 656 | rn = bgp_node_get (bgp_connected_table[AFI_IP], (struct prefix *) &p); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 657 | if (rn->info) |
| 658 | { |
| 659 | bc = rn->info; |
| 660 | bc->refcnt++; |
| 661 | } |
| 662 | else |
| 663 | { |
Chris Caputo | 6c88b44 | 2010-07-27 16:28:55 +0000 | [diff] [blame] | 664 | bc = XCALLOC (MTYPE_BGP_CONN, sizeof (struct bgp_connected_ref)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 665 | bc->refcnt = 1; |
| 666 | rn->info = bc; |
| 667 | } |
| 668 | } |
| 669 | #ifdef HAVE_IPV6 |
Andrew J. Schorr | e452963 | 2006-12-12 19:18:21 +0000 | [diff] [blame] | 670 | else if (addr->family == AF_INET6) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 671 | { |
Andrew J. Schorr | e452963 | 2006-12-12 19:18:21 +0000 | [diff] [blame] | 672 | PREFIX_COPY_IPV6(&p, CONNECTED_PREFIX(ifc)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 673 | apply_mask_ipv6 ((struct prefix_ipv6 *) &p); |
| 674 | |
| 675 | if (IN6_IS_ADDR_UNSPECIFIED (&p.u.prefix6)) |
| 676 | return; |
| 677 | |
| 678 | if (IN6_IS_ADDR_LINKLOCAL (&p.u.prefix6)) |
| 679 | return; |
| 680 | |
paul | 5932020 | 2004-11-09 01:54:03 +0000 | [diff] [blame] | 681 | rn = bgp_node_get (bgp_connected_table[AFI_IP6], (struct prefix *) &p); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 682 | if (rn->info) |
| 683 | { |
| 684 | bc = rn->info; |
| 685 | bc->refcnt++; |
| 686 | } |
| 687 | else |
| 688 | { |
Chris Caputo | 6c88b44 | 2010-07-27 16:28:55 +0000 | [diff] [blame] | 689 | bc = XCALLOC (MTYPE_BGP_CONN, sizeof (struct bgp_connected_ref)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 690 | bc->refcnt = 1; |
| 691 | rn->info = bc; |
| 692 | } |
| 693 | } |
| 694 | #endif /* HAVE_IPV6 */ |
| 695 | } |
| 696 | |
| 697 | void |
| 698 | bgp_connected_delete (struct connected *ifc) |
| 699 | { |
| 700 | struct prefix p; |
| 701 | struct prefix *addr; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 702 | struct interface *ifp; |
| 703 | struct bgp_node *rn; |
paul | 5932020 | 2004-11-09 01:54:03 +0000 | [diff] [blame] | 704 | struct bgp_connected_ref *bc; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 705 | |
| 706 | ifp = ifc->ifp; |
| 707 | |
| 708 | if (if_is_loopback (ifp)) |
| 709 | return; |
| 710 | |
| 711 | addr = ifc->address; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 712 | |
| 713 | if (addr->family == AF_INET) |
| 714 | { |
Andrew J. Schorr | e452963 | 2006-12-12 19:18:21 +0000 | [diff] [blame] | 715 | PREFIX_COPY_IPV4(&p, CONNECTED_PREFIX(ifc)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 716 | apply_mask_ipv4 ((struct prefix_ipv4 *) &p); |
| 717 | |
| 718 | if (prefix_ipv4_any ((struct prefix_ipv4 *) &p)) |
| 719 | return; |
| 720 | |
Jorge Boncompte [DTI2] | 10f9bf3 | 2012-05-07 16:52:52 +0000 | [diff] [blame] | 721 | bgp_address_del (addr); |
| 722 | |
paul | 5932020 | 2004-11-09 01:54:03 +0000 | [diff] [blame] | 723 | rn = bgp_node_lookup (bgp_connected_table[AFI_IP], &p); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 724 | if (! rn) |
| 725 | return; |
| 726 | |
| 727 | bc = rn->info; |
| 728 | bc->refcnt--; |
| 729 | if (bc->refcnt == 0) |
| 730 | { |
Chris Caputo | 6c88b44 | 2010-07-27 16:28:55 +0000 | [diff] [blame] | 731 | XFREE (MTYPE_BGP_CONN, bc); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 732 | rn->info = NULL; |
| 733 | } |
| 734 | bgp_unlock_node (rn); |
| 735 | bgp_unlock_node (rn); |
| 736 | } |
| 737 | #ifdef HAVE_IPV6 |
| 738 | else if (addr->family == AF_INET6) |
| 739 | { |
Andrew J. Schorr | e452963 | 2006-12-12 19:18:21 +0000 | [diff] [blame] | 740 | PREFIX_COPY_IPV6(&p, CONNECTED_PREFIX(ifc)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 741 | apply_mask_ipv6 ((struct prefix_ipv6 *) &p); |
| 742 | |
| 743 | if (IN6_IS_ADDR_UNSPECIFIED (&p.u.prefix6)) |
| 744 | return; |
| 745 | |
| 746 | if (IN6_IS_ADDR_LINKLOCAL (&p.u.prefix6)) |
| 747 | return; |
| 748 | |
paul | 5932020 | 2004-11-09 01:54:03 +0000 | [diff] [blame] | 749 | rn = bgp_node_lookup (bgp_connected_table[AFI_IP6], (struct prefix *) &p); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 750 | if (! rn) |
| 751 | return; |
| 752 | |
| 753 | bc = rn->info; |
| 754 | bc->refcnt--; |
| 755 | if (bc->refcnt == 0) |
| 756 | { |
Chris Caputo | 6c88b44 | 2010-07-27 16:28:55 +0000 | [diff] [blame] | 757 | XFREE (MTYPE_BGP_CONN, bc); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 758 | rn->info = NULL; |
| 759 | } |
| 760 | bgp_unlock_node (rn); |
| 761 | bgp_unlock_node (rn); |
| 762 | } |
| 763 | #endif /* HAVE_IPV6 */ |
| 764 | } |
| 765 | |
| 766 | int |
Jorge Boncompte [DTI2] | 10f9bf3 | 2012-05-07 16:52:52 +0000 | [diff] [blame] | 767 | bgp_nexthop_self (struct attr *attr) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 768 | { |
Jorge Boncompte [DTI2] | 10f9bf3 | 2012-05-07 16:52:52 +0000 | [diff] [blame] | 769 | struct bgp_addr tmp, *addr; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 770 | |
Jorge Boncompte [DTI2] | 10f9bf3 | 2012-05-07 16:52:52 +0000 | [diff] [blame] | 771 | tmp.addr = attr->nexthop; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 772 | |
Jorge Boncompte [DTI2] | 10f9bf3 | 2012-05-07 16:52:52 +0000 | [diff] [blame] | 773 | addr = hash_lookup (bgp_address_hash, &tmp); |
| 774 | if (addr) |
| 775 | return 1; |
| 776 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 777 | return 0; |
| 778 | } |
| 779 | |
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 (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 marker; |
| 786 | u_char version; |
| 787 | uint16_t command; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 788 | int nbytes; |
| 789 | struct in_addr raddr; |
paul | d3092e7 | 2006-01-17 17:33:46 +0000 | [diff] [blame] | 790 | uint32_t metric; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 791 | int i; |
| 792 | u_char nexthop_num; |
| 793 | struct nexthop *nexthop; |
| 794 | struct bgp_nexthop_cache *bnc; |
| 795 | |
| 796 | s = zlookup->ibuf; |
| 797 | stream_reset (s); |
| 798 | |
| 799 | nbytes = stream_read (s, zlookup->sock, 2); |
| 800 | length = stream_getw (s); |
| 801 | |
| 802 | nbytes = stream_read (s, zlookup->sock, length - 2); |
paul | d3092e7 | 2006-01-17 17:33:46 +0000 | [diff] [blame] | 803 | marker = stream_getc (s); |
| 804 | version = stream_getc (s); |
| 805 | |
| 806 | if (version != ZSERV_VERSION || marker != ZEBRA_HEADER_MARKER) |
| 807 | { |
| 808 | zlog_err("%s: socket %d version mismatch, marker %d, version %d", |
| 809 | __func__, zlookup->sock, marker, version); |
| 810 | return NULL; |
| 811 | } |
| 812 | |
| 813 | command = stream_getw (s); |
| 814 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 815 | raddr.s_addr = stream_get_ipv4 (s); |
| 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_IPV4: |
| 833 | nexthop->gate.ipv4.s_addr = stream_get_ipv4 (s); |
| 834 | break; |
| 835 | case ZEBRA_NEXTHOP_IFINDEX: |
| 836 | case ZEBRA_NEXTHOP_IFNAME: |
| 837 | nexthop->ifindex = stream_getl (s); |
| 838 | break; |
hasso | fa2b17e | 2004-03-04 17:45:00 +0000 | [diff] [blame] | 839 | default: |
| 840 | /* do nothing */ |
| 841 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 842 | } |
| 843 | bnc_nexthop_add (bnc, nexthop); |
| 844 | } |
| 845 | } |
| 846 | else |
| 847 | return NULL; |
| 848 | |
| 849 | return bnc; |
| 850 | } |
| 851 | |
| 852 | struct bgp_nexthop_cache * |
| 853 | zlookup_query (struct in_addr addr) |
| 854 | { |
| 855 | int ret; |
| 856 | struct stream *s; |
| 857 | |
| 858 | /* Check socket. */ |
| 859 | if (zlookup->sock < 0) |
| 860 | return NULL; |
| 861 | |
| 862 | s = zlookup->obuf; |
| 863 | stream_reset (s); |
paul | d3092e7 | 2006-01-17 17:33:46 +0000 | [diff] [blame] | 864 | zclient_create_header (s, ZEBRA_IPV4_NEXTHOP_LOOKUP); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 865 | stream_put_in_addr (s, &addr); |
paul | d3092e7 | 2006-01-17 17:33:46 +0000 | [diff] [blame] | 866 | |
| 867 | stream_putw_at (s, 0, stream_get_endp (s)); |
| 868 | |
| 869 | ret = writen (zlookup->sock, s->data, stream_get_endp (s)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 870 | if (ret < 0) |
| 871 | { |
| 872 | zlog_err ("can't write to zlookup->sock"); |
| 873 | close (zlookup->sock); |
| 874 | zlookup->sock = -1; |
| 875 | return NULL; |
| 876 | } |
| 877 | if (ret == 0) |
| 878 | { |
| 879 | zlog_err ("zlookup->sock connection closed"); |
| 880 | close (zlookup->sock); |
| 881 | zlookup->sock = -1; |
| 882 | return NULL; |
| 883 | } |
| 884 | |
| 885 | return zlookup_read (); |
| 886 | } |
| 887 | |
| 888 | #ifdef HAVE_IPV6 |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 889 | static struct bgp_nexthop_cache * |
Stephen Hemminger | 66e5cd8 | 2009-02-09 10:14:16 -0800 | [diff] [blame] | 890 | zlookup_read_ipv6 (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 891 | { |
| 892 | struct stream *s; |
paul | d3092e7 | 2006-01-17 17:33:46 +0000 | [diff] [blame] | 893 | uint16_t length; |
| 894 | u_char version, marker; |
| 895 | uint16_t command; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 896 | int nbytes; |
| 897 | struct in6_addr raddr; |
paul | d3092e7 | 2006-01-17 17:33:46 +0000 | [diff] [blame] | 898 | uint32_t metric; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 899 | int i; |
| 900 | u_char nexthop_num; |
| 901 | struct nexthop *nexthop; |
| 902 | struct bgp_nexthop_cache *bnc; |
| 903 | |
| 904 | s = zlookup->ibuf; |
| 905 | stream_reset (s); |
| 906 | |
| 907 | nbytes = stream_read (s, zlookup->sock, 2); |
| 908 | length = stream_getw (s); |
| 909 | |
| 910 | nbytes = stream_read (s, zlookup->sock, length - 2); |
paul | d3092e7 | 2006-01-17 17:33:46 +0000 | [diff] [blame] | 911 | marker = stream_getc (s); |
| 912 | version = stream_getc (s); |
| 913 | |
| 914 | if (version != ZSERV_VERSION || marker != ZEBRA_HEADER_MARKER) |
| 915 | { |
| 916 | zlog_err("%s: socket %d version mismatch, marker %d, version %d", |
| 917 | __func__, zlookup->sock, marker, version); |
| 918 | return NULL; |
| 919 | } |
| 920 | |
| 921 | command = stream_getw (s); |
| 922 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 923 | stream_get (&raddr, s, 16); |
| 924 | |
| 925 | metric = stream_getl (s); |
| 926 | nexthop_num = stream_getc (s); |
| 927 | |
| 928 | if (nexthop_num) |
| 929 | { |
| 930 | bnc = bnc_new (); |
| 931 | bnc->valid = 1; |
| 932 | bnc->metric = metric; |
| 933 | bnc->nexthop_num = nexthop_num; |
| 934 | |
| 935 | for (i = 0; i < nexthop_num; i++) |
| 936 | { |
Stephen Hemminger | 393deb9 | 2008-08-18 14:13:29 -0700 | [diff] [blame] | 937 | nexthop = XCALLOC (MTYPE_NEXTHOP, sizeof (struct nexthop)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 938 | nexthop->type = stream_getc (s); |
| 939 | switch (nexthop->type) |
| 940 | { |
| 941 | case ZEBRA_NEXTHOP_IPV6: |
| 942 | stream_get (&nexthop->gate.ipv6, s, 16); |
| 943 | break; |
| 944 | case ZEBRA_NEXTHOP_IPV6_IFINDEX: |
| 945 | case ZEBRA_NEXTHOP_IPV6_IFNAME: |
| 946 | stream_get (&nexthop->gate.ipv6, s, 16); |
| 947 | nexthop->ifindex = stream_getl (s); |
| 948 | break; |
| 949 | case ZEBRA_NEXTHOP_IFINDEX: |
| 950 | case ZEBRA_NEXTHOP_IFNAME: |
| 951 | nexthop->ifindex = stream_getl (s); |
| 952 | break; |
hasso | fa2b17e | 2004-03-04 17:45:00 +0000 | [diff] [blame] | 953 | default: |
| 954 | /* do nothing */ |
| 955 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 956 | } |
| 957 | bnc_nexthop_add (bnc, nexthop); |
| 958 | } |
| 959 | } |
| 960 | else |
| 961 | return NULL; |
| 962 | |
| 963 | return bnc; |
| 964 | } |
| 965 | |
| 966 | struct bgp_nexthop_cache * |
| 967 | zlookup_query_ipv6 (struct in6_addr *addr) |
| 968 | { |
| 969 | int ret; |
| 970 | struct stream *s; |
| 971 | |
| 972 | /* Check socket. */ |
| 973 | if (zlookup->sock < 0) |
| 974 | return NULL; |
| 975 | |
| 976 | s = zlookup->obuf; |
| 977 | stream_reset (s); |
paul | d3092e7 | 2006-01-17 17:33:46 +0000 | [diff] [blame] | 978 | zclient_create_header (s, ZEBRA_IPV6_NEXTHOP_LOOKUP); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 979 | stream_put (s, addr, 16); |
paul | d3092e7 | 2006-01-17 17:33:46 +0000 | [diff] [blame] | 980 | stream_putw_at (s, 0, stream_get_endp (s)); |
| 981 | |
| 982 | ret = writen (zlookup->sock, s->data, stream_get_endp (s)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 983 | if (ret < 0) |
| 984 | { |
| 985 | zlog_err ("can't write to zlookup->sock"); |
| 986 | close (zlookup->sock); |
| 987 | zlookup->sock = -1; |
| 988 | return NULL; |
| 989 | } |
| 990 | if (ret == 0) |
| 991 | { |
| 992 | zlog_err ("zlookup->sock connection closed"); |
| 993 | close (zlookup->sock); |
| 994 | zlookup->sock = -1; |
| 995 | return NULL; |
| 996 | } |
| 997 | |
| 998 | return zlookup_read_ipv6 (); |
| 999 | } |
| 1000 | #endif /* HAVE_IPV6 */ |
| 1001 | |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 1002 | static int |
| 1003 | bgp_import_check (struct prefix *p, u_int32_t *igpmetric, |
| 1004 | struct in_addr *igpnexthop) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1005 | { |
| 1006 | struct stream *s; |
| 1007 | int ret; |
paul | d3092e7 | 2006-01-17 17:33:46 +0000 | [diff] [blame] | 1008 | u_int16_t length, command; |
| 1009 | u_char version, marker; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1010 | int nbytes; |
| 1011 | struct in_addr addr; |
| 1012 | struct in_addr nexthop; |
| 1013 | u_int32_t metric = 0; |
| 1014 | u_char nexthop_num; |
| 1015 | u_char nexthop_type; |
| 1016 | |
| 1017 | /* If lookup connection is not available return valid. */ |
| 1018 | if (zlookup->sock < 0) |
| 1019 | { |
| 1020 | if (igpmetric) |
| 1021 | *igpmetric = 0; |
| 1022 | return 1; |
| 1023 | } |
| 1024 | |
| 1025 | /* Send query to the lookup connection */ |
| 1026 | s = zlookup->obuf; |
| 1027 | stream_reset (s); |
paul | d3092e7 | 2006-01-17 17:33:46 +0000 | [diff] [blame] | 1028 | zclient_create_header (s, ZEBRA_IPV4_IMPORT_LOOKUP); |
| 1029 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1030 | stream_putc (s, p->prefixlen); |
| 1031 | stream_put_in_addr (s, &p->u.prefix4); |
paul | d3092e7 | 2006-01-17 17:33:46 +0000 | [diff] [blame] | 1032 | |
| 1033 | stream_putw_at (s, 0, stream_get_endp (s)); |
| 1034 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1035 | /* Write the packet. */ |
paul | d3092e7 | 2006-01-17 17:33:46 +0000 | [diff] [blame] | 1036 | ret = writen (zlookup->sock, s->data, stream_get_endp (s)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1037 | |
| 1038 | if (ret < 0) |
| 1039 | { |
| 1040 | zlog_err ("can't write to zlookup->sock"); |
| 1041 | close (zlookup->sock); |
| 1042 | zlookup->sock = -1; |
| 1043 | return 1; |
| 1044 | } |
| 1045 | if (ret == 0) |
| 1046 | { |
| 1047 | zlog_err ("zlookup->sock connection closed"); |
| 1048 | close (zlookup->sock); |
| 1049 | zlookup->sock = -1; |
| 1050 | return 1; |
| 1051 | } |
| 1052 | |
| 1053 | /* Get result. */ |
| 1054 | stream_reset (s); |
| 1055 | |
| 1056 | /* Fetch length. */ |
| 1057 | nbytes = stream_read (s, zlookup->sock, 2); |
| 1058 | length = stream_getw (s); |
| 1059 | |
| 1060 | /* Fetch whole data. */ |
| 1061 | nbytes = stream_read (s, zlookup->sock, length - 2); |
paul | d3092e7 | 2006-01-17 17:33:46 +0000 | [diff] [blame] | 1062 | marker = stream_getc (s); |
| 1063 | version = stream_getc (s); |
| 1064 | |
| 1065 | if (version != ZSERV_VERSION || marker != ZEBRA_HEADER_MARKER) |
| 1066 | { |
| 1067 | zlog_err("%s: socket %d version mismatch, marker %d, version %d", |
| 1068 | __func__, zlookup->sock, marker, version); |
| 1069 | return 0; |
| 1070 | } |
| 1071 | |
| 1072 | command = stream_getw (s); |
| 1073 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1074 | addr.s_addr = stream_get_ipv4 (s); |
| 1075 | metric = stream_getl (s); |
| 1076 | nexthop_num = stream_getc (s); |
| 1077 | |
| 1078 | /* Set IGP metric value. */ |
| 1079 | if (igpmetric) |
| 1080 | *igpmetric = metric; |
| 1081 | |
| 1082 | /* If there is nexthop then this is active route. */ |
| 1083 | if (nexthop_num) |
| 1084 | { |
| 1085 | nexthop.s_addr = 0; |
| 1086 | nexthop_type = stream_getc (s); |
| 1087 | if (nexthop_type == ZEBRA_NEXTHOP_IPV4) |
| 1088 | { |
| 1089 | nexthop.s_addr = stream_get_ipv4 (s); |
| 1090 | if (igpnexthop) |
| 1091 | *igpnexthop = nexthop; |
| 1092 | } |
| 1093 | else |
| 1094 | *igpnexthop = nexthop; |
| 1095 | |
| 1096 | return 1; |
| 1097 | } |
| 1098 | else |
| 1099 | return 0; |
| 1100 | } |
| 1101 | |
| 1102 | /* Scan all configured BGP route then check the route exists in IGP or |
| 1103 | not. */ |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 1104 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1105 | bgp_import (struct thread *t) |
| 1106 | { |
| 1107 | struct bgp *bgp; |
| 1108 | struct bgp_node *rn; |
| 1109 | struct bgp_static *bgp_static; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 1110 | struct listnode *node, *nnode; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1111 | int valid; |
| 1112 | u_int32_t metric; |
| 1113 | struct in_addr nexthop; |
| 1114 | afi_t afi; |
| 1115 | safi_t safi; |
| 1116 | |
| 1117 | bgp_import_thread = |
| 1118 | thread_add_timer (master, bgp_import, NULL, bgp_import_interval); |
| 1119 | |
hasso | f418446 | 2005-02-01 20:13:16 +0000 | [diff] [blame] | 1120 | if (BGP_DEBUG (events, EVENTS)) |
| 1121 | zlog_debug ("Import timer expired."); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1122 | |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 1123 | for (ALL_LIST_ELEMENTS (bm->bgp, node, nnode, bgp)) |
paul | 6cbbc3c | 2003-04-28 17:11:02 +0000 | [diff] [blame] | 1124 | { |
| 1125 | for (afi = AFI_IP; afi < AFI_MAX; afi++) |
| 1126 | for (safi = SAFI_UNICAST; safi < SAFI_MPLS_VPN; safi++) |
| 1127 | for (rn = bgp_table_top (bgp->route[afi][safi]); rn; |
| 1128 | rn = bgp_route_next (rn)) |
| 1129 | if ((bgp_static = rn->info) != NULL) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1130 | { |
paul | 6cbbc3c | 2003-04-28 17:11:02 +0000 | [diff] [blame] | 1131 | if (bgp_static->backdoor) |
| 1132 | continue; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1133 | |
paul | 6cbbc3c | 2003-04-28 17:11:02 +0000 | [diff] [blame] | 1134 | valid = bgp_static->valid; |
| 1135 | metric = bgp_static->igpmetric; |
| 1136 | nexthop = bgp_static->igpnexthop; |
| 1137 | |
| 1138 | if (bgp_flag_check (bgp, BGP_FLAG_IMPORT_CHECK) |
| 1139 | && afi == AFI_IP && safi == SAFI_UNICAST) |
| 1140 | bgp_static->valid = bgp_import_check (&rn->p, &bgp_static->igpmetric, |
| 1141 | &bgp_static->igpnexthop); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1142 | else |
paul | 6cbbc3c | 2003-04-28 17:11:02 +0000 | [diff] [blame] | 1143 | { |
| 1144 | bgp_static->valid = 1; |
| 1145 | bgp_static->igpmetric = 0; |
| 1146 | bgp_static->igpnexthop.s_addr = 0; |
| 1147 | } |
| 1148 | |
| 1149 | if (bgp_static->valid != valid) |
| 1150 | { |
| 1151 | if (bgp_static->valid) |
| 1152 | bgp_static_update (bgp, &rn->p, bgp_static, afi, safi); |
| 1153 | else |
| 1154 | bgp_static_withdraw (bgp, &rn->p, afi, safi); |
| 1155 | } |
| 1156 | else if (bgp_static->valid) |
| 1157 | { |
| 1158 | if (bgp_static->igpmetric != metric |
| 1159 | || bgp_static->igpnexthop.s_addr != nexthop.s_addr |
| 1160 | || bgp_static->rmap.name) |
| 1161 | bgp_static_update (bgp, &rn->p, bgp_static, afi, safi); |
| 1162 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1163 | } |
paul | 6cbbc3c | 2003-04-28 17:11:02 +0000 | [diff] [blame] | 1164 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1165 | return 0; |
| 1166 | } |
| 1167 | |
| 1168 | /* Connect to zebra for nexthop lookup. */ |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 1169 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1170 | zlookup_connect (struct thread *t) |
| 1171 | { |
| 1172 | struct zclient *zlookup; |
| 1173 | |
| 1174 | zlookup = THREAD_ARG (t); |
| 1175 | zlookup->t_connect = NULL; |
| 1176 | |
| 1177 | if (zlookup->sock != -1) |
| 1178 | return 0; |
| 1179 | |
Vyacheslav Trushkin | b511468 | 2011-11-25 18:51:48 +0400 | [diff] [blame] | 1180 | if (zclient_socket_connect (zlookup) < 0) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1181 | return -1; |
| 1182 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1183 | return 0; |
| 1184 | } |
| 1185 | |
| 1186 | /* Check specified multiaccess next-hop. */ |
| 1187 | int |
| 1188 | bgp_multiaccess_check_v4 (struct in_addr nexthop, char *peer) |
| 1189 | { |
| 1190 | struct bgp_node *rn1; |
| 1191 | struct bgp_node *rn2; |
| 1192 | struct prefix p1; |
| 1193 | struct prefix p2; |
| 1194 | struct in_addr addr; |
| 1195 | int ret; |
| 1196 | |
| 1197 | ret = inet_aton (peer, &addr); |
| 1198 | if (! ret) |
| 1199 | return 0; |
| 1200 | |
| 1201 | memset (&p1, 0, sizeof (struct prefix)); |
| 1202 | p1.family = AF_INET; |
| 1203 | p1.prefixlen = IPV4_MAX_BITLEN; |
| 1204 | p1.u.prefix4 = nexthop; |
| 1205 | memset (&p2, 0, sizeof (struct prefix)); |
| 1206 | p2.family = AF_INET; |
| 1207 | p2.prefixlen = IPV4_MAX_BITLEN; |
| 1208 | p2.u.prefix4 = addr; |
| 1209 | |
| 1210 | /* If bgp scan is not enabled, return invalid. */ |
| 1211 | if (zlookup->sock < 0) |
| 1212 | return 0; |
| 1213 | |
paul | 5932020 | 2004-11-09 01:54:03 +0000 | [diff] [blame] | 1214 | rn1 = bgp_node_match (bgp_connected_table[AFI_IP], &p1); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1215 | if (! rn1) |
| 1216 | return 0; |
Chris Caputo | 6c88b44 | 2010-07-27 16:28:55 +0000 | [diff] [blame] | 1217 | bgp_unlock_node (rn1); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1218 | |
paul | 5932020 | 2004-11-09 01:54:03 +0000 | [diff] [blame] | 1219 | rn2 = bgp_node_match (bgp_connected_table[AFI_IP], &p2); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1220 | if (! rn2) |
| 1221 | return 0; |
Chris Caputo | 6c88b44 | 2010-07-27 16:28:55 +0000 | [diff] [blame] | 1222 | bgp_unlock_node (rn2); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1223 | |
Chris Caputo | 6c88b44 | 2010-07-27 16:28:55 +0000 | [diff] [blame] | 1224 | /* This is safe, even with above unlocks, since we are just |
| 1225 | comparing pointers to the objects, not the objects themselves. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1226 | if (rn1 == rn2) |
| 1227 | return 1; |
| 1228 | |
| 1229 | return 0; |
| 1230 | } |
| 1231 | |
| 1232 | DEFUN (bgp_scan_time, |
| 1233 | bgp_scan_time_cmd, |
| 1234 | "bgp scan-time <5-60>", |
| 1235 | "BGP specific commands\n" |
| 1236 | "Configure background scanner interval\n" |
| 1237 | "Scanner interval (seconds)\n") |
| 1238 | { |
| 1239 | bgp_scan_interval = atoi (argv[0]); |
| 1240 | |
| 1241 | if (bgp_scan_thread) |
| 1242 | { |
| 1243 | thread_cancel (bgp_scan_thread); |
| 1244 | bgp_scan_thread = |
paul | 5932020 | 2004-11-09 01:54:03 +0000 | [diff] [blame] | 1245 | thread_add_timer (master, bgp_scan_timer, NULL, bgp_scan_interval); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1246 | } |
| 1247 | |
| 1248 | return CMD_SUCCESS; |
| 1249 | } |
| 1250 | |
| 1251 | DEFUN (no_bgp_scan_time, |
| 1252 | no_bgp_scan_time_cmd, |
| 1253 | "no bgp scan-time", |
| 1254 | NO_STR |
| 1255 | "BGP specific commands\n" |
| 1256 | "Configure background scanner interval\n") |
| 1257 | { |
| 1258 | bgp_scan_interval = BGP_SCAN_INTERVAL_DEFAULT; |
| 1259 | |
| 1260 | if (bgp_scan_thread) |
| 1261 | { |
| 1262 | thread_cancel (bgp_scan_thread); |
| 1263 | bgp_scan_thread = |
paul | 5932020 | 2004-11-09 01:54:03 +0000 | [diff] [blame] | 1264 | thread_add_timer (master, bgp_scan_timer, NULL, bgp_scan_interval); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1265 | } |
| 1266 | |
| 1267 | return CMD_SUCCESS; |
| 1268 | } |
| 1269 | |
| 1270 | ALIAS (no_bgp_scan_time, |
| 1271 | no_bgp_scan_time_val_cmd, |
| 1272 | "no bgp scan-time <5-60>", |
| 1273 | NO_STR |
| 1274 | "BGP specific commands\n" |
| 1275 | "Configure background scanner interval\n" |
| 1276 | "Scanner interval (seconds)\n") |
| 1277 | |
Denis Ovsienko | 318f0d8 | 2011-08-05 21:47:08 +0400 | [diff] [blame] | 1278 | static int |
| 1279 | show_ip_bgp_scan_tables (struct vty *vty, const char detail) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1280 | { |
| 1281 | struct bgp_node *rn; |
| 1282 | struct bgp_nexthop_cache *bnc; |
Denis Ovsienko | 318f0d8 | 2011-08-05 21:47:08 +0400 | [diff] [blame] | 1283 | char buf[INET6_ADDRSTRLEN]; |
| 1284 | u_char i; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1285 | |
| 1286 | if (bgp_scan_thread) |
| 1287 | vty_out (vty, "BGP scan is running%s", VTY_NEWLINE); |
| 1288 | else |
| 1289 | vty_out (vty, "BGP scan is not running%s", VTY_NEWLINE); |
| 1290 | vty_out (vty, "BGP scan interval is %d%s", bgp_scan_interval, VTY_NEWLINE); |
| 1291 | |
| 1292 | vty_out (vty, "Current BGP nexthop cache:%s", VTY_NEWLINE); |
paul | 5932020 | 2004-11-09 01:54:03 +0000 | [diff] [blame] | 1293 | 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] | 1294 | if ((bnc = rn->info) != NULL) |
| 1295 | { |
| 1296 | if (bnc->valid) |
Denis Ovsienko | 318f0d8 | 2011-08-05 21:47:08 +0400 | [diff] [blame] | 1297 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1298 | vty_out (vty, " %s valid [IGP metric %d]%s", |
Denis Ovsienko | 318f0d8 | 2011-08-05 21:47:08 +0400 | [diff] [blame] | 1299 | inet_ntop (AF_INET, &rn->p.u.prefix4, buf, INET6_ADDRSTRLEN), bnc->metric, VTY_NEWLINE); |
| 1300 | if (detail) |
| 1301 | for (i = 0; i < bnc->nexthop_num; i++) |
Denis Ovsienko | 0e8032d | 2011-08-09 14:42:58 +0400 | [diff] [blame] | 1302 | switch (bnc->nexthop[i].type) |
| 1303 | { |
| 1304 | case NEXTHOP_TYPE_IPV4: |
| 1305 | vty_out (vty, " gate %s%s", inet_ntop (AF_INET, &bnc->nexthop[i].gate.ipv4, buf, INET6_ADDRSTRLEN), VTY_NEWLINE); |
| 1306 | break; |
| 1307 | case NEXTHOP_TYPE_IFINDEX: |
| 1308 | vty_out (vty, " ifidx %u%s", bnc->nexthop[i].ifindex, VTY_NEWLINE); |
| 1309 | break; |
| 1310 | default: |
| 1311 | vty_out (vty, " invalid nexthop type %u%s", bnc->nexthop[i].type, VTY_NEWLINE); |
| 1312 | } |
Denis Ovsienko | 318f0d8 | 2011-08-05 21:47:08 +0400 | [diff] [blame] | 1313 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1314 | else |
| 1315 | vty_out (vty, " %s invalid%s", |
Denis Ovsienko | 318f0d8 | 2011-08-05 21:47:08 +0400 | [diff] [blame] | 1316 | inet_ntop (AF_INET, &rn->p.u.prefix4, buf, INET6_ADDRSTRLEN), VTY_NEWLINE); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1317 | } |
| 1318 | |
| 1319 | #ifdef HAVE_IPV6 |
| 1320 | { |
paul | 5932020 | 2004-11-09 01:54:03 +0000 | [diff] [blame] | 1321 | for (rn = bgp_table_top (bgp_nexthop_cache_table[AFI_IP6]); |
| 1322 | rn; |
| 1323 | rn = bgp_route_next (rn)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1324 | if ((bnc = rn->info) != NULL) |
| 1325 | { |
| 1326 | if (bnc->valid) |
Denis Ovsienko | 318f0d8 | 2011-08-05 21:47:08 +0400 | [diff] [blame] | 1327 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1328 | vty_out (vty, " %s valid [IGP metric %d]%s", |
Denis Ovsienko | 318f0d8 | 2011-08-05 21:47:08 +0400 | [diff] [blame] | 1329 | inet_ntop (AF_INET6, &rn->p.u.prefix6, buf, INET6_ADDRSTRLEN), |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1330 | bnc->metric, VTY_NEWLINE); |
Denis Ovsienko | 318f0d8 | 2011-08-05 21:47:08 +0400 | [diff] [blame] | 1331 | if (detail) |
| 1332 | for (i = 0; i < bnc->nexthop_num; i++) |
Denis Ovsienko | 0e8032d | 2011-08-09 14:42:58 +0400 | [diff] [blame] | 1333 | switch (bnc->nexthop[i].type) |
| 1334 | { |
| 1335 | case NEXTHOP_TYPE_IPV6: |
| 1336 | vty_out (vty, " gate %s%s", inet_ntop (AF_INET6, &bnc->nexthop[i].gate.ipv6, buf, INET6_ADDRSTRLEN), VTY_NEWLINE); |
| 1337 | break; |
| 1338 | case NEXTHOP_TYPE_IFINDEX: |
| 1339 | vty_out (vty, " ifidx %u%s", bnc->nexthop[i].ifindex, VTY_NEWLINE); |
| 1340 | break; |
| 1341 | default: |
| 1342 | vty_out (vty, " invalid nexthop type %u%s", bnc->nexthop[i].type, VTY_NEWLINE); |
| 1343 | } |
Denis Ovsienko | 318f0d8 | 2011-08-05 21:47:08 +0400 | [diff] [blame] | 1344 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1345 | else |
| 1346 | vty_out (vty, " %s invalid%s", |
Denis Ovsienko | 318f0d8 | 2011-08-05 21:47:08 +0400 | [diff] [blame] | 1347 | inet_ntop (AF_INET6, &rn->p.u.prefix6, buf, INET6_ADDRSTRLEN), |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1348 | VTY_NEWLINE); |
| 1349 | } |
| 1350 | } |
| 1351 | #endif /* HAVE_IPV6 */ |
| 1352 | |
| 1353 | vty_out (vty, "BGP connected route:%s", VTY_NEWLINE); |
paul | 5932020 | 2004-11-09 01:54:03 +0000 | [diff] [blame] | 1354 | for (rn = bgp_table_top (bgp_connected_table[AFI_IP]); |
| 1355 | rn; |
| 1356 | rn = bgp_route_next (rn)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1357 | if (rn->info != NULL) |
| 1358 | vty_out (vty, " %s/%d%s", inet_ntoa (rn->p.u.prefix4), rn->p.prefixlen, |
| 1359 | VTY_NEWLINE); |
| 1360 | |
| 1361 | #ifdef HAVE_IPV6 |
| 1362 | { |
paul | 5932020 | 2004-11-09 01:54:03 +0000 | [diff] [blame] | 1363 | for (rn = bgp_table_top (bgp_connected_table[AFI_IP6]); |
| 1364 | rn; |
| 1365 | rn = bgp_route_next (rn)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1366 | if (rn->info != NULL) |
| 1367 | vty_out (vty, " %s/%d%s", |
Denis Ovsienko | 318f0d8 | 2011-08-05 21:47:08 +0400 | [diff] [blame] | 1368 | inet_ntop (AF_INET6, &rn->p.u.prefix6, buf, INET6_ADDRSTRLEN), |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1369 | rn->p.prefixlen, |
| 1370 | VTY_NEWLINE); |
| 1371 | } |
| 1372 | #endif /* HAVE_IPV6 */ |
| 1373 | |
| 1374 | return CMD_SUCCESS; |
| 1375 | } |
| 1376 | |
Denis Ovsienko | 318f0d8 | 2011-08-05 21:47:08 +0400 | [diff] [blame] | 1377 | DEFUN (show_ip_bgp_scan, |
| 1378 | show_ip_bgp_scan_cmd, |
| 1379 | "show ip bgp scan", |
| 1380 | SHOW_STR |
| 1381 | IP_STR |
| 1382 | BGP_STR |
| 1383 | "BGP scan status\n") |
| 1384 | { |
| 1385 | return show_ip_bgp_scan_tables (vty, 0); |
| 1386 | } |
| 1387 | |
| 1388 | DEFUN (show_ip_bgp_scan_detail, |
| 1389 | show_ip_bgp_scan_detail_cmd, |
| 1390 | "show ip bgp scan detail", |
| 1391 | SHOW_STR |
| 1392 | IP_STR |
| 1393 | BGP_STR |
| 1394 | "BGP scan status\n" |
| 1395 | "More detailed output\n") |
| 1396 | { |
| 1397 | return show_ip_bgp_scan_tables (vty, 1); |
| 1398 | } |
| 1399 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1400 | int |
| 1401 | bgp_config_write_scan_time (struct vty *vty) |
| 1402 | { |
| 1403 | if (bgp_scan_interval != BGP_SCAN_INTERVAL_DEFAULT) |
| 1404 | vty_out (vty, " bgp scan-time %d%s", bgp_scan_interval, VTY_NEWLINE); |
| 1405 | return CMD_SUCCESS; |
| 1406 | } |
| 1407 | |
| 1408 | void |
Stephen Hemminger | 66e5cd8 | 2009-02-09 10:14:16 -0800 | [diff] [blame] | 1409 | bgp_scan_init (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1410 | { |
| 1411 | zlookup = zclient_new (); |
| 1412 | zlookup->sock = -1; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1413 | zlookup->t_connect = thread_add_event (master, zlookup_connect, zlookup, 0); |
| 1414 | |
| 1415 | bgp_scan_interval = BGP_SCAN_INTERVAL_DEFAULT; |
| 1416 | bgp_import_interval = BGP_IMPORT_INTERVAL_DEFAULT; |
| 1417 | |
Paul Jakma | 64e580a | 2006-02-21 01:09:01 +0000 | [diff] [blame] | 1418 | cache1_table[AFI_IP] = bgp_table_init (AFI_IP, SAFI_UNICAST); |
| 1419 | cache2_table[AFI_IP] = bgp_table_init (AFI_IP, SAFI_UNICAST); |
paul | 5932020 | 2004-11-09 01:54:03 +0000 | [diff] [blame] | 1420 | bgp_nexthop_cache_table[AFI_IP] = cache1_table[AFI_IP]; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1421 | |
Paul Jakma | 64e580a | 2006-02-21 01:09:01 +0000 | [diff] [blame] | 1422 | bgp_connected_table[AFI_IP] = bgp_table_init (AFI_IP, SAFI_UNICAST); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1423 | |
| 1424 | #ifdef HAVE_IPV6 |
Paul Jakma | 64e580a | 2006-02-21 01:09:01 +0000 | [diff] [blame] | 1425 | cache1_table[AFI_IP6] = bgp_table_init (AFI_IP6, SAFI_UNICAST); |
| 1426 | cache2_table[AFI_IP6] = bgp_table_init (AFI_IP6, SAFI_UNICAST); |
paul | 5932020 | 2004-11-09 01:54:03 +0000 | [diff] [blame] | 1427 | bgp_nexthop_cache_table[AFI_IP6] = cache1_table[AFI_IP6]; |
Paul Jakma | 64e580a | 2006-02-21 01:09:01 +0000 | [diff] [blame] | 1428 | bgp_connected_table[AFI_IP6] = bgp_table_init (AFI_IP6, SAFI_UNICAST); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1429 | #endif /* HAVE_IPV6 */ |
| 1430 | |
| 1431 | /* Make BGP scan thread. */ |
paul | 5932020 | 2004-11-09 01:54:03 +0000 | [diff] [blame] | 1432 | bgp_scan_thread = thread_add_timer (master, bgp_scan_timer, |
| 1433 | NULL, bgp_scan_interval); |
paul | 6cbbc3c | 2003-04-28 17:11:02 +0000 | [diff] [blame] | 1434 | /* Make BGP import there. */ |
| 1435 | bgp_import_thread = thread_add_timer (master, bgp_import, NULL, 0); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1436 | |
| 1437 | install_element (BGP_NODE, &bgp_scan_time_cmd); |
| 1438 | install_element (BGP_NODE, &no_bgp_scan_time_cmd); |
| 1439 | install_element (BGP_NODE, &no_bgp_scan_time_val_cmd); |
| 1440 | install_element (VIEW_NODE, &show_ip_bgp_scan_cmd); |
Denis Ovsienko | 318f0d8 | 2011-08-05 21:47:08 +0400 | [diff] [blame] | 1441 | install_element (VIEW_NODE, &show_ip_bgp_scan_detail_cmd); |
Paul Jakma | 62687ff | 2008-08-23 14:27:06 +0100 | [diff] [blame] | 1442 | install_element (RESTRICTED_NODE, &show_ip_bgp_scan_cmd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1443 | install_element (ENABLE_NODE, &show_ip_bgp_scan_cmd); |
Denis Ovsienko | 318f0d8 | 2011-08-05 21:47:08 +0400 | [diff] [blame] | 1444 | install_element (ENABLE_NODE, &show_ip_bgp_scan_detail_cmd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1445 | } |
Chris Caputo | 228da42 | 2009-07-18 05:44:03 +0000 | [diff] [blame] | 1446 | |
| 1447 | void |
| 1448 | bgp_scan_finish (void) |
| 1449 | { |
Chris Caputo | 6c88b44 | 2010-07-27 16:28:55 +0000 | [diff] [blame] | 1450 | /* Only the current one needs to be reset. */ |
| 1451 | bgp_nexthop_cache_reset (bgp_nexthop_cache_table[AFI_IP]); |
| 1452 | |
Chris Caputo | 228da42 | 2009-07-18 05:44:03 +0000 | [diff] [blame] | 1453 | bgp_table_unlock (cache1_table[AFI_IP]); |
| 1454 | cache1_table[AFI_IP] = NULL; |
| 1455 | |
| 1456 | bgp_table_unlock (cache2_table[AFI_IP]); |
| 1457 | cache2_table[AFI_IP] = NULL; |
| 1458 | |
| 1459 | bgp_table_unlock (bgp_connected_table[AFI_IP]); |
| 1460 | bgp_connected_table[AFI_IP] = NULL; |
| 1461 | |
| 1462 | #ifdef HAVE_IPV6 |
Chris Caputo | 6c88b44 | 2010-07-27 16:28:55 +0000 | [diff] [blame] | 1463 | /* Only the current one needs to be reset. */ |
| 1464 | bgp_nexthop_cache_reset (bgp_nexthop_cache_table[AFI_IP6]); |
| 1465 | |
Chris Caputo | 228da42 | 2009-07-18 05:44:03 +0000 | [diff] [blame] | 1466 | bgp_table_unlock (cache1_table[AFI_IP6]); |
| 1467 | cache1_table[AFI_IP6] = NULL; |
| 1468 | |
| 1469 | bgp_table_unlock (cache2_table[AFI_IP6]); |
| 1470 | cache2_table[AFI_IP6] = NULL; |
| 1471 | |
| 1472 | bgp_table_unlock (bgp_connected_table[AFI_IP6]); |
| 1473 | bgp_connected_table[AFI_IP6] = NULL; |
| 1474 | #endif /* HAVE_IPV6 */ |
| 1475 | } |