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