blob: ed47e6738471f2cab7eba787bb865ec408973052 [file] [log] [blame]
Everton Marques871dbcf2009-08-11 15:43:05 -03001/*
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 $QuaggaId: $Format:%an, %ai, %h$ $
21*/
22
23#include <zebra.h>
24#include "zebra/rib.h"
25
26#include "log.h"
27#include "prefix.h"
28#include "zclient.h"
29#include "stream.h"
30#include "network.h"
31#include "thread.h"
32
33#include "pimd.h"
34#include "pim_pim.h"
35#include "pim_str.h"
36#include "pim_zlookup.h"
37
38extern int zclient_debug;
39
40static void zclient_lookup_sched(struct zclient *zlookup, int delay);
41
42/* Connect to zebra for nexthop lookup. */
43static int zclient_lookup_connect(struct thread *t)
44{
45 struct zclient *zlookup;
46
47 zlookup = THREAD_ARG(t);
48 zlookup->t_connect = NULL;
49
50 if (zlookup->sock >= 0) {
51 return 0;
52 }
53
Everton Marquesc77f01b2014-02-13 14:54:43 -020054#ifdef PIM_ZCLIENT_DEBUG
55
Everton Marques871dbcf2009-08-11 15:43:05 -030056#ifdef HAVE_TCP_ZEBRA
57 zlog_debug("%s: FIXME blocking connect: zclient_socket()",
58 __PRETTY_FUNCTION__);
59 zlookup->sock = zclient_socket();
60 if (zlookup->sock < 0) {
61 zlog_warn("%s: failure connecting TCP socket %s,%d",
62 __PRETTY_FUNCTION__, "127.0.0.1", ZEBRA_PORT);
63 }
64 else if (zclient_debug) {
65 zlog_notice("%s: connected TCP socket %s,%d",
66 __PRETTY_FUNCTION__, "127.0.0.1", ZEBRA_PORT);
67 }
68#else
Everton Marques1f298942014-08-21 15:47:28 -030069 {
70 const char *const path = zclient_serv_path_get();
71 zlog_debug("%s: FIXME blocking connect: zclient_socket_un()",
72 __PRETTY_FUNCTION__);
73 zlookup->sock = zclient_socket_un(path);
74 if (zlookup->sock < 0) {
75 zlog_warn("%s: failure connecting UNIX socket %s",
76 __PRETTY_FUNCTION__, path);
77 }
78 else if (zclient_debug) {
79 zlog_notice("%s: connected UNIX socket %s",
80 __PRETTY_FUNCTION__, path);
81 }
Everton Marques871dbcf2009-08-11 15:43:05 -030082 }
83#endif /* HAVE_TCP_ZEBRA */
84
Everton Marquesc77f01b2014-02-13 14:54:43 -020085#else
86
87 zlog_debug("%s: FIXME blocking connect: zclient_socket_connect()",
88 __PRETTY_FUNCTION__);
89 if (zclient_socket_connect(zlookup) < 0) {
90 zlog_warn("%s: failure connecting zclient socket",
91 __PRETTY_FUNCTION__);
92 }
93
94#endif /* PIM_ZCLIENT_DEBUG */
95
Everton Marques871dbcf2009-08-11 15:43:05 -030096 zassert(!zlookup->t_connect);
97 if (zlookup->sock < 0) {
98 /* Since last connect failed, retry within 10 secs */
99 zclient_lookup_sched(zlookup, 10);
100 return -1;
101 }
102
103 return 0;
104}
105
106/* Schedule connection with delay. */
107static void zclient_lookup_sched(struct zclient *zlookup, int delay)
108{
109 zassert(!zlookup->t_connect);
110
111 THREAD_TIMER_ON(master, zlookup->t_connect,
112 zclient_lookup_connect,
113 zlookup, delay);
114
115 zlog_notice("%s: zclient lookup connection scheduled for %d seconds",
116 __PRETTY_FUNCTION__, delay);
117}
118
119/* Schedule connection for now. */
120static void zclient_lookup_sched_now(struct zclient *zlookup)
121{
122 zassert(!zlookup->t_connect);
123
124 zlookup->t_connect = thread_add_event(master, zclient_lookup_connect,
125 zlookup, 0);
126
127 zlog_notice("%s: zclient lookup immediate connection scheduled",
128 __PRETTY_FUNCTION__);
129}
130
131/* Schedule reconnection, if needed. */
132static void zclient_lookup_reconnect(struct zclient *zlookup)
133{
134 if (zlookup->t_connect) {
135 return;
136 }
137
138 zclient_lookup_sched_now(zlookup);
139}
140
141struct zclient *zclient_lookup_new()
142{
143 struct zclient *zlookup;
144
145 zlookup = zclient_new();
146 if (!zlookup) {
147 zlog_err("%s: zclient_new() failure",
148 __PRETTY_FUNCTION__);
149 return 0;
150 }
151
152 zlookup->sock = -1;
153 zlookup->ibuf = stream_new(ZEBRA_MAX_PACKET_SIZ);
154 zlookup->obuf = stream_new(ZEBRA_MAX_PACKET_SIZ);
155 zlookup->t_connect = 0;
156
157 zclient_lookup_sched_now(zlookup);
158
159 zlog_notice("%s: zclient lookup socket initialized",
160 __PRETTY_FUNCTION__);
161
162 return zlookup;
163}
164
165static int zclient_read_nexthop(struct zclient *zlookup,
166 struct pim_zlookup_nexthop nexthop_tab[],
167 const int tab_size,
168 struct in_addr addr)
169{
170 int num_ifindex = 0;
171 struct stream *s;
172 const uint16_t MIN_LEN = 14; /* getc=1 getc=1 getw=2 getipv4=4 getc=1 getl=4 getc=1 */
173 uint16_t length, len;
174 u_char marker;
175 u_char version;
176 uint16_t command;
177 int nbytes;
178 struct in_addr raddr;
179 uint8_t distance;
180 uint32_t metric;
181 int nexthop_num;
182 int i;
183
184 if (PIM_DEBUG_ZEBRA) {
185 char addr_str[100];
186 pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str));
187 zlog_debug("%s: addr=%s",
188 __PRETTY_FUNCTION__,
189 addr_str);
190 }
191
192 s = zlookup->ibuf;
193 stream_reset(s);
194
195 nbytes = stream_read(s, zlookup->sock, 2);
196 if (nbytes < 2) {
197 zlog_err("%s %s: failure reading zclient lookup socket: nbytes=%d",
198 __FILE__, __PRETTY_FUNCTION__, nbytes);
199 close(zlookup->sock);
200 zlookup->sock = -1;
201 zclient_lookup_reconnect(zlookup);
202 return -1;
203 }
204 length = stream_getw(s);
205
206 len = length - 2;
207
208 if (len < MIN_LEN) {
209 zlog_err("%s %s: failure reading zclient lookup socket: len=%d < MIN_LEN=%d",
210 __FILE__, __PRETTY_FUNCTION__, len, MIN_LEN);
211 close(zlookup->sock);
212 zlookup->sock = -1;
213 zclient_lookup_reconnect(zlookup);
214 return -2;
215 }
216
217 nbytes = stream_read(s, zlookup->sock, len);
218 if (nbytes < (length - 2)) {
219 zlog_err("%s %s: failure reading zclient lookup socket: nbytes=%d < len=%d",
220 __FILE__, __PRETTY_FUNCTION__, nbytes, len);
221 close(zlookup->sock);
222 zlookup->sock = -1;
223 zclient_lookup_reconnect(zlookup);
224 return -3;
225 }
226 marker = stream_getc(s);
227 version = stream_getc(s);
228
229 if (version != ZSERV_VERSION || marker != ZEBRA_HEADER_MARKER) {
230 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
231 __func__, zlookup->sock, marker, version);
232 return -4;
233 }
234
235 command = stream_getw(s);
Everton Marques04c833a2014-09-22 19:35:24 -0300236 if (command != ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB) {
Everton Marques871dbcf2009-08-11 15:43:05 -0300237 zlog_err("%s: socket %d command mismatch: %d",
238 __func__, zlookup->sock, command);
239 return -5;
240 }
241
242 raddr.s_addr = stream_get_ipv4(s);
243
244 if (raddr.s_addr != addr.s_addr) {
245 char addr_str[100];
246 char raddr_str[100];
247 pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str));
248 pim_inet4_dump("<raddr?>", raddr, raddr_str, sizeof(raddr_str));
249 zlog_warn("%s: address mismatch: addr=%s raddr=%s",
250 __PRETTY_FUNCTION__,
251 addr_str, raddr_str);
252 /* warning only */
253 }
254
255 distance = stream_getc(s);
256 metric = stream_getl(s);
257 nexthop_num = stream_getc(s);
258
259 if (nexthop_num < 1) {
260 zlog_err("%s: socket %d bad nexthop_num=%d",
261 __func__, zlookup->sock, nexthop_num);
262 return -6;
263 }
264
265 len -= MIN_LEN;
266
267 for (i = 0; i < nexthop_num; ++i) {
268 enum nexthop_types_t nexthop_type;
269
270 if (len < 1) {
271 zlog_err("%s: socket %d empty input expecting nexthop_type: len=%d",
272 __func__, zlookup->sock, len);
273 return -7;
274 }
275
276 nexthop_type = stream_getc(s);
277 --len;
278
279 switch (nexthop_type) {
280 case ZEBRA_NEXTHOP_IFINDEX:
281 case ZEBRA_NEXTHOP_IFNAME:
282 case ZEBRA_NEXTHOP_IPV4_IFINDEX:
283 if (num_ifindex >= tab_size) {
284 char addr_str[100];
285 pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str));
286 zlog_warn("%s %s: found too many nexthop ifindexes (%d > %d) for address %s",
287 __FILE__, __PRETTY_FUNCTION__,
288 (num_ifindex + 1), tab_size, addr_str);
289 return num_ifindex;
290 }
291 if (nexthop_type == ZEBRA_NEXTHOP_IPV4_IFINDEX) {
292 if (len < 4) {
293 zlog_err("%s: socket %d short input expecting nexthop IPv4-addr: len=%d",
294 __func__, zlookup->sock, len);
295 return -8;
296 }
297 nexthop_tab[num_ifindex].nexthop_addr.s_addr = stream_get_ipv4(s);
298 len -= 4;
299 }
300 else {
301 nexthop_tab[num_ifindex].nexthop_addr.s_addr = PIM_NET_INADDR_ANY;
302 }
303 nexthop_tab[num_ifindex].ifindex = stream_getl(s);
304 nexthop_tab[num_ifindex].protocol_distance = distance;
305 nexthop_tab[num_ifindex].route_metric = metric;
306 ++num_ifindex;
307 break;
308 case ZEBRA_NEXTHOP_IPV4:
309 if (num_ifindex >= tab_size) {
310 char addr_str[100];
311 pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str));
312 zlog_warn("%s %s: found too many nexthop ifindexes (%d > %d) for address %s",
313 __FILE__, __PRETTY_FUNCTION__,
314 (num_ifindex + 1), tab_size, addr_str);
315 return num_ifindex;
316 }
317 nexthop_tab[num_ifindex].nexthop_addr.s_addr = stream_get_ipv4(s);
318 len -= 4;
319 nexthop_tab[num_ifindex].ifindex = 0;
320 nexthop_tab[num_ifindex].protocol_distance = distance;
321 nexthop_tab[num_ifindex].route_metric = metric;
322 {
323 char addr_str[100];
324 char nexthop_str[100];
325 pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str));
326 pim_inet4_dump("<nexthop?>", nexthop_tab[num_ifindex].nexthop_addr, nexthop_str, sizeof(nexthop_str));
327 zlog_warn("%s %s: zebra returned recursive nexthop %s for address %s",
328 __FILE__, __PRETTY_FUNCTION__,
329 nexthop_str, addr_str);
330 }
331 ++num_ifindex;
332 break;
333 default:
334 /* do nothing */
335 {
336 char addr_str[100];
337 pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str));
338 zlog_warn("%s %s: found non-ifindex nexthop type=%d for address %s",
339 __FILE__, __PRETTY_FUNCTION__,
340 nexthop_type, addr_str);
341 }
342 break;
343 }
344 }
345
346 return num_ifindex;
347}
348
349static int zclient_lookup_nexthop_once(struct zclient *zlookup,
350 struct pim_zlookup_nexthop nexthop_tab[],
351 const int tab_size,
352 struct in_addr addr)
353{
354 struct stream *s;
355 int ret;
356
357 if (PIM_DEBUG_ZEBRA) {
358 char addr_str[100];
359 pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str));
360 zlog_debug("%s: addr=%s",
361 __PRETTY_FUNCTION__,
362 addr_str);
363 }
364
365 /* Check socket. */
366 if (zlookup->sock < 0) {
367 zlog_err("%s %s: zclient lookup socket is not connected",
368 __FILE__, __PRETTY_FUNCTION__);
369 zclient_lookup_reconnect(zlookup);
370 return -1;
371 }
372
373 s = zlookup->obuf;
374 stream_reset(s);
Everton Marques04c833a2014-09-22 19:35:24 -0300375 zclient_create_header(s, ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB);
Everton Marques871dbcf2009-08-11 15:43:05 -0300376 stream_put_in_addr(s, &addr);
377 stream_putw_at(s, 0, stream_get_endp(s));
378
379 ret = writen(zlookup->sock, s->data, stream_get_endp(s));
380 if (ret < 0) {
381 zlog_err("%s %s: writen() failure writing to zclient lookup socket",
382 __FILE__, __PRETTY_FUNCTION__);
383 close(zlookup->sock);
384 zlookup->sock = -1;
385 zclient_lookup_reconnect(zlookup);
386 return -2;
387 }
388 if (ret == 0) {
389 zlog_err("%s %s: connection closed on zclient lookup socket",
390 __FILE__, __PRETTY_FUNCTION__);
391 close(zlookup->sock);
392 zlookup->sock = -1;
393 zclient_lookup_reconnect(zlookup);
394 return -3;
395 }
396
397 return zclient_read_nexthop(zlookup, nexthop_tab,
398 tab_size, addr);
399}
400
401int zclient_lookup_nexthop(struct zclient *zlookup,
402 struct pim_zlookup_nexthop nexthop_tab[],
403 const int tab_size,
404 struct in_addr addr,
405 int max_lookup)
406{
407 int lookup;
Everton Marquese4f2a2b2014-07-16 12:19:40 -0300408 uint32_t route_metric = 0xFFFFFFFF;
409 uint8_t protocol_distance = 0xFF;
Everton Marques871dbcf2009-08-11 15:43:05 -0300410
411 for (lookup = 0; lookup < max_lookup; ++lookup) {
412 int num_ifindex;
413 int first_ifindex;
414 struct in_addr nexthop_addr;
415
416 num_ifindex = zclient_lookup_nexthop_once(qpim_zclient_lookup, nexthop_tab,
417 PIM_NEXTHOP_IFINDEX_TAB_SIZE, addr);
418 if (num_ifindex < 1) {
419 char addr_str[100];
420 pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str));
421 zlog_warn("%s %s: lookup=%d/%d: could not find nexthop ifindex for address %s",
422 __FILE__, __PRETTY_FUNCTION__,
423 lookup, max_lookup, addr_str);
424 return -1;
425 }
Everton Marquese4f2a2b2014-07-16 12:19:40 -0300426
427 if (lookup < 1) {
428 /* this is the non-recursive lookup - save original metric/distance */
429 route_metric = nexthop_tab[0].route_metric;
430 protocol_distance = nexthop_tab[0].protocol_distance;
431 }
Everton Marques871dbcf2009-08-11 15:43:05 -0300432
433 /*
434 FIXME: Non-recursive nexthop ensured only for first ifindex.
435 However, recursive route lookup should really be fixed in zebra daemon.
436 See also TODO T24.
437 */
438 first_ifindex = nexthop_tab[0].ifindex;
439 nexthop_addr = nexthop_tab[0].nexthop_addr;
440 if (first_ifindex > 0) {
441 /* found: first ifindex is non-recursive nexthop */
442
443 if (lookup > 0) {
444 /* Report non-recursive success after first lookup */
445 char addr_str[100];
446 pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str));
Everton Marquese4f2a2b2014-07-16 12:19:40 -0300447 zlog_info("%s %s: lookup=%d/%d: found non-recursive ifindex=%d for address %s dist=%d met=%d",
Everton Marques871dbcf2009-08-11 15:43:05 -0300448 __FILE__, __PRETTY_FUNCTION__,
Everton Marquese4f2a2b2014-07-16 12:19:40 -0300449 lookup, max_lookup, first_ifindex, addr_str,
450 nexthop_tab[0].protocol_distance,
451 nexthop_tab[0].route_metric);
Everton Marques871dbcf2009-08-11 15:43:05 -0300452
453 /* use last address as nexthop address */
454 nexthop_tab[0].nexthop_addr = addr;
Everton Marquese4f2a2b2014-07-16 12:19:40 -0300455
456 /* report original route metric/distance */
457 nexthop_tab[0].route_metric = route_metric;
458 nexthop_tab[0].protocol_distance = protocol_distance;
Everton Marques871dbcf2009-08-11 15:43:05 -0300459 }
460
461 return num_ifindex;
462 }
463
464 {
465 char addr_str[100];
466 char nexthop_str[100];
467 pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str));
468 pim_inet4_dump("<nexthop?>", nexthop_addr, nexthop_str, sizeof(nexthop_str));
Everton Marquese4f2a2b2014-07-16 12:19:40 -0300469 zlog_warn("%s %s: lookup=%d/%d: zebra returned recursive nexthop %s for address %s dist=%d met=%d",
Everton Marques871dbcf2009-08-11 15:43:05 -0300470 __FILE__, __PRETTY_FUNCTION__,
Everton Marquese4f2a2b2014-07-16 12:19:40 -0300471 lookup, max_lookup, nexthop_str, addr_str,
472 nexthop_tab[0].protocol_distance,
473 nexthop_tab[0].route_metric);
Everton Marques871dbcf2009-08-11 15:43:05 -0300474 }
475
476 addr = nexthop_addr; /* use nexthop addr for recursive lookup */
477
478 } /* for (max_lookup) */
479
480 char addr_str[100];
481 pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str));
482 zlog_warn("%s %s: lookup=%d/%d: failure searching recursive nexthop ifindex for address %s",
483 __FILE__, __PRETTY_FUNCTION__,
484 lookup, max_lookup, addr_str);
485
486 return -2;
487}