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