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