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