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