]>
Commit | Line | Data |
---|---|---|
718e3744 | 1 | /* Zebra daemon server routine. |
2 | * Copyright (C) 1997, 98, 99 Kunihiro Ishiguro | |
3 | * | |
4 | * This file is part of GNU Zebra. | |
5 | * | |
6 | * GNU Zebra is free software; you can redistribute it and/or modify it | |
7 | * under the terms of the GNU General Public License as published by the | |
8 | * Free Software Foundation; either version 2, or (at your option) any | |
9 | * later version. | |
10 | * | |
11 | * GNU Zebra is distributed in the hope that it will be useful, but | |
12 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | * General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with GNU Zebra; see the file COPYING. If not, write to the | |
18 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
19 | * Boston, MA 02111-1307, USA. | |
20 | */ | |
21 | ||
22 | #include <zebra.h> | |
23 | ||
24 | #include "prefix.h" | |
25 | #include "command.h" | |
26 | #include "if.h" | |
27 | #include "thread.h" | |
28 | #include "stream.h" | |
29 | #include "memory.h" | |
4a1ab8e4 | 30 | #include "zebra_memory.h" |
718e3744 | 31 | #include "table.h" |
32 | #include "rib.h" | |
33 | #include "network.h" | |
34 | #include "sockunion.h" | |
35 | #include "log.h" | |
36 | #include "zclient.h" | |
edd7c245 | 37 | #include "privs.h" |
719e9741 | 38 | #include "network.h" |
39 | #include "buffer.h" | |
fb018d25 | 40 | #include "nexthop.h" |
78104b9b | 41 | #include "vrf.h" |
718e3744 | 42 | |
43 | #include "zebra/zserv.h" | |
7c551956 DS |
44 | #include "zebra/zebra_ns.h" |
45 | #include "zebra/zebra_vrf.h" | |
18a6dce6 | 46 | #include "zebra/router-id.h" |
718e3744 | 47 | #include "zebra/redistribute.h" |
48 | #include "zebra/debug.h" | |
49 | #include "zebra/ipforward.h" | |
fb018d25 | 50 | #include "zebra/zebra_rnh.h" |
5c610faf | 51 | #include "zebra/rt_netlink.h" |
88177fe3 DS |
52 | #include "zebra/interface.h" |
53 | #include "zebra/zebra_ptm.h" | |
4a04e5f7 | 54 | #include "zebra/rtadv.h" |
ce549947 | 55 | #include "zebra/zebra_mpls.h" |
e3be0432 | 56 | #include "zebra/zebra_mroute.h" |
fea12efb | 57 | #include "zebra/label_manager.h" |
6b0655a2 | 58 | |
718e3744 | 59 | /* Event list of zebra. */ |
60 | enum event { ZEBRA_SERV, ZEBRA_READ, ZEBRA_WRITE }; | |
61 | ||
b9df2d25 | 62 | static void zebra_event (enum event event, int sock, struct zserv *client); |
ccf3557b | 63 | |
edd7c245 | 64 | extern struct zebra_privs_t zserv_privs; |
6b0655a2 | 65 | |
719e9741 | 66 | static void zebra_client_close (struct zserv *client); |
ccf3557b | 67 | |
719e9741 | 68 | static int |
69 | zserv_delayed_close(struct thread *thread) | |
ccf3557b | 70 | { |
719e9741 | 71 | struct zserv *client = THREAD_ARG(thread); |
ccf3557b | 72 | |
719e9741 | 73 | client->t_suicide = NULL; |
74 | zebra_client_close(client); | |
ccf3557b | 75 | return 0; |
76 | } | |
77 | ||
719e9741 | 78 | static int |
79 | zserv_flush_data(struct thread *thread) | |
ccf3557b | 80 | { |
719e9741 | 81 | struct zserv *client = THREAD_ARG(thread); |
ccf3557b | 82 | |
719e9741 | 83 | client->t_write = NULL; |
84 | if (client->t_suicide) | |
ccf3557b | 85 | { |
719e9741 | 86 | zebra_client_close(client); |
87 | return -1; | |
ccf3557b | 88 | } |
719e9741 | 89 | switch (buffer_flush_available(client->wb, client->sock)) |
ccf3557b | 90 | { |
719e9741 | 91 | case BUFFER_ERROR: |
92 | zlog_warn("%s: buffer_flush_available failed on zserv client fd %d, " | |
93 | "closing", __func__, client->sock); | |
94 | zebra_client_close(client); | |
4b21f878 | 95 | client = NULL; |
719e9741 | 96 | break; |
97 | case BUFFER_PENDING: | |
98 | client->t_write = thread_add_write(zebrad.master, zserv_flush_data, | |
99 | client, client->sock); | |
100 | break; | |
101 | case BUFFER_EMPTY: | |
102 | break; | |
ccf3557b | 103 | } |
04b02fda | 104 | |
4b21f878 DS |
105 | if (client) |
106 | client->last_write_time = monotime(NULL); | |
719e9741 | 107 | return 0; |
108 | } | |
ccf3557b | 109 | |
fb018d25 | 110 | int |
719e9741 | 111 | zebra_server_send_message(struct zserv *client) |
112 | { | |
113 | if (client->t_suicide) | |
114 | return -1; | |
04b02fda | 115 | |
fea12efb | 116 | if (client->is_synchronous) |
117 | return 0; | |
118 | ||
04b02fda | 119 | stream_set_getp(client->obuf, 0); |
80ab3edf | 120 | client->last_write_cmd = stream_getw_from(client->obuf, 6); |
719e9741 | 121 | switch (buffer_write(client->wb, client->sock, STREAM_DATA(client->obuf), |
122 | stream_get_endp(client->obuf))) | |
123 | { | |
124 | case BUFFER_ERROR: | |
125 | zlog_warn("%s: buffer_write failed to zserv client fd %d, closing", | |
126 | __func__, client->sock); | |
127 | /* Schedule a delayed close since many of the functions that call this | |
128 | one do not check the return code. They do not allow for the | |
129 | possibility that an I/O error may have caused the client to be | |
130 | deleted. */ | |
131 | client->t_suicide = thread_add_event(zebrad.master, zserv_delayed_close, | |
132 | client, 0); | |
133 | return -1; | |
719e9741 | 134 | case BUFFER_EMPTY: |
135 | THREAD_OFF(client->t_write); | |
136 | break; | |
137 | case BUFFER_PENDING: | |
138 | THREAD_WRITE_ON(zebrad.master, client->t_write, | |
139 | zserv_flush_data, client, client->sock); | |
140 | break; | |
141 | } | |
04b02fda | 142 | |
cf672a86 | 143 | client->last_write_time = monotime(NULL); |
ccf3557b | 144 | return 0; |
145 | } | |
146 | ||
fb018d25 | 147 | void |
7076bb2f | 148 | zserv_create_header (struct stream *s, uint16_t cmd, vrf_id_t vrf_id) |
c1b9800a | 149 | { |
150 | /* length placeholder, caller can update */ | |
151 | stream_putw (s, ZEBRA_HEADER_SIZE); | |
152 | stream_putc (s, ZEBRA_HEADER_MARKER); | |
153 | stream_putc (s, ZSERV_VERSION); | |
7076bb2f | 154 | stream_putw (s, vrf_id); |
c1b9800a | 155 | stream_putw (s, cmd); |
156 | } | |
157 | ||
51d4ef83 JB |
158 | static void |
159 | zserv_encode_interface (struct stream *s, struct interface *ifp) | |
160 | { | |
161 | /* Interface information. */ | |
162 | stream_put (s, ifp->name, INTERFACE_NAMSIZ); | |
163 | stream_putl (s, ifp->ifindex); | |
164 | stream_putc (s, ifp->status); | |
165 | stream_putq (s, ifp->flags); | |
244c1cdc DS |
166 | stream_putc (s, ifp->ptm_enable); |
167 | stream_putc (s, ifp->ptm_status); | |
51d4ef83 | 168 | stream_putl (s, ifp->metric); |
2d7f0d76 | 169 | stream_putl (s, ifp->speed); |
51d4ef83 JB |
170 | stream_putl (s, ifp->mtu); |
171 | stream_putl (s, ifp->mtu6); | |
172 | stream_putl (s, ifp->bandwidth); | |
8ccc7e80 | 173 | stream_putl (s, ifp->ll_type); |
51d4ef83 JB |
174 | stream_putl (s, ifp->hw_addr_len); |
175 | if (ifp->hw_addr_len) | |
176 | stream_put (s, ifp->hw_addr, ifp->hw_addr_len); | |
51d4ef83 | 177 | |
16f1b9ee OD |
178 | /* Then, Traffic Engineering parameters if any */ |
179 | if (HAS_LINK_PARAMS(ifp) && IS_LINK_PARAMS_SET(ifp->link_params)) | |
180 | { | |
181 | stream_putc (s, 1); | |
182 | zebra_interface_link_params_write (s, ifp); | |
183 | } | |
184 | else | |
185 | stream_putc (s, 0); | |
186 | ||
51d4ef83 JB |
187 | /* Write packet size. */ |
188 | stream_putw_at (s, 0, stream_get_endp (s)); | |
189 | } | |
190 | ||
12f6fb97 | 191 | static void |
9e1bf607 | 192 | zserv_encode_vrf (struct stream *s, struct zebra_vrf *zvrf) |
12f6fb97 DS |
193 | { |
194 | /* Interface information. */ | |
661512bf | 195 | stream_put (s, zvrf_name (zvrf), VRF_NAMSIZ); |
12f6fb97 DS |
196 | |
197 | /* Write packet size. */ | |
198 | stream_putw_at (s, 0, stream_get_endp (s)); | |
199 | } | |
200 | ||
718e3744 | 201 | /* Interface is added. Send ZEBRA_INTERFACE_ADD to client. */ |
b9df2d25 | 202 | /* |
203 | * This function is called in the following situations: | |
204 | * - in response to a 3-byte ZEBRA_INTERFACE_ADD request | |
205 | * from the client. | |
206 | * - at startup, when zebra figures out the available interfaces | |
207 | * - when an interface is added (where support for | |
208 | * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when | |
209 | * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is | |
210 | * received) | |
211 | */ | |
718e3744 | 212 | int |
213 | zsend_interface_add (struct zserv *client, struct interface *ifp) | |
214 | { | |
215 | struct stream *s; | |
216 | ||
718e3744 | 217 | s = client->obuf; |
218 | stream_reset (s); | |
219 | ||
7076bb2f | 220 | zserv_create_header (s, ZEBRA_INTERFACE_ADD, ifp->vrf_id); |
51d4ef83 | 221 | zserv_encode_interface (s, ifp); |
718e3744 | 222 | |
04b02fda | 223 | client->ifadd_cnt++; |
719e9741 | 224 | return zebra_server_send_message(client); |
718e3744 | 225 | } |
226 | ||
227 | /* Interface deletion from zebra daemon. */ | |
228 | int | |
229 | zsend_interface_delete (struct zserv *client, struct interface *ifp) | |
230 | { | |
231 | struct stream *s; | |
232 | ||
718e3744 | 233 | s = client->obuf; |
234 | stream_reset (s); | |
718e3744 | 235 | |
7076bb2f | 236 | zserv_create_header (s, ZEBRA_INTERFACE_DELETE, ifp->vrf_id); |
51d4ef83 | 237 | zserv_encode_interface (s, ifp); |
718e3744 | 238 | |
04b02fda | 239 | client->ifdel_cnt++; |
719e9741 | 240 | return zebra_server_send_message (client); |
718e3744 | 241 | } |
242 | ||
12f6fb97 | 243 | int |
9e1bf607 | 244 | zsend_vrf_add (struct zserv *client, struct zebra_vrf *zvrf) |
12f6fb97 DS |
245 | { |
246 | struct stream *s; | |
247 | ||
248 | s = client->obuf; | |
249 | stream_reset (s); | |
250 | ||
661512bf | 251 | zserv_create_header (s, ZEBRA_VRF_ADD, zvrf_id (zvrf)); |
9e1bf607 | 252 | zserv_encode_vrf (s, zvrf); |
12f6fb97 DS |
253 | |
254 | client->vrfadd_cnt++; | |
255 | return zebra_server_send_message(client); | |
256 | } | |
257 | ||
258 | /* VRF deletion from zebra daemon. */ | |
259 | int | |
9e1bf607 | 260 | zsend_vrf_delete (struct zserv *client, struct zebra_vrf *zvrf) |
12f6fb97 DS |
261 | { |
262 | struct stream *s; | |
263 | ||
264 | s = client->obuf; | |
265 | stream_reset (s); | |
266 | ||
661512bf | 267 | zserv_create_header (s, ZEBRA_VRF_DELETE, zvrf_id (zvrf)); |
9e1bf607 | 268 | zserv_encode_vrf (s, zvrf); |
12f6fb97 DS |
269 | |
270 | client->vrfdel_cnt++; | |
271 | return zebra_server_send_message (client); | |
272 | } | |
273 | ||
16f1b9ee OD |
274 | int |
275 | zsend_interface_link_params (struct zserv *client, struct interface *ifp) | |
276 | { | |
277 | struct stream *s; | |
278 | ||
279 | /* Check this client need interface information. */ | |
280 | if (! client->ifinfo) | |
281 | return 0; | |
282 | ||
283 | if (!ifp->link_params) | |
284 | return 0; | |
285 | s = client->obuf; | |
286 | stream_reset (s); | |
287 | ||
288 | zserv_create_header (s, ZEBRA_INTERFACE_LINK_PARAMS, ifp->vrf_id); | |
289 | ||
290 | /* Add Interface Index */ | |
291 | stream_putl (s, ifp->ifindex); | |
292 | ||
293 | /* Then TE Link Parameters */ | |
294 | if (zebra_interface_link_params_write (s, ifp) == 0) | |
295 | return 0; | |
296 | ||
297 | /* Write packet size. */ | |
298 | stream_putw_at (s, 0, stream_get_endp (s)); | |
299 | ||
300 | return zebra_server_send_message (client); | |
301 | } | |
302 | ||
b9df2d25 | 303 | /* Interface address is added/deleted. Send ZEBRA_INTERFACE_ADDRESS_ADD or |
304 | * ZEBRA_INTERFACE_ADDRESS_DELETE to the client. | |
305 | * | |
306 | * A ZEBRA_INTERFACE_ADDRESS_ADD is sent in the following situations: | |
307 | * - in response to a 3-byte ZEBRA_INTERFACE_ADD request | |
308 | * from the client, after the ZEBRA_INTERFACE_ADD has been | |
309 | * sent from zebra to the client | |
310 | * - redistribute new address info to all clients in the following situations | |
311 | * - at startup, when zebra figures out the available interfaces | |
312 | * - when an interface is added (where support for | |
313 | * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when | |
314 | * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is | |
315 | * received) | |
316 | * - for the vty commands "ip address A.B.C.D/M [<secondary>|<label LINE>]" | |
317 | * and "no bandwidth <1-10000000>", "ipv6 address X:X::X:X/M" | |
318 | * - when an RTM_NEWADDR message is received from the kernel, | |
319 | * | |
320 | * The call tree that triggers ZEBRA_INTERFACE_ADDRESS_DELETE: | |
321 | * | |
322 | * zsend_interface_address(DELETE) | |
323 | * ^ | |
324 | * | | |
325 | * zebra_interface_address_delete_update | |
326 | * ^ ^ ^ | |
6eb8827d | 327 | * | | if_delete_update |
328 | * | | | |
b9df2d25 | 329 | * ip_address_uninstall connected_delete_ipv4 |
330 | * [ipv6_addresss_uninstall] [connected_delete_ipv6] | |
331 | * ^ ^ | |
332 | * | | | |
333 | * | RTM_NEWADDR on routing/netlink socket | |
334 | * | | |
335 | * vty commands: | |
336 | * "no ip address A.B.C.D/M [label LINE]" | |
337 | * "no ip address A.B.C.D/M secondary" | |
338 | * ["no ipv6 address X:X::X:X/M"] | |
339 | * | |
340 | */ | |
718e3744 | 341 | int |
b9df2d25 | 342 | zsend_interface_address (int cmd, struct zserv *client, |
343 | struct interface *ifp, struct connected *ifc) | |
718e3744 | 344 | { |
345 | int blen; | |
346 | struct stream *s; | |
347 | struct prefix *p; | |
348 | ||
718e3744 | 349 | s = client->obuf; |
350 | stream_reset (s); | |
c1b9800a | 351 | |
7076bb2f | 352 | zserv_create_header (s, cmd, ifp->vrf_id); |
718e3744 | 353 | stream_putl (s, ifp->ifindex); |
354 | ||
355 | /* Interface address flag. */ | |
356 | stream_putc (s, ifc->flags); | |
357 | ||
358 | /* Prefix information. */ | |
359 | p = ifc->address; | |
360 | stream_putc (s, p->family); | |
361 | blen = prefix_blen (p); | |
362 | stream_put (s, &p->u.prefix, blen); | |
b9df2d25 | 363 | |
364 | /* | |
365 | * XXX gnu version does not send prefixlen for ZEBRA_INTERFACE_ADDRESS_DELETE | |
366 | * but zebra_interface_address_delete_read() in the gnu version | |
367 | * expects to find it | |
368 | */ | |
718e3744 | 369 | stream_putc (s, p->prefixlen); |
370 | ||
371 | /* Destination. */ | |
372 | p = ifc->destination; | |
373 | if (p) | |
374 | stream_put (s, &p->u.prefix, blen); | |
375 | else | |
376 | stream_put (s, NULL, blen); | |
377 | ||
378 | /* Write packet size. */ | |
379 | stream_putw_at (s, 0, stream_get_endp (s)); | |
380 | ||
04b02fda | 381 | client->connected_rt_add_cnt++; |
719e9741 | 382 | return zebra_server_send_message(client); |
718e3744 | 383 | } |
384 | ||
a80beece DS |
385 | static int |
386 | zsend_interface_nbr_address (int cmd, struct zserv *client, | |
387 | struct interface *ifp, struct nbr_connected *ifc) | |
388 | { | |
389 | int blen; | |
390 | struct stream *s; | |
391 | struct prefix *p; | |
392 | ||
a80beece DS |
393 | s = client->obuf; |
394 | stream_reset (s); | |
395 | ||
7076bb2f | 396 | zserv_create_header (s, cmd, ifp->vrf_id); |
a80beece DS |
397 | stream_putl (s, ifp->ifindex); |
398 | ||
399 | /* Prefix information. */ | |
400 | p = ifc->address; | |
401 | stream_putc (s, p->family); | |
402 | blen = prefix_blen (p); | |
403 | stream_put (s, &p->u.prefix, blen); | |
404 | ||
405 | /* | |
406 | * XXX gnu version does not send prefixlen for ZEBRA_INTERFACE_ADDRESS_DELETE | |
407 | * but zebra_interface_address_delete_read() in the gnu version | |
408 | * expects to find it | |
409 | */ | |
410 | stream_putc (s, p->prefixlen); | |
411 | ||
412 | /* Write packet size. */ | |
413 | stream_putw_at (s, 0, stream_get_endp (s)); | |
414 | ||
415 | return zebra_server_send_message(client); | |
416 | } | |
417 | ||
418 | /* Interface address addition. */ | |
419 | static void | |
420 | zebra_interface_nbr_address_add_update (struct interface *ifp, | |
421 | struct nbr_connected *ifc) | |
422 | { | |
423 | struct listnode *node, *nnode; | |
424 | struct zserv *client; | |
425 | struct prefix *p; | |
426 | ||
427 | if (IS_ZEBRA_DEBUG_EVENT) | |
428 | { | |
429 | char buf[INET6_ADDRSTRLEN]; | |
430 | ||
431 | p = ifc->address; | |
432 | zlog_debug ("MESSAGE: ZEBRA_INTERFACE_NBR_ADDRESS_ADD %s/%d on %s", | |
433 | inet_ntop (p->family, &p->u.prefix, buf, INET6_ADDRSTRLEN), | |
434 | p->prefixlen, ifc->ifp->name); | |
435 | } | |
436 | ||
437 | for (ALL_LIST_ELEMENTS (zebrad.client_list, node, nnode, client)) | |
4fe51714 | 438 | zsend_interface_nbr_address (ZEBRA_INTERFACE_NBR_ADDRESS_ADD, client, ifp, ifc); |
a80beece DS |
439 | } |
440 | ||
441 | /* Interface address deletion. */ | |
442 | static void | |
443 | zebra_interface_nbr_address_delete_update (struct interface *ifp, | |
444 | struct nbr_connected *ifc) | |
445 | { | |
446 | struct listnode *node, *nnode; | |
447 | struct zserv *client; | |
448 | struct prefix *p; | |
449 | ||
450 | if (IS_ZEBRA_DEBUG_EVENT) | |
451 | { | |
452 | char buf[INET6_ADDRSTRLEN]; | |
453 | ||
454 | p = ifc->address; | |
455 | zlog_debug ("MESSAGE: ZEBRA_INTERFACE_NBR_ADDRESS_DELETE %s/%d on %s", | |
456 | inet_ntop (p->family, &p->u.prefix, buf, INET6_ADDRSTRLEN), | |
457 | p->prefixlen, ifc->ifp->name); | |
458 | } | |
459 | ||
460 | for (ALL_LIST_ELEMENTS (zebrad.client_list, node, nnode, client)) | |
4fe51714 | 461 | zsend_interface_nbr_address (ZEBRA_INTERFACE_NBR_ADDRESS_DELETE, client, ifp, ifc); |
a80beece DS |
462 | } |
463 | ||
c8e264b6 | 464 | /* Send addresses on interface to client */ |
465 | int | |
466 | zsend_interface_addresses (struct zserv *client, struct interface *ifp) | |
467 | { | |
468 | struct listnode *cnode, *cnnode; | |
469 | struct connected *c; | |
470 | struct nbr_connected *nc; | |
471 | ||
472 | /* Send interface addresses. */ | |
473 | for (ALL_LIST_ELEMENTS (ifp->connected, cnode, cnnode, c)) | |
474 | { | |
475 | if (!CHECK_FLAG (c->conf, ZEBRA_IFC_REAL)) | |
476 | continue; | |
477 | ||
478 | if (zsend_interface_address (ZEBRA_INTERFACE_ADDRESS_ADD, client, | |
479 | ifp, c) < 0) | |
480 | return -1; | |
481 | } | |
482 | ||
483 | /* Send interface neighbors. */ | |
484 | for (ALL_LIST_ELEMENTS (ifp->nbr_connected, cnode, cnnode, nc)) | |
485 | { | |
486 | if (zsend_interface_nbr_address (ZEBRA_INTERFACE_NBR_ADDRESS_ADD, | |
487 | client, ifp, nc) < 0) | |
488 | return -1; | |
489 | } | |
490 | ||
491 | return 0; | |
492 | } | |
493 | ||
494 | /* Notify client about interface moving from one VRF to another. | |
495 | * Whether client is interested in old and new VRF is checked by caller. | |
496 | */ | |
497 | int | |
498 | zsend_interface_vrf_update (struct zserv *client, struct interface *ifp, | |
499 | vrf_id_t vrf_id) | |
500 | { | |
501 | struct stream *s; | |
502 | ||
503 | s = client->obuf; | |
504 | stream_reset (s); | |
505 | ||
506 | zserv_create_header (s, ZEBRA_INTERFACE_VRF_UPDATE, ifp->vrf_id); | |
507 | ||
508 | /* Fill in the ifIndex of the interface and its new VRF (id) */ | |
509 | stream_putl (s, ifp->ifindex); | |
510 | stream_putw (s, vrf_id); | |
511 | ||
512 | /* Write packet size. */ | |
513 | stream_putw_at (s, 0, stream_get_endp (s)); | |
514 | ||
515 | client->if_vrfchg_cnt++; | |
516 | return zebra_server_send_message(client); | |
517 | } | |
518 | ||
1d20ccf3 | 519 | /* Add new nbr connected IPv6 address */ |
a80beece | 520 | void |
1d20ccf3 | 521 | nbr_connected_add_ipv6 (struct interface *ifp, struct in6_addr *address) |
a80beece DS |
522 | { |
523 | struct nbr_connected *ifc; | |
524 | struct prefix p; | |
525 | ||
526 | p.family = AF_INET6; | |
527 | IPV6_ADDR_COPY (&p.u.prefix, address); | |
1d20ccf3 | 528 | p.prefixlen = IPV6_MAX_PREFIXLEN; |
a80beece DS |
529 | |
530 | if (!(ifc = listnode_head(ifp->nbr_connected))) | |
531 | { | |
532 | /* new addition */ | |
533 | ifc = nbr_connected_new (); | |
534 | ifc->address = prefix_new(); | |
535 | ifc->ifp = ifp; | |
536 | listnode_add (ifp->nbr_connected, ifc); | |
537 | } | |
538 | ||
539 | prefix_copy(ifc->address, &p); | |
540 | ||
541 | zebra_interface_nbr_address_add_update (ifp, ifc); | |
5c610faf DS |
542 | |
543 | if_nbr_ipv6ll_to_ipv4ll_neigh_update (ifp, address, 1); | |
a80beece DS |
544 | } |
545 | ||
546 | void | |
1d20ccf3 | 547 | nbr_connected_delete_ipv6 (struct interface *ifp, struct in6_addr *address) |
a80beece DS |
548 | { |
549 | struct nbr_connected *ifc; | |
550 | struct prefix p; | |
551 | ||
552 | p.family = AF_INET6; | |
553 | IPV6_ADDR_COPY (&p.u.prefix, address); | |
1d20ccf3 | 554 | p.prefixlen = IPV6_MAX_PREFIXLEN; |
a80beece DS |
555 | |
556 | ifc = nbr_connected_check(ifp, &p); | |
557 | if (!ifc) | |
558 | return; | |
559 | ||
560 | listnode_delete (ifp->nbr_connected, ifc); | |
561 | ||
562 | zebra_interface_nbr_address_delete_update (ifp, ifc); | |
563 | ||
5c610faf DS |
564 | if_nbr_ipv6ll_to_ipv4ll_neigh_update (ifp, address, 0); |
565 | ||
a80beece DS |
566 | nbr_connected_free (ifc); |
567 | } | |
568 | ||
b9df2d25 | 569 | /* |
570 | * The cmd passed to zsend_interface_update may be ZEBRA_INTERFACE_UP or | |
571 | * ZEBRA_INTERFACE_DOWN. | |
572 | * | |
573 | * The ZEBRA_INTERFACE_UP message is sent from the zebra server to | |
574 | * the clients in one of 2 situations: | |
575 | * - an if_up is detected e.g., as a result of an RTM_IFINFO message | |
576 | * - a vty command modifying the bandwidth of an interface is received. | |
577 | * The ZEBRA_INTERFACE_DOWN message is sent when an if_down is detected. | |
578 | */ | |
718e3744 | 579 | int |
b9df2d25 | 580 | zsend_interface_update (int cmd, struct zserv *client, struct interface *ifp) |
718e3744 | 581 | { |
582 | struct stream *s; | |
583 | ||
718e3744 | 584 | s = client->obuf; |
585 | stream_reset (s); | |
586 | ||
7076bb2f | 587 | zserv_create_header (s, cmd, ifp->vrf_id); |
51d4ef83 | 588 | zserv_encode_interface (s, ifp); |
718e3744 | 589 | |
04b02fda DS |
590 | if (cmd == ZEBRA_INTERFACE_UP) |
591 | client->ifup_cnt++; | |
592 | else | |
593 | client->ifdown_cnt++; | |
594 | ||
719e9741 | 595 | return zebra_server_send_message(client); |
718e3744 | 596 | } |
597 | ||
b9df2d25 | 598 | /* |
5048fe14 | 599 | * This is the new function to announce and withdraw redistributed routes, used |
600 | * by Zebra. This is the old zsend_route_multipath() function. That function | |
601 | * was duplicating code to send a lot of information that was essentially thrown | |
602 | * away or ignored by the receiver. This is the leaner function that is not a | |
603 | * duplicate of the zapi_ipv4_route_add/del. | |
b9df2d25 | 604 | * |
5048fe14 | 605 | * The primary difference is that this function merely sends a single NH instead of |
606 | * all the nexthops. | |
b9df2d25 | 607 | */ |
718e3744 | 608 | int |
9c6060d4 | 609 | zsend_redistribute_route (int add, struct zserv *client, struct prefix *p, |
05737783 | 610 | struct prefix *src_p, struct rib *rib) |
718e3744 | 611 | { |
9c6060d4 RW |
612 | afi_t afi; |
613 | int cmd; | |
718e3744 | 614 | int psize; |
615 | struct stream *s; | |
616 | struct nexthop *nexthop; | |
1dcb5172 | 617 | unsigned long nhnummark = 0, messmark = 0; |
b9df2d25 | 618 | int nhnum = 0; |
1dcb5172 | 619 | u_char zapi_flags = 0; |
5048fe14 | 620 | struct nexthop dummy_nh; |
621 | ||
9c6060d4 RW |
622 | afi = family2afi (p->family); |
623 | if (add) | |
624 | { | |
625 | switch (afi) | |
626 | { | |
627 | case AFI_IP: | |
628 | cmd = ZEBRA_REDISTRIBUTE_IPV4_ADD; | |
629 | client->redist_v4_add_cnt++; | |
630 | break; | |
631 | case AFI_IP6: | |
632 | cmd = ZEBRA_REDISTRIBUTE_IPV6_ADD; | |
633 | client->redist_v6_add_cnt++; | |
634 | break; | |
635 | default: | |
636 | return -1; | |
637 | } | |
638 | } | |
639 | else | |
640 | { | |
641 | switch (afi) | |
642 | { | |
643 | case AFI_IP: | |
644 | cmd = ZEBRA_REDISTRIBUTE_IPV4_DEL; | |
645 | client->redist_v4_del_cnt++; | |
646 | break; | |
647 | case AFI_IP6: | |
648 | cmd = ZEBRA_REDISTRIBUTE_IPV6_DEL; | |
649 | client->redist_v6_del_cnt++; | |
650 | break; | |
651 | default: | |
652 | return -1; | |
653 | } | |
654 | } | |
7076bb2f | 655 | |
718e3744 | 656 | s = client->obuf; |
657 | stream_reset (s); | |
5048fe14 | 658 | memset(&dummy_nh, 0, sizeof(struct nexthop)); |
659 | ||
7076bb2f FL |
660 | zserv_create_header (s, cmd, rib->vrf_id); |
661 | ||
c1b9800a | 662 | /* Put type and nexthop. */ |
718e3744 | 663 | stream_putc (s, rib->type); |
7c8ff89e | 664 | stream_putw (s, rib->instance); |
0fc452dc | 665 | stream_putl (s, rib->flags); |
5048fe14 | 666 | |
1dcb5172 | 667 | /* marker for message flags field */ |
668 | messmark = stream_get_endp (s); | |
669 | stream_putc (s, 0); | |
718e3744 | 670 | |
671 | /* Prefix. */ | |
672 | psize = PSIZE (p->prefixlen); | |
673 | stream_putc (s, p->prefixlen); | |
b9df2d25 | 674 | stream_write (s, (u_char *) & p->u.prefix, psize); |
675 | ||
05737783 CF |
676 | if (src_p) |
677 | { | |
678 | SET_FLAG (zapi_flags, ZAPI_MESSAGE_SRCPFX); | |
679 | psize = PSIZE (src_p->prefixlen); | |
680 | stream_putc (s, src_p->prefixlen); | |
681 | stream_write (s, (u_char *) & src_p->u.prefix, psize); | |
682 | } | |
683 | ||
718e3744 | 684 | for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next) |
685 | { | |
5048fe14 | 686 | /* We don't send any nexthops when there's a multipath */ |
eac6e3f0 | 687 | if (rib->nexthop_active_num > 1 && client->proto != ZEBRA_ROUTE_LDP) |
5048fe14 | 688 | { |
689 | SET_FLAG (zapi_flags, ZAPI_MESSAGE_NEXTHOP); | |
690 | SET_FLAG (zapi_flags, ZAPI_MESSAGE_IFINDEX); | |
691 | ||
692 | stream_putc(s, 1); | |
693 | if (p->family == AF_INET) | |
694 | { | |
695 | stream_put_in_addr (s, &dummy_nh.gate.ipv4); | |
696 | } | |
697 | else if (p->family == AF_INET6) | |
698 | { | |
699 | stream_write (s, (u_char *) &dummy_nh.gate.ipv6, 16); | |
700 | } | |
701 | else | |
702 | { | |
703 | /* We don't handle anything else now, abort */ | |
704 | zlog_err("%s: Unable to redistribute route of unknown family, %d\n", | |
705 | __func__, p->family); | |
706 | return -1; | |
707 | } | |
708 | stream_putc (s, 1); | |
709 | stream_putl (s, 0); /* dummy ifindex */ | |
710 | break; | |
711 | } | |
712 | ||
446bb95e | 713 | if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE)) |
b9df2d25 | 714 | { |
1dcb5172 | 715 | SET_FLAG (zapi_flags, ZAPI_MESSAGE_NEXTHOP); |
716 | SET_FLAG (zapi_flags, ZAPI_MESSAGE_IFINDEX); | |
1dcb5172 | 717 | if (nhnummark == 0) |
718 | { | |
719 | nhnummark = stream_get_endp (s); | |
720 | stream_putc (s, 1); /* placeholder */ | |
721 | } | |
b9df2d25 | 722 | nhnum++; |
723 | ||
724 | switch(nexthop->type) | |
725 | { | |
726 | case NEXTHOP_TYPE_IPV4: | |
727 | case NEXTHOP_TYPE_IPV4_IFINDEX: | |
728 | stream_put_in_addr (s, &nexthop->gate.ipv4); | |
729 | break; | |
b9df2d25 | 730 | case NEXTHOP_TYPE_IPV6: |
731 | case NEXTHOP_TYPE_IPV6_IFINDEX: | |
5048fe14 | 732 | /* Only BGP supports IPv4 prefix with IPv6 NH, so kill this */ |
733 | if (p->family == AF_INET) | |
734 | stream_put_in_addr(s, &dummy_nh.gate.ipv4); | |
735 | else | |
736 | stream_write (s, (u_char *) &nexthop->gate.ipv6, 16); | |
b9df2d25 | 737 | break; |
b9df2d25 | 738 | default: |
5048fe14 | 739 | if (cmd == ZEBRA_REDISTRIBUTE_IPV4_ADD |
740 | || cmd == ZEBRA_REDISTRIBUTE_IPV4_DEL) | |
b9df2d25 | 741 | { |
742 | struct in_addr empty; | |
44983cf8 | 743 | memset (&empty, 0, sizeof (struct in_addr)); |
b9df2d25 | 744 | stream_write (s, (u_char *) &empty, IPV4_MAX_BYTELEN); |
745 | } | |
746 | else | |
747 | { | |
748 | struct in6_addr empty; | |
749 | memset (&empty, 0, sizeof (struct in6_addr)); | |
750 | stream_write (s, (u_char *) &empty, IPV6_MAX_BYTELEN); | |
751 | } | |
752 | } | |
753 | ||
754 | /* Interface index. */ | |
755 | stream_putc (s, 1); | |
756 | stream_putl (s, nexthop->ifindex); | |
757 | ||
eac6e3f0 RW |
758 | /* ldpd needs all nexthops */ |
759 | if (client->proto != ZEBRA_ROUTE_LDP) | |
a16d0e7b | 760 | break; |
b9df2d25 | 761 | } |
718e3744 | 762 | } |
763 | ||
7fe041ac | 764 | /* Distance */ |
a16d0e7b RW |
765 | SET_FLAG (zapi_flags, ZAPI_MESSAGE_DISTANCE); |
766 | stream_putc (s, rib->distance); | |
7fe041ac | 767 | |
718e3744 | 768 | /* Metric */ |
a16d0e7b RW |
769 | SET_FLAG (zapi_flags, ZAPI_MESSAGE_METRIC); |
770 | stream_putl (s, rib->metric); | |
0d9551dc | 771 | |
7fe041ac | 772 | /* Tag */ |
a16d0e7b RW |
773 | if (rib->tag) |
774 | { | |
775 | SET_FLAG(zapi_flags, ZAPI_MESSAGE_TAG); | |
dc9ffce8 | 776 | stream_putl(s, rib->tag); |
a16d0e7b | 777 | } |
5048fe14 | 778 | |
7fe041ac | 779 | /* MTU */ |
a16d0e7b RW |
780 | SET_FLAG (zapi_flags, ZAPI_MESSAGE_MTU); |
781 | stream_putl (s, rib->mtu); | |
5048fe14 | 782 | |
1dcb5172 | 783 | /* write real message flags value */ |
784 | stream_putc_at (s, messmark, zapi_flags); | |
5048fe14 | 785 | |
b9df2d25 | 786 | /* Write next-hop number */ |
787 | if (nhnummark) | |
c1eaa442 | 788 | stream_putc_at (s, nhnummark, nhnum); |
5048fe14 | 789 | |
718e3744 | 790 | /* Write packet size. */ |
791 | stream_putw_at (s, 0, stream_get_endp (s)); | |
792 | ||
719e9741 | 793 | return zebra_server_send_message(client); |
718e3744 | 794 | } |
795 | ||
10fbd59a DS |
796 | static int |
797 | zsend_write_nexthop (struct stream *s, struct nexthop *nexthop) | |
798 | { | |
799 | stream_putc (s, nexthop->type); | |
800 | switch (nexthop->type) | |
801 | { | |
802 | case NEXTHOP_TYPE_IPV4: | |
10fbd59a DS |
803 | case NEXTHOP_TYPE_IPV4_IFINDEX: |
804 | stream_put_in_addr (s, &nexthop->gate.ipv4); | |
805 | stream_putl (s, nexthop->ifindex); | |
806 | break; | |
807 | case NEXTHOP_TYPE_IPV6: | |
808 | stream_put (s, &nexthop->gate.ipv6, 16); | |
809 | break; | |
810 | case NEXTHOP_TYPE_IPV6_IFINDEX: | |
811 | stream_put (s, &nexthop->gate.ipv6, 16); | |
812 | stream_putl (s, nexthop->ifindex); | |
813 | break; | |
814 | case NEXTHOP_TYPE_IFINDEX: | |
815 | stream_putl (s, nexthop->ifindex); | |
816 | break; | |
817 | default: | |
818 | /* do nothing */ | |
819 | break; | |
820 | } | |
821 | return 1; | |
822 | } | |
823 | ||
fb018d25 DS |
824 | /* Nexthop register */ |
825 | static int | |
078430f6 | 826 | zserv_rnh_register (struct zserv *client, int sock, u_short length, |
d651649e | 827 | rnh_type_t type, struct zebra_vrf *zvrf) |
fb018d25 DS |
828 | { |
829 | struct rnh *rnh; | |
830 | struct stream *s; | |
831 | struct prefix p; | |
832 | u_short l = 0; | |
078430f6 | 833 | u_char flags = 0; |
fb018d25 DS |
834 | |
835 | if (IS_ZEBRA_DEBUG_NHT) | |
078430f6 DS |
836 | zlog_debug("rnh_register msg from client %s: length=%d, type=%s\n", |
837 | zebra_route_string(client->proto), length, | |
838 | (type == RNH_NEXTHOP_TYPE) ? "nexthop" : "route"); | |
fb018d25 DS |
839 | |
840 | s = client->ibuf; | |
841 | ||
cf672a86 | 842 | client->nh_reg_time = monotime(NULL); |
078430f6 | 843 | |
fb018d25 DS |
844 | while (l < length) |
845 | { | |
078430f6 | 846 | flags = stream_getc(s); |
fb018d25 DS |
847 | p.family = stream_getw(s); |
848 | p.prefixlen = stream_getc(s); | |
fc9a856f | 849 | l += 4; |
078430f6 DS |
850 | if (p.family == AF_INET) |
851 | { | |
852 | p.u.prefix4.s_addr = stream_get_ipv4(s); | |
853 | l += IPV4_MAX_BYTELEN; | |
854 | } | |
855 | else if (p.family == AF_INET6) | |
856 | { | |
857 | stream_get(&p.u.prefix6, s, IPV6_MAX_BYTELEN); | |
858 | l += IPV6_MAX_BYTELEN; | |
859 | } | |
860 | else | |
861 | { | |
862 | zlog_err("rnh_register: Received unknown family type %d\n", | |
863 | p.family); | |
864 | return -1; | |
865 | } | |
661512bf | 866 | rnh = zebra_add_rnh(&p, zvrf_id (zvrf), type); |
078430f6 DS |
867 | if (type == RNH_NEXTHOP_TYPE) |
868 | { | |
869 | if (flags && !CHECK_FLAG(rnh->flags, ZEBRA_NHT_CONNECTED)) | |
870 | SET_FLAG(rnh->flags, ZEBRA_NHT_CONNECTED); | |
871 | else if (!flags && CHECK_FLAG(rnh->flags, ZEBRA_NHT_CONNECTED)) | |
872 | UNSET_FLAG(rnh->flags, ZEBRA_NHT_CONNECTED); | |
873 | } | |
874 | else if (type == RNH_IMPORT_CHECK_TYPE) | |
875 | { | |
876 | if (flags && !CHECK_FLAG(rnh->flags, ZEBRA_NHT_EXACT_MATCH)) | |
877 | SET_FLAG(rnh->flags, ZEBRA_NHT_EXACT_MATCH); | |
878 | else if (!flags && CHECK_FLAG(rnh->flags, ZEBRA_NHT_EXACT_MATCH)) | |
879 | UNSET_FLAG(rnh->flags, ZEBRA_NHT_EXACT_MATCH); | |
880 | } | |
881 | ||
661512bf | 882 | zebra_add_rnh_client(rnh, client, type, zvrf_id (zvrf)); |
078430f6 | 883 | /* Anything not AF_INET/INET6 has been filtered out above */ |
661512bf | 884 | zebra_evaluate_rnh(zvrf_id (zvrf), p.family, 1, type, &p); |
fb018d25 | 885 | } |
fb018d25 DS |
886 | return 0; |
887 | } | |
888 | ||
889 | /* Nexthop register */ | |
890 | static int | |
078430f6 | 891 | zserv_rnh_unregister (struct zserv *client, int sock, u_short length, |
d651649e | 892 | rnh_type_t type, struct zebra_vrf *zvrf) |
fb018d25 DS |
893 | { |
894 | struct rnh *rnh; | |
895 | struct stream *s; | |
896 | struct prefix p; | |
897 | u_short l = 0; | |
898 | ||
899 | if (IS_ZEBRA_DEBUG_NHT) | |
078430f6 | 900 | zlog_debug("rnh_unregister msg from client %s: length=%d\n", |
fb018d25 DS |
901 | zebra_route_string(client->proto), length); |
902 | ||
903 | s = client->ibuf; | |
904 | ||
905 | while (l < length) | |
906 | { | |
4e3afb14 | 907 | (void)stream_getc(s); //Connected or not. Not used in this function |
fb018d25 DS |
908 | p.family = stream_getw(s); |
909 | p.prefixlen = stream_getc(s); | |
fc9a856f | 910 | l += 4; |
078430f6 DS |
911 | if (p.family == AF_INET) |
912 | { | |
913 | p.u.prefix4.s_addr = stream_get_ipv4(s); | |
914 | l += IPV4_MAX_BYTELEN; | |
915 | } | |
916 | else if (p.family == AF_INET6) | |
917 | { | |
918 | stream_get(&p.u.prefix6, s, IPV6_MAX_BYTELEN); | |
919 | l += IPV6_MAX_BYTELEN; | |
920 | } | |
921 | else | |
922 | { | |
923 | zlog_err("rnh_register: Received unknown family type %d\n", | |
924 | p.family); | |
925 | return -1; | |
926 | } | |
661512bf | 927 | rnh = zebra_lookup_rnh(&p, zvrf_id (zvrf), type); |
fb018d25 | 928 | if (rnh) |
04b02fda | 929 | { |
cf672a86 | 930 | client->nh_dereg_time = monotime(NULL); |
078430f6 | 931 | zebra_remove_rnh_client(rnh, client, type); |
04b02fda | 932 | } |
fb018d25 DS |
933 | } |
934 | return 0; | |
935 | } | |
936 | ||
5aba114a DS |
937 | /* FEC register */ |
938 | static int | |
939 | zserv_fec_register (struct zserv *client, int sock, u_short length) | |
940 | { | |
941 | struct stream *s; | |
942 | struct zebra_vrf *zvrf; | |
943 | u_short l = 0; | |
944 | struct prefix p; | |
28d58fd7 VV |
945 | u_int16_t flags; |
946 | u_int32_t label_index = MPLS_INVALID_LABEL_INDEX; | |
5aba114a DS |
947 | |
948 | s = client->ibuf; | |
949 | zvrf = vrf_info_lookup(VRF_DEFAULT); | |
950 | if (!zvrf) | |
951 | return 0; // unexpected | |
952 | ||
953 | while (l < length) | |
954 | { | |
28d58fd7 | 955 | flags = stream_getw(s); |
5aba114a DS |
956 | p.family = stream_getw(s); |
957 | p.prefixlen = stream_getc(s); | |
958 | l += 5; | |
959 | stream_get(&p.u.prefix, s, PSIZE(p.prefixlen)); | |
960 | l += PSIZE(p.prefixlen); | |
28d58fd7 VV |
961 | if (flags & ZEBRA_FEC_REGISTER_LABEL_INDEX) |
962 | { | |
963 | label_index = stream_getl(s); | |
964 | l += 4; | |
965 | } | |
966 | zebra_mpls_fec_register (zvrf, &p, label_index, client); | |
5aba114a DS |
967 | } |
968 | ||
969 | return 0; | |
970 | } | |
971 | ||
972 | /* FEC unregister */ | |
973 | static int | |
974 | zserv_fec_unregister (struct zserv *client, int sock, u_short length) | |
975 | { | |
976 | struct stream *s; | |
977 | struct zebra_vrf *zvrf; | |
978 | u_short l = 0; | |
979 | struct prefix p; | |
85154a40 | 980 | //u_int16_t flags; |
5aba114a DS |
981 | |
982 | s = client->ibuf; | |
983 | zvrf = vrf_info_lookup(VRF_DEFAULT); | |
984 | if (!zvrf) | |
985 | return 0; // unexpected | |
986 | ||
987 | while (l < length) | |
988 | { | |
85154a40 DS |
989 | //flags = stream_getw(s); |
990 | (void)stream_getw(s); | |
5aba114a DS |
991 | p.family = stream_getw(s); |
992 | p.prefixlen = stream_getc(s); | |
993 | l += 5; | |
994 | stream_get(&p.u.prefix, s, PSIZE(p.prefixlen)); | |
995 | l += PSIZE(p.prefixlen); | |
996 | zebra_mpls_fec_unregister (zvrf, &p, client); | |
997 | } | |
998 | ||
999 | return 0; | |
1000 | } | |
1001 | ||
6f61a5a3 EM |
1002 | /* |
1003 | Modified version of zsend_ipv4_nexthop_lookup(): | |
1004 | Query unicast rib if nexthop is not found on mrib. | |
1005 | Returns both route metric and protocol distance. | |
1006 | */ | |
1007 | static int | |
4623d897 | 1008 | zsend_ipv4_nexthop_lookup_mrib (struct zserv *client, struct in_addr addr, struct rib *rib, struct zebra_vrf *zvrf) |
6f61a5a3 EM |
1009 | { |
1010 | struct stream *s; | |
6f61a5a3 EM |
1011 | unsigned long nump; |
1012 | u_char num; | |
1013 | struct nexthop *nexthop; | |
1014 | ||
6f61a5a3 EM |
1015 | /* Get output stream. */ |
1016 | s = client->obuf; | |
1017 | stream_reset (s); | |
1018 | ||
1019 | /* Fill in result. */ | |
661512bf | 1020 | zserv_create_header (s, ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB, zvrf_id (zvrf)); |
6f61a5a3 EM |
1021 | stream_put_in_addr (s, &addr); |
1022 | ||
1023 | if (rib) | |
1024 | { | |
1025 | stream_putc (s, rib->distance); | |
1026 | stream_putl (s, rib->metric); | |
1027 | num = 0; | |
1028 | nump = stream_get_endp(s); /* remember position for nexthop_num */ | |
1029 | stream_putc (s, 0); /* reserve room for nexthop_num */ | |
1030 | /* Only non-recursive routes are elegible to resolve the nexthop we | |
1031 | * are looking up. Therefore, we will just iterate over the top | |
1032 | * chain of nexthops. */ | |
1033 | for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next) | |
446bb95e | 1034 | if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE)) |
10fbd59a | 1035 | num += zsend_write_nexthop (s, nexthop); |
6f61a5a3 EM |
1036 | |
1037 | stream_putc_at (s, nump, num); /* store nexthop_num */ | |
1038 | } | |
1039 | else | |
1040 | { | |
1041 | stream_putc (s, 0); /* distance */ | |
1042 | stream_putl (s, 0); /* metric */ | |
1043 | stream_putc (s, 0); /* nexthop_num */ | |
1044 | } | |
1045 | ||
1046 | stream_putw_at (s, 0, stream_get_endp (s)); | |
1047 | ||
1048 | return zebra_server_send_message(client); | |
1049 | } | |
1050 | ||
18a6dce6 | 1051 | /* Router-id is updated. Send ZEBRA_ROUTER_ID_ADD to client. */ |
1052 | int | |
c6ffe645 FL |
1053 | zsend_router_id_update (struct zserv *client, struct prefix *p, |
1054 | vrf_id_t vrf_id) | |
18a6dce6 | 1055 | { |
1056 | struct stream *s; | |
1057 | int blen; | |
1058 | ||
1059 | /* Check this client need interface information. */ | |
7076bb2f | 1060 | if (! vrf_bitmap_check (client->ridinfo, vrf_id)) |
719e9741 | 1061 | return 0; |
18a6dce6 | 1062 | |
1063 | s = client->obuf; | |
1064 | stream_reset (s); | |
1065 | ||
18a6dce6 | 1066 | /* Message type. */ |
7076bb2f | 1067 | zserv_create_header (s, ZEBRA_ROUTER_ID_UPDATE, vrf_id); |
18a6dce6 | 1068 | |
1069 | /* Prefix information. */ | |
1070 | stream_putc (s, p->family); | |
1071 | blen = prefix_blen (p); | |
1072 | stream_put (s, &p->u.prefix, blen); | |
1073 | stream_putc (s, p->prefixlen); | |
1074 | ||
1075 | /* Write packet size. */ | |
1076 | stream_putw_at (s, 0, stream_get_endp (s)); | |
1077 | ||
719e9741 | 1078 | return zebra_server_send_message(client); |
18a6dce6 | 1079 | } |
6b0655a2 | 1080 | |
718e3744 | 1081 | /* Register zebra server interface information. Send current all |
1082 | interface and address information. */ | |
719e9741 | 1083 | static int |
d651649e | 1084 | zread_interface_add (struct zserv *client, u_short length, struct zebra_vrf *zvrf) |
718e3744 | 1085 | { |
1a1a7065 | 1086 | struct vrf *vrf; |
1eb8ef25 | 1087 | struct listnode *ifnode, *ifnnode; |
718e3744 | 1088 | struct interface *ifp; |
718e3744 | 1089 | |
1090 | /* Interface information is needed. */ | |
661512bf | 1091 | vrf_bitmap_set (client->ifinfo, zvrf_id (zvrf)); |
718e3744 | 1092 | |
1a1a7065 | 1093 | RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) |
718e3744 | 1094 | { |
1a1a7065 | 1095 | for (ALL_LIST_ELEMENTS (vrf->iflist, ifnode, ifnnode, ifp)) |
4fe51714 DS |
1096 | { |
1097 | /* Skip pseudo interface. */ | |
1098 | if (! CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE)) | |
1099 | continue; | |
718e3744 | 1100 | |
4fe51714 DS |
1101 | if (zsend_interface_add (client, ifp) < 0) |
1102 | return -1; | |
718e3744 | 1103 | |
4fe51714 DS |
1104 | if (zsend_interface_addresses (client, ifp) < 0) |
1105 | return -1; | |
1106 | } | |
718e3744 | 1107 | } |
719e9741 | 1108 | return 0; |
718e3744 | 1109 | } |
1110 | ||
1111 | /* Unregister zebra server interface information. */ | |
719e9741 | 1112 | static int |
d651649e | 1113 | zread_interface_delete (struct zserv *client, u_short length, struct zebra_vrf *zvrf) |
718e3744 | 1114 | { |
661512bf | 1115 | vrf_bitmap_unset (client->ifinfo, zvrf_id (zvrf)); |
719e9741 | 1116 | return 0; |
718e3744 | 1117 | } |
1118 | ||
6878b9db | 1119 | void |
4f87aceb | 1120 | zserv_nexthop_num_warn (const char *caller, const struct prefix *p, const unsigned int nexthop_num) |
6878b9db | 1121 | { |
37fe7731 | 1122 | if (nexthop_num > multipath_num) |
6878b9db | 1123 | { |
4690c7d7 DS |
1124 | char buff[PREFIX2STR_BUFFER]; |
1125 | prefix2str(p, buff, sizeof (buff)); | |
6878b9db | 1126 | zlog_warn("%s: Prefix %s has %d nexthops, but we can only use the first %d", |
37fe7731 | 1127 | caller, buff, nexthop_num, multipath_num); |
6878b9db DS |
1128 | } |
1129 | } | |
1130 | ||
718e3744 | 1131 | /* This function support multiple nexthop. */ |
b9df2d25 | 1132 | /* |
1133 | * Parse the ZEBRA_IPV4_ROUTE_ADD sent from client. Update rib and | |
1134 | * add kernel route. | |
1135 | */ | |
719e9741 | 1136 | static int |
d651649e | 1137 | zread_ipv4_add (struct zserv *client, u_short length, struct zebra_vrf *zvrf) |
718e3744 | 1138 | { |
1139 | int i; | |
1140 | struct rib *rib; | |
b4c034b0 | 1141 | struct prefix p; |
718e3744 | 1142 | u_char message; |
a64448ba | 1143 | struct in_addr nhop_addr; |
718e3744 | 1144 | u_char nexthop_num; |
1145 | u_char nexthop_type; | |
1146 | struct stream *s; | |
b892f1dd | 1147 | ifindex_t ifindex; |
d44ca835 | 1148 | safi_t safi; |
04b02fda | 1149 | int ret; |
a64448ba DS |
1150 | mpls_label_t label; |
1151 | struct nexthop *nexthop; | |
718e3744 | 1152 | |
1153 | /* Get input stream. */ | |
1154 | s = client->ibuf; | |
1155 | ||
1156 | /* Allocate new rib. */ | |
4d38fdb4 | 1157 | rib = XCALLOC (MTYPE_RIB, sizeof (struct rib)); |
1158 | ||
718e3744 | 1159 | /* Type, flags, message. */ |
1160 | rib->type = stream_getc (s); | |
7c8ff89e | 1161 | rib->instance = stream_getw (s); |
0fc452dc | 1162 | rib->flags = stream_getl (s); |
b9df2d25 | 1163 | message = stream_getc (s); |
cddf391b | 1164 | safi = stream_getw (s); |
718e3744 | 1165 | rib->uptime = time (NULL); |
1166 | ||
1167 | /* IPv4 prefix. */ | |
1168 | memset (&p, 0, sizeof (struct prefix_ipv4)); | |
1169 | p.family = AF_INET; | |
1170 | p.prefixlen = stream_getc (s); | |
b4c034b0 | 1171 | stream_get (&p.u.prefix4, s, PSIZE (p.prefixlen)); |
718e3744 | 1172 | |
78104b9b | 1173 | /* VRF ID */ |
661512bf | 1174 | rib->vrf_id = zvrf_id (zvrf); |
78104b9b | 1175 | |
718e3744 | 1176 | /* Nexthop parse. */ |
1177 | if (CHECK_FLAG (message, ZAPI_MESSAGE_NEXTHOP)) | |
1178 | { | |
1179 | nexthop_num = stream_getc (s); | |
6878b9db | 1180 | zserv_nexthop_num_warn(__func__, (const struct prefix *)&p, nexthop_num); |
718e3744 | 1181 | |
1182 | for (i = 0; i < nexthop_num; i++) | |
1183 | { | |
1184 | nexthop_type = stream_getc (s); | |
1185 | ||
1186 | switch (nexthop_type) | |
1187 | { | |
5b30316e | 1188 | case NEXTHOP_TYPE_IFINDEX: |
718e3744 | 1189 | ifindex = stream_getl (s); |
a399694f | 1190 | rib_nexthop_ifindex_add (rib, ifindex); |
718e3744 | 1191 | break; |
5b30316e | 1192 | case NEXTHOP_TYPE_IPV4: |
a64448ba DS |
1193 | nhop_addr.s_addr = stream_get_ipv4 (s); |
1194 | nexthop = rib_nexthop_ipv4_add (rib, &nhop_addr, NULL); | |
1195 | /* For labeled-unicast, each nexthop is followed by label. */ | |
1196 | if (CHECK_FLAG (message, ZAPI_MESSAGE_LABEL)) | |
1197 | { | |
1198 | label = (mpls_label_t)stream_getl (s); | |
1199 | nexthop_add_labels (nexthop, nexthop->nh_label_type, 1, &label); | |
1200 | } | |
718e3744 | 1201 | break; |
5b30316e | 1202 | case NEXTHOP_TYPE_IPV4_IFINDEX: |
a64448ba | 1203 | nhop_addr.s_addr = stream_get_ipv4 (s); |
c963c203 | 1204 | ifindex = stream_getl (s); |
a64448ba | 1205 | rib_nexthop_ipv4_ifindex_add (rib, &nhop_addr, NULL, ifindex); |
c963c203 | 1206 | break; |
5b30316e | 1207 | case NEXTHOP_TYPE_IPV6: |
9985f83c | 1208 | stream_forward_getp (s, IPV6_MAX_BYTELEN); |
718e3744 | 1209 | break; |
5b30316e | 1210 | case NEXTHOP_TYPE_BLACKHOLE: |
a399694f | 1211 | rib_nexthop_blackhole_add (rib); |
6902c69a SV |
1212 | break; |
1213 | } | |
718e3744 | 1214 | } |
1215 | } | |
1216 | ||
1217 | /* Distance. */ | |
1218 | if (CHECK_FLAG (message, ZAPI_MESSAGE_DISTANCE)) | |
1219 | rib->distance = stream_getc (s); | |
1220 | ||
1221 | /* Metric. */ | |
1222 | if (CHECK_FLAG (message, ZAPI_MESSAGE_METRIC)) | |
1223 | rib->metric = stream_getl (s); | |
1224 | ||
0d9551dc DS |
1225 | /* Tag */ |
1226 | if (CHECK_FLAG (message, ZAPI_MESSAGE_TAG)) | |
dc9ffce8 | 1227 | rib->tag = stream_getl (s); |
0d9551dc DS |
1228 | else |
1229 | rib->tag = 0; | |
1230 | ||
c50ca33a TT |
1231 | if (CHECK_FLAG (message, ZAPI_MESSAGE_MTU)) |
1232 | rib->mtu = stream_getl (s); | |
1233 | else | |
1234 | rib->mtu = 0; | |
1235 | ||
171eee31 | 1236 | /* Table */ |
d651649e | 1237 | rib->table = zvrf->table_id; |
12f6fb97 | 1238 | |
3c7c91d0 | 1239 | ret = rib_add_multipath (AFI_IP, safi, &p, NULL, rib); |
04b02fda DS |
1240 | |
1241 | /* Stats */ | |
1242 | if (ret > 0) | |
1243 | client->v4_route_add_cnt++; | |
1244 | else if (ret < 0) | |
1245 | client->v4_route_upd8_cnt++; | |
719e9741 | 1246 | return 0; |
718e3744 | 1247 | } |
1248 | ||
1249 | /* Zebra server IPv4 prefix delete function. */ | |
719e9741 | 1250 | static int |
d651649e | 1251 | zread_ipv4_delete (struct zserv *client, u_short length, struct zebra_vrf *zvrf) |
718e3744 | 1252 | { |
1253 | int i; | |
1254 | struct stream *s; | |
1255 | struct zapi_ipv4 api; | |
616368ed DS |
1256 | struct in_addr nexthop; |
1257 | union g_addr *nexthop_p; | |
718e3744 | 1258 | unsigned long ifindex; |
616368ed | 1259 | struct prefix p; |
718e3744 | 1260 | u_char nexthop_num; |
1261 | u_char nexthop_type; | |
12f6fb97 | 1262 | u_int32_t table_id; |
12f6fb97 | 1263 | |
718e3744 | 1264 | s = client->ibuf; |
1265 | ifindex = 0; | |
1266 | nexthop.s_addr = 0; | |
6902c69a | 1267 | nexthop_p = NULL; |
718e3744 | 1268 | |
1269 | /* Type, flags, message. */ | |
1270 | api.type = stream_getc (s); | |
7c8ff89e | 1271 | api.instance = stream_getw (s); |
0fc452dc | 1272 | api.flags = stream_getl (s); |
718e3744 | 1273 | api.message = stream_getc (s); |
cddf391b | 1274 | api.safi = stream_getw (s); |
718e3744 | 1275 | |
1276 | /* IPv4 prefix. */ | |
4fdeb6b0 | 1277 | memset (&p, 0, sizeof (struct prefix)); |
718e3744 | 1278 | p.family = AF_INET; |
1279 | p.prefixlen = stream_getc (s); | |
616368ed | 1280 | stream_get (&p.u.prefix4, s, PSIZE (p.prefixlen)); |
718e3744 | 1281 | |
1282 | /* Nexthop, ifindex, distance, metric. */ | |
1283 | if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP)) | |
1284 | { | |
1285 | nexthop_num = stream_getc (s); | |
1286 | ||
1287 | for (i = 0; i < nexthop_num; i++) | |
1288 | { | |
1289 | nexthop_type = stream_getc (s); | |
1290 | ||
1291 | switch (nexthop_type) | |
1292 | { | |
5b30316e | 1293 | case NEXTHOP_TYPE_IFINDEX: |
718e3744 | 1294 | ifindex = stream_getl (s); |
1295 | break; | |
5b30316e | 1296 | case NEXTHOP_TYPE_IPV4: |
718e3744 | 1297 | nexthop.s_addr = stream_get_ipv4 (s); |
a64448ba DS |
1298 | /* For labeled-unicast, each nexthop is followed by label, but |
1299 | * we don't care for delete. | |
1300 | */ | |
1301 | if (CHECK_FLAG (api.message, ZAPI_MESSAGE_LABEL)) | |
1302 | stream_forward_getp (s, sizeof(u_int32_t)); | |
616368ed | 1303 | nexthop_p = (union g_addr *)&nexthop; |
718e3744 | 1304 | break; |
5b30316e | 1305 | case NEXTHOP_TYPE_IPV4_IFINDEX: |
c963c203 | 1306 | nexthop.s_addr = stream_get_ipv4 (s); |
616368ed | 1307 | nexthop_p = (union g_addr *)&nexthop; |
c963c203 JT |
1308 | ifindex = stream_getl (s); |
1309 | break; | |
5b30316e | 1310 | case NEXTHOP_TYPE_IPV6: |
9985f83c | 1311 | stream_forward_getp (s, IPV6_MAX_BYTELEN); |
718e3744 | 1312 | break; |
1313 | } | |
1314 | } | |
1315 | } | |
1316 | ||
1317 | /* Distance. */ | |
1318 | if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE)) | |
1319 | api.distance = stream_getc (s); | |
1320 | else | |
1321 | api.distance = 0; | |
1322 | ||
1323 | /* Metric. */ | |
1324 | if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC)) | |
1325 | api.metric = stream_getl (s); | |
1326 | else | |
1327 | api.metric = 0; | |
1328 | ||
0d9551dc DS |
1329 | /* tag */ |
1330 | if (CHECK_FLAG (api.message, ZAPI_MESSAGE_TAG)) | |
dc9ffce8 | 1331 | api.tag = stream_getl (s); |
0d9551dc DS |
1332 | else |
1333 | api.tag = 0; | |
1334 | ||
d651649e | 1335 | table_id = zvrf->table_id; |
12f6fb97 | 1336 | |
661512bf | 1337 | rib_delete (AFI_IP, api.safi, zvrf_id (zvrf), api.type, api.instance, |
3c7c91d0 | 1338 | api.flags, &p, NULL, nexthop_p, ifindex, table_id); |
04b02fda | 1339 | client->v4_route_del_cnt++; |
719e9741 | 1340 | return 0; |
718e3744 | 1341 | } |
1342 | ||
6f61a5a3 EM |
1343 | /* MRIB Nexthop lookup for IPv4. */ |
1344 | static int | |
1345 | zread_ipv4_nexthop_lookup_mrib (struct zserv *client, u_short length, struct zebra_vrf *zvrf) | |
1346 | { | |
1347 | struct in_addr addr; | |
4623d897 | 1348 | struct rib *rib; |
6f61a5a3 EM |
1349 | |
1350 | addr.s_addr = stream_get_ipv4 (client->ibuf); | |
661512bf | 1351 | rib = rib_match_ipv4_multicast (zvrf_id (zvrf), addr, NULL); |
4623d897 | 1352 | return zsend_ipv4_nexthop_lookup_mrib (client, addr, rib, zvrf); |
6f61a5a3 EM |
1353 | } |
1354 | ||
8a92a8a0 DS |
1355 | /* Zebra server IPv6 prefix add function. */ |
1356 | static int | |
d651649e | 1357 | zread_ipv4_route_ipv6_nexthop_add (struct zserv *client, u_short length, struct zebra_vrf *zvrf) |
8a92a8a0 | 1358 | { |
37fe7731 | 1359 | unsigned int i; |
8a92a8a0 DS |
1360 | struct stream *s; |
1361 | struct in6_addr nexthop; | |
1362 | struct rib *rib; | |
1363 | u_char message; | |
1364 | u_char nexthop_num; | |
1365 | u_char nexthop_type; | |
b4c034b0 | 1366 | struct prefix p; |
8a92a8a0 DS |
1367 | safi_t safi; |
1368 | static struct in6_addr nexthops[MULTIPATH_NUM]; | |
1369 | static unsigned int ifindices[MULTIPATH_NUM]; | |
1370 | int ret; | |
1371 | ||
1372 | /* Get input stream. */ | |
1373 | s = client->ibuf; | |
1374 | ||
8a92a8a0 DS |
1375 | memset (&nexthop, 0, sizeof (struct in6_addr)); |
1376 | ||
1377 | /* Allocate new rib. */ | |
1378 | rib = XCALLOC (MTYPE_RIB, sizeof (struct rib)); | |
1379 | ||
1380 | /* Type, flags, message. */ | |
1381 | rib->type = stream_getc (s); | |
1382 | rib->instance = stream_getw (s); | |
0fc452dc | 1383 | rib->flags = stream_getl (s); |
8a92a8a0 DS |
1384 | message = stream_getc (s); |
1385 | safi = stream_getw (s); | |
1386 | rib->uptime = time (NULL); | |
1387 | ||
1388 | /* IPv4 prefix. */ | |
1389 | memset (&p, 0, sizeof (struct prefix_ipv4)); | |
1390 | p.family = AF_INET; | |
1391 | p.prefixlen = stream_getc (s); | |
b4c034b0 | 1392 | stream_get (&p.u.prefix4, s, PSIZE (p.prefixlen)); |
8a92a8a0 | 1393 | |
154caaed | 1394 | /* VRF ID */ |
661512bf | 1395 | rib->vrf_id = zvrf_id (zvrf); |
154caaed | 1396 | |
8a92a8a0 DS |
1397 | /* We need to give nh-addr, nh-ifindex with the same next-hop object |
1398 | * to the rib to ensure that IPv6 multipathing works; need to coalesce | |
1399 | * these. Clients should send the same number of paired set of | |
1400 | * next-hop-addr/next-hop-ifindices. */ | |
1401 | if (CHECK_FLAG (message, ZAPI_MESSAGE_NEXTHOP)) | |
1402 | { | |
37fe7731 DS |
1403 | unsigned int nh_count = 0; |
1404 | unsigned int if_count = 0; | |
1405 | unsigned int max_nh_if = 0; | |
8a92a8a0 DS |
1406 | |
1407 | nexthop_num = stream_getc (s); | |
6878b9db | 1408 | zserv_nexthop_num_warn(__func__, (const struct prefix *)&p, nexthop_num); |
8a92a8a0 DS |
1409 | for (i = 0; i < nexthop_num; i++) |
1410 | { | |
1411 | nexthop_type = stream_getc (s); | |
1412 | ||
1413 | switch (nexthop_type) | |
1414 | { | |
5b30316e | 1415 | case NEXTHOP_TYPE_IPV6: |
8a92a8a0 | 1416 | stream_get (&nexthop, s, 16); |
37fe7731 | 1417 | if (nh_count < multipath_num) { |
8a92a8a0 DS |
1418 | nexthops[nh_count++] = nexthop; |
1419 | } | |
1420 | break; | |
5b30316e | 1421 | case NEXTHOP_TYPE_IFINDEX: |
37fe7731 | 1422 | if (if_count < multipath_num) { |
8a92a8a0 DS |
1423 | ifindices[if_count++] = stream_getl (s); |
1424 | } | |
1425 | break; | |
5b30316e | 1426 | case NEXTHOP_TYPE_BLACKHOLE: |
a399694f | 1427 | rib_nexthop_blackhole_add (rib); |
8a92a8a0 DS |
1428 | break; |
1429 | } | |
1430 | } | |
1431 | ||
1432 | max_nh_if = (nh_count > if_count) ? nh_count : if_count; | |
1433 | for (i = 0; i < max_nh_if; i++) | |
1434 | { | |
1435 | if ((i < nh_count) && !IN6_IS_ADDR_UNSPECIFIED (&nexthops[i])) { | |
1436 | if ((i < if_count) && ifindices[i]) { | |
a399694f | 1437 | rib_nexthop_ipv6_ifindex_add (rib, &nexthops[i], ifindices[i]); |
8a92a8a0 DS |
1438 | } |
1439 | else { | |
a399694f | 1440 | rib_nexthop_ipv6_add (rib, &nexthops[i]); |
8a92a8a0 DS |
1441 | } |
1442 | } | |
1443 | else { | |
1444 | if ((i < if_count) && ifindices[i]) { | |
a399694f | 1445 | rib_nexthop_ifindex_add (rib, ifindices[i]); |
8a92a8a0 DS |
1446 | } |
1447 | } | |
1448 | } | |
1449 | } | |
1450 | ||
1451 | /* Distance. */ | |
1452 | if (CHECK_FLAG (message, ZAPI_MESSAGE_DISTANCE)) | |
1453 | rib->distance = stream_getc (s); | |
1454 | ||
1455 | /* Metric. */ | |
1456 | if (CHECK_FLAG (message, ZAPI_MESSAGE_METRIC)) | |
1457 | rib->metric = stream_getl (s); | |
1458 | ||
1459 | /* Tag */ | |
1460 | if (CHECK_FLAG (message, ZAPI_MESSAGE_TAG)) | |
dc9ffce8 | 1461 | rib->tag = stream_getl (s); |
8a92a8a0 DS |
1462 | else |
1463 | rib->tag = 0; | |
1464 | ||
c50ca33a TT |
1465 | if (CHECK_FLAG (message, ZAPI_MESSAGE_MTU)) |
1466 | rib->mtu = stream_getl (s); | |
1467 | else | |
1468 | rib->mtu = 0; | |
1469 | ||
8a92a8a0 | 1470 | /* Table */ |
d651649e | 1471 | rib->table = zvrf->table_id; |
12f6fb97 | 1472 | |
3c7c91d0 | 1473 | ret = rib_add_multipath (AFI_IP6, safi, &p, NULL, rib); |
8a92a8a0 DS |
1474 | /* Stats */ |
1475 | if (ret > 0) | |
1476 | client->v4_route_add_cnt++; | |
1477 | else if (ret < 0) | |
1478 | client->v4_route_upd8_cnt++; | |
1479 | ||
1480 | return 0; | |
1481 | } | |
1482 | ||
719e9741 | 1483 | static int |
d651649e | 1484 | zread_ipv6_add (struct zserv *client, u_short length, struct zebra_vrf *zvrf) |
718e3744 | 1485 | { |
37fe7731 | 1486 | unsigned int i; |
718e3744 | 1487 | struct stream *s; |
a64448ba | 1488 | struct in6_addr nhop_addr; |
41fc2714 DS |
1489 | struct rib *rib; |
1490 | u_char message; | |
1491 | u_char nexthop_num; | |
1492 | u_char nexthop_type; | |
b4c034b0 | 1493 | struct prefix p; |
3c7c91d0 | 1494 | struct prefix_ipv6 src_p, *src_pp; |
41fc2714 DS |
1495 | safi_t safi; |
1496 | static struct in6_addr nexthops[MULTIPATH_NUM]; | |
1497 | static unsigned int ifindices[MULTIPATH_NUM]; | |
04b02fda | 1498 | int ret; |
a64448ba DS |
1499 | static mpls_label_t labels[MULTIPATH_NUM]; |
1500 | mpls_label_t label; | |
1501 | struct nexthop *nexthop; | |
41fc2714 DS |
1502 | |
1503 | /* Get input stream. */ | |
718e3744 | 1504 | s = client->ibuf; |
41fc2714 | 1505 | |
a64448ba | 1506 | memset (&nhop_addr, 0, sizeof (struct in6_addr)); |
718e3744 | 1507 | |
41fc2714 DS |
1508 | /* Allocate new rib. */ |
1509 | rib = XCALLOC (MTYPE_RIB, sizeof (struct rib)); | |
1510 | ||
718e3744 | 1511 | /* Type, flags, message. */ |
41fc2714 | 1512 | rib->type = stream_getc (s); |
7c8ff89e | 1513 | rib->instance = stream_getw (s); |
0fc452dc | 1514 | rib->flags = stream_getl (s); |
41fc2714 DS |
1515 | message = stream_getc (s); |
1516 | safi = stream_getw (s); | |
1517 | rib->uptime = time (NULL); | |
718e3744 | 1518 | |
41fc2714 | 1519 | /* IPv6 prefix. */ |
718e3744 | 1520 | memset (&p, 0, sizeof (struct prefix_ipv6)); |
1521 | p.family = AF_INET6; | |
1522 | p.prefixlen = stream_getc (s); | |
b4c034b0 | 1523 | stream_get (&p.u.prefix6, s, PSIZE (p.prefixlen)); |
718e3744 | 1524 | |
3c7c91d0 DL |
1525 | if (CHECK_FLAG (message, ZAPI_MESSAGE_SRCPFX)) |
1526 | { | |
1527 | memset (&src_p, 0, sizeof (struct prefix_ipv6)); | |
1528 | src_p.family = AF_INET6; | |
1529 | src_p.prefixlen = stream_getc (s); | |
1530 | stream_get (&src_p.prefix, s, PSIZE (src_p.prefixlen)); | |
1531 | src_pp = &src_p; | |
1532 | } | |
1533 | else | |
1534 | src_pp = NULL; | |
1535 | ||
41fc2714 DS |
1536 | /* We need to give nh-addr, nh-ifindex with the same next-hop object |
1537 | * to the rib to ensure that IPv6 multipathing works; need to coalesce | |
1538 | * these. Clients should send the same number of paired set of | |
1539 | * next-hop-addr/next-hop-ifindices. */ | |
1540 | if (CHECK_FLAG (message, ZAPI_MESSAGE_NEXTHOP)) | |
718e3744 | 1541 | { |
37fe7731 DS |
1542 | unsigned int nh_count = 0; |
1543 | unsigned int if_count = 0; | |
1544 | unsigned int max_nh_if = 0; | |
718e3744 | 1545 | |
41fc2714 | 1546 | nexthop_num = stream_getc (s); |
6878b9db DS |
1547 | zserv_nexthop_num_warn(__func__, (const struct prefix *)&p, nexthop_num); |
1548 | for (i = 0; i < nexthop_num; i++) | |
718e3744 | 1549 | { |
1550 | nexthop_type = stream_getc (s); | |
1551 | ||
1552 | switch (nexthop_type) | |
1553 | { | |
5b30316e | 1554 | case NEXTHOP_TYPE_IPV6: |
a64448ba DS |
1555 | stream_get (&nhop_addr, s, 16); |
1556 | if (nh_count < MULTIPATH_NUM) | |
1557 | { | |
1558 | /* For labeled-unicast, each nexthop is followed by label. */ | |
1559 | if (CHECK_FLAG (message, ZAPI_MESSAGE_LABEL)) | |
1560 | { | |
1561 | label = (mpls_label_t)stream_getl (s); | |
1562 | labels[nh_count++] = label; | |
1563 | } | |
1564 | nexthops[nh_count++] = nhop_addr; | |
1565 | } | |
718e3744 | 1566 | break; |
5b30316e | 1567 | case NEXTHOP_TYPE_IFINDEX: |
37fe7731 | 1568 | if (if_count < multipath_num) { |
41fc2714 DS |
1569 | ifindices[if_count++] = stream_getl (s); |
1570 | } | |
718e3744 | 1571 | break; |
5b30316e | 1572 | case NEXTHOP_TYPE_BLACKHOLE: |
a399694f | 1573 | rib_nexthop_blackhole_add (rib); |
c3c0ac83 | 1574 | break; |
718e3744 | 1575 | } |
1576 | } | |
41fc2714 DS |
1577 | |
1578 | max_nh_if = (nh_count > if_count) ? nh_count : if_count; | |
1579 | for (i = 0; i < max_nh_if; i++) | |
1580 | { | |
1581 | if ((i < nh_count) && !IN6_IS_ADDR_UNSPECIFIED (&nexthops[i])) { | |
6f20b80d | 1582 | if ((i < if_count) && ifindices[i]) |
a64448ba | 1583 | nexthop = rib_nexthop_ipv6_ifindex_add (rib, &nexthops[i], ifindices[i]); |
6f20b80d | 1584 | else |
a64448ba DS |
1585 | nexthop = rib_nexthop_ipv6_add (rib, &nexthops[i]); |
1586 | if (CHECK_FLAG (message, ZAPI_MESSAGE_LABEL)) | |
1587 | nexthop_add_labels (nexthop, nexthop->nh_label_type, 1, &labels[i]); | |
41fc2714 DS |
1588 | } |
1589 | else { | |
6f20b80d | 1590 | if ((i < if_count) && ifindices[i]) |
a399694f | 1591 | rib_nexthop_ifindex_add (rib, ifindices[i]); |
41fc2714 DS |
1592 | } |
1593 | } | |
718e3744 | 1594 | } |
1595 | ||
41fc2714 DS |
1596 | /* Distance. */ |
1597 | if (CHECK_FLAG (message, ZAPI_MESSAGE_DISTANCE)) | |
1598 | rib->distance = stream_getc (s); | |
718e3744 | 1599 | |
41fc2714 DS |
1600 | /* Metric. */ |
1601 | if (CHECK_FLAG (message, ZAPI_MESSAGE_METRIC)) | |
1602 | rib->metric = stream_getl (s); | |
718e3744 | 1603 | |
0d9551dc DS |
1604 | /* Tag */ |
1605 | if (CHECK_FLAG (message, ZAPI_MESSAGE_TAG)) | |
dc9ffce8 | 1606 | rib->tag = stream_getl (s); |
0d9551dc DS |
1607 | else |
1608 | rib->tag = 0; | |
c50ca33a TT |
1609 | |
1610 | if (CHECK_FLAG (message, ZAPI_MESSAGE_MTU)) | |
1611 | rib->mtu = stream_getl (s); | |
1612 | else | |
1613 | rib->mtu = 0; | |
0d9551dc | 1614 | |
154caaed | 1615 | /* VRF ID */ |
661512bf | 1616 | rib->vrf_id = zvrf_id (zvrf); |
d651649e | 1617 | rib->table = zvrf->table_id; |
154caaed | 1618 | |
3c7c91d0 | 1619 | ret = rib_add_multipath (AFI_IP6, safi, &p, src_pp, rib); |
04b02fda DS |
1620 | /* Stats */ |
1621 | if (ret > 0) | |
1622 | client->v6_route_add_cnt++; | |
1623 | else if (ret < 0) | |
1624 | client->v6_route_upd8_cnt++; | |
1625 | ||
719e9741 | 1626 | return 0; |
718e3744 | 1627 | } |
1628 | ||
1629 | /* Zebra server IPv6 prefix delete function. */ | |
719e9741 | 1630 | static int |
d651649e | 1631 | zread_ipv6_delete (struct zserv *client, u_short length, struct zebra_vrf *zvrf) |
718e3744 | 1632 | { |
1633 | int i; | |
1634 | struct stream *s; | |
1635 | struct zapi_ipv6 api; | |
1636 | struct in6_addr nexthop; | |
4b2792b5 | 1637 | union g_addr *pnexthop = NULL; |
718e3744 | 1638 | unsigned long ifindex; |
616368ed | 1639 | struct prefix p; |
3c7c91d0 | 1640 | struct prefix_ipv6 src_p, *src_pp; |
718e3744 | 1641 | |
1642 | s = client->ibuf; | |
1643 | ifindex = 0; | |
1644 | memset (&nexthop, 0, sizeof (struct in6_addr)); | |
1645 | ||
1646 | /* Type, flags, message. */ | |
1647 | api.type = stream_getc (s); | |
7c8ff89e | 1648 | api.instance = stream_getw (s); |
0fc452dc | 1649 | api.flags = stream_getl (s); |
718e3744 | 1650 | api.message = stream_getc (s); |
f768f367 | 1651 | api.safi = stream_getw (s); |
718e3744 | 1652 | |
1653 | /* IPv4 prefix. */ | |
1654 | memset (&p, 0, sizeof (struct prefix_ipv6)); | |
1655 | p.family = AF_INET6; | |
1656 | p.prefixlen = stream_getc (s); | |
616368ed | 1657 | stream_get (&p.u.prefix6, s, PSIZE (p.prefixlen)); |
718e3744 | 1658 | |
3c7c91d0 DL |
1659 | if (CHECK_FLAG (api.message, ZAPI_MESSAGE_SRCPFX)) |
1660 | { | |
1661 | memset (&src_p, 0, sizeof (struct prefix_ipv6)); | |
1662 | src_p.family = AF_INET6; | |
1663 | src_p.prefixlen = stream_getc (s); | |
1664 | stream_get (&src_p.prefix, s, PSIZE (src_p.prefixlen)); | |
1665 | src_pp = &src_p; | |
1666 | } | |
1667 | else | |
1668 | src_pp = NULL; | |
1669 | ||
718e3744 | 1670 | /* Nexthop, ifindex, distance, metric. */ |
1671 | if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP)) | |
1672 | { | |
1673 | u_char nexthop_type; | |
1674 | ||
1675 | api.nexthop_num = stream_getc (s); | |
1676 | for (i = 0; i < api.nexthop_num; i++) | |
1677 | { | |
1678 | nexthop_type = stream_getc (s); | |
1679 | ||
1680 | switch (nexthop_type) | |
1681 | { | |
5b30316e | 1682 | case NEXTHOP_TYPE_IPV6: |
718e3744 | 1683 | stream_get (&nexthop, s, 16); |
a64448ba DS |
1684 | /* For labeled-unicast, each nexthop is followed by label, but |
1685 | * we don't care for delete. | |
1686 | */ | |
1687 | if (CHECK_FLAG (api.message, ZAPI_MESSAGE_LABEL)) | |
1688 | stream_forward_getp (s, sizeof(u_int32_t)); | |
616368ed | 1689 | pnexthop = (union g_addr *)&nexthop; |
718e3744 | 1690 | break; |
5b30316e | 1691 | case NEXTHOP_TYPE_IFINDEX: |
718e3744 | 1692 | ifindex = stream_getl (s); |
1693 | break; | |
1694 | } | |
1695 | } | |
1696 | } | |
1697 | ||
0d9551dc | 1698 | /* Distance. */ |
718e3744 | 1699 | if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE)) |
1700 | api.distance = stream_getc (s); | |
1701 | else | |
1702 | api.distance = 0; | |
0d9551dc DS |
1703 | |
1704 | /* Metric. */ | |
718e3744 | 1705 | if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC)) |
1706 | api.metric = stream_getl (s); | |
1707 | else | |
1708 | api.metric = 0; | |
1709 | ||
0d9551dc DS |
1710 | /* tag */ |
1711 | if (CHECK_FLAG (api.message, ZAPI_MESSAGE_TAG)) | |
dc9ffce8 | 1712 | api.tag = stream_getl (s); |
0d9551dc DS |
1713 | else |
1714 | api.tag = 0; | |
1715 | ||
718e3744 | 1716 | if (IN6_IS_ADDR_UNSPECIFIED (&nexthop)) |
661512bf | 1717 | rib_delete (AFI_IP6, api.safi, zvrf_id (zvrf), api.type, api.instance, |
3c7c91d0 | 1718 | api.flags, &p, src_pp, NULL, ifindex, client->rtm_table); |
718e3744 | 1719 | else |
661512bf | 1720 | rib_delete (AFI_IP6, api.safi, zvrf_id (zvrf), api.type, api.instance, |
3c7c91d0 | 1721 | api.flags, &p, src_pp, pnexthop, ifindex, client->rtm_table); |
04b02fda DS |
1722 | |
1723 | client->v6_route_del_cnt++; | |
719e9741 | 1724 | return 0; |
718e3744 | 1725 | } |
1726 | ||
18a6dce6 | 1727 | /* Register zebra server router-id information. Send current router-id */ |
719e9741 | 1728 | static int |
d651649e | 1729 | zread_router_id_add (struct zserv *client, u_short length, struct zebra_vrf *zvrf) |
18a6dce6 | 1730 | { |
1731 | struct prefix p; | |
1732 | ||
1733 | /* Router-id information is needed. */ | |
661512bf | 1734 | vrf_bitmap_set (client->ridinfo, zvrf_id (zvrf)); |
18a6dce6 | 1735 | |
661512bf | 1736 | router_id_get (&p, zvrf_id (zvrf)); |
18a6dce6 | 1737 | |
661512bf | 1738 | return zsend_router_id_update (client, &p, zvrf_id (zvrf)); |
18a6dce6 | 1739 | } |
1740 | ||
1741 | /* Unregister zebra server router-id information. */ | |
719e9741 | 1742 | static int |
d651649e | 1743 | zread_router_id_delete (struct zserv *client, u_short length, struct zebra_vrf *zvrf) |
18a6dce6 | 1744 | { |
661512bf | 1745 | vrf_bitmap_unset (client->ridinfo, zvrf_id (zvrf)); |
719e9741 | 1746 | return 0; |
18a6dce6 | 1747 | } |
1748 | ||
2ea1ab1c VT |
1749 | /* Tie up route-type and client->sock */ |
1750 | static void | |
1751 | zread_hello (struct zserv *client) | |
1752 | { | |
1753 | /* type of protocol (lib/zebra.h) */ | |
1754 | u_char proto; | |
7c8ff89e DS |
1755 | u_short instance; |
1756 | ||
2ea1ab1c | 1757 | proto = stream_getc (client->ibuf); |
7c8ff89e | 1758 | instance = stream_getw (client->ibuf); |
2ea1ab1c VT |
1759 | |
1760 | /* accept only dynamic routing protocols */ | |
1761 | if ((proto < ZEBRA_ROUTE_MAX) | |
1762 | && (proto > ZEBRA_ROUTE_STATIC)) | |
1763 | { | |
1764 | zlog_notice ("client %d says hello and bids fair to announce only %s routes", | |
1765 | client->sock, zebra_route_string(proto)); | |
7c8ff89e DS |
1766 | if (instance) |
1767 | zlog_notice ("client protocol instance %d", instance); | |
2ea1ab1c | 1768 | |
fb018d25 | 1769 | client->proto = proto; |
7c8ff89e | 1770 | client->instance = instance; |
2ea1ab1c VT |
1771 | } |
1772 | } | |
1773 | ||
7076bb2f FL |
1774 | /* Unregister all information in a VRF. */ |
1775 | static int | |
d651649e | 1776 | zread_vrf_unregister (struct zserv *client, u_short length, struct zebra_vrf *zvrf) |
7076bb2f FL |
1777 | { |
1778 | int i; | |
1779 | afi_t afi; | |
1780 | ||
1781 | for (afi = AFI_IP; afi < AFI_MAX; afi++) | |
1782 | for (i = 0; i < ZEBRA_ROUTE_MAX; i++) | |
661512bf RW |
1783 | vrf_bitmap_unset (client->redist[afi][i], zvrf_id (zvrf)); |
1784 | vrf_bitmap_unset (client->redist_default, zvrf_id (zvrf)); | |
1785 | vrf_bitmap_unset (client->ifinfo, zvrf_id (zvrf)); | |
1786 | vrf_bitmap_unset (client->ridinfo, zvrf_id (zvrf)); | |
7076bb2f FL |
1787 | |
1788 | return 0; | |
1789 | } | |
1790 | ||
ce549947 RW |
1791 | static void |
1792 | zread_mpls_labels (int command, struct zserv *client, u_short length, | |
1793 | vrf_id_t vrf_id) | |
1794 | { | |
1795 | struct stream *s; | |
1796 | enum lsp_types_t type; | |
1797 | struct prefix prefix; | |
1798 | enum nexthop_types_t gtype; | |
1799 | union g_addr gate; | |
88d88a9c | 1800 | ifindex_t ifindex; |
ce549947 RW |
1801 | mpls_label_t in_label, out_label; |
1802 | u_int8_t distance; | |
1803 | struct zebra_vrf *zvrf; | |
1804 | ||
1805 | zvrf = vrf_info_lookup (vrf_id); | |
1806 | if (!zvrf) | |
1807 | return; | |
1808 | ||
1809 | /* Get input stream. */ | |
1810 | s = client->ibuf; | |
1811 | ||
1812 | /* Get data. */ | |
1813 | type = stream_getc (s); | |
1814 | prefix.family = stream_getl (s); | |
1815 | switch (prefix.family) | |
1816 | { | |
1817 | case AF_INET: | |
1818 | prefix.u.prefix4.s_addr = stream_get_ipv4 (s); | |
1819 | prefix.prefixlen = stream_getc (s); | |
ce549947 RW |
1820 | gate.ipv4.s_addr = stream_get_ipv4 (s); |
1821 | break; | |
1822 | case AF_INET6: | |
1823 | stream_get (&prefix.u.prefix6, s, 16); | |
1824 | prefix.prefixlen = stream_getc (s); | |
ce549947 RW |
1825 | stream_get (&gate.ipv6, s, 16); |
1826 | break; | |
1827 | default: | |
1828 | return; | |
1829 | } | |
88d88a9c | 1830 | ifindex = stream_getl (s); |
ce549947 RW |
1831 | distance = stream_getc (s); |
1832 | in_label = stream_getl (s); | |
1833 | out_label = stream_getl (s); | |
1834 | ||
88d88a9c RW |
1835 | switch (prefix.family) |
1836 | { | |
1837 | case AF_INET: | |
1838 | if (ifindex) | |
1839 | gtype = NEXTHOP_TYPE_IPV4_IFINDEX; | |
1840 | else | |
1841 | gtype = NEXTHOP_TYPE_IPV4; | |
1842 | break; | |
1843 | case AF_INET6: | |
1844 | if (ifindex) | |
1845 | gtype = NEXTHOP_TYPE_IPV6_IFINDEX; | |
1846 | else | |
1847 | gtype = NEXTHOP_TYPE_IPV6; | |
1848 | break; | |
1849 | default: | |
1850 | return; | |
1851 | } | |
1852 | ||
fe6c7157 RW |
1853 | if (! mpls_enabled) |
1854 | return; | |
1855 | ||
ce549947 RW |
1856 | if (command == ZEBRA_MPLS_LABELS_ADD) |
1857 | { | |
1858 | mpls_lsp_install (zvrf, type, in_label, out_label, gtype, &gate, | |
a64448ba | 1859 | ifindex); |
ce549947 | 1860 | if (out_label != MPLS_IMP_NULL_LABEL) |
88d88a9c RW |
1861 | mpls_ftn_update (1, zvrf, type, &prefix, gtype, &gate, ifindex, |
1862 | distance, out_label); | |
ce549947 RW |
1863 | } |
1864 | else if (command == ZEBRA_MPLS_LABELS_DELETE) | |
1865 | { | |
a64448ba | 1866 | mpls_lsp_uninstall (zvrf, type, in_label, gtype, &gate, ifindex); |
ce549947 | 1867 | if (out_label != MPLS_IMP_NULL_LABEL) |
88d88a9c RW |
1868 | mpls_ftn_update (0, zvrf, type, &prefix, gtype, &gate, ifindex, |
1869 | distance, out_label); | |
ce549947 RW |
1870 | } |
1871 | } | |
fea12efb | 1872 | /* Send response to a label manager connect request to client */ |
1873 | static int | |
1874 | zsend_label_manager_connect_response (struct zserv *client, vrf_id_t vrf_id, u_short result) | |
1875 | { | |
1876 | struct stream *s; | |
1877 | ||
1878 | s = client->obuf; | |
1879 | stream_reset (s); | |
1880 | ||
1881 | zserv_create_header (s, ZEBRA_LABEL_MANAGER_CONNECT, vrf_id); | |
1882 | ||
1883 | /* result */ | |
1884 | stream_putc (s, result); | |
1885 | ||
1886 | /* Write packet size. */ | |
1887 | stream_putw_at (s, 0, stream_get_endp (s)); | |
1888 | ||
1889 | return writen (client->sock, s->data, stream_get_endp (s)); | |
1890 | } | |
1891 | ||
1892 | static void | |
1893 | zread_label_manager_connect (struct zserv *client, vrf_id_t vrf_id) | |
1894 | { | |
1895 | struct stream *s; | |
1896 | /* type of protocol (lib/zebra.h) */ | |
1897 | u_char proto; | |
1898 | u_short instance; | |
1899 | ||
1900 | /* Get input stream. */ | |
1901 | s = client->ibuf; | |
1902 | ||
1903 | /* Get data. */ | |
1904 | proto = stream_getc (s); | |
1905 | instance = stream_getw (s); | |
1906 | ||
1907 | /* accept only dynamic routing protocols */ | |
1908 | if ((proto >= ZEBRA_ROUTE_MAX) | |
1909 | || (proto <= ZEBRA_ROUTE_STATIC)) | |
1910 | { | |
1911 | zlog_err ("client %d has wrong protocol %s", | |
1912 | client->sock, zebra_route_string(proto)); | |
1913 | zsend_label_manager_connect_response (client, vrf_id, 1); | |
1914 | return; | |
1915 | } | |
1916 | zlog_notice ("client %d with instance %u connected as %s", | |
1917 | client->sock, instance, zebra_route_string(proto)); | |
1918 | client->proto = proto; | |
1919 | client->instance = instance; | |
1920 | ||
1921 | /* | |
1922 | Release previous labels of same protocol and instance. | |
1923 | This is done in case it restarted from an unexpected shutdown. | |
1924 | */ | |
1925 | release_daemon_chunks (proto, instance); | |
1926 | ||
1927 | zlog_debug (" Label Manager client connected: sock %d, proto %s, instance %u", | |
1928 | client->sock, zebra_route_string(proto), instance); | |
1929 | /* send response back */ | |
1930 | zsend_label_manager_connect_response (client, vrf_id, 0); | |
1931 | } | |
1932 | /* Send response to a get label chunk request to client */ | |
1933 | static int | |
1934 | zsend_assign_label_chunk_response (struct zserv *client, vrf_id_t vrf_id, | |
1935 | struct label_manager_chunk *lmc) | |
1936 | { | |
1937 | struct stream *s; | |
1938 | ||
1939 | s = client->obuf; | |
1940 | stream_reset (s); | |
1941 | ||
1942 | zserv_create_header (s, ZEBRA_GET_LABEL_CHUNK, vrf_id); | |
1943 | ||
1944 | if (lmc) | |
1945 | { | |
1946 | /* keep */ | |
1947 | stream_putc (s, lmc->keep); | |
1948 | /* start and end labels */ | |
1949 | stream_putl (s, lmc->start); | |
1950 | stream_putl (s, lmc->end); | |
1951 | ||
1952 | } | |
1953 | ||
1954 | /* Write packet size. */ | |
1955 | stream_putw_at (s, 0, stream_get_endp (s)); | |
1956 | ||
1957 | return writen (client->sock, s->data, stream_get_endp (s)); | |
1958 | } | |
1959 | ||
1960 | static void | |
1961 | zread_get_label_chunk (struct zserv *client, vrf_id_t vrf_id) | |
1962 | { | |
1963 | struct stream *s; | |
1964 | u_char keep; | |
1965 | uint32_t size; | |
1966 | struct label_manager_chunk *lmc; | |
1967 | ||
1968 | /* Get input stream. */ | |
1969 | s = client->ibuf; | |
1970 | ||
1971 | /* Get data. */ | |
1972 | keep = stream_getc (s); | |
1973 | size = stream_getl (s); | |
1974 | ||
1975 | lmc = assign_label_chunk (client->proto, client->instance, keep, size); | |
1976 | if (!lmc) | |
1977 | zlog_err ("%s: Unable to assign Label Chunk of size %u", __func__, size); | |
1978 | else | |
1979 | zlog_debug ("Assigned Label Chunk %u - %u to %u", | |
1980 | lmc->start, lmc->end, keep); | |
1981 | /* send response back */ | |
1982 | zsend_assign_label_chunk_response (client, vrf_id, lmc); | |
1983 | } | |
1984 | ||
1985 | static void | |
1986 | zread_release_label_chunk (struct zserv *client) | |
1987 | { | |
1988 | struct stream *s; | |
1989 | uint32_t start, end; | |
1990 | ||
1991 | /* Get input stream. */ | |
1992 | s = client->ibuf; | |
1993 | ||
1994 | /* Get data. */ | |
1995 | start = stream_getl (s); | |
1996 | end = stream_getl (s); | |
1997 | ||
1998 | release_label_chunk (client->proto, client->instance, start, end); | |
1999 | } | |
2000 | static void | |
2001 | zread_label_manager_request (int cmd, struct zserv *client, vrf_id_t vrf_id) | |
2002 | { | |
2003 | /* to avoid sending other messages like ZERBA_INTERFACE_UP */ | |
2004 | if (cmd == ZEBRA_LABEL_MANAGER_CONNECT) | |
2005 | client->is_synchronous = 1; | |
2006 | ||
2007 | /* external label manager */ | |
2008 | if (lm_is_external) | |
2009 | { | |
2010 | if (zread_relay_label_manager_request (cmd, client) != 0) | |
2011 | zsend_label_manager_connect_response (client, vrf_id, 1); | |
2012 | } | |
2013 | /* this is a label manager */ | |
2014 | else | |
2015 | { | |
2016 | if (cmd == ZEBRA_LABEL_MANAGER_CONNECT) | |
2017 | zread_label_manager_connect (client, vrf_id); | |
2018 | else | |
2019 | { | |
2020 | /* Sanity: don't allow 'unidentified' requests */ | |
2021 | if (!client->proto) | |
2022 | { | |
2023 | zlog_err ("Got label request from an unidentified client"); | |
2024 | return; | |
2025 | } | |
2026 | if (cmd == ZEBRA_GET_LABEL_CHUNK) | |
2027 | zread_get_label_chunk (client, vrf_id); | |
2028 | else if (cmd == ZEBRA_RELEASE_LABEL_CHUNK) | |
2029 | zread_release_label_chunk (client); | |
2030 | } | |
2031 | } | |
2032 | } | |
ce549947 | 2033 | |
4b33b75a | 2034 | /* Cleanup registered nexthops (across VRFs) upon client disconnect. */ |
2035 | static void | |
2036 | zebra_client_close_cleanup_rnh (struct zserv *client) | |
2037 | { | |
1a1a7065 | 2038 | struct vrf *vrf; |
4b33b75a | 2039 | struct zebra_vrf *zvrf; |
2040 | ||
1a1a7065 | 2041 | RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id) |
4b33b75a | 2042 | { |
1a1a7065 | 2043 | if ((zvrf = vrf->info) != NULL) |
4b33b75a | 2044 | { |
661512bf RW |
2045 | zebra_cleanup_rnh_client(zvrf_id (zvrf), AF_INET, client, RNH_NEXTHOP_TYPE); |
2046 | zebra_cleanup_rnh_client(zvrf_id (zvrf), AF_INET6, client, RNH_NEXTHOP_TYPE); | |
2047 | zebra_cleanup_rnh_client(zvrf_id (zvrf), AF_INET, client, RNH_IMPORT_CHECK_TYPE); | |
2048 | zebra_cleanup_rnh_client(zvrf_id (zvrf), AF_INET6, client, RNH_IMPORT_CHECK_TYPE); | |
ce549947 RW |
2049 | if (client->proto == ZEBRA_ROUTE_LDP) |
2050 | { | |
2051 | hash_iterate(zvrf->lsp_table, mpls_ldp_lsp_uninstall_all, | |
2052 | zvrf->lsp_table); | |
2053 | mpls_ldp_ftn_uninstall_all (zvrf, AFI_IP); | |
2054 | mpls_ldp_ftn_uninstall_all (zvrf, AFI_IP6); | |
2055 | } | |
4b33b75a | 2056 | } |
2057 | } | |
2058 | } | |
2059 | ||
718e3744 | 2060 | /* Close zebra client. */ |
b9df2d25 | 2061 | static void |
718e3744 | 2062 | zebra_client_close (struct zserv *client) |
2063 | { | |
567b877d | 2064 | /* Send client de-registration to BFD */ |
2376c3f2 | 2065 | zebra_ptm_bfd_client_deregister(client->proto); |
567b877d | 2066 | |
4b33b75a | 2067 | /* Cleanup any registered nexthops - across all VRFs. */ |
2068 | zebra_client_close_cleanup_rnh (client); | |
fb018d25 | 2069 | |
fea12efb | 2070 | /* Release Label Manager chunks */ |
2071 | release_daemon_chunks (client->proto, client->instance); | |
2072 | ||
5aba114a DS |
2073 | /* Cleanup any FECs registered by this client. */ |
2074 | zebra_mpls_cleanup_fecs_for_client (vrf_info_lookup(VRF_DEFAULT), client); | |
2075 | ||
718e3744 | 2076 | /* Close file descriptor. */ |
2077 | if (client->sock) | |
2078 | { | |
7c8ff89e DS |
2079 | unsigned long nroutes; |
2080 | ||
718e3744 | 2081 | close (client->sock); |
7c8ff89e DS |
2082 | nroutes = rib_score_proto (client->proto, client->instance); |
2083 | zlog_notice ("client %d disconnected. %lu %s routes removed from the rib", | |
2084 | client->sock, nroutes, zebra_route_string (client->proto)); | |
718e3744 | 2085 | client->sock = -1; |
2086 | } | |
2087 | ||
2088 | /* Free stream buffers. */ | |
2089 | if (client->ibuf) | |
2090 | stream_free (client->ibuf); | |
2091 | if (client->obuf) | |
2092 | stream_free (client->obuf); | |
719e9741 | 2093 | if (client->wb) |
2094 | buffer_free(client->wb); | |
718e3744 | 2095 | |
2096 | /* Release threads. */ | |
2097 | if (client->t_read) | |
2098 | thread_cancel (client->t_read); | |
2099 | if (client->t_write) | |
2100 | thread_cancel (client->t_write); | |
719e9741 | 2101 | if (client->t_suicide) |
2102 | thread_cancel (client->t_suicide); | |
718e3744 | 2103 | |
2104 | /* Free client structure. */ | |
b21b19c5 | 2105 | listnode_delete (zebrad.client_list, client); |
c66f9c61 | 2106 | XFREE (MTYPE_TMP, client); |
718e3744 | 2107 | } |
2108 | ||
2109 | /* Make new client. */ | |
b9df2d25 | 2110 | static void |
718e3744 | 2111 | zebra_client_create (int sock) |
2112 | { | |
2113 | struct zserv *client; | |
7076bb2f FL |
2114 | int i; |
2115 | afi_t afi; | |
718e3744 | 2116 | |
c66f9c61 | 2117 | client = XCALLOC (MTYPE_TMP, sizeof (struct zserv)); |
718e3744 | 2118 | |
2119 | /* Make client input/output buffer. */ | |
2120 | client->sock = sock; | |
2121 | client->ibuf = stream_new (ZEBRA_MAX_PACKET_SIZ); | |
2122 | client->obuf = stream_new (ZEBRA_MAX_PACKET_SIZ); | |
719e9741 | 2123 | client->wb = buffer_new(0); |
718e3744 | 2124 | |
2125 | /* Set table number. */ | |
b21b19c5 | 2126 | client->rtm_table = zebrad.rtm_table_default; |
718e3744 | 2127 | |
cf672a86 | 2128 | client->connect_time = monotime(NULL); |
7076bb2f FL |
2129 | /* Initialize flags */ |
2130 | for (afi = AFI_IP; afi < AFI_MAX; afi++) | |
2131 | for (i = 0; i < ZEBRA_ROUTE_MAX; i++) | |
2132 | client->redist[afi][i] = vrf_bitmap_init (); | |
2133 | client->redist_default = vrf_bitmap_init (); | |
2134 | client->ifinfo = vrf_bitmap_init (); | |
2135 | client->ridinfo = vrf_bitmap_init (); | |
04b02fda | 2136 | |
fea12efb | 2137 | /* by default, it's not a synchronous client */ |
2138 | client->is_synchronous = 0; | |
2139 | ||
718e3744 | 2140 | /* Add this client to linked list. */ |
b21b19c5 | 2141 | listnode_add (zebrad.client_list, client); |
718e3744 | 2142 | |
2143 | /* Make new read thread. */ | |
2144 | zebra_event (ZEBRA_READ, sock, client); | |
12f6fb97 DS |
2145 | |
2146 | zebra_vrf_update_all (client); | |
718e3744 | 2147 | } |
2148 | ||
2149 | /* Handler of zebra service request. */ | |
b9df2d25 | 2150 | static int |
718e3744 | 2151 | zebra_client_read (struct thread *thread) |
2152 | { | |
2153 | int sock; | |
2154 | struct zserv *client; | |
57a1477b | 2155 | size_t already; |
c1b9800a | 2156 | uint16_t length, command; |
2157 | uint8_t marker, version; | |
7076bb2f | 2158 | vrf_id_t vrf_id; |
d651649e | 2159 | struct zebra_vrf *zvrf; |
718e3744 | 2160 | |
2161 | /* Get thread data. Reset reading thread because I'm running. */ | |
2162 | sock = THREAD_FD (thread); | |
2163 | client = THREAD_ARG (thread); | |
2164 | client->t_read = NULL; | |
2165 | ||
719e9741 | 2166 | if (client->t_suicide) |
718e3744 | 2167 | { |
719e9741 | 2168 | zebra_client_close(client); |
718e3744 | 2169 | return -1; |
2170 | } | |
719e9741 | 2171 | |
2172 | /* Read length and command (if we don't have it already). */ | |
57a1477b | 2173 | if ((already = stream_get_endp(client->ibuf)) < ZEBRA_HEADER_SIZE) |
719e9741 | 2174 | { |
57a1477b | 2175 | ssize_t nbyte; |
719e9741 | 2176 | if (((nbyte = stream_read_try (client->ibuf, sock, |
57a1477b | 2177 | ZEBRA_HEADER_SIZE-already)) == 0) || |
719e9741 | 2178 | (nbyte == -1)) |
2179 | { | |
2180 | if (IS_ZEBRA_DEBUG_EVENT) | |
2181 | zlog_debug ("connection closed socket [%d]", sock); | |
2182 | zebra_client_close (client); | |
2183 | return -1; | |
2184 | } | |
57a1477b | 2185 | if (nbyte != (ssize_t)(ZEBRA_HEADER_SIZE-already)) |
719e9741 | 2186 | { |
2187 | /* Try again later. */ | |
2188 | zebra_event (ZEBRA_READ, sock, client); | |
2189 | return 0; | |
2190 | } | |
57a1477b | 2191 | already = ZEBRA_HEADER_SIZE; |
719e9741 | 2192 | } |
2193 | ||
2194 | /* Reset to read from the beginning of the incoming packet. */ | |
2195 | stream_set_getp(client->ibuf, 0); | |
2196 | ||
c1b9800a | 2197 | /* Fetch header values */ |
718e3744 | 2198 | length = stream_getw (client->ibuf); |
c1b9800a | 2199 | marker = stream_getc (client->ibuf); |
2200 | version = stream_getc (client->ibuf); | |
7076bb2f | 2201 | vrf_id = stream_getw (client->ibuf); |
c1b9800a | 2202 | command = stream_getw (client->ibuf); |
718e3744 | 2203 | |
c1b9800a | 2204 | if (marker != ZEBRA_HEADER_MARKER || version != ZSERV_VERSION) |
2205 | { | |
2206 | zlog_err("%s: socket %d version mismatch, marker %d, version %d", | |
2207 | __func__, sock, marker, version); | |
2208 | zebra_client_close (client); | |
2209 | return -1; | |
2210 | } | |
719e9741 | 2211 | if (length < ZEBRA_HEADER_SIZE) |
718e3744 | 2212 | { |
57a1477b | 2213 | zlog_warn("%s: socket %d message length %u is less than header size %d", |
2214 | __func__, sock, length, ZEBRA_HEADER_SIZE); | |
2215 | zebra_client_close (client); | |
2216 | return -1; | |
2217 | } | |
2218 | if (length > STREAM_SIZE(client->ibuf)) | |
2219 | { | |
2220 | zlog_warn("%s: socket %d message length %u exceeds buffer size %lu", | |
2221 | __func__, sock, length, (u_long)STREAM_SIZE(client->ibuf)); | |
718e3744 | 2222 | zebra_client_close (client); |
2223 | return -1; | |
2224 | } | |
2225 | ||
718e3744 | 2226 | /* Read rest of data. */ |
57a1477b | 2227 | if (already < length) |
718e3744 | 2228 | { |
57a1477b | 2229 | ssize_t nbyte; |
2230 | if (((nbyte = stream_read_try (client->ibuf, sock, | |
2231 | length-already)) == 0) || | |
2232 | (nbyte == -1)) | |
718e3744 | 2233 | { |
2234 | if (IS_ZEBRA_DEBUG_EVENT) | |
b6178002 | 2235 | zlog_debug ("connection closed [%d] when reading zebra data", sock); |
718e3744 | 2236 | zebra_client_close (client); |
2237 | return -1; | |
2238 | } | |
57a1477b | 2239 | if (nbyte != (ssize_t)(length-already)) |
719e9741 | 2240 | { |
2241 | /* Try again later. */ | |
2242 | zebra_event (ZEBRA_READ, sock, client); | |
2243 | return 0; | |
2244 | } | |
718e3744 | 2245 | } |
2246 | ||
719e9741 | 2247 | length -= ZEBRA_HEADER_SIZE; |
2248 | ||
718e3744 | 2249 | /* Debug packet information. */ |
2250 | if (IS_ZEBRA_DEBUG_EVENT) | |
b6178002 | 2251 | zlog_debug ("zebra message comes from socket [%d]", sock); |
718e3744 | 2252 | |
2253 | if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV) | |
7076bb2f FL |
2254 | zlog_debug ("zebra message received [%s] %d in VRF %u", |
2255 | zserv_command_string (command), length, vrf_id); | |
718e3744 | 2256 | |
cf672a86 | 2257 | client->last_read_time = monotime(NULL); |
04b02fda DS |
2258 | client->last_read_cmd = command; |
2259 | ||
5f3d1bdf | 2260 | zvrf = zebra_vrf_lookup_by_id (vrf_id); |
d651649e DS |
2261 | if (!zvrf) |
2262 | { | |
2263 | if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV) | |
2264 | zlog_debug ("zebra received unknown VRF[%u]", vrf_id); | |
2265 | goto zclient_read_out; | |
2266 | } | |
2267 | ||
718e3744 | 2268 | switch (command) |
2269 | { | |
18a6dce6 | 2270 | case ZEBRA_ROUTER_ID_ADD: |
d651649e | 2271 | zread_router_id_add (client, length, zvrf); |
18a6dce6 | 2272 | break; |
2273 | case ZEBRA_ROUTER_ID_DELETE: | |
d651649e | 2274 | zread_router_id_delete (client, length, zvrf); |
18a6dce6 | 2275 | break; |
718e3744 | 2276 | case ZEBRA_INTERFACE_ADD: |
d651649e | 2277 | zread_interface_add (client, length, zvrf); |
718e3744 | 2278 | break; |
2279 | case ZEBRA_INTERFACE_DELETE: | |
d651649e | 2280 | zread_interface_delete (client, length, zvrf); |
718e3744 | 2281 | break; |
2282 | case ZEBRA_IPV4_ROUTE_ADD: | |
d651649e | 2283 | zread_ipv4_add (client, length, zvrf); |
718e3744 | 2284 | break; |
2285 | case ZEBRA_IPV4_ROUTE_DELETE: | |
d651649e | 2286 | zread_ipv4_delete (client, length, zvrf); |
718e3744 | 2287 | break; |
8a92a8a0 | 2288 | case ZEBRA_IPV4_ROUTE_IPV6_NEXTHOP_ADD: |
d651649e | 2289 | zread_ipv4_route_ipv6_nexthop_add (client, length, zvrf); |
8a92a8a0 | 2290 | break; |
65efcfce LB |
2291 | case ZEBRA_IPV4_NEXTHOP_ADD: |
2292 | zread_ipv4_add(client, length, zvrf); /* LB: r1.0 merge - id was 1 */ | |
2293 | break; | |
2294 | case ZEBRA_IPV4_NEXTHOP_DELETE: | |
2295 | zread_ipv4_delete(client, length, zvrf); /* LB: r1.0 merge - id was 1 */ | |
2296 | break; | |
718e3744 | 2297 | case ZEBRA_IPV6_ROUTE_ADD: |
d651649e | 2298 | zread_ipv6_add (client, length, zvrf); |
718e3744 | 2299 | break; |
2300 | case ZEBRA_IPV6_ROUTE_DELETE: | |
d651649e | 2301 | zread_ipv6_delete (client, length, zvrf); |
718e3744 | 2302 | break; |
718e3744 | 2303 | case ZEBRA_REDISTRIBUTE_ADD: |
d651649e | 2304 | zebra_redistribute_add (command, client, length, zvrf); |
718e3744 | 2305 | break; |
2306 | case ZEBRA_REDISTRIBUTE_DELETE: | |
d651649e | 2307 | zebra_redistribute_delete (command, client, length, zvrf); |
718e3744 | 2308 | break; |
2309 | case ZEBRA_REDISTRIBUTE_DEFAULT_ADD: | |
d651649e | 2310 | zebra_redistribute_default_add (command, client, length, zvrf); |
718e3744 | 2311 | break; |
2312 | case ZEBRA_REDISTRIBUTE_DEFAULT_DELETE: | |
d651649e | 2313 | zebra_redistribute_default_delete (command, client, length, zvrf); |
718e3744 | 2314 | break; |
6f61a5a3 EM |
2315 | case ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB: |
2316 | zread_ipv4_nexthop_lookup_mrib (client, length, zvrf); | |
718e3744 | 2317 | break; |
2ea1ab1c VT |
2318 | case ZEBRA_HELLO: |
2319 | zread_hello (client); | |
2320 | break; | |
fb018d25 | 2321 | case ZEBRA_NEXTHOP_REGISTER: |
d651649e | 2322 | zserv_rnh_register(client, sock, length, RNH_NEXTHOP_TYPE, zvrf); |
fb018d25 DS |
2323 | break; |
2324 | case ZEBRA_NEXTHOP_UNREGISTER: | |
d651649e | 2325 | zserv_rnh_unregister(client, sock, length, RNH_NEXTHOP_TYPE, zvrf); |
078430f6 DS |
2326 | break; |
2327 | case ZEBRA_IMPORT_ROUTE_REGISTER: | |
d651649e | 2328 | zserv_rnh_register(client, sock, length, RNH_IMPORT_CHECK_TYPE, zvrf); |
078430f6 DS |
2329 | break; |
2330 | case ZEBRA_IMPORT_ROUTE_UNREGISTER: | |
d651649e | 2331 | zserv_rnh_unregister(client, sock, length, RNH_IMPORT_CHECK_TYPE, zvrf); |
fb018d25 | 2332 | break; |
c43ed2e4 DS |
2333 | case ZEBRA_BFD_DEST_UPDATE: |
2334 | case ZEBRA_BFD_DEST_REGISTER: | |
d651649e | 2335 | zebra_ptm_bfd_dst_register(client, sock, length, command, zvrf); |
c43ed2e4 DS |
2336 | break; |
2337 | case ZEBRA_BFD_DEST_DEREGISTER: | |
d651649e | 2338 | zebra_ptm_bfd_dst_deregister(client, sock, length, zvrf); |
c43ed2e4 | 2339 | break; |
7076bb2f | 2340 | case ZEBRA_VRF_UNREGISTER: |
d651649e | 2341 | zread_vrf_unregister (client, length, zvrf); |
7076bb2f | 2342 | break; |
055c4dfc | 2343 | case ZEBRA_BFD_CLIENT_REGISTER: |
2344 | zebra_ptm_bfd_client_register(client, sock, length); | |
2345 | break; | |
4a04e5f7 | 2346 | case ZEBRA_INTERFACE_ENABLE_RADV: |
2347 | zebra_interface_radv_set (client, sock, length, zvrf, 1); | |
2348 | break; | |
2349 | case ZEBRA_INTERFACE_DISABLE_RADV: | |
2350 | zebra_interface_radv_set (client, sock, length, zvrf, 0); | |
2351 | break; | |
ce549947 RW |
2352 | case ZEBRA_MPLS_LABELS_ADD: |
2353 | case ZEBRA_MPLS_LABELS_DELETE: | |
2354 | zread_mpls_labels (command, client, length, vrf_id); | |
2355 | break; | |
e3be0432 DS |
2356 | case ZEBRA_IPMR_ROUTE_STATS: |
2357 | zebra_ipmr_route_stats (client, sock, length, zvrf); | |
2358 | break; | |
fea12efb | 2359 | case ZEBRA_LABEL_MANAGER_CONNECT: |
2360 | case ZEBRA_GET_LABEL_CHUNK: | |
2361 | case ZEBRA_RELEASE_LABEL_CHUNK: | |
2362 | zread_label_manager_request (command, client, vrf_id); | |
2363 | break; | |
5aba114a DS |
2364 | case ZEBRA_FEC_REGISTER: |
2365 | zserv_fec_register (client, sock, length); | |
2366 | break; | |
2367 | case ZEBRA_FEC_UNREGISTER: | |
2368 | zserv_fec_unregister (client, sock, length); | |
2369 | break; | |
718e3744 | 2370 | default: |
2371 | zlog_info ("Zebra received unknown command %d", command); | |
2372 | break; | |
2373 | } | |
2374 | ||
719e9741 | 2375 | if (client->t_suicide) |
2376 | { | |
2377 | /* No need to wait for thread callback, just kill immediately. */ | |
2378 | zebra_client_close(client); | |
2379 | return -1; | |
2380 | } | |
2381 | ||
d651649e | 2382 | zclient_read_out: |
718e3744 | 2383 | stream_reset (client->ibuf); |
2384 | zebra_event (ZEBRA_READ, sock, client); | |
718e3744 | 2385 | return 0; |
2386 | } | |
2387 | ||
718e3744 | 2388 | |
2389 | /* Accept code of zebra server socket. */ | |
b9df2d25 | 2390 | static int |
718e3744 | 2391 | zebra_accept (struct thread *thread) |
2392 | { | |
2393 | int accept_sock; | |
2394 | int client_sock; | |
2395 | struct sockaddr_in client; | |
2396 | socklen_t len; | |
2397 | ||
2398 | accept_sock = THREAD_FD (thread); | |
2399 | ||
719e9741 | 2400 | /* Reregister myself. */ |
2401 | zebra_event (ZEBRA_SERV, accept_sock, NULL); | |
2402 | ||
718e3744 | 2403 | len = sizeof (struct sockaddr_in); |
2404 | client_sock = accept (accept_sock, (struct sockaddr *) &client, &len); | |
2405 | ||
2406 | if (client_sock < 0) | |
2407 | { | |
6099b3b5 | 2408 | zlog_warn ("Can't accept zebra socket: %s", safe_strerror (errno)); |
718e3744 | 2409 | return -1; |
2410 | } | |
2411 | ||
ccf3557b | 2412 | /* Make client socket non-blocking. */ |
719e9741 | 2413 | set_nonblocking(client_sock); |
865b852c | 2414 | |
718e3744 | 2415 | /* Create new zebra client. */ |
2416 | zebra_client_create (client_sock); | |
2417 | ||
718e3744 | 2418 | return 0; |
2419 | } | |
2420 | ||
b9df2d25 | 2421 | #ifdef HAVE_TCP_ZEBRA |
718e3744 | 2422 | /* Make zebra's server socket. */ |
b9df2d25 | 2423 | static void |
718e3744 | 2424 | zebra_serv () |
2425 | { | |
2426 | int ret; | |
2427 | int accept_sock; | |
2428 | struct sockaddr_in addr; | |
2429 | ||
2430 | accept_sock = socket (AF_INET, SOCK_STREAM, 0); | |
2431 | ||
2432 | if (accept_sock < 0) | |
2433 | { | |
3d1dc857 | 2434 | zlog_warn ("Can't create zserv stream socket: %s", |
2435 | safe_strerror (errno)); | |
718e3744 | 2436 | zlog_warn ("zebra can't provice full functionality due to above error"); |
2437 | return; | |
2438 | } | |
2439 | ||
2440 | memset (&addr, 0, sizeof (struct sockaddr_in)); | |
2441 | addr.sin_family = AF_INET; | |
2442 | addr.sin_port = htons (ZEBRA_PORT); | |
6f0e3f6e | 2443 | #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN |
718e3744 | 2444 | addr.sin_len = sizeof (struct sockaddr_in); |
6f0e3f6e | 2445 | #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */ |
718e3744 | 2446 | addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK); |
2447 | ||
2448 | sockopt_reuseaddr (accept_sock); | |
2449 | sockopt_reuseport (accept_sock); | |
2450 | ||
edd7c245 | 2451 | if ( zserv_privs.change(ZPRIVS_RAISE) ) |
4525281a | 2452 | zlog_err("Can't raise privileges"); |
edd7c245 | 2453 | |
718e3744 | 2454 | ret = bind (accept_sock, (struct sockaddr *)&addr, |
2455 | sizeof (struct sockaddr_in)); | |
2456 | if (ret < 0) | |
2457 | { | |
3d1dc857 | 2458 | zlog_warn ("Can't bind to stream socket: %s", |
2459 | safe_strerror (errno)); | |
718e3744 | 2460 | zlog_warn ("zebra can't provice full functionality due to above error"); |
2461 | close (accept_sock); /* Avoid sd leak. */ | |
2462 | return; | |
2463 | } | |
edd7c245 | 2464 | |
2465 | if ( zserv_privs.change(ZPRIVS_LOWER) ) | |
4525281a | 2466 | zlog_err("Can't lower privileges"); |
718e3744 | 2467 | |
2468 | ret = listen (accept_sock, 1); | |
2469 | if (ret < 0) | |
2470 | { | |
3d1dc857 | 2471 | zlog_warn ("Can't listen to stream socket: %s", |
2472 | safe_strerror (errno)); | |
718e3744 | 2473 | zlog_warn ("zebra can't provice full functionality due to above error"); |
2474 | close (accept_sock); /* Avoid sd leak. */ | |
2475 | return; | |
2476 | } | |
2477 | ||
2478 | zebra_event (ZEBRA_SERV, accept_sock, NULL); | |
2479 | } | |
fbedba64 | 2480 | #else /* HAVE_TCP_ZEBRA */ |
718e3744 | 2481 | |
2482 | /* For sockaddr_un. */ | |
2483 | #include <sys/un.h> | |
2484 | ||
2485 | /* zebra server UNIX domain socket. */ | |
b9df2d25 | 2486 | static void |
fce954f8 | 2487 | zebra_serv_un (const char *path) |
718e3744 | 2488 | { |
2489 | int ret; | |
2490 | int sock, len; | |
2491 | struct sockaddr_un serv; | |
2492 | mode_t old_mask; | |
2493 | ||
2494 | /* First of all, unlink existing socket */ | |
2495 | unlink (path); | |
2496 | ||
2497 | /* Set umask */ | |
2498 | old_mask = umask (0077); | |
2499 | ||
2500 | /* Make UNIX domain socket. */ | |
2501 | sock = socket (AF_UNIX, SOCK_STREAM, 0); | |
2502 | if (sock < 0) | |
2503 | { | |
3d1dc857 | 2504 | zlog_warn ("Can't create zserv unix socket: %s", |
2505 | safe_strerror (errno)); | |
2506 | zlog_warn ("zebra can't provide full functionality due to above error"); | |
718e3744 | 2507 | return; |
2508 | } | |
2509 | ||
2510 | /* Make server socket. */ | |
2511 | memset (&serv, 0, sizeof (struct sockaddr_un)); | |
2512 | serv.sun_family = AF_UNIX; | |
2513 | strncpy (serv.sun_path, path, strlen (path)); | |
6f0e3f6e | 2514 | #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN |
718e3744 | 2515 | len = serv.sun_len = SUN_LEN(&serv); |
2516 | #else | |
2517 | len = sizeof (serv.sun_family) + strlen (serv.sun_path); | |
6f0e3f6e | 2518 | #endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */ |
718e3744 | 2519 | |
2520 | ret = bind (sock, (struct sockaddr *) &serv, len); | |
2521 | if (ret < 0) | |
2522 | { | |
3d1dc857 | 2523 | zlog_warn ("Can't bind to unix socket %s: %s", |
2524 | path, safe_strerror (errno)); | |
2525 | zlog_warn ("zebra can't provide full functionality due to above error"); | |
718e3744 | 2526 | close (sock); |
2527 | return; | |
2528 | } | |
2529 | ||
2530 | ret = listen (sock, 5); | |
2531 | if (ret < 0) | |
2532 | { | |
3d1dc857 | 2533 | zlog_warn ("Can't listen to unix socket %s: %s", |
2534 | path, safe_strerror (errno)); | |
2535 | zlog_warn ("zebra can't provide full functionality due to above error"); | |
718e3744 | 2536 | close (sock); |
2537 | return; | |
2538 | } | |
2539 | ||
2540 | umask (old_mask); | |
2541 | ||
2542 | zebra_event (ZEBRA_SERV, sock, NULL); | |
2543 | } | |
fbedba64 | 2544 | #endif /* HAVE_TCP_ZEBRA */ |
6b0655a2 | 2545 | |
718e3744 | 2546 | |
b9df2d25 | 2547 | static void |
718e3744 | 2548 | zebra_event (enum event event, int sock, struct zserv *client) |
2549 | { | |
2550 | switch (event) | |
2551 | { | |
2552 | case ZEBRA_SERV: | |
b21b19c5 | 2553 | thread_add_read (zebrad.master, zebra_accept, client, sock); |
718e3744 | 2554 | break; |
2555 | case ZEBRA_READ: | |
2556 | client->t_read = | |
b21b19c5 | 2557 | thread_add_read (zebrad.master, zebra_client_read, client, sock); |
718e3744 | 2558 | break; |
2559 | case ZEBRA_WRITE: | |
2560 | /**/ | |
2561 | break; | |
2562 | } | |
2563 | } | |
6b0655a2 | 2564 | |
04b02fda DS |
2565 | #define ZEBRA_TIME_BUF 32 |
2566 | static char * | |
2567 | zserv_time_buf(time_t *time1, char *buf, int buflen) | |
2568 | { | |
2569 | struct tm *tm; | |
2570 | time_t now; | |
2571 | ||
2572 | assert (buf != NULL); | |
2573 | assert (buflen >= ZEBRA_TIME_BUF); | |
2574 | assert (time1 != NULL); | |
2575 | ||
2576 | if (!*time1) | |
2577 | { | |
2578 | snprintf(buf, buflen, "never "); | |
2579 | return (buf); | |
2580 | } | |
2581 | ||
cf672a86 | 2582 | now = monotime(NULL); |
04b02fda DS |
2583 | now -= *time1; |
2584 | tm = gmtime(&now); | |
2585 | ||
2586 | /* Making formatted timer strings. */ | |
2587 | #define ONE_DAY_SECOND 60*60*24 | |
2588 | #define ONE_WEEK_SECOND 60*60*24*7 | |
2589 | ||
2590 | if (now < ONE_DAY_SECOND) | |
2591 | snprintf (buf, buflen, "%02d:%02d:%02d", | |
2592 | tm->tm_hour, tm->tm_min, tm->tm_sec); | |
2593 | else if (now < ONE_WEEK_SECOND) | |
2594 | snprintf (buf, buflen, "%dd%02dh%02dm", | |
2595 | tm->tm_yday, tm->tm_hour, tm->tm_min); | |
2596 | else | |
2597 | snprintf (buf, buflen, "%02dw%dd%02dh", | |
2598 | tm->tm_yday/7, tm->tm_yday - ((tm->tm_yday/7) * 7), tm->tm_hour); | |
2599 | return buf; | |
2600 | } | |
2601 | ||
2602 | static void | |
2603 | zebra_show_client_detail (struct vty *vty, struct zserv *client) | |
2604 | { | |
2605 | char cbuf[ZEBRA_TIME_BUF], rbuf[ZEBRA_TIME_BUF]; | |
2606 | char wbuf[ZEBRA_TIME_BUF], nhbuf[ZEBRA_TIME_BUF], mbuf[ZEBRA_TIME_BUF]; | |
2607 | ||
7c8ff89e DS |
2608 | vty_out (vty, "Client: %s", zebra_route_string(client->proto)); |
2609 | if (client->instance) | |
2610 | vty_out (vty, " Instance: %d", client->instance); | |
2611 | vty_out (vty, "%s", VTY_NEWLINE); | |
2612 | ||
04b02fda DS |
2613 | vty_out (vty, "------------------------ %s", VTY_NEWLINE); |
2614 | vty_out (vty, "FD: %d %s", client->sock, VTY_NEWLINE); | |
2615 | vty_out (vty, "Route Table ID: %d %s", client->rtm_table, VTY_NEWLINE); | |
2616 | ||
2617 | vty_out (vty, "Connect Time: %s %s", | |
2618 | zserv_time_buf(&client->connect_time, cbuf, ZEBRA_TIME_BUF), | |
2619 | VTY_NEWLINE); | |
2620 | if (client->nh_reg_time) | |
2621 | { | |
2622 | vty_out (vty, "Nexthop Registry Time: %s %s", | |
2623 | zserv_time_buf(&client->nh_reg_time, nhbuf, ZEBRA_TIME_BUF), | |
2624 | VTY_NEWLINE); | |
2625 | if (client->nh_last_upd_time) | |
2626 | vty_out (vty, "Nexthop Last Update Time: %s %s", | |
2627 | zserv_time_buf(&client->nh_last_upd_time, mbuf, ZEBRA_TIME_BUF), | |
2628 | VTY_NEWLINE); | |
2629 | else | |
2630 | vty_out (vty, "No Nexthop Update sent%s", VTY_NEWLINE); | |
2631 | } | |
2632 | else | |
2633 | vty_out (vty, "Not registered for Nexthop Updates%s", VTY_NEWLINE); | |
2634 | ||
2635 | vty_out (vty, "Last Msg Rx Time: %s %s", | |
2636 | zserv_time_buf(&client->last_read_time, rbuf, ZEBRA_TIME_BUF), | |
2637 | VTY_NEWLINE); | |
2638 | vty_out (vty, "Last Msg Tx Time: %s %s", | |
2639 | zserv_time_buf(&client->last_write_time, wbuf, ZEBRA_TIME_BUF), | |
2640 | VTY_NEWLINE); | |
2641 | if (client->last_read_time) | |
2642 | vty_out (vty, "Last Rcvd Cmd: %s %s", | |
2643 | zserv_command_string(client->last_read_cmd), VTY_NEWLINE); | |
2644 | if (client->last_write_time) | |
2645 | vty_out (vty, "Last Sent Cmd: %s %s", | |
2646 | zserv_command_string(client->last_write_cmd), VTY_NEWLINE); | |
2647 | vty_out (vty, "%s", VTY_NEWLINE); | |
2648 | ||
2649 | vty_out (vty, "Type Add Update Del %s", VTY_NEWLINE); | |
2650 | vty_out (vty, "================================================== %s", VTY_NEWLINE); | |
2651 | vty_out (vty, "IPv4 %-12d%-12d%-12d%s", client->v4_route_add_cnt, | |
2652 | client->v4_route_upd8_cnt, client->v4_route_del_cnt, VTY_NEWLINE); | |
2653 | vty_out (vty, "IPv6 %-12d%-12d%-12d%s", client->v6_route_add_cnt, | |
2654 | client->v6_route_upd8_cnt, client->v6_route_del_cnt, VTY_NEWLINE); | |
2655 | vty_out (vty, "Redist:v4 %-12d%-12d%-12d%s", client->redist_v4_add_cnt, 0, | |
2656 | client->redist_v4_del_cnt, VTY_NEWLINE); | |
2657 | vty_out (vty, "Redist:v6 %-12d%-12d%-12d%s", client->redist_v6_add_cnt, 0, | |
2658 | client->redist_v6_del_cnt, VTY_NEWLINE); | |
2659 | vty_out (vty, "Connected %-12d%-12d%-12d%s", client->ifadd_cnt, 0, | |
2660 | client->ifdel_cnt, VTY_NEWLINE); | |
c43ed2e4 DS |
2661 | vty_out (vty, "BFD peer %-12d%-12d%-12d%s", client->bfd_peer_add_cnt, |
2662 | client->bfd_peer_upd8_cnt, client->bfd_peer_del_cnt, VTY_NEWLINE); | |
04b02fda DS |
2663 | vty_out (vty, "Interface Up Notifications: %d%s", client->ifup_cnt, |
2664 | VTY_NEWLINE); | |
2665 | vty_out (vty, "Interface Down Notifications: %d%s", client->ifdown_cnt, | |
2666 | VTY_NEWLINE); | |
2667 | ||
2668 | vty_out (vty, "%s", VTY_NEWLINE); | |
2669 | return; | |
2670 | } | |
2671 | ||
2672 | static void | |
2673 | zebra_show_client_brief (struct vty *vty, struct zserv *client) | |
2674 | { | |
2675 | char cbuf[ZEBRA_TIME_BUF], rbuf[ZEBRA_TIME_BUF]; | |
2676 | char wbuf[ZEBRA_TIME_BUF]; | |
2677 | ||
2678 | vty_out (vty, "%-8s%12s %12s%12s%8d/%-8d%8d/%-8d%s", | |
2679 | zebra_route_string(client->proto), | |
2680 | zserv_time_buf(&client->connect_time, cbuf, ZEBRA_TIME_BUF), | |
2681 | zserv_time_buf(&client->last_read_time, rbuf, ZEBRA_TIME_BUF), | |
2682 | zserv_time_buf(&client->last_write_time, wbuf, ZEBRA_TIME_BUF), | |
2683 | client->v4_route_add_cnt+client->v4_route_upd8_cnt, | |
2684 | client->v4_route_del_cnt, | |
2685 | client->v6_route_add_cnt+client->v6_route_upd8_cnt, | |
2686 | client->v6_route_del_cnt, VTY_NEWLINE); | |
2687 | ||
2688 | } | |
2689 | ||
8ed6821e | 2690 | struct zserv * |
2691 | zebra_find_client (u_char proto) | |
2692 | { | |
2693 | struct listnode *node, *nnode; | |
2694 | struct zserv *client; | |
2695 | ||
2696 | for (ALL_LIST_ELEMENTS (zebrad.client_list, node, nnode, client)) | |
2697 | { | |
2698 | if (client->proto == proto) | |
2699 | return client; | |
2700 | } | |
2701 | ||
2702 | return NULL; | |
2703 | } | |
2704 | ||
9bf75362 | 2705 | #ifdef HAVE_NETLINK |
718e3744 | 2706 | /* Display default rtm_table for all clients. */ |
2707 | DEFUN (show_table, | |
2708 | show_table_cmd, | |
2709 | "show table", | |
2710 | SHOW_STR | |
2711 | "default routing table to use for all clients\n") | |
2712 | { | |
b21b19c5 | 2713 | vty_out (vty, "table %d%s", zebrad.rtm_table_default, |
718e3744 | 2714 | VTY_NEWLINE); |
2715 | return CMD_SUCCESS; | |
2716 | } | |
2717 | ||
6147e2c6 | 2718 | DEFUN (config_table, |
718e3744 | 2719 | config_table_cmd, |
2720 | "table TABLENO", | |
2721 | "Configure target kernel routing table\n" | |
2722 | "TABLE integer\n") | |
2723 | { | |
6af6be86 | 2724 | zebrad.rtm_table_default = strtol (argv[1]->arg, (char**)0, 10); |
718e3744 | 2725 | return CMD_SUCCESS; |
2726 | } | |
2727 | ||
813d4307 DW |
2728 | DEFUN (no_config_table, |
2729 | no_config_table_cmd, | |
6af6be86 | 2730 | "no table [TABLENO]", |
813d4307 DW |
2731 | NO_STR |
2732 | "Configure target kernel routing table\n" | |
2733 | "TABLE integer\n") | |
2734 | { | |
2735 | zebrad.rtm_table_default = 0; | |
2736 | return CMD_SUCCESS; | |
2737 | } | |
9bf75362 | 2738 | #endif |
813d4307 | 2739 | |
647e4f1f | 2740 | DEFUN (ip_forwarding, |
2741 | ip_forwarding_cmd, | |
2742 | "ip forwarding", | |
2743 | IP_STR | |
2744 | "Turn on IP forwarding") | |
2745 | { | |
2746 | int ret; | |
2747 | ||
2748 | ret = ipforward (); | |
b71f00f2 | 2749 | if (ret == 0) |
2750 | ret = ipforward_on (); | |
647e4f1f | 2751 | |
647e4f1f | 2752 | if (ret == 0) |
2753 | { | |
2754 | vty_out (vty, "Can't turn on IP forwarding%s", VTY_NEWLINE); | |
2755 | return CMD_WARNING; | |
2756 | } | |
2757 | ||
2758 | return CMD_SUCCESS; | |
2759 | } | |
2760 | ||
718e3744 | 2761 | DEFUN (no_ip_forwarding, |
2762 | no_ip_forwarding_cmd, | |
2763 | "no ip forwarding", | |
2764 | NO_STR | |
2765 | IP_STR | |
2766 | "Turn off IP forwarding") | |
2767 | { | |
2768 | int ret; | |
2769 | ||
2770 | ret = ipforward (); | |
b71f00f2 | 2771 | if (ret != 0) |
2772 | ret = ipforward_off (); | |
718e3744 | 2773 | |
718e3744 | 2774 | if (ret != 0) |
2775 | { | |
2776 | vty_out (vty, "Can't turn off IP forwarding%s", VTY_NEWLINE); | |
2777 | return CMD_WARNING; | |
2778 | } | |
2779 | ||
2780 | return CMD_SUCCESS; | |
2781 | } | |
2782 | ||
2783 | /* This command is for debugging purpose. */ | |
2784 | DEFUN (show_zebra_client, | |
2785 | show_zebra_client_cmd, | |
2786 | "show zebra client", | |
2787 | SHOW_STR | |
49d73233 | 2788 | "Zebra information\n" |
b9ee4999 | 2789 | "Client information\n") |
718e3744 | 2790 | { |
52dc7ee6 | 2791 | struct listnode *node; |
718e3744 | 2792 | struct zserv *client; |
2793 | ||
1eb8ef25 | 2794 | for (ALL_LIST_ELEMENTS_RO (zebrad.client_list, node, client)) |
04b02fda DS |
2795 | zebra_show_client_detail(vty, client); |
2796 | ||
2797 | return CMD_SUCCESS; | |
2798 | } | |
2799 | ||
2800 | /* This command is for debugging purpose. */ | |
2801 | DEFUN (show_zebra_client_summary, | |
2802 | show_zebra_client_summary_cmd, | |
2803 | "show zebra client summary", | |
2804 | SHOW_STR | |
b9ee4999 DS |
2805 | "Zebra information brief\n" |
2806 | "Client information brief\n" | |
2807 | "Brief Summary\n") | |
04b02fda DS |
2808 | { |
2809 | struct listnode *node; | |
2810 | struct zserv *client; | |
2811 | ||
2812 | vty_out (vty, "Name Connect Time Last Read Last Write IPv4 Routes IPv6 Routes %s", | |
2813 | VTY_NEWLINE); | |
2814 | vty_out (vty,"--------------------------------------------------------------------------------%s", | |
2815 | VTY_NEWLINE); | |
2816 | ||
2817 | for (ALL_LIST_ELEMENTS_RO (zebrad.client_list, node, client)) | |
2818 | zebra_show_client_brief(vty, client); | |
fb018d25 | 2819 | |
04b02fda | 2820 | vty_out (vty, "Routes column shows (added+updated)/deleted%s", VTY_NEWLINE); |
718e3744 | 2821 | return CMD_SUCCESS; |
2822 | } | |
2823 | ||
2824 | /* Table configuration write function. */ | |
b9df2d25 | 2825 | static int |
718e3744 | 2826 | config_write_table (struct vty *vty) |
2827 | { | |
b21b19c5 | 2828 | if (zebrad.rtm_table_default) |
2829 | vty_out (vty, "table %d%s", zebrad.rtm_table_default, | |
718e3744 | 2830 | VTY_NEWLINE); |
2831 | return 0; | |
2832 | } | |
2833 | ||
2834 | /* table node for routing tables. */ | |
7fc626de | 2835 | static struct cmd_node table_node = |
718e3744 | 2836 | { |
2837 | TABLE_NODE, | |
2838 | "", /* This node has no interface. */ | |
2839 | 1 | |
2840 | }; | |
6b0655a2 | 2841 | |
718e3744 | 2842 | /* Only display ip forwarding is enabled or not. */ |
2843 | DEFUN (show_ip_forwarding, | |
2844 | show_ip_forwarding_cmd, | |
2845 | "show ip forwarding", | |
2846 | SHOW_STR | |
2847 | IP_STR | |
2848 | "IP forwarding status\n") | |
2849 | { | |
2850 | int ret; | |
2851 | ||
2852 | ret = ipforward (); | |
2853 | ||
2854 | if (ret == 0) | |
2855 | vty_out (vty, "IP forwarding is off%s", VTY_NEWLINE); | |
2856 | else | |
2857 | vty_out (vty, "IP forwarding is on%s", VTY_NEWLINE); | |
2858 | return CMD_SUCCESS; | |
2859 | } | |
2860 | ||
718e3744 | 2861 | /* Only display ipv6 forwarding is enabled or not. */ |
2862 | DEFUN (show_ipv6_forwarding, | |
2863 | show_ipv6_forwarding_cmd, | |
2864 | "show ipv6 forwarding", | |
2865 | SHOW_STR | |
2866 | "IPv6 information\n" | |
2867 | "Forwarding status\n") | |
2868 | { | |
2869 | int ret; | |
2870 | ||
2871 | ret = ipforward_ipv6 (); | |
2872 | ||
2873 | switch (ret) | |
2874 | { | |
2875 | case -1: | |
2876 | vty_out (vty, "ipv6 forwarding is unknown%s", VTY_NEWLINE); | |
2877 | break; | |
2878 | case 0: | |
2879 | vty_out (vty, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE); | |
2880 | break; | |
2881 | case 1: | |
2882 | vty_out (vty, "ipv6 forwarding is %s%s", "on", VTY_NEWLINE); | |
2883 | break; | |
2884 | default: | |
2885 | vty_out (vty, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE); | |
2886 | break; | |
2887 | } | |
2888 | return CMD_SUCCESS; | |
2889 | } | |
2890 | ||
55906724 | 2891 | DEFUN (ipv6_forwarding, |
2892 | ipv6_forwarding_cmd, | |
2893 | "ipv6 forwarding", | |
2894 | IPV6_STR | |
2895 | "Turn on IPv6 forwarding") | |
2896 | { | |
2897 | int ret; | |
2898 | ||
41d3fc96 | 2899 | ret = ipforward_ipv6 (); |
b71f00f2 | 2900 | if (ret == 0) |
2901 | ret = ipforward_ipv6_on (); | |
41d3fc96 | 2902 | |
41d3fc96 | 2903 | if (ret == 0) |
55906724 | 2904 | { |
2905 | vty_out (vty, "Can't turn on IPv6 forwarding%s", VTY_NEWLINE); | |
2906 | return CMD_WARNING; | |
2907 | } | |
2908 | ||
2909 | return CMD_SUCCESS; | |
2910 | } | |
2911 | ||
718e3744 | 2912 | DEFUN (no_ipv6_forwarding, |
2913 | no_ipv6_forwarding_cmd, | |
2914 | "no ipv6 forwarding", | |
2915 | NO_STR | |
55906724 | 2916 | IPV6_STR |
2917 | "Turn off IPv6 forwarding") | |
718e3744 | 2918 | { |
2919 | int ret; | |
2920 | ||
41d3fc96 | 2921 | ret = ipforward_ipv6 (); |
b71f00f2 | 2922 | if (ret != 0) |
2923 | ret = ipforward_ipv6_off (); | |
41d3fc96 | 2924 | |
718e3744 | 2925 | if (ret != 0) |
2926 | { | |
2927 | vty_out (vty, "Can't turn off IPv6 forwarding%s", VTY_NEWLINE); | |
2928 | return CMD_WARNING; | |
2929 | } | |
2930 | ||
2931 | return CMD_SUCCESS; | |
2932 | } | |
2933 | ||
718e3744 | 2934 | /* IPForwarding configuration write function. */ |
719e9741 | 2935 | static int |
718e3744 | 2936 | config_write_forwarding (struct vty *vty) |
2937 | { | |
18a6dce6 | 2938 | /* FIXME: Find better place for that. */ |
2939 | router_id_write (vty); | |
2940 | ||
f40dd648 QY |
2941 | if (!ipforward ()) |
2942 | vty_out (vty, "no ip forwarding%s", VTY_NEWLINE); | |
f40dd648 QY |
2943 | if (!ipforward_ipv6 ()) |
2944 | vty_out (vty, "no ipv6 forwarding%s", VTY_NEWLINE); | |
718e3744 | 2945 | vty_out (vty, "!%s", VTY_NEWLINE); |
2946 | return 0; | |
2947 | } | |
2948 | ||
2949 | /* table node for routing tables. */ | |
7fc626de | 2950 | static struct cmd_node forwarding_node = |
718e3744 | 2951 | { |
2952 | FORWARDING_NODE, | |
2953 | "", /* This node has no interface. */ | |
2954 | 1 | |
2955 | }; | |
2956 | ||
718e3744 | 2957 | /* Initialisation of zebra and installation of commands. */ |
2958 | void | |
a1ac18c4 | 2959 | zebra_init (void) |
718e3744 | 2960 | { |
2961 | /* Client list init. */ | |
b21b19c5 | 2962 | zebrad.client_list = list_new (); |
718e3744 | 2963 | |
718e3744 | 2964 | /* Install configuration write function. */ |
2965 | install_node (&table_node, config_write_table); | |
2966 | install_node (&forwarding_node, config_write_forwarding); | |
2967 | ||
2968 | install_element (VIEW_NODE, &show_ip_forwarding_cmd); | |
647e4f1f | 2969 | install_element (CONFIG_NODE, &ip_forwarding_cmd); |
718e3744 | 2970 | install_element (CONFIG_NODE, &no_ip_forwarding_cmd); |
2971 | install_element (ENABLE_NODE, &show_zebra_client_cmd); | |
04b02fda | 2972 | install_element (ENABLE_NODE, &show_zebra_client_summary_cmd); |
718e3744 | 2973 | |
2974 | #ifdef HAVE_NETLINK | |
2975 | install_element (VIEW_NODE, &show_table_cmd); | |
718e3744 | 2976 | install_element (CONFIG_NODE, &config_table_cmd); |
813d4307 | 2977 | install_element (CONFIG_NODE, &no_config_table_cmd); |
718e3744 | 2978 | #endif /* HAVE_NETLINK */ |
2979 | ||
718e3744 | 2980 | install_element (VIEW_NODE, &show_ipv6_forwarding_cmd); |
55906724 | 2981 | install_element (CONFIG_NODE, &ipv6_forwarding_cmd); |
718e3744 | 2982 | install_element (CONFIG_NODE, &no_ipv6_forwarding_cmd); |
7514fb77 PJ |
2983 | |
2984 | /* Route-map */ | |
2985 | zebra_route_map_init (); | |
718e3744 | 2986 | } |
97be79f9 DO |
2987 | |
2988 | /* Make zebra server socket, wiping any existing one (see bug #403). */ | |
2989 | void | |
b5114685 | 2990 | zebra_zserv_socket_init (char *path) |
97be79f9 DO |
2991 | { |
2992 | #ifdef HAVE_TCP_ZEBRA | |
2993 | zebra_serv (); | |
2994 | #else | |
b5114685 | 2995 | zebra_serv_un (path ? path : ZEBRA_SERV_PATH); |
97be79f9 DO |
2996 | #endif /* HAVE_TCP_ZEBRA */ |
2997 | } |