Coverage Report

Created: 2025-07-14 06:48

/src/frr/pimd/pim_msdp.c
Line
Count
Source (jump to first uncovered line)
1
// SPDX-License-Identifier: GPL-2.0-or-later
2
/*
3
 * IP MSDP for Quagga
4
 * Copyright (C) 2016 Cumulus Networks, Inc.
5
 */
6
7
#include <zebra.h>
8
9
#include <lib/hash.h>
10
#include <lib/jhash.h>
11
#include <lib/log.h>
12
#include <lib/prefix.h>
13
#include <lib/sockunion.h>
14
#include <lib/stream.h>
15
#include <frrevent.h>
16
#include <lib/vty.h>
17
#include <lib/plist.h>
18
#include <lib/lib_errors.h>
19
20
#include "pimd.h"
21
#include "pim_memory.h"
22
#include "pim_instance.h"
23
#include "pim_iface.h"
24
#include "pim_rp.h"
25
#include "pim_str.h"
26
#include "pim_time.h"
27
#include "pim_upstream.h"
28
#include "pim_oil.h"
29
30
#include "pim_msdp.h"
31
#include "pim_msdp_packet.h"
32
#include "pim_msdp_socket.h"
33
34
// struct pim_msdp pim_msdp, *msdp = &pim_msdp;
35
36
static void pim_msdp_peer_listen(struct pim_msdp_peer *mp);
37
static void pim_msdp_peer_cr_timer_setup(struct pim_msdp_peer *mp, bool start);
38
static void pim_msdp_peer_ka_timer_setup(struct pim_msdp_peer *mp, bool start);
39
static void pim_msdp_peer_hold_timer_setup(struct pim_msdp_peer *mp,
40
             bool start);
41
static void pim_msdp_peer_free(struct pim_msdp_peer *mp);
42
static void pim_msdp_enable(struct pim_instance *pim);
43
static void pim_msdp_sa_adv_timer_setup(struct pim_instance *pim, bool start);
44
static void pim_msdp_sa_deref(struct pim_msdp_sa *sa,
45
            enum pim_msdp_sa_flags flags);
46
static int pim_msdp_mg_mbr_comp(const void *p1, const void *p2);
47
static void pim_msdp_mg_mbr_free(struct pim_msdp_mg_mbr *mbr);
48
49
/************************ SA cache management ******************************/
50
static void pim_msdp_sa_timer_expiry_log(struct pim_msdp_sa *sa,
51
           const char *timer_str)
52
0
{
53
0
  zlog_debug("MSDP SA %s %s timer expired", sa->sg_str, timer_str);
54
0
}
55
56
/* RFC-3618:Sec-5.1 - global active source advertisement timer */
57
static void pim_msdp_sa_adv_timer_cb(struct event *t)
58
0
{
59
0
  struct pim_instance *pim = EVENT_ARG(t);
60
0
61
0
  if (PIM_DEBUG_MSDP_EVENTS) {
62
0
    zlog_debug("MSDP SA advertisement timer expired");
63
0
  }
64
0
65
0
  pim_msdp_sa_adv_timer_setup(pim, true /* start */);
66
0
  pim_msdp_pkt_sa_tx(pim);
67
0
}
68
69
static void pim_msdp_sa_adv_timer_setup(struct pim_instance *pim, bool start)
70
0
{
71
0
  EVENT_OFF(pim->msdp.sa_adv_timer);
72
0
  if (start) {
73
0
    event_add_timer(pim->msdp.master, pim_msdp_sa_adv_timer_cb, pim,
74
0
        PIM_MSDP_SA_ADVERTISMENT_TIME,
75
0
        &pim->msdp.sa_adv_timer);
76
0
  }
77
0
}
78
79
/* RFC-3618:Sec-5.3 - SA cache state timer */
80
static void pim_msdp_sa_state_timer_cb(struct event *t)
81
0
{
82
0
  struct pim_msdp_sa *sa;
83
0
84
0
  sa = EVENT_ARG(t);
85
0
86
0
  if (PIM_DEBUG_MSDP_EVENTS) {
87
0
    pim_msdp_sa_timer_expiry_log(sa, "state");
88
0
  }
89
0
90
0
  pim_msdp_sa_deref(sa, PIM_MSDP_SAF_PEER);
91
0
}
92
93
static void pim_msdp_sa_state_timer_setup(struct pim_msdp_sa *sa, bool start)
94
0
{
95
0
  EVENT_OFF(sa->sa_state_timer);
96
0
  if (start) {
97
0
    event_add_timer(sa->pim->msdp.master,
98
0
        pim_msdp_sa_state_timer_cb, sa,
99
0
        PIM_MSDP_SA_HOLD_TIME, &sa->sa_state_timer);
100
0
  }
101
0
}
102
103
static void pim_msdp_sa_upstream_del(struct pim_msdp_sa *sa)
104
0
{
105
0
  struct pim_upstream *up = sa->up;
106
0
  if (!up) {
107
0
    return;
108
0
  }
109
110
0
  sa->up = NULL;
111
0
  if (PIM_UPSTREAM_FLAG_TEST_SRC_MSDP(up->flags)) {
112
0
    PIM_UPSTREAM_FLAG_UNSET_SRC_MSDP(up->flags);
113
0
    sa->flags |= PIM_MSDP_SAF_UP_DEL_IN_PROG;
114
0
    up = pim_upstream_del(sa->pim, up, __func__);
115
    /* re-eval joinDesired; clearing peer-msdp-sa flag can
116
     * cause JD to change
117
     */
118
0
    if (up)
119
0
      pim_upstream_update_join_desired(sa->pim, up);
120
0
    sa->flags &= ~PIM_MSDP_SAF_UP_DEL_IN_PROG;
121
0
  }
122
123
0
  if (PIM_DEBUG_MSDP_EVENTS) {
124
0
    zlog_debug("MSDP SA %s de-referenced SPT", sa->sg_str);
125
0
  }
126
0
}
127
128
static bool pim_msdp_sa_upstream_add_ok(struct pim_msdp_sa *sa,
129
          struct pim_upstream *xg_up)
130
0
{
131
0
  if (!(sa->flags & PIM_MSDP_SAF_PEER)) {
132
    /* SA should have been rxed from a peer */
133
0
    return false;
134
0
  }
135
  /* check if we are RP */
136
0
  if (!I_am_RP(sa->pim, sa->sg.grp)) {
137
0
    return false;
138
0
  }
139
140
  /* check if we have a (*, G) with a non-empty immediate OIL */
141
0
  if (!xg_up) {
142
0
    pim_sgaddr sg;
143
144
0
    memset(&sg, 0, sizeof(sg));
145
0
    sg.grp = sa->sg.grp;
146
147
0
    xg_up = pim_upstream_find(sa->pim, &sg);
148
0
  }
149
0
  if (!xg_up || (xg_up->join_state != PIM_UPSTREAM_JOINED)) {
150
    /* join desired will be true for such (*, G) entries so we will
151
     * just look at join_state and let the PIM state machine do the
152
     * rest of
153
     * the magic */
154
0
    return false;
155
0
  }
156
157
0
  return true;
158
0
}
159
160
/* Upstream add evaluation needs to happen everytime -
161
 * 1. Peer reference is added or removed.
162
 * 2. The RP for a group changes.
163
 * 3. joinDesired for the associated (*, G) changes
164
 * 4. associated (*, G) is removed - this seems like a bit redundant
165
 *    (considering #4); but just in case an entry gets nuked without
166
 *    upstream state transition
167
 *    */
168
static void pim_msdp_sa_upstream_update(struct pim_msdp_sa *sa,
169
          struct pim_upstream *xg_up,
170
          const char *ctx)
