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