2 * OSPF Sending and Receiving OSPF Packets.
3 * Copyright (C) 1999, 2000 Toshiaki Takada
5 * This file is part of GNU Zebra.
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
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.
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
31 #include "sockunion.h"
37 #include "ospfd/ospfd.h"
38 #include "ospfd/ospf_network.h"
39 #include "ospfd/ospf_interface.h"
40 #include "ospfd/ospf_ism.h"
41 #include "ospfd/ospf_asbr.h"
42 #include "ospfd/ospf_lsa.h"
43 #include "ospfd/ospf_lsdb.h"
44 #include "ospfd/ospf_neighbor.h"
45 #include "ospfd/ospf_nsm.h"
46 #include "ospfd/ospf_packet.h"
47 #include "ospfd/ospf_spf.h"
48 #include "ospfd/ospf_flood.h"
49 #include "ospfd/ospf_dump.h"
51 static void ospf_ls_ack_send_list (struct ospf_interface
*, list
,
54 /* Packet Type String. */
55 char *ospf_packet_type_str
[] =
59 "Database Description",
62 "Link State Acknowledgment",
65 extern int in_cksum (void *ptr
, int nbytes
);
67 /* OSPF authentication checking function */
69 ospf_auth_type (struct ospf_interface
*oi
)
73 if (OSPF_IF_PARAM (oi
, auth_type
) == OSPF_AUTH_NOTSET
)
74 auth_type
= oi
->area
->auth_type
;
76 auth_type
= OSPF_IF_PARAM (oi
, auth_type
);
78 /* Handle case where MD5 key list is not configured aka Cisco */
79 if (auth_type
== OSPF_AUTH_CRYPTOGRAPHIC
&&
80 list_isempty (OSPF_IF_PARAM (oi
, auth_crypt
)))
81 return OSPF_AUTH_NULL
;
87 /* forward output pointer. */
89 ospf_output_forward (struct stream
*s
, int size
)
95 ospf_packet_new (size_t size
)
97 struct ospf_packet
*new;
99 new = XCALLOC (MTYPE_OSPF_PACKET
, sizeof (struct ospf_packet
));
100 new->s
= stream_new (size
);
106 ospf_packet_free (struct ospf_packet
*op
)
111 XFREE (MTYPE_OSPF_PACKET
, op
);
119 struct ospf_fifo
*new;
121 new = XCALLOC (MTYPE_OSPF_FIFO
, sizeof (struct ospf_fifo
));
125 /* Add new packet to fifo. */
127 ospf_fifo_push (struct ospf_fifo
*fifo
, struct ospf_packet
*op
)
130 fifo
->tail
->next
= op
;
139 /* Delete first packet from fifo. */
141 ospf_fifo_pop (struct ospf_fifo
*fifo
)
143 struct ospf_packet
*op
;
149 fifo
->head
= op
->next
;
151 if (fifo
->head
== NULL
)
160 /* Return first fifo entry. */
162 ospf_fifo_head (struct ospf_fifo
*fifo
)
167 /* Flush ospf packet fifo. */
169 ospf_fifo_flush (struct ospf_fifo
*fifo
)
171 struct ospf_packet
*op
;
172 struct ospf_packet
*next
;
174 for (op
= fifo
->head
; op
; op
= next
)
177 ospf_packet_free (op
);
179 fifo
->head
= fifo
->tail
= NULL
;
183 /* Free ospf packet fifo. */
185 ospf_fifo_free (struct ospf_fifo
*fifo
)
187 ospf_fifo_flush (fifo
);
189 XFREE (MTYPE_OSPF_FIFO
, fifo
);
193 ospf_packet_add (struct ospf_interface
*oi
, struct ospf_packet
*op
)
195 /* Add packet to end of queue. */
196 ospf_fifo_push (oi
->obuf
, op
);
198 /* Debug of packet fifo*/
199 /* ospf_fifo_debug (oi->obuf); */
203 ospf_packet_delete (struct ospf_interface
*oi
)
205 struct ospf_packet
*op
;
207 op
= ospf_fifo_pop (oi
->obuf
);
210 ospf_packet_free (op
);
214 ospf_stream_copy (struct stream
*new, struct stream
*s
)
220 memcpy (new->data
, s
->data
, stream_get_endp (s
));
226 ospf_packet_dup (struct ospf_packet
*op
)
228 struct ospf_packet
*new;
230 if (stream_get_endp(op
->s
) != op
->length
)
231 zlog_warn ("ospf_packet_dup stream %ld ospf_packet %d size mismatch",
232 STREAM_SIZE(op
->s
), op
->length
);
234 /* Reserve space for MD5 authentication that may be added later. */
235 new = ospf_packet_new (stream_get_endp(op
->s
) + OSPF_AUTH_MD5_SIZE
);
236 ospf_stream_copy (new->s
, op
->s
);
239 new->length
= op
->length
;
245 ospf_packet_max (struct ospf_interface
*oi
)
249 if ( ospf_auth_type (oi
) == OSPF_AUTH_CRYPTOGRAPHIC
)
250 max
= oi
->ifp
->mtu
- OSPF_AUTH_MD5_SIZE
- 88;
252 max
= oi
->ifp
->mtu
- 88;
259 ospf_check_md5_digest (struct ospf_interface
*oi
, struct stream
*s
,
264 unsigned char digest
[OSPF_AUTH_MD5_SIZE
];
265 unsigned char *pdigest
;
266 struct crypt_key
*ck
;
267 struct ospf_header
*ospfh
;
268 struct ospf_neighbor
*nbr
;
271 ibuf
= STREAM_PNT (s
);
272 ospfh
= (struct ospf_header
*) ibuf
;
274 /* Get pointer to the end of the packet. */
275 pdigest
= ibuf
+ length
;
277 /* Get secret key. */
278 ck
= ospf_crypt_key_lookup (OSPF_IF_PARAM (oi
, auth_crypt
),
279 ospfh
->u
.crypt
.key_id
);
282 zlog_warn ("interface %s: ospf_check_md5 no key %d",
283 IF_NAME (oi
), ospfh
->u
.crypt
.key_id
);
287 /* check crypto seqnum. */
288 nbr
= ospf_nbr_lookup_by_routerid (oi
->nbrs
, &ospfh
->router_id
);
290 if (nbr
&& ntohl(nbr
->crypt_seqnum
) > ntohl(ospfh
->u
.crypt
.crypt_seqnum
))
292 zlog_warn ("interface %s: ospf_check_md5 bad sequence %d (expect %d)",
294 ntohl(ospfh
->u
.crypt
.crypt_seqnum
),
295 ntohl(nbr
->crypt_seqnum
));
299 /* Generate a digest for the ospf packet - their digest + our digest. */
301 md5_process_bytes (ibuf
, length
, &ctx
);
302 md5_process_bytes (ck
->auth_key
, OSPF_AUTH_MD5_SIZE
, &ctx
);
303 md5_finish_ctx (&ctx
, digest
);
305 /* compare the two */
306 if (memcmp (pdigest
, digest
, OSPF_AUTH_MD5_SIZE
))
308 zlog_warn ("interface %s: ospf_check_md5 checksum mismatch",
313 /* save neighbor's crypt_seqnum */
315 nbr
->crypt_seqnum
= ospfh
->u
.crypt
.crypt_seqnum
;
319 /* This function is called from ospf_write(), it will detect the
320 authentication scheme and if it is MD5, it will change the sequence
321 and update the MD5 digest. */
323 ospf_make_md5_digest (struct ospf_interface
*oi
, struct ospf_packet
*op
)
325 struct ospf_header
*ospfh
;
326 unsigned char digest
[OSPF_AUTH_MD5_SIZE
];
329 unsigned long oldputp
;
331 struct crypt_key
*ck
;
334 ibuf
= STREAM_DATA (op
->s
);
335 ospfh
= (struct ospf_header
*) ibuf
;
337 if (ntohs (ospfh
->auth_type
) != OSPF_AUTH_CRYPTOGRAPHIC
)
340 /* We do this here so when we dup a packet, we don't have to
341 waste CPU rewriting other headers. */
342 t
= (time(NULL
) & 0xFFFFFFFF);
343 oi
->crypt_seqnum
= ( t
> oi
->crypt_seqnum
? t
: oi
->crypt_seqnum
++);
344 ospfh
->u
.crypt
.crypt_seqnum
= htonl (oi
->crypt_seqnum
);
346 /* Get MD5 Authentication key from auth_key list. */
347 if (list_isempty (OSPF_IF_PARAM (oi
, auth_crypt
)))
351 ck
= getdata (OSPF_IF_PARAM (oi
, auth_crypt
)->tail
);
352 auth_key
= ck
->auth_key
;
355 /* Generate a digest for the entire packet + our secret key. */
357 md5_process_bytes (ibuf
, ntohs (ospfh
->length
), &ctx
);
358 md5_process_bytes (auth_key
, OSPF_AUTH_MD5_SIZE
, &ctx
);
359 md5_finish_ctx (&ctx
, digest
);
361 /* Append md5 digest to the end of the stream. */
362 oldputp
= stream_get_putp (op
->s
);
363 stream_set_putp (op
->s
, ntohs (ospfh
->length
));
364 stream_put (op
->s
, digest
, OSPF_AUTH_MD5_SIZE
);
365 stream_set_putp (op
->s
, oldputp
);
367 /* We do *NOT* increment the OSPF header length. */
368 op
->length
= ntohs (ospfh
->length
) + OSPF_AUTH_MD5_SIZE
;
370 if (stream_get_endp(op
->s
) != op
->length
)
371 zlog_warn("ospf_make_md5_digest: length mismatch stream %ld ospf_packet %d", stream_get_endp(op
->s
), op
->length
);
373 return OSPF_AUTH_MD5_SIZE
;
378 ospf_ls_req_timer (struct thread
*thread
)
380 struct ospf_neighbor
*nbr
;
382 nbr
= THREAD_ARG (thread
);
383 nbr
->t_ls_req
= NULL
;
385 /* Send Link State Request. */
386 if (ospf_ls_request_count (nbr
))
387 ospf_ls_req_send (nbr
);
389 /* Set Link State Request retransmission timer. */
390 OSPF_NSM_TIMER_ON (nbr
->t_ls_req
, ospf_ls_req_timer
, nbr
->v_ls_req
);
396 ospf_ls_req_event (struct ospf_neighbor
*nbr
)
400 thread_cancel (nbr
->t_ls_req
);
401 nbr
->t_ls_req
= NULL
;
403 nbr
->t_ls_req
= thread_add_event (master
, ospf_ls_req_timer
, nbr
, 0);
406 /* Cyclic timer function. Fist registered in ospf_nbr_new () in
409 ospf_ls_upd_timer (struct thread
*thread
)
411 struct ospf_neighbor
*nbr
;
413 nbr
= THREAD_ARG (thread
);
414 nbr
->t_ls_upd
= NULL
;
416 /* Send Link State Update. */
417 if (ospf_ls_retransmit_count (nbr
) > 0)
420 struct ospf_lsdb
*lsdb
;
423 int retransmit_interval
;
425 gettimeofday (&now
, NULL
);
426 retransmit_interval
= OSPF_IF_PARAM (nbr
->oi
, retransmit_interval
);
428 lsdb
= &nbr
->ls_rxmt
;
429 update
= list_new ();
431 for (i
= OSPF_MIN_LSA
; i
< OSPF_MAX_LSA
; i
++)
433 struct route_table
*table
= lsdb
->type
[i
].db
;
434 struct route_node
*rn
;
436 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
438 struct ospf_lsa
*lsa
;
440 if ((lsa
= rn
->info
) != NULL
)
441 /* Don't retransmit an LSA if we received it within
442 the last RxmtInterval seconds - this is to allow the
443 neighbour a chance to acknowledge the LSA as it may
444 have ben just received before the retransmit timer
445 fired. This is a small tweak to what is in the RFC,
446 but it will cut out out a lot of retransmit traffic
448 if (tv_cmp (tv_sub (now
, lsa
->tv_recv
),
449 int2tv (retransmit_interval
)) >= 0)
450 listnode_add (update
, rn
->info
);
454 if (listcount (update
) > 0)
455 ospf_ls_upd_send (nbr
, update
, OSPF_SEND_PACKET_DIRECT
);
456 list_delete (update
);
459 /* Set LS Update retransmission timer. */
460 OSPF_NSM_TIMER_ON (nbr
->t_ls_upd
, ospf_ls_upd_timer
, nbr
->v_ls_upd
);
466 ospf_ls_ack_timer (struct thread
*thread
)
468 struct ospf_interface
*oi
;
470 oi
= THREAD_ARG (thread
);
473 /* Send Link State Acknowledgment. */
474 if (listcount (oi
->ls_ack
) > 0)
475 ospf_ls_ack_send_delayed (oi
);
477 /* Set LS Ack timer. */
478 OSPF_ISM_TIMER_ON (oi
->t_ls_ack
, ospf_ls_ack_timer
, oi
->v_ls_ack
);
484 ospf_write (struct thread
*thread
)
486 struct ospf
*ospf
= THREAD_ARG (thread
);
487 struct ospf_interface
*oi
;
488 struct ospf_packet
*op
;
489 struct sockaddr_in sa_dst
;
498 ospf
->t_write
= NULL
;
500 node
= listhead (ospf
->oi_write_q
);
505 /* Get one packet from queue. */
506 op
= ospf_fifo_head (oi
->obuf
);
508 assert (op
->length
>= OSPF_HEADER_SIZE
);
510 if (op
->dst
.s_addr
== htonl (OSPF_ALLSPFROUTERS
)
511 || op
->dst
.s_addr
== htonl (OSPF_ALLDROUTERS
))
512 ospf_if_ipmulticast (ospf
, oi
->address
, oi
->ifp
->ifindex
);
514 /* Rewrite the md5 signature & update the seq */
515 ospf_make_md5_digest (oi
, op
);
517 memset (&sa_dst
, 0, sizeof (sa_dst
));
518 sa_dst
.sin_family
= AF_INET
;
520 sa_dst
.sin_len
= sizeof(sa_dst
);
521 #endif /* HAVE_SIN_LEN */
522 sa_dst
.sin_addr
= op
->dst
;
523 sa_dst
.sin_port
= htons (0);
525 /* Set DONTROUTE flag if dst is unicast. */
526 if (oi
->type
!= OSPF_IFTYPE_VIRTUALLINK
)
527 if (!IN_MULTICAST (htonl (op
->dst
.s_addr
)))
528 flags
= MSG_DONTROUTE
;
530 iph
.ip_hl
= sizeof (struct ip
) >> 2;
531 iph
.ip_v
= IPVERSION
;
532 iph
.ip_tos
= IPTOS_PREC_INTERNETCONTROL
;
533 #if defined(__NetBSD__) || defined(__FreeBSD__)
534 iph
.ip_len
= iph
.ip_hl
*4 + op
->length
;
536 iph
.ip_len
= htons (iph
.ip_hl
*4 + op
->length
);
540 if (oi
->type
== OSPF_IFTYPE_VIRTUALLINK
)
541 iph
.ip_ttl
= OSPF_VL_IP_TTL
;
543 iph
.ip_ttl
= OSPF_IP_TTL
;
544 iph
.ip_p
= IPPROTO_OSPFIGP
;
546 iph
.ip_src
.s_addr
= oi
->address
->u
.prefix4
.s_addr
;
547 iph
.ip_dst
.s_addr
= op
->dst
.s_addr
;
549 memset (&msg
, 0, sizeof (msg
));
550 msg
.msg_name
= &sa_dst
;
551 msg
.msg_namelen
= sizeof (sa_dst
);
554 iov
[0].iov_base
= (char*)&iph
;
555 iov
[0].iov_len
= iph
.ip_hl
*4;
556 iov
[1].iov_base
= STREAM_DATA (op
->s
);
557 iov
[1].iov_len
= op
->length
;
559 ret
= sendmsg (ospf
->fd
, &msg
, flags
);
562 zlog_warn ("*** sendmsg in ospf_write to %s failed with %s",
563 inet_ntoa (iph
.ip_dst
), strerror (errno
));
565 /* Retrieve OSPF packet type. */
566 stream_set_getp (op
->s
, 1);
567 type
= stream_getc (op
->s
);
569 /* Show debug sending packet. */
570 if (IS_DEBUG_OSPF_PACKET (type
- 1, SEND
))
572 if (IS_DEBUG_OSPF_PACKET (type
- 1, DETAIL
))
574 zlog_info ("-----------------------------------------------------");
575 stream_set_getp (op
->s
, 0);
576 ospf_packet_dump (op
->s
);
579 zlog_info ("%s sent to [%s] via [%s].",
580 ospf_packet_type_str
[type
], inet_ntoa (op
->dst
),
583 if (IS_DEBUG_OSPF_PACKET (type
- 1, DETAIL
))
584 zlog_info ("-----------------------------------------------------");
587 /* Now delete packet from queue. */
588 ospf_packet_delete (oi
);
590 if (ospf_fifo_head (oi
->obuf
) == NULL
)
593 list_delete_node (ospf
->oi_write_q
, node
);
596 /* If packets still remain in queue, call write thread. */
597 if (!list_isempty (ospf
->oi_write_q
))
599 thread_add_write (master
, ospf_write
, ospf
, ospf
->fd
);
604 /* OSPF Hello message read -- RFC2328 Section 10.5. */
606 ospf_hello (struct ip
*iph
, struct ospf_header
*ospfh
,
607 struct stream
* s
, struct ospf_interface
*oi
, int size
)
609 struct ospf_hello
*hello
;
610 struct ospf_neighbor
*nbr
;
614 /* increment statistics. */
617 hello
= (struct ospf_hello
*) STREAM_PNT (s
);
619 /* If Hello is myself, silently discard. */
620 if (IPV4_ADDR_SAME (&ospfh
->router_id
, &oi
->ospf
->router_id
))
622 if (IS_DEBUG_OSPF_PACKET (ospfh
->type
- 1, RECV
))
624 zlog_info ("ospf_header[%s/%s]: selforiginated, "
626 ospf_packet_type_str
[ospfh
->type
],
627 inet_ntoa (iph
->ip_src
));
632 /* If incoming interface is passive one, ignore Hello. */
633 if (OSPF_IF_PARAM (oi
, passive_interface
) == OSPF_IF_PASSIVE
) {
634 zlog_info ("Packet %s [HELLO:RECV]: oi is passive",
635 inet_ntoa (ospfh
->router_id
));
639 /* get neighbor prefix. */
641 p
.prefixlen
= ip_masklen (hello
->network_mask
);
642 p
.u
.prefix4
= iph
->ip_src
;
644 /* Compare network mask. */
645 /* Checking is ignored for Point-to-Point and Virtual link. */
646 if (oi
->type
!= OSPF_IFTYPE_POINTOPOINT
647 && oi
->type
!= OSPF_IFTYPE_VIRTUALLINK
)
648 if (oi
->address
->prefixlen
!= p
.prefixlen
)
650 zlog_warn ("Packet %s [Hello:RECV]: NetworkMask mismatch.",
651 inet_ntoa (ospfh
->router_id
));
655 /* Compare Hello Interval. */
656 if (OSPF_IF_PARAM (oi
, v_hello
) != ntohs (hello
->hello_interval
))
658 zlog_warn ("Packet %s [Hello:RECV]: HelloInterval mismatch.",
659 inet_ntoa (ospfh
->router_id
));
663 /* Compare Router Dead Interval. */
664 if (OSPF_IF_PARAM (oi
, v_wait
) != ntohl (hello
->dead_interval
))
666 zlog_warn ("Packet %s [Hello:RECV]: RouterDeadInterval mismatch.",
667 inet_ntoa (ospfh
->router_id
));
671 if (IS_DEBUG_OSPF_EVENT
)
672 zlog_info ("Packet %s [Hello:RECV]: Options %s",
673 inet_ntoa (ospfh
->router_id
),
674 ospf_options_dump (hello
->options
));
676 /* Compare options. */
677 #define REJECT_IF_TBIT_ON 1 /* XXX */
678 #ifdef REJECT_IF_TBIT_ON
679 if (CHECK_FLAG (hello
->options
, OSPF_OPTION_T
))
682 * This router does not support non-zero TOS.
683 * Drop this Hello packet not to establish neighbor relationship.
685 zlog_warn ("Packet %s [Hello:RECV]: T-bit on, drop it.",
686 inet_ntoa (ospfh
->router_id
));
689 #endif /* REJECT_IF_TBIT_ON */
691 #ifdef HAVE_OPAQUE_LSA
692 if (CHECK_FLAG (oi
->ospf
->config
, OSPF_OPAQUE_CAPABLE
)
693 && CHECK_FLAG (hello
->options
, OSPF_OPTION_O
))
696 * This router does know the correct usage of O-bit
697 * the bit should be set in DD packet only.
699 zlog_warn ("Packet %s [Hello:RECV]: O-bit abuse?",
700 inet_ntoa (ospfh
->router_id
));
701 #ifdef STRICT_OBIT_USAGE_CHECK
702 return; /* Reject this packet. */
703 #else /* STRICT_OBIT_USAGE_CHECK */
704 UNSET_FLAG (hello
->options
, OSPF_OPTION_O
); /* Ignore O-bit. */
705 #endif /* STRICT_OBIT_USAGE_CHECK */
707 #endif /* HAVE_OPAQUE_LSA */
709 /* new for NSSA is to ensure that NP is on and E is off */
711 if (oi
->area
->external_routing
== OSPF_AREA_NSSA
)
713 if (! (CHECK_FLAG (OPTIONS (oi
), OSPF_OPTION_NP
)
714 && CHECK_FLAG (hello
->options
, OSPF_OPTION_NP
)
715 && ! CHECK_FLAG (OPTIONS (oi
), OSPF_OPTION_E
)
716 && ! CHECK_FLAG (hello
->options
, OSPF_OPTION_E
)))
718 zlog_warn ("NSSA-Packet-%s[Hello:RECV]: my options: %x, his options %x", inet_ntoa (ospfh
->router_id
), OPTIONS (oi
), hello
->options
);
721 if (IS_DEBUG_OSPF_NSSA
)
722 zlog_info ("NSSA-Hello:RECV:Packet from %s:", inet_ntoa(ospfh
->router_id
));
725 /* The setting of the E-bit found in the Hello Packet's Options
726 field must match this area's ExternalRoutingCapability A
727 mismatch causes processing to stop and the packet to be
728 dropped. The setting of the rest of the bits in the Hello
729 Packet's Options field should be ignored. */
730 if (CHECK_FLAG (OPTIONS (oi
), OSPF_OPTION_E
) !=
731 CHECK_FLAG (hello
->options
, OSPF_OPTION_E
))
733 zlog_warn ("Packet[Hello:RECV]: my options: %x, his options %x",
734 OPTIONS (oi
), hello
->options
);
738 /* get neighbour struct */
739 nbr
= ospf_nbr_get (oi
, ospfh
, iph
, &p
);
741 /* neighbour must be valid, ospf_nbr_get creates if none existed */
744 old_state
= nbr
->state
;
746 /* Add event to thread. */
747 OSPF_NSM_EVENT_EXECUTE (nbr
, NSM_HelloReceived
);
749 /* RFC2328 Section 9.5.1
750 If the router is not eligible to become Designated Router,
751 (snip) It must also send an Hello Packet in reply to an
752 Hello Packet received from any eligible neighbor (other than
753 the current Designated Router and Backup Designated Router). */
754 if (oi
->type
== OSPF_IFTYPE_NBMA
)
755 if (PRIORITY(oi
) == 0 && hello
->priority
> 0
756 && IPV4_ADDR_CMP(&DR(oi
), &iph
->ip_src
)
757 && IPV4_ADDR_CMP(&BDR(oi
), &iph
->ip_src
))
758 OSPF_NSM_TIMER_ON (nbr
->t_hello_reply
, ospf_hello_reply_timer
,
759 OSPF_HELLO_REPLY_DELAY
);
761 /* on NBMA network type, it happens to receive bidirectional Hello packet
762 without advance 1-Way Received event.
763 To avoid incorrect DR-seletion, raise 1-Way Received event.*/
764 if (oi
->type
== OSPF_IFTYPE_NBMA
&&
765 (old_state
== NSM_Down
|| old_state
== NSM_Attempt
))
767 OSPF_NSM_EVENT_EXECUTE (nbr
, NSM_OneWayReceived
);
768 nbr
->priority
= hello
->priority
;
769 nbr
->d_router
= hello
->d_router
;
770 nbr
->bd_router
= hello
->bd_router
;
774 if (ospf_nbr_bidirectional (&oi
->ospf
->router_id
, hello
->neighbors
,
775 size
- OSPF_HELLO_MIN_SIZE
))
777 OSPF_NSM_EVENT_EXECUTE (nbr
, NSM_TwoWayReceived
);
778 nbr
->options
|= hello
->options
;
782 OSPF_NSM_EVENT_EXECUTE (nbr
, NSM_OneWayReceived
);
783 /* Set neighbor information. */
784 nbr
->priority
= hello
->priority
;
785 nbr
->d_router
= hello
->d_router
;
786 nbr
->bd_router
= hello
->bd_router
;
790 /* If neighbor itself declares DR and no BDR exists,
791 cause event BackupSeen */
792 if (IPV4_ADDR_SAME (&nbr
->address
.u
.prefix4
, &hello
->d_router
))
793 if (hello
->bd_router
.s_addr
== 0 && oi
->state
== ISM_Waiting
)
794 OSPF_ISM_EVENT_SCHEDULE (oi
, ISM_BackupSeen
);
796 /* neighbor itself declares BDR. */
797 if (oi
->state
== ISM_Waiting
&&
798 IPV4_ADDR_SAME (&nbr
->address
.u
.prefix4
, &hello
->bd_router
))
799 OSPF_ISM_EVENT_SCHEDULE (oi
, ISM_BackupSeen
);
801 /* had not previously. */
802 if ((IPV4_ADDR_SAME (&nbr
->address
.u
.prefix4
, &hello
->d_router
) &&
803 IPV4_ADDR_CMP (&nbr
->address
.u
.prefix4
, &nbr
->d_router
)) ||
804 (IPV4_ADDR_CMP (&nbr
->address
.u
.prefix4
, &hello
->d_router
) &&
805 IPV4_ADDR_SAME (&nbr
->address
.u
.prefix4
, &nbr
->d_router
)))
806 OSPF_ISM_EVENT_SCHEDULE (oi
, ISM_NeighborChange
);
808 /* had not previously. */
809 if ((IPV4_ADDR_SAME (&nbr
->address
.u
.prefix4
, &hello
->bd_router
) &&
810 IPV4_ADDR_CMP (&nbr
->address
.u
.prefix4
, &nbr
->bd_router
)) ||
811 (IPV4_ADDR_CMP (&nbr
->address
.u
.prefix4
, &hello
->bd_router
) &&
812 IPV4_ADDR_SAME (&nbr
->address
.u
.prefix4
, &nbr
->bd_router
)))
813 OSPF_ISM_EVENT_SCHEDULE (oi
, ISM_NeighborChange
);
815 /* Neighbor priority check. */
816 if (nbr
->priority
>= 0 && nbr
->priority
!= hello
->priority
)
817 OSPF_ISM_EVENT_SCHEDULE (oi
, ISM_NeighborChange
);
819 /* Set neighbor information. */
820 nbr
->priority
= hello
->priority
;
821 nbr
->d_router
= hello
->d_router
;
822 nbr
->bd_router
= hello
->bd_router
;
825 /* Save DD flags/options/Seqnum received. */
827 ospf_db_desc_save_current (struct ospf_neighbor
*nbr
,
828 struct ospf_db_desc
*dd
)
830 nbr
->last_recv
.flags
= dd
->flags
;
831 nbr
->last_recv
.options
= dd
->options
;
832 nbr
->last_recv
.dd_seqnum
= ntohl (dd
->dd_seqnum
);
835 /* Process rest of DD packet. */
837 ospf_db_desc_proc (struct stream
*s
, struct ospf_interface
*oi
,
838 struct ospf_neighbor
*nbr
, struct ospf_db_desc
*dd
,
841 struct ospf_lsa
*new, *find
;
842 struct lsa_header
*lsah
;
844 stream_forward (s
, OSPF_DB_DESC_MIN_SIZE
);
845 for (size
-= OSPF_DB_DESC_MIN_SIZE
;
846 size
>= OSPF_LSA_HEADER_SIZE
; size
-= OSPF_LSA_HEADER_SIZE
)
848 lsah
= (struct lsa_header
*) STREAM_PNT (s
);
849 stream_forward (s
, OSPF_LSA_HEADER_SIZE
);
851 /* Unknown LS type. */
852 if (lsah
->type
< OSPF_MIN_LSA
|| lsah
->type
>= OSPF_MAX_LSA
)
854 zlog_warn ("Pakcet [DD:RECV]: Unknown LS type %d.", lsah
->type
);
855 OSPF_NSM_EVENT_SCHEDULE (nbr
, NSM_SeqNumberMismatch
);
859 #ifdef HAVE_OPAQUE_LSA
860 if (IS_OPAQUE_LSA (lsah
->type
)
861 && ! CHECK_FLAG (nbr
->options
, OSPF_OPTION_O
))
863 zlog_warn ("LSA[Type%d:%s]: Opaque capability mismatch?", lsah
->type
, inet_ntoa (lsah
->id
));
864 OSPF_NSM_EVENT_SCHEDULE (nbr
, NSM_SeqNumberMismatch
);
867 #endif /* HAVE_OPAQUE_LSA */
871 case OSPF_AS_EXTERNAL_LSA
:
872 #ifdef HAVE_OPAQUE_LSA
873 case OSPF_OPAQUE_AS_LSA
:
874 #endif /* HAVE_OPAQUE_LSA */
875 /* Check for stub area. Reject if AS-External from stub but
876 allow if from NSSA. */
877 if (oi
->area
->external_routing
== OSPF_AREA_STUB
)
879 zlog_warn ("Packet [DD:RECV]: LSA[Type%d:%s] from %s area.",
880 lsah
->type
, inet_ntoa (lsah
->id
),
881 (oi
->area
->external_routing
== OSPF_AREA_STUB
) ?\
883 OSPF_NSM_EVENT_SCHEDULE (nbr
, NSM_SeqNumberMismatch
);
891 /* Create LS-request object. */
892 new = ospf_ls_request_new (lsah
);
894 /* Lookup received LSA, then add LS request list. */
895 find
= ospf_lsa_lookup_by_header (oi
->area
, lsah
);
896 if (!find
|| ospf_lsa_more_recent (find
, new) < 0)
898 ospf_ls_request_add (nbr
, new);
899 ospf_lsa_discard (new);
903 /* Received LSA is not recent. */
904 if (IS_DEBUG_OSPF_EVENT
)
905 zlog_info ("Packet [DD:RECV]: LSA received Type %d, "
906 "ID %s is not recent.", lsah
->type
, inet_ntoa (lsah
->id
));
907 ospf_lsa_discard (new);
913 if (IS_SET_DD_MS (nbr
->dd_flags
))
916 /* Entire DD packet sent. */
917 if (!IS_SET_DD_M (dd
->flags
) && !IS_SET_DD_M (nbr
->dd_flags
))
918 OSPF_NSM_EVENT_SCHEDULE (nbr
, NSM_ExchangeDone
);
920 /* Send new DD packet. */
921 ospf_db_desc_send (nbr
);
926 nbr
->dd_seqnum
= ntohl (dd
->dd_seqnum
);
928 /* When master's more flags is not set. */
929 if (!IS_SET_DD_M (dd
->flags
) && ospf_db_summary_isempty (nbr
))
931 nbr
->dd_flags
&= ~(OSPF_DD_FLAG_M
);
932 OSPF_NSM_EVENT_SCHEDULE (nbr
, NSM_ExchangeDone
);
935 /* Send DD pakcet in reply. */
936 ospf_db_desc_send (nbr
);
939 /* Save received neighbor values from DD. */
940 ospf_db_desc_save_current (nbr
, dd
);
944 ospf_db_desc_is_dup (struct ospf_db_desc
*dd
, struct ospf_neighbor
*nbr
)
946 /* Is DD duplicated? */
947 if (dd
->options
== nbr
->last_recv
.options
&&
948 dd
->flags
== nbr
->last_recv
.flags
&&
949 dd
->dd_seqnum
== htonl (nbr
->last_recv
.dd_seqnum
))
955 /* OSPF Database Description message read -- RFC2328 Section 10.6. */
957 ospf_db_desc (struct ip
*iph
, struct ospf_header
*ospfh
,
958 struct stream
*s
, struct ospf_interface
*oi
, u_int16_t size
)
960 struct ospf_db_desc
*dd
;
961 struct ospf_neighbor
*nbr
;
963 /* Increment statistics. */
966 dd
= (struct ospf_db_desc
*) STREAM_PNT (s
);
968 nbr
= ospf_nbr_lookup (oi
, iph
, ospfh
);
971 zlog_warn ("Packet[DD]: Unknown Neighbor %s",
972 inet_ntoa (ospfh
->router_id
));
977 if (ntohs (dd
->mtu
) > oi
->ifp
->mtu
)
979 zlog_warn ("Packet[DD]: MTU is larger than [%s]'s MTU", IF_NAME (oi
));
984 * XXX HACK by Hasso Tepper. Setting N/P bit in NSSA area DD packets is not
985 * required. In fact at least JunOS sends DD packets with P bit clear.
986 * Until proper solution is developped, this hack should help.
988 * Update: According to the RFCs, N bit is specified /only/ for Hello
989 * options, unfortunately its use in DD options is not specified. Hence some
990 * implementations follow E-bit semantics and set it in DD options, and some
991 * treat it as unspecified and hence follow the directive "default for
992 * options is clear", ie unset.
994 * Reset the flag, as ospfd follows E-bit semantics.
996 if ( (oi
->area
->external_routing
== OSPF_AREA_NSSA
)
997 && (CHECK_FLAG (nbr
->options
, OSPF_OPTION_NP
))
998 && (!CHECK_FLAG (dd
->options
, OSPF_OPTION_NP
)) )
1000 if (IS_DEBUG_OSPF_EVENT
)
1001 zlog_notice ("Packet[DD]: Neighbour %s: Has NSSA capability, sends with N bit clear in DD options",
1002 inet_ntoa (nbr
->router_id
) );
1003 SET_FLAG (dd
->options
, OSPF_OPTION_NP
);
1006 #ifdef REJECT_IF_TBIT_ON
1007 if (CHECK_FLAG (dd
->options
, OSPF_OPTION_T
))
1010 * In Hello protocol, optional capability must have checked
1011 * to prevent this T-bit enabled router be my neighbor.
1013 zlog_warn ("Packet[DD]: Neighbor %s: T-bit on?", inet_ntoa (nbr
->router_id
));
1016 #endif /* REJECT_IF_TBIT_ON */
1018 #ifdef HAVE_OPAQUE_LSA
1019 if (CHECK_FLAG (dd
->options
, OSPF_OPTION_O
)
1020 && !CHECK_FLAG (oi
->ospf
->config
, OSPF_OPAQUE_CAPABLE
))
1023 * This node is not configured to handle O-bit, for now.
1024 * Clear it to ignore unsupported capability proposed by neighbor.
1026 UNSET_FLAG (dd
->options
, OSPF_OPTION_O
);
1028 #endif /* HAVE_OPAQUE_LSA */
1030 /* Process DD packet by neighbor status. */
1036 zlog_warn ("Packet[DD]: Neighbor state is %s, packet discarded.",
1037 LOOKUP (ospf_nsm_state_msg
, nbr
->state
));
1040 OSPF_NSM_EVENT_EXECUTE (nbr
, NSM_TwoWayReceived
);
1041 /* If the new state is ExStart, the processing of the current
1042 packet should then continue in this new state by falling
1043 through to case ExStart below. */
1044 if (nbr
->state
!= NSM_ExStart
)
1048 if ((IS_SET_DD_ALL (dd
->flags
) == OSPF_DD_FLAG_ALL
) &&
1049 (size
== OSPF_DB_DESC_MIN_SIZE
))
1051 if (IPV4_ADDR_CMP (&nbr
->router_id
, &oi
->ospf
->router_id
) > 0)
1053 /* We're Slave---obey */
1054 zlog_warn ("Packet[DD]: Negotiation done (Slave).");
1055 nbr
->dd_seqnum
= ntohl (dd
->dd_seqnum
);
1056 nbr
->dd_flags
&= ~(OSPF_DD_FLAG_MS
|OSPF_DD_FLAG_I
); /* Reset I/MS */
1060 /* We're Master, ignore the initial DBD from Slave */
1061 zlog_warn ("Packet[DD]: Initial DBD from Slave, ignoring.");
1065 /* Ack from the Slave */
1066 else if (!IS_SET_DD_MS (dd
->flags
) && !IS_SET_DD_I (dd
->flags
) &&
1067 ntohl (dd
->dd_seqnum
) == nbr
->dd_seqnum
&&
1068 IPV4_ADDR_CMP (&nbr
->router_id
, &oi
->ospf
->router_id
) < 0)
1070 zlog_warn ("Packet[DD]: Negotiation done (Master).");
1071 nbr
->dd_flags
&= ~OSPF_DD_FLAG_I
;
1075 zlog_warn ("Packet[DD]: Negotiation fails.");
1079 /* This is where the real Options are saved */
1080 nbr
->options
= dd
->options
;
1082 #ifdef HAVE_OPAQUE_LSA
1083 if (CHECK_FLAG (oi
->ospf
->config
, OSPF_OPAQUE_CAPABLE
))
1085 if (IS_DEBUG_OSPF_EVENT
)
1086 zlog_info ("Neighbor[%s] is %sOpaque-capable.",
1087 inet_ntoa (nbr
->router_id
),
1088 CHECK_FLAG (nbr
->options
, OSPF_OPTION_O
) ? "" : "NOT ");
1090 if (! CHECK_FLAG (nbr
->options
, OSPF_OPTION_O
)
1091 && IPV4_ADDR_SAME (&DR (oi
), &nbr
->address
.u
.prefix4
))
1093 zlog_warn ("DR-neighbor[%s] is NOT opaque-capable; Opaque-LSAs cannot be reliably advertised in this network.", inet_ntoa (nbr
->router_id
));
1094 /* This situation is undesirable, but not a real error. */
1097 #endif /* HAVE_OPAQUE_LSA */
1099 OSPF_NSM_EVENT_EXECUTE (nbr
, NSM_NegotiationDone
);
1101 /* continue processing rest of packet. */
1102 ospf_db_desc_proc (s
, oi
, nbr
, dd
, size
);
1105 if (ospf_db_desc_is_dup (dd
, nbr
))
1107 if (IS_SET_DD_MS (nbr
->dd_flags
))
1108 /* Master: discard duplicated DD packet. */
1109 zlog_warn ("Packet[DD] (Master): packet duplicated.");
1111 /* Slave: cause to retransmit the last Database Description. */
1113 zlog_warn ("Packet[DD] [Slave]: packet duplicated.");
1114 ospf_db_desc_resend (nbr
);
1119 /* Otherwise DD packet should be checked. */
1120 /* Check Master/Slave bit mismatch */
1121 if (IS_SET_DD_MS (dd
->flags
) != IS_SET_DD_MS (nbr
->last_recv
.flags
))
1123 zlog_warn ("Packet[DD]: MS-bit mismatch.");
1124 OSPF_NSM_EVENT_SCHEDULE (nbr
, NSM_SeqNumberMismatch
);
1125 if (IS_DEBUG_OSPF_EVENT
)
1126 zlog_info ("Packet[DD]: dd->flags=%d, nbr->dd_flags=%d",
1127 dd
->flags
, nbr
->dd_flags
);
1131 /* Check initialize bit is set. */
1132 if (IS_SET_DD_I (dd
->flags
))
1134 zlog_warn ("Packet[DD]: I-bit set.");
1135 OSPF_NSM_EVENT_SCHEDULE (nbr
, NSM_SeqNumberMismatch
);
1139 /* Check DD Options. */
1140 if (dd
->options
!= nbr
->options
)
1142 #ifdef ORIGINAL_CODING
1143 /* Save the new options for debugging */
1144 nbr
->options
= dd
->options
;
1145 #endif /* ORIGINAL_CODING */
1146 zlog_warn ("Packet[DD]: options mismatch.");
1147 OSPF_NSM_EVENT_SCHEDULE (nbr
, NSM_SeqNumberMismatch
);
1151 /* Check DD sequence number. */
1152 if ((IS_SET_DD_MS (nbr
->dd_flags
) &&
1153 ntohl (dd
->dd_seqnum
) != nbr
->dd_seqnum
) ||
1154 (!IS_SET_DD_MS (nbr
->dd_flags
) &&
1155 ntohl (dd
->dd_seqnum
) != nbr
->dd_seqnum
+ 1))
1157 zlog_warn ("Pakcet[DD]: sequence number mismatch.");
1158 OSPF_NSM_EVENT_SCHEDULE (nbr
, NSM_SeqNumberMismatch
);
1162 /* Continue processing rest of packet. */
1163 ospf_db_desc_proc (s
, oi
, nbr
, dd
, size
);
1167 if (ospf_db_desc_is_dup (dd
, nbr
))
1169 if (IS_SET_DD_MS (nbr
->dd_flags
))
1171 /* Master should discard duplicate DD packet. */
1172 zlog_warn ("Pakcet[DD]: duplicated, packet discarded.");
1177 struct timeval t
, now
;
1178 gettimeofday (&now
, NULL
);
1179 t
= tv_sub (now
, nbr
->last_send_ts
);
1180 if (tv_cmp (t
, int2tv (nbr
->v_inactivity
)) < 0)
1182 /* In states Loading and Full the slave must resend
1183 its last Database Description packet in response to
1184 duplicate Database Description packets received
1185 from the master. For this reason the slave must
1186 wait RouterDeadInterval seconds before freeing the
1187 last Database Description packet. Reception of a
1188 Database Description packet from the master after
1189 this interval will generate a SeqNumberMismatch
1190 neighbor event. RFC2328 Section 10.8 */
1191 ospf_db_desc_resend (nbr
);
1197 OSPF_NSM_EVENT_SCHEDULE (nbr
, NSM_SeqNumberMismatch
);
1200 zlog_warn ("Packet[DD]: NSM illegal status.");
1205 #define OSPF_LSA_KEY_SIZE 12 /* type(4) + id(4) + ar(4) */
1207 /* OSPF Link State Request Read -- RFC2328 Section 10.7. */
1209 ospf_ls_req (struct ip
*iph
, struct ospf_header
*ospfh
,
1210 struct stream
*s
, struct ospf_interface
*oi
, u_int16_t size
)
1212 struct ospf_neighbor
*nbr
;
1214 struct in_addr ls_id
;
1215 struct in_addr adv_router
;
1216 struct ospf_lsa
*find
;
1220 /* Increment statistics. */
1223 nbr
= ospf_nbr_lookup (oi
, iph
, ospfh
);
1226 zlog_warn ("Link State Request: Unknown Neighbor %s.",
1227 inet_ntoa (ospfh
->router_id
));
1231 /* Neighbor State should be Exchange or later. */
1232 if (nbr
->state
!= NSM_Exchange
&&
1233 nbr
->state
!= NSM_Loading
&&
1234 nbr
->state
!= NSM_Full
)
1236 zlog_warn ("Link State Request: Neighbor state is %s, packet discarded.",
1237 LOOKUP (ospf_nsm_state_msg
, nbr
->state
));
1241 /* Send Link State Update for ALL requested LSAs. */
1242 ls_upd
= list_new ();
1243 length
= OSPF_HEADER_SIZE
+ OSPF_LS_UPD_MIN_SIZE
;
1245 while (size
>= OSPF_LSA_KEY_SIZE
)
1247 /* Get one slice of Link State Request. */
1248 ls_type
= stream_getl (s
);
1249 ls_id
.s_addr
= stream_get_ipv4 (s
);
1250 adv_router
.s_addr
= stream_get_ipv4 (s
);
1252 /* Verify LSA type. */
1253 if (ls_type
< OSPF_MIN_LSA
|| ls_type
>= OSPF_MAX_LSA
)
1255 OSPF_NSM_EVENT_SCHEDULE (nbr
, NSM_BadLSReq
);
1256 list_delete (ls_upd
);
1260 /* Search proper LSA in LSDB. */
1261 find
= ospf_lsa_lookup (oi
->area
, ls_type
, ls_id
, adv_router
);
1264 OSPF_NSM_EVENT_SCHEDULE (nbr
, NSM_BadLSReq
);
1265 list_delete (ls_upd
);
1269 /* Packet overflows MTU size, send immediatly. */
1270 if (length
+ ntohs (find
->data
->length
) > OSPF_PACKET_MAX (oi
))
1272 if (oi
->type
== OSPF_IFTYPE_NBMA
)
1273 ospf_ls_upd_send (nbr
, ls_upd
, OSPF_SEND_PACKET_DIRECT
);
1275 ospf_ls_upd_send (nbr
, ls_upd
, OSPF_SEND_PACKET_INDIRECT
);
1277 /* Only remove list contents. Keep ls_upd. */
1278 list_delete_all_node (ls_upd
);
1280 length
= OSPF_HEADER_SIZE
+ OSPF_LS_UPD_MIN_SIZE
;
1283 /* Append LSA to update list. */
1284 listnode_add (ls_upd
, find
);
1285 length
+= ntohs (find
->data
->length
);
1287 size
-= OSPF_LSA_KEY_SIZE
;
1290 /* Send rest of Link State Update. */
1291 if (listcount (ls_upd
) > 0)
1293 if (oi
->type
== OSPF_IFTYPE_NBMA
)
1294 ospf_ls_upd_send (nbr
, ls_upd
, OSPF_SEND_PACKET_DIRECT
);
1296 ospf_ls_upd_send (nbr
, ls_upd
, OSPF_SEND_PACKET_INDIRECT
);
1298 list_delete (ls_upd
);
1304 /* Get the list of LSAs from Link State Update packet.
1305 And process some validation -- RFC2328 Section 13. (1)-(2). */
1307 ospf_ls_upd_list_lsa (struct ospf_neighbor
*nbr
, struct stream
*s
,
1308 struct ospf_interface
*oi
, size_t size
)
1310 u_int16_t count
, sum
;
1312 struct lsa_header
*lsah
;
1313 struct ospf_lsa
*lsa
;
1318 count
= stream_getl (s
);
1319 size
-= OSPF_LS_UPD_MIN_SIZE
; /* # LSAs */
1321 for (; size
>= OSPF_LSA_HEADER_SIZE
&& count
> 0;
1322 size
-= length
, stream_forward (s
, length
), count
--)
1324 lsah
= (struct lsa_header
*) STREAM_PNT (s
);
1325 length
= ntohs (lsah
->length
);
1329 zlog_warn ("Link State Update: LSA length exceeds packet size.");
1333 /* Validate the LSA's LS checksum. */
1334 sum
= lsah
->checksum
;
1335 if (sum
!= ospf_lsa_checksum (lsah
))
1337 zlog_warn ("Link State Update: LSA checksum error %x, %x.",
1338 sum
, lsah
->checksum
);
1342 /* Examine the LSA's LS type. */
1343 if (lsah
->type
< OSPF_MIN_LSA
|| lsah
->type
>= OSPF_MAX_LSA
)
1345 zlog_warn ("Link State Update: Unknown LS type %d", lsah
->type
);
1350 * What if the received LSA's age is greater than MaxAge?
1351 * Treat it as a MaxAge case -- endo.
1353 if (ntohs (lsah
->ls_age
) > OSPF_LSA_MAXAGE
)
1354 lsah
->ls_age
= htons (OSPF_LSA_MAXAGE
);
1356 #ifdef HAVE_OPAQUE_LSA
1357 if (CHECK_FLAG (nbr
->options
, OSPF_OPTION_O
))
1359 #ifdef STRICT_OBIT_USAGE_CHECK
1360 if ((IS_OPAQUE_LSA(lsah
->type
) &&
1361 ! CHECK_FLAG (lsah
->options
, OSPF_OPTION_O
))
1362 || (! IS_OPAQUE_LSA(lsah
->type
) &&
1363 CHECK_FLAG (lsah
->options
, OSPF_OPTION_O
)))
1366 * This neighbor must know the exact usage of O-bit;
1367 * the bit will be set in Type-9,10,11 LSAs only.
1369 zlog_warn ("LSA[Type%d:%s]: O-bit abuse?", lsah
->type
, inet_ntoa (lsah
->id
));
1372 #endif /* STRICT_OBIT_USAGE_CHECK */
1374 /* Do not take in AS External Opaque-LSAs if we are a stub. */
1375 if (lsah
->type
== OSPF_OPAQUE_AS_LSA
1376 && nbr
->oi
->area
->external_routing
!= OSPF_AREA_DEFAULT
)
1378 if (IS_DEBUG_OSPF_EVENT
)
1379 zlog_info ("LSA[Type%d:%s]: We are a stub, don't take this LSA.", lsah
->type
, inet_ntoa (lsah
->id
));
1383 else if (IS_OPAQUE_LSA(lsah
->type
))
1385 zlog_warn ("LSA[Type%d:%s]: Opaque capability mismatch?", lsah
->type
, inet_ntoa (lsah
->id
));
1388 #endif /* HAVE_OPAQUE_LSA */
1390 /* Create OSPF LSA instance. */
1391 lsa
= ospf_lsa_new ();
1393 /* We may wish to put some error checking if type NSSA comes in
1394 and area not in NSSA mode */
1397 case OSPF_AS_EXTERNAL_LSA
:
1398 #ifdef HAVE_OPAQUE_LSA
1399 case OSPF_OPAQUE_AS_LSA
:
1402 case OSPF_OPAQUE_LINK_LSA
:
1403 lsa
->oi
= oi
; /* Remember incoming interface for flooding control. */
1405 #endif /* HAVE_OPAQUE_LSA */
1407 lsa
->area
= oi
->area
;
1411 lsa
->data
= ospf_lsa_data_new (length
);
1412 memcpy (lsa
->data
, lsah
, length
);
1414 if (IS_DEBUG_OSPF_EVENT
)
1415 zlog_info("LSA[Type%d:%s]: %p new LSA created with Link State Update",
1416 lsa
->data
->type
, inet_ntoa (lsa
->data
->id
), lsa
);
1417 listnode_add (lsas
, lsa
);
1423 /* Cleanup Update list. */
1425 ospf_upd_list_clean (list lsas
)
1428 struct ospf_lsa
*lsa
;
1430 for (node
= listhead (lsas
); node
; nextnode (node
))
1431 if ((lsa
= getdata (node
)) != NULL
)
1432 ospf_lsa_discard (lsa
);
1437 /* OSPF Link State Update message read -- RFC2328 Section 13. */
1439 ospf_ls_upd (struct ip
*iph
, struct ospf_header
*ospfh
,
1440 struct stream
*s
, struct ospf_interface
*oi
, u_int16_t size
)
1442 struct ospf_neighbor
*nbr
;
1444 #ifdef HAVE_OPAQUE_LSA
1445 list mylsa_acks
, mylsa_upds
;
1446 #endif /* HAVE_OPAQUE_LSA */
1447 listnode node
, next
;
1448 struct ospf_lsa
*lsa
= NULL
;
1449 /* unsigned long ls_req_found = 0; */
1451 /* Dis-assemble the stream, update each entry, re-encapsulate for flooding */
1453 /* Increment statistics. */
1456 /* Check neighbor. */
1457 nbr
= ospf_nbr_lookup (oi
, iph
, ospfh
);
1460 zlog_warn ("Link State Update: Unknown Neighbor %s on int: %s",
1461 inet_ntoa (ospfh
->router_id
), IF_NAME (oi
));
1465 /* Check neighbor state. */
1466 if (nbr
->state
< NSM_Exchange
)
1468 zlog_warn ("Link State Update: Neighbor[%s] state is less than Exchange",
1469 inet_ntoa (ospfh
->router_id
));
1473 /* Get list of LSAs from Link State Update packet. - Also perorms Stages
1474 * 1 (validate LSA checksum) and 2 (check for LSA consistent type)
1477 lsas
= ospf_ls_upd_list_lsa (nbr
, s
, oi
, size
);
1479 #ifdef HAVE_OPAQUE_LSA
1481 * Prepare two kinds of lists to clean up unwanted self-originated
1482 * Opaque-LSAs from the routing domain as soon as possible.
1484 mylsa_acks
= list_new (); /* Let the sender cease retransmission. */
1485 mylsa_upds
= list_new (); /* Flush target LSAs if necessary. */
1488 * If self-originated Opaque-LSAs that have flooded before restart
1489 * are contained in the received LSUpd message, corresponding LSReq
1490 * messages to be sent may have to be modified.
1491 * To eliminate possible race conditions such that flushing and normal
1492 * updating for the same LSA would take place alternately, this trick
1493 * must be done before entering to the loop below.
1495 ospf_opaque_adjust_lsreq (nbr
, lsas
);
1496 #endif /* HAVE_OPAQUE_LSA */
1498 #define DISCARD_LSA(L,N) {\
1499 if (IS_DEBUG_OSPF_EVENT) \
1500 zlog_info ("ospf_lsa_discard() in ospf_ls_upd() point %d: lsa %p Type-%d", N, lsa, (int) lsa->data->type); \
1501 ospf_lsa_discard (L); \
1504 /* Process each LSA received in the one packet. */
1505 for (node
= listhead (lsas
); node
; node
= next
)
1507 struct ospf_lsa
*ls_ret
, *current
;
1512 lsa
= getdata (node
);
1514 if (IS_DEBUG_OSPF_NSSA
)
1516 char buf1
[INET_ADDRSTRLEN
];
1517 char buf2
[INET_ADDRSTRLEN
];
1518 char buf3
[INET_ADDRSTRLEN
];
1520 zlog_info("LSA Type-%d from %s, ID: %s, ADV: %s",
1522 inet_ntop (AF_INET
, &ospfh
->router_id
,
1523 buf1
, INET_ADDRSTRLEN
),
1524 inet_ntop (AF_INET
, &lsa
->data
->id
,
1525 buf2
, INET_ADDRSTRLEN
),
1526 inet_ntop (AF_INET
, &lsa
->data
->adv_router
,
1527 buf3
, INET_ADDRSTRLEN
));
1530 listnode_delete (lsas
, lsa
); /* We don't need it in list anymore */
1532 /* Validate Checksum - Done above by ospf_ls_upd_list_lsa() */
1534 /* LSA Type - Done above by ospf_ls_upd_list_lsa() */
1536 /* Do not take in AS External LSAs if we are a stub or NSSA. */
1538 /* Do not take in AS NSSA if this neighbor and we are not NSSA */
1540 /* Do take in Type-7's if we are an NSSA */
1542 /* If we are also an ABR, later translate them to a Type-5 packet */
1544 /* Later, an NSSA Re-fresh can Re-fresh Type-7's and an ABR will
1545 translate them to a separate Type-5 packet. */
1547 if (lsa
->data
->type
== OSPF_AS_EXTERNAL_LSA
)
1548 /* Reject from STUB or NSSA */
1549 if (nbr
->oi
->area
->external_routing
!= OSPF_AREA_DEFAULT
)
1551 DISCARD_LSA (lsa
, 1);
1552 if (IS_DEBUG_OSPF_NSSA
)
1553 zlog_info("Incoming External LSA Discarded: We are NSSA/STUB Area");
1556 if (lsa
->data
->type
== OSPF_AS_NSSA_LSA
)
1557 if (nbr
->oi
->area
->external_routing
!= OSPF_AREA_NSSA
)
1559 DISCARD_LSA (lsa
,2);
1560 if (IS_DEBUG_OSPF_NSSA
)
1561 zlog_info("Incoming NSSA LSA Discarded: Not NSSA Area");
1564 /* Find the LSA in the current database. */
1566 current
= ospf_lsa_lookup_by_header (oi
->area
, lsa
->data
);
1568 /* If the LSA's LS age is equal to MaxAge, and there is currently
1569 no instance of the LSA in the router's link state database,
1570 and none of router's neighbors are in states Exchange or Loading,
1571 then take the following actions. */
1573 if (IS_LSA_MAXAGE (lsa
) && !current
&&
1574 (ospf_nbr_count (oi
, NSM_Exchange
) +
1575 ospf_nbr_count (oi
, NSM_Loading
)) == 0)
1577 /* Response Link State Acknowledgment. */
1578 ospf_ls_ack_send (nbr
, lsa
);
1581 zlog_warn ("Link State Update: LS age is equal to MaxAge.");
1582 DISCARD_LSA (lsa
, 3);
1585 #ifdef HAVE_OPAQUE_LSA
1586 if (IS_OPAQUE_LSA (lsa
->data
->type
)
1587 && IPV4_ADDR_SAME (&lsa
->data
->adv_router
, &oi
->ospf
->router_id
))
1590 * Even if initial flushing seems to be completed, there might
1591 * be a case that self-originated LSA with MaxAge still remain
1592 * in the routing domain.
1593 * Just send an LSAck message to cease retransmission.
1595 if (IS_LSA_MAXAGE (lsa
))
1597 zlog_warn ("LSA[%s]: Boomerang effect?", dump_lsa_key (lsa
));
1598 ospf_ls_ack_send (nbr
, lsa
);
1599 ospf_lsa_discard (lsa
);
1601 if (current
!= NULL
&& ! IS_LSA_MAXAGE (current
))
1602 ospf_opaque_lsa_refresh_schedule (current
);
1607 * If an instance of self-originated Opaque-LSA is not found
1608 * in the LSDB, there are some possible cases here.
1610 * 1) This node lost opaque-capability after restart.
1611 * 2) Else, a part of opaque-type is no more supported.
1612 * 3) Else, a part of opaque-id is no more supported.
1614 * Anyway, it is still this node's responsibility to flush it.
1615 * Otherwise, the LSA instance remains in the routing domain
1616 * until its age reaches to MaxAge.
1618 if (current
== NULL
)
1620 if (IS_DEBUG_OSPF_EVENT
)
1621 zlog_info ("LSA[%s]: Previously originated Opaque-LSA, not found in the LSDB.", dump_lsa_key (lsa
));
1623 SET_FLAG (lsa
->flags
, OSPF_LSA_SELF
);
1624 listnode_add (mylsa_upds
, ospf_lsa_dup (lsa
));
1625 listnode_add (mylsa_acks
, ospf_lsa_lock (lsa
));
1629 #endif /* HAVE_OPAQUE_LSA */
1630 /* It might be happen that received LSA is self-originated network LSA, but
1631 * router ID is cahnged. So, we should check if LSA is a network-LSA whose
1632 * Link State ID is one of the router's own IP interface addresses but whose
1633 * Advertising Router is not equal to the router's own Router ID
1634 * According to RFC 2328 12.4.2 and 13.4 this LSA should be flushed.
1637 if(lsa
->data
->type
== OSPF_NETWORK_LSA
)
1642 for(oi_node
= listhead(oi
->ospf
->oiflist
); oi_node
; oi_node
= nextnode(oi_node
))
1644 struct ospf_interface
*out_if
= getdata(oi_node
);
1648 if((IPV4_ADDR_SAME(&out_if
->address
->u
.prefix4
, &lsa
->data
->id
)) &&
1649 (!(IPV4_ADDR_SAME(&oi
->ospf
->router_id
, &lsa
->data
->adv_router
))))
1651 if(out_if
->network_lsa_self
)
1653 ospf_lsa_flush_area(lsa
,out_if
->area
);
1654 if(IS_DEBUG_OSPF_EVENT
)
1655 zlog_info ("ospf_lsa_discard() in ospf_ls_upd() point 9: lsa %p Type-%d",
1656 lsa
, (int) lsa
->data
->type
);
1657 ospf_lsa_discard (lsa
);
1667 /* (5) Find the instance of this LSA that is currently contained
1668 in the router's link state database. If there is no
1669 database copy, or the received LSA is more recent than
1670 the database copy the following steps must be performed. */
1672 if (current
== NULL
||
1673 (ret
= ospf_lsa_more_recent (current
, lsa
)) < 0)
1675 /* Actual flooding procedure. */
1676 if (ospf_flood (oi
->ospf
, nbr
, current
, lsa
) < 0) /* Trap NSSA later. */
1677 DISCARD_LSA (lsa
, 4);
1681 /* (6) Else, If there is an instance of the LSA on the sending
1682 neighbor's Link state request list, an error has occurred in
1683 the Database Exchange process. In this case, restart the
1684 Database Exchange process by generating the neighbor event
1685 BadLSReq for the sending neighbor and stop processing the
1686 Link State Update packet. */
1688 if (ospf_ls_request_lookup (nbr
, lsa
))
1690 OSPF_NSM_EVENT_SCHEDULE (nbr
, NSM_BadLSReq
);
1691 zlog_warn ("LSA instance exists on Link state request list");
1693 /* Clean list of LSAs. */
1694 ospf_upd_list_clean (lsas
);
1695 /* this lsa is not on lsas list already. */
1696 ospf_lsa_discard (lsa
);
1697 #ifdef HAVE_OPAQUE_LSA
1698 list_delete (mylsa_acks
);
1699 list_delete (mylsa_upds
);
1700 #endif /* HAVE_OPAQUE_LSA */
1704 /* If the received LSA is the same instance as the database copy
1705 (i.e., neither one is more recent) the following two steps
1706 should be performed: */
1710 /* If the LSA is listed in the Link state retransmission list
1711 for the receiving adjacency, the router itself is expecting
1712 an acknowledgment for this LSA. The router should treat the
1713 received LSA as an acknowledgment by removing the LSA from
1714 the Link state retransmission list. This is termed an
1715 "implied acknowledgment". */
1717 ls_ret
= ospf_ls_retransmit_lookup (nbr
, lsa
);
1721 ospf_ls_retransmit_delete (nbr
, ls_ret
);
1723 /* Delayed acknowledgment sent if advertisement received
1724 from Designated Router, otherwise do nothing. */
1725 if (oi
->state
== ISM_Backup
)
1726 if (NBR_IS_DR (nbr
))
1727 listnode_add (oi
->ls_ack
, ospf_lsa_lock (lsa
));
1729 DISCARD_LSA (lsa
, 5);
1732 /* Acknowledge the receipt of the LSA by sending a
1733 Link State Acknowledgment packet back out the receiving
1736 ospf_ls_ack_send (nbr
, lsa
);
1737 DISCARD_LSA (lsa
, 6);
1741 /* The database copy is more recent. If the database copy
1742 has LS age equal to MaxAge and LS sequence number equal to
1743 MaxSequenceNumber, simply discard the received LSA without
1744 acknowledging it. (In this case, the LSA's LS sequence number is
1745 wrapping, and the MaxSequenceNumber LSA must be completely
1746 flushed before any new LSA instance can be introduced). */
1748 else if (ret
> 0) /* Database copy is more recent */
1750 if (IS_LSA_MAXAGE (current
) &&
1751 current
->data
->ls_seqnum
== htonl (OSPF_MAX_SEQUENCE_NUMBER
))
1753 DISCARD_LSA (lsa
, 7);
1755 /* Otherwise, as long as the database copy has not been sent in a
1756 Link State Update within the last MinLSArrival seconds, send the
1757 database copy back to the sending neighbor, encapsulated within
1758 a Link State Update Packet. The Link State Update Packet should
1759 be sent directly to the neighbor. In so doing, do not put the
1760 database copy of the LSA on the neighbor's link state
1761 retransmission list, and do not acknowledge the received (less
1762 recent) LSA instance. */
1767 gettimeofday (&now
, NULL
);
1769 if (tv_cmp (tv_sub (now
, current
->tv_orig
),
1770 int2tv (OSPF_MIN_LS_ARRIVAL
)) > 0)
1771 /* Trap NSSA type later.*/
1772 ospf_ls_upd_send_lsa (nbr
, current
, OSPF_SEND_PACKET_DIRECT
);
1773 DISCARD_LSA (lsa
, 8);
1778 #ifdef HAVE_OPAQUE_LSA
1780 * Now that previously originated Opaque-LSAs those which not yet
1781 * installed into LSDB are captured, take several steps to clear
1782 * them completely from the routing domain, before proceeding to
1783 * origination for the current target Opaque-LSAs.
1785 while (listcount (mylsa_acks
) > 0)
1786 ospf_ls_ack_send_list (oi
, mylsa_acks
, nbr
->address
.u
.prefix4
);
1788 if (listcount (mylsa_upds
) > 0)
1789 ospf_opaque_self_originated_lsa_received (nbr
, mylsa_upds
);
1791 list_delete (mylsa_upds
);
1792 list_delete (mylsa_acks
);
1793 #endif /* HAVE_OPAQUE_LSA */
1795 assert (listcount (lsas
) == 0);
1799 /* OSPF Link State Acknowledgment message read -- RFC2328 Section 13.7. */
1801 ospf_ls_ack (struct ip
*iph
, struct ospf_header
*ospfh
,
1802 struct stream
*s
, struct ospf_interface
*oi
, u_int16_t size
)
1804 struct ospf_neighbor
*nbr
;
1805 #ifdef HAVE_OPAQUE_LSA
1807 #endif /* HAVE_OPAQUE_LSA */
1809 /* increment statistics. */
1812 nbr
= ospf_nbr_lookup (oi
, iph
, ospfh
);
1815 zlog_warn ("Link State Acknowledgment: Unknown Neighbor %s.",
1816 inet_ntoa (ospfh
->router_id
));
1820 if (nbr
->state
< NSM_Exchange
)
1822 zlog_warn ("Link State Acknowledgment: State is less than Exchange.");
1826 #ifdef HAVE_OPAQUE_LSA
1827 opaque_acks
= list_new ();
1828 #endif /* HAVE_OPAQUE_LSA */
1830 while (size
>= OSPF_LSA_HEADER_SIZE
)
1832 struct ospf_lsa
*lsa
, *lsr
;
1834 lsa
= ospf_lsa_new ();
1835 lsa
->data
= (struct lsa_header
*) STREAM_PNT (s
);
1837 /* lsah = (struct lsa_header *) STREAM_PNT (s); */
1838 size
-= OSPF_LSA_HEADER_SIZE
;
1839 stream_forward (s
, OSPF_LSA_HEADER_SIZE
);
1841 if (lsa
->data
->type
< OSPF_MIN_LSA
|| lsa
->data
->type
>= OSPF_MAX_LSA
)
1844 ospf_lsa_discard (lsa
);
1848 lsr
= ospf_ls_retransmit_lookup (nbr
, lsa
);
1850 if (lsr
!= NULL
&& lsr
->data
->ls_seqnum
== lsa
->data
->ls_seqnum
)
1852 #ifdef HAVE_OPAQUE_LSA
1853 /* Keep this LSA entry for later reference. */
1854 if (IS_OPAQUE_LSA (lsr
->data
->type
))
1855 listnode_add (opaque_acks
, ospf_lsa_dup (lsr
));
1856 #endif /* HAVE_OPAQUE_LSA */
1858 ospf_ls_retransmit_delete (nbr
, lsr
);
1862 ospf_lsa_discard (lsa
);
1865 #ifdef HAVE_OPAQUE_LSA
1866 if (listcount (opaque_acks
) > 0)
1867 ospf_opaque_ls_ack_received (nbr
, opaque_acks
);
1869 list_delete (opaque_acks
);
1871 #endif /* HAVE_OPAQUE_LSA */
1875 ospf_recv_packet (int fd
, struct interface
**ifp
)
1880 struct stream
*ibuf
;
1881 unsigned int ifindex
= 0;
1883 struct cmsghdr
*cmsg
;
1884 #if defined(CMSG_SPACE)
1885 /* Header and data both require alignment. */
1886 char buff
[CMSG_SPACE(SOPT_SIZE_CMSG_IFINDEX_IPV4())];
1888 char buff
[sizeof (*cmsg
) + SOPT_SIZE_CMSG_IFINDEX_IPV4()];
1892 msgh
.msg_name
= NULL
;
1893 msgh
.msg_namelen
= 0;
1894 msgh
.msg_iov
= &iov
;
1895 msgh
.msg_iovlen
= 1;
1896 msgh
.msg_control
= (caddr_t
) buff
;
1897 msgh
.msg_controllen
= sizeof (buff
);
1900 ret
= recvfrom (fd
, (void *)&iph
, sizeof (iph
), MSG_PEEK
, NULL
, 0);
1902 if (ret
!= sizeof (iph
))
1904 zlog_warn ("ospf_recv_packet packet smaller than ip header");
1908 #if defined(__NetBSD__) || defined(__FreeBSD__) || (defined(__OpenBSD__) && (OpenBSD < 200311))
1909 ip_len
= iph
.ip_len
;
1911 ip_len
= ntohs (iph
.ip_len
);
1914 #if !defined(GNU_LINUX) && (OpenBSD < 200311)
1916 * Kernel network code touches incoming IP header parameters,
1917 * before protocol specific processing.
1919 * 1) Convert byteorder to host representation.
1920 * --> ip_len, ip_id, ip_off
1922 * 2) Adjust ip_len to strip IP header size!
1923 * --> If user process receives entire IP packet via RAW
1924 * socket, it must consider adding IP header size to
1925 * the "ip_len" field of "ip" structure.
1927 * For more details, see <netinet/ip_input.c>.
1929 ip_len
= ip_len
+ (iph
.ip_hl
<< 2);
1932 ibuf
= stream_new (ip_len
);
1933 iov
.iov_base
= STREAM_DATA (ibuf
);
1934 iov
.iov_len
= ip_len
;
1935 ret
= recvmsg (fd
, &msgh
, 0);
1937 ifindex
= getsockopt_ifindex (AF_INET
, &msgh
);
1939 *ifp
= if_lookup_by_index (ifindex
);
1943 zlog_warn ("ospf_recv_packet short read. "
1944 "ip_len %d bytes read %d", ip_len
, ret
);
1952 struct ospf_interface
*
1953 ospf_associate_packet_vl (struct ospf
*ospf
, struct interface
*ifp
,
1954 struct ip
*iph
, struct ospf_header
*ospfh
)
1956 struct ospf_interface
*rcv_oi
;
1957 struct ospf_vl_data
*vl_data
;
1958 struct ospf_area
*vl_area
;
1961 if (IN_MULTICAST (ntohl (iph
->ip_dst
.s_addr
)) ||
1962 !OSPF_IS_AREA_BACKBONE (ospfh
))
1965 /* look for local OSPF interface matching the destination
1966 * to determine Area ID. We presume therefore the destination address
1967 * is unique, or at least (for "unnumbered" links), not used in other
1970 if ((rcv_oi
= ospf_if_lookup_by_local_addr (ospf
, NULL
,
1971 iph
->ip_dst
)) == NULL
)
1974 for (node
= listhead (ospf
->vlinks
); node
; nextnode (node
))
1976 if ((vl_data
= getdata (node
)) == NULL
)
1979 vl_area
= ospf_area_lookup_by_area_id (ospf
, vl_data
->vl_area_id
);
1983 if (OSPF_AREA_SAME (&vl_area
, &rcv_oi
->area
) &&
1984 IPV4_ADDR_SAME (&vl_data
->vl_peer
, &ospfh
->router_id
))
1986 if (IS_DEBUG_OSPF_EVENT
)
1987 zlog_info ("associating packet with %s",
1988 IF_NAME (vl_data
->vl_oi
));
1989 if (! CHECK_FLAG (vl_data
->vl_oi
->ifp
->flags
, IFF_UP
))
1991 if (IS_DEBUG_OSPF_EVENT
)
1992 zlog_info ("This VL is not up yet, sorry");
1996 return vl_data
->vl_oi
;
2000 if (IS_DEBUG_OSPF_EVENT
)
2001 zlog_info ("couldn't find any VL to associate the packet with");
2007 ospf_check_area_id (struct ospf_interface
*oi
, struct ospf_header
*ospfh
)
2009 /* Check match the Area ID of the receiving interface. */
2010 if (OSPF_AREA_SAME (&oi
->area
, &ospfh
))
2016 /* Unbound socket will accept any Raw IP packets if proto is matched.
2017 To prevent it, compare src IP address and i/f address with masking
2018 i/f network mask. */
2020 ospf_check_network_mask (struct ospf_interface
*oi
, struct in_addr ip_src
)
2022 struct in_addr mask
, me
, him
;
2024 if (oi
->type
== OSPF_IFTYPE_POINTOPOINT
||
2025 oi
->type
== OSPF_IFTYPE_VIRTUALLINK
)
2028 masklen2ip (oi
->address
->prefixlen
, &mask
);
2030 me
.s_addr
= oi
->address
->u
.prefix4
.s_addr
& mask
.s_addr
;
2031 him
.s_addr
= ip_src
.s_addr
& mask
.s_addr
;
2033 if (IPV4_ADDR_SAME (&me
, &him
))
2040 ospf_check_auth (struct ospf_interface
*oi
, struct stream
*ibuf
,
2041 struct ospf_header
*ospfh
)
2044 struct crypt_key
*ck
;
2046 switch (ntohs (ospfh
->auth_type
))
2048 case OSPF_AUTH_NULL
:
2051 case OSPF_AUTH_SIMPLE
:
2052 if (!memcmp (OSPF_IF_PARAM (oi
, auth_simple
), ospfh
->u
.auth_data
, OSPF_AUTH_SIMPLE_SIZE
))
2057 case OSPF_AUTH_CRYPTOGRAPHIC
:
2058 if ((ck
= getdata (OSPF_IF_PARAM (oi
,auth_crypt
)->tail
)) == NULL
)
2064 /* This is very basic, the digest processing is elsewhere */
2065 if (ospfh
->u
.crypt
.auth_data_len
== OSPF_AUTH_MD5_SIZE
&&
2066 ospfh
->u
.crypt
.key_id
== ck
->key_id
&&
2067 ntohs (ospfh
->length
) + OSPF_AUTH_SIMPLE_SIZE
<= stream_get_size (ibuf
))
2081 ospf_check_sum (struct ospf_header
*ospfh
)
2085 int in_cksum (void *ptr
, int nbytes
);
2087 /* clear auth_data for checksum. */
2088 memset (ospfh
->u
.auth_data
, 0, OSPF_AUTH_SIMPLE_SIZE
);
2090 /* keep checksum and clear. */
2091 sum
= ospfh
->checksum
;
2092 memset (&ospfh
->checksum
, 0, sizeof (u_int16_t
));
2094 /* calculate checksum. */
2095 ret
= in_cksum (ospfh
, ntohs (ospfh
->length
));
2099 zlog_info ("ospf_check_sum(): checksum mismatch, my %X, his %X",
2107 /* OSPF Header verification. */
2109 ospf_verify_header (struct stream
*ibuf
, struct ospf_interface
*oi
,
2110 struct ip
*iph
, struct ospf_header
*ospfh
)
2112 /* check version. */
2113 if (ospfh
->version
!= OSPF_VERSION
)
2115 zlog_warn ("interface %s: ospf_read version number mismatch.",
2120 /* Check Area ID. */
2121 if (!ospf_check_area_id (oi
, ospfh
))
2123 zlog_warn ("interface %s: ospf_read invalid Area ID %s.",
2124 IF_NAME (oi
), inet_ntoa (ospfh
->area_id
));
2128 /* Check network mask, Silently discarded. */
2129 if (! ospf_check_network_mask (oi
, iph
->ip_src
))
2131 zlog_warn ("interface %s: ospf_read network address is not same [%s]",
2132 IF_NAME (oi
), inet_ntoa (iph
->ip_src
));
2136 /* Check authentication. */
2137 if (ospf_auth_type (oi
) != ntohs (ospfh
->auth_type
))
2139 zlog_warn ("interface %s: ospf_read authentication type mismatch.",
2144 if (! ospf_check_auth (oi
, ibuf
, ospfh
))
2146 zlog_warn ("interface %s: ospf_read authentication failed.",
2151 /* if check sum is invalid, packet is discarded. */
2152 if (ntohs (ospfh
->auth_type
) != OSPF_AUTH_CRYPTOGRAPHIC
)
2154 if (! ospf_check_sum (ospfh
))
2156 zlog_warn ("interface %s: ospf_read packet checksum error %s",
2157 IF_NAME (oi
), inet_ntoa (ospfh
->router_id
));
2163 if (ospfh
->checksum
!= 0)
2165 if (ospf_check_md5_digest (oi
, ibuf
, ntohs (ospfh
->length
)) == 0)
2167 zlog_warn ("interface %s: ospf_read md5 authentication failed.",
2176 /* Starting point of packet process function. */
2178 ospf_read (struct thread
*thread
)
2181 struct stream
*ibuf
;
2183 struct ospf_interface
*oi
;
2185 struct ospf_header
*ospfh
;
2187 struct interface
*ifp
;
2189 /* first of all get interface pointer. */
2190 ospf
= THREAD_ARG (thread
);
2191 ospf
->t_read
= NULL
;
2193 /* read OSPF packet. */
2194 ibuf
= ospf_recv_packet (ospf
->fd
, &ifp
);
2204 iph
= (struct ip
*) STREAM_DATA (ibuf
);
2206 /* prepare for next packet. */
2207 ospf
->t_read
= thread_add_read (master
, ospf_read
, ospf
, ospf
->fd
);
2209 /* IP Header dump. */
2210 if (IS_DEBUG_OSPF_PACKET(0, RECV
))
2211 ospf_ip_header_dump (ibuf
);
2213 /* Self-originated packet should be discarded silently. */
2214 if (ospf_if_lookup_by_local_addr (ospf
, NULL
, iph
->ip_src
))
2216 if (IS_DEBUG_OSPF_PACKET (0, RECV
))
2218 zlog_info ("ospf_read[%s]: Dropping self-originated packet",
2219 inet_ntoa (iph
->ip_src
));
2225 /* Adjust size to message length. */
2226 stream_forward (ibuf
, iph
->ip_hl
* 4);
2228 /* Get ospf packet header. */
2229 ospfh
= (struct ospf_header
*) STREAM_PNT (ibuf
);
2231 /* associate packet with ospf interface */
2232 oi
= ospf_if_lookup_recv_if (ospf
, iph
->ip_src
);
2234 /* if no local ospf_interface,
2235 * or header area is backbone but ospf_interface is not
2236 * check for VLINK interface
2238 if ( (oi
== NULL
) ||
2239 (OSPF_IS_AREA_ID_BACKBONE(ospfh
->area_id
)
2240 && !OSPF_IS_AREA_ID_BACKBONE(oi
->area
->area_id
))
2243 if ((oi
= ospf_associate_packet_vl (ospf
, ifp
, iph
, ospfh
)) == NULL
)
2245 zlog_warn ("Packet from [%s] received on link %s"
2246 " but no ospf_interface",
2247 inet_ntoa (iph
->ip_src
), ifp
->name
);
2253 /* else it must be a local ospf interface, check it was received on
2256 else if (oi
->ifp
!= ifp
)
2258 zlog_warn ("Packet from [%s] received on wrong link %s",
2259 inet_ntoa (iph
->ip_src
), ifp
->name
);
2265 * If the received packet is destined for AllDRouters, the packet
2266 * should be accepted only if the received ospf interface state is
2267 * either DR or Backup -- endo.
2269 if (iph
->ip_dst
.s_addr
== htonl (OSPF_ALLDROUTERS
)
2270 && (oi
->state
!= ISM_DR
&& oi
->state
!= ISM_Backup
))
2272 zlog_info ("Packet for AllDRouters from [%s] via [%s] (ISM: %s)",
2273 inet_ntoa (iph
->ip_src
), IF_NAME (oi
),
2274 LOOKUP (ospf_ism_state_msg
, oi
->state
));
2279 /* Show debug receiving packet. */
2280 if (IS_DEBUG_OSPF_PACKET (ospfh
->type
- 1, RECV
))
2282 if (IS_DEBUG_OSPF_PACKET (ospfh
->type
- 1, DETAIL
))
2284 zlog_info ("-----------------------------------------------------");
2285 ospf_packet_dump (ibuf
);
2288 zlog_info ("%s received from [%s] via [%s]",
2289 ospf_packet_type_str
[ospfh
->type
],
2290 inet_ntoa (ospfh
->router_id
), IF_NAME (oi
));
2291 zlog_info (" src [%s],", inet_ntoa (iph
->ip_src
));
2292 zlog_info (" dst [%s]", inet_ntoa (iph
->ip_dst
));
2294 if (IS_DEBUG_OSPF_PACKET (ospfh
->type
- 1, DETAIL
))
2295 zlog_info ("-----------------------------------------------------");
2298 /* Some header verification. */
2299 ret
= ospf_verify_header (ibuf
, oi
, iph
, ospfh
);
2302 if (IS_DEBUG_OSPF_PACKET (ospfh
->type
- 1, RECV
))
2304 zlog_info ("ospf_read[%s/%s]: Header check failed, "
2306 ospf_packet_type_str
[ospfh
->type
],
2307 inet_ntoa (iph
->ip_src
));
2313 stream_forward (ibuf
, OSPF_HEADER_SIZE
);
2315 /* Adjust size to message length. */
2316 length
= ntohs (ospfh
->length
) - OSPF_HEADER_SIZE
;
2318 /* Read rest of the packet and call each sort of packet routine. */
2319 switch (ospfh
->type
)
2321 case OSPF_MSG_HELLO
:
2322 ospf_hello (iph
, ospfh
, ibuf
, oi
, length
);
2324 case OSPF_MSG_DB_DESC
:
2325 ospf_db_desc (iph
, ospfh
, ibuf
, oi
, length
);
2327 case OSPF_MSG_LS_REQ
:
2328 ospf_ls_req (iph
, ospfh
, ibuf
, oi
, length
);
2330 case OSPF_MSG_LS_UPD
:
2331 ospf_ls_upd (iph
, ospfh
, ibuf
, oi
, length
);
2333 case OSPF_MSG_LS_ACK
:
2334 ospf_ls_ack (iph
, ospfh
, ibuf
, oi
, length
);
2337 zlog (NULL
, LOG_WARNING
,
2338 "interface %s: OSPF packet header type %d is illegal",
2339 IF_NAME (oi
), ospfh
->type
);
2347 /* Make OSPF header. */
2349 ospf_make_header (int type
, struct ospf_interface
*oi
, struct stream
*s
)
2351 struct ospf_header
*ospfh
;
2353 ospfh
= (struct ospf_header
*) STREAM_DATA (s
);
2355 ospfh
->version
= (u_char
) OSPF_VERSION
;
2356 ospfh
->type
= (u_char
) type
;
2358 ospfh
->router_id
= oi
->ospf
->router_id
;
2360 ospfh
->checksum
= 0;
2361 ospfh
->area_id
= oi
->area
->area_id
;
2362 ospfh
->auth_type
= htons (ospf_auth_type (oi
));
2364 memset (ospfh
->u
.auth_data
, 0, OSPF_AUTH_SIMPLE_SIZE
);
2366 ospf_output_forward (s
, OSPF_HEADER_SIZE
);
2369 /* Make Authentication Data. */
2371 ospf_make_auth (struct ospf_interface
*oi
, struct ospf_header
*ospfh
)
2373 struct crypt_key
*ck
;
2375 switch (ospf_auth_type (oi
))
2377 case OSPF_AUTH_NULL
:
2378 /* memset (ospfh->u.auth_data, 0, sizeof (ospfh->u.auth_data)); */
2380 case OSPF_AUTH_SIMPLE
:
2381 memcpy (ospfh
->u
.auth_data
, OSPF_IF_PARAM (oi
, auth_simple
),
2382 OSPF_AUTH_SIMPLE_SIZE
);
2384 case OSPF_AUTH_CRYPTOGRAPHIC
:
2385 /* If key is not set, then set 0. */
2386 if (list_isempty (OSPF_IF_PARAM (oi
, auth_crypt
)))
2388 ospfh
->u
.crypt
.zero
= 0;
2389 ospfh
->u
.crypt
.key_id
= 0;
2390 ospfh
->u
.crypt
.auth_data_len
= OSPF_AUTH_MD5_SIZE
;
2394 ck
= getdata (OSPF_IF_PARAM (oi
, auth_crypt
)->tail
);
2395 ospfh
->u
.crypt
.zero
= 0;
2396 ospfh
->u
.crypt
.key_id
= ck
->key_id
;
2397 ospfh
->u
.crypt
.auth_data_len
= OSPF_AUTH_MD5_SIZE
;
2399 /* note: the seq is done in ospf_make_md5_digest() */
2402 /* memset (ospfh->u.auth_data, 0, sizeof (ospfh->u.auth_data)); */
2409 /* Fill rest of OSPF header. */
2411 ospf_fill_header (struct ospf_interface
*oi
,
2412 struct stream
*s
, u_int16_t length
)
2414 struct ospf_header
*ospfh
;
2416 ospfh
= (struct ospf_header
*) STREAM_DATA (s
);
2419 ospfh
->length
= htons (length
);
2421 /* Calculate checksum. */
2422 if (ntohs (ospfh
->auth_type
) != OSPF_AUTH_CRYPTOGRAPHIC
)
2423 ospfh
->checksum
= in_cksum (ospfh
, length
);
2425 ospfh
->checksum
= 0;
2427 /* Add Authentication Data. */
2428 ospf_make_auth (oi
, ospfh
);
2432 ospf_make_hello (struct ospf_interface
*oi
, struct stream
*s
)
2434 struct ospf_neighbor
*nbr
;
2435 struct route_node
*rn
;
2436 u_int16_t length
= OSPF_HELLO_MIN_SIZE
;
2437 struct in_addr mask
;
2441 /* Set netmask of interface. */
2442 if (oi
->type
!= OSPF_IFTYPE_POINTOPOINT
&&
2443 oi
->type
!= OSPF_IFTYPE_VIRTUALLINK
)
2444 masklen2ip (oi
->address
->prefixlen
, &mask
);
2446 memset ((char *) &mask
, 0, sizeof (struct in_addr
));
2447 stream_put_ipv4 (s
, mask
.s_addr
);
2449 /* Set Hello Interval. */
2450 stream_putw (s
, OSPF_IF_PARAM (oi
, v_hello
));
2452 if (IS_DEBUG_OSPF_EVENT
)
2453 zlog_info ("make_hello: options: %x, int: %s",
2454 OPTIONS(oi
), IF_NAME (oi
));
2457 stream_putc (s
, OPTIONS (oi
));
2459 /* Set Router Priority. */
2460 stream_putc (s
, PRIORITY (oi
));
2462 /* Set Router Dead Interval. */
2463 stream_putl (s
, OSPF_IF_PARAM (oi
, v_wait
));
2465 /* Set Designated Router. */
2466 stream_put_ipv4 (s
, DR (oi
).s_addr
);
2470 /* Set Backup Designated Router. */
2471 stream_put_ipv4 (s
, BDR (oi
).s_addr
);
2473 /* Add neighbor seen. */
2474 for (rn
= route_top (oi
->nbrs
); rn
; rn
= route_next (rn
))
2475 if ((nbr
= rn
->info
))
2476 if (nbr
->router_id
.s_addr
!= 0) /* Ignore 0.0.0.0 node. */
2477 if (nbr
->state
!= NSM_Attempt
) /* Ignore Down neighbor. */
2478 if (nbr
->state
!= NSM_Down
) /* This is myself for DR election. */
2479 if (!IPV4_ADDR_SAME (&nbr
->router_id
, &oi
->ospf
->router_id
))
2481 /* Check neighbor is sane? */
2482 if (nbr
->d_router
.s_addr
!= 0
2483 && IPV4_ADDR_SAME (&nbr
->d_router
, &oi
->address
->u
.prefix4
)
2484 && IPV4_ADDR_SAME (&nbr
->bd_router
, &oi
->address
->u
.prefix4
))
2487 stream_put_ipv4 (s
, nbr
->router_id
.s_addr
);
2491 /* Let neighbor generate BackupSeen. */
2494 stream_set_putp (s
, p
);
2495 stream_put_ipv4 (s
, 0);
2502 ospf_make_db_desc (struct ospf_interface
*oi
, struct ospf_neighbor
*nbr
,
2505 struct ospf_lsa
*lsa
;
2506 u_int16_t length
= OSPF_DB_DESC_MIN_SIZE
;
2510 struct ospf_lsdb
*lsdb
;
2512 /* Set Interface MTU. */
2513 if (oi
->type
== OSPF_IFTYPE_VIRTUALLINK
)
2516 stream_putw (s
, oi
->ifp
->mtu
);
2519 options
= OPTIONS (oi
);
2520 #ifdef HAVE_OPAQUE_LSA
2521 if (CHECK_FLAG (oi
->ospf
->config
, OSPF_OPAQUE_CAPABLE
))
2523 if (IS_SET_DD_I (nbr
->dd_flags
)
2524 || CHECK_FLAG (nbr
->options
, OSPF_OPTION_O
))
2526 * Set O-bit in the outgoing DD packet for capablity negotiation,
2527 * if one of following case is applicable.
2529 * 1) WaitTimer expiration event triggered the neighbor state to
2530 * change to Exstart, but no (valid) DD packet has received
2531 * from the neighbor yet.
2533 * 2) At least one DD packet with O-bit on has received from the
2536 SET_FLAG (options
, OSPF_OPTION_O
);
2538 #endif /* HAVE_OPAQUE_LSA */
2539 stream_putc (s
, options
);
2541 /* Keep pointer to flags. */
2542 pp
= stream_get_putp (s
);
2543 stream_putc (s
, nbr
->dd_flags
);
2545 /* Set DD Sequence Number. */
2546 stream_putl (s
, nbr
->dd_seqnum
);
2548 if (ospf_db_summary_isempty (nbr
))
2550 if (nbr
->state
>= NSM_Exchange
)
2552 nbr
->dd_flags
&= ~OSPF_DD_FLAG_M
;
2553 /* Set DD flags again */
2554 stream_set_putp (s
, pp
);
2555 stream_putc (s
, nbr
->dd_flags
);
2560 /* Describe LSA Header from Database Summary List. */
2561 lsdb
= &nbr
->db_sum
;
2563 for (i
= OSPF_MIN_LSA
; i
< OSPF_MAX_LSA
; i
++)
2565 struct route_table
*table
= lsdb
->type
[i
].db
;
2566 struct route_node
*rn
;
2568 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
2569 if ((lsa
= rn
->info
) != NULL
)
2571 #ifdef HAVE_OPAQUE_LSA
2572 if (IS_OPAQUE_LSA (lsa
->data
->type
)
2573 && (! CHECK_FLAG (options
, OSPF_OPTION_O
)))
2575 /* Suppress advertising opaque-informations. */
2576 /* Remove LSA from DB summary list. */
2577 ospf_lsdb_delete (lsdb
, lsa
);
2580 #endif /* HAVE_OPAQUE_LSA */
2582 if (!CHECK_FLAG (lsa
->flags
, OSPF_LSA_DISCARD
))
2584 struct lsa_header
*lsah
;
2587 /* DD packet overflows interface MTU. */
2588 if (length
+ OSPF_LSA_HEADER_SIZE
> OSPF_PACKET_MAX (oi
))
2591 /* Keep pointer to LS age. */
2592 lsah
= (struct lsa_header
*) (STREAM_DATA (s
) +
2593 stream_get_putp (s
));
2595 /* Proceed stream pointer. */
2596 stream_put (s
, lsa
->data
, OSPF_LSA_HEADER_SIZE
);
2597 length
+= OSPF_LSA_HEADER_SIZE
;
2600 ls_age
= LS_AGE (lsa
);
2601 lsah
->ls_age
= htons (ls_age
);
2605 /* Remove LSA from DB summary list. */
2606 ospf_lsdb_delete (lsdb
, lsa
);
2614 ospf_make_ls_req_func (struct stream
*s
, u_int16_t
*length
,
2615 unsigned long delta
, struct ospf_neighbor
*nbr
,
2616 struct ospf_lsa
*lsa
)
2618 struct ospf_interface
*oi
;
2622 /* LS Request packet overflows interface MTU. */
2623 if (*length
+ delta
> OSPF_PACKET_MAX(oi
))
2626 stream_putl (s
, lsa
->data
->type
);
2627 stream_put_ipv4 (s
, lsa
->data
->id
.s_addr
);
2628 stream_put_ipv4 (s
, lsa
->data
->adv_router
.s_addr
);
2630 ospf_lsa_unlock (nbr
->ls_req_last
);
2631 nbr
->ls_req_last
= ospf_lsa_lock (lsa
);
2638 ospf_make_ls_req (struct ospf_neighbor
*nbr
, struct stream
*s
)
2640 struct ospf_lsa
*lsa
;
2641 u_int16_t length
= OSPF_LS_REQ_MIN_SIZE
;
2642 unsigned long delta
= stream_get_putp(s
)+12;
2643 struct route_table
*table
;
2644 struct route_node
*rn
;
2646 struct ospf_lsdb
*lsdb
;
2648 lsdb
= &nbr
->ls_req
;
2650 for (i
= OSPF_MIN_LSA
; i
< OSPF_MAX_LSA
; i
++)
2652 table
= lsdb
->type
[i
].db
;
2653 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
2654 if ((lsa
= (rn
->info
)) != NULL
)
2655 if (ospf_make_ls_req_func (s
, &length
, delta
, nbr
, lsa
) == 0)
2657 route_unlock_node (rn
);
2665 ls_age_increment (struct ospf_lsa
*lsa
, int delay
)
2669 age
= IS_LSA_MAXAGE (lsa
) ? OSPF_LSA_MAXAGE
: LS_AGE (lsa
) + delay
;
2671 return (age
> OSPF_LSA_MAXAGE
? OSPF_LSA_MAXAGE
: age
);
2675 ospf_make_ls_upd (struct ospf_interface
*oi
, list update
, struct stream
*s
)
2677 struct ospf_lsa
*lsa
;
2679 u_int16_t length
= OSPF_LS_UPD_MIN_SIZE
;
2680 unsigned long delta
= stream_get_putp (s
);
2684 if (IS_DEBUG_OSPF_EVENT
)
2685 zlog_info ("ospf_make_ls_upd: Start");
2687 pp
= stream_get_putp (s
);
2688 ospf_output_forward (s
, 4);
2690 while ((node
= listhead (update
)) != NULL
)
2692 struct lsa_header
*lsah
;
2695 if (IS_DEBUG_OSPF_EVENT
)
2696 zlog_info ("ospf_make_ls_upd: List Iteration");
2698 lsa
= getdata (node
);
2702 /* Check packet size. */
2703 /* XXX: LSA can be > packet-headers, eg router-lsas for machines
2704 * with hundreds of interfaces, received as several
2705 * fragmented packets.
2707 if (length
+ delta
+ ntohs (lsa
->data
->length
) > OSPF_PACKET_MAX (oi
))
2710 /* Keep pointer to LS age. */
2711 lsah
= (struct lsa_header
*) (STREAM_DATA (s
) + stream_get_putp (s
));
2713 /* Put LSA to Link State Request. */
2714 stream_put (s
, lsa
->data
, ntohs (lsa
->data
->length
));
2717 /* each hop must increment an lsa_age by transmit_delay
2718 of OSPF interface */
2719 ls_age
= ls_age_increment (lsa
, OSPF_IF_PARAM (oi
, transmit_delay
));
2720 lsah
->ls_age
= htons (ls_age
);
2722 length
+= ntohs (lsa
->data
->length
);
2725 list_delete_node (update
, node
);
2726 ospf_lsa_unlock (lsa
);
2729 /* Now set #LSAs. */
2730 stream_set_putp (s
, pp
);
2731 stream_putl (s
, count
);
2733 stream_set_putp (s
, s
->endp
);
2735 if (IS_DEBUG_OSPF_EVENT
)
2736 zlog_info ("ospf_make_ls_upd: Stop");
2741 ospf_make_ls_ack (struct ospf_interface
*oi
, list ack
, struct stream
*s
)
2745 u_int16_t length
= OSPF_LS_ACK_MIN_SIZE
;
2746 unsigned long delta
= stream_get_putp(s
) + 24;
2747 struct ospf_lsa
*lsa
;
2749 rm_list
= list_new ();
2751 for (node
= listhead (ack
); node
; nextnode (node
))
2753 lsa
= getdata (node
);
2756 if (length
+ delta
> OSPF_PACKET_MAX (oi
))
2759 stream_put (s
, lsa
->data
, OSPF_LSA_HEADER_SIZE
);
2760 length
+= OSPF_LSA_HEADER_SIZE
;
2762 listnode_add (rm_list
, lsa
);
2765 /* Remove LSA from LS-Ack list. */
2766 for (node
= listhead (rm_list
); node
; nextnode (node
))
2768 lsa
= (struct ospf_lsa
*) getdata (node
);
2770 listnode_delete (ack
, lsa
);
2771 ospf_lsa_unlock (lsa
);
2774 list_delete (rm_list
);
2780 ospf_hello_send_sub (struct ospf_interface
*oi
, struct in_addr
*addr
)
2782 struct ospf_packet
*op
;
2783 u_int16_t length
= OSPF_HEADER_SIZE
;
2785 op
= ospf_packet_new (oi
->ifp
->mtu
);
2787 /* Prepare OSPF common header. */
2788 ospf_make_header (OSPF_MSG_HELLO
, oi
, op
->s
);
2790 /* Prepare OSPF Hello body. */
2791 length
+= ospf_make_hello (oi
, op
->s
);
2793 /* Fill OSPF header. */
2794 ospf_fill_header (oi
, op
->s
, length
);
2796 /* Set packet length. */
2797 op
->length
= length
;
2799 op
->dst
.s_addr
= addr
->s_addr
;
2801 /* Add packet to the interface output queue. */
2802 ospf_packet_add (oi
, op
);
2804 /* Hook thread to write packet. */
2805 OSPF_ISM_WRITE_ON (oi
->ospf
);
2809 ospf_poll_send (struct ospf_nbr_nbma
*nbr_nbma
)
2811 struct ospf_interface
*oi
;
2816 /* If this is passive interface, do not send OSPF Hello. */
2817 if (OSPF_IF_PARAM (oi
, passive_interface
) == OSPF_IF_PASSIVE
)
2820 if (oi
->type
!= OSPF_IFTYPE_NBMA
)
2823 if (nbr_nbma
->nbr
!= NULL
&& nbr_nbma
->nbr
->state
!= NSM_Down
)
2826 if (PRIORITY(oi
) == 0)
2829 if (nbr_nbma
->priority
== 0
2830 && oi
->state
!= ISM_DR
&& oi
->state
!= ISM_Backup
)
2833 ospf_hello_send_sub (oi
, &nbr_nbma
->addr
);
2837 ospf_poll_timer (struct thread
*thread
)
2839 struct ospf_nbr_nbma
*nbr_nbma
;
2841 nbr_nbma
= THREAD_ARG (thread
);
2842 nbr_nbma
->t_poll
= NULL
;
2844 if (IS_DEBUG_OSPF (nsm
, NSM_TIMERS
))
2845 zlog (NULL
, LOG_INFO
, "NSM[%s:%s]: Timer (Poll timer expire)",
2846 IF_NAME (nbr_nbma
->oi
), inet_ntoa (nbr_nbma
->addr
));
2848 ospf_poll_send (nbr_nbma
);
2850 if (nbr_nbma
->v_poll
> 0)
2851 OSPF_POLL_TIMER_ON (nbr_nbma
->t_poll
, ospf_poll_timer
,
2859 ospf_hello_reply_timer (struct thread
*thread
)
2861 struct ospf_neighbor
*nbr
;
2863 nbr
= THREAD_ARG (thread
);
2864 nbr
->t_hello_reply
= NULL
;
2868 if (IS_DEBUG_OSPF (nsm
, NSM_TIMERS
))
2869 zlog (NULL
, LOG_INFO
, "NSM[%s:%s]: Timer (hello-reply timer expire)",
2870 IF_NAME (nbr
->oi
), inet_ntoa (nbr
->router_id
));
2872 ospf_hello_send_sub (nbr
->oi
, &nbr
->address
.u
.prefix4
);
2877 /* Send OSPF Hello. */
2879 ospf_hello_send (struct ospf_interface
*oi
)
2881 struct ospf_packet
*op
;
2882 u_int16_t length
= OSPF_HEADER_SIZE
;
2884 /* If this is passive interface, do not send OSPF Hello. */
2885 if (OSPF_IF_PARAM (oi
, passive_interface
) == OSPF_IF_PASSIVE
)
2888 op
= ospf_packet_new (oi
->ifp
->mtu
);
2890 /* Prepare OSPF common header. */
2891 ospf_make_header (OSPF_MSG_HELLO
, oi
, op
->s
);
2893 /* Prepare OSPF Hello body. */
2894 length
+= ospf_make_hello (oi
, op
->s
);
2896 /* Fill OSPF header. */
2897 ospf_fill_header (oi
, op
->s
, length
);
2899 /* Set packet length. */
2900 op
->length
= length
;
2902 if (oi
->type
== OSPF_IFTYPE_NBMA
)
2904 struct ospf_neighbor
*nbr
;
2905 struct route_node
*rn
;
2907 for (rn
= route_top (oi
->nbrs
); rn
; rn
= route_next (rn
))
2908 if ((nbr
= rn
->info
))
2909 if (nbr
!= oi
->nbr_self
)
2910 if (nbr
->state
!= NSM_Down
)
2912 /* RFC 2328 Section 9.5.1
2913 If the router is not eligible to become Designated Router,
2914 it must periodically send Hello Packets to both the
2915 Designated Router and the Backup Designated Router (if they
2917 if (PRIORITY(oi
) == 0 &&
2918 IPV4_ADDR_CMP(&DR(oi
), &nbr
->address
.u
.prefix4
) &&
2919 IPV4_ADDR_CMP(&BDR(oi
), &nbr
->address
.u
.prefix4
))
2922 /* If the router is eligible to become Designated Router, it
2923 must periodically send Hello Packets to all neighbors that
2924 are also eligible. In addition, if the router is itself the
2925 Designated Router or Backup Designated Router, it must also
2926 send periodic Hello Packets to all other neighbors. */
2928 if (nbr
->priority
== 0 && oi
->state
== ISM_DROther
)
2930 /* if oi->state == Waiting, send hello to all neighbors */
2932 struct ospf_packet
*op_dup
;
2934 op_dup
= ospf_packet_dup(op
);
2935 op_dup
->dst
= nbr
->address
.u
.prefix4
;
2937 /* Add packet to the interface output queue. */
2938 ospf_packet_add (oi
, op_dup
);
2940 OSPF_ISM_WRITE_ON (oi
->ospf
);
2944 ospf_packet_free (op
);
2948 /* Decide destination address. */
2949 if (oi
->type
== OSPF_IFTYPE_VIRTUALLINK
)
2950 op
->dst
.s_addr
= oi
->vl_data
->peer_addr
.s_addr
;
2952 op
->dst
.s_addr
= htonl (OSPF_ALLSPFROUTERS
);
2954 /* Add packet to the interface output queue. */
2955 ospf_packet_add (oi
, op
);
2957 /* Hook thread to write packet. */
2958 OSPF_ISM_WRITE_ON (oi
->ospf
);
2962 /* Send OSPF Database Description. */
2964 ospf_db_desc_send (struct ospf_neighbor
*nbr
)
2966 struct ospf_interface
*oi
;
2967 struct ospf_packet
*op
;
2968 u_int16_t length
= OSPF_HEADER_SIZE
;
2971 op
= ospf_packet_new (oi
->ifp
->mtu
);
2973 /* Prepare OSPF common header. */
2974 ospf_make_header (OSPF_MSG_DB_DESC
, oi
, op
->s
);
2976 /* Prepare OSPF Database Description body. */
2977 length
+= ospf_make_db_desc (oi
, nbr
, op
->s
);
2979 /* Fill OSPF header. */
2980 ospf_fill_header (oi
, op
->s
, length
);
2982 /* Set packet length. */
2983 op
->length
= length
;
2985 /* Decide destination address. */
2986 op
->dst
= nbr
->address
.u
.prefix4
;
2988 /* Add packet to the interface output queue. */
2989 ospf_packet_add (oi
, op
);
2991 /* Hook thread to write packet. */
2992 OSPF_ISM_WRITE_ON (oi
->ospf
);
2994 /* Remove old DD packet, then copy new one and keep in neighbor structure. */
2996 ospf_packet_free (nbr
->last_send
);
2997 nbr
->last_send
= ospf_packet_dup (op
);
2998 gettimeofday (&nbr
->last_send_ts
, NULL
);
3001 /* Re-send Database Description. */
3003 ospf_db_desc_resend (struct ospf_neighbor
*nbr
)
3005 struct ospf_interface
*oi
;
3009 /* Add packet to the interface output queue. */
3010 ospf_packet_add (oi
, ospf_packet_dup (nbr
->last_send
));
3012 /* Hook thread to write packet. */
3013 OSPF_ISM_WRITE_ON (oi
->ospf
);
3016 /* Send Link State Request. */
3018 ospf_ls_req_send (struct ospf_neighbor
*nbr
)
3020 struct ospf_interface
*oi
;
3021 struct ospf_packet
*op
;
3022 u_int16_t length
= OSPF_HEADER_SIZE
;
3025 op
= ospf_packet_new (oi
->ifp
->mtu
);
3027 /* Prepare OSPF common header. */
3028 ospf_make_header (OSPF_MSG_LS_REQ
, oi
, op
->s
);
3030 /* Prepare OSPF Link State Request body. */
3031 length
+= ospf_make_ls_req (nbr
, op
->s
);
3032 if (length
== OSPF_HEADER_SIZE
)
3034 ospf_packet_free (op
);
3038 /* Fill OSPF header. */
3039 ospf_fill_header (oi
, op
->s
, length
);
3041 /* Set packet length. */
3042 op
->length
= length
;
3044 /* Decide destination address. */
3045 op
->dst
= nbr
->address
.u
.prefix4
;
3047 /* Add packet to the interface output queue. */
3048 ospf_packet_add (oi
, op
);
3050 /* Hook thread to write packet. */
3051 OSPF_ISM_WRITE_ON (oi
->ospf
);
3053 /* Add Link State Request Retransmission Timer. */
3054 OSPF_NSM_TIMER_ON (nbr
->t_ls_req
, ospf_ls_req_timer
, nbr
->v_ls_req
);
3057 /* Send Link State Update with an LSA. */
3059 ospf_ls_upd_send_lsa (struct ospf_neighbor
*nbr
, struct ospf_lsa
*lsa
,
3064 update
= list_new ();
3066 listnode_add (update
, lsa
);
3067 ospf_ls_upd_send (nbr
, update
, flag
);
3069 list_delete (update
);
3073 ospf_ls_upd_queue_send (struct ospf_interface
*oi
, list update
,
3074 struct in_addr addr
)
3076 struct ospf_packet
*op
;
3077 u_int16_t length
= OSPF_HEADER_SIZE
;
3079 if (IS_DEBUG_OSPF_EVENT
)
3080 zlog_info ("listcount = %d, dst %s", listcount (update
), inet_ntoa(addr
));
3082 op
= ospf_packet_new (oi
->ifp
->mtu
);
3084 /* Prepare OSPF common header. */
3085 ospf_make_header (OSPF_MSG_LS_UPD
, oi
, op
->s
);
3087 /* Prepare OSPF Link State Update body.
3088 * Includes Type-7 translation.
3090 length
+= ospf_make_ls_upd (oi
, update
, op
->s
);
3092 /* Fill OSPF header. */
3093 ospf_fill_header (oi
, op
->s
, length
);
3095 /* Set packet length. */
3096 op
->length
= length
;
3098 /* Decide destination address. */
3099 op
->dst
.s_addr
= addr
.s_addr
;
3101 /* Add packet to the interface output queue. */
3102 ospf_packet_add (oi
, op
);
3104 /* Hook thread to write packet. */
3105 OSPF_ISM_WRITE_ON (oi
->ospf
);
3109 ospf_ls_upd_send_queue_event (struct thread
*thread
)
3111 struct ospf_interface
*oi
= THREAD_ARG(thread
);
3112 struct route_node
*rn
;
3113 struct route_node
*rnext
;
3114 struct list
*update
;
3115 struct listnode
*tn
, *nn
;
3116 unsigned int again
= 0;
3118 oi
->t_ls_upd_event
= NULL
;
3120 if (IS_DEBUG_OSPF_EVENT
)
3121 zlog_info ("ospf_ls_upd_send_queue start");
3123 for (rn
= route_top (oi
->ls_upd_queue
); rn
; rn
= rnext
)
3125 update
= (struct list
*)rn
->info
;
3126 rnext
= route_next (rn
);
3128 if (rn
->info
== NULL
)
3131 ospf_ls_upd_queue_send (oi
, update
, rn
->p
.u
.prefix4
);
3133 /* list might not be empty.
3134 * TODO: work out what to do about oversized LSAs.
3136 if (listcount(update
) == 0)
3138 list_delete (rn
->info
);
3140 route_unlock_node (rn
);
3148 if (IS_DEBUG_OSPF_EVENT
)
3149 zlog_info ("ospf_ls_upd_send_queue: update lists not cleared,"
3150 " %d nodes to try again, raising new event", again
);
3151 oi
->t_ls_upd_event
=
3152 thread_add_event (master
, ospf_ls_upd_send_queue_event
, oi
, 0);
3155 if (IS_DEBUG_OSPF_EVENT
)
3156 zlog_info ("ospf_ls_upd_send_queue stop");
3162 ospf_ls_upd_send (struct ospf_neighbor
*nbr
, list update
, int flag
)
3164 struct ospf_interface
*oi
;
3165 struct prefix_ipv4 p
;
3166 struct route_node
*rn
;
3172 p
.prefixlen
= IPV4_MAX_BITLEN
;
3174 /* Decide destination address. */
3175 if (oi
->type
== OSPF_IFTYPE_VIRTUALLINK
)
3176 p
.prefix
= oi
->vl_data
->peer_addr
;
3177 else if (flag
== OSPF_SEND_PACKET_DIRECT
)
3178 p
.prefix
= nbr
->address
.u
.prefix4
;
3179 else if (oi
->state
== ISM_DR
|| oi
->state
== ISM_Backup
)
3180 p
.prefix
.s_addr
= htonl (OSPF_ALLSPFROUTERS
);
3181 else if ((oi
->type
== OSPF_IFTYPE_POINTOPOINT
)
3182 && (flag
== OSPF_SEND_PACKET_INDIRECT
))
3183 p
.prefix
.s_addr
= htonl (OSPF_ALLSPFROUTERS
);
3184 else if (oi
->type
== OSPF_IFTYPE_POINTOMULTIPOINT
)
3185 p
.prefix
.s_addr
= htonl (OSPF_ALLSPFROUTERS
);
3187 p
.prefix
.s_addr
= htonl (OSPF_ALLDROUTERS
);
3189 if (oi
->type
== OSPF_IFTYPE_NBMA
)
3191 if (flag
== OSPF_SEND_PACKET_INDIRECT
)
3192 zlog_warn ("* LS-Update is directly sent on NBMA network.");
3193 if (IPV4_ADDR_SAME(&oi
->address
->u
.prefix4
, &p
.prefix
.s_addr
))
3194 zlog_warn ("* LS-Update is sent to myself.");
3197 rn
= route_node_get (oi
->ls_upd_queue
, (struct prefix
*) &p
);
3199 if (rn
->info
== NULL
)
3200 rn
->info
= list_new ();
3202 for (n
= listhead (update
); n
; nextnode (n
))
3203 listnode_add (rn
->info
, ospf_lsa_lock (getdata (n
)));
3205 if (oi
->t_ls_upd_event
== NULL
)
3206 oi
->t_ls_upd_event
=
3207 thread_add_event (master
, ospf_ls_upd_send_queue_event
, oi
, 0);
3211 ospf_ls_ack_send_list (struct ospf_interface
*oi
, list ack
, struct in_addr dst
)
3213 struct ospf_packet
*op
;
3214 u_int16_t length
= OSPF_HEADER_SIZE
;
3216 op
= ospf_packet_new (oi
->ifp
->mtu
);
3218 /* Prepare OSPF common header. */
3219 ospf_make_header (OSPF_MSG_LS_ACK
, oi
, op
->s
);
3221 /* Prepare OSPF Link State Acknowledgment body. */
3222 length
+= ospf_make_ls_ack (oi
, ack
, op
->s
);
3224 /* Fill OSPF header. */
3225 ospf_fill_header (oi
, op
->s
, length
);
3227 /* Set packet length. */
3228 op
->length
= length
;
3230 /* Set destination IP address. */
3233 /* Add packet to the interface output queue. */
3234 ospf_packet_add (oi
, op
);
3236 /* Hook thread to write packet. */
3237 OSPF_ISM_WRITE_ON (oi
->ospf
);
3241 ospf_ls_ack_send_event (struct thread
*thread
)
3243 struct ospf_interface
*oi
= THREAD_ARG (thread
);
3245 oi
->t_ls_ack_direct
= NULL
;
3247 while (listcount (oi
->ls_ack_direct
.ls_ack
))
3248 ospf_ls_ack_send_list (oi
, oi
->ls_ack_direct
.ls_ack
,
3249 oi
->ls_ack_direct
.dst
);
3255 ospf_ls_ack_send (struct ospf_neighbor
*nbr
, struct ospf_lsa
*lsa
)
3257 struct ospf_interface
*oi
= nbr
->oi
;
3259 if (listcount (oi
->ls_ack_direct
.ls_ack
) == 0)
3260 oi
->ls_ack_direct
.dst
= nbr
->address
.u
.prefix4
;
3262 listnode_add (oi
->ls_ack_direct
.ls_ack
, ospf_lsa_lock (lsa
));
3264 if (oi
->t_ls_ack_direct
== NULL
)
3265 oi
->t_ls_ack_direct
=
3266 thread_add_event (master
, ospf_ls_ack_send_event
, oi
, 0);
3269 /* Send Link State Acknowledgment delayed. */
3271 ospf_ls_ack_send_delayed (struct ospf_interface
*oi
)
3275 /* Decide destination address. */
3276 /* RFC2328 Section 13.5 On non-broadcast
3277 networks, delayed Link State Acknowledgment packets must be
3278 unicast separately over each adjacency (i.e., neighbor whose
3279 state is >= Exchange). */
3280 if (oi
->type
== OSPF_IFTYPE_NBMA
)
3282 struct ospf_neighbor
*nbr
;
3283 struct route_node
*rn
;
3285 for (rn
= route_top (oi
->nbrs
); rn
; rn
= route_next (rn
))
3286 if ((nbr
= rn
->info
) != NULL
)
3287 if (nbr
!= oi
->nbr_self
&& nbr
->state
>= NSM_Exchange
)
3288 while (listcount (oi
->ls_ack
))
3289 ospf_ls_ack_send_list (oi
, oi
->ls_ack
, nbr
->address
.u
.prefix4
);
3292 if (oi
->type
== OSPF_IFTYPE_VIRTUALLINK
)
3293 dst
.s_addr
= oi
->vl_data
->peer_addr
.s_addr
;
3294 else if (oi
->state
== ISM_DR
|| oi
->state
== ISM_Backup
)
3295 dst
.s_addr
= htonl (OSPF_ALLSPFROUTERS
);
3296 else if (oi
->type
== OSPF_IFTYPE_POINTOPOINT
)
3297 dst
.s_addr
= htonl (OSPF_ALLSPFROUTERS
);
3299 dst
.s_addr
= htonl (OSPF_ALLDROUTERS
);
3301 while (listcount (oi
->ls_ack
))
3302 ospf_ls_ack_send_list (oi
, oi
->ls_ack
, dst
);