]> git.proxmox.com Git - mirror_frr.git/blame - pimd/pim_msdp.c
pimd: move, rename and deploy pim_addr_is_any()
[mirror_frr.git] / pimd / pim_msdp.c
CommitLineData
11128587 1/*
2a333e0f 2 * IP MSDP for Quagga
11128587 3 * Copyright (C) 2016 Cumulus Networks, Inc.
11128587
DS
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
896014f4
DL
15 * You should have received a copy of the GNU General Public License along
16 * with this program; see the file COPYING; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
11128587
DS
18 */
19
20#include <zebra.h>
21
2a333e0f 22#include <lib/hash.h>
23#include <lib/jhash.h>
24#include <lib/log.h>
3c72d654 25#include <lib/prefix.h>
2a333e0f 26#include <lib/sockunion.h>
27#include <lib/stream.h>
28#include <lib/thread.h>
3c72d654 29#include <lib/vty.h>
30#include <lib/plist.h>
3613d898 31#include <lib/lib_errors.h>
11128587 32
2a333e0f 33#include "pimd.h"
34#include "pim_cmd.h"
35#include "pim_memory.h"
7176984f 36#include "pim_iface.h"
3c72d654 37#include "pim_rp.h"
2a333e0f 38#include "pim_str.h"
39#include "pim_time.h"
1bf16443 40#include "pim_upstream.h"
472ad383 41#include "pim_oil.h"
11128587 42
2a333e0f 43#include "pim_msdp.h"
44#include "pim_msdp_packet.h"
45#include "pim_msdp_socket.h"
46
2ad78035 47// struct pim_msdp pim_msdp, *msdp = &pim_msdp;
2a333e0f 48
49static void pim_msdp_peer_listen(struct pim_msdp_peer *mp);
50static void pim_msdp_peer_cr_timer_setup(struct pim_msdp_peer *mp, bool start);
51static void pim_msdp_peer_ka_timer_setup(struct pim_msdp_peer *mp, bool start);
d62a17ae 52static void pim_msdp_peer_hold_timer_setup(struct pim_msdp_peer *mp,
53 bool start);
2a333e0f 54static void pim_msdp_peer_free(struct pim_msdp_peer *mp);
472ad383
DS
55static void pim_msdp_enable(struct pim_instance *pim);
56static void pim_msdp_sa_adv_timer_setup(struct pim_instance *pim, bool start);
d62a17ae 57static void pim_msdp_sa_deref(struct pim_msdp_sa *sa,
58 enum pim_msdp_sa_flags flags);
977d71cc 59static int pim_msdp_mg_mbr_comp(const void *p1, const void *p2);
60static void pim_msdp_mg_mbr_free(struct pim_msdp_mg_mbr *mbr);
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 */
d62a17ae 70static int 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);
d62a17ae 80 return 0;
3c72d654 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 */
d62a17ae 93static int 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);
104 return 0;
3c72d654 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 */
318 if (PIM_INADDR_ISNOT_ANY(sa->peer)) {
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)) {
d62a17ae 635 if (sa->sg.grp.s_addr != xg_up->sg.grp.s_addr) {
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)) {
d62a17ae 659 if (sa->sg.grp.s_addr != sg->grp.s_addr) {
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
d62a17ae 683 return (jhash_2words(sa->sg.src.s_addr, sa->sg.grp.s_addr, 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
d62a17ae 691 return ((sa1->sg.src.s_addr == sa2->sg.src.s_addr)
692 && (sa1->sg.grp.s_addr == sa2->sg.grp.s_addr));
3c72d654 693}
694
d62a17ae 695static int pim_msdp_sa_comp(const void *p1, const void *p2)
3c72d654 696{
d62a17ae 697 const struct pim_msdp_sa *sa1 = p1;
698 const struct pim_msdp_sa *sa2 = p2;
3c72d654 699
d62a17ae 700 if (ntohl(sa1->sg.grp.s_addr) < ntohl(sa2->sg.grp.s_addr))
701 return -1;
3c72d654 702
d62a17ae 703 if (ntohl(sa1->sg.grp.s_addr) > ntohl(sa2->sg.grp.s_addr))
704 return 1;
3c72d654 705
d62a17ae 706 if (ntohl(sa1->sg.src.s_addr) < ntohl(sa2->sg.src.s_addr))
707 return -1;
3c72d654 708
d62a17ae 709 if (ntohl(sa1->sg.src.s_addr) > ntohl(sa2->sg.src.s_addr))
710 return 1;
3c72d654 711
d62a17ae 712 return 0;
3c72d654 713}
714
715/* RFC-3618:Sec-10.1.3 - Peer-RPF forwarding */
716/* XXX: this can use a bit of refining and extensions */
d62a17ae 717bool pim_msdp_peer_rpf_check(struct pim_msdp_peer *mp, struct in_addr rp)
3c72d654 718{
b0a13ffb 719 struct pim_nexthop nexthop = {0};
9fbd9fc4 720
d62a17ae 721 if (mp->peer.s_addr == rp.s_addr) {
722 return true;
723 }
3c72d654 724
9fbd9fc4
AMR
725 /* check if the MSDP peer is the nexthop for the RP */
726 if (pim_nexthop_lookup(mp->pim, &nexthop, rp, 0)
727 && nexthop.mrib_nexthop_addr.u.prefix4.s_addr == mp->peer.s_addr) {
728 return true;
729 }
730
d62a17ae 731 return false;
3c72d654 732}
d62a17ae 733
3c72d654 734/************************ Peer session management **************************/
d62a17ae 735char *pim_msdp_state_dump(enum pim_msdp_peer_state state, char *buf,
736 int buf_size)
737{
738 switch (state) {
739 case PIM_MSDP_DISABLED:
740 snprintf(buf, buf_size, "%s", "disabled");
741 break;
742 case PIM_MSDP_INACTIVE:
743 snprintf(buf, buf_size, "%s", "inactive");
744 break;
745 case PIM_MSDP_LISTEN:
746 snprintf(buf, buf_size, "%s", "listen");
747 break;
748 case PIM_MSDP_CONNECTING:
749 snprintf(buf, buf_size, "%s", "connecting");
750 break;
751 case PIM_MSDP_ESTABLISHED:
752 snprintf(buf, buf_size, "%s", "established");
753 break;
754 default:
755 snprintf(buf, buf_size, "unk-%d", state);
756 }
757 return buf;
758}
759
d62a17ae 760static void pim_msdp_peer_state_chg_log(struct pim_msdp_peer *mp)
761{
762 char state_str[PIM_MSDP_STATE_STRLEN];
763
764 pim_msdp_state_dump(mp->state, state_str, sizeof(state_str));
765 zlog_debug("MSDP peer %s state chg to %s", mp->key_str, state_str);
2a333e0f 766}
767
768/* MSDP Connection State Machine actions (defined in RFC-3618:Sec-11.2) */
769/* 11.2.A2: active peer - start connect retry timer; when the timer fires
770 * a tcp connection will be made */
d62a17ae 771static void pim_msdp_peer_connect(struct pim_msdp_peer *mp)
2a333e0f 772{
d62a17ae 773 mp->state = PIM_MSDP_CONNECTING;
774 if (PIM_DEBUG_MSDP_EVENTS) {
775 pim_msdp_peer_state_chg_log(mp);
776 }
2a333e0f 777
d62a17ae 778 pim_msdp_peer_cr_timer_setup(mp, true /* start */);
2a333e0f 779}
780
781/* 11.2.A3: passive peer - just listen for connections */
d62a17ae 782static void pim_msdp_peer_listen(struct pim_msdp_peer *mp)
2a333e0f 783{
d62a17ae 784 mp->state = PIM_MSDP_LISTEN;
785 if (PIM_DEBUG_MSDP_EVENTS) {
786 pim_msdp_peer_state_chg_log(mp);
787 }
2a333e0f 788
d62a17ae 789 /* this is interntionally asymmetric i.e. we set up listen-socket when
9d303b37
DL
790 * the
791 * first listening peer is configured; but don't bother tearing it down
792 * when
793 * all the peers go down */
472ad383 794 pim_msdp_sock_listen(mp->pim);
2a333e0f 795}
796
797/* 11.2.A4 and 11.2.A5: transition active or passive peer to
798 * established state */
d62a17ae 799void pim_msdp_peer_established(struct pim_msdp_peer *mp)
2a333e0f 800{
d62a17ae 801 if (mp->state != PIM_MSDP_ESTABLISHED) {
802 ++mp->est_flaps;
803 }
977d71cc 804
d62a17ae 805 mp->state = PIM_MSDP_ESTABLISHED;
806 mp->uptime = pim_time_monotonic_sec();
2a333e0f 807
d62a17ae 808 if (PIM_DEBUG_MSDP_EVENTS) {
809 pim_msdp_peer_state_chg_log(mp);
810 }
2a333e0f 811
d62a17ae 812 /* stop retry timer on active peers */
813 pim_msdp_peer_cr_timer_setup(mp, false /* start */);
2a333e0f 814
d62a17ae 815 /* send KA; start KA and hold timers */
816 pim_msdp_pkt_ka_tx(mp);
817 pim_msdp_peer_ka_timer_setup(mp, true /* start */);
818 pim_msdp_peer_hold_timer_setup(mp, true /* start */);
2a333e0f 819
d62a17ae 820 pim_msdp_pkt_sa_tx_to_one_peer(mp);
3c72d654 821
d62a17ae 822 PIM_MSDP_PEER_WRITE_ON(mp);
823 PIM_MSDP_PEER_READ_ON(mp);
2a333e0f 824}
825
826/* 11.2.A6, 11.2.A7 and 11.2.A8: shutdown the peer tcp connection */
d62a17ae 827void pim_msdp_peer_stop_tcp_conn(struct pim_msdp_peer *mp, bool chg_state)
828{
829 if (chg_state) {
830 if (mp->state == PIM_MSDP_ESTABLISHED) {
831 ++mp->est_flaps;
832 }
833 mp->state = PIM_MSDP_INACTIVE;
834 if (PIM_DEBUG_MSDP_EVENTS) {
835 pim_msdp_peer_state_chg_log(mp);
836 }
837 }
838
839 if (PIM_DEBUG_MSDP_INTERNAL) {
840 zlog_debug("MSDP peer %s pim_msdp_peer_stop_tcp_conn",
841 mp->key_str);
842 }
843 /* stop read and write threads */
844 PIM_MSDP_PEER_READ_OFF(mp);
845 PIM_MSDP_PEER_WRITE_OFF(mp);
846
847 /* reset buffers */
848 mp->packet_size = 0;
849 if (mp->ibuf)
850 stream_reset(mp->ibuf);
851 if (mp->obuf)
852 stream_fifo_clean(mp->obuf);
853
854 /* stop all peer timers */
855 pim_msdp_peer_ka_timer_setup(mp, false /* start */);
856 pim_msdp_peer_cr_timer_setup(mp, false /* start */);
857 pim_msdp_peer_hold_timer_setup(mp, false /* start */);
858
859 /* close connection */
860 if (mp->fd >= 0) {
861 close(mp->fd);
862 mp->fd = -1;
863 }
2a333e0f 864}
865
866/* RFC-3618:Sec-5.6 - stop the peer tcp connection and startover */
d62a17ae 867void pim_msdp_peer_reset_tcp_conn(struct pim_msdp_peer *mp, const char *rc_str)
2a333e0f 868{
d62a17ae 869 if (PIM_DEBUG_EVENTS) {
870 zlog_debug("MSDP peer %s tcp reset %s", mp->key_str, rc_str);
871 snprintf(mp->last_reset, sizeof(mp->last_reset), "%s", rc_str);
872 }
2a333e0f 873
d62a17ae 874 /* close the connection and transition to listening or connecting */
875 pim_msdp_peer_stop_tcp_conn(mp, true /* chg_state */);
876 if (PIM_MSDP_PEER_IS_LISTENER(mp)) {
877 pim_msdp_peer_listen(mp);
878 } else {
879 pim_msdp_peer_connect(mp);
880 }
2a333e0f 881}
882
d62a17ae 883static void pim_msdp_peer_timer_expiry_log(struct pim_msdp_peer *mp,
884 const char *timer_str)
2a333e0f 885{
d62a17ae 886 zlog_debug("MSDP peer %s %s timer expired", mp->key_str, timer_str);
2a333e0f 887}
888
889/* RFC-3618:Sec-5.4 - peer hold timer */
d62a17ae 890static int pim_msdp_peer_hold_timer_cb(struct thread *t)
2a333e0f 891{
d62a17ae 892 struct pim_msdp_peer *mp;
2a333e0f 893
d62a17ae 894 mp = THREAD_ARG(t);
2a333e0f 895
d62a17ae 896 if (PIM_DEBUG_MSDP_EVENTS) {
897 pim_msdp_peer_timer_expiry_log(mp, "hold");
898 }
2a333e0f 899
d62a17ae 900 if (mp->state != PIM_MSDP_ESTABLISHED) {
901 return 0;
902 }
2a333e0f 903
d62a17ae 904 if (PIM_DEBUG_MSDP_EVENTS) {
905 pim_msdp_peer_state_chg_log(mp);
906 }
907 pim_msdp_peer_reset_tcp_conn(mp, "ht-expired");
908 return 0;
2a333e0f 909}
472ad383 910
d62a17ae 911static void pim_msdp_peer_hold_timer_setup(struct pim_msdp_peer *mp, bool start)
2a333e0f 912{
472ad383 913 struct pim_instance *pim = mp->pim;
d62a17ae 914 THREAD_OFF(mp->hold_timer);
915 if (start) {
472ad383 916 thread_add_timer(pim->msdp.master, pim_msdp_peer_hold_timer_cb,
622fd3f1 917 mp, pim->msdp.hold_time, &mp->hold_timer);
d62a17ae 918 }
2a333e0f 919}
920
921
922/* RFC-3618:Sec-5.5 - peer keepalive timer */
d62a17ae 923static int pim_msdp_peer_ka_timer_cb(struct thread *t)
2a333e0f 924{
d62a17ae 925 struct pim_msdp_peer *mp;
2a333e0f 926
d62a17ae 927 mp = THREAD_ARG(t);
2a333e0f 928
d62a17ae 929 if (PIM_DEBUG_MSDP_EVENTS) {
930 pim_msdp_peer_timer_expiry_log(mp, "ka");
931 }
2a333e0f 932
d62a17ae 933 pim_msdp_pkt_ka_tx(mp);
934 pim_msdp_peer_ka_timer_setup(mp, true /* start */);
935 return 0;
2a333e0f 936}
d62a17ae 937static void pim_msdp_peer_ka_timer_setup(struct pim_msdp_peer *mp, bool start)
2a333e0f 938{
d62a17ae 939 THREAD_OFF(mp->ka_timer);
940 if (start) {
472ad383
DS
941 thread_add_timer(mp->pim->msdp.master,
942 pim_msdp_peer_ka_timer_cb, mp,
622fd3f1 943 mp->pim->msdp.keep_alive, &mp->ka_timer);
d62a17ae 944 }
2a333e0f 945}
946
d62a17ae 947static void pim_msdp_peer_active_connect(struct pim_msdp_peer *mp)
2a333e0f 948{
d62a17ae 949 int rc;
950 ++mp->conn_attempts;
951 rc = pim_msdp_sock_connect(mp);
2a333e0f 952
d62a17ae 953 if (PIM_DEBUG_MSDP_INTERNAL) {
954 zlog_debug("MSDP peer %s pim_msdp_peer_active_connect: %d",
955 mp->key_str, rc);
956 }
2a333e0f 957
d62a17ae 958 switch (rc) {
959 case connect_error:
960 case -1:
961 /* connect failed restart the connect-retry timer */
962 pim_msdp_peer_cr_timer_setup(mp, true /* start */);
963 break;
2a333e0f 964
d62a17ae 965 case connect_success:
966 /* connect was sucessful move to established */
967 pim_msdp_peer_established(mp);
968 break;
2a333e0f 969
d62a17ae 970 case connect_in_progress:
971 /* for NB content we need to wait till sock is readable or
972 * writeable */
973 PIM_MSDP_PEER_WRITE_ON(mp);
974 PIM_MSDP_PEER_READ_ON(mp);
975 /* also restart connect-retry timer to reset the socket if
976 * connect is
977 * not sucessful */
978 pim_msdp_peer_cr_timer_setup(mp, true /* start */);
979 break;
980 }
2a333e0f 981}
982
983/* RFC-3618:Sec-5.6 - connection retry on active peer */
d62a17ae 984static int pim_msdp_peer_cr_timer_cb(struct thread *t)
2a333e0f 985{
d62a17ae 986 struct pim_msdp_peer *mp;
2a333e0f 987
d62a17ae 988 mp = THREAD_ARG(t);
2a333e0f 989
d62a17ae 990 if (PIM_DEBUG_MSDP_EVENTS) {
991 pim_msdp_peer_timer_expiry_log(mp, "connect-retry");
992 }
2a333e0f 993
d62a17ae 994 if (mp->state != PIM_MSDP_CONNECTING || PIM_MSDP_PEER_IS_LISTENER(mp)) {
995 return 0;
996 }
2a333e0f 997
d62a17ae 998 pim_msdp_peer_active_connect(mp);
999 return 0;
2a333e0f 1000}
d62a17ae 1001static void pim_msdp_peer_cr_timer_setup(struct pim_msdp_peer *mp, bool start)
2a333e0f 1002{
d62a17ae 1003 THREAD_OFF(mp->cr_timer);
1004 if (start) {
622fd3f1
RZ
1005 thread_add_timer(mp->pim->msdp.master,
1006 pim_msdp_peer_cr_timer_cb, mp,
1007 mp->pim->msdp.connection_retry, &mp->cr_timer);
d62a17ae 1008 }
2a333e0f 1009}
1010
1011/* if a valid packet is rxed from the peer we can restart hold timer */
d62a17ae 1012void pim_msdp_peer_pkt_rxed(struct pim_msdp_peer *mp)
2a333e0f 1013{
d62a17ae 1014 if (mp->state == PIM_MSDP_ESTABLISHED) {
1015 pim_msdp_peer_hold_timer_setup(mp, true /* start */);
1016 }
2a333e0f 1017}
1018
3c72d654 1019/* if a valid packet is txed to the peer we can restart ka timer and avoid
1020 * unnecessary ka noise in the network */
d62a17ae 1021void pim_msdp_peer_pkt_txed(struct pim_msdp_peer *mp)
3c72d654 1022{
d62a17ae 1023 if (mp->state == PIM_MSDP_ESTABLISHED) {
1024 pim_msdp_peer_ka_timer_setup(mp, true /* start */);
1025 if (PIM_DEBUG_MSDP_INTERNAL) {
1026 zlog_debug("MSDP ka timer restart on pkt tx to %s",
1027 mp->key_str);
1028 }
1029 }
3c72d654 1030}
1031
2a333e0f 1032static void pim_msdp_addr2su(union sockunion *su, struct in_addr addr)
1033{
d62a17ae 1034 sockunion_init(su);
1035 su->sin.sin_addr = addr;
1036 su->sin.sin_family = AF_INET;
2a333e0f 1037#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
d62a17ae 1038 su->sin.sin_len = sizeof(struct sockaddr_in);
2a333e0f 1039#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
1040}
1041
1042/* 11.2.A1: create a new peer and transition state to listen or connecting */
c72d97a5 1043struct pim_msdp_peer *pim_msdp_peer_add(struct pim_instance *pim,
56697b3e
RZ
1044 const struct in_addr *peer,
1045 const struct in_addr *local,
1046 const char *mesh_group_name)
d62a17ae 1047{
1048 struct pim_msdp_peer *mp;
1049
472ad383 1050 pim_msdp_enable(pim);
d62a17ae 1051
1052 mp = XCALLOC(MTYPE_PIM_MSDP_PEER, sizeof(*mp));
d62a17ae 1053
472ad383 1054 mp->pim = pim;
56697b3e 1055 mp->peer = *peer;
d62a17ae 1056 pim_inet4_dump("<peer?>", mp->peer, mp->key_str, sizeof(mp->key_str));
1057 pim_msdp_addr2su(&mp->su_peer, mp->peer);
56697b3e 1058 mp->local = *local;
d62a17ae 1059 /* XXX: originator_id setting needs to move to the mesh group */
56697b3e 1060 pim->msdp.originator_id = *local;
d62a17ae 1061 pim_msdp_addr2su(&mp->su_local, mp->local);
0ce04a08
RZ
1062 if (mesh_group_name)
1063 mp->mesh_group_name =
1064 XSTRDUP(MTYPE_PIM_MSDP_MG_NAME, mesh_group_name);
1065
d62a17ae 1066 mp->state = PIM_MSDP_INACTIVE;
1067 mp->fd = -1;
c35b7e6b 1068 strlcpy(mp->last_reset, "-", sizeof(mp->last_reset));
d62a17ae 1069 /* higher IP address is listener */
1070 if (ntohl(mp->local.s_addr) > ntohl(mp->peer.s_addr)) {
1071 mp->flags |= PIM_MSDP_PEERF_LISTENER;
1072 }
1073
1074 /* setup packet buffers */
1075 mp->ibuf = stream_new(PIM_MSDP_MAX_PACKET_SIZE);
1076 mp->obuf = stream_fifo_new();
1077
1078 /* insert into misc tables for easy access */
472ad383
DS
1079 mp = hash_get(pim->msdp.peer_hash, mp, hash_alloc_intern);
1080 listnode_add_sort(pim->msdp.peer_list, mp);
d62a17ae 1081
1082 if (PIM_DEBUG_MSDP_EVENTS) {
1083 zlog_debug("MSDP peer %s created", mp->key_str);
1084
1085 pim_msdp_peer_state_chg_log(mp);
1086 }
1087
1088 /* fireup the connect state machine */
1089 if (PIM_MSDP_PEER_IS_LISTENER(mp)) {
1090 pim_msdp_peer_listen(mp);
1091 } else {
1092 pim_msdp_peer_connect(mp);
1093 }
56697b3e 1094 return mp;
d62a17ae 1095}
1096
472ad383
DS
1097struct pim_msdp_peer *pim_msdp_peer_find(struct pim_instance *pim,
1098 struct in_addr peer_addr)
d62a17ae 1099{
1100 struct pim_msdp_peer lookup;
1101
1102 lookup.peer = peer_addr;
472ad383 1103 return hash_lookup(pim->msdp.peer_hash, &lookup);
2a333e0f 1104}
1105
2a333e0f 1106/* release all mem associated with a peer */
d62a17ae 1107static void pim_msdp_peer_free(struct pim_msdp_peer *mp)
11128587 1108{
ec2f0e53
DS
1109 /*
1110 * Let's make sure we are not running when we delete
1111 * the underlying data structure
1112 */
845d9af7 1113 pim_msdp_peer_stop_tcp_conn(mp, false);
ec2f0e53 1114
d62a17ae 1115 if (mp->ibuf) {
1116 stream_free(mp->ibuf);
1117 }
2a333e0f 1118
d62a17ae 1119 if (mp->obuf) {
1120 stream_fifo_free(mp->obuf);
1121 }
2a333e0f 1122
e1b36e13 1123 XFREE(MTYPE_PIM_MSDP_MG_NAME, mp->mesh_group_name);
845d9af7
DS
1124
1125 mp->pim = NULL;
d62a17ae 1126 XFREE(MTYPE_PIM_MSDP_PEER, mp);
2a333e0f 1127}
1128
1129/* delete the peer config */
c72d97a5 1130void pim_msdp_peer_del(struct pim_msdp_peer **mp)
2a333e0f 1131{
56697b3e
RZ
1132 if (*mp == NULL)
1133 return;
1134
d62a17ae 1135 /* stop the tcp connection and shutdown all timers */
56697b3e 1136 pim_msdp_peer_stop_tcp_conn(*mp, true /* chg_state */);
2a333e0f 1137
d62a17ae 1138 /* remove the session from various tables */
56697b3e
RZ
1139 listnode_delete((*mp)->pim->msdp.peer_list, *mp);
1140 hash_release((*mp)->pim->msdp.peer_hash, *mp);
2a333e0f 1141
d62a17ae 1142 if (PIM_DEBUG_MSDP_EVENTS) {
56697b3e 1143 zlog_debug("MSDP peer %s deleted", (*mp)->key_str);
d62a17ae 1144 }
2a333e0f 1145
d62a17ae 1146 /* free up any associated memory */
56697b3e
RZ
1147 pim_msdp_peer_free(*mp);
1148 *mp = NULL;
2a333e0f 1149}
1150
56697b3e
RZ
1151void pim_msdp_peer_change_source(struct pim_msdp_peer *mp,
1152 const struct in_addr *addr)
977d71cc 1153{
56697b3e 1154 pim_msdp_peer_stop_tcp_conn(mp, true);
977d71cc 1155
56697b3e 1156 mp->local = *addr;
977d71cc 1157
56697b3e
RZ
1158 if (PIM_MSDP_PEER_IS_LISTENER(mp))
1159 pim_msdp_peer_listen(mp);
1160 else
1161 pim_msdp_peer_connect(mp);
977d71cc 1162}
1163
2a333e0f 1164/* peer hash and peer list helpers */
d8b87afe 1165static unsigned int pim_msdp_peer_hash_key_make(const void *p)
2a333e0f 1166{
d8b87afe 1167 const struct pim_msdp_peer *mp = p;
d62a17ae 1168 return (jhash_1word(mp->peer.s_addr, 0));
2a333e0f 1169}
1170
74df8d6d 1171static bool pim_msdp_peer_hash_eq(const void *p1, const void *p2)
2a333e0f 1172{
d62a17ae 1173 const struct pim_msdp_peer *mp1 = p1;
1174 const struct pim_msdp_peer *mp2 = p2;
2a333e0f 1175
d62a17ae 1176 return (mp1->peer.s_addr == mp2->peer.s_addr);
2a333e0f 1177}
1178
d62a17ae 1179static int pim_msdp_peer_comp(const void *p1, const void *p2)
2a333e0f 1180{
d62a17ae 1181 const struct pim_msdp_peer *mp1 = p1;
1182 const struct pim_msdp_peer *mp2 = p2;
2a333e0f 1183
d62a17ae 1184 if (ntohl(mp1->peer.s_addr) < ntohl(mp2->peer.s_addr))
1185 return -1;
2a333e0f 1186
d62a17ae 1187 if (ntohl(mp1->peer.s_addr) > ntohl(mp2->peer.s_addr))
1188 return 1;
2a333e0f 1189
d62a17ae 1190 return 0;
2a333e0f 1191}
1192
977d71cc 1193/************************** Mesh group management **************************/
e2809e61 1194void pim_msdp_mg_free(struct pim_instance *pim, struct pim_msdp_mg **mgp)
977d71cc 1195{
e2809e61
RZ
1196 struct pim_msdp_mg_mbr *mbr;
1197 struct listnode *n, *nn;
822d3c85 1198
e2809e61 1199 if (*mgp == NULL)
d62a17ae 1200 return;
e2809e61
RZ
1201
1202 /* SIP is being removed - tear down all active peer sessions */
1203 for (ALL_LIST_ELEMENTS((*mgp)->mbr_list, n, nn, mbr))
fb35f654 1204 pim_msdp_mg_mbr_del((*mgp), mbr);
977d71cc 1205
d62a17ae 1206 if (PIM_DEBUG_MSDP_EVENTS) {
e2809e61
RZ
1207 zlog_debug("MSDP mesh-group %s deleted",
1208 (*mgp)->mesh_group_name);
d62a17ae 1209 }
977d71cc 1210
e2809e61
RZ
1211 XFREE(MTYPE_PIM_MSDP_MG_NAME, (*mgp)->mesh_group_name);
1212
1213 if ((*mgp)->mbr_list)
1214 list_delete(&(*mgp)->mbr_list);
b9b1e1f2 1215
3e7ec159 1216 SLIST_REMOVE(&pim->msdp.mglist, (*mgp), pim_msdp_mg, mg_entry);
e2809e61 1217 XFREE(MTYPE_PIM_MSDP_MG, (*mgp));
977d71cc 1218}
1219
e2809e61
RZ
1220struct pim_msdp_mg *pim_msdp_mg_new(struct pim_instance *pim,
1221 const char *mesh_group_name)
977d71cc 1222{
d62a17ae 1223 struct pim_msdp_mg *mg;
977d71cc 1224
d62a17ae 1225 mg = XCALLOC(MTYPE_PIM_MSDP_MG, sizeof(*mg));
977d71cc 1226
d62a17ae 1227 mg->mesh_group_name = XSTRDUP(MTYPE_PIM_MSDP_MG_NAME, mesh_group_name);
1228 mg->mbr_list = list_new();
1229 mg->mbr_list->del = (void (*)(void *))pim_msdp_mg_mbr_free;
1230 mg->mbr_list->cmp = (int (*)(void *, void *))pim_msdp_mg_mbr_comp;
977d71cc 1231
d62a17ae 1232 if (PIM_DEBUG_MSDP_EVENTS) {
1233 zlog_debug("MSDP mesh-group %s created", mg->mesh_group_name);
1234 }
977d71cc 1235
e2809e61 1236 SLIST_INSERT_HEAD(&pim->msdp.mglist, mg, mg_entry);
977d71cc 1237
e2809e61 1238 return mg;
977d71cc 1239}
1240
d62a17ae 1241static int pim_msdp_mg_mbr_comp(const void *p1, const void *p2)
977d71cc 1242{
d62a17ae 1243 const struct pim_msdp_mg_mbr *mbr1 = p1;
1244 const struct pim_msdp_mg_mbr *mbr2 = p2;
977d71cc 1245
d62a17ae 1246 if (ntohl(mbr1->mbr_ip.s_addr) < ntohl(mbr2->mbr_ip.s_addr))
1247 return -1;
977d71cc 1248
d62a17ae 1249 if (ntohl(mbr1->mbr_ip.s_addr) > ntohl(mbr2->mbr_ip.s_addr))
1250 return 1;
977d71cc 1251
d62a17ae 1252 return 0;
977d71cc 1253}
1254
d62a17ae 1255static void pim_msdp_mg_mbr_free(struct pim_msdp_mg_mbr *mbr)
977d71cc 1256{
d62a17ae 1257 XFREE(MTYPE_PIM_MSDP_MG_MBR, mbr);
977d71cc 1258}
1259
fb35f654 1260void pim_msdp_mg_mbr_del(struct pim_msdp_mg *mg, struct pim_msdp_mg_mbr *mbr)
977d71cc 1261{
d62a17ae 1262 /* Delete active peer session if any */
1263 if (mbr->mp) {
c72d97a5 1264 pim_msdp_peer_del(&mbr->mp);
d62a17ae 1265 }
977d71cc 1266
d62a17ae 1267 listnode_delete(mg->mbr_list, mbr);
1268 if (PIM_DEBUG_MSDP_EVENTS) {
1269 char ip_str[INET_ADDRSTRLEN];
1270 pim_inet4_dump("<mbr?>", mbr->mbr_ip, ip_str, sizeof(ip_str));
1271 zlog_debug("MSDP mesh-group %s mbr %s deleted",
1272 mg->mesh_group_name, ip_str);
1273 }
1274 pim_msdp_mg_mbr_free(mbr);
1275 if (mg->mbr_cnt) {
1276 --mg->mbr_cnt;
1277 }
977d71cc 1278}
1279
fb35f654 1280static void pim_msdp_src_del(struct pim_msdp_mg *mg)
d62a17ae 1281{
1282 struct pim_msdp_mg_mbr *mbr;
1283 struct listnode *mbr_node;
d62a17ae 1284
1285 /* SIP is being removed - tear down all active peer sessions */
1286 for (ALL_LIST_ELEMENTS_RO(mg->mbr_list, mbr_node, mbr)) {
56697b3e 1287 if (mbr->mp)
c72d97a5 1288 pim_msdp_peer_del(&mbr->mp);
d62a17ae 1289 }
1290 if (PIM_DEBUG_MSDP_EVENTS) {
1291 zlog_debug("MSDP mesh-group %s src cleared",
1292 mg->mesh_group_name);
1293 }
1294}
1295
3c72d654 1296/*********************** MSDP feature APIs *********************************/
84f25989
DS
1297int pim_msdp_config_write(struct pim_instance *pim, struct vty *vty,
1298 const char *spaces)
d62a17ae 1299{
e2809e61 1300 struct pim_msdp_mg *mg;
d62a17ae 1301 struct listnode *mbrnode;
1302 struct pim_msdp_mg_mbr *mbr;
d62a17ae 1303 char src_str[INET_ADDRSTRLEN];
1304 int count = 0;
1305
e2809e61 1306 if (SLIST_EMPTY(&pim->msdp.mglist))
d62a17ae 1307 return count;
d62a17ae 1308
e2809e61
RZ
1309 SLIST_FOREACH (mg, &pim->msdp.mglist, mg_entry) {
1310 if (mg->src_ip.s_addr != INADDR_ANY) {
1311 pim_inet4_dump("<src?>", mg->src_ip, src_str,
1312 sizeof(src_str));
1313 vty_out(vty, "%sip msdp mesh-group %s source %s\n",
1314 spaces, mg->mesh_group_name, src_str);
1315 ++count;
1316 }
d62a17ae 1317
e2809e61 1318 for (ALL_LIST_ELEMENTS_RO(mg->mbr_list, mbrnode, mbr)) {
97495b70
CS
1319 vty_out(vty, "%sip msdp mesh-group %s member %pI4\n",
1320 spaces, mg->mesh_group_name, &mbr->mbr_ip);
e2809e61
RZ
1321 ++count;
1322 }
d62a17ae 1323 }
e2809e61 1324
d62a17ae 1325 return count;
472ad383
DS
1326}
1327
8c70c9e2
RZ
1328bool pim_msdp_peer_config_write(struct vty *vty, struct pim_instance *pim,
1329 const char *spaces)
1330{
1331 struct pim_msdp_peer *mp;
1332 struct listnode *node;
1333 bool written = false;
1334
1335 for (ALL_LIST_ELEMENTS_RO(pim->msdp.peer_list, node, mp)) {
0ce04a08
RZ
1336 /* Skip meshed group peers. */
1337 if (mp->flags & PIM_MSDP_PEERF_IN_GROUP)
8c70c9e2
RZ
1338 continue;
1339
1340 vty_out(vty, "%sip msdp peer %pI4 source %pI4\n", spaces,
1341 &mp->peer, &mp->local);
1342 written = true;
1343 }
1344
1345 return written;
1346}
1347
3c72d654 1348/* Enable feature including active/periodic timers etc. on the first peer
1349 * config. Till then MSDP should just stay quiet. */
472ad383 1350static void pim_msdp_enable(struct pim_instance *pim)
3c72d654 1351{
472ad383 1352 if (pim->msdp.flags & PIM_MSDPF_ENABLE) {
d62a17ae 1353 /* feature is already enabled */
1354 return;
1355 }
472ad383
DS
1356 pim->msdp.flags |= PIM_MSDPF_ENABLE;
1357 pim->msdp.work_obuf = stream_new(PIM_MSDP_MAX_PACKET_SIZE);
1358 pim_msdp_sa_adv_timer_setup(pim, true /* start */);
d62a17ae 1359 /* setup sa cache based on local sources */
472ad383 1360 pim_msdp_sa_local_setup(pim);
3c72d654 1361}
1362
2a333e0f 1363/* MSDP init */
64c86530 1364void pim_msdp_init(struct pim_instance *pim, struct thread_master *master)
2a333e0f 1365{
472ad383 1366 pim->msdp.master = master;
9fb302f4 1367 char hash_name[64];
3c72d654 1368
772270f3
QY
1369 snprintf(hash_name, sizeof(hash_name), "PIM %s MSDP Peer Hash",
1370 pim->vrf->name);
2ad78035 1371 pim->msdp.peer_hash = hash_create(pim_msdp_peer_hash_key_make,
9fb302f4 1372 pim_msdp_peer_hash_eq, hash_name);
2ad78035
DS
1373 pim->msdp.peer_list = list_new();
1374 pim->msdp.peer_list->del = (void (*)(void *))pim_msdp_peer_free;
1375 pim->msdp.peer_list->cmp = (int (*)(void *, void *))pim_msdp_peer_comp;
3c72d654 1376
772270f3
QY
1377 snprintf(hash_name, sizeof(hash_name), "PIM %s MSDP SA Hash",
1378 pim->vrf->name);
2ad78035 1379 pim->msdp.sa_hash = hash_create(pim_msdp_sa_hash_key_make,
9fb302f4 1380 pim_msdp_sa_hash_eq, hash_name);
2ad78035
DS
1381 pim->msdp.sa_list = list_new();
1382 pim->msdp.sa_list->del = (void (*)(void *))pim_msdp_sa_free;
1383 pim->msdp.sa_list->cmp = (int (*)(void *, void *))pim_msdp_sa_comp;
2a333e0f 1384}
1385
1386/* counterpart to MSDP init; XXX: unused currently */
2ad78035 1387void pim_msdp_exit(struct pim_instance *pim)
2a333e0f 1388{
e2809e61 1389 struct pim_msdp_mg *mg;
ec2f0e53 1390
e2809e61 1391 pim_msdp_sa_adv_timer_setup(pim, false);
2a333e0f 1392
e2809e61
RZ
1393 /* Stop listener and delete all peer sessions */
1394 while ((mg = SLIST_FIRST(&pim->msdp.mglist)) != NULL)
1395 pim_msdp_mg_free(pim, &mg);
822d3c85 1396
2ad78035 1397 if (pim->msdp.peer_hash) {
36ef2c10 1398 hash_clean(pim->msdp.peer_hash, NULL);
2ad78035
DS
1399 hash_free(pim->msdp.peer_hash);
1400 pim->msdp.peer_hash = NULL;
d62a17ae 1401 }
2a333e0f 1402
2ad78035 1403 if (pim->msdp.peer_list) {
6a154c88 1404 list_delete(&pim->msdp.peer_list);
d62a17ae 1405 }
3da8a099
DS
1406
1407 if (pim->msdp.sa_hash) {
36ef2c10 1408 hash_clean(pim->msdp.sa_hash, NULL);
3da8a099
DS
1409 hash_free(pim->msdp.sa_hash);
1410 pim->msdp.sa_hash = NULL;
1411 }
1412
1413 if (pim->msdp.sa_list) {
6a154c88 1414 list_delete(&pim->msdp.sa_list);
3da8a099 1415 }
36ef2c10
DS
1416
1417 if (pim->msdp.work_obuf)
1418 stream_free(pim->msdp.work_obuf);
1419 pim->msdp.work_obuf = NULL;
11128587 1420}
e2809e61 1421
fb35f654
RZ
1422void pim_msdp_mg_src_add(struct pim_instance *pim, struct pim_msdp_mg *mg,
1423 struct in_addr *ai)
e2809e61
RZ
1424{
1425 struct pim_msdp_mg_mbr *mbr;
1426 struct listnode *mbr_node;
1427
1428 /* Stop all connections and remove data structures. */
fb35f654 1429 pim_msdp_src_del(mg);
e2809e61
RZ
1430
1431 /* Set new address. */
1432 mg->src_ip = *ai;
1433
1434 /* No new address, disable everyone. */
1435 if (ai->s_addr == INADDR_ANY) {
1436 if (PIM_DEBUG_MSDP_EVENTS)
1437 zlog_debug("MSDP mesh-group %s src unset",
1438 mg->mesh_group_name);
1439 return;
1440 }
1441
1442 /* Create data structures and start TCP connection. */
1443 for (ALL_LIST_ELEMENTS_RO(mg->mbr_list, mbr_node, mbr))
c72d97a5 1444 mbr->mp = pim_msdp_peer_add(pim, &mbr->mbr_ip, &mg->src_ip,
56697b3e 1445 mg->mesh_group_name);
e2809e61
RZ
1446
1447 if (PIM_DEBUG_MSDP_EVENTS)
1448 zlog_debug("MSDP mesh-group %s src %pI4 set",
1449 mg->mesh_group_name, &mg->src_ip);
1450}
1451
fb35f654
RZ
1452struct pim_msdp_mg_mbr *pim_msdp_mg_mbr_add(struct pim_instance *pim,
1453 struct pim_msdp_mg *mg,
1454 struct in_addr *ia)
e2809e61
RZ
1455{
1456 struct pim_msdp_mg_mbr *mbr;
1457
1458 mbr = XCALLOC(MTYPE_PIM_MSDP_MG_MBR, sizeof(*mbr));
1459 mbr->mbr_ip = *ia;
1460 listnode_add_sort(mg->mbr_list, mbr);
1461
1462 /* if valid SIP has been configured add peer session */
1463 if (mg->src_ip.s_addr != INADDR_ANY)
c72d97a5 1464 mbr->mp = pim_msdp_peer_add(pim, &mbr->mbr_ip, &mg->src_ip,
56697b3e 1465 mg->mesh_group_name);
e2809e61
RZ
1466
1467 if (PIM_DEBUG_MSDP_EVENTS)
1468 zlog_debug("MSDP mesh-group %s mbr %pI4 created",
1469 mg->mesh_group_name, &mbr->mbr_ip);
1470
1471 ++mg->mbr_cnt;
1472
1473 return mbr;
1474}