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