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