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