171
0
{
172
0
  struct pim_upstream *up;
173
174
0
  if (!pim_msdp_sa_upstream_add_ok(sa, xg_up)) {
175
0
    pim_msdp_sa_upstream_del(sa);
176
0
    return;
177
0
  }
178
179
0
  if (sa->up) {
180
    /* nothing to do */
181
0
    return;
182
0
  }
183
184
0
  up = pim_upstream_find(sa->pim, &sa->sg);
185
0
  if (up && (PIM_UPSTREAM_FLAG_TEST_SRC_MSDP(up->flags))) {
186
    /* somehow we lost track of the upstream ptr? best log it */
187
0
    sa->up = up;
188
0
    if (PIM_DEBUG_MSDP_EVENTS) {
189
0
      zlog_debug("MSDP SA %s SPT reference missing",
190
0
           sa->sg_str);
191
0
    }
192
0
    return;
193
0
  }
194
195
  /* RFC3618: "RP triggers a (S, G) join event towards the data source
196
   * as if a JP message was rxed addressed to the RP itself." */
197
0
  up = pim_upstream_add(sa->pim, &sa->sg, NULL /* iif */,
198
0
            PIM_UPSTREAM_FLAG_MASK_SRC_MSDP, __func__, NULL);
199
200
0
  sa->up = up;
201
0
  if (up) {
202
    /* update inherited oil */
203
0
    pim_upstream_inherited_olist(sa->pim, up);
204
    /* should we also start the kat in parallel? we will need it
205
     * when the
206
     * SA ages out */
207
0
    if (PIM_DEBUG_MSDP_EVENTS) {
208
0
      zlog_debug("MSDP SA %s referenced SPT", sa->sg_str);
209
0
    }
210
0
  } else {
211
0
    if (PIM_DEBUG_MSDP_EVENTS) {
212
0
      zlog_debug("MSDP SA %s SPT reference failed",
213
0
           sa->sg_str);
214
0
    }
215
0
  }
216
0
}
217
218
/* release all mem associated with a sa */
219
static void pim_msdp_sa_free(struct pim_msdp_sa *sa)
220
0
{
221
0
  pim_msdp_sa_state_timer_setup(sa, false);
222
223
0
  XFREE(MTYPE_PIM_MSDP_SA, sa);
224
0
}
225
226
static struct pim_msdp_sa *pim_msdp_sa_new(struct pim_instance *pim,
227
             pim_sgaddr *sg, struct in_addr rp)
228
0
{
229
0
  struct pim_msdp_sa *sa;
230
231
0
  sa = XCALLOC(MTYPE_PIM_MSDP_SA, sizeof(*sa));
232
233
0
  sa->pim = pim;
234
0
  sa->sg = *sg;
235
0
  snprintfrr(sa->sg_str, sizeof(sa->sg_str), "%pSG", sg);
236
0
  sa->rp = rp;
237
0
  sa->uptime = pim_time_monotonic_sec();
238
239
  /* insert into misc tables for easy access */
240
0
  sa = hash_get(pim->msdp.sa_hash, sa, hash_alloc_intern);
241
0
  listnode_add_sort(pim->msdp.sa_list, sa);
242
243
0
  if (PIM_DEBUG_MSDP_EVENTS) {
244
0
    zlog_debug("MSDP SA %s created", sa->sg_str);
245
0
  }
246
247
0
  return sa;
248
0
}
249
250
static struct pim_msdp_sa *pim_msdp_sa_find(struct pim_instance *pim,
251
              pim_sgaddr *sg)
252
52.6k
{
253
52.6k
  struct pim_msdp_sa lookup;
254
255
52.6k
  lookup.sg = *sg;
256
52.6k
  return hash_lookup(pim->msdp.sa_hash, &lookup);
257
52.6k
}
258
259
static struct pim_msdp_sa *pim_msdp_sa_add(struct pim_instance *pim,
260
             pim_sgaddr *sg, struct in_addr rp)
261
0
{
262
0
  struct pim_msdp_sa *sa;
263
264
0
  sa = pim_msdp_sa_find(pim, sg);
265
0
  if (sa) {
266
0
    return sa;
267
0
  }
268
269
0
  return pim_msdp_sa_new(pim, sg, rp);
270
0
}
271
272
static void pim_msdp_sa_del(struct pim_msdp_sa *sa)
273
0
{
274
  /* this is somewhat redundant - still want to be careful not to leave
275
   * stale upstream references */
276
0
  pim_msdp_sa_upstream_del(sa);
277
278
  /* stop timers */
279
0
  pim_msdp_sa_state_timer_setup(sa, false /* start */);
280
281
  /* remove the entry from various tables */
282
0
  listnode_delete(sa->pim->msdp.sa_list, sa);
283
0
  hash_release(sa->pim->msdp.sa_hash, sa);
284
285
0
  if (PIM_DEBUG_MSDP_EVENTS) {
286
0
    zlog_debug("MSDP SA %s deleted", sa->sg_str);
287
0
  }
288
289
  /* free up any associated memory */
290
0
  pim_msdp_sa_free(sa);
291
0
}
292
293
static void pim_msdp_sa_peer_ip_set(struct pim_msdp_sa *sa,
294
            struct pim_msdp_peer *mp, struct in_addr rp)
295
0
{
296
0
  struct pim_msdp_peer *old_mp;
297
298
  /* optimize the "no change" case as it will happen
299
   * frequently/periodically */
300
0
  if (mp && (sa->peer.s_addr == mp->peer.s_addr)) {
301
0
    return;
302
0
  }
303
304
  /* any time the peer ip changes also update the rp address */
305
0
  if (sa->peer.s_addr != INADDR_ANY) {
306
0
    old_mp = pim_msdp_peer_find(sa->pim, sa->peer);
307
0
    if (old_mp && old_mp->sa_cnt) {
308
0
      --old_mp->sa_cnt;
309
0
    }
310
0
  }
311
312
0
  if (mp) {
313
0
    ++mp->sa_cnt;
314
0
    sa->peer = mp->peer;
315
0
  } else {
316
0
    sa->peer.s_addr = PIM_NET_INADDR_ANY;
317
0
  }
318
0
  sa->rp = rp;
319
0
}
320
321
/* When a local active-source is removed there is no way to withdraw the
322
 * source from peers. We will simply remove it from the SA cache so it will
323
 * not be sent in supsequent SA updates. Peers will consequently timeout the
324
 * SA.
325
 * Similarly a "peer-added" SA is never explicitly deleted. It is simply
326
 * aged out overtime if not seen in the SA updates from the peers.
327
 * XXX: should we provide a knob to drop entries learnt from a peer when the
328
 * peer goes down? */
329
static void pim_msdp_sa_deref(struct pim_msdp_sa *sa,
330
            enum pim_msdp_sa_flags flags)
331
0
{
332
0
  bool update_up = false;
333
334
0
  if ((sa->flags & PIM_MSDP_SAF_LOCAL)) {
335
0
    if (flags & PIM_MSDP_SAF_LOCAL) {
336
0
      if (PIM_DEBUG_MSDP_EVENTS) {
337
0
        zlog_debug("MSDP SA %s local reference removed",
338
0
             sa->sg_str);
339
0
      }
340
0
      if (sa->pim->msdp.local_cnt)
341
0
        --sa->pim->msdp.local_cnt;
342
0
    }
343
0
  }
344
345
0
  if ((sa->flags & PIM_MSDP_SAF_PEER)) {
346
0
    if (flags & PIM_MSDP_SAF_PEER) {
347
0
      struct in_addr rp;
348
349
0
      if (PIM_DEBUG_MSDP_EVENTS) {
350
0
        zlog_debug("MSDP SA %s peer reference removed",
351
0
             sa->sg_str);
352
0
      }
353
0
      pim_msdp_sa_state_timer_setup(sa, false /* start */);
354
0
      rp.s_addr = INADDR_ANY;
355
0
      pim_msdp_sa_peer_ip_set(sa, NULL /* mp */, rp);
356
      /* if peer ref was removed we need to remove the msdp
357
       * reference on the
358
       * msdp entry */
359
0
      update_up = true;
360
0
    }
361
0
  }
362
363
0
  sa->flags &= ~flags;
364
0
  if (update_up) {
365
0
    pim_msdp_sa_upstream_update(sa, NULL /* xg_up */, "sa-deref");
366
0
  }
367
368
0
  if (!(sa->flags & PIM_MSDP_SAF_REF)) {
369
0
    pim_msdp_sa_del(sa);
370
0
  }
371
0
}
372
373
void pim_msdp_sa_ref(struct pim_instance *pim, struct pim_msdp_peer *mp,
374
         pim_sgaddr *sg, struct in_addr rp)
