]> git.proxmox.com Git - mirror_frr.git/blob - pimd/pim_rpf.c
Merge pull request #5767 from ton31337/fix/replace_s_addr_0_to_INADDR_ANY
[mirror_frr.git] / pimd / pim_rpf.c
1 /*
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 */
19
20 #include <zebra.h>
21
22 #include "if.h"
23
24 #include "log.h"
25 #include "prefix.h"
26 #include "memory.h"
27 #include "jhash.h"
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"
36 #include "pim_time.h"
37 #include "pim_nht.h"
38 #include "pim_oil.h"
39
40 static struct in_addr pim_rpf_find_rpf_addr(struct pim_upstream *up);
41
42 void pim_rpf_set_refresh_time(struct pim_instance *pim)
43 {
44 pim->last_route_change_time = pim_time_monotonic_usec();
45 if (PIM_DEBUG_PIM_TRACE)
46 zlog_debug("%s: vrf(%s) New last route change time: %" PRId64,
47 __PRETTY_FUNCTION__, pim->vrf->name,
48 pim->last_route_change_time);
49 }
50
51 bool pim_nexthop_lookup(struct pim_instance *pim, struct pim_nexthop *nexthop,
52 struct in_addr addr, int neighbor_needed)
53 {
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
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)
68 return false;
69
70 if ((nexthop->last_lookup.s_addr == addr.s_addr)
71 && (nexthop->last_lookup_time > pim->last_route_change_time)) {
72 if (PIM_DEBUG_PIM_NHT) {
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(
80 "%s: Using last lookup for %s at %lld, %" PRId64 " addr %s",
81 __PRETTY_FUNCTION__, addr_str,
82 nexthop->last_lookup_time,
83 pim->last_route_change_time, nexthop_str);
84 }
85 pim->nexthop_lookups_avoided++;
86 return true;
87 } else {
88 if (PIM_DEBUG_PIM_NHT) {
89 char addr_str[INET_ADDRSTRLEN];
90 pim_inet4_dump("<addr?>", addr, addr_str,
91 sizeof(addr_str));
92 zlog_debug(
93 "%s: Looking up: %s, last lookup time: %lld, %" PRId64,
94 __PRETTY_FUNCTION__, addr_str,
95 nexthop->last_lookup_time,
96 pim->last_route_change_time);
97 }
98 }
99
100 memset(nexthop_tab, 0,
101 sizeof(struct pim_zlookup_nexthop) * MULTIPATH_NUM);
102 num_ifindex = zclient_lookup_nexthop(pim, nexthop_tab, MULTIPATH_NUM,
103 addr, PIM_NEXTHOP_LOOKUP_MAX);
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);
110 return false;
111 }
112
113 while (!found && (i < num_ifindex)) {
114 first_ifindex = nexthop_tab[i].ifindex;
115
116 ifp = if_lookup_by_index(first_ifindex, pim->vrf_id);
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;
155 }
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 }
173 /* update nexthop data */
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;
182 return true;
183 } else
184 return false;
185 }
186
187 static int nexthop_mismatch(const struct pim_nexthop *nh1,
188 const struct pim_nexthop *nh2)
189 {
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);
195 }
196
197 enum pim_rpf_result pim_rpf_update(struct pim_instance *pim,
198 struct pim_upstream *up, struct pim_rpf *old,
199 const char *caller)
200 {
201 struct pim_rpf *rpf = &up->rpf;
202 struct pim_rpf saved;
203 struct prefix nht_p;
204 struct prefix src, grp;
205 bool neigh_needed = true;
206
207 if (PIM_UPSTREAM_FLAG_TEST_STATIC_IIF(up->flags))
208 return PIM_RPF_OK;
209
210 if (up->upstream_addr.s_addr == INADDR_ANY) {
211 zlog_debug("%s(%s): RP is not configured yet for %s",
212 __func__, caller, up->sg_str);
213 return PIM_RPF_OK;
214 }
215
216 saved.source_nexthop = rpf->source_nexthop;
217 saved.rpf_addr = rpf->rpf_addr;
218 if (old) {
219 old->source_nexthop = saved.source_nexthop;
220 old->rpf_addr = saved.rpf_addr;
221 }
222
223 nht_p.family = AF_INET;
224 nht_p.prefixlen = IPV4_MAX_BITLEN;
225 nht_p.u.prefix4.s_addr = up->upstream_addr.s_addr;
226
227 src.family = AF_INET;
228 src.prefixlen = IPV4_MAX_BITLEN;
229 src.u.prefix4 = up->upstream_addr; // RP or Src address
230 grp.family = AF_INET;
231 grp.prefixlen = IPV4_MAX_BITLEN;
232 grp.u.prefix4 = up->sg.grp;
233
234 if ((up->sg.src.s_addr == INADDR_ANY && I_am_RP(pim, up->sg.grp)) ||
235 PIM_UPSTREAM_FLAG_TEST_FHR(up->flags))
236 neigh_needed = false;
237 pim_find_or_track_nexthop(pim, &nht_p, up, NULL, false, NULL);
238 if (!pim_ecmp_nexthop_lookup(pim, &rpf->source_nexthop, &src, &grp,
239 neigh_needed))
240 return PIM_RPF_FAILURE;
241
242 rpf->rpf_addr.family = AF_INET;
243 rpf->rpf_addr.u.prefix4 = pim_rpf_find_rpf_addr(up);
244 if (pim_rpf_addr_is_inaddr_any(rpf) && PIM_DEBUG_ZEBRA) {
245 /* RPF'(S,G) not found */
246 zlog_debug("%s(%s): RPF'%s not found: won't send join upstream",
247 __func__, caller, up->sg_str);
248 /* warning only */
249 }
250
251 /* detect change in pim_nexthop */
252 if (nexthop_mismatch(&rpf->source_nexthop, &saved.source_nexthop)) {
253
254 if (PIM_DEBUG_ZEBRA) {
255 char nhaddr_str[PREFIX_STRLEN];
256 pim_addr_dump("<addr?>",
257 &rpf->source_nexthop.mrib_nexthop_addr,
258 nhaddr_str, sizeof(nhaddr_str));
259 zlog_debug("%s(%s): (S,G)=%s source nexthop now is: interface=%s address=%s pref=%d metric=%d",
260 __func__, caller,
261 up->sg_str,
262 rpf->source_nexthop.interface ? rpf->source_nexthop.interface->name : "<ifname?>",
263 nhaddr_str,
264 rpf->source_nexthop.mrib_metric_preference,
265 rpf->source_nexthop.mrib_route_metric);
266 }
267
268 pim_upstream_update_join_desired(pim, up);
269 pim_upstream_update_could_assert(up);
270 pim_upstream_update_my_assert_metric(up);
271 }
272
273 /* detect change in RPF_interface(S) */
274 if (saved.source_nexthop.interface != rpf->source_nexthop.interface) {
275
276 if (PIM_DEBUG_ZEBRA) {
277 zlog_debug("%s(%s): (S,G)=%s RPF_interface(S) changed from %s to %s",
278 __func__, caller,
279 up->sg_str,
280 saved.source_nexthop.interface ? saved.source_nexthop.interface->name : "<oldif?>",
281 rpf->source_nexthop.interface ? rpf->source_nexthop.interface->name : "<newif?>");
282 /* warning only */
283 }
284
285 pim_upstream_rpf_interface_changed(
286 up, saved.source_nexthop.interface);
287 }
288
289 /* detect change in RPF'(S,G) */
290 if (saved.rpf_addr.u.prefix4.s_addr != rpf->rpf_addr.u.prefix4.s_addr
291 || saved.source_nexthop
292 .interface != rpf->source_nexthop.interface) {
293
294 return PIM_RPF_CHANGED;
295 }
296
297 return PIM_RPF_OK;
298 }
299
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) {
310 up->rpf.source_nexthop.interface = NULL;
311 up->rpf.source_nexthop.mrib_nexthop_addr.u.prefix4.s_addr =
312 PIM_NET_INADDR_ANY;
313 up->rpf.source_nexthop.mrib_metric_preference =
314 router->infinite_assert_metric.metric_preference;
315 up->rpf.source_nexthop.mrib_route_metric =
316 router->infinite_assert_metric.route_metric;
317 up->rpf.rpf_addr.u.prefix4.s_addr = PIM_NET_INADDR_ANY;
318 pim_upstream_mroute_iif_update(up->channel_oil, __func__);
319 }
320 }
321
322 /*
323 RFC 4601: 4.1.6. State Summarization Macros
324
325 neighbor RPF'(S,G) {
326 if ( I_Am_Assert_Loser(S, G, RPF_interface(S) )) {
327 return AssertWinner(S, G, RPF_interface(S) )
328 } else {
329 return NBR( RPF_interface(S), MRIB.next_hop( S ) )
330 }
331 }
332
333 RPF'(*,G) and RPF'(S,G) indicate the neighbor from which data
334 packets should be coming and to which joins should be sent on the RP
335 tree and SPT, respectively.
336 */
337 static struct in_addr pim_rpf_find_rpf_addr(struct pim_upstream *up)
338 {
339 struct pim_ifchannel *rpf_ch;
340 struct pim_neighbor *neigh;
341 struct in_addr rpf_addr;
342
343 if (!up->rpf.source_nexthop.interface) {
344 zlog_warn("%s: missing RPF interface for upstream (S,G)=%s",
345 __PRETTY_FUNCTION__, up->sg_str);
346
347 rpf_addr.s_addr = PIM_NET_INADDR_ANY;
348 return rpf_addr;
349 }
350
351 rpf_ch = pim_ifchannel_find(up->rpf.source_nexthop.interface, &up->sg);
352 if (rpf_ch) {
353 if (rpf_ch->ifassert_state == PIM_IFASSERT_I_AM_LOSER) {
354 return rpf_ch->ifassert_winner;
355 }
356 }
357
358 /* return NBR( RPF_interface(S), MRIB.next_hop( S ) ) */
359
360 neigh = pim_if_find_neighbor(
361 up->rpf.source_nexthop.interface,
362 up->rpf.source_nexthop.mrib_nexthop_addr.u.prefix4);
363 if (neigh)
364 rpf_addr = neigh->source_addr;
365 else
366 rpf_addr.s_addr = PIM_NET_INADDR_ANY;
367
368 return rpf_addr;
369 }
370
371 int pim_rpf_addr_is_inaddr_none(struct pim_rpf *rpf)
372 {
373 switch (rpf->rpf_addr.family) {
374 case AF_INET:
375 return rpf->rpf_addr.u.prefix4.s_addr == INADDR_NONE;
376 break;
377 case AF_INET6:
378 zlog_warn("%s: v6 Unimplmeneted", __PRETTY_FUNCTION__);
379 return 1;
380 break;
381 default:
382 return 0;
383 break;
384 }
385
386 return 0;
387 }
388
389 int pim_rpf_addr_is_inaddr_any(struct pim_rpf *rpf)
390 {
391 switch (rpf->rpf_addr.family) {
392 case AF_INET:
393 return rpf->rpf_addr.u.prefix4.s_addr == INADDR_ANY;
394 break;
395 case AF_INET6:
396 zlog_warn("%s: v6 Unimplmented", __PRETTY_FUNCTION__);
397 return 1;
398 break;
399 default:
400 return 0;
401 break;
402 }
403
404 return 0;
405 }
406
407 int pim_rpf_is_same(struct pim_rpf *rpf1, struct pim_rpf *rpf2)
408 {
409 if (rpf1->source_nexthop.interface == rpf2->source_nexthop.interface)
410 return 1;
411
412 return 0;
413 }
414
415 unsigned int pim_rpf_hash_key(const void *arg)
416 {
417 const struct pim_nexthop_cache *r = arg;
418
419 return jhash_1word(r->rpf.rpf_addr.u.prefix4.s_addr, 0);
420 }
421
422 bool pim_rpf_equal(const void *arg1, const void *arg2)
423 {
424 const struct pim_nexthop_cache *r1 =
425 (const struct pim_nexthop_cache *)arg1;
426 const struct pim_nexthop_cache *r2 =
427 (const struct pim_nexthop_cache *)arg2;
428
429 return prefix_same(&r1->rpf.rpf_addr, &r2->rpf.rpf_addr);
430 }