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