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