375
0
{
376
0
  struct pim_msdp_sa *sa;
377
378
0
  sa = pim_msdp_sa_add(pim, sg, rp);
379
0
  if (!sa) {
380
0
    return;
381
0
  }
382
383
  /* reference it */
384
0
  if (mp) {
385
0
    if (!(sa->flags & PIM_MSDP_SAF_PEER)) {
386
0
      sa->flags |= PIM_MSDP_SAF_PEER;
387
0
      if (PIM_DEBUG_MSDP_EVENTS) {
388
0
        zlog_debug("MSDP SA %s added by peer",
389
0
             sa->sg_str);
390
0
      }
391
0
    }
392
0
    pim_msdp_sa_peer_ip_set(sa, mp, rp);
393
    /* start/re-start the state timer to prevent cache expiry */
394
0
    pim_msdp_sa_state_timer_setup(sa, true /* start */);
395
    /* We re-evaluate SA "SPT-trigger" everytime we hear abt it from
396
     * a
397
     * peer. XXX: If this becomes too much of a periodic overhead we
398
     * can make it event based */
399
0
    pim_msdp_sa_upstream_update(sa, NULL /* xg_up */, "peer-ref");
400
0
  } else {
401
0
    if (!(sa->flags & PIM_MSDP_SAF_LOCAL)) {
402
0
      sa->flags |= PIM_MSDP_SAF_LOCAL;
403
0
      ++sa->pim->msdp.local_cnt;
404
0
      if (PIM_DEBUG_MSDP_EVENTS) {
405
0
        zlog_debug("MSDP SA %s added locally",
406
0
             sa->sg_str);
407
0
      }
408
      /* send an immediate SA update to peers */
409
0
      sa->rp = pim->msdp.originator_id;
410
0
      pim_msdp_pkt_sa_tx_one(sa);
411
0
    }
412
0
    sa->flags &= ~PIM_MSDP_SAF_STALE;
413
0
  }
414
0
}
415
416
/* The following criteria must be met to originate an SA from the MSDP
417
 * speaker -
418
 * 1. KAT must be running i.e. source is active.
419
 * 2. We must be RP for the group.
420
 * 3. Source must be registrable to the RP (this is where the RFC is vague
421
 *    and especially ambiguous in CLOS networks; with anycast RP all sources
422
 *    are potentially registrable to all RPs in the domain). We assume #3 is
423
 *    satisfied if -
424
 *    a. We are also the FHR-DR for the source (OR)
425
 *    b. We rxed a pim register (null or data encapsulated) within the last
426
 *       (3 * (1.5 * register_suppression_timer))).
427
 */
428
static bool pim_msdp_sa_local_add_ok(struct pim_upstream *up)
429
0
{
430
0
  struct pim_instance *pim = up->channel_oil->pim;
431
432
0
  if (!(pim->msdp.flags & PIM_MSDPF_ENABLE)) {
433
0
    return false;
434
0
  }
435
436
0
  if (!pim_upstream_is_kat_running(up))
437
    /* stream is not active */
438
0
    return false;
439
440
0
  if (!I_am_RP(pim, up->sg.grp)) {
441
    /* we are not RP for the group */
442
0
    return false;
443
0
  }
444
445
  /* we are the FHR-DR for this stream  or we are RP and have seen
446
   * registers
447
   * from a FHR for this source */
448
0
  if (PIM_UPSTREAM_FLAG_TEST_FHR(up->flags) || up->t_msdp_reg_timer) {
449
0
    return true;
450
0
  }
451
452
0
  return false;
453
0
}
454
455
static void pim_msdp_sa_local_add(struct pim_instance *pim, pim_sgaddr *sg)
456
0
{
457
0
  struct in_addr rp;
458
0
  rp.s_addr = INADDR_ANY;
459
0
  pim_msdp_sa_ref(pim, NULL /* mp */, sg, rp);
460
0
}
461
462
void pim_msdp_sa_local_del(struct pim_instance *pim, pim_sgaddr *sg)
463
0
{
464
0
  struct pim_msdp_sa *sa;
465
466
0
  sa = pim_msdp_sa_find(pim, sg);
467
0
  if (sa) {
468
0
    pim_msdp_sa_deref(sa, PIM_MSDP_SAF_LOCAL);
469
0
  }
470
0
}
471
472
/* we need to be very cautious with this API as SA del too can trigger an
473
 * upstream del and we will get stuck in a simple loop */
474
static void pim_msdp_sa_local_del_on_up_del(struct pim_instance *pim,
475
              pim_sgaddr *sg)
476
52.6k
{
477
52.6k
  struct pim_msdp_sa *sa;
478
479
52.6k
  sa = pim_msdp_sa_find(pim, sg);
480
52.6k
  if (sa) {
481
0
    if (PIM_DEBUG_MSDP_INTERNAL) {
482
0
      zlog_debug("MSDP local sa %s del on up del",
483
0
           sa->sg_str);
484
0
    }
485
486
    /* if there is no local reference escape */
487
0
    if (!(sa->flags & PIM_MSDP_SAF_LOCAL)) {
488
0
      if (PIM_DEBUG_MSDP_INTERNAL) {
489
0
        zlog_debug("MSDP local sa %s del; no local ref",
490
0
             sa->sg_str);
491
0
      }
492
0
      return;
493
0
    }
494
495
0
    if (sa->flags & PIM_MSDP_SAF_UP_DEL_IN_PROG) {
496
      /* MSDP is the one that triggered the upstream del. if
497
       * this happens
498
       * we most certainly have a bug in the PIM upstream
499
       * state machine. We
500
       * will not have a local reference unless the KAT is
501
       * running. And if the
502
       * KAT is running there MUST be an additional
503
       * source-stream reference to
504
       * the flow. Accounting for such cases requires lot of
505
       * changes; perhaps
506
       * address this in the next release? - XXX  */
507
0
      flog_err(
508
0
        EC_LIB_DEVELOPMENT,
509
0
        "MSDP sa %s SPT teardown is causing the local entry to be removed",
510
0
        sa->sg_str);
511
0
      return;
512
0
    }
513
514
    /* we are dropping the sa on upstream del we should not have an
515
     * upstream reference */
516
0
    if (sa->up) {
517
0
      if (PIM_DEBUG_MSDP_INTERNAL) {
518
0
        zlog_debug("MSDP local sa %s del; up non-NULL",
519
0
             sa->sg_str);
520
0
      }
521
0
      sa->up = NULL;
522
0
    }
523
0
    pim_msdp_sa_deref(sa, PIM_MSDP_SAF_LOCAL);
524
0
  }
525
52.6k
}
526
527
/* Local SA qualification needs to be re-evaluated when -
528
 * 1. KAT is started or stopped
529
 * 2. on RP changes
530
 * 3. Whenever FHR status changes for a (S,G) - XXX - currently there
531
 *    is no clear path to transition an entry out of "MASK_FHR" need
532
 *    to discuss this with Donald. May result in some strangeness if the
533
 *    FHR is also the RP.
534
 * 4. When msdp_reg timer is started or stopped
535
 */
