Line | Count | Source (jump to first uncovered line) |
1 | | // SPDX-License-Identifier: GPL-2.0-or-later |
2 | | /* |
3 | | * PIM for Quagga |
4 | | * Copyright (C) 2008 Everton da Silva Marques |
5 | | */ |
6 | | |
7 | | #include <zebra.h> |
8 | | |
9 | | #include "if.h" |
10 | | |
11 | | #include "log.h" |
12 | | #include "prefix.h" |
13 | | #include "memory.h" |
14 | | #include "jhash.h" |
15 | | |
16 | | #include "pimd.h" |
17 | | #include "pim_instance.h" |
18 | | #include "pim_rpf.h" |
19 | | #include "pim_pim.h" |
20 | | #include "pim_str.h" |
21 | | #include "pim_iface.h" |
22 | | #include "pim_neighbor.h" |
23 | | #include "pim_zlookup.h" |
24 | | #include "pim_ifchannel.h" |
25 | | #include "pim_time.h" |
26 | | #include "pim_nht.h" |
27 | | #include "pim_oil.h" |
28 | | #include "pim_mlag.h" |
29 | | |
30 | | static pim_addr pim_rpf_find_rpf_addr(struct pim_upstream *up); |
31 | | |
32 | | void pim_rpf_set_refresh_time(struct pim_instance *pim) |
33 | 1.10k | { |
34 | 1.10k | pim->last_route_change_time = pim_time_monotonic_usec(); |
35 | 1.10k | if (PIM_DEBUG_PIM_TRACE) |
36 | 0 | zlog_debug("%s: vrf(%s) New last route change time: %" PRId64, |
37 | 1.10k | __func__, pim->vrf->name, |
38 | 1.10k | pim->last_route_change_time); |
39 | 1.10k | } |
40 | | |
41 | | bool pim_nexthop_lookup(struct pim_instance *pim, struct pim_nexthop *nexthop, |
42 | | pim_addr addr, int neighbor_needed) |
43 | 0 | { |
44 | 0 | struct pim_zlookup_nexthop nexthop_tab[router->multipath]; |
45 | 0 | struct pim_neighbor *nbr = NULL; |
46 | 0 | int num_ifindex; |
47 | 0 | struct interface *ifp = NULL; |
48 | 0 | ifindex_t first_ifindex = 0; |
49 | 0 | int found = 0; |
50 | 0 | int i = 0; |
51 | 0 | struct pim_interface *pim_ifp; |
52 | |
|
53 | 0 | #if PIM_IPV == 4 |
54 | | /* |
55 | | * We should not attempt to lookup a |
56 | | * 255.255.255.255 address, since |
57 | | * it will never work |
58 | | */ |
59 | 0 | if (pim_addr_is_any(addr)) |
60 | 0 | return false; |
61 | 0 | #endif |
62 | | |
63 | 0 | if ((!pim_addr_cmp(nexthop->last_lookup, addr)) && |
64 | 0 | (nexthop->last_lookup_time > pim->last_route_change_time)) { |
65 | 0 | if (PIM_DEBUG_PIM_NHT) |
66 | 0 | zlog_debug( |
67 | 0 | "%s: Using last lookup for %pPAs at %lld, %" PRId64 |
68 | 0 | " addr %pPAs", |
69 | 0 | __func__, &addr, nexthop->last_lookup_time, |
70 | 0 | pim->last_route_change_time, |
71 | 0 | &nexthop->mrib_nexthop_addr); |
72 | 0 | pim->nexthop_lookups_avoided++; |
73 | 0 | return true; |
74 | 0 | } else { |
75 | 0 | if (PIM_DEBUG_PIM_NHT) |
76 | 0 | zlog_debug( |
77 | 0 | "%s: Looking up: %pPAs, last lookup time: %lld, %" PRId64, |
78 | 0 | __func__, &addr, nexthop->last_lookup_time, |
79 | 0 | pim->last_route_change_time); |
80 | 0 | } |
81 | | |
82 | 0 | memset(nexthop_tab, 0, |
83 | 0 | sizeof(struct pim_zlookup_nexthop) * router->multipath); |
84 | 0 | num_ifindex = |
85 | 0 | zclient_lookup_nexthop(pim, nexthop_tab, router->multipath, |
86 | 0 | addr, PIM_NEXTHOP_LOOKUP_MAX); |
87 | 0 | if (num_ifindex < 1) { |
88 | 0 | if (PIM_DEBUG_PIM_NHT) |
89 | 0 | zlog_debug( |
90 | 0 | "%s %s: could not find nexthop ifindex for address %pPAs", |
91 | 0 | __FILE__, __func__, &addr); |
92 | 0 | return false; |
93 | 0 | } |
94 | | |
95 | 0 | while (!found && (i < num_ifindex)) { |
96 | 0 | first_ifindex = nexthop_tab[i].ifindex; |
97 | |
|
98 | 0 | ifp = if_lookup_by_index(first_ifindex, pim->vrf->vrf_id); |
99 | 0 | if (!ifp) { |
100 | 0 | if (PIM_DEBUG_ZEBRA) |
101 | 0 | zlog_debug( |
102 | 0 | "%s %s: could not find interface for ifindex %d (address %pPAs)", |
103 | 0 | __FILE__, __func__, first_ifindex, |
104 | 0 | &addr); |
105 | 0 | i++; |
106 | 0 | continue; |
107 | 0 | } |
108 | | |
109 | 0 | pim_ifp = ifp->info; |
110 | 0 | if (!pim_ifp || !pim_ifp->pim_enable) { |
111 | 0 | if (PIM_DEBUG_ZEBRA) |
112 | 0 | zlog_debug( |
113 | 0 | "%s: pim not enabled on input interface %s (ifindex=%d, RPF for source %pPAs)", |
114 | 0 | __func__, ifp->name, first_ifindex, |
115 | 0 | &addr); |
116 | 0 | i++; |
117 | 0 | } else if (neighbor_needed && |
118 | 0 | !pim_if_connected_to_source(ifp, addr)) { |
119 | 0 | nbr = pim_neighbor_find( |
120 | 0 | ifp, nexthop_tab[i].nexthop_addr, true); |
121 | 0 | if (PIM_DEBUG_PIM_TRACE_DETAIL) |
122 | 0 | zlog_debug("ifp name: %s, pim nbr: %p", |
123 | 0 | ifp->name, nbr); |
124 | 0 | if (!nbr && !if_is_loopback(ifp)) |
125 | 0 | i++; |
126 | 0 | else |
127 | 0 | found = 1; |
128 | 0 | } else |
129 | 0 | found = 1; |
130 | 0 | } |
131 | |
|
132 | 0 | if (found) { |
133 | 0 | if (PIM_DEBUG_ZEBRA) |
134 | 0 | zlog_debug( |
135 | 0 | "%s %s: found nexthop %pPAs for address %pPAs: interface %s ifindex=%d metric=%d pref=%d", |
136 | 0 | __FILE__, __func__, |
137 | 0 | &nexthop_tab[i].nexthop_addr, &addr, ifp->name, |
138 | 0 | first_ifindex, nexthop_tab[i].route_metric, |
139 | 0 | nexthop_tab[i].protocol_distance); |
140 | | |
141 | | /* update nexthop data */ |
142 | 0 | nexthop->interface = ifp; |
143 | 0 | nexthop->mrib_nexthop_addr = nexthop_tab[i].nexthop_addr; |
144 | 0 | nexthop->mrib_metric_preference = |
145 | 0 | nexthop_tab[i].protocol_distance; |
146 | 0 | nexthop->mrib_route_metric = nexthop_tab[i].route_metric; |
147 | 0 | nexthop->last_lookup = addr; |
148 | 0 | nexthop->last_lookup_time = pim_time_monotonic_usec(); |
149 | 0 | nexthop->nbr = nbr; |
150 | 0 | return true; |
151 | 0 | } else |
152 | 0 | return false; |
153 | 0 | } |
154 | | |
155 | | static int nexthop_mismatch(const struct pim_nexthop *nh1, |
156 | | const struct pim_nexthop *nh2) |
157 | 0 | { |
158 | 0 | return (nh1->interface != nh2->interface) || |
159 | 0 | (pim_addr_cmp(nh1->mrib_nexthop_addr, nh2->mrib_nexthop_addr)) || |
160 | 0 | (nh1->mrib_metric_preference != nh2->mrib_metric_preference) || |
161 | 0 | (nh1->mrib_route_metric != nh2->mrib_route_metric); |
162 | 0 | } |
163 | | |
164 | | static void pim_rpf_cost_change(struct pim_instance *pim, |
165 | | struct pim_upstream *up, uint32_t old_cost) |
166 | 59.4k | { |
167 | 59.4k | struct pim_rpf *rpf = &up->rpf; |
168 | 59.4k | uint32_t new_cost; |
169 | | |
170 | 59.4k | new_cost = pim_up_mlag_local_cost(up); |
171 | 59.4k | if (PIM_DEBUG_MLAG) |
172 | 0 | zlog_debug( |
173 | 59.4k | "%s: Cost_to_rp of upstream-%s changed to:%u, from:%u", |
174 | 59.4k | __func__, up->sg_str, new_cost, old_cost); |
175 | | |
176 | 59.4k | if (old_cost == new_cost) |
177 | 59.4k | return; |
178 | | |
179 | | /* Cost changed, it might Impact MLAG DF election, update */ |
180 | 0 | if (PIM_DEBUG_MLAG) |
181 | 0 | zlog_debug( |
182 | 0 | "%s: Cost_to_rp of upstream-%s changed to:%u", |
183 | 0 | __func__, up->sg_str, |
184 | 0 | rpf->source_nexthop.mrib_route_metric); |
185 | |
|
186 | 0 | if (pim_up_mlag_is_local(up)) |
187 | 0 | pim_mlag_up_local_add(pim, up); |
188 | 0 | } |
189 | | |
190 | | enum pim_rpf_result pim_rpf_update(struct pim_instance *pim, |
191 | | struct pim_upstream *up, struct pim_rpf *old, |
192 | | const char *caller) |
193 | 59.9k | { |
194 | 59.9k | struct pim_rpf *rpf = &up->rpf; |
195 | 59.9k | struct pim_rpf saved; |
196 | 59.9k | pim_addr src; |
197 | 59.9k | struct prefix grp; |
198 | 59.9k | bool neigh_needed = true; |
199 | 59.9k | uint32_t saved_mrib_route_metric; |
200 | | |
201 | 59.9k | if (PIM_UPSTREAM_FLAG_TEST_STATIC_IIF(up->flags)) |
202 | 0 | return PIM_RPF_OK; |
203 | | |
204 | 59.9k | if (pim_addr_is_any(up->upstream_addr)) { |
205 | 513 | zlog_debug("%s(%s): RP is not configured yet for %s", |
206 | 513 | __func__, caller, up->sg_str); |
207 | 513 | return PIM_RPF_OK; |
208 | 513 | } |
209 | | |
210 | 59.4k | saved.source_nexthop = rpf->source_nexthop; |
211 | 59.4k | saved.rpf_addr = rpf->rpf_addr; |
212 | 59.4k | saved_mrib_route_metric = pim_up_mlag_local_cost(up); |
213 | 59.4k | if (old) { |
214 | 31.2k | old->source_nexthop = saved.source_nexthop; |
215 | 31.2k | old->rpf_addr = saved.rpf_addr; |
216 | 31.2k | } |
217 | | |
218 | 59.4k | src = up->upstream_addr; // RP or Src address |
219 | 59.4k | pim_addr_to_prefix(&grp, up->sg.grp); |
220 | | |
221 | 59.4k | if ((pim_addr_is_any(up->sg.src) && I_am_RP(pim, up->sg.grp)) || |
222 | 59.4k | PIM_UPSTREAM_FLAG_TEST_FHR(up->flags)) |
223 | 114 | neigh_needed = false; |
224 | 59.4k | pim_find_or_track_nexthop(pim, up->upstream_addr, up, NULL, NULL); |
225 | 59.4k | if (!pim_ecmp_nexthop_lookup(pim, &rpf->source_nexthop, src, &grp, |
226 | 59.4k | neigh_needed)) { |
227 | | /* Route is Deleted in Zebra, reset the stored NH data */ |
228 | 59.4k | pim_upstream_rpf_clear(pim, up); |
229 | 59.4k | pim_rpf_cost_change(pim, up, saved_mrib_route_metric); |
230 | 59.4k | return PIM_RPF_FAILURE; |
231 | 59.4k | } |
232 | | |
233 | 0 | rpf->rpf_addr = pim_rpf_find_rpf_addr(up); |
234 | |
|
235 | 0 | if (pim_rpf_addr_is_inaddr_any(rpf) && PIM_DEBUG_ZEBRA) { |
236 | | /* RPF'(S,G) not found */ |
237 | 0 | zlog_debug("%s(%s): RPF'%s not found: won't send join upstream", |
238 | 0 | __func__, caller, up->sg_str); |
239 | | /* warning only */ |
240 | 0 | } |
241 | | |
242 | | /* detect change in pim_nexthop */ |
243 | 0 | if (nexthop_mismatch(&rpf->source_nexthop, &saved.source_nexthop)) { |
244 | |
|
245 | 0 | if (PIM_DEBUG_ZEBRA) |
246 | 0 | zlog_debug("%s(%s): (S,G)=%s source nexthop now is: interface=%s address=%pPAs pref=%d metric=%d", |
247 | 0 | __func__, caller, |
248 | 0 | up->sg_str, |
249 | 0 | rpf->source_nexthop.interface ? rpf->source_nexthop.interface->name : "<ifname?>", |
250 | 0 | &rpf->source_nexthop.mrib_nexthop_addr, |
251 | 0 | rpf->source_nexthop.mrib_metric_preference, |
252 | 0 | rpf->source_nexthop.mrib_route_metric); |
253 | |
|
254 | 0 | pim_upstream_update_join_desired(pim, up); |
255 | 0 | pim_upstream_update_could_assert(up); |
256 | 0 | pim_upstream_update_my_assert_metric(up); |
257 | 0 | } |
258 | | |
259 | | /* detect change in RPF_interface(S) */ |
260 | 0 | if (saved.source_nexthop.interface != rpf->source_nexthop.interface) { |
261 | |
|
262 | 0 | if (PIM_DEBUG_ZEBRA) { |
263 | 0 | zlog_debug("%s(%s): (S,G)=%s RPF_interface(S) changed from %s to %s", |
264 | 0 | __func__, caller, |
265 | 0 | up->sg_str, |
266 | 0 | saved.source_nexthop.interface ? saved.source_nexthop.interface->name : "<oldif?>", |
267 | 0 | rpf->source_nexthop.interface ? rpf->source_nexthop.interface->name : "<newif?>"); |
268 | | /* warning only */ |
269 | 0 | } |
270 | |
|
271 | 0 | pim_upstream_rpf_interface_changed( |
272 | 0 | up, saved.source_nexthop.interface); |
273 | 0 | } |
274 | | |
275 | | /* detect change in RPF'(S,G) */ |
276 | 0 | if (pim_addr_cmp(saved.rpf_addr, rpf->rpf_addr) || |
277 | 0 | saved.source_nexthop.interface != rpf->source_nexthop.interface) { |
278 | 0 | pim_rpf_cost_change(pim, up, saved_mrib_route_metric); |
279 | 0 | return PIM_RPF_CHANGED; |
280 | 0 | } |
281 | | |
282 | 0 | if (PIM_DEBUG_MLAG) |
283 | 0 | zlog_debug( |
284 | 0 | "%s(%s): Cost_to_rp of upstream-%s changed to:%u", |
285 | 0 | __func__, caller, up->sg_str, |
286 | 0 | rpf->source_nexthop.mrib_route_metric); |
287 | |
|
288 | 0 | pim_rpf_cost_change(pim, up, saved_mrib_route_metric); |
289 | |
|
290 | 0 | return PIM_RPF_OK; |
291 | 0 | } |
292 | | |
293 | | /* |
294 | | * In the case of RP deletion and RP unreachablity, |
295 | | * uninstall the mroute in the kernel and clear the |
296 | | * rpf information in the pim upstream and pim channel |
297 | | * oil data structure. |
298 | | */ |
299 | | void pim_upstream_rpf_clear(struct pim_instance *pim, |
300 | | struct pim_upstream *up) |
301 | 68.6k | { |
302 | 68.6k | if (up->rpf.source_nexthop.interface) { |
303 | 0 | pim_upstream_switch(pim, up, PIM_UPSTREAM_NOTJOINED); |
304 | 0 | up->rpf.source_nexthop.interface = NULL; |
305 | 0 | up->rpf.source_nexthop.mrib_nexthop_addr = PIMADDR_ANY; |
306 | 0 | up->rpf.source_nexthop.mrib_metric_preference = |
307 | 0 | router->infinite_assert_metric.metric_preference; |
308 | 0 | up->rpf.source_nexthop.mrib_route_metric = |
309 | 0 | router->infinite_assert_metric.route_metric; |
310 | 0 | up->rpf.rpf_addr = PIMADDR_ANY; |
311 | 0 | pim_upstream_mroute_iif_update(up->channel_oil, __func__); |
312 | 0 | } |
313 | 68.6k | } |
314 | | |
315 | | /* |
316 | | RFC 4601: 4.1.6. State Summarization Macros |
317 | | |
318 | | neighbor RPF'(S,G) { |
319 | | if ( I_Am_Assert_Loser(S, G, RPF_interface(S) )) { |
320 | | return AssertWinner(S, G, RPF_interface(S) ) |
321 | | } else { |
322 | | return NBR( RPF_interface(S), MRIB.next_hop( S ) ) |
323 | | } |
324 | | } |
325 | | |
326 | | RPF'(*,G) and RPF'(S,G) indicate the neighbor from which data |
327 | | packets should be coming and to which joins should be sent on the RP |
328 | | tree and SPT, respectively. |
329 | | */ |
330 | | static pim_addr pim_rpf_find_rpf_addr(struct pim_upstream *up) |
331 | 0 | { |
332 | 0 | struct pim_ifchannel *rpf_ch; |
333 | 0 | struct pim_neighbor *neigh; |
334 | 0 | pim_addr rpf_addr; |
335 | |
|
336 | 0 | if (!up->rpf.source_nexthop.interface) { |
337 | 0 | zlog_warn("%s: missing RPF interface for upstream (S,G)=%s", |
338 | 0 | __func__, up->sg_str); |
339 | |
|
340 | 0 | return PIMADDR_ANY; |
341 | 0 | } |
342 | | |
343 | 0 | rpf_ch = pim_ifchannel_find(up->rpf.source_nexthop.interface, &up->sg); |
344 | 0 | if (rpf_ch) { |
345 | 0 | if (rpf_ch->ifassert_state == PIM_IFASSERT_I_AM_LOSER) { |
346 | 0 | return rpf_ch->ifassert_winner; |
347 | 0 | } |
348 | 0 | } |
349 | | |
350 | | /* return NBR( RPF_interface(S), MRIB.next_hop( S ) ) */ |
351 | | |
352 | 0 | neigh = pim_if_find_neighbor(up->rpf.source_nexthop.interface, |
353 | 0 | up->rpf.source_nexthop.mrib_nexthop_addr); |
354 | 0 | if (neigh) |
355 | 0 | rpf_addr = neigh->source_addr; |
356 | 0 | else |
357 | 0 | rpf_addr = PIMADDR_ANY; |
358 | |
|
359 | 0 | return rpf_addr; |
360 | 0 | } |
361 | | |
362 | | int pim_rpf_addr_is_inaddr_any(struct pim_rpf *rpf) |
363 | 241k | { |
364 | 241k | return pim_addr_is_any(rpf->rpf_addr); |
365 | 241k | } |
366 | | |
367 | | int pim_rpf_is_same(struct pim_rpf *rpf1, struct pim_rpf *rpf2) |
368 | 0 | { |
369 | 0 | if (rpf1->source_nexthop.interface == rpf2->source_nexthop.interface) |
370 | 0 | return 1; |
371 | | |
372 | 0 | return 0; |
373 | 0 | } |
374 | | |
375 | | unsigned int pim_rpf_hash_key(const void *arg) |
376 | 387k | { |
377 | 387k | const struct pim_nexthop_cache *r = arg; |
378 | | |
379 | 387k | #if PIM_IPV == 4 |
380 | 387k | return jhash_1word(r->rpf.rpf_addr.s_addr, 0); |
381 | | #else |
382 | | return jhash2(r->rpf.rpf_addr.s6_addr32, |
383 | | array_size(r->rpf.rpf_addr.s6_addr32), 0); |
384 | | #endif |
385 | 387k | } |
386 | | |
387 | | bool pim_rpf_equal(const void *arg1, const void *arg2) |
388 | 303k | { |
389 | 303k | const struct pim_nexthop_cache *r1 = |
390 | 303k | (const struct pim_nexthop_cache *)arg1; |
391 | 303k | const struct pim_nexthop_cache *r2 = |
392 | 303k | (const struct pim_nexthop_cache *)arg2; |
393 | | |
394 | 303k | return (!pim_addr_cmp(r1->rpf.rpf_addr, r2->rpf.rpf_addr)); |
395 | 303k | } |