paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1 | /* |
| 2 | * OSPF Sending and Receiving OSPF Packets. |
| 3 | * Copyright (C) 1999, 2000 Toshiaki Takada |
| 4 | * |
| 5 | * This file is part of GNU Zebra. |
| 6 | * |
| 7 | * GNU Zebra is free software; you can redistribute it and/or modify it |
| 8 | * under the terms of the GNU General Public License as published by the |
| 9 | * Free Software Foundation; either version 2, or (at your option) any |
| 10 | * later version. |
| 11 | * |
| 12 | * GNU Zebra is distributed in the hope that it will be useful, but |
| 13 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with GNU Zebra; see the file COPYING. If not, write to the Free |
| 19 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
| 20 | * 02111-1307, USA. |
| 21 | */ |
| 22 | |
| 23 | #include <zebra.h> |
| 24 | |
| 25 | #include "thread.h" |
| 26 | #include "memory.h" |
| 27 | #include "linklist.h" |
| 28 | #include "prefix.h" |
| 29 | #include "if.h" |
| 30 | #include "table.h" |
| 31 | #include "sockunion.h" |
| 32 | #include "stream.h" |
| 33 | #include "log.h" |
paul | 2dd8bb4 | 2004-07-23 15:13:48 +0000 | [diff] [blame] | 34 | #include "sockopt.h" |
paul | 484315f | 2005-11-03 09:08:29 +0000 | [diff] [blame] | 35 | #include "checksum.h" |
vincent | c1a03d4 | 2005-09-28 15:47:44 +0000 | [diff] [blame] | 36 | #include "md5.h" |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 37 | |
| 38 | #include "ospfd/ospfd.h" |
| 39 | #include "ospfd/ospf_network.h" |
| 40 | #include "ospfd/ospf_interface.h" |
| 41 | #include "ospfd/ospf_ism.h" |
| 42 | #include "ospfd/ospf_asbr.h" |
| 43 | #include "ospfd/ospf_lsa.h" |
| 44 | #include "ospfd/ospf_lsdb.h" |
| 45 | #include "ospfd/ospf_neighbor.h" |
| 46 | #include "ospfd/ospf_nsm.h" |
| 47 | #include "ospfd/ospf_packet.h" |
| 48 | #include "ospfd/ospf_spf.h" |
| 49 | #include "ospfd/ospf_flood.h" |
| 50 | #include "ospfd/ospf_dump.h" |
| 51 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 52 | /* Packet Type String. */ |
Denis Ovsienko | 272ca1e | 2012-01-15 19:12:19 +0400 | [diff] [blame] | 53 | const struct message ospf_packet_type_str[] = |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 54 | { |
Denis Ovsienko | 272ca1e | 2012-01-15 19:12:19 +0400 | [diff] [blame] | 55 | { OSPF_MSG_HELLO, "Hello" }, |
| 56 | { OSPF_MSG_DB_DESC, "Database Description" }, |
| 57 | { OSPF_MSG_LS_REQ, "Link State Request" }, |
| 58 | { OSPF_MSG_LS_UPD, "Link State Update" }, |
| 59 | { OSPF_MSG_LS_ACK, "Link State Acknowledgment" }, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 60 | }; |
Denis Ovsienko | 272ca1e | 2012-01-15 19:12:19 +0400 | [diff] [blame] | 61 | const size_t ospf_packet_type_str_max = sizeof (ospf_packet_type_str) / |
| 62 | sizeof (ospf_packet_type_str[0]); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 63 | |
Denis Ovsienko | 75c8eab | 2012-01-30 15:41:39 +0400 | [diff] [blame] | 64 | /* Minimum (besides OSPF_HEADER_SIZE) lengths for OSPF packets of |
| 65 | particular types, offset is the "type" field of a packet. */ |
| 66 | static const u_int16_t ospf_packet_minlen[] = |
| 67 | { |
| 68 | 0, |
| 69 | OSPF_HELLO_MIN_SIZE, |
| 70 | OSPF_DB_DESC_MIN_SIZE, |
| 71 | OSPF_LS_REQ_MIN_SIZE, |
| 72 | OSPF_LS_UPD_MIN_SIZE, |
| 73 | OSPF_LS_ACK_MIN_SIZE, |
| 74 | }; |
| 75 | |
Denis Ovsienko | 4e31de7 | 2012-02-17 16:20:50 +0400 | [diff] [blame] | 76 | /* Minimum (besides OSPF_LSA_HEADER_SIZE) lengths for LSAs of particular |
| 77 | types, offset is the "LSA type" field. */ |
| 78 | static const u_int16_t ospf_lsa_minlen[] = |
| 79 | { |
| 80 | 0, |
| 81 | OSPF_ROUTER_LSA_MIN_SIZE, |
| 82 | OSPF_NETWORK_LSA_MIN_SIZE, |
| 83 | OSPF_SUMMARY_LSA_MIN_SIZE, |
| 84 | OSPF_SUMMARY_LSA_MIN_SIZE, |
| 85 | OSPF_AS_EXTERNAL_LSA_MIN_SIZE, |
| 86 | 0, |
| 87 | OSPF_AS_EXTERNAL_LSA_MIN_SIZE, |
| 88 | 0, |
| 89 | 0, |
| 90 | 0, |
| 91 | 0, |
| 92 | }; |
| 93 | |
Denis Ovsienko | bd5651f | 2012-02-26 17:59:43 +0400 | [diff] [blame] | 94 | /* for ospf_check_auth() */ |
| 95 | static int ospf_check_sum (struct ospf_header *); |
| 96 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 97 | /* OSPF authentication checking function */ |
paul | 4dadc29 | 2005-05-06 21:37:42 +0000 | [diff] [blame] | 98 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 99 | ospf_auth_type (struct ospf_interface *oi) |
| 100 | { |
| 101 | int auth_type; |
| 102 | |
| 103 | if (OSPF_IF_PARAM (oi, auth_type) == OSPF_AUTH_NOTSET) |
| 104 | auth_type = oi->area->auth_type; |
| 105 | else |
| 106 | auth_type = OSPF_IF_PARAM (oi, auth_type); |
| 107 | |
| 108 | /* Handle case where MD5 key list is not configured aka Cisco */ |
| 109 | if (auth_type == OSPF_AUTH_CRYPTOGRAPHIC && |
| 110 | list_isempty (OSPF_IF_PARAM (oi, auth_crypt))) |
| 111 | return OSPF_AUTH_NULL; |
| 112 | |
| 113 | return auth_type; |
| 114 | |
| 115 | } |
| 116 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 117 | struct ospf_packet * |
| 118 | ospf_packet_new (size_t size) |
| 119 | { |
| 120 | struct ospf_packet *new; |
| 121 | |
| 122 | new = XCALLOC (MTYPE_OSPF_PACKET, sizeof (struct ospf_packet)); |
| 123 | new->s = stream_new (size); |
| 124 | |
| 125 | return new; |
| 126 | } |
| 127 | |
| 128 | void |
| 129 | ospf_packet_free (struct ospf_packet *op) |
| 130 | { |
| 131 | if (op->s) |
| 132 | stream_free (op->s); |
| 133 | |
| 134 | XFREE (MTYPE_OSPF_PACKET, op); |
| 135 | |
| 136 | op = NULL; |
| 137 | } |
| 138 | |
| 139 | struct ospf_fifo * |
| 140 | ospf_fifo_new () |
| 141 | { |
| 142 | struct ospf_fifo *new; |
| 143 | |
| 144 | new = XCALLOC (MTYPE_OSPF_FIFO, sizeof (struct ospf_fifo)); |
| 145 | return new; |
| 146 | } |
| 147 | |
| 148 | /* Add new packet to fifo. */ |
| 149 | void |
| 150 | ospf_fifo_push (struct ospf_fifo *fifo, struct ospf_packet *op) |
| 151 | { |
| 152 | if (fifo->tail) |
| 153 | fifo->tail->next = op; |
| 154 | else |
| 155 | fifo->head = op; |
| 156 | |
| 157 | fifo->tail = op; |
| 158 | |
| 159 | fifo->count++; |
| 160 | } |
| 161 | |
Paul Jakma | aa276fd | 2010-01-08 17:11:15 +0000 | [diff] [blame] | 162 | /* Add new packet to head of fifo. */ |
| 163 | static void |
| 164 | ospf_fifo_push_head (struct ospf_fifo *fifo, struct ospf_packet *op) |
| 165 | { |
| 166 | op->next = fifo->head; |
| 167 | |
| 168 | if (fifo->tail == NULL) |
| 169 | fifo->tail = op; |
| 170 | |
| 171 | fifo->head = op; |
| 172 | |
| 173 | fifo->count++; |
| 174 | } |
| 175 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 176 | /* Delete first packet from fifo. */ |
| 177 | struct ospf_packet * |
| 178 | ospf_fifo_pop (struct ospf_fifo *fifo) |
| 179 | { |
| 180 | struct ospf_packet *op; |
| 181 | |
| 182 | op = fifo->head; |
| 183 | |
| 184 | if (op) |
| 185 | { |
| 186 | fifo->head = op->next; |
| 187 | |
| 188 | if (fifo->head == NULL) |
| 189 | fifo->tail = NULL; |
| 190 | |
| 191 | fifo->count--; |
| 192 | } |
| 193 | |
| 194 | return op; |
| 195 | } |
| 196 | |
| 197 | /* Return first fifo entry. */ |
| 198 | struct ospf_packet * |
| 199 | ospf_fifo_head (struct ospf_fifo *fifo) |
| 200 | { |
| 201 | return fifo->head; |
| 202 | } |
| 203 | |
| 204 | /* Flush ospf packet fifo. */ |
| 205 | void |
| 206 | ospf_fifo_flush (struct ospf_fifo *fifo) |
| 207 | { |
| 208 | struct ospf_packet *op; |
| 209 | struct ospf_packet *next; |
| 210 | |
| 211 | for (op = fifo->head; op; op = next) |
| 212 | { |
| 213 | next = op->next; |
| 214 | ospf_packet_free (op); |
| 215 | } |
| 216 | fifo->head = fifo->tail = NULL; |
| 217 | fifo->count = 0; |
| 218 | } |
| 219 | |
| 220 | /* Free ospf packet fifo. */ |
| 221 | void |
| 222 | ospf_fifo_free (struct ospf_fifo *fifo) |
| 223 | { |
| 224 | ospf_fifo_flush (fifo); |
| 225 | |
| 226 | XFREE (MTYPE_OSPF_FIFO, fifo); |
| 227 | } |
| 228 | |
| 229 | void |
| 230 | ospf_packet_add (struct ospf_interface *oi, struct ospf_packet *op) |
| 231 | { |
ajs | c3eab87 | 2005-01-29 15:52:07 +0000 | [diff] [blame] | 232 | if (!oi->obuf) |
| 233 | { |
| 234 | zlog_err("ospf_packet_add(interface %s in state %d [%s], packet type %s, " |
| 235 | "destination %s) called with NULL obuf, ignoring " |
| 236 | "(please report this bug)!\n", |
| 237 | IF_NAME(oi), oi->state, LOOKUP (ospf_ism_state_msg, oi->state), |
Denis Ovsienko | 272ca1e | 2012-01-15 19:12:19 +0400 | [diff] [blame] | 238 | LOOKUP (ospf_packet_type_str, stream_getc_from(op->s, 1)), |
ajs | c3eab87 | 2005-01-29 15:52:07 +0000 | [diff] [blame] | 239 | inet_ntoa (op->dst)); |
| 240 | return; |
| 241 | } |
| 242 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 243 | /* Add packet to end of queue. */ |
| 244 | ospf_fifo_push (oi->obuf, op); |
| 245 | |
| 246 | /* Debug of packet fifo*/ |
| 247 | /* ospf_fifo_debug (oi->obuf); */ |
| 248 | } |
| 249 | |
Paul Jakma | aa276fd | 2010-01-08 17:11:15 +0000 | [diff] [blame] | 250 | static void |
| 251 | ospf_packet_add_top (struct ospf_interface *oi, struct ospf_packet *op) |
| 252 | { |
| 253 | if (!oi->obuf) |
| 254 | { |
| 255 | zlog_err("ospf_packet_add(interface %s in state %d [%s], packet type %s, " |
| 256 | "destination %s) called with NULL obuf, ignoring " |
| 257 | "(please report this bug)!\n", |
| 258 | IF_NAME(oi), oi->state, LOOKUP (ospf_ism_state_msg, oi->state), |
Denis Ovsienko | 7e0e2cb | 2012-01-20 22:32:10 +0400 | [diff] [blame] | 259 | LOOKUP (ospf_packet_type_str, stream_getc_from(op->s, 1)), |
Paul Jakma | aa276fd | 2010-01-08 17:11:15 +0000 | [diff] [blame] | 260 | inet_ntoa (op->dst)); |
| 261 | return; |
| 262 | } |
| 263 | |
| 264 | /* Add packet to head of queue. */ |
| 265 | ospf_fifo_push_head (oi->obuf, op); |
| 266 | |
| 267 | /* Debug of packet fifo*/ |
| 268 | /* ospf_fifo_debug (oi->obuf); */ |
| 269 | } |
| 270 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 271 | void |
| 272 | ospf_packet_delete (struct ospf_interface *oi) |
| 273 | { |
| 274 | struct ospf_packet *op; |
| 275 | |
| 276 | op = ospf_fifo_pop (oi->obuf); |
| 277 | |
| 278 | if (op) |
| 279 | ospf_packet_free (op); |
| 280 | } |
| 281 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 282 | struct ospf_packet * |
| 283 | ospf_packet_dup (struct ospf_packet *op) |
| 284 | { |
| 285 | struct ospf_packet *new; |
| 286 | |
paul | 37163d6 | 2003-02-03 18:40:56 +0000 | [diff] [blame] | 287 | if (stream_get_endp(op->s) != op->length) |
Andrew J. Schorr | 08c8367 | 2006-09-25 13:26:14 +0000 | [diff] [blame] | 288 | /* XXX size_t */ |
| 289 | zlog_warn ("ospf_packet_dup stream %lu ospf_packet %u size mismatch", |
| 290 | (u_long)STREAM_SIZE(op->s), op->length); |
paul | 30961a1 | 2002-12-13 20:56:48 +0000 | [diff] [blame] | 291 | |
| 292 | /* Reserve space for MD5 authentication that may be added later. */ |
| 293 | new = ospf_packet_new (stream_get_endp(op->s) + OSPF_AUTH_MD5_SIZE); |
paul | fa81b71 | 2005-02-19 01:19:20 +0000 | [diff] [blame] | 294 | stream_copy (new->s, op->s); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 295 | |
| 296 | new->dst = op->dst; |
| 297 | new->length = op->length; |
| 298 | |
| 299 | return new; |
| 300 | } |
| 301 | |
gdt | 86f1fd9 | 2005-01-10 14:20:43 +0000 | [diff] [blame] | 302 | /* XXX inline */ |
Paul Jakma | f63f06d | 2011-04-08 12:44:43 +0100 | [diff] [blame] | 303 | static unsigned int |
gdt | 86f1fd9 | 2005-01-10 14:20:43 +0000 | [diff] [blame] | 304 | ospf_packet_authspace (struct ospf_interface *oi) |
| 305 | { |
| 306 | int auth = 0; |
| 307 | |
| 308 | if ( ospf_auth_type (oi) == OSPF_AUTH_CRYPTOGRAPHIC) |
| 309 | auth = OSPF_AUTH_MD5_SIZE; |
| 310 | |
| 311 | return auth; |
| 312 | } |
| 313 | |
paul | 4dadc29 | 2005-05-06 21:37:42 +0000 | [diff] [blame] | 314 | static unsigned int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 315 | ospf_packet_max (struct ospf_interface *oi) |
| 316 | { |
| 317 | int max; |
| 318 | |
gdt | 86f1fd9 | 2005-01-10 14:20:43 +0000 | [diff] [blame] | 319 | max = oi->ifp->mtu - ospf_packet_authspace(oi); |
| 320 | |
paul | 68b7339 | 2004-09-12 14:21:37 +0000 | [diff] [blame] | 321 | max -= (OSPF_HEADER_SIZE + sizeof (struct ip)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 322 | |
| 323 | return max; |
| 324 | } |
| 325 | |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 326 | |
paul | 4dadc29 | 2005-05-06 21:37:42 +0000 | [diff] [blame] | 327 | static int |
Denis Ovsienko | 2d8223c | 2012-01-30 20:32:39 +0400 | [diff] [blame] | 328 | ospf_check_md5_digest (struct ospf_interface *oi, struct ospf_header *ospfh) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 329 | { |
vincent | c1a03d4 | 2005-09-28 15:47:44 +0000 | [diff] [blame] | 330 | MD5_CTX ctx; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 331 | unsigned char digest[OSPF_AUTH_MD5_SIZE]; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 332 | struct crypt_key *ck; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 333 | struct ospf_neighbor *nbr; |
Denis Ovsienko | 2d8223c | 2012-01-30 20:32:39 +0400 | [diff] [blame] | 334 | u_int16_t length = ntohs (ospfh->length); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 335 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 336 | /* Get secret key. */ |
| 337 | ck = ospf_crypt_key_lookup (OSPF_IF_PARAM (oi, auth_crypt), |
| 338 | ospfh->u.crypt.key_id); |
| 339 | if (ck == NULL) |
| 340 | { |
| 341 | zlog_warn ("interface %s: ospf_check_md5 no key %d", |
| 342 | IF_NAME (oi), ospfh->u.crypt.key_id); |
| 343 | return 0; |
| 344 | } |
| 345 | |
| 346 | /* check crypto seqnum. */ |
| 347 | nbr = ospf_nbr_lookup_by_routerid (oi->nbrs, &ospfh->router_id); |
| 348 | |
| 349 | if (nbr && ntohl(nbr->crypt_seqnum) > ntohl(ospfh->u.crypt.crypt_seqnum)) |
| 350 | { |
| 351 | zlog_warn ("interface %s: ospf_check_md5 bad sequence %d (expect %d)", |
| 352 | IF_NAME (oi), |
| 353 | ntohl(ospfh->u.crypt.crypt_seqnum), |
| 354 | ntohl(nbr->crypt_seqnum)); |
| 355 | return 0; |
| 356 | } |
| 357 | |
| 358 | /* Generate a digest for the ospf packet - their digest + our digest. */ |
vincent | c1a03d4 | 2005-09-28 15:47:44 +0000 | [diff] [blame] | 359 | memset(&ctx, 0, sizeof(ctx)); |
| 360 | MD5Init(&ctx); |
Denis Ovsienko | 2d8223c | 2012-01-30 20:32:39 +0400 | [diff] [blame] | 361 | MD5Update(&ctx, ospfh, length); |
vincent | c1a03d4 | 2005-09-28 15:47:44 +0000 | [diff] [blame] | 362 | MD5Update(&ctx, ck->auth_key, OSPF_AUTH_MD5_SIZE); |
| 363 | MD5Final(digest, &ctx); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 364 | |
| 365 | /* compare the two */ |
Denis Ovsienko | 2d8223c | 2012-01-30 20:32:39 +0400 | [diff] [blame] | 366 | if (memcmp ((caddr_t)ospfh + length, digest, OSPF_AUTH_MD5_SIZE)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 367 | { |
| 368 | zlog_warn ("interface %s: ospf_check_md5 checksum mismatch", |
| 369 | IF_NAME (oi)); |
| 370 | return 0; |
| 371 | } |
| 372 | |
| 373 | /* save neighbor's crypt_seqnum */ |
| 374 | if (nbr) |
| 375 | nbr->crypt_seqnum = ospfh->u.crypt.crypt_seqnum; |
| 376 | return 1; |
| 377 | } |
| 378 | |
| 379 | /* This function is called from ospf_write(), it will detect the |
| 380 | authentication scheme and if it is MD5, it will change the sequence |
| 381 | and update the MD5 digest. */ |
paul | 4dadc29 | 2005-05-06 21:37:42 +0000 | [diff] [blame] | 382 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 383 | ospf_make_md5_digest (struct ospf_interface *oi, struct ospf_packet *op) |
| 384 | { |
| 385 | struct ospf_header *ospfh; |
Joakim Tjernlund | ea2a598 | 2009-11-26 12:23:07 +0000 | [diff] [blame] | 386 | unsigned char digest[OSPF_AUTH_MD5_SIZE] = {0}; |
vincent | c1a03d4 | 2005-09-28 15:47:44 +0000 | [diff] [blame] | 387 | MD5_CTX ctx; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 388 | void *ibuf; |
paul | 9483e15 | 2002-12-13 20:55:25 +0000 | [diff] [blame] | 389 | u_int32_t t; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 390 | struct crypt_key *ck; |
paul | 3623814 | 2005-10-11 04:12:54 +0000 | [diff] [blame] | 391 | const u_int8_t *auth_key; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 392 | |
| 393 | ibuf = STREAM_DATA (op->s); |
| 394 | ospfh = (struct ospf_header *) ibuf; |
| 395 | |
| 396 | if (ntohs (ospfh->auth_type) != OSPF_AUTH_CRYPTOGRAPHIC) |
| 397 | return 0; |
| 398 | |
| 399 | /* We do this here so when we dup a packet, we don't have to |
Paul Jakma | 2518efd | 2006-08-27 06:49:29 +0000 | [diff] [blame] | 400 | waste CPU rewriting other headers. |
| 401 | |
| 402 | Note that quagga_time /deliberately/ is not used here */ |
paul | 9483e15 | 2002-12-13 20:55:25 +0000 | [diff] [blame] | 403 | t = (time(NULL) & 0xFFFFFFFF); |
paul | 818e56c | 2006-01-10 23:27:05 +0000 | [diff] [blame] | 404 | if (t > oi->crypt_seqnum) |
| 405 | oi->crypt_seqnum = t; |
| 406 | else |
| 407 | oi->crypt_seqnum++; |
| 408 | |
paul | 9483e15 | 2002-12-13 20:55:25 +0000 | [diff] [blame] | 409 | ospfh->u.crypt.crypt_seqnum = htonl (oi->crypt_seqnum); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 410 | |
| 411 | /* Get MD5 Authentication key from auth_key list. */ |
| 412 | if (list_isempty (OSPF_IF_PARAM (oi, auth_crypt))) |
Joakim Tjernlund | ea2a598 | 2009-11-26 12:23:07 +0000 | [diff] [blame] | 413 | auth_key = (const u_int8_t *) digest; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 414 | else |
| 415 | { |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 416 | ck = listgetdata (listtail(OSPF_IF_PARAM (oi, auth_crypt))); |
paul | 4dadc29 | 2005-05-06 21:37:42 +0000 | [diff] [blame] | 417 | auth_key = ck->auth_key; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | /* Generate a digest for the entire packet + our secret key. */ |
vincent | c1a03d4 | 2005-09-28 15:47:44 +0000 | [diff] [blame] | 421 | memset(&ctx, 0, sizeof(ctx)); |
| 422 | MD5Init(&ctx); |
| 423 | MD5Update(&ctx, ibuf, ntohs (ospfh->length)); |
| 424 | MD5Update(&ctx, auth_key, OSPF_AUTH_MD5_SIZE); |
| 425 | MD5Final(digest, &ctx); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 426 | |
| 427 | /* Append md5 digest to the end of the stream. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 428 | stream_put (op->s, digest, OSPF_AUTH_MD5_SIZE); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 429 | |
| 430 | /* We do *NOT* increment the OSPF header length. */ |
paul | 30961a1 | 2002-12-13 20:56:48 +0000 | [diff] [blame] | 431 | op->length = ntohs (ospfh->length) + OSPF_AUTH_MD5_SIZE; |
| 432 | |
paul | 37163d6 | 2003-02-03 18:40:56 +0000 | [diff] [blame] | 433 | if (stream_get_endp(op->s) != op->length) |
Andrew J. Schorr | 08c8367 | 2006-09-25 13:26:14 +0000 | [diff] [blame] | 434 | /* XXX size_t */ |
| 435 | zlog_warn("ospf_make_md5_digest: length mismatch stream %lu ospf_packet %u", |
| 436 | (u_long)stream_get_endp(op->s), op->length); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 437 | |
| 438 | return OSPF_AUTH_MD5_SIZE; |
| 439 | } |
| 440 | |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 441 | |
paul | 4dadc29 | 2005-05-06 21:37:42 +0000 | [diff] [blame] | 442 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 443 | ospf_ls_req_timer (struct thread *thread) |
| 444 | { |
| 445 | struct ospf_neighbor *nbr; |
| 446 | |
| 447 | nbr = THREAD_ARG (thread); |
| 448 | nbr->t_ls_req = NULL; |
| 449 | |
| 450 | /* Send Link State Request. */ |
| 451 | if (ospf_ls_request_count (nbr)) |
| 452 | ospf_ls_req_send (nbr); |
| 453 | |
| 454 | /* Set Link State Request retransmission timer. */ |
| 455 | OSPF_NSM_TIMER_ON (nbr->t_ls_req, ospf_ls_req_timer, nbr->v_ls_req); |
| 456 | |
| 457 | return 0; |
| 458 | } |
| 459 | |
| 460 | void |
| 461 | ospf_ls_req_event (struct ospf_neighbor *nbr) |
| 462 | { |
| 463 | if (nbr->t_ls_req) |
| 464 | { |
| 465 | thread_cancel (nbr->t_ls_req); |
| 466 | nbr->t_ls_req = NULL; |
| 467 | } |
| 468 | nbr->t_ls_req = thread_add_event (master, ospf_ls_req_timer, nbr, 0); |
| 469 | } |
| 470 | |
| 471 | /* Cyclic timer function. Fist registered in ospf_nbr_new () in |
| 472 | ospf_neighbor.c */ |
| 473 | int |
| 474 | ospf_ls_upd_timer (struct thread *thread) |
| 475 | { |
| 476 | struct ospf_neighbor *nbr; |
| 477 | |
| 478 | nbr = THREAD_ARG (thread); |
| 479 | nbr->t_ls_upd = NULL; |
| 480 | |
| 481 | /* Send Link State Update. */ |
| 482 | if (ospf_ls_retransmit_count (nbr) > 0) |
| 483 | { |
hasso | 52dc7ee | 2004-09-23 19:18:23 +0000 | [diff] [blame] | 484 | struct list *update; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 485 | struct ospf_lsdb *lsdb; |
| 486 | int i; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 487 | int retransmit_interval; |
| 488 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 489 | retransmit_interval = OSPF_IF_PARAM (nbr->oi, retransmit_interval); |
| 490 | |
| 491 | lsdb = &nbr->ls_rxmt; |
| 492 | update = list_new (); |
| 493 | |
| 494 | for (i = OSPF_MIN_LSA; i < OSPF_MAX_LSA; i++) |
| 495 | { |
| 496 | struct route_table *table = lsdb->type[i].db; |
| 497 | struct route_node *rn; |
| 498 | |
| 499 | for (rn = route_top (table); rn; rn = route_next (rn)) |
| 500 | { |
| 501 | struct ospf_lsa *lsa; |
| 502 | |
| 503 | if ((lsa = rn->info) != NULL) |
| 504 | /* Don't retransmit an LSA if we received it within |
| 505 | the last RxmtInterval seconds - this is to allow the |
| 506 | neighbour a chance to acknowledge the LSA as it may |
| 507 | have ben just received before the retransmit timer |
| 508 | fired. This is a small tweak to what is in the RFC, |
| 509 | but it will cut out out a lot of retransmit traffic |
| 510 | - MAG */ |
Paul Jakma | 2518efd | 2006-08-27 06:49:29 +0000 | [diff] [blame] | 511 | if (tv_cmp (tv_sub (recent_relative_time (), lsa->tv_recv), |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 512 | int2tv (retransmit_interval)) >= 0) |
| 513 | listnode_add (update, rn->info); |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | if (listcount (update) > 0) |
| 518 | ospf_ls_upd_send (nbr, update, OSPF_SEND_PACKET_DIRECT); |
| 519 | list_delete (update); |
| 520 | } |
| 521 | |
| 522 | /* Set LS Update retransmission timer. */ |
| 523 | OSPF_NSM_TIMER_ON (nbr->t_ls_upd, ospf_ls_upd_timer, nbr->v_ls_upd); |
| 524 | |
| 525 | return 0; |
| 526 | } |
| 527 | |
| 528 | int |
| 529 | ospf_ls_ack_timer (struct thread *thread) |
| 530 | { |
| 531 | struct ospf_interface *oi; |
| 532 | |
| 533 | oi = THREAD_ARG (thread); |
| 534 | oi->t_ls_ack = NULL; |
| 535 | |
| 536 | /* Send Link State Acknowledgment. */ |
| 537 | if (listcount (oi->ls_ack) > 0) |
| 538 | ospf_ls_ack_send_delayed (oi); |
| 539 | |
| 540 | /* Set LS Ack timer. */ |
| 541 | OSPF_ISM_TIMER_ON (oi->t_ls_ack, ospf_ls_ack_timer, oi->v_ls_ack); |
| 542 | |
| 543 | return 0; |
| 544 | } |
| 545 | |
paul | 0bfeca3 | 2004-09-24 08:07:54 +0000 | [diff] [blame] | 546 | #ifdef WANT_OSPF_WRITE_FRAGMENT |
ajs | 5dcbdf8 | 2005-03-29 16:13:49 +0000 | [diff] [blame] | 547 | static void |
paul | 6a99f83 | 2004-09-27 12:56:30 +0000 | [diff] [blame] | 548 | ospf_write_frags (int fd, struct ospf_packet *op, struct ip *iph, |
paul | 62d8e96 | 2004-11-02 20:26:45 +0000 | [diff] [blame] | 549 | struct msghdr *msg, unsigned int maxdatasize, |
paul | 37ccfa3 | 2004-10-31 11:24:51 +0000 | [diff] [blame] | 550 | unsigned int mtu, int flags, u_char type) |
paul | 0bfeca3 | 2004-09-24 08:07:54 +0000 | [diff] [blame] | 551 | { |
| 552 | #define OSPF_WRITE_FRAG_SHIFT 3 |
paul | 6a99f83 | 2004-09-27 12:56:30 +0000 | [diff] [blame] | 553 | u_int16_t offset; |
paul | 62d8e96 | 2004-11-02 20:26:45 +0000 | [diff] [blame] | 554 | struct iovec *iovp; |
paul | 6a99f83 | 2004-09-27 12:56:30 +0000 | [diff] [blame] | 555 | int ret; |
paul | 0bfeca3 | 2004-09-24 08:07:54 +0000 | [diff] [blame] | 556 | |
| 557 | assert ( op->length == stream_get_endp(op->s) ); |
paul | 62d8e96 | 2004-11-02 20:26:45 +0000 | [diff] [blame] | 558 | assert (msg->msg_iovlen == 2); |
paul | 0bfeca3 | 2004-09-24 08:07:54 +0000 | [diff] [blame] | 559 | |
| 560 | /* we can but try. |
| 561 | * |
| 562 | * SunOS, BSD and BSD derived kernels likely will clear ip_id, as |
| 563 | * well as the IP_MF flag, making this all quite pointless. |
| 564 | * |
| 565 | * However, for a system on which IP_MF is left alone, and ip_id left |
| 566 | * alone or else which sets same ip_id for each fragment this might |
| 567 | * work, eg linux. |
| 568 | * |
| 569 | * XXX-TODO: It would be much nicer to have the kernel's use their |
| 570 | * existing fragmentation support to do this for us. Bugs/RFEs need to |
| 571 | * be raised against the various kernels. |
| 572 | */ |
| 573 | |
| 574 | /* set More Frag */ |
| 575 | iph->ip_off |= IP_MF; |
| 576 | |
| 577 | /* ip frag offset is expressed in units of 8byte words */ |
| 578 | offset = maxdatasize >> OSPF_WRITE_FRAG_SHIFT; |
| 579 | |
paul | 62d8e96 | 2004-11-02 20:26:45 +0000 | [diff] [blame] | 580 | iovp = &msg->msg_iov[1]; |
| 581 | |
paul | 0bfeca3 | 2004-09-24 08:07:54 +0000 | [diff] [blame] | 582 | while ( (stream_get_endp(op->s) - stream_get_getp (op->s)) |
| 583 | > maxdatasize ) |
| 584 | { |
| 585 | /* data length of this frag is to next offset value */ |
paul | 62d8e96 | 2004-11-02 20:26:45 +0000 | [diff] [blame] | 586 | iovp->iov_len = offset << OSPF_WRITE_FRAG_SHIFT; |
| 587 | iph->ip_len = iovp->iov_len + sizeof (struct ip); |
paul | 6a99f83 | 2004-09-27 12:56:30 +0000 | [diff] [blame] | 588 | assert (iph->ip_len <= mtu); |
paul | 0bfeca3 | 2004-09-24 08:07:54 +0000 | [diff] [blame] | 589 | |
paul | 18b12c3 | 2004-10-05 14:38:29 +0000 | [diff] [blame] | 590 | sockopt_iphdrincl_swab_htosys (iph); |
paul | 0bfeca3 | 2004-09-24 08:07:54 +0000 | [diff] [blame] | 591 | |
paul | 6a99f83 | 2004-09-27 12:56:30 +0000 | [diff] [blame] | 592 | ret = sendmsg (fd, msg, flags); |
paul | 0bfeca3 | 2004-09-24 08:07:54 +0000 | [diff] [blame] | 593 | |
paul | 18b12c3 | 2004-10-05 14:38:29 +0000 | [diff] [blame] | 594 | sockopt_iphdrincl_swab_systoh (iph); |
paul | 0bfeca3 | 2004-09-24 08:07:54 +0000 | [diff] [blame] | 595 | |
| 596 | if (ret < 0) |
paul | 37ccfa3 | 2004-10-31 11:24:51 +0000 | [diff] [blame] | 597 | zlog_warn ("*** ospf_write_frags: sendmsg failed to %s," |
ajs | 5dcbdf8 | 2005-03-29 16:13:49 +0000 | [diff] [blame] | 598 | " id %d, off %d, len %d, mtu %u failed with %s", |
| 599 | inet_ntoa (iph->ip_dst), |
| 600 | iph->ip_id, |
| 601 | iph->ip_off, |
| 602 | iph->ip_len, |
| 603 | mtu, |
| 604 | safe_strerror (errno)); |
paul | 0bfeca3 | 2004-09-24 08:07:54 +0000 | [diff] [blame] | 605 | |
paul | 37ccfa3 | 2004-10-31 11:24:51 +0000 | [diff] [blame] | 606 | if (IS_DEBUG_OSPF_PACKET (type - 1, SEND)) |
| 607 | { |
ajs | 2a42e28 | 2004-12-08 18:43:03 +0000 | [diff] [blame] | 608 | zlog_debug ("ospf_write_frags: sent id %d, off %d, len %d to %s\n", |
paul | 37ccfa3 | 2004-10-31 11:24:51 +0000 | [diff] [blame] | 609 | iph->ip_id, iph->ip_off, iph->ip_len, |
| 610 | inet_ntoa (iph->ip_dst)); |
| 611 | if (IS_DEBUG_OSPF_PACKET (type - 1, DETAIL)) |
| 612 | { |
ajs | 2a42e28 | 2004-12-08 18:43:03 +0000 | [diff] [blame] | 613 | zlog_debug ("-----------------IP Header Dump----------------------"); |
paul | 37ccfa3 | 2004-10-31 11:24:51 +0000 | [diff] [blame] | 614 | ospf_ip_header_dump (iph); |
ajs | 2a42e28 | 2004-12-08 18:43:03 +0000 | [diff] [blame] | 615 | zlog_debug ("-----------------------------------------------------"); |
paul | 37ccfa3 | 2004-10-31 11:24:51 +0000 | [diff] [blame] | 616 | } |
| 617 | } |
| 618 | |
paul | 0bfeca3 | 2004-09-24 08:07:54 +0000 | [diff] [blame] | 619 | iph->ip_off += offset; |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 620 | stream_forward_getp (op->s, iovp->iov_len); |
paul | 62d8e96 | 2004-11-02 20:26:45 +0000 | [diff] [blame] | 621 | iovp->iov_base = STREAM_PNT (op->s); |
paul | 0bfeca3 | 2004-09-24 08:07:54 +0000 | [diff] [blame] | 622 | } |
| 623 | |
| 624 | /* setup for final fragment */ |
paul | 62d8e96 | 2004-11-02 20:26:45 +0000 | [diff] [blame] | 625 | iovp->iov_len = stream_get_endp(op->s) - stream_get_getp (op->s); |
| 626 | iph->ip_len = iovp->iov_len + sizeof (struct ip); |
paul | 0bfeca3 | 2004-09-24 08:07:54 +0000 | [diff] [blame] | 627 | iph->ip_off &= (~IP_MF); |
| 628 | } |
| 629 | #endif /* WANT_OSPF_WRITE_FRAGMENT */ |
| 630 | |
ajs | 5dcbdf8 | 2005-03-29 16:13:49 +0000 | [diff] [blame] | 631 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 632 | ospf_write (struct thread *thread) |
| 633 | { |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 634 | struct ospf *ospf = THREAD_ARG (thread); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 635 | struct ospf_interface *oi; |
| 636 | struct ospf_packet *op; |
| 637 | struct sockaddr_in sa_dst; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 638 | struct ip iph; |
| 639 | struct msghdr msg; |
paul | 62d8e96 | 2004-11-02 20:26:45 +0000 | [diff] [blame] | 640 | struct iovec iov[2]; |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 641 | u_char type; |
| 642 | int ret; |
| 643 | int flags = 0; |
hasso | 52dc7ee | 2004-09-23 19:18:23 +0000 | [diff] [blame] | 644 | struct listnode *node; |
paul | 0bfeca3 | 2004-09-24 08:07:54 +0000 | [diff] [blame] | 645 | #ifdef WANT_OSPF_WRITE_FRAGMENT |
paul | 68b7339 | 2004-09-12 14:21:37 +0000 | [diff] [blame] | 646 | static u_int16_t ipid = 0; |
paul | 0bfeca3 | 2004-09-24 08:07:54 +0000 | [diff] [blame] | 647 | #endif /* WANT_OSPF_WRITE_FRAGMENT */ |
paul | 6a99f83 | 2004-09-27 12:56:30 +0000 | [diff] [blame] | 648 | u_int16_t maxdatasize; |
paul | 68b7339 | 2004-09-12 14:21:37 +0000 | [diff] [blame] | 649 | #define OSPF_WRITE_IPHL_SHIFT 2 |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 650 | |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 651 | ospf->t_write = NULL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 652 | |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 653 | node = listhead (ospf->oi_write_q); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 654 | assert (node); |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 655 | oi = listgetdata (node); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 656 | assert (oi); |
paul | 0bfeca3 | 2004-09-24 08:07:54 +0000 | [diff] [blame] | 657 | |
| 658 | #ifdef WANT_OSPF_WRITE_FRAGMENT |
paul | 68b7339 | 2004-09-12 14:21:37 +0000 | [diff] [blame] | 659 | /* seed ipid static with low order bits of time */ |
| 660 | if (ipid == 0) |
| 661 | ipid = (time(NULL) & 0xffff); |
paul | 0bfeca3 | 2004-09-24 08:07:54 +0000 | [diff] [blame] | 662 | #endif /* WANT_OSPF_WRITE_FRAGMENT */ |
| 663 | |
Denis Ovsienko | b7fe414 | 2007-08-21 16:32:56 +0000 | [diff] [blame] | 664 | /* convenience - max OSPF data per packet, |
| 665 | * and reliability - not more data, than our |
| 666 | * socket can accept |
| 667 | */ |
| 668 | maxdatasize = MIN (oi->ifp->mtu, ospf->maxsndbuflen) - |
| 669 | sizeof (struct ip); |
paul | 68b7339 | 2004-09-12 14:21:37 +0000 | [diff] [blame] | 670 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 671 | /* Get one packet from queue. */ |
| 672 | op = ospf_fifo_head (oi->obuf); |
| 673 | assert (op); |
| 674 | assert (op->length >= OSPF_HEADER_SIZE); |
| 675 | |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 676 | if (op->dst.s_addr == htonl (OSPF_ALLSPFROUTERS) |
| 677 | || op->dst.s_addr == htonl (OSPF_ALLDROUTERS)) |
paul | 68b7339 | 2004-09-12 14:21:37 +0000 | [diff] [blame] | 678 | ospf_if_ipmulticast (ospf, oi->address, oi->ifp->ifindex); |
| 679 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 680 | /* Rewrite the md5 signature & update the seq */ |
| 681 | ospf_make_md5_digest (oi, op); |
| 682 | |
paul | 37ccfa3 | 2004-10-31 11:24:51 +0000 | [diff] [blame] | 683 | /* Retrieve OSPF packet type. */ |
| 684 | stream_set_getp (op->s, 1); |
| 685 | type = stream_getc (op->s); |
| 686 | |
paul | 68b7339 | 2004-09-12 14:21:37 +0000 | [diff] [blame] | 687 | /* reset get pointer */ |
| 688 | stream_set_getp (op->s, 0); |
| 689 | |
| 690 | memset (&iph, 0, sizeof (struct ip)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 691 | memset (&sa_dst, 0, sizeof (sa_dst)); |
paul | 68b7339 | 2004-09-12 14:21:37 +0000 | [diff] [blame] | 692 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 693 | sa_dst.sin_family = AF_INET; |
Paul Jakma | 6f0e3f6 | 2007-05-10 02:38:51 +0000 | [diff] [blame] | 694 | #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 695 | sa_dst.sin_len = sizeof(sa_dst); |
Paul Jakma | 6f0e3f6 | 2007-05-10 02:38:51 +0000 | [diff] [blame] | 696 | #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 697 | sa_dst.sin_addr = op->dst; |
| 698 | sa_dst.sin_port = htons (0); |
| 699 | |
| 700 | /* Set DONTROUTE flag if dst is unicast. */ |
| 701 | if (oi->type != OSPF_IFTYPE_VIRTUALLINK) |
| 702 | if (!IN_MULTICAST (htonl (op->dst.s_addr))) |
| 703 | flags = MSG_DONTROUTE; |
| 704 | |
paul | 68b7339 | 2004-09-12 14:21:37 +0000 | [diff] [blame] | 705 | iph.ip_hl = sizeof (struct ip) >> OSPF_WRITE_IPHL_SHIFT; |
| 706 | /* it'd be very strange for header to not be 4byte-word aligned but.. */ |
paul | 6c83567 | 2004-10-11 11:00:30 +0000 | [diff] [blame] | 707 | if ( sizeof (struct ip) |
| 708 | > (unsigned int)(iph.ip_hl << OSPF_WRITE_IPHL_SHIFT) ) |
paul | 68b7339 | 2004-09-12 14:21:37 +0000 | [diff] [blame] | 709 | iph.ip_hl++; /* we presume sizeof struct ip cant overflow ip_hl.. */ |
| 710 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 711 | iph.ip_v = IPVERSION; |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 712 | iph.ip_tos = IPTOS_PREC_INTERNETCONTROL; |
paul | 68b7339 | 2004-09-12 14:21:37 +0000 | [diff] [blame] | 713 | iph.ip_len = (iph.ip_hl << OSPF_WRITE_IPHL_SHIFT) + op->length; |
paul | 68b7339 | 2004-09-12 14:21:37 +0000 | [diff] [blame] | 714 | |
David BÉRARD | 0150c9c | 2010-05-11 10:17:53 +0200 | [diff] [blame] | 715 | #if defined(__DragonFly__) |
| 716 | /* |
| 717 | * DragonFly's raw socket expects ip_len/ip_off in network byte order. |
| 718 | */ |
| 719 | iph.ip_len = htons(iph.ip_len); |
| 720 | #endif |
| 721 | |
paul | 0bfeca3 | 2004-09-24 08:07:54 +0000 | [diff] [blame] | 722 | #ifdef WANT_OSPF_WRITE_FRAGMENT |
paul | 68b7339 | 2004-09-12 14:21:37 +0000 | [diff] [blame] | 723 | /* XXX-MT: not thread-safe at all.. |
| 724 | * XXX: this presumes this is only programme sending OSPF packets |
| 725 | * otherwise, no guarantee ipid will be unique |
| 726 | */ |
| 727 | iph.ip_id = ++ipid; |
paul | 0bfeca3 | 2004-09-24 08:07:54 +0000 | [diff] [blame] | 728 | #endif /* WANT_OSPF_WRITE_FRAGMENT */ |
| 729 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 730 | iph.ip_off = 0; |
| 731 | if (oi->type == OSPF_IFTYPE_VIRTUALLINK) |
| 732 | iph.ip_ttl = OSPF_VL_IP_TTL; |
| 733 | else |
| 734 | iph.ip_ttl = OSPF_IP_TTL; |
| 735 | iph.ip_p = IPPROTO_OSPFIGP; |
| 736 | iph.ip_sum = 0; |
| 737 | iph.ip_src.s_addr = oi->address->u.prefix4.s_addr; |
| 738 | iph.ip_dst.s_addr = op->dst.s_addr; |
| 739 | |
| 740 | memset (&msg, 0, sizeof (msg)); |
paul | 68defd6 | 2004-09-27 07:27:13 +0000 | [diff] [blame] | 741 | msg.msg_name = (caddr_t) &sa_dst; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 742 | msg.msg_namelen = sizeof (sa_dst); |
| 743 | msg.msg_iov = iov; |
| 744 | msg.msg_iovlen = 2; |
| 745 | iov[0].iov_base = (char*)&iph; |
paul | 68b7339 | 2004-09-12 14:21:37 +0000 | [diff] [blame] | 746 | iov[0].iov_len = iph.ip_hl << OSPF_WRITE_IPHL_SHIFT; |
| 747 | iov[1].iov_base = STREAM_PNT (op->s); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 748 | iov[1].iov_len = op->length; |
paul | 68b7339 | 2004-09-12 14:21:37 +0000 | [diff] [blame] | 749 | |
| 750 | /* Sadly we can not rely on kernels to fragment packets because of either |
| 751 | * IP_HDRINCL and/or multicast destination being set. |
| 752 | */ |
paul | 0bfeca3 | 2004-09-24 08:07:54 +0000 | [diff] [blame] | 753 | #ifdef WANT_OSPF_WRITE_FRAGMENT |
paul | 68b7339 | 2004-09-12 14:21:37 +0000 | [diff] [blame] | 754 | if ( op->length > maxdatasize ) |
paul | 62d8e96 | 2004-11-02 20:26:45 +0000 | [diff] [blame] | 755 | ospf_write_frags (ospf->fd, op, &iph, &msg, maxdatasize, |
| 756 | oi->ifp->mtu, flags, type); |
paul | 0bfeca3 | 2004-09-24 08:07:54 +0000 | [diff] [blame] | 757 | #endif /* WANT_OSPF_WRITE_FRAGMENT */ |
paul | 68b7339 | 2004-09-12 14:21:37 +0000 | [diff] [blame] | 758 | |
| 759 | /* send final fragment (could be first) */ |
paul | 18b12c3 | 2004-10-05 14:38:29 +0000 | [diff] [blame] | 760 | sockopt_iphdrincl_swab_htosys (&iph); |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 761 | ret = sendmsg (ospf->fd, &msg, flags); |
paul | 6b33361 | 2004-10-11 10:11:25 +0000 | [diff] [blame] | 762 | sockopt_iphdrincl_swab_systoh (&iph); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 763 | |
| 764 | if (ret < 0) |
ajs | 083ee9d | 2005-02-09 15:35:50 +0000 | [diff] [blame] | 765 | zlog_warn ("*** sendmsg in ospf_write failed to %s, " |
ajs | 5dcbdf8 | 2005-03-29 16:13:49 +0000 | [diff] [blame] | 766 | "id %d, off %d, len %d, interface %s, mtu %u: %s", |
ajs | 083ee9d | 2005-02-09 15:35:50 +0000 | [diff] [blame] | 767 | inet_ntoa (iph.ip_dst), iph.ip_id, iph.ip_off, iph.ip_len, |
ajs | 5dcbdf8 | 2005-03-29 16:13:49 +0000 | [diff] [blame] | 768 | oi->ifp->name, oi->ifp->mtu, safe_strerror (errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 769 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 770 | /* Show debug sending packet. */ |
| 771 | if (IS_DEBUG_OSPF_PACKET (type - 1, SEND)) |
| 772 | { |
| 773 | if (IS_DEBUG_OSPF_PACKET (type - 1, DETAIL)) |
| 774 | { |
ajs | 2a42e28 | 2004-12-08 18:43:03 +0000 | [diff] [blame] | 775 | zlog_debug ("-----------------------------------------------------"); |
paul | 37ccfa3 | 2004-10-31 11:24:51 +0000 | [diff] [blame] | 776 | ospf_ip_header_dump (&iph); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 777 | stream_set_getp (op->s, 0); |
| 778 | ospf_packet_dump (op->s); |
| 779 | } |
| 780 | |
ajs | 2a42e28 | 2004-12-08 18:43:03 +0000 | [diff] [blame] | 781 | zlog_debug ("%s sent to [%s] via [%s].", |
Denis Ovsienko | 272ca1e | 2012-01-15 19:12:19 +0400 | [diff] [blame] | 782 | LOOKUP (ospf_packet_type_str, type), inet_ntoa (op->dst), |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 783 | IF_NAME (oi)); |
| 784 | |
| 785 | if (IS_DEBUG_OSPF_PACKET (type - 1, DETAIL)) |
ajs | 2a42e28 | 2004-12-08 18:43:03 +0000 | [diff] [blame] | 786 | zlog_debug ("-----------------------------------------------------"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 787 | } |
| 788 | |
| 789 | /* Now delete packet from queue. */ |
| 790 | ospf_packet_delete (oi); |
| 791 | |
Dinesh G Dutt | 1c06334 | 2014-09-30 13:04:45 -0700 | [diff] [blame] | 792 | /* Move this interface to the tail of write_q to |
| 793 | serve everyone in a round robin fashion */ |
Paul Jakma | 6d83113 | 2014-10-09 16:05:15 +0100 | [diff] [blame] | 794 | listnode_move_to_tail (ospf->oi_write_q, node); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 795 | if (ospf_fifo_head (oi->obuf) == NULL) |
| 796 | { |
| 797 | oi->on_write_q = 0; |
Paul Jakma | 6d83113 | 2014-10-09 16:05:15 +0100 | [diff] [blame] | 798 | list_delete_node (ospf->oi_write_q, node); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 799 | } |
| 800 | |
| 801 | /* If packets still remain in queue, call write thread. */ |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 802 | if (!list_isempty (ospf->oi_write_q)) |
| 803 | ospf->t_write = |
| 804 | thread_add_write (master, ospf_write, ospf, ospf->fd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 805 | |
| 806 | return 0; |
| 807 | } |
| 808 | |
| 809 | /* OSPF Hello message read -- RFC2328 Section 10.5. */ |
paul | 4dadc29 | 2005-05-06 21:37:42 +0000 | [diff] [blame] | 810 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 811 | ospf_hello (struct ip *iph, struct ospf_header *ospfh, |
| 812 | struct stream * s, struct ospf_interface *oi, int size) |
| 813 | { |
| 814 | struct ospf_hello *hello; |
| 815 | struct ospf_neighbor *nbr; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 816 | int old_state; |
paul | d3f0d62 | 2004-05-05 15:27:15 +0000 | [diff] [blame] | 817 | struct prefix p; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 818 | |
| 819 | /* increment statistics. */ |
| 820 | oi->hello_in++; |
| 821 | |
| 822 | hello = (struct ospf_hello *) STREAM_PNT (s); |
| 823 | |
| 824 | /* If Hello is myself, silently discard. */ |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 825 | if (IPV4_ADDR_SAME (&ospfh->router_id, &oi->ospf->router_id)) |
paul | d324181 | 2003-09-29 12:42:39 +0000 | [diff] [blame] | 826 | { |
| 827 | if (IS_DEBUG_OSPF_PACKET (ospfh->type - 1, RECV)) |
| 828 | { |
ajs | 2a42e28 | 2004-12-08 18:43:03 +0000 | [diff] [blame] | 829 | zlog_debug ("ospf_header[%s/%s]: selforiginated, " |
paul | d324181 | 2003-09-29 12:42:39 +0000 | [diff] [blame] | 830 | "dropping.", |
Denis Ovsienko | 272ca1e | 2012-01-15 19:12:19 +0400 | [diff] [blame] | 831 | LOOKUP (ospf_packet_type_str, ospfh->type), |
paul | d324181 | 2003-09-29 12:42:39 +0000 | [diff] [blame] | 832 | inet_ntoa (iph->ip_src)); |
| 833 | } |
| 834 | return; |
| 835 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 836 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 837 | /* get neighbor prefix. */ |
| 838 | p.family = AF_INET; |
| 839 | p.prefixlen = ip_masklen (hello->network_mask); |
| 840 | p.u.prefix4 = iph->ip_src; |
| 841 | |
| 842 | /* Compare network mask. */ |
| 843 | /* Checking is ignored for Point-to-Point and Virtual link. */ |
| 844 | if (oi->type != OSPF_IFTYPE_POINTOPOINT |
| 845 | && oi->type != OSPF_IFTYPE_VIRTUALLINK) |
| 846 | if (oi->address->prefixlen != p.prefixlen) |
| 847 | { |
Andrew J. Schorr | 13cd3dc | 2006-07-11 01:50:30 +0000 | [diff] [blame] | 848 | zlog_warn ("Packet %s [Hello:RECV]: NetworkMask mismatch on %s (configured prefix length is %d, but hello packet indicates %d).", |
| 849 | inet_ntoa(ospfh->router_id), IF_NAME(oi), |
| 850 | (int)oi->address->prefixlen, (int)p.prefixlen); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 851 | return; |
| 852 | } |
| 853 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 854 | /* Compare Router Dead Interval. */ |
| 855 | if (OSPF_IF_PARAM (oi, v_wait) != ntohl (hello->dead_interval)) |
| 856 | { |
Andrew J. Schorr | 08c8367 | 2006-09-25 13:26:14 +0000 | [diff] [blame] | 857 | zlog_warn ("Packet %s [Hello:RECV]: RouterDeadInterval mismatch " |
| 858 | "(expected %u, but received %u).", |
| 859 | inet_ntoa(ospfh->router_id), |
| 860 | OSPF_IF_PARAM(oi, v_wait), ntohl(hello->dead_interval)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 861 | return; |
| 862 | } |
| 863 | |
paul | f9ad937 | 2005-10-21 00:45:17 +0000 | [diff] [blame] | 864 | /* Compare Hello Interval - ignored if fast-hellos are set. */ |
| 865 | if (OSPF_IF_PARAM (oi, fast_hello) == 0) |
| 866 | { |
| 867 | if (OSPF_IF_PARAM (oi, v_hello) != ntohs (hello->hello_interval)) |
| 868 | { |
Andrew J. Schorr | 08c8367 | 2006-09-25 13:26:14 +0000 | [diff] [blame] | 869 | zlog_warn ("Packet %s [Hello:RECV]: HelloInterval mismatch " |
| 870 | "(expected %u, but received %u).", |
| 871 | inet_ntoa(ospfh->router_id), |
| 872 | OSPF_IF_PARAM(oi, v_hello), ntohs(hello->hello_interval)); |
paul | f9ad937 | 2005-10-21 00:45:17 +0000 | [diff] [blame] | 873 | return; |
| 874 | } |
| 875 | } |
| 876 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 877 | if (IS_DEBUG_OSPF_EVENT) |
ajs | 2a42e28 | 2004-12-08 18:43:03 +0000 | [diff] [blame] | 878 | zlog_debug ("Packet %s [Hello:RECV]: Options %s", |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 879 | inet_ntoa (ospfh->router_id), |
| 880 | ospf_options_dump (hello->options)); |
| 881 | |
| 882 | /* Compare options. */ |
| 883 | #define REJECT_IF_TBIT_ON 1 /* XXX */ |
| 884 | #ifdef REJECT_IF_TBIT_ON |
| 885 | if (CHECK_FLAG (hello->options, OSPF_OPTION_T)) |
| 886 | { |
| 887 | /* |
| 888 | * This router does not support non-zero TOS. |
| 889 | * Drop this Hello packet not to establish neighbor relationship. |
| 890 | */ |
| 891 | zlog_warn ("Packet %s [Hello:RECV]: T-bit on, drop it.", |
| 892 | inet_ntoa (ospfh->router_id)); |
| 893 | return; |
| 894 | } |
| 895 | #endif /* REJECT_IF_TBIT_ON */ |
| 896 | |
| 897 | #ifdef HAVE_OPAQUE_LSA |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 898 | if (CHECK_FLAG (oi->ospf->config, OSPF_OPAQUE_CAPABLE) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 899 | && CHECK_FLAG (hello->options, OSPF_OPTION_O)) |
| 900 | { |
| 901 | /* |
| 902 | * This router does know the correct usage of O-bit |
| 903 | * the bit should be set in DD packet only. |
| 904 | */ |
| 905 | zlog_warn ("Packet %s [Hello:RECV]: O-bit abuse?", |
| 906 | inet_ntoa (ospfh->router_id)); |
| 907 | #ifdef STRICT_OBIT_USAGE_CHECK |
| 908 | return; /* Reject this packet. */ |
| 909 | #else /* STRICT_OBIT_USAGE_CHECK */ |
| 910 | UNSET_FLAG (hello->options, OSPF_OPTION_O); /* Ignore O-bit. */ |
| 911 | #endif /* STRICT_OBIT_USAGE_CHECK */ |
| 912 | } |
| 913 | #endif /* HAVE_OPAQUE_LSA */ |
| 914 | |
| 915 | /* new for NSSA is to ensure that NP is on and E is off */ |
| 916 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 917 | if (oi->area->external_routing == OSPF_AREA_NSSA) |
| 918 | { |
| 919 | if (! (CHECK_FLAG (OPTIONS (oi), OSPF_OPTION_NP) |
| 920 | && CHECK_FLAG (hello->options, OSPF_OPTION_NP) |
| 921 | && ! CHECK_FLAG (OPTIONS (oi), OSPF_OPTION_E) |
| 922 | && ! CHECK_FLAG (hello->options, OSPF_OPTION_E))) |
| 923 | { |
| 924 | zlog_warn ("NSSA-Packet-%s[Hello:RECV]: my options: %x, his options %x", inet_ntoa (ospfh->router_id), OPTIONS (oi), hello->options); |
| 925 | return; |
| 926 | } |
| 927 | if (IS_DEBUG_OSPF_NSSA) |
ajs | 2a42e28 | 2004-12-08 18:43:03 +0000 | [diff] [blame] | 928 | zlog_debug ("NSSA-Hello:RECV:Packet from %s:", inet_ntoa(ospfh->router_id)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 929 | } |
| 930 | else |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 931 | /* The setting of the E-bit found in the Hello Packet's Options |
| 932 | field must match this area's ExternalRoutingCapability A |
| 933 | mismatch causes processing to stop and the packet to be |
| 934 | dropped. The setting of the rest of the bits in the Hello |
| 935 | Packet's Options field should be ignored. */ |
| 936 | if (CHECK_FLAG (OPTIONS (oi), OSPF_OPTION_E) != |
| 937 | CHECK_FLAG (hello->options, OSPF_OPTION_E)) |
| 938 | { |
ajs | 3aa8d5f | 2004-12-11 18:00:06 +0000 | [diff] [blame] | 939 | zlog_warn ("Packet %s [Hello:RECV]: my options: %x, his options %x", |
| 940 | inet_ntoa(ospfh->router_id), OPTIONS (oi), hello->options); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 941 | return; |
| 942 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 943 | |
paul | d3f0d62 | 2004-05-05 15:27:15 +0000 | [diff] [blame] | 944 | /* get neighbour struct */ |
| 945 | nbr = ospf_nbr_get (oi, ospfh, iph, &p); |
| 946 | |
| 947 | /* neighbour must be valid, ospf_nbr_get creates if none existed */ |
| 948 | assert (nbr); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 949 | |
| 950 | old_state = nbr->state; |
| 951 | |
| 952 | /* Add event to thread. */ |
Paul Jakma | 57c5c65 | 2010-01-07 06:12:53 +0000 | [diff] [blame] | 953 | OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_PacketReceived); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 954 | |
| 955 | /* RFC2328 Section 9.5.1 |
| 956 | If the router is not eligible to become Designated Router, |
| 957 | (snip) It must also send an Hello Packet in reply to an |
| 958 | Hello Packet received from any eligible neighbor (other than |
| 959 | the current Designated Router and Backup Designated Router). */ |
| 960 | if (oi->type == OSPF_IFTYPE_NBMA) |
| 961 | if (PRIORITY(oi) == 0 && hello->priority > 0 |
| 962 | && IPV4_ADDR_CMP(&DR(oi), &iph->ip_src) |
| 963 | && IPV4_ADDR_CMP(&BDR(oi), &iph->ip_src)) |
| 964 | OSPF_NSM_TIMER_ON (nbr->t_hello_reply, ospf_hello_reply_timer, |
| 965 | OSPF_HELLO_REPLY_DELAY); |
| 966 | |
| 967 | /* on NBMA network type, it happens to receive bidirectional Hello packet |
| 968 | without advance 1-Way Received event. |
| 969 | To avoid incorrect DR-seletion, raise 1-Way Received event.*/ |
| 970 | if (oi->type == OSPF_IFTYPE_NBMA && |
| 971 | (old_state == NSM_Down || old_state == NSM_Attempt)) |
| 972 | { |
Paul Jakma | 57c5c65 | 2010-01-07 06:12:53 +0000 | [diff] [blame] | 973 | OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_OneWayReceived); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 974 | nbr->priority = hello->priority; |
| 975 | nbr->d_router = hello->d_router; |
| 976 | nbr->bd_router = hello->bd_router; |
| 977 | return; |
| 978 | } |
| 979 | |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 980 | if (ospf_nbr_bidirectional (&oi->ospf->router_id, hello->neighbors, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 981 | size - OSPF_HELLO_MIN_SIZE)) |
| 982 | { |
Paul Jakma | 57c5c65 | 2010-01-07 06:12:53 +0000 | [diff] [blame] | 983 | OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_TwoWayReceived); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 984 | nbr->options |= hello->options; |
| 985 | } |
| 986 | else |
| 987 | { |
Paul Jakma | 57c5c65 | 2010-01-07 06:12:53 +0000 | [diff] [blame] | 988 | OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_OneWayReceived); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 989 | /* Set neighbor information. */ |
| 990 | nbr->priority = hello->priority; |
| 991 | nbr->d_router = hello->d_router; |
| 992 | nbr->bd_router = hello->bd_router; |
| 993 | return; |
| 994 | } |
| 995 | |
| 996 | /* If neighbor itself declares DR and no BDR exists, |
| 997 | cause event BackupSeen */ |
| 998 | if (IPV4_ADDR_SAME (&nbr->address.u.prefix4, &hello->d_router)) |
| 999 | if (hello->bd_router.s_addr == 0 && oi->state == ISM_Waiting) |
| 1000 | OSPF_ISM_EVENT_SCHEDULE (oi, ISM_BackupSeen); |
| 1001 | |
| 1002 | /* neighbor itself declares BDR. */ |
| 1003 | if (oi->state == ISM_Waiting && |
| 1004 | IPV4_ADDR_SAME (&nbr->address.u.prefix4, &hello->bd_router)) |
| 1005 | OSPF_ISM_EVENT_SCHEDULE (oi, ISM_BackupSeen); |
| 1006 | |
| 1007 | /* had not previously. */ |
| 1008 | if ((IPV4_ADDR_SAME (&nbr->address.u.prefix4, &hello->d_router) && |
| 1009 | IPV4_ADDR_CMP (&nbr->address.u.prefix4, &nbr->d_router)) || |
| 1010 | (IPV4_ADDR_CMP (&nbr->address.u.prefix4, &hello->d_router) && |
| 1011 | IPV4_ADDR_SAME (&nbr->address.u.prefix4, &nbr->d_router))) |
| 1012 | OSPF_ISM_EVENT_SCHEDULE (oi, ISM_NeighborChange); |
| 1013 | |
| 1014 | /* had not previously. */ |
| 1015 | if ((IPV4_ADDR_SAME (&nbr->address.u.prefix4, &hello->bd_router) && |
| 1016 | IPV4_ADDR_CMP (&nbr->address.u.prefix4, &nbr->bd_router)) || |
| 1017 | (IPV4_ADDR_CMP (&nbr->address.u.prefix4, &hello->bd_router) && |
| 1018 | IPV4_ADDR_SAME (&nbr->address.u.prefix4, &nbr->bd_router))) |
| 1019 | OSPF_ISM_EVENT_SCHEDULE (oi, ISM_NeighborChange); |
| 1020 | |
| 1021 | /* Neighbor priority check. */ |
| 1022 | if (nbr->priority >= 0 && nbr->priority != hello->priority) |
| 1023 | OSPF_ISM_EVENT_SCHEDULE (oi, ISM_NeighborChange); |
| 1024 | |
| 1025 | /* Set neighbor information. */ |
| 1026 | nbr->priority = hello->priority; |
| 1027 | nbr->d_router = hello->d_router; |
| 1028 | nbr->bd_router = hello->bd_router; |
| 1029 | } |
| 1030 | |
| 1031 | /* Save DD flags/options/Seqnum received. */ |
paul | 4dadc29 | 2005-05-06 21:37:42 +0000 | [diff] [blame] | 1032 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1033 | ospf_db_desc_save_current (struct ospf_neighbor *nbr, |
| 1034 | struct ospf_db_desc *dd) |
| 1035 | { |
| 1036 | nbr->last_recv.flags = dd->flags; |
| 1037 | nbr->last_recv.options = dd->options; |
| 1038 | nbr->last_recv.dd_seqnum = ntohl (dd->dd_seqnum); |
| 1039 | } |
| 1040 | |
| 1041 | /* Process rest of DD packet. */ |
| 1042 | static void |
| 1043 | ospf_db_desc_proc (struct stream *s, struct ospf_interface *oi, |
| 1044 | struct ospf_neighbor *nbr, struct ospf_db_desc *dd, |
| 1045 | u_int16_t size) |
| 1046 | { |
| 1047 | struct ospf_lsa *new, *find; |
| 1048 | struct lsa_header *lsah; |
| 1049 | |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 1050 | stream_forward_getp (s, OSPF_DB_DESC_MIN_SIZE); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1051 | for (size -= OSPF_DB_DESC_MIN_SIZE; |
| 1052 | size >= OSPF_LSA_HEADER_SIZE; size -= OSPF_LSA_HEADER_SIZE) |
| 1053 | { |
| 1054 | lsah = (struct lsa_header *) STREAM_PNT (s); |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 1055 | stream_forward_getp (s, OSPF_LSA_HEADER_SIZE); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1056 | |
| 1057 | /* Unknown LS type. */ |
| 1058 | if (lsah->type < OSPF_MIN_LSA || lsah->type >= OSPF_MAX_LSA) |
| 1059 | { |
ajs | bec595a | 2004-11-30 22:38:43 +0000 | [diff] [blame] | 1060 | zlog_warn ("Packet [DD:RECV]: Unknown LS type %d.", lsah->type); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1061 | OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_SeqNumberMismatch); |
| 1062 | return; |
| 1063 | } |
| 1064 | |
| 1065 | #ifdef HAVE_OPAQUE_LSA |
| 1066 | if (IS_OPAQUE_LSA (lsah->type) |
| 1067 | && ! CHECK_FLAG (nbr->options, OSPF_OPTION_O)) |
| 1068 | { |
| 1069 | zlog_warn ("LSA[Type%d:%s]: Opaque capability mismatch?", lsah->type, inet_ntoa (lsah->id)); |
| 1070 | OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_SeqNumberMismatch); |
| 1071 | return; |
| 1072 | } |
| 1073 | #endif /* HAVE_OPAQUE_LSA */ |
| 1074 | |
| 1075 | switch (lsah->type) |
| 1076 | { |
| 1077 | case OSPF_AS_EXTERNAL_LSA: |
| 1078 | #ifdef HAVE_OPAQUE_LSA |
| 1079 | case OSPF_OPAQUE_AS_LSA: |
| 1080 | #endif /* HAVE_OPAQUE_LSA */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1081 | /* Check for stub area. Reject if AS-External from stub but |
| 1082 | allow if from NSSA. */ |
| 1083 | if (oi->area->external_routing == OSPF_AREA_STUB) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1084 | { |
| 1085 | zlog_warn ("Packet [DD:RECV]: LSA[Type%d:%s] from %s area.", |
| 1086 | lsah->type, inet_ntoa (lsah->id), |
| 1087 | (oi->area->external_routing == OSPF_AREA_STUB) ?\ |
| 1088 | "STUB" : "NSSA"); |
| 1089 | OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_SeqNumberMismatch); |
| 1090 | return; |
| 1091 | } |
| 1092 | break; |
| 1093 | default: |
| 1094 | break; |
| 1095 | } |
| 1096 | |
| 1097 | /* Create LS-request object. */ |
| 1098 | new = ospf_ls_request_new (lsah); |
| 1099 | |
| 1100 | /* Lookup received LSA, then add LS request list. */ |
| 1101 | find = ospf_lsa_lookup_by_header (oi->area, lsah); |
Paul Jakma | f0894cf | 2006-08-27 06:40:04 +0000 | [diff] [blame] | 1102 | |
| 1103 | /* ospf_lsa_more_recent is fine with NULL pointers */ |
| 1104 | switch (ospf_lsa_more_recent (find, new)) |
| 1105 | { |
| 1106 | case -1: |
| 1107 | /* Neighbour has a more recent LSA, we must request it */ |
| 1108 | ospf_ls_request_add (nbr, new); |
| 1109 | case 0: |
| 1110 | /* If we have a copy of this LSA, it's either less recent |
| 1111 | * and we're requesting it from neighbour (the case above), or |
| 1112 | * it's as recent and we both have same copy (this case). |
| 1113 | * |
| 1114 | * In neither of these two cases is there any point in |
| 1115 | * describing our copy of the LSA to the neighbour in a |
| 1116 | * DB-Summary packet, if we're still intending to do so. |
| 1117 | * |
| 1118 | * See: draft-ogier-ospf-dbex-opt-00.txt, describing the |
| 1119 | * backward compatible optimisation to OSPF DB Exchange / |
| 1120 | * DB Description process implemented here. |
| 1121 | */ |
| 1122 | if (find) |
| 1123 | ospf_lsdb_delete (&nbr->db_sum, find); |
| 1124 | ospf_lsa_discard (new); |
| 1125 | break; |
| 1126 | default: |
| 1127 | /* We have the more recent copy, nothing specific to do: |
| 1128 | * - no need to request neighbours stale copy |
| 1129 | * - must leave DB summary list copy alone |
| 1130 | */ |
| 1131 | if (IS_DEBUG_OSPF_EVENT) |
| 1132 | zlog_debug ("Packet [DD:RECV]: LSA received Type %d, " |
| 1133 | "ID %s is not recent.", lsah->type, inet_ntoa (lsah->id)); |
| 1134 | ospf_lsa_discard (new); |
| 1135 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1136 | } |
| 1137 | |
| 1138 | /* Master */ |
| 1139 | if (IS_SET_DD_MS (nbr->dd_flags)) |
| 1140 | { |
| 1141 | nbr->dd_seqnum++; |
Paul Jakma | 8dd24ee | 2006-08-27 06:29:30 +0000 | [diff] [blame] | 1142 | |
| 1143 | /* Both sides have no More, then we're done with Exchange */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1144 | if (!IS_SET_DD_M (dd->flags) && !IS_SET_DD_M (nbr->dd_flags)) |
| 1145 | OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_ExchangeDone); |
| 1146 | else |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1147 | ospf_db_desc_send (nbr); |
| 1148 | } |
| 1149 | /* Slave */ |
| 1150 | else |
| 1151 | { |
| 1152 | nbr->dd_seqnum = ntohl (dd->dd_seqnum); |
| 1153 | |
Paul Jakma | 8dd24ee | 2006-08-27 06:29:30 +0000 | [diff] [blame] | 1154 | /* Send DD packet in reply. |
| 1155 | * |
| 1156 | * Must be done to acknowledge the Master's DD, regardless of |
| 1157 | * whether we have more LSAs ourselves to describe. |
| 1158 | * |
| 1159 | * This function will clear the 'More' bit, if after this DD |
| 1160 | * we have no more LSAs to describe to the master.. |
| 1161 | */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1162 | ospf_db_desc_send (nbr); |
Paul Jakma | 8dd24ee | 2006-08-27 06:29:30 +0000 | [diff] [blame] | 1163 | |
| 1164 | /* Slave can raise ExchangeDone now, if master is also done */ |
| 1165 | if (!IS_SET_DD_M (dd->flags) && !IS_SET_DD_M (nbr->dd_flags)) |
| 1166 | OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_ExchangeDone); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1167 | } |
Paul Jakma | 8dd24ee | 2006-08-27 06:29:30 +0000 | [diff] [blame] | 1168 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1169 | /* Save received neighbor values from DD. */ |
| 1170 | ospf_db_desc_save_current (nbr, dd); |
| 1171 | } |
| 1172 | |
paul | 4dadc29 | 2005-05-06 21:37:42 +0000 | [diff] [blame] | 1173 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1174 | ospf_db_desc_is_dup (struct ospf_db_desc *dd, struct ospf_neighbor *nbr) |
| 1175 | { |
| 1176 | /* Is DD duplicated? */ |
| 1177 | if (dd->options == nbr->last_recv.options && |
| 1178 | dd->flags == nbr->last_recv.flags && |
| 1179 | dd->dd_seqnum == htonl (nbr->last_recv.dd_seqnum)) |
| 1180 | return 1; |
| 1181 | |
| 1182 | return 0; |
| 1183 | } |
| 1184 | |
| 1185 | /* OSPF Database Description message read -- RFC2328 Section 10.6. */ |
ajs | 3aa8d5f | 2004-12-11 18:00:06 +0000 | [diff] [blame] | 1186 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1187 | ospf_db_desc (struct ip *iph, struct ospf_header *ospfh, |
| 1188 | struct stream *s, struct ospf_interface *oi, u_int16_t size) |
| 1189 | { |
| 1190 | struct ospf_db_desc *dd; |
| 1191 | struct ospf_neighbor *nbr; |
| 1192 | |
| 1193 | /* Increment statistics. */ |
| 1194 | oi->db_desc_in++; |
| 1195 | |
| 1196 | dd = (struct ospf_db_desc *) STREAM_PNT (s); |
paul | d363df2 | 2003-06-19 00:26:34 +0000 | [diff] [blame] | 1197 | |
paul | d3f0d62 | 2004-05-05 15:27:15 +0000 | [diff] [blame] | 1198 | nbr = ospf_nbr_lookup (oi, iph, ospfh); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1199 | if (nbr == NULL) |
| 1200 | { |
| 1201 | zlog_warn ("Packet[DD]: Unknown Neighbor %s", |
| 1202 | inet_ntoa (ospfh->router_id)); |
| 1203 | return; |
| 1204 | } |
| 1205 | |
| 1206 | /* Check MTU. */ |
vincent | ba68253 | 2005-09-29 13:52:57 +0000 | [diff] [blame] | 1207 | if ((OSPF_IF_PARAM (oi, mtu_ignore) == 0) && |
| 1208 | (ntohs (dd->mtu) > oi->ifp->mtu)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1209 | { |
ajs | 3aa8d5f | 2004-12-11 18:00:06 +0000 | [diff] [blame] | 1210 | zlog_warn ("Packet[DD]: Neighbor %s MTU %u is larger than [%s]'s MTU %u", |
| 1211 | inet_ntoa (nbr->router_id), ntohs (dd->mtu), |
| 1212 | IF_NAME (oi), oi->ifp->mtu); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1213 | return; |
| 1214 | } |
| 1215 | |
paul | d363df2 | 2003-06-19 00:26:34 +0000 | [diff] [blame] | 1216 | /* |
| 1217 | * XXX HACK by Hasso Tepper. Setting N/P bit in NSSA area DD packets is not |
| 1218 | * required. In fact at least JunOS sends DD packets with P bit clear. |
| 1219 | * Until proper solution is developped, this hack should help. |
| 1220 | * |
| 1221 | * Update: According to the RFCs, N bit is specified /only/ for Hello |
| 1222 | * options, unfortunately its use in DD options is not specified. Hence some |
| 1223 | * implementations follow E-bit semantics and set it in DD options, and some |
| 1224 | * treat it as unspecified and hence follow the directive "default for |
| 1225 | * options is clear", ie unset. |
| 1226 | * |
| 1227 | * Reset the flag, as ospfd follows E-bit semantics. |
| 1228 | */ |
| 1229 | if ( (oi->area->external_routing == OSPF_AREA_NSSA) |
| 1230 | && (CHECK_FLAG (nbr->options, OSPF_OPTION_NP)) |
| 1231 | && (!CHECK_FLAG (dd->options, OSPF_OPTION_NP)) ) |
| 1232 | { |
| 1233 | if (IS_DEBUG_OSPF_EVENT) |
ajs | 1210fa6 | 2004-12-03 16:43:24 +0000 | [diff] [blame] | 1234 | zlog_debug ("Packet[DD]: Neighbour %s: Has NSSA capability, sends with N bit clear in DD options", |
paul | d363df2 | 2003-06-19 00:26:34 +0000 | [diff] [blame] | 1235 | inet_ntoa (nbr->router_id) ); |
| 1236 | SET_FLAG (dd->options, OSPF_OPTION_NP); |
| 1237 | } |
paul | d363df2 | 2003-06-19 00:26:34 +0000 | [diff] [blame] | 1238 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1239 | #ifdef REJECT_IF_TBIT_ON |
| 1240 | if (CHECK_FLAG (dd->options, OSPF_OPTION_T)) |
| 1241 | { |
| 1242 | /* |
| 1243 | * In Hello protocol, optional capability must have checked |
| 1244 | * to prevent this T-bit enabled router be my neighbor. |
| 1245 | */ |
| 1246 | zlog_warn ("Packet[DD]: Neighbor %s: T-bit on?", inet_ntoa (nbr->router_id)); |
| 1247 | return; |
| 1248 | } |
| 1249 | #endif /* REJECT_IF_TBIT_ON */ |
| 1250 | |
| 1251 | #ifdef HAVE_OPAQUE_LSA |
| 1252 | if (CHECK_FLAG (dd->options, OSPF_OPTION_O) |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 1253 | && !CHECK_FLAG (oi->ospf->config, OSPF_OPAQUE_CAPABLE)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1254 | { |
| 1255 | /* |
| 1256 | * This node is not configured to handle O-bit, for now. |
| 1257 | * Clear it to ignore unsupported capability proposed by neighbor. |
| 1258 | */ |
| 1259 | UNSET_FLAG (dd->options, OSPF_OPTION_O); |
| 1260 | } |
| 1261 | #endif /* HAVE_OPAQUE_LSA */ |
| 1262 | |
Paul Jakma | 57c5c65 | 2010-01-07 06:12:53 +0000 | [diff] [blame] | 1263 | /* Add event to thread. */ |
| 1264 | OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_PacketReceived); |
| 1265 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1266 | /* Process DD packet by neighbor status. */ |
| 1267 | switch (nbr->state) |
| 1268 | { |
| 1269 | case NSM_Down: |
| 1270 | case NSM_Attempt: |
| 1271 | case NSM_TwoWay: |
ajs | bec595a | 2004-11-30 22:38:43 +0000 | [diff] [blame] | 1272 | zlog_warn ("Packet[DD]: Neighbor %s state is %s, packet discarded.", |
ajs | 3aa8d5f | 2004-12-11 18:00:06 +0000 | [diff] [blame] | 1273 | inet_ntoa(nbr->router_id), |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1274 | LOOKUP (ospf_nsm_state_msg, nbr->state)); |
| 1275 | break; |
| 1276 | case NSM_Init: |
| 1277 | OSPF_NSM_EVENT_EXECUTE (nbr, NSM_TwoWayReceived); |
| 1278 | /* If the new state is ExStart, the processing of the current |
| 1279 | packet should then continue in this new state by falling |
| 1280 | through to case ExStart below. */ |
| 1281 | if (nbr->state != NSM_ExStart) |
| 1282 | break; |
| 1283 | case NSM_ExStart: |
| 1284 | /* Initial DBD */ |
| 1285 | if ((IS_SET_DD_ALL (dd->flags) == OSPF_DD_FLAG_ALL) && |
| 1286 | (size == OSPF_DB_DESC_MIN_SIZE)) |
| 1287 | { |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 1288 | if (IPV4_ADDR_CMP (&nbr->router_id, &oi->ospf->router_id) > 0) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1289 | { |
| 1290 | /* We're Slave---obey */ |
ajs | 17eaa72 | 2004-12-29 21:04:48 +0000 | [diff] [blame] | 1291 | zlog_info ("Packet[DD]: Neighbor %s Negotiation done (Slave).", |
ajs | 3aa8d5f | 2004-12-11 18:00:06 +0000 | [diff] [blame] | 1292 | inet_ntoa(nbr->router_id)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1293 | nbr->dd_seqnum = ntohl (dd->dd_seqnum); |
Paul Jakma | 8dd24ee | 2006-08-27 06:29:30 +0000 | [diff] [blame] | 1294 | |
| 1295 | /* Reset I/MS */ |
| 1296 | UNSET_FLAG (nbr->dd_flags, (OSPF_DD_FLAG_MS|OSPF_DD_FLAG_I)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1297 | } |
| 1298 | else |
| 1299 | { |
| 1300 | /* We're Master, ignore the initial DBD from Slave */ |
paul | 6d45276 | 2005-11-03 11:15:44 +0000 | [diff] [blame] | 1301 | zlog_info ("Packet[DD]: Neighbor %s: Initial DBD from Slave, " |
ajs | 3aa8d5f | 2004-12-11 18:00:06 +0000 | [diff] [blame] | 1302 | "ignoring.", inet_ntoa(nbr->router_id)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1303 | break; |
| 1304 | } |
| 1305 | } |
| 1306 | /* Ack from the Slave */ |
| 1307 | else if (!IS_SET_DD_MS (dd->flags) && !IS_SET_DD_I (dd->flags) && |
| 1308 | ntohl (dd->dd_seqnum) == nbr->dd_seqnum && |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 1309 | IPV4_ADDR_CMP (&nbr->router_id, &oi->ospf->router_id) < 0) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1310 | { |
ajs | 17eaa72 | 2004-12-29 21:04:48 +0000 | [diff] [blame] | 1311 | zlog_info ("Packet[DD]: Neighbor %s Negotiation done (Master).", |
ajs | 3aa8d5f | 2004-12-11 18:00:06 +0000 | [diff] [blame] | 1312 | inet_ntoa(nbr->router_id)); |
Paul Jakma | 8dd24ee | 2006-08-27 06:29:30 +0000 | [diff] [blame] | 1313 | /* Reset I, leaving MS */ |
| 1314 | UNSET_FLAG (nbr->dd_flags, OSPF_DD_FLAG_I); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1315 | } |
| 1316 | else |
| 1317 | { |
ajs | 3aa8d5f | 2004-12-11 18:00:06 +0000 | [diff] [blame] | 1318 | zlog_warn ("Packet[DD]: Neighbor %s Negotiation fails.", |
| 1319 | inet_ntoa(nbr->router_id)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1320 | break; |
| 1321 | } |
| 1322 | |
| 1323 | /* This is where the real Options are saved */ |
| 1324 | nbr->options = dd->options; |
| 1325 | |
| 1326 | #ifdef HAVE_OPAQUE_LSA |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 1327 | if (CHECK_FLAG (oi->ospf->config, OSPF_OPAQUE_CAPABLE)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1328 | { |
| 1329 | if (IS_DEBUG_OSPF_EVENT) |
ajs | 2a42e28 | 2004-12-08 18:43:03 +0000 | [diff] [blame] | 1330 | zlog_debug ("Neighbor[%s] is %sOpaque-capable.", |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1331 | inet_ntoa (nbr->router_id), |
| 1332 | CHECK_FLAG (nbr->options, OSPF_OPTION_O) ? "" : "NOT "); |
| 1333 | |
| 1334 | if (! CHECK_FLAG (nbr->options, OSPF_OPTION_O) |
| 1335 | && IPV4_ADDR_SAME (&DR (oi), &nbr->address.u.prefix4)) |
| 1336 | { |
paul | 6d45276 | 2005-11-03 11:15:44 +0000 | [diff] [blame] | 1337 | zlog_warn ("DR-neighbor[%s] is NOT opaque-capable; " |
| 1338 | "Opaque-LSAs cannot be reliably advertised " |
| 1339 | "in this network.", |
| 1340 | inet_ntoa (nbr->router_id)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1341 | /* This situation is undesirable, but not a real error. */ |
| 1342 | } |
| 1343 | } |
| 1344 | #endif /* HAVE_OPAQUE_LSA */ |
| 1345 | |
| 1346 | OSPF_NSM_EVENT_EXECUTE (nbr, NSM_NegotiationDone); |
| 1347 | |
| 1348 | /* continue processing rest of packet. */ |
| 1349 | ospf_db_desc_proc (s, oi, nbr, dd, size); |
| 1350 | break; |
| 1351 | case NSM_Exchange: |
| 1352 | if (ospf_db_desc_is_dup (dd, nbr)) |
| 1353 | { |
| 1354 | if (IS_SET_DD_MS (nbr->dd_flags)) |
| 1355 | /* Master: discard duplicated DD packet. */ |
paul | 6d45276 | 2005-11-03 11:15:44 +0000 | [diff] [blame] | 1356 | zlog_info ("Packet[DD] (Master): Neighbor %s packet duplicated.", |
ajs | 3aa8d5f | 2004-12-11 18:00:06 +0000 | [diff] [blame] | 1357 | inet_ntoa (nbr->router_id)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1358 | else |
| 1359 | /* Slave: cause to retransmit the last Database Description. */ |
| 1360 | { |
paul | 6d45276 | 2005-11-03 11:15:44 +0000 | [diff] [blame] | 1361 | zlog_info ("Packet[DD] [Slave]: Neighbor %s packet duplicated.", |
ajs | 3aa8d5f | 2004-12-11 18:00:06 +0000 | [diff] [blame] | 1362 | inet_ntoa (nbr->router_id)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1363 | ospf_db_desc_resend (nbr); |
| 1364 | } |
| 1365 | break; |
| 1366 | } |
| 1367 | |
| 1368 | /* Otherwise DD packet should be checked. */ |
| 1369 | /* Check Master/Slave bit mismatch */ |
| 1370 | if (IS_SET_DD_MS (dd->flags) != IS_SET_DD_MS (nbr->last_recv.flags)) |
| 1371 | { |
ajs | 3aa8d5f | 2004-12-11 18:00:06 +0000 | [diff] [blame] | 1372 | zlog_warn ("Packet[DD]: Neighbor %s MS-bit mismatch.", |
| 1373 | inet_ntoa(nbr->router_id)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1374 | OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_SeqNumberMismatch); |
| 1375 | if (IS_DEBUG_OSPF_EVENT) |
ajs | 2a42e28 | 2004-12-08 18:43:03 +0000 | [diff] [blame] | 1376 | zlog_debug ("Packet[DD]: dd->flags=%d, nbr->dd_flags=%d", |
ajs | 3aa8d5f | 2004-12-11 18:00:06 +0000 | [diff] [blame] | 1377 | dd->flags, nbr->dd_flags); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1378 | break; |
| 1379 | } |
| 1380 | |
| 1381 | /* Check initialize bit is set. */ |
| 1382 | if (IS_SET_DD_I (dd->flags)) |
| 1383 | { |
paul | 6d45276 | 2005-11-03 11:15:44 +0000 | [diff] [blame] | 1384 | zlog_info ("Packet[DD]: Neighbor %s I-bit set.", |
ajs | 3aa8d5f | 2004-12-11 18:00:06 +0000 | [diff] [blame] | 1385 | inet_ntoa(nbr->router_id)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1386 | OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_SeqNumberMismatch); |
| 1387 | break; |
| 1388 | } |
| 1389 | |
| 1390 | /* Check DD Options. */ |
| 1391 | if (dd->options != nbr->options) |
| 1392 | { |
| 1393 | #ifdef ORIGINAL_CODING |
| 1394 | /* Save the new options for debugging */ |
| 1395 | nbr->options = dd->options; |
| 1396 | #endif /* ORIGINAL_CODING */ |
ajs | 3aa8d5f | 2004-12-11 18:00:06 +0000 | [diff] [blame] | 1397 | zlog_warn ("Packet[DD]: Neighbor %s options mismatch.", |
| 1398 | inet_ntoa(nbr->router_id)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1399 | OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_SeqNumberMismatch); |
| 1400 | break; |
| 1401 | } |
| 1402 | |
| 1403 | /* Check DD sequence number. */ |
| 1404 | if ((IS_SET_DD_MS (nbr->dd_flags) && |
| 1405 | ntohl (dd->dd_seqnum) != nbr->dd_seqnum) || |
| 1406 | (!IS_SET_DD_MS (nbr->dd_flags) && |
| 1407 | ntohl (dd->dd_seqnum) != nbr->dd_seqnum + 1)) |
| 1408 | { |
ajs | 3aa8d5f | 2004-12-11 18:00:06 +0000 | [diff] [blame] | 1409 | zlog_warn ("Packet[DD]: Neighbor %s sequence number mismatch.", |
| 1410 | inet_ntoa(nbr->router_id)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1411 | OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_SeqNumberMismatch); |
| 1412 | break; |
| 1413 | } |
| 1414 | |
| 1415 | /* Continue processing rest of packet. */ |
| 1416 | ospf_db_desc_proc (s, oi, nbr, dd, size); |
| 1417 | break; |
| 1418 | case NSM_Loading: |
| 1419 | case NSM_Full: |
| 1420 | if (ospf_db_desc_is_dup (dd, nbr)) |
| 1421 | { |
| 1422 | if (IS_SET_DD_MS (nbr->dd_flags)) |
| 1423 | { |
| 1424 | /* Master should discard duplicate DD packet. */ |
paul | 6d45276 | 2005-11-03 11:15:44 +0000 | [diff] [blame] | 1425 | zlog_info ("Packet[DD]: Neighbor %s duplicated, " |
| 1426 | "packet discarded.", |
ajs | 3aa8d5f | 2004-12-11 18:00:06 +0000 | [diff] [blame] | 1427 | inet_ntoa(nbr->router_id)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1428 | break; |
| 1429 | } |
| 1430 | else |
| 1431 | { |
| 1432 | struct timeval t, now; |
Paul Jakma | 2518efd | 2006-08-27 06:49:29 +0000 | [diff] [blame] | 1433 | quagga_gettime (QUAGGA_CLK_MONOTONIC, &now); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1434 | t = tv_sub (now, nbr->last_send_ts); |
| 1435 | if (tv_cmp (t, int2tv (nbr->v_inactivity)) < 0) |
| 1436 | { |
| 1437 | /* In states Loading and Full the slave must resend |
| 1438 | its last Database Description packet in response to |
| 1439 | duplicate Database Description packets received |
| 1440 | from the master. For this reason the slave must |
| 1441 | wait RouterDeadInterval seconds before freeing the |
| 1442 | last Database Description packet. Reception of a |
| 1443 | Database Description packet from the master after |
| 1444 | this interval will generate a SeqNumberMismatch |
| 1445 | neighbor event. RFC2328 Section 10.8 */ |
| 1446 | ospf_db_desc_resend (nbr); |
| 1447 | break; |
| 1448 | } |
| 1449 | } |
| 1450 | } |
| 1451 | |
| 1452 | OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_SeqNumberMismatch); |
| 1453 | break; |
| 1454 | default: |
ajs | 3aa8d5f | 2004-12-11 18:00:06 +0000 | [diff] [blame] | 1455 | zlog_warn ("Packet[DD]: Neighbor %s NSM illegal status %u.", |
| 1456 | inet_ntoa(nbr->router_id), nbr->state); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1457 | break; |
| 1458 | } |
| 1459 | } |
| 1460 | |
| 1461 | #define OSPF_LSA_KEY_SIZE 12 /* type(4) + id(4) + ar(4) */ |
| 1462 | |
| 1463 | /* OSPF Link State Request Read -- RFC2328 Section 10.7. */ |
paul | 4dadc29 | 2005-05-06 21:37:42 +0000 | [diff] [blame] | 1464 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1465 | ospf_ls_req (struct ip *iph, struct ospf_header *ospfh, |
| 1466 | struct stream *s, struct ospf_interface *oi, u_int16_t size) |
| 1467 | { |
| 1468 | struct ospf_neighbor *nbr; |
| 1469 | u_int32_t ls_type; |
| 1470 | struct in_addr ls_id; |
| 1471 | struct in_addr adv_router; |
| 1472 | struct ospf_lsa *find; |
hasso | 52dc7ee | 2004-09-23 19:18:23 +0000 | [diff] [blame] | 1473 | struct list *ls_upd; |
paul | 6c83567 | 2004-10-11 11:00:30 +0000 | [diff] [blame] | 1474 | unsigned int length; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1475 | |
| 1476 | /* Increment statistics. */ |
| 1477 | oi->ls_req_in++; |
| 1478 | |
paul | d3f0d62 | 2004-05-05 15:27:15 +0000 | [diff] [blame] | 1479 | nbr = ospf_nbr_lookup (oi, iph, ospfh); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1480 | if (nbr == NULL) |
| 1481 | { |
| 1482 | zlog_warn ("Link State Request: Unknown Neighbor %s.", |
| 1483 | inet_ntoa (ospfh->router_id)); |
| 1484 | return; |
| 1485 | } |
| 1486 | |
Paul Jakma | 57c5c65 | 2010-01-07 06:12:53 +0000 | [diff] [blame] | 1487 | /* Add event to thread. */ |
| 1488 | OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_PacketReceived); |
| 1489 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1490 | /* Neighbor State should be Exchange or later. */ |
| 1491 | if (nbr->state != NSM_Exchange && |
| 1492 | nbr->state != NSM_Loading && |
| 1493 | nbr->state != NSM_Full) |
| 1494 | { |
ajs | bec595a | 2004-11-30 22:38:43 +0000 | [diff] [blame] | 1495 | zlog_warn ("Link State Request received from %s: " |
| 1496 | "Neighbor state is %s, packet discarded.", |
| 1497 | inet_ntoa (ospfh->router_id), |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1498 | LOOKUP (ospf_nsm_state_msg, nbr->state)); |
| 1499 | return; |
| 1500 | } |
| 1501 | |
| 1502 | /* Send Link State Update for ALL requested LSAs. */ |
| 1503 | ls_upd = list_new (); |
| 1504 | length = OSPF_HEADER_SIZE + OSPF_LS_UPD_MIN_SIZE; |
| 1505 | |
| 1506 | while (size >= OSPF_LSA_KEY_SIZE) |
| 1507 | { |
| 1508 | /* Get one slice of Link State Request. */ |
| 1509 | ls_type = stream_getl (s); |
| 1510 | ls_id.s_addr = stream_get_ipv4 (s); |
| 1511 | adv_router.s_addr = stream_get_ipv4 (s); |
| 1512 | |
| 1513 | /* Verify LSA type. */ |
| 1514 | if (ls_type < OSPF_MIN_LSA || ls_type >= OSPF_MAX_LSA) |
| 1515 | { |
| 1516 | OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_BadLSReq); |
| 1517 | list_delete (ls_upd); |
| 1518 | return; |
| 1519 | } |
| 1520 | |
| 1521 | /* Search proper LSA in LSDB. */ |
| 1522 | find = ospf_lsa_lookup (oi->area, ls_type, ls_id, adv_router); |
| 1523 | if (find == NULL) |
| 1524 | { |
| 1525 | OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_BadLSReq); |
| 1526 | list_delete (ls_upd); |
| 1527 | return; |
| 1528 | } |
| 1529 | |
gdt | 86f1fd9 | 2005-01-10 14:20:43 +0000 | [diff] [blame] | 1530 | /* Packet overflows MTU size, send immediately. */ |
| 1531 | if (length + ntohs (find->data->length) > ospf_packet_max (oi)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1532 | { |
| 1533 | if (oi->type == OSPF_IFTYPE_NBMA) |
| 1534 | ospf_ls_upd_send (nbr, ls_upd, OSPF_SEND_PACKET_DIRECT); |
| 1535 | else |
| 1536 | ospf_ls_upd_send (nbr, ls_upd, OSPF_SEND_PACKET_INDIRECT); |
| 1537 | |
| 1538 | /* Only remove list contents. Keep ls_upd. */ |
| 1539 | list_delete_all_node (ls_upd); |
| 1540 | |
| 1541 | length = OSPF_HEADER_SIZE + OSPF_LS_UPD_MIN_SIZE; |
| 1542 | } |
| 1543 | |
| 1544 | /* Append LSA to update list. */ |
| 1545 | listnode_add (ls_upd, find); |
| 1546 | length += ntohs (find->data->length); |
| 1547 | |
| 1548 | size -= OSPF_LSA_KEY_SIZE; |
| 1549 | } |
| 1550 | |
| 1551 | /* Send rest of Link State Update. */ |
| 1552 | if (listcount (ls_upd) > 0) |
| 1553 | { |
| 1554 | if (oi->type == OSPF_IFTYPE_NBMA) |
| 1555 | ospf_ls_upd_send (nbr, ls_upd, OSPF_SEND_PACKET_DIRECT); |
| 1556 | else |
| 1557 | ospf_ls_upd_send (nbr, ls_upd, OSPF_SEND_PACKET_INDIRECT); |
| 1558 | |
| 1559 | list_delete (ls_upd); |
| 1560 | } |
| 1561 | else |
| 1562 | list_free (ls_upd); |
| 1563 | } |
| 1564 | |
| 1565 | /* Get the list of LSAs from Link State Update packet. |
| 1566 | And process some validation -- RFC2328 Section 13. (1)-(2). */ |
hasso | 52dc7ee | 2004-09-23 19:18:23 +0000 | [diff] [blame] | 1567 | static struct list * |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1568 | ospf_ls_upd_list_lsa (struct ospf_neighbor *nbr, struct stream *s, |
| 1569 | struct ospf_interface *oi, size_t size) |
| 1570 | { |
| 1571 | u_int16_t count, sum; |
| 1572 | u_int32_t length; |
| 1573 | struct lsa_header *lsah; |
| 1574 | struct ospf_lsa *lsa; |
hasso | 52dc7ee | 2004-09-23 19:18:23 +0000 | [diff] [blame] | 1575 | struct list *lsas; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1576 | |
| 1577 | lsas = list_new (); |
| 1578 | |
| 1579 | count = stream_getl (s); |
| 1580 | size -= OSPF_LS_UPD_MIN_SIZE; /* # LSAs */ |
| 1581 | |
| 1582 | for (; size >= OSPF_LSA_HEADER_SIZE && count > 0; |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 1583 | size -= length, stream_forward_getp (s, length), count--) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1584 | { |
| 1585 | lsah = (struct lsa_header *) STREAM_PNT (s); |
| 1586 | length = ntohs (lsah->length); |
| 1587 | |
| 1588 | if (length > size) |
| 1589 | { |
| 1590 | zlog_warn ("Link State Update: LSA length exceeds packet size."); |
| 1591 | break; |
| 1592 | } |
| 1593 | |
| 1594 | /* Validate the LSA's LS checksum. */ |
| 1595 | sum = lsah->checksum; |
JR Rivers | d8a4e42 | 2012-09-13 17:17:36 +0000 | [diff] [blame] | 1596 | if (! ospf_lsa_checksum_valid (lsah)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1597 | { |
Jaroslav Fojtik | 223da1a | 2011-12-11 18:22:16 +0400 | [diff] [blame] | 1598 | /* (bug #685) more details in a one-line message make it possible |
| 1599 | * to identify problem source on the one hand and to have a better |
| 1600 | * chance to compress repeated messages in syslog on the other */ |
| 1601 | zlog_warn ("Link State Update: LSA checksum error %x/%x, ID=%s from: nbr %s, router ID %s, adv router %s", |
| 1602 | sum, lsah->checksum, inet_ntoa (lsah->id), |
| 1603 | inet_ntoa (nbr->src), inet_ntoa (nbr->router_id), |
| 1604 | inet_ntoa (lsah->adv_router)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1605 | continue; |
| 1606 | } |
| 1607 | |
| 1608 | /* Examine the LSA's LS type. */ |
| 1609 | if (lsah->type < OSPF_MIN_LSA || lsah->type >= OSPF_MAX_LSA) |
| 1610 | { |
| 1611 | zlog_warn ("Link State Update: Unknown LS type %d", lsah->type); |
| 1612 | continue; |
| 1613 | } |
| 1614 | |
| 1615 | /* |
| 1616 | * What if the received LSA's age is greater than MaxAge? |
| 1617 | * Treat it as a MaxAge case -- endo. |
| 1618 | */ |
| 1619 | if (ntohs (lsah->ls_age) > OSPF_LSA_MAXAGE) |
| 1620 | lsah->ls_age = htons (OSPF_LSA_MAXAGE); |
| 1621 | |
| 1622 | #ifdef HAVE_OPAQUE_LSA |
| 1623 | if (CHECK_FLAG (nbr->options, OSPF_OPTION_O)) |
| 1624 | { |
| 1625 | #ifdef STRICT_OBIT_USAGE_CHECK |
| 1626 | if ((IS_OPAQUE_LSA(lsah->type) && |
| 1627 | ! CHECK_FLAG (lsah->options, OSPF_OPTION_O)) |
| 1628 | || (! IS_OPAQUE_LSA(lsah->type) && |
| 1629 | CHECK_FLAG (lsah->options, OSPF_OPTION_O))) |
| 1630 | { |
| 1631 | /* |
| 1632 | * This neighbor must know the exact usage of O-bit; |
| 1633 | * the bit will be set in Type-9,10,11 LSAs only. |
| 1634 | */ |
| 1635 | zlog_warn ("LSA[Type%d:%s]: O-bit abuse?", lsah->type, inet_ntoa (lsah->id)); |
| 1636 | continue; |
| 1637 | } |
| 1638 | #endif /* STRICT_OBIT_USAGE_CHECK */ |
| 1639 | |
| 1640 | /* Do not take in AS External Opaque-LSAs if we are a stub. */ |
| 1641 | if (lsah->type == OSPF_OPAQUE_AS_LSA |
| 1642 | && nbr->oi->area->external_routing != OSPF_AREA_DEFAULT) |
| 1643 | { |
| 1644 | if (IS_DEBUG_OSPF_EVENT) |
ajs | 2a42e28 | 2004-12-08 18:43:03 +0000 | [diff] [blame] | 1645 | zlog_debug ("LSA[Type%d:%s]: We are a stub, don't take this LSA.", lsah->type, inet_ntoa (lsah->id)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1646 | continue; |
| 1647 | } |
| 1648 | } |
| 1649 | else if (IS_OPAQUE_LSA(lsah->type)) |
| 1650 | { |
| 1651 | zlog_warn ("LSA[Type%d:%s]: Opaque capability mismatch?", lsah->type, inet_ntoa (lsah->id)); |
| 1652 | continue; |
| 1653 | } |
| 1654 | #endif /* HAVE_OPAQUE_LSA */ |
| 1655 | |
| 1656 | /* Create OSPF LSA instance. */ |
| 1657 | lsa = ospf_lsa_new (); |
| 1658 | |
| 1659 | /* We may wish to put some error checking if type NSSA comes in |
| 1660 | and area not in NSSA mode */ |
| 1661 | switch (lsah->type) |
| 1662 | { |
| 1663 | case OSPF_AS_EXTERNAL_LSA: |
| 1664 | #ifdef HAVE_OPAQUE_LSA |
| 1665 | case OSPF_OPAQUE_AS_LSA: |
Christian Franke | 5895249 | 2013-02-20 10:00:53 +0000 | [diff] [blame] | 1666 | #endif /* HAVE_OPAQUE_LSA */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1667 | lsa->area = NULL; |
| 1668 | break; |
Christian Franke | 5895249 | 2013-02-20 10:00:53 +0000 | [diff] [blame] | 1669 | #ifdef HAVE_OPAQUE_LSA |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1670 | case OSPF_OPAQUE_LINK_LSA: |
| 1671 | lsa->oi = oi; /* Remember incoming interface for flooding control. */ |
| 1672 | /* Fallthrough */ |
| 1673 | #endif /* HAVE_OPAQUE_LSA */ |
| 1674 | default: |
| 1675 | lsa->area = oi->area; |
| 1676 | break; |
| 1677 | } |
| 1678 | |
| 1679 | lsa->data = ospf_lsa_data_new (length); |
| 1680 | memcpy (lsa->data, lsah, length); |
| 1681 | |
| 1682 | if (IS_DEBUG_OSPF_EVENT) |
ajs | 2a42e28 | 2004-12-08 18:43:03 +0000 | [diff] [blame] | 1683 | zlog_debug("LSA[Type%d:%s]: %p new LSA created with Link State Update", |
David Lamparter | eed3c48 | 2015-03-03 08:51:53 +0100 | [diff] [blame] | 1684 | lsa->data->type, inet_ntoa (lsa->data->id), (void *)lsa); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1685 | listnode_add (lsas, lsa); |
| 1686 | } |
| 1687 | |
| 1688 | return lsas; |
| 1689 | } |
| 1690 | |
| 1691 | /* Cleanup Update list. */ |
paul | 4dadc29 | 2005-05-06 21:37:42 +0000 | [diff] [blame] | 1692 | static void |
hasso | 52dc7ee | 2004-09-23 19:18:23 +0000 | [diff] [blame] | 1693 | ospf_upd_list_clean (struct list *lsas) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1694 | { |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 1695 | struct listnode *node, *nnode; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1696 | struct ospf_lsa *lsa; |
| 1697 | |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 1698 | for (ALL_LIST_ELEMENTS (lsas, node, nnode, lsa)) |
| 1699 | ospf_lsa_discard (lsa); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1700 | |
| 1701 | list_delete (lsas); |
| 1702 | } |
| 1703 | |
| 1704 | /* OSPF Link State Update message read -- RFC2328 Section 13. */ |
paul | 4dadc29 | 2005-05-06 21:37:42 +0000 | [diff] [blame] | 1705 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1706 | ospf_ls_upd (struct ip *iph, struct ospf_header *ospfh, |
| 1707 | struct stream *s, struct ospf_interface *oi, u_int16_t size) |
| 1708 | { |
| 1709 | struct ospf_neighbor *nbr; |
hasso | 52dc7ee | 2004-09-23 19:18:23 +0000 | [diff] [blame] | 1710 | struct list *lsas; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 1711 | struct listnode *node, *nnode; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1712 | struct ospf_lsa *lsa = NULL; |
| 1713 | /* unsigned long ls_req_found = 0; */ |
| 1714 | |
| 1715 | /* Dis-assemble the stream, update each entry, re-encapsulate for flooding */ |
| 1716 | |
| 1717 | /* Increment statistics. */ |
| 1718 | oi->ls_upd_in++; |
| 1719 | |
| 1720 | /* Check neighbor. */ |
paul | d3f0d62 | 2004-05-05 15:27:15 +0000 | [diff] [blame] | 1721 | nbr = ospf_nbr_lookup (oi, iph, ospfh); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1722 | if (nbr == NULL) |
| 1723 | { |
| 1724 | zlog_warn ("Link State Update: Unknown Neighbor %s on int: %s", |
| 1725 | inet_ntoa (ospfh->router_id), IF_NAME (oi)); |
| 1726 | return; |
| 1727 | } |
| 1728 | |
Paul Jakma | 57c5c65 | 2010-01-07 06:12:53 +0000 | [diff] [blame] | 1729 | /* Add event to thread. */ |
| 1730 | OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_PacketReceived); |
| 1731 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1732 | /* Check neighbor state. */ |
| 1733 | if (nbr->state < NSM_Exchange) |
| 1734 | { |
ajs | 3aa8d5f | 2004-12-11 18:00:06 +0000 | [diff] [blame] | 1735 | zlog_warn ("Link State Update: " |
| 1736 | "Neighbor[%s] state %s is less than Exchange", |
| 1737 | inet_ntoa (ospfh->router_id), |
| 1738 | LOOKUP(ospf_nsm_state_msg, nbr->state)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1739 | return; |
| 1740 | } |
| 1741 | |
| 1742 | /* Get list of LSAs from Link State Update packet. - Also perorms Stages |
| 1743 | * 1 (validate LSA checksum) and 2 (check for LSA consistent type) |
| 1744 | * of section 13. |
| 1745 | */ |
| 1746 | lsas = ospf_ls_upd_list_lsa (nbr, s, oi, size); |
| 1747 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1748 | #define DISCARD_LSA(L,N) {\ |
| 1749 | if (IS_DEBUG_OSPF_EVENT) \ |
David Lamparter | eed3c48 | 2015-03-03 08:51:53 +0100 | [diff] [blame] | 1750 | zlog_debug ("ospf_lsa_discard() in ospf_ls_upd() point %d: lsa %p" \ |
| 1751 | " Type-%d", N, (void *)lsa, (int) lsa->data->type); \ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1752 | ospf_lsa_discard (L); \ |
| 1753 | continue; } |
| 1754 | |
Andrew Certain | f92c57f | 2012-12-04 13:29:21 -0800 | [diff] [blame] | 1755 | /* Process each LSA received in the one packet. |
| 1756 | * |
| 1757 | * Numbers in parentheses, e.g. (1), (2), etc., and the corresponding |
Andrew Certain | 0798cee | 2012-12-04 13:43:42 -0800 | [diff] [blame] | 1758 | * text below are from the steps in RFC 2328, Section 13. |
Andrew Certain | f92c57f | 2012-12-04 13:29:21 -0800 | [diff] [blame] | 1759 | */ |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 1760 | for (ALL_LIST_ELEMENTS (lsas, node, nnode, lsa)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1761 | { |
| 1762 | struct ospf_lsa *ls_ret, *current; |
| 1763 | int ret = 1; |
| 1764 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1765 | if (IS_DEBUG_OSPF_NSSA) |
| 1766 | { |
| 1767 | char buf1[INET_ADDRSTRLEN]; |
| 1768 | char buf2[INET_ADDRSTRLEN]; |
| 1769 | char buf3[INET_ADDRSTRLEN]; |
| 1770 | |
ajs | 2a42e28 | 2004-12-08 18:43:03 +0000 | [diff] [blame] | 1771 | zlog_debug("LSA Type-%d from %s, ID: %s, ADV: %s", |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1772 | lsa->data->type, |
| 1773 | inet_ntop (AF_INET, &ospfh->router_id, |
| 1774 | buf1, INET_ADDRSTRLEN), |
| 1775 | inet_ntop (AF_INET, &lsa->data->id, |
| 1776 | buf2, INET_ADDRSTRLEN), |
| 1777 | inet_ntop (AF_INET, &lsa->data->adv_router, |
| 1778 | buf3, INET_ADDRSTRLEN)); |
| 1779 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1780 | |
| 1781 | listnode_delete (lsas, lsa); /* We don't need it in list anymore */ |
| 1782 | |
Andrew Certain | f92c57f | 2012-12-04 13:29:21 -0800 | [diff] [blame] | 1783 | /* (1) Validate Checksum - Done above by ospf_ls_upd_list_lsa() */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1784 | |
Andrew Certain | f92c57f | 2012-12-04 13:29:21 -0800 | [diff] [blame] | 1785 | /* (2) LSA Type - Done above by ospf_ls_upd_list_lsa() */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1786 | |
Andrew Certain | f92c57f | 2012-12-04 13:29:21 -0800 | [diff] [blame] | 1787 | /* (3) Do not take in AS External LSAs if we are a stub or NSSA. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1788 | |
| 1789 | /* Do not take in AS NSSA if this neighbor and we are not NSSA */ |
| 1790 | |
| 1791 | /* Do take in Type-7's if we are an NSSA */ |
| 1792 | |
| 1793 | /* If we are also an ABR, later translate them to a Type-5 packet */ |
| 1794 | |
| 1795 | /* Later, an NSSA Re-fresh can Re-fresh Type-7's and an ABR will |
| 1796 | translate them to a separate Type-5 packet. */ |
| 1797 | |
| 1798 | if (lsa->data->type == OSPF_AS_EXTERNAL_LSA) |
| 1799 | /* Reject from STUB or NSSA */ |
| 1800 | if (nbr->oi->area->external_routing != OSPF_AREA_DEFAULT) |
| 1801 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1802 | if (IS_DEBUG_OSPF_NSSA) |
ajs | 2a42e28 | 2004-12-08 18:43:03 +0000 | [diff] [blame] | 1803 | zlog_debug("Incoming External LSA Discarded: We are NSSA/STUB Area"); |
Paul Jakma | 2cd754d | 2010-01-14 16:26:12 +0300 | [diff] [blame] | 1804 | DISCARD_LSA (lsa, 1); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1805 | } |
| 1806 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1807 | if (lsa->data->type == OSPF_AS_NSSA_LSA) |
| 1808 | if (nbr->oi->area->external_routing != OSPF_AREA_NSSA) |
| 1809 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1810 | if (IS_DEBUG_OSPF_NSSA) |
ajs | 2a42e28 | 2004-12-08 18:43:03 +0000 | [diff] [blame] | 1811 | zlog_debug("Incoming NSSA LSA Discarded: Not NSSA Area"); |
Paul Jakma | 2cd754d | 2010-01-14 16:26:12 +0300 | [diff] [blame] | 1812 | DISCARD_LSA (lsa,2); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1813 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1814 | |
David Lamparter | 23cd8fb | 2013-08-02 07:27:53 +0000 | [diff] [blame] | 1815 | /* VU229804: Router-LSA Adv-ID must be equal to LS-ID */ |
| 1816 | if (lsa->data->type == OSPF_ROUTER_LSA) |
| 1817 | if (!IPV4_ADDR_SAME(&lsa->data->id, &lsa->data->adv_router)) |
| 1818 | { |
| 1819 | char buf1[INET_ADDRSTRLEN]; |
| 1820 | char buf2[INET_ADDRSTRLEN]; |
| 1821 | char buf3[INET_ADDRSTRLEN]; |
| 1822 | |
| 1823 | zlog_err("Incoming Router-LSA from %s with " |
| 1824 | "Adv-ID[%s] != LS-ID[%s]", |
| 1825 | inet_ntop (AF_INET, &ospfh->router_id, |
| 1826 | buf1, INET_ADDRSTRLEN), |
| 1827 | inet_ntop (AF_INET, &lsa->data->id, |
| 1828 | buf2, INET_ADDRSTRLEN), |
| 1829 | inet_ntop (AF_INET, &lsa->data->adv_router, |
| 1830 | buf3, INET_ADDRSTRLEN)); |
| 1831 | zlog_err("OSPF domain compromised by attack or corruption. " |
| 1832 | "Verify correct operation of -ALL- OSPF routers."); |
| 1833 | DISCARD_LSA (lsa, 0); |
| 1834 | } |
| 1835 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1836 | /* Find the LSA in the current database. */ |
| 1837 | |
| 1838 | current = ospf_lsa_lookup_by_header (oi->area, lsa->data); |
| 1839 | |
Andrew Certain | f92c57f | 2012-12-04 13:29:21 -0800 | [diff] [blame] | 1840 | /* (4) If the LSA's LS age is equal to MaxAge, and there is currently |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1841 | no instance of the LSA in the router's link state database, |
| 1842 | and none of router's neighbors are in states Exchange or Loading, |
Andrew Certain | f92c57f | 2012-12-04 13:29:21 -0800 | [diff] [blame] | 1843 | then take the following actions: */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1844 | |
| 1845 | if (IS_LSA_MAXAGE (lsa) && !current && |
Christian Franke | 4c14b7f | 2013-02-20 10:00:54 +0000 | [diff] [blame] | 1846 | ospf_check_nbr_status(oi->ospf)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1847 | { |
Andrew Certain | f92c57f | 2012-12-04 13:29:21 -0800 | [diff] [blame] | 1848 | /* (4a) Response Link State Acknowledgment. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1849 | ospf_ls_ack_send (nbr, lsa); |
| 1850 | |
Andrew Certain | f92c57f | 2012-12-04 13:29:21 -0800 | [diff] [blame] | 1851 | /* (4b) Discard LSA. */ |
Ayan Banerjee | faf9875 | 2012-12-04 10:49:12 -0800 | [diff] [blame] | 1852 | if (IS_DEBUG_OSPF (lsa, LSA)) |
| 1853 | { |
| 1854 | zlog_debug ("Link State Update[%s]: LS age is equal to MaxAge.", |
| 1855 | dump_lsa_key(lsa)); |
| 1856 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1857 | DISCARD_LSA (lsa, 3); |
| 1858 | } |
| 1859 | |
| 1860 | #ifdef HAVE_OPAQUE_LSA |
| 1861 | if (IS_OPAQUE_LSA (lsa->data->type) |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 1862 | && IPV4_ADDR_SAME (&lsa->data->adv_router, &oi->ospf->router_id)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1863 | { |
| 1864 | /* |
| 1865 | * Even if initial flushing seems to be completed, there might |
| 1866 | * be a case that self-originated LSA with MaxAge still remain |
| 1867 | * in the routing domain. |
| 1868 | * Just send an LSAck message to cease retransmission. |
| 1869 | */ |
| 1870 | if (IS_LSA_MAXAGE (lsa)) |
| 1871 | { |
| 1872 | zlog_warn ("LSA[%s]: Boomerang effect?", dump_lsa_key (lsa)); |
| 1873 | ospf_ls_ack_send (nbr, lsa); |
| 1874 | ospf_lsa_discard (lsa); |
| 1875 | |
| 1876 | if (current != NULL && ! IS_LSA_MAXAGE (current)) |
| 1877 | ospf_opaque_lsa_refresh_schedule (current); |
| 1878 | continue; |
| 1879 | } |
| 1880 | |
| 1881 | /* |
| 1882 | * If an instance of self-originated Opaque-LSA is not found |
| 1883 | * in the LSDB, there are some possible cases here. |
| 1884 | * |
| 1885 | * 1) This node lost opaque-capability after restart. |
| 1886 | * 2) Else, a part of opaque-type is no more supported. |
| 1887 | * 3) Else, a part of opaque-id is no more supported. |
| 1888 | * |
| 1889 | * Anyway, it is still this node's responsibility to flush it. |
| 1890 | * Otherwise, the LSA instance remains in the routing domain |
| 1891 | * until its age reaches to MaxAge. |
| 1892 | */ |
paul | 69310a6 | 2005-05-11 18:09:59 +0000 | [diff] [blame] | 1893 | /* XXX: We should deal with this for *ALL* LSAs, not just opaque */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1894 | if (current == NULL) |
| 1895 | { |
| 1896 | if (IS_DEBUG_OSPF_EVENT) |
paul | 69310a6 | 2005-05-11 18:09:59 +0000 | [diff] [blame] | 1897 | zlog_debug ("LSA[%s]: Previously originated Opaque-LSA," |
| 1898 | "not found in the LSDB.", dump_lsa_key (lsa)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1899 | |
| 1900 | SET_FLAG (lsa->flags, OSPF_LSA_SELF); |
paul | 69310a6 | 2005-05-11 18:09:59 +0000 | [diff] [blame] | 1901 | |
| 1902 | ospf_opaque_self_originated_lsa_received (nbr, lsa); |
| 1903 | ospf_ls_ack_send (nbr, lsa); |
| 1904 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1905 | continue; |
| 1906 | } |
| 1907 | } |
| 1908 | #endif /* HAVE_OPAQUE_LSA */ |
paul | 69310a6 | 2005-05-11 18:09:59 +0000 | [diff] [blame] | 1909 | |
hasso | cb05eb2 | 2004-02-11 21:10:19 +0000 | [diff] [blame] | 1910 | /* It might be happen that received LSA is self-originated network LSA, but |
Andrew Certain | f92c57f | 2012-12-04 13:29:21 -0800 | [diff] [blame] | 1911 | * router ID is changed. So, we should check if LSA is a network-LSA whose |
hasso | cb05eb2 | 2004-02-11 21:10:19 +0000 | [diff] [blame] | 1912 | * Link State ID is one of the router's own IP interface addresses but whose |
| 1913 | * Advertising Router is not equal to the router's own Router ID |
| 1914 | * According to RFC 2328 12.4.2 and 13.4 this LSA should be flushed. |
| 1915 | */ |
| 1916 | |
| 1917 | if(lsa->data->type == OSPF_NETWORK_LSA) |
| 1918 | { |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 1919 | struct listnode *oinode, *oinnode; |
| 1920 | struct ospf_interface *out_if; |
hasso | cb05eb2 | 2004-02-11 21:10:19 +0000 | [diff] [blame] | 1921 | int Flag = 0; |
| 1922 | |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 1923 | for (ALL_LIST_ELEMENTS (oi->ospf->oiflist, oinode, oinnode, out_if)) |
hasso | cb05eb2 | 2004-02-11 21:10:19 +0000 | [diff] [blame] | 1924 | { |
hasso | cb05eb2 | 2004-02-11 21:10:19 +0000 | [diff] [blame] | 1925 | if(out_if == NULL) |
| 1926 | break; |
| 1927 | |
| 1928 | if((IPV4_ADDR_SAME(&out_if->address->u.prefix4, &lsa->data->id)) && |
| 1929 | (!(IPV4_ADDR_SAME(&oi->ospf->router_id, &lsa->data->adv_router)))) |
| 1930 | { |
| 1931 | if(out_if->network_lsa_self) |
| 1932 | { |
| 1933 | ospf_lsa_flush_area(lsa,out_if->area); |
| 1934 | if(IS_DEBUG_OSPF_EVENT) |
ajs | 2a42e28 | 2004-12-08 18:43:03 +0000 | [diff] [blame] | 1935 | zlog_debug ("ospf_lsa_discard() in ospf_ls_upd() point 9: lsa %p Type-%d", |
David Lamparter | eed3c48 | 2015-03-03 08:51:53 +0100 | [diff] [blame] | 1936 | (void *)lsa, (int) lsa->data->type); |
hasso | cb05eb2 | 2004-02-11 21:10:19 +0000 | [diff] [blame] | 1937 | ospf_lsa_discard (lsa); |
| 1938 | Flag = 1; |
| 1939 | } |
| 1940 | break; |
| 1941 | } |
| 1942 | } |
| 1943 | if(Flag) |
| 1944 | continue; |
| 1945 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1946 | |
| 1947 | /* (5) Find the instance of this LSA that is currently contained |
| 1948 | in the router's link state database. If there is no |
| 1949 | database copy, or the received LSA is more recent than |
Andrew Certain | f92c57f | 2012-12-04 13:29:21 -0800 | [diff] [blame] | 1950 | the database copy the following steps must be performed. |
| 1951 | (The sub steps from RFC 2328 section 13 step (5) will be performed in |
| 1952 | ospf_flood() ) */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1953 | |
| 1954 | if (current == NULL || |
| 1955 | (ret = ospf_lsa_more_recent (current, lsa)) < 0) |
| 1956 | { |
| 1957 | /* Actual flooding procedure. */ |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 1958 | if (ospf_flood (oi->ospf, nbr, current, lsa) < 0) /* Trap NSSA later. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1959 | DISCARD_LSA (lsa, 4); |
| 1960 | continue; |
| 1961 | } |
| 1962 | |
| 1963 | /* (6) Else, If there is an instance of the LSA on the sending |
| 1964 | neighbor's Link state request list, an error has occurred in |
| 1965 | the Database Exchange process. In this case, restart the |
| 1966 | Database Exchange process by generating the neighbor event |
| 1967 | BadLSReq for the sending neighbor and stop processing the |
| 1968 | Link State Update packet. */ |
| 1969 | |
| 1970 | if (ospf_ls_request_lookup (nbr, lsa)) |
| 1971 | { |
| 1972 | OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_BadLSReq); |
ajs | 3aa8d5f | 2004-12-11 18:00:06 +0000 | [diff] [blame] | 1973 | zlog_warn("LSA[%s] instance exists on Link state request list", |
| 1974 | dump_lsa_key(lsa)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1975 | |
| 1976 | /* Clean list of LSAs. */ |
| 1977 | ospf_upd_list_clean (lsas); |
| 1978 | /* this lsa is not on lsas list already. */ |
| 1979 | ospf_lsa_discard (lsa); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1980 | return; |
| 1981 | } |
| 1982 | |
| 1983 | /* If the received LSA is the same instance as the database copy |
| 1984 | (i.e., neither one is more recent) the following two steps |
| 1985 | should be performed: */ |
| 1986 | |
| 1987 | if (ret == 0) |
| 1988 | { |
| 1989 | /* If the LSA is listed in the Link state retransmission list |
| 1990 | for the receiving adjacency, the router itself is expecting |
| 1991 | an acknowledgment for this LSA. The router should treat the |
| 1992 | received LSA as an acknowledgment by removing the LSA from |
| 1993 | the Link state retransmission list. This is termed an |
| 1994 | "implied acknowledgment". */ |
| 1995 | |
| 1996 | ls_ret = ospf_ls_retransmit_lookup (nbr, lsa); |
| 1997 | |
| 1998 | if (ls_ret != NULL) |
| 1999 | { |
| 2000 | ospf_ls_retransmit_delete (nbr, ls_ret); |
| 2001 | |
| 2002 | /* Delayed acknowledgment sent if advertisement received |
| 2003 | from Designated Router, otherwise do nothing. */ |
| 2004 | if (oi->state == ISM_Backup) |
| 2005 | if (NBR_IS_DR (nbr)) |
| 2006 | listnode_add (oi->ls_ack, ospf_lsa_lock (lsa)); |
| 2007 | |
| 2008 | DISCARD_LSA (lsa, 5); |
| 2009 | } |
| 2010 | else |
| 2011 | /* Acknowledge the receipt of the LSA by sending a |
| 2012 | Link State Acknowledgment packet back out the receiving |
| 2013 | interface. */ |
| 2014 | { |
| 2015 | ospf_ls_ack_send (nbr, lsa); |
| 2016 | DISCARD_LSA (lsa, 6); |
| 2017 | } |
| 2018 | } |
| 2019 | |
| 2020 | /* The database copy is more recent. If the database copy |
| 2021 | has LS age equal to MaxAge and LS sequence number equal to |
| 2022 | MaxSequenceNumber, simply discard the received LSA without |
| 2023 | acknowledging it. (In this case, the LSA's LS sequence number is |
| 2024 | wrapping, and the MaxSequenceNumber LSA must be completely |
| 2025 | flushed before any new LSA instance can be introduced). */ |
| 2026 | |
| 2027 | else if (ret > 0) /* Database copy is more recent */ |
| 2028 | { |
| 2029 | if (IS_LSA_MAXAGE (current) && |
| 2030 | current->data->ls_seqnum == htonl (OSPF_MAX_SEQUENCE_NUMBER)) |
| 2031 | { |
| 2032 | DISCARD_LSA (lsa, 7); |
| 2033 | } |
| 2034 | /* Otherwise, as long as the database copy has not been sent in a |
| 2035 | Link State Update within the last MinLSArrival seconds, send the |
| 2036 | database copy back to the sending neighbor, encapsulated within |
| 2037 | a Link State Update Packet. The Link State Update Packet should |
| 2038 | be sent directly to the neighbor. In so doing, do not put the |
| 2039 | database copy of the LSA on the neighbor's link state |
| 2040 | retransmission list, and do not acknowledge the received (less |
| 2041 | recent) LSA instance. */ |
| 2042 | else |
| 2043 | { |
| 2044 | struct timeval now; |
| 2045 | |
Paul Jakma | 2518efd | 2006-08-27 06:49:29 +0000 | [diff] [blame] | 2046 | quagga_gettime (QUAGGA_CLK_MONOTONIC, &now); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2047 | |
| 2048 | if (tv_cmp (tv_sub (now, current->tv_orig), |
Paul Jakma | 2c9f8e3 | 2010-01-11 16:22:12 +0000 | [diff] [blame] | 2049 | int2tv (OSPF_MIN_LS_ARRIVAL)) >= 0) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2050 | /* Trap NSSA type later.*/ |
| 2051 | ospf_ls_upd_send_lsa (nbr, current, OSPF_SEND_PACKET_DIRECT); |
| 2052 | DISCARD_LSA (lsa, 8); |
| 2053 | } |
| 2054 | } |
| 2055 | } |
Paul Jakma | 2cd754d | 2010-01-14 16:26:12 +0300 | [diff] [blame] | 2056 | #undef DISCARD_LSA |
| 2057 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2058 | assert (listcount (lsas) == 0); |
| 2059 | list_delete (lsas); |
| 2060 | } |
| 2061 | |
| 2062 | /* OSPF Link State Acknowledgment message read -- RFC2328 Section 13.7. */ |
paul | 4dadc29 | 2005-05-06 21:37:42 +0000 | [diff] [blame] | 2063 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2064 | ospf_ls_ack (struct ip *iph, struct ospf_header *ospfh, |
| 2065 | struct stream *s, struct ospf_interface *oi, u_int16_t size) |
| 2066 | { |
| 2067 | struct ospf_neighbor *nbr; |
paul | 69310a6 | 2005-05-11 18:09:59 +0000 | [diff] [blame] | 2068 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2069 | /* increment statistics. */ |
| 2070 | oi->ls_ack_in++; |
| 2071 | |
paul | d3f0d62 | 2004-05-05 15:27:15 +0000 | [diff] [blame] | 2072 | nbr = ospf_nbr_lookup (oi, iph, ospfh); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2073 | if (nbr == NULL) |
| 2074 | { |
| 2075 | zlog_warn ("Link State Acknowledgment: Unknown Neighbor %s.", |
| 2076 | inet_ntoa (ospfh->router_id)); |
| 2077 | return; |
| 2078 | } |
| 2079 | |
Paul Jakma | 57c5c65 | 2010-01-07 06:12:53 +0000 | [diff] [blame] | 2080 | /* Add event to thread. */ |
| 2081 | OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_PacketReceived); |
| 2082 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2083 | if (nbr->state < NSM_Exchange) |
| 2084 | { |
ajs | 3aa8d5f | 2004-12-11 18:00:06 +0000 | [diff] [blame] | 2085 | zlog_warn ("Link State Acknowledgment: " |
| 2086 | "Neighbor[%s] state %s is less than Exchange", |
| 2087 | inet_ntoa (ospfh->router_id), |
| 2088 | LOOKUP(ospf_nsm_state_msg, nbr->state)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2089 | return; |
| 2090 | } |
paul | 69310a6 | 2005-05-11 18:09:59 +0000 | [diff] [blame] | 2091 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2092 | while (size >= OSPF_LSA_HEADER_SIZE) |
| 2093 | { |
| 2094 | struct ospf_lsa *lsa, *lsr; |
| 2095 | |
| 2096 | lsa = ospf_lsa_new (); |
| 2097 | lsa->data = (struct lsa_header *) STREAM_PNT (s); |
| 2098 | |
| 2099 | /* lsah = (struct lsa_header *) STREAM_PNT (s); */ |
| 2100 | size -= OSPF_LSA_HEADER_SIZE; |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 2101 | stream_forward_getp (s, OSPF_LSA_HEADER_SIZE); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2102 | |
| 2103 | if (lsa->data->type < OSPF_MIN_LSA || lsa->data->type >= OSPF_MAX_LSA) |
| 2104 | { |
| 2105 | lsa->data = NULL; |
| 2106 | ospf_lsa_discard (lsa); |
| 2107 | continue; |
| 2108 | } |
| 2109 | |
| 2110 | lsr = ospf_ls_retransmit_lookup (nbr, lsa); |
| 2111 | |
Lu Feng | 49d7af1 | 2014-02-21 08:11:15 +0000 | [diff] [blame] | 2112 | if (lsr != NULL && ospf_lsa_more_recent (lsr, lsa) == 0) |
Paul Jakma | e30677a | 2015-01-20 15:45:36 +0000 | [diff] [blame] | 2113 | ospf_ls_retransmit_delete (nbr, lsr); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2114 | |
| 2115 | lsa->data = NULL; |
| 2116 | ospf_lsa_discard (lsa); |
| 2117 | } |
| 2118 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2119 | return; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2120 | } |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 2121 | |
ajs | 038163f | 2005-02-17 19:55:59 +0000 | [diff] [blame] | 2122 | static struct stream * |
ajs | 5c33349 | 2005-02-23 15:43:01 +0000 | [diff] [blame] | 2123 | ospf_recv_packet (int fd, struct interface **ifp, struct stream *ibuf) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2124 | { |
| 2125 | int ret; |
ajs | 5c33349 | 2005-02-23 15:43:01 +0000 | [diff] [blame] | 2126 | struct ip *iph; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2127 | u_int16_t ip_len; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2128 | unsigned int ifindex = 0; |
| 2129 | struct iovec iov; |
gdt | d0deca6 | 2004-08-26 13:14:07 +0000 | [diff] [blame] | 2130 | /* Header and data both require alignment. */ |
gdt | e304982 | 2004-08-26 13:19:40 +0000 | [diff] [blame] | 2131 | char buff [CMSG_SPACE(SOPT_SIZE_CMSG_IFINDEX_IPV4())]; |
paul | 2dd8bb4 | 2004-07-23 15:13:48 +0000 | [diff] [blame] | 2132 | struct msghdr msgh; |
| 2133 | |
paul | 68defd6 | 2004-09-27 07:27:13 +0000 | [diff] [blame] | 2134 | memset (&msgh, 0, sizeof (struct msghdr)); |
paul | 2dd8bb4 | 2004-07-23 15:13:48 +0000 | [diff] [blame] | 2135 | msgh.msg_iov = &iov; |
| 2136 | msgh.msg_iovlen = 1; |
| 2137 | msgh.msg_control = (caddr_t) buff; |
| 2138 | msgh.msg_controllen = sizeof (buff); |
paul | 2dd8bb4 | 2004-07-23 15:13:48 +0000 | [diff] [blame] | 2139 | |
ajs | 5c33349 | 2005-02-23 15:43:01 +0000 | [diff] [blame] | 2140 | ret = stream_recvmsg (ibuf, fd, &msgh, 0, OSPF_MAX_PACKET_SIZE+1); |
| 2141 | if (ret < 0) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2142 | { |
ajs | 5c33349 | 2005-02-23 15:43:01 +0000 | [diff] [blame] | 2143 | zlog_warn("stream_recvmsg failed: %s", safe_strerror(errno)); |
| 2144 | return NULL; |
| 2145 | } |
paul | 69310a6 | 2005-05-11 18:09:59 +0000 | [diff] [blame] | 2146 | if ((unsigned int)ret < sizeof(iph)) /* ret must be > 0 now */ |
ajs | 5c33349 | 2005-02-23 15:43:01 +0000 | [diff] [blame] | 2147 | { |
| 2148 | zlog_warn("ospf_recv_packet: discarding runt packet of length %d " |
| 2149 | "(ip header size is %u)", |
| 2150 | ret, (u_int)sizeof(iph)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2151 | return NULL; |
| 2152 | } |
paul | 18b12c3 | 2004-10-05 14:38:29 +0000 | [diff] [blame] | 2153 | |
ajs | 5c33349 | 2005-02-23 15:43:01 +0000 | [diff] [blame] | 2154 | /* Note that there should not be alignment problems with this assignment |
| 2155 | because this is at the beginning of the stream data buffer. */ |
| 2156 | iph = (struct ip *) STREAM_DATA(ibuf); |
| 2157 | sockopt_iphdrincl_swab_systoh (iph); |
paul | 18b12c3 | 2004-10-05 14:38:29 +0000 | [diff] [blame] | 2158 | |
ajs | 5c33349 | 2005-02-23 15:43:01 +0000 | [diff] [blame] | 2159 | ip_len = iph->ip_len; |
paul | 6b33361 | 2004-10-11 10:11:25 +0000 | [diff] [blame] | 2160 | |
Dmitrij Tejblum | de5ccb9 | 2011-12-12 20:30:10 +0400 | [diff] [blame] | 2161 | #if !defined(GNU_LINUX) && (OpenBSD < 200311) && (__FreeBSD_version < 1000000) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2162 | /* |
| 2163 | * Kernel network code touches incoming IP header parameters, |
| 2164 | * before protocol specific processing. |
| 2165 | * |
| 2166 | * 1) Convert byteorder to host representation. |
| 2167 | * --> ip_len, ip_id, ip_off |
| 2168 | * |
| 2169 | * 2) Adjust ip_len to strip IP header size! |
| 2170 | * --> If user process receives entire IP packet via RAW |
| 2171 | * socket, it must consider adding IP header size to |
| 2172 | * the "ip_len" field of "ip" structure. |
| 2173 | * |
| 2174 | * For more details, see <netinet/ip_input.c>. |
| 2175 | */ |
ajs | 5c33349 | 2005-02-23 15:43:01 +0000 | [diff] [blame] | 2176 | ip_len = ip_len + (iph->ip_hl << 2); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2177 | #endif |
| 2178 | |
David BÉRARD | 0150c9c | 2010-05-11 10:17:53 +0200 | [diff] [blame] | 2179 | #if defined(__DragonFly__) |
| 2180 | /* |
| 2181 | * in DragonFly's raw socket, ip_len/ip_off are read |
| 2182 | * in network byte order. |
| 2183 | * As OpenBSD < 200311 adjust ip_len to strip IP header size! |
| 2184 | */ |
| 2185 | ip_len = ntohs(iph->ip_len) + (iph->ip_hl << 2); |
| 2186 | #endif |
| 2187 | |
paul | 863082d | 2004-08-19 04:43:43 +0000 | [diff] [blame] | 2188 | ifindex = getsockopt_ifindex (AF_INET, &msgh); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2189 | |
| 2190 | *ifp = if_lookup_by_index (ifindex); |
| 2191 | |
| 2192 | if (ret != ip_len) |
| 2193 | { |
ajs | 5c33349 | 2005-02-23 15:43:01 +0000 | [diff] [blame] | 2194 | zlog_warn ("ospf_recv_packet read length mismatch: ip_len is %d, " |
| 2195 | "but recvmsg returned %d", ip_len, ret); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2196 | return NULL; |
| 2197 | } |
| 2198 | |
| 2199 | return ibuf; |
| 2200 | } |
| 2201 | |
paul | 4dadc29 | 2005-05-06 21:37:42 +0000 | [diff] [blame] | 2202 | static struct ospf_interface * |
paul | d3f0d62 | 2004-05-05 15:27:15 +0000 | [diff] [blame] | 2203 | ospf_associate_packet_vl (struct ospf *ospf, struct interface *ifp, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2204 | struct ip *iph, struct ospf_header *ospfh) |
| 2205 | { |
| 2206 | struct ospf_interface *rcv_oi; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2207 | struct ospf_vl_data *vl_data; |
| 2208 | struct ospf_area *vl_area; |
hasso | 52dc7ee | 2004-09-23 19:18:23 +0000 | [diff] [blame] | 2209 | struct listnode *node; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2210 | |
| 2211 | if (IN_MULTICAST (ntohl (iph->ip_dst.s_addr)) || |
| 2212 | !OSPF_IS_AREA_BACKBONE (ospfh)) |
paul | d3f0d62 | 2004-05-05 15:27:15 +0000 | [diff] [blame] | 2213 | return NULL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2214 | |
paul | d3f0d62 | 2004-05-05 15:27:15 +0000 | [diff] [blame] | 2215 | /* look for local OSPF interface matching the destination |
| 2216 | * to determine Area ID. We presume therefore the destination address |
| 2217 | * is unique, or at least (for "unnumbered" links), not used in other |
| 2218 | * areas |
| 2219 | */ |
| 2220 | if ((rcv_oi = ospf_if_lookup_by_local_addr (ospf, NULL, |
| 2221 | iph->ip_dst)) == NULL) |
| 2222 | return NULL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2223 | |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 2224 | for (ALL_LIST_ELEMENTS_RO (ospf->vlinks, node, vl_data)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2225 | { |
paul | 020709f | 2003-04-04 02:44:16 +0000 | [diff] [blame] | 2226 | vl_area = ospf_area_lookup_by_area_id (ospf, vl_data->vl_area_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2227 | if (!vl_area) |
| 2228 | continue; |
| 2229 | |
| 2230 | if (OSPF_AREA_SAME (&vl_area, &rcv_oi->area) && |
| 2231 | IPV4_ADDR_SAME (&vl_data->vl_peer, &ospfh->router_id)) |
| 2232 | { |
| 2233 | if (IS_DEBUG_OSPF_EVENT) |
ajs | 2a42e28 | 2004-12-08 18:43:03 +0000 | [diff] [blame] | 2234 | zlog_debug ("associating packet with %s", |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2235 | IF_NAME (vl_data->vl_oi)); |
| 2236 | if (! CHECK_FLAG (vl_data->vl_oi->ifp->flags, IFF_UP)) |
| 2237 | { |
| 2238 | if (IS_DEBUG_OSPF_EVENT) |
ajs | 2a42e28 | 2004-12-08 18:43:03 +0000 | [diff] [blame] | 2239 | zlog_debug ("This VL is not up yet, sorry"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2240 | return NULL; |
| 2241 | } |
| 2242 | |
| 2243 | return vl_data->vl_oi; |
| 2244 | } |
| 2245 | } |
| 2246 | |
| 2247 | if (IS_DEBUG_OSPF_EVENT) |
ajs | 2a42e28 | 2004-12-08 18:43:03 +0000 | [diff] [blame] | 2248 | zlog_debug ("couldn't find any VL to associate the packet with"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2249 | |
paul | d3f0d62 | 2004-05-05 15:27:15 +0000 | [diff] [blame] | 2250 | return NULL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2251 | } |
| 2252 | |
Paul Jakma | f63f06d | 2011-04-08 12:44:43 +0100 | [diff] [blame] | 2253 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2254 | ospf_check_area_id (struct ospf_interface *oi, struct ospf_header *ospfh) |
| 2255 | { |
| 2256 | /* Check match the Area ID of the receiving interface. */ |
| 2257 | if (OSPF_AREA_SAME (&oi->area, &ospfh)) |
| 2258 | return 1; |
| 2259 | |
| 2260 | return 0; |
| 2261 | } |
| 2262 | |
| 2263 | /* Unbound socket will accept any Raw IP packets if proto is matched. |
| 2264 | To prevent it, compare src IP address and i/f address with masking |
| 2265 | i/f network mask. */ |
paul | 4dadc29 | 2005-05-06 21:37:42 +0000 | [diff] [blame] | 2266 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2267 | ospf_check_network_mask (struct ospf_interface *oi, struct in_addr ip_src) |
| 2268 | { |
| 2269 | struct in_addr mask, me, him; |
| 2270 | |
| 2271 | if (oi->type == OSPF_IFTYPE_POINTOPOINT || |
| 2272 | oi->type == OSPF_IFTYPE_VIRTUALLINK) |
| 2273 | return 1; |
| 2274 | |
| 2275 | masklen2ip (oi->address->prefixlen, &mask); |
| 2276 | |
| 2277 | me.s_addr = oi->address->u.prefix4.s_addr & mask.s_addr; |
| 2278 | him.s_addr = ip_src.s_addr & mask.s_addr; |
| 2279 | |
| 2280 | if (IPV4_ADDR_SAME (&me, &him)) |
| 2281 | return 1; |
| 2282 | |
| 2283 | return 0; |
| 2284 | } |
| 2285 | |
Denis Ovsienko | bd5651f | 2012-02-26 17:59:43 +0400 | [diff] [blame] | 2286 | /* Return 1, if the packet is properly authenticated and checksummed, |
| 2287 | 0 otherwise. In particular, check that AuType header field is valid and |
| 2288 | matches the locally configured AuType, and that D.5 requirements are met. */ |
paul | 4dadc29 | 2005-05-06 21:37:42 +0000 | [diff] [blame] | 2289 | static int |
Denis Ovsienko | e525914 | 2012-01-30 16:07:18 +0400 | [diff] [blame] | 2290 | ospf_check_auth (struct ospf_interface *oi, struct ospf_header *ospfh) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2291 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2292 | struct crypt_key *ck; |
Denis Ovsienko | bd5651f | 2012-02-26 17:59:43 +0400 | [diff] [blame] | 2293 | u_int16_t iface_auth_type; |
| 2294 | u_int16_t pkt_auth_type = ntohs (ospfh->auth_type); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2295 | |
Denis Ovsienko | bd5651f | 2012-02-26 17:59:43 +0400 | [diff] [blame] | 2296 | switch (pkt_auth_type) |
| 2297 | { |
| 2298 | case OSPF_AUTH_NULL: /* RFC2328 D.5.1 */ |
| 2299 | if (OSPF_AUTH_NULL != (iface_auth_type = ospf_auth_type (oi))) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2300 | { |
Denis Ovsienko | bd5651f | 2012-02-26 17:59:43 +0400 | [diff] [blame] | 2301 | if (IS_DEBUG_OSPF_PACKET (ospfh->type - 1, RECV)) |
| 2302 | zlog_warn ("interface %s: auth-type mismatch, local %s, rcvd Null", |
| 2303 | IF_NAME (oi), LOOKUP (ospf_auth_type_str, iface_auth_type)); |
| 2304 | return 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2305 | } |
Denis Ovsienko | bd5651f | 2012-02-26 17:59:43 +0400 | [diff] [blame] | 2306 | if (! ospf_check_sum (ospfh)) |
| 2307 | { |
| 2308 | if (IS_DEBUG_OSPF_PACKET (ospfh->type - 1, RECV)) |
| 2309 | zlog_warn ("interface %s: Null auth OK, but checksum error, Router-ID %s", |
| 2310 | IF_NAME (oi), inet_ntoa (ospfh->router_id)); |
| 2311 | return 0; |
| 2312 | } |
| 2313 | return 1; |
| 2314 | case OSPF_AUTH_SIMPLE: /* RFC2328 D.5.2 */ |
| 2315 | if (OSPF_AUTH_SIMPLE != (iface_auth_type = ospf_auth_type (oi))) |
| 2316 | { |
| 2317 | if (IS_DEBUG_OSPF_PACKET (ospfh->type - 1, RECV)) |
| 2318 | zlog_warn ("interface %s: auth-type mismatch, local %s, rcvd Simple", |
| 2319 | IF_NAME (oi), LOOKUP (ospf_auth_type_str, iface_auth_type)); |
| 2320 | return 0; |
| 2321 | } |
| 2322 | if (memcmp (OSPF_IF_PARAM (oi, auth_simple), ospfh->u.auth_data, OSPF_AUTH_SIMPLE_SIZE)) |
| 2323 | { |
| 2324 | if (IS_DEBUG_OSPF_PACKET (ospfh->type - 1, RECV)) |
| 2325 | zlog_warn ("interface %s: Simple auth failed", IF_NAME (oi)); |
| 2326 | return 0; |
| 2327 | } |
| 2328 | if (! ospf_check_sum (ospfh)) |
| 2329 | { |
| 2330 | if (IS_DEBUG_OSPF_PACKET (ospfh->type - 1, RECV)) |
| 2331 | zlog_warn ("interface %s: Simple auth OK, checksum error, Router-ID %s", |
| 2332 | IF_NAME (oi), inet_ntoa (ospfh->router_id)); |
| 2333 | return 0; |
| 2334 | } |
| 2335 | return 1; |
| 2336 | case OSPF_AUTH_CRYPTOGRAPHIC: /* RFC2328 D.5.3 */ |
| 2337 | if (OSPF_AUTH_CRYPTOGRAPHIC != (iface_auth_type = ospf_auth_type (oi))) |
| 2338 | { |
| 2339 | if (IS_DEBUG_OSPF_PACKET (ospfh->type - 1, RECV)) |
| 2340 | zlog_warn ("interface %s: auth-type mismatch, local %s, rcvd Cryptographic", |
| 2341 | IF_NAME (oi), LOOKUP (ospf_auth_type_str, iface_auth_type)); |
| 2342 | return 0; |
| 2343 | } |
| 2344 | if (ospfh->checksum) |
| 2345 | { |
| 2346 | if (IS_DEBUG_OSPF_PACKET (ospfh->type - 1, RECV)) |
| 2347 | zlog_warn ("interface %s: OSPF header checksum is not 0", IF_NAME (oi)); |
| 2348 | return 0; |
| 2349 | } |
| 2350 | /* only MD5 crypto method can pass ospf_packet_examin() */ |
| 2351 | if |
| 2352 | ( |
| 2353 | NULL == (ck = listgetdata (listtail(OSPF_IF_PARAM (oi,auth_crypt)))) || |
| 2354 | ospfh->u.crypt.key_id != ck->key_id || |
| 2355 | /* Condition above uses the last key ID on the list, which is |
| 2356 | different from what ospf_crypt_key_lookup() does. A bug? */ |
| 2357 | ! ospf_check_md5_digest (oi, ospfh) |
| 2358 | ) |
| 2359 | { |
| 2360 | if (IS_DEBUG_OSPF_PACKET (ospfh->type - 1, RECV)) |
| 2361 | zlog_warn ("interface %s: MD5 auth failed", IF_NAME (oi)); |
| 2362 | return 0; |
| 2363 | } |
| 2364 | return 1; |
| 2365 | default: |
| 2366 | if (IS_DEBUG_OSPF_PACKET (ospfh->type - 1, RECV)) |
| 2367 | zlog_warn ("interface %s: invalid packet auth-type (%02x)", |
| 2368 | IF_NAME (oi), pkt_auth_type); |
| 2369 | return 0; |
| 2370 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2371 | } |
| 2372 | |
paul | 4dadc29 | 2005-05-06 21:37:42 +0000 | [diff] [blame] | 2373 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2374 | ospf_check_sum (struct ospf_header *ospfh) |
| 2375 | { |
| 2376 | u_int32_t ret; |
| 2377 | u_int16_t sum; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2378 | |
| 2379 | /* clear auth_data for checksum. */ |
| 2380 | memset (ospfh->u.auth_data, 0, OSPF_AUTH_SIMPLE_SIZE); |
| 2381 | |
| 2382 | /* keep checksum and clear. */ |
| 2383 | sum = ospfh->checksum; |
| 2384 | memset (&ospfh->checksum, 0, sizeof (u_int16_t)); |
| 2385 | |
| 2386 | /* calculate checksum. */ |
| 2387 | ret = in_cksum (ospfh, ntohs (ospfh->length)); |
| 2388 | |
| 2389 | if (ret != sum) |
| 2390 | { |
| 2391 | zlog_info ("ospf_check_sum(): checksum mismatch, my %X, his %X", |
| 2392 | ret, sum); |
| 2393 | return 0; |
| 2394 | } |
| 2395 | |
| 2396 | return 1; |
| 2397 | } |
| 2398 | |
Denis Ovsienko | 4e31de7 | 2012-02-17 16:20:50 +0400 | [diff] [blame] | 2399 | /* Verify, that given link/TOS records are properly sized/aligned and match |
| 2400 | Router-LSA "# links" and "# TOS" fields as specified in RFC2328 A.4.2. */ |
| 2401 | static unsigned |
| 2402 | ospf_router_lsa_links_examin |
| 2403 | ( |
| 2404 | struct router_lsa_link * link, |
| 2405 | u_int16_t linkbytes, |
| 2406 | const u_int16_t num_links |
| 2407 | ) |
| 2408 | { |
| 2409 | unsigned counted_links = 0, thislinklen; |
| 2410 | |
| 2411 | while (linkbytes) |
| 2412 | { |
| 2413 | thislinklen = OSPF_ROUTER_LSA_LINK_SIZE + 4 * link->m[0].tos_count; |
| 2414 | if (thislinklen > linkbytes) |
| 2415 | { |
| 2416 | if (IS_DEBUG_OSPF_PACKET (0, RECV)) |
| 2417 | zlog_debug ("%s: length error in link block #%u", __func__, counted_links); |
| 2418 | return MSG_NG; |
| 2419 | } |
| 2420 | link = (struct router_lsa_link *)((caddr_t) link + thislinklen); |
| 2421 | linkbytes -= thislinklen; |
| 2422 | counted_links++; |
| 2423 | } |
| 2424 | if (counted_links != num_links) |
| 2425 | { |
| 2426 | if (IS_DEBUG_OSPF_PACKET (0, RECV)) |
| 2427 | zlog_debug ("%s: %u link blocks declared, %u present", |
| 2428 | __func__, num_links, counted_links); |
| 2429 | return MSG_NG; |
| 2430 | } |
| 2431 | return MSG_OK; |
| 2432 | } |
| 2433 | |
| 2434 | /* Verify, that the given LSA is properly sized/aligned (including type-specific |
| 2435 | minimum length constraint). */ |
| 2436 | static unsigned |
| 2437 | ospf_lsa_examin (struct lsa_header * lsah, const u_int16_t lsalen, const u_char headeronly) |
| 2438 | { |
| 2439 | unsigned ret; |
| 2440 | struct router_lsa * rlsa; |
| 2441 | if |
| 2442 | ( |
| 2443 | lsah->type < OSPF_MAX_LSA && |
| 2444 | ospf_lsa_minlen[lsah->type] && |
| 2445 | lsalen < OSPF_LSA_HEADER_SIZE + ospf_lsa_minlen[lsah->type] |
| 2446 | ) |
| 2447 | { |
| 2448 | if (IS_DEBUG_OSPF_PACKET (0, RECV)) |
| 2449 | zlog_debug ("%s: undersized (%u B) %s", |
| 2450 | __func__, lsalen, LOOKUP (ospf_lsa_type_msg, lsah->type)); |
| 2451 | return MSG_NG; |
| 2452 | } |
| 2453 | switch (lsah->type) |
| 2454 | { |
| 2455 | case OSPF_ROUTER_LSA: |
| 2456 | /* RFC2328 A.4.2, LSA header + 4 bytes followed by N>=1 (12+)-byte link blocks */ |
| 2457 | if (headeronly) |
| 2458 | { |
| 2459 | ret = (lsalen - OSPF_LSA_HEADER_SIZE - OSPF_ROUTER_LSA_MIN_SIZE) % 4 ? MSG_NG : MSG_OK; |
| 2460 | break; |
| 2461 | } |
| 2462 | rlsa = (struct router_lsa *) lsah; |
| 2463 | ret = ospf_router_lsa_links_examin |
| 2464 | ( |
| 2465 | (struct router_lsa_link *) rlsa->link, |
| 2466 | lsalen - OSPF_LSA_HEADER_SIZE - 4, /* skip: basic header, "flags", 0, "# links" */ |
| 2467 | ntohs (rlsa->links) /* 16 bits */ |
| 2468 | ); |
| 2469 | break; |
| 2470 | case OSPF_AS_EXTERNAL_LSA: |
| 2471 | /* RFC2328 A.4.5, LSA header + 4 bytes followed by N>=1 12-bytes long blocks */ |
| 2472 | case OSPF_AS_NSSA_LSA: |
| 2473 | /* RFC3101 C, idem */ |
| 2474 | ret = (lsalen - OSPF_LSA_HEADER_SIZE - OSPF_AS_EXTERNAL_LSA_MIN_SIZE) % 12 ? MSG_NG : MSG_OK; |
| 2475 | break; |
| 2476 | /* Following LSA types are considered OK length-wise as soon as their minimum |
| 2477 | * length constraint is met and length of the whole LSA is a multiple of 4 |
| 2478 | * (basic LSA header size is already a multiple of 4). */ |
| 2479 | case OSPF_NETWORK_LSA: |
| 2480 | /* RFC2328 A.4.3, LSA header + 4 bytes followed by N>=1 router-IDs */ |
| 2481 | case OSPF_SUMMARY_LSA: |
| 2482 | case OSPF_ASBR_SUMMARY_LSA: |
| 2483 | /* RFC2328 A.4.4, LSA header + 4 bytes followed by N>=1 4-bytes TOS blocks */ |
| 2484 | #ifdef HAVE_OPAQUE_LSA |
| 2485 | case OSPF_OPAQUE_LINK_LSA: |
| 2486 | case OSPF_OPAQUE_AREA_LSA: |
| 2487 | case OSPF_OPAQUE_AS_LSA: |
| 2488 | /* RFC5250 A.2, "some number of octets (of application-specific |
| 2489 | * data) padded to 32-bit alignment." This is considered equivalent |
| 2490 | * to 4-byte alignment of all other LSA types, see OSPF-ALIGNMENT.txt |
| 2491 | * file for the detailed analysis of this passage. */ |
| 2492 | #endif |
| 2493 | ret = lsalen % 4 ? MSG_NG : MSG_OK; |
| 2494 | break; |
| 2495 | default: |
| 2496 | if (IS_DEBUG_OSPF_PACKET (0, RECV)) |
| 2497 | zlog_debug ("%s: unsupported LSA type 0x%02x", __func__, lsah->type); |
| 2498 | return MSG_NG; |
| 2499 | } |
| 2500 | if (ret != MSG_OK && IS_DEBUG_OSPF_PACKET (0, RECV)) |
| 2501 | zlog_debug ("%s: alignment error in %s", |
| 2502 | __func__, LOOKUP (ospf_lsa_type_msg, lsah->type)); |
| 2503 | return ret; |
| 2504 | } |
| 2505 | |
| 2506 | /* Verify if the provided input buffer is a valid sequence of LSAs. This |
| 2507 | includes verification of LSA blocks length/alignment and dispatching |
| 2508 | of deeper-level checks. */ |
| 2509 | static unsigned |
| 2510 | ospf_lsaseq_examin |
| 2511 | ( |
| 2512 | struct lsa_header *lsah, /* start of buffered data */ |
| 2513 | size_t length, |
| 2514 | const u_char headeronly, |
| 2515 | /* When declared_num_lsas is not 0, compare it to the real number of LSAs |
| 2516 | and treat the difference as an error. */ |
| 2517 | const u_int32_t declared_num_lsas |
| 2518 | ) |
| 2519 | { |
| 2520 | u_int32_t counted_lsas = 0; |
| 2521 | |
| 2522 | while (length) |
| 2523 | { |
| 2524 | u_int16_t lsalen; |
| 2525 | if (length < OSPF_LSA_HEADER_SIZE) |
| 2526 | { |
| 2527 | if (IS_DEBUG_OSPF_PACKET (0, RECV)) |
| 2528 | zlog_debug ("%s: undersized (%zu B) trailing (#%u) LSA header", |
| 2529 | __func__, length, counted_lsas); |
| 2530 | return MSG_NG; |
| 2531 | } |
| 2532 | /* save on ntohs() calls here and in the LSA validator */ |
| 2533 | lsalen = ntohs (lsah->length); |
| 2534 | if (lsalen < OSPF_LSA_HEADER_SIZE) |
| 2535 | { |
| 2536 | if (IS_DEBUG_OSPF_PACKET (0, RECV)) |
| 2537 | zlog_debug ("%s: malformed LSA header #%u, declared length is %u B", |
| 2538 | __func__, counted_lsas, lsalen); |
| 2539 | return MSG_NG; |
| 2540 | } |
| 2541 | if (headeronly) |
| 2542 | { |
| 2543 | /* less checks here and in ospf_lsa_examin() */ |
| 2544 | if (MSG_OK != ospf_lsa_examin (lsah, lsalen, 1)) |
| 2545 | { |
| 2546 | if (IS_DEBUG_OSPF_PACKET (0, RECV)) |
| 2547 | zlog_debug ("%s: malformed header-only LSA #%u", __func__, counted_lsas); |
| 2548 | return MSG_NG; |
| 2549 | } |
| 2550 | lsah = (struct lsa_header *) ((caddr_t) lsah + OSPF_LSA_HEADER_SIZE); |
| 2551 | length -= OSPF_LSA_HEADER_SIZE; |
| 2552 | } |
| 2553 | else |
| 2554 | { |
| 2555 | /* make sure the input buffer is deep enough before further checks */ |
| 2556 | if (lsalen > length) |
| 2557 | { |
| 2558 | if (IS_DEBUG_OSPF_PACKET (0, RECV)) |
| 2559 | zlog_debug ("%s: anomaly in LSA #%u: declared length is %u B, buffered length is %zu B", |
| 2560 | __func__, counted_lsas, lsalen, length); |
| 2561 | return MSG_NG; |
| 2562 | } |
| 2563 | if (MSG_OK != ospf_lsa_examin (lsah, lsalen, 0)) |
| 2564 | { |
| 2565 | if (IS_DEBUG_OSPF_PACKET (0, RECV)) |
| 2566 | zlog_debug ("%s: malformed LSA #%u", __func__, counted_lsas); |
| 2567 | return MSG_NG; |
| 2568 | } |
| 2569 | lsah = (struct lsa_header *) ((caddr_t) lsah + lsalen); |
| 2570 | length -= lsalen; |
| 2571 | } |
| 2572 | counted_lsas++; |
| 2573 | } |
| 2574 | |
| 2575 | if (declared_num_lsas && counted_lsas != declared_num_lsas) |
| 2576 | { |
| 2577 | if (IS_DEBUG_OSPF_PACKET (0, RECV)) |
| 2578 | zlog_debug ("%s: #LSAs declared (%u) does not match actual (%u)", |
| 2579 | __func__, declared_num_lsas, counted_lsas); |
| 2580 | return MSG_NG; |
| 2581 | } |
| 2582 | return MSG_OK; |
| 2583 | } |
| 2584 | |
Denis Ovsienko | 75c8eab | 2012-01-30 15:41:39 +0400 | [diff] [blame] | 2585 | /* Verify a complete OSPF packet for proper sizing/alignment. */ |
| 2586 | static unsigned |
| 2587 | ospf_packet_examin (struct ospf_header * oh, const unsigned bytesonwire) |
| 2588 | { |
Denis Ovsienko | bd5651f | 2012-02-26 17:59:43 +0400 | [diff] [blame] | 2589 | u_int16_t bytesdeclared, bytesauth; |
Denis Ovsienko | 4e31de7 | 2012-02-17 16:20:50 +0400 | [diff] [blame] | 2590 | unsigned ret; |
| 2591 | struct ospf_ls_update * lsupd; |
Denis Ovsienko | 75c8eab | 2012-01-30 15:41:39 +0400 | [diff] [blame] | 2592 | |
| 2593 | /* Length, 1st approximation. */ |
| 2594 | if (bytesonwire < OSPF_HEADER_SIZE) |
| 2595 | { |
| 2596 | if (IS_DEBUG_OSPF_PACKET (0, RECV)) |
| 2597 | zlog_debug ("%s: undersized (%u B) packet", __func__, bytesonwire); |
| 2598 | return MSG_NG; |
| 2599 | } |
| 2600 | /* Now it is safe to access header fields. Performing length check, allow |
| 2601 | * for possible extra bytes of crypto auth/padding, which are not counted |
| 2602 | * in the OSPF header "length" field. */ |
Denis Ovsienko | aee5674 | 2012-02-28 15:15:29 +0400 | [diff] [blame] | 2603 | if (oh->version != OSPF_VERSION) |
| 2604 | { |
| 2605 | if (IS_DEBUG_OSPF_PACKET (0, RECV)) |
| 2606 | zlog_debug ("%s: invalid (%u) protocol version", __func__, oh->version); |
| 2607 | return MSG_NG; |
| 2608 | } |
Denis Ovsienko | 75c8eab | 2012-01-30 15:41:39 +0400 | [diff] [blame] | 2609 | bytesdeclared = ntohs (oh->length); |
Denis Ovsienko | bd5651f | 2012-02-26 17:59:43 +0400 | [diff] [blame] | 2610 | if (ntohs (oh->auth_type) != OSPF_AUTH_CRYPTOGRAPHIC) |
| 2611 | bytesauth = 0; |
| 2612 | else |
| 2613 | { |
| 2614 | if (oh->u.crypt.auth_data_len != OSPF_AUTH_MD5_SIZE) |
| 2615 | { |
| 2616 | if (IS_DEBUG_OSPF_PACKET (0, RECV)) |
| 2617 | zlog_debug ("%s: unsupported crypto auth length (%u B)", |
| 2618 | __func__, oh->u.crypt.auth_data_len); |
| 2619 | return MSG_NG; |
| 2620 | } |
| 2621 | bytesauth = OSPF_AUTH_MD5_SIZE; |
| 2622 | } |
| 2623 | if (bytesdeclared + bytesauth > bytesonwire) |
Denis Ovsienko | 75c8eab | 2012-01-30 15:41:39 +0400 | [diff] [blame] | 2624 | { |
| 2625 | if (IS_DEBUG_OSPF_PACKET (0, RECV)) |
Denis Ovsienko | bd5651f | 2012-02-26 17:59:43 +0400 | [diff] [blame] | 2626 | zlog_debug ("%s: packet length error (%u real, %u+%u declared)", |
| 2627 | __func__, bytesonwire, bytesdeclared, bytesauth); |
Denis Ovsienko | 75c8eab | 2012-01-30 15:41:39 +0400 | [diff] [blame] | 2628 | return MSG_NG; |
| 2629 | } |
| 2630 | /* Length, 2nd approximation. The type-specific constraint is checked |
| 2631 | against declared length, not amount of bytes on wire. */ |
| 2632 | if |
| 2633 | ( |
| 2634 | oh->type >= OSPF_MSG_HELLO && |
| 2635 | oh->type <= OSPF_MSG_LS_ACK && |
| 2636 | bytesdeclared < OSPF_HEADER_SIZE + ospf_packet_minlen[oh->type] |
| 2637 | ) |
| 2638 | { |
| 2639 | if (IS_DEBUG_OSPF_PACKET (0, RECV)) |
| 2640 | zlog_debug ("%s: undersized (%u B) %s packet", __func__, |
| 2641 | bytesdeclared, LOOKUP (ospf_packet_type_str, oh->type)); |
| 2642 | return MSG_NG; |
| 2643 | } |
Denis Ovsienko | 4e31de7 | 2012-02-17 16:20:50 +0400 | [diff] [blame] | 2644 | switch (oh->type) |
| 2645 | { |
| 2646 | case OSPF_MSG_HELLO: |
| 2647 | /* RFC2328 A.3.2, packet header + OSPF_HELLO_MIN_SIZE bytes followed |
| 2648 | by N>=0 router-IDs. */ |
Denis Ovsienko | b29adf9 | 2012-02-20 23:08:10 +0400 | [diff] [blame] | 2649 | ret = (bytesdeclared - OSPF_HEADER_SIZE - OSPF_HELLO_MIN_SIZE) % 4 ? MSG_NG : MSG_OK; |
Denis Ovsienko | 4e31de7 | 2012-02-17 16:20:50 +0400 | [diff] [blame] | 2650 | break; |
| 2651 | case OSPF_MSG_DB_DESC: |
| 2652 | /* RFC2328 A.3.3, packet header + OSPF_DB_DESC_MIN_SIZE bytes followed |
| 2653 | by N>=0 header-only LSAs. */ |
| 2654 | ret = ospf_lsaseq_examin |
| 2655 | ( |
| 2656 | (struct lsa_header *) ((caddr_t) oh + OSPF_HEADER_SIZE + OSPF_DB_DESC_MIN_SIZE), |
Denis Ovsienko | b29adf9 | 2012-02-20 23:08:10 +0400 | [diff] [blame] | 2657 | bytesdeclared - OSPF_HEADER_SIZE - OSPF_DB_DESC_MIN_SIZE, |
Denis Ovsienko | 4e31de7 | 2012-02-17 16:20:50 +0400 | [diff] [blame] | 2658 | 1, /* header-only LSAs */ |
| 2659 | 0 |
| 2660 | ); |
| 2661 | break; |
| 2662 | case OSPF_MSG_LS_REQ: |
| 2663 | /* RFC2328 A.3.4, packet header followed by N>=0 12-bytes request blocks. */ |
Denis Ovsienko | b29adf9 | 2012-02-20 23:08:10 +0400 | [diff] [blame] | 2664 | ret = (bytesdeclared - OSPF_HEADER_SIZE - OSPF_LS_REQ_MIN_SIZE) % |
Denis Ovsienko | 4e31de7 | 2012-02-17 16:20:50 +0400 | [diff] [blame] | 2665 | OSPF_LSA_KEY_SIZE ? MSG_NG : MSG_OK; |
| 2666 | break; |
| 2667 | case OSPF_MSG_LS_UPD: |
| 2668 | /* RFC2328 A.3.5, packet header + OSPF_LS_UPD_MIN_SIZE bytes followed |
| 2669 | by N>=0 full LSAs (with N declared beforehand). */ |
| 2670 | lsupd = (struct ospf_ls_update *) ((caddr_t) oh + OSPF_HEADER_SIZE); |
| 2671 | ret = ospf_lsaseq_examin |
| 2672 | ( |
| 2673 | (struct lsa_header *) ((caddr_t) lsupd + OSPF_LS_UPD_MIN_SIZE), |
Denis Ovsienko | b29adf9 | 2012-02-20 23:08:10 +0400 | [diff] [blame] | 2674 | bytesdeclared - OSPF_HEADER_SIZE - OSPF_LS_UPD_MIN_SIZE, |
Denis Ovsienko | 4e31de7 | 2012-02-17 16:20:50 +0400 | [diff] [blame] | 2675 | 0, /* full LSAs */ |
| 2676 | ntohl (lsupd->num_lsas) /* 32 bits */ |
| 2677 | ); |
| 2678 | break; |
| 2679 | case OSPF_MSG_LS_ACK: |
| 2680 | /* RFC2328 A.3.6, packet header followed by N>=0 header-only LSAs. */ |
| 2681 | ret = ospf_lsaseq_examin |
| 2682 | ( |
| 2683 | (struct lsa_header *) ((caddr_t) oh + OSPF_HEADER_SIZE + OSPF_LS_ACK_MIN_SIZE), |
Denis Ovsienko | b29adf9 | 2012-02-20 23:08:10 +0400 | [diff] [blame] | 2684 | bytesdeclared - OSPF_HEADER_SIZE - OSPF_LS_ACK_MIN_SIZE, |
Denis Ovsienko | 4e31de7 | 2012-02-17 16:20:50 +0400 | [diff] [blame] | 2685 | 1, /* header-only LSAs */ |
| 2686 | 0 |
| 2687 | ); |
| 2688 | break; |
| 2689 | default: |
| 2690 | if (IS_DEBUG_OSPF_PACKET (0, RECV)) |
| 2691 | zlog_debug ("%s: invalid packet type 0x%02x", __func__, oh->type); |
| 2692 | return MSG_NG; |
| 2693 | } |
| 2694 | if (ret != MSG_OK && IS_DEBUG_OSPF_PACKET (0, RECV)) |
| 2695 | zlog_debug ("%s: malformed %s packet", __func__, LOOKUP (ospf_packet_type_str, oh->type)); |
| 2696 | return ret; |
Denis Ovsienko | 75c8eab | 2012-01-30 15:41:39 +0400 | [diff] [blame] | 2697 | } |
| 2698 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2699 | /* OSPF Header verification. */ |
paul | 4dadc29 | 2005-05-06 21:37:42 +0000 | [diff] [blame] | 2700 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2701 | ospf_verify_header (struct stream *ibuf, struct ospf_interface *oi, |
| 2702 | struct ip *iph, struct ospf_header *ospfh) |
| 2703 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2704 | /* Check Area ID. */ |
| 2705 | if (!ospf_check_area_id (oi, ospfh)) |
| 2706 | { |
| 2707 | zlog_warn ("interface %s: ospf_read invalid Area ID %s.", |
| 2708 | IF_NAME (oi), inet_ntoa (ospfh->area_id)); |
| 2709 | return -1; |
| 2710 | } |
| 2711 | |
| 2712 | /* Check network mask, Silently discarded. */ |
| 2713 | if (! ospf_check_network_mask (oi, iph->ip_src)) |
| 2714 | { |
| 2715 | zlog_warn ("interface %s: ospf_read network address is not same [%s]", |
| 2716 | IF_NAME (oi), inet_ntoa (iph->ip_src)); |
| 2717 | return -1; |
| 2718 | } |
| 2719 | |
Denis Ovsienko | bd5651f | 2012-02-26 17:59:43 +0400 | [diff] [blame] | 2720 | /* Check authentication. The function handles logging actions, where required. */ |
Denis Ovsienko | e525914 | 2012-01-30 16:07:18 +0400 | [diff] [blame] | 2721 | if (! ospf_check_auth (oi, ospfh)) |
Denis Ovsienko | bd5651f | 2012-02-26 17:59:43 +0400 | [diff] [blame] | 2722 | return -1; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2723 | |
| 2724 | return 0; |
| 2725 | } |
| 2726 | |
| 2727 | /* Starting point of packet process function. */ |
| 2728 | int |
| 2729 | ospf_read (struct thread *thread) |
| 2730 | { |
| 2731 | int ret; |
| 2732 | struct stream *ibuf; |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 2733 | struct ospf *ospf; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2734 | struct ospf_interface *oi; |
| 2735 | struct ip *iph; |
| 2736 | struct ospf_header *ospfh; |
| 2737 | u_int16_t length; |
| 2738 | struct interface *ifp; |
| 2739 | |
| 2740 | /* first of all get interface pointer. */ |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 2741 | ospf = THREAD_ARG (thread); |
ajs | 038163f | 2005-02-17 19:55:59 +0000 | [diff] [blame] | 2742 | |
| 2743 | /* prepare for next packet. */ |
| 2744 | ospf->t_read = thread_add_read (master, ospf_read, ospf, ospf->fd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2745 | |
ajs | 5c33349 | 2005-02-23 15:43:01 +0000 | [diff] [blame] | 2746 | stream_reset(ospf->ibuf); |
| 2747 | if (!(ibuf = ospf_recv_packet (ospf->fd, &ifp, ospf->ibuf))) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2748 | return -1; |
Denis Ovsienko | 75c8eab | 2012-01-30 15:41:39 +0400 | [diff] [blame] | 2749 | /* This raw packet is known to be at least as big as its IP header. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2750 | |
ajs | 5c33349 | 2005-02-23 15:43:01 +0000 | [diff] [blame] | 2751 | /* Note that there should not be alignment problems with this assignment |
| 2752 | because this is at the beginning of the stream data buffer. */ |
paul | 06f953f | 2004-10-22 17:00:38 +0000 | [diff] [blame] | 2753 | iph = (struct ip *) STREAM_DATA (ibuf); |
ajs | 5c33349 | 2005-02-23 15:43:01 +0000 | [diff] [blame] | 2754 | /* Note that sockopt_iphdrincl_swab_systoh was called in ospf_recv_packet. */ |
paul | 06f953f | 2004-10-22 17:00:38 +0000 | [diff] [blame] | 2755 | |
paul | ac19123 | 2004-10-22 12:05:17 +0000 | [diff] [blame] | 2756 | if (ifp == NULL) |
ajs | b87f772 | 2004-12-29 20:41:26 +0000 | [diff] [blame] | 2757 | /* Handle cases where the platform does not support retrieving the ifindex, |
| 2758 | and also platforms (such as Solaris 8) that claim to support ifindex |
| 2759 | retrieval but do not. */ |
paul | ac19123 | 2004-10-22 12:05:17 +0000 | [diff] [blame] | 2760 | ifp = if_lookup_address (iph->ip_src); |
paul | ac19123 | 2004-10-22 12:05:17 +0000 | [diff] [blame] | 2761 | |
paul | d3f0d62 | 2004-05-05 15:27:15 +0000 | [diff] [blame] | 2762 | if (ifp == NULL) |
ajs | 5c33349 | 2005-02-23 15:43:01 +0000 | [diff] [blame] | 2763 | return 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2764 | |
| 2765 | /* IP Header dump. */ |
paul | 17b78d3 | 2003-02-13 22:04:01 +0000 | [diff] [blame] | 2766 | if (IS_DEBUG_OSPF_PACKET(0, RECV)) |
paul | 6b33361 | 2004-10-11 10:11:25 +0000 | [diff] [blame] | 2767 | ospf_ip_header_dump (iph); |
paul | 7d95c61 | 2003-01-27 12:00:55 +0000 | [diff] [blame] | 2768 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2769 | /* Self-originated packet should be discarded silently. */ |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 2770 | if (ospf_if_lookup_by_local_addr (ospf, NULL, iph->ip_src)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2771 | { |
paul | d324181 | 2003-09-29 12:42:39 +0000 | [diff] [blame] | 2772 | if (IS_DEBUG_OSPF_PACKET (0, RECV)) |
| 2773 | { |
ajs | 2a42e28 | 2004-12-08 18:43:03 +0000 | [diff] [blame] | 2774 | zlog_debug ("ospf_read[%s]: Dropping self-originated packet", |
paul | d324181 | 2003-09-29 12:42:39 +0000 | [diff] [blame] | 2775 | inet_ntoa (iph->ip_src)); |
| 2776 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2777 | return 0; |
| 2778 | } |
| 2779 | |
Denis Ovsienko | 61ab030 | 2011-09-26 13:17:52 +0400 | [diff] [blame] | 2780 | /* Advance from IP header to OSPF header (iph->ip_hl has been verified |
| 2781 | by ospf_recv_packet() to be correct). */ |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 2782 | stream_forward_getp (ibuf, iph->ip_hl * 4); |
Denis Ovsienko | 61ab030 | 2011-09-26 13:17:52 +0400 | [diff] [blame] | 2783 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2784 | ospfh = (struct ospf_header *) STREAM_PNT (ibuf); |
Denis Ovsienko | 75c8eab | 2012-01-30 15:41:39 +0400 | [diff] [blame] | 2785 | if (MSG_OK != ospf_packet_examin (ospfh, stream_get_endp (ibuf) - stream_get_getp (ibuf))) |
| 2786 | return -1; |
| 2787 | /* Now it is safe to access all fields of OSPF packet header. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2788 | |
| 2789 | /* associate packet with ospf interface */ |
Joakim Tjernlund | 05cf46b | 2009-07-27 12:42:30 +0200 | [diff] [blame] | 2790 | oi = ospf_if_lookup_recv_if (ospf, iph->ip_src, ifp); |
paul | d3f0d62 | 2004-05-05 15:27:15 +0000 | [diff] [blame] | 2791 | |
YAMAMOTO Shigeru | 3aad46b | 2011-09-28 21:00:14 +0400 | [diff] [blame] | 2792 | /* ospf_verify_header() relies on a valid "oi" and thus can be called only |
| 2793 | after the passive/backbone/other checks below are passed. These checks |
| 2794 | in turn access the fields of unverified "ospfh" structure for their own |
| 2795 | purposes and must remain very accurate in doing this. */ |
Denis Ovsienko | 7177504 | 2011-09-26 13:18:02 +0400 | [diff] [blame] | 2796 | |
Joakim Tjernlund | 491eddc | 2008-09-24 17:03:59 +0100 | [diff] [blame] | 2797 | /* If incoming interface is passive one, ignore it. */ |
| 2798 | if (oi && OSPF_IF_PASSIVE_STATUS (oi) == OSPF_IF_PASSIVE) |
| 2799 | { |
| 2800 | char buf[3][INET_ADDRSTRLEN]; |
| 2801 | |
| 2802 | if (IS_DEBUG_OSPF_EVENT) |
| 2803 | zlog_debug ("ignoring packet from router %s sent to %s, " |
| 2804 | "received on a passive interface, %s", |
| 2805 | inet_ntop(AF_INET, &ospfh->router_id, buf[0], sizeof(buf[0])), |
| 2806 | inet_ntop(AF_INET, &iph->ip_dst, buf[1], sizeof(buf[1])), |
| 2807 | inet_ntop(AF_INET, &oi->address->u.prefix4, |
| 2808 | buf[2], sizeof(buf[2]))); |
| 2809 | |
| 2810 | if (iph->ip_dst.s_addr == htonl(OSPF_ALLSPFROUTERS)) |
| 2811 | { |
| 2812 | /* Try to fix multicast membership. |
| 2813 | * Some OS:es may have problems in this area, |
| 2814 | * make sure it is removed. |
| 2815 | */ |
| 2816 | OI_MEMBER_JOINED(oi, MEMBER_ALLROUTERS); |
| 2817 | ospf_if_set_multicast(oi); |
| 2818 | } |
| 2819 | return 0; |
| 2820 | } |
| 2821 | |
| 2822 | |
paul | d3f0d62 | 2004-05-05 15:27:15 +0000 | [diff] [blame] | 2823 | /* if no local ospf_interface, |
| 2824 | * or header area is backbone but ospf_interface is not |
| 2825 | * check for VLINK interface |
| 2826 | */ |
| 2827 | if ( (oi == NULL) || |
| 2828 | (OSPF_IS_AREA_ID_BACKBONE(ospfh->area_id) |
| 2829 | && !OSPF_IS_AREA_ID_BACKBONE(oi->area->area_id)) |
| 2830 | ) |
| 2831 | { |
| 2832 | if ((oi = ospf_associate_packet_vl (ospf, ifp, iph, ospfh)) == NULL) |
| 2833 | { |
Paul Jakma | 88871b1 | 2006-06-15 11:41:19 +0000 | [diff] [blame] | 2834 | if (IS_DEBUG_OSPF_EVENT) |
| 2835 | zlog_debug ("Packet from [%s] received on link %s" |
| 2836 | " but no ospf_interface", |
| 2837 | inet_ntoa (iph->ip_src), ifp->name); |
paul | d3f0d62 | 2004-05-05 15:27:15 +0000 | [diff] [blame] | 2838 | return 0; |
| 2839 | } |
| 2840 | } |
Joakim Tjernlund | 05cf46b | 2009-07-27 12:42:30 +0200 | [diff] [blame] | 2841 | |
paul | d3f0d62 | 2004-05-05 15:27:15 +0000 | [diff] [blame] | 2842 | /* else it must be a local ospf interface, check it was received on |
| 2843 | * correct link |
| 2844 | */ |
| 2845 | else if (oi->ifp != ifp) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2846 | { |
Paul Jakma | 1163743 | 2009-08-11 12:25:42 +0100 | [diff] [blame] | 2847 | if (IS_DEBUG_OSPF_EVENT) |
| 2848 | zlog_warn ("Packet from [%s] received on wrong link %s", |
| 2849 | inet_ntoa (iph->ip_src), ifp->name); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2850 | return 0; |
| 2851 | } |
ajs | 847947f | 2005-02-02 18:38:48 +0000 | [diff] [blame] | 2852 | else if (oi->state == ISM_Down) |
ajs | c3eab87 | 2005-01-29 15:52:07 +0000 | [diff] [blame] | 2853 | { |
ajs | ba6454e | 2005-02-08 15:37:30 +0000 | [diff] [blame] | 2854 | char buf[2][INET_ADDRSTRLEN]; |
| 2855 | zlog_warn ("Ignoring packet from %s to %s received on interface that is " |
ajs | 847947f | 2005-02-02 18:38:48 +0000 | [diff] [blame] | 2856 | "down [%s]; interface flags are %s", |
ajs | ba6454e | 2005-02-08 15:37:30 +0000 | [diff] [blame] | 2857 | inet_ntop(AF_INET, &iph->ip_src, buf[0], sizeof(buf[0])), |
| 2858 | inet_ntop(AF_INET, &iph->ip_dst, buf[1], sizeof(buf[1])), |
| 2859 | ifp->name, if_flag_dump(ifp->flags)); |
ajs | ba6454e | 2005-02-08 15:37:30 +0000 | [diff] [blame] | 2860 | /* Fix multicast memberships? */ |
| 2861 | if (iph->ip_dst.s_addr == htonl(OSPF_ALLSPFROUTERS)) |
Paul Jakma | 429ac78 | 2006-06-15 18:40:49 +0000 | [diff] [blame] | 2862 | OI_MEMBER_JOINED(oi, MEMBER_ALLROUTERS); |
ajs | ba6454e | 2005-02-08 15:37:30 +0000 | [diff] [blame] | 2863 | else if (iph->ip_dst.s_addr == htonl(OSPF_ALLDROUTERS)) |
Paul Jakma | 429ac78 | 2006-06-15 18:40:49 +0000 | [diff] [blame] | 2864 | OI_MEMBER_JOINED(oi, MEMBER_DROUTERS); |
ajs | ba6454e | 2005-02-08 15:37:30 +0000 | [diff] [blame] | 2865 | if (oi->multicast_memberships) |
| 2866 | ospf_if_set_multicast(oi); |
ajs | c3eab87 | 2005-01-29 15:52:07 +0000 | [diff] [blame] | 2867 | return 0; |
| 2868 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2869 | |
| 2870 | /* |
| 2871 | * If the received packet is destined for AllDRouters, the packet |
| 2872 | * should be accepted only if the received ospf interface state is |
| 2873 | * either DR or Backup -- endo. |
| 2874 | */ |
| 2875 | if (iph->ip_dst.s_addr == htonl (OSPF_ALLDROUTERS) |
| 2876 | && (oi->state != ISM_DR && oi->state != ISM_Backup)) |
| 2877 | { |
ajs | ba6454e | 2005-02-08 15:37:30 +0000 | [diff] [blame] | 2878 | zlog_warn ("Dropping packet for AllDRouters from [%s] via [%s] (ISM: %s)", |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2879 | inet_ntoa (iph->ip_src), IF_NAME (oi), |
| 2880 | LOOKUP (ospf_ism_state_msg, oi->state)); |
ajs | ba6454e | 2005-02-08 15:37:30 +0000 | [diff] [blame] | 2881 | /* Try to fix multicast membership. */ |
| 2882 | SET_FLAG(oi->multicast_memberships, MEMBER_DROUTERS); |
| 2883 | ospf_if_set_multicast(oi); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2884 | return 0; |
| 2885 | } |
| 2886 | |
YAMAMOTO Shigeru | 3aad46b | 2011-09-28 21:00:14 +0400 | [diff] [blame] | 2887 | /* Verify more OSPF header fields. */ |
| 2888 | ret = ospf_verify_header (ibuf, oi, iph, ospfh); |
| 2889 | if (ret < 0) |
| 2890 | { |
| 2891 | if (IS_DEBUG_OSPF_PACKET (0, RECV)) |
| 2892 | zlog_debug ("ospf_read[%s]: Header check failed, " |
| 2893 | "dropping.", |
| 2894 | inet_ntoa (iph->ip_src)); |
| 2895 | return ret; |
| 2896 | } |
| 2897 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2898 | /* Show debug receiving packet. */ |
paul | 1aa7b39 | 2003-04-08 08:51:58 +0000 | [diff] [blame] | 2899 | if (IS_DEBUG_OSPF_PACKET (ospfh->type - 1, RECV)) |
| 2900 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2901 | if (IS_DEBUG_OSPF_PACKET (ospfh->type - 1, DETAIL)) |
paul | 1aa7b39 | 2003-04-08 08:51:58 +0000 | [diff] [blame] | 2902 | { |
ajs | 2a42e28 | 2004-12-08 18:43:03 +0000 | [diff] [blame] | 2903 | zlog_debug ("-----------------------------------------------------"); |
paul | 1aa7b39 | 2003-04-08 08:51:58 +0000 | [diff] [blame] | 2904 | ospf_packet_dump (ibuf); |
| 2905 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2906 | |
ajs | 2a42e28 | 2004-12-08 18:43:03 +0000 | [diff] [blame] | 2907 | zlog_debug ("%s received from [%s] via [%s]", |
Denis Ovsienko | 272ca1e | 2012-01-15 19:12:19 +0400 | [diff] [blame] | 2908 | LOOKUP (ospf_packet_type_str, ospfh->type), |
paul | 1aa7b39 | 2003-04-08 08:51:58 +0000 | [diff] [blame] | 2909 | inet_ntoa (ospfh->router_id), IF_NAME (oi)); |
ajs | 2a42e28 | 2004-12-08 18:43:03 +0000 | [diff] [blame] | 2910 | zlog_debug (" src [%s],", inet_ntoa (iph->ip_src)); |
| 2911 | zlog_debug (" dst [%s]", inet_ntoa (iph->ip_dst)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2912 | |
| 2913 | if (IS_DEBUG_OSPF_PACKET (ospfh->type - 1, DETAIL)) |
ajs | 2a42e28 | 2004-12-08 18:43:03 +0000 | [diff] [blame] | 2914 | zlog_debug ("-----------------------------------------------------"); |
paul | 1aa7b39 | 2003-04-08 08:51:58 +0000 | [diff] [blame] | 2915 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2916 | |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 2917 | stream_forward_getp (ibuf, OSPF_HEADER_SIZE); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2918 | |
| 2919 | /* Adjust size to message length. */ |
| 2920 | length = ntohs (ospfh->length) - OSPF_HEADER_SIZE; |
| 2921 | |
| 2922 | /* Read rest of the packet and call each sort of packet routine. */ |
| 2923 | switch (ospfh->type) |
| 2924 | { |
| 2925 | case OSPF_MSG_HELLO: |
| 2926 | ospf_hello (iph, ospfh, ibuf, oi, length); |
| 2927 | break; |
| 2928 | case OSPF_MSG_DB_DESC: |
| 2929 | ospf_db_desc (iph, ospfh, ibuf, oi, length); |
| 2930 | break; |
| 2931 | case OSPF_MSG_LS_REQ: |
| 2932 | ospf_ls_req (iph, ospfh, ibuf, oi, length); |
| 2933 | break; |
| 2934 | case OSPF_MSG_LS_UPD: |
| 2935 | ospf_ls_upd (iph, ospfh, ibuf, oi, length); |
| 2936 | break; |
| 2937 | case OSPF_MSG_LS_ACK: |
| 2938 | ospf_ls_ack (iph, ospfh, ibuf, oi, length); |
| 2939 | break; |
| 2940 | default: |
| 2941 | zlog (NULL, LOG_WARNING, |
| 2942 | "interface %s: OSPF packet header type %d is illegal", |
| 2943 | IF_NAME (oi), ospfh->type); |
| 2944 | break; |
| 2945 | } |
| 2946 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2947 | return 0; |
| 2948 | } |
| 2949 | |
| 2950 | /* Make OSPF header. */ |
paul | 4dadc29 | 2005-05-06 21:37:42 +0000 | [diff] [blame] | 2951 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2952 | ospf_make_header (int type, struct ospf_interface *oi, struct stream *s) |
| 2953 | { |
| 2954 | struct ospf_header *ospfh; |
| 2955 | |
| 2956 | ospfh = (struct ospf_header *) STREAM_DATA (s); |
| 2957 | |
| 2958 | ospfh->version = (u_char) OSPF_VERSION; |
| 2959 | ospfh->type = (u_char) type; |
| 2960 | |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 2961 | ospfh->router_id = oi->ospf->router_id; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2962 | |
| 2963 | ospfh->checksum = 0; |
| 2964 | ospfh->area_id = oi->area->area_id; |
| 2965 | ospfh->auth_type = htons (ospf_auth_type (oi)); |
| 2966 | |
| 2967 | memset (ospfh->u.auth_data, 0, OSPF_AUTH_SIMPLE_SIZE); |
| 2968 | |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 2969 | stream_forward_endp (s, OSPF_HEADER_SIZE); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2970 | } |
| 2971 | |
| 2972 | /* Make Authentication Data. */ |
paul | 4dadc29 | 2005-05-06 21:37:42 +0000 | [diff] [blame] | 2973 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2974 | ospf_make_auth (struct ospf_interface *oi, struct ospf_header *ospfh) |
| 2975 | { |
| 2976 | struct crypt_key *ck; |
| 2977 | |
| 2978 | switch (ospf_auth_type (oi)) |
| 2979 | { |
| 2980 | case OSPF_AUTH_NULL: |
| 2981 | /* memset (ospfh->u.auth_data, 0, sizeof (ospfh->u.auth_data)); */ |
| 2982 | break; |
| 2983 | case OSPF_AUTH_SIMPLE: |
| 2984 | memcpy (ospfh->u.auth_data, OSPF_IF_PARAM (oi, auth_simple), |
| 2985 | OSPF_AUTH_SIMPLE_SIZE); |
| 2986 | break; |
| 2987 | case OSPF_AUTH_CRYPTOGRAPHIC: |
| 2988 | /* If key is not set, then set 0. */ |
| 2989 | if (list_isempty (OSPF_IF_PARAM (oi, auth_crypt))) |
| 2990 | { |
| 2991 | ospfh->u.crypt.zero = 0; |
| 2992 | ospfh->u.crypt.key_id = 0; |
| 2993 | ospfh->u.crypt.auth_data_len = OSPF_AUTH_MD5_SIZE; |
| 2994 | } |
| 2995 | else |
| 2996 | { |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 2997 | ck = listgetdata (listtail(OSPF_IF_PARAM (oi, auth_crypt))); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2998 | ospfh->u.crypt.zero = 0; |
| 2999 | ospfh->u.crypt.key_id = ck->key_id; |
| 3000 | ospfh->u.crypt.auth_data_len = OSPF_AUTH_MD5_SIZE; |
| 3001 | } |
| 3002 | /* note: the seq is done in ospf_make_md5_digest() */ |
| 3003 | break; |
| 3004 | default: |
| 3005 | /* memset (ospfh->u.auth_data, 0, sizeof (ospfh->u.auth_data)); */ |
| 3006 | break; |
| 3007 | } |
| 3008 | |
| 3009 | return 0; |
| 3010 | } |
| 3011 | |
| 3012 | /* Fill rest of OSPF header. */ |
paul | 4dadc29 | 2005-05-06 21:37:42 +0000 | [diff] [blame] | 3013 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3014 | ospf_fill_header (struct ospf_interface *oi, |
| 3015 | struct stream *s, u_int16_t length) |
| 3016 | { |
| 3017 | struct ospf_header *ospfh; |
| 3018 | |
| 3019 | ospfh = (struct ospf_header *) STREAM_DATA (s); |
| 3020 | |
| 3021 | /* Fill length. */ |
| 3022 | ospfh->length = htons (length); |
| 3023 | |
| 3024 | /* Calculate checksum. */ |
| 3025 | if (ntohs (ospfh->auth_type) != OSPF_AUTH_CRYPTOGRAPHIC) |
| 3026 | ospfh->checksum = in_cksum (ospfh, length); |
| 3027 | else |
| 3028 | ospfh->checksum = 0; |
| 3029 | |
| 3030 | /* Add Authentication Data. */ |
| 3031 | ospf_make_auth (oi, ospfh); |
| 3032 | } |
| 3033 | |
paul | 4dadc29 | 2005-05-06 21:37:42 +0000 | [diff] [blame] | 3034 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3035 | ospf_make_hello (struct ospf_interface *oi, struct stream *s) |
| 3036 | { |
| 3037 | struct ospf_neighbor *nbr; |
| 3038 | struct route_node *rn; |
| 3039 | u_int16_t length = OSPF_HELLO_MIN_SIZE; |
| 3040 | struct in_addr mask; |
| 3041 | unsigned long p; |
| 3042 | int flag = 0; |
| 3043 | |
| 3044 | /* Set netmask of interface. */ |
| 3045 | if (oi->type != OSPF_IFTYPE_POINTOPOINT && |
| 3046 | oi->type != OSPF_IFTYPE_VIRTUALLINK) |
| 3047 | masklen2ip (oi->address->prefixlen, &mask); |
| 3048 | else |
| 3049 | memset ((char *) &mask, 0, sizeof (struct in_addr)); |
| 3050 | stream_put_ipv4 (s, mask.s_addr); |
| 3051 | |
| 3052 | /* Set Hello Interval. */ |
paul | f9ad937 | 2005-10-21 00:45:17 +0000 | [diff] [blame] | 3053 | if (OSPF_IF_PARAM (oi, fast_hello) == 0) |
| 3054 | stream_putw (s, OSPF_IF_PARAM (oi, v_hello)); |
| 3055 | else |
| 3056 | stream_putw (s, 0); /* hello-interval of 0 for fast-hellos */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3057 | |
| 3058 | if (IS_DEBUG_OSPF_EVENT) |
ajs | 2a42e28 | 2004-12-08 18:43:03 +0000 | [diff] [blame] | 3059 | zlog_debug ("make_hello: options: %x, int: %s", |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3060 | OPTIONS(oi), IF_NAME (oi)); |
| 3061 | |
| 3062 | /* Set Options. */ |
| 3063 | stream_putc (s, OPTIONS (oi)); |
| 3064 | |
| 3065 | /* Set Router Priority. */ |
| 3066 | stream_putc (s, PRIORITY (oi)); |
| 3067 | |
| 3068 | /* Set Router Dead Interval. */ |
| 3069 | stream_putl (s, OSPF_IF_PARAM (oi, v_wait)); |
| 3070 | |
| 3071 | /* Set Designated Router. */ |
| 3072 | stream_put_ipv4 (s, DR (oi).s_addr); |
| 3073 | |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 3074 | p = stream_get_endp (s); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3075 | |
| 3076 | /* Set Backup Designated Router. */ |
| 3077 | stream_put_ipv4 (s, BDR (oi).s_addr); |
| 3078 | |
| 3079 | /* Add neighbor seen. */ |
| 3080 | for (rn = route_top (oi->nbrs); rn; rn = route_next (rn)) |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 3081 | if ((nbr = rn->info)) |
| 3082 | if (nbr->router_id.s_addr != 0) /* Ignore 0.0.0.0 node. */ |
| 3083 | if (nbr->state != NSM_Attempt) /* Ignore Down neighbor. */ |
| 3084 | if (nbr->state != NSM_Down) /* This is myself for DR election. */ |
| 3085 | if (!IPV4_ADDR_SAME (&nbr->router_id, &oi->ospf->router_id)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3086 | { |
| 3087 | /* Check neighbor is sane? */ |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 3088 | if (nbr->d_router.s_addr != 0 |
| 3089 | && IPV4_ADDR_SAME (&nbr->d_router, &oi->address->u.prefix4) |
| 3090 | && IPV4_ADDR_SAME (&nbr->bd_router, &oi->address->u.prefix4)) |
| 3091 | flag = 1; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3092 | |
| 3093 | stream_put_ipv4 (s, nbr->router_id.s_addr); |
| 3094 | length += 4; |
| 3095 | } |
| 3096 | |
| 3097 | /* Let neighbor generate BackupSeen. */ |
| 3098 | if (flag == 1) |
paul | 3a9eb09 | 2005-02-08 11:29:41 +0000 | [diff] [blame] | 3099 | stream_putl_at (s, p, 0); /* ipv4 address, normally */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3100 | |
| 3101 | return length; |
| 3102 | } |
| 3103 | |
paul | 4dadc29 | 2005-05-06 21:37:42 +0000 | [diff] [blame] | 3104 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3105 | ospf_make_db_desc (struct ospf_interface *oi, struct ospf_neighbor *nbr, |
| 3106 | struct stream *s) |
| 3107 | { |
| 3108 | struct ospf_lsa *lsa; |
| 3109 | u_int16_t length = OSPF_DB_DESC_MIN_SIZE; |
| 3110 | u_char options; |
| 3111 | unsigned long pp; |
| 3112 | int i; |
| 3113 | struct ospf_lsdb *lsdb; |
| 3114 | |
| 3115 | /* Set Interface MTU. */ |
| 3116 | if (oi->type == OSPF_IFTYPE_VIRTUALLINK) |
| 3117 | stream_putw (s, 0); |
| 3118 | else |
| 3119 | stream_putw (s, oi->ifp->mtu); |
| 3120 | |
| 3121 | /* Set Options. */ |
| 3122 | options = OPTIONS (oi); |
| 3123 | #ifdef HAVE_OPAQUE_LSA |
paul | 6898008 | 2003-03-25 05:07:42 +0000 | [diff] [blame] | 3124 | if (CHECK_FLAG (oi->ospf->config, OSPF_OPAQUE_CAPABLE)) |
Lu Feng | 86ce951 | 2015-01-08 01:39:18 +0000 | [diff] [blame] | 3125 | SET_FLAG (options, OSPF_OPTION_O); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3126 | #endif /* HAVE_OPAQUE_LSA */ |
| 3127 | stream_putc (s, options); |
| 3128 | |
Paul Jakma | 8dd24ee | 2006-08-27 06:29:30 +0000 | [diff] [blame] | 3129 | /* DD flags */ |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 3130 | pp = stream_get_endp (s); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3131 | stream_putc (s, nbr->dd_flags); |
| 3132 | |
| 3133 | /* Set DD Sequence Number. */ |
| 3134 | stream_putl (s, nbr->dd_seqnum); |
| 3135 | |
Paul Jakma | b5aeb44 | 2006-08-30 18:47:37 +0000 | [diff] [blame] | 3136 | /* shortcut unneeded walk of (empty) summary LSDBs */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3137 | if (ospf_db_summary_isempty (nbr)) |
Paul Jakma | b5aeb44 | 2006-08-30 18:47:37 +0000 | [diff] [blame] | 3138 | goto empty; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3139 | |
| 3140 | /* Describe LSA Header from Database Summary List. */ |
| 3141 | lsdb = &nbr->db_sum; |
| 3142 | |
| 3143 | for (i = OSPF_MIN_LSA; i < OSPF_MAX_LSA; i++) |
| 3144 | { |
| 3145 | struct route_table *table = lsdb->type[i].db; |
| 3146 | struct route_node *rn; |
| 3147 | |
| 3148 | for (rn = route_top (table); rn; rn = route_next (rn)) |
| 3149 | if ((lsa = rn->info) != NULL) |
| 3150 | { |
| 3151 | #ifdef HAVE_OPAQUE_LSA |
| 3152 | if (IS_OPAQUE_LSA (lsa->data->type) |
| 3153 | && (! CHECK_FLAG (options, OSPF_OPTION_O))) |
| 3154 | { |
| 3155 | /* Suppress advertising opaque-informations. */ |
| 3156 | /* Remove LSA from DB summary list. */ |
| 3157 | ospf_lsdb_delete (lsdb, lsa); |
| 3158 | continue; |
| 3159 | } |
| 3160 | #endif /* HAVE_OPAQUE_LSA */ |
| 3161 | |
| 3162 | if (!CHECK_FLAG (lsa->flags, OSPF_LSA_DISCARD)) |
| 3163 | { |
| 3164 | struct lsa_header *lsah; |
| 3165 | u_int16_t ls_age; |
| 3166 | |
| 3167 | /* DD packet overflows interface MTU. */ |
gdt | 86f1fd9 | 2005-01-10 14:20:43 +0000 | [diff] [blame] | 3168 | if (length + OSPF_LSA_HEADER_SIZE > ospf_packet_max (oi)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3169 | break; |
| 3170 | |
| 3171 | /* Keep pointer to LS age. */ |
| 3172 | lsah = (struct lsa_header *) (STREAM_DATA (s) + |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 3173 | stream_get_endp (s)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3174 | |
| 3175 | /* Proceed stream pointer. */ |
| 3176 | stream_put (s, lsa->data, OSPF_LSA_HEADER_SIZE); |
| 3177 | length += OSPF_LSA_HEADER_SIZE; |
| 3178 | |
| 3179 | /* Set LS age. */ |
| 3180 | ls_age = LS_AGE (lsa); |
| 3181 | lsah->ls_age = htons (ls_age); |
| 3182 | |
| 3183 | } |
| 3184 | |
| 3185 | /* Remove LSA from DB summary list. */ |
| 3186 | ospf_lsdb_delete (lsdb, lsa); |
| 3187 | } |
| 3188 | } |
| 3189 | |
Paul Jakma | 8dd24ee | 2006-08-27 06:29:30 +0000 | [diff] [blame] | 3190 | /* Update 'More' bit */ |
| 3191 | if (ospf_db_summary_isempty (nbr)) |
| 3192 | { |
Paul Jakma | b5aeb44 | 2006-08-30 18:47:37 +0000 | [diff] [blame] | 3193 | empty: |
| 3194 | if (nbr->state >= NSM_Exchange) |
| 3195 | { |
| 3196 | UNSET_FLAG (nbr->dd_flags, OSPF_DD_FLAG_M); |
| 3197 | /* Rewrite DD flags */ |
| 3198 | stream_putc_at (s, pp, nbr->dd_flags); |
| 3199 | } |
| 3200 | else |
| 3201 | { |
| 3202 | assert (IS_SET_DD_M(nbr->dd_flags)); |
| 3203 | } |
Paul Jakma | 8dd24ee | 2006-08-27 06:29:30 +0000 | [diff] [blame] | 3204 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3205 | return length; |
| 3206 | } |
| 3207 | |
paul | 4dadc29 | 2005-05-06 21:37:42 +0000 | [diff] [blame] | 3208 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3209 | ospf_make_ls_req_func (struct stream *s, u_int16_t *length, |
| 3210 | unsigned long delta, struct ospf_neighbor *nbr, |
| 3211 | struct ospf_lsa *lsa) |
| 3212 | { |
| 3213 | struct ospf_interface *oi; |
| 3214 | |
| 3215 | oi = nbr->oi; |
| 3216 | |
| 3217 | /* LS Request packet overflows interface MTU. */ |
gdt | 86f1fd9 | 2005-01-10 14:20:43 +0000 | [diff] [blame] | 3218 | if (*length + delta > ospf_packet_max(oi)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3219 | return 0; |
| 3220 | |
| 3221 | stream_putl (s, lsa->data->type); |
| 3222 | stream_put_ipv4 (s, lsa->data->id.s_addr); |
| 3223 | stream_put_ipv4 (s, lsa->data->adv_router.s_addr); |
| 3224 | |
Paul Jakma | 1fe6ed3 | 2006-07-26 09:37:26 +0000 | [diff] [blame] | 3225 | ospf_lsa_unlock (&nbr->ls_req_last); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3226 | nbr->ls_req_last = ospf_lsa_lock (lsa); |
| 3227 | |
| 3228 | *length += 12; |
| 3229 | return 1; |
| 3230 | } |
| 3231 | |
paul | 4dadc29 | 2005-05-06 21:37:42 +0000 | [diff] [blame] | 3232 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3233 | ospf_make_ls_req (struct ospf_neighbor *nbr, struct stream *s) |
| 3234 | { |
| 3235 | struct ospf_lsa *lsa; |
| 3236 | u_int16_t length = OSPF_LS_REQ_MIN_SIZE; |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 3237 | unsigned long delta = stream_get_endp(s)+12; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3238 | struct route_table *table; |
| 3239 | struct route_node *rn; |
| 3240 | int i; |
| 3241 | struct ospf_lsdb *lsdb; |
| 3242 | |
| 3243 | lsdb = &nbr->ls_req; |
| 3244 | |
| 3245 | for (i = OSPF_MIN_LSA; i < OSPF_MAX_LSA; i++) |
| 3246 | { |
| 3247 | table = lsdb->type[i].db; |
| 3248 | for (rn = route_top (table); rn; rn = route_next (rn)) |
| 3249 | if ((lsa = (rn->info)) != NULL) |
| 3250 | if (ospf_make_ls_req_func (s, &length, delta, nbr, lsa) == 0) |
| 3251 | { |
| 3252 | route_unlock_node (rn); |
| 3253 | break; |
| 3254 | } |
| 3255 | } |
| 3256 | return length; |
| 3257 | } |
| 3258 | |
paul | 4dadc29 | 2005-05-06 21:37:42 +0000 | [diff] [blame] | 3259 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3260 | ls_age_increment (struct ospf_lsa *lsa, int delay) |
| 3261 | { |
| 3262 | int age; |
| 3263 | |
| 3264 | age = IS_LSA_MAXAGE (lsa) ? OSPF_LSA_MAXAGE : LS_AGE (lsa) + delay; |
| 3265 | |
| 3266 | return (age > OSPF_LSA_MAXAGE ? OSPF_LSA_MAXAGE : age); |
| 3267 | } |
| 3268 | |
paul | 4dadc29 | 2005-05-06 21:37:42 +0000 | [diff] [blame] | 3269 | static int |
hasso | 52dc7ee | 2004-09-23 19:18:23 +0000 | [diff] [blame] | 3270 | ospf_make_ls_upd (struct ospf_interface *oi, struct list *update, struct stream *s) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3271 | { |
| 3272 | struct ospf_lsa *lsa; |
hasso | 52dc7ee | 2004-09-23 19:18:23 +0000 | [diff] [blame] | 3273 | struct listnode *node; |
Dmitry Tejblum | c9035cc | 2009-06-24 20:14:30 +0400 | [diff] [blame] | 3274 | u_int16_t length = 0; |
gdt | 86f1fd9 | 2005-01-10 14:20:43 +0000 | [diff] [blame] | 3275 | unsigned int size_noauth; |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 3276 | unsigned long delta = stream_get_endp (s); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3277 | unsigned long pp; |
| 3278 | int count = 0; |
| 3279 | |
| 3280 | if (IS_DEBUG_OSPF_EVENT) |
ajs | 2a42e28 | 2004-12-08 18:43:03 +0000 | [diff] [blame] | 3281 | zlog_debug ("ospf_make_ls_upd: Start"); |
paul | 59ea14c | 2004-07-14 20:50:36 +0000 | [diff] [blame] | 3282 | |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 3283 | pp = stream_get_endp (s); |
| 3284 | stream_forward_endp (s, OSPF_LS_UPD_MIN_SIZE); |
Dmitry Tejblum | c9035cc | 2009-06-24 20:14:30 +0400 | [diff] [blame] | 3285 | length += OSPF_LS_UPD_MIN_SIZE; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3286 | |
gdt | 86f1fd9 | 2005-01-10 14:20:43 +0000 | [diff] [blame] | 3287 | /* Calculate amount of packet usable for data. */ |
| 3288 | size_noauth = stream_get_size(s) - ospf_packet_authspace(oi); |
| 3289 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3290 | while ((node = listhead (update)) != NULL) |
| 3291 | { |
| 3292 | struct lsa_header *lsah; |
| 3293 | u_int16_t ls_age; |
| 3294 | |
| 3295 | if (IS_DEBUG_OSPF_EVENT) |
Dinesh G Dutt | 1c06334 | 2014-09-30 13:04:45 -0700 | [diff] [blame] | 3296 | zlog_debug ("ospf_make_ls_upd: List Iteration %d", count); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3297 | |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 3298 | lsa = listgetdata (node); |
| 3299 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3300 | assert (lsa->data); |
| 3301 | |
paul | 68b7339 | 2004-09-12 14:21:37 +0000 | [diff] [blame] | 3302 | /* Will it fit? */ |
gdt | 86f1fd9 | 2005-01-10 14:20:43 +0000 | [diff] [blame] | 3303 | if (length + delta + ntohs (lsa->data->length) > size_noauth) |
paul | 59ea14c | 2004-07-14 20:50:36 +0000 | [diff] [blame] | 3304 | break; |
| 3305 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3306 | /* Keep pointer to LS age. */ |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 3307 | lsah = (struct lsa_header *) (STREAM_DATA (s) + stream_get_endp (s)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3308 | |
| 3309 | /* Put LSA to Link State Request. */ |
| 3310 | stream_put (s, lsa->data, ntohs (lsa->data->length)); |
| 3311 | |
| 3312 | /* Set LS age. */ |
| 3313 | /* each hop must increment an lsa_age by transmit_delay |
| 3314 | of OSPF interface */ |
| 3315 | ls_age = ls_age_increment (lsa, OSPF_IF_PARAM (oi, transmit_delay)); |
| 3316 | lsah->ls_age = htons (ls_age); |
| 3317 | |
| 3318 | length += ntohs (lsa->data->length); |
| 3319 | count++; |
| 3320 | |
| 3321 | list_delete_node (update, node); |
Paul Jakma | 1fe6ed3 | 2006-07-26 09:37:26 +0000 | [diff] [blame] | 3322 | ospf_lsa_unlock (&lsa); /* oi->ls_upd_queue */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3323 | } |
| 3324 | |
| 3325 | /* Now set #LSAs. */ |
paul | 3a9eb09 | 2005-02-08 11:29:41 +0000 | [diff] [blame] | 3326 | stream_putl_at (s, pp, count); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3327 | |
| 3328 | if (IS_DEBUG_OSPF_EVENT) |
ajs | 2a42e28 | 2004-12-08 18:43:03 +0000 | [diff] [blame] | 3329 | zlog_debug ("ospf_make_ls_upd: Stop"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3330 | return length; |
| 3331 | } |
| 3332 | |
paul | 4dadc29 | 2005-05-06 21:37:42 +0000 | [diff] [blame] | 3333 | static int |
hasso | 52dc7ee | 2004-09-23 19:18:23 +0000 | [diff] [blame] | 3334 | ospf_make_ls_ack (struct ospf_interface *oi, struct list *ack, struct stream *s) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3335 | { |
Paul Jakma | 1fe6ed3 | 2006-07-26 09:37:26 +0000 | [diff] [blame] | 3336 | struct listnode *node, *nnode; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3337 | u_int16_t length = OSPF_LS_ACK_MIN_SIZE; |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 3338 | unsigned long delta = stream_get_endp(s) + 24; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3339 | struct ospf_lsa *lsa; |
| 3340 | |
Paul Jakma | 1fe6ed3 | 2006-07-26 09:37:26 +0000 | [diff] [blame] | 3341 | for (ALL_LIST_ELEMENTS (ack, node, nnode, lsa)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3342 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3343 | assert (lsa); |
| 3344 | |
gdt | 86f1fd9 | 2005-01-10 14:20:43 +0000 | [diff] [blame] | 3345 | if (length + delta > ospf_packet_max (oi)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3346 | break; |
| 3347 | |
| 3348 | stream_put (s, lsa->data, OSPF_LSA_HEADER_SIZE); |
| 3349 | length += OSPF_LSA_HEADER_SIZE; |
| 3350 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3351 | listnode_delete (ack, lsa); |
Paul Jakma | 1fe6ed3 | 2006-07-26 09:37:26 +0000 | [diff] [blame] | 3352 | ospf_lsa_unlock (&lsa); /* oi->ls_ack_direct.ls_ack */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3353 | } |
| 3354 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3355 | return length; |
| 3356 | } |
| 3357 | |
Paul Jakma | aa276fd | 2010-01-08 17:11:15 +0000 | [diff] [blame] | 3358 | static void |
| 3359 | ospf_hello_send_sub (struct ospf_interface *oi, in_addr_t addr) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3360 | { |
| 3361 | struct ospf_packet *op; |
| 3362 | u_int16_t length = OSPF_HEADER_SIZE; |
| 3363 | |
| 3364 | op = ospf_packet_new (oi->ifp->mtu); |
| 3365 | |
| 3366 | /* Prepare OSPF common header. */ |
| 3367 | ospf_make_header (OSPF_MSG_HELLO, oi, op->s); |
| 3368 | |
| 3369 | /* Prepare OSPF Hello body. */ |
| 3370 | length += ospf_make_hello (oi, op->s); |
| 3371 | |
| 3372 | /* Fill OSPF header. */ |
| 3373 | ospf_fill_header (oi, op->s, length); |
| 3374 | |
| 3375 | /* Set packet length. */ |
| 3376 | op->length = length; |
| 3377 | |
Paul Jakma | aa276fd | 2010-01-08 17:11:15 +0000 | [diff] [blame] | 3378 | op->dst.s_addr = addr; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3379 | |
Paul Jakma | aa276fd | 2010-01-08 17:11:15 +0000 | [diff] [blame] | 3380 | /* Add packet to the top of the interface output queue, so that they |
| 3381 | * can't get delayed by things like long queues of LS Update packets |
| 3382 | */ |
| 3383 | ospf_packet_add_top (oi, op); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3384 | |
| 3385 | /* Hook thread to write packet. */ |
paul | 020709f | 2003-04-04 02:44:16 +0000 | [diff] [blame] | 3386 | OSPF_ISM_WRITE_ON (oi->ospf); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3387 | } |
| 3388 | |
paul | 4dadc29 | 2005-05-06 21:37:42 +0000 | [diff] [blame] | 3389 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3390 | ospf_poll_send (struct ospf_nbr_nbma *nbr_nbma) |
| 3391 | { |
| 3392 | struct ospf_interface *oi; |
| 3393 | |
| 3394 | oi = nbr_nbma->oi; |
| 3395 | assert(oi); |
| 3396 | |
| 3397 | /* If this is passive interface, do not send OSPF Hello. */ |
Paul Jakma | 7ffa8fa | 2006-10-22 20:07:53 +0000 | [diff] [blame] | 3398 | if (OSPF_IF_PASSIVE_STATUS (oi) == OSPF_IF_PASSIVE) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3399 | return; |
| 3400 | |
| 3401 | if (oi->type != OSPF_IFTYPE_NBMA) |
| 3402 | return; |
| 3403 | |
| 3404 | if (nbr_nbma->nbr != NULL && nbr_nbma->nbr->state != NSM_Down) |
| 3405 | return; |
| 3406 | |
| 3407 | if (PRIORITY(oi) == 0) |
| 3408 | return; |
| 3409 | |
| 3410 | if (nbr_nbma->priority == 0 |
| 3411 | && oi->state != ISM_DR && oi->state != ISM_Backup) |
| 3412 | return; |
| 3413 | |
Paul Jakma | aa276fd | 2010-01-08 17:11:15 +0000 | [diff] [blame] | 3414 | ospf_hello_send_sub (oi, nbr_nbma->addr.s_addr); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3415 | } |
| 3416 | |
| 3417 | int |
| 3418 | ospf_poll_timer (struct thread *thread) |
| 3419 | { |
| 3420 | struct ospf_nbr_nbma *nbr_nbma; |
| 3421 | |
| 3422 | nbr_nbma = THREAD_ARG (thread); |
| 3423 | nbr_nbma->t_poll = NULL; |
| 3424 | |
| 3425 | if (IS_DEBUG_OSPF (nsm, NSM_TIMERS)) |
ajs | 2a42e28 | 2004-12-08 18:43:03 +0000 | [diff] [blame] | 3426 | zlog (NULL, LOG_DEBUG, "NSM[%s:%s]: Timer (Poll timer expire)", |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3427 | IF_NAME (nbr_nbma->oi), inet_ntoa (nbr_nbma->addr)); |
| 3428 | |
| 3429 | ospf_poll_send (nbr_nbma); |
| 3430 | |
| 3431 | if (nbr_nbma->v_poll > 0) |
| 3432 | OSPF_POLL_TIMER_ON (nbr_nbma->t_poll, ospf_poll_timer, |
| 3433 | nbr_nbma->v_poll); |
| 3434 | |
| 3435 | return 0; |
| 3436 | } |
| 3437 | |
| 3438 | |
| 3439 | int |
| 3440 | ospf_hello_reply_timer (struct thread *thread) |
| 3441 | { |
| 3442 | struct ospf_neighbor *nbr; |
| 3443 | |
| 3444 | nbr = THREAD_ARG (thread); |
| 3445 | nbr->t_hello_reply = NULL; |
| 3446 | |
| 3447 | assert (nbr->oi); |
| 3448 | |
| 3449 | if (IS_DEBUG_OSPF (nsm, NSM_TIMERS)) |
ajs | 2a42e28 | 2004-12-08 18:43:03 +0000 | [diff] [blame] | 3450 | zlog (NULL, LOG_DEBUG, "NSM[%s:%s]: Timer (hello-reply timer expire)", |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3451 | IF_NAME (nbr->oi), inet_ntoa (nbr->router_id)); |
| 3452 | |
Paul Jakma | aa276fd | 2010-01-08 17:11:15 +0000 | [diff] [blame] | 3453 | ospf_hello_send_sub (nbr->oi, nbr->address.u.prefix4.s_addr); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3454 | |
| 3455 | return 0; |
| 3456 | } |
| 3457 | |
| 3458 | /* Send OSPF Hello. */ |
| 3459 | void |
| 3460 | ospf_hello_send (struct ospf_interface *oi) |
| 3461 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3462 | /* If this is passive interface, do not send OSPF Hello. */ |
Paul Jakma | 7ffa8fa | 2006-10-22 20:07:53 +0000 | [diff] [blame] | 3463 | if (OSPF_IF_PASSIVE_STATUS (oi) == OSPF_IF_PASSIVE) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3464 | return; |
| 3465 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3466 | if (oi->type == OSPF_IFTYPE_NBMA) |
| 3467 | { |
| 3468 | struct ospf_neighbor *nbr; |
| 3469 | struct route_node *rn; |
| 3470 | |
| 3471 | for (rn = route_top (oi->nbrs); rn; rn = route_next (rn)) |
| 3472 | if ((nbr = rn->info)) |
| 3473 | if (nbr != oi->nbr_self) |
| 3474 | if (nbr->state != NSM_Down) |
| 3475 | { |
| 3476 | /* RFC 2328 Section 9.5.1 |
| 3477 | If the router is not eligible to become Designated Router, |
| 3478 | it must periodically send Hello Packets to both the |
| 3479 | Designated Router and the Backup Designated Router (if they |
| 3480 | exist). */ |
| 3481 | if (PRIORITY(oi) == 0 && |
| 3482 | IPV4_ADDR_CMP(&DR(oi), &nbr->address.u.prefix4) && |
| 3483 | IPV4_ADDR_CMP(&BDR(oi), &nbr->address.u.prefix4)) |
| 3484 | continue; |
| 3485 | |
| 3486 | /* If the router is eligible to become Designated Router, it |
| 3487 | must periodically send Hello Packets to all neighbors that |
| 3488 | are also eligible. In addition, if the router is itself the |
| 3489 | Designated Router or Backup Designated Router, it must also |
| 3490 | send periodic Hello Packets to all other neighbors. */ |
| 3491 | |
| 3492 | if (nbr->priority == 0 && oi->state == ISM_DROther) |
| 3493 | continue; |
| 3494 | /* if oi->state == Waiting, send hello to all neighbors */ |
Paul Jakma | aa276fd | 2010-01-08 17:11:15 +0000 | [diff] [blame] | 3495 | ospf_hello_send_sub (oi, nbr->address.u.prefix4.s_addr); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3496 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3497 | } |
| 3498 | else |
| 3499 | { |
| 3500 | /* Decide destination address. */ |
| 3501 | if (oi->type == OSPF_IFTYPE_VIRTUALLINK) |
Paul Jakma | aa276fd | 2010-01-08 17:11:15 +0000 | [diff] [blame] | 3502 | ospf_hello_send_sub (oi, oi->vl_data->peer_addr.s_addr); |
| 3503 | else |
| 3504 | ospf_hello_send_sub (oi, htonl (OSPF_ALLSPFROUTERS)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3505 | } |
| 3506 | } |
| 3507 | |
| 3508 | /* Send OSPF Database Description. */ |
| 3509 | void |
| 3510 | ospf_db_desc_send (struct ospf_neighbor *nbr) |
| 3511 | { |
| 3512 | struct ospf_interface *oi; |
| 3513 | struct ospf_packet *op; |
| 3514 | u_int16_t length = OSPF_HEADER_SIZE; |
| 3515 | |
| 3516 | oi = nbr->oi; |
| 3517 | op = ospf_packet_new (oi->ifp->mtu); |
| 3518 | |
| 3519 | /* Prepare OSPF common header. */ |
| 3520 | ospf_make_header (OSPF_MSG_DB_DESC, oi, op->s); |
| 3521 | |
| 3522 | /* Prepare OSPF Database Description body. */ |
| 3523 | length += ospf_make_db_desc (oi, nbr, op->s); |
| 3524 | |
| 3525 | /* Fill OSPF header. */ |
| 3526 | ospf_fill_header (oi, op->s, length); |
| 3527 | |
| 3528 | /* Set packet length. */ |
| 3529 | op->length = length; |
| 3530 | |
| 3531 | /* Decide destination address. */ |
Joakim Tjernlund | 53d0dec | 2008-05-30 16:04:39 +0200 | [diff] [blame] | 3532 | if (oi->type == OSPF_IFTYPE_POINTOPOINT) |
| 3533 | op->dst.s_addr = htonl (OSPF_ALLSPFROUTERS); |
| 3534 | else |
| 3535 | op->dst = nbr->address.u.prefix4; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3536 | |
| 3537 | /* Add packet to the interface output queue. */ |
| 3538 | ospf_packet_add (oi, op); |
| 3539 | |
| 3540 | /* Hook thread to write packet. */ |
paul | 020709f | 2003-04-04 02:44:16 +0000 | [diff] [blame] | 3541 | OSPF_ISM_WRITE_ON (oi->ospf); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3542 | |
| 3543 | /* Remove old DD packet, then copy new one and keep in neighbor structure. */ |
| 3544 | if (nbr->last_send) |
| 3545 | ospf_packet_free (nbr->last_send); |
| 3546 | nbr->last_send = ospf_packet_dup (op); |
Paul Jakma | 2518efd | 2006-08-27 06:49:29 +0000 | [diff] [blame] | 3547 | quagga_gettime (QUAGGA_CLK_MONOTONIC, &nbr->last_send_ts); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3548 | } |
| 3549 | |
| 3550 | /* Re-send Database Description. */ |
| 3551 | void |
| 3552 | ospf_db_desc_resend (struct ospf_neighbor *nbr) |
| 3553 | { |
| 3554 | struct ospf_interface *oi; |
| 3555 | |
| 3556 | oi = nbr->oi; |
| 3557 | |
| 3558 | /* Add packet to the interface output queue. */ |
| 3559 | ospf_packet_add (oi, ospf_packet_dup (nbr->last_send)); |
| 3560 | |
| 3561 | /* Hook thread to write packet. */ |
paul | 020709f | 2003-04-04 02:44:16 +0000 | [diff] [blame] | 3562 | OSPF_ISM_WRITE_ON (oi->ospf); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3563 | } |
| 3564 | |
| 3565 | /* Send Link State Request. */ |
| 3566 | void |
| 3567 | ospf_ls_req_send (struct ospf_neighbor *nbr) |
| 3568 | { |
| 3569 | struct ospf_interface *oi; |
| 3570 | struct ospf_packet *op; |
| 3571 | u_int16_t length = OSPF_HEADER_SIZE; |
| 3572 | |
| 3573 | oi = nbr->oi; |
| 3574 | op = ospf_packet_new (oi->ifp->mtu); |
| 3575 | |
| 3576 | /* Prepare OSPF common header. */ |
| 3577 | ospf_make_header (OSPF_MSG_LS_REQ, oi, op->s); |
| 3578 | |
| 3579 | /* Prepare OSPF Link State Request body. */ |
| 3580 | length += ospf_make_ls_req (nbr, op->s); |
| 3581 | if (length == OSPF_HEADER_SIZE) |
| 3582 | { |
| 3583 | ospf_packet_free (op); |
| 3584 | return; |
| 3585 | } |
| 3586 | |
| 3587 | /* Fill OSPF header. */ |
| 3588 | ospf_fill_header (oi, op->s, length); |
| 3589 | |
| 3590 | /* Set packet length. */ |
| 3591 | op->length = length; |
| 3592 | |
| 3593 | /* Decide destination address. */ |
Joakim Tjernlund | 53d0dec | 2008-05-30 16:04:39 +0200 | [diff] [blame] | 3594 | if (oi->type == OSPF_IFTYPE_POINTOPOINT) |
| 3595 | op->dst.s_addr = htonl (OSPF_ALLSPFROUTERS); |
| 3596 | else |
| 3597 | op->dst = nbr->address.u.prefix4; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3598 | |
| 3599 | /* Add packet to the interface output queue. */ |
| 3600 | ospf_packet_add (oi, op); |
| 3601 | |
| 3602 | /* Hook thread to write packet. */ |
paul | 020709f | 2003-04-04 02:44:16 +0000 | [diff] [blame] | 3603 | OSPF_ISM_WRITE_ON (oi->ospf); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3604 | |
| 3605 | /* Add Link State Request Retransmission Timer. */ |
| 3606 | OSPF_NSM_TIMER_ON (nbr->t_ls_req, ospf_ls_req_timer, nbr->v_ls_req); |
| 3607 | } |
| 3608 | |
| 3609 | /* Send Link State Update with an LSA. */ |
| 3610 | void |
| 3611 | ospf_ls_upd_send_lsa (struct ospf_neighbor *nbr, struct ospf_lsa *lsa, |
| 3612 | int flag) |
| 3613 | { |
hasso | 52dc7ee | 2004-09-23 19:18:23 +0000 | [diff] [blame] | 3614 | struct list *update; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3615 | |
| 3616 | update = list_new (); |
| 3617 | |
| 3618 | listnode_add (update, lsa); |
| 3619 | ospf_ls_upd_send (nbr, update, flag); |
| 3620 | |
| 3621 | list_delete (update); |
| 3622 | } |
| 3623 | |
paul | 68b7339 | 2004-09-12 14:21:37 +0000 | [diff] [blame] | 3624 | /* Determine size for packet. Must be at least big enough to accomodate next |
| 3625 | * LSA on list, which may be bigger than MTU size. |
| 3626 | * |
| 3627 | * Return pointer to new ospf_packet |
| 3628 | * NULL if we can not allocate, eg because LSA is bigger than imposed limit |
| 3629 | * on packet sizes (in which case offending LSA is deleted from update list) |
| 3630 | */ |
| 3631 | static struct ospf_packet * |
| 3632 | ospf_ls_upd_packet_new (struct list *update, struct ospf_interface *oi) |
| 3633 | { |
| 3634 | struct ospf_lsa *lsa; |
| 3635 | struct listnode *ln; |
| 3636 | size_t size; |
| 3637 | static char warned = 0; |
| 3638 | |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 3639 | lsa = listgetdata((ln = listhead (update))); |
paul | 68b7339 | 2004-09-12 14:21:37 +0000 | [diff] [blame] | 3640 | assert (lsa->data); |
| 3641 | |
| 3642 | if ((OSPF_LS_UPD_MIN_SIZE + ntohs (lsa->data->length)) |
| 3643 | > ospf_packet_max (oi)) |
| 3644 | { |
| 3645 | if (!warned) |
| 3646 | { |
| 3647 | zlog_warn ("ospf_ls_upd_packet_new: oversized LSA encountered!" |
| 3648 | "will need to fragment. Not optimal. Try divide up" |
| 3649 | " your network with areas. Use 'debug ospf packet send'" |
| 3650 | " to see details, or look at 'show ip ospf database ..'"); |
| 3651 | warned = 1; |
| 3652 | } |
| 3653 | |
| 3654 | if (IS_DEBUG_OSPF_PACKET (0, SEND)) |
ajs | 2a42e28 | 2004-12-08 18:43:03 +0000 | [diff] [blame] | 3655 | zlog_debug ("ospf_ls_upd_packet_new: oversized LSA id:%s," |
paul | 68b7339 | 2004-09-12 14:21:37 +0000 | [diff] [blame] | 3656 | " %d bytes originated by %s, will be fragmented!", |
| 3657 | inet_ntoa (lsa->data->id), |
| 3658 | ntohs (lsa->data->length), |
| 3659 | inet_ntoa (lsa->data->adv_router)); |
| 3660 | |
| 3661 | /* |
| 3662 | * Allocate just enough to fit this LSA only, to avoid including other |
| 3663 | * LSAs in fragmented LSA Updates. |
| 3664 | */ |
| 3665 | size = ntohs (lsa->data->length) + (oi->ifp->mtu - ospf_packet_max (oi)) |
| 3666 | + OSPF_LS_UPD_MIN_SIZE; |
| 3667 | } |
| 3668 | else |
| 3669 | size = oi->ifp->mtu; |
| 3670 | |
| 3671 | if (size > OSPF_MAX_PACKET_SIZE) |
| 3672 | { |
| 3673 | zlog_warn ("ospf_ls_upd_packet_new: oversized LSA id:%s too big," |
paul | 64511f3 | 2004-10-31 18:01:13 +0000 | [diff] [blame] | 3674 | " %d bytes, packet size %ld, dropping it completely." |
paul | 68b7339 | 2004-09-12 14:21:37 +0000 | [diff] [blame] | 3675 | " OSPF routing is broken!", |
paul | 37ccfa3 | 2004-10-31 11:24:51 +0000 | [diff] [blame] | 3676 | inet_ntoa (lsa->data->id), ntohs (lsa->data->length), |
paul | 62d8e96 | 2004-11-02 20:26:45 +0000 | [diff] [blame] | 3677 | (long int) size); |
paul | 68b7339 | 2004-09-12 14:21:37 +0000 | [diff] [blame] | 3678 | list_delete_node (update, ln); |
| 3679 | return NULL; |
| 3680 | } |
| 3681 | |
Dmitry Tejblum | c9035cc | 2009-06-24 20:14:30 +0400 | [diff] [blame] | 3682 | /* IP header is built up separately by ospf_write(). This means, that we must |
| 3683 | * reduce the "affordable" size just calculated by length of an IP header. |
| 3684 | * This makes sure, that even if we manage to fill the payload with LSA data |
| 3685 | * completely, the final packet (our data plus IP header) still fits into |
| 3686 | * outgoing interface MTU. This correction isn't really meaningful for an |
| 3687 | * oversized LSA, but for consistency the correction is done for both cases. |
| 3688 | * |
| 3689 | * P.S. OSPF_MAX_PACKET_SIZE above already includes IP header size |
| 3690 | */ |
| 3691 | return ospf_packet_new (size - sizeof (struct ip)); |
paul | 68b7339 | 2004-09-12 14:21:37 +0000 | [diff] [blame] | 3692 | } |
| 3693 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3694 | static void |
hasso | 52dc7ee | 2004-09-23 19:18:23 +0000 | [diff] [blame] | 3695 | ospf_ls_upd_queue_send (struct ospf_interface *oi, struct list *update, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3696 | struct in_addr addr) |
| 3697 | { |
| 3698 | struct ospf_packet *op; |
| 3699 | u_int16_t length = OSPF_HEADER_SIZE; |
| 3700 | |
| 3701 | if (IS_DEBUG_OSPF_EVENT) |
Dinesh G Dutt | 1c06334 | 2014-09-30 13:04:45 -0700 | [diff] [blame] | 3702 | zlog_debug ("listcount = %d, [%s]dst %s", listcount (update), IF_NAME(oi), |
| 3703 | inet_ntoa(addr)); |
paul | 68b7339 | 2004-09-12 14:21:37 +0000 | [diff] [blame] | 3704 | |
| 3705 | op = ospf_ls_upd_packet_new (update, oi); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3706 | |
| 3707 | /* Prepare OSPF common header. */ |
| 3708 | ospf_make_header (OSPF_MSG_LS_UPD, oi, op->s); |
| 3709 | |
paul | 59ea14c | 2004-07-14 20:50:36 +0000 | [diff] [blame] | 3710 | /* Prepare OSPF Link State Update body. |
| 3711 | * Includes Type-7 translation. |
| 3712 | */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3713 | length += ospf_make_ls_upd (oi, update, op->s); |
| 3714 | |
| 3715 | /* Fill OSPF header. */ |
| 3716 | ospf_fill_header (oi, op->s, length); |
| 3717 | |
| 3718 | /* Set packet length. */ |
| 3719 | op->length = length; |
| 3720 | |
| 3721 | /* Decide destination address. */ |
Joakim Tjernlund | 53d0dec | 2008-05-30 16:04:39 +0200 | [diff] [blame] | 3722 | if (oi->type == OSPF_IFTYPE_POINTOPOINT) |
| 3723 | op->dst.s_addr = htonl (OSPF_ALLSPFROUTERS); |
| 3724 | else |
| 3725 | op->dst.s_addr = addr.s_addr; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3726 | |
| 3727 | /* Add packet to the interface output queue. */ |
| 3728 | ospf_packet_add (oi, op); |
| 3729 | |
| 3730 | /* Hook thread to write packet. */ |
paul | 020709f | 2003-04-04 02:44:16 +0000 | [diff] [blame] | 3731 | OSPF_ISM_WRITE_ON (oi->ospf); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3732 | } |
| 3733 | |
| 3734 | static int |
| 3735 | ospf_ls_upd_send_queue_event (struct thread *thread) |
| 3736 | { |
| 3737 | struct ospf_interface *oi = THREAD_ARG(thread); |
| 3738 | struct route_node *rn; |
paul | 736d344 | 2003-07-24 23:22:57 +0000 | [diff] [blame] | 3739 | struct route_node *rnext; |
paul | 59ea14c | 2004-07-14 20:50:36 +0000 | [diff] [blame] | 3740 | struct list *update; |
paul | 68b7339 | 2004-09-12 14:21:37 +0000 | [diff] [blame] | 3741 | char again = 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3742 | |
| 3743 | oi->t_ls_upd_event = NULL; |
| 3744 | |
| 3745 | if (IS_DEBUG_OSPF_EVENT) |
ajs | 2a42e28 | 2004-12-08 18:43:03 +0000 | [diff] [blame] | 3746 | zlog_debug ("ospf_ls_upd_send_queue start"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3747 | |
paul | 736d344 | 2003-07-24 23:22:57 +0000 | [diff] [blame] | 3748 | for (rn = route_top (oi->ls_upd_queue); rn; rn = rnext) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3749 | { |
paul | 736d344 | 2003-07-24 23:22:57 +0000 | [diff] [blame] | 3750 | rnext = route_next (rn); |
| 3751 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3752 | if (rn->info == NULL) |
paul | 736d344 | 2003-07-24 23:22:57 +0000 | [diff] [blame] | 3753 | continue; |
paul | 68b7339 | 2004-09-12 14:21:37 +0000 | [diff] [blame] | 3754 | |
| 3755 | update = (struct list *)rn->info; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3756 | |
paul | 48fe13b | 2004-07-27 17:40:44 +0000 | [diff] [blame] | 3757 | ospf_ls_upd_queue_send (oi, update, rn->p.u.prefix4); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3758 | |
paul | 68b7339 | 2004-09-12 14:21:37 +0000 | [diff] [blame] | 3759 | /* list might not be empty. */ |
paul | 59ea14c | 2004-07-14 20:50:36 +0000 | [diff] [blame] | 3760 | if (listcount(update) == 0) |
| 3761 | { |
| 3762 | list_delete (rn->info); |
| 3763 | rn->info = NULL; |
| 3764 | route_unlock_node (rn); |
| 3765 | } |
| 3766 | else |
paul | 68b7339 | 2004-09-12 14:21:37 +0000 | [diff] [blame] | 3767 | again = 1; |
paul | 59ea14c | 2004-07-14 20:50:36 +0000 | [diff] [blame] | 3768 | } |
| 3769 | |
| 3770 | if (again != 0) |
| 3771 | { |
| 3772 | if (IS_DEBUG_OSPF_EVENT) |
ajs | 2a42e28 | 2004-12-08 18:43:03 +0000 | [diff] [blame] | 3773 | zlog_debug ("ospf_ls_upd_send_queue: update lists not cleared," |
paul | 59ea14c | 2004-07-14 20:50:36 +0000 | [diff] [blame] | 3774 | " %d nodes to try again, raising new event", again); |
| 3775 | oi->t_ls_upd_event = |
| 3776 | thread_add_event (master, ospf_ls_upd_send_queue_event, oi, 0); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3777 | } |
| 3778 | |
| 3779 | if (IS_DEBUG_OSPF_EVENT) |
ajs | 2a42e28 | 2004-12-08 18:43:03 +0000 | [diff] [blame] | 3780 | zlog_debug ("ospf_ls_upd_send_queue stop"); |
paul | 59ea14c | 2004-07-14 20:50:36 +0000 | [diff] [blame] | 3781 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3782 | return 0; |
| 3783 | } |
| 3784 | |
| 3785 | void |
hasso | 52dc7ee | 2004-09-23 19:18:23 +0000 | [diff] [blame] | 3786 | ospf_ls_upd_send (struct ospf_neighbor *nbr, struct list *update, int flag) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3787 | { |
| 3788 | struct ospf_interface *oi; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 3789 | struct ospf_lsa *lsa; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3790 | struct prefix_ipv4 p; |
| 3791 | struct route_node *rn; |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 3792 | struct listnode *node; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3793 | |
| 3794 | oi = nbr->oi; |
| 3795 | |
| 3796 | p.family = AF_INET; |
| 3797 | p.prefixlen = IPV4_MAX_BITLEN; |
| 3798 | |
| 3799 | /* Decide destination address. */ |
| 3800 | if (oi->type == OSPF_IFTYPE_VIRTUALLINK) |
| 3801 | p.prefix = oi->vl_data->peer_addr; |
Joakim Tjernlund | 53d0dec | 2008-05-30 16:04:39 +0200 | [diff] [blame] | 3802 | else if (oi->type == OSPF_IFTYPE_POINTOPOINT) |
| 3803 | p.prefix.s_addr = htonl (OSPF_ALLSPFROUTERS); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3804 | else if (flag == OSPF_SEND_PACKET_DIRECT) |
| 3805 | p.prefix = nbr->address.u.prefix4; |
| 3806 | else if (oi->state == ISM_DR || oi->state == ISM_Backup) |
| 3807 | p.prefix.s_addr = htonl (OSPF_ALLSPFROUTERS); |
paul | 7afa08d | 2002-12-13 20:59:45 +0000 | [diff] [blame] | 3808 | else if (oi->type == OSPF_IFTYPE_POINTOMULTIPOINT) |
| 3809 | p.prefix.s_addr = htonl (OSPF_ALLSPFROUTERS); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3810 | else |
| 3811 | p.prefix.s_addr = htonl (OSPF_ALLDROUTERS); |
| 3812 | |
| 3813 | if (oi->type == OSPF_IFTYPE_NBMA) |
| 3814 | { |
| 3815 | if (flag == OSPF_SEND_PACKET_INDIRECT) |
| 3816 | zlog_warn ("* LS-Update is directly sent on NBMA network."); |
| 3817 | if (IPV4_ADDR_SAME(&oi->address->u.prefix4, &p.prefix.s_addr)) |
| 3818 | zlog_warn ("* LS-Update is sent to myself."); |
| 3819 | } |
| 3820 | |
| 3821 | rn = route_node_get (oi->ls_upd_queue, (struct prefix *) &p); |
| 3822 | |
| 3823 | if (rn->info == NULL) |
| 3824 | rn->info = list_new (); |
Joakim Tjernlund | 4eaecdc | 2010-03-08 13:58:13 +0100 | [diff] [blame] | 3825 | else |
| 3826 | route_unlock_node (rn); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3827 | |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 3828 | for (ALL_LIST_ELEMENTS_RO (update, node, lsa)) |
Paul Jakma | 1fe6ed3 | 2006-07-26 09:37:26 +0000 | [diff] [blame] | 3829 | listnode_add (rn->info, ospf_lsa_lock (lsa)); /* oi->ls_upd_queue */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3830 | |
| 3831 | if (oi->t_ls_upd_event == NULL) |
| 3832 | oi->t_ls_upd_event = |
| 3833 | thread_add_event (master, ospf_ls_upd_send_queue_event, oi, 0); |
| 3834 | } |
| 3835 | |
| 3836 | static void |
hasso | 52dc7ee | 2004-09-23 19:18:23 +0000 | [diff] [blame] | 3837 | ospf_ls_ack_send_list (struct ospf_interface *oi, struct list *ack, |
| 3838 | struct in_addr dst) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3839 | { |
| 3840 | struct ospf_packet *op; |
| 3841 | u_int16_t length = OSPF_HEADER_SIZE; |
| 3842 | |
| 3843 | op = ospf_packet_new (oi->ifp->mtu); |
| 3844 | |
| 3845 | /* Prepare OSPF common header. */ |
| 3846 | ospf_make_header (OSPF_MSG_LS_ACK, oi, op->s); |
| 3847 | |
| 3848 | /* Prepare OSPF Link State Acknowledgment body. */ |
| 3849 | length += ospf_make_ls_ack (oi, ack, op->s); |
| 3850 | |
| 3851 | /* Fill OSPF header. */ |
| 3852 | ospf_fill_header (oi, op->s, length); |
| 3853 | |
| 3854 | /* Set packet length. */ |
| 3855 | op->length = length; |
| 3856 | |
| 3857 | /* Set destination IP address. */ |
| 3858 | op->dst = dst; |
| 3859 | |
| 3860 | /* Add packet to the interface output queue. */ |
| 3861 | ospf_packet_add (oi, op); |
| 3862 | |
| 3863 | /* Hook thread to write packet. */ |
paul | 020709f | 2003-04-04 02:44:16 +0000 | [diff] [blame] | 3864 | OSPF_ISM_WRITE_ON (oi->ospf); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3865 | } |
| 3866 | |
| 3867 | static int |
| 3868 | ospf_ls_ack_send_event (struct thread *thread) |
| 3869 | { |
| 3870 | struct ospf_interface *oi = THREAD_ARG (thread); |
| 3871 | |
| 3872 | oi->t_ls_ack_direct = NULL; |
| 3873 | |
| 3874 | while (listcount (oi->ls_ack_direct.ls_ack)) |
| 3875 | ospf_ls_ack_send_list (oi, oi->ls_ack_direct.ls_ack, |
| 3876 | oi->ls_ack_direct.dst); |
| 3877 | |
| 3878 | return 0; |
| 3879 | } |
| 3880 | |
| 3881 | void |
| 3882 | ospf_ls_ack_send (struct ospf_neighbor *nbr, struct ospf_lsa *lsa) |
| 3883 | { |
| 3884 | struct ospf_interface *oi = nbr->oi; |
| 3885 | |
| 3886 | if (listcount (oi->ls_ack_direct.ls_ack) == 0) |
| 3887 | oi->ls_ack_direct.dst = nbr->address.u.prefix4; |
| 3888 | |
| 3889 | listnode_add (oi->ls_ack_direct.ls_ack, ospf_lsa_lock (lsa)); |
| 3890 | |
| 3891 | if (oi->t_ls_ack_direct == NULL) |
| 3892 | oi->t_ls_ack_direct = |
| 3893 | thread_add_event (master, ospf_ls_ack_send_event, oi, 0); |
| 3894 | } |
| 3895 | |
| 3896 | /* Send Link State Acknowledgment delayed. */ |
| 3897 | void |
| 3898 | ospf_ls_ack_send_delayed (struct ospf_interface *oi) |
| 3899 | { |
| 3900 | struct in_addr dst; |
| 3901 | |
| 3902 | /* Decide destination address. */ |
| 3903 | /* RFC2328 Section 13.5 On non-broadcast |
| 3904 | networks, delayed Link State Acknowledgment packets must be |
| 3905 | unicast separately over each adjacency (i.e., neighbor whose |
| 3906 | state is >= Exchange). */ |
| 3907 | if (oi->type == OSPF_IFTYPE_NBMA) |
| 3908 | { |
| 3909 | struct ospf_neighbor *nbr; |
| 3910 | struct route_node *rn; |
| 3911 | |
| 3912 | for (rn = route_top (oi->nbrs); rn; rn = route_next (rn)) |
| 3913 | if ((nbr = rn->info) != NULL) |
| 3914 | if (nbr != oi->nbr_self && nbr->state >= NSM_Exchange) |
| 3915 | while (listcount (oi->ls_ack)) |
| 3916 | ospf_ls_ack_send_list (oi, oi->ls_ack, nbr->address.u.prefix4); |
| 3917 | return; |
| 3918 | } |
| 3919 | if (oi->type == OSPF_IFTYPE_VIRTUALLINK) |
| 3920 | dst.s_addr = oi->vl_data->peer_addr.s_addr; |
| 3921 | else if (oi->state == ISM_DR || oi->state == ISM_Backup) |
| 3922 | dst.s_addr = htonl (OSPF_ALLSPFROUTERS); |
| 3923 | else if (oi->type == OSPF_IFTYPE_POINTOPOINT) |
| 3924 | dst.s_addr = htonl (OSPF_ALLSPFROUTERS); |
gdt | 630e480 | 2004-08-31 17:28:41 +0000 | [diff] [blame] | 3925 | else if (oi->type == OSPF_IFTYPE_POINTOMULTIPOINT) |
| 3926 | dst.s_addr = htonl (OSPF_ALLSPFROUTERS); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 3927 | else |
| 3928 | dst.s_addr = htonl (OSPF_ALLDROUTERS); |
| 3929 | |
| 3930 | while (listcount (oi->ls_ack)) |
| 3931 | ospf_ls_ack_send_list (oi, oi->ls_ack, dst); |
| 3932 | } |