]>
Commit | Line | Data |
---|---|---|
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 | |
39 | static long long last_route_change_time = -1; | |
40 | long long nexthop_lookups_avoided = 0; | |
12e41d03 | 41 | |
1bc98276 | 42 | static struct in_addr pim_rpf_find_rpf_addr (struct pim_upstream *up); |
12e41d03 | 43 | |
e71bf8f7 DS |
44 | void |
45 | 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 | ||
63b8f7a3 | 53 | int 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)); | |
70 | zlog_debug ("%s: Using last lookup for %s at %lld, %lld", | |
71 | __PRETTY_FUNCTION__, | |
72 | addr_str, | |
73 | nexthop->last_lookup_time, | |
74 | last_route_change_time); | |
75 | } | |
76 | nexthop_lookups_avoided++; | |
77 | return 0; | |
78 | } | |
79 | else | |
80 | { | |
81 | if (PIM_DEBUG_TRACE) | |
82 | { | |
83 | char addr_str[INET_ADDRSTRLEN]; | |
84 | pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str)); | |
85 | zlog_debug ("%s: Looking up: %s, last lookup time: %lld, %lld", | |
86 | __PRETTY_FUNCTION__, | |
87 | addr_str, | |
88 | nexthop->last_lookup_time, | |
89 | last_route_change_time); | |
90 | } | |
91 | } | |
92 | ||
7b2c4d16 | 93 | memset (nexthop_tab, 0, sizeof (struct pim_zlookup_nexthop) * MULTIPATH_NUM); |
f24405b1 | 94 | num_ifindex = zclient_lookup_nexthop(nexthop_tab, |
7b2c4d16 | 95 | MULTIPATH_NUM, |
f24405b1 DS |
96 | addr, PIM_NEXTHOP_LOOKUP_MAX); |
97 | if (num_ifindex < 1) { | |
eaa54bdb | 98 | char addr_str[INET_ADDRSTRLEN]; |
f24405b1 DS |
99 | pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str)); |
100 | zlog_warn("%s %s: could not find nexthop ifindex for address %s", | |
101 | __FILE__, __PRETTY_FUNCTION__, | |
102 | addr_str); | |
103 | return -1; | |
104 | } | |
105 | ||
518e5727 | 106 | while (!found && (i < num_ifindex)) |
63b8f7a3 DS |
107 | { |
108 | first_ifindex = nexthop_tab[i].ifindex; | |
109 | ||
7e2b7603 | 110 | ifp = if_lookup_by_index(first_ifindex, VRF_DEFAULT); |
63b8f7a3 DS |
111 | if (!ifp) |
112 | { | |
113 | if (PIM_DEBUG_ZEBRA) | |
114 | { | |
eaa54bdb | 115 | char addr_str[INET_ADDRSTRLEN]; |
63b8f7a3 DS |
116 | pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str)); |
117 | zlog_debug("%s %s: could not find interface for ifindex %d (address %s)", | |
118 | __FILE__, __PRETTY_FUNCTION__, | |
119 | first_ifindex, addr_str); | |
120 | } | |
28185336 DS |
121 | i++; |
122 | continue; | |
63b8f7a3 DS |
123 | } |
124 | ||
518e5727 | 125 | if (!ifp->info) |
63b8f7a3 | 126 | { |
518e5727 DS |
127 | if (PIM_DEBUG_ZEBRA) |
128 | { | |
129 | char addr_str[INET_ADDRSTRLEN]; | |
130 | pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str)); | |
131 | zlog_debug("%s: multicast not enabled on input interface %s (ifindex=%d, RPF for source %s)", | |
132 | __PRETTY_FUNCTION__, | |
133 | ifp->name, first_ifindex, addr_str); | |
134 | } | |
135 | i++; | |
63b8f7a3 | 136 | } |
518e5727 | 137 | else if (neighbor_needed && !pim_if_connected_to_source (ifp, addr)) |
63b8f7a3 | 138 | { |
63b8f7a3 | 139 | nbr = pim_neighbor_find (ifp, nexthop_tab[i].nexthop_addr.u.prefix4); |
802b226c | 140 | if (PIM_DEBUG_PIM_TRACE_DETAIL) |
63b8f7a3 DS |
141 | zlog_debug ("ifp name: %s, pim nbr: %p", ifp->name, nbr); |
142 | if (!nbr && !if_is_loopback (ifp)) | |
143 | i++; | |
144 | else | |
145 | found = 1; | |
146 | } | |
147 | else | |
148 | found = 1; | |
149 | } | |
150 | ||
151 | if (found) | |
152 | { | |
153 | if (PIM_DEBUG_ZEBRA) { | |
eaa54bdb DW |
154 | char nexthop_str[PREFIX_STRLEN]; |
155 | char addr_str[INET_ADDRSTRLEN]; | |
58c51da2 | 156 | pim_addr_dump("<nexthop?>", &nexthop_tab[i].nexthop_addr, nexthop_str, sizeof(nexthop_str)); |
63b8f7a3 DS |
157 | pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str)); |
158 | zlog_debug("%s %s: found nexthop %s for address %s: interface %s ifindex=%d metric=%d pref=%d", | |
159 | __FILE__, __PRETTY_FUNCTION__, | |
160 | nexthop_str, addr_str, | |
161 | ifp->name, first_ifindex, | |
58c51da2 | 162 | nexthop_tab[i].route_metric, |
163 | nexthop_tab[i].protocol_distance); | |
63b8f7a3 DS |
164 | } |
165 | /* update nextop data */ | |
166 | nexthop->interface = ifp; | |
58c51da2 | 167 | nexthop->mrib_nexthop_addr = nexthop_tab[i].nexthop_addr; |
168 | nexthop->mrib_metric_preference = nexthop_tab[i].protocol_distance; | |
169 | nexthop->mrib_route_metric = nexthop_tab[i].route_metric; | |
e71bf8f7 DS |
170 | nexthop->last_lookup = addr; |
171 | nexthop->last_lookup_time = pim_time_monotonic_usec(); | |
982bff89 | 172 | nexthop->nbr = nbr; |
63b8f7a3 DS |
173 | return 0; |
174 | } | |
175 | else | |
7f748d95 | 176 | return -1; |
12e41d03 DL |
177 | } |
178 | ||
179 | static int nexthop_mismatch(const struct pim_nexthop *nh1, | |
180 | const struct pim_nexthop *nh2) | |
181 | { | |
63c59d0c DS |
182 | return (nh1->interface != nh2->interface) || |
183 | (nh1->mrib_nexthop_addr.u.prefix4.s_addr != nh2->mrib_nexthop_addr.u.prefix4.s_addr) || | |
184 | (nh1->mrib_metric_preference != nh2->mrib_metric_preference) || | |
12e41d03 DL |
185 | (nh1->mrib_route_metric != nh2->mrib_route_metric); |
186 | } | |
187 | ||
1bc98276 | 188 | enum pim_rpf_result pim_rpf_update(struct pim_upstream *up, struct pim_rpf *old, uint8_t is_new) |
12e41d03 | 189 | { |
12e41d03 | 190 | struct pim_rpf *rpf = &up->rpf; |
42a0111b | 191 | struct pim_rpf saved; |
1bc98276 | 192 | struct prefix nht_p; |
12e41d03 | 193 | |
42a0111b DS |
194 | saved.source_nexthop = rpf->source_nexthop; |
195 | saved.rpf_addr = rpf->rpf_addr; | |
12e41d03 | 196 | |
1bc98276 CS |
197 | if (is_new) |
198 | { | |
199 | if (PIM_DEBUG_ZEBRA) | |
200 | { | |
201 | char source_str[INET_ADDRSTRLEN]; | |
202 | pim_inet4_dump("<source?>", up->upstream_addr, source_str, sizeof(source_str)); | |
203 | zlog_debug ("%s: NHT Register upstream %s addr %s with Zebra.", | |
204 | __PRETTY_FUNCTION__, up->sg_str, source_str); | |
205 | } | |
206 | /* Register addr with Zebra NHT */ | |
207 | nht_p.family = AF_INET; | |
208 | nht_p.prefixlen = IPV4_MAX_BITLEN; | |
209 | nht_p.u.prefix4.s_addr = up->upstream_addr.s_addr; | |
210 | pim_find_or_track_nexthop (&nht_p, up, NULL); | |
211 | } | |
212 | ||
63b8f7a3 | 213 | if (pim_nexthop_lookup(&rpf->source_nexthop, |
4a40c37a DS |
214 | up->upstream_addr, |
215 | !PIM_UPSTREAM_FLAG_TEST_FHR (up->flags) && | |
216 | !PIM_UPSTREAM_FLAG_TEST_SRC_IGMP (up->flags))) { | |
12e41d03 DL |
217 | return PIM_RPF_FAILURE; |
218 | } | |
219 | ||
63c59d0c DS |
220 | rpf->rpf_addr.family = AF_INET; |
221 | rpf->rpf_addr.u.prefix4 = pim_rpf_find_rpf_addr(up); | |
222 | if (pim_rpf_addr_is_inaddr_any(rpf) && PIM_DEBUG_ZEBRA) { | |
12e41d03 | 223 | /* RPF'(S,G) not found */ |
05e451f8 | 224 | zlog_debug("%s %s: RPF'%s not found: won't send join upstream", |
7adf0260 | 225 | __FILE__, __PRETTY_FUNCTION__, |
8bfb8b67 | 226 | up->sg_str); |
12e41d03 DL |
227 | /* warning only */ |
228 | } | |
229 | ||
230 | /* detect change in pim_nexthop */ | |
42a0111b | 231 | if (nexthop_mismatch(&rpf->source_nexthop, &saved.source_nexthop)) { |
12e41d03 | 232 | |
ff67a923 | 233 | if (PIM_DEBUG_ZEBRA) { |
eaa54bdb | 234 | char nhaddr_str[PREFIX_STRLEN]; |
63c59d0c | 235 | pim_addr_dump("<addr?>", &rpf->source_nexthop.mrib_nexthop_addr, nhaddr_str, sizeof(nhaddr_str)); |
05e451f8 | 236 | zlog_debug("%s %s: (S,G)=%s source nexthop now is: interface=%s address=%s pref=%d metric=%d", |
12e41d03 | 237 | __FILE__, __PRETTY_FUNCTION__, |
8bfb8b67 | 238 | up->sg_str, |
12e41d03 DL |
239 | rpf->source_nexthop.interface ? rpf->source_nexthop.interface->name : "<ifname?>", |
240 | nhaddr_str, | |
241 | rpf->source_nexthop.mrib_metric_preference, | |
242 | rpf->source_nexthop.mrib_route_metric); | |
12e41d03 DL |
243 | } |
244 | ||
245 | pim_upstream_update_join_desired(up); | |
246 | pim_upstream_update_could_assert(up); | |
247 | pim_upstream_update_my_assert_metric(up); | |
248 | } | |
249 | ||
250 | /* detect change in RPF_interface(S) */ | |
42a0111b | 251 | if (saved.source_nexthop.interface != rpf->source_nexthop.interface) { |
12e41d03 | 252 | |
ff67a923 | 253 | if (PIM_DEBUG_ZEBRA) { |
05e451f8 | 254 | zlog_debug("%s %s: (S,G)=%s RPF_interface(S) changed from %s to %s", |
12e41d03 | 255 | __FILE__, __PRETTY_FUNCTION__, |
8bfb8b67 | 256 | up->sg_str, |
42a0111b | 257 | saved.source_nexthop.interface ? saved.source_nexthop.interface->name : "<oldif?>", |
12e41d03 DL |
258 | rpf->source_nexthop.interface ? rpf->source_nexthop.interface->name : "<newif?>"); |
259 | /* warning only */ | |
260 | } | |
261 | ||
42a0111b | 262 | pim_upstream_rpf_interface_changed(up, saved.source_nexthop.interface); |
12e41d03 DL |
263 | } |
264 | ||
265 | /* detect change in RPF'(S,G) */ | |
ce2b6ce0 DS |
266 | if (saved.rpf_addr.u.prefix4.s_addr != rpf->rpf_addr.u.prefix4.s_addr || |
267 | saved.source_nexthop.interface != rpf->source_nexthop.interface) | |
268 | { | |
12e41d03 DL |
269 | |
270 | /* return old rpf to caller ? */ | |
42a0111b DS |
271 | if (old) |
272 | { | |
273 | old->source_nexthop = saved.source_nexthop; | |
274 | old->rpf_addr = saved.rpf_addr; | |
275 | } | |
12e41d03 DL |
276 | return PIM_RPF_CHANGED; |
277 | } | |
278 | ||
279 | return PIM_RPF_OK; | |
280 | } | |
281 | ||
282 | /* | |
283 | RFC 4601: 4.1.6. State Summarization Macros | |
284 | ||
285 | neighbor RPF'(S,G) { | |
286 | if ( I_Am_Assert_Loser(S, G, RPF_interface(S) )) { | |
287 | return AssertWinner(S, G, RPF_interface(S) ) | |
288 | } else { | |
289 | return NBR( RPF_interface(S), MRIB.next_hop( S ) ) | |
290 | } | |
291 | } | |
292 | ||
293 | RPF'(*,G) and RPF'(S,G) indicate the neighbor from which data | |
294 | packets should be coming and to which joins should be sent on the RP | |
295 | tree and SPT, respectively. | |
296 | */ | |
297 | static struct in_addr pim_rpf_find_rpf_addr(struct pim_upstream *up) | |
298 | { | |
299 | struct pim_ifchannel *rpf_ch; | |
300 | struct pim_neighbor *neigh; | |
301 | struct in_addr rpf_addr; | |
302 | ||
303 | if (!up->rpf.source_nexthop.interface) { | |
05e451f8 | 304 | zlog_warn("%s: missing RPF interface for upstream (S,G)=%s", |
12e41d03 | 305 | __PRETTY_FUNCTION__, |
8bfb8b67 | 306 | up->sg_str); |
12e41d03 DL |
307 | |
308 | rpf_addr.s_addr = PIM_NET_INADDR_ANY; | |
309 | return rpf_addr; | |
310 | } | |
311 | ||
312 | rpf_ch = pim_ifchannel_find(up->rpf.source_nexthop.interface, | |
5074a423 | 313 | &up->sg); |
12e41d03 DL |
314 | if (rpf_ch) { |
315 | if (rpf_ch->ifassert_state == PIM_IFASSERT_I_AM_LOSER) { | |
316 | return rpf_ch->ifassert_winner; | |
317 | } | |
318 | } | |
319 | ||
320 | /* return NBR( RPF_interface(S), MRIB.next_hop( S ) ) */ | |
321 | ||
322 | neigh = pim_if_find_neighbor(up->rpf.source_nexthop.interface, | |
63c59d0c | 323 | up->rpf.source_nexthop.mrib_nexthop_addr.u.prefix4); |
12e41d03 DL |
324 | if (neigh) |
325 | rpf_addr = neigh->source_addr; | |
326 | else | |
327 | rpf_addr.s_addr = PIM_NET_INADDR_ANY; | |
328 | ||
329 | return rpf_addr; | |
330 | } | |
63c59d0c DS |
331 | |
332 | int | |
333 | pim_rpf_addr_is_inaddr_none (struct pim_rpf *rpf) | |
334 | { | |
335 | switch (rpf->rpf_addr.family) | |
336 | { | |
fa8da98c | 337 | case AF_INET: |
63c59d0c DS |
338 | return rpf->rpf_addr.u.prefix4.s_addr == INADDR_NONE; |
339 | break; | |
fa8da98c | 340 | case AF_INET6: |
63c59d0c DS |
341 | zlog_warn ("%s: v6 Unimplmeneted", __PRETTY_FUNCTION__); |
342 | return 1; | |
343 | break; | |
344 | default: | |
345 | return 0; | |
346 | break; | |
347 | } | |
348 | ||
349 | return 0; | |
350 | } | |
351 | ||
352 | int | |
353 | pim_rpf_addr_is_inaddr_any (struct pim_rpf *rpf) | |
354 | { | |
355 | switch (rpf->rpf_addr.family) | |
356 | { | |
fa8da98c | 357 | case AF_INET: |
63c59d0c DS |
358 | return rpf->rpf_addr.u.prefix4.s_addr == INADDR_ANY; |
359 | break; | |
fa8da98c | 360 | case AF_INET6: |
63c59d0c DS |
361 | zlog_warn ("%s: v6 Unimplmented", __PRETTY_FUNCTION__); |
362 | return 1; | |
363 | break; | |
364 | default: | |
365 | return 0; | |
366 | break; | |
367 | } | |
368 | ||
369 | return 0; | |
370 | } | |
66cc32fa DS |
371 | |
372 | int | |
373 | pim_rpf_is_same (struct pim_rpf *rpf1, struct pim_rpf *rpf2) | |
374 | { | |
375 | if (rpf1->source_nexthop.interface == rpf2->source_nexthop.interface) | |
376 | return 1; | |
377 | ||
378 | return 0; | |
379 | } |