blob: d79df53543921a67ca7dcbf96f66c8256f140ec7 [file] [log] [blame]
paul718e3742002-12-13 20:15:29 +00001/*
2 * OSPF Sending and Receiving OSPF Packets.
3 * Copyright (C) 1999, 2000 Toshiaki Takada
4 *
5 * This file is part of GNU Zebra.
6 *
7 * GNU Zebra is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
10 * later version.
11 *
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with GNU Zebra; see the file COPYING. If not, write to the Free
19 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 * 02111-1307, USA.
21 */
22
23#include <zebra.h>
24
25#include "thread.h"
26#include "memory.h"
27#include "linklist.h"
28#include "prefix.h"
29#include "if.h"
30#include "table.h"
31#include "sockunion.h"
32#include "stream.h"
33#include "log.h"
paul2dd8bb42004-07-23 15:13:48 +000034#include "sockopt.h"
paul484315f2005-11-03 09:08:29 +000035#include "checksum.h"
vincentc1a03d42005-09-28 15:47:44 +000036#include "md5.h"
paul718e3742002-12-13 20:15:29 +000037
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
paul718e3742002-12-13 20:15:29 +000052/* Packet Type String. */
Denis Ovsienko272ca1e2012-01-15 19:12:19 +040053const struct message ospf_packet_type_str[] =
paul718e3742002-12-13 20:15:29 +000054{
Denis Ovsienko272ca1e2012-01-15 19:12:19 +040055 { OSPF_MSG_HELLO, "Hello" },
56 { OSPF_MSG_DB_DESC, "Database Description" },
57 { OSPF_MSG_LS_REQ, "Link State Request" },
58 { OSPF_MSG_LS_UPD, "Link State Update" },
59 { OSPF_MSG_LS_ACK, "Link State Acknowledgment" },
paul718e3742002-12-13 20:15:29 +000060};
Denis Ovsienko272ca1e2012-01-15 19:12:19 +040061const size_t ospf_packet_type_str_max = sizeof (ospf_packet_type_str) /
62 sizeof (ospf_packet_type_str[0]);
paul718e3742002-12-13 20:15:29 +000063
Denis Ovsienko75c8eab2012-01-30 15:41:39 +040064/* Minimum (besides OSPF_HEADER_SIZE) lengths for OSPF packets of
65 particular types, offset is the "type" field of a packet. */
66static const u_int16_t ospf_packet_minlen[] =
67{
68 0,
69 OSPF_HELLO_MIN_SIZE,
70 OSPF_DB_DESC_MIN_SIZE,
71 OSPF_LS_REQ_MIN_SIZE,
72 OSPF_LS_UPD_MIN_SIZE,
73 OSPF_LS_ACK_MIN_SIZE,
74};
75
Denis Ovsienko4e31de72012-02-17 16:20:50 +040076/* Minimum (besides OSPF_LSA_HEADER_SIZE) lengths for LSAs of particular
77 types, offset is the "LSA type" field. */
78static const u_int16_t ospf_lsa_minlen[] =
79{
80 0,
81 OSPF_ROUTER_LSA_MIN_SIZE,
82 OSPF_NETWORK_LSA_MIN_SIZE,
83 OSPF_SUMMARY_LSA_MIN_SIZE,
84 OSPF_SUMMARY_LSA_MIN_SIZE,
85 OSPF_AS_EXTERNAL_LSA_MIN_SIZE,
86 0,
87 OSPF_AS_EXTERNAL_LSA_MIN_SIZE,
88 0,
89 0,
90 0,
91 0,
92};
93
Denis Ovsienkobd5651f2012-02-26 17:59:43 +040094/* for ospf_check_auth() */
95static int ospf_check_sum (struct ospf_header *);
96
paul718e3742002-12-13 20:15:29 +000097/* OSPF authentication checking function */
paul4dadc292005-05-06 21:37:42 +000098static int
paul718e3742002-12-13 20:15:29 +000099ospf_auth_type (struct ospf_interface *oi)
100{
101 int auth_type;
102
103 if (OSPF_IF_PARAM (oi, auth_type) == OSPF_AUTH_NOTSET)
104 auth_type = oi->area->auth_type;
105 else
106 auth_type = OSPF_IF_PARAM (oi, auth_type);
107
108 /* Handle case where MD5 key list is not configured aka Cisco */
109 if (auth_type == OSPF_AUTH_CRYPTOGRAPHIC &&
110 list_isempty (OSPF_IF_PARAM (oi, auth_crypt)))
111 return OSPF_AUTH_NULL;
112
113 return auth_type;
114
115}
116
paul718e3742002-12-13 20:15:29 +0000117struct ospf_packet *
118ospf_packet_new (size_t size)
119{
120 struct ospf_packet *new;
121
122 new = XCALLOC (MTYPE_OSPF_PACKET, sizeof (struct ospf_packet));
123 new->s = stream_new (size);
124
125 return new;
126}
127
128void
129ospf_packet_free (struct ospf_packet *op)
130{
131 if (op->s)
132 stream_free (op->s);
133
134 XFREE (MTYPE_OSPF_PACKET, op);
135
136 op = NULL;
137}
138
139struct ospf_fifo *
140ospf_fifo_new ()
141{
142 struct ospf_fifo *new;
143
144 new = XCALLOC (MTYPE_OSPF_FIFO, sizeof (struct ospf_fifo));
145 return new;
146}
147
148/* Add new packet to fifo. */
149void
150ospf_fifo_push (struct ospf_fifo *fifo, struct ospf_packet *op)
151{
152 if (fifo->tail)
153 fifo->tail->next = op;
154 else
155 fifo->head = op;
156
157 fifo->tail = op;
158
159 fifo->count++;
160}
161
Paul Jakmaaa276fd2010-01-08 17:11:15 +0000162/* Add new packet to head of fifo. */
163static void
164ospf_fifo_push_head (struct ospf_fifo *fifo, struct ospf_packet *op)
165{
166 op->next = fifo->head;
167
168 if (fifo->tail == NULL)
169 fifo->tail = op;
170
171 fifo->head = op;
172
173 fifo->count++;
174}
175
paul718e3742002-12-13 20:15:29 +0000176/* Delete first packet from fifo. */
177struct ospf_packet *
178ospf_fifo_pop (struct ospf_fifo *fifo)
179{
180 struct ospf_packet *op;
181
182 op = fifo->head;
183
184 if (op)
185 {
186 fifo->head = op->next;
187
188 if (fifo->head == NULL)
189 fifo->tail = NULL;
190
191 fifo->count--;
192 }
193
194 return op;
195}
196
197/* Return first fifo entry. */
198struct ospf_packet *
199ospf_fifo_head (struct ospf_fifo *fifo)
200{
201 return fifo->head;
202}
203
204/* Flush ospf packet fifo. */
205void
206ospf_fifo_flush (struct ospf_fifo *fifo)
207{
208 struct ospf_packet *op;
209 struct ospf_packet *next;
210
211 for (op = fifo->head; op; op = next)
212 {
213 next = op->next;
214 ospf_packet_free (op);
215 }
216 fifo->head = fifo->tail = NULL;
217 fifo->count = 0;
218}
219
220/* Free ospf packet fifo. */
221void
222ospf_fifo_free (struct ospf_fifo *fifo)
223{
224 ospf_fifo_flush (fifo);
225
226 XFREE (MTYPE_OSPF_FIFO, fifo);
227}
228
229void
230ospf_packet_add (struct ospf_interface *oi, struct ospf_packet *op)
231{
ajsc3eab872005-01-29 15:52:07 +0000232 if (!oi->obuf)
233 {
234 zlog_err("ospf_packet_add(interface %s in state %d [%s], packet type %s, "
235 "destination %s) called with NULL obuf, ignoring "
236 "(please report this bug)!\n",
237 IF_NAME(oi), oi->state, LOOKUP (ospf_ism_state_msg, oi->state),
Denis Ovsienko272ca1e2012-01-15 19:12:19 +0400238 LOOKUP (ospf_packet_type_str, stream_getc_from(op->s, 1)),
ajsc3eab872005-01-29 15:52:07 +0000239 inet_ntoa (op->dst));
240 return;
241 }
242
paul718e3742002-12-13 20:15:29 +0000243 /* Add packet to end of queue. */
244 ospf_fifo_push (oi->obuf, op);
245
246 /* Debug of packet fifo*/
247 /* ospf_fifo_debug (oi->obuf); */
248}
249
Paul Jakmaaa276fd2010-01-08 17:11:15 +0000250static void
251ospf_packet_add_top (struct ospf_interface *oi, struct ospf_packet *op)
252{
253 if (!oi->obuf)
254 {
255 zlog_err("ospf_packet_add(interface %s in state %d [%s], packet type %s, "
256 "destination %s) called with NULL obuf, ignoring "
257 "(please report this bug)!\n",
258 IF_NAME(oi), oi->state, LOOKUP (ospf_ism_state_msg, oi->state),
Denis Ovsienko7e0e2cb2012-01-20 22:32:10 +0400259 LOOKUP (ospf_packet_type_str, stream_getc_from(op->s, 1)),
Paul Jakmaaa276fd2010-01-08 17:11:15 +0000260 inet_ntoa (op->dst));
261 return;
262 }
263
264 /* Add packet to head of queue. */
265 ospf_fifo_push_head (oi->obuf, op);
266
267 /* Debug of packet fifo*/
268 /* ospf_fifo_debug (oi->obuf); */
269}
270
paul718e3742002-12-13 20:15:29 +0000271void
272ospf_packet_delete (struct ospf_interface *oi)
273{
274 struct ospf_packet *op;
275
276 op = ospf_fifo_pop (oi->obuf);
277
278 if (op)
279 ospf_packet_free (op);
280}
281
paul718e3742002-12-13 20:15:29 +0000282struct ospf_packet *
283ospf_packet_dup (struct ospf_packet *op)
284{
285 struct ospf_packet *new;
286
paul37163d62003-02-03 18:40:56 +0000287 if (stream_get_endp(op->s) != op->length)
Andrew J. Schorr08c83672006-09-25 13:26:14 +0000288 /* XXX size_t */
289 zlog_warn ("ospf_packet_dup stream %lu ospf_packet %u size mismatch",
290 (u_long)STREAM_SIZE(op->s), op->length);
paul30961a12002-12-13 20:56:48 +0000291
292 /* Reserve space for MD5 authentication that may be added later. */
293 new = ospf_packet_new (stream_get_endp(op->s) + OSPF_AUTH_MD5_SIZE);
paulfa81b712005-02-19 01:19:20 +0000294 stream_copy (new->s, op->s);
paul718e3742002-12-13 20:15:29 +0000295
296 new->dst = op->dst;
297 new->length = op->length;
298
299 return new;
300}
301
gdt86f1fd92005-01-10 14:20:43 +0000302/* XXX inline */
Paul Jakmaf63f06d2011-04-08 12:44:43 +0100303static unsigned int
gdt86f1fd92005-01-10 14:20:43 +0000304ospf_packet_authspace (struct ospf_interface *oi)
305{
306 int auth = 0;
307
308 if ( ospf_auth_type (oi) == OSPF_AUTH_CRYPTOGRAPHIC)
309 auth = OSPF_AUTH_MD5_SIZE;
310
311 return auth;
312}
313
paul4dadc292005-05-06 21:37:42 +0000314static unsigned int
paul718e3742002-12-13 20:15:29 +0000315ospf_packet_max (struct ospf_interface *oi)
316{
317 int max;
318
gdt86f1fd92005-01-10 14:20:43 +0000319 max = oi->ifp->mtu - ospf_packet_authspace(oi);
320
paul68b73392004-09-12 14:21:37 +0000321 max -= (OSPF_HEADER_SIZE + sizeof (struct ip));
paul718e3742002-12-13 20:15:29 +0000322
323 return max;
324}
325
326
paul4dadc292005-05-06 21:37:42 +0000327static int
Denis Ovsienko2d8223c2012-01-30 20:32:39 +0400328ospf_check_md5_digest (struct ospf_interface *oi, struct ospf_header *ospfh)
paul718e3742002-12-13 20:15:29 +0000329{
vincentc1a03d42005-09-28 15:47:44 +0000330 MD5_CTX ctx;
paul718e3742002-12-13 20:15:29 +0000331 unsigned char digest[OSPF_AUTH_MD5_SIZE];
paul718e3742002-12-13 20:15:29 +0000332 struct crypt_key *ck;
paul718e3742002-12-13 20:15:29 +0000333 struct ospf_neighbor *nbr;
Denis Ovsienko2d8223c2012-01-30 20:32:39 +0400334 u_int16_t length = ntohs (ospfh->length);
paul718e3742002-12-13 20:15:29 +0000335
paul718e3742002-12-13 20:15:29 +0000336 /* Get secret key. */
337 ck = ospf_crypt_key_lookup (OSPF_IF_PARAM (oi, auth_crypt),
338 ospfh->u.crypt.key_id);
339 if (ck == NULL)
340 {
341 zlog_warn ("interface %s: ospf_check_md5 no key %d",
342 IF_NAME (oi), ospfh->u.crypt.key_id);
343 return 0;
344 }
345
346 /* check crypto seqnum. */
347 nbr = ospf_nbr_lookup_by_routerid (oi->nbrs, &ospfh->router_id);
348
349 if (nbr && ntohl(nbr->crypt_seqnum) > ntohl(ospfh->u.crypt.crypt_seqnum))
350 {
351 zlog_warn ("interface %s: ospf_check_md5 bad sequence %d (expect %d)",
352 IF_NAME (oi),
353 ntohl(ospfh->u.crypt.crypt_seqnum),
354 ntohl(nbr->crypt_seqnum));
355 return 0;
356 }
357
358 /* Generate a digest for the ospf packet - their digest + our digest. */
vincentc1a03d42005-09-28 15:47:44 +0000359 memset(&ctx, 0, sizeof(ctx));
360 MD5Init(&ctx);
Denis Ovsienko2d8223c2012-01-30 20:32:39 +0400361 MD5Update(&ctx, ospfh, length);
vincentc1a03d42005-09-28 15:47:44 +0000362 MD5Update(&ctx, ck->auth_key, OSPF_AUTH_MD5_SIZE);
363 MD5Final(digest, &ctx);
paul718e3742002-12-13 20:15:29 +0000364
365 /* compare the two */
Denis Ovsienko2d8223c2012-01-30 20:32:39 +0400366 if (memcmp ((caddr_t)ospfh + length, digest, OSPF_AUTH_MD5_SIZE))
paul718e3742002-12-13 20:15:29 +0000367 {
368 zlog_warn ("interface %s: ospf_check_md5 checksum mismatch",
369 IF_NAME (oi));
370 return 0;
371 }
372
373 /* save neighbor's crypt_seqnum */
374 if (nbr)
375 nbr->crypt_seqnum = ospfh->u.crypt.crypt_seqnum;
376 return 1;
377}
378
379/* This function is called from ospf_write(), it will detect the
380 authentication scheme and if it is MD5, it will change the sequence
381 and update the MD5 digest. */
paul4dadc292005-05-06 21:37:42 +0000382static int
paul718e3742002-12-13 20:15:29 +0000383ospf_make_md5_digest (struct ospf_interface *oi, struct ospf_packet *op)
384{
385 struct ospf_header *ospfh;
386 unsigned char digest[OSPF_AUTH_MD5_SIZE];
vincentc1a03d42005-09-28 15:47:44 +0000387 MD5_CTX ctx;
paul718e3742002-12-13 20:15:29 +0000388 void *ibuf;
paul9483e152002-12-13 20:55:25 +0000389 u_int32_t t;
paul718e3742002-12-13 20:15:29 +0000390 struct crypt_key *ck;
paul36238142005-10-11 04:12:54 +0000391 const u_int8_t *auth_key;
paul718e3742002-12-13 20:15:29 +0000392
393 ibuf = STREAM_DATA (op->s);
394 ospfh = (struct ospf_header *) ibuf;
395
396 if (ntohs (ospfh->auth_type) != OSPF_AUTH_CRYPTOGRAPHIC)
397 return 0;
398
399 /* We do this here so when we dup a packet, we don't have to
Paul Jakma2518efd2006-08-27 06:49:29 +0000400 waste CPU rewriting other headers.
401
402 Note that quagga_time /deliberately/ is not used here */
paul9483e152002-12-13 20:55:25 +0000403 t = (time(NULL) & 0xFFFFFFFF);
paul818e56c2006-01-10 23:27:05 +0000404 if (t > oi->crypt_seqnum)
405 oi->crypt_seqnum = t;
406 else
407 oi->crypt_seqnum++;
408
paul9483e152002-12-13 20:55:25 +0000409 ospfh->u.crypt.crypt_seqnum = htonl (oi->crypt_seqnum);
paul718e3742002-12-13 20:15:29 +0000410
411 /* Get MD5 Authentication key from auth_key list. */
412 if (list_isempty (OSPF_IF_PARAM (oi, auth_crypt)))
paul36238142005-10-11 04:12:54 +0000413 auth_key = (const u_int8_t *) "";
paul718e3742002-12-13 20:15:29 +0000414 else
415 {
paul1eb8ef22005-04-07 07:30:20 +0000416 ck = listgetdata (listtail(OSPF_IF_PARAM (oi, auth_crypt)));
paul4dadc292005-05-06 21:37:42 +0000417 auth_key = ck->auth_key;
paul718e3742002-12-13 20:15:29 +0000418 }
419
420 /* Generate a digest for the entire packet + our secret key. */
vincentc1a03d42005-09-28 15:47:44 +0000421 memset(&ctx, 0, sizeof(ctx));
422 MD5Init(&ctx);
423 MD5Update(&ctx, ibuf, ntohs (ospfh->length));
424 MD5Update(&ctx, auth_key, OSPF_AUTH_MD5_SIZE);
425 MD5Final(digest, &ctx);
paul718e3742002-12-13 20:15:29 +0000426
427 /* Append md5 digest to the end of the stream. */
paul718e3742002-12-13 20:15:29 +0000428 stream_put (op->s, digest, OSPF_AUTH_MD5_SIZE);
paul718e3742002-12-13 20:15:29 +0000429
430 /* We do *NOT* increment the OSPF header length. */
paul30961a12002-12-13 20:56:48 +0000431 op->length = ntohs (ospfh->length) + OSPF_AUTH_MD5_SIZE;
432
paul37163d62003-02-03 18:40:56 +0000433 if (stream_get_endp(op->s) != op->length)
Andrew J. Schorr08c83672006-09-25 13:26:14 +0000434 /* XXX size_t */
435 zlog_warn("ospf_make_md5_digest: length mismatch stream %lu ospf_packet %u",
436 (u_long)stream_get_endp(op->s), op->length);
paul718e3742002-12-13 20:15:29 +0000437
438 return OSPF_AUTH_MD5_SIZE;
439}
440
441
paul4dadc292005-05-06 21:37:42 +0000442static int
paul718e3742002-12-13 20:15:29 +0000443ospf_ls_req_timer (struct thread *thread)
444{
445 struct ospf_neighbor *nbr;
446
447 nbr = THREAD_ARG (thread);
448 nbr->t_ls_req = NULL;
449
450 /* Send Link State Request. */
451 if (ospf_ls_request_count (nbr))
452 ospf_ls_req_send (nbr);
453
454 /* Set Link State Request retransmission timer. */
455 OSPF_NSM_TIMER_ON (nbr->t_ls_req, ospf_ls_req_timer, nbr->v_ls_req);
456
457 return 0;
458}
459
460void
461ospf_ls_req_event (struct ospf_neighbor *nbr)
462{
463 if (nbr->t_ls_req)
464 {
465 thread_cancel (nbr->t_ls_req);
466 nbr->t_ls_req = NULL;
467 }
468 nbr->t_ls_req = thread_add_event (master, ospf_ls_req_timer, nbr, 0);
469}
470
471/* Cyclic timer function. Fist registered in ospf_nbr_new () in
472 ospf_neighbor.c */
473int
474ospf_ls_upd_timer (struct thread *thread)
475{
476 struct ospf_neighbor *nbr;
477
478 nbr = THREAD_ARG (thread);
479 nbr->t_ls_upd = NULL;
480
481 /* Send Link State Update. */
482 if (ospf_ls_retransmit_count (nbr) > 0)
483 {
hasso52dc7ee2004-09-23 19:18:23 +0000484 struct list *update;
paul718e3742002-12-13 20:15:29 +0000485 struct ospf_lsdb *lsdb;
486 int i;
paul718e3742002-12-13 20:15:29 +0000487 int retransmit_interval;
488
paul718e3742002-12-13 20:15:29 +0000489 retransmit_interval = OSPF_IF_PARAM (nbr->oi, retransmit_interval);
490
491 lsdb = &nbr->ls_rxmt;
492 update = list_new ();
493
494 for (i = OSPF_MIN_LSA; i < OSPF_MAX_LSA; i++)
495 {
496 struct route_table *table = lsdb->type[i].db;
497 struct route_node *rn;
498
499 for (rn = route_top (table); rn; rn = route_next (rn))
500 {
501 struct ospf_lsa *lsa;
502
503 if ((lsa = rn->info) != NULL)
504 /* Don't retransmit an LSA if we received it within
505 the last RxmtInterval seconds - this is to allow the
506 neighbour a chance to acknowledge the LSA as it may
507 have ben just received before the retransmit timer
508 fired. This is a small tweak to what is in the RFC,
509 but it will cut out out a lot of retransmit traffic
510 - MAG */
Paul Jakma2518efd2006-08-27 06:49:29 +0000511 if (tv_cmp (tv_sub (recent_relative_time (), lsa->tv_recv),
paul718e3742002-12-13 20:15:29 +0000512 int2tv (retransmit_interval)) >= 0)
513 listnode_add (update, rn->info);
514 }
515 }
516
517 if (listcount (update) > 0)
518 ospf_ls_upd_send (nbr, update, OSPF_SEND_PACKET_DIRECT);
519 list_delete (update);
520 }
521
522 /* Set LS Update retransmission timer. */
523 OSPF_NSM_TIMER_ON (nbr->t_ls_upd, ospf_ls_upd_timer, nbr->v_ls_upd);
524
525 return 0;
526}
527
528int
529ospf_ls_ack_timer (struct thread *thread)
530{
531 struct ospf_interface *oi;
532
533 oi = THREAD_ARG (thread);
534 oi->t_ls_ack = NULL;
535
536 /* Send Link State Acknowledgment. */
537 if (listcount (oi->ls_ack) > 0)
538 ospf_ls_ack_send_delayed (oi);
539
540 /* Set LS Ack timer. */
541 OSPF_ISM_TIMER_ON (oi->t_ls_ack, ospf_ls_ack_timer, oi->v_ls_ack);
542
543 return 0;
544}
545
paul0bfeca32004-09-24 08:07:54 +0000546#ifdef WANT_OSPF_WRITE_FRAGMENT
ajs5dcbdf82005-03-29 16:13:49 +0000547static void
paul6a99f832004-09-27 12:56:30 +0000548ospf_write_frags (int fd, struct ospf_packet *op, struct ip *iph,
paul62d8e962004-11-02 20:26:45 +0000549 struct msghdr *msg, unsigned int maxdatasize,
paul37ccfa32004-10-31 11:24:51 +0000550 unsigned int mtu, int flags, u_char type)
paul0bfeca32004-09-24 08:07:54 +0000551{
552#define OSPF_WRITE_FRAG_SHIFT 3
paul6a99f832004-09-27 12:56:30 +0000553 u_int16_t offset;
paul62d8e962004-11-02 20:26:45 +0000554 struct iovec *iovp;
paul6a99f832004-09-27 12:56:30 +0000555 int ret;
paul0bfeca32004-09-24 08:07:54 +0000556
557 assert ( op->length == stream_get_endp(op->s) );
paul62d8e962004-11-02 20:26:45 +0000558 assert (msg->msg_iovlen == 2);
paul0bfeca32004-09-24 08:07:54 +0000559
560 /* we can but try.
561 *
562 * SunOS, BSD and BSD derived kernels likely will clear ip_id, as
563 * well as the IP_MF flag, making this all quite pointless.
564 *
565 * However, for a system on which IP_MF is left alone, and ip_id left
566 * alone or else which sets same ip_id for each fragment this might
567 * work, eg linux.
568 *
569 * XXX-TODO: It would be much nicer to have the kernel's use their
570 * existing fragmentation support to do this for us. Bugs/RFEs need to
571 * be raised against the various kernels.
572 */
573
574 /* set More Frag */
575 iph->ip_off |= IP_MF;
576
577 /* ip frag offset is expressed in units of 8byte words */
578 offset = maxdatasize >> OSPF_WRITE_FRAG_SHIFT;
579
paul62d8e962004-11-02 20:26:45 +0000580 iovp = &msg->msg_iov[1];
581
paul0bfeca32004-09-24 08:07:54 +0000582 while ( (stream_get_endp(op->s) - stream_get_getp (op->s))
583 > maxdatasize )
584 {
585 /* data length of this frag is to next offset value */
paul62d8e962004-11-02 20:26:45 +0000586 iovp->iov_len = offset << OSPF_WRITE_FRAG_SHIFT;
587 iph->ip_len = iovp->iov_len + sizeof (struct ip);
paul6a99f832004-09-27 12:56:30 +0000588 assert (iph->ip_len <= mtu);
paul0bfeca32004-09-24 08:07:54 +0000589
paul18b12c32004-10-05 14:38:29 +0000590 sockopt_iphdrincl_swab_htosys (iph);
paul0bfeca32004-09-24 08:07:54 +0000591
paul6a99f832004-09-27 12:56:30 +0000592 ret = sendmsg (fd, msg, flags);
paul0bfeca32004-09-24 08:07:54 +0000593
paul18b12c32004-10-05 14:38:29 +0000594 sockopt_iphdrincl_swab_systoh (iph);
paul0bfeca32004-09-24 08:07:54 +0000595
596 if (ret < 0)
paul37ccfa32004-10-31 11:24:51 +0000597 zlog_warn ("*** ospf_write_frags: sendmsg failed to %s,"
ajs5dcbdf82005-03-29 16:13:49 +0000598 " id %d, off %d, len %d, mtu %u failed with %s",
599 inet_ntoa (iph->ip_dst),
600 iph->ip_id,
601 iph->ip_off,
602 iph->ip_len,
603 mtu,
604 safe_strerror (errno));
paul0bfeca32004-09-24 08:07:54 +0000605
paul37ccfa32004-10-31 11:24:51 +0000606 if (IS_DEBUG_OSPF_PACKET (type - 1, SEND))
607 {
ajs2a42e282004-12-08 18:43:03 +0000608 zlog_debug ("ospf_write_frags: sent id %d, off %d, len %d to %s\n",
paul37ccfa32004-10-31 11:24:51 +0000609 iph->ip_id, iph->ip_off, iph->ip_len,
610 inet_ntoa (iph->ip_dst));
611 if (IS_DEBUG_OSPF_PACKET (type - 1, DETAIL))
612 {
ajs2a42e282004-12-08 18:43:03 +0000613 zlog_debug ("-----------------IP Header Dump----------------------");
paul37ccfa32004-10-31 11:24:51 +0000614 ospf_ip_header_dump (iph);
ajs2a42e282004-12-08 18:43:03 +0000615 zlog_debug ("-----------------------------------------------------");
paul37ccfa32004-10-31 11:24:51 +0000616 }
617 }
618
paul0bfeca32004-09-24 08:07:54 +0000619 iph->ip_off += offset;
paul9985f832005-02-09 15:51:56 +0000620 stream_forward_getp (op->s, iovp->iov_len);
paul62d8e962004-11-02 20:26:45 +0000621 iovp->iov_base = STREAM_PNT (op->s);
paul0bfeca32004-09-24 08:07:54 +0000622 }
623
624 /* setup for final fragment */
paul62d8e962004-11-02 20:26:45 +0000625 iovp->iov_len = stream_get_endp(op->s) - stream_get_getp (op->s);
626 iph->ip_len = iovp->iov_len + sizeof (struct ip);
paul0bfeca32004-09-24 08:07:54 +0000627 iph->ip_off &= (~IP_MF);
628}
629#endif /* WANT_OSPF_WRITE_FRAGMENT */
630
ajs5dcbdf82005-03-29 16:13:49 +0000631static int
paul718e3742002-12-13 20:15:29 +0000632ospf_write (struct thread *thread)
633{
paul68980082003-03-25 05:07:42 +0000634 struct ospf *ospf = THREAD_ARG (thread);
paul718e3742002-12-13 20:15:29 +0000635 struct ospf_interface *oi;
636 struct ospf_packet *op;
637 struct sockaddr_in sa_dst;
paul718e3742002-12-13 20:15:29 +0000638 struct ip iph;
639 struct msghdr msg;
paul62d8e962004-11-02 20:26:45 +0000640 struct iovec iov[2];
paul68980082003-03-25 05:07:42 +0000641 u_char type;
642 int ret;
643 int flags = 0;
hasso52dc7ee2004-09-23 19:18:23 +0000644 struct listnode *node;
paul0bfeca32004-09-24 08:07:54 +0000645#ifdef WANT_OSPF_WRITE_FRAGMENT
paul68b73392004-09-12 14:21:37 +0000646 static u_int16_t ipid = 0;
paul0bfeca32004-09-24 08:07:54 +0000647#endif /* WANT_OSPF_WRITE_FRAGMENT */
paul6a99f832004-09-27 12:56:30 +0000648 u_int16_t maxdatasize;
paul68b73392004-09-12 14:21:37 +0000649#define OSPF_WRITE_IPHL_SHIFT 2
paul718e3742002-12-13 20:15:29 +0000650
paul68980082003-03-25 05:07:42 +0000651 ospf->t_write = NULL;
paul718e3742002-12-13 20:15:29 +0000652
paul68980082003-03-25 05:07:42 +0000653 node = listhead (ospf->oi_write_q);
paul718e3742002-12-13 20:15:29 +0000654 assert (node);
paul1eb8ef22005-04-07 07:30:20 +0000655 oi = listgetdata (node);
paul718e3742002-12-13 20:15:29 +0000656 assert (oi);
paul0bfeca32004-09-24 08:07:54 +0000657
658#ifdef WANT_OSPF_WRITE_FRAGMENT
paul68b73392004-09-12 14:21:37 +0000659 /* seed ipid static with low order bits of time */
660 if (ipid == 0)
661 ipid = (time(NULL) & 0xffff);
paul0bfeca32004-09-24 08:07:54 +0000662#endif /* WANT_OSPF_WRITE_FRAGMENT */
663
Denis Ovsienkob7fe4142007-08-21 16:32:56 +0000664 /* convenience - max OSPF data per packet,
665 * and reliability - not more data, than our
666 * socket can accept
667 */
668 maxdatasize = MIN (oi->ifp->mtu, ospf->maxsndbuflen) -
669 sizeof (struct ip);
paul68b73392004-09-12 14:21:37 +0000670
paul718e3742002-12-13 20:15:29 +0000671 /* Get one packet from queue. */
672 op = ospf_fifo_head (oi->obuf);
673 assert (op);
674 assert (op->length >= OSPF_HEADER_SIZE);
675
paul68980082003-03-25 05:07:42 +0000676 if (op->dst.s_addr == htonl (OSPF_ALLSPFROUTERS)
677 || op->dst.s_addr == htonl (OSPF_ALLDROUTERS))
paul68b73392004-09-12 14:21:37 +0000678 ospf_if_ipmulticast (ospf, oi->address, oi->ifp->ifindex);
679
paul718e3742002-12-13 20:15:29 +0000680 /* Rewrite the md5 signature & update the seq */
681 ospf_make_md5_digest (oi, op);
682
paul37ccfa32004-10-31 11:24:51 +0000683 /* Retrieve OSPF packet type. */
684 stream_set_getp (op->s, 1);
685 type = stream_getc (op->s);
686
paul68b73392004-09-12 14:21:37 +0000687 /* reset get pointer */
688 stream_set_getp (op->s, 0);
689
690 memset (&iph, 0, sizeof (struct ip));
paul718e3742002-12-13 20:15:29 +0000691 memset (&sa_dst, 0, sizeof (sa_dst));
paul68b73392004-09-12 14:21:37 +0000692
paul718e3742002-12-13 20:15:29 +0000693 sa_dst.sin_family = AF_INET;
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000694#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
paul718e3742002-12-13 20:15:29 +0000695 sa_dst.sin_len = sizeof(sa_dst);
Paul Jakma6f0e3f62007-05-10 02:38:51 +0000696#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
paul718e3742002-12-13 20:15:29 +0000697 sa_dst.sin_addr = op->dst;
698 sa_dst.sin_port = htons (0);
699
700 /* Set DONTROUTE flag if dst is unicast. */
701 if (oi->type != OSPF_IFTYPE_VIRTUALLINK)
702 if (!IN_MULTICAST (htonl (op->dst.s_addr)))
703 flags = MSG_DONTROUTE;
704
paul68b73392004-09-12 14:21:37 +0000705 iph.ip_hl = sizeof (struct ip) >> OSPF_WRITE_IPHL_SHIFT;
706 /* it'd be very strange for header to not be 4byte-word aligned but.. */
paul6c835672004-10-11 11:00:30 +0000707 if ( sizeof (struct ip)
708 > (unsigned int)(iph.ip_hl << OSPF_WRITE_IPHL_SHIFT) )
paul68b73392004-09-12 14:21:37 +0000709 iph.ip_hl++; /* we presume sizeof struct ip cant overflow ip_hl.. */
710
paul718e3742002-12-13 20:15:29 +0000711 iph.ip_v = IPVERSION;
paul68980082003-03-25 05:07:42 +0000712 iph.ip_tos = IPTOS_PREC_INTERNETCONTROL;
paul68b73392004-09-12 14:21:37 +0000713 iph.ip_len = (iph.ip_hl << OSPF_WRITE_IPHL_SHIFT) + op->length;
paul68b73392004-09-12 14:21:37 +0000714
David BÉRARD0150c9c2010-05-11 10:17:53 +0200715#if defined(__DragonFly__)
716 /*
717 * DragonFly's raw socket expects ip_len/ip_off in network byte order.
718 */
719 iph.ip_len = htons(iph.ip_len);
720#endif
721
paul0bfeca32004-09-24 08:07:54 +0000722#ifdef WANT_OSPF_WRITE_FRAGMENT
paul68b73392004-09-12 14:21:37 +0000723 /* XXX-MT: not thread-safe at all..
724 * XXX: this presumes this is only programme sending OSPF packets
725 * otherwise, no guarantee ipid will be unique
726 */
727 iph.ip_id = ++ipid;
paul0bfeca32004-09-24 08:07:54 +0000728#endif /* WANT_OSPF_WRITE_FRAGMENT */
729
paul718e3742002-12-13 20:15:29 +0000730 iph.ip_off = 0;
731 if (oi->type == OSPF_IFTYPE_VIRTUALLINK)
732 iph.ip_ttl = OSPF_VL_IP_TTL;
733 else
734 iph.ip_ttl = OSPF_IP_TTL;
735 iph.ip_p = IPPROTO_OSPFIGP;
736 iph.ip_sum = 0;
737 iph.ip_src.s_addr = oi->address->u.prefix4.s_addr;
738 iph.ip_dst.s_addr = op->dst.s_addr;
739
740 memset (&msg, 0, sizeof (msg));
paul68defd62004-09-27 07:27:13 +0000741 msg.msg_name = (caddr_t) &sa_dst;
paul718e3742002-12-13 20:15:29 +0000742 msg.msg_namelen = sizeof (sa_dst);
743 msg.msg_iov = iov;
744 msg.msg_iovlen = 2;
745 iov[0].iov_base = (char*)&iph;
paul68b73392004-09-12 14:21:37 +0000746 iov[0].iov_len = iph.ip_hl << OSPF_WRITE_IPHL_SHIFT;
747 iov[1].iov_base = STREAM_PNT (op->s);
paul718e3742002-12-13 20:15:29 +0000748 iov[1].iov_len = op->length;
paul68b73392004-09-12 14:21:37 +0000749
750 /* Sadly we can not rely on kernels to fragment packets because of either
751 * IP_HDRINCL and/or multicast destination being set.
752 */
paul0bfeca32004-09-24 08:07:54 +0000753#ifdef WANT_OSPF_WRITE_FRAGMENT
paul68b73392004-09-12 14:21:37 +0000754 if ( op->length > maxdatasize )
paul62d8e962004-11-02 20:26:45 +0000755 ospf_write_frags (ospf->fd, op, &iph, &msg, maxdatasize,
756 oi->ifp->mtu, flags, type);
paul0bfeca32004-09-24 08:07:54 +0000757#endif /* WANT_OSPF_WRITE_FRAGMENT */
paul68b73392004-09-12 14:21:37 +0000758
759 /* send final fragment (could be first) */
paul18b12c32004-10-05 14:38:29 +0000760 sockopt_iphdrincl_swab_htosys (&iph);
paul68980082003-03-25 05:07:42 +0000761 ret = sendmsg (ospf->fd, &msg, flags);
paul6b333612004-10-11 10:11:25 +0000762 sockopt_iphdrincl_swab_systoh (&iph);
paul718e3742002-12-13 20:15:29 +0000763
764 if (ret < 0)
ajs083ee9d2005-02-09 15:35:50 +0000765 zlog_warn ("*** sendmsg in ospf_write failed to %s, "
ajs5dcbdf82005-03-29 16:13:49 +0000766 "id %d, off %d, len %d, interface %s, mtu %u: %s",
ajs083ee9d2005-02-09 15:35:50 +0000767 inet_ntoa (iph.ip_dst), iph.ip_id, iph.ip_off, iph.ip_len,
ajs5dcbdf82005-03-29 16:13:49 +0000768 oi->ifp->name, oi->ifp->mtu, safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +0000769
paul718e3742002-12-13 20:15:29 +0000770 /* Show debug sending packet. */
771 if (IS_DEBUG_OSPF_PACKET (type - 1, SEND))
772 {
773 if (IS_DEBUG_OSPF_PACKET (type - 1, DETAIL))
774 {
ajs2a42e282004-12-08 18:43:03 +0000775 zlog_debug ("-----------------------------------------------------");
paul37ccfa32004-10-31 11:24:51 +0000776 ospf_ip_header_dump (&iph);
paul718e3742002-12-13 20:15:29 +0000777 stream_set_getp (op->s, 0);
778 ospf_packet_dump (op->s);
779 }
780
ajs2a42e282004-12-08 18:43:03 +0000781 zlog_debug ("%s sent to [%s] via [%s].",
Denis Ovsienko272ca1e2012-01-15 19:12:19 +0400782 LOOKUP (ospf_packet_type_str, type), inet_ntoa (op->dst),
paul718e3742002-12-13 20:15:29 +0000783 IF_NAME (oi));
784
785 if (IS_DEBUG_OSPF_PACKET (type - 1, DETAIL))
ajs2a42e282004-12-08 18:43:03 +0000786 zlog_debug ("-----------------------------------------------------");
paul718e3742002-12-13 20:15:29 +0000787 }
788
789 /* Now delete packet from queue. */
790 ospf_packet_delete (oi);
791
792 if (ospf_fifo_head (oi->obuf) == NULL)
793 {
794 oi->on_write_q = 0;
paul68980082003-03-25 05:07:42 +0000795 list_delete_node (ospf->oi_write_q, node);
paul718e3742002-12-13 20:15:29 +0000796 }
797
798 /* If packets still remain in queue, call write thread. */
paul68980082003-03-25 05:07:42 +0000799 if (!list_isempty (ospf->oi_write_q))
800 ospf->t_write =
801 thread_add_write (master, ospf_write, ospf, ospf->fd);
paul718e3742002-12-13 20:15:29 +0000802
803 return 0;
804}
805
806/* OSPF Hello message read -- RFC2328 Section 10.5. */
paul4dadc292005-05-06 21:37:42 +0000807static void
paul718e3742002-12-13 20:15:29 +0000808ospf_hello (struct ip *iph, struct ospf_header *ospfh,
809 struct stream * s, struct ospf_interface *oi, int size)
810{
811 struct ospf_hello *hello;
812 struct ospf_neighbor *nbr;
paul718e3742002-12-13 20:15:29 +0000813 int old_state;
pauld3f0d622004-05-05 15:27:15 +0000814 struct prefix p;
paul718e3742002-12-13 20:15:29 +0000815
816 /* increment statistics. */
817 oi->hello_in++;
818
819 hello = (struct ospf_hello *) STREAM_PNT (s);
820
821 /* If Hello is myself, silently discard. */
paul68980082003-03-25 05:07:42 +0000822 if (IPV4_ADDR_SAME (&ospfh->router_id, &oi->ospf->router_id))
pauld3241812003-09-29 12:42:39 +0000823 {
824 if (IS_DEBUG_OSPF_PACKET (ospfh->type - 1, RECV))
825 {
ajs2a42e282004-12-08 18:43:03 +0000826 zlog_debug ("ospf_header[%s/%s]: selforiginated, "
pauld3241812003-09-29 12:42:39 +0000827 "dropping.",
Denis Ovsienko272ca1e2012-01-15 19:12:19 +0400828 LOOKUP (ospf_packet_type_str, ospfh->type),
pauld3241812003-09-29 12:42:39 +0000829 inet_ntoa (iph->ip_src));
830 }
831 return;
832 }
paul718e3742002-12-13 20:15:29 +0000833
paul718e3742002-12-13 20:15:29 +0000834 /* get neighbor prefix. */
835 p.family = AF_INET;
836 p.prefixlen = ip_masklen (hello->network_mask);
837 p.u.prefix4 = iph->ip_src;
838
839 /* Compare network mask. */
840 /* Checking is ignored for Point-to-Point and Virtual link. */
841 if (oi->type != OSPF_IFTYPE_POINTOPOINT
842 && oi->type != OSPF_IFTYPE_VIRTUALLINK)
843 if (oi->address->prefixlen != p.prefixlen)
844 {
Andrew J. Schorr13cd3dc2006-07-11 01:50:30 +0000845 zlog_warn ("Packet %s [Hello:RECV]: NetworkMask mismatch on %s (configured prefix length is %d, but hello packet indicates %d).",
846 inet_ntoa(ospfh->router_id), IF_NAME(oi),
847 (int)oi->address->prefixlen, (int)p.prefixlen);
paul718e3742002-12-13 20:15:29 +0000848 return;
849 }
850
paul718e3742002-12-13 20:15:29 +0000851 /* Compare Router Dead Interval. */
852 if (OSPF_IF_PARAM (oi, v_wait) != ntohl (hello->dead_interval))
853 {
Andrew J. Schorr08c83672006-09-25 13:26:14 +0000854 zlog_warn ("Packet %s [Hello:RECV]: RouterDeadInterval mismatch "
855 "(expected %u, but received %u).",
856 inet_ntoa(ospfh->router_id),
857 OSPF_IF_PARAM(oi, v_wait), ntohl(hello->dead_interval));
paul718e3742002-12-13 20:15:29 +0000858 return;
859 }
860
paulf9ad9372005-10-21 00:45:17 +0000861 /* Compare Hello Interval - ignored if fast-hellos are set. */
862 if (OSPF_IF_PARAM (oi, fast_hello) == 0)
863 {
864 if (OSPF_IF_PARAM (oi, v_hello) != ntohs (hello->hello_interval))
865 {
Andrew J. Schorr08c83672006-09-25 13:26:14 +0000866 zlog_warn ("Packet %s [Hello:RECV]: HelloInterval mismatch "
867 "(expected %u, but received %u).",
868 inet_ntoa(ospfh->router_id),
869 OSPF_IF_PARAM(oi, v_hello), ntohs(hello->hello_interval));
paulf9ad9372005-10-21 00:45:17 +0000870 return;
871 }
872 }
873
paul718e3742002-12-13 20:15:29 +0000874 if (IS_DEBUG_OSPF_EVENT)
ajs2a42e282004-12-08 18:43:03 +0000875 zlog_debug ("Packet %s [Hello:RECV]: Options %s",
paul718e3742002-12-13 20:15:29 +0000876 inet_ntoa (ospfh->router_id),
877 ospf_options_dump (hello->options));
878
879 /* Compare options. */
880#define REJECT_IF_TBIT_ON 1 /* XXX */
881#ifdef REJECT_IF_TBIT_ON
882 if (CHECK_FLAG (hello->options, OSPF_OPTION_T))
883 {
884 /*
885 * This router does not support non-zero TOS.
886 * Drop this Hello packet not to establish neighbor relationship.
887 */
888 zlog_warn ("Packet %s [Hello:RECV]: T-bit on, drop it.",
889 inet_ntoa (ospfh->router_id));
890 return;
891 }
892#endif /* REJECT_IF_TBIT_ON */
893
894#ifdef HAVE_OPAQUE_LSA
paul68980082003-03-25 05:07:42 +0000895 if (CHECK_FLAG (oi->ospf->config, OSPF_OPAQUE_CAPABLE)
paul718e3742002-12-13 20:15:29 +0000896 && CHECK_FLAG (hello->options, OSPF_OPTION_O))
897 {
898 /*
899 * This router does know the correct usage of O-bit
900 * the bit should be set in DD packet only.
901 */
902 zlog_warn ("Packet %s [Hello:RECV]: O-bit abuse?",
903 inet_ntoa (ospfh->router_id));
904#ifdef STRICT_OBIT_USAGE_CHECK
905 return; /* Reject this packet. */
906#else /* STRICT_OBIT_USAGE_CHECK */
907 UNSET_FLAG (hello->options, OSPF_OPTION_O); /* Ignore O-bit. */
908#endif /* STRICT_OBIT_USAGE_CHECK */
909 }
910#endif /* HAVE_OPAQUE_LSA */
911
912 /* new for NSSA is to ensure that NP is on and E is off */
913
paul718e3742002-12-13 20:15:29 +0000914 if (oi->area->external_routing == OSPF_AREA_NSSA)
915 {
916 if (! (CHECK_FLAG (OPTIONS (oi), OSPF_OPTION_NP)
917 && CHECK_FLAG (hello->options, OSPF_OPTION_NP)
918 && ! CHECK_FLAG (OPTIONS (oi), OSPF_OPTION_E)
919 && ! CHECK_FLAG (hello->options, OSPF_OPTION_E)))
920 {
921 zlog_warn ("NSSA-Packet-%s[Hello:RECV]: my options: %x, his options %x", inet_ntoa (ospfh->router_id), OPTIONS (oi), hello->options);
922 return;
923 }
924 if (IS_DEBUG_OSPF_NSSA)
ajs2a42e282004-12-08 18:43:03 +0000925 zlog_debug ("NSSA-Hello:RECV:Packet from %s:", inet_ntoa(ospfh->router_id));
paul718e3742002-12-13 20:15:29 +0000926 }
927 else
paul718e3742002-12-13 20:15:29 +0000928 /* The setting of the E-bit found in the Hello Packet's Options
929 field must match this area's ExternalRoutingCapability A
930 mismatch causes processing to stop and the packet to be
931 dropped. The setting of the rest of the bits in the Hello
932 Packet's Options field should be ignored. */
933 if (CHECK_FLAG (OPTIONS (oi), OSPF_OPTION_E) !=
934 CHECK_FLAG (hello->options, OSPF_OPTION_E))
935 {
ajs3aa8d5f2004-12-11 18:00:06 +0000936 zlog_warn ("Packet %s [Hello:RECV]: my options: %x, his options %x",
937 inet_ntoa(ospfh->router_id), OPTIONS (oi), hello->options);
paul718e3742002-12-13 20:15:29 +0000938 return;
939 }
paul718e3742002-12-13 20:15:29 +0000940
pauld3f0d622004-05-05 15:27:15 +0000941 /* get neighbour struct */
942 nbr = ospf_nbr_get (oi, ospfh, iph, &p);
943
944 /* neighbour must be valid, ospf_nbr_get creates if none existed */
945 assert (nbr);
paul718e3742002-12-13 20:15:29 +0000946
947 old_state = nbr->state;
948
949 /* Add event to thread. */
Paul Jakma57c5c652010-01-07 06:12:53 +0000950 OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_PacketReceived);
paul718e3742002-12-13 20:15:29 +0000951
952 /* RFC2328 Section 9.5.1
953 If the router is not eligible to become Designated Router,
954 (snip) It must also send an Hello Packet in reply to an
955 Hello Packet received from any eligible neighbor (other than
956 the current Designated Router and Backup Designated Router). */
957 if (oi->type == OSPF_IFTYPE_NBMA)
958 if (PRIORITY(oi) == 0 && hello->priority > 0
959 && IPV4_ADDR_CMP(&DR(oi), &iph->ip_src)
960 && IPV4_ADDR_CMP(&BDR(oi), &iph->ip_src))
961 OSPF_NSM_TIMER_ON (nbr->t_hello_reply, ospf_hello_reply_timer,
962 OSPF_HELLO_REPLY_DELAY);
963
964 /* on NBMA network type, it happens to receive bidirectional Hello packet
965 without advance 1-Way Received event.
966 To avoid incorrect DR-seletion, raise 1-Way Received event.*/
967 if (oi->type == OSPF_IFTYPE_NBMA &&
968 (old_state == NSM_Down || old_state == NSM_Attempt))
969 {
Paul Jakma57c5c652010-01-07 06:12:53 +0000970 OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_OneWayReceived);
paul718e3742002-12-13 20:15:29 +0000971 nbr->priority = hello->priority;
972 nbr->d_router = hello->d_router;
973 nbr->bd_router = hello->bd_router;
974 return;
975 }
976
paul68980082003-03-25 05:07:42 +0000977 if (ospf_nbr_bidirectional (&oi->ospf->router_id, hello->neighbors,
paul718e3742002-12-13 20:15:29 +0000978 size - OSPF_HELLO_MIN_SIZE))
979 {
Paul Jakma57c5c652010-01-07 06:12:53 +0000980 OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_TwoWayReceived);
paul718e3742002-12-13 20:15:29 +0000981 nbr->options |= hello->options;
982 }
983 else
984 {
Paul Jakma57c5c652010-01-07 06:12:53 +0000985 OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_OneWayReceived);
paul718e3742002-12-13 20:15:29 +0000986 /* Set neighbor information. */
987 nbr->priority = hello->priority;
988 nbr->d_router = hello->d_router;
989 nbr->bd_router = hello->bd_router;
990 return;
991 }
992
993 /* If neighbor itself declares DR and no BDR exists,
994 cause event BackupSeen */
995 if (IPV4_ADDR_SAME (&nbr->address.u.prefix4, &hello->d_router))
996 if (hello->bd_router.s_addr == 0 && oi->state == ISM_Waiting)
997 OSPF_ISM_EVENT_SCHEDULE (oi, ISM_BackupSeen);
998
999 /* neighbor itself declares BDR. */
1000 if (oi->state == ISM_Waiting &&
1001 IPV4_ADDR_SAME (&nbr->address.u.prefix4, &hello->bd_router))
1002 OSPF_ISM_EVENT_SCHEDULE (oi, ISM_BackupSeen);
1003
1004 /* had not previously. */
1005 if ((IPV4_ADDR_SAME (&nbr->address.u.prefix4, &hello->d_router) &&
1006 IPV4_ADDR_CMP (&nbr->address.u.prefix4, &nbr->d_router)) ||
1007 (IPV4_ADDR_CMP (&nbr->address.u.prefix4, &hello->d_router) &&
1008 IPV4_ADDR_SAME (&nbr->address.u.prefix4, &nbr->d_router)))
1009 OSPF_ISM_EVENT_SCHEDULE (oi, ISM_NeighborChange);
1010
1011 /* had not previously. */
1012 if ((IPV4_ADDR_SAME (&nbr->address.u.prefix4, &hello->bd_router) &&
1013 IPV4_ADDR_CMP (&nbr->address.u.prefix4, &nbr->bd_router)) ||
1014 (IPV4_ADDR_CMP (&nbr->address.u.prefix4, &hello->bd_router) &&
1015 IPV4_ADDR_SAME (&nbr->address.u.prefix4, &nbr->bd_router)))
1016 OSPF_ISM_EVENT_SCHEDULE (oi, ISM_NeighborChange);
1017
1018 /* Neighbor priority check. */
1019 if (nbr->priority >= 0 && nbr->priority != hello->priority)
1020 OSPF_ISM_EVENT_SCHEDULE (oi, ISM_NeighborChange);
1021
1022 /* Set neighbor information. */
1023 nbr->priority = hello->priority;
1024 nbr->d_router = hello->d_router;
1025 nbr->bd_router = hello->bd_router;
1026}
1027
1028/* Save DD flags/options/Seqnum received. */
paul4dadc292005-05-06 21:37:42 +00001029static void
paul718e3742002-12-13 20:15:29 +00001030ospf_db_desc_save_current (struct ospf_neighbor *nbr,
1031 struct ospf_db_desc *dd)
1032{
1033 nbr->last_recv.flags = dd->flags;
1034 nbr->last_recv.options = dd->options;
1035 nbr->last_recv.dd_seqnum = ntohl (dd->dd_seqnum);
1036}
1037
1038/* Process rest of DD packet. */
1039static void
1040ospf_db_desc_proc (struct stream *s, struct ospf_interface *oi,
1041 struct ospf_neighbor *nbr, struct ospf_db_desc *dd,
1042 u_int16_t size)
1043{
1044 struct ospf_lsa *new, *find;
1045 struct lsa_header *lsah;
1046
paul9985f832005-02-09 15:51:56 +00001047 stream_forward_getp (s, OSPF_DB_DESC_MIN_SIZE);
paul718e3742002-12-13 20:15:29 +00001048 for (size -= OSPF_DB_DESC_MIN_SIZE;
1049 size >= OSPF_LSA_HEADER_SIZE; size -= OSPF_LSA_HEADER_SIZE)
1050 {
1051 lsah = (struct lsa_header *) STREAM_PNT (s);
paul9985f832005-02-09 15:51:56 +00001052 stream_forward_getp (s, OSPF_LSA_HEADER_SIZE);
paul718e3742002-12-13 20:15:29 +00001053
1054 /* Unknown LS type. */
1055 if (lsah->type < OSPF_MIN_LSA || lsah->type >= OSPF_MAX_LSA)
1056 {
ajsbec595a2004-11-30 22:38:43 +00001057 zlog_warn ("Packet [DD:RECV]: Unknown LS type %d.", lsah->type);
paul718e3742002-12-13 20:15:29 +00001058 OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_SeqNumberMismatch);
1059 return;
1060 }
1061
1062#ifdef HAVE_OPAQUE_LSA
1063 if (IS_OPAQUE_LSA (lsah->type)
1064 && ! CHECK_FLAG (nbr->options, OSPF_OPTION_O))
1065 {
1066 zlog_warn ("LSA[Type%d:%s]: Opaque capability mismatch?", lsah->type, inet_ntoa (lsah->id));
1067 OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_SeqNumberMismatch);
1068 return;
1069 }
1070#endif /* HAVE_OPAQUE_LSA */
1071
1072 switch (lsah->type)
1073 {
1074 case OSPF_AS_EXTERNAL_LSA:
1075#ifdef HAVE_OPAQUE_LSA
1076 case OSPF_OPAQUE_AS_LSA:
1077#endif /* HAVE_OPAQUE_LSA */
paul718e3742002-12-13 20:15:29 +00001078 /* Check for stub area. Reject if AS-External from stub but
1079 allow if from NSSA. */
1080 if (oi->area->external_routing == OSPF_AREA_STUB)
paul718e3742002-12-13 20:15:29 +00001081 {
1082 zlog_warn ("Packet [DD:RECV]: LSA[Type%d:%s] from %s area.",
1083 lsah->type, inet_ntoa (lsah->id),
1084 (oi->area->external_routing == OSPF_AREA_STUB) ?\
1085 "STUB" : "NSSA");
1086 OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_SeqNumberMismatch);
1087 return;
1088 }
1089 break;
1090 default:
1091 break;
1092 }
1093
1094 /* Create LS-request object. */
1095 new = ospf_ls_request_new (lsah);
1096
1097 /* Lookup received LSA, then add LS request list. */
1098 find = ospf_lsa_lookup_by_header (oi->area, lsah);
Paul Jakmaf0894cf2006-08-27 06:40:04 +00001099
1100 /* ospf_lsa_more_recent is fine with NULL pointers */
1101 switch (ospf_lsa_more_recent (find, new))
1102 {
1103 case -1:
1104 /* Neighbour has a more recent LSA, we must request it */
1105 ospf_ls_request_add (nbr, new);
1106 case 0:
1107 /* If we have a copy of this LSA, it's either less recent
1108 * and we're requesting it from neighbour (the case above), or
1109 * it's as recent and we both have same copy (this case).
1110 *
1111 * In neither of these two cases is there any point in
1112 * describing our copy of the LSA to the neighbour in a
1113 * DB-Summary packet, if we're still intending to do so.
1114 *
1115 * See: draft-ogier-ospf-dbex-opt-00.txt, describing the
1116 * backward compatible optimisation to OSPF DB Exchange /
1117 * DB Description process implemented here.
1118 */
1119 if (find)
1120 ospf_lsdb_delete (&nbr->db_sum, find);
1121 ospf_lsa_discard (new);
1122 break;
1123 default:
1124 /* We have the more recent copy, nothing specific to do:
1125 * - no need to request neighbours stale copy
1126 * - must leave DB summary list copy alone
1127 */
1128 if (IS_DEBUG_OSPF_EVENT)
1129 zlog_debug ("Packet [DD:RECV]: LSA received Type %d, "
1130 "ID %s is not recent.", lsah->type, inet_ntoa (lsah->id));
1131 ospf_lsa_discard (new);
1132 }
paul718e3742002-12-13 20:15:29 +00001133 }
1134
1135 /* Master */
1136 if (IS_SET_DD_MS (nbr->dd_flags))
1137 {
1138 nbr->dd_seqnum++;
Paul Jakma8dd24ee2006-08-27 06:29:30 +00001139
1140 /* Both sides have no More, then we're done with Exchange */
paul718e3742002-12-13 20:15:29 +00001141 if (!IS_SET_DD_M (dd->flags) && !IS_SET_DD_M (nbr->dd_flags))
1142 OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_ExchangeDone);
1143 else
paul718e3742002-12-13 20:15:29 +00001144 ospf_db_desc_send (nbr);
1145 }
1146 /* Slave */
1147 else
1148 {
1149 nbr->dd_seqnum = ntohl (dd->dd_seqnum);
1150
Paul Jakma8dd24ee2006-08-27 06:29:30 +00001151 /* Send DD packet in reply.
1152 *
1153 * Must be done to acknowledge the Master's DD, regardless of
1154 * whether we have more LSAs ourselves to describe.
1155 *
1156 * This function will clear the 'More' bit, if after this DD
1157 * we have no more LSAs to describe to the master..
1158 */
paul718e3742002-12-13 20:15:29 +00001159 ospf_db_desc_send (nbr);
Paul Jakma8dd24ee2006-08-27 06:29:30 +00001160
1161 /* Slave can raise ExchangeDone now, if master is also done */
1162 if (!IS_SET_DD_M (dd->flags) && !IS_SET_DD_M (nbr->dd_flags))
1163 OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_ExchangeDone);
paul718e3742002-12-13 20:15:29 +00001164 }
Paul Jakma8dd24ee2006-08-27 06:29:30 +00001165
paul718e3742002-12-13 20:15:29 +00001166 /* Save received neighbor values from DD. */
1167 ospf_db_desc_save_current (nbr, dd);
1168}
1169
paul4dadc292005-05-06 21:37:42 +00001170static int
paul718e3742002-12-13 20:15:29 +00001171ospf_db_desc_is_dup (struct ospf_db_desc *dd, struct ospf_neighbor *nbr)
1172{
1173 /* Is DD duplicated? */
1174 if (dd->options == nbr->last_recv.options &&
1175 dd->flags == nbr->last_recv.flags &&
1176 dd->dd_seqnum == htonl (nbr->last_recv.dd_seqnum))
1177 return 1;
1178
1179 return 0;
1180}
1181
1182/* OSPF Database Description message read -- RFC2328 Section 10.6. */
ajs3aa8d5f2004-12-11 18:00:06 +00001183static void
paul718e3742002-12-13 20:15:29 +00001184ospf_db_desc (struct ip *iph, struct ospf_header *ospfh,
1185 struct stream *s, struct ospf_interface *oi, u_int16_t size)
1186{
1187 struct ospf_db_desc *dd;
1188 struct ospf_neighbor *nbr;
1189
1190 /* Increment statistics. */
1191 oi->db_desc_in++;
1192
1193 dd = (struct ospf_db_desc *) STREAM_PNT (s);
pauld363df22003-06-19 00:26:34 +00001194
pauld3f0d622004-05-05 15:27:15 +00001195 nbr = ospf_nbr_lookup (oi, iph, ospfh);
paul718e3742002-12-13 20:15:29 +00001196 if (nbr == NULL)
1197 {
1198 zlog_warn ("Packet[DD]: Unknown Neighbor %s",
1199 inet_ntoa (ospfh->router_id));
1200 return;
1201 }
1202
1203 /* Check MTU. */
vincentba682532005-09-29 13:52:57 +00001204 if ((OSPF_IF_PARAM (oi, mtu_ignore) == 0) &&
1205 (ntohs (dd->mtu) > oi->ifp->mtu))
paul718e3742002-12-13 20:15:29 +00001206 {
ajs3aa8d5f2004-12-11 18:00:06 +00001207 zlog_warn ("Packet[DD]: Neighbor %s MTU %u is larger than [%s]'s MTU %u",
1208 inet_ntoa (nbr->router_id), ntohs (dd->mtu),
1209 IF_NAME (oi), oi->ifp->mtu);
paul718e3742002-12-13 20:15:29 +00001210 return;
1211 }
1212
pauld363df22003-06-19 00:26:34 +00001213 /*
1214 * XXX HACK by Hasso Tepper. Setting N/P bit in NSSA area DD packets is not
1215 * required. In fact at least JunOS sends DD packets with P bit clear.
1216 * Until proper solution is developped, this hack should help.
1217 *
1218 * Update: According to the RFCs, N bit is specified /only/ for Hello
1219 * options, unfortunately its use in DD options is not specified. Hence some
1220 * implementations follow E-bit semantics and set it in DD options, and some
1221 * treat it as unspecified and hence follow the directive "default for
1222 * options is clear", ie unset.
1223 *
1224 * Reset the flag, as ospfd follows E-bit semantics.
1225 */
1226 if ( (oi->area->external_routing == OSPF_AREA_NSSA)
1227 && (CHECK_FLAG (nbr->options, OSPF_OPTION_NP))
1228 && (!CHECK_FLAG (dd->options, OSPF_OPTION_NP)) )
1229 {
1230 if (IS_DEBUG_OSPF_EVENT)
ajs1210fa62004-12-03 16:43:24 +00001231 zlog_debug ("Packet[DD]: Neighbour %s: Has NSSA capability, sends with N bit clear in DD options",
pauld363df22003-06-19 00:26:34 +00001232 inet_ntoa (nbr->router_id) );
1233 SET_FLAG (dd->options, OSPF_OPTION_NP);
1234 }
pauld363df22003-06-19 00:26:34 +00001235
paul718e3742002-12-13 20:15:29 +00001236#ifdef REJECT_IF_TBIT_ON
1237 if (CHECK_FLAG (dd->options, OSPF_OPTION_T))
1238 {
1239 /*
1240 * In Hello protocol, optional capability must have checked
1241 * to prevent this T-bit enabled router be my neighbor.
1242 */
1243 zlog_warn ("Packet[DD]: Neighbor %s: T-bit on?", inet_ntoa (nbr->router_id));
1244 return;
1245 }
1246#endif /* REJECT_IF_TBIT_ON */
1247
1248#ifdef HAVE_OPAQUE_LSA
1249 if (CHECK_FLAG (dd->options, OSPF_OPTION_O)
paul68980082003-03-25 05:07:42 +00001250 && !CHECK_FLAG (oi->ospf->config, OSPF_OPAQUE_CAPABLE))
paul718e3742002-12-13 20:15:29 +00001251 {
1252 /*
1253 * This node is not configured to handle O-bit, for now.
1254 * Clear it to ignore unsupported capability proposed by neighbor.
1255 */
1256 UNSET_FLAG (dd->options, OSPF_OPTION_O);
1257 }
1258#endif /* HAVE_OPAQUE_LSA */
1259
Paul Jakma57c5c652010-01-07 06:12:53 +00001260 /* Add event to thread. */
1261 OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_PacketReceived);
1262
paul718e3742002-12-13 20:15:29 +00001263 /* Process DD packet by neighbor status. */
1264 switch (nbr->state)
1265 {
1266 case NSM_Down:
1267 case NSM_Attempt:
1268 case NSM_TwoWay:
ajsbec595a2004-11-30 22:38:43 +00001269 zlog_warn ("Packet[DD]: Neighbor %s state is %s, packet discarded.",
ajs3aa8d5f2004-12-11 18:00:06 +00001270 inet_ntoa(nbr->router_id),
paul718e3742002-12-13 20:15:29 +00001271 LOOKUP (ospf_nsm_state_msg, nbr->state));
1272 break;
1273 case NSM_Init:
1274 OSPF_NSM_EVENT_EXECUTE (nbr, NSM_TwoWayReceived);
1275 /* If the new state is ExStart, the processing of the current
1276 packet should then continue in this new state by falling
1277 through to case ExStart below. */
1278 if (nbr->state != NSM_ExStart)
1279 break;
1280 case NSM_ExStart:
1281 /* Initial DBD */
1282 if ((IS_SET_DD_ALL (dd->flags) == OSPF_DD_FLAG_ALL) &&
1283 (size == OSPF_DB_DESC_MIN_SIZE))
1284 {
paul68980082003-03-25 05:07:42 +00001285 if (IPV4_ADDR_CMP (&nbr->router_id, &oi->ospf->router_id) > 0)
paul718e3742002-12-13 20:15:29 +00001286 {
1287 /* We're Slave---obey */
ajs17eaa722004-12-29 21:04:48 +00001288 zlog_info ("Packet[DD]: Neighbor %s Negotiation done (Slave).",
ajs3aa8d5f2004-12-11 18:00:06 +00001289 inet_ntoa(nbr->router_id));
paul718e3742002-12-13 20:15:29 +00001290 nbr->dd_seqnum = ntohl (dd->dd_seqnum);
Paul Jakma8dd24ee2006-08-27 06:29:30 +00001291
1292 /* Reset I/MS */
1293 UNSET_FLAG (nbr->dd_flags, (OSPF_DD_FLAG_MS|OSPF_DD_FLAG_I));
paul718e3742002-12-13 20:15:29 +00001294 }
1295 else
1296 {
1297 /* We're Master, ignore the initial DBD from Slave */
paul6d452762005-11-03 11:15:44 +00001298 zlog_info ("Packet[DD]: Neighbor %s: Initial DBD from Slave, "
ajs3aa8d5f2004-12-11 18:00:06 +00001299 "ignoring.", inet_ntoa(nbr->router_id));
paul718e3742002-12-13 20:15:29 +00001300 break;
1301 }
1302 }
1303 /* Ack from the Slave */
1304 else if (!IS_SET_DD_MS (dd->flags) && !IS_SET_DD_I (dd->flags) &&
1305 ntohl (dd->dd_seqnum) == nbr->dd_seqnum &&
paul68980082003-03-25 05:07:42 +00001306 IPV4_ADDR_CMP (&nbr->router_id, &oi->ospf->router_id) < 0)
paul718e3742002-12-13 20:15:29 +00001307 {
ajs17eaa722004-12-29 21:04:48 +00001308 zlog_info ("Packet[DD]: Neighbor %s Negotiation done (Master).",
ajs3aa8d5f2004-12-11 18:00:06 +00001309 inet_ntoa(nbr->router_id));
Paul Jakma8dd24ee2006-08-27 06:29:30 +00001310 /* Reset I, leaving MS */
1311 UNSET_FLAG (nbr->dd_flags, OSPF_DD_FLAG_I);
paul718e3742002-12-13 20:15:29 +00001312 }
1313 else
1314 {
ajs3aa8d5f2004-12-11 18:00:06 +00001315 zlog_warn ("Packet[DD]: Neighbor %s Negotiation fails.",
1316 inet_ntoa(nbr->router_id));
paul718e3742002-12-13 20:15:29 +00001317 break;
1318 }
1319
1320 /* This is where the real Options are saved */
1321 nbr->options = dd->options;
1322
1323#ifdef HAVE_OPAQUE_LSA
paul68980082003-03-25 05:07:42 +00001324 if (CHECK_FLAG (oi->ospf->config, OSPF_OPAQUE_CAPABLE))
paul718e3742002-12-13 20:15:29 +00001325 {
1326 if (IS_DEBUG_OSPF_EVENT)
ajs2a42e282004-12-08 18:43:03 +00001327 zlog_debug ("Neighbor[%s] is %sOpaque-capable.",
paul718e3742002-12-13 20:15:29 +00001328 inet_ntoa (nbr->router_id),
1329 CHECK_FLAG (nbr->options, OSPF_OPTION_O) ? "" : "NOT ");
1330
1331 if (! CHECK_FLAG (nbr->options, OSPF_OPTION_O)
1332 && IPV4_ADDR_SAME (&DR (oi), &nbr->address.u.prefix4))
1333 {
paul6d452762005-11-03 11:15:44 +00001334 zlog_warn ("DR-neighbor[%s] is NOT opaque-capable; "
1335 "Opaque-LSAs cannot be reliably advertised "
1336 "in this network.",
1337 inet_ntoa (nbr->router_id));
paul718e3742002-12-13 20:15:29 +00001338 /* This situation is undesirable, but not a real error. */
1339 }
1340 }
1341#endif /* HAVE_OPAQUE_LSA */
1342
1343 OSPF_NSM_EVENT_EXECUTE (nbr, NSM_NegotiationDone);
1344
1345 /* continue processing rest of packet. */
1346 ospf_db_desc_proc (s, oi, nbr, dd, size);
1347 break;
1348 case NSM_Exchange:
1349 if (ospf_db_desc_is_dup (dd, nbr))
1350 {
1351 if (IS_SET_DD_MS (nbr->dd_flags))
1352 /* Master: discard duplicated DD packet. */
paul6d452762005-11-03 11:15:44 +00001353 zlog_info ("Packet[DD] (Master): Neighbor %s packet duplicated.",
ajs3aa8d5f2004-12-11 18:00:06 +00001354 inet_ntoa (nbr->router_id));
paul718e3742002-12-13 20:15:29 +00001355 else
1356 /* Slave: cause to retransmit the last Database Description. */
1357 {
paul6d452762005-11-03 11:15:44 +00001358 zlog_info ("Packet[DD] [Slave]: Neighbor %s packet duplicated.",
ajs3aa8d5f2004-12-11 18:00:06 +00001359 inet_ntoa (nbr->router_id));
paul718e3742002-12-13 20:15:29 +00001360 ospf_db_desc_resend (nbr);
1361 }
1362 break;
1363 }
1364
1365 /* Otherwise DD packet should be checked. */
1366 /* Check Master/Slave bit mismatch */
1367 if (IS_SET_DD_MS (dd->flags) != IS_SET_DD_MS (nbr->last_recv.flags))
1368 {
ajs3aa8d5f2004-12-11 18:00:06 +00001369 zlog_warn ("Packet[DD]: Neighbor %s MS-bit mismatch.",
1370 inet_ntoa(nbr->router_id));
paul718e3742002-12-13 20:15:29 +00001371 OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_SeqNumberMismatch);
1372 if (IS_DEBUG_OSPF_EVENT)
ajs2a42e282004-12-08 18:43:03 +00001373 zlog_debug ("Packet[DD]: dd->flags=%d, nbr->dd_flags=%d",
ajs3aa8d5f2004-12-11 18:00:06 +00001374 dd->flags, nbr->dd_flags);
paul718e3742002-12-13 20:15:29 +00001375 break;
1376 }
1377
1378 /* Check initialize bit is set. */
1379 if (IS_SET_DD_I (dd->flags))
1380 {
paul6d452762005-11-03 11:15:44 +00001381 zlog_info ("Packet[DD]: Neighbor %s I-bit set.",
ajs3aa8d5f2004-12-11 18:00:06 +00001382 inet_ntoa(nbr->router_id));
paul718e3742002-12-13 20:15:29 +00001383 OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_SeqNumberMismatch);
1384 break;
1385 }
1386
1387 /* Check DD Options. */
1388 if (dd->options != nbr->options)
1389 {
1390#ifdef ORIGINAL_CODING
1391 /* Save the new options for debugging */
1392 nbr->options = dd->options;
1393#endif /* ORIGINAL_CODING */
ajs3aa8d5f2004-12-11 18:00:06 +00001394 zlog_warn ("Packet[DD]: Neighbor %s options mismatch.",
1395 inet_ntoa(nbr->router_id));
paul718e3742002-12-13 20:15:29 +00001396 OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_SeqNumberMismatch);
1397 break;
1398 }
1399
1400 /* Check DD sequence number. */
1401 if ((IS_SET_DD_MS (nbr->dd_flags) &&
1402 ntohl (dd->dd_seqnum) != nbr->dd_seqnum) ||
1403 (!IS_SET_DD_MS (nbr->dd_flags) &&
1404 ntohl (dd->dd_seqnum) != nbr->dd_seqnum + 1))
1405 {
ajs3aa8d5f2004-12-11 18:00:06 +00001406 zlog_warn ("Packet[DD]: Neighbor %s sequence number mismatch.",
1407 inet_ntoa(nbr->router_id));
paul718e3742002-12-13 20:15:29 +00001408 OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_SeqNumberMismatch);
1409 break;
1410 }
1411
1412 /* Continue processing rest of packet. */
1413 ospf_db_desc_proc (s, oi, nbr, dd, size);
1414 break;
1415 case NSM_Loading:
1416 case NSM_Full:
1417 if (ospf_db_desc_is_dup (dd, nbr))
1418 {
1419 if (IS_SET_DD_MS (nbr->dd_flags))
1420 {
1421 /* Master should discard duplicate DD packet. */
paul6d452762005-11-03 11:15:44 +00001422 zlog_info ("Packet[DD]: Neighbor %s duplicated, "
1423 "packet discarded.",
ajs3aa8d5f2004-12-11 18:00:06 +00001424 inet_ntoa(nbr->router_id));
paul718e3742002-12-13 20:15:29 +00001425 break;
1426 }
1427 else
1428 {
1429 struct timeval t, now;
Paul Jakma2518efd2006-08-27 06:49:29 +00001430 quagga_gettime (QUAGGA_CLK_MONOTONIC, &now);
paul718e3742002-12-13 20:15:29 +00001431 t = tv_sub (now, nbr->last_send_ts);
1432 if (tv_cmp (t, int2tv (nbr->v_inactivity)) < 0)
1433 {
1434 /* In states Loading and Full the slave must resend
1435 its last Database Description packet in response to
1436 duplicate Database Description packets received
1437 from the master. For this reason the slave must
1438 wait RouterDeadInterval seconds before freeing the
1439 last Database Description packet. Reception of a
1440 Database Description packet from the master after
1441 this interval will generate a SeqNumberMismatch
1442 neighbor event. RFC2328 Section 10.8 */
1443 ospf_db_desc_resend (nbr);
1444 break;
1445 }
1446 }
1447 }
1448
1449 OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_SeqNumberMismatch);
1450 break;
1451 default:
ajs3aa8d5f2004-12-11 18:00:06 +00001452 zlog_warn ("Packet[DD]: Neighbor %s NSM illegal status %u.",
1453 inet_ntoa(nbr->router_id), nbr->state);
paul718e3742002-12-13 20:15:29 +00001454 break;
1455 }
1456}
1457
1458#define OSPF_LSA_KEY_SIZE 12 /* type(4) + id(4) + ar(4) */
1459
1460/* OSPF Link State Request Read -- RFC2328 Section 10.7. */
paul4dadc292005-05-06 21:37:42 +00001461static void
paul718e3742002-12-13 20:15:29 +00001462ospf_ls_req (struct ip *iph, struct ospf_header *ospfh,
1463 struct stream *s, struct ospf_interface *oi, u_int16_t size)
1464{
1465 struct ospf_neighbor *nbr;
1466 u_int32_t ls_type;
1467 struct in_addr ls_id;
1468 struct in_addr adv_router;
1469 struct ospf_lsa *find;
hasso52dc7ee2004-09-23 19:18:23 +00001470 struct list *ls_upd;
paul6c835672004-10-11 11:00:30 +00001471 unsigned int length;
paul718e3742002-12-13 20:15:29 +00001472
1473 /* Increment statistics. */
1474 oi->ls_req_in++;
1475
pauld3f0d622004-05-05 15:27:15 +00001476 nbr = ospf_nbr_lookup (oi, iph, ospfh);
paul718e3742002-12-13 20:15:29 +00001477 if (nbr == NULL)
1478 {
1479 zlog_warn ("Link State Request: Unknown Neighbor %s.",
1480 inet_ntoa (ospfh->router_id));
1481 return;
1482 }
1483
Paul Jakma57c5c652010-01-07 06:12:53 +00001484 /* Add event to thread. */
1485 OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_PacketReceived);
1486
paul718e3742002-12-13 20:15:29 +00001487 /* Neighbor State should be Exchange or later. */
1488 if (nbr->state != NSM_Exchange &&
1489 nbr->state != NSM_Loading &&
1490 nbr->state != NSM_Full)
1491 {
ajsbec595a2004-11-30 22:38:43 +00001492 zlog_warn ("Link State Request received from %s: "
1493 "Neighbor state is %s, packet discarded.",
1494 inet_ntoa (ospfh->router_id),
paul718e3742002-12-13 20:15:29 +00001495 LOOKUP (ospf_nsm_state_msg, nbr->state));
1496 return;
1497 }
1498
1499 /* Send Link State Update for ALL requested LSAs. */
1500 ls_upd = list_new ();
1501 length = OSPF_HEADER_SIZE + OSPF_LS_UPD_MIN_SIZE;
1502
1503 while (size >= OSPF_LSA_KEY_SIZE)
1504 {
1505 /* Get one slice of Link State Request. */
1506 ls_type = stream_getl (s);
1507 ls_id.s_addr = stream_get_ipv4 (s);
1508 adv_router.s_addr = stream_get_ipv4 (s);
1509
1510 /* Verify LSA type. */
1511 if (ls_type < OSPF_MIN_LSA || ls_type >= OSPF_MAX_LSA)
1512 {
1513 OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_BadLSReq);
1514 list_delete (ls_upd);
1515 return;
1516 }
1517
1518 /* Search proper LSA in LSDB. */
1519 find = ospf_lsa_lookup (oi->area, ls_type, ls_id, adv_router);
1520 if (find == NULL)
1521 {
1522 OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_BadLSReq);
1523 list_delete (ls_upd);
1524 return;
1525 }
1526
gdt86f1fd92005-01-10 14:20:43 +00001527 /* Packet overflows MTU size, send immediately. */
1528 if (length + ntohs (find->data->length) > ospf_packet_max (oi))
paul718e3742002-12-13 20:15:29 +00001529 {
1530 if (oi->type == OSPF_IFTYPE_NBMA)
1531 ospf_ls_upd_send (nbr, ls_upd, OSPF_SEND_PACKET_DIRECT);
1532 else
1533 ospf_ls_upd_send (nbr, ls_upd, OSPF_SEND_PACKET_INDIRECT);
1534
1535 /* Only remove list contents. Keep ls_upd. */
1536 list_delete_all_node (ls_upd);
1537
1538 length = OSPF_HEADER_SIZE + OSPF_LS_UPD_MIN_SIZE;
1539 }
1540
1541 /* Append LSA to update list. */
1542 listnode_add (ls_upd, find);
1543 length += ntohs (find->data->length);
1544
1545 size -= OSPF_LSA_KEY_SIZE;
1546 }
1547
1548 /* Send rest of Link State Update. */
1549 if (listcount (ls_upd) > 0)
1550 {
1551 if (oi->type == OSPF_IFTYPE_NBMA)
1552 ospf_ls_upd_send (nbr, ls_upd, OSPF_SEND_PACKET_DIRECT);
1553 else
1554 ospf_ls_upd_send (nbr, ls_upd, OSPF_SEND_PACKET_INDIRECT);
1555
1556 list_delete (ls_upd);
1557 }
1558 else
1559 list_free (ls_upd);
1560}
1561
1562/* Get the list of LSAs from Link State Update packet.
1563 And process some validation -- RFC2328 Section 13. (1)-(2). */
hasso52dc7ee2004-09-23 19:18:23 +00001564static struct list *
paul718e3742002-12-13 20:15:29 +00001565ospf_ls_upd_list_lsa (struct ospf_neighbor *nbr, struct stream *s,
1566 struct ospf_interface *oi, size_t size)
1567{
1568 u_int16_t count, sum;
1569 u_int32_t length;
1570 struct lsa_header *lsah;
1571 struct ospf_lsa *lsa;
hasso52dc7ee2004-09-23 19:18:23 +00001572 struct list *lsas;
paul718e3742002-12-13 20:15:29 +00001573
1574 lsas = list_new ();
1575
1576 count = stream_getl (s);
1577 size -= OSPF_LS_UPD_MIN_SIZE; /* # LSAs */
1578
1579 for (; size >= OSPF_LSA_HEADER_SIZE && count > 0;
paul9985f832005-02-09 15:51:56 +00001580 size -= length, stream_forward_getp (s, length), count--)
paul718e3742002-12-13 20:15:29 +00001581 {
1582 lsah = (struct lsa_header *) STREAM_PNT (s);
1583 length = ntohs (lsah->length);
1584
1585 if (length > size)
1586 {
1587 zlog_warn ("Link State Update: LSA length exceeds packet size.");
1588 break;
1589 }
1590
1591 /* Validate the LSA's LS checksum. */
1592 sum = lsah->checksum;
JR Riversd8a4e422012-09-13 17:17:36 +00001593 if (! ospf_lsa_checksum_valid (lsah))
paul718e3742002-12-13 20:15:29 +00001594 {
Jaroslav Fojtik223da1a2011-12-11 18:22:16 +04001595 /* (bug #685) more details in a one-line message make it possible
1596 * to identify problem source on the one hand and to have a better
1597 * chance to compress repeated messages in syslog on the other */
1598 zlog_warn ("Link State Update: LSA checksum error %x/%x, ID=%s from: nbr %s, router ID %s, adv router %s",
1599 sum, lsah->checksum, inet_ntoa (lsah->id),
1600 inet_ntoa (nbr->src), inet_ntoa (nbr->router_id),
1601 inet_ntoa (lsah->adv_router));
paul718e3742002-12-13 20:15:29 +00001602 continue;
1603 }
1604
1605 /* Examine the LSA's LS type. */
1606 if (lsah->type < OSPF_MIN_LSA || lsah->type >= OSPF_MAX_LSA)
1607 {
1608 zlog_warn ("Link State Update: Unknown LS type %d", lsah->type);
1609 continue;
1610 }
1611
1612 /*
1613 * What if the received LSA's age is greater than MaxAge?
1614 * Treat it as a MaxAge case -- endo.
1615 */
1616 if (ntohs (lsah->ls_age) > OSPF_LSA_MAXAGE)
1617 lsah->ls_age = htons (OSPF_LSA_MAXAGE);
1618
1619#ifdef HAVE_OPAQUE_LSA
1620 if (CHECK_FLAG (nbr->options, OSPF_OPTION_O))
1621 {
1622#ifdef STRICT_OBIT_USAGE_CHECK
1623 if ((IS_OPAQUE_LSA(lsah->type) &&
1624 ! CHECK_FLAG (lsah->options, OSPF_OPTION_O))
1625 || (! IS_OPAQUE_LSA(lsah->type) &&
1626 CHECK_FLAG (lsah->options, OSPF_OPTION_O)))
1627 {
1628 /*
1629 * This neighbor must know the exact usage of O-bit;
1630 * the bit will be set in Type-9,10,11 LSAs only.
1631 */
1632 zlog_warn ("LSA[Type%d:%s]: O-bit abuse?", lsah->type, inet_ntoa (lsah->id));
1633 continue;
1634 }
1635#endif /* STRICT_OBIT_USAGE_CHECK */
1636
1637 /* Do not take in AS External Opaque-LSAs if we are a stub. */
1638 if (lsah->type == OSPF_OPAQUE_AS_LSA
1639 && nbr->oi->area->external_routing != OSPF_AREA_DEFAULT)
1640 {
1641 if (IS_DEBUG_OSPF_EVENT)
ajs2a42e282004-12-08 18:43:03 +00001642 zlog_debug ("LSA[Type%d:%s]: We are a stub, don't take this LSA.", lsah->type, inet_ntoa (lsah->id));
paul718e3742002-12-13 20:15:29 +00001643 continue;
1644 }
1645 }
1646 else if (IS_OPAQUE_LSA(lsah->type))
1647 {
1648 zlog_warn ("LSA[Type%d:%s]: Opaque capability mismatch?", lsah->type, inet_ntoa (lsah->id));
1649 continue;
1650 }
1651#endif /* HAVE_OPAQUE_LSA */
1652
1653 /* Create OSPF LSA instance. */
1654 lsa = ospf_lsa_new ();
1655
1656 /* We may wish to put some error checking if type NSSA comes in
1657 and area not in NSSA mode */
1658 switch (lsah->type)
1659 {
1660 case OSPF_AS_EXTERNAL_LSA:
1661#ifdef HAVE_OPAQUE_LSA
1662 case OSPF_OPAQUE_AS_LSA:
1663 lsa->area = NULL;
1664 break;
1665 case OSPF_OPAQUE_LINK_LSA:
1666 lsa->oi = oi; /* Remember incoming interface for flooding control. */
1667 /* Fallthrough */
1668#endif /* HAVE_OPAQUE_LSA */
1669 default:
1670 lsa->area = oi->area;
1671 break;
1672 }
1673
1674 lsa->data = ospf_lsa_data_new (length);
1675 memcpy (lsa->data, lsah, length);
1676
1677 if (IS_DEBUG_OSPF_EVENT)
ajs2a42e282004-12-08 18:43:03 +00001678 zlog_debug("LSA[Type%d:%s]: %p new LSA created with Link State Update",
paul718e3742002-12-13 20:15:29 +00001679 lsa->data->type, inet_ntoa (lsa->data->id), lsa);
1680 listnode_add (lsas, lsa);
1681 }
1682
1683 return lsas;
1684}
1685
1686/* Cleanup Update list. */
paul4dadc292005-05-06 21:37:42 +00001687static void
hasso52dc7ee2004-09-23 19:18:23 +00001688ospf_upd_list_clean (struct list *lsas)
paul718e3742002-12-13 20:15:29 +00001689{
paul1eb8ef22005-04-07 07:30:20 +00001690 struct listnode *node, *nnode;
paul718e3742002-12-13 20:15:29 +00001691 struct ospf_lsa *lsa;
1692
paul1eb8ef22005-04-07 07:30:20 +00001693 for (ALL_LIST_ELEMENTS (lsas, node, nnode, lsa))
1694 ospf_lsa_discard (lsa);
paul718e3742002-12-13 20:15:29 +00001695
1696 list_delete (lsas);
1697}
1698
1699/* OSPF Link State Update message read -- RFC2328 Section 13. */
paul4dadc292005-05-06 21:37:42 +00001700static void
paul718e3742002-12-13 20:15:29 +00001701ospf_ls_upd (struct ip *iph, struct ospf_header *ospfh,
1702 struct stream *s, struct ospf_interface *oi, u_int16_t size)
1703{
1704 struct ospf_neighbor *nbr;
hasso52dc7ee2004-09-23 19:18:23 +00001705 struct list *lsas;
paul1eb8ef22005-04-07 07:30:20 +00001706 struct listnode *node, *nnode;
paul718e3742002-12-13 20:15:29 +00001707 struct ospf_lsa *lsa = NULL;
1708 /* unsigned long ls_req_found = 0; */
1709
1710 /* Dis-assemble the stream, update each entry, re-encapsulate for flooding */
1711
1712 /* Increment statistics. */
1713 oi->ls_upd_in++;
1714
1715 /* Check neighbor. */
pauld3f0d622004-05-05 15:27:15 +00001716 nbr = ospf_nbr_lookup (oi, iph, ospfh);
paul718e3742002-12-13 20:15:29 +00001717 if (nbr == NULL)
1718 {
1719 zlog_warn ("Link State Update: Unknown Neighbor %s on int: %s",
1720 inet_ntoa (ospfh->router_id), IF_NAME (oi));
1721 return;
1722 }
1723
Paul Jakma57c5c652010-01-07 06:12:53 +00001724 /* Add event to thread. */
1725 OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_PacketReceived);
1726
paul718e3742002-12-13 20:15:29 +00001727 /* Check neighbor state. */
1728 if (nbr->state < NSM_Exchange)
1729 {
ajs3aa8d5f2004-12-11 18:00:06 +00001730 zlog_warn ("Link State Update: "
1731 "Neighbor[%s] state %s is less than Exchange",
1732 inet_ntoa (ospfh->router_id),
1733 LOOKUP(ospf_nsm_state_msg, nbr->state));
paul718e3742002-12-13 20:15:29 +00001734 return;
1735 }
1736
1737 /* Get list of LSAs from Link State Update packet. - Also perorms Stages
1738 * 1 (validate LSA checksum) and 2 (check for LSA consistent type)
1739 * of section 13.
1740 */
1741 lsas = ospf_ls_upd_list_lsa (nbr, s, oi, size);
1742
1743#ifdef HAVE_OPAQUE_LSA
1744 /*
paul718e3742002-12-13 20:15:29 +00001745 * If self-originated Opaque-LSAs that have flooded before restart
1746 * are contained in the received LSUpd message, corresponding LSReq
1747 * messages to be sent may have to be modified.
1748 * To eliminate possible race conditions such that flushing and normal
1749 * updating for the same LSA would take place alternately, this trick
1750 * must be done before entering to the loop below.
1751 */
paul69310a62005-05-11 18:09:59 +00001752 /* XXX: Why is this Opaque specific? Either our core code is deficient
1753 * and this should be fixed generally, or Opaque is inventing strawman
1754 * problems */
paul718e3742002-12-13 20:15:29 +00001755 ospf_opaque_adjust_lsreq (nbr, lsas);
1756#endif /* HAVE_OPAQUE_LSA */
1757
1758#define DISCARD_LSA(L,N) {\
1759 if (IS_DEBUG_OSPF_EVENT) \
ajs2a42e282004-12-08 18:43:03 +00001760 zlog_debug ("ospf_lsa_discard() in ospf_ls_upd() point %d: lsa %p Type-%d", N, lsa, (int) lsa->data->type); \
paul718e3742002-12-13 20:15:29 +00001761 ospf_lsa_discard (L); \
1762 continue; }
1763
1764 /* Process each LSA received in the one packet. */
paul1eb8ef22005-04-07 07:30:20 +00001765 for (ALL_LIST_ELEMENTS (lsas, node, nnode, lsa))
paul718e3742002-12-13 20:15:29 +00001766 {
1767 struct ospf_lsa *ls_ret, *current;
1768 int ret = 1;
1769
paul718e3742002-12-13 20:15:29 +00001770 if (IS_DEBUG_OSPF_NSSA)
1771 {
1772 char buf1[INET_ADDRSTRLEN];
1773 char buf2[INET_ADDRSTRLEN];
1774 char buf3[INET_ADDRSTRLEN];
1775
ajs2a42e282004-12-08 18:43:03 +00001776 zlog_debug("LSA Type-%d from %s, ID: %s, ADV: %s",
paul718e3742002-12-13 20:15:29 +00001777 lsa->data->type,
1778 inet_ntop (AF_INET, &ospfh->router_id,
1779 buf1, INET_ADDRSTRLEN),
1780 inet_ntop (AF_INET, &lsa->data->id,
1781 buf2, INET_ADDRSTRLEN),
1782 inet_ntop (AF_INET, &lsa->data->adv_router,
1783 buf3, INET_ADDRSTRLEN));
1784 }
paul718e3742002-12-13 20:15:29 +00001785
1786 listnode_delete (lsas, lsa); /* We don't need it in list anymore */
1787
1788 /* Validate Checksum - Done above by ospf_ls_upd_list_lsa() */
1789
1790 /* LSA Type - Done above by ospf_ls_upd_list_lsa() */
1791
1792 /* Do not take in AS External LSAs if we are a stub or NSSA. */
1793
1794 /* Do not take in AS NSSA if this neighbor and we are not NSSA */
1795
1796 /* Do take in Type-7's if we are an NSSA */
1797
1798 /* If we are also an ABR, later translate them to a Type-5 packet */
1799
1800 /* Later, an NSSA Re-fresh can Re-fresh Type-7's and an ABR will
1801 translate them to a separate Type-5 packet. */
1802
1803 if (lsa->data->type == OSPF_AS_EXTERNAL_LSA)
1804 /* Reject from STUB or NSSA */
1805 if (nbr->oi->area->external_routing != OSPF_AREA_DEFAULT)
1806 {
paul718e3742002-12-13 20:15:29 +00001807 if (IS_DEBUG_OSPF_NSSA)
ajs2a42e282004-12-08 18:43:03 +00001808 zlog_debug("Incoming External LSA Discarded: We are NSSA/STUB Area");
Paul Jakma2cd754d2010-01-14 16:26:12 +03001809 DISCARD_LSA (lsa, 1);
paul718e3742002-12-13 20:15:29 +00001810 }
1811
paul718e3742002-12-13 20:15:29 +00001812 if (lsa->data->type == OSPF_AS_NSSA_LSA)
1813 if (nbr->oi->area->external_routing != OSPF_AREA_NSSA)
1814 {
paul718e3742002-12-13 20:15:29 +00001815 if (IS_DEBUG_OSPF_NSSA)
ajs2a42e282004-12-08 18:43:03 +00001816 zlog_debug("Incoming NSSA LSA Discarded: Not NSSA Area");
Paul Jakma2cd754d2010-01-14 16:26:12 +03001817 DISCARD_LSA (lsa,2);
paul718e3742002-12-13 20:15:29 +00001818 }
paul718e3742002-12-13 20:15:29 +00001819
1820 /* Find the LSA in the current database. */
1821
1822 current = ospf_lsa_lookup_by_header (oi->area, lsa->data);
1823
1824 /* If the LSA's LS age is equal to MaxAge, and there is currently
1825 no instance of the LSA in the router's link state database,
1826 and none of router's neighbors are in states Exchange or Loading,
1827 then take the following actions. */
1828
1829 if (IS_LSA_MAXAGE (lsa) && !current &&
paul68980082003-03-25 05:07:42 +00001830 (ospf_nbr_count (oi, NSM_Exchange) +
1831 ospf_nbr_count (oi, NSM_Loading)) == 0)
paul718e3742002-12-13 20:15:29 +00001832 {
1833 /* Response Link State Acknowledgment. */
1834 ospf_ls_ack_send (nbr, lsa);
1835
1836 /* Discard LSA. */
Ayan Banerjeefaf98752012-12-04 10:49:12 -08001837 if (IS_DEBUG_OSPF (lsa, LSA))
1838 {
1839 zlog_debug ("Link State Update[%s]: LS age is equal to MaxAge.",
1840 dump_lsa_key(lsa));
1841 }
paul718e3742002-12-13 20:15:29 +00001842 DISCARD_LSA (lsa, 3);
1843 }
1844
1845#ifdef HAVE_OPAQUE_LSA
1846 if (IS_OPAQUE_LSA (lsa->data->type)
paul68980082003-03-25 05:07:42 +00001847 && IPV4_ADDR_SAME (&lsa->data->adv_router, &oi->ospf->router_id))
paul718e3742002-12-13 20:15:29 +00001848 {
1849 /*
1850 * Even if initial flushing seems to be completed, there might
1851 * be a case that self-originated LSA with MaxAge still remain
1852 * in the routing domain.
1853 * Just send an LSAck message to cease retransmission.
1854 */
1855 if (IS_LSA_MAXAGE (lsa))
1856 {
1857 zlog_warn ("LSA[%s]: Boomerang effect?", dump_lsa_key (lsa));
1858 ospf_ls_ack_send (nbr, lsa);
1859 ospf_lsa_discard (lsa);
1860
1861 if (current != NULL && ! IS_LSA_MAXAGE (current))
1862 ospf_opaque_lsa_refresh_schedule (current);
1863 continue;
1864 }
1865
1866 /*
1867 * If an instance of self-originated Opaque-LSA is not found
1868 * in the LSDB, there are some possible cases here.
1869 *
1870 * 1) This node lost opaque-capability after restart.
1871 * 2) Else, a part of opaque-type is no more supported.
1872 * 3) Else, a part of opaque-id is no more supported.
1873 *
1874 * Anyway, it is still this node's responsibility to flush it.
1875 * Otherwise, the LSA instance remains in the routing domain
1876 * until its age reaches to MaxAge.
1877 */
paul69310a62005-05-11 18:09:59 +00001878 /* XXX: We should deal with this for *ALL* LSAs, not just opaque */
paul718e3742002-12-13 20:15:29 +00001879 if (current == NULL)
1880 {
1881 if (IS_DEBUG_OSPF_EVENT)
paul69310a62005-05-11 18:09:59 +00001882 zlog_debug ("LSA[%s]: Previously originated Opaque-LSA,"
1883 "not found in the LSDB.", dump_lsa_key (lsa));
paul718e3742002-12-13 20:15:29 +00001884
1885 SET_FLAG (lsa->flags, OSPF_LSA_SELF);
paul69310a62005-05-11 18:09:59 +00001886
1887 ospf_opaque_self_originated_lsa_received (nbr, lsa);
1888 ospf_ls_ack_send (nbr, lsa);
1889
paul718e3742002-12-13 20:15:29 +00001890 continue;
1891 }
1892 }
1893#endif /* HAVE_OPAQUE_LSA */
paul69310a62005-05-11 18:09:59 +00001894
hassocb05eb22004-02-11 21:10:19 +00001895 /* It might be happen that received LSA is self-originated network LSA, but
1896 * router ID is cahnged. So, we should check if LSA is a network-LSA whose
1897 * Link State ID is one of the router's own IP interface addresses but whose
1898 * Advertising Router is not equal to the router's own Router ID
1899 * According to RFC 2328 12.4.2 and 13.4 this LSA should be flushed.
1900 */
1901
1902 if(lsa->data->type == OSPF_NETWORK_LSA)
1903 {
paul1eb8ef22005-04-07 07:30:20 +00001904 struct listnode *oinode, *oinnode;
1905 struct ospf_interface *out_if;
hassocb05eb22004-02-11 21:10:19 +00001906 int Flag = 0;
1907
paul1eb8ef22005-04-07 07:30:20 +00001908 for (ALL_LIST_ELEMENTS (oi->ospf->oiflist, oinode, oinnode, out_if))
hassocb05eb22004-02-11 21:10:19 +00001909 {
hassocb05eb22004-02-11 21:10:19 +00001910 if(out_if == NULL)
1911 break;
1912
1913 if((IPV4_ADDR_SAME(&out_if->address->u.prefix4, &lsa->data->id)) &&
1914 (!(IPV4_ADDR_SAME(&oi->ospf->router_id, &lsa->data->adv_router))))
1915 {
1916 if(out_if->network_lsa_self)
1917 {
1918 ospf_lsa_flush_area(lsa,out_if->area);
1919 if(IS_DEBUG_OSPF_EVENT)
ajs2a42e282004-12-08 18:43:03 +00001920 zlog_debug ("ospf_lsa_discard() in ospf_ls_upd() point 9: lsa %p Type-%d",
hassocb05eb22004-02-11 21:10:19 +00001921 lsa, (int) lsa->data->type);
1922 ospf_lsa_discard (lsa);
1923 Flag = 1;
1924 }
1925 break;
1926 }
1927 }
1928 if(Flag)
1929 continue;
1930 }
paul718e3742002-12-13 20:15:29 +00001931
1932 /* (5) Find the instance of this LSA that is currently contained
1933 in the router's link state database. If there is no
1934 database copy, or the received LSA is more recent than
1935 the database copy the following steps must be performed. */
1936
1937 if (current == NULL ||
1938 (ret = ospf_lsa_more_recent (current, lsa)) < 0)
1939 {
1940 /* Actual flooding procedure. */
paul68980082003-03-25 05:07:42 +00001941 if (ospf_flood (oi->ospf, nbr, current, lsa) < 0) /* Trap NSSA later. */
paul718e3742002-12-13 20:15:29 +00001942 DISCARD_LSA (lsa, 4);
1943 continue;
1944 }
1945
1946 /* (6) Else, If there is an instance of the LSA on the sending
1947 neighbor's Link state request list, an error has occurred in
1948 the Database Exchange process. In this case, restart the
1949 Database Exchange process by generating the neighbor event
1950 BadLSReq for the sending neighbor and stop processing the
1951 Link State Update packet. */
1952
1953 if (ospf_ls_request_lookup (nbr, lsa))
1954 {
1955 OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_BadLSReq);
ajs3aa8d5f2004-12-11 18:00:06 +00001956 zlog_warn("LSA[%s] instance exists on Link state request list",
1957 dump_lsa_key(lsa));
paul718e3742002-12-13 20:15:29 +00001958
1959 /* Clean list of LSAs. */
1960 ospf_upd_list_clean (lsas);
1961 /* this lsa is not on lsas list already. */
1962 ospf_lsa_discard (lsa);
paul718e3742002-12-13 20:15:29 +00001963 return;
1964 }
1965
1966 /* If the received LSA is the same instance as the database copy
1967 (i.e., neither one is more recent) the following two steps
1968 should be performed: */
1969
1970 if (ret == 0)
1971 {
1972 /* If the LSA is listed in the Link state retransmission list
1973 for the receiving adjacency, the router itself is expecting
1974 an acknowledgment for this LSA. The router should treat the
1975 received LSA as an acknowledgment by removing the LSA from
1976 the Link state retransmission list. This is termed an
1977 "implied acknowledgment". */
1978
1979 ls_ret = ospf_ls_retransmit_lookup (nbr, lsa);
1980
1981 if (ls_ret != NULL)
1982 {
1983 ospf_ls_retransmit_delete (nbr, ls_ret);
1984
1985 /* Delayed acknowledgment sent if advertisement received
1986 from Designated Router, otherwise do nothing. */
1987 if (oi->state == ISM_Backup)
1988 if (NBR_IS_DR (nbr))
1989 listnode_add (oi->ls_ack, ospf_lsa_lock (lsa));
1990
1991 DISCARD_LSA (lsa, 5);
1992 }
1993 else
1994 /* Acknowledge the receipt of the LSA by sending a
1995 Link State Acknowledgment packet back out the receiving
1996 interface. */
1997 {
1998 ospf_ls_ack_send (nbr, lsa);
1999 DISCARD_LSA (lsa, 6);
2000 }
2001 }
2002
2003 /* The database copy is more recent. If the database copy
2004 has LS age equal to MaxAge and LS sequence number equal to
2005 MaxSequenceNumber, simply discard the received LSA without
2006 acknowledging it. (In this case, the LSA's LS sequence number is
2007 wrapping, and the MaxSequenceNumber LSA must be completely
2008 flushed before any new LSA instance can be introduced). */
2009
2010 else if (ret > 0) /* Database copy is more recent */
2011 {
2012 if (IS_LSA_MAXAGE (current) &&
2013 current->data->ls_seqnum == htonl (OSPF_MAX_SEQUENCE_NUMBER))
2014 {
2015 DISCARD_LSA (lsa, 7);
2016 }
2017 /* Otherwise, as long as the database copy has not been sent in a
2018 Link State Update within the last MinLSArrival seconds, send the
2019 database copy back to the sending neighbor, encapsulated within
2020 a Link State Update Packet. The Link State Update Packet should
2021 be sent directly to the neighbor. In so doing, do not put the
2022 database copy of the LSA on the neighbor's link state
2023 retransmission list, and do not acknowledge the received (less
2024 recent) LSA instance. */
2025 else
2026 {
2027 struct timeval now;
2028
Paul Jakma2518efd2006-08-27 06:49:29 +00002029 quagga_gettime (QUAGGA_CLK_MONOTONIC, &now);
paul718e3742002-12-13 20:15:29 +00002030
2031 if (tv_cmp (tv_sub (now, current->tv_orig),
Paul Jakma2c9f8e32010-01-11 16:22:12 +00002032 int2tv (OSPF_MIN_LS_ARRIVAL)) >= 0)
paul718e3742002-12-13 20:15:29 +00002033 /* Trap NSSA type later.*/
2034 ospf_ls_upd_send_lsa (nbr, current, OSPF_SEND_PACKET_DIRECT);
2035 DISCARD_LSA (lsa, 8);
2036 }
2037 }
2038 }
Paul Jakma2cd754d2010-01-14 16:26:12 +03002039#undef DISCARD_LSA
2040
paul718e3742002-12-13 20:15:29 +00002041 assert (listcount (lsas) == 0);
2042 list_delete (lsas);
2043}
2044
2045/* OSPF Link State Acknowledgment message read -- RFC2328 Section 13.7. */
paul4dadc292005-05-06 21:37:42 +00002046static void
paul718e3742002-12-13 20:15:29 +00002047ospf_ls_ack (struct ip *iph, struct ospf_header *ospfh,
2048 struct stream *s, struct ospf_interface *oi, u_int16_t size)
2049{
2050 struct ospf_neighbor *nbr;
paul69310a62005-05-11 18:09:59 +00002051
paul718e3742002-12-13 20:15:29 +00002052 /* increment statistics. */
2053 oi->ls_ack_in++;
2054
pauld3f0d622004-05-05 15:27:15 +00002055 nbr = ospf_nbr_lookup (oi, iph, ospfh);
paul718e3742002-12-13 20:15:29 +00002056 if (nbr == NULL)
2057 {
2058 zlog_warn ("Link State Acknowledgment: Unknown Neighbor %s.",
2059 inet_ntoa (ospfh->router_id));
2060 return;
2061 }
2062
Paul Jakma57c5c652010-01-07 06:12:53 +00002063 /* Add event to thread. */
2064 OSPF_NSM_EVENT_SCHEDULE (nbr, NSM_PacketReceived);
2065
paul718e3742002-12-13 20:15:29 +00002066 if (nbr->state < NSM_Exchange)
2067 {
ajs3aa8d5f2004-12-11 18:00:06 +00002068 zlog_warn ("Link State Acknowledgment: "
2069 "Neighbor[%s] state %s is less than Exchange",
2070 inet_ntoa (ospfh->router_id),
2071 LOOKUP(ospf_nsm_state_msg, nbr->state));
paul718e3742002-12-13 20:15:29 +00002072 return;
2073 }
paul69310a62005-05-11 18:09:59 +00002074
paul718e3742002-12-13 20:15:29 +00002075 while (size >= OSPF_LSA_HEADER_SIZE)
2076 {
2077 struct ospf_lsa *lsa, *lsr;
2078
2079 lsa = ospf_lsa_new ();
2080 lsa->data = (struct lsa_header *) STREAM_PNT (s);
2081
2082 /* lsah = (struct lsa_header *) STREAM_PNT (s); */
2083 size -= OSPF_LSA_HEADER_SIZE;
paul9985f832005-02-09 15:51:56 +00002084 stream_forward_getp (s, OSPF_LSA_HEADER_SIZE);
paul718e3742002-12-13 20:15:29 +00002085
2086 if (lsa->data->type < OSPF_MIN_LSA || lsa->data->type >= OSPF_MAX_LSA)
2087 {
2088 lsa->data = NULL;
2089 ospf_lsa_discard (lsa);
2090 continue;
2091 }
2092
2093 lsr = ospf_ls_retransmit_lookup (nbr, lsa);
2094
2095 if (lsr != NULL && lsr->data->ls_seqnum == lsa->data->ls_seqnum)
2096 {
2097#ifdef HAVE_OPAQUE_LSA
paul718e3742002-12-13 20:15:29 +00002098 if (IS_OPAQUE_LSA (lsr->data->type))
paul69310a62005-05-11 18:09:59 +00002099 ospf_opaque_ls_ack_received (nbr, lsr);
paul718e3742002-12-13 20:15:29 +00002100#endif /* HAVE_OPAQUE_LSA */
2101
2102 ospf_ls_retransmit_delete (nbr, lsr);
2103 }
2104
2105 lsa->data = NULL;
2106 ospf_lsa_discard (lsa);
2107 }
2108
paul718e3742002-12-13 20:15:29 +00002109 return;
paul718e3742002-12-13 20:15:29 +00002110}
2111
ajs038163f2005-02-17 19:55:59 +00002112static struct stream *
ajs5c333492005-02-23 15:43:01 +00002113ospf_recv_packet (int fd, struct interface **ifp, struct stream *ibuf)
paul718e3742002-12-13 20:15:29 +00002114{
2115 int ret;
ajs5c333492005-02-23 15:43:01 +00002116 struct ip *iph;
paul718e3742002-12-13 20:15:29 +00002117 u_int16_t ip_len;
paul718e3742002-12-13 20:15:29 +00002118 unsigned int ifindex = 0;
2119 struct iovec iov;
gdtd0deca62004-08-26 13:14:07 +00002120 /* Header and data both require alignment. */
gdte3049822004-08-26 13:19:40 +00002121 char buff [CMSG_SPACE(SOPT_SIZE_CMSG_IFINDEX_IPV4())];
paul2dd8bb42004-07-23 15:13:48 +00002122 struct msghdr msgh;
2123
paul68defd62004-09-27 07:27:13 +00002124 memset (&msgh, 0, sizeof (struct msghdr));
paul2dd8bb42004-07-23 15:13:48 +00002125 msgh.msg_iov = &iov;
2126 msgh.msg_iovlen = 1;
2127 msgh.msg_control = (caddr_t) buff;
2128 msgh.msg_controllen = sizeof (buff);
paul2dd8bb42004-07-23 15:13:48 +00002129
ajs5c333492005-02-23 15:43:01 +00002130 ret = stream_recvmsg (ibuf, fd, &msgh, 0, OSPF_MAX_PACKET_SIZE+1);
2131 if (ret < 0)
paul718e3742002-12-13 20:15:29 +00002132 {
ajs5c333492005-02-23 15:43:01 +00002133 zlog_warn("stream_recvmsg failed: %s", safe_strerror(errno));
2134 return NULL;
2135 }
paul69310a62005-05-11 18:09:59 +00002136 if ((unsigned int)ret < sizeof(iph)) /* ret must be > 0 now */
ajs5c333492005-02-23 15:43:01 +00002137 {
2138 zlog_warn("ospf_recv_packet: discarding runt packet of length %d "
2139 "(ip header size is %u)",
2140 ret, (u_int)sizeof(iph));
paul718e3742002-12-13 20:15:29 +00002141 return NULL;
2142 }
paul18b12c32004-10-05 14:38:29 +00002143
ajs5c333492005-02-23 15:43:01 +00002144 /* Note that there should not be alignment problems with this assignment
2145 because this is at the beginning of the stream data buffer. */
2146 iph = (struct ip *) STREAM_DATA(ibuf);
2147 sockopt_iphdrincl_swab_systoh (iph);
paul18b12c32004-10-05 14:38:29 +00002148
ajs5c333492005-02-23 15:43:01 +00002149 ip_len = iph->ip_len;
paul6b333612004-10-11 10:11:25 +00002150
Dmitrij Tejblumde5ccb92011-12-12 20:30:10 +04002151#if !defined(GNU_LINUX) && (OpenBSD < 200311) && (__FreeBSD_version < 1000000)
paul718e3742002-12-13 20:15:29 +00002152 /*
2153 * Kernel network code touches incoming IP header parameters,
2154 * before protocol specific processing.
2155 *
2156 * 1) Convert byteorder to host representation.
2157 * --> ip_len, ip_id, ip_off
2158 *
2159 * 2) Adjust ip_len to strip IP header size!
2160 * --> If user process receives entire IP packet via RAW
2161 * socket, it must consider adding IP header size to
2162 * the "ip_len" field of "ip" structure.
2163 *
2164 * For more details, see <netinet/ip_input.c>.
2165 */
ajs5c333492005-02-23 15:43:01 +00002166 ip_len = ip_len + (iph->ip_hl << 2);
paul718e3742002-12-13 20:15:29 +00002167#endif
2168
David BÉRARD0150c9c2010-05-11 10:17:53 +02002169#if defined(__DragonFly__)
2170 /*
2171 * in DragonFly's raw socket, ip_len/ip_off are read
2172 * in network byte order.
2173 * As OpenBSD < 200311 adjust ip_len to strip IP header size!
2174 */
2175 ip_len = ntohs(iph->ip_len) + (iph->ip_hl << 2);
2176#endif
2177
paul863082d2004-08-19 04:43:43 +00002178 ifindex = getsockopt_ifindex (AF_INET, &msgh);
paul718e3742002-12-13 20:15:29 +00002179
2180 *ifp = if_lookup_by_index (ifindex);
2181
2182 if (ret != ip_len)
2183 {
ajs5c333492005-02-23 15:43:01 +00002184 zlog_warn ("ospf_recv_packet read length mismatch: ip_len is %d, "
2185 "but recvmsg returned %d", ip_len, ret);
paul718e3742002-12-13 20:15:29 +00002186 return NULL;
2187 }
2188
2189 return ibuf;
2190}
2191
paul4dadc292005-05-06 21:37:42 +00002192static struct ospf_interface *
pauld3f0d622004-05-05 15:27:15 +00002193ospf_associate_packet_vl (struct ospf *ospf, struct interface *ifp,
paul718e3742002-12-13 20:15:29 +00002194 struct ip *iph, struct ospf_header *ospfh)
2195{
2196 struct ospf_interface *rcv_oi;
paul718e3742002-12-13 20:15:29 +00002197 struct ospf_vl_data *vl_data;
2198 struct ospf_area *vl_area;
hasso52dc7ee2004-09-23 19:18:23 +00002199 struct listnode *node;
paul718e3742002-12-13 20:15:29 +00002200
2201 if (IN_MULTICAST (ntohl (iph->ip_dst.s_addr)) ||
2202 !OSPF_IS_AREA_BACKBONE (ospfh))
pauld3f0d622004-05-05 15:27:15 +00002203 return NULL;
paul718e3742002-12-13 20:15:29 +00002204
pauld3f0d622004-05-05 15:27:15 +00002205 /* look for local OSPF interface matching the destination
2206 * to determine Area ID. We presume therefore the destination address
2207 * is unique, or at least (for "unnumbered" links), not used in other
2208 * areas
2209 */
2210 if ((rcv_oi = ospf_if_lookup_by_local_addr (ospf, NULL,
2211 iph->ip_dst)) == NULL)
2212 return NULL;
paul718e3742002-12-13 20:15:29 +00002213
paul1eb8ef22005-04-07 07:30:20 +00002214 for (ALL_LIST_ELEMENTS_RO (ospf->vlinks, node, vl_data))
paul718e3742002-12-13 20:15:29 +00002215 {
paul020709f2003-04-04 02:44:16 +00002216 vl_area = ospf_area_lookup_by_area_id (ospf, vl_data->vl_area_id);
paul718e3742002-12-13 20:15:29 +00002217 if (!vl_area)
2218 continue;
2219
2220 if (OSPF_AREA_SAME (&vl_area, &rcv_oi->area) &&
2221 IPV4_ADDR_SAME (&vl_data->vl_peer, &ospfh->router_id))
2222 {
2223 if (IS_DEBUG_OSPF_EVENT)
ajs2a42e282004-12-08 18:43:03 +00002224 zlog_debug ("associating packet with %s",
paul718e3742002-12-13 20:15:29 +00002225 IF_NAME (vl_data->vl_oi));
2226 if (! CHECK_FLAG (vl_data->vl_oi->ifp->flags, IFF_UP))
2227 {
2228 if (IS_DEBUG_OSPF_EVENT)
ajs2a42e282004-12-08 18:43:03 +00002229 zlog_debug ("This VL is not up yet, sorry");
paul718e3742002-12-13 20:15:29 +00002230 return NULL;
2231 }
2232
2233 return vl_data->vl_oi;
2234 }
2235 }
2236
2237 if (IS_DEBUG_OSPF_EVENT)
ajs2a42e282004-12-08 18:43:03 +00002238 zlog_debug ("couldn't find any VL to associate the packet with");
paul718e3742002-12-13 20:15:29 +00002239
pauld3f0d622004-05-05 15:27:15 +00002240 return NULL;
paul718e3742002-12-13 20:15:29 +00002241}
2242
Paul Jakmaf63f06d2011-04-08 12:44:43 +01002243static int
paul718e3742002-12-13 20:15:29 +00002244ospf_check_area_id (struct ospf_interface *oi, struct ospf_header *ospfh)
2245{
2246 /* Check match the Area ID of the receiving interface. */
2247 if (OSPF_AREA_SAME (&oi->area, &ospfh))
2248 return 1;
2249
2250 return 0;
2251}
2252
2253/* Unbound socket will accept any Raw IP packets if proto is matched.
2254 To prevent it, compare src IP address and i/f address with masking
2255 i/f network mask. */
paul4dadc292005-05-06 21:37:42 +00002256static int
paul718e3742002-12-13 20:15:29 +00002257ospf_check_network_mask (struct ospf_interface *oi, struct in_addr ip_src)
2258{
2259 struct in_addr mask, me, him;
2260
2261 if (oi->type == OSPF_IFTYPE_POINTOPOINT ||
2262 oi->type == OSPF_IFTYPE_VIRTUALLINK)
2263 return 1;
2264
2265 masklen2ip (oi->address->prefixlen, &mask);
2266
2267 me.s_addr = oi->address->u.prefix4.s_addr & mask.s_addr;
2268 him.s_addr = ip_src.s_addr & mask.s_addr;
2269
2270 if (IPV4_ADDR_SAME (&me, &him))
2271 return 1;
2272
2273 return 0;
2274}
2275
Denis Ovsienkobd5651f2012-02-26 17:59:43 +04002276/* Return 1, if the packet is properly authenticated and checksummed,
2277 0 otherwise. In particular, check that AuType header field is valid and
2278 matches the locally configured AuType, and that D.5 requirements are met. */
paul4dadc292005-05-06 21:37:42 +00002279static int
Denis Ovsienkoe5259142012-01-30 16:07:18 +04002280ospf_check_auth (struct ospf_interface *oi, struct ospf_header *ospfh)
paul718e3742002-12-13 20:15:29 +00002281{
paul718e3742002-12-13 20:15:29 +00002282 struct crypt_key *ck;
Denis Ovsienkobd5651f2012-02-26 17:59:43 +04002283 u_int16_t iface_auth_type;
2284 u_int16_t pkt_auth_type = ntohs (ospfh->auth_type);
paul718e3742002-12-13 20:15:29 +00002285
Denis Ovsienkobd5651f2012-02-26 17:59:43 +04002286 switch (pkt_auth_type)
2287 {
2288 case OSPF_AUTH_NULL: /* RFC2328 D.5.1 */
2289 if (OSPF_AUTH_NULL != (iface_auth_type = ospf_auth_type (oi)))
paul718e3742002-12-13 20:15:29 +00002290 {
Denis Ovsienkobd5651f2012-02-26 17:59:43 +04002291 if (IS_DEBUG_OSPF_PACKET (ospfh->type - 1, RECV))
2292 zlog_warn ("interface %s: auth-type mismatch, local %s, rcvd Null",
2293 IF_NAME (oi), LOOKUP (ospf_auth_type_str, iface_auth_type));
2294 return 0;
paul718e3742002-12-13 20:15:29 +00002295 }
Denis Ovsienkobd5651f2012-02-26 17:59:43 +04002296 if (! ospf_check_sum (ospfh))
2297 {
2298 if (IS_DEBUG_OSPF_PACKET (ospfh->type - 1, RECV))
2299 zlog_warn ("interface %s: Null auth OK, but checksum error, Router-ID %s",
2300 IF_NAME (oi), inet_ntoa (ospfh->router_id));
2301 return 0;
2302 }
2303 return 1;
2304 case OSPF_AUTH_SIMPLE: /* RFC2328 D.5.2 */
2305 if (OSPF_AUTH_SIMPLE != (iface_auth_type = ospf_auth_type (oi)))
2306 {
2307 if (IS_DEBUG_OSPF_PACKET (ospfh->type - 1, RECV))
2308 zlog_warn ("interface %s: auth-type mismatch, local %s, rcvd Simple",
2309 IF_NAME (oi), LOOKUP (ospf_auth_type_str, iface_auth_type));
2310 return 0;
2311 }
2312 if (memcmp (OSPF_IF_PARAM (oi, auth_simple), ospfh->u.auth_data, OSPF_AUTH_SIMPLE_SIZE))
2313 {
2314 if (IS_DEBUG_OSPF_PACKET (ospfh->type - 1, RECV))
2315 zlog_warn ("interface %s: Simple auth failed", IF_NAME (oi));
2316 return 0;
2317 }
2318 if (! ospf_check_sum (ospfh))
2319 {
2320 if (IS_DEBUG_OSPF_PACKET (ospfh->type - 1, RECV))
2321 zlog_warn ("interface %s: Simple auth OK, checksum error, Router-ID %s",
2322 IF_NAME (oi), inet_ntoa (ospfh->router_id));
2323 return 0;
2324 }
2325 return 1;
2326 case OSPF_AUTH_CRYPTOGRAPHIC: /* RFC2328 D.5.3 */
2327 if (OSPF_AUTH_CRYPTOGRAPHIC != (iface_auth_type = ospf_auth_type (oi)))
2328 {
2329 if (IS_DEBUG_OSPF_PACKET (ospfh->type - 1, RECV))
2330 zlog_warn ("interface %s: auth-type mismatch, local %s, rcvd Cryptographic",
2331 IF_NAME (oi), LOOKUP (ospf_auth_type_str, iface_auth_type));
2332 return 0;
2333 }
2334 if (ospfh->checksum)
2335 {
2336 if (IS_DEBUG_OSPF_PACKET (ospfh->type - 1, RECV))
2337 zlog_warn ("interface %s: OSPF header checksum is not 0", IF_NAME (oi));
2338 return 0;
2339 }
2340 /* only MD5 crypto method can pass ospf_packet_examin() */
2341 if
2342 (
2343 NULL == (ck = listgetdata (listtail(OSPF_IF_PARAM (oi,auth_crypt)))) ||
2344 ospfh->u.crypt.key_id != ck->key_id ||
2345 /* Condition above uses the last key ID on the list, which is
2346 different from what ospf_crypt_key_lookup() does. A bug? */
2347 ! ospf_check_md5_digest (oi, ospfh)
2348 )
2349 {
2350 if (IS_DEBUG_OSPF_PACKET (ospfh->type - 1, RECV))
2351 zlog_warn ("interface %s: MD5 auth failed", IF_NAME (oi));
2352 return 0;
2353 }
2354 return 1;
2355 default:
2356 if (IS_DEBUG_OSPF_PACKET (ospfh->type - 1, RECV))
2357 zlog_warn ("interface %s: invalid packet auth-type (%02x)",
2358 IF_NAME (oi), pkt_auth_type);
2359 return 0;
2360 }
paul718e3742002-12-13 20:15:29 +00002361}
2362
paul4dadc292005-05-06 21:37:42 +00002363static int
paul718e3742002-12-13 20:15:29 +00002364ospf_check_sum (struct ospf_header *ospfh)
2365{
2366 u_int32_t ret;
2367 u_int16_t sum;
paul718e3742002-12-13 20:15:29 +00002368
2369 /* clear auth_data for checksum. */
2370 memset (ospfh->u.auth_data, 0, OSPF_AUTH_SIMPLE_SIZE);
2371
2372 /* keep checksum and clear. */
2373 sum = ospfh->checksum;
2374 memset (&ospfh->checksum, 0, sizeof (u_int16_t));
2375
2376 /* calculate checksum. */
2377 ret = in_cksum (ospfh, ntohs (ospfh->length));
2378
2379 if (ret != sum)
2380 {
2381 zlog_info ("ospf_check_sum(): checksum mismatch, my %X, his %X",
2382 ret, sum);
2383 return 0;
2384 }
2385
2386 return 1;
2387}
2388
Denis Ovsienko4e31de72012-02-17 16:20:50 +04002389/* Verify, that given link/TOS records are properly sized/aligned and match
2390 Router-LSA "# links" and "# TOS" fields as specified in RFC2328 A.4.2. */
2391static unsigned
2392ospf_router_lsa_links_examin
2393(
2394 struct router_lsa_link * link,
2395 u_int16_t linkbytes,
2396 const u_int16_t num_links
2397)
2398{
2399 unsigned counted_links = 0, thislinklen;
2400
2401 while (linkbytes)
2402 {
2403 thislinklen = OSPF_ROUTER_LSA_LINK_SIZE + 4 * link->m[0].tos_count;
2404 if (thislinklen > linkbytes)
2405 {
2406 if (IS_DEBUG_OSPF_PACKET (0, RECV))
2407 zlog_debug ("%s: length error in link block #%u", __func__, counted_links);
2408 return MSG_NG;
2409 }
2410 link = (struct router_lsa_link *)((caddr_t) link + thislinklen);
2411 linkbytes -= thislinklen;
2412 counted_links++;
2413 }
2414 if (counted_links != num_links)
2415 {
2416 if (IS_DEBUG_OSPF_PACKET (0, RECV))
2417 zlog_debug ("%s: %u link blocks declared, %u present",
2418 __func__, num_links, counted_links);
2419 return MSG_NG;
2420 }
2421 return MSG_OK;
2422}
2423
2424/* Verify, that the given LSA is properly sized/aligned (including type-specific
2425 minimum length constraint). */
2426static unsigned
2427ospf_lsa_examin (struct lsa_header * lsah, const u_int16_t lsalen, const u_char headeronly)
2428{
2429 unsigned ret;
2430 struct router_lsa * rlsa;
2431 if
2432 (
2433 lsah->type < OSPF_MAX_LSA &&
2434 ospf_lsa_minlen[lsah->type] &&
2435 lsalen < OSPF_LSA_HEADER_SIZE + ospf_lsa_minlen[lsah->type]
2436 )
2437 {
2438 if (IS_DEBUG_OSPF_PACKET (0, RECV))
2439 zlog_debug ("%s: undersized (%u B) %s",
2440 __func__, lsalen, LOOKUP (ospf_lsa_type_msg, lsah->type));
2441 return MSG_NG;
2442 }
2443 switch (lsah->type)
2444 {
2445 case OSPF_ROUTER_LSA:
2446 /* RFC2328 A.4.2, LSA header + 4 bytes followed by N>=1 (12+)-byte link blocks */
2447 if (headeronly)
2448 {
2449 ret = (lsalen - OSPF_LSA_HEADER_SIZE - OSPF_ROUTER_LSA_MIN_SIZE) % 4 ? MSG_NG : MSG_OK;
2450 break;
2451 }
2452 rlsa = (struct router_lsa *) lsah;
2453 ret = ospf_router_lsa_links_examin
2454 (
2455 (struct router_lsa_link *) rlsa->link,
2456 lsalen - OSPF_LSA_HEADER_SIZE - 4, /* skip: basic header, "flags", 0, "# links" */
2457 ntohs (rlsa->links) /* 16 bits */
2458 );
2459 break;
2460 case OSPF_AS_EXTERNAL_LSA:
2461 /* RFC2328 A.4.5, LSA header + 4 bytes followed by N>=1 12-bytes long blocks */
2462 case OSPF_AS_NSSA_LSA:
2463 /* RFC3101 C, idem */
2464 ret = (lsalen - OSPF_LSA_HEADER_SIZE - OSPF_AS_EXTERNAL_LSA_MIN_SIZE) % 12 ? MSG_NG : MSG_OK;
2465 break;
2466 /* Following LSA types are considered OK length-wise as soon as their minimum
2467 * length constraint is met and length of the whole LSA is a multiple of 4
2468 * (basic LSA header size is already a multiple of 4). */
2469 case OSPF_NETWORK_LSA:
2470 /* RFC2328 A.4.3, LSA header + 4 bytes followed by N>=1 router-IDs */
2471 case OSPF_SUMMARY_LSA:
2472 case OSPF_ASBR_SUMMARY_LSA:
2473 /* RFC2328 A.4.4, LSA header + 4 bytes followed by N>=1 4-bytes TOS blocks */
2474#ifdef HAVE_OPAQUE_LSA
2475 case OSPF_OPAQUE_LINK_LSA:
2476 case OSPF_OPAQUE_AREA_LSA:
2477 case OSPF_OPAQUE_AS_LSA:
2478 /* RFC5250 A.2, "some number of octets (of application-specific
2479 * data) padded to 32-bit alignment." This is considered equivalent
2480 * to 4-byte alignment of all other LSA types, see OSPF-ALIGNMENT.txt
2481 * file for the detailed analysis of this passage. */
2482#endif
2483 ret = lsalen % 4 ? MSG_NG : MSG_OK;
2484 break;
2485 default:
2486 if (IS_DEBUG_OSPF_PACKET (0, RECV))
2487 zlog_debug ("%s: unsupported LSA type 0x%02x", __func__, lsah->type);
2488 return MSG_NG;
2489 }
2490 if (ret != MSG_OK && IS_DEBUG_OSPF_PACKET (0, RECV))
2491 zlog_debug ("%s: alignment error in %s",
2492 __func__, LOOKUP (ospf_lsa_type_msg, lsah->type));
2493 return ret;
2494}
2495
2496/* Verify if the provided input buffer is a valid sequence of LSAs. This
2497 includes verification of LSA blocks length/alignment and dispatching
2498 of deeper-level checks. */
2499static unsigned
2500ospf_lsaseq_examin
2501(
2502 struct lsa_header *lsah, /* start of buffered data */
2503 size_t length,
2504 const u_char headeronly,
2505 /* When declared_num_lsas is not 0, compare it to the real number of LSAs
2506 and treat the difference as an error. */
2507 const u_int32_t declared_num_lsas
2508)
2509{
2510 u_int32_t counted_lsas = 0;
2511
2512 while (length)
2513 {
2514 u_int16_t lsalen;
2515 if (length < OSPF_LSA_HEADER_SIZE)
2516 {
2517 if (IS_DEBUG_OSPF_PACKET (0, RECV))
2518 zlog_debug ("%s: undersized (%zu B) trailing (#%u) LSA header",
2519 __func__, length, counted_lsas);
2520 return MSG_NG;
2521 }
2522 /* save on ntohs() calls here and in the LSA validator */
2523 lsalen = ntohs (lsah->length);
2524 if (lsalen < OSPF_LSA_HEADER_SIZE)
2525 {
2526 if (IS_DEBUG_OSPF_PACKET (0, RECV))
2527 zlog_debug ("%s: malformed LSA header #%u, declared length is %u B",
2528 __func__, counted_lsas, lsalen);
2529 return MSG_NG;
2530 }
2531 if (headeronly)
2532 {
2533 /* less checks here and in ospf_lsa_examin() */
2534 if (MSG_OK != ospf_lsa_examin (lsah, lsalen, 1))
2535 {
2536 if (IS_DEBUG_OSPF_PACKET (0, RECV))
2537 zlog_debug ("%s: malformed header-only LSA #%u", __func__, counted_lsas);
2538 return MSG_NG;
2539 }
2540 lsah = (struct lsa_header *) ((caddr_t) lsah + OSPF_LSA_HEADER_SIZE);
2541 length -= OSPF_LSA_HEADER_SIZE;
2542 }
2543 else
2544 {
2545 /* make sure the input buffer is deep enough before further checks */
2546 if (lsalen > length)
2547 {
2548 if (IS_DEBUG_OSPF_PACKET (0, RECV))
2549 zlog_debug ("%s: anomaly in LSA #%u: declared length is %u B, buffered length is %zu B",
2550 __func__, counted_lsas, lsalen, length);
2551 return MSG_NG;
2552 }
2553 if (MSG_OK != ospf_lsa_examin (lsah, lsalen, 0))
2554 {
2555 if (IS_DEBUG_OSPF_PACKET (0, RECV))
2556 zlog_debug ("%s: malformed LSA #%u", __func__, counted_lsas);
2557 return MSG_NG;
2558 }
2559 lsah = (struct lsa_header *) ((caddr_t) lsah + lsalen);
2560 length -= lsalen;
2561 }
2562 counted_lsas++;
2563 }
2564
2565 if (declared_num_lsas && counted_lsas != declared_num_lsas)
2566 {
2567 if (IS_DEBUG_OSPF_PACKET (0, RECV))
2568 zlog_debug ("%s: #LSAs declared (%u) does not match actual (%u)",
2569 __func__, declared_num_lsas, counted_lsas);
2570 return MSG_NG;
2571 }
2572 return MSG_OK;
2573}
2574
Denis Ovsienko75c8eab2012-01-30 15:41:39 +04002575/* Verify a complete OSPF packet for proper sizing/alignment. */
2576static unsigned
2577ospf_packet_examin (struct ospf_header * oh, const unsigned bytesonwire)
2578{
Denis Ovsienkobd5651f2012-02-26 17:59:43 +04002579 u_int16_t bytesdeclared, bytesauth;
Denis Ovsienko4e31de72012-02-17 16:20:50 +04002580 unsigned ret;
2581 struct ospf_ls_update * lsupd;
Denis Ovsienko75c8eab2012-01-30 15:41:39 +04002582
2583 /* Length, 1st approximation. */
2584 if (bytesonwire < OSPF_HEADER_SIZE)
2585 {
2586 if (IS_DEBUG_OSPF_PACKET (0, RECV))
2587 zlog_debug ("%s: undersized (%u B) packet", __func__, bytesonwire);
2588 return MSG_NG;
2589 }
2590 /* Now it is safe to access header fields. Performing length check, allow
2591 * for possible extra bytes of crypto auth/padding, which are not counted
2592 * in the OSPF header "length" field. */
Denis Ovsienkoaee56742012-02-28 15:15:29 +04002593 if (oh->version != OSPF_VERSION)
2594 {
2595 if (IS_DEBUG_OSPF_PACKET (0, RECV))
2596 zlog_debug ("%s: invalid (%u) protocol version", __func__, oh->version);
2597 return MSG_NG;
2598 }
Denis Ovsienko75c8eab2012-01-30 15:41:39 +04002599 bytesdeclared = ntohs (oh->length);
Denis Ovsienkobd5651f2012-02-26 17:59:43 +04002600 if (ntohs (oh->auth_type) != OSPF_AUTH_CRYPTOGRAPHIC)
2601 bytesauth = 0;
2602 else
2603 {
2604 if (oh->u.crypt.auth_data_len != OSPF_AUTH_MD5_SIZE)
2605 {
2606 if (IS_DEBUG_OSPF_PACKET (0, RECV))
2607 zlog_debug ("%s: unsupported crypto auth length (%u B)",
2608 __func__, oh->u.crypt.auth_data_len);
2609 return MSG_NG;
2610 }
2611 bytesauth = OSPF_AUTH_MD5_SIZE;
2612 }
2613 if (bytesdeclared + bytesauth > bytesonwire)
Denis Ovsienko75c8eab2012-01-30 15:41:39 +04002614 {
2615 if (IS_DEBUG_OSPF_PACKET (0, RECV))
Denis Ovsienkobd5651f2012-02-26 17:59:43 +04002616 zlog_debug ("%s: packet length error (%u real, %u+%u declared)",
2617 __func__, bytesonwire, bytesdeclared, bytesauth);
Denis Ovsienko75c8eab2012-01-30 15:41:39 +04002618 return MSG_NG;
2619 }
2620 /* Length, 2nd approximation. The type-specific constraint is checked
2621 against declared length, not amount of bytes on wire. */
2622 if
2623 (
2624 oh->type >= OSPF_MSG_HELLO &&
2625 oh->type <= OSPF_MSG_LS_ACK &&
2626 bytesdeclared < OSPF_HEADER_SIZE + ospf_packet_minlen[oh->type]
2627 )
2628 {
2629 if (IS_DEBUG_OSPF_PACKET (0, RECV))
2630 zlog_debug ("%s: undersized (%u B) %s packet", __func__,
2631 bytesdeclared, LOOKUP (ospf_packet_type_str, oh->type));
2632 return MSG_NG;
2633 }
Denis Ovsienko4e31de72012-02-17 16:20:50 +04002634 switch (oh->type)
2635 {
2636 case OSPF_MSG_HELLO:
2637 /* RFC2328 A.3.2, packet header + OSPF_HELLO_MIN_SIZE bytes followed
2638 by N>=0 router-IDs. */
Denis Ovsienkob29adf92012-02-20 23:08:10 +04002639 ret = (bytesdeclared - OSPF_HEADER_SIZE - OSPF_HELLO_MIN_SIZE) % 4 ? MSG_NG : MSG_OK;
Denis Ovsienko4e31de72012-02-17 16:20:50 +04002640 break;
2641 case OSPF_MSG_DB_DESC:
2642 /* RFC2328 A.3.3, packet header + OSPF_DB_DESC_MIN_SIZE bytes followed
2643 by N>=0 header-only LSAs. */
2644 ret = ospf_lsaseq_examin
2645 (
2646 (struct lsa_header *) ((caddr_t) oh + OSPF_HEADER_SIZE + OSPF_DB_DESC_MIN_SIZE),
Denis Ovsienkob29adf92012-02-20 23:08:10 +04002647 bytesdeclared - OSPF_HEADER_SIZE - OSPF_DB_DESC_MIN_SIZE,
Denis Ovsienko4e31de72012-02-17 16:20:50 +04002648 1, /* header-only LSAs */
2649 0
2650 );
2651 break;
2652 case OSPF_MSG_LS_REQ:
2653 /* RFC2328 A.3.4, packet header followed by N>=0 12-bytes request blocks. */
Denis Ovsienkob29adf92012-02-20 23:08:10 +04002654 ret = (bytesdeclared - OSPF_HEADER_SIZE - OSPF_LS_REQ_MIN_SIZE) %
Denis Ovsienko4e31de72012-02-17 16:20:50 +04002655 OSPF_LSA_KEY_SIZE ? MSG_NG : MSG_OK;
2656 break;
2657 case OSPF_MSG_LS_UPD:
2658 /* RFC2328 A.3.5, packet header + OSPF_LS_UPD_MIN_SIZE bytes followed
2659 by N>=0 full LSAs (with N declared beforehand). */
2660 lsupd = (struct ospf_ls_update *) ((caddr_t) oh + OSPF_HEADER_SIZE);
2661 ret = ospf_lsaseq_examin
2662 (
2663 (struct lsa_header *) ((caddr_t) lsupd + OSPF_LS_UPD_MIN_SIZE),
Denis Ovsienkob29adf92012-02-20 23:08:10 +04002664 bytesdeclared - OSPF_HEADER_SIZE - OSPF_LS_UPD_MIN_SIZE,
Denis Ovsienko4e31de72012-02-17 16:20:50 +04002665 0, /* full LSAs */
2666 ntohl (lsupd->num_lsas) /* 32 bits */
2667 );
2668 break;
2669 case OSPF_MSG_LS_ACK:
2670 /* RFC2328 A.3.6, packet header followed by N>=0 header-only LSAs. */
2671 ret = ospf_lsaseq_examin
2672 (
2673 (struct lsa_header *) ((caddr_t) oh + OSPF_HEADER_SIZE + OSPF_LS_ACK_MIN_SIZE),
Denis Ovsienkob29adf92012-02-20 23:08:10 +04002674 bytesdeclared - OSPF_HEADER_SIZE - OSPF_LS_ACK_MIN_SIZE,
Denis Ovsienko4e31de72012-02-17 16:20:50 +04002675 1, /* header-only LSAs */
2676 0
2677 );
2678 break;
2679 default:
2680 if (IS_DEBUG_OSPF_PACKET (0, RECV))
2681 zlog_debug ("%s: invalid packet type 0x%02x", __func__, oh->type);
2682 return MSG_NG;
2683 }
2684 if (ret != MSG_OK && IS_DEBUG_OSPF_PACKET (0, RECV))
2685 zlog_debug ("%s: malformed %s packet", __func__, LOOKUP (ospf_packet_type_str, oh->type));
2686 return ret;
Denis Ovsienko75c8eab2012-01-30 15:41:39 +04002687}
2688
paul718e3742002-12-13 20:15:29 +00002689/* OSPF Header verification. */
paul4dadc292005-05-06 21:37:42 +00002690static int
paul718e3742002-12-13 20:15:29 +00002691ospf_verify_header (struct stream *ibuf, struct ospf_interface *oi,
2692 struct ip *iph, struct ospf_header *ospfh)
2693{
paul718e3742002-12-13 20:15:29 +00002694 /* Check Area ID. */
2695 if (!ospf_check_area_id (oi, ospfh))
2696 {
2697 zlog_warn ("interface %s: ospf_read invalid Area ID %s.",
2698 IF_NAME (oi), inet_ntoa (ospfh->area_id));
2699 return -1;
2700 }
2701
2702 /* Check network mask, Silently discarded. */
2703 if (! ospf_check_network_mask (oi, iph->ip_src))
2704 {
2705 zlog_warn ("interface %s: ospf_read network address is not same [%s]",
2706 IF_NAME (oi), inet_ntoa (iph->ip_src));
2707 return -1;
2708 }
2709
Denis Ovsienkobd5651f2012-02-26 17:59:43 +04002710 /* Check authentication. The function handles logging actions, where required. */
Denis Ovsienkoe5259142012-01-30 16:07:18 +04002711 if (! ospf_check_auth (oi, ospfh))
Denis Ovsienkobd5651f2012-02-26 17:59:43 +04002712 return -1;
paul718e3742002-12-13 20:15:29 +00002713
2714 return 0;
2715}
2716
2717/* Starting point of packet process function. */
2718int
2719ospf_read (struct thread *thread)
2720{
2721 int ret;
2722 struct stream *ibuf;
paul68980082003-03-25 05:07:42 +00002723 struct ospf *ospf;
paul718e3742002-12-13 20:15:29 +00002724 struct ospf_interface *oi;
2725 struct ip *iph;
2726 struct ospf_header *ospfh;
2727 u_int16_t length;
2728 struct interface *ifp;
2729
2730 /* first of all get interface pointer. */
paul68980082003-03-25 05:07:42 +00002731 ospf = THREAD_ARG (thread);
ajs038163f2005-02-17 19:55:59 +00002732
2733 /* prepare for next packet. */
2734 ospf->t_read = thread_add_read (master, ospf_read, ospf, ospf->fd);
paul718e3742002-12-13 20:15:29 +00002735
ajs5c333492005-02-23 15:43:01 +00002736 stream_reset(ospf->ibuf);
2737 if (!(ibuf = ospf_recv_packet (ospf->fd, &ifp, ospf->ibuf)))
paul718e3742002-12-13 20:15:29 +00002738 return -1;
Denis Ovsienko75c8eab2012-01-30 15:41:39 +04002739 /* This raw packet is known to be at least as big as its IP header. */
paul718e3742002-12-13 20:15:29 +00002740
ajs5c333492005-02-23 15:43:01 +00002741 /* Note that there should not be alignment problems with this assignment
2742 because this is at the beginning of the stream data buffer. */
paul06f953f2004-10-22 17:00:38 +00002743 iph = (struct ip *) STREAM_DATA (ibuf);
ajs5c333492005-02-23 15:43:01 +00002744 /* Note that sockopt_iphdrincl_swab_systoh was called in ospf_recv_packet. */
paul06f953f2004-10-22 17:00:38 +00002745
paulac191232004-10-22 12:05:17 +00002746 if (ifp == NULL)
ajsb87f7722004-12-29 20:41:26 +00002747 /* Handle cases where the platform does not support retrieving the ifindex,
2748 and also platforms (such as Solaris 8) that claim to support ifindex
2749 retrieval but do not. */
paulac191232004-10-22 12:05:17 +00002750 ifp = if_lookup_address (iph->ip_src);
paulac191232004-10-22 12:05:17 +00002751
pauld3f0d622004-05-05 15:27:15 +00002752 if (ifp == NULL)
ajs5c333492005-02-23 15:43:01 +00002753 return 0;
paul718e3742002-12-13 20:15:29 +00002754
2755 /* IP Header dump. */
paul17b78d32003-02-13 22:04:01 +00002756 if (IS_DEBUG_OSPF_PACKET(0, RECV))
paul6b333612004-10-11 10:11:25 +00002757 ospf_ip_header_dump (iph);
paul7d95c612003-01-27 12:00:55 +00002758
paul718e3742002-12-13 20:15:29 +00002759 /* Self-originated packet should be discarded silently. */
paul68980082003-03-25 05:07:42 +00002760 if (ospf_if_lookup_by_local_addr (ospf, NULL, iph->ip_src))
paul718e3742002-12-13 20:15:29 +00002761 {
pauld3241812003-09-29 12:42:39 +00002762 if (IS_DEBUG_OSPF_PACKET (0, RECV))
2763 {
ajs2a42e282004-12-08 18:43:03 +00002764 zlog_debug ("ospf_read[%s]: Dropping self-originated packet",
pauld3241812003-09-29 12:42:39 +00002765 inet_ntoa (iph->ip_src));
2766 }
paul718e3742002-12-13 20:15:29 +00002767 return 0;
2768 }
2769
Denis Ovsienko61ab0302011-09-26 13:17:52 +04002770 /* Advance from IP header to OSPF header (iph->ip_hl has been verified
2771 by ospf_recv_packet() to be correct). */
paul9985f832005-02-09 15:51:56 +00002772 stream_forward_getp (ibuf, iph->ip_hl * 4);
Denis Ovsienko61ab0302011-09-26 13:17:52 +04002773
paul718e3742002-12-13 20:15:29 +00002774 ospfh = (struct ospf_header *) STREAM_PNT (ibuf);
Denis Ovsienko75c8eab2012-01-30 15:41:39 +04002775 if (MSG_OK != ospf_packet_examin (ospfh, stream_get_endp (ibuf) - stream_get_getp (ibuf)))
2776 return -1;
2777 /* Now it is safe to access all fields of OSPF packet header. */
paul718e3742002-12-13 20:15:29 +00002778
2779 /* associate packet with ospf interface */
Joakim Tjernlund05cf46b2009-07-27 12:42:30 +02002780 oi = ospf_if_lookup_recv_if (ospf, iph->ip_src, ifp);
pauld3f0d622004-05-05 15:27:15 +00002781
YAMAMOTO Shigeru3aad46b2011-09-28 21:00:14 +04002782 /* ospf_verify_header() relies on a valid "oi" and thus can be called only
2783 after the passive/backbone/other checks below are passed. These checks
2784 in turn access the fields of unverified "ospfh" structure for their own
2785 purposes and must remain very accurate in doing this. */
Denis Ovsienko71775042011-09-26 13:18:02 +04002786
Joakim Tjernlund491eddc2008-09-24 17:03:59 +01002787 /* If incoming interface is passive one, ignore it. */
2788 if (oi && OSPF_IF_PASSIVE_STATUS (oi) == OSPF_IF_PASSIVE)
2789 {
2790 char buf[3][INET_ADDRSTRLEN];
2791
2792 if (IS_DEBUG_OSPF_EVENT)
2793 zlog_debug ("ignoring packet from router %s sent to %s, "
2794 "received on a passive interface, %s",
2795 inet_ntop(AF_INET, &ospfh->router_id, buf[0], sizeof(buf[0])),
2796 inet_ntop(AF_INET, &iph->ip_dst, buf[1], sizeof(buf[1])),
2797 inet_ntop(AF_INET, &oi->address->u.prefix4,
2798 buf[2], sizeof(buf[2])));
2799
2800 if (iph->ip_dst.s_addr == htonl(OSPF_ALLSPFROUTERS))
2801 {
2802 /* Try to fix multicast membership.
2803 * Some OS:es may have problems in this area,
2804 * make sure it is removed.
2805 */
2806 OI_MEMBER_JOINED(oi, MEMBER_ALLROUTERS);
2807 ospf_if_set_multicast(oi);
2808 }
2809 return 0;
2810 }
2811
2812
pauld3f0d622004-05-05 15:27:15 +00002813 /* if no local ospf_interface,
2814 * or header area is backbone but ospf_interface is not
2815 * check for VLINK interface
2816 */
2817 if ( (oi == NULL) ||
2818 (OSPF_IS_AREA_ID_BACKBONE(ospfh->area_id)
2819 && !OSPF_IS_AREA_ID_BACKBONE(oi->area->area_id))
2820 )
2821 {
2822 if ((oi = ospf_associate_packet_vl (ospf, ifp, iph, ospfh)) == NULL)
2823 {
Paul Jakma88871b12006-06-15 11:41:19 +00002824 if (IS_DEBUG_OSPF_EVENT)
2825 zlog_debug ("Packet from [%s] received on link %s"
2826 " but no ospf_interface",
2827 inet_ntoa (iph->ip_src), ifp->name);
pauld3f0d622004-05-05 15:27:15 +00002828 return 0;
2829 }
2830 }
Joakim Tjernlund05cf46b2009-07-27 12:42:30 +02002831
pauld3f0d622004-05-05 15:27:15 +00002832 /* else it must be a local ospf interface, check it was received on
2833 * correct link
2834 */
2835 else if (oi->ifp != ifp)
paul718e3742002-12-13 20:15:29 +00002836 {
Paul Jakma11637432009-08-11 12:25:42 +01002837 if (IS_DEBUG_OSPF_EVENT)
2838 zlog_warn ("Packet from [%s] received on wrong link %s",
2839 inet_ntoa (iph->ip_src), ifp->name);
paul718e3742002-12-13 20:15:29 +00002840 return 0;
2841 }
ajs847947f2005-02-02 18:38:48 +00002842 else if (oi->state == ISM_Down)
ajsc3eab872005-01-29 15:52:07 +00002843 {
ajsba6454e2005-02-08 15:37:30 +00002844 char buf[2][INET_ADDRSTRLEN];
2845 zlog_warn ("Ignoring packet from %s to %s received on interface that is "
ajs847947f2005-02-02 18:38:48 +00002846 "down [%s]; interface flags are %s",
ajsba6454e2005-02-08 15:37:30 +00002847 inet_ntop(AF_INET, &iph->ip_src, buf[0], sizeof(buf[0])),
2848 inet_ntop(AF_INET, &iph->ip_dst, buf[1], sizeof(buf[1])),
2849 ifp->name, if_flag_dump(ifp->flags));
ajsba6454e2005-02-08 15:37:30 +00002850 /* Fix multicast memberships? */
2851 if (iph->ip_dst.s_addr == htonl(OSPF_ALLSPFROUTERS))
Paul Jakma429ac782006-06-15 18:40:49 +00002852 OI_MEMBER_JOINED(oi, MEMBER_ALLROUTERS);
ajsba6454e2005-02-08 15:37:30 +00002853 else if (iph->ip_dst.s_addr == htonl(OSPF_ALLDROUTERS))
Paul Jakma429ac782006-06-15 18:40:49 +00002854 OI_MEMBER_JOINED(oi, MEMBER_DROUTERS);
ajsba6454e2005-02-08 15:37:30 +00002855 if (oi->multicast_memberships)
2856 ospf_if_set_multicast(oi);
ajsc3eab872005-01-29 15:52:07 +00002857 return 0;
2858 }
paul718e3742002-12-13 20:15:29 +00002859
2860 /*
2861 * If the received packet is destined for AllDRouters, the packet
2862 * should be accepted only if the received ospf interface state is
2863 * either DR or Backup -- endo.
2864 */
2865 if (iph->ip_dst.s_addr == htonl (OSPF_ALLDROUTERS)
2866 && (oi->state != ISM_DR && oi->state != ISM_Backup))
2867 {
ajsba6454e2005-02-08 15:37:30 +00002868 zlog_warn ("Dropping packet for AllDRouters from [%s] via [%s] (ISM: %s)",
paul718e3742002-12-13 20:15:29 +00002869 inet_ntoa (iph->ip_src), IF_NAME (oi),
2870 LOOKUP (ospf_ism_state_msg, oi->state));
ajsba6454e2005-02-08 15:37:30 +00002871 /* Try to fix multicast membership. */
2872 SET_FLAG(oi->multicast_memberships, MEMBER_DROUTERS);
2873 ospf_if_set_multicast(oi);
paul718e3742002-12-13 20:15:29 +00002874 return 0;
2875 }
2876
YAMAMOTO Shigeru3aad46b2011-09-28 21:00:14 +04002877 /* Verify more OSPF header fields. */
2878 ret = ospf_verify_header (ibuf, oi, iph, ospfh);
2879 if (ret < 0)
2880 {
2881 if (IS_DEBUG_OSPF_PACKET (0, RECV))
2882 zlog_debug ("ospf_read[%s]: Header check failed, "
2883 "dropping.",
2884 inet_ntoa (iph->ip_src));
2885 return ret;
2886 }
2887
paul718e3742002-12-13 20:15:29 +00002888 /* Show debug receiving packet. */
paul1aa7b392003-04-08 08:51:58 +00002889 if (IS_DEBUG_OSPF_PACKET (ospfh->type - 1, RECV))
2890 {
paul718e3742002-12-13 20:15:29 +00002891 if (IS_DEBUG_OSPF_PACKET (ospfh->type - 1, DETAIL))
paul1aa7b392003-04-08 08:51:58 +00002892 {
ajs2a42e282004-12-08 18:43:03 +00002893 zlog_debug ("-----------------------------------------------------");
paul1aa7b392003-04-08 08:51:58 +00002894 ospf_packet_dump (ibuf);
2895 }
paul718e3742002-12-13 20:15:29 +00002896
ajs2a42e282004-12-08 18:43:03 +00002897 zlog_debug ("%s received from [%s] via [%s]",
Denis Ovsienko272ca1e2012-01-15 19:12:19 +04002898 LOOKUP (ospf_packet_type_str, ospfh->type),
paul1aa7b392003-04-08 08:51:58 +00002899 inet_ntoa (ospfh->router_id), IF_NAME (oi));
ajs2a42e282004-12-08 18:43:03 +00002900 zlog_debug (" src [%s],", inet_ntoa (iph->ip_src));
2901 zlog_debug (" dst [%s]", inet_ntoa (iph->ip_dst));
paul718e3742002-12-13 20:15:29 +00002902
2903 if (IS_DEBUG_OSPF_PACKET (ospfh->type - 1, DETAIL))
ajs2a42e282004-12-08 18:43:03 +00002904 zlog_debug ("-----------------------------------------------------");
paul1aa7b392003-04-08 08:51:58 +00002905 }
paul718e3742002-12-13 20:15:29 +00002906
paul9985f832005-02-09 15:51:56 +00002907 stream_forward_getp (ibuf, OSPF_HEADER_SIZE);
paul718e3742002-12-13 20:15:29 +00002908
2909 /* Adjust size to message length. */
2910 length = ntohs (ospfh->length) - OSPF_HEADER_SIZE;
2911
2912 /* Read rest of the packet and call each sort of packet routine. */
2913 switch (ospfh->type)
2914 {
2915 case OSPF_MSG_HELLO:
2916 ospf_hello (iph, ospfh, ibuf, oi, length);
2917 break;
2918 case OSPF_MSG_DB_DESC:
2919 ospf_db_desc (iph, ospfh, ibuf, oi, length);
2920 break;
2921 case OSPF_MSG_LS_REQ:
2922 ospf_ls_req (iph, ospfh, ibuf, oi, length);
2923 break;
2924 case OSPF_MSG_LS_UPD:
2925 ospf_ls_upd (iph, ospfh, ibuf, oi, length);
2926 break;
2927 case OSPF_MSG_LS_ACK:
2928 ospf_ls_ack (iph, ospfh, ibuf, oi, length);
2929 break;
2930 default:
2931 zlog (NULL, LOG_WARNING,
2932 "interface %s: OSPF packet header type %d is illegal",
2933 IF_NAME (oi), ospfh->type);
2934 break;
2935 }
2936
paul718e3742002-12-13 20:15:29 +00002937 return 0;
2938}
2939
2940/* Make OSPF header. */
paul4dadc292005-05-06 21:37:42 +00002941static void
paul718e3742002-12-13 20:15:29 +00002942ospf_make_header (int type, struct ospf_interface *oi, struct stream *s)
2943{
2944 struct ospf_header *ospfh;
2945
2946 ospfh = (struct ospf_header *) STREAM_DATA (s);
2947
2948 ospfh->version = (u_char) OSPF_VERSION;
2949 ospfh->type = (u_char) type;
2950
paul68980082003-03-25 05:07:42 +00002951 ospfh->router_id = oi->ospf->router_id;
paul718e3742002-12-13 20:15:29 +00002952
2953 ospfh->checksum = 0;
2954 ospfh->area_id = oi->area->area_id;
2955 ospfh->auth_type = htons (ospf_auth_type (oi));
2956
2957 memset (ospfh->u.auth_data, 0, OSPF_AUTH_SIMPLE_SIZE);
2958
paul9985f832005-02-09 15:51:56 +00002959 stream_forward_endp (s, OSPF_HEADER_SIZE);
paul718e3742002-12-13 20:15:29 +00002960}
2961
2962/* Make Authentication Data. */
paul4dadc292005-05-06 21:37:42 +00002963static int
paul718e3742002-12-13 20:15:29 +00002964ospf_make_auth (struct ospf_interface *oi, struct ospf_header *ospfh)
2965{
2966 struct crypt_key *ck;
2967
2968 switch (ospf_auth_type (oi))
2969 {
2970 case OSPF_AUTH_NULL:
2971 /* memset (ospfh->u.auth_data, 0, sizeof (ospfh->u.auth_data)); */
2972 break;
2973 case OSPF_AUTH_SIMPLE:
2974 memcpy (ospfh->u.auth_data, OSPF_IF_PARAM (oi, auth_simple),
2975 OSPF_AUTH_SIMPLE_SIZE);
2976 break;
2977 case OSPF_AUTH_CRYPTOGRAPHIC:
2978 /* If key is not set, then set 0. */
2979 if (list_isempty (OSPF_IF_PARAM (oi, auth_crypt)))
2980 {
2981 ospfh->u.crypt.zero = 0;
2982 ospfh->u.crypt.key_id = 0;
2983 ospfh->u.crypt.auth_data_len = OSPF_AUTH_MD5_SIZE;
2984 }
2985 else
2986 {
paul1eb8ef22005-04-07 07:30:20 +00002987 ck = listgetdata (listtail(OSPF_IF_PARAM (oi, auth_crypt)));
paul718e3742002-12-13 20:15:29 +00002988 ospfh->u.crypt.zero = 0;
2989 ospfh->u.crypt.key_id = ck->key_id;
2990 ospfh->u.crypt.auth_data_len = OSPF_AUTH_MD5_SIZE;
2991 }
2992 /* note: the seq is done in ospf_make_md5_digest() */
2993 break;
2994 default:
2995 /* memset (ospfh->u.auth_data, 0, sizeof (ospfh->u.auth_data)); */
2996 break;
2997 }
2998
2999 return 0;
3000}
3001
3002/* Fill rest of OSPF header. */
paul4dadc292005-05-06 21:37:42 +00003003static void
paul718e3742002-12-13 20:15:29 +00003004ospf_fill_header (struct ospf_interface *oi,
3005 struct stream *s, u_int16_t length)
3006{
3007 struct ospf_header *ospfh;
3008
3009 ospfh = (struct ospf_header *) STREAM_DATA (s);
3010
3011 /* Fill length. */
3012 ospfh->length = htons (length);
3013
3014 /* Calculate checksum. */
3015 if (ntohs (ospfh->auth_type) != OSPF_AUTH_CRYPTOGRAPHIC)
3016 ospfh->checksum = in_cksum (ospfh, length);
3017 else
3018 ospfh->checksum = 0;
3019
3020 /* Add Authentication Data. */
3021 ospf_make_auth (oi, ospfh);
3022}
3023
paul4dadc292005-05-06 21:37:42 +00003024static int
paul718e3742002-12-13 20:15:29 +00003025ospf_make_hello (struct ospf_interface *oi, struct stream *s)
3026{
3027 struct ospf_neighbor *nbr;
3028 struct route_node *rn;
3029 u_int16_t length = OSPF_HELLO_MIN_SIZE;
3030 struct in_addr mask;
3031 unsigned long p;
3032 int flag = 0;
3033
3034 /* Set netmask of interface. */
3035 if (oi->type != OSPF_IFTYPE_POINTOPOINT &&
3036 oi->type != OSPF_IFTYPE_VIRTUALLINK)
3037 masklen2ip (oi->address->prefixlen, &mask);
3038 else
3039 memset ((char *) &mask, 0, sizeof (struct in_addr));
3040 stream_put_ipv4 (s, mask.s_addr);
3041
3042 /* Set Hello Interval. */
paulf9ad9372005-10-21 00:45:17 +00003043 if (OSPF_IF_PARAM (oi, fast_hello) == 0)
3044 stream_putw (s, OSPF_IF_PARAM (oi, v_hello));
3045 else
3046 stream_putw (s, 0); /* hello-interval of 0 for fast-hellos */
paul718e3742002-12-13 20:15:29 +00003047
3048 if (IS_DEBUG_OSPF_EVENT)
ajs2a42e282004-12-08 18:43:03 +00003049 zlog_debug ("make_hello: options: %x, int: %s",
paul718e3742002-12-13 20:15:29 +00003050 OPTIONS(oi), IF_NAME (oi));
3051
3052 /* Set Options. */
3053 stream_putc (s, OPTIONS (oi));
3054
3055 /* Set Router Priority. */
3056 stream_putc (s, PRIORITY (oi));
3057
3058 /* Set Router Dead Interval. */
3059 stream_putl (s, OSPF_IF_PARAM (oi, v_wait));
3060
3061 /* Set Designated Router. */
3062 stream_put_ipv4 (s, DR (oi).s_addr);
3063
paul9985f832005-02-09 15:51:56 +00003064 p = stream_get_endp (s);
paul718e3742002-12-13 20:15:29 +00003065
3066 /* Set Backup Designated Router. */
3067 stream_put_ipv4 (s, BDR (oi).s_addr);
3068
3069 /* Add neighbor seen. */
3070 for (rn = route_top (oi->nbrs); rn; rn = route_next (rn))
paul68980082003-03-25 05:07:42 +00003071 if ((nbr = rn->info))
3072 if (nbr->router_id.s_addr != 0) /* Ignore 0.0.0.0 node. */
3073 if (nbr->state != NSM_Attempt) /* Ignore Down neighbor. */
3074 if (nbr->state != NSM_Down) /* This is myself for DR election. */
3075 if (!IPV4_ADDR_SAME (&nbr->router_id, &oi->ospf->router_id))
paul718e3742002-12-13 20:15:29 +00003076 {
3077 /* Check neighbor is sane? */
paul68980082003-03-25 05:07:42 +00003078 if (nbr->d_router.s_addr != 0
3079 && IPV4_ADDR_SAME (&nbr->d_router, &oi->address->u.prefix4)
3080 && IPV4_ADDR_SAME (&nbr->bd_router, &oi->address->u.prefix4))
3081 flag = 1;
paul718e3742002-12-13 20:15:29 +00003082
3083 stream_put_ipv4 (s, nbr->router_id.s_addr);
3084 length += 4;
3085 }
3086
3087 /* Let neighbor generate BackupSeen. */
3088 if (flag == 1)
paul3a9eb092005-02-08 11:29:41 +00003089 stream_putl_at (s, p, 0); /* ipv4 address, normally */
paul718e3742002-12-13 20:15:29 +00003090
3091 return length;
3092}
3093
paul4dadc292005-05-06 21:37:42 +00003094static int
paul718e3742002-12-13 20:15:29 +00003095ospf_make_db_desc (struct ospf_interface *oi, struct ospf_neighbor *nbr,
3096 struct stream *s)
3097{
3098 struct ospf_lsa *lsa;
3099 u_int16_t length = OSPF_DB_DESC_MIN_SIZE;
3100 u_char options;
3101 unsigned long pp;
3102 int i;
3103 struct ospf_lsdb *lsdb;
3104
3105 /* Set Interface MTU. */
3106 if (oi->type == OSPF_IFTYPE_VIRTUALLINK)
3107 stream_putw (s, 0);
3108 else
3109 stream_putw (s, oi->ifp->mtu);
3110
3111 /* Set Options. */
3112 options = OPTIONS (oi);
3113#ifdef HAVE_OPAQUE_LSA
paul68980082003-03-25 05:07:42 +00003114 if (CHECK_FLAG (oi->ospf->config, OSPF_OPAQUE_CAPABLE))
paul718e3742002-12-13 20:15:29 +00003115 {
3116 if (IS_SET_DD_I (nbr->dd_flags)
3117 || CHECK_FLAG (nbr->options, OSPF_OPTION_O))
3118 /*
3119 * Set O-bit in the outgoing DD packet for capablity negotiation,
3120 * if one of following case is applicable.
3121 *
3122 * 1) WaitTimer expiration event triggered the neighbor state to
3123 * change to Exstart, but no (valid) DD packet has received
3124 * from the neighbor yet.
3125 *
3126 * 2) At least one DD packet with O-bit on has received from the
3127 * neighbor.
3128 */
3129 SET_FLAG (options, OSPF_OPTION_O);
3130 }
3131#endif /* HAVE_OPAQUE_LSA */
3132 stream_putc (s, options);
3133
Paul Jakma8dd24ee2006-08-27 06:29:30 +00003134 /* DD flags */
paul9985f832005-02-09 15:51:56 +00003135 pp = stream_get_endp (s);
paul718e3742002-12-13 20:15:29 +00003136 stream_putc (s, nbr->dd_flags);
3137
3138 /* Set DD Sequence Number. */
3139 stream_putl (s, nbr->dd_seqnum);
3140
Paul Jakmab5aeb442006-08-30 18:47:37 +00003141 /* shortcut unneeded walk of (empty) summary LSDBs */
paul718e3742002-12-13 20:15:29 +00003142 if (ospf_db_summary_isempty (nbr))
Paul Jakmab5aeb442006-08-30 18:47:37 +00003143 goto empty;
paul718e3742002-12-13 20:15:29 +00003144
3145 /* Describe LSA Header from Database Summary List. */
3146 lsdb = &nbr->db_sum;
3147
3148 for (i = OSPF_MIN_LSA; i < OSPF_MAX_LSA; i++)
3149 {
3150 struct route_table *table = lsdb->type[i].db;
3151 struct route_node *rn;
3152
3153 for (rn = route_top (table); rn; rn = route_next (rn))
3154 if ((lsa = rn->info) != NULL)
3155 {
3156#ifdef HAVE_OPAQUE_LSA
3157 if (IS_OPAQUE_LSA (lsa->data->type)
3158 && (! CHECK_FLAG (options, OSPF_OPTION_O)))
3159 {
3160 /* Suppress advertising opaque-informations. */
3161 /* Remove LSA from DB summary list. */
3162 ospf_lsdb_delete (lsdb, lsa);
3163 continue;
3164 }
3165#endif /* HAVE_OPAQUE_LSA */
3166
3167 if (!CHECK_FLAG (lsa->flags, OSPF_LSA_DISCARD))
3168 {
3169 struct lsa_header *lsah;
3170 u_int16_t ls_age;
3171
3172 /* DD packet overflows interface MTU. */
gdt86f1fd92005-01-10 14:20:43 +00003173 if (length + OSPF_LSA_HEADER_SIZE > ospf_packet_max (oi))
paul718e3742002-12-13 20:15:29 +00003174 break;
3175
3176 /* Keep pointer to LS age. */
3177 lsah = (struct lsa_header *) (STREAM_DATA (s) +
paul9985f832005-02-09 15:51:56 +00003178 stream_get_endp (s));
paul718e3742002-12-13 20:15:29 +00003179
3180 /* Proceed stream pointer. */
3181 stream_put (s, lsa->data, OSPF_LSA_HEADER_SIZE);
3182 length += OSPF_LSA_HEADER_SIZE;
3183
3184 /* Set LS age. */
3185 ls_age = LS_AGE (lsa);
3186 lsah->ls_age = htons (ls_age);
3187
3188 }
3189
3190 /* Remove LSA from DB summary list. */
3191 ospf_lsdb_delete (lsdb, lsa);
3192 }
3193 }
3194
Paul Jakma8dd24ee2006-08-27 06:29:30 +00003195 /* Update 'More' bit */
3196 if (ospf_db_summary_isempty (nbr))
3197 {
Paul Jakmab5aeb442006-08-30 18:47:37 +00003198empty:
3199 if (nbr->state >= NSM_Exchange)
3200 {
3201 UNSET_FLAG (nbr->dd_flags, OSPF_DD_FLAG_M);
3202 /* Rewrite DD flags */
3203 stream_putc_at (s, pp, nbr->dd_flags);
3204 }
3205 else
3206 {
3207 assert (IS_SET_DD_M(nbr->dd_flags));
3208 }
Paul Jakma8dd24ee2006-08-27 06:29:30 +00003209 }
paul718e3742002-12-13 20:15:29 +00003210 return length;
3211}
3212
paul4dadc292005-05-06 21:37:42 +00003213static int
paul718e3742002-12-13 20:15:29 +00003214ospf_make_ls_req_func (struct stream *s, u_int16_t *length,
3215 unsigned long delta, struct ospf_neighbor *nbr,
3216 struct ospf_lsa *lsa)
3217{
3218 struct ospf_interface *oi;
3219
3220 oi = nbr->oi;
3221
3222 /* LS Request packet overflows interface MTU. */
gdt86f1fd92005-01-10 14:20:43 +00003223 if (*length + delta > ospf_packet_max(oi))
paul718e3742002-12-13 20:15:29 +00003224 return 0;
3225
3226 stream_putl (s, lsa->data->type);
3227 stream_put_ipv4 (s, lsa->data->id.s_addr);
3228 stream_put_ipv4 (s, lsa->data->adv_router.s_addr);
3229
Paul Jakma1fe6ed32006-07-26 09:37:26 +00003230 ospf_lsa_unlock (&nbr->ls_req_last);
paul718e3742002-12-13 20:15:29 +00003231 nbr->ls_req_last = ospf_lsa_lock (lsa);
3232
3233 *length += 12;
3234 return 1;
3235}
3236
paul4dadc292005-05-06 21:37:42 +00003237static int
paul718e3742002-12-13 20:15:29 +00003238ospf_make_ls_req (struct ospf_neighbor *nbr, struct stream *s)
3239{
3240 struct ospf_lsa *lsa;
3241 u_int16_t length = OSPF_LS_REQ_MIN_SIZE;
paul9985f832005-02-09 15:51:56 +00003242 unsigned long delta = stream_get_endp(s)+12;
paul718e3742002-12-13 20:15:29 +00003243 struct route_table *table;
3244 struct route_node *rn;
3245 int i;
3246 struct ospf_lsdb *lsdb;
3247
3248 lsdb = &nbr->ls_req;
3249
3250 for (i = OSPF_MIN_LSA; i < OSPF_MAX_LSA; i++)
3251 {
3252 table = lsdb->type[i].db;
3253 for (rn = route_top (table); rn; rn = route_next (rn))
3254 if ((lsa = (rn->info)) != NULL)
3255 if (ospf_make_ls_req_func (s, &length, delta, nbr, lsa) == 0)
3256 {
3257 route_unlock_node (rn);
3258 break;
3259 }
3260 }
3261 return length;
3262}
3263
paul4dadc292005-05-06 21:37:42 +00003264static int
paul718e3742002-12-13 20:15:29 +00003265ls_age_increment (struct ospf_lsa *lsa, int delay)
3266{
3267 int age;
3268
3269 age = IS_LSA_MAXAGE (lsa) ? OSPF_LSA_MAXAGE : LS_AGE (lsa) + delay;
3270
3271 return (age > OSPF_LSA_MAXAGE ? OSPF_LSA_MAXAGE : age);
3272}
3273
paul4dadc292005-05-06 21:37:42 +00003274static int
hasso52dc7ee2004-09-23 19:18:23 +00003275ospf_make_ls_upd (struct ospf_interface *oi, struct list *update, struct stream *s)
paul718e3742002-12-13 20:15:29 +00003276{
3277 struct ospf_lsa *lsa;
hasso52dc7ee2004-09-23 19:18:23 +00003278 struct listnode *node;
Dmitry Tejblumc9035cc2009-06-24 20:14:30 +04003279 u_int16_t length = 0;
gdt86f1fd92005-01-10 14:20:43 +00003280 unsigned int size_noauth;
paul9985f832005-02-09 15:51:56 +00003281 unsigned long delta = stream_get_endp (s);
paul718e3742002-12-13 20:15:29 +00003282 unsigned long pp;
3283 int count = 0;
3284
3285 if (IS_DEBUG_OSPF_EVENT)
ajs2a42e282004-12-08 18:43:03 +00003286 zlog_debug ("ospf_make_ls_upd: Start");
paul59ea14c2004-07-14 20:50:36 +00003287
paul9985f832005-02-09 15:51:56 +00003288 pp = stream_get_endp (s);
3289 stream_forward_endp (s, OSPF_LS_UPD_MIN_SIZE);
Dmitry Tejblumc9035cc2009-06-24 20:14:30 +04003290 length += OSPF_LS_UPD_MIN_SIZE;
paul718e3742002-12-13 20:15:29 +00003291
gdt86f1fd92005-01-10 14:20:43 +00003292 /* Calculate amount of packet usable for data. */
3293 size_noauth = stream_get_size(s) - ospf_packet_authspace(oi);
3294
paul718e3742002-12-13 20:15:29 +00003295 while ((node = listhead (update)) != NULL)
3296 {
3297 struct lsa_header *lsah;
3298 u_int16_t ls_age;
3299
3300 if (IS_DEBUG_OSPF_EVENT)
ajs2a42e282004-12-08 18:43:03 +00003301 zlog_debug ("ospf_make_ls_upd: List Iteration");
paul718e3742002-12-13 20:15:29 +00003302
paul1eb8ef22005-04-07 07:30:20 +00003303 lsa = listgetdata (node);
3304
paul718e3742002-12-13 20:15:29 +00003305 assert (lsa->data);
3306
paul68b73392004-09-12 14:21:37 +00003307 /* Will it fit? */
gdt86f1fd92005-01-10 14:20:43 +00003308 if (length + delta + ntohs (lsa->data->length) > size_noauth)
paul59ea14c2004-07-14 20:50:36 +00003309 break;
3310
paul718e3742002-12-13 20:15:29 +00003311 /* Keep pointer to LS age. */
paul9985f832005-02-09 15:51:56 +00003312 lsah = (struct lsa_header *) (STREAM_DATA (s) + stream_get_endp (s));
paul718e3742002-12-13 20:15:29 +00003313
3314 /* Put LSA to Link State Request. */
3315 stream_put (s, lsa->data, ntohs (lsa->data->length));
3316
3317 /* Set LS age. */
3318 /* each hop must increment an lsa_age by transmit_delay
3319 of OSPF interface */
3320 ls_age = ls_age_increment (lsa, OSPF_IF_PARAM (oi, transmit_delay));
3321 lsah->ls_age = htons (ls_age);
3322
3323 length += ntohs (lsa->data->length);
3324 count++;
3325
3326 list_delete_node (update, node);
Paul Jakma1fe6ed32006-07-26 09:37:26 +00003327 ospf_lsa_unlock (&lsa); /* oi->ls_upd_queue */
paul718e3742002-12-13 20:15:29 +00003328 }
3329
3330 /* Now set #LSAs. */
paul3a9eb092005-02-08 11:29:41 +00003331 stream_putl_at (s, pp, count);
paul718e3742002-12-13 20:15:29 +00003332
3333 if (IS_DEBUG_OSPF_EVENT)
ajs2a42e282004-12-08 18:43:03 +00003334 zlog_debug ("ospf_make_ls_upd: Stop");
paul718e3742002-12-13 20:15:29 +00003335 return length;
3336}
3337
paul4dadc292005-05-06 21:37:42 +00003338static int
hasso52dc7ee2004-09-23 19:18:23 +00003339ospf_make_ls_ack (struct ospf_interface *oi, struct list *ack, struct stream *s)
paul718e3742002-12-13 20:15:29 +00003340{
Paul Jakma1fe6ed32006-07-26 09:37:26 +00003341 struct listnode *node, *nnode;
paul718e3742002-12-13 20:15:29 +00003342 u_int16_t length = OSPF_LS_ACK_MIN_SIZE;
paul9985f832005-02-09 15:51:56 +00003343 unsigned long delta = stream_get_endp(s) + 24;
paul718e3742002-12-13 20:15:29 +00003344 struct ospf_lsa *lsa;
3345
Paul Jakma1fe6ed32006-07-26 09:37:26 +00003346 for (ALL_LIST_ELEMENTS (ack, node, nnode, lsa))
paul718e3742002-12-13 20:15:29 +00003347 {
paul718e3742002-12-13 20:15:29 +00003348 assert (lsa);
3349
gdt86f1fd92005-01-10 14:20:43 +00003350 if (length + delta > ospf_packet_max (oi))
paul718e3742002-12-13 20:15:29 +00003351 break;
3352
3353 stream_put (s, lsa->data, OSPF_LSA_HEADER_SIZE);
3354 length += OSPF_LSA_HEADER_SIZE;
3355
paul718e3742002-12-13 20:15:29 +00003356 listnode_delete (ack, lsa);
Paul Jakma1fe6ed32006-07-26 09:37:26 +00003357 ospf_lsa_unlock (&lsa); /* oi->ls_ack_direct.ls_ack */
paul718e3742002-12-13 20:15:29 +00003358 }
3359
paul718e3742002-12-13 20:15:29 +00003360 return length;
3361}
3362
Paul Jakmaaa276fd2010-01-08 17:11:15 +00003363static void
3364ospf_hello_send_sub (struct ospf_interface *oi, in_addr_t addr)
paul718e3742002-12-13 20:15:29 +00003365{
3366 struct ospf_packet *op;
3367 u_int16_t length = OSPF_HEADER_SIZE;
3368
3369 op = ospf_packet_new (oi->ifp->mtu);
3370
3371 /* Prepare OSPF common header. */
3372 ospf_make_header (OSPF_MSG_HELLO, oi, op->s);
3373
3374 /* Prepare OSPF Hello body. */
3375 length += ospf_make_hello (oi, op->s);
3376
3377 /* Fill OSPF header. */
3378 ospf_fill_header (oi, op->s, length);
3379
3380 /* Set packet length. */
3381 op->length = length;
3382
Paul Jakmaaa276fd2010-01-08 17:11:15 +00003383 op->dst.s_addr = addr;
paul718e3742002-12-13 20:15:29 +00003384
Paul Jakmaaa276fd2010-01-08 17:11:15 +00003385 /* Add packet to the top of the interface output queue, so that they
3386 * can't get delayed by things like long queues of LS Update packets
3387 */
3388 ospf_packet_add_top (oi, op);
paul718e3742002-12-13 20:15:29 +00003389
3390 /* Hook thread to write packet. */
paul020709f2003-04-04 02:44:16 +00003391 OSPF_ISM_WRITE_ON (oi->ospf);
paul718e3742002-12-13 20:15:29 +00003392}
3393
paul4dadc292005-05-06 21:37:42 +00003394static void
paul718e3742002-12-13 20:15:29 +00003395ospf_poll_send (struct ospf_nbr_nbma *nbr_nbma)
3396{
3397 struct ospf_interface *oi;
3398
3399 oi = nbr_nbma->oi;
3400 assert(oi);
3401
3402 /* If this is passive interface, do not send OSPF Hello. */
Paul Jakma7ffa8fa2006-10-22 20:07:53 +00003403 if (OSPF_IF_PASSIVE_STATUS (oi) == OSPF_IF_PASSIVE)
paul718e3742002-12-13 20:15:29 +00003404 return;
3405
3406 if (oi->type != OSPF_IFTYPE_NBMA)
3407 return;
3408
3409 if (nbr_nbma->nbr != NULL && nbr_nbma->nbr->state != NSM_Down)
3410 return;
3411
3412 if (PRIORITY(oi) == 0)
3413 return;
3414
3415 if (nbr_nbma->priority == 0
3416 && oi->state != ISM_DR && oi->state != ISM_Backup)
3417 return;
3418
Paul Jakmaaa276fd2010-01-08 17:11:15 +00003419 ospf_hello_send_sub (oi, nbr_nbma->addr.s_addr);
paul718e3742002-12-13 20:15:29 +00003420}
3421
3422int
3423ospf_poll_timer (struct thread *thread)
3424{
3425 struct ospf_nbr_nbma *nbr_nbma;
3426
3427 nbr_nbma = THREAD_ARG (thread);
3428 nbr_nbma->t_poll = NULL;
3429
3430 if (IS_DEBUG_OSPF (nsm, NSM_TIMERS))
ajs2a42e282004-12-08 18:43:03 +00003431 zlog (NULL, LOG_DEBUG, "NSM[%s:%s]: Timer (Poll timer expire)",
paul718e3742002-12-13 20:15:29 +00003432 IF_NAME (nbr_nbma->oi), inet_ntoa (nbr_nbma->addr));
3433
3434 ospf_poll_send (nbr_nbma);
3435
3436 if (nbr_nbma->v_poll > 0)
3437 OSPF_POLL_TIMER_ON (nbr_nbma->t_poll, ospf_poll_timer,
3438 nbr_nbma->v_poll);
3439
3440 return 0;
3441}
3442
3443
3444int
3445ospf_hello_reply_timer (struct thread *thread)
3446{
3447 struct ospf_neighbor *nbr;
3448
3449 nbr = THREAD_ARG (thread);
3450 nbr->t_hello_reply = NULL;
3451
3452 assert (nbr->oi);
3453
3454 if (IS_DEBUG_OSPF (nsm, NSM_TIMERS))
ajs2a42e282004-12-08 18:43:03 +00003455 zlog (NULL, LOG_DEBUG, "NSM[%s:%s]: Timer (hello-reply timer expire)",
paul718e3742002-12-13 20:15:29 +00003456 IF_NAME (nbr->oi), inet_ntoa (nbr->router_id));
3457
Paul Jakmaaa276fd2010-01-08 17:11:15 +00003458 ospf_hello_send_sub (nbr->oi, nbr->address.u.prefix4.s_addr);
paul718e3742002-12-13 20:15:29 +00003459
3460 return 0;
3461}
3462
3463/* Send OSPF Hello. */
3464void
3465ospf_hello_send (struct ospf_interface *oi)
3466{
paul718e3742002-12-13 20:15:29 +00003467 /* If this is passive interface, do not send OSPF Hello. */
Paul Jakma7ffa8fa2006-10-22 20:07:53 +00003468 if (OSPF_IF_PASSIVE_STATUS (oi) == OSPF_IF_PASSIVE)
paul718e3742002-12-13 20:15:29 +00003469 return;
3470
paul718e3742002-12-13 20:15:29 +00003471 if (oi->type == OSPF_IFTYPE_NBMA)
3472 {
3473 struct ospf_neighbor *nbr;
3474 struct route_node *rn;
3475
3476 for (rn = route_top (oi->nbrs); rn; rn = route_next (rn))
3477 if ((nbr = rn->info))
3478 if (nbr != oi->nbr_self)
3479 if (nbr->state != NSM_Down)
3480 {
3481 /* RFC 2328 Section 9.5.1
3482 If the router is not eligible to become Designated Router,
3483 it must periodically send Hello Packets to both the
3484 Designated Router and the Backup Designated Router (if they
3485 exist). */
3486 if (PRIORITY(oi) == 0 &&
3487 IPV4_ADDR_CMP(&DR(oi), &nbr->address.u.prefix4) &&
3488 IPV4_ADDR_CMP(&BDR(oi), &nbr->address.u.prefix4))
3489 continue;
3490
3491 /* If the router is eligible to become Designated Router, it
3492 must periodically send Hello Packets to all neighbors that
3493 are also eligible. In addition, if the router is itself the
3494 Designated Router or Backup Designated Router, it must also
3495 send periodic Hello Packets to all other neighbors. */
3496
3497 if (nbr->priority == 0 && oi->state == ISM_DROther)
3498 continue;
3499 /* if oi->state == Waiting, send hello to all neighbors */
Paul Jakmaaa276fd2010-01-08 17:11:15 +00003500 ospf_hello_send_sub (oi, nbr->address.u.prefix4.s_addr);
paul718e3742002-12-13 20:15:29 +00003501 }
paul718e3742002-12-13 20:15:29 +00003502 }
3503 else
3504 {
3505 /* Decide destination address. */
3506 if (oi->type == OSPF_IFTYPE_VIRTUALLINK)
Paul Jakmaaa276fd2010-01-08 17:11:15 +00003507 ospf_hello_send_sub (oi, oi->vl_data->peer_addr.s_addr);
3508 else
3509 ospf_hello_send_sub (oi, htonl (OSPF_ALLSPFROUTERS));
paul718e3742002-12-13 20:15:29 +00003510 }
3511}
3512
3513/* Send OSPF Database Description. */
3514void
3515ospf_db_desc_send (struct ospf_neighbor *nbr)
3516{
3517 struct ospf_interface *oi;
3518 struct ospf_packet *op;
3519 u_int16_t length = OSPF_HEADER_SIZE;
3520
3521 oi = nbr->oi;
3522 op = ospf_packet_new (oi->ifp->mtu);
3523
3524 /* Prepare OSPF common header. */
3525 ospf_make_header (OSPF_MSG_DB_DESC, oi, op->s);
3526
3527 /* Prepare OSPF Database Description body. */
3528 length += ospf_make_db_desc (oi, nbr, op->s);
3529
3530 /* Fill OSPF header. */
3531 ospf_fill_header (oi, op->s, length);
3532
3533 /* Set packet length. */
3534 op->length = length;
3535
3536 /* Decide destination address. */
Joakim Tjernlund53d0dec2008-05-30 16:04:39 +02003537 if (oi->type == OSPF_IFTYPE_POINTOPOINT)
3538 op->dst.s_addr = htonl (OSPF_ALLSPFROUTERS);
3539 else
3540 op->dst = nbr->address.u.prefix4;
paul718e3742002-12-13 20:15:29 +00003541
3542 /* Add packet to the interface output queue. */
3543 ospf_packet_add (oi, op);
3544
3545 /* Hook thread to write packet. */
paul020709f2003-04-04 02:44:16 +00003546 OSPF_ISM_WRITE_ON (oi->ospf);
paul718e3742002-12-13 20:15:29 +00003547
3548 /* Remove old DD packet, then copy new one and keep in neighbor structure. */
3549 if (nbr->last_send)
3550 ospf_packet_free (nbr->last_send);
3551 nbr->last_send = ospf_packet_dup (op);
Paul Jakma2518efd2006-08-27 06:49:29 +00003552 quagga_gettime (QUAGGA_CLK_MONOTONIC, &nbr->last_send_ts);
paul718e3742002-12-13 20:15:29 +00003553}
3554
3555/* Re-send Database Description. */
3556void
3557ospf_db_desc_resend (struct ospf_neighbor *nbr)
3558{
3559 struct ospf_interface *oi;
3560
3561 oi = nbr->oi;
3562
3563 /* Add packet to the interface output queue. */
3564 ospf_packet_add (oi, ospf_packet_dup (nbr->last_send));
3565
3566 /* Hook thread to write packet. */
paul020709f2003-04-04 02:44:16 +00003567 OSPF_ISM_WRITE_ON (oi->ospf);
paul718e3742002-12-13 20:15:29 +00003568}
3569
3570/* Send Link State Request. */
3571void
3572ospf_ls_req_send (struct ospf_neighbor *nbr)
3573{
3574 struct ospf_interface *oi;
3575 struct ospf_packet *op;
3576 u_int16_t length = OSPF_HEADER_SIZE;
3577
3578 oi = nbr->oi;
3579 op = ospf_packet_new (oi->ifp->mtu);
3580
3581 /* Prepare OSPF common header. */
3582 ospf_make_header (OSPF_MSG_LS_REQ, oi, op->s);
3583
3584 /* Prepare OSPF Link State Request body. */
3585 length += ospf_make_ls_req (nbr, op->s);
3586 if (length == OSPF_HEADER_SIZE)
3587 {
3588 ospf_packet_free (op);
3589 return;
3590 }
3591
3592 /* Fill OSPF header. */
3593 ospf_fill_header (oi, op->s, length);
3594
3595 /* Set packet length. */
3596 op->length = length;
3597
3598 /* Decide destination address. */
Joakim Tjernlund53d0dec2008-05-30 16:04:39 +02003599 if (oi->type == OSPF_IFTYPE_POINTOPOINT)
3600 op->dst.s_addr = htonl (OSPF_ALLSPFROUTERS);
3601 else
3602 op->dst = nbr->address.u.prefix4;
paul718e3742002-12-13 20:15:29 +00003603
3604 /* Add packet to the interface output queue. */
3605 ospf_packet_add (oi, op);
3606
3607 /* Hook thread to write packet. */
paul020709f2003-04-04 02:44:16 +00003608 OSPF_ISM_WRITE_ON (oi->ospf);
paul718e3742002-12-13 20:15:29 +00003609
3610 /* Add Link State Request Retransmission Timer. */
3611 OSPF_NSM_TIMER_ON (nbr->t_ls_req, ospf_ls_req_timer, nbr->v_ls_req);
3612}
3613
3614/* Send Link State Update with an LSA. */
3615void
3616ospf_ls_upd_send_lsa (struct ospf_neighbor *nbr, struct ospf_lsa *lsa,
3617 int flag)
3618{
hasso52dc7ee2004-09-23 19:18:23 +00003619 struct list *update;
paul718e3742002-12-13 20:15:29 +00003620
3621 update = list_new ();
3622
3623 listnode_add (update, lsa);
3624 ospf_ls_upd_send (nbr, update, flag);
3625
3626 list_delete (update);
3627}
3628
paul68b73392004-09-12 14:21:37 +00003629/* Determine size for packet. Must be at least big enough to accomodate next
3630 * LSA on list, which may be bigger than MTU size.
3631 *
3632 * Return pointer to new ospf_packet
3633 * NULL if we can not allocate, eg because LSA is bigger than imposed limit
3634 * on packet sizes (in which case offending LSA is deleted from update list)
3635 */
3636static struct ospf_packet *
3637ospf_ls_upd_packet_new (struct list *update, struct ospf_interface *oi)
3638{
3639 struct ospf_lsa *lsa;
3640 struct listnode *ln;
3641 size_t size;
3642 static char warned = 0;
3643
paul1eb8ef22005-04-07 07:30:20 +00003644 lsa = listgetdata((ln = listhead (update)));
paul68b73392004-09-12 14:21:37 +00003645 assert (lsa->data);
3646
3647 if ((OSPF_LS_UPD_MIN_SIZE + ntohs (lsa->data->length))
3648 > ospf_packet_max (oi))
3649 {
3650 if (!warned)
3651 {
3652 zlog_warn ("ospf_ls_upd_packet_new: oversized LSA encountered!"
3653 "will need to fragment. Not optimal. Try divide up"
3654 " your network with areas. Use 'debug ospf packet send'"
3655 " to see details, or look at 'show ip ospf database ..'");
3656 warned = 1;
3657 }
3658
3659 if (IS_DEBUG_OSPF_PACKET (0, SEND))
ajs2a42e282004-12-08 18:43:03 +00003660 zlog_debug ("ospf_ls_upd_packet_new: oversized LSA id:%s,"
paul68b73392004-09-12 14:21:37 +00003661 " %d bytes originated by %s, will be fragmented!",
3662 inet_ntoa (lsa->data->id),
3663 ntohs (lsa->data->length),
3664 inet_ntoa (lsa->data->adv_router));
3665
3666 /*
3667 * Allocate just enough to fit this LSA only, to avoid including other
3668 * LSAs in fragmented LSA Updates.
3669 */
3670 size = ntohs (lsa->data->length) + (oi->ifp->mtu - ospf_packet_max (oi))
3671 + OSPF_LS_UPD_MIN_SIZE;
3672 }
3673 else
3674 size = oi->ifp->mtu;
3675
3676 if (size > OSPF_MAX_PACKET_SIZE)
3677 {
3678 zlog_warn ("ospf_ls_upd_packet_new: oversized LSA id:%s too big,"
paul64511f32004-10-31 18:01:13 +00003679 " %d bytes, packet size %ld, dropping it completely."
paul68b73392004-09-12 14:21:37 +00003680 " OSPF routing is broken!",
paul37ccfa32004-10-31 11:24:51 +00003681 inet_ntoa (lsa->data->id), ntohs (lsa->data->length),
paul62d8e962004-11-02 20:26:45 +00003682 (long int) size);
paul68b73392004-09-12 14:21:37 +00003683 list_delete_node (update, ln);
3684 return NULL;
3685 }
3686
Dmitry Tejblumc9035cc2009-06-24 20:14:30 +04003687 /* IP header is built up separately by ospf_write(). This means, that we must
3688 * reduce the "affordable" size just calculated by length of an IP header.
3689 * This makes sure, that even if we manage to fill the payload with LSA data
3690 * completely, the final packet (our data plus IP header) still fits into
3691 * outgoing interface MTU. This correction isn't really meaningful for an
3692 * oversized LSA, but for consistency the correction is done for both cases.
3693 *
3694 * P.S. OSPF_MAX_PACKET_SIZE above already includes IP header size
3695 */
3696 return ospf_packet_new (size - sizeof (struct ip));
paul68b73392004-09-12 14:21:37 +00003697}
3698
paul718e3742002-12-13 20:15:29 +00003699static void
hasso52dc7ee2004-09-23 19:18:23 +00003700ospf_ls_upd_queue_send (struct ospf_interface *oi, struct list *update,
paul718e3742002-12-13 20:15:29 +00003701 struct in_addr addr)
3702{
3703 struct ospf_packet *op;
3704 u_int16_t length = OSPF_HEADER_SIZE;
3705
3706 if (IS_DEBUG_OSPF_EVENT)
ajs2a42e282004-12-08 18:43:03 +00003707 zlog_debug ("listcount = %d, dst %s", listcount (update), inet_ntoa(addr));
paul68b73392004-09-12 14:21:37 +00003708
3709 op = ospf_ls_upd_packet_new (update, oi);
paul718e3742002-12-13 20:15:29 +00003710
3711 /* Prepare OSPF common header. */
3712 ospf_make_header (OSPF_MSG_LS_UPD, oi, op->s);
3713
paul59ea14c2004-07-14 20:50:36 +00003714 /* Prepare OSPF Link State Update body.
3715 * Includes Type-7 translation.
3716 */
paul718e3742002-12-13 20:15:29 +00003717 length += ospf_make_ls_upd (oi, update, op->s);
3718
3719 /* Fill OSPF header. */
3720 ospf_fill_header (oi, op->s, length);
3721
3722 /* Set packet length. */
3723 op->length = length;
3724
3725 /* Decide destination address. */
Joakim Tjernlund53d0dec2008-05-30 16:04:39 +02003726 if (oi->type == OSPF_IFTYPE_POINTOPOINT)
3727 op->dst.s_addr = htonl (OSPF_ALLSPFROUTERS);
3728 else
3729 op->dst.s_addr = addr.s_addr;
paul718e3742002-12-13 20:15:29 +00003730
3731 /* Add packet to the interface output queue. */
3732 ospf_packet_add (oi, op);
3733
3734 /* Hook thread to write packet. */
paul020709f2003-04-04 02:44:16 +00003735 OSPF_ISM_WRITE_ON (oi->ospf);
paul718e3742002-12-13 20:15:29 +00003736}
3737
3738static int
3739ospf_ls_upd_send_queue_event (struct thread *thread)
3740{
3741 struct ospf_interface *oi = THREAD_ARG(thread);
3742 struct route_node *rn;
paul736d3442003-07-24 23:22:57 +00003743 struct route_node *rnext;
paul59ea14c2004-07-14 20:50:36 +00003744 struct list *update;
paul68b73392004-09-12 14:21:37 +00003745 char again = 0;
paul718e3742002-12-13 20:15:29 +00003746
3747 oi->t_ls_upd_event = NULL;
3748
3749 if (IS_DEBUG_OSPF_EVENT)
ajs2a42e282004-12-08 18:43:03 +00003750 zlog_debug ("ospf_ls_upd_send_queue start");
paul718e3742002-12-13 20:15:29 +00003751
paul736d3442003-07-24 23:22:57 +00003752 for (rn = route_top (oi->ls_upd_queue); rn; rn = rnext)
paul718e3742002-12-13 20:15:29 +00003753 {
paul736d3442003-07-24 23:22:57 +00003754 rnext = route_next (rn);
3755
paul718e3742002-12-13 20:15:29 +00003756 if (rn->info == NULL)
paul736d3442003-07-24 23:22:57 +00003757 continue;
paul68b73392004-09-12 14:21:37 +00003758
3759 update = (struct list *)rn->info;
paul718e3742002-12-13 20:15:29 +00003760
paul48fe13b2004-07-27 17:40:44 +00003761 ospf_ls_upd_queue_send (oi, update, rn->p.u.prefix4);
paul718e3742002-12-13 20:15:29 +00003762
paul68b73392004-09-12 14:21:37 +00003763 /* list might not be empty. */
paul59ea14c2004-07-14 20:50:36 +00003764 if (listcount(update) == 0)
3765 {
3766 list_delete (rn->info);
3767 rn->info = NULL;
3768 route_unlock_node (rn);
3769 }
3770 else
paul68b73392004-09-12 14:21:37 +00003771 again = 1;
paul59ea14c2004-07-14 20:50:36 +00003772 }
3773
3774 if (again != 0)
3775 {
3776 if (IS_DEBUG_OSPF_EVENT)
ajs2a42e282004-12-08 18:43:03 +00003777 zlog_debug ("ospf_ls_upd_send_queue: update lists not cleared,"
paul59ea14c2004-07-14 20:50:36 +00003778 " %d nodes to try again, raising new event", again);
3779 oi->t_ls_upd_event =
3780 thread_add_event (master, ospf_ls_upd_send_queue_event, oi, 0);
paul718e3742002-12-13 20:15:29 +00003781 }
3782
3783 if (IS_DEBUG_OSPF_EVENT)
ajs2a42e282004-12-08 18:43:03 +00003784 zlog_debug ("ospf_ls_upd_send_queue stop");
paul59ea14c2004-07-14 20:50:36 +00003785
paul718e3742002-12-13 20:15:29 +00003786 return 0;
3787}
3788
3789void
hasso52dc7ee2004-09-23 19:18:23 +00003790ospf_ls_upd_send (struct ospf_neighbor *nbr, struct list *update, int flag)
paul718e3742002-12-13 20:15:29 +00003791{
3792 struct ospf_interface *oi;
paul1eb8ef22005-04-07 07:30:20 +00003793 struct ospf_lsa *lsa;
paul718e3742002-12-13 20:15:29 +00003794 struct prefix_ipv4 p;
3795 struct route_node *rn;
paul1eb8ef22005-04-07 07:30:20 +00003796 struct listnode *node;
paul718e3742002-12-13 20:15:29 +00003797
3798 oi = nbr->oi;
3799
3800 p.family = AF_INET;
3801 p.prefixlen = IPV4_MAX_BITLEN;
3802
3803 /* Decide destination address. */
3804 if (oi->type == OSPF_IFTYPE_VIRTUALLINK)
3805 p.prefix = oi->vl_data->peer_addr;
Joakim Tjernlund53d0dec2008-05-30 16:04:39 +02003806 else if (oi->type == OSPF_IFTYPE_POINTOPOINT)
3807 p.prefix.s_addr = htonl (OSPF_ALLSPFROUTERS);
paul718e3742002-12-13 20:15:29 +00003808 else if (flag == OSPF_SEND_PACKET_DIRECT)
3809 p.prefix = nbr->address.u.prefix4;
3810 else if (oi->state == ISM_DR || oi->state == ISM_Backup)
3811 p.prefix.s_addr = htonl (OSPF_ALLSPFROUTERS);
paul7afa08d2002-12-13 20:59:45 +00003812 else if (oi->type == OSPF_IFTYPE_POINTOMULTIPOINT)
3813 p.prefix.s_addr = htonl (OSPF_ALLSPFROUTERS);
paul718e3742002-12-13 20:15:29 +00003814 else
3815 p.prefix.s_addr = htonl (OSPF_ALLDROUTERS);
3816
3817 if (oi->type == OSPF_IFTYPE_NBMA)
3818 {
3819 if (flag == OSPF_SEND_PACKET_INDIRECT)
3820 zlog_warn ("* LS-Update is directly sent on NBMA network.");
3821 if (IPV4_ADDR_SAME(&oi->address->u.prefix4, &p.prefix.s_addr))
3822 zlog_warn ("* LS-Update is sent to myself.");
3823 }
3824
3825 rn = route_node_get (oi->ls_upd_queue, (struct prefix *) &p);
3826
3827 if (rn->info == NULL)
3828 rn->info = list_new ();
3829
paul1eb8ef22005-04-07 07:30:20 +00003830 for (ALL_LIST_ELEMENTS_RO (update, node, lsa))
Paul Jakma1fe6ed32006-07-26 09:37:26 +00003831 listnode_add (rn->info, ospf_lsa_lock (lsa)); /* oi->ls_upd_queue */
paul718e3742002-12-13 20:15:29 +00003832
3833 if (oi->t_ls_upd_event == NULL)
3834 oi->t_ls_upd_event =
3835 thread_add_event (master, ospf_ls_upd_send_queue_event, oi, 0);
3836}
3837
3838static void
hasso52dc7ee2004-09-23 19:18:23 +00003839ospf_ls_ack_send_list (struct ospf_interface *oi, struct list *ack,
3840 struct in_addr dst)
paul718e3742002-12-13 20:15:29 +00003841{
3842 struct ospf_packet *op;
3843 u_int16_t length = OSPF_HEADER_SIZE;
3844
3845 op = ospf_packet_new (oi->ifp->mtu);
3846
3847 /* Prepare OSPF common header. */
3848 ospf_make_header (OSPF_MSG_LS_ACK, oi, op->s);
3849
3850 /* Prepare OSPF Link State Acknowledgment body. */
3851 length += ospf_make_ls_ack (oi, ack, op->s);
3852
3853 /* Fill OSPF header. */
3854 ospf_fill_header (oi, op->s, length);
3855
3856 /* Set packet length. */
3857 op->length = length;
3858
3859 /* Set destination IP address. */
3860 op->dst = dst;
3861
3862 /* Add packet to the interface output queue. */
3863 ospf_packet_add (oi, op);
3864
3865 /* Hook thread to write packet. */
paul020709f2003-04-04 02:44:16 +00003866 OSPF_ISM_WRITE_ON (oi->ospf);
paul718e3742002-12-13 20:15:29 +00003867}
3868
3869static int
3870ospf_ls_ack_send_event (struct thread *thread)
3871{
3872 struct ospf_interface *oi = THREAD_ARG (thread);
3873
3874 oi->t_ls_ack_direct = NULL;
3875
3876 while (listcount (oi->ls_ack_direct.ls_ack))
3877 ospf_ls_ack_send_list (oi, oi->ls_ack_direct.ls_ack,
3878 oi->ls_ack_direct.dst);
3879
3880 return 0;
3881}
3882
3883void
3884ospf_ls_ack_send (struct ospf_neighbor *nbr, struct ospf_lsa *lsa)
3885{
3886 struct ospf_interface *oi = nbr->oi;
3887
3888 if (listcount (oi->ls_ack_direct.ls_ack) == 0)
3889 oi->ls_ack_direct.dst = nbr->address.u.prefix4;
3890
3891 listnode_add (oi->ls_ack_direct.ls_ack, ospf_lsa_lock (lsa));
3892
3893 if (oi->t_ls_ack_direct == NULL)
3894 oi->t_ls_ack_direct =
3895 thread_add_event (master, ospf_ls_ack_send_event, oi, 0);
3896}
3897
3898/* Send Link State Acknowledgment delayed. */
3899void
3900ospf_ls_ack_send_delayed (struct ospf_interface *oi)
3901{
3902 struct in_addr dst;
3903
3904 /* Decide destination address. */
3905 /* RFC2328 Section 13.5 On non-broadcast
3906 networks, delayed Link State Acknowledgment packets must be
3907 unicast separately over each adjacency (i.e., neighbor whose
3908 state is >= Exchange). */
3909 if (oi->type == OSPF_IFTYPE_NBMA)
3910 {
3911 struct ospf_neighbor *nbr;
3912 struct route_node *rn;
3913
3914 for (rn = route_top (oi->nbrs); rn; rn = route_next (rn))
3915 if ((nbr = rn->info) != NULL)
3916 if (nbr != oi->nbr_self && nbr->state >= NSM_Exchange)
3917 while (listcount (oi->ls_ack))
3918 ospf_ls_ack_send_list (oi, oi->ls_ack, nbr->address.u.prefix4);
3919 return;
3920 }
3921 if (oi->type == OSPF_IFTYPE_VIRTUALLINK)
3922 dst.s_addr = oi->vl_data->peer_addr.s_addr;
3923 else if (oi->state == ISM_DR || oi->state == ISM_Backup)
3924 dst.s_addr = htonl (OSPF_ALLSPFROUTERS);
3925 else if (oi->type == OSPF_IFTYPE_POINTOPOINT)
3926 dst.s_addr = htonl (OSPF_ALLSPFROUTERS);
gdt630e4802004-08-31 17:28:41 +00003927 else if (oi->type == OSPF_IFTYPE_POINTOMULTIPOINT)
3928 dst.s_addr = htonl (OSPF_ALLSPFROUTERS);
paul718e3742002-12-13 20:15:29 +00003929 else
3930 dst.s_addr = htonl (OSPF_ALLDROUTERS);
3931
3932 while (listcount (oi->ls_ack))
3933 ospf_ls_ack_send_list (oi, oi->ls_ack, dst);
3934}