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