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