536
void pim_msdp_sa_local_update(struct pim_upstream *up)
537
0
{
538
0
  struct pim_instance *pim = up->channel_oil->pim;
539
540
0
  if (pim_msdp_sa_local_add_ok(up)) {
541
0
    pim_msdp_sa_local_add(pim, &up->sg);
542
0
  } else {
543
0
    pim_msdp_sa_local_del(pim, &up->sg);
544
0
  }
545
0
}
546
547
static void pim_msdp_sa_local_setup(struct pim_instance *pim)
548
0
{
549
0
  struct pim_upstream *up;
550
551
0
  frr_each (rb_pim_upstream, &pim->upstream_head, up)
552
0
    pim_msdp_sa_local_update(up);
553
0
}
554
555
/* whenever the RP changes we need to re-evaluate the "local" SA-cache */
556
/* XXX: needs to be tested */
557
void pim_msdp_i_am_rp_changed(struct pim_instance *pim)
558
20.2k
{
559
20.2k
  struct listnode *sanode;
560
20.2k
  struct listnode *nextnode;
561
20.2k
  struct pim_msdp_sa *sa;
562
563
20.2k
  if (!(pim->msdp.flags & PIM_MSDPF_ENABLE)) {
564
    /* if the feature is not enabled do nothing */
565
20.2k
    return;
566
20.2k
  }
567
568
0
  if (PIM_DEBUG_MSDP_INTERNAL) {
569
0
    zlog_debug("MSDP i_am_rp changed");
570
0
  }
571
572
  /* mark all local entries as stale */
573
0
  for (ALL_LIST_ELEMENTS_RO(pim->msdp.sa_list, sanode, sa)) {
574
0
    if (sa->flags & PIM_MSDP_SAF_LOCAL) {
575
0
      sa->flags |= PIM_MSDP_SAF_STALE;
576
0
    }
577
0
  }
578
579
  /* re-setup local SA entries */
580
0
  pim_msdp_sa_local_setup(pim);
581
582
0
  for (ALL_LIST_ELEMENTS(pim->msdp.sa_list, sanode, nextnode, sa)) {
583
    /* purge stale SA entries */
584
0
    if (sa->flags & PIM_MSDP_SAF_STALE) {
585
      /* clear the stale flag; the entry may be kept even
586
       * after
587
       * "local-deref" */
588
0
      sa->flags &= ~PIM_MSDP_SAF_STALE;
589
      /* sa_deref can end up freeing the sa; so don't access
590
       * contents after */
591
0
      pim_msdp_sa_deref(sa, PIM_MSDP_SAF_LOCAL);
592
0
    } else {
593
      /* if the souce is still active check if we can
594
       * influence SPT */
595
0
      pim_msdp_sa_upstream_update(sa, NULL /* xg_up */,
596
0
                "rp-change");
597
0
    }
598
0
  }
599
0
}
600
601
/* We track the join state of (*, G) entries. If G has sources in the SA-cache
602
 * we need to setup or teardown SPT when the JoinDesired status changes for
603
 * (*, G) */
604
void pim_msdp_up_join_state_changed(struct pim_instance *pim,
605
            struct pim_upstream *xg_up)
606
0
{
607
0
  struct listnode *sanode;
608
0
  struct pim_msdp_sa *sa;
609
610
0
  if (PIM_DEBUG_MSDP_INTERNAL) {
611
0
    zlog_debug("MSDP join state changed for %s", xg_up->sg_str);
612
0
  }
613
614
  /* If this is not really an XG entry just move on */
615
0
  if (!pim_addr_is_any(xg_up->sg.src) || pim_addr_is_any(xg_up->sg.grp)) {
616
0
    return;
617
0
  }
618
619
  /* XXX: Need to maintain SAs per-group to avoid all this unnecessary
620
   * walking */
621
0
  for (ALL_LIST_ELEMENTS_RO(pim->msdp.sa_list, sanode, sa)) {
622
0
    if (pim_addr_cmp(sa->sg.grp, xg_up->sg.grp)) {
623
0
      continue;
624
0
    }
625
0
    pim_msdp_sa_upstream_update(sa, xg_up, "up-jp-change");
626
0
  }
627
0
}
628
629
static void pim_msdp_up_xg_del(struct pim_instance *pim, pim_sgaddr *sg)
630
0
{
631
0
  struct listnode *sanode;
632
0
  struct pim_msdp_sa *sa;
633
634
0
  if (PIM_DEBUG_MSDP_INTERNAL) {
635
0
    zlog_debug("MSDP %pSG del", sg);
636
0
  }
637
638
  /* If this is not really an XG entry just move on */
639
0
  if (!pim_addr_is_any(sg->src) || pim_addr_is_any(sg->grp)) {
640
0
    return;
641
0
  }
642
643
  /* XXX: Need to maintain SAs per-group to avoid all this unnecessary
644
   * walking */
645
0
  for (ALL_LIST_ELEMENTS_RO(pim->msdp.sa_list, sanode, sa)) {
646
0
    if (pim_addr_cmp(sa->sg.grp, sg->grp)) {
647
0
      continue;
648
0
    }
649
0
    pim_msdp_sa_upstream_update(sa, NULL /* xg */, "up-jp-change");
650
0
  }
651
0
}
652
653
void pim_msdp_up_del(struct pim_instance *pim, pim_sgaddr *sg)
654
52.6k
{
655
52.6k
  if (PIM_DEBUG_MSDP_INTERNAL) {
656
0
    zlog_debug("MSDP up %pSG del", sg);
657
0
  }
658
52.6k
  if (pim_addr_is_any(sg->src)) {
659
0
    pim_msdp_up_xg_del(pim, sg);
660
52.6k
  } else {
661
52.6k
    pim_msdp_sa_local_del_on_up_del(pim, sg);
662
52.6k
  }
663
52.6k
}
664
665
/* sa hash and peer list helpers */
666
static unsigned int pim_msdp_sa_hash_key_make(const void *p)
667
0
{
668
0
  const struct pim_msdp_sa *sa = p;
669
670
0
  return pim_sgaddr_hash(sa->sg, 0);
671
0
}
672
673
static bool pim_msdp_sa_hash_eq(const void *p1, const void *p2)
674
0
{
675
0
  const struct pim_msdp_sa *sa1 = p1;
676
0
  const struct pim_msdp_sa *sa2 = p2;
677
678
0
  return !pim_sgaddr_cmp(sa1->sg, sa2->sg);
679
0
}
680
681
static int pim_msdp_sa_comp(const void *p1, const void *p2)
682
0
{
683
0
  const struct pim_msdp_sa *sa1 = p1;
684
0
  const struct pim_msdp_sa *sa2 = p2;
685
686
0
  return pim_sgaddr_cmp(sa1->sg, sa2->sg);
687
0
}
688
689
/* RFC-3618:Sec-10.1.3 - Peer-RPF forwarding */
690
/* XXX: this can use a bit of refining and extensions */
691
bool pim_msdp_peer_rpf_check(struct pim_msdp_peer *mp, struct in_addr rp)
692
0
{
693
0
  struct pim_nexthop nexthop = {0};
694
695
0
  if (mp->peer.s_addr == rp.s_addr) {
696
0
    return true;
697
0
  }
698
699
  /* check if the MSDP peer is the nexthop for the RP */
700
0
  if (pim_nexthop_lookup(mp->pim, &nexthop, rp, 0) &&
701
0
      nexthop.mrib_nexthop_addr.s_addr == mp->peer.s_addr) {
702
0
    return true;
703
0
  }
704
705
0
  return false;
706
0
}
707
708
/************************ Peer session management **************************/
709
char *pim_msdp_state_dump(enum pim_msdp_peer_state state, char *buf,
710
        int buf_size)
711
0
{
712
0
  switch (state) {
713
0
  case PIM_MSDP_DISABLED:
714
0
    snprintf(buf, buf_size, "%s", "disabled");
715
0
    break;
716
0
  case PIM_MSDP_INACTIVE:
717
0
    snprintf(buf, buf_size, "%s", "inactive");
718
0
    break;
719
0
  case PIM_MSDP_LISTEN:
720
0
    snprintf(buf, buf_size, "%s", "listen");
721
0
    break;
722
0
  case PIM_MSDP_CONNECTING:
723
0
    snprintf(buf, buf_size, "%s", "connecting");
724
0
    break;
725
0
  case PIM_MSDP_ESTABLISHED:
726
0
    snprintf(buf, buf_size, "%s", "established");
727
0
    break;
728
0
  default:
729
0
    snprintf(buf, buf_size, "unk-%d", state);
730
0
  }
731
0
  return buf;
732
0
}
733
734
static void pim_msdp_peer_state_chg_log(struct pim_msdp_peer *mp)
735
0
{
736
0
  char state_str[PIM_MSDP_STATE_STRLEN];
737
738
0
  pim_msdp_state_dump(mp->state, state_str, sizeof(state_str));
739
0
  zlog_debug("MSDP peer %s state chg to %s", mp->key_str, state_str);
740
0
}
741
742
/* MSDP Connection State Machine actions (defined in RFC-3618:Sec-11.2) */
743
/* 11.2.A2: active peer - start connect retry timer; when the timer fires
744
 * a tcp connection will be made */
