]>
Commit | Line | Data |
---|---|---|
8f5f5e91 DS |
1 | /* |
2 | * PIM for Quagga | |
3 | * Copyright (C) 2015 Cumulus Networks, Inc. | |
4 | * Donald Sharp | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; either version 2 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, but | |
12 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | * General Public License for more details. | |
896014f4 DL |
15 | * |
16 | * You should have received a copy of the GNU General Public License along | |
17 | * with this program; see the file COPYING; if not, write to the Free Software | |
18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
8f5f5e91 DS |
19 | */ |
20 | #include <zebra.h> | |
21 | ||
9bf3c633 | 22 | #include "lib/json.h" |
54b97c74 | 23 | #include "log.h" |
8f5f5e91 | 24 | #include "network.h" |
744d91b3 | 25 | #include "if.h" |
36d6bd7d DS |
26 | #include "linklist.h" |
27 | #include "prefix.h" | |
28 | #include "memory.h" | |
00d07c6f DS |
29 | #include "vty.h" |
30 | #include "vrf.h" | |
dfe43e25 | 31 | #include "plist.h" |
cba44481 | 32 | #include "nexthop.h" |
89b68082 | 33 | #include "table.h" |
3613d898 | 34 | #include "lib_errors.h" |
8f5f5e91 DS |
35 | |
36 | #include "pimd.h" | |
75a26779 | 37 | #include "pim_vty.h" |
54b97c74 | 38 | #include "pim_str.h" |
7176984f | 39 | #include "pim_iface.h" |
8f5f5e91 | 40 | #include "pim_rp.h" |
ed66602c DS |
41 | #include "pim_str.h" |
42 | #include "pim_rpf.h" | |
13afbd05 | 43 | #include "pim_sock.h" |
36d6bd7d | 44 | #include "pim_memory.h" |
00d07c6f | 45 | #include "pim_iface.h" |
7667c556 | 46 | #include "pim_msdp.h" |
1bc98276 | 47 | #include "pim_nht.h" |
28309df0 SP |
48 | #include "pim_mroute.h" |
49 | #include "pim_oil.h" | |
50 | #include "pim_zebra.h" | |
d0e418b4 | 51 | #include "pim_bsm.h" |
36d6bd7d | 52 | |
c2cf4b02 DS |
53 | /* Cleanup pim->rpf_hash each node data */ |
54 | void pim_rp_list_hash_clean(void *data) | |
55 | { | |
7c591950 | 56 | struct pim_nexthop_cache *pnc = (struct pim_nexthop_cache *)data; |
c2cf4b02 | 57 | |
6a154c88 | 58 | list_delete(&pnc->rp_list); |
6e1ef388 | 59 | |
7c591950 DS |
60 | hash_clean(pnc->upstream_hash, NULL); |
61 | hash_free(pnc->upstream_hash); | |
62 | pnc->upstream_hash = NULL; | |
1159c202 SP |
63 | if (pnc->nexthop) |
64 | nexthops_free(pnc->nexthop); | |
6e1ef388 DS |
65 | |
66 | XFREE(MTYPE_PIM_NEXTHOP_CACHE, pnc); | |
c2cf4b02 DS |
67 | } |
68 | ||
d62a17ae | 69 | static void pim_rp_info_free(struct rp_info *rp_info) |
36d6bd7d | 70 | { |
0a22ddfb | 71 | XFREE(MTYPE_PIM_FILTER_NAME, rp_info->plist); |
ff823fc9 | 72 | |
d62a17ae | 73 | XFREE(MTYPE_PIM_RP, rp_info); |
36d6bd7d DS |
74 | } |
75 | ||
d62a17ae | 76 | int pim_rp_list_cmp(void *v1, void *v2) |
36d6bd7d | 77 | { |
d62a17ae | 78 | struct rp_info *rp1 = (struct rp_info *)v1; |
79 | struct rp_info *rp2 = (struct rp_info *)v2; | |
80 | ||
81 | /* | |
82 | * Sort by RP IP address | |
83 | */ | |
84 | if (rp1->rp.rpf_addr.u.prefix4.s_addr | |
85 | < rp2->rp.rpf_addr.u.prefix4.s_addr) | |
86 | return -1; | |
87 | ||
88 | if (rp1->rp.rpf_addr.u.prefix4.s_addr | |
89 | > rp2->rp.rpf_addr.u.prefix4.s_addr) | |
90 | return 1; | |
91 | ||
92 | /* | |
93 | * Sort by group IP address | |
94 | */ | |
95 | if (rp1->group.u.prefix4.s_addr < rp2->group.u.prefix4.s_addr) | |
96 | return -1; | |
97 | ||
98 | if (rp1->group.u.prefix4.s_addr > rp2->group.u.prefix4.s_addr) | |
99 | return 1; | |
100 | ||
101 | return 0; | |
36d6bd7d DS |
102 | } |
103 | ||
d9c9a9ee | 104 | void pim_rp_init(struct pim_instance *pim) |
36d6bd7d | 105 | { |
d62a17ae | 106 | struct rp_info *rp_info; |
89b68082 | 107 | struct route_node *rn; |
36d6bd7d | 108 | |
d9c9a9ee DS |
109 | pim->rp_list = list_new(); |
110 | pim->rp_list->del = (void (*)(void *))pim_rp_info_free; | |
111 | pim->rp_list->cmp = pim_rp_list_cmp; | |
36d6bd7d | 112 | |
89b68082 | 113 | pim->rp_table = route_table_init(); |
89b68082 | 114 | |
d62a17ae | 115 | rp_info = XCALLOC(MTYPE_PIM_RP, sizeof(*rp_info)); |
36d6bd7d | 116 | |
2e8345c1 | 117 | if (!str2prefix("224.0.0.0/4", &rp_info->group)) { |
450971aa | 118 | flog_err(EC_LIB_DEVELOPMENT, |
1c50c1c0 | 119 | "Unable to convert 224.0.0.0/4 to prefix"); |
6a154c88 | 120 | list_delete(&pim->rp_list); |
89b68082 | 121 | route_table_finish(pim->rp_table); |
2e8345c1 DS |
122 | XFREE(MTYPE_PIM_RP, rp_info); |
123 | return; | |
124 | } | |
d62a17ae | 125 | rp_info->group.family = AF_INET; |
126 | rp_info->rp.rpf_addr.family = AF_INET; | |
127 | rp_info->rp.rpf_addr.prefixlen = IPV4_MAX_PREFIXLEN; | |
128 | rp_info->rp.rpf_addr.u.prefix4.s_addr = INADDR_NONE; | |
36d6bd7d | 129 | |
d9c9a9ee | 130 | listnode_add(pim->rp_list, rp_info); |
89b68082 DS |
131 | |
132 | rn = route_node_get(pim->rp_table, &rp_info->group); | |
89b68082 | 133 | rn->info = rp_info; |
23fc858a | 134 | if (PIM_DEBUG_PIM_TRACE) |
996c9314 LB |
135 | zlog_debug( |
136 | "Allocated: %p for rp_info: %p(224.0.0.0/4) Lock: %d", | |
137 | rn, rp_info, rn->lock); | |
36d6bd7d DS |
138 | } |
139 | ||
d9c9a9ee | 140 | void pim_rp_free(struct pim_instance *pim) |
36d6bd7d | 141 | { |
d9c9a9ee | 142 | if (pim->rp_list) |
6a154c88 | 143 | list_delete(&pim->rp_list); |
36d6bd7d DS |
144 | } |
145 | ||
dfe43e25 DW |
146 | /* |
147 | * Given an RP's prefix-list, return the RP's rp_info for that prefix-list | |
148 | */ | |
d9c9a9ee DS |
149 | static struct rp_info *pim_rp_find_prefix_list(struct pim_instance *pim, |
150 | struct in_addr rp, | |
d62a17ae | 151 | const char *plist) |
36d6bd7d | 152 | { |
d62a17ae | 153 | struct listnode *node; |
154 | struct rp_info *rp_info; | |
155 | ||
d9c9a9ee | 156 | for (ALL_LIST_ELEMENTS_RO(pim->rp_list, node, rp_info)) { |
d62a17ae | 157 | if (rp.s_addr == rp_info->rp.rpf_addr.u.prefix4.s_addr |
158 | && rp_info->plist && strcmp(rp_info->plist, plist) == 0) { | |
159 | return rp_info; | |
160 | } | |
161 | } | |
162 | ||
163 | return NULL; | |
36d6bd7d DS |
164 | } |
165 | ||
dfe43e25 DW |
166 | /* |
167 | * Return true if plist is used by any rp_info | |
168 | */ | |
d9c9a9ee | 169 | static int pim_rp_prefix_list_used(struct pim_instance *pim, const char *plist) |
dfe43e25 | 170 | { |
d62a17ae | 171 | struct listnode *node; |
172 | struct rp_info *rp_info; | |
173 | ||
d9c9a9ee | 174 | for (ALL_LIST_ELEMENTS_RO(pim->rp_list, node, rp_info)) { |
d62a17ae | 175 | if (rp_info->plist && strcmp(rp_info->plist, plist) == 0) { |
176 | return 1; | |
177 | } | |
178 | } | |
179 | ||
180 | return 0; | |
dfe43e25 DW |
181 | } |
182 | ||
183 | /* | |
d62a17ae | 184 | * Given an RP's address, return the RP's rp_info that is an exact match for |
185 | * 'group' | |
dfe43e25 | 186 | */ |
d9c9a9ee DS |
187 | static struct rp_info *pim_rp_find_exact(struct pim_instance *pim, |
188 | struct in_addr rp, | |
123214ef | 189 | const struct prefix *group) |
36d6bd7d | 190 | { |
d62a17ae | 191 | struct listnode *node; |
192 | struct rp_info *rp_info; | |
36d6bd7d | 193 | |
d9c9a9ee | 194 | for (ALL_LIST_ELEMENTS_RO(pim->rp_list, node, rp_info)) { |
d62a17ae | 195 | if (rp.s_addr == rp_info->rp.rpf_addr.u.prefix4.s_addr |
196 | && prefix_same(&rp_info->group, group)) | |
197 | return rp_info; | |
198 | } | |
54b97c74 | 199 | |
d62a17ae | 200 | return NULL; |
36d6bd7d DS |
201 | } |
202 | ||
dfe43e25 DW |
203 | /* |
204 | * Given a group, return the rp_info for that group | |
205 | */ | |
28309df0 | 206 | struct rp_info *pim_rp_find_match_group(struct pim_instance *pim, |
123214ef | 207 | const struct prefix *group) |
36d6bd7d | 208 | { |
d62a17ae | 209 | struct listnode *node; |
b77b1550 | 210 | struct rp_info *best = NULL; |
d62a17ae | 211 | struct rp_info *rp_info; |
212 | struct prefix_list *plist; | |
123214ef | 213 | const struct prefix *p, *bp; |
89b68082 | 214 | struct route_node *rn; |
d62a17ae | 215 | |
4646b86a | 216 | bp = NULL; |
d9c9a9ee | 217 | for (ALL_LIST_ELEMENTS_RO(pim->rp_list, node, rp_info)) { |
d62a17ae | 218 | if (rp_info->plist) { |
219 | plist = prefix_list_lookup(AFI_IP, rp_info->plist); | |
220 | ||
996c9314 LB |
221 | if (prefix_list_apply_which_prefix(plist, &p, group) |
222 | == PREFIX_DENY) | |
b77b1550 DS |
223 | continue; |
224 | ||
225 | if (!best) { | |
226 | best = rp_info; | |
227 | bp = p; | |
228 | continue; | |
229 | } | |
230 | ||
4646b86a | 231 | if (bp && bp->prefixlen < p->prefixlen) { |
b77b1550 DS |
232 | best = rp_info; |
233 | bp = p; | |
234 | } | |
d62a17ae | 235 | } |
236 | } | |
237 | ||
89b68082 DS |
238 | rn = route_node_match(pim->rp_table, group); |
239 | if (!rn) { | |
af4c2728 | 240 | flog_err( |
450971aa | 241 | EC_LIB_DEVELOPMENT, |
996c9314 LB |
242 | "%s: BUG We should have found default group information\n", |
243 | __PRETTY_FUNCTION__); | |
89b68082 DS |
244 | return best; |
245 | } | |
246 | ||
247 | rp_info = rn->info; | |
23fc858a | 248 | if (PIM_DEBUG_PIM_TRACE) { |
89b68082 DS |
249 | char buf[PREFIX_STRLEN]; |
250 | ||
251 | route_unlock_node(rn); | |
996c9314 LB |
252 | zlog_debug("Lookedup: %p for rp_info: %p(%s) Lock: %d", rn, |
253 | rp_info, | |
89b68082 DS |
254 | prefix2str(&rp_info->group, buf, sizeof(buf)), |
255 | rn->lock); | |
256 | } | |
257 | ||
258 | if (!best) | |
259 | return rp_info; | |
260 | ||
261 | if (rp_info->group.prefixlen < best->group.prefixlen) | |
262 | best = rp_info; | |
263 | ||
b77b1550 | 264 | return best; |
36d6bd7d | 265 | } |
75a26779 | 266 | |
dfe43e25 DW |
267 | /* |
268 | * When the user makes "ip pim rp" configuration changes or if they change the | |
269 | * prefix-list(s) used by these statements we must tickle the upstream state | |
270 | * for each group to make them re-lookup who their RP should be. | |
271 | * | |
272 | * This is a placeholder function for now. | |
273 | */ | |
472ad383 | 274 | static void pim_rp_refresh_group_to_rp_mapping(struct pim_instance *pim) |
dfe43e25 | 275 | { |
472ad383 | 276 | pim_msdp_i_am_rp_changed(pim); |
dfe43e25 DW |
277 | } |
278 | ||
d9c9a9ee DS |
279 | void pim_rp_prefix_list_update(struct pim_instance *pim, |
280 | struct prefix_list *plist) | |
dfe43e25 | 281 | { |
d62a17ae | 282 | struct listnode *node; |
283 | struct rp_info *rp_info; | |
284 | int refresh_needed = 0; | |
285 | ||
d9c9a9ee | 286 | for (ALL_LIST_ELEMENTS_RO(pim->rp_list, node, rp_info)) { |
d62a17ae | 287 | if (rp_info->plist |
288 | && strcmp(rp_info->plist, prefix_list_name(plist)) == 0) { | |
289 | refresh_needed = 1; | |
290 | break; | |
291 | } | |
292 | } | |
293 | ||
294 | if (refresh_needed) | |
472ad383 | 295 | pim_rp_refresh_group_to_rp_mapping(pim); |
dfe43e25 DW |
296 | } |
297 | ||
d62a17ae | 298 | static int pim_rp_check_interface_addrs(struct rp_info *rp_info, |
299 | struct pim_interface *pim_ifp) | |
7176984f | 300 | { |
d62a17ae | 301 | struct listnode *node; |
302 | struct pim_secondary_addr *sec_addr; | |
7176984f | 303 | |
d62a17ae | 304 | if (pim_ifp->primary_address.s_addr |
305 | == rp_info->rp.rpf_addr.u.prefix4.s_addr) | |
306 | return 1; | |
7176984f | 307 | |
d62a17ae | 308 | if (!pim_ifp->sec_addr_list) { |
309 | return 0; | |
310 | } | |
7176984f | 311 | |
d62a17ae | 312 | for (ALL_LIST_ELEMENTS_RO(pim_ifp->sec_addr_list, node, sec_addr)) { |
313 | if (prefix_same(&sec_addr->addr, &rp_info->rp.rpf_addr)) { | |
314 | return 1; | |
315 | } | |
316 | } | |
7176984f | 317 | |
d62a17ae | 318 | return 0; |
7176984f | 319 | } |
320 | ||
fec883d9 DS |
321 | static void pim_rp_check_interfaces(struct pim_instance *pim, |
322 | struct rp_info *rp_info) | |
00d07c6f | 323 | { |
d62a17ae | 324 | struct interface *ifp; |
00d07c6f | 325 | |
d62a17ae | 326 | rp_info->i_am_rp = 0; |
451fda4f | 327 | FOR_ALL_INTERFACES (pim->vrf, ifp) { |
d62a17ae | 328 | struct pim_interface *pim_ifp = ifp->info; |
00d07c6f | 329 | |
d62a17ae | 330 | if (!pim_ifp) |
331 | continue; | |
00d07c6f | 332 | |
d62a17ae | 333 | if (pim_rp_check_interface_addrs(rp_info, pim_ifp)) { |
334 | rp_info->i_am_rp = 1; | |
335 | } | |
336 | } | |
00d07c6f DS |
337 | } |
338 | ||
28309df0 SP |
339 | void pim_upstream_update(struct pim_instance *pim, struct pim_upstream *up) |
340 | { | |
341 | struct pim_rpf old_rpf; | |
342 | enum pim_rpf_result rpf_result; | |
343 | struct in_addr old_upstream_addr; | |
344 | struct in_addr new_upstream_addr; | |
345 | struct prefix nht_p; | |
346 | ||
347 | old_upstream_addr = up->upstream_addr; | |
348 | pim_rp_set_upstream_addr(pim, &new_upstream_addr, up->sg.src, | |
349 | up->sg.grp); | |
350 | ||
23fc858a | 351 | if (PIM_DEBUG_PIM_TRACE) |
28309df0 SP |
352 | zlog_debug("%s: pim upstream update for old upstream %s", |
353 | __PRETTY_FUNCTION__, | |
354 | inet_ntoa(old_upstream_addr)); | |
355 | ||
356 | if (old_upstream_addr.s_addr == new_upstream_addr.s_addr) | |
357 | return; | |
358 | ||
359 | /* Lets consider a case, where a PIM upstream has a better RP as a | |
360 | * result of a new RP configuration with more precise group range. | |
361 | * This upstream has to be added to the upstream hash of new RP's | |
362 | * NHT(pnc) and has to be removed from old RP's NHT upstream hash | |
363 | */ | |
364 | if (old_upstream_addr.s_addr != INADDR_ANY) { | |
365 | /* Deregister addr with Zebra NHT */ | |
366 | nht_p.family = AF_INET; | |
367 | nht_p.prefixlen = IPV4_MAX_BITLEN; | |
368 | nht_p.u.prefix4 = old_upstream_addr; | |
23fc858a | 369 | if (PIM_DEBUG_PIM_TRACE) { |
28309df0 SP |
370 | char buf[PREFIX2STR_BUFFER]; |
371 | ||
372 | prefix2str(&nht_p, buf, sizeof(buf)); | |
373 | zlog_debug("%s: Deregister upstream %s addr %s with Zebra NHT", | |
374 | __PRETTY_FUNCTION__, up->sg_str, buf); | |
375 | } | |
4533b847 | 376 | pim_delete_tracked_nexthop(pim, &nht_p, up, NULL, false); |
28309df0 SP |
377 | } |
378 | ||
379 | /* Update the upstream address */ | |
380 | up->upstream_addr = new_upstream_addr; | |
381 | ||
382 | old_rpf.source_nexthop.interface = up->rpf.source_nexthop.interface; | |
383 | ||
ae14da48 | 384 | rpf_result = pim_rpf_update(pim, up, &old_rpf); |
28309df0 SP |
385 | if (rpf_result == PIM_RPF_FAILURE) |
386 | pim_mroute_del(up->channel_oil, __PRETTY_FUNCTION__); | |
387 | ||
388 | /* update kernel multicast forwarding cache (MFC) */ | |
389 | if (up->rpf.source_nexthop.interface && up->channel_oil) { | |
390 | ifindex_t ifindex = up->rpf.source_nexthop.interface->ifindex; | |
391 | int vif_index = pim_if_find_vifindex_by_ifindex(pim, ifindex); | |
392 | /* Pass Current selected NH vif index to mroute download */ | |
393 | if (vif_index) | |
394 | pim_scan_individual_oil(up->channel_oil, vif_index); | |
395 | else { | |
396 | if (PIM_DEBUG_PIM_NHT) | |
397 | zlog_debug( | |
398 | "%s: NHT upstream %s channel_oil IIF %s vif_index is not valid", | |
399 | __PRETTY_FUNCTION__, up->sg_str, | |
400 | up->rpf.source_nexthop.interface->name); | |
401 | } | |
402 | } | |
403 | ||
404 | if (rpf_result == PIM_RPF_CHANGED) | |
405 | pim_zebra_upstream_rpf_changed(pim, up, &old_rpf); | |
406 | ||
407 | pim_zebra_update_all_interfaces(pim); | |
408 | } | |
409 | ||
d0e418b4 | 410 | int pim_rp_new_config(struct pim_instance *pim, const char *rp, |
411 | const char *group_range, const char *plist) | |
75a26779 | 412 | { |
d62a17ae | 413 | int result = 0; |
d0e418b4 | 414 | struct prefix group; |
415 | struct in_addr rp_addr; | |
416 | ||
417 | if (group_range == NULL) | |
418 | result = str2prefix("224.0.0.0/4", &group); | |
419 | else { | |
420 | result = str2prefix(group_range, &group); | |
421 | if (result) { | |
422 | struct prefix temp; | |
423 | ||
424 | prefix_copy(&temp, &group); | |
425 | apply_mask(&temp); | |
426 | if (!prefix_same(&group, &temp)) | |
427 | return PIM_GROUP_BAD_ADDR_MASK_COMBO; | |
428 | } | |
429 | } | |
430 | ||
431 | if (!result) | |
432 | return PIM_GROUP_BAD_ADDRESS; | |
433 | ||
434 | result = inet_pton(AF_INET, rp, &rp_addr); | |
435 | ||
436 | if (result <= 0) | |
437 | return PIM_RP_BAD_ADDRESS; | |
438 | ||
439 | result = pim_rp_new(pim, rp_addr, group, plist, RP_SRC_STATIC); | |
440 | return result; | |
441 | } | |
442 | ||
443 | int pim_rp_new(struct pim_instance *pim, struct in_addr rp_addr, | |
444 | struct prefix group, const char *plist, | |
445 | enum rp_source rp_src_flag) | |
446 | { | |
447 | int result = 0; | |
448 | char rp[INET_ADDRSTRLEN]; | |
d62a17ae | 449 | struct rp_info *rp_info; |
450 | struct rp_info *rp_all; | |
451 | struct prefix group_all; | |
452 | struct listnode *node, *nnode; | |
453 | struct rp_info *tmp_rp_info; | |
454 | char buffer[BUFSIZ]; | |
455 | struct prefix nht_p; | |
89b68082 | 456 | struct route_node *rn; |
28309df0 SP |
457 | struct pim_upstream *up; |
458 | struct listnode *upnode; | |
d62a17ae | 459 | |
1e0d1c25 DS |
460 | if (rp_addr.s_addr == INADDR_ANY || |
461 | rp_addr.s_addr == INADDR_NONE) | |
462 | return PIM_RP_BAD_ADDRESS; | |
463 | ||
d62a17ae | 464 | rp_info = XCALLOC(MTYPE_PIM_RP, sizeof(*rp_info)); |
d62a17ae | 465 | |
d62a17ae | 466 | rp_info->rp.rpf_addr.family = AF_INET; |
467 | rp_info->rp.rpf_addr.prefixlen = IPV4_MAX_PREFIXLEN; | |
d0e418b4 | 468 | rp_info->rp.rpf_addr.u.prefix4 = rp_addr; |
469 | prefix_copy(&rp_info->group, &group); | |
470 | rp_info->rp_src = rp_src_flag; | |
d62a17ae | 471 | |
d0e418b4 | 472 | inet_ntop(AF_INET, &rp_info->rp.rpf_addr.u.prefix4, rp, sizeof(rp)); |
d62a17ae | 473 | |
474 | if (plist) { | |
475 | /* | |
476 | * Return if the prefix-list is already configured for this RP | |
477 | */ | |
d9c9a9ee | 478 | if (pim_rp_find_prefix_list(pim, rp_info->rp.rpf_addr.u.prefix4, |
d62a17ae | 479 | plist)) { |
480 | XFREE(MTYPE_PIM_RP, rp_info); | |
481 | return PIM_SUCCESS; | |
482 | } | |
483 | ||
484 | /* | |
485 | * Barf if the prefix-list is already configured for an RP | |
486 | */ | |
d9c9a9ee | 487 | if (pim_rp_prefix_list_used(pim, plist)) { |
d62a17ae | 488 | XFREE(MTYPE_PIM_RP, rp_info); |
489 | return PIM_RP_PFXLIST_IN_USE; | |
490 | } | |
491 | ||
492 | /* | |
493 | * Free any existing rp_info entries for this RP | |
494 | */ | |
d9c9a9ee | 495 | for (ALL_LIST_ELEMENTS(pim->rp_list, node, nnode, |
d62a17ae | 496 | tmp_rp_info)) { |
497 | if (rp_info->rp.rpf_addr.u.prefix4.s_addr | |
498 | == tmp_rp_info->rp.rpf_addr.u.prefix4.s_addr) { | |
499 | if (tmp_rp_info->plist) | |
d0e418b4 | 500 | pim_rp_del_config(pim, rp, NULL, |
501 | tmp_rp_info->plist); | |
d62a17ae | 502 | else |
d0e418b4 | 503 | pim_rp_del_config( |
fec883d9 | 504 | pim, rp, |
d62a17ae | 505 | prefix2str(&tmp_rp_info->group, |
506 | buffer, BUFSIZ), | |
507 | NULL); | |
508 | } | |
509 | } | |
510 | ||
511 | rp_info->plist = XSTRDUP(MTYPE_PIM_FILTER_NAME, plist); | |
512 | } else { | |
89b68082 | 513 | |
2e8345c1 DS |
514 | if (!str2prefix("224.0.0.0/4", &group_all)) { |
515 | XFREE(MTYPE_PIM_RP, rp_info); | |
516 | return PIM_GROUP_BAD_ADDRESS; | |
517 | } | |
d9c9a9ee | 518 | rp_all = pim_rp_find_match_group(pim, &group_all); |
d62a17ae | 519 | |
520 | /* | |
521 | * Barf if group is a non-multicast subnet | |
522 | */ | |
523 | if (!prefix_match(&rp_all->group, &rp_info->group)) { | |
524 | XFREE(MTYPE_PIM_RP, rp_info); | |
525 | return PIM_GROUP_BAD_ADDRESS; | |
526 | } | |
527 | ||
528 | /* | |
529 | * Remove any prefix-list rp_info entries for this RP | |
530 | */ | |
d9c9a9ee | 531 | for (ALL_LIST_ELEMENTS(pim->rp_list, node, nnode, |
d62a17ae | 532 | tmp_rp_info)) { |
533 | if (tmp_rp_info->plist | |
534 | && rp_info->rp.rpf_addr.u.prefix4.s_addr | |
535 | == tmp_rp_info->rp.rpf_addr.u.prefix4 | |
536 | .s_addr) { | |
d0e418b4 | 537 | pim_rp_del_config(pim, rp, NULL, |
538 | tmp_rp_info->plist); | |
d62a17ae | 539 | } |
540 | } | |
541 | ||
542 | /* | |
543 | * Take over the 224.0.0.0/4 group if the rp is INADDR_NONE | |
544 | */ | |
545 | if (prefix_same(&rp_all->group, &rp_info->group) | |
546 | && pim_rpf_addr_is_inaddr_none(&rp_all->rp)) { | |
547 | rp_all->rp.rpf_addr = rp_info->rp.rpf_addr; | |
d0e418b4 | 548 | rp_all->rp_src = rp_src_flag; |
d62a17ae | 549 | XFREE(MTYPE_PIM_RP, rp_info); |
550 | ||
551 | /* Register addr with Zebra NHT */ | |
552 | nht_p.family = AF_INET; | |
553 | nht_p.prefixlen = IPV4_MAX_BITLEN; | |
554 | nht_p.u.prefix4 = | |
555 | rp_all->rp.rpf_addr.u.prefix4; // RP address | |
3d225d48 | 556 | if (PIM_DEBUG_PIM_NHT_RP) { |
d62a17ae | 557 | char buf[PREFIX2STR_BUFFER]; |
558 | char buf1[PREFIX2STR_BUFFER]; | |
559 | prefix2str(&nht_p, buf, sizeof(buf)); | |
560 | prefix2str(&rp_all->group, buf1, sizeof(buf1)); | |
561 | zlog_debug( | |
562 | "%s: NHT Register rp_all addr %s grp %s ", | |
563 | __PRETTY_FUNCTION__, buf, buf1); | |
564 | } | |
28309df0 SP |
565 | |
566 | for (ALL_LIST_ELEMENTS_RO(pim->upstream_list, upnode, | |
567 | up)) { | |
568 | /* Find (*, G) upstream whose RP is not | |
569 | * configured yet | |
570 | */ | |
571 | if ((up->upstream_addr.s_addr == INADDR_ANY) | |
572 | && (up->sg.src.s_addr == INADDR_ANY)) { | |
573 | struct prefix grp; | |
574 | struct rp_info *trp_info; | |
575 | ||
576 | grp.family = AF_INET; | |
577 | grp.prefixlen = IPV4_MAX_BITLEN; | |
578 | grp.u.prefix4 = up->sg.grp; | |
d0e418b4 | 579 | trp_info = pim_rp_find_match_group( |
580 | pim, &grp); | |
28309df0 SP |
581 | if (trp_info == rp_all) |
582 | pim_upstream_update(pim, up); | |
583 | } | |
584 | } | |
585 | ||
5bd51314 DS |
586 | pim_rp_check_interfaces(pim, rp_all); |
587 | pim_rp_refresh_group_to_rp_mapping(pim); | |
43763b11 | 588 | pim_find_or_track_nexthop(pim, &nht_p, NULL, rp_all, |
4533b847 | 589 | false, NULL); |
590 | ||
43763b11 DS |
591 | if (!pim_ecmp_nexthop_lookup(pim, |
592 | &rp_all->rp.source_nexthop, | |
593 | &nht_p, &rp_all->group, 1)) | |
594 | return PIM_RP_NO_PATH; | |
d62a17ae | 595 | return PIM_SUCCESS; |
596 | } | |
597 | ||
598 | /* | |
599 | * Return if the group is already configured for this RP | |
600 | */ | |
d0e418b4 | 601 | tmp_rp_info = pim_rp_find_exact( |
602 | pim, rp_info->rp.rpf_addr.u.prefix4, &rp_info->group); | |
603 | if (tmp_rp_info) { | |
604 | if ((tmp_rp_info->rp_src != rp_src_flag) | |
605 | && (rp_src_flag == RP_SRC_STATIC)) | |
606 | tmp_rp_info->rp_src = rp_src_flag; | |
d62a17ae | 607 | XFREE(MTYPE_PIM_RP, rp_info); |
d0e418b4 | 608 | return result; |
d62a17ae | 609 | } |
610 | ||
611 | /* | |
612 | * Barf if this group is already covered by some other RP | |
613 | */ | |
d9c9a9ee | 614 | tmp_rp_info = pim_rp_find_match_group(pim, &rp_info->group); |
d62a17ae | 615 | |
616 | if (tmp_rp_info) { | |
617 | if (tmp_rp_info->plist) { | |
618 | XFREE(MTYPE_PIM_RP, rp_info); | |
619 | return PIM_GROUP_PFXLIST_OVERLAP; | |
620 | } else { | |
621 | /* | |
622 | * If the only RP that covers this group is an | |
623 | * RP configured for | |
624 | * 224.0.0.0/4 that is fine, ignore that one. | |
625 | * For all others | |
626 | * though we must return PIM_GROUP_OVERLAP | |
627 | */ | |
89b68082 DS |
628 | if (prefix_same(&rp_info->group, |
629 | &tmp_rp_info->group)) { | |
d0e418b4 | 630 | if ((rp_src_flag == RP_SRC_STATIC) |
631 | && (tmp_rp_info->rp_src | |
632 | == RP_SRC_STATIC)) { | |
633 | XFREE(MTYPE_PIM_RP, rp_info); | |
634 | return PIM_GROUP_OVERLAP; | |
635 | } | |
636 | ||
637 | result = pim_rp_change( | |
638 | pim, | |
639 | rp_info->rp.rpf_addr.u.prefix4, | |
640 | tmp_rp_info->group, | |
641 | rp_src_flag); | |
d62a17ae | 642 | XFREE(MTYPE_PIM_RP, rp_info); |
d0e418b4 | 643 | return result; |
d62a17ae | 644 | } |
645 | } | |
646 | } | |
647 | } | |
648 | ||
d9c9a9ee | 649 | listnode_add_sort(pim->rp_list, rp_info); |
89b68082 | 650 | rn = route_node_get(pim->rp_table, &rp_info->group); |
89b68082 DS |
651 | rn->info = rp_info; |
652 | ||
23fc858a | 653 | if (PIM_DEBUG_PIM_TRACE) { |
89b68082 DS |
654 | char buf[PREFIX_STRLEN]; |
655 | ||
996c9314 LB |
656 | zlog_debug("Allocated: %p for rp_info: %p(%s) Lock: %d", rn, |
657 | rp_info, | |
89b68082 DS |
658 | prefix2str(&rp_info->group, buf, sizeof(buf)), |
659 | rn->lock); | |
660 | } | |
d62a17ae | 661 | |
28309df0 SP |
662 | for (ALL_LIST_ELEMENTS_RO(pim->upstream_list, upnode, up)) { |
663 | if (up->sg.src.s_addr == INADDR_ANY) { | |
664 | struct prefix grp; | |
665 | struct rp_info *trp_info; | |
666 | ||
667 | grp.family = AF_INET; | |
668 | grp.prefixlen = IPV4_MAX_BITLEN; | |
669 | grp.u.prefix4 = up->sg.grp; | |
670 | trp_info = pim_rp_find_match_group(pim, &grp); | |
671 | ||
672 | if (trp_info == rp_info) | |
673 | pim_upstream_update(pim, up); | |
674 | } | |
675 | } | |
676 | ||
5bd51314 DS |
677 | pim_rp_check_interfaces(pim, rp_info); |
678 | pim_rp_refresh_group_to_rp_mapping(pim); | |
679 | ||
d62a17ae | 680 | /* Register addr with Zebra NHT */ |
681 | nht_p.family = AF_INET; | |
682 | nht_p.prefixlen = IPV4_MAX_BITLEN; | |
683 | nht_p.u.prefix4 = rp_info->rp.rpf_addr.u.prefix4; | |
3d225d48 | 684 | if (PIM_DEBUG_PIM_NHT_RP) { |
d62a17ae | 685 | char buf[PREFIX2STR_BUFFER]; |
686 | char buf1[PREFIX2STR_BUFFER]; | |
687 | prefix2str(&nht_p, buf, sizeof(buf)); | |
688 | prefix2str(&rp_info->group, buf1, sizeof(buf1)); | |
689 | zlog_debug("%s: NHT Register RP addr %s grp %s with Zebra ", | |
690 | __PRETTY_FUNCTION__, buf, buf1); | |
691 | } | |
4533b847 | 692 | pim_find_or_track_nexthop(pim, &nht_p, NULL, rp_info, false, NULL); |
43763b11 DS |
693 | if (!pim_ecmp_nexthop_lookup(pim, &rp_info->rp.source_nexthop, &nht_p, |
694 | &rp_info->group, 1)) | |
695 | return PIM_RP_NO_PATH; | |
d62a17ae | 696 | |
d62a17ae | 697 | return PIM_SUCCESS; |
75a26779 DS |
698 | } |
699 | ||
d0e418b4 | 700 | int pim_rp_del_config(struct pim_instance *pim, const char *rp, |
701 | const char *group_range, const char *plist) | |
75a26779 | 702 | { |
d62a17ae | 703 | struct prefix group; |
704 | struct in_addr rp_addr; | |
d62a17ae | 705 | int result; |
d62a17ae | 706 | |
707 | if (group_range == NULL) | |
708 | result = str2prefix("224.0.0.0/4", &group); | |
709 | else | |
710 | result = str2prefix(group_range, &group); | |
711 | ||
712 | if (!result) | |
713 | return PIM_GROUP_BAD_ADDRESS; | |
714 | ||
715 | result = inet_pton(AF_INET, rp, &rp_addr); | |
716 | if (result <= 0) | |
717 | return PIM_RP_BAD_ADDRESS; | |
718 | ||
d0e418b4 | 719 | result = pim_rp_del(pim, rp_addr, group, plist, RP_SRC_STATIC); |
720 | return result; | |
721 | } | |
722 | ||
723 | int pim_rp_del(struct pim_instance *pim, struct in_addr rp_addr, | |
724 | struct prefix group, const char *plist, | |
725 | enum rp_source rp_src_flag) | |
726 | { | |
727 | struct prefix g_all; | |
728 | struct rp_info *rp_info; | |
729 | struct rp_info *rp_all; | |
730 | struct prefix nht_p; | |
731 | struct route_node *rn; | |
732 | bool was_plist = false; | |
733 | struct rp_info *trp_info; | |
734 | struct pim_upstream *up; | |
735 | struct listnode *upnode; | |
736 | struct bsgrp_node *bsgrp = NULL; | |
737 | struct bsm_rpinfo *bsrp = NULL; | |
738 | char grp_str[PREFIX2STR_BUFFER]; | |
739 | char rp_str[INET_ADDRSTRLEN]; | |
740 | ||
741 | if (!inet_ntop(AF_INET, &rp_addr, rp_str, sizeof(rp_str))) | |
742 | sprintf(rp_str, "<rp?>"); | |
743 | prefix2str(&group, grp_str, sizeof(grp_str)); | |
744 | ||
d62a17ae | 745 | if (plist) |
d9c9a9ee | 746 | rp_info = pim_rp_find_prefix_list(pim, rp_addr, plist); |
d62a17ae | 747 | else |
d9c9a9ee | 748 | rp_info = pim_rp_find_exact(pim, rp_addr, &group); |
d62a17ae | 749 | |
750 | if (!rp_info) | |
751 | return PIM_RP_NOT_FOUND; | |
752 | ||
753 | if (rp_info->plist) { | |
754 | XFREE(MTYPE_PIM_FILTER_NAME, rp_info->plist); | |
89b68082 | 755 | was_plist = true; |
d62a17ae | 756 | } |
757 | ||
23fc858a | 758 | if (PIM_DEBUG_PIM_TRACE) |
d0e418b4 | 759 | zlog_debug("%s: Delete RP %s for the group %s", |
760 | __PRETTY_FUNCTION__, rp_str, grp_str); | |
761 | ||
762 | /* While static RP is getting deleted, we need to check if dynamic RP | |
763 | * present for the same group in BSM RP table, then install the dynamic | |
764 | * RP for the group node into the main rp table | |
765 | */ | |
766 | if (rp_src_flag == RP_SRC_STATIC) { | |
767 | bsgrp = pim_bsm_get_bsgrp_node(&pim->global_scope, &group); | |
768 | ||
769 | if (bsgrp) { | |
770 | bsrp = listnode_head(bsgrp->bsrp_list); | |
771 | if (bsrp) { | |
23fc858a | 772 | if (PIM_DEBUG_PIM_TRACE) { |
d0e418b4 | 773 | char bsrp_str[INET_ADDRSTRLEN]; |
774 | ||
775 | if (!inet_ntop(AF_INET, bsrp, bsrp_str, | |
776 | sizeof(bsrp_str))) | |
777 | sprintf(bsrp_str, "<bsrp?>"); | |
778 | ||
779 | zlog_debug("%s: BSM RP %s found for the group %s", | |
780 | __PRETTY_FUNCTION__, | |
781 | bsrp_str, grp_str); | |
782 | } | |
783 | return pim_rp_change(pim, bsrp->rp_address, | |
784 | group, RP_SRC_BSR); | |
785 | } | |
786 | } else { | |
23fc858a | 787 | if (PIM_DEBUG_PIM_TRACE) |
d0e418b4 | 788 | zlog_debug( |
789 | "%s: BSM RP not found for the group %s", | |
790 | __PRETTY_FUNCTION__, grp_str); | |
791 | } | |
792 | } | |
793 | ||
d62a17ae | 794 | /* Deregister addr with Zebra NHT */ |
795 | nht_p.family = AF_INET; | |
796 | nht_p.prefixlen = IPV4_MAX_BITLEN; | |
797 | nht_p.u.prefix4 = rp_info->rp.rpf_addr.u.prefix4; | |
3d225d48 | 798 | if (PIM_DEBUG_PIM_NHT_RP) { |
d62a17ae | 799 | char buf[PREFIX2STR_BUFFER]; |
800 | prefix2str(&nht_p, buf, sizeof(buf)); | |
801 | zlog_debug("%s: Deregister RP addr %s with Zebra ", | |
802 | __PRETTY_FUNCTION__, buf); | |
803 | } | |
4533b847 | 804 | pim_delete_tracked_nexthop(pim, &nht_p, NULL, rp_info, false); |
d62a17ae | 805 | |
e691f179 DS |
806 | if (!str2prefix("224.0.0.0/4", &g_all)) |
807 | return PIM_RP_BAD_ADDRESS; | |
808 | ||
d9c9a9ee | 809 | rp_all = pim_rp_find_match_group(pim, &g_all); |
d62a17ae | 810 | |
811 | if (rp_all == rp_info) { | |
8cfd7268 SP |
812 | for (ALL_LIST_ELEMENTS_RO(pim->upstream_list, upnode, up)) { |
813 | /* Find the upstream (*, G) whose upstream address is | |
814 | * same as the deleted RP | |
815 | */ | |
d0e418b4 | 816 | if ((up->upstream_addr.s_addr |
817 | == rp_info->rp.rpf_addr.u.prefix4.s_addr) | |
818 | && (up->sg.src.s_addr == INADDR_ANY)) { | |
8cfd7268 SP |
819 | struct prefix grp; |
820 | grp.family = AF_INET; | |
821 | grp.prefixlen = IPV4_MAX_BITLEN; | |
822 | grp.u.prefix4 = up->sg.grp; | |
823 | trp_info = pim_rp_find_match_group(pim, &grp); | |
824 | if (trp_info == rp_all) { | |
825 | pim_upstream_rpf_clear(pim, up); | |
826 | up->upstream_addr.s_addr = INADDR_ANY; | |
827 | } | |
828 | } | |
829 | } | |
d62a17ae | 830 | rp_all->rp.rpf_addr.family = AF_INET; |
831 | rp_all->rp.rpf_addr.u.prefix4.s_addr = INADDR_NONE; | |
832 | rp_all->i_am_rp = 0; | |
833 | return PIM_SUCCESS; | |
834 | } | |
835 | ||
d9c9a9ee | 836 | listnode_delete(pim->rp_list, rp_info); |
89b68082 DS |
837 | |
838 | if (!was_plist) { | |
839 | rn = route_node_get(pim->rp_table, &rp_info->group); | |
840 | if (rn) { | |
841 | if (rn->info != rp_info) | |
af4c2728 | 842 | flog_err( |
450971aa | 843 | EC_LIB_DEVELOPMENT, |
3613d898 | 844 | "Expected rn->info to be equal to rp_info"); |
89b68082 | 845 | |
23fc858a | 846 | if (PIM_DEBUG_PIM_TRACE) { |
89b68082 DS |
847 | char buf[PREFIX_STRLEN]; |
848 | ||
996c9314 LB |
849 | zlog_debug( |
850 | "%s:Found for Freeing: %p for rp_info: %p(%s) Lock: %d", | |
851 | __PRETTY_FUNCTION__, rn, rp_info, | |
852 | prefix2str(&rp_info->group, buf, | |
853 | sizeof(buf)), | |
854 | rn->lock); | |
89b68082 DS |
855 | } |
856 | rn->info = NULL; | |
857 | route_unlock_node(rn); | |
858 | route_unlock_node(rn); | |
859 | } | |
860 | } | |
861 | ||
472ad383 | 862 | pim_rp_refresh_group_to_rp_mapping(pim); |
093c928f | 863 | |
8cfd7268 SP |
864 | for (ALL_LIST_ELEMENTS_RO(pim->upstream_list, upnode, up)) { |
865 | /* Find the upstream (*, G) whose upstream address is same as | |
866 | * the deleted RP | |
867 | */ | |
d0e418b4 | 868 | if ((up->upstream_addr.s_addr |
869 | == rp_info->rp.rpf_addr.u.prefix4.s_addr) | |
870 | && (up->sg.src.s_addr == INADDR_ANY)) { | |
8cfd7268 SP |
871 | struct prefix grp; |
872 | ||
873 | grp.family = AF_INET; | |
874 | grp.prefixlen = IPV4_MAX_BITLEN; | |
875 | grp.u.prefix4 = up->sg.grp; | |
876 | ||
877 | trp_info = pim_rp_find_match_group(pim, &grp); | |
878 | ||
879 | /* RP not found for the group grp */ | |
880 | if (pim_rpf_addr_is_inaddr_none(&trp_info->rp)) { | |
881 | pim_upstream_rpf_clear(pim, up); | |
d0e418b4 | 882 | pim_rp_set_upstream_addr( |
883 | pim, &up->upstream_addr, up->sg.src, | |
884 | up->sg.grp); | |
8cfd7268 SP |
885 | } |
886 | ||
887 | /* RP found for the group grp */ | |
888 | else | |
889 | pim_upstream_update(pim, up); | |
890 | } | |
891 | } | |
892 | ||
093c928f | 893 | XFREE(MTYPE_PIM_RP, rp_info); |
d62a17ae | 894 | return PIM_SUCCESS; |
75a26779 | 895 | } |
13afbd05 | 896 | |
d0e418b4 | 897 | int pim_rp_change(struct pim_instance *pim, struct in_addr new_rp_addr, |
898 | struct prefix group, enum rp_source rp_src_flag) | |
899 | { | |
900 | struct prefix nht_p; | |
901 | struct route_node *rn; | |
902 | int result = 0; | |
903 | struct rp_info *rp_info = NULL; | |
904 | struct pim_upstream *up; | |
905 | struct listnode *upnode; | |
906 | ||
907 | rn = route_node_lookup(pim->rp_table, &group); | |
908 | if (!rn) { | |
909 | result = pim_rp_new(pim, new_rp_addr, group, NULL, rp_src_flag); | |
910 | return result; | |
911 | } | |
912 | ||
913 | rp_info = rn->info; | |
914 | ||
915 | if (!rp_info) { | |
916 | route_unlock_node(rn); | |
917 | result = pim_rp_new(pim, new_rp_addr, group, NULL, rp_src_flag); | |
918 | return result; | |
919 | } | |
920 | ||
921 | if (rp_info->rp.rpf_addr.u.prefix4.s_addr == new_rp_addr.s_addr) { | |
922 | if (rp_info->rp_src != rp_src_flag) { | |
923 | rp_info->rp_src = rp_src_flag; | |
924 | route_unlock_node(rn); | |
925 | return PIM_SUCCESS; | |
926 | } | |
927 | } | |
928 | ||
35a30302 DS |
929 | nht_p.family = AF_INET; |
930 | nht_p.prefixlen = IPV4_MAX_BITLEN; | |
931 | ||
d0e418b4 | 932 | /* Deregister old RP addr with Zebra NHT */ |
933 | if (rp_info->rp.rpf_addr.u.prefix4.s_addr != INADDR_ANY) { | |
d0e418b4 | 934 | nht_p.u.prefix4 = rp_info->rp.rpf_addr.u.prefix4; |
935 | if (PIM_DEBUG_PIM_NHT_RP) { | |
936 | char buf[PREFIX2STR_BUFFER]; | |
937 | ||
938 | prefix2str(&nht_p, buf, sizeof(buf)); | |
939 | zlog_debug("%s: Deregister RP addr %s with Zebra ", | |
940 | __PRETTY_FUNCTION__, buf); | |
941 | } | |
942 | pim_delete_tracked_nexthop(pim, &nht_p, NULL, rp_info, false); | |
943 | } | |
944 | ||
945 | pim_rp_nexthop_del(rp_info); | |
946 | listnode_delete(pim->rp_list, rp_info); | |
947 | /* Update the new RP address*/ | |
948 | rp_info->rp.rpf_addr.u.prefix4 = new_rp_addr; | |
949 | rp_info->rp_src = rp_src_flag; | |
950 | rp_info->i_am_rp = 0; | |
951 | ||
952 | listnode_add_sort(pim->rp_list, rp_info); | |
953 | ||
954 | for (ALL_LIST_ELEMENTS_RO(pim->upstream_list, upnode, up)) { | |
955 | if (up->sg.src.s_addr == INADDR_ANY) { | |
956 | struct prefix grp; | |
957 | struct rp_info *trp_info; | |
958 | ||
959 | grp.family = AF_INET; | |
960 | grp.prefixlen = IPV4_MAX_BITLEN; | |
961 | grp.u.prefix4 = up->sg.grp; | |
962 | trp_info = pim_rp_find_match_group(pim, &grp); | |
963 | ||
964 | if (trp_info == rp_info) | |
965 | pim_upstream_update(pim, up); | |
966 | } | |
967 | } | |
968 | ||
969 | /* Register new RP addr with Zebra NHT */ | |
970 | nht_p.u.prefix4 = rp_info->rp.rpf_addr.u.prefix4; | |
971 | if (PIM_DEBUG_PIM_NHT_RP) { | |
972 | char buf[PREFIX2STR_BUFFER]; | |
973 | char buf1[PREFIX2STR_BUFFER]; | |
974 | ||
975 | prefix2str(&nht_p, buf, sizeof(buf)); | |
976 | prefix2str(&rp_info->group, buf1, sizeof(buf1)); | |
977 | zlog_debug("%s: NHT Register RP addr %s grp %s with Zebra ", | |
978 | __PRETTY_FUNCTION__, buf, buf1); | |
979 | } | |
980 | ||
981 | pim_find_or_track_nexthop(pim, &nht_p, NULL, rp_info, false, NULL); | |
982 | if (!pim_ecmp_nexthop_lookup(pim, &rp_info->rp.source_nexthop, &nht_p, | |
983 | &rp_info->group, 1)) { | |
984 | route_unlock_node(rn); | |
985 | return PIM_RP_NO_PATH; | |
986 | } | |
987 | ||
988 | pim_rp_check_interfaces(pim, rp_info); | |
989 | ||
990 | route_unlock_node(rn); | |
991 | ||
992 | pim_rp_refresh_group_to_rp_mapping(pim); | |
993 | ||
994 | return result; | |
995 | } | |
996 | ||
fec883d9 | 997 | void pim_rp_setup(struct pim_instance *pim) |
13afbd05 | 998 | { |
d62a17ae | 999 | struct listnode *node; |
1000 | struct rp_info *rp_info; | |
d62a17ae | 1001 | struct prefix nht_p; |
d62a17ae | 1002 | |
d9c9a9ee | 1003 | for (ALL_LIST_ELEMENTS_RO(pim->rp_list, node, rp_info)) { |
d62a17ae | 1004 | if (rp_info->rp.rpf_addr.u.prefix4.s_addr == INADDR_NONE) |
1005 | continue; | |
1006 | ||
1007 | nht_p.family = AF_INET; | |
1008 | nht_p.prefixlen = IPV4_MAX_BITLEN; | |
1009 | nht_p.u.prefix4 = rp_info->rp.rpf_addr.u.prefix4; | |
8cfd7268 | 1010 | |
4533b847 | 1011 | pim_find_or_track_nexthop(pim, &nht_p, NULL, rp_info, false, |
1012 | NULL); | |
43763b11 DS |
1013 | if (!pim_ecmp_nexthop_lookup(pim, &rp_info->rp.source_nexthop, |
1014 | &nht_p, &rp_info->group, 1)) | |
1015 | if (PIM_DEBUG_PIM_NHT_RP) | |
d62a17ae | 1016 | zlog_debug( |
43763b11 | 1017 | "Unable to lookup nexthop for rp specified"); |
d62a17ae | 1018 | } |
13afbd05 DS |
1019 | } |
1020 | ||
54b97c74 | 1021 | /* |
7176984f | 1022 | * Checks to see if we should elect ourself the actual RP when new if |
1023 | * addresses are added against an interface. | |
54b97c74 | 1024 | */ |
d62a17ae | 1025 | void pim_rp_check_on_if_add(struct pim_interface *pim_ifp) |
54b97c74 | 1026 | { |
d62a17ae | 1027 | struct listnode *node; |
1028 | struct rp_info *rp_info; | |
1029 | bool i_am_rp_changed = false; | |
d9c9a9ee | 1030 | struct pim_instance *pim = pim_ifp->pim; |
d62a17ae | 1031 | |
d9c9a9ee | 1032 | if (pim->rp_list == NULL) |
d62a17ae | 1033 | return; |
1034 | ||
d9c9a9ee | 1035 | for (ALL_LIST_ELEMENTS_RO(pim->rp_list, node, rp_info)) { |
d62a17ae | 1036 | if (pim_rpf_addr_is_inaddr_none(&rp_info->rp)) |
1037 | continue; | |
1038 | ||
1039 | /* if i_am_rp is already set nothing to be done (adding new | |
1040 | * addresses | |
1041 | * is not going to make a difference). */ | |
1042 | if (rp_info->i_am_rp) { | |
1043 | continue; | |
1044 | } | |
1045 | ||
1046 | if (pim_rp_check_interface_addrs(rp_info, pim_ifp)) { | |
1047 | i_am_rp_changed = true; | |
1048 | rp_info->i_am_rp = 1; | |
3d225d48 | 1049 | if (PIM_DEBUG_PIM_NHT_RP) { |
d62a17ae | 1050 | char rp[PREFIX_STRLEN]; |
1051 | pim_addr_dump("<rp?>", &rp_info->rp.rpf_addr, | |
1052 | rp, sizeof(rp)); | |
1053 | zlog_debug("%s: %s: i am rp", __func__, rp); | |
1054 | } | |
1055 | } | |
1056 | } | |
1057 | ||
1058 | if (i_am_rp_changed) { | |
472ad383 | 1059 | pim_msdp_i_am_rp_changed(pim); |
d62a17ae | 1060 | } |
7176984f | 1061 | } |
36d6bd7d | 1062 | |
7176984f | 1063 | /* up-optimized re-evaluation of "i_am_rp". this is used when ifaddresses |
1064 | * are removed. Removing numbers is an uncommon event in an active network | |
1065 | * so I have made no attempt to optimize it. */ | |
fec883d9 | 1066 | void pim_i_am_rp_re_evaluate(struct pim_instance *pim) |
7176984f | 1067 | { |
d62a17ae | 1068 | struct listnode *node; |
1069 | struct rp_info *rp_info; | |
1070 | bool i_am_rp_changed = false; | |
1071 | int old_i_am_rp; | |
1072 | ||
d9c9a9ee | 1073 | if (pim->rp_list == NULL) |
d62a17ae | 1074 | return; |
1075 | ||
d9c9a9ee | 1076 | for (ALL_LIST_ELEMENTS_RO(pim->rp_list, node, rp_info)) { |
d62a17ae | 1077 | if (pim_rpf_addr_is_inaddr_none(&rp_info->rp)) |
1078 | continue; | |
1079 | ||
1080 | old_i_am_rp = rp_info->i_am_rp; | |
fec883d9 | 1081 | pim_rp_check_interfaces(pim, rp_info); |
d62a17ae | 1082 | |
1083 | if (old_i_am_rp != rp_info->i_am_rp) { | |
1084 | i_am_rp_changed = true; | |
3d225d48 | 1085 | if (PIM_DEBUG_PIM_NHT_RP) { |
d62a17ae | 1086 | char rp[PREFIX_STRLEN]; |
1087 | pim_addr_dump("<rp?>", &rp_info->rp.rpf_addr, | |
1088 | rp, sizeof(rp)); | |
1089 | if (rp_info->i_am_rp) { | |
1090 | zlog_debug("%s: %s: i am rp", __func__, | |
1091 | rp); | |
1092 | } else { | |
1093 | zlog_debug("%s: %s: i am no longer rp", | |
1094 | __func__, rp); | |
1095 | } | |
1096 | } | |
1097 | } | |
1098 | } | |
1099 | ||
1100 | if (i_am_rp_changed) { | |
472ad383 | 1101 | pim_msdp_i_am_rp_changed(pim); |
d62a17ae | 1102 | } |
54b97c74 DS |
1103 | } |
1104 | ||
1105 | /* | |
1106 | * I_am_RP(G) is true if the group-to-RP mapping indicates that | |
1107 | * this router is the RP for the group. | |
1108 | * | |
1109 | * Since we only have static RP, all groups are part of this RP | |
1110 | */ | |
d9c9a9ee | 1111 | int pim_rp_i_am_rp(struct pim_instance *pim, struct in_addr group) |
54b97c74 | 1112 | { |
d62a17ae | 1113 | struct prefix g; |
1114 | struct rp_info *rp_info; | |
36d6bd7d | 1115 | |
d62a17ae | 1116 | memset(&g, 0, sizeof(g)); |
1117 | g.family = AF_INET; | |
1118 | g.prefixlen = 32; | |
1119 | g.u.prefix4 = group; | |
36d6bd7d | 1120 | |
d9c9a9ee | 1121 | rp_info = pim_rp_find_match_group(pim, &g); |
36d6bd7d | 1122 | |
d62a17ae | 1123 | if (rp_info) |
1124 | return rp_info->i_am_rp; | |
36d6bd7d | 1125 | |
d62a17ae | 1126 | return 0; |
54b97c74 DS |
1127 | } |
1128 | ||
71694057 DS |
1129 | /* |
1130 | * RP(G) | |
1131 | * | |
1132 | * Return the RP that the Group belongs too. | |
1133 | */ | |
fec883d9 | 1134 | struct pim_rpf *pim_rp_g(struct pim_instance *pim, struct in_addr group) |
71694057 | 1135 | { |
d62a17ae | 1136 | struct prefix g; |
1137 | struct rp_info *rp_info; | |
1138 | ||
1139 | memset(&g, 0, sizeof(g)); | |
1140 | g.family = AF_INET; | |
1141 | g.prefixlen = 32; | |
1142 | g.u.prefix4 = group; | |
1143 | ||
d9c9a9ee | 1144 | rp_info = pim_rp_find_match_group(pim, &g); |
d62a17ae | 1145 | |
1146 | if (rp_info) { | |
1147 | struct prefix nht_p; | |
43763b11 | 1148 | |
d62a17ae | 1149 | /* Register addr with Zebra NHT */ |
1150 | nht_p.family = AF_INET; | |
1151 | nht_p.prefixlen = IPV4_MAX_BITLEN; | |
1152 | nht_p.u.prefix4 = rp_info->rp.rpf_addr.u.prefix4; | |
3d225d48 | 1153 | if (PIM_DEBUG_PIM_NHT_RP) { |
d62a17ae | 1154 | char buf[PREFIX2STR_BUFFER]; |
1155 | char buf1[PREFIX2STR_BUFFER]; | |
1156 | prefix2str(&nht_p, buf, sizeof(buf)); | |
1157 | prefix2str(&rp_info->group, buf1, sizeof(buf1)); | |
1158 | zlog_debug( | |
1159 | "%s: NHT Register RP addr %s grp %s with Zebra", | |
1160 | __PRETTY_FUNCTION__, buf, buf1); | |
1161 | } | |
4533b847 | 1162 | pim_find_or_track_nexthop(pim, &nht_p, NULL, rp_info, false, |
1163 | NULL); | |
43763b11 DS |
1164 | pim_rpf_set_refresh_time(pim); |
1165 | (void)pim_ecmp_nexthop_lookup(pim, &rp_info->rp.source_nexthop, | |
1166 | &nht_p, &rp_info->group, 1); | |
d62a17ae | 1167 | return (&rp_info->rp); |
1168 | } | |
1169 | ||
1170 | // About to Go Down | |
1171 | return NULL; | |
71694057 DS |
1172 | } |
1173 | ||
8f5f5e91 DS |
1174 | /* |
1175 | * Set the upstream IP address we want to talk to based upon | |
1176 | * the rp configured and the source address | |
1177 | * | |
1178 | * If we have don't have a RP configured and the source address is * | |
732c209c | 1179 | * then set the upstream addr as INADDR_ANY and return failure. |
8f5f5e91 DS |
1180 | * |
1181 | */ | |
d9c9a9ee DS |
1182 | int pim_rp_set_upstream_addr(struct pim_instance *pim, struct in_addr *up, |
1183 | struct in_addr source, struct in_addr group) | |
8f5f5e91 | 1184 | { |
d62a17ae | 1185 | struct rp_info *rp_info; |
1186 | struct prefix g; | |
36d6bd7d | 1187 | |
d62a17ae | 1188 | memset(&g, 0, sizeof(g)); |
1189 | g.family = AF_INET; | |
1190 | g.prefixlen = 32; | |
1191 | g.u.prefix4 = group; | |
36d6bd7d | 1192 | |
d9c9a9ee | 1193 | rp_info = pim_rp_find_match_group(pim, &g); |
36d6bd7d | 1194 | |
d62a17ae | 1195 | if ((pim_rpf_addr_is_inaddr_none(&rp_info->rp)) |
1196 | && (source.s_addr == INADDR_ANY)) { | |
3d225d48 | 1197 | if (PIM_DEBUG_PIM_NHT_RP) |
d62a17ae | 1198 | zlog_debug("%s: Received a (*,G) with no RP configured", |
1199 | __PRETTY_FUNCTION__); | |
732c209c | 1200 | up->s_addr = INADDR_ANY; |
d62a17ae | 1201 | return 0; |
1202 | } | |
8f5f5e91 | 1203 | |
d62a17ae | 1204 | *up = (source.s_addr == INADDR_ANY) ? rp_info->rp.rpf_addr.u.prefix4 |
1205 | : source; | |
8f5f5e91 | 1206 | |
d62a17ae | 1207 | return 1; |
8f5f5e91 | 1208 | } |
75a26779 | 1209 | |
0c8b717e DS |
1210 | int pim_rp_config_write(struct pim_instance *pim, struct vty *vty, |
1211 | const char *spaces) | |
75a26779 | 1212 | { |
d62a17ae | 1213 | struct listnode *node; |
1214 | struct rp_info *rp_info; | |
1215 | char rp_buffer[32]; | |
1216 | char group_buffer[32]; | |
1217 | int count = 0; | |
1218 | ||
d9c9a9ee | 1219 | for (ALL_LIST_ELEMENTS_RO(pim->rp_list, node, rp_info)) { |
d62a17ae | 1220 | if (pim_rpf_addr_is_inaddr_none(&rp_info->rp)) |
1221 | continue; | |
1222 | ||
d0e418b4 | 1223 | if (rp_info->rp_src == RP_SRC_BSR) |
1224 | continue; | |
1225 | ||
d62a17ae | 1226 | if (rp_info->plist) |
0c8b717e | 1227 | vty_out(vty, "%sip pim rp %s prefix-list %s\n", spaces, |
d62a17ae | 1228 | inet_ntop(AF_INET, |
1229 | &rp_info->rp.rpf_addr.u.prefix4, | |
1230 | rp_buffer, 32), | |
1231 | rp_info->plist); | |
1232 | else | |
0c8b717e | 1233 | vty_out(vty, "%sip pim rp %s %s\n", spaces, |
d62a17ae | 1234 | inet_ntop(AF_INET, |
1235 | &rp_info->rp.rpf_addr.u.prefix4, | |
1236 | rp_buffer, 32), | |
1237 | prefix2str(&rp_info->group, group_buffer, 32)); | |
1238 | count++; | |
1239 | } | |
1240 | ||
1241 | return count; | |
75a26779 DS |
1242 | } |
1243 | ||
f43593d9 DS |
1244 | bool pim_rp_check_is_my_ip_address(struct pim_instance *pim, |
1245 | struct in_addr dest_addr) | |
75a26779 | 1246 | { |
fec883d9 | 1247 | if (if_lookup_exact_address(&dest_addr, AF_INET, pim->vrf_id)) |
f43593d9 | 1248 | return true; |
d62a17ae | 1249 | |
f43593d9 | 1250 | return false; |
75a26779 | 1251 | } |
00d07c6f | 1252 | |
088f1098 | 1253 | void pim_rp_show_information(struct pim_instance *pim, struct vty *vty, bool uj) |
00d07c6f | 1254 | { |
d62a17ae | 1255 | struct rp_info *rp_info; |
1256 | struct rp_info *prev_rp_info = NULL; | |
1257 | struct listnode *node; | |
025725f7 | 1258 | char source[7]; |
d62a17ae | 1259 | |
1260 | json_object *json = NULL; | |
1261 | json_object *json_rp_rows = NULL; | |
1262 | json_object *json_row = NULL; | |
1263 | ||
1264 | if (uj) | |
1265 | json = json_object_new_object(); | |
1266 | else | |
1267 | vty_out(vty, | |
d6593fc5 | 1268 | "RP address group/prefix-list OIF I am RP Source\n"); |
d9c9a9ee | 1269 | for (ALL_LIST_ELEMENTS_RO(pim->rp_list, node, rp_info)) { |
d62a17ae | 1270 | if (!pim_rpf_addr_is_inaddr_none(&rp_info->rp)) { |
1271 | char buf[48]; | |
1272 | ||
025725f7 | 1273 | if (rp_info->rp_src == RP_SRC_STATIC) |
1274 | strcpy(source, "Static"); | |
1275 | else if (rp_info->rp_src == RP_SRC_BSR) | |
1276 | strcpy(source, "BSR"); | |
1277 | else | |
1278 | strcpy(source, "None"); | |
d62a17ae | 1279 | if (uj) { |
1280 | /* | |
1281 | * If we have moved on to a new RP then add the | |
1282 | * entry for the previous RP | |
1283 | */ | |
1284 | if (prev_rp_info | |
1285 | && prev_rp_info->rp.rpf_addr.u.prefix4 | |
1286 | .s_addr | |
1287 | != rp_info->rp.rpf_addr.u.prefix4 | |
1288 | .s_addr) { | |
1289 | json_object_object_add( | |
1290 | json, | |
1291 | inet_ntoa(prev_rp_info->rp | |
1292 | .rpf_addr.u | |
1293 | .prefix4), | |
1294 | json_rp_rows); | |
1295 | json_rp_rows = NULL; | |
1296 | } | |
1297 | ||
1298 | if (!json_rp_rows) | |
1299 | json_rp_rows = json_object_new_array(); | |
1300 | ||
1301 | json_row = json_object_new_object(); | |
1302 | if (rp_info->rp.source_nexthop.interface) | |
1303 | json_object_string_add( | |
1304 | json_row, "outboundInterface", | |
1305 | rp_info->rp.source_nexthop | |
1306 | .interface->name); | |
1307 | ||
1308 | if (rp_info->i_am_rp) | |
1309 | json_object_boolean_true_add(json_row, | |
1310 | "iAmRP"); | |
1311 | ||
1312 | if (rp_info->plist) | |
1313 | json_object_string_add(json_row, | |
1314 | "prefixList", | |
1315 | rp_info->plist); | |
1316 | else | |
1317 | json_object_string_add( | |
1318 | json_row, "group", | |
1319 | prefix2str(&rp_info->group, buf, | |
1320 | 48)); | |
d0e418b4 | 1321 | json_object_string_add(json_row, "source", |
025725f7 | 1322 | source); |
d62a17ae | 1323 | |
1324 | json_object_array_add(json_rp_rows, json_row); | |
1325 | } else { | |
1326 | vty_out(vty, "%-15s ", | |
1327 | inet_ntoa(rp_info->rp.rpf_addr.u | |
1328 | .prefix4)); | |
1329 | ||
1330 | if (rp_info->plist) | |
1331 | vty_out(vty, "%-18s ", rp_info->plist); | |
1332 | else | |
1333 | vty_out(vty, "%-18s ", | |
1334 | prefix2str(&rp_info->group, buf, | |
1335 | 48)); | |
1336 | ||
1337 | if (rp_info->rp.source_nexthop.interface) | |
ff415346 | 1338 | vty_out(vty, "%-16s ", |
d62a17ae | 1339 | rp_info->rp.source_nexthop |
1340 | .interface->name); | |
1341 | else | |
ff415346 | 1342 | vty_out(vty, "%-16s ", "(Unknown)"); |
d62a17ae | 1343 | |
1344 | if (rp_info->i_am_rp) | |
d6593fc5 | 1345 | vty_out(vty, "yes"); |
d62a17ae | 1346 | else |
025725f7 | 1347 | vty_out(vty, "no"); |
d62a17ae | 1348 | |
025725f7 | 1349 | vty_out(vty, "%14s\n", source); |
1350 | } | |
d62a17ae | 1351 | prev_rp_info = rp_info; |
1352 | } | |
1353 | } | |
1354 | ||
1355 | if (uj) { | |
1356 | if (prev_rp_info && json_rp_rows) | |
1357 | json_object_object_add( | |
1358 | json, | |
1359 | inet_ntoa(prev_rp_info->rp.rpf_addr.u.prefix4), | |
1360 | json_rp_rows); | |
1361 | ||
9d303b37 DL |
1362 | vty_out(vty, "%s\n", json_object_to_json_string_ext( |
1363 | json, JSON_C_TO_STRING_PRETTY)); | |
d62a17ae | 1364 | json_object_free(json); |
1365 | } | |
00d07c6f | 1366 | } |
cba44481 | 1367 | |
c9cd7fbc | 1368 | void pim_resolve_rp_nh(struct pim_instance *pim, struct pim_neighbor *nbr) |
cba44481 | 1369 | { |
d62a17ae | 1370 | struct listnode *node = NULL; |
1371 | struct rp_info *rp_info = NULL; | |
1372 | struct nexthop *nh_node = NULL; | |
1373 | struct prefix nht_p; | |
1374 | struct pim_nexthop_cache pnc; | |
d62a17ae | 1375 | |
d9c9a9ee | 1376 | for (ALL_LIST_ELEMENTS_RO(pim->rp_list, node, rp_info)) { |
d62a17ae | 1377 | if (rp_info->rp.rpf_addr.u.prefix4.s_addr == INADDR_NONE) |
1378 | continue; | |
1379 | ||
1380 | nht_p.family = AF_INET; | |
1381 | nht_p.prefixlen = IPV4_MAX_BITLEN; | |
1382 | nht_p.u.prefix4 = rp_info->rp.rpf_addr.u.prefix4; | |
1383 | memset(&pnc, 0, sizeof(struct pim_nexthop_cache)); | |
fec883d9 | 1384 | if (!pim_find_or_track_nexthop(pim, &nht_p, NULL, rp_info, |
4533b847 | 1385 | false, &pnc)) |
cb9c7c50 DS |
1386 | continue; |
1387 | ||
1388 | for (nh_node = pnc.nexthop; nh_node; nh_node = nh_node->next) { | |
1389 | if (nh_node->gate.ipv4.s_addr != 0) | |
1390 | continue; | |
1391 | ||
1392 | struct interface *ifp1 = if_lookup_by_index( | |
fec883d9 | 1393 | nh_node->ifindex, pim->vrf_id); |
c9cd7fbc DS |
1394 | |
1395 | if (nbr->interface != ifp1) | |
cb9c7c50 DS |
1396 | continue; |
1397 | ||
1398 | nh_node->gate.ipv4 = nbr->source_addr; | |
3d225d48 | 1399 | if (PIM_DEBUG_PIM_NHT_RP) { |
cb9c7c50 DS |
1400 | char str[PREFIX_STRLEN]; |
1401 | char str1[INET_ADDRSTRLEN]; | |
1402 | pim_inet4_dump("<nht_nbr?>", nbr->source_addr, | |
1403 | str1, sizeof(str1)); | |
1404 | pim_addr_dump("<nht_addr?>", &nht_p, str, | |
1405 | sizeof(str)); | |
1406 | zlog_debug( | |
1407 | "%s: addr %s new nexthop addr %s interface %s", | |
1408 | __PRETTY_FUNCTION__, str, str1, | |
1409 | ifp1->name); | |
d62a17ae | 1410 | } |
1411 | } | |
1412 | } | |
cba44481 | 1413 | } |