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