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