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