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