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