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