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