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