paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1 | /* Routing Information Base. |
| 2 | * Copyright (C) 1997, 98, 99, 2001 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 | |
| 22 | #include <zebra.h> |
| 23 | |
| 24 | #include "prefix.h" |
| 25 | #include "table.h" |
| 26 | #include "memory.h" |
| 27 | #include "str.h" |
| 28 | #include "command.h" |
| 29 | #include "if.h" |
| 30 | #include "log.h" |
| 31 | #include "sockunion.h" |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 32 | #include "linklist.h" |
| 33 | #include "thread.h" |
| 34 | #include "workqueue.h" |
Paul Jakma | 7514fb7 | 2007-05-02 16:05:35 +0000 | [diff] [blame] | 35 | #include "prefix.h" |
| 36 | #include "routemap.h" |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 37 | |
| 38 | #include "zebra/rib.h" |
| 39 | #include "zebra/rt.h" |
| 40 | #include "zebra/zserv.h" |
| 41 | #include "zebra/redistribute.h" |
| 42 | #include "zebra/debug.h" |
Avneesh Sachdev | 5adc252 | 2012-11-13 22:48:59 +0000 | [diff] [blame] | 43 | #include "zebra/zebra_fpm.h" |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 44 | |
| 45 | /* Default rtm_table for all clients */ |
paul | b21b19c | 2003-06-15 01:28:29 +0000 | [diff] [blame] | 46 | extern struct zebra_t zebrad; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 47 | |
Paul Jakma | 457eb9a | 2006-07-27 19:59:58 +0000 | [diff] [blame] | 48 | /* Hold time for RIB process, should be very minimal. |
| 49 | * it is useful to able to set it otherwise for testing, hence exported |
| 50 | * as global here for test-rig code. |
| 51 | */ |
| 52 | int rib_process_hold_time = 10; |
| 53 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 54 | /* Each route type's string and default distance value. */ |
Stephen Hemminger | d145bc0 | 2008-08-17 17:41:37 +0100 | [diff] [blame] | 55 | static const struct |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 56 | { |
| 57 | int key; |
| 58 | int distance; |
Paul Jakma | 5734509 | 2011-12-25 17:52:09 +0100 | [diff] [blame] | 59 | } route_info[ZEBRA_ROUTE_MAX] = |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 60 | { |
Paul Jakma | 5734509 | 2011-12-25 17:52:09 +0100 | [diff] [blame] | 61 | [ZEBRA_ROUTE_SYSTEM] = {ZEBRA_ROUTE_SYSTEM, 0}, |
| 62 | [ZEBRA_ROUTE_KERNEL] = {ZEBRA_ROUTE_KERNEL, 0}, |
| 63 | [ZEBRA_ROUTE_CONNECT] = {ZEBRA_ROUTE_CONNECT, 0}, |
| 64 | [ZEBRA_ROUTE_STATIC] = {ZEBRA_ROUTE_STATIC, 1}, |
| 65 | [ZEBRA_ROUTE_RIP] = {ZEBRA_ROUTE_RIP, 120}, |
| 66 | [ZEBRA_ROUTE_RIPNG] = {ZEBRA_ROUTE_RIPNG, 120}, |
| 67 | [ZEBRA_ROUTE_OSPF] = {ZEBRA_ROUTE_OSPF, 110}, |
| 68 | [ZEBRA_ROUTE_OSPF6] = {ZEBRA_ROUTE_OSPF6, 110}, |
| 69 | [ZEBRA_ROUTE_ISIS] = {ZEBRA_ROUTE_ISIS, 115}, |
| 70 | [ZEBRA_ROUTE_BGP] = {ZEBRA_ROUTE_BGP, 20 /* IBGP is 200. */}, |
| 71 | [ZEBRA_ROUTE_BABEL] = {ZEBRA_ROUTE_BABEL, 95}, |
David Lamparter | 7052f22 | 2009-08-27 00:28:28 +0200 | [diff] [blame] | 72 | /* no entry/default: 150 */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 73 | }; |
| 74 | |
| 75 | /* Vector for routing table. */ |
Stephen Hemminger | d145bc0 | 2008-08-17 17:41:37 +0100 | [diff] [blame] | 76 | static vector vrf_vector; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 77 | |
Avneesh Sachdev | 1b5ed1b | 2012-11-13 22:48:54 +0000 | [diff] [blame] | 78 | /* |
| 79 | * vrf_table_create |
| 80 | */ |
| 81 | static void |
| 82 | vrf_table_create (struct vrf *vrf, afi_t afi, safi_t safi) |
| 83 | { |
| 84 | rib_table_info_t *info; |
| 85 | struct route_table *table; |
| 86 | |
| 87 | assert (!vrf->table[afi][safi]); |
| 88 | |
| 89 | table = route_table_init (); |
| 90 | vrf->table[afi][safi] = table; |
| 91 | |
| 92 | info = XCALLOC (MTYPE_RIB_TABLE_INFO, sizeof (*info)); |
| 93 | info->vrf = vrf; |
| 94 | info->afi = afi; |
| 95 | info->safi = safi; |
| 96 | table->info = info; |
| 97 | } |
| 98 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 99 | /* Allocate new VRF. */ |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 100 | static struct vrf * |
hasso | fce954f | 2004-10-07 20:29:24 +0000 | [diff] [blame] | 101 | vrf_alloc (const char *name) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 102 | { |
| 103 | struct vrf *vrf; |
| 104 | |
| 105 | vrf = XCALLOC (MTYPE_VRF, sizeof (struct vrf)); |
| 106 | |
| 107 | /* Put name. */ |
| 108 | if (name) |
| 109 | vrf->name = XSTRDUP (MTYPE_VRF_NAME, name); |
| 110 | |
| 111 | /* Allocate routing table and static table. */ |
Avneesh Sachdev | 1b5ed1b | 2012-11-13 22:48:54 +0000 | [diff] [blame] | 112 | vrf_table_create (vrf, AFI_IP, SAFI_UNICAST); |
| 113 | vrf_table_create (vrf, AFI_IP6, SAFI_UNICAST); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 114 | vrf->stable[AFI_IP][SAFI_UNICAST] = route_table_init (); |
| 115 | vrf->stable[AFI_IP6][SAFI_UNICAST] = route_table_init (); |
Avneesh Sachdev | 1b5ed1b | 2012-11-13 22:48:54 +0000 | [diff] [blame] | 116 | vrf_table_create (vrf, AFI_IP, SAFI_MULTICAST); |
| 117 | vrf_table_create (vrf, AFI_IP6, SAFI_MULTICAST); |
G.Balaji | cddf391 | 2011-11-26 21:59:32 +0400 | [diff] [blame] | 118 | vrf->stable[AFI_IP][SAFI_MULTICAST] = route_table_init (); |
| 119 | vrf->stable[AFI_IP6][SAFI_MULTICAST] = route_table_init (); |
| 120 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 121 | |
| 122 | return vrf; |
| 123 | } |
| 124 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 125 | /* Lookup VRF by identifier. */ |
| 126 | struct vrf * |
| 127 | vrf_lookup (u_int32_t id) |
| 128 | { |
| 129 | return vector_lookup (vrf_vector, id); |
| 130 | } |
| 131 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 132 | /* Initialize VRF. */ |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 133 | static void |
| 134 | vrf_init (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 135 | { |
| 136 | struct vrf *default_table; |
| 137 | |
| 138 | /* Allocate VRF vector. */ |
| 139 | vrf_vector = vector_init (1); |
| 140 | |
| 141 | /* Allocate default main table. */ |
| 142 | default_table = vrf_alloc ("Default-IP-Routing-Table"); |
| 143 | |
| 144 | /* Default table index must be 0. */ |
| 145 | vector_set_index (vrf_vector, 0, default_table); |
| 146 | } |
| 147 | |
| 148 | /* Lookup route table. */ |
| 149 | struct route_table * |
| 150 | vrf_table (afi_t afi, safi_t safi, u_int32_t id) |
| 151 | { |
| 152 | struct vrf *vrf; |
| 153 | |
| 154 | vrf = vrf_lookup (id); |
| 155 | if (! vrf) |
| 156 | return NULL; |
| 157 | |
Leonid Rosenboim | 9499bf2 | 2012-12-06 20:17:41 +0000 | [diff] [blame] | 158 | if( afi >= AFI_MAX || safi >= SAFI_MAX ) |
| 159 | return NULL; |
| 160 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 161 | return vrf->table[afi][safi]; |
| 162 | } |
| 163 | |
| 164 | /* Lookup static route table. */ |
| 165 | struct route_table * |
| 166 | vrf_static_table (afi_t afi, safi_t safi, u_int32_t id) |
| 167 | { |
| 168 | struct vrf *vrf; |
| 169 | |
| 170 | vrf = vrf_lookup (id); |
| 171 | if (! vrf) |
| 172 | return NULL; |
| 173 | |
Leonid Rosenboim | 9499bf2 | 2012-12-06 20:17:41 +0000 | [diff] [blame] | 174 | if( afi >= AFI_MAX || safi >= SAFI_MAX ) |
| 175 | return NULL; |
| 176 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 177 | return vrf->stable[afi][safi]; |
| 178 | } |
| 179 | |
Avneesh Sachdev | 78deec4 | 2012-11-13 22:48:56 +0000 | [diff] [blame] | 180 | /* |
| 181 | * nexthop_type_to_str |
| 182 | */ |
| 183 | const char * |
| 184 | nexthop_type_to_str (enum nexthop_types_t nh_type) |
| 185 | { |
| 186 | static const char *desc[] = { |
| 187 | "none", |
| 188 | "Directly connected", |
| 189 | "Interface route", |
| 190 | "IPv4 nexthop", |
| 191 | "IPv4 nexthop with ifindex", |
| 192 | "IPv4 nexthop with ifname", |
| 193 | "IPv6 nexthop", |
| 194 | "IPv6 nexthop with ifindex", |
| 195 | "IPv6 nexthop with ifname", |
| 196 | "Null0 nexthop", |
| 197 | }; |
| 198 | |
| 199 | if (nh_type >= ZEBRA_NUM_OF (desc)) |
| 200 | return "<Invalid nh type>"; |
| 201 | |
| 202 | return desc[nh_type]; |
| 203 | } |
| 204 | |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 205 | /* Add nexthop to the end of a nexthop list. */ |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 206 | static void |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 207 | _nexthop_add (struct nexthop **target, struct nexthop *nexthop) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 208 | { |
| 209 | struct nexthop *last; |
| 210 | |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 211 | for (last = *target; last && last->next; last = last->next) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 212 | ; |
| 213 | if (last) |
| 214 | last->next = nexthop; |
| 215 | else |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 216 | *target = nexthop; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 217 | nexthop->prev = last; |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 218 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 219 | |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 220 | /* Add nexthop to the end of a rib node's nexthop list */ |
| 221 | static void |
| 222 | nexthop_add (struct rib *rib, struct nexthop *nexthop) |
| 223 | { |
| 224 | _nexthop_add(&rib->nexthop, nexthop); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 225 | rib->nexthop_num++; |
| 226 | } |
| 227 | |
| 228 | /* Delete specified nexthop from the list. */ |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 229 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 230 | nexthop_delete (struct rib *rib, struct nexthop *nexthop) |
| 231 | { |
| 232 | if (nexthop->next) |
| 233 | nexthop->next->prev = nexthop->prev; |
| 234 | if (nexthop->prev) |
| 235 | nexthop->prev->next = nexthop->next; |
| 236 | else |
| 237 | rib->nexthop = nexthop->next; |
| 238 | rib->nexthop_num--; |
| 239 | } |
| 240 | |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 241 | static void nexthops_free(struct nexthop *nexthop); |
| 242 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 243 | /* Free nexthop. */ |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 244 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 245 | nexthop_free (struct nexthop *nexthop) |
| 246 | { |
paul | a4b7076 | 2003-05-16 17:19:48 +0000 | [diff] [blame] | 247 | if (nexthop->ifname) |
| 248 | XFREE (0, nexthop->ifname); |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 249 | if (nexthop->resolved) |
| 250 | nexthops_free(nexthop->resolved); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 251 | XFREE (MTYPE_NEXTHOP, nexthop); |
| 252 | } |
| 253 | |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 254 | /* Frees a list of nexthops */ |
| 255 | static void |
| 256 | nexthops_free (struct nexthop *nexthop) |
| 257 | { |
| 258 | struct nexthop *nh, *next; |
| 259 | |
| 260 | for (nh = nexthop; nh; nh = next) |
| 261 | { |
| 262 | next = nh->next; |
| 263 | nexthop_free (nh); |
| 264 | } |
| 265 | } |
| 266 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 267 | struct nexthop * |
| 268 | nexthop_ifindex_add (struct rib *rib, unsigned int ifindex) |
| 269 | { |
| 270 | struct nexthop *nexthop; |
| 271 | |
Stephen Hemminger | 393deb9 | 2008-08-18 14:13:29 -0700 | [diff] [blame] | 272 | nexthop = XCALLOC (MTYPE_NEXTHOP, sizeof (struct nexthop)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 273 | nexthop->type = NEXTHOP_TYPE_IFINDEX; |
| 274 | nexthop->ifindex = ifindex; |
| 275 | |
| 276 | nexthop_add (rib, nexthop); |
| 277 | |
| 278 | return nexthop; |
| 279 | } |
| 280 | |
| 281 | struct nexthop * |
| 282 | nexthop_ifname_add (struct rib *rib, char *ifname) |
| 283 | { |
| 284 | struct nexthop *nexthop; |
| 285 | |
Stephen Hemminger | 393deb9 | 2008-08-18 14:13:29 -0700 | [diff] [blame] | 286 | nexthop = XCALLOC (MTYPE_NEXTHOP, sizeof (struct nexthop)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 287 | nexthop->type = NEXTHOP_TYPE_IFNAME; |
paul | a4b7076 | 2003-05-16 17:19:48 +0000 | [diff] [blame] | 288 | nexthop->ifname = XSTRDUP (0, ifname); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 289 | |
| 290 | nexthop_add (rib, nexthop); |
| 291 | |
| 292 | return nexthop; |
| 293 | } |
| 294 | |
| 295 | struct nexthop * |
Paul Jakma | 7514fb7 | 2007-05-02 16:05:35 +0000 | [diff] [blame] | 296 | nexthop_ipv4_add (struct rib *rib, struct in_addr *ipv4, struct in_addr *src) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 297 | { |
| 298 | struct nexthop *nexthop; |
| 299 | |
Stephen Hemminger | 393deb9 | 2008-08-18 14:13:29 -0700 | [diff] [blame] | 300 | nexthop = XCALLOC (MTYPE_NEXTHOP, sizeof (struct nexthop)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 301 | nexthop->type = NEXTHOP_TYPE_IPV4; |
| 302 | nexthop->gate.ipv4 = *ipv4; |
Paul Jakma | 7514fb7 | 2007-05-02 16:05:35 +0000 | [diff] [blame] | 303 | if (src) |
| 304 | nexthop->src.ipv4 = *src; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 305 | |
| 306 | nexthop_add (rib, nexthop); |
| 307 | |
| 308 | return nexthop; |
| 309 | } |
| 310 | |
Josh Bailey | 26e2ae3 | 2012-03-22 01:09:21 -0700 | [diff] [blame] | 311 | struct nexthop * |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 312 | nexthop_ipv4_ifindex_add (struct rib *rib, struct in_addr *ipv4, |
Paul Jakma | 7514fb7 | 2007-05-02 16:05:35 +0000 | [diff] [blame] | 313 | struct in_addr *src, unsigned int ifindex) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 314 | { |
| 315 | struct nexthop *nexthop; |
| 316 | |
Stephen Hemminger | 393deb9 | 2008-08-18 14:13:29 -0700 | [diff] [blame] | 317 | nexthop = XCALLOC (MTYPE_NEXTHOP, sizeof (struct nexthop)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 318 | nexthop->type = NEXTHOP_TYPE_IPV4_IFINDEX; |
| 319 | nexthop->gate.ipv4 = *ipv4; |
Paul Jakma | 7514fb7 | 2007-05-02 16:05:35 +0000 | [diff] [blame] | 320 | if (src) |
| 321 | nexthop->src.ipv4 = *src; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 322 | nexthop->ifindex = ifindex; |
| 323 | |
| 324 | nexthop_add (rib, nexthop); |
| 325 | |
| 326 | return nexthop; |
| 327 | } |
| 328 | |
| 329 | #ifdef HAVE_IPV6 |
| 330 | struct nexthop * |
| 331 | nexthop_ipv6_add (struct rib *rib, struct in6_addr *ipv6) |
| 332 | { |
| 333 | struct nexthop *nexthop; |
| 334 | |
Stephen Hemminger | 393deb9 | 2008-08-18 14:13:29 -0700 | [diff] [blame] | 335 | nexthop = XCALLOC (MTYPE_NEXTHOP, sizeof (struct nexthop)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 336 | nexthop->type = NEXTHOP_TYPE_IPV6; |
| 337 | nexthop->gate.ipv6 = *ipv6; |
| 338 | |
| 339 | nexthop_add (rib, nexthop); |
| 340 | |
| 341 | return nexthop; |
| 342 | } |
| 343 | |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 344 | static struct nexthop * |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 345 | nexthop_ipv6_ifname_add (struct rib *rib, struct in6_addr *ipv6, |
| 346 | char *ifname) |
| 347 | { |
| 348 | struct nexthop *nexthop; |
| 349 | |
Stephen Hemminger | 393deb9 | 2008-08-18 14:13:29 -0700 | [diff] [blame] | 350 | nexthop = XCALLOC (MTYPE_NEXTHOP, sizeof (struct nexthop)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 351 | nexthop->type = NEXTHOP_TYPE_IPV6_IFNAME; |
| 352 | nexthop->gate.ipv6 = *ipv6; |
| 353 | nexthop->ifname = XSTRDUP (0, ifname); |
| 354 | |
| 355 | nexthop_add (rib, nexthop); |
| 356 | |
| 357 | return nexthop; |
| 358 | } |
| 359 | |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 360 | static struct nexthop * |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 361 | nexthop_ipv6_ifindex_add (struct rib *rib, struct in6_addr *ipv6, |
| 362 | unsigned int ifindex) |
| 363 | { |
| 364 | struct nexthop *nexthop; |
| 365 | |
Stephen Hemminger | 393deb9 | 2008-08-18 14:13:29 -0700 | [diff] [blame] | 366 | nexthop = XCALLOC (MTYPE_NEXTHOP, sizeof (struct nexthop)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 367 | nexthop->type = NEXTHOP_TYPE_IPV6_IFINDEX; |
| 368 | nexthop->gate.ipv6 = *ipv6; |
| 369 | nexthop->ifindex = ifindex; |
| 370 | |
| 371 | nexthop_add (rib, nexthop); |
| 372 | |
| 373 | return nexthop; |
| 374 | } |
| 375 | #endif /* HAVE_IPV6 */ |
| 376 | |
paul | 595db7f | 2003-05-25 21:35:06 +0000 | [diff] [blame] | 377 | struct nexthop * |
| 378 | nexthop_blackhole_add (struct rib *rib) |
| 379 | { |
| 380 | struct nexthop *nexthop; |
| 381 | |
Stephen Hemminger | 393deb9 | 2008-08-18 14:13:29 -0700 | [diff] [blame] | 382 | nexthop = XCALLOC (MTYPE_NEXTHOP, sizeof (struct nexthop)); |
paul | 595db7f | 2003-05-25 21:35:06 +0000 | [diff] [blame] | 383 | nexthop->type = NEXTHOP_TYPE_BLACKHOLE; |
| 384 | SET_FLAG (rib->flags, ZEBRA_FLAG_BLACKHOLE); |
| 385 | |
| 386 | nexthop_add (rib, nexthop); |
| 387 | |
| 388 | return nexthop; |
| 389 | } |
| 390 | |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 391 | /* This method checks whether a recursive nexthop has at |
| 392 | * least one resolved nexthop in the fib. |
| 393 | */ |
| 394 | int |
| 395 | nexthop_has_fib_child(struct nexthop *nexthop) |
| 396 | { |
| 397 | struct nexthop *nh; |
| 398 | |
| 399 | if (! CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE)) |
| 400 | return 0; |
| 401 | |
| 402 | for (nh = nexthop->resolved; nh; nh = nh->next) |
| 403 | if (CHECK_FLAG (nh->flags, NEXTHOP_FLAG_FIB)) |
| 404 | return 1; |
| 405 | |
| 406 | return 0; |
| 407 | } |
| 408 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 409 | /* If force flag is not set, do not modify falgs at all for uninstall |
| 410 | the route from FIB. */ |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 411 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 412 | nexthop_active_ipv4 (struct rib *rib, struct nexthop *nexthop, int set, |
| 413 | struct route_node *top) |
| 414 | { |
| 415 | struct prefix_ipv4 p; |
| 416 | struct route_table *table; |
| 417 | struct route_node *rn; |
| 418 | struct rib *match; |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 419 | int resolved; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 420 | struct nexthop *newhop; |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 421 | struct nexthop *resolved_hop; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 422 | |
| 423 | if (nexthop->type == NEXTHOP_TYPE_IPV4) |
| 424 | nexthop->ifindex = 0; |
| 425 | |
| 426 | if (set) |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 427 | { |
| 428 | UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE); |
| 429 | nexthops_free(nexthop->resolved); |
| 430 | nexthop->resolved = NULL; |
| 431 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 432 | |
| 433 | /* Make lookup prefix. */ |
| 434 | memset (&p, 0, sizeof (struct prefix_ipv4)); |
| 435 | p.family = AF_INET; |
| 436 | p.prefixlen = IPV4_MAX_PREFIXLEN; |
| 437 | p.prefix = nexthop->gate.ipv4; |
| 438 | |
| 439 | /* Lookup table. */ |
| 440 | table = vrf_table (AFI_IP, SAFI_UNICAST, 0); |
| 441 | if (! table) |
| 442 | return 0; |
| 443 | |
| 444 | rn = route_node_match (table, (struct prefix *) &p); |
| 445 | while (rn) |
| 446 | { |
| 447 | route_unlock_node (rn); |
| 448 | |
David Ward | a50c107 | 2009-12-03 15:34:39 +0300 | [diff] [blame] | 449 | /* If lookup self prefix return immediately. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 450 | if (rn == top) |
| 451 | return 0; |
| 452 | |
| 453 | /* Pick up selected route. */ |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 454 | RNODE_FOREACH_RIB (rn, match) |
Stephen Hemminger | 16814f9 | 2008-08-17 17:39:31 +0100 | [diff] [blame] | 455 | { |
| 456 | if (CHECK_FLAG (match->status, RIB_ENTRY_REMOVED)) |
| 457 | continue; |
| 458 | if (CHECK_FLAG (match->flags, ZEBRA_FLAG_SELECTED)) |
| 459 | break; |
| 460 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 461 | |
| 462 | /* If there is no selected route or matched route is EGP, go up |
| 463 | tree. */ |
| 464 | if (! match |
| 465 | || match->type == ZEBRA_ROUTE_BGP) |
| 466 | { |
| 467 | do { |
| 468 | rn = rn->parent; |
| 469 | } while (rn && rn->info == NULL); |
| 470 | if (rn) |
| 471 | route_lock_node (rn); |
| 472 | } |
| 473 | else |
| 474 | { |
Christian Franke | 48a53dc | 2013-07-05 15:35:38 +0000 | [diff] [blame] | 475 | /* If the longest prefix match for the nexthop yields |
| 476 | * a blackhole, mark it as inactive. */ |
| 477 | if (CHECK_FLAG (match->flags, ZEBRA_FLAG_BLACKHOLE) |
| 478 | || CHECK_FLAG (match->flags, ZEBRA_FLAG_REJECT)) |
| 479 | return 0; |
| 480 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 481 | if (match->type == ZEBRA_ROUTE_CONNECT) |
| 482 | { |
| 483 | /* Directly point connected route. */ |
| 484 | newhop = match->nexthop; |
| 485 | if (newhop && nexthop->type == NEXTHOP_TYPE_IPV4) |
| 486 | nexthop->ifindex = newhop->ifindex; |
| 487 | |
| 488 | return 1; |
| 489 | } |
| 490 | else if (CHECK_FLAG (rib->flags, ZEBRA_FLAG_INTERNAL)) |
| 491 | { |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 492 | resolved = 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 493 | for (newhop = match->nexthop; newhop; newhop = newhop->next) |
| 494 | if (CHECK_FLAG (newhop->flags, NEXTHOP_FLAG_FIB) |
| 495 | && ! CHECK_FLAG (newhop->flags, NEXTHOP_FLAG_RECURSIVE)) |
| 496 | { |
| 497 | if (set) |
| 498 | { |
| 499 | SET_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE); |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 500 | |
| 501 | resolved_hop = XCALLOC(MTYPE_NEXTHOP, sizeof (struct nexthop)); |
| 502 | SET_FLAG (resolved_hop->flags, NEXTHOP_FLAG_ACTIVE); |
| 503 | |
| 504 | resolved_hop->type = newhop->type; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 505 | if (newhop->type == NEXTHOP_TYPE_IPV4 || |
| 506 | newhop->type == NEXTHOP_TYPE_IPV4_IFINDEX) |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 507 | resolved_hop->gate.ipv4 = newhop->gate.ipv4; |
| 508 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 509 | if (newhop->type == NEXTHOP_TYPE_IFINDEX |
| 510 | || newhop->type == NEXTHOP_TYPE_IFNAME |
| 511 | || newhop->type == NEXTHOP_TYPE_IPV4_IFINDEX) |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 512 | resolved_hop->ifindex = newhop->ifindex; |
| 513 | |
| 514 | _nexthop_add(&nexthop->resolved, resolved_hop); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 515 | } |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 516 | resolved = 1; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 517 | } |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 518 | return resolved; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 519 | } |
| 520 | else |
| 521 | { |
| 522 | return 0; |
| 523 | } |
| 524 | } |
| 525 | } |
| 526 | return 0; |
| 527 | } |
| 528 | |
| 529 | #ifdef HAVE_IPV6 |
| 530 | /* If force flag is not set, do not modify falgs at all for uninstall |
| 531 | the route from FIB. */ |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 532 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 533 | nexthop_active_ipv6 (struct rib *rib, struct nexthop *nexthop, int set, |
| 534 | struct route_node *top) |
| 535 | { |
| 536 | struct prefix_ipv6 p; |
| 537 | struct route_table *table; |
| 538 | struct route_node *rn; |
| 539 | struct rib *match; |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 540 | int resolved; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 541 | struct nexthop *newhop; |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 542 | struct nexthop *resolved_hop; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 543 | |
| 544 | if (nexthop->type == NEXTHOP_TYPE_IPV6) |
| 545 | nexthop->ifindex = 0; |
| 546 | |
| 547 | if (set) |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 548 | { |
| 549 | UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE); |
| 550 | nexthops_free(nexthop->resolved); |
| 551 | nexthop->resolved = NULL; |
| 552 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 553 | |
| 554 | /* Make lookup prefix. */ |
| 555 | memset (&p, 0, sizeof (struct prefix_ipv6)); |
| 556 | p.family = AF_INET6; |
| 557 | p.prefixlen = IPV6_MAX_PREFIXLEN; |
| 558 | p.prefix = nexthop->gate.ipv6; |
| 559 | |
| 560 | /* Lookup table. */ |
| 561 | table = vrf_table (AFI_IP6, SAFI_UNICAST, 0); |
| 562 | if (! table) |
| 563 | return 0; |
| 564 | |
| 565 | rn = route_node_match (table, (struct prefix *) &p); |
| 566 | while (rn) |
| 567 | { |
| 568 | route_unlock_node (rn); |
| 569 | |
David Ward | a50c107 | 2009-12-03 15:34:39 +0300 | [diff] [blame] | 570 | /* If lookup self prefix return immediately. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 571 | if (rn == top) |
| 572 | return 0; |
| 573 | |
| 574 | /* Pick up selected route. */ |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 575 | RNODE_FOREACH_RIB (rn, match) |
Stephen Hemminger | 16814f9 | 2008-08-17 17:39:31 +0100 | [diff] [blame] | 576 | { |
| 577 | if (CHECK_FLAG (match->status, RIB_ENTRY_REMOVED)) |
| 578 | continue; |
| 579 | if (CHECK_FLAG (match->flags, ZEBRA_FLAG_SELECTED)) |
| 580 | break; |
| 581 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 582 | |
| 583 | /* If there is no selected route or matched route is EGP, go up |
| 584 | tree. */ |
| 585 | if (! match |
| 586 | || match->type == ZEBRA_ROUTE_BGP) |
| 587 | { |
| 588 | do { |
| 589 | rn = rn->parent; |
| 590 | } while (rn && rn->info == NULL); |
| 591 | if (rn) |
| 592 | route_lock_node (rn); |
| 593 | } |
| 594 | else |
| 595 | { |
Christian Franke | 48a53dc | 2013-07-05 15:35:38 +0000 | [diff] [blame] | 596 | /* If the longest prefix match for the nexthop yields |
| 597 | * a blackhole, mark it as inactive. */ |
| 598 | if (CHECK_FLAG (match->flags, ZEBRA_FLAG_BLACKHOLE) |
| 599 | || CHECK_FLAG (match->flags, ZEBRA_FLAG_REJECT)) |
| 600 | return 0; |
| 601 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 602 | if (match->type == ZEBRA_ROUTE_CONNECT) |
| 603 | { |
| 604 | /* Directly point connected route. */ |
| 605 | newhop = match->nexthop; |
| 606 | |
| 607 | if (newhop && nexthop->type == NEXTHOP_TYPE_IPV6) |
| 608 | nexthop->ifindex = newhop->ifindex; |
| 609 | |
| 610 | return 1; |
| 611 | } |
| 612 | else if (CHECK_FLAG (rib->flags, ZEBRA_FLAG_INTERNAL)) |
| 613 | { |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 614 | resolved = 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 615 | for (newhop = match->nexthop; newhop; newhop = newhop->next) |
| 616 | if (CHECK_FLAG (newhop->flags, NEXTHOP_FLAG_FIB) |
| 617 | && ! CHECK_FLAG (newhop->flags, NEXTHOP_FLAG_RECURSIVE)) |
| 618 | { |
| 619 | if (set) |
| 620 | { |
| 621 | SET_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE); |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 622 | |
| 623 | resolved_hop = XCALLOC(MTYPE_NEXTHOP, sizeof (struct nexthop)); |
| 624 | SET_FLAG (resolved_hop->flags, NEXTHOP_FLAG_ACTIVE); |
| 625 | |
| 626 | resolved_hop->type = newhop->type; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 627 | if (newhop->type == NEXTHOP_TYPE_IPV6 |
| 628 | || newhop->type == NEXTHOP_TYPE_IPV6_IFINDEX |
| 629 | || newhop->type == NEXTHOP_TYPE_IPV6_IFNAME) |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 630 | resolved_hop->gate.ipv6 = newhop->gate.ipv6; |
| 631 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 632 | if (newhop->type == NEXTHOP_TYPE_IFINDEX |
| 633 | || newhop->type == NEXTHOP_TYPE_IFNAME |
| 634 | || newhop->type == NEXTHOP_TYPE_IPV6_IFINDEX |
| 635 | || newhop->type == NEXTHOP_TYPE_IPV6_IFNAME) |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 636 | resolved_hop->ifindex = newhop->ifindex; |
| 637 | |
| 638 | _nexthop_add(&nexthop->resolved, resolved_hop); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 639 | } |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 640 | resolved = 1; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 641 | } |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 642 | return resolved; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 643 | } |
| 644 | else |
| 645 | { |
| 646 | return 0; |
| 647 | } |
| 648 | } |
| 649 | } |
| 650 | return 0; |
| 651 | } |
| 652 | #endif /* HAVE_IPV6 */ |
| 653 | |
| 654 | struct rib * |
| 655 | rib_match_ipv4 (struct in_addr addr) |
| 656 | { |
| 657 | struct prefix_ipv4 p; |
| 658 | struct route_table *table; |
| 659 | struct route_node *rn; |
| 660 | struct rib *match; |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 661 | struct nexthop *newhop, *tnewhop; |
| 662 | int recursing; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 663 | |
| 664 | /* Lookup table. */ |
| 665 | table = vrf_table (AFI_IP, SAFI_UNICAST, 0); |
| 666 | if (! table) |
| 667 | return 0; |
| 668 | |
| 669 | memset (&p, 0, sizeof (struct prefix_ipv4)); |
| 670 | p.family = AF_INET; |
| 671 | p.prefixlen = IPV4_MAX_PREFIXLEN; |
| 672 | p.prefix = addr; |
| 673 | |
| 674 | rn = route_node_match (table, (struct prefix *) &p); |
| 675 | |
| 676 | while (rn) |
| 677 | { |
| 678 | route_unlock_node (rn); |
| 679 | |
| 680 | /* Pick up selected route. */ |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 681 | RNODE_FOREACH_RIB (rn, match) |
Stephen Hemminger | 16814f9 | 2008-08-17 17:39:31 +0100 | [diff] [blame] | 682 | { |
| 683 | if (CHECK_FLAG (match->status, RIB_ENTRY_REMOVED)) |
| 684 | continue; |
| 685 | if (CHECK_FLAG (match->flags, ZEBRA_FLAG_SELECTED)) |
| 686 | break; |
| 687 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 688 | |
| 689 | /* If there is no selected route or matched route is EGP, go up |
| 690 | tree. */ |
| 691 | if (! match |
| 692 | || match->type == ZEBRA_ROUTE_BGP) |
| 693 | { |
| 694 | do { |
| 695 | rn = rn->parent; |
| 696 | } while (rn && rn->info == NULL); |
| 697 | if (rn) |
| 698 | route_lock_node (rn); |
| 699 | } |
| 700 | else |
| 701 | { |
| 702 | if (match->type == ZEBRA_ROUTE_CONNECT) |
| 703 | /* Directly point connected route. */ |
| 704 | return match; |
| 705 | else |
| 706 | { |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 707 | for (ALL_NEXTHOPS_RO(match->nexthop, newhop, tnewhop, recursing)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 708 | if (CHECK_FLAG (newhop->flags, NEXTHOP_FLAG_FIB)) |
| 709 | return match; |
| 710 | return NULL; |
| 711 | } |
| 712 | } |
| 713 | } |
| 714 | return NULL; |
| 715 | } |
| 716 | |
| 717 | struct rib * |
| 718 | rib_lookup_ipv4 (struct prefix_ipv4 *p) |
| 719 | { |
| 720 | struct route_table *table; |
| 721 | struct route_node *rn; |
| 722 | struct rib *match; |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 723 | struct nexthop *nexthop, *tnexthop; |
| 724 | int recursing; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 725 | |
| 726 | /* Lookup table. */ |
| 727 | table = vrf_table (AFI_IP, SAFI_UNICAST, 0); |
| 728 | if (! table) |
| 729 | return 0; |
| 730 | |
| 731 | rn = route_node_lookup (table, (struct prefix *) p); |
| 732 | |
| 733 | /* No route for this prefix. */ |
| 734 | if (! rn) |
| 735 | return NULL; |
| 736 | |
| 737 | /* Unlock node. */ |
| 738 | route_unlock_node (rn); |
| 739 | |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 740 | RNODE_FOREACH_RIB (rn, match) |
Stephen Hemminger | 16814f9 | 2008-08-17 17:39:31 +0100 | [diff] [blame] | 741 | { |
| 742 | if (CHECK_FLAG (match->status, RIB_ENTRY_REMOVED)) |
| 743 | continue; |
| 744 | if (CHECK_FLAG (match->flags, ZEBRA_FLAG_SELECTED)) |
| 745 | break; |
| 746 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 747 | |
| 748 | if (! match || match->type == ZEBRA_ROUTE_BGP) |
| 749 | return NULL; |
| 750 | |
| 751 | if (match->type == ZEBRA_ROUTE_CONNECT) |
| 752 | return match; |
| 753 | |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 754 | for (ALL_NEXTHOPS_RO(match->nexthop, nexthop, tnexthop, recursing)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 755 | if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB)) |
| 756 | return match; |
| 757 | |
| 758 | return NULL; |
| 759 | } |
| 760 | |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 761 | /* |
| 762 | * This clone function, unlike its original rib_lookup_ipv4(), checks |
| 763 | * if specified IPv4 route record (prefix/mask -> gate) exists in |
| 764 | * the whole RIB and has ZEBRA_FLAG_SELECTED set. |
| 765 | * |
| 766 | * Return values: |
| 767 | * -1: error |
| 768 | * 0: exact match found |
| 769 | * 1: a match was found with a different gate |
| 770 | * 2: connected route found |
| 771 | * 3: no matches found |
| 772 | */ |
| 773 | int |
| 774 | rib_lookup_ipv4_route (struct prefix_ipv4 *p, union sockunion * qgate) |
| 775 | { |
| 776 | struct route_table *table; |
| 777 | struct route_node *rn; |
| 778 | struct rib *match; |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 779 | struct nexthop *nexthop, *tnexthop; |
| 780 | int recursing; |
| 781 | int nexthops_active; |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 782 | |
| 783 | /* Lookup table. */ |
| 784 | table = vrf_table (AFI_IP, SAFI_UNICAST, 0); |
| 785 | if (! table) |
| 786 | return ZEBRA_RIB_LOOKUP_ERROR; |
| 787 | |
| 788 | /* Scan the RIB table for exactly matching RIB entry. */ |
| 789 | rn = route_node_lookup (table, (struct prefix *) p); |
| 790 | |
| 791 | /* No route for this prefix. */ |
| 792 | if (! rn) |
| 793 | return ZEBRA_RIB_NOTFOUND; |
| 794 | |
| 795 | /* Unlock node. */ |
| 796 | route_unlock_node (rn); |
| 797 | |
| 798 | /* Find out if a "selected" RR for the discovered RIB entry exists ever. */ |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 799 | RNODE_FOREACH_RIB (rn, match) |
Stephen Hemminger | 16814f9 | 2008-08-17 17:39:31 +0100 | [diff] [blame] | 800 | { |
| 801 | if (CHECK_FLAG (match->status, RIB_ENTRY_REMOVED)) |
| 802 | continue; |
| 803 | if (CHECK_FLAG (match->flags, ZEBRA_FLAG_SELECTED)) |
| 804 | break; |
| 805 | } |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 806 | |
| 807 | /* None such found :( */ |
| 808 | if (!match) |
| 809 | return ZEBRA_RIB_NOTFOUND; |
| 810 | |
| 811 | if (match->type == ZEBRA_ROUTE_CONNECT) |
| 812 | return ZEBRA_RIB_FOUND_CONNECTED; |
| 813 | |
| 814 | /* Ok, we have a cood candidate, let's check it's nexthop list... */ |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 815 | nexthops_active = 0; |
| 816 | for (ALL_NEXTHOPS_RO(match->nexthop, nexthop, tnexthop, recursing)) |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 817 | if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB)) |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 818 | { |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 819 | nexthops_active = 1; |
| 820 | if (nexthop->gate.ipv4.s_addr == sockunion2ip (qgate)) |
| 821 | return ZEBRA_RIB_FOUND_EXACT; |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 822 | if (IS_ZEBRA_DEBUG_RIB) |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 823 | { |
| 824 | char gate_buf[INET_ADDRSTRLEN], qgate_buf[INET_ADDRSTRLEN]; |
| 825 | inet_ntop (AF_INET, &nexthop->gate.ipv4.s_addr, gate_buf, INET_ADDRSTRLEN); |
| 826 | inet_ntop (AF_INET, &sockunion2ip(qgate), qgate_buf, INET_ADDRSTRLEN); |
| 827 | zlog_debug ("%s: qgate == %s, %s == %s", __func__, |
| 828 | qgate_buf, recursing ? "rgate" : "gate", gate_buf); |
| 829 | } |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 830 | } |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 831 | |
| 832 | if (nexthops_active) |
| 833 | return ZEBRA_RIB_FOUND_NOGATE; |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 834 | |
| 835 | return ZEBRA_RIB_NOTFOUND; |
| 836 | } |
| 837 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 838 | #ifdef HAVE_IPV6 |
| 839 | struct rib * |
| 840 | rib_match_ipv6 (struct in6_addr *addr) |
| 841 | { |
| 842 | struct prefix_ipv6 p; |
| 843 | struct route_table *table; |
| 844 | struct route_node *rn; |
| 845 | struct rib *match; |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 846 | struct nexthop *newhop, *tnewhop; |
| 847 | int recursing; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 848 | |
| 849 | /* Lookup table. */ |
| 850 | table = vrf_table (AFI_IP6, SAFI_UNICAST, 0); |
| 851 | if (! table) |
| 852 | return 0; |
| 853 | |
| 854 | memset (&p, 0, sizeof (struct prefix_ipv6)); |
| 855 | p.family = AF_INET6; |
| 856 | p.prefixlen = IPV6_MAX_PREFIXLEN; |
| 857 | IPV6_ADDR_COPY (&p.prefix, addr); |
| 858 | |
| 859 | rn = route_node_match (table, (struct prefix *) &p); |
| 860 | |
| 861 | while (rn) |
| 862 | { |
| 863 | route_unlock_node (rn); |
| 864 | |
| 865 | /* Pick up selected route. */ |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 866 | RNODE_FOREACH_RIB (rn, match) |
Stephen Hemminger | 16814f9 | 2008-08-17 17:39:31 +0100 | [diff] [blame] | 867 | { |
| 868 | if (CHECK_FLAG (match->status, RIB_ENTRY_REMOVED)) |
| 869 | continue; |
| 870 | if (CHECK_FLAG (match->flags, ZEBRA_FLAG_SELECTED)) |
| 871 | break; |
| 872 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 873 | |
| 874 | /* If there is no selected route or matched route is EGP, go up |
| 875 | tree. */ |
| 876 | if (! match |
| 877 | || match->type == ZEBRA_ROUTE_BGP) |
| 878 | { |
| 879 | do { |
| 880 | rn = rn->parent; |
| 881 | } while (rn && rn->info == NULL); |
| 882 | if (rn) |
| 883 | route_lock_node (rn); |
| 884 | } |
| 885 | else |
| 886 | { |
| 887 | if (match->type == ZEBRA_ROUTE_CONNECT) |
| 888 | /* Directly point connected route. */ |
| 889 | return match; |
| 890 | else |
| 891 | { |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 892 | for (ALL_NEXTHOPS_RO(match->nexthop, newhop, tnewhop, recursing)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 893 | if (CHECK_FLAG (newhop->flags, NEXTHOP_FLAG_FIB)) |
| 894 | return match; |
| 895 | return NULL; |
| 896 | } |
| 897 | } |
| 898 | } |
| 899 | return NULL; |
| 900 | } |
| 901 | #endif /* HAVE_IPV6 */ |
| 902 | |
Paul Jakma | 7514fb7 | 2007-05-02 16:05:35 +0000 | [diff] [blame] | 903 | #define RIB_SYSTEM_ROUTE(R) \ |
| 904 | ((R)->type == ZEBRA_ROUTE_KERNEL || (R)->type == ZEBRA_ROUTE_CONNECT) |
| 905 | |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 906 | /* This function verifies reachability of one given nexthop, which can be |
| 907 | * numbered or unnumbered, IPv4 or IPv6. The result is unconditionally stored |
| 908 | * in nexthop->flags field. If the 4th parameter, 'set', is non-zero, |
| 909 | * nexthop->ifindex will be updated appropriately as well. |
| 910 | * An existing route map can turn (otherwise active) nexthop into inactive, but |
| 911 | * not vice versa. |
| 912 | * |
| 913 | * The return value is the final value of 'ACTIVE' flag. |
| 914 | */ |
| 915 | |
Stephen Hemminger | d02c56c | 2009-12-08 13:14:27 +0300 | [diff] [blame] | 916 | static unsigned |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 917 | nexthop_active_check (struct route_node *rn, struct rib *rib, |
| 918 | struct nexthop *nexthop, int set) |
| 919 | { |
| 920 | struct interface *ifp; |
Paul Jakma | 7514fb7 | 2007-05-02 16:05:35 +0000 | [diff] [blame] | 921 | route_map_result_t ret = RMAP_MATCH; |
| 922 | extern char *proto_rm[AFI_MAX][ZEBRA_ROUTE_MAX+1]; |
| 923 | struct route_map *rmap; |
| 924 | int family; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 925 | |
Paul Jakma | 7514fb7 | 2007-05-02 16:05:35 +0000 | [diff] [blame] | 926 | family = 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 927 | switch (nexthop->type) |
| 928 | { |
| 929 | case NEXTHOP_TYPE_IFINDEX: |
| 930 | ifp = if_lookup_by_index (nexthop->ifindex); |
Andrew J. Schorr | 3f08767 | 2008-01-08 20:12:46 +0000 | [diff] [blame] | 931 | if (ifp && if_is_operative(ifp)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 932 | SET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE); |
| 933 | else |
| 934 | UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE); |
| 935 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 936 | case NEXTHOP_TYPE_IPV6_IFNAME: |
Paul Jakma | 7514fb7 | 2007-05-02 16:05:35 +0000 | [diff] [blame] | 937 | family = AFI_IP6; |
| 938 | case NEXTHOP_TYPE_IFNAME: |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 939 | ifp = if_lookup_by_name (nexthop->ifname); |
Andrew J. Schorr | 3f08767 | 2008-01-08 20:12:46 +0000 | [diff] [blame] | 940 | if (ifp && if_is_operative(ifp)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 941 | { |
| 942 | if (set) |
| 943 | nexthop->ifindex = ifp->ifindex; |
| 944 | SET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE); |
| 945 | } |
| 946 | else |
| 947 | { |
| 948 | if (set) |
| 949 | nexthop->ifindex = 0; |
| 950 | UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE); |
| 951 | } |
| 952 | break; |
| 953 | case NEXTHOP_TYPE_IPV4: |
| 954 | case NEXTHOP_TYPE_IPV4_IFINDEX: |
Paul Jakma | 7514fb7 | 2007-05-02 16:05:35 +0000 | [diff] [blame] | 955 | family = AFI_IP; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 956 | if (nexthop_active_ipv4 (rib, nexthop, set, rn)) |
| 957 | SET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE); |
| 958 | else |
| 959 | UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE); |
| 960 | break; |
| 961 | #ifdef HAVE_IPV6 |
| 962 | case NEXTHOP_TYPE_IPV6: |
Paul Jakma | 7514fb7 | 2007-05-02 16:05:35 +0000 | [diff] [blame] | 963 | family = AFI_IP6; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 964 | if (nexthop_active_ipv6 (rib, nexthop, set, rn)) |
| 965 | SET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE); |
| 966 | else |
| 967 | UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE); |
| 968 | break; |
| 969 | case NEXTHOP_TYPE_IPV6_IFINDEX: |
Paul Jakma | 7514fb7 | 2007-05-02 16:05:35 +0000 | [diff] [blame] | 970 | family = AFI_IP6; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 971 | if (IN6_IS_ADDR_LINKLOCAL (&nexthop->gate.ipv6)) |
| 972 | { |
| 973 | ifp = if_lookup_by_index (nexthop->ifindex); |
Andrew J. Schorr | 3f08767 | 2008-01-08 20:12:46 +0000 | [diff] [blame] | 974 | if (ifp && if_is_operative(ifp)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 975 | SET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE); |
| 976 | else |
| 977 | UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE); |
| 978 | } |
| 979 | else |
| 980 | { |
| 981 | if (nexthop_active_ipv6 (rib, nexthop, set, rn)) |
| 982 | SET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE); |
| 983 | else |
| 984 | UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE); |
| 985 | } |
| 986 | break; |
| 987 | #endif /* HAVE_IPV6 */ |
paul | 595db7f | 2003-05-25 21:35:06 +0000 | [diff] [blame] | 988 | case NEXTHOP_TYPE_BLACKHOLE: |
| 989 | SET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE); |
| 990 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 991 | default: |
| 992 | break; |
| 993 | } |
Paul Jakma | 7514fb7 | 2007-05-02 16:05:35 +0000 | [diff] [blame] | 994 | if (! CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE)) |
| 995 | return 0; |
| 996 | |
| 997 | if (RIB_SYSTEM_ROUTE(rib) || |
| 998 | (family == AFI_IP && rn->p.family != AF_INET) || |
| 999 | (family == AFI_IP6 && rn->p.family != AF_INET6)) |
| 1000 | return CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE); |
| 1001 | |
| 1002 | rmap = 0; |
| 1003 | if (rib->type >= 0 && rib->type < ZEBRA_ROUTE_MAX && |
| 1004 | proto_rm[family][rib->type]) |
| 1005 | rmap = route_map_lookup_by_name (proto_rm[family][rib->type]); |
| 1006 | if (!rmap && proto_rm[family][ZEBRA_ROUTE_MAX]) |
| 1007 | rmap = route_map_lookup_by_name (proto_rm[family][ZEBRA_ROUTE_MAX]); |
| 1008 | if (rmap) { |
| 1009 | ret = route_map_apply(rmap, &rn->p, RMAP_ZEBRA, nexthop); |
| 1010 | } |
| 1011 | |
| 1012 | if (ret == RMAP_DENYMATCH) |
| 1013 | UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1014 | return CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE); |
| 1015 | } |
| 1016 | |
Denis Ovsienko | 03e232a | 2007-08-14 09:46:48 +0000 | [diff] [blame] | 1017 | /* Iterate over all nexthops of the given RIB entry and refresh their |
| 1018 | * ACTIVE flag. rib->nexthop_active_num is updated accordingly. If any |
| 1019 | * nexthop is found to toggle the ACTIVE flag, the whole rib structure |
| 1020 | * is flagged with ZEBRA_FLAG_CHANGED. The 4th 'set' argument is |
| 1021 | * transparently passed to nexthop_active_check(). |
| 1022 | * |
| 1023 | * Return value is the new number of active nexthops. |
| 1024 | */ |
| 1025 | |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 1026 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1027 | nexthop_active_update (struct route_node *rn, struct rib *rib, int set) |
| 1028 | { |
| 1029 | struct nexthop *nexthop; |
Stephen Hemminger | d02c56c | 2009-12-08 13:14:27 +0300 | [diff] [blame] | 1030 | unsigned int prev_active, prev_index, new_active; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1031 | |
| 1032 | rib->nexthop_active_num = 0; |
| 1033 | UNSET_FLAG (rib->flags, ZEBRA_FLAG_CHANGED); |
| 1034 | |
| 1035 | for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next) |
Denis Ovsienko | 03e232a | 2007-08-14 09:46:48 +0000 | [diff] [blame] | 1036 | { |
| 1037 | prev_active = CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE); |
Joakim Tjernlund | c3a5606 | 2009-06-24 19:15:36 +0200 | [diff] [blame] | 1038 | prev_index = nexthop->ifindex; |
Denis Ovsienko | 03e232a | 2007-08-14 09:46:48 +0000 | [diff] [blame] | 1039 | if ((new_active = nexthop_active_check (rn, rib, nexthop, set))) |
| 1040 | rib->nexthop_active_num++; |
Joakim Tjernlund | c3a5606 | 2009-06-24 19:15:36 +0200 | [diff] [blame] | 1041 | if (prev_active != new_active || |
| 1042 | prev_index != nexthop->ifindex) |
Denis Ovsienko | 03e232a | 2007-08-14 09:46:48 +0000 | [diff] [blame] | 1043 | SET_FLAG (rib->flags, ZEBRA_FLAG_CHANGED); |
| 1044 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1045 | return rib->nexthop_active_num; |
| 1046 | } |
paul | 6baeb98 | 2003-10-28 03:47:15 +0000 | [diff] [blame] | 1047 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1048 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1049 | |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 1050 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1051 | rib_install_kernel (struct route_node *rn, struct rib *rib) |
| 1052 | { |
| 1053 | int ret = 0; |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 1054 | struct nexthop *nexthop, *tnexthop; |
| 1055 | int recursing; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1056 | |
Avneesh Sachdev | 5adc252 | 2012-11-13 22:48:59 +0000 | [diff] [blame] | 1057 | /* |
| 1058 | * Make sure we update the FPM any time we send new information to |
| 1059 | * the kernel. |
| 1060 | */ |
| 1061 | zfpm_trigger_update (rn, "installing in kernel"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1062 | switch (PREFIX_FAMILY (&rn->p)) |
| 1063 | { |
| 1064 | case AF_INET: |
| 1065 | ret = kernel_add_ipv4 (&rn->p, rib); |
| 1066 | break; |
| 1067 | #ifdef HAVE_IPV6 |
| 1068 | case AF_INET6: |
| 1069 | ret = kernel_add_ipv6 (&rn->p, rib); |
| 1070 | break; |
| 1071 | #endif /* HAVE_IPV6 */ |
| 1072 | } |
| 1073 | |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 1074 | /* This condition is never met, if we are using rt_socket.c */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1075 | if (ret < 0) |
| 1076 | { |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 1077 | for (ALL_NEXTHOPS_RO(rib->nexthop, nexthop, tnexthop, recursing)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1078 | UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB); |
| 1079 | } |
| 1080 | } |
| 1081 | |
| 1082 | /* Uninstall the route from kernel. */ |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 1083 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1084 | rib_uninstall_kernel (struct route_node *rn, struct rib *rib) |
| 1085 | { |
| 1086 | int ret = 0; |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 1087 | struct nexthop *nexthop, *tnexthop; |
| 1088 | int recursing; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1089 | |
Avneesh Sachdev | 5adc252 | 2012-11-13 22:48:59 +0000 | [diff] [blame] | 1090 | /* |
| 1091 | * Make sure we update the FPM any time we send new information to |
| 1092 | * the kernel. |
| 1093 | */ |
| 1094 | zfpm_trigger_update (rn, "uninstalling from kernel"); |
| 1095 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1096 | switch (PREFIX_FAMILY (&rn->p)) |
| 1097 | { |
| 1098 | case AF_INET: |
| 1099 | ret = kernel_delete_ipv4 (&rn->p, rib); |
| 1100 | break; |
| 1101 | #ifdef HAVE_IPV6 |
| 1102 | case AF_INET6: |
| 1103 | ret = kernel_delete_ipv6 (&rn->p, rib); |
| 1104 | break; |
| 1105 | #endif /* HAVE_IPV6 */ |
| 1106 | } |
| 1107 | |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 1108 | for (ALL_NEXTHOPS_RO(rib->nexthop, nexthop, tnexthop, recursing)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1109 | UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB); |
| 1110 | |
| 1111 | return ret; |
| 1112 | } |
| 1113 | |
| 1114 | /* Uninstall the route from kernel. */ |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 1115 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1116 | rib_uninstall (struct route_node *rn, struct rib *rib) |
| 1117 | { |
| 1118 | if (CHECK_FLAG (rib->flags, ZEBRA_FLAG_SELECTED)) |
| 1119 | { |
Avneesh Sachdev | 5adc252 | 2012-11-13 22:48:59 +0000 | [diff] [blame] | 1120 | zfpm_trigger_update (rn, "rib_uninstall"); |
| 1121 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1122 | redistribute_delete (&rn->p, rib); |
| 1123 | if (! RIB_SYSTEM_ROUTE (rib)) |
| 1124 | rib_uninstall_kernel (rn, rib); |
| 1125 | UNSET_FLAG (rib->flags, ZEBRA_FLAG_SELECTED); |
| 1126 | } |
| 1127 | } |
| 1128 | |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1129 | static void rib_unlink (struct route_node *, struct rib *); |
| 1130 | |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 1131 | /* |
| 1132 | * rib_can_delete_dest |
| 1133 | * |
| 1134 | * Returns TRUE if the given dest can be deleted from the table. |
| 1135 | */ |
| 1136 | static int |
| 1137 | rib_can_delete_dest (rib_dest_t *dest) |
| 1138 | { |
| 1139 | if (dest->routes) |
| 1140 | { |
| 1141 | return 0; |
| 1142 | } |
| 1143 | |
Avneesh Sachdev | 5adc252 | 2012-11-13 22:48:59 +0000 | [diff] [blame] | 1144 | /* |
| 1145 | * Don't delete the dest if we have to update the FPM about this |
| 1146 | * prefix. |
| 1147 | */ |
| 1148 | if (CHECK_FLAG (dest->flags, RIB_DEST_UPDATE_FPM) || |
| 1149 | CHECK_FLAG (dest->flags, RIB_DEST_SENT_TO_FPM)) |
| 1150 | return 0; |
| 1151 | |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 1152 | return 1; |
| 1153 | } |
| 1154 | |
| 1155 | /* |
| 1156 | * rib_gc_dest |
| 1157 | * |
| 1158 | * Garbage collect the rib dest corresponding to the given route node |
| 1159 | * if appropriate. |
| 1160 | * |
| 1161 | * Returns TRUE if the dest was deleted, FALSE otherwise. |
| 1162 | */ |
| 1163 | int |
| 1164 | rib_gc_dest (struct route_node *rn) |
| 1165 | { |
| 1166 | rib_dest_t *dest; |
| 1167 | char buf[INET6_ADDRSTRLEN]; |
| 1168 | |
| 1169 | dest = rib_dest_from_rnode (rn); |
| 1170 | if (!dest) |
| 1171 | return 0; |
| 1172 | |
| 1173 | if (!rib_can_delete_dest (dest)) |
| 1174 | return 0; |
| 1175 | |
| 1176 | if (IS_ZEBRA_DEBUG_RIB) |
| 1177 | { |
| 1178 | inet_ntop (rn->p.family, &rn->p.u.prefix, buf, sizeof (buf)); |
| 1179 | zlog_debug ("%s: %s/%d: removing dest from table", __func__, |
| 1180 | buf, rn->p.prefixlen); |
| 1181 | } |
| 1182 | |
| 1183 | dest->rnode = NULL; |
| 1184 | XFREE (MTYPE_RIB_DEST, dest); |
| 1185 | rn->info = NULL; |
| 1186 | |
| 1187 | /* |
| 1188 | * Release the one reference that we keep on the route node. |
| 1189 | */ |
| 1190 | route_unlock_node (rn); |
| 1191 | return 1; |
| 1192 | } |
| 1193 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1194 | /* Core function for processing routing information base. */ |
Denis Ovsienko | e96f920 | 2008-06-02 12:03:22 +0000 | [diff] [blame] | 1195 | static void |
| 1196 | rib_process (struct route_node *rn) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1197 | { |
| 1198 | struct rib *rib; |
| 1199 | struct rib *next; |
| 1200 | struct rib *fib = NULL; |
| 1201 | struct rib *select = NULL; |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1202 | struct rib *del = NULL; |
paul | d753e9e | 2003-01-22 19:45:50 +0000 | [diff] [blame] | 1203 | int installed = 0; |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 1204 | struct nexthop *nexthop = NULL, *tnexthop; |
| 1205 | int recursing; |
Denis Ovsienko | f304cb4 | 2007-10-03 12:27:16 +0000 | [diff] [blame] | 1206 | char buf[INET6_ADDRSTRLEN]; |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 1207 | |
| 1208 | assert (rn); |
| 1209 | |
Paul Jakma | 93bdada | 2007-08-06 19:25:11 +0000 | [diff] [blame] | 1210 | if (IS_ZEBRA_DEBUG_RIB || IS_ZEBRA_DEBUG_RIB_Q) |
Denis Ovsienko | f304cb4 | 2007-10-03 12:27:16 +0000 | [diff] [blame] | 1211 | inet_ntop (rn->p.family, &rn->p.u.prefix, buf, INET6_ADDRSTRLEN); |
Paul Jakma | 93bdada | 2007-08-06 19:25:11 +0000 | [diff] [blame] | 1212 | |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 1213 | RNODE_FOREACH_RIB_SAFE (rn, rib, next) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1214 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1215 | /* Currently installed rib. */ |
| 1216 | if (CHECK_FLAG (rib->flags, ZEBRA_FLAG_SELECTED)) |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1217 | { |
| 1218 | assert (fib == NULL); |
| 1219 | fib = rib; |
| 1220 | } |
| 1221 | |
| 1222 | /* Unlock removed routes, so they'll be freed, bar the FIB entry, |
| 1223 | * which we need to do do further work with below. |
| 1224 | */ |
| 1225 | if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED)) |
| 1226 | { |
| 1227 | if (rib != fib) |
| 1228 | { |
| 1229 | if (IS_ZEBRA_DEBUG_RIB) |
Paul Jakma | 93bdada | 2007-08-06 19:25:11 +0000 | [diff] [blame] | 1230 | zlog_debug ("%s: %s/%d: rn %p, removing rib %p", __func__, |
| 1231 | buf, rn->p.prefixlen, rn, rib); |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 1232 | rib_unlink (rn, rib); |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1233 | } |
| 1234 | else |
| 1235 | del = rib; |
| 1236 | |
| 1237 | continue; |
| 1238 | } |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 1239 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1240 | /* Skip unreachable nexthop. */ |
| 1241 | if (! nexthop_active_update (rn, rib, 0)) |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1242 | continue; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1243 | |
| 1244 | /* Infinit distance. */ |
| 1245 | if (rib->distance == DISTANCE_INFINITY) |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 1246 | continue; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1247 | |
paul | af887b5 | 2006-01-18 14:52:52 +0000 | [diff] [blame] | 1248 | /* Newly selected rib, the common case. */ |
| 1249 | if (!select) |
| 1250 | { |
| 1251 | select = rib; |
| 1252 | continue; |
| 1253 | } |
| 1254 | |
| 1255 | /* filter route selection in following order: |
paul | af887b5 | 2006-01-18 14:52:52 +0000 | [diff] [blame] | 1256 | * - connected beats other types |
paul | a8d9c1f | 2006-01-25 06:31:04 +0000 | [diff] [blame] | 1257 | * - lower distance beats higher |
paul | af887b5 | 2006-01-18 14:52:52 +0000 | [diff] [blame] | 1258 | * - lower metric beats higher for equal distance |
| 1259 | * - last, hence oldest, route wins tie break. |
| 1260 | */ |
paul | a1038a1 | 2006-01-30 14:08:51 +0000 | [diff] [blame] | 1261 | |
| 1262 | /* Connected routes. Pick the last connected |
| 1263 | * route of the set of lowest metric connected routes. |
| 1264 | */ |
paul | a8d9c1f | 2006-01-25 06:31:04 +0000 | [diff] [blame] | 1265 | if (rib->type == ZEBRA_ROUTE_CONNECT) |
| 1266 | { |
paul | a1038a1 | 2006-01-30 14:08:51 +0000 | [diff] [blame] | 1267 | if (select->type != ZEBRA_ROUTE_CONNECT |
paul | a8d9c1f | 2006-01-25 06:31:04 +0000 | [diff] [blame] | 1268 | || rib->metric <= select->metric) |
paul | a1038a1 | 2006-01-30 14:08:51 +0000 | [diff] [blame] | 1269 | select = rib; |
| 1270 | continue; |
paul | a8d9c1f | 2006-01-25 06:31:04 +0000 | [diff] [blame] | 1271 | } |
| 1272 | else if (select->type == ZEBRA_ROUTE_CONNECT) |
| 1273 | continue; |
| 1274 | |
| 1275 | /* higher distance loses */ |
| 1276 | if (rib->distance > select->distance) |
| 1277 | continue; |
| 1278 | |
| 1279 | /* lower wins */ |
| 1280 | if (rib->distance < select->distance) |
| 1281 | { |
paul | af887b5 | 2006-01-18 14:52:52 +0000 | [diff] [blame] | 1282 | select = rib; |
paul | a8d9c1f | 2006-01-25 06:31:04 +0000 | [diff] [blame] | 1283 | continue; |
| 1284 | } |
| 1285 | |
| 1286 | /* metric tie-breaks equal distance */ |
| 1287 | if (rib->metric <= select->metric) |
| 1288 | select = rib; |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 1289 | } /* RNODE_FOREACH_RIB_SAFE */ |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 1290 | |
| 1291 | /* After the cycle is finished, the following pointers will be set: |
| 1292 | * select --- the winner RIB entry, if any was found, otherwise NULL |
| 1293 | * fib --- the SELECTED RIB entry, if any, otherwise NULL |
| 1294 | * del --- equal to fib, if fib is queued for deletion, NULL otherwise |
| 1295 | * rib --- NULL |
| 1296 | */ |
| 1297 | |
| 1298 | /* Same RIB entry is selected. Update FIB and finish. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1299 | if (select && select == fib) |
| 1300 | { |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1301 | if (IS_ZEBRA_DEBUG_RIB) |
Paul Jakma | 93bdada | 2007-08-06 19:25:11 +0000 | [diff] [blame] | 1302 | zlog_debug ("%s: %s/%d: Updating existing route, select %p, fib %p", |
| 1303 | __func__, buf, rn->p.prefixlen, select, fib); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1304 | if (CHECK_FLAG (select->flags, ZEBRA_FLAG_CHANGED)) |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 1305 | { |
Avneesh Sachdev | 5adc252 | 2012-11-13 22:48:59 +0000 | [diff] [blame] | 1306 | zfpm_trigger_update (rn, "updating existing route"); |
| 1307 | |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 1308 | redistribute_delete (&rn->p, select); |
| 1309 | if (! RIB_SYSTEM_ROUTE (select)) |
| 1310 | rib_uninstall_kernel (rn, select); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1311 | |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 1312 | /* Set real nexthop. */ |
| 1313 | nexthop_active_update (rn, select, 1); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1314 | |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 1315 | if (! RIB_SYSTEM_ROUTE (select)) |
| 1316 | rib_install_kernel (rn, select); |
| 1317 | redistribute_add (&rn->p, select); |
| 1318 | } |
paul | d753e9e | 2003-01-22 19:45:50 +0000 | [diff] [blame] | 1319 | else if (! RIB_SYSTEM_ROUTE (select)) |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 1320 | { |
| 1321 | /* Housekeeping code to deal with |
| 1322 | race conditions in kernel with linux |
| 1323 | netlink reporting interface up before IPv4 or IPv6 protocol |
| 1324 | is ready to add routes. |
| 1325 | This makes sure the routes are IN the kernel. |
| 1326 | */ |
paul | d753e9e | 2003-01-22 19:45:50 +0000 | [diff] [blame] | 1327 | |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 1328 | for (ALL_NEXTHOPS_RO(select->nexthop, nexthop, tnexthop, recursing)) |
Denis Ovsienko | a3aaf5b | 2007-10-04 10:49:21 +0000 | [diff] [blame] | 1329 | if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB)) |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 1330 | { |
Denis Ovsienko | a3aaf5b | 2007-10-04 10:49:21 +0000 | [diff] [blame] | 1331 | installed = 1; |
| 1332 | break; |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 1333 | } |
| 1334 | if (! installed) |
| 1335 | rib_install_kernel (rn, select); |
| 1336 | } |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1337 | goto end; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1338 | } |
| 1339 | |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 1340 | /* At this point we either haven't found the best RIB entry or it is |
| 1341 | * different from what we currently intend to flag with SELECTED. In both |
| 1342 | * cases, if a RIB block is present in FIB, it should be withdrawn. |
| 1343 | */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1344 | if (fib) |
| 1345 | { |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1346 | if (IS_ZEBRA_DEBUG_RIB) |
Paul Jakma | 93bdada | 2007-08-06 19:25:11 +0000 | [diff] [blame] | 1347 | zlog_debug ("%s: %s/%d: Removing existing route, fib %p", __func__, |
| 1348 | buf, rn->p.prefixlen, fib); |
Avneesh Sachdev | 5adc252 | 2012-11-13 22:48:59 +0000 | [diff] [blame] | 1349 | |
| 1350 | zfpm_trigger_update (rn, "removing existing route"); |
| 1351 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1352 | redistribute_delete (&rn->p, fib); |
| 1353 | if (! RIB_SYSTEM_ROUTE (fib)) |
| 1354 | rib_uninstall_kernel (rn, fib); |
| 1355 | UNSET_FLAG (fib->flags, ZEBRA_FLAG_SELECTED); |
| 1356 | |
| 1357 | /* Set real nexthop. */ |
| 1358 | nexthop_active_update (rn, fib, 1); |
| 1359 | } |
| 1360 | |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 1361 | /* Regardless of some RIB entry being SELECTED or not before, now we can |
| 1362 | * tell, that if a new winner exists, FIB is still not updated with this |
| 1363 | * data, but ready to be. |
| 1364 | */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1365 | if (select) |
| 1366 | { |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1367 | if (IS_ZEBRA_DEBUG_RIB) |
Paul Jakma | 93bdada | 2007-08-06 19:25:11 +0000 | [diff] [blame] | 1368 | zlog_debug ("%s: %s/%d: Adding route, select %p", __func__, buf, |
| 1369 | rn->p.prefixlen, select); |
Avneesh Sachdev | 5adc252 | 2012-11-13 22:48:59 +0000 | [diff] [blame] | 1370 | |
| 1371 | zfpm_trigger_update (rn, "new route selected"); |
| 1372 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1373 | /* Set real nexthop. */ |
| 1374 | nexthop_active_update (rn, select, 1); |
| 1375 | |
| 1376 | if (! RIB_SYSTEM_ROUTE (select)) |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 1377 | rib_install_kernel (rn, select); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1378 | SET_FLAG (select->flags, ZEBRA_FLAG_SELECTED); |
| 1379 | redistribute_add (&rn->p, select); |
| 1380 | } |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 1381 | |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1382 | /* FIB route was removed, should be deleted */ |
| 1383 | if (del) |
| 1384 | { |
| 1385 | if (IS_ZEBRA_DEBUG_RIB) |
Paul Jakma | 93bdada | 2007-08-06 19:25:11 +0000 | [diff] [blame] | 1386 | zlog_debug ("%s: %s/%d: Deleting fib %p, rn %p", __func__, buf, |
| 1387 | rn->p.prefixlen, del, rn); |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1388 | rib_unlink (rn, del); |
| 1389 | } |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 1390 | |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1391 | end: |
| 1392 | if (IS_ZEBRA_DEBUG_RIB_Q) |
Paul Jakma | 93bdada | 2007-08-06 19:25:11 +0000 | [diff] [blame] | 1393 | zlog_debug ("%s: %s/%d: rn %p dequeued", __func__, buf, rn->p.prefixlen, rn); |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 1394 | |
| 1395 | /* |
| 1396 | * Check if the dest can be deleted now. |
| 1397 | */ |
| 1398 | rib_gc_dest (rn); |
Denis Ovsienko | e96f920 | 2008-06-02 12:03:22 +0000 | [diff] [blame] | 1399 | } |
| 1400 | |
Stephen Hemminger | 5110a0c | 2008-08-11 16:22:15 -0700 | [diff] [blame] | 1401 | /* Take a list of route_node structs and return 1, if there was a record |
| 1402 | * picked from it and processed by rib_process(). Don't process more, |
| 1403 | * than one RN record; operate only in the specified sub-queue. |
Denis Ovsienko | e96f920 | 2008-06-02 12:03:22 +0000 | [diff] [blame] | 1404 | */ |
Stephen Hemminger | ef9b113 | 2008-08-17 17:44:47 +0100 | [diff] [blame] | 1405 | static unsigned int |
Denis Ovsienko | e96f920 | 2008-06-02 12:03:22 +0000 | [diff] [blame] | 1406 | process_subq (struct list * subq, u_char qindex) |
| 1407 | { |
Stephen Hemminger | 5110a0c | 2008-08-11 16:22:15 -0700 | [diff] [blame] | 1408 | struct listnode *lnode = listhead (subq); |
Denis Ovsienko | e96f920 | 2008-06-02 12:03:22 +0000 | [diff] [blame] | 1409 | struct route_node *rnode; |
Stephen Hemminger | 5110a0c | 2008-08-11 16:22:15 -0700 | [diff] [blame] | 1410 | |
| 1411 | if (!lnode) |
Denis Ovsienko | e96f920 | 2008-06-02 12:03:22 +0000 | [diff] [blame] | 1412 | return 0; |
Stephen Hemminger | 5110a0c | 2008-08-11 16:22:15 -0700 | [diff] [blame] | 1413 | |
Denis Ovsienko | e96f920 | 2008-06-02 12:03:22 +0000 | [diff] [blame] | 1414 | rnode = listgetdata (lnode); |
| 1415 | rib_process (rnode); |
Stephen Hemminger | 5110a0c | 2008-08-11 16:22:15 -0700 | [diff] [blame] | 1416 | |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 1417 | if (rnode->info) |
| 1418 | UNSET_FLAG (rib_dest_from_rnode (rnode)->flags, RIB_ROUTE_QUEUED (qindex)); |
| 1419 | |
Chris Caputo | 67b9467 | 2009-07-18 04:02:26 +0000 | [diff] [blame] | 1420 | #if 0 |
Stephen Hemminger | 5110a0c | 2008-08-11 16:22:15 -0700 | [diff] [blame] | 1421 | else |
| 1422 | { |
| 1423 | zlog_debug ("%s: called for route_node (%p, %d) with no ribs", |
| 1424 | __func__, rnode, rnode->lock); |
| 1425 | zlog_backtrace(LOG_DEBUG); |
| 1426 | } |
Chris Caputo | 67b9467 | 2009-07-18 04:02:26 +0000 | [diff] [blame] | 1427 | #endif |
Denis Ovsienko | e96f920 | 2008-06-02 12:03:22 +0000 | [diff] [blame] | 1428 | route_unlock_node (rnode); |
| 1429 | list_delete_node (subq, lnode); |
| 1430 | return 1; |
| 1431 | } |
| 1432 | |
| 1433 | /* Dispatch the meta queue by picking, processing and unlocking the next RN from |
| 1434 | * a non-empty sub-queue with lowest priority. wq is equal to zebra->ribq and data |
| 1435 | * is pointed to the meta queue structure. |
| 1436 | */ |
| 1437 | static wq_item_status |
| 1438 | meta_queue_process (struct work_queue *dummy, void *data) |
| 1439 | { |
| 1440 | struct meta_queue * mq = data; |
Stephen Hemminger | 5110a0c | 2008-08-11 16:22:15 -0700 | [diff] [blame] | 1441 | unsigned i; |
| 1442 | |
Denis Ovsienko | e96f920 | 2008-06-02 12:03:22 +0000 | [diff] [blame] | 1443 | for (i = 0; i < MQ_SIZE; i++) |
| 1444 | if (process_subq (mq->subq[i], i)) |
Stephen Hemminger | 5110a0c | 2008-08-11 16:22:15 -0700 | [diff] [blame] | 1445 | { |
| 1446 | mq->size--; |
| 1447 | break; |
| 1448 | } |
Denis Ovsienko | e96f920 | 2008-06-02 12:03:22 +0000 | [diff] [blame] | 1449 | return mq->size ? WQ_REQUEUE : WQ_SUCCESS; |
| 1450 | } |
| 1451 | |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 1452 | /* |
| 1453 | * Map from rib types to queue type (priority) in meta queue |
| 1454 | */ |
Stephen Hemminger | 5110a0c | 2008-08-11 16:22:15 -0700 | [diff] [blame] | 1455 | static const u_char meta_queue_map[ZEBRA_ROUTE_MAX] = { |
| 1456 | [ZEBRA_ROUTE_SYSTEM] = 4, |
| 1457 | [ZEBRA_ROUTE_KERNEL] = 0, |
| 1458 | [ZEBRA_ROUTE_CONNECT] = 0, |
| 1459 | [ZEBRA_ROUTE_STATIC] = 1, |
| 1460 | [ZEBRA_ROUTE_RIP] = 2, |
| 1461 | [ZEBRA_ROUTE_RIPNG] = 2, |
| 1462 | [ZEBRA_ROUTE_OSPF] = 2, |
| 1463 | [ZEBRA_ROUTE_OSPF6] = 2, |
| 1464 | [ZEBRA_ROUTE_ISIS] = 2, |
| 1465 | [ZEBRA_ROUTE_BGP] = 3, |
| 1466 | [ZEBRA_ROUTE_HSLS] = 4, |
Paul Jakma | 5734509 | 2011-12-25 17:52:09 +0100 | [diff] [blame] | 1467 | [ZEBRA_ROUTE_BABEL] = 2, |
Stephen Hemminger | 5110a0c | 2008-08-11 16:22:15 -0700 | [diff] [blame] | 1468 | }; |
| 1469 | |
| 1470 | /* Look into the RN and queue it into one or more priority queues, |
| 1471 | * increasing the size for each data push done. |
Denis Ovsienko | e96f920 | 2008-06-02 12:03:22 +0000 | [diff] [blame] | 1472 | */ |
Stephen Hemminger | ef9b113 | 2008-08-17 17:44:47 +0100 | [diff] [blame] | 1473 | static void |
| 1474 | rib_meta_queue_add (struct meta_queue *mq, struct route_node *rn) |
Denis Ovsienko | e96f920 | 2008-06-02 12:03:22 +0000 | [diff] [blame] | 1475 | { |
Denis Ovsienko | e96f920 | 2008-06-02 12:03:22 +0000 | [diff] [blame] | 1476 | struct rib *rib; |
| 1477 | char buf[INET6_ADDRSTRLEN]; |
Stephen Hemminger | 5110a0c | 2008-08-11 16:22:15 -0700 | [diff] [blame] | 1478 | |
Denis Ovsienko | e96f920 | 2008-06-02 12:03:22 +0000 | [diff] [blame] | 1479 | if (IS_ZEBRA_DEBUG_RIB_Q) |
| 1480 | inet_ntop (rn->p.family, &rn->p.u.prefix, buf, INET6_ADDRSTRLEN); |
Stephen Hemminger | 5110a0c | 2008-08-11 16:22:15 -0700 | [diff] [blame] | 1481 | |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 1482 | RNODE_FOREACH_RIB (rn, rib) |
Denis Ovsienko | e96f920 | 2008-06-02 12:03:22 +0000 | [diff] [blame] | 1483 | { |
Stephen Hemminger | 5110a0c | 2008-08-11 16:22:15 -0700 | [diff] [blame] | 1484 | u_char qindex = meta_queue_map[rib->type]; |
| 1485 | |
| 1486 | /* Invariant: at this point we always have rn->info set. */ |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 1487 | if (CHECK_FLAG (rib_dest_from_rnode (rn)->flags, |
| 1488 | RIB_ROUTE_QUEUED (qindex))) |
Stephen Hemminger | 5110a0c | 2008-08-11 16:22:15 -0700 | [diff] [blame] | 1489 | { |
| 1490 | if (IS_ZEBRA_DEBUG_RIB_Q) |
| 1491 | zlog_debug ("%s: %s/%d: rn %p is already queued in sub-queue %u", |
| 1492 | __func__, buf, rn->p.prefixlen, rn, qindex); |
| 1493 | continue; |
| 1494 | } |
| 1495 | |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 1496 | SET_FLAG (rib_dest_from_rnode (rn)->flags, RIB_ROUTE_QUEUED (qindex)); |
Stephen Hemminger | 5110a0c | 2008-08-11 16:22:15 -0700 | [diff] [blame] | 1497 | listnode_add (mq->subq[qindex], rn); |
| 1498 | route_lock_node (rn); |
| 1499 | mq->size++; |
| 1500 | |
Denis Ovsienko | e96f920 | 2008-06-02 12:03:22 +0000 | [diff] [blame] | 1501 | if (IS_ZEBRA_DEBUG_RIB_Q) |
Stephen Hemminger | 5110a0c | 2008-08-11 16:22:15 -0700 | [diff] [blame] | 1502 | zlog_debug ("%s: %s/%d: queued rn %p into sub-queue %u", |
| 1503 | __func__, buf, rn->p.prefixlen, rn, qindex); |
Denis Ovsienko | e96f920 | 2008-06-02 12:03:22 +0000 | [diff] [blame] | 1504 | } |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 1505 | } |
| 1506 | |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1507 | /* Add route_node to work queue and schedule processing */ |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 1508 | static void |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1509 | rib_queue_add (struct zebra_t *zebra, struct route_node *rn) |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 1510 | { |
Subbaiah Venkata | fc328ac | 2012-03-27 16:35:22 -0700 | [diff] [blame] | 1511 | char buf[INET_ADDRSTRLEN]; |
| 1512 | assert (zebra && rn); |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 1513 | |
Paul Jakma | 93bdada | 2007-08-06 19:25:11 +0000 | [diff] [blame] | 1514 | if (IS_ZEBRA_DEBUG_RIB_Q) |
Subbaiah Venkata | fc328ac | 2012-03-27 16:35:22 -0700 | [diff] [blame] | 1515 | inet_ntop (AF_INET, &rn->p.u.prefix, buf, INET_ADDRSTRLEN); |
Stephen Hemminger | cc2dd92 | 2009-12-09 17:54:49 +0300 | [diff] [blame] | 1516 | |
Subbaiah Venkata | fc328ac | 2012-03-27 16:35:22 -0700 | [diff] [blame] | 1517 | /* Pointless to queue a route_node with no RIB entries to add or remove */ |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 1518 | if (!rnode_to_ribs (rn)) |
Subbaiah Venkata | fc328ac | 2012-03-27 16:35:22 -0700 | [diff] [blame] | 1519 | { |
| 1520 | zlog_debug ("%s: called for route_node (%p, %d) with no ribs", |
| 1521 | __func__, rn, rn->lock); |
| 1522 | zlog_backtrace(LOG_DEBUG); |
| 1523 | return; |
| 1524 | } |
| 1525 | |
| 1526 | if (IS_ZEBRA_DEBUG_RIB_Q) |
| 1527 | zlog_info ("%s: %s/%d: work queue added", __func__, buf, rn->p.prefixlen); |
| 1528 | |
| 1529 | assert (zebra); |
| 1530 | |
| 1531 | if (zebra->ribq == NULL) |
| 1532 | { |
| 1533 | zlog_err ("%s: work_queue does not exist!", __func__); |
| 1534 | return; |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1535 | } |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 1536 | |
Stephen Hemminger | cc2dd92 | 2009-12-09 17:54:49 +0300 | [diff] [blame] | 1537 | /* |
| 1538 | * The RIB queue should normally be either empty or holding the only |
| 1539 | * work_queue_item element. In the latter case this element would |
| 1540 | * hold a pointer to the meta queue structure, which must be used to |
| 1541 | * actually queue the route nodes to process. So create the MQ |
| 1542 | * holder, if necessary, then push the work into it in any case. |
Denis Ovsienko | e96f920 | 2008-06-02 12:03:22 +0000 | [diff] [blame] | 1543 | * This semantics was introduced after 0.99.9 release. |
| 1544 | */ |
Denis Ovsienko | e96f920 | 2008-06-02 12:03:22 +0000 | [diff] [blame] | 1545 | if (!zebra->ribq->items->count) |
| 1546 | work_queue_add (zebra->ribq, zebra->mq); |
| 1547 | |
| 1548 | rib_meta_queue_add (zebra->mq, rn); |
Subbaiah Venkata | fc328ac | 2012-03-27 16:35:22 -0700 | [diff] [blame] | 1549 | |
| 1550 | if (IS_ZEBRA_DEBUG_RIB_Q) |
| 1551 | zlog_debug ("%s: %s/%d: rn %p queued", __func__, buf, rn->p.prefixlen, rn); |
| 1552 | |
| 1553 | return; |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 1554 | } |
| 1555 | |
Stephen Hemminger | 5110a0c | 2008-08-11 16:22:15 -0700 | [diff] [blame] | 1556 | /* Create new meta queue. |
| 1557 | A destructor function doesn't seem to be necessary here. |
| 1558 | */ |
Stephen Hemminger | ef9b113 | 2008-08-17 17:44:47 +0100 | [diff] [blame] | 1559 | static struct meta_queue * |
| 1560 | meta_queue_new (void) |
Denis Ovsienko | e96f920 | 2008-06-02 12:03:22 +0000 | [diff] [blame] | 1561 | { |
| 1562 | struct meta_queue *new; |
Stephen Hemminger | 5110a0c | 2008-08-11 16:22:15 -0700 | [diff] [blame] | 1563 | unsigned i; |
Denis Ovsienko | e96f920 | 2008-06-02 12:03:22 +0000 | [diff] [blame] | 1564 | |
Stephen Hemminger | 5110a0c | 2008-08-11 16:22:15 -0700 | [diff] [blame] | 1565 | new = XCALLOC (MTYPE_WORK_QUEUE, sizeof (struct meta_queue)); |
| 1566 | assert(new); |
| 1567 | |
Denis Ovsienko | e96f920 | 2008-06-02 12:03:22 +0000 | [diff] [blame] | 1568 | for (i = 0; i < MQ_SIZE; i++) |
Stephen Hemminger | 5110a0c | 2008-08-11 16:22:15 -0700 | [diff] [blame] | 1569 | { |
| 1570 | new->subq[i] = list_new (); |
| 1571 | assert(new->subq[i]); |
| 1572 | } |
| 1573 | |
Denis Ovsienko | e96f920 | 2008-06-02 12:03:22 +0000 | [diff] [blame] | 1574 | return new; |
| 1575 | } |
| 1576 | |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 1577 | /* initialise zebra rib work queue */ |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 1578 | static void |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 1579 | rib_queue_init (struct zebra_t *zebra) |
| 1580 | { |
Subbaiah Venkata | fc328ac | 2012-03-27 16:35:22 -0700 | [diff] [blame] | 1581 | assert (zebra); |
| 1582 | |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 1583 | if (! (zebra->ribq = work_queue_new (zebra->master, |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1584 | "route_node processing"))) |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 1585 | { |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1586 | zlog_err ("%s: could not initialise work queue!", __func__); |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 1587 | return; |
| 1588 | } |
| 1589 | |
| 1590 | /* fill in the work queue spec */ |
Denis Ovsienko | e96f920 | 2008-06-02 12:03:22 +0000 | [diff] [blame] | 1591 | zebra->ribq->spec.workfunc = &meta_queue_process; |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 1592 | zebra->ribq->spec.errorfunc = NULL; |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 1593 | /* XXX: TODO: These should be runtime configurable via vty */ |
| 1594 | zebra->ribq->spec.max_retries = 3; |
Paul Jakma | 457eb9a | 2006-07-27 19:59:58 +0000 | [diff] [blame] | 1595 | zebra->ribq->spec.hold = rib_process_hold_time; |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 1596 | |
Denis Ovsienko | e96f920 | 2008-06-02 12:03:22 +0000 | [diff] [blame] | 1597 | if (!(zebra->mq = meta_queue_new ())) |
Subbaiah Venkata | fc328ac | 2012-03-27 16:35:22 -0700 | [diff] [blame] | 1598 | { |
Denis Ovsienko | e96f920 | 2008-06-02 12:03:22 +0000 | [diff] [blame] | 1599 | zlog_err ("%s: could not initialise meta queue!", __func__); |
Subbaiah Venkata | fc328ac | 2012-03-27 16:35:22 -0700 | [diff] [blame] | 1600 | return; |
| 1601 | } |
| 1602 | return; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1603 | } |
| 1604 | |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1605 | /* RIB updates are processed via a queue of pointers to route_nodes. |
| 1606 | * |
| 1607 | * The queue length is bounded by the maximal size of the routing table, |
| 1608 | * as a route_node will not be requeued, if already queued. |
| 1609 | * |
Paul Jakma | 3c0755d | 2006-12-08 00:53:14 +0000 | [diff] [blame] | 1610 | * RIBs are submitted via rib_addnode or rib_delnode which set minimal |
| 1611 | * state, or static_install_ipv{4,6} (when an existing RIB is updated) |
| 1612 | * and then submit route_node to queue for best-path selection later. |
| 1613 | * Order of add/delete state changes are preserved for any given RIB. |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1614 | * |
| 1615 | * Deleted RIBs are reaped during best-path selection. |
| 1616 | * |
| 1617 | * rib_addnode |
| 1618 | * |-> rib_link or unset RIB_ENTRY_REMOVE |->Update kernel with |
Paul Jakma | 3c0755d | 2006-12-08 00:53:14 +0000 | [diff] [blame] | 1619 | * |-------->| | best RIB, if required |
| 1620 | * | | |
| 1621 | * static_install->|->rib_addqueue...... -> rib_process |
| 1622 | * | | |
| 1623 | * |-------->| |-> rib_unlink |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1624 | * |-> set RIB_ENTRY_REMOVE | |
| 1625 | * rib_delnode (RIB freed) |
| 1626 | * |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 1627 | * The 'info' pointer of a route_node points to a rib_dest_t |
| 1628 | * ('dest'). Queueing state for a route_node is kept on the dest. The |
| 1629 | * dest is created on-demand by rib_link() and is kept around at least |
| 1630 | * as long as there are ribs hanging off it (@see rib_gc_dest()). |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1631 | * |
| 1632 | * Refcounting (aka "locking" throughout the GNU Zebra and Quagga code): |
| 1633 | * |
| 1634 | * - route_nodes: refcounted by: |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 1635 | * - dest attached to route_node: |
| 1636 | * - managed by: rib_link/rib_gc_dest |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1637 | * - route_node processing queue |
| 1638 | * - managed by: rib_addqueue, rib_process. |
| 1639 | * |
| 1640 | */ |
| 1641 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1642 | /* Add RIB to head of the route node. */ |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 1643 | static void |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1644 | rib_link (struct route_node *rn, struct rib *rib) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1645 | { |
| 1646 | struct rib *head; |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 1647 | rib_dest_t *dest; |
Denis Ovsienko | f304cb4 | 2007-10-03 12:27:16 +0000 | [diff] [blame] | 1648 | char buf[INET6_ADDRSTRLEN]; |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 1649 | |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 1650 | assert (rib && rn); |
| 1651 | |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1652 | if (IS_ZEBRA_DEBUG_RIB) |
Paul Jakma | 93bdada | 2007-08-06 19:25:11 +0000 | [diff] [blame] | 1653 | { |
Denis Ovsienko | f304cb4 | 2007-10-03 12:27:16 +0000 | [diff] [blame] | 1654 | inet_ntop (rn->p.family, &rn->p.u.prefix, buf, INET6_ADDRSTRLEN); |
Paul Jakma | 93bdada | 2007-08-06 19:25:11 +0000 | [diff] [blame] | 1655 | zlog_debug ("%s: %s/%d: rn %p, rib %p", __func__, |
| 1656 | buf, rn->p.prefixlen, rn, rib); |
| 1657 | } |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1658 | |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 1659 | dest = rib_dest_from_rnode (rn); |
| 1660 | if (!dest) |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1661 | { |
| 1662 | if (IS_ZEBRA_DEBUG_RIB) |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 1663 | { |
| 1664 | zlog_debug ("%s: %s/%d: adding dest to table", __func__, |
| 1665 | buf, rn->p.prefixlen); |
| 1666 | } |
| 1667 | |
| 1668 | dest = XCALLOC (MTYPE_RIB_DEST, sizeof (rib_dest_t)); |
| 1669 | route_lock_node (rn); /* rn route table reference */ |
| 1670 | rn->info = dest; |
| 1671 | dest->rnode = rn; |
| 1672 | } |
| 1673 | |
| 1674 | head = dest->routes; |
| 1675 | if (head) |
| 1676 | { |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1677 | head->prev = rib; |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1678 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1679 | rib->next = head; |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 1680 | dest->routes = rib; |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1681 | rib_queue_add (&zebrad, rn); |
| 1682 | } |
| 1683 | |
| 1684 | static void |
| 1685 | rib_addnode (struct route_node *rn, struct rib *rib) |
| 1686 | { |
| 1687 | /* RIB node has been un-removed before route-node is processed. |
| 1688 | * route_node must hence already be on the queue for processing.. |
| 1689 | */ |
| 1690 | if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED)) |
| 1691 | { |
| 1692 | if (IS_ZEBRA_DEBUG_RIB) |
Paul Jakma | 93bdada | 2007-08-06 19:25:11 +0000 | [diff] [blame] | 1693 | { |
Denis Ovsienko | f304cb4 | 2007-10-03 12:27:16 +0000 | [diff] [blame] | 1694 | char buf[INET6_ADDRSTRLEN]; |
| 1695 | inet_ntop (rn->p.family, &rn->p.u.prefix, buf, INET6_ADDRSTRLEN); |
Paul Jakma | 93bdada | 2007-08-06 19:25:11 +0000 | [diff] [blame] | 1696 | zlog_debug ("%s: %s/%d: rn %p, un-removed rib %p", |
| 1697 | __func__, buf, rn->p.prefixlen, rn, rib); |
| 1698 | } |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1699 | UNSET_FLAG (rib->status, RIB_ENTRY_REMOVED); |
| 1700 | return; |
| 1701 | } |
| 1702 | rib_link (rn, rib); |
| 1703 | } |
| 1704 | |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 1705 | /* |
| 1706 | * rib_unlink |
| 1707 | * |
| 1708 | * Detach a rib structure from a route_node. |
| 1709 | * |
| 1710 | * Note that a call to rib_unlink() should be followed by a call to |
| 1711 | * rib_gc_dest() at some point. This allows a rib_dest_t that is no |
| 1712 | * longer required to be deleted. |
| 1713 | */ |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1714 | static void |
| 1715 | rib_unlink (struct route_node *rn, struct rib *rib) |
| 1716 | { |
Denis Ovsienko | f304cb4 | 2007-10-03 12:27:16 +0000 | [diff] [blame] | 1717 | char buf[INET6_ADDRSTRLEN]; |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 1718 | rib_dest_t *dest; |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1719 | |
| 1720 | assert (rn && rib); |
| 1721 | |
| 1722 | if (IS_ZEBRA_DEBUG_RIB) |
Paul Jakma | 93bdada | 2007-08-06 19:25:11 +0000 | [diff] [blame] | 1723 | { |
Denis Ovsienko | f304cb4 | 2007-10-03 12:27:16 +0000 | [diff] [blame] | 1724 | inet_ntop (rn->p.family, &rn->p.u.prefix, buf, INET6_ADDRSTRLEN); |
Paul Jakma | 93bdada | 2007-08-06 19:25:11 +0000 | [diff] [blame] | 1725 | zlog_debug ("%s: %s/%d: rn %p, rib %p", |
| 1726 | __func__, buf, rn->p.prefixlen, rn, rib); |
| 1727 | } |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1728 | |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 1729 | dest = rib_dest_from_rnode (rn); |
| 1730 | |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1731 | if (rib->next) |
| 1732 | rib->next->prev = rib->prev; |
| 1733 | |
| 1734 | if (rib->prev) |
| 1735 | rib->prev->next = rib->next; |
| 1736 | else |
| 1737 | { |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 1738 | dest->routes = rib->next; |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1739 | } |
| 1740 | |
| 1741 | /* free RIB and nexthops */ |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 1742 | nexthops_free(rib->nexthop); |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1743 | XFREE (MTYPE_RIB, rib); |
| 1744 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1745 | } |
| 1746 | |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 1747 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1748 | rib_delnode (struct route_node *rn, struct rib *rib) |
| 1749 | { |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1750 | if (IS_ZEBRA_DEBUG_RIB) |
Paul Jakma | 93bdada | 2007-08-06 19:25:11 +0000 | [diff] [blame] | 1751 | { |
Denis Ovsienko | f304cb4 | 2007-10-03 12:27:16 +0000 | [diff] [blame] | 1752 | char buf[INET6_ADDRSTRLEN]; |
| 1753 | inet_ntop (rn->p.family, &rn->p.u.prefix, buf, INET6_ADDRSTRLEN); |
Paul Jakma | 93bdada | 2007-08-06 19:25:11 +0000 | [diff] [blame] | 1754 | zlog_debug ("%s: %s/%d: rn %p, rib %p, removing", __func__, |
| 1755 | buf, rn->p.prefixlen, rn, rib); |
| 1756 | } |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1757 | SET_FLAG (rib->status, RIB_ENTRY_REMOVED); |
| 1758 | rib_queue_add (&zebrad, rn); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1759 | } |
| 1760 | |
| 1761 | int |
| 1762 | rib_add_ipv4 (int type, int flags, struct prefix_ipv4 *p, |
Paul Jakma | 7514fb7 | 2007-05-02 16:05:35 +0000 | [diff] [blame] | 1763 | struct in_addr *gate, struct in_addr *src, |
| 1764 | unsigned int ifindex, u_int32_t vrf_id, |
G.Balaji | cddf391 | 2011-11-26 21:59:32 +0400 | [diff] [blame] | 1765 | u_int32_t metric, u_char distance, safi_t safi) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1766 | { |
| 1767 | struct rib *rib; |
| 1768 | struct rib *same = NULL; |
| 1769 | struct route_table *table; |
| 1770 | struct route_node *rn; |
| 1771 | struct nexthop *nexthop; |
| 1772 | |
| 1773 | /* Lookup table. */ |
G.Balaji | cddf391 | 2011-11-26 21:59:32 +0400 | [diff] [blame] | 1774 | table = vrf_table (AFI_IP, safi, 0); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1775 | if (! table) |
| 1776 | return 0; |
| 1777 | |
| 1778 | /* Make it sure prefixlen is applied to the prefix. */ |
| 1779 | apply_mask_ipv4 (p); |
| 1780 | |
| 1781 | /* Set default distance by route type. */ |
| 1782 | if (distance == 0) |
| 1783 | { |
Balaji.G | 837d16c | 2012-09-26 14:09:10 +0530 | [diff] [blame] | 1784 | if ((unsigned)type >= array_size(route_info)) |
David Lamparter | 7052f22 | 2009-08-27 00:28:28 +0200 | [diff] [blame] | 1785 | distance = 150; |
| 1786 | else |
| 1787 | distance = route_info[type].distance; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1788 | |
| 1789 | /* iBGP distance is 200. */ |
| 1790 | if (type == ZEBRA_ROUTE_BGP && CHECK_FLAG (flags, ZEBRA_FLAG_IBGP)) |
| 1791 | distance = 200; |
| 1792 | } |
| 1793 | |
| 1794 | /* Lookup route node.*/ |
| 1795 | rn = route_node_get (table, (struct prefix *) p); |
| 1796 | |
| 1797 | /* If same type of route are installed, treat it as a implicit |
| 1798 | withdraw. */ |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 1799 | RNODE_FOREACH_RIB (rn, rib) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1800 | { |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1801 | if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED)) |
| 1802 | continue; |
| 1803 | |
hasso | ebf1ead | 2005-09-21 14:58:20 +0000 | [diff] [blame] | 1804 | if (rib->type != type) |
| 1805 | continue; |
| 1806 | if (rib->type != ZEBRA_ROUTE_CONNECT) |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 1807 | { |
| 1808 | same = rib; |
| 1809 | break; |
| 1810 | } |
hasso | ebf1ead | 2005-09-21 14:58:20 +0000 | [diff] [blame] | 1811 | /* Duplicate connected route comes in. */ |
| 1812 | else if ((nexthop = rib->nexthop) && |
| 1813 | nexthop->type == NEXTHOP_TYPE_IFINDEX && |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 1814 | nexthop->ifindex == ifindex && |
| 1815 | !CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED)) |
hasso | ebf1ead | 2005-09-21 14:58:20 +0000 | [diff] [blame] | 1816 | { |
| 1817 | rib->refcnt++; |
| 1818 | return 0 ; |
| 1819 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1820 | } |
| 1821 | |
| 1822 | /* Allocate new rib structure. */ |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 1823 | rib = XCALLOC (MTYPE_RIB, sizeof (struct rib)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1824 | rib->type = type; |
| 1825 | rib->distance = distance; |
| 1826 | rib->flags = flags; |
| 1827 | rib->metric = metric; |
paul | b5f4502 | 2003-11-02 07:28:05 +0000 | [diff] [blame] | 1828 | rib->table = vrf_id; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1829 | rib->nexthop_num = 0; |
| 1830 | rib->uptime = time (NULL); |
| 1831 | |
| 1832 | /* Nexthop settings. */ |
| 1833 | if (gate) |
| 1834 | { |
| 1835 | if (ifindex) |
Paul Jakma | 7514fb7 | 2007-05-02 16:05:35 +0000 | [diff] [blame] | 1836 | nexthop_ipv4_ifindex_add (rib, gate, src, ifindex); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1837 | else |
Paul Jakma | 7514fb7 | 2007-05-02 16:05:35 +0000 | [diff] [blame] | 1838 | nexthop_ipv4_add (rib, gate, src); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1839 | } |
| 1840 | else |
| 1841 | nexthop_ifindex_add (rib, ifindex); |
| 1842 | |
| 1843 | /* If this route is kernel route, set FIB flag to the route. */ |
| 1844 | if (type == ZEBRA_ROUTE_KERNEL || type == ZEBRA_ROUTE_CONNECT) |
| 1845 | for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next) |
| 1846 | SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB); |
| 1847 | |
| 1848 | /* Link new rib to node.*/ |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 1849 | if (IS_ZEBRA_DEBUG_RIB) |
| 1850 | zlog_debug ("%s: calling rib_addnode (%p, %p)", __func__, rn, rib); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1851 | rib_addnode (rn, rib); |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 1852 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1853 | /* Free implicit route.*/ |
| 1854 | if (same) |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 1855 | { |
| 1856 | if (IS_ZEBRA_DEBUG_RIB) |
| 1857 | zlog_debug ("%s: calling rib_delnode (%p, %p)", __func__, rn, rib); |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 1858 | rib_delnode (rn, same); |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 1859 | } |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 1860 | |
| 1861 | route_unlock_node (rn); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1862 | return 0; |
| 1863 | } |
| 1864 | |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 1865 | /* This function dumps the contents of a given RIB entry into |
| 1866 | * standard debug log. Calling function name and IP prefix in |
| 1867 | * question are passed as 1st and 2nd arguments. |
| 1868 | */ |
| 1869 | |
| 1870 | void rib_dump (const char * func, const struct prefix_ipv4 * p, const struct rib * rib) |
| 1871 | { |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 1872 | char straddr[INET_ADDRSTRLEN]; |
| 1873 | struct nexthop *nexthop, *tnexthop; |
| 1874 | int recursing; |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 1875 | |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 1876 | inet_ntop (AF_INET, &p->prefix, straddr, INET_ADDRSTRLEN); |
| 1877 | zlog_debug ("%s: dumping RIB entry %p for %s/%d", func, rib, straddr, p->prefixlen); |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 1878 | zlog_debug |
| 1879 | ( |
Stephen Hemminger | d02c56c | 2009-12-08 13:14:27 +0300 | [diff] [blame] | 1880 | "%s: refcnt == %lu, uptime == %lu, type == %u, table == %d", |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 1881 | func, |
| 1882 | rib->refcnt, |
Stephen Hemminger | d02c56c | 2009-12-08 13:14:27 +0300 | [diff] [blame] | 1883 | (unsigned long) rib->uptime, |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 1884 | rib->type, |
| 1885 | rib->table |
| 1886 | ); |
| 1887 | zlog_debug |
| 1888 | ( |
| 1889 | "%s: metric == %u, distance == %u, flags == %u, status == %u", |
| 1890 | func, |
| 1891 | rib->metric, |
| 1892 | rib->distance, |
| 1893 | rib->flags, |
| 1894 | rib->status |
| 1895 | ); |
| 1896 | zlog_debug |
| 1897 | ( |
| 1898 | "%s: nexthop_num == %u, nexthop_active_num == %u, nexthop_fib_num == %u", |
| 1899 | func, |
| 1900 | rib->nexthop_num, |
| 1901 | rib->nexthop_active_num, |
| 1902 | rib->nexthop_fib_num |
| 1903 | ); |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 1904 | for (ALL_NEXTHOPS_RO(rib->nexthop, nexthop, tnexthop, recursing)) |
| 1905 | { |
| 1906 | inet_ntop (AF_INET, &nexthop->gate.ipv4.s_addr, straddr, INET_ADDRSTRLEN); |
| 1907 | zlog_debug |
| 1908 | ( |
| 1909 | "%s: %s %s with flags %s%s%s", |
| 1910 | func, |
| 1911 | (recursing ? " NH" : "NH"), |
| 1912 | straddr, |
| 1913 | (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE) ? "ACTIVE " : ""), |
| 1914 | (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB) ? "FIB " : ""), |
| 1915 | (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_RECURSIVE) ? "RECURSIVE" : "") |
| 1916 | ); |
| 1917 | } |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 1918 | zlog_debug ("%s: dump complete", func); |
| 1919 | } |
| 1920 | |
| 1921 | /* This is an exported helper to rtm_read() to dump the strange |
| 1922 | * RIB entry found by rib_lookup_ipv4_route() |
| 1923 | */ |
| 1924 | |
| 1925 | void rib_lookup_and_dump (struct prefix_ipv4 * p) |
| 1926 | { |
| 1927 | struct route_table *table; |
| 1928 | struct route_node *rn; |
| 1929 | struct rib *rib; |
| 1930 | char prefix_buf[INET_ADDRSTRLEN]; |
| 1931 | |
| 1932 | /* Lookup table. */ |
| 1933 | table = vrf_table (AFI_IP, SAFI_UNICAST, 0); |
| 1934 | if (! table) |
| 1935 | { |
| 1936 | zlog_err ("%s: vrf_table() returned NULL", __func__); |
| 1937 | return; |
| 1938 | } |
| 1939 | |
| 1940 | inet_ntop (AF_INET, &p->prefix.s_addr, prefix_buf, INET_ADDRSTRLEN); |
| 1941 | /* Scan the RIB table for exactly matching RIB entry. */ |
| 1942 | rn = route_node_lookup (table, (struct prefix *) p); |
| 1943 | |
| 1944 | /* No route for this prefix. */ |
| 1945 | if (! rn) |
| 1946 | { |
| 1947 | zlog_debug ("%s: lookup failed for %s/%d", __func__, prefix_buf, p->prefixlen); |
| 1948 | return; |
| 1949 | } |
| 1950 | |
| 1951 | /* Unlock node. */ |
| 1952 | route_unlock_node (rn); |
| 1953 | |
| 1954 | /* let's go */ |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 1955 | RNODE_FOREACH_RIB (rn, rib) |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 1956 | { |
| 1957 | zlog_debug |
| 1958 | ( |
| 1959 | "%s: rn %p, rib %p: %s, %s", |
| 1960 | __func__, |
| 1961 | rn, |
| 1962 | rib, |
| 1963 | (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED) ? "removed" : "NOT removed"), |
| 1964 | (CHECK_FLAG (rib->flags, ZEBRA_FLAG_SELECTED) ? "selected" : "NOT selected") |
| 1965 | ); |
| 1966 | rib_dump (__func__, p, rib); |
| 1967 | } |
| 1968 | } |
| 1969 | |
Denis Ovsienko | 20e5ff0 | 2008-02-26 14:02:24 +0000 | [diff] [blame] | 1970 | /* Check if requested address assignment will fail due to another |
| 1971 | * route being installed by zebra in FIB already. Take necessary |
| 1972 | * actions, if needed: remove such a route from FIB and deSELECT |
| 1973 | * corresponding RIB entry. Then put affected RN into RIBQ head. |
| 1974 | */ |
| 1975 | void rib_lookup_and_pushup (struct prefix_ipv4 * p) |
| 1976 | { |
| 1977 | struct route_table *table; |
| 1978 | struct route_node *rn; |
| 1979 | struct rib *rib; |
| 1980 | unsigned changed = 0; |
| 1981 | |
| 1982 | if (NULL == (table = vrf_table (AFI_IP, SAFI_UNICAST, 0))) |
| 1983 | { |
| 1984 | zlog_err ("%s: vrf_table() returned NULL", __func__); |
| 1985 | return; |
| 1986 | } |
| 1987 | |
| 1988 | /* No matches would be the simplest case. */ |
| 1989 | if (NULL == (rn = route_node_lookup (table, (struct prefix *) p))) |
| 1990 | return; |
| 1991 | |
| 1992 | /* Unlock node. */ |
| 1993 | route_unlock_node (rn); |
| 1994 | |
| 1995 | /* Check all RIB entries. In case any changes have to be done, requeue |
| 1996 | * the RN into RIBQ head. If the routing message about the new connected |
| 1997 | * route (generated by the IP address we are going to assign very soon) |
| 1998 | * comes before the RIBQ is processed, the new RIB entry will join |
| 1999 | * RIBQ record already on head. This is necessary for proper revalidation |
| 2000 | * of the rest of the RIB. |
| 2001 | */ |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 2002 | RNODE_FOREACH_RIB (rn, rib) |
Denis Ovsienko | 20e5ff0 | 2008-02-26 14:02:24 +0000 | [diff] [blame] | 2003 | { |
| 2004 | if (CHECK_FLAG (rib->flags, ZEBRA_FLAG_SELECTED) && |
| 2005 | ! RIB_SYSTEM_ROUTE (rib)) |
| 2006 | { |
| 2007 | changed = 1; |
| 2008 | if (IS_ZEBRA_DEBUG_RIB) |
| 2009 | { |
| 2010 | char buf[INET_ADDRSTRLEN]; |
| 2011 | inet_ntop (rn->p.family, &p->prefix, buf, INET_ADDRSTRLEN); |
| 2012 | zlog_debug ("%s: freeing way for connected prefix %s/%d", __func__, buf, p->prefixlen); |
| 2013 | rib_dump (__func__, (struct prefix_ipv4 *)&rn->p, rib); |
| 2014 | } |
| 2015 | rib_uninstall (rn, rib); |
| 2016 | } |
| 2017 | } |
| 2018 | if (changed) |
Denis Ovsienko | 20e5ff0 | 2008-02-26 14:02:24 +0000 | [diff] [blame] | 2019 | rib_queue_add (&zebrad, rn); |
Denis Ovsienko | 20e5ff0 | 2008-02-26 14:02:24 +0000 | [diff] [blame] | 2020 | } |
| 2021 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2022 | int |
G.Balaji | cddf391 | 2011-11-26 21:59:32 +0400 | [diff] [blame] | 2023 | rib_add_ipv4_multipath (struct prefix_ipv4 *p, struct rib *rib, safi_t safi) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2024 | { |
| 2025 | struct route_table *table; |
| 2026 | struct route_node *rn; |
| 2027 | struct rib *same; |
| 2028 | struct nexthop *nexthop; |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 2029 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2030 | /* Lookup table. */ |
G.Balaji | cddf391 | 2011-11-26 21:59:32 +0400 | [diff] [blame] | 2031 | table = vrf_table (AFI_IP, safi, 0); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2032 | if (! table) |
| 2033 | return 0; |
G.Balaji | cddf391 | 2011-11-26 21:59:32 +0400 | [diff] [blame] | 2034 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2035 | /* Make it sure prefixlen is applied to the prefix. */ |
| 2036 | apply_mask_ipv4 (p); |
| 2037 | |
| 2038 | /* Set default distance by route type. */ |
| 2039 | if (rib->distance == 0) |
| 2040 | { |
| 2041 | rib->distance = route_info[rib->type].distance; |
| 2042 | |
| 2043 | /* iBGP distance is 200. */ |
| 2044 | if (rib->type == ZEBRA_ROUTE_BGP |
| 2045 | && CHECK_FLAG (rib->flags, ZEBRA_FLAG_IBGP)) |
| 2046 | rib->distance = 200; |
| 2047 | } |
| 2048 | |
| 2049 | /* Lookup route node.*/ |
| 2050 | rn = route_node_get (table, (struct prefix *) p); |
| 2051 | |
| 2052 | /* If same type of route are installed, treat it as a implicit |
| 2053 | withdraw. */ |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 2054 | RNODE_FOREACH_RIB (rn, same) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2055 | { |
Paul Jakma | 0b8c4f1 | 2007-06-27 11:12:38 +0000 | [diff] [blame] | 2056 | if (CHECK_FLAG (same->status, RIB_ENTRY_REMOVED)) |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 2057 | continue; |
| 2058 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2059 | if (same->type == rib->type && same->table == rib->table |
| 2060 | && same->type != ZEBRA_ROUTE_CONNECT) |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 2061 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2062 | } |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 2063 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2064 | /* If this route is kernel route, set FIB flag to the route. */ |
| 2065 | if (rib->type == ZEBRA_ROUTE_KERNEL || rib->type == ZEBRA_ROUTE_CONNECT) |
| 2066 | for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next) |
| 2067 | SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB); |
| 2068 | |
| 2069 | /* Link new rib to node.*/ |
| 2070 | rib_addnode (rn, rib); |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 2071 | if (IS_ZEBRA_DEBUG_RIB) |
| 2072 | { |
| 2073 | zlog_debug ("%s: called rib_addnode (%p, %p) on new RIB entry", |
| 2074 | __func__, rn, rib); |
| 2075 | rib_dump (__func__, p, rib); |
| 2076 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2077 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2078 | /* Free implicit route.*/ |
| 2079 | if (same) |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 2080 | { |
| 2081 | if (IS_ZEBRA_DEBUG_RIB) |
| 2082 | { |
| 2083 | zlog_debug ("%s: calling rib_delnode (%p, %p) on existing RIB entry", |
| 2084 | __func__, rn, same); |
| 2085 | rib_dump (__func__, p, same); |
| 2086 | } |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 2087 | rib_delnode (rn, same); |
Denis Ovsienko | dc95824 | 2007-08-13 16:03:06 +0000 | [diff] [blame] | 2088 | } |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 2089 | |
| 2090 | route_unlock_node (rn); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2091 | return 0; |
| 2092 | } |
| 2093 | |
hasso | ebf1ead | 2005-09-21 14:58:20 +0000 | [diff] [blame] | 2094 | /* XXX factor with rib_delete_ipv6 */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2095 | int |
| 2096 | rib_delete_ipv4 (int type, int flags, struct prefix_ipv4 *p, |
G.Balaji | cddf391 | 2011-11-26 21:59:32 +0400 | [diff] [blame] | 2097 | struct in_addr *gate, unsigned int ifindex, u_int32_t vrf_id, safi_t safi) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2098 | { |
| 2099 | struct route_table *table; |
| 2100 | struct route_node *rn; |
| 2101 | struct rib *rib; |
| 2102 | struct rib *fib = NULL; |
| 2103 | struct rib *same = NULL; |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 2104 | struct nexthop *nexthop, *tnexthop; |
| 2105 | int recursing; |
Stephen Hemminger | 81cce01 | 2009-04-28 14:28:00 -0700 | [diff] [blame] | 2106 | char buf1[INET_ADDRSTRLEN]; |
| 2107 | char buf2[INET_ADDRSTRLEN]; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2108 | |
| 2109 | /* Lookup table. */ |
G.Balaji | cddf391 | 2011-11-26 21:59:32 +0400 | [diff] [blame] | 2110 | table = vrf_table (AFI_IP, safi, 0); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2111 | if (! table) |
| 2112 | return 0; |
| 2113 | |
| 2114 | /* Apply mask. */ |
| 2115 | apply_mask_ipv4 (p); |
| 2116 | |
paul | 5ec90d2 | 2003-06-19 01:41:37 +0000 | [diff] [blame] | 2117 | if (IS_ZEBRA_DEBUG_KERNEL && gate) |
ajs | b617800 | 2004-12-07 21:12:56 +0000 | [diff] [blame] | 2118 | zlog_debug ("rib_delete_ipv4(): route delete %s/%d via %s ifindex %d", |
Stephen Hemminger | 81cce01 | 2009-04-28 14:28:00 -0700 | [diff] [blame] | 2119 | inet_ntop (AF_INET, &p->prefix, buf1, INET_ADDRSTRLEN), |
paul | 5ec90d2 | 2003-06-19 01:41:37 +0000 | [diff] [blame] | 2120 | p->prefixlen, |
| 2121 | inet_ntoa (*gate), |
| 2122 | ifindex); |
| 2123 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2124 | /* Lookup route node. */ |
| 2125 | rn = route_node_lookup (table, (struct prefix *) p); |
| 2126 | if (! rn) |
| 2127 | { |
| 2128 | if (IS_ZEBRA_DEBUG_KERNEL) |
| 2129 | { |
| 2130 | if (gate) |
ajs | b617800 | 2004-12-07 21:12:56 +0000 | [diff] [blame] | 2131 | zlog_debug ("route %s/%d via %s ifindex %d doesn't exist in rib", |
Stephen Hemminger | 81cce01 | 2009-04-28 14:28:00 -0700 | [diff] [blame] | 2132 | inet_ntop (AF_INET, &p->prefix, buf1, INET_ADDRSTRLEN), |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2133 | p->prefixlen, |
Stephen Hemminger | 81cce01 | 2009-04-28 14:28:00 -0700 | [diff] [blame] | 2134 | inet_ntop (AF_INET, gate, buf2, INET_ADDRSTRLEN), |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2135 | ifindex); |
| 2136 | else |
ajs | b617800 | 2004-12-07 21:12:56 +0000 | [diff] [blame] | 2137 | zlog_debug ("route %s/%d ifindex %d doesn't exist in rib", |
Stephen Hemminger | 81cce01 | 2009-04-28 14:28:00 -0700 | [diff] [blame] | 2138 | inet_ntop (AF_INET, &p->prefix, buf1, INET_ADDRSTRLEN), |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2139 | p->prefixlen, |
| 2140 | ifindex); |
| 2141 | } |
| 2142 | return ZEBRA_ERR_RTNOEXIST; |
| 2143 | } |
| 2144 | |
| 2145 | /* Lookup same type route. */ |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 2146 | RNODE_FOREACH_RIB (rn, rib) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2147 | { |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 2148 | if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED)) |
| 2149 | continue; |
| 2150 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2151 | if (CHECK_FLAG (rib->flags, ZEBRA_FLAG_SELECTED)) |
| 2152 | fib = rib; |
| 2153 | |
hasso | ebf1ead | 2005-09-21 14:58:20 +0000 | [diff] [blame] | 2154 | if (rib->type != type) |
| 2155 | continue; |
| 2156 | if (rib->type == ZEBRA_ROUTE_CONNECT && (nexthop = rib->nexthop) && |
Matthias Ferdinand | 4f1735f | 2011-12-26 16:35:30 +0400 | [diff] [blame] | 2157 | nexthop->type == NEXTHOP_TYPE_IFINDEX) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2158 | { |
Matthias Ferdinand | 4f1735f | 2011-12-26 16:35:30 +0400 | [diff] [blame] | 2159 | if (nexthop->ifindex != ifindex) |
| 2160 | continue; |
hasso | ebf1ead | 2005-09-21 14:58:20 +0000 | [diff] [blame] | 2161 | if (rib->refcnt) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2162 | { |
hasso | ebf1ead | 2005-09-21 14:58:20 +0000 | [diff] [blame] | 2163 | rib->refcnt--; |
| 2164 | route_unlock_node (rn); |
| 2165 | route_unlock_node (rn); |
| 2166 | return 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2167 | } |
hasso | ebf1ead | 2005-09-21 14:58:20 +0000 | [diff] [blame] | 2168 | same = rib; |
| 2169 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2170 | } |
hasso | ebf1ead | 2005-09-21 14:58:20 +0000 | [diff] [blame] | 2171 | /* Make sure that the route found has the same gateway. */ |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 2172 | else |
paul | 5ec90d2 | 2003-06-19 01:41:37 +0000 | [diff] [blame] | 2173 | { |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 2174 | if (gate == NULL) |
| 2175 | { |
| 2176 | same = rib; |
| 2177 | break; |
| 2178 | } |
| 2179 | for (ALL_NEXTHOPS_RO(rib->nexthop, nexthop, tnexthop, recursing)) |
| 2180 | if (IPV4_ADDR_SAME (&nexthop->gate.ipv4, gate)) |
| 2181 | { |
| 2182 | same = rib; |
| 2183 | break; |
| 2184 | } |
| 2185 | if (same) |
| 2186 | break; |
| 2187 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2188 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2189 | /* If same type of route can't be found and this message is from |
| 2190 | kernel. */ |
| 2191 | if (! same) |
| 2192 | { |
| 2193 | if (fib && type == ZEBRA_ROUTE_KERNEL) |
| 2194 | { |
| 2195 | /* Unset flags. */ |
| 2196 | for (nexthop = fib->nexthop; nexthop; nexthop = nexthop->next) |
| 2197 | UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB); |
| 2198 | |
| 2199 | UNSET_FLAG (fib->flags, ZEBRA_FLAG_SELECTED); |
| 2200 | } |
| 2201 | else |
| 2202 | { |
| 2203 | if (IS_ZEBRA_DEBUG_KERNEL) |
| 2204 | { |
| 2205 | if (gate) |
ajs | b617800 | 2004-12-07 21:12:56 +0000 | [diff] [blame] | 2206 | zlog_debug ("route %s/%d via %s ifindex %d type %d doesn't exist in rib", |
Stephen Hemminger | 81cce01 | 2009-04-28 14:28:00 -0700 | [diff] [blame] | 2207 | inet_ntop (AF_INET, &p->prefix, buf1, INET_ADDRSTRLEN), |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2208 | p->prefixlen, |
Stephen Hemminger | 81cce01 | 2009-04-28 14:28:00 -0700 | [diff] [blame] | 2209 | inet_ntop (AF_INET, gate, buf2, INET_ADDRSTRLEN), |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2210 | ifindex, |
| 2211 | type); |
| 2212 | else |
ajs | b617800 | 2004-12-07 21:12:56 +0000 | [diff] [blame] | 2213 | zlog_debug ("route %s/%d ifindex %d type %d doesn't exist in rib", |
Stephen Hemminger | 81cce01 | 2009-04-28 14:28:00 -0700 | [diff] [blame] | 2214 | inet_ntop (AF_INET, &p->prefix, buf1, INET_ADDRSTRLEN), |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2215 | p->prefixlen, |
| 2216 | ifindex, |
| 2217 | type); |
| 2218 | } |
| 2219 | route_unlock_node (rn); |
| 2220 | return ZEBRA_ERR_RTNOEXIST; |
| 2221 | } |
| 2222 | } |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 2223 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2224 | if (same) |
| 2225 | rib_delnode (rn, same); |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 2226 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2227 | route_unlock_node (rn); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2228 | return 0; |
| 2229 | } |
| 2230 | |
| 2231 | /* Install static route into rib. */ |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 2232 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2233 | static_install_ipv4 (struct prefix *p, struct static_ipv4 *si) |
| 2234 | { |
| 2235 | struct rib *rib; |
| 2236 | struct route_node *rn; |
| 2237 | struct route_table *table; |
| 2238 | |
| 2239 | /* Lookup table. */ |
| 2240 | table = vrf_table (AFI_IP, SAFI_UNICAST, 0); |
| 2241 | if (! table) |
| 2242 | return; |
| 2243 | |
| 2244 | /* Lookup existing route */ |
| 2245 | rn = route_node_get (table, p); |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 2246 | RNODE_FOREACH_RIB (rn, rib) |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 2247 | { |
| 2248 | if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED)) |
| 2249 | continue; |
| 2250 | |
| 2251 | if (rib->type == ZEBRA_ROUTE_STATIC && rib->distance == si->distance) |
| 2252 | break; |
| 2253 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2254 | |
| 2255 | if (rib) |
| 2256 | { |
| 2257 | /* Same distance static route is there. Update it with new |
| 2258 | nexthop. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2259 | route_unlock_node (rn); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2260 | switch (si->type) |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 2261 | { |
| 2262 | case STATIC_IPV4_GATEWAY: |
Paul Jakma | 7514fb7 | 2007-05-02 16:05:35 +0000 | [diff] [blame] | 2263 | nexthop_ipv4_add (rib, &si->gate.ipv4, NULL); |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 2264 | break; |
| 2265 | case STATIC_IPV4_IFNAME: |
| 2266 | nexthop_ifname_add (rib, si->gate.ifname); |
| 2267 | break; |
| 2268 | case STATIC_IPV4_BLACKHOLE: |
| 2269 | nexthop_blackhole_add (rib); |
| 2270 | break; |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 2271 | } |
Paul Jakma | 3c0755d | 2006-12-08 00:53:14 +0000 | [diff] [blame] | 2272 | rib_queue_add (&zebrad, rn); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2273 | } |
| 2274 | else |
| 2275 | { |
| 2276 | /* This is new static route. */ |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 2277 | rib = XCALLOC (MTYPE_RIB, sizeof (struct rib)); |
| 2278 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2279 | rib->type = ZEBRA_ROUTE_STATIC; |
| 2280 | rib->distance = si->distance; |
| 2281 | rib->metric = 0; |
Nolan Leake | b0145dd | 2012-09-13 17:17:31 +0000 | [diff] [blame] | 2282 | rib->table = zebrad.rtm_table_default; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2283 | rib->nexthop_num = 0; |
| 2284 | |
| 2285 | switch (si->type) |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 2286 | { |
| 2287 | case STATIC_IPV4_GATEWAY: |
Paul Jakma | 7514fb7 | 2007-05-02 16:05:35 +0000 | [diff] [blame] | 2288 | nexthop_ipv4_add (rib, &si->gate.ipv4, NULL); |
paul | 7021c42 | 2003-07-15 12:52:22 +0000 | [diff] [blame] | 2289 | break; |
| 2290 | case STATIC_IPV4_IFNAME: |
| 2291 | nexthop_ifname_add (rib, si->gate.ifname); |
| 2292 | break; |
| 2293 | case STATIC_IPV4_BLACKHOLE: |
| 2294 | nexthop_blackhole_add (rib); |
| 2295 | break; |
| 2296 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2297 | |
hasso | 81dfcaa | 2003-05-25 19:21:25 +0000 | [diff] [blame] | 2298 | /* Save the flags of this static routes (reject, blackhole) */ |
| 2299 | rib->flags = si->flags; |
| 2300 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2301 | /* Link this rib to the tree. */ |
| 2302 | rib_addnode (rn, rib); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2303 | } |
| 2304 | } |
| 2305 | |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 2306 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2307 | static_ipv4_nexthop_same (struct nexthop *nexthop, struct static_ipv4 *si) |
| 2308 | { |
| 2309 | if (nexthop->type == NEXTHOP_TYPE_IPV4 |
| 2310 | && si->type == STATIC_IPV4_GATEWAY |
| 2311 | && IPV4_ADDR_SAME (&nexthop->gate.ipv4, &si->gate.ipv4)) |
| 2312 | return 1; |
| 2313 | if (nexthop->type == NEXTHOP_TYPE_IFNAME |
| 2314 | && si->type == STATIC_IPV4_IFNAME |
| 2315 | && strcmp (nexthop->ifname, si->gate.ifname) == 0) |
| 2316 | return 1; |
paul | 595db7f | 2003-05-25 21:35:06 +0000 | [diff] [blame] | 2317 | if (nexthop->type == NEXTHOP_TYPE_BLACKHOLE |
| 2318 | && si->type == STATIC_IPV4_BLACKHOLE) |
| 2319 | return 1; |
paul | e8e1946 | 2006-01-19 20:16:55 +0000 | [diff] [blame] | 2320 | return 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2321 | } |
| 2322 | |
| 2323 | /* Uninstall static route from RIB. */ |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 2324 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2325 | static_uninstall_ipv4 (struct prefix *p, struct static_ipv4 *si) |
| 2326 | { |
| 2327 | struct route_node *rn; |
| 2328 | struct rib *rib; |
| 2329 | struct nexthop *nexthop; |
| 2330 | struct route_table *table; |
| 2331 | |
| 2332 | /* Lookup table. */ |
| 2333 | table = vrf_table (AFI_IP, SAFI_UNICAST, 0); |
| 2334 | if (! table) |
| 2335 | return; |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 2336 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2337 | /* Lookup existing route with type and distance. */ |
| 2338 | rn = route_node_lookup (table, p); |
| 2339 | if (! rn) |
| 2340 | return; |
| 2341 | |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 2342 | RNODE_FOREACH_RIB (rn, rib) |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 2343 | { |
| 2344 | if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED)) |
| 2345 | continue; |
| 2346 | |
| 2347 | if (rib->type == ZEBRA_ROUTE_STATIC && rib->distance == si->distance) |
| 2348 | break; |
| 2349 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2350 | |
| 2351 | if (! rib) |
| 2352 | { |
| 2353 | route_unlock_node (rn); |
| 2354 | return; |
| 2355 | } |
| 2356 | |
| 2357 | /* Lookup nexthop. */ |
| 2358 | for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next) |
| 2359 | if (static_ipv4_nexthop_same (nexthop, si)) |
| 2360 | break; |
| 2361 | |
| 2362 | /* Can't find nexthop. */ |
| 2363 | if (! nexthop) |
| 2364 | { |
| 2365 | route_unlock_node (rn); |
| 2366 | return; |
| 2367 | } |
| 2368 | |
| 2369 | /* Check nexthop. */ |
| 2370 | if (rib->nexthop_num == 1) |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 2371 | rib_delnode (rn, rib); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2372 | else |
| 2373 | { |
paul | 6baeb98 | 2003-10-28 03:47:15 +0000 | [diff] [blame] | 2374 | if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB)) |
| 2375 | rib_uninstall (rn, rib); |
paul | 319572c | 2005-09-21 12:30:08 +0000 | [diff] [blame] | 2376 | nexthop_delete (rib, nexthop); |
| 2377 | nexthop_free (nexthop); |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 2378 | rib_queue_add (&zebrad, rn); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2379 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2380 | /* Unlock node. */ |
| 2381 | route_unlock_node (rn); |
| 2382 | } |
| 2383 | |
| 2384 | /* Add static route into static route configuration. */ |
| 2385 | int |
hasso | 39db97e | 2004-10-12 20:50:58 +0000 | [diff] [blame] | 2386 | static_add_ipv4 (struct prefix *p, struct in_addr *gate, const char *ifname, |
hasso | 81dfcaa | 2003-05-25 19:21:25 +0000 | [diff] [blame] | 2387 | u_char flags, u_char distance, u_int32_t vrf_id) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2388 | { |
| 2389 | u_char type = 0; |
| 2390 | struct route_node *rn; |
| 2391 | struct static_ipv4 *si; |
| 2392 | struct static_ipv4 *pp; |
| 2393 | struct static_ipv4 *cp; |
| 2394 | struct static_ipv4 *update = NULL; |
| 2395 | struct route_table *stable; |
| 2396 | |
| 2397 | /* Lookup table. */ |
| 2398 | stable = vrf_static_table (AFI_IP, SAFI_UNICAST, vrf_id); |
| 2399 | if (! stable) |
| 2400 | return -1; |
| 2401 | |
| 2402 | /* Lookup static route prefix. */ |
| 2403 | rn = route_node_get (stable, p); |
| 2404 | |
| 2405 | /* Make flags. */ |
| 2406 | if (gate) |
| 2407 | type = STATIC_IPV4_GATEWAY; |
paul | 368aa3f | 2003-05-25 23:24:50 +0000 | [diff] [blame] | 2408 | else if (ifname) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2409 | type = STATIC_IPV4_IFNAME; |
paul | 595db7f | 2003-05-25 21:35:06 +0000 | [diff] [blame] | 2410 | else |
| 2411 | type = STATIC_IPV4_BLACKHOLE; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2412 | |
| 2413 | /* Do nothing if there is a same static route. */ |
| 2414 | for (si = rn->info; si; si = si->next) |
| 2415 | { |
| 2416 | if (type == si->type |
| 2417 | && (! gate || IPV4_ADDR_SAME (gate, &si->gate.ipv4)) |
| 2418 | && (! ifname || strcmp (ifname, si->gate.ifname) == 0)) |
| 2419 | { |
| 2420 | if (distance == si->distance) |
| 2421 | { |
| 2422 | route_unlock_node (rn); |
| 2423 | return 0; |
| 2424 | } |
| 2425 | else |
| 2426 | update = si; |
| 2427 | } |
| 2428 | } |
| 2429 | |
Paul Jakma | 3c0755d | 2006-12-08 00:53:14 +0000 | [diff] [blame] | 2430 | /* Distance changed. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2431 | if (update) |
| 2432 | static_delete_ipv4 (p, gate, ifname, update->distance, vrf_id); |
| 2433 | |
| 2434 | /* Make new static route structure. */ |
Stephen Hemminger | 393deb9 | 2008-08-18 14:13:29 -0700 | [diff] [blame] | 2435 | si = XCALLOC (MTYPE_STATIC_IPV4, sizeof (struct static_ipv4)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2436 | |
| 2437 | si->type = type; |
| 2438 | si->distance = distance; |
hasso | 81dfcaa | 2003-05-25 19:21:25 +0000 | [diff] [blame] | 2439 | si->flags = flags; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2440 | |
| 2441 | if (gate) |
| 2442 | si->gate.ipv4 = *gate; |
| 2443 | if (ifname) |
| 2444 | si->gate.ifname = XSTRDUP (0, ifname); |
| 2445 | |
| 2446 | /* Add new static route information to the tree with sort by |
| 2447 | distance value and gateway address. */ |
| 2448 | for (pp = NULL, cp = rn->info; cp; pp = cp, cp = cp->next) |
| 2449 | { |
| 2450 | if (si->distance < cp->distance) |
| 2451 | break; |
| 2452 | if (si->distance > cp->distance) |
| 2453 | continue; |
| 2454 | if (si->type == STATIC_IPV4_GATEWAY && cp->type == STATIC_IPV4_GATEWAY) |
| 2455 | { |
| 2456 | if (ntohl (si->gate.ipv4.s_addr) < ntohl (cp->gate.ipv4.s_addr)) |
| 2457 | break; |
| 2458 | if (ntohl (si->gate.ipv4.s_addr) > ntohl (cp->gate.ipv4.s_addr)) |
| 2459 | continue; |
| 2460 | } |
| 2461 | } |
| 2462 | |
| 2463 | /* Make linked list. */ |
| 2464 | if (pp) |
| 2465 | pp->next = si; |
| 2466 | else |
| 2467 | rn->info = si; |
| 2468 | if (cp) |
| 2469 | cp->prev = si; |
| 2470 | si->prev = pp; |
| 2471 | si->next = cp; |
| 2472 | |
| 2473 | /* Install into rib. */ |
| 2474 | static_install_ipv4 (p, si); |
| 2475 | |
| 2476 | return 1; |
| 2477 | } |
| 2478 | |
| 2479 | /* Delete static route from static route configuration. */ |
| 2480 | int |
hasso | 39db97e | 2004-10-12 20:50:58 +0000 | [diff] [blame] | 2481 | static_delete_ipv4 (struct prefix *p, struct in_addr *gate, const char *ifname, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2482 | u_char distance, u_int32_t vrf_id) |
| 2483 | { |
| 2484 | u_char type = 0; |
| 2485 | struct route_node *rn; |
| 2486 | struct static_ipv4 *si; |
| 2487 | struct route_table *stable; |
| 2488 | |
| 2489 | /* Lookup table. */ |
| 2490 | stable = vrf_static_table (AFI_IP, SAFI_UNICAST, vrf_id); |
| 2491 | if (! stable) |
| 2492 | return -1; |
| 2493 | |
| 2494 | /* Lookup static route prefix. */ |
| 2495 | rn = route_node_lookup (stable, p); |
| 2496 | if (! rn) |
| 2497 | return 0; |
| 2498 | |
| 2499 | /* Make flags. */ |
| 2500 | if (gate) |
| 2501 | type = STATIC_IPV4_GATEWAY; |
| 2502 | else if (ifname) |
| 2503 | type = STATIC_IPV4_IFNAME; |
paul | 595db7f | 2003-05-25 21:35:06 +0000 | [diff] [blame] | 2504 | else |
| 2505 | type = STATIC_IPV4_BLACKHOLE; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2506 | |
| 2507 | /* Find same static route is the tree */ |
| 2508 | for (si = rn->info; si; si = si->next) |
| 2509 | if (type == si->type |
| 2510 | && (! gate || IPV4_ADDR_SAME (gate, &si->gate.ipv4)) |
| 2511 | && (! ifname || strcmp (ifname, si->gate.ifname) == 0)) |
| 2512 | break; |
| 2513 | |
| 2514 | /* Can't find static route. */ |
| 2515 | if (! si) |
| 2516 | { |
| 2517 | route_unlock_node (rn); |
| 2518 | return 0; |
| 2519 | } |
| 2520 | |
| 2521 | /* Install into rib. */ |
| 2522 | static_uninstall_ipv4 (p, si); |
| 2523 | |
| 2524 | /* Unlink static route from linked list. */ |
| 2525 | if (si->prev) |
| 2526 | si->prev->next = si->next; |
| 2527 | else |
| 2528 | rn->info = si->next; |
| 2529 | if (si->next) |
| 2530 | si->next->prev = si->prev; |
paul | 143a385 | 2003-09-29 20:06:13 +0000 | [diff] [blame] | 2531 | route_unlock_node (rn); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2532 | |
| 2533 | /* Free static route configuration. */ |
paul | a0f6acd | 2003-05-14 18:29:13 +0000 | [diff] [blame] | 2534 | if (ifname) |
| 2535 | XFREE (0, si->gate.ifname); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2536 | XFREE (MTYPE_STATIC_IPV4, si); |
| 2537 | |
paul | 143a385 | 2003-09-29 20:06:13 +0000 | [diff] [blame] | 2538 | route_unlock_node (rn); |
| 2539 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2540 | return 1; |
| 2541 | } |
| 2542 | |
| 2543 | |
| 2544 | #ifdef HAVE_IPV6 |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 2545 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2546 | rib_bogus_ipv6 (int type, struct prefix_ipv6 *p, |
| 2547 | struct in6_addr *gate, unsigned int ifindex, int table) |
| 2548 | { |
hasso | 726f9b2 | 2003-05-25 21:04:54 +0000 | [diff] [blame] | 2549 | if (type == ZEBRA_ROUTE_CONNECT && IN6_IS_ADDR_UNSPECIFIED (&p->prefix)) { |
| 2550 | #if defined (MUSICA) || defined (LINUX) |
| 2551 | /* IN6_IS_ADDR_V4COMPAT(&p->prefix) */ |
| 2552 | if (p->prefixlen == 96) |
| 2553 | return 0; |
| 2554 | #endif /* MUSICA */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2555 | return 1; |
hasso | 726f9b2 | 2003-05-25 21:04:54 +0000 | [diff] [blame] | 2556 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2557 | if (type == ZEBRA_ROUTE_KERNEL && IN6_IS_ADDR_UNSPECIFIED (&p->prefix) |
| 2558 | && p->prefixlen == 96 && gate && IN6_IS_ADDR_UNSPECIFIED (gate)) |
| 2559 | { |
| 2560 | kernel_delete_ipv6_old (p, gate, ifindex, 0, table); |
| 2561 | return 1; |
| 2562 | } |
| 2563 | return 0; |
| 2564 | } |
| 2565 | |
| 2566 | int |
| 2567 | rib_add_ipv6 (int type, int flags, struct prefix_ipv6 *p, |
hasso | be61c4e | 2005-08-27 06:05:47 +0000 | [diff] [blame] | 2568 | struct in6_addr *gate, unsigned int ifindex, u_int32_t vrf_id, |
G.Balaji | f768f36 | 2011-11-26 22:10:39 +0400 | [diff] [blame] | 2569 | u_int32_t metric, u_char distance, safi_t safi) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2570 | { |
| 2571 | struct rib *rib; |
| 2572 | struct rib *same = NULL; |
| 2573 | struct route_table *table; |
| 2574 | struct route_node *rn; |
| 2575 | struct nexthop *nexthop; |
| 2576 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2577 | /* Lookup table. */ |
G.Balaji | f768f36 | 2011-11-26 22:10:39 +0400 | [diff] [blame] | 2578 | table = vrf_table (AFI_IP6, safi, 0); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2579 | if (! table) |
| 2580 | return 0; |
| 2581 | |
| 2582 | /* Make sure mask is applied. */ |
| 2583 | apply_mask_ipv6 (p); |
| 2584 | |
| 2585 | /* Set default distance by route type. */ |
hasso | be61c4e | 2005-08-27 06:05:47 +0000 | [diff] [blame] | 2586 | if (!distance) |
| 2587 | distance = route_info[type].distance; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2588 | |
| 2589 | if (type == ZEBRA_ROUTE_BGP && CHECK_FLAG (flags, ZEBRA_FLAG_IBGP)) |
| 2590 | distance = 200; |
| 2591 | |
| 2592 | /* Filter bogus route. */ |
| 2593 | if (rib_bogus_ipv6 (type, p, gate, ifindex, 0)) |
| 2594 | return 0; |
| 2595 | |
| 2596 | /* Lookup route node.*/ |
| 2597 | rn = route_node_get (table, (struct prefix *) p); |
| 2598 | |
| 2599 | /* If same type of route are installed, treat it as a implicit |
| 2600 | withdraw. */ |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 2601 | RNODE_FOREACH_RIB (rn, rib) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2602 | { |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 2603 | if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED)) |
| 2604 | continue; |
| 2605 | |
hasso | ebf1ead | 2005-09-21 14:58:20 +0000 | [diff] [blame] | 2606 | if (rib->type != type) |
| 2607 | continue; |
| 2608 | if (rib->type != ZEBRA_ROUTE_CONNECT) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2609 | { |
| 2610 | same = rib; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2611 | break; |
| 2612 | } |
hasso | ebf1ead | 2005-09-21 14:58:20 +0000 | [diff] [blame] | 2613 | else if ((nexthop = rib->nexthop) && |
| 2614 | nexthop->type == NEXTHOP_TYPE_IFINDEX && |
| 2615 | nexthop->ifindex == ifindex) |
| 2616 | { |
| 2617 | rib->refcnt++; |
| 2618 | return 0; |
| 2619 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2620 | } |
| 2621 | |
| 2622 | /* Allocate new rib structure. */ |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 2623 | rib = XCALLOC (MTYPE_RIB, sizeof (struct rib)); |
| 2624 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2625 | rib->type = type; |
| 2626 | rib->distance = distance; |
| 2627 | rib->flags = flags; |
| 2628 | rib->metric = metric; |
paul | b5f4502 | 2003-11-02 07:28:05 +0000 | [diff] [blame] | 2629 | rib->table = vrf_id; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2630 | rib->nexthop_num = 0; |
| 2631 | rib->uptime = time (NULL); |
| 2632 | |
| 2633 | /* Nexthop settings. */ |
| 2634 | if (gate) |
| 2635 | { |
| 2636 | if (ifindex) |
| 2637 | nexthop_ipv6_ifindex_add (rib, gate, ifindex); |
| 2638 | else |
| 2639 | nexthop_ipv6_add (rib, gate); |
| 2640 | } |
| 2641 | else |
| 2642 | nexthop_ifindex_add (rib, ifindex); |
| 2643 | |
| 2644 | /* If this route is kernel route, set FIB flag to the route. */ |
| 2645 | if (type == ZEBRA_ROUTE_KERNEL || type == ZEBRA_ROUTE_CONNECT) |
| 2646 | for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next) |
| 2647 | SET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB); |
| 2648 | |
| 2649 | /* Link new rib to node.*/ |
| 2650 | rib_addnode (rn, rib); |
| 2651 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2652 | /* Free implicit route.*/ |
| 2653 | if (same) |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 2654 | rib_delnode (rn, same); |
| 2655 | |
| 2656 | route_unlock_node (rn); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2657 | return 0; |
| 2658 | } |
| 2659 | |
hasso | ebf1ead | 2005-09-21 14:58:20 +0000 | [diff] [blame] | 2660 | /* XXX factor with rib_delete_ipv6 */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2661 | int |
| 2662 | rib_delete_ipv6 (int type, int flags, struct prefix_ipv6 *p, |
G.Balaji | f768f36 | 2011-11-26 22:10:39 +0400 | [diff] [blame] | 2663 | struct in6_addr *gate, unsigned int ifindex, u_int32_t vrf_id, safi_t safi) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2664 | { |
| 2665 | struct route_table *table; |
| 2666 | struct route_node *rn; |
| 2667 | struct rib *rib; |
| 2668 | struct rib *fib = NULL; |
| 2669 | struct rib *same = NULL; |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 2670 | struct nexthop *nexthop, *tnexthop; |
| 2671 | int recursing; |
Stephen Hemminger | 81cce01 | 2009-04-28 14:28:00 -0700 | [diff] [blame] | 2672 | char buf1[INET6_ADDRSTRLEN]; |
| 2673 | char buf2[INET6_ADDRSTRLEN]; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2674 | |
| 2675 | /* Apply mask. */ |
| 2676 | apply_mask_ipv6 (p); |
| 2677 | |
| 2678 | /* Lookup table. */ |
G.Balaji | f768f36 | 2011-11-26 22:10:39 +0400 | [diff] [blame] | 2679 | table = vrf_table (AFI_IP6, safi, 0); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2680 | if (! table) |
| 2681 | return 0; |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 2682 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2683 | /* Lookup route node. */ |
| 2684 | rn = route_node_lookup (table, (struct prefix *) p); |
| 2685 | if (! rn) |
| 2686 | { |
| 2687 | if (IS_ZEBRA_DEBUG_KERNEL) |
| 2688 | { |
| 2689 | if (gate) |
ajs | b617800 | 2004-12-07 21:12:56 +0000 | [diff] [blame] | 2690 | zlog_debug ("route %s/%d via %s ifindex %d doesn't exist in rib", |
Stephen Hemminger | 81cce01 | 2009-04-28 14:28:00 -0700 | [diff] [blame] | 2691 | inet_ntop (AF_INET6, &p->prefix, buf1, INET6_ADDRSTRLEN), |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2692 | p->prefixlen, |
Stephen Hemminger | 81cce01 | 2009-04-28 14:28:00 -0700 | [diff] [blame] | 2693 | inet_ntop (AF_INET6, gate, buf2, INET6_ADDRSTRLEN), |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2694 | ifindex); |
| 2695 | else |
ajs | b617800 | 2004-12-07 21:12:56 +0000 | [diff] [blame] | 2696 | zlog_debug ("route %s/%d ifindex %d doesn't exist in rib", |
Stephen Hemminger | 81cce01 | 2009-04-28 14:28:00 -0700 | [diff] [blame] | 2697 | inet_ntop (AF_INET6, &p->prefix, buf1, INET6_ADDRSTRLEN), |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2698 | p->prefixlen, |
| 2699 | ifindex); |
| 2700 | } |
| 2701 | return ZEBRA_ERR_RTNOEXIST; |
| 2702 | } |
| 2703 | |
| 2704 | /* Lookup same type route. */ |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 2705 | RNODE_FOREACH_RIB (rn, rib) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2706 | { |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 2707 | if (CHECK_FLAG(rib->status, RIB_ENTRY_REMOVED)) |
| 2708 | continue; |
| 2709 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2710 | if (CHECK_FLAG (rib->flags, ZEBRA_FLAG_SELECTED)) |
| 2711 | fib = rib; |
| 2712 | |
hasso | ebf1ead | 2005-09-21 14:58:20 +0000 | [diff] [blame] | 2713 | if (rib->type != type) |
| 2714 | continue; |
| 2715 | if (rib->type == ZEBRA_ROUTE_CONNECT && (nexthop = rib->nexthop) && |
Matthias Ferdinand | 4f1735f | 2011-12-26 16:35:30 +0400 | [diff] [blame] | 2716 | nexthop->type == NEXTHOP_TYPE_IFINDEX) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2717 | { |
Matthias Ferdinand | 4f1735f | 2011-12-26 16:35:30 +0400 | [diff] [blame] | 2718 | if (nexthop->ifindex != ifindex) |
| 2719 | continue; |
hasso | ebf1ead | 2005-09-21 14:58:20 +0000 | [diff] [blame] | 2720 | if (rib->refcnt) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2721 | { |
hasso | ebf1ead | 2005-09-21 14:58:20 +0000 | [diff] [blame] | 2722 | rib->refcnt--; |
| 2723 | route_unlock_node (rn); |
| 2724 | route_unlock_node (rn); |
| 2725 | return 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2726 | } |
hasso | ebf1ead | 2005-09-21 14:58:20 +0000 | [diff] [blame] | 2727 | same = rib; |
| 2728 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2729 | } |
hasso | ebf1ead | 2005-09-21 14:58:20 +0000 | [diff] [blame] | 2730 | /* Make sure that the route found has the same gateway. */ |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 2731 | else |
| 2732 | { |
| 2733 | if (gate == NULL) |
| 2734 | { |
| 2735 | same = rib; |
| 2736 | break; |
| 2737 | } |
| 2738 | for (ALL_NEXTHOPS_RO(rib->nexthop, nexthop, tnexthop, recursing)) |
| 2739 | if (IPV6_ADDR_SAME (&nexthop->gate.ipv6, gate)) |
| 2740 | { |
| 2741 | same = rib; |
| 2742 | break; |
| 2743 | } |
| 2744 | if (same) |
| 2745 | break; |
| 2746 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2747 | } |
| 2748 | |
| 2749 | /* If same type of route can't be found and this message is from |
| 2750 | kernel. */ |
| 2751 | if (! same) |
| 2752 | { |
| 2753 | if (fib && type == ZEBRA_ROUTE_KERNEL) |
| 2754 | { |
| 2755 | /* Unset flags. */ |
| 2756 | for (nexthop = fib->nexthop; nexthop; nexthop = nexthop->next) |
| 2757 | UNSET_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB); |
| 2758 | |
| 2759 | UNSET_FLAG (fib->flags, ZEBRA_FLAG_SELECTED); |
| 2760 | } |
| 2761 | else |
| 2762 | { |
| 2763 | if (IS_ZEBRA_DEBUG_KERNEL) |
| 2764 | { |
| 2765 | if (gate) |
ajs | b617800 | 2004-12-07 21:12:56 +0000 | [diff] [blame] | 2766 | zlog_debug ("route %s/%d via %s ifindex %d type %d doesn't exist in rib", |
Stephen Hemminger | 81cce01 | 2009-04-28 14:28:00 -0700 | [diff] [blame] | 2767 | inet_ntop (AF_INET6, &p->prefix, buf1, INET6_ADDRSTRLEN), |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2768 | p->prefixlen, |
Stephen Hemminger | 81cce01 | 2009-04-28 14:28:00 -0700 | [diff] [blame] | 2769 | inet_ntop (AF_INET6, gate, buf2, INET6_ADDRSTRLEN), |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2770 | ifindex, |
| 2771 | type); |
| 2772 | else |
ajs | b617800 | 2004-12-07 21:12:56 +0000 | [diff] [blame] | 2773 | zlog_debug ("route %s/%d ifindex %d type %d doesn't exist in rib", |
Stephen Hemminger | 81cce01 | 2009-04-28 14:28:00 -0700 | [diff] [blame] | 2774 | inet_ntop (AF_INET6, &p->prefix, buf1, INET6_ADDRSTRLEN), |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2775 | p->prefixlen, |
| 2776 | ifindex, |
| 2777 | type); |
| 2778 | } |
| 2779 | route_unlock_node (rn); |
| 2780 | return ZEBRA_ERR_RTNOEXIST; |
| 2781 | } |
| 2782 | } |
| 2783 | |
| 2784 | if (same) |
| 2785 | rib_delnode (rn, same); |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 2786 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2787 | route_unlock_node (rn); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2788 | return 0; |
| 2789 | } |
| 2790 | |
| 2791 | /* Install static route into rib. */ |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 2792 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2793 | static_install_ipv6 (struct prefix *p, struct static_ipv6 *si) |
| 2794 | { |
| 2795 | struct rib *rib; |
| 2796 | struct route_table *table; |
| 2797 | struct route_node *rn; |
| 2798 | |
| 2799 | /* Lookup table. */ |
| 2800 | table = vrf_table (AFI_IP6, SAFI_UNICAST, 0); |
| 2801 | if (! table) |
| 2802 | return; |
| 2803 | |
| 2804 | /* Lookup existing route */ |
| 2805 | rn = route_node_get (table, p); |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 2806 | RNODE_FOREACH_RIB (rn, rib) |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 2807 | { |
| 2808 | if (CHECK_FLAG(rib->status, RIB_ENTRY_REMOVED)) |
| 2809 | continue; |
| 2810 | |
| 2811 | if (rib->type == ZEBRA_ROUTE_STATIC && rib->distance == si->distance) |
| 2812 | break; |
| 2813 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2814 | |
| 2815 | if (rib) |
| 2816 | { |
| 2817 | /* Same distance static route is there. Update it with new |
| 2818 | nexthop. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2819 | route_unlock_node (rn); |
| 2820 | |
| 2821 | switch (si->type) |
| 2822 | { |
| 2823 | case STATIC_IPV6_GATEWAY: |
| 2824 | nexthop_ipv6_add (rib, &si->ipv6); |
| 2825 | break; |
| 2826 | case STATIC_IPV6_IFNAME: |
| 2827 | nexthop_ifname_add (rib, si->ifname); |
| 2828 | break; |
| 2829 | case STATIC_IPV6_GATEWAY_IFNAME: |
| 2830 | nexthop_ipv6_ifname_add (rib, &si->ipv6, si->ifname); |
| 2831 | break; |
| 2832 | } |
Paul Jakma | 3c0755d | 2006-12-08 00:53:14 +0000 | [diff] [blame] | 2833 | rib_queue_add (&zebrad, rn); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2834 | } |
| 2835 | else |
| 2836 | { |
| 2837 | /* This is new static route. */ |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 2838 | rib = XCALLOC (MTYPE_RIB, sizeof (struct rib)); |
| 2839 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2840 | rib->type = ZEBRA_ROUTE_STATIC; |
| 2841 | rib->distance = si->distance; |
| 2842 | rib->metric = 0; |
| 2843 | rib->nexthop_num = 0; |
| 2844 | |
| 2845 | switch (si->type) |
| 2846 | { |
| 2847 | case STATIC_IPV6_GATEWAY: |
| 2848 | nexthop_ipv6_add (rib, &si->ipv6); |
| 2849 | break; |
| 2850 | case STATIC_IPV6_IFNAME: |
| 2851 | nexthop_ifname_add (rib, si->ifname); |
| 2852 | break; |
| 2853 | case STATIC_IPV6_GATEWAY_IFNAME: |
| 2854 | nexthop_ipv6_ifname_add (rib, &si->ipv6, si->ifname); |
| 2855 | break; |
| 2856 | } |
| 2857 | |
hasso | 81dfcaa | 2003-05-25 19:21:25 +0000 | [diff] [blame] | 2858 | /* Save the flags of this static routes (reject, blackhole) */ |
| 2859 | rib->flags = si->flags; |
| 2860 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2861 | /* Link this rib to the tree. */ |
| 2862 | rib_addnode (rn, rib); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2863 | } |
| 2864 | } |
| 2865 | |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 2866 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2867 | static_ipv6_nexthop_same (struct nexthop *nexthop, struct static_ipv6 *si) |
| 2868 | { |
| 2869 | if (nexthop->type == NEXTHOP_TYPE_IPV6 |
| 2870 | && si->type == STATIC_IPV6_GATEWAY |
| 2871 | && IPV6_ADDR_SAME (&nexthop->gate.ipv6, &si->ipv6)) |
| 2872 | return 1; |
| 2873 | if (nexthop->type == NEXTHOP_TYPE_IFNAME |
| 2874 | && si->type == STATIC_IPV6_IFNAME |
| 2875 | && strcmp (nexthop->ifname, si->ifname) == 0) |
| 2876 | return 1; |
| 2877 | if (nexthop->type == NEXTHOP_TYPE_IPV6_IFNAME |
| 2878 | && si->type == STATIC_IPV6_GATEWAY_IFNAME |
| 2879 | && IPV6_ADDR_SAME (&nexthop->gate.ipv6, &si->ipv6) |
| 2880 | && strcmp (nexthop->ifname, si->ifname) == 0) |
| 2881 | return 1; |
paul | e8e1946 | 2006-01-19 20:16:55 +0000 | [diff] [blame] | 2882 | return 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2883 | } |
| 2884 | |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 2885 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2886 | static_uninstall_ipv6 (struct prefix *p, struct static_ipv6 *si) |
| 2887 | { |
| 2888 | struct route_table *table; |
| 2889 | struct route_node *rn; |
| 2890 | struct rib *rib; |
| 2891 | struct nexthop *nexthop; |
| 2892 | |
| 2893 | /* Lookup table. */ |
| 2894 | table = vrf_table (AFI_IP6, SAFI_UNICAST, 0); |
| 2895 | if (! table) |
| 2896 | return; |
| 2897 | |
| 2898 | /* Lookup existing route with type and distance. */ |
| 2899 | rn = route_node_lookup (table, (struct prefix *) p); |
| 2900 | if (! rn) |
| 2901 | return; |
| 2902 | |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 2903 | RNODE_FOREACH_RIB (rn, rib) |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 2904 | { |
| 2905 | if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED)) |
| 2906 | continue; |
| 2907 | |
| 2908 | if (rib->type == ZEBRA_ROUTE_STATIC && rib->distance == si->distance) |
| 2909 | break; |
| 2910 | } |
| 2911 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2912 | if (! rib) |
| 2913 | { |
| 2914 | route_unlock_node (rn); |
| 2915 | return; |
| 2916 | } |
| 2917 | |
| 2918 | /* Lookup nexthop. */ |
| 2919 | for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next) |
| 2920 | if (static_ipv6_nexthop_same (nexthop, si)) |
| 2921 | break; |
| 2922 | |
| 2923 | /* Can't find nexthop. */ |
| 2924 | if (! nexthop) |
| 2925 | { |
| 2926 | route_unlock_node (rn); |
| 2927 | return; |
| 2928 | } |
| 2929 | |
| 2930 | /* Check nexthop. */ |
| 2931 | if (rib->nexthop_num == 1) |
| 2932 | { |
| 2933 | rib_delnode (rn, rib); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2934 | } |
| 2935 | else |
| 2936 | { |
paul | 6baeb98 | 2003-10-28 03:47:15 +0000 | [diff] [blame] | 2937 | if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB)) |
| 2938 | rib_uninstall (rn, rib); |
paul | 319572c | 2005-09-21 12:30:08 +0000 | [diff] [blame] | 2939 | nexthop_delete (rib, nexthop); |
| 2940 | nexthop_free (nexthop); |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 2941 | rib_queue_add (&zebrad, rn); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2942 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2943 | /* Unlock node. */ |
| 2944 | route_unlock_node (rn); |
| 2945 | } |
| 2946 | |
| 2947 | /* Add static route into static route configuration. */ |
| 2948 | int |
| 2949 | static_add_ipv6 (struct prefix *p, u_char type, struct in6_addr *gate, |
hasso | 39db97e | 2004-10-12 20:50:58 +0000 | [diff] [blame] | 2950 | const char *ifname, u_char flags, u_char distance, |
| 2951 | u_int32_t vrf_id) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2952 | { |
| 2953 | struct route_node *rn; |
| 2954 | struct static_ipv6 *si; |
| 2955 | struct static_ipv6 *pp; |
| 2956 | struct static_ipv6 *cp; |
| 2957 | struct route_table *stable; |
| 2958 | |
| 2959 | /* Lookup table. */ |
| 2960 | stable = vrf_static_table (AFI_IP6, SAFI_UNICAST, vrf_id); |
| 2961 | if (! stable) |
| 2962 | return -1; |
Paul Jakma | 27b4725 | 2006-07-02 16:38:54 +0000 | [diff] [blame] | 2963 | |
| 2964 | if (!gate && |
| 2965 | (type == STATIC_IPV6_GATEWAY || type == STATIC_IPV6_GATEWAY_IFNAME)) |
| 2966 | return -1; |
| 2967 | |
| 2968 | if (!ifname && |
| 2969 | (type == STATIC_IPV6_GATEWAY_IFNAME || type == STATIC_IPV6_IFNAME)) |
| 2970 | return -1; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2971 | |
| 2972 | /* Lookup static route prefix. */ |
| 2973 | rn = route_node_get (stable, p); |
| 2974 | |
| 2975 | /* Do nothing if there is a same static route. */ |
| 2976 | for (si = rn->info; si; si = si->next) |
| 2977 | { |
| 2978 | if (distance == si->distance |
| 2979 | && type == si->type |
| 2980 | && (! gate || IPV6_ADDR_SAME (gate, &si->ipv6)) |
| 2981 | && (! ifname || strcmp (ifname, si->ifname) == 0)) |
| 2982 | { |
| 2983 | route_unlock_node (rn); |
| 2984 | return 0; |
| 2985 | } |
| 2986 | } |
| 2987 | |
| 2988 | /* Make new static route structure. */ |
Stephen Hemminger | 393deb9 | 2008-08-18 14:13:29 -0700 | [diff] [blame] | 2989 | si = XCALLOC (MTYPE_STATIC_IPV6, sizeof (struct static_ipv6)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2990 | |
| 2991 | si->type = type; |
| 2992 | si->distance = distance; |
hasso | 81dfcaa | 2003-05-25 19:21:25 +0000 | [diff] [blame] | 2993 | si->flags = flags; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2994 | |
| 2995 | switch (type) |
| 2996 | { |
| 2997 | case STATIC_IPV6_GATEWAY: |
| 2998 | si->ipv6 = *gate; |
| 2999 | break; |
| 3000 | case STATIC_IPV6_IFNAME: |
| 3001 | si->ifname = XSTRDUP (0, ifname); |
| 3002 | break; |
| 3003 | case STATIC_IPV6_GATEWAY_IFNAME: |
| 3004 | si->ipv6 = *gate; |
| 3005 | si->ifname = XSTRDUP (0, ifname); |
| 3006 | break; |
| 3007 | } |
| 3008 | |
| 3009 | /* Add new static route information to the tree with sort by |
| 3010 | distance value and gateway address. */ |
| 3011 | for (pp = NULL, cp = rn->info; cp; pp = cp, cp = cp->next) |
| 3012 | { |
| 3013 | if (si->distance < cp->distance) |
| 3014 | break; |
| 3015 | if (si->distance > cp->distance) |
| 3016 | continue; |
| 3017 | } |
| 3018 | |
| 3019 | /* Make linked list. */ |
| 3020 | if (pp) |
| 3021 | pp->next = si; |
| 3022 | else |
| 3023 | rn->info = si; |
| 3024 | if (cp) |
| 3025 | cp->prev = si; |
| 3026 | si->prev = pp; |
| 3027 | si->next = cp; |
| 3028 | |
| 3029 | /* Install into rib. */ |
| 3030 | static_install_ipv6 (p, si); |
| 3031 | |
| 3032 | return 1; |
| 3033 | } |
| 3034 | |
| 3035 | /* Delete static route from static route configuration. */ |
| 3036 | int |
| 3037 | static_delete_ipv6 (struct prefix *p, u_char type, struct in6_addr *gate, |
hasso | 39db97e | 2004-10-12 20:50:58 +0000 | [diff] [blame] | 3038 | const char *ifname, u_char distance, u_int32_t vrf_id) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3039 | { |
| 3040 | struct route_node *rn; |
| 3041 | struct static_ipv6 *si; |
| 3042 | struct route_table *stable; |
| 3043 | |
| 3044 | /* Lookup table. */ |
| 3045 | stable = vrf_static_table (AFI_IP6, SAFI_UNICAST, vrf_id); |
| 3046 | if (! stable) |
| 3047 | return -1; |
| 3048 | |
| 3049 | /* Lookup static route prefix. */ |
| 3050 | rn = route_node_lookup (stable, p); |
| 3051 | if (! rn) |
| 3052 | return 0; |
| 3053 | |
| 3054 | /* Find same static route is the tree */ |
| 3055 | for (si = rn->info; si; si = si->next) |
| 3056 | if (distance == si->distance |
| 3057 | && type == si->type |
| 3058 | && (! gate || IPV6_ADDR_SAME (gate, &si->ipv6)) |
| 3059 | && (! ifname || strcmp (ifname, si->ifname) == 0)) |
| 3060 | break; |
| 3061 | |
| 3062 | /* Can't find static route. */ |
| 3063 | if (! si) |
| 3064 | { |
| 3065 | route_unlock_node (rn); |
| 3066 | return 0; |
| 3067 | } |
| 3068 | |
| 3069 | /* Install into rib. */ |
| 3070 | static_uninstall_ipv6 (p, si); |
| 3071 | |
| 3072 | /* Unlink static route from linked list. */ |
| 3073 | if (si->prev) |
| 3074 | si->prev->next = si->next; |
| 3075 | else |
| 3076 | rn->info = si->next; |
| 3077 | if (si->next) |
| 3078 | si->next->prev = si->prev; |
| 3079 | |
| 3080 | /* Free static route configuration. */ |
paul | a0f6acd | 2003-05-14 18:29:13 +0000 | [diff] [blame] | 3081 | if (ifname) |
| 3082 | XFREE (0, si->ifname); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3083 | XFREE (MTYPE_STATIC_IPV6, si); |
| 3084 | |
| 3085 | return 1; |
| 3086 | } |
| 3087 | #endif /* HAVE_IPV6 */ |
| 3088 | |
| 3089 | /* RIB update function. */ |
| 3090 | void |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 3091 | rib_update (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3092 | { |
| 3093 | struct route_node *rn; |
| 3094 | struct route_table *table; |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 3095 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3096 | table = vrf_table (AFI_IP, SAFI_UNICAST, 0); |
| 3097 | if (table) |
| 3098 | for (rn = route_top (table); rn; rn = route_next (rn)) |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 3099 | if (rnode_to_ribs (rn)) |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 3100 | rib_queue_add (&zebrad, rn); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3101 | |
| 3102 | table = vrf_table (AFI_IP6, SAFI_UNICAST, 0); |
| 3103 | if (table) |
| 3104 | for (rn = route_top (table); rn; rn = route_next (rn)) |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 3105 | if (rnode_to_ribs (rn)) |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 3106 | rib_queue_add (&zebrad, rn); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3107 | } |
| 3108 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3109 | |
| 3110 | /* Remove all routes which comes from non main table. */ |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 3111 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3112 | rib_weed_table (struct route_table *table) |
| 3113 | { |
| 3114 | struct route_node *rn; |
| 3115 | struct rib *rib; |
| 3116 | struct rib *next; |
| 3117 | |
| 3118 | if (table) |
| 3119 | for (rn = route_top (table); rn; rn = route_next (rn)) |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 3120 | RNODE_FOREACH_RIB_SAFE (rn, rib, next) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3121 | { |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 3122 | if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED)) |
| 3123 | continue; |
| 3124 | |
paul | b21b19c | 2003-06-15 01:28:29 +0000 | [diff] [blame] | 3125 | if (rib->table != zebrad.rtm_table_default && |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3126 | rib->table != RT_TABLE_MAIN) |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 3127 | rib_delnode (rn, rib); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3128 | } |
| 3129 | } |
| 3130 | |
| 3131 | /* Delete all routes from non main table. */ |
| 3132 | void |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 3133 | rib_weed_tables (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3134 | { |
| 3135 | rib_weed_table (vrf_table (AFI_IP, SAFI_UNICAST, 0)); |
| 3136 | rib_weed_table (vrf_table (AFI_IP6, SAFI_UNICAST, 0)); |
| 3137 | } |
| 3138 | |
| 3139 | /* Delete self installed routes after zebra is relaunched. */ |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 3140 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3141 | rib_sweep_table (struct route_table *table) |
| 3142 | { |
| 3143 | struct route_node *rn; |
| 3144 | struct rib *rib; |
| 3145 | struct rib *next; |
| 3146 | int ret = 0; |
| 3147 | |
| 3148 | if (table) |
| 3149 | for (rn = route_top (table); rn; rn = route_next (rn)) |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 3150 | RNODE_FOREACH_RIB_SAFE (rn, rib, next) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3151 | { |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 3152 | if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED)) |
| 3153 | continue; |
| 3154 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3155 | if (rib->type == ZEBRA_ROUTE_KERNEL && |
| 3156 | CHECK_FLAG (rib->flags, ZEBRA_FLAG_SELFROUTE)) |
| 3157 | { |
| 3158 | ret = rib_uninstall_kernel (rn, rib); |
| 3159 | if (! ret) |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 3160 | rib_delnode (rn, rib); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3161 | } |
| 3162 | } |
| 3163 | } |
| 3164 | |
| 3165 | /* Sweep all RIB tables. */ |
| 3166 | void |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 3167 | rib_sweep_route (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3168 | { |
| 3169 | rib_sweep_table (vrf_table (AFI_IP, SAFI_UNICAST, 0)); |
| 3170 | rib_sweep_table (vrf_table (AFI_IP6, SAFI_UNICAST, 0)); |
| 3171 | } |
Vyacheslav Trushkin | 2ea1ab1 | 2011-12-11 18:48:47 +0400 | [diff] [blame] | 3172 | |
| 3173 | /* Remove specific by protocol routes from 'table'. */ |
| 3174 | static unsigned long |
| 3175 | rib_score_proto_table (u_char proto, struct route_table *table) |
| 3176 | { |
| 3177 | struct route_node *rn; |
| 3178 | struct rib *rib; |
| 3179 | struct rib *next; |
| 3180 | unsigned long n = 0; |
| 3181 | |
| 3182 | if (table) |
| 3183 | for (rn = route_top (table); rn; rn = route_next (rn)) |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 3184 | RNODE_FOREACH_RIB_SAFE (rn, rib, next) |
Vyacheslav Trushkin | 2ea1ab1 | 2011-12-11 18:48:47 +0400 | [diff] [blame] | 3185 | { |
Vyacheslav Trushkin | 2ea1ab1 | 2011-12-11 18:48:47 +0400 | [diff] [blame] | 3186 | if (CHECK_FLAG (rib->status, RIB_ENTRY_REMOVED)) |
| 3187 | continue; |
| 3188 | if (rib->type == proto) |
| 3189 | { |
| 3190 | rib_delnode (rn, rib); |
| 3191 | n++; |
| 3192 | } |
| 3193 | } |
| 3194 | |
| 3195 | return n; |
| 3196 | } |
| 3197 | |
| 3198 | /* Remove specific by protocol routes. */ |
| 3199 | unsigned long |
| 3200 | rib_score_proto (u_char proto) |
| 3201 | { |
| 3202 | return rib_score_proto_table (proto, vrf_table (AFI_IP, SAFI_UNICAST, 0)) |
| 3203 | +rib_score_proto_table (proto, vrf_table (AFI_IP6, SAFI_UNICAST, 0)); |
| 3204 | } |
| 3205 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3206 | /* Close RIB and clean up kernel routes. */ |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 3207 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3208 | rib_close_table (struct route_table *table) |
| 3209 | { |
| 3210 | struct route_node *rn; |
| 3211 | struct rib *rib; |
| 3212 | |
| 3213 | if (table) |
| 3214 | for (rn = route_top (table); rn; rn = route_next (rn)) |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 3215 | RNODE_FOREACH_RIB (rn, rib) |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 3216 | { |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 3217 | if (!CHECK_FLAG (rib->flags, ZEBRA_FLAG_SELECTED)) |
| 3218 | continue; |
| 3219 | |
Avneesh Sachdev | 5adc252 | 2012-11-13 22:48:59 +0000 | [diff] [blame] | 3220 | zfpm_trigger_update (rn, NULL); |
| 3221 | |
Avneesh Sachdev | 9fd92e3 | 2012-11-13 22:48:53 +0000 | [diff] [blame] | 3222 | if (! RIB_SYSTEM_ROUTE (rib)) |
| 3223 | rib_uninstall_kernel (rn, rib); |
Paul Jakma | 6d69112 | 2006-07-27 21:49:00 +0000 | [diff] [blame] | 3224 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3225 | } |
| 3226 | |
| 3227 | /* Close all RIB tables. */ |
| 3228 | void |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 3229 | rib_close (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3230 | { |
| 3231 | rib_close_table (vrf_table (AFI_IP, SAFI_UNICAST, 0)); |
| 3232 | rib_close_table (vrf_table (AFI_IP6, SAFI_UNICAST, 0)); |
| 3233 | } |
| 3234 | |
| 3235 | /* Routing information base initialize. */ |
| 3236 | void |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 3237 | rib_init (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3238 | { |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 3239 | rib_queue_init (&zebrad); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3240 | /* VRF initialization. */ |
| 3241 | vrf_init (); |
| 3242 | } |
Avneesh Sachdev | 0915bb0 | 2012-11-13 22:48:55 +0000 | [diff] [blame] | 3243 | |
| 3244 | /* |
| 3245 | * vrf_id_get_next |
| 3246 | * |
| 3247 | * Get the first vrf id that is greater than the given vrf id if any. |
| 3248 | * |
| 3249 | * Returns TRUE if a vrf id was found, FALSE otherwise. |
| 3250 | */ |
| 3251 | static inline int |
| 3252 | vrf_id_get_next (uint32_t id, uint32_t *next_id_p) |
| 3253 | { |
| 3254 | while (++id < vector_active (vrf_vector)) |
| 3255 | { |
| 3256 | if (vrf_lookup (id)) |
| 3257 | { |
| 3258 | *next_id_p = id; |
| 3259 | return 1; |
| 3260 | } |
| 3261 | } |
| 3262 | |
| 3263 | return 0; |
| 3264 | } |
| 3265 | |
| 3266 | /* |
| 3267 | * rib_tables_iter_next |
| 3268 | * |
| 3269 | * Returns the next table in the iteration. |
| 3270 | */ |
| 3271 | struct route_table * |
| 3272 | rib_tables_iter_next (rib_tables_iter_t *iter) |
| 3273 | { |
| 3274 | struct route_table *table; |
| 3275 | |
| 3276 | /* |
| 3277 | * Array that helps us go over all AFI/SAFI combinations via one |
| 3278 | * index. |
| 3279 | */ |
| 3280 | static struct { |
| 3281 | afi_t afi; |
| 3282 | safi_t safi; |
| 3283 | } afi_safis[] = { |
| 3284 | { AFI_IP, SAFI_UNICAST }, |
| 3285 | { AFI_IP, SAFI_MULTICAST }, |
| 3286 | { AFI_IP6, SAFI_UNICAST }, |
| 3287 | { AFI_IP6, SAFI_MULTICAST }, |
| 3288 | }; |
| 3289 | |
| 3290 | table = NULL; |
| 3291 | |
| 3292 | switch (iter->state) |
| 3293 | { |
| 3294 | |
| 3295 | case RIB_TABLES_ITER_S_INIT: |
| 3296 | iter->vrf_id = 0; |
| 3297 | iter->afi_safi_ix = -1; |
| 3298 | |
| 3299 | /* Fall through */ |
| 3300 | |
| 3301 | case RIB_TABLES_ITER_S_ITERATING: |
| 3302 | iter->afi_safi_ix++; |
| 3303 | while (1) |
| 3304 | { |
| 3305 | |
| 3306 | while (iter->afi_safi_ix < (int) ZEBRA_NUM_OF (afi_safis)) |
| 3307 | { |
| 3308 | table = vrf_table (afi_safis[iter->afi_safi_ix].afi, |
| 3309 | afi_safis[iter->afi_safi_ix].safi, |
| 3310 | iter->vrf_id); |
| 3311 | if (table) |
| 3312 | break; |
| 3313 | |
| 3314 | iter->afi_safi_ix++; |
| 3315 | } |
| 3316 | |
| 3317 | /* |
| 3318 | * Found another table in this vrf. |
| 3319 | */ |
| 3320 | if (table) |
| 3321 | break; |
| 3322 | |
| 3323 | /* |
| 3324 | * Done with all tables in the current vrf, go to the next |
| 3325 | * one. |
| 3326 | */ |
| 3327 | if (!vrf_id_get_next (iter->vrf_id, &iter->vrf_id)) |
| 3328 | break; |
| 3329 | |
| 3330 | iter->afi_safi_ix = 0; |
| 3331 | } |
| 3332 | |
| 3333 | break; |
| 3334 | |
| 3335 | case RIB_TABLES_ITER_S_DONE: |
| 3336 | return NULL; |
| 3337 | } |
| 3338 | |
| 3339 | if (table) |
| 3340 | iter->state = RIB_TABLES_ITER_S_ITERATING; |
| 3341 | else |
| 3342 | iter->state = RIB_TABLES_ITER_S_DONE; |
| 3343 | |
| 3344 | return table; |
| 3345 | } |