]> git.proxmox.com Git - mirror_frr.git/blame - zebra/zserv.c
Merge pull request #549 from qlyoung/fix-poll-eof
[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)
2048 {
2049 if (zread_relay_label_manager_request (cmd, client) != 0)
2050 zsend_label_manager_connect_response (client, vrf_id, 1);
2051 }
2052 /* this is a label manager */
2053 else
2054 {
2055 if (cmd == ZEBRA_LABEL_MANAGER_CONNECT)
2056 zread_label_manager_connect (client, vrf_id);
2057 else
2058 {
2059 /* Sanity: don't allow 'unidentified' requests */
2060 if (!client->proto)
2061 {
2062 zlog_err ("Got label request from an unidentified client");
2063 return;
2064 }
2065 if (cmd == ZEBRA_GET_LABEL_CHUNK)
2066 zread_get_label_chunk (client, vrf_id);
2067 else if (cmd == ZEBRA_RELEASE_LABEL_CHUNK)
2068 zread_release_label_chunk (client);
2069 }
2070 }
2071}
ce549947 2072
4b33b75a 2073/* Cleanup registered nexthops (across VRFs) upon client disconnect. */
2074static void
2075zebra_client_close_cleanup_rnh (struct zserv *client)
2076{
1a1a7065 2077 struct vrf *vrf;
4b33b75a 2078 struct zebra_vrf *zvrf;
2079
1a1a7065 2080 RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id)
4b33b75a 2081 {
1a1a7065 2082 if ((zvrf = vrf->info) != NULL)
4b33b75a 2083 {
661512bf
RW
2084 zebra_cleanup_rnh_client(zvrf_id (zvrf), AF_INET, client, RNH_NEXTHOP_TYPE);
2085 zebra_cleanup_rnh_client(zvrf_id (zvrf), AF_INET6, client, RNH_NEXTHOP_TYPE);
2086 zebra_cleanup_rnh_client(zvrf_id (zvrf), AF_INET, client, RNH_IMPORT_CHECK_TYPE);
2087 zebra_cleanup_rnh_client(zvrf_id (zvrf), AF_INET6, client, RNH_IMPORT_CHECK_TYPE);
ce549947
RW
2088 if (client->proto == ZEBRA_ROUTE_LDP)
2089 {
2090 hash_iterate(zvrf->lsp_table, mpls_ldp_lsp_uninstall_all,
2091 zvrf->lsp_table);
2092 mpls_ldp_ftn_uninstall_all (zvrf, AFI_IP);
2093 mpls_ldp_ftn_uninstall_all (zvrf, AFI_IP6);
2094 }
4b33b75a 2095 }
2096 }
2097}
2098
718e3744 2099/* Close zebra client. */
b9df2d25 2100static void
718e3744 2101zebra_client_close (struct zserv *client)
2102{
567b877d 2103 /* Send client de-registration to BFD */
2376c3f2 2104 zebra_ptm_bfd_client_deregister(client->proto);
567b877d 2105
4b33b75a 2106 /* Cleanup any registered nexthops - across all VRFs. */
2107 zebra_client_close_cleanup_rnh (client);
fb018d25 2108
fea12efb 2109 /* Release Label Manager chunks */
2110 release_daemon_chunks (client->proto, client->instance);
2111
5aba114a
DS
2112 /* Cleanup any FECs registered by this client. */
2113 zebra_mpls_cleanup_fecs_for_client (vrf_info_lookup(VRF_DEFAULT), client);
2114
718e3744 2115 /* Close file descriptor. */
2116 if (client->sock)
2117 {
7c8ff89e
DS
2118 unsigned long nroutes;
2119
718e3744 2120 close (client->sock);
7c8ff89e
DS
2121 nroutes = rib_score_proto (client->proto, client->instance);
2122 zlog_notice ("client %d disconnected. %lu %s routes removed from the rib",
2123 client->sock, nroutes, zebra_route_string (client->proto));
718e3744 2124 client->sock = -1;
2125 }
2126
2127 /* Free stream buffers. */
2128 if (client->ibuf)
2129 stream_free (client->ibuf);
2130 if (client->obuf)
2131 stream_free (client->obuf);
719e9741 2132 if (client->wb)
2133 buffer_free(client->wb);
718e3744 2134
2135 /* Release threads. */
2136 if (client->t_read)
2137 thread_cancel (client->t_read);
2138 if (client->t_write)
2139 thread_cancel (client->t_write);
719e9741 2140 if (client->t_suicide)
2141 thread_cancel (client->t_suicide);
718e3744 2142
2143 /* Free client structure. */
b21b19c5 2144 listnode_delete (zebrad.client_list, client);
c66f9c61 2145 XFREE (MTYPE_TMP, client);
718e3744 2146}
2147
2148/* Make new client. */
b9df2d25 2149static void
718e3744 2150zebra_client_create (int sock)
2151{
2152 struct zserv *client;
7076bb2f
FL
2153 int i;
2154 afi_t afi;
718e3744 2155
c66f9c61 2156 client = XCALLOC (MTYPE_TMP, sizeof (struct zserv));
718e3744 2157
2158 /* Make client input/output buffer. */
2159 client->sock = sock;
2160 client->ibuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
2161 client->obuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
719e9741 2162 client->wb = buffer_new(0);
718e3744 2163
2164 /* Set table number. */
b21b19c5 2165 client->rtm_table = zebrad.rtm_table_default;
718e3744 2166
cf672a86 2167 client->connect_time = monotime(NULL);
7076bb2f
FL
2168 /* Initialize flags */
2169 for (afi = AFI_IP; afi < AFI_MAX; afi++)
2170 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
2171 client->redist[afi][i] = vrf_bitmap_init ();
2172 client->redist_default = vrf_bitmap_init ();
2173 client->ifinfo = vrf_bitmap_init ();
2174 client->ridinfo = vrf_bitmap_init ();
04b02fda 2175
fea12efb 2176 /* by default, it's not a synchronous client */
2177 client->is_synchronous = 0;
2178
718e3744 2179 /* Add this client to linked list. */
b21b19c5 2180 listnode_add (zebrad.client_list, client);
718e3744 2181
2182 /* Make new read thread. */
2183 zebra_event (ZEBRA_READ, sock, client);
12f6fb97
DS
2184
2185 zebra_vrf_update_all (client);
718e3744 2186}
2187
2188/* Handler of zebra service request. */
b9df2d25 2189static int
718e3744 2190zebra_client_read (struct thread *thread)
2191{
2192 int sock;
2193 struct zserv *client;
57a1477b 2194 size_t already;
c1b9800a 2195 uint16_t length, command;
2196 uint8_t marker, version;
7076bb2f 2197 vrf_id_t vrf_id;
d651649e 2198 struct zebra_vrf *zvrf;
718e3744 2199
2200 /* Get thread data. Reset reading thread because I'm running. */
2201 sock = THREAD_FD (thread);
2202 client = THREAD_ARG (thread);
2203 client->t_read = NULL;
2204
719e9741 2205 if (client->t_suicide)
718e3744 2206 {
719e9741 2207 zebra_client_close(client);
718e3744 2208 return -1;
2209 }
719e9741 2210
2211 /* Read length and command (if we don't have it already). */
57a1477b 2212 if ((already = stream_get_endp(client->ibuf)) < ZEBRA_HEADER_SIZE)
719e9741 2213 {
57a1477b 2214 ssize_t nbyte;
719e9741 2215 if (((nbyte = stream_read_try (client->ibuf, sock,
57a1477b 2216 ZEBRA_HEADER_SIZE-already)) == 0) ||
719e9741 2217 (nbyte == -1))
2218 {
2219 if (IS_ZEBRA_DEBUG_EVENT)
2220 zlog_debug ("connection closed socket [%d]", sock);
2221 zebra_client_close (client);
2222 return -1;
2223 }
57a1477b 2224 if (nbyte != (ssize_t)(ZEBRA_HEADER_SIZE-already))
719e9741 2225 {
2226 /* Try again later. */
2227 zebra_event (ZEBRA_READ, sock, client);
2228 return 0;
2229 }
57a1477b 2230 already = ZEBRA_HEADER_SIZE;
719e9741 2231 }
2232
2233 /* Reset to read from the beginning of the incoming packet. */
2234 stream_set_getp(client->ibuf, 0);
2235
c1b9800a 2236 /* Fetch header values */
718e3744 2237 length = stream_getw (client->ibuf);
c1b9800a 2238 marker = stream_getc (client->ibuf);
2239 version = stream_getc (client->ibuf);
7076bb2f 2240 vrf_id = stream_getw (client->ibuf);
c1b9800a 2241 command = stream_getw (client->ibuf);
718e3744 2242
c1b9800a 2243 if (marker != ZEBRA_HEADER_MARKER || version != ZSERV_VERSION)
2244 {
2245 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
2246 __func__, sock, marker, version);
2247 zebra_client_close (client);
2248 return -1;
2249 }
719e9741 2250 if (length < ZEBRA_HEADER_SIZE)
718e3744 2251 {
57a1477b 2252 zlog_warn("%s: socket %d message length %u is less than header size %d",
2253 __func__, sock, length, ZEBRA_HEADER_SIZE);
2254 zebra_client_close (client);
2255 return -1;
2256 }
2257 if (length > STREAM_SIZE(client->ibuf))
2258 {
2259 zlog_warn("%s: socket %d message length %u exceeds buffer size %lu",
2260 __func__, sock, length, (u_long)STREAM_SIZE(client->ibuf));
718e3744 2261 zebra_client_close (client);
2262 return -1;
2263 }
2264
718e3744 2265 /* Read rest of data. */
57a1477b 2266 if (already < length)
718e3744 2267 {
57a1477b 2268 ssize_t nbyte;
2269 if (((nbyte = stream_read_try (client->ibuf, sock,
2270 length-already)) == 0) ||
2271 (nbyte == -1))
718e3744 2272 {
2273 if (IS_ZEBRA_DEBUG_EVENT)
b6178002 2274 zlog_debug ("connection closed [%d] when reading zebra data", sock);
718e3744 2275 zebra_client_close (client);
2276 return -1;
2277 }
57a1477b 2278 if (nbyte != (ssize_t)(length-already))
719e9741 2279 {
2280 /* Try again later. */
2281 zebra_event (ZEBRA_READ, sock, client);
2282 return 0;
2283 }
718e3744 2284 }
2285
719e9741 2286 length -= ZEBRA_HEADER_SIZE;
2287
718e3744 2288 /* Debug packet information. */
2289 if (IS_ZEBRA_DEBUG_EVENT)
b6178002 2290 zlog_debug ("zebra message comes from socket [%d]", sock);
718e3744 2291
2292 if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
7076bb2f
FL
2293 zlog_debug ("zebra message received [%s] %d in VRF %u",
2294 zserv_command_string (command), length, vrf_id);
718e3744 2295
cf672a86 2296 client->last_read_time = monotime(NULL);
04b02fda
DS
2297 client->last_read_cmd = command;
2298
5f3d1bdf 2299 zvrf = zebra_vrf_lookup_by_id (vrf_id);
d651649e
DS
2300 if (!zvrf)
2301 {
2302 if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
2303 zlog_debug ("zebra received unknown VRF[%u]", vrf_id);
2304 goto zclient_read_out;
2305 }
2306
718e3744 2307 switch (command)
2308 {
18a6dce6 2309 case ZEBRA_ROUTER_ID_ADD:
d651649e 2310 zread_router_id_add (client, length, zvrf);
18a6dce6 2311 break;
2312 case ZEBRA_ROUTER_ID_DELETE:
d651649e 2313 zread_router_id_delete (client, length, zvrf);
18a6dce6 2314 break;
718e3744 2315 case ZEBRA_INTERFACE_ADD:
d651649e 2316 zread_interface_add (client, length, zvrf);
718e3744 2317 break;
2318 case ZEBRA_INTERFACE_DELETE:
d651649e 2319 zread_interface_delete (client, length, zvrf);
718e3744 2320 break;
2321 case ZEBRA_IPV4_ROUTE_ADD:
d651649e 2322 zread_ipv4_add (client, length, zvrf);
718e3744 2323 break;
2324 case ZEBRA_IPV4_ROUTE_DELETE:
d651649e 2325 zread_ipv4_delete (client, length, zvrf);
718e3744 2326 break;
8a92a8a0 2327 case ZEBRA_IPV4_ROUTE_IPV6_NEXTHOP_ADD:
d651649e 2328 zread_ipv4_route_ipv6_nexthop_add (client, length, zvrf);
8a92a8a0 2329 break;
65efcfce
LB
2330 case ZEBRA_IPV4_NEXTHOP_ADD:
2331 zread_ipv4_add(client, length, zvrf); /* LB: r1.0 merge - id was 1 */
2332 break;
2333 case ZEBRA_IPV4_NEXTHOP_DELETE:
2334 zread_ipv4_delete(client, length, zvrf); /* LB: r1.0 merge - id was 1 */
2335 break;
718e3744 2336 case ZEBRA_IPV6_ROUTE_ADD:
d651649e 2337 zread_ipv6_add (client, length, zvrf);
718e3744 2338 break;
2339 case ZEBRA_IPV6_ROUTE_DELETE:
d651649e 2340 zread_ipv6_delete (client, length, zvrf);
718e3744 2341 break;
718e3744 2342 case ZEBRA_REDISTRIBUTE_ADD:
d651649e 2343 zebra_redistribute_add (command, client, length, zvrf);
718e3744 2344 break;
2345 case ZEBRA_REDISTRIBUTE_DELETE:
d651649e 2346 zebra_redistribute_delete (command, client, length, zvrf);
718e3744 2347 break;
2348 case ZEBRA_REDISTRIBUTE_DEFAULT_ADD:
d651649e 2349 zebra_redistribute_default_add (command, client, length, zvrf);
718e3744 2350 break;
2351 case ZEBRA_REDISTRIBUTE_DEFAULT_DELETE:
d651649e 2352 zebra_redistribute_default_delete (command, client, length, zvrf);
718e3744 2353 break;
6f61a5a3
EM
2354 case ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB:
2355 zread_ipv4_nexthop_lookup_mrib (client, length, zvrf);
718e3744 2356 break;
2ea1ab1c
VT
2357 case ZEBRA_HELLO:
2358 zread_hello (client);
2359 break;
fb018d25 2360 case ZEBRA_NEXTHOP_REGISTER:
d651649e 2361 zserv_rnh_register(client, sock, length, RNH_NEXTHOP_TYPE, zvrf);
fb018d25
DS
2362 break;
2363 case ZEBRA_NEXTHOP_UNREGISTER:
d651649e 2364 zserv_rnh_unregister(client, sock, length, RNH_NEXTHOP_TYPE, zvrf);
078430f6
DS
2365 break;
2366 case ZEBRA_IMPORT_ROUTE_REGISTER:
d651649e 2367 zserv_rnh_register(client, sock, length, RNH_IMPORT_CHECK_TYPE, zvrf);
078430f6
DS
2368 break;
2369 case ZEBRA_IMPORT_ROUTE_UNREGISTER:
d651649e 2370 zserv_rnh_unregister(client, sock, length, RNH_IMPORT_CHECK_TYPE, zvrf);
fb018d25 2371 break;
c43ed2e4
DS
2372 case ZEBRA_BFD_DEST_UPDATE:
2373 case ZEBRA_BFD_DEST_REGISTER:
d651649e 2374 zebra_ptm_bfd_dst_register(client, sock, length, command, zvrf);
c43ed2e4
DS
2375 break;
2376 case ZEBRA_BFD_DEST_DEREGISTER:
d651649e 2377 zebra_ptm_bfd_dst_deregister(client, sock, length, zvrf);
c43ed2e4 2378 break;
7076bb2f 2379 case ZEBRA_VRF_UNREGISTER:
d651649e 2380 zread_vrf_unregister (client, length, zvrf);
7076bb2f 2381 break;
055c4dfc 2382 case ZEBRA_BFD_CLIENT_REGISTER:
2383 zebra_ptm_bfd_client_register(client, sock, length);
2384 break;
4a04e5f7 2385 case ZEBRA_INTERFACE_ENABLE_RADV:
2386 zebra_interface_radv_set (client, sock, length, zvrf, 1);
2387 break;
2388 case ZEBRA_INTERFACE_DISABLE_RADV:
2389 zebra_interface_radv_set (client, sock, length, zvrf, 0);
2390 break;
ce549947
RW
2391 case ZEBRA_MPLS_LABELS_ADD:
2392 case ZEBRA_MPLS_LABELS_DELETE:
2393 zread_mpls_labels (command, client, length, vrf_id);
2394 break;
e3be0432
DS
2395 case ZEBRA_IPMR_ROUTE_STATS:
2396 zebra_ipmr_route_stats (client, sock, length, zvrf);
2397 break;
fea12efb 2398 case ZEBRA_LABEL_MANAGER_CONNECT:
2399 case ZEBRA_GET_LABEL_CHUNK:
2400 case ZEBRA_RELEASE_LABEL_CHUNK:
2401 zread_label_manager_request (command, client, vrf_id);
2402 break;
5aba114a
DS
2403 case ZEBRA_FEC_REGISTER:
2404 zserv_fec_register (client, sock, length);
2405 break;
2406 case ZEBRA_FEC_UNREGISTER:
2407 zserv_fec_unregister (client, sock, length);
2408 break;
718e3744 2409 default:
2410 zlog_info ("Zebra received unknown command %d", command);
2411 break;
2412 }
2413
719e9741 2414 if (client->t_suicide)
2415 {
2416 /* No need to wait for thread callback, just kill immediately. */
2417 zebra_client_close(client);
2418 return -1;
2419 }
2420
d651649e 2421 zclient_read_out:
718e3744 2422 stream_reset (client->ibuf);
2423 zebra_event (ZEBRA_READ, sock, client);
718e3744 2424 return 0;
2425}
2426
718e3744 2427
2428/* Accept code of zebra server socket. */
b9df2d25 2429static int
718e3744 2430zebra_accept (struct thread *thread)
2431{
2432 int accept_sock;
2433 int client_sock;
2434 struct sockaddr_in client;
2435 socklen_t len;
2436
2437 accept_sock = THREAD_FD (thread);
2438
719e9741 2439 /* Reregister myself. */
2440 zebra_event (ZEBRA_SERV, accept_sock, NULL);
2441
718e3744 2442 len = sizeof (struct sockaddr_in);
2443 client_sock = accept (accept_sock, (struct sockaddr *) &client, &len);
2444
2445 if (client_sock < 0)
2446 {
6099b3b5 2447 zlog_warn ("Can't accept zebra socket: %s", safe_strerror (errno));
718e3744 2448 return -1;
2449 }
2450
ccf3557b 2451 /* Make client socket non-blocking. */
719e9741 2452 set_nonblocking(client_sock);
865b852c 2453
718e3744 2454 /* Create new zebra client. */
2455 zebra_client_create (client_sock);
2456
718e3744 2457 return 0;
2458}
2459
b9df2d25 2460#ifdef HAVE_TCP_ZEBRA
718e3744 2461/* Make zebra's server socket. */
b9df2d25 2462static void
718e3744 2463zebra_serv ()
2464{
2465 int ret;
2466 int accept_sock;
2467 struct sockaddr_in addr;
2468
2469 accept_sock = socket (AF_INET, SOCK_STREAM, 0);
2470
2471 if (accept_sock < 0)
2472 {
3d1dc857 2473 zlog_warn ("Can't create zserv stream socket: %s",
2474 safe_strerror (errno));
718e3744 2475 zlog_warn ("zebra can't provice full functionality due to above error");
2476 return;
2477 }
2478
2479 memset (&addr, 0, sizeof (struct sockaddr_in));
2480 addr.sin_family = AF_INET;
2481 addr.sin_port = htons (ZEBRA_PORT);
6f0e3f6e 2482#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
718e3744 2483 addr.sin_len = sizeof (struct sockaddr_in);
6f0e3f6e 2484#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
718e3744 2485 addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
2486
2487 sockopt_reuseaddr (accept_sock);
2488 sockopt_reuseport (accept_sock);
2489
edd7c245 2490 if ( zserv_privs.change(ZPRIVS_RAISE) )
4525281a 2491 zlog_err("Can't raise privileges");
edd7c245 2492
718e3744 2493 ret = bind (accept_sock, (struct sockaddr *)&addr,
2494 sizeof (struct sockaddr_in));
2495 if (ret < 0)
2496 {
3d1dc857 2497 zlog_warn ("Can't bind to stream socket: %s",
2498 safe_strerror (errno));
718e3744 2499 zlog_warn ("zebra can't provice full functionality due to above error");
2500 close (accept_sock); /* Avoid sd leak. */
2501 return;
2502 }
edd7c245 2503
2504 if ( zserv_privs.change(ZPRIVS_LOWER) )
4525281a 2505 zlog_err("Can't lower privileges");
718e3744 2506
2507 ret = listen (accept_sock, 1);
2508 if (ret < 0)
2509 {
3d1dc857 2510 zlog_warn ("Can't listen to stream socket: %s",
2511 safe_strerror (errno));
718e3744 2512 zlog_warn ("zebra can't provice full functionality due to above error");
2513 close (accept_sock); /* Avoid sd leak. */
2514 return;
2515 }
2516
2517 zebra_event (ZEBRA_SERV, accept_sock, NULL);
2518}
fbedba64 2519#else /* HAVE_TCP_ZEBRA */
718e3744 2520
2521/* For sockaddr_un. */
2522#include <sys/un.h>
2523
2524/* zebra server UNIX domain socket. */
b9df2d25 2525static void
fce954f8 2526zebra_serv_un (const char *path)
718e3744 2527{
2528 int ret;
2529 int sock, len;
2530 struct sockaddr_un serv;
2531 mode_t old_mask;
2532
2533 /* First of all, unlink existing socket */
2534 unlink (path);
2535
2536 /* Set umask */
2537 old_mask = umask (0077);
2538
2539 /* Make UNIX domain socket. */
2540 sock = socket (AF_UNIX, SOCK_STREAM, 0);
2541 if (sock < 0)
2542 {
3d1dc857 2543 zlog_warn ("Can't create zserv unix socket: %s",
2544 safe_strerror (errno));
2545 zlog_warn ("zebra can't provide full functionality due to above error");
718e3744 2546 return;
2547 }
2548
2549 /* Make server socket. */
2550 memset (&serv, 0, sizeof (struct sockaddr_un));
2551 serv.sun_family = AF_UNIX;
2552 strncpy (serv.sun_path, path, strlen (path));
6f0e3f6e 2553#ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
718e3744 2554 len = serv.sun_len = SUN_LEN(&serv);
2555#else
2556 len = sizeof (serv.sun_family) + strlen (serv.sun_path);
6f0e3f6e 2557#endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
718e3744 2558
2559 ret = bind (sock, (struct sockaddr *) &serv, len);
2560 if (ret < 0)
2561 {
3d1dc857 2562 zlog_warn ("Can't bind to unix socket %s: %s",
2563 path, safe_strerror (errno));
2564 zlog_warn ("zebra can't provide full functionality due to above error");
718e3744 2565 close (sock);
2566 return;
2567 }
2568
2569 ret = listen (sock, 5);
2570 if (ret < 0)
2571 {
3d1dc857 2572 zlog_warn ("Can't listen to unix socket %s: %s",
2573 path, safe_strerror (errno));
2574 zlog_warn ("zebra can't provide full functionality due to above error");
718e3744 2575 close (sock);
2576 return;
2577 }
2578
2579 umask (old_mask);
2580
2581 zebra_event (ZEBRA_SERV, sock, NULL);
2582}
fbedba64 2583#endif /* HAVE_TCP_ZEBRA */
6b0655a2 2584
718e3744 2585
b9df2d25 2586static void
718e3744 2587zebra_event (enum event event, int sock, struct zserv *client)
2588{
2589 switch (event)
2590 {
2591 case ZEBRA_SERV:
ffa2c898 2592 thread_add_read(zebrad.master, zebra_accept, client, sock, NULL);
718e3744 2593 break;
2594 case ZEBRA_READ:
66e78ae6
QY
2595 client->t_read = NULL;
2596 thread_add_read(zebrad.master, zebra_client_read, client, sock,
2597 &client->t_read);
718e3744 2598 break;
2599 case ZEBRA_WRITE:
2600 /**/
2601 break;
2602 }
2603}
6b0655a2 2604
04b02fda
DS
2605#define ZEBRA_TIME_BUF 32
2606static char *
2607zserv_time_buf(time_t *time1, char *buf, int buflen)
2608{
2609 struct tm *tm;
2610 time_t now;
2611
2612 assert (buf != NULL);
2613 assert (buflen >= ZEBRA_TIME_BUF);
2614 assert (time1 != NULL);
2615
2616 if (!*time1)
2617 {
2618 snprintf(buf, buflen, "never ");
2619 return (buf);
2620 }
2621
cf672a86 2622 now = monotime(NULL);
04b02fda
DS
2623 now -= *time1;
2624 tm = gmtime(&now);
2625
2626 /* Making formatted timer strings. */
2627#define ONE_DAY_SECOND 60*60*24
2628#define ONE_WEEK_SECOND 60*60*24*7
2629
2630 if (now < ONE_DAY_SECOND)
2631 snprintf (buf, buflen, "%02d:%02d:%02d",
2632 tm->tm_hour, tm->tm_min, tm->tm_sec);
2633 else if (now < ONE_WEEK_SECOND)
2634 snprintf (buf, buflen, "%dd%02dh%02dm",
2635 tm->tm_yday, tm->tm_hour, tm->tm_min);
2636 else
2637 snprintf (buf, buflen, "%02dw%dd%02dh",
2638 tm->tm_yday/7, tm->tm_yday - ((tm->tm_yday/7) * 7), tm->tm_hour);
2639 return buf;
2640}
2641
2642static void
2643zebra_show_client_detail (struct vty *vty, struct zserv *client)
2644{
2645 char cbuf[ZEBRA_TIME_BUF], rbuf[ZEBRA_TIME_BUF];
2646 char wbuf[ZEBRA_TIME_BUF], nhbuf[ZEBRA_TIME_BUF], mbuf[ZEBRA_TIME_BUF];
2647
7c8ff89e
DS
2648 vty_out (vty, "Client: %s", zebra_route_string(client->proto));
2649 if (client->instance)
2650 vty_out (vty, " Instance: %d", client->instance);
2651 vty_out (vty, "%s", VTY_NEWLINE);
2652
04b02fda
DS
2653 vty_out (vty, "------------------------ %s", VTY_NEWLINE);
2654 vty_out (vty, "FD: %d %s", client->sock, VTY_NEWLINE);
2655 vty_out (vty, "Route Table ID: %d %s", client->rtm_table, VTY_NEWLINE);
2656
2657 vty_out (vty, "Connect Time: %s %s",
2658 zserv_time_buf(&client->connect_time, cbuf, ZEBRA_TIME_BUF),
2659 VTY_NEWLINE);
2660 if (client->nh_reg_time)
2661 {
2662 vty_out (vty, "Nexthop Registry Time: %s %s",
2663 zserv_time_buf(&client->nh_reg_time, nhbuf, ZEBRA_TIME_BUF),
2664 VTY_NEWLINE);
2665 if (client->nh_last_upd_time)
2666 vty_out (vty, "Nexthop Last Update Time: %s %s",
2667 zserv_time_buf(&client->nh_last_upd_time, mbuf, ZEBRA_TIME_BUF),
2668 VTY_NEWLINE);
2669 else
2670 vty_out (vty, "No Nexthop Update sent%s", VTY_NEWLINE);
2671 }
2672 else
2673 vty_out (vty, "Not registered for Nexthop Updates%s", VTY_NEWLINE);
2674
2675 vty_out (vty, "Last Msg Rx Time: %s %s",
2676 zserv_time_buf(&client->last_read_time, rbuf, ZEBRA_TIME_BUF),
2677 VTY_NEWLINE);
2678 vty_out (vty, "Last Msg Tx Time: %s %s",
2679 zserv_time_buf(&client->last_write_time, wbuf, ZEBRA_TIME_BUF),
2680 VTY_NEWLINE);
2681 if (client->last_read_time)
2682 vty_out (vty, "Last Rcvd Cmd: %s %s",
2683 zserv_command_string(client->last_read_cmd), VTY_NEWLINE);
2684 if (client->last_write_time)
2685 vty_out (vty, "Last Sent Cmd: %s %s",
2686 zserv_command_string(client->last_write_cmd), VTY_NEWLINE);
2687 vty_out (vty, "%s", VTY_NEWLINE);
2688
2689 vty_out (vty, "Type Add Update Del %s", VTY_NEWLINE);
2690 vty_out (vty, "================================================== %s", VTY_NEWLINE);
2691 vty_out (vty, "IPv4 %-12d%-12d%-12d%s", client->v4_route_add_cnt,
2692 client->v4_route_upd8_cnt, client->v4_route_del_cnt, VTY_NEWLINE);
2693 vty_out (vty, "IPv6 %-12d%-12d%-12d%s", client->v6_route_add_cnt,
2694 client->v6_route_upd8_cnt, client->v6_route_del_cnt, VTY_NEWLINE);
2695 vty_out (vty, "Redist:v4 %-12d%-12d%-12d%s", client->redist_v4_add_cnt, 0,
2696 client->redist_v4_del_cnt, VTY_NEWLINE);
2697 vty_out (vty, "Redist:v6 %-12d%-12d%-12d%s", client->redist_v6_add_cnt, 0,
2698 client->redist_v6_del_cnt, VTY_NEWLINE);
2699 vty_out (vty, "Connected %-12d%-12d%-12d%s", client->ifadd_cnt, 0,
2700 client->ifdel_cnt, VTY_NEWLINE);
c43ed2e4
DS
2701 vty_out (vty, "BFD peer %-12d%-12d%-12d%s", client->bfd_peer_add_cnt,
2702 client->bfd_peer_upd8_cnt, client->bfd_peer_del_cnt, VTY_NEWLINE);
04b02fda
DS
2703 vty_out (vty, "Interface Up Notifications: %d%s", client->ifup_cnt,
2704 VTY_NEWLINE);
2705 vty_out (vty, "Interface Down Notifications: %d%s", client->ifdown_cnt,
2706 VTY_NEWLINE);
2707
2708 vty_out (vty, "%s", VTY_NEWLINE);
2709 return;
2710}
2711
2712static void
2713zebra_show_client_brief (struct vty *vty, struct zserv *client)
2714{
2715 char cbuf[ZEBRA_TIME_BUF], rbuf[ZEBRA_TIME_BUF];
2716 char wbuf[ZEBRA_TIME_BUF];
2717
2718 vty_out (vty, "%-8s%12s %12s%12s%8d/%-8d%8d/%-8d%s",
2719 zebra_route_string(client->proto),
2720 zserv_time_buf(&client->connect_time, cbuf, ZEBRA_TIME_BUF),
2721 zserv_time_buf(&client->last_read_time, rbuf, ZEBRA_TIME_BUF),
2722 zserv_time_buf(&client->last_write_time, wbuf, ZEBRA_TIME_BUF),
2723 client->v4_route_add_cnt+client->v4_route_upd8_cnt,
2724 client->v4_route_del_cnt,
2725 client->v6_route_add_cnt+client->v6_route_upd8_cnt,
2726 client->v6_route_del_cnt, VTY_NEWLINE);
2727
2728}
2729
8ed6821e 2730struct zserv *
2731zebra_find_client (u_char proto)
2732{
2733 struct listnode *node, *nnode;
2734 struct zserv *client;
2735
2736 for (ALL_LIST_ELEMENTS (zebrad.client_list, node, nnode, client))
2737 {
2738 if (client->proto == proto)
2739 return client;
2740 }
2741
2742 return NULL;
2743}
2744
9bf75362 2745#ifdef HAVE_NETLINK
718e3744 2746/* Display default rtm_table for all clients. */
2747DEFUN (show_table,
2748 show_table_cmd,
2749 "show table",
2750 SHOW_STR
2751 "default routing table to use for all clients\n")
2752{
b21b19c5 2753 vty_out (vty, "table %d%s", zebrad.rtm_table_default,
718e3744 2754 VTY_NEWLINE);
2755 return CMD_SUCCESS;
2756}
2757
6147e2c6 2758DEFUN (config_table,
718e3744 2759 config_table_cmd,
2760 "table TABLENO",
2761 "Configure target kernel routing table\n"
2762 "TABLE integer\n")
2763{
6af6be86 2764 zebrad.rtm_table_default = strtol (argv[1]->arg, (char**)0, 10);
718e3744 2765 return CMD_SUCCESS;
2766}
2767
813d4307
DW
2768DEFUN (no_config_table,
2769 no_config_table_cmd,
6af6be86 2770 "no table [TABLENO]",
813d4307
DW
2771 NO_STR
2772 "Configure target kernel routing table\n"
2773 "TABLE integer\n")
2774{
2775 zebrad.rtm_table_default = 0;
2776 return CMD_SUCCESS;
2777}
9bf75362 2778#endif
813d4307 2779
647e4f1f 2780DEFUN (ip_forwarding,
2781 ip_forwarding_cmd,
2782 "ip forwarding",
2783 IP_STR
2784 "Turn on IP forwarding")
2785{
2786 int ret;
2787
2788 ret = ipforward ();
b71f00f2 2789 if (ret == 0)
2790 ret = ipforward_on ();
647e4f1f 2791
647e4f1f 2792 if (ret == 0)
2793 {
2794 vty_out (vty, "Can't turn on IP forwarding%s", VTY_NEWLINE);
2795 return CMD_WARNING;
2796 }
2797
2798 return CMD_SUCCESS;
2799}
2800
718e3744 2801DEFUN (no_ip_forwarding,
2802 no_ip_forwarding_cmd,
2803 "no ip forwarding",
2804 NO_STR
2805 IP_STR
2806 "Turn off IP forwarding")
2807{
2808 int ret;
2809
2810 ret = ipforward ();
b71f00f2 2811 if (ret != 0)
2812 ret = ipforward_off ();
718e3744 2813
718e3744 2814 if (ret != 0)
2815 {
2816 vty_out (vty, "Can't turn off IP forwarding%s", VTY_NEWLINE);
2817 return CMD_WARNING;
2818 }
2819
2820 return CMD_SUCCESS;
2821}
2822
2823/* This command is for debugging purpose. */
2824DEFUN (show_zebra_client,
2825 show_zebra_client_cmd,
2826 "show zebra client",
2827 SHOW_STR
49d73233 2828 "Zebra information\n"
b9ee4999 2829 "Client information\n")
718e3744 2830{
52dc7ee6 2831 struct listnode *node;
718e3744 2832 struct zserv *client;
2833
1eb8ef25 2834 for (ALL_LIST_ELEMENTS_RO (zebrad.client_list, node, client))
04b02fda
DS
2835 zebra_show_client_detail(vty, client);
2836
2837 return CMD_SUCCESS;
2838}
2839
2840/* This command is for debugging purpose. */
2841DEFUN (show_zebra_client_summary,
2842 show_zebra_client_summary_cmd,
2843 "show zebra client summary",
2844 SHOW_STR
b9ee4999
DS
2845 "Zebra information brief\n"
2846 "Client information brief\n"
2847 "Brief Summary\n")
04b02fda
DS
2848{
2849 struct listnode *node;
2850 struct zserv *client;
2851
2852 vty_out (vty, "Name Connect Time Last Read Last Write IPv4 Routes IPv6 Routes %s",
2853 VTY_NEWLINE);
2854 vty_out (vty,"--------------------------------------------------------------------------------%s",
2855 VTY_NEWLINE);
2856
2857 for (ALL_LIST_ELEMENTS_RO (zebrad.client_list, node, client))
2858 zebra_show_client_brief(vty, client);
fb018d25 2859
04b02fda 2860 vty_out (vty, "Routes column shows (added+updated)/deleted%s", VTY_NEWLINE);
718e3744 2861 return CMD_SUCCESS;
2862}
2863
2864/* Table configuration write function. */
b9df2d25 2865static int
718e3744 2866config_write_table (struct vty *vty)
2867{
b21b19c5 2868 if (zebrad.rtm_table_default)
2869 vty_out (vty, "table %d%s", zebrad.rtm_table_default,
718e3744 2870 VTY_NEWLINE);
2871 return 0;
2872}
2873
2874/* table node for routing tables. */
7fc626de 2875static struct cmd_node table_node =
718e3744 2876{
2877 TABLE_NODE,
2878 "", /* This node has no interface. */
2879 1
2880};
6b0655a2 2881
718e3744 2882/* Only display ip forwarding is enabled or not. */
2883DEFUN (show_ip_forwarding,
2884 show_ip_forwarding_cmd,
2885 "show ip forwarding",
2886 SHOW_STR
2887 IP_STR
2888 "IP forwarding status\n")
2889{
2890 int ret;
2891
2892 ret = ipforward ();
2893
2894 if (ret == 0)
2895 vty_out (vty, "IP forwarding is off%s", VTY_NEWLINE);
2896 else
2897 vty_out (vty, "IP forwarding is on%s", VTY_NEWLINE);
2898 return CMD_SUCCESS;
2899}
2900
718e3744 2901/* Only display ipv6 forwarding is enabled or not. */
2902DEFUN (show_ipv6_forwarding,
2903 show_ipv6_forwarding_cmd,
2904 "show ipv6 forwarding",
2905 SHOW_STR
2906 "IPv6 information\n"
2907 "Forwarding status\n")
2908{
2909 int ret;
2910
2911 ret = ipforward_ipv6 ();
2912
2913 switch (ret)
2914 {
2915 case -1:
2916 vty_out (vty, "ipv6 forwarding is unknown%s", VTY_NEWLINE);
2917 break;
2918 case 0:
2919 vty_out (vty, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE);
2920 break;
2921 case 1:
2922 vty_out (vty, "ipv6 forwarding is %s%s", "on", VTY_NEWLINE);
2923 break;
2924 default:
2925 vty_out (vty, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE);
2926 break;
2927 }
2928 return CMD_SUCCESS;
2929}
2930
55906724 2931DEFUN (ipv6_forwarding,
2932 ipv6_forwarding_cmd,
2933 "ipv6 forwarding",
2934 IPV6_STR
2935 "Turn on IPv6 forwarding")
2936{
2937 int ret;
2938
41d3fc96 2939 ret = ipforward_ipv6 ();
b71f00f2 2940 if (ret == 0)
2941 ret = ipforward_ipv6_on ();
41d3fc96 2942
41d3fc96 2943 if (ret == 0)
55906724 2944 {
2945 vty_out (vty, "Can't turn on IPv6 forwarding%s", VTY_NEWLINE);
2946 return CMD_WARNING;
2947 }
2948
2949 return CMD_SUCCESS;
2950}
2951
718e3744 2952DEFUN (no_ipv6_forwarding,
2953 no_ipv6_forwarding_cmd,
2954 "no ipv6 forwarding",
2955 NO_STR
55906724 2956 IPV6_STR
2957 "Turn off IPv6 forwarding")
718e3744 2958{
2959 int ret;
2960
41d3fc96 2961 ret = ipforward_ipv6 ();
b71f00f2 2962 if (ret != 0)
2963 ret = ipforward_ipv6_off ();
41d3fc96 2964
718e3744 2965 if (ret != 0)
2966 {
2967 vty_out (vty, "Can't turn off IPv6 forwarding%s", VTY_NEWLINE);
2968 return CMD_WARNING;
2969 }
2970
2971 return CMD_SUCCESS;
2972}
2973
718e3744 2974/* IPForwarding configuration write function. */
719e9741 2975static int
718e3744 2976config_write_forwarding (struct vty *vty)
2977{
18a6dce6 2978 /* FIXME: Find better place for that. */
2979 router_id_write (vty);
2980
f40dd648
QY
2981 if (!ipforward ())
2982 vty_out (vty, "no ip forwarding%s", VTY_NEWLINE);
f40dd648
QY
2983 if (!ipforward_ipv6 ())
2984 vty_out (vty, "no ipv6 forwarding%s", VTY_NEWLINE);
718e3744 2985 vty_out (vty, "!%s", VTY_NEWLINE);
2986 return 0;
2987}
2988
2989/* table node for routing tables. */
7fc626de 2990static struct cmd_node forwarding_node =
718e3744 2991{
2992 FORWARDING_NODE,
2993 "", /* This node has no interface. */
2994 1
2995};
2996
718e3744 2997/* Initialisation of zebra and installation of commands. */
2998void
a1ac18c4 2999zebra_init (void)
718e3744 3000{
3001 /* Client list init. */
b21b19c5 3002 zebrad.client_list = list_new ();
718e3744 3003
718e3744 3004 /* Install configuration write function. */
3005 install_node (&table_node, config_write_table);
3006 install_node (&forwarding_node, config_write_forwarding);
3007
3008 install_element (VIEW_NODE, &show_ip_forwarding_cmd);
647e4f1f 3009 install_element (CONFIG_NODE, &ip_forwarding_cmd);
718e3744 3010 install_element (CONFIG_NODE, &no_ip_forwarding_cmd);
3011 install_element (ENABLE_NODE, &show_zebra_client_cmd);
04b02fda 3012 install_element (ENABLE_NODE, &show_zebra_client_summary_cmd);
718e3744 3013
3014#ifdef HAVE_NETLINK
3015 install_element (VIEW_NODE, &show_table_cmd);
718e3744 3016 install_element (CONFIG_NODE, &config_table_cmd);
813d4307 3017 install_element (CONFIG_NODE, &no_config_table_cmd);
718e3744 3018#endif /* HAVE_NETLINK */
3019
718e3744 3020 install_element (VIEW_NODE, &show_ipv6_forwarding_cmd);
55906724 3021 install_element (CONFIG_NODE, &ipv6_forwarding_cmd);
718e3744 3022 install_element (CONFIG_NODE, &no_ipv6_forwarding_cmd);
7514fb77
PJ
3023
3024 /* Route-map */
3025 zebra_route_map_init ();
718e3744 3026}
97be79f9
DO
3027
3028/* Make zebra server socket, wiping any existing one (see bug #403). */
3029void
b5114685 3030zebra_zserv_socket_init (char *path)
97be79f9
DO
3031{
3032#ifdef HAVE_TCP_ZEBRA
3033 zebra_serv ();
3034#else
b5114685 3035 zebra_serv_un (path ? path : ZEBRA_SERV_PATH);
97be79f9
DO
3036#endif /* HAVE_TCP_ZEBRA */
3037}