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