745
static void pim_msdp_peer_connect(struct pim_msdp_peer *mp)
746
0
{
747
0
  mp->state = PIM_MSDP_CONNECTING;
748
0
  if (PIM_DEBUG_MSDP_EVENTS) {
749
0
    pim_msdp_peer_state_chg_log(mp);
750
0
  }
751
752
0
  pim_msdp_peer_cr_timer_setup(mp, true /* start */);
753
0
}
754
755
/* 11.2.A3: passive peer - just listen for connections */
756
static void pim_msdp_peer_listen(struct pim_msdp_peer *mp)
757
0
{
758
0
  mp->state = PIM_MSDP_LISTEN;
759
0
  if (PIM_DEBUG_MSDP_EVENTS) {
760
0
    pim_msdp_peer_state_chg_log(mp);
761
0
  }
762
763
  /* this is interntionally asymmetric i.e. we set up listen-socket when
764
  * the
765
  * first listening peer is configured; but don't bother tearing it down
766
  * when
767
  * all the peers go down */
768
0
  pim_msdp_sock_listen(mp->pim);
769
0
}
770
771
/* 11.2.A4 and 11.2.A5: transition active or passive peer to
772
 * established state */
773
void pim_msdp_peer_established(struct pim_msdp_peer *mp)
774
0
{
775
0
  if (mp->state != PIM_MSDP_ESTABLISHED) {
776
0
    ++mp->est_flaps;
777
0
  }
778
779
0
  mp->state = PIM_MSDP_ESTABLISHED;
780
0
  mp->uptime = pim_time_monotonic_sec();
781
782
0
  if (PIM_DEBUG_MSDP_EVENTS) {
783
0
    pim_msdp_peer_state_chg_log(mp);
784
0
  }
785
786
  /* stop retry timer on active peers */
787
0
  pim_msdp_peer_cr_timer_setup(mp, false /* start */);
788
789
  /* send KA; start KA and hold timers */
790
0
  pim_msdp_pkt_ka_tx(mp);
791
0
  pim_msdp_peer_ka_timer_setup(mp, true /* start */);
792
0
  pim_msdp_peer_hold_timer_setup(mp, true /* start */);
793
794
0
  pim_msdp_pkt_sa_tx_to_one_peer(mp);
795
796
0
  PIM_MSDP_PEER_WRITE_ON(mp);
797
0
  PIM_MSDP_PEER_READ_ON(mp);
798
0
}
799
800
/* 11.2.A6, 11.2.A7 and 11.2.A8: shutdown the peer tcp connection */
801
void pim_msdp_peer_stop_tcp_conn(struct pim_msdp_peer *mp, bool chg_state)
802
0
{
803
0
  if (chg_state) {
804
0
    if (mp->state == PIM_MSDP_ESTABLISHED) {
805
0
      ++mp->est_flaps;
806
0
    }
807
0
    mp->state = PIM_MSDP_INACTIVE;
808
0
    if (PIM_DEBUG_MSDP_EVENTS) {
809
0
      pim_msdp_peer_state_chg_log(mp);
810
0
    }
811
0
  }
812
813
0
  if (PIM_DEBUG_MSDP_INTERNAL) {
814
0
    zlog_debug("MSDP peer %s pim_msdp_peer_stop_tcp_conn",
815
0
         mp->key_str);
816
0
  }
817
  /* stop read and write threads */
818
0
  PIM_MSDP_PEER_READ_OFF(mp);
819
0
  PIM_MSDP_PEER_WRITE_OFF(mp);
820
821
  /* reset buffers */
822
0
  mp->packet_size = 0;
823
0
  if (mp->ibuf)
824
0
    stream_reset(mp->ibuf);
825
0
  if (mp->obuf)
826
0
    stream_fifo_clean(mp->obuf);
827
828
  /* stop all peer timers */
829
0
  pim_msdp_peer_ka_timer_setup(mp, false /* start */);
830
0
  pim_msdp_peer_cr_timer_setup(mp, false /* start */);
831
0
  pim_msdp_peer_hold_timer_setup(mp, false /* start */);
832
833
  /* close connection */
834
0
  if (mp->fd >= 0) {
835
0
    close(mp->fd);
836
0
    mp->fd = -1;
837
0
  }
838
0
}
839
840
/* RFC-3618:Sec-5.6 - stop the peer tcp connection and startover */
841
void pim_msdp_peer_reset_tcp_conn(struct pim_msdp_peer *mp, const char *rc_str)
842
0
{
843
0
  if (PIM_DEBUG_EVENTS) {
844
0
    zlog_debug("MSDP peer %s tcp reset %s", mp->key_str, rc_str);
845
0
    snprintf(mp->last_reset, sizeof(mp->last_reset), "%s", rc_str);
846
0
  }
847
848
  /* close the connection and transition to listening or connecting */
849
0
  pim_msdp_peer_stop_tcp_conn(mp, true /* chg_state */);
850
0
  if (PIM_MSDP_PEER_IS_LISTENER(mp)) {
851
0
    pim_msdp_peer_listen(mp);
852
0
  } else {
853
0
    pim_msdp_peer_connect(mp);
854
0
  }
855
0
}
856
857
static void pim_msdp_peer_timer_expiry_log(struct pim_msdp_peer *mp,
858
             const char *timer_str)
