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