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