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