paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1 | /* |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 2 | * Copyright (C) 2003 Yasuhiro Ohara |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 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 |
| 18 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 19 | * Boston, MA 02111-1307, USA. |
| 20 | */ |
| 21 | |
| 22 | #include <zebra.h> |
| 23 | |
| 24 | #include "memory.h" |
| 25 | #include "log.h" |
| 26 | #include "command.h" |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 27 | #include "prefix.h" |
| 28 | #include "table.h" |
hasso | 049207c | 2004-08-04 20:02:13 +0000 | [diff] [blame] | 29 | #include "vty.h" |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 30 | |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 31 | #include "ospf6_proto.h" |
| 32 | #include "ospf6_lsa.h" |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 33 | #include "ospf6_lsdb.h" |
hasso | 049207c | 2004-08-04 20:02:13 +0000 | [diff] [blame] | 34 | #include "ospf6d.h" |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 35 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 36 | struct ospf6_lsdb * |
hasso | 6452df0 | 2004-08-15 05:52:07 +0000 | [diff] [blame] | 37 | ospf6_lsdb_create (void *data) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 38 | { |
| 39 | struct ospf6_lsdb *lsdb; |
| 40 | |
| 41 | lsdb = XCALLOC (MTYPE_OSPF6_LSDB, sizeof (struct ospf6_lsdb)); |
| 42 | if (lsdb == NULL) |
| 43 | { |
| 44 | zlog_warn ("Can't malloc lsdb"); |
| 45 | return NULL; |
| 46 | } |
| 47 | memset (lsdb, 0, sizeof (struct ospf6_lsdb)); |
| 48 | |
hasso | 6452df0 | 2004-08-15 05:52:07 +0000 | [diff] [blame] | 49 | lsdb->data = data; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 50 | lsdb->table = route_table_init (); |
| 51 | return lsdb; |
| 52 | } |
| 53 | |
| 54 | void |
| 55 | ospf6_lsdb_delete (struct ospf6_lsdb *lsdb) |
| 56 | { |
Dinesh Dutt | a765eb9 | 2013-08-24 07:55:14 +0000 | [diff] [blame] | 57 | if (lsdb != NULL) |
| 58 | { |
| 59 | ospf6_lsdb_remove_all (lsdb); |
| 60 | route_table_finish (lsdb->table); |
| 61 | XFREE (MTYPE_OSPF6_LSDB, lsdb); |
| 62 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | static void |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 66 | ospf6_lsdb_set_key (struct prefix_ipv6 *key, void *value, int len) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 67 | { |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 68 | assert (key->prefixlen % 8 == 0); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 69 | |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 70 | memcpy ((caddr_t) &key->prefix + key->prefixlen / 8, |
| 71 | (caddr_t) value, len); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 72 | key->family = AF_INET6; |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 73 | key->prefixlen += len * 8; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Dinesh Dutt | e39d053 | 2013-08-24 07:55:36 +0000 | [diff] [blame] | 76 | #ifdef DEBUG |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 77 | static void |
| 78 | _lsdb_count_assert (struct ospf6_lsdb *lsdb) |
| 79 | { |
| 80 | struct ospf6_lsa *debug; |
paul | 0c083ee | 2004-10-10 12:54:58 +0000 | [diff] [blame] | 81 | unsigned int num = 0; |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 82 | for (debug = ospf6_lsdb_head (lsdb); debug; |
| 83 | debug = ospf6_lsdb_next (debug)) |
| 84 | num++; |
| 85 | |
| 86 | if (num == lsdb->count) |
| 87 | return; |
| 88 | |
hasso | c6487d6 | 2004-12-24 06:00:11 +0000 | [diff] [blame] | 89 | zlog_debug ("PANIC !! lsdb[%p]->count = %d, real = %d", |
hasso | 3b68735 | 2004-08-19 06:56:53 +0000 | [diff] [blame] | 90 | lsdb, lsdb->count, num); |
| 91 | for (debug = ospf6_lsdb_head (lsdb); debug; |
| 92 | debug = ospf6_lsdb_next (debug)) |
hasso | c6487d6 | 2004-12-24 06:00:11 +0000 | [diff] [blame] | 93 | zlog_debug ("%p %p %s lsdb[%p]", debug->prev, debug->next, debug->name, |
hasso | 3b68735 | 2004-08-19 06:56:53 +0000 | [diff] [blame] | 94 | debug->lsdb); |
hasso | c6487d6 | 2004-12-24 06:00:11 +0000 | [diff] [blame] | 95 | zlog_debug ("DUMP END"); |
hasso | 3b68735 | 2004-08-19 06:56:53 +0000 | [diff] [blame] | 96 | |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 97 | assert (num == lsdb->count); |
| 98 | } |
| 99 | #define ospf6_lsdb_count_assert(t) (_lsdb_count_assert (t)) |
Dinesh Dutt | e39d053 | 2013-08-24 07:55:36 +0000 | [diff] [blame] | 100 | #else /*DEBUG*/ |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 101 | #define ospf6_lsdb_count_assert(t) ((void) 0) |
Dinesh Dutt | e39d053 | 2013-08-24 07:55:36 +0000 | [diff] [blame] | 102 | #endif /*DEBUG*/ |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 103 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 104 | void |
| 105 | ospf6_lsdb_add (struct ospf6_lsa *lsa, struct ospf6_lsdb *lsdb) |
| 106 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 107 | struct prefix_ipv6 key; |
Dinesh Dutt | a765eb9 | 2013-08-24 07:55:14 +0000 | [diff] [blame] | 108 | struct route_node *current; |
| 109 | struct ospf6_lsa *old = NULL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 110 | |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 111 | memset (&key, 0, sizeof (key)); |
| 112 | ospf6_lsdb_set_key (&key, &lsa->header->type, sizeof (lsa->header->type)); |
| 113 | ospf6_lsdb_set_key (&key, &lsa->header->adv_router, |
| 114 | sizeof (lsa->header->adv_router)); |
| 115 | ospf6_lsdb_set_key (&key, &lsa->header->id, sizeof (lsa->header->id)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 116 | |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 117 | current = route_node_get (lsdb->table, (struct prefix *) &key); |
| 118 | old = current->info; |
| 119 | current->info = lsa; |
Dinesh Dutt | a765eb9 | 2013-08-24 07:55:14 +0000 | [diff] [blame] | 120 | lsa->rn = current; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 121 | ospf6_lsa_lock (lsa); |
| 122 | |
Dinesh Dutt | a765eb9 | 2013-08-24 07:55:14 +0000 | [diff] [blame] | 123 | if (!old) |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 124 | { |
Dinesh Dutt | a765eb9 | 2013-08-24 07:55:14 +0000 | [diff] [blame] | 125 | lsdb->count++; |
| 126 | |
| 127 | if (OSPF6_LSA_IS_MAXAGE (lsa)) |
| 128 | { |
| 129 | if (lsdb->hook_remove) |
| 130 | (*lsdb->hook_remove) (lsa); |
| 131 | } |
| 132 | else |
| 133 | { |
| 134 | if (lsdb->hook_add) |
| 135 | (*lsdb->hook_add) (lsa); |
| 136 | } |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 137 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 138 | else |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 139 | { |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 140 | if (OSPF6_LSA_IS_CHANGED (old, lsa)) |
| 141 | { |
| 142 | if (OSPF6_LSA_IS_MAXAGE (lsa)) |
| 143 | { |
| 144 | if (lsdb->hook_remove) |
| 145 | { |
| 146 | (*lsdb->hook_remove) (old); |
| 147 | (*lsdb->hook_remove) (lsa); |
| 148 | } |
| 149 | } |
hasso | ccb59b1 | 2004-08-25 09:10:37 +0000 | [diff] [blame] | 150 | else if (OSPF6_LSA_IS_MAXAGE (old)) |
| 151 | { |
| 152 | if (lsdb->hook_add) |
| 153 | (*lsdb->hook_add) (lsa); |
| 154 | } |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 155 | else |
| 156 | { |
| 157 | if (lsdb->hook_remove) |
| 158 | (*lsdb->hook_remove) (old); |
| 159 | if (lsdb->hook_add) |
| 160 | (*lsdb->hook_add) (lsa); |
| 161 | } |
| 162 | } |
Dinesh Dutt | a765eb9 | 2013-08-24 07:55:14 +0000 | [diff] [blame] | 163 | ospf6_lsa_unlock (old); |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 164 | } |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 165 | |
| 166 | ospf6_lsdb_count_assert (lsdb); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | void |
| 170 | ospf6_lsdb_remove (struct ospf6_lsa *lsa, struct ospf6_lsdb *lsdb) |
| 171 | { |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 172 | struct route_node *node; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 173 | struct prefix_ipv6 key; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 174 | |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 175 | memset (&key, 0, sizeof (key)); |
| 176 | ospf6_lsdb_set_key (&key, &lsa->header->type, sizeof (lsa->header->type)); |
| 177 | ospf6_lsdb_set_key (&key, &lsa->header->adv_router, |
| 178 | sizeof (lsa->header->adv_router)); |
| 179 | ospf6_lsdb_set_key (&key, &lsa->header->id, sizeof (lsa->header->id)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 180 | |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 181 | node = route_node_lookup (lsdb->table, (struct prefix *) &key); |
| 182 | assert (node && node->info == lsa); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 183 | |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 184 | node->info = NULL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 185 | lsdb->count--; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 186 | |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 187 | if (lsdb->hook_remove) |
| 188 | (*lsdb->hook_remove) (lsa); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 189 | |
Dinesh Dutt | a765eb9 | 2013-08-24 07:55:14 +0000 | [diff] [blame] | 190 | route_unlock_node (node); /* to free the lookup lock */ |
| 191 | route_unlock_node (node); /* to free the original lock */ |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 192 | ospf6_lsa_unlock (lsa); |
hasso | 6452df0 | 2004-08-15 05:52:07 +0000 | [diff] [blame] | 193 | |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 194 | ospf6_lsdb_count_assert (lsdb); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | struct ospf6_lsa * |
| 198 | ospf6_lsdb_lookup (u_int16_t type, u_int32_t id, u_int32_t adv_router, |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 199 | struct ospf6_lsdb *lsdb) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 200 | { |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 201 | struct route_node *node; |
| 202 | struct prefix_ipv6 key; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 203 | |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 204 | if (lsdb == NULL) |
| 205 | return NULL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 206 | |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 207 | memset (&key, 0, sizeof (key)); |
| 208 | ospf6_lsdb_set_key (&key, &type, sizeof (type)); |
| 209 | ospf6_lsdb_set_key (&key, &adv_router, sizeof (adv_router)); |
| 210 | ospf6_lsdb_set_key (&key, &id, sizeof (id)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 211 | |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 212 | node = route_node_lookup (lsdb->table, (struct prefix *) &key); |
| 213 | if (node == NULL || node->info == NULL) |
| 214 | return NULL; |
Dinesh Dutt | a765eb9 | 2013-08-24 07:55:14 +0000 | [diff] [blame] | 215 | |
| 216 | route_unlock_node (node); |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 217 | return (struct ospf6_lsa *) node->info; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 218 | } |
| 219 | |
hasso | 2680aa2 | 2004-11-25 20:54:46 +0000 | [diff] [blame] | 220 | struct ospf6_lsa * |
| 221 | ospf6_lsdb_lookup_next (u_int16_t type, u_int32_t id, u_int32_t adv_router, |
| 222 | struct ospf6_lsdb *lsdb) |
| 223 | { |
| 224 | struct route_node *node; |
| 225 | struct route_node *matched = NULL; |
| 226 | struct prefix_ipv6 key; |
| 227 | struct prefix *p; |
| 228 | |
| 229 | if (lsdb == NULL) |
| 230 | return NULL; |
| 231 | |
| 232 | memset (&key, 0, sizeof (key)); |
| 233 | ospf6_lsdb_set_key (&key, &type, sizeof (type)); |
| 234 | ospf6_lsdb_set_key (&key, &adv_router, sizeof (adv_router)); |
| 235 | ospf6_lsdb_set_key (&key, &id, sizeof (id)); |
| 236 | p = (struct prefix *) &key; |
| 237 | |
| 238 | { |
| 239 | char buf[64]; |
| 240 | prefix2str (p, buf, sizeof (buf)); |
hasso | c6487d6 | 2004-12-24 06:00:11 +0000 | [diff] [blame] | 241 | zlog_debug ("lsdb_lookup_next: key: %s", buf); |
hasso | 2680aa2 | 2004-11-25 20:54:46 +0000 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | node = lsdb->table->top; |
| 245 | /* walk down tree. */ |
| 246 | while (node && node->p.prefixlen <= p->prefixlen && |
| 247 | prefix_match (&node->p, p)) |
| 248 | { |
| 249 | matched = node; |
Stephen Hemminger | 1352ef3 | 2009-12-09 14:43:17 +0300 | [diff] [blame] | 250 | node = node->link[prefix_bit(&p->u.prefix, node->p.prefixlen)]; |
hasso | 2680aa2 | 2004-11-25 20:54:46 +0000 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | if (matched) |
| 254 | node = matched; |
| 255 | else |
| 256 | node = lsdb->table->top; |
| 257 | route_lock_node (node); |
| 258 | |
| 259 | /* skip to real existing entry */ |
| 260 | while (node && node->info == NULL) |
| 261 | node = route_next (node); |
| 262 | |
| 263 | if (! node) |
| 264 | return NULL; |
| 265 | |
| 266 | if (prefix_same (&node->p, p)) |
| 267 | { |
hasso | 2680aa2 | 2004-11-25 20:54:46 +0000 | [diff] [blame] | 268 | node = route_next (node); |
| 269 | while (node && node->info == NULL) |
| 270 | node = route_next (node); |
hasso | 2680aa2 | 2004-11-25 20:54:46 +0000 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | if (! node) |
| 274 | return NULL; |
| 275 | |
| 276 | route_unlock_node (node); |
| 277 | return (struct ospf6_lsa *) node->info; |
| 278 | } |
| 279 | |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 280 | /* Iteration function */ |
| 281 | struct ospf6_lsa * |
| 282 | ospf6_lsdb_head (struct ospf6_lsdb *lsdb) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 283 | { |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 284 | struct route_node *node; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 285 | |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 286 | node = route_top (lsdb->table); |
| 287 | if (node == NULL) |
| 288 | return NULL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 289 | |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 290 | /* skip to the existing lsdb entry */ |
| 291 | while (node && node->info == NULL) |
| 292 | node = route_next (node); |
| 293 | if (node == NULL) |
| 294 | return NULL; |
| 295 | |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 296 | if (node->info) |
| 297 | ospf6_lsa_lock ((struct ospf6_lsa *) node->info); |
| 298 | return (struct ospf6_lsa *) node->info; |
| 299 | } |
| 300 | |
| 301 | struct ospf6_lsa * |
| 302 | ospf6_lsdb_next (struct ospf6_lsa *lsa) |
| 303 | { |
Dinesh Dutt | a765eb9 | 2013-08-24 07:55:14 +0000 | [diff] [blame] | 304 | struct route_node *node = lsa->rn; |
| 305 | struct ospf6_lsa *next = NULL; |
| 306 | |
| 307 | do { |
| 308 | node = route_next (node); |
| 309 | } while (node && node->info == NULL); |
| 310 | |
| 311 | if ((node != NULL) && (node->info != NULL)) |
| 312 | { |
| 313 | next = node->info; |
| 314 | ospf6_lsa_lock (next); |
| 315 | } |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 316 | |
| 317 | ospf6_lsa_unlock (lsa); |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 318 | return next; |
| 319 | } |
| 320 | |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 321 | struct ospf6_lsa * |
| 322 | ospf6_lsdb_type_router_head (u_int16_t type, u_int32_t adv_router, |
| 323 | struct ospf6_lsdb *lsdb) |
| 324 | { |
| 325 | struct route_node *node; |
| 326 | struct prefix_ipv6 key; |
| 327 | struct ospf6_lsa *lsa; |
| 328 | |
| 329 | memset (&key, 0, sizeof (key)); |
| 330 | ospf6_lsdb_set_key (&key, &type, sizeof (type)); |
| 331 | ospf6_lsdb_set_key (&key, &adv_router, sizeof (adv_router)); |
| 332 | |
| 333 | node = lsdb->table->top; |
| 334 | |
| 335 | /* Walk down tree. */ |
| 336 | while (node && node->p.prefixlen <= key.prefixlen && |
| 337 | prefix_match (&node->p, (struct prefix *) &key)) |
Stephen Hemminger | 813f6a0 | 2009-12-10 16:13:09 +0300 | [diff] [blame] | 338 | node = node->link[prefix6_bit(&key.prefix, node->p.prefixlen)]; |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 339 | |
| 340 | if (node) |
| 341 | route_lock_node (node); |
| 342 | while (node && node->info == NULL) |
| 343 | node = route_next (node); |
| 344 | |
| 345 | if (node == NULL) |
| 346 | return NULL; |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 347 | |
| 348 | if (! prefix_match ((struct prefix *) &key, &node->p)) |
| 349 | return NULL; |
| 350 | |
| 351 | lsa = node->info; |
| 352 | ospf6_lsa_lock (lsa); |
| 353 | |
| 354 | return lsa; |
| 355 | } |
| 356 | |
| 357 | struct ospf6_lsa * |
| 358 | ospf6_lsdb_type_router_next (u_int16_t type, u_int32_t adv_router, |
| 359 | struct ospf6_lsa *lsa) |
| 360 | { |
Dinesh Dutt | a765eb9 | 2013-08-24 07:55:14 +0000 | [diff] [blame] | 361 | struct ospf6_lsa *next = ospf6_lsdb_next(lsa); |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 362 | |
| 363 | if (next) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 364 | { |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 365 | if (next->header->type != type || |
| 366 | next->header->adv_router != adv_router) |
Dinesh Dutt | a765eb9 | 2013-08-24 07:55:14 +0000 | [diff] [blame] | 367 | { |
| 368 | route_unlock_node (next->rn); |
| 369 | ospf6_lsa_unlock (next); |
| 370 | next = NULL; |
| 371 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 372 | } |
| 373 | |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 374 | return next; |
| 375 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 376 | |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 377 | struct ospf6_lsa * |
| 378 | ospf6_lsdb_type_head (u_int16_t type, struct ospf6_lsdb *lsdb) |
| 379 | { |
| 380 | struct route_node *node; |
| 381 | struct prefix_ipv6 key; |
| 382 | struct ospf6_lsa *lsa; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 383 | |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 384 | memset (&key, 0, sizeof (key)); |
| 385 | ospf6_lsdb_set_key (&key, &type, sizeof (type)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 386 | |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 387 | /* Walk down tree. */ |
| 388 | node = lsdb->table->top; |
| 389 | while (node && node->p.prefixlen <= key.prefixlen && |
| 390 | prefix_match (&node->p, (struct prefix *) &key)) |
Stephen Hemminger | 813f6a0 | 2009-12-10 16:13:09 +0300 | [diff] [blame] | 391 | node = node->link[prefix6_bit(&key.prefix, node->p.prefixlen)]; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 392 | |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 393 | if (node) |
| 394 | route_lock_node (node); |
| 395 | while (node && node->info == NULL) |
| 396 | node = route_next (node); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 397 | |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 398 | if (node == NULL) |
| 399 | return NULL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 400 | |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 401 | if (! prefix_match ((struct prefix *) &key, &node->p)) |
| 402 | return NULL; |
| 403 | |
| 404 | lsa = node->info; |
| 405 | ospf6_lsa_lock (lsa); |
| 406 | |
| 407 | return lsa; |
| 408 | } |
| 409 | |
| 410 | struct ospf6_lsa * |
| 411 | ospf6_lsdb_type_next (u_int16_t type, struct ospf6_lsa *lsa) |
| 412 | { |
Dinesh Dutt | a765eb9 | 2013-08-24 07:55:14 +0000 | [diff] [blame] | 413 | struct ospf6_lsa *next = ospf6_lsdb_next (lsa); |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 414 | |
| 415 | if (next) |
| 416 | { |
| 417 | if (next->header->type != type) |
Dinesh Dutt | a765eb9 | 2013-08-24 07:55:14 +0000 | [diff] [blame] | 418 | { |
| 419 | route_unlock_node (next->rn); |
| 420 | ospf6_lsa_unlock (next); |
| 421 | next = NULL; |
| 422 | } |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 423 | } |
| 424 | |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 425 | return next; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | void |
| 429 | ospf6_lsdb_remove_all (struct ospf6_lsdb *lsdb) |
| 430 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 431 | struct ospf6_lsa *lsa; |
Dinesh Dutt | a765eb9 | 2013-08-24 07:55:14 +0000 | [diff] [blame] | 432 | |
| 433 | if (lsdb == NULL) |
| 434 | return; |
| 435 | |
hasso | 508e53e | 2004-05-18 18:57:06 +0000 | [diff] [blame] | 436 | for (lsa = ospf6_lsdb_head (lsdb); lsa; lsa = ospf6_lsdb_next (lsa)) |
| 437 | ospf6_lsdb_remove (lsa, lsdb); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 438 | } |
| 439 | |
Dinesh Dutt | a765eb9 | 2013-08-24 07:55:14 +0000 | [diff] [blame] | 440 | void |
| 441 | ospf6_lsdb_lsa_unlock (struct ospf6_lsa *lsa) |
| 442 | { |
| 443 | if (lsa != NULL) |
| 444 | { |
| 445 | if (lsa->rn != NULL) |
| 446 | route_unlock_node (lsa->rn); |
| 447 | ospf6_lsa_unlock (lsa); |
| 448 | } |
| 449 | } |
| 450 | |
Dinesh Dutt | 2449fcd | 2013-08-24 07:54:17 +0000 | [diff] [blame] | 451 | int |
| 452 | ospf6_lsdb_maxage_remover (struct ospf6_lsdb *lsdb) |
| 453 | { |
| 454 | int reschedule = 0; |
| 455 | struct ospf6_lsa *lsa; |
| 456 | |
| 457 | for (lsa = ospf6_lsdb_head (lsdb); lsa; lsa = ospf6_lsdb_next (lsa)) |
| 458 | { |
| 459 | if (! OSPF6_LSA_IS_MAXAGE (lsa)) |
| 460 | continue; |
| 461 | if (lsa->retrans_count != 0) |
| 462 | { |
| 463 | reschedule = 1; |
| 464 | continue; |
| 465 | } |
| 466 | if (IS_OSPF6_DEBUG_LSA_TYPE (lsa->header->type)) |
| 467 | zlog_debug ("Remove MaxAge %s", lsa->name); |
Dinesh Dutt | 3b22028 | 2013-08-24 08:00:44 +0000 | [diff] [blame] | 468 | if (CHECK_FLAG(lsa->flag, OSPF6_LSA_SEQWRAPPED)) |
| 469 | { |
| 470 | UNSET_FLAG(lsa->flag, OSPF6_LSA_SEQWRAPPED); |
| 471 | /* |
| 472 | * lsa->header->age = 0; |
| 473 | */ |
| 474 | lsa->header->seqnum = htonl(OSPF_MAX_SEQUENCE_NUMBER + 1); |
| 475 | ospf6_lsa_checksum (lsa->header); |
David Lamparter | 11b4f01 | 2013-12-09 16:48:55 +0100 | [diff] [blame] | 476 | |
| 477 | THREAD_OFF(lsa->refresh); |
Dinesh Dutt | 3b22028 | 2013-08-24 08:00:44 +0000 | [diff] [blame] | 478 | thread_execute (master, ospf6_lsa_refresh, lsa, 0); |
| 479 | } else { |
| 480 | ospf6_lsdb_remove (lsa, lsdb); |
| 481 | } |
Dinesh Dutt | 2449fcd | 2013-08-24 07:54:17 +0000 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | return (reschedule); |
| 485 | } |
| 486 | |
hasso | 049207c | 2004-08-04 20:02:13 +0000 | [diff] [blame] | 487 | void |
Paul Jakma | 7bef33c | 2014-09-19 15:35:15 +0100 | [diff] [blame] | 488 | ospf6_lsdb_show (struct vty *vty, enum ospf_lsdb_show_level level, |
hasso | 049207c | 2004-08-04 20:02:13 +0000 | [diff] [blame] | 489 | u_int16_t *type, u_int32_t *id, u_int32_t *adv_router, |
| 490 | struct ospf6_lsdb *lsdb) |
| 491 | { |
| 492 | struct ospf6_lsa *lsa; |
| 493 | void (*showfunc) (struct vty *, struct ospf6_lsa *) = NULL; |
| 494 | |
Paul Jakma | 7bef33c | 2014-09-19 15:35:15 +0100 | [diff] [blame] | 495 | switch (level) |
| 496 | { |
| 497 | case OSPF6_LSDB_SHOW_LEVEL_DETAIL: |
| 498 | showfunc = ospf6_lsa_show; |
| 499 | break; |
| 500 | case OSPF6_LSDB_SHOW_LEVEL_INTERNAL: |
| 501 | showfunc = ospf6_lsa_show_internal; |
| 502 | break; |
| 503 | case OSPF6_LSDB_SHOW_LEVEL_DUMP: |
| 504 | showfunc = ospf6_lsa_show_dump; |
| 505 | break; |
| 506 | case OSPF6_LSDB_SHOW_LEVEL_NORMAL: |
| 507 | default: |
| 508 | showfunc = ospf6_lsa_show_summary; |
| 509 | } |
| 510 | |
hasso | 049207c | 2004-08-04 20:02:13 +0000 | [diff] [blame] | 511 | if (type && id && adv_router) |
| 512 | { |
| 513 | lsa = ospf6_lsdb_lookup (*type, *id, *adv_router, lsdb); |
| 514 | if (lsa) |
| 515 | { |
| 516 | if (level == OSPF6_LSDB_SHOW_LEVEL_NORMAL) |
| 517 | ospf6_lsa_show (vty, lsa); |
| 518 | else |
| 519 | (*showfunc) (vty, lsa); |
| 520 | } |
| 521 | return; |
| 522 | } |
| 523 | |
| 524 | if (level == OSPF6_LSDB_SHOW_LEVEL_NORMAL) |
| 525 | ospf6_lsa_show_summary_header (vty); |
| 526 | |
| 527 | if (type && adv_router) |
| 528 | lsa = ospf6_lsdb_type_router_head (*type, *adv_router, lsdb); |
| 529 | else if (type) |
| 530 | lsa = ospf6_lsdb_type_head (*type, lsdb); |
| 531 | else |
| 532 | lsa = ospf6_lsdb_head (lsdb); |
| 533 | while (lsa) |
| 534 | { |
| 535 | if ((! adv_router || lsa->header->adv_router == *adv_router) && |
| 536 | (! id || lsa->header->id == *id)) |
| 537 | (*showfunc) (vty, lsa); |
| 538 | |
| 539 | if (type && adv_router) |
| 540 | lsa = ospf6_lsdb_type_router_next (*type, *adv_router, lsa); |
| 541 | else if (type) |
| 542 | lsa = ospf6_lsdb_type_next (*type, lsa); |
| 543 | else |
| 544 | lsa = ospf6_lsdb_next (lsa); |
| 545 | } |
| 546 | } |
| 547 | |
| 548 | /* Decide new Link State ID to originate. |
| 549 | note return value is network byte order */ |
| 550 | u_int32_t |
| 551 | ospf6_new_ls_id (u_int16_t type, u_int32_t adv_router, |
| 552 | struct ospf6_lsdb *lsdb) |
| 553 | { |
| 554 | struct ospf6_lsa *lsa; |
| 555 | u_int32_t id = 1; |
| 556 | |
| 557 | for (lsa = ospf6_lsdb_type_router_head (type, adv_router, lsdb); lsa; |
| 558 | lsa = ospf6_lsdb_type_router_next (type, adv_router, lsa)) |
| 559 | { |
| 560 | if (ntohl (lsa->header->id) < id) |
| 561 | continue; |
| 562 | if (ntohl (lsa->header->id) > id) |
Tom Goff | 16c1c48 | 2009-12-01 21:12:38 +0300 | [diff] [blame] | 563 | { |
Dinesh Dutt | a765eb9 | 2013-08-24 07:55:14 +0000 | [diff] [blame] | 564 | ospf6_lsdb_lsa_unlock (lsa); |
hasso | 6452df0 | 2004-08-15 05:52:07 +0000 | [diff] [blame] | 565 | break; |
Tom Goff | 16c1c48 | 2009-12-01 21:12:38 +0300 | [diff] [blame] | 566 | } |
hasso | 049207c | 2004-08-04 20:02:13 +0000 | [diff] [blame] | 567 | id++; |
| 568 | } |
| 569 | |
| 570 | return ((u_int32_t) htonl (id)); |
| 571 | } |
| 572 | |
| 573 | /* Decide new LS sequence number to originate. |
| 574 | note return value is network byte order */ |
| 575 | u_int32_t |
| 576 | ospf6_new_ls_seqnum (u_int16_t type, u_int32_t id, u_int32_t adv_router, |
| 577 | struct ospf6_lsdb *lsdb) |
| 578 | { |
| 579 | struct ospf6_lsa *lsa; |
| 580 | signed long seqnum = 0; |
| 581 | |
| 582 | /* if current database copy not found, return InitialSequenceNumber */ |
| 583 | lsa = ospf6_lsdb_lookup (type, id, adv_router, lsdb); |
| 584 | if (lsa == NULL) |
Dinesh Dutt | 8551e6d | 2013-10-22 17:42:18 -0700 | [diff] [blame] | 585 | seqnum = OSPF_INITIAL_SEQUENCE_NUMBER; |
hasso | 049207c | 2004-08-04 20:02:13 +0000 | [diff] [blame] | 586 | else |
| 587 | seqnum = (signed long) ntohl (lsa->header->seqnum) + 1; |
| 588 | |
| 589 | return ((u_int32_t) htonl (seqnum)); |
| 590 | } |
| 591 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 592 | |