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