859
0
{
860
0
  zlog_debug("MSDP peer %s %s timer expired", mp->key_str, timer_str);
861
0
}
862
863
/* RFC-3618:Sec-5.4 - peer hold timer */
864
static void pim_msdp_peer_hold_timer_cb(struct event *t)
865
0
{
866
0
  struct pim_msdp_peer *mp;
867
0
868
0
  mp = EVENT_ARG(t);
869
0
870
0
  if (PIM_DEBUG_MSDP_EVENTS) {
871
0
    pim_msdp_peer_timer_expiry_log(mp, "hold");
872
0
  }
873
0
874
0
  if (mp->state != PIM_MSDP_ESTABLISHED) {
875
0
    return;
876
0
  }
877
0
878
0
  if (PIM_DEBUG_MSDP_EVENTS) {
879
0
    pim_msdp_peer_state_chg_log(mp);
880
0
  }
881
0
  pim_msdp_peer_reset_tcp_conn(mp, "ht-expired");
882
0
}
883
884
static void pim_msdp_peer_hold_timer_setup(struct pim_msdp_peer *mp, bool start)
885
0
{
886
0
  struct pim_instance *pim = mp->pim;
887
0
  EVENT_OFF(mp->hold_timer);
888
0
  if (start) {
889
0
    event_add_timer(pim->msdp.master, pim_msdp_peer_hold_timer_cb,
890
0
        mp, pim->msdp.hold_time, &mp->hold_timer);
891
0
  }
892
0
}
893
894
895
/* RFC-3618:Sec-5.5 - peer keepalive timer */
896
static void pim_msdp_peer_ka_timer_cb(struct event *t)
897
0
{
898
0
  struct pim_msdp_peer *mp;
899
0
900
0
  mp = EVENT_ARG(t);
901
0
902
0
  if (PIM_DEBUG_MSDP_EVENTS) {
903
0
    pim_msdp_peer_timer_expiry_log(mp, "ka");
904
0
  }
905
0
906
0
  pim_msdp_pkt_ka_tx(mp);
907
0
  pim_msdp_peer_ka_timer_setup(mp, true /* start */);
908
0
}
909
910
static void pim_msdp_peer_ka_timer_setup(struct pim_msdp_peer *mp, bool start)
911
0
{
912
0
  EVENT_OFF(mp->ka_timer);
913
0
  if (start) {
914
0
    event_add_timer(mp->pim->msdp.master, pim_msdp_peer_ka_timer_cb,
915
0
        mp, mp->pim->msdp.keep_alive, &mp->ka_timer);
916
0
  }
917
0
}
918
919
static void pim_msdp_peer_active_connect(struct pim_msdp_peer *mp)
920
0
{
921
0
  int rc;
922
0
  ++mp->conn_attempts;
923
0
  rc = pim_msdp_sock_connect(mp);
924
0
925
0
  if (PIM_DEBUG_MSDP_INTERNAL) {
926
0
    zlog_debug("MSDP peer %s pim_msdp_peer_active_connect: %d",
927
0
         mp->key_str, rc);
928
0
  }
929
0
930
0
  switch (rc) {
931
0
  case connect_error:
932
0
  case -1:
933
0
    /* connect failed restart the connect-retry timer */
934
0
    pim_msdp_peer_cr_timer_setup(mp, true /* start */);
935
0
    break;
936
0
937
0
  case connect_success:
938
0
    /* connect was sucessful move to established */
939
0
    pim_msdp_peer_established(mp);
940
0
    break;
941
0
942
0
  case connect_in_progress:
943
0
    /* for NB content we need to wait till sock is readable or
944
0
     * writeable */
945
0
    PIM_MSDP_PEER_WRITE_ON(mp);
946
0
    PIM_MSDP_PEER_READ_ON(mp);
947
0
    /* also restart connect-retry timer to reset the socket if
948
0
     * connect is
949
0
     * not sucessful */
950
0
    pim_msdp_peer_cr_timer_setup(mp, true /* start */);
951
0
    break;
952
0
  }
953
0
}
954
955
/* RFC-3618:Sec-5.6 - connection retry on active peer */
956
static void pim_msdp_peer_cr_timer_cb(struct event *t)
957
0
{
958
0
  struct pim_msdp_peer *mp;
959
0
960
0
  mp = EVENT_ARG(t);
961
0
962
0
  if (PIM_DEBUG_MSDP_EVENTS) {
963
0
    pim_msdp_peer_timer_expiry_log(mp, "connect-retry");
964
0
  }
965
0
966
0
  if (mp->state != PIM_MSDP_CONNECTING || PIM_MSDP_PEER_IS_LISTENER(mp)) {
967
0
    return;
968
0
  }
969
0
970
0
  pim_msdp_peer_active_connect(mp);
971
0
}
972
973
static void pim_msdp_peer_cr_timer_setup(struct pim_msdp_peer *mp, bool start)
974
0
{
975
0
  EVENT_OFF(mp->cr_timer);
976
0
  if (start) {
977
0
    event_add_timer(mp->pim->msdp.master, pim_msdp_peer_cr_timer_cb,
978
0
        mp, mp->pim->msdp.connection_retry,
979
0
        &mp->cr_timer);
980
0
  }
981
0
}
982
983
/* if a valid packet is rxed from the peer we can restart hold timer */
984
void pim_msdp_peer_pkt_rxed(struct pim_msdp_peer *mp)
985
0
{
986
0
  if (mp->state == PIM_MSDP_ESTABLISHED) {
987
0
    pim_msdp_peer_hold_timer_setup(mp, true /* start */);
988
0
  }
989
0
}
990
991
/* if a valid packet is txed to the peer we can restart ka timer and avoid
992
 * unnecessary ka noise in the network */
993
void pim_msdp_peer_pkt_txed(struct pim_msdp_peer *mp)
994
0
{
995
0
  if (mp->state == PIM_MSDP_ESTABLISHED) {
996
0
    pim_msdp_peer_ka_timer_setup(mp, true /* start */);
997
0
    if (PIM_DEBUG_MSDP_INTERNAL) {
998
0
      zlog_debug("MSDP ka timer restart on pkt tx to %s",
999
0
           mp->key_str);
1000
0
    }
1001
0
  }
1002
0
}
1003
1004
static void pim_msdp_addr2su(union sockunion *su, struct in_addr addr)
1005
0
{
1006
0
  sockunion_init(su);
1007
0
  su->sin.sin_addr = addr;
1008
0
  su->sin.sin_family = AF_INET;
1009
#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
1010
  su->sin.sin_len = sizeof(struct sockaddr_in);
1011
#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
1012
0
}
1013
1014
/* 11.2.A1: create a new peer and transition state to listen or connecting */
1015
struct pim_msdp_peer *pim_msdp_peer_add(struct pim_instance *pim,
1016
          const struct in_addr *peer,
1017
          const struct in_addr *local,
1018
          const char *mesh_group_name)
1019
0
{
1020
0
  struct pim_msdp_peer *mp;
1021
1022
0
  pim_msdp_enable(pim);
1023
1024
0
  mp = XCALLOC(MTYPE_PIM_MSDP_PEER, sizeof(*mp));
1025
1026
0
  mp->pim = pim;
1027
0
  mp->peer = *peer;
1028
0
  pim_inet4_dump("<peer?>", mp->peer, mp->key_str, sizeof(mp->key_str));
1029
0
  pim_msdp_addr2su(&mp->su_peer, mp->peer);
1030
0
  mp->local = *local;
1031
  /* XXX: originator_id setting needs to move to the mesh group */
1032
0
  pim->msdp.originator_id = *local;
1033
0
  pim_msdp_addr2su(&mp->su_local, mp->local);
1034
0
  if (mesh_group_name)
1035
0
    mp->mesh_group_name =
1036
0
      XSTRDUP(MTYPE_PIM_MSDP_MG_NAME, mesh_group_name);
1037
1038
0
  mp->state = PIM_MSDP_INACTIVE;
1039
0
  mp->fd = -1;
1040
0
  strlcpy(mp->last_reset, "-", sizeof(mp->last_reset));
1041
  /* higher IP address is listener */
1042
0
  if (ntohl(mp->local.s_addr) > ntohl(mp->peer.s_addr)) {
1043
0
    mp->flags |= PIM_MSDP_PEERF_LISTENER;
1044
0
  }
1045
1046
  /* setup packet buffers */
1047
0
  mp->ibuf = stream_new(PIM_MSDP_MAX_PACKET_SIZE);
1048
0
  mp->obuf = stream_fifo_new();
1049
1050
  /* insert into misc tables for easy access */
1051
0
  mp = hash_get(pim->msdp.peer_hash, mp, hash_alloc_intern);
1052
0
  listnode_add_sort(pim->msdp.peer_list, mp);
1053
1054
0
  if (PIM_DEBUG_MSDP_EVENTS) {
1055
0
    zlog_debug("MSDP peer %s created", mp->key_str);
1056
1057
0
    pim_msdp_peer_state_chg_log(mp);
1058
0
  }
1059
1060
  /* fireup the connect state machine */
1061
0
  if (PIM_MSDP_PEER_IS_LISTENER(mp)) {
1062
0
    pim_msdp_peer_listen(mp);
1063
0
  } else {
1064
0
    pim_msdp_peer_connect(mp);
1065
0
  }
1066
0
  return mp;
1067
0
}
1068
1069
struct pim_msdp_peer *pim_msdp_peer_find(struct pim_instance *pim,
1070
           struct in_addr peer_addr)
1071
0
{
1072
0
  struct pim_msdp_peer lookup;
1073
1074
0
  lookup.peer = peer_addr;
1075
0
  return hash_lookup(pim->msdp.peer_hash, &lookup);
1076
0
}
1077
1078
/* release all mem associated with a peer */
1079
static void pim_msdp_peer_free(struct pim_msdp_peer *mp)
1080
0
{
1081
  /*
1082
   * Let's make sure we are not running when we delete
1083
   * the underlying data structure
1084
   */
1085
0
  pim_msdp_peer_stop_tcp_conn(mp, false);
1086
1087
0
  if (mp->ibuf) {
1088
0
    stream_free(mp->ibuf);
1089
0
  }
1090
1091
0
  if (mp->obuf) {
1092
0
    stream_fifo_free(mp->obuf);
1093
0
  }
1094
1095
0
  XFREE(MTYPE_PIM_MSDP_MG_NAME, mp->mesh_group_name);
1096
1097
0
  mp->pim = NULL;
1098
0
  XFREE(MTYPE_PIM_MSDP_PEER, mp);
1099
0
}
1100
1101
/* delete the peer config */
1102
void pim_msdp_peer_del(struct pim_msdp_peer **mp)
1103
0
{
1104
0
  if (*mp == NULL)
1105
0
    return;
1106
1107
  /* stop the tcp connection and shutdown all timers */
1108
0
  pim_msdp_peer_stop_tcp_conn(*mp, true /* chg_state */);
1109
1110
  /* remove the session from various tables */
1111
0
  listnode_delete((*mp)->pim->msdp.peer_list, *mp);
1112
0
  hash_release((*mp)->pim->msdp.peer_hash, *mp);
1113
1114
0
  if (PIM_DEBUG_MSDP_EVENTS) {
1115
0
    zlog_debug("MSDP peer %s deleted", (*mp)->key_str);
1116
0
  }
1117
1118
  /* free up any associated memory */
1119
0
  pim_msdp_peer_free(*mp);
1120
0
  *mp = NULL;
1121
0
}
1122
1123
void pim_msdp_peer_change_source(struct pim_msdp_peer *mp,
1124
         const struct in_addr *addr)
