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" |
Donald Sharp | 0490729 | 2016-01-07 10:03:01 -0500 | [diff] [blame] | 33 | #include "filter.h" |
Pradosh Mohapatra | 60cc959 | 2015-11-09 20:21:41 -0500 | [diff] [blame] | 34 | #include "nexthop.h" |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 35 | |
| 36 | #include "bgpd/bgpd.h" |
| 37 | #include "bgpd/bgp_table.h" |
| 38 | #include "bgpd/bgp_route.h" |
| 39 | #include "bgpd/bgp_attr.h" |
| 40 | #include "bgpd/bgp_nexthop.h" |
Pradosh Mohapatra | 60cc959 | 2015-11-09 20:21:41 -0500 | [diff] [blame] | 41 | #include "bgpd/bgp_nht.h" |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 42 | #include "bgpd/bgp_debug.h" |
| 43 | #include "bgpd/bgp_damp.h" |
| 44 | #include "zebra/rib.h" |
| 45 | #include "zebra/zserv.h" /* For ZEBRA_SERV_PATH. */ |
| 46 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 47 | |
| 48 | /* Route table for next-hop lookup cache. */ |
Pradosh Mohapatra | 60cc959 | 2015-11-09 20:21:41 -0500 | [diff] [blame] | 49 | struct bgp_table *bgp_nexthop_cache_table[AFI_MAX]; |
paul | 00d252c | 2005-05-23 14:19:54 +0000 | [diff] [blame] | 50 | static struct bgp_table *cache1_table[AFI_MAX]; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 51 | |
| 52 | /* Route table for connected route. */ |
paul | 00d252c | 2005-05-23 14:19:54 +0000 | [diff] [blame] | 53 | static struct bgp_table *bgp_connected_table[AFI_MAX]; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 54 | |
Pradosh Mohapatra | 60cc959 | 2015-11-09 20:21:41 -0500 | [diff] [blame] | 55 | char * |
| 56 | bnc_str (struct bgp_nexthop_cache *bnc, char *buf, int size) |
| 57 | { |
| 58 | prefix2str(&(bnc->node->p), buf, size); |
| 59 | return buf; |
| 60 | } |
| 61 | |
Pradosh Mohapatra | 60cc959 | 2015-11-09 20:21:41 -0500 | [diff] [blame] | 62 | void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 63 | bnc_nexthop_free (struct bgp_nexthop_cache *bnc) |
| 64 | { |
| 65 | struct nexthop *nexthop; |
| 66 | struct nexthop *next = NULL; |
| 67 | |
| 68 | for (nexthop = bnc->nexthop; nexthop; nexthop = next) |
| 69 | { |
| 70 | next = nexthop->next; |
| 71 | XFREE (MTYPE_NEXTHOP, nexthop); |
| 72 | } |
| 73 | } |
| 74 | |
Pradosh Mohapatra | 60cc959 | 2015-11-09 20:21:41 -0500 | [diff] [blame] | 75 | struct bgp_nexthop_cache * |
Stephen Hemminger | 66e5cd8 | 2009-02-09 10:14:16 -0800 | [diff] [blame] | 76 | bnc_new (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 77 | { |
Pradosh Mohapatra | 60cc959 | 2015-11-09 20:21:41 -0500 | [diff] [blame] | 78 | struct bgp_nexthop_cache *bnc; |
| 79 | |
| 80 | bnc = XCALLOC (MTYPE_BGP_NEXTHOP_CACHE, sizeof (struct bgp_nexthop_cache)); |
| 81 | LIST_INIT(&(bnc->paths)); |
| 82 | return bnc; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Pradosh Mohapatra | 60cc959 | 2015-11-09 20:21:41 -0500 | [diff] [blame] | 85 | void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 86 | bnc_free (struct bgp_nexthop_cache *bnc) |
| 87 | { |
| 88 | bnc_nexthop_free (bnc); |
| 89 | XFREE (MTYPE_BGP_NEXTHOP_CACHE, bnc); |
| 90 | } |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 91 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 92 | /* If nexthop exists on connected network return 1. */ |
| 93 | int |
Denis Ovsienko | 8e80bdf | 2011-08-05 18:52:52 +0400 | [diff] [blame] | 94 | bgp_nexthop_onlink (afi_t afi, struct attr *attr) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 95 | { |
| 96 | struct bgp_node *rn; |
Paul Jakma | fc98d16 | 2012-01-09 11:36:23 +0000 | [diff] [blame] | 97 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 98 | /* Lookup the address is onlink or not. */ |
| 99 | if (afi == AFI_IP) |
| 100 | { |
paul | 5932020 | 2004-11-09 01:54:03 +0000 | [diff] [blame] | 101 | rn = bgp_node_match_ipv4 (bgp_connected_table[AFI_IP], &attr->nexthop); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 102 | if (rn) |
| 103 | { |
| 104 | bgp_unlock_node (rn); |
| 105 | return 1; |
| 106 | } |
| 107 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 108 | else if (afi == AFI_IP6) |
| 109 | { |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 110 | if (attr->extra->mp_nexthop_len == 32) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 111 | return 1; |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 112 | else if (attr->extra->mp_nexthop_len == 16) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 113 | { |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 114 | if (IN6_IS_ADDR_LINKLOCAL (&attr->extra->mp_nexthop_global)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 115 | return 1; |
| 116 | |
paul | 5932020 | 2004-11-09 01:54:03 +0000 | [diff] [blame] | 117 | rn = bgp_node_match_ipv6 (bgp_connected_table[AFI_IP6], |
Paul Jakma | fb982c2 | 2007-05-04 20:15:47 +0000 | [diff] [blame] | 118 | &attr->extra->mp_nexthop_global); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 119 | if (rn) |
| 120 | { |
| 121 | bgp_unlock_node (rn); |
| 122 | return 1; |
| 123 | } |
| 124 | } |
| 125 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 126 | return 0; |
| 127 | } |
| 128 | |
Jorge Boncompte [DTI2] | 10f9bf3 | 2012-05-07 16:52:52 +0000 | [diff] [blame] | 129 | /* BGP own address structure */ |
| 130 | struct bgp_addr |
| 131 | { |
| 132 | struct in_addr addr; |
| 133 | int refcnt; |
| 134 | }; |
| 135 | |
| 136 | static struct hash *bgp_address_hash; |
| 137 | |
| 138 | static void * |
| 139 | bgp_address_hash_alloc (void *p) |
| 140 | { |
| 141 | struct in_addr *val = p; |
| 142 | struct bgp_addr *addr; |
| 143 | |
| 144 | addr = XMALLOC (MTYPE_BGP_ADDR, sizeof (struct bgp_addr)); |
| 145 | addr->refcnt = 0; |
| 146 | addr->addr.s_addr = val->s_addr; |
| 147 | |
| 148 | return addr; |
| 149 | } |
| 150 | |
| 151 | static unsigned int |
| 152 | bgp_address_hash_key_make (void *p) |
| 153 | { |
| 154 | const struct bgp_addr *addr = p; |
| 155 | |
| 156 | return jhash_1word(addr->addr.s_addr, 0); |
| 157 | } |
| 158 | |
| 159 | static int |
| 160 | bgp_address_hash_cmp (const void *p1, const void *p2) |
| 161 | { |
| 162 | const struct bgp_addr *addr1 = p1; |
| 163 | const struct bgp_addr *addr2 = p2; |
| 164 | |
| 165 | return addr1->addr.s_addr == addr2->addr.s_addr; |
| 166 | } |
| 167 | |
| 168 | void |
| 169 | bgp_address_init (void) |
| 170 | { |
| 171 | bgp_address_hash = hash_create (bgp_address_hash_key_make, |
| 172 | bgp_address_hash_cmp); |
| 173 | } |
| 174 | |
Lou Berger | 82dd707 | 2016-01-12 13:41:57 -0500 | [diff] [blame] | 175 | void |
| 176 | bgp_address_destroy (void) |
| 177 | { |
Lou Berger | d5d5e3e | 2016-01-12 13:41:58 -0500 | [diff] [blame] | 178 | if (bgp_address_hash == NULL) |
| 179 | return; |
| 180 | |
Lou Berger | 82dd707 | 2016-01-12 13:41:57 -0500 | [diff] [blame] | 181 | hash_clean(bgp_address_hash, NULL); |
| 182 | hash_free(bgp_address_hash); |
| 183 | bgp_address_hash = NULL; |
| 184 | } |
| 185 | |
Jorge Boncompte [DTI2] | 10f9bf3 | 2012-05-07 16:52:52 +0000 | [diff] [blame] | 186 | static void |
| 187 | bgp_address_add (struct prefix *p) |
| 188 | { |
| 189 | struct bgp_addr tmp; |
| 190 | struct bgp_addr *addr; |
| 191 | |
| 192 | tmp.addr = p->u.prefix4; |
| 193 | |
| 194 | addr = hash_get (bgp_address_hash, &tmp, bgp_address_hash_alloc); |
Pradosh Mohapatra | 94627e6 | 2015-11-09 20:21:45 -0500 | [diff] [blame] | 195 | if (!addr) |
| 196 | return; |
| 197 | |
Jorge Boncompte [DTI2] | 10f9bf3 | 2012-05-07 16:52:52 +0000 | [diff] [blame] | 198 | addr->refcnt++; |
| 199 | } |
| 200 | |
| 201 | static void |
| 202 | bgp_address_del (struct prefix *p) |
| 203 | { |
| 204 | struct bgp_addr tmp; |
| 205 | struct bgp_addr *addr; |
| 206 | |
| 207 | tmp.addr = p->u.prefix4; |
| 208 | |
| 209 | addr = hash_lookup (bgp_address_hash, &tmp); |
Rakesh Garimella | 9e47abd | 2013-03-11 12:38:31 +0000 | [diff] [blame] | 210 | /* may have been deleted earlier by bgp_interface_down() */ |
| 211 | if (addr == NULL) |
| 212 | return; |
| 213 | |
Jorge Boncompte [DTI2] | 10f9bf3 | 2012-05-07 16:52:52 +0000 | [diff] [blame] | 214 | addr->refcnt--; |
| 215 | |
| 216 | if (addr->refcnt == 0) |
| 217 | { |
| 218 | hash_release (bgp_address_hash, addr); |
| 219 | XFREE (MTYPE_BGP_ADDR, addr); |
| 220 | } |
| 221 | } |
| 222 | |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 223 | |
paul | 5932020 | 2004-11-09 01:54:03 +0000 | [diff] [blame] | 224 | struct bgp_connected_ref |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 225 | { |
| 226 | unsigned int refcnt; |
| 227 | }; |
| 228 | |
| 229 | void |
| 230 | bgp_connected_add (struct connected *ifc) |
| 231 | { |
| 232 | struct prefix p; |
| 233 | struct prefix *addr; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 234 | struct interface *ifp; |
| 235 | struct bgp_node *rn; |
paul | 5932020 | 2004-11-09 01:54:03 +0000 | [diff] [blame] | 236 | struct bgp_connected_ref *bc; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 237 | |
| 238 | ifp = ifc->ifp; |
| 239 | |
| 240 | if (! ifp) |
| 241 | return; |
| 242 | |
| 243 | if (if_is_loopback (ifp)) |
| 244 | return; |
| 245 | |
| 246 | addr = ifc->address; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 247 | |
Donald Sharp | 26a18eb | 2015-09-29 09:25:10 -0400 | [diff] [blame] | 248 | p = *(CONNECTED_PREFIX(ifc)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 249 | if (addr->family == AF_INET) |
| 250 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 251 | apply_mask_ipv4 ((struct prefix_ipv4 *) &p); |
| 252 | |
| 253 | if (prefix_ipv4_any ((struct prefix_ipv4 *) &p)) |
| 254 | return; |
| 255 | |
Jorge Boncompte [DTI2] | 10f9bf3 | 2012-05-07 16:52:52 +0000 | [diff] [blame] | 256 | bgp_address_add (addr); |
| 257 | |
paul | 5932020 | 2004-11-09 01:54:03 +0000 | [diff] [blame] | 258 | rn = bgp_node_get (bgp_connected_table[AFI_IP], (struct prefix *) &p); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 259 | if (rn->info) |
| 260 | { |
| 261 | bc = rn->info; |
| 262 | bc->refcnt++; |
| 263 | } |
| 264 | else |
| 265 | { |
Chris Caputo | 6c88b44 | 2010-07-27 16:28:55 +0000 | [diff] [blame] | 266 | bc = XCALLOC (MTYPE_BGP_CONN, sizeof (struct bgp_connected_ref)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 267 | bc->refcnt = 1; |
| 268 | rn->info = bc; |
| 269 | } |
| 270 | } |
Andrew J. Schorr | e452963 | 2006-12-12 19:18:21 +0000 | [diff] [blame] | 271 | else if (addr->family == AF_INET6) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 272 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 273 | apply_mask_ipv6 ((struct prefix_ipv6 *) &p); |
| 274 | |
| 275 | if (IN6_IS_ADDR_UNSPECIFIED (&p.u.prefix6)) |
| 276 | return; |
| 277 | |
| 278 | if (IN6_IS_ADDR_LINKLOCAL (&p.u.prefix6)) |
| 279 | return; |
| 280 | |
paul | 5932020 | 2004-11-09 01:54:03 +0000 | [diff] [blame] | 281 | rn = bgp_node_get (bgp_connected_table[AFI_IP6], (struct prefix *) &p); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 282 | if (rn->info) |
| 283 | { |
| 284 | bc = rn->info; |
| 285 | bc->refcnt++; |
| 286 | } |
| 287 | else |
| 288 | { |
Chris Caputo | 6c88b44 | 2010-07-27 16:28:55 +0000 | [diff] [blame] | 289 | bc = XCALLOC (MTYPE_BGP_CONN, sizeof (struct bgp_connected_ref)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 290 | bc->refcnt = 1; |
| 291 | rn->info = bc; |
| 292 | } |
| 293 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | void |
| 297 | bgp_connected_delete (struct connected *ifc) |
| 298 | { |
| 299 | struct prefix p; |
| 300 | struct prefix *addr; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 301 | struct interface *ifp; |
| 302 | struct bgp_node *rn; |
paul | 5932020 | 2004-11-09 01:54:03 +0000 | [diff] [blame] | 303 | struct bgp_connected_ref *bc; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 304 | |
| 305 | ifp = ifc->ifp; |
| 306 | |
| 307 | if (if_is_loopback (ifp)) |
| 308 | return; |
| 309 | |
| 310 | addr = ifc->address; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 311 | |
Donald Sharp | 26a18eb | 2015-09-29 09:25:10 -0400 | [diff] [blame] | 312 | p = *(CONNECTED_PREFIX(ifc)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 313 | if (addr->family == AF_INET) |
| 314 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 315 | apply_mask_ipv4 ((struct prefix_ipv4 *) &p); |
| 316 | |
| 317 | if (prefix_ipv4_any ((struct prefix_ipv4 *) &p)) |
| 318 | return; |
| 319 | |
Jorge Boncompte [DTI2] | 10f9bf3 | 2012-05-07 16:52:52 +0000 | [diff] [blame] | 320 | bgp_address_del (addr); |
| 321 | |
paul | 5932020 | 2004-11-09 01:54:03 +0000 | [diff] [blame] | 322 | rn = bgp_node_lookup (bgp_connected_table[AFI_IP], &p); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 323 | if (! rn) |
| 324 | return; |
| 325 | |
| 326 | bc = rn->info; |
| 327 | bc->refcnt--; |
| 328 | if (bc->refcnt == 0) |
| 329 | { |
Chris Caputo | 6c88b44 | 2010-07-27 16:28:55 +0000 | [diff] [blame] | 330 | XFREE (MTYPE_BGP_CONN, bc); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 331 | rn->info = NULL; |
| 332 | } |
| 333 | bgp_unlock_node (rn); |
| 334 | bgp_unlock_node (rn); |
| 335 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 336 | else if (addr->family == AF_INET6) |
| 337 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 338 | apply_mask_ipv6 ((struct prefix_ipv6 *) &p); |
| 339 | |
| 340 | if (IN6_IS_ADDR_UNSPECIFIED (&p.u.prefix6)) |
| 341 | return; |
| 342 | |
| 343 | if (IN6_IS_ADDR_LINKLOCAL (&p.u.prefix6)) |
| 344 | return; |
| 345 | |
paul | 5932020 | 2004-11-09 01:54:03 +0000 | [diff] [blame] | 346 | rn = bgp_node_lookup (bgp_connected_table[AFI_IP6], (struct prefix *) &p); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 347 | if (! rn) |
| 348 | return; |
| 349 | |
| 350 | bc = rn->info; |
| 351 | bc->refcnt--; |
| 352 | if (bc->refcnt == 0) |
| 353 | { |
Chris Caputo | 6c88b44 | 2010-07-27 16:28:55 +0000 | [diff] [blame] | 354 | XFREE (MTYPE_BGP_CONN, bc); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 355 | rn->info = NULL; |
| 356 | } |
| 357 | bgp_unlock_node (rn); |
| 358 | bgp_unlock_node (rn); |
| 359 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | int |
Jorge Boncompte [DTI2] | 10f9bf3 | 2012-05-07 16:52:52 +0000 | [diff] [blame] | 363 | bgp_nexthop_self (struct attr *attr) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 364 | { |
Jorge Boncompte [DTI2] | 10f9bf3 | 2012-05-07 16:52:52 +0000 | [diff] [blame] | 365 | struct bgp_addr tmp, *addr; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 366 | |
Jorge Boncompte [DTI2] | 10f9bf3 | 2012-05-07 16:52:52 +0000 | [diff] [blame] | 367 | tmp.addr = attr->nexthop; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 368 | |
Jorge Boncompte [DTI2] | 10f9bf3 | 2012-05-07 16:52:52 +0000 | [diff] [blame] | 369 | addr = hash_lookup (bgp_address_hash, &tmp); |
| 370 | if (addr) |
| 371 | return 1; |
| 372 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 373 | return 0; |
| 374 | } |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 375 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 376 | int |
Dinesh Dutt | d9ab53a | 2015-05-19 17:47:21 -0700 | [diff] [blame] | 377 | bgp_multiaccess_check_v4 (struct in_addr nexthop, struct peer *peer) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 378 | { |
| 379 | struct bgp_node *rn1; |
| 380 | struct bgp_node *rn2; |
Dinesh Dutt | d9ab53a | 2015-05-19 17:47:21 -0700 | [diff] [blame] | 381 | struct prefix p; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 382 | int ret; |
| 383 | |
Dinesh Dutt | d9ab53a | 2015-05-19 17:47:21 -0700 | [diff] [blame] | 384 | p.family = AF_INET; |
| 385 | p.prefixlen = IPV4_MAX_BITLEN; |
| 386 | p.u.prefix4 = nexthop; |
| 387 | |
| 388 | rn1 = bgp_node_match (bgp_connected_table[AFI_IP], &p); |
| 389 | if (!rn1) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 390 | return 0; |
| 391 | |
Dinesh Dutt | d9ab53a | 2015-05-19 17:47:21 -0700 | [diff] [blame] | 392 | p.family = AF_INET; |
| 393 | p.prefixlen = IPV4_MAX_BITLEN; |
| 394 | p.u.prefix4 = peer->su.sin.sin_addr; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 395 | |
Dinesh Dutt | d9ab53a | 2015-05-19 17:47:21 -0700 | [diff] [blame] | 396 | rn2 = bgp_node_match (bgp_connected_table[AFI_IP], &p); |
| 397 | if (!rn2) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 398 | { |
Dinesh Dutt | d9ab53a | 2015-05-19 17:47:21 -0700 | [diff] [blame] | 399 | bgp_unlock_node(rn1); |
| 400 | return 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 401 | } |
| 402 | |
Dinesh Dutt | d9ab53a | 2015-05-19 17:47:21 -0700 | [diff] [blame] | 403 | ret = (rn1 == rn2) ? 1 : 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 404 | |
Dinesh Dutt | d9ab53a | 2015-05-19 17:47:21 -0700 | [diff] [blame] | 405 | bgp_unlock_node(rn1); |
| 406 | bgp_unlock_node(rn2); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 407 | |
Dinesh Dutt | d9ab53a | 2015-05-19 17:47:21 -0700 | [diff] [blame] | 408 | return (ret); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 409 | } |
| 410 | |
Pradosh Mohapatra | 60cc959 | 2015-11-09 20:21:41 -0500 | [diff] [blame] | 411 | static int |
| 412 | show_ip_bgp_nexthop_table (struct vty *vty, int detail) |
| 413 | { |
| 414 | struct bgp_node *rn; |
| 415 | struct bgp_nexthop_cache *bnc; |
| 416 | char buf[INET6_ADDRSTRLEN]; |
| 417 | struct nexthop *nexthop; |
| 418 | time_t tbuf; |
| 419 | afi_t afi; |
| 420 | |
| 421 | vty_out (vty, "Current BGP nexthop cache:%s", VTY_NEWLINE); |
| 422 | for (afi = AFI_IP ; afi < AFI_MAX ; afi++) |
| 423 | { |
Paul Jakma | 789dfc9 | 2016-09-06 11:20:27 +0100 | [diff] [blame] | 424 | if (!bgp_nexthop_cache_table[afi]) |
| 425 | continue; |
| 426 | |
Pradosh Mohapatra | 60cc959 | 2015-11-09 20:21:41 -0500 | [diff] [blame] | 427 | for (rn = bgp_table_top (bgp_nexthop_cache_table[afi]); rn; rn = bgp_route_next (rn)) |
| 428 | { |
| 429 | if ((bnc = rn->info) != NULL) |
| 430 | { |
| 431 | if (CHECK_FLAG(bnc->flags, BGP_NEXTHOP_VALID)) |
| 432 | { |
| 433 | vty_out (vty, " %s valid [IGP metric %d], #paths %d%s", |
| 434 | inet_ntop (rn->p.family, &rn->p.u.prefix, buf, sizeof (buf)), |
| 435 | bnc->metric, bnc->path_count, VTY_NEWLINE); |
| 436 | if (detail) |
| 437 | for (nexthop = bnc->nexthop ; nexthop; nexthop = nexthop->next) |
| 438 | switch (nexthop->type) |
| 439 | { |
| 440 | case NEXTHOP_TYPE_IPV6: |
| 441 | vty_out (vty, " gate %s%s", |
| 442 | inet_ntop (AF_INET6, &nexthop->gate.ipv6, |
| 443 | buf, INET6_ADDRSTRLEN), VTY_NEWLINE); |
| 444 | break; |
| 445 | case NEXTHOP_TYPE_IPV6_IFINDEX: |
| 446 | vty_out(vty, " gate %s, if %s%s", |
| 447 | inet_ntop(AF_INET6, &nexthop->gate.ipv6, buf, |
| 448 | INET6_ADDRSTRLEN), |
| 449 | ifindex2ifname(nexthop->ifindex), |
| 450 | VTY_NEWLINE); |
| 451 | break; |
| 452 | case NEXTHOP_TYPE_IPV4: |
| 453 | vty_out (vty, " gate %s%s", |
| 454 | inet_ntop (AF_INET, &nexthop->gate.ipv4, buf, |
| 455 | INET6_ADDRSTRLEN), VTY_NEWLINE); |
| 456 | break; |
| 457 | case NEXTHOP_TYPE_IFINDEX: |
| 458 | vty_out (vty, " if %s%s", |
| 459 | ifindex2ifname(nexthop->ifindex), VTY_NEWLINE); |
| 460 | break; |
| 461 | case NEXTHOP_TYPE_IPV4_IFINDEX: |
| 462 | vty_out (vty, " gate %s, if %s%s", |
| 463 | inet_ntop(AF_INET, &nexthop->gate.ipv4, buf, |
| 464 | INET6_ADDRSTRLEN), |
| 465 | ifindex2ifname(nexthop->ifindex), VTY_NEWLINE); |
| 466 | break; |
| 467 | default: |
| 468 | vty_out (vty, " invalid nexthop type %u%s", |
| 469 | nexthop->type, VTY_NEWLINE); |
| 470 | } |
| 471 | } |
| 472 | else |
| 473 | vty_out (vty, " %s invalid%s", |
| 474 | inet_ntop (AF_INET, &rn->p.u.prefix, buf, sizeof (buf)), VTY_NEWLINE); |
| 475 | #ifdef HAVE_CLOCK_MONOTONIC |
| 476 | tbuf = time(NULL) - (bgp_clock() - bnc->last_update); |
| 477 | vty_out (vty, " Last update: %s", ctime(&tbuf)); |
| 478 | #else |
| 479 | vty_out (vty, " Last update: %s", ctime(&bnc->uptime)); |
| 480 | #endif /* HAVE_CLOCK_MONOTONIC */ |
| 481 | vty_out(vty, "%s", VTY_NEWLINE); |
| 482 | } |
| 483 | } |
| 484 | } |
| 485 | return CMD_SUCCESS; |
| 486 | } |
| 487 | |
Pradosh Mohapatra | 60cc959 | 2015-11-09 20:21:41 -0500 | [diff] [blame] | 488 | DEFUN (show_ip_bgp_nexthop, |
| 489 | show_ip_bgp_nexthop_cmd, |
| 490 | "show ip bgp nexthop", |
| 491 | SHOW_STR |
| 492 | IP_STR |
| 493 | BGP_STR |
| 494 | "BGP nexthop table\n") |
| 495 | { |
| 496 | return show_ip_bgp_nexthop_table (vty, 0); |
| 497 | } |
| 498 | |
| 499 | DEFUN (show_ip_bgp_nexthop_detail, |
| 500 | show_ip_bgp_nexthop_detail_cmd, |
| 501 | "show ip bgp nexthop detail", |
| 502 | SHOW_STR |
| 503 | IP_STR |
| 504 | BGP_STR |
| 505 | "BGP nexthop table\n") |
| 506 | { |
| 507 | return show_ip_bgp_nexthop_table (vty, 1); |
| 508 | } |
| 509 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 510 | void |
Stephen Hemminger | 66e5cd8 | 2009-02-09 10:14:16 -0800 | [diff] [blame] | 511 | bgp_scan_init (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 512 | { |
Paul Jakma | 64e580a | 2006-02-21 01:09:01 +0000 | [diff] [blame] | 513 | cache1_table[AFI_IP] = bgp_table_init (AFI_IP, SAFI_UNICAST); |
paul | 5932020 | 2004-11-09 01:54:03 +0000 | [diff] [blame] | 514 | bgp_nexthop_cache_table[AFI_IP] = cache1_table[AFI_IP]; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 515 | |
Paul Jakma | 64e580a | 2006-02-21 01:09:01 +0000 | [diff] [blame] | 516 | bgp_connected_table[AFI_IP] = bgp_table_init (AFI_IP, SAFI_UNICAST); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 517 | |
Paul Jakma | 64e580a | 2006-02-21 01:09:01 +0000 | [diff] [blame] | 518 | cache1_table[AFI_IP6] = bgp_table_init (AFI_IP6, SAFI_UNICAST); |
paul | 5932020 | 2004-11-09 01:54:03 +0000 | [diff] [blame] | 519 | bgp_nexthop_cache_table[AFI_IP6] = cache1_table[AFI_IP6]; |
Paul Jakma | 64e580a | 2006-02-21 01:09:01 +0000 | [diff] [blame] | 520 | bgp_connected_table[AFI_IP6] = bgp_table_init (AFI_IP6, SAFI_UNICAST); |
Lou Berger | 810ab34 | 2016-09-05 12:18:15 -0400 | [diff] [blame^] | 521 | |
| 522 | cache1_table[AFI_ETHER] = bgp_table_init (AFI_ETHER, SAFI_UNICAST); |
| 523 | bgp_nexthop_cache_table[AFI_ETHER] = cache1_table[AFI_ETHER]; |
| 524 | bgp_connected_table[AFI_ETHER] = bgp_table_init (AFI_ETHER, SAFI_UNICAST); |
Dinesh Dutt | d9ab53a | 2015-05-19 17:47:21 -0700 | [diff] [blame] | 525 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 526 | |
Dinesh Dutt | d9ab53a | 2015-05-19 17:47:21 -0700 | [diff] [blame] | 527 | void |
| 528 | bgp_scan_vty_init() |
| 529 | { |
Pradosh Mohapatra | 60cc959 | 2015-11-09 20:21:41 -0500 | [diff] [blame] | 530 | install_element (VIEW_NODE, &show_ip_bgp_nexthop_cmd); |
| 531 | install_element (VIEW_NODE, &show_ip_bgp_nexthop_detail_cmd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 532 | } |
Chris Caputo | 228da42 | 2009-07-18 05:44:03 +0000 | [diff] [blame] | 533 | |
| 534 | void |
| 535 | bgp_scan_finish (void) |
| 536 | { |
Lou Berger | 056f376 | 2013-04-10 12:30:04 -0700 | [diff] [blame] | 537 | if (cache1_table[AFI_IP]) |
| 538 | bgp_table_unlock (cache1_table[AFI_IP]); |
Chris Caputo | 228da42 | 2009-07-18 05:44:03 +0000 | [diff] [blame] | 539 | cache1_table[AFI_IP] = NULL; |
| 540 | |
Lou Berger | 056f376 | 2013-04-10 12:30:04 -0700 | [diff] [blame] | 541 | if (bgp_connected_table[AFI_IP]) |
| 542 | bgp_table_unlock (bgp_connected_table[AFI_IP]); |
Chris Caputo | 228da42 | 2009-07-18 05:44:03 +0000 | [diff] [blame] | 543 | bgp_connected_table[AFI_IP] = NULL; |
| 544 | |
Lou Berger | 056f376 | 2013-04-10 12:30:04 -0700 | [diff] [blame] | 545 | if (cache1_table[AFI_IP6]) |
| 546 | bgp_table_unlock (cache1_table[AFI_IP6]); |
Chris Caputo | 228da42 | 2009-07-18 05:44:03 +0000 | [diff] [blame] | 547 | cache1_table[AFI_IP6] = NULL; |
| 548 | |
Lou Berger | 056f376 | 2013-04-10 12:30:04 -0700 | [diff] [blame] | 549 | if (bgp_connected_table[AFI_IP6]) |
| 550 | bgp_table_unlock (bgp_connected_table[AFI_IP6]); |
Chris Caputo | 228da42 | 2009-07-18 05:44:03 +0000 | [diff] [blame] | 551 | bgp_connected_table[AFI_IP6] = NULL; |
Lou Berger | 810ab34 | 2016-09-05 12:18:15 -0400 | [diff] [blame^] | 552 | |
| 553 | if (cache1_table[AFI_ETHER]) |
| 554 | bgp_table_unlock (cache1_table[AFI_ETHER]); |
| 555 | cache1_table[AFI_ETHER] = NULL; |
| 556 | |
| 557 | if (bgp_connected_table[AFI_ETHER]) |
| 558 | bgp_table_unlock (bgp_connected_table[AFI_ETHER]); |
| 559 | bgp_connected_table[AFI_ETHER] = NULL; |
| 560 | |
Chris Caputo | 228da42 | 2009-07-18 05:44:03 +0000 | [diff] [blame] | 561 | } |
Lou Berger | 82dd707 | 2016-01-12 13:41:57 -0500 | [diff] [blame] | 562 | |
| 563 | void |
| 564 | bgp_scan_destroy (void) |
| 565 | { |
Lou Berger | 82dd707 | 2016-01-12 13:41:57 -0500 | [diff] [blame] | 566 | bgp_scan_finish(); |
Lou Berger | 82dd707 | 2016-01-12 13:41:57 -0500 | [diff] [blame] | 567 | } |