blob: 528e8d385f6b2c24fceba129d1265d32fbeba845 [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;
610 struct route_node *rn;
611 struct prefix p, key;
612 int old_state;
613
614 /* increment statistics. */
615 oi->hello_in++;
616
617 hello = (struct ospf_hello *) STREAM_PNT (s);
618
619 /* If Hello is myself, silently discard. */
paul68980082003-03-25 05:07:42 +0000620 if (IPV4_ADDR_SAME (&ospfh->router_id, &oi->ospf->router_id))
pauld3241812003-09-29 12:42:39 +0000621 {
622 if (IS_DEBUG_OSPF_PACKET (ospfh->type - 1, RECV))
623 {
624 zlog_info ("ospf_header[%s/%s]: selforiginated, "
625 "dropping.",
626 ospf_packet_type_str[ospfh->type],
627 inet_ntoa (iph->ip_src));
628 }
629 return;
630 }
paul718e3742002-12-13 20:15:29 +0000631
632 /* If incoming interface is passive one, ignore Hello. */
paulf2c80652002-12-13 21:44:27 +0000633 if (OSPF_IF_PARAM (oi, passive_interface) == OSPF_IF_PASSIVE) {
paulc2191ea2003-04-18 23:59:35 +0000634 zlog_info ("Packet %s [HELLO:RECV]: oi is passive",
635 inet_ntoa (ospfh->router_id));
paul718e3742002-12-13 20:15:29 +0000636 return;
paulf2c80652002-12-13 21:44:27 +0000637 }
paul718e3742002-12-13 20:15:29 +0000638
639 /* get neighbor prefix. */
640 p.family = AF_INET;
641 p.prefixlen = ip_masklen (hello->network_mask);
642 p.u.prefix4 = iph->ip_src;
643
644 /* Compare network mask. */
645 /* Checking is ignored for Point-to-Point and Virtual link. */
646 if (oi->type != OSPF_IFTYPE_POINTOPOINT
647 && oi->type != OSPF_IFTYPE_VIRTUALLINK)
648 if (oi->address->prefixlen != p.prefixlen)
649 {
650 zlog_warn ("Packet %s [Hello:RECV]: NetworkMask mismatch.",
651 inet_ntoa (ospfh->router_id));
652 return;
653 }
654
655 /* Compare Hello Interval. */
656 if (OSPF_IF_PARAM (oi, v_hello) != ntohs (hello->hello_interval))
657 {
658 zlog_warn ("Packet %s [Hello:RECV]: HelloInterval mismatch.",
659 inet_ntoa (ospfh->router_id));
660 return;
661 }
662
663 /* Compare Router Dead Interval. */
664 if (OSPF_IF_PARAM (oi, v_wait) != ntohl (hello->dead_interval))
665 {
666 zlog_warn ("Packet %s [Hello:RECV]: RouterDeadInterval mismatch.",
667 inet_ntoa (ospfh->router_id));
668 return;
669 }
670
671 if (IS_DEBUG_OSPF_EVENT)
672 zlog_info ("Packet %s [Hello:RECV]: Options %s",
673 inet_ntoa (ospfh->router_id),
674 ospf_options_dump (hello->options));
675
676 /* Compare options. */
677#define REJECT_IF_TBIT_ON 1 /* XXX */
678#ifdef REJECT_IF_TBIT_ON
679 if (CHECK_FLAG (hello->options, OSPF_OPTION_T))
680 {
681 /*
682 * This router does not support non-zero TOS.
683 * Drop this Hello packet not to establish neighbor relationship.
684 */
685 zlog_warn ("Packet %s [Hello:RECV]: T-bit on, drop it.",
686 inet_ntoa (ospfh->router_id));
687 return;
688 }
689#endif /* REJECT_IF_TBIT_ON */
690
691#ifdef HAVE_OPAQUE_LSA
paul68980082003-03-25 05:07:42 +0000692 if (CHECK_FLAG (oi->ospf->config, OSPF_OPAQUE_CAPABLE)
paul718e3742002-12-13 20:15:29 +0000693 && CHECK_FLAG (hello->options, OSPF_OPTION_O))
694 {
695 /*
696 * This router does know the correct usage of O-bit
697 * the bit should be set in DD packet only.
698 */
699 zlog_warn ("Packet %s [Hello:RECV]: O-bit abuse?",
700 inet_ntoa (ospfh->router_id));
701#ifdef STRICT_OBIT_USAGE_CHECK
702 return; /* Reject this packet. */
703#else /* STRICT_OBIT_USAGE_CHECK */
704 UNSET_FLAG (hello->options, OSPF_OPTION_O); /* Ignore O-bit. */
705#endif /* STRICT_OBIT_USAGE_CHECK */
706 }
707#endif /* HAVE_OPAQUE_LSA */
708
709 /* new for NSSA is to ensure that NP is on and E is off */
710
711#ifdef HAVE_NSSA
712 if (oi->area->external_routing == OSPF_AREA_NSSA)
713 {
714 if (! (CHECK_FLAG (OPTIONS (oi), OSPF_OPTION_NP)
715 && CHECK_FLAG (hello->options, OSPF_OPTION_NP)
716 && ! CHECK_FLAG (OPTIONS (oi), OSPF_OPTION_E)
717 && ! CHECK_FLAG (hello->options, OSPF_OPTION_E)))
718 {
719 zlog_warn ("NSSA-Packet-%s[Hello:RECV]: my options: %x, his options %x", inet_ntoa (ospfh->router_id), OPTIONS (oi), hello->options);
720 return;
721 }
722 if (IS_DEBUG_OSPF_NSSA)
723 zlog_info ("NSSA-Hello:RECV:Packet from %s:", inet_ntoa(ospfh->router_id));
724 }
725 else
726#endif /* HAVE_NSSA */
727 /* The setting of the E-bit found in the Hello Packet's Options
728 field must match this area's ExternalRoutingCapability A
729 mismatch causes processing to stop and the packet to be
730 dropped. The setting of the rest of the bits in the Hello
731 Packet's Options field should be ignored. */
732 if (CHECK_FLAG (OPTIONS (oi), OSPF_OPTION_E) !=
733 CHECK_FLAG (hello->options, OSPF_OPTION_E))
734 {
735 zlog_warn ("Packet[Hello:RECV]: my options: %x, his options %x",
736 OPTIONS (oi), hello->options);
737 return;
738 }
739
740
741 /* Get neighbor information from table. */
742 key.family = AF_INET;
743 key.prefixlen = IPV4_MAX_BITLEN;
744 key.u.prefix4 = iph->ip_src;
745
746 rn = route_node_get (oi->nbrs, &key);
747 if (rn->info)
748 {
749 route_unlock_node (rn);
750 nbr = rn->info;
751
752 if (oi->type == OSPF_IFTYPE_NBMA && nbr->state == NSM_Attempt)
753 {
754 nbr->src = iph->ip_src;
755 nbr->address = p;
756 }
757 }
758 else
759 {
760 /* Create new OSPF Neighbor structure. */
761 nbr = ospf_nbr_new (oi);
762 nbr->state = NSM_Down;
763 nbr->src = iph->ip_src;
764 nbr->address = p;
765
766 rn->info = nbr;
767
768 nbr->nbr_nbma = NULL;
769
770 if (oi->type == OSPF_IFTYPE_NBMA)
771 {
772 struct ospf_nbr_nbma *nbr_nbma;
773 listnode node;
774
775 for (node = listhead (oi->nbr_nbma); node; nextnode (node))
776 {
777 nbr_nbma = getdata (node);
778 assert (nbr_nbma);
779
780 if (IPV4_ADDR_SAME(&nbr_nbma->addr, &iph->ip_src))
781 {
782 nbr_nbma->nbr = nbr;
783 nbr->nbr_nbma = nbr_nbma;
784
785 if (nbr_nbma->t_poll)
786 OSPF_POLL_TIMER_OFF (nbr_nbma->t_poll);
787
788 nbr->state_change = nbr_nbma->state_change + 1;
789 }
790 }
791 }
792
793 /* New nbr, save the crypto sequence number if necessary */
794 if (ntohs (ospfh->auth_type) == OSPF_AUTH_CRYPTOGRAPHIC)
795 nbr->crypt_seqnum = ospfh->u.crypt.crypt_seqnum;
796
797 if (IS_DEBUG_OSPF_EVENT)
798 zlog_info ("NSM[%s:%s]: start", IF_NAME (nbr->oi),
799 inet_ntoa (nbr->router_id));
800 }
801
802 nbr->router_id = ospfh->router_id;
803
804 old_state = nbr->state;
805
806 /* Add event to thread. */
807 OSPF_NSM_EVENT_EXECUTE (nbr, NSM_HelloReceived);
808
809 /* RFC2328 Section 9.5.1
810 If the router is not eligible to become Designated Router,
811 (snip) It must also send an Hello Packet in reply to an
812 Hello Packet received from any eligible neighbor (other than
813 the current Designated Router and Backup Designated Router). */
814 if (oi->type == OSPF_IFTYPE_NBMA)
815 if (PRIORITY(oi) == 0 && hello->priority > 0
816 && IPV4_ADDR_CMP(&DR(oi), &iph->ip_src)
817 && IPV4_ADDR_CMP(&BDR(oi), &iph->ip_src))
818 OSPF_NSM_TIMER_ON (nbr->t_hello_reply, ospf_hello_reply_timer,
819 OSPF_HELLO_REPLY_DELAY);
820
821 /* on NBMA network type, it happens to receive bidirectional Hello packet
822 without advance 1-Way Received event.
823 To avoid incorrect DR-seletion, raise 1-Way Received event.*/
824 if (oi->type == OSPF_IFTYPE_NBMA &&
825 (old_state == NSM_Down || old_state == NSM_Attempt))
826 {
827 OSPF_NSM_EVENT_EXECUTE (nbr, NSM_OneWayReceived);
828 nbr->priority = hello->priority;
829 nbr->d_router = hello->d_router;
830 nbr->bd_router = hello->bd_router;
831 return;
832 }
833
paul68980082003-03-25 05:07:42 +0000834 if (ospf_nbr_bidirectional (&oi->ospf->router_id, hello->neighbors,
paul718e3742002-12-13 20:15:29 +0000835 size - OSPF_HELLO_MIN_SIZE))
836 {
837 OSPF_NSM_EVENT_EXECUTE (nbr, NSM_TwoWayReceived);
838 nbr->options |= hello->options;
839 }
840 else
841 {
842 OSPF_NSM_EVENT_EXECUTE (nbr, NSM_OneWayReceived);
843 /* Set neighbor information. */
844 nbr->priority = hello->priority;
845 nbr->d_router = hello->d_router;
846 nbr->bd_router = hello->bd_router;
847 return;
848 }
849
850 /* If neighbor itself declares DR and no BDR exists,
851 cause event BackupSeen */
852 if (IPV4_ADDR_SAME (&nbr->address.u.prefix4, &hello->d_router))
853 if (hello->bd_router.s_addr == 0 && oi->state == ISM_Waiting)
854 OSPF_ISM_EVENT_SCHEDULE (oi, ISM_BackupSeen);
855
856 /* neighbor itself declares BDR. */
857 if (oi->state == ISM_Waiting &&
858 IPV4_ADDR_SAME (&nbr->address.u.prefix4, &hello->bd_router))
859 OSPF_ISM_EVENT_SCHEDULE (oi, ISM_BackupSeen);
860
861 /* had not previously. */
862 if ((IPV4_ADDR_SAME (&nbr->address.u.prefix4, &hello->d_router) &&
863 IPV4_ADDR_CMP (&nbr->address.u.prefix4, &nbr->d_router)) ||
864 (IPV4_ADDR_CMP (&nbr->address.u.prefix4, &hello->d_router) &&
865 IPV4_ADDR_SAME (&nbr->address.u.prefix4, &nbr->d_router)))
866 OSPF_ISM_EVENT_SCHEDULE (oi, ISM_NeighborChange);
867
868 /* had not previously. */
869 if ((IPV4_ADDR_SAME (&nbr->address.u.prefix4, &hello->bd_router) &&
870 IPV4_ADDR_CMP (&nbr->address.u.prefix4, &nbr->bd_router)) ||
871 (IPV4_ADDR_CMP (&nbr->address.u.prefix4, &hello->bd_router) &&
872 IPV4_ADDR_SAME (&nbr->address.u.prefix4, &nbr->bd_router)))
873 OSPF_ISM_EVENT_SCHEDULE (oi, ISM_NeighborChange);
874
875 /* Neighbor priority check. */
876 if (nbr->priority >= 0 && nbr->priority != hello->priority)
877 OSPF_ISM_EVENT_SCHEDULE (oi, ISM_NeighborChange);
878
879 /* Set neighbor information. */
880 nbr->priority = hello->priority;
881 nbr->d_router = hello->d_router;
882 nbr->bd_router = hello->bd_router;
883}
884
885/* Save DD flags/options/Seqnum received. */
886void
887ospf_db_desc_save_current (struct ospf_neighbor *nbr,
888 struct ospf_db_desc *dd)
889{
890 nbr->last_recv.flags = dd->flags;
891 nbr->last_recv.options = dd->options;
892 nbr->last_recv.dd_seqnum = ntohl (dd->dd_seqnum);
893}
894
895/* Process rest of DD packet. */
896static void
897ospf_db_desc_proc (struct stream *s, struct ospf_interface *oi,
898 struct ospf_neighbor *nbr, struct ospf_db_desc *dd,
899 u_int16_t size)
900{
901 struct ospf_lsa *new, *find;
902 struct lsa_header *lsah;
903
904 stream_forward (s, OSPF_DB_DESC_MIN_SIZE);
905 for (size -= OSPF_DB_DESC_MIN_SIZE;
906 size >= OSPF_LSA_HEADER_SIZE; size -= OSPF_LSA_HEADER_SIZE)
907 {
908 lsah = (struct lsa_header *) STREAM_PNT (s);
909 stream_forward (s, OSPF_LSA_HEADER_SIZE);
910
911 /* Unknown LS type. */
912 if (lsah->type < OSPF_MIN_LSA || lsah->type >= OSPF_MAX_LSA)
913 {
914 zlog_warn ("Pakcet [DD:RECV]: Unknown LS type %d.", lsah->type);
915 OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_SeqNumberMismatch);
916 return;
917 }
918
919#ifdef HAVE_OPAQUE_LSA
920 if (IS_OPAQUE_LSA (lsah->type)
921 && ! CHECK_FLAG (nbr->options, OSPF_OPTION_O))
922 {
923 zlog_warn ("LSA[Type%d:%s]: Opaque capability mismatch?", lsah->type, inet_ntoa (lsah->id));
924 OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_SeqNumberMismatch);
925 return;
926 }
927#endif /* HAVE_OPAQUE_LSA */
928
929 switch (lsah->type)
930 {
931 case OSPF_AS_EXTERNAL_LSA:
932#ifdef HAVE_OPAQUE_LSA
933 case OSPF_OPAQUE_AS_LSA:
934#endif /* HAVE_OPAQUE_LSA */
935#ifdef HAVE_NSSA
936 /* Check for stub area. Reject if AS-External from stub but
937 allow if from NSSA. */
938 if (oi->area->external_routing == OSPF_AREA_STUB)
939#else /* ! HAVE_NSSA */
940 if (oi->area->external_routing != OSPF_AREA_DEFAULT)
941#endif /* HAVE_NSSA */
942 {
943 zlog_warn ("Packet [DD:RECV]: LSA[Type%d:%s] from %s area.",
944 lsah->type, inet_ntoa (lsah->id),
945 (oi->area->external_routing == OSPF_AREA_STUB) ?\
946 "STUB" : "NSSA");
947 OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_SeqNumberMismatch);
948 return;
949 }
950 break;
951 default:
952 break;
953 }
954
955 /* Create LS-request object. */
956 new = ospf_ls_request_new (lsah);
957
958 /* Lookup received LSA, then add LS request list. */
959 find = ospf_lsa_lookup_by_header (oi->area, lsah);
960 if (!find || ospf_lsa_more_recent (find, new) < 0)
961 {
962 ospf_ls_request_add (nbr, new);
963 ospf_lsa_discard (new);
964 }
965 else
966 {
967 /* Received LSA is not recent. */
968 if (IS_DEBUG_OSPF_EVENT)
969 zlog_info ("Packet [DD:RECV]: LSA received Type %d, "
970 "ID %s is not recent.", lsah->type, inet_ntoa (lsah->id));
971 ospf_lsa_discard (new);
972 continue;
973 }
974 }
975
976 /* Master */
977 if (IS_SET_DD_MS (nbr->dd_flags))
978 {
979 nbr->dd_seqnum++;
980 /* Entire DD packet sent. */
981 if (!IS_SET_DD_M (dd->flags) && !IS_SET_DD_M (nbr->dd_flags))
982 OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_ExchangeDone);
983 else
984 /* Send new DD packet. */
985 ospf_db_desc_send (nbr);
986 }
987 /* Slave */
988 else
989 {
990 nbr->dd_seqnum = ntohl (dd->dd_seqnum);
991
992 /* When master's more flags is not set. */
993 if (!IS_SET_DD_M (dd->flags) && ospf_db_summary_isempty (nbr))
994 {
995 nbr->dd_flags &= ~(OSPF_DD_FLAG_M);
996 OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_ExchangeDone);
997 }
998
999 /* Send DD pakcet in reply. */
1000 ospf_db_desc_send (nbr);
1001 }
1002
1003 /* Save received neighbor values from DD. */
1004 ospf_db_desc_save_current (nbr, dd);
1005}
1006
1007int
1008ospf_db_desc_is_dup (struct ospf_db_desc *dd, struct ospf_neighbor *nbr)
1009{
1010 /* Is DD duplicated? */
1011 if (dd->options == nbr->last_recv.options &&
1012 dd->flags == nbr->last_recv.flags &&
1013 dd->dd_seqnum == htonl (nbr->last_recv.dd_seqnum))
1014 return 1;
1015
1016 return 0;
1017}
1018
1019/* OSPF Database Description message read -- RFC2328 Section 10.6. */
1020void
1021ospf_db_desc (struct ip *iph, struct ospf_header *ospfh,
1022 struct stream *s, struct ospf_interface *oi, u_int16_t size)
1023{
1024 struct ospf_db_desc *dd;
1025 struct ospf_neighbor *nbr;
1026
1027 /* Increment statistics. */
1028 oi->db_desc_in++;
1029
1030 dd = (struct ospf_db_desc *) STREAM_PNT (s);
pauld363df22003-06-19 00:26:34 +00001031
paul718e3742002-12-13 20:15:29 +00001032 nbr = ospf_nbr_lookup_by_addr (oi->nbrs, &iph->ip_src);
1033 if (nbr == NULL)
1034 {
1035 zlog_warn ("Packet[DD]: Unknown Neighbor %s",
1036 inet_ntoa (ospfh->router_id));
1037 return;
1038 }
1039
1040 /* Check MTU. */
1041 if (ntohs (dd->mtu) > oi->ifp->mtu)
1042 {
1043 zlog_warn ("Packet[DD]: MTU is larger than [%s]'s MTU", IF_NAME (oi));
1044 return;
1045 }
1046
pauld363df22003-06-19 00:26:34 +00001047#ifdef HAVE_NSSA
1048 /*
1049 * XXX HACK by Hasso Tepper. Setting N/P bit in NSSA area DD packets is not
1050 * required. In fact at least JunOS sends DD packets with P bit clear.
1051 * Until proper solution is developped, this hack should help.
1052 *
1053 * Update: According to the RFCs, N bit is specified /only/ for Hello
1054 * options, unfortunately its use in DD options is not specified. Hence some
1055 * implementations follow E-bit semantics and set it in DD options, and some
1056 * treat it as unspecified and hence follow the directive "default for
1057 * options is clear", ie unset.
1058 *
1059 * Reset the flag, as ospfd follows E-bit semantics.
1060 */
1061 if ( (oi->area->external_routing == OSPF_AREA_NSSA)
1062 && (CHECK_FLAG (nbr->options, OSPF_OPTION_NP))
1063 && (!CHECK_FLAG (dd->options, OSPF_OPTION_NP)) )
1064 {
1065 if (IS_DEBUG_OSPF_EVENT)
paul3db0a772003-06-19 01:07:40 +00001066 zlog_notice ("Packet[DD]: Neighbour %s: Has NSSA capability, sends with N bit clear in DD options",
pauld363df22003-06-19 00:26:34 +00001067 inet_ntoa (nbr->router_id) );
1068 SET_FLAG (dd->options, OSPF_OPTION_NP);
1069 }
pauld363df22003-06-19 00:26:34 +00001070#endif /* HAVE_NSSA */
1071
paul718e3742002-12-13 20:15:29 +00001072#ifdef REJECT_IF_TBIT_ON
1073 if (CHECK_FLAG (dd->options, OSPF_OPTION_T))
1074 {
1075 /*
1076 * In Hello protocol, optional capability must have checked
1077 * to prevent this T-bit enabled router be my neighbor.
1078 */
1079 zlog_warn ("Packet[DD]: Neighbor %s: T-bit on?", inet_ntoa (nbr->router_id));
1080 return;
1081 }
1082#endif /* REJECT_IF_TBIT_ON */
1083
1084#ifdef HAVE_OPAQUE_LSA
1085 if (CHECK_FLAG (dd->options, OSPF_OPTION_O)
paul68980082003-03-25 05:07:42 +00001086 && !CHECK_FLAG (oi->ospf->config, OSPF_OPAQUE_CAPABLE))
paul718e3742002-12-13 20:15:29 +00001087 {
1088 /*
1089 * This node is not configured to handle O-bit, for now.
1090 * Clear it to ignore unsupported capability proposed by neighbor.
1091 */
1092 UNSET_FLAG (dd->options, OSPF_OPTION_O);
1093 }
1094#endif /* HAVE_OPAQUE_LSA */
1095
1096 /* Process DD packet by neighbor status. */
1097 switch (nbr->state)
1098 {
1099 case NSM_Down:
1100 case NSM_Attempt:
1101 case NSM_TwoWay:
1102 zlog_warn ("Packet[DD]: Neighbor state is %s, packet discarded.",
1103 LOOKUP (ospf_nsm_state_msg, nbr->state));
1104 break;
1105 case NSM_Init:
1106 OSPF_NSM_EVENT_EXECUTE (nbr, NSM_TwoWayReceived);
1107 /* If the new state is ExStart, the processing of the current
1108 packet should then continue in this new state by falling
1109 through to case ExStart below. */
1110 if (nbr->state != NSM_ExStart)
1111 break;
1112 case NSM_ExStart:
1113 /* Initial DBD */
1114 if ((IS_SET_DD_ALL (dd->flags) == OSPF_DD_FLAG_ALL) &&
1115 (size == OSPF_DB_DESC_MIN_SIZE))
1116 {
paul68980082003-03-25 05:07:42 +00001117 if (IPV4_ADDR_CMP (&nbr->router_id, &oi->ospf->router_id) > 0)
paul718e3742002-12-13 20:15:29 +00001118 {
1119 /* We're Slave---obey */
1120 zlog_warn ("Packet[DD]: Negotiation done (Slave).");
1121 nbr->dd_seqnum = ntohl (dd->dd_seqnum);
1122 nbr->dd_flags &= ~(OSPF_DD_FLAG_MS|OSPF_DD_FLAG_I); /* Reset I/MS */
1123 }
1124 else
1125 {
1126 /* We're Master, ignore the initial DBD from Slave */
1127 zlog_warn ("Packet[DD]: Initial DBD from Slave, ignoring.");
1128 break;
1129 }
1130 }
1131 /* Ack from the Slave */
1132 else if (!IS_SET_DD_MS (dd->flags) && !IS_SET_DD_I (dd->flags) &&
1133 ntohl (dd->dd_seqnum) == nbr->dd_seqnum &&
paul68980082003-03-25 05:07:42 +00001134 IPV4_ADDR_CMP (&nbr->router_id, &oi->ospf->router_id) < 0)
paul718e3742002-12-13 20:15:29 +00001135 {
1136 zlog_warn ("Packet[DD]: Negotiation done (Master).");
1137 nbr->dd_flags &= ~OSPF_DD_FLAG_I;
1138 }
1139 else
1140 {
1141 zlog_warn ("Packet[DD]: Negotiation fails.");
1142 break;
1143 }
1144
1145 /* This is where the real Options are saved */
1146 nbr->options = dd->options;
1147
1148#ifdef HAVE_OPAQUE_LSA
paul68980082003-03-25 05:07:42 +00001149 if (CHECK_FLAG (oi->ospf->config, OSPF_OPAQUE_CAPABLE))
paul718e3742002-12-13 20:15:29 +00001150 {
1151 if (IS_DEBUG_OSPF_EVENT)
1152 zlog_info ("Neighbor[%s] is %sOpaque-capable.",
1153 inet_ntoa (nbr->router_id),
1154 CHECK_FLAG (nbr->options, OSPF_OPTION_O) ? "" : "NOT ");
1155
1156 if (! CHECK_FLAG (nbr->options, OSPF_OPTION_O)
1157 && IPV4_ADDR_SAME (&DR (oi), &nbr->address.u.prefix4))
1158 {
1159 zlog_warn ("DR-neighbor[%s] is NOT opaque-capable; Opaque-LSAs cannot be reliably advertised in this network.", inet_ntoa (nbr->router_id));
1160 /* This situation is undesirable, but not a real error. */
1161 }
1162 }
1163#endif /* HAVE_OPAQUE_LSA */
1164
1165 OSPF_NSM_EVENT_EXECUTE (nbr, NSM_NegotiationDone);
1166
1167 /* continue processing rest of packet. */
1168 ospf_db_desc_proc (s, oi, nbr, dd, size);
1169 break;
1170 case NSM_Exchange:
1171 if (ospf_db_desc_is_dup (dd, nbr))
1172 {
1173 if (IS_SET_DD_MS (nbr->dd_flags))
1174 /* Master: discard duplicated DD packet. */
1175 zlog_warn ("Packet[DD] (Master): packet duplicated.");
1176 else
1177 /* Slave: cause to retransmit the last Database Description. */
1178 {
1179 zlog_warn ("Packet[DD] [Slave]: packet duplicated.");
1180 ospf_db_desc_resend (nbr);
1181 }
1182 break;
1183 }
1184
1185 /* Otherwise DD packet should be checked. */
1186 /* Check Master/Slave bit mismatch */
1187 if (IS_SET_DD_MS (dd->flags) != IS_SET_DD_MS (nbr->last_recv.flags))
1188 {
1189 zlog_warn ("Packet[DD]: MS-bit mismatch.");
1190 OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_SeqNumberMismatch);
1191 if (IS_DEBUG_OSPF_EVENT)
1192 zlog_info ("Packet[DD]: dd->flags=%d, nbr->dd_flags=%d",
1193 dd->flags, nbr->dd_flags);
1194 break;
1195 }
1196
1197 /* Check initialize bit is set. */
1198 if (IS_SET_DD_I (dd->flags))
1199 {
1200 zlog_warn ("Packet[DD]: I-bit set.");
1201 OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_SeqNumberMismatch);
1202 break;
1203 }
1204
1205 /* Check DD Options. */
1206 if (dd->options != nbr->options)
1207 {
1208#ifdef ORIGINAL_CODING
1209 /* Save the new options for debugging */
1210 nbr->options = dd->options;
1211#endif /* ORIGINAL_CODING */
1212 zlog_warn ("Packet[DD]: options mismatch.");
1213 OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_SeqNumberMismatch);
1214 break;
1215 }
1216
1217 /* Check DD sequence number. */
1218 if ((IS_SET_DD_MS (nbr->dd_flags) &&
1219 ntohl (dd->dd_seqnum) != nbr->dd_seqnum) ||
1220 (!IS_SET_DD_MS (nbr->dd_flags) &&
1221 ntohl (dd->dd_seqnum) != nbr->dd_seqnum + 1))
1222 {
1223 zlog_warn ("Pakcet[DD]: sequence number mismatch.");
1224 OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_SeqNumberMismatch);
1225 break;
1226 }
1227
1228 /* Continue processing rest of packet. */
1229 ospf_db_desc_proc (s, oi, nbr, dd, size);
1230 break;
1231 case NSM_Loading:
1232 case NSM_Full:
1233 if (ospf_db_desc_is_dup (dd, nbr))
1234 {
1235 if (IS_SET_DD_MS (nbr->dd_flags))
1236 {
1237 /* Master should discard duplicate DD packet. */
1238 zlog_warn ("Pakcet[DD]: duplicated, packet discarded.");
1239 break;
1240 }
1241 else
1242 {
1243 struct timeval t, now;
1244 gettimeofday (&now, NULL);
1245 t = tv_sub (now, nbr->last_send_ts);
1246 if (tv_cmp (t, int2tv (nbr->v_inactivity)) < 0)
1247 {
1248 /* In states Loading and Full the slave must resend
1249 its last Database Description packet in response to
1250 duplicate Database Description packets received
1251 from the master. For this reason the slave must
1252 wait RouterDeadInterval seconds before freeing the
1253 last Database Description packet. Reception of a
1254 Database Description packet from the master after
1255 this interval will generate a SeqNumberMismatch
1256 neighbor event. RFC2328 Section 10.8 */
1257 ospf_db_desc_resend (nbr);
1258 break;
1259 }
1260 }
1261 }
1262
1263 OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_SeqNumberMismatch);
1264 break;
1265 default:
1266 zlog_warn ("Packet[DD]: NSM illegal status.");
1267 break;
1268 }
1269}
1270
1271#define OSPF_LSA_KEY_SIZE 12 /* type(4) + id(4) + ar(4) */
1272
1273/* OSPF Link State Request Read -- RFC2328 Section 10.7. */
1274void
1275ospf_ls_req (struct ip *iph, struct ospf_header *ospfh,
1276 struct stream *s, struct ospf_interface *oi, u_int16_t size)
1277{
1278 struct ospf_neighbor *nbr;
1279 u_int32_t ls_type;
1280 struct in_addr ls_id;
1281 struct in_addr adv_router;
1282 struct ospf_lsa *find;
1283 list ls_upd;
1284 int length;
1285
1286 /* Increment statistics. */
1287 oi->ls_req_in++;
1288
1289 nbr = ospf_nbr_lookup_by_addr (oi->nbrs, &iph->ip_src);
1290 if (nbr == NULL)
1291 {
1292 zlog_warn ("Link State Request: Unknown Neighbor %s.",
1293 inet_ntoa (ospfh->router_id));
1294 return;
1295 }
1296
1297 /* Neighbor State should be Exchange or later. */
1298 if (nbr->state != NSM_Exchange &&
1299 nbr->state != NSM_Loading &&
1300 nbr->state != NSM_Full)
1301 {
1302 zlog_warn ("Link State Request: Neighbor state is %s, packet discarded.",
1303 LOOKUP (ospf_nsm_state_msg, nbr->state));
1304 return;
1305 }
1306
1307 /* Send Link State Update for ALL requested LSAs. */
1308 ls_upd = list_new ();
1309 length = OSPF_HEADER_SIZE + OSPF_LS_UPD_MIN_SIZE;
1310
1311 while (size >= OSPF_LSA_KEY_SIZE)
1312 {
1313 /* Get one slice of Link State Request. */
1314 ls_type = stream_getl (s);
1315 ls_id.s_addr = stream_get_ipv4 (s);
1316 adv_router.s_addr = stream_get_ipv4 (s);
1317
1318 /* Verify LSA type. */
1319 if (ls_type < OSPF_MIN_LSA || ls_type >= OSPF_MAX_LSA)
1320 {
1321 OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_BadLSReq);
1322 list_delete (ls_upd);
1323 return;
1324 }
1325
1326 /* Search proper LSA in LSDB. */
1327 find = ospf_lsa_lookup (oi->area, ls_type, ls_id, adv_router);
1328 if (find == NULL)
1329 {
1330 OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_BadLSReq);
1331 list_delete (ls_upd);
1332 return;
1333 }
1334
1335 /* Packet overflows MTU size, send immediatly. */
1336 if (length + ntohs (find->data->length) > OSPF_PACKET_MAX (oi))
1337 {
1338 if (oi->type == OSPF_IFTYPE_NBMA)
1339 ospf_ls_upd_send (nbr, ls_upd, OSPF_SEND_PACKET_DIRECT);
1340 else
1341 ospf_ls_upd_send (nbr, ls_upd, OSPF_SEND_PACKET_INDIRECT);
1342
1343 /* Only remove list contents. Keep ls_upd. */
1344 list_delete_all_node (ls_upd);
1345
1346 length = OSPF_HEADER_SIZE + OSPF_LS_UPD_MIN_SIZE;
1347 }
1348
1349 /* Append LSA to update list. */
1350 listnode_add (ls_upd, find);
1351 length += ntohs (find->data->length);
1352
1353 size -= OSPF_LSA_KEY_SIZE;
1354 }
1355
1356 /* Send rest of Link State Update. */
1357 if (listcount (ls_upd) > 0)
1358 {
1359 if (oi->type == OSPF_IFTYPE_NBMA)
1360 ospf_ls_upd_send (nbr, ls_upd, OSPF_SEND_PACKET_DIRECT);
1361 else
1362 ospf_ls_upd_send (nbr, ls_upd, OSPF_SEND_PACKET_INDIRECT);
1363
1364 list_delete (ls_upd);
1365 }
1366 else
1367 list_free (ls_upd);
1368}
1369
1370/* Get the list of LSAs from Link State Update packet.
1371 And process some validation -- RFC2328 Section 13. (1)-(2). */
1372static list
1373ospf_ls_upd_list_lsa (struct ospf_neighbor *nbr, struct stream *s,
1374 struct ospf_interface *oi, size_t size)
1375{
1376 u_int16_t count, sum;
1377 u_int32_t length;
1378 struct lsa_header *lsah;
1379 struct ospf_lsa *lsa;
1380 list lsas;
1381
1382 lsas = list_new ();
1383
1384 count = stream_getl (s);
1385 size -= OSPF_LS_UPD_MIN_SIZE; /* # LSAs */
1386
1387 for (; size >= OSPF_LSA_HEADER_SIZE && count > 0;
1388 size -= length, stream_forward (s, length), count--)
1389 {
1390 lsah = (struct lsa_header *) STREAM_PNT (s);
1391 length = ntohs (lsah->length);
1392
1393 if (length > size)
1394 {
1395 zlog_warn ("Link State Update: LSA length exceeds packet size.");
1396 break;
1397 }
1398
1399 /* Validate the LSA's LS checksum. */
1400 sum = lsah->checksum;
1401 if (sum != ospf_lsa_checksum (lsah))
1402 {
1403 zlog_warn ("Link State Update: LSA checksum error %x, %x.",
1404 sum, lsah->checksum);
1405 continue;
1406 }
1407
1408 /* Examine the LSA's LS type. */
1409 if (lsah->type < OSPF_MIN_LSA || lsah->type >= OSPF_MAX_LSA)
1410 {
1411 zlog_warn ("Link State Update: Unknown LS type %d", lsah->type);
1412 continue;
1413 }
1414
1415 /*
1416 * What if the received LSA's age is greater than MaxAge?
1417 * Treat it as a MaxAge case -- endo.
1418 */
1419 if (ntohs (lsah->ls_age) > OSPF_LSA_MAXAGE)
1420 lsah->ls_age = htons (OSPF_LSA_MAXAGE);
1421
1422#ifdef HAVE_OPAQUE_LSA
1423 if (CHECK_FLAG (nbr->options, OSPF_OPTION_O))
1424 {
1425#ifdef STRICT_OBIT_USAGE_CHECK
1426 if ((IS_OPAQUE_LSA(lsah->type) &&
1427 ! CHECK_FLAG (lsah->options, OSPF_OPTION_O))
1428 || (! IS_OPAQUE_LSA(lsah->type) &&
1429 CHECK_FLAG (lsah->options, OSPF_OPTION_O)))
1430 {
1431 /*
1432 * This neighbor must know the exact usage of O-bit;
1433 * the bit will be set in Type-9,10,11 LSAs only.
1434 */
1435 zlog_warn ("LSA[Type%d:%s]: O-bit abuse?", lsah->type, inet_ntoa (lsah->id));
1436 continue;
1437 }
1438#endif /* STRICT_OBIT_USAGE_CHECK */
1439
1440 /* Do not take in AS External Opaque-LSAs if we are a stub. */
1441 if (lsah->type == OSPF_OPAQUE_AS_LSA
1442 && nbr->oi->area->external_routing != OSPF_AREA_DEFAULT)
1443 {
1444 if (IS_DEBUG_OSPF_EVENT)
1445 zlog_info ("LSA[Type%d:%s]: We are a stub, don't take this LSA.", lsah->type, inet_ntoa (lsah->id));
1446 continue;
1447 }
1448 }
1449 else if (IS_OPAQUE_LSA(lsah->type))
1450 {
1451 zlog_warn ("LSA[Type%d:%s]: Opaque capability mismatch?", lsah->type, inet_ntoa (lsah->id));
1452 continue;
1453 }
1454#endif /* HAVE_OPAQUE_LSA */
1455
1456 /* Create OSPF LSA instance. */
1457 lsa = ospf_lsa_new ();
1458
1459 /* We may wish to put some error checking if type NSSA comes in
1460 and area not in NSSA mode */
1461 switch (lsah->type)
1462 {
1463 case OSPF_AS_EXTERNAL_LSA:
1464#ifdef HAVE_OPAQUE_LSA
1465 case OSPF_OPAQUE_AS_LSA:
1466 lsa->area = NULL;
1467 break;
1468 case OSPF_OPAQUE_LINK_LSA:
1469 lsa->oi = oi; /* Remember incoming interface for flooding control. */
1470 /* Fallthrough */
1471#endif /* HAVE_OPAQUE_LSA */
1472 default:
1473 lsa->area = oi->area;
1474 break;
1475 }
1476
1477 lsa->data = ospf_lsa_data_new (length);
1478 memcpy (lsa->data, lsah, length);
1479
1480 if (IS_DEBUG_OSPF_EVENT)
1481 zlog_info("LSA[Type%d:%s]: %p new LSA created with Link State Update",
1482 lsa->data->type, inet_ntoa (lsa->data->id), lsa);
1483 listnode_add (lsas, lsa);
1484 }
1485
1486 return lsas;
1487}
1488
1489/* Cleanup Update list. */
1490void
1491ospf_upd_list_clean (list lsas)
1492{
1493 listnode node;
1494 struct ospf_lsa *lsa;
1495
1496 for (node = listhead (lsas); node; nextnode (node))
1497 if ((lsa = getdata (node)) != NULL)
1498 ospf_lsa_discard (lsa);
1499
1500 list_delete (lsas);
1501}
1502
1503/* OSPF Link State Update message read -- RFC2328 Section 13. */
1504void
1505ospf_ls_upd (struct ip *iph, struct ospf_header *ospfh,
1506 struct stream *s, struct ospf_interface *oi, u_int16_t size)
1507{
1508 struct ospf_neighbor *nbr;
1509 list lsas;
1510#ifdef HAVE_OPAQUE_LSA
1511 list mylsa_acks, mylsa_upds;
1512#endif /* HAVE_OPAQUE_LSA */
1513 listnode node, next;
1514 struct ospf_lsa *lsa = NULL;
1515 /* unsigned long ls_req_found = 0; */
1516
1517 /* Dis-assemble the stream, update each entry, re-encapsulate for flooding */
1518
1519 /* Increment statistics. */
1520 oi->ls_upd_in++;
1521
1522 /* Check neighbor. */
1523 nbr = ospf_nbr_lookup_by_addr (oi->nbrs, &iph->ip_src);
1524 if (nbr == NULL)
1525 {
1526 zlog_warn ("Link State Update: Unknown Neighbor %s on int: %s",
1527 inet_ntoa (ospfh->router_id), IF_NAME (oi));
1528 return;
1529 }
1530
1531 /* Check neighbor state. */
1532 if (nbr->state < NSM_Exchange)
1533 {
1534 zlog_warn ("Link State Update: Neighbor[%s] state is less than Exchange",
1535 inet_ntoa (ospfh->router_id));
1536 return;
1537 }
1538
1539 /* Get list of LSAs from Link State Update packet. - Also perorms Stages
1540 * 1 (validate LSA checksum) and 2 (check for LSA consistent type)
1541 * of section 13.
1542 */
1543 lsas = ospf_ls_upd_list_lsa (nbr, s, oi, size);
1544
1545#ifdef HAVE_OPAQUE_LSA
1546 /*
1547 * Prepare two kinds of lists to clean up unwanted self-originated
1548 * Opaque-LSAs from the routing domain as soon as possible.
1549 */
1550 mylsa_acks = list_new (); /* Let the sender cease retransmission. */
1551 mylsa_upds = list_new (); /* Flush target LSAs if necessary. */
1552
1553 /*
1554 * If self-originated Opaque-LSAs that have flooded before restart
1555 * are contained in the received LSUpd message, corresponding LSReq
1556 * messages to be sent may have to be modified.
1557 * To eliminate possible race conditions such that flushing and normal
1558 * updating for the same LSA would take place alternately, this trick
1559 * must be done before entering to the loop below.
1560 */
1561 ospf_opaque_adjust_lsreq (nbr, lsas);
1562#endif /* HAVE_OPAQUE_LSA */
1563
1564#define DISCARD_LSA(L,N) {\
1565 if (IS_DEBUG_OSPF_EVENT) \
1566 zlog_info ("ospf_lsa_discard() in ospf_ls_upd() point %d: lsa %p Type-%d", N, lsa, (int) lsa->data->type); \
1567 ospf_lsa_discard (L); \
1568 continue; }
1569
1570 /* Process each LSA received in the one packet. */
1571 for (node = listhead (lsas); node; node = next)
1572 {
1573 struct ospf_lsa *ls_ret, *current;
1574 int ret = 1;
1575
1576 next = node->next;
1577
1578 lsa = getdata (node);
1579
1580#ifdef HAVE_NSSA
1581 if (IS_DEBUG_OSPF_NSSA)
1582 {
1583 char buf1[INET_ADDRSTRLEN];
1584 char buf2[INET_ADDRSTRLEN];
1585 char buf3[INET_ADDRSTRLEN];
1586
1587 zlog_info("LSA Type-%d from %s, ID: %s, ADV: %s",
1588 lsa->data->type,
1589 inet_ntop (AF_INET, &ospfh->router_id,
1590 buf1, INET_ADDRSTRLEN),
1591 inet_ntop (AF_INET, &lsa->data->id,
1592 buf2, INET_ADDRSTRLEN),
1593 inet_ntop (AF_INET, &lsa->data->adv_router,
1594 buf3, INET_ADDRSTRLEN));
1595 }
1596#endif /* HAVE_NSSA */
1597
1598 listnode_delete (lsas, lsa); /* We don't need it in list anymore */
1599
1600 /* Validate Checksum - Done above by ospf_ls_upd_list_lsa() */
1601
1602 /* LSA Type - Done above by ospf_ls_upd_list_lsa() */
1603
1604 /* Do not take in AS External LSAs if we are a stub or NSSA. */
1605
1606 /* Do not take in AS NSSA if this neighbor and we are not NSSA */
1607
1608 /* Do take in Type-7's if we are an NSSA */
1609
1610 /* If we are also an ABR, later translate them to a Type-5 packet */
1611
1612 /* Later, an NSSA Re-fresh can Re-fresh Type-7's and an ABR will
1613 translate them to a separate Type-5 packet. */
1614
1615 if (lsa->data->type == OSPF_AS_EXTERNAL_LSA)
1616 /* Reject from STUB or NSSA */
1617 if (nbr->oi->area->external_routing != OSPF_AREA_DEFAULT)
1618 {
1619 DISCARD_LSA (lsa, 1);
1620#ifdef HAVE_NSSA
1621 if (IS_DEBUG_OSPF_NSSA)
1622 zlog_info("Incoming External LSA Discarded: We are NSSA/STUB Area");
1623#endif /* HAVE_NSSA */
1624 }
1625
1626#ifdef HAVE_NSSA
1627 if (lsa->data->type == OSPF_AS_NSSA_LSA)
1628 if (nbr->oi->area->external_routing != OSPF_AREA_NSSA)
1629 {
1630 DISCARD_LSA (lsa,2);
1631 if (IS_DEBUG_OSPF_NSSA)
1632 zlog_info("Incoming NSSA LSA Discarded: Not NSSA Area");
1633 }
1634#endif /* HAVE_NSSA */
1635
1636 /* Find the LSA in the current database. */
1637
1638 current = ospf_lsa_lookup_by_header (oi->area, lsa->data);
1639
1640 /* If the LSA's LS age is equal to MaxAge, and there is currently
1641 no instance of the LSA in the router's link state database,
1642 and none of router's neighbors are in states Exchange or Loading,
1643 then take the following actions. */
1644
1645 if (IS_LSA_MAXAGE (lsa) && !current &&
paul68980082003-03-25 05:07:42 +00001646 (ospf_nbr_count (oi, NSM_Exchange) +
1647 ospf_nbr_count (oi, NSM_Loading)) == 0)
paul718e3742002-12-13 20:15:29 +00001648 {
1649 /* Response Link State Acknowledgment. */
1650 ospf_ls_ack_send (nbr, lsa);
1651
1652 /* Discard LSA. */
1653 zlog_warn ("Link State Update: LS age is equal to MaxAge.");
1654 DISCARD_LSA (lsa, 3);
1655 }
1656
1657#ifdef HAVE_OPAQUE_LSA
1658 if (IS_OPAQUE_LSA (lsa->data->type)
paul68980082003-03-25 05:07:42 +00001659 && IPV4_ADDR_SAME (&lsa->data->adv_router, &oi->ospf->router_id))
paul718e3742002-12-13 20:15:29 +00001660 {
1661 /*
1662 * Even if initial flushing seems to be completed, there might
1663 * be a case that self-originated LSA with MaxAge still remain
1664 * in the routing domain.
1665 * Just send an LSAck message to cease retransmission.
1666 */
1667 if (IS_LSA_MAXAGE (lsa))
1668 {
1669 zlog_warn ("LSA[%s]: Boomerang effect?", dump_lsa_key (lsa));
1670 ospf_ls_ack_send (nbr, lsa);
1671 ospf_lsa_discard (lsa);
1672
1673 if (current != NULL && ! IS_LSA_MAXAGE (current))
1674 ospf_opaque_lsa_refresh_schedule (current);
1675 continue;
1676 }
1677
1678 /*
1679 * If an instance of self-originated Opaque-LSA is not found
1680 * in the LSDB, there are some possible cases here.
1681 *
1682 * 1) This node lost opaque-capability after restart.
1683 * 2) Else, a part of opaque-type is no more supported.
1684 * 3) Else, a part of opaque-id is no more supported.
1685 *
1686 * Anyway, it is still this node's responsibility to flush it.
1687 * Otherwise, the LSA instance remains in the routing domain
1688 * until its age reaches to MaxAge.
1689 */
1690 if (current == NULL)
1691 {
1692 if (IS_DEBUG_OSPF_EVENT)
1693 zlog_info ("LSA[%s]: Previously originated Opaque-LSA, not found in the LSDB.", dump_lsa_key (lsa));
1694
1695 SET_FLAG (lsa->flags, OSPF_LSA_SELF);
1696 listnode_add (mylsa_upds, ospf_lsa_dup (lsa));
1697 listnode_add (mylsa_acks, ospf_lsa_lock (lsa));
1698 continue;
1699 }
1700 }
1701#endif /* HAVE_OPAQUE_LSA */
hassocb05eb22004-02-11 21:10:19 +00001702 /* It might be happen that received LSA is self-originated network LSA, but
1703 * router ID is cahnged. So, we should check if LSA is a network-LSA whose
1704 * Link State ID is one of the router's own IP interface addresses but whose
1705 * Advertising Router is not equal to the router's own Router ID
1706 * According to RFC 2328 12.4.2 and 13.4 this LSA should be flushed.
1707 */
1708
1709 if(lsa->data->type == OSPF_NETWORK_LSA)
1710 {
1711 listnode oi_node;
1712 int Flag = 0;
1713
1714 for(oi_node = listhead(oi->ospf->oiflist); oi_node; oi_node = nextnode(oi_node))
1715 {
1716 struct ospf_interface *out_if = getdata(oi_node);
1717 if(out_if == NULL)
1718 break;
1719
1720 if((IPV4_ADDR_SAME(&out_if->address->u.prefix4, &lsa->data->id)) &&
1721 (!(IPV4_ADDR_SAME(&oi->ospf->router_id, &lsa->data->adv_router))))
1722 {
1723 if(out_if->network_lsa_self)
1724 {
1725 ospf_lsa_flush_area(lsa,out_if->area);
1726 if(IS_DEBUG_OSPF_EVENT)
1727 zlog_info ("ospf_lsa_discard() in ospf_ls_upd() point 9: lsa %p Type-%d",
1728 lsa, (int) lsa->data->type);
1729 ospf_lsa_discard (lsa);
1730 Flag = 1;
1731 }
1732 break;
1733 }
1734 }
1735 if(Flag)
1736 continue;
1737 }
paul718e3742002-12-13 20:15:29 +00001738
1739 /* (5) Find the instance of this LSA that is currently contained
1740 in the router's link state database. If there is no
1741 database copy, or the received LSA is more recent than
1742 the database copy the following steps must be performed. */
1743
1744 if (current == NULL ||
1745 (ret = ospf_lsa_more_recent (current, lsa)) < 0)
1746 {
1747 /* Actual flooding procedure. */
paul68980082003-03-25 05:07:42 +00001748 if (ospf_flood (oi->ospf, nbr, current, lsa) < 0) /* Trap NSSA later. */
paul718e3742002-12-13 20:15:29 +00001749 DISCARD_LSA (lsa, 4);
1750 continue;
1751 }
1752
1753 /* (6) Else, If there is an instance of the LSA on the sending
1754 neighbor's Link state request list, an error has occurred in
1755 the Database Exchange process. In this case, restart the
1756 Database Exchange process by generating the neighbor event
1757 BadLSReq for the sending neighbor and stop processing the
1758 Link State Update packet. */
1759
1760 if (ospf_ls_request_lookup (nbr, lsa))
1761 {
1762 OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_BadLSReq);
1763 zlog_warn ("LSA instance exists on Link state request list");
1764
1765 /* Clean list of LSAs. */
1766 ospf_upd_list_clean (lsas);
1767 /* this lsa is not on lsas list already. */
1768 ospf_lsa_discard (lsa);
1769#ifdef HAVE_OPAQUE_LSA
1770 list_delete (mylsa_acks);
1771 list_delete (mylsa_upds);
1772#endif /* HAVE_OPAQUE_LSA */
1773 return;
1774 }
1775
1776 /* If the received LSA is the same instance as the database copy
1777 (i.e., neither one is more recent) the following two steps
1778 should be performed: */
1779
1780 if (ret == 0)
1781 {
1782 /* If the LSA is listed in the Link state retransmission list
1783 for the receiving adjacency, the router itself is expecting
1784 an acknowledgment for this LSA. The router should treat the
1785 received LSA as an acknowledgment by removing the LSA from
1786 the Link state retransmission list. This is termed an
1787 "implied acknowledgment". */
1788
1789 ls_ret = ospf_ls_retransmit_lookup (nbr, lsa);
1790
1791 if (ls_ret != NULL)
1792 {
1793 ospf_ls_retransmit_delete (nbr, ls_ret);
1794
1795 /* Delayed acknowledgment sent if advertisement received
1796 from Designated Router, otherwise do nothing. */
1797 if (oi->state == ISM_Backup)
1798 if (NBR_IS_DR (nbr))
1799 listnode_add (oi->ls_ack, ospf_lsa_lock (lsa));
1800
1801 DISCARD_LSA (lsa, 5);
1802 }
1803 else
1804 /* Acknowledge the receipt of the LSA by sending a
1805 Link State Acknowledgment packet back out the receiving
1806 interface. */
1807 {
1808 ospf_ls_ack_send (nbr, lsa);
1809 DISCARD_LSA (lsa, 6);
1810 }
1811 }
1812
1813 /* The database copy is more recent. If the database copy
1814 has LS age equal to MaxAge and LS sequence number equal to
1815 MaxSequenceNumber, simply discard the received LSA without
1816 acknowledging it. (In this case, the LSA's LS sequence number is
1817 wrapping, and the MaxSequenceNumber LSA must be completely
1818 flushed before any new LSA instance can be introduced). */
1819
1820 else if (ret > 0) /* Database copy is more recent */
1821 {
1822 if (IS_LSA_MAXAGE (current) &&
1823 current->data->ls_seqnum == htonl (OSPF_MAX_SEQUENCE_NUMBER))
1824 {
1825 DISCARD_LSA (lsa, 7);
1826 }
1827 /* Otherwise, as long as the database copy has not been sent in a
1828 Link State Update within the last MinLSArrival seconds, send the
1829 database copy back to the sending neighbor, encapsulated within
1830 a Link State Update Packet. The Link State Update Packet should
1831 be sent directly to the neighbor. In so doing, do not put the
1832 database copy of the LSA on the neighbor's link state
1833 retransmission list, and do not acknowledge the received (less
1834 recent) LSA instance. */
1835 else
1836 {
1837 struct timeval now;
1838
1839 gettimeofday (&now, NULL);
1840
1841 if (tv_cmp (tv_sub (now, current->tv_orig),
1842 int2tv (OSPF_MIN_LS_ARRIVAL)) > 0)
1843 /* Trap NSSA type later.*/
1844 ospf_ls_upd_send_lsa (nbr, current, OSPF_SEND_PACKET_DIRECT);
1845 DISCARD_LSA (lsa, 8);
1846 }
1847 }
1848 }
1849
1850#ifdef HAVE_OPAQUE_LSA
1851 /*
1852 * Now that previously originated Opaque-LSAs those which not yet
1853 * installed into LSDB are captured, take several steps to clear
1854 * them completely from the routing domain, before proceeding to
1855 * origination for the current target Opaque-LSAs.
1856 */
1857 while (listcount (mylsa_acks) > 0)
1858 ospf_ls_ack_send_list (oi, mylsa_acks, nbr->address.u.prefix4);
1859
1860 if (listcount (mylsa_upds) > 0)
1861 ospf_opaque_self_originated_lsa_received (nbr, mylsa_upds);
1862
1863 list_delete (mylsa_upds);
paul683b2262003-03-28 00:43:48 +00001864 list_delete (mylsa_acks);
paul718e3742002-12-13 20:15:29 +00001865#endif /* HAVE_OPAQUE_LSA */
1866
1867 assert (listcount (lsas) == 0);
1868 list_delete (lsas);
1869}
1870
1871/* OSPF Link State Acknowledgment message read -- RFC2328 Section 13.7. */
1872void
1873ospf_ls_ack (struct ip *iph, struct ospf_header *ospfh,
1874 struct stream *s, struct ospf_interface *oi, u_int16_t size)
1875{
1876 struct ospf_neighbor *nbr;
1877#ifdef HAVE_OPAQUE_LSA
1878 list opaque_acks;
1879#endif /* HAVE_OPAQUE_LSA */
1880
1881 /* increment statistics. */
1882 oi->ls_ack_in++;
1883
1884 nbr = ospf_nbr_lookup_by_addr (oi->nbrs, &iph->ip_src);
1885 if (nbr == NULL)
1886 {
1887 zlog_warn ("Link State Acknowledgment: Unknown Neighbor %s.",
1888 inet_ntoa (ospfh->router_id));
1889 return;
1890 }
1891
1892 if (nbr->state < NSM_Exchange)
1893 {
1894 zlog_warn ("Link State Acknowledgment: State is less than Exchange.");
1895 return;
1896 }
1897
1898#ifdef HAVE_OPAQUE_LSA
1899 opaque_acks = list_new ();
1900#endif /* HAVE_OPAQUE_LSA */
1901
1902 while (size >= OSPF_LSA_HEADER_SIZE)
1903 {
1904 struct ospf_lsa *lsa, *lsr;
1905
1906 lsa = ospf_lsa_new ();
1907 lsa->data = (struct lsa_header *) STREAM_PNT (s);
1908
1909 /* lsah = (struct lsa_header *) STREAM_PNT (s); */
1910 size -= OSPF_LSA_HEADER_SIZE;
1911 stream_forward (s, OSPF_LSA_HEADER_SIZE);
1912
1913 if (lsa->data->type < OSPF_MIN_LSA || lsa->data->type >= OSPF_MAX_LSA)
1914 {
1915 lsa->data = NULL;
1916 ospf_lsa_discard (lsa);
1917 continue;
1918 }
1919
1920 lsr = ospf_ls_retransmit_lookup (nbr, lsa);
1921
1922 if (lsr != NULL && lsr->data->ls_seqnum == lsa->data->ls_seqnum)
1923 {
1924#ifdef HAVE_OPAQUE_LSA
1925 /* Keep this LSA entry for later reference. */
1926 if (IS_OPAQUE_LSA (lsr->data->type))
1927 listnode_add (opaque_acks, ospf_lsa_dup (lsr));
1928#endif /* HAVE_OPAQUE_LSA */
1929
1930 ospf_ls_retransmit_delete (nbr, lsr);
1931 }
1932
1933 lsa->data = NULL;
1934 ospf_lsa_discard (lsa);
1935 }
1936
1937#ifdef HAVE_OPAQUE_LSA
1938 if (listcount (opaque_acks) > 0)
1939 ospf_opaque_ls_ack_received (nbr, opaque_acks);
1940
1941 list_delete (opaque_acks);
1942 return;
1943#endif /* HAVE_OPAQUE_LSA */
1944}
1945
1946struct stream *
1947ospf_recv_packet (int fd, struct interface **ifp)
1948{
1949 int ret;
1950 struct ip iph;
1951 u_int16_t ip_len;
1952 struct stream *ibuf;
1953 unsigned int ifindex = 0;
1954 struct iovec iov;
1955 struct cmsghdr *cmsg;
1956#if defined (IP_PKTINFO)
1957 struct in_pktinfo *pktinfo;
1958#elif defined (IP_RECVIF)
1959 struct sockaddr_dl *pktinfo;
1960#else
1961 char *pktinfo; /* dummy */
1962#endif
1963 char buff [sizeof (*cmsg) + sizeof (*pktinfo)];
1964 struct msghdr msgh = {NULL, 0, &iov, 1, buff,
1965 sizeof (*cmsg) + sizeof (*pktinfo), 0};
1966
1967 ret = recvfrom (fd, (void *)&iph, sizeof (iph), MSG_PEEK, NULL, 0);
1968
1969 if (ret != sizeof (iph))
1970 {
1971 zlog_warn ("ospf_recv_packet packet smaller than ip header");
1972 return NULL;
1973 }
1974
paul239aecc2003-12-08 10:34:54 +00001975#if defined(__NetBSD__) || defined(__FreeBSD__) || (defined(__OpenBSD__) && (OpenBSD < 200311))
paul718e3742002-12-13 20:15:29 +00001976 ip_len = iph.ip_len;
1977#else
1978 ip_len = ntohs (iph.ip_len);
1979#endif
1980
paul239aecc2003-12-08 10:34:54 +00001981#if !defined(GNU_LINUX) && (OpenBSD < 200311)
paul718e3742002-12-13 20:15:29 +00001982 /*
1983 * Kernel network code touches incoming IP header parameters,
1984 * before protocol specific processing.
1985 *
1986 * 1) Convert byteorder to host representation.
1987 * --> ip_len, ip_id, ip_off
1988 *
1989 * 2) Adjust ip_len to strip IP header size!
1990 * --> If user process receives entire IP packet via RAW
1991 * socket, it must consider adding IP header size to
1992 * the "ip_len" field of "ip" structure.
1993 *
1994 * For more details, see <netinet/ip_input.c>.
1995 */
1996 ip_len = ip_len + (iph.ip_hl << 2);
1997#endif
1998
1999 ibuf = stream_new (ip_len);
2000 iov.iov_base = STREAM_DATA (ibuf);
2001 iov.iov_len = ip_len;
2002 ret = recvmsg (fd, &msgh, 0);
2003
2004 cmsg = CMSG_FIRSTHDR (&msgh);
2005
2006 if (cmsg != NULL && //cmsg->cmsg_len == sizeof (*pktinfo) &&
2007 cmsg->cmsg_level == IPPROTO_IP &&
2008#if defined (IP_PKTINFO)
2009 cmsg->cmsg_type == IP_PKTINFO
2010#elif defined (IP_RECVIF)
2011 cmsg->cmsg_type == IP_RECVIF
2012#else
2013 0
2014#endif
2015 )
2016 {
2017#if defined (IP_PKTINFO)
2018 pktinfo = (struct in_pktinfo *)CMSG_DATA(cmsg);
2019 ifindex = pktinfo->ipi_ifindex;
2020#elif defined (IP_RECVIF)
2021 pktinfo = (struct sockaddr_dl *)CMSG_DATA(cmsg);
2022 ifindex = pktinfo->sdl_index;
2023#else
2024 ifindex = 0;
2025#endif
2026 }
2027
2028 *ifp = if_lookup_by_index (ifindex);
2029
2030 if (ret != ip_len)
2031 {
2032 zlog_warn ("ospf_recv_packet short read. "
2033 "ip_len %d bytes read %d", ip_len, ret);
2034 stream_free (ibuf);
2035 return NULL;
2036 }
2037
2038 return ibuf;
2039}
2040
2041struct ospf_interface *
paul020709f2003-04-04 02:44:16 +00002042ospf_associate_packet_vl (struct ospf *ospf,
2043 struct interface *ifp, struct ospf_interface *oi,
paul718e3742002-12-13 20:15:29 +00002044 struct ip *iph, struct ospf_header *ospfh)
2045{
2046 struct ospf_interface *rcv_oi;
paul718e3742002-12-13 20:15:29 +00002047 struct ospf_vl_data *vl_data;
2048 struct ospf_area *vl_area;
paul68980082003-03-25 05:07:42 +00002049 listnode node;
paul718e3742002-12-13 20:15:29 +00002050
2051 if (IN_MULTICAST (ntohl (iph->ip_dst.s_addr)) ||
2052 !OSPF_IS_AREA_BACKBONE (ospfh))
2053 return oi;
2054
2055 if ((rcv_oi = oi) == NULL)
2056 {
paul2ca35dd2003-09-23 23:20:08 +00002057 if ((rcv_oi = ospf_if_lookup_by_local_addr (ospf, NULL,
2058 iph->ip_dst)) == NULL)
paul718e3742002-12-13 20:15:29 +00002059 return NULL;
2060 }
2061
paul020709f2003-04-04 02:44:16 +00002062 for (node = listhead (ospf->vlinks); node; nextnode (node))
paul718e3742002-12-13 20:15:29 +00002063 {
2064 if ((vl_data = getdata (node)) == NULL)
2065 continue;
2066
paul020709f2003-04-04 02:44:16 +00002067 vl_area = ospf_area_lookup_by_area_id (ospf, vl_data->vl_area_id);
paul718e3742002-12-13 20:15:29 +00002068 if (!vl_area)
2069 continue;
2070
2071 if (OSPF_AREA_SAME (&vl_area, &rcv_oi->area) &&
2072 IPV4_ADDR_SAME (&vl_data->vl_peer, &ospfh->router_id))
2073 {
2074 if (IS_DEBUG_OSPF_EVENT)
2075 zlog_info ("associating packet with %s",
2076 IF_NAME (vl_data->vl_oi));
2077 if (! CHECK_FLAG (vl_data->vl_oi->ifp->flags, IFF_UP))
2078 {
2079 if (IS_DEBUG_OSPF_EVENT)
2080 zlog_info ("This VL is not up yet, sorry");
2081 return NULL;
2082 }
2083
2084 return vl_data->vl_oi;
2085 }
2086 }
2087
2088 if (IS_DEBUG_OSPF_EVENT)
2089 zlog_info ("couldn't find any VL to associate the packet with");
2090
2091 return oi;
2092}
2093
2094int
2095ospf_check_area_id (struct ospf_interface *oi, struct ospf_header *ospfh)
2096{
2097 /* Check match the Area ID of the receiving interface. */
2098 if (OSPF_AREA_SAME (&oi->area, &ospfh))
2099 return 1;
2100
2101 return 0;
2102}
2103
2104/* Unbound socket will accept any Raw IP packets if proto is matched.
2105 To prevent it, compare src IP address and i/f address with masking
2106 i/f network mask. */
2107int
2108ospf_check_network_mask (struct ospf_interface *oi, struct in_addr ip_src)
2109{
2110 struct in_addr mask, me, him;
2111
2112 if (oi->type == OSPF_IFTYPE_POINTOPOINT ||
2113 oi->type == OSPF_IFTYPE_VIRTUALLINK)
2114 return 1;
2115
2116 masklen2ip (oi->address->prefixlen, &mask);
2117
2118 me.s_addr = oi->address->u.prefix4.s_addr & mask.s_addr;
2119 him.s_addr = ip_src.s_addr & mask.s_addr;
2120
2121 if (IPV4_ADDR_SAME (&me, &him))
2122 return 1;
2123
2124 return 0;
2125}
2126
2127int
2128ospf_check_auth (struct ospf_interface *oi, struct stream *ibuf,
2129 struct ospf_header *ospfh)
2130{
2131 int ret = 0;
2132 struct crypt_key *ck;
2133
2134 switch (ntohs (ospfh->auth_type))
2135 {
2136 case OSPF_AUTH_NULL:
2137 ret = 1;
2138 break;
2139 case OSPF_AUTH_SIMPLE:
2140 if (!memcmp (OSPF_IF_PARAM (oi, auth_simple), ospfh->u.auth_data, OSPF_AUTH_SIMPLE_SIZE))
2141 ret = 1;
2142 else
2143 ret = 0;
2144 break;
2145 case OSPF_AUTH_CRYPTOGRAPHIC:
2146 if ((ck = getdata (OSPF_IF_PARAM (oi,auth_crypt)->tail)) == NULL)
2147 {
2148 ret = 0;
2149 break;
2150 }
2151
2152 /* This is very basic, the digest processing is elsewhere */
2153 if (ospfh->u.crypt.auth_data_len == OSPF_AUTH_MD5_SIZE &&
2154 ospfh->u.crypt.key_id == ck->key_id &&
2155 ntohs (ospfh->length) + OSPF_AUTH_SIMPLE_SIZE <= stream_get_size (ibuf))
2156 ret = 1;
2157 else
2158 ret = 0;
2159 break;
2160 default:
2161 ret = 0;
2162 break;
2163 }
2164
2165 return ret;
2166}
2167
2168int
2169ospf_check_sum (struct ospf_header *ospfh)
2170{
2171 u_int32_t ret;
2172 u_int16_t sum;
2173 int in_cksum (void *ptr, int nbytes);
2174
2175 /* clear auth_data for checksum. */
2176 memset (ospfh->u.auth_data, 0, OSPF_AUTH_SIMPLE_SIZE);
2177
2178 /* keep checksum and clear. */
2179 sum = ospfh->checksum;
2180 memset (&ospfh->checksum, 0, sizeof (u_int16_t));
2181
2182 /* calculate checksum. */
2183 ret = in_cksum (ospfh, ntohs (ospfh->length));
2184
2185 if (ret != sum)
2186 {
2187 zlog_info ("ospf_check_sum(): checksum mismatch, my %X, his %X",
2188 ret, sum);
2189 return 0;
2190 }
2191
2192 return 1;
2193}
2194
2195/* OSPF Header verification. */
2196int
2197ospf_verify_header (struct stream *ibuf, struct ospf_interface *oi,
2198 struct ip *iph, struct ospf_header *ospfh)
2199{
2200 /* check version. */
2201 if (ospfh->version != OSPF_VERSION)
2202 {
2203 zlog_warn ("interface %s: ospf_read version number mismatch.",
2204 IF_NAME (oi));
2205 return -1;
2206 }
2207
2208 /* Check Area ID. */
2209 if (!ospf_check_area_id (oi, ospfh))
2210 {
2211 zlog_warn ("interface %s: ospf_read invalid Area ID %s.",
2212 IF_NAME (oi), inet_ntoa (ospfh->area_id));
2213 return -1;
2214 }
2215
2216 /* Check network mask, Silently discarded. */
2217 if (! ospf_check_network_mask (oi, iph->ip_src))
2218 {
2219 zlog_warn ("interface %s: ospf_read network address is not same [%s]",
2220 IF_NAME (oi), inet_ntoa (iph->ip_src));
2221 return -1;
2222 }
2223
2224 /* Check authentication. */
2225 if (ospf_auth_type (oi) != ntohs (ospfh->auth_type))
2226 {
2227 zlog_warn ("interface %s: ospf_read authentication type mismatch.",
2228 IF_NAME (oi));
2229 return -1;
2230 }
2231
2232 if (! ospf_check_auth (oi, ibuf, ospfh))
2233 {
2234 zlog_warn ("interface %s: ospf_read authentication failed.",
2235 IF_NAME (oi));
2236 return -1;
2237 }
2238
2239 /* if check sum is invalid, packet is discarded. */
2240 if (ntohs (ospfh->auth_type) != OSPF_AUTH_CRYPTOGRAPHIC)
2241 {
2242 if (! ospf_check_sum (ospfh))
2243 {
2244 zlog_warn ("interface %s: ospf_read packet checksum error %s",
2245 IF_NAME (oi), inet_ntoa (ospfh->router_id));
2246 return -1;
2247 }
2248 }
2249 else
2250 {
2251 if (ospfh->checksum != 0)
2252 return -1;
2253 if (ospf_check_md5_digest (oi, ibuf, ntohs (ospfh->length)) == 0)
2254 {
2255 zlog_warn ("interface %s: ospf_read md5 authentication failed.",
2256 IF_NAME (oi));
2257 return -1;
2258 }
2259 }
2260
2261 return 0;
2262}
2263
2264/* Starting point of packet process function. */
2265int
2266ospf_read (struct thread *thread)
2267{
2268 int ret;
2269 struct stream *ibuf;
paul68980082003-03-25 05:07:42 +00002270 struct ospf *ospf;
paul718e3742002-12-13 20:15:29 +00002271 struct ospf_interface *oi;
2272 struct ip *iph;
2273 struct ospf_header *ospfh;
2274 u_int16_t length;
2275 struct interface *ifp;
2276
2277 /* first of all get interface pointer. */
paul68980082003-03-25 05:07:42 +00002278 ospf = THREAD_ARG (thread);
2279 ospf->t_read = NULL;
paul718e3742002-12-13 20:15:29 +00002280
2281 /* read OSPF packet. */
paul68980082003-03-25 05:07:42 +00002282 ibuf = ospf_recv_packet (ospf->fd, &ifp);
paul718e3742002-12-13 20:15:29 +00002283 if (ibuf == NULL)
2284 return -1;
2285
2286 iph = (struct ip *) STREAM_DATA (ibuf);
2287
2288 /* prepare for next packet. */
paul68980082003-03-25 05:07:42 +00002289 ospf->t_read = thread_add_read (master, ospf_read, ospf, ospf->fd);
paul718e3742002-12-13 20:15:29 +00002290
2291 /* IP Header dump. */
paul17b78d32003-02-13 22:04:01 +00002292 if (IS_DEBUG_OSPF_PACKET(0, RECV))
paul7d95c612003-01-27 12:00:55 +00002293 ospf_ip_header_dump (ibuf);
2294
paul718e3742002-12-13 20:15:29 +00002295 /* Self-originated packet should be discarded silently. */
paul68980082003-03-25 05:07:42 +00002296 if (ospf_if_lookup_by_local_addr (ospf, NULL, iph->ip_src))
paul718e3742002-12-13 20:15:29 +00002297 {
pauld3241812003-09-29 12:42:39 +00002298 if (IS_DEBUG_OSPF_PACKET (0, RECV))
2299 {
2300 zlog_info ("ospf_read[%s]: Dropping self-originated packet",
2301 inet_ntoa (iph->ip_src));
2302 }
paul718e3742002-12-13 20:15:29 +00002303 stream_free (ibuf);
2304 return 0;
2305 }
2306
2307 /* Adjust size to message length. */
2308 stream_forward (ibuf, iph->ip_hl * 4);
2309
2310 /* Get ospf packet header. */
2311 ospfh = (struct ospf_header *) STREAM_PNT (ibuf);
2312
2313 /* associate packet with ospf interface */
paul68980082003-03-25 05:07:42 +00002314 oi = ospf_if_lookup_recv_if (ospf, iph->ip_src);
paul718e3742002-12-13 20:15:29 +00002315 if (ifp && oi && oi->ifp != ifp)
2316 {
2317 zlog_warn ("Packet from [%s] received on wrong link %s",
pauld3241812003-09-29 12:42:39 +00002318 inet_ntoa (iph->ip_src), ifp->name);
paul718e3742002-12-13 20:15:29 +00002319 stream_free (ibuf);
2320 return 0;
2321 }
2322
paul020709f2003-04-04 02:44:16 +00002323 if ((oi = ospf_associate_packet_vl (ospf, ifp, oi, iph, ospfh)) == NULL)
paul718e3742002-12-13 20:15:29 +00002324 {
pauld3241812003-09-29 12:42:39 +00002325 if (IS_DEBUG_OSPF_PACKET (ospfh->type - 1, RECV))
2326 {
2327 zlog_info ("ospf_read[%s/%s]: Could not associate packet with VL, "
2328 "dropping.",
2329 ospf_packet_type_str[ospfh->type],
2330 inet_ntoa (iph->ip_src));
2331 }
paul718e3742002-12-13 20:15:29 +00002332 stream_free (ibuf);
2333 return 0;
2334 }
2335
2336 /*
2337 * If the received packet is destined for AllDRouters, the packet
2338 * should be accepted only if the received ospf interface state is
2339 * either DR or Backup -- endo.
2340 */
2341 if (iph->ip_dst.s_addr == htonl (OSPF_ALLDROUTERS)
2342 && (oi->state != ISM_DR && oi->state != ISM_Backup))
2343 {
2344 zlog_info ("Packet for AllDRouters from [%s] via [%s] (ISM: %s)",
2345 inet_ntoa (iph->ip_src), IF_NAME (oi),
2346 LOOKUP (ospf_ism_state_msg, oi->state));
2347 stream_free (ibuf);
2348 return 0;
2349 }
2350
2351 /* Show debug receiving packet. */
paul1aa7b392003-04-08 08:51:58 +00002352 if (IS_DEBUG_OSPF_PACKET (ospfh->type - 1, RECV))
2353 {
paul718e3742002-12-13 20:15:29 +00002354 if (IS_DEBUG_OSPF_PACKET (ospfh->type - 1, DETAIL))
paul1aa7b392003-04-08 08:51:58 +00002355 {
2356 zlog_info ("-----------------------------------------------------");
2357 ospf_packet_dump (ibuf);
2358 }
paul718e3742002-12-13 20:15:29 +00002359
2360 zlog_info ("%s received from [%s] via [%s]",
paul1aa7b392003-04-08 08:51:58 +00002361 ospf_packet_type_str[ospfh->type],
2362 inet_ntoa (ospfh->router_id), IF_NAME (oi));
paul718e3742002-12-13 20:15:29 +00002363 zlog_info (" src [%s],", inet_ntoa (iph->ip_src));
2364 zlog_info (" dst [%s]", inet_ntoa (iph->ip_dst));
2365
2366 if (IS_DEBUG_OSPF_PACKET (ospfh->type - 1, DETAIL))
2367 zlog_info ("-----------------------------------------------------");
paul1aa7b392003-04-08 08:51:58 +00002368 }
paul718e3742002-12-13 20:15:29 +00002369
2370 /* Some header verification. */
2371 ret = ospf_verify_header (ibuf, oi, iph, ospfh);
2372 if (ret < 0)
2373 {
pauld3241812003-09-29 12:42:39 +00002374 if (IS_DEBUG_OSPF_PACKET (ospfh->type - 1, RECV))
2375 {
2376 zlog_info ("ospf_read[%s/%s]: Header check failed, "
2377 "dropping.",
2378 ospf_packet_type_str[ospfh->type],
2379 inet_ntoa (iph->ip_src));
2380 }
paul718e3742002-12-13 20:15:29 +00002381 stream_free (ibuf);
2382 return ret;
2383 }
2384
2385 stream_forward (ibuf, OSPF_HEADER_SIZE);
2386
2387 /* Adjust size to message length. */
2388 length = ntohs (ospfh->length) - OSPF_HEADER_SIZE;
2389
2390 /* Read rest of the packet and call each sort of packet routine. */
2391 switch (ospfh->type)
2392 {
2393 case OSPF_MSG_HELLO:
2394 ospf_hello (iph, ospfh, ibuf, oi, length);
2395 break;
2396 case OSPF_MSG_DB_DESC:
2397 ospf_db_desc (iph, ospfh, ibuf, oi, length);
2398 break;
2399 case OSPF_MSG_LS_REQ:
2400 ospf_ls_req (iph, ospfh, ibuf, oi, length);
2401 break;
2402 case OSPF_MSG_LS_UPD:
2403 ospf_ls_upd (iph, ospfh, ibuf, oi, length);
2404 break;
2405 case OSPF_MSG_LS_ACK:
2406 ospf_ls_ack (iph, ospfh, ibuf, oi, length);
2407 break;
2408 default:
2409 zlog (NULL, LOG_WARNING,
2410 "interface %s: OSPF packet header type %d is illegal",
2411 IF_NAME (oi), ospfh->type);
2412 break;
2413 }
2414
2415 stream_free (ibuf);
2416 return 0;
2417}
2418
2419/* Make OSPF header. */
2420void
2421ospf_make_header (int type, struct ospf_interface *oi, struct stream *s)
2422{
2423 struct ospf_header *ospfh;
2424
2425 ospfh = (struct ospf_header *) STREAM_DATA (s);
2426
2427 ospfh->version = (u_char) OSPF_VERSION;
2428 ospfh->type = (u_char) type;
2429
paul68980082003-03-25 05:07:42 +00002430 ospfh->router_id = oi->ospf->router_id;
paul718e3742002-12-13 20:15:29 +00002431
2432 ospfh->checksum = 0;
2433 ospfh->area_id = oi->area->area_id;
2434 ospfh->auth_type = htons (ospf_auth_type (oi));
2435
2436 memset (ospfh->u.auth_data, 0, OSPF_AUTH_SIMPLE_SIZE);
2437
2438 ospf_output_forward (s, OSPF_HEADER_SIZE);
2439}
2440
2441/* Make Authentication Data. */
2442int
2443ospf_make_auth (struct ospf_interface *oi, struct ospf_header *ospfh)
2444{
2445 struct crypt_key *ck;
2446
2447 switch (ospf_auth_type (oi))
2448 {
2449 case OSPF_AUTH_NULL:
2450 /* memset (ospfh->u.auth_data, 0, sizeof (ospfh->u.auth_data)); */
2451 break;
2452 case OSPF_AUTH_SIMPLE:
2453 memcpy (ospfh->u.auth_data, OSPF_IF_PARAM (oi, auth_simple),
2454 OSPF_AUTH_SIMPLE_SIZE);
2455 break;
2456 case OSPF_AUTH_CRYPTOGRAPHIC:
2457 /* If key is not set, then set 0. */
2458 if (list_isempty (OSPF_IF_PARAM (oi, auth_crypt)))
2459 {
2460 ospfh->u.crypt.zero = 0;
2461 ospfh->u.crypt.key_id = 0;
2462 ospfh->u.crypt.auth_data_len = OSPF_AUTH_MD5_SIZE;
2463 }
2464 else
2465 {
2466 ck = getdata (OSPF_IF_PARAM (oi, auth_crypt)->tail);
2467 ospfh->u.crypt.zero = 0;
2468 ospfh->u.crypt.key_id = ck->key_id;
2469 ospfh->u.crypt.auth_data_len = OSPF_AUTH_MD5_SIZE;
2470 }
2471 /* note: the seq is done in ospf_make_md5_digest() */
2472 break;
2473 default:
2474 /* memset (ospfh->u.auth_data, 0, sizeof (ospfh->u.auth_data)); */
2475 break;
2476 }
2477
2478 return 0;
2479}
2480
2481/* Fill rest of OSPF header. */
2482void
2483ospf_fill_header (struct ospf_interface *oi,
2484 struct stream *s, u_int16_t length)
2485{
2486 struct ospf_header *ospfh;
2487
2488 ospfh = (struct ospf_header *) STREAM_DATA (s);
2489
2490 /* Fill length. */
2491 ospfh->length = htons (length);
2492
2493 /* Calculate checksum. */
2494 if (ntohs (ospfh->auth_type) != OSPF_AUTH_CRYPTOGRAPHIC)
2495 ospfh->checksum = in_cksum (ospfh, length);
2496 else
2497 ospfh->checksum = 0;
2498
2499 /* Add Authentication Data. */
2500 ospf_make_auth (oi, ospfh);
2501}
2502
2503int
2504ospf_make_hello (struct ospf_interface *oi, struct stream *s)
2505{
2506 struct ospf_neighbor *nbr;
2507 struct route_node *rn;
2508 u_int16_t length = OSPF_HELLO_MIN_SIZE;
2509 struct in_addr mask;
2510 unsigned long p;
2511 int flag = 0;
2512
2513 /* Set netmask of interface. */
2514 if (oi->type != OSPF_IFTYPE_POINTOPOINT &&
2515 oi->type != OSPF_IFTYPE_VIRTUALLINK)
2516 masklen2ip (oi->address->prefixlen, &mask);
2517 else
2518 memset ((char *) &mask, 0, sizeof (struct in_addr));
2519 stream_put_ipv4 (s, mask.s_addr);
2520
2521 /* Set Hello Interval. */
2522 stream_putw (s, OSPF_IF_PARAM (oi, v_hello));
2523
2524 if (IS_DEBUG_OSPF_EVENT)
2525 zlog_info ("make_hello: options: %x, int: %s",
2526 OPTIONS(oi), IF_NAME (oi));
2527
2528 /* Set Options. */
2529 stream_putc (s, OPTIONS (oi));
2530
2531 /* Set Router Priority. */
2532 stream_putc (s, PRIORITY (oi));
2533
2534 /* Set Router Dead Interval. */
2535 stream_putl (s, OSPF_IF_PARAM (oi, v_wait));
2536
2537 /* Set Designated Router. */
2538 stream_put_ipv4 (s, DR (oi).s_addr);
2539
2540 p = s->putp;
2541
2542 /* Set Backup Designated Router. */
2543 stream_put_ipv4 (s, BDR (oi).s_addr);
2544
2545 /* Add neighbor seen. */
2546 for (rn = route_top (oi->nbrs); rn; rn = route_next (rn))
paul68980082003-03-25 05:07:42 +00002547 if ((nbr = rn->info))
2548 if (nbr->router_id.s_addr != 0) /* Ignore 0.0.0.0 node. */
2549 if (nbr->state != NSM_Attempt) /* Ignore Down neighbor. */
2550 if (nbr->state != NSM_Down) /* This is myself for DR election. */
2551 if (!IPV4_ADDR_SAME (&nbr->router_id, &oi->ospf->router_id))
paul718e3742002-12-13 20:15:29 +00002552 {
2553 /* Check neighbor is sane? */
paul68980082003-03-25 05:07:42 +00002554 if (nbr->d_router.s_addr != 0
2555 && IPV4_ADDR_SAME (&nbr->d_router, &oi->address->u.prefix4)
2556 && IPV4_ADDR_SAME (&nbr->bd_router, &oi->address->u.prefix4))
2557 flag = 1;
paul718e3742002-12-13 20:15:29 +00002558
2559 stream_put_ipv4 (s, nbr->router_id.s_addr);
2560 length += 4;
2561 }
2562
2563 /* Let neighbor generate BackupSeen. */
2564 if (flag == 1)
2565 {
2566 stream_set_putp (s, p);
2567 stream_put_ipv4 (s, 0);
2568 }
2569
2570 return length;
2571}
2572
2573int
2574ospf_make_db_desc (struct ospf_interface *oi, struct ospf_neighbor *nbr,
2575 struct stream *s)
2576{
2577 struct ospf_lsa *lsa;
2578 u_int16_t length = OSPF_DB_DESC_MIN_SIZE;
2579 u_char options;
2580 unsigned long pp;
2581 int i;
2582 struct ospf_lsdb *lsdb;
2583
2584 /* Set Interface MTU. */
2585 if (oi->type == OSPF_IFTYPE_VIRTUALLINK)
2586 stream_putw (s, 0);
2587 else
2588 stream_putw (s, oi->ifp->mtu);
2589
2590 /* Set Options. */
2591 options = OPTIONS (oi);
2592#ifdef HAVE_OPAQUE_LSA
paul68980082003-03-25 05:07:42 +00002593 if (CHECK_FLAG (oi->ospf->config, OSPF_OPAQUE_CAPABLE))
paul718e3742002-12-13 20:15:29 +00002594 {
2595 if (IS_SET_DD_I (nbr->dd_flags)
2596 || CHECK_FLAG (nbr->options, OSPF_OPTION_O))
2597 /*
2598 * Set O-bit in the outgoing DD packet for capablity negotiation,
2599 * if one of following case is applicable.
2600 *
2601 * 1) WaitTimer expiration event triggered the neighbor state to
2602 * change to Exstart, but no (valid) DD packet has received
2603 * from the neighbor yet.
2604 *
2605 * 2) At least one DD packet with O-bit on has received from the
2606 * neighbor.
2607 */
2608 SET_FLAG (options, OSPF_OPTION_O);
2609 }
2610#endif /* HAVE_OPAQUE_LSA */
2611 stream_putc (s, options);
2612
2613 /* Keep pointer to flags. */
2614 pp = stream_get_putp (s);
2615 stream_putc (s, nbr->dd_flags);
2616
2617 /* Set DD Sequence Number. */
2618 stream_putl (s, nbr->dd_seqnum);
2619
2620 if (ospf_db_summary_isempty (nbr))
2621 {
2622 if (nbr->state >= NSM_Exchange)
2623 {
2624 nbr->dd_flags &= ~OSPF_DD_FLAG_M;
2625 /* Set DD flags again */
2626 stream_set_putp (s, pp);
2627 stream_putc (s, nbr->dd_flags);
2628 }
2629 return length;
2630 }
2631
2632 /* Describe LSA Header from Database Summary List. */
2633 lsdb = &nbr->db_sum;
2634
2635 for (i = OSPF_MIN_LSA; i < OSPF_MAX_LSA; i++)
2636 {
2637 struct route_table *table = lsdb->type[i].db;
2638 struct route_node *rn;
2639
2640 for (rn = route_top (table); rn; rn = route_next (rn))
2641 if ((lsa = rn->info) != NULL)
2642 {
2643#ifdef HAVE_OPAQUE_LSA
2644 if (IS_OPAQUE_LSA (lsa->data->type)
2645 && (! CHECK_FLAG (options, OSPF_OPTION_O)))
2646 {
2647 /* Suppress advertising opaque-informations. */
2648 /* Remove LSA from DB summary list. */
2649 ospf_lsdb_delete (lsdb, lsa);
2650 continue;
2651 }
2652#endif /* HAVE_OPAQUE_LSA */
2653
2654 if (!CHECK_FLAG (lsa->flags, OSPF_LSA_DISCARD))
2655 {
2656 struct lsa_header *lsah;
2657 u_int16_t ls_age;
2658
2659 /* DD packet overflows interface MTU. */
2660 if (length + OSPF_LSA_HEADER_SIZE > OSPF_PACKET_MAX (oi))
2661 break;
2662
2663 /* Keep pointer to LS age. */
2664 lsah = (struct lsa_header *) (STREAM_DATA (s) +
2665 stream_get_putp (s));
2666
2667 /* Proceed stream pointer. */
2668 stream_put (s, lsa->data, OSPF_LSA_HEADER_SIZE);
2669 length += OSPF_LSA_HEADER_SIZE;
2670
2671 /* Set LS age. */
2672 ls_age = LS_AGE (lsa);
2673 lsah->ls_age = htons (ls_age);
2674
2675 }
2676
2677 /* Remove LSA from DB summary list. */
2678 ospf_lsdb_delete (lsdb, lsa);
2679 }
2680 }
2681
2682 return length;
2683}
2684
2685int
2686ospf_make_ls_req_func (struct stream *s, u_int16_t *length,
2687 unsigned long delta, struct ospf_neighbor *nbr,
2688 struct ospf_lsa *lsa)
2689{
2690 struct ospf_interface *oi;
2691
2692 oi = nbr->oi;
2693
2694 /* LS Request packet overflows interface MTU. */
2695 if (*length + delta > OSPF_PACKET_MAX(oi))
2696 return 0;
2697
2698 stream_putl (s, lsa->data->type);
2699 stream_put_ipv4 (s, lsa->data->id.s_addr);
2700 stream_put_ipv4 (s, lsa->data->adv_router.s_addr);
2701
2702 ospf_lsa_unlock (nbr->ls_req_last);
2703 nbr->ls_req_last = ospf_lsa_lock (lsa);
2704
2705 *length += 12;
2706 return 1;
2707}
2708
2709int
2710ospf_make_ls_req (struct ospf_neighbor *nbr, struct stream *s)
2711{
2712 struct ospf_lsa *lsa;
2713 u_int16_t length = OSPF_LS_REQ_MIN_SIZE;
2714 unsigned long delta = stream_get_putp(s)+12;
2715 struct route_table *table;
2716 struct route_node *rn;
2717 int i;
2718 struct ospf_lsdb *lsdb;
2719
2720 lsdb = &nbr->ls_req;
2721
2722 for (i = OSPF_MIN_LSA; i < OSPF_MAX_LSA; i++)
2723 {
2724 table = lsdb->type[i].db;
2725 for (rn = route_top (table); rn; rn = route_next (rn))
2726 if ((lsa = (rn->info)) != NULL)
2727 if (ospf_make_ls_req_func (s, &length, delta, nbr, lsa) == 0)
2728 {
2729 route_unlock_node (rn);
2730 break;
2731 }
2732 }
2733 return length;
2734}
2735
2736int
2737ls_age_increment (struct ospf_lsa *lsa, int delay)
2738{
2739 int age;
2740
2741 age = IS_LSA_MAXAGE (lsa) ? OSPF_LSA_MAXAGE : LS_AGE (lsa) + delay;
2742
2743 return (age > OSPF_LSA_MAXAGE ? OSPF_LSA_MAXAGE : age);
2744}
2745
2746int
2747ospf_make_ls_upd (struct ospf_interface *oi, list update, struct stream *s)
2748{
2749 struct ospf_lsa *lsa;
2750 listnode node;
2751 u_int16_t length = OSPF_LS_UPD_MIN_SIZE;
2752 unsigned long delta = stream_get_putp (s);
2753 unsigned long pp;
2754 int count = 0;
2755
2756 if (IS_DEBUG_OSPF_EVENT)
2757 zlog_info("ospf_make_ls_upd: Start");
2758
2759 pp = stream_get_putp (s);
2760 ospf_output_forward (s, 4);
2761
2762 while ((node = listhead (update)) != NULL)
2763 {
2764 struct lsa_header *lsah;
2765 u_int16_t ls_age;
2766
2767 if (IS_DEBUG_OSPF_EVENT)
2768 zlog_info("ospf_make_ls_upd: List Iteration");
2769
2770 lsa = getdata (node);
2771 assert (lsa);
2772 assert (lsa->data);
2773
2774 /* Check packet size. */
2775 if (length + delta + ntohs (lsa->data->length) > OSPF_PACKET_MAX (oi))
paul93fe5c52003-06-30 21:41:26 +00002776 break;
paul718e3742002-12-13 20:15:29 +00002777
2778 /* Keep pointer to LS age. */
2779 lsah = (struct lsa_header *) (STREAM_DATA (s) + stream_get_putp (s));
2780
2781 /* Put LSA to Link State Request. */
2782 stream_put (s, lsa->data, ntohs (lsa->data->length));
2783
2784 /* Set LS age. */
2785 /* each hop must increment an lsa_age by transmit_delay
2786 of OSPF interface */
2787 ls_age = ls_age_increment (lsa, OSPF_IF_PARAM (oi, transmit_delay));
2788 lsah->ls_age = htons (ls_age);
2789
2790 length += ntohs (lsa->data->length);
2791 count++;
2792
2793 list_delete_node (update, node);
2794 ospf_lsa_unlock (lsa);
2795 }
2796
2797 /* Now set #LSAs. */
2798 stream_set_putp (s, pp);
2799 stream_putl (s, count);
2800
2801 stream_set_putp (s, s->endp);
2802
2803 if (IS_DEBUG_OSPF_EVENT)
2804 zlog_info("ospf_make_ls_upd: Stop");
2805 return length;
2806}
2807
2808int
2809ospf_make_ls_ack (struct ospf_interface *oi, list ack, struct stream *s)
2810{
2811 list rm_list;
2812 listnode node;
2813 u_int16_t length = OSPF_LS_ACK_MIN_SIZE;
2814 unsigned long delta = stream_get_putp(s) + 24;
2815 struct ospf_lsa *lsa;
2816
2817 rm_list = list_new ();
2818
2819 for (node = listhead (ack); node; nextnode (node))
2820 {
2821 lsa = getdata (node);
2822 assert (lsa);
2823
2824 if (length + delta > OSPF_PACKET_MAX (oi))
2825 break;
2826
2827 stream_put (s, lsa->data, OSPF_LSA_HEADER_SIZE);
2828 length += OSPF_LSA_HEADER_SIZE;
2829
2830 listnode_add (rm_list, lsa);
2831 }
2832
2833 /* Remove LSA from LS-Ack list. */
2834 for (node = listhead (rm_list); node; nextnode (node))
2835 {
2836 lsa = (struct ospf_lsa *) getdata (node);
2837
2838 listnode_delete (ack, lsa);
2839 ospf_lsa_unlock (lsa);
2840 }
2841
2842 list_delete (rm_list);
2843
2844 return length;
2845}
2846
2847void
2848ospf_hello_send_sub (struct ospf_interface *oi, struct in_addr *addr)
2849{
2850 struct ospf_packet *op;
2851 u_int16_t length = OSPF_HEADER_SIZE;
2852
2853 op = ospf_packet_new (oi->ifp->mtu);
2854
2855 /* Prepare OSPF common header. */
2856 ospf_make_header (OSPF_MSG_HELLO, oi, op->s);
2857
2858 /* Prepare OSPF Hello body. */
2859 length += ospf_make_hello (oi, op->s);
2860
2861 /* Fill OSPF header. */
2862 ospf_fill_header (oi, op->s, length);
2863
2864 /* Set packet length. */
2865 op->length = length;
2866
2867 op->dst.s_addr = addr->s_addr;
2868
2869 /* Add packet to the interface output queue. */
2870 ospf_packet_add (oi, op);
2871
2872 /* Hook thread to write packet. */
paul020709f2003-04-04 02:44:16 +00002873 OSPF_ISM_WRITE_ON (oi->ospf);
paul718e3742002-12-13 20:15:29 +00002874}
2875
2876void
2877ospf_poll_send (struct ospf_nbr_nbma *nbr_nbma)
2878{
2879 struct ospf_interface *oi;
2880
2881 oi = nbr_nbma->oi;
2882 assert(oi);
2883
2884 /* If this is passive interface, do not send OSPF Hello. */
2885 if (OSPF_IF_PARAM (oi, passive_interface) == OSPF_IF_PASSIVE)
2886 return;
2887
2888 if (oi->type != OSPF_IFTYPE_NBMA)
2889 return;
2890
2891 if (nbr_nbma->nbr != NULL && nbr_nbma->nbr->state != NSM_Down)
2892 return;
2893
2894 if (PRIORITY(oi) == 0)
2895 return;
2896
2897 if (nbr_nbma->priority == 0
2898 && oi->state != ISM_DR && oi->state != ISM_Backup)
2899 return;
2900
2901 ospf_hello_send_sub (oi, &nbr_nbma->addr);
2902}
2903
2904int
2905ospf_poll_timer (struct thread *thread)
2906{
2907 struct ospf_nbr_nbma *nbr_nbma;
2908
2909 nbr_nbma = THREAD_ARG (thread);
2910 nbr_nbma->t_poll = NULL;
2911
2912 if (IS_DEBUG_OSPF (nsm, NSM_TIMERS))
2913 zlog (NULL, LOG_INFO, "NSM[%s:%s]: Timer (Poll timer expire)",
2914 IF_NAME (nbr_nbma->oi), inet_ntoa (nbr_nbma->addr));
2915
2916 ospf_poll_send (nbr_nbma);
2917
2918 if (nbr_nbma->v_poll > 0)
2919 OSPF_POLL_TIMER_ON (nbr_nbma->t_poll, ospf_poll_timer,
2920 nbr_nbma->v_poll);
2921
2922 return 0;
2923}
2924
2925
2926int
2927ospf_hello_reply_timer (struct thread *thread)
2928{
2929 struct ospf_neighbor *nbr;
2930
2931 nbr = THREAD_ARG (thread);
2932 nbr->t_hello_reply = NULL;
2933
2934 assert (nbr->oi);
2935
2936 if (IS_DEBUG_OSPF (nsm, NSM_TIMERS))
2937 zlog (NULL, LOG_INFO, "NSM[%s:%s]: Timer (hello-reply timer expire)",
2938 IF_NAME (nbr->oi), inet_ntoa (nbr->router_id));
2939
2940 ospf_hello_send_sub (nbr->oi, &nbr->address.u.prefix4);
2941
2942 return 0;
2943}
2944
2945/* Send OSPF Hello. */
2946void
2947ospf_hello_send (struct ospf_interface *oi)
2948{
2949 struct ospf_packet *op;
2950 u_int16_t length = OSPF_HEADER_SIZE;
2951
2952 /* If this is passive interface, do not send OSPF Hello. */
2953 if (OSPF_IF_PARAM (oi, passive_interface) == OSPF_IF_PASSIVE)
2954 return;
2955
2956 op = ospf_packet_new (oi->ifp->mtu);
2957
2958 /* Prepare OSPF common header. */
2959 ospf_make_header (OSPF_MSG_HELLO, oi, op->s);
2960
2961 /* Prepare OSPF Hello body. */
2962 length += ospf_make_hello (oi, op->s);
2963
2964 /* Fill OSPF header. */
2965 ospf_fill_header (oi, op->s, length);
2966
2967 /* Set packet length. */
2968 op->length = length;
2969
2970 if (oi->type == OSPF_IFTYPE_NBMA)
2971 {
2972 struct ospf_neighbor *nbr;
2973 struct route_node *rn;
2974
2975 for (rn = route_top (oi->nbrs); rn; rn = route_next (rn))
2976 if ((nbr = rn->info))
2977 if (nbr != oi->nbr_self)
2978 if (nbr->state != NSM_Down)
2979 {
2980 /* RFC 2328 Section 9.5.1
2981 If the router is not eligible to become Designated Router,
2982 it must periodically send Hello Packets to both the
2983 Designated Router and the Backup Designated Router (if they
2984 exist). */
2985 if (PRIORITY(oi) == 0 &&
2986 IPV4_ADDR_CMP(&DR(oi), &nbr->address.u.prefix4) &&
2987 IPV4_ADDR_CMP(&BDR(oi), &nbr->address.u.prefix4))
2988 continue;
2989
2990 /* If the router is eligible to become Designated Router, it
2991 must periodically send Hello Packets to all neighbors that
2992 are also eligible. In addition, if the router is itself the
2993 Designated Router or Backup Designated Router, it must also
2994 send periodic Hello Packets to all other neighbors. */
2995
2996 if (nbr->priority == 0 && oi->state == ISM_DROther)
2997 continue;
2998 /* if oi->state == Waiting, send hello to all neighbors */
2999 {
3000 struct ospf_packet *op_dup;
3001
3002 op_dup = ospf_packet_dup(op);
3003 op_dup->dst = nbr->address.u.prefix4;
3004
3005 /* Add packet to the interface output queue. */
3006 ospf_packet_add (oi, op_dup);
3007
paul020709f2003-04-04 02:44:16 +00003008 OSPF_ISM_WRITE_ON (oi->ospf);
paul718e3742002-12-13 20:15:29 +00003009 }
3010
3011 }
3012 ospf_packet_free (op);
3013 }
3014 else
3015 {
3016 /* Decide destination address. */
3017 if (oi->type == OSPF_IFTYPE_VIRTUALLINK)
3018 op->dst.s_addr = oi->vl_data->peer_addr.s_addr;
3019 else
3020 op->dst.s_addr = htonl (OSPF_ALLSPFROUTERS);
3021
3022 /* Add packet to the interface output queue. */
3023 ospf_packet_add (oi, op);
3024
3025 /* Hook thread to write packet. */
paul020709f2003-04-04 02:44:16 +00003026 OSPF_ISM_WRITE_ON (oi->ospf);
paul718e3742002-12-13 20:15:29 +00003027 }
3028}
3029
3030/* Send OSPF Database Description. */
3031void
3032ospf_db_desc_send (struct ospf_neighbor *nbr)
3033{
3034 struct ospf_interface *oi;
3035 struct ospf_packet *op;
3036 u_int16_t length = OSPF_HEADER_SIZE;
3037
3038 oi = nbr->oi;
3039 op = ospf_packet_new (oi->ifp->mtu);
3040
3041 /* Prepare OSPF common header. */
3042 ospf_make_header (OSPF_MSG_DB_DESC, oi, op->s);
3043
3044 /* Prepare OSPF Database Description body. */
3045 length += ospf_make_db_desc (oi, nbr, op->s);
3046
3047 /* Fill OSPF header. */
3048 ospf_fill_header (oi, op->s, length);
3049
3050 /* Set packet length. */
3051 op->length = length;
3052
3053 /* Decide destination address. */
3054 op->dst = nbr->address.u.prefix4;
3055
3056 /* Add packet to the interface output queue. */
3057 ospf_packet_add (oi, op);
3058
3059 /* Hook thread to write packet. */
paul020709f2003-04-04 02:44:16 +00003060 OSPF_ISM_WRITE_ON (oi->ospf);
paul718e3742002-12-13 20:15:29 +00003061
3062 /* Remove old DD packet, then copy new one and keep in neighbor structure. */
3063 if (nbr->last_send)
3064 ospf_packet_free (nbr->last_send);
3065 nbr->last_send = ospf_packet_dup (op);
3066 gettimeofday (&nbr->last_send_ts, NULL);
3067}
3068
3069/* Re-send Database Description. */
3070void
3071ospf_db_desc_resend (struct ospf_neighbor *nbr)
3072{
3073 struct ospf_interface *oi;
3074
3075 oi = nbr->oi;
3076
3077 /* Add packet to the interface output queue. */
3078 ospf_packet_add (oi, ospf_packet_dup (nbr->last_send));
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
3084/* Send Link State Request. */
3085void
3086ospf_ls_req_send (struct ospf_neighbor *nbr)
3087{
3088 struct ospf_interface *oi;
3089 struct ospf_packet *op;
3090 u_int16_t length = OSPF_HEADER_SIZE;
3091
3092 oi = nbr->oi;
3093 op = ospf_packet_new (oi->ifp->mtu);
3094
3095 /* Prepare OSPF common header. */
3096 ospf_make_header (OSPF_MSG_LS_REQ, oi, op->s);
3097
3098 /* Prepare OSPF Link State Request body. */
3099 length += ospf_make_ls_req (nbr, op->s);
3100 if (length == OSPF_HEADER_SIZE)
3101 {
3102 ospf_packet_free (op);
3103 return;
3104 }
3105
3106 /* Fill OSPF header. */
3107 ospf_fill_header (oi, op->s, length);
3108
3109 /* Set packet length. */
3110 op->length = length;
3111
3112 /* Decide destination address. */
3113 op->dst = nbr->address.u.prefix4;
3114
3115 /* Add packet to the interface output queue. */
3116 ospf_packet_add (oi, op);
3117
3118 /* Hook thread to write packet. */
paul020709f2003-04-04 02:44:16 +00003119 OSPF_ISM_WRITE_ON (oi->ospf);
paul718e3742002-12-13 20:15:29 +00003120
3121 /* Add Link State Request Retransmission Timer. */
3122 OSPF_NSM_TIMER_ON (nbr->t_ls_req, ospf_ls_req_timer, nbr->v_ls_req);
3123}
3124
3125/* Send Link State Update with an LSA. */
3126void
3127ospf_ls_upd_send_lsa (struct ospf_neighbor *nbr, struct ospf_lsa *lsa,
3128 int flag)
3129{
3130 list update;
3131
3132 update = list_new ();
3133
3134 listnode_add (update, lsa);
3135 ospf_ls_upd_send (nbr, update, flag);
3136
3137 list_delete (update);
3138}
3139
3140static void
3141ospf_ls_upd_queue_send (struct ospf_interface *oi, list update,
3142 struct in_addr addr)
3143{
3144 struct ospf_packet *op;
3145 u_int16_t length = OSPF_HEADER_SIZE;
3146
3147 if (IS_DEBUG_OSPF_EVENT)
3148 zlog_info ("listcount = %d, dst %s", listcount (update), inet_ntoa(addr));
3149
3150 op = ospf_packet_new (oi->ifp->mtu);
3151
3152 /* Prepare OSPF common header. */
3153 ospf_make_header (OSPF_MSG_LS_UPD, oi, op->s);
3154
3155 /* Prepare OSPF Link State Update body. */
3156 /* Includes Type-7 translation. */
3157 length += ospf_make_ls_upd (oi, update, op->s);
3158
3159 /* Fill OSPF header. */
3160 ospf_fill_header (oi, op->s, length);
3161
3162 /* Set packet length. */
3163 op->length = length;
3164
3165 /* Decide destination address. */
3166 op->dst.s_addr = addr.s_addr;
3167
3168 /* Add packet to the interface output queue. */
3169 ospf_packet_add (oi, op);
3170
3171 /* Hook thread to write packet. */
paul020709f2003-04-04 02:44:16 +00003172 OSPF_ISM_WRITE_ON (oi->ospf);
paul718e3742002-12-13 20:15:29 +00003173}
3174
3175static int
3176ospf_ls_upd_send_queue_event (struct thread *thread)
3177{
3178 struct ospf_interface *oi = THREAD_ARG(thread);
3179 struct route_node *rn;
paul736d3442003-07-24 23:22:57 +00003180 struct route_node *rnext;
paul718e3742002-12-13 20:15:29 +00003181
3182 oi->t_ls_upd_event = NULL;
3183
3184 if (IS_DEBUG_OSPF_EVENT)
3185 zlog_info ("ospf_ls_upd_send_queue start");
3186
paul736d3442003-07-24 23:22:57 +00003187 for (rn = route_top (oi->ls_upd_queue); rn; rn = rnext)
paul718e3742002-12-13 20:15:29 +00003188 {
paul736d3442003-07-24 23:22:57 +00003189
3190 rnext = route_next (rn);
3191
paul718e3742002-12-13 20:15:29 +00003192 if (rn->info == NULL)
paul736d3442003-07-24 23:22:57 +00003193 continue;
paul718e3742002-12-13 20:15:29 +00003194
3195 while (!list_isempty ((list)rn->info))
paul736d3442003-07-24 23:22:57 +00003196 ospf_ls_upd_queue_send (oi, rn->info, rn->p.u.prefix4);
paul718e3742002-12-13 20:15:29 +00003197
3198 list_delete (rn->info);
3199 rn->info = NULL;
3200
3201 route_unlock_node (rn);
3202 }
3203
3204 if (IS_DEBUG_OSPF_EVENT)
3205 zlog_info ("ospf_ls_upd_send_queue stop");
3206 return 0;
3207}
3208
3209void
3210ospf_ls_upd_send (struct ospf_neighbor *nbr, list update, int flag)
3211{
3212 struct ospf_interface *oi;
3213 struct prefix_ipv4 p;
3214 struct route_node *rn;
3215 listnode n;
3216
3217 oi = nbr->oi;
3218
3219 p.family = AF_INET;
3220 p.prefixlen = IPV4_MAX_BITLEN;
3221
3222 /* Decide destination address. */
3223 if (oi->type == OSPF_IFTYPE_VIRTUALLINK)
3224 p.prefix = oi->vl_data->peer_addr;
3225 else if (flag == OSPF_SEND_PACKET_DIRECT)
3226 p.prefix = nbr->address.u.prefix4;
3227 else if (oi->state == ISM_DR || oi->state == ISM_Backup)
3228 p.prefix.s_addr = htonl (OSPF_ALLSPFROUTERS);
3229 else if ((oi->type == OSPF_IFTYPE_POINTOPOINT)
3230 && (flag == OSPF_SEND_PACKET_INDIRECT))
3231 p.prefix.s_addr = htonl (OSPF_ALLSPFROUTERS);
paul7afa08d2002-12-13 20:59:45 +00003232 else if (oi->type == OSPF_IFTYPE_POINTOMULTIPOINT)
3233 p.prefix.s_addr = htonl (OSPF_ALLSPFROUTERS);
paul718e3742002-12-13 20:15:29 +00003234 else
3235 p.prefix.s_addr = htonl (OSPF_ALLDROUTERS);
3236
3237 if (oi->type == OSPF_IFTYPE_NBMA)
3238 {
3239 if (flag == OSPF_SEND_PACKET_INDIRECT)
3240 zlog_warn ("* LS-Update is directly sent on NBMA network.");
3241 if (IPV4_ADDR_SAME(&oi->address->u.prefix4, &p.prefix.s_addr))
3242 zlog_warn ("* LS-Update is sent to myself.");
3243 }
3244
3245 rn = route_node_get (oi->ls_upd_queue, (struct prefix *) &p);
3246
3247 if (rn->info == NULL)
3248 rn->info = list_new ();
3249
3250 for (n = listhead (update); n; nextnode (n))
3251 listnode_add (rn->info, ospf_lsa_lock (getdata (n)));
3252
3253 if (oi->t_ls_upd_event == NULL)
3254 oi->t_ls_upd_event =
3255 thread_add_event (master, ospf_ls_upd_send_queue_event, oi, 0);
3256}
3257
3258static void
3259ospf_ls_ack_send_list (struct ospf_interface *oi, list ack, struct in_addr dst)
3260{
3261 struct ospf_packet *op;
3262 u_int16_t length = OSPF_HEADER_SIZE;
3263
3264 op = ospf_packet_new (oi->ifp->mtu);
3265
3266 /* Prepare OSPF common header. */
3267 ospf_make_header (OSPF_MSG_LS_ACK, oi, op->s);
3268
3269 /* Prepare OSPF Link State Acknowledgment body. */
3270 length += ospf_make_ls_ack (oi, ack, op->s);
3271
3272 /* Fill OSPF header. */
3273 ospf_fill_header (oi, op->s, length);
3274
3275 /* Set packet length. */
3276 op->length = length;
3277
3278 /* Set destination IP address. */
3279 op->dst = dst;
3280
3281 /* Add packet to the interface output queue. */
3282 ospf_packet_add (oi, op);
3283
3284 /* Hook thread to write packet. */
paul020709f2003-04-04 02:44:16 +00003285 OSPF_ISM_WRITE_ON (oi->ospf);
paul718e3742002-12-13 20:15:29 +00003286}
3287
3288static int
3289ospf_ls_ack_send_event (struct thread *thread)
3290{
3291 struct ospf_interface *oi = THREAD_ARG (thread);
3292
3293 oi->t_ls_ack_direct = NULL;
3294
3295 while (listcount (oi->ls_ack_direct.ls_ack))
3296 ospf_ls_ack_send_list (oi, oi->ls_ack_direct.ls_ack,
3297 oi->ls_ack_direct.dst);
3298
3299 return 0;
3300}
3301
3302void
3303ospf_ls_ack_send (struct ospf_neighbor *nbr, struct ospf_lsa *lsa)
3304{
3305 struct ospf_interface *oi = nbr->oi;
3306
3307 if (listcount (oi->ls_ack_direct.ls_ack) == 0)
3308 oi->ls_ack_direct.dst = nbr->address.u.prefix4;
3309
3310 listnode_add (oi->ls_ack_direct.ls_ack, ospf_lsa_lock (lsa));
3311
3312 if (oi->t_ls_ack_direct == NULL)
3313 oi->t_ls_ack_direct =
3314 thread_add_event (master, ospf_ls_ack_send_event, oi, 0);
3315}
3316
3317/* Send Link State Acknowledgment delayed. */
3318void
3319ospf_ls_ack_send_delayed (struct ospf_interface *oi)
3320{
3321 struct in_addr dst;
3322
3323 /* Decide destination address. */
3324 /* RFC2328 Section 13.5 On non-broadcast
3325 networks, delayed Link State Acknowledgment packets must be
3326 unicast separately over each adjacency (i.e., neighbor whose
3327 state is >= Exchange). */
3328 if (oi->type == OSPF_IFTYPE_NBMA)
3329 {
3330 struct ospf_neighbor *nbr;
3331 struct route_node *rn;
3332
3333 for (rn = route_top (oi->nbrs); rn; rn = route_next (rn))
3334 if ((nbr = rn->info) != NULL)
3335 if (nbr != oi->nbr_self && nbr->state >= NSM_Exchange)
3336 while (listcount (oi->ls_ack))
3337 ospf_ls_ack_send_list (oi, oi->ls_ack, nbr->address.u.prefix4);
3338 return;
3339 }
3340 if (oi->type == OSPF_IFTYPE_VIRTUALLINK)
3341 dst.s_addr = oi->vl_data->peer_addr.s_addr;
3342 else if (oi->state == ISM_DR || oi->state == ISM_Backup)
3343 dst.s_addr = htonl (OSPF_ALLSPFROUTERS);
3344 else if (oi->type == OSPF_IFTYPE_POINTOPOINT)
3345 dst.s_addr = htonl (OSPF_ALLSPFROUTERS);
3346 else
3347 dst.s_addr = htonl (OSPF_ALLDROUTERS);
3348
3349 while (listcount (oi->ls_ack))
3350 ospf_ls_ack_send_list (oi, oi->ls_ack, dst);
3351}