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