back to topotato report
topotato coverage report
Current view: top level - ospfd - ospf_packet.c (source / functions) Hit Total Coverage
Test: aggregated run ( view descriptions ) Lines: 1132 1760 64.3 %
Date: 2023-02-24 19:38:44 Functions: 59 71 83.1 %

          Line data    Source code
       1             : /*
       2             :  * OSPF Sending and Receiving OSPF Packets.
       3             :  * Copyright (C) 1999, 2000 Toshiaki Takada
       4             :  *
       5             :  * This file is part of GNU Zebra.
       6             :  *
       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
      10             :  * later version.
      11             :  *
      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.
      16             :  *
      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
      20             :  */
      21             : 
      22             : #include <zebra.h>
      23             : 
      24             : #include "monotime.h"
      25             : #include "thread.h"
      26             : #include "memory.h"
      27             : #include "linklist.h"
      28             : #include "prefix.h"
      29             : #include "if.h"
      30             : #include "table.h"
      31             : #include "sockunion.h"
      32             : #include "stream.h"
      33             : #include "log.h"
      34             : #include "sockopt.h"
      35             : #include "checksum.h"
      36             : #ifdef CRYPTO_INTERNAL
      37             : #include "md5.h"
      38             : #endif
      39             : #include "vrf.h"
      40             : #include "lib_errors.h"
      41             : 
      42             : #include "ospfd/ospfd.h"
      43             : #include "ospfd/ospf_network.h"
      44             : #include "ospfd/ospf_interface.h"
      45             : #include "ospfd/ospf_ism.h"
      46             : #include "ospfd/ospf_asbr.h"
      47             : #include "ospfd/ospf_lsa.h"
      48             : #include "ospfd/ospf_lsdb.h"
      49             : #include "ospfd/ospf_neighbor.h"
      50             : #include "ospfd/ospf_nsm.h"
      51             : #include "ospfd/ospf_packet.h"
      52             : #include "ospfd/ospf_spf.h"
      53             : #include "ospfd/ospf_flood.h"
      54             : #include "ospfd/ospf_dump.h"
      55             : #include "ospfd/ospf_errors.h"
      56             : #include "ospfd/ospf_zebra.h"
      57             : #include "ospfd/ospf_gr.h"
      58             : 
      59             : /*
      60             :  * OSPF Fragmentation / fragmented writes
      61             :  *
      62             :  * ospfd can support writing fragmented packets, for cases where
      63             :  * kernel will not fragment IP_HDRINCL and/or multicast destined
      64             :  * packets (ie TTBOMK all kernels, BSD, SunOS, Linux). However,
      65             :  * SunOS, probably BSD too, clobber the user supplied IP ID and IP
      66             :  * flags fields, hence user-space fragmentation will not work.
      67             :  * Only Linux is known to leave IP header unmolested.
      68             :  * Further, fragmentation really should be done the kernel, which already
      69             :  * supports it, and which avoids nasty IP ID state problems.
      70             :  *
      71             :  * Fragmentation of OSPF packets can be required on networks with router
      72             :  * with many many interfaces active in one area, or on networks with links
      73             :  * with low MTUs.
      74             :  */
      75             : #ifdef GNU_LINUX
      76             : #define WANT_OSPF_WRITE_FRAGMENT
      77             : #endif
      78             : 
      79             : /* Packet Type String. */
      80             : const struct message ospf_packet_type_str[] = {
      81             :         {OSPF_MSG_HELLO, "Hello"},
      82             :         {OSPF_MSG_DB_DESC, "Database Description"},
      83             :         {OSPF_MSG_LS_REQ, "Link State Request"},
      84             :         {OSPF_MSG_LS_UPD, "Link State Update"},
      85             :         {OSPF_MSG_LS_ACK, "Link State Acknowledgment"},
      86             :         {0}};
      87             : 
      88             : /* Minimum (besides OSPF_HEADER_SIZE) lengths for OSPF packets of
      89             :    particular types, offset is the "type" field of a packet. */
      90             : static const uint16_t ospf_packet_minlen[] = {
      91             :         0,
      92             :         OSPF_HELLO_MIN_SIZE,
      93             :         OSPF_DB_DESC_MIN_SIZE,
      94             :         OSPF_LS_REQ_MIN_SIZE,
      95             :         OSPF_LS_UPD_MIN_SIZE,
      96             :         OSPF_LS_ACK_MIN_SIZE,
      97             : };
      98             : 
      99             : /* Minimum (besides OSPF_LSA_HEADER_SIZE) lengths for LSAs of particular
     100             :    types, offset is the "LSA type" field. */
     101             : static const uint16_t ospf_lsa_minlen[] = {
     102             :         0,                             /* OSPF_UNKNOWN_LSA */
     103             :         OSPF_ROUTER_LSA_MIN_SIZE,      /* OSPF_ROUTER_LSA */
     104             :         OSPF_NETWORK_LSA_MIN_SIZE,     /* OSPF_NETWORK_LSA */
     105             :         OSPF_SUMMARY_LSA_MIN_SIZE,     /* OSPF_SUMMARY_LSA */
     106             :         OSPF_SUMMARY_LSA_MIN_SIZE,     /* OSPF_ASBR_SUMMARY_LSA */
     107             :         OSPF_AS_EXTERNAL_LSA_MIN_SIZE, /* OSPF_AS_EXTERNAL_LSA */
     108             :         0,                             /* Unsupported, OSPF_GROUP_MEMBER_LSA */
     109             :         OSPF_AS_EXTERNAL_LSA_MIN_SIZE, /* OSPF_AS_NSSA_LSA */
     110             :         0,                             /* Unsupported, OSPF_EXTERNAL_ATTRIBURES_LSA */
     111             :         OSPF_OPAQUE_LSA_MIN_SIZE,      /* OSPF_OPAQUE_LINK_LSA */
     112             :         OSPF_OPAQUE_LSA_MIN_SIZE,      /* OSPF_OPAQUE_AREA_LSA */
     113             :         OSPF_OPAQUE_LSA_MIN_SIZE,      /* OSPF_OPAQUE_AS_LSA */
     114             : };
     115             : 
     116             : /* for ospf_check_auth() */
     117             : static int ospf_check_sum(struct ospf_header *);
     118             : 
     119             : /* OSPF authentication checking function */
     120        1007 : static int ospf_auth_type(struct ospf_interface *oi)
     121             : {
     122        1007 :         int auth_type;
     123             : 
     124        1007 :         if (OSPF_IF_PARAM(oi, auth_type) == OSPF_AUTH_NOTSET)
     125        1007 :                 auth_type = oi->area->auth_type;
     126             :         else
     127           0 :                 auth_type = OSPF_IF_PARAM(oi, auth_type);
     128             : 
     129             :         /* Handle case where MD5 key list is not configured aka Cisco */
     130        1007 :         if (auth_type == OSPF_AUTH_CRYPTOGRAPHIC
     131           0 :             && list_isempty(OSPF_IF_PARAM(oi, auth_crypt)))
     132           0 :                 return OSPF_AUTH_NULL;
     133             : 
     134             :         return auth_type;
     135             : }
     136             : 
     137         262 : static struct ospf_packet *ospf_packet_new(size_t size)
     138             : {
     139         262 :         struct ospf_packet *new;
     140             : 
     141         262 :         new = XCALLOC(MTYPE_OSPF_PACKET, sizeof(struct ospf_packet));
     142         262 :         new->s = stream_new(size);
     143             : 
     144         262 :         return new;
     145             : }
     146             : 
     147         262 : void ospf_packet_free(struct ospf_packet *op)
     148             : {
     149         262 :         if (op->s)
     150         262 :                 stream_free(op->s);
     151             : 
     152         262 :         XFREE(MTYPE_OSPF_PACKET, op);
     153         262 : }
     154             : 
     155          18 : struct ospf_fifo *ospf_fifo_new(void)
     156             : {
     157          18 :         struct ospf_fifo *new;
     158             : 
     159          18 :         new = XCALLOC(MTYPE_OSPF_FIFO, sizeof(struct ospf_fifo));
     160          18 :         return new;
     161             : }
     162             : 
     163             : /* Add new packet to fifo. */
     164          99 : void ospf_fifo_push(struct ospf_fifo *fifo, struct ospf_packet *op)
     165             : {
     166           0 :         if (fifo->tail)
     167          22 :                 fifo->tail->next = op;
     168             :         else
     169          77 :                 fifo->head = op;
     170             : 
     171          99 :         fifo->tail = op;
     172             : 
     173          99 :         fifo->count++;
     174           0 : }
     175             : 
     176             : /* Add new packet to head of fifo. */
     177         139 : static void ospf_fifo_push_head(struct ospf_fifo *fifo, struct ospf_packet *op)
     178             : {
     179         139 :         op->next = fifo->head;
     180             : 
     181         139 :         if (fifo->tail == NULL)
     182         134 :                 fifo->tail = op;
     183             : 
     184         139 :         fifo->head = op;
     185             : 
     186         139 :         fifo->count++;
     187             : }
     188             : 
     189             : /* Delete first packet from fifo. */
     190         238 : struct ospf_packet *ospf_fifo_pop(struct ospf_fifo *fifo)
     191             : {
     192         238 :         struct ospf_packet *op;
     193             : 
     194         238 :         op = fifo->head;
     195             : 
     196           0 :         if (op) {
     197         238 :                 fifo->head = op->next;
     198             : 
     199         238 :                 if (fifo->head == NULL)
     200         211 :                         fifo->tail = NULL;
     201             : 
     202           0 :                 fifo->count--;
     203             :         }
     204             : 
     205         238 :         return op;
     206             : }
     207             : 
     208             : /* Return first fifo entry. */
     209         476 : struct ospf_packet *ospf_fifo_head(struct ospf_fifo *fifo)
     210             : {
     211         476 :         return fifo->head;
     212             : }
     213             : 
     214             : /* Flush ospf packet fifo. */
     215          51 : void ospf_fifo_flush(struct ospf_fifo *fifo)
     216             : {
     217          51 :         struct ospf_packet *op;
     218          51 :         struct ospf_packet *next;
     219             : 
     220          51 :         for (op = fifo->head; op; op = next) {
     221           0 :                 next = op->next;
     222           0 :                 ospf_packet_free(op);
     223             :         }
     224          51 :         fifo->head = fifo->tail = NULL;
     225          51 :         fifo->count = 0;
     226          51 : }
     227             : 
     228             : /* Free ospf packet fifo. */
     229          18 : void ospf_fifo_free(struct ospf_fifo *fifo)
     230             : {
     231          18 :         ospf_fifo_flush(fifo);
     232             : 
     233          18 :         XFREE(MTYPE_OSPF_FIFO, fifo);
     234          18 : }
     235             : 
     236          99 : static void ospf_packet_add(struct ospf_interface *oi, struct ospf_packet *op)
     237             : {
     238             :         /* Add packet to end of queue. */
     239          99 :         ospf_fifo_push(oi->obuf, op);
     240             : 
     241             :         /* Debug of packet fifo*/
     242             :         /* ospf_fifo_debug (oi->obuf); */
     243          99 : }
     244             : 
     245         139 : static void ospf_packet_add_top(struct ospf_interface *oi,
     246             :                                 struct ospf_packet *op)
     247             : {
     248             :         /* Add packet to head of queue. */
     249         139 :         ospf_fifo_push_head(oi->obuf, op);
     250             : 
     251             :         /* Debug of packet fifo*/
     252             :         /* ospf_fifo_debug (oi->obuf); */
     253         139 : }
     254             : 
     255         238 : static void ospf_packet_delete(struct ospf_interface *oi)
     256             : {
     257         238 :         struct ospf_packet *op;
     258             : 
     259         238 :         op = ospf_fifo_pop(oi->obuf);
     260             : 
     261         238 :         if (op)
     262         238 :                 ospf_packet_free(op);
     263         238 : }
     264             : 
     265          20 : static struct ospf_packet *ospf_packet_dup(struct ospf_packet *op)
     266             : {
     267          20 :         struct ospf_packet *new;
     268             : 
     269          20 :         if (stream_get_endp(op->s) != op->length)
     270             :                 /* XXX size_t */
     271           0 :                 zlog_debug(
     272             :                         "ospf_packet_dup stream %lu ospf_packet %u size mismatch",
     273             :                         (unsigned long)STREAM_SIZE(op->s), op->length);
     274             : 
     275             :         /* Reserve space for MD5 authentication that may be added later. */
     276          20 :         new = ospf_packet_new(stream_get_endp(op->s) + OSPF_AUTH_MD5_SIZE);
     277          20 :         stream_copy(new->s, op->s);
     278             : 
     279          20 :         new->dst = op->dst;
     280          20 :         new->length = op->length;
     281             : 
     282          20 :         return new;
     283             : }
     284             : 
     285             : /* XXX inline */
     286         324 : static unsigned int ospf_packet_authspace(struct ospf_interface *oi)
     287             : {
     288         324 :         int auth = 0;
     289             : 
     290         324 :         if (ospf_auth_type(oi) == OSPF_AUTH_CRYPTOGRAPHIC)
     291           0 :                 auth = OSPF_AUTH_MD5_SIZE;
     292             : 
     293         324 :         return auth;
     294             : }
     295             : 
     296         276 : static unsigned int ospf_packet_max(struct ospf_interface *oi)
     297             : {
     298         276 :         int max;
     299             : 
     300         276 :         max = oi->ifp->mtu - ospf_packet_authspace(oi);
     301             : 
     302         276 :         max -= (OSPF_HEADER_SIZE + sizeof(struct ip));
     303             : 
     304         276 :         return max;
     305             : }
     306             : 
     307             : 
     308           0 : static int ospf_check_md5_digest(struct ospf_interface *oi,
     309             :                                  struct ospf_header *ospfh)
     310             : {
     311             : #ifdef CRYPTO_OPENSSL
     312             :         EVP_MD_CTX *ctx;
     313             : #elif CRYPTO_INTERNAL
     314           0 :         MD5_CTX ctx;
     315             : #endif
     316           0 :         unsigned char digest[OSPF_AUTH_MD5_SIZE];
     317           0 :         struct crypt_key *ck;
     318           0 :         struct ospf_neighbor *nbr;
     319           0 :         uint16_t length = ntohs(ospfh->length);
     320             : 
     321             :         /* Get secret key. */
     322           0 :         ck = ospf_crypt_key_lookup(OSPF_IF_PARAM(oi, auth_crypt),
     323           0 :                                    ospfh->u.crypt.key_id);
     324           0 :         if (ck == NULL) {
     325           0 :                 flog_warn(EC_OSPF_MD5, "interface %s: ospf_check_md5 no key %d",
     326             :                           IF_NAME(oi), ospfh->u.crypt.key_id);
     327           0 :                 return 0;
     328             :         }
     329             : 
     330             :         /* check crypto seqnum. */
     331           0 :         nbr = ospf_nbr_lookup_by_routerid(oi->nbrs, &ospfh->router_id);
     332             : 
     333           0 :         if (nbr
     334           0 :             && ntohl(nbr->crypt_seqnum) > ntohl(ospfh->u.crypt.crypt_seqnum)) {
     335           0 :                 flog_warn(
     336             :                         EC_OSPF_MD5,
     337             :                         "interface %s: ospf_check_md5 bad sequence %d (expect %d)",
     338             :                         IF_NAME(oi), ntohl(ospfh->u.crypt.crypt_seqnum),
     339             :                         ntohl(nbr->crypt_seqnum));
     340           0 :                 return 0;
     341             :         }
     342             : 
     343             :         /* Generate a digest for the ospf packet - their digest + our digest. */
     344             : #ifdef CRYPTO_OPENSSL
     345             :         unsigned int md5_size = OSPF_AUTH_MD5_SIZE;
     346             :         ctx = EVP_MD_CTX_new();
     347             :         EVP_DigestInit(ctx, EVP_md5());
     348             :         EVP_DigestUpdate(ctx, ospfh, length);
     349             :         EVP_DigestUpdate(ctx, ck->auth_key, OSPF_AUTH_MD5_SIZE);
     350             :         EVP_DigestFinal(ctx, digest, &md5_size);
     351             :         EVP_MD_CTX_free(ctx);
     352             : #elif CRYPTO_INTERNAL
     353           0 :         memset(&ctx, 0, sizeof(ctx));
     354           0 :         MD5Init(&ctx);
     355           0 :         MD5Update(&ctx, ospfh, length);
     356           0 :         MD5Update(&ctx, ck->auth_key, OSPF_AUTH_MD5_SIZE);
     357           0 :         MD5Final(digest, &ctx);
     358             : #endif
     359             : 
     360             :         /* compare the two */
     361           0 :         if (memcmp((caddr_t)ospfh + length, digest, OSPF_AUTH_MD5_SIZE)) {
     362           0 :                 flog_warn(EC_OSPF_MD5,
     363             :                           "interface %s: ospf_check_md5 checksum mismatch",
     364             :                           IF_NAME(oi));
     365           0 :                 return 0;
     366             :         }
     367             : 
     368             :         /* save neighbor's crypt_seqnum */
     369           0 :         if (nbr)
     370           0 :                 nbr->crypt_seqnum = ospfh->u.crypt.crypt_seqnum;
     371             :         return 1;
     372             : }
     373             : 
     374             : /* This function is called from ospf_write(), it will detect the
     375             :    authentication scheme and if it is MD5, it will change the sequence
     376             :    and update the MD5 digest. */
     377         238 : static int ospf_make_md5_digest(struct ospf_interface *oi,
     378             :                                 struct ospf_packet *op)
     379             : {
     380         238 :         struct ospf_header *ospfh;
     381         238 :         unsigned char digest[OSPF_AUTH_MD5_SIZE] = {0};
     382             : #ifdef CRYPTO_OPENSSL
     383             :         EVP_MD_CTX *ctx;
     384             : #elif CRYPTO_INTERNAL
     385         238 :         MD5_CTX ctx;
     386             : #endif
     387         238 :         void *ibuf;
     388         238 :         uint32_t t;
     389         238 :         struct crypt_key *ck;
     390         238 :         const uint8_t *auth_key;
     391             : 
     392         238 :         ibuf = STREAM_DATA(op->s);
     393         238 :         ospfh = (struct ospf_header *)ibuf;
     394             : 
     395         238 :         if (ntohs(ospfh->auth_type) != OSPF_AUTH_CRYPTOGRAPHIC)
     396             :                 return 0;
     397             : 
     398             :         /* We do this here so when we dup a packet, we don't have to
     399             :            waste CPU rewriting other headers.
     400             : 
     401             :            Note that frr_time /deliberately/ is not used here */
     402           0 :         t = (time(NULL) & 0xFFFFFFFF);
     403           0 :         if (t > oi->crypt_seqnum)
     404           0 :                 oi->crypt_seqnum = t;
     405             :         else
     406           0 :                 oi->crypt_seqnum++;
     407             : 
     408           0 :         ospfh->u.crypt.crypt_seqnum = htonl(oi->crypt_seqnum);
     409             : 
     410             :         /* Get MD5 Authentication key from auth_key list. */
     411           0 :         if (list_isempty(OSPF_IF_PARAM(oi, auth_crypt)))
     412             :                 auth_key = (const uint8_t *)digest;
     413             :         else {
     414           0 :                 ck = listgetdata(listtail(OSPF_IF_PARAM(oi, auth_crypt)));
     415           0 :                 auth_key = ck->auth_key;
     416             :         }
     417             : 
     418             :         /* Generate a digest for the entire packet + our secret key. */
     419             : #ifdef CRYPTO_OPENSSL
     420             :         unsigned int md5_size = OSPF_AUTH_MD5_SIZE;
     421             :         ctx = EVP_MD_CTX_new();
     422             :         EVP_DigestInit(ctx, EVP_md5());
     423             :         EVP_DigestUpdate(ctx, ibuf, ntohs(ospfh->length));
     424             :         EVP_DigestUpdate(ctx, auth_key, OSPF_AUTH_MD5_SIZE);
     425             :         EVP_DigestFinal(ctx, digest, &md5_size);
     426             :         EVP_MD_CTX_free(ctx);
     427             : #elif CRYPTO_INTERNAL
     428           0 :         memset(&ctx, 0, sizeof(ctx));
     429           0 :         MD5Init(&ctx);
     430           0 :         MD5Update(&ctx, ibuf, ntohs(ospfh->length));
     431           0 :         MD5Update(&ctx, auth_key, OSPF_AUTH_MD5_SIZE);
     432           0 :         MD5Final(digest, &ctx);
     433             : #endif
     434             : 
     435             :         /* Append md5 digest to the end of the stream. */
     436           0 :         stream_put(op->s, digest, OSPF_AUTH_MD5_SIZE);
     437             : 
     438             :         /* We do *NOT* increment the OSPF header length. */
     439           0 :         op->length = ntohs(ospfh->length) + OSPF_AUTH_MD5_SIZE;
     440             : 
     441           0 :         if (stream_get_endp(op->s) != op->length)
     442             :                 /* XXX size_t */
     443           0 :                 flog_warn(EC_OSPF_MD5,
     444             :                           "%s: length mismatch stream %lu ospf_packet %u",
     445             :                           __func__, (unsigned long)stream_get_endp(op->s),
     446             :                           op->length);
     447             : 
     448             :         return OSPF_AUTH_MD5_SIZE;
     449             : }
     450             : 
     451             : 
     452          26 : static void ospf_ls_req_timer(struct thread *thread)
     453             : {
     454          26 :         struct ospf_neighbor *nbr;
     455             : 
     456          26 :         nbr = THREAD_ARG(thread);
     457          26 :         nbr->t_ls_req = NULL;
     458             : 
     459             :         /* Send Link State Request. */
     460          26 :         if (ospf_ls_request_count(nbr))
     461           0 :                 ospf_ls_req_send(nbr);
     462             : 
     463             :         /* Set Link State Request retransmission timer. */
     464          26 :         OSPF_NSM_TIMER_ON(nbr->t_ls_req, ospf_ls_req_timer, nbr->v_ls_req);
     465          26 : }
     466             : 
     467           0 : void ospf_ls_req_event(struct ospf_neighbor *nbr)
     468             : {
     469           0 :         THREAD_OFF(nbr->t_ls_req);
     470           0 :         thread_add_event(master, ospf_ls_req_timer, nbr, 0, &nbr->t_ls_req);
     471           0 : }
     472             : 
     473             : /* Cyclic timer function.  Fist registered in ospf_nbr_new () in
     474             :    ospf_neighbor.c  */
     475          26 : void ospf_ls_upd_timer(struct thread *thread)
     476             : {
     477          26 :         struct ospf_neighbor *nbr;
     478             : 
     479          26 :         nbr = THREAD_ARG(thread);
     480          26 :         nbr->t_ls_upd = NULL;
     481             : 
     482             :         /* Send Link State Update. */
     483          26 :         if (ospf_ls_retransmit_count(nbr) > 0) {
     484          18 :                 struct list *update;
     485          18 :                 struct ospf_lsdb *lsdb;
     486          18 :                 int i;
     487          18 :                 int retransmit_interval;
     488             : 
     489          36 :                 retransmit_interval =
     490          18 :                         OSPF_IF_PARAM(nbr->oi, retransmit_interval);
     491             : 
     492          18 :                 lsdb = &nbr->ls_rxmt;
     493          18 :                 update = list_new();
     494             : 
     495         216 :                 for (i = OSPF_MIN_LSA; i < OSPF_MAX_LSA; i++) {
     496         198 :                         struct route_table *table = lsdb->type[i].db;
     497         198 :                         struct route_node *rn;
     498             : 
     499         232 :                         for (rn = route_top(table); rn; rn = route_next(rn)) {
     500          34 :                                 struct ospf_lsa *lsa;
     501             : 
     502          34 :                                 if ((lsa = rn->info) != NULL) {
     503             :                                         /* Don't retransmit an LSA if we
     504             :                                           received it within
     505             :                                           the last RxmtInterval seconds - this
     506             :                                           is to allow the
     507             :                                           neighbour a chance to acknowledge the
     508             :                                           LSA as it may
     509             :                                           have ben just received before the
     510             :                                           retransmit timer
     511             :                                           fired.  This is a small tweak to what
     512             :                                           is in the RFC,
     513             :                                           but it will cut out out a lot of
     514             :                                           retransmit traffic
     515             :                                           - MAG */
     516          29 :                                         if (monotime_since(&lsa->tv_recv, NULL)
     517          29 :                                             >= retransmit_interval * 1000000LL)
     518          12 :                                                 listnode_add(update, rn->info);
     519             :                                 }
     520             :                         }
     521             :                 }
     522             : 
     523          18 :                 if (listcount(update) > 0)
     524           8 :                         ospf_ls_upd_send(nbr, update, OSPF_SEND_PACKET_DIRECT,
     525             :                                          0);
     526          18 :                 list_delete(&update);
     527             :         }
     528             : 
     529             :         /* Set LS Update retransmission timer. */
     530          26 :         OSPF_NSM_TIMER_ON(nbr->t_ls_upd, ospf_ls_upd_timer, nbr->v_ls_upd);
     531          26 : }
     532             : 
     533         103 : void ospf_ls_ack_timer(struct thread *thread)
     534             : {
     535         103 :         struct ospf_interface *oi;
     536             : 
     537         103 :         oi = THREAD_ARG(thread);
     538         103 :         oi->t_ls_ack = NULL;
     539             : 
     540             :         /* Send Link State Acknowledgment. */
     541         103 :         if (listcount(oi->ls_ack) > 0)
     542          14 :                 ospf_ls_ack_send_delayed(oi);
     543             : 
     544             :         /* Set LS Ack timer. */
     545         103 :         OSPF_ISM_TIMER_ON(oi->t_ls_ack, ospf_ls_ack_timer, oi->v_ls_ack);
     546         103 : }
     547             : 
     548             : #ifdef WANT_OSPF_WRITE_FRAGMENT
     549           0 : static void ospf_write_frags(int fd, struct ospf_packet *op, struct ip *iph,
     550             :                              struct msghdr *msg, unsigned int maxdatasize,
     551             :                              unsigned int mtu, int flags, uint8_t type)
     552             : {
     553             : #define OSPF_WRITE_FRAG_SHIFT 3
     554           0 :         uint16_t offset;
     555           0 :         struct iovec *iovp;
     556           0 :         int ret;
     557             : 
     558           0 :         assert(op->length == stream_get_endp(op->s));
     559           0 :         assert(msg->msg_iovlen == 2);
     560             : 
     561             :         /* we can but try.
     562             :          *
     563             :          * SunOS, BSD and BSD derived kernels likely will clear ip_id, as
     564             :          * well as the IP_MF flag, making this all quite pointless.
     565             :          *
     566             :          * However, for a system on which IP_MF is left alone, and ip_id left
     567             :          * alone or else which sets same ip_id for each fragment this might
     568             :          * work, eg linux.
     569             :          *
     570             :          * XXX-TODO: It would be much nicer to have the kernel's use their
     571             :          * existing fragmentation support to do this for us. Bugs/RFEs need to
     572             :          * be raised against the various kernels.
     573             :          */
     574             : 
     575             :         /* set More Frag */
     576           0 :         iph->ip_off |= IP_MF;
     577             : 
     578             :         /* ip frag offset is expressed in units of 8byte words */
     579           0 :         offset = maxdatasize >> OSPF_WRITE_FRAG_SHIFT;
     580             : 
     581           0 :         iovp = &msg->msg_iov[1];
     582             : 
     583           0 :         while ((stream_get_endp(op->s) - stream_get_getp(op->s))
     584           0 :                > maxdatasize) {
     585             :                 /* data length of this frag is to next offset value */
     586           0 :                 iovp->iov_len = offset << OSPF_WRITE_FRAG_SHIFT;
     587           0 :                 iph->ip_len = iovp->iov_len + sizeof(struct ip);
     588           0 :                 assert(iph->ip_len <= mtu);
     589             : 
     590           0 :                 sockopt_iphdrincl_swab_htosys(iph);
     591             : 
     592           0 :                 ret = sendmsg(fd, msg, flags);
     593             : 
     594           0 :                 sockopt_iphdrincl_swab_systoh(iph);
     595             : 
     596           0 :                 if (ret < 0)
     597           0 :                         flog_err(
     598             :                                 EC_LIB_SOCKET,
     599             :                                 "*** %s: sendmsg failed to %pI4, id %d, off %d, len %d, mtu %u failed with %s",
     600             :                                 __func__, &iph->ip_dst, iph->ip_id, iph->ip_off,
     601             :                                 iph->ip_len, mtu, safe_strerror(errno));
     602             : 
     603           0 :                 if (IS_DEBUG_OSPF_PACKET(type - 1, SEND)) {
     604           0 :                         zlog_debug("%s: sent id %d, off %d, len %d to %pI4",
     605             :                                    __func__, iph->ip_id, iph->ip_off,
     606             :                                    iph->ip_len, &iph->ip_dst);
     607             :                 }
     608             : 
     609           0 :                 iph->ip_off += offset;
     610           0 :                 stream_forward_getp(op->s, iovp->iov_len);
     611           0 :                 iovp->iov_base = stream_pnt(op->s);
     612             :         }
     613             : 
     614             :         /* setup for final fragment */
     615           0 :         iovp->iov_len = stream_get_endp(op->s) - stream_get_getp(op->s);
     616           0 :         iph->ip_len = iovp->iov_len + sizeof(struct ip);
     617           0 :         iph->ip_off &= (~IP_MF);
     618           0 : }
     619             : #endif /* WANT_OSPF_WRITE_FRAGMENT */
     620             : 
     621         209 : static void ospf_write(struct thread *thread)
     622             : {
     623         209 :         struct ospf *ospf = THREAD_ARG(thread);
     624         209 :         struct ospf_interface *oi;
     625         209 :         struct ospf_packet *op;
     626         209 :         struct sockaddr_in sa_dst;
     627         209 :         struct ip iph;
     628         209 :         struct msghdr msg;
     629         209 :         struct iovec iov[2];
     630         209 :         uint8_t type;
     631         209 :         int ret;
     632         209 :         int flags = 0;
     633         209 :         struct listnode *node;
     634             : #ifdef WANT_OSPF_WRITE_FRAGMENT
     635         209 :         static uint16_t ipid = 0;
     636         209 :         uint16_t maxdatasize;
     637             : #endif /* WANT_OSPF_WRITE_FRAGMENT */
     638             : #define OSPF_WRITE_IPHL_SHIFT 2
     639         209 :         int pkt_count = 0;
     640             : 
     641             : #ifdef GNU_LINUX
     642         209 :         unsigned char cmsgbuf[64] = {};
     643         209 :         struct cmsghdr *cm = (struct cmsghdr *)cmsgbuf;
     644         209 :         struct in_pktinfo *pi;
     645             : #endif
     646             : 
     647         209 :         if (ospf->fd < 0 || ospf->oi_running == 0) {
     648           0 :                 if (IS_DEBUG_OSPF_EVENT)
     649           0 :                         zlog_debug("%s failed to send, fd %d, instance %u",
     650             :                                    __func__, ospf->fd, ospf->oi_running);
     651           0 :                 return;
     652             :         }
     653             : 
     654         209 :         node = listhead(ospf->oi_write_q);
     655         209 :         assert(node);
     656         209 :         oi = listgetdata(node);
     657             : 
     658             : #ifdef WANT_OSPF_WRITE_FRAGMENT
     659             :         /* seed ipid static with low order bits of time */
     660         209 :         if (ipid == 0)
     661           4 :                 ipid = (time(NULL) & 0xffff);
     662             : #endif /* WANT_OSPF_WRITE_FRAGMENT */
     663             : 
     664         447 :         while ((pkt_count < ospf->write_oi_count) && oi) {
     665         238 :                 pkt_count++;
     666             : #ifdef WANT_OSPF_WRITE_FRAGMENT
     667             :                 /* convenience - max OSPF data per packet */
     668         238 :                 maxdatasize = oi->ifp->mtu - sizeof(struct ip);
     669             : #endif /* WANT_OSPF_WRITE_FRAGMENT */
     670             :                 /* Get one packet from queue. */
     671         238 :                 op = ospf_fifo_head(oi->obuf);
     672         238 :                 assert(op);
     673         238 :                 assert(op->length >= OSPF_HEADER_SIZE);
     674             : 
     675         238 :                 if (op->dst.s_addr == htonl(OSPF_ALLSPFROUTERS)
     676          56 :                     || op->dst.s_addr == htonl(OSPF_ALLDROUTERS))
     677         193 :                         ospf_if_ipmulticast(ospf, oi->address,
     678             :                                             oi->ifp->ifindex);
     679             : 
     680             :                 /* Rewrite the md5 signature & update the seq */
     681         238 :                 ospf_make_md5_digest(oi, op);
     682             : 
     683             :                 /* Retrieve OSPF packet type. */
     684         238 :                 stream_set_getp(op->s, 1);
     685         238 :                 type = stream_getc(op->s);
     686             : 
     687             :                 /* reset get pointer */
     688         238 :                 stream_set_getp(op->s, 0);
     689             : 
     690         238 :                 memset(&iph, 0, sizeof(iph));
     691         238 :                 memset(&sa_dst, 0, sizeof(sa_dst));
     692             : 
     693         238 :                 sa_dst.sin_family = AF_INET;
     694             : #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
     695             :                 sa_dst.sin_len = sizeof(sa_dst);
     696             : #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
     697         238 :                 sa_dst.sin_addr = op->dst;
     698         238 :                 sa_dst.sin_port = htons(0);
     699             : 
     700             :                 /* Set DONTROUTE flag if dst is unicast. */
     701         238 :                 if (oi->type != OSPF_IFTYPE_VIRTUALLINK)
     702         238 :                         if (!IN_MULTICAST(htonl(op->dst.s_addr)))
     703         238 :                                 flags = MSG_DONTROUTE;
     704             : 
     705         238 :                 iph.ip_hl = sizeof(struct ip) >> OSPF_WRITE_IPHL_SHIFT;
     706             :                 /* it'd be very strange for header to not be 4byte-word aligned
     707             :                  * but.. */
     708         238 :                 if (sizeof(struct ip)
     709             :                     > (unsigned int)(iph.ip_hl << OSPF_WRITE_IPHL_SHIFT))
     710             :                         iph.ip_hl++; /* we presume sizeof(struct ip) cant
     711             :                                         overflow ip_hl.. */
     712             : 
     713         238 :                 iph.ip_v = IPVERSION;
     714         238 :                 iph.ip_tos = IPTOS_PREC_INTERNETCONTROL;
     715         238 :                 iph.ip_len = (iph.ip_hl << OSPF_WRITE_IPHL_SHIFT) + op->length;
     716             : 
     717             : #if defined(__DragonFly__)
     718             :                 /*
     719             :                  * DragonFly's raw socket expects ip_len/ip_off in network byte
     720             :                  * order.
     721             :                  */
     722             :                 iph.ip_len = htons(iph.ip_len);
     723             : #endif
     724             : 
     725             : #ifdef WANT_OSPF_WRITE_FRAGMENT
     726             :                 /* XXX-MT: not thread-safe at all..
     727             :                  * XXX: this presumes this is only programme sending OSPF
     728             :                  * packets
     729             :                  * otherwise, no guarantee ipid will be unique
     730             :                  */
     731         238 :                 iph.ip_id = ++ipid;
     732             : #endif /* WANT_OSPF_WRITE_FRAGMENT */
     733             : 
     734         238 :                 iph.ip_off = 0;
     735         238 :                 if (oi->type == OSPF_IFTYPE_VIRTUALLINK)
     736           0 :                         iph.ip_ttl = OSPF_VL_IP_TTL;
     737             :                 else
     738         238 :                         iph.ip_ttl = OSPF_IP_TTL;
     739         238 :                 iph.ip_p = IPPROTO_OSPFIGP;
     740         238 :                 iph.ip_sum = 0;
     741         238 :                 iph.ip_src.s_addr = oi->address->u.prefix4.s_addr;
     742         238 :                 iph.ip_dst.s_addr = op->dst.s_addr;
     743             : 
     744         238 :                 memset(&msg, 0, sizeof(msg));
     745         238 :                 msg.msg_name = (caddr_t)&sa_dst;
     746         238 :                 msg.msg_namelen = sizeof(sa_dst);
     747         238 :                 msg.msg_iov = iov;
     748         238 :                 msg.msg_iovlen = 2;
     749             : 
     750         238 :                 iov[0].iov_base = (char *)&iph;
     751         238 :                 iov[0].iov_len = iph.ip_hl << OSPF_WRITE_IPHL_SHIFT;
     752         238 :                 iov[1].iov_base = stream_pnt(op->s);
     753         238 :                 iov[1].iov_len = op->length;
     754             : 
     755             : #ifdef GNU_LINUX
     756         238 :                 msg.msg_control = (caddr_t)cm;
     757         238 :                 cm->cmsg_level = SOL_IP;
     758         238 :                 cm->cmsg_type = IP_PKTINFO;
     759         238 :                 cm->cmsg_len = CMSG_LEN(sizeof(struct in_pktinfo));
     760         238 :                 pi = (struct in_pktinfo *)CMSG_DATA(cm);
     761         238 :                 pi->ipi_ifindex = oi->ifp->ifindex;
     762             : 
     763         238 :                 msg.msg_controllen = cm->cmsg_len;
     764             : #endif
     765             : 
     766             : /* Sadly we can not rely on kernels to fragment packets
     767             :  * because of either IP_HDRINCL and/or multicast
     768             :  * destination being set.
     769             :  */
     770             : 
     771             : #ifdef WANT_OSPF_WRITE_FRAGMENT
     772         238 :                 if (op->length > maxdatasize)
     773           0 :                         ospf_write_frags(ospf->fd, op, &iph, &msg, maxdatasize,
     774             :                                          oi->ifp->mtu, flags, type);
     775             : #endif /* WANT_OSPF_WRITE_FRAGMENT */
     776             : 
     777             :                 /* send final fragment (could be first) */
     778         238 :                 sockopt_iphdrincl_swab_htosys(&iph);
     779         238 :                 ret = sendmsg(ospf->fd, &msg, flags);
     780         238 :                 sockopt_iphdrincl_swab_systoh(&iph);
     781         238 :                 if (IS_DEBUG_OSPF_EVENT)
     782         238 :                         zlog_debug(
     783             :                                 "%s to %pI4, id %d, off %d, len %d, interface %s, mtu %u:",
     784             :                                 __func__, &iph.ip_dst, iph.ip_id, iph.ip_off,
     785             :                                 iph.ip_len, oi->ifp->name, oi->ifp->mtu);
     786             : 
     787             :                 /* sendmsg will return EPERM if firewall is blocking sending.
     788             :                  * This is a normal situation when 'ip nhrp map multicast xxx'
     789             :                  * is being used to send multicast packets to DMVPN peers. In
     790             :                  * that case the original message is blocked with iptables rule
     791             :                  * causing the EPERM result
     792             :                  */
     793         238 :                 if (ret < 0 && errno != EPERM)
     794           0 :                         flog_err(
     795             :                                 EC_LIB_SOCKET,
     796             :                                 "*** sendmsg in %s failed to %pI4, id %d, off %d, len %d, interface %s, mtu %u: %s",
     797             :                                 __func__, &iph.ip_dst, iph.ip_id, iph.ip_off,
     798             :                                 iph.ip_len, oi->ifp->name, oi->ifp->mtu,
     799             :                                 safe_strerror(errno));
     800             : 
     801             :                 /* Show debug sending packet. */
     802         238 :                 if (IS_DEBUG_OSPF_PACKET(type - 1, SEND)) {
     803           0 :                         if (IS_DEBUG_OSPF_PACKET(type - 1, DETAIL)) {
     804           0 :                                 zlog_debug(
     805             :                                         "-----------------------------------------------------");
     806           0 :                                 stream_set_getp(op->s, 0);
     807           0 :                                 ospf_packet_dump(op->s);
     808             :                         }
     809             : 
     810           0 :                         zlog_debug("%s sent to [%pI4] via [%s].",
     811             :                                    lookup_msg(ospf_packet_type_str, type, NULL),
     812             :                                    &op->dst, IF_NAME(oi));
     813             : 
     814           0 :                         if (IS_DEBUG_OSPF_PACKET(type - 1, DETAIL))
     815           0 :                                 zlog_debug(
     816             :                                         "-----------------------------------------------------");
     817             :                 }
     818             : 
     819         238 :                 switch (type) {
     820         139 :                 case OSPF_MSG_HELLO:
     821         139 :                         oi->hello_out++;
     822         139 :                         break;
     823          20 :                 case OSPF_MSG_DB_DESC:
     824          20 :                         oi->db_desc_out++;
     825          20 :                         break;
     826           8 :                 case OSPF_MSG_LS_REQ:
     827           8 :                         oi->ls_req_out++;
     828           8 :                         break;
     829          48 :                 case OSPF_MSG_LS_UPD:
     830          48 :                         oi->ls_upd_out++;
     831          48 :                         break;
     832          23 :                 case OSPF_MSG_LS_ACK:
     833          23 :                         oi->ls_ack_out++;
     834          23 :                         break;
     835             :                 default:
     836             :                         break;
     837             :                 }
     838             : 
     839             :                 /* Now delete packet from queue. */
     840         238 :                 ospf_packet_delete(oi);
     841             : 
     842             :                 /* Move this interface to the tail of write_q to
     843             :                        serve everyone in a round robin fashion */
     844         238 :                 list_delete_node(ospf->oi_write_q, node);
     845         238 :                 if (ospf_fifo_head(oi->obuf) == NULL) {
     846         211 :                         oi->on_write_q = 0;
     847         211 :                         oi = NULL;
     848             :                 } else
     849          27 :                         listnode_add(ospf->oi_write_q, oi);
     850             : 
     851             :                 /* Setup to service from the head of the queue again */
     852         238 :                 if (!list_isempty(ospf->oi_write_q)) {
     853          29 :                         node = listhead(ospf->oi_write_q);
     854          29 :                         oi = listgetdata(node);
     855             :                 }
     856             :         }
     857             : 
     858             :         /* If packets still remain in queue, call write thread. */
     859         209 :         if (!list_isempty(ospf->oi_write_q))
     860           0 :                 thread_add_write(master, ospf_write, ospf, ospf->fd,
     861             :                                  &ospf->t_write);
     862             : }
     863             : 
     864             : /* OSPF Hello message read -- RFC2328 Section 10.5. */
     865          94 : static void ospf_hello(struct ip *iph, struct ospf_header *ospfh,
     866             :                        struct stream *s, struct ospf_interface *oi, int size)
     867             : {
     868          94 :         struct ospf_hello *hello;
     869          94 :         struct ospf_neighbor *nbr;
     870          94 :         int old_state;
     871          94 :         struct prefix p;
     872             : 
     873             :         /* increment statistics. */
     874          94 :         oi->hello_in++;
     875             : 
     876          94 :         hello = (struct ospf_hello *)stream_pnt(s);
     877             : 
     878             :         /* If Hello is myself, silently discard. */
     879          94 :         if (IPV4_ADDR_SAME(&ospfh->router_id, &oi->ospf->router_id)) {
     880           0 :                 if (IS_DEBUG_OSPF_PACKET(ospfh->type - 1, RECV)) {
     881           0 :                         zlog_debug(
     882             :                                 "ospf_header[%s/%pI4]: selforiginated, dropping.",
     883             :                                 lookup_msg(ospf_packet_type_str, ospfh->type,
     884             :                                            NULL),
     885             :                                 &iph->ip_src);
     886             :                 }
     887           4 :                 return;
     888             :         }
     889             : 
     890             :         /* get neighbor prefix. */
     891          94 :         p.family = AF_INET;
     892          94 :         p.prefixlen = ip_masklen(hello->network_mask);
     893          94 :         p.u.prefix4 = iph->ip_src;
     894             : 
     895             :         /* Compare network mask. */
     896             :         /* Checking is ignored for Point-to-Point and Virtual link. */
     897             :         /* Checking is also ignored for Point-to-Multipoint with /32 prefix */
     898          94 :         if (oi->type != OSPF_IFTYPE_POINTOPOINT
     899          94 :             && oi->type != OSPF_IFTYPE_VIRTUALLINK
     900          94 :             && !(oi->type == OSPF_IFTYPE_POINTOMULTIPOINT
     901           0 :                  && oi->address->prefixlen == IPV4_MAX_BITLEN))
     902          94 :                 if (oi->address->prefixlen != p.prefixlen) {
     903           0 :                         flog_warn(
     904             :                                 EC_OSPF_PACKET,
     905             :                                 "Packet %pI4 [Hello:RECV]: NetworkMask mismatch on %s (configured prefix length is %d, but hello packet indicates %d).",
     906             :                                 &ospfh->router_id, IF_NAME(oi),
     907             :                                 (int)oi->address->prefixlen, (int)p.prefixlen);
     908           0 :                         return;
     909             :                 }
     910             : 
     911             :         /* Compare Router Dead Interval. */
     912          94 :         if (OSPF_IF_PARAM(oi, v_wait) != ntohl(hello->dead_interval)) {
     913           0 :                 flog_warn(EC_OSPF_PACKET,
     914             :                           "Packet %pI4 [Hello:RECV]: RouterDeadInterval mismatch (expected %u, but received %u).",
     915             :                           &ospfh->router_id,
     916             :                           OSPF_IF_PARAM(oi, v_wait),
     917             :                           ntohl(hello->dead_interval));
     918           0 :                 return;
     919             :         }
     920             : 
     921             :         /* Compare Hello Interval - ignored if fast-hellos are set. */
     922          94 :         if (OSPF_IF_PARAM(oi, fast_hello) == 0) {
     923          94 :                 if (OSPF_IF_PARAM(oi, v_hello)
     924          94 :                     != ntohs(hello->hello_interval)) {
     925           0 :                         flog_warn(
     926             :                                 EC_OSPF_PACKET,
     927             :                                 "Packet %pI4 [Hello:RECV]: HelloInterval mismatch (expected %u, but received %u).",
     928             :                                 &ospfh->router_id,
     929             :                                 OSPF_IF_PARAM(oi, v_hello),
     930             :                                 ntohs(hello->hello_interval));
     931           0 :                         return;
     932             :                 }
     933             :         }
     934             : 
     935          94 :         if (IS_DEBUG_OSPF_EVENT)
     936          94 :                 zlog_debug("Packet %pI4 [Hello:RECV]: Options %s vrf %s",
     937             :                            &ospfh->router_id,
     938             :                            ospf_options_dump(hello->options),
     939             :                            ospf_vrf_id_to_name(oi->ospf->vrf_id));
     940             : 
     941             : /* Compare options. */
     942             : #define REJECT_IF_TBIT_ON       1 /* XXX */
     943             : #ifdef REJECT_IF_TBIT_ON
     944          94 :         if (CHECK_FLAG(hello->options, OSPF_OPTION_MT)) {
     945             :                 /*
     946             :                  * This router does not support non-zero TOS.
     947             :                  * Drop this Hello packet not to establish neighbor
     948             :                  * relationship.
     949             :                  */
     950           0 :                 flog_warn(EC_OSPF_PACKET,
     951             :                           "Packet %pI4 [Hello:RECV]: T-bit on, drop it.",
     952             :                           &ospfh->router_id);
     953           0 :                 return;
     954             :         }
     955             : #endif /* REJECT_IF_TBIT_ON */
     956             : 
     957          94 :         if (CHECK_FLAG(oi->ospf->config, OSPF_OPAQUE_CAPABLE)
     958           0 :             && CHECK_FLAG(hello->options, OSPF_OPTION_O)) {
     959             :                 /*
     960             :                  * This router does know the correct usage of O-bit
     961             :                  * the bit should be set in DD packet only.
     962             :                  */
     963           0 :                 flog_warn(EC_OSPF_PACKET,
     964             :                           "Packet %pI4 [Hello:RECV]: O-bit abuse?",
     965             :                           &ospfh->router_id);
     966             : #ifdef STRICT_OBIT_USAGE_CHECK
     967             :                 return; /* Reject this packet. */
     968             : #else                   /* STRICT_OBIT_USAGE_CHECK */
     969           0 :                 UNSET_FLAG(hello->options, OSPF_OPTION_O); /* Ignore O-bit. */
     970             : #endif                  /* STRICT_OBIT_USAGE_CHECK */
     971             :         }
     972             : 
     973             :         /* new for NSSA is to ensure that NP is on and E is off */
     974             : 
     975          94 :         if (oi->area->external_routing == OSPF_AREA_NSSA) {
     976           0 :                 if (!(CHECK_FLAG(OPTIONS(oi), OSPF_OPTION_NP)
     977           0 :                       && CHECK_FLAG(hello->options, OSPF_OPTION_NP)
     978           0 :                       && !CHECK_FLAG(OPTIONS(oi), OSPF_OPTION_E)
     979             :                       && !CHECK_FLAG(hello->options, OSPF_OPTION_E))) {
     980           0 :                         flog_warn(
     981             :                                 EC_OSPF_PACKET,
     982             :                                 "NSSA-Packet-%pI4[Hello:RECV]: my options: %x, his options %x",
     983             :                                 &ospfh->router_id, OPTIONS(oi),
     984             :                                 hello->options);
     985           0 :                         return;
     986             :                 }
     987           0 :                 if (IS_DEBUG_OSPF_NSSA)
     988           0 :                         zlog_debug("NSSA-Hello:RECV:Packet from %pI4:",
     989             :                                    &ospfh->router_id);
     990             :         } else
     991             :                 /* The setting of the E-bit found in the Hello Packet's Options
     992             :                    field must match this area's ExternalRoutingCapability A
     993             :                    mismatch causes processing to stop and the packet to be
     994             :                    dropped. The setting of the rest of the bits in the Hello
     995             :                    Packet's Options field should be ignored. */
     996          94 :                 if (CHECK_FLAG(OPTIONS(oi), OSPF_OPTION_E)
     997          94 :                     != CHECK_FLAG(hello->options, OSPF_OPTION_E)) {
     998           0 :                 flog_warn(
     999             :                         EC_OSPF_PACKET,
    1000             :                         "Packet %pI4 [Hello:RECV]: my options: %x, his options %x",
    1001             :                         &ospfh->router_id, OPTIONS(oi),
    1002             :                         hello->options);
    1003           0 :                 return;
    1004             :         }
    1005             : 
    1006             :         /* get neighbour struct */
    1007          94 :         nbr = ospf_nbr_get(oi, ospfh, iph, &p);
    1008             : 
    1009             :         /* neighbour must be valid, ospf_nbr_get creates if none existed */
    1010          94 :         assert(nbr);
    1011             : 
    1012          94 :         old_state = nbr->state;
    1013             : 
    1014             :         /* Add event to thread. */
    1015          94 :         OSPF_NSM_EVENT_SCHEDULE(nbr, NSM_HelloReceived);
    1016             : 
    1017             :         /*  RFC2328  Section 9.5.1
    1018             :             If the router is not eligible to become Designated Router,
    1019             :             (snip)   It must also send an Hello Packet in reply to an
    1020             :             Hello Packet received from any eligible neighbor (other than
    1021             :             the current Designated Router and Backup Designated Router).  */
    1022          94 :         if (oi->type == OSPF_IFTYPE_NBMA)
    1023           0 :                 if (PRIORITY(oi) == 0 && hello->priority > 0
    1024           0 :                     && IPV4_ADDR_CMP(&DR(oi), &iph->ip_src)
    1025           0 :                     && IPV4_ADDR_CMP(&BDR(oi), &iph->ip_src))
    1026           0 :                         OSPF_NSM_TIMER_ON(nbr->t_hello_reply,
    1027             :                                           ospf_hello_reply_timer,
    1028             :                                           OSPF_HELLO_REPLY_DELAY);
    1029             : 
    1030             :         /* on NBMA network type, it happens to receive bidirectional Hello
    1031             :            packet
    1032             :            without advance 1-Way Received event.
    1033             :            To avoid incorrect DR-seletion, raise 1-Way Received event.*/
    1034          94 :         if (oi->type == OSPF_IFTYPE_NBMA
    1035           0 :             && (old_state == NSM_Down || old_state == NSM_Attempt)) {
    1036           0 :                 OSPF_NSM_EVENT_SCHEDULE(nbr, NSM_OneWayReceived);
    1037           0 :                 nbr->priority = hello->priority;
    1038           0 :                 nbr->d_router = hello->d_router;
    1039           0 :                 nbr->bd_router = hello->bd_router;
    1040           0 :                 return;
    1041             :         }
    1042             : 
    1043          94 :         if (ospf_nbr_bidirectional(&oi->ospf->router_id, hello->neighbors,
    1044          94 :                                    size - OSPF_HELLO_MIN_SIZE)) {
    1045          90 :                 OSPF_NSM_EVENT_SCHEDULE(nbr, NSM_TwoWayReceived);
    1046          90 :                 nbr->options |= hello->options;
    1047             :         } else {
    1048             :                 /* If the router is DR_OTHER, RESTARTER will not wait
    1049             :                  * until it receives the hello from it if it receives
    1050             :                  * from DR and BDR.
    1051             :                  * So, helper might receives ONW_WAY hello from
    1052             :                  * RESTARTER. So not allowing to change the state if it
    1053             :                  * receives one_way hellow when it acts as HELPER for
    1054             :                  * that specific neighbor.
    1055             :                  */
    1056           4 :                 if (!OSPF_GR_IS_ACTIVE_HELPER(nbr))
    1057           4 :                         OSPF_NSM_EVENT_SCHEDULE(nbr, NSM_OneWayReceived);
    1058             :                 /* Set neighbor information. */
    1059           4 :                 nbr->priority = hello->priority;
    1060           4 :                 nbr->d_router = hello->d_router;
    1061           4 :                 nbr->bd_router = hello->bd_router;
    1062           4 :                 return;
    1063             :         }
    1064             : 
    1065          90 :         if (OSPF_GR_IS_ACTIVE_HELPER(nbr)) {
    1066             :                 /* As per the GR Conformance Test Case 7.2. Section 3
    1067             :                  * "Also, if X was the Designated Router on network segment S
    1068             :                  * when the helping relationship began, Y maintains X as the
    1069             :                  * Designated Router until the helping relationship is
    1070             :                  * terminated."
    1071             :                  * When I am helper for this neighbor, I should not trigger the
    1072             :                  * ISM Events. Also Intentionally not setting the priority and
    1073             :                  * other fields so that when the neighbor exits the Grace
    1074             :                  * period, it can handle if there is any change before GR and
    1075             :                  * after GR. */
    1076           0 :                 if (IS_DEBUG_OSPF_GR)
    1077           0 :                         zlog_debug(
    1078             :                                 "%s, Neighbor is under GR Restart, hence ignoring the ISM Events",
    1079             :                                 __PRETTY_FUNCTION__);
    1080             :         } else {
    1081             :                 /* If neighbor itself declares DR and no BDR exists,
    1082             :                    cause event BackupSeen */
    1083          90 :                 if (IPV4_ADDR_SAME(&nbr->address.u.prefix4, &hello->d_router))
    1084          32 :                         if (hello->bd_router.s_addr == INADDR_ANY
    1085           4 :                             && oi->state == ISM_Waiting)
    1086           0 :                                 OSPF_ISM_EVENT_SCHEDULE(oi, ISM_BackupSeen);
    1087             : 
    1088             :                 /* neighbor itself declares BDR. */
    1089          90 :                 if (oi->state == ISM_Waiting
    1090          10 :                     && IPV4_ADDR_SAME(&nbr->address.u.prefix4,
    1091             :                                       &hello->bd_router))
    1092           0 :                         OSPF_ISM_EVENT_SCHEDULE(oi, ISM_BackupSeen);
    1093             : 
    1094             :                 /* had not previously. */
    1095          90 :                 if ((IPV4_ADDR_SAME(&nbr->address.u.prefix4, &hello->d_router)
    1096          32 :                      && IPV4_ADDR_CMP(&nbr->address.u.prefix4, &nbr->d_router))
    1097          86 :                     || (IPV4_ADDR_CMP(&nbr->address.u.prefix4, &hello->d_router)
    1098          58 :                         && IPV4_ADDR_SAME(&nbr->address.u.prefix4,
    1099             :                                           &nbr->d_router)))
    1100           4 :                         OSPF_ISM_EVENT_SCHEDULE(oi, ISM_NeighborChange);
    1101             : 
    1102             :                 /* had not previously. */
    1103          90 :                 if ((IPV4_ADDR_SAME(&nbr->address.u.prefix4, &hello->bd_router)
    1104          30 :                      && IPV4_ADDR_CMP(&nbr->address.u.prefix4, &nbr->bd_router))
    1105          86 :                     || (IPV4_ADDR_CMP(&nbr->address.u.prefix4,
    1106             :                                       &hello->bd_router)
    1107          60 :                         && IPV4_ADDR_SAME(&nbr->address.u.prefix4,
    1108             :                                           &nbr->bd_router)))
    1109           5 :                         OSPF_ISM_EVENT_SCHEDULE(oi, ISM_NeighborChange);
    1110             : 
    1111             :                 /* Neighbor priority check. */
    1112          90 :                 if (nbr->priority >= 0 && nbr->priority != hello->priority)
    1113           0 :                         OSPF_ISM_EVENT_SCHEDULE(oi, ISM_NeighborChange);
    1114             :         }
    1115             : 
    1116             :         /* Set neighbor information. */
    1117          90 :         nbr->priority = hello->priority;
    1118          90 :         nbr->d_router = hello->d_router;
    1119          90 :         nbr->bd_router = hello->bd_router;
    1120             : 
    1121             :         /*
    1122             :          * RFC 3623 - Section 2:
    1123             :          * "If the restarting router determines that it was the Designated
    1124             :          * Router on a given segment prior to the restart, it elects
    1125             :          * itself as the Designated Router again.  The restarting router
    1126             :          * knows that it was the Designated Router if, while the
    1127             :          * associated interface is in Waiting state, a Hello packet is
    1128             :          * received from a neighbor listing the router as the Designated
    1129             :          * Router".
    1130             :          */
    1131          90 :         if (oi->area->ospf->gr_info.restart_in_progress
    1132           0 :             && oi->state == ISM_Waiting
    1133           0 :             && IPV4_ADDR_SAME(&hello->d_router, &oi->address->u.prefix4))
    1134           0 :                 DR(oi) = hello->d_router;
    1135             : }
    1136             : 
    1137             : /* Save DD flags/options/Seqnum received. */
    1138          16 : static void ospf_db_desc_save_current(struct ospf_neighbor *nbr,
    1139             :                                       struct ospf_db_desc *dd)
    1140             : {
    1141          16 :         nbr->last_recv.flags = dd->flags;
    1142          16 :         nbr->last_recv.options = dd->options;
    1143          16 :         nbr->last_recv.dd_seqnum = ntohl(dd->dd_seqnum);
    1144             : }
    1145             : 
    1146             : /* Process rest of DD packet. */
    1147          16 : static void ospf_db_desc_proc(struct stream *s, struct ospf_interface *oi,
    1148             :                               struct ospf_neighbor *nbr,
    1149             :                               struct ospf_db_desc *dd, uint16_t size)
    1150             : {
    1151          16 :         struct ospf_lsa *new, *find;
    1152          16 :         struct lsa_header *lsah;
    1153             : 
    1154          16 :         stream_forward_getp(s, OSPF_DB_DESC_MIN_SIZE);
    1155          38 :         for (size -= OSPF_DB_DESC_MIN_SIZE; size >= OSPF_LSA_HEADER_SIZE;
    1156          22 :              size -= OSPF_LSA_HEADER_SIZE) {
    1157          22 :                 lsah = (struct lsa_header *)stream_pnt(s);
    1158          22 :                 stream_forward_getp(s, OSPF_LSA_HEADER_SIZE);
    1159             : 
    1160             :                 /* Unknown LS type. */
    1161          22 :                 if (lsah->type < OSPF_MIN_LSA || lsah->type >= OSPF_MAX_LSA) {
    1162           0 :                         flog_warn(EC_OSPF_PACKET,
    1163             :                                   "Packet [DD:RECV]: Unknown LS type %d.",
    1164             :                                   lsah->type);
    1165           0 :                         OSPF_NSM_EVENT_SCHEDULE(nbr, NSM_SeqNumberMismatch);
    1166           0 :                         return;
    1167             :                 }
    1168             : 
    1169          22 :                 if (IS_OPAQUE_LSA(lsah->type)
    1170           0 :                     && !CHECK_FLAG(nbr->options, OSPF_OPTION_O)) {
    1171           0 :                         flog_warn(EC_OSPF_PACKET,
    1172             :                                   "LSA[Type%d:%pI4] from %pI4: Opaque capability mismatch?",
    1173             :                                   lsah->type, &lsah->id, &lsah->adv_router);
    1174           0 :                         OSPF_NSM_EVENT_SCHEDULE(nbr, NSM_SeqNumberMismatch);
    1175           0 :                         return;
    1176             :                 }
    1177             : 
    1178          22 :                 switch (lsah->type) {
    1179          10 :                 case OSPF_AS_EXTERNAL_LSA:
    1180             :                 case OSPF_OPAQUE_AS_LSA:
    1181             :                         /* Check for stub area.  Reject if AS-External from stub
    1182             :                            but
    1183             :                            allow if from NSSA. */
    1184          10 :                         if (oi->area->external_routing == OSPF_AREA_STUB) {
    1185           0 :                                 flog_warn(
    1186             :                                         EC_OSPF_PACKET,
    1187             :                                         "Packet [DD:RECV]: LSA[Type%d:%pI4] from %s area.",
    1188             :                                         lsah->type, &lsah->id,
    1189             :                                         (oi->area->external_routing
    1190             :                                          == OSPF_AREA_STUB)
    1191             :                                                 ? "STUB"
    1192             :                                                 : "NSSA");
    1193           0 :                                 OSPF_NSM_EVENT_SCHEDULE(nbr,
    1194             :                                                         NSM_SeqNumberMismatch);
    1195           0 :                                 return;
    1196             :                         }
    1197             :                         break;
    1198             :                 default:
    1199             :                         break;
    1200             :                 }
    1201             : 
    1202             :                 /* Create LS-request object. */
    1203          22 :                 new = ospf_ls_request_new(lsah);
    1204             : 
    1205             :                 /* Lookup received LSA, then add LS request list. */
    1206          22 :                 find = ospf_lsa_lookup_by_header(oi->area, lsah);
    1207             : 
    1208             :                 /* ospf_lsa_more_recent is fine with NULL pointers */
    1209          22 :                 switch (ospf_lsa_more_recent(find, new)) {
    1210          21 :                 case -1:
    1211             :                         /* Neighbour has a more recent LSA, we must request it
    1212             :                          */
    1213          21 :                         ospf_ls_request_add(nbr, new);
    1214             :                 /* fallthru */
    1215          22 :                 case 0:
    1216             :                         /* If we have a copy of this LSA, it's either less
    1217             :                          * recent
    1218             :                          * and we're requesting it from neighbour (the case
    1219             :                          * above), or
    1220             :                          * it's as recent and we both have same copy (this
    1221             :                          * case).
    1222             :                          *
    1223             :                          * In neither of these two cases is there any point in
    1224             :                          * describing our copy of the LSA to the neighbour in a
    1225             :                          * DB-Summary packet, if we're still intending to do so.
    1226             :                          *
    1227             :                          * See: draft-ogier-ospf-dbex-opt-00.txt, describing the
    1228             :                          * backward compatible optimisation to OSPF DB Exchange
    1229             :                          * /
    1230             :                          * DB Description process implemented here.
    1231             :                          */
    1232          22 :                         if (find)
    1233           1 :                                 ospf_lsdb_delete(&nbr->db_sum, find);
    1234          22 :                         ospf_lsa_discard(new);
    1235          22 :                         break;
    1236           0 :                 default:
    1237             :                         /* We have the more recent copy, nothing specific to do:
    1238             :                          * - no need to request neighbours stale copy
    1239             :                          * - must leave DB summary list copy alone
    1240             :                          */
    1241           0 :                         if (IS_DEBUG_OSPF_EVENT)
    1242           0 :                                 zlog_debug(
    1243             :                                         "Packet [DD:RECV]: LSA received Type %d, ID %pI4 is not recent.",
    1244             :                                         lsah->type, &lsah->id);
    1245           0 :                         ospf_lsa_discard(new);
    1246             :                 }
    1247             :         }
    1248             : 
    1249             :         /* Master */
    1250          16 :         if (IS_SET_DD_MS(nbr->dd_flags)) {
    1251           8 :                 nbr->dd_seqnum++;
    1252             : 
    1253             :                 /* Both sides have no More, then we're done with Exchange */
    1254           8 :                 if (!IS_SET_DD_M(dd->flags) && !IS_SET_DD_M(nbr->dd_flags))
    1255           4 :                         OSPF_NSM_EVENT_SCHEDULE(nbr, NSM_ExchangeDone);
    1256             :                 else
    1257           4 :                         ospf_db_desc_send(nbr);
    1258             :         }
    1259             :         /* Slave */
    1260             :         else {
    1261           8 :                 nbr->dd_seqnum = ntohl(dd->dd_seqnum);
    1262             : 
    1263             :                 /* Send DD packet in reply.
    1264             :                  *
    1265             :                  * Must be done to acknowledge the Master's DD, regardless of
    1266             :                  * whether we have more LSAs ourselves to describe.
    1267             :                  *
    1268             :                  * This function will clear the 'More' bit, if after this DD
    1269             :                  * we have no more LSAs to describe to the master..
    1270             :                  */
    1271           8 :                 ospf_db_desc_send(nbr);
    1272             : 
    1273             :                 /* Slave can raise ExchangeDone now, if master is also done */
    1274           8 :                 if (!IS_SET_DD_M(dd->flags) && !IS_SET_DD_M(nbr->dd_flags))
    1275           4 :                         OSPF_NSM_EVENT_SCHEDULE(nbr, NSM_ExchangeDone);
    1276             :         }
    1277             : 
    1278             :         /* Save received neighbor values from DD. */
    1279          16 :         ospf_db_desc_save_current(nbr, dd);
    1280             : 
    1281          16 :         if (!nbr->t_ls_req)
    1282          12 :                 ospf_ls_req_send(nbr);
    1283             : }
    1284             : 
    1285           8 : static int ospf_db_desc_is_dup(struct ospf_db_desc *dd,
    1286             :                                struct ospf_neighbor *nbr)
    1287             : {
    1288             :         /* Is DD duplicated? */
    1289           8 :         if (dd->options == nbr->last_recv.options
    1290           8 :             && dd->flags == nbr->last_recv.flags
    1291           4 :             && dd->dd_seqnum == htonl(nbr->last_recv.dd_seqnum))
    1292           0 :                 return 1;
    1293             : 
    1294             :         return 0;
    1295             : }
    1296             : 
    1297             : /* OSPF Database Description message read -- RFC2328 Section 10.6. */
    1298          20 : static void ospf_db_desc(struct ip *iph, struct ospf_header *ospfh,
    1299             :                          struct stream *s, struct ospf_interface *oi,
    1300             :                          uint16_t size)
    1301             : {
    1302          20 :         struct ospf_db_desc *dd;
    1303          20 :         struct ospf_neighbor *nbr;
    1304             : 
    1305             :         /* Increment statistics. */
    1306          20 :         oi->db_desc_in++;
    1307             : 
    1308          20 :         dd = (struct ospf_db_desc *)stream_pnt(s);
    1309             : 
    1310          20 :         nbr = ospf_nbr_lookup(oi, iph, ospfh);
    1311          20 :         if (nbr == NULL) {
    1312           0 :                 flog_warn(EC_OSPF_PACKET, "Packet[DD]: Unknown Neighbor %pI4",
    1313             :                           &ospfh->router_id);
    1314           0 :                 return;
    1315             :         }
    1316             : 
    1317             :         /* Check MTU. */
    1318          20 :         if ((OSPF_IF_PARAM(oi, mtu_ignore) == 0)
    1319          20 :             && (ntohs(dd->mtu) > oi->ifp->mtu)) {
    1320           0 :                 flog_warn(
    1321             :                         EC_OSPF_PACKET,
    1322             :                         "Packet[DD]: Neighbor %pI4 MTU %u is larger than [%s]'s MTU %u",
    1323             :                         &nbr->router_id, ntohs(dd->mtu), IF_NAME(oi),
    1324             :                         oi->ifp->mtu);
    1325           0 :                 return;
    1326             :         }
    1327             : 
    1328             :         /*
    1329             :          * XXX HACK by Hasso Tepper. Setting N/P bit in NSSA area DD packets is
    1330             :          * not
    1331             :          * required. In fact at least JunOS sends DD packets with P bit clear.
    1332             :          * Until proper solution is developped, this hack should help.
    1333             :          *
    1334             :          * Update: According to the RFCs, N bit is specified /only/ for Hello
    1335             :          * options, unfortunately its use in DD options is not specified. Hence
    1336             :          * some
    1337             :          * implementations follow E-bit semantics and set it in DD options, and
    1338             :          * some
    1339             :          * treat it as unspecified and hence follow the directive "default for
    1340             :          * options is clear", ie unset.
    1341             :          *
    1342             :          * Reset the flag, as ospfd follows E-bit semantics.
    1343             :          */
    1344          20 :         if ((oi->area->external_routing == OSPF_AREA_NSSA)
    1345           0 :             && (CHECK_FLAG(nbr->options, OSPF_OPTION_NP))
    1346           0 :             && (!CHECK_FLAG(dd->options, OSPF_OPTION_NP))) {
    1347           0 :                 if (IS_DEBUG_OSPF_EVENT)
    1348           0 :                         zlog_debug(
    1349             :                                 "Packet[DD]: Neighbour %pI4: Has NSSA capability, sends with N bit clear in DD options",
    1350             :                                 &nbr->router_id);
    1351           0 :                 SET_FLAG(dd->options, OSPF_OPTION_NP);
    1352             :         }
    1353             : 
    1354             : #ifdef REJECT_IF_TBIT_ON
    1355          20 :         if (CHECK_FLAG(dd->options, OSPF_OPTION_MT)) {
    1356             :                 /*
    1357             :                  * In Hello protocol, optional capability must have checked
    1358             :                  * to prevent this T-bit enabled router be my neighbor.
    1359             :                  */
    1360           0 :                 flog_warn(EC_OSPF_PACKET, "Packet[DD]: Neighbor %pI4: T-bit on?",
    1361             :                           &nbr->router_id);
    1362           0 :                 return;
    1363             :         }
    1364             : #endif /* REJECT_IF_TBIT_ON */
    1365             : 
    1366          20 :         if (CHECK_FLAG(dd->options, OSPF_OPTION_O)
    1367           0 :             && !CHECK_FLAG(oi->ospf->config, OSPF_OPAQUE_CAPABLE)) {
    1368             :                 /*
    1369             :                  * This node is not configured to handle O-bit, for now.
    1370             :                  * Clear it to ignore unsupported capability proposed by
    1371             :                  * neighbor.
    1372             :                  */
    1373           0 :                 UNSET_FLAG(dd->options, OSPF_OPTION_O);
    1374             :         }
    1375             : 
    1376          20 :         if (CHECK_FLAG(oi->ospf->config, OSPF_LOG_ADJACENCY_DETAIL))
    1377           0 :                 zlog_info(
    1378             :                         "%s:Packet[DD]: Neighbor %pI4 state is %s, seq_num:0x%x, local:0x%x",
    1379             :                         ospf_get_name(oi->ospf), &nbr->router_id,
    1380             :                         lookup_msg(ospf_nsm_state_msg, nbr->state, NULL),
    1381             :                         ntohl(dd->dd_seqnum), nbr->dd_seqnum);
    1382             : 
    1383             :         /* Process DD packet by neighbor status. */
    1384          20 :         switch (nbr->state) {
    1385           2 :         case NSM_Down:
    1386             :         case NSM_Attempt:
    1387             :         case NSM_TwoWay:
    1388           2 :                 if (CHECK_FLAG(oi->ospf->config, OSPF_LOG_ADJACENCY_DETAIL))
    1389           0 :                         zlog_info(
    1390             :                                 "Packet[DD]: Neighbor %pI4 state is %s, packet discarded.",
    1391             :                                 &nbr->router_id,
    1392             :                                 lookup_msg(ospf_nsm_state_msg, nbr->state,
    1393             :                                            NULL));
    1394             :                 break;
    1395           1 :         case NSM_Init:
    1396           1 :                 OSPF_NSM_EVENT_EXECUTE(nbr, NSM_TwoWayReceived);
    1397             :                 /* If the new state is ExStart, the processing of the current
    1398             :                    packet should then continue in this new state by falling
    1399             :                    through to case ExStart below.  */
    1400           1 :                 if (nbr->state != NSM_ExStart)
    1401             :                         break;
    1402             :         /* fallthru */
    1403             :         case NSM_ExStart:
    1404             :                 /* Initial DBD */
    1405           9 :                 if ((IS_SET_DD_ALL(dd->flags) == OSPF_DD_FLAG_ALL)
    1406           5 :                     && (size == OSPF_DB_DESC_MIN_SIZE)) {
    1407           5 :                         if (IPV4_ADDR_CMP(&nbr->router_id, &oi->ospf->router_id)
    1408             :                             > 0) {
    1409             :                                 /* We're Slave---obey */
    1410           4 :                                 if (CHECK_FLAG(oi->ospf->config,
    1411             :                                                OSPF_LOG_ADJACENCY_DETAIL))
    1412           0 :                                         zlog_info(
    1413             :                                                 "Packet[DD]: Neighbor %pI4 Negotiation done (Slave).",
    1414             :                                                 &nbr->router_id);
    1415             : 
    1416           4 :                                 nbr->dd_seqnum = ntohl(dd->dd_seqnum);
    1417             : 
    1418             :                                 /* Reset I/MS */
    1419           4 :                                 UNSET_FLAG(nbr->dd_flags,
    1420             :                                            (OSPF_DD_FLAG_MS | OSPF_DD_FLAG_I));
    1421             :                         } else {
    1422             :                                 /* We're Master, ignore the initial DBD from
    1423             :                                  * Slave */
    1424           1 :                                 if (CHECK_FLAG(oi->ospf->config,
    1425             :                                                OSPF_LOG_ADJACENCY_DETAIL))
    1426           0 :                                         zlog_info(
    1427             :                                                 "Packet[DD]: Neighbor %pI4: Initial DBD from Slave, ignoring.",
    1428             :                                                 &nbr->router_id);
    1429             :                                 break;
    1430             :                         }
    1431             :                 }
    1432             :                 /* Ack from the Slave */
    1433           4 :                 else if (!IS_SET_DD_MS(dd->flags) && !IS_SET_DD_I(dd->flags)
    1434           4 :                          && ntohl(dd->dd_seqnum) == nbr->dd_seqnum
    1435           4 :                          && IPV4_ADDR_CMP(&nbr->router_id, &oi->ospf->router_id)
    1436             :                                     < 0) {
    1437           4 :                         zlog_info(
    1438             :                                 "Packet[DD]: Neighbor %pI4 Negotiation done (Master).",
    1439             :                                 &nbr->router_id);
    1440             :                         /* Reset I, leaving MS */
    1441           4 :                         UNSET_FLAG(nbr->dd_flags, OSPF_DD_FLAG_I);
    1442             :                 } else {
    1443           0 :                         flog_warn(EC_OSPF_PACKET,
    1444             :                                   "Packet[DD]: Neighbor %pI4 Negotiation fails.",
    1445             :                                   &nbr->router_id);
    1446           0 :                         break;
    1447             :                 }
    1448             : 
    1449             :                 /* This is where the real Options are saved */
    1450           8 :                 nbr->options = dd->options;
    1451             : 
    1452           8 :                 if (CHECK_FLAG(oi->ospf->config, OSPF_OPAQUE_CAPABLE)) {
    1453           0 :                         if (IS_DEBUG_OSPF_EVENT)
    1454           0 :                                 zlog_debug(
    1455             :                                         "Neighbor[%pI4] is %sOpaque-capable.",
    1456             :                                         &nbr->router_id,
    1457             :                                         CHECK_FLAG(nbr->options, OSPF_OPTION_O)
    1458             :                                                 ? ""
    1459             :                                                 : "NOT ");
    1460             : 
    1461           0 :                         if (!CHECK_FLAG(nbr->options, OSPF_OPTION_O)
    1462           0 :                             && IPV4_ADDR_SAME(&DR(oi),
    1463             :                                               &nbr->address.u.prefix4)) {
    1464           0 :                                 flog_warn(
    1465             :                                         EC_OSPF_PACKET,
    1466             :                                         "DR-neighbor[%pI4] is NOT opaque-capable; Opaque-LSAs cannot be reliably advertised in this network.",
    1467             :                                         &nbr->router_id);
    1468             :                                 /* This situation is undesirable, but not a real
    1469             :                                  * error. */
    1470             :                         }
    1471             :                 }
    1472             : 
    1473           8 :                 OSPF_NSM_EVENT_EXECUTE(nbr, NSM_NegotiationDone);
    1474             : 
    1475             :                 /* continue processing rest of packet. */
    1476           8 :                 ospf_db_desc_proc(s, oi, nbr, dd, size);
    1477           8 :                 break;
    1478             :         case NSM_Exchange:
    1479           8 :                 if (ospf_db_desc_is_dup(dd, nbr)) {
    1480           0 :                         if (IS_SET_DD_MS(nbr->dd_flags))
    1481             :                                 /* Master: discard duplicated DD packet. */
    1482           0 :                                 zlog_info(
    1483             :                                         "Packet[DD] (Master): Neighbor %pI4 packet duplicated.",
    1484             :                                         &nbr->router_id);
    1485             :                         else
    1486             :                         /* Slave: cause to retransmit the last Database
    1487             :                            Description. */
    1488             :                         {
    1489           0 :                                 zlog_info(
    1490             :                                         "Packet[DD] [Slave]: Neighbor %pI4 packet duplicated.",
    1491             :                                         &nbr->router_id);
    1492           0 :                                 ospf_db_desc_resend(nbr);
    1493             :                         }
    1494             :                         break;
    1495             :                 }
    1496             : 
    1497             :                 /* Otherwise DD packet should be checked. */
    1498             :                 /* Check Master/Slave bit mismatch */
    1499           8 :                 if (IS_SET_DD_MS(dd->flags)
    1500           8 :                     != IS_SET_DD_MS(nbr->last_recv.flags)) {
    1501           0 :                         flog_warn(EC_OSPF_PACKET,
    1502             :                                   "Packet[DD]: Neighbor %pI4 MS-bit mismatch.",
    1503             :                                   &nbr->router_id);
    1504           0 :                         OSPF_NSM_EVENT_SCHEDULE(nbr, NSM_SeqNumberMismatch);
    1505           0 :                         if (IS_DEBUG_OSPF_EVENT)
    1506           0 :                                 zlog_debug(
    1507             :                                         "Packet[DD]: dd->flags=%d, nbr->dd_flags=%d",
    1508             :                                         dd->flags, nbr->dd_flags);
    1509             :                         break;
    1510             :                 }
    1511             : 
    1512             :                 /* Check initialize bit is set. */
    1513           8 :                 if (IS_SET_DD_I(dd->flags)) {
    1514           0 :                         zlog_info("Packet[DD]: Neighbor %pI4 I-bit set.",
    1515             :                                   &nbr->router_id);
    1516           0 :                         OSPF_NSM_EVENT_SCHEDULE(nbr, NSM_SeqNumberMismatch);
    1517           0 :                         break;
    1518             :                 }
    1519             : 
    1520             :                 /* Check DD Options. */
    1521           8 :                 if (dd->options != nbr->options) {
    1522           0 :                         flog_warn(EC_OSPF_PACKET,
    1523             :                                   "Packet[DD]: Neighbor %pI4 options mismatch.",
    1524             :                                   &nbr->router_id);
    1525           0 :                         OSPF_NSM_EVENT_SCHEDULE(nbr, NSM_SeqNumberMismatch);
    1526           0 :                         break;
    1527             :                 }
    1528             : 
    1529             :                 /* Check DD sequence number. */
    1530           8 :                 if ((IS_SET_DD_MS(nbr->dd_flags)
    1531           4 :                      && ntohl(dd->dd_seqnum) != nbr->dd_seqnum)
    1532           8 :                     || (!IS_SET_DD_MS(nbr->dd_flags)
    1533           4 :                         && ntohl(dd->dd_seqnum) != nbr->dd_seqnum + 1)) {
    1534           0 :                         flog_warn(
    1535             :                                 EC_OSPF_PACKET,
    1536             :                                 "Packet[DD]: Neighbor %pI4 sequence number mismatch.",
    1537             :                                 &nbr->router_id);
    1538           0 :                         OSPF_NSM_EVENT_SCHEDULE(nbr, NSM_SeqNumberMismatch);
    1539           0 :                         break;
    1540             :                 }
    1541             : 
    1542             :                 /* Continue processing rest of packet. */
    1543           8 :                 ospf_db_desc_proc(s, oi, nbr, dd, size);
    1544           8 :                 break;
    1545             :         case NSM_Loading:
    1546             :         case NSM_Full:
    1547           0 :                 if (ospf_db_desc_is_dup(dd, nbr)) {
    1548           0 :                         if (IS_SET_DD_MS(nbr->dd_flags)) {
    1549             :                                 /* Master should discard duplicate DD packet. */
    1550           0 :                                 zlog_info(
    1551             :                                         "Packet[DD]: Neighbor %pI4 duplicated, packet discarded.",
    1552             :                                         &nbr->router_id);
    1553           0 :                                 break;
    1554             :                         } else {
    1555           0 :                                 if (monotime_since(&nbr->last_send_ts, NULL)
    1556           0 :                                     < nbr->v_inactivity * 1000000LL) {
    1557             :                                         /* In states Loading and Full the slave
    1558             :                                            must resend
    1559             :                                            its last Database Description packet
    1560             :                                            in response to
    1561             :                                            duplicate Database Description
    1562             :                                            packets received
    1563             :                                            from the master.  For this reason the
    1564             :                                            slave must
    1565             :                                            wait RouterDeadInterval seconds
    1566             :                                            before freeing the
    1567             :                                            last Database Description packet.
    1568             :                                            Reception of a
    1569             :                                            Database Description packet from the
    1570             :                                            master after
    1571             :                                            this interval will generate a
    1572             :                                            SeqNumberMismatch
    1573             :                                            neighbor event. RFC2328 Section 10.8
    1574             :                                            */
    1575           0 :                                         ospf_db_desc_resend(nbr);
    1576           0 :                                         break;
    1577             :                                 }
    1578             :                         }
    1579             :                 }
    1580             : 
    1581           0 :                 OSPF_NSM_EVENT_SCHEDULE(nbr, NSM_SeqNumberMismatch);
    1582           0 :                 break;
    1583           0 :         default:
    1584           0 :                 flog_warn(EC_OSPF_PACKET,
    1585             :                           "Packet[DD]: Neighbor %pI4 NSM illegal status %u.",
    1586             :                           &nbr->router_id, nbr->state);
    1587           0 :                 break;
    1588             :         }
    1589             : }
    1590             : 
    1591             : #define OSPF_LSA_KEY_SIZE       12 /* type(4) + id(4) + ar(4) */
    1592             : 
    1593             : /* OSPF Link State Request Read -- RFC2328 Section 10.7. */
    1594           8 : static void ospf_ls_req(struct ip *iph, struct ospf_header *ospfh,
    1595             :                         struct stream *s, struct ospf_interface *oi,
    1596             :                         uint16_t size)
    1597             : {
    1598           8 :         struct ospf_neighbor *nbr;
    1599           8 :         uint32_t ls_type;
    1600           8 :         struct in_addr ls_id;
    1601           8 :         struct in_addr adv_router;
    1602           8 :         struct ospf_lsa *find;
    1603           8 :         struct list *ls_upd;
    1604           8 :         unsigned int length;
    1605             : 
    1606             :         /* Increment statistics. */
    1607           8 :         oi->ls_req_in++;
    1608             : 
    1609           8 :         nbr = ospf_nbr_lookup(oi, iph, ospfh);
    1610           8 :         if (nbr == NULL) {
    1611           0 :                 flog_warn(EC_OSPF_PACKET,
    1612             :                           "Link State Request: Unknown Neighbor %pI4",
    1613             :                           &ospfh->router_id);
    1614           0 :                 return;
    1615             :         }
    1616             : 
    1617             :         /* Neighbor State should be Exchange or later. */
    1618           8 :         if (nbr->state != NSM_Exchange && nbr->state != NSM_Loading
    1619           8 :             && nbr->state != NSM_Full) {
    1620           0 :                 flog_warn(
    1621             :                         EC_OSPF_PACKET,
    1622             :                         "Link State Request received from %pI4: Neighbor state is %s, packet discarded.",
    1623             :                         &ospfh->router_id,
    1624             :                         lookup_msg(ospf_nsm_state_msg, nbr->state, NULL));
    1625           0 :                 return;
    1626             :         }
    1627             : 
    1628             :         /* Send Link State Update for ALL requested LSAs. */
    1629           8 :         ls_upd = list_new();
    1630           8 :         length = OSPF_HEADER_SIZE + OSPF_LS_UPD_MIN_SIZE;
    1631             : 
    1632          29 :         while (size >= OSPF_LSA_KEY_SIZE) {
    1633             :                 /* Get one slice of Link State Request. */
    1634          21 :                 ls_type = stream_getl(s);
    1635          21 :                 ls_id.s_addr = stream_get_ipv4(s);
    1636          21 :                 adv_router.s_addr = stream_get_ipv4(s);
    1637             : 
    1638             :                 /* Verify LSA type. */
    1639          21 :                 if (ls_type < OSPF_MIN_LSA || ls_type >= OSPF_MAX_LSA) {
    1640           0 :                         OSPF_NSM_EVENT_SCHEDULE(nbr, NSM_BadLSReq);
    1641           0 :                         list_delete(&ls_upd);
    1642           0 :                         return;
    1643             :                 }
    1644             : 
    1645             :                 /* Search proper LSA in LSDB. */
    1646          21 :                 find = ospf_lsa_lookup(oi->ospf, oi->area, ls_type, ls_id,
    1647             :                                        adv_router);
    1648          21 :                 if (find == NULL) {
    1649           0 :                         OSPF_NSM_EVENT_SCHEDULE(nbr, NSM_BadLSReq);
    1650           0 :                         list_delete(&ls_upd);
    1651           0 :                         return;
    1652             :                 }
    1653             : 
    1654             :                 /* Packet overflows MTU size, send immediately. */
    1655          21 :                 if (length + ntohs(find->data->length) > ospf_packet_max(oi)) {
    1656           0 :                         if (oi->type == OSPF_IFTYPE_NBMA)
    1657           0 :                                 ospf_ls_upd_send(nbr, ls_upd,
    1658             :                                                  OSPF_SEND_PACKET_DIRECT, 0);
    1659             :                         else
    1660           0 :                                 ospf_ls_upd_send(nbr, ls_upd,
    1661             :                                                  OSPF_SEND_PACKET_INDIRECT, 0);
    1662             : 
    1663             :                         /* Only remove list contents.  Keep ls_upd. */
    1664           0 :                         list_delete_all_node(ls_upd);
    1665             : 
    1666           0 :                         length = OSPF_HEADER_SIZE + OSPF_LS_UPD_MIN_SIZE;
    1667             :                 }
    1668             : 
    1669             :                 /* Append LSA to update list. */
    1670          21 :                 listnode_add(ls_upd, find);
    1671          21 :                 length += ntohs(find->data->length);
    1672             : 
    1673          21 :                 size -= OSPF_LSA_KEY_SIZE;
    1674             :         }
    1675             : 
    1676             :         /* Send rest of Link State Update. */
    1677           8 :         if (listcount(ls_upd) > 0) {
    1678           8 :                 if (oi->type == OSPF_IFTYPE_NBMA)
    1679           0 :                         ospf_ls_upd_send(nbr, ls_upd, OSPF_SEND_PACKET_DIRECT,
    1680             :                                          0);
    1681             :                 else
    1682           8 :                         ospf_ls_upd_send(nbr, ls_upd, OSPF_SEND_PACKET_INDIRECT,
    1683             :                                          0);
    1684             : 
    1685           8 :                 list_delete(&ls_upd);
    1686             :         } else
    1687           0 :                 list_delete(&ls_upd);
    1688             : }
    1689             : 
    1690             : /* Get the list of LSAs from Link State Update packet.
    1691             :    And process some validation -- RFC2328 Section 13. (1)-(2). */
    1692          53 : static struct list *ospf_ls_upd_list_lsa(struct ospf_neighbor *nbr,
    1693             :                                          struct stream *s,
    1694             :                                          struct ospf_interface *oi, size_t size)
    1695             : {
    1696          53 :         uint16_t count, sum;
    1697          53 :         uint32_t length;
    1698          53 :         struct lsa_header *lsah;
    1699          53 :         struct ospf_lsa *lsa;
    1700          53 :         struct list *lsas;
    1701             : 
    1702          53 :         lsas = list_new();
    1703             : 
    1704          53 :         count = stream_getl(s);
    1705          53 :         size -= OSPF_LS_UPD_MIN_SIZE; /* # LSAs */
    1706             : 
    1707         165 :         for (; size >= OSPF_LSA_HEADER_SIZE && count > 0;
    1708         112 :              size -= length, stream_forward_getp(s, length), count--) {
    1709         112 :                 lsah = (struct lsa_header *)stream_pnt(s);
    1710         112 :                 length = ntohs(lsah->length);
    1711             : 
    1712         112 :                 if (length > size) {
    1713           0 :                         flog_warn(
    1714             :                                 EC_OSPF_PACKET,
    1715             :                                 "Link State Update: LSA length exceeds packet size.");
    1716           0 :                         break;
    1717             :                 }
    1718             : 
    1719         112 :                 if (length < OSPF_LSA_HEADER_SIZE) {
    1720           0 :                         flog_warn(EC_OSPF_PACKET,
    1721             :                                   "Link State Update: LSA length too small.");
    1722           0 :                         break;
    1723             :                 }
    1724             : 
    1725             :                 /* Validate the LSA's LS checksum. */
    1726         112 :                 sum = lsah->checksum;
    1727         112 :                 if (!ospf_lsa_checksum_valid(lsah)) {
    1728             :                         /* (bug #685) more details in a one-line message make it
    1729             :                          * possible
    1730             :                          * to identify problem source on the one hand and to
    1731             :                          * have a better
    1732             :                          * chance to compress repeated messages in syslog on the
    1733             :                          * other */
    1734           0 :                         flog_warn(
    1735             :                                 EC_OSPF_PACKET,
    1736             :                                 "Link State Update: LSA checksum error %x/%x, ID=%pI4 from: nbr %pI4, router ID %pI4, adv router %pI4",
    1737             :                                 sum, lsah->checksum, &lsah->id,
    1738             :                                 &nbr->src, &nbr->router_id,
    1739             :                                 &lsah->adv_router);
    1740           0 :                         continue;
    1741             :                 }
    1742             : 
    1743             :                 /* Examine the LSA's LS type. */
    1744         112 :                 if (lsah->type < OSPF_MIN_LSA || lsah->type >= OSPF_MAX_LSA) {
    1745           0 :                         flog_warn(EC_OSPF_PACKET,
    1746             :                                   "Link State Update: Unknown LS type %d",
    1747             :                                   lsah->type);
    1748           0 :                         continue;
    1749             :                 }
    1750             : 
    1751             :                 /*
    1752             :                  * What if the received LSA's age is greater than MaxAge?
    1753             :                  * Treat it as a MaxAge case -- endo.
    1754             :                  */
    1755         112 :                 if (ntohs(lsah->ls_age) > OSPF_LSA_MAXAGE)
    1756           0 :                         lsah->ls_age = htons(OSPF_LSA_MAXAGE);
    1757             : 
    1758         112 :                 if (CHECK_FLAG(nbr->options, OSPF_OPTION_O)) {
    1759             : #ifdef STRICT_OBIT_USAGE_CHECK
    1760             :                         if ((IS_OPAQUE_LSA(lsah->type)
    1761             :                              && !CHECK_FLAG(lsah->options, OSPF_OPTION_O))
    1762             :                             || (!IS_OPAQUE_LSA(lsah->type)
    1763             :                                 && CHECK_FLAG(lsah->options, OSPF_OPTION_O))) {
    1764             :                                 /*
    1765             :                                  * This neighbor must know the exact usage of
    1766             :                                  * O-bit;
    1767             :                                  * the bit will be set in Type-9,10,11 LSAs
    1768             :                                  * only.
    1769             :                                  */
    1770             :                                 flog_warn(EC_OSPF_PACKET,
    1771             :                                           "LSA[Type%d:%pI4]: O-bit abuse?",
    1772             :                                           lsah->type, &lsah->id);
    1773             :                                 continue;
    1774             :                         }
    1775             : #endif /* STRICT_OBIT_USAGE_CHECK */
    1776             : 
    1777             :                         /* Do not take in AS External Opaque-LSAs if we are a
    1778             :                          * stub. */
    1779           0 :                         if (lsah->type == OSPF_OPAQUE_AS_LSA
    1780           0 :                             && nbr->oi->area->external_routing
    1781             :                                        != OSPF_AREA_DEFAULT) {
    1782           0 :                                 if (IS_DEBUG_OSPF_EVENT)
    1783           0 :                                         zlog_debug(
    1784             :                                                 "LSA[Type%d:%pI4]: We are a stub, don't take this LSA.",
    1785             :                                                 lsah->type,
    1786             :                                                 &lsah->id);
    1787           0 :                                 continue;
    1788             :                         }
    1789         112 :                 } else if (IS_OPAQUE_LSA(lsah->type)) {
    1790           0 :                         flog_warn(
    1791             :                                 EC_OSPF_PACKET,
    1792             :                                 "LSA[Type%d:%pI4] from %pI4: Opaque capability mismatch?",
    1793             :                                 lsah->type, &lsah->id, &lsah->adv_router);
    1794           0 :                         continue;
    1795             :                 }
    1796             : 
    1797             :                 /* Create OSPF LSA instance. */
    1798         112 :                 lsa = ospf_lsa_new_and_data(length);
    1799             : 
    1800         112 :                 lsa->vrf_id = oi->ospf->vrf_id;
    1801             :                 /* We may wish to put some error checking if type NSSA comes in
    1802             :                    and area not in NSSA mode */
    1803         112 :                 switch (lsah->type) {
    1804          24 :                 case OSPF_AS_EXTERNAL_LSA:
    1805             :                 case OSPF_OPAQUE_AS_LSA:
    1806          24 :                         lsa->area = NULL;
    1807          24 :                         break;
    1808           0 :                 case OSPF_OPAQUE_LINK_LSA:
    1809           0 :                         lsa->oi = oi; /* Remember incoming interface for
    1810             :                                          flooding control. */
    1811             :                 /* Fallthrough */
    1812          88 :                 default:
    1813          88 :                         lsa->area = oi->area;
    1814          88 :                         break;
    1815             :                 }
    1816             : 
    1817         112 :                 memcpy(lsa->data, lsah, length);
    1818             : 
    1819         112 :                 if (IS_DEBUG_OSPF_EVENT)
    1820         112 :                         zlog_debug(
    1821             :                                 "LSA[Type%d:%pI4]: %p new LSA created with Link State Update",
    1822             :                                 lsa->data->type, &lsa->data->id,
    1823             :                                 (void *)lsa);
    1824         112 :                 listnode_add(lsas, lsa);
    1825             :         }
    1826             : 
    1827          53 :         return lsas;
    1828             : }
    1829             : 
    1830             : /* Cleanup Update list. */
    1831           0 : static void ospf_upd_list_clean(struct list *lsas)
    1832             : {
    1833           0 :         struct listnode *node, *nnode;
    1834           0 :         struct ospf_lsa *lsa;
    1835             : 
    1836           0 :         for (ALL_LIST_ELEMENTS(lsas, node, nnode, lsa))
    1837           0 :                 ospf_lsa_discard(lsa);
    1838             : 
    1839           0 :         list_delete(&lsas);
    1840           0 : }
    1841             : 
    1842             : /* OSPF Link State Update message read -- RFC2328 Section 13. */
    1843          53 : static void ospf_ls_upd(struct ospf *ospf, struct ip *iph,
    1844             :                         struct ospf_header *ospfh, struct stream *s,
    1845             :                         struct ospf_interface *oi, uint16_t size)
    1846             : {
    1847          53 :         struct ospf_neighbor *nbr;
    1848          53 :         struct list *lsas;
    1849          53 :         struct listnode *node, *nnode;
    1850          53 :         struct ospf_lsa *lsa = NULL;
    1851             :         /* unsigned long ls_req_found = 0; */
    1852             : 
    1853             :         /* Dis-assemble the stream, update each entry, re-encapsulate for
    1854             :          * flooding */
    1855             : 
    1856             :         /* Increment statistics. */
    1857          53 :         oi->ls_upd_in++;
    1858             : 
    1859             :         /* Check neighbor. */
    1860          53 :         nbr = ospf_nbr_lookup(oi, iph, ospfh);
    1861          53 :         if (nbr == NULL) {
    1862           0 :                 flog_warn(EC_OSPF_PACKET,
    1863             :                           "Link State Update: Unknown Neighbor %pI4 on int: %s",
    1864             :                           &ospfh->router_id, IF_NAME(oi));
    1865           0 :                 return;
    1866             :         }
    1867             : 
    1868             :         /* Check neighbor state. */
    1869          53 :         if (nbr->state < NSM_Exchange) {
    1870           0 :                 if (IS_DEBUG_OSPF(nsm, NSM_EVENTS))
    1871           0 :                         zlog_debug(
    1872             :                                 "Link State Update: Neighbor[%pI4] state %s is less than Exchange",
    1873             :                                 &ospfh->router_id,
    1874             :                                 lookup_msg(ospf_nsm_state_msg, nbr->state,
    1875             :                                            NULL));
    1876           0 :                 return;
    1877             :         }
    1878             : 
    1879             :         /* Get list of LSAs from Link State Update packet. - Also performs
    1880             :          * Stages 1 (validate LSA checksum) and 2 (check for LSA consistent
    1881             :          * type) of section 13.
    1882             :          */
    1883          53 :         lsas = ospf_ls_upd_list_lsa(nbr, s, oi, size);
    1884             : 
    1885          53 :         if (lsas == NULL)
    1886             :                 return;
    1887             : #define DISCARD_LSA(L, N)                                                              \
    1888             :         {                                                                              \
    1889             :                 if (IS_DEBUG_OSPF_EVENT)                                               \
    1890             :                         zlog_debug(                                                    \
    1891             :                                 "ospf_lsa_discard() in ospf_ls_upd() point %d: lsa %p" \
    1892             :                                 " Type-%d",                                            \
    1893             :                                 N, (void *)lsa, (int)lsa->data->type);                 \
    1894             :                 ospf_lsa_discard(L);                                                   \
    1895             :                 continue;                                                              \
    1896             :         }
    1897             : 
    1898             :         /* Process each LSA received in the one packet.
    1899             :          *
    1900             :          * Numbers in parentheses, e.g. (1), (2), etc., and the corresponding
    1901             :          * text below are from the steps in RFC 2328, Section 13.
    1902             :          */
    1903         165 :         for (ALL_LIST_ELEMENTS(lsas, node, nnode, lsa)) {
    1904         112 :                 struct ospf_lsa *ls_ret, *current;
    1905         112 :                 int ret = 1;
    1906             : 
    1907         112 :                 if (IS_DEBUG_OSPF(lsa, LSA))
    1908           0 :                         zlog_debug("LSA Type-%d from %pI4, ID: %pI4, ADV: %pI4",
    1909             :                                    lsa->data->type, &ospfh->router_id,
    1910             :                                    &lsa->data->id, &lsa->data->adv_router);
    1911             : 
    1912         112 :                 listnode_delete(lsas,
    1913             :                                 lsa); /* We don't need it in list anymore */
    1914             : 
    1915             :                 /* (1) Validate Checksum - Done above by ospf_ls_upd_list_lsa()
    1916             :                  */
    1917             : 
    1918             :                 /* (2) LSA Type  - Done above by ospf_ls_upd_list_lsa() */
    1919             : 
    1920             :                 /* (3) Do not take in AS External LSAs if we are a stub or NSSA.
    1921             :                  */
    1922             : 
    1923             :                 /* Do not take in AS NSSA if this neighbor and we are not NSSA
    1924             :                  */
    1925             : 
    1926             :                 /* Do take in Type-7's if we are an NSSA  */
    1927             : 
    1928             :                 /* If we are also an ABR, later translate them to a Type-5
    1929             :                  * packet */
    1930             : 
    1931             :                 /* Later, an NSSA Re-fresh can Re-fresh Type-7's and an ABR will
    1932             :                    translate them to a separate Type-5 packet.  */
    1933             : 
    1934         112 :                 if (lsa->data->type == OSPF_AS_EXTERNAL_LSA)
    1935             :                         /* Reject from STUB or NSSA */
    1936          24 :                         if (nbr->oi->area->external_routing
    1937             :                             != OSPF_AREA_DEFAULT) {
    1938           0 :                                 if (IS_DEBUG_OSPF_NSSA)
    1939           0 :                                         zlog_debug(
    1940             :                                                 "Incoming External LSA Discarded: We are NSSA/STUB Area");
    1941           0 :                                 DISCARD_LSA(lsa, 1);
    1942             :                         }
    1943             : 
    1944         112 :                 if (lsa->data->type == OSPF_AS_NSSA_LSA)
    1945           0 :                         if (nbr->oi->area->external_routing != OSPF_AREA_NSSA) {
    1946           0 :                                 if (IS_DEBUG_OSPF_NSSA)
    1947           0 :                                         zlog_debug(
    1948             :                                                 "Incoming NSSA LSA Discarded:  Not NSSA Area");
    1949           0 :                                 DISCARD_LSA(lsa, 2);
    1950             :                         }
    1951             : 
    1952             :                 /* VU229804: Router-LSA Adv-ID must be equal to LS-ID */
    1953         112 :                 if (lsa->data->type == OSPF_ROUTER_LSA)
    1954          54 :                         if (!IPV4_ADDR_SAME(&lsa->data->id,
    1955             :                                             &lsa->data->adv_router)) {
    1956           0 :                                 flog_err(
    1957             :                                         EC_OSPF_ROUTER_LSA_MISMATCH,
    1958             :                                         "Incoming Router-LSA from %pI4 with Adv-ID[%pI4] != LS-ID[%pI4]",
    1959             :                                         &ospfh->router_id, &lsa->data->id,
    1960             :                                         &lsa->data->adv_router);
    1961           0 :                                 flog_err(
    1962             :                                         EC_OSPF_DOMAIN_CORRUPT,
    1963             :                                         "OSPF domain compromised by attack or corruption. Verify correct operation of -ALL- OSPF routers.");
    1964           0 :                                 DISCARD_LSA(lsa, 0);
    1965             :                         }
    1966             : 
    1967             :                 /* Find the LSA in the current database. */
    1968             : 
    1969         112 :                 current = ospf_lsa_lookup_by_header(oi->area, lsa->data);
    1970             : 
    1971             :                 /* (4) If the LSA's LS age is equal to MaxAge, and there is
    1972             :                    currently
    1973             :                    no instance of the LSA in the router's link state database,
    1974             :                    and none of router's neighbors are in states Exchange or
    1975             :                    Loading,
    1976             :                    then take the following actions: */
    1977             : 
    1978         112 :                 if (IS_LSA_MAXAGE(lsa) && !current
    1979           0 :                     && ospf_check_nbr_status(oi->ospf)) {
    1980             :                         /* (4a) Response Link State Acknowledgment. */
    1981           0 :                         ospf_ls_ack_send(nbr, lsa);
    1982             : 
    1983             :                         /* (4b) Discard LSA. */
    1984           0 :                         if (IS_DEBUG_OSPF(lsa, LSA)) {
    1985           0 :                                 zlog_debug(
    1986             :                                         "Link State Update[%s]: LS age is equal to MaxAge.",
    1987             :                                         dump_lsa_key(lsa));
    1988             :                         }
    1989           0 :                         DISCARD_LSA(lsa, 3);
    1990             :                 }
    1991             : 
    1992         112 :                 if (IS_OPAQUE_LSA(lsa->data->type)
    1993           0 :                     && IPV4_ADDR_SAME(&lsa->data->adv_router,
    1994             :                                       &oi->ospf->router_id)) {
    1995             :                         /*
    1996             :                          * Even if initial flushing seems to be completed, there
    1997             :                          * might
    1998             :                          * be a case that self-originated LSA with MaxAge still
    1999             :                          * remain
    2000             :                          * in the routing domain.
    2001             :                          * Just send an LSAck message to cease retransmission.
    2002             :                          */
    2003           0 :                         if (IS_LSA_MAXAGE(lsa)) {
    2004           0 :                                 zlog_info("LSA[%s]: Boomerang effect?",
    2005             :                                           dump_lsa_key(lsa));
    2006           0 :                                 ospf_ls_ack_send(nbr, lsa);
    2007           0 :                                 ospf_lsa_discard(lsa);
    2008             : 
    2009           0 :                                 if (current != NULL && !IS_LSA_MAXAGE(current))
    2010           0 :                                         ospf_opaque_lsa_refresh_schedule(
    2011             :                                                 current);
    2012           0 :                                 continue;
    2013             :                         }
    2014             : 
    2015             :                         /*
    2016             :                          * If an instance of self-originated Opaque-LSA is not
    2017             :                          * found
    2018             :                          * in the LSDB, there are some possible cases here.
    2019             :                          *
    2020             :                          * 1) This node lost opaque-capability after restart.
    2021             :                          * 2) Else, a part of opaque-type is no more supported.
    2022             :                          * 3) Else, a part of opaque-id is no more supported.
    2023             :                          *
    2024             :                          * Anyway, it is still this node's responsibility to
    2025             :                          * flush it.
    2026             :                          * Otherwise, the LSA instance remains in the routing
    2027             :                          * domain
    2028             :                          * until its age reaches to MaxAge.
    2029             :                          */
    2030             :                         /* XXX: We should deal with this for *ALL* LSAs, not
    2031             :                          * just opaque */
    2032           0 :                         if (current == NULL) {
    2033           0 :                                 if (IS_DEBUG_OSPF_EVENT)
    2034           0 :                                         zlog_debug(
    2035             :                                                 "LSA[%s]: Previously originated Opaque-LSA,not found in the LSDB.",
    2036             :                                                 dump_lsa_key(lsa));
    2037             : 
    2038           0 :                                 SET_FLAG(lsa->flags, OSPF_LSA_SELF);
    2039             : 
    2040           0 :                                 ospf_ls_ack_send(nbr, lsa);
    2041             : 
    2042           0 :                                 if (!ospf->gr_info.restart_in_progress) {
    2043           0 :                                         ospf_opaque_self_originated_lsa_received(
    2044             :                                                 nbr, lsa);
    2045           0 :                                         continue;
    2046             :                                 }
    2047             :                         }
    2048             :                 }
    2049             : 
    2050             :                 /* It might be happen that received LSA is self-originated
    2051             :                  * network LSA, but
    2052             :                  * router ID is changed. So, we should check if LSA is a
    2053             :                  * network-LSA whose
    2054             :                  * Link State ID is one of the router's own IP interface
    2055             :                  * addresses but whose
    2056             :                  * Advertising Router is not equal to the router's own Router ID
    2057             :                  * According to RFC 2328 12.4.2 and 13.4 this LSA should be
    2058             :                  * flushed.
    2059             :                  */
    2060             : 
    2061         112 :                 if (lsa->data->type == OSPF_NETWORK_LSA) {
    2062           9 :                         struct listnode *oinode, *oinnode;
    2063           9 :                         struct ospf_interface *out_if;
    2064           9 :                         int Flag = 0;
    2065             : 
    2066          37 :                         for (ALL_LIST_ELEMENTS(oi->ospf->oiflist, oinode,
    2067             :                                                oinnode, out_if)) {
    2068          19 :                                 if (out_if == NULL)
    2069             :                                         break;
    2070             : 
    2071          19 :                                 if ((IPV4_ADDR_SAME(&out_if->address->u.prefix4,
    2072             :                                                     &lsa->data->id))
    2073           0 :                                     && (!(IPV4_ADDR_SAME(
    2074             :                                                &oi->ospf->router_id,
    2075             :                                                &lsa->data->adv_router)))) {
    2076           0 :                                         if (out_if->network_lsa_self) {
    2077           0 :                                                 ospf_lsa_flush_area(
    2078             :                                                         lsa, out_if->area);
    2079           0 :                                                 if (IS_DEBUG_OSPF_EVENT)
    2080           0 :                                                         zlog_debug(
    2081             :                                                                 "ospf_lsa_discard() in ospf_ls_upd() point 9: lsa %p Type-%d",
    2082             :                                                                 (void *)lsa,
    2083             :                                                                 (int)lsa->data
    2084             :                                                                         ->type);
    2085           0 :                                                 ospf_lsa_discard(lsa);
    2086           0 :                                                 Flag = 1;
    2087             :                                         }
    2088             :                                         break;
    2089             :                                 }
    2090             :                         }
    2091           0 :                         if (Flag)
    2092           0 :                                 continue;
    2093             :                 }
    2094             : 
    2095             :                 /* (5) Find the instance of this LSA that is currently contained
    2096             :                    in the router's link state database.  If there is no
    2097             :                    database copy, or the received LSA is more recent than
    2098             :                    the database copy the following steps must be performed.
    2099             :                    (The sub steps from RFC 2328 section 13 step (5) will be
    2100             :                    performed in
    2101             :                    ospf_flood() ) */
    2102             : 
    2103         112 :                 if (current == NULL
    2104          76 :                     || (ret = ospf_lsa_more_recent(current, lsa)) < 0) {
    2105             :                         /* CVE-2017-3224 */
    2106          47 :                         if (current && (IS_LSA_MAX_SEQ(current))
    2107           0 :                             && (IS_LSA_MAX_SEQ(lsa)) && !IS_LSA_MAXAGE(lsa)) {
    2108           0 :                                 zlog_debug(
    2109             :                                         "Link State Update[%s]: has Max Seq and higher checksum but not MaxAge. Dropping it",
    2110             :                                         dump_lsa_key(lsa));
    2111             : 
    2112           0 :                                 DISCARD_LSA(lsa, 4);
    2113             :                         }
    2114             : 
    2115             :                         /* Actual flooding procedure. */
    2116          83 :                         if (ospf_flood(oi->ospf, nbr, current, lsa)
    2117             :                             < 0) /* Trap NSSA later. */
    2118          25 :                                 DISCARD_LSA(lsa, 5);
    2119          58 :                         continue;
    2120             :                 }
    2121             : 
    2122             :                 /* (6) Else, If there is an instance of the LSA on the sending
    2123             :                    neighbor's Link state request list, an error has occurred in
    2124             :                    the Database Exchange process.  In this case, restart the
    2125             :                    Database Exchange process by generating the neighbor event
    2126             :                    BadLSReq for the sending neighbor and stop processing the
    2127             :                    Link State Update packet. */
    2128             : 
    2129          29 :                 if (ospf_ls_request_lookup(nbr, lsa)) {
    2130           0 :                         OSPF_NSM_EVENT_SCHEDULE(nbr, NSM_BadLSReq);
    2131           0 :                         flog_warn(
    2132             :                                 EC_OSPF_PACKET,
    2133             :                                 "LSA[%s] instance exists on Link state request list",
    2134             :                                 dump_lsa_key(lsa));
    2135             : 
    2136             :                         /* Clean list of LSAs. */
    2137           0 :                         ospf_upd_list_clean(lsas);
    2138             :                         /* this lsa is not on lsas list already. */
    2139           0 :                         ospf_lsa_discard(lsa);
    2140           0 :                         return;
    2141             :                 }
    2142             : 
    2143             :                 /* If the received LSA is the same instance as the database copy
    2144             :                    (i.e., neither one is more recent) the following two steps
    2145             :                    should be performed: */
    2146             : 
    2147          29 :                 if (ret == 0) {
    2148             :                         /* If the LSA is listed in the Link state retransmission
    2149             :                            list
    2150             :                            for the receiving adjacency, the router itself is
    2151             :                            expecting
    2152             :                            an acknowledgment for this LSA.  The router should
    2153             :                            treat the
    2154             :                            received LSA as an acknowledgment by removing the LSA
    2155             :                            from
    2156             :                            the Link state retransmission list.  This is termed
    2157             :                            an
    2158             :                            "implied acknowledgment". */
    2159             : 
    2160          25 :                         ls_ret = ospf_ls_retransmit_lookup(nbr, lsa);
    2161             : 
    2162          25 :                         if (ls_ret != NULL) {
    2163           2 :                                 ospf_ls_retransmit_delete(nbr, ls_ret);
    2164             : 
    2165             :                                 /* Delayed acknowledgment sent if advertisement
    2166             :                                    received
    2167             :                                    from Designated Router, otherwise do nothing.
    2168             :                                    */
    2169           2 :                                 if (oi->state == ISM_Backup)
    2170           1 :                                         if (NBR_IS_DR(nbr))
    2171           1 :                                                 listnode_add(
    2172             :                                                         oi->ls_ack,
    2173           1 :                                                         ospf_lsa_lock(lsa));
    2174             : 
    2175           2 :                                 DISCARD_LSA(lsa, 6);
    2176             :                         } else
    2177             :                         /* Acknowledge the receipt of the LSA by sending a
    2178             :                            Link State Acknowledgment packet back out the
    2179             :                            receiving
    2180             :                            interface. */
    2181             :                         {
    2182          23 :                                 ospf_ls_ack_send(nbr, lsa);
    2183          23 :                                 DISCARD_LSA(lsa, 7);
    2184             :                         }
    2185             :                 }
    2186             : 
    2187             :                 /* The database copy is more recent.  If the database copy
    2188             :                    has LS age equal to MaxAge and LS sequence number equal to
    2189             :                    MaxSequenceNumber, simply discard the received LSA without
    2190             :                    acknowledging it. (In this case, the LSA's LS sequence number
    2191             :                    is
    2192             :                    wrapping, and the MaxSequenceNumber LSA must be completely
    2193             :                    flushed before any new LSA instance can be introduced). */
    2194             : 
    2195           4 :                 else if (ret > 0) /* Database copy is more recent */
    2196             :                 {
    2197           4 :                         if (IS_LSA_MAXAGE(current)
    2198           0 :                             && current->data->ls_seqnum
    2199           0 :                                        == htonl(OSPF_MAX_SEQUENCE_NUMBER)) {
    2200           0 :                                 DISCARD_LSA(lsa, 8);
    2201             :                         }
    2202             :                         /* Otherwise, as long as the database copy has not been
    2203             :                            sent in a
    2204             :                            Link State Update within the last MinLSArrival
    2205             :                            seconds, send the
    2206             :                            database copy back to the sending neighbor,
    2207             :                            encapsulated within
    2208             :                            a Link State Update Packet. The Link State Update
    2209             :                            Packet should
    2210             :                            be sent directly to the neighbor. In so doing, do not
    2211             :                            put the
    2212             :                            database copy of the LSA on the neighbor's link state
    2213             :                            retransmission list, and do not acknowledge the
    2214             :                            received (less
    2215             :                            recent) LSA instance. */
    2216             :                         else {
    2217           4 :                                 if (monotime_since(&current->tv_orig, NULL)
    2218           4 :                                     >= ospf->min_ls_arrival * 1000LL)
    2219             :                                         /* Trap NSSA type later.*/
    2220           0 :                                         ospf_ls_upd_send_lsa(
    2221             :                                                 nbr, current,
    2222             :                                                 OSPF_SEND_PACKET_DIRECT);
    2223           4 :                                 DISCARD_LSA(lsa, 9);
    2224             :                         }
    2225             :                 }
    2226             :         }
    2227             : #undef DISCARD_LSA
    2228             : 
    2229          53 :         assert(listcount(lsas) == 0);
    2230          53 :         list_delete(&lsas);
    2231             : 
    2232          53 :         if (ospf->gr_info.restart_in_progress)
    2233           0 :                 ospf_gr_check_lsdb_consistency(oi->ospf, oi->area);
    2234             : }
    2235             : 
    2236             : /* OSPF Link State Acknowledgment message read -- RFC2328 Section 13.7. */
    2237          28 : static void ospf_ls_ack(struct ip *iph, struct ospf_header *ospfh,
    2238             :                         struct stream *s, struct ospf_interface *oi,
    2239             :                         uint16_t size)
    2240             : {
    2241          28 :         struct ospf_neighbor *nbr;
    2242             : 
    2243             :         /* increment statistics. */
    2244          28 :         oi->ls_ack_in++;
    2245             : 
    2246          28 :         nbr = ospf_nbr_lookup(oi, iph, ospfh);
    2247          28 :         if (nbr == NULL) {
    2248           0 :                 flog_warn(EC_OSPF_PACKET,
    2249             :                           "Link State Acknowledgment: Unknown Neighbor %pI4",
    2250             :                           &ospfh->router_id);
    2251           0 :                 return;
    2252             :         }
    2253             : 
    2254          28 :         if (nbr->state < NSM_Exchange) {
    2255           0 :                 if (IS_DEBUG_OSPF(nsm, NSM_EVENTS))
    2256           0 :                         zlog_debug(
    2257             :                                 "Link State Acknowledgment: Neighbor[%pI4] state %s is less than Exchange",
    2258             :                                 &ospfh->router_id,
    2259             :                                 lookup_msg(ospf_nsm_state_msg, nbr->state,
    2260             :                                            NULL));
    2261           0 :                 return;
    2262             :         }
    2263             : 
    2264         117 :         while (size >= OSPF_LSA_HEADER_SIZE) {
    2265          89 :                 struct ospf_lsa *lsa, *lsr;
    2266             : 
    2267          89 :                 lsa = ospf_lsa_new();
    2268          89 :                 lsa->data = (struct lsa_header *)stream_pnt(s);
    2269          89 :                 lsa->vrf_id = oi->ospf->vrf_id;
    2270             : 
    2271             :                 /* lsah = (struct lsa_header *) stream_pnt (s); */
    2272          89 :                 size -= OSPF_LSA_HEADER_SIZE;
    2273          89 :                 stream_forward_getp(s, OSPF_LSA_HEADER_SIZE);
    2274             : 
    2275          89 :                 if (lsa->data->type < OSPF_MIN_LSA
    2276          89 :                     || lsa->data->type >= OSPF_MAX_LSA) {
    2277           0 :                         lsa->data = NULL;
    2278           0 :                         ospf_lsa_discard(lsa);
    2279           0 :                         continue;
    2280             :                 }
    2281             : 
    2282          89 :                 lsr = ospf_ls_retransmit_lookup(nbr, lsa);
    2283             : 
    2284          89 :                 if (lsr != NULL && ospf_lsa_more_recent(lsr, lsa) == 0) {
    2285          50 :                         ospf_ls_retransmit_delete(nbr, lsr);
    2286          50 :                         ospf_check_and_gen_init_seq_lsa(oi, lsa);
    2287             :                 }
    2288             : 
    2289          89 :                 lsa->data = NULL;
    2290          89 :                 ospf_lsa_discard(lsa);
    2291             :         }
    2292             : 
    2293             :         return;
    2294             : }
    2295             : 
    2296         364 : static struct stream *ospf_recv_packet(struct ospf *ospf, int fd,
    2297             :                                        struct interface **ifp,
    2298             :                                        struct stream *ibuf)
    2299             : {
    2300         364 :         int ret;
    2301         364 :         struct ip *iph;
    2302         364 :         uint16_t ip_len;
    2303         364 :         ifindex_t ifindex = 0;
    2304         364 :         struct iovec iov;
    2305             :         /* Header and data both require alignment. */
    2306         364 :         char buff[CMSG_SPACE(SOPT_SIZE_CMSG_IFINDEX_IPV4())];
    2307         364 :         struct msghdr msgh;
    2308             : 
    2309         364 :         memset(&msgh, 0, sizeof(msgh));
    2310         364 :         msgh.msg_iov = &iov;
    2311         364 :         msgh.msg_iovlen = 1;
    2312         364 :         msgh.msg_control = (caddr_t)buff;
    2313         364 :         msgh.msg_controllen = sizeof(buff);
    2314             : 
    2315         364 :         ret = stream_recvmsg(ibuf, fd, &msgh, MSG_DONTWAIT,
    2316             :                              OSPF_MAX_PACKET_SIZE + 1);
    2317         364 :         if (ret < 0) {
    2318         161 :                 if (errno != EAGAIN && errno != EWOULDBLOCK)
    2319           0 :                         flog_warn(EC_OSPF_PACKET, "stream_recvmsg failed: %s",
    2320             :                                   safe_strerror(errno));
    2321         161 :                 return NULL;
    2322             :         }
    2323         203 :         if ((unsigned int)ret < sizeof(struct ip)) {
    2324           0 :                 flog_warn(
    2325             :                         EC_OSPF_PACKET,
    2326             :                         "%s: discarding runt packet of length %d (ip header size is %u)",
    2327             :                         __func__, ret, (unsigned int)sizeof(iph));
    2328           0 :                 return NULL;
    2329             :         }
    2330             : 
    2331             :         /* Note that there should not be alignment problems with this assignment
    2332             :            because this is at the beginning of the stream data buffer. */
    2333         203 :         iph = (struct ip *)STREAM_DATA(ibuf);
    2334         203 :         sockopt_iphdrincl_swab_systoh(iph);
    2335             : 
    2336         203 :         ip_len = iph->ip_len;
    2337             : 
    2338             : #if defined(__FreeBSD__) && (__FreeBSD_version < 1000000)
    2339             :         /*
    2340             :          * Kernel network code touches incoming IP header parameters,
    2341             :          * before protocol specific processing.
    2342             :          *
    2343             :          *   1) Convert byteorder to host representation.
    2344             :          *      --> ip_len, ip_id, ip_off
    2345             :          *
    2346             :          *   2) Adjust ip_len to strip IP header size!
    2347             :          *      --> If user process receives entire IP packet via RAW
    2348             :          *          socket, it must consider adding IP header size to
    2349             :          *          the "ip_len" field of "ip" structure.
    2350             :          *
    2351             :          * For more details, see <netinet/ip_input.c>.
    2352             :          */
    2353             :         ip_len = ip_len + (iph->ip_hl << 2);
    2354             : #endif
    2355             : 
    2356             : #if defined(__DragonFly__)
    2357             :         /*
    2358             :          * in DragonFly's raw socket, ip_len/ip_off are read
    2359             :          * in network byte order.
    2360             :          * As OpenBSD < 200311 adjust ip_len to strip IP header size!
    2361             :          */
    2362             :         ip_len = ntohs(iph->ip_len) + (iph->ip_hl << 2);
    2363             : #endif
    2364             : 
    2365         203 :         ifindex = getsockopt_ifindex(AF_INET, &msgh);
    2366             : 
    2367         203 :         *ifp = if_lookup_by_index(ifindex, ospf->vrf_id);
    2368             : 
    2369         203 :         if (ret != ip_len) {
    2370           0 :                 flog_warn(
    2371             :                         EC_OSPF_PACKET,
    2372             :                         "%s read length mismatch: ip_len is %d, but recvmsg returned %d",
    2373             :                         __func__, ip_len, ret);
    2374           0 :                 return NULL;
    2375             :         }
    2376             : 
    2377         203 :         if (IS_DEBUG_OSPF_PACKET(0, RECV))
    2378           0 :                 zlog_debug("%s: fd %d(%s) on interface %d(%s)", __func__, fd,
    2379             :                            ospf_get_name(ospf), ifindex,
    2380             :                            *ifp ? (*ifp)->name : "Unknown");
    2381             :         return ibuf;
    2382             : }
    2383             : 
    2384             : static struct ospf_interface *
    2385           0 : ospf_associate_packet_vl(struct ospf *ospf, struct interface *ifp,
    2386             :                          struct ip *iph, struct ospf_header *ospfh)
    2387             : {
    2388           0 :         struct ospf_interface *rcv_oi;
    2389           0 :         struct ospf_vl_data *vl_data;
    2390           0 :         struct ospf_area *vl_area;
    2391           0 :         struct listnode *node;
    2392             : 
    2393           0 :         if (IN_MULTICAST(ntohl(iph->ip_dst.s_addr))
    2394           0 :             || !OSPF_IS_AREA_BACKBONE(ospfh))
    2395             :                 return NULL;
    2396             : 
    2397             :         /* look for local OSPF interface matching the destination
    2398             :          * to determine Area ID. We presume therefore the destination address
    2399             :          * is unique, or at least (for "unnumbered" links), not used in other
    2400             :          * areas
    2401             :          */
    2402           0 :         if ((rcv_oi = ospf_if_lookup_by_local_addr(ospf, NULL, iph->ip_dst))
    2403             :             == NULL)
    2404             :                 return NULL;
    2405             : 
    2406           0 :         for (ALL_LIST_ELEMENTS_RO(ospf->vlinks, node, vl_data)) {
    2407           0 :                 vl_area =
    2408           0 :                         ospf_area_lookup_by_area_id(ospf, vl_data->vl_area_id);
    2409           0 :                 if (!vl_area)
    2410           0 :                         continue;
    2411             : 
    2412           0 :                 if (OSPF_AREA_SAME(&vl_area, &rcv_oi->area)
    2413           0 :                     && IPV4_ADDR_SAME(&vl_data->vl_peer, &ospfh->router_id)) {
    2414           0 :                         if (IS_DEBUG_OSPF_EVENT)
    2415           0 :                                 zlog_debug("associating packet with %s",
    2416             :                                            IF_NAME(vl_data->vl_oi));
    2417           0 :                         if (!CHECK_FLAG(vl_data->vl_oi->ifp->flags, IFF_UP)) {
    2418           0 :                                 if (IS_DEBUG_OSPF_EVENT)
    2419           0 :                                         zlog_debug(
    2420             :                                                 "This VL is not up yet, sorry");
    2421           0 :                                 return NULL;
    2422             :                         }
    2423             : 
    2424             :                         return vl_data->vl_oi;
    2425             :                 }
    2426             :         }
    2427             : 
    2428           0 :         if (IS_DEBUG_OSPF_EVENT)
    2429           0 :                 zlog_debug("couldn't find any VL to associate the packet with");
    2430             : 
    2431             :         return NULL;
    2432             : }
    2433             : 
    2434         203 : static int ospf_check_area_id(struct ospf_interface *oi,
    2435             :                               struct ospf_header *ospfh)
    2436             : {
    2437             :         /* Check match the Area ID of the receiving interface. */
    2438         203 :         if (OSPF_AREA_SAME(&oi->area, &ospfh))
    2439         203 :                 return 1;
    2440             : 
    2441             :         return 0;
    2442             : }
    2443             : 
    2444             : /* Unbound socket will accept any Raw IP packets if proto is matched.
    2445             :    To prevent it, compare src IP address and i/f address with masking
    2446             :    i/f network mask. */
    2447         203 : static int ospf_check_network_mask(struct ospf_interface *oi,
    2448             :                                    struct in_addr ip_src)
    2449             : {
    2450         203 :         struct in_addr mask, me, him;
    2451             : 
    2452         203 :         if (oi->type == OSPF_IFTYPE_POINTOPOINT
    2453         203 :             || oi->type == OSPF_IFTYPE_VIRTUALLINK)
    2454             :                 return 1;
    2455             : 
    2456             :         /* Ignore mask check for max prefix length (32) */
    2457         203 :         if (oi->type == OSPF_IFTYPE_POINTOMULTIPOINT
    2458           0 :             && oi->address->prefixlen == IPV4_MAX_BITLEN)
    2459             :                 return 1;
    2460             : 
    2461         203 :         masklen2ip(oi->address->prefixlen, &mask);
    2462             : 
    2463         203 :         me.s_addr = oi->address->u.prefix4.s_addr & mask.s_addr;
    2464         203 :         him.s_addr = ip_src.s_addr & mask.s_addr;
    2465             : 
    2466         203 :         if (IPV4_ADDR_SAME(&me, &him))
    2467             :                 return 1;
    2468             : 
    2469             :         return 0;
    2470             : }
    2471             : 
    2472             : /* Return 1, if the packet is properly authenticated and checksummed,
    2473             :    0 otherwise. In particular, check that AuType header field is valid and
    2474             :    matches the locally configured AuType, and that D.5 requirements are met. */
    2475         203 : static int ospf_check_auth(struct ospf_interface *oi, struct ospf_header *ospfh)
    2476             : {
    2477         203 :         struct crypt_key *ck;
    2478         203 :         uint16_t iface_auth_type;
    2479         203 :         uint16_t pkt_auth_type = ntohs(ospfh->auth_type);
    2480             : 
    2481         203 :         switch (pkt_auth_type) {
    2482         203 :         case OSPF_AUTH_NULL: /* RFC2328 D.5.1 */
    2483         203 :                 if (OSPF_AUTH_NULL != (iface_auth_type = ospf_auth_type(oi))) {
    2484           0 :                         if (IS_DEBUG_OSPF_PACKET(ospfh->type - 1, RECV))
    2485           0 :                                 flog_warn(
    2486             :                                         EC_OSPF_PACKET,
    2487             :                                         "interface %s: auth-type mismatch, local %s, rcvd Null",
    2488             :                                         IF_NAME(oi),
    2489             :                                         lookup_msg(ospf_auth_type_str,
    2490             :                                                    iface_auth_type, NULL));
    2491           0 :                         return 0;
    2492             :                 }
    2493         203 :                 if (!ospf_check_sum(ospfh)) {
    2494           0 :                         if (IS_DEBUG_OSPF_PACKET(ospfh->type - 1, RECV))
    2495           0 :                                 flog_warn(
    2496             :                                         EC_OSPF_PACKET,
    2497             :                                         "interface %s: Null auth OK, but checksum error, Router-ID %pI4",
    2498             :                                         IF_NAME(oi),
    2499             :                                         &ospfh->router_id);
    2500           0 :                         return 0;
    2501             :                 }
    2502             :                 return 1;
    2503           0 :         case OSPF_AUTH_SIMPLE: /* RFC2328 D.5.2 */
    2504           0 :                 if (OSPF_AUTH_SIMPLE
    2505           0 :                     != (iface_auth_type = ospf_auth_type(oi))) {
    2506           0 :                         if (IS_DEBUG_OSPF_PACKET(ospfh->type - 1, RECV))
    2507           0 :                                 flog_warn(
    2508             :                                         EC_OSPF_PACKET,
    2509             :                                         "interface %s: auth-type mismatch, local %s, rcvd Simple",
    2510             :                                         IF_NAME(oi),
    2511             :                                         lookup_msg(ospf_auth_type_str,
    2512             :                                                    iface_auth_type, NULL));
    2513           0 :                         return 0;
    2514             :                 }
    2515           0 :                 if (memcmp(OSPF_IF_PARAM(oi, auth_simple), ospfh->u.auth_data,
    2516             :                            OSPF_AUTH_SIMPLE_SIZE)) {
    2517           0 :                         if (IS_DEBUG_OSPF_PACKET(ospfh->type - 1, RECV))
    2518           0 :                                 flog_warn(EC_OSPF_PACKET,
    2519             :                                           "interface %s: Simple auth failed",
    2520             :                                           IF_NAME(oi));
    2521           0 :                         return 0;
    2522             :                 }
    2523           0 :                 if (!ospf_check_sum(ospfh)) {
    2524           0 :                         if (IS_DEBUG_OSPF_PACKET(ospfh->type - 1, RECV))
    2525           0 :                                 flog_warn(
    2526             :                                         EC_OSPF_PACKET,
    2527             :                                         "interface %s: Simple auth OK, checksum error, Router-ID %pI4",
    2528             :                                         IF_NAME(oi),
    2529             :                                         &ospfh->router_id);
    2530           0 :                         return 0;
    2531             :                 }
    2532             :                 return 1;
    2533           0 :         case OSPF_AUTH_CRYPTOGRAPHIC: /* RFC2328 D.5.3 */
    2534           0 :                 if (OSPF_AUTH_CRYPTOGRAPHIC
    2535           0 :                     != (iface_auth_type = ospf_auth_type(oi))) {
    2536           0 :                         if (IS_DEBUG_OSPF_PACKET(ospfh->type - 1, RECV))
    2537           0 :                                 flog_warn(
    2538             :                                         EC_OSPF_PACKET,
    2539             :                                         "interface %s: auth-type mismatch, local %s, rcvd Cryptographic",
    2540             :                                         IF_NAME(oi),
    2541             :                                         lookup_msg(ospf_auth_type_str,
    2542             :                                                    iface_auth_type, NULL));
    2543           0 :                         return 0;
    2544             :                 }
    2545           0 :                 if (ospfh->checksum) {
    2546           0 :                         if (IS_DEBUG_OSPF_PACKET(ospfh->type - 1, RECV))
    2547           0 :                                 flog_warn(
    2548             :                                         EC_OSPF_PACKET,
    2549             :                                         "interface %s: OSPF header checksum is not 0",
    2550             :                                         IF_NAME(oi));
    2551           0 :                         return 0;
    2552             :                 }
    2553             :                 /* only MD5 crypto method can pass ospf_packet_examin() */
    2554           0 :                 if (NULL == (ck = listgetdata(
    2555             :                                      listtail(OSPF_IF_PARAM(oi, auth_crypt))))
    2556           0 :                     || ospfh->u.crypt.key_id != ck->key_id ||
    2557             :                     /* Condition above uses the last key ID on the list,
    2558             :                        which is
    2559             :                        different from what ospf_crypt_key_lookup() does. A
    2560             :                        bug? */
    2561           0 :                     !ospf_check_md5_digest(oi, ospfh)) {
    2562           0 :                         if (IS_DEBUG_OSPF_PACKET(ospfh->type - 1, RECV))
    2563           0 :                                 flog_warn(EC_OSPF_MD5,
    2564             :                                           "interface %s: MD5 auth failed",
    2565             :                                           IF_NAME(oi));
    2566           0 :                         return 0;
    2567             :                 }
    2568             :                 return 1;
    2569           0 :         default:
    2570           0 :                 if (IS_DEBUG_OSPF_PACKET(ospfh->type - 1, RECV))
    2571           0 :                         flog_warn(
    2572             :                                 EC_OSPF_PACKET,
    2573             :                                 "interface %s: invalid packet auth-type (%02x)",
    2574             :                                 IF_NAME(oi), pkt_auth_type);
    2575             :                 return 0;
    2576             :         }
    2577             : }
    2578             : 
    2579         203 : static int ospf_check_sum(struct ospf_header *ospfh)
    2580             : {
    2581         203 :         uint32_t ret;
    2582         203 :         uint16_t sum;
    2583             : 
    2584             :         /* clear auth_data for checksum. */
    2585         203 :         memset(ospfh->u.auth_data, 0, OSPF_AUTH_SIMPLE_SIZE);
    2586             : 
    2587             :         /* keep checksum and clear. */
    2588         203 :         sum = ospfh->checksum;
    2589         203 :         memset(&ospfh->checksum, 0, sizeof(uint16_t));
    2590             : 
    2591             :         /* calculate checksum. */
    2592         203 :         ret = in_cksum(ospfh, ntohs(ospfh->length));
    2593             : 
    2594         203 :         if (ret != sum) {
    2595           0 :                 zlog_info("%s: checksum mismatch, my %X, his %X", __func__, ret,
    2596             :                           sum);
    2597           0 :                 return 0;
    2598             :         }
    2599             : 
    2600             :         return 1;
    2601             : }
    2602             : 
    2603             : /* Verify, that given link/TOS records are properly sized/aligned and match
    2604             :    Router-LSA "# links" and "# TOS" fields as specified in RFC2328 A.4.2. */
    2605          54 : static unsigned ospf_router_lsa_links_examin(struct router_lsa_link *link,
    2606             :                                              uint16_t linkbytes,
    2607             :                                              const uint16_t num_links)
    2608             : {
    2609          54 :         unsigned counted_links = 0, thislinklen;
    2610             : 
    2611         157 :         while (linkbytes >= OSPF_ROUTER_LSA_LINK_SIZE) {
    2612         103 :                 thislinklen =
    2613         103 :                         OSPF_ROUTER_LSA_LINK_SIZE + 4 * link->m[0].tos_count;
    2614         103 :                 if (thislinklen > linkbytes) {
    2615           0 :                         if (IS_DEBUG_OSPF_PACKET(0, RECV))
    2616           0 :                                 zlog_debug("%s: length error in link block #%u",
    2617             :                                            __func__, counted_links);
    2618           0 :                         return MSG_NG;
    2619             :                 }
    2620         103 :                 link = (struct router_lsa_link *)((caddr_t)link + thislinklen);
    2621         103 :                 linkbytes -= thislinklen;
    2622         103 :                 counted_links++;
    2623             :         }
    2624          54 :         if (counted_links != num_links) {
    2625           0 :                 if (IS_DEBUG_OSPF_PACKET(0, RECV))
    2626           0 :                         zlog_debug("%s: %u link blocks declared, %u present",
    2627             :                                    __func__, num_links, counted_links);
    2628           0 :                 return MSG_NG;
    2629             :         }
    2630             :         return MSG_OK;
    2631             : }
    2632             : 
    2633             : /* Verify, that the given LSA is properly sized/aligned (including type-specific
    2634             :    minimum length constraint). */
    2635         223 : static unsigned ospf_lsa_examin(struct lsa_header *lsah, const uint16_t lsalen,
    2636             :                                 const uint8_t headeronly)
    2637             : {
    2638         223 :         unsigned ret;
    2639         223 :         struct router_lsa *rlsa;
    2640         223 :         if (lsah->type < OSPF_MAX_LSA && ospf_lsa_minlen[lsah->type]
    2641         223 :             && lsalen < OSPF_LSA_HEADER_SIZE + ospf_lsa_minlen[lsah->type]) {
    2642           0 :                 if (IS_DEBUG_OSPF_PACKET(0, RECV))
    2643           0 :                         zlog_debug("%s: undersized (%u B) %s", __func__, lsalen,
    2644             :                                    lookup_msg(ospf_lsa_type_msg, lsah->type,
    2645             :                                               NULL));
    2646           0 :                 return MSG_NG;
    2647             :         }
    2648         223 :         switch (lsah->type) {
    2649          90 :         case OSPF_ROUTER_LSA: {
    2650             :                 /*
    2651             :                  * RFC2328 A.4.2, LSA header + 4 bytes followed by N>=0
    2652             :                  * (12+)-byte link blocks
    2653             :                  */
    2654          90 :                 size_t linkbytes_len = lsalen - OSPF_LSA_HEADER_SIZE
    2655          90 :                                        - OSPF_ROUTER_LSA_MIN_SIZE;
    2656             : 
    2657             :                 /*
    2658             :                  * LSA link blocks are variable length but always multiples of
    2659             :                  * 4; basic sanity check
    2660             :                  */
    2661          90 :                 if (linkbytes_len % 4 != 0)
    2662             :                         return MSG_NG;
    2663             : 
    2664          90 :                 if (headeronly)
    2665             :                         return MSG_OK;
    2666             : 
    2667          54 :                 rlsa = (struct router_lsa *)lsah;
    2668             : 
    2669          54 :                 ret = ospf_router_lsa_links_examin(
    2670          54 :                         (struct router_lsa_link *)rlsa->link,
    2671             :                         linkbytes_len,
    2672          54 :                         ntohs(rlsa->links));
    2673          54 :                 break;
    2674             :         }
    2675          60 :         case OSPF_AS_EXTERNAL_LSA:
    2676             :         /* RFC2328 A.4.5, LSA header + 4 bytes followed by N>=1 12-bytes long
    2677             :          * blocks */
    2678             :         case OSPF_AS_NSSA_LSA:
    2679             :                 /* RFC3101 C, idem */
    2680          60 :                 ret = (lsalen - OSPF_LSA_HEADER_SIZE
    2681          60 :                        - OSPF_AS_EXTERNAL_LSA_MIN_SIZE)
    2682          60 :                                       % 12
    2683             :                               ? MSG_NG
    2684          60 :                               : MSG_OK;
    2685          60 :                 break;
    2686             :         /* Following LSA types are considered OK length-wise as soon as their
    2687             :          * minimum
    2688             :          * length constraint is met and length of the whole LSA is a multiple of
    2689             :          * 4
    2690             :          * (basic LSA header size is already a multiple of 4). */
    2691          73 :         case OSPF_NETWORK_LSA:
    2692             :         /* RFC2328 A.4.3, LSA header + 4 bytes followed by N>=1 router-IDs */
    2693             :         case OSPF_SUMMARY_LSA:
    2694             :         case OSPF_ASBR_SUMMARY_LSA:
    2695             :         /* RFC2328 A.4.4, LSA header + 4 bytes followed by N>=1 4-bytes TOS
    2696             :          * blocks */
    2697             :         case OSPF_OPAQUE_LINK_LSA:
    2698             :         case OSPF_OPAQUE_AREA_LSA:
    2699             :         case OSPF_OPAQUE_AS_LSA:
    2700             :                 /* RFC5250 A.2, "some number of octets (of application-specific
    2701             :                  * data) padded to 32-bit alignment." This is considered
    2702             :                  * equivalent
    2703             :                  * to 4-byte alignment of all other LSA types, see
    2704             :                  * OSPF-ALIGNMENT.txt
    2705             :                  * file for the detailed analysis of this passage. */
    2706          73 :                 ret = lsalen % 4 ? MSG_NG : MSG_OK;
    2707          73 :                 break;
    2708           0 :         default:
    2709           0 :                 if (IS_DEBUG_OSPF_PACKET(0, RECV))
    2710           0 :                         zlog_debug("%s: unsupported LSA type 0x%02x", __func__,
    2711             :                                    lsah->type);
    2712             :                 return MSG_NG;
    2713             :         }
    2714         187 :         if (ret != MSG_OK && IS_DEBUG_OSPF_PACKET(0, RECV))
    2715           0 :                 zlog_debug("%s: alignment error in %s", __func__,
    2716             :                            lookup_msg(ospf_lsa_type_msg, lsah->type, NULL));
    2717             :         return ret;
    2718             : }
    2719             : 
    2720             : /* Verify if the provided input buffer is a valid sequence of LSAs. This
    2721             :    includes verification of LSA blocks length/alignment and dispatching
    2722             :    of deeper-level checks. */
    2723             : static unsigned
    2724         101 : ospf_lsaseq_examin(struct lsa_header *lsah, /* start of buffered data */
    2725             :                    size_t length, const uint8_t headeronly,
    2726             :                    /* When declared_num_lsas is not 0, compare it to the real
    2727             :                       number of LSAs
    2728             :                       and treat the difference as an error. */
    2729             :                    const uint32_t declared_num_lsas)
    2730             : {
    2731         101 :         uint32_t counted_lsas = 0;
    2732             : 
    2733         324 :         while (length) {
    2734         223 :                 uint16_t lsalen;
    2735         223 :                 if (length < OSPF_LSA_HEADER_SIZE) {
    2736           0 :                         if (IS_DEBUG_OSPF_PACKET(0, RECV))
    2737           0 :                                 zlog_debug(
    2738             :                                         "%s: undersized (%zu B) trailing (#%u) LSA header",
    2739             :                                         __func__, length, counted_lsas);
    2740           0 :                         return MSG_NG;
    2741             :                 }
    2742             :                 /* save on ntohs() calls here and in the LSA validator */
    2743         223 :                 lsalen = ntohs(lsah->length);
    2744         223 :                 if (lsalen < OSPF_LSA_HEADER_SIZE) {
    2745           0 :                         if (IS_DEBUG_OSPF_PACKET(0, RECV))
    2746           0 :                                 zlog_debug(
    2747             :                                         "%s: malformed LSA header #%u, declared length is %u B",
    2748             :                                         __func__, counted_lsas, lsalen);
    2749           0 :                         return MSG_NG;
    2750             :                 }
    2751         223 :                 if (headeronly) {
    2752             :                         /* less checks here and in ospf_lsa_examin() */
    2753         111 :                         if (MSG_OK != ospf_lsa_examin(lsah, lsalen, 1)) {
    2754           0 :                                 if (IS_DEBUG_OSPF_PACKET(0, RECV))
    2755           0 :                                         zlog_debug(
    2756             :                                                 "%s: malformed header-only LSA #%u",
    2757             :                                                 __func__, counted_lsas);
    2758           0 :                                 return MSG_NG;
    2759             :                         }
    2760         111 :                         lsah = (struct lsa_header *)((caddr_t)lsah
    2761             :                                                      + OSPF_LSA_HEADER_SIZE);
    2762         111 :                         length -= OSPF_LSA_HEADER_SIZE;
    2763             :                 } else {
    2764             :                         /* make sure the input buffer is deep enough before
    2765             :                          * further checks */
    2766         112 :                         if (lsalen > length) {
    2767           0 :                                 if (IS_DEBUG_OSPF_PACKET(0, RECV))
    2768           0 :                                         zlog_debug(
    2769             :                                                 "%s: anomaly in LSA #%u: declared length is %u B, buffered length is %zu B",
    2770             :                                                 __func__, counted_lsas, lsalen,
    2771             :                                                 length);
    2772           0 :                                 return MSG_NG;
    2773             :                         }
    2774         112 :                         if (MSG_OK != ospf_lsa_examin(lsah, lsalen, 0)) {
    2775           0 :                                 if (IS_DEBUG_OSPF_PACKET(0, RECV))
    2776           0 :                                         zlog_debug("%s: malformed LSA #%u",
    2777             :                                                    __func__, counted_lsas);
    2778           0 :                                 return MSG_NG;
    2779             :                         }
    2780         112 :                         lsah = (struct lsa_header *)((caddr_t)lsah + lsalen);
    2781         112 :                         length -= lsalen;
    2782             :                 }
    2783         223 :                 counted_lsas++;
    2784             :         }
    2785             : 
    2786         101 :         if (declared_num_lsas && counted_lsas != declared_num_lsas) {
    2787           0 :                 if (IS_DEBUG_OSPF_PACKET(0, RECV))
    2788           0 :                         zlog_debug(
    2789             :                                 "%s: #LSAs declared (%u) does not match actual (%u)",
    2790             :                                 __func__, declared_num_lsas, counted_lsas);
    2791           0 :                 return MSG_NG;
    2792             :         }
    2793             :         return MSG_OK;
    2794             : }
    2795             : 
    2796             : /* Verify a complete OSPF packet for proper sizing/alignment. */
    2797         203 : static unsigned ospf_packet_examin(struct ospf_header *oh,
    2798             :                                    const unsigned bytesonwire)
    2799             : {
    2800         203 :         uint16_t bytesdeclared, bytesauth;
    2801         203 :         unsigned ret;
    2802         203 :         struct ospf_ls_update *lsupd;
    2803             : 
    2804             :         /* Length, 1st approximation. */
    2805         203 :         if (bytesonwire < OSPF_HEADER_SIZE) {
    2806           0 :                 if (IS_DEBUG_OSPF_PACKET(0, RECV))
    2807           0 :                         zlog_debug("%s: undersized (%u B) packet", __func__,
    2808             :                                    bytesonwire);
    2809           0 :                 return MSG_NG;
    2810             :         }
    2811             :         /* Now it is safe to access header fields. Performing length check,
    2812             :          * allow
    2813             :          * for possible extra bytes of crypto auth/padding, which are not
    2814             :          * counted
    2815             :          * in the OSPF header "length" field. */
    2816         203 :         if (oh->version != OSPF_VERSION) {
    2817           0 :                 if (IS_DEBUG_OSPF_PACKET(0, RECV))
    2818           0 :                         zlog_debug("%s: invalid (%u) protocol version",
    2819             :                                    __func__, oh->version);
    2820           0 :                 return MSG_NG;
    2821             :         }
    2822         203 :         bytesdeclared = ntohs(oh->length);
    2823         203 :         if (ntohs(oh->auth_type) != OSPF_AUTH_CRYPTOGRAPHIC)
    2824             :                 bytesauth = 0;
    2825             :         else {
    2826           0 :                 if (oh->u.crypt.auth_data_len != OSPF_AUTH_MD5_SIZE) {
    2827           0 :                         if (IS_DEBUG_OSPF_PACKET(0, RECV))
    2828           0 :                                 zlog_debug(
    2829             :                                         "%s: unsupported crypto auth length (%u B)",
    2830             :                                         __func__, oh->u.crypt.auth_data_len);
    2831           0 :                         return MSG_NG;
    2832             :                 }
    2833             :                 bytesauth = OSPF_AUTH_MD5_SIZE;
    2834             :         }
    2835         203 :         if (bytesdeclared + bytesauth > bytesonwire) {
    2836           0 :                 if (IS_DEBUG_OSPF_PACKET(0, RECV))
    2837           0 :                         zlog_debug(
    2838             :                                 "%s: packet length error (%u real, %u+%u declared)",
    2839             :                                 __func__, bytesonwire, bytesdeclared,
    2840             :                                 bytesauth);
    2841           0 :                 return MSG_NG;
    2842             :         }
    2843             :         /* Length, 2nd approximation. The type-specific constraint is checked
    2844             :            against declared length, not amount of bytes on wire. */
    2845         203 :         if (oh->type >= OSPF_MSG_HELLO && oh->type <= OSPF_MSG_LS_ACK
    2846         203 :             && bytesdeclared
    2847         203 :                        < OSPF_HEADER_SIZE + ospf_packet_minlen[oh->type]) {
    2848           0 :                 if (IS_DEBUG_OSPF_PACKET(0, RECV))
    2849           0 :                         zlog_debug("%s: undersized (%u B) %s packet", __func__,
    2850             :                                    bytesdeclared,
    2851             :                                    lookup_msg(ospf_packet_type_str, oh->type,
    2852             :                                               NULL));
    2853           0 :                 return MSG_NG;
    2854             :         }
    2855         203 :         switch (oh->type) {
    2856          94 :         case OSPF_MSG_HELLO:
    2857             :                 /* RFC2328 A.3.2, packet header + OSPF_HELLO_MIN_SIZE bytes
    2858             :                    followed
    2859             :                    by N>=0 router-IDs. */
    2860          94 :                 ret = (bytesdeclared - OSPF_HEADER_SIZE - OSPF_HELLO_MIN_SIZE)
    2861             :                                       % 4
    2862             :                               ? MSG_NG
    2863          94 :                               : MSG_OK;
    2864          94 :                 break;
    2865          20 :         case OSPF_MSG_DB_DESC:
    2866             :                 /* RFC2328 A.3.3, packet header + OSPF_DB_DESC_MIN_SIZE bytes
    2867             :                    followed
    2868             :                    by N>=0 header-only LSAs. */
    2869          20 :                 ret = ospf_lsaseq_examin(
    2870             :                         (struct lsa_header *)((caddr_t)oh + OSPF_HEADER_SIZE
    2871             :                                               + OSPF_DB_DESC_MIN_SIZE),
    2872             :                         bytesdeclared - OSPF_HEADER_SIZE
    2873          20 :                                 - OSPF_DB_DESC_MIN_SIZE,
    2874             :                         1, /* header-only LSAs */
    2875             :                         0);
    2876          20 :                 break;
    2877           8 :         case OSPF_MSG_LS_REQ:
    2878             :                 /* RFC2328 A.3.4, packet header followed by N>=0 12-bytes
    2879             :                  * request blocks. */
    2880           8 :                 ret = (bytesdeclared - OSPF_HEADER_SIZE - OSPF_LS_REQ_MIN_SIZE)
    2881           8 :                                       % OSPF_LSA_KEY_SIZE
    2882             :                               ? MSG_NG
    2883           8 :                               : MSG_OK;
    2884           8 :                 break;
    2885          53 :         case OSPF_MSG_LS_UPD:
    2886             :                 /* RFC2328 A.3.5, packet header + OSPF_LS_UPD_MIN_SIZE bytes
    2887             :                    followed
    2888             :                    by N>=0 full LSAs (with N declared beforehand). */
    2889          53 :                 lsupd = (struct ospf_ls_update *)((caddr_t)oh
    2890             :                                                   + OSPF_HEADER_SIZE);
    2891          53 :                 ret = ospf_lsaseq_examin(
    2892             :                         (struct lsa_header *)((caddr_t)lsupd
    2893             :                                               + OSPF_LS_UPD_MIN_SIZE),
    2894          53 :                         bytesdeclared - OSPF_HEADER_SIZE - OSPF_LS_UPD_MIN_SIZE,
    2895             :                         0,                     /* full LSAs */
    2896             :                         ntohl(lsupd->num_lsas) /* 32 bits */
    2897             :                         );
    2898          53 :                 break;
    2899          28 :         case OSPF_MSG_LS_ACK:
    2900             :                 /* RFC2328 A.3.6, packet header followed by N>=0 header-only
    2901             :                  * LSAs. */
    2902          28 :                 ret = ospf_lsaseq_examin(
    2903             :                         (struct lsa_header *)((caddr_t)oh + OSPF_HEADER_SIZE
    2904             :                                               + OSPF_LS_ACK_MIN_SIZE),
    2905          28 :                         bytesdeclared - OSPF_HEADER_SIZE - OSPF_LS_ACK_MIN_SIZE,
    2906             :                         1, /* header-only LSAs */
    2907             :                         0);
    2908          28 :                 break;
    2909           0 :         default:
    2910           0 :                 if (IS_DEBUG_OSPF_PACKET(0, RECV))
    2911           0 :                         zlog_debug("%s: invalid packet type 0x%02x", __func__,
    2912             :                                    oh->type);
    2913             :                 return MSG_NG;
    2914             :         }
    2915         203 :         if (ret != MSG_OK && IS_DEBUG_OSPF_PACKET(0, RECV))
    2916           0 :                 zlog_debug("%s: malformed %s packet", __func__,
    2917             :                            lookup_msg(ospf_packet_type_str, oh->type, NULL));
    2918             :         return ret;
    2919             : }
    2920             : 
    2921             : /* OSPF Header verification. */
    2922         203 : static int ospf_verify_header(struct stream *ibuf, struct ospf_interface *oi,
    2923             :                               struct ip *iph, struct ospf_header *ospfh)
    2924             : {
    2925             :         /* Check Area ID. */
    2926         203 :         if (!ospf_check_area_id(oi, ospfh)) {
    2927           0 :                 flog_warn(EC_OSPF_PACKET,
    2928             :                           "interface %s: ospf_read invalid Area ID %pI4",
    2929             :                           IF_NAME(oi), &ospfh->area_id);
    2930           0 :                 return -1;
    2931             :         }
    2932             : 
    2933             :         /* Check network mask, Silently discarded. */
    2934         203 :         if (!ospf_check_network_mask(oi, iph->ip_src)) {
    2935           0 :                 flog_warn(
    2936             :                         EC_OSPF_PACKET,
    2937             :                         "interface %s: ospf_read network address is not same [%pI4]",
    2938             :                         IF_NAME(oi), &iph->ip_src);
    2939           0 :                 return -1;
    2940             :         }
    2941             : 
    2942             :         /* Check authentication. The function handles logging actions, where
    2943             :          * required. */
    2944         203 :         if (!ospf_check_auth(oi, ospfh))
    2945             :                 return -1;
    2946             : 
    2947             :         return 0;
    2948             : }
    2949             : 
    2950             : enum ospf_read_return_enum {
    2951             :         OSPF_READ_ERROR,
    2952             :         OSPF_READ_CONTINUE,
    2953             : };
    2954             : 
    2955         364 : static enum ospf_read_return_enum ospf_read_helper(struct ospf *ospf)
    2956             : {
    2957         364 :         int ret;
    2958         364 :         struct stream *ibuf;
    2959         364 :         struct ospf_interface *oi;
    2960         364 :         struct ip *iph;
    2961         364 :         struct ospf_header *ospfh;
    2962         364 :         uint16_t length;
    2963         364 :         struct connected *c;
    2964         364 :         struct interface *ifp = NULL;
    2965             : 
    2966         364 :         stream_reset(ospf->ibuf);
    2967         364 :         ibuf = ospf_recv_packet(ospf, ospf->fd, &ifp, ospf->ibuf);
    2968         364 :         if (ibuf == NULL)
    2969             :                 return OSPF_READ_ERROR;
    2970             : 
    2971             :         /*
    2972             :          * This raw packet is known to be at least as big as its
    2973             :          * IP header. Note that there should not be alignment problems with
    2974             :          * this assignment because this is at the beginning of the
    2975             :          * stream data buffer.
    2976             :          */
    2977         203 :         iph = (struct ip *)STREAM_DATA(ibuf);
    2978             :         /*
    2979             :          * Note that sockopt_iphdrincl_swab_systoh was called in
    2980             :          * ospf_recv_packet.
    2981             :          */
    2982         203 :         if (ifp == NULL) {
    2983             :                 /*
    2984             :                  * Handle cases where the platform does not support
    2985             :                  * retrieving the ifindex, and also platforms (such as
    2986             :                  * Solaris 8) that claim to support ifindex retrieval but do
    2987             :                  * not.
    2988             :                  */
    2989           0 :                 c = if_lookup_address((void *)&iph->ip_src, AF_INET,
    2990             :                                       ospf->vrf_id);
    2991           0 :                 if (c)
    2992           0 :                         ifp = c->ifp;
    2993           0 :                 if (ifp == NULL) {
    2994           0 :                         if (IS_DEBUG_OSPF_PACKET(0, RECV))
    2995           0 :                                 zlog_debug(
    2996             :                                         "%s: Unable to determine incoming interface from: %pI4(%s)",
    2997             :                                         __func__, &iph->ip_src,
    2998             :                                         ospf_get_name(ospf));
    2999           0 :                         return OSPF_READ_CONTINUE;
    3000             :                 }
    3001             :         }
    3002             : 
    3003         203 :         if (ospf->vrf_id == VRF_DEFAULT && ospf->vrf_id != ifp->vrf->vrf_id) {
    3004             :                 /*
    3005             :                  * We may have a situation where l3mdev_accept == 1
    3006             :                  * let's just kindly drop the packet and move on.
    3007             :                  * ospf really really really does not like when
    3008             :                  * we receive the same packet multiple times.
    3009             :                  */
    3010             :                 return OSPF_READ_CONTINUE;
    3011             :         }
    3012             : 
    3013             :         /* Self-originated packet should be discarded silently. */
    3014         203 :         if (ospf_if_lookup_by_local_addr(ospf, NULL, iph->ip_src)) {
    3015           0 :                 if (IS_DEBUG_OSPF_PACKET(0, RECV)) {
    3016           0 :                         zlog_debug(
    3017             :                                 "ospf_read[%pI4]: Dropping self-originated packet",
    3018             :                                 &iph->ip_src);
    3019             :                 }
    3020           0 :                 return OSPF_READ_CONTINUE;
    3021             :         }
    3022             : 
    3023             :         /* Check that we have enough for an IP header */
    3024         203 :         if ((unsigned int)(iph->ip_hl << 2) >= STREAM_READABLE(ibuf)) {
    3025           0 :                 if ((unsigned int)(iph->ip_hl << 2) == STREAM_READABLE(ibuf)) {
    3026           0 :                         flog_warn(
    3027             :                                 EC_OSPF_PACKET,
    3028             :                                 "Rx'd IP packet with OSPF protocol number but no payload");
    3029             :                 } else {
    3030           0 :                         flog_warn(
    3031             :                                 EC_OSPF_PACKET,
    3032             :                                 "IP header length field claims header is %u bytes, but we only have %zu",
    3033             :                                 (unsigned int)(iph->ip_hl << 2),
    3034             :                                 STREAM_READABLE(ibuf));
    3035             :                 }
    3036             : 
    3037           0 :                 return OSPF_READ_ERROR;
    3038             :         }
    3039         203 :         stream_forward_getp(ibuf, iph->ip_hl << 2);
    3040             : 
    3041         203 :         ospfh = (struct ospf_header *)stream_pnt(ibuf);
    3042         203 :         if (MSG_OK
    3043         406 :             != ospf_packet_examin(ospfh, stream_get_endp(ibuf)
    3044         203 :                                                  - stream_get_getp(ibuf)))
    3045             :                 return OSPF_READ_CONTINUE;
    3046             :         /* Now it is safe to access all fields of OSPF packet header. */
    3047             : 
    3048             :         /* associate packet with ospf interface */
    3049         203 :         oi = ospf_if_lookup_recv_if(ospf, iph->ip_src, ifp);
    3050             : 
    3051             :         /*
    3052             :          * ospf_verify_header() relies on a valid "oi" and thus can be called
    3053             :          * only after the passive/backbone/other checks below are passed.
    3054             :          * These checks in turn access the fields of unverified "ospfh"
    3055             :          * structure for their own purposes and must remain very accurate
    3056             :          * in doing this.
    3057             :          */
    3058             : 
    3059             :         /* If incoming interface is passive one, ignore it. */
    3060         203 :         if (oi && OSPF_IF_PASSIVE_STATUS(oi) == OSPF_IF_PASSIVE) {
    3061           0 :                 if (IS_DEBUG_OSPF_EVENT)
    3062           0 :                         zlog_debug(
    3063             :                                 "ignoring packet from router %pI4 sent to %pI4, received on a passive interface, %pI4",
    3064             :                                 &ospfh->router_id, &iph->ip_dst,
    3065             :                                 &oi->address->u.prefix4);
    3066             : 
    3067           0 :                 if (iph->ip_dst.s_addr == htonl(OSPF_ALLSPFROUTERS)) {
    3068             :                         /* Try to fix multicast membership.
    3069             :                          * Some OS:es may have problems in this area,
    3070             :                          * make sure it is removed.
    3071             :                          */
    3072           0 :                         OI_MEMBER_JOINED(oi, MEMBER_ALLROUTERS);
    3073           0 :                         ospf_if_set_multicast(oi);
    3074             :                 }
    3075           0 :                 return OSPF_READ_CONTINUE;
    3076             :         }
    3077             : 
    3078             : 
    3079             :         /* if no local ospf_interface,
    3080             :          * or header area is backbone but ospf_interface is not
    3081             :          * check for VLINK interface
    3082             :          */
    3083         203 :         if ((oi == NULL)
    3084         203 :             || (OSPF_IS_AREA_ID_BACKBONE(ospfh->area_id)
    3085         144 :                 && !OSPF_IS_AREA_ID_BACKBONE(oi->area->area_id))) {
    3086           0 :                 if ((oi = ospf_associate_packet_vl(ospf, ifp, iph, ospfh))
    3087             :                     == NULL) {
    3088           0 :                         if (!ospf->instance && IS_DEBUG_OSPF_EVENT)
    3089           0 :                                 zlog_debug(
    3090             :                                         "Packet from [%pI4] received on link %s but no ospf_interface",
    3091             :                                         &iph->ip_src, ifp->name);
    3092           0 :                         return OSPF_READ_CONTINUE;
    3093             :                 }
    3094             :         }
    3095             : 
    3096             :         /*
    3097             :          * else it must be a local ospf interface, check it was
    3098             :          * received on correct link
    3099             :          */
    3100         203 :         else if (oi->ifp != ifp) {
    3101           0 :                 if (IS_DEBUG_OSPF_EVENT)
    3102           0 :                         flog_warn(EC_OSPF_PACKET,
    3103             :                                   "Packet from [%pI4] received on wrong link %s",
    3104             :                                   &iph->ip_src, ifp->name);
    3105           0 :                 return OSPF_READ_CONTINUE;
    3106         203 :         } else if (oi->state == ISM_Down) {
    3107           0 :                 flog_warn(
    3108             :                         EC_OSPF_PACKET,
    3109             :                         "Ignoring packet from %pI4 to %pI4 received on interface that is down [%s]; interface flags are %s",
    3110             :                         &iph->ip_src, &iph->ip_dst, ifp->name,
    3111             :                         if_flag_dump(ifp->flags));
    3112             :                 /* Fix multicast memberships? */
    3113           0 :                 if (iph->ip_dst.s_addr == htonl(OSPF_ALLSPFROUTERS))
    3114           0 :                         OI_MEMBER_JOINED(oi, MEMBER_ALLROUTERS);
    3115           0 :                 else if (iph->ip_dst.s_addr == htonl(OSPF_ALLDROUTERS))
    3116           0 :                         OI_MEMBER_JOINED(oi, MEMBER_DROUTERS);
    3117           0 :                 if (oi->multicast_memberships)
    3118           0 :                         ospf_if_set_multicast(oi);
    3119           0 :                 return OSPF_READ_CONTINUE;
    3120             :         }
    3121             : 
    3122             :         /*
    3123             :          * If the received packet is destined for AllDRouters, the
    3124             :          * packet should be accepted only if the received ospf
    3125             :          * interface state is either DR or Backup -- endo.
    3126             :          *
    3127             :          * I wonder who endo is?
    3128             :          */
    3129         203 :         if (iph->ip_dst.s_addr == htonl(OSPF_ALLDROUTERS)
    3130          17 :             && (oi->state != ISM_DR && oi->state != ISM_Backup)) {
    3131           0 :                 flog_warn(
    3132             :                         EC_OSPF_PACKET,
    3133             :                         "Dropping packet for AllDRouters from [%pI4] via [%s] (ISM: %s)",
    3134             :                         &iph->ip_src, IF_NAME(oi),
    3135             :                         lookup_msg(ospf_ism_state_msg, oi->state, NULL));
    3136             :                 /* Try to fix multicast membership. */
    3137           0 :                 SET_FLAG(oi->multicast_memberships, MEMBER_DROUTERS);
    3138           0 :                 ospf_if_set_multicast(oi);
    3139           0 :                 return OSPF_READ_CONTINUE;
    3140             :         }
    3141             : 
    3142             :         /* Verify more OSPF header fields. */
    3143         203 :         ret = ospf_verify_header(ibuf, oi, iph, ospfh);
    3144         203 :         if (ret < 0) {
    3145           0 :                 if (IS_DEBUG_OSPF_PACKET(0, RECV))
    3146           0 :                         zlog_debug(
    3147             :                                 "ospf_read[%pI4]: Header check failed, dropping.",
    3148             :                                 &iph->ip_src);
    3149           0 :                 return OSPF_READ_CONTINUE;
    3150             :         }
    3151             : 
    3152             :         /* Show debug receiving packet. */
    3153         203 :         if (IS_DEBUG_OSPF_PACKET(ospfh->type - 1, RECV)) {
    3154           0 :                 if (IS_DEBUG_OSPF_PACKET(ospfh->type - 1, DETAIL)) {
    3155           0 :                         zlog_debug(
    3156             :                                 "-----------------------------------------------------");
    3157           0 :                         ospf_packet_dump(ibuf);
    3158             :                 }
    3159             : 
    3160           0 :                 zlog_debug("%s received from [%pI4] via [%s]",
    3161             :                            lookup_msg(ospf_packet_type_str, ospfh->type, NULL),
    3162             :                            &ospfh->router_id, IF_NAME(oi));
    3163           0 :                 zlog_debug(" src [%pI4],", &iph->ip_src);
    3164           0 :                 zlog_debug(" dst [%pI4]", &iph->ip_dst);
    3165             : 
    3166           0 :                 if (IS_DEBUG_OSPF_PACKET(ospfh->type - 1, DETAIL))
    3167           0 :                         zlog_debug(
    3168             :                                 "-----------------------------------------------------");
    3169             :         }
    3170             : 
    3171         203 :         stream_forward_getp(ibuf, OSPF_HEADER_SIZE);
    3172             : 
    3173             :         /* Adjust size to message length. */
    3174         203 :         length = ntohs(ospfh->length) - OSPF_HEADER_SIZE;
    3175             : 
    3176             :         /* Read rest of the packet and call each sort of packet routine.
    3177             :          */
    3178         203 :         switch (ospfh->type) {
    3179          94 :         case OSPF_MSG_HELLO:
    3180          94 :                 ospf_hello(iph, ospfh, ibuf, oi, length);
    3181          94 :                 break;
    3182          20 :         case OSPF_MSG_DB_DESC:
    3183          20 :                 ospf_db_desc(iph, ospfh, ibuf, oi, length);
    3184          20 :                 break;
    3185           8 :         case OSPF_MSG_LS_REQ:
    3186           8 :                 ospf_ls_req(iph, ospfh, ibuf, oi, length);
    3187           8 :                 break;
    3188          53 :         case OSPF_MSG_LS_UPD:
    3189          53 :                 ospf_ls_upd(ospf, iph, ospfh, ibuf, oi, length);
    3190          53 :                 break;
    3191          28 :         case OSPF_MSG_LS_ACK:
    3192          28 :                 ospf_ls_ack(iph, ospfh, ibuf, oi, length);
    3193          28 :                 break;
    3194           0 :         default:
    3195           0 :                 flog_warn(
    3196             :                         EC_OSPF_PACKET,
    3197             :                         "interface %s(%s): OSPF packet header type %d is illegal",
    3198             :                         IF_NAME(oi), ospf_get_name(ospf), ospfh->type);
    3199           0 :                 break;
    3200             :         }
    3201             : 
    3202             :         return OSPF_READ_CONTINUE;
    3203             : }
    3204             : 
    3205             : /* Starting point of packet process function. */
    3206         161 : void ospf_read(struct thread *thread)
    3207             : {
    3208         161 :         struct ospf *ospf;
    3209         161 :         int32_t count = 0;
    3210         161 :         enum ospf_read_return_enum ret;
    3211             : 
    3212             :         /* first of all get interface pointer. */
    3213         161 :         ospf = THREAD_ARG(thread);
    3214             : 
    3215             :         /* prepare for next packet. */
    3216         161 :         thread_add_read(master, ospf_read, ospf, ospf->fd, &ospf->t_read);
    3217             : 
    3218         364 :         while (count < ospf->write_oi_count) {
    3219         364 :                 count++;
    3220         364 :                 ret = ospf_read_helper(ospf);
    3221         364 :                 switch (ret) {
    3222             :                 case OSPF_READ_ERROR:
    3223             :                         return;
    3224             :                 case OSPF_READ_CONTINUE:
    3225             :                         break;
    3226             :                 }
    3227             :         }
    3228             : }
    3229             : 
    3230             : /* Make OSPF header. */
    3231         242 : static void ospf_make_header(int type, struct ospf_interface *oi,
    3232             :                              struct stream *s)
    3233             : {
    3234         242 :         struct ospf_header *ospfh;
    3235             : 
    3236         242 :         ospfh = (struct ospf_header *)STREAM_DATA(s);
    3237             : 
    3238         242 :         ospfh->version = (uint8_t)OSPF_VERSION;
    3239         242 :         ospfh->type = (uint8_t)type;
    3240             : 
    3241         242 :         ospfh->router_id = oi->ospf->router_id;
    3242             : 
    3243         242 :         ospfh->checksum = 0;
    3244         242 :         ospfh->area_id = oi->area->area_id;
    3245         242 :         ospfh->auth_type = htons(ospf_auth_type(oi));
    3246             : 
    3247         242 :         memset(ospfh->u.auth_data, 0, OSPF_AUTH_SIMPLE_SIZE);
    3248             : 
    3249         242 :         stream_forward_endp(s, OSPF_HEADER_SIZE);
    3250         242 : }
    3251             : 
    3252             : /* Make Authentication Data. */
    3253         238 : static int ospf_make_auth(struct ospf_interface *oi, struct ospf_header *ospfh)
    3254             : {
    3255         238 :         struct crypt_key *ck;
    3256             : 
    3257         238 :         switch (ospf_auth_type(oi)) {
    3258             :         case OSPF_AUTH_NULL:
    3259             :                 /* memset (ospfh->u.auth_data, 0, sizeof(ospfh->u.auth_data));
    3260             :                  */
    3261             :                 break;
    3262           0 :         case OSPF_AUTH_SIMPLE:
    3263           0 :                 memcpy(ospfh->u.auth_data, OSPF_IF_PARAM(oi, auth_simple),
    3264             :                        OSPF_AUTH_SIMPLE_SIZE);
    3265           0 :                 break;
    3266           0 :         case OSPF_AUTH_CRYPTOGRAPHIC:
    3267             :                 /* If key is not set, then set 0. */
    3268           0 :                 if (list_isempty(OSPF_IF_PARAM(oi, auth_crypt))) {
    3269           0 :                         ospfh->u.crypt.zero = 0;
    3270           0 :                         ospfh->u.crypt.key_id = 0;
    3271           0 :                         ospfh->u.crypt.auth_data_len = OSPF_AUTH_MD5_SIZE;
    3272             :                 } else {
    3273           0 :                         ck = listgetdata(
    3274             :                                 listtail(OSPF_IF_PARAM(oi, auth_crypt)));
    3275           0 :                         ospfh->u.crypt.zero = 0;
    3276           0 :                         ospfh->u.crypt.key_id = ck->key_id;
    3277           0 :                         ospfh->u.crypt.auth_data_len = OSPF_AUTH_MD5_SIZE;
    3278             :                 }
    3279             :                 /* note: the seq is done in ospf_make_md5_digest() */
    3280             :                 break;
    3281             :         default:
    3282             :                 /* memset (ospfh->u.auth_data, 0, sizeof(ospfh->u.auth_data));
    3283             :                  */
    3284             :                 break;
    3285             :         }
    3286             : 
    3287         238 :         return 0;
    3288             : }
    3289             : 
    3290             : /* Fill rest of OSPF header. */
    3291         238 : static void ospf_fill_header(struct ospf_interface *oi, struct stream *s,
    3292             :                              uint16_t length)
    3293             : {
    3294         238 :         struct ospf_header *ospfh;
    3295             : 
    3296         238 :         ospfh = (struct ospf_header *)STREAM_DATA(s);
    3297             : 
    3298             :         /* Fill length. */
    3299         238 :         ospfh->length = htons(length);
    3300             : 
    3301             :         /* Calculate checksum. */
    3302         238 :         if (ntohs(ospfh->auth_type) != OSPF_AUTH_CRYPTOGRAPHIC)
    3303         238 :                 ospfh->checksum = in_cksum(ospfh, length);
    3304             :         else
    3305           0 :                 ospfh->checksum = 0;
    3306             : 
    3307             :         /* Add Authentication Data. */
    3308         238 :         ospf_make_auth(oi, ospfh);
    3309         238 : }
    3310             : 
    3311         139 : static int ospf_make_hello(struct ospf_interface *oi, struct stream *s)
    3312             : {
    3313         139 :         struct ospf_neighbor *nbr;
    3314         139 :         struct route_node *rn;
    3315         139 :         uint16_t length = OSPF_HELLO_MIN_SIZE;
    3316         139 :         struct in_addr mask;
    3317         139 :         unsigned long p;
    3318         139 :         int flag = 0;
    3319             : 
    3320             :         /* Set netmask of interface. */
    3321         139 :         if (!(CHECK_FLAG(oi->connected->flags, ZEBRA_IFA_UNNUMBERED)
    3322           0 :               && oi->type == OSPF_IFTYPE_POINTOPOINT)
    3323         139 :             && oi->type != OSPF_IFTYPE_VIRTUALLINK)
    3324         139 :                 masklen2ip(oi->address->prefixlen, &mask);
    3325             :         else
    3326           0 :                 memset((char *)&mask, 0, sizeof(struct in_addr));
    3327         139 :         stream_put_ipv4(s, mask.s_addr);
    3328             : 
    3329             :         /* Set Hello Interval. */
    3330         139 :         if (OSPF_IF_PARAM(oi, fast_hello) == 0)
    3331         139 :                 stream_putw(s, OSPF_IF_PARAM(oi, v_hello));
    3332             :         else
    3333           0 :                 stream_putw(s, 0); /* hello-interval of 0 for fast-hellos */
    3334             : 
    3335         139 :         if (IS_DEBUG_OSPF_EVENT)
    3336         139 :                 zlog_debug("%s: options: %x, int: %s", __func__, OPTIONS(oi),
    3337             :                            IF_NAME(oi));
    3338             : 
    3339             :         /* Set Options. */
    3340         139 :         stream_putc(s, OPTIONS(oi));
    3341             : 
    3342             :         /* Set Router Priority. */
    3343         139 :         stream_putc(s, PRIORITY(oi));
    3344             : 
    3345             :         /* Set Router Dead Interval. */
    3346         139 :         stream_putl(s, OSPF_IF_PARAM(oi, v_wait));
    3347             : 
    3348             :         /* Set Designated Router. */
    3349         139 :         stream_put_ipv4(s, DR(oi).s_addr);
    3350             : 
    3351         139 :         p = stream_get_endp(s);
    3352             : 
    3353             :         /* Set Backup Designated Router. */
    3354         139 :         stream_put_ipv4(s, BDR(oi).s_addr);
    3355             : 
    3356             :         /* Add neighbor seen. */
    3357         466 :         for (rn = route_top(oi->nbrs); rn; rn = route_next(rn)) {
    3358         327 :                 nbr = rn->info;
    3359             : 
    3360         327 :                 if (!nbr)
    3361          94 :                         continue;
    3362             : 
    3363             :                 /* Ignore the 0.0.0.0 node */
    3364         233 :                 if (nbr->router_id.s_addr == INADDR_ANY)
    3365           0 :                         continue;
    3366             : 
    3367             :                 /* Ignore Down neighbor */
    3368         233 :                 if (nbr->state == NSM_Attempt)
    3369           0 :                         continue;
    3370             : 
    3371             :                 /* This is myself for DR election */
    3372         233 :                 if (nbr->state == NSM_Down)
    3373           0 :                         continue;
    3374             : 
    3375         233 :                 if (IPV4_ADDR_SAME(&nbr->router_id, &oi->ospf->router_id))
    3376         139 :                         continue;
    3377             :                 /* Check neighbor is  sane? */
    3378          94 :                 if (nbr->d_router.s_addr != INADDR_ANY &&
    3379          84 :                     IPV4_ADDR_SAME(&nbr->d_router, &oi->address->u.prefix4) &&
    3380          32 :                     IPV4_ADDR_SAME(&nbr->bd_router, &oi->address->u.prefix4))
    3381          94 :                         flag = 1;
    3382             : 
    3383             :                 /* Hello packet overflows interface MTU.
    3384             :                  */
    3385          94 :                 if (length + sizeof(uint32_t) > ospf_packet_max(oi)) {
    3386           0 :                         flog_err(
    3387             :                                 EC_OSPF_LARGE_HELLO,
    3388             :                                 "Oversized Hello packet! Larger than MTU. Not sending it out");
    3389           0 :                         return 0;
    3390             :                 }
    3391             : 
    3392          94 :                 stream_put_ipv4(s, nbr->router_id.s_addr);
    3393          94 :                 length += 4;
    3394             :         }
    3395             : 
    3396             :         /* Let neighbor generate BackupSeen. */
    3397         139 :         if (flag == 1)
    3398           3 :                 stream_putl_at(s, p, 0); /* ipv4 address, normally */
    3399             : 
    3400         139 :         return length;
    3401             : }
    3402             : 
    3403          20 : static int ospf_make_db_desc(struct ospf_interface *oi,
    3404             :                              struct ospf_neighbor *nbr, struct stream *s)
    3405             : {
    3406          20 :         struct ospf_lsa *lsa;
    3407          20 :         uint16_t length = OSPF_DB_DESC_MIN_SIZE;
    3408          20 :         uint8_t options;
    3409          20 :         unsigned long pp;
    3410          20 :         int i;
    3411          20 :         struct ospf_lsdb *lsdb;
    3412             : 
    3413             :         /* Set Interface MTU. */
    3414          20 :         if (oi->type == OSPF_IFTYPE_VIRTUALLINK)
    3415           0 :                 stream_putw(s, 0);
    3416             :         else
    3417          20 :                 stream_putw(s, oi->ifp->mtu);
    3418             : 
    3419             :         /* Set Options. */
    3420          20 :         options = OPTIONS(oi);
    3421          20 :         if (CHECK_FLAG(oi->ospf->config, OSPF_OPAQUE_CAPABLE))
    3422           0 :                 SET_FLAG(options, OSPF_OPTION_O);
    3423          20 :         stream_putc(s, options);
    3424             : 
    3425             :         /* DD flags */
    3426          20 :         pp = stream_get_endp(s);
    3427          20 :         stream_putc(s, nbr->dd_flags);
    3428             : 
    3429             :         /* Set DD Sequence Number. */
    3430          20 :         stream_putl(s, nbr->dd_seqnum);
    3431             : 
    3432             :         /* shortcut unneeded walk of (empty) summary LSDBs */
    3433          20 :         if (ospf_db_summary_isempty(nbr))
    3434          12 :                 goto empty;
    3435             : 
    3436             :         /* Describe LSA Header from Database Summary List. */
    3437           8 :         lsdb = &nbr->db_sum;
    3438             : 
    3439          96 :         for (i = OSPF_MIN_LSA; i < OSPF_MAX_LSA; i++) {
    3440          88 :                 struct route_table *table = lsdb->type[i].db;
    3441          88 :                 struct route_node *rn;
    3442             : 
    3443         113 :                 for (rn = route_top(table); rn; rn = route_next(rn))
    3444          25 :                         if ((lsa = rn->info) != NULL) {
    3445          22 :                                 if (IS_OPAQUE_LSA(lsa->data->type)
    3446           0 :                                     && (!CHECK_FLAG(options, OSPF_OPTION_O))) {
    3447             :                                         /* Suppress advertising
    3448             :                                          * opaque-information. */
    3449             :                                         /* Remove LSA from DB summary list. */
    3450           0 :                                         ospf_lsdb_delete(lsdb, lsa);
    3451           0 :                                         continue;
    3452             :                                 }
    3453             : 
    3454          22 :                                 if (!CHECK_FLAG(lsa->flags, OSPF_LSA_DISCARD)) {
    3455          22 :                                         struct lsa_header *lsah;
    3456          22 :                                         uint16_t ls_age;
    3457             : 
    3458             :                                         /* DD packet overflows interface MTU. */
    3459          22 :                                         if (length + OSPF_LSA_HEADER_SIZE
    3460          22 :                                             > ospf_packet_max(oi))
    3461             :                                                 break;
    3462             : 
    3463             :                                         /* Keep pointer to LS age. */
    3464          44 :                                         lsah = (struct lsa_header
    3465          22 :                                                         *)(STREAM_DATA(s)
    3466          22 :                                                            + stream_get_endp(
    3467             :                                                                      s));
    3468             : 
    3469             :                                         /* Proceed stream pointer. */
    3470          22 :                                         stream_put(s, lsa->data,
    3471             :                                                    OSPF_LSA_HEADER_SIZE);
    3472          22 :                                         length += OSPF_LSA_HEADER_SIZE;
    3473             : 
    3474             :                                         /* Set LS age. */
    3475          22 :                                         ls_age = LS_AGE(lsa);
    3476          22 :                                         lsah->ls_age = htons(ls_age);
    3477             :                                 }
    3478             : 
    3479             :                                 /* Remove LSA from DB summary list. */
    3480          22 :                                 ospf_lsdb_delete(lsdb, lsa);
    3481             :                         }
    3482             :         }
    3483             : 
    3484             :         /* Update 'More' bit */
    3485           8 :         if (ospf_db_summary_isempty(nbr)) {
    3486           8 :         empty:
    3487          20 :                 if (nbr->state >= NSM_Exchange) {
    3488          12 :                         UNSET_FLAG(nbr->dd_flags, OSPF_DD_FLAG_M);
    3489             :                         /* Rewrite DD flags */
    3490          12 :                         stream_putc_at(s, pp, nbr->dd_flags);
    3491             :                 } else {
    3492           8 :                         assert(IS_SET_DD_M(nbr->dd_flags));
    3493             :                 }
    3494             :         }
    3495          20 :         return length;
    3496             : }
    3497             : 
    3498          21 : static int ospf_make_ls_req_func(struct stream *s, uint16_t *length,
    3499             :                                  unsigned long delta, struct ospf_neighbor *nbr,
    3500             :                                  struct ospf_lsa *lsa)
    3501             : {
    3502          21 :         struct ospf_interface *oi;
    3503             : 
    3504          21 :         oi = nbr->oi;
    3505             : 
    3506             :         /* LS Request packet overflows interface MTU
    3507             :          * delta is just number of bytes required for 1 LS Req
    3508             :          * ospf_packet_max will return the number of bytes can
    3509             :          * be accommodated without ospf header. So length+delta
    3510             :          * can be compared to ospf_packet_max
    3511             :          * to check if it can fit another lsreq in the same packet.
    3512             :          */
    3513             : 
    3514          21 :         if (*length + delta > ospf_packet_max(oi))
    3515             :                 return 0;
    3516             : 
    3517          21 :         stream_putl(s, lsa->data->type);
    3518          21 :         stream_put_ipv4(s, lsa->data->id.s_addr);
    3519          21 :         stream_put_ipv4(s, lsa->data->adv_router.s_addr);
    3520             : 
    3521          21 :         ospf_lsa_unlock(&nbr->ls_req_last);
    3522          21 :         nbr->ls_req_last = ospf_lsa_lock(lsa);
    3523             : 
    3524          21 :         *length += 12;
    3525          21 :         return 1;
    3526             : }
    3527             : 
    3528          12 : static int ospf_make_ls_req(struct ospf_neighbor *nbr, struct stream *s)
    3529             : {
    3530          12 :         struct ospf_lsa *lsa;
    3531          12 :         uint16_t length = OSPF_LS_REQ_MIN_SIZE;
    3532          12 :         unsigned long delta = 12;
    3533          12 :         struct route_table *table;
    3534          12 :         struct route_node *rn;
    3535          12 :         int i;
    3536          12 :         struct ospf_lsdb *lsdb;
    3537             : 
    3538          12 :         lsdb = &nbr->ls_req;
    3539             : 
    3540         144 :         for (i = OSPF_MIN_LSA; i < OSPF_MAX_LSA; i++) {
    3541         132 :                 table = lsdb->type[i].db;
    3542         156 :                 for (rn = route_top(table); rn; rn = route_next(rn))
    3543          24 :                         if ((lsa = (rn->info)) != NULL)
    3544          21 :                                 if (ospf_make_ls_req_func(s, &length, delta,
    3545             :                                                           nbr, lsa)
    3546             :                                     == 0) {
    3547           0 :                                         route_unlock_node(rn);
    3548           0 :                                         break;
    3549             :                                 }
    3550             :         }
    3551          12 :         return length;
    3552             : }
    3553             : 
    3554          89 : static int ls_age_increment(struct ospf_lsa *lsa, int delay)
    3555             : {
    3556          89 :         int age;
    3557             : 
    3558          89 :         age = IS_LSA_MAXAGE(lsa) ? OSPF_LSA_MAXAGE : LS_AGE(lsa) + delay;
    3559             : 
    3560          89 :         return (age > OSPF_LSA_MAXAGE ? OSPF_LSA_MAXAGE : age);
    3561             : }
    3562             : 
    3563          48 : static int ospf_make_ls_upd(struct ospf_interface *oi, struct list *update,
    3564             :                             struct stream *s)
    3565             : {
    3566          48 :         struct ospf_lsa *lsa;
    3567          48 :         struct listnode *node;
    3568          48 :         uint16_t length = 0;
    3569          48 :         unsigned int size_noauth;
    3570          48 :         unsigned long delta = stream_get_endp(s);
    3571          48 :         unsigned long pp;
    3572          48 :         int count = 0;
    3573             : 
    3574          48 :         if (IS_DEBUG_OSPF_EVENT)
    3575          48 :                 zlog_debug("%s: Start", __func__);
    3576             : 
    3577          48 :         pp = stream_get_endp(s);
    3578          48 :         stream_forward_endp(s, OSPF_LS_UPD_MIN_SIZE);
    3579          48 :         length += OSPF_LS_UPD_MIN_SIZE;
    3580             : 
    3581             :         /* Calculate amount of packet usable for data. */
    3582          48 :         size_noauth = stream_get_size(s) - ospf_packet_authspace(oi);
    3583             : 
    3584         137 :         while ((node = listhead(update)) != NULL) {
    3585          89 :                 struct lsa_header *lsah;
    3586          89 :                 uint16_t ls_age;
    3587             : 
    3588          89 :                 lsa = listgetdata(node);
    3589          89 :                 assert(lsa->data);
    3590             : 
    3591          89 :                 if (IS_DEBUG_OSPF_EVENT)
    3592          89 :                         zlog_debug("%s: List Iteration %d LSA[%s]", __func__,
    3593             :                                    count, dump_lsa_key(lsa));
    3594             : 
    3595             :                 /* Will it fit? Minimum it has to fit at least one */
    3596          89 :                 if ((length + delta + ntohs(lsa->data->length) > size_noauth) &&
    3597             :                                 (count > 0))
    3598             :                         break;
    3599             : 
    3600             :                 /* Keep pointer to LS age. */
    3601         178 :                 lsah = (struct lsa_header *)(STREAM_DATA(s)
    3602          89 :                                              + stream_get_endp(s));
    3603             : 
    3604             :                 /* Put LSA to Link State Request. */
    3605          89 :                 stream_put(s, lsa->data, ntohs(lsa->data->length));
    3606             : 
    3607             :                 /* Set LS age. */
    3608             :                 /* each hop must increment an lsa_age by transmit_delay
    3609             :                    of OSPF interface */
    3610          89 :                 ls_age = ls_age_increment(lsa,
    3611          89 :                                           OSPF_IF_PARAM(oi, transmit_delay));
    3612          89 :                 lsah->ls_age = htons(ls_age);
    3613             : 
    3614          89 :                 length += ntohs(lsa->data->length);
    3615          89 :                 count++;
    3616             : 
    3617          89 :                 list_delete_node(update, node);
    3618          89 :                 ospf_lsa_unlock(&lsa); /* oi->ls_upd_queue */
    3619             :         }
    3620             : 
    3621             :         /* Now set #LSAs. */
    3622          48 :         stream_putl_at(s, pp, count);
    3623             : 
    3624          48 :         if (IS_DEBUG_OSPF_EVENT)
    3625          48 :                 zlog_debug("%s: Stop", __func__);
    3626          48 :         return length;
    3627             : }
    3628             : 
    3629          23 : static int ospf_make_ls_ack(struct ospf_interface *oi, struct list *ack,
    3630             :                             struct stream *s)
    3631             : {
    3632          23 :         struct listnode *node, *nnode;
    3633          23 :         uint16_t length = OSPF_LS_ACK_MIN_SIZE;
    3634          23 :         unsigned long delta = OSPF_LSA_HEADER_SIZE;
    3635          23 :         struct ospf_lsa *lsa;
    3636             : 
    3637          93 :         for (ALL_LIST_ELEMENTS(ack, node, nnode, lsa)) {
    3638          70 :                 assert(lsa);
    3639             : 
    3640             :                 /* LS Ack packet overflows interface MTU
    3641             :                  * delta is just number of bytes required for
    3642             :                  * 1 LS Ack(1 LS Hdr) ospf_packet_max will return
    3643             :                  * the number of bytes can be accommodated without
    3644             :                  * ospf header. So length+delta can be compared
    3645             :                  * against ospf_packet_max to check if it can fit
    3646             :                  * another ls header in the same packet.
    3647             :                  */
    3648          70 :                 if ((length + delta) > ospf_packet_max(oi))
    3649             :                         break;
    3650             : 
    3651          70 :                 stream_put(s, lsa->data, OSPF_LSA_HEADER_SIZE);
    3652          70 :                 length += OSPF_LSA_HEADER_SIZE;
    3653             : 
    3654          70 :                 listnode_delete(ack, lsa);
    3655          70 :                 ospf_lsa_unlock(&lsa); /* oi->ls_ack_direct.ls_ack */
    3656             :         }
    3657             : 
    3658          23 :         return length;
    3659             : }
    3660             : 
    3661         139 : static void ospf_hello_send_sub(struct ospf_interface *oi, in_addr_t addr)
    3662             : {
    3663         139 :         struct ospf_packet *op;
    3664         139 :         uint16_t length = OSPF_HEADER_SIZE;
    3665             : 
    3666         139 :         op = ospf_packet_new(oi->ifp->mtu);
    3667             : 
    3668             :         /* Prepare OSPF common header. */
    3669         139 :         ospf_make_header(OSPF_MSG_HELLO, oi, op->s);
    3670             : 
    3671             :         /* Prepare OSPF Hello body. */
    3672         139 :         length += ospf_make_hello(oi, op->s);
    3673         139 :         if (length == OSPF_HEADER_SIZE) {
    3674             :                 /* Hello overshooting MTU */
    3675           0 :                 ospf_packet_free(op);
    3676           0 :                 return;
    3677             :         }
    3678             : 
    3679             :         /* Fill OSPF header. */
    3680         139 :         ospf_fill_header(oi, op->s, length);
    3681             : 
    3682             :         /* Set packet length. */
    3683         139 :         op->length = length;
    3684             : 
    3685         139 :         op->dst.s_addr = addr;
    3686             : 
    3687         139 :         if (IS_DEBUG_OSPF_EVENT) {
    3688         139 :                 if (oi->ospf->vrf_id)
    3689           0 :                         zlog_debug(
    3690             :                                 "%s: Hello Tx interface %s ospf vrf %s id %u",
    3691             :                                 __func__, oi->ifp->name,
    3692             :                                 ospf_vrf_id_to_name(oi->ospf->vrf_id),
    3693             :                                 oi->ospf->vrf_id);
    3694             :         }
    3695             :         /* Add packet to the top of the interface output queue, so that they
    3696             :          * can't get delayed by things like long queues of LS Update packets
    3697             :          */
    3698         139 :         ospf_packet_add_top(oi, op);
    3699             : 
    3700             :         /* Hook thread to write packet. */
    3701         139 :         OSPF_ISM_WRITE_ON(oi->ospf);
    3702             : }
    3703             : 
    3704           0 : static void ospf_poll_send(struct ospf_nbr_nbma *nbr_nbma)
    3705             : {
    3706           0 :         struct ospf_interface *oi;
    3707             : 
    3708           0 :         oi = nbr_nbma->oi;
    3709           0 :         assert(oi);
    3710             : 
    3711             :         /* If this is passive interface, do not send OSPF Hello. */
    3712           0 :         if (OSPF_IF_PASSIVE_STATUS(oi) == OSPF_IF_PASSIVE)
    3713             :                 return;
    3714             : 
    3715           0 :         if (oi->type != OSPF_IFTYPE_NBMA)
    3716             :                 return;
    3717             : 
    3718           0 :         if (nbr_nbma->nbr != NULL && nbr_nbma->nbr->state != NSM_Down)
    3719             :                 return;
    3720             : 
    3721           0 :         if (PRIORITY(oi) == 0)
    3722             :                 return;
    3723             : 
    3724           0 :         if (nbr_nbma->priority == 0 && oi->state != ISM_DR
    3725           0 :             && oi->state != ISM_Backup)
    3726             :                 return;
    3727             : 
    3728           0 :         ospf_hello_send_sub(oi, nbr_nbma->addr.s_addr);
    3729             : }
    3730             : 
    3731           0 : void ospf_poll_timer(struct thread *thread)
    3732             : {
    3733           0 :         struct ospf_nbr_nbma *nbr_nbma;
    3734             : 
    3735           0 :         nbr_nbma = THREAD_ARG(thread);
    3736           0 :         nbr_nbma->t_poll = NULL;
    3737             : 
    3738           0 :         if (IS_DEBUG_OSPF(nsm, NSM_TIMERS))
    3739           0 :                 zlog_debug("NSM[%s:%pI4]: Timer (Poll timer expire)",
    3740             :                            IF_NAME(nbr_nbma->oi), &nbr_nbma->addr);
    3741             : 
    3742           0 :         ospf_poll_send(nbr_nbma);
    3743             : 
    3744           0 :         if (nbr_nbma->v_poll > 0)
    3745           0 :                 OSPF_POLL_TIMER_ON(nbr_nbma->t_poll, ospf_poll_timer,
    3746             :                                    nbr_nbma->v_poll);
    3747           0 : }
    3748             : 
    3749             : 
    3750           0 : void ospf_hello_reply_timer(struct thread *thread)
    3751             : {
    3752           0 :         struct ospf_neighbor *nbr;
    3753             : 
    3754           0 :         nbr = THREAD_ARG(thread);
    3755           0 :         nbr->t_hello_reply = NULL;
    3756             : 
    3757           0 :         if (IS_DEBUG_OSPF(nsm, NSM_TIMERS))
    3758           0 :                 zlog_debug("NSM[%s:%pI4]: Timer (hello-reply timer expire)",
    3759             :                            IF_NAME(nbr->oi), &nbr->router_id);
    3760             : 
    3761           0 :         ospf_hello_send_sub(nbr->oi, nbr->address.u.prefix4.s_addr);
    3762           0 : }
    3763             : 
    3764             : /* Send OSPF Hello. */
    3765         139 : void ospf_hello_send(struct ospf_interface *oi)
    3766             : {
    3767             :         /* If this is passive interface, do not send OSPF Hello. */
    3768         139 :         if (OSPF_IF_PASSIVE_STATUS(oi) == OSPF_IF_PASSIVE)
    3769             :                 return;
    3770             : 
    3771         139 :         if (oi->type == OSPF_IFTYPE_NBMA) {
    3772           0 :                 struct ospf_neighbor *nbr;
    3773           0 :                 struct route_node *rn;
    3774             : 
    3775           0 :                 for (rn = route_top(oi->nbrs); rn; rn = route_next(rn)) {
    3776           0 :                         nbr = rn->info;
    3777           0 :                         if (!nbr)
    3778           0 :                                 continue;
    3779             : 
    3780           0 :                         if (nbr == oi->nbr_self)
    3781           0 :                                 continue;
    3782             : 
    3783           0 :                         if (nbr->state == NSM_Down)
    3784           0 :                                 continue;
    3785             : 
    3786             :                         /*
    3787             :                          * RFC 2328  Section 9.5.1
    3788             :                          * If the router is not eligible to become Designated
    3789             :                          * Router, it must periodically send Hello Packets to
    3790             :                          * both the Designated Router and the Backup
    3791             :                          * Designated Router (if they exist).
    3792             :                          */
    3793           0 :                         if (PRIORITY(oi) == 0 &&
    3794           0 :                             IPV4_ADDR_CMP(&DR(oi), &nbr->address.u.prefix4) &&
    3795           0 :                             IPV4_ADDR_CMP(&BDR(oi), &nbr->address.u.prefix4))
    3796           0 :                                 continue;
    3797             : 
    3798             :                         /*
    3799             :                          * If the router is eligible to become Designated
    3800             :                          * Router, it must periodically send Hello Packets to
    3801             :                          * all neighbors that are also eligible. In addition,
    3802             :                          * if the router is itself the Designated Router or
    3803             :                          * Backup Designated Router, it must also send periodic
    3804             :                          * Hello Packets to all other neighbors.
    3805             :                          */
    3806           0 :                         if (nbr->priority == 0 && oi->state == ISM_DROther)
    3807           0 :                                 continue;
    3808             : 
    3809             :                         /* if oi->state == Waiting, send
    3810             :                          * hello to all neighbors */
    3811           0 :                         ospf_hello_send_sub(oi, nbr->address.u.prefix4.s_addr);
    3812             :                 }
    3813             :         } else {
    3814             :                 /* Decide destination address. */
    3815         139 :                 if (oi->type == OSPF_IFTYPE_VIRTUALLINK)
    3816           0 :                         ospf_hello_send_sub(oi, oi->vl_data->peer_addr.s_addr);
    3817             :                 else
    3818         139 :                         ospf_hello_send_sub(oi, htonl(OSPF_ALLSPFROUTERS));
    3819             :         }
    3820             : }
    3821             : 
    3822             : /* Send OSPF Database Description. */
    3823          20 : void ospf_db_desc_send(struct ospf_neighbor *nbr)
    3824             : {
    3825          20 :         struct ospf_interface *oi;
    3826          20 :         struct ospf_packet *op;
    3827          20 :         uint16_t length = OSPF_HEADER_SIZE;
    3828             : 
    3829          20 :         oi = nbr->oi;
    3830          20 :         op = ospf_packet_new(oi->ifp->mtu);
    3831             : 
    3832             :         /* Prepare OSPF common header. */
    3833          20 :         ospf_make_header(OSPF_MSG_DB_DESC, oi, op->s);
    3834             : 
    3835             :         /* Prepare OSPF Database Description body. */
    3836          20 :         length += ospf_make_db_desc(oi, nbr, op->s);
    3837             : 
    3838             :         /* Fill OSPF header. */
    3839          20 :         ospf_fill_header(oi, op->s, length);
    3840             : 
    3841             :         /* Set packet length. */
    3842          20 :         op->length = length;
    3843             : 
    3844             :         /* Decide destination address. */
    3845          20 :         if (oi->type == OSPF_IFTYPE_POINTOPOINT)
    3846           0 :                 op->dst.s_addr = htonl(OSPF_ALLSPFROUTERS);
    3847             :         else
    3848          20 :                 op->dst = nbr->address.u.prefix4;
    3849             : 
    3850             :         /* Add packet to the interface output queue. */
    3851          20 :         ospf_packet_add(oi, op);
    3852             : 
    3853             :         /* Hook thread to write packet. */
    3854          20 :         OSPF_ISM_WRITE_ON(oi->ospf);
    3855             : 
    3856             :         /* Remove old DD packet, then copy new one and keep in neighbor
    3857             :          * structure. */
    3858          20 :         if (nbr->last_send)
    3859          12 :                 ospf_packet_free(nbr->last_send);
    3860          20 :         nbr->last_send = ospf_packet_dup(op);
    3861          20 :         monotime(&nbr->last_send_ts);
    3862          20 :         if (CHECK_FLAG(oi->ospf->config, OSPF_LOG_ADJACENCY_DETAIL))
    3863           0 :                 zlog_info(
    3864             :                         "%s:Packet[DD]: %pI4 DB Desc send with seqnum:%x , flags:%x",
    3865             :                         ospf_get_name(oi->ospf), &nbr->router_id,
    3866             :                         nbr->dd_seqnum, nbr->dd_flags);
    3867          20 : }
    3868             : 
    3869             : /* Re-send Database Description. */
    3870           0 : void ospf_db_desc_resend(struct ospf_neighbor *nbr)
    3871             : {
    3872           0 :         struct ospf_interface *oi;
    3873             : 
    3874           0 :         oi = nbr->oi;
    3875             : 
    3876             :         /* Add packet to the interface output queue. */
    3877           0 :         ospf_packet_add(oi, ospf_packet_dup(nbr->last_send));
    3878             : 
    3879             :         /* Hook thread to write packet. */
    3880           0 :         OSPF_ISM_WRITE_ON(oi->ospf);
    3881           0 :         if (CHECK_FLAG(oi->ospf->config, OSPF_LOG_ADJACENCY_DETAIL))
    3882           0 :                 zlog_info(
    3883             :                         "%s:Packet[DD]: %pI4 DB Desc resend with seqnum:%x , flags:%x",
    3884             :                         ospf_get_name(oi->ospf), &nbr->router_id,
    3885             :                         nbr->dd_seqnum, nbr->dd_flags);
    3886           0 : }
    3887             : 
    3888             : /* Send Link State Request. */
    3889          12 : void ospf_ls_req_send(struct ospf_neighbor *nbr)
    3890             : {
    3891          12 :         struct ospf_interface *oi;
    3892          12 :         struct ospf_packet *op;
    3893          12 :         uint16_t length = OSPF_HEADER_SIZE;
    3894             : 
    3895          12 :         oi = nbr->oi;
    3896          12 :         op = ospf_packet_new(oi->ifp->mtu);
    3897             : 
    3898             :         /* Prepare OSPF common header. */
    3899          12 :         ospf_make_header(OSPF_MSG_LS_REQ, oi, op->s);
    3900             : 
    3901             :         /* Prepare OSPF Link State Request body. */
    3902          12 :         length += ospf_make_ls_req(nbr, op->s);
    3903          12 :         if (length == OSPF_HEADER_SIZE) {
    3904           4 :                 ospf_packet_free(op);
    3905           4 :                 return;
    3906             :         }
    3907             : 
    3908             :         /* Fill OSPF header. */
    3909           8 :         ospf_fill_header(oi, op->s, length);
    3910             : 
    3911             :         /* Set packet length. */
    3912           8 :         op->length = length;
    3913             : 
    3914             :         /* Decide destination address. */
    3915           8 :         if (oi->type == OSPF_IFTYPE_POINTOPOINT)
    3916           0 :                 op->dst.s_addr = htonl(OSPF_ALLSPFROUTERS);
    3917             :         else
    3918           8 :                 op->dst = nbr->address.u.prefix4;
    3919             : 
    3920             :         /* Add packet to the interface output queue. */
    3921           8 :         ospf_packet_add(oi, op);
    3922             : 
    3923             :         /* Hook thread to write packet. */
    3924           8 :         OSPF_ISM_WRITE_ON(oi->ospf);
    3925             : 
    3926             :         /* Add Link State Request Retransmission Timer. */
    3927           8 :         OSPF_NSM_TIMER_ON(nbr->t_ls_req, ospf_ls_req_timer, nbr->v_ls_req);
    3928             : }
    3929             : 
    3930             : /* Send Link State Update with an LSA. */
    3931          64 : void ospf_ls_upd_send_lsa(struct ospf_neighbor *nbr, struct ospf_lsa *lsa,
    3932             :                           int flag)
    3933             : {
    3934          64 :         struct list *update;
    3935             : 
    3936          64 :         update = list_new();
    3937             : 
    3938          64 :         listnode_add(update, lsa);
    3939             : 
    3940             :         /*ospf instance is going down, send self originated
    3941             :          * MAXAGE LSA update to neighbors to remove from LSDB */
    3942          64 :         if (nbr->oi->ospf->inst_shutdown && IS_LSA_MAXAGE(lsa))
    3943          16 :                 ospf_ls_upd_send(nbr, update, flag, 1);
    3944             :         else
    3945          48 :                 ospf_ls_upd_send(nbr, update, flag, 0);
    3946             : 
    3947          64 :         list_delete(&update);
    3948          64 : }
    3949             : 
    3950             : /* Determine size for packet. Must be at least big enough to accommodate next
    3951             :  * LSA on list, which may be bigger than MTU size.
    3952             :  *
    3953             :  * Return pointer to new ospf_packet
    3954             :  * NULL if we can not allocate, eg because LSA is bigger than imposed limit
    3955             :  * on packet sizes (in which case offending LSA is deleted from update list)
    3956             :  */
    3957          48 : static struct ospf_packet *ospf_ls_upd_packet_new(struct list *update,
    3958             :                                                   struct ospf_interface *oi)
    3959             : {
    3960          48 :         struct ospf_lsa *lsa;
    3961          48 :         struct listnode *ln;
    3962          48 :         size_t size;
    3963          48 :         static char warned = 0;
    3964             : 
    3965          48 :         lsa = listgetdata((ln = listhead(update)));
    3966          48 :         assert(lsa->data);
    3967             : 
    3968          48 :         if ((OSPF_LS_UPD_MIN_SIZE + ntohs(lsa->data->length))
    3969          48 :             > ospf_packet_max(oi)) {
    3970           0 :                 if (!warned) {
    3971           0 :                         flog_warn(
    3972             :                                 EC_OSPF_LARGE_LSA,
    3973             :                                 "%s: oversized LSA encountered!will need to fragment. Not optimal. Try divide up your network with areas. Use 'debug ospf packet send' to see details, or look at 'show ip ospf database ..'",
    3974             :                                 __func__);
    3975           0 :                         warned = 1;
    3976             :                 }
    3977             : 
    3978           0 :                 if (IS_DEBUG_OSPF_PACKET(0, SEND))
    3979           0 :                         zlog_debug(
    3980             :                                 "%s: oversized LSA id:%pI4, %d bytes originated by %pI4, will be fragmented!",
    3981             :                                 __func__, &lsa->data->id,
    3982             :                                 ntohs(lsa->data->length),
    3983             :                                 &lsa->data->adv_router);
    3984             : 
    3985             :                 /*
    3986             :                  * Allocate just enough to fit this LSA only, to avoid including
    3987             :                  * other
    3988             :                  * LSAs in fragmented LSA Updates.
    3989             :                  */
    3990           0 :                 size = ntohs(lsa->data->length)
    3991           0 :                        + (oi->ifp->mtu - ospf_packet_max(oi))
    3992           0 :                        + OSPF_LS_UPD_MIN_SIZE;
    3993             :         } else
    3994          48 :                 size = oi->ifp->mtu;
    3995             : 
    3996          48 :         if (size > OSPF_MAX_PACKET_SIZE) {
    3997           0 :                 flog_warn(
    3998             :                         EC_OSPF_LARGE_LSA,
    3999             :                         "%s: oversized LSA id:%pI4 too big, %d bytes, packet size %ld, dropping it completely. OSPF routing is broken!",
    4000             :                         __func__, &lsa->data->id, ntohs(lsa->data->length),
    4001             :                         (long int)size);
    4002           0 :                 list_delete_node(update, ln);
    4003           0 :                 return NULL;
    4004             :         }
    4005             : 
    4006             :         /* IP header is built up separately by ospf_write(). This means, that we
    4007             :          * must
    4008             :          * reduce the "affordable" size just calculated by length of an IP
    4009             :          * header.
    4010             :          * This makes sure, that even if we manage to fill the payload with LSA
    4011             :          * data
    4012             :          * completely, the final packet (our data plus IP header) still fits
    4013             :          * into
    4014             :          * outgoing interface MTU. This correction isn't really meaningful for
    4015             :          * an
    4016             :          * oversized LSA, but for consistency the correction is done for both
    4017             :          * cases.
    4018             :          *
    4019             :          * P.S. OSPF_MAX_PACKET_SIZE above already includes IP header size
    4020             :          */
    4021          48 :         return ospf_packet_new(size - sizeof(struct ip));
    4022             : }
    4023             : 
    4024          48 : static void ospf_ls_upd_queue_send(struct ospf_interface *oi,
    4025             :                                    struct list *update, struct in_addr addr,
    4026             :                                    int send_lsupd_now)
    4027             : {
    4028          48 :         struct ospf_packet *op;
    4029          48 :         uint16_t length = OSPF_HEADER_SIZE;
    4030             : 
    4031          48 :         if (IS_DEBUG_OSPF_EVENT)
    4032          48 :                 zlog_debug("listcount = %d, [%s]dst %pI4", listcount(update),
    4033             :                            IF_NAME(oi), &addr);
    4034             : 
    4035             :         /* Check that we have really something to process */
    4036          48 :         if (listcount(update) == 0)
    4037             :                 return;
    4038             : 
    4039          48 :         op = ospf_ls_upd_packet_new(update, oi);
    4040             : 
    4041             :         /* Prepare OSPF common header. */
    4042          48 :         ospf_make_header(OSPF_MSG_LS_UPD, oi, op->s);
    4043             : 
    4044             :         /* Prepare OSPF Link State Update body.
    4045             :          * Includes Type-7 translation.
    4046             :          */
    4047          48 :         length += ospf_make_ls_upd(oi, update, op->s);
    4048             : 
    4049             :         /* Fill OSPF header. */
    4050          48 :         ospf_fill_header(oi, op->s, length);
    4051             : 
    4052             :         /* Set packet length. */
    4053          48 :         op->length = length;
    4054             : 
    4055             :         /* Decide destination address. */
    4056          48 :         if (oi->type == OSPF_IFTYPE_POINTOPOINT)
    4057           0 :                 op->dst.s_addr = htonl(OSPF_ALLSPFROUTERS);
    4058             :         else
    4059          48 :                 op->dst.s_addr = addr.s_addr;
    4060             : 
    4061             :         /* Add packet to the interface output queue. */
    4062          48 :         ospf_packet_add(oi, op);
    4063             :         /* Call ospf_write() right away to send ospf packets to neighbors */
    4064          48 :         if (send_lsupd_now) {
    4065          16 :                 struct thread os_packet_thd;
    4066             : 
    4067          16 :                 os_packet_thd.arg = (void *)oi->ospf;
    4068          16 :                 if (oi->on_write_q == 0) {
    4069          16 :                         listnode_add(oi->ospf->oi_write_q, oi);
    4070          16 :                         oi->on_write_q = 1;
    4071             :                 }
    4072          16 :                 ospf_write(&os_packet_thd);
    4073             :                 /*
    4074             :                  * We are fake calling ospf_write with a fake
    4075             :                  * thread.  Imagine that we have oi_a already
    4076             :                  * enqueued and we have turned on the write
    4077             :                  * thread(t_write).
    4078             :                  * Now this function calls this for oi_b
    4079             :                  * so the on_write_q has oi_a and oi_b on
    4080             :                  * it, ospf_write runs and clears the packets
    4081             :                  * for both oi_a and oi_b.  Removing them from
    4082             :                  * the on_write_q.  After this thread of execution
    4083             :                  * finishes we will execute the t_write thread
    4084             :                  * with nothing in the on_write_q causing an
    4085             :                  * assert.  So just make sure that the t_write
    4086             :                  * is actually turned off.
    4087             :                  */
    4088          16 :                 if (list_isempty(oi->ospf->oi_write_q))
    4089          16 :                         THREAD_OFF(oi->ospf->t_write);
    4090             :         } else {
    4091             :                 /* Hook thread to write packet. */
    4092          32 :                 OSPF_ISM_WRITE_ON(oi->ospf);
    4093             :         }
    4094             : }
    4095             : 
    4096          31 : static void ospf_ls_upd_send_queue_event(struct thread *thread)
    4097             : {
    4098          31 :         struct ospf_interface *oi = THREAD_ARG(thread);
    4099          31 :         struct route_node *rn;
    4100          31 :         struct route_node *rnext;
    4101          31 :         struct list *update;
    4102          31 :         char again = 0;
    4103             : 
    4104          31 :         oi->t_ls_upd_event = NULL;
    4105             : 
    4106          31 :         if (IS_DEBUG_OSPF_EVENT)
    4107          31 :                 zlog_debug("%s start", __func__);
    4108             : 
    4109          64 :         for (rn = route_top(oi->ls_upd_queue); rn; rn = rnext) {
    4110          33 :                 rnext = route_next(rn);
    4111             : 
    4112          33 :                 if (rn->info == NULL)
    4113           1 :                         continue;
    4114             : 
    4115          32 :                 update = (struct list *)rn->info;
    4116             : 
    4117          32 :                 ospf_ls_upd_queue_send(oi, update, rn->p.u.prefix4, 0);
    4118             : 
    4119             :                 /* list might not be empty. */
    4120          32 :                 if (listcount(update) == 0) {
    4121          32 :                         list_delete((struct list **)&rn->info);
    4122          32 :                         route_unlock_node(rn);
    4123             :                 } else
    4124             :                         again = 1;
    4125             :         }
    4126             : 
    4127          31 :         if (again != 0) {
    4128           0 :                 if (IS_DEBUG_OSPF_EVENT)
    4129           0 :                         zlog_debug(
    4130             :                                 "%s: update lists not cleared, %d nodes to try again, raising new event",
    4131             :                                 __func__, again);
    4132           0 :                 oi->t_ls_upd_event = NULL;
    4133           0 :                 thread_add_event(master, ospf_ls_upd_send_queue_event, oi, 0,
    4134             :                                  &oi->t_ls_upd_event);
    4135             :         }
    4136             : 
    4137          31 :         if (IS_DEBUG_OSPF_EVENT)
    4138          31 :                 zlog_debug("%s stop", __func__);
    4139          31 : }
    4140             : 
    4141          80 : void ospf_ls_upd_send(struct ospf_neighbor *nbr, struct list *update, int flag,
    4142             :                       int send_lsupd_now)
    4143             : {
    4144          80 :         struct ospf_interface *oi;
    4145          80 :         struct ospf_lsa *lsa;
    4146          80 :         struct prefix_ipv4 p;
    4147          80 :         struct route_node *rn;
    4148          80 :         struct listnode *node;
    4149             : 
    4150          80 :         oi = nbr->oi;
    4151             : 
    4152          80 :         p.family = AF_INET;
    4153          80 :         p.prefixlen = IPV4_MAX_BITLEN;
    4154             : 
    4155             :         /* Decide destination address. */
    4156          80 :         if (oi->type == OSPF_IFTYPE_VIRTUALLINK)
    4157           0 :                 p.prefix = oi->vl_data->peer_addr;
    4158          80 :         else if (oi->type == OSPF_IFTYPE_POINTOPOINT)
    4159           0 :                 p.prefix.s_addr = htonl(OSPF_ALLSPFROUTERS);
    4160          80 :         else if (flag == OSPF_SEND_PACKET_DIRECT)
    4161           8 :                 p.prefix = nbr->address.u.prefix4;
    4162          72 :         else if (oi->state == ISM_DR || oi->state == ISM_Backup)
    4163          64 :                 p.prefix.s_addr = htonl(OSPF_ALLSPFROUTERS);
    4164           8 :         else if (oi->type == OSPF_IFTYPE_POINTOMULTIPOINT)
    4165           0 :                 p.prefix.s_addr = htonl(OSPF_ALLSPFROUTERS);
    4166             :         else
    4167           8 :                 p.prefix.s_addr = htonl(OSPF_ALLDROUTERS);
    4168             : 
    4169          80 :         if (oi->type == OSPF_IFTYPE_NBMA) {
    4170           0 :                 if (flag == OSPF_SEND_PACKET_INDIRECT)
    4171           0 :                         flog_warn(
    4172             :                                 EC_OSPF_PACKET,
    4173             :                                 "* LS-Update is directly sent on NBMA network.");
    4174           0 :                 if (IPV4_ADDR_SAME(&oi->address->u.prefix4, &p.prefix))
    4175           0 :                         flog_warn(EC_OSPF_PACKET,
    4176             :                                   "* LS-Update is sent to myself.");
    4177             :         }
    4178             : 
    4179          80 :         rn = route_node_get(oi->ls_upd_queue, (struct prefix *)&p);
    4180             : 
    4181          80 :         if (rn->info == NULL)
    4182          37 :                 rn->info = list_new();
    4183             :         else
    4184          43 :                 route_unlock_node(rn);
    4185             : 
    4186         257 :         for (ALL_LIST_ELEMENTS_RO(update, node, lsa))
    4187          97 :                 listnode_add(rn->info,
    4188          97 :                              ospf_lsa_lock(lsa)); /* oi->ls_upd_queue */
    4189          80 :         if (send_lsupd_now) {
    4190          16 :                 struct list *send_update_list;
    4191          16 :                 struct route_node *rnext;
    4192             : 
    4193          32 :                 for (rn = route_top(oi->ls_upd_queue); rn; rn = rnext) {
    4194          16 :                         rnext = route_next(rn);
    4195             : 
    4196          16 :                         if (rn->info == NULL)
    4197           0 :                                 continue;
    4198             : 
    4199          16 :                         send_update_list = (struct list *)rn->info;
    4200             : 
    4201          16 :                         ospf_ls_upd_queue_send(oi, send_update_list,
    4202             :                                                rn->p.u.prefix4, 1);
    4203             :                 }
    4204             :         } else
    4205          64 :                 thread_add_event(master, ospf_ls_upd_send_queue_event, oi, 0,
    4206             :                                  &oi->t_ls_upd_event);
    4207          80 : }
    4208             : 
    4209          23 : static void ospf_ls_ack_send_list(struct ospf_interface *oi, struct list *ack,
    4210             :                                   struct in_addr dst)
    4211             : {
    4212          23 :         struct ospf_packet *op;
    4213          23 :         uint16_t length = OSPF_HEADER_SIZE;
    4214             : 
    4215          23 :         op = ospf_packet_new(oi->ifp->mtu);
    4216             : 
    4217             :         /* Prepare OSPF common header. */
    4218          23 :         ospf_make_header(OSPF_MSG_LS_ACK, oi, op->s);
    4219             : 
    4220             :         /* Prepare OSPF Link State Acknowledgment body. */
    4221          23 :         length += ospf_make_ls_ack(oi, ack, op->s);
    4222             : 
    4223             :         /* Fill OSPF header. */
    4224          23 :         ospf_fill_header(oi, op->s, length);
    4225             : 
    4226             :         /* Set packet length. */
    4227          23 :         op->length = length;
    4228             : 
    4229             :         /* Decide destination address. */
    4230          23 :         if (oi->type == OSPF_IFTYPE_POINTOPOINT ||
    4231             :             oi->type == OSPF_IFTYPE_POINTOMULTIPOINT)
    4232           0 :                 op->dst.s_addr = htonl(OSPF_ALLSPFROUTERS);
    4233             :         else
    4234          23 :                 op->dst.s_addr = dst.s_addr;
    4235             : 
    4236             :         /* Add packet to the interface output queue. */
    4237          23 :         ospf_packet_add(oi, op);
    4238             : 
    4239             :         /* Hook thread to write packet. */
    4240          23 :         OSPF_ISM_WRITE_ON(oi->ospf);
    4241          23 : }
    4242             : 
    4243           9 : static void ospf_ls_ack_send_event(struct thread *thread)
    4244             : {
    4245           9 :         struct ospf_interface *oi = THREAD_ARG(thread);
    4246             : 
    4247           9 :         oi->t_ls_ack_direct = NULL;
    4248             : 
    4249          18 :         while (listcount(oi->ls_ack_direct.ls_ack))
    4250           9 :                 ospf_ls_ack_send_list(oi, oi->ls_ack_direct.ls_ack,
    4251             :                                       oi->ls_ack_direct.dst);
    4252           9 : }
    4253             : 
    4254          23 : void ospf_ls_ack_send(struct ospf_neighbor *nbr, struct ospf_lsa *lsa)
    4255             : {
    4256          23 :         struct ospf_interface *oi = nbr->oi;
    4257             : 
    4258          23 :         if (IS_GRACE_LSA(lsa)) {
    4259           0 :                 if (IS_DEBUG_OSPF_GR)
    4260           0 :                         zlog_debug("%s, Sending GRACE ACK to Restarter.",
    4261             :                                    __func__);
    4262             :         }
    4263             : 
    4264          23 :         if (listcount(oi->ls_ack_direct.ls_ack) == 0)
    4265           9 :                 oi->ls_ack_direct.dst = nbr->address.u.prefix4;
    4266             : 
    4267          23 :         listnode_add(oi->ls_ack_direct.ls_ack, ospf_lsa_lock(lsa));
    4268             : 
    4269          23 :         thread_add_event(master, ospf_ls_ack_send_event, oi, 0,
    4270             :                          &oi->t_ls_ack_direct);
    4271          23 : }
    4272             : 
    4273             : /* Send Link State Acknowledgment delayed. */
    4274          14 : void ospf_ls_ack_send_delayed(struct ospf_interface *oi)
    4275             : {
    4276          14 :         struct in_addr dst;
    4277             : 
    4278             :         /* Decide destination address. */
    4279             :         /* RFC2328 Section 13.5                           On non-broadcast
    4280             :               networks, delayed Link State Acknowledgment packets must be
    4281             :               unicast   separately over each adjacency (i.e., neighbor whose
    4282             :               state is >= Exchange).  */
    4283          14 :         if (oi->type == OSPF_IFTYPE_NBMA) {
    4284           0 :                 struct ospf_neighbor *nbr;
    4285           0 :                 struct route_node *rn;
    4286             : 
    4287           0 :                 for (rn = route_top(oi->nbrs); rn; rn = route_next(rn)) {
    4288           0 :                         nbr = rn->info;
    4289             : 
    4290           0 :                         if (!nbr)
    4291           0 :                                 continue;
    4292             : 
    4293           0 :                         if (nbr != oi->nbr_self && nbr->state >= NSM_Exchange)
    4294           0 :                                 while (listcount(oi->ls_ack))
    4295           0 :                                         ospf_ls_ack_send_list(
    4296             :                                                 oi, oi->ls_ack,
    4297             :                                                 nbr->address.u.prefix4);
    4298             :                 }
    4299           0 :                 return;
    4300             :         }
    4301          14 :         if (oi->type == OSPF_IFTYPE_VIRTUALLINK)
    4302           0 :                 dst.s_addr = oi->vl_data->peer_addr.s_addr;
    4303          14 :         else if (oi->state == ISM_DR || oi->state == ISM_Backup)
    4304          10 :                 dst.s_addr = htonl(OSPF_ALLSPFROUTERS);
    4305           4 :         else if (oi->type == OSPF_IFTYPE_POINTOPOINT)
    4306           0 :                 dst.s_addr = htonl(OSPF_ALLSPFROUTERS);
    4307           4 :         else if (oi->type == OSPF_IFTYPE_POINTOMULTIPOINT)
    4308           0 :                 dst.s_addr = htonl(OSPF_ALLSPFROUTERS);
    4309             :         else
    4310           4 :                 dst.s_addr = htonl(OSPF_ALLDROUTERS);
    4311             : 
    4312          28 :         while (listcount(oi->ls_ack))
    4313          14 :                 ospf_ls_ack_send_list(oi, oi->ls_ack, dst);
    4314             : }
    4315             : 
    4316             : /*
    4317             :  * On pt-to-pt links, all OSPF control packets are sent to the multicast
    4318             :  * address. As a result, the kernel does not need to learn the interface
    4319             :  * MAC of the OSPF neighbor. However, in our world, this will delay
    4320             :  * convergence. Take the case when due to a link flap, all routes now
    4321             :  * want to use an interface which was deemed to be costlier prior to this
    4322             :  * event. For routes that will be installed, the missing MAC will have
    4323             :  * punt-to-CPU set on them. This may overload the CPU control path that
    4324             :  * can be avoided if the MAC was known apriori.
    4325             :  */
    4326          31 : void ospf_proactively_arp(struct ospf_neighbor *nbr)
    4327             : {
    4328          31 :         if (!nbr || !nbr->oi->ospf->proactive_arp)
    4329             :                 return;
    4330             : 
    4331          31 :         ospf_zebra_send_arp(nbr->oi->ifp, &nbr->address);
    4332             : }

Generated by: LCOV version v1.16-topotato