]>
Commit | Line | Data |
---|---|---|
12e41d03 | 1 | /* |
896014f4 DL |
2 | * PIM for Quagga |
3 | * Copyright (C) 2008 Everton da Silva Marques | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or modify | |
6 | * it under the terms of the GNU General Public License as published by | |
7 | * the Free Software Foundation; either version 2 of the License, or | |
8 | * (at your option) any later version. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, but | |
11 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | * General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License along | |
16 | * with this program; see the file COPYING; if not, write to the Free Software | |
17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
18 | */ | |
12e41d03 DL |
19 | |
20 | #include <zebra.h> | |
21 | ||
744d91b3 DS |
22 | #include "if.h" |
23 | ||
12e41d03 DL |
24 | #include "log.h" |
25 | #include "prefix.h" | |
26 | #include "memory.h" | |
c2cf4b02 | 27 | #include "jhash.h" |
12e41d03 DL |
28 | |
29 | #include "pimd.h" | |
30 | #include "pim_rpf.h" | |
31 | #include "pim_pim.h" | |
32 | #include "pim_str.h" | |
33 | #include "pim_iface.h" | |
34 | #include "pim_zlookup.h" | |
35 | #include "pim_ifchannel.h" | |
e71bf8f7 | 36 | #include "pim_time.h" |
1bc98276 | 37 | #include "pim_nht.h" |
cc0cecae | 38 | #include "pim_oil.h" |
e71bf8f7 | 39 | |
d62a17ae | 40 | static struct in_addr pim_rpf_find_rpf_addr(struct pim_upstream *up); |
12e41d03 | 41 | |
bfc92019 | 42 | void pim_rpf_set_refresh_time(struct pim_instance *pim) |
e71bf8f7 | 43 | { |
bfc92019 | 44 | pim->last_route_change_time = pim_time_monotonic_usec(); |
d62a17ae | 45 | if (PIM_DEBUG_TRACE) |
bfc92019 DS |
46 | zlog_debug("%s: vrf(%s) New last route change time: %" PRId64, |
47 | __PRETTY_FUNCTION__, pim->vrf->name, | |
48 | pim->last_route_change_time); | |
e71bf8f7 DS |
49 | } |
50 | ||
ade155e1 DS |
51 | bool pim_nexthop_lookup(struct pim_instance *pim, struct pim_nexthop *nexthop, |
52 | struct in_addr addr, int neighbor_needed) | |
12e41d03 | 53 | { |
d62a17ae | 54 | struct pim_zlookup_nexthop nexthop_tab[MULTIPATH_NUM]; |
55 | struct pim_neighbor *nbr = NULL; | |
56 | int num_ifindex; | |
57 | struct interface *ifp = NULL; | |
58 | ifindex_t first_ifindex = 0; | |
59 | int found = 0; | |
60 | int i = 0; | |
61 | ||
ea89ab14 DS |
62 | /* |
63 | * We should not attempt to lookup a | |
64 | * 255.255.255.255 address, since | |
65 | * it will never work | |
66 | */ | |
67 | if (addr.s_addr == INADDR_NONE) | |
ade155e1 | 68 | return false; |
ea89ab14 | 69 | |
d62a17ae | 70 | if ((nexthop->last_lookup.s_addr == addr.s_addr) |
bfc92019 | 71 | && (nexthop->last_lookup_time > pim->last_route_change_time)) { |
d62a17ae | 72 | if (PIM_DEBUG_TRACE) { |
73 | char addr_str[INET_ADDRSTRLEN]; | |
74 | pim_inet4_dump("<addr?>", addr, addr_str, | |
75 | sizeof(addr_str)); | |
76 | char nexthop_str[PREFIX_STRLEN]; | |
77 | pim_addr_dump("<nexthop?>", &nexthop->mrib_nexthop_addr, | |
78 | nexthop_str, sizeof(nexthop_str)); | |
79 | zlog_debug( | |
5271457d | 80 | "%s: Using last lookup for %s at %lld, %" PRId64 " addr %s", |
d62a17ae | 81 | __PRETTY_FUNCTION__, addr_str, |
82 | nexthop->last_lookup_time, | |
bfc92019 | 83 | pim->last_route_change_time, nexthop_str); |
d62a17ae | 84 | } |
bfc92019 | 85 | pim->nexthop_lookups_avoided++; |
ade155e1 | 86 | return true; |
d62a17ae | 87 | } else { |
88 | if (PIM_DEBUG_TRACE) { | |
89 | char addr_str[INET_ADDRSTRLEN]; | |
90 | pim_inet4_dump("<addr?>", addr, addr_str, | |
91 | sizeof(addr_str)); | |
92 | zlog_debug( | |
bfc92019 | 93 | "%s: Looking up: %s, last lookup time: %lld, %" PRId64, |
d62a17ae | 94 | __PRETTY_FUNCTION__, addr_str, |
95 | nexthop->last_lookup_time, | |
bfc92019 | 96 | pim->last_route_change_time); |
d62a17ae | 97 | } |
e71bf8f7 | 98 | } |
d62a17ae | 99 | |
100 | memset(nexthop_tab, 0, | |
101 | sizeof(struct pim_zlookup_nexthop) * MULTIPATH_NUM); | |
71edad0f DS |
102 | num_ifindex = zclient_lookup_nexthop(pim, nexthop_tab, MULTIPATH_NUM, |
103 | addr, PIM_NEXTHOP_LOOKUP_MAX); | |
d62a17ae | 104 | if (num_ifindex < 1) { |
105 | char addr_str[INET_ADDRSTRLEN]; | |
106 | pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str)); | |
107 | zlog_warn( | |
108 | "%s %s: could not find nexthop ifindex for address %s", | |
109 | __FILE__, __PRETTY_FUNCTION__, addr_str); | |
ade155e1 | 110 | return false; |
d62a17ae | 111 | } |
112 | ||
113 | while (!found && (i < num_ifindex)) { | |
114 | first_ifindex = nexthop_tab[i].ifindex; | |
115 | ||
cc0cecae | 116 | ifp = if_lookup_by_index(first_ifindex, pim->vrf_id); |
d62a17ae | 117 | if (!ifp) { |
118 | if (PIM_DEBUG_ZEBRA) { | |
119 | char addr_str[INET_ADDRSTRLEN]; | |
120 | pim_inet4_dump("<addr?>", addr, addr_str, | |
121 | sizeof(addr_str)); | |
122 | zlog_debug( | |
123 | "%s %s: could not find interface for ifindex %d (address %s)", | |
124 | __FILE__, __PRETTY_FUNCTION__, | |
125 | first_ifindex, addr_str); | |
126 | } | |
127 | i++; | |
128 | continue; | |
129 | } | |
130 | ||
131 | if (!ifp->info) { | |
132 | if (PIM_DEBUG_ZEBRA) { | |
133 | char addr_str[INET_ADDRSTRLEN]; | |
134 | pim_inet4_dump("<addr?>", addr, addr_str, | |
135 | sizeof(addr_str)); | |
136 | zlog_debug( | |
137 | "%s: multicast not enabled on input interface %s (ifindex=%d, RPF for source %s)", | |
138 | __PRETTY_FUNCTION__, ifp->name, | |
139 | first_ifindex, addr_str); | |
140 | } | |
141 | i++; | |
142 | } else if (neighbor_needed | |
143 | && !pim_if_connected_to_source(ifp, addr)) { | |
144 | nbr = pim_neighbor_find( | |
145 | ifp, nexthop_tab[i].nexthop_addr.u.prefix4); | |
146 | if (PIM_DEBUG_PIM_TRACE_DETAIL) | |
147 | zlog_debug("ifp name: %s, pim nbr: %p", | |
148 | ifp->name, nbr); | |
149 | if (!nbr && !if_is_loopback(ifp)) | |
150 | i++; | |
151 | else | |
152 | found = 1; | |
153 | } else | |
154 | found = 1; | |
e71bf8f7 | 155 | } |
d62a17ae | 156 | |
157 | if (found) { | |
158 | if (PIM_DEBUG_ZEBRA) { | |
159 | char nexthop_str[PREFIX_STRLEN]; | |
160 | char addr_str[INET_ADDRSTRLEN]; | |
161 | pim_addr_dump("<nexthop?>", | |
162 | &nexthop_tab[i].nexthop_addr, nexthop_str, | |
163 | sizeof(nexthop_str)); | |
164 | pim_inet4_dump("<addr?>", addr, addr_str, | |
165 | sizeof(addr_str)); | |
166 | zlog_debug( | |
167 | "%s %s: found nexthop %s for address %s: interface %s ifindex=%d metric=%d pref=%d", | |
168 | __FILE__, __PRETTY_FUNCTION__, nexthop_str, | |
169 | addr_str, ifp->name, first_ifindex, | |
170 | nexthop_tab[i].route_metric, | |
171 | nexthop_tab[i].protocol_distance); | |
172 | } | |
1a81b790 | 173 | /* update nexthop data */ |
d62a17ae | 174 | nexthop->interface = ifp; |
175 | nexthop->mrib_nexthop_addr = nexthop_tab[i].nexthop_addr; | |
176 | nexthop->mrib_metric_preference = | |
177 | nexthop_tab[i].protocol_distance; | |
178 | nexthop->mrib_route_metric = nexthop_tab[i].route_metric; | |
179 | nexthop->last_lookup = addr; | |
180 | nexthop->last_lookup_time = pim_time_monotonic_usec(); | |
181 | nexthop->nbr = nbr; | |
ade155e1 | 182 | return true; |
d62a17ae | 183 | } else |
ade155e1 | 184 | return false; |
12e41d03 DL |
185 | } |
186 | ||
187 | static int nexthop_mismatch(const struct pim_nexthop *nh1, | |
188 | const struct pim_nexthop *nh2) | |
189 | { | |
d62a17ae | 190 | return (nh1->interface != nh2->interface) |
191 | || (nh1->mrib_nexthop_addr.u.prefix4.s_addr | |
192 | != nh2->mrib_nexthop_addr.u.prefix4.s_addr) | |
193 | || (nh1->mrib_metric_preference != nh2->mrib_metric_preference) | |
194 | || (nh1->mrib_route_metric != nh2->mrib_route_metric); | |
12e41d03 DL |
195 | } |
196 | ||
2002dcdb | 197 | enum pim_rpf_result pim_rpf_update(struct pim_instance *pim, |
ae14da48 | 198 | struct pim_upstream *up, struct pim_rpf *old) |
12e41d03 | 199 | { |
d62a17ae | 200 | struct pim_rpf *rpf = &up->rpf; |
201 | struct pim_rpf saved; | |
202 | struct prefix nht_p; | |
d62a17ae | 203 | struct prefix src, grp; |
57695eb6 | 204 | bool neigh_needed = true; |
d62a17ae | 205 | |
6a5de0ad AK |
206 | if (PIM_UPSTREAM_FLAG_TEST_STATIC_IIF(up->flags)) |
207 | return PIM_RPF_OK; | |
208 | ||
957d93ea SP |
209 | if (up->upstream_addr.s_addr == INADDR_ANY) { |
210 | zlog_debug("%s: RP is not configured yet for %s", | |
211 | __PRETTY_FUNCTION__, up->sg_str); | |
212 | return PIM_RPF_OK; | |
213 | } | |
214 | ||
d62a17ae | 215 | saved.source_nexthop = rpf->source_nexthop; |
216 | saved.rpf_addr = rpf->rpf_addr; | |
217 | ||
d62a17ae | 218 | nht_p.family = AF_INET; |
219 | nht_p.prefixlen = IPV4_MAX_BITLEN; | |
220 | nht_p.u.prefix4.s_addr = up->upstream_addr.s_addr; | |
221 | ||
222 | src.family = AF_INET; | |
223 | src.prefixlen = IPV4_MAX_BITLEN; | |
224 | src.u.prefix4 = up->upstream_addr; // RP or Src address | |
225 | grp.family = AF_INET; | |
226 | grp.prefixlen = IPV4_MAX_BITLEN; | |
227 | grp.u.prefix4 = up->sg.grp; | |
57695eb6 DS |
228 | |
229 | if ((up->sg.src.s_addr == INADDR_ANY && I_am_RP(pim, up->sg.grp)) || | |
230 | PIM_UPSTREAM_FLAG_TEST_FHR(up->flags)) | |
2951a7a4 | 231 | neigh_needed = false; |
4533b847 | 232 | pim_find_or_track_nexthop(pim, &nht_p, up, NULL, false, NULL); |
43763b11 DS |
233 | if (!pim_ecmp_nexthop_lookup(pim, &rpf->source_nexthop, &src, &grp, |
234 | neigh_needed)) | |
235 | return PIM_RPF_FAILURE; | |
d62a17ae | 236 | |
237 | rpf->rpf_addr.family = AF_INET; | |
238 | rpf->rpf_addr.u.prefix4 = pim_rpf_find_rpf_addr(up); | |
239 | if (pim_rpf_addr_is_inaddr_any(rpf) && PIM_DEBUG_ZEBRA) { | |
240 | /* RPF'(S,G) not found */ | |
241 | zlog_debug("%s %s: RPF'%s not found: won't send join upstream", | |
242 | __FILE__, __PRETTY_FUNCTION__, up->sg_str); | |
243 | /* warning only */ | |
244 | } | |
245 | ||
246 | /* detect change in pim_nexthop */ | |
247 | if (nexthop_mismatch(&rpf->source_nexthop, &saved.source_nexthop)) { | |
248 | ||
249 | if (PIM_DEBUG_ZEBRA) { | |
250 | char nhaddr_str[PREFIX_STRLEN]; | |
251 | pim_addr_dump("<addr?>", | |
252 | &rpf->source_nexthop.mrib_nexthop_addr, | |
253 | nhaddr_str, sizeof(nhaddr_str)); | |
254 | zlog_debug("%s %s: (S,G)=%s source nexthop now is: interface=%s address=%s pref=%d metric=%d", | |
12e41d03 | 255 | __FILE__, __PRETTY_FUNCTION__, |
8bfb8b67 | 256 | up->sg_str, |
12e41d03 DL |
257 | rpf->source_nexthop.interface ? rpf->source_nexthop.interface->name : "<ifname?>", |
258 | nhaddr_str, | |
259 | rpf->source_nexthop.mrib_metric_preference, | |
260 | rpf->source_nexthop.mrib_route_metric); | |
d62a17ae | 261 | } |
12e41d03 | 262 | |
cc0cecae | 263 | pim_upstream_update_join_desired(pim, up); |
d62a17ae | 264 | pim_upstream_update_could_assert(up); |
265 | pim_upstream_update_my_assert_metric(up); | |
266 | } | |
12e41d03 | 267 | |
d62a17ae | 268 | /* detect change in RPF_interface(S) */ |
269 | if (saved.source_nexthop.interface != rpf->source_nexthop.interface) { | |
12e41d03 | 270 | |
d62a17ae | 271 | if (PIM_DEBUG_ZEBRA) { |
272 | zlog_debug("%s %s: (S,G)=%s RPF_interface(S) changed from %s to %s", | |
12e41d03 | 273 | __FILE__, __PRETTY_FUNCTION__, |
8bfb8b67 | 274 | up->sg_str, |
42a0111b | 275 | saved.source_nexthop.interface ? saved.source_nexthop.interface->name : "<oldif?>", |
12e41d03 | 276 | rpf->source_nexthop.interface ? rpf->source_nexthop.interface->name : "<newif?>"); |
d62a17ae | 277 | /* warning only */ |
278 | } | |
279 | ||
280 | pim_upstream_rpf_interface_changed( | |
281 | up, saved.source_nexthop.interface); | |
282 | } | |
283 | ||
284 | /* detect change in RPF'(S,G) */ | |
285 | if (saved.rpf_addr.u.prefix4.s_addr != rpf->rpf_addr.u.prefix4.s_addr | |
286 | || saved.source_nexthop | |
287 | .interface != rpf->source_nexthop.interface) { | |
288 | ||
289 | /* return old rpf to caller ? */ | |
290 | if (old) { | |
291 | old->source_nexthop = saved.source_nexthop; | |
292 | old->rpf_addr = saved.rpf_addr; | |
293 | } | |
294 | return PIM_RPF_CHANGED; | |
295 | } | |
296 | ||
297 | return PIM_RPF_OK; | |
12e41d03 DL |
298 | } |
299 | ||
1250cb5d SP |
300 | /* |
301 | * In the case of RP deletion and RP unreachablity, | |
302 | * uninstall the mroute in the kernel and clear the | |
303 | * rpf information in the pim upstream and pim channel | |
304 | * oil data structure. | |
305 | */ | |
306 | void pim_upstream_rpf_clear(struct pim_instance *pim, | |
307 | struct pim_upstream *up) | |
308 | { | |
309 | if (up->rpf.source_nexthop.interface) { | |
afb2f470 DS |
310 | if (up->channel_oil) |
311 | pim_channel_oil_change_iif(pim, up->channel_oil, | |
312 | MAXVIFS, | |
313 | __PRETTY_FUNCTION__); | |
1250cb5d | 314 | |
1250cb5d SP |
315 | pim_upstream_switch(pim, up, PIM_UPSTREAM_NOTJOINED); |
316 | up->rpf.source_nexthop.interface = NULL; | |
317 | up->rpf.source_nexthop.mrib_nexthop_addr.u.prefix4.s_addr = | |
318 | PIM_NET_INADDR_ANY; | |
319 | up->rpf.source_nexthop.mrib_metric_preference = | |
320 | router->infinite_assert_metric.metric_preference; | |
321 | up->rpf.source_nexthop.mrib_route_metric = | |
322 | router->infinite_assert_metric.route_metric; | |
323 | up->rpf.rpf_addr.u.prefix4.s_addr = PIM_NET_INADDR_ANY; | |
324 | } | |
325 | } | |
326 | ||
12e41d03 DL |
327 | /* |
328 | RFC 4601: 4.1.6. State Summarization Macros | |
329 | ||
330 | neighbor RPF'(S,G) { | |
d62a17ae | 331 | if ( I_Am_Assert_Loser(S, G, RPF_interface(S) )) { |
332 | return AssertWinner(S, G, RPF_interface(S) ) | |
333 | } else { | |
334 | return NBR( RPF_interface(S), MRIB.next_hop( S ) ) | |
335 | } | |
12e41d03 DL |
336 | } |
337 | ||
338 | RPF'(*,G) and RPF'(S,G) indicate the neighbor from which data | |
339 | packets should be coming and to which joins should be sent on the RP | |
340 | tree and SPT, respectively. | |
341 | */ | |
342 | static struct in_addr pim_rpf_find_rpf_addr(struct pim_upstream *up) | |
343 | { | |
d62a17ae | 344 | struct pim_ifchannel *rpf_ch; |
345 | struct pim_neighbor *neigh; | |
346 | struct in_addr rpf_addr; | |
347 | ||
348 | if (!up->rpf.source_nexthop.interface) { | |
349 | zlog_warn("%s: missing RPF interface for upstream (S,G)=%s", | |
350 | __PRETTY_FUNCTION__, up->sg_str); | |
351 | ||
352 | rpf_addr.s_addr = PIM_NET_INADDR_ANY; | |
353 | return rpf_addr; | |
354 | } | |
355 | ||
356 | rpf_ch = pim_ifchannel_find(up->rpf.source_nexthop.interface, &up->sg); | |
357 | if (rpf_ch) { | |
358 | if (rpf_ch->ifassert_state == PIM_IFASSERT_I_AM_LOSER) { | |
359 | return rpf_ch->ifassert_winner; | |
360 | } | |
361 | } | |
362 | ||
363 | /* return NBR( RPF_interface(S), MRIB.next_hop( S ) ) */ | |
364 | ||
365 | neigh = pim_if_find_neighbor( | |
366 | up->rpf.source_nexthop.interface, | |
367 | up->rpf.source_nexthop.mrib_nexthop_addr.u.prefix4); | |
368 | if (neigh) | |
369 | rpf_addr = neigh->source_addr; | |
370 | else | |
371 | rpf_addr.s_addr = PIM_NET_INADDR_ANY; | |
372 | ||
373 | return rpf_addr; | |
12e41d03 | 374 | } |
63c59d0c | 375 | |
d62a17ae | 376 | int pim_rpf_addr_is_inaddr_none(struct pim_rpf *rpf) |
63c59d0c | 377 | { |
d62a17ae | 378 | switch (rpf->rpf_addr.family) { |
379 | case AF_INET: | |
380 | return rpf->rpf_addr.u.prefix4.s_addr == INADDR_NONE; | |
381 | break; | |
382 | case AF_INET6: | |
383 | zlog_warn("%s: v6 Unimplmeneted", __PRETTY_FUNCTION__); | |
384 | return 1; | |
385 | break; | |
386 | default: | |
387 | return 0; | |
388 | break; | |
389 | } | |
390 | ||
391 | return 0; | |
63c59d0c DS |
392 | } |
393 | ||
d62a17ae | 394 | int pim_rpf_addr_is_inaddr_any(struct pim_rpf *rpf) |
63c59d0c | 395 | { |
d62a17ae | 396 | switch (rpf->rpf_addr.family) { |
397 | case AF_INET: | |
398 | return rpf->rpf_addr.u.prefix4.s_addr == INADDR_ANY; | |
399 | break; | |
400 | case AF_INET6: | |
401 | zlog_warn("%s: v6 Unimplmented", __PRETTY_FUNCTION__); | |
402 | return 1; | |
403 | break; | |
404 | default: | |
405 | return 0; | |
406 | break; | |
407 | } | |
408 | ||
409 | return 0; | |
63c59d0c | 410 | } |
66cc32fa | 411 | |
d62a17ae | 412 | int pim_rpf_is_same(struct pim_rpf *rpf1, struct pim_rpf *rpf2) |
66cc32fa | 413 | { |
d62a17ae | 414 | if (rpf1->source_nexthop.interface == rpf2->source_nexthop.interface) |
415 | return 1; | |
66cc32fa | 416 | |
d62a17ae | 417 | return 0; |
66cc32fa | 418 | } |
c2cf4b02 | 419 | |
d8b87afe | 420 | unsigned int pim_rpf_hash_key(const void *arg) |
c2cf4b02 | 421 | { |
d8b87afe | 422 | const struct pim_nexthop_cache *r = arg; |
c2cf4b02 DS |
423 | |
424 | return jhash_1word(r->rpf.rpf_addr.u.prefix4.s_addr, 0); | |
425 | } | |
426 | ||
74df8d6d | 427 | bool pim_rpf_equal(const void *arg1, const void *arg2) |
c2cf4b02 DS |
428 | { |
429 | const struct pim_nexthop_cache *r1 = | |
430 | (const struct pim_nexthop_cache *)arg1; | |
431 | const struct pim_nexthop_cache *r2 = | |
432 | (const struct pim_nexthop_cache *)arg2; | |
433 | ||
434 | return prefix_same(&r1->rpf.rpf_addr, &r2->rpf.rpf_addr); | |
435 | } |