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