]> git.proxmox.com Git - mirror_frr.git/blob - pimd/pim_rpf.c
Merge pull request #964 from opensourcerouting/plist-trie-corruption-3.0
[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 long long last_route_change_time = -1;
41 long long nexthop_lookups_avoided = 0;
42
43 static struct in_addr pim_rpf_find_rpf_addr(struct pim_upstream *up);
44
45 void pim_rpf_set_refresh_time(void)
46 {
47 last_route_change_time = pim_time_monotonic_usec();
48 if (PIM_DEBUG_TRACE)
49 zlog_debug("%s: New last route change time: %lld",
50 __PRETTY_FUNCTION__, last_route_change_time);
51 }
52
53 int pim_nexthop_lookup(struct pim_instance *pim, struct pim_nexthop *nexthop,
54 struct in_addr addr, int neighbor_needed)
55 {
56 struct pim_zlookup_nexthop nexthop_tab[MULTIPATH_NUM];
57 struct pim_neighbor *nbr = NULL;
58 int num_ifindex;
59 struct interface *ifp = NULL;
60 ifindex_t first_ifindex = 0;
61 int found = 0;
62 int i = 0;
63
64 if ((nexthop->last_lookup.s_addr == addr.s_addr)
65 && (nexthop->last_lookup_time > last_route_change_time)) {
66 if (PIM_DEBUG_TRACE) {
67 char addr_str[INET_ADDRSTRLEN];
68 pim_inet4_dump("<addr?>", addr, addr_str,
69 sizeof(addr_str));
70 char nexthop_str[PREFIX_STRLEN];
71 pim_addr_dump("<nexthop?>", &nexthop->mrib_nexthop_addr,
72 nexthop_str, sizeof(nexthop_str));
73 zlog_debug(
74 "%s: Using last lookup for %s at %lld, %lld addr%s",
75 __PRETTY_FUNCTION__, addr_str,
76 nexthop->last_lookup_time,
77 last_route_change_time, nexthop_str);
78 }
79 nexthop_lookups_avoided++;
80 return 0;
81 } else {
82 if (PIM_DEBUG_TRACE) {
83 char addr_str[INET_ADDRSTRLEN];
84 pim_inet4_dump("<addr?>", addr, addr_str,
85 sizeof(addr_str));
86 zlog_debug(
87 "%s: Looking up: %s, last lookup time: %lld, %lld",
88 __PRETTY_FUNCTION__, addr_str,
89 nexthop->last_lookup_time,
90 last_route_change_time);
91 }
92 }
93
94 memset(nexthop_tab, 0,
95 sizeof(struct pim_zlookup_nexthop) * MULTIPATH_NUM);
96 num_ifindex = zclient_lookup_nexthop(pim, nexthop_tab, MULTIPATH_NUM,
97 addr, PIM_NEXTHOP_LOOKUP_MAX);
98 if (num_ifindex < 1) {
99 char addr_str[INET_ADDRSTRLEN];
100 pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str));
101 zlog_warn(
102 "%s %s: could not find nexthop ifindex for address %s",
103 __FILE__, __PRETTY_FUNCTION__, addr_str);
104 return -1;
105 }
106
107 while (!found && (i < num_ifindex)) {
108 first_ifindex = nexthop_tab[i].ifindex;
109
110 ifp = if_lookup_by_index(first_ifindex, pim->vrf_id);
111 if (!ifp) {
112 if (PIM_DEBUG_ZEBRA) {
113 char addr_str[INET_ADDRSTRLEN];
114 pim_inet4_dump("<addr?>", addr, addr_str,
115 sizeof(addr_str));
116 zlog_debug(
117 "%s %s: could not find interface for ifindex %d (address %s)",
118 __FILE__, __PRETTY_FUNCTION__,
119 first_ifindex, addr_str);
120 }
121 i++;
122 continue;
123 }
124
125 if (!ifp->info) {
126 if (PIM_DEBUG_ZEBRA) {
127 char addr_str[INET_ADDRSTRLEN];
128 pim_inet4_dump("<addr?>", addr, addr_str,
129 sizeof(addr_str));
130 zlog_debug(
131 "%s: multicast not enabled on input interface %s (ifindex=%d, RPF for source %s)",
132 __PRETTY_FUNCTION__, ifp->name,
133 first_ifindex, addr_str);
134 }
135 i++;
136 } else if (neighbor_needed
137 && !pim_if_connected_to_source(ifp, addr)) {
138 nbr = pim_neighbor_find(
139 ifp, nexthop_tab[i].nexthop_addr.u.prefix4);
140 if (PIM_DEBUG_PIM_TRACE_DETAIL)
141 zlog_debug("ifp name: %s, pim nbr: %p",
142 ifp->name, nbr);
143 if (!nbr && !if_is_loopback(ifp))
144 i++;
145 else
146 found = 1;
147 } else
148 found = 1;
149 }
150
151 if (found) {
152 if (PIM_DEBUG_ZEBRA) {
153 char nexthop_str[PREFIX_STRLEN];
154 char addr_str[INET_ADDRSTRLEN];
155 pim_addr_dump("<nexthop?>",
156 &nexthop_tab[i].nexthop_addr, nexthop_str,
157 sizeof(nexthop_str));
158 pim_inet4_dump("<addr?>", addr, addr_str,
159 sizeof(addr_str));
160 zlog_debug(
161 "%s %s: found nexthop %s for address %s: interface %s ifindex=%d metric=%d pref=%d",
162 __FILE__, __PRETTY_FUNCTION__, nexthop_str,
163 addr_str, ifp->name, first_ifindex,
164 nexthop_tab[i].route_metric,
165 nexthop_tab[i].protocol_distance);
166 }
167 /* update nextop data */
168 nexthop->interface = ifp;
169 nexthop->mrib_nexthop_addr = nexthop_tab[i].nexthop_addr;
170 nexthop->mrib_metric_preference =
171 nexthop_tab[i].protocol_distance;
172 nexthop->mrib_route_metric = nexthop_tab[i].route_metric;
173 nexthop->last_lookup = addr;
174 nexthop->last_lookup_time = pim_time_monotonic_usec();
175 nexthop->nbr = nbr;
176 return 0;
177 } else
178 return -1;
179 }
180
181 static int nexthop_mismatch(const struct pim_nexthop *nh1,
182 const struct pim_nexthop *nh2)
183 {
184 return (nh1->interface != nh2->interface)
185 || (nh1->mrib_nexthop_addr.u.prefix4.s_addr
186 != nh2->mrib_nexthop_addr.u.prefix4.s_addr)
187 || (nh1->mrib_metric_preference != nh2->mrib_metric_preference)
188 || (nh1->mrib_route_metric != nh2->mrib_route_metric);
189 }
190
191 enum pim_rpf_result pim_rpf_update(struct pim_instance *pim,
192 struct pim_upstream *up, struct pim_rpf *old,
193 uint8_t is_new)
194 {
195 struct pim_rpf *rpf = &up->rpf;
196 struct pim_rpf saved;
197 struct prefix nht_p;
198 struct pim_nexthop_cache pnc;
199 struct prefix src, grp;
200
201 saved.source_nexthop = rpf->source_nexthop;
202 saved.rpf_addr = rpf->rpf_addr;
203
204 if (is_new && PIM_DEBUG_ZEBRA) {
205 char source_str[INET_ADDRSTRLEN];
206 pim_inet4_dump("<source?>", up->upstream_addr, source_str,
207 sizeof(source_str));
208 zlog_debug("%s: NHT Register upstream %s addr %s with Zebra.",
209 __PRETTY_FUNCTION__, up->sg_str, source_str);
210 }
211 /* Register addr with Zebra NHT */
212 nht_p.family = AF_INET;
213 nht_p.prefixlen = IPV4_MAX_BITLEN;
214 nht_p.u.prefix4.s_addr = up->upstream_addr.s_addr;
215
216 src.family = AF_INET;
217 src.prefixlen = IPV4_MAX_BITLEN;
218 src.u.prefix4 = up->upstream_addr; // RP or Src address
219 grp.family = AF_INET;
220 grp.prefixlen = IPV4_MAX_BITLEN;
221 grp.u.prefix4 = up->sg.grp;
222 memset(&pnc, 0, sizeof(struct pim_nexthop_cache));
223 if (pim_find_or_track_nexthop(pim, &nht_p, up, NULL, &pnc)) {
224 if (pnc.nexthop_num) {
225 if (!pim_ecmp_nexthop_search(
226 pim, &pnc, &up->rpf.source_nexthop, &src,
227 &grp,
228 !PIM_UPSTREAM_FLAG_TEST_FHR(up->flags)
229 && !PIM_UPSTREAM_FLAG_TEST_SRC_IGMP(
230 up->flags)))
231 return PIM_RPF_FAILURE;
232 }
233 } else {
234 if (!pim_ecmp_nexthop_lookup(
235 pim, &rpf->source_nexthop, up->upstream_addr, &src,
236 &grp, !PIM_UPSTREAM_FLAG_TEST_FHR(up->flags)
237 && !PIM_UPSTREAM_FLAG_TEST_SRC_IGMP(
238 up->flags)))
239 return PIM_RPF_FAILURE;
240 }
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 __FILE__, __PRETTY_FUNCTION__, 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 __FILE__, __PRETTY_FUNCTION__,
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 __FILE__, __PRETTY_FUNCTION__,
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 old rpf to caller ? */
295 if (old) {
296 old->source_nexthop = saved.source_nexthop;
297 old->rpf_addr = saved.rpf_addr;
298 }
299 return PIM_RPF_CHANGED;
300 }
301
302 return PIM_RPF_OK;
303 }
304
305 /*
306 RFC 4601: 4.1.6. State Summarization Macros
307
308 neighbor RPF'(S,G) {
309 if ( I_Am_Assert_Loser(S, G, RPF_interface(S) )) {
310 return AssertWinner(S, G, RPF_interface(S) )
311 } else {
312 return NBR( RPF_interface(S), MRIB.next_hop( S ) )
313 }
314 }
315
316 RPF'(*,G) and RPF'(S,G) indicate the neighbor from which data
317 packets should be coming and to which joins should be sent on the RP
318 tree and SPT, respectively.
319 */
320 static struct in_addr pim_rpf_find_rpf_addr(struct pim_upstream *up)
321 {
322 struct pim_ifchannel *rpf_ch;
323 struct pim_neighbor *neigh;
324 struct in_addr rpf_addr;
325
326 if (!up->rpf.source_nexthop.interface) {
327 zlog_warn("%s: missing RPF interface for upstream (S,G)=%s",
328 __PRETTY_FUNCTION__, up->sg_str);
329
330 rpf_addr.s_addr = PIM_NET_INADDR_ANY;
331 return rpf_addr;
332 }
333
334 rpf_ch = pim_ifchannel_find(up->rpf.source_nexthop.interface, &up->sg);
335 if (rpf_ch) {
336 if (rpf_ch->ifassert_state == PIM_IFASSERT_I_AM_LOSER) {
337 return rpf_ch->ifassert_winner;
338 }
339 }
340
341 /* return NBR( RPF_interface(S), MRIB.next_hop( S ) ) */
342
343 neigh = pim_if_find_neighbor(
344 up->rpf.source_nexthop.interface,
345 up->rpf.source_nexthop.mrib_nexthop_addr.u.prefix4);
346 if (neigh)
347 rpf_addr = neigh->source_addr;
348 else
349 rpf_addr.s_addr = PIM_NET_INADDR_ANY;
350
351 return rpf_addr;
352 }
353
354 int pim_rpf_addr_is_inaddr_none(struct pim_rpf *rpf)
355 {
356 switch (rpf->rpf_addr.family) {
357 case AF_INET:
358 return rpf->rpf_addr.u.prefix4.s_addr == INADDR_NONE;
359 break;
360 case AF_INET6:
361 zlog_warn("%s: v6 Unimplmeneted", __PRETTY_FUNCTION__);
362 return 1;
363 break;
364 default:
365 return 0;
366 break;
367 }
368
369 return 0;
370 }
371
372 int pim_rpf_addr_is_inaddr_any(struct pim_rpf *rpf)
373 {
374 switch (rpf->rpf_addr.family) {
375 case AF_INET:
376 return rpf->rpf_addr.u.prefix4.s_addr == INADDR_ANY;
377 break;
378 case AF_INET6:
379 zlog_warn("%s: v6 Unimplmented", __PRETTY_FUNCTION__);
380 return 1;
381 break;
382 default:
383 return 0;
384 break;
385 }
386
387 return 0;
388 }
389
390 int pim_rpf_is_same(struct pim_rpf *rpf1, struct pim_rpf *rpf2)
391 {
392 if (rpf1->source_nexthop.interface == rpf2->source_nexthop.interface)
393 return 1;
394
395 return 0;
396 }
397
398 unsigned int pim_rpf_hash_key(void *arg)
399 {
400 struct pim_nexthop_cache *r = (struct pim_nexthop_cache *)arg;
401
402 return jhash_1word(r->rpf.rpf_addr.u.prefix4.s_addr, 0);
403 }
404
405 int pim_rpf_equal(const void *arg1, const void *arg2)
406 {
407 const struct pim_nexthop_cache *r1 =
408 (const struct pim_nexthop_cache *)arg1;
409 const struct pim_nexthop_cache *r2 =
410 (const struct pim_nexthop_cache *)arg2;
411
412 return prefix_same(&r1->rpf.rpf_addr, &r2->rpf.rpf_addr);
413 }