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 along
18 * with this program; see the file COPYING; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
31 #include "sockunion.h"
38 #include "lib_errors.h"
40 #include "ospfd/ospfd.h"
41 #include "ospfd/ospf_network.h"
42 #include "ospfd/ospf_interface.h"
43 #include "ospfd/ospf_ism.h"
44 #include "ospfd/ospf_asbr.h"
45 #include "ospfd/ospf_lsa.h"
46 #include "ospfd/ospf_lsdb.h"
47 #include "ospfd/ospf_neighbor.h"
48 #include "ospfd/ospf_nsm.h"
49 #include "ospfd/ospf_packet.h"
50 #include "ospfd/ospf_spf.h"
51 #include "ospfd/ospf_flood.h"
52 #include "ospfd/ospf_dump.h"
53 #include "ospfd/ospf_errors.h"
56 * OSPF Fragmentation / fragmented writes
58 * ospfd can support writing fragmented packets, for cases where
59 * kernel will not fragment IP_HDRINCL and/or multicast destined
60 * packets (ie TTBOMK all kernels, BSD, SunOS, Linux). However,
61 * SunOS, probably BSD too, clobber the user supplied IP ID and IP
62 * flags fields, hence user-space fragmentation will not work.
63 * Only Linux is known to leave IP header unmolested.
64 * Further, fragmentation really should be done the kernel, which already
65 * supports it, and which avoids nasty IP ID state problems.
67 * Fragmentation of OSPF packets can be required on networks with router
68 * with many many interfaces active in one area, or on networks with links
72 #define WANT_OSPF_WRITE_FRAGMENT
75 /* Packet Type String. */
76 const struct message ospf_packet_type_str
[] = {
77 {OSPF_MSG_HELLO
, "Hello"},
78 {OSPF_MSG_DB_DESC
, "Database Description"},
79 {OSPF_MSG_LS_REQ
, "Link State Request"},
80 {OSPF_MSG_LS_UPD
, "Link State Update"},
81 {OSPF_MSG_LS_ACK
, "Link State Acknowledgment"},
84 /* Minimum (besides OSPF_HEADER_SIZE) lengths for OSPF packets of
85 particular types, offset is the "type" field of a packet. */
86 static const uint16_t ospf_packet_minlen
[] = {
89 OSPF_DB_DESC_MIN_SIZE
,
95 /* Minimum (besides OSPF_LSA_HEADER_SIZE) lengths for LSAs of particular
96 types, offset is the "LSA type" field. */
97 static const uint16_t ospf_lsa_minlen
[] = {
99 OSPF_ROUTER_LSA_MIN_SIZE
,
100 OSPF_NETWORK_LSA_MIN_SIZE
,
101 OSPF_SUMMARY_LSA_MIN_SIZE
,
102 OSPF_SUMMARY_LSA_MIN_SIZE
,
103 OSPF_AS_EXTERNAL_LSA_MIN_SIZE
,
105 OSPF_AS_EXTERNAL_LSA_MIN_SIZE
,
112 /* for ospf_check_auth() */
113 static int ospf_check_sum(struct ospf_header
*);
115 /* OSPF authentication checking function */
116 static int ospf_auth_type(struct ospf_interface
*oi
)
120 if (OSPF_IF_PARAM(oi
, auth_type
) == OSPF_AUTH_NOTSET
)
121 auth_type
= oi
->area
->auth_type
;
123 auth_type
= OSPF_IF_PARAM(oi
, auth_type
);
125 /* Handle case where MD5 key list is not configured aka Cisco */
126 if (auth_type
== OSPF_AUTH_CRYPTOGRAPHIC
127 && list_isempty(OSPF_IF_PARAM(oi
, auth_crypt
)))
128 return OSPF_AUTH_NULL
;
133 struct ospf_packet
*ospf_packet_new(size_t size
)
135 struct ospf_packet
*new;
137 new = XCALLOC(MTYPE_OSPF_PACKET
, sizeof(struct ospf_packet
));
138 new->s
= stream_new(size
);
143 void ospf_packet_free(struct ospf_packet
*op
)
148 XFREE(MTYPE_OSPF_PACKET
, op
);
151 struct ospf_fifo
*ospf_fifo_new(void)
153 struct ospf_fifo
*new;
155 new = XCALLOC(MTYPE_OSPF_FIFO
, sizeof(struct ospf_fifo
));
159 /* Add new packet to fifo. */
160 void ospf_fifo_push(struct ospf_fifo
*fifo
, struct ospf_packet
*op
)
163 fifo
->tail
->next
= op
;
172 /* Add new packet to head of fifo. */
173 static void ospf_fifo_push_head(struct ospf_fifo
*fifo
, struct ospf_packet
*op
)
175 op
->next
= fifo
->head
;
177 if (fifo
->tail
== NULL
)
185 /* Delete first packet from fifo. */
186 struct ospf_packet
*ospf_fifo_pop(struct ospf_fifo
*fifo
)
188 struct ospf_packet
*op
;
193 fifo
->head
= op
->next
;
195 if (fifo
->head
== NULL
)
204 /* Return first fifo entry. */
205 struct ospf_packet
*ospf_fifo_head(struct ospf_fifo
*fifo
)
210 /* Flush ospf packet fifo. */
211 void ospf_fifo_flush(struct ospf_fifo
*fifo
)
213 struct ospf_packet
*op
;
214 struct ospf_packet
*next
;
216 for (op
= fifo
->head
; op
; op
= next
) {
218 ospf_packet_free(op
);
220 fifo
->head
= fifo
->tail
= NULL
;
224 /* Free ospf packet fifo. */
225 void ospf_fifo_free(struct ospf_fifo
*fifo
)
227 ospf_fifo_flush(fifo
);
229 XFREE(MTYPE_OSPF_FIFO
, fifo
);
232 void ospf_packet_add(struct ospf_interface
*oi
, struct ospf_packet
*op
)
237 "ospf_packet_add(interface %s in state %d [%s], packet type %s, "
238 "destination %s) called with NULL obuf, ignoring "
239 "(please report this bug)!\n",
240 IF_NAME(oi
), oi
->state
,
241 lookup_msg(ospf_ism_state_msg
, oi
->state
, NULL
),
242 lookup_msg(ospf_packet_type_str
,
243 stream_getc_from(op
->s
, 1), NULL
),
248 /* Add packet to end of queue. */
249 ospf_fifo_push(oi
->obuf
, op
);
251 /* Debug of packet fifo*/
252 /* ospf_fifo_debug (oi->obuf); */
255 static void ospf_packet_add_top(struct ospf_interface
*oi
,
256 struct ospf_packet
*op
)
261 "ospf_packet_add(interface %s in state %d [%s], packet type %s, "
262 "destination %s) called with NULL obuf, ignoring "
263 "(please report this bug)!\n",
264 IF_NAME(oi
), oi
->state
,
265 lookup_msg(ospf_ism_state_msg
, oi
->state
, NULL
),
266 lookup_msg(ospf_packet_type_str
,
267 stream_getc_from(op
->s
, 1), NULL
),
272 /* Add packet to head of queue. */
273 ospf_fifo_push_head(oi
->obuf
, op
);
275 /* Debug of packet fifo*/
276 /* ospf_fifo_debug (oi->obuf); */
279 void ospf_packet_delete(struct ospf_interface
*oi
)
281 struct ospf_packet
*op
;
283 op
= ospf_fifo_pop(oi
->obuf
);
286 ospf_packet_free(op
);
289 struct ospf_packet
*ospf_packet_dup(struct ospf_packet
*op
)
291 struct ospf_packet
*new;
293 if (stream_get_endp(op
->s
) != op
->length
)
296 "ospf_packet_dup stream %lu ospf_packet %u size mismatch",
297 (unsigned long)STREAM_SIZE(op
->s
), op
->length
);
299 /* Reserve space for MD5 authentication that may be added later. */
300 new = ospf_packet_new(stream_get_endp(op
->s
) + OSPF_AUTH_MD5_SIZE
);
301 stream_copy(new->s
, op
->s
);
304 new->length
= op
->length
;
310 static unsigned int ospf_packet_authspace(struct ospf_interface
*oi
)
314 if (ospf_auth_type(oi
) == OSPF_AUTH_CRYPTOGRAPHIC
)
315 auth
= OSPF_AUTH_MD5_SIZE
;
320 static unsigned int ospf_packet_max(struct ospf_interface
*oi
)
324 max
= oi
->ifp
->mtu
- ospf_packet_authspace(oi
);
326 max
-= (OSPF_HEADER_SIZE
+ sizeof(struct ip
));
332 static int ospf_check_md5_digest(struct ospf_interface
*oi
,
333 struct ospf_header
*ospfh
)
336 unsigned char digest
[OSPF_AUTH_MD5_SIZE
];
337 struct crypt_key
*ck
;
338 struct ospf_neighbor
*nbr
;
339 uint16_t length
= ntohs(ospfh
->length
);
341 /* Get secret key. */
342 ck
= ospf_crypt_key_lookup(OSPF_IF_PARAM(oi
, auth_crypt
),
343 ospfh
->u
.crypt
.key_id
);
345 flog_warn(EC_OSPF_MD5
, "interface %s: ospf_check_md5 no key %d",
346 IF_NAME(oi
), ospfh
->u
.crypt
.key_id
);
350 /* check crypto seqnum. */
351 nbr
= ospf_nbr_lookup_by_routerid(oi
->nbrs
, &ospfh
->router_id
);
354 && ntohl(nbr
->crypt_seqnum
) > ntohl(ospfh
->u
.crypt
.crypt_seqnum
)) {
357 "interface %s: ospf_check_md5 bad sequence %d (expect %d)",
358 IF_NAME(oi
), ntohl(ospfh
->u
.crypt
.crypt_seqnum
),
359 ntohl(nbr
->crypt_seqnum
));
363 /* Generate a digest for the ospf packet - their digest + our digest. */
364 memset(&ctx
, 0, sizeof(ctx
));
366 MD5Update(&ctx
, ospfh
, length
);
367 MD5Update(&ctx
, ck
->auth_key
, OSPF_AUTH_MD5_SIZE
);
368 MD5Final(digest
, &ctx
);
370 /* compare the two */
371 if (memcmp((caddr_t
)ospfh
+ length
, digest
, OSPF_AUTH_MD5_SIZE
)) {
372 flog_warn(EC_OSPF_MD5
,
373 "interface %s: ospf_check_md5 checksum mismatch",
378 /* save neighbor's crypt_seqnum */
380 nbr
->crypt_seqnum
= ospfh
->u
.crypt
.crypt_seqnum
;
384 /* This function is called from ospf_write(), it will detect the
385 authentication scheme and if it is MD5, it will change the sequence
386 and update the MD5 digest. */
387 static int ospf_make_md5_digest(struct ospf_interface
*oi
,
388 struct ospf_packet
*op
)
390 struct ospf_header
*ospfh
;
391 unsigned char digest
[OSPF_AUTH_MD5_SIZE
] = {0};
395 struct crypt_key
*ck
;
396 const uint8_t *auth_key
;
398 ibuf
= STREAM_DATA(op
->s
);
399 ospfh
= (struct ospf_header
*)ibuf
;
401 if (ntohs(ospfh
->auth_type
) != OSPF_AUTH_CRYPTOGRAPHIC
)
404 /* We do this here so when we dup a packet, we don't have to
405 waste CPU rewriting other headers.
407 Note that quagga_time /deliberately/ is not used here */
408 t
= (time(NULL
) & 0xFFFFFFFF);
409 if (t
> oi
->crypt_seqnum
)
410 oi
->crypt_seqnum
= t
;
414 ospfh
->u
.crypt
.crypt_seqnum
= htonl(oi
->crypt_seqnum
);
416 /* Get MD5 Authentication key from auth_key list. */
417 if (list_isempty(OSPF_IF_PARAM(oi
, auth_crypt
)))
418 auth_key
= (const uint8_t *)digest
;
420 ck
= listgetdata(listtail(OSPF_IF_PARAM(oi
, auth_crypt
)));
421 auth_key
= ck
->auth_key
;
424 /* Generate a digest for the entire packet + our secret key. */
425 memset(&ctx
, 0, sizeof(ctx
));
427 MD5Update(&ctx
, ibuf
, ntohs(ospfh
->length
));
428 MD5Update(&ctx
, auth_key
, OSPF_AUTH_MD5_SIZE
);
429 MD5Final(digest
, &ctx
);
431 /* Append md5 digest to the end of the stream. */
432 stream_put(op
->s
, digest
, OSPF_AUTH_MD5_SIZE
);
434 /* We do *NOT* increment the OSPF header length. */
435 op
->length
= ntohs(ospfh
->length
) + OSPF_AUTH_MD5_SIZE
;
437 if (stream_get_endp(op
->s
) != op
->length
)
441 "ospf_make_md5_digest: length mismatch stream %lu ospf_packet %u",
442 (unsigned long)stream_get_endp(op
->s
), op
->length
);
444 return OSPF_AUTH_MD5_SIZE
;
448 static int ospf_ls_req_timer(struct thread
*thread
)
450 struct ospf_neighbor
*nbr
;
452 nbr
= THREAD_ARG(thread
);
453 nbr
->t_ls_req
= NULL
;
455 /* Send Link State Request. */
456 if (ospf_ls_request_count(nbr
))
457 ospf_ls_req_send(nbr
);
459 /* Set Link State Request retransmission timer. */
460 OSPF_NSM_TIMER_ON(nbr
->t_ls_req
, ospf_ls_req_timer
, nbr
->v_ls_req
);
465 void ospf_ls_req_event(struct ospf_neighbor
*nbr
)
468 thread_cancel(nbr
->t_ls_req
);
469 nbr
->t_ls_req
= NULL
;
471 nbr
->t_ls_req
= NULL
;
472 thread_add_event(master
, ospf_ls_req_timer
, nbr
, 0, &nbr
->t_ls_req
);
475 /* Cyclic timer function. Fist registered in ospf_nbr_new () in
477 int ospf_ls_upd_timer(struct thread
*thread
)
479 struct ospf_neighbor
*nbr
;
481 nbr
= THREAD_ARG(thread
);
482 nbr
->t_ls_upd
= NULL
;
484 /* Send Link State Update. */
485 if (ospf_ls_retransmit_count(nbr
) > 0) {
487 struct ospf_lsdb
*lsdb
;
489 int retransmit_interval
;
491 retransmit_interval
=
492 OSPF_IF_PARAM(nbr
->oi
, retransmit_interval
);
494 lsdb
= &nbr
->ls_rxmt
;
497 for (i
= OSPF_MIN_LSA
; i
< OSPF_MAX_LSA
; i
++) {
498 struct route_table
*table
= lsdb
->type
[i
].db
;
499 struct route_node
*rn
;
501 for (rn
= route_top(table
); rn
; rn
= route_next(rn
)) {
502 struct ospf_lsa
*lsa
;
504 if ((lsa
= rn
->info
) != NULL
) {
505 /* Don't retransmit an LSA if we
507 the last RxmtInterval seconds - this
509 neighbour a chance to acknowledge the
511 have ben just received before the
513 fired. This is a small tweak to what
515 but it will cut out out a lot of
518 if (monotime_since(&lsa
->tv_recv
, NULL
)
519 >= retransmit_interval
* 1000000LL)
520 listnode_add(update
, rn
->info
);
525 if (listcount(update
) > 0)
526 ospf_ls_upd_send(nbr
, update
, OSPF_SEND_PACKET_DIRECT
,
528 list_delete(&update
);
531 /* Set LS Update retransmission timer. */
532 OSPF_NSM_TIMER_ON(nbr
->t_ls_upd
, ospf_ls_upd_timer
, nbr
->v_ls_upd
);
537 int ospf_ls_ack_timer(struct thread
*thread
)
539 struct ospf_interface
*oi
;
541 oi
= THREAD_ARG(thread
);
544 /* Send Link State Acknowledgment. */
545 if (listcount(oi
->ls_ack
) > 0)
546 ospf_ls_ack_send_delayed(oi
);
548 /* Set LS Ack timer. */
549 OSPF_ISM_TIMER_ON(oi
->t_ls_ack
, ospf_ls_ack_timer
, oi
->v_ls_ack
);
554 #ifdef WANT_OSPF_WRITE_FRAGMENT
555 static void ospf_write_frags(int fd
, struct ospf_packet
*op
, struct ip
*iph
,
556 struct msghdr
*msg
, unsigned int maxdatasize
,
557 unsigned int mtu
, int flags
, uint8_t type
)
559 #define OSPF_WRITE_FRAG_SHIFT 3
564 assert(op
->length
== stream_get_endp(op
->s
));
565 assert(msg
->msg_iovlen
== 2);
569 * SunOS, BSD and BSD derived kernels likely will clear ip_id, as
570 * well as the IP_MF flag, making this all quite pointless.
572 * However, for a system on which IP_MF is left alone, and ip_id left
573 * alone or else which sets same ip_id for each fragment this might
576 * XXX-TODO: It would be much nicer to have the kernel's use their
577 * existing fragmentation support to do this for us. Bugs/RFEs need to
578 * be raised against the various kernels.
582 iph
->ip_off
|= IP_MF
;
584 /* ip frag offset is expressed in units of 8byte words */
585 offset
= maxdatasize
>> OSPF_WRITE_FRAG_SHIFT
;
587 iovp
= &msg
->msg_iov
[1];
589 while ((stream_get_endp(op
->s
) - stream_get_getp(op
->s
))
591 /* data length of this frag is to next offset value */
592 iovp
->iov_len
= offset
<< OSPF_WRITE_FRAG_SHIFT
;
593 iph
->ip_len
= iovp
->iov_len
+ sizeof(struct ip
);
594 assert(iph
->ip_len
<= mtu
);
596 sockopt_iphdrincl_swab_htosys(iph
);
598 ret
= sendmsg(fd
, msg
, flags
);
600 sockopt_iphdrincl_swab_systoh(iph
);
605 "*** ospf_write_frags: sendmsg failed to %s,"
606 " id %d, off %d, len %d, mtu %u failed with %s",
607 inet_ntoa(iph
->ip_dst
), iph
->ip_id
, iph
->ip_off
,
608 iph
->ip_len
, mtu
, safe_strerror(errno
));
610 if (IS_DEBUG_OSPF_PACKET(type
- 1, SEND
)) {
612 "ospf_write_frags: sent id %d, off %d, len %d to %s\n",
613 iph
->ip_id
, iph
->ip_off
, iph
->ip_len
,
614 inet_ntoa(iph
->ip_dst
));
615 if (IS_DEBUG_OSPF_PACKET(type
- 1, DETAIL
)) {
617 "-----------------IP Header Dump----------------------");
618 ospf_ip_header_dump(iph
);
620 "-----------------------------------------------------");
624 iph
->ip_off
+= offset
;
625 stream_forward_getp(op
->s
, iovp
->iov_len
);
626 iovp
->iov_base
= stream_pnt(op
->s
);
629 /* setup for final fragment */
630 iovp
->iov_len
= stream_get_endp(op
->s
) - stream_get_getp(op
->s
);
631 iph
->ip_len
= iovp
->iov_len
+ sizeof(struct ip
);
632 iph
->ip_off
&= (~IP_MF
);
634 #endif /* WANT_OSPF_WRITE_FRAGMENT */
636 static int ospf_write(struct thread
*thread
)
638 struct ospf
*ospf
= THREAD_ARG(thread
);
639 struct ospf_interface
*oi
;
640 struct ospf_interface
*last_serviced_oi
= NULL
;
641 struct ospf_packet
*op
;
642 struct sockaddr_in sa_dst
;
649 struct listnode
*node
;
650 #ifdef WANT_OSPF_WRITE_FRAGMENT
651 static uint16_t ipid
= 0;
652 uint16_t maxdatasize
;
653 #endif /* WANT_OSPF_WRITE_FRAGMENT */
654 #define OSPF_WRITE_IPHL_SHIFT 2
658 unsigned char cmsgbuf
[64] = {};
659 struct cmsghdr
*cm
= (struct cmsghdr
*)cmsgbuf
;
660 struct in_pktinfo
*pi
;
663 if (ospf
->fd
< 0 || ospf
->oi_running
== 0) {
664 if (IS_DEBUG_OSPF_EVENT
)
666 "ospf_write failed to send, fd %d, instance %u"
667 ,ospf
->fd
, ospf
->oi_running
);
671 ospf
->t_write
= NULL
;
673 node
= listhead(ospf
->oi_write_q
);
675 oi
= listgetdata(node
);
678 #ifdef WANT_OSPF_WRITE_FRAGMENT
679 /* seed ipid static with low order bits of time */
681 ipid
= (time(NULL
) & 0xffff);
682 #endif /* WANT_OSPF_WRITE_FRAGMENT */
684 while ((pkt_count
< ospf
->write_oi_count
) && oi
685 && (last_serviced_oi
!= oi
)) {
686 /* If there is only packet in the queue, the oi is removed from
687 write-q, so fix up the last interface that was serviced */
688 if (last_serviced_oi
== NULL
) {
689 last_serviced_oi
= oi
;
692 #ifdef WANT_OSPF_WRITE_FRAGMENT
693 /* convenience - max OSPF data per packet */
694 maxdatasize
= oi
->ifp
->mtu
- sizeof(struct ip
);
695 #endif /* WANT_OSPF_WRITE_FRAGMENT */
696 /* Get one packet from queue. */
697 op
= ospf_fifo_head(oi
->obuf
);
699 assert(op
->length
>= OSPF_HEADER_SIZE
);
701 if (op
->dst
.s_addr
== htonl(OSPF_ALLSPFROUTERS
)
702 || op
->dst
.s_addr
== htonl(OSPF_ALLDROUTERS
))
703 ospf_if_ipmulticast(ospf
, oi
->address
,
706 /* Rewrite the md5 signature & update the seq */
707 ospf_make_md5_digest(oi
, op
);
709 /* Retrieve OSPF packet type. */
710 stream_set_getp(op
->s
, 1);
711 type
= stream_getc(op
->s
);
713 /* reset get pointer */
714 stream_set_getp(op
->s
, 0);
716 memset(&iph
, 0, sizeof(struct ip
));
717 memset(&sa_dst
, 0, sizeof(sa_dst
));
719 sa_dst
.sin_family
= AF_INET
;
720 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
721 sa_dst
.sin_len
= sizeof(sa_dst
);
722 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
723 sa_dst
.sin_addr
= op
->dst
;
724 sa_dst
.sin_port
= htons(0);
726 /* Set DONTROUTE flag if dst is unicast. */
727 if (oi
->type
!= OSPF_IFTYPE_VIRTUALLINK
)
728 if (!IN_MULTICAST(htonl(op
->dst
.s_addr
)))
729 flags
= MSG_DONTROUTE
;
731 iph
.ip_hl
= sizeof(struct ip
) >> OSPF_WRITE_IPHL_SHIFT
;
732 /* it'd be very strange for header to not be 4byte-word aligned
734 if (sizeof(struct ip
)
735 > (unsigned int)(iph
.ip_hl
<< OSPF_WRITE_IPHL_SHIFT
))
736 iph
.ip_hl
++; /* we presume sizeof struct ip cant
739 iph
.ip_v
= IPVERSION
;
740 iph
.ip_tos
= IPTOS_PREC_INTERNETCONTROL
;
741 iph
.ip_len
= (iph
.ip_hl
<< OSPF_WRITE_IPHL_SHIFT
) + op
->length
;
743 #if defined(__DragonFly__)
745 * DragonFly's raw socket expects ip_len/ip_off in network byte
748 iph
.ip_len
= htons(iph
.ip_len
);
751 #ifdef WANT_OSPF_WRITE_FRAGMENT
752 /* XXX-MT: not thread-safe at all..
753 * XXX: this presumes this is only programme sending OSPF
755 * otherwise, no guarantee ipid will be unique
758 #endif /* WANT_OSPF_WRITE_FRAGMENT */
761 if (oi
->type
== OSPF_IFTYPE_VIRTUALLINK
)
762 iph
.ip_ttl
= OSPF_VL_IP_TTL
;
764 iph
.ip_ttl
= OSPF_IP_TTL
;
765 iph
.ip_p
= IPPROTO_OSPFIGP
;
767 iph
.ip_src
.s_addr
= oi
->address
->u
.prefix4
.s_addr
;
768 iph
.ip_dst
.s_addr
= op
->dst
.s_addr
;
770 memset(&msg
, 0, sizeof(msg
));
771 msg
.msg_name
= (caddr_t
)&sa_dst
;
772 msg
.msg_namelen
= sizeof(sa_dst
);
776 iov
[0].iov_base
= (char *)&iph
;
777 iov
[0].iov_len
= iph
.ip_hl
<< OSPF_WRITE_IPHL_SHIFT
;
778 iov
[1].iov_base
= stream_pnt(op
->s
);
779 iov
[1].iov_len
= op
->length
;
782 msg
.msg_control
= (caddr_t
)cm
;
783 cm
->cmsg_level
= SOL_IP
;
784 cm
->cmsg_type
= IP_PKTINFO
;
785 cm
->cmsg_len
= CMSG_LEN(sizeof(struct in_pktinfo
));
786 pi
= (struct in_pktinfo
*)CMSG_DATA(cm
);
787 pi
->ipi_ifindex
= oi
->ifp
->ifindex
;
789 msg
.msg_controllen
= cm
->cmsg_len
;
792 /* Sadly we can not rely on kernels to fragment packets
793 * because of either IP_HDRINCL and/or multicast
794 * destination being set.
797 #ifdef WANT_OSPF_WRITE_FRAGMENT
798 if (op
->length
> maxdatasize
)
799 ospf_write_frags(ospf
->fd
, op
, &iph
, &msg
, maxdatasize
,
800 oi
->ifp
->mtu
, flags
, type
);
801 #endif /* WANT_OSPF_WRITE_FRAGMENT */
803 /* send final fragment (could be first) */
804 sockopt_iphdrincl_swab_htosys(&iph
);
805 ret
= sendmsg(ospf
->fd
, &msg
, flags
);
806 sockopt_iphdrincl_swab_systoh(&iph
);
807 if (IS_DEBUG_OSPF_EVENT
)
810 "id %d, off %d, len %d, interface %s, mtu %u:",
811 inet_ntoa(iph
.ip_dst
), iph
.ip_id
, iph
.ip_off
,
812 iph
.ip_len
, oi
->ifp
->name
, oi
->ifp
->mtu
);
817 "*** sendmsg in ospf_write failed to %s, "
818 "id %d, off %d, len %d, interface %s, mtu %u: %s",
819 inet_ntoa(iph
.ip_dst
), iph
.ip_id
, iph
.ip_off
,
820 iph
.ip_len
, oi
->ifp
->name
, oi
->ifp
->mtu
,
821 safe_strerror(errno
));
823 /* Show debug sending packet. */
824 if (IS_DEBUG_OSPF_PACKET(type
- 1, SEND
)) {
825 if (IS_DEBUG_OSPF_PACKET(type
- 1, DETAIL
)) {
827 "-----------------------------------------------------");
828 ospf_ip_header_dump(&iph
);
829 stream_set_getp(op
->s
, 0);
830 ospf_packet_dump(op
->s
);
833 zlog_debug("%s sent to [%s] via [%s].",
834 lookup_msg(ospf_packet_type_str
, type
, NULL
),
835 inet_ntoa(op
->dst
), IF_NAME(oi
));
837 if (IS_DEBUG_OSPF_PACKET(type
- 1, DETAIL
))
839 "-----------------------------------------------------");
846 case OSPF_MSG_DB_DESC
:
849 case OSPF_MSG_LS_REQ
:
852 case OSPF_MSG_LS_UPD
:
855 case OSPF_MSG_LS_ACK
:
862 /* Now delete packet from queue. */
863 ospf_packet_delete(oi
);
865 /* Move this interface to the tail of write_q to
866 serve everyone in a round robin fashion */
867 list_delete_node(ospf
->oi_write_q
, node
);
868 if (ospf_fifo_head(oi
->obuf
) == NULL
) {
870 last_serviced_oi
= NULL
;
873 listnode_add(ospf
->oi_write_q
, oi
);
876 /* Setup to service from the head of the queue again */
877 if (!list_isempty(ospf
->oi_write_q
)) {
878 node
= listhead(ospf
->oi_write_q
);
880 oi
= listgetdata(node
);
885 /* If packets still remain in queue, call write thread. */
886 if (!list_isempty(ospf
->oi_write_q
))
887 thread_add_write(master
, ospf_write
, ospf
, ospf
->fd
,
893 /* OSPF Hello message read -- RFC2328 Section 10.5. */
894 static void ospf_hello(struct ip
*iph
, struct ospf_header
*ospfh
,
895 struct stream
*s
, struct ospf_interface
*oi
, int size
)
897 struct ospf_hello
*hello
;
898 struct ospf_neighbor
*nbr
;
902 /* increment statistics. */
905 hello
= (struct ospf_hello
*)stream_pnt(s
);
907 /* If Hello is myself, silently discard. */
908 if (IPV4_ADDR_SAME(&ospfh
->router_id
, &oi
->ospf
->router_id
)) {
909 if (IS_DEBUG_OSPF_PACKET(ospfh
->type
- 1, RECV
)) {
911 "ospf_header[%s/%s]: selforiginated, "
913 lookup_msg(ospf_packet_type_str
, ospfh
->type
,
915 inet_ntoa(iph
->ip_src
));
920 /* get neighbor prefix. */
922 p
.prefixlen
= ip_masklen(hello
->network_mask
);
923 p
.u
.prefix4
= iph
->ip_src
;
925 /* Compare network mask. */
926 /* Checking is ignored for Point-to-Point and Virtual link. */
927 if (oi
->type
!= OSPF_IFTYPE_POINTOPOINT
928 && oi
->type
!= OSPF_IFTYPE_VIRTUALLINK
)
929 if (oi
->address
->prefixlen
!= p
.prefixlen
) {
932 "Packet %s [Hello:RECV]: NetworkMask mismatch on %s (configured prefix length is %d, but hello packet indicates %d).",
933 inet_ntoa(ospfh
->router_id
), IF_NAME(oi
),
934 (int)oi
->address
->prefixlen
, (int)p
.prefixlen
);
938 /* Compare Router Dead Interval. */
939 if (OSPF_IF_PARAM(oi
, v_wait
) != ntohl(hello
->dead_interval
)) {
940 flog_warn(EC_OSPF_PACKET
,
941 "Packet %s [Hello:RECV]: RouterDeadInterval mismatch "
942 "(expected %u, but received %u).",
943 inet_ntoa(ospfh
->router_id
),
944 OSPF_IF_PARAM(oi
, v_wait
),
945 ntohl(hello
->dead_interval
));
949 /* Compare Hello Interval - ignored if fast-hellos are set. */
950 if (OSPF_IF_PARAM(oi
, fast_hello
) == 0) {
951 if (OSPF_IF_PARAM(oi
, v_hello
)
952 != ntohs(hello
->hello_interval
)) {
955 "Packet %s [Hello:RECV]: HelloInterval mismatch "
956 "(expected %u, but received %u).",
957 inet_ntoa(ospfh
->router_id
),
958 OSPF_IF_PARAM(oi
, v_hello
),
959 ntohs(hello
->hello_interval
));
964 if (IS_DEBUG_OSPF_EVENT
)
965 zlog_debug("Packet %s [Hello:RECV]: Options %s vrf %s",
966 inet_ntoa(ospfh
->router_id
),
967 ospf_options_dump(hello
->options
),
968 ospf_vrf_id_to_name(oi
->ospf
->vrf_id
));
970 /* Compare options. */
971 #define REJECT_IF_TBIT_ON 1 /* XXX */
972 #ifdef REJECT_IF_TBIT_ON
973 if (CHECK_FLAG(hello
->options
, OSPF_OPTION_MT
)) {
975 * This router does not support non-zero TOS.
976 * Drop this Hello packet not to establish neighbor
979 flog_warn(EC_OSPF_PACKET
,
980 "Packet %s [Hello:RECV]: T-bit on, drop it.",
981 inet_ntoa(ospfh
->router_id
));
984 #endif /* REJECT_IF_TBIT_ON */
986 if (CHECK_FLAG(oi
->ospf
->config
, OSPF_OPAQUE_CAPABLE
)
987 && CHECK_FLAG(hello
->options
, OSPF_OPTION_O
)) {
989 * This router does know the correct usage of O-bit
990 * the bit should be set in DD packet only.
992 flog_warn(EC_OSPF_PACKET
,
993 "Packet %s [Hello:RECV]: O-bit abuse?",
994 inet_ntoa(ospfh
->router_id
));
995 #ifdef STRICT_OBIT_USAGE_CHECK
996 return; /* Reject this packet. */
997 #else /* STRICT_OBIT_USAGE_CHECK */
998 UNSET_FLAG(hello
->options
, OSPF_OPTION_O
); /* Ignore O-bit. */
999 #endif /* STRICT_OBIT_USAGE_CHECK */
1002 /* new for NSSA is to ensure that NP is on and E is off */
1004 if (oi
->area
->external_routing
== OSPF_AREA_NSSA
) {
1005 if (!(CHECK_FLAG(OPTIONS(oi
), OSPF_OPTION_NP
)
1006 && CHECK_FLAG(hello
->options
, OSPF_OPTION_NP
)
1007 && !CHECK_FLAG(OPTIONS(oi
), OSPF_OPTION_E
)
1008 && !CHECK_FLAG(hello
->options
, OSPF_OPTION_E
))) {
1011 "NSSA-Packet-%s[Hello:RECV]: my options: %x, his options %x",
1012 inet_ntoa(ospfh
->router_id
), OPTIONS(oi
),
1016 if (IS_DEBUG_OSPF_NSSA
)
1017 zlog_debug("NSSA-Hello:RECV:Packet from %s:",
1018 inet_ntoa(ospfh
->router_id
));
1020 /* The setting of the E-bit found in the Hello Packet's Options
1021 field must match this area's ExternalRoutingCapability A
1022 mismatch causes processing to stop and the packet to be
1023 dropped. The setting of the rest of the bits in the Hello
1024 Packet's Options field should be ignored. */
1025 if (CHECK_FLAG(OPTIONS(oi
), OSPF_OPTION_E
)
1026 != CHECK_FLAG(hello
->options
, OSPF_OPTION_E
)) {
1029 "Packet %s [Hello:RECV]: my options: %x, his options %x",
1030 inet_ntoa(ospfh
->router_id
), OPTIONS(oi
),
1035 /* get neighbour struct */
1036 nbr
= ospf_nbr_get(oi
, ospfh
, iph
, &p
);
1038 /* neighbour must be valid, ospf_nbr_get creates if none existed */
1041 old_state
= nbr
->state
;
1043 /* Add event to thread. */
1044 OSPF_NSM_EVENT_SCHEDULE(nbr
, NSM_PacketReceived
);
1046 /* RFC2328 Section 9.5.1
1047 If the router is not eligible to become Designated Router,
1048 (snip) It must also send an Hello Packet in reply to an
1049 Hello Packet received from any eligible neighbor (other than
1050 the current Designated Router and Backup Designated Router). */
1051 if (oi
->type
== OSPF_IFTYPE_NBMA
)
1052 if (PRIORITY(oi
) == 0 && hello
->priority
> 0
1053 && IPV4_ADDR_CMP(&DR(oi
), &iph
->ip_src
)
1054 && IPV4_ADDR_CMP(&BDR(oi
), &iph
->ip_src
))
1055 OSPF_NSM_TIMER_ON(nbr
->t_hello_reply
,
1056 ospf_hello_reply_timer
,
1057 OSPF_HELLO_REPLY_DELAY
);
1059 /* on NBMA network type, it happens to receive bidirectional Hello
1061 without advance 1-Way Received event.
1062 To avoid incorrect DR-seletion, raise 1-Way Received event.*/
1063 if (oi
->type
== OSPF_IFTYPE_NBMA
1064 && (old_state
== NSM_Down
|| old_state
== NSM_Attempt
)) {
1065 OSPF_NSM_EVENT_SCHEDULE(nbr
, NSM_OneWayReceived
);
1066 nbr
->priority
= hello
->priority
;
1067 nbr
->d_router
= hello
->d_router
;
1068 nbr
->bd_router
= hello
->bd_router
;
1072 if (ospf_nbr_bidirectional(&oi
->ospf
->router_id
, hello
->neighbors
,
1073 size
- OSPF_HELLO_MIN_SIZE
)) {
1074 OSPF_NSM_EVENT_SCHEDULE(nbr
, NSM_TwoWayReceived
);
1075 nbr
->options
|= hello
->options
;
1077 OSPF_NSM_EVENT_SCHEDULE(nbr
, NSM_OneWayReceived
);
1078 /* Set neighbor information. */
1079 nbr
->priority
= hello
->priority
;
1080 nbr
->d_router
= hello
->d_router
;
1081 nbr
->bd_router
= hello
->bd_router
;
1085 /* If neighbor itself declares DR and no BDR exists,
1086 cause event BackupSeen */
1087 if (IPV4_ADDR_SAME(&nbr
->address
.u
.prefix4
, &hello
->d_router
))
1088 if (hello
->bd_router
.s_addr
== 0 && oi
->state
== ISM_Waiting
)
1089 OSPF_ISM_EVENT_SCHEDULE(oi
, ISM_BackupSeen
);
1091 /* neighbor itself declares BDR. */
1092 if (oi
->state
== ISM_Waiting
1093 && IPV4_ADDR_SAME(&nbr
->address
.u
.prefix4
, &hello
->bd_router
))
1094 OSPF_ISM_EVENT_SCHEDULE(oi
, ISM_BackupSeen
);
1096 /* had not previously. */
1097 if ((IPV4_ADDR_SAME(&nbr
->address
.u
.prefix4
, &hello
->d_router
)
1098 && IPV4_ADDR_CMP(&nbr
->address
.u
.prefix4
, &nbr
->d_router
))
1099 || (IPV4_ADDR_CMP(&nbr
->address
.u
.prefix4
, &hello
->d_router
)
1100 && IPV4_ADDR_SAME(&nbr
->address
.u
.prefix4
, &nbr
->d_router
)))
1101 OSPF_ISM_EVENT_SCHEDULE(oi
, ISM_NeighborChange
);
1103 /* had not previously. */
1104 if ((IPV4_ADDR_SAME(&nbr
->address
.u
.prefix4
, &hello
->bd_router
)
1105 && IPV4_ADDR_CMP(&nbr
->address
.u
.prefix4
, &nbr
->bd_router
))
1106 || (IPV4_ADDR_CMP(&nbr
->address
.u
.prefix4
, &hello
->bd_router
)
1107 && IPV4_ADDR_SAME(&nbr
->address
.u
.prefix4
, &nbr
->bd_router
)))
1108 OSPF_ISM_EVENT_SCHEDULE(oi
, ISM_NeighborChange
);
1110 /* Neighbor priority check. */
1111 if (nbr
->priority
>= 0 && nbr
->priority
!= hello
->priority
)
1112 OSPF_ISM_EVENT_SCHEDULE(oi
, ISM_NeighborChange
);
1114 /* Set neighbor information. */
1115 nbr
->priority
= hello
->priority
;
1116 nbr
->d_router
= hello
->d_router
;
1117 nbr
->bd_router
= hello
->bd_router
;
1120 /* Save DD flags/options/Seqnum received. */
1121 static void ospf_db_desc_save_current(struct ospf_neighbor
*nbr
,
1122 struct ospf_db_desc
*dd
)
1124 nbr
->last_recv
.flags
= dd
->flags
;
1125 nbr
->last_recv
.options
= dd
->options
;
1126 nbr
->last_recv
.dd_seqnum
= ntohl(dd
->dd_seqnum
);
1129 /* Process rest of DD packet. */
1130 static void ospf_db_desc_proc(struct stream
*s
, struct ospf_interface
*oi
,
1131 struct ospf_neighbor
*nbr
,
1132 struct ospf_db_desc
*dd
, uint16_t size
)
1134 struct ospf_lsa
*new, *find
;
1135 struct lsa_header
*lsah
;
1137 stream_forward_getp(s
, OSPF_DB_DESC_MIN_SIZE
);
1138 for (size
-= OSPF_DB_DESC_MIN_SIZE
; size
>= OSPF_LSA_HEADER_SIZE
;
1139 size
-= OSPF_LSA_HEADER_SIZE
) {
1140 lsah
= (struct lsa_header
*)stream_pnt(s
);
1141 stream_forward_getp(s
, OSPF_LSA_HEADER_SIZE
);
1143 /* Unknown LS type. */
1144 if (lsah
->type
< OSPF_MIN_LSA
|| lsah
->type
>= OSPF_MAX_LSA
) {
1145 flog_warn(EC_OSPF_PACKET
,
1146 "Packet [DD:RECV]: Unknown LS type %d.",
1148 OSPF_NSM_EVENT_SCHEDULE(nbr
, NSM_SeqNumberMismatch
);
1152 if (IS_OPAQUE_LSA(lsah
->type
)
1153 && !CHECK_FLAG(nbr
->options
, OSPF_OPTION_O
)) {
1154 flog_warn(EC_OSPF_PACKET
,
1155 "LSA[Type%d:%s]: Opaque capability mismatch?",
1156 lsah
->type
, inet_ntoa(lsah
->id
));
1157 OSPF_NSM_EVENT_SCHEDULE(nbr
, NSM_SeqNumberMismatch
);
1161 switch (lsah
->type
) {
1162 case OSPF_AS_EXTERNAL_LSA
:
1163 case OSPF_OPAQUE_AS_LSA
:
1164 /* Check for stub area. Reject if AS-External from stub
1166 allow if from NSSA. */
1167 if (oi
->area
->external_routing
== OSPF_AREA_STUB
) {
1170 "Packet [DD:RECV]: LSA[Type%d:%s] from %s area.",
1171 lsah
->type
, inet_ntoa(lsah
->id
),
1172 (oi
->area
->external_routing
1176 OSPF_NSM_EVENT_SCHEDULE(nbr
,
1177 NSM_SeqNumberMismatch
);
1185 /* Create LS-request object. */
1186 new = ospf_ls_request_new(lsah
);
1188 /* Lookup received LSA, then add LS request list. */
1189 find
= ospf_lsa_lookup_by_header(oi
->area
, lsah
);
1191 /* ospf_lsa_more_recent is fine with NULL pointers */
1192 switch (ospf_lsa_more_recent(find
, new)) {
1194 /* Neighbour has a more recent LSA, we must request it
1196 ospf_ls_request_add(nbr
, new);
1199 /* If we have a copy of this LSA, it's either less
1201 * and we're requesting it from neighbour (the case
1203 * it's as recent and we both have same copy (this
1206 * In neither of these two cases is there any point in
1207 * describing our copy of the LSA to the neighbour in a
1208 * DB-Summary packet, if we're still intending to do so.
1210 * See: draft-ogier-ospf-dbex-opt-00.txt, describing the
1211 * backward compatible optimisation to OSPF DB Exchange
1213 * DB Description process implemented here.
1216 ospf_lsdb_delete(&nbr
->db_sum
, find
);
1217 ospf_lsa_discard(new);
1220 /* We have the more recent copy, nothing specific to do:
1221 * - no need to request neighbours stale copy
1222 * - must leave DB summary list copy alone
1224 if (IS_DEBUG_OSPF_EVENT
)
1226 "Packet [DD:RECV]: LSA received Type %d, "
1227 "ID %s is not recent.",
1228 lsah
->type
, inet_ntoa(lsah
->id
));
1229 ospf_lsa_discard(new);
1234 if (IS_SET_DD_MS(nbr
->dd_flags
)) {
1237 /* Both sides have no More, then we're done with Exchange */
1238 if (!IS_SET_DD_M(dd
->flags
) && !IS_SET_DD_M(nbr
->dd_flags
))
1239 OSPF_NSM_EVENT_SCHEDULE(nbr
, NSM_ExchangeDone
);
1241 ospf_db_desc_send(nbr
);
1245 nbr
->dd_seqnum
= ntohl(dd
->dd_seqnum
);
1247 /* Send DD packet in reply.
1249 * Must be done to acknowledge the Master's DD, regardless of
1250 * whether we have more LSAs ourselves to describe.
1252 * This function will clear the 'More' bit, if after this DD
1253 * we have no more LSAs to describe to the master..
1255 ospf_db_desc_send(nbr
);
1257 /* Slave can raise ExchangeDone now, if master is also done */
1258 if (!IS_SET_DD_M(dd
->flags
) && !IS_SET_DD_M(nbr
->dd_flags
))
1259 OSPF_NSM_EVENT_SCHEDULE(nbr
, NSM_ExchangeDone
);
1262 /* Save received neighbor values from DD. */
1263 ospf_db_desc_save_current(nbr
, dd
);
1266 ospf_ls_req_send(nbr
);
1269 static int ospf_db_desc_is_dup(struct ospf_db_desc
*dd
,
1270 struct ospf_neighbor
*nbr
)
1272 /* Is DD duplicated? */
1273 if (dd
->options
== nbr
->last_recv
.options
1274 && dd
->flags
== nbr
->last_recv
.flags
1275 && dd
->dd_seqnum
== htonl(nbr
->last_recv
.dd_seqnum
))
1281 /* OSPF Database Description message read -- RFC2328 Section 10.6. */
1282 static void ospf_db_desc(struct ip
*iph
, struct ospf_header
*ospfh
,
1283 struct stream
*s
, struct ospf_interface
*oi
,
1286 struct ospf_db_desc
*dd
;
1287 struct ospf_neighbor
*nbr
;
1289 /* Increment statistics. */
1292 dd
= (struct ospf_db_desc
*)stream_pnt(s
);
1294 nbr
= ospf_nbr_lookup(oi
, iph
, ospfh
);
1296 flog_warn(EC_OSPF_PACKET
, "Packet[DD]: Unknown Neighbor %s",
1297 inet_ntoa(ospfh
->router_id
));
1302 if ((OSPF_IF_PARAM(oi
, mtu_ignore
) == 0)
1303 && (ntohs(dd
->mtu
) > oi
->ifp
->mtu
)) {
1306 "Packet[DD]: Neighbor %s MTU %u is larger than [%s]'s MTU %u",
1307 inet_ntoa(nbr
->router_id
), ntohs(dd
->mtu
), IF_NAME(oi
),
1313 * XXX HACK by Hasso Tepper. Setting N/P bit in NSSA area DD packets is
1315 * required. In fact at least JunOS sends DD packets with P bit clear.
1316 * Until proper solution is developped, this hack should help.
1318 * Update: According to the RFCs, N bit is specified /only/ for Hello
1319 * options, unfortunately its use in DD options is not specified. Hence
1321 * implementations follow E-bit semantics and set it in DD options, and
1323 * treat it as unspecified and hence follow the directive "default for
1324 * options is clear", ie unset.
1326 * Reset the flag, as ospfd follows E-bit semantics.
1328 if ((oi
->area
->external_routing
== OSPF_AREA_NSSA
)
1329 && (CHECK_FLAG(nbr
->options
, OSPF_OPTION_NP
))
1330 && (!CHECK_FLAG(dd
->options
, OSPF_OPTION_NP
))) {
1331 if (IS_DEBUG_OSPF_EVENT
)
1333 "Packet[DD]: Neighbour %s: Has NSSA capability, sends with N bit clear in DD options",
1334 inet_ntoa(nbr
->router_id
));
1335 SET_FLAG(dd
->options
, OSPF_OPTION_NP
);
1338 #ifdef REJECT_IF_TBIT_ON
1339 if (CHECK_FLAG(dd
->options
, OSPF_OPTION_MT
)) {
1341 * In Hello protocol, optional capability must have checked
1342 * to prevent this T-bit enabled router be my neighbor.
1344 flog_warn(EC_OSPF_PACKET
, "Packet[DD]: Neighbor %s: T-bit on?",
1345 inet_ntoa(nbr
->router_id
));
1348 #endif /* REJECT_IF_TBIT_ON */
1350 if (CHECK_FLAG(dd
->options
, OSPF_OPTION_O
)
1351 && !CHECK_FLAG(oi
->ospf
->config
, OSPF_OPAQUE_CAPABLE
)) {
1353 * This node is not configured to handle O-bit, for now.
1354 * Clear it to ignore unsupported capability proposed by
1357 UNSET_FLAG(dd
->options
, OSPF_OPTION_O
);
1360 /* Add event to thread. */
1361 OSPF_NSM_EVENT_SCHEDULE(nbr
, NSM_PacketReceived
);
1363 /* Process DD packet by neighbor status. */
1364 switch (nbr
->state
) {
1370 "Packet[DD]: Neighbor %s state is %s, packet discarded.",
1371 inet_ntoa(nbr
->router_id
),
1372 lookup_msg(ospf_nsm_state_msg
, nbr
->state
, NULL
));
1375 OSPF_NSM_EVENT_EXECUTE(nbr
, NSM_TwoWayReceived
);
1376 /* If the new state is ExStart, the processing of the current
1377 packet should then continue in this new state by falling
1378 through to case ExStart below. */
1379 if (nbr
->state
!= NSM_ExStart
)
1384 if ((IS_SET_DD_ALL(dd
->flags
) == OSPF_DD_FLAG_ALL
)
1385 && (size
== OSPF_DB_DESC_MIN_SIZE
)) {
1386 if (IPV4_ADDR_CMP(&nbr
->router_id
, &oi
->ospf
->router_id
)
1388 /* We're Slave---obey */
1389 if (CHECK_FLAG(oi
->ospf
->config
,
1390 OSPF_LOG_ADJACENCY_DETAIL
))
1392 "Packet[DD]: Neighbor %s Negotiation done (Slave).",
1393 inet_ntoa(nbr
->router_id
));
1395 nbr
->dd_seqnum
= ntohl(dd
->dd_seqnum
);
1398 UNSET_FLAG(nbr
->dd_flags
,
1399 (OSPF_DD_FLAG_MS
| OSPF_DD_FLAG_I
));
1401 /* We're Master, ignore the initial DBD from
1403 if (CHECK_FLAG(oi
->ospf
->config
,
1404 OSPF_LOG_ADJACENCY_DETAIL
))
1406 "Packet[DD]: Neighbor %s: Initial DBD from Slave, "
1408 inet_ntoa(nbr
->router_id
));
1412 /* Ack from the Slave */
1413 else if (!IS_SET_DD_MS(dd
->flags
) && !IS_SET_DD_I(dd
->flags
)
1414 && ntohl(dd
->dd_seqnum
) == nbr
->dd_seqnum
1415 && IPV4_ADDR_CMP(&nbr
->router_id
, &oi
->ospf
->router_id
)
1418 "Packet[DD]: Neighbor %s Negotiation done (Master).",
1419 inet_ntoa(nbr
->router_id
));
1420 /* Reset I, leaving MS */
1421 UNSET_FLAG(nbr
->dd_flags
, OSPF_DD_FLAG_I
);
1423 flog_warn(EC_OSPF_PACKET
,
1424 "Packet[DD]: Neighbor %s Negotiation fails.",
1425 inet_ntoa(nbr
->router_id
));
1429 /* This is where the real Options are saved */
1430 nbr
->options
= dd
->options
;
1432 if (CHECK_FLAG(oi
->ospf
->config
, OSPF_OPAQUE_CAPABLE
)) {
1433 if (IS_DEBUG_OSPF_EVENT
)
1435 "Neighbor[%s] is %sOpaque-capable.",
1436 inet_ntoa(nbr
->router_id
),
1437 CHECK_FLAG(nbr
->options
, OSPF_OPTION_O
)
1441 if (!CHECK_FLAG(nbr
->options
, OSPF_OPTION_O
)
1442 && IPV4_ADDR_SAME(&DR(oi
),
1443 &nbr
->address
.u
.prefix4
)) {
1446 "DR-neighbor[%s] is NOT opaque-capable; Opaque-LSAs cannot be reliably advertised in this network.",
1447 inet_ntoa(nbr
->router_id
));
1448 /* This situation is undesirable, but not a real
1453 OSPF_NSM_EVENT_EXECUTE(nbr
, NSM_NegotiationDone
);
1455 /* continue processing rest of packet. */
1456 ospf_db_desc_proc(s
, oi
, nbr
, dd
, size
);
1459 if (ospf_db_desc_is_dup(dd
, nbr
)) {
1460 if (IS_SET_DD_MS(nbr
->dd_flags
))
1461 /* Master: discard duplicated DD packet. */
1463 "Packet[DD] (Master): Neighbor %s packet duplicated.",
1464 inet_ntoa(nbr
->router_id
));
1466 /* Slave: cause to retransmit the last Database
1470 "Packet[DD] [Slave]: Neighbor %s packet duplicated.",
1471 inet_ntoa(nbr
->router_id
));
1472 ospf_db_desc_resend(nbr
);
1477 /* Otherwise DD packet should be checked. */
1478 /* Check Master/Slave bit mismatch */
1479 if (IS_SET_DD_MS(dd
->flags
)
1480 != IS_SET_DD_MS(nbr
->last_recv
.flags
)) {
1481 flog_warn(EC_OSPF_PACKET
,
1482 "Packet[DD]: Neighbor %s MS-bit mismatch.",
1483 inet_ntoa(nbr
->router_id
));
1484 OSPF_NSM_EVENT_SCHEDULE(nbr
, NSM_SeqNumberMismatch
);
1485 if (IS_DEBUG_OSPF_EVENT
)
1487 "Packet[DD]: dd->flags=%d, nbr->dd_flags=%d",
1488 dd
->flags
, nbr
->dd_flags
);
1492 /* Check initialize bit is set. */
1493 if (IS_SET_DD_I(dd
->flags
)) {
1494 zlog_info("Packet[DD]: Neighbor %s I-bit set.",
1495 inet_ntoa(nbr
->router_id
));
1496 OSPF_NSM_EVENT_SCHEDULE(nbr
, NSM_SeqNumberMismatch
);
1500 /* Check DD Options. */
1501 if (dd
->options
!= nbr
->options
) {
1502 #ifdef ORIGINAL_CODING
1503 /* Save the new options for debugging */
1504 nbr
->options
= dd
->options
;
1505 #endif /* ORIGINAL_CODING */
1506 flog_warn(EC_OSPF_PACKET
,
1507 "Packet[DD]: Neighbor %s options mismatch.",
1508 inet_ntoa(nbr
->router_id
));
1509 OSPF_NSM_EVENT_SCHEDULE(nbr
, NSM_SeqNumberMismatch
);
1513 /* Check DD sequence number. */
1514 if ((IS_SET_DD_MS(nbr
->dd_flags
)
1515 && ntohl(dd
->dd_seqnum
) != nbr
->dd_seqnum
)
1516 || (!IS_SET_DD_MS(nbr
->dd_flags
)
1517 && ntohl(dd
->dd_seqnum
) != nbr
->dd_seqnum
+ 1)) {
1520 "Packet[DD]: Neighbor %s sequence number mismatch.",
1521 inet_ntoa(nbr
->router_id
));
1522 OSPF_NSM_EVENT_SCHEDULE(nbr
, NSM_SeqNumberMismatch
);
1526 /* Continue processing rest of packet. */
1527 ospf_db_desc_proc(s
, oi
, nbr
, dd
, size
);
1531 if (ospf_db_desc_is_dup(dd
, nbr
)) {
1532 if (IS_SET_DD_MS(nbr
->dd_flags
)) {
1533 /* Master should discard duplicate DD packet. */
1535 "Packet[DD]: Neighbor %s duplicated, "
1536 "packet discarded.",
1537 inet_ntoa(nbr
->router_id
));
1540 if (monotime_since(&nbr
->last_send_ts
, NULL
)
1541 < nbr
->v_inactivity
* 1000000LL) {
1542 /* In states Loading and Full the slave
1544 its last Database Description packet
1546 duplicate Database Description
1548 from the master. For this reason the
1550 wait RouterDeadInterval seconds
1552 last Database Description packet.
1554 Database Description packet from the
1556 this interval will generate a
1558 neighbor event. RFC2328 Section 10.8
1560 ospf_db_desc_resend(nbr
);
1566 OSPF_NSM_EVENT_SCHEDULE(nbr
, NSM_SeqNumberMismatch
);
1569 flog_warn(EC_OSPF_PACKET
,
1570 "Packet[DD]: Neighbor %s NSM illegal status %u.",
1571 inet_ntoa(nbr
->router_id
), nbr
->state
);
1576 #define OSPF_LSA_KEY_SIZE 12 /* type(4) + id(4) + ar(4) */
1578 /* OSPF Link State Request Read -- RFC2328 Section 10.7. */
1579 static void ospf_ls_req(struct ip
*iph
, struct ospf_header
*ospfh
,
1580 struct stream
*s
, struct ospf_interface
*oi
,
1583 struct ospf_neighbor
*nbr
;
1585 struct in_addr ls_id
;
1586 struct in_addr adv_router
;
1587 struct ospf_lsa
*find
;
1588 struct list
*ls_upd
;
1589 unsigned int length
;
1591 /* Increment statistics. */
1594 nbr
= ospf_nbr_lookup(oi
, iph
, ospfh
);
1596 flog_warn(EC_OSPF_PACKET
,
1597 "Link State Request: Unknown Neighbor %s.",
1598 inet_ntoa(ospfh
->router_id
));
1602 /* Add event to thread. */
1603 OSPF_NSM_EVENT_SCHEDULE(nbr
, NSM_PacketReceived
);
1605 /* Neighbor State should be Exchange or later. */
1606 if (nbr
->state
!= NSM_Exchange
&& nbr
->state
!= NSM_Loading
1607 && nbr
->state
!= NSM_Full
) {
1610 "Link State Request received from %s: Neighbor state is %s, packet discarded.",
1611 inet_ntoa(ospfh
->router_id
),
1612 lookup_msg(ospf_nsm_state_msg
, nbr
->state
, NULL
));
1616 /* Send Link State Update for ALL requested LSAs. */
1617 ls_upd
= list_new();
1618 length
= OSPF_HEADER_SIZE
+ OSPF_LS_UPD_MIN_SIZE
;
1620 while (size
>= OSPF_LSA_KEY_SIZE
) {
1621 /* Get one slice of Link State Request. */
1622 ls_type
= stream_getl(s
);
1623 ls_id
.s_addr
= stream_get_ipv4(s
);
1624 adv_router
.s_addr
= stream_get_ipv4(s
);
1626 /* Verify LSA type. */
1627 if (ls_type
< OSPF_MIN_LSA
|| ls_type
>= OSPF_MAX_LSA
) {
1628 OSPF_NSM_EVENT_SCHEDULE(nbr
, NSM_BadLSReq
);
1629 list_delete(&ls_upd
);
1633 /* Search proper LSA in LSDB. */
1634 find
= ospf_lsa_lookup(oi
->ospf
, oi
->area
, ls_type
, ls_id
,
1637 OSPF_NSM_EVENT_SCHEDULE(nbr
, NSM_BadLSReq
);
1638 list_delete(&ls_upd
);
1642 /* Packet overflows MTU size, send immediately. */
1643 if (length
+ ntohs(find
->data
->length
) > ospf_packet_max(oi
)) {
1644 if (oi
->type
== OSPF_IFTYPE_NBMA
)
1645 ospf_ls_upd_send(nbr
, ls_upd
,
1646 OSPF_SEND_PACKET_DIRECT
, 0);
1648 ospf_ls_upd_send(nbr
, ls_upd
,
1649 OSPF_SEND_PACKET_INDIRECT
, 0);
1651 /* Only remove list contents. Keep ls_upd. */
1652 list_delete_all_node(ls_upd
);
1654 length
= OSPF_HEADER_SIZE
+ OSPF_LS_UPD_MIN_SIZE
;
1657 /* Append LSA to update list. */
1658 listnode_add(ls_upd
, find
);
1659 length
+= ntohs(find
->data
->length
);
1661 size
-= OSPF_LSA_KEY_SIZE
;
1664 /* Send rest of Link State Update. */
1665 if (listcount(ls_upd
) > 0) {
1666 if (oi
->type
== OSPF_IFTYPE_NBMA
)
1667 ospf_ls_upd_send(nbr
, ls_upd
, OSPF_SEND_PACKET_DIRECT
,
1670 ospf_ls_upd_send(nbr
, ls_upd
, OSPF_SEND_PACKET_INDIRECT
,
1673 list_delete(&ls_upd
);
1675 list_delete(&ls_upd
);
1678 /* Get the list of LSAs from Link State Update packet.
1679 And process some validation -- RFC2328 Section 13. (1)-(2). */
1680 static struct list
*ospf_ls_upd_list_lsa(struct ospf_neighbor
*nbr
,
1682 struct ospf_interface
*oi
, size_t size
)
1684 uint16_t count
, sum
;
1686 struct lsa_header
*lsah
;
1687 struct ospf_lsa
*lsa
;
1692 count
= stream_getl(s
);
1693 size
-= OSPF_LS_UPD_MIN_SIZE
; /* # LSAs */
1695 for (; size
>= OSPF_LSA_HEADER_SIZE
&& count
> 0;
1696 size
-= length
, stream_forward_getp(s
, length
), count
--) {
1697 lsah
= (struct lsa_header
*)stream_pnt(s
);
1698 length
= ntohs(lsah
->length
);
1700 if (length
> size
) {
1703 "Link State Update: LSA length exceeds packet size.");
1707 /* Validate the LSA's LS checksum. */
1708 sum
= lsah
->checksum
;
1709 if (!ospf_lsa_checksum_valid(lsah
)) {
1710 /* (bug #685) more details in a one-line message make it
1712 * to identify problem source on the one hand and to
1714 * chance to compress repeated messages in syslog on the
1718 "Link State Update: LSA checksum error %x/%x, ID=%s from: nbr %s, router ID %s, adv router %s",
1719 sum
, lsah
->checksum
, inet_ntoa(lsah
->id
),
1720 inet_ntoa(nbr
->src
), inet_ntoa(nbr
->router_id
),
1721 inet_ntoa(lsah
->adv_router
));
1725 /* Examine the LSA's LS type. */
1726 if (lsah
->type
< OSPF_MIN_LSA
|| lsah
->type
>= OSPF_MAX_LSA
) {
1727 flog_warn(EC_OSPF_PACKET
,
1728 "Link State Update: Unknown LS type %d",
1734 * What if the received LSA's age is greater than MaxAge?
1735 * Treat it as a MaxAge case -- endo.
1737 if (ntohs(lsah
->ls_age
) > OSPF_LSA_MAXAGE
)
1738 lsah
->ls_age
= htons(OSPF_LSA_MAXAGE
);
1740 if (CHECK_FLAG(nbr
->options
, OSPF_OPTION_O
)) {
1741 #ifdef STRICT_OBIT_USAGE_CHECK
1742 if ((IS_OPAQUE_LSA(lsah
->type
)
1743 && !CHECK_FLAG(lsah
->options
, OSPF_OPTION_O
))
1744 || (!IS_OPAQUE_LSA(lsah
->type
)
1745 && CHECK_FLAG(lsah
->options
, OSPF_OPTION_O
))) {
1747 * This neighbor must know the exact usage of
1749 * the bit will be set in Type-9,10,11 LSAs
1752 flog_warn(EC_OSPF_PACKET
,
1753 "LSA[Type%d:%s]: O-bit abuse?",
1754 lsah
->type
, inet_ntoa(lsah
->id
));
1757 #endif /* STRICT_OBIT_USAGE_CHECK */
1759 /* Do not take in AS External Opaque-LSAs if we are a
1761 if (lsah
->type
== OSPF_OPAQUE_AS_LSA
1762 && nbr
->oi
->area
->external_routing
1763 != OSPF_AREA_DEFAULT
) {
1764 if (IS_DEBUG_OSPF_EVENT
)
1766 "LSA[Type%d:%s]: We are a stub, don't take this LSA.",
1768 inet_ntoa(lsah
->id
));
1771 } else if (IS_OPAQUE_LSA(lsah
->type
)) {
1772 flog_warn(EC_OSPF_PACKET
,
1773 "LSA[Type%d:%s]: Opaque capability mismatch?",
1774 lsah
->type
, inet_ntoa(lsah
->id
));
1778 /* Create OSPF LSA instance. */
1779 lsa
= ospf_lsa_new_and_data(length
);
1781 lsa
->vrf_id
= oi
->ospf
->vrf_id
;
1782 /* We may wish to put some error checking if type NSSA comes in
1783 and area not in NSSA mode */
1784 switch (lsah
->type
) {
1785 case OSPF_AS_EXTERNAL_LSA
:
1786 case OSPF_OPAQUE_AS_LSA
:
1789 case OSPF_OPAQUE_LINK_LSA
:
1790 lsa
->oi
= oi
; /* Remember incoming interface for
1791 flooding control. */
1794 lsa
->area
= oi
->area
;
1798 memcpy(lsa
->data
, lsah
, length
);
1800 if (IS_DEBUG_OSPF_EVENT
)
1802 "LSA[Type%d:%s]: %p new LSA created with Link State Update",
1803 lsa
->data
->type
, inet_ntoa(lsa
->data
->id
),
1805 listnode_add(lsas
, lsa
);
1811 /* Cleanup Update list. */
1812 static void ospf_upd_list_clean(struct list
*lsas
)
1814 struct listnode
*node
, *nnode
;
1815 struct ospf_lsa
*lsa
;
1817 for (ALL_LIST_ELEMENTS(lsas
, node
, nnode
, lsa
))
1818 ospf_lsa_discard(lsa
);
1823 /* OSPF Link State Update message read -- RFC2328 Section 13. */
1824 static void ospf_ls_upd(struct ospf
*ospf
, struct ip
*iph
,
1825 struct ospf_header
*ospfh
, struct stream
*s
,
1826 struct ospf_interface
*oi
, uint16_t size
)
1828 struct ospf_neighbor
*nbr
;
1830 struct listnode
*node
, *nnode
;
1831 struct ospf_lsa
*lsa
= NULL
;
1832 /* unsigned long ls_req_found = 0; */
1834 /* Dis-assemble the stream, update each entry, re-encapsulate for
1837 /* Increment statistics. */
1840 /* Check neighbor. */
1841 nbr
= ospf_nbr_lookup(oi
, iph
, ospfh
);
1843 flog_warn(EC_OSPF_PACKET
,
1844 "Link State Update: Unknown Neighbor %s on int: %s",
1845 inet_ntoa(ospfh
->router_id
), IF_NAME(oi
));
1849 /* Add event to thread. */
1850 OSPF_NSM_EVENT_SCHEDULE(nbr
, NSM_PacketReceived
);
1852 /* Check neighbor state. */
1853 if (nbr
->state
< NSM_Exchange
) {
1854 if (IS_DEBUG_OSPF(nsm
, NSM_EVENTS
))
1856 "Link State Update: "
1857 "Neighbor[%s] state %s is less than Exchange",
1858 inet_ntoa(ospfh
->router_id
),
1859 lookup_msg(ospf_nsm_state_msg
, nbr
->state
,
1864 /* Get list of LSAs from Link State Update packet. - Also perorms Stages
1865 * 1 (validate LSA checksum) and 2 (check for LSA consistent type)
1868 lsas
= ospf_ls_upd_list_lsa(nbr
, s
, oi
, size
);
1872 #define DISCARD_LSA(L, N) \
1874 if (IS_DEBUG_OSPF_EVENT) \
1876 "ospf_lsa_discard() in ospf_ls_upd() point %d: lsa %p" \
1878 N, (void *)lsa, (int)lsa->data->type); \
1879 ospf_lsa_discard(L); \
1883 /* Process each LSA received in the one packet.
1885 * Numbers in parentheses, e.g. (1), (2), etc., and the corresponding
1886 * text below are from the steps in RFC 2328, Section 13.
1888 for (ALL_LIST_ELEMENTS(lsas
, node
, nnode
, lsa
)) {
1889 struct ospf_lsa
*ls_ret
, *current
;
1892 if (IS_DEBUG_OSPF_NSSA
) {
1893 char buf1
[INET_ADDRSTRLEN
];
1894 char buf2
[INET_ADDRSTRLEN
];
1895 char buf3
[INET_ADDRSTRLEN
];
1897 zlog_debug("LSA Type-%d from %s, ID: %s, ADV: %s",
1899 inet_ntop(AF_INET
, &ospfh
->router_id
, buf1
,
1901 inet_ntop(AF_INET
, &lsa
->data
->id
, buf2
,
1903 inet_ntop(AF_INET
, &lsa
->data
->adv_router
,
1904 buf3
, INET_ADDRSTRLEN
));
1907 listnode_delete(lsas
,
1908 lsa
); /* We don't need it in list anymore */
1910 /* (1) Validate Checksum - Done above by ospf_ls_upd_list_lsa()
1913 /* (2) LSA Type - Done above by ospf_ls_upd_list_lsa() */
1915 /* (3) Do not take in AS External LSAs if we are a stub or NSSA.
1918 /* Do not take in AS NSSA if this neighbor and we are not NSSA
1921 /* Do take in Type-7's if we are an NSSA */
1923 /* If we are also an ABR, later translate them to a Type-5
1926 /* Later, an NSSA Re-fresh can Re-fresh Type-7's and an ABR will
1927 translate them to a separate Type-5 packet. */
1929 if (lsa
->data
->type
== OSPF_AS_EXTERNAL_LSA
)
1930 /* Reject from STUB or NSSA */
1931 if (nbr
->oi
->area
->external_routing
1932 != OSPF_AREA_DEFAULT
) {
1933 if (IS_DEBUG_OSPF_NSSA
)
1935 "Incoming External LSA Discarded: We are NSSA/STUB Area");
1936 DISCARD_LSA(lsa
, 1);
1939 if (lsa
->data
->type
== OSPF_AS_NSSA_LSA
)
1940 if (nbr
->oi
->area
->external_routing
!= OSPF_AREA_NSSA
) {
1941 if (IS_DEBUG_OSPF_NSSA
)
1943 "Incoming NSSA LSA Discarded: Not NSSA Area");
1944 DISCARD_LSA(lsa
, 2);
1947 /* VU229804: Router-LSA Adv-ID must be equal to LS-ID */
1948 if (lsa
->data
->type
== OSPF_ROUTER_LSA
)
1949 if (!IPV4_ADDR_SAME(&lsa
->data
->id
,
1950 &lsa
->data
->adv_router
)) {
1951 char buf1
[INET_ADDRSTRLEN
];
1952 char buf2
[INET_ADDRSTRLEN
];
1953 char buf3
[INET_ADDRSTRLEN
];
1955 flog_err(EC_OSPF_ROUTER_LSA_MISMATCH
,
1956 "Incoming Router-LSA from %s with "
1957 "Adv-ID[%s] != LS-ID[%s]",
1958 inet_ntop(AF_INET
, &ospfh
->router_id
,
1959 buf1
, INET_ADDRSTRLEN
),
1960 inet_ntop(AF_INET
, &lsa
->data
->id
,
1961 buf2
, INET_ADDRSTRLEN
),
1963 &lsa
->data
->adv_router
, buf3
,
1966 EC_OSPF_DOMAIN_CORRUPT
,
1967 "OSPF domain compromised by attack or corruption. "
1968 "Verify correct operation of -ALL- OSPF routers.");
1969 DISCARD_LSA(lsa
, 0);
1972 /* Find the LSA in the current database. */
1974 current
= ospf_lsa_lookup_by_header(oi
->area
, lsa
->data
);
1976 /* (4) If the LSA's LS age is equal to MaxAge, and there is
1978 no instance of the LSA in the router's link state database,
1979 and none of router's neighbors are in states Exchange or
1981 then take the following actions: */
1983 if (IS_LSA_MAXAGE(lsa
) && !current
1984 && ospf_check_nbr_status(oi
->ospf
)) {
1985 /* (4a) Response Link State Acknowledgment. */
1986 ospf_ls_ack_send(nbr
, lsa
);
1988 /* (4b) Discard LSA. */
1989 if (IS_DEBUG_OSPF(lsa
, LSA
)) {
1991 "Link State Update[%s]: LS age is equal to MaxAge.",
1994 DISCARD_LSA(lsa
, 3);
1997 if (IS_OPAQUE_LSA(lsa
->data
->type
)
1998 && IPV4_ADDR_SAME(&lsa
->data
->adv_router
,
1999 &oi
->ospf
->router_id
)) {
2001 * Even if initial flushing seems to be completed, there
2003 * be a case that self-originated LSA with MaxAge still
2005 * in the routing domain.
2006 * Just send an LSAck message to cease retransmission.
2008 if (IS_LSA_MAXAGE(lsa
)) {
2009 zlog_info("LSA[%s]: Boomerang effect?",
2011 ospf_ls_ack_send(nbr
, lsa
);
2012 ospf_lsa_discard(lsa
);
2014 if (current
!= NULL
&& !IS_LSA_MAXAGE(current
))
2015 ospf_opaque_lsa_refresh_schedule(
2021 * If an instance of self-originated Opaque-LSA is not
2023 * in the LSDB, there are some possible cases here.
2025 * 1) This node lost opaque-capability after restart.
2026 * 2) Else, a part of opaque-type is no more supported.
2027 * 3) Else, a part of opaque-id is no more supported.
2029 * Anyway, it is still this node's responsibility to
2031 * Otherwise, the LSA instance remains in the routing
2033 * until its age reaches to MaxAge.
2035 /* XXX: We should deal with this for *ALL* LSAs, not
2037 if (current
== NULL
) {
2038 if (IS_DEBUG_OSPF_EVENT
)
2040 "LSA[%s]: Previously originated Opaque-LSA,"
2041 "not found in the LSDB.",
2044 SET_FLAG(lsa
->flags
, OSPF_LSA_SELF
);
2046 ospf_opaque_self_originated_lsa_received(nbr
,
2048 ospf_ls_ack_send(nbr
, lsa
);
2054 /* It might be happen that received LSA is self-originated
2056 * router ID is changed. So, we should check if LSA is a
2058 * Link State ID is one of the router's own IP interface
2059 * addresses but whose
2060 * Advertising Router is not equal to the router's own Router ID
2061 * According to RFC 2328 12.4.2 and 13.4 this LSA should be
2065 if (lsa
->data
->type
== OSPF_NETWORK_LSA
) {
2066 struct listnode
*oinode
, *oinnode
;
2067 struct ospf_interface
*out_if
;
2070 for (ALL_LIST_ELEMENTS(oi
->ospf
->oiflist
, oinode
,
2075 if ((IPV4_ADDR_SAME(&out_if
->address
->u
.prefix4
,
2077 && (!(IPV4_ADDR_SAME(
2078 &oi
->ospf
->router_id
,
2079 &lsa
->data
->adv_router
)))) {
2080 if (out_if
->network_lsa_self
) {
2081 ospf_lsa_flush_area(
2083 if (IS_DEBUG_OSPF_EVENT
)
2085 "ospf_lsa_discard() in ospf_ls_upd() point 9: lsa %p Type-%d",
2089 ospf_lsa_discard(lsa
);
2099 /* (5) Find the instance of this LSA that is currently contained
2100 in the router's link state database. If there is no
2101 database copy, or the received LSA is more recent than
2102 the database copy the following steps must be performed.
2103 (The sub steps from RFC 2328 section 13 step (5) will be
2108 || (ret
= ospf_lsa_more_recent(current
, lsa
)) < 0) {
2110 if (current
&& (lsa
->data
->ls_seqnum
==
2111 htonl(OSPF_MAX_SEQUENCE_NUMBER
)
2112 && !IS_LSA_MAXAGE(lsa
))) {
2114 "Link State Update[%s]: has Max Seq but not MaxAge. Dropping it",
2117 DISCARD_LSA(lsa
, 4);
2120 /* Actual flooding procedure. */
2121 if (ospf_flood(oi
->ospf
, nbr
, current
, lsa
)
2122 < 0) /* Trap NSSA later. */
2123 DISCARD_LSA(lsa
, 5);
2127 /* (6) Else, If there is an instance of the LSA on the sending
2128 neighbor's Link state request list, an error has occurred in
2129 the Database Exchange process. In this case, restart the
2130 Database Exchange process by generating the neighbor event
2131 BadLSReq for the sending neighbor and stop processing the
2132 Link State Update packet. */
2134 if (ospf_ls_request_lookup(nbr
, lsa
)) {
2135 OSPF_NSM_EVENT_SCHEDULE(nbr
, NSM_BadLSReq
);
2138 "LSA[%s] instance exists on Link state request list",
2141 /* Clean list of LSAs. */
2142 ospf_upd_list_clean(lsas
);
2143 /* this lsa is not on lsas list already. */
2144 ospf_lsa_discard(lsa
);
2148 /* If the received LSA is the same instance as the database copy
2149 (i.e., neither one is more recent) the following two steps
2150 should be performed: */
2153 /* If the LSA is listed in the Link state retransmission
2155 for the receiving adjacency, the router itself is
2157 an acknowledgment for this LSA. The router should
2159 received LSA as an acknowledgment by removing the LSA
2161 the Link state retransmission list. This is termed
2163 "implied acknowledgment". */
2165 ls_ret
= ospf_ls_retransmit_lookup(nbr
, lsa
);
2167 if (ls_ret
!= NULL
) {
2168 ospf_ls_retransmit_delete(nbr
, ls_ret
);
2170 /* Delayed acknowledgment sent if advertisement
2172 from Designated Router, otherwise do nothing.
2174 if (oi
->state
== ISM_Backup
)
2178 ospf_lsa_lock(lsa
));
2180 DISCARD_LSA(lsa
, 6);
2182 /* Acknowledge the receipt of the LSA by sending a
2183 Link State Acknowledgment packet back out the
2187 ospf_ls_ack_send(nbr
, lsa
);
2188 DISCARD_LSA(lsa
, 7);
2192 /* The database copy is more recent. If the database copy
2193 has LS age equal to MaxAge and LS sequence number equal to
2194 MaxSequenceNumber, simply discard the received LSA without
2195 acknowledging it. (In this case, the LSA's LS sequence number
2197 wrapping, and the MaxSequenceNumber LSA must be completely
2198 flushed before any new LSA instance can be introduced). */
2200 else if (ret
> 0) /* Database copy is more recent */
2202 if (IS_LSA_MAXAGE(current
)
2203 && current
->data
->ls_seqnum
2204 == htonl(OSPF_MAX_SEQUENCE_NUMBER
)) {
2205 DISCARD_LSA(lsa
, 8);
2207 /* Otherwise, as long as the database copy has not been
2209 Link State Update within the last MinLSArrival
2211 database copy back to the sending neighbor,
2213 a Link State Update Packet. The Link State Update
2215 be sent directly to the neighbor. In so doing, do not
2217 database copy of the LSA on the neighbor's link state
2218 retransmission list, and do not acknowledge the
2220 recent) LSA instance. */
2222 if (monotime_since(¤t
->tv_orig
, NULL
)
2223 >= ospf
->min_ls_arrival
* 1000LL)
2224 /* Trap NSSA type later.*/
2225 ospf_ls_upd_send_lsa(
2227 OSPF_SEND_PACKET_DIRECT
);
2228 DISCARD_LSA(lsa
, 9);
2234 assert(listcount(lsas
) == 0);
2238 /* OSPF Link State Acknowledgment message read -- RFC2328 Section 13.7. */
2239 static void ospf_ls_ack(struct ip
*iph
, struct ospf_header
*ospfh
,
2240 struct stream
*s
, struct ospf_interface
*oi
,
2243 struct ospf_neighbor
*nbr
;
2245 /* increment statistics. */
2248 nbr
= ospf_nbr_lookup(oi
, iph
, ospfh
);
2250 flog_warn(EC_OSPF_PACKET
,
2251 "Link State Acknowledgment: Unknown Neighbor %s.",
2252 inet_ntoa(ospfh
->router_id
));
2256 /* Add event to thread. */
2257 OSPF_NSM_EVENT_SCHEDULE(nbr
, NSM_PacketReceived
);
2259 if (nbr
->state
< NSM_Exchange
) {
2260 if (IS_DEBUG_OSPF(nsm
, NSM_EVENTS
))
2262 "Link State Acknowledgment: "
2263 "Neighbor[%s] state %s is less than Exchange",
2264 inet_ntoa(ospfh
->router_id
),
2265 lookup_msg(ospf_nsm_state_msg
, nbr
->state
,
2270 while (size
>= OSPF_LSA_HEADER_SIZE
) {
2271 struct ospf_lsa
*lsa
, *lsr
;
2273 lsa
= ospf_lsa_new();
2274 lsa
->data
= (struct lsa_header
*)stream_pnt(s
);
2275 lsa
->vrf_id
= oi
->ospf
->vrf_id
;
2277 /* lsah = (struct lsa_header *) stream_pnt (s); */
2278 size
-= OSPF_LSA_HEADER_SIZE
;
2279 stream_forward_getp(s
, OSPF_LSA_HEADER_SIZE
);
2281 if (lsa
->data
->type
< OSPF_MIN_LSA
2282 || lsa
->data
->type
>= OSPF_MAX_LSA
) {
2284 ospf_lsa_discard(lsa
);
2288 lsr
= ospf_ls_retransmit_lookup(nbr
, lsa
);
2290 if (lsr
!= NULL
&& ospf_lsa_more_recent(lsr
, lsa
) == 0)
2291 ospf_ls_retransmit_delete(nbr
, lsr
);
2294 ospf_lsa_discard(lsa
);
2300 static struct stream
*ospf_recv_packet(struct ospf
*ospf
, int fd
,
2301 struct interface
**ifp
,
2302 struct stream
*ibuf
)
2307 ifindex_t ifindex
= 0;
2309 /* Header and data both require alignment. */
2310 char buff
[CMSG_SPACE(SOPT_SIZE_CMSG_IFINDEX_IPV4())];
2313 memset(&msgh
, 0, sizeof(struct msghdr
));
2314 msgh
.msg_iov
= &iov
;
2315 msgh
.msg_iovlen
= 1;
2316 msgh
.msg_control
= (caddr_t
)buff
;
2317 msgh
.msg_controllen
= sizeof(buff
);
2319 ret
= stream_recvmsg(ibuf
, fd
, &msgh
, 0, OSPF_MAX_PACKET_SIZE
+ 1);
2321 flog_warn(EC_OSPF_PACKET
, "stream_recvmsg failed: %s",
2322 safe_strerror(errno
));
2325 if ((unsigned int)ret
< sizeof(iph
)) /* ret must be > 0 now */
2329 "ospf_recv_packet: discarding runt packet of length %d "
2330 "(ip header size is %u)",
2331 ret
, (unsigned int)sizeof(iph
));
2335 /* Note that there should not be alignment problems with this assignment
2336 because this is at the beginning of the stream data buffer. */
2337 iph
= (struct ip
*)STREAM_DATA(ibuf
);
2338 sockopt_iphdrincl_swab_systoh(iph
);
2340 ip_len
= iph
->ip_len
;
2342 #if !defined(GNU_LINUX) && (OpenBSD < 200311) && (__FreeBSD_version < 1000000)
2344 * Kernel network code touches incoming IP header parameters,
2345 * before protocol specific processing.
2347 * 1) Convert byteorder to host representation.
2348 * --> ip_len, ip_id, ip_off
2350 * 2) Adjust ip_len to strip IP header size!
2351 * --> If user process receives entire IP packet via RAW
2352 * socket, it must consider adding IP header size to
2353 * the "ip_len" field of "ip" structure.
2355 * For more details, see <netinet/ip_input.c>.
2357 ip_len
= ip_len
+ (iph
->ip_hl
<< 2);
2360 #if defined(__DragonFly__)
2362 * in DragonFly's raw socket, ip_len/ip_off are read
2363 * in network byte order.
2364 * As OpenBSD < 200311 adjust ip_len to strip IP header size!
2366 ip_len
= ntohs(iph
->ip_len
) + (iph
->ip_hl
<< 2);
2369 ifindex
= getsockopt_ifindex(AF_INET
, &msgh
);
2371 *ifp
= if_lookup_by_index(ifindex
, ospf
->vrf_id
);
2373 if (ret
!= ip_len
) {
2376 "ospf_recv_packet read length mismatch: ip_len is %d, "
2377 "but recvmsg returned %d",
2385 static struct ospf_interface
*
2386 ospf_associate_packet_vl(struct ospf
*ospf
, struct interface
*ifp
,
2387 struct ip
*iph
, struct ospf_header
*ospfh
)
2389 struct ospf_interface
*rcv_oi
;
2390 struct ospf_vl_data
*vl_data
;
2391 struct ospf_area
*vl_area
;
2392 struct listnode
*node
;
2394 if (IN_MULTICAST(ntohl(iph
->ip_dst
.s_addr
))
2395 || !OSPF_IS_AREA_BACKBONE(ospfh
))
2398 /* look for local OSPF interface matching the destination
2399 * to determine Area ID. We presume therefore the destination address
2400 * is unique, or at least (for "unnumbered" links), not used in other
2403 if ((rcv_oi
= ospf_if_lookup_by_local_addr(ospf
, NULL
, iph
->ip_dst
))
2407 for (ALL_LIST_ELEMENTS_RO(ospf
->vlinks
, node
, vl_data
)) {
2409 ospf_area_lookup_by_area_id(ospf
, vl_data
->vl_area_id
);
2413 if (OSPF_AREA_SAME(&vl_area
, &rcv_oi
->area
)
2414 && IPV4_ADDR_SAME(&vl_data
->vl_peer
, &ospfh
->router_id
)) {
2415 if (IS_DEBUG_OSPF_EVENT
)
2416 zlog_debug("associating packet with %s",
2417 IF_NAME(vl_data
->vl_oi
));
2418 if (!CHECK_FLAG(vl_data
->vl_oi
->ifp
->flags
, IFF_UP
)) {
2419 if (IS_DEBUG_OSPF_EVENT
)
2421 "This VL is not up yet, sorry");
2425 return vl_data
->vl_oi
;
2429 if (IS_DEBUG_OSPF_EVENT
)
2430 zlog_debug("couldn't find any VL to associate the packet with");
2435 static int ospf_check_area_id(struct ospf_interface
*oi
,
2436 struct ospf_header
*ospfh
)
2438 /* Check match the Area ID of the receiving interface. */
2439 if (OSPF_AREA_SAME(&oi
->area
, &ospfh
))
2445 /* Unbound socket will accept any Raw IP packets if proto is matched.
2446 To prevent it, compare src IP address and i/f address with masking
2447 i/f network mask. */
2448 static int ospf_check_network_mask(struct ospf_interface
*oi
,
2449 struct in_addr ip_src
)
2451 struct in_addr mask
, me
, him
;
2453 if (oi
->type
== OSPF_IFTYPE_POINTOPOINT
2454 || oi
->type
== OSPF_IFTYPE_VIRTUALLINK
)
2457 masklen2ip(oi
->address
->prefixlen
, &mask
);
2459 me
.s_addr
= oi
->address
->u
.prefix4
.s_addr
& mask
.s_addr
;
2460 him
.s_addr
= ip_src
.s_addr
& mask
.s_addr
;
2462 if (IPV4_ADDR_SAME(&me
, &him
))
2468 /* Return 1, if the packet is properly authenticated and checksummed,
2469 0 otherwise. In particular, check that AuType header field is valid and
2470 matches the locally configured AuType, and that D.5 requirements are met. */
2471 static int ospf_check_auth(struct ospf_interface
*oi
, struct ospf_header
*ospfh
)
2473 struct crypt_key
*ck
;
2474 uint16_t iface_auth_type
;
2475 uint16_t pkt_auth_type
= ntohs(ospfh
->auth_type
);
2477 switch (pkt_auth_type
) {
2478 case OSPF_AUTH_NULL
: /* RFC2328 D.5.1 */
2479 if (OSPF_AUTH_NULL
!= (iface_auth_type
= ospf_auth_type(oi
))) {
2480 if (IS_DEBUG_OSPF_PACKET(ospfh
->type
- 1, RECV
))
2483 "interface %s: auth-type mismatch, local %s, rcvd Null",
2485 lookup_msg(ospf_auth_type_str
,
2486 iface_auth_type
, NULL
));
2489 if (!ospf_check_sum(ospfh
)) {
2490 if (IS_DEBUG_OSPF_PACKET(ospfh
->type
- 1, RECV
))
2493 "interface %s: Null auth OK, but checksum error, Router-ID %s",
2495 inet_ntoa(ospfh
->router_id
));
2499 case OSPF_AUTH_SIMPLE
: /* RFC2328 D.5.2 */
2500 if (OSPF_AUTH_SIMPLE
2501 != (iface_auth_type
= ospf_auth_type(oi
))) {
2502 if (IS_DEBUG_OSPF_PACKET(ospfh
->type
- 1, RECV
))
2505 "interface %s: auth-type mismatch, local %s, rcvd Simple",
2507 lookup_msg(ospf_auth_type_str
,
2508 iface_auth_type
, NULL
));
2511 if (memcmp(OSPF_IF_PARAM(oi
, auth_simple
), ospfh
->u
.auth_data
,
2512 OSPF_AUTH_SIMPLE_SIZE
)) {
2513 if (IS_DEBUG_OSPF_PACKET(ospfh
->type
- 1, RECV
))
2514 flog_warn(EC_OSPF_PACKET
,
2515 "interface %s: Simple auth failed",
2519 if (!ospf_check_sum(ospfh
)) {
2520 if (IS_DEBUG_OSPF_PACKET(ospfh
->type
- 1, RECV
))
2523 "interface %s: Simple auth OK, checksum error, Router-ID %s",
2525 inet_ntoa(ospfh
->router_id
));
2529 case OSPF_AUTH_CRYPTOGRAPHIC
: /* RFC2328 D.5.3 */
2530 if (OSPF_AUTH_CRYPTOGRAPHIC
2531 != (iface_auth_type
= ospf_auth_type(oi
))) {
2532 if (IS_DEBUG_OSPF_PACKET(ospfh
->type
- 1, RECV
))
2535 "interface %s: auth-type mismatch, local %s, rcvd Cryptographic",
2537 lookup_msg(ospf_auth_type_str
,
2538 iface_auth_type
, NULL
));
2541 if (ospfh
->checksum
) {
2542 if (IS_DEBUG_OSPF_PACKET(ospfh
->type
- 1, RECV
))
2545 "interface %s: OSPF header checksum is not 0",
2549 /* only MD5 crypto method can pass ospf_packet_examin() */
2550 if (NULL
== (ck
= listgetdata(
2551 listtail(OSPF_IF_PARAM(oi
, auth_crypt
))))
2552 || ospfh
->u
.crypt
.key_id
!= ck
->key_id
||
2553 /* Condition above uses the last key ID on the list,
2555 different from what ospf_crypt_key_lookup() does. A
2557 !ospf_check_md5_digest(oi
, ospfh
)) {
2558 if (IS_DEBUG_OSPF_PACKET(ospfh
->type
- 1, RECV
))
2559 flog_warn(EC_OSPF_MD5
,
2560 "interface %s: MD5 auth failed",
2566 if (IS_DEBUG_OSPF_PACKET(ospfh
->type
- 1, RECV
))
2569 "interface %s: invalid packet auth-type (%02x)",
2570 IF_NAME(oi
), pkt_auth_type
);
2575 static int ospf_check_sum(struct ospf_header
*ospfh
)
2580 /* clear auth_data for checksum. */
2581 memset(ospfh
->u
.auth_data
, 0, OSPF_AUTH_SIMPLE_SIZE
);
2583 /* keep checksum and clear. */
2584 sum
= ospfh
->checksum
;
2585 memset(&ospfh
->checksum
, 0, sizeof(uint16_t));
2587 /* calculate checksum. */
2588 ret
= in_cksum(ospfh
, ntohs(ospfh
->length
));
2591 zlog_info("ospf_check_sum(): checksum mismatch, my %X, his %X",
2599 /* Verify, that given link/TOS records are properly sized/aligned and match
2600 Router-LSA "# links" and "# TOS" fields as specified in RFC2328 A.4.2. */
2601 static unsigned ospf_router_lsa_links_examin(struct router_lsa_link
*link
,
2603 const uint16_t num_links
)
2605 unsigned counted_links
= 0, thislinklen
;
2609 OSPF_ROUTER_LSA_LINK_SIZE
+ 4 * link
->m
[0].tos_count
;
2610 if (thislinklen
> linkbytes
) {
2611 if (IS_DEBUG_OSPF_PACKET(0, RECV
))
2612 zlog_debug("%s: length error in link block #%u",
2613 __func__
, counted_links
);
2616 link
= (struct router_lsa_link
*)((caddr_t
)link
+ thislinklen
);
2617 linkbytes
-= thislinklen
;
2620 if (counted_links
!= num_links
) {
2621 if (IS_DEBUG_OSPF_PACKET(0, RECV
))
2622 zlog_debug("%s: %u link blocks declared, %u present",
2623 __func__
, num_links
, counted_links
);
2629 /* Verify, that the given LSA is properly sized/aligned (including type-specific
2630 minimum length constraint). */
2631 static unsigned ospf_lsa_examin(struct lsa_header
*lsah
, const uint16_t lsalen
,
2632 const uint8_t headeronly
)
2635 struct router_lsa
*rlsa
;
2636 if (lsah
->type
< OSPF_MAX_LSA
&& ospf_lsa_minlen
[lsah
->type
]
2637 && lsalen
< OSPF_LSA_HEADER_SIZE
+ ospf_lsa_minlen
[lsah
->type
]) {
2638 if (IS_DEBUG_OSPF_PACKET(0, RECV
))
2639 zlog_debug("%s: undersized (%u B) %s", __func__
, lsalen
,
2640 lookup_msg(ospf_lsa_type_msg
, lsah
->type
,
2644 switch (lsah
->type
) {
2645 case OSPF_ROUTER_LSA
:
2646 /* RFC2328 A.4.2, LSA header + 4 bytes followed by N>=1
2647 * (12+)-byte link blocks */
2649 ret
= (lsalen
- OSPF_LSA_HEADER_SIZE
2650 - OSPF_ROUTER_LSA_MIN_SIZE
)
2656 rlsa
= (struct router_lsa
*)lsah
;
2657 ret
= ospf_router_lsa_links_examin(
2658 (struct router_lsa_link
*)rlsa
->link
,
2659 lsalen
- OSPF_LSA_HEADER_SIZE
- 4, /* skip: basic
2662 ntohs(rlsa
->links
) /* 16 bits */
2665 case OSPF_AS_EXTERNAL_LSA
:
2666 /* RFC2328 A.4.5, LSA header + 4 bytes followed by N>=1 12-bytes long
2668 case OSPF_AS_NSSA_LSA
:
2669 /* RFC3101 C, idem */
2670 ret
= (lsalen
- OSPF_LSA_HEADER_SIZE
2671 - OSPF_AS_EXTERNAL_LSA_MIN_SIZE
)
2676 /* Following LSA types are considered OK length-wise as soon as their
2678 * length constraint is met and length of the whole LSA is a multiple of
2680 * (basic LSA header size is already a multiple of 4). */
2681 case OSPF_NETWORK_LSA
:
2682 /* RFC2328 A.4.3, LSA header + 4 bytes followed by N>=1 router-IDs */
2683 case OSPF_SUMMARY_LSA
:
2684 case OSPF_ASBR_SUMMARY_LSA
:
2685 /* RFC2328 A.4.4, LSA header + 4 bytes followed by N>=1 4-bytes TOS
2687 case OSPF_OPAQUE_LINK_LSA
:
2688 case OSPF_OPAQUE_AREA_LSA
:
2689 case OSPF_OPAQUE_AS_LSA
:
2690 /* RFC5250 A.2, "some number of octets (of application-specific
2691 * data) padded to 32-bit alignment." This is considered
2693 * to 4-byte alignment of all other LSA types, see
2694 * OSPF-ALIGNMENT.txt
2695 * file for the detailed analysis of this passage. */
2696 ret
= lsalen
% 4 ? MSG_NG
: MSG_OK
;
2699 if (IS_DEBUG_OSPF_PACKET(0, RECV
))
2700 zlog_debug("%s: unsupported LSA type 0x%02x", __func__
,
2704 if (ret
!= MSG_OK
&& IS_DEBUG_OSPF_PACKET(0, RECV
))
2705 zlog_debug("%s: alignment error in %s", __func__
,
2706 lookup_msg(ospf_lsa_type_msg
, lsah
->type
, NULL
));
2710 /* Verify if the provided input buffer is a valid sequence of LSAs. This
2711 includes verification of LSA blocks length/alignment and dispatching
2712 of deeper-level checks. */
2714 ospf_lsaseq_examin(struct lsa_header
*lsah
, /* start of buffered data */
2715 size_t length
, const uint8_t headeronly
,
2716 /* When declared_num_lsas is not 0, compare it to the real
2718 and treat the difference as an error. */
2719 const uint32_t declared_num_lsas
)
2721 uint32_t counted_lsas
= 0;
2725 if (length
< OSPF_LSA_HEADER_SIZE
) {
2726 if (IS_DEBUG_OSPF_PACKET(0, RECV
))
2728 "%s: undersized (%zu B) trailing (#%u) LSA header",
2729 __func__
, length
, counted_lsas
);
2732 /* save on ntohs() calls here and in the LSA validator */
2733 lsalen
= ntohs(lsah
->length
);
2734 if (lsalen
< OSPF_LSA_HEADER_SIZE
) {
2735 if (IS_DEBUG_OSPF_PACKET(0, RECV
))
2737 "%s: malformed LSA header #%u, declared length is %u B",
2738 __func__
, counted_lsas
, lsalen
);
2742 /* less checks here and in ospf_lsa_examin() */
2743 if (MSG_OK
!= ospf_lsa_examin(lsah
, lsalen
, 1)) {
2744 if (IS_DEBUG_OSPF_PACKET(0, RECV
))
2746 "%s: malformed header-only LSA #%u",
2747 __func__
, counted_lsas
);
2750 lsah
= (struct lsa_header
*)((caddr_t
)lsah
2751 + OSPF_LSA_HEADER_SIZE
);
2752 length
-= OSPF_LSA_HEADER_SIZE
;
2754 /* make sure the input buffer is deep enough before
2756 if (lsalen
> length
) {
2757 if (IS_DEBUG_OSPF_PACKET(0, RECV
))
2759 "%s: anomaly in LSA #%u: declared length is %u B, buffered length is %zu B",
2760 __func__
, counted_lsas
, lsalen
,
2764 if (MSG_OK
!= ospf_lsa_examin(lsah
, lsalen
, 0)) {
2765 if (IS_DEBUG_OSPF_PACKET(0, RECV
))
2766 zlog_debug("%s: malformed LSA #%u",
2767 __func__
, counted_lsas
);
2770 lsah
= (struct lsa_header
*)((caddr_t
)lsah
+ lsalen
);
2776 if (declared_num_lsas
&& counted_lsas
!= declared_num_lsas
) {
2777 if (IS_DEBUG_OSPF_PACKET(0, RECV
))
2779 "%s: #LSAs declared (%u) does not match actual (%u)",
2780 __func__
, declared_num_lsas
, counted_lsas
);
2786 /* Verify a complete OSPF packet for proper sizing/alignment. */
2787 static unsigned ospf_packet_examin(struct ospf_header
*oh
,
2788 const unsigned bytesonwire
)
2790 uint16_t bytesdeclared
, bytesauth
;
2792 struct ospf_ls_update
*lsupd
;
2794 /* Length, 1st approximation. */
2795 if (bytesonwire
< OSPF_HEADER_SIZE
) {
2796 if (IS_DEBUG_OSPF_PACKET(0, RECV
))
2797 zlog_debug("%s: undersized (%u B) packet", __func__
,
2801 /* Now it is safe to access header fields. Performing length check,
2803 * for possible extra bytes of crypto auth/padding, which are not
2805 * in the OSPF header "length" field. */
2806 if (oh
->version
!= OSPF_VERSION
) {
2807 if (IS_DEBUG_OSPF_PACKET(0, RECV
))
2808 zlog_debug("%s: invalid (%u) protocol version",
2809 __func__
, oh
->version
);
2812 bytesdeclared
= ntohs(oh
->length
);
2813 if (ntohs(oh
->auth_type
) != OSPF_AUTH_CRYPTOGRAPHIC
)
2816 if (oh
->u
.crypt
.auth_data_len
!= OSPF_AUTH_MD5_SIZE
) {
2817 if (IS_DEBUG_OSPF_PACKET(0, RECV
))
2819 "%s: unsupported crypto auth length (%u B)",
2820 __func__
, oh
->u
.crypt
.auth_data_len
);
2823 bytesauth
= OSPF_AUTH_MD5_SIZE
;
2825 if (bytesdeclared
+ bytesauth
> bytesonwire
) {
2826 if (IS_DEBUG_OSPF_PACKET(0, RECV
))
2828 "%s: packet length error (%u real, %u+%u declared)",
2829 __func__
, bytesonwire
, bytesdeclared
,
2833 /* Length, 2nd approximation. The type-specific constraint is checked
2834 against declared length, not amount of bytes on wire. */
2835 if (oh
->type
>= OSPF_MSG_HELLO
&& oh
->type
<= OSPF_MSG_LS_ACK
2837 < OSPF_HEADER_SIZE
+ ospf_packet_minlen
[oh
->type
]) {
2838 if (IS_DEBUG_OSPF_PACKET(0, RECV
))
2839 zlog_debug("%s: undersized (%u B) %s packet", __func__
,
2841 lookup_msg(ospf_packet_type_str
, oh
->type
,
2846 case OSPF_MSG_HELLO
:
2847 /* RFC2328 A.3.2, packet header + OSPF_HELLO_MIN_SIZE bytes
2849 by N>=0 router-IDs. */
2850 ret
= (bytesdeclared
- OSPF_HEADER_SIZE
- OSPF_HELLO_MIN_SIZE
)
2855 case OSPF_MSG_DB_DESC
:
2856 /* RFC2328 A.3.3, packet header + OSPF_DB_DESC_MIN_SIZE bytes
2858 by N>=0 header-only LSAs. */
2859 ret
= ospf_lsaseq_examin(
2860 (struct lsa_header
*)((caddr_t
)oh
+ OSPF_HEADER_SIZE
2861 + OSPF_DB_DESC_MIN_SIZE
),
2862 bytesdeclared
- OSPF_HEADER_SIZE
2863 - OSPF_DB_DESC_MIN_SIZE
,
2864 1, /* header-only LSAs */
2867 case OSPF_MSG_LS_REQ
:
2868 /* RFC2328 A.3.4, packet header followed by N>=0 12-bytes
2869 * request blocks. */
2870 ret
= (bytesdeclared
- OSPF_HEADER_SIZE
- OSPF_LS_REQ_MIN_SIZE
)
2875 case OSPF_MSG_LS_UPD
:
2876 /* RFC2328 A.3.5, packet header + OSPF_LS_UPD_MIN_SIZE bytes
2878 by N>=0 full LSAs (with N declared beforehand). */
2879 lsupd
= (struct ospf_ls_update
*)((caddr_t
)oh
2880 + OSPF_HEADER_SIZE
);
2881 ret
= ospf_lsaseq_examin(
2882 (struct lsa_header
*)((caddr_t
)lsupd
2883 + OSPF_LS_UPD_MIN_SIZE
),
2884 bytesdeclared
- OSPF_HEADER_SIZE
- OSPF_LS_UPD_MIN_SIZE
,
2886 ntohl(lsupd
->num_lsas
) /* 32 bits */
2889 case OSPF_MSG_LS_ACK
:
2890 /* RFC2328 A.3.6, packet header followed by N>=0 header-only
2892 ret
= ospf_lsaseq_examin(
2893 (struct lsa_header
*)((caddr_t
)oh
+ OSPF_HEADER_SIZE
2894 + OSPF_LS_ACK_MIN_SIZE
),
2895 bytesdeclared
- OSPF_HEADER_SIZE
- OSPF_LS_ACK_MIN_SIZE
,
2896 1, /* header-only LSAs */
2900 if (IS_DEBUG_OSPF_PACKET(0, RECV
))
2901 zlog_debug("%s: invalid packet type 0x%02x", __func__
,
2905 if (ret
!= MSG_OK
&& IS_DEBUG_OSPF_PACKET(0, RECV
))
2906 zlog_debug("%s: malformed %s packet", __func__
,
2907 lookup_msg(ospf_packet_type_str
, oh
->type
, NULL
));
2911 /* OSPF Header verification. */
2912 static int ospf_verify_header(struct stream
*ibuf
, struct ospf_interface
*oi
,
2913 struct ip
*iph
, struct ospf_header
*ospfh
)
2915 /* Check Area ID. */
2916 if (!ospf_check_area_id(oi
, ospfh
)) {
2917 flog_warn(EC_OSPF_PACKET
,
2918 "interface %s: ospf_read invalid Area ID %s.",
2919 IF_NAME(oi
), inet_ntoa(ospfh
->area_id
));
2923 /* Check network mask, Silently discarded. */
2924 if (!ospf_check_network_mask(oi
, iph
->ip_src
)) {
2927 "interface %s: ospf_read network address is not same [%s]",
2928 IF_NAME(oi
), inet_ntoa(iph
->ip_src
));
2932 /* Check authentication. The function handles logging actions, where
2934 if (!ospf_check_auth(oi
, ospfh
))
2940 /* Starting point of packet process function. */
2941 int ospf_read(struct thread
*thread
)
2944 struct stream
*ibuf
;
2946 struct ospf_interface
*oi
;
2948 struct ospf_header
*ospfh
;
2950 struct interface
*ifp
= NULL
;
2951 struct connected
*c
;
2953 /* first of all get interface pointer. */
2954 ospf
= THREAD_ARG(thread
);
2956 /* prepare for next packet. */
2957 ospf
->t_read
= NULL
;
2958 thread_add_read(master
, ospf_read
, ospf
, ospf
->fd
, &ospf
->t_read
);
2960 stream_reset(ospf
->ibuf
);
2961 ibuf
= ospf_recv_packet(ospf
, ospf
->fd
, &ifp
, ospf
->ibuf
);
2964 /* This raw packet is known to be at least as big as its IP header. */
2966 /* Note that there should not be alignment problems with this assignment
2967 because this is at the beginning of the stream data buffer. */
2968 iph
= (struct ip
*)STREAM_DATA(ibuf
);
2969 /* Note that sockopt_iphdrincl_swab_systoh was called in
2970 * ospf_recv_packet. */
2973 /* Handle cases where the platform does not support retrieving
2975 and also platforms (such as Solaris 8) that claim to support
2977 retrieval but do not. */
2978 c
= if_lookup_address((void *)&iph
->ip_src
, AF_INET
,
2986 /* IP Header dump. */
2987 if (IS_DEBUG_OSPF_PACKET(0, RECV
))
2988 ospf_ip_header_dump(iph
);
2990 /* Self-originated packet should be discarded silently. */
2991 if (ospf_if_lookup_by_local_addr(ospf
, NULL
, iph
->ip_src
)) {
2992 if (IS_DEBUG_OSPF_PACKET(0, RECV
)) {
2994 "ospf_read[%s]: Dropping self-originated packet",
2995 inet_ntoa(iph
->ip_src
));
3000 /* Advance from IP header to OSPF header (iph->ip_hl has been verified
3001 by ospf_recv_packet() to be correct). */
3002 stream_forward_getp(ibuf
, iph
->ip_hl
* 4);
3004 ospfh
= (struct ospf_header
*)stream_pnt(ibuf
);
3006 != ospf_packet_examin(
3007 ospfh
, stream_get_endp(ibuf
) - stream_get_getp(ibuf
)))
3009 /* Now it is safe to access all fields of OSPF packet header. */
3011 /* associate packet with ospf interface */
3012 oi
= ospf_if_lookup_recv_if(ospf
, iph
->ip_src
, ifp
);
3014 /* ospf_verify_header() relies on a valid "oi" and thus can be called
3016 after the passive/backbone/other checks below are passed. These
3018 in turn access the fields of unverified "ospfh" structure for their
3020 purposes and must remain very accurate in doing this. */
3022 /* If incoming interface is passive one, ignore it. */
3023 if (oi
&& OSPF_IF_PASSIVE_STATUS(oi
) == OSPF_IF_PASSIVE
) {
3024 char buf
[3][INET_ADDRSTRLEN
];
3026 if (IS_DEBUG_OSPF_EVENT
)
3028 "ignoring packet from router %s sent to %s, "
3029 "received on a passive interface, %s",
3030 inet_ntop(AF_INET
, &ospfh
->router_id
, buf
[0],
3032 inet_ntop(AF_INET
, &iph
->ip_dst
, buf
[1],
3034 inet_ntop(AF_INET
, &oi
->address
->u
.prefix4
,
3035 buf
[2], sizeof(buf
[2])));
3037 if (iph
->ip_dst
.s_addr
== htonl(OSPF_ALLSPFROUTERS
)) {
3038 /* Try to fix multicast membership.
3039 * Some OS:es may have problems in this area,
3040 * make sure it is removed.
3042 OI_MEMBER_JOINED(oi
, MEMBER_ALLROUTERS
);
3043 ospf_if_set_multicast(oi
);
3049 /* if no local ospf_interface,
3050 * or header area is backbone but ospf_interface is not
3051 * check for VLINK interface
3053 if ((oi
== NULL
) || (OSPF_IS_AREA_ID_BACKBONE(ospfh
->area_id
)
3054 && !OSPF_IS_AREA_ID_BACKBONE(oi
->area
->area_id
))) {
3055 if ((oi
= ospf_associate_packet_vl(ospf
, ifp
, iph
, ospfh
))
3057 if (!ospf
->instance
&& IS_DEBUG_OSPF_EVENT
)
3059 "Packet from [%s] received on link %s"
3060 " but no ospf_interface",
3061 inet_ntoa(iph
->ip_src
), ifp
->name
);
3066 /* else it must be a local ospf interface, check it was received on
3069 else if (oi
->ifp
!= ifp
) {
3070 if (IS_DEBUG_OSPF_EVENT
)
3071 flog_warn(EC_OSPF_PACKET
,
3072 "Packet from [%s] received on wrong link %s",
3073 inet_ntoa(iph
->ip_src
), ifp
->name
);
3075 } else if (oi
->state
== ISM_Down
) {
3076 char buf
[2][INET_ADDRSTRLEN
];
3079 "Ignoring packet from %s to %s received on interface that is "
3080 "down [%s]; interface flags are %s",
3081 inet_ntop(AF_INET
, &iph
->ip_src
, buf
[0],
3083 inet_ntop(AF_INET
, &iph
->ip_dst
, buf
[1],
3085 ifp
->name
, if_flag_dump(ifp
->flags
));
3086 /* Fix multicast memberships? */
3087 if (iph
->ip_dst
.s_addr
== htonl(OSPF_ALLSPFROUTERS
))
3088 OI_MEMBER_JOINED(oi
, MEMBER_ALLROUTERS
);
3089 else if (iph
->ip_dst
.s_addr
== htonl(OSPF_ALLDROUTERS
))
3090 OI_MEMBER_JOINED(oi
, MEMBER_DROUTERS
);
3091 if (oi
->multicast_memberships
)
3092 ospf_if_set_multicast(oi
);
3097 * If the received packet is destined for AllDRouters, the packet
3098 * should be accepted only if the received ospf interface state is
3099 * either DR or Backup -- endo.
3101 if (iph
->ip_dst
.s_addr
== htonl(OSPF_ALLDROUTERS
)
3102 && (oi
->state
!= ISM_DR
&& oi
->state
!= ISM_Backup
)) {
3105 "Dropping packet for AllDRouters from [%s] via [%s] (ISM: %s)",
3106 inet_ntoa(iph
->ip_src
), IF_NAME(oi
),
3107 lookup_msg(ospf_ism_state_msg
, oi
->state
, NULL
));
3108 /* Try to fix multicast membership. */
3109 SET_FLAG(oi
->multicast_memberships
, MEMBER_DROUTERS
);
3110 ospf_if_set_multicast(oi
);
3114 /* Verify more OSPF header fields. */
3115 ret
= ospf_verify_header(ibuf
, oi
, iph
, ospfh
);
3117 if (IS_DEBUG_OSPF_PACKET(0, RECV
))
3119 "ospf_read[%s]: Header check failed, "
3121 inet_ntoa(iph
->ip_src
));
3125 /* Show debug receiving packet. */
3126 if (IS_DEBUG_OSPF_PACKET(ospfh
->type
- 1, RECV
)) {
3127 if (IS_DEBUG_OSPF_PACKET(ospfh
->type
- 1, DETAIL
)) {
3129 "-----------------------------------------------------");
3130 ospf_packet_dump(ibuf
);
3133 zlog_debug("%s received from [%s] via [%s]",
3134 lookup_msg(ospf_packet_type_str
, ospfh
->type
, NULL
),
3135 inet_ntoa(ospfh
->router_id
), IF_NAME(oi
));
3136 zlog_debug(" src [%s],", inet_ntoa(iph
->ip_src
));
3137 zlog_debug(" dst [%s]", inet_ntoa(iph
->ip_dst
));
3139 if (IS_DEBUG_OSPF_PACKET(ospfh
->type
- 1, DETAIL
))
3141 "-----------------------------------------------------");
3144 stream_forward_getp(ibuf
, OSPF_HEADER_SIZE
);
3146 /* Adjust size to message length. */
3147 length
= ntohs(ospfh
->length
) - OSPF_HEADER_SIZE
;
3149 /* Read rest of the packet and call each sort of packet routine. */
3150 switch (ospfh
->type
) {
3151 case OSPF_MSG_HELLO
:
3152 ospf_hello(iph
, ospfh
, ibuf
, oi
, length
);
3154 case OSPF_MSG_DB_DESC
:
3155 ospf_db_desc(iph
, ospfh
, ibuf
, oi
, length
);
3157 case OSPF_MSG_LS_REQ
:
3158 ospf_ls_req(iph
, ospfh
, ibuf
, oi
, length
);
3160 case OSPF_MSG_LS_UPD
:
3161 ospf_ls_upd(ospf
, iph
, ospfh
, ibuf
, oi
, length
);
3163 case OSPF_MSG_LS_ACK
:
3164 ospf_ls_ack(iph
, ospfh
, ibuf
, oi
, length
);
3167 flog_warn(EC_OSPF_PACKET
,
3168 "interface %s: OSPF packet header type %d is illegal",
3169 IF_NAME(oi
), ospfh
->type
);
3176 /* Make OSPF header. */
3177 static void ospf_make_header(int type
, struct ospf_interface
*oi
,
3180 struct ospf_header
*ospfh
;
3182 ospfh
= (struct ospf_header
*)STREAM_DATA(s
);
3184 ospfh
->version
= (uint8_t)OSPF_VERSION
;
3185 ospfh
->type
= (uint8_t)type
;
3187 ospfh
->router_id
= oi
->ospf
->router_id
;
3189 ospfh
->checksum
= 0;
3190 ospfh
->area_id
= oi
->area
->area_id
;
3191 ospfh
->auth_type
= htons(ospf_auth_type(oi
));
3193 memset(ospfh
->u
.auth_data
, 0, OSPF_AUTH_SIMPLE_SIZE
);
3195 stream_forward_endp(s
, OSPF_HEADER_SIZE
);
3198 /* Make Authentication Data. */
3199 static int ospf_make_auth(struct ospf_interface
*oi
, struct ospf_header
*ospfh
)
3201 struct crypt_key
*ck
;
3203 switch (ospf_auth_type(oi
)) {
3204 case OSPF_AUTH_NULL
:
3205 /* memset (ospfh->u.auth_data, 0, sizeof (ospfh->u.auth_data));
3208 case OSPF_AUTH_SIMPLE
:
3209 memcpy(ospfh
->u
.auth_data
, OSPF_IF_PARAM(oi
, auth_simple
),
3210 OSPF_AUTH_SIMPLE_SIZE
);
3212 case OSPF_AUTH_CRYPTOGRAPHIC
:
3213 /* If key is not set, then set 0. */
3214 if (list_isempty(OSPF_IF_PARAM(oi
, auth_crypt
))) {
3215 ospfh
->u
.crypt
.zero
= 0;
3216 ospfh
->u
.crypt
.key_id
= 0;
3217 ospfh
->u
.crypt
.auth_data_len
= OSPF_AUTH_MD5_SIZE
;
3220 listtail(OSPF_IF_PARAM(oi
, auth_crypt
)));
3221 ospfh
->u
.crypt
.zero
= 0;
3222 ospfh
->u
.crypt
.key_id
= ck
->key_id
;
3223 ospfh
->u
.crypt
.auth_data_len
= OSPF_AUTH_MD5_SIZE
;
3225 /* note: the seq is done in ospf_make_md5_digest() */
3228 /* memset (ospfh->u.auth_data, 0, sizeof (ospfh->u.auth_data));
3236 /* Fill rest of OSPF header. */
3237 static void ospf_fill_header(struct ospf_interface
*oi
, struct stream
*s
,
3240 struct ospf_header
*ospfh
;
3242 ospfh
= (struct ospf_header
*)STREAM_DATA(s
);
3245 ospfh
->length
= htons(length
);
3247 /* Calculate checksum. */
3248 if (ntohs(ospfh
->auth_type
) != OSPF_AUTH_CRYPTOGRAPHIC
)
3249 ospfh
->checksum
= in_cksum(ospfh
, length
);
3251 ospfh
->checksum
= 0;
3253 /* Add Authentication Data. */
3254 ospf_make_auth(oi
, ospfh
);
3257 static int ospf_make_hello(struct ospf_interface
*oi
, struct stream
*s
)
3259 struct ospf_neighbor
*nbr
;
3260 struct route_node
*rn
;
3261 uint16_t length
= OSPF_HELLO_MIN_SIZE
;
3262 struct in_addr mask
;
3266 /* Set netmask of interface. */
3267 if (!(CHECK_FLAG(oi
->connected
->flags
, ZEBRA_IFA_UNNUMBERED
)
3268 && oi
->type
== OSPF_IFTYPE_POINTOPOINT
)
3269 && oi
->type
!= OSPF_IFTYPE_VIRTUALLINK
)
3270 masklen2ip(oi
->address
->prefixlen
, &mask
);
3272 memset((char *)&mask
, 0, sizeof(struct in_addr
));
3273 stream_put_ipv4(s
, mask
.s_addr
);
3275 /* Set Hello Interval. */
3276 if (OSPF_IF_PARAM(oi
, fast_hello
) == 0)
3277 stream_putw(s
, OSPF_IF_PARAM(oi
, v_hello
));
3279 stream_putw(s
, 0); /* hello-interval of 0 for fast-hellos */
3281 if (IS_DEBUG_OSPF_EVENT
)
3282 zlog_debug("make_hello: options: %x, int: %s", OPTIONS(oi
),
3286 stream_putc(s
, OPTIONS(oi
));
3288 /* Set Router Priority. */
3289 stream_putc(s
, PRIORITY(oi
));
3291 /* Set Router Dead Interval. */
3292 stream_putl(s
, OSPF_IF_PARAM(oi
, v_wait
));
3294 /* Set Designated Router. */
3295 stream_put_ipv4(s
, DR(oi
).s_addr
);
3297 p
= stream_get_endp(s
);
3299 /* Set Backup Designated Router. */
3300 stream_put_ipv4(s
, BDR(oi
).s_addr
);
3302 /* Add neighbor seen. */
3303 for (rn
= route_top(oi
->nbrs
); rn
; rn
= route_next(rn
))
3304 if ((nbr
= rn
->info
))
3305 if (nbr
->router_id
.s_addr
3306 != 0) /* Ignore 0.0.0.0 node. */
3308 != NSM_Attempt
) /* Ignore Down neighbor. */
3310 != NSM_Down
) /* This is myself for
3312 if (!IPV4_ADDR_SAME(
3314 &oi
->ospf
->router_id
)) {
3315 /* Check neighbor is
3317 if (nbr
->d_router
.s_addr
3331 /* Hello packet overflows interface MTU. */
3332 if (length
+ sizeof(uint32_t)
3333 > ospf_packet_max(oi
)) {
3335 EC_OSPF_LARGE_HELLO
,
3336 "Oversized Hello packet! Larger than MTU. Not sending it out");
3347 /* Let neighbor generate BackupSeen. */
3349 stream_putl_at(s
, p
, 0); /* ipv4 address, normally */
3354 static int ospf_make_db_desc(struct ospf_interface
*oi
,
3355 struct ospf_neighbor
*nbr
, struct stream
*s
)
3357 struct ospf_lsa
*lsa
;
3358 uint16_t length
= OSPF_DB_DESC_MIN_SIZE
;
3362 struct ospf_lsdb
*lsdb
;
3364 /* Set Interface MTU. */
3365 if (oi
->type
== OSPF_IFTYPE_VIRTUALLINK
)
3368 stream_putw(s
, oi
->ifp
->mtu
);
3371 options
= OPTIONS(oi
);
3372 if (CHECK_FLAG(oi
->ospf
->config
, OSPF_OPAQUE_CAPABLE
))
3373 SET_FLAG(options
, OSPF_OPTION_O
);
3374 stream_putc(s
, options
);
3377 pp
= stream_get_endp(s
);
3378 stream_putc(s
, nbr
->dd_flags
);
3380 /* Set DD Sequence Number. */
3381 stream_putl(s
, nbr
->dd_seqnum
);
3383 /* shortcut unneeded walk of (empty) summary LSDBs */
3384 if (ospf_db_summary_isempty(nbr
))
3387 /* Describe LSA Header from Database Summary List. */
3388 lsdb
= &nbr
->db_sum
;
3390 for (i
= OSPF_MIN_LSA
; i
< OSPF_MAX_LSA
; i
++) {
3391 struct route_table
*table
= lsdb
->type
[i
].db
;
3392 struct route_node
*rn
;
3394 for (rn
= route_top(table
); rn
; rn
= route_next(rn
))
3395 if ((lsa
= rn
->info
) != NULL
) {
3396 if (IS_OPAQUE_LSA(lsa
->data
->type
)
3397 && (!CHECK_FLAG(options
, OSPF_OPTION_O
))) {
3398 /* Suppress advertising
3399 * opaque-information. */
3400 /* Remove LSA from DB summary list. */
3401 ospf_lsdb_delete(lsdb
, lsa
);
3405 if (!CHECK_FLAG(lsa
->flags
, OSPF_LSA_DISCARD
)) {
3406 struct lsa_header
*lsah
;
3409 /* DD packet overflows interface MTU. */
3410 if (length
+ OSPF_LSA_HEADER_SIZE
3411 > ospf_packet_max(oi
))
3414 /* Keep pointer to LS age. */
3415 lsah
= (struct lsa_header
3420 /* Proceed stream pointer. */
3421 stream_put(s
, lsa
->data
,
3422 OSPF_LSA_HEADER_SIZE
);
3423 length
+= OSPF_LSA_HEADER_SIZE
;
3426 ls_age
= LS_AGE(lsa
);
3427 lsah
->ls_age
= htons(ls_age
);
3430 /* Remove LSA from DB summary list. */
3431 ospf_lsdb_delete(lsdb
, lsa
);
3435 /* Update 'More' bit */
3436 if (ospf_db_summary_isempty(nbr
)) {
3438 if (nbr
->state
>= NSM_Exchange
) {
3439 UNSET_FLAG(nbr
->dd_flags
, OSPF_DD_FLAG_M
);
3440 /* Rewrite DD flags */
3441 stream_putc_at(s
, pp
, nbr
->dd_flags
);
3443 assert(IS_SET_DD_M(nbr
->dd_flags
));
3449 static int ospf_make_ls_req_func(struct stream
*s
, uint16_t *length
,
3450 unsigned long delta
, struct ospf_neighbor
*nbr
,
3451 struct ospf_lsa
*lsa
)
3453 struct ospf_interface
*oi
;
3457 /* LS Request packet overflows interface MTU
3458 * delta is just number of bytes required for 1 LS Req
3459 * ospf_packet_max will return the number of bytes can
3460 * be accomodated without ospf header. So length+delta
3461 * can be compared to ospf_packet_max
3462 * to check if it can fit another lsreq in the same packet.
3465 if (*length
+ delta
> ospf_packet_max(oi
))
3468 stream_putl(s
, lsa
->data
->type
);
3469 stream_put_ipv4(s
, lsa
->data
->id
.s_addr
);
3470 stream_put_ipv4(s
, lsa
->data
->adv_router
.s_addr
);
3472 ospf_lsa_unlock(&nbr
->ls_req_last
);
3473 nbr
->ls_req_last
= ospf_lsa_lock(lsa
);
3479 static int ospf_make_ls_req(struct ospf_neighbor
*nbr
, struct stream
*s
)
3481 struct ospf_lsa
*lsa
;
3482 uint16_t length
= OSPF_LS_REQ_MIN_SIZE
;
3483 unsigned long delta
= 12;
3484 struct route_table
*table
;
3485 struct route_node
*rn
;
3487 struct ospf_lsdb
*lsdb
;
3489 lsdb
= &nbr
->ls_req
;
3491 for (i
= OSPF_MIN_LSA
; i
< OSPF_MAX_LSA
; i
++) {
3492 table
= lsdb
->type
[i
].db
;
3493 for (rn
= route_top(table
); rn
; rn
= route_next(rn
))
3494 if ((lsa
= (rn
->info
)) != NULL
)
3495 if (ospf_make_ls_req_func(s
, &length
, delta
,
3498 route_unlock_node(rn
);
3505 static int ls_age_increment(struct ospf_lsa
*lsa
, int delay
)
3509 age
= IS_LSA_MAXAGE(lsa
) ? OSPF_LSA_MAXAGE
: LS_AGE(lsa
) + delay
;
3511 return (age
> OSPF_LSA_MAXAGE
? OSPF_LSA_MAXAGE
: age
);
3514 static int ospf_make_ls_upd(struct ospf_interface
*oi
, struct list
*update
,
3517 struct ospf_lsa
*lsa
;
3518 struct listnode
*node
;
3519 uint16_t length
= 0;
3520 unsigned int size_noauth
;
3521 unsigned long delta
= stream_get_endp(s
);
3525 if (IS_DEBUG_OSPF_EVENT
)
3526 zlog_debug("ospf_make_ls_upd: Start");
3528 pp
= stream_get_endp(s
);
3529 stream_forward_endp(s
, OSPF_LS_UPD_MIN_SIZE
);
3530 length
+= OSPF_LS_UPD_MIN_SIZE
;
3532 /* Calculate amount of packet usable for data. */
3533 size_noauth
= stream_get_size(s
) - ospf_packet_authspace(oi
);
3535 while ((node
= listhead(update
)) != NULL
) {
3536 struct lsa_header
*lsah
;
3539 if (IS_DEBUG_OSPF_EVENT
)
3540 zlog_debug("ospf_make_ls_upd: List Iteration %d",
3543 lsa
= listgetdata(node
);
3547 /* Will it fit? Minimum it has to fit atleast one */
3548 if ((length
+ delta
+ ntohs(lsa
->data
->length
) > size_noauth
) &&
3552 /* Keep pointer to LS age. */
3553 lsah
= (struct lsa_header
*)(STREAM_DATA(s
)
3554 + stream_get_endp(s
));
3556 /* Put LSA to Link State Request. */
3557 stream_put(s
, lsa
->data
, ntohs(lsa
->data
->length
));
3560 /* each hop must increment an lsa_age by transmit_delay
3561 of OSPF interface */
3562 ls_age
= ls_age_increment(lsa
,
3563 OSPF_IF_PARAM(oi
, transmit_delay
));
3564 lsah
->ls_age
= htons(ls_age
);
3566 length
+= ntohs(lsa
->data
->length
);
3569 list_delete_node(update
, node
);
3570 ospf_lsa_unlock(&lsa
); /* oi->ls_upd_queue */
3573 /* Now set #LSAs. */
3574 stream_putl_at(s
, pp
, count
);
3576 if (IS_DEBUG_OSPF_EVENT
)
3577 zlog_debug("ospf_make_ls_upd: Stop");
3581 static int ospf_make_ls_ack(struct ospf_interface
*oi
, struct list
*ack
,
3584 struct listnode
*node
, *nnode
;
3585 uint16_t length
= OSPF_LS_ACK_MIN_SIZE
;
3586 unsigned long delta
= OSPF_LSA_HEADER_SIZE
;
3587 struct ospf_lsa
*lsa
;
3589 for (ALL_LIST_ELEMENTS(ack
, node
, nnode
, lsa
)) {
3592 /* LS Ack packet overflows interface MTU
3593 * delta is just number of bytes required for
3594 * 1 LS Ack(1 LS Hdr) ospf_packet_max will return
3595 * the number of bytes can be accomodated without
3596 * ospf header. So length+delta can be compared
3597 * against ospf_packet_max to check if it can fit
3598 * another ls header in the same packet.
3600 if ((length
+ delta
) > ospf_packet_max(oi
))
3603 stream_put(s
, lsa
->data
, OSPF_LSA_HEADER_SIZE
);
3604 length
+= OSPF_LSA_HEADER_SIZE
;
3606 listnode_delete(ack
, lsa
);
3607 ospf_lsa_unlock(&lsa
); /* oi->ls_ack_direct.ls_ack */
3613 static void ospf_hello_send_sub(struct ospf_interface
*oi
, in_addr_t addr
)
3615 struct ospf_packet
*op
;
3616 uint16_t length
= OSPF_HEADER_SIZE
;
3618 op
= ospf_packet_new(oi
->ifp
->mtu
);
3620 /* Prepare OSPF common header. */
3621 ospf_make_header(OSPF_MSG_HELLO
, oi
, op
->s
);
3623 /* Prepare OSPF Hello body. */
3624 length
+= ospf_make_hello(oi
, op
->s
);
3625 if (length
== OSPF_HEADER_SIZE
) {
3626 /* Hello overshooting MTU */
3627 ospf_packet_free(op
);
3631 /* Fill OSPF header. */
3632 ospf_fill_header(oi
, op
->s
, length
);
3634 /* Set packet length. */
3635 op
->length
= length
;
3637 op
->dst
.s_addr
= addr
;
3639 if (IS_DEBUG_OSPF_EVENT
) {
3640 if (oi
->ospf
->vrf_id
)
3642 "%s: Hello Tx interface %s ospf vrf %s id %u",
3643 __PRETTY_FUNCTION__
, oi
->ifp
->name
,
3644 ospf_vrf_id_to_name(oi
->ospf
->vrf_id
),
3647 /* Add packet to the top of the interface output queue, so that they
3648 * can't get delayed by things like long queues of LS Update packets
3650 ospf_packet_add_top(oi
, op
);
3652 /* Hook thread to write packet. */
3653 OSPF_ISM_WRITE_ON(oi
->ospf
);
3656 static void ospf_poll_send(struct ospf_nbr_nbma
*nbr_nbma
)
3658 struct ospf_interface
*oi
;
3663 /* If this is passive interface, do not send OSPF Hello. */
3664 if (OSPF_IF_PASSIVE_STATUS(oi
) == OSPF_IF_PASSIVE
)
3667 if (oi
->type
!= OSPF_IFTYPE_NBMA
)
3670 if (nbr_nbma
->nbr
!= NULL
&& nbr_nbma
->nbr
->state
!= NSM_Down
)
3673 if (PRIORITY(oi
) == 0)
3676 if (nbr_nbma
->priority
== 0 && oi
->state
!= ISM_DR
3677 && oi
->state
!= ISM_Backup
)
3680 ospf_hello_send_sub(oi
, nbr_nbma
->addr
.s_addr
);
3683 int ospf_poll_timer(struct thread
*thread
)
3685 struct ospf_nbr_nbma
*nbr_nbma
;
3687 nbr_nbma
= THREAD_ARG(thread
);
3688 nbr_nbma
->t_poll
= NULL
;
3690 if (IS_DEBUG_OSPF(nsm
, NSM_TIMERS
))
3691 zlog_debug("NSM[%s:%s]: Timer (Poll timer expire)",
3692 IF_NAME(nbr_nbma
->oi
), inet_ntoa(nbr_nbma
->addr
));
3694 ospf_poll_send(nbr_nbma
);
3696 if (nbr_nbma
->v_poll
> 0)
3697 OSPF_POLL_TIMER_ON(nbr_nbma
->t_poll
, ospf_poll_timer
,
3704 int ospf_hello_reply_timer(struct thread
*thread
)
3706 struct ospf_neighbor
*nbr
;
3708 nbr
= THREAD_ARG(thread
);
3709 nbr
->t_hello_reply
= NULL
;
3713 if (IS_DEBUG_OSPF(nsm
, NSM_TIMERS
))
3714 zlog_debug("NSM[%s:%s]: Timer (hello-reply timer expire)",
3715 IF_NAME(nbr
->oi
), inet_ntoa(nbr
->router_id
));
3717 ospf_hello_send_sub(nbr
->oi
, nbr
->address
.u
.prefix4
.s_addr
);
3722 /* Send OSPF Hello. */
3723 void ospf_hello_send(struct ospf_interface
*oi
)
3725 /* If this is passive interface, do not send OSPF Hello. */
3726 if (OSPF_IF_PASSIVE_STATUS(oi
) == OSPF_IF_PASSIVE
)
3729 if (oi
->type
== OSPF_IFTYPE_NBMA
) {
3730 struct ospf_neighbor
*nbr
;
3731 struct route_node
*rn
;
3733 for (rn
= route_top(oi
->nbrs
); rn
; rn
= route_next(rn
))
3734 if ((nbr
= rn
->info
))
3735 if (nbr
!= oi
->nbr_self
)
3736 if (nbr
->state
!= NSM_Down
) {
3737 /* RFC 2328 Section 9.5.1
3738 If the router is not
3739 eligible to become Designated
3741 it must periodically send
3742 Hello Packets to both the
3743 Designated Router and the
3744 Backup Designated Router (if
3747 if (PRIORITY(oi
) == 0
3758 /* If the router is eligible to
3759 become Designated Router, it
3760 must periodically send Hello
3761 Packets to all neighbors that
3762 are also eligible. In
3763 addition, if the router is
3765 Designated Router or Backup
3766 Designated Router, it must
3768 send periodic Hello Packets
3769 to all other neighbors. */
3771 if (nbr
->priority
== 0
3772 && oi
->state
== ISM_DROther
)
3774 /* if oi->state == Waiting, send
3775 * hello to all neighbors */
3776 ospf_hello_send_sub(
3778 nbr
->address
.u
.prefix4
3782 /* Decide destination address. */
3783 if (oi
->type
== OSPF_IFTYPE_VIRTUALLINK
)
3784 ospf_hello_send_sub(oi
, oi
->vl_data
->peer_addr
.s_addr
);
3786 ospf_hello_send_sub(oi
, htonl(OSPF_ALLSPFROUTERS
));
3790 /* Send OSPF Database Description. */
3791 void ospf_db_desc_send(struct ospf_neighbor
*nbr
)
3793 struct ospf_interface
*oi
;
3794 struct ospf_packet
*op
;
3795 uint16_t length
= OSPF_HEADER_SIZE
;
3798 op
= ospf_packet_new(oi
->ifp
->mtu
);
3800 /* Prepare OSPF common header. */
3801 ospf_make_header(OSPF_MSG_DB_DESC
, oi
, op
->s
);
3803 /* Prepare OSPF Database Description body. */
3804 length
+= ospf_make_db_desc(oi
, nbr
, op
->s
);
3806 /* Fill OSPF header. */
3807 ospf_fill_header(oi
, op
->s
, length
);
3809 /* Set packet length. */
3810 op
->length
= length
;
3812 /* Decide destination address. */
3813 if (oi
->type
== OSPF_IFTYPE_POINTOPOINT
)
3814 op
->dst
.s_addr
= htonl(OSPF_ALLSPFROUTERS
);
3816 op
->dst
= nbr
->address
.u
.prefix4
;
3818 /* Add packet to the interface output queue. */
3819 ospf_packet_add(oi
, op
);
3821 /* Hook thread to write packet. */
3822 OSPF_ISM_WRITE_ON(oi
->ospf
);
3824 /* Remove old DD packet, then copy new one and keep in neighbor
3827 ospf_packet_free(nbr
->last_send
);
3828 nbr
->last_send
= ospf_packet_dup(op
);
3829 monotime(&nbr
->last_send_ts
);
3832 /* Re-send Database Description. */
3833 void ospf_db_desc_resend(struct ospf_neighbor
*nbr
)
3835 struct ospf_interface
*oi
;
3839 /* Add packet to the interface output queue. */
3840 ospf_packet_add(oi
, ospf_packet_dup(nbr
->last_send
));
3842 /* Hook thread to write packet. */
3843 OSPF_ISM_WRITE_ON(oi
->ospf
);
3846 /* Send Link State Request. */
3847 void ospf_ls_req_send(struct ospf_neighbor
*nbr
)
3849 struct ospf_interface
*oi
;
3850 struct ospf_packet
*op
;
3851 uint16_t length
= OSPF_HEADER_SIZE
;
3854 op
= ospf_packet_new(oi
->ifp
->mtu
);
3856 /* Prepare OSPF common header. */
3857 ospf_make_header(OSPF_MSG_LS_REQ
, oi
, op
->s
);
3859 /* Prepare OSPF Link State Request body. */
3860 length
+= ospf_make_ls_req(nbr
, op
->s
);
3861 if (length
== OSPF_HEADER_SIZE
) {
3862 ospf_packet_free(op
);
3866 /* Fill OSPF header. */
3867 ospf_fill_header(oi
, op
->s
, length
);
3869 /* Set packet length. */
3870 op
->length
= length
;
3872 /* Decide destination address. */
3873 if (oi
->type
== OSPF_IFTYPE_POINTOPOINT
)
3874 op
->dst
.s_addr
= htonl(OSPF_ALLSPFROUTERS
);
3876 op
->dst
= nbr
->address
.u
.prefix4
;
3878 /* Add packet to the interface output queue. */
3879 ospf_packet_add(oi
, op
);
3881 /* Hook thread to write packet. */
3882 OSPF_ISM_WRITE_ON(oi
->ospf
);
3884 /* Add Link State Request Retransmission Timer. */
3885 OSPF_NSM_TIMER_ON(nbr
->t_ls_req
, ospf_ls_req_timer
, nbr
->v_ls_req
);
3888 /* Send Link State Update with an LSA. */
3889 void ospf_ls_upd_send_lsa(struct ospf_neighbor
*nbr
, struct ospf_lsa
*lsa
,
3892 struct list
*update
;
3894 update
= list_new();
3896 listnode_add(update
, lsa
);
3898 /*ospf instance is going down, send self originated
3899 * MAXAGE LSA update to neighbors to remove from LSDB */
3900 if (nbr
->oi
->ospf
->inst_shutdown
&& IS_LSA_MAXAGE(lsa
))
3901 ospf_ls_upd_send(nbr
, update
, flag
, 1);
3903 ospf_ls_upd_send(nbr
, update
, flag
, 0);
3905 list_delete(&update
);
3908 /* Determine size for packet. Must be at least big enough to accomodate next
3909 * LSA on list, which may be bigger than MTU size.
3911 * Return pointer to new ospf_packet
3912 * NULL if we can not allocate, eg because LSA is bigger than imposed limit
3913 * on packet sizes (in which case offending LSA is deleted from update list)
3915 static struct ospf_packet
*ospf_ls_upd_packet_new(struct list
*update
,
3916 struct ospf_interface
*oi
)
3918 struct ospf_lsa
*lsa
;
3919 struct listnode
*ln
;
3921 static char warned
= 0;
3923 lsa
= listgetdata((ln
= listhead(update
)));
3926 if ((OSPF_LS_UPD_MIN_SIZE
+ ntohs(lsa
->data
->length
))
3927 > ospf_packet_max(oi
)) {
3931 "ospf_ls_upd_packet_new: oversized LSA encountered!"
3932 "will need to fragment. Not optimal. Try divide up"
3933 " your network with areas. Use 'debug ospf packet send'"
3934 " to see details, or look at 'show ip ospf database ..'");
3938 if (IS_DEBUG_OSPF_PACKET(0, SEND
))
3940 "ospf_ls_upd_packet_new: oversized LSA id:%s,"
3941 " %d bytes originated by %s, will be fragmented!",
3942 inet_ntoa(lsa
->data
->id
),
3943 ntohs(lsa
->data
->length
),
3944 inet_ntoa(lsa
->data
->adv_router
));
3947 * Allocate just enough to fit this LSA only, to avoid including
3949 * LSAs in fragmented LSA Updates.
3951 size
= ntohs(lsa
->data
->length
)
3952 + (oi
->ifp
->mtu
- ospf_packet_max(oi
))
3953 + OSPF_LS_UPD_MIN_SIZE
;
3955 size
= oi
->ifp
->mtu
;
3957 if (size
> OSPF_MAX_PACKET_SIZE
) {
3958 flog_warn(EC_OSPF_LARGE_LSA
,
3959 "ospf_ls_upd_packet_new: oversized LSA id:%s too big,"
3960 " %d bytes, packet size %ld, dropping it completely."
3961 " OSPF routing is broken!",
3962 inet_ntoa(lsa
->data
->id
), ntohs(lsa
->data
->length
),
3964 list_delete_node(update
, ln
);
3968 /* IP header is built up separately by ospf_write(). This means, that we
3970 * reduce the "affordable" size just calculated by length of an IP
3972 * This makes sure, that even if we manage to fill the payload with LSA
3974 * completely, the final packet (our data plus IP header) still fits
3976 * outgoing interface MTU. This correction isn't really meaningful for
3978 * oversized LSA, but for consistency the correction is done for both
3981 * P.S. OSPF_MAX_PACKET_SIZE above already includes IP header size
3983 return ospf_packet_new(size
- sizeof(struct ip
));
3986 static void ospf_ls_upd_queue_send(struct ospf_interface
*oi
,
3987 struct list
*update
, struct in_addr addr
,
3990 struct ospf_packet
*op
;
3991 uint16_t length
= OSPF_HEADER_SIZE
;
3993 if (IS_DEBUG_OSPF_EVENT
)
3994 zlog_debug("listcount = %d, [%s]dst %s", listcount(update
),
3995 IF_NAME(oi
), inet_ntoa(addr
));
3997 /* Check that we have really something to process */
3998 if (listcount(update
) == 0)
4001 op
= ospf_ls_upd_packet_new(update
, oi
);
4003 /* Prepare OSPF common header. */
4004 ospf_make_header(OSPF_MSG_LS_UPD
, oi
, op
->s
);
4006 /* Prepare OSPF Link State Update body.
4007 * Includes Type-7 translation.
4009 length
+= ospf_make_ls_upd(oi
, update
, op
->s
);
4011 /* Fill OSPF header. */
4012 ospf_fill_header(oi
, op
->s
, length
);
4014 /* Set packet length. */
4015 op
->length
= length
;
4017 /* Decide destination address. */
4018 if (oi
->type
== OSPF_IFTYPE_POINTOPOINT
)
4019 op
->dst
.s_addr
= htonl(OSPF_ALLSPFROUTERS
);
4021 op
->dst
.s_addr
= addr
.s_addr
;
4023 /* Add packet to the interface output queue. */
4024 ospf_packet_add(oi
, op
);
4025 /* Call ospf_write() right away to send ospf packets to neighbors */
4026 if (send_lsupd_now
) {
4027 struct thread os_packet_thd
;
4029 os_packet_thd
.arg
= (void *)oi
->ospf
;
4030 if (oi
->on_write_q
== 0) {
4031 listnode_add(oi
->ospf
->oi_write_q
, oi
);
4034 ospf_write(&os_packet_thd
);
4036 /* Hook thread to write packet. */
4037 OSPF_ISM_WRITE_ON(oi
->ospf
);
4041 static int ospf_ls_upd_send_queue_event(struct thread
*thread
)
4043 struct ospf_interface
*oi
= THREAD_ARG(thread
);
4044 struct route_node
*rn
;
4045 struct route_node
*rnext
;
4046 struct list
*update
;
4049 oi
->t_ls_upd_event
= NULL
;
4051 if (IS_DEBUG_OSPF_EVENT
)
4052 zlog_debug("ospf_ls_upd_send_queue start");
4054 for (rn
= route_top(oi
->ls_upd_queue
); rn
; rn
= rnext
) {
4055 rnext
= route_next(rn
);
4057 if (rn
->info
== NULL
)
4060 update
= (struct list
*)rn
->info
;
4062 ospf_ls_upd_queue_send(oi
, update
, rn
->p
.u
.prefix4
, 0);
4064 /* list might not be empty. */
4065 if (listcount(update
) == 0) {
4066 list_delete((struct list
**)&rn
->info
);
4067 route_unlock_node(rn
);
4073 if (IS_DEBUG_OSPF_EVENT
)
4075 "ospf_ls_upd_send_queue: update lists not cleared,"
4076 " %d nodes to try again, raising new event",
4078 oi
->t_ls_upd_event
= NULL
;
4079 thread_add_event(master
, ospf_ls_upd_send_queue_event
, oi
, 0,
4080 &oi
->t_ls_upd_event
);
4083 if (IS_DEBUG_OSPF_EVENT
)
4084 zlog_debug("ospf_ls_upd_send_queue stop");
4089 void ospf_ls_upd_send(struct ospf_neighbor
*nbr
, struct list
*update
, int flag
,
4092 struct ospf_interface
*oi
;
4093 struct ospf_lsa
*lsa
;
4094 struct prefix_ipv4 p
;
4095 struct route_node
*rn
;
4096 struct listnode
*node
;
4101 p
.prefixlen
= IPV4_MAX_BITLEN
;
4103 /* Decide destination address. */
4104 if (oi
->type
== OSPF_IFTYPE_VIRTUALLINK
)
4105 p
.prefix
= oi
->vl_data
->peer_addr
;
4106 else if (oi
->type
== OSPF_IFTYPE_POINTOPOINT
)
4107 p
.prefix
.s_addr
= htonl(OSPF_ALLSPFROUTERS
);
4108 else if (flag
== OSPF_SEND_PACKET_DIRECT
)
4109 p
.prefix
= nbr
->address
.u
.prefix4
;
4110 else if (oi
->state
== ISM_DR
|| oi
->state
== ISM_Backup
)
4111 p
.prefix
.s_addr
= htonl(OSPF_ALLSPFROUTERS
);
4112 else if (oi
->type
== OSPF_IFTYPE_POINTOMULTIPOINT
)
4113 p
.prefix
.s_addr
= htonl(OSPF_ALLSPFROUTERS
);
4115 p
.prefix
.s_addr
= htonl(OSPF_ALLDROUTERS
);
4117 if (oi
->type
== OSPF_IFTYPE_NBMA
) {
4118 if (flag
== OSPF_SEND_PACKET_INDIRECT
)
4121 "* LS-Update is directly sent on NBMA network.");
4122 if (IPV4_ADDR_SAME(&oi
->address
->u
.prefix4
, &p
.prefix
))
4123 flog_warn(EC_OSPF_PACKET
,
4124 "* LS-Update is sent to myself.");
4127 rn
= route_node_get(oi
->ls_upd_queue
, (struct prefix
*)&p
);
4129 if (rn
->info
== NULL
)
4130 rn
->info
= list_new();
4132 route_unlock_node(rn
);
4134 for (ALL_LIST_ELEMENTS_RO(update
, node
, lsa
))
4135 listnode_add(rn
->info
,
4136 ospf_lsa_lock(lsa
)); /* oi->ls_upd_queue */
4137 if (send_lsupd_now
) {
4138 struct list
*send_update_list
;
4139 struct route_node
*rnext
;
4141 for (rn
= route_top(oi
->ls_upd_queue
); rn
; rn
= rnext
) {
4142 rnext
= route_next(rn
);
4144 if (rn
->info
== NULL
)
4147 send_update_list
= (struct list
*)rn
->info
;
4149 ospf_ls_upd_queue_send(oi
, send_update_list
,
4150 rn
->p
.u
.prefix4
, 1);
4153 thread_add_event(master
, ospf_ls_upd_send_queue_event
, oi
, 0,
4154 &oi
->t_ls_upd_event
);
4157 static void ospf_ls_ack_send_list(struct ospf_interface
*oi
, struct list
*ack
,
4160 struct ospf_packet
*op
;
4161 uint16_t length
= OSPF_HEADER_SIZE
;
4163 op
= ospf_packet_new(oi
->ifp
->mtu
);
4165 /* Prepare OSPF common header. */
4166 ospf_make_header(OSPF_MSG_LS_ACK
, oi
, op
->s
);
4168 /* Prepare OSPF Link State Acknowledgment body. */
4169 length
+= ospf_make_ls_ack(oi
, ack
, op
->s
);
4171 /* Fill OSPF header. */
4172 ospf_fill_header(oi
, op
->s
, length
);
4174 /* Set packet length. */
4175 op
->length
= length
;
4177 /* Decide destination address. */
4178 if (oi
->type
== OSPF_IFTYPE_POINTOPOINT
)
4179 op
->dst
.s_addr
= htonl(OSPF_ALLSPFROUTERS
);
4181 op
->dst
.s_addr
= dst
.s_addr
;
4183 /* Add packet to the interface output queue. */
4184 ospf_packet_add(oi
, op
);
4186 /* Hook thread to write packet. */
4187 OSPF_ISM_WRITE_ON(oi
->ospf
);
4190 static int ospf_ls_ack_send_event(struct thread
*thread
)
4192 struct ospf_interface
*oi
= THREAD_ARG(thread
);
4194 oi
->t_ls_ack_direct
= NULL
;
4196 while (listcount(oi
->ls_ack_direct
.ls_ack
))
4197 ospf_ls_ack_send_list(oi
, oi
->ls_ack_direct
.ls_ack
,
4198 oi
->ls_ack_direct
.dst
);
4203 void ospf_ls_ack_send(struct ospf_neighbor
*nbr
, struct ospf_lsa
*lsa
)
4205 struct ospf_interface
*oi
= nbr
->oi
;
4207 if (listcount(oi
->ls_ack_direct
.ls_ack
) == 0)
4208 oi
->ls_ack_direct
.dst
= nbr
->address
.u
.prefix4
;
4210 listnode_add(oi
->ls_ack_direct
.ls_ack
, ospf_lsa_lock(lsa
));
4212 thread_add_event(master
, ospf_ls_ack_send_event
, oi
, 0,
4213 &oi
->t_ls_ack_direct
);
4216 /* Send Link State Acknowledgment delayed. */
4217 void ospf_ls_ack_send_delayed(struct ospf_interface
*oi
)
4221 /* Decide destination address. */
4222 /* RFC2328 Section 13.5 On non-broadcast
4223 networks, delayed Link State Acknowledgment packets must be
4224 unicast separately over each adjacency (i.e., neighbor whose
4225 state is >= Exchange). */
4226 if (oi
->type
== OSPF_IFTYPE_NBMA
) {
4227 struct ospf_neighbor
*nbr
;
4228 struct route_node
*rn
;
4230 for (rn
= route_top(oi
->nbrs
); rn
; rn
= route_next(rn
))
4231 if ((nbr
= rn
->info
) != NULL
)
4232 if (nbr
!= oi
->nbr_self
4233 && nbr
->state
>= NSM_Exchange
)
4234 while (listcount(oi
->ls_ack
))
4235 ospf_ls_ack_send_list(
4237 nbr
->address
.u
.prefix4
);
4240 if (oi
->type
== OSPF_IFTYPE_VIRTUALLINK
)
4241 dst
.s_addr
= oi
->vl_data
->peer_addr
.s_addr
;
4242 else if (oi
->state
== ISM_DR
|| oi
->state
== ISM_Backup
)
4243 dst
.s_addr
= htonl(OSPF_ALLSPFROUTERS
);
4244 else if (oi
->type
== OSPF_IFTYPE_POINTOPOINT
)
4245 dst
.s_addr
= htonl(OSPF_ALLSPFROUTERS
);
4246 else if (oi
->type
== OSPF_IFTYPE_POINTOMULTIPOINT
)
4247 dst
.s_addr
= htonl(OSPF_ALLSPFROUTERS
);
4249 dst
.s_addr
= htonl(OSPF_ALLDROUTERS
);
4251 while (listcount(oi
->ls_ack
))
4252 ospf_ls_ack_send_list(oi
, oi
->ls_ack
, dst
);
4256 * On pt-to-pt links, all OSPF control packets are sent to the multicast
4257 * address. As a result, the kernel does not need to learn the interface
4258 * MAC of the OSPF neighbor. However, in our world, this will delay
4259 * convergence. Take the case when due to a link flap, all routes now
4260 * want to use an interface which was deemed to be costlier prior to this
4261 * event. For routes that will be installed, the missing MAC will have
4262 * punt-to-CPU set on them. This may overload the CPU control path that
4263 * can be avoided if the MAC was known apriori.
4265 #define OSPF_PING_NBR_STR_MAX (BUFSIZ)
4266 void ospf_proactively_arp(struct ospf_neighbor
*nbr
)
4268 char ping_nbr
[OSPF_PING_NBR_STR_MAX
];
4271 if (!nbr
|| !nbr
->oi
|| !nbr
->oi
->ifp
)
4274 snprintf(ping_nbr
, sizeof(ping_nbr
),
4275 "ping -c 1 -I %s %s > /dev/null 2>&1 &", nbr
->oi
->ifp
->name
,
4276 inet_ntoa(nbr
->address
.u
.prefix4
));
4278 ret
= system(ping_nbr
);
4279 if (IS_DEBUG_OSPF_EVENT
)
4280 zlog_debug("Executed %s %s", ping_nbr
,
4281 ((ret
== 0) ? "successfully" : "but failed"));