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