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