]> git.proxmox.com Git - mirror_frr.git/blame - pimd/pim_rpf.c
Merge pull request #531 from qlyoung/fix-stack-ref
[mirror_frr.git] / pimd / pim_rpf.c
CommitLineData
12e41d03
DL
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
16 along with this program; see the file COPYING; if not, write to the
17 Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
18 MA 02110-1301 USA
12e41d03
DL
19*/
20
21#include <zebra.h>
22
744d91b3
DS
23#include "if.h"
24
12e41d03
DL
25#include "log.h"
26#include "prefix.h"
27#include "memory.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"
e71bf8f7 36#include "pim_time.h"
1bc98276 37#include "pim_nht.h"
e71bf8f7
DS
38
39static long long last_route_change_time = -1;
40long long nexthop_lookups_avoided = 0;
12e41d03 41
1bc98276 42static struct in_addr pim_rpf_find_rpf_addr (struct pim_upstream *up);
12e41d03 43
e71bf8f7
DS
44void
45pim_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
63b8f7a3 53int pim_nexthop_lookup(struct pim_nexthop *nexthop, struct in_addr addr, int neighbor_needed)
12e41d03 54{
7b2c4d16 55 struct pim_zlookup_nexthop nexthop_tab[MULTIPATH_NUM];
982bff89 56 struct pim_neighbor *nbr = NULL;
12e41d03 57 int num_ifindex;
7a6e6567
DS
58 struct interface *ifp = NULL;
59 ifindex_t first_ifindex = 0;
63b8f7a3
DS
60 int found = 0;
61 int i = 0;
12e41d03 62
e71bf8f7
DS
63 if ((nexthop->last_lookup.s_addr == addr.s_addr) &&
64 (nexthop->last_lookup_time > last_route_change_time))
65 {
66 if (PIM_DEBUG_TRACE)
67 {
68 char addr_str[INET_ADDRSTRLEN];
69 pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str));
633988a7
CS
70 char nexthop_str[PREFIX_STRLEN];
71 pim_addr_dump("<nexthop?>", &nexthop->mrib_nexthop_addr,
72 nexthop_str, sizeof(nexthop_str));
73 zlog_debug ("%s: Using last lookup for %s at %lld, %lld addr%s",
e71bf8f7
DS
74 __PRETTY_FUNCTION__,
75 addr_str,
76 nexthop->last_lookup_time,
633988a7 77 last_route_change_time, nexthop_str);
e71bf8f7
DS
78 }
79 nexthop_lookups_avoided++;
80 return 0;
81 }
82 else
83 {
84 if (PIM_DEBUG_TRACE)
85 {
86 char addr_str[INET_ADDRSTRLEN];
87 pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str));
88 zlog_debug ("%s: Looking up: %s, last lookup time: %lld, %lld",
89 __PRETTY_FUNCTION__,
90 addr_str,
91 nexthop->last_lookup_time,
92 last_route_change_time);
93 }
94 }
95
7b2c4d16 96 memset (nexthop_tab, 0, sizeof (struct pim_zlookup_nexthop) * MULTIPATH_NUM);
f24405b1 97 num_ifindex = zclient_lookup_nexthop(nexthop_tab,
7b2c4d16 98 MULTIPATH_NUM,
f24405b1
DS
99 addr, PIM_NEXTHOP_LOOKUP_MAX);
100 if (num_ifindex < 1) {
eaa54bdb 101 char addr_str[INET_ADDRSTRLEN];
f24405b1
DS
102 pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str));
103 zlog_warn("%s %s: could not find nexthop ifindex for address %s",
104 __FILE__, __PRETTY_FUNCTION__,
105 addr_str);
106 return -1;
107 }
108
518e5727 109 while (!found && (i < num_ifindex))
63b8f7a3
DS
110 {
111 first_ifindex = nexthop_tab[i].ifindex;
112
7e2b7603 113 ifp = if_lookup_by_index(first_ifindex, VRF_DEFAULT);
63b8f7a3
DS
114 if (!ifp)
115 {
116 if (PIM_DEBUG_ZEBRA)
117 {
eaa54bdb 118 char addr_str[INET_ADDRSTRLEN];
63b8f7a3
DS
119 pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str));
120 zlog_debug("%s %s: could not find interface for ifindex %d (address %s)",
121 __FILE__, __PRETTY_FUNCTION__,
122 first_ifindex, addr_str);
123 }
28185336
DS
124 i++;
125 continue;
63b8f7a3
DS
126 }
127
518e5727 128 if (!ifp->info)
63b8f7a3 129 {
518e5727
DS
130 if (PIM_DEBUG_ZEBRA)
131 {
132 char addr_str[INET_ADDRSTRLEN];
133 pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str));
134 zlog_debug("%s: multicast not enabled on input interface %s (ifindex=%d, RPF for source %s)",
135 __PRETTY_FUNCTION__,
136 ifp->name, first_ifindex, addr_str);
137 }
138 i++;
63b8f7a3 139 }
518e5727 140 else if (neighbor_needed && !pim_if_connected_to_source (ifp, addr))
63b8f7a3 141 {
63b8f7a3 142 nbr = pim_neighbor_find (ifp, nexthop_tab[i].nexthop_addr.u.prefix4);
802b226c 143 if (PIM_DEBUG_PIM_TRACE_DETAIL)
63b8f7a3
DS
144 zlog_debug ("ifp name: %s, pim nbr: %p", ifp->name, nbr);
145 if (!nbr && !if_is_loopback (ifp))
146 i++;
147 else
148 found = 1;
149 }
150 else
151 found = 1;
152 }
153
154 if (found)
155 {
156 if (PIM_DEBUG_ZEBRA) {
eaa54bdb
DW
157 char nexthop_str[PREFIX_STRLEN];
158 char addr_str[INET_ADDRSTRLEN];
58c51da2 159 pim_addr_dump("<nexthop?>", &nexthop_tab[i].nexthop_addr, nexthop_str, sizeof(nexthop_str));
63b8f7a3
DS
160 pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str));
161 zlog_debug("%s %s: found nexthop %s for address %s: interface %s ifindex=%d metric=%d pref=%d",
162 __FILE__, __PRETTY_FUNCTION__,
163 nexthop_str, addr_str,
164 ifp->name, first_ifindex,
58c51da2 165 nexthop_tab[i].route_metric,
166 nexthop_tab[i].protocol_distance);
63b8f7a3
DS
167 }
168 /* update nextop data */
169 nexthop->interface = ifp;
58c51da2 170 nexthop->mrib_nexthop_addr = nexthop_tab[i].nexthop_addr;
171 nexthop->mrib_metric_preference = nexthop_tab[i].protocol_distance;
172 nexthop->mrib_route_metric = nexthop_tab[i].route_metric;
e71bf8f7
DS
173 nexthop->last_lookup = addr;
174 nexthop->last_lookup_time = pim_time_monotonic_usec();
982bff89 175 nexthop->nbr = nbr;
63b8f7a3
DS
176 return 0;
177 }
178 else
7f748d95 179 return -1;
12e41d03
DL
180}
181
182static int nexthop_mismatch(const struct pim_nexthop *nh1,
183 const struct pim_nexthop *nh2)
184{
63c59d0c
DS
185 return (nh1->interface != nh2->interface) ||
186 (nh1->mrib_nexthop_addr.u.prefix4.s_addr != nh2->mrib_nexthop_addr.u.prefix4.s_addr) ||
187 (nh1->mrib_metric_preference != nh2->mrib_metric_preference) ||
12e41d03
DL
188 (nh1->mrib_route_metric != nh2->mrib_route_metric);
189}
190
1bc98276 191enum pim_rpf_result pim_rpf_update(struct pim_upstream *up, struct pim_rpf *old, uint8_t is_new)
12e41d03 192{
12e41d03 193 struct pim_rpf *rpf = &up->rpf;
42a0111b 194 struct pim_rpf saved;
1bc98276 195 struct prefix nht_p;
633988a7
CS
196 struct pim_nexthop_cache pnc;
197 int ret = 0;
198 struct prefix src, grp;
12e41d03 199
42a0111b
DS
200 saved.source_nexthop = rpf->source_nexthop;
201 saved.rpf_addr = rpf->rpf_addr;
12e41d03 202
633988a7
CS
203 if (is_new && PIM_DEBUG_ZEBRA)
204 {
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 ((ret = pim_find_or_track_nexthop (&nht_p, up, NULL, &pnc)) == 1)
1bc98276 224 {
633988a7 225 if (pnc.nexthop_num)
1bc98276 226 {
633988a7 227 //Compute PIM RPF using Cached nexthop
1131c2eb
CS
228 if (pim_ecmp_nexthop_search (&pnc, &up->rpf.source_nexthop,
229 &src, &grp,
230 !PIM_UPSTREAM_FLAG_TEST_FHR (up->flags) &&
231 !PIM_UPSTREAM_FLAG_TEST_SRC_IGMP (up->flags)))
232
233 {
234 return PIM_RPF_FAILURE;
235 }
1bc98276 236 }
1bc98276 237 }
633988a7
CS
238 else
239 {
240 if (pim_ecmp_nexthop_lookup (&rpf->source_nexthop,
241 up->upstream_addr, &src, &grp,
242 !PIM_UPSTREAM_FLAG_TEST_FHR (up->flags) &&
4ba87bb9 243 !PIM_UPSTREAM_FLAG_TEST_SRC_IGMP (up->flags)))
633988a7
CS
244 {
245 return PIM_RPF_FAILURE;
246 }
247 }
12e41d03 248
63c59d0c
DS
249 rpf->rpf_addr.family = AF_INET;
250 rpf->rpf_addr.u.prefix4 = pim_rpf_find_rpf_addr(up);
251 if (pim_rpf_addr_is_inaddr_any(rpf) && PIM_DEBUG_ZEBRA) {
12e41d03 252 /* RPF'(S,G) not found */
05e451f8 253 zlog_debug("%s %s: RPF'%s not found: won't send join upstream",
7adf0260 254 __FILE__, __PRETTY_FUNCTION__,
8bfb8b67 255 up->sg_str);
12e41d03
DL
256 /* warning only */
257 }
258
259 /* detect change in pim_nexthop */
42a0111b 260 if (nexthop_mismatch(&rpf->source_nexthop, &saved.source_nexthop)) {
12e41d03 261
ff67a923 262 if (PIM_DEBUG_ZEBRA) {
eaa54bdb 263 char nhaddr_str[PREFIX_STRLEN];
63c59d0c 264 pim_addr_dump("<addr?>", &rpf->source_nexthop.mrib_nexthop_addr, nhaddr_str, sizeof(nhaddr_str));
05e451f8 265 zlog_debug("%s %s: (S,G)=%s source nexthop now is: interface=%s address=%s pref=%d metric=%d",
12e41d03 266 __FILE__, __PRETTY_FUNCTION__,
8bfb8b67 267 up->sg_str,
12e41d03
DL
268 rpf->source_nexthop.interface ? rpf->source_nexthop.interface->name : "<ifname?>",
269 nhaddr_str,
270 rpf->source_nexthop.mrib_metric_preference,
271 rpf->source_nexthop.mrib_route_metric);
12e41d03
DL
272 }
273
274 pim_upstream_update_join_desired(up);
275 pim_upstream_update_could_assert(up);
276 pim_upstream_update_my_assert_metric(up);
277 }
278
279 /* detect change in RPF_interface(S) */
42a0111b 280 if (saved.source_nexthop.interface != rpf->source_nexthop.interface) {
12e41d03 281
ff67a923 282 if (PIM_DEBUG_ZEBRA) {
05e451f8 283 zlog_debug("%s %s: (S,G)=%s RPF_interface(S) changed from %s to %s",
12e41d03 284 __FILE__, __PRETTY_FUNCTION__,
8bfb8b67 285 up->sg_str,
42a0111b 286 saved.source_nexthop.interface ? saved.source_nexthop.interface->name : "<oldif?>",
12e41d03
DL
287 rpf->source_nexthop.interface ? rpf->source_nexthop.interface->name : "<newif?>");
288 /* warning only */
289 }
290
42a0111b 291 pim_upstream_rpf_interface_changed(up, saved.source_nexthop.interface);
12e41d03
DL
292 }
293
294 /* detect change in RPF'(S,G) */
ce2b6ce0
DS
295 if (saved.rpf_addr.u.prefix4.s_addr != rpf->rpf_addr.u.prefix4.s_addr ||
296 saved.source_nexthop.interface != rpf->source_nexthop.interface)
297 {
12e41d03
DL
298
299 /* return old rpf to caller ? */
42a0111b
DS
300 if (old)
301 {
302 old->source_nexthop = saved.source_nexthop;
303 old->rpf_addr = saved.rpf_addr;
304 }
12e41d03
DL
305 return PIM_RPF_CHANGED;
306 }
307
308 return PIM_RPF_OK;
309}
310
311/*
312 RFC 4601: 4.1.6. State Summarization Macros
313
314 neighbor RPF'(S,G) {
315 if ( I_Am_Assert_Loser(S, G, RPF_interface(S) )) {
316 return AssertWinner(S, G, RPF_interface(S) )
317 } else {
318 return NBR( RPF_interface(S), MRIB.next_hop( S ) )
319 }
320 }
321
322 RPF'(*,G) and RPF'(S,G) indicate the neighbor from which data
323 packets should be coming and to which joins should be sent on the RP
324 tree and SPT, respectively.
325*/
326static struct in_addr pim_rpf_find_rpf_addr(struct pim_upstream *up)
327{
328 struct pim_ifchannel *rpf_ch;
329 struct pim_neighbor *neigh;
330 struct in_addr rpf_addr;
331
332 if (!up->rpf.source_nexthop.interface) {
05e451f8 333 zlog_warn("%s: missing RPF interface for upstream (S,G)=%s",
12e41d03 334 __PRETTY_FUNCTION__,
8bfb8b67 335 up->sg_str);
12e41d03
DL
336
337 rpf_addr.s_addr = PIM_NET_INADDR_ANY;
338 return rpf_addr;
339 }
340
341 rpf_ch = pim_ifchannel_find(up->rpf.source_nexthop.interface,
5074a423 342 &up->sg);
12e41d03
DL
343 if (rpf_ch) {
344 if (rpf_ch->ifassert_state == PIM_IFASSERT_I_AM_LOSER) {
345 return rpf_ch->ifassert_winner;
346 }
347 }
348
349 /* return NBR( RPF_interface(S), MRIB.next_hop( S ) ) */
350
351 neigh = pim_if_find_neighbor(up->rpf.source_nexthop.interface,
63c59d0c 352 up->rpf.source_nexthop.mrib_nexthop_addr.u.prefix4);
12e41d03
DL
353 if (neigh)
354 rpf_addr = neigh->source_addr;
355 else
356 rpf_addr.s_addr = PIM_NET_INADDR_ANY;
357
358 return rpf_addr;
359}
63c59d0c
DS
360
361int
362pim_rpf_addr_is_inaddr_none (struct pim_rpf *rpf)
363{
364 switch (rpf->rpf_addr.family)
365 {
fa8da98c 366 case AF_INET:
63c59d0c
DS
367 return rpf->rpf_addr.u.prefix4.s_addr == INADDR_NONE;
368 break;
fa8da98c 369 case AF_INET6:
63c59d0c
DS
370 zlog_warn ("%s: v6 Unimplmeneted", __PRETTY_FUNCTION__);
371 return 1;
372 break;
373 default:
374 return 0;
375 break;
376 }
377
378 return 0;
379}
380
381int
382pim_rpf_addr_is_inaddr_any (struct pim_rpf *rpf)
383{
384 switch (rpf->rpf_addr.family)
385 {
fa8da98c 386 case AF_INET:
63c59d0c
DS
387 return rpf->rpf_addr.u.prefix4.s_addr == INADDR_ANY;
388 break;
fa8da98c 389 case AF_INET6:
63c59d0c
DS
390 zlog_warn ("%s: v6 Unimplmented", __PRETTY_FUNCTION__);
391 return 1;
392 break;
393 default:
394 return 0;
395 break;
396 }
397
398 return 0;
399}
66cc32fa
DS
400
401int
402pim_rpf_is_same (struct pim_rpf *rpf1, struct pim_rpf *rpf2)
403{
404 if (rpf1->source_nexthop.interface == rpf2->source_nexthop.interface)
405 return 1;
406
407 return 0;
408}