]> git.proxmox.com Git - mirror_frr.git/blame - pimd/pim_msdp.c
Merge pull request #7290 from eololab/small-fix-for-frrcommon-in-tools
[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>
3613d898 31#include <lib/lib_errors.h>
11128587 32
2a333e0f 33#include "pimd.h"
34#include "pim_cmd.h"
35#include "pim_memory.h"
7176984f 36#include "pim_iface.h"
3c72d654 37#include "pim_rp.h"
2a333e0f 38#include "pim_str.h"
39#include "pim_time.h"
1bf16443 40#include "pim_upstream.h"
472ad383 41#include "pim_oil.h"
11128587 42
2a333e0f 43#include "pim_msdp.h"
44#include "pim_msdp_packet.h"
45#include "pim_msdp_socket.h"
46
2ad78035 47// struct pim_msdp pim_msdp, *msdp = &pim_msdp;
2a333e0f 48
49static void pim_msdp_peer_listen(struct pim_msdp_peer *mp);
50static void pim_msdp_peer_cr_timer_setup(struct pim_msdp_peer *mp, bool start);
51static void pim_msdp_peer_ka_timer_setup(struct pim_msdp_peer *mp, bool start);
d62a17ae 52static void pim_msdp_peer_hold_timer_setup(struct pim_msdp_peer *mp,
53 bool start);
2a333e0f 54static void pim_msdp_peer_free(struct pim_msdp_peer *mp);
472ad383
DS
55static void pim_msdp_enable(struct pim_instance *pim);
56static void pim_msdp_sa_adv_timer_setup(struct pim_instance *pim, bool start);
d62a17ae 57static void pim_msdp_sa_deref(struct pim_msdp_sa *sa,
58 enum pim_msdp_sa_flags flags);
977d71cc 59static int pim_msdp_mg_mbr_comp(const void *p1, const void *p2);
60static void pim_msdp_mg_mbr_free(struct pim_msdp_mg_mbr *mbr);
d62a17ae 61static void pim_msdp_mg_mbr_do_del(struct pim_msdp_mg *mg,
62 struct pim_msdp_mg_mbr *mbr);
2a333e0f 63
3c72d654 64/************************ SA cache management ******************************/
d62a17ae 65static void pim_msdp_sa_timer_expiry_log(struct pim_msdp_sa *sa,
66 const char *timer_str)
3c72d654 67{
d62a17ae 68 zlog_debug("MSDP SA %s %s timer expired", sa->sg_str, timer_str);
3c72d654 69}
70
71/* RFC-3618:Sec-5.1 - global active source advertisement timer */
d62a17ae 72static int pim_msdp_sa_adv_timer_cb(struct thread *t)
3c72d654 73{
472ad383
DS
74 struct pim_instance *pim = THREAD_ARG(t);
75
d62a17ae 76 if (PIM_DEBUG_MSDP_EVENTS) {
0437e105 77 zlog_debug("MSDP SA advertisement timer expired");
d62a17ae 78 }
3c72d654 79
472ad383
DS
80 pim_msdp_sa_adv_timer_setup(pim, true /* start */);
81 pim_msdp_pkt_sa_tx(pim);
d62a17ae 82 return 0;
3c72d654 83}
472ad383 84static void pim_msdp_sa_adv_timer_setup(struct pim_instance *pim, bool start)
3c72d654 85{
472ad383 86 THREAD_OFF(pim->msdp.sa_adv_timer);
d62a17ae 87 if (start) {
472ad383
DS
88 thread_add_timer(pim->msdp.master, pim_msdp_sa_adv_timer_cb,
89 pim, PIM_MSDP_SA_ADVERTISMENT_TIME,
90 &pim->msdp.sa_adv_timer);
d62a17ae 91 }
3c72d654 92}
93
94/* RFC-3618:Sec-5.3 - SA cache state timer */
d62a17ae 95static int pim_msdp_sa_state_timer_cb(struct thread *t)
3c72d654 96{
d62a17ae 97 struct pim_msdp_sa *sa;
3c72d654 98
d62a17ae 99 sa = THREAD_ARG(t);
3c72d654 100
d62a17ae 101 if (PIM_DEBUG_MSDP_EVENTS) {
102 pim_msdp_sa_timer_expiry_log(sa, "state");
103 }
3c72d654 104
d62a17ae 105 pim_msdp_sa_deref(sa, PIM_MSDP_SAF_PEER);
106 return 0;
3c72d654 107}
d62a17ae 108static void pim_msdp_sa_state_timer_setup(struct pim_msdp_sa *sa, bool start)
3c72d654 109{
d62a17ae 110 THREAD_OFF(sa->sa_state_timer);
111 if (start) {
472ad383
DS
112 thread_add_timer(sa->pim->msdp.master,
113 pim_msdp_sa_state_timer_cb, sa,
114 PIM_MSDP_SA_HOLD_TIME, &sa->sa_state_timer);
d62a17ae 115 }
3c72d654 116}
117
d62a17ae 118static void pim_msdp_sa_upstream_del(struct pim_msdp_sa *sa)
7667c556 119{
d62a17ae 120 struct pim_upstream *up = sa->up;
121 if (!up) {
122 return;
123 }
7667c556 124
d62a17ae 125 sa->up = NULL;
126 if (PIM_UPSTREAM_FLAG_TEST_SRC_MSDP(up->flags)) {
127 PIM_UPSTREAM_FLAG_UNSET_SRC_MSDP(up->flags);
128 sa->flags |= PIM_MSDP_SAF_UP_DEL_IN_PROG;
15569c58 129 up = pim_upstream_del(sa->pim, up, __func__);
41a115e4
AK
130 /* re-eval joinDesired; clearing peer-msdp-sa flag can
131 * cause JD to change
132 */
133 if (up)
134 pim_upstream_update_join_desired(sa->pim, up);
d62a17ae 135 sa->flags &= ~PIM_MSDP_SAF_UP_DEL_IN_PROG;
136 }
7667c556 137
d62a17ae 138 if (PIM_DEBUG_MSDP_EVENTS) {
139 zlog_debug("MSDP SA %s de-referenced SPT", sa->sg_str);
140 }
7667c556 141}
142
d62a17ae 143static bool pim_msdp_sa_upstream_add_ok(struct pim_msdp_sa *sa,
144 struct pim_upstream *xg_up)
7667c556 145{
d62a17ae 146 if (!(sa->flags & PIM_MSDP_SAF_PEER)) {
147 /* SA should have been rxed from a peer */
148 return false;
149 }
150 /* check if we are RP */
472ad383 151 if (!I_am_RP(sa->pim, sa->sg.grp)) {
d62a17ae 152 return false;
153 }
7667c556 154
d62a17ae 155 /* check if we have a (*, G) with a non-empty immediate OIL */
156 if (!xg_up) {
157 struct prefix_sg sg;
7667c556 158
d62a17ae 159 memset(&sg, 0, sizeof(sg));
160 sg.grp = sa->sg.grp;
7667c556 161
472ad383 162 xg_up = pim_upstream_find(sa->pim, &sg);
d62a17ae 163 }
164 if (!xg_up || (xg_up->join_state != PIM_UPSTREAM_JOINED)) {
165 /* join desired will be true for such (*, G) entries so we will
166 * just look at join_state and let the PIM state machine do the
167 * rest of
168 * the magic */
169 return false;
170 }
7667c556 171
d62a17ae 172 return true;
7667c556 173}
174
175/* Upstream add evaluation needs to happen everytime -
176 * 1. Peer reference is added or removed.
1bf16443 177 * 2. The RP for a group changes.
178 * 3. joinDesired for the associated (*, G) changes
179 * 4. associated (*, G) is removed - this seems like a bit redundant
7667c556 180 * (considering #4); but just in case an entry gets nuked without
181 * upstream state transition
182 * */
d62a17ae 183static void pim_msdp_sa_upstream_update(struct pim_msdp_sa *sa,
184 struct pim_upstream *xg_up,
185 const char *ctx)
186{
187 struct pim_upstream *up;
188
189 if (!pim_msdp_sa_upstream_add_ok(sa, xg_up)) {
190 pim_msdp_sa_upstream_del(sa);
191 return;
192 }
193
194 if (sa->up) {
195 /* nothing to do */
196 return;
197 }
198
472ad383 199 up = pim_upstream_find(sa->pim, &sa->sg);
d62a17ae 200 if (up && (PIM_UPSTREAM_FLAG_TEST_SRC_MSDP(up->flags))) {
201 /* somehow we lost track of the upstream ptr? best log it */
202 sa->up = up;
203 if (PIM_DEBUG_MSDP_EVENTS) {
204 zlog_debug("MSDP SA %s SPT reference missing",
205 sa->sg_str);
206 }
207 return;
208 }
209
210 /* RFC3618: "RP triggers a (S, G) join event towards the data source
211 * as if a JP message was rxed addressed to the RP itself." */
2002dcdb 212 up = pim_upstream_add(sa->pim, &sa->sg, NULL /* iif */,
15569c58 213 PIM_UPSTREAM_FLAG_MASK_SRC_MSDP, __func__, NULL);
d62a17ae 214
215 sa->up = up;
216 if (up) {
217 /* update inherited oil */
472ad383 218 pim_upstream_inherited_olist(sa->pim, up);
d62a17ae 219 /* should we also start the kat in parallel? we will need it
220 * when the
221 * SA ages out */
222 if (PIM_DEBUG_MSDP_EVENTS) {
223 zlog_debug("MSDP SA %s referenced SPT", sa->sg_str);
224 }
225 } else {
226 if (PIM_DEBUG_MSDP_EVENTS) {
227 zlog_debug("MSDP SA %s SPT reference failed",
228 sa->sg_str);
229 }
230 }
7667c556 231}
232
3c72d654 233/* release all mem associated with a sa */
d62a17ae 234static void pim_msdp_sa_free(struct pim_msdp_sa *sa)
3c72d654 235{
ec2f0e53
DS
236 pim_msdp_sa_state_timer_setup(sa, false);
237
d62a17ae 238 XFREE(MTYPE_PIM_MSDP_SA, sa);
3c72d654 239}
240
472ad383
DS
241static struct pim_msdp_sa *pim_msdp_sa_new(struct pim_instance *pim,
242 struct prefix_sg *sg,
d62a17ae 243 struct in_addr rp)
3c72d654 244{
d62a17ae 245 struct pim_msdp_sa *sa;
3c72d654 246
d62a17ae 247 sa = XCALLOC(MTYPE_PIM_MSDP_SA, sizeof(*sa));
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 */
9fbd9fc4 426 sa->rp = pim->msdp.originator_id;
d62a17ae 427 pim_msdp_pkt_sa_tx_one(sa);
428 }
429 sa->flags &= ~PIM_MSDP_SAF_STALE;
430 }
3c72d654 431}
432
1bf16443 433/* The following criteria must be met to originate an SA from the MSDP
434 * speaker -
435 * 1. KAT must be running i.e. source is active.
436 * 2. We must be RP for the group.
437 * 3. Source must be registrable to the RP (this is where the RFC is vague
438 * and especially ambiguous in CLOS networks; with anycast RP all sources
439 * are potentially registrable to all RPs in the domain). We assume #3 is
440 * satisfied if -
441 * a. We are also the FHR-DR for the source (OR)
442 * b. We rxed a pim register (null or data encapsulated) within the last
443 * (3 * (1.5 * register_suppression_timer))).
444 */
d62a17ae 445static bool pim_msdp_sa_local_add_ok(struct pim_upstream *up)
3c72d654 446{
472ad383
DS
447 struct pim_instance *pim = up->channel_oil->pim;
448
449 if (!(pim->msdp.flags & PIM_MSDPF_ENABLE)) {
d62a17ae 450 return false;
451 }
3c72d654 452
ec836533 453 if (!pim_upstream_is_kat_running(up))
d62a17ae 454 /* stream is not active */
455 return false;
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;
975a328e 476 rp.s_addr = INADDR_ANY;
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 */
af4c2728 525 flog_err(
1c50c1c0 526 EC_LIB_DEVELOPMENT,
d62a17ae 527 "MSDP sa %s SPT teardown is causing the local entry to be removed",
528 sa->sg_str);
529 return;
530 }
531
532 /* we are dropping the sa on upstream del we should not have an
533 * upstream reference */
534 if (sa->up) {
535 if (PIM_DEBUG_MSDP_INTERNAL) {
536 zlog_debug("MSDP local sa %s del; up non-NULL",
537 sa->sg_str);
538 }
539 sa->up = NULL;
540 }
541 pim_msdp_sa_deref(sa, PIM_MSDP_SAF_LOCAL);
542 }
36e466fe 543}
544
1bf16443 545/* Local SA qualification needs to be re-evaluated when -
546 * 1. KAT is started or stopped
547 * 2. on RP changes
548 * 3. Whenever FHR status changes for a (S,G) - XXX - currently there
549 * is no clear path to transition an entry out of "MASK_FHR" need
550 * to discuss this with Donald. May result in some strangeness if the
551 * FHR is also the RP.
552 * 4. When msdp_reg timer is started or stopped
553 */
d62a17ae 554void pim_msdp_sa_local_update(struct pim_upstream *up)
1bf16443 555{
472ad383
DS
556 struct pim_instance *pim = up->channel_oil->pim;
557
d62a17ae 558 if (pim_msdp_sa_local_add_ok(up)) {
472ad383 559 pim_msdp_sa_local_add(pim, &up->sg);
d62a17ae 560 } else {
472ad383 561 pim_msdp_sa_local_del(pim, &up->sg);
d62a17ae 562 }
1bf16443 563}
564
472ad383 565static void pim_msdp_sa_local_setup(struct pim_instance *pim)
3c72d654 566{
d62a17ae 567 struct pim_upstream *up;
3c72d654 568
dd3364cb 569 frr_each (rb_pim_upstream, &pim->upstream_head, up)
d62a17ae 570 pim_msdp_sa_local_update(up);
3c72d654 571}
572
7667c556 573/* whenever the RP changes we need to re-evaluate the "local" SA-cache */
574/* XXX: needs to be tested */
472ad383 575void pim_msdp_i_am_rp_changed(struct pim_instance *pim)
d62a17ae 576{
577 struct listnode *sanode;
578 struct listnode *nextnode;
579 struct pim_msdp_sa *sa;
580
472ad383 581 if (!(pim->msdp.flags & PIM_MSDPF_ENABLE)) {
d62a17ae 582 /* if the feature is not enabled do nothing */
583 return;
584 }
585
586 if (PIM_DEBUG_MSDP_INTERNAL) {
587 zlog_debug("MSDP i_am_rp changed");
588 }
589
590 /* mark all local entries as stale */
472ad383 591 for (ALL_LIST_ELEMENTS_RO(pim->msdp.sa_list, sanode, sa)) {
d62a17ae 592 if (sa->flags & PIM_MSDP_SAF_LOCAL) {
593 sa->flags |= PIM_MSDP_SAF_STALE;
594 }
595 }
596
597 /* re-setup local SA entries */
472ad383 598 pim_msdp_sa_local_setup(pim);
d62a17ae 599
472ad383 600 for (ALL_LIST_ELEMENTS(pim->msdp.sa_list, sanode, nextnode, sa)) {
d62a17ae 601 /* purge stale SA entries */
602 if (sa->flags & PIM_MSDP_SAF_STALE) {
603 /* clear the stale flag; the entry may be kept even
604 * after
605 * "local-deref" */
606 sa->flags &= ~PIM_MSDP_SAF_STALE;
607 /* sa_deref can end up freeing the sa; so don't access
608 * contents after */
609 pim_msdp_sa_deref(sa, PIM_MSDP_SAF_LOCAL);
610 } else {
611 /* if the souce is still active check if we can
612 * influence SPT */
613 pim_msdp_sa_upstream_update(sa, NULL /* xg_up */,
614 "rp-change");
615 }
616 }
7667c556 617}
618
619/* We track the join state of (*, G) entries. If G has sources in the SA-cache
620 * we need to setup or teardown SPT when the JoinDesired status changes for
621 * (*, G) */
1eca8576
DS
622void pim_msdp_up_join_state_changed(struct pim_instance *pim,
623 struct pim_upstream *xg_up)
d62a17ae 624{
625 struct listnode *sanode;
626 struct pim_msdp_sa *sa;
627
628 if (PIM_DEBUG_MSDP_INTERNAL) {
629 zlog_debug("MSDP join state changed for %s", xg_up->sg_str);
630 }
631
632 /* If this is not really an XG entry just move on */
633 if ((xg_up->sg.src.s_addr != INADDR_ANY)
634 || (xg_up->sg.grp.s_addr == INADDR_ANY)) {
635 return;
636 }
637
638 /* XXX: Need to maintain SAs per-group to avoid all this unnecessary
639 * walking */
472ad383 640 for (ALL_LIST_ELEMENTS_RO(pim->msdp.sa_list, sanode, sa)) {
d62a17ae 641 if (sa->sg.grp.s_addr != xg_up->sg.grp.s_addr) {
642 continue;
643 }
644 pim_msdp_sa_upstream_update(sa, xg_up, "up-jp-change");
645 }
646}
647
472ad383 648static void pim_msdp_up_xg_del(struct pim_instance *pim, struct prefix_sg *sg)
d62a17ae 649{
650 struct listnode *sanode;
651 struct pim_msdp_sa *sa;
652
653 if (PIM_DEBUG_MSDP_INTERNAL) {
654 zlog_debug("MSDP %s del", pim_str_sg_dump(sg));
655 }
656
657 /* If this is not really an XG entry just move on */
658 if ((sg->src.s_addr != INADDR_ANY) || (sg->grp.s_addr == INADDR_ANY)) {
659 return;
660 }
661
662 /* XXX: Need to maintain SAs per-group to avoid all this unnecessary
663 * walking */
472ad383 664 for (ALL_LIST_ELEMENTS_RO(pim->msdp.sa_list, sanode, sa)) {
d62a17ae 665 if (sa->sg.grp.s_addr != sg->grp.s_addr) {
666 continue;
667 }
668 pim_msdp_sa_upstream_update(sa, NULL /* xg */, "up-jp-change");
669 }
670}
671
472ad383 672void pim_msdp_up_del(struct pim_instance *pim, struct prefix_sg *sg)
d62a17ae 673{
674 if (PIM_DEBUG_MSDP_INTERNAL) {
675 zlog_debug("MSDP up %s del", pim_str_sg_dump(sg));
676 }
677 if (sg->src.s_addr == INADDR_ANY) {
472ad383 678 pim_msdp_up_xg_del(pim, sg);
d62a17ae 679 } else {
472ad383 680 pim_msdp_sa_local_del_on_up_del(pim, sg);
d62a17ae 681 }
36e466fe 682}
683
3c72d654 684/* sa hash and peer list helpers */
d8b87afe 685static unsigned int pim_msdp_sa_hash_key_make(const void *p)
3c72d654 686{
d8b87afe 687 const struct pim_msdp_sa *sa = p;
3c72d654 688
d62a17ae 689 return (jhash_2words(sa->sg.src.s_addr, sa->sg.grp.s_addr, 0));
3c72d654 690}
691
74df8d6d 692static bool pim_msdp_sa_hash_eq(const void *p1, const void *p2)
3c72d654 693{
d62a17ae 694 const struct pim_msdp_sa *sa1 = p1;
695 const struct pim_msdp_sa *sa2 = p2;
3c72d654 696
d62a17ae 697 return ((sa1->sg.src.s_addr == sa2->sg.src.s_addr)
698 && (sa1->sg.grp.s_addr == sa2->sg.grp.s_addr));
3c72d654 699}
700
d62a17ae 701static int pim_msdp_sa_comp(const void *p1, const void *p2)
3c72d654 702{
d62a17ae 703 const struct pim_msdp_sa *sa1 = p1;
704 const struct pim_msdp_sa *sa2 = p2;
3c72d654 705
d62a17ae 706 if (ntohl(sa1->sg.grp.s_addr) < ntohl(sa2->sg.grp.s_addr))
707 return -1;
3c72d654 708
d62a17ae 709 if (ntohl(sa1->sg.grp.s_addr) > ntohl(sa2->sg.grp.s_addr))
710 return 1;
3c72d654 711
d62a17ae 712 if (ntohl(sa1->sg.src.s_addr) < ntohl(sa2->sg.src.s_addr))
713 return -1;
3c72d654 714
d62a17ae 715 if (ntohl(sa1->sg.src.s_addr) > ntohl(sa2->sg.src.s_addr))
716 return 1;
3c72d654 717
d62a17ae 718 return 0;
3c72d654 719}
720
721/* RFC-3618:Sec-10.1.3 - Peer-RPF forwarding */
722/* XXX: this can use a bit of refining and extensions */
d62a17ae 723bool pim_msdp_peer_rpf_check(struct pim_msdp_peer *mp, struct in_addr rp)
3c72d654 724{
9fbd9fc4
AMR
725 struct pim_nexthop nexthop;
726
d62a17ae 727 if (mp->peer.s_addr == rp.s_addr) {
728 return true;
729 }
3c72d654 730
9fbd9fc4
AMR
731 /* check if the MSDP peer is the nexthop for the RP */
732 if (pim_nexthop_lookup(mp->pim, &nexthop, rp, 0)
733 && nexthop.mrib_nexthop_addr.u.prefix4.s_addr == mp->peer.s_addr) {
734 return true;
735 }
736
d62a17ae 737 return false;
3c72d654 738}
d62a17ae 739
3c72d654 740/************************ Peer session management **************************/
d62a17ae 741char *pim_msdp_state_dump(enum pim_msdp_peer_state state, char *buf,
742 int buf_size)
743{
744 switch (state) {
745 case PIM_MSDP_DISABLED:
746 snprintf(buf, buf_size, "%s", "disabled");
747 break;
748 case PIM_MSDP_INACTIVE:
749 snprintf(buf, buf_size, "%s", "inactive");
750 break;
751 case PIM_MSDP_LISTEN:
752 snprintf(buf, buf_size, "%s", "listen");
753 break;
754 case PIM_MSDP_CONNECTING:
755 snprintf(buf, buf_size, "%s", "connecting");
756 break;
757 case PIM_MSDP_ESTABLISHED:
758 snprintf(buf, buf_size, "%s", "established");
759 break;
760 default:
761 snprintf(buf, buf_size, "unk-%d", state);
762 }
763 return buf;
764}
765
766char *pim_msdp_peer_key_dump(struct pim_msdp_peer *mp, char *buf, int buf_size,
767 bool long_format)
768{
769 char peer_str[INET_ADDRSTRLEN];
770 char local_str[INET_ADDRSTRLEN];
771
772 pim_inet4_dump("<peer?>", mp->peer, peer_str, sizeof(peer_str));
773 if (long_format) {
774 pim_inet4_dump("<local?>", mp->local, local_str,
775 sizeof(local_str));
776 snprintf(buf, buf_size, "MSDP peer %s local %s mg %s", peer_str,
777 local_str, mp->mesh_group_name);
778 } else {
779 snprintf(buf, buf_size, "MSDP peer %s", peer_str);
780 }
781
782 return buf;
783}
784
785static void pim_msdp_peer_state_chg_log(struct pim_msdp_peer *mp)
786{
787 char state_str[PIM_MSDP_STATE_STRLEN];
788
789 pim_msdp_state_dump(mp->state, state_str, sizeof(state_str));
790 zlog_debug("MSDP peer %s state chg to %s", mp->key_str, state_str);
2a333e0f 791}
792
793/* MSDP Connection State Machine actions (defined in RFC-3618:Sec-11.2) */
794/* 11.2.A2: active peer - start connect retry timer; when the timer fires
795 * a tcp connection will be made */
d62a17ae 796static void pim_msdp_peer_connect(struct pim_msdp_peer *mp)
2a333e0f 797{
d62a17ae 798 mp->state = PIM_MSDP_CONNECTING;
799 if (PIM_DEBUG_MSDP_EVENTS) {
800 pim_msdp_peer_state_chg_log(mp);
801 }
2a333e0f 802
d62a17ae 803 pim_msdp_peer_cr_timer_setup(mp, true /* start */);
2a333e0f 804}
805
806/* 11.2.A3: passive peer - just listen for connections */
d62a17ae 807static void pim_msdp_peer_listen(struct pim_msdp_peer *mp)
2a333e0f 808{
d62a17ae 809 mp->state = PIM_MSDP_LISTEN;
810 if (PIM_DEBUG_MSDP_EVENTS) {
811 pim_msdp_peer_state_chg_log(mp);
812 }
2a333e0f 813
d62a17ae 814 /* this is interntionally asymmetric i.e. we set up listen-socket when
9d303b37
DL
815 * the
816 * first listening peer is configured; but don't bother tearing it down
817 * when
818 * all the peers go down */
472ad383 819 pim_msdp_sock_listen(mp->pim);
2a333e0f 820}
821
822/* 11.2.A4 and 11.2.A5: transition active or passive peer to
823 * established state */
d62a17ae 824void pim_msdp_peer_established(struct pim_msdp_peer *mp)
2a333e0f 825{
d62a17ae 826 if (mp->state != PIM_MSDP_ESTABLISHED) {
827 ++mp->est_flaps;
828 }
977d71cc 829
d62a17ae 830 mp->state = PIM_MSDP_ESTABLISHED;
831 mp->uptime = pim_time_monotonic_sec();
2a333e0f 832
d62a17ae 833 if (PIM_DEBUG_MSDP_EVENTS) {
834 pim_msdp_peer_state_chg_log(mp);
835 }
2a333e0f 836
d62a17ae 837 /* stop retry timer on active peers */
838 pim_msdp_peer_cr_timer_setup(mp, false /* start */);
2a333e0f 839
d62a17ae 840 /* send KA; start KA and hold timers */
841 pim_msdp_pkt_ka_tx(mp);
842 pim_msdp_peer_ka_timer_setup(mp, true /* start */);
843 pim_msdp_peer_hold_timer_setup(mp, true /* start */);
2a333e0f 844
d62a17ae 845 pim_msdp_pkt_sa_tx_to_one_peer(mp);
3c72d654 846
d62a17ae 847 PIM_MSDP_PEER_WRITE_ON(mp);
848 PIM_MSDP_PEER_READ_ON(mp);
2a333e0f 849}
850
851/* 11.2.A6, 11.2.A7 and 11.2.A8: shutdown the peer tcp connection */
d62a17ae 852void pim_msdp_peer_stop_tcp_conn(struct pim_msdp_peer *mp, bool chg_state)
853{
854 if (chg_state) {
855 if (mp->state == PIM_MSDP_ESTABLISHED) {
856 ++mp->est_flaps;
857 }
858 mp->state = PIM_MSDP_INACTIVE;
859 if (PIM_DEBUG_MSDP_EVENTS) {
860 pim_msdp_peer_state_chg_log(mp);
861 }
862 }
863
864 if (PIM_DEBUG_MSDP_INTERNAL) {
865 zlog_debug("MSDP peer %s pim_msdp_peer_stop_tcp_conn",
866 mp->key_str);
867 }
868 /* stop read and write threads */
869 PIM_MSDP_PEER_READ_OFF(mp);
870 PIM_MSDP_PEER_WRITE_OFF(mp);
871
872 /* reset buffers */
873 mp->packet_size = 0;
874 if (mp->ibuf)
875 stream_reset(mp->ibuf);
876 if (mp->obuf)
877 stream_fifo_clean(mp->obuf);
878
879 /* stop all peer timers */
880 pim_msdp_peer_ka_timer_setup(mp, false /* start */);
881 pim_msdp_peer_cr_timer_setup(mp, false /* start */);
882 pim_msdp_peer_hold_timer_setup(mp, false /* start */);
883
884 /* close connection */
885 if (mp->fd >= 0) {
886 close(mp->fd);
887 mp->fd = -1;
888 }
2a333e0f 889}
890
891/* RFC-3618:Sec-5.6 - stop the peer tcp connection and startover */
d62a17ae 892void pim_msdp_peer_reset_tcp_conn(struct pim_msdp_peer *mp, const char *rc_str)
2a333e0f 893{
d62a17ae 894 if (PIM_DEBUG_EVENTS) {
895 zlog_debug("MSDP peer %s tcp reset %s", mp->key_str, rc_str);
896 snprintf(mp->last_reset, sizeof(mp->last_reset), "%s", rc_str);
897 }
2a333e0f 898
d62a17ae 899 /* close the connection and transition to listening or connecting */
900 pim_msdp_peer_stop_tcp_conn(mp, true /* chg_state */);
901 if (PIM_MSDP_PEER_IS_LISTENER(mp)) {
902 pim_msdp_peer_listen(mp);
903 } else {
904 pim_msdp_peer_connect(mp);
905 }
2a333e0f 906}
907
d62a17ae 908static void pim_msdp_peer_timer_expiry_log(struct pim_msdp_peer *mp,
909 const char *timer_str)
2a333e0f 910{
d62a17ae 911 zlog_debug("MSDP peer %s %s timer expired", mp->key_str, timer_str);
2a333e0f 912}
913
914/* RFC-3618:Sec-5.4 - peer hold timer */
d62a17ae 915static int pim_msdp_peer_hold_timer_cb(struct thread *t)
2a333e0f 916{
d62a17ae 917 struct pim_msdp_peer *mp;
2a333e0f 918
d62a17ae 919 mp = THREAD_ARG(t);
2a333e0f 920
d62a17ae 921 if (PIM_DEBUG_MSDP_EVENTS) {
922 pim_msdp_peer_timer_expiry_log(mp, "hold");
923 }
2a333e0f 924
d62a17ae 925 if (mp->state != PIM_MSDP_ESTABLISHED) {
926 return 0;
927 }
2a333e0f 928
d62a17ae 929 if (PIM_DEBUG_MSDP_EVENTS) {
930 pim_msdp_peer_state_chg_log(mp);
931 }
932 pim_msdp_peer_reset_tcp_conn(mp, "ht-expired");
933 return 0;
2a333e0f 934}
472ad383 935
d62a17ae 936static void pim_msdp_peer_hold_timer_setup(struct pim_msdp_peer *mp, bool start)
2a333e0f 937{
472ad383 938 struct pim_instance *pim = mp->pim;
d62a17ae 939 THREAD_OFF(mp->hold_timer);
940 if (start) {
472ad383 941 thread_add_timer(pim->msdp.master, pim_msdp_peer_hold_timer_cb,
2ad78035 942 mp, PIM_MSDP_PEER_HOLD_TIME, &mp->hold_timer);
d62a17ae 943 }
2a333e0f 944}
945
946
947/* RFC-3618:Sec-5.5 - peer keepalive timer */
d62a17ae 948static int pim_msdp_peer_ka_timer_cb(struct thread *t)
2a333e0f 949{
d62a17ae 950 struct pim_msdp_peer *mp;
2a333e0f 951
d62a17ae 952 mp = THREAD_ARG(t);
2a333e0f 953
d62a17ae 954 if (PIM_DEBUG_MSDP_EVENTS) {
955 pim_msdp_peer_timer_expiry_log(mp, "ka");
956 }
2a333e0f 957
d62a17ae 958 pim_msdp_pkt_ka_tx(mp);
959 pim_msdp_peer_ka_timer_setup(mp, true /* start */);
960 return 0;
2a333e0f 961}
d62a17ae 962static void pim_msdp_peer_ka_timer_setup(struct pim_msdp_peer *mp, bool start)
2a333e0f 963{
d62a17ae 964 THREAD_OFF(mp->ka_timer);
965 if (start) {
472ad383
DS
966 thread_add_timer(mp->pim->msdp.master,
967 pim_msdp_peer_ka_timer_cb, mp,
968 PIM_MSDP_PEER_KA_TIME, &mp->ka_timer);
d62a17ae 969 }
2a333e0f 970}
971
d62a17ae 972static void pim_msdp_peer_active_connect(struct pim_msdp_peer *mp)
2a333e0f 973{
d62a17ae 974 int rc;
975 ++mp->conn_attempts;
976 rc = pim_msdp_sock_connect(mp);
2a333e0f 977
d62a17ae 978 if (PIM_DEBUG_MSDP_INTERNAL) {
979 zlog_debug("MSDP peer %s pim_msdp_peer_active_connect: %d",
980 mp->key_str, rc);
981 }
2a333e0f 982
d62a17ae 983 switch (rc) {
984 case connect_error:
985 case -1:
986 /* connect failed restart the connect-retry timer */
987 pim_msdp_peer_cr_timer_setup(mp, true /* start */);
988 break;
2a333e0f 989
d62a17ae 990 case connect_success:
991 /* connect was sucessful move to established */
992 pim_msdp_peer_established(mp);
993 break;
2a333e0f 994
d62a17ae 995 case connect_in_progress:
996 /* for NB content we need to wait till sock is readable or
997 * writeable */
998 PIM_MSDP_PEER_WRITE_ON(mp);
999 PIM_MSDP_PEER_READ_ON(mp);
1000 /* also restart connect-retry timer to reset the socket if
1001 * connect is
1002 * not sucessful */
1003 pim_msdp_peer_cr_timer_setup(mp, true /* start */);
1004 break;
1005 }
2a333e0f 1006}
1007
1008/* RFC-3618:Sec-5.6 - connection retry on active peer */
d62a17ae 1009static int pim_msdp_peer_cr_timer_cb(struct thread *t)
2a333e0f 1010{
d62a17ae 1011 struct pim_msdp_peer *mp;
2a333e0f 1012
d62a17ae 1013 mp = THREAD_ARG(t);
2a333e0f 1014
d62a17ae 1015 if (PIM_DEBUG_MSDP_EVENTS) {
1016 pim_msdp_peer_timer_expiry_log(mp, "connect-retry");
1017 }
2a333e0f 1018
d62a17ae 1019 if (mp->state != PIM_MSDP_CONNECTING || PIM_MSDP_PEER_IS_LISTENER(mp)) {
1020 return 0;
1021 }
2a333e0f 1022
d62a17ae 1023 pim_msdp_peer_active_connect(mp);
1024 return 0;
2a333e0f 1025}
d62a17ae 1026static void pim_msdp_peer_cr_timer_setup(struct pim_msdp_peer *mp, bool start)
2a333e0f 1027{
d62a17ae 1028 THREAD_OFF(mp->cr_timer);
1029 if (start) {
472ad383
DS
1030 thread_add_timer(
1031 mp->pim->msdp.master, pim_msdp_peer_cr_timer_cb, mp,
1032 PIM_MSDP_PEER_CONNECT_RETRY_TIME, &mp->cr_timer);
d62a17ae 1033 }
2a333e0f 1034}
1035
1036/* if a valid packet is rxed from the peer we can restart hold timer */
d62a17ae 1037void pim_msdp_peer_pkt_rxed(struct pim_msdp_peer *mp)
2a333e0f 1038{
d62a17ae 1039 if (mp->state == PIM_MSDP_ESTABLISHED) {
1040 pim_msdp_peer_hold_timer_setup(mp, true /* start */);
1041 }
2a333e0f 1042}
1043
3c72d654 1044/* if a valid packet is txed to the peer we can restart ka timer and avoid
1045 * unnecessary ka noise in the network */
d62a17ae 1046void pim_msdp_peer_pkt_txed(struct pim_msdp_peer *mp)
3c72d654 1047{
d62a17ae 1048 if (mp->state == PIM_MSDP_ESTABLISHED) {
1049 pim_msdp_peer_ka_timer_setup(mp, true /* start */);
1050 if (PIM_DEBUG_MSDP_INTERNAL) {
1051 zlog_debug("MSDP ka timer restart on pkt tx to %s",
1052 mp->key_str);
1053 }
1054 }
3c72d654 1055}
1056
2a333e0f 1057static void pim_msdp_addr2su(union sockunion *su, struct in_addr addr)
1058{
d62a17ae 1059 sockunion_init(su);
1060 su->sin.sin_addr = addr;
1061 su->sin.sin_family = AF_INET;
2a333e0f 1062#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
d62a17ae 1063 su->sin.sin_len = sizeof(struct sockaddr_in);
2a333e0f 1064#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
1065}
1066
1067/* 11.2.A1: create a new peer and transition state to listen or connecting */
472ad383
DS
1068static enum pim_msdp_err pim_msdp_peer_new(struct pim_instance *pim,
1069 struct in_addr peer_addr,
d62a17ae 1070 struct in_addr local_addr,
1071 const char *mesh_group_name,
1072 struct pim_msdp_peer **mp_p)
1073{
1074 struct pim_msdp_peer *mp;
1075
472ad383 1076 pim_msdp_enable(pim);
d62a17ae 1077
1078 mp = XCALLOC(MTYPE_PIM_MSDP_PEER, sizeof(*mp));
d62a17ae 1079
472ad383 1080 mp->pim = pim;
d62a17ae 1081 mp->peer = peer_addr;
1082 pim_inet4_dump("<peer?>", mp->peer, mp->key_str, sizeof(mp->key_str));
1083 pim_msdp_addr2su(&mp->su_peer, mp->peer);
1084 mp->local = local_addr;
1085 /* XXX: originator_id setting needs to move to the mesh group */
472ad383 1086 pim->msdp.originator_id = local_addr;
d62a17ae 1087 pim_msdp_addr2su(&mp->su_local, mp->local);
1088 mp->mesh_group_name = XSTRDUP(MTYPE_PIM_MSDP_MG_NAME, mesh_group_name);
1089 mp->state = PIM_MSDP_INACTIVE;
1090 mp->fd = -1;
c35b7e6b 1091 strlcpy(mp->last_reset, "-", sizeof(mp->last_reset));
d62a17ae 1092 /* higher IP address is listener */
1093 if (ntohl(mp->local.s_addr) > ntohl(mp->peer.s_addr)) {
1094 mp->flags |= PIM_MSDP_PEERF_LISTENER;
1095 }
1096
1097 /* setup packet buffers */
1098 mp->ibuf = stream_new(PIM_MSDP_MAX_PACKET_SIZE);
1099 mp->obuf = stream_fifo_new();
1100
1101 /* insert into misc tables for easy access */
472ad383
DS
1102 mp = hash_get(pim->msdp.peer_hash, mp, hash_alloc_intern);
1103 listnode_add_sort(pim->msdp.peer_list, mp);
d62a17ae 1104
1105 if (PIM_DEBUG_MSDP_EVENTS) {
1106 zlog_debug("MSDP peer %s created", mp->key_str);
1107
1108 pim_msdp_peer_state_chg_log(mp);
1109 }
1110
1111 /* fireup the connect state machine */
1112 if (PIM_MSDP_PEER_IS_LISTENER(mp)) {
1113 pim_msdp_peer_listen(mp);
1114 } else {
1115 pim_msdp_peer_connect(mp);
1116 }
1117 if (mp_p) {
1118 *mp_p = mp;
1119 }
1120 return PIM_MSDP_ERR_NONE;
1121}
1122
472ad383
DS
1123struct pim_msdp_peer *pim_msdp_peer_find(struct pim_instance *pim,
1124 struct in_addr peer_addr)
d62a17ae 1125{
1126 struct pim_msdp_peer lookup;
1127
1128 lookup.peer = peer_addr;
472ad383 1129 return hash_lookup(pim->msdp.peer_hash, &lookup);
2a333e0f 1130}
1131
1132/* add peer configuration if it doesn't already exist */
472ad383
DS
1133enum pim_msdp_err pim_msdp_peer_add(struct pim_instance *pim,
1134 struct in_addr peer_addr,
d62a17ae 1135 struct in_addr local_addr,
1136 const char *mesh_group_name,
1137 struct pim_msdp_peer **mp_p)
2a333e0f 1138{
d62a17ae 1139 struct pim_msdp_peer *mp;
2a333e0f 1140
d62a17ae 1141 if (mp_p) {
1142 *mp_p = NULL;
1143 }
977d71cc 1144
d62a17ae 1145 if (peer_addr.s_addr == local_addr.s_addr) {
1146 /* skip session setup if config is invalid */
1147 if (PIM_DEBUG_MSDP_EVENTS) {
1148 char peer_str[INET_ADDRSTRLEN];
977d71cc 1149
d62a17ae 1150 pim_inet4_dump("<peer?>", peer_addr, peer_str,
1151 sizeof(peer_str));
1152 zlog_debug("%s add skipped as DIP=SIP", peer_str);
1153 }
1154 return PIM_MSDP_ERR_SIP_EQ_DIP;
1155 }
977d71cc 1156
472ad383 1157 mp = pim_msdp_peer_find(pim, peer_addr);
d62a17ae 1158 if (mp) {
1159 if (mp_p) {
1160 *mp_p = mp;
1161 }
1162 return PIM_MSDP_ERR_PEER_EXISTS;
1163 }
2a333e0f 1164
472ad383
DS
1165 return pim_msdp_peer_new(pim, peer_addr, local_addr, mesh_group_name,
1166 mp_p);
2a333e0f 1167}
1168
1169/* release all mem associated with a peer */
d62a17ae 1170static void pim_msdp_peer_free(struct pim_msdp_peer *mp)
11128587 1171{
ec2f0e53
DS
1172 /*
1173 * Let's make sure we are not running when we delete
1174 * the underlying data structure
1175 */
845d9af7 1176 pim_msdp_peer_stop_tcp_conn(mp, false);
ec2f0e53 1177
d62a17ae 1178 if (mp->ibuf) {
1179 stream_free(mp->ibuf);
1180 }
2a333e0f 1181
d62a17ae 1182 if (mp->obuf) {
1183 stream_fifo_free(mp->obuf);
1184 }
2a333e0f 1185
e1b36e13 1186 XFREE(MTYPE_PIM_MSDP_MG_NAME, mp->mesh_group_name);
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 */
d8b87afe 1226static unsigned int pim_msdp_peer_hash_key_make(const void *p)
2a333e0f 1227{
d8b87afe 1228 const struct pim_msdp_peer *mp = p;
d62a17ae 1229 return (jhash_1word(mp->peer.s_addr, 0));
2a333e0f 1230}
1231
74df8d6d 1232static bool 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 **************************/
822d3c85 1255static void pim_msdp_mg_free(struct pim_instance *pim)
977d71cc 1256{
822d3c85
DS
1257 struct pim_msdp_mg *mg = pim->msdp.mg;
1258
d62a17ae 1259 /* If the mesh-group has valid member or src_ip don't delete it */
1260 if (!mg || mg->mbr_cnt || (mg->src_ip.s_addr != INADDR_ANY)) {
1261 return;
1262 }
977d71cc 1263
d62a17ae 1264 if (PIM_DEBUG_MSDP_EVENTS) {
1265 zlog_debug("MSDP mesh-group %s deleted", mg->mesh_group_name);
1266 }
0a22ddfb 1267 XFREE(MTYPE_PIM_MSDP_MG_NAME, mg->mesh_group_name);
977d71cc 1268
d62a17ae 1269 if (mg->mbr_list)
6a154c88 1270 list_delete(&mg->mbr_list);
b9b1e1f2 1271
822d3c85 1272 XFREE(MTYPE_PIM_MSDP_MG, pim->msdp.mg);
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));
977d71cc 1280
d62a17ae 1281 mg->mesh_group_name = XSTRDUP(MTYPE_PIM_MSDP_MG_NAME, mesh_group_name);
1282 mg->mbr_list = list_new();
1283 mg->mbr_list->del = (void (*)(void *))pim_msdp_mg_mbr_free;
1284 mg->mbr_list->cmp = (int (*)(void *, void *))pim_msdp_mg_mbr_comp;
977d71cc 1285
d62a17ae 1286 if (PIM_DEBUG_MSDP_EVENTS) {
1287 zlog_debug("MSDP mesh-group %s created", mg->mesh_group_name);
1288 }
1289 return mg;
977d71cc 1290}
1291
472ad383
DS
1292enum pim_msdp_err pim_msdp_mg_del(struct pim_instance *pim,
1293 const char *mesh_group_name)
977d71cc 1294{
472ad383 1295 struct pim_msdp_mg *mg = pim->msdp.mg;
d62a17ae 1296 struct pim_msdp_mg_mbr *mbr;
977d71cc 1297
34d86eff
SP
1298 if (!mg
1299 || (mesh_group_name
1300 && strcmp(mg->mesh_group_name, mesh_group_name))) {
d62a17ae 1301 return PIM_MSDP_ERR_NO_MG;
1302 }
977d71cc 1303
d62a17ae 1304 /* delete all the mesh-group members */
1305 while (!list_isempty(mg->mbr_list)) {
1306 mbr = listnode_head(mg->mbr_list);
1307 pim_msdp_mg_mbr_do_del(mg, mbr);
1308 }
977d71cc 1309
d62a17ae 1310 /* clear src ip */
1311 mg->src_ip.s_addr = INADDR_ANY;
977d71cc 1312
d62a17ae 1313 /* free up the mesh-group */
822d3c85 1314 pim_msdp_mg_free(pim);
d62a17ae 1315 return PIM_MSDP_ERR_NONE;
977d71cc 1316}
1317
472ad383
DS
1318static enum pim_msdp_err pim_msdp_mg_add(struct pim_instance *pim,
1319 const char *mesh_group_name)
977d71cc 1320{
472ad383
DS
1321 if (pim->msdp.mg) {
1322 if (!strcmp(pim->msdp.mg->mesh_group_name, mesh_group_name)) {
d62a17ae 1323 return PIM_MSDP_ERR_NONE;
1324 }
1325 /* currently only one mesh-group can exist at a time */
1326 return PIM_MSDP_ERR_MAX_MESH_GROUPS;
1327 }
977d71cc 1328
472ad383
DS
1329 pim->msdp.mg = pim_msdp_mg_new(mesh_group_name);
1330 if (!pim->msdp.mg) {
d62a17ae 1331 return PIM_MSDP_ERR_OOM;
1332 }
977d71cc 1333
d62a17ae 1334 return PIM_MSDP_ERR_NONE;
977d71cc 1335}
1336
d62a17ae 1337static int pim_msdp_mg_mbr_comp(const void *p1, const void *p2)
977d71cc 1338{
d62a17ae 1339 const struct pim_msdp_mg_mbr *mbr1 = p1;
1340 const struct pim_msdp_mg_mbr *mbr2 = p2;
977d71cc 1341
d62a17ae 1342 if (ntohl(mbr1->mbr_ip.s_addr) < ntohl(mbr2->mbr_ip.s_addr))
1343 return -1;
977d71cc 1344
d62a17ae 1345 if (ntohl(mbr1->mbr_ip.s_addr) > ntohl(mbr2->mbr_ip.s_addr))
1346 return 1;
977d71cc 1347
d62a17ae 1348 return 0;
977d71cc 1349}
1350
d62a17ae 1351static void pim_msdp_mg_mbr_free(struct pim_msdp_mg_mbr *mbr)
977d71cc 1352{
d62a17ae 1353 XFREE(MTYPE_PIM_MSDP_MG_MBR, mbr);
977d71cc 1354}
1355
472ad383
DS
1356static struct pim_msdp_mg_mbr *pim_msdp_mg_mbr_find(struct pim_instance *pim,
1357 struct in_addr mbr_ip)
977d71cc 1358{
d62a17ae 1359 struct pim_msdp_mg_mbr *mbr;
1360 struct listnode *mbr_node;
977d71cc 1361
472ad383 1362 if (!pim->msdp.mg) {
d62a17ae 1363 return NULL;
1364 }
1365 /* we can move this to a hash but considering that number of peers in
1366 * a mesh-group that seems like bit of an overkill */
472ad383 1367 for (ALL_LIST_ELEMENTS_RO(pim->msdp.mg->mbr_list, mbr_node, mbr)) {
d62a17ae 1368 if (mbr->mbr_ip.s_addr == mbr_ip.s_addr) {
1369 return mbr;
1370 }
1371 }
1372 return mbr;
977d71cc 1373}
1374
472ad383
DS
1375enum pim_msdp_err pim_msdp_mg_mbr_add(struct pim_instance *pim,
1376 const char *mesh_group_name,
d62a17ae 1377 struct in_addr mbr_ip)
977d71cc 1378{
d62a17ae 1379 int rc;
1380 struct pim_msdp_mg_mbr *mbr;
1381 struct pim_msdp_mg *mg;
977d71cc 1382
472ad383 1383 rc = pim_msdp_mg_add(pim, mesh_group_name);
d62a17ae 1384 if (rc != PIM_MSDP_ERR_NONE) {
1385 return rc;
1386 }
977d71cc 1387
472ad383
DS
1388 mg = pim->msdp.mg;
1389 mbr = pim_msdp_mg_mbr_find(pim, mbr_ip);
d62a17ae 1390 if (mbr) {
1391 return PIM_MSDP_ERR_MG_MBR_EXISTS;
1392 }
977d71cc 1393
d62a17ae 1394 mbr = XCALLOC(MTYPE_PIM_MSDP_MG_MBR, sizeof(*mbr));
d62a17ae 1395 mbr->mbr_ip = mbr_ip;
1396 listnode_add_sort(mg->mbr_list, mbr);
977d71cc 1397
d62a17ae 1398 /* if valid SIP has been configured add peer session */
1399 if (mg->src_ip.s_addr != INADDR_ANY) {
472ad383 1400 pim_msdp_peer_add(pim, mbr_ip, mg->src_ip, mesh_group_name,
d62a17ae 1401 &mbr->mp);
1402 }
977d71cc 1403
d62a17ae 1404 if (PIM_DEBUG_MSDP_EVENTS) {
1405 char ip_str[INET_ADDRSTRLEN];
1406 pim_inet4_dump("<mbr?>", mbr->mbr_ip, ip_str, sizeof(ip_str));
1407 zlog_debug("MSDP mesh-group %s mbr %s created",
1408 mg->mesh_group_name, ip_str);
1409 }
1410 ++mg->mbr_cnt;
1411 return PIM_MSDP_ERR_NONE;
977d71cc 1412}
1413
d62a17ae 1414static void pim_msdp_mg_mbr_do_del(struct pim_msdp_mg *mg,
1415 struct pim_msdp_mg_mbr *mbr)
977d71cc 1416{
d62a17ae 1417 /* Delete active peer session if any */
1418 if (mbr->mp) {
1419 pim_msdp_peer_do_del(mbr->mp);
1420 }
977d71cc 1421
d62a17ae 1422 listnode_delete(mg->mbr_list, mbr);
1423 if (PIM_DEBUG_MSDP_EVENTS) {
1424 char ip_str[INET_ADDRSTRLEN];
1425 pim_inet4_dump("<mbr?>", mbr->mbr_ip, ip_str, sizeof(ip_str));
1426 zlog_debug("MSDP mesh-group %s mbr %s deleted",
1427 mg->mesh_group_name, ip_str);
1428 }
1429 pim_msdp_mg_mbr_free(mbr);
1430 if (mg->mbr_cnt) {
1431 --mg->mbr_cnt;
1432 }
977d71cc 1433}
1434
472ad383
DS
1435enum pim_msdp_err pim_msdp_mg_mbr_del(struct pim_instance *pim,
1436 const char *mesh_group_name,
d62a17ae 1437 struct in_addr mbr_ip)
1438{
1439 struct pim_msdp_mg_mbr *mbr;
472ad383 1440 struct pim_msdp_mg *mg = pim->msdp.mg;
d62a17ae 1441
1442 if (!mg || strcmp(mg->mesh_group_name, mesh_group_name)) {
1443 return PIM_MSDP_ERR_NO_MG;
1444 }
1445
472ad383 1446 mbr = pim_msdp_mg_mbr_find(pim, mbr_ip);
d62a17ae 1447 if (!mbr) {
1448 return PIM_MSDP_ERR_NO_MG_MBR;
1449 }
1450
1451 pim_msdp_mg_mbr_do_del(mg, mbr);
1452 /* if there are no references to the mg free it */
822d3c85 1453 pim_msdp_mg_free(pim);
d62a17ae 1454
1455 return PIM_MSDP_ERR_NONE;
1456}
1457
472ad383 1458static void pim_msdp_mg_src_do_del(struct pim_instance *pim)
d62a17ae 1459{
1460 struct pim_msdp_mg_mbr *mbr;
1461 struct listnode *mbr_node;
472ad383 1462 struct pim_msdp_mg *mg = pim->msdp.mg;
d62a17ae 1463
1464 /* SIP is being removed - tear down all active peer sessions */
1465 for (ALL_LIST_ELEMENTS_RO(mg->mbr_list, mbr_node, mbr)) {
1466 if (mbr->mp) {
1467 pim_msdp_peer_do_del(mbr->mp);
1468 mbr->mp = NULL;
1469 }
1470 }
1471 if (PIM_DEBUG_MSDP_EVENTS) {
1472 zlog_debug("MSDP mesh-group %s src cleared",
1473 mg->mesh_group_name);
1474 }
1475}
1476
472ad383
DS
1477enum pim_msdp_err pim_msdp_mg_src_del(struct pim_instance *pim,
1478 const char *mesh_group_name)
d62a17ae 1479{
472ad383 1480 struct pim_msdp_mg *mg = pim->msdp.mg;
d62a17ae 1481
1482 if (!mg || strcmp(mg->mesh_group_name, mesh_group_name)) {
1483 return PIM_MSDP_ERR_NO_MG;
1484 }
1485
1486 if (mg->src_ip.s_addr != INADDR_ANY) {
1487 mg->src_ip.s_addr = INADDR_ANY;
472ad383 1488 pim_msdp_mg_src_do_del(pim);
d62a17ae 1489 /* if there are no references to the mg free it */
822d3c85 1490 pim_msdp_mg_free(pim);
d62a17ae 1491 }
1492 return PIM_MSDP_ERR_NONE;
1493}
1494
472ad383
DS
1495enum pim_msdp_err pim_msdp_mg_src_add(struct pim_instance *pim,
1496 const char *mesh_group_name,
d62a17ae 1497 struct in_addr src_ip)
1498{
1499 int rc;
1500 struct pim_msdp_mg_mbr *mbr;
1501 struct listnode *mbr_node;
1502 struct pim_msdp_mg *mg;
1503
1504 if (src_ip.s_addr == INADDR_ANY) {
472ad383 1505 pim_msdp_mg_src_del(pim, mesh_group_name);
d62a17ae 1506 return PIM_MSDP_ERR_NONE;
1507 }
1508
472ad383 1509 rc = pim_msdp_mg_add(pim, mesh_group_name);
d62a17ae 1510 if (rc != PIM_MSDP_ERR_NONE) {
1511 return rc;
1512 }
1513
472ad383 1514 mg = pim->msdp.mg;
d62a17ae 1515 if (mg->src_ip.s_addr != INADDR_ANY) {
472ad383 1516 pim_msdp_mg_src_do_del(pim);
d62a17ae 1517 }
1518 mg->src_ip = src_ip;
977d71cc 1519
d62a17ae 1520 for (ALL_LIST_ELEMENTS_RO(mg->mbr_list, mbr_node, mbr)) {
472ad383 1521 pim_msdp_peer_add(pim, mbr->mbr_ip, mg->src_ip, mesh_group_name,
d62a17ae 1522 &mbr->mp);
1523 }
977d71cc 1524
d62a17ae 1525 if (PIM_DEBUG_MSDP_EVENTS) {
1526 char ip_str[INET_ADDRSTRLEN];
1527 pim_inet4_dump("<src?>", mg->src_ip, ip_str, sizeof(ip_str));
1528 zlog_debug("MSDP mesh-group %s src %s set", mg->mesh_group_name,
1529 ip_str);
1530 }
1531 return PIM_MSDP_ERR_NONE;
977d71cc 1532}
1533
3c72d654 1534/*********************** MSDP feature APIs *********************************/
84f25989
DS
1535int pim_msdp_config_write(struct pim_instance *pim, struct vty *vty,
1536 const char *spaces)
d62a17ae 1537{
1538 struct listnode *mbrnode;
1539 struct pim_msdp_mg_mbr *mbr;
472ad383 1540 struct pim_msdp_mg *mg = pim->msdp.mg;
d62a17ae 1541 char mbr_str[INET_ADDRSTRLEN];
1542 char src_str[INET_ADDRSTRLEN];
1543 int count = 0;
1544
1545 if (!mg) {
1546 return count;
1547 }
1548
1549 if (mg->src_ip.s_addr != INADDR_ANY) {
1550 pim_inet4_dump("<src?>", mg->src_ip, src_str, sizeof(src_str));
c9657fdc 1551 vty_out(vty, "%sip msdp mesh-group %s source %s\n", spaces,
d62a17ae 1552 mg->mesh_group_name, src_str);
1553 ++count;
1554 }
1555
1556 for (ALL_LIST_ELEMENTS_RO(mg->mbr_list, mbrnode, mbr)) {
1557 pim_inet4_dump("<mbr?>", mbr->mbr_ip, mbr_str, sizeof(mbr_str));
c9657fdc 1558 vty_out(vty, "%sip msdp mesh-group %s member %s\n", spaces,
d62a17ae 1559 mg->mesh_group_name, mbr_str);
1560 ++count;
1561 }
1562 return count;
472ad383
DS
1563}
1564
3c72d654 1565/* Enable feature including active/periodic timers etc. on the first peer
1566 * config. Till then MSDP should just stay quiet. */
472ad383 1567static void pim_msdp_enable(struct pim_instance *pim)
3c72d654 1568{
472ad383 1569 if (pim->msdp.flags & PIM_MSDPF_ENABLE) {
d62a17ae 1570 /* feature is already enabled */
1571 return;
1572 }
472ad383
DS
1573 pim->msdp.flags |= PIM_MSDPF_ENABLE;
1574 pim->msdp.work_obuf = stream_new(PIM_MSDP_MAX_PACKET_SIZE);
1575 pim_msdp_sa_adv_timer_setup(pim, true /* start */);
d62a17ae 1576 /* setup sa cache based on local sources */
472ad383 1577 pim_msdp_sa_local_setup(pim);
3c72d654 1578}
1579
2a333e0f 1580/* MSDP init */
64c86530 1581void pim_msdp_init(struct pim_instance *pim, struct thread_master *master)
2a333e0f 1582{
472ad383 1583 pim->msdp.master = master;
9fb302f4 1584 char hash_name[64];
3c72d654 1585
772270f3
QY
1586 snprintf(hash_name, sizeof(hash_name), "PIM %s MSDP Peer Hash",
1587 pim->vrf->name);
2ad78035 1588 pim->msdp.peer_hash = hash_create(pim_msdp_peer_hash_key_make,
9fb302f4 1589 pim_msdp_peer_hash_eq, hash_name);
2ad78035
DS
1590 pim->msdp.peer_list = list_new();
1591 pim->msdp.peer_list->del = (void (*)(void *))pim_msdp_peer_free;
1592 pim->msdp.peer_list->cmp = (int (*)(void *, void *))pim_msdp_peer_comp;
3c72d654 1593
772270f3
QY
1594 snprintf(hash_name, sizeof(hash_name), "PIM %s MSDP SA Hash",
1595 pim->vrf->name);
2ad78035 1596 pim->msdp.sa_hash = hash_create(pim_msdp_sa_hash_key_make,
9fb302f4 1597 pim_msdp_sa_hash_eq, hash_name);
2ad78035
DS
1598 pim->msdp.sa_list = list_new();
1599 pim->msdp.sa_list->del = (void (*)(void *))pim_msdp_sa_free;
1600 pim->msdp.sa_list->cmp = (int (*)(void *, void *))pim_msdp_sa_comp;
2a333e0f 1601}
1602
1603/* counterpart to MSDP init; XXX: unused currently */
2ad78035 1604void pim_msdp_exit(struct pim_instance *pim)
2a333e0f 1605{
ec2f0e53
DS
1606 pim_msdp_sa_adv_timer_setup(pim, false);
1607
d62a17ae 1608 /* XXX: stop listener and delete all peer sessions */
2a333e0f 1609
822d3c85
DS
1610 pim_msdp_mg_free(pim);
1611
2ad78035 1612 if (pim->msdp.peer_hash) {
36ef2c10 1613 hash_clean(pim->msdp.peer_hash, NULL);
2ad78035
DS
1614 hash_free(pim->msdp.peer_hash);
1615 pim->msdp.peer_hash = NULL;
d62a17ae 1616 }
2a333e0f 1617
2ad78035 1618 if (pim->msdp.peer_list) {
6a154c88 1619 list_delete(&pim->msdp.peer_list);
d62a17ae 1620 }
3da8a099
DS
1621
1622 if (pim->msdp.sa_hash) {
36ef2c10 1623 hash_clean(pim->msdp.sa_hash, NULL);
3da8a099
DS
1624 hash_free(pim->msdp.sa_hash);
1625 pim->msdp.sa_hash = NULL;
1626 }
1627
1628 if (pim->msdp.sa_list) {
6a154c88 1629 list_delete(&pim->msdp.sa_list);
3da8a099 1630 }
36ef2c10
DS
1631
1632 if (pim->msdp.work_obuf)
1633 stream_free(pim->msdp.work_obuf);
1634 pim->msdp.work_obuf = NULL;
11128587 1635}