]> git.proxmox.com Git - mirror_frr.git/blame - zebra/zserv.c
babeld/nhrpd: ignore ipv6 srcdest routes
[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
b9df2d25 595/*
5048fe14 596 * This is the new function to announce and withdraw redistributed routes, used
597 * by Zebra. This is the old zsend_route_multipath() function. That function
598 * was duplicating code to send a lot of information that was essentially thrown
599 * away or ignored by the receiver. This is the leaner function that is not a
600 * duplicate of the zapi_ipv4_route_add/del.
b9df2d25 601 *
d62a17ae 602 * The primary difference is that this function merely sends a single NH instead
603 * of
5048fe14 604 * all the nexthops.
b9df2d25 605 */
d62a17ae 606int zsend_redistribute_route(int add, struct zserv *client, struct prefix *p,
607 struct prefix *src_p, struct route_entry *re)
608{
609 afi_t afi;
610 int cmd;
611 int psize;
612 struct stream *s;
613 struct nexthop *nexthop;
614 unsigned long nhnummark = 0, messmark = 0;
615 int nhnum = 0;
616 u_char zapi_flags = 0;
617 struct nexthop dummy_nh;
618
619 afi = family2afi(p->family);
620 if (add) {
621 switch (afi) {
622 case AFI_IP:
623 cmd = ZEBRA_REDISTRIBUTE_IPV4_ADD;
624 client->redist_v4_add_cnt++;
625 break;
626 case AFI_IP6:
627 cmd = ZEBRA_REDISTRIBUTE_IPV6_ADD;
628 client->redist_v6_add_cnt++;
629 break;
630 default:
631 return -1;
632 }
633 } else {
634 switch (afi) {
635 case AFI_IP:
636 cmd = ZEBRA_REDISTRIBUTE_IPV4_DEL;
637 client->redist_v4_del_cnt++;
638 break;
639 case AFI_IP6:
640 cmd = ZEBRA_REDISTRIBUTE_IPV6_DEL;
641 client->redist_v6_del_cnt++;
642 break;
643 default:
644 return -1;
645 }
5048fe14 646 }
647
d62a17ae 648 s = client->obuf;
649 stream_reset(s);
650 memset(&dummy_nh, 0, sizeof(struct nexthop));
10fbd59a 651
d62a17ae 652 zserv_create_header(s, cmd, re->vrf_id);
653
654 /* Put type and nexthop. */
655 stream_putc(s, re->type);
656 stream_putw(s, re->instance);
657 stream_putl(s, re->flags);
658
659 /* marker for message flags field */
660 messmark = stream_get_endp(s);
661 stream_putc(s, 0);
662
663 /* Prefix. */
664 psize = PSIZE(p->prefixlen);
665 stream_putc(s, p->prefixlen);
666 stream_write(s, (u_char *)&p->u.prefix, psize);
667
668 if (src_p) {
669 SET_FLAG(zapi_flags, ZAPI_MESSAGE_SRCPFX);
670 psize = PSIZE(src_p->prefixlen);
671 stream_putc(s, src_p->prefixlen);
672 stream_write(s, (u_char *)&src_p->u.prefix, psize);
078430f6 673 }
d62a17ae 674
675 for (nexthop = re->nexthop; nexthop; nexthop = nexthop->next) {
676 /* We don't send any nexthops when there's a multipath */
677 if (re->nexthop_active_num > 1
678 && client->proto != ZEBRA_ROUTE_LDP) {
679 SET_FLAG(zapi_flags, ZAPI_MESSAGE_NEXTHOP);
680 SET_FLAG(zapi_flags, ZAPI_MESSAGE_IFINDEX);
681
682 stream_putc(s, 1);
683 if (p->family == AF_INET) {
684 stream_put_in_addr(s, &dummy_nh.gate.ipv4);
685 } else if (p->family == AF_INET6) {
686 stream_write(s, (u_char *)&dummy_nh.gate.ipv6,
687 16);
688 } else {
689 /* We don't handle anything else now, abort */
690 zlog_err(
691 "%s: Unable to redistribute route of unknown family, %d\n",
692 __func__, p->family);
693 return -1;
694 }
695 stream_putc(s, 1);
696 stream_putl(s, 0); /* dummy ifindex */
697 break;
698 }
699
700 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE)) {
701 SET_FLAG(zapi_flags, ZAPI_MESSAGE_NEXTHOP);
702 SET_FLAG(zapi_flags, ZAPI_MESSAGE_IFINDEX);
703 if (nhnummark == 0) {
704 nhnummark = stream_get_endp(s);
705 stream_putc(s, 1); /* placeholder */
706 }
707 nhnum++;
708
709 switch (nexthop->type) {
710 case NEXTHOP_TYPE_IPV4:
711 case NEXTHOP_TYPE_IPV4_IFINDEX:
712 stream_put_in_addr(s, &nexthop->gate.ipv4);
713 break;
714 case NEXTHOP_TYPE_IPV6:
715 case NEXTHOP_TYPE_IPV6_IFINDEX:
716 /* Only BGP supports IPv4 prefix with IPv6 NH,
717 * so kill this */
718 if (p->family == AF_INET)
719 stream_put_in_addr(s,
720 &dummy_nh.gate.ipv4);
721 else
722 stream_write(
723 s,
724 (u_char *)&nexthop->gate.ipv6,
725 16);
726 break;
727 default:
728 if (cmd == ZEBRA_REDISTRIBUTE_IPV4_ADD
729 || cmd == ZEBRA_REDISTRIBUTE_IPV4_DEL) {
730 struct in_addr empty;
731 memset(&empty, 0,
732 sizeof(struct in_addr));
733 stream_write(s, (u_char *)&empty,
734 IPV4_MAX_BYTELEN);
735 } else {
736 struct in6_addr empty;
737 memset(&empty, 0,
738 sizeof(struct in6_addr));
739 stream_write(s, (u_char *)&empty,
740 IPV6_MAX_BYTELEN);
741 }
742 }
743
744 /* Interface index. */
745 stream_putc(s, 1);
746 stream_putl(s, nexthop->ifindex);
747
748 /* ldpd needs all nexthops */
749 if (client->proto != ZEBRA_ROUTE_LDP)
750 break;
751 }
078430f6 752 }
d62a17ae 753
754 /* Distance */
755 SET_FLAG(zapi_flags, ZAPI_MESSAGE_DISTANCE);
756 stream_putc(s, re->distance);
757
758 /* Metric */
759 SET_FLAG(zapi_flags, ZAPI_MESSAGE_METRIC);
760 stream_putl(s, re->metric);
761
762 /* Tag */
763 if (re->tag) {
764 SET_FLAG(zapi_flags, ZAPI_MESSAGE_TAG);
765 stream_putl(s, re->tag);
078430f6
DS
766 }
767
d62a17ae 768 /* MTU */
769 SET_FLAG(zapi_flags, ZAPI_MESSAGE_MTU);
770 stream_putl(s, re->mtu);
771
772 /* write real message flags value */
773 stream_putc_at(s, messmark, zapi_flags);
774
775 /* Write next-hop number */
776 if (nhnummark)
777 stream_putc_at(s, nhnummark, nhnum);
778
779 /* Write packet size. */
780 stream_putw_at(s, 0, stream_get_endp(s));
781
782 return zebra_server_send_message(client);
783}
784
785static int zsend_write_nexthop(struct stream *s, struct nexthop *nexthop)
786{
787 stream_putc(s, nexthop->type);
788 switch (nexthop->type) {
789 case NEXTHOP_TYPE_IPV4:
790 case NEXTHOP_TYPE_IPV4_IFINDEX:
791 stream_put_in_addr(s, &nexthop->gate.ipv4);
792 stream_putl(s, nexthop->ifindex);
793 break;
794 case NEXTHOP_TYPE_IPV6:
795 stream_put(s, &nexthop->gate.ipv6, 16);
796 break;
797 case NEXTHOP_TYPE_IPV6_IFINDEX:
798 stream_put(s, &nexthop->gate.ipv6, 16);
799 stream_putl(s, nexthop->ifindex);
800 break;
801 case NEXTHOP_TYPE_IFINDEX:
802 stream_putl(s, nexthop->ifindex);
803 break;
804 default:
805 /* do nothing */
806 break;
807 }
808 return 1;
fb018d25
DS
809}
810
811/* Nexthop register */
d62a17ae 812static int zserv_rnh_register(struct zserv *client, int sock, u_short length,
813 rnh_type_t type, struct zebra_vrf *zvrf)
814{
815 struct rnh *rnh;
816 struct stream *s;
817 struct prefix p;
818 u_short l = 0;
819 u_char flags = 0;
820
821 if (IS_ZEBRA_DEBUG_NHT)
822 zlog_debug(
823 "rnh_register msg from client %s: length=%d, type=%s\n",
824 zebra_route_string(client->proto), length,
825 (type == RNH_NEXTHOP_TYPE) ? "nexthop" : "route");
826
827 s = client->ibuf;
828
829 client->nh_reg_time = monotime(NULL);
830
831 while (l < length) {
832 flags = stream_getc(s);
833 p.family = stream_getw(s);
834 p.prefixlen = stream_getc(s);
835 l += 4;
836 if (p.family == AF_INET) {
837 p.u.prefix4.s_addr = stream_get_ipv4(s);
838 l += IPV4_MAX_BYTELEN;
839 } else if (p.family == AF_INET6) {
840 stream_get(&p.u.prefix6, s, IPV6_MAX_BYTELEN);
841 l += IPV6_MAX_BYTELEN;
842 } else {
843 zlog_err(
844 "rnh_register: Received unknown family type %d\n",
845 p.family);
846 return -1;
847 }
848 rnh = zebra_add_rnh(&p, zvrf_id(zvrf), type);
849 if (type == RNH_NEXTHOP_TYPE) {
850 if (flags
851 && !CHECK_FLAG(rnh->flags, ZEBRA_NHT_CONNECTED))
852 SET_FLAG(rnh->flags, ZEBRA_NHT_CONNECTED);
853 else if (!flags
854 && CHECK_FLAG(rnh->flags, ZEBRA_NHT_CONNECTED))
855 UNSET_FLAG(rnh->flags, ZEBRA_NHT_CONNECTED);
856 } else if (type == RNH_IMPORT_CHECK_TYPE) {
857 if (flags
858 && !CHECK_FLAG(rnh->flags, ZEBRA_NHT_EXACT_MATCH))
859 SET_FLAG(rnh->flags, ZEBRA_NHT_EXACT_MATCH);
9d303b37
DL
860 else if (!flags && CHECK_FLAG(rnh->flags,
861 ZEBRA_NHT_EXACT_MATCH))
d62a17ae 862 UNSET_FLAG(rnh->flags, ZEBRA_NHT_EXACT_MATCH);
863 }
864
865 zebra_add_rnh_client(rnh, client, type, zvrf_id(zvrf));
866 /* Anything not AF_INET/INET6 has been filtered out above */
867 zebra_evaluate_rnh(zvrf_id(zvrf), p.family, 1, type, &p);
078430f6 868 }
d62a17ae 869 return 0;
870}
871
872/* Nexthop register */
873static int zserv_rnh_unregister(struct zserv *client, int sock, u_short length,
874 rnh_type_t type, struct zebra_vrf *zvrf)
875{
876 struct rnh *rnh;
877 struct stream *s;
878 struct prefix p;
879 u_short l = 0;
880
881 if (IS_ZEBRA_DEBUG_NHT)
882 zlog_debug("rnh_unregister msg from client %s: length=%d\n",
883 zebra_route_string(client->proto), length);
884
885 s = client->ibuf;
886
887 while (l < length) {
888 (void)stream_getc(
889 s); // Connected or not. Not used in this function
890 p.family = stream_getw(s);
891 p.prefixlen = stream_getc(s);
892 l += 4;
893 if (p.family == AF_INET) {
894 p.u.prefix4.s_addr = stream_get_ipv4(s);
895 l += IPV4_MAX_BYTELEN;
896 } else if (p.family == AF_INET6) {
897 stream_get(&p.u.prefix6, s, IPV6_MAX_BYTELEN);
898 l += IPV6_MAX_BYTELEN;
899 } else {
900 zlog_err(
901 "rnh_register: Received unknown family type %d\n",
902 p.family);
903 return -1;
904 }
905 rnh = zebra_lookup_rnh(&p, zvrf_id(zvrf), type);
906 if (rnh) {
907 client->nh_dereg_time = monotime(NULL);
908 zebra_remove_rnh_client(rnh, client, type);
909 }
04b02fda 910 }
d62a17ae 911 return 0;
fb018d25
DS
912}
913
b5ab78e6 914#define ZEBRA_MIN_FEC_LENGTH 5
7abc04e6 915
5aba114a 916/* FEC register */
d62a17ae 917static int zserv_fec_register(struct zserv *client, int sock, u_short length)
918{
919 struct stream *s;
920 struct zebra_vrf *zvrf;
921 u_short l = 0;
922 struct prefix p;
923 u_int16_t flags;
924 u_int32_t label_index = MPLS_INVALID_LABEL_INDEX;
925
926 s = client->ibuf;
927 zvrf = vrf_info_lookup(VRF_DEFAULT);
928 if (!zvrf)
929 return 0; // unexpected
930
931 /*
932 * The minimum amount of data that can be sent for one fec
933 * registration
934 */
935 if (length < ZEBRA_MIN_FEC_LENGTH) {
936 zlog_err(
937 "fec_register: Received a fec register of length %d, it is of insufficient size to properly decode",
938 length);
939 return -1;
940 }
941
942 while (l < length) {
943 flags = stream_getw(s);
944 p.family = stream_getw(s);
945 if (p.family != AF_INET && p.family != AF_INET6) {
946 zlog_err(
947 "fec_register: Received unknown family type %d\n",
948 p.family);
949 return -1;
950 }
951 p.prefixlen = stream_getc(s);
952 l += 5;
953 stream_get(&p.u.prefix, s, PSIZE(p.prefixlen));
954 l += PSIZE(p.prefixlen);
955 if (flags & ZEBRA_FEC_REGISTER_LABEL_INDEX) {
956 label_index = stream_getl(s);
957 l += 4;
958 } else
959 label_index = MPLS_INVALID_LABEL_INDEX;
960 zebra_mpls_fec_register(zvrf, &p, label_index, client);
961 }
962
963 return 0;
5aba114a
DS
964}
965
966/* FEC unregister */
d62a17ae 967static int zserv_fec_unregister(struct zserv *client, int sock, u_short length)
968{
969 struct stream *s;
970 struct zebra_vrf *zvrf;
971 u_short l = 0;
972 struct prefix p;
973 // u_int16_t flags;
974
975 s = client->ibuf;
976 zvrf = vrf_info_lookup(VRF_DEFAULT);
977 if (!zvrf)
978 return 0; // unexpected
979
980 /*
981 * The minimum amount of data that can be sent for one
982 * fec unregistration
983 */
984 if (length < ZEBRA_MIN_FEC_LENGTH) {
985 zlog_err(
986 "fec_unregister: Received a fec unregister of length %d, it is of insufficient size to properly decode",
987 length);
988 return -1;
989 }
990
991 while (l < length) {
992 // flags = stream_getw(s);
993 (void)stream_getw(s);
994 p.family = stream_getw(s);
995 if (p.family != AF_INET && p.family != AF_INET6) {
996 zlog_err(
997 "fec_unregister: Received unknown family type %d\n",
998 p.family);
999 return -1;
1000 }
1001 p.prefixlen = stream_getc(s);
1002 l += 5;
1003 stream_get(&p.u.prefix, s, PSIZE(p.prefixlen));
1004 l += PSIZE(p.prefixlen);
1005 zebra_mpls_fec_unregister(zvrf, &p, client);
1006 }
1007
1008 return 0;
5aba114a
DS
1009}
1010
6f61a5a3
EM
1011/*
1012 Modified version of zsend_ipv4_nexthop_lookup():
1013 Query unicast rib if nexthop is not found on mrib.
1014 Returns both route metric and protocol distance.
1015*/
d62a17ae 1016static int zsend_ipv4_nexthop_lookup_mrib(struct zserv *client,
1017 struct in_addr addr,
1018 struct route_entry *re,
1019 struct zebra_vrf *zvrf)
1020{
1021 struct stream *s;
1022 unsigned long nump;
1023 u_char num;
1024 struct nexthop *nexthop;
1025
1026 /* Get output stream. */
1027 s = client->obuf;
1028 stream_reset(s);
1029
1030 /* Fill in result. */
1031 zserv_create_header(s, ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB, zvrf_id(zvrf));
1032 stream_put_in_addr(s, &addr);
1033
1034 if (re) {
1035 stream_putc(s, re->distance);
1036 stream_putl(s, re->metric);
1037 num = 0;
1038 nump = stream_get_endp(
1039 s); /* remember position for nexthop_num */
1040 stream_putc(s, 0); /* reserve room for nexthop_num */
1041 /* Only non-recursive routes are elegible to resolve the nexthop
1042 * we
1043 * are looking up. Therefore, we will just iterate over the top
1044 * chain of nexthops. */
1045 for (nexthop = re->nexthop; nexthop; nexthop = nexthop->next)
1046 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE))
1047 num += zsend_write_nexthop(s, nexthop);
1048
1049 stream_putc_at(s, nump, num); /* store nexthop_num */
1050 } else {
1051 stream_putc(s, 0); /* distance */
1052 stream_putl(s, 0); /* metric */
1053 stream_putc(s, 0); /* nexthop_num */
1054 }
1055
1056 stream_putw_at(s, 0, stream_get_endp(s));
1057
1058 return zebra_server_send_message(client);
6f61a5a3
EM
1059}
1060
18a6dce6 1061/* Router-id is updated. Send ZEBRA_ROUTER_ID_ADD to client. */
d62a17ae 1062int zsend_router_id_update(struct zserv *client, struct prefix *p,
1063 vrf_id_t vrf_id)
18a6dce6 1064{
d62a17ae 1065 struct stream *s;
1066 int blen;
18a6dce6 1067
d62a17ae 1068 /* Check this client need interface information. */
1069 if (!vrf_bitmap_check(client->ridinfo, vrf_id))
1070 return 0;
18a6dce6 1071
d62a17ae 1072 s = client->obuf;
1073 stream_reset(s);
18a6dce6 1074
d62a17ae 1075 /* Message type. */
1076 zserv_create_header(s, ZEBRA_ROUTER_ID_UPDATE, vrf_id);
18a6dce6 1077
d62a17ae 1078 /* Prefix information. */
1079 stream_putc(s, p->family);
1080 blen = prefix_blen(p);
1081 stream_put(s, &p->u.prefix, blen);
1082 stream_putc(s, p->prefixlen);
18a6dce6 1083
d62a17ae 1084 /* Write packet size. */
1085 stream_putw_at(s, 0, stream_get_endp(s));
18a6dce6 1086
d62a17ae 1087 return zebra_server_send_message(client);
18a6dce6 1088}
6b0655a2 1089
6833ae01 1090/*
1091 * Function used by Zebra to send a PW status update to LDP daemon
1092 */
1093int zsend_pw_update(struct zserv *client, struct zebra_pw *pw)
1094{
1095 struct stream *s;
1096
1097 s = client->obuf;
1098 stream_reset(s);
1099
1100 zserv_create_header(s, ZEBRA_PW_STATUS_UPDATE, pw->vrf_id);
1101 stream_write(s, pw->ifname, IF_NAMESIZE);
1102 stream_putl(s, pw->ifindex);
1103 stream_putl(s, pw->status);
1104
1105 /* Put length at the first point of the stream. */
1106 stream_putw_at(s, 0, stream_get_endp(s));
1107
1108 return zebra_server_send_message(client);
1109}
1110
718e3744 1111/* Register zebra server interface information. Send current all
1112 interface and address information. */
d62a17ae 1113static int zread_interface_add(struct zserv *client, u_short length,
1114 struct zebra_vrf *zvrf)
718e3744 1115{
d62a17ae 1116 struct vrf *vrf;
1117 struct listnode *ifnode, *ifnnode;
1118 struct interface *ifp;
718e3744 1119
d62a17ae 1120 /* Interface information is needed. */
1121 vrf_bitmap_set(client->ifinfo, zvrf_id(zvrf));
718e3744 1122
d62a17ae 1123 RB_FOREACH(vrf, vrf_id_head, &vrfs_by_id)
1124 {
1125 for (ALL_LIST_ELEMENTS(vrf->iflist, ifnode, ifnnode, ifp)) {
1126 /* Skip pseudo interface. */
1127 if (!CHECK_FLAG(ifp->status, ZEBRA_INTERFACE_ACTIVE))
1128 continue;
718e3744 1129
d62a17ae 1130 if (zsend_interface_add(client, ifp) < 0)
1131 return -1;
718e3744 1132
d62a17ae 1133 if (zsend_interface_addresses(client, ifp) < 0)
1134 return -1;
1135 }
1136 }
1137 return 0;
718e3744 1138}
1139
1140/* Unregister zebra server interface information. */
d62a17ae 1141static int zread_interface_delete(struct zserv *client, u_short length,
1142 struct zebra_vrf *zvrf)
718e3744 1143{
d62a17ae 1144 vrf_bitmap_unset(client->ifinfo, zvrf_id(zvrf));
1145 return 0;
718e3744 1146}
1147
d62a17ae 1148void zserv_nexthop_num_warn(const char *caller, const struct prefix *p,
1149 const unsigned int nexthop_num)
6878b9db 1150{
d62a17ae 1151 if (nexthop_num > multipath_num) {
1152 char buff[PREFIX2STR_BUFFER];
1153 prefix2str(p, buff, sizeof(buff));
1154 zlog_warn(
1155 "%s: Prefix %s has %d nexthops, but we can only use the first %d",
1156 caller, buff, nexthop_num, multipath_num);
1157 }
6878b9db
DS
1158}
1159
718e3744 1160/* This function support multiple nexthop. */
d62a17ae 1161/*
f0f77c9a 1162 * Parse the ZEBRA_IPV4_ROUTE_ADD sent from client. Update re and
d62a17ae 1163 * add kernel route.
b9df2d25 1164 */
d62a17ae 1165static int zread_ipv4_add(struct zserv *client, u_short length,
1166 struct zebra_vrf *zvrf)
1167{
1168 int i;
1169 struct route_entry *re;
1170 struct prefix p;
1171 u_char message;
1172 struct in_addr nhop_addr;
1173 u_char nexthop_num;
1174 u_char nexthop_type;
1175 struct stream *s;
1176 ifindex_t ifindex;
1177 safi_t safi;
1178 int ret;
1179 mpls_label_t label;
1180 struct nexthop *nexthop;
1181
1182 /* Get input stream. */
1183 s = client->ibuf;
1184
1185 /* Allocate new re. */
1186 re = XCALLOC(MTYPE_RE, sizeof(struct route_entry));
1187
1188 /* Type, flags, message. */
1189 re->type = stream_getc(s);
1190 re->instance = stream_getw(s);
1191 re->flags = stream_getl(s);
1192 message = stream_getc(s);
1193 safi = stream_getw(s);
1194 re->uptime = time(NULL);
1195
1196 /* IPv4 prefix. */
1197 memset(&p, 0, sizeof(struct prefix_ipv4));
1198 p.family = AF_INET;
1199 p.prefixlen = stream_getc(s);
1200 stream_get(&p.u.prefix4, s, PSIZE(p.prefixlen));
1201
1202 /* VRF ID */
1203 re->vrf_id = zvrf_id(zvrf);
1204
1205 /* Nexthop parse. */
1206 if (CHECK_FLAG(message, ZAPI_MESSAGE_NEXTHOP)) {
1207 nexthop_num = stream_getc(s);
1208 zserv_nexthop_num_warn(__func__, (const struct prefix *)&p,
1209 nexthop_num);
1210
1211 for (i = 0; i < nexthop_num; i++) {
1212 nexthop_type = stream_getc(s);
1213
1214 switch (nexthop_type) {
1215 case NEXTHOP_TYPE_IFINDEX:
1216 ifindex = stream_getl(s);
1217 route_entry_nexthop_ifindex_add(re, ifindex);
1218 break;
1219 case NEXTHOP_TYPE_IPV4:
1220 nhop_addr.s_addr = stream_get_ipv4(s);
1221 nexthop = route_entry_nexthop_ipv4_add(
1222 re, &nhop_addr, NULL);
1223 /* For labeled-unicast, each nexthop is followed
1224 * by label. */
1225 if (CHECK_FLAG(message, ZAPI_MESSAGE_LABEL)) {
1226 label = (mpls_label_t)stream_getl(s);
1227 nexthop_add_labels(
1228 nexthop, nexthop->nh_label_type,
1229 1, &label);
1230 }
1231 break;
1232 case NEXTHOP_TYPE_IPV4_IFINDEX:
1233 nhop_addr.s_addr = stream_get_ipv4(s);
1234 ifindex = stream_getl(s);
1235 route_entry_nexthop_ipv4_ifindex_add(
1236 re, &nhop_addr, NULL, ifindex);
1237 break;
1238 case NEXTHOP_TYPE_IPV6:
1239 stream_forward_getp(s, IPV6_MAX_BYTELEN);
1240 break;
1241 case NEXTHOP_TYPE_BLACKHOLE:
1242 route_entry_nexthop_blackhole_add(re);
1243 break;
1244 }
1245 }
718e3744 1246 }
718e3744 1247
d62a17ae 1248 /* Distance. */
1249 if (CHECK_FLAG(message, ZAPI_MESSAGE_DISTANCE))
1250 re->distance = stream_getc(s);
718e3744 1251
d62a17ae 1252 /* Metric. */
1253 if (CHECK_FLAG(message, ZAPI_MESSAGE_METRIC))
1254 re->metric = stream_getl(s);
0d9551dc 1255
d62a17ae 1256 /* Tag */
1257 if (CHECK_FLAG(message, ZAPI_MESSAGE_TAG))
1258 re->tag = stream_getl(s);
1259 else
1260 re->tag = 0;
c50ca33a 1261
d62a17ae 1262 if (CHECK_FLAG(message, ZAPI_MESSAGE_MTU))
1263 re->mtu = stream_getl(s);
1264 else
1265 re->mtu = 0;
1266
1267 /* Table */
1268 re->table = zvrf->table_id;
12f6fb97 1269
d62a17ae 1270 ret = rib_add_multipath(AFI_IP, safi, &p, NULL, re);
04b02fda 1271
d62a17ae 1272 /* Stats */
1273 if (ret > 0)
1274 client->v4_route_add_cnt++;
1275 else if (ret < 0)
1276 client->v4_route_upd8_cnt++;
1277 return 0;
718e3744 1278}
1279
1280/* Zebra server IPv4 prefix delete function. */
d62a17ae 1281static int zread_ipv4_delete(struct zserv *client, u_short length,
1282 struct zebra_vrf *zvrf)
1283{
1284 int i;
1285 struct stream *s;
1286 struct zapi_ipv4 api;
1287 struct in_addr nexthop;
1288 union g_addr *nexthop_p;
1289 unsigned long ifindex;
1290 struct prefix p;
1291 u_char nexthop_num;
1292 u_char nexthop_type;
1293 u_int32_t table_id;
1294
1295 s = client->ibuf;
1296 ifindex = 0;
1297 nexthop.s_addr = 0;
1298 nexthop_p = NULL;
1299
1300 /* Type, flags, message. */
1301 api.type = stream_getc(s);
1302 api.instance = stream_getw(s);
1303 api.flags = stream_getl(s);
1304 api.message = stream_getc(s);
1305 api.safi = stream_getw(s);
1306
1307 /* IPv4 prefix. */
1308 memset(&p, 0, sizeof(struct prefix));
1309 p.family = AF_INET;
1310 p.prefixlen = stream_getc(s);
1311 stream_get(&p.u.prefix4, s, PSIZE(p.prefixlen));
1312
1313 /* Nexthop, ifindex, distance, metric. */
1314 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP)) {
1315 nexthop_num = stream_getc(s);
1316
1317 for (i = 0; i < nexthop_num; i++) {
1318 nexthop_type = stream_getc(s);
1319
1320 switch (nexthop_type) {
1321 case NEXTHOP_TYPE_IFINDEX:
1322 ifindex = stream_getl(s);
1323 break;
1324 case NEXTHOP_TYPE_IPV4:
1325 nexthop.s_addr = stream_get_ipv4(s);
1326 /* For labeled-unicast, each nexthop is followed
1327 * by label, but
1328 * we don't care for delete.
1329 */
1330 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_LABEL))
1331 stream_forward_getp(s,
1332 sizeof(u_int32_t));
1333 nexthop_p = (union g_addr *)&nexthop;
1334 break;
1335 case NEXTHOP_TYPE_IPV4_IFINDEX:
1336 nexthop.s_addr = stream_get_ipv4(s);
1337 nexthop_p = (union g_addr *)&nexthop;
1338 ifindex = stream_getl(s);
1339 break;
1340 case NEXTHOP_TYPE_IPV6:
1341 stream_forward_getp(s, IPV6_MAX_BYTELEN);
1342 break;
1343 }
1344 }
718e3744 1345 }
d62a17ae 1346
1347 /* Distance. */
1348 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_DISTANCE))
1349 api.distance = stream_getc(s);
1350 else
1351 api.distance = 0;
1352
1353 /* Metric. */
1354 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_METRIC))
1355 api.metric = stream_getl(s);
1356 else
1357 api.metric = 0;
1358
1359 /* tag */
1360 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_TAG))
1361 api.tag = stream_getl(s);
1362 else
1363 api.tag = 0;
1364
1365 table_id = zvrf->table_id;
1366
1367 rib_delete(AFI_IP, api.safi, zvrf_id(zvrf), api.type, api.instance,
f19435a8
DS
1368 api.flags, &p, NULL, nexthop_p, ifindex, table_id,
1369 api.metric);
d62a17ae 1370 client->v4_route_del_cnt++;
1371 return 0;
718e3744 1372}
1373
6f61a5a3 1374/* MRIB Nexthop lookup for IPv4. */
d62a17ae 1375static int zread_ipv4_nexthop_lookup_mrib(struct zserv *client, u_short length,
1376 struct zebra_vrf *zvrf)
6f61a5a3 1377{
d62a17ae 1378 struct in_addr addr;
1379 struct route_entry *re;
6f61a5a3 1380
d62a17ae 1381 addr.s_addr = stream_get_ipv4(client->ibuf);
1382 re = rib_match_ipv4_multicast(zvrf_id(zvrf), addr, NULL);
1383 return zsend_ipv4_nexthop_lookup_mrib(client, addr, re, zvrf);
6f61a5a3
EM
1384}
1385
8a92a8a0 1386/* Zebra server IPv6 prefix add function. */
d62a17ae 1387static int zread_ipv4_route_ipv6_nexthop_add(struct zserv *client,
1388 u_short length,
1389 struct zebra_vrf *zvrf)
1390{
1391 unsigned int i;
1392 struct stream *s;
1393 struct in6_addr nhop_addr;
1394 struct route_entry *re;
1395 u_char message;
1396 u_char nexthop_num;
1397 u_char nexthop_type;
1398 struct prefix p;
1399 safi_t safi;
1400 static struct in6_addr nexthops[MULTIPATH_NUM];
1401 static unsigned int ifindices[MULTIPATH_NUM];
1402 int ret;
1403 static mpls_label_t labels[MULTIPATH_NUM];
1404 mpls_label_t label;
1405 struct nexthop *nexthop;
1406
1407 /* Get input stream. */
1408 s = client->ibuf;
1409
1410 memset(&nhop_addr, 0, sizeof(struct in6_addr));
1411
1412 /* Allocate new re. */
1413 re = XCALLOC(MTYPE_RE, sizeof(struct route_entry));
1414
1415 /* Type, flags, message. */
1416 re->type = stream_getc(s);
1417 re->instance = stream_getw(s);
1418 re->flags = stream_getl(s);
1419 message = stream_getc(s);
1420 safi = stream_getw(s);
1421 re->uptime = time(NULL);
1422
1423 /* IPv4 prefix. */
1424 memset(&p, 0, sizeof(struct prefix_ipv4));
1425 p.family = AF_INET;
1426 p.prefixlen = stream_getc(s);
1427 stream_get(&p.u.prefix4, s, PSIZE(p.prefixlen));
1428
1429 /* VRF ID */
1430 re->vrf_id = zvrf_id(zvrf);
1431
1432 /* We need to give nh-addr, nh-ifindex with the same next-hop object
1433 * to the re to ensure that IPv6 multipathing works; need to coalesce
1434 * these. Clients should send the same number of paired set of
1435 * next-hop-addr/next-hop-ifindices. */
1436 if (CHECK_FLAG(message, ZAPI_MESSAGE_NEXTHOP)) {
1437 unsigned int nh_count = 0;
1438 unsigned int if_count = 0;
1439 unsigned int max_nh_if = 0;
1440
1441 nexthop_num = stream_getc(s);
1442 zserv_nexthop_num_warn(__func__, (const struct prefix *)&p,
1443 nexthop_num);
1444 for (i = 0; i < nexthop_num; i++) {
1445 nexthop_type = stream_getc(s);
1446
1447 switch (nexthop_type) {
1448 case NEXTHOP_TYPE_IPV6:
1449 stream_get(&nhop_addr, s, 16);
1450 if (nh_count < MULTIPATH_NUM) {
1451 /* For labeled-unicast, each nexthop is
1452 * followed by label. */
1453 if (CHECK_FLAG(message,
1454 ZAPI_MESSAGE_LABEL)) {
1455 label = (mpls_label_t)
1456 stream_getl(s);
1457 labels[nh_count] = label;
1458 }
1459 nexthops[nh_count] = nhop_addr;
1460 nh_count++;
1461 }
1462 break;
1463 case NEXTHOP_TYPE_IFINDEX:
1464 if (if_count < multipath_num) {
1465 ifindices[if_count++] = stream_getl(s);
1466 }
1467 break;
1468 case NEXTHOP_TYPE_BLACKHOLE:
1469 route_entry_nexthop_blackhole_add(re);
1470 break;
1471 }
1472 }
1473
1474 max_nh_if = (nh_count > if_count) ? nh_count : if_count;
1475 for (i = 0; i < max_nh_if; i++) {
1476 if ((i < nh_count)
1477 && !IN6_IS_ADDR_UNSPECIFIED(&nexthops[i])) {
1478 if ((i < if_count) && ifindices[i])
1479 nexthop =
1480 route_entry_nexthop_ipv6_ifindex_add(
1481 re, &nexthops[i],
1482 ifindices[i]);
1483 else
1484 nexthop = route_entry_nexthop_ipv6_add(
1485 re, &nexthops[i]);
1486
1487 if (CHECK_FLAG(message, ZAPI_MESSAGE_LABEL))
1488 nexthop_add_labels(
1489 nexthop, nexthop->nh_label_type,
1490 1, &labels[i]);
1491 } else {
1492 if ((i < if_count) && ifindices[i])
1493 route_entry_nexthop_ifindex_add(
1494 re, ifindices[i]);
1495 }
1496 }
8a92a8a0
DS
1497 }
1498
d62a17ae 1499 /* Distance. */
1500 if (CHECK_FLAG(message, ZAPI_MESSAGE_DISTANCE))
1501 re->distance = stream_getc(s);
41fc2714 1502
d62a17ae 1503 /* Metric. */
1504 if (CHECK_FLAG(message, ZAPI_MESSAGE_METRIC))
1505 re->metric = stream_getl(s);
1506
1507 /* Tag */
1508 if (CHECK_FLAG(message, ZAPI_MESSAGE_TAG))
1509 re->tag = stream_getl(s);
1510 else
1511 re->tag = 0;
1512
1513 if (CHECK_FLAG(message, ZAPI_MESSAGE_MTU))
1514 re->mtu = stream_getl(s);
1515 else
1516 re->mtu = 0;
1517
1518 /* Table */
1519 re->table = zvrf->table_id;
1520
1521 ret = rib_add_multipath(AFI_IP6, safi, &p, NULL, re);
1522 /* Stats */
1523 if (ret > 0)
1524 client->v4_route_add_cnt++;
1525 else if (ret < 0)
1526 client->v4_route_upd8_cnt++;
1527
1528 return 0;
1529}
1530
1531static int zread_ipv6_add(struct zserv *client, u_short length,
1532 struct zebra_vrf *zvrf)
1533{
1534 unsigned int i;
1535 struct stream *s;
1536 struct in6_addr nhop_addr;
1537 struct route_entry *re;
1538 u_char message;
1539 u_char nexthop_num;
1540 u_char nexthop_type;
1541 struct prefix p;
1542 struct prefix_ipv6 src_p, *src_pp;
1543 safi_t safi;
1544 static struct in6_addr nexthops[MULTIPATH_NUM];
1545 static unsigned int ifindices[MULTIPATH_NUM];
1546 int ret;
1547 static mpls_label_t labels[MULTIPATH_NUM];
1548 mpls_label_t label;
1549 struct nexthop *nexthop;
1550
1551 /* Get input stream. */
1552 s = client->ibuf;
1553
1554 memset(&nhop_addr, 0, sizeof(struct in6_addr));
1555
1556 /* Allocate new re. */
1557 re = XCALLOC(MTYPE_RE, sizeof(struct route_entry));
1558
1559 /* Type, flags, message. */
1560 re->type = stream_getc(s);
1561 re->instance = stream_getw(s);
1562 re->flags = stream_getl(s);
1563 message = stream_getc(s);
1564 safi = stream_getw(s);
1565 re->uptime = time(NULL);
1566
1567 /* IPv6 prefix. */
1568 memset(&p, 0, sizeof(struct prefix_ipv6));
1569 p.family = AF_INET6;
1570 p.prefixlen = stream_getc(s);
1571 stream_get(&p.u.prefix6, s, PSIZE(p.prefixlen));
1572
1573 if (CHECK_FLAG(message, ZAPI_MESSAGE_SRCPFX)) {
1574 memset(&src_p, 0, sizeof(struct prefix_ipv6));
1575 src_p.family = AF_INET6;
1576 src_p.prefixlen = stream_getc(s);
1577 stream_get(&src_p.prefix, s, PSIZE(src_p.prefixlen));
1578 src_pp = &src_p;
1579 } else
1580 src_pp = NULL;
1581
1582 /* We need to give nh-addr, nh-ifindex with the same next-hop object
1583 * to the re to ensure that IPv6 multipathing works; need to coalesce
1584 * these. Clients should send the same number of paired set of
1585 * next-hop-addr/next-hop-ifindices. */
1586 if (CHECK_FLAG(message, ZAPI_MESSAGE_NEXTHOP)) {
1587 unsigned int nh_count = 0;
1588 unsigned int if_count = 0;
1589 unsigned int max_nh_if = 0;
1590
1591 nexthop_num = stream_getc(s);
1592 zserv_nexthop_num_warn(__func__, (const struct prefix *)&p,
1593 nexthop_num);
1594 for (i = 0; i < nexthop_num; i++) {
1595 nexthop_type = stream_getc(s);
1596
1597 switch (nexthop_type) {
1598 case NEXTHOP_TYPE_IPV6:
1599 stream_get(&nhop_addr, s, 16);
1600 if (nh_count < MULTIPATH_NUM) {
1601 /* For labeled-unicast, each nexthop is
1602 * followed by label. */
1603 if (CHECK_FLAG(message,
1604 ZAPI_MESSAGE_LABEL)) {
1605 label = (mpls_label_t)
1606 stream_getl(s);
1607 labels[nh_count] = label;
1608 }
1609 nexthops[nh_count++] = nhop_addr;
1610 }
1611 break;
1612 case NEXTHOP_TYPE_IFINDEX:
1613 if (if_count < multipath_num) {
1614 ifindices[if_count++] = stream_getl(s);
1615 }
1616 break;
1617 case NEXTHOP_TYPE_BLACKHOLE:
1618 route_entry_nexthop_blackhole_add(re);
1619 break;
1620 }
1621 }
1622
1623 max_nh_if = (nh_count > if_count) ? nh_count : if_count;
1624 for (i = 0; i < max_nh_if; i++) {
1625 if ((i < nh_count)
1626 && !IN6_IS_ADDR_UNSPECIFIED(&nexthops[i])) {
1627 if ((i < if_count) && ifindices[i])
1628 nexthop =
1629 route_entry_nexthop_ipv6_ifindex_add(
1630 re, &nexthops[i],
1631 ifindices[i]);
1632 else
1633 nexthop = route_entry_nexthop_ipv6_add(
1634 re, &nexthops[i]);
1635 if (CHECK_FLAG(message, ZAPI_MESSAGE_LABEL))
1636 nexthop_add_labels(
1637 nexthop, nexthop->nh_label_type,
1638 1, &labels[i]);
1639 } else {
1640 if ((i < if_count) && ifindices[i])
1641 route_entry_nexthop_ifindex_add(
1642 re, ifindices[i]);
1643 }
1644 }
41fc2714 1645 }
718e3744 1646
d62a17ae 1647 /* Distance. */
1648 if (CHECK_FLAG(message, ZAPI_MESSAGE_DISTANCE))
1649 re->distance = stream_getc(s);
1650
1651 /* Metric. */
1652 if (CHECK_FLAG(message, ZAPI_MESSAGE_METRIC))
1653 re->metric = stream_getl(s);
718e3744 1654
d62a17ae 1655 /* Tag */
1656 if (CHECK_FLAG(message, ZAPI_MESSAGE_TAG))
1657 re->tag = stream_getl(s);
1658 else
1659 re->tag = 0;
c50ca33a 1660
d62a17ae 1661 if (CHECK_FLAG(message, ZAPI_MESSAGE_MTU))
1662 re->mtu = stream_getl(s);
1663 else
1664 re->mtu = 0;
0d9551dc 1665
d62a17ae 1666 /* VRF ID */
1667 re->vrf_id = zvrf_id(zvrf);
1668 re->table = zvrf->table_id;
154caaed 1669
d62a17ae 1670 ret = rib_add_multipath(AFI_IP6, safi, &p, src_pp, re);
1671 /* Stats */
1672 if (ret > 0)
1673 client->v6_route_add_cnt++;
1674 else if (ret < 0)
1675 client->v6_route_upd8_cnt++;
04b02fda 1676
d62a17ae 1677 return 0;
718e3744 1678}
1679
1680/* Zebra server IPv6 prefix delete function. */
d62a17ae 1681static int zread_ipv6_delete(struct zserv *client, u_short length,
1682 struct zebra_vrf *zvrf)
1683{
1684 int i;
1685 struct stream *s;
1686 struct zapi_ipv6 api;
1687 struct in6_addr nexthop;
1688 union g_addr *pnexthop = NULL;
1689 unsigned long ifindex;
1690 struct prefix p;
1691 struct prefix_ipv6 src_p, *src_pp;
1692
1693 s = client->ibuf;
1694 ifindex = 0;
1695 memset(&nexthop, 0, sizeof(struct in6_addr));
1696
1697 /* Type, flags, message. */
1698 api.type = stream_getc(s);
1699 api.instance = stream_getw(s);
1700 api.flags = stream_getl(s);
1701 api.message = stream_getc(s);
1702 api.safi = stream_getw(s);
1703
1704 /* IPv4 prefix. */
1705 memset(&p, 0, sizeof(struct prefix_ipv6));
1706 p.family = AF_INET6;
1707 p.prefixlen = stream_getc(s);
1708 stream_get(&p.u.prefix6, s, PSIZE(p.prefixlen));
1709
1710 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_SRCPFX)) {
1711 memset(&src_p, 0, sizeof(struct prefix_ipv6));
1712 src_p.family = AF_INET6;
1713 src_p.prefixlen = stream_getc(s);
1714 stream_get(&src_p.prefix, s, PSIZE(src_p.prefixlen));
1715 src_pp = &src_p;
1716 } else
1717 src_pp = NULL;
1718
1719 /* Nexthop, ifindex, distance, metric. */
1720 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_NEXTHOP)) {
1721 u_char nexthop_type;
1722
1723 api.nexthop_num = stream_getc(s);
1724 for (i = 0; i < api.nexthop_num; i++) {
1725 nexthop_type = stream_getc(s);
1726
1727 switch (nexthop_type) {
1728 case NEXTHOP_TYPE_IPV6:
1729 stream_get(&nexthop, s, 16);
1730 /* For labeled-unicast, each nexthop is followed
1731 * by label, but
1732 * we don't care for delete.
1733 */
1734 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_LABEL))
1735 stream_forward_getp(s,
1736 sizeof(u_int32_t));
1737 pnexthop = (union g_addr *)&nexthop;
1738 break;
1739 case NEXTHOP_TYPE_IFINDEX:
1740 ifindex = stream_getl(s);
1741 break;
1742 }
1743 }
718e3744 1744 }
d62a17ae 1745
1746 /* Distance. */
1747 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_DISTANCE))
1748 api.distance = stream_getc(s);
1749 else
1750 api.distance = 0;
1751
1752 /* Metric. */
1753 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_METRIC))
1754 api.metric = stream_getl(s);
1755 else
1756 api.metric = 0;
1757
1758 /* tag */
1759 if (CHECK_FLAG(api.message, ZAPI_MESSAGE_TAG))
1760 api.tag = stream_getl(s);
1761 else
1762 api.tag = 0;
1763
1764 if (IN6_IS_ADDR_UNSPECIFIED(&nexthop))
1765 rib_delete(AFI_IP6, api.safi, zvrf_id(zvrf), api.type,
1766 api.instance, api.flags, &p, src_pp, NULL, ifindex,
f19435a8 1767 client->rtm_table, api.metric);
d62a17ae 1768 else
1769 rib_delete(AFI_IP6, api.safi, zvrf_id(zvrf), api.type,
1770 api.instance, api.flags, &p, src_pp, pnexthop,
f19435a8 1771 ifindex, client->rtm_table, api.metric);
d62a17ae 1772
1773 client->v6_route_del_cnt++;
1774 return 0;
718e3744 1775}
1776
18a6dce6 1777/* Register zebra server router-id information. Send current router-id */
d62a17ae 1778static int zread_router_id_add(struct zserv *client, u_short length,
1779 struct zebra_vrf *zvrf)
18a6dce6 1780{
d62a17ae 1781 struct prefix p;
18a6dce6 1782
d62a17ae 1783 /* Router-id information is needed. */
1784 vrf_bitmap_set(client->ridinfo, zvrf_id(zvrf));
18a6dce6 1785
d62a17ae 1786 router_id_get(&p, zvrf_id(zvrf));
18a6dce6 1787
d62a17ae 1788 return zsend_router_id_update(client, &p, zvrf_id(zvrf));
18a6dce6 1789}
1790
1791/* Unregister zebra server router-id information. */
d62a17ae 1792static int zread_router_id_delete(struct zserv *client, u_short length,
1793 struct zebra_vrf *zvrf)
18a6dce6 1794{
d62a17ae 1795 vrf_bitmap_unset(client->ridinfo, zvrf_id(zvrf));
1796 return 0;
18a6dce6 1797}
1798
2ea1ab1c 1799/* Tie up route-type and client->sock */
d62a17ae 1800static void zread_hello(struct zserv *client)
2ea1ab1c 1801{
d62a17ae 1802 /* type of protocol (lib/zebra.h) */
1803 u_char proto;
1804 u_short instance;
7c8ff89e 1805
d62a17ae 1806 proto = stream_getc(client->ibuf);
1807 instance = stream_getw(client->ibuf);
2ea1ab1c 1808
d62a17ae 1809 /* accept only dynamic routing protocols */
1810 if ((proto < ZEBRA_ROUTE_MAX) && (proto > ZEBRA_ROUTE_STATIC)) {
1811 zlog_notice(
1812 "client %d says hello and bids fair to announce only %s routes",
1813 client->sock, zebra_route_string(proto));
1814 if (instance)
1815 zlog_notice("client protocol instance %d", instance);
2ea1ab1c 1816
d62a17ae 1817 client->proto = proto;
1818 client->instance = instance;
1819 }
2ea1ab1c
VT
1820}
1821
7076bb2f 1822/* Unregister all information in a VRF. */
d62a17ae 1823static int zread_vrf_unregister(struct zserv *client, u_short length,
1824 struct zebra_vrf *zvrf)
1825{
1826 int i;
1827 afi_t afi;
1828
1829 for (afi = AFI_IP; afi < AFI_MAX; afi++)
1830 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
1831 vrf_bitmap_unset(client->redist[afi][i], zvrf_id(zvrf));
1832 vrf_bitmap_unset(client->redist_default, zvrf_id(zvrf));
1833 vrf_bitmap_unset(client->ifinfo, zvrf_id(zvrf));
1834 vrf_bitmap_unset(client->ridinfo, zvrf_id(zvrf));
1835
1836 return 0;
1837}
1838
1839static void zread_mpls_labels(int command, struct zserv *client, u_short length,
1840 vrf_id_t vrf_id)
1841{
1842 struct stream *s;
1843 enum lsp_types_t type;
1844 struct prefix prefix;
1845 enum nexthop_types_t gtype;
1846 union g_addr gate;
1847 ifindex_t ifindex;
1848 mpls_label_t in_label, out_label;
1849 u_int8_t distance;
1850 struct zebra_vrf *zvrf;
1851
1852 zvrf = vrf_info_lookup(vrf_id);
1853 if (!zvrf)
1854 return;
1855
1856 /* Get input stream. */
1857 s = client->ibuf;
1858
1859 /* Get data. */
1860 type = stream_getc(s);
1861 prefix.family = stream_getl(s);
1862 switch (prefix.family) {
1863 case AF_INET:
1864 prefix.u.prefix4.s_addr = stream_get_ipv4(s);
1865 prefix.prefixlen = stream_getc(s);
1866 gate.ipv4.s_addr = stream_get_ipv4(s);
1867 break;
1868 case AF_INET6:
1869 stream_get(&prefix.u.prefix6, s, 16);
1870 prefix.prefixlen = stream_getc(s);
1871 stream_get(&gate.ipv6, s, 16);
1872 break;
1873 default:
1874 return;
1875 }
1876 ifindex = stream_getl(s);
1877 distance = stream_getc(s);
1878 in_label = stream_getl(s);
1879 out_label = stream_getl(s);
1880
1881 switch (prefix.family) {
1882 case AF_INET:
1883 if (ifindex)
1884 gtype = NEXTHOP_TYPE_IPV4_IFINDEX;
1885 else
1886 gtype = NEXTHOP_TYPE_IPV4;
1887 break;
1888 case AF_INET6:
1889 if (ifindex)
1890 gtype = NEXTHOP_TYPE_IPV6_IFINDEX;
1891 else
1892 gtype = NEXTHOP_TYPE_IPV6;
1893 break;
1894 default:
1895 return;
1896 }
1897
1898 if (!mpls_enabled)
1899 return;
1900
1901 if (command == ZEBRA_MPLS_LABELS_ADD) {
1902 mpls_lsp_install(zvrf, type, in_label, out_label, gtype, &gate,
1903 ifindex);
2f9c59f0
RW
1904 mpls_ftn_update(1, zvrf, type, &prefix, gtype, &gate, ifindex,
1905 distance, out_label);
d62a17ae 1906 } else if (command == ZEBRA_MPLS_LABELS_DELETE) {
1907 mpls_lsp_uninstall(zvrf, type, in_label, gtype, &gate, ifindex);
2f9c59f0
RW
1908 mpls_ftn_update(0, zvrf, type, &prefix, gtype, &gate, ifindex,
1909 distance, out_label);
d62a17ae 1910 }
ce549947 1911}
fea12efb 1912/* Send response to a label manager connect request to client */
d62a17ae 1913static int zsend_label_manager_connect_response(struct zserv *client,
1914 vrf_id_t vrf_id, u_short result)
fea12efb 1915{
d62a17ae 1916 struct stream *s;
fea12efb 1917
d62a17ae 1918 s = client->obuf;
1919 stream_reset(s);
fea12efb 1920
d62a17ae 1921 zserv_create_header(s, ZEBRA_LABEL_MANAGER_CONNECT, vrf_id);
fea12efb 1922
d62a17ae 1923 /* result */
1924 stream_putc(s, result);
fea12efb 1925
d62a17ae 1926 /* Write packet size. */
1927 stream_putw_at(s, 0, stream_get_endp(s));
fea12efb 1928
d62a17ae 1929 return writen(client->sock, s->data, stream_get_endp(s));
fea12efb 1930}
1931
d62a17ae 1932static void zread_label_manager_connect(struct zserv *client, vrf_id_t vrf_id)
fea12efb 1933{
d62a17ae 1934 struct stream *s;
1935 /* type of protocol (lib/zebra.h) */
1936 u_char proto;
1937 u_short instance;
fea12efb 1938
d62a17ae 1939 /* Get input stream. */
1940 s = client->ibuf;
fea12efb 1941
d62a17ae 1942 /* Get data. */
1943 proto = stream_getc(s);
1944 instance = stream_getw(s);
fea12efb 1945
d62a17ae 1946 /* accept only dynamic routing protocols */
1947 if ((proto >= ZEBRA_ROUTE_MAX) || (proto <= ZEBRA_ROUTE_STATIC)) {
1948 zlog_err("client %d has wrong protocol %s", client->sock,
1949 zebra_route_string(proto));
1950 zsend_label_manager_connect_response(client, vrf_id, 1);
1951 return;
1952 }
1953 zlog_notice("client %d with instance %u connected as %s", client->sock,
1954 instance, zebra_route_string(proto));
1955 client->proto = proto;
1956 client->instance = instance;
1957
1958 /*
1959 Release previous labels of same protocol and instance.
1960 This is done in case it restarted from an unexpected shutdown.
1961 */
1962 release_daemon_chunks(proto, instance);
1963
1964 zlog_debug(
1965 " Label Manager client connected: sock %d, proto %s, instance %u",
1966 client->sock, zebra_route_string(proto), instance);
1967 /* send response back */
1968 zsend_label_manager_connect_response(client, vrf_id, 0);
fea12efb 1969}
1970/* Send response to a get label chunk request to client */
d62a17ae 1971static int zsend_assign_label_chunk_response(struct zserv *client,
1972 vrf_id_t vrf_id,
1973 struct label_manager_chunk *lmc)
fea12efb 1974{
d62a17ae 1975 struct stream *s;
fea12efb 1976
d62a17ae 1977 s = client->obuf;
1978 stream_reset(s);
fea12efb 1979
d62a17ae 1980 zserv_create_header(s, ZEBRA_GET_LABEL_CHUNK, vrf_id);
fea12efb 1981
d62a17ae 1982 if (lmc) {
1983 /* keep */
1984 stream_putc(s, lmc->keep);
1985 /* start and end labels */
1986 stream_putl(s, lmc->start);
1987 stream_putl(s, lmc->end);
1988 }
fea12efb 1989
d62a17ae 1990 /* Write packet size. */
1991 stream_putw_at(s, 0, stream_get_endp(s));
fea12efb 1992
d62a17ae 1993 return writen(client->sock, s->data, stream_get_endp(s));
fea12efb 1994}
1995
d62a17ae 1996static void zread_get_label_chunk(struct zserv *client, vrf_id_t vrf_id)
fea12efb 1997{
d62a17ae 1998 struct stream *s;
1999 u_char keep;
2000 uint32_t size;
2001 struct label_manager_chunk *lmc;
fea12efb 2002
d62a17ae 2003 /* Get input stream. */
2004 s = client->ibuf;
fea12efb 2005
d62a17ae 2006 /* Get data. */
2007 keep = stream_getc(s);
2008 size = stream_getl(s);
fea12efb 2009
d62a17ae 2010 lmc = assign_label_chunk(client->proto, client->instance, keep, size);
2011 if (!lmc)
2012 zlog_err("%s: Unable to assign Label Chunk of size %u",
2013 __func__, size);
2014 else
2015 zlog_debug("Assigned Label Chunk %u - %u to %u", lmc->start,
2016 lmc->end, keep);
2017 /* send response back */
2018 zsend_assign_label_chunk_response(client, vrf_id, lmc);
2019}
2020
2021static void zread_release_label_chunk(struct zserv *client)
2022{
2023 struct stream *s;
2024 uint32_t start, end;
2025
2026 /* Get input stream. */
2027 s = client->ibuf;
2028
2029 /* Get data. */
2030 start = stream_getl(s);
2031 end = stream_getl(s);
2032
2033 release_label_chunk(client->proto, client->instance, start, end);
2034}
2035static void zread_label_manager_request(int cmd, struct zserv *client,
2036 vrf_id_t vrf_id)
2037{
2038 /* to avoid sending other messages like ZERBA_INTERFACE_UP */
2039 if (cmd == ZEBRA_LABEL_MANAGER_CONNECT)
2040 client->is_synchronous = 1;
2041
2042 /* external label manager */
2043 if (lm_is_external)
2044 zread_relay_label_manager_request(cmd, client, vrf_id);
2045 /* this is a label manager */
2046 else {
2047 if (cmd == ZEBRA_LABEL_MANAGER_CONNECT)
2048 zread_label_manager_connect(client, vrf_id);
2049 else {
2050 /* Sanity: don't allow 'unidentified' requests */
2051 if (!client->proto) {
2052 zlog_err(
2053 "Got label request from an unidentified client");
2054 return;
2055 }
2056 if (cmd == ZEBRA_GET_LABEL_CHUNK)
2057 zread_get_label_chunk(client, vrf_id);
2058 else if (cmd == ZEBRA_RELEASE_LABEL_CHUNK)
2059 zread_release_label_chunk(client);
2060 }
2061 }
fea12efb 2062}
2063
6833ae01 2064static int zread_pseudowire(int command, struct zserv *client, u_short length,
2065 vrf_id_t vrf_id)
2066{
2067 struct stream *s;
2068 struct zebra_vrf *zvrf;
2069 char ifname[IF_NAMESIZE];
2070 ifindex_t ifindex;
2071 int type;
2072 int af;
2073 union g_addr nexthop;
2074 uint32_t local_label;
2075 uint32_t remote_label;
2076 uint8_t flags;
2077 union pw_protocol_fields data;
2078 uint8_t protocol;
2079 struct zebra_pw *pw;
2080
2081 zvrf = vrf_info_lookup(vrf_id);
2082 if (!zvrf)
2083 return -1;
2084
2085 /* Get input stream. */
2086 s = client->ibuf;
2087
2088 /* Get data. */
2089 stream_get(ifname, s, IF_NAMESIZE);
2090 ifindex = stream_getl(s);
2091 type = stream_getl(s);
2092 af = stream_getl(s);
2093 switch (af) {
2094 case AF_INET:
2095 nexthop.ipv4.s_addr = stream_get_ipv4(s);
2096 break;
2097 case AF_INET6:
2098 stream_get(&nexthop.ipv6, s, 16);
2099 break;
2100 default:
2101 return -1;
2102 }
2103 local_label = stream_getl(s);
2104 remote_label = stream_getl(s);
2105 flags = stream_getc(s);
2106 stream_get(&data, s, sizeof(data));
2107 protocol = client->proto;
2108
2109 pw = zebra_pw_find(zvrf, ifname);
2110 switch (command) {
2111 case ZEBRA_PW_ADD:
2112 if (pw) {
2113 zlog_warn("%s: pseudowire %s already exists [%s]",
2114 __func__, ifname,
2115 zserv_command_string(command));
2116 return -1;
2117 }
2118
2119 zebra_pw_add(zvrf, ifname, protocol, client);
2120 break;
2121 case ZEBRA_PW_DELETE:
2122 if (!pw) {
2123 zlog_warn("%s: pseudowire %s not found [%s]", __func__,
2124 ifname, zserv_command_string(command));
2125 return -1;
2126 }
2127
2128 zebra_pw_del(zvrf, pw);
2129 break;
2130 case ZEBRA_PW_SET:
2131 case ZEBRA_PW_UNSET:
2132 if (!pw) {
2133 zlog_warn("%s: pseudowire %s not found [%s]", __func__,
2134 ifname, zserv_command_string(command));
2135 return -1;
2136 }
2137
2138 switch (command) {
2139 case ZEBRA_PW_SET:
2140 pw->enabled = 1;
2141 break;
2142 case ZEBRA_PW_UNSET:
2143 pw->enabled = 0;
2144 break;
2145 }
2146
2147 zebra_pw_change(pw, ifindex, type, af, &nexthop, local_label,
2148 remote_label, flags, &data);
2149 break;
2150 }
2151
2152 return 0;
2153}
2154
d62a17ae 2155/* Cleanup registered nexthops (across VRFs) upon client disconnect. */
2156static void zebra_client_close_cleanup_rnh(struct zserv *client)
fea12efb 2157{
d62a17ae 2158 struct vrf *vrf;
2159 struct zebra_vrf *zvrf;
fea12efb 2160
d62a17ae 2161 RB_FOREACH(vrf, vrf_id_head, &vrfs_by_id)
2162 {
2163 if ((zvrf = vrf->info) != NULL) {
2164 zebra_cleanup_rnh_client(zvrf_id(zvrf), AF_INET, client,
2165 RNH_NEXTHOP_TYPE);
2166 zebra_cleanup_rnh_client(zvrf_id(zvrf), AF_INET6,
2167 client, RNH_NEXTHOP_TYPE);
2168 zebra_cleanup_rnh_client(zvrf_id(zvrf), AF_INET, client,
2169 RNH_IMPORT_CHECK_TYPE);
2170 zebra_cleanup_rnh_client(zvrf_id(zvrf), AF_INET6,
2171 client, RNH_IMPORT_CHECK_TYPE);
2172 if (client->proto == ZEBRA_ROUTE_LDP) {
2173 hash_iterate(zvrf->lsp_table,
2174 mpls_ldp_lsp_uninstall_all,
2175 zvrf->lsp_table);
2176 mpls_ldp_ftn_uninstall_all(zvrf, AFI_IP);
2177 mpls_ldp_ftn_uninstall_all(zvrf, AFI_IP6);
2178 }
2179 }
2180 }
fea12efb 2181}
d62a17ae 2182
2183/* Close zebra client. */
2184static void zebra_client_close(struct zserv *client)
fea12efb 2185{
d62a17ae 2186 /* Send client de-registration to BFD */
2187 zebra_ptm_bfd_client_deregister(client->proto);
fea12efb 2188
d62a17ae 2189 /* Cleanup any registered nexthops - across all VRFs. */
2190 zebra_client_close_cleanup_rnh(client);
ce549947 2191
d62a17ae 2192 /* Release Label Manager chunks */
2193 release_daemon_chunks(client->proto, client->instance);
4b33b75a 2194
d62a17ae 2195 /* Cleanup any FECs registered by this client. */
2196 zebra_mpls_cleanup_fecs_for_client(vrf_info_lookup(VRF_DEFAULT),
2197 client);
2198
6833ae01 2199 /* Remove pseudowires associated with this client */
2200 zebra_pw_client_close(client);
2201
d62a17ae 2202 /* Close file descriptor. */
2203 if (client->sock) {
2204 unsigned long nroutes;
2205
2206 close(client->sock);
2207 nroutes = rib_score_proto(client->proto, client->instance);
2208 zlog_notice(
2209 "client %d disconnected. %lu %s routes removed from the rib",
2210 client->sock, nroutes,
2211 zebra_route_string(client->proto));
2212 client->sock = -1;
2213 }
2214
2215 /* Free stream buffers. */
2216 if (client->ibuf)
2217 stream_free(client->ibuf);
2218 if (client->obuf)
2219 stream_free(client->obuf);
2220 if (client->wb)
2221 buffer_free(client->wb);
2222
2223 /* Release threads. */
2224 if (client->t_read)
2225 thread_cancel(client->t_read);
2226 if (client->t_write)
2227 thread_cancel(client->t_write);
2228 if (client->t_suicide)
2229 thread_cancel(client->t_suicide);
2230
2231 /* Free bitmaps. */
2232 for (afi_t afi = AFI_IP; afi < AFI_MAX; afi++)
2233 for (int i = 0; i < ZEBRA_ROUTE_MAX; i++)
2234 vrf_bitmap_free(client->redist[afi][i]);
2235
2236 vrf_bitmap_free(client->redist_default);
2237 vrf_bitmap_free(client->ifinfo);
2238 vrf_bitmap_free(client->ridinfo);
2239
2240 /* Free client structure. */
2241 listnode_delete(zebrad.client_list, client);
2242 XFREE(MTYPE_TMP, client);
718e3744 2243}
2244
2245/* Make new client. */
d62a17ae 2246static void zebra_client_create(int sock)
718e3744 2247{
d62a17ae 2248 struct zserv *client;
2249 int i;
2250 afi_t afi;
2251
2252 client = XCALLOC(MTYPE_TMP, sizeof(struct zserv));
718e3744 2253
d62a17ae 2254 /* Make client input/output buffer. */
2255 client->sock = sock;
2256 client->ibuf = stream_new(ZEBRA_MAX_PACKET_SIZ);
2257 client->obuf = stream_new(ZEBRA_MAX_PACKET_SIZ);
2258 client->wb = buffer_new(0);
718e3744 2259
d62a17ae 2260 /* Set table number. */
2261 client->rtm_table = zebrad.rtm_table_default;
718e3744 2262
d62a17ae 2263 client->connect_time = monotime(NULL);
2264 /* Initialize flags */
2265 for (afi = AFI_IP; afi < AFI_MAX; afi++)
2266 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
2267 client->redist[afi][i] = vrf_bitmap_init();
2268 client->redist_default = vrf_bitmap_init();
2269 client->ifinfo = vrf_bitmap_init();
2270 client->ridinfo = vrf_bitmap_init();
718e3744 2271
d62a17ae 2272 /* by default, it's not a synchronous client */
2273 client->is_synchronous = 0;
04b02fda 2274
d62a17ae 2275 /* Add this client to linked list. */
2276 listnode_add(zebrad.client_list, client);
fea12efb 2277
d62a17ae 2278 /* Make new read thread. */
2279 zebra_event(ZEBRA_READ, sock, client);
12f6fb97 2280
d62a17ae 2281 zebra_vrf_update_all(client);
718e3744 2282}
2283
e0ae31b8
DS
2284static int zread_interface_set_master(struct zserv *client, int sock,
2285 u_short length)
2286{
2287 struct interface *master;
2288 struct interface *slave;
2289 struct stream *s = client->ibuf;
2290 int ifindex;
2291 vrf_id_t vrf_id;
2292
2293 vrf_id = stream_getw(s);
2294 ifindex = stream_getl(s);
2295 master = if_lookup_by_index(ifindex, vrf_id);
2296
2297 vrf_id = stream_getw(s);
2298 ifindex = stream_getl(s);
2299 slave = if_lookup_by_index(ifindex, vrf_id);
2300
2301 if (!master || !slave)
2302 return 0;
2303
2304 kernel_interface_set_master(master, slave);
2305
2306 return 1;
2307}
2308
718e3744 2309/* Handler of zebra service request. */
d62a17ae 2310static int zebra_client_read(struct thread *thread)
2311{
2312 int sock;
2313 struct zserv *client;
2314 size_t already;
2315 uint16_t length, command;
2316 uint8_t marker, version;
2317 vrf_id_t vrf_id;
2318 struct zebra_vrf *zvrf;
2319
2320 /* Get thread data. Reset reading thread because I'm running. */
2321 sock = THREAD_FD(thread);
2322 client = THREAD_ARG(thread);
2323 client->t_read = NULL;
2324
2325 if (client->t_suicide) {
2326 zebra_client_close(client);
2327 return -1;
719e9741 2328 }
d62a17ae 2329
2330 /* Read length and command (if we don't have it already). */
2331 if ((already = stream_get_endp(client->ibuf)) < ZEBRA_HEADER_SIZE) {
2332 ssize_t nbyte;
2333 if (((nbyte = stream_read_try(client->ibuf, sock,
2334 ZEBRA_HEADER_SIZE - already))
2335 == 0)
2336 || (nbyte == -1)) {
2337 if (IS_ZEBRA_DEBUG_EVENT)
2338 zlog_debug("connection closed socket [%d]",
2339 sock);
2340 zebra_client_close(client);
2341 return -1;
2342 }
2343 if (nbyte != (ssize_t)(ZEBRA_HEADER_SIZE - already)) {
2344 /* Try again later. */
2345 zebra_event(ZEBRA_READ, sock, client);
2346 return 0;
2347 }
2348 already = ZEBRA_HEADER_SIZE;
719e9741 2349 }
d62a17ae 2350
2351 /* Reset to read from the beginning of the incoming packet. */
2352 stream_set_getp(client->ibuf, 0);
2353
2354 /* Fetch header values */
2355 length = stream_getw(client->ibuf);
2356 marker = stream_getc(client->ibuf);
2357 version = stream_getc(client->ibuf);
2358 vrf_id = stream_getw(client->ibuf);
2359 command = stream_getw(client->ibuf);
2360
2361 if (marker != ZEBRA_HEADER_MARKER || version != ZSERV_VERSION) {
2362 zlog_err(
2363 "%s: socket %d version mismatch, marker %d, version %d",
2364 __func__, sock, marker, version);
2365 zebra_client_close(client);
2366 return -1;
2367 }
2368 if (length < ZEBRA_HEADER_SIZE) {
2369 zlog_warn(
2370 "%s: socket %d message length %u is less than header size %d",
2371 __func__, sock, length, ZEBRA_HEADER_SIZE);
2372 zebra_client_close(client);
2373 return -1;
2374 }
2375 if (length > STREAM_SIZE(client->ibuf)) {
2376 zlog_warn(
2377 "%s: socket %d message length %u exceeds buffer size %lu",
2378 __func__, sock, length,
2379 (u_long)STREAM_SIZE(client->ibuf));
2380 zebra_client_close(client);
2381 return -1;
718e3744 2382 }
d62a17ae 2383
2384 /* Read rest of data. */
2385 if (already < length) {
2386 ssize_t nbyte;
2387 if (((nbyte = stream_read_try(client->ibuf, sock,
2388 length - already))
2389 == 0)
2390 || (nbyte == -1)) {
2391 if (IS_ZEBRA_DEBUG_EVENT)
2392 zlog_debug(
2393 "connection closed [%d] when reading zebra data",
2394 sock);
2395 zebra_client_close(client);
2396 return -1;
2397 }
2398 if (nbyte != (ssize_t)(length - already)) {
2399 /* Try again later. */
2400 zebra_event(ZEBRA_READ, sock, client);
2401 return 0;
2402 }
2403 }
2404
2405 length -= ZEBRA_HEADER_SIZE;
2406
2407 /* Debug packet information. */
2408 if (IS_ZEBRA_DEBUG_EVENT)
2409 zlog_debug("zebra message comes from socket [%d]", sock);
2410
2411 if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
2412 zlog_debug("zebra message received [%s] %d in VRF %u",
2413 zserv_command_string(command), length, vrf_id);
2414
2415 client->last_read_time = monotime(NULL);
2416 client->last_read_cmd = command;
2417
2418 zvrf = zebra_vrf_lookup_by_id(vrf_id);
2419 if (!zvrf) {
2420 if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
2421 zlog_debug("zebra received unknown VRF[%u]", vrf_id);
2422 goto zclient_read_out;
719e9741 2423 }
d62a17ae 2424
2425 switch (command) {
2426 case ZEBRA_ROUTER_ID_ADD:
2427 zread_router_id_add(client, length, zvrf);
2428 break;
2429 case ZEBRA_ROUTER_ID_DELETE:
2430 zread_router_id_delete(client, length, zvrf);
2431 break;
2432 case ZEBRA_INTERFACE_ADD:
2433 zread_interface_add(client, length, zvrf);
2434 break;
2435 case ZEBRA_INTERFACE_DELETE:
2436 zread_interface_delete(client, length, zvrf);
2437 break;
2438 case ZEBRA_IPV4_ROUTE_ADD:
2439 zread_ipv4_add(client, length, zvrf);
2440 break;
2441 case ZEBRA_IPV4_ROUTE_DELETE:
2442 zread_ipv4_delete(client, length, zvrf);
2443 break;
2444 case ZEBRA_IPV4_ROUTE_IPV6_NEXTHOP_ADD:
2445 zread_ipv4_route_ipv6_nexthop_add(client, length, zvrf);
2446 break;
d62a17ae 2447 case ZEBRA_IPV6_ROUTE_ADD:
2448 zread_ipv6_add(client, length, zvrf);
2449 break;
2450 case ZEBRA_IPV6_ROUTE_DELETE:
2451 zread_ipv6_delete(client, length, zvrf);
2452 break;
2453 case ZEBRA_REDISTRIBUTE_ADD:
2454 zebra_redistribute_add(command, client, length, zvrf);
2455 break;
2456 case ZEBRA_REDISTRIBUTE_DELETE:
2457 zebra_redistribute_delete(command, client, length, zvrf);
2458 break;
2459 case ZEBRA_REDISTRIBUTE_DEFAULT_ADD:
2460 zebra_redistribute_default_add(command, client, length, zvrf);
2461 break;
2462 case ZEBRA_REDISTRIBUTE_DEFAULT_DELETE:
2463 zebra_redistribute_default_delete(command, client, length,
2464 zvrf);
2465 break;
2466 case ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB:
2467 zread_ipv4_nexthop_lookup_mrib(client, length, zvrf);
2468 break;
2469 case ZEBRA_HELLO:
2470 zread_hello(client);
2471 break;
2472 case ZEBRA_NEXTHOP_REGISTER:
2473 zserv_rnh_register(client, sock, length, RNH_NEXTHOP_TYPE,
2474 zvrf);
2475 break;
2476 case ZEBRA_NEXTHOP_UNREGISTER:
2477 zserv_rnh_unregister(client, sock, length, RNH_NEXTHOP_TYPE,
2478 zvrf);
2479 break;
2480 case ZEBRA_IMPORT_ROUTE_REGISTER:
2481 zserv_rnh_register(client, sock, length, RNH_IMPORT_CHECK_TYPE,
2482 zvrf);
2483 break;
2484 case ZEBRA_IMPORT_ROUTE_UNREGISTER:
2485 zserv_rnh_unregister(client, sock, length,
2486 RNH_IMPORT_CHECK_TYPE, zvrf);
2487 break;
2488 case ZEBRA_BFD_DEST_UPDATE:
2489 case ZEBRA_BFD_DEST_REGISTER:
2490 zebra_ptm_bfd_dst_register(client, sock, length, command, zvrf);
2491 break;
2492 case ZEBRA_BFD_DEST_DEREGISTER:
2493 zebra_ptm_bfd_dst_deregister(client, sock, length, zvrf);
2494 break;
2495 case ZEBRA_VRF_UNREGISTER:
2496 zread_vrf_unregister(client, length, zvrf);
2497 break;
2498 case ZEBRA_BFD_CLIENT_REGISTER:
2499 zebra_ptm_bfd_client_register(client, sock, length);
2500 break;
2501 case ZEBRA_INTERFACE_ENABLE_RADV:
2502#if defined(HAVE_RTADV)
2503 zebra_interface_radv_set(client, sock, length, zvrf, 1);
9c3bf1ce 2504#endif
d62a17ae 2505 break;
2506 case ZEBRA_INTERFACE_DISABLE_RADV:
2507#if defined(HAVE_RTADV)
2508 zebra_interface_radv_set(client, sock, length, zvrf, 0);
9c3bf1ce 2509#endif
d62a17ae 2510 break;
2511 case ZEBRA_MPLS_LABELS_ADD:
2512 case ZEBRA_MPLS_LABELS_DELETE:
2513 zread_mpls_labels(command, client, length, vrf_id);
2514 break;
2515 case ZEBRA_IPMR_ROUTE_STATS:
2516 zebra_ipmr_route_stats(client, sock, length, zvrf);
2517 break;
2518 case ZEBRA_LABEL_MANAGER_CONNECT:
2519 case ZEBRA_GET_LABEL_CHUNK:
2520 case ZEBRA_RELEASE_LABEL_CHUNK:
2521 zread_label_manager_request(command, client, vrf_id);
2522 break;
2523 case ZEBRA_FEC_REGISTER:
2524 zserv_fec_register(client, sock, length);
2525 break;
2526 case ZEBRA_FEC_UNREGISTER:
2527 zserv_fec_unregister(client, sock, length);
2528 break;
1a98c087
MK
2529 case ZEBRA_ADVERTISE_DEFAULT_GW:
2530 zebra_vxlan_advertise_gw_macip(client, sock, length, zvrf);
2531 break;
d62a17ae 2532 case ZEBRA_ADVERTISE_ALL_VNI:
2533 zebra_vxlan_advertise_all_vni(client, sock, length, zvrf);
2534 break;
2535 case ZEBRA_REMOTE_VTEP_ADD:
2536 zebra_vxlan_remote_vtep_add(client, sock, length, zvrf);
2537 break;
2538 case ZEBRA_REMOTE_VTEP_DEL:
2539 zebra_vxlan_remote_vtep_del(client, sock, length, zvrf);
2540 break;
2541 case ZEBRA_REMOTE_MACIP_ADD:
2542 zebra_vxlan_remote_macip_add(client, sock, length, zvrf);
2543 break;
2544 case ZEBRA_REMOTE_MACIP_DEL:
2545 zebra_vxlan_remote_macip_del(client, sock, length, zvrf);
baca8cd4 2546 break;
e0ae31b8
DS
2547 case ZEBRA_INTERFACE_SET_MASTER:
2548 zread_interface_set_master(client, sock, length);
d62a17ae 2549 break;
6833ae01 2550 case ZEBRA_PW_ADD:
2551 case ZEBRA_PW_DELETE:
2552 case ZEBRA_PW_SET:
2553 case ZEBRA_PW_UNSET:
2554 zread_pseudowire(command, client, length, vrf_id);
2555 break;
d62a17ae 2556 default:
2557 zlog_info("Zebra received unknown command %d", command);
2558 break;
2559 }
2560
2561 if (client->t_suicide) {
2562 /* No need to wait for thread callback, just kill immediately.
2563 */
2564 zebra_client_close(client);
2565 return -1;
2566 }
2567
2568zclient_read_out:
2569 stream_reset(client->ibuf);
2570 zebra_event(ZEBRA_READ, sock, client);
2571 return 0;
718e3744 2572}
2573
718e3744 2574
2575/* Accept code of zebra server socket. */
d62a17ae 2576static int zebra_accept(struct thread *thread)
718e3744 2577{
d62a17ae 2578 int accept_sock;
2579 int client_sock;
2580 struct sockaddr_in client;
2581 socklen_t len;
2582
2583 accept_sock = THREAD_FD(thread);
718e3744 2584
d62a17ae 2585 /* Reregister myself. */
2586 zebra_event(ZEBRA_SERV, accept_sock, NULL);
718e3744 2587
d62a17ae 2588 len = sizeof(struct sockaddr_in);
2589 client_sock = accept(accept_sock, (struct sockaddr *)&client, &len);
719e9741 2590
d62a17ae 2591 if (client_sock < 0) {
2592 zlog_warn("Can't accept zebra socket: %s",
2593 safe_strerror(errno));
2594 return -1;
2595 }
718e3744 2596
d62a17ae 2597 /* Make client socket non-blocking. */
2598 set_nonblocking(client_sock);
718e3744 2599
d62a17ae 2600 /* Create new zebra client. */
2601 zebra_client_create(client_sock);
718e3744 2602
d62a17ae 2603 return 0;
718e3744 2604}
2605
689f5a8c
DL
2606/* Make zebra server socket, wiping any existing one (see bug #403). */
2607void zebra_zserv_socket_init(char *path)
d62a17ae 2608{
2609 int ret;
689f5a8c 2610 int sock;
d62a17ae 2611 mode_t old_mask;
689f5a8c
DL
2612 struct sockaddr_storage sa;
2613 socklen_t sa_len;
d62a17ae 2614
689f5a8c
DL
2615 if (!frr_zclient_addr(&sa, &sa_len, path))
2616 /* should be caught in zebra main() */
2617 return;
d62a17ae 2618
2619 /* Set umask */
2620 old_mask = umask(0077);
2621
2622 /* Make UNIX domain socket. */
689f5a8c 2623 sock = socket(sa.ss_family, SOCK_STREAM, 0);
d62a17ae 2624 if (sock < 0) {
689f5a8c 2625 zlog_warn("Can't create zserv socket: %s",
d62a17ae 2626 safe_strerror(errno));
2627 zlog_warn(
2628 "zebra can't provide full functionality due to above error");
2629 return;
2630 }
2631
689f5a8c
DL
2632 if (sa.ss_family != AF_UNIX) {
2633 sockopt_reuseaddr(sock);
2634 sockopt_reuseport(sock);
2635 } else {
2636 struct sockaddr_un *suna = (struct sockaddr_un *)&sa;
2637 if (suna->sun_path[0])
2638 unlink(suna->sun_path);
2639 }
2640
e6c1975a 2641 if (sa.ss_family != AF_UNIX && zserv_privs.change(ZPRIVS_RAISE))
689f5a8c
DL
2642 zlog_err("Can't raise privileges");
2643
2644 ret = bind(sock, (struct sockaddr *)&sa, sa_len);
d62a17ae 2645 if (ret < 0) {
689f5a8c 2646 zlog_warn("Can't bind zserv socket on %s: %s", path,
d62a17ae 2647 safe_strerror(errno));
2648 zlog_warn(
2649 "zebra can't provide full functionality due to above error");
2650 close(sock);
2651 return;
2652 }
e6c1975a 2653 if (sa.ss_family != AF_UNIX && zserv_privs.change(ZPRIVS_LOWER))
689f5a8c 2654 zlog_err("Can't lower privileges");
d62a17ae 2655
2656 ret = listen(sock, 5);
2657 if (ret < 0) {
689f5a8c 2658 zlog_warn("Can't listen to zserv socket %s: %s", path,
d62a17ae 2659 safe_strerror(errno));
2660 zlog_warn(
2661 "zebra can't provide full functionality due to above error");
2662 close(sock);
2663 return;
2664 }
2665
2666 umask(old_mask);
2667
2668 zebra_event(ZEBRA_SERV, sock, NULL);
718e3744 2669}
6b0655a2 2670
718e3744 2671
d62a17ae 2672static void zebra_event(enum event event, int sock, struct zserv *client)
2673{
2674 switch (event) {
2675 case ZEBRA_SERV:
2676 thread_add_read(zebrad.master, zebra_accept, client, sock,
2677 NULL);
2678 break;
2679 case ZEBRA_READ:
2680 client->t_read = NULL;
2681 thread_add_read(zebrad.master, zebra_client_read, client, sock,
2682 &client->t_read);
2683 break;
2684 case ZEBRA_WRITE:
2685 /**/
2686 break;
2687 }
718e3744 2688}
6b0655a2 2689
04b02fda 2690#define ZEBRA_TIME_BUF 32
d62a17ae 2691static char *zserv_time_buf(time_t *time1, char *buf, int buflen)
04b02fda 2692{
d62a17ae 2693 struct tm *tm;
2694 time_t now;
04b02fda 2695
d62a17ae 2696 assert(buf != NULL);
2697 assert(buflen >= ZEBRA_TIME_BUF);
2698 assert(time1 != NULL);
04b02fda 2699
d62a17ae 2700 if (!*time1) {
2701 snprintf(buf, buflen, "never ");
2702 return (buf);
2703 }
04b02fda 2704
d62a17ae 2705 now = monotime(NULL);
2706 now -= *time1;
2707 tm = gmtime(&now);
04b02fda 2708
d62a17ae 2709 if (now < ONE_DAY_SECOND)
2710 snprintf(buf, buflen, "%02d:%02d:%02d", tm->tm_hour, tm->tm_min,
2711 tm->tm_sec);
2712 else if (now < ONE_WEEK_SECOND)
2713 snprintf(buf, buflen, "%dd%02dh%02dm", tm->tm_yday, tm->tm_hour,
2714 tm->tm_min);
96ade3ed 2715 else
d62a17ae 2716 snprintf(buf, buflen, "%02dw%dd%02dh", tm->tm_yday / 7,
2717 tm->tm_yday - ((tm->tm_yday / 7) * 7), tm->tm_hour);
2718 return buf;
2719}
2720
2721static void zebra_show_client_detail(struct vty *vty, struct zserv *client)
2722{
2723 char cbuf[ZEBRA_TIME_BUF], rbuf[ZEBRA_TIME_BUF];
2724 char wbuf[ZEBRA_TIME_BUF], nhbuf[ZEBRA_TIME_BUF], mbuf[ZEBRA_TIME_BUF];
2725
2726 vty_out(vty, "Client: %s", zebra_route_string(client->proto));
2727 if (client->instance)
2728 vty_out(vty, " Instance: %d", client->instance);
2729 vty_out(vty, "\n");
2730
2731 vty_out(vty, "------------------------ \n");
2732 vty_out(vty, "FD: %d \n", client->sock);
2733 vty_out(vty, "Route Table ID: %d \n", client->rtm_table);
2734
2735 vty_out(vty, "Connect Time: %s \n",
2736 zserv_time_buf(&client->connect_time, cbuf, ZEBRA_TIME_BUF));
2737 if (client->nh_reg_time) {
2738 vty_out(vty, "Nexthop Registry Time: %s \n",
2739 zserv_time_buf(&client->nh_reg_time, nhbuf,
2740 ZEBRA_TIME_BUF));
2741 if (client->nh_last_upd_time)
2742 vty_out(vty, "Nexthop Last Update Time: %s \n",
2743 zserv_time_buf(&client->nh_last_upd_time, mbuf,
2744 ZEBRA_TIME_BUF));
2745 else
2746 vty_out(vty, "No Nexthop Update sent\n");
2747 } else
2748 vty_out(vty, "Not registered for Nexthop Updates\n");
2749
2750 vty_out(vty, "Last Msg Rx Time: %s \n",
2751 zserv_time_buf(&client->last_read_time, rbuf, ZEBRA_TIME_BUF));
2752 vty_out(vty, "Last Msg Tx Time: %s \n",
2753 zserv_time_buf(&client->last_write_time, wbuf, ZEBRA_TIME_BUF));
2754 if (client->last_read_time)
2755 vty_out(vty, "Last Rcvd Cmd: %s \n",
2756 zserv_command_string(client->last_read_cmd));
2757 if (client->last_write_time)
2758 vty_out(vty, "Last Sent Cmd: %s \n",
2759 zserv_command_string(client->last_write_cmd));
2760 vty_out(vty, "\n");
2761
2762 vty_out(vty, "Type Add Update Del \n");
2763 vty_out(vty, "================================================== \n");
2764 vty_out(vty, "IPv4 %-12d%-12d%-12d\n", client->v4_route_add_cnt,
2765 client->v4_route_upd8_cnt, client->v4_route_del_cnt);
2766 vty_out(vty, "IPv6 %-12d%-12d%-12d\n", client->v6_route_add_cnt,
2767 client->v6_route_upd8_cnt, client->v6_route_del_cnt);
2768 vty_out(vty, "Redist:v4 %-12d%-12d%-12d\n", client->redist_v4_add_cnt,
2769 0, client->redist_v4_del_cnt);
2770 vty_out(vty, "Redist:v6 %-12d%-12d%-12d\n", client->redist_v6_add_cnt,
2771 0, client->redist_v6_del_cnt);
2772 vty_out(vty, "Connected %-12d%-12d%-12d\n", client->ifadd_cnt, 0,
2773 client->ifdel_cnt);
2774 vty_out(vty, "BFD peer %-12d%-12d%-12d\n", client->bfd_peer_add_cnt,
2775 client->bfd_peer_upd8_cnt, client->bfd_peer_del_cnt);
2776 vty_out(vty, "Interface Up Notifications: %d\n", client->ifup_cnt);
2777 vty_out(vty, "Interface Down Notifications: %d\n", client->ifdown_cnt);
2778 vty_out(vty, "VNI add notifications: %d\n", client->vniadd_cnt);
2779 vty_out(vty, "VNI delete notifications: %d\n", client->vnidel_cnt);
2780 vty_out(vty, "MAC-IP add notifications: %d\n", client->macipadd_cnt);
2781 vty_out(vty, "MAC-IP delete notifications: %d\n", client->macipdel_cnt);
2782
2783 vty_out(vty, "\n");
2784 return;
2785}
2786
2787static void zebra_show_client_brief(struct vty *vty, struct zserv *client)
2788{
2789 char cbuf[ZEBRA_TIME_BUF], rbuf[ZEBRA_TIME_BUF];
2790 char wbuf[ZEBRA_TIME_BUF];
2791
2792 vty_out(vty, "%-8s%12s %12s%12s%8d/%-8d%8d/%-8d\n",
2793 zebra_route_string(client->proto),
2794 zserv_time_buf(&client->connect_time, cbuf, ZEBRA_TIME_BUF),
2795 zserv_time_buf(&client->last_read_time, rbuf, ZEBRA_TIME_BUF),
2796 zserv_time_buf(&client->last_write_time, wbuf, ZEBRA_TIME_BUF),
2797 client->v4_route_add_cnt + client->v4_route_upd8_cnt,
2798 client->v4_route_del_cnt,
2799 client->v6_route_add_cnt + client->v6_route_upd8_cnt,
2800 client->v6_route_del_cnt);
2801}
2802
2803struct zserv *zebra_find_client(u_char proto)
2804{
2805 struct listnode *node, *nnode;
2806 struct zserv *client;
2807
2808 for (ALL_LIST_ELEMENTS(zebrad.client_list, node, nnode, client)) {
2809 if (client->proto == proto)
2810 return client;
2811 }
2812
2813 return NULL;
8ed6821e 2814}
2815
9bf75362 2816#ifdef HAVE_NETLINK
718e3744 2817/* Display default rtm_table for all clients. */
2818DEFUN (show_table,
2819 show_table_cmd,
2820 "show table",
2821 SHOW_STR
2822 "default routing table to use for all clients\n")
2823{
d62a17ae 2824 vty_out(vty, "table %d\n", zebrad.rtm_table_default);
2825 return CMD_SUCCESS;
718e3744 2826}
2827
6147e2c6 2828DEFUN (config_table,
718e3744 2829 config_table_cmd,
2830 "table TABLENO",
2831 "Configure target kernel routing table\n"
2832 "TABLE integer\n")
2833{
d62a17ae 2834 zebrad.rtm_table_default = strtol(argv[1]->arg, (char **)0, 10);
2835 return CMD_SUCCESS;
718e3744 2836}
2837
813d4307
DW
2838DEFUN (no_config_table,
2839 no_config_table_cmd,
6af6be86 2840 "no table [TABLENO]",
813d4307
DW
2841 NO_STR
2842 "Configure target kernel routing table\n"
2843 "TABLE integer\n")
2844{
d62a17ae 2845 zebrad.rtm_table_default = 0;
2846 return CMD_SUCCESS;
813d4307 2847}
9bf75362 2848#endif
813d4307 2849
647e4f1f 2850DEFUN (ip_forwarding,
2851 ip_forwarding_cmd,
2852 "ip forwarding",
2853 IP_STR
2854 "Turn on IP forwarding")
2855{
d62a17ae 2856 int ret;
647e4f1f 2857
d62a17ae 2858 ret = ipforward();
2859 if (ret == 0)
2860 ret = ipforward_on();
647e4f1f 2861
d62a17ae 2862 if (ret == 0) {
2863 vty_out(vty, "Can't turn on IP forwarding\n");
2864 return CMD_WARNING_CONFIG_FAILED;
2865 }
647e4f1f 2866
d62a17ae 2867 return CMD_SUCCESS;
647e4f1f 2868}
2869
718e3744 2870DEFUN (no_ip_forwarding,
2871 no_ip_forwarding_cmd,
2872 "no ip forwarding",
2873 NO_STR
2874 IP_STR
2875 "Turn off IP forwarding")
2876{
d62a17ae 2877 int ret;
718e3744 2878
d62a17ae 2879 ret = ipforward();
2880 if (ret != 0)
2881 ret = ipforward_off();
718e3744 2882
d62a17ae 2883 if (ret != 0) {
2884 vty_out(vty, "Can't turn off IP forwarding\n");
2885 return CMD_WARNING_CONFIG_FAILED;
2886 }
718e3744 2887
d62a17ae 2888 return CMD_SUCCESS;
718e3744 2889}
2890
57282a31
DS
2891DEFUN (show_zebra,
2892 show_zebra_cmd,
2893 "show zebra",
2894 SHOW_STR
2895 "Zebra information\n")
2896{
d62a17ae 2897 struct vrf *vrf;
57282a31 2898
d62a17ae 2899 vty_out(vty,
2900 " Route Route Neighbor LSP LSP\n");
2901 vty_out(vty,
2902 "VRF Installs Removals Updates Installs Removals\n");
2903 RB_FOREACH(vrf, vrf_name_head, &vrfs_by_name)
2904 {
2905 struct zebra_vrf *zvrf = vrf->info;
9d303b37
DL
2906 vty_out(vty, "%-25s %10" PRIu64 " %10" PRIu64 " %10" PRIu64
2907 " %10" PRIu64 " %10" PRIu64 "\n",
d62a17ae 2908 vrf->name, zvrf->installs, zvrf->removals,
2909 zvrf->neigh_updates, zvrf->lsp_installs,
2910 zvrf->lsp_removals);
2911 }
57282a31 2912
d62a17ae 2913 return CMD_SUCCESS;
57282a31
DS
2914}
2915
718e3744 2916/* This command is for debugging purpose. */
2917DEFUN (show_zebra_client,
2918 show_zebra_client_cmd,
2919 "show zebra client",
2920 SHOW_STR
49d73233 2921 "Zebra information\n"
b9ee4999 2922 "Client information\n")
718e3744 2923{
d62a17ae 2924 struct listnode *node;
2925 struct zserv *client;
718e3744 2926
d62a17ae 2927 for (ALL_LIST_ELEMENTS_RO(zebrad.client_list, node, client))
2928 zebra_show_client_detail(vty, client);
04b02fda 2929
d62a17ae 2930 return CMD_SUCCESS;
04b02fda
DS
2931}
2932
2933/* This command is for debugging purpose. */
2934DEFUN (show_zebra_client_summary,
2935 show_zebra_client_summary_cmd,
2936 "show zebra client summary",
2937 SHOW_STR
b9ee4999
DS
2938 "Zebra information brief\n"
2939 "Client information brief\n"
2940 "Brief Summary\n")
04b02fda 2941{
d62a17ae 2942 struct listnode *node;
2943 struct zserv *client;
04b02fda 2944
d62a17ae 2945 vty_out(vty,
2946 "Name Connect Time Last Read Last Write IPv4 Routes IPv6 Routes \n");
2947 vty_out(vty,
2948 "--------------------------------------------------------------------------------\n");
04b02fda 2949
d62a17ae 2950 for (ALL_LIST_ELEMENTS_RO(zebrad.client_list, node, client))
2951 zebra_show_client_brief(vty, client);
fb018d25 2952
d62a17ae 2953 vty_out(vty, "Routes column shows (added+updated)/deleted\n");
2954 return CMD_SUCCESS;
718e3744 2955}
2956
2957/* Table configuration write function. */
d62a17ae 2958static int config_write_table(struct vty *vty)
718e3744 2959{
d62a17ae 2960 if (zebrad.rtm_table_default)
2961 vty_out(vty, "table %d\n", zebrad.rtm_table_default);
2962 return 0;
718e3744 2963}
2964
2965/* table node for routing tables. */
d62a17ae 2966static struct cmd_node table_node = {TABLE_NODE,
2967 "", /* This node has no interface. */
2968 1};
6b0655a2 2969
718e3744 2970/* Only display ip forwarding is enabled or not. */
2971DEFUN (show_ip_forwarding,
2972 show_ip_forwarding_cmd,
2973 "show ip forwarding",
2974 SHOW_STR
2975 IP_STR
2976 "IP forwarding status\n")
2977{
d62a17ae 2978 int ret;
718e3744 2979
d62a17ae 2980 ret = ipforward();
718e3744 2981
d62a17ae 2982 if (ret == 0)
2983 vty_out(vty, "IP forwarding is off\n");
2984 else
2985 vty_out(vty, "IP forwarding is on\n");
2986 return CMD_SUCCESS;
718e3744 2987}
2988
718e3744 2989/* Only display ipv6 forwarding is enabled or not. */
2990DEFUN (show_ipv6_forwarding,
2991 show_ipv6_forwarding_cmd,
2992 "show ipv6 forwarding",
2993 SHOW_STR
2994 "IPv6 information\n"
2995 "Forwarding status\n")
2996{
d62a17ae 2997 int ret;
718e3744 2998
d62a17ae 2999 ret = ipforward_ipv6();
718e3744 3000
d62a17ae 3001 switch (ret) {
3002 case -1:
3003 vty_out(vty, "ipv6 forwarding is unknown\n");
3004 break;
3005 case 0:
3006 vty_out(vty, "ipv6 forwarding is %s\n", "off");
3007 break;
3008 case 1:
3009 vty_out(vty, "ipv6 forwarding is %s\n", "on");
3010 break;
3011 default:
3012 vty_out(vty, "ipv6 forwarding is %s\n", "off");
3013 break;
3014 }
3015 return CMD_SUCCESS;
718e3744 3016}
3017
55906724 3018DEFUN (ipv6_forwarding,
3019 ipv6_forwarding_cmd,
3020 "ipv6 forwarding",
3021 IPV6_STR
3022 "Turn on IPv6 forwarding")
3023{
d62a17ae 3024 int ret;
55906724 3025
d62a17ae 3026 ret = ipforward_ipv6();
3027 if (ret == 0)
3028 ret = ipforward_ipv6_on();
41d3fc96 3029
d62a17ae 3030 if (ret == 0) {
3031 vty_out(vty, "Can't turn on IPv6 forwarding\n");
3032 return CMD_WARNING_CONFIG_FAILED;
3033 }
55906724 3034
d62a17ae 3035 return CMD_SUCCESS;
55906724 3036}
3037
718e3744 3038DEFUN (no_ipv6_forwarding,
3039 no_ipv6_forwarding_cmd,
3040 "no ipv6 forwarding",
3041 NO_STR
55906724 3042 IPV6_STR
3043 "Turn off IPv6 forwarding")
718e3744 3044{
d62a17ae 3045 int ret;
718e3744 3046
d62a17ae 3047 ret = ipforward_ipv6();
3048 if (ret != 0)
3049 ret = ipforward_ipv6_off();
41d3fc96 3050
d62a17ae 3051 if (ret != 0) {
3052 vty_out(vty, "Can't turn off IPv6 forwarding\n");
3053 return CMD_WARNING_CONFIG_FAILED;
3054 }
718e3744 3055
d62a17ae 3056 return CMD_SUCCESS;
718e3744 3057}
3058
718e3744 3059/* IPForwarding configuration write function. */
d62a17ae 3060static int config_write_forwarding(struct vty *vty)
718e3744 3061{
d62a17ae 3062 /* FIXME: Find better place for that. */
3063 router_id_write(vty);
18a6dce6 3064
d62a17ae 3065 if (!ipforward())
3066 vty_out(vty, "no ip forwarding\n");
3067 if (!ipforward_ipv6())
3068 vty_out(vty, "no ipv6 forwarding\n");
3069 vty_out(vty, "!\n");
3070 return 0;
718e3744 3071}
3072
3073/* table node for routing tables. */
d62a17ae 3074static struct cmd_node forwarding_node = {FORWARDING_NODE,
3075 "", /* This node has no interface. */
3076 1};
718e3744 3077
718e3744 3078/* Initialisation of zebra and installation of commands. */
d62a17ae 3079void zebra_init(void)
718e3744 3080{
d62a17ae 3081 /* Client list init. */
3082 zebrad.client_list = list_new();
718e3744 3083
d62a17ae 3084 /* Install configuration write function. */
3085 install_node(&table_node, config_write_table);
3086 install_node(&forwarding_node, config_write_forwarding);
718e3744 3087
d62a17ae 3088 install_element(VIEW_NODE, &show_ip_forwarding_cmd);
3089 install_element(CONFIG_NODE, &ip_forwarding_cmd);
3090 install_element(CONFIG_NODE, &no_ip_forwarding_cmd);
3091 install_element(ENABLE_NODE, &show_zebra_cmd);
3092 install_element(ENABLE_NODE, &show_zebra_client_cmd);
3093 install_element(ENABLE_NODE, &show_zebra_client_summary_cmd);
718e3744 3094
3095#ifdef HAVE_NETLINK
d62a17ae 3096 install_element(VIEW_NODE, &show_table_cmd);
3097 install_element(CONFIG_NODE, &config_table_cmd);
3098 install_element(CONFIG_NODE, &no_config_table_cmd);
718e3744 3099#endif /* HAVE_NETLINK */
3100
d62a17ae 3101 install_element(VIEW_NODE, &show_ipv6_forwarding_cmd);
3102 install_element(CONFIG_NODE, &ipv6_forwarding_cmd);
3103 install_element(CONFIG_NODE, &no_ipv6_forwarding_cmd);
7514fb77 3104
d62a17ae 3105 /* Route-map */
3106 zebra_route_map_init();
718e3744 3107}