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