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