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