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