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