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