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