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