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