]> git.proxmox.com Git - mirror_frr.git/blame - zebra/zserv.c
Merge pull request #634 from dwalton76/bgp-ipv6-nexthop-ll-and-global-takeII
[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 1399 struct stream *s;
79878cf7 1400 struct in6_addr nhop_addr;
8a92a8a0
DS
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;
79878cf7
DS
1410 static mpls_label_t labels[MULTIPATH_NUM];
1411 mpls_label_t label;
1412 struct nexthop *nexthop;
8a92a8a0
DS
1413
1414 /* Get input stream. */
1415 s = client->ibuf;
1416
79878cf7 1417 memset (&nhop_addr, 0, sizeof (struct in6_addr));
8a92a8a0
DS
1418
1419 /* Allocate new rib. */
1420 rib = XCALLOC (MTYPE_RIB, sizeof (struct rib));
1421
1422 /* Type, flags, message. */
1423 rib->type = stream_getc (s);
1424 rib->instance = stream_getw (s);
0fc452dc 1425 rib->flags = stream_getl (s);
8a92a8a0
DS
1426 message = stream_getc (s);
1427 safi = stream_getw (s);
1428 rib->uptime = time (NULL);
1429
1430 /* IPv4 prefix. */
1431 memset (&p, 0, sizeof (struct prefix_ipv4));
1432 p.family = AF_INET;
1433 p.prefixlen = stream_getc (s);
b4c034b0 1434 stream_get (&p.u.prefix4, s, PSIZE (p.prefixlen));
8a92a8a0 1435
154caaed 1436 /* VRF ID */
661512bf 1437 rib->vrf_id = zvrf_id (zvrf);
154caaed 1438
8a92a8a0
DS
1439 /* We need to give nh-addr, nh-ifindex with the same next-hop object
1440 * to the rib to ensure that IPv6 multipathing works; need to coalesce
1441 * these. Clients should send the same number of paired set of
1442 * next-hop-addr/next-hop-ifindices. */
1443 if (CHECK_FLAG (message, ZAPI_MESSAGE_NEXTHOP))
1444 {
37fe7731
DS
1445 unsigned int nh_count = 0;
1446 unsigned int if_count = 0;
1447 unsigned int max_nh_if = 0;
8a92a8a0
DS
1448
1449 nexthop_num = stream_getc (s);
6878b9db 1450 zserv_nexthop_num_warn(__func__, (const struct prefix *)&p, nexthop_num);
8a92a8a0
DS
1451 for (i = 0; i < nexthop_num; i++)
1452 {
1453 nexthop_type = stream_getc (s);
1454
1455 switch (nexthop_type)
1456 {
5b30316e 1457 case NEXTHOP_TYPE_IPV6:
79878cf7
DS
1458 stream_get (&nhop_addr, s, 16);
1459 if (nh_count < MULTIPATH_NUM)
1460 {
1461 /* For labeled-unicast, each nexthop is followed by label. */
1462 if (CHECK_FLAG (message, ZAPI_MESSAGE_LABEL))
1463 {
1464 label = (mpls_label_t)stream_getl (s);
1465 labels[nh_count] = label;
1466 }
1467 nexthops[nh_count] = nhop_addr;
1468 nh_count++;
1469 }
1470 break;
5b30316e 1471 case NEXTHOP_TYPE_IFINDEX:
37fe7731 1472 if (if_count < multipath_num) {
8a92a8a0
DS
1473 ifindices[if_count++] = stream_getl (s);
1474 }
1475 break;
5b30316e 1476 case NEXTHOP_TYPE_BLACKHOLE:
a399694f 1477 rib_nexthop_blackhole_add (rib);
8a92a8a0
DS
1478 break;
1479 }
1480 }
1481
1482 max_nh_if = (nh_count > if_count) ? nh_count : if_count;
1483 for (i = 0; i < max_nh_if; i++)
1484 {
1485 if ((i < nh_count) && !IN6_IS_ADDR_UNSPECIFIED (&nexthops[i])) {
79878cf7
DS
1486 if ((i < if_count) && ifindices[i])
1487 nexthop = rib_nexthop_ipv6_ifindex_add (rib, &nexthops[i], ifindices[i]);
1488 else
1489 nexthop = rib_nexthop_ipv6_add (rib, &nexthops[i]);
1490
1491 if (CHECK_FLAG (message, ZAPI_MESSAGE_LABEL))
1492 nexthop_add_labels (nexthop, nexthop->nh_label_type, 1, &labels[i]);
8a92a8a0
DS
1493 }
1494 else {
79878cf7 1495 if ((i < if_count) && ifindices[i])
a399694f 1496 rib_nexthop_ifindex_add (rib, ifindices[i]);
79878cf7 1497 }
8a92a8a0
DS
1498 }
1499 }
1500
1501 /* Distance. */
1502 if (CHECK_FLAG (message, ZAPI_MESSAGE_DISTANCE))
1503 rib->distance = stream_getc (s);
1504
1505 /* Metric. */
1506 if (CHECK_FLAG (message, ZAPI_MESSAGE_METRIC))
1507 rib->metric = stream_getl (s);
1508
1509 /* Tag */
1510 if (CHECK_FLAG (message, ZAPI_MESSAGE_TAG))
dc9ffce8 1511 rib->tag = stream_getl (s);
8a92a8a0
DS
1512 else
1513 rib->tag = 0;
1514
c50ca33a
TT
1515 if (CHECK_FLAG (message, ZAPI_MESSAGE_MTU))
1516 rib->mtu = stream_getl (s);
1517 else
1518 rib->mtu = 0;
1519
8a92a8a0 1520 /* Table */
d651649e 1521 rib->table = zvrf->table_id;
12f6fb97 1522
3c7c91d0 1523 ret = rib_add_multipath (AFI_IP6, safi, &p, NULL, rib);
8a92a8a0
DS
1524 /* Stats */
1525 if (ret > 0)
1526 client->v4_route_add_cnt++;
1527 else if (ret < 0)
1528 client->v4_route_upd8_cnt++;
1529
1530 return 0;
1531}
1532
719e9741 1533static int
d651649e 1534zread_ipv6_add (struct zserv *client, u_short length, struct zebra_vrf *zvrf)
718e3744 1535{
37fe7731 1536 unsigned int i;
718e3744 1537 struct stream *s;
a64448ba 1538 struct in6_addr nhop_addr;
41fc2714
DS
1539 struct rib *rib;
1540 u_char message;
1541 u_char nexthop_num;
1542 u_char nexthop_type;
b4c034b0 1543 struct prefix p;
3c7c91d0 1544 struct prefix_ipv6 src_p, *src_pp;
41fc2714
DS
1545 safi_t safi;
1546 static struct in6_addr nexthops[MULTIPATH_NUM];
1547 static unsigned int ifindices[MULTIPATH_NUM];
04b02fda 1548 int ret;
a64448ba
DS
1549 static mpls_label_t labels[MULTIPATH_NUM];
1550 mpls_label_t label;
1551 struct nexthop *nexthop;
41fc2714
DS
1552
1553 /* Get input stream. */
718e3744 1554 s = client->ibuf;
41fc2714 1555
a64448ba 1556 memset (&nhop_addr, 0, sizeof (struct in6_addr));
718e3744 1557
41fc2714
DS
1558 /* Allocate new rib. */
1559 rib = XCALLOC (MTYPE_RIB, sizeof (struct rib));
1560
718e3744 1561 /* Type, flags, message. */
41fc2714 1562 rib->type = stream_getc (s);
7c8ff89e 1563 rib->instance = stream_getw (s);
0fc452dc 1564 rib->flags = stream_getl (s);
41fc2714
DS
1565 message = stream_getc (s);
1566 safi = stream_getw (s);
1567 rib->uptime = time (NULL);
718e3744 1568
41fc2714 1569 /* IPv6 prefix. */
718e3744 1570 memset (&p, 0, sizeof (struct prefix_ipv6));
1571 p.family = AF_INET6;
1572 p.prefixlen = stream_getc (s);
b4c034b0 1573 stream_get (&p.u.prefix6, s, PSIZE (p.prefixlen));
718e3744 1574
3c7c91d0
DL
1575 if (CHECK_FLAG (message, ZAPI_MESSAGE_SRCPFX))
1576 {
1577 memset (&src_p, 0, sizeof (struct prefix_ipv6));
1578 src_p.family = AF_INET6;
1579 src_p.prefixlen = stream_getc (s);
1580 stream_get (&src_p.prefix, s, PSIZE (src_p.prefixlen));
1581 src_pp = &src_p;
1582 }
1583 else
1584 src_pp = NULL;
1585
41fc2714
DS
1586 /* We need to give nh-addr, nh-ifindex with the same next-hop object
1587 * to the rib to ensure that IPv6 multipathing works; need to coalesce
1588 * these. Clients should send the same number of paired set of
1589 * next-hop-addr/next-hop-ifindices. */
1590 if (CHECK_FLAG (message, ZAPI_MESSAGE_NEXTHOP))
718e3744 1591 {
37fe7731
DS
1592 unsigned int nh_count = 0;
1593 unsigned int if_count = 0;
1594 unsigned int max_nh_if = 0;
718e3744 1595
41fc2714 1596 nexthop_num = stream_getc (s);
6878b9db
DS
1597 zserv_nexthop_num_warn(__func__, (const struct prefix *)&p, nexthop_num);
1598 for (i = 0; i < nexthop_num; i++)
718e3744 1599 {
1600 nexthop_type = stream_getc (s);
1601
1602 switch (nexthop_type)
1603 {
5b30316e 1604 case NEXTHOP_TYPE_IPV6:
a64448ba
DS
1605 stream_get (&nhop_addr, s, 16);
1606 if (nh_count < MULTIPATH_NUM)
1607 {
1608 /* For labeled-unicast, each nexthop is followed by label. */
1609 if (CHECK_FLAG (message, ZAPI_MESSAGE_LABEL))
1610 {
1611 label = (mpls_label_t)stream_getl (s);
4038e804 1612 labels[nh_count] = label;
a64448ba
DS
1613 }
1614 nexthops[nh_count++] = nhop_addr;
1615 }
718e3744 1616 break;
5b30316e 1617 case NEXTHOP_TYPE_IFINDEX:
37fe7731 1618 if (if_count < multipath_num) {
41fc2714
DS
1619 ifindices[if_count++] = stream_getl (s);
1620 }
718e3744 1621 break;
5b30316e 1622 case NEXTHOP_TYPE_BLACKHOLE:
a399694f 1623 rib_nexthop_blackhole_add (rib);
c3c0ac83 1624 break;
718e3744 1625 }
1626 }
41fc2714
DS
1627
1628 max_nh_if = (nh_count > if_count) ? nh_count : if_count;
1629 for (i = 0; i < max_nh_if; i++)
1630 {
1631 if ((i < nh_count) && !IN6_IS_ADDR_UNSPECIFIED (&nexthops[i])) {
6f20b80d 1632 if ((i < if_count) && ifindices[i])
a64448ba 1633 nexthop = rib_nexthop_ipv6_ifindex_add (rib, &nexthops[i], ifindices[i]);
6f20b80d 1634 else
a64448ba
DS
1635 nexthop = rib_nexthop_ipv6_add (rib, &nexthops[i]);
1636 if (CHECK_FLAG (message, ZAPI_MESSAGE_LABEL))
1637 nexthop_add_labels (nexthop, nexthop->nh_label_type, 1, &labels[i]);
41fc2714
DS
1638 }
1639 else {
6f20b80d 1640 if ((i < if_count) && ifindices[i])
a399694f 1641 rib_nexthop_ifindex_add (rib, ifindices[i]);
41fc2714
DS
1642 }
1643 }
718e3744 1644 }
1645
41fc2714
DS
1646 /* Distance. */
1647 if (CHECK_FLAG (message, ZAPI_MESSAGE_DISTANCE))
1648 rib->distance = stream_getc (s);
718e3744 1649
41fc2714
DS
1650 /* Metric. */
1651 if (CHECK_FLAG (message, ZAPI_MESSAGE_METRIC))
1652 rib->metric = stream_getl (s);
718e3744 1653
0d9551dc
DS
1654 /* Tag */
1655 if (CHECK_FLAG (message, ZAPI_MESSAGE_TAG))
dc9ffce8 1656 rib->tag = stream_getl (s);
0d9551dc
DS
1657 else
1658 rib->tag = 0;
c50ca33a
TT
1659
1660 if (CHECK_FLAG (message, ZAPI_MESSAGE_MTU))
1661 rib->mtu = stream_getl (s);
1662 else
1663 rib->mtu = 0;
0d9551dc 1664
154caaed 1665 /* VRF ID */
661512bf 1666 rib->vrf_id = zvrf_id (zvrf);
d651649e 1667 rib->table = zvrf->table_id;
154caaed 1668
3c7c91d0 1669 ret = rib_add_multipath (AFI_IP6, safi, &p, src_pp, rib);
04b02fda
DS
1670 /* Stats */
1671 if (ret > 0)
1672 client->v6_route_add_cnt++;
1673 else if (ret < 0)
1674 client->v6_route_upd8_cnt++;
1675
719e9741 1676 return 0;
718e3744 1677}
1678
1679/* Zebra server IPv6 prefix delete function. */
719e9741 1680static int
d651649e 1681zread_ipv6_delete (struct zserv *client, u_short length, struct zebra_vrf *zvrf)
718e3744 1682{
1683 int i;
1684 struct stream *s;
1685 struct zapi_ipv6 api;
1686 struct in6_addr nexthop;
4b2792b5 1687 union g_addr *pnexthop = NULL;
718e3744 1688 unsigned long ifindex;
616368ed 1689 struct prefix p;
3c7c91d0 1690 struct prefix_ipv6 src_p, *src_pp;
718e3744 1691
1692 s = client->ibuf;
1693 ifindex = 0;
1694 memset (&nexthop, 0, sizeof (struct in6_addr));
1695
1696 /* Type, flags, message. */
1697 api.type = stream_getc (s);
7c8ff89e 1698 api.instance = stream_getw (s);
0fc452dc 1699 api.flags = stream_getl (s);
718e3744 1700 api.message = stream_getc (s);
f768f367 1701 api.safi = stream_getw (s);
718e3744 1702
1703 /* IPv4 prefix. */
1704 memset (&p, 0, sizeof (struct prefix_ipv6));
1705 p.family = AF_INET6;
1706 p.prefixlen = stream_getc (s);
616368ed 1707 stream_get (&p.u.prefix6, s, PSIZE (p.prefixlen));
718e3744 1708
3c7c91d0
DL
1709 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_SRCPFX))
1710 {
1711 memset (&src_p, 0, sizeof (struct prefix_ipv6));
1712 src_p.family = AF_INET6;
1713 src_p.prefixlen = stream_getc (s);
1714 stream_get (&src_p.prefix, s, PSIZE (src_p.prefixlen));
1715 src_pp = &src_p;
1716 }
1717 else
1718 src_pp = NULL;
1719
718e3744 1720 /* Nexthop, ifindex, distance, metric. */
1721 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
1722 {
1723 u_char nexthop_type;
1724
1725 api.nexthop_num = stream_getc (s);
1726 for (i = 0; i < api.nexthop_num; i++)
1727 {
1728 nexthop_type = stream_getc (s);
1729
1730 switch (nexthop_type)
1731 {
5b30316e 1732 case NEXTHOP_TYPE_IPV6:
718e3744 1733 stream_get (&nexthop, s, 16);
a64448ba
DS
1734 /* For labeled-unicast, each nexthop is followed by label, but
1735 * we don't care for delete.
1736 */
1737 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_LABEL))
1738 stream_forward_getp (s, sizeof(u_int32_t));
616368ed 1739 pnexthop = (union g_addr *)&nexthop;
718e3744 1740 break;
5b30316e 1741 case NEXTHOP_TYPE_IFINDEX:
718e3744 1742 ifindex = stream_getl (s);
1743 break;
1744 }
1745 }
1746 }
1747
0d9551dc 1748 /* Distance. */
718e3744 1749 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
1750 api.distance = stream_getc (s);
1751 else
1752 api.distance = 0;
0d9551dc
DS
1753
1754 /* Metric. */
718e3744 1755 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
1756 api.metric = stream_getl (s);
1757 else
1758 api.metric = 0;
1759
0d9551dc
DS
1760 /* tag */
1761 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_TAG))
dc9ffce8 1762 api.tag = stream_getl (s);
0d9551dc
DS
1763 else
1764 api.tag = 0;
1765
718e3744 1766 if (IN6_IS_ADDR_UNSPECIFIED (&nexthop))
661512bf 1767 rib_delete (AFI_IP6, api.safi, zvrf_id (zvrf), api.type, api.instance,
3c7c91d0 1768 api.flags, &p, src_pp, NULL, ifindex, client->rtm_table);
718e3744 1769 else
661512bf 1770 rib_delete (AFI_IP6, api.safi, zvrf_id (zvrf), api.type, api.instance,
3c7c91d0 1771 api.flags, &p, src_pp, pnexthop, ifindex, client->rtm_table);
04b02fda
DS
1772
1773 client->v6_route_del_cnt++;
719e9741 1774 return 0;
718e3744 1775}
1776
18a6dce6 1777/* Register zebra server router-id information. Send current router-id */
719e9741 1778static int
d651649e 1779zread_router_id_add (struct zserv *client, u_short length, struct zebra_vrf *zvrf)
18a6dce6 1780{
1781 struct prefix p;
1782
1783 /* Router-id information is needed. */
661512bf 1784 vrf_bitmap_set (client->ridinfo, zvrf_id (zvrf));
18a6dce6 1785
661512bf 1786 router_id_get (&p, zvrf_id (zvrf));
18a6dce6 1787
661512bf 1788 return zsend_router_id_update (client, &p, zvrf_id (zvrf));
18a6dce6 1789}
1790
1791/* Unregister zebra server router-id information. */
719e9741 1792static int
d651649e 1793zread_router_id_delete (struct zserv *client, u_short length, struct zebra_vrf *zvrf)
18a6dce6 1794{
661512bf 1795 vrf_bitmap_unset (client->ridinfo, zvrf_id (zvrf));
719e9741 1796 return 0;
18a6dce6 1797}
1798
2ea1ab1c
VT
1799/* Tie up route-type and client->sock */
1800static void
1801zread_hello (struct zserv *client)
1802{
1803 /* type of protocol (lib/zebra.h) */
1804 u_char proto;
7c8ff89e
DS
1805 u_short instance;
1806
2ea1ab1c 1807 proto = stream_getc (client->ibuf);
7c8ff89e 1808 instance = stream_getw (client->ibuf);
2ea1ab1c
VT
1809
1810 /* accept only dynamic routing protocols */
1811 if ((proto < ZEBRA_ROUTE_MAX)
1812 && (proto > ZEBRA_ROUTE_STATIC))
1813 {
1814 zlog_notice ("client %d says hello and bids fair to announce only %s routes",
1815 client->sock, zebra_route_string(proto));
7c8ff89e
DS
1816 if (instance)
1817 zlog_notice ("client protocol instance %d", instance);
2ea1ab1c 1818
fb018d25 1819 client->proto = proto;
7c8ff89e 1820 client->instance = instance;
2ea1ab1c
VT
1821 }
1822}
1823
7076bb2f
FL
1824/* Unregister all information in a VRF. */
1825static int
d651649e 1826zread_vrf_unregister (struct zserv *client, u_short length, struct zebra_vrf *zvrf)
7076bb2f
FL
1827{
1828 int i;
1829 afi_t afi;
1830
1831 for (afi = AFI_IP; afi < AFI_MAX; afi++)
1832 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
661512bf
RW
1833 vrf_bitmap_unset (client->redist[afi][i], zvrf_id (zvrf));
1834 vrf_bitmap_unset (client->redist_default, zvrf_id (zvrf));
1835 vrf_bitmap_unset (client->ifinfo, zvrf_id (zvrf));
1836 vrf_bitmap_unset (client->ridinfo, zvrf_id (zvrf));
7076bb2f
FL
1837
1838 return 0;
1839}
1840
ce549947
RW
1841static void
1842zread_mpls_labels (int command, struct zserv *client, u_short length,
1843 vrf_id_t vrf_id)
1844{
1845 struct stream *s;
1846 enum lsp_types_t type;
1847 struct prefix prefix;
1848 enum nexthop_types_t gtype;
1849 union g_addr gate;
88d88a9c 1850 ifindex_t ifindex;
ce549947
RW
1851 mpls_label_t in_label, out_label;
1852 u_int8_t distance;
1853 struct zebra_vrf *zvrf;
1854
1855 zvrf = vrf_info_lookup (vrf_id);
1856 if (!zvrf)
1857 return;
1858
1859 /* Get input stream. */
1860 s = client->ibuf;
1861
1862 /* Get data. */
1863 type = stream_getc (s);
1864 prefix.family = stream_getl (s);
1865 switch (prefix.family)
1866 {
1867 case AF_INET:
1868 prefix.u.prefix4.s_addr = stream_get_ipv4 (s);
1869 prefix.prefixlen = stream_getc (s);
ce549947
RW
1870 gate.ipv4.s_addr = stream_get_ipv4 (s);
1871 break;
1872 case AF_INET6:
1873 stream_get (&prefix.u.prefix6, s, 16);
1874 prefix.prefixlen = stream_getc (s);
ce549947
RW
1875 stream_get (&gate.ipv6, s, 16);
1876 break;
1877 default:
1878 return;
1879 }
88d88a9c 1880 ifindex = stream_getl (s);
ce549947
RW
1881 distance = stream_getc (s);
1882 in_label = stream_getl (s);
1883 out_label = stream_getl (s);
1884
88d88a9c
RW
1885 switch (prefix.family)
1886 {
1887 case AF_INET:
1888 if (ifindex)
1889 gtype = NEXTHOP_TYPE_IPV4_IFINDEX;
1890 else
1891 gtype = NEXTHOP_TYPE_IPV4;
1892 break;
1893 case AF_INET6:
1894 if (ifindex)
1895 gtype = NEXTHOP_TYPE_IPV6_IFINDEX;
1896 else
1897 gtype = NEXTHOP_TYPE_IPV6;
1898 break;
1899 default:
1900 return;
1901 }
1902
fe6c7157
RW
1903 if (! mpls_enabled)
1904 return;
1905
ce549947
RW
1906 if (command == ZEBRA_MPLS_LABELS_ADD)
1907 {
1908 mpls_lsp_install (zvrf, type, in_label, out_label, gtype, &gate,
a64448ba 1909 ifindex);
ce549947 1910 if (out_label != MPLS_IMP_NULL_LABEL)
88d88a9c
RW
1911 mpls_ftn_update (1, zvrf, type, &prefix, gtype, &gate, ifindex,
1912 distance, out_label);
ce549947
RW
1913 }
1914 else if (command == ZEBRA_MPLS_LABELS_DELETE)
1915 {
a64448ba 1916 mpls_lsp_uninstall (zvrf, type, in_label, gtype, &gate, ifindex);
ce549947 1917 if (out_label != MPLS_IMP_NULL_LABEL)
88d88a9c
RW
1918 mpls_ftn_update (0, zvrf, type, &prefix, gtype, &gate, ifindex,
1919 distance, out_label);
ce549947
RW
1920 }
1921}
fea12efb 1922/* Send response to a label manager connect request to client */
1923static int
1924zsend_label_manager_connect_response (struct zserv *client, vrf_id_t vrf_id, u_short result)
1925{
1926 struct stream *s;
1927
1928 s = client->obuf;
1929 stream_reset (s);
1930
1931 zserv_create_header (s, ZEBRA_LABEL_MANAGER_CONNECT, vrf_id);
1932
1933 /* result */
1934 stream_putc (s, result);
1935
1936 /* Write packet size. */
1937 stream_putw_at (s, 0, stream_get_endp (s));
1938
1939 return writen (client->sock, s->data, stream_get_endp (s));
1940}
1941
1942static void
1943zread_label_manager_connect (struct zserv *client, vrf_id_t vrf_id)
1944{
1945 struct stream *s;
1946 /* type of protocol (lib/zebra.h) */
1947 u_char proto;
1948 u_short instance;
1949
1950 /* Get input stream. */
1951 s = client->ibuf;
1952
1953 /* Get data. */
1954 proto = stream_getc (s);
1955 instance = stream_getw (s);
1956
1957 /* accept only dynamic routing protocols */
1958 if ((proto >= ZEBRA_ROUTE_MAX)
1959 || (proto <= ZEBRA_ROUTE_STATIC))
1960 {
1961 zlog_err ("client %d has wrong protocol %s",
1962 client->sock, zebra_route_string(proto));
1963 zsend_label_manager_connect_response (client, vrf_id, 1);
1964 return;
1965 }
1966 zlog_notice ("client %d with instance %u connected as %s",
1967 client->sock, instance, zebra_route_string(proto));
1968 client->proto = proto;
1969 client->instance = instance;
1970
1971 /*
1972 Release previous labels of same protocol and instance.
1973 This is done in case it restarted from an unexpected shutdown.
1974 */
1975 release_daemon_chunks (proto, instance);
1976
1977 zlog_debug (" Label Manager client connected: sock %d, proto %s, instance %u",
1978 client->sock, zebra_route_string(proto), instance);
1979 /* send response back */
1980 zsend_label_manager_connect_response (client, vrf_id, 0);
1981}
1982/* Send response to a get label chunk request to client */
1983static int
1984zsend_assign_label_chunk_response (struct zserv *client, vrf_id_t vrf_id,
1985 struct label_manager_chunk *lmc)
1986{
1987 struct stream *s;
1988
1989 s = client->obuf;
1990 stream_reset (s);
1991
1992 zserv_create_header (s, ZEBRA_GET_LABEL_CHUNK, vrf_id);
1993
1994 if (lmc)
1995 {
1996 /* keep */
1997 stream_putc (s, lmc->keep);
1998 /* start and end labels */
1999 stream_putl (s, lmc->start);
2000 stream_putl (s, lmc->end);
2001
2002 }
2003
2004 /* Write packet size. */
2005 stream_putw_at (s, 0, stream_get_endp (s));
2006
2007 return writen (client->sock, s->data, stream_get_endp (s));
2008}
2009
2010static void
2011zread_get_label_chunk (struct zserv *client, vrf_id_t vrf_id)
2012{
2013 struct stream *s;
2014 u_char keep;
2015 uint32_t size;
2016 struct label_manager_chunk *lmc;
2017
2018 /* Get input stream. */
2019 s = client->ibuf;
2020
2021 /* Get data. */
2022 keep = stream_getc (s);
2023 size = stream_getl (s);
2024
2025 lmc = assign_label_chunk (client->proto, client->instance, keep, size);
2026 if (!lmc)
2027 zlog_err ("%s: Unable to assign Label Chunk of size %u", __func__, size);
2028 else
2029 zlog_debug ("Assigned Label Chunk %u - %u to %u",
2030 lmc->start, lmc->end, keep);
2031 /* send response back */
2032 zsend_assign_label_chunk_response (client, vrf_id, lmc);
2033}
2034
2035static void
2036zread_release_label_chunk (struct zserv *client)
2037{
2038 struct stream *s;
2039 uint32_t start, end;
2040
2041 /* Get input stream. */
2042 s = client->ibuf;
2043
2044 /* Get data. */
2045 start = stream_getl (s);
2046 end = stream_getl (s);
2047
2048 release_label_chunk (client->proto, client->instance, start, end);
2049}
2050static void
2051zread_label_manager_request (int cmd, struct zserv *client, vrf_id_t vrf_id)
2052{
2053 /* to avoid sending other messages like ZERBA_INTERFACE_UP */
2054 if (cmd == ZEBRA_LABEL_MANAGER_CONNECT)
2055 client->is_synchronous = 1;
2056
2057 /* external label manager */
2058 if (lm_is_external)
5c7ef8dc 2059 zread_relay_label_manager_request (cmd, client, vrf_id);
fea12efb 2060 /* this is a label manager */
2061 else
2062 {
2063 if (cmd == ZEBRA_LABEL_MANAGER_CONNECT)
2064 zread_label_manager_connect (client, vrf_id);
2065 else
2066 {
2067 /* Sanity: don't allow 'unidentified' requests */
2068 if (!client->proto)
2069 {
2070 zlog_err ("Got label request from an unidentified client");
2071 return;
2072 }
2073 if (cmd == ZEBRA_GET_LABEL_CHUNK)
2074 zread_get_label_chunk (client, vrf_id);
2075 else if (cmd == ZEBRA_RELEASE_LABEL_CHUNK)
2076 zread_release_label_chunk (client);
2077 }
2078 }
2079}
ce549947 2080
4b33b75a 2081/* Cleanup registered nexthops (across VRFs) upon client disconnect. */
2082static void
2083zebra_client_close_cleanup_rnh (struct zserv *client)
2084{
1a1a7065 2085 struct vrf *vrf;
4b33b75a 2086 struct zebra_vrf *zvrf;
2087
1a1a7065 2088 RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id)
4b33b75a 2089 {
1a1a7065 2090 if ((zvrf = vrf->info) != NULL)
4b33b75a 2091 {
661512bf
RW
2092 zebra_cleanup_rnh_client(zvrf_id (zvrf), AF_INET, client, RNH_NEXTHOP_TYPE);
2093 zebra_cleanup_rnh_client(zvrf_id (zvrf), AF_INET6, client, RNH_NEXTHOP_TYPE);
2094 zebra_cleanup_rnh_client(zvrf_id (zvrf), AF_INET, client, RNH_IMPORT_CHECK_TYPE);
2095 zebra_cleanup_rnh_client(zvrf_id (zvrf), AF_INET6, client, RNH_IMPORT_CHECK_TYPE);
ce549947
RW
2096 if (client->proto == ZEBRA_ROUTE_LDP)
2097 {
2098 hash_iterate(zvrf->lsp_table, mpls_ldp_lsp_uninstall_all,
2099 zvrf->lsp_table);
2100 mpls_ldp_ftn_uninstall_all (zvrf, AFI_IP);
2101 mpls_ldp_ftn_uninstall_all (zvrf, AFI_IP6);
2102 }
4b33b75a 2103 }
2104 }
2105}
2106
718e3744 2107/* Close zebra client. */
b9df2d25 2108static void
718e3744 2109zebra_client_close (struct zserv *client)
2110{
567b877d 2111 /* Send client de-registration to BFD */
2376c3f2 2112 zebra_ptm_bfd_client_deregister(client->proto);
567b877d 2113
4b33b75a 2114 /* Cleanup any registered nexthops - across all VRFs. */
2115 zebra_client_close_cleanup_rnh (client);
fb018d25 2116
fea12efb 2117 /* Release Label Manager chunks */
2118 release_daemon_chunks (client->proto, client->instance);
2119
5aba114a
DS
2120 /* Cleanup any FECs registered by this client. */
2121 zebra_mpls_cleanup_fecs_for_client (vrf_info_lookup(VRF_DEFAULT), client);
2122
718e3744 2123 /* Close file descriptor. */
2124 if (client->sock)
2125 {
7c8ff89e
DS
2126 unsigned long nroutes;
2127
718e3744 2128 close (client->sock);
7c8ff89e
DS
2129 nroutes = rib_score_proto (client->proto, client->instance);
2130 zlog_notice ("client %d disconnected. %lu %s routes removed from the rib",
2131 client->sock, nroutes, zebra_route_string (client->proto));
718e3744 2132 client->sock = -1;
2133 }
2134
2135 /* Free stream buffers. */
2136 if (client->ibuf)
2137 stream_free (client->ibuf);
2138 if (client->obuf)
2139 stream_free (client->obuf);
719e9741 2140 if (client->wb)
2141 buffer_free(client->wb);
718e3744 2142
2143 /* Release threads. */
2144 if (client->t_read)
2145 thread_cancel (client->t_read);
2146 if (client->t_write)
2147 thread_cancel (client->t_write);
719e9741 2148 if (client->t_suicide)
2149 thread_cancel (client->t_suicide);
718e3744 2150
2151 /* Free client structure. */
b21b19c5 2152 listnode_delete (zebrad.client_list, client);
c66f9c61 2153 XFREE (MTYPE_TMP, client);
718e3744 2154}
2155
2156/* Make new client. */
b9df2d25 2157static void
718e3744 2158zebra_client_create (int sock)
2159{
2160 struct zserv *client;
7076bb2f
FL
2161 int i;
2162 afi_t afi;
718e3744 2163
c66f9c61 2164 client = XCALLOC (MTYPE_TMP, sizeof (struct zserv));
718e3744 2165
2166 /* Make client input/output buffer. */
2167 client->sock = sock;
2168 client->ibuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
2169 client->obuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
719e9741 2170 client->wb = buffer_new(0);
718e3744 2171
2172 /* Set table number. */
b21b19c5 2173 client->rtm_table = zebrad.rtm_table_default;
718e3744 2174
cf672a86 2175 client->connect_time = monotime(NULL);
7076bb2f
FL
2176 /* Initialize flags */
2177 for (afi = AFI_IP; afi < AFI_MAX; afi++)
2178 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
2179 client->redist[afi][i] = vrf_bitmap_init ();
2180 client->redist_default = vrf_bitmap_init ();
2181 client->ifinfo = vrf_bitmap_init ();
2182 client->ridinfo = vrf_bitmap_init ();
04b02fda 2183
fea12efb 2184 /* by default, it's not a synchronous client */
2185 client->is_synchronous = 0;
2186
718e3744 2187 /* Add this client to linked list. */
b21b19c5 2188 listnode_add (zebrad.client_list, client);
718e3744 2189
2190 /* Make new read thread. */
2191 zebra_event (ZEBRA_READ, sock, client);
12f6fb97
DS
2192
2193 zebra_vrf_update_all (client);
718e3744 2194}
2195
2196/* Handler of zebra service request. */
b9df2d25 2197static int
718e3744 2198zebra_client_read (struct thread *thread)
2199{
2200 int sock;
2201 struct zserv *client;
57a1477b 2202 size_t already;
c1b9800a 2203 uint16_t length, command;
2204 uint8_t marker, version;
7076bb2f 2205 vrf_id_t vrf_id;
d651649e 2206 struct zebra_vrf *zvrf;
718e3744 2207
2208 /* Get thread data. Reset reading thread because I'm running. */
2209 sock = THREAD_FD (thread);
2210 client = THREAD_ARG (thread);
2211 client->t_read = NULL;
2212
719e9741 2213 if (client->t_suicide)
718e3744 2214 {
719e9741 2215 zebra_client_close(client);
718e3744 2216 return -1;
2217 }
719e9741 2218
2219 /* Read length and command (if we don't have it already). */
57a1477b 2220 if ((already = stream_get_endp(client->ibuf)) < ZEBRA_HEADER_SIZE)
719e9741 2221 {
57a1477b 2222 ssize_t nbyte;
719e9741 2223 if (((nbyte = stream_read_try (client->ibuf, sock,
57a1477b 2224 ZEBRA_HEADER_SIZE-already)) == 0) ||
719e9741 2225 (nbyte == -1))
2226 {
2227 if (IS_ZEBRA_DEBUG_EVENT)
2228 zlog_debug ("connection closed socket [%d]", sock);
2229 zebra_client_close (client);
2230 return -1;
2231 }
57a1477b 2232 if (nbyte != (ssize_t)(ZEBRA_HEADER_SIZE-already))
719e9741 2233 {
2234 /* Try again later. */
2235 zebra_event (ZEBRA_READ, sock, client);
2236 return 0;
2237 }
57a1477b 2238 already = ZEBRA_HEADER_SIZE;
719e9741 2239 }
2240
2241 /* Reset to read from the beginning of the incoming packet. */
2242 stream_set_getp(client->ibuf, 0);
2243
c1b9800a 2244 /* Fetch header values */
718e3744 2245 length = stream_getw (client->ibuf);
c1b9800a 2246 marker = stream_getc (client->ibuf);
2247 version = stream_getc (client->ibuf);
7076bb2f 2248 vrf_id = stream_getw (client->ibuf);
c1b9800a 2249 command = stream_getw (client->ibuf);
718e3744 2250
c1b9800a 2251 if (marker != ZEBRA_HEADER_MARKER || version != ZSERV_VERSION)
2252 {
2253 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
2254 __func__, sock, marker, version);
2255 zebra_client_close (client);
2256 return -1;
2257 }
719e9741 2258 if (length < ZEBRA_HEADER_SIZE)
718e3744 2259 {
57a1477b 2260 zlog_warn("%s: socket %d message length %u is less than header size %d",
2261 __func__, sock, length, ZEBRA_HEADER_SIZE);
2262 zebra_client_close (client);
2263 return -1;
2264 }
2265 if (length > STREAM_SIZE(client->ibuf))
2266 {
2267 zlog_warn("%s: socket %d message length %u exceeds buffer size %lu",
2268 __func__, sock, length, (u_long)STREAM_SIZE(client->ibuf));
718e3744 2269 zebra_client_close (client);
2270 return -1;
2271 }
2272
718e3744 2273 /* Read rest of data. */
57a1477b 2274 if (already < length)
718e3744 2275 {
57a1477b 2276 ssize_t nbyte;
2277 if (((nbyte = stream_read_try (client->ibuf, sock,
2278 length-already)) == 0) ||
2279 (nbyte == -1))
718e3744 2280 {
2281 if (IS_ZEBRA_DEBUG_EVENT)
b6178002 2282 zlog_debug ("connection closed [%d] when reading zebra data", sock);
718e3744 2283 zebra_client_close (client);
2284 return -1;
2285 }
57a1477b 2286 if (nbyte != (ssize_t)(length-already))
719e9741 2287 {
2288 /* Try again later. */
2289 zebra_event (ZEBRA_READ, sock, client);
2290 return 0;
2291 }
718e3744 2292 }
2293
719e9741 2294 length -= ZEBRA_HEADER_SIZE;
2295
718e3744 2296 /* Debug packet information. */
2297 if (IS_ZEBRA_DEBUG_EVENT)
b6178002 2298 zlog_debug ("zebra message comes from socket [%d]", sock);
718e3744 2299
2300 if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
7076bb2f
FL
2301 zlog_debug ("zebra message received [%s] %d in VRF %u",
2302 zserv_command_string (command), length, vrf_id);
718e3744 2303
cf672a86 2304 client->last_read_time = monotime(NULL);
04b02fda
DS
2305 client->last_read_cmd = command;
2306
5f3d1bdf 2307 zvrf = zebra_vrf_lookup_by_id (vrf_id);
d651649e
DS
2308 if (!zvrf)
2309 {
2310 if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
2311 zlog_debug ("zebra received unknown VRF[%u]", vrf_id);
2312 goto zclient_read_out;
2313 }
2314
718e3744 2315 switch (command)
2316 {
18a6dce6 2317 case ZEBRA_ROUTER_ID_ADD:
d651649e 2318 zread_router_id_add (client, length, zvrf);
18a6dce6 2319 break;
2320 case ZEBRA_ROUTER_ID_DELETE:
d651649e 2321 zread_router_id_delete (client, length, zvrf);
18a6dce6 2322 break;
718e3744 2323 case ZEBRA_INTERFACE_ADD:
d651649e 2324 zread_interface_add (client, length, zvrf);
718e3744 2325 break;
2326 case ZEBRA_INTERFACE_DELETE:
d651649e 2327 zread_interface_delete (client, length, zvrf);
718e3744 2328 break;
2329 case ZEBRA_IPV4_ROUTE_ADD:
d651649e 2330 zread_ipv4_add (client, length, zvrf);
718e3744 2331 break;
2332 case ZEBRA_IPV4_ROUTE_DELETE:
d651649e 2333 zread_ipv4_delete (client, length, zvrf);
718e3744 2334 break;
8a92a8a0 2335 case ZEBRA_IPV4_ROUTE_IPV6_NEXTHOP_ADD:
d651649e 2336 zread_ipv4_route_ipv6_nexthop_add (client, length, zvrf);
8a92a8a0 2337 break;
65efcfce
LB
2338 case ZEBRA_IPV4_NEXTHOP_ADD:
2339 zread_ipv4_add(client, length, zvrf); /* LB: r1.0 merge - id was 1 */
2340 break;
2341 case ZEBRA_IPV4_NEXTHOP_DELETE:
2342 zread_ipv4_delete(client, length, zvrf); /* LB: r1.0 merge - id was 1 */
2343 break;
718e3744 2344 case ZEBRA_IPV6_ROUTE_ADD:
d651649e 2345 zread_ipv6_add (client, length, zvrf);
718e3744 2346 break;
2347 case ZEBRA_IPV6_ROUTE_DELETE:
d651649e 2348 zread_ipv6_delete (client, length, zvrf);
718e3744 2349 break;
718e3744 2350 case ZEBRA_REDISTRIBUTE_ADD:
d651649e 2351 zebra_redistribute_add (command, client, length, zvrf);
718e3744 2352 break;
2353 case ZEBRA_REDISTRIBUTE_DELETE:
d651649e 2354 zebra_redistribute_delete (command, client, length, zvrf);
718e3744 2355 break;
2356 case ZEBRA_REDISTRIBUTE_DEFAULT_ADD:
d651649e 2357 zebra_redistribute_default_add (command, client, length, zvrf);
718e3744 2358 break;
2359 case ZEBRA_REDISTRIBUTE_DEFAULT_DELETE:
d651649e 2360 zebra_redistribute_default_delete (command, client, length, zvrf);
718e3744 2361 break;
6f61a5a3
EM
2362 case ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB:
2363 zread_ipv4_nexthop_lookup_mrib (client, length, zvrf);
718e3744 2364 break;
2ea1ab1c
VT
2365 case ZEBRA_HELLO:
2366 zread_hello (client);
2367 break;
fb018d25 2368 case ZEBRA_NEXTHOP_REGISTER:
d651649e 2369 zserv_rnh_register(client, sock, length, RNH_NEXTHOP_TYPE, zvrf);
fb018d25
DS
2370 break;
2371 case ZEBRA_NEXTHOP_UNREGISTER:
d651649e 2372 zserv_rnh_unregister(client, sock, length, RNH_NEXTHOP_TYPE, zvrf);
078430f6
DS
2373 break;
2374 case ZEBRA_IMPORT_ROUTE_REGISTER:
d651649e 2375 zserv_rnh_register(client, sock, length, RNH_IMPORT_CHECK_TYPE, zvrf);
078430f6
DS
2376 break;
2377 case ZEBRA_IMPORT_ROUTE_UNREGISTER:
d651649e 2378 zserv_rnh_unregister(client, sock, length, RNH_IMPORT_CHECK_TYPE, zvrf);
fb018d25 2379 break;
c43ed2e4
DS
2380 case ZEBRA_BFD_DEST_UPDATE:
2381 case ZEBRA_BFD_DEST_REGISTER:
d651649e 2382 zebra_ptm_bfd_dst_register(client, sock, length, command, zvrf);
c43ed2e4
DS
2383 break;
2384 case ZEBRA_BFD_DEST_DEREGISTER:
d651649e 2385 zebra_ptm_bfd_dst_deregister(client, sock, length, zvrf);
c43ed2e4 2386 break;
7076bb2f 2387 case ZEBRA_VRF_UNREGISTER:
d651649e 2388 zread_vrf_unregister (client, length, zvrf);
7076bb2f 2389 break;
055c4dfc 2390 case ZEBRA_BFD_CLIENT_REGISTER:
2391 zebra_ptm_bfd_client_register(client, sock, length);
2392 break;
4a04e5f7 2393 case ZEBRA_INTERFACE_ENABLE_RADV:
2394 zebra_interface_radv_set (client, sock, length, zvrf, 1);
2395 break;
2396 case ZEBRA_INTERFACE_DISABLE_RADV:
2397 zebra_interface_radv_set (client, sock, length, zvrf, 0);
2398 break;
ce549947
RW
2399 case ZEBRA_MPLS_LABELS_ADD:
2400 case ZEBRA_MPLS_LABELS_DELETE:
2401 zread_mpls_labels (command, client, length, vrf_id);
2402 break;
e3be0432
DS
2403 case ZEBRA_IPMR_ROUTE_STATS:
2404 zebra_ipmr_route_stats (client, sock, length, zvrf);
2405 break;
fea12efb 2406 case ZEBRA_LABEL_MANAGER_CONNECT:
2407 case ZEBRA_GET_LABEL_CHUNK:
2408 case ZEBRA_RELEASE_LABEL_CHUNK:
2409 zread_label_manager_request (command, client, vrf_id);
2410 break;
5aba114a
DS
2411 case ZEBRA_FEC_REGISTER:
2412 zserv_fec_register (client, sock, length);
2413 break;
2414 case ZEBRA_FEC_UNREGISTER:
2415 zserv_fec_unregister (client, sock, length);
2416 break;
718e3744 2417 default:
2418 zlog_info ("Zebra received unknown command %d", command);
2419 break;
2420 }
2421
719e9741 2422 if (client->t_suicide)
2423 {
2424 /* No need to wait for thread callback, just kill immediately. */
2425 zebra_client_close(client);
2426 return -1;
2427 }
2428
d651649e 2429 zclient_read_out:
718e3744 2430 stream_reset (client->ibuf);
2431 zebra_event (ZEBRA_READ, sock, client);
718e3744 2432 return 0;
2433}
2434
718e3744 2435
2436/* Accept code of zebra server socket. */
b9df2d25 2437static int
718e3744 2438zebra_accept (struct thread *thread)
2439{
2440 int accept_sock;
2441 int client_sock;
2442 struct sockaddr_in client;
2443 socklen_t len;
2444
2445 accept_sock = THREAD_FD (thread);
2446
719e9741 2447 /* Reregister myself. */
2448 zebra_event (ZEBRA_SERV, accept_sock, NULL);
2449
718e3744 2450 len = sizeof (struct sockaddr_in);
2451 client_sock = accept (accept_sock, (struct sockaddr *) &client, &len);
2452
2453 if (client_sock < 0)
2454 {
6099b3b5 2455 zlog_warn ("Can't accept zebra socket: %s", safe_strerror (errno));
718e3744 2456 return -1;
2457 }
2458
ccf3557b 2459 /* Make client socket non-blocking. */
719e9741 2460 set_nonblocking(client_sock);
865b852c 2461
718e3744 2462 /* Create new zebra client. */
2463 zebra_client_create (client_sock);
2464
718e3744 2465 return 0;
2466}
2467
b9df2d25 2468#ifdef HAVE_TCP_ZEBRA
718e3744 2469/* Make zebra's server socket. */
b9df2d25 2470static void
718e3744 2471zebra_serv ()
2472{
2473 int ret;
2474 int accept_sock;
2475 struct sockaddr_in addr;
2476
2477 accept_sock = socket (AF_INET, SOCK_STREAM, 0);
2478
2479 if (accept_sock < 0)
2480 {
3d1dc857 2481 zlog_warn ("Can't create zserv stream socket: %s",
2482 safe_strerror (errno));
718e3744 2483 zlog_warn ("zebra can't provice full functionality due to above error");
2484 return;
2485 }
2486
2487 memset (&addr, 0, sizeof (struct sockaddr_in));
2488 addr.sin_family = AF_INET;
2489 addr.sin_port = htons (ZEBRA_PORT);
6f0e3f6e 2490#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
718e3744 2491 addr.sin_len = sizeof (struct sockaddr_in);
6f0e3f6e 2492#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
718e3744 2493 addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
2494
2495 sockopt_reuseaddr (accept_sock);
2496 sockopt_reuseport (accept_sock);
2497
edd7c245 2498 if ( zserv_privs.change(ZPRIVS_RAISE) )
4525281a 2499 zlog_err("Can't raise privileges");
edd7c245 2500
718e3744 2501 ret = bind (accept_sock, (struct sockaddr *)&addr,
2502 sizeof (struct sockaddr_in));
2503 if (ret < 0)
2504 {
3d1dc857 2505 zlog_warn ("Can't bind to stream socket: %s",
2506 safe_strerror (errno));
718e3744 2507 zlog_warn ("zebra can't provice full functionality due to above error");
2508 close (accept_sock); /* Avoid sd leak. */
2509 return;
2510 }
edd7c245 2511
2512 if ( zserv_privs.change(ZPRIVS_LOWER) )
4525281a 2513 zlog_err("Can't lower privileges");
718e3744 2514
2515 ret = listen (accept_sock, 1);
2516 if (ret < 0)
2517 {
3d1dc857 2518 zlog_warn ("Can't listen to stream socket: %s",
2519 safe_strerror (errno));
718e3744 2520 zlog_warn ("zebra can't provice full functionality due to above error");
2521 close (accept_sock); /* Avoid sd leak. */
2522 return;
2523 }
2524
2525 zebra_event (ZEBRA_SERV, accept_sock, NULL);
2526}
fbedba64 2527#else /* HAVE_TCP_ZEBRA */
718e3744 2528
2529/* For sockaddr_un. */
2530#include <sys/un.h>
2531
2532/* zebra server UNIX domain socket. */
b9df2d25 2533static void
fce954f8 2534zebra_serv_un (const char *path)
718e3744 2535{
2536 int ret;
2537 int sock, len;
2538 struct sockaddr_un serv;
2539 mode_t old_mask;
2540
2541 /* First of all, unlink existing socket */
2542 unlink (path);
2543
2544 /* Set umask */
2545 old_mask = umask (0077);
2546
2547 /* Make UNIX domain socket. */
2548 sock = socket (AF_UNIX, SOCK_STREAM, 0);
2549 if (sock < 0)
2550 {
3d1dc857 2551 zlog_warn ("Can't create zserv unix socket: %s",
2552 safe_strerror (errno));
2553 zlog_warn ("zebra can't provide full functionality due to above error");
718e3744 2554 return;
2555 }
2556
2557 /* Make server socket. */
2558 memset (&serv, 0, sizeof (struct sockaddr_un));
2559 serv.sun_family = AF_UNIX;
2560 strncpy (serv.sun_path, path, strlen (path));
6f0e3f6e 2561#ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
718e3744 2562 len = serv.sun_len = SUN_LEN(&serv);
2563#else
2564 len = sizeof (serv.sun_family) + strlen (serv.sun_path);
6f0e3f6e 2565#endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
718e3744 2566
2567 ret = bind (sock, (struct sockaddr *) &serv, len);
2568 if (ret < 0)
2569 {
3d1dc857 2570 zlog_warn ("Can't bind to unix socket %s: %s",
2571 path, safe_strerror (errno));
2572 zlog_warn ("zebra can't provide full functionality due to above error");
718e3744 2573 close (sock);
2574 return;
2575 }
2576
2577 ret = listen (sock, 5);
2578 if (ret < 0)
2579 {
3d1dc857 2580 zlog_warn ("Can't listen to unix socket %s: %s",
2581 path, safe_strerror (errno));
2582 zlog_warn ("zebra can't provide full functionality due to above error");
718e3744 2583 close (sock);
2584 return;
2585 }
2586
2587 umask (old_mask);
2588
2589 zebra_event (ZEBRA_SERV, sock, NULL);
2590}
fbedba64 2591#endif /* HAVE_TCP_ZEBRA */
6b0655a2 2592
718e3744 2593
b9df2d25 2594static void
718e3744 2595zebra_event (enum event event, int sock, struct zserv *client)
2596{
2597 switch (event)
2598 {
2599 case ZEBRA_SERV:
ffa2c898 2600 thread_add_read(zebrad.master, zebra_accept, client, sock, NULL);
718e3744 2601 break;
2602 case ZEBRA_READ:
66e78ae6
QY
2603 client->t_read = NULL;
2604 thread_add_read(zebrad.master, zebra_client_read, client, sock,
2605 &client->t_read);
718e3744 2606 break;
2607 case ZEBRA_WRITE:
2608 /**/
2609 break;
2610 }
2611}
6b0655a2 2612
04b02fda
DS
2613#define ZEBRA_TIME_BUF 32
2614static char *
2615zserv_time_buf(time_t *time1, char *buf, int buflen)
2616{
2617 struct tm *tm;
2618 time_t now;
2619
2620 assert (buf != NULL);
2621 assert (buflen >= ZEBRA_TIME_BUF);
2622 assert (time1 != NULL);
2623
2624 if (!*time1)
2625 {
2626 snprintf(buf, buflen, "never ");
2627 return (buf);
2628 }
2629
cf672a86 2630 now = monotime(NULL);
04b02fda
DS
2631 now -= *time1;
2632 tm = gmtime(&now);
2633
2634 /* Making formatted timer strings. */
2635#define ONE_DAY_SECOND 60*60*24
2636#define ONE_WEEK_SECOND 60*60*24*7
2637
2638 if (now < ONE_DAY_SECOND)
2639 snprintf (buf, buflen, "%02d:%02d:%02d",
2640 tm->tm_hour, tm->tm_min, tm->tm_sec);
2641 else if (now < ONE_WEEK_SECOND)
2642 snprintf (buf, buflen, "%dd%02dh%02dm",
2643 tm->tm_yday, tm->tm_hour, tm->tm_min);
2644 else
2645 snprintf (buf, buflen, "%02dw%dd%02dh",
2646 tm->tm_yday/7, tm->tm_yday - ((tm->tm_yday/7) * 7), tm->tm_hour);
2647 return buf;
2648}
2649
2650static void
2651zebra_show_client_detail (struct vty *vty, struct zserv *client)
2652{
2653 char cbuf[ZEBRA_TIME_BUF], rbuf[ZEBRA_TIME_BUF];
2654 char wbuf[ZEBRA_TIME_BUF], nhbuf[ZEBRA_TIME_BUF], mbuf[ZEBRA_TIME_BUF];
2655
7c8ff89e
DS
2656 vty_out (vty, "Client: %s", zebra_route_string(client->proto));
2657 if (client->instance)
2658 vty_out (vty, " Instance: %d", client->instance);
2659 vty_out (vty, "%s", VTY_NEWLINE);
2660
04b02fda
DS
2661 vty_out (vty, "------------------------ %s", VTY_NEWLINE);
2662 vty_out (vty, "FD: %d %s", client->sock, VTY_NEWLINE);
2663 vty_out (vty, "Route Table ID: %d %s", client->rtm_table, VTY_NEWLINE);
2664
2665 vty_out (vty, "Connect Time: %s %s",
2666 zserv_time_buf(&client->connect_time, cbuf, ZEBRA_TIME_BUF),
2667 VTY_NEWLINE);
2668 if (client->nh_reg_time)
2669 {
2670 vty_out (vty, "Nexthop Registry Time: %s %s",
2671 zserv_time_buf(&client->nh_reg_time, nhbuf, ZEBRA_TIME_BUF),
2672 VTY_NEWLINE);
2673 if (client->nh_last_upd_time)
2674 vty_out (vty, "Nexthop Last Update Time: %s %s",
2675 zserv_time_buf(&client->nh_last_upd_time, mbuf, ZEBRA_TIME_BUF),
2676 VTY_NEWLINE);
2677 else
2678 vty_out (vty, "No Nexthop Update sent%s", VTY_NEWLINE);
2679 }
2680 else
2681 vty_out (vty, "Not registered for Nexthop Updates%s", VTY_NEWLINE);
2682
2683 vty_out (vty, "Last Msg Rx Time: %s %s",
2684 zserv_time_buf(&client->last_read_time, rbuf, ZEBRA_TIME_BUF),
2685 VTY_NEWLINE);
2686 vty_out (vty, "Last Msg Tx Time: %s %s",
2687 zserv_time_buf(&client->last_write_time, wbuf, ZEBRA_TIME_BUF),
2688 VTY_NEWLINE);
2689 if (client->last_read_time)
2690 vty_out (vty, "Last Rcvd Cmd: %s %s",
2691 zserv_command_string(client->last_read_cmd), VTY_NEWLINE);
2692 if (client->last_write_time)
2693 vty_out (vty, "Last Sent Cmd: %s %s",
2694 zserv_command_string(client->last_write_cmd), VTY_NEWLINE);
2695 vty_out (vty, "%s", VTY_NEWLINE);
2696
2697 vty_out (vty, "Type Add Update Del %s", VTY_NEWLINE);
2698 vty_out (vty, "================================================== %s", VTY_NEWLINE);
2699 vty_out (vty, "IPv4 %-12d%-12d%-12d%s", client->v4_route_add_cnt,
2700 client->v4_route_upd8_cnt, client->v4_route_del_cnt, VTY_NEWLINE);
2701 vty_out (vty, "IPv6 %-12d%-12d%-12d%s", client->v6_route_add_cnt,
2702 client->v6_route_upd8_cnt, client->v6_route_del_cnt, VTY_NEWLINE);
2703 vty_out (vty, "Redist:v4 %-12d%-12d%-12d%s", client->redist_v4_add_cnt, 0,
2704 client->redist_v4_del_cnt, VTY_NEWLINE);
2705 vty_out (vty, "Redist:v6 %-12d%-12d%-12d%s", client->redist_v6_add_cnt, 0,
2706 client->redist_v6_del_cnt, VTY_NEWLINE);
2707 vty_out (vty, "Connected %-12d%-12d%-12d%s", client->ifadd_cnt, 0,
2708 client->ifdel_cnt, VTY_NEWLINE);
c43ed2e4
DS
2709 vty_out (vty, "BFD peer %-12d%-12d%-12d%s", client->bfd_peer_add_cnt,
2710 client->bfd_peer_upd8_cnt, client->bfd_peer_del_cnt, VTY_NEWLINE);
04b02fda
DS
2711 vty_out (vty, "Interface Up Notifications: %d%s", client->ifup_cnt,
2712 VTY_NEWLINE);
2713 vty_out (vty, "Interface Down Notifications: %d%s", client->ifdown_cnt,
2714 VTY_NEWLINE);
2715
2716 vty_out (vty, "%s", VTY_NEWLINE);
2717 return;
2718}
2719
2720static void
2721zebra_show_client_brief (struct vty *vty, struct zserv *client)
2722{
2723 char cbuf[ZEBRA_TIME_BUF], rbuf[ZEBRA_TIME_BUF];
2724 char wbuf[ZEBRA_TIME_BUF];
2725
2726 vty_out (vty, "%-8s%12s %12s%12s%8d/%-8d%8d/%-8d%s",
2727 zebra_route_string(client->proto),
2728 zserv_time_buf(&client->connect_time, cbuf, ZEBRA_TIME_BUF),
2729 zserv_time_buf(&client->last_read_time, rbuf, ZEBRA_TIME_BUF),
2730 zserv_time_buf(&client->last_write_time, wbuf, ZEBRA_TIME_BUF),
2731 client->v4_route_add_cnt+client->v4_route_upd8_cnt,
2732 client->v4_route_del_cnt,
2733 client->v6_route_add_cnt+client->v6_route_upd8_cnt,
2734 client->v6_route_del_cnt, VTY_NEWLINE);
2735
2736}
2737
8ed6821e 2738struct zserv *
2739zebra_find_client (u_char proto)
2740{
2741 struct listnode *node, *nnode;
2742 struct zserv *client;
2743
2744 for (ALL_LIST_ELEMENTS (zebrad.client_list, node, nnode, client))
2745 {
2746 if (client->proto == proto)
2747 return client;
2748 }
2749
2750 return NULL;
2751}
2752
9bf75362 2753#ifdef HAVE_NETLINK
718e3744 2754/* Display default rtm_table for all clients. */
2755DEFUN (show_table,
2756 show_table_cmd,
2757 "show table",
2758 SHOW_STR
2759 "default routing table to use for all clients\n")
2760{
b21b19c5 2761 vty_out (vty, "table %d%s", zebrad.rtm_table_default,
718e3744 2762 VTY_NEWLINE);
2763 return CMD_SUCCESS;
2764}
2765
6147e2c6 2766DEFUN (config_table,
718e3744 2767 config_table_cmd,
2768 "table TABLENO",
2769 "Configure target kernel routing table\n"
2770 "TABLE integer\n")
2771{
6af6be86 2772 zebrad.rtm_table_default = strtol (argv[1]->arg, (char**)0, 10);
718e3744 2773 return CMD_SUCCESS;
2774}
2775
813d4307
DW
2776DEFUN (no_config_table,
2777 no_config_table_cmd,
6af6be86 2778 "no table [TABLENO]",
813d4307
DW
2779 NO_STR
2780 "Configure target kernel routing table\n"
2781 "TABLE integer\n")
2782{
2783 zebrad.rtm_table_default = 0;
2784 return CMD_SUCCESS;
2785}
9bf75362 2786#endif
813d4307 2787
647e4f1f 2788DEFUN (ip_forwarding,
2789 ip_forwarding_cmd,
2790 "ip forwarding",
2791 IP_STR
2792 "Turn on IP forwarding")
2793{
2794 int ret;
2795
2796 ret = ipforward ();
b71f00f2 2797 if (ret == 0)
2798 ret = ipforward_on ();
647e4f1f 2799
647e4f1f 2800 if (ret == 0)
2801 {
2802 vty_out (vty, "Can't turn on IP forwarding%s", VTY_NEWLINE);
2803 return CMD_WARNING;
2804 }
2805
2806 return CMD_SUCCESS;
2807}
2808
718e3744 2809DEFUN (no_ip_forwarding,
2810 no_ip_forwarding_cmd,
2811 "no ip forwarding",
2812 NO_STR
2813 IP_STR
2814 "Turn off IP forwarding")
2815{
2816 int ret;
2817
2818 ret = ipforward ();
b71f00f2 2819 if (ret != 0)
2820 ret = ipforward_off ();
718e3744 2821
718e3744 2822 if (ret != 0)
2823 {
2824 vty_out (vty, "Can't turn off IP forwarding%s", VTY_NEWLINE);
2825 return CMD_WARNING;
2826 }
2827
2828 return CMD_SUCCESS;
2829}
2830
57282a31
DS
2831DEFUN (show_zebra,
2832 show_zebra_cmd,
2833 "show zebra",
2834 SHOW_STR
2835 "Zebra information\n")
2836{
2837 struct vrf *vrf;
2838
2839 vty_out (vty, " Route Route Neighbor LSP LSP%s", VTY_NEWLINE);
2840 vty_out (vty, "VRF Installs Removals Updates Installs Removals%s", VTY_NEWLINE);
2841 RB_FOREACH (vrf, vrf_name_head, &vrfs_by_name)
2842 {
2843 struct zebra_vrf *zvrf = vrf->info;
a1579fd3 2844 vty_out (vty,"%-25s %10" PRIu64 " %10" PRIu64 " %10" PRIu64 " %10" PRIu64 " %10" PRIu64 "%s",
57282a31
DS
2845 vrf->name, zvrf->installs, zvrf->removals,
2846 zvrf->neigh_updates, zvrf->lsp_installs, zvrf->lsp_removals,
2847 VTY_NEWLINE);
2848 }
2849
2850 return CMD_SUCCESS;
2851}
2852
718e3744 2853/* This command is for debugging purpose. */
2854DEFUN (show_zebra_client,
2855 show_zebra_client_cmd,
2856 "show zebra client",
2857 SHOW_STR
49d73233 2858 "Zebra information\n"
b9ee4999 2859 "Client information\n")
718e3744 2860{
52dc7ee6 2861 struct listnode *node;
718e3744 2862 struct zserv *client;
2863
1eb8ef25 2864 for (ALL_LIST_ELEMENTS_RO (zebrad.client_list, node, client))
04b02fda
DS
2865 zebra_show_client_detail(vty, client);
2866
2867 return CMD_SUCCESS;
2868}
2869
2870/* This command is for debugging purpose. */
2871DEFUN (show_zebra_client_summary,
2872 show_zebra_client_summary_cmd,
2873 "show zebra client summary",
2874 SHOW_STR
b9ee4999
DS
2875 "Zebra information brief\n"
2876 "Client information brief\n"
2877 "Brief Summary\n")
04b02fda
DS
2878{
2879 struct listnode *node;
2880 struct zserv *client;
2881
2882 vty_out (vty, "Name Connect Time Last Read Last Write IPv4 Routes IPv6 Routes %s",
2883 VTY_NEWLINE);
2884 vty_out (vty,"--------------------------------------------------------------------------------%s",
2885 VTY_NEWLINE);
2886
2887 for (ALL_LIST_ELEMENTS_RO (zebrad.client_list, node, client))
2888 zebra_show_client_brief(vty, client);
fb018d25 2889
04b02fda 2890 vty_out (vty, "Routes column shows (added+updated)/deleted%s", VTY_NEWLINE);
718e3744 2891 return CMD_SUCCESS;
2892}
2893
2894/* Table configuration write function. */
b9df2d25 2895static int
718e3744 2896config_write_table (struct vty *vty)
2897{
b21b19c5 2898 if (zebrad.rtm_table_default)
2899 vty_out (vty, "table %d%s", zebrad.rtm_table_default,
718e3744 2900 VTY_NEWLINE);
2901 return 0;
2902}
2903
2904/* table node for routing tables. */
7fc626de 2905static struct cmd_node table_node =
718e3744 2906{
2907 TABLE_NODE,
2908 "", /* This node has no interface. */
2909 1
2910};
6b0655a2 2911
718e3744 2912/* Only display ip forwarding is enabled or not. */
2913DEFUN (show_ip_forwarding,
2914 show_ip_forwarding_cmd,
2915 "show ip forwarding",
2916 SHOW_STR
2917 IP_STR
2918 "IP forwarding status\n")
2919{
2920 int ret;
2921
2922 ret = ipforward ();
2923
2924 if (ret == 0)
2925 vty_out (vty, "IP forwarding is off%s", VTY_NEWLINE);
2926 else
2927 vty_out (vty, "IP forwarding is on%s", VTY_NEWLINE);
2928 return CMD_SUCCESS;
2929}
2930
718e3744 2931/* Only display ipv6 forwarding is enabled or not. */
2932DEFUN (show_ipv6_forwarding,
2933 show_ipv6_forwarding_cmd,
2934 "show ipv6 forwarding",
2935 SHOW_STR
2936 "IPv6 information\n"
2937 "Forwarding status\n")
2938{
2939 int ret;
2940
2941 ret = ipforward_ipv6 ();
2942
2943 switch (ret)
2944 {
2945 case -1:
2946 vty_out (vty, "ipv6 forwarding is unknown%s", VTY_NEWLINE);
2947 break;
2948 case 0:
2949 vty_out (vty, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE);
2950 break;
2951 case 1:
2952 vty_out (vty, "ipv6 forwarding is %s%s", "on", VTY_NEWLINE);
2953 break;
2954 default:
2955 vty_out (vty, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE);
2956 break;
2957 }
2958 return CMD_SUCCESS;
2959}
2960
55906724 2961DEFUN (ipv6_forwarding,
2962 ipv6_forwarding_cmd,
2963 "ipv6 forwarding",
2964 IPV6_STR
2965 "Turn on IPv6 forwarding")
2966{
2967 int ret;
2968
41d3fc96 2969 ret = ipforward_ipv6 ();
b71f00f2 2970 if (ret == 0)
2971 ret = ipforward_ipv6_on ();
41d3fc96 2972
41d3fc96 2973 if (ret == 0)
55906724 2974 {
2975 vty_out (vty, "Can't turn on IPv6 forwarding%s", VTY_NEWLINE);
2976 return CMD_WARNING;
2977 }
2978
2979 return CMD_SUCCESS;
2980}
2981
718e3744 2982DEFUN (no_ipv6_forwarding,
2983 no_ipv6_forwarding_cmd,
2984 "no ipv6 forwarding",
2985 NO_STR
55906724 2986 IPV6_STR
2987 "Turn off IPv6 forwarding")
718e3744 2988{
2989 int ret;
2990
41d3fc96 2991 ret = ipforward_ipv6 ();
b71f00f2 2992 if (ret != 0)
2993 ret = ipforward_ipv6_off ();
41d3fc96 2994
718e3744 2995 if (ret != 0)
2996 {
2997 vty_out (vty, "Can't turn off IPv6 forwarding%s", VTY_NEWLINE);
2998 return CMD_WARNING;
2999 }
3000
3001 return CMD_SUCCESS;
3002}
3003
718e3744 3004/* IPForwarding configuration write function. */
719e9741 3005static int
718e3744 3006config_write_forwarding (struct vty *vty)
3007{
18a6dce6 3008 /* FIXME: Find better place for that. */
3009 router_id_write (vty);
3010
f40dd648
QY
3011 if (!ipforward ())
3012 vty_out (vty, "no ip forwarding%s", VTY_NEWLINE);
f40dd648
QY
3013 if (!ipforward_ipv6 ())
3014 vty_out (vty, "no ipv6 forwarding%s", VTY_NEWLINE);
718e3744 3015 vty_out (vty, "!%s", VTY_NEWLINE);
3016 return 0;
3017}
3018
3019/* table node for routing tables. */
7fc626de 3020static struct cmd_node forwarding_node =
718e3744 3021{
3022 FORWARDING_NODE,
3023 "", /* This node has no interface. */
3024 1
3025};
3026
718e3744 3027/* Initialisation of zebra and installation of commands. */
3028void
a1ac18c4 3029zebra_init (void)
718e3744 3030{
3031 /* Client list init. */
b21b19c5 3032 zebrad.client_list = list_new ();
718e3744 3033
718e3744 3034 /* Install configuration write function. */
3035 install_node (&table_node, config_write_table);
3036 install_node (&forwarding_node, config_write_forwarding);
3037
3038 install_element (VIEW_NODE, &show_ip_forwarding_cmd);
647e4f1f 3039 install_element (CONFIG_NODE, &ip_forwarding_cmd);
718e3744 3040 install_element (CONFIG_NODE, &no_ip_forwarding_cmd);
57282a31 3041 install_element (ENABLE_NODE, &show_zebra_cmd);
718e3744 3042 install_element (ENABLE_NODE, &show_zebra_client_cmd);
04b02fda 3043 install_element (ENABLE_NODE, &show_zebra_client_summary_cmd);
718e3744 3044
3045#ifdef HAVE_NETLINK
3046 install_element (VIEW_NODE, &show_table_cmd);
718e3744 3047 install_element (CONFIG_NODE, &config_table_cmd);
813d4307 3048 install_element (CONFIG_NODE, &no_config_table_cmd);
718e3744 3049#endif /* HAVE_NETLINK */
3050
718e3744 3051 install_element (VIEW_NODE, &show_ipv6_forwarding_cmd);
55906724 3052 install_element (CONFIG_NODE, &ipv6_forwarding_cmd);
718e3744 3053 install_element (CONFIG_NODE, &no_ipv6_forwarding_cmd);
7514fb77
PJ
3054
3055 /* Route-map */
3056 zebra_route_map_init ();
718e3744 3057}
97be79f9
DO
3058
3059/* Make zebra server socket, wiping any existing one (see bug #403). */
3060void
b5114685 3061zebra_zserv_socket_init (char *path)
97be79f9
DO
3062{
3063#ifdef HAVE_TCP_ZEBRA
3064 zebra_serv ();
3065#else
b5114685 3066 zebra_serv_un (path ? path : ZEBRA_SERV_PATH);
97be79f9
DO
3067#endif /* HAVE_TCP_ZEBRA */
3068}