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