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"
38 #include "ospfd/ospfd.h"
39 #include "ospfd/ospf_network.h"
40 #include "ospfd/ospf_interface.h"
41 #include "ospfd/ospf_ism.h"
42 #include "ospfd/ospf_asbr.h"
43 #include "ospfd/ospf_lsa.h"
44 #include "ospfd/ospf_lsdb.h"
45 #include "ospfd/ospf_neighbor.h"
46 #include "ospfd/ospf_nsm.h"
47 #include "ospfd/ospf_packet.h"
48 #include "ospfd/ospf_spf.h"
49 #include "ospfd/ospf_flood.h"
50 #include "ospfd/ospf_dump.h"
52 /* Packet Type String. */
53 const char *ospf_packet_type_str
[] =
57 "Database Description",
60 "Link State Acknowledgment",
63 /* OSPF authentication checking function */
65 ospf_auth_type (struct ospf_interface
*oi
)
69 if (OSPF_IF_PARAM (oi
, auth_type
) == OSPF_AUTH_NOTSET
)
70 auth_type
= oi
->area
->auth_type
;
72 auth_type
= OSPF_IF_PARAM (oi
, auth_type
);
74 /* Handle case where MD5 key list is not configured aka Cisco */
75 if (auth_type
== OSPF_AUTH_CRYPTOGRAPHIC
&&
76 list_isempty (OSPF_IF_PARAM (oi
, auth_crypt
)))
77 return OSPF_AUTH_NULL
;
84 ospf_packet_new (size_t size
)
86 struct ospf_packet
*new;
88 new = XCALLOC (MTYPE_OSPF_PACKET
, sizeof (struct ospf_packet
));
89 new->s
= stream_new (size
);
95 ospf_packet_free (struct ospf_packet
*op
)
100 XFREE (MTYPE_OSPF_PACKET
, op
);
108 struct ospf_fifo
*new;
110 new = XCALLOC (MTYPE_OSPF_FIFO
, sizeof (struct ospf_fifo
));
114 /* Add new packet to fifo. */
116 ospf_fifo_push (struct ospf_fifo
*fifo
, struct ospf_packet
*op
)
119 fifo
->tail
->next
= op
;
128 /* Add new packet to head of fifo. */
130 ospf_fifo_push_head (struct ospf_fifo
*fifo
, struct ospf_packet
*op
)
132 op
->next
= fifo
->head
;
134 if (fifo
->tail
== NULL
)
142 /* Delete first packet from fifo. */
144 ospf_fifo_pop (struct ospf_fifo
*fifo
)
146 struct ospf_packet
*op
;
152 fifo
->head
= op
->next
;
154 if (fifo
->head
== NULL
)
163 /* Return first fifo entry. */
165 ospf_fifo_head (struct ospf_fifo
*fifo
)
170 /* Flush ospf packet fifo. */
172 ospf_fifo_flush (struct ospf_fifo
*fifo
)
174 struct ospf_packet
*op
;
175 struct ospf_packet
*next
;
177 for (op
= fifo
->head
; op
; op
= next
)
180 ospf_packet_free (op
);
182 fifo
->head
= fifo
->tail
= NULL
;
186 /* Free ospf packet fifo. */
188 ospf_fifo_free (struct ospf_fifo
*fifo
)
190 ospf_fifo_flush (fifo
);
192 XFREE (MTYPE_OSPF_FIFO
, fifo
);
196 ospf_packet_add (struct ospf_interface
*oi
, struct ospf_packet
*op
)
200 zlog_err("ospf_packet_add(interface %s in state %d [%s], packet type %s, "
201 "destination %s) called with NULL obuf, ignoring "
202 "(please report this bug)!\n",
203 IF_NAME(oi
), oi
->state
, LOOKUP (ospf_ism_state_msg
, oi
->state
),
204 ospf_packet_type_str
[stream_getc_from(op
->s
, 1)],
205 inet_ntoa (op
->dst
));
209 /* Add packet to end of queue. */
210 ospf_fifo_push (oi
->obuf
, op
);
212 /* Debug of packet fifo*/
213 /* ospf_fifo_debug (oi->obuf); */
217 ospf_packet_add_top (struct ospf_interface
*oi
, struct ospf_packet
*op
)
221 zlog_err("ospf_packet_add(interface %s in state %d [%s], packet type %s, "
222 "destination %s) called with NULL obuf, ignoring "
223 "(please report this bug)!\n",
224 IF_NAME(oi
), oi
->state
, LOOKUP (ospf_ism_state_msg
, oi
->state
),
225 ospf_packet_type_str
[stream_getc_from(op
->s
, 1)],
226 inet_ntoa (op
->dst
));
230 /* Add packet to head of queue. */
231 ospf_fifo_push_head (oi
->obuf
, op
);
233 /* Debug of packet fifo*/
234 /* ospf_fifo_debug (oi->obuf); */
238 ospf_packet_delete (struct ospf_interface
*oi
)
240 struct ospf_packet
*op
;
242 op
= ospf_fifo_pop (oi
->obuf
);
245 ospf_packet_free (op
);
249 ospf_packet_dup (struct ospf_packet
*op
)
251 struct ospf_packet
*new;
253 if (stream_get_endp(op
->s
) != op
->length
)
255 zlog_warn ("ospf_packet_dup stream %lu ospf_packet %u size mismatch",
256 (u_long
)STREAM_SIZE(op
->s
), op
->length
);
258 /* Reserve space for MD5 authentication that may be added later. */
259 new = ospf_packet_new (stream_get_endp(op
->s
) + OSPF_AUTH_MD5_SIZE
);
260 stream_copy (new->s
, op
->s
);
263 new->length
= op
->length
;
269 static inline unsigned int
270 ospf_packet_authspace (struct ospf_interface
*oi
)
274 if ( ospf_auth_type (oi
) == OSPF_AUTH_CRYPTOGRAPHIC
)
275 auth
= OSPF_AUTH_MD5_SIZE
;
281 ospf_packet_max (struct ospf_interface
*oi
)
285 max
= oi
->ifp
->mtu
- ospf_packet_authspace(oi
);
287 max
-= (OSPF_HEADER_SIZE
+ sizeof (struct ip
));
294 ospf_check_md5_digest (struct ospf_interface
*oi
, struct stream
*s
,
299 unsigned char digest
[OSPF_AUTH_MD5_SIZE
];
300 unsigned char *pdigest
;
301 struct crypt_key
*ck
;
302 struct ospf_header
*ospfh
;
303 struct ospf_neighbor
*nbr
;
306 ibuf
= STREAM_PNT (s
);
307 ospfh
= (struct ospf_header
*) ibuf
;
309 /* Get pointer to the end of the packet. */
310 pdigest
= ibuf
+ length
;
312 /* Get secret key. */
313 ck
= ospf_crypt_key_lookup (OSPF_IF_PARAM (oi
, auth_crypt
),
314 ospfh
->u
.crypt
.key_id
);
317 zlog_warn ("interface %s: ospf_check_md5 no key %d",
318 IF_NAME (oi
), ospfh
->u
.crypt
.key_id
);
322 /* check crypto seqnum. */
323 nbr
= ospf_nbr_lookup_by_routerid (oi
->nbrs
, &ospfh
->router_id
);
325 if (nbr
&& ntohl(nbr
->crypt_seqnum
) > ntohl(ospfh
->u
.crypt
.crypt_seqnum
))
327 zlog_warn ("interface %s: ospf_check_md5 bad sequence %d (expect %d)",
329 ntohl(ospfh
->u
.crypt
.crypt_seqnum
),
330 ntohl(nbr
->crypt_seqnum
));
334 /* Generate a digest for the ospf packet - their digest + our digest. */
335 memset(&ctx
, 0, sizeof(ctx
));
337 MD5Update(&ctx
, ibuf
, length
);
338 MD5Update(&ctx
, ck
->auth_key
, OSPF_AUTH_MD5_SIZE
);
339 MD5Final(digest
, &ctx
);
341 /* compare the two */
342 if (memcmp (pdigest
, digest
, OSPF_AUTH_MD5_SIZE
))
344 zlog_warn ("interface %s: ospf_check_md5 checksum mismatch",
349 /* save neighbor's crypt_seqnum */
351 nbr
->crypt_seqnum
= ospfh
->u
.crypt
.crypt_seqnum
;
355 /* This function is called from ospf_write(), it will detect the
356 authentication scheme and if it is MD5, it will change the sequence
357 and update the MD5 digest. */
359 ospf_make_md5_digest (struct ospf_interface
*oi
, struct ospf_packet
*op
)
361 struct ospf_header
*ospfh
;
362 unsigned char digest
[OSPF_AUTH_MD5_SIZE
];
366 struct crypt_key
*ck
;
367 const u_int8_t
*auth_key
;
369 ibuf
= STREAM_DATA (op
->s
);
370 ospfh
= (struct ospf_header
*) ibuf
;
372 if (ntohs (ospfh
->auth_type
) != OSPF_AUTH_CRYPTOGRAPHIC
)
375 /* We do this here so when we dup a packet, we don't have to
376 waste CPU rewriting other headers.
378 Note that quagga_time /deliberately/ is not used here */
379 t
= (time(NULL
) & 0xFFFFFFFF);
380 if (t
> oi
->crypt_seqnum
)
381 oi
->crypt_seqnum
= t
;
385 ospfh
->u
.crypt
.crypt_seqnum
= htonl (oi
->crypt_seqnum
);
387 /* Get MD5 Authentication key from auth_key list. */
388 if (list_isempty (OSPF_IF_PARAM (oi
, auth_crypt
)))
389 auth_key
= (const u_int8_t
*) "";
392 ck
= listgetdata (listtail(OSPF_IF_PARAM (oi
, auth_crypt
)));
393 auth_key
= ck
->auth_key
;
396 /* Generate a digest for the entire packet + our secret key. */
397 memset(&ctx
, 0, sizeof(ctx
));
399 MD5Update(&ctx
, ibuf
, ntohs (ospfh
->length
));
400 MD5Update(&ctx
, auth_key
, OSPF_AUTH_MD5_SIZE
);
401 MD5Final(digest
, &ctx
);
403 /* Append md5 digest to the end of the stream. */
404 stream_put (op
->s
, digest
, OSPF_AUTH_MD5_SIZE
);
406 /* We do *NOT* increment the OSPF header length. */
407 op
->length
= ntohs (ospfh
->length
) + OSPF_AUTH_MD5_SIZE
;
409 if (stream_get_endp(op
->s
) != op
->length
)
411 zlog_warn("ospf_make_md5_digest: length mismatch stream %lu ospf_packet %u",
412 (u_long
)stream_get_endp(op
->s
), op
->length
);
414 return OSPF_AUTH_MD5_SIZE
;
419 ospf_ls_req_timer (struct thread
*thread
)
421 struct ospf_neighbor
*nbr
;
423 nbr
= THREAD_ARG (thread
);
424 nbr
->t_ls_req
= NULL
;
426 /* Send Link State Request. */
427 if (ospf_ls_request_count (nbr
))
428 ospf_ls_req_send (nbr
);
430 /* Set Link State Request retransmission timer. */
431 OSPF_NSM_TIMER_ON (nbr
->t_ls_req
, ospf_ls_req_timer
, nbr
->v_ls_req
);
437 ospf_ls_req_event (struct ospf_neighbor
*nbr
)
441 thread_cancel (nbr
->t_ls_req
);
442 nbr
->t_ls_req
= NULL
;
444 nbr
->t_ls_req
= thread_add_event (master
, ospf_ls_req_timer
, nbr
, 0);
447 /* Cyclic timer function. Fist registered in ospf_nbr_new () in
450 ospf_ls_upd_timer (struct thread
*thread
)
452 struct ospf_neighbor
*nbr
;
454 nbr
= THREAD_ARG (thread
);
455 nbr
->t_ls_upd
= NULL
;
457 /* Send Link State Update. */
458 if (ospf_ls_retransmit_count (nbr
) > 0)
461 struct ospf_lsdb
*lsdb
;
463 int retransmit_interval
;
465 retransmit_interval
= OSPF_IF_PARAM (nbr
->oi
, retransmit_interval
);
467 lsdb
= &nbr
->ls_rxmt
;
468 update
= list_new ();
470 for (i
= OSPF_MIN_LSA
; i
< OSPF_MAX_LSA
; i
++)
472 struct route_table
*table
= lsdb
->type
[i
].db
;
473 struct route_node
*rn
;
475 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
477 struct ospf_lsa
*lsa
;
479 if ((lsa
= rn
->info
) != NULL
)
480 /* Don't retransmit an LSA if we received it within
481 the last RxmtInterval seconds - this is to allow the
482 neighbour a chance to acknowledge the LSA as it may
483 have ben just received before the retransmit timer
484 fired. This is a small tweak to what is in the RFC,
485 but it will cut out out a lot of retransmit traffic
487 if (tv_cmp (tv_sub (recent_relative_time (), lsa
->tv_recv
),
488 int2tv (retransmit_interval
)) >= 0)
489 listnode_add (update
, rn
->info
);
493 if (listcount (update
) > 0)
494 ospf_ls_upd_send (nbr
, update
, OSPF_SEND_PACKET_DIRECT
);
495 list_delete (update
);
498 /* Set LS Update retransmission timer. */
499 OSPF_NSM_TIMER_ON (nbr
->t_ls_upd
, ospf_ls_upd_timer
, nbr
->v_ls_upd
);
505 ospf_ls_ack_timer (struct thread
*thread
)
507 struct ospf_interface
*oi
;
509 oi
= THREAD_ARG (thread
);
512 /* Send Link State Acknowledgment. */
513 if (listcount (oi
->ls_ack
) > 0)
514 ospf_ls_ack_send_delayed (oi
);
516 /* Set LS Ack timer. */
517 OSPF_ISM_TIMER_ON (oi
->t_ls_ack
, ospf_ls_ack_timer
, oi
->v_ls_ack
);
522 #ifdef WANT_OSPF_WRITE_FRAGMENT
524 ospf_write_frags (int fd
, struct ospf_packet
*op
, struct ip
*iph
,
525 struct msghdr
*msg
, unsigned int maxdatasize
,
526 unsigned int mtu
, int flags
, u_char type
)
528 #define OSPF_WRITE_FRAG_SHIFT 3
533 assert ( op
->length
== stream_get_endp(op
->s
) );
534 assert (msg
->msg_iovlen
== 2);
538 * SunOS, BSD and BSD derived kernels likely will clear ip_id, as
539 * well as the IP_MF flag, making this all quite pointless.
541 * However, for a system on which IP_MF is left alone, and ip_id left
542 * alone or else which sets same ip_id for each fragment this might
545 * XXX-TODO: It would be much nicer to have the kernel's use their
546 * existing fragmentation support to do this for us. Bugs/RFEs need to
547 * be raised against the various kernels.
551 iph
->ip_off
|= IP_MF
;
553 /* ip frag offset is expressed in units of 8byte words */
554 offset
= maxdatasize
>> OSPF_WRITE_FRAG_SHIFT
;
556 iovp
= &msg
->msg_iov
[1];
558 while ( (stream_get_endp(op
->s
) - stream_get_getp (op
->s
))
561 /* data length of this frag is to next offset value */
562 iovp
->iov_len
= offset
<< OSPF_WRITE_FRAG_SHIFT
;
563 iph
->ip_len
= iovp
->iov_len
+ sizeof (struct ip
);
564 assert (iph
->ip_len
<= mtu
);
566 sockopt_iphdrincl_swab_htosys (iph
);
568 ret
= sendmsg (fd
, msg
, flags
);
570 sockopt_iphdrincl_swab_systoh (iph
);
573 zlog_warn ("*** ospf_write_frags: sendmsg failed to %s,"
574 " id %d, off %d, len %d, mtu %u failed with %s",
575 inet_ntoa (iph
->ip_dst
),
580 safe_strerror (errno
));
582 if (IS_DEBUG_OSPF_PACKET (type
- 1, SEND
))
584 zlog_debug ("ospf_write_frags: sent id %d, off %d, len %d to %s\n",
585 iph
->ip_id
, iph
->ip_off
, iph
->ip_len
,
586 inet_ntoa (iph
->ip_dst
));
587 if (IS_DEBUG_OSPF_PACKET (type
- 1, DETAIL
))
589 zlog_debug ("-----------------IP Header Dump----------------------");
590 ospf_ip_header_dump (iph
);
591 zlog_debug ("-----------------------------------------------------");
595 iph
->ip_off
+= offset
;
596 stream_forward_getp (op
->s
, iovp
->iov_len
);
597 iovp
->iov_base
= STREAM_PNT (op
->s
);
600 /* setup for final fragment */
601 iovp
->iov_len
= stream_get_endp(op
->s
) - stream_get_getp (op
->s
);
602 iph
->ip_len
= iovp
->iov_len
+ sizeof (struct ip
);
603 iph
->ip_off
&= (~IP_MF
);
605 #endif /* WANT_OSPF_WRITE_FRAGMENT */
608 ospf_write (struct thread
*thread
)
610 struct ospf
*ospf
= THREAD_ARG (thread
);
611 struct ospf_interface
*oi
;
612 struct ospf_packet
*op
;
613 struct sockaddr_in sa_dst
;
620 struct listnode
*node
;
621 #ifdef WANT_OSPF_WRITE_FRAGMENT
622 static u_int16_t ipid
= 0;
623 #endif /* WANT_OSPF_WRITE_FRAGMENT */
624 u_int16_t maxdatasize
;
625 #define OSPF_WRITE_IPHL_SHIFT 2
627 ospf
->t_write
= NULL
;
629 node
= listhead (ospf
->oi_write_q
);
631 oi
= listgetdata (node
);
634 #ifdef WANT_OSPF_WRITE_FRAGMENT
635 /* seed ipid static with low order bits of time */
637 ipid
= (time(NULL
) & 0xffff);
638 #endif /* WANT_OSPF_WRITE_FRAGMENT */
640 /* convenience - max OSPF data per packet,
641 * and reliability - not more data, than our
644 maxdatasize
= MIN (oi
->ifp
->mtu
, ospf
->maxsndbuflen
) -
647 /* Get one packet from queue. */
648 op
= ospf_fifo_head (oi
->obuf
);
650 assert (op
->length
>= OSPF_HEADER_SIZE
);
652 if (op
->dst
.s_addr
== htonl (OSPF_ALLSPFROUTERS
)
653 || op
->dst
.s_addr
== htonl (OSPF_ALLDROUTERS
))
654 ospf_if_ipmulticast (ospf
, oi
->address
, oi
->ifp
->ifindex
);
656 /* Rewrite the md5 signature & update the seq */
657 ospf_make_md5_digest (oi
, op
);
659 /* Retrieve OSPF packet type. */
660 stream_set_getp (op
->s
, 1);
661 type
= stream_getc (op
->s
);
663 /* reset get pointer */
664 stream_set_getp (op
->s
, 0);
666 memset (&iph
, 0, sizeof (struct ip
));
667 memset (&sa_dst
, 0, sizeof (sa_dst
));
669 sa_dst
.sin_family
= AF_INET
;
670 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
671 sa_dst
.sin_len
= sizeof(sa_dst
);
672 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
673 sa_dst
.sin_addr
= op
->dst
;
674 sa_dst
.sin_port
= htons (0);
676 /* Set DONTROUTE flag if dst is unicast. */
677 if (oi
->type
!= OSPF_IFTYPE_VIRTUALLINK
)
678 if (!IN_MULTICAST (htonl (op
->dst
.s_addr
)))
679 flags
= MSG_DONTROUTE
;
681 iph
.ip_hl
= sizeof (struct ip
) >> OSPF_WRITE_IPHL_SHIFT
;
682 /* it'd be very strange for header to not be 4byte-word aligned but.. */
683 if ( sizeof (struct ip
)
684 > (unsigned int)(iph
.ip_hl
<< OSPF_WRITE_IPHL_SHIFT
) )
685 iph
.ip_hl
++; /* we presume sizeof struct ip cant overflow ip_hl.. */
687 iph
.ip_v
= IPVERSION
;
688 iph
.ip_tos
= IPTOS_PREC_INTERNETCONTROL
;
689 iph
.ip_len
= (iph
.ip_hl
<< OSPF_WRITE_IPHL_SHIFT
) + op
->length
;
691 #if defined(__DragonFly__)
693 * DragonFly's raw socket expects ip_len/ip_off in network byte order.
695 iph
.ip_len
= htons(iph
.ip_len
);
698 #ifdef WANT_OSPF_WRITE_FRAGMENT
699 /* XXX-MT: not thread-safe at all..
700 * XXX: this presumes this is only programme sending OSPF packets
701 * otherwise, no guarantee ipid will be unique
704 #endif /* WANT_OSPF_WRITE_FRAGMENT */
707 if (oi
->type
== OSPF_IFTYPE_VIRTUALLINK
)
708 iph
.ip_ttl
= OSPF_VL_IP_TTL
;
710 iph
.ip_ttl
= OSPF_IP_TTL
;
711 iph
.ip_p
= IPPROTO_OSPFIGP
;
713 iph
.ip_src
.s_addr
= oi
->address
->u
.prefix4
.s_addr
;
714 iph
.ip_dst
.s_addr
= op
->dst
.s_addr
;
716 memset (&msg
, 0, sizeof (msg
));
717 msg
.msg_name
= (caddr_t
) &sa_dst
;
718 msg
.msg_namelen
= sizeof (sa_dst
);
721 iov
[0].iov_base
= (char*)&iph
;
722 iov
[0].iov_len
= iph
.ip_hl
<< OSPF_WRITE_IPHL_SHIFT
;
723 iov
[1].iov_base
= STREAM_PNT (op
->s
);
724 iov
[1].iov_len
= op
->length
;
726 /* Sadly we can not rely on kernels to fragment packets because of either
727 * IP_HDRINCL and/or multicast destination being set.
729 #ifdef WANT_OSPF_WRITE_FRAGMENT
730 if ( op
->length
> maxdatasize
)
731 ospf_write_frags (ospf
->fd
, op
, &iph
, &msg
, maxdatasize
,
732 oi
->ifp
->mtu
, flags
, type
);
733 #endif /* WANT_OSPF_WRITE_FRAGMENT */
735 /* send final fragment (could be first) */
736 sockopt_iphdrincl_swab_htosys (&iph
);
737 ret
= sendmsg (ospf
->fd
, &msg
, flags
);
738 sockopt_iphdrincl_swab_systoh (&iph
);
741 zlog_warn ("*** sendmsg in ospf_write failed to %s, "
742 "id %d, off %d, len %d, interface %s, mtu %u: %s",
743 inet_ntoa (iph
.ip_dst
), iph
.ip_id
, iph
.ip_off
, iph
.ip_len
,
744 oi
->ifp
->name
, oi
->ifp
->mtu
, safe_strerror (errno
));
746 /* Show debug sending packet. */
747 if (IS_DEBUG_OSPF_PACKET (type
- 1, SEND
))
749 if (IS_DEBUG_OSPF_PACKET (type
- 1, DETAIL
))
751 zlog_debug ("-----------------------------------------------------");
752 ospf_ip_header_dump (&iph
);
753 stream_set_getp (op
->s
, 0);
754 ospf_packet_dump (op
->s
);
757 zlog_debug ("%s sent to [%s] via [%s].",
758 ospf_packet_type_str
[type
], inet_ntoa (op
->dst
),
761 if (IS_DEBUG_OSPF_PACKET (type
- 1, DETAIL
))
762 zlog_debug ("-----------------------------------------------------");
765 /* Now delete packet from queue. */
766 ospf_packet_delete (oi
);
768 if (ospf_fifo_head (oi
->obuf
) == NULL
)
771 list_delete_node (ospf
->oi_write_q
, node
);
774 /* If packets still remain in queue, call write thread. */
775 if (!list_isempty (ospf
->oi_write_q
))
777 thread_add_write (master
, ospf_write
, ospf
, ospf
->fd
);
782 /* OSPF Hello message read -- RFC2328 Section 10.5. */
784 ospf_hello (struct ip
*iph
, struct ospf_header
*ospfh
,
785 struct stream
* s
, struct ospf_interface
*oi
, int size
)
787 struct ospf_hello
*hello
;
788 struct ospf_neighbor
*nbr
;
792 /* increment statistics. */
795 hello
= (struct ospf_hello
*) STREAM_PNT (s
);
797 /* If Hello is myself, silently discard. */
798 if (IPV4_ADDR_SAME (&ospfh
->router_id
, &oi
->ospf
->router_id
))
800 if (IS_DEBUG_OSPF_PACKET (ospfh
->type
- 1, RECV
))
802 zlog_debug ("ospf_header[%s/%s]: selforiginated, "
804 ospf_packet_type_str
[ospfh
->type
],
805 inet_ntoa (iph
->ip_src
));
810 /* get neighbor prefix. */
812 p
.prefixlen
= ip_masklen (hello
->network_mask
);
813 p
.u
.prefix4
= iph
->ip_src
;
815 /* Compare network mask. */
816 /* Checking is ignored for Point-to-Point and Virtual link. */
817 if (oi
->type
!= OSPF_IFTYPE_POINTOPOINT
818 && oi
->type
!= OSPF_IFTYPE_VIRTUALLINK
)
819 if (oi
->address
->prefixlen
!= p
.prefixlen
)
821 zlog_warn ("Packet %s [Hello:RECV]: NetworkMask mismatch on %s (configured prefix length is %d, but hello packet indicates %d).",
822 inet_ntoa(ospfh
->router_id
), IF_NAME(oi
),
823 (int)oi
->address
->prefixlen
, (int)p
.prefixlen
);
827 /* Compare Router Dead Interval. */
828 if (OSPF_IF_PARAM (oi
, v_wait
) != ntohl (hello
->dead_interval
))
830 zlog_warn ("Packet %s [Hello:RECV]: RouterDeadInterval mismatch "
831 "(expected %u, but received %u).",
832 inet_ntoa(ospfh
->router_id
),
833 OSPF_IF_PARAM(oi
, v_wait
), ntohl(hello
->dead_interval
));
837 /* Compare Hello Interval - ignored if fast-hellos are set. */
838 if (OSPF_IF_PARAM (oi
, fast_hello
) == 0)
840 if (OSPF_IF_PARAM (oi
, v_hello
) != ntohs (hello
->hello_interval
))
842 zlog_warn ("Packet %s [Hello:RECV]: HelloInterval mismatch "
843 "(expected %u, but received %u).",
844 inet_ntoa(ospfh
->router_id
),
845 OSPF_IF_PARAM(oi
, v_hello
), ntohs(hello
->hello_interval
));
850 if (IS_DEBUG_OSPF_EVENT
)
851 zlog_debug ("Packet %s [Hello:RECV]: Options %s",
852 inet_ntoa (ospfh
->router_id
),
853 ospf_options_dump (hello
->options
));
855 /* Compare options. */
856 #define REJECT_IF_TBIT_ON 1 /* XXX */
857 #ifdef REJECT_IF_TBIT_ON
858 if (CHECK_FLAG (hello
->options
, OSPF_OPTION_T
))
861 * This router does not support non-zero TOS.
862 * Drop this Hello packet not to establish neighbor relationship.
864 zlog_warn ("Packet %s [Hello:RECV]: T-bit on, drop it.",
865 inet_ntoa (ospfh
->router_id
));
868 #endif /* REJECT_IF_TBIT_ON */
870 #ifdef HAVE_OPAQUE_LSA
871 if (CHECK_FLAG (oi
->ospf
->config
, OSPF_OPAQUE_CAPABLE
)
872 && CHECK_FLAG (hello
->options
, OSPF_OPTION_O
))
875 * This router does know the correct usage of O-bit
876 * the bit should be set in DD packet only.
878 zlog_warn ("Packet %s [Hello:RECV]: O-bit abuse?",
879 inet_ntoa (ospfh
->router_id
));
880 #ifdef STRICT_OBIT_USAGE_CHECK
881 return; /* Reject this packet. */
882 #else /* STRICT_OBIT_USAGE_CHECK */
883 UNSET_FLAG (hello
->options
, OSPF_OPTION_O
); /* Ignore O-bit. */
884 #endif /* STRICT_OBIT_USAGE_CHECK */
886 #endif /* HAVE_OPAQUE_LSA */
888 /* new for NSSA is to ensure that NP is on and E is off */
890 if (oi
->area
->external_routing
== OSPF_AREA_NSSA
)
892 if (! (CHECK_FLAG (OPTIONS (oi
), OSPF_OPTION_NP
)
893 && CHECK_FLAG (hello
->options
, OSPF_OPTION_NP
)
894 && ! CHECK_FLAG (OPTIONS (oi
), OSPF_OPTION_E
)
895 && ! CHECK_FLAG (hello
->options
, OSPF_OPTION_E
)))
897 zlog_warn ("NSSA-Packet-%s[Hello:RECV]: my options: %x, his options %x", inet_ntoa (ospfh
->router_id
), OPTIONS (oi
), hello
->options
);
900 if (IS_DEBUG_OSPF_NSSA
)
901 zlog_debug ("NSSA-Hello:RECV:Packet from %s:", inet_ntoa(ospfh
->router_id
));
904 /* The setting of the E-bit found in the Hello Packet's Options
905 field must match this area's ExternalRoutingCapability A
906 mismatch causes processing to stop and the packet to be
907 dropped. The setting of the rest of the bits in the Hello
908 Packet's Options field should be ignored. */
909 if (CHECK_FLAG (OPTIONS (oi
), OSPF_OPTION_E
) !=
910 CHECK_FLAG (hello
->options
, OSPF_OPTION_E
))
912 zlog_warn ("Packet %s [Hello:RECV]: my options: %x, his options %x",
913 inet_ntoa(ospfh
->router_id
), OPTIONS (oi
), hello
->options
);
917 /* get neighbour struct */
918 nbr
= ospf_nbr_get (oi
, ospfh
, iph
, &p
);
920 /* neighbour must be valid, ospf_nbr_get creates if none existed */
923 old_state
= nbr
->state
;
925 /* Add event to thread. */
926 OSPF_NSM_EVENT_SCHEDULE (nbr
, NSM_PacketReceived
);
928 /* RFC2328 Section 9.5.1
929 If the router is not eligible to become Designated Router,
930 (snip) It must also send an Hello Packet in reply to an
931 Hello Packet received from any eligible neighbor (other than
932 the current Designated Router and Backup Designated Router). */
933 if (oi
->type
== OSPF_IFTYPE_NBMA
)
934 if (PRIORITY(oi
) == 0 && hello
->priority
> 0
935 && IPV4_ADDR_CMP(&DR(oi
), &iph
->ip_src
)
936 && IPV4_ADDR_CMP(&BDR(oi
), &iph
->ip_src
))
937 OSPF_NSM_TIMER_ON (nbr
->t_hello_reply
, ospf_hello_reply_timer
,
938 OSPF_HELLO_REPLY_DELAY
);
940 /* on NBMA network type, it happens to receive bidirectional Hello packet
941 without advance 1-Way Received event.
942 To avoid incorrect DR-seletion, raise 1-Way Received event.*/
943 if (oi
->type
== OSPF_IFTYPE_NBMA
&&
944 (old_state
== NSM_Down
|| old_state
== NSM_Attempt
))
946 OSPF_NSM_EVENT_SCHEDULE (nbr
, NSM_OneWayReceived
);
947 nbr
->priority
= hello
->priority
;
948 nbr
->d_router
= hello
->d_router
;
949 nbr
->bd_router
= hello
->bd_router
;
953 if (ospf_nbr_bidirectional (&oi
->ospf
->router_id
, hello
->neighbors
,
954 size
- OSPF_HELLO_MIN_SIZE
))
956 OSPF_NSM_EVENT_SCHEDULE (nbr
, NSM_TwoWayReceived
);
957 nbr
->options
|= hello
->options
;
961 OSPF_NSM_EVENT_SCHEDULE (nbr
, NSM_OneWayReceived
);
962 /* Set neighbor information. */
963 nbr
->priority
= hello
->priority
;
964 nbr
->d_router
= hello
->d_router
;
965 nbr
->bd_router
= hello
->bd_router
;
969 /* If neighbor itself declares DR and no BDR exists,
970 cause event BackupSeen */
971 if (IPV4_ADDR_SAME (&nbr
->address
.u
.prefix4
, &hello
->d_router
))
972 if (hello
->bd_router
.s_addr
== 0 && oi
->state
== ISM_Waiting
)
973 OSPF_ISM_EVENT_SCHEDULE (oi
, ISM_BackupSeen
);
975 /* neighbor itself declares BDR. */
976 if (oi
->state
== ISM_Waiting
&&
977 IPV4_ADDR_SAME (&nbr
->address
.u
.prefix4
, &hello
->bd_router
))
978 OSPF_ISM_EVENT_SCHEDULE (oi
, ISM_BackupSeen
);
980 /* had not previously. */
981 if ((IPV4_ADDR_SAME (&nbr
->address
.u
.prefix4
, &hello
->d_router
) &&
982 IPV4_ADDR_CMP (&nbr
->address
.u
.prefix4
, &nbr
->d_router
)) ||
983 (IPV4_ADDR_CMP (&nbr
->address
.u
.prefix4
, &hello
->d_router
) &&
984 IPV4_ADDR_SAME (&nbr
->address
.u
.prefix4
, &nbr
->d_router
)))
985 OSPF_ISM_EVENT_SCHEDULE (oi
, ISM_NeighborChange
);
987 /* had not previously. */
988 if ((IPV4_ADDR_SAME (&nbr
->address
.u
.prefix4
, &hello
->bd_router
) &&
989 IPV4_ADDR_CMP (&nbr
->address
.u
.prefix4
, &nbr
->bd_router
)) ||
990 (IPV4_ADDR_CMP (&nbr
->address
.u
.prefix4
, &hello
->bd_router
) &&
991 IPV4_ADDR_SAME (&nbr
->address
.u
.prefix4
, &nbr
->bd_router
)))
992 OSPF_ISM_EVENT_SCHEDULE (oi
, ISM_NeighborChange
);
994 /* Neighbor priority check. */
995 if (nbr
->priority
>= 0 && nbr
->priority
!= hello
->priority
)
996 OSPF_ISM_EVENT_SCHEDULE (oi
, ISM_NeighborChange
);
998 /* Set neighbor information. */
999 nbr
->priority
= hello
->priority
;
1000 nbr
->d_router
= hello
->d_router
;
1001 nbr
->bd_router
= hello
->bd_router
;
1004 /* Save DD flags/options/Seqnum received. */
1006 ospf_db_desc_save_current (struct ospf_neighbor
*nbr
,
1007 struct ospf_db_desc
*dd
)
1009 nbr
->last_recv
.flags
= dd
->flags
;
1010 nbr
->last_recv
.options
= dd
->options
;
1011 nbr
->last_recv
.dd_seqnum
= ntohl (dd
->dd_seqnum
);
1014 /* Process rest of DD packet. */
1016 ospf_db_desc_proc (struct stream
*s
, struct ospf_interface
*oi
,
1017 struct ospf_neighbor
*nbr
, struct ospf_db_desc
*dd
,
1020 struct ospf_lsa
*new, *find
;
1021 struct lsa_header
*lsah
;
1023 stream_forward_getp (s
, OSPF_DB_DESC_MIN_SIZE
);
1024 for (size
-= OSPF_DB_DESC_MIN_SIZE
;
1025 size
>= OSPF_LSA_HEADER_SIZE
; size
-= OSPF_LSA_HEADER_SIZE
)
1027 lsah
= (struct lsa_header
*) STREAM_PNT (s
);
1028 stream_forward_getp (s
, OSPF_LSA_HEADER_SIZE
);
1030 /* Unknown LS type. */
1031 if (lsah
->type
< OSPF_MIN_LSA
|| lsah
->type
>= OSPF_MAX_LSA
)
1033 zlog_warn ("Packet [DD:RECV]: Unknown LS type %d.", lsah
->type
);
1034 OSPF_NSM_EVENT_SCHEDULE (nbr
, NSM_SeqNumberMismatch
);
1038 #ifdef HAVE_OPAQUE_LSA
1039 if (IS_OPAQUE_LSA (lsah
->type
)
1040 && ! CHECK_FLAG (nbr
->options
, OSPF_OPTION_O
))
1042 zlog_warn ("LSA[Type%d:%s]: Opaque capability mismatch?", lsah
->type
, inet_ntoa (lsah
->id
));
1043 OSPF_NSM_EVENT_SCHEDULE (nbr
, NSM_SeqNumberMismatch
);
1046 #endif /* HAVE_OPAQUE_LSA */
1050 case OSPF_AS_EXTERNAL_LSA
:
1051 #ifdef HAVE_OPAQUE_LSA
1052 case OSPF_OPAQUE_AS_LSA
:
1053 #endif /* HAVE_OPAQUE_LSA */
1054 /* Check for stub area. Reject if AS-External from stub but
1055 allow if from NSSA. */
1056 if (oi
->area
->external_routing
== OSPF_AREA_STUB
)
1058 zlog_warn ("Packet [DD:RECV]: LSA[Type%d:%s] from %s area.",
1059 lsah
->type
, inet_ntoa (lsah
->id
),
1060 (oi
->area
->external_routing
== OSPF_AREA_STUB
) ?\
1062 OSPF_NSM_EVENT_SCHEDULE (nbr
, NSM_SeqNumberMismatch
);
1070 /* Create LS-request object. */
1071 new = ospf_ls_request_new (lsah
);
1073 /* Lookup received LSA, then add LS request list. */
1074 find
= ospf_lsa_lookup_by_header (oi
->area
, lsah
);
1076 /* ospf_lsa_more_recent is fine with NULL pointers */
1077 switch (ospf_lsa_more_recent (find
, new))
1080 /* Neighbour has a more recent LSA, we must request it */
1081 ospf_ls_request_add (nbr
, new);
1083 /* If we have a copy of this LSA, it's either less recent
1084 * and we're requesting it from neighbour (the case above), or
1085 * it's as recent and we both have same copy (this case).
1087 * In neither of these two cases is there any point in
1088 * describing our copy of the LSA to the neighbour in a
1089 * DB-Summary packet, if we're still intending to do so.
1091 * See: draft-ogier-ospf-dbex-opt-00.txt, describing the
1092 * backward compatible optimisation to OSPF DB Exchange /
1093 * DB Description process implemented here.
1096 ospf_lsdb_delete (&nbr
->db_sum
, find
);
1097 ospf_lsa_discard (new);
1100 /* We have the more recent copy, nothing specific to do:
1101 * - no need to request neighbours stale copy
1102 * - must leave DB summary list copy alone
1104 if (IS_DEBUG_OSPF_EVENT
)
1105 zlog_debug ("Packet [DD:RECV]: LSA received Type %d, "
1106 "ID %s is not recent.", lsah
->type
, inet_ntoa (lsah
->id
));
1107 ospf_lsa_discard (new);
1112 if (IS_SET_DD_MS (nbr
->dd_flags
))
1116 /* Both sides have no More, then we're done with Exchange */
1117 if (!IS_SET_DD_M (dd
->flags
) && !IS_SET_DD_M (nbr
->dd_flags
))
1118 OSPF_NSM_EVENT_SCHEDULE (nbr
, NSM_ExchangeDone
);
1120 ospf_db_desc_send (nbr
);
1125 nbr
->dd_seqnum
= ntohl (dd
->dd_seqnum
);
1127 /* Send DD packet in reply.
1129 * Must be done to acknowledge the Master's DD, regardless of
1130 * whether we have more LSAs ourselves to describe.
1132 * This function will clear the 'More' bit, if after this DD
1133 * we have no more LSAs to describe to the master..
1135 ospf_db_desc_send (nbr
);
1137 /* Slave can raise ExchangeDone now, if master is also done */
1138 if (!IS_SET_DD_M (dd
->flags
) && !IS_SET_DD_M (nbr
->dd_flags
))
1139 OSPF_NSM_EVENT_SCHEDULE (nbr
, NSM_ExchangeDone
);
1142 /* Save received neighbor values from DD. */
1143 ospf_db_desc_save_current (nbr
, dd
);
1147 ospf_db_desc_is_dup (struct ospf_db_desc
*dd
, struct ospf_neighbor
*nbr
)
1149 /* Is DD duplicated? */
1150 if (dd
->options
== nbr
->last_recv
.options
&&
1151 dd
->flags
== nbr
->last_recv
.flags
&&
1152 dd
->dd_seqnum
== htonl (nbr
->last_recv
.dd_seqnum
))
1158 /* OSPF Database Description message read -- RFC2328 Section 10.6. */
1160 ospf_db_desc (struct ip
*iph
, struct ospf_header
*ospfh
,
1161 struct stream
*s
, struct ospf_interface
*oi
, u_int16_t size
)
1163 struct ospf_db_desc
*dd
;
1164 struct ospf_neighbor
*nbr
;
1166 /* Increment statistics. */
1169 dd
= (struct ospf_db_desc
*) STREAM_PNT (s
);
1171 nbr
= ospf_nbr_lookup (oi
, iph
, ospfh
);
1174 zlog_warn ("Packet[DD]: Unknown Neighbor %s",
1175 inet_ntoa (ospfh
->router_id
));
1180 if ((OSPF_IF_PARAM (oi
, mtu_ignore
) == 0) &&
1181 (ntohs (dd
->mtu
) > oi
->ifp
->mtu
))
1183 zlog_warn ("Packet[DD]: Neighbor %s MTU %u is larger than [%s]'s MTU %u",
1184 inet_ntoa (nbr
->router_id
), ntohs (dd
->mtu
),
1185 IF_NAME (oi
), oi
->ifp
->mtu
);
1190 * XXX HACK by Hasso Tepper. Setting N/P bit in NSSA area DD packets is not
1191 * required. In fact at least JunOS sends DD packets with P bit clear.
1192 * Until proper solution is developped, this hack should help.
1194 * Update: According to the RFCs, N bit is specified /only/ for Hello
1195 * options, unfortunately its use in DD options is not specified. Hence some
1196 * implementations follow E-bit semantics and set it in DD options, and some
1197 * treat it as unspecified and hence follow the directive "default for
1198 * options is clear", ie unset.
1200 * Reset the flag, as ospfd follows E-bit semantics.
1202 if ( (oi
->area
->external_routing
== OSPF_AREA_NSSA
)
1203 && (CHECK_FLAG (nbr
->options
, OSPF_OPTION_NP
))
1204 && (!CHECK_FLAG (dd
->options
, OSPF_OPTION_NP
)) )
1206 if (IS_DEBUG_OSPF_EVENT
)
1207 zlog_debug ("Packet[DD]: Neighbour %s: Has NSSA capability, sends with N bit clear in DD options",
1208 inet_ntoa (nbr
->router_id
) );
1209 SET_FLAG (dd
->options
, OSPF_OPTION_NP
);
1212 #ifdef REJECT_IF_TBIT_ON
1213 if (CHECK_FLAG (dd
->options
, OSPF_OPTION_T
))
1216 * In Hello protocol, optional capability must have checked
1217 * to prevent this T-bit enabled router be my neighbor.
1219 zlog_warn ("Packet[DD]: Neighbor %s: T-bit on?", inet_ntoa (nbr
->router_id
));
1222 #endif /* REJECT_IF_TBIT_ON */
1224 #ifdef HAVE_OPAQUE_LSA
1225 if (CHECK_FLAG (dd
->options
, OSPF_OPTION_O
)
1226 && !CHECK_FLAG (oi
->ospf
->config
, OSPF_OPAQUE_CAPABLE
))
1229 * This node is not configured to handle O-bit, for now.
1230 * Clear it to ignore unsupported capability proposed by neighbor.
1232 UNSET_FLAG (dd
->options
, OSPF_OPTION_O
);
1234 #endif /* HAVE_OPAQUE_LSA */
1236 /* Add event to thread. */
1237 OSPF_NSM_EVENT_SCHEDULE (nbr
, NSM_PacketReceived
);
1239 /* Process DD packet by neighbor status. */
1245 zlog_warn ("Packet[DD]: Neighbor %s state is %s, packet discarded.",
1246 inet_ntoa(nbr
->router_id
),
1247 LOOKUP (ospf_nsm_state_msg
, nbr
->state
));
1250 OSPF_NSM_EVENT_EXECUTE (nbr
, NSM_TwoWayReceived
);
1251 /* If the new state is ExStart, the processing of the current
1252 packet should then continue in this new state by falling
1253 through to case ExStart below. */
1254 if (nbr
->state
!= NSM_ExStart
)
1258 if ((IS_SET_DD_ALL (dd
->flags
) == OSPF_DD_FLAG_ALL
) &&
1259 (size
== OSPF_DB_DESC_MIN_SIZE
))
1261 if (IPV4_ADDR_CMP (&nbr
->router_id
, &oi
->ospf
->router_id
) > 0)
1263 /* We're Slave---obey */
1264 zlog_info ("Packet[DD]: Neighbor %s Negotiation done (Slave).",
1265 inet_ntoa(nbr
->router_id
));
1266 nbr
->dd_seqnum
= ntohl (dd
->dd_seqnum
);
1269 UNSET_FLAG (nbr
->dd_flags
, (OSPF_DD_FLAG_MS
|OSPF_DD_FLAG_I
));
1273 /* We're Master, ignore the initial DBD from Slave */
1274 zlog_info ("Packet[DD]: Neighbor %s: Initial DBD from Slave, "
1275 "ignoring.", inet_ntoa(nbr
->router_id
));
1279 /* Ack from the Slave */
1280 else if (!IS_SET_DD_MS (dd
->flags
) && !IS_SET_DD_I (dd
->flags
) &&
1281 ntohl (dd
->dd_seqnum
) == nbr
->dd_seqnum
&&
1282 IPV4_ADDR_CMP (&nbr
->router_id
, &oi
->ospf
->router_id
) < 0)
1284 zlog_info ("Packet[DD]: Neighbor %s Negotiation done (Master).",
1285 inet_ntoa(nbr
->router_id
));
1286 /* Reset I, leaving MS */
1287 UNSET_FLAG (nbr
->dd_flags
, OSPF_DD_FLAG_I
);
1291 zlog_warn ("Packet[DD]: Neighbor %s Negotiation fails.",
1292 inet_ntoa(nbr
->router_id
));
1296 /* This is where the real Options are saved */
1297 nbr
->options
= dd
->options
;
1299 #ifdef HAVE_OPAQUE_LSA
1300 if (CHECK_FLAG (oi
->ospf
->config
, OSPF_OPAQUE_CAPABLE
))
1302 if (IS_DEBUG_OSPF_EVENT
)
1303 zlog_debug ("Neighbor[%s] is %sOpaque-capable.",
1304 inet_ntoa (nbr
->router_id
),
1305 CHECK_FLAG (nbr
->options
, OSPF_OPTION_O
) ? "" : "NOT ");
1307 if (! CHECK_FLAG (nbr
->options
, OSPF_OPTION_O
)
1308 && IPV4_ADDR_SAME (&DR (oi
), &nbr
->address
.u
.prefix4
))
1310 zlog_warn ("DR-neighbor[%s] is NOT opaque-capable; "
1311 "Opaque-LSAs cannot be reliably advertised "
1313 inet_ntoa (nbr
->router_id
));
1314 /* This situation is undesirable, but not a real error. */
1317 #endif /* HAVE_OPAQUE_LSA */
1319 OSPF_NSM_EVENT_EXECUTE (nbr
, NSM_NegotiationDone
);
1321 /* continue processing rest of packet. */
1322 ospf_db_desc_proc (s
, oi
, nbr
, dd
, size
);
1325 if (ospf_db_desc_is_dup (dd
, nbr
))
1327 if (IS_SET_DD_MS (nbr
->dd_flags
))
1328 /* Master: discard duplicated DD packet. */
1329 zlog_info ("Packet[DD] (Master): Neighbor %s packet duplicated.",
1330 inet_ntoa (nbr
->router_id
));
1332 /* Slave: cause to retransmit the last Database Description. */
1334 zlog_info ("Packet[DD] [Slave]: Neighbor %s packet duplicated.",
1335 inet_ntoa (nbr
->router_id
));
1336 ospf_db_desc_resend (nbr
);
1341 /* Otherwise DD packet should be checked. */
1342 /* Check Master/Slave bit mismatch */
1343 if (IS_SET_DD_MS (dd
->flags
) != IS_SET_DD_MS (nbr
->last_recv
.flags
))
1345 zlog_warn ("Packet[DD]: Neighbor %s MS-bit mismatch.",
1346 inet_ntoa(nbr
->router_id
));
1347 OSPF_NSM_EVENT_SCHEDULE (nbr
, NSM_SeqNumberMismatch
);
1348 if (IS_DEBUG_OSPF_EVENT
)
1349 zlog_debug ("Packet[DD]: dd->flags=%d, nbr->dd_flags=%d",
1350 dd
->flags
, nbr
->dd_flags
);
1354 /* Check initialize bit is set. */
1355 if (IS_SET_DD_I (dd
->flags
))
1357 zlog_info ("Packet[DD]: Neighbor %s I-bit set.",
1358 inet_ntoa(nbr
->router_id
));
1359 OSPF_NSM_EVENT_SCHEDULE (nbr
, NSM_SeqNumberMismatch
);
1363 /* Check DD Options. */
1364 if (dd
->options
!= nbr
->options
)
1366 #ifdef ORIGINAL_CODING
1367 /* Save the new options for debugging */
1368 nbr
->options
= dd
->options
;
1369 #endif /* ORIGINAL_CODING */
1370 zlog_warn ("Packet[DD]: Neighbor %s options mismatch.",
1371 inet_ntoa(nbr
->router_id
));
1372 OSPF_NSM_EVENT_SCHEDULE (nbr
, NSM_SeqNumberMismatch
);
1376 /* Check DD sequence number. */
1377 if ((IS_SET_DD_MS (nbr
->dd_flags
) &&
1378 ntohl (dd
->dd_seqnum
) != nbr
->dd_seqnum
) ||
1379 (!IS_SET_DD_MS (nbr
->dd_flags
) &&
1380 ntohl (dd
->dd_seqnum
) != nbr
->dd_seqnum
+ 1))
1382 zlog_warn ("Packet[DD]: Neighbor %s sequence number mismatch.",
1383 inet_ntoa(nbr
->router_id
));
1384 OSPF_NSM_EVENT_SCHEDULE (nbr
, NSM_SeqNumberMismatch
);
1388 /* Continue processing rest of packet. */
1389 ospf_db_desc_proc (s
, oi
, nbr
, dd
, size
);
1393 if (ospf_db_desc_is_dup (dd
, nbr
))
1395 if (IS_SET_DD_MS (nbr
->dd_flags
))
1397 /* Master should discard duplicate DD packet. */
1398 zlog_info ("Packet[DD]: Neighbor %s duplicated, "
1399 "packet discarded.",
1400 inet_ntoa(nbr
->router_id
));
1405 struct timeval t
, now
;
1406 quagga_gettime (QUAGGA_CLK_MONOTONIC
, &now
);
1407 t
= tv_sub (now
, nbr
->last_send_ts
);
1408 if (tv_cmp (t
, int2tv (nbr
->v_inactivity
)) < 0)
1410 /* In states Loading and Full the slave must resend
1411 its last Database Description packet in response to
1412 duplicate Database Description packets received
1413 from the master. For this reason the slave must
1414 wait RouterDeadInterval seconds before freeing the
1415 last Database Description packet. Reception of a
1416 Database Description packet from the master after
1417 this interval will generate a SeqNumberMismatch
1418 neighbor event. RFC2328 Section 10.8 */
1419 ospf_db_desc_resend (nbr
);
1425 OSPF_NSM_EVENT_SCHEDULE (nbr
, NSM_SeqNumberMismatch
);
1428 zlog_warn ("Packet[DD]: Neighbor %s NSM illegal status %u.",
1429 inet_ntoa(nbr
->router_id
), nbr
->state
);
1434 #define OSPF_LSA_KEY_SIZE 12 /* type(4) + id(4) + ar(4) */
1436 /* OSPF Link State Request Read -- RFC2328 Section 10.7. */
1438 ospf_ls_req (struct ip
*iph
, struct ospf_header
*ospfh
,
1439 struct stream
*s
, struct ospf_interface
*oi
, u_int16_t size
)
1441 struct ospf_neighbor
*nbr
;
1443 struct in_addr ls_id
;
1444 struct in_addr adv_router
;
1445 struct ospf_lsa
*find
;
1446 struct list
*ls_upd
;
1447 unsigned int length
;
1449 /* Increment statistics. */
1452 nbr
= ospf_nbr_lookup (oi
, iph
, ospfh
);
1455 zlog_warn ("Link State Request: Unknown Neighbor %s.",
1456 inet_ntoa (ospfh
->router_id
));
1460 /* Add event to thread. */
1461 OSPF_NSM_EVENT_SCHEDULE (nbr
, NSM_PacketReceived
);
1463 /* Neighbor State should be Exchange or later. */
1464 if (nbr
->state
!= NSM_Exchange
&&
1465 nbr
->state
!= NSM_Loading
&&
1466 nbr
->state
!= NSM_Full
)
1468 zlog_warn ("Link State Request received from %s: "
1469 "Neighbor state is %s, packet discarded.",
1470 inet_ntoa (ospfh
->router_id
),
1471 LOOKUP (ospf_nsm_state_msg
, nbr
->state
));
1475 /* Send Link State Update for ALL requested LSAs. */
1476 ls_upd
= list_new ();
1477 length
= OSPF_HEADER_SIZE
+ OSPF_LS_UPD_MIN_SIZE
;
1479 while (size
>= OSPF_LSA_KEY_SIZE
)
1481 /* Get one slice of Link State Request. */
1482 ls_type
= stream_getl (s
);
1483 ls_id
.s_addr
= stream_get_ipv4 (s
);
1484 adv_router
.s_addr
= stream_get_ipv4 (s
);
1486 /* Verify LSA type. */
1487 if (ls_type
< OSPF_MIN_LSA
|| ls_type
>= OSPF_MAX_LSA
)
1489 OSPF_NSM_EVENT_SCHEDULE (nbr
, NSM_BadLSReq
);
1490 list_delete (ls_upd
);
1494 /* Search proper LSA in LSDB. */
1495 find
= ospf_lsa_lookup (oi
->area
, ls_type
, ls_id
, adv_router
);
1498 OSPF_NSM_EVENT_SCHEDULE (nbr
, NSM_BadLSReq
);
1499 list_delete (ls_upd
);
1503 /* Packet overflows MTU size, send immediately. */
1504 if (length
+ ntohs (find
->data
->length
) > ospf_packet_max (oi
))
1506 if (oi
->type
== OSPF_IFTYPE_NBMA
)
1507 ospf_ls_upd_send (nbr
, ls_upd
, OSPF_SEND_PACKET_DIRECT
);
1509 ospf_ls_upd_send (nbr
, ls_upd
, OSPF_SEND_PACKET_INDIRECT
);
1511 /* Only remove list contents. Keep ls_upd. */
1512 list_delete_all_node (ls_upd
);
1514 length
= OSPF_HEADER_SIZE
+ OSPF_LS_UPD_MIN_SIZE
;
1517 /* Append LSA to update list. */
1518 listnode_add (ls_upd
, find
);
1519 length
+= ntohs (find
->data
->length
);
1521 size
-= OSPF_LSA_KEY_SIZE
;
1524 /* Send rest of Link State Update. */
1525 if (listcount (ls_upd
) > 0)
1527 if (oi
->type
== OSPF_IFTYPE_NBMA
)
1528 ospf_ls_upd_send (nbr
, ls_upd
, OSPF_SEND_PACKET_DIRECT
);
1530 ospf_ls_upd_send (nbr
, ls_upd
, OSPF_SEND_PACKET_INDIRECT
);
1532 list_delete (ls_upd
);
1538 /* Get the list of LSAs from Link State Update packet.
1539 And process some validation -- RFC2328 Section 13. (1)-(2). */
1540 static struct list
*
1541 ospf_ls_upd_list_lsa (struct ospf_neighbor
*nbr
, struct stream
*s
,
1542 struct ospf_interface
*oi
, size_t size
)
1544 u_int16_t count
, sum
;
1546 struct lsa_header
*lsah
;
1547 struct ospf_lsa
*lsa
;
1552 count
= stream_getl (s
);
1553 size
-= OSPF_LS_UPD_MIN_SIZE
; /* # LSAs */
1555 for (; size
>= OSPF_LSA_HEADER_SIZE
&& count
> 0;
1556 size
-= length
, stream_forward_getp (s
, length
), count
--)
1558 lsah
= (struct lsa_header
*) STREAM_PNT (s
);
1559 length
= ntohs (lsah
->length
);
1563 zlog_warn ("Link State Update: LSA length exceeds packet size.");
1567 /* Validate the LSA's LS checksum. */
1568 sum
= lsah
->checksum
;
1569 if (sum
!= ospf_lsa_checksum (lsah
))
1571 zlog_warn ("Link State Update: LSA checksum error %x, %x.",
1572 sum
, lsah
->checksum
);
1576 /* Examine the LSA's LS type. */
1577 if (lsah
->type
< OSPF_MIN_LSA
|| lsah
->type
>= OSPF_MAX_LSA
)
1579 zlog_warn ("Link State Update: Unknown LS type %d", lsah
->type
);
1584 * What if the received LSA's age is greater than MaxAge?
1585 * Treat it as a MaxAge case -- endo.
1587 if (ntohs (lsah
->ls_age
) > OSPF_LSA_MAXAGE
)
1588 lsah
->ls_age
= htons (OSPF_LSA_MAXAGE
);
1590 #ifdef HAVE_OPAQUE_LSA
1591 if (CHECK_FLAG (nbr
->options
, OSPF_OPTION_O
))
1593 #ifdef STRICT_OBIT_USAGE_CHECK
1594 if ((IS_OPAQUE_LSA(lsah
->type
) &&
1595 ! CHECK_FLAG (lsah
->options
, OSPF_OPTION_O
))
1596 || (! IS_OPAQUE_LSA(lsah
->type
) &&
1597 CHECK_FLAG (lsah
->options
, OSPF_OPTION_O
)))
1600 * This neighbor must know the exact usage of O-bit;
1601 * the bit will be set in Type-9,10,11 LSAs only.
1603 zlog_warn ("LSA[Type%d:%s]: O-bit abuse?", lsah
->type
, inet_ntoa (lsah
->id
));
1606 #endif /* STRICT_OBIT_USAGE_CHECK */
1608 /* Do not take in AS External Opaque-LSAs if we are a stub. */
1609 if (lsah
->type
== OSPF_OPAQUE_AS_LSA
1610 && nbr
->oi
->area
->external_routing
!= OSPF_AREA_DEFAULT
)
1612 if (IS_DEBUG_OSPF_EVENT
)
1613 zlog_debug ("LSA[Type%d:%s]: We are a stub, don't take this LSA.", lsah
->type
, inet_ntoa (lsah
->id
));
1617 else if (IS_OPAQUE_LSA(lsah
->type
))
1619 zlog_warn ("LSA[Type%d:%s]: Opaque capability mismatch?", lsah
->type
, inet_ntoa (lsah
->id
));
1622 #endif /* HAVE_OPAQUE_LSA */
1624 /* Create OSPF LSA instance. */
1625 lsa
= ospf_lsa_new ();
1627 /* We may wish to put some error checking if type NSSA comes in
1628 and area not in NSSA mode */
1631 case OSPF_AS_EXTERNAL_LSA
:
1632 #ifdef HAVE_OPAQUE_LSA
1633 case OSPF_OPAQUE_AS_LSA
:
1636 case OSPF_OPAQUE_LINK_LSA
:
1637 lsa
->oi
= oi
; /* Remember incoming interface for flooding control. */
1639 #endif /* HAVE_OPAQUE_LSA */
1641 lsa
->area
= oi
->area
;
1645 lsa
->data
= ospf_lsa_data_new (length
);
1646 memcpy (lsa
->data
, lsah
, length
);
1648 if (IS_DEBUG_OSPF_EVENT
)
1649 zlog_debug("LSA[Type%d:%s]: %p new LSA created with Link State Update",
1650 lsa
->data
->type
, inet_ntoa (lsa
->data
->id
), lsa
);
1651 listnode_add (lsas
, lsa
);
1657 /* Cleanup Update list. */
1659 ospf_upd_list_clean (struct list
*lsas
)
1661 struct listnode
*node
, *nnode
;
1662 struct ospf_lsa
*lsa
;
1664 for (ALL_LIST_ELEMENTS (lsas
, node
, nnode
, lsa
))
1665 ospf_lsa_discard (lsa
);
1670 /* OSPF Link State Update message read -- RFC2328 Section 13. */
1672 ospf_ls_upd (struct ip
*iph
, struct ospf_header
*ospfh
,
1673 struct stream
*s
, struct ospf_interface
*oi
, u_int16_t size
)
1675 struct ospf_neighbor
*nbr
;
1677 struct listnode
*node
, *nnode
;
1678 struct ospf_lsa
*lsa
= NULL
;
1679 /* unsigned long ls_req_found = 0; */
1681 /* Dis-assemble the stream, update each entry, re-encapsulate for flooding */
1683 /* Increment statistics. */
1686 /* Check neighbor. */
1687 nbr
= ospf_nbr_lookup (oi
, iph
, ospfh
);
1690 zlog_warn ("Link State Update: Unknown Neighbor %s on int: %s",
1691 inet_ntoa (ospfh
->router_id
), IF_NAME (oi
));
1695 /* Add event to thread. */
1696 OSPF_NSM_EVENT_SCHEDULE (nbr
, NSM_PacketReceived
);
1698 /* Check neighbor state. */
1699 if (nbr
->state
< NSM_Exchange
)
1701 zlog_warn ("Link State Update: "
1702 "Neighbor[%s] state %s is less than Exchange",
1703 inet_ntoa (ospfh
->router_id
),
1704 LOOKUP(ospf_nsm_state_msg
, nbr
->state
));
1708 /* Get list of LSAs from Link State Update packet. - Also perorms Stages
1709 * 1 (validate LSA checksum) and 2 (check for LSA consistent type)
1712 lsas
= ospf_ls_upd_list_lsa (nbr
, s
, oi
, size
);
1714 #ifdef HAVE_OPAQUE_LSA
1716 * If self-originated Opaque-LSAs that have flooded before restart
1717 * are contained in the received LSUpd message, corresponding LSReq
1718 * messages to be sent may have to be modified.
1719 * To eliminate possible race conditions such that flushing and normal
1720 * updating for the same LSA would take place alternately, this trick
1721 * must be done before entering to the loop below.
1723 /* XXX: Why is this Opaque specific? Either our core code is deficient
1724 * and this should be fixed generally, or Opaque is inventing strawman
1726 ospf_opaque_adjust_lsreq (nbr
, lsas
);
1727 #endif /* HAVE_OPAQUE_LSA */
1729 #define DISCARD_LSA(L,N) {\
1730 if (IS_DEBUG_OSPF_EVENT) \
1731 zlog_debug ("ospf_lsa_discard() in ospf_ls_upd() point %d: lsa %p Type-%d", N, lsa, (int) lsa->data->type); \
1732 ospf_lsa_discard (L); \
1735 /* Process each LSA received in the one packet. */
1736 for (ALL_LIST_ELEMENTS (lsas
, node
, nnode
, lsa
))
1738 struct ospf_lsa
*ls_ret
, *current
;
1741 if (IS_DEBUG_OSPF_NSSA
)
1743 char buf1
[INET_ADDRSTRLEN
];
1744 char buf2
[INET_ADDRSTRLEN
];
1745 char buf3
[INET_ADDRSTRLEN
];
1747 zlog_debug("LSA Type-%d from %s, ID: %s, ADV: %s",
1749 inet_ntop (AF_INET
, &ospfh
->router_id
,
1750 buf1
, INET_ADDRSTRLEN
),
1751 inet_ntop (AF_INET
, &lsa
->data
->id
,
1752 buf2
, INET_ADDRSTRLEN
),
1753 inet_ntop (AF_INET
, &lsa
->data
->adv_router
,
1754 buf3
, INET_ADDRSTRLEN
));
1757 listnode_delete (lsas
, lsa
); /* We don't need it in list anymore */
1759 /* Validate Checksum - Done above by ospf_ls_upd_list_lsa() */
1761 /* LSA Type - Done above by ospf_ls_upd_list_lsa() */
1763 /* Do not take in AS External LSAs if we are a stub or NSSA. */
1765 /* Do not take in AS NSSA if this neighbor and we are not NSSA */
1767 /* Do take in Type-7's if we are an NSSA */
1769 /* If we are also an ABR, later translate them to a Type-5 packet */
1771 /* Later, an NSSA Re-fresh can Re-fresh Type-7's and an ABR will
1772 translate them to a separate Type-5 packet. */
1774 if (lsa
->data
->type
== OSPF_AS_EXTERNAL_LSA
)
1775 /* Reject from STUB or NSSA */
1776 if (nbr
->oi
->area
->external_routing
!= OSPF_AREA_DEFAULT
)
1778 if (IS_DEBUG_OSPF_NSSA
)
1779 zlog_debug("Incoming External LSA Discarded: We are NSSA/STUB Area");
1780 DISCARD_LSA (lsa
, 1);
1783 if (lsa
->data
->type
== OSPF_AS_NSSA_LSA
)
1784 if (nbr
->oi
->area
->external_routing
!= OSPF_AREA_NSSA
)
1786 if (IS_DEBUG_OSPF_NSSA
)
1787 zlog_debug("Incoming NSSA LSA Discarded: Not NSSA Area");
1788 DISCARD_LSA (lsa
,2);
1791 /* Find the LSA in the current database. */
1793 current
= ospf_lsa_lookup_by_header (oi
->area
, lsa
->data
);
1795 /* If the LSA's LS age is equal to MaxAge, and there is currently
1796 no instance of the LSA in the router's link state database,
1797 and none of router's neighbors are in states Exchange or Loading,
1798 then take the following actions. */
1800 if (IS_LSA_MAXAGE (lsa
) && !current
&&
1801 (ospf_nbr_count (oi
, NSM_Exchange
) +
1802 ospf_nbr_count (oi
, NSM_Loading
)) == 0)
1804 /* Response Link State Acknowledgment. */
1805 ospf_ls_ack_send (nbr
, lsa
);
1808 zlog_info ("Link State Update[%s]: LS age is equal to MaxAge.",
1810 DISCARD_LSA (lsa
, 3);
1813 #ifdef HAVE_OPAQUE_LSA
1814 if (IS_OPAQUE_LSA (lsa
->data
->type
)
1815 && IPV4_ADDR_SAME (&lsa
->data
->adv_router
, &oi
->ospf
->router_id
))
1818 * Even if initial flushing seems to be completed, there might
1819 * be a case that self-originated LSA with MaxAge still remain
1820 * in the routing domain.
1821 * Just send an LSAck message to cease retransmission.
1823 if (IS_LSA_MAXAGE (lsa
))
1825 zlog_warn ("LSA[%s]: Boomerang effect?", dump_lsa_key (lsa
));
1826 ospf_ls_ack_send (nbr
, lsa
);
1827 ospf_lsa_discard (lsa
);
1829 if (current
!= NULL
&& ! IS_LSA_MAXAGE (current
))
1830 ospf_opaque_lsa_refresh_schedule (current
);
1835 * If an instance of self-originated Opaque-LSA is not found
1836 * in the LSDB, there are some possible cases here.
1838 * 1) This node lost opaque-capability after restart.
1839 * 2) Else, a part of opaque-type is no more supported.
1840 * 3) Else, a part of opaque-id is no more supported.
1842 * Anyway, it is still this node's responsibility to flush it.
1843 * Otherwise, the LSA instance remains in the routing domain
1844 * until its age reaches to MaxAge.
1846 /* XXX: We should deal with this for *ALL* LSAs, not just opaque */
1847 if (current
== NULL
)
1849 if (IS_DEBUG_OSPF_EVENT
)
1850 zlog_debug ("LSA[%s]: Previously originated Opaque-LSA,"
1851 "not found in the LSDB.", dump_lsa_key (lsa
));
1853 SET_FLAG (lsa
->flags
, OSPF_LSA_SELF
);
1855 ospf_opaque_self_originated_lsa_received (nbr
, lsa
);
1856 ospf_ls_ack_send (nbr
, lsa
);
1861 #endif /* HAVE_OPAQUE_LSA */
1863 /* It might be happen that received LSA is self-originated network LSA, but
1864 * router ID is cahnged. So, we should check if LSA is a network-LSA whose
1865 * Link State ID is one of the router's own IP interface addresses but whose
1866 * Advertising Router is not equal to the router's own Router ID
1867 * According to RFC 2328 12.4.2 and 13.4 this LSA should be flushed.
1870 if(lsa
->data
->type
== OSPF_NETWORK_LSA
)
1872 struct listnode
*oinode
, *oinnode
;
1873 struct ospf_interface
*out_if
;
1876 for (ALL_LIST_ELEMENTS (oi
->ospf
->oiflist
, oinode
, oinnode
, out_if
))
1881 if((IPV4_ADDR_SAME(&out_if
->address
->u
.prefix4
, &lsa
->data
->id
)) &&
1882 (!(IPV4_ADDR_SAME(&oi
->ospf
->router_id
, &lsa
->data
->adv_router
))))
1884 if(out_if
->network_lsa_self
)
1886 ospf_lsa_flush_area(lsa
,out_if
->area
);
1887 if(IS_DEBUG_OSPF_EVENT
)
1888 zlog_debug ("ospf_lsa_discard() in ospf_ls_upd() point 9: lsa %p Type-%d",
1889 lsa
, (int) lsa
->data
->type
);
1890 ospf_lsa_discard (lsa
);
1900 /* (5) Find the instance of this LSA that is currently contained
1901 in the router's link state database. If there is no
1902 database copy, or the received LSA is more recent than
1903 the database copy the following steps must be performed. */
1905 if (current
== NULL
||
1906 (ret
= ospf_lsa_more_recent (current
, lsa
)) < 0)
1908 /* Actual flooding procedure. */
1909 if (ospf_flood (oi
->ospf
, nbr
, current
, lsa
) < 0) /* Trap NSSA later. */
1910 DISCARD_LSA (lsa
, 4);
1914 /* (6) Else, If there is an instance of the LSA on the sending
1915 neighbor's Link state request list, an error has occurred in
1916 the Database Exchange process. In this case, restart the
1917 Database Exchange process by generating the neighbor event
1918 BadLSReq for the sending neighbor and stop processing the
1919 Link State Update packet. */
1921 if (ospf_ls_request_lookup (nbr
, lsa
))
1923 OSPF_NSM_EVENT_SCHEDULE (nbr
, NSM_BadLSReq
);
1924 zlog_warn("LSA[%s] instance exists on Link state request list",
1927 /* Clean list of LSAs. */
1928 ospf_upd_list_clean (lsas
);
1929 /* this lsa is not on lsas list already. */
1930 ospf_lsa_discard (lsa
);
1934 /* If the received LSA is the same instance as the database copy
1935 (i.e., neither one is more recent) the following two steps
1936 should be performed: */
1940 /* If the LSA is listed in the Link state retransmission list
1941 for the receiving adjacency, the router itself is expecting
1942 an acknowledgment for this LSA. The router should treat the
1943 received LSA as an acknowledgment by removing the LSA from
1944 the Link state retransmission list. This is termed an
1945 "implied acknowledgment". */
1947 ls_ret
= ospf_ls_retransmit_lookup (nbr
, lsa
);
1951 ospf_ls_retransmit_delete (nbr
, ls_ret
);
1953 /* Delayed acknowledgment sent if advertisement received
1954 from Designated Router, otherwise do nothing. */
1955 if (oi
->state
== ISM_Backup
)
1956 if (NBR_IS_DR (nbr
))
1957 listnode_add (oi
->ls_ack
, ospf_lsa_lock (lsa
));
1959 DISCARD_LSA (lsa
, 5);
1962 /* Acknowledge the receipt of the LSA by sending a
1963 Link State Acknowledgment packet back out the receiving
1966 ospf_ls_ack_send (nbr
, lsa
);
1967 DISCARD_LSA (lsa
, 6);
1971 /* The database copy is more recent. If the database copy
1972 has LS age equal to MaxAge and LS sequence number equal to
1973 MaxSequenceNumber, simply discard the received LSA without
1974 acknowledging it. (In this case, the LSA's LS sequence number is
1975 wrapping, and the MaxSequenceNumber LSA must be completely
1976 flushed before any new LSA instance can be introduced). */
1978 else if (ret
> 0) /* Database copy is more recent */
1980 if (IS_LSA_MAXAGE (current
) &&
1981 current
->data
->ls_seqnum
== htonl (OSPF_MAX_SEQUENCE_NUMBER
))
1983 DISCARD_LSA (lsa
, 7);
1985 /* Otherwise, as long as the database copy has not been sent in a
1986 Link State Update within the last MinLSArrival seconds, send the
1987 database copy back to the sending neighbor, encapsulated within
1988 a Link State Update Packet. The Link State Update Packet should
1989 be sent directly to the neighbor. In so doing, do not put the
1990 database copy of the LSA on the neighbor's link state
1991 retransmission list, and do not acknowledge the received (less
1992 recent) LSA instance. */
1997 quagga_gettime (QUAGGA_CLK_MONOTONIC
, &now
);
1999 if (tv_cmp (tv_sub (now
, current
->tv_orig
),
2000 int2tv (OSPF_MIN_LS_ARRIVAL
)) >= 0)
2001 /* Trap NSSA type later.*/
2002 ospf_ls_upd_send_lsa (nbr
, current
, OSPF_SEND_PACKET_DIRECT
);
2003 DISCARD_LSA (lsa
, 8);
2009 assert (listcount (lsas
) == 0);
2013 /* OSPF Link State Acknowledgment message read -- RFC2328 Section 13.7. */
2015 ospf_ls_ack (struct ip
*iph
, struct ospf_header
*ospfh
,
2016 struct stream
*s
, struct ospf_interface
*oi
, u_int16_t size
)
2018 struct ospf_neighbor
*nbr
;
2020 /* increment statistics. */
2023 nbr
= ospf_nbr_lookup (oi
, iph
, ospfh
);
2026 zlog_warn ("Link State Acknowledgment: Unknown Neighbor %s.",
2027 inet_ntoa (ospfh
->router_id
));
2031 /* Add event to thread. */
2032 OSPF_NSM_EVENT_SCHEDULE (nbr
, NSM_PacketReceived
);
2034 if (nbr
->state
< NSM_Exchange
)
2036 zlog_warn ("Link State Acknowledgment: "
2037 "Neighbor[%s] state %s is less than Exchange",
2038 inet_ntoa (ospfh
->router_id
),
2039 LOOKUP(ospf_nsm_state_msg
, nbr
->state
));
2043 while (size
>= OSPF_LSA_HEADER_SIZE
)
2045 struct ospf_lsa
*lsa
, *lsr
;
2047 lsa
= ospf_lsa_new ();
2048 lsa
->data
= (struct lsa_header
*) STREAM_PNT (s
);
2050 /* lsah = (struct lsa_header *) STREAM_PNT (s); */
2051 size
-= OSPF_LSA_HEADER_SIZE
;
2052 stream_forward_getp (s
, OSPF_LSA_HEADER_SIZE
);
2054 if (lsa
->data
->type
< OSPF_MIN_LSA
|| lsa
->data
->type
>= OSPF_MAX_LSA
)
2057 ospf_lsa_discard (lsa
);
2061 lsr
= ospf_ls_retransmit_lookup (nbr
, lsa
);
2063 if (lsr
!= NULL
&& lsr
->data
->ls_seqnum
== lsa
->data
->ls_seqnum
)
2065 #ifdef HAVE_OPAQUE_LSA
2066 if (IS_OPAQUE_LSA (lsr
->data
->type
))
2067 ospf_opaque_ls_ack_received (nbr
, lsr
);
2068 #endif /* HAVE_OPAQUE_LSA */
2070 ospf_ls_retransmit_delete (nbr
, lsr
);
2074 ospf_lsa_discard (lsa
);
2080 static struct stream
*
2081 ospf_recv_packet (int fd
, struct interface
**ifp
, struct stream
*ibuf
)
2086 unsigned int ifindex
= 0;
2088 /* Header and data both require alignment. */
2089 char buff
[CMSG_SPACE(SOPT_SIZE_CMSG_IFINDEX_IPV4())];
2092 memset (&msgh
, 0, sizeof (struct msghdr
));
2093 msgh
.msg_iov
= &iov
;
2094 msgh
.msg_iovlen
= 1;
2095 msgh
.msg_control
= (caddr_t
) buff
;
2096 msgh
.msg_controllen
= sizeof (buff
);
2098 ret
= stream_recvmsg (ibuf
, fd
, &msgh
, 0, OSPF_MAX_PACKET_SIZE
+1);
2101 zlog_warn("stream_recvmsg failed: %s", safe_strerror(errno
));
2104 if ((unsigned int)ret
< sizeof(iph
)) /* ret must be > 0 now */
2106 zlog_warn("ospf_recv_packet: discarding runt packet of length %d "
2107 "(ip header size is %u)",
2108 ret
, (u_int
)sizeof(iph
));
2112 /* Note that there should not be alignment problems with this assignment
2113 because this is at the beginning of the stream data buffer. */
2114 iph
= (struct ip
*) STREAM_DATA(ibuf
);
2115 sockopt_iphdrincl_swab_systoh (iph
);
2117 ip_len
= iph
->ip_len
;
2119 #if !defined(GNU_LINUX) && (OpenBSD < 200311)
2121 * Kernel network code touches incoming IP header parameters,
2122 * before protocol specific processing.
2124 * 1) Convert byteorder to host representation.
2125 * --> ip_len, ip_id, ip_off
2127 * 2) Adjust ip_len to strip IP header size!
2128 * --> If user process receives entire IP packet via RAW
2129 * socket, it must consider adding IP header size to
2130 * the "ip_len" field of "ip" structure.
2132 * For more details, see <netinet/ip_input.c>.
2134 ip_len
= ip_len
+ (iph
->ip_hl
<< 2);
2137 #if defined(__DragonFly__)
2139 * in DragonFly's raw socket, ip_len/ip_off are read
2140 * in network byte order.
2141 * As OpenBSD < 200311 adjust ip_len to strip IP header size!
2143 ip_len
= ntohs(iph
->ip_len
) + (iph
->ip_hl
<< 2);
2146 ifindex
= getsockopt_ifindex (AF_INET
, &msgh
);
2148 *ifp
= if_lookup_by_index (ifindex
);
2152 zlog_warn ("ospf_recv_packet read length mismatch: ip_len is %d, "
2153 "but recvmsg returned %d", ip_len
, ret
);
2160 static struct ospf_interface
*
2161 ospf_associate_packet_vl (struct ospf
*ospf
, struct interface
*ifp
,
2162 struct ip
*iph
, struct ospf_header
*ospfh
)
2164 struct ospf_interface
*rcv_oi
;
2165 struct ospf_vl_data
*vl_data
;
2166 struct ospf_area
*vl_area
;
2167 struct listnode
*node
;
2169 if (IN_MULTICAST (ntohl (iph
->ip_dst
.s_addr
)) ||
2170 !OSPF_IS_AREA_BACKBONE (ospfh
))
2173 /* look for local OSPF interface matching the destination
2174 * to determine Area ID. We presume therefore the destination address
2175 * is unique, or at least (for "unnumbered" links), not used in other
2178 if ((rcv_oi
= ospf_if_lookup_by_local_addr (ospf
, NULL
,
2179 iph
->ip_dst
)) == NULL
)
2182 for (ALL_LIST_ELEMENTS_RO (ospf
->vlinks
, node
, vl_data
))
2184 vl_area
= ospf_area_lookup_by_area_id (ospf
, vl_data
->vl_area_id
);
2188 if (OSPF_AREA_SAME (&vl_area
, &rcv_oi
->area
) &&
2189 IPV4_ADDR_SAME (&vl_data
->vl_peer
, &ospfh
->router_id
))
2191 if (IS_DEBUG_OSPF_EVENT
)
2192 zlog_debug ("associating packet with %s",
2193 IF_NAME (vl_data
->vl_oi
));
2194 if (! CHECK_FLAG (vl_data
->vl_oi
->ifp
->flags
, IFF_UP
))
2196 if (IS_DEBUG_OSPF_EVENT
)
2197 zlog_debug ("This VL is not up yet, sorry");
2201 return vl_data
->vl_oi
;
2205 if (IS_DEBUG_OSPF_EVENT
)
2206 zlog_debug ("couldn't find any VL to associate the packet with");
2212 ospf_check_area_id (struct ospf_interface
*oi
, struct ospf_header
*ospfh
)
2214 /* Check match the Area ID of the receiving interface. */
2215 if (OSPF_AREA_SAME (&oi
->area
, &ospfh
))
2221 /* Unbound socket will accept any Raw IP packets if proto is matched.
2222 To prevent it, compare src IP address and i/f address with masking
2223 i/f network mask. */
2225 ospf_check_network_mask (struct ospf_interface
*oi
, struct in_addr ip_src
)
2227 struct in_addr mask
, me
, him
;
2229 if (oi
->type
== OSPF_IFTYPE_POINTOPOINT
||
2230 oi
->type
== OSPF_IFTYPE_VIRTUALLINK
)
2233 masklen2ip (oi
->address
->prefixlen
, &mask
);
2235 me
.s_addr
= oi
->address
->u
.prefix4
.s_addr
& mask
.s_addr
;
2236 him
.s_addr
= ip_src
.s_addr
& mask
.s_addr
;
2238 if (IPV4_ADDR_SAME (&me
, &him
))
2245 ospf_check_auth (struct ospf_interface
*oi
, struct stream
*ibuf
,
2246 struct ospf_header
*ospfh
)
2249 struct crypt_key
*ck
;
2251 switch (ntohs (ospfh
->auth_type
))
2253 case OSPF_AUTH_NULL
:
2256 case OSPF_AUTH_SIMPLE
:
2257 if (!memcmp (OSPF_IF_PARAM (oi
, auth_simple
), ospfh
->u
.auth_data
, OSPF_AUTH_SIMPLE_SIZE
))
2262 case OSPF_AUTH_CRYPTOGRAPHIC
:
2263 if ((ck
= listgetdata (listtail(OSPF_IF_PARAM (oi
,auth_crypt
)))) == NULL
)
2269 /* This is very basic, the digest processing is elsewhere */
2270 if (ospfh
->u
.crypt
.auth_data_len
== OSPF_AUTH_MD5_SIZE
&&
2271 ospfh
->u
.crypt
.key_id
== ck
->key_id
&&
2272 ntohs (ospfh
->length
) + OSPF_AUTH_SIMPLE_SIZE
<= stream_get_size (ibuf
))
2286 ospf_check_sum (struct ospf_header
*ospfh
)
2291 /* clear auth_data for checksum. */
2292 memset (ospfh
->u
.auth_data
, 0, OSPF_AUTH_SIMPLE_SIZE
);
2294 /* keep checksum and clear. */
2295 sum
= ospfh
->checksum
;
2296 memset (&ospfh
->checksum
, 0, sizeof (u_int16_t
));
2298 /* calculate checksum. */
2299 ret
= in_cksum (ospfh
, ntohs (ospfh
->length
));
2303 zlog_info ("ospf_check_sum(): checksum mismatch, my %X, his %X",
2311 /* OSPF Header verification. */
2313 ospf_verify_header (struct stream
*ibuf
, struct ospf_interface
*oi
,
2314 struct ip
*iph
, struct ospf_header
*ospfh
)
2316 /* check version. */
2317 if (ospfh
->version
!= OSPF_VERSION
)
2319 zlog_warn ("interface %s: ospf_read version number mismatch.",
2324 /* Check Area ID. */
2325 if (!ospf_check_area_id (oi
, ospfh
))
2327 zlog_warn ("interface %s: ospf_read invalid Area ID %s.",
2328 IF_NAME (oi
), inet_ntoa (ospfh
->area_id
));
2332 /* Check network mask, Silently discarded. */
2333 if (! ospf_check_network_mask (oi
, iph
->ip_src
))
2335 zlog_warn ("interface %s: ospf_read network address is not same [%s]",
2336 IF_NAME (oi
), inet_ntoa (iph
->ip_src
));
2340 /* Check authentication. */
2341 if (ospf_auth_type (oi
) != ntohs (ospfh
->auth_type
))
2343 zlog_warn ("interface %s: auth-type mismatch, local %d, rcvd %d",
2344 IF_NAME (oi
), ospf_auth_type (oi
), ntohs (ospfh
->auth_type
));
2348 if (! ospf_check_auth (oi
, ibuf
, ospfh
))
2350 zlog_warn ("interface %s: ospf_read authentication failed.",
2355 /* if check sum is invalid, packet is discarded. */
2356 if (ntohs (ospfh
->auth_type
) != OSPF_AUTH_CRYPTOGRAPHIC
)
2358 if (! ospf_check_sum (ospfh
))
2360 zlog_warn ("interface %s: ospf_read packet checksum error %s",
2361 IF_NAME (oi
), inet_ntoa (ospfh
->router_id
));
2367 if (ospfh
->checksum
!= 0)
2369 if (ospf_check_md5_digest (oi
, ibuf
, ntohs (ospfh
->length
)) == 0)
2371 zlog_warn ("interface %s: ospf_read md5 authentication failed.",
2380 /* Starting point of packet process function. */
2382 ospf_read (struct thread
*thread
)
2385 struct stream
*ibuf
;
2387 struct ospf_interface
*oi
;
2389 struct ospf_header
*ospfh
;
2391 struct interface
*ifp
;
2393 /* first of all get interface pointer. */
2394 ospf
= THREAD_ARG (thread
);
2396 /* prepare for next packet. */
2397 ospf
->t_read
= thread_add_read (master
, ospf_read
, ospf
, ospf
->fd
);
2399 /* read OSPF packet. */
2400 stream_reset(ospf
->ibuf
);
2401 if (!(ibuf
= ospf_recv_packet (ospf
->fd
, &ifp
, ospf
->ibuf
)))
2404 /* Note that there should not be alignment problems with this assignment
2405 because this is at the beginning of the stream data buffer. */
2406 iph
= (struct ip
*) STREAM_DATA (ibuf
);
2407 /* Note that sockopt_iphdrincl_swab_systoh was called in ospf_recv_packet. */
2410 /* Handle cases where the platform does not support retrieving the ifindex,
2411 and also platforms (such as Solaris 8) that claim to support ifindex
2412 retrieval but do not. */
2413 ifp
= if_lookup_address (iph
->ip_src
);
2418 /* IP Header dump. */
2419 if (IS_DEBUG_OSPF_PACKET(0, RECV
))
2420 ospf_ip_header_dump (iph
);
2422 /* Self-originated packet should be discarded silently. */
2423 if (ospf_if_lookup_by_local_addr (ospf
, NULL
, iph
->ip_src
))
2425 if (IS_DEBUG_OSPF_PACKET (0, RECV
))
2427 zlog_debug ("ospf_read[%s]: Dropping self-originated packet",
2428 inet_ntoa (iph
->ip_src
));
2433 /* Adjust size to message length. */
2434 stream_forward_getp (ibuf
, iph
->ip_hl
* 4);
2436 /* Get ospf packet header. */
2437 ospfh
= (struct ospf_header
*) STREAM_PNT (ibuf
);
2439 /* associate packet with ospf interface */
2440 oi
= ospf_if_lookup_recv_if (ospf
, iph
->ip_src
, ifp
);
2442 /* If incoming interface is passive one, ignore it. */
2443 if (oi
&& OSPF_IF_PASSIVE_STATUS (oi
) == OSPF_IF_PASSIVE
)
2445 char buf
[3][INET_ADDRSTRLEN
];
2447 if (IS_DEBUG_OSPF_EVENT
)
2448 zlog_debug ("ignoring packet from router %s sent to %s, "
2449 "received on a passive interface, %s",
2450 inet_ntop(AF_INET
, &ospfh
->router_id
, buf
[0], sizeof(buf
[0])),
2451 inet_ntop(AF_INET
, &iph
->ip_dst
, buf
[1], sizeof(buf
[1])),
2452 inet_ntop(AF_INET
, &oi
->address
->u
.prefix4
,
2453 buf
[2], sizeof(buf
[2])));
2455 if (iph
->ip_dst
.s_addr
== htonl(OSPF_ALLSPFROUTERS
))
2457 /* Try to fix multicast membership.
2458 * Some OS:es may have problems in this area,
2459 * make sure it is removed.
2461 OI_MEMBER_JOINED(oi
, MEMBER_ALLROUTERS
);
2462 ospf_if_set_multicast(oi
);
2468 /* if no local ospf_interface,
2469 * or header area is backbone but ospf_interface is not
2470 * check for VLINK interface
2472 if ( (oi
== NULL
) ||
2473 (OSPF_IS_AREA_ID_BACKBONE(ospfh
->area_id
)
2474 && !OSPF_IS_AREA_ID_BACKBONE(oi
->area
->area_id
))
2477 if ((oi
= ospf_associate_packet_vl (ospf
, ifp
, iph
, ospfh
)) == NULL
)
2479 if (IS_DEBUG_OSPF_EVENT
)
2480 zlog_debug ("Packet from [%s] received on link %s"
2481 " but no ospf_interface",
2482 inet_ntoa (iph
->ip_src
), ifp
->name
);
2487 /* else it must be a local ospf interface, check it was received on
2490 else if (oi
->ifp
!= ifp
)
2492 if (IS_DEBUG_OSPF_EVENT
)
2493 zlog_warn ("Packet from [%s] received on wrong link %s",
2494 inet_ntoa (iph
->ip_src
), ifp
->name
);
2497 else if (oi
->state
== ISM_Down
)
2499 char buf
[2][INET_ADDRSTRLEN
];
2500 zlog_warn ("Ignoring packet from %s to %s received on interface that is "
2501 "down [%s]; interface flags are %s",
2502 inet_ntop(AF_INET
, &iph
->ip_src
, buf
[0], sizeof(buf
[0])),
2503 inet_ntop(AF_INET
, &iph
->ip_dst
, buf
[1], sizeof(buf
[1])),
2504 ifp
->name
, if_flag_dump(ifp
->flags
));
2505 /* Fix multicast memberships? */
2506 if (iph
->ip_dst
.s_addr
== htonl(OSPF_ALLSPFROUTERS
))
2507 OI_MEMBER_JOINED(oi
, MEMBER_ALLROUTERS
);
2508 else if (iph
->ip_dst
.s_addr
== htonl(OSPF_ALLDROUTERS
))
2509 OI_MEMBER_JOINED(oi
, MEMBER_DROUTERS
);
2510 if (oi
->multicast_memberships
)
2511 ospf_if_set_multicast(oi
);
2516 * If the received packet is destined for AllDRouters, the packet
2517 * should be accepted only if the received ospf interface state is
2518 * either DR or Backup -- endo.
2520 if (iph
->ip_dst
.s_addr
== htonl (OSPF_ALLDROUTERS
)
2521 && (oi
->state
!= ISM_DR
&& oi
->state
!= ISM_Backup
))
2523 zlog_warn ("Dropping packet for AllDRouters from [%s] via [%s] (ISM: %s)",
2524 inet_ntoa (iph
->ip_src
), IF_NAME (oi
),
2525 LOOKUP (ospf_ism_state_msg
, oi
->state
));
2526 /* Try to fix multicast membership. */
2527 SET_FLAG(oi
->multicast_memberships
, MEMBER_DROUTERS
);
2528 ospf_if_set_multicast(oi
);
2532 /* Show debug receiving packet. */
2533 if (IS_DEBUG_OSPF_PACKET (ospfh
->type
- 1, RECV
))
2535 if (IS_DEBUG_OSPF_PACKET (ospfh
->type
- 1, DETAIL
))
2537 zlog_debug ("-----------------------------------------------------");
2538 ospf_packet_dump (ibuf
);
2541 zlog_debug ("%s received from [%s] via [%s]",
2542 ospf_packet_type_str
[ospfh
->type
],
2543 inet_ntoa (ospfh
->router_id
), IF_NAME (oi
));
2544 zlog_debug (" src [%s],", inet_ntoa (iph
->ip_src
));
2545 zlog_debug (" dst [%s]", inet_ntoa (iph
->ip_dst
));
2547 if (IS_DEBUG_OSPF_PACKET (ospfh
->type
- 1, DETAIL
))
2548 zlog_debug ("-----------------------------------------------------");
2551 /* Some header verification. */
2552 ret
= ospf_verify_header (ibuf
, oi
, iph
, ospfh
);
2555 if (IS_DEBUG_OSPF_PACKET (ospfh
->type
- 1, RECV
))
2557 zlog_debug ("ospf_read[%s/%s]: Header check failed, "
2559 ospf_packet_type_str
[ospfh
->type
],
2560 inet_ntoa (iph
->ip_src
));
2565 stream_forward_getp (ibuf
, OSPF_HEADER_SIZE
);
2567 /* Adjust size to message length. */
2568 length
= ntohs (ospfh
->length
) - OSPF_HEADER_SIZE
;
2570 /* Read rest of the packet and call each sort of packet routine. */
2571 switch (ospfh
->type
)
2573 case OSPF_MSG_HELLO
:
2574 ospf_hello (iph
, ospfh
, ibuf
, oi
, length
);
2576 case OSPF_MSG_DB_DESC
:
2577 ospf_db_desc (iph
, ospfh
, ibuf
, oi
, length
);
2579 case OSPF_MSG_LS_REQ
:
2580 ospf_ls_req (iph
, ospfh
, ibuf
, oi
, length
);
2582 case OSPF_MSG_LS_UPD
:
2583 ospf_ls_upd (iph
, ospfh
, ibuf
, oi
, length
);
2585 case OSPF_MSG_LS_ACK
:
2586 ospf_ls_ack (iph
, ospfh
, ibuf
, oi
, length
);
2589 zlog (NULL
, LOG_WARNING
,
2590 "interface %s: OSPF packet header type %d is illegal",
2591 IF_NAME (oi
), ospfh
->type
);
2598 /* Make OSPF header. */
2600 ospf_make_header (int type
, struct ospf_interface
*oi
, struct stream
*s
)
2602 struct ospf_header
*ospfh
;
2604 ospfh
= (struct ospf_header
*) STREAM_DATA (s
);
2606 ospfh
->version
= (u_char
) OSPF_VERSION
;
2607 ospfh
->type
= (u_char
) type
;
2609 ospfh
->router_id
= oi
->ospf
->router_id
;
2611 ospfh
->checksum
= 0;
2612 ospfh
->area_id
= oi
->area
->area_id
;
2613 ospfh
->auth_type
= htons (ospf_auth_type (oi
));
2615 memset (ospfh
->u
.auth_data
, 0, OSPF_AUTH_SIMPLE_SIZE
);
2617 stream_forward_endp (s
, OSPF_HEADER_SIZE
);
2620 /* Make Authentication Data. */
2622 ospf_make_auth (struct ospf_interface
*oi
, struct ospf_header
*ospfh
)
2624 struct crypt_key
*ck
;
2626 switch (ospf_auth_type (oi
))
2628 case OSPF_AUTH_NULL
:
2629 /* memset (ospfh->u.auth_data, 0, sizeof (ospfh->u.auth_data)); */
2631 case OSPF_AUTH_SIMPLE
:
2632 memcpy (ospfh
->u
.auth_data
, OSPF_IF_PARAM (oi
, auth_simple
),
2633 OSPF_AUTH_SIMPLE_SIZE
);
2635 case OSPF_AUTH_CRYPTOGRAPHIC
:
2636 /* If key is not set, then set 0. */
2637 if (list_isempty (OSPF_IF_PARAM (oi
, auth_crypt
)))
2639 ospfh
->u
.crypt
.zero
= 0;
2640 ospfh
->u
.crypt
.key_id
= 0;
2641 ospfh
->u
.crypt
.auth_data_len
= OSPF_AUTH_MD5_SIZE
;
2645 ck
= listgetdata (listtail(OSPF_IF_PARAM (oi
, auth_crypt
)));
2646 ospfh
->u
.crypt
.zero
= 0;
2647 ospfh
->u
.crypt
.key_id
= ck
->key_id
;
2648 ospfh
->u
.crypt
.auth_data_len
= OSPF_AUTH_MD5_SIZE
;
2650 /* note: the seq is done in ospf_make_md5_digest() */
2653 /* memset (ospfh->u.auth_data, 0, sizeof (ospfh->u.auth_data)); */
2660 /* Fill rest of OSPF header. */
2662 ospf_fill_header (struct ospf_interface
*oi
,
2663 struct stream
*s
, u_int16_t length
)
2665 struct ospf_header
*ospfh
;
2667 ospfh
= (struct ospf_header
*) STREAM_DATA (s
);
2670 ospfh
->length
= htons (length
);
2672 /* Calculate checksum. */
2673 if (ntohs (ospfh
->auth_type
) != OSPF_AUTH_CRYPTOGRAPHIC
)
2674 ospfh
->checksum
= in_cksum (ospfh
, length
);
2676 ospfh
->checksum
= 0;
2678 /* Add Authentication Data. */
2679 ospf_make_auth (oi
, ospfh
);
2683 ospf_make_hello (struct ospf_interface
*oi
, struct stream
*s
)
2685 struct ospf_neighbor
*nbr
;
2686 struct route_node
*rn
;
2687 u_int16_t length
= OSPF_HELLO_MIN_SIZE
;
2688 struct in_addr mask
;
2692 /* Set netmask of interface. */
2693 if (oi
->type
!= OSPF_IFTYPE_POINTOPOINT
&&
2694 oi
->type
!= OSPF_IFTYPE_VIRTUALLINK
)
2695 masklen2ip (oi
->address
->prefixlen
, &mask
);
2697 memset ((char *) &mask
, 0, sizeof (struct in_addr
));
2698 stream_put_ipv4 (s
, mask
.s_addr
);
2700 /* Set Hello Interval. */
2701 if (OSPF_IF_PARAM (oi
, fast_hello
) == 0)
2702 stream_putw (s
, OSPF_IF_PARAM (oi
, v_hello
));
2704 stream_putw (s
, 0); /* hello-interval of 0 for fast-hellos */
2706 if (IS_DEBUG_OSPF_EVENT
)
2707 zlog_debug ("make_hello: options: %x, int: %s",
2708 OPTIONS(oi
), IF_NAME (oi
));
2711 stream_putc (s
, OPTIONS (oi
));
2713 /* Set Router Priority. */
2714 stream_putc (s
, PRIORITY (oi
));
2716 /* Set Router Dead Interval. */
2717 stream_putl (s
, OSPF_IF_PARAM (oi
, v_wait
));
2719 /* Set Designated Router. */
2720 stream_put_ipv4 (s
, DR (oi
).s_addr
);
2722 p
= stream_get_endp (s
);
2724 /* Set Backup Designated Router. */
2725 stream_put_ipv4 (s
, BDR (oi
).s_addr
);
2727 /* Add neighbor seen. */
2728 for (rn
= route_top (oi
->nbrs
); rn
; rn
= route_next (rn
))
2729 if ((nbr
= rn
->info
))
2730 if (nbr
->router_id
.s_addr
!= 0) /* Ignore 0.0.0.0 node. */
2731 if (nbr
->state
!= NSM_Attempt
) /* Ignore Down neighbor. */
2732 if (nbr
->state
!= NSM_Down
) /* This is myself for DR election. */
2733 if (!IPV4_ADDR_SAME (&nbr
->router_id
, &oi
->ospf
->router_id
))
2735 /* Check neighbor is sane? */
2736 if (nbr
->d_router
.s_addr
!= 0
2737 && IPV4_ADDR_SAME (&nbr
->d_router
, &oi
->address
->u
.prefix4
)
2738 && IPV4_ADDR_SAME (&nbr
->bd_router
, &oi
->address
->u
.prefix4
))
2741 stream_put_ipv4 (s
, nbr
->router_id
.s_addr
);
2745 /* Let neighbor generate BackupSeen. */
2747 stream_putl_at (s
, p
, 0); /* ipv4 address, normally */
2753 ospf_make_db_desc (struct ospf_interface
*oi
, struct ospf_neighbor
*nbr
,
2756 struct ospf_lsa
*lsa
;
2757 u_int16_t length
= OSPF_DB_DESC_MIN_SIZE
;
2761 struct ospf_lsdb
*lsdb
;
2763 /* Set Interface MTU. */
2764 if (oi
->type
== OSPF_IFTYPE_VIRTUALLINK
)
2767 stream_putw (s
, oi
->ifp
->mtu
);
2770 options
= OPTIONS (oi
);
2771 #ifdef HAVE_OPAQUE_LSA
2772 if (CHECK_FLAG (oi
->ospf
->config
, OSPF_OPAQUE_CAPABLE
))
2774 if (IS_SET_DD_I (nbr
->dd_flags
)
2775 || CHECK_FLAG (nbr
->options
, OSPF_OPTION_O
))
2777 * Set O-bit in the outgoing DD packet for capablity negotiation,
2778 * if one of following case is applicable.
2780 * 1) WaitTimer expiration event triggered the neighbor state to
2781 * change to Exstart, but no (valid) DD packet has received
2782 * from the neighbor yet.
2784 * 2) At least one DD packet with O-bit on has received from the
2787 SET_FLAG (options
, OSPF_OPTION_O
);
2789 #endif /* HAVE_OPAQUE_LSA */
2790 stream_putc (s
, options
);
2793 pp
= stream_get_endp (s
);
2794 stream_putc (s
, nbr
->dd_flags
);
2796 /* Set DD Sequence Number. */
2797 stream_putl (s
, nbr
->dd_seqnum
);
2799 /* shortcut unneeded walk of (empty) summary LSDBs */
2800 if (ospf_db_summary_isempty (nbr
))
2803 /* Describe LSA Header from Database Summary List. */
2804 lsdb
= &nbr
->db_sum
;
2806 for (i
= OSPF_MIN_LSA
; i
< OSPF_MAX_LSA
; i
++)
2808 struct route_table
*table
= lsdb
->type
[i
].db
;
2809 struct route_node
*rn
;
2811 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
2812 if ((lsa
= rn
->info
) != NULL
)
2814 #ifdef HAVE_OPAQUE_LSA
2815 if (IS_OPAQUE_LSA (lsa
->data
->type
)
2816 && (! CHECK_FLAG (options
, OSPF_OPTION_O
)))
2818 /* Suppress advertising opaque-informations. */
2819 /* Remove LSA from DB summary list. */
2820 ospf_lsdb_delete (lsdb
, lsa
);
2823 #endif /* HAVE_OPAQUE_LSA */
2825 if (!CHECK_FLAG (lsa
->flags
, OSPF_LSA_DISCARD
))
2827 struct lsa_header
*lsah
;
2830 /* DD packet overflows interface MTU. */
2831 if (length
+ OSPF_LSA_HEADER_SIZE
> ospf_packet_max (oi
))
2834 /* Keep pointer to LS age. */
2835 lsah
= (struct lsa_header
*) (STREAM_DATA (s
) +
2836 stream_get_endp (s
));
2838 /* Proceed stream pointer. */
2839 stream_put (s
, lsa
->data
, OSPF_LSA_HEADER_SIZE
);
2840 length
+= OSPF_LSA_HEADER_SIZE
;
2843 ls_age
= LS_AGE (lsa
);
2844 lsah
->ls_age
= htons (ls_age
);
2848 /* Remove LSA from DB summary list. */
2849 ospf_lsdb_delete (lsdb
, lsa
);
2853 /* Update 'More' bit */
2854 if (ospf_db_summary_isempty (nbr
))
2857 if (nbr
->state
>= NSM_Exchange
)
2859 UNSET_FLAG (nbr
->dd_flags
, OSPF_DD_FLAG_M
);
2860 /* Rewrite DD flags */
2861 stream_putc_at (s
, pp
, nbr
->dd_flags
);
2865 assert (IS_SET_DD_M(nbr
->dd_flags
));
2872 ospf_make_ls_req_func (struct stream
*s
, u_int16_t
*length
,
2873 unsigned long delta
, struct ospf_neighbor
*nbr
,
2874 struct ospf_lsa
*lsa
)
2876 struct ospf_interface
*oi
;
2880 /* LS Request packet overflows interface MTU. */
2881 if (*length
+ delta
> ospf_packet_max(oi
))
2884 stream_putl (s
, lsa
->data
->type
);
2885 stream_put_ipv4 (s
, lsa
->data
->id
.s_addr
);
2886 stream_put_ipv4 (s
, lsa
->data
->adv_router
.s_addr
);
2888 ospf_lsa_unlock (&nbr
->ls_req_last
);
2889 nbr
->ls_req_last
= ospf_lsa_lock (lsa
);
2896 ospf_make_ls_req (struct ospf_neighbor
*nbr
, struct stream
*s
)
2898 struct ospf_lsa
*lsa
;
2899 u_int16_t length
= OSPF_LS_REQ_MIN_SIZE
;
2900 unsigned long delta
= stream_get_endp(s
)+12;
2901 struct route_table
*table
;
2902 struct route_node
*rn
;
2904 struct ospf_lsdb
*lsdb
;
2906 lsdb
= &nbr
->ls_req
;
2908 for (i
= OSPF_MIN_LSA
; i
< OSPF_MAX_LSA
; i
++)
2910 table
= lsdb
->type
[i
].db
;
2911 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
2912 if ((lsa
= (rn
->info
)) != NULL
)
2913 if (ospf_make_ls_req_func (s
, &length
, delta
, nbr
, lsa
) == 0)
2915 route_unlock_node (rn
);
2923 ls_age_increment (struct ospf_lsa
*lsa
, int delay
)
2927 age
= IS_LSA_MAXAGE (lsa
) ? OSPF_LSA_MAXAGE
: LS_AGE (lsa
) + delay
;
2929 return (age
> OSPF_LSA_MAXAGE
? OSPF_LSA_MAXAGE
: age
);
2933 ospf_make_ls_upd (struct ospf_interface
*oi
, struct list
*update
, struct stream
*s
)
2935 struct ospf_lsa
*lsa
;
2936 struct listnode
*node
;
2937 u_int16_t length
= 0;
2938 unsigned int size_noauth
;
2939 unsigned long delta
= stream_get_endp (s
);
2943 if (IS_DEBUG_OSPF_EVENT
)
2944 zlog_debug ("ospf_make_ls_upd: Start");
2946 pp
= stream_get_endp (s
);
2947 stream_forward_endp (s
, OSPF_LS_UPD_MIN_SIZE
);
2948 length
+= OSPF_LS_UPD_MIN_SIZE
;
2950 /* Calculate amount of packet usable for data. */
2951 size_noauth
= stream_get_size(s
) - ospf_packet_authspace(oi
);
2953 while ((node
= listhead (update
)) != NULL
)
2955 struct lsa_header
*lsah
;
2958 if (IS_DEBUG_OSPF_EVENT
)
2959 zlog_debug ("ospf_make_ls_upd: List Iteration");
2961 lsa
= listgetdata (node
);
2966 if (length
+ delta
+ ntohs (lsa
->data
->length
) > size_noauth
)
2969 /* Keep pointer to LS age. */
2970 lsah
= (struct lsa_header
*) (STREAM_DATA (s
) + stream_get_endp (s
));
2972 /* Put LSA to Link State Request. */
2973 stream_put (s
, lsa
->data
, ntohs (lsa
->data
->length
));
2976 /* each hop must increment an lsa_age by transmit_delay
2977 of OSPF interface */
2978 ls_age
= ls_age_increment (lsa
, OSPF_IF_PARAM (oi
, transmit_delay
));
2979 lsah
->ls_age
= htons (ls_age
);
2981 length
+= ntohs (lsa
->data
->length
);
2984 list_delete_node (update
, node
);
2985 ospf_lsa_unlock (&lsa
); /* oi->ls_upd_queue */
2988 /* Now set #LSAs. */
2989 stream_putl_at (s
, pp
, count
);
2991 if (IS_DEBUG_OSPF_EVENT
)
2992 zlog_debug ("ospf_make_ls_upd: Stop");
2997 ospf_make_ls_ack (struct ospf_interface
*oi
, struct list
*ack
, struct stream
*s
)
2999 struct listnode
*node
, *nnode
;
3000 u_int16_t length
= OSPF_LS_ACK_MIN_SIZE
;
3001 unsigned long delta
= stream_get_endp(s
) + 24;
3002 struct ospf_lsa
*lsa
;
3004 for (ALL_LIST_ELEMENTS (ack
, node
, nnode
, lsa
))
3008 if (length
+ delta
> ospf_packet_max (oi
))
3011 stream_put (s
, lsa
->data
, OSPF_LSA_HEADER_SIZE
);
3012 length
+= OSPF_LSA_HEADER_SIZE
;
3014 listnode_delete (ack
, lsa
);
3015 ospf_lsa_unlock (&lsa
); /* oi->ls_ack_direct.ls_ack */
3022 ospf_hello_send_sub (struct ospf_interface
*oi
, in_addr_t addr
)
3024 struct ospf_packet
*op
;
3025 u_int16_t length
= OSPF_HEADER_SIZE
;
3027 op
= ospf_packet_new (oi
->ifp
->mtu
);
3029 /* Prepare OSPF common header. */
3030 ospf_make_header (OSPF_MSG_HELLO
, oi
, op
->s
);
3032 /* Prepare OSPF Hello body. */
3033 length
+= ospf_make_hello (oi
, op
->s
);
3035 /* Fill OSPF header. */
3036 ospf_fill_header (oi
, op
->s
, length
);
3038 /* Set packet length. */
3039 op
->length
= length
;
3041 op
->dst
.s_addr
= addr
;
3043 /* Add packet to the top of the interface output queue, so that they
3044 * can't get delayed by things like long queues of LS Update packets
3046 ospf_packet_add_top (oi
, op
);
3048 /* Hook thread to write packet. */
3049 OSPF_ISM_WRITE_ON (oi
->ospf
);
3053 ospf_poll_send (struct ospf_nbr_nbma
*nbr_nbma
)
3055 struct ospf_interface
*oi
;
3060 /* If this is passive interface, do not send OSPF Hello. */
3061 if (OSPF_IF_PASSIVE_STATUS (oi
) == OSPF_IF_PASSIVE
)
3064 if (oi
->type
!= OSPF_IFTYPE_NBMA
)
3067 if (nbr_nbma
->nbr
!= NULL
&& nbr_nbma
->nbr
->state
!= NSM_Down
)
3070 if (PRIORITY(oi
) == 0)
3073 if (nbr_nbma
->priority
== 0
3074 && oi
->state
!= ISM_DR
&& oi
->state
!= ISM_Backup
)
3077 ospf_hello_send_sub (oi
, nbr_nbma
->addr
.s_addr
);
3081 ospf_poll_timer (struct thread
*thread
)
3083 struct ospf_nbr_nbma
*nbr_nbma
;
3085 nbr_nbma
= THREAD_ARG (thread
);
3086 nbr_nbma
->t_poll
= NULL
;
3088 if (IS_DEBUG_OSPF (nsm
, NSM_TIMERS
))
3089 zlog (NULL
, LOG_DEBUG
, "NSM[%s:%s]: Timer (Poll timer expire)",
3090 IF_NAME (nbr_nbma
->oi
), inet_ntoa (nbr_nbma
->addr
));
3092 ospf_poll_send (nbr_nbma
);
3094 if (nbr_nbma
->v_poll
> 0)
3095 OSPF_POLL_TIMER_ON (nbr_nbma
->t_poll
, ospf_poll_timer
,
3103 ospf_hello_reply_timer (struct thread
*thread
)
3105 struct ospf_neighbor
*nbr
;
3107 nbr
= THREAD_ARG (thread
);
3108 nbr
->t_hello_reply
= NULL
;
3112 if (IS_DEBUG_OSPF (nsm
, NSM_TIMERS
))
3113 zlog (NULL
, LOG_DEBUG
, "NSM[%s:%s]: Timer (hello-reply timer expire)",
3114 IF_NAME (nbr
->oi
), inet_ntoa (nbr
->router_id
));
3116 ospf_hello_send_sub (nbr
->oi
, nbr
->address
.u
.prefix4
.s_addr
);
3121 /* Send OSPF Hello. */
3123 ospf_hello_send (struct ospf_interface
*oi
)
3125 /* If this is passive interface, do not send OSPF Hello. */
3126 if (OSPF_IF_PASSIVE_STATUS (oi
) == OSPF_IF_PASSIVE
)
3129 if (oi
->type
== OSPF_IFTYPE_NBMA
)
3131 struct ospf_neighbor
*nbr
;
3132 struct route_node
*rn
;
3134 for (rn
= route_top (oi
->nbrs
); rn
; rn
= route_next (rn
))
3135 if ((nbr
= rn
->info
))
3136 if (nbr
!= oi
->nbr_self
)
3137 if (nbr
->state
!= NSM_Down
)
3139 /* RFC 2328 Section 9.5.1
3140 If the router is not eligible to become Designated Router,
3141 it must periodically send Hello Packets to both the
3142 Designated Router and the Backup Designated Router (if they
3144 if (PRIORITY(oi
) == 0 &&
3145 IPV4_ADDR_CMP(&DR(oi
), &nbr
->address
.u
.prefix4
) &&
3146 IPV4_ADDR_CMP(&BDR(oi
), &nbr
->address
.u
.prefix4
))
3149 /* If the router is eligible to become Designated Router, it
3150 must periodically send Hello Packets to all neighbors that
3151 are also eligible. In addition, if the router is itself the
3152 Designated Router or Backup Designated Router, it must also
3153 send periodic Hello Packets to all other neighbors. */
3155 if (nbr
->priority
== 0 && oi
->state
== ISM_DROther
)
3157 /* if oi->state == Waiting, send hello to all neighbors */
3158 ospf_hello_send_sub (oi
, nbr
->address
.u
.prefix4
.s_addr
);
3163 /* Decide destination address. */
3164 if (oi
->type
== OSPF_IFTYPE_VIRTUALLINK
)
3165 ospf_hello_send_sub (oi
, oi
->vl_data
->peer_addr
.s_addr
);
3167 ospf_hello_send_sub (oi
, htonl (OSPF_ALLSPFROUTERS
));
3171 /* Send OSPF Database Description. */
3173 ospf_db_desc_send (struct ospf_neighbor
*nbr
)
3175 struct ospf_interface
*oi
;
3176 struct ospf_packet
*op
;
3177 u_int16_t length
= OSPF_HEADER_SIZE
;
3180 op
= ospf_packet_new (oi
->ifp
->mtu
);
3182 /* Prepare OSPF common header. */
3183 ospf_make_header (OSPF_MSG_DB_DESC
, oi
, op
->s
);
3185 /* Prepare OSPF Database Description body. */
3186 length
+= ospf_make_db_desc (oi
, nbr
, op
->s
);
3188 /* Fill OSPF header. */
3189 ospf_fill_header (oi
, op
->s
, length
);
3191 /* Set packet length. */
3192 op
->length
= length
;
3194 /* Decide destination address. */
3195 if (oi
->type
== OSPF_IFTYPE_POINTOPOINT
)
3196 op
->dst
.s_addr
= htonl (OSPF_ALLSPFROUTERS
);
3198 op
->dst
= nbr
->address
.u
.prefix4
;
3200 /* Add packet to the interface output queue. */
3201 ospf_packet_add (oi
, op
);
3203 /* Hook thread to write packet. */
3204 OSPF_ISM_WRITE_ON (oi
->ospf
);
3206 /* Remove old DD packet, then copy new one and keep in neighbor structure. */
3208 ospf_packet_free (nbr
->last_send
);
3209 nbr
->last_send
= ospf_packet_dup (op
);
3210 quagga_gettime (QUAGGA_CLK_MONOTONIC
, &nbr
->last_send_ts
);
3213 /* Re-send Database Description. */
3215 ospf_db_desc_resend (struct ospf_neighbor
*nbr
)
3217 struct ospf_interface
*oi
;
3221 /* Add packet to the interface output queue. */
3222 ospf_packet_add (oi
, ospf_packet_dup (nbr
->last_send
));
3224 /* Hook thread to write packet. */
3225 OSPF_ISM_WRITE_ON (oi
->ospf
);
3228 /* Send Link State Request. */
3230 ospf_ls_req_send (struct ospf_neighbor
*nbr
)
3232 struct ospf_interface
*oi
;
3233 struct ospf_packet
*op
;
3234 u_int16_t length
= OSPF_HEADER_SIZE
;
3237 op
= ospf_packet_new (oi
->ifp
->mtu
);
3239 /* Prepare OSPF common header. */
3240 ospf_make_header (OSPF_MSG_LS_REQ
, oi
, op
->s
);
3242 /* Prepare OSPF Link State Request body. */
3243 length
+= ospf_make_ls_req (nbr
, op
->s
);
3244 if (length
== OSPF_HEADER_SIZE
)
3246 ospf_packet_free (op
);
3250 /* Fill OSPF header. */
3251 ospf_fill_header (oi
, op
->s
, length
);
3253 /* Set packet length. */
3254 op
->length
= length
;
3256 /* Decide destination address. */
3257 if (oi
->type
== OSPF_IFTYPE_POINTOPOINT
)
3258 op
->dst
.s_addr
= htonl (OSPF_ALLSPFROUTERS
);
3260 op
->dst
= nbr
->address
.u
.prefix4
;
3262 /* Add packet to the interface output queue. */
3263 ospf_packet_add (oi
, op
);
3265 /* Hook thread to write packet. */
3266 OSPF_ISM_WRITE_ON (oi
->ospf
);
3268 /* Add Link State Request Retransmission Timer. */
3269 OSPF_NSM_TIMER_ON (nbr
->t_ls_req
, ospf_ls_req_timer
, nbr
->v_ls_req
);
3272 /* Send Link State Update with an LSA. */
3274 ospf_ls_upd_send_lsa (struct ospf_neighbor
*nbr
, struct ospf_lsa
*lsa
,
3277 struct list
*update
;
3279 update
= list_new ();
3281 listnode_add (update
, lsa
);
3282 ospf_ls_upd_send (nbr
, update
, flag
);
3284 list_delete (update
);
3287 /* Determine size for packet. Must be at least big enough to accomodate next
3288 * LSA on list, which may be bigger than MTU size.
3290 * Return pointer to new ospf_packet
3291 * NULL if we can not allocate, eg because LSA is bigger than imposed limit
3292 * on packet sizes (in which case offending LSA is deleted from update list)
3294 static struct ospf_packet
*
3295 ospf_ls_upd_packet_new (struct list
*update
, struct ospf_interface
*oi
)
3297 struct ospf_lsa
*lsa
;
3298 struct listnode
*ln
;
3300 static char warned
= 0;
3302 lsa
= listgetdata((ln
= listhead (update
)));
3305 if ((OSPF_LS_UPD_MIN_SIZE
+ ntohs (lsa
->data
->length
))
3306 > ospf_packet_max (oi
))
3310 zlog_warn ("ospf_ls_upd_packet_new: oversized LSA encountered!"
3311 "will need to fragment. Not optimal. Try divide up"
3312 " your network with areas. Use 'debug ospf packet send'"
3313 " to see details, or look at 'show ip ospf database ..'");
3317 if (IS_DEBUG_OSPF_PACKET (0, SEND
))
3318 zlog_debug ("ospf_ls_upd_packet_new: oversized LSA id:%s,"
3319 " %d bytes originated by %s, will be fragmented!",
3320 inet_ntoa (lsa
->data
->id
),
3321 ntohs (lsa
->data
->length
),
3322 inet_ntoa (lsa
->data
->adv_router
));
3325 * Allocate just enough to fit this LSA only, to avoid including other
3326 * LSAs in fragmented LSA Updates.
3328 size
= ntohs (lsa
->data
->length
) + (oi
->ifp
->mtu
- ospf_packet_max (oi
))
3329 + OSPF_LS_UPD_MIN_SIZE
;
3332 size
= oi
->ifp
->mtu
;
3334 if (size
> OSPF_MAX_PACKET_SIZE
)
3336 zlog_warn ("ospf_ls_upd_packet_new: oversized LSA id:%s too big,"
3337 " %d bytes, packet size %ld, dropping it completely."
3338 " OSPF routing is broken!",
3339 inet_ntoa (lsa
->data
->id
), ntohs (lsa
->data
->length
),
3341 list_delete_node (update
, ln
);
3345 /* IP header is built up separately by ospf_write(). This means, that we must
3346 * reduce the "affordable" size just calculated by length of an IP header.
3347 * This makes sure, that even if we manage to fill the payload with LSA data
3348 * completely, the final packet (our data plus IP header) still fits into
3349 * outgoing interface MTU. This correction isn't really meaningful for an
3350 * oversized LSA, but for consistency the correction is done for both cases.
3352 * P.S. OSPF_MAX_PACKET_SIZE above already includes IP header size
3354 return ospf_packet_new (size
- sizeof (struct ip
));
3358 ospf_ls_upd_queue_send (struct ospf_interface
*oi
, struct list
*update
,
3359 struct in_addr addr
)
3361 struct ospf_packet
*op
;
3362 u_int16_t length
= OSPF_HEADER_SIZE
;
3364 if (IS_DEBUG_OSPF_EVENT
)
3365 zlog_debug ("listcount = %d, dst %s", listcount (update
), inet_ntoa(addr
));
3367 op
= ospf_ls_upd_packet_new (update
, oi
);
3369 /* Prepare OSPF common header. */
3370 ospf_make_header (OSPF_MSG_LS_UPD
, oi
, op
->s
);
3372 /* Prepare OSPF Link State Update body.
3373 * Includes Type-7 translation.
3375 length
+= ospf_make_ls_upd (oi
, update
, op
->s
);
3377 /* Fill OSPF header. */
3378 ospf_fill_header (oi
, op
->s
, length
);
3380 /* Set packet length. */
3381 op
->length
= length
;
3383 /* Decide destination address. */
3384 if (oi
->type
== OSPF_IFTYPE_POINTOPOINT
)
3385 op
->dst
.s_addr
= htonl (OSPF_ALLSPFROUTERS
);
3387 op
->dst
.s_addr
= addr
.s_addr
;
3389 /* Add packet to the interface output queue. */
3390 ospf_packet_add (oi
, op
);
3392 /* Hook thread to write packet. */
3393 OSPF_ISM_WRITE_ON (oi
->ospf
);
3397 ospf_ls_upd_send_queue_event (struct thread
*thread
)
3399 struct ospf_interface
*oi
= THREAD_ARG(thread
);
3400 struct route_node
*rn
;
3401 struct route_node
*rnext
;
3402 struct list
*update
;
3405 oi
->t_ls_upd_event
= NULL
;
3407 if (IS_DEBUG_OSPF_EVENT
)
3408 zlog_debug ("ospf_ls_upd_send_queue start");
3410 for (rn
= route_top (oi
->ls_upd_queue
); rn
; rn
= rnext
)
3412 rnext
= route_next (rn
);
3414 if (rn
->info
== NULL
)
3417 update
= (struct list
*)rn
->info
;
3419 ospf_ls_upd_queue_send (oi
, update
, rn
->p
.u
.prefix4
);
3421 /* list might not be empty. */
3422 if (listcount(update
) == 0)
3424 list_delete (rn
->info
);
3426 route_unlock_node (rn
);
3434 if (IS_DEBUG_OSPF_EVENT
)
3435 zlog_debug ("ospf_ls_upd_send_queue: update lists not cleared,"
3436 " %d nodes to try again, raising new event", again
);
3437 oi
->t_ls_upd_event
=
3438 thread_add_event (master
, ospf_ls_upd_send_queue_event
, oi
, 0);
3441 if (IS_DEBUG_OSPF_EVENT
)
3442 zlog_debug ("ospf_ls_upd_send_queue stop");
3448 ospf_ls_upd_send (struct ospf_neighbor
*nbr
, struct list
*update
, int flag
)
3450 struct ospf_interface
*oi
;
3451 struct ospf_lsa
*lsa
;
3452 struct prefix_ipv4 p
;
3453 struct route_node
*rn
;
3454 struct listnode
*node
;
3459 p
.prefixlen
= IPV4_MAX_BITLEN
;
3461 /* Decide destination address. */
3462 if (oi
->type
== OSPF_IFTYPE_VIRTUALLINK
)
3463 p
.prefix
= oi
->vl_data
->peer_addr
;
3464 else if (oi
->type
== OSPF_IFTYPE_POINTOPOINT
)
3465 p
.prefix
.s_addr
= htonl (OSPF_ALLSPFROUTERS
);
3466 else if (flag
== OSPF_SEND_PACKET_DIRECT
)
3467 p
.prefix
= nbr
->address
.u
.prefix4
;
3468 else if (oi
->state
== ISM_DR
|| oi
->state
== ISM_Backup
)
3469 p
.prefix
.s_addr
= htonl (OSPF_ALLSPFROUTERS
);
3470 else if (oi
->type
== OSPF_IFTYPE_POINTOMULTIPOINT
)
3471 p
.prefix
.s_addr
= htonl (OSPF_ALLSPFROUTERS
);
3473 p
.prefix
.s_addr
= htonl (OSPF_ALLDROUTERS
);
3475 if (oi
->type
== OSPF_IFTYPE_NBMA
)
3477 if (flag
== OSPF_SEND_PACKET_INDIRECT
)
3478 zlog_warn ("* LS-Update is directly sent on NBMA network.");
3479 if (IPV4_ADDR_SAME(&oi
->address
->u
.prefix4
, &p
.prefix
.s_addr
))
3480 zlog_warn ("* LS-Update is sent to myself.");
3483 rn
= route_node_get (oi
->ls_upd_queue
, (struct prefix
*) &p
);
3485 if (rn
->info
== NULL
)
3486 rn
->info
= list_new ();
3488 for (ALL_LIST_ELEMENTS_RO (update
, node
, lsa
))
3489 listnode_add (rn
->info
, ospf_lsa_lock (lsa
)); /* oi->ls_upd_queue */
3491 if (oi
->t_ls_upd_event
== NULL
)
3492 oi
->t_ls_upd_event
=
3493 thread_add_event (master
, ospf_ls_upd_send_queue_event
, oi
, 0);
3497 ospf_ls_ack_send_list (struct ospf_interface
*oi
, struct list
*ack
,
3500 struct ospf_packet
*op
;
3501 u_int16_t length
= OSPF_HEADER_SIZE
;
3503 op
= ospf_packet_new (oi
->ifp
->mtu
);
3505 /* Prepare OSPF common header. */
3506 ospf_make_header (OSPF_MSG_LS_ACK
, oi
, op
->s
);
3508 /* Prepare OSPF Link State Acknowledgment body. */
3509 length
+= ospf_make_ls_ack (oi
, ack
, op
->s
);
3511 /* Fill OSPF header. */
3512 ospf_fill_header (oi
, op
->s
, length
);
3514 /* Set packet length. */
3515 op
->length
= length
;
3517 /* Set destination IP address. */
3520 /* Add packet to the interface output queue. */
3521 ospf_packet_add (oi
, op
);
3523 /* Hook thread to write packet. */
3524 OSPF_ISM_WRITE_ON (oi
->ospf
);
3528 ospf_ls_ack_send_event (struct thread
*thread
)
3530 struct ospf_interface
*oi
= THREAD_ARG (thread
);
3532 oi
->t_ls_ack_direct
= NULL
;
3534 while (listcount (oi
->ls_ack_direct
.ls_ack
))
3535 ospf_ls_ack_send_list (oi
, oi
->ls_ack_direct
.ls_ack
,
3536 oi
->ls_ack_direct
.dst
);
3542 ospf_ls_ack_send (struct ospf_neighbor
*nbr
, struct ospf_lsa
*lsa
)
3544 struct ospf_interface
*oi
= nbr
->oi
;
3546 if (listcount (oi
->ls_ack_direct
.ls_ack
) == 0)
3547 oi
->ls_ack_direct
.dst
= nbr
->address
.u
.prefix4
;
3549 listnode_add (oi
->ls_ack_direct
.ls_ack
, ospf_lsa_lock (lsa
));
3551 if (oi
->t_ls_ack_direct
== NULL
)
3552 oi
->t_ls_ack_direct
=
3553 thread_add_event (master
, ospf_ls_ack_send_event
, oi
, 0);
3556 /* Send Link State Acknowledgment delayed. */
3558 ospf_ls_ack_send_delayed (struct ospf_interface
*oi
)
3562 /* Decide destination address. */
3563 /* RFC2328 Section 13.5 On non-broadcast
3564 networks, delayed Link State Acknowledgment packets must be
3565 unicast separately over each adjacency (i.e., neighbor whose
3566 state is >= Exchange). */
3567 if (oi
->type
== OSPF_IFTYPE_NBMA
)
3569 struct ospf_neighbor
*nbr
;
3570 struct route_node
*rn
;
3572 for (rn
= route_top (oi
->nbrs
); rn
; rn
= route_next (rn
))
3573 if ((nbr
= rn
->info
) != NULL
)
3574 if (nbr
!= oi
->nbr_self
&& nbr
->state
>= NSM_Exchange
)
3575 while (listcount (oi
->ls_ack
))
3576 ospf_ls_ack_send_list (oi
, oi
->ls_ack
, nbr
->address
.u
.prefix4
);
3579 if (oi
->type
== OSPF_IFTYPE_VIRTUALLINK
)
3580 dst
.s_addr
= oi
->vl_data
->peer_addr
.s_addr
;
3581 else if (oi
->state
== ISM_DR
|| oi
->state
== ISM_Backup
)
3582 dst
.s_addr
= htonl (OSPF_ALLSPFROUTERS
);
3583 else if (oi
->type
== OSPF_IFTYPE_POINTOPOINT
)
3584 dst
.s_addr
= htonl (OSPF_ALLSPFROUTERS
);
3585 else if (oi
->type
== OSPF_IFTYPE_POINTOMULTIPOINT
)
3586 dst
.s_addr
= htonl (OSPF_ALLSPFROUTERS
);
3588 dst
.s_addr
= htonl (OSPF_ALLDROUTERS
);
3590 while (listcount (oi
->ls_ack
))
3591 ospf_ls_ack_send_list (oi
, oi
->ls_ack
, dst
);