ospf_packet.c
上传用户:xiaozhuqw
上传日期:2009-11-15
资源大小:1338k
文件大小:86k
源码类别:

网络

开发平台:

Unix_Linux

  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
  18.  * along with GNU Zebra; see the file COPYING.  If not, write to the Free
  19.  * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  20.  * 02111-1307, USA.
  21.  */
  22. #include <zebra.h>
  23. #include "thread.h"
  24. #include "memory.h"
  25. #include "linklist.h"
  26. #include "prefix.h"
  27. #include "if.h"
  28. #include "table.h"
  29. #include "sockunion.h"
  30. #include "stream.h"
  31. #include "log.h"
  32. #include "checksum.h"
  33. #include "md5-gnu.h"
  34. #include "ospfd/ospfd.h"
  35. #include "ospfd/ospf_network.h"
  36. #include "ospfd/ospf_interface.h"
  37. #include "ospfd/ospf_ism.h"
  38. #include "ospfd/ospf_asbr.h"
  39. #include "ospfd/ospf_lsa.h"
  40. #include "ospfd/ospf_lsdb.h"
  41. #include "ospfd/ospf_neighbor.h"
  42. #include "ospfd/ospf_nsm.h"
  43. #include "ospfd/ospf_packet.h"
  44. #include "ospfd/ospf_spf.h"
  45. #include "ospfd/ospf_flood.h"
  46. #include "ospfd/ospf_dump.h"
  47. static void ospf_ls_ack_send_list (struct ospf_interface *, list,
  48.    struct in_addr);
  49. /* Packet Type String. */
  50. char *ospf_packet_type_str[] =
  51.   {
  52.     "unknown",
  53.     "Hello",
  54.     "Database Description",
  55.     "Link State Request",
  56.     "Link State Update",
  57.     "Link State Acknowledgment",
  58.   };
  59. /* OSPF authentication checking function */
  60. int
  61. ospf_auth_type (struct ospf_interface *oi)
  62. {
  63.   int auth_type;
  64.   if (OSPF_IF_PARAM (oi, auth_type) == OSPF_AUTH_NOTSET)
  65.     auth_type = oi->area->auth_type;
  66.   else
  67.     auth_type = OSPF_IF_PARAM (oi, auth_type);
  68.   /* Handle case where MD5 key list is not configured aka Cisco */
  69.   if (auth_type == OSPF_AUTH_CRYPTOGRAPHIC &&
  70.       list_isempty (OSPF_IF_PARAM (oi, auth_crypt)))
  71.     return OSPF_AUTH_NULL;
  72.   
  73.   return auth_type;
  74. }
  75. /* forward output pointer. */
  76. void
  77. ospf_output_forward (struct stream *s, int size)
  78. {
  79.   s->putp += size;
  80. }
  81. struct ospf_packet *
  82. ospf_packet_new (size_t size)
  83. {
  84.   struct ospf_packet *new;
  85.   new = XCALLOC (MTYPE_OSPF_PACKET, sizeof (struct ospf_packet));
  86.   new->s = stream_new (size);
  87.   return new;
  88. }
  89. void
  90. ospf_packet_free (struct ospf_packet *op)
  91. {
  92.   if (op->s)
  93.     stream_free (op->s);
  94.   XFREE (MTYPE_OSPF_PACKET, op);
  95.   op = NULL;
  96. }
  97. struct ospf_fifo *
  98. ospf_fifo_new ()
  99. {
  100.   struct ospf_fifo *new;
  101.   new = XCALLOC (MTYPE_OSPF_FIFO, sizeof (struct ospf_fifo));
  102.   return new;
  103. }
  104. /* Add new packet to fifo. */
  105. void
  106. ospf_fifo_push (struct ospf_fifo *fifo, struct ospf_packet *op)
  107. {
  108.   if (fifo->tail)
  109.     fifo->tail->next = op;
  110.   else
  111.     fifo->head = op;
  112.   fifo->tail = op;
  113.   fifo->count++;
  114. }
  115. /* Delete first packet from fifo. */
  116. struct ospf_packet *
  117. ospf_fifo_pop (struct ospf_fifo *fifo)
  118. {
  119.   struct ospf_packet *op;
  120.   op = fifo->head;
  121.   if (op)
  122.     {
  123.       fifo->head = op->next;
  124.       if (fifo->head == NULL)
  125. fifo->tail = NULL;
  126.       fifo->count--;
  127.     }
  128.   return op;
  129. }
  130. /* Return first fifo entry. */
  131. struct ospf_packet *
  132. ospf_fifo_head (struct ospf_fifo *fifo)
  133. {
  134.   return fifo->head;
  135. }
  136. /* Flush ospf packet fifo. */
  137. void
  138. ospf_fifo_flush (struct ospf_fifo *fifo)
  139. {
  140.   struct ospf_packet *op;
  141.   struct ospf_packet *next;
  142.   for (op = fifo->head; op; op = next)
  143.     {
  144.       next = op->next;
  145.       ospf_packet_free (op);
  146.     }
  147.   fifo->head = fifo->tail = NULL;
  148.   fifo->count = 0;
  149. }
  150. /* Free ospf packet fifo. */
  151. void
  152. ospf_fifo_free (struct ospf_fifo *fifo)
  153. {
  154.   ospf_fifo_flush (fifo);
  155.   XFREE (MTYPE_OSPF_FIFO, fifo);
  156. }
  157. void
  158. ospf_packet_add (struct ospf_interface *oi, struct ospf_packet *op)
  159. {
  160.   /* Add packet to end of queue. */
  161.   ospf_fifo_push (oi->obuf, op);
  162.   /* Debug of packet fifo*/
  163.   /* ospf_fifo_debug (oi->obuf); */
  164. }
  165. void
  166. ospf_packet_delete (struct ospf_interface *oi)
  167. {
  168.   struct ospf_packet *op;
  169.   
  170.   op = ospf_fifo_pop (oi->obuf);
  171.   if (op)
  172.     ospf_packet_free (op);
  173. }
  174. struct stream *
  175. ospf_stream_copy (struct stream *new, struct stream *s)
  176. {
  177.   new->endp = s->endp;
  178.   new->putp = s->putp;
  179.   new->getp = s->getp;
  180.   memcpy (new->data, s->data, stream_get_endp (s));
  181.   return new;
  182. }
  183. struct ospf_packet *
  184. ospf_packet_dup (struct ospf_packet *op)
  185. {
  186.   struct ospf_packet *new;
  187.   if (stream_get_endp(op->s) != op->length)
  188.     zlog_warn ("ospf_packet_dup stream %ld ospf_packet %d size mismatch",
  189.        STREAM_SIZE(op->s), op->length);
  190.   /* Reserve space for MD5 authentication that may be added later. */
  191.   new = ospf_packet_new (stream_get_endp(op->s) + OSPF_AUTH_MD5_SIZE);
  192.   ospf_stream_copy (new->s, op->s);
  193.   new->dst = op->dst;
  194.   new->length = op->length;
  195.   return new;
  196. }
  197. int
  198. ospf_packet_max (struct ospf_interface *oi)
  199. {
  200.   int max;
  201.   if ( ospf_auth_type (oi) == OSPF_AUTH_CRYPTOGRAPHIC)
  202.     max = oi->ifp->mtu - OSPF_AUTH_MD5_SIZE - 88;
  203.   else
  204.     max = oi->ifp->mtu - 88;
  205.   return max;
  206. }
  207. int
  208. ospf_check_md5_digest (struct ospf_interface *oi, struct stream *s,
  209.                        u_int16_t length)
  210. {
  211.   void *ibuf;
  212.   struct md5_ctx ctx;
  213.   unsigned char digest[OSPF_AUTH_MD5_SIZE];
  214.   unsigned char *pdigest;
  215.   struct crypt_key *ck;
  216.   struct ospf_header *ospfh;
  217.   struct ospf_neighbor *nbr;
  218.   
  219.   ibuf = STREAM_PNT (s);
  220.   ospfh = (struct ospf_header *) ibuf;
  221.   /* Get pointer to the end of the packet. */
  222.   pdigest = (unsigned char *) ibuf + length;
  223.   /* Get secret key. */
  224.   ck = ospf_crypt_key_lookup (OSPF_IF_PARAM (oi, auth_crypt),
  225.       ospfh->u.crypt.key_id);
  226.   if (ck == NULL)
  227.     {
  228.       zlog_warn ("interface %s: ospf_check_md5 no key %d",
  229.  IF_NAME (oi), ospfh->u.crypt.key_id);
  230.       return 0;
  231.     }
  232.   /* check crypto seqnum. */
  233.   nbr = ospf_nbr_lookup_by_routerid (oi->nbrs, &ospfh->router_id);
  234.   if (nbr && ntohl(nbr->crypt_seqnum) > ntohl(ospfh->u.crypt.crypt_seqnum))
  235.     {
  236.       zlog_warn ("interface %s: ospf_check_md5 bad sequence %d (expect %d)",
  237.  IF_NAME (oi),
  238.  ntohl(ospfh->u.crypt.crypt_seqnum),
  239.  ntohl(nbr->crypt_seqnum));
  240.       return 0;
  241.     }
  242.       
  243.   /* Generate a digest for the ospf packet - their digest + our digest. */
  244.   md5_init_ctx (&ctx);
  245.   md5_process_bytes (ibuf, length, &ctx);
  246.   md5_process_bytes (ck->auth_key, OSPF_AUTH_MD5_SIZE, &ctx);
  247.   md5_finish_ctx (&ctx, digest);
  248.   /* compare the two */
  249.   if (memcmp (pdigest, digest, OSPF_AUTH_MD5_SIZE))
  250.     {
  251.       zlog_warn ("interface %s: ospf_check_md5 checksum mismatch",
  252.  IF_NAME (oi));
  253.       return 0;
  254.     }
  255.   /* save neighbor's crypt_seqnum */
  256.   if (nbr)
  257.     nbr->crypt_seqnum = ospfh->u.crypt.crypt_seqnum;
  258.   return 1;
  259. }
  260. /* This function is called from ospf_write(), it will detect the
  261.    authentication scheme and if it is MD5, it will change the sequence
  262.    and update the MD5 digest. */
  263. int
  264. ospf_make_md5_digest (struct ospf_interface *oi, struct ospf_packet *op)
  265. {
  266.   struct ospf_header *ospfh;
  267.   unsigned char digest[OSPF_AUTH_MD5_SIZE];
  268.   struct md5_ctx ctx;
  269.   void *ibuf;
  270.   unsigned long oldputp;
  271.   u_int32_t t;
  272.   struct crypt_key *ck;
  273.   char *auth_key;
  274.   ibuf = STREAM_DATA (op->s);
  275.   ospfh = (struct ospf_header *) ibuf;
  276.   if (ntohs (ospfh->auth_type) != OSPF_AUTH_CRYPTOGRAPHIC)
  277.     return 0;
  278.   /* We do this here so when we dup a packet, we don't have to
  279.      waste CPU rewriting other headers. */
  280.   t = (time(NULL) & 0xFFFFFFFF);
  281.   oi->crypt_seqnum = ( t > oi->crypt_seqnum ? t : oi->crypt_seqnum++);
  282.   ospfh->u.crypt.crypt_seqnum = htonl (oi->crypt_seqnum); 
  283.   /* Get MD5 Authentication key from auth_key list. */
  284.   if (list_isempty (OSPF_IF_PARAM (oi, auth_crypt)))
  285.     auth_key = "";
  286.   else
  287.     {
  288.       ck = getdata (OSPF_IF_PARAM (oi, auth_crypt)->tail);
  289.       auth_key = ck->auth_key;
  290.     }
  291.   /* Generate a digest for the entire packet + our secret key. */
  292.   md5_init_ctx (&ctx);
  293.   md5_process_bytes (ibuf, ntohs (ospfh->length), &ctx);
  294.   md5_process_bytes (auth_key, OSPF_AUTH_MD5_SIZE, &ctx);
  295.   md5_finish_ctx (&ctx, digest);
  296.   /* Append md5 digest to the end of the stream. */
  297.   oldputp = stream_get_putp (op->s);
  298.   stream_set_putp (op->s, ntohs (ospfh->length));
  299.   stream_put (op->s, digest, OSPF_AUTH_MD5_SIZE);
  300.   stream_set_putp (op->s, oldputp);
  301.   /* We do *NOT* increment the OSPF header length. */
  302.   op->length = ntohs (ospfh->length) + OSPF_AUTH_MD5_SIZE;
  303.   if (stream_get_endp(op->s) != op->length)
  304.     zlog_warn("ospf_make_md5_digest: length mismatch stream %ld ospf_packet %d", stream_get_endp(op->s), op->length);
  305.   return OSPF_AUTH_MD5_SIZE;
  306. }
  307. int
  308. ospf_ls_req_timer (struct thread *thread)
  309. {
  310.   struct ospf_neighbor *nbr;
  311.   nbr = THREAD_ARG (thread);
  312.   nbr->t_ls_req = NULL;
  313.   /* Send Link State Request. */
  314.   if (ospf_ls_request_count (nbr))
  315.     ospf_ls_req_send (nbr);
  316.   /* Set Link State Request retransmission timer. */
  317.   OSPF_NSM_TIMER_ON (nbr->t_ls_req, ospf_ls_req_timer, nbr->v_ls_req);
  318.   return 0;
  319. }
  320. void
  321. ospf_ls_req_event (struct ospf_neighbor *nbr)
  322. {
  323.   if (nbr->t_ls_req)
  324.     {
  325.       thread_cancel (nbr->t_ls_req);
  326.       nbr->t_ls_req = NULL;
  327.     }
  328.   nbr->t_ls_req = thread_add_event (master, ospf_ls_req_timer, nbr, 0);
  329. }
  330. /* Cyclic timer function.  Fist registered in ospf_nbr_new () in
  331.    ospf_neighbor.c  */
  332. int
  333. ospf_ls_upd_timer (struct thread *thread)
  334. {
  335.   struct ospf_neighbor *nbr;
  336.   nbr = THREAD_ARG (thread);
  337.   nbr->t_ls_upd = NULL;
  338.   /* Send Link State Update. */
  339.   if (ospf_ls_retransmit_count (nbr) > 0)
  340.     {
  341.       list update;
  342.       struct ospf_lsdb *lsdb;
  343.       int i;
  344.       struct timeval now;
  345.       int retransmit_interval;
  346.       gettimeofday (&now, NULL);
  347.       retransmit_interval = OSPF_IF_PARAM (nbr->oi, retransmit_interval);
  348.       lsdb = &nbr->ls_rxmt;
  349.       update = list_new ();
  350.       for (i = OSPF_MIN_LSA; i < OSPF_MAX_LSA; i++)
  351. {
  352.   struct route_table *table = lsdb->type[i].db;
  353.   struct route_node *rn;
  354.   
  355.   for (rn = route_top (table); rn; rn = route_next (rn))
  356.     {
  357.       struct ospf_lsa *lsa;
  358.       
  359.       if ((lsa = rn->info) != NULL)
  360. /* Don't retransmit an LSA if we received it within
  361.    the last RxmtInterval seconds - this is to allow the
  362.    neighbour a chance to acknowledge the LSA as it may
  363.    have ben just received before the retransmit timer
  364.    fired.  This is a small tweak to what is in the RFC,
  365.    but it will cut out out a lot of retransmit traffic
  366.    - MAG */
  367. if (tv_cmp (tv_sub (now, lsa->tv_recv), 
  368.     int2tv (retransmit_interval)) >= 0)
  369.   listnode_add (update, rn->info);
  370.     }
  371. }
  372.       if (listcount (update) > 0)
  373. ospf_ls_upd_send (nbr, update, OSPF_SEND_PACKET_DIRECT);
  374.       list_delete (update);
  375.     }
  376.   /* Set LS Update retransmission timer. */
  377.   OSPF_NSM_TIMER_ON (nbr->t_ls_upd, ospf_ls_upd_timer, nbr->v_ls_upd);
  378.   return 0;
  379. }
  380. int
  381. ospf_ls_ack_timer (struct thread *thread)
  382. {
  383.   struct ospf_interface *oi;
  384.   oi = THREAD_ARG (thread);
  385.   oi->t_ls_ack = NULL;
  386.   /* Send Link State Acknowledgment. */
  387.   if (listcount (oi->ls_ack) > 0)
  388.     ospf_ls_ack_send_delayed (oi);
  389.   /* Set LS Ack timer. */
  390.   OSPF_ISM_TIMER_ON (oi->t_ls_ack, ospf_ls_ack_timer, oi->v_ls_ack);
  391.   return 0;
  392. }
  393. int
  394. ospf_write (struct thread *thread)
  395. {
  396.   struct ospf *ospf = THREAD_ARG (thread);
  397.   struct ospf_interface *oi;
  398.   struct ospf_packet *op;
  399.   struct sockaddr_in sa_dst;
  400.   struct ip iph;
  401.   struct msghdr msg;
  402.   struct iovec iov[2];
  403.   u_char type;
  404.   int ret;
  405.   int flags = 0;
  406.   listnode node;
  407.   
  408.   ospf->t_write = NULL;
  409.   node = listhead (ospf->oi_write_q);
  410.   assert (node);
  411.   oi = getdata (node);
  412.   assert (oi);
  413.   
  414.   /* Get one packet from queue. */
  415.   op = ospf_fifo_head (oi->obuf);
  416.   assert (op);
  417.   assert (op->length >= OSPF_HEADER_SIZE);
  418.   if (op->dst.s_addr == htonl (OSPF_ALLSPFROUTERS)
  419.       || op->dst.s_addr == htonl (OSPF_ALLDROUTERS))
  420.     ospf_if_ipmulticast (ospf, oi->address, oi->ifp->ifindex);
  421.   /* Rewrite the md5 signature & update the seq */
  422.   ospf_make_md5_digest (oi, op);
  423.   memset (&sa_dst, 0, sizeof (sa_dst));
  424.   sa_dst.sin_family = AF_INET;
  425. #ifdef HAVE_SIN_LEN
  426.   sa_dst.sin_len = sizeof(sa_dst);
  427. #endif /* HAVE_SIN_LEN */
  428.   sa_dst.sin_addr = op->dst;
  429.   sa_dst.sin_port = htons (0);
  430.   /* Set DONTROUTE flag if dst is unicast. */
  431.   if (oi->type != OSPF_IFTYPE_VIRTUALLINK)
  432.     if (!IN_MULTICAST (htonl (op->dst.s_addr)))
  433.       flags = MSG_DONTROUTE;
  434.   iph.ip_hl = sizeof (struct ip) >> 2;
  435.   iph.ip_v = IPVERSION;
  436.   iph.ip_tos = IPTOS_PREC_INTERNETCONTROL;
  437. #if defined(__NetBSD__) || defined(__FreeBSD__)
  438.   iph.ip_len = iph.ip_hl*4 + op->length;
  439. #else
  440.   iph.ip_len = htons (iph.ip_hl*4 + op->length);
  441. #endif
  442.   iph.ip_id = 0;
  443.   iph.ip_off = 0;
  444.   if (oi->type == OSPF_IFTYPE_VIRTUALLINK)
  445.     iph.ip_ttl = OSPF_VL_IP_TTL;
  446.   else
  447.     iph.ip_ttl = OSPF_IP_TTL;
  448.   iph.ip_p = IPPROTO_OSPFIGP;
  449.   iph.ip_sum = 0;
  450.   iph.ip_src.s_addr = oi->address->u.prefix4.s_addr;
  451.   iph.ip_dst.s_addr = op->dst.s_addr;
  452.   memset (&msg, 0, sizeof (msg));
  453.   msg.msg_name = &sa_dst;
  454.   msg.msg_namelen = sizeof (sa_dst); 
  455.   msg.msg_iov = iov;
  456.   msg.msg_iovlen = 2;
  457.   iov[0].iov_base = (char*)&iph;
  458.   iov[0].iov_len = iph.ip_hl*4;
  459.   iov[1].iov_base = STREAM_DATA (op->s);
  460.   iov[1].iov_len = op->length;
  461.   ret = sendmsg (ospf->fd, &msg, flags);
  462.   
  463.   if (ret < 0)
  464.     zlog_warn ("*** sendmsg in ospf_write failed with %s", strerror (errno));
  465.   /* Retrieve OSPF packet type. */
  466.   stream_set_getp (op->s, 1);
  467.   type = stream_getc (op->s);
  468.   /* Show debug sending packet. */
  469.   if (IS_DEBUG_OSPF_PACKET (type - 1, SEND))
  470.     {
  471.       if (IS_DEBUG_OSPF_PACKET (type - 1, DETAIL))
  472. {
  473.   zlog_info ("-----------------------------------------------------");
  474.   stream_set_getp (op->s, 0);
  475.   ospf_packet_dump (op->s);
  476. }
  477.       zlog_info ("%s sent to [%s] via [%s].",
  478.  ospf_packet_type_str[type], inet_ntoa (op->dst),
  479.  IF_NAME (oi));
  480.       if (IS_DEBUG_OSPF_PACKET (type - 1, DETAIL))
  481. zlog_info ("-----------------------------------------------------");
  482.     }
  483.   /* Now delete packet from queue. */
  484.   ospf_packet_delete (oi);
  485.   if (ospf_fifo_head (oi->obuf) == NULL)
  486.     {
  487.       oi->on_write_q = 0;
  488.       list_delete_node (ospf->oi_write_q, node);
  489.     }
  490.   
  491.   /* If packets still remain in queue, call write thread. */
  492.   if (!list_isempty (ospf->oi_write_q))
  493.     ospf->t_write =                                              
  494.       thread_add_write (master, ospf_write, ospf, ospf->fd);
  495.   return 0;
  496. }
  497. /* OSPF Hello message read -- RFC2328 Section 10.5. */
  498. void
  499. ospf_hello (struct ip *iph, struct ospf_header *ospfh,
  500.     struct stream * s, struct ospf_interface *oi, int size)
  501. {
  502.   struct ospf_hello *hello;
  503.   struct ospf_neighbor *nbr;
  504.   struct route_node *rn;
  505.   struct prefix p, key;
  506.   int old_state;
  507.   /* increment statistics. */
  508.   oi->hello_in++;
  509.   hello = (struct ospf_hello *) STREAM_PNT (s);
  510.   /* If Hello is myself, silently discard. */
  511.   if (IPV4_ADDR_SAME (&ospfh->router_id, &oi->ospf->router_id))
  512.     return;
  513.   /* If incoming interface is passive one, ignore Hello. */
  514.   if (OSPF_IF_PARAM (oi, passive_interface) == OSPF_IF_PASSIVE)
  515.     return;
  516.   /* get neighbor prefix. */
  517.   p.family = AF_INET;
  518.   p.prefixlen = ip_masklen (hello->network_mask);
  519.   p.u.prefix4 = iph->ip_src;
  520.   /* Compare network mask. */
  521.   /* Checking is ignored for Point-to-Point and Virtual link. */
  522.   if (oi->type != OSPF_IFTYPE_POINTOPOINT 
  523.       && oi->type != OSPF_IFTYPE_VIRTUALLINK)
  524.     if (oi->address->prefixlen != p.prefixlen)
  525.       {
  526. zlog_warn ("Packet %s [Hello:RECV]: NetworkMask mismatch.",
  527.    inet_ntoa (ospfh->router_id));
  528. return;
  529.       }
  530.   /* Compare Hello Interval. */
  531.   if (OSPF_IF_PARAM (oi, v_hello) != ntohs (hello->hello_interval))
  532.     {
  533.       zlog_warn ("Packet %s [Hello:RECV]: HelloInterval mismatch.",
  534.  inet_ntoa (ospfh->router_id));
  535.       return;
  536.     }
  537.   /* Compare Router Dead Interval. */
  538.   if (OSPF_IF_PARAM (oi, v_wait) != ntohl (hello->dead_interval))
  539.     {
  540.       zlog_warn ("Packet %s [Hello:RECV]: RouterDeadInterval mismatch.",
  541.  inet_ntoa (ospfh->router_id));
  542.       return;
  543.     }
  544.   if (IS_DEBUG_OSPF_EVENT)
  545.     zlog_info ("Packet %s [Hello:RECV]: Options %s",
  546.        inet_ntoa (ospfh->router_id),
  547.        ospf_options_dump (hello->options));
  548.   /* Compare options. */
  549. #define REJECT_IF_TBIT_ON 1 /* XXX */
  550. #ifdef REJECT_IF_TBIT_ON
  551.   if (CHECK_FLAG (hello->options, OSPF_OPTION_T))
  552.     {
  553.       /*
  554.        * This router does not support non-zero TOS.
  555.        * Drop this Hello packet not to establish neighbor relationship.
  556.        */
  557.       zlog_warn ("Packet %s [Hello:RECV]: T-bit on, drop it.",
  558.  inet_ntoa (ospfh->router_id));
  559.       return;
  560.     }
  561. #endif /* REJECT_IF_TBIT_ON */
  562. #ifdef HAVE_OPAQUE_LSA
  563.   if (CHECK_FLAG (oi->ospf->config, OSPF_OPAQUE_CAPABLE)
  564.       && CHECK_FLAG (hello->options, OSPF_OPTION_O))
  565.     {
  566.       /*
  567.        * This router does know the correct usage of O-bit
  568.        * the bit should be set in DD packet only.
  569.        */
  570.       zlog_warn ("Packet %s [Hello:RECV]: O-bit abuse?",
  571.  inet_ntoa (ospfh->router_id));
  572. #ifdef STRICT_OBIT_USAGE_CHECK
  573.       return;                                     /* Reject this packet. */
  574. #else /* STRICT_OBIT_USAGE_CHECK */
  575.       UNSET_FLAG (hello->options, OSPF_OPTION_O); /* Ignore O-bit. */
  576. #endif /* STRICT_OBIT_USAGE_CHECK */
  577.     }
  578. #endif /* HAVE_OPAQUE_LSA */
  579.   /* new for NSSA is to ensure that NP is on and E is off */
  580. #ifdef HAVE_NSSA
  581.   if (oi->area->external_routing == OSPF_AREA_NSSA) 
  582.     {
  583.       if (! (CHECK_FLAG (OPTIONS (oi), OSPF_OPTION_NP)
  584.      && CHECK_FLAG (hello->options, OSPF_OPTION_NP)
  585.      && ! CHECK_FLAG (OPTIONS (oi), OSPF_OPTION_E)
  586.      && ! CHECK_FLAG (hello->options, OSPF_OPTION_E)))
  587. {
  588.   zlog_warn ("NSSA-Packet-%s[Hello:RECV]: my options: %x, his options %x", inet_ntoa (ospfh->router_id), OPTIONS (oi), hello->options);
  589.   return;
  590. }
  591.       if (IS_DEBUG_OSPF_NSSA)
  592.         zlog_info ("NSSA-Hello:RECV:Packet from %s:", inet_ntoa(ospfh->router_id));
  593.     }
  594.   else    
  595. #endif /* HAVE_NSSA */
  596.     /* The setting of the E-bit found in the Hello Packet's Options
  597.        field must match this area's ExternalRoutingCapability A
  598.        mismatch causes processing to stop and the packet to be
  599.        dropped. The setting of the rest of the bits in the Hello
  600.        Packet's Options field should be ignored. */
  601.     if (CHECK_FLAG (OPTIONS (oi), OSPF_OPTION_E) !=
  602. CHECK_FLAG (hello->options, OSPF_OPTION_E))
  603.       {
  604. zlog_warn ("Packet[Hello:RECV]: my options: %x, his options %x",
  605.    OPTIONS (oi), hello->options);
  606. return;
  607.       }
  608.   /* Get neighbor information from table. */
  609.   key.family = AF_INET;
  610.   key.prefixlen = IPV4_MAX_BITLEN;
  611.   key.u.prefix4 = iph->ip_src;
  612.   rn = route_node_get (oi->nbrs, &key);
  613.   if (rn->info)
  614.     {
  615.       route_unlock_node (rn);
  616.       nbr = rn->info;
  617.       if (oi->type == OSPF_IFTYPE_NBMA && nbr->state == NSM_Attempt)
  618. {
  619.   nbr->src = iph->ip_src;
  620.   nbr->address = p;
  621. }
  622.     }
  623.   else
  624.     {
  625.       /* Create new OSPF Neighbor structure. */
  626.       nbr = ospf_nbr_new (oi);
  627.       nbr->state = NSM_Down;
  628.       nbr->src = iph->ip_src;
  629.       nbr->address = p;
  630.       rn->info = nbr;
  631.       nbr->nbr_nbma = NULL;
  632.       if (oi->type == OSPF_IFTYPE_NBMA)
  633. {
  634.   struct ospf_nbr_nbma *nbr_nbma;
  635.   listnode node;
  636.   for (node = listhead (oi->nbr_nbma); node; nextnode (node))
  637.     {
  638.       nbr_nbma = getdata (node);
  639.       assert (nbr_nbma);
  640.       
  641.       if (IPV4_ADDR_SAME(&nbr_nbma->addr, &iph->ip_src))
  642. {
  643.   nbr_nbma->nbr = nbr;
  644.   nbr->nbr_nbma = nbr_nbma;
  645.   if (nbr_nbma->t_poll)
  646.     OSPF_POLL_TIMER_OFF (nbr_nbma->t_poll);
  647.   
  648.   nbr->state_change = nbr_nbma->state_change + 1;
  649. }
  650.     }
  651. }
  652.       
  653.       /* New nbr, save the crypto sequence number if necessary */
  654.       if (ntohs (ospfh->auth_type) == OSPF_AUTH_CRYPTOGRAPHIC)
  655. nbr->crypt_seqnum = ospfh->u.crypt.crypt_seqnum;
  656.       if (IS_DEBUG_OSPF_EVENT)
  657. zlog_info ("NSM[%s:%s]: start", IF_NAME (nbr->oi),
  658.    inet_ntoa (nbr->router_id));
  659.     }
  660.   
  661.   nbr->router_id = ospfh->router_id;
  662.   old_state = nbr->state;
  663.   /* Add event to thread. */
  664.   OSPF_NSM_EVENT_EXECUTE (nbr, NSM_HelloReceived);
  665.   /*  RFC2328  Section 9.5.1
  666.       If the router is not eligible to become Designated Router,
  667.       (snip)   It must also send an Hello Packet in reply to an
  668.       Hello Packet received from any eligible neighbor (other than
  669.       the current Designated Router and Backup Designated Router).  */
  670.   if (oi->type == OSPF_IFTYPE_NBMA)
  671.     if (PRIORITY(oi) == 0 && hello->priority > 0
  672. && IPV4_ADDR_CMP(&DR(oi),  &iph->ip_src)
  673. && IPV4_ADDR_CMP(&BDR(oi), &iph->ip_src))
  674.       OSPF_NSM_TIMER_ON (nbr->t_hello_reply, ospf_hello_reply_timer,
  675.  OSPF_HELLO_REPLY_DELAY);
  676.   /* on NBMA network type, it happens to receive bidirectional Hello packet
  677.      without advance 1-Way Received event.
  678.      To avoid incorrect DR-seletion, raise 1-Way Received event.*/
  679.   if (oi->type == OSPF_IFTYPE_NBMA &&
  680.       (old_state == NSM_Down || old_state == NSM_Attempt))
  681.     {
  682.       OSPF_NSM_EVENT_EXECUTE (nbr, NSM_OneWayReceived);
  683.       nbr->priority = hello->priority;
  684.       nbr->d_router = hello->d_router;
  685.       nbr->bd_router = hello->bd_router;
  686.       return;
  687.     }
  688.   if (ospf_nbr_bidirectional (&oi->ospf->router_id, hello->neighbors,
  689.       size - OSPF_HELLO_MIN_SIZE))
  690.     {
  691.       OSPF_NSM_EVENT_EXECUTE (nbr, NSM_TwoWayReceived);
  692.       nbr->options |= hello->options;
  693.     }
  694.   else
  695.     {
  696.       OSPF_NSM_EVENT_EXECUTE (nbr, NSM_OneWayReceived);
  697.       /* Set neighbor information. */
  698.       nbr->priority = hello->priority;
  699.       nbr->d_router = hello->d_router;
  700.       nbr->bd_router = hello->bd_router;
  701.       return;
  702.     }
  703.   /* If neighbor itself declares DR and no BDR exists,
  704.      cause event BackupSeen */
  705.   if (IPV4_ADDR_SAME (&nbr->address.u.prefix4, &hello->d_router))
  706.     if (hello->bd_router.s_addr == 0 && oi->state == ISM_Waiting)
  707.       OSPF_ISM_EVENT_SCHEDULE (oi, ISM_BackupSeen);
  708.   /* neighbor itself declares BDR. */
  709.   if (oi->state == ISM_Waiting &&
  710.       IPV4_ADDR_SAME (&nbr->address.u.prefix4, &hello->bd_router))
  711.     OSPF_ISM_EVENT_SCHEDULE (oi, ISM_BackupSeen);
  712.   /* had not previously. */
  713.   if ((IPV4_ADDR_SAME (&nbr->address.u.prefix4, &hello->d_router) &&
  714.        IPV4_ADDR_CMP (&nbr->address.u.prefix4, &nbr->d_router)) ||
  715.       (IPV4_ADDR_CMP (&nbr->address.u.prefix4, &hello->d_router) &&
  716.        IPV4_ADDR_SAME (&nbr->address.u.prefix4, &nbr->d_router)))
  717.     OSPF_ISM_EVENT_SCHEDULE (oi, ISM_NeighborChange);
  718.   /* had not previously. */
  719.   if ((IPV4_ADDR_SAME (&nbr->address.u.prefix4, &hello->bd_router) &&
  720.        IPV4_ADDR_CMP (&nbr->address.u.prefix4, &nbr->bd_router)) ||
  721.       (IPV4_ADDR_CMP (&nbr->address.u.prefix4, &hello->bd_router) &&
  722.        IPV4_ADDR_SAME (&nbr->address.u.prefix4, &nbr->bd_router)))
  723.     OSPF_ISM_EVENT_SCHEDULE (oi, ISM_NeighborChange);
  724.   /* Neighbor priority check. */
  725.   if (nbr->priority >= 0 && nbr->priority != hello->priority)
  726.     OSPF_ISM_EVENT_SCHEDULE (oi, ISM_NeighborChange);
  727.   /* Set neighbor information. */
  728.   nbr->priority = hello->priority;
  729.   nbr->d_router = hello->d_router;
  730.   nbr->bd_router = hello->bd_router;
  731. }
  732. /* Save DD flags/options/Seqnum received. */
  733. void
  734. ospf_db_desc_save_current (struct ospf_neighbor *nbr,
  735.    struct ospf_db_desc *dd)
  736. {
  737.   nbr->last_recv.flags = dd->flags;
  738.   nbr->last_recv.options = dd->options;
  739.   nbr->last_recv.dd_seqnum = ntohl (dd->dd_seqnum);
  740. }
  741. /* Process rest of DD packet. */
  742. static void
  743. ospf_db_desc_proc (struct stream *s, struct ospf_interface *oi,
  744.    struct ospf_neighbor *nbr, struct ospf_db_desc *dd,
  745.    u_int16_t size)
  746. {
  747.   struct ospf_lsa *new, *find;
  748.   struct lsa_header *lsah;
  749.   stream_forward (s, OSPF_DB_DESC_MIN_SIZE);
  750.   for (size -= OSPF_DB_DESC_MIN_SIZE;
  751.        size >= OSPF_LSA_HEADER_SIZE; size -= OSPF_LSA_HEADER_SIZE) 
  752.     {
  753.       lsah = (struct lsa_header *) STREAM_PNT (s);
  754.       stream_forward (s, OSPF_LSA_HEADER_SIZE);
  755.       /* Unknown LS type. */
  756.       if (lsah->type < OSPF_MIN_LSA || lsah->type >= OSPF_MAX_LSA)
  757. {
  758.   zlog_warn ("Pakcet [DD:RECV]: Unknown LS type %d.", lsah->type);
  759.   OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_SeqNumberMismatch);
  760.   return;
  761. }
  762. #ifdef HAVE_OPAQUE_LSA
  763.       if (IS_OPAQUE_LSA (lsah->type)
  764.   &&  ! CHECK_FLAG (nbr->options, OSPF_OPTION_O))
  765.         {
  766.           zlog_warn ("LSA[Type%d:%s]: Opaque capability mismatch?", lsah->type, inet_ntoa (lsah->id));
  767.           OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_SeqNumberMismatch);
  768.           return;
  769.         }
  770. #endif /* HAVE_OPAQUE_LSA */
  771.       switch (lsah->type)
  772.         {
  773.         case OSPF_AS_EXTERNAL_LSA:
  774. #ifdef HAVE_OPAQUE_LSA
  775. case OSPF_OPAQUE_AS_LSA:
  776. #endif /* HAVE_OPAQUE_LSA */
  777. #ifdef HAVE_NSSA
  778.           /* Check for stub area.  Reject if AS-External from stub but
  779.              allow if from NSSA. */
  780.           if (oi->area->external_routing == OSPF_AREA_STUB)
  781. #else /* ! HAVE_NSSA */
  782.     if (oi->area->external_routing != OSPF_AREA_DEFAULT)
  783. #endif /* HAVE_NSSA */
  784.       {
  785. zlog_warn ("Packet [DD:RECV]: LSA[Type%d:%s] from %s area.",
  786.    lsah->type, inet_ntoa (lsah->id),
  787.    (oi->area->external_routing == OSPF_AREA_STUB) ?
  788.    "STUB" : "NSSA");
  789. OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_SeqNumberMismatch);
  790. return;
  791.       }
  792.           break;
  793. default:
  794.   break;
  795.         }
  796.       /* Create LS-request object. */
  797.       new = ospf_ls_request_new (lsah);
  798.       /* Lookup received LSA, then add LS request list. */
  799.       find = ospf_lsa_lookup_by_header (oi->area, lsah);
  800.       if (!find || ospf_lsa_more_recent (find, new) < 0)
  801. {
  802.   ospf_ls_request_add (nbr, new);
  803.   ospf_lsa_discard (new);
  804. }
  805.       else
  806. {
  807.   /* Received LSA is not recent. */
  808.   if (IS_DEBUG_OSPF_EVENT)
  809.     zlog_info ("Packet [DD:RECV]: LSA received Type %d, "
  810.        "ID %s is not recent.", lsah->type, inet_ntoa (lsah->id));
  811.   ospf_lsa_discard (new);
  812.   continue;
  813. }
  814.     }
  815.   /* Master */
  816.   if (IS_SET_DD_MS (nbr->dd_flags))
  817.     {
  818.       nbr->dd_seqnum++;
  819.       /* Entire DD packet sent. */
  820.       if (!IS_SET_DD_M (dd->flags) && !IS_SET_DD_M (nbr->dd_flags))
  821. OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_ExchangeDone);
  822.       else
  823. /* Send new DD packet. */
  824. ospf_db_desc_send (nbr);
  825.     }
  826.   /* Slave */
  827.   else
  828.     {
  829.       nbr->dd_seqnum = ntohl (dd->dd_seqnum);
  830.       /* When master's more flags is not set. */
  831.       if (!IS_SET_DD_M (dd->flags) && ospf_db_summary_isempty (nbr))
  832. {
  833.   nbr->dd_flags &= ~(OSPF_DD_FLAG_M);
  834.   OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_ExchangeDone);
  835. }
  836.       /* Send DD pakcet in reply. */
  837.       ospf_db_desc_send (nbr);
  838.     }
  839.   /* Save received neighbor values from DD. */
  840.   ospf_db_desc_save_current (nbr, dd);
  841. }
  842. int
  843. ospf_db_desc_is_dup (struct ospf_db_desc *dd, struct ospf_neighbor *nbr)
  844. {
  845.   /* Is DD duplicated? */
  846.   if (dd->options == nbr->last_recv.options &&
  847.       dd->flags == nbr->last_recv.flags &&
  848.       dd->dd_seqnum == htonl (nbr->last_recv.dd_seqnum))
  849.     return 1;
  850.   return 0;
  851. }
  852. /* OSPF Database Description message read -- RFC2328 Section 10.6. */
  853. void
  854. ospf_db_desc (struct ip *iph, struct ospf_header *ospfh,
  855.       struct stream *s, struct ospf_interface *oi, u_int16_t size)
  856. {
  857.   struct ospf_db_desc *dd;
  858.   struct ospf_neighbor *nbr;
  859.   /* Increment statistics. */
  860.   oi->db_desc_in++;
  861.   dd = (struct ospf_db_desc *) STREAM_PNT (s);
  862.   nbr = ospf_nbr_lookup_by_addr (oi->nbrs, &iph->ip_src);
  863.   if (nbr == NULL)
  864.     {
  865.       zlog_warn ("Packet[DD]: Unknown Neighbor %s",
  866.  inet_ntoa (ospfh->router_id));
  867.       return;
  868.     }
  869.   /* Check MTU. */
  870.   if (ntohs (dd->mtu) > oi->ifp->mtu)
  871.     {
  872.       zlog_warn ("Packet[DD]: MTU is larger than [%s]'s MTU", IF_NAME (oi));
  873.       return;
  874.     }
  875. #ifdef REJECT_IF_TBIT_ON
  876.   if (CHECK_FLAG (dd->options, OSPF_OPTION_T))
  877.     {
  878.       /*
  879.        * In Hello protocol, optional capability must have checked
  880.        * to prevent this T-bit enabled router be my neighbor.
  881.        */
  882.       zlog_warn ("Packet[DD]: Neighbor %s: T-bit on?", inet_ntoa (nbr->router_id));
  883.       return;
  884.     }
  885. #endif /* REJECT_IF_TBIT_ON */
  886. #ifdef HAVE_OPAQUE_LSA
  887.   if (CHECK_FLAG (dd->options, OSPF_OPTION_O)
  888.       && !CHECK_FLAG (oi->ospf->config, OSPF_OPAQUE_CAPABLE))
  889.     {
  890.       /*
  891.        * This node is not configured to handle O-bit, for now.
  892.        * Clear it to ignore unsupported capability proposed by neighbor.
  893.        */
  894.       UNSET_FLAG (dd->options, OSPF_OPTION_O);
  895.     }
  896. #endif /* HAVE_OPAQUE_LSA */
  897.   /* Process DD packet by neighbor status. */
  898.   switch (nbr->state)
  899.     {
  900.     case NSM_Down:
  901.     case NSM_Attempt:
  902.     case NSM_TwoWay:
  903.       zlog_warn ("Packet[DD]: Neighbor state is %s, packet discarded.",
  904.  LOOKUP (ospf_nsm_state_msg, nbr->state));
  905.       break;
  906.     case NSM_Init:
  907.       OSPF_NSM_EVENT_EXECUTE (nbr, NSM_TwoWayReceived);
  908.       /* If the new state is ExStart, the processing of the current
  909.  packet should then continue in this new state by falling
  910.  through to case ExStart below.  */
  911.       if (nbr->state != NSM_ExStart)
  912. break;
  913.     case NSM_ExStart:
  914.       /* Initial DBD */
  915.       if ((IS_SET_DD_ALL (dd->flags) == OSPF_DD_FLAG_ALL) &&
  916.   (size == OSPF_DB_DESC_MIN_SIZE))
  917. {
  918.   if (IPV4_ADDR_CMP (&nbr->router_id, &oi->ospf->router_id) > 0)
  919.     {
  920.       /* We're Slave---obey */
  921.       zlog_warn ("Packet[DD]: Negotiation done (Slave).");
  922.       nbr->dd_seqnum = ntohl (dd->dd_seqnum);
  923.       nbr->dd_flags &= ~(OSPF_DD_FLAG_MS|OSPF_DD_FLAG_I); /* Reset I/MS */
  924.     }
  925.   else
  926.     {
  927.       /* We're Master, ignore the initial DBD from Slave */
  928.       zlog_warn ("Packet[DD]: Initial DBD from Slave, ignoring.");
  929.       break;
  930.     }
  931. }
  932.       /* Ack from the Slave */
  933.       else if (!IS_SET_DD_MS (dd->flags) && !IS_SET_DD_I (dd->flags) &&
  934.        ntohl (dd->dd_seqnum) == nbr->dd_seqnum &&
  935.        IPV4_ADDR_CMP (&nbr->router_id, &oi->ospf->router_id) < 0)
  936. {
  937.   zlog_warn ("Packet[DD]: Negotiation done (Master).");
  938.   nbr->dd_flags &= ~OSPF_DD_FLAG_I;
  939. }
  940.       else
  941. {
  942.   zlog_warn ("Packet[DD]: Negotiation fails.");
  943.   break;
  944. }
  945.       
  946.       /* This is where the real Options are saved */
  947.       nbr->options = dd->options;
  948. #ifdef HAVE_OPAQUE_LSA
  949.       if (CHECK_FLAG (oi->ospf->config, OSPF_OPAQUE_CAPABLE))
  950.         {
  951.           if (IS_DEBUG_OSPF_EVENT)
  952.             zlog_info ("Neighbor[%s] is %sOpaque-capable.",
  953.        inet_ntoa (nbr->router_id),
  954.        CHECK_FLAG (nbr->options, OSPF_OPTION_O) ? "" : "NOT ");
  955.           if (! CHECK_FLAG (nbr->options, OSPF_OPTION_O)
  956.       &&  IPV4_ADDR_SAME (&DR (oi), &nbr->address.u.prefix4))
  957.             {
  958.               zlog_warn ("DR-neighbor[%s] is NOT opaque-capable; Opaque-LSAs cannot be reliably advertised in this network.", inet_ntoa (nbr->router_id));
  959.               /* This situation is undesirable, but not a real error. */
  960.             }
  961.         }
  962. #endif /* HAVE_OPAQUE_LSA */
  963.       OSPF_NSM_EVENT_EXECUTE (nbr, NSM_NegotiationDone);
  964.       /* continue processing rest of packet. */
  965.       ospf_db_desc_proc (s, oi, nbr, dd, size);
  966.       break;
  967.     case NSM_Exchange:
  968.       if (ospf_db_desc_is_dup (dd, nbr))
  969. {
  970.   if (IS_SET_DD_MS (nbr->dd_flags))
  971.     /* Master: discard duplicated DD packet. */
  972.     zlog_warn ("Packet[DD] (Master): packet duplicated.");
  973.   else
  974.     /* Slave: cause to retransmit the last Database Description. */
  975.     {
  976.       zlog_warn ("Packet[DD] [Slave]: packet duplicated.");
  977.       ospf_db_desc_resend (nbr);
  978.     }
  979.   break;
  980. }
  981.       /* Otherwise DD packet should be checked. */
  982.       /* Check Master/Slave bit mismatch */
  983.       if (IS_SET_DD_MS (dd->flags) != IS_SET_DD_MS (nbr->last_recv.flags))
  984. {
  985.   zlog_warn ("Packet[DD]: MS-bit mismatch.");
  986.   OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_SeqNumberMismatch);
  987.   if (IS_DEBUG_OSPF_EVENT)
  988.     zlog_info ("Packet[DD]: dd->flags=%d, nbr->dd_flags=%d",
  989.        dd->flags, nbr->dd_flags);
  990.   break;
  991. }
  992.       /* Check initialize bit is set. */
  993.       if (IS_SET_DD_I (dd->flags))
  994. {
  995.   zlog_warn ("Packet[DD]: I-bit set.");
  996.   OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_SeqNumberMismatch);
  997.   break;
  998. }
  999.       /* Check DD Options. */
  1000.       if (dd->options != nbr->options)
  1001. {
  1002.   zlog_warn ("Packet[DD]: options mismatch.");
  1003.   OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_SeqNumberMismatch);
  1004.   break;
  1005. }
  1006.       /* Check DD sequence number. */
  1007.       if ((IS_SET_DD_MS (nbr->dd_flags) &&
  1008.    ntohl (dd->dd_seqnum) != nbr->dd_seqnum) ||
  1009.   (!IS_SET_DD_MS (nbr->dd_flags) &&
  1010.    ntohl (dd->dd_seqnum) != nbr->dd_seqnum + 1))
  1011. {
  1012.   zlog_warn ("Pakcet[DD]: sequence number mismatch.");
  1013.   OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_SeqNumberMismatch);
  1014.   break;
  1015. }
  1016.       /* Continue processing rest of packet. */
  1017.       ospf_db_desc_proc (s, oi, nbr, dd, size);
  1018.       break;
  1019.     case NSM_Loading:
  1020.     case NSM_Full:
  1021.       if (ospf_db_desc_is_dup (dd, nbr))
  1022. {
  1023.   if (IS_SET_DD_MS (nbr->dd_flags))
  1024.     {
  1025.       /* Master should discard duplicate DD packet. */
  1026.       zlog_warn ("Pakcet[DD]: duplicated, packet discarded.");
  1027.       break;
  1028.     }
  1029.   else
  1030.     {
  1031.       struct timeval t, now;
  1032.       gettimeofday (&now, NULL);
  1033.       t = tv_sub (now, nbr->last_send_ts);
  1034.       if (tv_cmp (t, int2tv (nbr->v_inactivity)) < 0)
  1035. {
  1036.   /* In states Loading and Full the slave must resend
  1037.      its last Database Description packet in response to
  1038.      duplicate Database Description packets received
  1039.      from the master.  For this reason the slave must
  1040.      wait RouterDeadInterval seconds before freeing the
  1041.      last Database Description packet.  Reception of a
  1042.      Database Description packet from the master after
  1043.      this interval will generate a SeqNumberMismatch
  1044.      neighbor event. RFC2328 Section 10.8 */
  1045.   ospf_db_desc_resend (nbr);
  1046.   break;
  1047. }
  1048.     }
  1049. }
  1050.       OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_SeqNumberMismatch);
  1051.       break;
  1052.     default:
  1053.       zlog_warn ("Packet[DD]: NSM illegal status.");
  1054.       break;
  1055.     }
  1056. }
  1057. #define OSPF_LSA_KEY_SIZE       12 /* type(4) + id(4) + ar(4) */
  1058. /* OSPF Link State Request Read -- RFC2328 Section 10.7. */
  1059. void
  1060. ospf_ls_req (struct ip *iph, struct ospf_header *ospfh,
  1061.      struct stream *s, struct ospf_interface *oi, u_int16_t size)
  1062. {
  1063.   struct ospf_neighbor *nbr;
  1064.   u_int32_t ls_type;
  1065.   struct in_addr ls_id;
  1066.   struct in_addr adv_router;
  1067.   struct ospf_lsa *find;
  1068.   list ls_upd;
  1069.   int length;
  1070.   /* Increment statistics. */
  1071.   oi->ls_req_in++;
  1072.   nbr = ospf_nbr_lookup_by_addr (oi->nbrs, &iph->ip_src);
  1073.   if (nbr == NULL)
  1074.     {
  1075.       zlog_warn ("Link State Request: Unknown Neighbor %s.",
  1076.  inet_ntoa (ospfh->router_id));
  1077.       return;
  1078.     }
  1079.   /* Neighbor State should be Exchange or later. */
  1080.   if (nbr->state != NSM_Exchange &&
  1081.       nbr->state != NSM_Loading &&
  1082.       nbr->state != NSM_Full)
  1083.     {
  1084.       zlog_warn ("Link State Request: Neighbor state is %s, packet discarded.",
  1085.  LOOKUP (ospf_nsm_state_msg, nbr->state));
  1086.       return;
  1087.     }
  1088.   /* Send Link State Update for ALL requested LSAs. */
  1089.   ls_upd = list_new ();
  1090.   length = OSPF_HEADER_SIZE + OSPF_LS_UPD_MIN_SIZE;
  1091.   while (size >= OSPF_LSA_KEY_SIZE)
  1092.     {
  1093.       /* Get one slice of Link State Request. */
  1094.       ls_type = stream_getl (s);
  1095.       ls_id.s_addr = stream_get_ipv4 (s);
  1096.       adv_router.s_addr = stream_get_ipv4 (s);
  1097.       /* Verify LSA type. */
  1098.       if (ls_type < OSPF_MIN_LSA || ls_type >= OSPF_MAX_LSA)
  1099. {
  1100.   OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_BadLSReq);
  1101.   list_delete (ls_upd);
  1102.   return;
  1103. }
  1104.       /* Search proper LSA in LSDB. */
  1105.       find = ospf_lsa_lookup (oi->area, ls_type, ls_id, adv_router);
  1106.       if (find == NULL)
  1107. {
  1108.   OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_BadLSReq);
  1109.   list_delete (ls_upd);
  1110.   return;
  1111. }
  1112.       /* Packet overflows MTU size, send immediatly. */
  1113.       if (length + ntohs (find->data->length) > OSPF_PACKET_MAX (oi))
  1114. {
  1115.   if (oi->type == OSPF_IFTYPE_NBMA)
  1116.     ospf_ls_upd_send (nbr, ls_upd, OSPF_SEND_PACKET_DIRECT);
  1117.   else
  1118.     ospf_ls_upd_send (nbr, ls_upd, OSPF_SEND_PACKET_INDIRECT);
  1119.   /* Only remove list contents.  Keep ls_upd. */
  1120.   list_delete_all_node (ls_upd);
  1121.   length = OSPF_HEADER_SIZE + OSPF_LS_UPD_MIN_SIZE;
  1122. }
  1123.       /* Append LSA to update list. */
  1124.       listnode_add (ls_upd, find);
  1125.       length += ntohs (find->data->length);
  1126.       size -= OSPF_LSA_KEY_SIZE;
  1127.     }
  1128.   /* Send rest of Link State Update. */
  1129.   if (listcount (ls_upd) > 0)
  1130.     {
  1131.       if (oi->type == OSPF_IFTYPE_NBMA)
  1132. ospf_ls_upd_send (nbr, ls_upd, OSPF_SEND_PACKET_DIRECT);
  1133.       else
  1134. ospf_ls_upd_send (nbr, ls_upd, OSPF_SEND_PACKET_INDIRECT);
  1135.       list_delete (ls_upd);
  1136.     }
  1137.   else
  1138.     list_free (ls_upd);
  1139. }
  1140. /* Get the list of LSAs from Link State Update packet.
  1141.    And process some validation -- RFC2328 Section 13. (1)-(2). */
  1142. static list
  1143. ospf_ls_upd_list_lsa (struct ospf_neighbor *nbr, struct stream *s,
  1144.                       struct ospf_interface *oi, size_t size)
  1145. {
  1146.   u_int16_t count, sum;
  1147.   u_int32_t length;
  1148.   struct lsa_header *lsah;
  1149.   struct ospf_lsa *lsa;
  1150.   list lsas;
  1151.   lsas = list_new ();
  1152.   count = stream_getl (s);
  1153.   size -= OSPF_LS_UPD_MIN_SIZE; /* # LSAs */
  1154.   for (; size >= OSPF_LSA_HEADER_SIZE && count > 0;
  1155.        size -= length, stream_forward (s, length), count--)
  1156.     {
  1157.       lsah = (struct lsa_header *) STREAM_PNT (s);
  1158.       length = ntohs (lsah->length);
  1159.       if (length > size)
  1160. {
  1161.   zlog_warn ("Link State Update: LSA length exceeds packet size.");
  1162.   break;
  1163. }
  1164.       /* Validate the LSA's LS checksum. */
  1165.       sum = lsah->checksum;
  1166.       if (sum != ospf_lsa_checksum (lsah))
  1167. {
  1168.   zlog_warn ("Link State Update: LSA checksum error %x, %x.",
  1169.      sum, lsah->checksum);
  1170.   continue;
  1171. }
  1172.       /* Examine the LSA's LS type. */
  1173.       if (lsah->type < OSPF_MIN_LSA || lsah->type >= OSPF_MAX_LSA)
  1174. {
  1175.   zlog_warn ("Link State Update: Unknown LS type %d", lsah->type);
  1176.   continue;
  1177. }
  1178.       /*
  1179.        * What if the received LSA's age is greater than MaxAge?
  1180.        * Treat it as a MaxAge case -- endo.
  1181.        */
  1182.       if (ntohs (lsah->ls_age) > OSPF_LSA_MAXAGE)
  1183.         lsah->ls_age = htons (OSPF_LSA_MAXAGE);
  1184. #ifdef HAVE_OPAQUE_LSA
  1185.       if (CHECK_FLAG (nbr->options, OSPF_OPTION_O))
  1186.         {
  1187. #ifdef STRICT_OBIT_USAGE_CHECK
  1188.   if ((IS_OPAQUE_LSA(lsah->type) &&
  1189.                ! CHECK_FLAG (lsah->options, OSPF_OPTION_O))
  1190.       ||  (! IS_OPAQUE_LSA(lsah->type) &&
  1191.    CHECK_FLAG (lsah->options, OSPF_OPTION_O)))
  1192.             {
  1193.               /*
  1194.                * This neighbor must know the exact usage of O-bit;
  1195.                * the bit will be set in Type-9,10,11 LSAs only.
  1196.                */
  1197.               zlog_warn ("LSA[Type%d:%s]: O-bit abuse?", lsah->type, inet_ntoa (lsah->id));
  1198.               continue;
  1199.             }
  1200. #endif /* STRICT_OBIT_USAGE_CHECK */
  1201.           /* Do not take in AS External Opaque-LSAs if we are a stub. */
  1202.           if (lsah->type == OSPF_OPAQUE_AS_LSA
  1203.       && nbr->oi->area->external_routing != OSPF_AREA_DEFAULT) 
  1204.             {
  1205.               if (IS_DEBUG_OSPF_EVENT)
  1206.                 zlog_info ("LSA[Type%d:%s]: We are a stub, don't take this LSA.", lsah->type, inet_ntoa (lsah->id));
  1207.               continue;
  1208.             }
  1209.         }
  1210.       else if (IS_OPAQUE_LSA(lsah->type))
  1211.         {
  1212.           zlog_warn ("LSA[Type%d:%s]: Opaque capability mismatch?", lsah->type, inet_ntoa (lsah->id));
  1213.           continue;
  1214.         }
  1215. #endif /* HAVE_OPAQUE_LSA */
  1216.       /* Create OSPF LSA instance. */
  1217.       lsa = ospf_lsa_new ();
  1218.       /* We may wish to put some error checking if type NSSA comes in
  1219.          and area not in NSSA mode */
  1220.       switch (lsah->type)
  1221.         {
  1222.         case OSPF_AS_EXTERNAL_LSA:
  1223. #ifdef HAVE_OPAQUE_LSA
  1224.         case OSPF_OPAQUE_AS_LSA:
  1225.           lsa->area = NULL;
  1226.           break;
  1227.         case OSPF_OPAQUE_LINK_LSA:
  1228.           lsa->oi = oi; /* Remember incoming interface for flooding control. */
  1229.           /* Fallthrough */
  1230. #endif /* HAVE_OPAQUE_LSA */
  1231.         default:
  1232.           lsa->area = oi->area;
  1233.           break;
  1234.         }
  1235.       lsa->data = ospf_lsa_data_new (length);
  1236.       memcpy (lsa->data, lsah, length);
  1237.       if (IS_DEBUG_OSPF_EVENT)
  1238. zlog_info("LSA[Type%d:%s]: %p new LSA created with Link State Update",
  1239.   lsa->data->type, inet_ntoa (lsa->data->id), lsa);
  1240.       listnode_add (lsas, lsa);
  1241.     }
  1242.   return lsas;
  1243. }
  1244. /* Cleanup Update list. */
  1245. void
  1246. ospf_upd_list_clean (list lsas)
  1247. {
  1248.   listnode node;
  1249.   struct ospf_lsa *lsa;
  1250.   for (node = listhead (lsas); node; nextnode (node))
  1251.     if ((lsa = getdata (node)) != NULL)
  1252.       ospf_lsa_discard (lsa);
  1253.   list_delete (lsas);
  1254. }
  1255. /* OSPF Link State Update message read -- RFC2328 Section 13. */
  1256. void
  1257. ospf_ls_upd (struct ip *iph, struct ospf_header *ospfh,
  1258.      struct stream *s, struct ospf_interface *oi, u_int16_t size)
  1259. {
  1260.   struct ospf_neighbor *nbr;
  1261.   list lsas;
  1262. #ifdef HAVE_OPAQUE_LSA
  1263.   list mylsa_acks, mylsa_upds;
  1264. #endif /* HAVE_OPAQUE_LSA */
  1265.   listnode node, next;
  1266.   struct ospf_lsa *lsa = NULL;
  1267.   /* unsigned long ls_req_found = 0; */
  1268.   /* Dis-assemble the stream, update each entry, re-encapsulate for flooding */
  1269.   /* Increment statistics. */
  1270.   oi->ls_upd_in++;
  1271.   /* Check neighbor. */
  1272.   nbr = ospf_nbr_lookup_by_addr (oi->nbrs, &iph->ip_src);
  1273.   if (nbr == NULL)
  1274.     {
  1275.       zlog_warn ("Link State Update: Unknown Neighbor %s on int: %s",
  1276.  inet_ntoa (ospfh->router_id), IF_NAME (oi));
  1277.       return;
  1278.     }
  1279.   /* Check neighbor state. */
  1280.   if (nbr->state < NSM_Exchange)
  1281.     {
  1282.       zlog_warn ("Link State Update: Neighbor[%s] state is less than Exchange",
  1283.  inet_ntoa (ospfh->router_id));
  1284.       return;
  1285.     }
  1286.   /* Get list of LSAs from Link State Update packet. - Also perorms Stages 
  1287.    * 1 (validate LSA checksum) and 2 (check for LSA consistent type) 
  1288.    * of section 13. 
  1289.    */
  1290.   lsas = ospf_ls_upd_list_lsa (nbr, s, oi, size);
  1291. #ifdef HAVE_OPAQUE_LSA
  1292.   /*
  1293.    * Prepare two kinds of lists to clean up unwanted self-originated
  1294.    * Opaque-LSAs from the routing domain as soon as possible.
  1295.    */
  1296.   mylsa_acks = list_new (); /* Let the sender cease retransmission. */
  1297.   mylsa_upds = list_new (); /* Flush target LSAs if necessary. */
  1298.   /*
  1299.    * If self-originated Opaque-LSAs that have flooded before restart
  1300.    * are contained in the received LSUpd message, corresponding LSReq
  1301.    * messages to be sent may have to be modified.
  1302.    * To eliminate possible race conditions such that flushing and normal
  1303.    * updating for the same LSA would take place alternately, this trick
  1304.    * must be done before entering to the loop below.
  1305.    */
  1306.   ospf_opaque_adjust_lsreq (nbr, lsas);
  1307. #endif /* HAVE_OPAQUE_LSA */
  1308. #define DISCARD_LSA(L,N) {
  1309.         if (IS_DEBUG_OSPF_EVENT) 
  1310.           zlog_info ("ospf_lsa_discard() in ospf_ls_upd() point %d: lsa %p Type-%d", N, lsa, (int) lsa->data->type); 
  1311.         ospf_lsa_discard (L); 
  1312. continue; }
  1313.   /* Process each LSA received in the one packet. */
  1314.   for (node = listhead (lsas); node; node = next)
  1315.     {
  1316.       struct ospf_lsa *ls_ret, *current;
  1317.       int ret = 1;
  1318.       next = node->next;
  1319.       lsa = getdata (node);
  1320. #ifdef HAVE_NSSA
  1321.       if (IS_DEBUG_OSPF_NSSA)
  1322. {
  1323.   char buf1[INET_ADDRSTRLEN];
  1324.   char buf2[INET_ADDRSTRLEN];
  1325.   char buf3[INET_ADDRSTRLEN];
  1326.   zlog_info("LSA Type-%d from %s, ID: %s, ADV: %s",
  1327.     lsa->data->type,
  1328.     inet_ntop (AF_INET, &ospfh->router_id,
  1329.        buf1, INET_ADDRSTRLEN),
  1330.     inet_ntop (AF_INET, &lsa->data->id,
  1331.        buf2, INET_ADDRSTRLEN),
  1332.     inet_ntop (AF_INET, &lsa->data->adv_router,
  1333.        buf3, INET_ADDRSTRLEN));
  1334. }
  1335. #endif /* HAVE_NSSA */
  1336.       listnode_delete (lsas, lsa); /* We don't need it in list anymore */
  1337.       /* Validate Checksum - Done above by ospf_ls_upd_list_lsa() */
  1338.       /* LSA Type  - Done above by ospf_ls_upd_list_lsa() */
  1339.    
  1340.       /* Do not take in AS External LSAs if we are a stub or NSSA. */
  1341.       /* Do not take in AS NSSA if this neighbor and we are not NSSA */
  1342.       /* Do take in Type-7's if we are an NSSA  */ 
  1343.  
  1344.       /* If we are also an ABR, later translate them to a Type-5 packet */
  1345.  
  1346.       /* Later, an NSSA Re-fresh can Re-fresh Type-7's and an ABR will
  1347.  translate them to a separate Type-5 packet.  */
  1348.       if (lsa->data->type == OSPF_AS_EXTERNAL_LSA)
  1349.         /* Reject from STUB or NSSA */
  1350.         if (nbr->oi->area->external_routing != OSPF_AREA_DEFAULT) 
  1351.   {
  1352.     DISCARD_LSA (lsa, 1);
  1353. #ifdef HAVE_NSSA
  1354.     if (IS_DEBUG_OSPF_NSSA)
  1355.       zlog_info("Incoming External LSA Discarded: We are NSSA/STUB Area");
  1356. #endif /* HAVE_NSSA */
  1357.   }
  1358. #ifdef  HAVE_NSSA 
  1359.       if (lsa->data->type == OSPF_AS_NSSA_LSA)
  1360. if (nbr->oi->area->external_routing != OSPF_AREA_NSSA)
  1361.   {
  1362.     DISCARD_LSA (lsa,2);
  1363.     if (IS_DEBUG_OSPF_NSSA)
  1364.       zlog_info("Incoming NSSA LSA Discarded:  Not NSSA Area");
  1365.   }
  1366. #endif /* HAVE_NSSA */
  1367.       /* Find the LSA in the current database. */
  1368.       current = ospf_lsa_lookup_by_header (oi->area, lsa->data);
  1369.       /* If the LSA's LS age is equal to MaxAge, and there is currently
  1370.  no instance of the LSA in the router's link state database,
  1371.  and none of router's neighbors are in states Exchange or Loading,
  1372.  then take the following actions. */
  1373.       if (IS_LSA_MAXAGE (lsa) && !current &&
  1374.   (ospf_nbr_count (oi, NSM_Exchange) +
  1375.    ospf_nbr_count (oi, NSM_Loading)) == 0)
  1376. {
  1377.   /* Response Link State Acknowledgment. */
  1378.   ospf_ls_ack_send (nbr, lsa);
  1379.   /* Discard LSA. */   
  1380.   zlog_warn ("Link State Update: LS age is equal to MaxAge.");
  1381.           DISCARD_LSA (lsa, 3);
  1382. }
  1383. #ifdef HAVE_OPAQUE_LSA
  1384.       if (IS_OPAQUE_LSA (lsa->data->type)
  1385.   &&  IPV4_ADDR_SAME (&lsa->data->adv_router, &oi->ospf->router_id))
  1386.         {
  1387.           /*
  1388.            * Even if initial flushing seems to be completed, there might
  1389.            * be a case that self-originated LSA with MaxAge still remain
  1390.            * in the routing domain.
  1391.            * Just send an LSAck message to cease retransmission.
  1392.            */
  1393.           if (IS_LSA_MAXAGE (lsa))
  1394.             {
  1395.               zlog_warn ("LSA[%s]: Boomerang effect?", dump_lsa_key (lsa));
  1396.               ospf_ls_ack_send (nbr, lsa);
  1397.               ospf_lsa_discard (lsa);
  1398.               if (current != NULL && ! IS_LSA_MAXAGE (current))
  1399.                 ospf_opaque_lsa_refresh_schedule (current);
  1400.               continue;
  1401.             }
  1402.           /*
  1403.            * If an instance of self-originated Opaque-LSA is not found
  1404.            * in the LSDB, there are some possible cases here.
  1405.            *
  1406.            * 1) This node lost opaque-capability after restart.
  1407.            * 2) Else, a part of opaque-type is no more supported.
  1408.            * 3) Else, a part of opaque-id is no more supported.
  1409.            *
  1410.            * Anyway, it is still this node's responsibility to flush it.
  1411.            * Otherwise, the LSA instance remains in the routing domain
  1412.            * until its age reaches to MaxAge.
  1413.            */
  1414.           if (current == NULL)
  1415.             {
  1416.               if (IS_DEBUG_OSPF_EVENT)
  1417.                 zlog_info ("LSA[%s]: Previously originated Opaque-LSA, not found in the LSDB.", dump_lsa_key (lsa));
  1418.               SET_FLAG (lsa->flags, OSPF_LSA_SELF);
  1419.               listnode_add (mylsa_upds, ospf_lsa_dup  (lsa));
  1420.               listnode_add (mylsa_acks, ospf_lsa_lock (lsa));
  1421.               continue;
  1422.             }
  1423.         }
  1424. #endif /* HAVE_OPAQUE_LSA */
  1425.       /* (5) Find the instance of this LSA that is currently contained
  1426.  in the router's link state database.  If there is no
  1427.  database copy, or the received LSA is more recent than
  1428.  the database copy the following steps must be performed. */
  1429.       if (current == NULL ||
  1430.   (ret = ospf_lsa_more_recent (current, lsa)) < 0)
  1431. {
  1432.   /* Actual flooding procedure. */
  1433.   if (ospf_flood (oi->ospf, nbr, current, lsa) < 0)  /* Trap NSSA later. */
  1434.     DISCARD_LSA (lsa, 4);
  1435.   continue;
  1436. }
  1437.       /* (6) Else, If there is an instance of the LSA on the sending
  1438.  neighbor's Link state request list, an error has occurred in
  1439.  the Database Exchange process.  In this case, restart the
  1440.  Database Exchange process by generating the neighbor event
  1441.  BadLSReq for the sending neighbor and stop processing the
  1442.  Link State Update packet. */
  1443.       if (ospf_ls_request_lookup (nbr, lsa))
  1444. {
  1445.   OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_BadLSReq);
  1446.   zlog_warn ("LSA instance exists on Link state request list");
  1447.   /* Clean list of LSAs. */
  1448.           ospf_upd_list_clean (lsas);
  1449.   /* this lsa is not on lsas list already. */
  1450.   ospf_lsa_discard (lsa);
  1451. #ifdef HAVE_OPAQUE_LSA
  1452.           list_delete (mylsa_acks);
  1453.           list_delete (mylsa_upds);
  1454. #endif /* HAVE_OPAQUE_LSA */
  1455.   return;
  1456. }
  1457.       /* If the received LSA is the same instance as the database copy
  1458.  (i.e., neither one is more recent) the following two steps
  1459.  should be performed: */
  1460.       if (ret == 0)
  1461. {
  1462.   /* If the LSA is listed in the Link state retransmission list
  1463.      for the receiving adjacency, the router itself is expecting
  1464.      an acknowledgment for this LSA.  The router should treat the
  1465.      received LSA as an acknowledgment by removing the LSA from
  1466.      the Link state retransmission list.  This is termed an
  1467.      "implied acknowledgment". */
  1468.   ls_ret = ospf_ls_retransmit_lookup (nbr, lsa);
  1469.   if (ls_ret != NULL)
  1470.     {
  1471.       ospf_ls_retransmit_delete (nbr, ls_ret);
  1472.       /* Delayed acknowledgment sent if advertisement received
  1473.  from Designated Router, otherwise do nothing. */
  1474.       if (oi->state == ISM_Backup)
  1475. if (NBR_IS_DR (nbr))
  1476.   listnode_add (oi->ls_ack, ospf_lsa_lock (lsa));
  1477.               DISCARD_LSA (lsa, 5);
  1478.     }
  1479.   else
  1480.     /* Acknowledge the receipt of the LSA by sending a
  1481.        Link State Acknowledgment packet back out the receiving
  1482.        interface. */
  1483.     {
  1484.       ospf_ls_ack_send (nbr, lsa);
  1485.       DISCARD_LSA (lsa, 6);
  1486.     }
  1487. }
  1488.       /* The database copy is more recent.  If the database copy
  1489.  has LS age equal to MaxAge and LS sequence number equal to
  1490.  MaxSequenceNumber, simply discard the received LSA without
  1491.  acknowledging it. (In this case, the LSA's LS sequence number is
  1492.  wrapping, and the MaxSequenceNumber LSA must be completely
  1493.  flushed before any new LSA instance can be introduced). */
  1494.       else if (ret > 0)  /* Database copy is more recent */
  1495.   if (IS_LSA_MAXAGE (current) &&
  1496.       current->data->ls_seqnum == htonl (OSPF_MAX_SEQUENCE_NUMBER))
  1497.     {
  1498.       DISCARD_LSA (lsa, 7);
  1499.     }
  1500.   /* Otherwise, as long as the database copy has not been sent in a
  1501.      Link State Update within the last MinLSArrival seconds, send the
  1502.      database copy back to the sending neighbor, encapsulated within
  1503.      a Link State Update Packet. The Link State Update Packet should
  1504.      be sent directly to the neighbor. In so doing, do not put the
  1505.      database copy of the LSA on the neighbor's link state
  1506.      retransmission list, and do not acknowledge the received (less
  1507.      recent) LSA instance. */
  1508.   else
  1509.     {
  1510.       struct timeval now;
  1511.       
  1512.       gettimeofday (&now, NULL);
  1513.       
  1514.       if (tv_cmp (tv_sub (now, current->tv_orig), 
  1515.   int2tv (OSPF_MIN_LS_ARRIVAL)) > 0)
  1516. /* Trap NSSA type later.*/
  1517. ospf_ls_upd_send_lsa (nbr, current, OSPF_SEND_PACKET_DIRECT);
  1518.       DISCARD_LSA (lsa, 8);
  1519.     }
  1520. }
  1521.     }
  1522.   
  1523. #ifdef HAVE_OPAQUE_LSA
  1524.   /*
  1525.    * Now that previously originated Opaque-LSAs those which not yet
  1526.    * installed into LSDB are captured, take several steps to clear
  1527.    * them completely from the routing domain, before proceeding to
  1528.    * origination for the current target Opaque-LSAs.
  1529.    */
  1530.   while (listcount (mylsa_acks) > 0)
  1531.     ospf_ls_ack_send_list (oi, mylsa_acks, nbr->address.u.prefix4);
  1532.   if (listcount (mylsa_upds) > 0)
  1533.     ospf_opaque_self_originated_lsa_received (nbr, mylsa_upds);
  1534.   list_delete (mylsa_acks);
  1535.   list_delete (mylsa_upds);
  1536. #endif /* HAVE_OPAQUE_LSA */
  1537.   assert (listcount (lsas) == 0);
  1538.   list_delete (lsas);
  1539. }
  1540. /* OSPF Link State Acknowledgment message read -- RFC2328 Section 13.7. */
  1541. void
  1542. ospf_ls_ack (struct ip *iph, struct ospf_header *ospfh,
  1543.      struct stream *s, struct ospf_interface *oi, u_int16_t size)
  1544. {
  1545.   struct ospf_neighbor *nbr;
  1546. #ifdef HAVE_OPAQUE_LSA
  1547.   list opaque_acks;
  1548. #endif /* HAVE_OPAQUE_LSA */
  1549.   /* increment statistics. */
  1550.   oi->ls_ack_in++;
  1551.   nbr = ospf_nbr_lookup_by_addr (oi->nbrs, &iph->ip_src);
  1552.   if (nbr == NULL)
  1553.     {
  1554.       zlog_warn ("Link State Acknowledgment: Unknown Neighbor %s.",
  1555.  inet_ntoa (ospfh->router_id));
  1556.       return;
  1557.     }
  1558.   if (nbr->state < NSM_Exchange)
  1559.     {
  1560.       zlog_warn ("Link State Acknowledgment: State is less than Exchange.");
  1561.       return;
  1562.     }
  1563. #ifdef HAVE_OPAQUE_LSA
  1564.   opaque_acks = list_new ();
  1565. #endif /* HAVE_OPAQUE_LSA */
  1566.   while (size >= OSPF_LSA_HEADER_SIZE)
  1567.     {
  1568.       struct ospf_lsa *lsa, *lsr;
  1569.       lsa = ospf_lsa_new ();
  1570.       lsa->data = (struct lsa_header *) STREAM_PNT (s);
  1571.       /* lsah = (struct lsa_header *) STREAM_PNT (s); */
  1572.       size -= OSPF_LSA_HEADER_SIZE;
  1573.       stream_forward (s, OSPF_LSA_HEADER_SIZE);
  1574.       if (lsa->data->type < OSPF_MIN_LSA || lsa->data->type >= OSPF_MAX_LSA)
  1575. {
  1576.   lsa->data = NULL;
  1577.   ospf_lsa_discard (lsa);
  1578.   continue;
  1579. }
  1580.       lsr = ospf_ls_retransmit_lookup (nbr, lsa);
  1581.       if (lsr != NULL && lsr->data->ls_seqnum == lsa->data->ls_seqnum)
  1582.         {
  1583. #ifdef HAVE_OPAQUE_LSA
  1584.           /* Keep this LSA entry for later reference. */
  1585.           if (IS_OPAQUE_LSA (lsr->data->type))
  1586.             listnode_add (opaque_acks, ospf_lsa_dup (lsr));
  1587. #endif /* HAVE_OPAQUE_LSA */
  1588.           ospf_ls_retransmit_delete (nbr, lsr);
  1589.         }
  1590.       lsa->data = NULL;
  1591.       ospf_lsa_discard (lsa);
  1592.     }
  1593. #ifdef HAVE_OPAQUE_LSA
  1594.   if (listcount (opaque_acks) > 0)
  1595.     ospf_opaque_ls_ack_received (nbr, opaque_acks);
  1596.   list_delete (opaque_acks);
  1597.   return;
  1598. #endif /* HAVE_OPAQUE_LSA */
  1599. }
  1600. struct stream *
  1601. ospf_recv_packet (int fd, struct interface **ifp)
  1602. {
  1603.   int ret;
  1604.   struct ip iph;
  1605.   u_int16_t ip_len;
  1606.   struct stream *ibuf;
  1607.   unsigned int ifindex = 0;
  1608.   struct iovec iov;
  1609.   struct cmsghdr *cmsg;
  1610. #if defined (IP_PKTINFO)
  1611.   struct in_pktinfo *pktinfo;
  1612. #elif defined (IP_RECVIF)
  1613.   struct sockaddr_dl *pktinfo;
  1614. #else
  1615.   char *pktinfo; /* dummy */
  1616. #endif
  1617.   char buff [sizeof (*cmsg) + sizeof (*pktinfo)];
  1618.   struct msghdr msgh = {NULL, 0, &iov, 1, buff,
  1619. sizeof (*cmsg) + sizeof (*pktinfo), 0};
  1620.     
  1621.   ret = recvfrom (fd, (void *)&iph, sizeof (iph), MSG_PEEK, NULL, 0);
  1622.   
  1623.   if (ret != sizeof (iph))
  1624.     {
  1625.       zlog_warn ("ospf_recv_packet packet smaller than ip header");
  1626.       return NULL;
  1627.     }
  1628. #if defined(__NetBSD__) || defined(__FreeBSD__) || defined(OpenBSD_IP_LEN)
  1629.   ip_len = iph.ip_len;
  1630. #else
  1631.   ip_len = ntohs (iph.ip_len);
  1632. #endif
  1633. #if !defined(GNU_LINUX) && !defined(OpenBSD_IP_LEN)
  1634.   /*
  1635.    * Kernel network code touches incoming IP header parameters,
  1636.    * before protocol specific processing.
  1637.    *
  1638.    *   1) Convert byteorder to host representation.
  1639.    *      --> ip_len, ip_id, ip_off
  1640.    *
  1641.    *   2) Adjust ip_len to strip IP header size!
  1642.    *      --> If user process receives entire IP packet via RAW
  1643.    *          socket, it must consider adding IP header size to
  1644.    *          the "ip_len" field of "ip" structure.
  1645.    *
  1646.    * For more details, see <netinet/ip_input.c>.
  1647.    */
  1648.   ip_len = ip_len + (iph.ip_hl << 2);
  1649. #endif
  1650.   
  1651.   ibuf = stream_new (ip_len);
  1652.   iov.iov_base = STREAM_DATA (ibuf);
  1653.   iov.iov_len = ip_len;
  1654.   ret = recvmsg (fd, &msgh, 0);
  1655.   
  1656.   cmsg = CMSG_FIRSTHDR (&msgh);
  1657.   
  1658.   if (cmsg != NULL && //cmsg->cmsg_len == sizeof (*pktinfo) &&
  1659.       cmsg->cmsg_level == IPPROTO_IP &&
  1660. #if defined (IP_PKTINFO)
  1661.       cmsg->cmsg_type == IP_PKTINFO
  1662. #elif defined (IP_RECVIF)
  1663.       cmsg->cmsg_type == IP_RECVIF
  1664. #else
  1665.       0
  1666. #endif
  1667.       )
  1668.     {
  1669. #if defined (IP_PKTINFO)
  1670.       pktinfo = (struct in_pktinfo *)CMSG_DATA(cmsg);
  1671.       ifindex = pktinfo->ipi_ifindex;
  1672. #elif defined (IP_RECVIF)
  1673.       pktinfo = (struct sockaddr_dl *)CMSG_DATA(cmsg);
  1674.       ifindex = pktinfo->sdl_index;
  1675. #else
  1676.       ifindex = 0;
  1677. #endif
  1678.     }
  1679.   
  1680.   *ifp = if_lookup_by_index (ifindex);
  1681.   if (ret != ip_len)
  1682.     {
  1683.       zlog_warn ("ospf_recv_packet short read. "
  1684.  "ip_len %d bytes read %d", ip_len, ret);
  1685.       stream_free (ibuf);
  1686.       return NULL;
  1687.     }
  1688.   
  1689.   return ibuf;
  1690. }
  1691. struct ospf_interface *
  1692. ospf_associate_packet_vl (struct ospf *ospf,
  1693.   struct interface *ifp, struct ospf_interface *oi,
  1694.   struct ip *iph, struct ospf_header *ospfh)
  1695. {
  1696.   struct ospf_interface *rcv_oi;
  1697.   struct ospf_vl_data *vl_data;
  1698.   struct ospf_area *vl_area;
  1699.   listnode node;
  1700.   if (IN_MULTICAST (ntohl (iph->ip_dst.s_addr)) ||
  1701.       !OSPF_IS_AREA_BACKBONE (ospfh))
  1702.     return oi;
  1703.   if ((rcv_oi = oi) == NULL)
  1704.     {
  1705.       if ((rcv_oi = ospf_if_lookup_by_local_addr (ospf, ifp,
  1706.   iph->ip_dst)) == NULL)
  1707. return NULL;
  1708.     }
  1709.   for (node = listhead (ospf->vlinks); node; nextnode (node))
  1710.     {
  1711.       if ((vl_data = getdata (node)) == NULL)
  1712. continue;
  1713.       
  1714.       vl_area = ospf_area_lookup_by_area_id (ospf, vl_data->vl_area_id);
  1715.       if (!vl_area)
  1716. continue;
  1717.       
  1718.       if (OSPF_AREA_SAME (&vl_area, &rcv_oi->area) &&
  1719.   IPV4_ADDR_SAME (&vl_data->vl_peer, &ospfh->router_id))
  1720. {
  1721.   if (IS_DEBUG_OSPF_EVENT)
  1722.     zlog_info ("associating packet with %s",
  1723.        IF_NAME (vl_data->vl_oi));
  1724.   if (! CHECK_FLAG (vl_data->vl_oi->ifp->flags, IFF_UP))
  1725.     {
  1726.       if (IS_DEBUG_OSPF_EVENT)
  1727. zlog_info ("This VL is not up yet, sorry");
  1728.       return NULL;
  1729.     }
  1730.   
  1731.   return vl_data->vl_oi;
  1732. }
  1733.     }
  1734.   if (IS_DEBUG_OSPF_EVENT)
  1735.     zlog_info ("couldn't find any VL to associate the packet with");
  1736.   
  1737.   return oi;
  1738. }
  1739. int
  1740. ospf_check_area_id (struct ospf_interface *oi, struct ospf_header *ospfh)
  1741. {
  1742.   /* Check match the Area ID of the receiving interface. */
  1743.   if (OSPF_AREA_SAME (&oi->area, &ospfh))
  1744.     return 1;
  1745.   return 0;
  1746. }
  1747. /* Unbound socket will accept any Raw IP packets if proto is matched.
  1748.    To prevent it, compare src IP address and i/f address with masking
  1749.    i/f network mask. */
  1750. int
  1751. ospf_check_network_mask (struct ospf_interface *oi, struct in_addr ip_src)
  1752. {
  1753.   struct in_addr mask, me, him;
  1754.   if (oi->type == OSPF_IFTYPE_POINTOPOINT ||
  1755.       oi->type == OSPF_IFTYPE_VIRTUALLINK)
  1756.     return 1;
  1757.   masklen2ip (oi->address->prefixlen, &mask);
  1758.   me.s_addr = oi->address->u.prefix4.s_addr & mask.s_addr;
  1759.   him.s_addr = ip_src.s_addr & mask.s_addr;
  1760.   if (IPV4_ADDR_SAME (&me, &him))
  1761.     return 1;
  1762.   return 0;
  1763. }
  1764. int
  1765. ospf_check_auth (struct ospf_interface *oi, struct stream *ibuf,
  1766.  struct ospf_header *ospfh)
  1767. {
  1768.   int ret = 0;
  1769.   struct crypt_key *ck;
  1770.   list list;
  1771.   listnode node;
  1772.   switch (ntohs (ospfh->auth_type))
  1773.     {
  1774.     case OSPF_AUTH_NULL:
  1775.       ret = 1;
  1776.       break;
  1777.     case OSPF_AUTH_SIMPLE:
  1778.       if (!memcmp (OSPF_IF_PARAM (oi, auth_simple), ospfh->u.auth_data, OSPF_AUTH_SIMPLE_SIZE))
  1779. ret = 1;
  1780.       else
  1781. ret = 0;
  1782.       break;
  1783.     case OSPF_AUTH_CRYPTOGRAPHIC:
  1784.       ret = 0;
  1785.       list = OSPF_IF_PARAM (oi, auth_crypt);
  1786.       for (node = listhead (list); node; nextnode (node))
  1787.         {
  1788.           ck = getdata (node);
  1789.           if (ck == NULL)
  1790.             continue;
  1791.           /* This is very basic, the digest processing is elsewhere */
  1792.           if (ospfh->u.crypt.auth_data_len == OSPF_AUTH_MD5_SIZE && 
  1793.               ospfh->u.crypt.key_id == ck->key_id &&
  1794.               ntohs (ospfh->length) + OSPF_AUTH_SIMPLE_SIZE <=
  1795.                 stream_get_size (ibuf))
  1796.             {
  1797.               ret = 1;
  1798.               break;
  1799.             }
  1800.         }
  1801.       break;
  1802.     default:
  1803.       ret = 0;
  1804.       break;
  1805.     }
  1806.   return ret;
  1807. }
  1808. int
  1809. ospf_check_sum (struct ospf_header *ospfh)
  1810. {
  1811.   u_int32_t ret;
  1812.   u_int16_t sum;
  1813.   /* clear auth_data for checksum. */
  1814.   memset (ospfh->u.auth_data, 0, OSPF_AUTH_SIMPLE_SIZE);
  1815.   /* keep checksum and clear. */
  1816.   sum = ospfh->checksum;
  1817.   memset (&ospfh->checksum, 0, sizeof (u_int16_t));
  1818.   /* calculate checksum. */
  1819.   ret = in_cksum (ospfh, ntohs (ospfh->length));
  1820.   if (ret != sum)
  1821.     {
  1822.       zlog_info ("ospf_check_sum(): checksum mismatch, my %X, his %X",
  1823.  ret, sum);
  1824.       return 0;
  1825.     }
  1826.   return 1;
  1827. }
  1828. /* OSPF Header verification. */
  1829. int
  1830. ospf_verify_header (struct stream *ibuf, struct ospf_interface *oi,
  1831.     struct ip *iph, struct ospf_header *ospfh)
  1832. {
  1833.   /* check version. */
  1834.   if (ospfh->version != OSPF_VERSION)
  1835.     {
  1836.       zlog_warn ("interface %s: ospf_read version number mismatch.",
  1837.  IF_NAME (oi));
  1838.       return -1;
  1839.     }
  1840.   /* Check Area ID. */
  1841.   if (!ospf_check_area_id (oi, ospfh))
  1842.     {
  1843.       zlog_warn ("interface %s: ospf_read invalid Area ID %s.",
  1844.  IF_NAME (oi), inet_ntoa (ospfh->area_id));
  1845.       return -1;
  1846.     }
  1847.   /* Check network mask, Silently discarded. */
  1848.   if (! ospf_check_network_mask (oi, iph->ip_src))
  1849.     {
  1850.       zlog_warn ("interface %s: ospf_read network address is not same [%s]",
  1851.  IF_NAME (oi), inet_ntoa (iph->ip_src));
  1852.       return -1;
  1853.     }
  1854.   /* Check authentication. */
  1855.   if (ospf_auth_type (oi) != ntohs (ospfh->auth_type))
  1856.     {
  1857.       zlog_warn ("interface %s: ospf_read authentication type mismatch.",
  1858.  IF_NAME (oi));
  1859.       return -1;
  1860.     }
  1861.   if (! ospf_check_auth (oi, ibuf, ospfh))
  1862.     {
  1863.       zlog_warn ("interface %s: ospf_read authentication failed.",
  1864.  IF_NAME (oi));
  1865.       return -1;
  1866.     }
  1867.   /* if check sum is invalid, packet is discarded. */
  1868.   if (ntohs (ospfh->auth_type) != OSPF_AUTH_CRYPTOGRAPHIC)
  1869.     {
  1870.       if (! ospf_check_sum (ospfh))
  1871. {
  1872.   zlog_warn ("interface %s: ospf_read packet checksum error %s",
  1873.      IF_NAME (oi), inet_ntoa (ospfh->router_id));
  1874.   return -1;
  1875. }
  1876.     }
  1877.   else
  1878.     {
  1879.       if (ospfh->checksum != 0)
  1880. return -1;
  1881.       if (ospf_check_md5_digest (oi, ibuf, ntohs (ospfh->length)) == 0)
  1882. {
  1883.   zlog_warn ("interface %s: ospf_read md5 authentication failed.",
  1884.      IF_NAME (oi));
  1885.   return -1;
  1886. }
  1887.     }
  1888.   return 0;
  1889. }
  1890. /* Starting point of packet process function. */
  1891. int
  1892. ospf_read (struct thread *thread)
  1893. {
  1894.   int ret;
  1895.   struct stream *ibuf;
  1896.   struct ospf *ospf;
  1897.   struct ospf_interface *oi;
  1898.   struct ip *iph;
  1899.   struct ospf_header *ospfh;
  1900.   u_int16_t length;
  1901.   struct interface *ifp;
  1902.   /* first of all get interface pointer. */
  1903.   ospf = THREAD_ARG (thread);
  1904.   ospf->t_read = NULL;
  1905.   /* prepare for next packet. */
  1906.   ospf->t_read = thread_add_read (master, ospf_read, ospf, ospf->fd);
  1907.   /* read OSPF packet. */
  1908.   ibuf = ospf_recv_packet (ospf->fd, &ifp);
  1909.   if (ibuf == NULL)
  1910.     return -1;
  1911.   
  1912.   iph = (struct ip *) STREAM_DATA (ibuf);
  1913.   /* Self-originated packet should be discarded silently. */
  1914.   if (ospf_if_lookup_by_local_addr (ospf, NULL, iph->ip_src))
  1915.     {
  1916.       stream_free (ibuf);
  1917.       return 0;
  1918.     }
  1919.   /* Adjust size to message length. */
  1920.   stream_forward (ibuf, iph->ip_hl * 4);
  1921.   
  1922.   /* Get ospf packet header. */
  1923.   ospfh = (struct ospf_header *) STREAM_PNT (ibuf);
  1924.   /* associate packet with ospf interface */
  1925.   oi = ospf_if_lookup_recv_if (ospf, iph->ip_src);
  1926.   if (ifp && oi && oi->ifp != ifp)
  1927.     {
  1928.       zlog_warn ("Packet from [%s] received on wrong link %s",
  1929.  inet_ntoa (iph->ip_src), ifp->name); 
  1930.       stream_free (ibuf);
  1931.       return 0;
  1932.     }
  1933.   
  1934.   if ((oi = ospf_associate_packet_vl (ospf, ifp, oi, iph, ospfh)) == NULL)
  1935.     {
  1936.       stream_free (ibuf);
  1937.       return 0;
  1938.     }
  1939.   /*
  1940.    * If the received packet is destined for AllDRouters, the packet
  1941.    * should be accepted only if the received ospf interface state is
  1942.    * either DR or Backup -- endo.
  1943.    */
  1944.   if (iph->ip_dst.s_addr == htonl (OSPF_ALLDROUTERS)
  1945.       && (oi->state != ISM_DR && oi->state != ISM_Backup))
  1946.     {
  1947.       zlog_info ("Packet for AllDRouters from [%s] via [%s] (ISM: %s)",
  1948.                  inet_ntoa (iph->ip_src), IF_NAME (oi),
  1949.                  LOOKUP (ospf_ism_state_msg, oi->state));
  1950.       stream_free (ibuf);
  1951.       return 0;
  1952.     }
  1953.   /* Show debug receiving packet. */
  1954.   if (IS_DEBUG_OSPF_PACKET (ospfh->type - 1, RECV))
  1955.     {
  1956.       if (IS_DEBUG_OSPF_PACKET (ospfh->type - 1, DETAIL))
  1957. {
  1958.   zlog_info ("-----------------------------------------------------");
  1959.   ospf_packet_dump (ibuf);
  1960. }
  1961.       zlog_info ("%s received from [%s] via [%s]",
  1962.  ospf_packet_type_str[ospfh->type],
  1963.  inet_ntoa (ospfh->router_id), IF_NAME (oi));
  1964.       zlog_info (" src [%s],", inet_ntoa (iph->ip_src));
  1965.       zlog_info (" dst [%s]", inet_ntoa (iph->ip_dst));
  1966.       if (IS_DEBUG_OSPF_PACKET (ospfh->type - 1, DETAIL))
  1967. zlog_info ("-----------------------------------------------------");
  1968.     }
  1969.   /* Some header verification. */
  1970.   ret = ospf_verify_header (ibuf, oi, iph, ospfh);
  1971.   if (ret < 0)
  1972.     {
  1973.       stream_free (ibuf);
  1974.       return ret;
  1975.     }
  1976.   stream_forward (ibuf, OSPF_HEADER_SIZE);
  1977.   /* Adjust size to message length. */
  1978.   length = ntohs (ospfh->length) - OSPF_HEADER_SIZE;
  1979.   /* Read rest of the packet and call each sort of packet routine. */
  1980.   switch (ospfh->type)
  1981.     {
  1982.     case OSPF_MSG_HELLO:
  1983.       ospf_hello (iph, ospfh, ibuf, oi, length);
  1984.       break;
  1985.     case OSPF_MSG_DB_DESC:
  1986.       ospf_db_desc (iph, ospfh, ibuf, oi, length);
  1987.       break;
  1988.     case OSPF_MSG_LS_REQ:
  1989.       ospf_ls_req (iph, ospfh, ibuf, oi, length);
  1990.       break;
  1991.     case OSPF_MSG_LS_UPD:
  1992.       ospf_ls_upd (iph, ospfh, ibuf, oi, length);
  1993.       break;
  1994.     case OSPF_MSG_LS_ACK:
  1995.       ospf_ls_ack (iph, ospfh, ibuf, oi, length);
  1996.       break;
  1997.     default:
  1998.       zlog (NULL, LOG_WARNING,
  1999.     "interface %s: OSPF packet header type %d is illegal",
  2000.     IF_NAME (oi), ospfh->type);
  2001.       break;
  2002.     }
  2003.   stream_free (ibuf);
  2004.   return 0;
  2005. }
  2006. /* Make OSPF header. */
  2007. void
  2008. ospf_make_header (int type, struct ospf_interface *oi, struct stream *s)
  2009. {
  2010.   struct ospf_header *ospfh;
  2011.   ospfh = (struct ospf_header *) STREAM_DATA (s);
  2012.   ospfh->version = (u_char) OSPF_VERSION;
  2013.   ospfh->type = (u_char) type;
  2014.   ospfh->router_id = oi->ospf->router_id;
  2015.   ospfh->checksum = 0;
  2016.   ospfh->area_id = oi->area->area_id;
  2017.   ospfh->auth_type = htons (ospf_auth_type (oi));
  2018.   memset (ospfh->u.auth_data, 0, OSPF_AUTH_SIMPLE_SIZE);
  2019.   ospf_output_forward (s, OSPF_HEADER_SIZE);
  2020. }
  2021. /* Make Authentication Data. */
  2022. int
  2023. ospf_make_auth (struct ospf_interface *oi, struct ospf_header *ospfh)
  2024. {
  2025.   struct crypt_key *ck;
  2026.   switch (ospf_auth_type (oi))
  2027.     {
  2028.     case OSPF_AUTH_NULL:
  2029.       break;
  2030.     case OSPF_AUTH_SIMPLE:
  2031.       memcpy (ospfh->u.auth_data, OSPF_IF_PARAM (oi, auth_simple),
  2032.       OSPF_AUTH_SIMPLE_SIZE);
  2033.       break;
  2034.     case OSPF_AUTH_CRYPTOGRAPHIC:
  2035.       /* If key is not set, then set 0. */
  2036.       if (list_isempty (OSPF_IF_PARAM (oi, auth_crypt)))
  2037. {
  2038.   ospfh->u.crypt.zero = 0;
  2039.   ospfh->u.crypt.key_id = 0;
  2040.   ospfh->u.crypt.auth_data_len = OSPF_AUTH_MD5_SIZE;
  2041. }
  2042.       else
  2043. {
  2044.   ck = getdata (OSPF_IF_PARAM (oi, auth_crypt)->tail);
  2045.   ospfh->u.crypt.zero = 0;
  2046.   ospfh->u.crypt.key_id = ck->key_id;
  2047.   ospfh->u.crypt.auth_data_len = OSPF_AUTH_MD5_SIZE;
  2048. }
  2049.       /* note: the seq is done in ospf_make_md5_digest() */
  2050.       break;
  2051.     default:
  2052.       break;
  2053.     }
  2054.   return 0;
  2055. }
  2056. /* Fill rest of OSPF header. */
  2057. void
  2058. ospf_fill_header (struct ospf_interface *oi,
  2059.   struct stream *s, u_int16_t length)
  2060. {
  2061.   struct ospf_header *ospfh;
  2062.   ospfh = (struct ospf_header *) STREAM_DATA (s);
  2063.   /* Fill length. */
  2064.   ospfh->length = htons (length);
  2065.   /* Calculate checksum. */
  2066.   if (ntohs (ospfh->auth_type) != OSPF_AUTH_CRYPTOGRAPHIC)
  2067.     ospfh->checksum = in_cksum (ospfh, length);
  2068.   else
  2069.     ospfh->checksum = 0;
  2070.   /* Add Authentication Data. */
  2071.   ospf_make_auth (oi, ospfh);
  2072. }
  2073. int
  2074. ospf_make_hello (struct ospf_interface *oi, struct stream *s)
  2075. {
  2076.   struct ospf_neighbor *nbr;
  2077.   struct route_node *rn;
  2078.   u_int16_t length = OSPF_HELLO_MIN_SIZE;
  2079.   struct in_addr mask;
  2080.   unsigned long p;
  2081.   int flag = 0;
  2082.   /* Set netmask of interface. */
  2083.   if (oi->type != OSPF_IFTYPE_POINTOPOINT &&
  2084.       oi->type != OSPF_IFTYPE_VIRTUALLINK)
  2085.     masklen2ip (oi->address->prefixlen, &mask);
  2086.   else
  2087.     memset ((char *) &mask, 0, sizeof (struct in_addr));
  2088.   stream_put_ipv4 (s, mask.s_addr);
  2089.   /* Set Hello Interval. */
  2090.   stream_putw (s, OSPF_IF_PARAM (oi, v_hello));
  2091.   if (IS_DEBUG_OSPF_EVENT)
  2092.     zlog_info ("make_hello: options: %x, int: %s",
  2093.        OPTIONS(oi), IF_NAME (oi));
  2094.   /* Set Options. */
  2095.   stream_putc (s, OPTIONS (oi));
  2096.   /* Set Router Priority. */
  2097.   stream_putc (s, PRIORITY (oi));
  2098.   /* Set Router Dead Interval. */
  2099.   stream_putl (s, OSPF_IF_PARAM (oi, v_wait));
  2100.   /* Set Designated Router. */
  2101.   stream_put_ipv4 (s, DR (oi).s_addr);
  2102.   p = s->putp;
  2103.   /* Set Backup Designated Router. */
  2104.   stream_put_ipv4 (s, BDR (oi).s_addr);
  2105.   /* Add neighbor seen. */
  2106.   for (rn = route_top (oi->nbrs); rn; rn = route_next (rn))
  2107.     if ((nbr = rn->info))
  2108.       if (nbr->router_id.s_addr != 0) /* Ignore 0.0.0.0 node. */
  2109. if (nbr->state != NSM_Attempt)  /* Ignore Down neighbor. */
  2110.   if (nbr->state != NSM_Down)     /* This is myself for DR election. */
  2111.     if (!IPV4_ADDR_SAME (&nbr->router_id, &oi->ospf->router_id))
  2112.       {
  2113. /* Check neighbor is sane? */
  2114. if (nbr->d_router.s_addr != 0
  2115.     && IPV4_ADDR_SAME (&nbr->d_router, &oi->address->u.prefix4)
  2116.     && IPV4_ADDR_SAME (&nbr->bd_router, &oi->address->u.prefix4))
  2117.   flag = 1;
  2118. stream_put_ipv4 (s, nbr->router_id.s_addr);
  2119. length += 4;
  2120.       }
  2121.   /* Let neighbor generate BackupSeen. */
  2122.   if (flag == 1)
  2123.     {
  2124.       stream_set_putp (s, p);
  2125.       stream_put_ipv4 (s, 0);
  2126.     }
  2127.   return length;
  2128. }
  2129. int
  2130. ospf_make_db_desc (struct ospf_interface *oi, struct ospf_neighbor *nbr,
  2131.    struct stream *s)
  2132. {
  2133.   struct ospf_lsa *lsa;
  2134.   u_int16_t length = OSPF_DB_DESC_MIN_SIZE;
  2135.   u_char options;
  2136.   unsigned long pp;
  2137.   int i;
  2138.   struct ospf_lsdb *lsdb;
  2139.   
  2140.   /* Set Interface MTU. */
  2141.   if (oi->type == OSPF_IFTYPE_VIRTUALLINK)
  2142.     stream_putw (s, 0);
  2143.   else
  2144.     stream_putw (s, oi->ifp->mtu);
  2145.   /* Set Options. */
  2146.   options = OPTIONS (oi);
  2147. #ifdef HAVE_OPAQUE_LSA
  2148.   if (CHECK_FLAG (oi->ospf->config, OSPF_OPAQUE_CAPABLE))
  2149.     {
  2150.       if (IS_SET_DD_I (nbr->dd_flags)
  2151.   ||  CHECK_FLAG (nbr->options, OSPF_OPTION_O))
  2152.         /*
  2153.          * Set O-bit in the outgoing DD packet for capablity negotiation,
  2154.          * if one of following case is applicable. 
  2155.          *
  2156.          * 1) WaitTimer expiration event triggered the neighbor state to
  2157.          *    change to Exstart, but no (valid) DD packet has received
  2158.          *    from the neighbor yet.
  2159.          *
  2160.          * 2) At least one DD packet with O-bit on has received from the
  2161.          *    neighbor.
  2162.          */
  2163.         SET_FLAG (options, OSPF_OPTION_O);
  2164.     }
  2165. #endif /* HAVE_OPAQUE_LSA */
  2166.   stream_putc (s, options);
  2167.   /* Keep pointer to flags. */
  2168.   pp = stream_get_putp (s);
  2169.   stream_putc (s, nbr->dd_flags);
  2170.   /* Set DD Sequence Number. */
  2171.   stream_putl (s, nbr->dd_seqnum);
  2172.   if (ospf_db_summary_isempty (nbr))
  2173.     {
  2174.       if (nbr->state >= NSM_Exchange)
  2175. {
  2176.   nbr->dd_flags &= ~OSPF_DD_FLAG_M;
  2177.   /* Set DD flags again */
  2178.   stream_set_putp (s, pp);
  2179.   stream_putc (s, nbr->dd_flags);
  2180. }
  2181.       return length;
  2182.     }
  2183.   /* Describe LSA Header from Database Summary List. */
  2184.   lsdb = &nbr->db_sum;
  2185.   for (i = OSPF_MIN_LSA; i < OSPF_MAX_LSA; i++)
  2186.     {
  2187.       struct route_table *table = lsdb->type[i].db;
  2188.       struct route_node *rn;
  2189.       for (rn = route_top (table); rn; rn = route_next (rn))
  2190. if ((lsa = rn->info) != NULL)
  2191.   {
  2192. #ifdef HAVE_OPAQUE_LSA
  2193.             if (IS_OPAQUE_LSA (lsa->data->type)
  2194. && (! CHECK_FLAG (options, OSPF_OPTION_O)))
  2195.               {
  2196.                 /* Suppress advertising opaque-informations. */
  2197.                 /* Remove LSA from DB summary list. */
  2198.                 ospf_lsdb_delete (lsdb, lsa);
  2199.                 continue;
  2200.               }
  2201. #endif /* HAVE_OPAQUE_LSA */
  2202.     if (!CHECK_FLAG (lsa->flags, OSPF_LSA_DISCARD))
  2203.       {
  2204. struct lsa_header *lsah;
  2205. u_int16_t ls_age;
  2206. /* DD packet overflows interface MTU. */
  2207. if (length + OSPF_LSA_HEADER_SIZE > OSPF_PACKET_MAX (oi))
  2208.   break;
  2209. /* Keep pointer to LS age. */
  2210. lsah = (struct lsa_header *) (STREAM_DATA (s) +
  2211.       stream_get_putp (s));
  2212. /* Proceed stream pointer. */
  2213. stream_put (s, lsa->data, OSPF_LSA_HEADER_SIZE);
  2214. length += OSPF_LSA_HEADER_SIZE;
  2215. /* Set LS age. */
  2216. ls_age = LS_AGE (lsa);
  2217. lsah->ls_age = htons (ls_age);
  2218.       }
  2219.     
  2220.     /* Remove LSA from DB summary list. */
  2221.     ospf_lsdb_delete (lsdb, lsa);
  2222.   }
  2223.     }
  2224.   return length;
  2225. }
  2226. int
  2227. ospf_make_ls_req_func (struct stream *s, u_int16_t *length,
  2228.        unsigned long delta, struct ospf_neighbor *nbr,
  2229.        struct ospf_lsa *lsa)
  2230. {
  2231.   struct ospf_interface *oi;
  2232.   oi = nbr->oi;
  2233.   /* LS Request packet overflows interface MTU. */
  2234.   if (*length + delta > OSPF_PACKET_MAX(oi))
  2235.     return 0;
  2236.   stream_putl (s, lsa->data->type);
  2237.   stream_put_ipv4 (s, lsa->data->id.s_addr);
  2238.   stream_put_ipv4 (s, lsa->data->adv_router.s_addr);
  2239.   
  2240.   ospf_lsa_unlock (nbr->ls_req_last);
  2241.   nbr->ls_req_last = ospf_lsa_lock (lsa);
  2242.   
  2243.   *length += 12;
  2244.   return 1;
  2245. }
  2246. int
  2247. ospf_make_ls_req (struct ospf_neighbor *nbr, struct stream *s)
  2248. {
  2249.   struct ospf_lsa *lsa;
  2250.   u_int16_t length = OSPF_LS_REQ_MIN_SIZE;
  2251.   unsigned long delta = stream_get_putp(s)+12;
  2252.   struct route_table *table;
  2253.   struct route_node *rn;
  2254.   int i;
  2255.   struct ospf_lsdb *lsdb;
  2256.   lsdb = &nbr->ls_req;
  2257.   for (i = OSPF_MIN_LSA; i < OSPF_MAX_LSA; i++)
  2258.     {
  2259.       table = lsdb->type[i].db;
  2260.       for (rn = route_top (table); rn; rn = route_next (rn))
  2261. if ((lsa = (rn->info)) != NULL)
  2262.   if (ospf_make_ls_req_func (s, &length, delta, nbr, lsa) == 0)
  2263.     {
  2264.       route_unlock_node (rn);
  2265.       break;
  2266.     }
  2267.     }
  2268.   return length;
  2269. }
  2270. int
  2271. ls_age_increment (struct ospf_lsa *lsa, int delay)
  2272. {
  2273.   int age;
  2274.   age = IS_LSA_MAXAGE (lsa) ? OSPF_LSA_MAXAGE : LS_AGE (lsa) + delay;
  2275.   return (age > OSPF_LSA_MAXAGE ? OSPF_LSA_MAXAGE : age);
  2276. }
  2277. int
  2278. ospf_make_ls_upd (struct ospf_interface *oi, list update, struct stream *s)
  2279. {
  2280.   struct ospf_lsa *lsa;
  2281.   listnode node;
  2282.   u_int16_t length = OSPF_LS_UPD_MIN_SIZE;
  2283.   unsigned long delta = stream_get_putp (s);
  2284.   unsigned long pp;
  2285.   int count = 0;
  2286.   if (IS_DEBUG_OSPF_EVENT)
  2287.     zlog_info("ospf_make_ls_upd: Start");
  2288.   
  2289.   pp = stream_get_putp (s);
  2290.   ospf_output_forward (s, 4);
  2291.   while ((node = listhead (update)) != NULL)
  2292.     {
  2293.       struct lsa_header *lsah;
  2294.       u_int16_t ls_age;
  2295.       if (IS_DEBUG_OSPF_EVENT)
  2296. zlog_info("ospf_make_ls_upd: List Iteration");
  2297.       lsa = getdata (node);
  2298.       assert (lsa);
  2299.       assert (lsa->data);
  2300.       /* Check packet size. */
  2301.       if (length + delta + ntohs (lsa->data->length) > OSPF_PACKET_MAX (oi))
  2302. break;
  2303.       
  2304.       /* Keep pointer to LS age. */
  2305.       lsah = (struct lsa_header *) (STREAM_DATA (s) + stream_get_putp (s));
  2306.       /* Put LSA to Link State Request. */
  2307.       stream_put (s, lsa->data, ntohs (lsa->data->length));
  2308.       /* Set LS age. */
  2309.       /* each hop must increment an lsa_age by transmit_delay 
  2310.          of OSPF interface */
  2311.       ls_age = ls_age_increment (lsa, OSPF_IF_PARAM (oi, transmit_delay));
  2312.       lsah->ls_age = htons (ls_age);
  2313.       length += ntohs (lsa->data->length);
  2314.       count++;
  2315.       list_delete_node (update, node);
  2316.       ospf_lsa_unlock (lsa);
  2317.     }
  2318.   /* Now set #LSAs. */
  2319.   stream_set_putp (s, pp);
  2320.   stream_putl (s, count);
  2321.   stream_set_putp (s, s->endp);
  2322.   if (IS_DEBUG_OSPF_EVENT)
  2323.     zlog_info("ospf_make_ls_upd: Stop");
  2324.   return length;
  2325. }
  2326. int
  2327. ospf_make_ls_ack (struct ospf_interface *oi, list ack, struct stream *s)
  2328. {
  2329.   list rm_list;
  2330.   listnode node;
  2331.   u_int16_t length = OSPF_LS_ACK_MIN_SIZE;
  2332.   unsigned long delta = stream_get_putp(s) + 24;
  2333.   struct ospf_lsa *lsa;
  2334.   rm_list = list_new ();
  2335.   
  2336.   for (node = listhead (ack); node; nextnode (node))
  2337.     {
  2338.       lsa = getdata (node);
  2339.       assert (lsa);
  2340.       
  2341.       if (length + delta > OSPF_PACKET_MAX (oi))
  2342. break;
  2343.       
  2344.       stream_put (s, lsa->data, OSPF_LSA_HEADER_SIZE);
  2345.       length += OSPF_LSA_HEADER_SIZE;
  2346.       
  2347.       listnode_add (rm_list, lsa);
  2348.     }
  2349.   
  2350.   /* Remove LSA from LS-Ack list. */
  2351.   for (node = listhead (rm_list); node; nextnode (node))
  2352.     {
  2353.       lsa = (struct ospf_lsa *) getdata (node);
  2354.       
  2355.       listnode_delete (ack, lsa);
  2356.       ospf_lsa_unlock (lsa);
  2357.     }
  2358.   
  2359.   list_delete (rm_list);
  2360.   
  2361.   return length;
  2362. }
  2363. void
  2364. ospf_hello_send_sub (struct ospf_interface *oi, struct in_addr *addr)
  2365. {
  2366.   struct ospf_packet *op;
  2367.   u_int16_t length = OSPF_HEADER_SIZE;
  2368.   op = ospf_packet_new (oi->ifp->mtu);
  2369.   /* Prepare OSPF common header. */
  2370.   ospf_make_header (OSPF_MSG_HELLO, oi, op->s);
  2371.   /* Prepare OSPF Hello body. */
  2372.   length += ospf_make_hello (oi, op->s);
  2373.   /* Fill OSPF header. */
  2374.   ospf_fill_header (oi, op->s, length);
  2375.   /* Set packet length. */
  2376.   op->length = length;
  2377.   op->dst.s_addr = addr->s_addr;
  2378.   /* Add packet to the interface output queue. */
  2379.   ospf_packet_add (oi, op);
  2380.   /* Hook thread to write packet. */
  2381.   OSPF_ISM_WRITE_ON (oi->ospf);
  2382. }
  2383. void
  2384. ospf_poll_send (struct ospf_nbr_nbma *nbr_nbma)
  2385. {
  2386.   struct ospf_interface *oi;
  2387.   oi = nbr_nbma->oi;
  2388.   assert(oi);
  2389.   /* If this is passive interface, do not send OSPF Hello. */
  2390.   if (OSPF_IF_PARAM (oi, passive_interface) == OSPF_IF_PASSIVE)
  2391.     return;
  2392.   if (oi->type != OSPF_IFTYPE_NBMA)
  2393.     return;
  2394.   if (nbr_nbma->nbr != NULL && nbr_nbma->nbr->state != NSM_Down)
  2395.     return;
  2396.   if (PRIORITY(oi) == 0)
  2397.     return;
  2398.   if (nbr_nbma->priority == 0
  2399.       && oi->state != ISM_DR && oi->state != ISM_Backup)
  2400.     return;
  2401.   ospf_hello_send_sub (oi, &nbr_nbma->addr);
  2402. }
  2403. int
  2404. ospf_poll_timer (struct thread *thread)
  2405. {
  2406.   struct ospf_nbr_nbma *nbr_nbma;
  2407.   nbr_nbma = THREAD_ARG (thread);
  2408.   nbr_nbma->t_poll = NULL;
  2409.   if (IS_DEBUG_OSPF (nsm, NSM_TIMERS))
  2410.     zlog (NULL, LOG_INFO, "NSM[%s:%s]: Timer (Poll timer expire)",
  2411.   IF_NAME (nbr_nbma->oi), inet_ntoa (nbr_nbma->addr));
  2412.   ospf_poll_send (nbr_nbma);
  2413.   if (nbr_nbma->v_poll > 0)
  2414.     OSPF_POLL_TIMER_ON (nbr_nbma->t_poll, ospf_poll_timer,
  2415. nbr_nbma->v_poll);
  2416.   return 0;
  2417. }
  2418. int
  2419. ospf_hello_reply_timer (struct thread *thread)
  2420. {
  2421.   struct ospf_neighbor *nbr;
  2422.   nbr = THREAD_ARG (thread);
  2423.   nbr->t_hello_reply = NULL;
  2424.   assert (nbr->oi);
  2425.   if (IS_DEBUG_OSPF (nsm, NSM_TIMERS))
  2426.     zlog (NULL, LOG_INFO, "NSM[%s:%s]: Timer (hello-reply timer expire)",
  2427.   IF_NAME (nbr->oi), inet_ntoa (nbr->router_id));
  2428.   ospf_hello_send_sub (nbr->oi, &nbr->address.u.prefix4);
  2429.   return 0;
  2430. }
  2431. /* Send OSPF Hello. */
  2432. void
  2433. ospf_hello_send (struct ospf_interface *oi)
  2434. {
  2435.   struct ospf_packet *op;
  2436.   u_int16_t length = OSPF_HEADER_SIZE;
  2437.   /* If this is passive interface, do not send OSPF Hello. */
  2438.   if (OSPF_IF_PARAM (oi, passive_interface) == OSPF_IF_PASSIVE)
  2439.     return;
  2440.   op = ospf_packet_new (oi->ifp->mtu);
  2441.   /* Prepare OSPF common header. */
  2442.   ospf_make_header (OSPF_MSG_HELLO, oi, op->s);
  2443.   /* Prepare OSPF Hello body. */
  2444.   length += ospf_make_hello (oi, op->s);
  2445.   /* Fill OSPF header. */
  2446.   ospf_fill_header (oi, op->s, length);
  2447.   /* Set packet length. */
  2448.   op->length = length;
  2449.   if (oi->type == OSPF_IFTYPE_NBMA)
  2450.     {
  2451.       struct ospf_neighbor *nbr;
  2452.       struct route_node *rn;
  2453.       for (rn = route_top (oi->nbrs); rn; rn = route_next (rn))
  2454. if ((nbr = rn->info))
  2455.   if (nbr != oi->nbr_self)
  2456.     if (nbr->state != NSM_Down)
  2457.       {
  2458. /*  RFC 2328  Section 9.5.1
  2459.     If the router is not eligible to become Designated Router,
  2460.     it must periodically send Hello Packets to both the
  2461.     Designated Router and the Backup Designated Router (if they
  2462.     exist).  */
  2463. if (PRIORITY(oi) == 0 &&
  2464.     IPV4_ADDR_CMP(&DR(oi),  &nbr->address.u.prefix4) &&
  2465.     IPV4_ADDR_CMP(&BDR(oi), &nbr->address.u.prefix4))
  2466.   continue;
  2467. /*  If the router is eligible to become Designated Router, it
  2468.     must periodically send Hello Packets to all neighbors that
  2469.     are also eligible. In addition, if the router is itself the
  2470.     Designated Router or Backup Designated Router, it must also
  2471.     send periodic Hello Packets to all other neighbors. */
  2472. if (nbr->priority == 0 && oi->state == ISM_DROther)
  2473.   continue;
  2474. /* if oi->state == Waiting, send hello to all neighbors */
  2475. {
  2476.   struct ospf_packet *op_dup;
  2477.   op_dup = ospf_packet_dup(op);
  2478.   op_dup->dst = nbr->address.u.prefix4;
  2479.   /* Add packet to the interface output queue. */
  2480.   ospf_packet_add (oi, op_dup);
  2481.   OSPF_ISM_WRITE_ON (oi->ospf);
  2482. }
  2483.       }
  2484.       ospf_packet_free (op);
  2485.     }
  2486.   else
  2487.     {
  2488.       /* Decide destination address. */
  2489.       if (oi->type == OSPF_IFTYPE_VIRTUALLINK)
  2490. op->dst.s_addr = oi->vl_data->peer_addr.s_addr;
  2491.       else 
  2492. op->dst.s_addr = htonl (OSPF_ALLSPFROUTERS);
  2493.       /* Add packet to the interface output queue. */
  2494.       ospf_packet_add (oi, op);
  2495.       /* Hook thread to write packet. */
  2496.       OSPF_ISM_WRITE_ON (oi->ospf);
  2497.     }
  2498. }
  2499. /* Send OSPF Database Description. */
  2500. void
  2501. ospf_db_desc_send (struct ospf_neighbor *nbr)
  2502. {
  2503.   struct ospf_interface *oi;
  2504.   struct ospf_packet *op;
  2505.   u_int16_t length = OSPF_HEADER_SIZE;
  2506.   oi = nbr->oi;
  2507.   op = ospf_packet_new (oi->ifp->mtu);
  2508.   /* Prepare OSPF common header. */
  2509.   ospf_make_header (OSPF_MSG_DB_DESC, oi, op->s);
  2510.   /* Prepare OSPF Database Description body. */
  2511.   length += ospf_make_db_desc (oi, nbr, op->s);
  2512.   /* Fill OSPF header. */
  2513.   ospf_fill_header (oi, op->s, length);
  2514.   /* Set packet length. */
  2515.   op->length = length;
  2516.   /* Decide destination address. */
  2517.   op->dst = nbr->address.u.prefix4;
  2518.   /* Add packet to the interface output queue. */
  2519.   ospf_packet_add (oi, op);
  2520.   /* Hook thread to write packet. */
  2521.   OSPF_ISM_WRITE_ON (oi->ospf);
  2522.   /* Remove old DD packet, then copy new one and keep in neighbor structure. */
  2523.   if (nbr->last_send)
  2524.     ospf_packet_free (nbr->last_send);
  2525.   nbr->last_send = ospf_packet_dup (op);
  2526.   gettimeofday (&nbr->last_send_ts, NULL);
  2527. }
  2528. /* Re-send Database Description. */
  2529. void
  2530. ospf_db_desc_resend (struct ospf_neighbor *nbr)
  2531. {
  2532.   struct ospf_interface *oi;
  2533.   oi = nbr->oi;
  2534.   /* Add packet to the interface output queue. */
  2535.   ospf_packet_add (oi, ospf_packet_dup (nbr->last_send));
  2536.   /* Hook thread to write packet. */
  2537.   OSPF_ISM_WRITE_ON (oi->ospf);
  2538. }
  2539. /* Send Link State Request. */
  2540. void
  2541. ospf_ls_req_send (struct ospf_neighbor *nbr)
  2542. {
  2543.   struct ospf_interface *oi;
  2544.   struct ospf_packet *op;
  2545.   u_int16_t length = OSPF_HEADER_SIZE;
  2546.   oi = nbr->oi;
  2547.   op = ospf_packet_new (oi->ifp->mtu);
  2548.   /* Prepare OSPF common header. */
  2549.   ospf_make_header (OSPF_MSG_LS_REQ, oi, op->s);
  2550.   /* Prepare OSPF Link State Request body. */
  2551.   length += ospf_make_ls_req (nbr, op->s);
  2552.   if (length == OSPF_HEADER_SIZE)
  2553.     {
  2554.       ospf_packet_free (op);
  2555.       return;
  2556.     }
  2557.   /* Fill OSPF header. */
  2558.   ospf_fill_header (oi, op->s, length);
  2559.   /* Set packet length. */
  2560.   op->length = length;
  2561.   /* Decide destination address. */
  2562.   op->dst = nbr->address.u.prefix4;
  2563.   /* Add packet to the interface output queue. */
  2564.   ospf_packet_add (oi, op);
  2565.   /* Hook thread to write packet. */
  2566.   OSPF_ISM_WRITE_ON (oi->ospf);
  2567.   /* Add Link State Request Retransmission Timer. */
  2568.   OSPF_NSM_TIMER_ON (nbr->t_ls_req, ospf_ls_req_timer, nbr->v_ls_req);
  2569. }
  2570. /* Send Link State Update with an LSA. */
  2571. void
  2572. ospf_ls_upd_send_lsa (struct ospf_neighbor *nbr, struct ospf_lsa *lsa,
  2573.       int flag)
  2574. {
  2575.   list update;
  2576.   update = list_new ();
  2577.   listnode_add (update, lsa);
  2578.   ospf_ls_upd_send (nbr, update, flag);
  2579.   list_delete (update);
  2580. }
  2581. static void
  2582. ospf_ls_upd_queue_send (struct ospf_interface *oi, list update,
  2583. struct in_addr addr)
  2584. {
  2585.   struct ospf_packet *op;
  2586.   u_int16_t length = OSPF_HEADER_SIZE;
  2587.   if (IS_DEBUG_OSPF_EVENT)
  2588.     zlog_info ("listcount = %d, dst %s", listcount (update), inet_ntoa(addr));
  2589.   op = ospf_packet_new (oi->ifp->mtu);
  2590.   /* Prepare OSPF common header. */
  2591.   ospf_make_header (OSPF_MSG_LS_UPD, oi, op->s);
  2592.   /* Prepare OSPF Link State Update body. */
  2593.   /* Includes Type-7 translation. */
  2594.   length += ospf_make_ls_upd (oi, update, op->s);
  2595.   /* Fill OSPF header. */
  2596.   ospf_fill_header (oi, op->s, length);
  2597.   /* Set packet length. */
  2598.   op->length = length;
  2599.   /* Decide destination address. */
  2600.   op->dst.s_addr = addr.s_addr;
  2601.   /* Add packet to the interface output queue. */
  2602.   ospf_packet_add (oi, op);
  2603.   /* Hook thread to write packet. */
  2604.   OSPF_ISM_WRITE_ON (oi->ospf);
  2605. }
  2606. static int
  2607. ospf_ls_upd_send_queue_event (struct thread *thread)
  2608. {
  2609.   struct ospf_interface *oi = THREAD_ARG(thread);
  2610.   struct route_node *rn;
  2611.   
  2612.   oi->t_ls_upd_event = NULL;
  2613.   if (IS_DEBUG_OSPF_EVENT)
  2614.     zlog_info ("ospf_ls_upd_send_queue start");
  2615.   for (rn = route_top (oi->ls_upd_queue); rn; rn = route_next (rn))
  2616.     {
  2617.       if (rn->info == NULL)
  2618. continue;
  2619.       while (!list_isempty ((list)rn->info))
  2620. ospf_ls_upd_queue_send (oi, rn->info, rn->p.u.prefix4);
  2621.       list_delete (rn->info);
  2622.       rn->info = NULL;
  2623.       
  2624.       route_unlock_node (rn);
  2625.     }
  2626.   if (IS_DEBUG_OSPF_EVENT)
  2627.     zlog_info ("ospf_ls_upd_send_queue stop");
  2628.   return 0;
  2629. }
  2630. void
  2631. ospf_ls_upd_send (struct ospf_neighbor *nbr, list update, int flag)
  2632. {
  2633.   struct ospf_interface *oi;
  2634.   struct prefix_ipv4 p;
  2635.   struct route_node *rn;
  2636.   listnode n;
  2637.   
  2638.   oi = nbr->oi;
  2639.   p.family = AF_INET;
  2640.   p.prefixlen = IPV4_MAX_BITLEN;
  2641.   
  2642.   /* Decide destination address. */
  2643.   if (oi->type == OSPF_IFTYPE_VIRTUALLINK)
  2644.     p.prefix = oi->vl_data->peer_addr;
  2645.   else if (flag == OSPF_SEND_PACKET_DIRECT)
  2646.     p.prefix = nbr->address.u.prefix4;
  2647.   else if (oi->state == ISM_DR || oi->state == ISM_Backup)
  2648.     p.prefix.s_addr = htonl (OSPF_ALLSPFROUTERS);
  2649.   else if ((oi->type == OSPF_IFTYPE_POINTOPOINT) 
  2650.    && (flag == OSPF_SEND_PACKET_INDIRECT))
  2651.     p.prefix.s_addr = htonl (OSPF_ALLSPFROUTERS);
  2652.   else if (oi->type == OSPF_IFTYPE_POINTOMULTIPOINT)
  2653.     p.prefix.s_addr = htonl (OSPF_ALLSPFROUTERS);
  2654.   else
  2655.     p.prefix.s_addr = htonl (OSPF_ALLDROUTERS);
  2656.   if (oi->type == OSPF_IFTYPE_NBMA)
  2657.     {
  2658.       if (flag == OSPF_SEND_PACKET_INDIRECT)
  2659. zlog_warn ("* LS-Update is directly sent on NBMA network.");
  2660.       if (IPV4_ADDR_SAME(&oi->address->u.prefix4, &p.prefix.s_addr))
  2661. zlog_warn ("* LS-Update is sent to myself.");
  2662.     }
  2663.   rn = route_node_get (oi->ls_upd_queue, (struct prefix *) &p);
  2664.   if (rn->info == NULL)
  2665.     rn->info = list_new ();
  2666.   for (n = listhead (update); n; nextnode (n))
  2667.     listnode_add (rn->info, ospf_lsa_lock (getdata (n)));
  2668.   if (oi->t_ls_upd_event == NULL)
  2669.     oi->t_ls_upd_event =
  2670.       thread_add_event (master, ospf_ls_upd_send_queue_event, oi, 0);
  2671. }
  2672. static void
  2673. ospf_ls_ack_send_list (struct ospf_interface *oi, list ack, struct in_addr dst)
  2674. {
  2675.   struct ospf_packet *op;
  2676.   u_int16_t length = OSPF_HEADER_SIZE;
  2677.   op = ospf_packet_new (oi->ifp->mtu);
  2678.   /* Prepare OSPF common header. */
  2679.   ospf_make_header (OSPF_MSG_LS_ACK, oi, op->s);
  2680.   /* Prepare OSPF Link State Acknowledgment body. */
  2681.   length += ospf_make_ls_ack (oi, ack, op->s);
  2682.   /* Fill OSPF header. */
  2683.   ospf_fill_header (oi, op->s, length);
  2684.   /* Set packet length. */
  2685.   op->length = length;
  2686.   /* Set destination IP address. */
  2687.   op->dst = dst;
  2688.   
  2689.   /* Add packet to the interface output queue. */
  2690.   ospf_packet_add (oi, op);
  2691.   /* Hook thread to write packet. */
  2692.   OSPF_ISM_WRITE_ON (oi->ospf);
  2693. }
  2694. static int
  2695. ospf_ls_ack_send_event (struct thread *thread)
  2696. {
  2697.   struct ospf_interface *oi = THREAD_ARG (thread);
  2698.   oi->t_ls_ack_direct = NULL;
  2699.   
  2700.   while (listcount (oi->ls_ack_direct.ls_ack))
  2701.     ospf_ls_ack_send_list (oi, oi->ls_ack_direct.ls_ack,
  2702.    oi->ls_ack_direct.dst);
  2703.   return 0;
  2704. }
  2705. void
  2706. ospf_ls_ack_send (struct ospf_neighbor *nbr, struct ospf_lsa *lsa)
  2707. {
  2708.   struct ospf_interface *oi = nbr->oi;
  2709.   if (listcount (oi->ls_ack_direct.ls_ack) == 0)
  2710.     oi->ls_ack_direct.dst = nbr->address.u.prefix4;
  2711.   
  2712.   listnode_add (oi->ls_ack_direct.ls_ack, ospf_lsa_lock (lsa));
  2713.   
  2714.   if (oi->t_ls_ack_direct == NULL)
  2715.     oi->t_ls_ack_direct =
  2716.       thread_add_event (master, ospf_ls_ack_send_event, oi, 0);
  2717. }
  2718. /* Send Link State Acknowledgment delayed. */
  2719. void
  2720. ospf_ls_ack_send_delayed (struct ospf_interface *oi)
  2721. {
  2722.   struct in_addr dst;
  2723.   
  2724.   /* Decide destination address. */
  2725.   /* RFC2328 Section 13.5                           On non-broadcast
  2726.      networks, delayed Link State Acknowledgment packets must be
  2727.      unicast separately over each adjacency (i.e., neighbor whose
  2728.      state is >= Exchange).  */
  2729.   if (oi->type == OSPF_IFTYPE_NBMA)
  2730.     {
  2731.       struct ospf_neighbor *nbr;
  2732.       struct route_node *rn;
  2733.       for (rn = route_top (oi->nbrs); rn; rn = route_next (rn))
  2734. if ((nbr = rn->info) != NULL)
  2735.   if (nbr != oi->nbr_self && nbr->state >= NSM_Exchange)
  2736.     while (listcount (oi->ls_ack))
  2737.       ospf_ls_ack_send_list (oi, oi->ls_ack, nbr->address.u.prefix4);
  2738.       return;
  2739.     }
  2740.   if (oi->type == OSPF_IFTYPE_VIRTUALLINK)
  2741.     dst.s_addr = oi->vl_data->peer_addr.s_addr;
  2742.   else if (oi->state == ISM_DR || oi->state == ISM_Backup)
  2743.     dst.s_addr = htonl (OSPF_ALLSPFROUTERS);
  2744.   else if (oi->type == OSPF_IFTYPE_POINTOPOINT)
  2745.     dst.s_addr = htonl (OSPF_ALLSPFROUTERS);
  2746.   else
  2747.     dst.s_addr = htonl (OSPF_ALLDROUTERS);
  2748.   while (listcount (oi->ls_ack))
  2749.     ospf_ls_ack_send_list (oi, oi->ls_ack, dst);
  2750. }