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