]>
Commit | Line | Data |
---|---|---|
1 | /* Zebra's client library. | |
2 | * Copyright (C) 1999 Kunihiro Ishiguro | |
3 | * Copyright (C) 2005 Andrew J. Schorr | |
4 | * | |
5 | * This file is part of GNU Zebra. | |
6 | * | |
7 | * GNU Zebra is free software; you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published | |
9 | * by the Free Software Foundation; either version 2, or (at your | |
10 | * option) any later version. | |
11 | * | |
12 | * GNU Zebra is distributed in the hope that it will be useful, but | |
13 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 | * General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License along | |
18 | * with this program; see the file COPYING; if not, write to the Free Software | |
19 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
20 | */ | |
21 | ||
22 | #include <zebra.h> | |
23 | ||
24 | #include "prefix.h" | |
25 | #include "stream.h" | |
26 | #include "buffer.h" | |
27 | #include "network.h" | |
28 | #include "vrf.h" | |
29 | #include "vrf_int.h" | |
30 | #include "if.h" | |
31 | #include "log.h" | |
32 | #include "thread.h" | |
33 | #include "zclient.h" | |
34 | #include "memory.h" | |
35 | #include "table.h" | |
36 | #include "nexthop.h" | |
37 | #include "mpls.h" | |
38 | #include "sockopt.h" | |
39 | #include "pbr.h" | |
40 | #include "nexthop_group.h" | |
41 | #include "lib_errors.h" | |
42 | ||
43 | DEFINE_MTYPE_STATIC(LIB, ZCLIENT, "Zclient") | |
44 | DEFINE_MTYPE_STATIC(LIB, REDIST_INST, "Redistribution instance IDs") | |
45 | ||
46 | /* Zebra client events. */ | |
47 | enum event { ZCLIENT_SCHEDULE, ZCLIENT_READ, ZCLIENT_CONNECT }; | |
48 | ||
49 | /* Prototype for event manager. */ | |
50 | static void zclient_event(enum event, struct zclient *); | |
51 | ||
52 | struct zclient_options zclient_options_default = {.receive_notify = false}; | |
53 | ||
54 | struct sockaddr_storage zclient_addr; | |
55 | socklen_t zclient_addr_len; | |
56 | ||
57 | /* This file local debug flag. */ | |
58 | static int zclient_debug; | |
59 | ||
60 | /* Allocate zclient structure. */ | |
61 | struct zclient *zclient_new(struct thread_master *master, | |
62 | struct zclient_options *opt) | |
63 | { | |
64 | struct zclient *zclient; | |
65 | zclient = XCALLOC(MTYPE_ZCLIENT, sizeof(struct zclient)); | |
66 | ||
67 | zclient->ibuf = stream_new(ZEBRA_MAX_PACKET_SIZ); | |
68 | zclient->obuf = stream_new(ZEBRA_MAX_PACKET_SIZ); | |
69 | zclient->wb = buffer_new(0); | |
70 | zclient->master = master; | |
71 | ||
72 | zclient->receive_notify = opt->receive_notify; | |
73 | ||
74 | return zclient; | |
75 | } | |
76 | ||
77 | /* This function is only called when exiting, because | |
78 | many parts of the code do not check for I/O errors, so they could | |
79 | reference an invalid pointer if the structure was ever freed. | |
80 | ||
81 | Free zclient structure. */ | |
82 | void zclient_free(struct zclient *zclient) | |
83 | { | |
84 | if (zclient->ibuf) | |
85 | stream_free(zclient->ibuf); | |
86 | if (zclient->obuf) | |
87 | stream_free(zclient->obuf); | |
88 | if (zclient->wb) | |
89 | buffer_free(zclient->wb); | |
90 | ||
91 | XFREE(MTYPE_ZCLIENT, zclient); | |
92 | } | |
93 | ||
94 | unsigned short *redist_check_instance(struct redist_proto *red, | |
95 | unsigned short instance) | |
96 | { | |
97 | struct listnode *node; | |
98 | unsigned short *id; | |
99 | ||
100 | if (!red->instances) | |
101 | return NULL; | |
102 | ||
103 | for (ALL_LIST_ELEMENTS_RO(red->instances, node, id)) | |
104 | if (*id == instance) | |
105 | return id; | |
106 | ||
107 | return NULL; | |
108 | } | |
109 | ||
110 | void redist_add_instance(struct redist_proto *red, unsigned short instance) | |
111 | { | |
112 | unsigned short *in; | |
113 | ||
114 | red->enabled = 1; | |
115 | ||
116 | if (!red->instances) | |
117 | red->instances = list_new(); | |
118 | ||
119 | in = XMALLOC(MTYPE_REDIST_INST, sizeof(unsigned short)); | |
120 | *in = instance; | |
121 | listnode_add(red->instances, in); | |
122 | } | |
123 | ||
124 | void redist_del_instance(struct redist_proto *red, unsigned short instance) | |
125 | { | |
126 | unsigned short *id; | |
127 | ||
128 | id = redist_check_instance(red, instance); | |
129 | if (!id) | |
130 | return; | |
131 | ||
132 | listnode_delete(red->instances, id); | |
133 | XFREE(MTYPE_REDIST_INST, id); | |
134 | if (!red->instances->count) { | |
135 | red->enabled = 0; | |
136 | list_delete(&red->instances); | |
137 | } | |
138 | } | |
139 | ||
140 | /* Stop zebra client services. */ | |
141 | void zclient_stop(struct zclient *zclient) | |
142 | { | |
143 | afi_t afi; | |
144 | int i; | |
145 | ||
146 | if (zclient_debug) | |
147 | zlog_debug("zclient stopped"); | |
148 | ||
149 | /* Stop threads. */ | |
150 | THREAD_OFF(zclient->t_read); | |
151 | THREAD_OFF(zclient->t_connect); | |
152 | THREAD_OFF(zclient->t_write); | |
153 | ||
154 | /* Reset streams. */ | |
155 | stream_reset(zclient->ibuf); | |
156 | stream_reset(zclient->obuf); | |
157 | ||
158 | /* Empty the write buffer. */ | |
159 | buffer_reset(zclient->wb); | |
160 | ||
161 | /* Close socket. */ | |
162 | if (zclient->sock >= 0) { | |
163 | close(zclient->sock); | |
164 | zclient->sock = -1; | |
165 | } | |
166 | zclient->fail = 0; | |
167 | ||
168 | for (afi = AFI_IP; afi < AFI_MAX; afi++) { | |
169 | for (i = 0; i < ZEBRA_ROUTE_MAX; i++) { | |
170 | vrf_bitmap_free(zclient->redist[afi][i]); | |
171 | zclient->redist[afi][i] = VRF_BITMAP_NULL; | |
172 | } | |
173 | redist_del_instance( | |
174 | &zclient->mi_redist[afi][zclient->redist_default], | |
175 | zclient->instance); | |
176 | ||
177 | vrf_bitmap_free(zclient->default_information[afi]); | |
178 | zclient->default_information[afi] = VRF_BITMAP_NULL; | |
179 | } | |
180 | } | |
181 | ||
182 | void zclient_reset(struct zclient *zclient) | |
183 | { | |
184 | afi_t afi; | |
185 | ||
186 | zclient_stop(zclient); | |
187 | ||
188 | for (afi = AFI_IP; afi < AFI_MAX; afi++) | |
189 | redist_del_instance( | |
190 | &zclient->mi_redist[afi][zclient->redist_default], | |
191 | zclient->instance); | |
192 | ||
193 | zclient_init(zclient, zclient->redist_default, zclient->instance, | |
194 | zclient->privs); | |
195 | } | |
196 | ||
197 | /** | |
198 | * Connect to zebra daemon. | |
199 | * @param zclient a pointer to zclient structure | |
200 | * @return socket fd just to make sure that connection established | |
201 | * @see zclient_init | |
202 | * @see zclient_new | |
203 | */ | |
204 | int zclient_socket_connect(struct zclient *zclient) | |
205 | { | |
206 | int sock; | |
207 | int ret; | |
208 | ||
209 | /* We should think about IPv6 connection. */ | |
210 | sock = socket(zclient_addr.ss_family, SOCK_STREAM, 0); | |
211 | if (sock < 0) | |
212 | return -1; | |
213 | ||
214 | set_cloexec(sock); | |
215 | setsockopt_so_sendbuf(sock, 1048576); | |
216 | ||
217 | /* Connect to zebra. */ | |
218 | ret = connect(sock, (struct sockaddr *)&zclient_addr, zclient_addr_len); | |
219 | if (ret < 0) { | |
220 | if (zclient_debug) | |
221 | zlog_debug("%s connect failure: %d(%s)", | |
222 | __PRETTY_FUNCTION__, errno, | |
223 | safe_strerror(errno)); | |
224 | close(sock); | |
225 | return -1; | |
226 | } | |
227 | ||
228 | zclient->sock = sock; | |
229 | return sock; | |
230 | } | |
231 | ||
232 | static int zclient_failed(struct zclient *zclient) | |
233 | { | |
234 | zclient->fail++; | |
235 | zclient_stop(zclient); | |
236 | zclient_event(ZCLIENT_CONNECT, zclient); | |
237 | return -1; | |
238 | } | |
239 | ||
240 | static int zclient_flush_data(struct thread *thread) | |
241 | { | |
242 | struct zclient *zclient = THREAD_ARG(thread); | |
243 | ||
244 | zclient->t_write = NULL; | |
245 | if (zclient->sock < 0) | |
246 | return -1; | |
247 | switch (buffer_flush_available(zclient->wb, zclient->sock)) { | |
248 | case BUFFER_ERROR: | |
249 | flog_err( | |
250 | EC_LIB_ZAPI_SOCKET, | |
251 | "%s: buffer_flush_available failed on zclient fd %d, closing", | |
252 | __func__, zclient->sock); | |
253 | return zclient_failed(zclient); | |
254 | break; | |
255 | case BUFFER_PENDING: | |
256 | zclient->t_write = NULL; | |
257 | thread_add_write(zclient->master, zclient_flush_data, zclient, | |
258 | zclient->sock, &zclient->t_write); | |
259 | break; | |
260 | case BUFFER_EMPTY: | |
261 | break; | |
262 | } | |
263 | return 0; | |
264 | } | |
265 | ||
266 | int zclient_send_message(struct zclient *zclient) | |
267 | { | |
268 | if (zclient->sock < 0) | |
269 | return -1; | |
270 | switch (buffer_write(zclient->wb, zclient->sock, | |
271 | STREAM_DATA(zclient->obuf), | |
272 | stream_get_endp(zclient->obuf))) { | |
273 | case BUFFER_ERROR: | |
274 | flog_err(EC_LIB_ZAPI_SOCKET, | |
275 | "%s: buffer_write failed to zclient fd %d, closing", | |
276 | __func__, zclient->sock); | |
277 | return zclient_failed(zclient); | |
278 | break; | |
279 | case BUFFER_EMPTY: | |
280 | THREAD_OFF(zclient->t_write); | |
281 | break; | |
282 | case BUFFER_PENDING: | |
283 | thread_add_write(zclient->master, zclient_flush_data, zclient, | |
284 | zclient->sock, &zclient->t_write); | |
285 | break; | |
286 | } | |
287 | return 0; | |
288 | } | |
289 | ||
290 | void zclient_create_header(struct stream *s, uint16_t command, vrf_id_t vrf_id) | |
291 | { | |
292 | /* length placeholder, caller can update */ | |
293 | stream_putw(s, ZEBRA_HEADER_SIZE); | |
294 | stream_putc(s, ZEBRA_HEADER_MARKER); | |
295 | stream_putc(s, ZSERV_VERSION); | |
296 | stream_putl(s, vrf_id); | |
297 | stream_putw(s, command); | |
298 | } | |
299 | ||
300 | int zclient_read_header(struct stream *s, int sock, uint16_t *size, | |
301 | uint8_t *marker, uint8_t *version, vrf_id_t *vrf_id, | |
302 | uint16_t *cmd) | |
303 | { | |
304 | if (stream_read(s, sock, ZEBRA_HEADER_SIZE) != ZEBRA_HEADER_SIZE) | |
305 | return -1; | |
306 | ||
307 | STREAM_GETW(s, *size); | |
308 | *size -= ZEBRA_HEADER_SIZE; | |
309 | STREAM_GETC(s, *marker); | |
310 | STREAM_GETC(s, *version); | |
311 | STREAM_GETL(s, *vrf_id); | |
312 | STREAM_GETW(s, *cmd); | |
313 | ||
314 | if (*version != ZSERV_VERSION || *marker != ZEBRA_HEADER_MARKER) { | |
315 | flog_err( | |
316 | EC_LIB_ZAPI_MISSMATCH, | |
317 | "%s: socket %d version mismatch, marker %d, version %d", | |
318 | __func__, sock, *marker, *version); | |
319 | return -1; | |
320 | } | |
321 | ||
322 | if (*size && stream_read(s, sock, *size) != *size) | |
323 | return -1; | |
324 | ||
325 | stream_failure: | |
326 | return 0; | |
327 | } | |
328 | ||
329 | bool zapi_parse_header(struct stream *zmsg, struct zmsghdr *hdr) | |
330 | { | |
331 | STREAM_GETW(zmsg, hdr->length); | |
332 | STREAM_GETC(zmsg, hdr->marker); | |
333 | STREAM_GETC(zmsg, hdr->version); | |
334 | STREAM_GETL(zmsg, hdr->vrf_id); | |
335 | STREAM_GETW(zmsg, hdr->command); | |
336 | return true; | |
337 | stream_failure: | |
338 | return false; | |
339 | } | |
340 | ||
341 | /* Send simple Zebra message. */ | |
342 | static int zebra_message_send(struct zclient *zclient, int command, | |
343 | vrf_id_t vrf_id) | |
344 | { | |
345 | struct stream *s; | |
346 | ||
347 | /* Get zclient output buffer. */ | |
348 | s = zclient->obuf; | |
349 | stream_reset(s); | |
350 | ||
351 | /* Send very simple command only Zebra message. */ | |
352 | zclient_create_header(s, command, vrf_id); | |
353 | ||
354 | return zclient_send_message(zclient); | |
355 | } | |
356 | ||
357 | static int zebra_hello_send(struct zclient *zclient) | |
358 | { | |
359 | struct stream *s; | |
360 | ||
361 | if (zclient->redist_default) { | |
362 | s = zclient->obuf; | |
363 | stream_reset(s); | |
364 | ||
365 | /* The VRF ID in the HELLO message is always 0. */ | |
366 | zclient_create_header(s, ZEBRA_HELLO, VRF_DEFAULT); | |
367 | stream_putc(s, zclient->redist_default); | |
368 | stream_putw(s, zclient->instance); | |
369 | if (zclient->receive_notify) | |
370 | stream_putc(s, 1); | |
371 | else | |
372 | stream_putc(s, 0); | |
373 | ||
374 | stream_putw_at(s, 0, stream_get_endp(s)); | |
375 | return zclient_send_message(zclient); | |
376 | } | |
377 | ||
378 | return 0; | |
379 | } | |
380 | ||
381 | void zclient_send_vrf_label(struct zclient *zclient, vrf_id_t vrf_id, afi_t afi, | |
382 | mpls_label_t label, enum lsp_types_t ltype) | |
383 | { | |
384 | struct stream *s; | |
385 | ||
386 | s = zclient->obuf; | |
387 | stream_reset(s); | |
388 | ||
389 | zclient_create_header(s, ZEBRA_VRF_LABEL, vrf_id); | |
390 | stream_putl(s, label); | |
391 | stream_putc(s, afi); | |
392 | stream_putc(s, ltype); | |
393 | stream_putw_at(s, 0, stream_get_endp(s)); | |
394 | zclient_send_message(zclient); | |
395 | } | |
396 | ||
397 | /* Send register requests to zebra daemon for the information in a VRF. */ | |
398 | void zclient_send_reg_requests(struct zclient *zclient, vrf_id_t vrf_id) | |
399 | { | |
400 | int i; | |
401 | afi_t afi; | |
402 | ||
403 | /* If not connected to the zebra yet. */ | |
404 | if (zclient->sock < 0) | |
405 | return; | |
406 | ||
407 | if (zclient_debug) | |
408 | zlog_debug("%s: send register messages for VRF %u", __func__, | |
409 | vrf_id); | |
410 | ||
411 | /* We need router-id information. */ | |
412 | zebra_message_send(zclient, ZEBRA_ROUTER_ID_ADD, vrf_id); | |
413 | ||
414 | /* We need interface information. */ | |
415 | zebra_message_send(zclient, ZEBRA_INTERFACE_ADD, vrf_id); | |
416 | ||
417 | /* Set unwanted redistribute route. */ | |
418 | for (afi = AFI_IP; afi < AFI_MAX; afi++) | |
419 | vrf_bitmap_set(zclient->redist[afi][zclient->redist_default], | |
420 | vrf_id); | |
421 | ||
422 | /* Flush all redistribute request. */ | |
423 | if (vrf_id == VRF_DEFAULT) { | |
424 | for (afi = AFI_IP; afi < AFI_MAX; afi++) { | |
425 | for (i = 0; i < ZEBRA_ROUTE_MAX; i++) { | |
426 | if (!zclient->mi_redist[afi][i].enabled) | |
427 | continue; | |
428 | ||
429 | struct listnode *node; | |
430 | unsigned short *id; | |
431 | ||
432 | for (ALL_LIST_ELEMENTS_RO( | |
433 | zclient->mi_redist[afi][i] | |
434 | .instances, | |
435 | node, id)) | |
436 | if (!(i == zclient->redist_default | |
437 | && *id == zclient->instance)) | |
438 | zebra_redistribute_send( | |
439 | ZEBRA_REDISTRIBUTE_ADD, | |
440 | zclient, afi, i, *id, | |
441 | VRF_DEFAULT); | |
442 | } | |
443 | } | |
444 | } | |
445 | ||
446 | /* Resend all redistribute request. */ | |
447 | for (afi = AFI_IP; afi < AFI_MAX; afi++) { | |
448 | for (i = 0; i < ZEBRA_ROUTE_MAX; i++) | |
449 | if (i != zclient->redist_default | |
450 | && vrf_bitmap_check(zclient->redist[afi][i], | |
451 | vrf_id)) | |
452 | zebra_redistribute_send(ZEBRA_REDISTRIBUTE_ADD, | |
453 | zclient, afi, i, 0, | |
454 | vrf_id); | |
455 | ||
456 | /* If default information is needed. */ | |
457 | if (vrf_bitmap_check(zclient->default_information[afi], vrf_id)) | |
458 | zebra_redistribute_default_send( | |
459 | ZEBRA_REDISTRIBUTE_DEFAULT_ADD, zclient, afi, | |
460 | vrf_id); | |
461 | } | |
462 | } | |
463 | ||
464 | /* Send unregister requests to zebra daemon for the information in a VRF. */ | |
465 | void zclient_send_dereg_requests(struct zclient *zclient, vrf_id_t vrf_id) | |
466 | { | |
467 | int i; | |
468 | afi_t afi; | |
469 | ||
470 | /* If not connected to the zebra yet. */ | |
471 | if (zclient->sock < 0) | |
472 | return; | |
473 | ||
474 | if (zclient_debug) | |
475 | zlog_debug("%s: send deregister messages for VRF %u", __func__, | |
476 | vrf_id); | |
477 | ||
478 | /* We need router-id information. */ | |
479 | zebra_message_send(zclient, ZEBRA_ROUTER_ID_DELETE, vrf_id); | |
480 | ||
481 | zebra_message_send(zclient, ZEBRA_INTERFACE_DELETE, vrf_id); | |
482 | ||
483 | /* Set unwanted redistribute route. */ | |
484 | for (afi = AFI_IP; afi < AFI_MAX; afi++) | |
485 | vrf_bitmap_unset(zclient->redist[afi][zclient->redist_default], | |
486 | vrf_id); | |
487 | ||
488 | /* Flush all redistribute request. */ | |
489 | if (vrf_id == VRF_DEFAULT) { | |
490 | for (afi = AFI_IP; afi < AFI_MAX; afi++) { | |
491 | for (i = 0; i < ZEBRA_ROUTE_MAX; i++) { | |
492 | if (!zclient->mi_redist[afi][i].enabled) | |
493 | continue; | |
494 | ||
495 | struct listnode *node; | |
496 | unsigned short *id; | |
497 | ||
498 | for (ALL_LIST_ELEMENTS_RO( | |
499 | zclient->mi_redist[afi][i] | |
500 | .instances, | |
501 | node, id)) | |
502 | if (!(i == zclient->redist_default | |
503 | && *id == zclient->instance)) | |
504 | zebra_redistribute_send( | |
505 | ZEBRA_REDISTRIBUTE_DELETE, | |
506 | zclient, afi, i, *id, | |
507 | VRF_DEFAULT); | |
508 | } | |
509 | } | |
510 | } | |
511 | ||
512 | /* Flush all redistribute request. */ | |
513 | for (afi = AFI_IP; afi < AFI_MAX; afi++) { | |
514 | for (i = 0; i < ZEBRA_ROUTE_MAX; i++) | |
515 | if (i != zclient->redist_default | |
516 | && vrf_bitmap_check(zclient->redist[afi][i], | |
517 | vrf_id)) | |
518 | zebra_redistribute_send( | |
519 | ZEBRA_REDISTRIBUTE_DELETE, zclient, afi, | |
520 | i, 0, vrf_id); | |
521 | ||
522 | /* If default information is needed. */ | |
523 | if (vrf_bitmap_check(zclient->default_information[afi], vrf_id)) | |
524 | zebra_redistribute_default_send( | |
525 | ZEBRA_REDISTRIBUTE_DEFAULT_DELETE, zclient, afi, | |
526 | vrf_id); | |
527 | } | |
528 | } | |
529 | ||
530 | /* Send request to zebra daemon to start or stop RA. */ | |
531 | void zclient_send_interface_radv_req(struct zclient *zclient, vrf_id_t vrf_id, | |
532 | struct interface *ifp, int enable, | |
533 | int ra_interval) | |
534 | { | |
535 | struct stream *s; | |
536 | ||
537 | /* If not connected to the zebra yet. */ | |
538 | if (zclient->sock < 0) | |
539 | return; | |
540 | ||
541 | /* Form and send message. */ | |
542 | s = zclient->obuf; | |
543 | stream_reset(s); | |
544 | ||
545 | if (enable) | |
546 | zclient_create_header(s, ZEBRA_INTERFACE_ENABLE_RADV, vrf_id); | |
547 | else | |
548 | zclient_create_header(s, ZEBRA_INTERFACE_DISABLE_RADV, vrf_id); | |
549 | ||
550 | stream_putl(s, ifp->ifindex); | |
551 | stream_putl(s, ra_interval); | |
552 | ||
553 | stream_putw_at(s, 0, stream_get_endp(s)); | |
554 | ||
555 | zclient_send_message(zclient); | |
556 | } | |
557 | ||
558 | int zclient_send_interface_protodown(struct zclient *zclient, vrf_id_t vrf_id, | |
559 | struct interface *ifp, bool down) | |
560 | { | |
561 | struct stream *s; | |
562 | ||
563 | if (zclient->sock < 0) | |
564 | return -1; | |
565 | ||
566 | s = zclient->obuf; | |
567 | stream_reset(s); | |
568 | zclient_create_header(s, ZEBRA_INTERFACE_SET_PROTODOWN, vrf_id); | |
569 | stream_putl(s, ifp->ifindex); | |
570 | stream_putc(s, !!down); | |
571 | stream_putw_at(s, 0, stream_get_endp(s)); | |
572 | zclient_send_message(zclient); | |
573 | ||
574 | return 0; | |
575 | } | |
576 | ||
577 | /* Make connection to zebra daemon. */ | |
578 | int zclient_start(struct zclient *zclient) | |
579 | { | |
580 | if (zclient_debug) | |
581 | zlog_info("zclient_start is called"); | |
582 | ||
583 | /* If already connected to the zebra. */ | |
584 | if (zclient->sock >= 0) | |
585 | return 0; | |
586 | ||
587 | /* Check connect thread. */ | |
588 | if (zclient->t_connect) | |
589 | return 0; | |
590 | ||
591 | if (zclient_socket_connect(zclient) < 0) { | |
592 | if (zclient_debug) | |
593 | zlog_debug("zclient connection fail"); | |
594 | zclient->fail++; | |
595 | zclient_event(ZCLIENT_CONNECT, zclient); | |
596 | return -1; | |
597 | } | |
598 | ||
599 | if (set_nonblocking(zclient->sock) < 0) | |
600 | flog_err(EC_LIB_ZAPI_SOCKET, "%s: set_nonblocking(%d) failed", | |
601 | __func__, zclient->sock); | |
602 | ||
603 | /* Clear fail count. */ | |
604 | zclient->fail = 0; | |
605 | if (zclient_debug) | |
606 | zlog_debug("zclient connect success with socket [%d]", | |
607 | zclient->sock); | |
608 | ||
609 | /* Create read thread. */ | |
610 | zclient_event(ZCLIENT_READ, zclient); | |
611 | ||
612 | zebra_hello_send(zclient); | |
613 | ||
614 | zebra_message_send(zclient, ZEBRA_INTERFACE_ADD, VRF_DEFAULT); | |
615 | ||
616 | /* Inform the successful connection. */ | |
617 | if (zclient->zebra_connected) | |
618 | (*zclient->zebra_connected)(zclient); | |
619 | ||
620 | return 0; | |
621 | } | |
622 | ||
623 | /* Initialize zebra client. Argument redist_default is unwanted | |
624 | redistribute route type. */ | |
625 | void zclient_init(struct zclient *zclient, int redist_default, | |
626 | unsigned short instance, struct zebra_privs_t *privs) | |
627 | { | |
628 | int afi, i; | |
629 | ||
630 | /* Set -1 to the default socket value. */ | |
631 | zclient->sock = -1; | |
632 | zclient->privs = privs; | |
633 | ||
634 | /* Clear redistribution flags. */ | |
635 | for (afi = AFI_IP; afi < AFI_MAX; afi++) | |
636 | for (i = 0; i < ZEBRA_ROUTE_MAX; i++) | |
637 | zclient->redist[afi][i] = vrf_bitmap_init(); | |
638 | ||
639 | /* Set unwanted redistribute route. bgpd does not need BGP route | |
640 | redistribution. */ | |
641 | zclient->redist_default = redist_default; | |
642 | zclient->instance = instance; | |
643 | /* Pending: make afi(s) an arg. */ | |
644 | for (afi = AFI_IP; afi < AFI_MAX; afi++) { | |
645 | redist_add_instance(&zclient->mi_redist[afi][redist_default], | |
646 | instance); | |
647 | ||
648 | /* Set default-information redistribute to zero. */ | |
649 | zclient->default_information[afi] = vrf_bitmap_init(); | |
650 | } | |
651 | ||
652 | if (zclient_debug) | |
653 | zlog_debug("scheduling zclient connection"); | |
654 | ||
655 | zclient_event(ZCLIENT_SCHEDULE, zclient); | |
656 | } | |
657 | ||
658 | /* This function is a wrapper function for calling zclient_start from | |
659 | timer or event thread. */ | |
660 | static int zclient_connect(struct thread *t) | |
661 | { | |
662 | struct zclient *zclient; | |
663 | ||
664 | zclient = THREAD_ARG(t); | |
665 | zclient->t_connect = NULL; | |
666 | ||
667 | if (zclient_debug) | |
668 | zlog_debug("zclient_connect is called"); | |
669 | ||
670 | return zclient_start(zclient); | |
671 | } | |
672 | ||
673 | int zclient_send_rnh(struct zclient *zclient, int command, struct prefix *p, | |
674 | bool exact_match, vrf_id_t vrf_id) | |
675 | { | |
676 | struct stream *s; | |
677 | ||
678 | s = zclient->obuf; | |
679 | stream_reset(s); | |
680 | zclient_create_header(s, command, vrf_id); | |
681 | stream_putc(s, (exact_match) ? 1 : 0); | |
682 | ||
683 | stream_putw(s, PREFIX_FAMILY(p)); | |
684 | stream_putc(s, p->prefixlen); | |
685 | switch (PREFIX_FAMILY(p)) { | |
686 | case AF_INET: | |
687 | stream_put_in_addr(s, &p->u.prefix4); | |
688 | break; | |
689 | case AF_INET6: | |
690 | stream_put(s, &(p->u.prefix6), 16); | |
691 | break; | |
692 | default: | |
693 | break; | |
694 | } | |
695 | stream_putw_at(s, 0, stream_get_endp(s)); | |
696 | ||
697 | return zclient_send_message(zclient); | |
698 | } | |
699 | ||
700 | /* | |
701 | * "xdr_encode"-like interface that allows daemon (client) to send | |
702 | * a message to zebra server for a route that needs to be | |
703 | * added/deleted to the kernel. Info about the route is specified | |
704 | * by the caller in a struct zapi_route. zapi_route_encode() then writes | |
705 | * the info down the zclient socket using the stream_* functions. | |
706 | * | |
707 | * The corresponding read ("xdr_decode") function on the server | |
708 | * side is zapi_route_decode(). | |
709 | * | |
710 | * 0 1 2 3 4 5 6 7 8 9 A B C D E F 0 1 2 3 4 5 6 7 8 9 A B C D E F | |
711 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
712 | * | Length (2) | Command | Route Type | | |
713 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
714 | * | ZEBRA Flags | Message Flags | Prefix length | | |
715 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
716 | * | Destination IPv4 Prefix for route | | |
717 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
718 | * | Nexthop count | | |
719 | * +-+-+-+-+-+-+-+-+ | |
720 | * | |
721 | * | |
722 | * A number of IPv4 nexthop(s) or nexthop interface index(es) are then | |
723 | * described, as per the Nexthop count. Each nexthop described as: | |
724 | * | |
725 | * +-+-+-+-+-+-+-+-+ | |
726 | * | Nexthop Type | Set to one of ZEBRA_NEXTHOP_* | |
727 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
728 | * | IPv4 Nexthop address or Interface Index number | | |
729 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
730 | * | |
731 | * Alternatively, if the route is a blackhole route, then Nexthop count | |
732 | * is set to 1 and a nexthop of type NEXTHOP_TYPE_BLACKHOLE is the sole | |
733 | * nexthop. | |
734 | * | |
735 | * The original struct zapi_route_*() infrastructure was built around | |
736 | * the traditional (32-bit "gate OR ifindex") nexthop data unit. | |
737 | * A special encoding can be used to feed onlink (64-bit "gate AND ifindex") | |
738 | * nexthops into zapi_route_encode() using the same zapi_route structure. | |
739 | * This is done by setting zapi_route fields as follows: | |
740 | * - .message |= ZAPI_MESSAGE_NEXTHOP | ZAPI_MESSAGE_ONLINK | |
741 | * - .nexthop_num == .ifindex_num | |
742 | * - .nexthop and .ifindex are filled with gate and ifindex parts of | |
743 | * each compound nexthop, both in the same order | |
744 | * | |
745 | * If ZAPI_MESSAGE_DISTANCE is set, the distance value is written as a 1 | |
746 | * byte value. | |
747 | * | |
748 | * If ZAPI_MESSAGE_METRIC is set, the metric value is written as an 8 | |
749 | * byte value. | |
750 | * | |
751 | * If ZAPI_MESSAGE_TAG is set, the tag value is written as a 4 byte value | |
752 | * | |
753 | * If ZAPI_MESSAGE_MTU is set, the mtu value is written as a 4 byte value | |
754 | * | |
755 | * XXX: No attention paid to alignment. | |
756 | */ | |
757 | int zclient_route_send(uint8_t cmd, struct zclient *zclient, | |
758 | struct zapi_route *api) | |
759 | { | |
760 | if (zapi_route_encode(cmd, zclient->obuf, api) < 0) | |
761 | return -1; | |
762 | return zclient_send_message(zclient); | |
763 | } | |
764 | ||
765 | int zapi_route_encode(uint8_t cmd, struct stream *s, struct zapi_route *api) | |
766 | { | |
767 | struct zapi_nexthop *api_nh; | |
768 | int i; | |
769 | int psize; | |
770 | ||
771 | stream_reset(s); | |
772 | zclient_create_header(s, cmd, api->vrf_id); | |
773 | ||
774 | if (api->type >= ZEBRA_ROUTE_MAX) { | |
775 | flog_err(EC_LIB_ZAPI_ENCODE, | |
776 | "%s: Specified route type (%u) is not a legal value\n", | |
777 | __PRETTY_FUNCTION__, api->type); | |
778 | return -1; | |
779 | } | |
780 | stream_putc(s, api->type); | |
781 | ||
782 | stream_putw(s, api->instance); | |
783 | stream_putl(s, api->flags); | |
784 | stream_putc(s, api->message); | |
785 | ||
786 | if (api->safi < SAFI_UNICAST || api->safi >= SAFI_MAX) { | |
787 | flog_err(EC_LIB_ZAPI_ENCODE, | |
788 | "%s: Specified route SAFI (%u) is not a legal value\n", | |
789 | __PRETTY_FUNCTION__, api->safi); | |
790 | return -1; | |
791 | } | |
792 | stream_putc(s, api->safi); | |
793 | ||
794 | /* Put prefix information. */ | |
795 | stream_putc(s, api->prefix.family); | |
796 | psize = PSIZE(api->prefix.prefixlen); | |
797 | stream_putc(s, api->prefix.prefixlen); | |
798 | stream_write(s, (uint8_t *)&api->prefix.u.prefix, psize); | |
799 | ||
800 | if (CHECK_FLAG(api->message, ZAPI_MESSAGE_SRCPFX)) { | |
801 | psize = PSIZE(api->src_prefix.prefixlen); | |
802 | stream_putc(s, api->src_prefix.prefixlen); | |
803 | stream_write(s, (uint8_t *)&api->src_prefix.prefix, psize); | |
804 | } | |
805 | ||
806 | /* Nexthops. */ | |
807 | if (CHECK_FLAG(api->message, ZAPI_MESSAGE_NEXTHOP)) { | |
808 | /* limit the number of nexthops if necessary */ | |
809 | if (api->nexthop_num > MULTIPATH_NUM) { | |
810 | char buf[PREFIX2STR_BUFFER]; | |
811 | ||
812 | prefix2str(&api->prefix, buf, sizeof(buf)); | |
813 | flog_err( | |
814 | EC_LIB_ZAPI_ENCODE, | |
815 | "%s: prefix %s: can't encode %u nexthops (maximum is %u)", | |
816 | __func__, buf, api->nexthop_num, MULTIPATH_NUM); | |
817 | return -1; | |
818 | } | |
819 | ||
820 | stream_putw(s, api->nexthop_num); | |
821 | ||
822 | for (i = 0; i < api->nexthop_num; i++) { | |
823 | api_nh = &api->nexthops[i]; | |
824 | ||
825 | stream_putl(s, api_nh->vrf_id); | |
826 | stream_putc(s, api_nh->type); | |
827 | stream_putc(s, api_nh->onlink); | |
828 | switch (api_nh->type) { | |
829 | case NEXTHOP_TYPE_BLACKHOLE: | |
830 | stream_putc(s, api_nh->bh_type); | |
831 | break; | |
832 | case NEXTHOP_TYPE_IPV4: | |
833 | stream_put_in_addr(s, &api_nh->gate.ipv4); | |
834 | break; | |
835 | case NEXTHOP_TYPE_IPV4_IFINDEX: | |
836 | stream_put_in_addr(s, &api_nh->gate.ipv4); | |
837 | stream_putl(s, api_nh->ifindex); | |
838 | break; | |
839 | case NEXTHOP_TYPE_IFINDEX: | |
840 | stream_putl(s, api_nh->ifindex); | |
841 | break; | |
842 | case NEXTHOP_TYPE_IPV6: | |
843 | stream_write(s, (uint8_t *)&api_nh->gate.ipv6, | |
844 | 16); | |
845 | break; | |
846 | case NEXTHOP_TYPE_IPV6_IFINDEX: | |
847 | stream_write(s, (uint8_t *)&api_nh->gate.ipv6, | |
848 | 16); | |
849 | stream_putl(s, api_nh->ifindex); | |
850 | break; | |
851 | } | |
852 | ||
853 | /* MPLS labels for BGP-LU or Segment Routing */ | |
854 | if (CHECK_FLAG(api->message, ZAPI_MESSAGE_LABEL)) { | |
855 | if (api_nh->label_num > MPLS_MAX_LABELS) { | |
856 | char buf[PREFIX2STR_BUFFER]; | |
857 | prefix2str(&api->prefix, buf, | |
858 | sizeof(buf)); | |
859 | flog_err(EC_LIB_ZAPI_ENCODE, | |
860 | "%s: prefix %s: can't encode " | |
861 | "%u labels (maximum is %u)", | |
862 | __func__, buf, | |
863 | api_nh->label_num, | |
864 | MPLS_MAX_LABELS); | |
865 | return -1; | |
866 | } | |
867 | ||
868 | stream_putc(s, api_nh->label_num); | |
869 | stream_put(s, &api_nh->labels[0], | |
870 | api_nh->label_num | |
871 | * sizeof(mpls_label_t)); | |
872 | } | |
873 | ||
874 | /* Router MAC for EVPN routes. */ | |
875 | if (CHECK_FLAG(api->flags, ZEBRA_FLAG_EVPN_ROUTE)) | |
876 | stream_put(s, &(api_nh->rmac), | |
877 | sizeof(struct ethaddr)); | |
878 | } | |
879 | } | |
880 | ||
881 | /* Attributes. */ | |
882 | if (CHECK_FLAG(api->message, ZAPI_MESSAGE_DISTANCE)) | |
883 | stream_putc(s, api->distance); | |
884 | if (CHECK_FLAG(api->message, ZAPI_MESSAGE_METRIC)) | |
885 | stream_putl(s, api->metric); | |
886 | if (CHECK_FLAG(api->message, ZAPI_MESSAGE_TAG)) | |
887 | stream_putl(s, api->tag); | |
888 | if (CHECK_FLAG(api->message, ZAPI_MESSAGE_MTU)) | |
889 | stream_putl(s, api->mtu); | |
890 | if (CHECK_FLAG(api->message, ZAPI_MESSAGE_TABLEID)) | |
891 | stream_putl(s, api->tableid); | |
892 | ||
893 | /* Put length at the first point of the stream. */ | |
894 | stream_putw_at(s, 0, stream_get_endp(s)); | |
895 | ||
896 | return 0; | |
897 | } | |
898 | ||
899 | int zapi_route_decode(struct stream *s, struct zapi_route *api) | |
900 | { | |
901 | struct zapi_nexthop *api_nh; | |
902 | int i; | |
903 | ||
904 | memset(api, 0, sizeof(*api)); | |
905 | ||
906 | /* Type, flags, message. */ | |
907 | STREAM_GETC(s, api->type); | |
908 | if (api->type >= ZEBRA_ROUTE_MAX) { | |
909 | flog_err(EC_LIB_ZAPI_ENCODE, | |
910 | "%s: Specified route type: %d is not a legal value\n", | |
911 | __PRETTY_FUNCTION__, api->type); | |
912 | return -1; | |
913 | } | |
914 | ||
915 | STREAM_GETW(s, api->instance); | |
916 | STREAM_GETL(s, api->flags); | |
917 | STREAM_GETC(s, api->message); | |
918 | STREAM_GETC(s, api->safi); | |
919 | if (api->safi < SAFI_UNICAST || api->safi >= SAFI_MAX) { | |
920 | flog_err(EC_LIB_ZAPI_ENCODE, | |
921 | "%s: Specified route SAFI (%u) is not a legal value\n", | |
922 | __PRETTY_FUNCTION__, api->safi); | |
923 | return -1; | |
924 | } | |
925 | ||
926 | /* Prefix. */ | |
927 | STREAM_GETC(s, api->prefix.family); | |
928 | STREAM_GETC(s, api->prefix.prefixlen); | |
929 | switch (api->prefix.family) { | |
930 | case AF_INET: | |
931 | if (api->prefix.prefixlen > IPV4_MAX_PREFIXLEN) { | |
932 | flog_err( | |
933 | EC_LIB_ZAPI_ENCODE, | |
934 | "%s: V4 prefixlen is %d which should not be more than 32", | |
935 | __PRETTY_FUNCTION__, api->prefix.prefixlen); | |
936 | return -1; | |
937 | } | |
938 | break; | |
939 | case AF_INET6: | |
940 | if (api->prefix.prefixlen > IPV6_MAX_PREFIXLEN) { | |
941 | flog_err( | |
942 | EC_LIB_ZAPI_ENCODE, | |
943 | "%s: v6 prefixlen is %d which should not be more than 128", | |
944 | __PRETTY_FUNCTION__, api->prefix.prefixlen); | |
945 | return -1; | |
946 | } | |
947 | break; | |
948 | default: | |
949 | flog_err(EC_LIB_ZAPI_ENCODE, | |
950 | "%s: Specified family %d is not v4 or v6", | |
951 | __PRETTY_FUNCTION__, api->prefix.family); | |
952 | return -1; | |
953 | } | |
954 | STREAM_GET(&api->prefix.u.prefix, s, PSIZE(api->prefix.prefixlen)); | |
955 | ||
956 | if (CHECK_FLAG(api->message, ZAPI_MESSAGE_SRCPFX)) { | |
957 | api->src_prefix.family = AF_INET6; | |
958 | STREAM_GETC(s, api->src_prefix.prefixlen); | |
959 | if (api->src_prefix.prefixlen > IPV6_MAX_PREFIXLEN) { | |
960 | flog_err( | |
961 | EC_LIB_ZAPI_ENCODE, | |
962 | "%s: SRC Prefix prefixlen received: %d is too large", | |
963 | __PRETTY_FUNCTION__, api->src_prefix.prefixlen); | |
964 | return -1; | |
965 | } | |
966 | STREAM_GET(&api->src_prefix.prefix, s, | |
967 | PSIZE(api->src_prefix.prefixlen)); | |
968 | ||
969 | if (api->prefix.family != AF_INET6 | |
970 | || api->src_prefix.prefixlen == 0) { | |
971 | flog_err( | |
972 | EC_LIB_ZAPI_ENCODE, | |
973 | "%s: SRC prefix specified in some manner that makes no sense", | |
974 | __PRETTY_FUNCTION__); | |
975 | return -1; | |
976 | } | |
977 | } | |
978 | ||
979 | /* Nexthops. */ | |
980 | if (CHECK_FLAG(api->message, ZAPI_MESSAGE_NEXTHOP)) { | |
981 | STREAM_GETW(s, api->nexthop_num); | |
982 | if (api->nexthop_num > MULTIPATH_NUM) { | |
983 | flog_err(EC_LIB_ZAPI_ENCODE, | |
984 | "%s: invalid number of nexthops (%u)", | |
985 | __func__, api->nexthop_num); | |
986 | return -1; | |
987 | } | |
988 | ||
989 | for (i = 0; i < api->nexthop_num; i++) { | |
990 | api_nh = &api->nexthops[i]; | |
991 | ||
992 | STREAM_GETL(s, api_nh->vrf_id); | |
993 | STREAM_GETC(s, api_nh->type); | |
994 | STREAM_GETC(s, api_nh->onlink); | |
995 | switch (api_nh->type) { | |
996 | case NEXTHOP_TYPE_BLACKHOLE: | |
997 | STREAM_GETC(s, api_nh->bh_type); | |
998 | break; | |
999 | case NEXTHOP_TYPE_IPV4: | |
1000 | STREAM_GET(&api_nh->gate.ipv4.s_addr, s, | |
1001 | IPV4_MAX_BYTELEN); | |
1002 | break; | |
1003 | case NEXTHOP_TYPE_IPV4_IFINDEX: | |
1004 | STREAM_GET(&api_nh->gate.ipv4.s_addr, s, | |
1005 | IPV4_MAX_BYTELEN); | |
1006 | STREAM_GETL(s, api_nh->ifindex); | |
1007 | break; | |
1008 | case NEXTHOP_TYPE_IFINDEX: | |
1009 | STREAM_GETL(s, api_nh->ifindex); | |
1010 | break; | |
1011 | case NEXTHOP_TYPE_IPV6: | |
1012 | STREAM_GET(&api_nh->gate.ipv6, s, 16); | |
1013 | break; | |
1014 | case NEXTHOP_TYPE_IPV6_IFINDEX: | |
1015 | STREAM_GET(&api_nh->gate.ipv6, s, 16); | |
1016 | STREAM_GETL(s, api_nh->ifindex); | |
1017 | break; | |
1018 | } | |
1019 | ||
1020 | /* MPLS labels for BGP-LU or Segment Routing */ | |
1021 | if (CHECK_FLAG(api->message, ZAPI_MESSAGE_LABEL)) { | |
1022 | STREAM_GETC(s, api_nh->label_num); | |
1023 | ||
1024 | if (api_nh->label_num > MPLS_MAX_LABELS) { | |
1025 | flog_err( | |
1026 | EC_LIB_ZAPI_ENCODE, | |
1027 | "%s: invalid number of MPLS labels (%u)", | |
1028 | __func__, api_nh->label_num); | |
1029 | return -1; | |
1030 | } | |
1031 | ||
1032 | STREAM_GET(&api_nh->labels[0], s, | |
1033 | api_nh->label_num | |
1034 | * sizeof(mpls_label_t)); | |
1035 | } | |
1036 | ||
1037 | /* Router MAC for EVPN routes. */ | |
1038 | if (CHECK_FLAG(api->flags, ZEBRA_FLAG_EVPN_ROUTE)) | |
1039 | stream_get(&(api_nh->rmac), s, | |
1040 | sizeof(struct ethaddr)); | |
1041 | } | |
1042 | } | |
1043 | ||
1044 | /* Attributes. */ | |
1045 | if (CHECK_FLAG(api->message, ZAPI_MESSAGE_DISTANCE)) | |
1046 | STREAM_GETC(s, api->distance); | |
1047 | if (CHECK_FLAG(api->message, ZAPI_MESSAGE_METRIC)) | |
1048 | STREAM_GETL(s, api->metric); | |
1049 | if (CHECK_FLAG(api->message, ZAPI_MESSAGE_TAG)) | |
1050 | STREAM_GETL(s, api->tag); | |
1051 | if (CHECK_FLAG(api->message, ZAPI_MESSAGE_MTU)) | |
1052 | STREAM_GETL(s, api->mtu); | |
1053 | if (CHECK_FLAG(api->message, ZAPI_MESSAGE_TABLEID)) | |
1054 | STREAM_GETL(s, api->tableid); | |
1055 | ||
1056 | stream_failure: | |
1057 | return 0; | |
1058 | } | |
1059 | ||
1060 | static void zapi_encode_prefix(struct stream *s, struct prefix *p, | |
1061 | uint8_t family) | |
1062 | { | |
1063 | struct prefix any; | |
1064 | ||
1065 | if (!p) { | |
1066 | memset(&any, 0, sizeof(any)); | |
1067 | any.family = family; | |
1068 | p = &any; | |
1069 | } | |
1070 | ||
1071 | stream_putc(s, p->family); | |
1072 | stream_putc(s, p->prefixlen); | |
1073 | stream_put(s, &p->u.prefix, prefix_blen(p)); | |
1074 | } | |
1075 | ||
1076 | int zapi_pbr_rule_encode(uint8_t cmd, struct stream *s, struct pbr_rule *zrule) | |
1077 | { | |
1078 | stream_reset(s); | |
1079 | zclient_create_header(s, cmd, zrule->vrf_id); | |
1080 | ||
1081 | /* | |
1082 | * We are sending one item at a time at the moment | |
1083 | */ | |
1084 | stream_putl(s, 1); | |
1085 | ||
1086 | stream_putl(s, zrule->seq); | |
1087 | stream_putl(s, zrule->priority); | |
1088 | stream_putl(s, zrule->unique); | |
1089 | ||
1090 | zapi_encode_prefix(s, &(zrule->filter.src_ip), | |
1091 | zrule->filter.src_ip.family); | |
1092 | stream_putw(s, zrule->filter.src_port); /* src port */ | |
1093 | zapi_encode_prefix(s, &(zrule->filter.dst_ip), | |
1094 | zrule->filter.src_ip.family); | |
1095 | stream_putw(s, zrule->filter.dst_port); /* dst port */ | |
1096 | stream_putw(s, zrule->filter.fwmark); /* fwmark */ | |
1097 | ||
1098 | stream_putl(s, zrule->action.table); | |
1099 | stream_putl(s, zrule->ifindex); | |
1100 | ||
1101 | /* Put length at the first point of the stream. */ | |
1102 | stream_putw_at(s, 0, stream_get_endp(s)); | |
1103 | ||
1104 | return 0; | |
1105 | } | |
1106 | ||
1107 | bool zapi_route_notify_decode(struct stream *s, struct prefix *p, | |
1108 | uint32_t *tableid, | |
1109 | enum zapi_route_notify_owner *note) | |
1110 | { | |
1111 | uint32_t t; | |
1112 | ||
1113 | STREAM_GET(note, s, sizeof(*note)); | |
1114 | ||
1115 | STREAM_GETC(s, p->family); | |
1116 | STREAM_GETC(s, p->prefixlen); | |
1117 | STREAM_GET(&p->u.prefix, s, prefix_blen(p)); | |
1118 | STREAM_GETL(s, t); | |
1119 | ||
1120 | *tableid = t; | |
1121 | ||
1122 | return true; | |
1123 | ||
1124 | stream_failure: | |
1125 | return false; | |
1126 | } | |
1127 | ||
1128 | bool zapi_rule_notify_decode(struct stream *s, uint32_t *seqno, | |
1129 | uint32_t *priority, uint32_t *unique, | |
1130 | ifindex_t *ifindex, | |
1131 | enum zapi_rule_notify_owner *note) | |
1132 | { | |
1133 | uint32_t prio, seq, uni; | |
1134 | ifindex_t ifi; | |
1135 | ||
1136 | STREAM_GET(note, s, sizeof(*note)); | |
1137 | ||
1138 | STREAM_GETL(s, seq); | |
1139 | STREAM_GETL(s, prio); | |
1140 | STREAM_GETL(s, uni); | |
1141 | STREAM_GETL(s, ifi); | |
1142 | ||
1143 | if (zclient_debug) | |
1144 | zlog_debug("%s: %u %u %u %u", __PRETTY_FUNCTION__, seq, prio, | |
1145 | uni, ifi); | |
1146 | *seqno = seq; | |
1147 | *priority = prio; | |
1148 | *unique = uni; | |
1149 | *ifindex = ifi; | |
1150 | ||
1151 | return true; | |
1152 | ||
1153 | stream_failure: | |
1154 | return false; | |
1155 | } | |
1156 | ||
1157 | bool zapi_ipset_notify_decode(struct stream *s, uint32_t *unique, | |
1158 | enum zapi_ipset_notify_owner *note) | |
1159 | { | |
1160 | uint32_t uni; | |
1161 | ||
1162 | STREAM_GET(note, s, sizeof(*note)); | |
1163 | ||
1164 | STREAM_GETL(s, uni); | |
1165 | ||
1166 | if (zclient_debug) | |
1167 | zlog_debug("%s: %u", __PRETTY_FUNCTION__, uni); | |
1168 | *unique = uni; | |
1169 | ||
1170 | return true; | |
1171 | ||
1172 | stream_failure: | |
1173 | return false; | |
1174 | } | |
1175 | ||
1176 | bool zapi_ipset_entry_notify_decode(struct stream *s, uint32_t *unique, | |
1177 | char *ipset_name, | |
1178 | enum zapi_ipset_entry_notify_owner *note) | |
1179 | { | |
1180 | uint32_t uni; | |
1181 | ||
1182 | STREAM_GET(note, s, sizeof(*note)); | |
1183 | ||
1184 | STREAM_GETL(s, uni); | |
1185 | ||
1186 | STREAM_GET(ipset_name, s, ZEBRA_IPSET_NAME_SIZE); | |
1187 | ||
1188 | if (zclient_debug) | |
1189 | zlog_debug("%s: %u", __PRETTY_FUNCTION__, uni); | |
1190 | *unique = uni; | |
1191 | ||
1192 | return true; | |
1193 | ||
1194 | stream_failure: | |
1195 | return false; | |
1196 | } | |
1197 | ||
1198 | bool zapi_iptable_notify_decode(struct stream *s, | |
1199 | uint32_t *unique, | |
1200 | enum zapi_iptable_notify_owner *note) | |
1201 | { | |
1202 | uint32_t uni; | |
1203 | ||
1204 | STREAM_GET(note, s, sizeof(*note)); | |
1205 | ||
1206 | STREAM_GETL(s, uni); | |
1207 | ||
1208 | if (zclient_debug) | |
1209 | zlog_debug("%s: %u", __PRETTY_FUNCTION__, uni); | |
1210 | *unique = uni; | |
1211 | ||
1212 | return true; | |
1213 | ||
1214 | stream_failure: | |
1215 | return false; | |
1216 | } | |
1217 | ||
1218 | struct nexthop *nexthop_from_zapi_nexthop(struct zapi_nexthop *znh) | |
1219 | { | |
1220 | struct nexthop *n = nexthop_new(); | |
1221 | ||
1222 | n->type = znh->type; | |
1223 | n->vrf_id = znh->vrf_id; | |
1224 | n->ifindex = znh->ifindex; | |
1225 | n->gate = znh->gate; | |
1226 | ||
1227 | /* | |
1228 | * This function currently handles labels | |
1229 | */ | |
1230 | if (znh->label_num) { | |
1231 | nexthop_add_labels(n, ZEBRA_LSP_NONE, znh->label_num, | |
1232 | znh->labels); | |
1233 | } | |
1234 | ||
1235 | return n; | |
1236 | } | |
1237 | ||
1238 | bool zapi_nexthop_update_decode(struct stream *s, struct zapi_route *nhr) | |
1239 | { | |
1240 | uint32_t i; | |
1241 | ||
1242 | memset(nhr, 0, sizeof(*nhr)); | |
1243 | ||
1244 | STREAM_GETW(s, nhr->prefix.family); | |
1245 | STREAM_GETC(s, nhr->prefix.prefixlen); | |
1246 | switch (nhr->prefix.family) { | |
1247 | case AF_INET: | |
1248 | STREAM_GET(&nhr->prefix.u.prefix4.s_addr, s, IPV4_MAX_BYTELEN); | |
1249 | break; | |
1250 | case AF_INET6: | |
1251 | STREAM_GET(&nhr->prefix.u.prefix6, s, IPV6_MAX_BYTELEN); | |
1252 | break; | |
1253 | default: | |
1254 | break; | |
1255 | } | |
1256 | ||
1257 | STREAM_GETC(s, nhr->type); | |
1258 | STREAM_GETW(s, nhr->instance); | |
1259 | STREAM_GETC(s, nhr->distance); | |
1260 | STREAM_GETL(s, nhr->metric); | |
1261 | STREAM_GETC(s, nhr->nexthop_num); | |
1262 | ||
1263 | for (i = 0; i < nhr->nexthop_num; i++) { | |
1264 | STREAM_GETL(s, nhr->nexthops[i].vrf_id); | |
1265 | STREAM_GETC(s, nhr->nexthops[i].type); | |
1266 | switch (nhr->nexthops[i].type) { | |
1267 | case NEXTHOP_TYPE_IPV4: | |
1268 | case NEXTHOP_TYPE_IPV4_IFINDEX: | |
1269 | STREAM_GET(&nhr->nexthops[i].gate.ipv4.s_addr, s, | |
1270 | IPV4_MAX_BYTELEN); | |
1271 | STREAM_GETL(s, nhr->nexthops[i].ifindex); | |
1272 | break; | |
1273 | case NEXTHOP_TYPE_IFINDEX: | |
1274 | STREAM_GETL(s, nhr->nexthops[i].ifindex); | |
1275 | break; | |
1276 | case NEXTHOP_TYPE_IPV6: | |
1277 | case NEXTHOP_TYPE_IPV6_IFINDEX: | |
1278 | STREAM_GET(&nhr->nexthops[i].gate.ipv6, s, | |
1279 | IPV6_MAX_BYTELEN); | |
1280 | STREAM_GETL(s, nhr->nexthops[i].ifindex); | |
1281 | break; | |
1282 | case NEXTHOP_TYPE_BLACKHOLE: | |
1283 | break; | |
1284 | } | |
1285 | STREAM_GETC(s, nhr->nexthops[i].label_num); | |
1286 | if (nhr->nexthops[i].label_num > MPLS_MAX_LABELS) { | |
1287 | flog_err(EC_LIB_ZAPI_ENCODE, | |
1288 | "%s: invalid number of MPLS labels (%u)", | |
1289 | __func__, nhr->nexthops[i].label_num); | |
1290 | return false; | |
1291 | } | |
1292 | if (nhr->nexthops[i].label_num) | |
1293 | STREAM_GET(&nhr->nexthops[i].labels[0], s, | |
1294 | nhr->nexthops[i].label_num | |
1295 | * sizeof(mpls_label_t)); | |
1296 | } | |
1297 | ||
1298 | return true; | |
1299 | stream_failure: | |
1300 | return false; | |
1301 | } | |
1302 | ||
1303 | /* | |
1304 | * send a ZEBRA_REDISTRIBUTE_ADD or ZEBRA_REDISTRIBUTE_DELETE | |
1305 | * for the route type (ZEBRA_ROUTE_KERNEL etc.). The zebra server will | |
1306 | * then set/unset redist[type] in the client handle (a struct zserv) for the | |
1307 | * sending client | |
1308 | */ | |
1309 | int zebra_redistribute_send(int command, struct zclient *zclient, afi_t afi, | |
1310 | int type, unsigned short instance, vrf_id_t vrf_id) | |
1311 | { | |
1312 | struct stream *s; | |
1313 | ||
1314 | s = zclient->obuf; | |
1315 | stream_reset(s); | |
1316 | ||
1317 | zclient_create_header(s, command, vrf_id); | |
1318 | stream_putc(s, afi); | |
1319 | stream_putc(s, type); | |
1320 | stream_putw(s, instance); | |
1321 | ||
1322 | stream_putw_at(s, 0, stream_get_endp(s)); | |
1323 | ||
1324 | return zclient_send_message(zclient); | |
1325 | } | |
1326 | ||
1327 | int zebra_redistribute_default_send(int command, struct zclient *zclient, | |
1328 | afi_t afi, vrf_id_t vrf_id) | |
1329 | { | |
1330 | struct stream *s; | |
1331 | ||
1332 | s = zclient->obuf; | |
1333 | stream_reset(s); | |
1334 | ||
1335 | zclient_create_header(s, command, vrf_id); | |
1336 | stream_putc(s, afi); | |
1337 | ||
1338 | stream_putw_at(s, 0, stream_get_endp(s)); | |
1339 | ||
1340 | return zclient_send_message(zclient); | |
1341 | } | |
1342 | ||
1343 | /* Get prefix in ZServ format; family should be filled in on prefix */ | |
1344 | static void zclient_stream_get_prefix(struct stream *s, struct prefix *p) | |
1345 | { | |
1346 | size_t plen = prefix_blen(p); | |
1347 | uint8_t c; | |
1348 | p->prefixlen = 0; | |
1349 | ||
1350 | if (plen == 0) | |
1351 | return; | |
1352 | ||
1353 | stream_get(&p->u.prefix, s, plen); | |
1354 | STREAM_GETC(s, c); | |
1355 | p->prefixlen = MIN(plen * 8, c); | |
1356 | ||
1357 | stream_failure: | |
1358 | return; | |
1359 | } | |
1360 | ||
1361 | /* Router-id update from zebra daemon. */ | |
1362 | void zebra_router_id_update_read(struct stream *s, struct prefix *rid) | |
1363 | { | |
1364 | /* Fetch interface address. */ | |
1365 | STREAM_GETC(s, rid->family); | |
1366 | ||
1367 | zclient_stream_get_prefix(s, rid); | |
1368 | ||
1369 | stream_failure: | |
1370 | return; | |
1371 | } | |
1372 | ||
1373 | /* Interface addition from zebra daemon. */ | |
1374 | /* | |
1375 | * The format of the message sent with type ZEBRA_INTERFACE_ADD or | |
1376 | * ZEBRA_INTERFACE_DELETE from zebra to the client is: | |
1377 | * 0 1 2 3 | |
1378 | * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 | |
1379 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
1380 | * | ifname | | |
1381 | * | | | |
1382 | * | | | |
1383 | * | | | |
1384 | * | | | |
1385 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
1386 | * | ifindex | | |
1387 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
1388 | * | status | | |
1389 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
1390 | * | if_flags | | |
1391 | * | | | |
1392 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
1393 | * | metric | | |
1394 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
1395 | * | speed | | |
1396 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
1397 | * | ifmtu | | |
1398 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
1399 | * | ifmtu6 | | |
1400 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
1401 | * | bandwidth | | |
1402 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
1403 | * | Link Layer Type | | |
1404 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
1405 | * | Harware Address Length | | |
1406 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
1407 | * | Hardware Address if HW lenght different from 0 | | |
1408 | * | ... max INTERFACE_HWADDR_MAX | | |
1409 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
1410 | * | Link_params? | Whether a link-params follows: 1 or 0. | |
1411 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
1412 | * | Link_params 0 or 1 INTERFACE_LINK_PARAMS_SIZE sized | | |
1413 | * | .... (struct if_link_params). | | |
1414 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ | |
1415 | */ | |
1416 | ||
1417 | static void zclient_vrf_add(struct zclient *zclient, vrf_id_t vrf_id) | |
1418 | { | |
1419 | struct vrf *vrf; | |
1420 | char vrfname_tmp[VRF_NAMSIZ]; | |
1421 | struct vrf_data data; | |
1422 | ||
1423 | stream_get(&data, zclient->ibuf, sizeof(struct vrf_data)); | |
1424 | /* Read interface name. */ | |
1425 | stream_get(vrfname_tmp, zclient->ibuf, VRF_NAMSIZ); | |
1426 | ||
1427 | /* Lookup/create vrf by vrf_id. */ | |
1428 | vrf = vrf_get(vrf_id, vrfname_tmp); | |
1429 | vrf->data.l.table_id = data.l.table_id; | |
1430 | memcpy(vrf->data.l.netns_name, data.l.netns_name, NS_NAMSIZ); | |
1431 | /* overwrite default vrf */ | |
1432 | if (vrf_id == VRF_DEFAULT) | |
1433 | vrf_set_default_name(vrfname_tmp, false); | |
1434 | vrf_enable(vrf); | |
1435 | } | |
1436 | ||
1437 | static void zclient_vrf_delete(struct zclient *zclient, vrf_id_t vrf_id) | |
1438 | { | |
1439 | struct vrf *vrf; | |
1440 | ||
1441 | /* Lookup vrf by vrf_id. */ | |
1442 | vrf = vrf_lookup_by_id(vrf_id); | |
1443 | ||
1444 | /* | |
1445 | * If a routing protocol doesn't know about a | |
1446 | * vrf that is about to be deleted. There is | |
1447 | * no point in attempting to delete it. | |
1448 | */ | |
1449 | if (!vrf) | |
1450 | return; | |
1451 | ||
1452 | vrf_delete(vrf); | |
1453 | } | |
1454 | ||
1455 | struct interface *zebra_interface_add_read(struct stream *s, vrf_id_t vrf_id) | |
1456 | { | |
1457 | struct interface *ifp; | |
1458 | char ifname_tmp[INTERFACE_NAMSIZ]; | |
1459 | ||
1460 | /* Read interface name. */ | |
1461 | stream_get(ifname_tmp, s, INTERFACE_NAMSIZ); | |
1462 | ||
1463 | /* Lookup/create interface by name. */ | |
1464 | ifp = if_get_by_name(ifname_tmp, vrf_id); | |
1465 | ||
1466 | zebra_interface_if_set_value(s, ifp); | |
1467 | ||
1468 | return ifp; | |
1469 | } | |
1470 | ||
1471 | /* | |
1472 | * Read interface up/down msg (ZEBRA_INTERFACE_UP/ZEBRA_INTERFACE_DOWN) | |
1473 | * from zebra server. The format of this message is the same as | |
1474 | * that sent for ZEBRA_INTERFACE_ADD/ZEBRA_INTERFACE_DELETE (see | |
1475 | * comments for zebra_interface_add_read), except that no sockaddr_dl | |
1476 | * is sent at the tail of the message. | |
1477 | */ | |
1478 | struct interface *zebra_interface_state_read(struct stream *s, vrf_id_t vrf_id) | |
1479 | { | |
1480 | struct interface *ifp; | |
1481 | char ifname_tmp[INTERFACE_NAMSIZ]; | |
1482 | ||
1483 | /* Read interface name. */ | |
1484 | stream_get(ifname_tmp, s, INTERFACE_NAMSIZ); | |
1485 | ||
1486 | /* Lookup this by interface index. */ | |
1487 | ifp = if_lookup_by_name(ifname_tmp, vrf_id); | |
1488 | if (ifp == NULL) { | |
1489 | flog_err(EC_LIB_ZAPI_ENCODE, | |
1490 | "INTERFACE_STATE: Cannot find IF %s in VRF %d", | |
1491 | ifname_tmp, vrf_id); | |
1492 | return NULL; | |
1493 | } | |
1494 | ||
1495 | zebra_interface_if_set_value(s, ifp); | |
1496 | ||
1497 | return ifp; | |
1498 | } | |
1499 | ||
1500 | static void link_params_set_value(struct stream *s, struct if_link_params *iflp) | |
1501 | { | |
1502 | ||
1503 | if (iflp == NULL) | |
1504 | return; | |
1505 | ||
1506 | iflp->lp_status = stream_getl(s); | |
1507 | iflp->te_metric = stream_getl(s); | |
1508 | iflp->max_bw = stream_getf(s); | |
1509 | iflp->max_rsv_bw = stream_getf(s); | |
1510 | uint32_t bwclassnum = stream_getl(s); | |
1511 | { | |
1512 | unsigned int i; | |
1513 | for (i = 0; i < bwclassnum && i < MAX_CLASS_TYPE; i++) | |
1514 | iflp->unrsv_bw[i] = stream_getf(s); | |
1515 | if (i < bwclassnum) | |
1516 | flog_err( | |
1517 | EC_LIB_ZAPI_MISSMATCH, | |
1518 | "%s: received %d > %d (MAX_CLASS_TYPE) bw entries" | |
1519 | " - outdated library?", | |
1520 | __func__, bwclassnum, MAX_CLASS_TYPE); | |
1521 | } | |
1522 | iflp->admin_grp = stream_getl(s); | |
1523 | iflp->rmt_as = stream_getl(s); | |
1524 | iflp->rmt_ip.s_addr = stream_get_ipv4(s); | |
1525 | ||
1526 | iflp->av_delay = stream_getl(s); | |
1527 | iflp->min_delay = stream_getl(s); | |
1528 | iflp->max_delay = stream_getl(s); | |
1529 | iflp->delay_var = stream_getl(s); | |
1530 | ||
1531 | iflp->pkt_loss = stream_getf(s); | |
1532 | iflp->res_bw = stream_getf(s); | |
1533 | iflp->ava_bw = stream_getf(s); | |
1534 | iflp->use_bw = stream_getf(s); | |
1535 | } | |
1536 | ||
1537 | struct interface *zebra_interface_link_params_read(struct stream *s, | |
1538 | vrf_id_t vrf_id) | |
1539 | { | |
1540 | struct if_link_params *iflp; | |
1541 | ifindex_t ifindex; | |
1542 | ||
1543 | assert(s); | |
1544 | ||
1545 | ifindex = stream_getl(s); | |
1546 | ||
1547 | struct interface *ifp = if_lookup_by_index(ifindex, vrf_id); | |
1548 | ||
1549 | if (ifp == NULL) { | |
1550 | flog_err(EC_LIB_ZAPI_ENCODE, | |
1551 | "%s: unknown ifindex %u, shouldn't happen", __func__, | |
1552 | ifindex); | |
1553 | return NULL; | |
1554 | } | |
1555 | ||
1556 | if ((iflp = if_link_params_get(ifp)) == NULL) | |
1557 | return NULL; | |
1558 | ||
1559 | link_params_set_value(s, iflp); | |
1560 | ||
1561 | return ifp; | |
1562 | } | |
1563 | ||
1564 | void zebra_interface_if_set_value(struct stream *s, struct interface *ifp) | |
1565 | { | |
1566 | uint8_t link_params_status = 0; | |
1567 | ifindex_t old_ifindex; | |
1568 | ||
1569 | old_ifindex = ifp->ifindex; | |
1570 | /* Read interface's index. */ | |
1571 | if_set_index(ifp, stream_getl(s)); | |
1572 | ifp->status = stream_getc(s); | |
1573 | ||
1574 | /* Read interface's value. */ | |
1575 | ifp->flags = stream_getq(s); | |
1576 | ifp->ptm_enable = stream_getc(s); | |
1577 | ifp->ptm_status = stream_getc(s); | |
1578 | ifp->metric = stream_getl(s); | |
1579 | ifp->speed = stream_getl(s); | |
1580 | ifp->mtu = stream_getl(s); | |
1581 | ifp->mtu6 = stream_getl(s); | |
1582 | ifp->bandwidth = stream_getl(s); | |
1583 | ifp->ll_type = stream_getl(s); | |
1584 | ifp->hw_addr_len = stream_getl(s); | |
1585 | if (ifp->hw_addr_len) | |
1586 | stream_get(ifp->hw_addr, s, | |
1587 | MIN(ifp->hw_addr_len, INTERFACE_HWADDR_MAX)); | |
1588 | ||
1589 | /* Read Traffic Engineering status */ | |
1590 | link_params_status = stream_getc(s); | |
1591 | /* Then, Traffic Engineering parameters if any */ | |
1592 | if (link_params_status) { | |
1593 | struct if_link_params *iflp = if_link_params_get(ifp); | |
1594 | link_params_set_value(s, iflp); | |
1595 | } | |
1596 | ||
1597 | nexthop_group_interface_state_change(ifp, old_ifindex); | |
1598 | } | |
1599 | ||
1600 | size_t zebra_interface_link_params_write(struct stream *s, | |
1601 | struct interface *ifp) | |
1602 | { | |
1603 | size_t w; | |
1604 | struct if_link_params *iflp; | |
1605 | int i; | |
1606 | ||
1607 | if (s == NULL || ifp == NULL || ifp->link_params == NULL) | |
1608 | return 0; | |
1609 | ||
1610 | iflp = ifp->link_params; | |
1611 | w = 0; | |
1612 | ||
1613 | w += stream_putl(s, iflp->lp_status); | |
1614 | ||
1615 | w += stream_putl(s, iflp->te_metric); | |
1616 | w += stream_putf(s, iflp->max_bw); | |
1617 | w += stream_putf(s, iflp->max_rsv_bw); | |
1618 | ||
1619 | w += stream_putl(s, MAX_CLASS_TYPE); | |
1620 | for (i = 0; i < MAX_CLASS_TYPE; i++) | |
1621 | w += stream_putf(s, iflp->unrsv_bw[i]); | |
1622 | ||
1623 | w += stream_putl(s, iflp->admin_grp); | |
1624 | w += stream_putl(s, iflp->rmt_as); | |
1625 | w += stream_put_in_addr(s, &iflp->rmt_ip); | |
1626 | ||
1627 | w += stream_putl(s, iflp->av_delay); | |
1628 | w += stream_putl(s, iflp->min_delay); | |
1629 | w += stream_putl(s, iflp->max_delay); | |
1630 | w += stream_putl(s, iflp->delay_var); | |
1631 | ||
1632 | w += stream_putf(s, iflp->pkt_loss); | |
1633 | w += stream_putf(s, iflp->res_bw); | |
1634 | w += stream_putf(s, iflp->ava_bw); | |
1635 | w += stream_putf(s, iflp->use_bw); | |
1636 | ||
1637 | return w; | |
1638 | } | |
1639 | ||
1640 | /* | |
1641 | * format of message for address additon is: | |
1642 | * 0 | |
1643 | * 0 1 2 3 4 5 6 7 | |
1644 | * +-+-+-+-+-+-+-+-+ | |
1645 | * | type | ZEBRA_INTERFACE_ADDRESS_ADD or | |
1646 | * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_ADDRES_DELETE | |
1647 | * | | | |
1648 | * + + | |
1649 | * | ifindex | | |
1650 | * + + | |
1651 | * | | | |
1652 | * + + | |
1653 | * | | | |
1654 | * +-+-+-+-+-+-+-+-+ | |
1655 | * | ifc_flags | flags for connected address | |
1656 | * +-+-+-+-+-+-+-+-+ | |
1657 | * | addr_family | | |
1658 | * +-+-+-+-+-+-+-+-+ | |
1659 | * | addr... | | |
1660 | * : : | |
1661 | * | | | |
1662 | * +-+-+-+-+-+-+-+-+ | |
1663 | * | addr_len | len of addr. E.g., addr_len = 4 for ipv4 addrs. | |
1664 | * +-+-+-+-+-+-+-+-+ | |
1665 | * | daddr.. | | |
1666 | * : : | |
1667 | * | | | |
1668 | * +-+-+-+-+-+-+-+-+ | |
1669 | */ | |
1670 | ||
1671 | static int memconstant(const void *s, int c, size_t n) | |
1672 | { | |
1673 | const uint8_t *p = s; | |
1674 | ||
1675 | while (n-- > 0) | |
1676 | if (*p++ != c) | |
1677 | return 0; | |
1678 | return 1; | |
1679 | } | |
1680 | ||
1681 | ||
1682 | struct connected *zebra_interface_address_read(int type, struct stream *s, | |
1683 | vrf_id_t vrf_id) | |
1684 | { | |
1685 | ifindex_t ifindex; | |
1686 | struct interface *ifp; | |
1687 | struct connected *ifc; | |
1688 | struct prefix p, d, *dp; | |
1689 | int plen; | |
1690 | uint8_t ifc_flags; | |
1691 | ||
1692 | memset(&p, 0, sizeof(p)); | |
1693 | memset(&d, 0, sizeof(d)); | |
1694 | ||
1695 | /* Get interface index. */ | |
1696 | ifindex = stream_getl(s); | |
1697 | ||
1698 | /* Lookup index. */ | |
1699 | ifp = if_lookup_by_index(ifindex, vrf_id); | |
1700 | if (ifp == NULL) { | |
1701 | flog_err(EC_LIB_ZAPI_ENCODE, | |
1702 | "INTERFACE_ADDRESS_%s: Cannot find IF %u in VRF %d", | |
1703 | (type == ZEBRA_INTERFACE_ADDRESS_ADD) ? "ADD" : "DEL", | |
1704 | ifindex, vrf_id); | |
1705 | return NULL; | |
1706 | } | |
1707 | ||
1708 | /* Fetch flag. */ | |
1709 | ifc_flags = stream_getc(s); | |
1710 | ||
1711 | /* Fetch interface address. */ | |
1712 | d.family = p.family = stream_getc(s); | |
1713 | plen = prefix_blen(&d); | |
1714 | ||
1715 | zclient_stream_get_prefix(s, &p); | |
1716 | ||
1717 | /* Fetch destination address. */ | |
1718 | stream_get(&d.u.prefix, s, plen); | |
1719 | ||
1720 | /* N.B. NULL destination pointers are encoded as all zeroes */ | |
1721 | dp = memconstant(&d.u.prefix, 0, plen) ? NULL : &d; | |
1722 | ||
1723 | if (type == ZEBRA_INTERFACE_ADDRESS_ADD) { | |
1724 | ifc = connected_lookup_prefix_exact(ifp, &p); | |
1725 | if (!ifc) { | |
1726 | /* N.B. NULL destination pointers are encoded as all | |
1727 | * zeroes */ | |
1728 | ifc = connected_add_by_prefix(ifp, &p, dp); | |
1729 | } | |
1730 | if (ifc) { | |
1731 | ifc->flags = ifc_flags; | |
1732 | if (ifc->destination) | |
1733 | ifc->destination->prefixlen = | |
1734 | ifc->address->prefixlen; | |
1735 | else if (CHECK_FLAG(ifc->flags, ZEBRA_IFA_PEER)) { | |
1736 | /* carp interfaces on OpenBSD with 0.0.0.0/0 as | |
1737 | * "peer" */ | |
1738 | char buf[PREFIX_STRLEN]; | |
1739 | flog_err( | |
1740 | EC_LIB_ZAPI_ENCODE, | |
1741 | "warning: interface %s address %s with peer flag set, but no peer address!", | |
1742 | ifp->name, | |
1743 | prefix2str(ifc->address, buf, | |
1744 | sizeof buf)); | |
1745 | UNSET_FLAG(ifc->flags, ZEBRA_IFA_PEER); | |
1746 | } | |
1747 | } | |
1748 | } else { | |
1749 | assert(type == ZEBRA_INTERFACE_ADDRESS_DELETE); | |
1750 | ifc = connected_delete_by_prefix(ifp, &p); | |
1751 | } | |
1752 | ||
1753 | return ifc; | |
1754 | } | |
1755 | ||
1756 | /* | |
1757 | * format of message for neighbor connected address is: | |
1758 | * 0 | |
1759 | * 0 1 2 3 4 5 6 7 | |
1760 | * +-+-+-+-+-+-+-+-+ | |
1761 | * | type | ZEBRA_INTERFACE_NBR_ADDRESS_ADD or | |
1762 | * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_NBR_ADDRES_DELETE | |
1763 | * | | | |
1764 | * + + | |
1765 | * | ifindex | | |
1766 | * + + | |
1767 | * | | | |
1768 | * + + | |
1769 | * | | | |
1770 | * +-+-+-+-+-+-+-+-+ | |
1771 | * | addr_family | | |
1772 | * +-+-+-+-+-+-+-+-+ | |
1773 | * | addr... | | |
1774 | * : : | |
1775 | * | | | |
1776 | * +-+-+-+-+-+-+-+-+ | |
1777 | * | addr_len | len of addr. | |
1778 | * +-+-+-+-+-+-+-+-+ | |
1779 | */ | |
1780 | struct nbr_connected * | |
1781 | zebra_interface_nbr_address_read(int type, struct stream *s, vrf_id_t vrf_id) | |
1782 | { | |
1783 | unsigned int ifindex; | |
1784 | struct interface *ifp; | |
1785 | struct prefix p; | |
1786 | struct nbr_connected *ifc; | |
1787 | ||
1788 | /* Get interface index. */ | |
1789 | ifindex = stream_getl(s); | |
1790 | ||
1791 | /* Lookup index. */ | |
1792 | ifp = if_lookup_by_index(ifindex, vrf_id); | |
1793 | if (ifp == NULL) { | |
1794 | flog_err(EC_LIB_ZAPI_ENCODE, | |
1795 | "INTERFACE_NBR_%s: Cannot find IF %u in VRF %d", | |
1796 | (type == ZEBRA_INTERFACE_NBR_ADDRESS_ADD) ? "ADD" | |
1797 | : "DELETE", | |
1798 | ifindex, vrf_id); | |
1799 | return NULL; | |
1800 | } | |
1801 | ||
1802 | p.family = stream_getc(s); | |
1803 | stream_get(&p.u.prefix, s, prefix_blen(&p)); | |
1804 | p.prefixlen = stream_getc(s); | |
1805 | ||
1806 | if (type == ZEBRA_INTERFACE_NBR_ADDRESS_ADD) { | |
1807 | /* Currently only supporting P2P links, so any new RA source | |
1808 | address is | |
1809 | considered as the replacement of the previously learnt | |
1810 | Link-Local address. */ | |
1811 | if (!(ifc = listnode_head(ifp->nbr_connected))) { | |
1812 | ifc = nbr_connected_new(); | |
1813 | ifc->address = prefix_new(); | |
1814 | ifc->ifp = ifp; | |
1815 | listnode_add(ifp->nbr_connected, ifc); | |
1816 | } | |
1817 | ||
1818 | prefix_copy(ifc->address, &p); | |
1819 | } else { | |
1820 | assert(type == ZEBRA_INTERFACE_NBR_ADDRESS_DELETE); | |
1821 | ||
1822 | ifc = nbr_connected_check(ifp, &p); | |
1823 | if (ifc) | |
1824 | listnode_delete(ifp->nbr_connected, ifc); | |
1825 | } | |
1826 | ||
1827 | return ifc; | |
1828 | } | |
1829 | ||
1830 | struct interface *zebra_interface_vrf_update_read(struct stream *s, | |
1831 | vrf_id_t vrf_id, | |
1832 | vrf_id_t *new_vrf_id) | |
1833 | { | |
1834 | char ifname[INTERFACE_NAMSIZ]; | |
1835 | struct interface *ifp; | |
1836 | vrf_id_t new_id; | |
1837 | ||
1838 | /* Read interface name. */ | |
1839 | stream_get(ifname, s, INTERFACE_NAMSIZ); | |
1840 | ||
1841 | /* Lookup interface. */ | |
1842 | ifp = if_lookup_by_name(ifname, vrf_id); | |
1843 | if (ifp == NULL) { | |
1844 | flog_err(EC_LIB_ZAPI_ENCODE, | |
1845 | "INTERFACE_VRF_UPDATE: Cannot find IF %s in VRF %d", | |
1846 | ifname, vrf_id); | |
1847 | return NULL; | |
1848 | } | |
1849 | ||
1850 | /* Fetch new VRF Id. */ | |
1851 | new_id = stream_getw(s); | |
1852 | ||
1853 | *new_vrf_id = new_id; | |
1854 | return ifp; | |
1855 | } | |
1856 | ||
1857 | /* filter unwanted messages until the expected one arrives */ | |
1858 | static int zclient_read_sync_response(struct zclient *zclient, | |
1859 | uint16_t expected_cmd) | |
1860 | { | |
1861 | struct stream *s; | |
1862 | uint16_t size = -1; | |
1863 | uint8_t marker; | |
1864 | uint8_t version; | |
1865 | vrf_id_t vrf_id; | |
1866 | uint16_t cmd; | |
1867 | fd_set readfds; | |
1868 | int ret; | |
1869 | ||
1870 | ret = 0; | |
1871 | cmd = expected_cmd + 1; | |
1872 | while (ret == 0 && cmd != expected_cmd) { | |
1873 | s = zclient->ibuf; | |
1874 | stream_reset(s); | |
1875 | ||
1876 | /* wait until response arrives */ | |
1877 | FD_ZERO(&readfds); | |
1878 | FD_SET(zclient->sock, &readfds); | |
1879 | select(zclient->sock + 1, &readfds, NULL, NULL, NULL); | |
1880 | if (!FD_ISSET(zclient->sock, &readfds)) | |
1881 | continue; | |
1882 | /* read response */ | |
1883 | ret = zclient_read_header(s, zclient->sock, &size, &marker, | |
1884 | &version, &vrf_id, &cmd); | |
1885 | if (zclient_debug) | |
1886 | zlog_debug("%s: Response (%d bytes) received", __func__, | |
1887 | size); | |
1888 | } | |
1889 | if (ret != 0) { | |
1890 | flog_err(EC_LIB_ZAPI_ENCODE, "%s: Invalid Sync Message Reply", | |
1891 | __func__); | |
1892 | return -1; | |
1893 | } | |
1894 | ||
1895 | return 0; | |
1896 | } | |
1897 | /** | |
1898 | * Connect to label manager in a syncronous way | |
1899 | * | |
1900 | * It first writes the request to zcient output buffer and then | |
1901 | * immediately reads the answer from the input buffer. | |
1902 | * | |
1903 | * @param zclient Zclient used to connect to label manager (zebra) | |
1904 | * @param async Synchronous (0) or asynchronous (1) operation | |
1905 | * @result Result of response | |
1906 | */ | |
1907 | int lm_label_manager_connect(struct zclient *zclient, int async) | |
1908 | { | |
1909 | int ret; | |
1910 | struct stream *s; | |
1911 | uint8_t result; | |
1912 | uint16_t cmd = async ? ZEBRA_LABEL_MANAGER_CONNECT_ASYNC : | |
1913 | ZEBRA_LABEL_MANAGER_CONNECT; | |
1914 | ||
1915 | if (zclient_debug) | |
1916 | zlog_debug("Connecting to Label Manager (LM)"); | |
1917 | ||
1918 | if (zclient->sock < 0) { | |
1919 | zlog_debug("%s: invalid zclient socket", __func__); | |
1920 | return -1; | |
1921 | } | |
1922 | ||
1923 | /* send request */ | |
1924 | s = zclient->obuf; | |
1925 | stream_reset(s); | |
1926 | zclient_create_header(s, cmd, VRF_DEFAULT); | |
1927 | ||
1928 | /* proto */ | |
1929 | stream_putc(s, zclient->redist_default); | |
1930 | /* instance */ | |
1931 | stream_putw(s, zclient->instance); | |
1932 | ||
1933 | /* Put length at the first point of the stream. */ | |
1934 | stream_putw_at(s, 0, stream_get_endp(s)); | |
1935 | ||
1936 | ret = writen(zclient->sock, s->data, stream_get_endp(s)); | |
1937 | if (ret < 0) { | |
1938 | flog_err(EC_LIB_ZAPI_SOCKET, "Can't write to zclient sock"); | |
1939 | close(zclient->sock); | |
1940 | zclient->sock = -1; | |
1941 | return -1; | |
1942 | } | |
1943 | if (ret == 0) { | |
1944 | flog_err(EC_LIB_ZAPI_SOCKET, "Zclient sock closed"); | |
1945 | close(zclient->sock); | |
1946 | zclient->sock = -1; | |
1947 | return -1; | |
1948 | } | |
1949 | if (zclient_debug) | |
1950 | zlog_debug("LM connect request sent (%d bytes)", ret); | |
1951 | ||
1952 | if (async) | |
1953 | return 0; | |
1954 | ||
1955 | /* read response */ | |
1956 | if (zclient_read_sync_response(zclient, cmd) | |
1957 | != 0) | |
1958 | return -1; | |
1959 | ||
1960 | s = zclient->ibuf; | |
1961 | ||
1962 | /* read instance and proto */ | |
1963 | uint8_t proto = stream_getc(s); | |
1964 | uint16_t instance = stream_getw(s); | |
1965 | ||
1966 | /* sanity */ | |
1967 | if (proto != zclient->redist_default) | |
1968 | flog_err( | |
1969 | EC_LIB_ZAPI_ENCODE, | |
1970 | "Wrong proto (%u) in LM connect response. Should be %u", | |
1971 | proto, zclient->redist_default); | |
1972 | if (instance != zclient->instance) | |
1973 | flog_err( | |
1974 | EC_LIB_ZAPI_ENCODE, | |
1975 | "Wrong instId (%u) in LM connect response. Should be %u", | |
1976 | instance, zclient->instance); | |
1977 | ||
1978 | /* result code */ | |
1979 | result = stream_getc(s); | |
1980 | if (zclient_debug) | |
1981 | zlog_debug("LM connect-response received, result %u", result); | |
1982 | ||
1983 | return (int)result; | |
1984 | } | |
1985 | ||
1986 | /* | |
1987 | * Asynchronous label chunk request | |
1988 | * | |
1989 | * @param zclient Zclient used to connect to label manager (zebra) | |
1990 | * @param keep Avoid garbage collection | |
1991 | * @param chunk_size Amount of labels requested | |
1992 | * @result 0 on success, -1 otherwise | |
1993 | */ | |
1994 | int zclient_send_get_label_chunk(struct zclient *zclient, uint8_t keep, | |
1995 | uint32_t chunk_size) | |
1996 | { | |
1997 | struct stream *s; | |
1998 | ||
1999 | if (zclient_debug) | |
2000 | zlog_debug("Getting Label Chunk"); | |
2001 | ||
2002 | if (zclient->sock < 0) | |
2003 | return -1; | |
2004 | ||
2005 | s = zclient->obuf; | |
2006 | stream_reset(s); | |
2007 | ||
2008 | zclient_create_header(s, ZEBRA_GET_LABEL_CHUNK, VRF_DEFAULT); | |
2009 | /* proto */ | |
2010 | stream_putc(s, zclient->redist_default); | |
2011 | /* instance */ | |
2012 | stream_putw(s, zclient->instance); | |
2013 | stream_putc(s, keep); | |
2014 | stream_putl(s, chunk_size); | |
2015 | ||
2016 | /* Put length at the first point of the stream. */ | |
2017 | stream_putw_at(s, 0, stream_get_endp(s)); | |
2018 | ||
2019 | return zclient_send_message(zclient); | |
2020 | } | |
2021 | ||
2022 | /** | |
2023 | * Function to request a label chunk in a syncronous way | |
2024 | * | |
2025 | * It first writes the request to zlcient output buffer and then | |
2026 | * immediately reads the answer from the input buffer. | |
2027 | * | |
2028 | * @param zclient Zclient used to connect to label manager (zebra) | |
2029 | * @param keep Avoid garbage collection | |
2030 | * @param chunk_size Amount of labels requested | |
2031 | * @param start To write first assigned chunk label to | |
2032 | * @param end To write last assigned chunk label to | |
2033 | * @result 0 on success, -1 otherwise | |
2034 | */ | |
2035 | int lm_get_label_chunk(struct zclient *zclient, uint8_t keep, | |
2036 | uint32_t chunk_size, uint32_t *start, uint32_t *end) | |
2037 | { | |
2038 | int ret; | |
2039 | struct stream *s; | |
2040 | uint8_t response_keep; | |
2041 | ||
2042 | if (zclient_debug) | |
2043 | zlog_debug("Getting Label Chunk"); | |
2044 | ||
2045 | if (zclient->sock < 0) | |
2046 | return -1; | |
2047 | ||
2048 | /* send request */ | |
2049 | s = zclient->obuf; | |
2050 | stream_reset(s); | |
2051 | zclient_create_header(s, ZEBRA_GET_LABEL_CHUNK, VRF_DEFAULT); | |
2052 | /* proto */ | |
2053 | stream_putc(s, zclient->redist_default); | |
2054 | /* instance */ | |
2055 | stream_putw(s, zclient->instance); | |
2056 | /* keep */ | |
2057 | stream_putc(s, keep); | |
2058 | /* chunk size */ | |
2059 | stream_putl(s, chunk_size); | |
2060 | /* Put length at the first point of the stream. */ | |
2061 | stream_putw_at(s, 0, stream_get_endp(s)); | |
2062 | ||
2063 | ret = writen(zclient->sock, s->data, stream_get_endp(s)); | |
2064 | if (ret < 0) { | |
2065 | flog_err(EC_LIB_ZAPI_SOCKET, "Can't write to zclient sock"); | |
2066 | close(zclient->sock); | |
2067 | zclient->sock = -1; | |
2068 | return -1; | |
2069 | } | |
2070 | if (ret == 0) { | |
2071 | flog_err(EC_LIB_ZAPI_SOCKET, "Zclient sock closed"); | |
2072 | close(zclient->sock); | |
2073 | zclient->sock = -1; | |
2074 | return -1; | |
2075 | } | |
2076 | if (zclient_debug) | |
2077 | zlog_debug("Label chunk request (%d bytes) sent", ret); | |
2078 | ||
2079 | /* read response */ | |
2080 | if (zclient_read_sync_response(zclient, ZEBRA_GET_LABEL_CHUNK) != 0) | |
2081 | return -1; | |
2082 | ||
2083 | /* parse response */ | |
2084 | s = zclient->ibuf; | |
2085 | ||
2086 | /* read proto and instance */ | |
2087 | uint8_t proto = stream_getc(s); | |
2088 | uint16_t instance = stream_getw(s); | |
2089 | ||
2090 | /* sanities */ | |
2091 | if (proto != zclient->redist_default) | |
2092 | flog_err(EC_LIB_ZAPI_ENCODE, | |
2093 | "Wrong proto (%u) in get chunk response. Should be %u", | |
2094 | proto, zclient->redist_default); | |
2095 | if (instance != zclient->instance) | |
2096 | flog_err(EC_LIB_ZAPI_ENCODE, | |
2097 | "Wrong instId (%u) in get chunk response Should be %u", | |
2098 | instance, zclient->instance); | |
2099 | ||
2100 | /* keep */ | |
2101 | response_keep = stream_getc(s); | |
2102 | /* start and end labels */ | |
2103 | *start = stream_getl(s); | |
2104 | *end = stream_getl(s); | |
2105 | ||
2106 | /* not owning this response */ | |
2107 | if (keep != response_keep) { | |
2108 | flog_err( | |
2109 | EC_LIB_ZAPI_ENCODE, | |
2110 | "Invalid Label chunk: %u - %u, keeps mismatch %u != %u", | |
2111 | *start, *end, keep, response_keep); | |
2112 | } | |
2113 | /* sanity */ | |
2114 | if (*start > *end || *start < MPLS_LABEL_UNRESERVED_MIN | |
2115 | || *end > MPLS_LABEL_UNRESERVED_MAX) { | |
2116 | flog_err(EC_LIB_ZAPI_ENCODE, "Invalid Label chunk: %u - %u", | |
2117 | *start, *end); | |
2118 | return -1; | |
2119 | } | |
2120 | ||
2121 | if (zclient_debug) | |
2122 | zlog_debug("Label Chunk assign: %u - %u (%u)", *start, *end, | |
2123 | response_keep); | |
2124 | ||
2125 | return 0; | |
2126 | } | |
2127 | ||
2128 | /** | |
2129 | * Function to release a label chunk | |
2130 | * | |
2131 | * @param zclient Zclient used to connect to label manager (zebra) | |
2132 | * @param start First label of chunk | |
2133 | * @param end Last label of chunk | |
2134 | * @result 0 on success, -1 otherwise | |
2135 | */ | |
2136 | int lm_release_label_chunk(struct zclient *zclient, uint32_t start, | |
2137 | uint32_t end) | |
2138 | { | |
2139 | int ret; | |
2140 | struct stream *s; | |
2141 | ||
2142 | if (zclient_debug) | |
2143 | zlog_debug("Releasing Label Chunk %u - %u", start, end); | |
2144 | ||
2145 | if (zclient->sock < 0) | |
2146 | return -1; | |
2147 | ||
2148 | /* send request */ | |
2149 | s = zclient->obuf; | |
2150 | stream_reset(s); | |
2151 | zclient_create_header(s, ZEBRA_RELEASE_LABEL_CHUNK, VRF_DEFAULT); | |
2152 | ||
2153 | /* proto */ | |
2154 | stream_putc(s, zclient->redist_default); | |
2155 | /* instance */ | |
2156 | stream_putw(s, zclient->instance); | |
2157 | /* start */ | |
2158 | stream_putl(s, start); | |
2159 | /* end */ | |
2160 | stream_putl(s, end); | |
2161 | ||
2162 | /* Put length at the first point of the stream. */ | |
2163 | stream_putw_at(s, 0, stream_get_endp(s)); | |
2164 | ||
2165 | ret = writen(zclient->sock, s->data, stream_get_endp(s)); | |
2166 | if (ret < 0) { | |
2167 | flog_err(EC_LIB_ZAPI_SOCKET, "Can't write to zclient sock"); | |
2168 | close(zclient->sock); | |
2169 | zclient->sock = -1; | |
2170 | return -1; | |
2171 | } | |
2172 | if (ret == 0) { | |
2173 | flog_err(EC_LIB_ZAPI_SOCKET, "Zclient sock connection closed"); | |
2174 | close(zclient->sock); | |
2175 | zclient->sock = -1; | |
2176 | return -1; | |
2177 | } | |
2178 | ||
2179 | return 0; | |
2180 | } | |
2181 | ||
2182 | /** | |
2183 | * Connect to table manager in a syncronous way | |
2184 | * | |
2185 | * It first writes the request to zcient output buffer and then | |
2186 | * immediately reads the answer from the input buffer. | |
2187 | * | |
2188 | * @param zclient Zclient used to connect to table manager (zebra) | |
2189 | * @result Result of response | |
2190 | */ | |
2191 | int tm_table_manager_connect(struct zclient *zclient) | |
2192 | { | |
2193 | int ret; | |
2194 | struct stream *s; | |
2195 | uint8_t result; | |
2196 | ||
2197 | if (zclient_debug) | |
2198 | zlog_debug("Connecting to Table Manager"); | |
2199 | ||
2200 | if (zclient->sock < 0) | |
2201 | return -1; | |
2202 | ||
2203 | /* send request */ | |
2204 | s = zclient->obuf; | |
2205 | stream_reset(s); | |
2206 | zclient_create_header(s, ZEBRA_TABLE_MANAGER_CONNECT, VRF_DEFAULT); | |
2207 | ||
2208 | /* proto */ | |
2209 | stream_putc(s, zclient->redist_default); | |
2210 | /* instance */ | |
2211 | stream_putw(s, zclient->instance); | |
2212 | ||
2213 | /* Put length at the first point of the stream. */ | |
2214 | stream_putw_at(s, 0, stream_get_endp(s)); | |
2215 | ||
2216 | ret = zclient_send_message(zclient); | |
2217 | if (ret < 0) | |
2218 | return -1; | |
2219 | ||
2220 | if (zclient_debug) | |
2221 | zlog_debug("%s: Table manager connect request sent", __func__); | |
2222 | ||
2223 | /* read response */ | |
2224 | if (zclient_read_sync_response(zclient, ZEBRA_TABLE_MANAGER_CONNECT) | |
2225 | != 0) | |
2226 | return -1; | |
2227 | ||
2228 | /* result */ | |
2229 | s = zclient->ibuf; | |
2230 | STREAM_GETC(s, result); | |
2231 | if (zclient_debug) | |
2232 | zlog_debug( | |
2233 | "%s: Table Manager connect response received, result %u", | |
2234 | __func__, result); | |
2235 | ||
2236 | return (int)result; | |
2237 | stream_failure: | |
2238 | return 0; | |
2239 | } | |
2240 | ||
2241 | /** | |
2242 | * Function to request a table chunk in a syncronous way | |
2243 | * | |
2244 | * It first writes the request to zclient output buffer and then | |
2245 | * immediately reads the answer from the input buffer. | |
2246 | * | |
2247 | * @param zclient Zclient used to connect to table manager (zebra) | |
2248 | * @param chunk_size Amount of table requested | |
2249 | * @param start to write first assigned chunk table RT ID to | |
2250 | * @param end To write last assigned chunk table RT ID to | |
2251 | * @result 0 on success, -1 otherwise | |
2252 | */ | |
2253 | int tm_get_table_chunk(struct zclient *zclient, uint32_t chunk_size, | |
2254 | uint32_t *start, uint32_t *end) | |
2255 | { | |
2256 | int ret; | |
2257 | struct stream *s; | |
2258 | ||
2259 | if (zclient_debug) | |
2260 | zlog_debug("Getting Table Chunk"); | |
2261 | ||
2262 | if (zclient->sock < 0) | |
2263 | return -1; | |
2264 | ||
2265 | /* send request */ | |
2266 | s = zclient->obuf; | |
2267 | stream_reset(s); | |
2268 | zclient_create_header(s, ZEBRA_GET_TABLE_CHUNK, VRF_DEFAULT); | |
2269 | /* chunk size */ | |
2270 | stream_putl(s, chunk_size); | |
2271 | /* Put length at the first point of the stream. */ | |
2272 | stream_putw_at(s, 0, stream_get_endp(s)); | |
2273 | ||
2274 | ret = writen(zclient->sock, s->data, stream_get_endp(s)); | |
2275 | if (ret < 0) { | |
2276 | flog_err(EC_LIB_ZAPI_SOCKET, "%s: can't write to zclient->sock", | |
2277 | __func__); | |
2278 | close(zclient->sock); | |
2279 | zclient->sock = -1; | |
2280 | return -1; | |
2281 | } | |
2282 | if (ret == 0) { | |
2283 | flog_err(EC_LIB_ZAPI_SOCKET, | |
2284 | "%s: zclient->sock connection closed", __func__); | |
2285 | close(zclient->sock); | |
2286 | zclient->sock = -1; | |
2287 | return -1; | |
2288 | } | |
2289 | if (zclient_debug) | |
2290 | zlog_debug("%s: Table chunk request (%d bytes) sent", __func__, | |
2291 | ret); | |
2292 | ||
2293 | /* read response */ | |
2294 | if (zclient_read_sync_response(zclient, ZEBRA_GET_TABLE_CHUNK) != 0) | |
2295 | return -1; | |
2296 | ||
2297 | s = zclient->ibuf; | |
2298 | /* start and end table IDs */ | |
2299 | STREAM_GETL(s, *start); | |
2300 | STREAM_GETL(s, *end); | |
2301 | ||
2302 | if (zclient_debug) | |
2303 | zlog_debug("Table Chunk assign: %u - %u ", *start, *end); | |
2304 | ||
2305 | stream_failure: | |
2306 | return 0; | |
2307 | } | |
2308 | ||
2309 | /** | |
2310 | * Function to release a table chunk | |
2311 | * | |
2312 | * @param zclient Zclient used to connect to table manager (zebra) | |
2313 | * @param start First label of table | |
2314 | * @param end Last label of chunk | |
2315 | * @result 0 on success, -1 otherwise | |
2316 | */ | |
2317 | int tm_release_table_chunk(struct zclient *zclient, uint32_t start, | |
2318 | uint32_t end) | |
2319 | { | |
2320 | struct stream *s; | |
2321 | ||
2322 | if (zclient_debug) | |
2323 | zlog_debug("Releasing Table Chunk"); | |
2324 | ||
2325 | if (zclient->sock < 0) | |
2326 | return -1; | |
2327 | ||
2328 | /* send request */ | |
2329 | s = zclient->obuf; | |
2330 | stream_reset(s); | |
2331 | zclient_create_header(s, ZEBRA_RELEASE_TABLE_CHUNK, VRF_DEFAULT); | |
2332 | ||
2333 | /* start */ | |
2334 | stream_putl(s, start); | |
2335 | /* end */ | |
2336 | stream_putl(s, end); | |
2337 | ||
2338 | /* Put length at the first point of the stream. */ | |
2339 | stream_putw_at(s, 0, stream_get_endp(s)); | |
2340 | ||
2341 | return zclient_send_message(zclient); | |
2342 | } | |
2343 | ||
2344 | ||
2345 | int zebra_send_pw(struct zclient *zclient, int command, struct zapi_pw *pw) | |
2346 | { | |
2347 | struct stream *s; | |
2348 | ||
2349 | /* Reset stream. */ | |
2350 | s = zclient->obuf; | |
2351 | stream_reset(s); | |
2352 | ||
2353 | zclient_create_header(s, command, VRF_DEFAULT); | |
2354 | stream_write(s, pw->ifname, IF_NAMESIZE); | |
2355 | stream_putl(s, pw->ifindex); | |
2356 | ||
2357 | /* Put type */ | |
2358 | stream_putl(s, pw->type); | |
2359 | ||
2360 | /* Put nexthop */ | |
2361 | stream_putl(s, pw->af); | |
2362 | switch (pw->af) { | |
2363 | case AF_INET: | |
2364 | stream_put_in_addr(s, &pw->nexthop.ipv4); | |
2365 | break; | |
2366 | case AF_INET6: | |
2367 | stream_write(s, (uint8_t *)&pw->nexthop.ipv6, 16); | |
2368 | break; | |
2369 | default: | |
2370 | flog_err(EC_LIB_ZAPI_ENCODE, "%s: unknown af", __func__); | |
2371 | return -1; | |
2372 | } | |
2373 | ||
2374 | /* Put labels */ | |
2375 | stream_putl(s, pw->local_label); | |
2376 | stream_putl(s, pw->remote_label); | |
2377 | ||
2378 | /* Put flags */ | |
2379 | stream_putc(s, pw->flags); | |
2380 | ||
2381 | /* Protocol specific fields */ | |
2382 | stream_write(s, &pw->data, sizeof(union pw_protocol_fields)); | |
2383 | ||
2384 | /* Put length at the first point of the stream. */ | |
2385 | stream_putw_at(s, 0, stream_get_endp(s)); | |
2386 | ||
2387 | return zclient_send_message(zclient); | |
2388 | } | |
2389 | ||
2390 | /* | |
2391 | * Receive PW status update from Zebra and send it to LDE process. | |
2392 | */ | |
2393 | void zebra_read_pw_status_update(ZAPI_CALLBACK_ARGS, struct zapi_pw_status *pw) | |
2394 | { | |
2395 | struct stream *s; | |
2396 | ||
2397 | memset(pw, 0, sizeof(struct zapi_pw_status)); | |
2398 | s = zclient->ibuf; | |
2399 | ||
2400 | /* Get data. */ | |
2401 | stream_get(pw->ifname, s, IF_NAMESIZE); | |
2402 | pw->ifindex = stream_getl(s); | |
2403 | pw->status = stream_getl(s); | |
2404 | } | |
2405 | ||
2406 | static void zclient_capability_decode(ZAPI_CALLBACK_ARGS) | |
2407 | { | |
2408 | struct zclient_capabilities cap; | |
2409 | struct stream *s = zclient->ibuf; | |
2410 | int vrf_backend; | |
2411 | uint8_t mpls_enabled; | |
2412 | ||
2413 | STREAM_GETL(s, vrf_backend); | |
2414 | vrf_configure_backend(vrf_backend); | |
2415 | ||
2416 | memset(&cap, 0, sizeof(cap)); | |
2417 | STREAM_GETC(s, mpls_enabled); | |
2418 | cap.mpls_enabled = !!mpls_enabled; | |
2419 | STREAM_GETL(s, cap.ecmp); | |
2420 | STREAM_GETC(s, cap.role); | |
2421 | ||
2422 | if (zclient->zebra_capabilities) | |
2423 | (*zclient->zebra_capabilities)(&cap); | |
2424 | ||
2425 | stream_failure: | |
2426 | return; | |
2427 | } | |
2428 | ||
2429 | /* Zebra client message read function. */ | |
2430 | static int zclient_read(struct thread *thread) | |
2431 | { | |
2432 | size_t already; | |
2433 | uint16_t length, command; | |
2434 | uint8_t marker, version; | |
2435 | vrf_id_t vrf_id; | |
2436 | struct zclient *zclient; | |
2437 | ||
2438 | /* Get socket to zebra. */ | |
2439 | zclient = THREAD_ARG(thread); | |
2440 | zclient->t_read = NULL; | |
2441 | ||
2442 | /* Read zebra header (if we don't have it already). */ | |
2443 | if ((already = stream_get_endp(zclient->ibuf)) < ZEBRA_HEADER_SIZE) { | |
2444 | ssize_t nbyte; | |
2445 | if (((nbyte = stream_read_try(zclient->ibuf, zclient->sock, | |
2446 | ZEBRA_HEADER_SIZE - already)) | |
2447 | == 0) | |
2448 | || (nbyte == -1)) { | |
2449 | if (zclient_debug) | |
2450 | zlog_debug( | |
2451 | "zclient connection closed socket [%d].", | |
2452 | zclient->sock); | |
2453 | return zclient_failed(zclient); | |
2454 | } | |
2455 | if (nbyte != (ssize_t)(ZEBRA_HEADER_SIZE - already)) { | |
2456 | /* Try again later. */ | |
2457 | zclient_event(ZCLIENT_READ, zclient); | |
2458 | return 0; | |
2459 | } | |
2460 | already = ZEBRA_HEADER_SIZE; | |
2461 | } | |
2462 | ||
2463 | /* Reset to read from the beginning of the incoming packet. */ | |
2464 | stream_set_getp(zclient->ibuf, 0); | |
2465 | ||
2466 | /* Fetch header values. */ | |
2467 | length = stream_getw(zclient->ibuf); | |
2468 | marker = stream_getc(zclient->ibuf); | |
2469 | version = stream_getc(zclient->ibuf); | |
2470 | vrf_id = stream_getl(zclient->ibuf); | |
2471 | command = stream_getw(zclient->ibuf); | |
2472 | ||
2473 | if (marker != ZEBRA_HEADER_MARKER || version != ZSERV_VERSION) { | |
2474 | flog_err( | |
2475 | EC_LIB_ZAPI_MISSMATCH, | |
2476 | "%s: socket %d version mismatch, marker %d, version %d", | |
2477 | __func__, zclient->sock, marker, version); | |
2478 | return zclient_failed(zclient); | |
2479 | } | |
2480 | ||
2481 | if (length < ZEBRA_HEADER_SIZE) { | |
2482 | flog_err(EC_LIB_ZAPI_MISSMATCH, | |
2483 | "%s: socket %d message length %u is less than %d ", | |
2484 | __func__, zclient->sock, length, ZEBRA_HEADER_SIZE); | |
2485 | return zclient_failed(zclient); | |
2486 | } | |
2487 | ||
2488 | /* Length check. */ | |
2489 | if (length > STREAM_SIZE(zclient->ibuf)) { | |
2490 | struct stream *ns; | |
2491 | flog_err( | |
2492 | EC_LIB_ZAPI_ENCODE, | |
2493 | "%s: message size %u exceeds buffer size %lu, expanding...", | |
2494 | __func__, length, | |
2495 | (unsigned long)STREAM_SIZE(zclient->ibuf)); | |
2496 | ns = stream_new(length); | |
2497 | stream_copy(ns, zclient->ibuf); | |
2498 | stream_free(zclient->ibuf); | |
2499 | zclient->ibuf = ns; | |
2500 | } | |
2501 | ||
2502 | /* Read rest of zebra packet. */ | |
2503 | if (already < length) { | |
2504 | ssize_t nbyte; | |
2505 | if (((nbyte = stream_read_try(zclient->ibuf, zclient->sock, | |
2506 | length - already)) | |
2507 | == 0) | |
2508 | || (nbyte == -1)) { | |
2509 | if (zclient_debug) | |
2510 | zlog_debug( | |
2511 | "zclient connection closed socket [%d].", | |
2512 | zclient->sock); | |
2513 | return zclient_failed(zclient); | |
2514 | } | |
2515 | if (nbyte != (ssize_t)(length - already)) { | |
2516 | /* Try again later. */ | |
2517 | zclient_event(ZCLIENT_READ, zclient); | |
2518 | return 0; | |
2519 | } | |
2520 | } | |
2521 | ||
2522 | length -= ZEBRA_HEADER_SIZE; | |
2523 | ||
2524 | if (zclient_debug) | |
2525 | zlog_debug("zclient 0x%p command %s VRF %u", | |
2526 | (void *)zclient, zserv_command_string(command), | |
2527 | vrf_id); | |
2528 | ||
2529 | switch (command) { | |
2530 | case ZEBRA_CAPABILITIES: | |
2531 | zclient_capability_decode(command, zclient, length, vrf_id); | |
2532 | break; | |
2533 | case ZEBRA_ROUTER_ID_UPDATE: | |
2534 | if (zclient->router_id_update) | |
2535 | (*zclient->router_id_update)(command, zclient, length, | |
2536 | vrf_id); | |
2537 | break; | |
2538 | case ZEBRA_VRF_ADD: | |
2539 | zclient_vrf_add(zclient, vrf_id); | |
2540 | break; | |
2541 | case ZEBRA_VRF_DELETE: | |
2542 | zclient_vrf_delete(zclient, vrf_id); | |
2543 | break; | |
2544 | case ZEBRA_INTERFACE_ADD: | |
2545 | if (zclient->interface_add) | |
2546 | (*zclient->interface_add)(command, zclient, length, | |
2547 | vrf_id); | |
2548 | break; | |
2549 | case ZEBRA_INTERFACE_DELETE: | |
2550 | if (zclient->interface_delete) | |
2551 | (*zclient->interface_delete)(command, zclient, length, | |
2552 | vrf_id); | |
2553 | break; | |
2554 | case ZEBRA_INTERFACE_ADDRESS_ADD: | |
2555 | if (zclient->interface_address_add) | |
2556 | (*zclient->interface_address_add)(command, zclient, | |
2557 | length, vrf_id); | |
2558 | break; | |
2559 | case ZEBRA_INTERFACE_ADDRESS_DELETE: | |
2560 | if (zclient->interface_address_delete) | |
2561 | (*zclient->interface_address_delete)(command, zclient, | |
2562 | length, vrf_id); | |
2563 | break; | |
2564 | case ZEBRA_INTERFACE_BFD_DEST_UPDATE: | |
2565 | if (zclient->interface_bfd_dest_update) | |
2566 | (*zclient->interface_bfd_dest_update)(command, zclient, | |
2567 | length, vrf_id); | |
2568 | break; | |
2569 | case ZEBRA_INTERFACE_NBR_ADDRESS_ADD: | |
2570 | if (zclient->interface_nbr_address_add) | |
2571 | (*zclient->interface_nbr_address_add)(command, zclient, | |
2572 | length, vrf_id); | |
2573 | break; | |
2574 | case ZEBRA_INTERFACE_NBR_ADDRESS_DELETE: | |
2575 | if (zclient->interface_nbr_address_delete) | |
2576 | (*zclient->interface_nbr_address_delete)( | |
2577 | command, zclient, length, vrf_id); | |
2578 | break; | |
2579 | case ZEBRA_INTERFACE_UP: | |
2580 | if (zclient->interface_up) | |
2581 | (*zclient->interface_up)(command, zclient, length, | |
2582 | vrf_id); | |
2583 | break; | |
2584 | case ZEBRA_INTERFACE_DOWN: | |
2585 | if (zclient->interface_down) | |
2586 | (*zclient->interface_down)(command, zclient, length, | |
2587 | vrf_id); | |
2588 | break; | |
2589 | case ZEBRA_INTERFACE_VRF_UPDATE: | |
2590 | if (zclient->interface_vrf_update) | |
2591 | (*zclient->interface_vrf_update)(command, zclient, | |
2592 | length, vrf_id); | |
2593 | break; | |
2594 | case ZEBRA_NEXTHOP_UPDATE: | |
2595 | if (zclient_debug) | |
2596 | zlog_debug("zclient rcvd nexthop update"); | |
2597 | if (zclient->nexthop_update) | |
2598 | (*zclient->nexthop_update)(command, zclient, length, | |
2599 | vrf_id); | |
2600 | break; | |
2601 | case ZEBRA_IMPORT_CHECK_UPDATE: | |
2602 | if (zclient_debug) | |
2603 | zlog_debug("zclient rcvd import check update"); | |
2604 | if (zclient->import_check_update) | |
2605 | (*zclient->import_check_update)(command, zclient, | |
2606 | length, vrf_id); | |
2607 | break; | |
2608 | case ZEBRA_BFD_DEST_REPLAY: | |
2609 | if (zclient->bfd_dest_replay) | |
2610 | (*zclient->bfd_dest_replay)(command, zclient, length, | |
2611 | vrf_id); | |
2612 | break; | |
2613 | case ZEBRA_REDISTRIBUTE_ROUTE_ADD: | |
2614 | if (zclient->redistribute_route_add) | |
2615 | (*zclient->redistribute_route_add)(command, zclient, | |
2616 | length, vrf_id); | |
2617 | break; | |
2618 | case ZEBRA_REDISTRIBUTE_ROUTE_DEL: | |
2619 | if (zclient->redistribute_route_del) | |
2620 | (*zclient->redistribute_route_del)(command, zclient, | |
2621 | length, vrf_id); | |
2622 | break; | |
2623 | case ZEBRA_INTERFACE_LINK_PARAMS: | |
2624 | if (zclient->interface_link_params) | |
2625 | (*zclient->interface_link_params)(command, zclient, | |
2626 | length, vrf_id); | |
2627 | break; | |
2628 | case ZEBRA_FEC_UPDATE: | |
2629 | if (zclient_debug) | |
2630 | zlog_debug("zclient rcvd fec update"); | |
2631 | if (zclient->fec_update) | |
2632 | (*zclient->fec_update)(command, zclient, length); | |
2633 | break; | |
2634 | case ZEBRA_LOCAL_ES_ADD: | |
2635 | if (zclient->local_es_add) | |
2636 | (*zclient->local_es_add)(command, zclient, length, | |
2637 | vrf_id); | |
2638 | break; | |
2639 | case ZEBRA_LOCAL_ES_DEL: | |
2640 | if (zclient->local_es_del) | |
2641 | (*zclient->local_es_del)(command, zclient, length, | |
2642 | vrf_id); | |
2643 | break; | |
2644 | case ZEBRA_VNI_ADD: | |
2645 | if (zclient->local_vni_add) | |
2646 | (*zclient->local_vni_add)(command, zclient, length, | |
2647 | vrf_id); | |
2648 | break; | |
2649 | case ZEBRA_VNI_DEL: | |
2650 | if (zclient->local_vni_del) | |
2651 | (*zclient->local_vni_del)(command, zclient, length, | |
2652 | vrf_id); | |
2653 | break; | |
2654 | case ZEBRA_L3VNI_ADD: | |
2655 | if (zclient->local_l3vni_add) | |
2656 | (*zclient->local_l3vni_add)(command, zclient, length, | |
2657 | vrf_id); | |
2658 | break; | |
2659 | case ZEBRA_L3VNI_DEL: | |
2660 | if (zclient->local_l3vni_del) | |
2661 | (*zclient->local_l3vni_del)(command, zclient, length, | |
2662 | vrf_id); | |
2663 | break; | |
2664 | case ZEBRA_MACIP_ADD: | |
2665 | if (zclient->local_macip_add) | |
2666 | (*zclient->local_macip_add)(command, zclient, length, | |
2667 | vrf_id); | |
2668 | break; | |
2669 | case ZEBRA_MACIP_DEL: | |
2670 | if (zclient->local_macip_del) | |
2671 | (*zclient->local_macip_del)(command, zclient, length, | |
2672 | vrf_id); | |
2673 | break; | |
2674 | case ZEBRA_IP_PREFIX_ROUTE_ADD: | |
2675 | if (zclient->local_ip_prefix_add) | |
2676 | (*zclient->local_ip_prefix_add)(command, zclient, | |
2677 | length, vrf_id); | |
2678 | break; | |
2679 | case ZEBRA_IP_PREFIX_ROUTE_DEL: | |
2680 | if (zclient->local_ip_prefix_del) | |
2681 | (*zclient->local_ip_prefix_del)(command, zclient, | |
2682 | length, vrf_id); | |
2683 | break; | |
2684 | case ZEBRA_PW_STATUS_UPDATE: | |
2685 | if (zclient->pw_status_update) | |
2686 | (*zclient->pw_status_update)(command, zclient, length, | |
2687 | vrf_id); | |
2688 | break; | |
2689 | case ZEBRA_ROUTE_NOTIFY_OWNER: | |
2690 | if (zclient->route_notify_owner) | |
2691 | (*zclient->route_notify_owner)(command, zclient, length, | |
2692 | vrf_id); | |
2693 | break; | |
2694 | case ZEBRA_RULE_NOTIFY_OWNER: | |
2695 | if (zclient->rule_notify_owner) | |
2696 | (*zclient->rule_notify_owner)(command, zclient, length, | |
2697 | vrf_id); | |
2698 | break; | |
2699 | case ZEBRA_GET_LABEL_CHUNK: | |
2700 | if (zclient->label_chunk) | |
2701 | (*zclient->label_chunk)(command, zclient, length, | |
2702 | vrf_id); | |
2703 | break; | |
2704 | case ZEBRA_IPSET_NOTIFY_OWNER: | |
2705 | if (zclient->ipset_notify_owner) | |
2706 | (*zclient->ipset_notify_owner)(command, zclient, length, | |
2707 | vrf_id); | |
2708 | break; | |
2709 | case ZEBRA_IPSET_ENTRY_NOTIFY_OWNER: | |
2710 | if (zclient->ipset_entry_notify_owner) | |
2711 | (*zclient->ipset_entry_notify_owner)(command, | |
2712 | zclient, length, | |
2713 | vrf_id); | |
2714 | break; | |
2715 | case ZEBRA_IPTABLE_NOTIFY_OWNER: | |
2716 | if (zclient->iptable_notify_owner) | |
2717 | (*zclient->iptable_notify_owner)(command, | |
2718 | zclient, length, | |
2719 | vrf_id); | |
2720 | break; | |
2721 | case ZEBRA_VXLAN_SG_ADD: | |
2722 | if (zclient->vxlan_sg_add) | |
2723 | (*zclient->vxlan_sg_add)(command, zclient, length, | |
2724 | vrf_id); | |
2725 | break; | |
2726 | case ZEBRA_VXLAN_SG_DEL: | |
2727 | if (zclient->vxlan_sg_del) | |
2728 | (*zclient->vxlan_sg_del)(command, zclient, length, | |
2729 | vrf_id); | |
2730 | break; | |
2731 | default: | |
2732 | break; | |
2733 | } | |
2734 | ||
2735 | if (zclient->sock < 0) | |
2736 | /* Connection was closed during packet processing. */ | |
2737 | return -1; | |
2738 | ||
2739 | /* Register read thread. */ | |
2740 | stream_reset(zclient->ibuf); | |
2741 | zclient_event(ZCLIENT_READ, zclient); | |
2742 | ||
2743 | return 0; | |
2744 | } | |
2745 | ||
2746 | void zclient_redistribute(int command, struct zclient *zclient, afi_t afi, | |
2747 | int type, unsigned short instance, vrf_id_t vrf_id) | |
2748 | { | |
2749 | ||
2750 | if (instance) { | |
2751 | if (command == ZEBRA_REDISTRIBUTE_ADD) { | |
2752 | if (redist_check_instance( | |
2753 | &zclient->mi_redist[afi][type], instance)) | |
2754 | return; | |
2755 | redist_add_instance(&zclient->mi_redist[afi][type], | |
2756 | instance); | |
2757 | } else { | |
2758 | if (!redist_check_instance( | |
2759 | &zclient->mi_redist[afi][type], instance)) | |
2760 | return; | |
2761 | redist_del_instance(&zclient->mi_redist[afi][type], | |
2762 | instance); | |
2763 | } | |
2764 | ||
2765 | } else { | |
2766 | if (command == ZEBRA_REDISTRIBUTE_ADD) { | |
2767 | if (vrf_bitmap_check(zclient->redist[afi][type], | |
2768 | vrf_id)) | |
2769 | return; | |
2770 | vrf_bitmap_set(zclient->redist[afi][type], vrf_id); | |
2771 | } else { | |
2772 | if (!vrf_bitmap_check(zclient->redist[afi][type], | |
2773 | vrf_id)) | |
2774 | return; | |
2775 | vrf_bitmap_unset(zclient->redist[afi][type], vrf_id); | |
2776 | } | |
2777 | } | |
2778 | ||
2779 | if (zclient->sock > 0) | |
2780 | zebra_redistribute_send(command, zclient, afi, type, instance, | |
2781 | vrf_id); | |
2782 | } | |
2783 | ||
2784 | ||
2785 | void zclient_redistribute_default(int command, struct zclient *zclient, | |
2786 | afi_t afi, vrf_id_t vrf_id) | |
2787 | { | |
2788 | ||
2789 | if (command == ZEBRA_REDISTRIBUTE_DEFAULT_ADD) { | |
2790 | if (vrf_bitmap_check(zclient->default_information[afi], vrf_id)) | |
2791 | return; | |
2792 | vrf_bitmap_set(zclient->default_information[afi], vrf_id); | |
2793 | } else { | |
2794 | if (!vrf_bitmap_check(zclient->default_information[afi], | |
2795 | vrf_id)) | |
2796 | return; | |
2797 | vrf_bitmap_unset(zclient->default_information[afi], vrf_id); | |
2798 | } | |
2799 | ||
2800 | if (zclient->sock > 0) | |
2801 | zebra_redistribute_default_send(command, zclient, afi, vrf_id); | |
2802 | } | |
2803 | ||
2804 | static void zclient_event(enum event event, struct zclient *zclient) | |
2805 | { | |
2806 | switch (event) { | |
2807 | case ZCLIENT_SCHEDULE: | |
2808 | thread_add_event(zclient->master, zclient_connect, zclient, 0, | |
2809 | &zclient->t_connect); | |
2810 | break; | |
2811 | case ZCLIENT_CONNECT: | |
2812 | if (zclient_debug) | |
2813 | zlog_debug( | |
2814 | "zclient connect failures: %d schedule interval is now %d", | |
2815 | zclient->fail, zclient->fail < 3 ? 10 : 60); | |
2816 | thread_add_timer(zclient->master, zclient_connect, zclient, | |
2817 | zclient->fail < 3 ? 10 : 60, | |
2818 | &zclient->t_connect); | |
2819 | break; | |
2820 | case ZCLIENT_READ: | |
2821 | zclient->t_read = NULL; | |
2822 | thread_add_read(zclient->master, zclient_read, zclient, | |
2823 | zclient->sock, &zclient->t_read); | |
2824 | break; | |
2825 | } | |
2826 | } | |
2827 | ||
2828 | void zclient_interface_set_master(struct zclient *client, | |
2829 | struct interface *master, | |
2830 | struct interface *slave) | |
2831 | { | |
2832 | struct stream *s; | |
2833 | ||
2834 | s = client->obuf; | |
2835 | stream_reset(s); | |
2836 | ||
2837 | zclient_create_header(s, ZEBRA_INTERFACE_SET_MASTER, master->vrf_id); | |
2838 | ||
2839 | stream_putl(s, master->vrf_id); | |
2840 | stream_putl(s, master->ifindex); | |
2841 | stream_putl(s, slave->vrf_id); | |
2842 | stream_putl(s, slave->ifindex); | |
2843 | ||
2844 | stream_putw_at(s, 0, stream_get_endp(s)); | |
2845 | zclient_send_message(client); | |
2846 | } |