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