2 * OSPF Sending and Receiving OSPF Packets.
3 * Copyright (C) 1999, 2000 Toshiaki Takada
5 * This file is part of GNU Zebra.
7 * GNU Zebra is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with GNU Zebra; see the file COPYING. If not, write to the Free
19 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
31 #include "sockunion.h"
37 #include "ospfd/ospfd.h"
38 #include "ospfd/ospf_network.h"
39 #include "ospfd/ospf_interface.h"
40 #include "ospfd/ospf_ism.h"
41 #include "ospfd/ospf_asbr.h"
42 #include "ospfd/ospf_lsa.h"
43 #include "ospfd/ospf_lsdb.h"
44 #include "ospfd/ospf_neighbor.h"
45 #include "ospfd/ospf_nsm.h"
46 #include "ospfd/ospf_packet.h"
47 #include "ospfd/ospf_spf.h"
48 #include "ospfd/ospf_flood.h"
49 #include "ospfd/ospf_dump.h"
51 static void ospf_ls_ack_send_list (struct ospf_interface
*, struct list
*,
54 /* Packet Type String. */
55 char *ospf_packet_type_str
[] =
59 "Database Description",
62 "Link State Acknowledgment",
65 extern int in_cksum (void *ptr
, int nbytes
);
67 /* OSPF authentication checking function */
69 ospf_auth_type (struct ospf_interface
*oi
)
73 if (OSPF_IF_PARAM (oi
, auth_type
) == OSPF_AUTH_NOTSET
)
74 auth_type
= oi
->area
->auth_type
;
76 auth_type
= OSPF_IF_PARAM (oi
, auth_type
);
78 /* Handle case where MD5 key list is not configured aka Cisco */
79 if (auth_type
== OSPF_AUTH_CRYPTOGRAPHIC
&&
80 list_isempty (OSPF_IF_PARAM (oi
, auth_crypt
)))
81 return OSPF_AUTH_NULL
;
87 /* forward output pointer. */
89 ospf_output_forward (struct stream
*s
, int size
)
95 ospf_packet_new (size_t size
)
97 struct ospf_packet
*new;
99 new = XCALLOC (MTYPE_OSPF_PACKET
, sizeof (struct ospf_packet
));
100 new->s
= stream_new (size
);
106 ospf_packet_free (struct ospf_packet
*op
)
111 XFREE (MTYPE_OSPF_PACKET
, op
);
119 struct ospf_fifo
*new;
121 new = XCALLOC (MTYPE_OSPF_FIFO
, sizeof (struct ospf_fifo
));
125 /* Add new packet to fifo. */
127 ospf_fifo_push (struct ospf_fifo
*fifo
, struct ospf_packet
*op
)
130 fifo
->tail
->next
= op
;
139 /* Delete first packet from fifo. */
141 ospf_fifo_pop (struct ospf_fifo
*fifo
)
143 struct ospf_packet
*op
;
149 fifo
->head
= op
->next
;
151 if (fifo
->head
== NULL
)
160 /* Return first fifo entry. */
162 ospf_fifo_head (struct ospf_fifo
*fifo
)
167 /* Flush ospf packet fifo. */
169 ospf_fifo_flush (struct ospf_fifo
*fifo
)
171 struct ospf_packet
*op
;
172 struct ospf_packet
*next
;
174 for (op
= fifo
->head
; op
; op
= next
)
177 ospf_packet_free (op
);
179 fifo
->head
= fifo
->tail
= NULL
;
183 /* Free ospf packet fifo. */
185 ospf_fifo_free (struct ospf_fifo
*fifo
)
187 ospf_fifo_flush (fifo
);
189 XFREE (MTYPE_OSPF_FIFO
, fifo
);
193 ospf_packet_add (struct ospf_interface
*oi
, struct ospf_packet
*op
)
195 /* Add packet to end of queue. */
196 ospf_fifo_push (oi
->obuf
, op
);
198 /* Debug of packet fifo*/
199 /* ospf_fifo_debug (oi->obuf); */
203 ospf_packet_delete (struct ospf_interface
*oi
)
205 struct ospf_packet
*op
;
207 op
= ospf_fifo_pop (oi
->obuf
);
210 ospf_packet_free (op
);
214 ospf_stream_copy (struct stream
*new, struct stream
*s
)
220 memcpy (new->data
, s
->data
, stream_get_endp (s
));
226 ospf_packet_dup (struct ospf_packet
*op
)
228 struct ospf_packet
*new;
230 if (stream_get_endp(op
->s
) != op
->length
)
231 zlog_warn ("ospf_packet_dup stream %ld ospf_packet %d size mismatch",
232 STREAM_SIZE(op
->s
), op
->length
);
234 /* Reserve space for MD5 authentication that may be added later. */
235 new = ospf_packet_new (stream_get_endp(op
->s
) + OSPF_AUTH_MD5_SIZE
);
236 ospf_stream_copy (new->s
, op
->s
);
239 new->length
= op
->length
;
245 ospf_packet_max (struct ospf_interface
*oi
)
249 if ( ospf_auth_type (oi
) == OSPF_AUTH_CRYPTOGRAPHIC
)
250 max
= oi
->ifp
->mtu
- OSPF_AUTH_MD5_SIZE
;
254 max
-= (OSPF_HEADER_SIZE
+ sizeof (struct ip
));
261 ospf_check_md5_digest (struct ospf_interface
*oi
, struct stream
*s
,
266 unsigned char digest
[OSPF_AUTH_MD5_SIZE
];
267 unsigned char *pdigest
;
268 struct crypt_key
*ck
;
269 struct ospf_header
*ospfh
;
270 struct ospf_neighbor
*nbr
;
273 ibuf
= STREAM_PNT (s
);
274 ospfh
= (struct ospf_header
*) ibuf
;
276 /* Get pointer to the end of the packet. */
277 pdigest
= ibuf
+ length
;
279 /* Get secret key. */
280 ck
= ospf_crypt_key_lookup (OSPF_IF_PARAM (oi
, auth_crypt
),
281 ospfh
->u
.crypt
.key_id
);
284 zlog_warn ("interface %s: ospf_check_md5 no key %d",
285 IF_NAME (oi
), ospfh
->u
.crypt
.key_id
);
289 /* check crypto seqnum. */
290 nbr
= ospf_nbr_lookup_by_routerid (oi
->nbrs
, &ospfh
->router_id
);
292 if (nbr
&& ntohl(nbr
->crypt_seqnum
) > ntohl(ospfh
->u
.crypt
.crypt_seqnum
))
294 zlog_warn ("interface %s: ospf_check_md5 bad sequence %d (expect %d)",
296 ntohl(ospfh
->u
.crypt
.crypt_seqnum
),
297 ntohl(nbr
->crypt_seqnum
));
301 /* Generate a digest for the ospf packet - their digest + our digest. */
303 md5_process_bytes (ibuf
, length
, &ctx
);
304 md5_process_bytes (ck
->auth_key
, OSPF_AUTH_MD5_SIZE
, &ctx
);
305 md5_finish_ctx (&ctx
, digest
);
307 /* compare the two */
308 if (memcmp (pdigest
, digest
, OSPF_AUTH_MD5_SIZE
))
310 zlog_warn ("interface %s: ospf_check_md5 checksum mismatch",
315 /* save neighbor's crypt_seqnum */
317 nbr
->crypt_seqnum
= ospfh
->u
.crypt
.crypt_seqnum
;
321 /* This function is called from ospf_write(), it will detect the
322 authentication scheme and if it is MD5, it will change the sequence
323 and update the MD5 digest. */
325 ospf_make_md5_digest (struct ospf_interface
*oi
, struct ospf_packet
*op
)
327 struct ospf_header
*ospfh
;
328 unsigned char digest
[OSPF_AUTH_MD5_SIZE
];
331 unsigned long oldputp
;
333 struct crypt_key
*ck
;
336 ibuf
= STREAM_DATA (op
->s
);
337 ospfh
= (struct ospf_header
*) ibuf
;
339 if (ntohs (ospfh
->auth_type
) != OSPF_AUTH_CRYPTOGRAPHIC
)
342 /* We do this here so when we dup a packet, we don't have to
343 waste CPU rewriting other headers. */
344 t
= (time(NULL
) & 0xFFFFFFFF);
345 oi
->crypt_seqnum
= ( t
> oi
->crypt_seqnum
? t
: oi
->crypt_seqnum
++);
346 ospfh
->u
.crypt
.crypt_seqnum
= htonl (oi
->crypt_seqnum
);
348 /* Get MD5 Authentication key from auth_key list. */
349 if (list_isempty (OSPF_IF_PARAM (oi
, auth_crypt
)))
353 ck
= getdata (OSPF_IF_PARAM (oi
, auth_crypt
)->tail
);
354 auth_key
= ck
->auth_key
;
357 /* Generate a digest for the entire packet + our secret key. */
359 md5_process_bytes (ibuf
, ntohs (ospfh
->length
), &ctx
);
360 md5_process_bytes (auth_key
, OSPF_AUTH_MD5_SIZE
, &ctx
);
361 md5_finish_ctx (&ctx
, digest
);
363 /* Append md5 digest to the end of the stream. */
364 oldputp
= stream_get_putp (op
->s
);
365 stream_set_putp (op
->s
, ntohs (ospfh
->length
));
366 stream_put (op
->s
, digest
, OSPF_AUTH_MD5_SIZE
);
367 stream_set_putp (op
->s
, oldputp
);
369 /* We do *NOT* increment the OSPF header length. */
370 op
->length
= ntohs (ospfh
->length
) + OSPF_AUTH_MD5_SIZE
;
372 if (stream_get_endp(op
->s
) != op
->length
)
373 zlog_warn("ospf_make_md5_digest: length mismatch stream %ld ospf_packet %d", stream_get_endp(op
->s
), op
->length
);
375 return OSPF_AUTH_MD5_SIZE
;
380 ospf_ls_req_timer (struct thread
*thread
)
382 struct ospf_neighbor
*nbr
;
384 nbr
= THREAD_ARG (thread
);
385 nbr
->t_ls_req
= NULL
;
387 /* Send Link State Request. */
388 if (ospf_ls_request_count (nbr
))
389 ospf_ls_req_send (nbr
);
391 /* Set Link State Request retransmission timer. */
392 OSPF_NSM_TIMER_ON (nbr
->t_ls_req
, ospf_ls_req_timer
, nbr
->v_ls_req
);
398 ospf_ls_req_event (struct ospf_neighbor
*nbr
)
402 thread_cancel (nbr
->t_ls_req
);
403 nbr
->t_ls_req
= NULL
;
405 nbr
->t_ls_req
= thread_add_event (master
, ospf_ls_req_timer
, nbr
, 0);
408 /* Cyclic timer function. Fist registered in ospf_nbr_new () in
411 ospf_ls_upd_timer (struct thread
*thread
)
413 struct ospf_neighbor
*nbr
;
415 nbr
= THREAD_ARG (thread
);
416 nbr
->t_ls_upd
= NULL
;
418 /* Send Link State Update. */
419 if (ospf_ls_retransmit_count (nbr
) > 0)
422 struct ospf_lsdb
*lsdb
;
425 int retransmit_interval
;
427 gettimeofday (&now
, NULL
);
428 retransmit_interval
= OSPF_IF_PARAM (nbr
->oi
, retransmit_interval
);
430 lsdb
= &nbr
->ls_rxmt
;
431 update
= list_new ();
433 for (i
= OSPF_MIN_LSA
; i
< OSPF_MAX_LSA
; i
++)
435 struct route_table
*table
= lsdb
->type
[i
].db
;
436 struct route_node
*rn
;
438 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
440 struct ospf_lsa
*lsa
;
442 if ((lsa
= rn
->info
) != NULL
)
443 /* Don't retransmit an LSA if we received it within
444 the last RxmtInterval seconds - this is to allow the
445 neighbour a chance to acknowledge the LSA as it may
446 have ben just received before the retransmit timer
447 fired. This is a small tweak to what is in the RFC,
448 but it will cut out out a lot of retransmit traffic
450 if (tv_cmp (tv_sub (now
, lsa
->tv_recv
),
451 int2tv (retransmit_interval
)) >= 0)
452 listnode_add (update
, rn
->info
);
456 if (listcount (update
) > 0)
457 ospf_ls_upd_send (nbr
, update
, OSPF_SEND_PACKET_DIRECT
);
458 list_delete (update
);
461 /* Set LS Update retransmission timer. */
462 OSPF_NSM_TIMER_ON (nbr
->t_ls_upd
, ospf_ls_upd_timer
, nbr
->v_ls_upd
);
468 ospf_ls_ack_timer (struct thread
*thread
)
470 struct ospf_interface
*oi
;
472 oi
= THREAD_ARG (thread
);
475 /* Send Link State Acknowledgment. */
476 if (listcount (oi
->ls_ack
) > 0)
477 ospf_ls_ack_send_delayed (oi
);
479 /* Set LS Ack timer. */
480 OSPF_ISM_TIMER_ON (oi
->t_ls_ack
, ospf_ls_ack_timer
, oi
->v_ls_ack
);
485 /* swab ip header fields to required order for sendmsg */
487 ospf_swab_iph_ton (struct ip
*iph
)
489 /* BSD and derived take iph in network order, except for
493 iph
->ip_len
= htons(iph
->ip_len
);
494 iph
->ip_off
= htons(iph
->ip_off
);
496 iph
->ip_id
= htons(iph
->ip_id
);
499 /* swab ip header fields to host order, as required */
501 ospf_swab_iph_toh (struct ip
*iph
)
504 iph
->ip_len
= ntohs(iph
->ip_len
);
505 iph
->ip_off
= ntohs(iph
->ip_off
);
507 iph
->ip_id
= ntohs(iph
->ip_id
);
511 ospf_write (struct thread
*thread
)
513 struct ospf
*ospf
= THREAD_ARG (thread
);
514 struct ospf_interface
*oi
;
515 struct ospf_packet
*op
;
516 struct sockaddr_in sa_dst
;
523 struct listnode
*node
;
524 static u_int16_t ipid
= 0;
525 u_int16_t maxdatasize
, offset
;
526 #define OSPF_WRITE_IPHL_SHIFT 2
527 #define OSPF_WRITE_FRAG_SHIFT 3
529 ospf
->t_write
= NULL
;
531 node
= listhead (ospf
->oi_write_q
);
536 /* seed ipid static with low order bits of time */
538 ipid
= (time(NULL
) & 0xffff);
540 /* convenience - max OSPF data per packet */
541 maxdatasize
= oi
->ifp
->mtu
- sizeof (struct ip
);
543 /* Get one packet from queue. */
544 op
= ospf_fifo_head (oi
->obuf
);
546 assert (op
->length
>= OSPF_HEADER_SIZE
);
548 if (op
->dst
.s_addr
== htonl (OSPF_ALLSPFROUTERS
)
549 || op
->dst
.s_addr
== htonl (OSPF_ALLDROUTERS
))
550 ospf_if_ipmulticast (ospf
, oi
->address
, oi
->ifp
->ifindex
);
552 /* Rewrite the md5 signature & update the seq */
553 ospf_make_md5_digest (oi
, op
);
555 /* reset get pointer */
556 stream_set_getp (op
->s
, 0);
558 memset (&iph
, 0, sizeof (struct ip
));
559 memset (&sa_dst
, 0, sizeof (sa_dst
));
561 sa_dst
.sin_family
= AF_INET
;
563 sa_dst
.sin_len
= sizeof(sa_dst
);
564 #endif /* HAVE_SIN_LEN */
565 sa_dst
.sin_addr
= op
->dst
;
566 sa_dst
.sin_port
= htons (0);
568 /* Set DONTROUTE flag if dst is unicast. */
569 if (oi
->type
!= OSPF_IFTYPE_VIRTUALLINK
)
570 if (!IN_MULTICAST (htonl (op
->dst
.s_addr
)))
571 flags
= MSG_DONTROUTE
;
573 iph
.ip_hl
= sizeof (struct ip
) >> OSPF_WRITE_IPHL_SHIFT
;
574 /* it'd be very strange for header to not be 4byte-word aligned but.. */
575 if ( sizeof (struct ip
) > (iph
.ip_hl
<< OSPF_WRITE_IPHL_SHIFT
) )
576 iph
.ip_hl
++; /* we presume sizeof struct ip cant overflow ip_hl.. */
578 iph
.ip_v
= IPVERSION
;
579 iph
.ip_tos
= IPTOS_PREC_INTERNETCONTROL
;
580 iph
.ip_len
= (iph
.ip_hl
<< OSPF_WRITE_IPHL_SHIFT
) + op
->length
;
583 /* XXX-MT: not thread-safe at all..
584 * XXX: this presumes this is only programme sending OSPF packets
585 * otherwise, no guarantee ipid will be unique
590 if (oi
->type
== OSPF_IFTYPE_VIRTUALLINK
)
591 iph
.ip_ttl
= OSPF_VL_IP_TTL
;
593 iph
.ip_ttl
= OSPF_IP_TTL
;
594 iph
.ip_p
= IPPROTO_OSPFIGP
;
596 iph
.ip_src
.s_addr
= oi
->address
->u
.prefix4
.s_addr
;
597 iph
.ip_dst
.s_addr
= op
->dst
.s_addr
;
599 memset (&msg
, 0, sizeof (msg
));
600 msg
.msg_name
= &sa_dst
;
601 msg
.msg_namelen
= sizeof (sa_dst
);
604 iov
[0].iov_base
= (char*)&iph
;
605 iov
[0].iov_len
= iph
.ip_hl
<< OSPF_WRITE_IPHL_SHIFT
;
606 iov
[1].iov_base
= STREAM_PNT (op
->s
);
607 iov
[1].iov_len
= op
->length
;
609 /* Sadly we can not rely on kernels to fragment packets because of either
610 * IP_HDRINCL and/or multicast destination being set.
612 if ( op
->length
> maxdatasize
)
614 assert ( op
->length
== stream_get_endp(op
->s
) );
618 * SunOS, BSD and BSD derived kernels likely will clear ip_id, as
619 * well as the IP_MF flag, making this all quite pointless.
621 * However, for a system on which IP_MF is left alone, and ip_id left
622 * alone or else which sets same ip_id for each fragment this might
625 * XXX-TODO: It would be much nicer to have the kernel's use their
626 * existing fragmentation support to do this for us. Bugs/RFEs need to
627 * be raised against the various kernels.
633 /* ip frag offset is expressed in units of 8byte words */
634 offset
= maxdatasize
>> OSPF_WRITE_FRAG_SHIFT
;
636 while ( (stream_get_endp(op
->s
) - stream_get_getp (op
->s
))
639 /* data length of this frag is to next offset value */
640 iov
[1].iov_len
= offset
<< OSPF_WRITE_FRAG_SHIFT
;
641 iph
.ip_len
= iov
[1].iov_len
+ sizeof (struct ip
);
642 assert (iph
.ip_len
<= oi
->ifp
->mtu
);
644 ospf_swab_iph_ton (&iph
);
646 ret
= sendmsg (ospf
->fd
, &msg
, flags
);
648 ospf_swab_iph_toh (&iph
);
651 zlog_warn ("*** sendmsg in ospf_write to %s,"
652 " id %d, off %d, len %d failed with %s",
653 inet_ntoa (iph
.ip_dst
),
659 iph
.ip_off
+= offset
;
660 stream_forward (op
->s
, iov
[1].iov_len
);
661 iov
[1].iov_base
= STREAM_PNT (op
->s
);
664 /* setup for final fragment */
665 iov
[1].iov_len
= stream_get_endp(op
->s
) - stream_get_getp (op
->s
);
666 iph
.ip_len
= iov
[1].iov_len
+ sizeof (struct ip
);
667 iph
.ip_off
&= (~IP_MF
);
670 /* send final fragment (could be first) */
671 ospf_swab_iph_ton (&iph
);
672 ret
= sendmsg (ospf
->fd
, &msg
, flags
);
673 ospf_swab_iph_toh (&iph
);
676 zlog_warn ("*** sendmsg in ospf_write to %s failed with %s",
677 inet_ntoa (iph
.ip_dst
), strerror (errno
));
679 /* Retrieve OSPF packet type. */
680 stream_set_getp (op
->s
, 1);
681 type
= stream_getc (op
->s
);
683 /* Show debug sending packet. */
684 if (IS_DEBUG_OSPF_PACKET (type
- 1, SEND
))
686 if (IS_DEBUG_OSPF_PACKET (type
- 1, DETAIL
))
688 zlog_info ("-----------------------------------------------------");
689 stream_set_getp (op
->s
, 0);
690 ospf_packet_dump (op
->s
);
693 zlog_info ("%s sent to [%s] via [%s].",
694 ospf_packet_type_str
[type
], inet_ntoa (op
->dst
),
697 if (IS_DEBUG_OSPF_PACKET (type
- 1, DETAIL
))
698 zlog_info ("-----------------------------------------------------");
701 /* Now delete packet from queue. */
702 ospf_packet_delete (oi
);
704 if (ospf_fifo_head (oi
->obuf
) == NULL
)
707 list_delete_node (ospf
->oi_write_q
, node
);
710 /* If packets still remain in queue, call write thread. */
711 if (!list_isempty (ospf
->oi_write_q
))
713 thread_add_write (master
, ospf_write
, ospf
, ospf
->fd
);
718 /* OSPF Hello message read -- RFC2328 Section 10.5. */
720 ospf_hello (struct ip
*iph
, struct ospf_header
*ospfh
,
721 struct stream
* s
, struct ospf_interface
*oi
, int size
)
723 struct ospf_hello
*hello
;
724 struct ospf_neighbor
*nbr
;
728 /* increment statistics. */
731 hello
= (struct ospf_hello
*) STREAM_PNT (s
);
733 /* If Hello is myself, silently discard. */
734 if (IPV4_ADDR_SAME (&ospfh
->router_id
, &oi
->ospf
->router_id
))
736 if (IS_DEBUG_OSPF_PACKET (ospfh
->type
- 1, RECV
))
738 zlog_info ("ospf_header[%s/%s]: selforiginated, "
740 ospf_packet_type_str
[ospfh
->type
],
741 inet_ntoa (iph
->ip_src
));
746 /* If incoming interface is passive one, ignore Hello. */
747 if (OSPF_IF_PARAM (oi
, passive_interface
) == OSPF_IF_PASSIVE
) {
748 zlog_info ("Packet %s [HELLO:RECV]: oi is passive",
749 inet_ntoa (ospfh
->router_id
));
753 /* get neighbor prefix. */
755 p
.prefixlen
= ip_masklen (hello
->network_mask
);
756 p
.u
.prefix4
= iph
->ip_src
;
758 /* Compare network mask. */
759 /* Checking is ignored for Point-to-Point and Virtual link. */
760 if (oi
->type
!= OSPF_IFTYPE_POINTOPOINT
761 && oi
->type
!= OSPF_IFTYPE_VIRTUALLINK
)
762 if (oi
->address
->prefixlen
!= p
.prefixlen
)
764 zlog_warn ("Packet %s [Hello:RECV]: NetworkMask mismatch.",
765 inet_ntoa (ospfh
->router_id
));
769 /* Compare Hello Interval. */
770 if (OSPF_IF_PARAM (oi
, v_hello
) != ntohs (hello
->hello_interval
))
772 zlog_warn ("Packet %s [Hello:RECV]: HelloInterval mismatch.",
773 inet_ntoa (ospfh
->router_id
));
777 /* Compare Router Dead Interval. */
778 if (OSPF_IF_PARAM (oi
, v_wait
) != ntohl (hello
->dead_interval
))
780 zlog_warn ("Packet %s [Hello:RECV]: RouterDeadInterval mismatch.",
781 inet_ntoa (ospfh
->router_id
));
785 if (IS_DEBUG_OSPF_EVENT
)
786 zlog_info ("Packet %s [Hello:RECV]: Options %s",
787 inet_ntoa (ospfh
->router_id
),
788 ospf_options_dump (hello
->options
));
790 /* Compare options. */
791 #define REJECT_IF_TBIT_ON 1 /* XXX */
792 #ifdef REJECT_IF_TBIT_ON
793 if (CHECK_FLAG (hello
->options
, OSPF_OPTION_T
))
796 * This router does not support non-zero TOS.
797 * Drop this Hello packet not to establish neighbor relationship.
799 zlog_warn ("Packet %s [Hello:RECV]: T-bit on, drop it.",
800 inet_ntoa (ospfh
->router_id
));
803 #endif /* REJECT_IF_TBIT_ON */
805 #ifdef HAVE_OPAQUE_LSA
806 if (CHECK_FLAG (oi
->ospf
->config
, OSPF_OPAQUE_CAPABLE
)
807 && CHECK_FLAG (hello
->options
, OSPF_OPTION_O
))
810 * This router does know the correct usage of O-bit
811 * the bit should be set in DD packet only.
813 zlog_warn ("Packet %s [Hello:RECV]: O-bit abuse?",
814 inet_ntoa (ospfh
->router_id
));
815 #ifdef STRICT_OBIT_USAGE_CHECK
816 return; /* Reject this packet. */
817 #else /* STRICT_OBIT_USAGE_CHECK */
818 UNSET_FLAG (hello
->options
, OSPF_OPTION_O
); /* Ignore O-bit. */
819 #endif /* STRICT_OBIT_USAGE_CHECK */
821 #endif /* HAVE_OPAQUE_LSA */
823 /* new for NSSA is to ensure that NP is on and E is off */
825 if (oi
->area
->external_routing
== OSPF_AREA_NSSA
)
827 if (! (CHECK_FLAG (OPTIONS (oi
), OSPF_OPTION_NP
)
828 && CHECK_FLAG (hello
->options
, OSPF_OPTION_NP
)
829 && ! CHECK_FLAG (OPTIONS (oi
), OSPF_OPTION_E
)
830 && ! CHECK_FLAG (hello
->options
, OSPF_OPTION_E
)))
832 zlog_warn ("NSSA-Packet-%s[Hello:RECV]: my options: %x, his options %x", inet_ntoa (ospfh
->router_id
), OPTIONS (oi
), hello
->options
);
835 if (IS_DEBUG_OSPF_NSSA
)
836 zlog_info ("NSSA-Hello:RECV:Packet from %s:", inet_ntoa(ospfh
->router_id
));
839 /* The setting of the E-bit found in the Hello Packet's Options
840 field must match this area's ExternalRoutingCapability A
841 mismatch causes processing to stop and the packet to be
842 dropped. The setting of the rest of the bits in the Hello
843 Packet's Options field should be ignored. */
844 if (CHECK_FLAG (OPTIONS (oi
), OSPF_OPTION_E
) !=
845 CHECK_FLAG (hello
->options
, OSPF_OPTION_E
))
847 zlog_warn ("Packet[Hello:RECV]: my options: %x, his options %x",
848 OPTIONS (oi
), hello
->options
);
852 /* get neighbour struct */
853 nbr
= ospf_nbr_get (oi
, ospfh
, iph
, &p
);
855 /* neighbour must be valid, ospf_nbr_get creates if none existed */
858 old_state
= nbr
->state
;
860 /* Add event to thread. */
861 OSPF_NSM_EVENT_EXECUTE (nbr
, NSM_HelloReceived
);
863 /* RFC2328 Section 9.5.1
864 If the router is not eligible to become Designated Router,
865 (snip) It must also send an Hello Packet in reply to an
866 Hello Packet received from any eligible neighbor (other than
867 the current Designated Router and Backup Designated Router). */
868 if (oi
->type
== OSPF_IFTYPE_NBMA
)
869 if (PRIORITY(oi
) == 0 && hello
->priority
> 0
870 && IPV4_ADDR_CMP(&DR(oi
), &iph
->ip_src
)
871 && IPV4_ADDR_CMP(&BDR(oi
), &iph
->ip_src
))
872 OSPF_NSM_TIMER_ON (nbr
->t_hello_reply
, ospf_hello_reply_timer
,
873 OSPF_HELLO_REPLY_DELAY
);
875 /* on NBMA network type, it happens to receive bidirectional Hello packet
876 without advance 1-Way Received event.
877 To avoid incorrect DR-seletion, raise 1-Way Received event.*/
878 if (oi
->type
== OSPF_IFTYPE_NBMA
&&
879 (old_state
== NSM_Down
|| old_state
== NSM_Attempt
))
881 OSPF_NSM_EVENT_EXECUTE (nbr
, NSM_OneWayReceived
);
882 nbr
->priority
= hello
->priority
;
883 nbr
->d_router
= hello
->d_router
;
884 nbr
->bd_router
= hello
->bd_router
;
888 if (ospf_nbr_bidirectional (&oi
->ospf
->router_id
, hello
->neighbors
,
889 size
- OSPF_HELLO_MIN_SIZE
))
891 OSPF_NSM_EVENT_EXECUTE (nbr
, NSM_TwoWayReceived
);
892 nbr
->options
|= hello
->options
;
896 OSPF_NSM_EVENT_EXECUTE (nbr
, NSM_OneWayReceived
);
897 /* Set neighbor information. */
898 nbr
->priority
= hello
->priority
;
899 nbr
->d_router
= hello
->d_router
;
900 nbr
->bd_router
= hello
->bd_router
;
904 /* If neighbor itself declares DR and no BDR exists,
905 cause event BackupSeen */
906 if (IPV4_ADDR_SAME (&nbr
->address
.u
.prefix4
, &hello
->d_router
))
907 if (hello
->bd_router
.s_addr
== 0 && oi
->state
== ISM_Waiting
)
908 OSPF_ISM_EVENT_SCHEDULE (oi
, ISM_BackupSeen
);
910 /* neighbor itself declares BDR. */
911 if (oi
->state
== ISM_Waiting
&&
912 IPV4_ADDR_SAME (&nbr
->address
.u
.prefix4
, &hello
->bd_router
))
913 OSPF_ISM_EVENT_SCHEDULE (oi
, ISM_BackupSeen
);
915 /* had not previously. */
916 if ((IPV4_ADDR_SAME (&nbr
->address
.u
.prefix4
, &hello
->d_router
) &&
917 IPV4_ADDR_CMP (&nbr
->address
.u
.prefix4
, &nbr
->d_router
)) ||
918 (IPV4_ADDR_CMP (&nbr
->address
.u
.prefix4
, &hello
->d_router
) &&
919 IPV4_ADDR_SAME (&nbr
->address
.u
.prefix4
, &nbr
->d_router
)))
920 OSPF_ISM_EVENT_SCHEDULE (oi
, ISM_NeighborChange
);
922 /* had not previously. */
923 if ((IPV4_ADDR_SAME (&nbr
->address
.u
.prefix4
, &hello
->bd_router
) &&
924 IPV4_ADDR_CMP (&nbr
->address
.u
.prefix4
, &nbr
->bd_router
)) ||
925 (IPV4_ADDR_CMP (&nbr
->address
.u
.prefix4
, &hello
->bd_router
) &&
926 IPV4_ADDR_SAME (&nbr
->address
.u
.prefix4
, &nbr
->bd_router
)))
927 OSPF_ISM_EVENT_SCHEDULE (oi
, ISM_NeighborChange
);
929 /* Neighbor priority check. */
930 if (nbr
->priority
>= 0 && nbr
->priority
!= hello
->priority
)
931 OSPF_ISM_EVENT_SCHEDULE (oi
, ISM_NeighborChange
);
933 /* Set neighbor information. */
934 nbr
->priority
= hello
->priority
;
935 nbr
->d_router
= hello
->d_router
;
936 nbr
->bd_router
= hello
->bd_router
;
939 /* Save DD flags/options/Seqnum received. */
941 ospf_db_desc_save_current (struct ospf_neighbor
*nbr
,
942 struct ospf_db_desc
*dd
)
944 nbr
->last_recv
.flags
= dd
->flags
;
945 nbr
->last_recv
.options
= dd
->options
;
946 nbr
->last_recv
.dd_seqnum
= ntohl (dd
->dd_seqnum
);
949 /* Process rest of DD packet. */
951 ospf_db_desc_proc (struct stream
*s
, struct ospf_interface
*oi
,
952 struct ospf_neighbor
*nbr
, struct ospf_db_desc
*dd
,
955 struct ospf_lsa
*new, *find
;
956 struct lsa_header
*lsah
;
958 stream_forward (s
, OSPF_DB_DESC_MIN_SIZE
);
959 for (size
-= OSPF_DB_DESC_MIN_SIZE
;
960 size
>= OSPF_LSA_HEADER_SIZE
; size
-= OSPF_LSA_HEADER_SIZE
)
962 lsah
= (struct lsa_header
*) STREAM_PNT (s
);
963 stream_forward (s
, OSPF_LSA_HEADER_SIZE
);
965 /* Unknown LS type. */
966 if (lsah
->type
< OSPF_MIN_LSA
|| lsah
->type
>= OSPF_MAX_LSA
)
968 zlog_warn ("Pakcet [DD:RECV]: Unknown LS type %d.", lsah
->type
);
969 OSPF_NSM_EVENT_SCHEDULE (nbr
, NSM_SeqNumberMismatch
);
973 #ifdef HAVE_OPAQUE_LSA
974 if (IS_OPAQUE_LSA (lsah
->type
)
975 && ! CHECK_FLAG (nbr
->options
, OSPF_OPTION_O
))
977 zlog_warn ("LSA[Type%d:%s]: Opaque capability mismatch?", lsah
->type
, inet_ntoa (lsah
->id
));
978 OSPF_NSM_EVENT_SCHEDULE (nbr
, NSM_SeqNumberMismatch
);
981 #endif /* HAVE_OPAQUE_LSA */
985 case OSPF_AS_EXTERNAL_LSA
:
986 #ifdef HAVE_OPAQUE_LSA
987 case OSPF_OPAQUE_AS_LSA
:
988 #endif /* HAVE_OPAQUE_LSA */
989 /* Check for stub area. Reject if AS-External from stub but
990 allow if from NSSA. */
991 if (oi
->area
->external_routing
== OSPF_AREA_STUB
)
993 zlog_warn ("Packet [DD:RECV]: LSA[Type%d:%s] from %s area.",
994 lsah
->type
, inet_ntoa (lsah
->id
),
995 (oi
->area
->external_routing
== OSPF_AREA_STUB
) ?\
997 OSPF_NSM_EVENT_SCHEDULE (nbr
, NSM_SeqNumberMismatch
);
1005 /* Create LS-request object. */
1006 new = ospf_ls_request_new (lsah
);
1008 /* Lookup received LSA, then add LS request list. */
1009 find
= ospf_lsa_lookup_by_header (oi
->area
, lsah
);
1010 if (!find
|| ospf_lsa_more_recent (find
, new) < 0)
1012 ospf_ls_request_add (nbr
, new);
1013 ospf_lsa_discard (new);
1017 /* Received LSA is not recent. */
1018 if (IS_DEBUG_OSPF_EVENT
)
1019 zlog_info ("Packet [DD:RECV]: LSA received Type %d, "
1020 "ID %s is not recent.", lsah
->type
, inet_ntoa (lsah
->id
));
1021 ospf_lsa_discard (new);
1027 if (IS_SET_DD_MS (nbr
->dd_flags
))
1030 /* Entire DD packet sent. */
1031 if (!IS_SET_DD_M (dd
->flags
) && !IS_SET_DD_M (nbr
->dd_flags
))
1032 OSPF_NSM_EVENT_SCHEDULE (nbr
, NSM_ExchangeDone
);
1034 /* Send new DD packet. */
1035 ospf_db_desc_send (nbr
);
1040 nbr
->dd_seqnum
= ntohl (dd
->dd_seqnum
);
1042 /* When master's more flags is not set. */
1043 if (!IS_SET_DD_M (dd
->flags
) && ospf_db_summary_isempty (nbr
))
1045 nbr
->dd_flags
&= ~(OSPF_DD_FLAG_M
);
1046 OSPF_NSM_EVENT_SCHEDULE (nbr
, NSM_ExchangeDone
);
1049 /* Send DD pakcet in reply. */
1050 ospf_db_desc_send (nbr
);
1053 /* Save received neighbor values from DD. */
1054 ospf_db_desc_save_current (nbr
, dd
);
1058 ospf_db_desc_is_dup (struct ospf_db_desc
*dd
, struct ospf_neighbor
*nbr
)
1060 /* Is DD duplicated? */
1061 if (dd
->options
== nbr
->last_recv
.options
&&
1062 dd
->flags
== nbr
->last_recv
.flags
&&
1063 dd
->dd_seqnum
== htonl (nbr
->last_recv
.dd_seqnum
))
1069 /* OSPF Database Description message read -- RFC2328 Section 10.6. */
1071 ospf_db_desc (struct ip
*iph
, struct ospf_header
*ospfh
,
1072 struct stream
*s
, struct ospf_interface
*oi
, u_int16_t size
)
1074 struct ospf_db_desc
*dd
;
1075 struct ospf_neighbor
*nbr
;
1077 /* Increment statistics. */
1080 dd
= (struct ospf_db_desc
*) STREAM_PNT (s
);
1082 nbr
= ospf_nbr_lookup (oi
, iph
, ospfh
);
1085 zlog_warn ("Packet[DD]: Unknown Neighbor %s",
1086 inet_ntoa (ospfh
->router_id
));
1091 if (ntohs (dd
->mtu
) > oi
->ifp
->mtu
)
1093 zlog_warn ("Packet[DD]: MTU is larger than [%s]'s MTU", IF_NAME (oi
));
1098 * XXX HACK by Hasso Tepper. Setting N/P bit in NSSA area DD packets is not
1099 * required. In fact at least JunOS sends DD packets with P bit clear.
1100 * Until proper solution is developped, this hack should help.
1102 * Update: According to the RFCs, N bit is specified /only/ for Hello
1103 * options, unfortunately its use in DD options is not specified. Hence some
1104 * implementations follow E-bit semantics and set it in DD options, and some
1105 * treat it as unspecified and hence follow the directive "default for
1106 * options is clear", ie unset.
1108 * Reset the flag, as ospfd follows E-bit semantics.
1110 if ( (oi
->area
->external_routing
== OSPF_AREA_NSSA
)
1111 && (CHECK_FLAG (nbr
->options
, OSPF_OPTION_NP
))
1112 && (!CHECK_FLAG (dd
->options
, OSPF_OPTION_NP
)) )
1114 if (IS_DEBUG_OSPF_EVENT
)
1115 zlog_notice ("Packet[DD]: Neighbour %s: Has NSSA capability, sends with N bit clear in DD options",
1116 inet_ntoa (nbr
->router_id
) );
1117 SET_FLAG (dd
->options
, OSPF_OPTION_NP
);
1120 #ifdef REJECT_IF_TBIT_ON
1121 if (CHECK_FLAG (dd
->options
, OSPF_OPTION_T
))
1124 * In Hello protocol, optional capability must have checked
1125 * to prevent this T-bit enabled router be my neighbor.
1127 zlog_warn ("Packet[DD]: Neighbor %s: T-bit on?", inet_ntoa (nbr
->router_id
));
1130 #endif /* REJECT_IF_TBIT_ON */
1132 #ifdef HAVE_OPAQUE_LSA
1133 if (CHECK_FLAG (dd
->options
, OSPF_OPTION_O
)
1134 && !CHECK_FLAG (oi
->ospf
->config
, OSPF_OPAQUE_CAPABLE
))
1137 * This node is not configured to handle O-bit, for now.
1138 * Clear it to ignore unsupported capability proposed by neighbor.
1140 UNSET_FLAG (dd
->options
, OSPF_OPTION_O
);
1142 #endif /* HAVE_OPAQUE_LSA */
1144 /* Process DD packet by neighbor status. */
1150 zlog_warn ("Packet[DD]: Neighbor state is %s, packet discarded.",
1151 LOOKUP (ospf_nsm_state_msg
, nbr
->state
));
1154 OSPF_NSM_EVENT_EXECUTE (nbr
, NSM_TwoWayReceived
);
1155 /* If the new state is ExStart, the processing of the current
1156 packet should then continue in this new state by falling
1157 through to case ExStart below. */
1158 if (nbr
->state
!= NSM_ExStart
)
1162 if ((IS_SET_DD_ALL (dd
->flags
) == OSPF_DD_FLAG_ALL
) &&
1163 (size
== OSPF_DB_DESC_MIN_SIZE
))
1165 if (IPV4_ADDR_CMP (&nbr
->router_id
, &oi
->ospf
->router_id
) > 0)
1167 /* We're Slave---obey */
1168 zlog_warn ("Packet[DD]: Negotiation done (Slave).");
1169 nbr
->dd_seqnum
= ntohl (dd
->dd_seqnum
);
1170 nbr
->dd_flags
&= ~(OSPF_DD_FLAG_MS
|OSPF_DD_FLAG_I
); /* Reset I/MS */
1174 /* We're Master, ignore the initial DBD from Slave */
1175 zlog_warn ("Packet[DD]: Initial DBD from Slave, ignoring.");
1179 /* Ack from the Slave */
1180 else if (!IS_SET_DD_MS (dd
->flags
) && !IS_SET_DD_I (dd
->flags
) &&
1181 ntohl (dd
->dd_seqnum
) == nbr
->dd_seqnum
&&
1182 IPV4_ADDR_CMP (&nbr
->router_id
, &oi
->ospf
->router_id
) < 0)
1184 zlog_warn ("Packet[DD]: Negotiation done (Master).");
1185 nbr
->dd_flags
&= ~OSPF_DD_FLAG_I
;
1189 zlog_warn ("Packet[DD]: Negotiation fails.");
1193 /* This is where the real Options are saved */
1194 nbr
->options
= dd
->options
;
1196 #ifdef HAVE_OPAQUE_LSA
1197 if (CHECK_FLAG (oi
->ospf
->config
, OSPF_OPAQUE_CAPABLE
))
1199 if (IS_DEBUG_OSPF_EVENT
)
1200 zlog_info ("Neighbor[%s] is %sOpaque-capable.",
1201 inet_ntoa (nbr
->router_id
),
1202 CHECK_FLAG (nbr
->options
, OSPF_OPTION_O
) ? "" : "NOT ");
1204 if (! CHECK_FLAG (nbr
->options
, OSPF_OPTION_O
)
1205 && IPV4_ADDR_SAME (&DR (oi
), &nbr
->address
.u
.prefix4
))
1207 zlog_warn ("DR-neighbor[%s] is NOT opaque-capable; Opaque-LSAs cannot be reliably advertised in this network.", inet_ntoa (nbr
->router_id
));
1208 /* This situation is undesirable, but not a real error. */
1211 #endif /* HAVE_OPAQUE_LSA */
1213 OSPF_NSM_EVENT_EXECUTE (nbr
, NSM_NegotiationDone
);
1215 /* continue processing rest of packet. */
1216 ospf_db_desc_proc (s
, oi
, nbr
, dd
, size
);
1219 if (ospf_db_desc_is_dup (dd
, nbr
))
1221 if (IS_SET_DD_MS (nbr
->dd_flags
))
1222 /* Master: discard duplicated DD packet. */
1223 zlog_warn ("Packet[DD] (Master): packet duplicated.");
1225 /* Slave: cause to retransmit the last Database Description. */
1227 zlog_warn ("Packet[DD] [Slave]: packet duplicated.");
1228 ospf_db_desc_resend (nbr
);
1233 /* Otherwise DD packet should be checked. */
1234 /* Check Master/Slave bit mismatch */
1235 if (IS_SET_DD_MS (dd
->flags
) != IS_SET_DD_MS (nbr
->last_recv
.flags
))
1237 zlog_warn ("Packet[DD]: MS-bit mismatch.");
1238 OSPF_NSM_EVENT_SCHEDULE (nbr
, NSM_SeqNumberMismatch
);
1239 if (IS_DEBUG_OSPF_EVENT
)
1240 zlog_info ("Packet[DD]: dd->flags=%d, nbr->dd_flags=%d",
1241 dd
->flags
, nbr
->dd_flags
);
1245 /* Check initialize bit is set. */
1246 if (IS_SET_DD_I (dd
->flags
))
1248 zlog_warn ("Packet[DD]: I-bit set.");
1249 OSPF_NSM_EVENT_SCHEDULE (nbr
, NSM_SeqNumberMismatch
);
1253 /* Check DD Options. */
1254 if (dd
->options
!= nbr
->options
)
1256 #ifdef ORIGINAL_CODING
1257 /* Save the new options for debugging */
1258 nbr
->options
= dd
->options
;
1259 #endif /* ORIGINAL_CODING */
1260 zlog_warn ("Packet[DD]: options mismatch.");
1261 OSPF_NSM_EVENT_SCHEDULE (nbr
, NSM_SeqNumberMismatch
);
1265 /* Check DD sequence number. */
1266 if ((IS_SET_DD_MS (nbr
->dd_flags
) &&
1267 ntohl (dd
->dd_seqnum
) != nbr
->dd_seqnum
) ||
1268 (!IS_SET_DD_MS (nbr
->dd_flags
) &&
1269 ntohl (dd
->dd_seqnum
) != nbr
->dd_seqnum
+ 1))
1271 zlog_warn ("Pakcet[DD]: sequence number mismatch.");
1272 OSPF_NSM_EVENT_SCHEDULE (nbr
, NSM_SeqNumberMismatch
);
1276 /* Continue processing rest of packet. */
1277 ospf_db_desc_proc (s
, oi
, nbr
, dd
, size
);
1281 if (ospf_db_desc_is_dup (dd
, nbr
))
1283 if (IS_SET_DD_MS (nbr
->dd_flags
))
1285 /* Master should discard duplicate DD packet. */
1286 zlog_warn ("Pakcet[DD]: duplicated, packet discarded.");
1291 struct timeval t
, now
;
1292 gettimeofday (&now
, NULL
);
1293 t
= tv_sub (now
, nbr
->last_send_ts
);
1294 if (tv_cmp (t
, int2tv (nbr
->v_inactivity
)) < 0)
1296 /* In states Loading and Full the slave must resend
1297 its last Database Description packet in response to
1298 duplicate Database Description packets received
1299 from the master. For this reason the slave must
1300 wait RouterDeadInterval seconds before freeing the
1301 last Database Description packet. Reception of a
1302 Database Description packet from the master after
1303 this interval will generate a SeqNumberMismatch
1304 neighbor event. RFC2328 Section 10.8 */
1305 ospf_db_desc_resend (nbr
);
1311 OSPF_NSM_EVENT_SCHEDULE (nbr
, NSM_SeqNumberMismatch
);
1314 zlog_warn ("Packet[DD]: NSM illegal status.");
1319 #define OSPF_LSA_KEY_SIZE 12 /* type(4) + id(4) + ar(4) */
1321 /* OSPF Link State Request Read -- RFC2328 Section 10.7. */
1323 ospf_ls_req (struct ip
*iph
, struct ospf_header
*ospfh
,
1324 struct stream
*s
, struct ospf_interface
*oi
, u_int16_t size
)
1326 struct ospf_neighbor
*nbr
;
1328 struct in_addr ls_id
;
1329 struct in_addr adv_router
;
1330 struct ospf_lsa
*find
;
1331 struct list
*ls_upd
;
1334 /* Increment statistics. */
1337 nbr
= ospf_nbr_lookup (oi
, iph
, ospfh
);
1340 zlog_warn ("Link State Request: Unknown Neighbor %s.",
1341 inet_ntoa (ospfh
->router_id
));
1345 /* Neighbor State should be Exchange or later. */
1346 if (nbr
->state
!= NSM_Exchange
&&
1347 nbr
->state
!= NSM_Loading
&&
1348 nbr
->state
!= NSM_Full
)
1350 zlog_warn ("Link State Request: Neighbor state is %s, packet discarded.",
1351 LOOKUP (ospf_nsm_state_msg
, nbr
->state
));
1355 /* Send Link State Update for ALL requested LSAs. */
1356 ls_upd
= list_new ();
1357 length
= OSPF_HEADER_SIZE
+ OSPF_LS_UPD_MIN_SIZE
;
1359 while (size
>= OSPF_LSA_KEY_SIZE
)
1361 /* Get one slice of Link State Request. */
1362 ls_type
= stream_getl (s
);
1363 ls_id
.s_addr
= stream_get_ipv4 (s
);
1364 adv_router
.s_addr
= stream_get_ipv4 (s
);
1366 /* Verify LSA type. */
1367 if (ls_type
< OSPF_MIN_LSA
|| ls_type
>= OSPF_MAX_LSA
)
1369 OSPF_NSM_EVENT_SCHEDULE (nbr
, NSM_BadLSReq
);
1370 list_delete (ls_upd
);
1374 /* Search proper LSA in LSDB. */
1375 find
= ospf_lsa_lookup (oi
->area
, ls_type
, ls_id
, adv_router
);
1378 OSPF_NSM_EVENT_SCHEDULE (nbr
, NSM_BadLSReq
);
1379 list_delete (ls_upd
);
1383 /* Packet overflows MTU size, send immediatly. */
1384 if (length
+ ntohs (find
->data
->length
) > OSPF_PACKET_MAX (oi
))
1386 if (oi
->type
== OSPF_IFTYPE_NBMA
)
1387 ospf_ls_upd_send (nbr
, ls_upd
, OSPF_SEND_PACKET_DIRECT
);
1389 ospf_ls_upd_send (nbr
, ls_upd
, OSPF_SEND_PACKET_INDIRECT
);
1391 /* Only remove list contents. Keep ls_upd. */
1392 list_delete_all_node (ls_upd
);
1394 length
= OSPF_HEADER_SIZE
+ OSPF_LS_UPD_MIN_SIZE
;
1397 /* Append LSA to update list. */
1398 listnode_add (ls_upd
, find
);
1399 length
+= ntohs (find
->data
->length
);
1401 size
-= OSPF_LSA_KEY_SIZE
;
1404 /* Send rest of Link State Update. */
1405 if (listcount (ls_upd
) > 0)
1407 if (oi
->type
== OSPF_IFTYPE_NBMA
)
1408 ospf_ls_upd_send (nbr
, ls_upd
, OSPF_SEND_PACKET_DIRECT
);
1410 ospf_ls_upd_send (nbr
, ls_upd
, OSPF_SEND_PACKET_INDIRECT
);
1412 list_delete (ls_upd
);
1418 /* Get the list of LSAs from Link State Update packet.
1419 And process some validation -- RFC2328 Section 13. (1)-(2). */
1420 static struct list
*
1421 ospf_ls_upd_list_lsa (struct ospf_neighbor
*nbr
, struct stream
*s
,
1422 struct ospf_interface
*oi
, size_t size
)
1424 u_int16_t count
, sum
;
1426 struct lsa_header
*lsah
;
1427 struct ospf_lsa
*lsa
;
1432 count
= stream_getl (s
);
1433 size
-= OSPF_LS_UPD_MIN_SIZE
; /* # LSAs */
1435 for (; size
>= OSPF_LSA_HEADER_SIZE
&& count
> 0;
1436 size
-= length
, stream_forward (s
, length
), count
--)
1438 lsah
= (struct lsa_header
*) STREAM_PNT (s
);
1439 length
= ntohs (lsah
->length
);
1443 zlog_warn ("Link State Update: LSA length exceeds packet size.");
1447 /* Validate the LSA's LS checksum. */
1448 sum
= lsah
->checksum
;
1449 if (sum
!= ospf_lsa_checksum (lsah
))
1451 zlog_warn ("Link State Update: LSA checksum error %x, %x.",
1452 sum
, lsah
->checksum
);
1456 /* Examine the LSA's LS type. */
1457 if (lsah
->type
< OSPF_MIN_LSA
|| lsah
->type
>= OSPF_MAX_LSA
)
1459 zlog_warn ("Link State Update: Unknown LS type %d", lsah
->type
);
1464 * What if the received LSA's age is greater than MaxAge?
1465 * Treat it as a MaxAge case -- endo.
1467 if (ntohs (lsah
->ls_age
) > OSPF_LSA_MAXAGE
)
1468 lsah
->ls_age
= htons (OSPF_LSA_MAXAGE
);
1470 #ifdef HAVE_OPAQUE_LSA
1471 if (CHECK_FLAG (nbr
->options
, OSPF_OPTION_O
))
1473 #ifdef STRICT_OBIT_USAGE_CHECK
1474 if ((IS_OPAQUE_LSA(lsah
->type
) &&
1475 ! CHECK_FLAG (lsah
->options
, OSPF_OPTION_O
))
1476 || (! IS_OPAQUE_LSA(lsah
->type
) &&
1477 CHECK_FLAG (lsah
->options
, OSPF_OPTION_O
)))
1480 * This neighbor must know the exact usage of O-bit;
1481 * the bit will be set in Type-9,10,11 LSAs only.
1483 zlog_warn ("LSA[Type%d:%s]: O-bit abuse?", lsah
->type
, inet_ntoa (lsah
->id
));
1486 #endif /* STRICT_OBIT_USAGE_CHECK */
1488 /* Do not take in AS External Opaque-LSAs if we are a stub. */
1489 if (lsah
->type
== OSPF_OPAQUE_AS_LSA
1490 && nbr
->oi
->area
->external_routing
!= OSPF_AREA_DEFAULT
)
1492 if (IS_DEBUG_OSPF_EVENT
)
1493 zlog_info ("LSA[Type%d:%s]: We are a stub, don't take this LSA.", lsah
->type
, inet_ntoa (lsah
->id
));
1497 else if (IS_OPAQUE_LSA(lsah
->type
))
1499 zlog_warn ("LSA[Type%d:%s]: Opaque capability mismatch?", lsah
->type
, inet_ntoa (lsah
->id
));
1502 #endif /* HAVE_OPAQUE_LSA */
1504 /* Create OSPF LSA instance. */
1505 lsa
= ospf_lsa_new ();
1507 /* We may wish to put some error checking if type NSSA comes in
1508 and area not in NSSA mode */
1511 case OSPF_AS_EXTERNAL_LSA
:
1512 #ifdef HAVE_OPAQUE_LSA
1513 case OSPF_OPAQUE_AS_LSA
:
1516 case OSPF_OPAQUE_LINK_LSA
:
1517 lsa
->oi
= oi
; /* Remember incoming interface for flooding control. */
1519 #endif /* HAVE_OPAQUE_LSA */
1521 lsa
->area
= oi
->area
;
1525 lsa
->data
= ospf_lsa_data_new (length
);
1526 memcpy (lsa
->data
, lsah
, length
);
1528 if (IS_DEBUG_OSPF_EVENT
)
1529 zlog_info("LSA[Type%d:%s]: %p new LSA created with Link State Update",
1530 lsa
->data
->type
, inet_ntoa (lsa
->data
->id
), lsa
);
1531 listnode_add (lsas
, lsa
);
1537 /* Cleanup Update list. */
1539 ospf_upd_list_clean (struct list
*lsas
)
1541 struct listnode
*node
;
1542 struct ospf_lsa
*lsa
;
1544 for (node
= listhead (lsas
); node
; nextnode (node
))
1545 if ((lsa
= getdata (node
)) != NULL
)
1546 ospf_lsa_discard (lsa
);
1551 /* OSPF Link State Update message read -- RFC2328 Section 13. */
1553 ospf_ls_upd (struct ip
*iph
, struct ospf_header
*ospfh
,
1554 struct stream
*s
, struct ospf_interface
*oi
, u_int16_t size
)
1556 struct ospf_neighbor
*nbr
;
1558 #ifdef HAVE_OPAQUE_LSA
1559 struct list
*mylsa_acks
, *mylsa_upds
;
1560 #endif /* HAVE_OPAQUE_LSA */
1561 struct listnode
*node
, *next
;
1562 struct ospf_lsa
*lsa
= NULL
;
1563 /* unsigned long ls_req_found = 0; */
1565 /* Dis-assemble the stream, update each entry, re-encapsulate for flooding */
1567 /* Increment statistics. */
1570 /* Check neighbor. */
1571 nbr
= ospf_nbr_lookup (oi
, iph
, ospfh
);
1574 zlog_warn ("Link State Update: Unknown Neighbor %s on int: %s",
1575 inet_ntoa (ospfh
->router_id
), IF_NAME (oi
));
1579 /* Check neighbor state. */
1580 if (nbr
->state
< NSM_Exchange
)
1582 zlog_warn ("Link State Update: Neighbor[%s] state is less than Exchange",
1583 inet_ntoa (ospfh
->router_id
));
1587 /* Get list of LSAs from Link State Update packet. - Also perorms Stages
1588 * 1 (validate LSA checksum) and 2 (check for LSA consistent type)
1591 lsas
= ospf_ls_upd_list_lsa (nbr
, s
, oi
, size
);
1593 #ifdef HAVE_OPAQUE_LSA
1595 * Prepare two kinds of lists to clean up unwanted self-originated
1596 * Opaque-LSAs from the routing domain as soon as possible.
1598 mylsa_acks
= list_new (); /* Let the sender cease retransmission. */
1599 mylsa_upds
= list_new (); /* Flush target LSAs if necessary. */
1602 * If self-originated Opaque-LSAs that have flooded before restart
1603 * are contained in the received LSUpd message, corresponding LSReq
1604 * messages to be sent may have to be modified.
1605 * To eliminate possible race conditions such that flushing and normal
1606 * updating for the same LSA would take place alternately, this trick
1607 * must be done before entering to the loop below.
1609 ospf_opaque_adjust_lsreq (nbr
, lsas
);
1610 #endif /* HAVE_OPAQUE_LSA */
1612 #define DISCARD_LSA(L,N) {\
1613 if (IS_DEBUG_OSPF_EVENT) \
1614 zlog_info ("ospf_lsa_discard() in ospf_ls_upd() point %d: lsa %p Type-%d", N, lsa, (int) lsa->data->type); \
1615 ospf_lsa_discard (L); \
1618 /* Process each LSA received in the one packet. */
1619 for (node
= listhead (lsas
); node
; node
= next
)
1621 struct ospf_lsa
*ls_ret
, *current
;
1626 lsa
= getdata (node
);
1628 if (IS_DEBUG_OSPF_NSSA
)
1630 char buf1
[INET_ADDRSTRLEN
];
1631 char buf2
[INET_ADDRSTRLEN
];
1632 char buf3
[INET_ADDRSTRLEN
];
1634 zlog_info("LSA Type-%d from %s, ID: %s, ADV: %s",
1636 inet_ntop (AF_INET
, &ospfh
->router_id
,
1637 buf1
, INET_ADDRSTRLEN
),
1638 inet_ntop (AF_INET
, &lsa
->data
->id
,
1639 buf2
, INET_ADDRSTRLEN
),
1640 inet_ntop (AF_INET
, &lsa
->data
->adv_router
,
1641 buf3
, INET_ADDRSTRLEN
));
1644 listnode_delete (lsas
, lsa
); /* We don't need it in list anymore */
1646 /* Validate Checksum - Done above by ospf_ls_upd_list_lsa() */
1648 /* LSA Type - Done above by ospf_ls_upd_list_lsa() */
1650 /* Do not take in AS External LSAs if we are a stub or NSSA. */
1652 /* Do not take in AS NSSA if this neighbor and we are not NSSA */
1654 /* Do take in Type-7's if we are an NSSA */
1656 /* If we are also an ABR, later translate them to a Type-5 packet */
1658 /* Later, an NSSA Re-fresh can Re-fresh Type-7's and an ABR will
1659 translate them to a separate Type-5 packet. */
1661 if (lsa
->data
->type
== OSPF_AS_EXTERNAL_LSA
)
1662 /* Reject from STUB or NSSA */
1663 if (nbr
->oi
->area
->external_routing
!= OSPF_AREA_DEFAULT
)
1665 DISCARD_LSA (lsa
, 1);
1666 if (IS_DEBUG_OSPF_NSSA
)
1667 zlog_info("Incoming External LSA Discarded: We are NSSA/STUB Area");
1670 if (lsa
->data
->type
== OSPF_AS_NSSA_LSA
)
1671 if (nbr
->oi
->area
->external_routing
!= OSPF_AREA_NSSA
)
1673 DISCARD_LSA (lsa
,2);
1674 if (IS_DEBUG_OSPF_NSSA
)
1675 zlog_info("Incoming NSSA LSA Discarded: Not NSSA Area");
1678 /* Find the LSA in the current database. */
1680 current
= ospf_lsa_lookup_by_header (oi
->area
, lsa
->data
);
1682 /* If the LSA's LS age is equal to MaxAge, and there is currently
1683 no instance of the LSA in the router's link state database,
1684 and none of router's neighbors are in states Exchange or Loading,
1685 then take the following actions. */
1687 if (IS_LSA_MAXAGE (lsa
) && !current
&&
1688 (ospf_nbr_count (oi
, NSM_Exchange
) +
1689 ospf_nbr_count (oi
, NSM_Loading
)) == 0)
1691 /* Response Link State Acknowledgment. */
1692 ospf_ls_ack_send (nbr
, lsa
);
1695 zlog_warn ("Link State Update: LS age is equal to MaxAge.");
1696 DISCARD_LSA (lsa
, 3);
1699 #ifdef HAVE_OPAQUE_LSA
1700 if (IS_OPAQUE_LSA (lsa
->data
->type
)
1701 && IPV4_ADDR_SAME (&lsa
->data
->adv_router
, &oi
->ospf
->router_id
))
1704 * Even if initial flushing seems to be completed, there might
1705 * be a case that self-originated LSA with MaxAge still remain
1706 * in the routing domain.
1707 * Just send an LSAck message to cease retransmission.
1709 if (IS_LSA_MAXAGE (lsa
))
1711 zlog_warn ("LSA[%s]: Boomerang effect?", dump_lsa_key (lsa
));
1712 ospf_ls_ack_send (nbr
, lsa
);
1713 ospf_lsa_discard (lsa
);
1715 if (current
!= NULL
&& ! IS_LSA_MAXAGE (current
))
1716 ospf_opaque_lsa_refresh_schedule (current
);
1721 * If an instance of self-originated Opaque-LSA is not found
1722 * in the LSDB, there are some possible cases here.
1724 * 1) This node lost opaque-capability after restart.
1725 * 2) Else, a part of opaque-type is no more supported.
1726 * 3) Else, a part of opaque-id is no more supported.
1728 * Anyway, it is still this node's responsibility to flush it.
1729 * Otherwise, the LSA instance remains in the routing domain
1730 * until its age reaches to MaxAge.
1732 if (current
== NULL
)
1734 if (IS_DEBUG_OSPF_EVENT
)
1735 zlog_info ("LSA[%s]: Previously originated Opaque-LSA, not found in the LSDB.", dump_lsa_key (lsa
));
1737 SET_FLAG (lsa
->flags
, OSPF_LSA_SELF
);
1738 listnode_add (mylsa_upds
, ospf_lsa_dup (lsa
));
1739 listnode_add (mylsa_acks
, ospf_lsa_lock (lsa
));
1743 #endif /* HAVE_OPAQUE_LSA */
1744 /* It might be happen that received LSA is self-originated network LSA, but
1745 * router ID is cahnged. So, we should check if LSA is a network-LSA whose
1746 * Link State ID is one of the router's own IP interface addresses but whose
1747 * Advertising Router is not equal to the router's own Router ID
1748 * According to RFC 2328 12.4.2 and 13.4 this LSA should be flushed.
1751 if(lsa
->data
->type
== OSPF_NETWORK_LSA
)
1753 struct listnode
*oi_node
;
1756 for(oi_node
= listhead(oi
->ospf
->oiflist
); oi_node
; oi_node
= nextnode(oi_node
))
1758 struct ospf_interface
*out_if
= getdata(oi_node
);
1762 if((IPV4_ADDR_SAME(&out_if
->address
->u
.prefix4
, &lsa
->data
->id
)) &&
1763 (!(IPV4_ADDR_SAME(&oi
->ospf
->router_id
, &lsa
->data
->adv_router
))))
1765 if(out_if
->network_lsa_self
)
1767 ospf_lsa_flush_area(lsa
,out_if
->area
);
1768 if(IS_DEBUG_OSPF_EVENT
)
1769 zlog_info ("ospf_lsa_discard() in ospf_ls_upd() point 9: lsa %p Type-%d",
1770 lsa
, (int) lsa
->data
->type
);
1771 ospf_lsa_discard (lsa
);
1781 /* (5) Find the instance of this LSA that is currently contained
1782 in the router's link state database. If there is no
1783 database copy, or the received LSA is more recent than
1784 the database copy the following steps must be performed. */
1786 if (current
== NULL
||
1787 (ret
= ospf_lsa_more_recent (current
, lsa
)) < 0)
1789 /* Actual flooding procedure. */
1790 if (ospf_flood (oi
->ospf
, nbr
, current
, lsa
) < 0) /* Trap NSSA later. */
1791 DISCARD_LSA (lsa
, 4);
1795 /* (6) Else, If there is an instance of the LSA on the sending
1796 neighbor's Link state request list, an error has occurred in
1797 the Database Exchange process. In this case, restart the
1798 Database Exchange process by generating the neighbor event
1799 BadLSReq for the sending neighbor and stop processing the
1800 Link State Update packet. */
1802 if (ospf_ls_request_lookup (nbr
, lsa
))
1804 OSPF_NSM_EVENT_SCHEDULE (nbr
, NSM_BadLSReq
);
1805 zlog_warn ("LSA instance exists on Link state request list");
1807 /* Clean list of LSAs. */
1808 ospf_upd_list_clean (lsas
);
1809 /* this lsa is not on lsas list already. */
1810 ospf_lsa_discard (lsa
);
1811 #ifdef HAVE_OPAQUE_LSA
1812 list_delete (mylsa_acks
);
1813 list_delete (mylsa_upds
);
1814 #endif /* HAVE_OPAQUE_LSA */
1818 /* If the received LSA is the same instance as the database copy
1819 (i.e., neither one is more recent) the following two steps
1820 should be performed: */
1824 /* If the LSA is listed in the Link state retransmission list
1825 for the receiving adjacency, the router itself is expecting
1826 an acknowledgment for this LSA. The router should treat the
1827 received LSA as an acknowledgment by removing the LSA from
1828 the Link state retransmission list. This is termed an
1829 "implied acknowledgment". */
1831 ls_ret
= ospf_ls_retransmit_lookup (nbr
, lsa
);
1835 ospf_ls_retransmit_delete (nbr
, ls_ret
);
1837 /* Delayed acknowledgment sent if advertisement received
1838 from Designated Router, otherwise do nothing. */
1839 if (oi
->state
== ISM_Backup
)
1840 if (NBR_IS_DR (nbr
))
1841 listnode_add (oi
->ls_ack
, ospf_lsa_lock (lsa
));
1843 DISCARD_LSA (lsa
, 5);
1846 /* Acknowledge the receipt of the LSA by sending a
1847 Link State Acknowledgment packet back out the receiving
1850 ospf_ls_ack_send (nbr
, lsa
);
1851 DISCARD_LSA (lsa
, 6);
1855 /* The database copy is more recent. If the database copy
1856 has LS age equal to MaxAge and LS sequence number equal to
1857 MaxSequenceNumber, simply discard the received LSA without
1858 acknowledging it. (In this case, the LSA's LS sequence number is
1859 wrapping, and the MaxSequenceNumber LSA must be completely
1860 flushed before any new LSA instance can be introduced). */
1862 else if (ret
> 0) /* Database copy is more recent */
1864 if (IS_LSA_MAXAGE (current
) &&
1865 current
->data
->ls_seqnum
== htonl (OSPF_MAX_SEQUENCE_NUMBER
))
1867 DISCARD_LSA (lsa
, 7);
1869 /* Otherwise, as long as the database copy has not been sent in a
1870 Link State Update within the last MinLSArrival seconds, send the
1871 database copy back to the sending neighbor, encapsulated within
1872 a Link State Update Packet. The Link State Update Packet should
1873 be sent directly to the neighbor. In so doing, do not put the
1874 database copy of the LSA on the neighbor's link state
1875 retransmission list, and do not acknowledge the received (less
1876 recent) LSA instance. */
1881 gettimeofday (&now
, NULL
);
1883 if (tv_cmp (tv_sub (now
, current
->tv_orig
),
1884 int2tv (OSPF_MIN_LS_ARRIVAL
)) > 0)
1885 /* Trap NSSA type later.*/
1886 ospf_ls_upd_send_lsa (nbr
, current
, OSPF_SEND_PACKET_DIRECT
);
1887 DISCARD_LSA (lsa
, 8);
1892 #ifdef HAVE_OPAQUE_LSA
1894 * Now that previously originated Opaque-LSAs those which not yet
1895 * installed into LSDB are captured, take several steps to clear
1896 * them completely from the routing domain, before proceeding to
1897 * origination for the current target Opaque-LSAs.
1899 while (listcount (mylsa_acks
) > 0)
1900 ospf_ls_ack_send_list (oi
, mylsa_acks
, nbr
->address
.u
.prefix4
);
1902 if (listcount (mylsa_upds
) > 0)
1903 ospf_opaque_self_originated_lsa_received (nbr
, mylsa_upds
);
1905 list_delete (mylsa_upds
);
1906 list_delete (mylsa_acks
);
1907 #endif /* HAVE_OPAQUE_LSA */
1909 assert (listcount (lsas
) == 0);
1913 /* OSPF Link State Acknowledgment message read -- RFC2328 Section 13.7. */
1915 ospf_ls_ack (struct ip
*iph
, struct ospf_header
*ospfh
,
1916 struct stream
*s
, struct ospf_interface
*oi
, u_int16_t size
)
1918 struct ospf_neighbor
*nbr
;
1919 #ifdef HAVE_OPAQUE_LSA
1920 struct list
*opaque_acks
;
1921 #endif /* HAVE_OPAQUE_LSA */
1923 /* increment statistics. */
1926 nbr
= ospf_nbr_lookup (oi
, iph
, ospfh
);
1929 zlog_warn ("Link State Acknowledgment: Unknown Neighbor %s.",
1930 inet_ntoa (ospfh
->router_id
));
1934 if (nbr
->state
< NSM_Exchange
)
1936 zlog_warn ("Link State Acknowledgment: State is less than Exchange.");
1940 #ifdef HAVE_OPAQUE_LSA
1941 opaque_acks
= list_new ();
1942 #endif /* HAVE_OPAQUE_LSA */
1944 while (size
>= OSPF_LSA_HEADER_SIZE
)
1946 struct ospf_lsa
*lsa
, *lsr
;
1948 lsa
= ospf_lsa_new ();
1949 lsa
->data
= (struct lsa_header
*) STREAM_PNT (s
);
1951 /* lsah = (struct lsa_header *) STREAM_PNT (s); */
1952 size
-= OSPF_LSA_HEADER_SIZE
;
1953 stream_forward (s
, OSPF_LSA_HEADER_SIZE
);
1955 if (lsa
->data
->type
< OSPF_MIN_LSA
|| lsa
->data
->type
>= OSPF_MAX_LSA
)
1958 ospf_lsa_discard (lsa
);
1962 lsr
= ospf_ls_retransmit_lookup (nbr
, lsa
);
1964 if (lsr
!= NULL
&& lsr
->data
->ls_seqnum
== lsa
->data
->ls_seqnum
)
1966 #ifdef HAVE_OPAQUE_LSA
1967 /* Keep this LSA entry for later reference. */
1968 if (IS_OPAQUE_LSA (lsr
->data
->type
))
1969 listnode_add (opaque_acks
, ospf_lsa_dup (lsr
));
1970 #endif /* HAVE_OPAQUE_LSA */
1972 ospf_ls_retransmit_delete (nbr
, lsr
);
1976 ospf_lsa_discard (lsa
);
1979 #ifdef HAVE_OPAQUE_LSA
1980 if (listcount (opaque_acks
) > 0)
1981 ospf_opaque_ls_ack_received (nbr
, opaque_acks
);
1983 list_delete (opaque_acks
);
1985 #endif /* HAVE_OPAQUE_LSA */
1989 ospf_recv_packet (int fd
, struct interface
**ifp
)
1994 struct stream
*ibuf
;
1995 unsigned int ifindex
= 0;
1997 struct cmsghdr
*cmsg
;
1998 #if defined(CMSG_SPACE)
1999 /* Header and data both require alignment. */
2000 char buff
[CMSG_SPACE(SOPT_SIZE_CMSG_IFINDEX_IPV4())];
2002 char buff
[sizeof (*cmsg
) + SOPT_SIZE_CMSG_IFINDEX_IPV4()];
2006 msgh
.msg_name
= NULL
;
2007 msgh
.msg_namelen
= 0;
2008 msgh
.msg_iov
= &iov
;
2009 msgh
.msg_iovlen
= 1;
2010 msgh
.msg_control
= (caddr_t
) buff
;
2011 msgh
.msg_controllen
= sizeof (buff
);
2014 ret
= recvfrom (fd
, (void *)&iph
, sizeof (iph
), MSG_PEEK
, NULL
, 0);
2016 if (ret
!= sizeof (iph
))
2018 zlog_warn ("ospf_recv_packet packet smaller than ip header");
2022 #if defined(__NetBSD__) || defined(__FreeBSD__) || (defined(__OpenBSD__) && (OpenBSD < 200311))
2023 ip_len
= iph
.ip_len
;
2025 ip_len
= ntohs (iph
.ip_len
);
2028 #if !defined(GNU_LINUX) && (OpenBSD < 200311)
2030 * Kernel network code touches incoming IP header parameters,
2031 * before protocol specific processing.
2033 * 1) Convert byteorder to host representation.
2034 * --> ip_len, ip_id, ip_off
2036 * 2) Adjust ip_len to strip IP header size!
2037 * --> If user process receives entire IP packet via RAW
2038 * socket, it must consider adding IP header size to
2039 * the "ip_len" field of "ip" structure.
2041 * For more details, see <netinet/ip_input.c>.
2043 ip_len
= ip_len
+ (iph
.ip_hl
<< 2);
2046 ibuf
= stream_new (ip_len
);
2047 iov
.iov_base
= STREAM_DATA (ibuf
);
2048 iov
.iov_len
= ip_len
;
2049 ret
= recvmsg (fd
, &msgh
, 0);
2051 ifindex
= getsockopt_ifindex (AF_INET
, &msgh
);
2053 *ifp
= if_lookup_by_index (ifindex
);
2057 zlog_warn ("ospf_recv_packet short read. "
2058 "ip_len %d bytes read %d", ip_len
, ret
);
2066 struct ospf_interface
*
2067 ospf_associate_packet_vl (struct ospf
*ospf
, struct interface
*ifp
,
2068 struct ip
*iph
, struct ospf_header
*ospfh
)
2070 struct ospf_interface
*rcv_oi
;
2071 struct ospf_vl_data
*vl_data
;
2072 struct ospf_area
*vl_area
;
2073 struct listnode
*node
;
2075 if (IN_MULTICAST (ntohl (iph
->ip_dst
.s_addr
)) ||
2076 !OSPF_IS_AREA_BACKBONE (ospfh
))
2079 /* look for local OSPF interface matching the destination
2080 * to determine Area ID. We presume therefore the destination address
2081 * is unique, or at least (for "unnumbered" links), not used in other
2084 if ((rcv_oi
= ospf_if_lookup_by_local_addr (ospf
, NULL
,
2085 iph
->ip_dst
)) == NULL
)
2088 for (node
= listhead (ospf
->vlinks
); node
; nextnode (node
))
2090 if ((vl_data
= getdata (node
)) == NULL
)
2093 vl_area
= ospf_area_lookup_by_area_id (ospf
, vl_data
->vl_area_id
);
2097 if (OSPF_AREA_SAME (&vl_area
, &rcv_oi
->area
) &&
2098 IPV4_ADDR_SAME (&vl_data
->vl_peer
, &ospfh
->router_id
))
2100 if (IS_DEBUG_OSPF_EVENT
)
2101 zlog_info ("associating packet with %s",
2102 IF_NAME (vl_data
->vl_oi
));
2103 if (! CHECK_FLAG (vl_data
->vl_oi
->ifp
->flags
, IFF_UP
))
2105 if (IS_DEBUG_OSPF_EVENT
)
2106 zlog_info ("This VL is not up yet, sorry");
2110 return vl_data
->vl_oi
;
2114 if (IS_DEBUG_OSPF_EVENT
)
2115 zlog_info ("couldn't find any VL to associate the packet with");
2121 ospf_check_area_id (struct ospf_interface
*oi
, struct ospf_header
*ospfh
)
2123 /* Check match the Area ID of the receiving interface. */
2124 if (OSPF_AREA_SAME (&oi
->area
, &ospfh
))
2130 /* Unbound socket will accept any Raw IP packets if proto is matched.
2131 To prevent it, compare src IP address and i/f address with masking
2132 i/f network mask. */
2134 ospf_check_network_mask (struct ospf_interface
*oi
, struct in_addr ip_src
)
2136 struct in_addr mask
, me
, him
;
2138 if (oi
->type
== OSPF_IFTYPE_POINTOPOINT
||
2139 oi
->type
== OSPF_IFTYPE_VIRTUALLINK
)
2142 masklen2ip (oi
->address
->prefixlen
, &mask
);
2144 me
.s_addr
= oi
->address
->u
.prefix4
.s_addr
& mask
.s_addr
;
2145 him
.s_addr
= ip_src
.s_addr
& mask
.s_addr
;
2147 if (IPV4_ADDR_SAME (&me
, &him
))
2154 ospf_check_auth (struct ospf_interface
*oi
, struct stream
*ibuf
,
2155 struct ospf_header
*ospfh
)
2158 struct crypt_key
*ck
;
2160 switch (ntohs (ospfh
->auth_type
))
2162 case OSPF_AUTH_NULL
:
2165 case OSPF_AUTH_SIMPLE
:
2166 if (!memcmp (OSPF_IF_PARAM (oi
, auth_simple
), ospfh
->u
.auth_data
, OSPF_AUTH_SIMPLE_SIZE
))
2171 case OSPF_AUTH_CRYPTOGRAPHIC
:
2172 if ((ck
= getdata (OSPF_IF_PARAM (oi
,auth_crypt
)->tail
)) == NULL
)
2178 /* This is very basic, the digest processing is elsewhere */
2179 if (ospfh
->u
.crypt
.auth_data_len
== OSPF_AUTH_MD5_SIZE
&&
2180 ospfh
->u
.crypt
.key_id
== ck
->key_id
&&
2181 ntohs (ospfh
->length
) + OSPF_AUTH_SIMPLE_SIZE
<= stream_get_size (ibuf
))
2195 ospf_check_sum (struct ospf_header
*ospfh
)
2199 int in_cksum (void *ptr
, int nbytes
);
2201 /* clear auth_data for checksum. */
2202 memset (ospfh
->u
.auth_data
, 0, OSPF_AUTH_SIMPLE_SIZE
);
2204 /* keep checksum and clear. */
2205 sum
= ospfh
->checksum
;
2206 memset (&ospfh
->checksum
, 0, sizeof (u_int16_t
));
2208 /* calculate checksum. */
2209 ret
= in_cksum (ospfh
, ntohs (ospfh
->length
));
2213 zlog_info ("ospf_check_sum(): checksum mismatch, my %X, his %X",
2221 /* OSPF Header verification. */
2223 ospf_verify_header (struct stream
*ibuf
, struct ospf_interface
*oi
,
2224 struct ip
*iph
, struct ospf_header
*ospfh
)
2226 /* check version. */
2227 if (ospfh
->version
!= OSPF_VERSION
)
2229 zlog_warn ("interface %s: ospf_read version number mismatch.",
2234 /* Check Area ID. */
2235 if (!ospf_check_area_id (oi
, ospfh
))
2237 zlog_warn ("interface %s: ospf_read invalid Area ID %s.",
2238 IF_NAME (oi
), inet_ntoa (ospfh
->area_id
));
2242 /* Check network mask, Silently discarded. */
2243 if (! ospf_check_network_mask (oi
, iph
->ip_src
))
2245 zlog_warn ("interface %s: ospf_read network address is not same [%s]",
2246 IF_NAME (oi
), inet_ntoa (iph
->ip_src
));
2250 /* Check authentication. */
2251 if (ospf_auth_type (oi
) != ntohs (ospfh
->auth_type
))
2253 zlog_warn ("interface %s: ospf_read authentication type mismatch.",
2258 if (! ospf_check_auth (oi
, ibuf
, ospfh
))
2260 zlog_warn ("interface %s: ospf_read authentication failed.",
2265 /* if check sum is invalid, packet is discarded. */
2266 if (ntohs (ospfh
->auth_type
) != OSPF_AUTH_CRYPTOGRAPHIC
)
2268 if (! ospf_check_sum (ospfh
))
2270 zlog_warn ("interface %s: ospf_read packet checksum error %s",
2271 IF_NAME (oi
), inet_ntoa (ospfh
->router_id
));
2277 if (ospfh
->checksum
!= 0)
2279 if (ospf_check_md5_digest (oi
, ibuf
, ntohs (ospfh
->length
)) == 0)
2281 zlog_warn ("interface %s: ospf_read md5 authentication failed.",
2290 /* Starting point of packet process function. */
2292 ospf_read (struct thread
*thread
)
2295 struct stream
*ibuf
;
2297 struct ospf_interface
*oi
;
2299 struct ospf_header
*ospfh
;
2301 struct interface
*ifp
;
2303 /* first of all get interface pointer. */
2304 ospf
= THREAD_ARG (thread
);
2305 ospf
->t_read
= NULL
;
2307 /* read OSPF packet. */
2308 ibuf
= ospf_recv_packet (ospf
->fd
, &ifp
);
2318 iph
= (struct ip
*) STREAM_DATA (ibuf
);
2320 /* prepare for next packet. */
2321 ospf
->t_read
= thread_add_read (master
, ospf_read
, ospf
, ospf
->fd
);
2323 /* IP Header dump. */
2324 if (IS_DEBUG_OSPF_PACKET(0, RECV
))
2325 ospf_ip_header_dump (ibuf
);
2327 /* Self-originated packet should be discarded silently. */
2328 if (ospf_if_lookup_by_local_addr (ospf
, NULL
, iph
->ip_src
))
2330 if (IS_DEBUG_OSPF_PACKET (0, RECV
))
2332 zlog_info ("ospf_read[%s]: Dropping self-originated packet",
2333 inet_ntoa (iph
->ip_src
));
2339 /* Adjust size to message length. */
2340 stream_forward (ibuf
, iph
->ip_hl
* 4);
2342 /* Get ospf packet header. */
2343 ospfh
= (struct ospf_header
*) STREAM_PNT (ibuf
);
2345 /* associate packet with ospf interface */
2346 oi
= ospf_if_lookup_recv_if (ospf
, iph
->ip_src
);
2348 /* if no local ospf_interface,
2349 * or header area is backbone but ospf_interface is not
2350 * check for VLINK interface
2352 if ( (oi
== NULL
) ||
2353 (OSPF_IS_AREA_ID_BACKBONE(ospfh
->area_id
)
2354 && !OSPF_IS_AREA_ID_BACKBONE(oi
->area
->area_id
))
2357 if ((oi
= ospf_associate_packet_vl (ospf
, ifp
, iph
, ospfh
)) == NULL
)
2359 zlog_warn ("Packet from [%s] received on link %s"
2360 " but no ospf_interface",
2361 inet_ntoa (iph
->ip_src
), ifp
->name
);
2367 /* else it must be a local ospf interface, check it was received on
2370 else if (oi
->ifp
!= ifp
)
2372 zlog_warn ("Packet from [%s] received on wrong link %s",
2373 inet_ntoa (iph
->ip_src
), ifp
->name
);
2379 * If the received packet is destined for AllDRouters, the packet
2380 * should be accepted only if the received ospf interface state is
2381 * either DR or Backup -- endo.
2383 if (iph
->ip_dst
.s_addr
== htonl (OSPF_ALLDROUTERS
)
2384 && (oi
->state
!= ISM_DR
&& oi
->state
!= ISM_Backup
))
2386 zlog_info ("Packet for AllDRouters from [%s] via [%s] (ISM: %s)",
2387 inet_ntoa (iph
->ip_src
), IF_NAME (oi
),
2388 LOOKUP (ospf_ism_state_msg
, oi
->state
));
2393 /* Show debug receiving packet. */
2394 if (IS_DEBUG_OSPF_PACKET (ospfh
->type
- 1, RECV
))
2396 if (IS_DEBUG_OSPF_PACKET (ospfh
->type
- 1, DETAIL
))
2398 zlog_info ("-----------------------------------------------------");
2399 ospf_packet_dump (ibuf
);
2402 zlog_info ("%s received from [%s] via [%s]",
2403 ospf_packet_type_str
[ospfh
->type
],
2404 inet_ntoa (ospfh
->router_id
), IF_NAME (oi
));
2405 zlog_info (" src [%s],", inet_ntoa (iph
->ip_src
));
2406 zlog_info (" dst [%s]", inet_ntoa (iph
->ip_dst
));
2408 if (IS_DEBUG_OSPF_PACKET (ospfh
->type
- 1, DETAIL
))
2409 zlog_info ("-----------------------------------------------------");
2412 /* Some header verification. */
2413 ret
= ospf_verify_header (ibuf
, oi
, iph
, ospfh
);
2416 if (IS_DEBUG_OSPF_PACKET (ospfh
->type
- 1, RECV
))
2418 zlog_info ("ospf_read[%s/%s]: Header check failed, "
2420 ospf_packet_type_str
[ospfh
->type
],
2421 inet_ntoa (iph
->ip_src
));
2427 stream_forward (ibuf
, OSPF_HEADER_SIZE
);
2429 /* Adjust size to message length. */
2430 length
= ntohs (ospfh
->length
) - OSPF_HEADER_SIZE
;
2432 /* Read rest of the packet and call each sort of packet routine. */
2433 switch (ospfh
->type
)
2435 case OSPF_MSG_HELLO
:
2436 ospf_hello (iph
, ospfh
, ibuf
, oi
, length
);
2438 case OSPF_MSG_DB_DESC
:
2439 ospf_db_desc (iph
, ospfh
, ibuf
, oi
, length
);
2441 case OSPF_MSG_LS_REQ
:
2442 ospf_ls_req (iph
, ospfh
, ibuf
, oi
, length
);
2444 case OSPF_MSG_LS_UPD
:
2445 ospf_ls_upd (iph
, ospfh
, ibuf
, oi
, length
);
2447 case OSPF_MSG_LS_ACK
:
2448 ospf_ls_ack (iph
, ospfh
, ibuf
, oi
, length
);
2451 zlog (NULL
, LOG_WARNING
,
2452 "interface %s: OSPF packet header type %d is illegal",
2453 IF_NAME (oi
), ospfh
->type
);
2461 /* Make OSPF header. */
2463 ospf_make_header (int type
, struct ospf_interface
*oi
, struct stream
*s
)
2465 struct ospf_header
*ospfh
;
2467 ospfh
= (struct ospf_header
*) STREAM_DATA (s
);
2469 ospfh
->version
= (u_char
) OSPF_VERSION
;
2470 ospfh
->type
= (u_char
) type
;
2472 ospfh
->router_id
= oi
->ospf
->router_id
;
2474 ospfh
->checksum
= 0;
2475 ospfh
->area_id
= oi
->area
->area_id
;
2476 ospfh
->auth_type
= htons (ospf_auth_type (oi
));
2478 memset (ospfh
->u
.auth_data
, 0, OSPF_AUTH_SIMPLE_SIZE
);
2480 ospf_output_forward (s
, OSPF_HEADER_SIZE
);
2483 /* Make Authentication Data. */
2485 ospf_make_auth (struct ospf_interface
*oi
, struct ospf_header
*ospfh
)
2487 struct crypt_key
*ck
;
2489 switch (ospf_auth_type (oi
))
2491 case OSPF_AUTH_NULL
:
2492 /* memset (ospfh->u.auth_data, 0, sizeof (ospfh->u.auth_data)); */
2494 case OSPF_AUTH_SIMPLE
:
2495 memcpy (ospfh
->u
.auth_data
, OSPF_IF_PARAM (oi
, auth_simple
),
2496 OSPF_AUTH_SIMPLE_SIZE
);
2498 case OSPF_AUTH_CRYPTOGRAPHIC
:
2499 /* If key is not set, then set 0. */
2500 if (list_isempty (OSPF_IF_PARAM (oi
, auth_crypt
)))
2502 ospfh
->u
.crypt
.zero
= 0;
2503 ospfh
->u
.crypt
.key_id
= 0;
2504 ospfh
->u
.crypt
.auth_data_len
= OSPF_AUTH_MD5_SIZE
;
2508 ck
= getdata (OSPF_IF_PARAM (oi
, auth_crypt
)->tail
);
2509 ospfh
->u
.crypt
.zero
= 0;
2510 ospfh
->u
.crypt
.key_id
= ck
->key_id
;
2511 ospfh
->u
.crypt
.auth_data_len
= OSPF_AUTH_MD5_SIZE
;
2513 /* note: the seq is done in ospf_make_md5_digest() */
2516 /* memset (ospfh->u.auth_data, 0, sizeof (ospfh->u.auth_data)); */
2523 /* Fill rest of OSPF header. */
2525 ospf_fill_header (struct ospf_interface
*oi
,
2526 struct stream
*s
, u_int16_t length
)
2528 struct ospf_header
*ospfh
;
2530 ospfh
= (struct ospf_header
*) STREAM_DATA (s
);
2533 ospfh
->length
= htons (length
);
2535 /* Calculate checksum. */
2536 if (ntohs (ospfh
->auth_type
) != OSPF_AUTH_CRYPTOGRAPHIC
)
2537 ospfh
->checksum
= in_cksum (ospfh
, length
);
2539 ospfh
->checksum
= 0;
2541 /* Add Authentication Data. */
2542 ospf_make_auth (oi
, ospfh
);
2546 ospf_make_hello (struct ospf_interface
*oi
, struct stream
*s
)
2548 struct ospf_neighbor
*nbr
;
2549 struct route_node
*rn
;
2550 u_int16_t length
= OSPF_HELLO_MIN_SIZE
;
2551 struct in_addr mask
;
2555 /* Set netmask of interface. */
2556 if (oi
->type
!= OSPF_IFTYPE_POINTOPOINT
&&
2557 oi
->type
!= OSPF_IFTYPE_VIRTUALLINK
)
2558 masklen2ip (oi
->address
->prefixlen
, &mask
);
2560 memset ((char *) &mask
, 0, sizeof (struct in_addr
));
2561 stream_put_ipv4 (s
, mask
.s_addr
);
2563 /* Set Hello Interval. */
2564 stream_putw (s
, OSPF_IF_PARAM (oi
, v_hello
));
2566 if (IS_DEBUG_OSPF_EVENT
)
2567 zlog_info ("make_hello: options: %x, int: %s",
2568 OPTIONS(oi
), IF_NAME (oi
));
2571 stream_putc (s
, OPTIONS (oi
));
2573 /* Set Router Priority. */
2574 stream_putc (s
, PRIORITY (oi
));
2576 /* Set Router Dead Interval. */
2577 stream_putl (s
, OSPF_IF_PARAM (oi
, v_wait
));
2579 /* Set Designated Router. */
2580 stream_put_ipv4 (s
, DR (oi
).s_addr
);
2584 /* Set Backup Designated Router. */
2585 stream_put_ipv4 (s
, BDR (oi
).s_addr
);
2587 /* Add neighbor seen. */
2588 for (rn
= route_top (oi
->nbrs
); rn
; rn
= route_next (rn
))
2589 if ((nbr
= rn
->info
))
2590 if (nbr
->router_id
.s_addr
!= 0) /* Ignore 0.0.0.0 node. */
2591 if (nbr
->state
!= NSM_Attempt
) /* Ignore Down neighbor. */
2592 if (nbr
->state
!= NSM_Down
) /* This is myself for DR election. */
2593 if (!IPV4_ADDR_SAME (&nbr
->router_id
, &oi
->ospf
->router_id
))
2595 /* Check neighbor is sane? */
2596 if (nbr
->d_router
.s_addr
!= 0
2597 && IPV4_ADDR_SAME (&nbr
->d_router
, &oi
->address
->u
.prefix4
)
2598 && IPV4_ADDR_SAME (&nbr
->bd_router
, &oi
->address
->u
.prefix4
))
2601 stream_put_ipv4 (s
, nbr
->router_id
.s_addr
);
2605 /* Let neighbor generate BackupSeen. */
2608 stream_set_putp (s
, p
);
2609 stream_put_ipv4 (s
, 0);
2616 ospf_make_db_desc (struct ospf_interface
*oi
, struct ospf_neighbor
*nbr
,
2619 struct ospf_lsa
*lsa
;
2620 u_int16_t length
= OSPF_DB_DESC_MIN_SIZE
;
2624 struct ospf_lsdb
*lsdb
;
2626 /* Set Interface MTU. */
2627 if (oi
->type
== OSPF_IFTYPE_VIRTUALLINK
)
2630 stream_putw (s
, oi
->ifp
->mtu
);
2633 options
= OPTIONS (oi
);
2634 #ifdef HAVE_OPAQUE_LSA
2635 if (CHECK_FLAG (oi
->ospf
->config
, OSPF_OPAQUE_CAPABLE
))
2637 if (IS_SET_DD_I (nbr
->dd_flags
)
2638 || CHECK_FLAG (nbr
->options
, OSPF_OPTION_O
))
2640 * Set O-bit in the outgoing DD packet for capablity negotiation,
2641 * if one of following case is applicable.
2643 * 1) WaitTimer expiration event triggered the neighbor state to
2644 * change to Exstart, but no (valid) DD packet has received
2645 * from the neighbor yet.
2647 * 2) At least one DD packet with O-bit on has received from the
2650 SET_FLAG (options
, OSPF_OPTION_O
);
2652 #endif /* HAVE_OPAQUE_LSA */
2653 stream_putc (s
, options
);
2655 /* Keep pointer to flags. */
2656 pp
= stream_get_putp (s
);
2657 stream_putc (s
, nbr
->dd_flags
);
2659 /* Set DD Sequence Number. */
2660 stream_putl (s
, nbr
->dd_seqnum
);
2662 if (ospf_db_summary_isempty (nbr
))
2664 if (nbr
->state
>= NSM_Exchange
)
2666 nbr
->dd_flags
&= ~OSPF_DD_FLAG_M
;
2667 /* Set DD flags again */
2668 stream_set_putp (s
, pp
);
2669 stream_putc (s
, nbr
->dd_flags
);
2674 /* Describe LSA Header from Database Summary List. */
2675 lsdb
= &nbr
->db_sum
;
2677 for (i
= OSPF_MIN_LSA
; i
< OSPF_MAX_LSA
; i
++)
2679 struct route_table
*table
= lsdb
->type
[i
].db
;
2680 struct route_node
*rn
;
2682 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
2683 if ((lsa
= rn
->info
) != NULL
)
2685 #ifdef HAVE_OPAQUE_LSA
2686 if (IS_OPAQUE_LSA (lsa
->data
->type
)
2687 && (! CHECK_FLAG (options
, OSPF_OPTION_O
)))
2689 /* Suppress advertising opaque-informations. */
2690 /* Remove LSA from DB summary list. */
2691 ospf_lsdb_delete (lsdb
, lsa
);
2694 #endif /* HAVE_OPAQUE_LSA */
2696 if (!CHECK_FLAG (lsa
->flags
, OSPF_LSA_DISCARD
))
2698 struct lsa_header
*lsah
;
2701 /* DD packet overflows interface MTU. */
2702 if (length
+ OSPF_LSA_HEADER_SIZE
> OSPF_PACKET_MAX (oi
))
2705 /* Keep pointer to LS age. */
2706 lsah
= (struct lsa_header
*) (STREAM_DATA (s
) +
2707 stream_get_putp (s
));
2709 /* Proceed stream pointer. */
2710 stream_put (s
, lsa
->data
, OSPF_LSA_HEADER_SIZE
);
2711 length
+= OSPF_LSA_HEADER_SIZE
;
2714 ls_age
= LS_AGE (lsa
);
2715 lsah
->ls_age
= htons (ls_age
);
2719 /* Remove LSA from DB summary list. */
2720 ospf_lsdb_delete (lsdb
, lsa
);
2728 ospf_make_ls_req_func (struct stream
*s
, u_int16_t
*length
,
2729 unsigned long delta
, struct ospf_neighbor
*nbr
,
2730 struct ospf_lsa
*lsa
)
2732 struct ospf_interface
*oi
;
2736 /* LS Request packet overflows interface MTU. */
2737 if (*length
+ delta
> OSPF_PACKET_MAX(oi
))
2740 stream_putl (s
, lsa
->data
->type
);
2741 stream_put_ipv4 (s
, lsa
->data
->id
.s_addr
);
2742 stream_put_ipv4 (s
, lsa
->data
->adv_router
.s_addr
);
2744 ospf_lsa_unlock (nbr
->ls_req_last
);
2745 nbr
->ls_req_last
= ospf_lsa_lock (lsa
);
2752 ospf_make_ls_req (struct ospf_neighbor
*nbr
, struct stream
*s
)
2754 struct ospf_lsa
*lsa
;
2755 u_int16_t length
= OSPF_LS_REQ_MIN_SIZE
;
2756 unsigned long delta
= stream_get_putp(s
)+12;
2757 struct route_table
*table
;
2758 struct route_node
*rn
;
2760 struct ospf_lsdb
*lsdb
;
2762 lsdb
= &nbr
->ls_req
;
2764 for (i
= OSPF_MIN_LSA
; i
< OSPF_MAX_LSA
; i
++)
2766 table
= lsdb
->type
[i
].db
;
2767 for (rn
= route_top (table
); rn
; rn
= route_next (rn
))
2768 if ((lsa
= (rn
->info
)) != NULL
)
2769 if (ospf_make_ls_req_func (s
, &length
, delta
, nbr
, lsa
) == 0)
2771 route_unlock_node (rn
);
2779 ls_age_increment (struct ospf_lsa
*lsa
, int delay
)
2783 age
= IS_LSA_MAXAGE (lsa
) ? OSPF_LSA_MAXAGE
: LS_AGE (lsa
) + delay
;
2785 return (age
> OSPF_LSA_MAXAGE
? OSPF_LSA_MAXAGE
: age
);
2789 ospf_make_ls_upd (struct ospf_interface
*oi
, struct list
*update
, struct stream
*s
)
2791 struct ospf_lsa
*lsa
;
2792 struct listnode
*node
;
2793 u_int16_t length
= OSPF_LS_UPD_MIN_SIZE
;
2794 unsigned long delta
= stream_get_putp (s
);
2798 if (IS_DEBUG_OSPF_EVENT
)
2799 zlog_info ("ospf_make_ls_upd: Start");
2801 pp
= stream_get_putp (s
);
2802 ospf_output_forward (s
, OSPF_LS_UPD_MIN_SIZE
);
2804 while ((node
= listhead (update
)) != NULL
)
2806 struct lsa_header
*lsah
;
2809 if (IS_DEBUG_OSPF_EVENT
)
2810 zlog_info ("ospf_make_ls_upd: List Iteration");
2812 lsa
= getdata (node
);
2817 if (length
+ delta
+ ntohs (lsa
->data
->length
) > stream_get_size (s
))
2820 /* Keep pointer to LS age. */
2821 lsah
= (struct lsa_header
*) (STREAM_DATA (s
) + stream_get_putp (s
));
2823 /* Put LSA to Link State Request. */
2824 stream_put (s
, lsa
->data
, ntohs (lsa
->data
->length
));
2827 /* each hop must increment an lsa_age by transmit_delay
2828 of OSPF interface */
2829 ls_age
= ls_age_increment (lsa
, OSPF_IF_PARAM (oi
, transmit_delay
));
2830 lsah
->ls_age
= htons (ls_age
);
2832 length
+= ntohs (lsa
->data
->length
);
2835 list_delete_node (update
, node
);
2836 ospf_lsa_unlock (lsa
);
2839 /* Now set #LSAs. */
2840 stream_set_putp (s
, pp
);
2841 stream_putl (s
, count
);
2843 stream_set_putp (s
, s
->endp
);
2845 if (IS_DEBUG_OSPF_EVENT
)
2846 zlog_info ("ospf_make_ls_upd: Stop");
2851 ospf_make_ls_ack (struct ospf_interface
*oi
, struct list
*ack
, struct stream
*s
)
2853 struct list
*rm_list
;
2854 struct listnode
*node
;
2855 u_int16_t length
= OSPF_LS_ACK_MIN_SIZE
;
2856 unsigned long delta
= stream_get_putp(s
) + 24;
2857 struct ospf_lsa
*lsa
;
2859 rm_list
= list_new ();
2861 for (node
= listhead (ack
); node
; nextnode (node
))
2863 lsa
= getdata (node
);
2866 if (length
+ delta
> OSPF_PACKET_MAX (oi
))
2869 stream_put (s
, lsa
->data
, OSPF_LSA_HEADER_SIZE
);
2870 length
+= OSPF_LSA_HEADER_SIZE
;
2872 listnode_add (rm_list
, lsa
);
2875 /* Remove LSA from LS-Ack list. */
2876 for (node
= listhead (rm_list
); node
; nextnode (node
))
2878 lsa
= (struct ospf_lsa
*) getdata (node
);
2880 listnode_delete (ack
, lsa
);
2881 ospf_lsa_unlock (lsa
);
2884 list_delete (rm_list
);
2890 ospf_hello_send_sub (struct ospf_interface
*oi
, struct in_addr
*addr
)
2892 struct ospf_packet
*op
;
2893 u_int16_t length
= OSPF_HEADER_SIZE
;
2895 op
= ospf_packet_new (oi
->ifp
->mtu
);
2897 /* Prepare OSPF common header. */
2898 ospf_make_header (OSPF_MSG_HELLO
, oi
, op
->s
);
2900 /* Prepare OSPF Hello body. */
2901 length
+= ospf_make_hello (oi
, op
->s
);
2903 /* Fill OSPF header. */
2904 ospf_fill_header (oi
, op
->s
, length
);
2906 /* Set packet length. */
2907 op
->length
= length
;
2909 op
->dst
.s_addr
= addr
->s_addr
;
2911 /* Add packet to the interface output queue. */
2912 ospf_packet_add (oi
, op
);
2914 /* Hook thread to write packet. */
2915 OSPF_ISM_WRITE_ON (oi
->ospf
);
2919 ospf_poll_send (struct ospf_nbr_nbma
*nbr_nbma
)
2921 struct ospf_interface
*oi
;
2926 /* If this is passive interface, do not send OSPF Hello. */
2927 if (OSPF_IF_PARAM (oi
, passive_interface
) == OSPF_IF_PASSIVE
)
2930 if (oi
->type
!= OSPF_IFTYPE_NBMA
)
2933 if (nbr_nbma
->nbr
!= NULL
&& nbr_nbma
->nbr
->state
!= NSM_Down
)
2936 if (PRIORITY(oi
) == 0)
2939 if (nbr_nbma
->priority
== 0
2940 && oi
->state
!= ISM_DR
&& oi
->state
!= ISM_Backup
)
2943 ospf_hello_send_sub (oi
, &nbr_nbma
->addr
);
2947 ospf_poll_timer (struct thread
*thread
)
2949 struct ospf_nbr_nbma
*nbr_nbma
;
2951 nbr_nbma
= THREAD_ARG (thread
);
2952 nbr_nbma
->t_poll
= NULL
;
2954 if (IS_DEBUG_OSPF (nsm
, NSM_TIMERS
))
2955 zlog (NULL
, LOG_INFO
, "NSM[%s:%s]: Timer (Poll timer expire)",
2956 IF_NAME (nbr_nbma
->oi
), inet_ntoa (nbr_nbma
->addr
));
2958 ospf_poll_send (nbr_nbma
);
2960 if (nbr_nbma
->v_poll
> 0)
2961 OSPF_POLL_TIMER_ON (nbr_nbma
->t_poll
, ospf_poll_timer
,
2969 ospf_hello_reply_timer (struct thread
*thread
)
2971 struct ospf_neighbor
*nbr
;
2973 nbr
= THREAD_ARG (thread
);
2974 nbr
->t_hello_reply
= NULL
;
2978 if (IS_DEBUG_OSPF (nsm
, NSM_TIMERS
))
2979 zlog (NULL
, LOG_INFO
, "NSM[%s:%s]: Timer (hello-reply timer expire)",
2980 IF_NAME (nbr
->oi
), inet_ntoa (nbr
->router_id
));
2982 ospf_hello_send_sub (nbr
->oi
, &nbr
->address
.u
.prefix4
);
2987 /* Send OSPF Hello. */
2989 ospf_hello_send (struct ospf_interface
*oi
)
2991 struct ospf_packet
*op
;
2992 u_int16_t length
= OSPF_HEADER_SIZE
;
2994 /* If this is passive interface, do not send OSPF Hello. */
2995 if (OSPF_IF_PARAM (oi
, passive_interface
) == OSPF_IF_PASSIVE
)
2998 op
= ospf_packet_new (oi
->ifp
->mtu
);
3000 /* Prepare OSPF common header. */
3001 ospf_make_header (OSPF_MSG_HELLO
, oi
, op
->s
);
3003 /* Prepare OSPF Hello body. */
3004 length
+= ospf_make_hello (oi
, op
->s
);
3006 /* Fill OSPF header. */
3007 ospf_fill_header (oi
, op
->s
, length
);
3009 /* Set packet length. */
3010 op
->length
= length
;
3012 if (oi
->type
== OSPF_IFTYPE_NBMA
)
3014 struct ospf_neighbor
*nbr
;
3015 struct route_node
*rn
;
3017 for (rn
= route_top (oi
->nbrs
); rn
; rn
= route_next (rn
))
3018 if ((nbr
= rn
->info
))
3019 if (nbr
!= oi
->nbr_self
)
3020 if (nbr
->state
!= NSM_Down
)
3022 /* RFC 2328 Section 9.5.1
3023 If the router is not eligible to become Designated Router,
3024 it must periodically send Hello Packets to both the
3025 Designated Router and the Backup Designated Router (if they
3027 if (PRIORITY(oi
) == 0 &&
3028 IPV4_ADDR_CMP(&DR(oi
), &nbr
->address
.u
.prefix4
) &&
3029 IPV4_ADDR_CMP(&BDR(oi
), &nbr
->address
.u
.prefix4
))
3032 /* If the router is eligible to become Designated Router, it
3033 must periodically send Hello Packets to all neighbors that
3034 are also eligible. In addition, if the router is itself the
3035 Designated Router or Backup Designated Router, it must also
3036 send periodic Hello Packets to all other neighbors. */
3038 if (nbr
->priority
== 0 && oi
->state
== ISM_DROther
)
3040 /* if oi->state == Waiting, send hello to all neighbors */
3042 struct ospf_packet
*op_dup
;
3044 op_dup
= ospf_packet_dup(op
);
3045 op_dup
->dst
= nbr
->address
.u
.prefix4
;
3047 /* Add packet to the interface output queue. */
3048 ospf_packet_add (oi
, op_dup
);
3050 OSPF_ISM_WRITE_ON (oi
->ospf
);
3054 ospf_packet_free (op
);
3058 /* Decide destination address. */
3059 if (oi
->type
== OSPF_IFTYPE_VIRTUALLINK
)
3060 op
->dst
.s_addr
= oi
->vl_data
->peer_addr
.s_addr
;
3062 op
->dst
.s_addr
= htonl (OSPF_ALLSPFROUTERS
);
3064 /* Add packet to the interface output queue. */
3065 ospf_packet_add (oi
, op
);
3067 /* Hook thread to write packet. */
3068 OSPF_ISM_WRITE_ON (oi
->ospf
);
3072 /* Send OSPF Database Description. */
3074 ospf_db_desc_send (struct ospf_neighbor
*nbr
)
3076 struct ospf_interface
*oi
;
3077 struct ospf_packet
*op
;
3078 u_int16_t length
= OSPF_HEADER_SIZE
;
3081 op
= ospf_packet_new (oi
->ifp
->mtu
);
3083 /* Prepare OSPF common header. */
3084 ospf_make_header (OSPF_MSG_DB_DESC
, oi
, op
->s
);
3086 /* Prepare OSPF Database Description body. */
3087 length
+= ospf_make_db_desc (oi
, nbr
, op
->s
);
3089 /* Fill OSPF header. */
3090 ospf_fill_header (oi
, op
->s
, length
);
3092 /* Set packet length. */
3093 op
->length
= length
;
3095 /* Decide destination address. */
3096 op
->dst
= nbr
->address
.u
.prefix4
;
3098 /* Add packet to the interface output queue. */
3099 ospf_packet_add (oi
, op
);
3101 /* Hook thread to write packet. */
3102 OSPF_ISM_WRITE_ON (oi
->ospf
);
3104 /* Remove old DD packet, then copy new one and keep in neighbor structure. */
3106 ospf_packet_free (nbr
->last_send
);
3107 nbr
->last_send
= ospf_packet_dup (op
);
3108 gettimeofday (&nbr
->last_send_ts
, NULL
);
3111 /* Re-send Database Description. */
3113 ospf_db_desc_resend (struct ospf_neighbor
*nbr
)
3115 struct ospf_interface
*oi
;
3119 /* Add packet to the interface output queue. */
3120 ospf_packet_add (oi
, ospf_packet_dup (nbr
->last_send
));
3122 /* Hook thread to write packet. */
3123 OSPF_ISM_WRITE_ON (oi
->ospf
);
3126 /* Send Link State Request. */
3128 ospf_ls_req_send (struct ospf_neighbor
*nbr
)
3130 struct ospf_interface
*oi
;
3131 struct ospf_packet
*op
;
3132 u_int16_t length
= OSPF_HEADER_SIZE
;
3135 op
= ospf_packet_new (oi
->ifp
->mtu
);
3137 /* Prepare OSPF common header. */
3138 ospf_make_header (OSPF_MSG_LS_REQ
, oi
, op
->s
);
3140 /* Prepare OSPF Link State Request body. */
3141 length
+= ospf_make_ls_req (nbr
, op
->s
);
3142 if (length
== OSPF_HEADER_SIZE
)
3144 ospf_packet_free (op
);
3148 /* Fill OSPF header. */
3149 ospf_fill_header (oi
, op
->s
, length
);
3151 /* Set packet length. */
3152 op
->length
= length
;
3154 /* Decide destination address. */
3155 op
->dst
= nbr
->address
.u
.prefix4
;
3157 /* Add packet to the interface output queue. */
3158 ospf_packet_add (oi
, op
);
3160 /* Hook thread to write packet. */
3161 OSPF_ISM_WRITE_ON (oi
->ospf
);
3163 /* Add Link State Request Retransmission Timer. */
3164 OSPF_NSM_TIMER_ON (nbr
->t_ls_req
, ospf_ls_req_timer
, nbr
->v_ls_req
);
3167 /* Send Link State Update with an LSA. */
3169 ospf_ls_upd_send_lsa (struct ospf_neighbor
*nbr
, struct ospf_lsa
*lsa
,
3172 struct list
*update
;
3174 update
= list_new ();
3176 listnode_add (update
, lsa
);
3177 ospf_ls_upd_send (nbr
, update
, flag
);
3179 list_delete (update
);
3182 /* Determine size for packet. Must be at least big enough to accomodate next
3183 * LSA on list, which may be bigger than MTU size.
3185 * Return pointer to new ospf_packet
3186 * NULL if we can not allocate, eg because LSA is bigger than imposed limit
3187 * on packet sizes (in which case offending LSA is deleted from update list)
3189 static struct ospf_packet
*
3190 ospf_ls_upd_packet_new (struct list
*update
, struct ospf_interface
*oi
)
3192 struct ospf_lsa
*lsa
;
3193 struct listnode
*ln
;
3195 static char warned
= 0;
3197 ln
= listhead (update
);
3202 if ((OSPF_LS_UPD_MIN_SIZE
+ ntohs (lsa
->data
->length
))
3203 > ospf_packet_max (oi
))
3207 zlog_warn ("ospf_ls_upd_packet_new: oversized LSA encountered!"
3208 "will need to fragment. Not optimal. Try divide up"
3209 " your network with areas. Use 'debug ospf packet send'"
3210 " to see details, or look at 'show ip ospf database ..'");
3214 if (IS_DEBUG_OSPF_PACKET (0, SEND
))
3215 zlog_warn ("ospf_ls_upd_packet_new: oversized LSA id:%s,"
3216 " %d bytes originated by %s, will be fragmented!",
3217 inet_ntoa (lsa
->data
->id
),
3218 ntohs (lsa
->data
->length
),
3219 inet_ntoa (lsa
->data
->adv_router
));
3222 * Allocate just enough to fit this LSA only, to avoid including other
3223 * LSAs in fragmented LSA Updates.
3225 size
= ntohs (lsa
->data
->length
) + (oi
->ifp
->mtu
- ospf_packet_max (oi
))
3226 + OSPF_LS_UPD_MIN_SIZE
;
3229 size
= oi
->ifp
->mtu
;
3231 if (size
> OSPF_MAX_PACKET_SIZE
)
3233 zlog_warn ("ospf_ls_upd_packet_new: oversized LSA id:%s too big,"
3234 " %d bytes, dropping it completely."
3235 " OSPF routing is broken!",
3236 inet_ntoa (lsa
->data
->id
), ntohs (lsa
->data
->length
));
3237 list_delete_node (update
, ln
);
3241 return ospf_packet_new (size
);
3245 ospf_ls_upd_queue_send (struct ospf_interface
*oi
, struct list
*update
,
3246 struct in_addr addr
)
3248 struct ospf_packet
*op
;
3249 u_int16_t length
= OSPF_HEADER_SIZE
;
3251 if (IS_DEBUG_OSPF_EVENT
)
3252 zlog_info ("listcount = %d, dst %s", listcount (update
), inet_ntoa(addr
));
3254 op
= ospf_ls_upd_packet_new (update
, oi
);
3256 /* Prepare OSPF common header. */
3257 ospf_make_header (OSPF_MSG_LS_UPD
, oi
, op
->s
);
3259 /* Prepare OSPF Link State Update body.
3260 * Includes Type-7 translation.
3262 length
+= ospf_make_ls_upd (oi
, update
, op
->s
);
3264 /* Fill OSPF header. */
3265 ospf_fill_header (oi
, op
->s
, length
);
3267 /* Set packet length. */
3268 op
->length
= length
;
3270 /* Decide destination address. */
3271 op
->dst
.s_addr
= addr
.s_addr
;
3273 /* Add packet to the interface output queue. */
3274 ospf_packet_add (oi
, op
);
3276 /* Hook thread to write packet. */
3277 OSPF_ISM_WRITE_ON (oi
->ospf
);
3281 ospf_ls_upd_send_queue_event (struct thread
*thread
)
3283 struct ospf_interface
*oi
= THREAD_ARG(thread
);
3284 struct route_node
*rn
;
3285 struct route_node
*rnext
;
3286 struct list
*update
;
3289 oi
->t_ls_upd_event
= NULL
;
3291 if (IS_DEBUG_OSPF_EVENT
)
3292 zlog_info ("ospf_ls_upd_send_queue start");
3294 for (rn
= route_top (oi
->ls_upd_queue
); rn
; rn
= rnext
)
3296 rnext
= route_next (rn
);
3298 if (rn
->info
== NULL
)
3301 update
= (struct list
*)rn
->info
;
3303 ospf_ls_upd_queue_send (oi
, update
, rn
->p
.u
.prefix4
);
3305 /* list might not be empty. */
3306 if (listcount(update
) == 0)
3308 list_delete (rn
->info
);
3310 route_unlock_node (rn
);
3318 if (IS_DEBUG_OSPF_EVENT
)
3319 zlog_info ("ospf_ls_upd_send_queue: update lists not cleared,"
3320 " %d nodes to try again, raising new event", again
);
3321 oi
->t_ls_upd_event
=
3322 thread_add_event (master
, ospf_ls_upd_send_queue_event
, oi
, 0);
3325 if (IS_DEBUG_OSPF_EVENT
)
3326 zlog_info ("ospf_ls_upd_send_queue stop");
3332 ospf_ls_upd_send (struct ospf_neighbor
*nbr
, struct list
*update
, int flag
)
3334 struct ospf_interface
*oi
;
3335 struct prefix_ipv4 p
;
3336 struct route_node
*rn
;
3342 p
.prefixlen
= IPV4_MAX_BITLEN
;
3344 /* Decide destination address. */
3345 if (oi
->type
== OSPF_IFTYPE_VIRTUALLINK
)
3346 p
.prefix
= oi
->vl_data
->peer_addr
;
3347 else if (flag
== OSPF_SEND_PACKET_DIRECT
)
3348 p
.prefix
= nbr
->address
.u
.prefix4
;
3349 else if (oi
->state
== ISM_DR
|| oi
->state
== ISM_Backup
)
3350 p
.prefix
.s_addr
= htonl (OSPF_ALLSPFROUTERS
);
3351 else if ((oi
->type
== OSPF_IFTYPE_POINTOPOINT
)
3352 && (flag
== OSPF_SEND_PACKET_INDIRECT
))
3353 p
.prefix
.s_addr
= htonl (OSPF_ALLSPFROUTERS
);
3354 else if (oi
->type
== OSPF_IFTYPE_POINTOMULTIPOINT
)
3355 p
.prefix
.s_addr
= htonl (OSPF_ALLSPFROUTERS
);
3357 p
.prefix
.s_addr
= htonl (OSPF_ALLDROUTERS
);
3359 if (oi
->type
== OSPF_IFTYPE_NBMA
)
3361 if (flag
== OSPF_SEND_PACKET_INDIRECT
)
3362 zlog_warn ("* LS-Update is directly sent on NBMA network.");
3363 if (IPV4_ADDR_SAME(&oi
->address
->u
.prefix4
, &p
.prefix
.s_addr
))
3364 zlog_warn ("* LS-Update is sent to myself.");
3367 rn
= route_node_get (oi
->ls_upd_queue
, (struct prefix
*) &p
);
3369 if (rn
->info
== NULL
)
3370 rn
->info
= list_new ();
3372 for (n
= listhead (update
); n
; nextnode (n
))
3373 listnode_add (rn
->info
, ospf_lsa_lock (getdata (n
)));
3375 if (oi
->t_ls_upd_event
== NULL
)
3376 oi
->t_ls_upd_event
=
3377 thread_add_event (master
, ospf_ls_upd_send_queue_event
, oi
, 0);
3381 ospf_ls_ack_send_list (struct ospf_interface
*oi
, struct list
*ack
,
3384 struct ospf_packet
*op
;
3385 u_int16_t length
= OSPF_HEADER_SIZE
;
3387 op
= ospf_packet_new (oi
->ifp
->mtu
);
3389 /* Prepare OSPF common header. */
3390 ospf_make_header (OSPF_MSG_LS_ACK
, oi
, op
->s
);
3392 /* Prepare OSPF Link State Acknowledgment body. */
3393 length
+= ospf_make_ls_ack (oi
, ack
, op
->s
);
3395 /* Fill OSPF header. */
3396 ospf_fill_header (oi
, op
->s
, length
);
3398 /* Set packet length. */
3399 op
->length
= length
;
3401 /* Set destination IP address. */
3404 /* Add packet to the interface output queue. */
3405 ospf_packet_add (oi
, op
);
3407 /* Hook thread to write packet. */
3408 OSPF_ISM_WRITE_ON (oi
->ospf
);
3412 ospf_ls_ack_send_event (struct thread
*thread
)
3414 struct ospf_interface
*oi
= THREAD_ARG (thread
);
3416 oi
->t_ls_ack_direct
= NULL
;
3418 while (listcount (oi
->ls_ack_direct
.ls_ack
))
3419 ospf_ls_ack_send_list (oi
, oi
->ls_ack_direct
.ls_ack
,
3420 oi
->ls_ack_direct
.dst
);
3426 ospf_ls_ack_send (struct ospf_neighbor
*nbr
, struct ospf_lsa
*lsa
)
3428 struct ospf_interface
*oi
= nbr
->oi
;
3430 if (listcount (oi
->ls_ack_direct
.ls_ack
) == 0)
3431 oi
->ls_ack_direct
.dst
= nbr
->address
.u
.prefix4
;
3433 listnode_add (oi
->ls_ack_direct
.ls_ack
, ospf_lsa_lock (lsa
));
3435 if (oi
->t_ls_ack_direct
== NULL
)
3436 oi
->t_ls_ack_direct
=
3437 thread_add_event (master
, ospf_ls_ack_send_event
, oi
, 0);
3440 /* Send Link State Acknowledgment delayed. */
3442 ospf_ls_ack_send_delayed (struct ospf_interface
*oi
)
3446 /* Decide destination address. */
3447 /* RFC2328 Section 13.5 On non-broadcast
3448 networks, delayed Link State Acknowledgment packets must be
3449 unicast separately over each adjacency (i.e., neighbor whose
3450 state is >= Exchange). */
3451 if (oi
->type
== OSPF_IFTYPE_NBMA
)
3453 struct ospf_neighbor
*nbr
;
3454 struct route_node
*rn
;
3456 for (rn
= route_top (oi
->nbrs
); rn
; rn
= route_next (rn
))
3457 if ((nbr
= rn
->info
) != NULL
)
3458 if (nbr
!= oi
->nbr_self
&& nbr
->state
>= NSM_Exchange
)
3459 while (listcount (oi
->ls_ack
))
3460 ospf_ls_ack_send_list (oi
, oi
->ls_ack
, nbr
->address
.u
.prefix4
);
3463 if (oi
->type
== OSPF_IFTYPE_VIRTUALLINK
)
3464 dst
.s_addr
= oi
->vl_data
->peer_addr
.s_addr
;
3465 else if (oi
->state
== ISM_DR
|| oi
->state
== ISM_Backup
)
3466 dst
.s_addr
= htonl (OSPF_ALLSPFROUTERS
);
3467 else if (oi
->type
== OSPF_IFTYPE_POINTOPOINT
)
3468 dst
.s_addr
= htonl (OSPF_ALLSPFROUTERS
);
3469 else if (oi
->type
== OSPF_IFTYPE_POINTOMULTIPOINT
)
3470 dst
.s_addr
= htonl (OSPF_ALLSPFROUTERS
);
3472 dst
.s_addr
= htonl (OSPF_ALLDROUTERS
);
3474 while (listcount (oi
->ls_ack
))
3475 ospf_ls_ack_send_list (oi
, oi
->ls_ack
, dst
);