Coverage Report

Created: 2025-12-12 06:43

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