1125
0
{
1126
0
  pim_msdp_peer_stop_tcp_conn(mp, true);
1127
1128
0
  mp->local = *addr;
1129
1130
0
  if (PIM_MSDP_PEER_IS_LISTENER(mp))
1131
0
    pim_msdp_peer_listen(mp);
1132
0
  else
1133
0
    pim_msdp_peer_connect(mp);
1134
0
}
1135
1136
/* peer hash and peer list helpers */
1137
static unsigned int pim_msdp_peer_hash_key_make(const void *p)
1138
0
{
1139
0
  const struct pim_msdp_peer *mp = p;
1140
0
  return (jhash_1word(mp->peer.s_addr, 0));
1141
0
}
1142
1143
static bool pim_msdp_peer_hash_eq(const void *p1, const void *p2)
1144
0
{
1145
0
  const struct pim_msdp_peer *mp1 = p1;
1146
0
  const struct pim_msdp_peer *mp2 = p2;
1147
1148
0
  return (mp1->peer.s_addr == mp2->peer.s_addr);
1149
0
}
1150
1151
static int pim_msdp_peer_comp(const void *p1, const void *p2)
1152
0
{
1153
0
  const struct pim_msdp_peer *mp1 = p1;
1154
0
  const struct pim_msdp_peer *mp2 = p2;
1155
1156
0
  if (ntohl(mp1->peer.s_addr) < ntohl(mp2->peer.s_addr))
1157
0
    return -1;
1158
1159
0
  if (ntohl(mp1->peer.s_addr) > ntohl(mp2->peer.s_addr))
1160
0
    return 1;
1161
1162
0
  return 0;
1163
0
}
1164
1165
/************************** Mesh group management **************************/
1166
void pim_msdp_mg_free(struct pim_instance *pim, struct pim_msdp_mg **mgp)
1167
0
{
1168
0
  struct pim_msdp_mg_mbr *mbr;
1169
0
  struct listnode *n, *nn;
1170
1171
0
  if (*mgp == NULL)
1172
0
    return;
1173
1174
  /* SIP is being removed - tear down all active peer sessions */
1175
0
  for (ALL_LIST_ELEMENTS((*mgp)->mbr_list, n, nn, mbr))
1176
0
    pim_msdp_mg_mbr_del((*mgp), mbr);
1177
1178
0
  if (PIM_DEBUG_MSDP_EVENTS) {
1179
0
    zlog_debug("MSDP mesh-group %s deleted",
1180
0
         (*mgp)->mesh_group_name);
1181
0
  }
1182
1183
0
  XFREE(MTYPE_PIM_MSDP_MG_NAME, (*mgp)->mesh_group_name);
1184
1185
0
  if ((*mgp)->mbr_list)
1186
0
    list_delete(&(*mgp)->mbr_list);
1187
1188
0
  SLIST_REMOVE(&pim->msdp.mglist, (*mgp), pim_msdp_mg, mg_entry);
1189
0
  XFREE(MTYPE_PIM_MSDP_MG, (*mgp));
1190
0
}
1191
1192
struct pim_msdp_mg *pim_msdp_mg_new(struct pim_instance *pim,
1193
            const char *mesh_group_name)
1194
0
{
1195
0
  struct pim_msdp_mg *mg;
1196
1197
0
  mg = XCALLOC(MTYPE_PIM_MSDP_MG, sizeof(*mg));
1198
1199
0
  mg->mesh_group_name = XSTRDUP(MTYPE_PIM_MSDP_MG_NAME, mesh_group_name);
1200
0
  mg->mbr_list = list_new();
1201
0
  mg->mbr_list->del = (void (*)(void *))pim_msdp_mg_mbr_free;
1202
0
  mg->mbr_list->cmp = (int (*)(void *, void *))pim_msdp_mg_mbr_comp;
1203
1204
0
  if (PIM_DEBUG_MSDP_EVENTS) {
1205
0
    zlog_debug("MSDP mesh-group %s created", mg->mesh_group_name);
1206
0
  }
1207
1208
0
  SLIST_INSERT_HEAD(&pim->msdp.mglist, mg, mg_entry);
1209
1210
0
  return mg;
1211
0
}
1212
1213
static int pim_msdp_mg_mbr_comp(const void *p1, const void *p2)
1214
0
{
1215
0
  const struct pim_msdp_mg_mbr *mbr1 = p1;
1216
0
  const struct pim_msdp_mg_mbr *mbr2 = p2;
1217
1218
0
  if (ntohl(mbr1->mbr_ip.s_addr) < ntohl(mbr2->mbr_ip.s_addr))
1219
0
    return -1;
1220
1221
0
  if (ntohl(mbr1->mbr_ip.s_addr) > ntohl(mbr2->mbr_ip.s_addr))
1222
0
    return 1;
1223
1224
0
  return 0;
1225
0
}
1226
1227
static void pim_msdp_mg_mbr_free(struct pim_msdp_mg_mbr *mbr)
1228
0
{
1229
0
  XFREE(MTYPE_PIM_MSDP_MG_MBR, mbr);
1230
0
}
1231
1232
void pim_msdp_mg_mbr_del(struct pim_msdp_mg *mg, struct pim_msdp_mg_mbr *mbr)
1233
0
{
1234
  /* Delete active peer session if any */
1235
0
  if (mbr->mp) {
1236
0
    pim_msdp_peer_del(&mbr->mp);
1237
0
  }
1238
1239
0
  listnode_delete(mg->mbr_list, mbr);
1240
0
  if (PIM_DEBUG_MSDP_EVENTS) {
1241
0
    char ip_str[INET_ADDRSTRLEN];
1242
0
    pim_inet4_dump("<mbr?>", mbr->mbr_ip, ip_str, sizeof(ip_str));
1243
0
    zlog_debug("MSDP mesh-group %s mbr %s deleted",
1244
0
         mg->mesh_group_name, ip_str);
1245
0
  }
1246
0
  pim_msdp_mg_mbr_free(mbr);
1247
0
  if (mg->mbr_cnt) {
1248
0
    --mg->mbr_cnt;
1249
0
  }
1250
0
}
1251
1252
static void pim_msdp_src_del(struct pim_msdp_mg *mg)
1253
0
{
1254
0
  struct pim_msdp_mg_mbr *mbr;
1255
0
  struct listnode *mbr_node;
1256
1257
  /* SIP is being removed - tear down all active peer sessions */
1258
0
  for (ALL_LIST_ELEMENTS_RO(mg->mbr_list, mbr_node, mbr)) {
1259
0
    if (mbr->mp)
1260
0
      pim_msdp_peer_del(&mbr->mp);
1261
0
  }
1262
0
  if (PIM_DEBUG_MSDP_EVENTS) {
1263
0
    zlog_debug("MSDP mesh-group %s src cleared",
1264
0
         mg->mesh_group_name);
1265
0
  }
1266
0
}
1267
1268
/*********************** MSDP feature APIs *********************************/
1269
int pim_msdp_config_write(struct pim_instance *pim, struct vty *vty,
1270
        const char *spaces)
