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