blob: 60003670cf6d83b84bb22293efabfaa644f75c6d [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 if (zclient_socket_connect(zlookup) < 0) {
Everton Marquesa59f21b2014-09-30 17:23:56 -030055 ++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 */
Everton Marquesc77f01b2014-02-13 14:54:43 -020061 }
62
Everton Marques871dbcf2009-08-11 15:43:05 -030063 zassert(!zlookup->t_connect);
64 if (zlookup->sock < 0) {
65 /* Since last connect failed, retry within 10 secs */
66 zclient_lookup_sched(zlookup, 10);
67 return -1;
68 }
69
70 return 0;
71}
72
73/* Schedule connection with delay. */
74static void zclient_lookup_sched(struct zclient *zlookup, int delay)
75{
76 zassert(!zlookup->t_connect);
77
78 THREAD_TIMER_ON(master, zlookup->t_connect,
79 zclient_lookup_connect,
80 zlookup, delay);
81
82 zlog_notice("%s: zclient lookup connection scheduled for %d seconds",
83 __PRETTY_FUNCTION__, delay);
84}
85
86/* Schedule connection for now. */
87static void zclient_lookup_sched_now(struct zclient *zlookup)
88{
89 zassert(!zlookup->t_connect);
90
91 zlookup->t_connect = thread_add_event(master, zclient_lookup_connect,
92 zlookup, 0);
93
94 zlog_notice("%s: zclient lookup immediate connection scheduled",
95 __PRETTY_FUNCTION__);
96}
97
98/* Schedule reconnection, if needed. */
99static void zclient_lookup_reconnect(struct zclient *zlookup)
100{
101 if (zlookup->t_connect) {
102 return;
103 }
104
105 zclient_lookup_sched_now(zlookup);
106}
107
Everton Marquesa59f21b2014-09-30 17:23:56 -0300108static void zclient_lookup_failed(struct zclient *zlookup)
109{
110 if (zlookup->sock >= 0) {
111 if (close(zlookup->sock)) {
112 zlog_warn("%s: closing fd=%d: errno=%d %s", __func__, zlookup->sock,
113 errno, safe_strerror(errno));
114 }
115 zlookup->sock = -1;
116 }
117
118 zclient_lookup_reconnect(zlookup);
119}
120
Everton Marques871dbcf2009-08-11 15:43:05 -0300121struct zclient *zclient_lookup_new()
122{
123 struct zclient *zlookup;
124
125 zlookup = zclient_new();
126 if (!zlookup) {
127 zlog_err("%s: zclient_new() failure",
128 __PRETTY_FUNCTION__);
129 return 0;
130 }
131
132 zlookup->sock = -1;
133 zlookup->ibuf = stream_new(ZEBRA_MAX_PACKET_SIZ);
134 zlookup->obuf = stream_new(ZEBRA_MAX_PACKET_SIZ);
135 zlookup->t_connect = 0;
136
137 zclient_lookup_sched_now(zlookup);
138
139 zlog_notice("%s: zclient lookup socket initialized",
140 __PRETTY_FUNCTION__);
141
142 return zlookup;
143}
144
145static 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 = 14; /* getc=1 getc=1 getw=2 getipv4=4 getc=1 getl=4 getc=1 */
153 uint16_t length, len;
154 u_char marker;
155 u_char version;
156 uint16_t command;
157 int nbytes;
158 struct in_addr raddr;
159 uint8_t distance;
160 uint32_t metric;
161 int nexthop_num;
162 int i;
163
164 if (PIM_DEBUG_ZEBRA) {
165 char addr_str[100];
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 stream_reset(s);
174
175 nbytes = stream_read(s, zlookup->sock, 2);
176 if (nbytes < 2) {
177 zlog_err("%s %s: failure reading zclient lookup socket: nbytes=%d",
178 __FILE__, __PRETTY_FUNCTION__, nbytes);
Everton Marquesa59f21b2014-09-30 17:23:56 -0300179 zclient_lookup_failed(zlookup);
Everton Marques871dbcf2009-08-11 15:43:05 -0300180 return -1;
181 }
182 length = stream_getw(s);
183
184 len = length - 2;
185
186 if (len < MIN_LEN) {
187 zlog_err("%s %s: failure reading zclient lookup socket: len=%d < MIN_LEN=%d",
188 __FILE__, __PRETTY_FUNCTION__, len, MIN_LEN);
Everton Marquesa59f21b2014-09-30 17:23:56 -0300189 zclient_lookup_failed(zlookup);
Everton Marques871dbcf2009-08-11 15:43:05 -0300190 return -2;
191 }
192
193 nbytes = stream_read(s, zlookup->sock, len);
194 if (nbytes < (length - 2)) {
195 zlog_err("%s %s: failure reading zclient lookup socket: nbytes=%d < len=%d",
196 __FILE__, __PRETTY_FUNCTION__, nbytes, len);
Everton Marquesa59f21b2014-09-30 17:23:56 -0300197 zclient_lookup_failed(zlookup);
Everton Marques871dbcf2009-08-11 15:43:05 -0300198 return -3;
199 }
200 marker = stream_getc(s);
201 version = stream_getc(s);
202
203 if (version != ZSERV_VERSION || marker != ZEBRA_HEADER_MARKER) {
204 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
205 __func__, zlookup->sock, marker, version);
206 return -4;
207 }
208
209 command = stream_getw(s);
Everton Marques04c833a2014-09-22 19:35:24 -0300210 if (command != ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB) {
Everton Marques871dbcf2009-08-11 15:43:05 -0300211 zlog_err("%s: socket %d command mismatch: %d",
212 __func__, zlookup->sock, command);
213 return -5;
214 }
215
216 raddr.s_addr = stream_get_ipv4(s);
217
218 if (raddr.s_addr != addr.s_addr) {
219 char addr_str[100];
220 char raddr_str[100];
221 pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str));
222 pim_inet4_dump("<raddr?>", raddr, raddr_str, sizeof(raddr_str));
223 zlog_warn("%s: address mismatch: addr=%s raddr=%s",
224 __PRETTY_FUNCTION__,
225 addr_str, raddr_str);
226 /* warning only */
227 }
228
229 distance = stream_getc(s);
230 metric = stream_getl(s);
231 nexthop_num = stream_getc(s);
232
233 if (nexthop_num < 1) {
234 zlog_err("%s: socket %d bad nexthop_num=%d",
235 __func__, zlookup->sock, nexthop_num);
236 return -6;
237 }
238
239 len -= MIN_LEN;
240
241 for (i = 0; i < nexthop_num; ++i) {
242 enum nexthop_types_t nexthop_type;
243
244 if (len < 1) {
245 zlog_err("%s: socket %d empty input expecting nexthop_type: len=%d",
246 __func__, zlookup->sock, len);
247 return -7;
248 }
249
250 nexthop_type = stream_getc(s);
251 --len;
252
253 switch (nexthop_type) {
254 case ZEBRA_NEXTHOP_IFINDEX:
255 case ZEBRA_NEXTHOP_IFNAME:
256 case ZEBRA_NEXTHOP_IPV4_IFINDEX:
257 if (num_ifindex >= tab_size) {
258 char addr_str[100];
259 pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str));
260 zlog_warn("%s %s: found too many nexthop ifindexes (%d > %d) for address %s",
261 __FILE__, __PRETTY_FUNCTION__,
262 (num_ifindex + 1), tab_size, addr_str);
263 return num_ifindex;
264 }
265 if (nexthop_type == ZEBRA_NEXTHOP_IPV4_IFINDEX) {
266 if (len < 4) {
267 zlog_err("%s: socket %d short input expecting nexthop IPv4-addr: len=%d",
268 __func__, zlookup->sock, len);
269 return -8;
270 }
271 nexthop_tab[num_ifindex].nexthop_addr.s_addr = stream_get_ipv4(s);
272 len -= 4;
273 }
274 else {
275 nexthop_tab[num_ifindex].nexthop_addr.s_addr = PIM_NET_INADDR_ANY;
276 }
277 nexthop_tab[num_ifindex].ifindex = stream_getl(s);
278 nexthop_tab[num_ifindex].protocol_distance = distance;
279 nexthop_tab[num_ifindex].route_metric = metric;
280 ++num_ifindex;
281 break;
282 case ZEBRA_NEXTHOP_IPV4:
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 nexthop_tab[num_ifindex].nexthop_addr.s_addr = stream_get_ipv4(s);
292 len -= 4;
293 nexthop_tab[num_ifindex].ifindex = 0;
294 nexthop_tab[num_ifindex].protocol_distance = distance;
295 nexthop_tab[num_ifindex].route_metric = metric;
296 {
297 char addr_str[100];
298 char nexthop_str[100];
299 pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str));
300 pim_inet4_dump("<nexthop?>", nexthop_tab[num_ifindex].nexthop_addr, nexthop_str, sizeof(nexthop_str));
301 zlog_warn("%s %s: zebra returned recursive nexthop %s for address %s",
302 __FILE__, __PRETTY_FUNCTION__,
303 nexthop_str, addr_str);
304 }
305 ++num_ifindex;
306 break;
307 default:
308 /* do nothing */
309 {
310 char addr_str[100];
311 pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str));
312 zlog_warn("%s %s: found non-ifindex nexthop type=%d for address %s",
313 __FILE__, __PRETTY_FUNCTION__,
314 nexthop_type, addr_str);
315 }
316 break;
317 }
318 }
319
320 return num_ifindex;
321}
322
323static int zclient_lookup_nexthop_once(struct zclient *zlookup,
324 struct pim_zlookup_nexthop nexthop_tab[],
325 const int tab_size,
326 struct in_addr addr)
327{
328 struct stream *s;
329 int ret;
330
331 if (PIM_DEBUG_ZEBRA) {
332 char addr_str[100];
333 pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str));
334 zlog_debug("%s: addr=%s",
335 __PRETTY_FUNCTION__,
336 addr_str);
337 }
338
339 /* Check socket. */
340 if (zlookup->sock < 0) {
341 zlog_err("%s %s: zclient lookup socket is not connected",
342 __FILE__, __PRETTY_FUNCTION__);
Everton Marquesa59f21b2014-09-30 17:23:56 -0300343 zclient_lookup_failed(zlookup);
Everton Marques871dbcf2009-08-11 15:43:05 -0300344 return -1;
345 }
346
347 s = zlookup->obuf;
348 stream_reset(s);
Feng Luc99f3482014-10-16 09:52:36 +0800349 zclient_create_header(s, ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB, VRF_DEFAULT);
Everton Marques871dbcf2009-08-11 15:43:05 -0300350 stream_put_in_addr(s, &addr);
351 stream_putw_at(s, 0, stream_get_endp(s));
352
353 ret = writen(zlookup->sock, s->data, stream_get_endp(s));
354 if (ret < 0) {
355 zlog_err("%s %s: writen() failure writing to zclient lookup socket",
356 __FILE__, __PRETTY_FUNCTION__);
Everton Marquesa59f21b2014-09-30 17:23:56 -0300357 zclient_lookup_failed(zlookup);
Everton Marques871dbcf2009-08-11 15:43:05 -0300358 return -2;
359 }
360 if (ret == 0) {
361 zlog_err("%s %s: connection closed on zclient lookup socket",
362 __FILE__, __PRETTY_FUNCTION__);
Everton Marquesa59f21b2014-09-30 17:23:56 -0300363 zclient_lookup_failed(zlookup);
Everton Marques871dbcf2009-08-11 15:43:05 -0300364 return -3;
365 }
366
367 return zclient_read_nexthop(zlookup, nexthop_tab,
368 tab_size, addr);
369}
370
371int zclient_lookup_nexthop(struct zclient *zlookup,
372 struct pim_zlookup_nexthop nexthop_tab[],
373 const int tab_size,
374 struct in_addr addr,
375 int max_lookup)
376{
377 int lookup;
Everton Marquese4f2a2b2014-07-16 12:19:40 -0300378 uint32_t route_metric = 0xFFFFFFFF;
379 uint8_t protocol_distance = 0xFF;
Everton Marques871dbcf2009-08-11 15:43:05 -0300380
381 for (lookup = 0; lookup < max_lookup; ++lookup) {
382 int num_ifindex;
383 int first_ifindex;
384 struct in_addr nexthop_addr;
385
386 num_ifindex = zclient_lookup_nexthop_once(qpim_zclient_lookup, nexthop_tab,
387 PIM_NEXTHOP_IFINDEX_TAB_SIZE, addr);
388 if (num_ifindex < 1) {
389 char addr_str[100];
390 pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str));
391 zlog_warn("%s %s: lookup=%d/%d: could not find nexthop ifindex for address %s",
392 __FILE__, __PRETTY_FUNCTION__,
393 lookup, max_lookup, addr_str);
394 return -1;
395 }
Everton Marquese4f2a2b2014-07-16 12:19:40 -0300396
397 if (lookup < 1) {
398 /* this is the non-recursive lookup - save original metric/distance */
399 route_metric = nexthop_tab[0].route_metric;
400 protocol_distance = nexthop_tab[0].protocol_distance;
401 }
Everton Marques871dbcf2009-08-11 15:43:05 -0300402
403 /*
404 FIXME: Non-recursive nexthop ensured only for first ifindex.
405 However, recursive route lookup should really be fixed in zebra daemon.
406 See also TODO T24.
407 */
408 first_ifindex = nexthop_tab[0].ifindex;
409 nexthop_addr = nexthop_tab[0].nexthop_addr;
410 if (first_ifindex > 0) {
411 /* found: first ifindex is non-recursive nexthop */
412
413 if (lookup > 0) {
414 /* Report non-recursive success after first lookup */
415 char addr_str[100];
416 pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str));
Everton Marquese4f2a2b2014-07-16 12:19:40 -0300417 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 -0300418 __FILE__, __PRETTY_FUNCTION__,
Everton Marquese4f2a2b2014-07-16 12:19:40 -0300419 lookup, max_lookup, first_ifindex, addr_str,
420 nexthop_tab[0].protocol_distance,
421 nexthop_tab[0].route_metric);
Everton Marques871dbcf2009-08-11 15:43:05 -0300422
423 /* use last address as nexthop address */
424 nexthop_tab[0].nexthop_addr = addr;
Everton Marquese4f2a2b2014-07-16 12:19:40 -0300425
426 /* report original route metric/distance */
427 nexthop_tab[0].route_metric = route_metric;
428 nexthop_tab[0].protocol_distance = protocol_distance;
Everton Marques871dbcf2009-08-11 15:43:05 -0300429 }
430
431 return num_ifindex;
432 }
433
434 {
435 char addr_str[100];
436 char nexthop_str[100];
437 pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str));
438 pim_inet4_dump("<nexthop?>", nexthop_addr, nexthop_str, sizeof(nexthop_str));
Everton Marquese4f2a2b2014-07-16 12:19:40 -0300439 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 -0300440 __FILE__, __PRETTY_FUNCTION__,
Everton Marquese4f2a2b2014-07-16 12:19:40 -0300441 lookup, max_lookup, nexthop_str, addr_str,
442 nexthop_tab[0].protocol_distance,
443 nexthop_tab[0].route_metric);
Everton Marques871dbcf2009-08-11 15:43:05 -0300444 }
445
446 addr = nexthop_addr; /* use nexthop addr for recursive lookup */
447
448 } /* for (max_lookup) */
449
450 char addr_str[100];
451 pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str));
452 zlog_warn("%s %s: lookup=%d/%d: failure searching recursive nexthop ifindex for address %s",
453 __FILE__, __PRETTY_FUNCTION__,
454 lookup, max_lookup, addr_str);
455
456 return -2;
457}