]> git.proxmox.com Git - mirror_frr.git/blob - pimd/pim_zlookup.c
Merge pull request #883 from daveolson53/master
[mirror_frr.git] / pimd / pim_zlookup.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 #include "zebra/rib.h"
22
23 #include "log.h"
24 #include "prefix.h"
25 #include "zclient.h"
26 #include "stream.h"
27 #include "network.h"
28 #include "thread.h"
29 #include "prefix.h"
30 #include "vty.h"
31
32 #include "pimd.h"
33 #include "pim_iface.h"
34 #include "pim_pim.h"
35 #include "pim_str.h"
36 #include "pim_oil.h"
37 #include "pim_zlookup.h"
38
39 static struct zclient *zlookup = NULL;
40
41 static void zclient_lookup_sched(struct zclient *zlookup, int delay);
42
43 /* Connect to zebra for nexthop lookup. */
44 static int zclient_lookup_connect(struct thread *t)
45 {
46 struct zclient *zlookup;
47
48 zlookup = THREAD_ARG(t);
49
50 if (zlookup->sock >= 0) {
51 return 0;
52 }
53
54 if (zclient_socket_connect(zlookup) < 0) {
55 ++zlookup->fail;
56 zlog_warn("%s: failure connecting zclient socket: failures=%d",
57 __PRETTY_FUNCTION__, zlookup->fail);
58 } else {
59 zlookup->fail = 0; /* reset counter on connection */
60 }
61
62 if (zlookup->sock < 0) {
63 /* Since last connect failed, retry within 10 secs */
64 zclient_lookup_sched(zlookup, 10);
65 return -1;
66 }
67
68 return 0;
69 }
70
71 /* Schedule connection with delay. */
72 static void zclient_lookup_sched(struct zclient *zlookup, int delay)
73 {
74 thread_add_timer(master, zclient_lookup_connect, zlookup, delay,
75 &zlookup->t_connect);
76
77 zlog_notice("%s: zclient lookup connection scheduled for %d seconds",
78 __PRETTY_FUNCTION__, delay);
79 }
80
81 /* Schedule connection for now. */
82 static void zclient_lookup_sched_now(struct zclient *zlookup)
83 {
84 thread_add_event(master, zclient_lookup_connect, zlookup, 0,
85 &zlookup->t_connect);
86
87 zlog_notice("%s: zclient lookup immediate connection scheduled",
88 __PRETTY_FUNCTION__);
89 }
90
91 /* Schedule reconnection, if needed. */
92 static void zclient_lookup_reconnect(struct zclient *zlookup)
93 {
94 if (zlookup->t_connect) {
95 return;
96 }
97
98 zclient_lookup_sched_now(zlookup);
99 }
100
101 static void zclient_lookup_failed(struct zclient *zlookup)
102 {
103 if (zlookup->sock >= 0) {
104 if (close(zlookup->sock)) {
105 zlog_warn("%s: closing fd=%d: errno=%d %s", __func__,
106 zlookup->sock, errno, safe_strerror(errno));
107 }
108 zlookup->sock = -1;
109 }
110
111 zclient_lookup_reconnect(zlookup);
112 }
113
114 void zclient_lookup_free(void)
115 {
116 zclient_stop(zlookup);
117 zclient_free(zlookup);
118 zlookup = NULL;
119 }
120
121 void zclient_lookup_new(void)
122 {
123 zlookup = zclient_new(master);
124 if (!zlookup) {
125 zlog_err("%s: zclient_new() failure", __PRETTY_FUNCTION__);
126 return;
127 }
128
129 zlookup->sock = -1;
130 zlookup->t_connect = NULL;
131
132 zclient_lookup_sched_now(zlookup);
133
134 zlog_notice("%s: zclient lookup socket initialized",
135 __PRETTY_FUNCTION__);
136 }
137
138 static int zclient_read_nexthop(struct pim_instance *pim,
139 struct zclient *zlookup,
140 struct pim_zlookup_nexthop nexthop_tab[],
141 const int tab_size, struct in_addr addr)
142 {
143 int num_ifindex = 0;
144 struct stream *s;
145 uint16_t length;
146 u_char marker;
147 u_char version;
148 vrf_id_t vrf_id;
149 uint16_t command = 0;
150 struct in_addr raddr;
151 uint8_t distance;
152 uint32_t metric;
153 int nexthop_num;
154 int i, err;
155
156 if (PIM_DEBUG_PIM_NHT_DETAIL) {
157 char addr_str[INET_ADDRSTRLEN];
158 pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str));
159 zlog_debug("%s: addr=%s(%s)", __PRETTY_FUNCTION__, addr_str,
160 pim->vrf->name);
161 }
162
163 s = zlookup->ibuf;
164
165 while (command != ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB) {
166 stream_reset(s);
167 err = zclient_read_header(s, zlookup->sock, &length, &marker,
168 &version, &vrf_id, &command);
169 if (err < 0) {
170 zlog_err("%s: zclient_read_header() failed",
171 __PRETTY_FUNCTION__);
172 zclient_lookup_failed(zlookup);
173 return -1;
174 }
175 }
176
177 raddr.s_addr = stream_get_ipv4(s);
178
179 if (raddr.s_addr != addr.s_addr) {
180 char addr_str[INET_ADDRSTRLEN];
181 char raddr_str[INET_ADDRSTRLEN];
182 pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str));
183 pim_inet4_dump("<raddr?>", raddr, raddr_str, sizeof(raddr_str));
184 zlog_warn("%s: address mismatch: addr=%s(%s) raddr=%s",
185 __PRETTY_FUNCTION__, addr_str, pim->vrf->name,
186 raddr_str);
187 /* warning only */
188 }
189
190 distance = stream_getc(s);
191 metric = stream_getl(s);
192 nexthop_num = stream_getc(s);
193
194 if (nexthop_num < 1) {
195 if (PIM_DEBUG_PIM_NHT_DETAIL)
196 zlog_debug("%s: socket %d bad nexthop_num=%d", __func__,
197 zlookup->sock, nexthop_num);
198 return -6;
199 }
200
201 for (i = 0; i < nexthop_num; ++i) {
202 enum nexthop_types_t nexthop_type;
203 struct pim_neighbor *nbr;
204 struct prefix p;
205
206 nexthop_type = stream_getc(s);
207 if (num_ifindex >= tab_size) {
208 char addr_str[INET_ADDRSTRLEN];
209 pim_inet4_dump("<addr?>", addr, addr_str,
210 sizeof(addr_str));
211 zlog_warn(
212 "%s: found too many nexthop ifindexes (%d > %d) for address %s(%s)",
213 __PRETTY_FUNCTION__, (num_ifindex + 1),
214 tab_size, addr_str, pim->vrf->name);
215 return num_ifindex;
216 }
217 switch (nexthop_type) {
218 case NEXTHOP_TYPE_IFINDEX:
219 case NEXTHOP_TYPE_IPV4_IFINDEX:
220 case NEXTHOP_TYPE_IPV4:
221 nexthop_tab[num_ifindex].nexthop_addr.family = AF_INET;
222 if (nexthop_type == NEXTHOP_TYPE_IPV4_IFINDEX
223 || nexthop_type == NEXTHOP_TYPE_IPV4) {
224 nexthop_tab[num_ifindex]
225 .nexthop_addr.u.prefix4.s_addr =
226 stream_get_ipv4(s);
227 } else {
228 nexthop_tab[num_ifindex]
229 .nexthop_addr.u.prefix4.s_addr =
230 PIM_NET_INADDR_ANY;
231 }
232 nexthop_tab[num_ifindex].ifindex = stream_getl(s);
233 nexthop_tab[num_ifindex].protocol_distance = distance;
234 nexthop_tab[num_ifindex].route_metric = metric;
235 ++num_ifindex;
236 break;
237 case NEXTHOP_TYPE_IPV6_IFINDEX:
238 nexthop_tab[num_ifindex].nexthop_addr.family = AF_INET6;
239 stream_get(&nexthop_tab[num_ifindex]
240 .nexthop_addr.u.prefix6,
241 s, sizeof(struct in6_addr));
242 nexthop_tab[num_ifindex].ifindex = stream_getl(s);
243
244 p.family = AF_INET6;
245 p.prefixlen = IPV6_MAX_PREFIXLEN;
246 memcpy(&p.u.prefix6,
247 &nexthop_tab[num_ifindex].nexthop_addr.u.prefix6,
248 sizeof(struct in6_addr));
249
250 /*
251 * If we are sending v6 secondary assume we receive v6
252 * secondary
253 */
254 if (pim->send_v6_secondary)
255 nbr = pim_neighbor_find_by_secondary(
256 if_lookup_by_index(
257 nexthop_tab[num_ifindex]
258 .ifindex,
259 vrf_id),
260 &p);
261 else
262 nbr = pim_neighbor_find_if(if_lookup_by_index(
263 nexthop_tab[num_ifindex].ifindex,
264 vrf_id));
265 if (nbr) {
266 nexthop_tab[num_ifindex].nexthop_addr.family =
267 AF_INET;
268 nexthop_tab[num_ifindex]
269 .nexthop_addr.u.prefix4 =
270 nbr->source_addr;
271 }
272 ++num_ifindex;
273 break;
274 default:
275 /* do nothing */
276 {
277 char addr_str[INET_ADDRSTRLEN];
278 pim_inet4_dump("<addr?>", addr, addr_str,
279 sizeof(addr_str));
280 zlog_warn(
281 "%s: found non-ifindex nexthop type=%d for address %s(%s)",
282 __PRETTY_FUNCTION__, nexthop_type,
283 addr_str, pim->vrf->name);
284 }
285 break;
286 }
287 }
288
289 return num_ifindex;
290 }
291
292 static int zclient_lookup_nexthop_once(struct pim_instance *pim,
293 struct pim_zlookup_nexthop nexthop_tab[],
294 const int tab_size, struct in_addr addr)
295 {
296 struct stream *s;
297 int ret;
298
299 if (PIM_DEBUG_PIM_NHT_DETAIL) {
300 char addr_str[INET_ADDRSTRLEN];
301 pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str));
302 zlog_debug("%s: addr=%s(%s)", __PRETTY_FUNCTION__, addr_str,
303 pim->vrf->name);
304 }
305
306 /* Check socket. */
307 if (zlookup->sock < 0) {
308 zlog_err("%s: zclient lookup socket is not connected",
309 __PRETTY_FUNCTION__);
310 zclient_lookup_failed(zlookup);
311 return -1;
312 }
313
314 if (pim->vrf->vrf_id == VRF_UNKNOWN) {
315 zlog_err(
316 "%s: VRF: %s does not fully exist yet, delaying lookup",
317 __PRETTY_FUNCTION__, pim->vrf->name);
318 return -1;
319 }
320
321 s = zlookup->obuf;
322 stream_reset(s);
323 zclient_create_header(s, ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB, pim->vrf_id);
324 stream_put_in_addr(s, &addr);
325 stream_putw_at(s, 0, stream_get_endp(s));
326
327 ret = writen(zlookup->sock, s->data, stream_get_endp(s));
328 if (ret < 0) {
329 zlog_err(
330 "%s: writen() failure: %d writing to zclient lookup socket",
331 __PRETTY_FUNCTION__, errno);
332 zclient_lookup_failed(zlookup);
333 return -2;
334 }
335 if (ret == 0) {
336 zlog_err("%s: connection closed on zclient lookup socket",
337 __PRETTY_FUNCTION__);
338 zclient_lookup_failed(zlookup);
339 return -3;
340 }
341
342 return zclient_read_nexthop(pim, zlookup, nexthop_tab, tab_size, addr);
343 }
344
345 int zclient_lookup_nexthop(struct pim_instance *pim,
346 struct pim_zlookup_nexthop nexthop_tab[],
347 const int tab_size, struct in_addr addr,
348 int max_lookup)
349 {
350 int lookup;
351 uint32_t route_metric = 0xFFFFFFFF;
352 uint8_t protocol_distance = 0xFF;
353
354 qpim_nexthop_lookups++;
355
356 for (lookup = 0; lookup < max_lookup; ++lookup) {
357 int num_ifindex;
358 int first_ifindex;
359 struct prefix nexthop_addr;
360
361 num_ifindex = zclient_lookup_nexthop_once(pim, nexthop_tab,
362 tab_size, addr);
363 if (num_ifindex < 1) {
364 if (PIM_DEBUG_PIM_NHT) {
365 char addr_str[INET_ADDRSTRLEN];
366 pim_inet4_dump("<addr?>", addr, addr_str,
367 sizeof(addr_str));
368 zlog_debug(
369 "%s: lookup=%d/%d: could not find nexthop ifindex for address %s(%s)",
370 __PRETTY_FUNCTION__, lookup, max_lookup,
371 addr_str, pim->vrf->name);
372 }
373 return -1;
374 }
375
376 if (lookup < 1) {
377 /* this is the non-recursive lookup - save original
378 * metric/distance */
379 route_metric = nexthop_tab[0].route_metric;
380 protocol_distance = nexthop_tab[0].protocol_distance;
381 }
382
383 /*
384 * FIXME: Non-recursive nexthop ensured only for first ifindex.
385 * However, recursive route lookup should really be fixed in
386 * zebra daemon.
387 * See also TODO T24.
388 *
389 * So Zebra for NEXTHOP_TYPE_IPV4 returns the ifindex now since
390 * it was being stored. This Doesn't solve all cases of
391 * recursive lookup but for the most common types it does.
392 */
393 first_ifindex = nexthop_tab[0].ifindex;
394 nexthop_addr = nexthop_tab[0].nexthop_addr;
395 if (first_ifindex > 0) {
396 /* found: first ifindex is non-recursive nexthop */
397
398 if (lookup > 0) {
399 /* Report non-recursive success after first
400 * lookup */
401 if (PIM_DEBUG_PIM_NHT) {
402 char addr_str[INET_ADDRSTRLEN];
403 pim_inet4_dump("<addr?>", addr,
404 addr_str,
405 sizeof(addr_str));
406 zlog_debug(
407 "%s: lookup=%d/%d: found non-recursive ifindex=%d for address %s(%s) dist=%d met=%d",
408 __PRETTY_FUNCTION__, lookup,
409 max_lookup, first_ifindex,
410 addr_str, pim->vrf->name,
411 nexthop_tab[0]
412 .protocol_distance,
413 nexthop_tab[0].route_metric);
414 }
415
416 /* use last address as nexthop address */
417 nexthop_tab[0].nexthop_addr.u.prefix4 = addr;
418
419 /* report original route metric/distance */
420 nexthop_tab[0].route_metric = route_metric;
421 nexthop_tab[0].protocol_distance =
422 protocol_distance;
423 }
424
425 return num_ifindex;
426 }
427
428 if (PIM_DEBUG_PIM_NHT) {
429 char addr_str[INET_ADDRSTRLEN];
430 char nexthop_str[PREFIX_STRLEN];
431 pim_inet4_dump("<addr?>", addr, addr_str,
432 sizeof(addr_str));
433 pim_addr_dump("<nexthop?>", &nexthop_addr, nexthop_str,
434 sizeof(nexthop_str));
435 zlog_debug(
436 "%s: lookup=%d/%d: zebra returned recursive nexthop %s for address %s(%s) dist=%d met=%d",
437 __PRETTY_FUNCTION__, lookup, max_lookup,
438 nexthop_str, addr_str, pim->vrf->name,
439 nexthop_tab[0].protocol_distance,
440 nexthop_tab[0].route_metric);
441 }
442
443 addr =
444 nexthop_addr.u.prefix4; /* use nexthop addr for
445 recursive lookup */
446
447 } /* for (max_lookup) */
448
449 if (PIM_DEBUG_PIM_NHT) {
450 char addr_str[INET_ADDRSTRLEN];
451 pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str));
452 zlog_warn(
453 "%s: lookup=%d/%d: failure searching recursive nexthop ifindex for address %s(%s)",
454 __PRETTY_FUNCTION__, lookup, max_lookup, addr_str,
455 pim->vrf->name);
456 }
457
458 return -2;
459 }
460
461 void pim_zlookup_show_ip_multicast(struct vty *vty)
462 {
463 vty_out(vty, "Zclient lookup socket: ");
464 if (zlookup) {
465 vty_out(vty, "%d failures=%d\n", zlookup->sock, zlookup->fail);
466 } else {
467 vty_out(vty, "<null zclient>\n");
468 }
469 }
470
471 int pim_zlookup_sg_statistics(struct channel_oil *c_oil)
472 {
473 struct stream *s = zlookup->obuf;
474 uint16_t command = 0;
475 unsigned long long lastused;
476 struct prefix_sg sg;
477 int count = 0;
478 int ret;
479 struct interface *ifp =
480 pim_if_find_by_vif_index(c_oil->pim, c_oil->oil.mfcc_parent);
481
482 if (PIM_DEBUG_ZEBRA) {
483 struct prefix_sg more;
484
485 more.src = c_oil->oil.mfcc_origin;
486 more.grp = c_oil->oil.mfcc_mcastgrp;
487 zlog_debug(
488 "Sending Request for New Channel Oil Information(%s) VIIF %d(%s)",
489 pim_str_sg_dump(&more), c_oil->oil.mfcc_parent,
490 c_oil->pim->vrf->name);
491 }
492
493 if (!ifp)
494 return -1;
495
496 stream_reset(s);
497 zclient_create_header(s, ZEBRA_IPMR_ROUTE_STATS, c_oil->pim->vrf_id);
498 stream_put_in_addr(s, &c_oil->oil.mfcc_origin);
499 stream_put_in_addr(s, &c_oil->oil.mfcc_mcastgrp);
500 stream_putl(s, ifp->ifindex);
501 stream_putw_at(s, 0, stream_get_endp(s));
502
503 count = stream_get_endp(s);
504 ret = writen(zlookup->sock, s->data, count);
505 if (ret <= 0) {
506 zlog_err(
507 "%s: writen() failure: %d writing to zclient lookup socket",
508 __PRETTY_FUNCTION__, errno);
509 return -1;
510 }
511
512 s = zlookup->ibuf;
513
514 while (command != ZEBRA_IPMR_ROUTE_STATS) {
515 int err;
516 uint16_t length = 0;
517 vrf_id_t vrf_id;
518 u_char marker;
519 u_char version;
520
521 stream_reset(s);
522 err = zclient_read_header(s, zlookup->sock, &length, &marker,
523 &version, &vrf_id, &command);
524 if (err < 0) {
525 zlog_err("%s: zclient_read_header() failed",
526 __PRETTY_FUNCTION__);
527 zclient_lookup_failed(zlookup);
528 return -1;
529 }
530 }
531
532 sg.src.s_addr = stream_get_ipv4(s);
533 sg.grp.s_addr = stream_get_ipv4(s);
534 if (sg.src.s_addr != c_oil->oil.mfcc_origin.s_addr
535 || sg.grp.s_addr != c_oil->oil.mfcc_mcastgrp.s_addr) {
536 if (PIM_DEBUG_ZEBRA) {
537 struct prefix_sg more;
538
539 more.src = c_oil->oil.mfcc_origin;
540 more.grp = c_oil->oil.mfcc_mcastgrp;
541 zlog_err(
542 "%s: Received wrong %s(%s) information requested",
543 __PRETTY_FUNCTION__, pim_str_sg_dump(&more),
544 c_oil->pim->vrf->name);
545 }
546 zclient_lookup_failed(zlookup);
547 return -3;
548 }
549
550 stream_get(&lastused, s, sizeof(lastused));
551 stream_getl(s);
552
553 c_oil->cc.lastused = lastused;
554
555 return 0;
556 }