]> git.proxmox.com Git - mirror_frr.git/blob - pimd/pim_zlookup.c
bgpd: fix the IGP metric for best path selection on VPN import
[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_instance.h"
34 #include "pim_iface.h"
35 #include "pim_neighbor.h"
36 #include "pim_pim.h"
37 #include "pim_str.h"
38 #include "pim_oil.h"
39 #include "pim_zlookup.h"
40 #include "pim_addr.h"
41
42 static struct zclient *zlookup = NULL;
43 struct thread *zlookup_read;
44
45 static void zclient_lookup_sched(struct zclient *zlookup, int delay);
46 static void zclient_lookup_read_pipe(struct thread *thread);
47
48 /* Connect to zebra for nexthop lookup. */
49 static void zclient_lookup_connect(struct thread *t)
50 {
51 struct zclient *zlookup;
52
53 zlookup = THREAD_ARG(t);
54
55 if (zlookup->sock >= 0) {
56 return;
57 }
58
59 if (zclient_socket_connect(zlookup) < 0) {
60 ++zlookup->fail;
61 zlog_warn("%s: failure connecting zclient socket: failures=%d",
62 __func__, zlookup->fail);
63 } else {
64 zlookup->fail = 0; /* reset counter on connection */
65 }
66
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));
71 }
72 zlookup->sock = -1;
73 }
74
75 if (zlookup->sock < 0) {
76 /* Since last connect failed, retry within 10 secs */
77 zclient_lookup_sched(zlookup, 10);
78 return;
79 }
80
81 thread_add_timer(router->master, zclient_lookup_read_pipe, zlookup, 60,
82 &zlookup_read);
83 }
84
85 /* Schedule connection with delay. */
86 static void zclient_lookup_sched(struct zclient *zlookup, int delay)
87 {
88 thread_add_timer(router->master, zclient_lookup_connect, zlookup, delay,
89 &zlookup->t_connect);
90
91 zlog_notice("%s: zclient lookup connection scheduled for %d seconds",
92 __func__, delay);
93 }
94
95 /* Schedule connection for now. */
96 static void zclient_lookup_sched_now(struct zclient *zlookup)
97 {
98 thread_add_event(router->master, zclient_lookup_connect, zlookup, 0,
99 &zlookup->t_connect);
100
101 zlog_notice("%s: zclient lookup immediate connection scheduled",
102 __func__);
103 }
104
105 /* Schedule reconnection, if needed. */
106 static void zclient_lookup_reconnect(struct zclient *zlookup)
107 {
108 if (zlookup->t_connect) {
109 return;
110 }
111
112 zclient_lookup_sched_now(zlookup);
113 }
114
115 static void zclient_lookup_failed(struct zclient *zlookup)
116 {
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));
121 }
122 zlookup->sock = -1;
123 }
124
125 zclient_lookup_reconnect(zlookup);
126 }
127
128 void zclient_lookup_free(void)
129 {
130 THREAD_OFF(zlookup_read);
131 zclient_stop(zlookup);
132 zclient_free(zlookup);
133 zlookup = NULL;
134 }
135
136 void zclient_lookup_new(void)
137 {
138 struct zclient_options options = zclient_options_default;
139 options.synchronous = true;
140
141 zlookup = zclient_new(router->master, &options, NULL, 0);
142 if (!zlookup) {
143 flog_err(EC_LIB_ZAPI_SOCKET, "%s: zclient_new() failure",
144 __func__);
145 return;
146 }
147
148 zlookup->sock = -1;
149 zlookup->t_connect = NULL;
150 zlookup->privs = &pimd_privs;
151
152 zclient_lookup_sched_now(zlookup);
153
154 zlog_notice("%s: zclient lookup socket initialized", __func__);
155 }
156
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)
161 {
162 int num_ifindex = 0;
163 struct stream *s;
164 uint16_t length;
165 uint8_t marker;
166 uint8_t version;
167 vrf_id_t vrf_id;
168 uint16_t command = 0;
169 struct ipaddr raddr;
170 uint8_t distance;
171 uint32_t metric;
172 int nexthop_num;
173 int i, err;
174
175 if (PIM_DEBUG_PIM_NHT_DETAIL)
176 zlog_debug("%s: addr=%pPAs(%s)", __func__, &addr,
177 pim->vrf->name);
178
179 s = zlookup->ibuf;
180
181 while (command != ZEBRA_NEXTHOP_LOOKUP_MRIB) {
182 stream_reset(s);
183 err = zclient_read_header(s, zlookup->sock, &length, &marker,
184 &version, &vrf_id, &command);
185 if (err < 0) {
186 flog_err(EC_LIB_ZAPI_MISSMATCH,
187 "%s: zclient_read_header() failed", __func__);
188 zclient_lookup_failed(zlookup);
189 return -1;
190 }
191
192 if (command == ZEBRA_ERROR) {
193 enum zebra_error_types error;
194
195 zapi_error_decode(s, &error);
196 /* Do nothing with it for now */
197 return -1;
198 }
199 }
200
201 stream_get_ipaddr(s, &raddr);
202
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);
207 /* warning only */
208 }
209
210 distance = stream_getc(s);
211 metric = stream_getl(s);
212 nexthop_num = stream_getc(s);
213
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);
218 return -6;
219 }
220
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;
226 ifindex_t nh_ifi;
227
228 nexthop_vrf_id = stream_getl(s);
229 nexthop_type = stream_getc(s);
230 if (num_ifindex >= tab_size) {
231 zlog_warn(
232 "%s: found too many nexthop ifindexes (%d > %d) for address %pPAs(%s)",
233 __func__, (num_ifindex + 1), tab_size, &addr,
234 pim->vrf->name);
235 return num_ifindex;
236 }
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);
243 /*
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.
248 */
249 nexthop_tab[num_ifindex].nexthop_addr = addr;
250 ++num_ifindex;
251 break;
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);
256 #if PIM_IPV == 4
257 nexthop_tab[num_ifindex].nexthop_addr = nh_ip4;
258 nexthop_tab[num_ifindex].ifindex = nh_ifi;
259 ++num_ifindex;
260 #else
261 zlog_warn(
262 "cannot use IPv4 nexthop %pI4(%d) for IPv6 %pPA",
263 &nh_ip4, nh_ifi, &addr);
264 #endif
265 break;
266 case NEXTHOP_TYPE_IPV6_IFINDEX:
267 stream_get(&nh_ip6, s, sizeof(nh_ip6));
268 nh_ifi = stream_getl(s);
269
270 #if PIM_IPV == 6
271 nexthop_tab[num_ifindex].nexthop_addr = nh_ip6;
272 nexthop_tab[num_ifindex].ifindex = nh_ifi;
273 ++num_ifindex;
274 #else
275 /* RFC 5549 v4-over-v6 nexthop handling */
276
277 /*
278 * If we are sending v6 secondary assume we receive v6
279 * secondary
280 */
281 struct interface *ifp = if_lookup_by_index(
282 nh_ifi,
283 nexthop_vrf_id);
284
285 if (!ifp)
286 break;
287
288 struct pim_neighbor *nbr;
289
290 if (pim->send_v6_secondary) {
291 struct prefix p;
292
293 p.family = AF_INET6;
294 p.prefixlen = IPV6_MAX_BITLEN;
295 p.u.prefix6 = nh_ip6;
296
297 nbr = pim_neighbor_find_by_secondary(ifp, &p);
298 } else
299 nbr = pim_neighbor_find_if(ifp);
300
301 if (!nbr)
302 break;
303
304 nexthop_tab[num_ifindex].nexthop_addr =
305 nbr->source_addr;
306 nexthop_tab[num_ifindex].ifindex = nh_ifi;
307 ++num_ifindex;
308 #endif
309 break;
310 default:
311 /* do nothing */
312 zlog_warn(
313 "%s: found non-ifindex nexthop type=%d for address %pPAs(%s)",
314 __func__, nexthop_type, &addr, pim->vrf->name);
315 break;
316 }
317 }
318
319 return num_ifindex;
320 }
321
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)
325 {
326 struct stream *s;
327 int ret;
328 struct ipaddr ipaddr;
329
330 if (PIM_DEBUG_PIM_NHT_DETAIL)
331 zlog_debug("%s: addr=%pPAs(%s)", __func__, &addr,
332 pim->vrf->name);
333
334 /* Check socket. */
335 if (zlookup->sock < 0) {
336 flog_err(EC_LIB_ZAPI_SOCKET,
337 "%s: zclient lookup socket is not connected",
338 __func__);
339 zclient_lookup_failed(zlookup);
340 return -1;
341 }
342
343 if (pim->vrf->vrf_id == VRF_UNKNOWN) {
344 zlog_notice(
345 "%s: VRF: %s does not fully exist yet, delaying lookup",
346 __func__, pim->vrf->name);
347 return -1;
348 }
349
350 ipaddr.ipa_type = PIM_IPADDR;
351 ipaddr.ipaddr_pim = addr;
352
353 s = zlookup->obuf;
354 stream_reset(s);
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));
358
359 ret = writen(zlookup->sock, s->data, stream_get_endp(s));
360 if (ret < 0) {
361 flog_err(
362 EC_LIB_SOCKET,
363 "%s: writen() failure: %d writing to zclient lookup socket",
364 __func__, errno);
365 zclient_lookup_failed(zlookup);
366 return -2;
367 }
368 if (ret == 0) {
369 flog_err_sys(EC_LIB_SOCKET,
370 "%s: connection closed on zclient lookup socket",
371 __func__);
372 zclient_lookup_failed(zlookup);
373 return -3;
374 }
375
376 return zclient_read_nexthop(pim, zlookup, nexthop_tab, tab_size, addr);
377 }
378
379 void zclient_lookup_read_pipe(struct thread *thread)
380 {
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;
385
386 if (!pim) {
387 if (PIM_DEBUG_PIM_NHT_DETAIL)
388 zlog_debug("%s: Unable to find pim instance", __func__);
389 return;
390 }
391
392 zclient_lookup_nexthop_once(pim, nexthop_tab, 10, l);
393 thread_add_timer(router->master, zclient_lookup_read_pipe, zlookup, 60,
394 &zlookup_read);
395 }
396
397 int zclient_lookup_nexthop(struct pim_instance *pim,
398 struct pim_zlookup_nexthop nexthop_tab[],
399 const int tab_size, pim_addr addr,
400 int max_lookup)
401 {
402 int lookup;
403 uint32_t route_metric = 0xFFFFFFFF;
404 uint8_t protocol_distance = 0xFF;
405
406 pim->nexthop_lookups++;
407
408 for (lookup = 0; lookup < max_lookup; ++lookup) {
409 int num_ifindex;
410 int first_ifindex;
411 pim_addr nexthop_addr;
412
413 num_ifindex = zclient_lookup_nexthop_once(pim, nexthop_tab,
414 tab_size, addr);
415 if (num_ifindex < 1) {
416 if (PIM_DEBUG_PIM_NHT_DETAIL)
417 zlog_debug(
418 "%s: lookup=%d/%d: could not find nexthop ifindex for address %pPA(%s)",
419 __func__, lookup, max_lookup, &addr,
420 pim->vrf->name);
421 return -1;
422 }
423
424 if (lookup < 1) {
425 /* this is the non-recursive lookup - save original
426 * metric/distance */
427 route_metric = nexthop_tab[0].route_metric;
428 protocol_distance = nexthop_tab[0].protocol_distance;
429 }
430
431 /*
432 * FIXME: Non-recursive nexthop ensured only for first ifindex.
433 * However, recursive route lookup should really be fixed in
434 * zebra daemon.
435 * See also TODO T24.
436 *
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.
440 */
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 */
445
446 if (lookup > 0) {
447 /* Report non-recursive success after first
448 * lookup */
449 if (PIM_DEBUG_PIM_NHT)
450 zlog_debug(
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,
454 pim->vrf->name,
455 nexthop_tab[0]
456 .protocol_distance,
457 nexthop_tab[0].route_metric);
458
459 /* use last address as nexthop address */
460 nexthop_tab[0].nexthop_addr = addr;
461
462 /* report original route metric/distance */
463 nexthop_tab[0].route_metric = route_metric;
464 nexthop_tab[0].protocol_distance =
465 protocol_distance;
466 }
467
468 return num_ifindex;
469 }
470
471 if (PIM_DEBUG_PIM_NHT)
472 zlog_debug(
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);
478
479 addr = nexthop_addr; /* use nexthop
480 addr for recursive lookup */
481
482 } /* for (max_lookup) */
483
484 if (PIM_DEBUG_PIM_NHT)
485 zlog_warn(
486 "%s: lookup=%d/%d: failure searching recursive nexthop ifindex for address %pPA(%s)",
487 __func__, lookup, max_lookup, &addr, pim->vrf->name);
488
489 return -2;
490 }
491
492 void pim_zlookup_show_ip_multicast(struct vty *vty)
493 {
494 vty_out(vty, "Zclient lookup socket: ");
495 if (zlookup) {
496 vty_out(vty, "%d failures=%d\n", zlookup->sock, zlookup->fail);
497 } else {
498 vty_out(vty, "<null zclient>\n");
499 }
500 }
501
502 int pim_zlookup_sg_statistics(struct channel_oil *c_oil)
503 {
504 struct stream *s = zlookup->obuf;
505 uint16_t command = 0;
506 unsigned long long lastused;
507 pim_sgaddr sg;
508 int count = 0;
509 int ret;
510 pim_sgaddr more = {};
511 struct interface *ifp =
512 pim_if_find_by_vif_index(c_oil->pim, *oil_parent(c_oil));
513
514 if (PIM_DEBUG_ZEBRA) {
515 more.src = *oil_origin(c_oil);
516 more.grp = *oil_mcastgrp(c_oil);
517 zlog_debug(
518 "Sending Request for New Channel Oil Information%pSG VIIF %d(%s)",
519 &more, *oil_parent(c_oil), c_oil->pim->vrf->name);
520 }
521
522 if (!ifp)
523 return -1;
524
525 stream_reset(s);
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));
533
534 count = stream_get_endp(s);
535 ret = writen(zlookup->sock, s->data, count);
536 if (ret <= 0) {
537 flog_err(
538 EC_LIB_SOCKET,
539 "%s: writen() failure: %d writing to zclient lookup socket",
540 __func__, errno);
541 return -1;
542 }
543
544 s = zlookup->ibuf;
545
546 while (command != ZEBRA_IPMR_ROUTE_STATS) {
547 int err;
548 uint16_t length = 0;
549 vrf_id_t vrf_id;
550 uint8_t marker;
551 uint8_t version;
552
553 stream_reset(s);
554 err = zclient_read_header(s, zlookup->sock, &length, &marker,
555 &version, &vrf_id, &command);
556 if (err < 0) {
557 flog_err(EC_LIB_ZAPI_MISSMATCH,
558 "%s: zclient_read_header() failed", __func__);
559 zclient_lookup_failed(zlookup);
560 return -1;
561 }
562 }
563
564 stream_get(&sg.src, s, sizeof(pim_addr));
565 stream_get(&sg.grp, s, sizeof(pim_addr));
566
567 more.src = *oil_origin(c_oil);
568 more.grp = *oil_mcastgrp(c_oil);
569 if (pim_sgaddr_cmp(sg, more)) {
570 if (PIM_DEBUG_ZEBRA)
571 flog_err(
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);
576 return -3;
577 }
578
579 stream_get(&lastused, s, sizeof(lastused));
580 /* signed success value from netlink_talk; currently unused */
581 (void)stream_getl(s);
582
583 c_oil->cc.lastused = lastused;
584
585 return 0;
586 }