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