3 * Copyright (C) 2008 Everton da Silva Marques
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.
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.
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
30 #include "lib_errors.h"
33 #include "pim_instance.h"
34 #include "pim_iface.h"
35 #include "pim_neighbor.h"
39 #include "pim_zlookup.h"
42 static struct zclient
*zlookup
= NULL
;
43 struct thread
*zlookup_read
;
45 static void zclient_lookup_sched(struct zclient
*zlookup
, int delay
);
46 static void zclient_lookup_read_pipe(struct thread
*thread
);
48 /* Connect to zebra for nexthop lookup. */
49 static void zclient_lookup_connect(struct thread
*t
)
51 struct zclient
*zlookup
;
53 zlookup
= THREAD_ARG(t
);
55 if (zlookup
->sock
>= 0) {
59 if (zclient_socket_connect(zlookup
) < 0) {
61 zlog_warn("%s: failure connecting zclient socket: failures=%d",
62 __func__
, zlookup
->fail
);
64 zlookup
->fail
= 0; /* reset counter on connection */
67 if (zclient_send_hello(zlookup
) == ZCLIENT_SEND_FAILURE
) {
68 if (close(zlookup
->sock
)) {
69 zlog_warn("%s: closing fd=%d: errno=%d %s", __func__
,
70 zlookup
->sock
, errno
, safe_strerror(errno
));
75 if (zlookup
->sock
< 0) {
76 /* Since last connect failed, retry within 10 secs */
77 zclient_lookup_sched(zlookup
, 10);
81 thread_add_timer(router
->master
, zclient_lookup_read_pipe
, zlookup
, 60,
85 /* Schedule connection with delay. */
86 static void zclient_lookup_sched(struct zclient
*zlookup
, int delay
)
88 thread_add_timer(router
->master
, zclient_lookup_connect
, zlookup
, delay
,
91 zlog_notice("%s: zclient lookup connection scheduled for %d seconds",
95 /* Schedule connection for now. */
96 static void zclient_lookup_sched_now(struct zclient
*zlookup
)
98 thread_add_event(router
->master
, zclient_lookup_connect
, zlookup
, 0,
101 zlog_notice("%s: zclient lookup immediate connection scheduled",
105 /* Schedule reconnection, if needed. */
106 static void zclient_lookup_reconnect(struct zclient
*zlookup
)
108 if (zlookup
->t_connect
) {
112 zclient_lookup_sched_now(zlookup
);
115 static void zclient_lookup_failed(struct zclient
*zlookup
)
117 if (zlookup
->sock
>= 0) {
118 if (close(zlookup
->sock
)) {
119 zlog_warn("%s: closing fd=%d: errno=%d %s", __func__
,
120 zlookup
->sock
, errno
, safe_strerror(errno
));
125 zclient_lookup_reconnect(zlookup
);
128 void zclient_lookup_free(void)
130 THREAD_OFF(zlookup_read
);
131 zclient_stop(zlookup
);
132 zclient_free(zlookup
);
136 void zclient_lookup_new(void)
138 struct zclient_options options
= zclient_options_default
;
139 options
.synchronous
= true;
141 zlookup
= zclient_new(router
->master
, &options
, NULL
, 0);
143 flog_err(EC_LIB_ZAPI_SOCKET
, "%s: zclient_new() failure",
149 zlookup
->t_connect
= NULL
;
150 zlookup
->privs
= &pimd_privs
;
152 zclient_lookup_sched_now(zlookup
);
154 zlog_notice("%s: zclient lookup socket initialized", __func__
);
157 static int zclient_read_nexthop(struct pim_instance
*pim
,
158 struct zclient
*zlookup
,
159 struct pim_zlookup_nexthop nexthop_tab
[],
160 const int tab_size
, pim_addr addr
)
168 uint16_t command
= 0;
175 if (PIM_DEBUG_PIM_NHT_DETAIL
)
176 zlog_debug("%s: addr=%pPAs(%s)", __func__
, &addr
,
181 while (command
!= ZEBRA_NEXTHOP_LOOKUP_MRIB
) {
183 err
= zclient_read_header(s
, zlookup
->sock
, &length
, &marker
,
184 &version
, &vrf_id
, &command
);
186 flog_err(EC_LIB_ZAPI_MISSMATCH
,
187 "%s: zclient_read_header() failed", __func__
);
188 zclient_lookup_failed(zlookup
);
192 if (command
== ZEBRA_ERROR
) {
193 enum zebra_error_types error
;
195 zapi_error_decode(s
, &error
);
196 /* Do nothing with it for now */
201 stream_get_ipaddr(s
, &raddr
);
203 if (raddr
.ipa_type
!= PIM_IPADDR
||
204 pim_addr_cmp(raddr
.ipaddr_pim
, addr
)) {
205 zlog_warn("%s: address mismatch: addr=%pPA(%s) raddr=%pIA",
206 __func__
, &addr
, pim
->vrf
->name
, &raddr
);
210 distance
= stream_getc(s
);
211 metric
= stream_getl(s
);
212 nexthop_num
= stream_getc(s
);
214 if (nexthop_num
< 1 || nexthop_num
> router
->multipath
) {
215 if (PIM_DEBUG_PIM_NHT_DETAIL
)
216 zlog_debug("%s: socket %d bad nexthop_num=%d", __func__
,
217 zlookup
->sock
, nexthop_num
);
221 for (i
= 0; i
< nexthop_num
; ++i
) {
222 vrf_id_t nexthop_vrf_id
;
223 enum nexthop_types_t nexthop_type
;
224 struct in_addr nh_ip4
;
225 struct in6_addr nh_ip6
;
228 nexthop_vrf_id
= stream_getl(s
);
229 nexthop_type
= stream_getc(s
);
230 if (num_ifindex
>= tab_size
) {
232 "%s: found too many nexthop ifindexes (%d > %d) for address %pPAs(%s)",
233 __func__
, (num_ifindex
+ 1), tab_size
, &addr
,
237 nexthop_tab
[num_ifindex
].protocol_distance
= distance
;
238 nexthop_tab
[num_ifindex
].route_metric
= metric
;
239 nexthop_tab
[num_ifindex
].vrf_id
= nexthop_vrf_id
;
240 switch (nexthop_type
) {
241 case NEXTHOP_TYPE_IFINDEX
:
242 nexthop_tab
[num_ifindex
].ifindex
= stream_getl(s
);
244 * Connected route (i.e. no nexthop), use
245 * address passed in as PIM nexthop. This will
246 * allow us to work in cases where we are
247 * trying to find a route for this box.
249 nexthop_tab
[num_ifindex
].nexthop_addr
= addr
;
252 case NEXTHOP_TYPE_IPV4_IFINDEX
:
253 case NEXTHOP_TYPE_IPV4
:
254 nh_ip4
.s_addr
= stream_get_ipv4(s
);
255 nh_ifi
= stream_getl(s
);
257 nexthop_tab
[num_ifindex
].nexthop_addr
= nh_ip4
;
258 nexthop_tab
[num_ifindex
].ifindex
= nh_ifi
;
262 "cannot use IPv4 nexthop %pI4(%d) for IPv6 %pPA",
263 &nh_ip4
, nh_ifi
, &addr
);
266 case NEXTHOP_TYPE_IPV6_IFINDEX
:
267 stream_get(&nh_ip6
, s
, sizeof(nh_ip6
));
268 nh_ifi
= stream_getl(s
);
271 nexthop_tab
[num_ifindex
].nexthop_addr
= nh_ip6
;
272 nexthop_tab
[num_ifindex
].ifindex
= nh_ifi
;
275 /* RFC 5549 v4-over-v6 nexthop handling */
278 * If we are sending v6 secondary assume we receive v6
281 struct interface
*ifp
= if_lookup_by_index(
288 struct pim_neighbor
*nbr
;
290 if (pim
->send_v6_secondary
) {
294 p
.prefixlen
= IPV6_MAX_BITLEN
;
295 p
.u
.prefix6
= nh_ip6
;
297 nbr
= pim_neighbor_find_by_secondary(ifp
, &p
);
299 nbr
= pim_neighbor_find_if(ifp
);
304 nexthop_tab
[num_ifindex
].nexthop_addr
=
306 nexthop_tab
[num_ifindex
].ifindex
= nh_ifi
;
313 "%s: found non-ifindex nexthop type=%d for address %pPAs(%s)",
314 __func__
, nexthop_type
, &addr
, pim
->vrf
->name
);
322 static int zclient_lookup_nexthop_once(struct pim_instance
*pim
,
323 struct pim_zlookup_nexthop nexthop_tab
[],
324 const int tab_size
, pim_addr addr
)
328 struct ipaddr ipaddr
;
330 if (PIM_DEBUG_PIM_NHT_DETAIL
)
331 zlog_debug("%s: addr=%pPAs(%s)", __func__
, &addr
,
335 if (zlookup
->sock
< 0) {
336 flog_err(EC_LIB_ZAPI_SOCKET
,
337 "%s: zclient lookup socket is not connected",
339 zclient_lookup_failed(zlookup
);
343 if (pim
->vrf
->vrf_id
== VRF_UNKNOWN
) {
345 "%s: VRF: %s does not fully exist yet, delaying lookup",
346 __func__
, pim
->vrf
->name
);
350 ipaddr
.ipa_type
= PIM_IPADDR
;
351 ipaddr
.ipaddr_pim
= addr
;
355 zclient_create_header(s
, ZEBRA_NEXTHOP_LOOKUP_MRIB
, pim
->vrf
->vrf_id
);
356 stream_put_ipaddr(s
, &ipaddr
);
357 stream_putw_at(s
, 0, stream_get_endp(s
));
359 ret
= writen(zlookup
->sock
, s
->data
, stream_get_endp(s
));
363 "%s: writen() failure: %d writing to zclient lookup socket",
365 zclient_lookup_failed(zlookup
);
369 flog_err_sys(EC_LIB_SOCKET
,
370 "%s: connection closed on zclient lookup socket",
372 zclient_lookup_failed(zlookup
);
376 return zclient_read_nexthop(pim
, zlookup
, nexthop_tab
, tab_size
, addr
);
379 void zclient_lookup_read_pipe(struct thread
*thread
)
381 struct zclient
*zlookup
= THREAD_ARG(thread
);
382 struct pim_instance
*pim
= pim_get_pim_instance(VRF_DEFAULT
);
383 struct pim_zlookup_nexthop nexthop_tab
[10];
384 pim_addr l
= PIMADDR_ANY
;
387 if (PIM_DEBUG_PIM_NHT_DETAIL
)
388 zlog_debug("%s: Unable to find pim instance", __func__
);
392 zclient_lookup_nexthop_once(pim
, nexthop_tab
, 10, l
);
393 thread_add_timer(router
->master
, zclient_lookup_read_pipe
, zlookup
, 60,
397 int zclient_lookup_nexthop(struct pim_instance
*pim
,
398 struct pim_zlookup_nexthop nexthop_tab
[],
399 const int tab_size
, pim_addr addr
,
403 uint32_t route_metric
= 0xFFFFFFFF;
404 uint8_t protocol_distance
= 0xFF;
406 pim
->nexthop_lookups
++;
408 for (lookup
= 0; lookup
< max_lookup
; ++lookup
) {
411 pim_addr nexthop_addr
;
413 num_ifindex
= zclient_lookup_nexthop_once(pim
, nexthop_tab
,
415 if (num_ifindex
< 1) {
416 if (PIM_DEBUG_PIM_NHT_DETAIL
)
418 "%s: lookup=%d/%d: could not find nexthop ifindex for address %pPA(%s)",
419 __func__
, lookup
, max_lookup
, &addr
,
425 /* this is the non-recursive lookup - save original
427 route_metric
= nexthop_tab
[0].route_metric
;
428 protocol_distance
= nexthop_tab
[0].protocol_distance
;
432 * FIXME: Non-recursive nexthop ensured only for first ifindex.
433 * However, recursive route lookup should really be fixed in
437 * So Zebra for NEXTHOP_TYPE_IPV4 returns the ifindex now since
438 * it was being stored. This Doesn't solve all cases of
439 * recursive lookup but for the most common types it does.
441 first_ifindex
= nexthop_tab
[0].ifindex
;
442 nexthop_addr
= nexthop_tab
[0].nexthop_addr
;
443 if (first_ifindex
> 0) {
444 /* found: first ifindex is non-recursive nexthop */
447 /* Report non-recursive success after first
449 if (PIM_DEBUG_PIM_NHT
)
451 "%s: lookup=%d/%d: found non-recursive ifindex=%d for address %pPA(%s) dist=%d met=%d",
452 __func__
, lookup
, max_lookup
,
453 first_ifindex
, &addr
,
457 nexthop_tab
[0].route_metric
);
459 /* use last address as nexthop address */
460 nexthop_tab
[0].nexthop_addr
= addr
;
462 /* report original route metric/distance */
463 nexthop_tab
[0].route_metric
= route_metric
;
464 nexthop_tab
[0].protocol_distance
=
471 if (PIM_DEBUG_PIM_NHT
)
473 "%s: lookup=%d/%d: zebra returned recursive nexthop %pPAs for address %pPA(%s) dist=%d met=%d",
474 __func__
, lookup
, max_lookup
, &nexthop_addr
,
475 &addr
, pim
->vrf
->name
,
476 nexthop_tab
[0].protocol_distance
,
477 nexthop_tab
[0].route_metric
);
479 addr
= nexthop_addr
; /* use nexthop
480 addr for recursive lookup */
482 } /* for (max_lookup) */
484 if (PIM_DEBUG_PIM_NHT
)
486 "%s: lookup=%d/%d: failure searching recursive nexthop ifindex for address %pPA(%s)",
487 __func__
, lookup
, max_lookup
, &addr
, pim
->vrf
->name
);
492 void pim_zlookup_show_ip_multicast(struct vty
*vty
)
494 vty_out(vty
, "Zclient lookup socket: ");
496 vty_out(vty
, "%d failures=%d\n", zlookup
->sock
, zlookup
->fail
);
498 vty_out(vty
, "<null zclient>\n");
502 int pim_zlookup_sg_statistics(struct channel_oil
*c_oil
)
504 struct stream
*s
= zlookup
->obuf
;
505 uint16_t command
= 0;
506 unsigned long long lastused
;
510 pim_sgaddr more
= {};
511 struct interface
*ifp
=
512 pim_if_find_by_vif_index(c_oil
->pim
, *oil_parent(c_oil
));
514 if (PIM_DEBUG_ZEBRA
) {
515 more
.src
= *oil_origin(c_oil
);
516 more
.grp
= *oil_mcastgrp(c_oil
);
518 "Sending Request for New Channel Oil Information%pSG VIIF %d(%s)",
519 &more
, *oil_parent(c_oil
), c_oil
->pim
->vrf
->name
);
526 zclient_create_header(s
, ZEBRA_IPMR_ROUTE_STATS
,
527 c_oil
->pim
->vrf
->vrf_id
);
528 stream_putl(s
, PIM_AF
);
529 stream_write(s
, oil_origin(c_oil
), sizeof(pim_addr
));
530 stream_write(s
, oil_mcastgrp(c_oil
), sizeof(pim_addr
));
531 stream_putl(s
, ifp
->ifindex
);
532 stream_putw_at(s
, 0, stream_get_endp(s
));
534 count
= stream_get_endp(s
);
535 ret
= writen(zlookup
->sock
, s
->data
, count
);
539 "%s: writen() failure: %d writing to zclient lookup socket",
546 while (command
!= ZEBRA_IPMR_ROUTE_STATS
) {
554 err
= zclient_read_header(s
, zlookup
->sock
, &length
, &marker
,
555 &version
, &vrf_id
, &command
);
557 flog_err(EC_LIB_ZAPI_MISSMATCH
,
558 "%s: zclient_read_header() failed", __func__
);
559 zclient_lookup_failed(zlookup
);
564 stream_get(&sg
.src
, s
, sizeof(pim_addr
));
565 stream_get(&sg
.grp
, s
, sizeof(pim_addr
));
567 more
.src
= *oil_origin(c_oil
);
568 more
.grp
= *oil_mcastgrp(c_oil
);
569 if (pim_sgaddr_cmp(sg
, more
)) {
572 EC_LIB_ZAPI_MISSMATCH
,
573 "%s: Received wrong %pSG(%s) information requested",
574 __func__
, &more
, c_oil
->pim
->vrf
->name
);
575 zclient_lookup_failed(zlookup
);
579 stream_get(&lastused
, s
, sizeof(lastused
));
580 /* signed success value from netlink_talk; currently unused */
581 (void)stream_getl(s
);
583 c_oil
->cc
.lastused
= lastused
;