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 ospf
->t_write
= NULL
;
665 node
= listhead(ospf
->oi_write_q
);
667 oi
= listgetdata(node
);
670 #ifdef WANT_OSPF_WRITE_FRAGMENT
671 /* seed ipid static with low order bits of time */
673 ipid
= (time(NULL
) & 0xffff);
674 #endif /* WANT_OSPF_WRITE_FRAGMENT */
676 while ((pkt_count
< ospf
->write_oi_count
) && oi
677 && (last_serviced_oi
!= oi
)) {
678 /* If there is only packet in the queue, the oi is removed from
679 write-q, so fix up the last interface that was serviced */
680 if (last_serviced_oi
== NULL
) {
681 last_serviced_oi
= oi
;
684 #ifdef WANT_OSPF_WRITE_FRAGMENT
685 /* convenience - max OSPF data per packet */
686 maxdatasize
= oi
->ifp
->mtu
- sizeof(struct ip
);
687 #endif /* WANT_OSPF_WRITE_FRAGMENT */
688 /* Get one packet from queue. */
689 op
= ospf_fifo_head(oi
->obuf
);
691 assert(op
->length
>= OSPF_HEADER_SIZE
);
693 if (op
->dst
.s_addr
== htonl(OSPF_ALLSPFROUTERS
)
694 || op
->dst
.s_addr
== htonl(OSPF_ALLDROUTERS
))
695 ospf_if_ipmulticast(ospf
, oi
->address
,
698 /* Rewrite the md5 signature & update the seq */
699 ospf_make_md5_digest(oi
, op
);
701 /* Retrieve OSPF packet type. */
702 stream_set_getp(op
->s
, 1);
703 type
= stream_getc(op
->s
);
705 /* reset get pointer */
706 stream_set_getp(op
->s
, 0);
708 memset(&iph
, 0, sizeof(struct ip
));
709 memset(&sa_dst
, 0, sizeof(sa_dst
));
711 sa_dst
.sin_family
= AF_INET
;
712 #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
713 sa_dst
.sin_len
= sizeof(sa_dst
);
714 #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
715 sa_dst
.sin_addr
= op
->dst
;
716 sa_dst
.sin_port
= htons(0);
718 /* Set DONTROUTE flag if dst is unicast. */
719 if (oi
->type
!= OSPF_IFTYPE_VIRTUALLINK
)
720 if (!IN_MULTICAST(htonl(op
->dst
.s_addr
)))
721 flags
= MSG_DONTROUTE
;
723 iph
.ip_hl
= sizeof(struct ip
) >> OSPF_WRITE_IPHL_SHIFT
;
724 /* it'd be very strange for header to not be 4byte-word aligned
726 if (sizeof(struct ip
)
727 > (unsigned int)(iph
.ip_hl
<< OSPF_WRITE_IPHL_SHIFT
))
728 iph
.ip_hl
++; /* we presume sizeof struct ip cant
731 iph
.ip_v
= IPVERSION
;
732 iph
.ip_tos
= IPTOS_PREC_INTERNETCONTROL
;
733 iph
.ip_len
= (iph
.ip_hl
<< OSPF_WRITE_IPHL_SHIFT
) + op
->length
;
735 #if defined(__DragonFly__)
737 * DragonFly's raw socket expects ip_len/ip_off in network byte
740 iph
.ip_len
= htons(iph
.ip_len
);
743 #ifdef WANT_OSPF_WRITE_FRAGMENT
744 /* XXX-MT: not thread-safe at all..
745 * XXX: this presumes this is only programme sending OSPF
747 * otherwise, no guarantee ipid will be unique
750 #endif /* WANT_OSPF_WRITE_FRAGMENT */
753 if (oi
->type
== OSPF_IFTYPE_VIRTUALLINK
)
754 iph
.ip_ttl
= OSPF_VL_IP_TTL
;
756 iph
.ip_ttl
= OSPF_IP_TTL
;
757 iph
.ip_p
= IPPROTO_OSPFIGP
;
759 iph
.ip_src
.s_addr
= oi
->address
->u
.prefix4
.s_addr
;
760 iph
.ip_dst
.s_addr
= op
->dst
.s_addr
;
762 memset(&msg
, 0, sizeof(msg
));
763 msg
.msg_name
= (caddr_t
)&sa_dst
;
764 msg
.msg_namelen
= sizeof(sa_dst
);
768 iov
[0].iov_base
= (char *)&iph
;
769 iov
[0].iov_len
= iph
.ip_hl
<< OSPF_WRITE_IPHL_SHIFT
;
770 iov
[1].iov_base
= stream_pnt(op
->s
);
771 iov
[1].iov_len
= op
->length
;
774 msg
.msg_control
= (caddr_t
)cm
;
775 cm
->cmsg_level
= SOL_IP
;
776 cm
->cmsg_type
= IP_PKTINFO
;
777 cm
->cmsg_len
= CMSG_LEN(sizeof(struct in_pktinfo
));
778 pi
= (struct in_pktinfo
*)CMSG_DATA(cm
);
779 pi
->ipi_ifindex
= oi
->ifp
->ifindex
;
781 msg
.msg_controllen
= cm
->cmsg_len
;
784 /* Sadly we can not rely on kernels to fragment packets
785 * because of either IP_HDRINCL and/or multicast
786 * destination being set.
789 #ifdef WANT_OSPF_WRITE_FRAGMENT
790 if (op
->length
> maxdatasize
)
791 ospf_write_frags(ospf
->fd
, op
, &iph
, &msg
, maxdatasize
,
792 oi
->ifp
->mtu
, flags
, type
);
793 #endif /* WANT_OSPF_WRITE_FRAGMENT */
795 /* send final fragment (could be first) */
796 sockopt_iphdrincl_swab_htosys(&iph
);
797 ret
= sendmsg(ospf
->fd
, &msg
, flags
);
798 sockopt_iphdrincl_swab_systoh(&iph
);
799 if (IS_DEBUG_OSPF_EVENT
)
802 "id %d, off %d, len %d, interface %s, mtu %u:",
803 inet_ntoa(iph
.ip_dst
), iph
.ip_id
, iph
.ip_off
,
804 iph
.ip_len
, oi
->ifp
->name
, oi
->ifp
->mtu
);
809 "*** sendmsg in ospf_write failed to %s, "
810 "id %d, off %d, len %d, interface %s, mtu %u: %s",
811 inet_ntoa(iph
.ip_dst
), iph
.ip_id
, iph
.ip_off
,
812 iph
.ip_len
, oi
->ifp
->name
, oi
->ifp
->mtu
,
813 safe_strerror(errno
));
815 /* Show debug sending packet. */
816 if (IS_DEBUG_OSPF_PACKET(type
- 1, SEND
)) {
817 if (IS_DEBUG_OSPF_PACKET(type
- 1, DETAIL
)) {
819 "-----------------------------------------------------");
820 ospf_ip_header_dump(&iph
);
821 stream_set_getp(op
->s
, 0);
822 ospf_packet_dump(op
->s
);
825 zlog_debug("%s sent to [%s] via [%s].",
826 lookup_msg(ospf_packet_type_str
, type
, NULL
),
827 inet_ntoa(op
->dst
), IF_NAME(oi
));
829 if (IS_DEBUG_OSPF_PACKET(type
- 1, DETAIL
))
831 "-----------------------------------------------------");
838 case OSPF_MSG_DB_DESC
:
841 case OSPF_MSG_LS_REQ
:
844 case OSPF_MSG_LS_UPD
:
847 case OSPF_MSG_LS_ACK
:
854 /* Now delete packet from queue. */
855 ospf_packet_delete(oi
);
857 /* Move this interface to the tail of write_q to
858 serve everyone in a round robin fashion */
859 list_delete_node(ospf
->oi_write_q
, node
);
860 if (ospf_fifo_head(oi
->obuf
) == NULL
) {
862 last_serviced_oi
= NULL
;
865 listnode_add(ospf
->oi_write_q
, oi
);
868 /* Setup to service from the head of the queue again */
869 if (!list_isempty(ospf
->oi_write_q
)) {
870 node
= listhead(ospf
->oi_write_q
);
872 oi
= listgetdata(node
);
877 /* If packets still remain in queue, call write thread. */
878 if (!list_isempty(ospf
->oi_write_q
))
879 thread_add_write(master
, ospf_write
, ospf
, ospf
->fd
,
885 /* OSPF Hello message read -- RFC2328 Section 10.5. */
886 static void ospf_hello(struct ip
*iph
, struct ospf_header
*ospfh
,
887 struct stream
*s
, struct ospf_interface
*oi
, int size
)
889 struct ospf_hello
*hello
;
890 struct ospf_neighbor
*nbr
;
894 /* increment statistics. */
897 hello
= (struct ospf_hello
*)stream_pnt(s
);
899 /* If Hello is myself, silently discard. */
900 if (IPV4_ADDR_SAME(&ospfh
->router_id
, &oi
->ospf
->router_id
)) {
901 if (IS_DEBUG_OSPF_PACKET(ospfh
->type
- 1, RECV
)) {
903 "ospf_header[%s/%s]: selforiginated, "
905 lookup_msg(ospf_packet_type_str
, ospfh
->type
,
907 inet_ntoa(iph
->ip_src
));
912 /* get neighbor prefix. */
914 p
.prefixlen
= ip_masklen(hello
->network_mask
);
915 p
.u
.prefix4
= iph
->ip_src
;
917 /* Compare network mask. */
918 /* Checking is ignored for Point-to-Point and Virtual link. */
919 if (oi
->type
!= OSPF_IFTYPE_POINTOPOINT
920 && oi
->type
!= OSPF_IFTYPE_VIRTUALLINK
)
921 if (oi
->address
->prefixlen
!= p
.prefixlen
) {
924 "Packet %s [Hello:RECV]: NetworkMask mismatch on %s (configured prefix length is %d, but hello packet indicates %d).",
925 inet_ntoa(ospfh
->router_id
), IF_NAME(oi
),
926 (int)oi
->address
->prefixlen
, (int)p
.prefixlen
);
930 /* Compare Router Dead Interval. */
931 if (OSPF_IF_PARAM(oi
, v_wait
) != ntohl(hello
->dead_interval
)) {
932 flog_warn(EC_OSPF_PACKET
,
933 "Packet %s [Hello:RECV]: RouterDeadInterval mismatch "
934 "(expected %u, but received %u).",
935 inet_ntoa(ospfh
->router_id
),
936 OSPF_IF_PARAM(oi
, v_wait
),
937 ntohl(hello
->dead_interval
));
941 /* Compare Hello Interval - ignored if fast-hellos are set. */
942 if (OSPF_IF_PARAM(oi
, fast_hello
) == 0) {
943 if (OSPF_IF_PARAM(oi
, v_hello
)
944 != ntohs(hello
->hello_interval
)) {
947 "Packet %s [Hello:RECV]: HelloInterval mismatch "
948 "(expected %u, but received %u).",
949 inet_ntoa(ospfh
->router_id
),
950 OSPF_IF_PARAM(oi
, v_hello
),
951 ntohs(hello
->hello_interval
));
956 if (IS_DEBUG_OSPF_EVENT
)
957 zlog_debug("Packet %s [Hello:RECV]: Options %s vrf %s",
958 inet_ntoa(ospfh
->router_id
),
959 ospf_options_dump(hello
->options
),
960 ospf_vrf_id_to_name(oi
->ospf
->vrf_id
));
962 /* Compare options. */
963 #define REJECT_IF_TBIT_ON 1 /* XXX */
964 #ifdef REJECT_IF_TBIT_ON
965 if (CHECK_FLAG(hello
->options
, OSPF_OPTION_MT
)) {
967 * This router does not support non-zero TOS.
968 * Drop this Hello packet not to establish neighbor
971 flog_warn(EC_OSPF_PACKET
,
972 "Packet %s [Hello:RECV]: T-bit on, drop it.",
973 inet_ntoa(ospfh
->router_id
));
976 #endif /* REJECT_IF_TBIT_ON */
978 if (CHECK_FLAG(oi
->ospf
->config
, OSPF_OPAQUE_CAPABLE
)
979 && CHECK_FLAG(hello
->options
, OSPF_OPTION_O
)) {
981 * This router does know the correct usage of O-bit
982 * the bit should be set in DD packet only.
984 flog_warn(EC_OSPF_PACKET
,
985 "Packet %s [Hello:RECV]: O-bit abuse?",
986 inet_ntoa(ospfh
->router_id
));
987 #ifdef STRICT_OBIT_USAGE_CHECK
988 return; /* Reject this packet. */
989 #else /* STRICT_OBIT_USAGE_CHECK */
990 UNSET_FLAG(hello
->options
, OSPF_OPTION_O
); /* Ignore O-bit. */
991 #endif /* STRICT_OBIT_USAGE_CHECK */
994 /* new for NSSA is to ensure that NP is on and E is off */
996 if (oi
->area
->external_routing
== OSPF_AREA_NSSA
) {
997 if (!(CHECK_FLAG(OPTIONS(oi
), OSPF_OPTION_NP
)
998 && CHECK_FLAG(hello
->options
, OSPF_OPTION_NP
)
999 && !CHECK_FLAG(OPTIONS(oi
), OSPF_OPTION_E
)
1000 && !CHECK_FLAG(hello
->options
, OSPF_OPTION_E
))) {
1003 "NSSA-Packet-%s[Hello:RECV]: my options: %x, his options %x",
1004 inet_ntoa(ospfh
->router_id
), OPTIONS(oi
),
1008 if (IS_DEBUG_OSPF_NSSA
)
1009 zlog_debug("NSSA-Hello:RECV:Packet from %s:",
1010 inet_ntoa(ospfh
->router_id
));
1012 /* The setting of the E-bit found in the Hello Packet's Options
1013 field must match this area's ExternalRoutingCapability A
1014 mismatch causes processing to stop and the packet to be
1015 dropped. The setting of the rest of the bits in the Hello
1016 Packet's Options field should be ignored. */
1017 if (CHECK_FLAG(OPTIONS(oi
), OSPF_OPTION_E
)
1018 != CHECK_FLAG(hello
->options
, OSPF_OPTION_E
)) {
1021 "Packet %s [Hello:RECV]: my options: %x, his options %x",
1022 inet_ntoa(ospfh
->router_id
), OPTIONS(oi
),
1027 /* get neighbour struct */
1028 nbr
= ospf_nbr_get(oi
, ospfh
, iph
, &p
);
1030 /* neighbour must be valid, ospf_nbr_get creates if none existed */
1033 old_state
= nbr
->state
;
1035 /* Add event to thread. */
1036 OSPF_NSM_EVENT_SCHEDULE(nbr
, NSM_PacketReceived
);
1038 /* RFC2328 Section 9.5.1
1039 If the router is not eligible to become Designated Router,
1040 (snip) It must also send an Hello Packet in reply to an
1041 Hello Packet received from any eligible neighbor (other than
1042 the current Designated Router and Backup Designated Router). */
1043 if (oi
->type
== OSPF_IFTYPE_NBMA
)
1044 if (PRIORITY(oi
) == 0 && hello
->priority
> 0
1045 && IPV4_ADDR_CMP(&DR(oi
), &iph
->ip_src
)
1046 && IPV4_ADDR_CMP(&BDR(oi
), &iph
->ip_src
))
1047 OSPF_NSM_TIMER_ON(nbr
->t_hello_reply
,
1048 ospf_hello_reply_timer
,
1049 OSPF_HELLO_REPLY_DELAY
);
1051 /* on NBMA network type, it happens to receive bidirectional Hello
1053 without advance 1-Way Received event.
1054 To avoid incorrect DR-seletion, raise 1-Way Received event.*/
1055 if (oi
->type
== OSPF_IFTYPE_NBMA
1056 && (old_state
== NSM_Down
|| old_state
== NSM_Attempt
)) {
1057 OSPF_NSM_EVENT_SCHEDULE(nbr
, NSM_OneWayReceived
);
1058 nbr
->priority
= hello
->priority
;
1059 nbr
->d_router
= hello
->d_router
;
1060 nbr
->bd_router
= hello
->bd_router
;
1064 if (ospf_nbr_bidirectional(&oi
->ospf
->router_id
, hello
->neighbors
,
1065 size
- OSPF_HELLO_MIN_SIZE
)) {
1066 OSPF_NSM_EVENT_SCHEDULE(nbr
, NSM_TwoWayReceived
);
1067 nbr
->options
|= hello
->options
;
1069 OSPF_NSM_EVENT_SCHEDULE(nbr
, NSM_OneWayReceived
);
1070 /* Set neighbor information. */
1071 nbr
->priority
= hello
->priority
;
1072 nbr
->d_router
= hello
->d_router
;
1073 nbr
->bd_router
= hello
->bd_router
;
1077 /* If neighbor itself declares DR and no BDR exists,
1078 cause event BackupSeen */
1079 if (IPV4_ADDR_SAME(&nbr
->address
.u
.prefix4
, &hello
->d_router
))
1080 if (hello
->bd_router
.s_addr
== 0 && oi
->state
== ISM_Waiting
)
1081 OSPF_ISM_EVENT_SCHEDULE(oi
, ISM_BackupSeen
);
1083 /* neighbor itself declares BDR. */
1084 if (oi
->state
== ISM_Waiting
1085 && IPV4_ADDR_SAME(&nbr
->address
.u
.prefix4
, &hello
->bd_router
))
1086 OSPF_ISM_EVENT_SCHEDULE(oi
, ISM_BackupSeen
);
1088 /* had not previously. */
1089 if ((IPV4_ADDR_SAME(&nbr
->address
.u
.prefix4
, &hello
->d_router
)
1090 && IPV4_ADDR_CMP(&nbr
->address
.u
.prefix4
, &nbr
->d_router
))
1091 || (IPV4_ADDR_CMP(&nbr
->address
.u
.prefix4
, &hello
->d_router
)
1092 && IPV4_ADDR_SAME(&nbr
->address
.u
.prefix4
, &nbr
->d_router
)))
1093 OSPF_ISM_EVENT_SCHEDULE(oi
, ISM_NeighborChange
);
1095 /* had not previously. */
1096 if ((IPV4_ADDR_SAME(&nbr
->address
.u
.prefix4
, &hello
->bd_router
)
1097 && IPV4_ADDR_CMP(&nbr
->address
.u
.prefix4
, &nbr
->bd_router
))
1098 || (IPV4_ADDR_CMP(&nbr
->address
.u
.prefix4
, &hello
->bd_router
)
1099 && IPV4_ADDR_SAME(&nbr
->address
.u
.prefix4
, &nbr
->bd_router
)))
1100 OSPF_ISM_EVENT_SCHEDULE(oi
, ISM_NeighborChange
);
1102 /* Neighbor priority check. */
1103 if (nbr
->priority
>= 0 && nbr
->priority
!= hello
->priority
)
1104 OSPF_ISM_EVENT_SCHEDULE(oi
, ISM_NeighborChange
);
1106 /* Set neighbor information. */
1107 nbr
->priority
= hello
->priority
;
1108 nbr
->d_router
= hello
->d_router
;
1109 nbr
->bd_router
= hello
->bd_router
;
1112 /* Save DD flags/options/Seqnum received. */
1113 static void ospf_db_desc_save_current(struct ospf_neighbor
*nbr
,
1114 struct ospf_db_desc
*dd
)
1116 nbr
->last_recv
.flags
= dd
->flags
;
1117 nbr
->last_recv
.options
= dd
->options
;
1118 nbr
->last_recv
.dd_seqnum
= ntohl(dd
->dd_seqnum
);
1121 /* Process rest of DD packet. */
1122 static void ospf_db_desc_proc(struct stream
*s
, struct ospf_interface
*oi
,
1123 struct ospf_neighbor
*nbr
,
1124 struct ospf_db_desc
*dd
, uint16_t size
)
1126 struct ospf_lsa
*new, *find
;
1127 struct lsa_header
*lsah
;
1129 stream_forward_getp(s
, OSPF_DB_DESC_MIN_SIZE
);
1130 for (size
-= OSPF_DB_DESC_MIN_SIZE
; size
>= OSPF_LSA_HEADER_SIZE
;
1131 size
-= OSPF_LSA_HEADER_SIZE
) {
1132 lsah
= (struct lsa_header
*)stream_pnt(s
);
1133 stream_forward_getp(s
, OSPF_LSA_HEADER_SIZE
);
1135 /* Unknown LS type. */
1136 if (lsah
->type
< OSPF_MIN_LSA
|| lsah
->type
>= OSPF_MAX_LSA
) {
1137 flog_warn(EC_OSPF_PACKET
,
1138 "Packet [DD:RECV]: Unknown LS type %d.",
1140 OSPF_NSM_EVENT_SCHEDULE(nbr
, NSM_SeqNumberMismatch
);
1144 if (IS_OPAQUE_LSA(lsah
->type
)
1145 && !CHECK_FLAG(nbr
->options
, OSPF_OPTION_O
)) {
1146 flog_warn(EC_OSPF_PACKET
,
1147 "LSA[Type%d:%s]: Opaque capability mismatch?",
1148 lsah
->type
, inet_ntoa(lsah
->id
));
1149 OSPF_NSM_EVENT_SCHEDULE(nbr
, NSM_SeqNumberMismatch
);
1153 switch (lsah
->type
) {
1154 case OSPF_AS_EXTERNAL_LSA
:
1155 case OSPF_OPAQUE_AS_LSA
:
1156 /* Check for stub area. Reject if AS-External from stub
1158 allow if from NSSA. */
1159 if (oi
->area
->external_routing
== OSPF_AREA_STUB
) {
1162 "Packet [DD:RECV]: LSA[Type%d:%s] from %s area.",
1163 lsah
->type
, inet_ntoa(lsah
->id
),
1164 (oi
->area
->external_routing
1168 OSPF_NSM_EVENT_SCHEDULE(nbr
,
1169 NSM_SeqNumberMismatch
);
1177 /* Create LS-request object. */
1178 new = ospf_ls_request_new(lsah
);
1180 /* Lookup received LSA, then add LS request list. */
1181 find
= ospf_lsa_lookup_by_header(oi
->area
, lsah
);
1183 /* ospf_lsa_more_recent is fine with NULL pointers */
1184 switch (ospf_lsa_more_recent(find
, new)) {
1186 /* Neighbour has a more recent LSA, we must request it
1188 ospf_ls_request_add(nbr
, new);
1191 /* If we have a copy of this LSA, it's either less
1193 * and we're requesting it from neighbour (the case
1195 * it's as recent and we both have same copy (this
1198 * In neither of these two cases is there any point in
1199 * describing our copy of the LSA to the neighbour in a
1200 * DB-Summary packet, if we're still intending to do so.
1202 * See: draft-ogier-ospf-dbex-opt-00.txt, describing the
1203 * backward compatible optimisation to OSPF DB Exchange
1205 * DB Description process implemented here.
1208 ospf_lsdb_delete(&nbr
->db_sum
, find
);
1209 ospf_lsa_discard(new);
1212 /* We have the more recent copy, nothing specific to do:
1213 * - no need to request neighbours stale copy
1214 * - must leave DB summary list copy alone
1216 if (IS_DEBUG_OSPF_EVENT
)
1218 "Packet [DD:RECV]: LSA received Type %d, "
1219 "ID %s is not recent.",
1220 lsah
->type
, inet_ntoa(lsah
->id
));
1221 ospf_lsa_discard(new);
1226 if (IS_SET_DD_MS(nbr
->dd_flags
)) {
1229 /* Both sides have no More, then we're done with Exchange */
1230 if (!IS_SET_DD_M(dd
->flags
) && !IS_SET_DD_M(nbr
->dd_flags
))
1231 OSPF_NSM_EVENT_SCHEDULE(nbr
, NSM_ExchangeDone
);
1233 ospf_db_desc_send(nbr
);
1237 nbr
->dd_seqnum
= ntohl(dd
->dd_seqnum
);
1239 /* Send DD packet in reply.
1241 * Must be done to acknowledge the Master's DD, regardless of
1242 * whether we have more LSAs ourselves to describe.
1244 * This function will clear the 'More' bit, if after this DD
1245 * we have no more LSAs to describe to the master..
1247 ospf_db_desc_send(nbr
);
1249 /* Slave can raise ExchangeDone now, if master is also done */
1250 if (!IS_SET_DD_M(dd
->flags
) && !IS_SET_DD_M(nbr
->dd_flags
))
1251 OSPF_NSM_EVENT_SCHEDULE(nbr
, NSM_ExchangeDone
);
1254 /* Save received neighbor values from DD. */
1255 ospf_db_desc_save_current(nbr
, dd
);
1258 ospf_ls_req_send(nbr
);
1261 static int ospf_db_desc_is_dup(struct ospf_db_desc
*dd
,
1262 struct ospf_neighbor
*nbr
)
1264 /* Is DD duplicated? */
1265 if (dd
->options
== nbr
->last_recv
.options
1266 && dd
->flags
== nbr
->last_recv
.flags
1267 && dd
->dd_seqnum
== htonl(nbr
->last_recv
.dd_seqnum
))
1273 /* OSPF Database Description message read -- RFC2328 Section 10.6. */
1274 static void ospf_db_desc(struct ip
*iph
, struct ospf_header
*ospfh
,
1275 struct stream
*s
, struct ospf_interface
*oi
,
1278 struct ospf_db_desc
*dd
;
1279 struct ospf_neighbor
*nbr
;
1281 /* Increment statistics. */
1284 dd
= (struct ospf_db_desc
*)stream_pnt(s
);
1286 nbr
= ospf_nbr_lookup(oi
, iph
, ospfh
);
1288 flog_warn(EC_OSPF_PACKET
, "Packet[DD]: Unknown Neighbor %s",
1289 inet_ntoa(ospfh
->router_id
));
1294 if ((OSPF_IF_PARAM(oi
, mtu_ignore
) == 0)
1295 && (ntohs(dd
->mtu
) > oi
->ifp
->mtu
)) {
1298 "Packet[DD]: Neighbor %s MTU %u is larger than [%s]'s MTU %u",
1299 inet_ntoa(nbr
->router_id
), ntohs(dd
->mtu
), IF_NAME(oi
),
1305 * XXX HACK by Hasso Tepper. Setting N/P bit in NSSA area DD packets is
1307 * required. In fact at least JunOS sends DD packets with P bit clear.
1308 * Until proper solution is developped, this hack should help.
1310 * Update: According to the RFCs, N bit is specified /only/ for Hello
1311 * options, unfortunately its use in DD options is not specified. Hence
1313 * implementations follow E-bit semantics and set it in DD options, and
1315 * treat it as unspecified and hence follow the directive "default for
1316 * options is clear", ie unset.
1318 * Reset the flag, as ospfd follows E-bit semantics.
1320 if ((oi
->area
->external_routing
== OSPF_AREA_NSSA
)
1321 && (CHECK_FLAG(nbr
->options
, OSPF_OPTION_NP
))
1322 && (!CHECK_FLAG(dd
->options
, OSPF_OPTION_NP
))) {
1323 if (IS_DEBUG_OSPF_EVENT
)
1325 "Packet[DD]: Neighbour %s: Has NSSA capability, sends with N bit clear in DD options",
1326 inet_ntoa(nbr
->router_id
));
1327 SET_FLAG(dd
->options
, OSPF_OPTION_NP
);
1330 #ifdef REJECT_IF_TBIT_ON
1331 if (CHECK_FLAG(dd
->options
, OSPF_OPTION_MT
)) {
1333 * In Hello protocol, optional capability must have checked
1334 * to prevent this T-bit enabled router be my neighbor.
1336 flog_warn(EC_OSPF_PACKET
, "Packet[DD]: Neighbor %s: T-bit on?",
1337 inet_ntoa(nbr
->router_id
));
1340 #endif /* REJECT_IF_TBIT_ON */
1342 if (CHECK_FLAG(dd
->options
, OSPF_OPTION_O
)
1343 && !CHECK_FLAG(oi
->ospf
->config
, OSPF_OPAQUE_CAPABLE
)) {
1345 * This node is not configured to handle O-bit, for now.
1346 * Clear it to ignore unsupported capability proposed by
1349 UNSET_FLAG(dd
->options
, OSPF_OPTION_O
);
1352 /* Add event to thread. */
1353 OSPF_NSM_EVENT_SCHEDULE(nbr
, NSM_PacketReceived
);
1355 /* Process DD packet by neighbor status. */
1356 switch (nbr
->state
) {
1362 "Packet[DD]: Neighbor %s state is %s, packet discarded.",
1363 inet_ntoa(nbr
->router_id
),
1364 lookup_msg(ospf_nsm_state_msg
, nbr
->state
, NULL
));
1367 OSPF_NSM_EVENT_EXECUTE(nbr
, NSM_TwoWayReceived
);
1368 /* If the new state is ExStart, the processing of the current
1369 packet should then continue in this new state by falling
1370 through to case ExStart below. */
1371 if (nbr
->state
!= NSM_ExStart
)
1376 if ((IS_SET_DD_ALL(dd
->flags
) == OSPF_DD_FLAG_ALL
)
1377 && (size
== OSPF_DB_DESC_MIN_SIZE
)) {
1378 if (IPV4_ADDR_CMP(&nbr
->router_id
, &oi
->ospf
->router_id
)
1380 /* We're Slave---obey */
1381 if (CHECK_FLAG(oi
->ospf
->config
,
1382 OSPF_LOG_ADJACENCY_DETAIL
))
1384 "Packet[DD]: Neighbor %s Negotiation done (Slave).",
1385 inet_ntoa(nbr
->router_id
));
1387 nbr
->dd_seqnum
= ntohl(dd
->dd_seqnum
);
1390 UNSET_FLAG(nbr
->dd_flags
,
1391 (OSPF_DD_FLAG_MS
| OSPF_DD_FLAG_I
));
1393 /* We're Master, ignore the initial DBD from
1395 if (CHECK_FLAG(oi
->ospf
->config
,
1396 OSPF_LOG_ADJACENCY_DETAIL
))
1398 "Packet[DD]: Neighbor %s: Initial DBD from Slave, "
1400 inet_ntoa(nbr
->router_id
));
1404 /* Ack from the Slave */
1405 else if (!IS_SET_DD_MS(dd
->flags
) && !IS_SET_DD_I(dd
->flags
)
1406 && ntohl(dd
->dd_seqnum
) == nbr
->dd_seqnum
1407 && IPV4_ADDR_CMP(&nbr
->router_id
, &oi
->ospf
->router_id
)
1410 "Packet[DD]: Neighbor %s Negotiation done (Master).",
1411 inet_ntoa(nbr
->router_id
));
1412 /* Reset I, leaving MS */
1413 UNSET_FLAG(nbr
->dd_flags
, OSPF_DD_FLAG_I
);
1415 flog_warn(EC_OSPF_PACKET
,
1416 "Packet[DD]: Neighbor %s Negotiation fails.",
1417 inet_ntoa(nbr
->router_id
));
1421 /* This is where the real Options are saved */
1422 nbr
->options
= dd
->options
;
1424 if (CHECK_FLAG(oi
->ospf
->config
, OSPF_OPAQUE_CAPABLE
)) {
1425 if (IS_DEBUG_OSPF_EVENT
)
1427 "Neighbor[%s] is %sOpaque-capable.",
1428 inet_ntoa(nbr
->router_id
),
1429 CHECK_FLAG(nbr
->options
, OSPF_OPTION_O
)
1433 if (!CHECK_FLAG(nbr
->options
, OSPF_OPTION_O
)
1434 && IPV4_ADDR_SAME(&DR(oi
),
1435 &nbr
->address
.u
.prefix4
)) {
1438 "DR-neighbor[%s] is NOT opaque-capable; Opaque-LSAs cannot be reliably advertised in this network.",
1439 inet_ntoa(nbr
->router_id
));
1440 /* This situation is undesirable, but not a real
1445 OSPF_NSM_EVENT_EXECUTE(nbr
, NSM_NegotiationDone
);
1447 /* continue processing rest of packet. */
1448 ospf_db_desc_proc(s
, oi
, nbr
, dd
, size
);
1451 if (ospf_db_desc_is_dup(dd
, nbr
)) {
1452 if (IS_SET_DD_MS(nbr
->dd_flags
))
1453 /* Master: discard duplicated DD packet. */
1455 "Packet[DD] (Master): Neighbor %s packet duplicated.",
1456 inet_ntoa(nbr
->router_id
));
1458 /* Slave: cause to retransmit the last Database
1462 "Packet[DD] [Slave]: Neighbor %s packet duplicated.",
1463 inet_ntoa(nbr
->router_id
));
1464 ospf_db_desc_resend(nbr
);
1469 /* Otherwise DD packet should be checked. */
1470 /* Check Master/Slave bit mismatch */
1471 if (IS_SET_DD_MS(dd
->flags
)
1472 != IS_SET_DD_MS(nbr
->last_recv
.flags
)) {
1473 flog_warn(EC_OSPF_PACKET
,
1474 "Packet[DD]: Neighbor %s MS-bit mismatch.",
1475 inet_ntoa(nbr
->router_id
));
1476 OSPF_NSM_EVENT_SCHEDULE(nbr
, NSM_SeqNumberMismatch
);
1477 if (IS_DEBUG_OSPF_EVENT
)
1479 "Packet[DD]: dd->flags=%d, nbr->dd_flags=%d",
1480 dd
->flags
, nbr
->dd_flags
);
1484 /* Check initialize bit is set. */
1485 if (IS_SET_DD_I(dd
->flags
)) {
1486 zlog_info("Packet[DD]: Neighbor %s I-bit set.",
1487 inet_ntoa(nbr
->router_id
));
1488 OSPF_NSM_EVENT_SCHEDULE(nbr
, NSM_SeqNumberMismatch
);
1492 /* Check DD Options. */
1493 if (dd
->options
!= nbr
->options
) {
1494 #ifdef ORIGINAL_CODING
1495 /* Save the new options for debugging */
1496 nbr
->options
= dd
->options
;
1497 #endif /* ORIGINAL_CODING */
1498 flog_warn(EC_OSPF_PACKET
,
1499 "Packet[DD]: Neighbor %s options mismatch.",
1500 inet_ntoa(nbr
->router_id
));
1501 OSPF_NSM_EVENT_SCHEDULE(nbr
, NSM_SeqNumberMismatch
);
1505 /* Check DD sequence number. */
1506 if ((IS_SET_DD_MS(nbr
->dd_flags
)
1507 && ntohl(dd
->dd_seqnum
) != nbr
->dd_seqnum
)
1508 || (!IS_SET_DD_MS(nbr
->dd_flags
)
1509 && ntohl(dd
->dd_seqnum
) != nbr
->dd_seqnum
+ 1)) {
1512 "Packet[DD]: Neighbor %s sequence number mismatch.",
1513 inet_ntoa(nbr
->router_id
));
1514 OSPF_NSM_EVENT_SCHEDULE(nbr
, NSM_SeqNumberMismatch
);
1518 /* Continue processing rest of packet. */
1519 ospf_db_desc_proc(s
, oi
, nbr
, dd
, size
);
1523 if (ospf_db_desc_is_dup(dd
, nbr
)) {
1524 if (IS_SET_DD_MS(nbr
->dd_flags
)) {
1525 /* Master should discard duplicate DD packet. */
1527 "Packet[DD]: Neighbor %s duplicated, "
1528 "packet discarded.",
1529 inet_ntoa(nbr
->router_id
));
1532 if (monotime_since(&nbr
->last_send_ts
, NULL
)
1533 < nbr
->v_inactivity
* 1000000LL) {
1534 /* In states Loading and Full the slave
1536 its last Database Description packet
1538 duplicate Database Description
1540 from the master. For this reason the
1542 wait RouterDeadInterval seconds
1544 last Database Description packet.
1546 Database Description packet from the
1548 this interval will generate a
1550 neighbor event. RFC2328 Section 10.8
1552 ospf_db_desc_resend(nbr
);
1558 OSPF_NSM_EVENT_SCHEDULE(nbr
, NSM_SeqNumberMismatch
);
1561 flog_warn(EC_OSPF_PACKET
,
1562 "Packet[DD]: Neighbor %s NSM illegal status %u.",
1563 inet_ntoa(nbr
->router_id
), nbr
->state
);
1568 #define OSPF_LSA_KEY_SIZE 12 /* type(4) + id(4) + ar(4) */
1570 /* OSPF Link State Request Read -- RFC2328 Section 10.7. */
1571 static void ospf_ls_req(struct ip
*iph
, struct ospf_header
*ospfh
,
1572 struct stream
*s
, struct ospf_interface
*oi
,
1575 struct ospf_neighbor
*nbr
;
1577 struct in_addr ls_id
;
1578 struct in_addr adv_router
;
1579 struct ospf_lsa
*find
;
1580 struct list
*ls_upd
;
1581 unsigned int length
;
1583 /* Increment statistics. */
1586 nbr
= ospf_nbr_lookup(oi
, iph
, ospfh
);
1588 flog_warn(EC_OSPF_PACKET
,
1589 "Link State Request: Unknown Neighbor %s.",
1590 inet_ntoa(ospfh
->router_id
));
1594 /* Add event to thread. */
1595 OSPF_NSM_EVENT_SCHEDULE(nbr
, NSM_PacketReceived
);
1597 /* Neighbor State should be Exchange or later. */
1598 if (nbr
->state
!= NSM_Exchange
&& nbr
->state
!= NSM_Loading
1599 && nbr
->state
!= NSM_Full
) {
1602 "Link State Request received from %s: Neighbor state is %s, packet discarded.",
1603 inet_ntoa(ospfh
->router_id
),
1604 lookup_msg(ospf_nsm_state_msg
, nbr
->state
, NULL
));
1608 /* Send Link State Update for ALL requested LSAs. */
1609 ls_upd
= list_new();
1610 length
= OSPF_HEADER_SIZE
+ OSPF_LS_UPD_MIN_SIZE
;
1612 while (size
>= OSPF_LSA_KEY_SIZE
) {
1613 /* Get one slice of Link State Request. */
1614 ls_type
= stream_getl(s
);
1615 ls_id
.s_addr
= stream_get_ipv4(s
);
1616 adv_router
.s_addr
= stream_get_ipv4(s
);
1618 /* Verify LSA type. */
1619 if (ls_type
< OSPF_MIN_LSA
|| ls_type
>= OSPF_MAX_LSA
) {
1620 OSPF_NSM_EVENT_SCHEDULE(nbr
, NSM_BadLSReq
);
1621 list_delete(&ls_upd
);
1625 /* Search proper LSA in LSDB. */
1626 find
= ospf_lsa_lookup(oi
->ospf
, oi
->area
, ls_type
, ls_id
,
1629 OSPF_NSM_EVENT_SCHEDULE(nbr
, NSM_BadLSReq
);
1630 list_delete(&ls_upd
);
1634 /* Packet overflows MTU size, send immediately. */
1635 if (length
+ ntohs(find
->data
->length
) > ospf_packet_max(oi
)) {
1636 if (oi
->type
== OSPF_IFTYPE_NBMA
)
1637 ospf_ls_upd_send(nbr
, ls_upd
,
1638 OSPF_SEND_PACKET_DIRECT
, 0);
1640 ospf_ls_upd_send(nbr
, ls_upd
,
1641 OSPF_SEND_PACKET_INDIRECT
, 0);
1643 /* Only remove list contents. Keep ls_upd. */
1644 list_delete_all_node(ls_upd
);
1646 length
= OSPF_HEADER_SIZE
+ OSPF_LS_UPD_MIN_SIZE
;
1649 /* Append LSA to update list. */
1650 listnode_add(ls_upd
, find
);
1651 length
+= ntohs(find
->data
->length
);
1653 size
-= OSPF_LSA_KEY_SIZE
;
1656 /* Send rest of Link State Update. */
1657 if (listcount(ls_upd
) > 0) {
1658 if (oi
->type
== OSPF_IFTYPE_NBMA
)
1659 ospf_ls_upd_send(nbr
, ls_upd
, OSPF_SEND_PACKET_DIRECT
,
1662 ospf_ls_upd_send(nbr
, ls_upd
, OSPF_SEND_PACKET_INDIRECT
,
1665 list_delete(&ls_upd
);
1667 list_delete(&ls_upd
);
1670 /* Get the list of LSAs from Link State Update packet.
1671 And process some validation -- RFC2328 Section 13. (1)-(2). */
1672 static struct list
*ospf_ls_upd_list_lsa(struct ospf_neighbor
*nbr
,
1674 struct ospf_interface
*oi
, size_t size
)
1676 uint16_t count
, sum
;
1678 struct lsa_header
*lsah
;
1679 struct ospf_lsa
*lsa
;
1684 count
= stream_getl(s
);
1685 size
-= OSPF_LS_UPD_MIN_SIZE
; /* # LSAs */
1687 for (; size
>= OSPF_LSA_HEADER_SIZE
&& count
> 0;
1688 size
-= length
, stream_forward_getp(s
, length
), count
--) {
1689 lsah
= (struct lsa_header
*)stream_pnt(s
);
1690 length
= ntohs(lsah
->length
);
1692 if (length
> size
) {
1695 "Link State Update: LSA length exceeds packet size.");
1699 /* Validate the LSA's LS checksum. */
1700 sum
= lsah
->checksum
;
1701 if (!ospf_lsa_checksum_valid(lsah
)) {
1702 /* (bug #685) more details in a one-line message make it
1704 * to identify problem source on the one hand and to
1706 * chance to compress repeated messages in syslog on the
1710 "Link State Update: LSA checksum error %x/%x, ID=%s from: nbr %s, router ID %s, adv router %s",
1711 sum
, lsah
->checksum
, inet_ntoa(lsah
->id
),
1712 inet_ntoa(nbr
->src
), inet_ntoa(nbr
->router_id
),
1713 inet_ntoa(lsah
->adv_router
));
1717 /* Examine the LSA's LS type. */
1718 if (lsah
->type
< OSPF_MIN_LSA
|| lsah
->type
>= OSPF_MAX_LSA
) {
1719 flog_warn(EC_OSPF_PACKET
,
1720 "Link State Update: Unknown LS type %d",
1726 * What if the received LSA's age is greater than MaxAge?
1727 * Treat it as a MaxAge case -- endo.
1729 if (ntohs(lsah
->ls_age
) > OSPF_LSA_MAXAGE
)
1730 lsah
->ls_age
= htons(OSPF_LSA_MAXAGE
);
1732 if (CHECK_FLAG(nbr
->options
, OSPF_OPTION_O
)) {
1733 #ifdef STRICT_OBIT_USAGE_CHECK
1734 if ((IS_OPAQUE_LSA(lsah
->type
)
1735 && !CHECK_FLAG(lsah
->options
, OSPF_OPTION_O
))
1736 || (!IS_OPAQUE_LSA(lsah
->type
)
1737 && CHECK_FLAG(lsah
->options
, OSPF_OPTION_O
))) {
1739 * This neighbor must know the exact usage of
1741 * the bit will be set in Type-9,10,11 LSAs
1744 flog_warn(EC_OSPF_PACKET
,
1745 "LSA[Type%d:%s]: O-bit abuse?",
1746 lsah
->type
, inet_ntoa(lsah
->id
));
1749 #endif /* STRICT_OBIT_USAGE_CHECK */
1751 /* Do not take in AS External Opaque-LSAs if we are a
1753 if (lsah
->type
== OSPF_OPAQUE_AS_LSA
1754 && nbr
->oi
->area
->external_routing
1755 != OSPF_AREA_DEFAULT
) {
1756 if (IS_DEBUG_OSPF_EVENT
)
1758 "LSA[Type%d:%s]: We are a stub, don't take this LSA.",
1760 inet_ntoa(lsah
->id
));
1763 } else if (IS_OPAQUE_LSA(lsah
->type
)) {
1764 flog_warn(EC_OSPF_PACKET
,
1765 "LSA[Type%d:%s]: Opaque capability mismatch?",
1766 lsah
->type
, inet_ntoa(lsah
->id
));
1770 /* Create OSPF LSA instance. */
1771 lsa
= ospf_lsa_new_and_data(length
);
1773 lsa
->vrf_id
= oi
->ospf
->vrf_id
;
1774 /* We may wish to put some error checking if type NSSA comes in
1775 and area not in NSSA mode */
1776 switch (lsah
->type
) {
1777 case OSPF_AS_EXTERNAL_LSA
:
1778 case OSPF_OPAQUE_AS_LSA
:
1781 case OSPF_OPAQUE_LINK_LSA
:
1782 lsa
->oi
= oi
; /* Remember incoming interface for
1783 flooding control. */
1786 lsa
->area
= oi
->area
;
1790 memcpy(lsa
->data
, lsah
, length
);
1792 if (IS_DEBUG_OSPF_EVENT
)
1794 "LSA[Type%d:%s]: %p new LSA created with Link State Update",
1795 lsa
->data
->type
, inet_ntoa(lsa
->data
->id
),
1797 listnode_add(lsas
, lsa
);
1803 /* Cleanup Update list. */
1804 static void ospf_upd_list_clean(struct list
*lsas
)
1806 struct listnode
*node
, *nnode
;
1807 struct ospf_lsa
*lsa
;
1809 for (ALL_LIST_ELEMENTS(lsas
, node
, nnode
, lsa
))
1810 ospf_lsa_discard(lsa
);
1815 /* OSPF Link State Update message read -- RFC2328 Section 13. */
1816 static void ospf_ls_upd(struct ospf
*ospf
, struct ip
*iph
,
1817 struct ospf_header
*ospfh
, struct stream
*s
,
1818 struct ospf_interface
*oi
, uint16_t size
)
1820 struct ospf_neighbor
*nbr
;
1822 struct listnode
*node
, *nnode
;
1823 struct ospf_lsa
*lsa
= NULL
;
1824 /* unsigned long ls_req_found = 0; */
1826 /* Dis-assemble the stream, update each entry, re-encapsulate for
1829 /* Increment statistics. */
1832 /* Check neighbor. */
1833 nbr
= ospf_nbr_lookup(oi
, iph
, ospfh
);
1835 flog_warn(EC_OSPF_PACKET
,
1836 "Link State Update: Unknown Neighbor %s on int: %s",
1837 inet_ntoa(ospfh
->router_id
), IF_NAME(oi
));
1841 /* Add event to thread. */
1842 OSPF_NSM_EVENT_SCHEDULE(nbr
, NSM_PacketReceived
);
1844 /* Check neighbor state. */
1845 if (nbr
->state
< NSM_Exchange
) {
1846 if (IS_DEBUG_OSPF(nsm
, NSM_EVENTS
))
1848 "Link State Update: "
1849 "Neighbor[%s] state %s is less than Exchange",
1850 inet_ntoa(ospfh
->router_id
),
1851 lookup_msg(ospf_nsm_state_msg
, nbr
->state
,
1856 /* Get list of LSAs from Link State Update packet. - Also perorms Stages
1857 * 1 (validate LSA checksum) and 2 (check for LSA consistent type)
1860 lsas
= ospf_ls_upd_list_lsa(nbr
, s
, oi
, size
);
1864 #define DISCARD_LSA(L, N) \
1866 if (IS_DEBUG_OSPF_EVENT) \
1868 "ospf_lsa_discard() in ospf_ls_upd() point %d: lsa %p" \
1870 N, (void *)lsa, (int)lsa->data->type); \
1871 ospf_lsa_discard(L); \
1875 /* Process each LSA received in the one packet.
1877 * Numbers in parentheses, e.g. (1), (2), etc., and the corresponding
1878 * text below are from the steps in RFC 2328, Section 13.
1880 for (ALL_LIST_ELEMENTS(lsas
, node
, nnode
, lsa
)) {
1881 struct ospf_lsa
*ls_ret
, *current
;
1884 if (IS_DEBUG_OSPF_NSSA
) {
1885 char buf1
[INET_ADDRSTRLEN
];
1886 char buf2
[INET_ADDRSTRLEN
];
1887 char buf3
[INET_ADDRSTRLEN
];
1889 zlog_debug("LSA Type-%d from %s, ID: %s, ADV: %s",
1891 inet_ntop(AF_INET
, &ospfh
->router_id
, buf1
,
1893 inet_ntop(AF_INET
, &lsa
->data
->id
, buf2
,
1895 inet_ntop(AF_INET
, &lsa
->data
->adv_router
,
1896 buf3
, INET_ADDRSTRLEN
));
1899 listnode_delete(lsas
,
1900 lsa
); /* We don't need it in list anymore */
1902 /* (1) Validate Checksum - Done above by ospf_ls_upd_list_lsa()
1905 /* (2) LSA Type - Done above by ospf_ls_upd_list_lsa() */
1907 /* (3) Do not take in AS External LSAs if we are a stub or NSSA.
1910 /* Do not take in AS NSSA if this neighbor and we are not NSSA
1913 /* Do take in Type-7's if we are an NSSA */
1915 /* If we are also an ABR, later translate them to a Type-5
1918 /* Later, an NSSA Re-fresh can Re-fresh Type-7's and an ABR will
1919 translate them to a separate Type-5 packet. */
1921 if (lsa
->data
->type
== OSPF_AS_EXTERNAL_LSA
)
1922 /* Reject from STUB or NSSA */
1923 if (nbr
->oi
->area
->external_routing
1924 != OSPF_AREA_DEFAULT
) {
1925 if (IS_DEBUG_OSPF_NSSA
)
1927 "Incoming External LSA Discarded: We are NSSA/STUB Area");
1928 DISCARD_LSA(lsa
, 1);
1931 if (lsa
->data
->type
== OSPF_AS_NSSA_LSA
)
1932 if (nbr
->oi
->area
->external_routing
!= OSPF_AREA_NSSA
) {
1933 if (IS_DEBUG_OSPF_NSSA
)
1935 "Incoming NSSA LSA Discarded: Not NSSA Area");
1936 DISCARD_LSA(lsa
, 2);
1939 /* VU229804: Router-LSA Adv-ID must be equal to LS-ID */
1940 if (lsa
->data
->type
== OSPF_ROUTER_LSA
)
1941 if (!IPV4_ADDR_SAME(&lsa
->data
->id
,
1942 &lsa
->data
->adv_router
)) {
1943 char buf1
[INET_ADDRSTRLEN
];
1944 char buf2
[INET_ADDRSTRLEN
];
1945 char buf3
[INET_ADDRSTRLEN
];
1947 flog_err(EC_OSPF_ROUTER_LSA_MISMATCH
,
1948 "Incoming Router-LSA from %s with "
1949 "Adv-ID[%s] != LS-ID[%s]",
1950 inet_ntop(AF_INET
, &ospfh
->router_id
,
1951 buf1
, INET_ADDRSTRLEN
),
1952 inet_ntop(AF_INET
, &lsa
->data
->id
,
1953 buf2
, INET_ADDRSTRLEN
),
1955 &lsa
->data
->adv_router
, buf3
,
1958 EC_OSPF_DOMAIN_CORRUPT
,
1959 "OSPF domain compromised by attack or corruption. "
1960 "Verify correct operation of -ALL- OSPF routers.");
1961 DISCARD_LSA(lsa
, 0);
1964 /* Find the LSA in the current database. */
1966 current
= ospf_lsa_lookup_by_header(oi
->area
, lsa
->data
);
1968 /* (4) If the LSA's LS age is equal to MaxAge, and there is
1970 no instance of the LSA in the router's link state database,
1971 and none of router's neighbors are in states Exchange or
1973 then take the following actions: */
1975 if (IS_LSA_MAXAGE(lsa
) && !current
1976 && ospf_check_nbr_status(oi
->ospf
)) {
1977 /* (4a) Response Link State Acknowledgment. */
1978 ospf_ls_ack_send(nbr
, lsa
);
1980 /* (4b) Discard LSA. */
1981 if (IS_DEBUG_OSPF(lsa
, LSA
)) {
1983 "Link State Update[%s]: LS age is equal to MaxAge.",
1986 DISCARD_LSA(lsa
, 3);
1989 if (IS_OPAQUE_LSA(lsa
->data
->type
)
1990 && IPV4_ADDR_SAME(&lsa
->data
->adv_router
,
1991 &oi
->ospf
->router_id
)) {
1993 * Even if initial flushing seems to be completed, there
1995 * be a case that self-originated LSA with MaxAge still
1997 * in the routing domain.
1998 * Just send an LSAck message to cease retransmission.
2000 if (IS_LSA_MAXAGE(lsa
)) {
2001 zlog_info("LSA[%s]: Boomerang effect?",
2003 ospf_ls_ack_send(nbr
, lsa
);
2004 ospf_lsa_discard(lsa
);
2006 if (current
!= NULL
&& !IS_LSA_MAXAGE(current
))
2007 ospf_opaque_lsa_refresh_schedule(
2013 * If an instance of self-originated Opaque-LSA is not
2015 * in the LSDB, there are some possible cases here.
2017 * 1) This node lost opaque-capability after restart.
2018 * 2) Else, a part of opaque-type is no more supported.
2019 * 3) Else, a part of opaque-id is no more supported.
2021 * Anyway, it is still this node's responsibility to
2023 * Otherwise, the LSA instance remains in the routing
2025 * until its age reaches to MaxAge.
2027 /* XXX: We should deal with this for *ALL* LSAs, not
2029 if (current
== NULL
) {
2030 if (IS_DEBUG_OSPF_EVENT
)
2032 "LSA[%s]: Previously originated Opaque-LSA,"
2033 "not found in the LSDB.",
2036 SET_FLAG(lsa
->flags
, OSPF_LSA_SELF
);
2038 ospf_opaque_self_originated_lsa_received(nbr
,
2040 ospf_ls_ack_send(nbr
, lsa
);
2046 /* It might be happen that received LSA is self-originated
2048 * router ID is changed. So, we should check if LSA is a
2050 * Link State ID is one of the router's own IP interface
2051 * addresses but whose
2052 * Advertising Router is not equal to the router's own Router ID
2053 * According to RFC 2328 12.4.2 and 13.4 this LSA should be
2057 if (lsa
->data
->type
== OSPF_NETWORK_LSA
) {
2058 struct listnode
*oinode
, *oinnode
;
2059 struct ospf_interface
*out_if
;
2062 for (ALL_LIST_ELEMENTS(oi
->ospf
->oiflist
, oinode
,
2067 if ((IPV4_ADDR_SAME(&out_if
->address
->u
.prefix4
,
2069 && (!(IPV4_ADDR_SAME(
2070 &oi
->ospf
->router_id
,
2071 &lsa
->data
->adv_router
)))) {
2072 if (out_if
->network_lsa_self
) {
2073 ospf_lsa_flush_area(
2075 if (IS_DEBUG_OSPF_EVENT
)
2077 "ospf_lsa_discard() in ospf_ls_upd() point 9: lsa %p Type-%d",
2081 ospf_lsa_discard(lsa
);
2091 /* (5) Find the instance of this LSA that is currently contained
2092 in the router's link state database. If there is no
2093 database copy, or the received LSA is more recent than
2094 the database copy the following steps must be performed.
2095 (The sub steps from RFC 2328 section 13 step (5) will be
2100 || (ret
= ospf_lsa_more_recent(current
, lsa
)) < 0) {
2102 if (current
&& (lsa
->data
->ls_seqnum
==
2103 htonl(OSPF_MAX_SEQUENCE_NUMBER
)
2104 && !IS_LSA_MAXAGE(lsa
))) {
2106 "Link State Update[%s]: has Max Seq but not MaxAge. Dropping it",
2109 DISCARD_LSA(lsa
, 4);
2113 /* Actual flooding procedure. */
2114 if (ospf_flood(oi
->ospf
, nbr
, current
, lsa
)
2115 < 0) /* Trap NSSA later. */
2116 DISCARD_LSA(lsa
, 5);
2120 /* (6) Else, If there is an instance of the LSA on the sending
2121 neighbor's Link state request list, an error has occurred in
2122 the Database Exchange process. In this case, restart the
2123 Database Exchange process by generating the neighbor event
2124 BadLSReq for the sending neighbor and stop processing the
2125 Link State Update packet. */
2127 if (ospf_ls_request_lookup(nbr
, lsa
)) {
2128 OSPF_NSM_EVENT_SCHEDULE(nbr
, NSM_BadLSReq
);
2131 "LSA[%s] instance exists on Link state request list",
2134 /* Clean list of LSAs. */
2135 ospf_upd_list_clean(lsas
);
2136 /* this lsa is not on lsas list already. */
2137 ospf_lsa_discard(lsa
);
2141 /* If the received LSA is the same instance as the database copy
2142 (i.e., neither one is more recent) the following two steps
2143 should be performed: */
2146 /* If the LSA is listed in the Link state retransmission
2148 for the receiving adjacency, the router itself is
2150 an acknowledgment for this LSA. The router should
2152 received LSA as an acknowledgment by removing the LSA
2154 the Link state retransmission list. This is termed
2156 "implied acknowledgment". */
2158 ls_ret
= ospf_ls_retransmit_lookup(nbr
, lsa
);
2160 if (ls_ret
!= NULL
) {
2161 ospf_ls_retransmit_delete(nbr
, ls_ret
);
2163 /* Delayed acknowledgment sent if advertisement
2165 from Designated Router, otherwise do nothing.
2167 if (oi
->state
== ISM_Backup
)
2171 ospf_lsa_lock(lsa
));
2173 DISCARD_LSA(lsa
, 6);
2175 /* Acknowledge the receipt of the LSA by sending a
2176 Link State Acknowledgment packet back out the
2180 ospf_ls_ack_send(nbr
, lsa
);
2181 DISCARD_LSA(lsa
, 7);
2185 /* The database copy is more recent. If the database copy
2186 has LS age equal to MaxAge and LS sequence number equal to
2187 MaxSequenceNumber, simply discard the received LSA without
2188 acknowledging it. (In this case, the LSA's LS sequence number
2190 wrapping, and the MaxSequenceNumber LSA must be completely
2191 flushed before any new LSA instance can be introduced). */
2193 else if (ret
> 0) /* Database copy is more recent */
2195 if (IS_LSA_MAXAGE(current
)
2196 && current
->data
->ls_seqnum
2197 == htonl(OSPF_MAX_SEQUENCE_NUMBER
)) {
2198 DISCARD_LSA(lsa
, 8);
2200 /* Otherwise, as long as the database copy has not been
2202 Link State Update within the last MinLSArrival
2204 database copy back to the sending neighbor,
2206 a Link State Update Packet. The Link State Update
2208 be sent directly to the neighbor. In so doing, do not
2210 database copy of the LSA on the neighbor's link state
2211 retransmission list, and do not acknowledge the
2213 recent) LSA instance. */
2215 if (monotime_since(¤t
->tv_orig
, NULL
)
2216 >= ospf
->min_ls_arrival
* 1000LL)
2217 /* Trap NSSA type later.*/
2218 ospf_ls_upd_send_lsa(
2220 OSPF_SEND_PACKET_DIRECT
);
2221 DISCARD_LSA(lsa
, 9);
2227 assert(listcount(lsas
) == 0);
2231 /* OSPF Link State Acknowledgment message read -- RFC2328 Section 13.7. */
2232 static void ospf_ls_ack(struct ip
*iph
, struct ospf_header
*ospfh
,
2233 struct stream
*s
, struct ospf_interface
*oi
,
2236 struct ospf_neighbor
*nbr
;
2238 /* increment statistics. */
2241 nbr
= ospf_nbr_lookup(oi
, iph
, ospfh
);
2243 flog_warn(EC_OSPF_PACKET
,
2244 "Link State Acknowledgment: Unknown Neighbor %s.",
2245 inet_ntoa(ospfh
->router_id
));
2249 /* Add event to thread. */
2250 OSPF_NSM_EVENT_SCHEDULE(nbr
, NSM_PacketReceived
);
2252 if (nbr
->state
< NSM_Exchange
) {
2253 if (IS_DEBUG_OSPF(nsm
, NSM_EVENTS
))
2255 "Link State Acknowledgment: "
2256 "Neighbor[%s] state %s is less than Exchange",
2257 inet_ntoa(ospfh
->router_id
),
2258 lookup_msg(ospf_nsm_state_msg
, nbr
->state
,
2263 while (size
>= OSPF_LSA_HEADER_SIZE
) {
2264 struct ospf_lsa
*lsa
, *lsr
;
2266 lsa
= ospf_lsa_new();
2267 lsa
->data
= (struct lsa_header
*)stream_pnt(s
);
2268 lsa
->vrf_id
= oi
->ospf
->vrf_id
;
2270 /* lsah = (struct lsa_header *) stream_pnt (s); */
2271 size
-= OSPF_LSA_HEADER_SIZE
;
2272 stream_forward_getp(s
, OSPF_LSA_HEADER_SIZE
);
2274 if (lsa
->data
->type
< OSPF_MIN_LSA
2275 || lsa
->data
->type
>= OSPF_MAX_LSA
) {
2277 ospf_lsa_discard(lsa
);
2281 lsr
= ospf_ls_retransmit_lookup(nbr
, lsa
);
2283 if (lsr
!= NULL
&& ospf_lsa_more_recent(lsr
, lsa
) == 0)
2284 ospf_ls_retransmit_delete(nbr
, lsr
);
2287 ospf_lsa_discard(lsa
);
2293 static struct stream
*ospf_recv_packet(struct ospf
*ospf
, int fd
,
2294 struct interface
**ifp
,
2295 struct stream
*ibuf
)
2300 ifindex_t ifindex
= 0;
2302 /* Header and data both require alignment. */
2303 char buff
[CMSG_SPACE(SOPT_SIZE_CMSG_IFINDEX_IPV4())];
2306 memset(&msgh
, 0, sizeof(struct msghdr
));
2307 msgh
.msg_iov
= &iov
;
2308 msgh
.msg_iovlen
= 1;
2309 msgh
.msg_control
= (caddr_t
)buff
;
2310 msgh
.msg_controllen
= sizeof(buff
);
2312 ret
= stream_recvmsg(ibuf
, fd
, &msgh
, 0, OSPF_MAX_PACKET_SIZE
+ 1);
2314 flog_warn(EC_OSPF_PACKET
, "stream_recvmsg failed: %s",
2315 safe_strerror(errno
));
2318 if ((unsigned int)ret
< sizeof(iph
)) /* ret must be > 0 now */
2322 "ospf_recv_packet: discarding runt packet of length %d "
2323 "(ip header size is %u)",
2324 ret
, (unsigned int)sizeof(iph
));
2328 /* Note that there should not be alignment problems with this assignment
2329 because this is at the beginning of the stream data buffer. */
2330 iph
= (struct ip
*)STREAM_DATA(ibuf
);
2331 sockopt_iphdrincl_swab_systoh(iph
);
2333 ip_len
= iph
->ip_len
;
2335 #if !defined(GNU_LINUX) && (OpenBSD < 200311) && (__FreeBSD_version < 1000000)
2337 * Kernel network code touches incoming IP header parameters,
2338 * before protocol specific processing.
2340 * 1) Convert byteorder to host representation.
2341 * --> ip_len, ip_id, ip_off
2343 * 2) Adjust ip_len to strip IP header size!
2344 * --> If user process receives entire IP packet via RAW
2345 * socket, it must consider adding IP header size to
2346 * the "ip_len" field of "ip" structure.
2348 * For more details, see <netinet/ip_input.c>.
2350 ip_len
= ip_len
+ (iph
->ip_hl
<< 2);
2353 #if defined(__DragonFly__)
2355 * in DragonFly's raw socket, ip_len/ip_off are read
2356 * in network byte order.
2357 * As OpenBSD < 200311 adjust ip_len to strip IP header size!
2359 ip_len
= ntohs(iph
->ip_len
) + (iph
->ip_hl
<< 2);
2362 ifindex
= getsockopt_ifindex(AF_INET
, &msgh
);
2364 *ifp
= if_lookup_by_index(ifindex
, ospf
->vrf_id
);
2366 if (ret
!= ip_len
) {
2369 "ospf_recv_packet read length mismatch: ip_len is %d, "
2370 "but recvmsg returned %d",
2378 static struct ospf_interface
*
2379 ospf_associate_packet_vl(struct ospf
*ospf
, struct interface
*ifp
,
2380 struct ip
*iph
, struct ospf_header
*ospfh
)
2382 struct ospf_interface
*rcv_oi
;
2383 struct ospf_vl_data
*vl_data
;
2384 struct ospf_area
*vl_area
;
2385 struct listnode
*node
;
2387 if (IN_MULTICAST(ntohl(iph
->ip_dst
.s_addr
))
2388 || !OSPF_IS_AREA_BACKBONE(ospfh
))
2391 /* look for local OSPF interface matching the destination
2392 * to determine Area ID. We presume therefore the destination address
2393 * is unique, or at least (for "unnumbered" links), not used in other
2396 if ((rcv_oi
= ospf_if_lookup_by_local_addr(ospf
, NULL
, iph
->ip_dst
))
2400 for (ALL_LIST_ELEMENTS_RO(ospf
->vlinks
, node
, vl_data
)) {
2402 ospf_area_lookup_by_area_id(ospf
, vl_data
->vl_area_id
);
2406 if (OSPF_AREA_SAME(&vl_area
, &rcv_oi
->area
)
2407 && IPV4_ADDR_SAME(&vl_data
->vl_peer
, &ospfh
->router_id
)) {
2408 if (IS_DEBUG_OSPF_EVENT
)
2409 zlog_debug("associating packet with %s",
2410 IF_NAME(vl_data
->vl_oi
));
2411 if (!CHECK_FLAG(vl_data
->vl_oi
->ifp
->flags
, IFF_UP
)) {
2412 if (IS_DEBUG_OSPF_EVENT
)
2414 "This VL is not up yet, sorry");
2418 return vl_data
->vl_oi
;
2422 if (IS_DEBUG_OSPF_EVENT
)
2423 zlog_debug("couldn't find any VL to associate the packet with");
2428 static int ospf_check_area_id(struct ospf_interface
*oi
,
2429 struct ospf_header
*ospfh
)
2431 /* Check match the Area ID of the receiving interface. */
2432 if (OSPF_AREA_SAME(&oi
->area
, &ospfh
))
2438 /* Unbound socket will accept any Raw IP packets if proto is matched.
2439 To prevent it, compare src IP address and i/f address with masking
2440 i/f network mask. */
2441 static int ospf_check_network_mask(struct ospf_interface
*oi
,
2442 struct in_addr ip_src
)
2444 struct in_addr mask
, me
, him
;
2446 if (oi
->type
== OSPF_IFTYPE_POINTOPOINT
2447 || oi
->type
== OSPF_IFTYPE_VIRTUALLINK
)
2450 masklen2ip(oi
->address
->prefixlen
, &mask
);
2452 me
.s_addr
= oi
->address
->u
.prefix4
.s_addr
& mask
.s_addr
;
2453 him
.s_addr
= ip_src
.s_addr
& mask
.s_addr
;
2455 if (IPV4_ADDR_SAME(&me
, &him
))
2461 /* Return 1, if the packet is properly authenticated and checksummed,
2462 0 otherwise. In particular, check that AuType header field is valid and
2463 matches the locally configured AuType, and that D.5 requirements are met. */
2464 static int ospf_check_auth(struct ospf_interface
*oi
, struct ospf_header
*ospfh
)
2466 struct crypt_key
*ck
;
2467 uint16_t iface_auth_type
;
2468 uint16_t pkt_auth_type
= ntohs(ospfh
->auth_type
);
2470 switch (pkt_auth_type
) {
2471 case OSPF_AUTH_NULL
: /* RFC2328 D.5.1 */
2472 if (OSPF_AUTH_NULL
!= (iface_auth_type
= ospf_auth_type(oi
))) {
2473 if (IS_DEBUG_OSPF_PACKET(ospfh
->type
- 1, RECV
))
2476 "interface %s: auth-type mismatch, local %s, rcvd Null",
2478 lookup_msg(ospf_auth_type_str
,
2479 iface_auth_type
, NULL
));
2482 if (!ospf_check_sum(ospfh
)) {
2483 if (IS_DEBUG_OSPF_PACKET(ospfh
->type
- 1, RECV
))
2486 "interface %s: Null auth OK, but checksum error, Router-ID %s",
2488 inet_ntoa(ospfh
->router_id
));
2492 case OSPF_AUTH_SIMPLE
: /* RFC2328 D.5.2 */
2493 if (OSPF_AUTH_SIMPLE
2494 != (iface_auth_type
= ospf_auth_type(oi
))) {
2495 if (IS_DEBUG_OSPF_PACKET(ospfh
->type
- 1, RECV
))
2498 "interface %s: auth-type mismatch, local %s, rcvd Simple",
2500 lookup_msg(ospf_auth_type_str
,
2501 iface_auth_type
, NULL
));
2504 if (memcmp(OSPF_IF_PARAM(oi
, auth_simple
), ospfh
->u
.auth_data
,
2505 OSPF_AUTH_SIMPLE_SIZE
)) {
2506 if (IS_DEBUG_OSPF_PACKET(ospfh
->type
- 1, RECV
))
2507 flog_warn(EC_OSPF_PACKET
,
2508 "interface %s: Simple auth failed",
2512 if (!ospf_check_sum(ospfh
)) {
2513 if (IS_DEBUG_OSPF_PACKET(ospfh
->type
- 1, RECV
))
2516 "interface %s: Simple auth OK, checksum error, Router-ID %s",
2518 inet_ntoa(ospfh
->router_id
));
2522 case OSPF_AUTH_CRYPTOGRAPHIC
: /* RFC2328 D.5.3 */
2523 if (OSPF_AUTH_CRYPTOGRAPHIC
2524 != (iface_auth_type
= ospf_auth_type(oi
))) {
2525 if (IS_DEBUG_OSPF_PACKET(ospfh
->type
- 1, RECV
))
2528 "interface %s: auth-type mismatch, local %s, rcvd Cryptographic",
2530 lookup_msg(ospf_auth_type_str
,
2531 iface_auth_type
, NULL
));
2534 if (ospfh
->checksum
) {
2535 if (IS_DEBUG_OSPF_PACKET(ospfh
->type
- 1, RECV
))
2538 "interface %s: OSPF header checksum is not 0",
2542 /* only MD5 crypto method can pass ospf_packet_examin() */
2543 if (NULL
== (ck
= listgetdata(
2544 listtail(OSPF_IF_PARAM(oi
, auth_crypt
))))
2545 || ospfh
->u
.crypt
.key_id
!= ck
->key_id
||
2546 /* Condition above uses the last key ID on the list,
2548 different from what ospf_crypt_key_lookup() does. A
2550 !ospf_check_md5_digest(oi
, ospfh
)) {
2551 if (IS_DEBUG_OSPF_PACKET(ospfh
->type
- 1, RECV
))
2552 flog_warn(EC_OSPF_MD5
,
2553 "interface %s: MD5 auth failed",
2559 if (IS_DEBUG_OSPF_PACKET(ospfh
->type
- 1, RECV
))
2562 "interface %s: invalid packet auth-type (%02x)",
2563 IF_NAME(oi
), pkt_auth_type
);
2568 static int ospf_check_sum(struct ospf_header
*ospfh
)
2573 /* clear auth_data for checksum. */
2574 memset(ospfh
->u
.auth_data
, 0, OSPF_AUTH_SIMPLE_SIZE
);
2576 /* keep checksum and clear. */
2577 sum
= ospfh
->checksum
;
2578 memset(&ospfh
->checksum
, 0, sizeof(uint16_t));
2580 /* calculate checksum. */
2581 ret
= in_cksum(ospfh
, ntohs(ospfh
->length
));
2584 zlog_info("ospf_check_sum(): checksum mismatch, my %X, his %X",
2592 /* Verify, that given link/TOS records are properly sized/aligned and match
2593 Router-LSA "# links" and "# TOS" fields as specified in RFC2328 A.4.2. */
2594 static unsigned ospf_router_lsa_links_examin(struct router_lsa_link
*link
,
2596 const uint16_t num_links
)
2598 unsigned counted_links
= 0, thislinklen
;
2602 OSPF_ROUTER_LSA_LINK_SIZE
+ 4 * link
->m
[0].tos_count
;
2603 if (thislinklen
> linkbytes
) {
2604 if (IS_DEBUG_OSPF_PACKET(0, RECV
))
2605 zlog_debug("%s: length error in link block #%u",
2606 __func__
, counted_links
);
2609 link
= (struct router_lsa_link
*)((caddr_t
)link
+ thislinklen
);
2610 linkbytes
-= thislinklen
;
2613 if (counted_links
!= num_links
) {
2614 if (IS_DEBUG_OSPF_PACKET(0, RECV
))
2615 zlog_debug("%s: %u link blocks declared, %u present",
2616 __func__
, num_links
, counted_links
);
2622 /* Verify, that the given LSA is properly sized/aligned (including type-specific
2623 minimum length constraint). */
2624 static unsigned ospf_lsa_examin(struct lsa_header
*lsah
, const uint16_t lsalen
,
2625 const uint8_t headeronly
)
2628 struct router_lsa
*rlsa
;
2629 if (lsah
->type
< OSPF_MAX_LSA
&& ospf_lsa_minlen
[lsah
->type
]
2630 && lsalen
< OSPF_LSA_HEADER_SIZE
+ ospf_lsa_minlen
[lsah
->type
]) {
2631 if (IS_DEBUG_OSPF_PACKET(0, RECV
))
2632 zlog_debug("%s: undersized (%u B) %s", __func__
, lsalen
,
2633 lookup_msg(ospf_lsa_type_msg
, lsah
->type
,
2637 switch (lsah
->type
) {
2638 case OSPF_ROUTER_LSA
:
2639 /* RFC2328 A.4.2, LSA header + 4 bytes followed by N>=1
2640 * (12+)-byte link blocks */
2642 ret
= (lsalen
- OSPF_LSA_HEADER_SIZE
2643 - OSPF_ROUTER_LSA_MIN_SIZE
)
2649 rlsa
= (struct router_lsa
*)lsah
;
2650 ret
= ospf_router_lsa_links_examin(
2651 (struct router_lsa_link
*)rlsa
->link
,
2652 lsalen
- OSPF_LSA_HEADER_SIZE
- 4, /* skip: basic
2655 ntohs(rlsa
->links
) /* 16 bits */
2658 case OSPF_AS_EXTERNAL_LSA
:
2659 /* RFC2328 A.4.5, LSA header + 4 bytes followed by N>=1 12-bytes long
2661 case OSPF_AS_NSSA_LSA
:
2662 /* RFC3101 C, idem */
2663 ret
= (lsalen
- OSPF_LSA_HEADER_SIZE
2664 - OSPF_AS_EXTERNAL_LSA_MIN_SIZE
)
2669 /* Following LSA types are considered OK length-wise as soon as their
2671 * length constraint is met and length of the whole LSA is a multiple of
2673 * (basic LSA header size is already a multiple of 4). */
2674 case OSPF_NETWORK_LSA
:
2675 /* RFC2328 A.4.3, LSA header + 4 bytes followed by N>=1 router-IDs */
2676 case OSPF_SUMMARY_LSA
:
2677 case OSPF_ASBR_SUMMARY_LSA
:
2678 /* RFC2328 A.4.4, LSA header + 4 bytes followed by N>=1 4-bytes TOS
2680 case OSPF_OPAQUE_LINK_LSA
:
2681 case OSPF_OPAQUE_AREA_LSA
:
2682 case OSPF_OPAQUE_AS_LSA
:
2683 /* RFC5250 A.2, "some number of octets (of application-specific
2684 * data) padded to 32-bit alignment." This is considered
2686 * to 4-byte alignment of all other LSA types, see
2687 * OSPF-ALIGNMENT.txt
2688 * file for the detailed analysis of this passage. */
2689 ret
= lsalen
% 4 ? MSG_NG
: MSG_OK
;
2692 if (IS_DEBUG_OSPF_PACKET(0, RECV
))
2693 zlog_debug("%s: unsupported LSA type 0x%02x", __func__
,
2697 if (ret
!= MSG_OK
&& IS_DEBUG_OSPF_PACKET(0, RECV
))
2698 zlog_debug("%s: alignment error in %s", __func__
,
2699 lookup_msg(ospf_lsa_type_msg
, lsah
->type
, NULL
));
2703 /* Verify if the provided input buffer is a valid sequence of LSAs. This
2704 includes verification of LSA blocks length/alignment and dispatching
2705 of deeper-level checks. */
2707 ospf_lsaseq_examin(struct lsa_header
*lsah
, /* start of buffered data */
2708 size_t length
, const uint8_t headeronly
,
2709 /* When declared_num_lsas is not 0, compare it to the real
2711 and treat the difference as an error. */
2712 const uint32_t declared_num_lsas
)
2714 uint32_t counted_lsas
= 0;
2718 if (length
< OSPF_LSA_HEADER_SIZE
) {
2719 if (IS_DEBUG_OSPF_PACKET(0, RECV
))
2721 "%s: undersized (%zu B) trailing (#%u) LSA header",
2722 __func__
, length
, counted_lsas
);
2725 /* save on ntohs() calls here and in the LSA validator */
2726 lsalen
= ntohs(lsah
->length
);
2727 if (lsalen
< OSPF_LSA_HEADER_SIZE
) {
2728 if (IS_DEBUG_OSPF_PACKET(0, RECV
))
2730 "%s: malformed LSA header #%u, declared length is %u B",
2731 __func__
, counted_lsas
, lsalen
);
2735 /* less checks here and in ospf_lsa_examin() */
2736 if (MSG_OK
!= ospf_lsa_examin(lsah
, lsalen
, 1)) {
2737 if (IS_DEBUG_OSPF_PACKET(0, RECV
))
2739 "%s: malformed header-only LSA #%u",
2740 __func__
, counted_lsas
);
2743 lsah
= (struct lsa_header
*)((caddr_t
)lsah
2744 + OSPF_LSA_HEADER_SIZE
);
2745 length
-= OSPF_LSA_HEADER_SIZE
;
2747 /* make sure the input buffer is deep enough before
2749 if (lsalen
> length
) {
2750 if (IS_DEBUG_OSPF_PACKET(0, RECV
))
2752 "%s: anomaly in LSA #%u: declared length is %u B, buffered length is %zu B",
2753 __func__
, counted_lsas
, lsalen
,
2757 if (MSG_OK
!= ospf_lsa_examin(lsah
, lsalen
, 0)) {
2758 if (IS_DEBUG_OSPF_PACKET(0, RECV
))
2759 zlog_debug("%s: malformed LSA #%u",
2760 __func__
, counted_lsas
);
2763 lsah
= (struct lsa_header
*)((caddr_t
)lsah
+ lsalen
);
2769 if (declared_num_lsas
&& counted_lsas
!= declared_num_lsas
) {
2770 if (IS_DEBUG_OSPF_PACKET(0, RECV
))
2772 "%s: #LSAs declared (%u) does not match actual (%u)",
2773 __func__
, declared_num_lsas
, counted_lsas
);
2779 /* Verify a complete OSPF packet for proper sizing/alignment. */
2780 static unsigned ospf_packet_examin(struct ospf_header
*oh
,
2781 const unsigned bytesonwire
)
2783 uint16_t bytesdeclared
, bytesauth
;
2785 struct ospf_ls_update
*lsupd
;
2787 /* Length, 1st approximation. */
2788 if (bytesonwire
< OSPF_HEADER_SIZE
) {
2789 if (IS_DEBUG_OSPF_PACKET(0, RECV
))
2790 zlog_debug("%s: undersized (%u B) packet", __func__
,
2794 /* Now it is safe to access header fields. Performing length check,
2796 * for possible extra bytes of crypto auth/padding, which are not
2798 * in the OSPF header "length" field. */
2799 if (oh
->version
!= OSPF_VERSION
) {
2800 if (IS_DEBUG_OSPF_PACKET(0, RECV
))
2801 zlog_debug("%s: invalid (%u) protocol version",
2802 __func__
, oh
->version
);
2805 bytesdeclared
= ntohs(oh
->length
);
2806 if (ntohs(oh
->auth_type
) != OSPF_AUTH_CRYPTOGRAPHIC
)
2809 if (oh
->u
.crypt
.auth_data_len
!= OSPF_AUTH_MD5_SIZE
) {
2810 if (IS_DEBUG_OSPF_PACKET(0, RECV
))
2812 "%s: unsupported crypto auth length (%u B)",
2813 __func__
, oh
->u
.crypt
.auth_data_len
);
2816 bytesauth
= OSPF_AUTH_MD5_SIZE
;
2818 if (bytesdeclared
+ bytesauth
> bytesonwire
) {
2819 if (IS_DEBUG_OSPF_PACKET(0, RECV
))
2821 "%s: packet length error (%u real, %u+%u declared)",
2822 __func__
, bytesonwire
, bytesdeclared
,
2826 /* Length, 2nd approximation. The type-specific constraint is checked
2827 against declared length, not amount of bytes on wire. */
2828 if (oh
->type
>= OSPF_MSG_HELLO
&& oh
->type
<= OSPF_MSG_LS_ACK
2830 < OSPF_HEADER_SIZE
+ ospf_packet_minlen
[oh
->type
]) {
2831 if (IS_DEBUG_OSPF_PACKET(0, RECV
))
2832 zlog_debug("%s: undersized (%u B) %s packet", __func__
,
2834 lookup_msg(ospf_packet_type_str
, oh
->type
,
2839 case OSPF_MSG_HELLO
:
2840 /* RFC2328 A.3.2, packet header + OSPF_HELLO_MIN_SIZE bytes
2842 by N>=0 router-IDs. */
2843 ret
= (bytesdeclared
- OSPF_HEADER_SIZE
- OSPF_HELLO_MIN_SIZE
)
2848 case OSPF_MSG_DB_DESC
:
2849 /* RFC2328 A.3.3, packet header + OSPF_DB_DESC_MIN_SIZE bytes
2851 by N>=0 header-only LSAs. */
2852 ret
= ospf_lsaseq_examin(
2853 (struct lsa_header
*)((caddr_t
)oh
+ OSPF_HEADER_SIZE
2854 + OSPF_DB_DESC_MIN_SIZE
),
2855 bytesdeclared
- OSPF_HEADER_SIZE
2856 - OSPF_DB_DESC_MIN_SIZE
,
2857 1, /* header-only LSAs */
2860 case OSPF_MSG_LS_REQ
:
2861 /* RFC2328 A.3.4, packet header followed by N>=0 12-bytes
2862 * request blocks. */
2863 ret
= (bytesdeclared
- OSPF_HEADER_SIZE
- OSPF_LS_REQ_MIN_SIZE
)
2868 case OSPF_MSG_LS_UPD
:
2869 /* RFC2328 A.3.5, packet header + OSPF_LS_UPD_MIN_SIZE bytes
2871 by N>=0 full LSAs (with N declared beforehand). */
2872 lsupd
= (struct ospf_ls_update
*)((caddr_t
)oh
2873 + OSPF_HEADER_SIZE
);
2874 ret
= ospf_lsaseq_examin(
2875 (struct lsa_header
*)((caddr_t
)lsupd
2876 + OSPF_LS_UPD_MIN_SIZE
),
2877 bytesdeclared
- OSPF_HEADER_SIZE
- OSPF_LS_UPD_MIN_SIZE
,
2879 ntohl(lsupd
->num_lsas
) /* 32 bits */
2882 case OSPF_MSG_LS_ACK
:
2883 /* RFC2328 A.3.6, packet header followed by N>=0 header-only
2885 ret
= ospf_lsaseq_examin(
2886 (struct lsa_header
*)((caddr_t
)oh
+ OSPF_HEADER_SIZE
2887 + OSPF_LS_ACK_MIN_SIZE
),
2888 bytesdeclared
- OSPF_HEADER_SIZE
- OSPF_LS_ACK_MIN_SIZE
,
2889 1, /* header-only LSAs */
2893 if (IS_DEBUG_OSPF_PACKET(0, RECV
))
2894 zlog_debug("%s: invalid packet type 0x%02x", __func__
,
2898 if (ret
!= MSG_OK
&& IS_DEBUG_OSPF_PACKET(0, RECV
))
2899 zlog_debug("%s: malformed %s packet", __func__
,
2900 lookup_msg(ospf_packet_type_str
, oh
->type
, NULL
));
2904 /* OSPF Header verification. */
2905 static int ospf_verify_header(struct stream
*ibuf
, struct ospf_interface
*oi
,
2906 struct ip
*iph
, struct ospf_header
*ospfh
)
2908 /* Check Area ID. */
2909 if (!ospf_check_area_id(oi
, ospfh
)) {
2910 flog_warn(EC_OSPF_PACKET
,
2911 "interface %s: ospf_read invalid Area ID %s.",
2912 IF_NAME(oi
), inet_ntoa(ospfh
->area_id
));
2916 /* Check network mask, Silently discarded. */
2917 if (!ospf_check_network_mask(oi
, iph
->ip_src
)) {
2920 "interface %s: ospf_read network address is not same [%s]",
2921 IF_NAME(oi
), inet_ntoa(iph
->ip_src
));
2925 /* Check authentication. The function handles logging actions, where
2927 if (!ospf_check_auth(oi
, ospfh
))
2933 /* Starting point of packet process function. */
2934 int ospf_read(struct thread
*thread
)
2937 struct stream
*ibuf
;
2939 struct ospf_interface
*oi
;
2941 struct ospf_header
*ospfh
;
2943 struct interface
*ifp
= NULL
;
2944 struct connected
*c
;
2946 /* first of all get interface pointer. */
2947 ospf
= THREAD_ARG(thread
);
2949 /* prepare for next packet. */
2950 ospf
->t_read
= NULL
;
2951 thread_add_read(master
, ospf_read
, ospf
, ospf
->fd
, &ospf
->t_read
);
2953 stream_reset(ospf
->ibuf
);
2954 ibuf
= ospf_recv_packet(ospf
, ospf
->fd
, &ifp
, ospf
->ibuf
);
2957 /* This raw packet is known to be at least as big as its IP header. */
2959 /* Note that there should not be alignment problems with this assignment
2960 because this is at the beginning of the stream data buffer. */
2961 iph
= (struct ip
*)STREAM_DATA(ibuf
);
2962 /* Note that sockopt_iphdrincl_swab_systoh was called in
2963 * ospf_recv_packet. */
2966 /* Handle cases where the platform does not support retrieving
2968 and also platforms (such as Solaris 8) that claim to support
2970 retrieval but do not. */
2971 c
= if_lookup_address((void *)&iph
->ip_src
, AF_INET
,
2979 /* IP Header dump. */
2980 if (IS_DEBUG_OSPF_PACKET(0, RECV
))
2981 ospf_ip_header_dump(iph
);
2983 /* Self-originated packet should be discarded silently. */
2984 if (ospf_if_lookup_by_local_addr(ospf
, NULL
, iph
->ip_src
)) {
2985 if (IS_DEBUG_OSPF_PACKET(0, RECV
)) {
2987 "ospf_read[%s]: Dropping self-originated packet",
2988 inet_ntoa(iph
->ip_src
));
2993 /* Advance from IP header to OSPF header (iph->ip_hl has been verified
2994 by ospf_recv_packet() to be correct). */
2995 stream_forward_getp(ibuf
, iph
->ip_hl
* 4);
2997 ospfh
= (struct ospf_header
*)stream_pnt(ibuf
);
2999 != ospf_packet_examin(
3000 ospfh
, stream_get_endp(ibuf
) - stream_get_getp(ibuf
)))
3002 /* Now it is safe to access all fields of OSPF packet header. */
3004 /* associate packet with ospf interface */
3005 oi
= ospf_if_lookup_recv_if(ospf
, iph
->ip_src
, ifp
);
3007 /* ospf_verify_header() relies on a valid "oi" and thus can be called
3009 after the passive/backbone/other checks below are passed. These
3011 in turn access the fields of unverified "ospfh" structure for their
3013 purposes and must remain very accurate in doing this. */
3015 /* If incoming interface is passive one, ignore it. */
3016 if (oi
&& OSPF_IF_PASSIVE_STATUS(oi
) == OSPF_IF_PASSIVE
) {
3017 char buf
[3][INET_ADDRSTRLEN
];
3019 if (IS_DEBUG_OSPF_EVENT
)
3021 "ignoring packet from router %s sent to %s, "
3022 "received on a passive interface, %s",
3023 inet_ntop(AF_INET
, &ospfh
->router_id
, buf
[0],
3025 inet_ntop(AF_INET
, &iph
->ip_dst
, buf
[1],
3027 inet_ntop(AF_INET
, &oi
->address
->u
.prefix4
,
3028 buf
[2], sizeof(buf
[2])));
3030 if (iph
->ip_dst
.s_addr
== htonl(OSPF_ALLSPFROUTERS
)) {
3031 /* Try to fix multicast membership.
3032 * Some OS:es may have problems in this area,
3033 * make sure it is removed.
3035 OI_MEMBER_JOINED(oi
, MEMBER_ALLROUTERS
);
3036 ospf_if_set_multicast(oi
);
3042 /* if no local ospf_interface,
3043 * or header area is backbone but ospf_interface is not
3044 * check for VLINK interface
3046 if ((oi
== NULL
) || (OSPF_IS_AREA_ID_BACKBONE(ospfh
->area_id
)
3047 && !OSPF_IS_AREA_ID_BACKBONE(oi
->area
->area_id
))) {
3048 if ((oi
= ospf_associate_packet_vl(ospf
, ifp
, iph
, ospfh
))
3050 if (!ospf
->instance
&& IS_DEBUG_OSPF_EVENT
)
3052 "Packet from [%s] received on link %s"
3053 " but no ospf_interface",
3054 inet_ntoa(iph
->ip_src
), ifp
->name
);
3059 /* else it must be a local ospf interface, check it was received on
3062 else if (oi
->ifp
!= ifp
) {
3063 if (IS_DEBUG_OSPF_EVENT
)
3064 flog_warn(EC_OSPF_PACKET
,
3065 "Packet from [%s] received on wrong link %s",
3066 inet_ntoa(iph
->ip_src
), ifp
->name
);
3068 } else if (oi
->state
== ISM_Down
) {
3069 char buf
[2][INET_ADDRSTRLEN
];
3072 "Ignoring packet from %s to %s received on interface that is "
3073 "down [%s]; interface flags are %s",
3074 inet_ntop(AF_INET
, &iph
->ip_src
, buf
[0],
3076 inet_ntop(AF_INET
, &iph
->ip_dst
, buf
[1],
3078 ifp
->name
, if_flag_dump(ifp
->flags
));
3079 /* Fix multicast memberships? */
3080 if (iph
->ip_dst
.s_addr
== htonl(OSPF_ALLSPFROUTERS
))
3081 OI_MEMBER_JOINED(oi
, MEMBER_ALLROUTERS
);
3082 else if (iph
->ip_dst
.s_addr
== htonl(OSPF_ALLDROUTERS
))
3083 OI_MEMBER_JOINED(oi
, MEMBER_DROUTERS
);
3084 if (oi
->multicast_memberships
)
3085 ospf_if_set_multicast(oi
);
3090 * If the received packet is destined for AllDRouters, the packet
3091 * should be accepted only if the received ospf interface state is
3092 * either DR or Backup -- endo.
3094 if (iph
->ip_dst
.s_addr
== htonl(OSPF_ALLDROUTERS
)
3095 && (oi
->state
!= ISM_DR
&& oi
->state
!= ISM_Backup
)) {
3098 "Dropping packet for AllDRouters from [%s] via [%s] (ISM: %s)",
3099 inet_ntoa(iph
->ip_src
), IF_NAME(oi
),
3100 lookup_msg(ospf_ism_state_msg
, oi
->state
, NULL
));
3101 /* Try to fix multicast membership. */
3102 SET_FLAG(oi
->multicast_memberships
, MEMBER_DROUTERS
);
3103 ospf_if_set_multicast(oi
);
3107 /* Verify more OSPF header fields. */
3108 ret
= ospf_verify_header(ibuf
, oi
, iph
, ospfh
);
3110 if (IS_DEBUG_OSPF_PACKET(0, RECV
))
3112 "ospf_read[%s]: Header check failed, "
3114 inet_ntoa(iph
->ip_src
));
3118 /* Show debug receiving packet. */
3119 if (IS_DEBUG_OSPF_PACKET(ospfh
->type
- 1, RECV
)) {
3120 if (IS_DEBUG_OSPF_PACKET(ospfh
->type
- 1, DETAIL
)) {
3122 "-----------------------------------------------------");
3123 ospf_packet_dump(ibuf
);
3126 zlog_debug("%s received from [%s] via [%s]",
3127 lookup_msg(ospf_packet_type_str
, ospfh
->type
, NULL
),
3128 inet_ntoa(ospfh
->router_id
), IF_NAME(oi
));
3129 zlog_debug(" src [%s],", inet_ntoa(iph
->ip_src
));
3130 zlog_debug(" dst [%s]", inet_ntoa(iph
->ip_dst
));
3132 if (IS_DEBUG_OSPF_PACKET(ospfh
->type
- 1, DETAIL
))
3134 "-----------------------------------------------------");
3137 stream_forward_getp(ibuf
, OSPF_HEADER_SIZE
);
3139 /* Adjust size to message length. */
3140 length
= ntohs(ospfh
->length
) - OSPF_HEADER_SIZE
;
3142 /* Read rest of the packet and call each sort of packet routine. */
3143 switch (ospfh
->type
) {
3144 case OSPF_MSG_HELLO
:
3145 ospf_hello(iph
, ospfh
, ibuf
, oi
, length
);
3147 case OSPF_MSG_DB_DESC
:
3148 ospf_db_desc(iph
, ospfh
, ibuf
, oi
, length
);
3150 case OSPF_MSG_LS_REQ
:
3151 ospf_ls_req(iph
, ospfh
, ibuf
, oi
, length
);
3153 case OSPF_MSG_LS_UPD
:
3154 ospf_ls_upd(ospf
, iph
, ospfh
, ibuf
, oi
, length
);
3156 case OSPF_MSG_LS_ACK
:
3157 ospf_ls_ack(iph
, ospfh
, ibuf
, oi
, length
);
3160 flog_warn(EC_OSPF_PACKET
,
3161 "interface %s: OSPF packet header type %d is illegal",
3162 IF_NAME(oi
), ospfh
->type
);
3169 /* Make OSPF header. */
3170 static void ospf_make_header(int type
, struct ospf_interface
*oi
,
3173 struct ospf_header
*ospfh
;
3175 ospfh
= (struct ospf_header
*)STREAM_DATA(s
);
3177 ospfh
->version
= (uint8_t)OSPF_VERSION
;
3178 ospfh
->type
= (uint8_t)type
;
3180 ospfh
->router_id
= oi
->ospf
->router_id
;
3182 ospfh
->checksum
= 0;
3183 ospfh
->area_id
= oi
->area
->area_id
;
3184 ospfh
->auth_type
= htons(ospf_auth_type(oi
));
3186 memset(ospfh
->u
.auth_data
, 0, OSPF_AUTH_SIMPLE_SIZE
);
3188 stream_forward_endp(s
, OSPF_HEADER_SIZE
);
3191 /* Make Authentication Data. */
3192 static int ospf_make_auth(struct ospf_interface
*oi
, struct ospf_header
*ospfh
)
3194 struct crypt_key
*ck
;
3196 switch (ospf_auth_type(oi
)) {
3197 case OSPF_AUTH_NULL
:
3198 /* memset (ospfh->u.auth_data, 0, sizeof (ospfh->u.auth_data));
3201 case OSPF_AUTH_SIMPLE
:
3202 memcpy(ospfh
->u
.auth_data
, OSPF_IF_PARAM(oi
, auth_simple
),
3203 OSPF_AUTH_SIMPLE_SIZE
);
3205 case OSPF_AUTH_CRYPTOGRAPHIC
:
3206 /* If key is not set, then set 0. */
3207 if (list_isempty(OSPF_IF_PARAM(oi
, auth_crypt
))) {
3208 ospfh
->u
.crypt
.zero
= 0;
3209 ospfh
->u
.crypt
.key_id
= 0;
3210 ospfh
->u
.crypt
.auth_data_len
= OSPF_AUTH_MD5_SIZE
;
3213 listtail(OSPF_IF_PARAM(oi
, auth_crypt
)));
3214 ospfh
->u
.crypt
.zero
= 0;
3215 ospfh
->u
.crypt
.key_id
= ck
->key_id
;
3216 ospfh
->u
.crypt
.auth_data_len
= OSPF_AUTH_MD5_SIZE
;
3218 /* note: the seq is done in ospf_make_md5_digest() */
3221 /* memset (ospfh->u.auth_data, 0, sizeof (ospfh->u.auth_data));
3229 /* Fill rest of OSPF header. */
3230 static void ospf_fill_header(struct ospf_interface
*oi
, struct stream
*s
,
3233 struct ospf_header
*ospfh
;
3235 ospfh
= (struct ospf_header
*)STREAM_DATA(s
);
3238 ospfh
->length
= htons(length
);
3240 /* Calculate checksum. */
3241 if (ntohs(ospfh
->auth_type
) != OSPF_AUTH_CRYPTOGRAPHIC
)
3242 ospfh
->checksum
= in_cksum(ospfh
, length
);
3244 ospfh
->checksum
= 0;
3246 /* Add Authentication Data. */
3247 ospf_make_auth(oi
, ospfh
);
3250 static int ospf_make_hello(struct ospf_interface
*oi
, struct stream
*s
)
3252 struct ospf_neighbor
*nbr
;
3253 struct route_node
*rn
;
3254 uint16_t length
= OSPF_HELLO_MIN_SIZE
;
3255 struct in_addr mask
;
3259 /* Set netmask of interface. */
3260 if (!(CHECK_FLAG(oi
->connected
->flags
, ZEBRA_IFA_UNNUMBERED
)
3261 && oi
->type
== OSPF_IFTYPE_POINTOPOINT
)
3262 && oi
->type
!= OSPF_IFTYPE_VIRTUALLINK
)
3263 masklen2ip(oi
->address
->prefixlen
, &mask
);
3265 memset((char *)&mask
, 0, sizeof(struct in_addr
));
3266 stream_put_ipv4(s
, mask
.s_addr
);
3268 /* Set Hello Interval. */
3269 if (OSPF_IF_PARAM(oi
, fast_hello
) == 0)
3270 stream_putw(s
, OSPF_IF_PARAM(oi
, v_hello
));
3272 stream_putw(s
, 0); /* hello-interval of 0 for fast-hellos */
3274 if (IS_DEBUG_OSPF_EVENT
)
3275 zlog_debug("make_hello: options: %x, int: %s", OPTIONS(oi
),
3279 stream_putc(s
, OPTIONS(oi
));
3281 /* Set Router Priority. */
3282 stream_putc(s
, PRIORITY(oi
));
3284 /* Set Router Dead Interval. */
3285 stream_putl(s
, OSPF_IF_PARAM(oi
, v_wait
));
3287 /* Set Designated Router. */
3288 stream_put_ipv4(s
, DR(oi
).s_addr
);
3290 p
= stream_get_endp(s
);
3292 /* Set Backup Designated Router. */
3293 stream_put_ipv4(s
, BDR(oi
).s_addr
);
3295 /* Add neighbor seen. */
3296 for (rn
= route_top(oi
->nbrs
); rn
; rn
= route_next(rn
))
3297 if ((nbr
= rn
->info
))
3298 if (nbr
->router_id
.s_addr
3299 != 0) /* Ignore 0.0.0.0 node. */
3301 != NSM_Attempt
) /* Ignore Down neighbor. */
3303 != NSM_Down
) /* This is myself for
3305 if (!IPV4_ADDR_SAME(
3307 &oi
->ospf
->router_id
)) {
3308 /* Check neighbor is
3310 if (nbr
->d_router
.s_addr
3324 /* Hello packet overflows interface MTU. */
3325 if (length
+ sizeof(uint32_t)
3326 > ospf_packet_max(oi
)) {
3328 EC_OSPF_LARGE_HELLO
,
3329 "Oversized Hello packet! Larger than MTU. Not sending it out");
3340 /* Let neighbor generate BackupSeen. */
3342 stream_putl_at(s
, p
, 0); /* ipv4 address, normally */
3347 static int ospf_make_db_desc(struct ospf_interface
*oi
,
3348 struct ospf_neighbor
*nbr
, struct stream
*s
)
3350 struct ospf_lsa
*lsa
;
3351 uint16_t length
= OSPF_DB_DESC_MIN_SIZE
;
3355 struct ospf_lsdb
*lsdb
;
3357 /* Set Interface MTU. */
3358 if (oi
->type
== OSPF_IFTYPE_VIRTUALLINK
)
3361 stream_putw(s
, oi
->ifp
->mtu
);
3364 options
= OPTIONS(oi
);
3365 if (CHECK_FLAG(oi
->ospf
->config
, OSPF_OPAQUE_CAPABLE
))
3366 SET_FLAG(options
, OSPF_OPTION_O
);
3367 stream_putc(s
, options
);
3370 pp
= stream_get_endp(s
);
3371 stream_putc(s
, nbr
->dd_flags
);
3373 /* Set DD Sequence Number. */
3374 stream_putl(s
, nbr
->dd_seqnum
);
3376 /* shortcut unneeded walk of (empty) summary LSDBs */
3377 if (ospf_db_summary_isempty(nbr
))
3380 /* Describe LSA Header from Database Summary List. */
3381 lsdb
= &nbr
->db_sum
;
3383 for (i
= OSPF_MIN_LSA
; i
< OSPF_MAX_LSA
; i
++) {
3384 struct route_table
*table
= lsdb
->type
[i
].db
;
3385 struct route_node
*rn
;
3387 for (rn
= route_top(table
); rn
; rn
= route_next(rn
))
3388 if ((lsa
= rn
->info
) != NULL
) {
3389 if (IS_OPAQUE_LSA(lsa
->data
->type
)
3390 && (!CHECK_FLAG(options
, OSPF_OPTION_O
))) {
3391 /* Suppress advertising
3392 * opaque-information. */
3393 /* Remove LSA from DB summary list. */
3394 ospf_lsdb_delete(lsdb
, lsa
);
3398 if (!CHECK_FLAG(lsa
->flags
, OSPF_LSA_DISCARD
)) {
3399 struct lsa_header
*lsah
;
3402 /* DD packet overflows interface MTU. */
3403 if (length
+ OSPF_LSA_HEADER_SIZE
3404 > ospf_packet_max(oi
))
3407 /* Keep pointer to LS age. */
3408 lsah
= (struct lsa_header
3413 /* Proceed stream pointer. */
3414 stream_put(s
, lsa
->data
,
3415 OSPF_LSA_HEADER_SIZE
);
3416 length
+= OSPF_LSA_HEADER_SIZE
;
3419 ls_age
= LS_AGE(lsa
);
3420 lsah
->ls_age
= htons(ls_age
);
3423 /* Remove LSA from DB summary list. */
3424 ospf_lsdb_delete(lsdb
, lsa
);
3428 /* Update 'More' bit */
3429 if (ospf_db_summary_isempty(nbr
)) {
3431 if (nbr
->state
>= NSM_Exchange
) {
3432 UNSET_FLAG(nbr
->dd_flags
, OSPF_DD_FLAG_M
);
3433 /* Rewrite DD flags */
3434 stream_putc_at(s
, pp
, nbr
->dd_flags
);
3436 assert(IS_SET_DD_M(nbr
->dd_flags
));
3442 static int ospf_make_ls_req_func(struct stream
*s
, uint16_t *length
,
3443 unsigned long delta
, struct ospf_neighbor
*nbr
,
3444 struct ospf_lsa
*lsa
)
3446 struct ospf_interface
*oi
;
3450 /* LS Request packet overflows interface MTU. */
3451 if (*length
+ delta
> ospf_packet_max(oi
))
3454 stream_putl(s
, lsa
->data
->type
);
3455 stream_put_ipv4(s
, lsa
->data
->id
.s_addr
);
3456 stream_put_ipv4(s
, lsa
->data
->adv_router
.s_addr
);
3458 ospf_lsa_unlock(&nbr
->ls_req_last
);
3459 nbr
->ls_req_last
= ospf_lsa_lock(lsa
);
3465 static int ospf_make_ls_req(struct ospf_neighbor
*nbr
, struct stream
*s
)
3467 struct ospf_lsa
*lsa
;
3468 uint16_t length
= OSPF_LS_REQ_MIN_SIZE
;
3469 unsigned long delta
= stream_get_endp(s
) + 12;
3470 struct route_table
*table
;
3471 struct route_node
*rn
;
3473 struct ospf_lsdb
*lsdb
;
3475 lsdb
= &nbr
->ls_req
;
3477 for (i
= OSPF_MIN_LSA
; i
< OSPF_MAX_LSA
; i
++) {
3478 table
= lsdb
->type
[i
].db
;
3479 for (rn
= route_top(table
); rn
; rn
= route_next(rn
))
3480 if ((lsa
= (rn
->info
)) != NULL
)
3481 if (ospf_make_ls_req_func(s
, &length
, delta
,
3484 route_unlock_node(rn
);
3491 static int ls_age_increment(struct ospf_lsa
*lsa
, int delay
)
3495 age
= IS_LSA_MAXAGE(lsa
) ? OSPF_LSA_MAXAGE
: LS_AGE(lsa
) + delay
;
3497 return (age
> OSPF_LSA_MAXAGE
? OSPF_LSA_MAXAGE
: age
);
3500 static int ospf_make_ls_upd(struct ospf_interface
*oi
, struct list
*update
,
3503 struct ospf_lsa
*lsa
;
3504 struct listnode
*node
;
3505 uint16_t length
= 0;
3506 unsigned int size_noauth
;
3507 unsigned long delta
= stream_get_endp(s
);
3511 if (IS_DEBUG_OSPF_EVENT
)
3512 zlog_debug("ospf_make_ls_upd: Start");
3514 pp
= stream_get_endp(s
);
3515 stream_forward_endp(s
, OSPF_LS_UPD_MIN_SIZE
);
3516 length
+= OSPF_LS_UPD_MIN_SIZE
;
3518 /* Calculate amount of packet usable for data. */
3519 size_noauth
= stream_get_size(s
) - ospf_packet_authspace(oi
);
3521 while ((node
= listhead(update
)) != NULL
) {
3522 struct lsa_header
*lsah
;
3525 if (IS_DEBUG_OSPF_EVENT
)
3526 zlog_debug("ospf_make_ls_upd: List Iteration %d",
3529 lsa
= listgetdata(node
);
3534 if (length
+ delta
+ ntohs(lsa
->data
->length
) > size_noauth
)
3537 /* Keep pointer to LS age. */
3538 lsah
= (struct lsa_header
*)(STREAM_DATA(s
)
3539 + stream_get_endp(s
));
3541 /* Put LSA to Link State Request. */
3542 stream_put(s
, lsa
->data
, ntohs(lsa
->data
->length
));
3545 /* each hop must increment an lsa_age by transmit_delay
3546 of OSPF interface */
3547 ls_age
= ls_age_increment(lsa
,
3548 OSPF_IF_PARAM(oi
, transmit_delay
));
3549 lsah
->ls_age
= htons(ls_age
);
3551 length
+= ntohs(lsa
->data
->length
);
3554 list_delete_node(update
, node
);
3555 ospf_lsa_unlock(&lsa
); /* oi->ls_upd_queue */
3558 /* Now set #LSAs. */
3559 stream_putl_at(s
, pp
, count
);
3561 if (IS_DEBUG_OSPF_EVENT
)
3562 zlog_debug("ospf_make_ls_upd: Stop");
3566 static int ospf_make_ls_ack(struct ospf_interface
*oi
, struct list
*ack
,
3569 struct listnode
*node
, *nnode
;
3570 uint16_t length
= OSPF_LS_ACK_MIN_SIZE
;
3571 unsigned long delta
= stream_get_endp(s
) + 24;
3572 struct ospf_lsa
*lsa
;
3574 for (ALL_LIST_ELEMENTS(ack
, node
, nnode
, lsa
)) {
3577 if (length
+ delta
> ospf_packet_max(oi
))
3580 stream_put(s
, lsa
->data
, OSPF_LSA_HEADER_SIZE
);
3581 length
+= OSPF_LSA_HEADER_SIZE
;
3583 listnode_delete(ack
, lsa
);
3584 ospf_lsa_unlock(&lsa
); /* oi->ls_ack_direct.ls_ack */
3590 static void ospf_hello_send_sub(struct ospf_interface
*oi
, in_addr_t addr
)
3592 struct ospf_packet
*op
;
3593 uint16_t length
= OSPF_HEADER_SIZE
;
3595 op
= ospf_packet_new(oi
->ifp
->mtu
);
3597 /* Prepare OSPF common header. */
3598 ospf_make_header(OSPF_MSG_HELLO
, oi
, op
->s
);
3600 /* Prepare OSPF Hello body. */
3601 length
+= ospf_make_hello(oi
, op
->s
);
3602 if (length
== OSPF_HEADER_SIZE
) {
3603 /* Hello overshooting MTU */
3604 ospf_packet_free(op
);
3608 /* Fill OSPF header. */
3609 ospf_fill_header(oi
, op
->s
, length
);
3611 /* Set packet length. */
3612 op
->length
= length
;
3614 op
->dst
.s_addr
= addr
;
3616 if (IS_DEBUG_OSPF_EVENT
) {
3617 if (oi
->ospf
->vrf_id
)
3619 "%s: Hello Tx interface %s ospf vrf %s id %u",
3620 __PRETTY_FUNCTION__
, oi
->ifp
->name
,
3621 ospf_vrf_id_to_name(oi
->ospf
->vrf_id
),
3624 /* Add packet to the top of the interface output queue, so that they
3625 * can't get delayed by things like long queues of LS Update packets
3627 ospf_packet_add_top(oi
, op
);
3629 /* Hook thread to write packet. */
3630 OSPF_ISM_WRITE_ON(oi
->ospf
);
3633 static void ospf_poll_send(struct ospf_nbr_nbma
*nbr_nbma
)
3635 struct ospf_interface
*oi
;
3640 /* If this is passive interface, do not send OSPF Hello. */
3641 if (OSPF_IF_PASSIVE_STATUS(oi
) == OSPF_IF_PASSIVE
)
3644 if (oi
->type
!= OSPF_IFTYPE_NBMA
)
3647 if (nbr_nbma
->nbr
!= NULL
&& nbr_nbma
->nbr
->state
!= NSM_Down
)
3650 if (PRIORITY(oi
) == 0)
3653 if (nbr_nbma
->priority
== 0 && oi
->state
!= ISM_DR
3654 && oi
->state
!= ISM_Backup
)
3657 ospf_hello_send_sub(oi
, nbr_nbma
->addr
.s_addr
);
3660 int ospf_poll_timer(struct thread
*thread
)
3662 struct ospf_nbr_nbma
*nbr_nbma
;
3664 nbr_nbma
= THREAD_ARG(thread
);
3665 nbr_nbma
->t_poll
= NULL
;
3667 if (IS_DEBUG_OSPF(nsm
, NSM_TIMERS
))
3668 zlog_debug("NSM[%s:%s]: Timer (Poll timer expire)",
3669 IF_NAME(nbr_nbma
->oi
), inet_ntoa(nbr_nbma
->addr
));
3671 ospf_poll_send(nbr_nbma
);
3673 if (nbr_nbma
->v_poll
> 0)
3674 OSPF_POLL_TIMER_ON(nbr_nbma
->t_poll
, ospf_poll_timer
,
3681 int ospf_hello_reply_timer(struct thread
*thread
)
3683 struct ospf_neighbor
*nbr
;
3685 nbr
= THREAD_ARG(thread
);
3686 nbr
->t_hello_reply
= NULL
;
3690 if (IS_DEBUG_OSPF(nsm
, NSM_TIMERS
))
3691 zlog_debug("NSM[%s:%s]: Timer (hello-reply timer expire)",
3692 IF_NAME(nbr
->oi
), inet_ntoa(nbr
->router_id
));
3694 ospf_hello_send_sub(nbr
->oi
, nbr
->address
.u
.prefix4
.s_addr
);
3699 /* Send OSPF Hello. */
3700 void ospf_hello_send(struct ospf_interface
*oi
)
3702 /* If this is passive interface, do not send OSPF Hello. */
3703 if (OSPF_IF_PASSIVE_STATUS(oi
) == OSPF_IF_PASSIVE
)
3706 if (oi
->type
== OSPF_IFTYPE_NBMA
) {
3707 struct ospf_neighbor
*nbr
;
3708 struct route_node
*rn
;
3710 for (rn
= route_top(oi
->nbrs
); rn
; rn
= route_next(rn
))
3711 if ((nbr
= rn
->info
))
3712 if (nbr
!= oi
->nbr_self
)
3713 if (nbr
->state
!= NSM_Down
) {
3714 /* RFC 2328 Section 9.5.1
3715 If the router is not
3716 eligible to become Designated
3718 it must periodically send
3719 Hello Packets to both the
3720 Designated Router and the
3721 Backup Designated Router (if
3724 if (PRIORITY(oi
) == 0
3735 /* If the router is eligible to
3736 become Designated Router, it
3737 must periodically send Hello
3738 Packets to all neighbors that
3739 are also eligible. In
3740 addition, if the router is
3742 Designated Router or Backup
3743 Designated Router, it must
3745 send periodic Hello Packets
3746 to all other neighbors. */
3748 if (nbr
->priority
== 0
3749 && oi
->state
== ISM_DROther
)
3751 /* if oi->state == Waiting, send
3752 * hello to all neighbors */
3753 ospf_hello_send_sub(
3755 nbr
->address
.u
.prefix4
3759 /* Decide destination address. */
3760 if (oi
->type
== OSPF_IFTYPE_VIRTUALLINK
)
3761 ospf_hello_send_sub(oi
, oi
->vl_data
->peer_addr
.s_addr
);
3763 ospf_hello_send_sub(oi
, htonl(OSPF_ALLSPFROUTERS
));
3767 /* Send OSPF Database Description. */
3768 void ospf_db_desc_send(struct ospf_neighbor
*nbr
)
3770 struct ospf_interface
*oi
;
3771 struct ospf_packet
*op
;
3772 uint16_t length
= OSPF_HEADER_SIZE
;
3775 op
= ospf_packet_new(oi
->ifp
->mtu
);
3777 /* Prepare OSPF common header. */
3778 ospf_make_header(OSPF_MSG_DB_DESC
, oi
, op
->s
);
3780 /* Prepare OSPF Database Description body. */
3781 length
+= ospf_make_db_desc(oi
, nbr
, op
->s
);
3783 /* Fill OSPF header. */
3784 ospf_fill_header(oi
, op
->s
, length
);
3786 /* Set packet length. */
3787 op
->length
= length
;
3789 /* Decide destination address. */
3790 if (oi
->type
== OSPF_IFTYPE_POINTOPOINT
)
3791 op
->dst
.s_addr
= htonl(OSPF_ALLSPFROUTERS
);
3793 op
->dst
= nbr
->address
.u
.prefix4
;
3795 /* Add packet to the interface output queue. */
3796 ospf_packet_add(oi
, op
);
3798 /* Hook thread to write packet. */
3799 OSPF_ISM_WRITE_ON(oi
->ospf
);
3801 /* Remove old DD packet, then copy new one and keep in neighbor
3804 ospf_packet_free(nbr
->last_send
);
3805 nbr
->last_send
= ospf_packet_dup(op
);
3806 monotime(&nbr
->last_send_ts
);
3809 /* Re-send Database Description. */
3810 void ospf_db_desc_resend(struct ospf_neighbor
*nbr
)
3812 struct ospf_interface
*oi
;
3816 /* Add packet to the interface output queue. */
3817 ospf_packet_add(oi
, ospf_packet_dup(nbr
->last_send
));
3819 /* Hook thread to write packet. */
3820 OSPF_ISM_WRITE_ON(oi
->ospf
);
3823 /* Send Link State Request. */
3824 void ospf_ls_req_send(struct ospf_neighbor
*nbr
)
3826 struct ospf_interface
*oi
;
3827 struct ospf_packet
*op
;
3828 uint16_t length
= OSPF_HEADER_SIZE
;
3831 op
= ospf_packet_new(oi
->ifp
->mtu
);
3833 /* Prepare OSPF common header. */
3834 ospf_make_header(OSPF_MSG_LS_REQ
, oi
, op
->s
);
3836 /* Prepare OSPF Link State Request body. */
3837 length
+= ospf_make_ls_req(nbr
, op
->s
);
3838 if (length
== OSPF_HEADER_SIZE
) {
3839 ospf_packet_free(op
);
3843 /* Fill OSPF header. */
3844 ospf_fill_header(oi
, op
->s
, length
);
3846 /* Set packet length. */
3847 op
->length
= length
;
3849 /* Decide destination address. */
3850 if (oi
->type
== OSPF_IFTYPE_POINTOPOINT
)
3851 op
->dst
.s_addr
= htonl(OSPF_ALLSPFROUTERS
);
3853 op
->dst
= nbr
->address
.u
.prefix4
;
3855 /* Add packet to the interface output queue. */
3856 ospf_packet_add(oi
, op
);
3858 /* Hook thread to write packet. */
3859 OSPF_ISM_WRITE_ON(oi
->ospf
);
3861 /* Add Link State Request Retransmission Timer. */
3862 OSPF_NSM_TIMER_ON(nbr
->t_ls_req
, ospf_ls_req_timer
, nbr
->v_ls_req
);
3865 /* Send Link State Update with an LSA. */
3866 void ospf_ls_upd_send_lsa(struct ospf_neighbor
*nbr
, struct ospf_lsa
*lsa
,
3869 struct list
*update
;
3871 update
= list_new();
3873 listnode_add(update
, lsa
);
3875 /*ospf instance is going down, send self originated
3876 * MAXAGE LSA update to neighbors to remove from LSDB */
3877 if (nbr
->oi
->ospf
->inst_shutdown
&& IS_LSA_MAXAGE(lsa
))
3878 ospf_ls_upd_send(nbr
, update
, flag
, 1);
3880 ospf_ls_upd_send(nbr
, update
, flag
, 0);
3882 list_delete(&update
);
3885 /* Determine size for packet. Must be at least big enough to accomodate next
3886 * LSA on list, which may be bigger than MTU size.
3888 * Return pointer to new ospf_packet
3889 * NULL if we can not allocate, eg because LSA is bigger than imposed limit
3890 * on packet sizes (in which case offending LSA is deleted from update list)
3892 static struct ospf_packet
*ospf_ls_upd_packet_new(struct list
*update
,
3893 struct ospf_interface
*oi
)
3895 struct ospf_lsa
*lsa
;
3896 struct listnode
*ln
;
3898 static char warned
= 0;
3900 lsa
= listgetdata((ln
= listhead(update
)));
3903 if ((OSPF_LS_UPD_MIN_SIZE
+ ntohs(lsa
->data
->length
))
3904 > ospf_packet_max(oi
)) {
3908 "ospf_ls_upd_packet_new: oversized LSA encountered!"
3909 "will need to fragment. Not optimal. Try divide up"
3910 " your network with areas. Use 'debug ospf packet send'"
3911 " to see details, or look at 'show ip ospf database ..'");
3915 if (IS_DEBUG_OSPF_PACKET(0, SEND
))
3917 "ospf_ls_upd_packet_new: oversized LSA id:%s,"
3918 " %d bytes originated by %s, will be fragmented!",
3919 inet_ntoa(lsa
->data
->id
),
3920 ntohs(lsa
->data
->length
),
3921 inet_ntoa(lsa
->data
->adv_router
));
3924 * Allocate just enough to fit this LSA only, to avoid including
3926 * LSAs in fragmented LSA Updates.
3928 size
= ntohs(lsa
->data
->length
)
3929 + (oi
->ifp
->mtu
- ospf_packet_max(oi
))
3930 + OSPF_LS_UPD_MIN_SIZE
;
3932 size
= oi
->ifp
->mtu
;
3934 if (size
> OSPF_MAX_PACKET_SIZE
) {
3935 flog_warn(EC_OSPF_LARGE_LSA
,
3936 "ospf_ls_upd_packet_new: oversized LSA id:%s too big,"
3937 " %d bytes, packet size %ld, dropping it completely."
3938 " OSPF routing is broken!",
3939 inet_ntoa(lsa
->data
->id
), ntohs(lsa
->data
->length
),
3941 list_delete_node(update
, ln
);
3945 /* IP header is built up separately by ospf_write(). This means, that we
3947 * reduce the "affordable" size just calculated by length of an IP
3949 * This makes sure, that even if we manage to fill the payload with LSA
3951 * completely, the final packet (our data plus IP header) still fits
3953 * outgoing interface MTU. This correction isn't really meaningful for
3955 * oversized LSA, but for consistency the correction is done for both
3958 * P.S. OSPF_MAX_PACKET_SIZE above already includes IP header size
3960 return ospf_packet_new(size
- sizeof(struct ip
));
3963 static void ospf_ls_upd_queue_send(struct ospf_interface
*oi
,
3964 struct list
*update
, struct in_addr addr
,
3967 struct ospf_packet
*op
;
3968 uint16_t length
= OSPF_HEADER_SIZE
;
3970 if (IS_DEBUG_OSPF_EVENT
)
3971 zlog_debug("listcount = %d, [%s]dst %s", listcount(update
),
3972 IF_NAME(oi
), inet_ntoa(addr
));
3974 /* Check that we have really something to process */
3975 if (listcount(update
) == 0)
3978 op
= ospf_ls_upd_packet_new(update
, oi
);
3980 /* Prepare OSPF common header. */
3981 ospf_make_header(OSPF_MSG_LS_UPD
, oi
, op
->s
);
3983 /* Prepare OSPF Link State Update body.
3984 * Includes Type-7 translation.
3986 length
+= ospf_make_ls_upd(oi
, update
, op
->s
);
3988 /* Fill OSPF header. */
3989 ospf_fill_header(oi
, op
->s
, length
);
3991 /* Set packet length. */
3992 op
->length
= length
;
3994 /* Decide destination address. */
3995 if (oi
->type
== OSPF_IFTYPE_POINTOPOINT
)
3996 op
->dst
.s_addr
= htonl(OSPF_ALLSPFROUTERS
);
3998 op
->dst
.s_addr
= addr
.s_addr
;
4000 /* Add packet to the interface output queue. */
4001 ospf_packet_add(oi
, op
);
4002 /* Call ospf_write() right away to send ospf packets to neighbors */
4003 if (send_lsupd_now
) {
4004 struct thread os_packet_thd
;
4006 os_packet_thd
.arg
= (void *)oi
->ospf
;
4007 if (oi
->on_write_q
== 0) {
4008 listnode_add(oi
->ospf
->oi_write_q
, oi
);
4011 ospf_write(&os_packet_thd
);
4013 /* Hook thread to write packet. */
4014 OSPF_ISM_WRITE_ON(oi
->ospf
);
4018 static int ospf_ls_upd_send_queue_event(struct thread
*thread
)
4020 struct ospf_interface
*oi
= THREAD_ARG(thread
);
4021 struct route_node
*rn
;
4022 struct route_node
*rnext
;
4023 struct list
*update
;
4026 oi
->t_ls_upd_event
= NULL
;
4028 if (IS_DEBUG_OSPF_EVENT
)
4029 zlog_debug("ospf_ls_upd_send_queue start");
4031 for (rn
= route_top(oi
->ls_upd_queue
); rn
; rn
= rnext
) {
4032 rnext
= route_next(rn
);
4034 if (rn
->info
== NULL
)
4037 update
= (struct list
*)rn
->info
;
4039 ospf_ls_upd_queue_send(oi
, update
, rn
->p
.u
.prefix4
, 0);
4041 /* list might not be empty. */
4042 if (listcount(update
) == 0) {
4043 list_delete((struct list
**)&rn
->info
);
4044 route_unlock_node(rn
);
4050 if (IS_DEBUG_OSPF_EVENT
)
4052 "ospf_ls_upd_send_queue: update lists not cleared,"
4053 " %d nodes to try again, raising new event",
4055 oi
->t_ls_upd_event
= NULL
;
4056 thread_add_event(master
, ospf_ls_upd_send_queue_event
, oi
, 0,
4057 &oi
->t_ls_upd_event
);
4060 if (IS_DEBUG_OSPF_EVENT
)
4061 zlog_debug("ospf_ls_upd_send_queue stop");
4066 void ospf_ls_upd_send(struct ospf_neighbor
*nbr
, struct list
*update
, int flag
,
4069 struct ospf_interface
*oi
;
4070 struct ospf_lsa
*lsa
;
4071 struct prefix_ipv4 p
;
4072 struct route_node
*rn
;
4073 struct listnode
*node
;
4078 p
.prefixlen
= IPV4_MAX_BITLEN
;
4080 /* Decide destination address. */
4081 if (oi
->type
== OSPF_IFTYPE_VIRTUALLINK
)
4082 p
.prefix
= oi
->vl_data
->peer_addr
;
4083 else if (oi
->type
== OSPF_IFTYPE_POINTOPOINT
)
4084 p
.prefix
.s_addr
= htonl(OSPF_ALLSPFROUTERS
);
4085 else if (flag
== OSPF_SEND_PACKET_DIRECT
)
4086 p
.prefix
= nbr
->address
.u
.prefix4
;
4087 else if (oi
->state
== ISM_DR
|| oi
->state
== ISM_Backup
)
4088 p
.prefix
.s_addr
= htonl(OSPF_ALLSPFROUTERS
);
4089 else if (oi
->type
== OSPF_IFTYPE_POINTOMULTIPOINT
)
4090 p
.prefix
.s_addr
= htonl(OSPF_ALLSPFROUTERS
);
4092 p
.prefix
.s_addr
= htonl(OSPF_ALLDROUTERS
);
4094 if (oi
->type
== OSPF_IFTYPE_NBMA
) {
4095 if (flag
== OSPF_SEND_PACKET_INDIRECT
)
4098 "* LS-Update is directly sent on NBMA network.");
4099 if (IPV4_ADDR_SAME(&oi
->address
->u
.prefix4
, &p
.prefix
))
4100 flog_warn(EC_OSPF_PACKET
,
4101 "* LS-Update is sent to myself.");
4104 rn
= route_node_get(oi
->ls_upd_queue
, (struct prefix
*)&p
);
4106 if (rn
->info
== NULL
)
4107 rn
->info
= list_new();
4109 route_unlock_node(rn
);
4111 for (ALL_LIST_ELEMENTS_RO(update
, node
, lsa
))
4112 listnode_add(rn
->info
,
4113 ospf_lsa_lock(lsa
)); /* oi->ls_upd_queue */
4114 if (send_lsupd_now
) {
4115 struct list
*send_update_list
;
4116 struct route_node
*rnext
;
4118 for (rn
= route_top(oi
->ls_upd_queue
); rn
; rn
= rnext
) {
4119 rnext
= route_next(rn
);
4121 if (rn
->info
== NULL
)
4124 send_update_list
= (struct list
*)rn
->info
;
4126 ospf_ls_upd_queue_send(oi
, send_update_list
,
4127 rn
->p
.u
.prefix4
, 1);
4130 thread_add_event(master
, ospf_ls_upd_send_queue_event
, oi
, 0,
4131 &oi
->t_ls_upd_event
);
4134 static void ospf_ls_ack_send_list(struct ospf_interface
*oi
, struct list
*ack
,
4137 struct ospf_packet
*op
;
4138 uint16_t length
= OSPF_HEADER_SIZE
;
4140 op
= ospf_packet_new(oi
->ifp
->mtu
);
4142 /* Prepare OSPF common header. */
4143 ospf_make_header(OSPF_MSG_LS_ACK
, oi
, op
->s
);
4145 /* Prepare OSPF Link State Acknowledgment body. */
4146 length
+= ospf_make_ls_ack(oi
, ack
, op
->s
);
4148 /* Fill OSPF header. */
4149 ospf_fill_header(oi
, op
->s
, length
);
4151 /* Set packet length. */
4152 op
->length
= length
;
4154 /* Decide destination address. */
4155 if (oi
->type
== OSPF_IFTYPE_POINTOPOINT
)
4156 op
->dst
.s_addr
= htonl(OSPF_ALLSPFROUTERS
);
4158 op
->dst
.s_addr
= dst
.s_addr
;
4160 /* Add packet to the interface output queue. */
4161 ospf_packet_add(oi
, op
);
4163 /* Hook thread to write packet. */
4164 OSPF_ISM_WRITE_ON(oi
->ospf
);
4167 static int ospf_ls_ack_send_event(struct thread
*thread
)
4169 struct ospf_interface
*oi
= THREAD_ARG(thread
);
4171 oi
->t_ls_ack_direct
= NULL
;
4173 while (listcount(oi
->ls_ack_direct
.ls_ack
))
4174 ospf_ls_ack_send_list(oi
, oi
->ls_ack_direct
.ls_ack
,
4175 oi
->ls_ack_direct
.dst
);
4180 void ospf_ls_ack_send(struct ospf_neighbor
*nbr
, struct ospf_lsa
*lsa
)
4182 struct ospf_interface
*oi
= nbr
->oi
;
4184 if (listcount(oi
->ls_ack_direct
.ls_ack
) == 0)
4185 oi
->ls_ack_direct
.dst
= nbr
->address
.u
.prefix4
;
4187 listnode_add(oi
->ls_ack_direct
.ls_ack
, ospf_lsa_lock(lsa
));
4189 thread_add_event(master
, ospf_ls_ack_send_event
, oi
, 0,
4190 &oi
->t_ls_ack_direct
);
4193 /* Send Link State Acknowledgment delayed. */
4194 void ospf_ls_ack_send_delayed(struct ospf_interface
*oi
)
4198 /* Decide destination address. */
4199 /* RFC2328 Section 13.5 On non-broadcast
4200 networks, delayed Link State Acknowledgment packets must be
4201 unicast separately over each adjacency (i.e., neighbor whose
4202 state is >= Exchange). */
4203 if (oi
->type
== OSPF_IFTYPE_NBMA
) {
4204 struct ospf_neighbor
*nbr
;
4205 struct route_node
*rn
;
4207 for (rn
= route_top(oi
->nbrs
); rn
; rn
= route_next(rn
))
4208 if ((nbr
= rn
->info
) != NULL
)
4209 if (nbr
!= oi
->nbr_self
4210 && nbr
->state
>= NSM_Exchange
)
4211 while (listcount(oi
->ls_ack
))
4212 ospf_ls_ack_send_list(
4214 nbr
->address
.u
.prefix4
);
4217 if (oi
->type
== OSPF_IFTYPE_VIRTUALLINK
)
4218 dst
.s_addr
= oi
->vl_data
->peer_addr
.s_addr
;
4219 else if (oi
->state
== ISM_DR
|| oi
->state
== ISM_Backup
)
4220 dst
.s_addr
= htonl(OSPF_ALLSPFROUTERS
);
4221 else if (oi
->type
== OSPF_IFTYPE_POINTOPOINT
)
4222 dst
.s_addr
= htonl(OSPF_ALLSPFROUTERS
);
4223 else if (oi
->type
== OSPF_IFTYPE_POINTOMULTIPOINT
)
4224 dst
.s_addr
= htonl(OSPF_ALLSPFROUTERS
);
4226 dst
.s_addr
= htonl(OSPF_ALLDROUTERS
);
4228 while (listcount(oi
->ls_ack
))
4229 ospf_ls_ack_send_list(oi
, oi
->ls_ack
, dst
);
4233 * On pt-to-pt links, all OSPF control packets are sent to the multicast
4234 * address. As a result, the kernel does not need to learn the interface
4235 * MAC of the OSPF neighbor. However, in our world, this will delay
4236 * convergence. Take the case when due to a link flap, all routes now
4237 * want to use an interface which was deemed to be costlier prior to this
4238 * event. For routes that will be installed, the missing MAC will have
4239 * punt-to-CPU set on them. This may overload the CPU control path that
4240 * can be avoided if the MAC was known apriori.
4242 #define OSPF_PING_NBR_STR_MAX (BUFSIZ)
4243 void ospf_proactively_arp(struct ospf_neighbor
*nbr
)
4245 char ping_nbr
[OSPF_PING_NBR_STR_MAX
];
4248 if (!nbr
|| !nbr
->oi
|| !nbr
->oi
->ifp
)
4251 snprintf(ping_nbr
, sizeof(ping_nbr
),
4252 "ping -c 1 -I %s %s > /dev/null 2>&1 &", nbr
->oi
->ifp
->name
,
4253 inet_ntoa(nbr
->address
.u
.prefix4
));
4255 ret
= system(ping_nbr
);
4256 if (IS_DEBUG_OSPF_EVENT
)
4257 zlog_debug("Executed %s %s", ping_nbr
,
4258 ((ret
== 0) ? "successfully" : "but failed"));