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