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