]> git.proxmox.com Git - mirror_frr.git/blob - pimd/pim_zlookup.c
pimd: Allow command zclient to find it's data
[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 $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
38 extern int zclient_debug;
39
40 static void zclient_lookup_sched(struct zclient *zlookup, int delay);
41
42 /* Connect to zebra for nexthop lookup. */
43 static 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
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 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. */
74 static 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. */
87 static 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. */
99 static 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
108 static 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
121 struct zclient *zclient_lookup_new()
122 {
123 struct zclient *zlookup;
124
125 zlookup = zclient_new (master);
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
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_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
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 if (version != ZSERV_VERSION || marker != ZEBRA_HEADER_MARKER) {
194 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
195 __func__, zlookup->sock, marker, version);
196 zclient_lookup_failed(zlookup);
197 return -4;
198 }
199 }
200
201 raddr.s_addr = stream_get_ipv4(s);
202
203 if (raddr.s_addr != addr.s_addr) {
204 char addr_str[100];
205 char raddr_str[100];
206 pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str));
207 pim_inet4_dump("<raddr?>", raddr, raddr_str, sizeof(raddr_str));
208 zlog_warn("%s: address mismatch: addr=%s raddr=%s",
209 __PRETTY_FUNCTION__,
210 addr_str, raddr_str);
211 /* warning only */
212 }
213
214 distance = stream_getc(s);
215 metric = stream_getl(s);
216 nexthop_num = stream_getc(s);
217
218 if (nexthop_num < 1) {
219 zlog_err("%s: socket %d bad nexthop_num=%d",
220 __func__, zlookup->sock, nexthop_num);
221 return -6;
222 }
223
224 length -= MIN_LEN;
225
226 for (i = 0; i < nexthop_num; ++i) {
227 enum nexthop_types_t nexthop_type;
228
229 if (length < 1) {
230 zlog_err("%s: socket %d empty input expecting nexthop_type: len=%d",
231 __func__, zlookup->sock, length);
232 return -7;
233 }
234
235 nexthop_type = stream_getc(s);
236 --length;
237
238 switch (nexthop_type) {
239 case ZEBRA_NEXTHOP_IFINDEX:
240 case ZEBRA_NEXTHOP_IPV4_IFINDEX:
241 if (num_ifindex >= tab_size) {
242 char addr_str[100];
243 pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str));
244 zlog_warn("%s %s: found too many nexthop ifindexes (%d > %d) for address %s",
245 __FILE__, __PRETTY_FUNCTION__,
246 (num_ifindex + 1), tab_size, addr_str);
247 return num_ifindex;
248 }
249 if (nexthop_type == ZEBRA_NEXTHOP_IPV4_IFINDEX) {
250 if (length < 4) {
251 zlog_err("%s: socket %d short input expecting nexthop IPv4-addr: len=%d",
252 __func__, zlookup->sock, length);
253 return -8;
254 }
255 nexthop_tab[num_ifindex].nexthop_addr.s_addr = stream_get_ipv4(s);
256 length -= 4;
257 }
258 else {
259 nexthop_tab[num_ifindex].nexthop_addr.s_addr = PIM_NET_INADDR_ANY;
260 }
261 nexthop_tab[num_ifindex].ifindex = stream_getl(s);
262 nexthop_tab[num_ifindex].protocol_distance = distance;
263 nexthop_tab[num_ifindex].route_metric = metric;
264 ++num_ifindex;
265 break;
266 case ZEBRA_NEXTHOP_IPV4:
267 if (num_ifindex >= tab_size) {
268 char addr_str[100];
269 pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str));
270 zlog_warn("%s %s: found too many nexthop ifindexes (%d > %d) for address %s",
271 __FILE__, __PRETTY_FUNCTION__,
272 (num_ifindex + 1), tab_size, addr_str);
273 return num_ifindex;
274 }
275 nexthop_tab[num_ifindex].nexthop_addr.s_addr = stream_get_ipv4(s);
276 length -= 4;
277 nexthop_tab[num_ifindex].ifindex = 0;
278 nexthop_tab[num_ifindex].protocol_distance = distance;
279 nexthop_tab[num_ifindex].route_metric = metric;
280 if (PIM_DEBUG_ZEBRA) {
281 char addr_str[100];
282 char nexthop_str[100];
283 pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str));
284 pim_inet4_dump("<nexthop?>", nexthop_tab[num_ifindex].nexthop_addr, nexthop_str, sizeof(nexthop_str));
285 zlog_debug("%s %s: zebra returned recursive nexthop %s for address %s",
286 __FILE__, __PRETTY_FUNCTION__,
287 nexthop_str, addr_str);
288 }
289 ++num_ifindex;
290 break;
291 default:
292 /* do nothing */
293 {
294 char addr_str[100];
295 pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str));
296 zlog_warn("%s %s: found non-ifindex nexthop type=%d for address %s",
297 __FILE__, __PRETTY_FUNCTION__,
298 nexthop_type, addr_str);
299 }
300 break;
301 }
302 }
303
304 return num_ifindex;
305 }
306
307 static int zclient_lookup_nexthop_once(struct zclient *zlookup,
308 struct pim_zlookup_nexthop nexthop_tab[],
309 const int tab_size,
310 struct in_addr addr)
311 {
312 struct stream *s;
313 int ret;
314
315 if (PIM_DEBUG_ZEBRA) {
316 char addr_str[100];
317 pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str));
318 zlog_debug("%s: addr=%s",
319 __PRETTY_FUNCTION__,
320 addr_str);
321 }
322
323 /* Check socket. */
324 if (zlookup->sock < 0) {
325 zlog_err("%s %s: zclient lookup socket is not connected",
326 __FILE__, __PRETTY_FUNCTION__);
327 zclient_lookup_failed(zlookup);
328 return -1;
329 }
330
331 s = zlookup->obuf;
332 stream_reset(s);
333 zclient_create_header(s, ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB, VRF_DEFAULT);
334 stream_put_in_addr(s, &addr);
335 stream_putw_at(s, 0, stream_get_endp(s));
336
337 ret = writen(zlookup->sock, s->data, stream_get_endp(s));
338 if (ret < 0) {
339 zlog_err("%s %s: writen() failure writing to zclient lookup socket",
340 __FILE__, __PRETTY_FUNCTION__);
341 zclient_lookup_failed(zlookup);
342 return -2;
343 }
344 if (ret == 0) {
345 zlog_err("%s %s: connection closed on zclient lookup socket",
346 __FILE__, __PRETTY_FUNCTION__);
347 zclient_lookup_failed(zlookup);
348 return -3;
349 }
350
351 return zclient_read_nexthop(zlookup, nexthop_tab,
352 tab_size, addr);
353 }
354
355 int zclient_lookup_nexthop(struct zclient *zlookup,
356 struct pim_zlookup_nexthop nexthop_tab[],
357 const int tab_size,
358 struct in_addr addr,
359 int max_lookup)
360 {
361 int lookup;
362 uint32_t route_metric = 0xFFFFFFFF;
363 uint8_t protocol_distance = 0xFF;
364
365 for (lookup = 0; lookup < max_lookup; ++lookup) {
366 int num_ifindex;
367 int first_ifindex;
368 struct in_addr nexthop_addr;
369
370 num_ifindex = zclient_lookup_nexthop_once(qpim_zclient_lookup, nexthop_tab,
371 PIM_NEXTHOP_IFINDEX_TAB_SIZE, addr);
372 if (num_ifindex < 1) {
373 if (PIM_DEBUG_ZEBRA) {
374 char addr_str[100];
375 pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str));
376 zlog_debug("%s %s: lookup=%d/%d: could not find nexthop ifindex for address %s",
377 __FILE__, __PRETTY_FUNCTION__,
378 lookup, max_lookup, addr_str);
379 }
380 return -1;
381 }
382
383 if (lookup < 1) {
384 /* this is the non-recursive lookup - save original metric/distance */
385 route_metric = nexthop_tab[0].route_metric;
386 protocol_distance = nexthop_tab[0].protocol_distance;
387 }
388
389 /*
390 FIXME: Non-recursive nexthop ensured only for first ifindex.
391 However, recursive route lookup should really be fixed in zebra daemon.
392 See also TODO T24.
393 */
394 first_ifindex = nexthop_tab[0].ifindex;
395 nexthop_addr = nexthop_tab[0].nexthop_addr;
396 if (first_ifindex > 0) {
397 /* found: first ifindex is non-recursive nexthop */
398
399 if (lookup > 0) {
400 /* Report non-recursive success after first lookup */
401 if (PIM_DEBUG_ZEBRA) {
402 char addr_str[100];
403 pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str));
404 zlog_debug("%s %s: lookup=%d/%d: found non-recursive ifindex=%d for address %s dist=%d met=%d",
405 __FILE__, __PRETTY_FUNCTION__,
406 lookup, max_lookup, first_ifindex, addr_str,
407 nexthop_tab[0].protocol_distance,
408 nexthop_tab[0].route_metric);
409 }
410
411 /* use last address as nexthop address */
412 nexthop_tab[0].nexthop_addr = addr;
413
414 /* report original route metric/distance */
415 nexthop_tab[0].route_metric = route_metric;
416 nexthop_tab[0].protocol_distance = protocol_distance;
417 }
418
419 return num_ifindex;
420 }
421
422 if (PIM_DEBUG_ZEBRA) {
423 char addr_str[100];
424 char nexthop_str[100];
425 pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str));
426 pim_inet4_dump("<nexthop?>", nexthop_addr, nexthop_str, sizeof(nexthop_str));
427 zlog_debug("%s %s: lookup=%d/%d: zebra returned recursive nexthop %s for address %s dist=%d met=%d",
428 __FILE__, __PRETTY_FUNCTION__,
429 lookup, max_lookup, nexthop_str, addr_str,
430 nexthop_tab[0].protocol_distance,
431 nexthop_tab[0].route_metric);
432 }
433
434 addr = nexthop_addr; /* use nexthop addr for recursive lookup */
435
436 } /* for (max_lookup) */
437
438 if (PIM_DEBUG_ZEBRA) {
439 char addr_str[100];
440 pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str));
441 zlog_warn("%s %s: lookup=%d/%d: failure searching recursive nexthop ifindex for address %s",
442 __FILE__, __PRETTY_FUNCTION__,
443 lookup, max_lookup, addr_str);
444 }
445
446 return -2;
447 }