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