1271
0
{
1272
0
  struct pim_msdp_mg *mg;
1273
0
  struct listnode *mbrnode;
1274
0
  struct pim_msdp_mg_mbr *mbr;
1275
0
  char src_str[INET_ADDRSTRLEN];
1276
0
  int count = 0;
1277
1278
0
  if (SLIST_EMPTY(&pim->msdp.mglist))
1279
0
    return count;
1280
1281
0
  SLIST_FOREACH (mg, &pim->msdp.mglist, mg_entry) {
1282
0
    if (mg->src_ip.s_addr != INADDR_ANY) {
1283
0
      pim_inet4_dump("<src?>", mg->src_ip, src_str,
1284
0
               sizeof(src_str));
1285
0
      vty_out(vty, "%sip msdp mesh-group %s source %s\n",
1286
0
        spaces, mg->mesh_group_name, src_str);
1287
0
      ++count;
1288
0
    }
1289
1290
0
    for (ALL_LIST_ELEMENTS_RO(mg->mbr_list, mbrnode, mbr)) {
1291
0
      vty_out(vty, "%sip msdp mesh-group %s member %pI4\n",
1292
0
        spaces, mg->mesh_group_name, &mbr->mbr_ip);
1293
0
      ++count;
1294
0
    }
1295
0
  }
1296
1297
0
  return count;
1298
0
}
1299
1300
bool pim_msdp_peer_config_write(struct vty *vty, struct pim_instance *pim,
1301
        const char *spaces)
1302
0
{
1303
0
  struct pim_msdp_peer *mp;
1304
0
  struct listnode *node;
1305
0
  bool written = false;
1306
1307
0
  for (ALL_LIST_ELEMENTS_RO(pim->msdp.peer_list, node, mp)) {
1308
    /* Skip meshed group peers. */
1309
0
    if (mp->flags & PIM_MSDP_PEERF_IN_GROUP)
1310
0
      continue;
1311
1312
0
    vty_out(vty, "%sip msdp peer %pI4 source %pI4\n", spaces,
1313
0
      &mp->peer, &mp->local);
1314
0
    written = true;
1315
0
  }
1316
1317
0
  return written;
1318
0
}
1319
1320
/* Enable feature including active/periodic timers etc. on the first peer
1321
 * config. Till then MSDP should just stay quiet. */
1322
static void pim_msdp_enable(struct pim_instance *pim)
1323
0
{
1324
0
  if (pim->msdp.flags & PIM_MSDPF_ENABLE) {
1325
    /* feature is already enabled */
1326
0
    return;
1327
0
  }
1328
0
  pim->msdp.flags |= PIM_MSDPF_ENABLE;
1329
0
  pim->msdp.work_obuf = stream_new(PIM_MSDP_MAX_PACKET_SIZE);
1330
0
  pim_msdp_sa_adv_timer_setup(pim, true /* start */);
1331
  /* setup sa cache based on local sources */
1332
0
  pim_msdp_sa_local_setup(pim);
1333
0
}
1334
1335
/* MSDP init */
1336
void pim_msdp_init(struct pim_instance *pim, struct event_loop *master)
1337
1
{
1338
1
  pim->msdp.master = master;
1339
1
  char hash_name[64];
1340
1341
1
  snprintf(hash_name, sizeof(hash_name), "PIM %s MSDP Peer Hash",
1342
1
     pim->vrf->name);
1343
1
  pim->msdp.peer_hash = hash_create(pim_msdp_peer_hash_key_make,
1344
1
            pim_msdp_peer_hash_eq, hash_name);
1345
1
  pim->msdp.peer_list = list_new();
1346
1
  pim->msdp.peer_list->del = (void (*)(void *))pim_msdp_peer_free;
1347
1
  pim->msdp.peer_list->cmp = (int (*)(void *, void *))pim_msdp_peer_comp;
1348
1349
1
  snprintf(hash_name, sizeof(hash_name), "PIM %s MSDP SA Hash",
1350
1
     pim->vrf->name);
1351
1
  pim->msdp.sa_hash = hash_create(pim_msdp_sa_hash_key_make,
1352
1
          pim_msdp_sa_hash_eq, hash_name);
1353
1
  pim->msdp.sa_list = list_new();
1354
1
  pim->msdp.sa_list->del = (void (*)(void *))pim_msdp_sa_free;
1355
1
  pim->msdp.sa_list->cmp = (int (*)(void *, void *))pim_msdp_sa_comp;
1356
1
}
1357
1358
/* counterpart to MSDP init; XXX: unused currently */
1359
void pim_msdp_exit(struct pim_instance *pim)
1360
0
{
1361
0
  struct pim_msdp_mg *mg;
1362
1363
0
  pim_msdp_sa_adv_timer_setup(pim, false);
1364
1365
  /* Stop listener and delete all peer sessions */
1366
0
  while ((mg = SLIST_FIRST(&pim->msdp.mglist)) != NULL)
1367
0
    pim_msdp_mg_free(pim, &mg);
1368
1369
0
  hash_clean_and_free(&pim->msdp.peer_hash, NULL);
1370
1371
0
  if (pim->msdp.peer_list) {
1372
0
    list_delete(&pim->msdp.peer_list);
1373
0
  }
1374
1375
0
  hash_clean_and_free(&pim->msdp.sa_hash, NULL);
1376
1377
0
  if (pim->msdp.sa_list) {
1378
0
    list_delete(&pim->msdp.sa_list);
1379
0
  }
1380
1381
0
  if (pim->msdp.work_obuf)
1382
0
    stream_free(pim->msdp.work_obuf);
1383
0
  pim->msdp.work_obuf = NULL;
1384
0
}
1385
1386
void pim_msdp_mg_src_add(struct pim_instance *pim, struct pim_msdp_mg *mg,
1387
       struct in_addr *ai)
1388
0
{
1389
0
  struct pim_msdp_mg_mbr *mbr;
1390
0
  struct listnode *mbr_node;
1391
1392
  /* Stop all connections and remove data structures. */
1393
0
  pim_msdp_src_del(mg);
1394
1395
  /* Set new address. */
1396
0
  mg->src_ip = *ai;
1397
1398
  /* No new address, disable everyone. */
1399
0
  if (ai->s_addr == INADDR_ANY) {
1400
0
    if (PIM_DEBUG_MSDP_EVENTS)
1401
0
      zlog_debug("MSDP mesh-group %s src unset",
1402
0
           mg->mesh_group_name);
1403
0
    return;
1404
0
  }
1405
1406
  /* Create data structures and start TCP connection. */
1407
0
  for (ALL_LIST_ELEMENTS_RO(mg->mbr_list, mbr_node, mbr))
1408
0
    mbr->mp = pim_msdp_peer_add(pim, &mbr->mbr_ip, &mg->src_ip,
1409
0
              mg->mesh_group_name);
1410
1411
0
  if (PIM_DEBUG_MSDP_EVENTS)
1412
0
    zlog_debug("MSDP mesh-group %s src %pI4 set",
1413
0
         mg->mesh_group_name, &mg->src_ip);
1414
0
}
1415
1416
struct pim_msdp_mg_mbr *pim_msdp_mg_mbr_add(struct pim_instance *pim,
1417
              struct pim_msdp_mg *mg,
1418
              struct in_addr *ia)
1419
0
{
1420
0
  struct pim_msdp_mg_mbr *mbr;
1421
1422
0
  mbr = XCALLOC(MTYPE_PIM_MSDP_MG_MBR, sizeof(*mbr));
1423
0
  mbr->mbr_ip = *ia;
1424
0
  listnode_add_sort(mg->mbr_list, mbr);
1425
1426
  /* if valid SIP has been configured add peer session */
1427
0
  if (mg->src_ip.s_addr != INADDR_ANY)
1428
0
    mbr->mp = pim_msdp_peer_add(pim, &mbr->mbr_ip, &mg->src_ip,
1429
0
              mg->mesh_group_name);
1430
1431
0
  if (PIM_DEBUG_MSDP_EVENTS)
1432
0
    zlog_debug("MSDP mesh-group %s mbr %pI4 created",
1433
0
         mg->mesh_group_name, &mbr->mbr_ip);
1434
1435
0
  ++mg->mbr_cnt;
1436
1437
0
  return mbr;
1438
0
}