blob: f0e8d121f9057fd33466ff9237f6a8597dd393d5 [file] [log] [blame]
paul718e3742002-12-13 20:15:29 +00001/* 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 *
16 * You should have received a copy of the GNU General Public License
17 * along with GNU Zebra; see the file COPYING. If not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
20 */
21
22#include <zebra.h>
23
24#include "prefix.h"
25#include "command.h"
26#include "if.h"
27#include "thread.h"
28#include "stream.h"
29#include "memory.h"
30#include "table.h"
31#include "rib.h"
32#include "network.h"
33#include "sockunion.h"
34#include "log.h"
35#include "zclient.h"
pauledd7c242003-06-04 13:59:38 +000036#include "privs.h"
ajs719e9742005-02-28 20:52:15 +000037#include "network.h"
38#include "buffer.h"
Feng Lu0d0686f2015-05-22 11:40:02 +020039#include "vrf.h"
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -050040#include "nexthop.h"
paul718e3742002-12-13 20:15:29 +000041
42#include "zebra/zserv.h"
hasso18a6dce2004-10-03 18:18:34 +000043#include "zebra/router-id.h"
paul718e3742002-12-13 20:15:29 +000044#include "zebra/redistribute.h"
45#include "zebra/debug.h"
46#include "zebra/ipforward.h"
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -050047#include "zebra/zebra_rnh.h"
David Lamparter6b0655a2014-06-04 06:53:35 +020048
paul718e3742002-12-13 20:15:29 +000049/* Event list of zebra. */
50enum event { ZEBRA_SERV, ZEBRA_READ, ZEBRA_WRITE };
51
paulb21b19c2003-06-15 01:28:29 +000052extern struct zebra_t zebrad;
paul718e3742002-12-13 20:15:29 +000053
paulb9df2d22004-05-09 09:09:59 +000054static void zebra_event (enum event event, int sock, struct zserv *client);
paulccf35572003-03-01 11:42:20 +000055
pauledd7c242003-06-04 13:59:38 +000056extern struct zebra_privs_t zserv_privs;
David Lamparter6b0655a2014-06-04 06:53:35 +020057
ajs719e9742005-02-28 20:52:15 +000058static void zebra_client_close (struct zserv *client);
59
60static int
61zserv_delayed_close(struct thread *thread)
paulccf35572003-03-01 11:42:20 +000062{
ajs719e9742005-02-28 20:52:15 +000063 struct zserv *client = THREAD_ARG(thread);
paulccf35572003-03-01 11:42:20 +000064
ajs719e9742005-02-28 20:52:15 +000065 client->t_suicide = NULL;
66 zebra_client_close(client);
paulccf35572003-03-01 11:42:20 +000067 return 0;
68}
69
Vyacheslav Trushkin2ea1ab12011-12-11 18:48:47 +040070/* When client connects, it sends hello message
71 * with promise to send zebra routes of specific type.
72 * Zebra stores a socket fd of the client into
73 * this array. And use it to clean up routes that
74 * client didn't remove for some reasons after closing
75 * connection.
76 */
77static int route_type_oaths[ZEBRA_ROUTE_MAX];
78
ajs719e9742005-02-28 20:52:15 +000079static int
80zserv_flush_data(struct thread *thread)
paulccf35572003-03-01 11:42:20 +000081{
ajs719e9742005-02-28 20:52:15 +000082 struct zserv *client = THREAD_ARG(thread);
paulccf35572003-03-01 11:42:20 +000083
ajs719e9742005-02-28 20:52:15 +000084 client->t_write = NULL;
85 if (client->t_suicide)
86 {
87 zebra_client_close(client);
88 return -1;
89 }
90 switch (buffer_flush_available(client->wb, client->sock))
91 {
92 case BUFFER_ERROR:
93 zlog_warn("%s: buffer_flush_available failed on zserv client fd %d, "
94 "closing", __func__, client->sock);
95 zebra_client_close(client);
96 break;
97 case BUFFER_PENDING:
98 client->t_write = thread_add_write(zebrad.master, zserv_flush_data,
99 client, client->sock);
100 break;
101 case BUFFER_EMPTY:
102 break;
103 }
Dinesh Dutt9ae85522015-05-19 17:47:22 -0700104
105 client->last_write_time = quagga_time(NULL);
ajs719e9742005-02-28 20:52:15 +0000106 return 0;
paulccf35572003-03-01 11:42:20 +0000107}
108
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -0500109int
ajs719e9742005-02-28 20:52:15 +0000110zebra_server_send_message(struct zserv *client)
paulccf35572003-03-01 11:42:20 +0000111{
ajs719e9742005-02-28 20:52:15 +0000112 if (client->t_suicide)
113 return -1;
Dinesh Dutt9ae85522015-05-19 17:47:22 -0700114
115 stream_set_getp(client->obuf, 0);
116 client->last_write_cmd = stream_getw_from(client->obuf, 4);
ajs719e9742005-02-28 20:52:15 +0000117 switch (buffer_write(client->wb, client->sock, STREAM_DATA(client->obuf),
118 stream_get_endp(client->obuf)))
paulccf35572003-03-01 11:42:20 +0000119 {
ajs719e9742005-02-28 20:52:15 +0000120 case BUFFER_ERROR:
121 zlog_warn("%s: buffer_write failed to zserv client fd %d, closing",
122 __func__, client->sock);
123 /* Schedule a delayed close since many of the functions that call this
124 one do not check the return code. They do not allow for the
125 possibility that an I/O error may have caused the client to be
126 deleted. */
127 client->t_suicide = thread_add_event(zebrad.master, zserv_delayed_close,
128 client, 0);
129 return -1;
ajs719e9742005-02-28 20:52:15 +0000130 case BUFFER_EMPTY:
131 THREAD_OFF(client->t_write);
132 break;
133 case BUFFER_PENDING:
134 THREAD_WRITE_ON(zebrad.master, client->t_write,
135 zserv_flush_data, client, client->sock);
136 break;
paulccf35572003-03-01 11:42:20 +0000137 }
Dinesh Dutt9ae85522015-05-19 17:47:22 -0700138
139 client->last_write_time = quagga_time(NULL);
paulccf35572003-03-01 11:42:20 +0000140 return 0;
141}
142
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -0500143void
Feng Luc99f3482014-10-16 09:52:36 +0800144zserv_create_header (struct stream *s, uint16_t cmd, vrf_id_t vrf_id)
paulc1b98002006-01-16 01:54:02 +0000145{
146 /* length placeholder, caller can update */
147 stream_putw (s, ZEBRA_HEADER_SIZE);
148 stream_putc (s, ZEBRA_HEADER_MARKER);
149 stream_putc (s, ZSERV_VERSION);
Feng Luc99f3482014-10-16 09:52:36 +0800150 stream_putw (s, vrf_id);
paulc1b98002006-01-16 01:54:02 +0000151 stream_putw (s, cmd);
152}
153
Josh Bailey51d4ef82012-03-21 17:13:39 -0700154static void
155zserv_encode_interface (struct stream *s, struct interface *ifp)
156{
157 /* Interface information. */
158 stream_put (s, ifp->name, INTERFACE_NAMSIZ);
159 stream_putl (s, ifp->ifindex);
160 stream_putc (s, ifp->status);
161 stream_putq (s, ifp->flags);
162 stream_putl (s, ifp->metric);
163 stream_putl (s, ifp->mtu);
164 stream_putl (s, ifp->mtu6);
165 stream_putl (s, ifp->bandwidth);
Timo Teräs954c7d62016-01-15 17:36:33 +0200166 stream_putl (s, ifp->ll_type);
Josh Bailey51d4ef82012-03-21 17:13:39 -0700167 stream_putl (s, ifp->hw_addr_len);
168 if (ifp->hw_addr_len)
169 stream_put (s, ifp->hw_addr, ifp->hw_addr_len);
Josh Bailey51d4ef82012-03-21 17:13:39 -0700170
Olivier Dugeon15773a82016-04-19 18:29:55 +0200171 zlog_info("Try to set TE Link Param");
172 /* Then, Traffic Engineering parameters if any */
173 if (HAS_LINK_PARAMS(ifp) && IS_LINK_PARAMS_SET(ifp->link_params))
174 {
175 stream_putc (s, 1);
176 zebra_interface_link_params_write (s, ifp);
177 }
178 else
179 stream_putc (s, 0);
180
Josh Bailey51d4ef82012-03-21 17:13:39 -0700181 /* Write packet size. */
182 stream_putw_at (s, 0, stream_get_endp (s));
183}
184
paul718e3742002-12-13 20:15:29 +0000185/* Interface is added. Send ZEBRA_INTERFACE_ADD to client. */
paulb9df2d22004-05-09 09:09:59 +0000186/*
187 * This function is called in the following situations:
188 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
189 * from the client.
190 * - at startup, when zebra figures out the available interfaces
191 * - when an interface is added (where support for
192 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
193 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
194 * received)
195 */
paul718e3742002-12-13 20:15:29 +0000196int
197zsend_interface_add (struct zserv *client, struct interface *ifp)
198{
199 struct stream *s;
200
201 /* Check this client need interface information. */
Feng Luc99f3482014-10-16 09:52:36 +0800202 if (! vrf_bitmap_check (client->ifinfo, ifp->vrf_id))
ajs719e9742005-02-28 20:52:15 +0000203 return 0;
paul718e3742002-12-13 20:15:29 +0000204
205 s = client->obuf;
206 stream_reset (s);
207
Feng Luc99f3482014-10-16 09:52:36 +0800208 zserv_create_header (s, ZEBRA_INTERFACE_ADD, ifp->vrf_id);
Josh Bailey51d4ef82012-03-21 17:13:39 -0700209 zserv_encode_interface (s, ifp);
paul718e3742002-12-13 20:15:29 +0000210
Dinesh Dutt9ae85522015-05-19 17:47:22 -0700211 client->ifadd_cnt++;
ajs719e9742005-02-28 20:52:15 +0000212 return zebra_server_send_message(client);
paul718e3742002-12-13 20:15:29 +0000213}
214
215/* Interface deletion from zebra daemon. */
216int
217zsend_interface_delete (struct zserv *client, struct interface *ifp)
218{
219 struct stream *s;
220
221 /* Check this client need interface information. */
Feng Luc99f3482014-10-16 09:52:36 +0800222 if (! vrf_bitmap_check (client->ifinfo, ifp->vrf_id))
ajs719e9742005-02-28 20:52:15 +0000223 return 0;
paul718e3742002-12-13 20:15:29 +0000224
225 s = client->obuf;
226 stream_reset (s);
paul718e3742002-12-13 20:15:29 +0000227
Feng Luc99f3482014-10-16 09:52:36 +0800228 zserv_create_header (s, ZEBRA_INTERFACE_DELETE, ifp->vrf_id);
Josh Bailey51d4ef82012-03-21 17:13:39 -0700229 zserv_encode_interface (s, ifp);
paul718e3742002-12-13 20:15:29 +0000230
Dinesh Dutt9ae85522015-05-19 17:47:22 -0700231 client->ifdel_cnt++;
ajs719e9742005-02-28 20:52:15 +0000232 return zebra_server_send_message (client);
paul718e3742002-12-13 20:15:29 +0000233}
234
Olivier Dugeon15773a82016-04-19 18:29:55 +0200235int
236zsend_interface_link_params (struct zserv *client, struct interface *ifp)
237{
238 struct stream *s;
239
240 /* Check this client need interface information. */
241 if (! client->ifinfo)
242 return 0;
243
244 if (!ifp->link_params)
245 return 0;
246 s = client->obuf;
247 stream_reset (s);
248
249 zserv_create_header (s, ZEBRA_INTERFACE_LINK_PARAMS, ifp->vrf_id);
250
251 /* Add Interface Index */
252 stream_putl (s, ifp->ifindex);
253
254 /* Then TE Link Parameters */
255 if (zebra_interface_link_params_write (s, ifp) == 0)
256 return 0;
257
258 /* Write packet size. */
259 stream_putw_at (s, 0, stream_get_endp (s));
260
261 return zebra_server_send_message (client);
262}
263
paulb9df2d22004-05-09 09:09:59 +0000264/* Interface address is added/deleted. Send ZEBRA_INTERFACE_ADDRESS_ADD or
265 * ZEBRA_INTERFACE_ADDRESS_DELETE to the client.
266 *
267 * A ZEBRA_INTERFACE_ADDRESS_ADD is sent in the following situations:
268 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
269 * from the client, after the ZEBRA_INTERFACE_ADD has been
270 * sent from zebra to the client
271 * - redistribute new address info to all clients in the following situations
272 * - at startup, when zebra figures out the available interfaces
273 * - when an interface is added (where support for
274 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
275 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
276 * received)
277 * - for the vty commands "ip address A.B.C.D/M [<secondary>|<label LINE>]"
278 * and "no bandwidth <1-10000000>", "ipv6 address X:X::X:X/M"
279 * - when an RTM_NEWADDR message is received from the kernel,
280 *
281 * The call tree that triggers ZEBRA_INTERFACE_ADDRESS_DELETE:
282 *
283 * zsend_interface_address(DELETE)
284 * ^
285 * |
286 * zebra_interface_address_delete_update
287 * ^ ^ ^
paul6eb88272005-07-29 14:36:00 +0000288 * | | if_delete_update
289 * | |
paulb9df2d22004-05-09 09:09:59 +0000290 * ip_address_uninstall connected_delete_ipv4
291 * [ipv6_addresss_uninstall] [connected_delete_ipv6]
292 * ^ ^
293 * | |
294 * | RTM_NEWADDR on routing/netlink socket
295 * |
296 * vty commands:
297 * "no ip address A.B.C.D/M [label LINE]"
298 * "no ip address A.B.C.D/M secondary"
299 * ["no ipv6 address X:X::X:X/M"]
300 *
301 */
paul718e3742002-12-13 20:15:29 +0000302int
paulb9df2d22004-05-09 09:09:59 +0000303zsend_interface_address (int cmd, struct zserv *client,
304 struct interface *ifp, struct connected *ifc)
paul718e3742002-12-13 20:15:29 +0000305{
306 int blen;
307 struct stream *s;
308 struct prefix *p;
309
310 /* Check this client need interface information. */
Feng Luc99f3482014-10-16 09:52:36 +0800311 if (! vrf_bitmap_check (client->ifinfo, ifp->vrf_id))
ajs719e9742005-02-28 20:52:15 +0000312 return 0;
paul718e3742002-12-13 20:15:29 +0000313
314 s = client->obuf;
315 stream_reset (s);
paulc1b98002006-01-16 01:54:02 +0000316
Feng Luc99f3482014-10-16 09:52:36 +0800317 zserv_create_header (s, cmd, ifp->vrf_id);
paul718e3742002-12-13 20:15:29 +0000318 stream_putl (s, ifp->ifindex);
319
320 /* Interface address flag. */
321 stream_putc (s, ifc->flags);
322
323 /* Prefix information. */
324 p = ifc->address;
325 stream_putc (s, p->family);
326 blen = prefix_blen (p);
327 stream_put (s, &p->u.prefix, blen);
paulb9df2d22004-05-09 09:09:59 +0000328
329 /*
330 * XXX gnu version does not send prefixlen for ZEBRA_INTERFACE_ADDRESS_DELETE
331 * but zebra_interface_address_delete_read() in the gnu version
332 * expects to find it
333 */
paul718e3742002-12-13 20:15:29 +0000334 stream_putc (s, p->prefixlen);
335
336 /* Destination. */
337 p = ifc->destination;
338 if (p)
339 stream_put (s, &p->u.prefix, blen);
340 else
341 stream_put (s, NULL, blen);
342
343 /* Write packet size. */
344 stream_putw_at (s, 0, stream_get_endp (s));
345
Dinesh Dutt9ae85522015-05-19 17:47:22 -0700346 client->connected_rt_add_cnt++;
ajs719e9742005-02-28 20:52:15 +0000347 return zebra_server_send_message(client);
paul718e3742002-12-13 20:15:29 +0000348}
349
paulb9df2d22004-05-09 09:09:59 +0000350/*
351 * The cmd passed to zsend_interface_update may be ZEBRA_INTERFACE_UP or
352 * ZEBRA_INTERFACE_DOWN.
353 *
354 * The ZEBRA_INTERFACE_UP message is sent from the zebra server to
355 * the clients in one of 2 situations:
356 * - an if_up is detected e.g., as a result of an RTM_IFINFO message
357 * - a vty command modifying the bandwidth of an interface is received.
358 * The ZEBRA_INTERFACE_DOWN message is sent when an if_down is detected.
359 */
paul718e3742002-12-13 20:15:29 +0000360int
paulb9df2d22004-05-09 09:09:59 +0000361zsend_interface_update (int cmd, struct zserv *client, struct interface *ifp)
paul718e3742002-12-13 20:15:29 +0000362{
363 struct stream *s;
364
365 /* Check this client need interface information. */
Feng Luc99f3482014-10-16 09:52:36 +0800366 if (! vrf_bitmap_check (client->ifinfo, ifp->vrf_id))
ajs719e9742005-02-28 20:52:15 +0000367 return 0;
paul718e3742002-12-13 20:15:29 +0000368
369 s = client->obuf;
370 stream_reset (s);
371
Feng Luc99f3482014-10-16 09:52:36 +0800372 zserv_create_header (s, cmd, ifp->vrf_id);
Josh Bailey51d4ef82012-03-21 17:13:39 -0700373 zserv_encode_interface (s, ifp);
paul718e3742002-12-13 20:15:29 +0000374
Dinesh Dutt9ae85522015-05-19 17:47:22 -0700375 if (cmd == ZEBRA_INTERFACE_UP)
376 client->ifup_cnt++;
377 else
378 client->ifdown_cnt++;
379
ajs719e9742005-02-28 20:52:15 +0000380 return zebra_server_send_message(client);
paul718e3742002-12-13 20:15:29 +0000381}
382
paulb9df2d22004-05-09 09:09:59 +0000383/*
384 * The zebra server sends the clients a ZEBRA_IPV4_ROUTE_ADD or a
385 * ZEBRA_IPV6_ROUTE_ADD via zsend_route_multipath in the following
386 * situations:
387 * - when the client starts up, and requests default information
388 * by sending a ZEBRA_REDISTRIBUTE_DEFAULT_ADD to the zebra server, in the
389 * - case of rip, ripngd, ospfd and ospf6d, when the client sends a
390 * ZEBRA_REDISTRIBUTE_ADD as a result of the "redistribute" vty cmd,
391 * - when the zebra server redistributes routes after it updates its rib
392 *
393 * The zebra server sends clients a ZEBRA_IPV4_ROUTE_DELETE or a
394 * ZEBRA_IPV6_ROUTE_DELETE via zsend_route_multipath when:
395 * - a "ip route" or "ipv6 route" vty command is issued, a prefix is
396 * - deleted from zebra's rib, and this info
397 * has to be redistributed to the clients
398 *
399 * XXX The ZEBRA_IPV*_ROUTE_ADD message is also sent by the client to the
400 * zebra server when the client wants to tell the zebra server to add a
401 * route to the kernel (zapi_ipv4_add etc. ). Since it's essentially the
402 * same message being sent back and forth, this function and
403 * zapi_ipv{4,6}_{add, delete} should be re-written to avoid code
404 * duplication.
405 */
paul718e3742002-12-13 20:15:29 +0000406int
paulb9df2d22004-05-09 09:09:59 +0000407zsend_route_multipath (int cmd, struct zserv *client, struct prefix *p,
408 struct rib *rib)
paul718e3742002-12-13 20:15:29 +0000409{
410 int psize;
411 struct stream *s;
412 struct nexthop *nexthop;
paul1dcb5172005-05-31 08:38:50 +0000413 unsigned long nhnummark = 0, messmark = 0;
paulb9df2d22004-05-09 09:09:59 +0000414 int nhnum = 0;
paul1dcb5172005-05-31 08:38:50 +0000415 u_char zapi_flags = 0;
Feng Luc99f3482014-10-16 09:52:36 +0800416
417 /* Check this client need this route. */
418 if (!vrf_bitmap_check (client->redist[rib->type], rib->vrf_id) &&
419 !(is_default (p) &&
420 vrf_bitmap_check (client->redist_default, rib->vrf_id)))
421 return 0;
422
paul718e3742002-12-13 20:15:29 +0000423 s = client->obuf;
424 stream_reset (s);
paulc1b98002006-01-16 01:54:02 +0000425
Feng Luc99f3482014-10-16 09:52:36 +0800426 zserv_create_header (s, cmd, rib->vrf_id);
paulc1b98002006-01-16 01:54:02 +0000427
428 /* Put type and nexthop. */
paul718e3742002-12-13 20:15:29 +0000429 stream_putc (s, rib->type);
430 stream_putc (s, rib->flags);
paul1dcb5172005-05-31 08:38:50 +0000431
432 /* marker for message flags field */
433 messmark = stream_get_endp (s);
434 stream_putc (s, 0);
paul718e3742002-12-13 20:15:29 +0000435
436 /* Prefix. */
437 psize = PSIZE (p->prefixlen);
438 stream_putc (s, p->prefixlen);
paulb9df2d22004-05-09 09:09:59 +0000439 stream_write (s, (u_char *) & p->u.prefix, psize);
paul718e3742002-12-13 20:15:29 +0000440
paulb9df2d22004-05-09 09:09:59 +0000441 /*
442 * XXX The message format sent by zebra below does not match the format
443 * of the corresponding message expected by the zebra server
444 * itself (e.g., see zread_ipv4_add). The nexthop_num is not set correctly,
445 * (is there a bug on the client side if more than one segment is sent?)
446 * nexthop ZEBRA_NEXTHOP_IPV4 is never set, ZEBRA_NEXTHOP_IFINDEX
447 * is hard-coded.
448 */
paul718e3742002-12-13 20:15:29 +0000449 /* Nexthop */
paul1dcb5172005-05-31 08:38:50 +0000450
paul718e3742002-12-13 20:15:29 +0000451 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
452 {
Timo Teräs325823a2016-01-15 17:36:31 +0200453 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE))
paulb9df2d22004-05-09 09:09:59 +0000454 {
paul1dcb5172005-05-31 08:38:50 +0000455 SET_FLAG (zapi_flags, ZAPI_MESSAGE_NEXTHOP);
456 SET_FLAG (zapi_flags, ZAPI_MESSAGE_IFINDEX);
457
458 if (nhnummark == 0)
459 {
460 nhnummark = stream_get_endp (s);
461 stream_putc (s, 1); /* placeholder */
462 }
463
paulb9df2d22004-05-09 09:09:59 +0000464 nhnum++;
paul718e3742002-12-13 20:15:29 +0000465
paulb9df2d22004-05-09 09:09:59 +0000466 switch(nexthop->type)
467 {
468 case NEXTHOP_TYPE_IPV4:
469 case NEXTHOP_TYPE_IPV4_IFINDEX:
470 stream_put_in_addr (s, &nexthop->gate.ipv4);
471 break;
472#ifdef HAVE_IPV6
473 case NEXTHOP_TYPE_IPV6:
474 case NEXTHOP_TYPE_IPV6_IFINDEX:
475 case NEXTHOP_TYPE_IPV6_IFNAME:
476 stream_write (s, (u_char *) &nexthop->gate.ipv6, 16);
477 break;
478#endif
479 default:
480 if (cmd == ZEBRA_IPV4_ROUTE_ADD
481 || cmd == ZEBRA_IPV4_ROUTE_DELETE)
482 {
483 struct in_addr empty;
paul44983cf2004-09-22 13:15:58 +0000484 memset (&empty, 0, sizeof (struct in_addr));
paulb9df2d22004-05-09 09:09:59 +0000485 stream_write (s, (u_char *) &empty, IPV4_MAX_BYTELEN);
486 }
487 else
488 {
489 struct in6_addr empty;
490 memset (&empty, 0, sizeof (struct in6_addr));
491 stream_write (s, (u_char *) &empty, IPV6_MAX_BYTELEN);
492 }
493 }
paul718e3742002-12-13 20:15:29 +0000494
paulb9df2d22004-05-09 09:09:59 +0000495 /* Interface index. */
496 stream_putc (s, 1);
497 stream_putl (s, nexthop->ifindex);
paul718e3742002-12-13 20:15:29 +0000498
paulb9df2d22004-05-09 09:09:59 +0000499 break;
500 }
paul718e3742002-12-13 20:15:29 +0000501 }
502
503 /* Metric */
Stephen Hemmingercf8a8312010-08-18 15:56:46 -0700504 if (cmd == ZEBRA_IPV4_ROUTE_ADD || cmd == ZEBRA_IPV6_ROUTE_ADD)
paul1dcb5172005-05-31 08:38:50 +0000505 {
vincentfbf5d032005-09-29 11:25:50 +0000506 SET_FLAG (zapi_flags, ZAPI_MESSAGE_DISTANCE);
507 stream_putc (s, rib->distance);
paul1dcb5172005-05-31 08:38:50 +0000508 SET_FLAG (zapi_flags, ZAPI_MESSAGE_METRIC);
509 stream_putl (s, rib->metric);
Timo Teräsb11f3b52015-11-02 16:50:07 +0200510 SET_FLAG (zapi_flags, ZAPI_MESSAGE_MTU);
511 stream_putl (s, rib->mtu);
Piotr Chytłaeefddcc2015-12-01 09:48:02 -0500512 /* tag */
513 if (rib->tag)
514 {
515 SET_FLAG(zapi_flags, ZAPI_MESSAGE_TAG);
Paul Jakma96d10602016-07-01 14:23:45 +0100516 stream_putl (s, rib->tag);
Piotr Chytłaeefddcc2015-12-01 09:48:02 -0500517 }
paul1dcb5172005-05-31 08:38:50 +0000518 }
519
520 /* write real message flags value */
521 stream_putc_at (s, messmark, zapi_flags);
522
paulb9df2d22004-05-09 09:09:59 +0000523 /* Write next-hop number */
524 if (nhnummark)
hassoc1eaa442004-10-19 06:26:01 +0000525 stream_putc_at (s, nhnummark, nhnum);
paulb9df2d22004-05-09 09:09:59 +0000526
paul718e3742002-12-13 20:15:29 +0000527 /* Write packet size. */
528 stream_putw_at (s, 0, stream_get_endp (s));
529
ajs719e9742005-02-28 20:52:15 +0000530 return zebra_server_send_message(client);
paul718e3742002-12-13 20:15:29 +0000531}
532
paul718e3742002-12-13 20:15:29 +0000533#ifdef HAVE_IPV6
ajs719e9742005-02-28 20:52:15 +0000534static int
Feng Luc99f3482014-10-16 09:52:36 +0800535zsend_ipv6_nexthop_lookup (struct zserv *client, struct in6_addr *addr,
536 vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +0000537{
538 struct stream *s;
539 struct rib *rib;
540 unsigned long nump;
541 u_char num;
542 struct nexthop *nexthop;
543
544 /* Lookup nexthop. */
Feng Luc99f3482014-10-16 09:52:36 +0800545 rib = rib_match_ipv6 (addr, vrf_id);
paul718e3742002-12-13 20:15:29 +0000546
547 /* Get output stream. */
548 s = client->obuf;
549 stream_reset (s);
550
551 /* Fill in result. */
Feng Luc99f3482014-10-16 09:52:36 +0800552 zserv_create_header (s, ZEBRA_IPV6_NEXTHOP_LOOKUP, vrf_id);
Hiroshi Yokoi8ccd74c2015-09-08 11:52:20 +0900553 stream_put (s, addr, 16);
paul718e3742002-12-13 20:15:29 +0000554
555 if (rib)
556 {
557 stream_putl (s, rib->metric);
558 num = 0;
paul9985f832005-02-09 15:51:56 +0000559 nump = stream_get_endp(s);
paul718e3742002-12-13 20:15:29 +0000560 stream_putc (s, 0);
Christian Frankefa713d92013-07-05 15:35:37 +0000561 /* Only non-recursive routes are elegible to resolve nexthop we
562 * are looking up. Therefore, we will just iterate over the top
563 * chain of nexthops. */
paul718e3742002-12-13 20:15:29 +0000564 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
Timo Teräs325823a2016-01-15 17:36:31 +0200565 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE))
paul718e3742002-12-13 20:15:29 +0000566 {
567 stream_putc (s, nexthop->type);
568 switch (nexthop->type)
569 {
570 case ZEBRA_NEXTHOP_IPV6:
571 stream_put (s, &nexthop->gate.ipv6, 16);
572 break;
573 case ZEBRA_NEXTHOP_IPV6_IFINDEX:
574 case ZEBRA_NEXTHOP_IPV6_IFNAME:
575 stream_put (s, &nexthop->gate.ipv6, 16);
576 stream_putl (s, nexthop->ifindex);
577 break;
578 case ZEBRA_NEXTHOP_IFINDEX:
579 case ZEBRA_NEXTHOP_IFNAME:
580 stream_putl (s, nexthop->ifindex);
581 break;
hassofa2b17e2004-03-04 17:45:00 +0000582 default:
583 /* do nothing */
584 break;
paul718e3742002-12-13 20:15:29 +0000585 }
586 num++;
587 }
588 stream_putc_at (s, nump, num);
589 }
590 else
591 {
592 stream_putl (s, 0);
593 stream_putc (s, 0);
594 }
595
596 stream_putw_at (s, 0, stream_get_endp (s));
Dinesh Dutt9ae85522015-05-19 17:47:22 -0700597
ajs719e9742005-02-28 20:52:15 +0000598 return zebra_server_send_message(client);
paul718e3742002-12-13 20:15:29 +0000599}
600#endif /* HAVE_IPV6 */
601
paulb9df2d22004-05-09 09:09:59 +0000602static int
Feng Luc99f3482014-10-16 09:52:36 +0800603zsend_ipv4_nexthop_lookup (struct zserv *client, struct in_addr addr,
604 vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +0000605{
606 struct stream *s;
607 struct rib *rib;
608 unsigned long nump;
609 u_char num;
610 struct nexthop *nexthop;
611
David Lamparterf598cf72014-11-22 14:44:20 -0800612 /* Lookup nexthop - eBGP excluded */
Feng Luc99f3482014-10-16 09:52:36 +0800613 rib = rib_match_ipv4_safi (addr, SAFI_UNICAST, 1, NULL, vrf_id);
paul718e3742002-12-13 20:15:29 +0000614
615 /* Get output stream. */
616 s = client->obuf;
617 stream_reset (s);
618
619 /* Fill in result. */
Feng Luc99f3482014-10-16 09:52:36 +0800620 zserv_create_header (s, ZEBRA_IPV4_NEXTHOP_LOOKUP, vrf_id);
paul718e3742002-12-13 20:15:29 +0000621 stream_put_in_addr (s, &addr);
622
623 if (rib)
624 {
Christian Frankebb97e462013-05-25 14:01:35 +0000625 if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
626 zlog_debug("%s: Matching rib entry found.", __func__);
paul718e3742002-12-13 20:15:29 +0000627 stream_putl (s, rib->metric);
628 num = 0;
paul9985f832005-02-09 15:51:56 +0000629 nump = stream_get_endp(s);
paul718e3742002-12-13 20:15:29 +0000630 stream_putc (s, 0);
Christian Frankefa713d92013-07-05 15:35:37 +0000631 /* Only non-recursive routes are elegible to resolve the nexthop we
632 * are looking up. Therefore, we will just iterate over the top
633 * chain of nexthops. */
paul718e3742002-12-13 20:15:29 +0000634 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
Timo Teräs325823a2016-01-15 17:36:31 +0200635 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE))
paul718e3742002-12-13 20:15:29 +0000636 {
637 stream_putc (s, nexthop->type);
638 switch (nexthop->type)
639 {
640 case ZEBRA_NEXTHOP_IPV4:
641 stream_put_in_addr (s, &nexthop->gate.ipv4);
642 break;
Christian Frankebb97e462013-05-25 14:01:35 +0000643 case ZEBRA_NEXTHOP_IPV4_IFINDEX:
644 stream_put_in_addr (s, &nexthop->gate.ipv4);
645 stream_putl (s, nexthop->ifindex);
646 break;
paul718e3742002-12-13 20:15:29 +0000647 case ZEBRA_NEXTHOP_IFINDEX:
648 case ZEBRA_NEXTHOP_IFNAME:
649 stream_putl (s, nexthop->ifindex);
650 break;
hassofa2b17e2004-03-04 17:45:00 +0000651 default:
652 /* do nothing */
653 break;
paul718e3742002-12-13 20:15:29 +0000654 }
655 num++;
656 }
657 stream_putc_at (s, nump, num);
658 }
659 else
660 {
Christian Frankebb97e462013-05-25 14:01:35 +0000661 if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
662 zlog_debug("%s: No matching rib entry found.", __func__);
paul718e3742002-12-13 20:15:29 +0000663 stream_putl (s, 0);
664 stream_putc (s, 0);
665 }
666
667 stream_putw_at (s, 0, stream_get_endp (s));
668
ajs719e9742005-02-28 20:52:15 +0000669 return zebra_server_send_message(client);
paul718e3742002-12-13 20:15:29 +0000670}
671
Everton Marques4e5275b2014-07-01 15:15:52 -0300672/*
673 Modified version of zsend_ipv4_nexthop_lookup():
674 Query unicast rib if nexthop is not found on mrib.
675 Returns both route metric and protocol distance.
676*/
677static int
David Lamparterbd078122015-01-06 19:53:24 +0100678zsend_ipv4_nexthop_lookup_mrib (struct zserv *client, struct in_addr addr,
679 struct rib *rib)
Everton Marques4e5275b2014-07-01 15:15:52 -0300680{
681 struct stream *s;
Everton Marques4e5275b2014-07-01 15:15:52 -0300682 unsigned long nump;
683 u_char num;
684 struct nexthop *nexthop;
Everton Marques4e5275b2014-07-01 15:15:52 -0300685
686 /* Get output stream. */
687 s = client->obuf;
688 stream_reset (s);
689
690 /* Fill in result. */
Jafar Al-Gharaibeh190591f2016-04-21 17:40:12 -0500691 zserv_create_header (s, ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB,
692 rib ? rib->vrf_id : VRF_DEFAULT);
Everton Marques4e5275b2014-07-01 15:15:52 -0300693 stream_put_in_addr (s, &addr);
694
695 if (rib)
696 {
697 stream_putc (s, rib->distance);
698 stream_putl (s, rib->metric);
699 num = 0;
700 nump = stream_get_endp(s); /* remember position for nexthop_num */
701 stream_putc (s, 0); /* reserve room for nexthop_num */
702 /* Only non-recursive routes are elegible to resolve the nexthop we
703 * are looking up. Therefore, we will just iterate over the top
704 * chain of nexthops. */
705 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
Timo Teräs325823a2016-01-15 17:36:31 +0200706 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE))
Everton Marques4e5275b2014-07-01 15:15:52 -0300707 {
708 stream_putc (s, nexthop->type);
709 switch (nexthop->type)
710 {
711 case ZEBRA_NEXTHOP_IPV4:
712 stream_put_in_addr (s, &nexthop->gate.ipv4);
713 break;
714 case ZEBRA_NEXTHOP_IPV4_IFINDEX:
715 stream_put_in_addr (s, &nexthop->gate.ipv4);
716 stream_putl (s, nexthop->ifindex);
717 break;
718 case ZEBRA_NEXTHOP_IFINDEX:
719 case ZEBRA_NEXTHOP_IFNAME:
720 stream_putl (s, nexthop->ifindex);
721 break;
722 default:
723 /* do nothing */
724 break;
725 }
726 num++;
727 }
728
729 stream_putc_at (s, nump, num); /* store nexthop_num */
730 }
731 else
732 {
733 stream_putc (s, 0); /* distance */
734 stream_putl (s, 0); /* metric */
735 stream_putc (s, 0); /* nexthop_num */
736 }
737
738 stream_putw_at (s, 0, stream_get_endp (s));
739
740 return zebra_server_send_message(client);
741}
742
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -0500743/* Nexthop register */
744static int
745zserv_nexthop_register (struct zserv *client, int sock, u_short length, vrf_id_t vrf_id)
746{
747 struct rnh *rnh;
748 struct stream *s;
749 struct prefix p;
750 u_short l = 0;
Dinesh Duttd9ab53a2015-05-19 17:47:21 -0700751 u_char connected;
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -0500752
753 if (IS_ZEBRA_DEBUG_NHT)
754 zlog_debug("nexthop_register msg from client %s: length=%d\n",
755 zebra_route_string(client->proto), length);
756
757 s = client->ibuf;
758
759 while (l < length)
760 {
Dinesh Duttd9ab53a2015-05-19 17:47:21 -0700761 connected = stream_getc(s);
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -0500762 p.family = stream_getw(s);
763 p.prefixlen = stream_getc(s);
Dinesh Duttd9ab53a2015-05-19 17:47:21 -0700764 l += 4;
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -0500765 stream_get(&p.u.prefix, s, PSIZE(p.prefixlen));
766 l += PSIZE(p.prefixlen);
767 rnh = zebra_add_rnh(&p, 0);
Dinesh Dutt9ae85522015-05-19 17:47:22 -0700768
769 client->nh_reg_time = quagga_time(NULL);
Dinesh Duttd9ab53a2015-05-19 17:47:21 -0700770
771 if (connected)
772 SET_FLAG(rnh->flags, ZEBRA_NHT_CONNECTED);
773
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -0500774 zebra_add_rnh_client(rnh, client, vrf_id);
775 }
776 zebra_evaluate_rnh_table(0, AF_INET);
777 zebra_evaluate_rnh_table(0, AF_INET6);
778 return 0;
779}
780
781/* Nexthop register */
782static int
783zserv_nexthop_unregister (struct zserv *client, int sock, u_short length)
784{
785 struct rnh *rnh;
786 struct stream *s;
787 struct prefix p;
788 u_short l = 0;
789
790 if (IS_ZEBRA_DEBUG_NHT)
791 zlog_debug("nexthop_unregister msg from client %s: length=%d\n",
792 zebra_route_string(client->proto), length);
793
794 s = client->ibuf;
795
796 while (l < length)
797 {
Dinesh Duttd9ab53a2015-05-19 17:47:21 -0700798 (void)stream_getc(s);
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -0500799 p.family = stream_getw(s);
800 p.prefixlen = stream_getc(s);
Dinesh Duttd9ab53a2015-05-19 17:47:21 -0700801 l += 4;
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -0500802 stream_get(&p.u.prefix, s, PSIZE(p.prefixlen));
803 l += PSIZE(p.prefixlen);
804 rnh = zebra_lookup_rnh(&p, 0);
805 if (rnh)
Dinesh Dutt9ae85522015-05-19 17:47:22 -0700806 {
807 client->nh_dereg_time = quagga_time(NULL);
808 zebra_remove_rnh_client(rnh, client);
809 }
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -0500810 }
811 return 0;
812}
813
paulb9df2d22004-05-09 09:09:59 +0000814static int
Feng Luc99f3482014-10-16 09:52:36 +0800815zsend_ipv4_import_lookup (struct zserv *client, struct prefix_ipv4 *p,
816 vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +0000817{
818 struct stream *s;
819 struct rib *rib;
820 unsigned long nump;
821 u_char num;
822 struct nexthop *nexthop;
823
824 /* Lookup nexthop. */
Feng Luc99f3482014-10-16 09:52:36 +0800825 rib = rib_lookup_ipv4 (p, vrf_id);
paul718e3742002-12-13 20:15:29 +0000826
827 /* Get output stream. */
828 s = client->obuf;
829 stream_reset (s);
830
831 /* Fill in result. */
Feng Luc99f3482014-10-16 09:52:36 +0800832 zserv_create_header (s, ZEBRA_IPV4_IMPORT_LOOKUP, vrf_id);
paul718e3742002-12-13 20:15:29 +0000833 stream_put_in_addr (s, &p->prefix);
834
835 if (rib)
836 {
837 stream_putl (s, rib->metric);
838 num = 0;
paul9985f832005-02-09 15:51:56 +0000839 nump = stream_get_endp(s);
paul718e3742002-12-13 20:15:29 +0000840 stream_putc (s, 0);
841 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
Timo Teräs325823a2016-01-15 17:36:31 +0200842 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE))
paul718e3742002-12-13 20:15:29 +0000843 {
844 stream_putc (s, nexthop->type);
845 switch (nexthop->type)
846 {
847 case ZEBRA_NEXTHOP_IPV4:
848 stream_put_in_addr (s, &nexthop->gate.ipv4);
849 break;
Christian Frankea12afd52013-05-25 14:01:36 +0000850 case ZEBRA_NEXTHOP_IPV4_IFINDEX:
851 stream_put_in_addr (s, &nexthop->gate.ipv4);
852 stream_putl (s, nexthop->ifindex);
853 break;
paul718e3742002-12-13 20:15:29 +0000854 case ZEBRA_NEXTHOP_IFINDEX:
855 case ZEBRA_NEXTHOP_IFNAME:
856 stream_putl (s, nexthop->ifindex);
857 break;
hassofa2b17e2004-03-04 17:45:00 +0000858 default:
859 /* do nothing */
860 break;
paul718e3742002-12-13 20:15:29 +0000861 }
862 num++;
863 }
864 stream_putc_at (s, nump, num);
865 }
866 else
867 {
868 stream_putl (s, 0);
869 stream_putc (s, 0);
870 }
871
872 stream_putw_at (s, 0, stream_get_endp (s));
Dinesh Dutt9ae85522015-05-19 17:47:22 -0700873
ajs719e9742005-02-28 20:52:15 +0000874 return zebra_server_send_message(client);
paul718e3742002-12-13 20:15:29 +0000875}
David Lamparter6b0655a2014-06-04 06:53:35 +0200876
hasso18a6dce2004-10-03 18:18:34 +0000877/* Router-id is updated. Send ZEBRA_ROUTER_ID_ADD to client. */
878int
Feng Luac19a442015-05-22 11:40:07 +0200879zsend_router_id_update (struct zserv *client, struct prefix *p,
880 vrf_id_t vrf_id)
hasso18a6dce2004-10-03 18:18:34 +0000881{
882 struct stream *s;
883 int blen;
884
885 /* Check this client need interface information. */
Feng Luc99f3482014-10-16 09:52:36 +0800886 if (! vrf_bitmap_check (client->ridinfo, vrf_id))
ajs719e9742005-02-28 20:52:15 +0000887 return 0;
hasso18a6dce2004-10-03 18:18:34 +0000888
889 s = client->obuf;
890 stream_reset (s);
891
hasso18a6dce2004-10-03 18:18:34 +0000892 /* Message type. */
Feng Luc99f3482014-10-16 09:52:36 +0800893 zserv_create_header (s, ZEBRA_ROUTER_ID_UPDATE, vrf_id);
hasso18a6dce2004-10-03 18:18:34 +0000894
895 /* Prefix information. */
896 stream_putc (s, p->family);
897 blen = prefix_blen (p);
898 stream_put (s, &p->u.prefix, blen);
899 stream_putc (s, p->prefixlen);
900
901 /* Write packet size. */
902 stream_putw_at (s, 0, stream_get_endp (s));
903
ajs719e9742005-02-28 20:52:15 +0000904 return zebra_server_send_message(client);
hasso18a6dce2004-10-03 18:18:34 +0000905}
David Lamparter6b0655a2014-06-04 06:53:35 +0200906
paul718e3742002-12-13 20:15:29 +0000907/* Register zebra server interface information. Send current all
908 interface and address information. */
ajs719e9742005-02-28 20:52:15 +0000909static int
Feng Luc99f3482014-10-16 09:52:36 +0800910zread_interface_add (struct zserv *client, u_short length, vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +0000911{
paul1eb8ef22005-04-07 07:30:20 +0000912 struct listnode *ifnode, *ifnnode;
913 struct listnode *cnode, *cnnode;
paul718e3742002-12-13 20:15:29 +0000914 struct interface *ifp;
915 struct connected *c;
916
917 /* Interface information is needed. */
Feng Luc99f3482014-10-16 09:52:36 +0800918 vrf_bitmap_set (client->ifinfo, vrf_id);
paul718e3742002-12-13 20:15:29 +0000919
Feng Luc99f3482014-10-16 09:52:36 +0800920 for (ALL_LIST_ELEMENTS (vrf_iflist (vrf_id), ifnode, ifnnode, ifp))
paul718e3742002-12-13 20:15:29 +0000921 {
paul718e3742002-12-13 20:15:29 +0000922 /* Skip pseudo interface. */
923 if (! CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE))
924 continue;
925
ajs719e9742005-02-28 20:52:15 +0000926 if (zsend_interface_add (client, ifp) < 0)
927 return -1;
paul718e3742002-12-13 20:15:29 +0000928
paul1eb8ef22005-04-07 07:30:20 +0000929 for (ALL_LIST_ELEMENTS (ifp->connected, cnode, cnnode, c))
paul718e3742002-12-13 20:15:29 +0000930 {
ajs719e9742005-02-28 20:52:15 +0000931 if (CHECK_FLAG (c->conf, ZEBRA_IFC_REAL) &&
932 (zsend_interface_address (ZEBRA_INTERFACE_ADDRESS_ADD, client,
933 ifp, c) < 0))
934 return -1;
paul718e3742002-12-13 20:15:29 +0000935 }
936 }
ajs719e9742005-02-28 20:52:15 +0000937 return 0;
paul718e3742002-12-13 20:15:29 +0000938}
939
940/* Unregister zebra server interface information. */
ajs719e9742005-02-28 20:52:15 +0000941static int
Feng Luc99f3482014-10-16 09:52:36 +0800942zread_interface_delete (struct zserv *client, u_short length, vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +0000943{
Feng Luc99f3482014-10-16 09:52:36 +0800944 vrf_bitmap_unset (client->ifinfo, vrf_id);
ajs719e9742005-02-28 20:52:15 +0000945 return 0;
paul718e3742002-12-13 20:15:29 +0000946}
947
948/* This function support multiple nexthop. */
paulb9df2d22004-05-09 09:09:59 +0000949/*
950 * Parse the ZEBRA_IPV4_ROUTE_ADD sent from client. Update rib and
951 * add kernel route.
952 */
ajs719e9742005-02-28 20:52:15 +0000953static int
Feng Luc99f3482014-10-16 09:52:36 +0800954zread_ipv4_add (struct zserv *client, u_short length, vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +0000955{
956 int i;
957 struct rib *rib;
958 struct prefix_ipv4 p;
959 u_char message;
960 struct in_addr nexthop;
961 u_char nexthop_num;
962 u_char nexthop_type;
963 struct stream *s;
Paul Jakma9099f9b2016-01-18 10:12:10 +0000964 ifindex_t ifindex;
paul718e3742002-12-13 20:15:29 +0000965 u_char ifname_len;
G.Balajicddf3912011-11-26 21:59:32 +0400966 safi_t safi;
Dinesh Dutt9ae85522015-05-19 17:47:22 -0700967 int ret;
paul718e3742002-12-13 20:15:29 +0000968
969 /* Get input stream. */
970 s = client->ibuf;
971
972 /* Allocate new rib. */
paul4d38fdb2005-04-28 17:35:14 +0000973 rib = XCALLOC (MTYPE_RIB, sizeof (struct rib));
974
paul718e3742002-12-13 20:15:29 +0000975 /* Type, flags, message. */
976 rib->type = stream_getc (s);
977 rib->flags = stream_getc (s);
paulb9df2d22004-05-09 09:09:59 +0000978 message = stream_getc (s);
G.Balajicddf3912011-11-26 21:59:32 +0400979 safi = stream_getw (s);
paul718e3742002-12-13 20:15:29 +0000980 rib->uptime = time (NULL);
981
982 /* IPv4 prefix. */
983 memset (&p, 0, sizeof (struct prefix_ipv4));
984 p.family = AF_INET;
985 p.prefixlen = stream_getc (s);
986 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
987
Feng Lu0d0686f2015-05-22 11:40:02 +0200988 /* VRF ID */
Feng Luc99f3482014-10-16 09:52:36 +0800989 rib->vrf_id = vrf_id;
Feng Lu0d0686f2015-05-22 11:40:02 +0200990
paul718e3742002-12-13 20:15:29 +0000991 /* Nexthop parse. */
992 if (CHECK_FLAG (message, ZAPI_MESSAGE_NEXTHOP))
993 {
994 nexthop_num = stream_getc (s);
995
996 for (i = 0; i < nexthop_num; i++)
997 {
998 nexthop_type = stream_getc (s);
999
1000 switch (nexthop_type)
1001 {
1002 case ZEBRA_NEXTHOP_IFINDEX:
1003 ifindex = stream_getl (s);
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -05001004 rib_nexthop_ifindex_add (rib, ifindex);
paul718e3742002-12-13 20:15:29 +00001005 break;
1006 case ZEBRA_NEXTHOP_IFNAME:
1007 ifname_len = stream_getc (s);
paul9985f832005-02-09 15:51:56 +00001008 stream_forward_getp (s, ifname_len);
paul718e3742002-12-13 20:15:29 +00001009 break;
1010 case ZEBRA_NEXTHOP_IPV4:
1011 nexthop.s_addr = stream_get_ipv4 (s);
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -05001012 rib_nexthop_ipv4_add (rib, &nexthop, NULL);
paul718e3742002-12-13 20:15:29 +00001013 break;
Joakim Tjernlundc963c202012-07-07 17:06:13 +02001014 case ZEBRA_NEXTHOP_IPV4_IFINDEX:
1015 nexthop.s_addr = stream_get_ipv4 (s);
1016 ifindex = stream_getl (s);
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -05001017 rib_nexthop_ipv4_ifindex_add (rib, &nexthop, NULL, ifindex);
Joakim Tjernlundc963c202012-07-07 17:06:13 +02001018 break;
paul718e3742002-12-13 20:15:29 +00001019 case ZEBRA_NEXTHOP_IPV6:
paul9985f832005-02-09 15:51:56 +00001020 stream_forward_getp (s, IPV6_MAX_BYTELEN);
paul718e3742002-12-13 20:15:29 +00001021 break;
Subbaiah Venkata6902c692012-03-27 19:21:29 -07001022 case ZEBRA_NEXTHOP_BLACKHOLE:
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -05001023 rib_nexthop_blackhole_add (rib);
Subbaiah Venkata6902c692012-03-27 19:21:29 -07001024 break;
1025 }
paul718e3742002-12-13 20:15:29 +00001026 }
1027 }
1028
1029 /* Distance. */
1030 if (CHECK_FLAG (message, ZAPI_MESSAGE_DISTANCE))
1031 rib->distance = stream_getc (s);
1032
1033 /* Metric. */
1034 if (CHECK_FLAG (message, ZAPI_MESSAGE_METRIC))
1035 rib->metric = stream_getl (s);
1036
Timo Teräsb11f3b52015-11-02 16:50:07 +02001037 if (CHECK_FLAG (message, ZAPI_MESSAGE_MTU))
1038 rib->mtu = stream_getl (s);
Piotr Chytłaeefddcc2015-12-01 09:48:02 -05001039 /* Tag */
1040 if (CHECK_FLAG (message, ZAPI_MESSAGE_TAG))
Paul Jakma96d10602016-07-01 14:23:45 +01001041 rib->tag = stream_getl (s);
Timo Teräsb11f3b52015-11-02 16:50:07 +02001042
Paul Jakma171eee32006-07-27 16:11:02 +00001043 /* Table */
1044 rib->table=zebrad.rtm_table_default;
Dinesh Dutt9ae85522015-05-19 17:47:22 -07001045 ret = rib_add_ipv4_multipath (&p, rib, safi);
1046
1047 /* Stats */
1048 if (ret > 0)
1049 client->v4_route_add_cnt++;
1050 else if (ret < 0)
1051 client->v4_route_upd8_cnt++;
ajs719e9742005-02-28 20:52:15 +00001052 return 0;
paul718e3742002-12-13 20:15:29 +00001053}
1054
1055/* Zebra server IPv4 prefix delete function. */
ajs719e9742005-02-28 20:52:15 +00001056static int
Feng Luc99f3482014-10-16 09:52:36 +08001057zread_ipv4_delete (struct zserv *client, u_short length, vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +00001058{
1059 int i;
1060 struct stream *s;
1061 struct zapi_ipv4 api;
Subbaiah Venkata6902c692012-03-27 19:21:29 -07001062 struct in_addr nexthop, *nexthop_p;
paul718e3742002-12-13 20:15:29 +00001063 unsigned long ifindex;
1064 struct prefix_ipv4 p;
1065 u_char nexthop_num;
1066 u_char nexthop_type;
1067 u_char ifname_len;
1068
1069 s = client->ibuf;
1070 ifindex = 0;
1071 nexthop.s_addr = 0;
Subbaiah Venkata6902c692012-03-27 19:21:29 -07001072 nexthop_p = NULL;
paul718e3742002-12-13 20:15:29 +00001073
1074 /* Type, flags, message. */
1075 api.type = stream_getc (s);
1076 api.flags = stream_getc (s);
1077 api.message = stream_getc (s);
G.Balajicddf3912011-11-26 21:59:32 +04001078 api.safi = stream_getw (s);
paul718e3742002-12-13 20:15:29 +00001079
1080 /* IPv4 prefix. */
1081 memset (&p, 0, sizeof (struct prefix_ipv4));
1082 p.family = AF_INET;
1083 p.prefixlen = stream_getc (s);
1084 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
1085
1086 /* Nexthop, ifindex, distance, metric. */
1087 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
1088 {
1089 nexthop_num = stream_getc (s);
1090
1091 for (i = 0; i < nexthop_num; i++)
1092 {
1093 nexthop_type = stream_getc (s);
1094
1095 switch (nexthop_type)
1096 {
1097 case ZEBRA_NEXTHOP_IFINDEX:
1098 ifindex = stream_getl (s);
1099 break;
1100 case ZEBRA_NEXTHOP_IFNAME:
1101 ifname_len = stream_getc (s);
paul9985f832005-02-09 15:51:56 +00001102 stream_forward_getp (s, ifname_len);
paul718e3742002-12-13 20:15:29 +00001103 break;
1104 case ZEBRA_NEXTHOP_IPV4:
1105 nexthop.s_addr = stream_get_ipv4 (s);
Subbaiah Venkata6902c692012-03-27 19:21:29 -07001106 nexthop_p = &nexthop;
paul718e3742002-12-13 20:15:29 +00001107 break;
Joakim Tjernlundc963c202012-07-07 17:06:13 +02001108 case ZEBRA_NEXTHOP_IPV4_IFINDEX:
1109 nexthop.s_addr = stream_get_ipv4 (s);
Christian Franke23f5f7c2013-11-27 17:06:14 +00001110 nexthop_p = &nexthop;
Joakim Tjernlundc963c202012-07-07 17:06:13 +02001111 ifindex = stream_getl (s);
1112 break;
paul718e3742002-12-13 20:15:29 +00001113 case ZEBRA_NEXTHOP_IPV6:
paul9985f832005-02-09 15:51:56 +00001114 stream_forward_getp (s, IPV6_MAX_BYTELEN);
paul718e3742002-12-13 20:15:29 +00001115 break;
1116 }
1117 }
1118 }
1119
1120 /* Distance. */
1121 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
1122 api.distance = stream_getc (s);
1123 else
1124 api.distance = 0;
1125
1126 /* Metric. */
1127 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
1128 api.metric = stream_getl (s);
1129 else
1130 api.metric = 0;
1131
Piotr Chytłaeefddcc2015-12-01 09:48:02 -05001132 /* tag */
1133 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_TAG))
Paul Jakma96d10602016-07-01 14:23:45 +01001134 api.tag = stream_getl (s);
Piotr Chytłaeefddcc2015-12-01 09:48:02 -05001135 else
1136 api.tag = 0;
1137
Subbaiah Venkata6902c692012-03-27 19:21:29 -07001138 rib_delete_ipv4 (api.type, api.flags, &p, nexthop_p, ifindex,
Feng Luc99f3482014-10-16 09:52:36 +08001139 vrf_id, api.safi);
Dinesh Dutt9ae85522015-05-19 17:47:22 -07001140 client->v4_route_del_cnt++;
ajs719e9742005-02-28 20:52:15 +00001141 return 0;
paul718e3742002-12-13 20:15:29 +00001142}
1143
1144/* Nexthop lookup for IPv4. */
ajs719e9742005-02-28 20:52:15 +00001145static int
Feng Luc99f3482014-10-16 09:52:36 +08001146zread_ipv4_nexthop_lookup (struct zserv *client, u_short length,
1147 vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +00001148{
1149 struct in_addr addr;
Christian Frankebb97e462013-05-25 14:01:35 +00001150 char buf[BUFSIZ];
paul718e3742002-12-13 20:15:29 +00001151
1152 addr.s_addr = stream_get_ipv4 (client->ibuf);
Christian Frankebb97e462013-05-25 14:01:35 +00001153 if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
1154 zlog_debug("%s: looking up %s", __func__,
1155 inet_ntop (AF_INET, &addr, buf, BUFSIZ));
Feng Luc99f3482014-10-16 09:52:36 +08001156 return zsend_ipv4_nexthop_lookup (client, addr, vrf_id);
paul718e3742002-12-13 20:15:29 +00001157}
1158
Everton Marques4e5275b2014-07-01 15:15:52 -03001159/* MRIB Nexthop lookup for IPv4. */
1160static int
Feng Luc99f3482014-10-16 09:52:36 +08001161zread_ipv4_nexthop_lookup_mrib (struct zserv *client, u_short length,
1162 vrf_id_t vrf_id)
Everton Marques4e5275b2014-07-01 15:15:52 -03001163{
1164 struct in_addr addr;
David Lamparterbd078122015-01-06 19:53:24 +01001165 struct rib *rib;
Everton Marques4e5275b2014-07-01 15:15:52 -03001166
1167 addr.s_addr = stream_get_ipv4 (client->ibuf);
Feng Luc99f3482014-10-16 09:52:36 +08001168 rib = rib_match_ipv4_multicast (addr, NULL, vrf_id);
David Lamparterbd078122015-01-06 19:53:24 +01001169 return zsend_ipv4_nexthop_lookup_mrib (client, addr, rib);
Everton Marques4e5275b2014-07-01 15:15:52 -03001170}
1171
paul718e3742002-12-13 20:15:29 +00001172/* Nexthop lookup for IPv4. */
ajs719e9742005-02-28 20:52:15 +00001173static int
Feng Luc99f3482014-10-16 09:52:36 +08001174zread_ipv4_import_lookup (struct zserv *client, u_short length,
1175 vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +00001176{
1177 struct prefix_ipv4 p;
1178
1179 p.family = AF_INET;
1180 p.prefixlen = stream_getc (client->ibuf);
1181 p.prefix.s_addr = stream_get_ipv4 (client->ibuf);
1182
Feng Luc99f3482014-10-16 09:52:36 +08001183 return zsend_ipv4_import_lookup (client, &p, vrf_id);
paul718e3742002-12-13 20:15:29 +00001184}
1185
1186#ifdef HAVE_IPV6
1187/* Zebra server IPv6 prefix add function. */
ajs719e9742005-02-28 20:52:15 +00001188static int
Feng Luc99f3482014-10-16 09:52:36 +08001189zread_ipv6_add (struct zserv *client, u_short length, vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +00001190{
1191 int i;
1192 struct stream *s;
paul718e3742002-12-13 20:15:29 +00001193 struct in6_addr nexthop;
Ayan Banerjee34c5d892015-11-09 20:14:53 -05001194 struct rib *rib;
1195 u_char message;
1196 u_char gateway_num;
1197 u_char nexthop_type;
paul718e3742002-12-13 20:15:29 +00001198 struct prefix_ipv6 p;
Ayan Banerjee34c5d892015-11-09 20:14:53 -05001199 safi_t safi;
1200 static struct in6_addr nexthops[MULTIPATH_NUM];
1201 static unsigned int ifindices[MULTIPATH_NUM];
Dinesh Dutt9ae85522015-05-19 17:47:22 -07001202 int ret;
Ayan Banerjee34c5d892015-11-09 20:14:53 -05001203
1204 /* Get input stream. */
paul718e3742002-12-13 20:15:29 +00001205 s = client->ibuf;
Ayan Banerjee34c5d892015-11-09 20:14:53 -05001206
paul718e3742002-12-13 20:15:29 +00001207 memset (&nexthop, 0, sizeof (struct in6_addr));
1208
Ayan Banerjee34c5d892015-11-09 20:14:53 -05001209 /* Allocate new rib. */
1210 rib = XCALLOC (MTYPE_RIB, sizeof (struct rib));
paul718e3742002-12-13 20:15:29 +00001211
Ayan Banerjee34c5d892015-11-09 20:14:53 -05001212 /* Type, flags, message. */
1213 rib->type = stream_getc (s);
1214 rib->flags = stream_getc (s);
1215 message = stream_getc (s);
1216 safi = stream_getw (s);
1217 rib->uptime = time (NULL);
1218
1219 /* IPv6 prefix. */
paul718e3742002-12-13 20:15:29 +00001220 memset (&p, 0, sizeof (struct prefix_ipv6));
1221 p.family = AF_INET6;
1222 p.prefixlen = stream_getc (s);
1223 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
1224
Ayan Banerjee34c5d892015-11-09 20:14:53 -05001225 /* We need to give nh-addr, nh-ifindex with the same next-hop object
1226 * to the rib to ensure that IPv6 multipathing works; need to coalesce
1227 * these. Clients should send the same number of paired set of
1228 * next-hop-addr/next-hop-ifindices. */
1229 if (CHECK_FLAG (message, ZAPI_MESSAGE_NEXTHOP))
paul718e3742002-12-13 20:15:29 +00001230 {
Ayan Banerjee34c5d892015-11-09 20:14:53 -05001231 int nh_count = 0;
1232 int if_count = 0;
1233 int max_nh_if = 0;
1234 unsigned int ifindex;
paul718e3742002-12-13 20:15:29 +00001235
Ayan Banerjee34c5d892015-11-09 20:14:53 -05001236 gateway_num = stream_getc (s);
1237 for (i = 0; i < gateway_num; i++)
paul718e3742002-12-13 20:15:29 +00001238 {
1239 nexthop_type = stream_getc (s);
1240
1241 switch (nexthop_type)
1242 {
1243 case ZEBRA_NEXTHOP_IPV6:
1244 stream_get (&nexthop, s, 16);
Ayan Banerjee34c5d892015-11-09 20:14:53 -05001245 if (nh_count < MULTIPATH_NUM) {
1246 nexthops[nh_count++] = nexthop;
1247 }
paul718e3742002-12-13 20:15:29 +00001248 break;
1249 case ZEBRA_NEXTHOP_IFINDEX:
1250 ifindex = stream_getl (s);
Ayan Banerjee34c5d892015-11-09 20:14:53 -05001251 if (if_count < MULTIPATH_NUM) {
1252 ifindices[if_count++] = ifindex;
1253 }
paul718e3742002-12-13 20:15:29 +00001254 break;
1255 }
1256 }
Ayan Banerjee34c5d892015-11-09 20:14:53 -05001257
1258 max_nh_if = (nh_count > if_count) ? nh_count : if_count;
1259 for (i = 0; i < max_nh_if; i++)
1260 {
1261 if ((i < nh_count) && !IN6_IS_ADDR_UNSPECIFIED (&nexthops[i]))
1262 {
1263 if ((i < if_count) && ifindices[i])
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -05001264 rib_nexthop_ipv6_ifindex_add (rib, &nexthops[i], ifindices[i]);
Ayan Banerjee34c5d892015-11-09 20:14:53 -05001265 else
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -05001266 rib_nexthop_ipv6_add (rib, &nexthops[i]);
Ayan Banerjee34c5d892015-11-09 20:14:53 -05001267 }
1268 else
1269 {
1270 if ((i < if_count) && ifindices[i])
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -05001271 rib_nexthop_ifindex_add (rib, ifindices[i]);
Ayan Banerjee34c5d892015-11-09 20:14:53 -05001272 }
1273 }
paul718e3742002-12-13 20:15:29 +00001274 }
1275
Ayan Banerjee34c5d892015-11-09 20:14:53 -05001276 /* Distance. */
1277 if (CHECK_FLAG (message, ZAPI_MESSAGE_DISTANCE))
1278 rib->distance = stream_getc (s);
paul718e3742002-12-13 20:15:29 +00001279
Ayan Banerjee34c5d892015-11-09 20:14:53 -05001280 /* Metric. */
1281 if (CHECK_FLAG (message, ZAPI_MESSAGE_METRIC))
1282 rib->metric = stream_getl (s);
Timo Teräsb11f3b52015-11-02 16:50:07 +02001283
Ayan Banerjee34c5d892015-11-09 20:14:53 -05001284 if (CHECK_FLAG (message, ZAPI_MESSAGE_MTU))
1285 rib->mtu = stream_getl (s);
1286
Piotr Chytłaeefddcc2015-12-01 09:48:02 -05001287 /* Tag */
1288 if (CHECK_FLAG (message, ZAPI_MESSAGE_TAG))
Paul Jakma96d10602016-07-01 14:23:45 +01001289 rib->tag = stream_getl (s);
Piotr Chytłaeefddcc2015-12-01 09:48:02 -05001290
Ayan Banerjee34c5d892015-11-09 20:14:53 -05001291 /* Table */
1292 rib->table=zebrad.rtm_table_default;
Dinesh Dutt9ae85522015-05-19 17:47:22 -07001293 ret = rib_add_ipv6_multipath (&p, rib, safi);
1294 /* Stats */
1295 if (ret > 0)
1296 client->v6_route_add_cnt++;
1297 else if (ret < 0)
1298 client->v6_route_upd8_cnt++;
1299
ajs719e9742005-02-28 20:52:15 +00001300 return 0;
paul718e3742002-12-13 20:15:29 +00001301}
1302
1303/* Zebra server IPv6 prefix delete function. */
ajs719e9742005-02-28 20:52:15 +00001304static int
Feng Luc99f3482014-10-16 09:52:36 +08001305zread_ipv6_delete (struct zserv *client, u_short length, vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +00001306{
1307 int i;
1308 struct stream *s;
1309 struct zapi_ipv6 api;
1310 struct in6_addr nexthop;
1311 unsigned long ifindex;
1312 struct prefix_ipv6 p;
1313
1314 s = client->ibuf;
1315 ifindex = 0;
1316 memset (&nexthop, 0, sizeof (struct in6_addr));
1317
1318 /* Type, flags, message. */
1319 api.type = stream_getc (s);
1320 api.flags = stream_getc (s);
1321 api.message = stream_getc (s);
G.Balajif768f362011-11-26 22:10:39 +04001322 api.safi = stream_getw (s);
paul718e3742002-12-13 20:15:29 +00001323
1324 /* IPv4 prefix. */
1325 memset (&p, 0, sizeof (struct prefix_ipv6));
1326 p.family = AF_INET6;
1327 p.prefixlen = stream_getc (s);
1328 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
1329
1330 /* Nexthop, ifindex, distance, metric. */
1331 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
1332 {
1333 u_char nexthop_type;
1334
1335 api.nexthop_num = stream_getc (s);
1336 for (i = 0; i < api.nexthop_num; i++)
1337 {
1338 nexthop_type = stream_getc (s);
1339
1340 switch (nexthop_type)
1341 {
1342 case ZEBRA_NEXTHOP_IPV6:
1343 stream_get (&nexthop, s, 16);
1344 break;
1345 case ZEBRA_NEXTHOP_IFINDEX:
1346 ifindex = stream_getl (s);
1347 break;
1348 }
1349 }
1350 }
1351
Piotr Chytłaeefddcc2015-12-01 09:48:02 -05001352 /* Distance. */
paul718e3742002-12-13 20:15:29 +00001353 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
1354 api.distance = stream_getc (s);
1355 else
1356 api.distance = 0;
Piotr Chytłaeefddcc2015-12-01 09:48:02 -05001357
1358 /* Metric. */
paul718e3742002-12-13 20:15:29 +00001359 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
1360 api.metric = stream_getl (s);
1361 else
1362 api.metric = 0;
1363
Piotr Chytłaeefddcc2015-12-01 09:48:02 -05001364 /* tag */
1365 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_TAG))
Paul Jakma96d10602016-07-01 14:23:45 +01001366 api.tag = stream_getl (s);
Piotr Chytłaeefddcc2015-12-01 09:48:02 -05001367 else
1368 api.tag = 0;
1369
paul718e3742002-12-13 20:15:29 +00001370 if (IN6_IS_ADDR_UNSPECIFIED (&nexthop))
Feng Luc99f3482014-10-16 09:52:36 +08001371 rib_delete_ipv6 (api.type, api.flags, &p, NULL, ifindex, vrf_id,
Feng Lu0d0686f2015-05-22 11:40:02 +02001372 api.safi);
paul718e3742002-12-13 20:15:29 +00001373 else
Feng Luc99f3482014-10-16 09:52:36 +08001374 rib_delete_ipv6 (api.type, api.flags, &p, &nexthop, ifindex, vrf_id,
Feng Lu0d0686f2015-05-22 11:40:02 +02001375 api.safi);
Dinesh Dutt9ae85522015-05-19 17:47:22 -07001376
1377 client->v6_route_del_cnt++;
ajs719e9742005-02-28 20:52:15 +00001378 return 0;
paul718e3742002-12-13 20:15:29 +00001379}
1380
ajs719e9742005-02-28 20:52:15 +00001381static int
Feng Luc99f3482014-10-16 09:52:36 +08001382zread_ipv6_nexthop_lookup (struct zserv *client, u_short length,
1383 vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +00001384{
1385 struct in6_addr addr;
1386 char buf[BUFSIZ];
1387
1388 stream_get (&addr, client->ibuf, 16);
Christian Frankea5207082013-04-11 08:24:29 +00001389 if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
1390 zlog_debug("%s: looking up %s", __func__,
1391 inet_ntop (AF_INET6, &addr, buf, BUFSIZ));
paul718e3742002-12-13 20:15:29 +00001392
Feng Luc99f3482014-10-16 09:52:36 +08001393 return zsend_ipv6_nexthop_lookup (client, &addr, vrf_id);
paul718e3742002-12-13 20:15:29 +00001394}
1395#endif /* HAVE_IPV6 */
1396
hasso18a6dce2004-10-03 18:18:34 +00001397/* Register zebra server router-id information. Send current router-id */
ajs719e9742005-02-28 20:52:15 +00001398static int
Feng Luc99f3482014-10-16 09:52:36 +08001399zread_router_id_add (struct zserv *client, u_short length, vrf_id_t vrf_id)
hasso18a6dce2004-10-03 18:18:34 +00001400{
1401 struct prefix p;
1402
1403 /* Router-id information is needed. */
Feng Luc99f3482014-10-16 09:52:36 +08001404 vrf_bitmap_set (client->ridinfo, vrf_id);
hasso18a6dce2004-10-03 18:18:34 +00001405
Feng Luc99f3482014-10-16 09:52:36 +08001406 router_id_get (&p, vrf_id);
hasso18a6dce2004-10-03 18:18:34 +00001407
Feng Luc99f3482014-10-16 09:52:36 +08001408 return zsend_router_id_update (client, &p, vrf_id);
hasso18a6dce2004-10-03 18:18:34 +00001409}
1410
1411/* Unregister zebra server router-id information. */
ajs719e9742005-02-28 20:52:15 +00001412static int
Feng Luc99f3482014-10-16 09:52:36 +08001413zread_router_id_delete (struct zserv *client, u_short length, vrf_id_t vrf_id)
hasso18a6dce2004-10-03 18:18:34 +00001414{
Feng Luc99f3482014-10-16 09:52:36 +08001415 vrf_bitmap_unset (client->ridinfo, vrf_id);
ajs719e9742005-02-28 20:52:15 +00001416 return 0;
hasso18a6dce2004-10-03 18:18:34 +00001417}
1418
Vyacheslav Trushkin2ea1ab12011-12-11 18:48:47 +04001419/* Tie up route-type and client->sock */
1420static void
1421zread_hello (struct zserv *client)
1422{
1423 /* type of protocol (lib/zebra.h) */
1424 u_char proto;
1425 proto = stream_getc (client->ibuf);
1426
1427 /* accept only dynamic routing protocols */
1428 if ((proto < ZEBRA_ROUTE_MAX)
1429 && (proto > ZEBRA_ROUTE_STATIC))
1430 {
1431 zlog_notice ("client %d says hello and bids fair to announce only %s routes",
1432 client->sock, zebra_route_string(proto));
1433
1434 /* if route-type was binded by other client */
1435 if (route_type_oaths[proto])
1436 zlog_warn ("sender of %s routes changed %c->%c",
1437 zebra_route_string(proto), route_type_oaths[proto],
1438 client->sock);
1439
1440 route_type_oaths[proto] = client->sock;
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -05001441 client->proto = proto;
Vyacheslav Trushkin2ea1ab12011-12-11 18:48:47 +04001442 }
1443}
1444
Feng Luc99f3482014-10-16 09:52:36 +08001445/* Unregister all information in a VRF. */
1446static int
1447zread_vrf_unregister (struct zserv *client, u_short length, vrf_id_t vrf_id)
1448{
1449 int i;
1450
1451 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
1452 vrf_bitmap_unset (client->redist[i], vrf_id);
1453 vrf_bitmap_unset (client->redist_default, vrf_id);
1454 vrf_bitmap_unset (client->ifinfo, vrf_id);
1455 vrf_bitmap_unset (client->ridinfo, vrf_id);
1456
1457 return 0;
1458}
1459
Vyacheslav Trushkin2ea1ab12011-12-11 18:48:47 +04001460/* If client sent routes of specific type, zebra removes it
1461 * and returns number of deleted routes.
1462 */
1463static void
1464zebra_score_rib (int client_sock)
1465{
1466 int i;
1467
1468 for (i = ZEBRA_ROUTE_RIP; i < ZEBRA_ROUTE_MAX; i++)
1469 if (client_sock == route_type_oaths[i])
1470 {
1471 zlog_notice ("client %d disconnected. %lu %s routes removed from the rib",
1472 client_sock, rib_score_proto (i), zebra_route_string (i));
1473 route_type_oaths[i] = 0;
1474 break;
1475 }
1476}
1477
paul718e3742002-12-13 20:15:29 +00001478/* Close zebra client. */
paulb9df2d22004-05-09 09:09:59 +00001479static void
paul718e3742002-12-13 20:15:29 +00001480zebra_client_close (struct zserv *client)
1481{
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -05001482 zebra_cleanup_rnh_client(0, AF_INET, client);
1483 zebra_cleanup_rnh_client(0, AF_INET6, client);
1484
paul718e3742002-12-13 20:15:29 +00001485 /* Close file descriptor. */
1486 if (client->sock)
1487 {
1488 close (client->sock);
Vyacheslav Trushkin2ea1ab12011-12-11 18:48:47 +04001489 zebra_score_rib (client->sock);
paul718e3742002-12-13 20:15:29 +00001490 client->sock = -1;
1491 }
1492
1493 /* Free stream buffers. */
1494 if (client->ibuf)
1495 stream_free (client->ibuf);
1496 if (client->obuf)
1497 stream_free (client->obuf);
ajs719e9742005-02-28 20:52:15 +00001498 if (client->wb)
1499 buffer_free(client->wb);
paul718e3742002-12-13 20:15:29 +00001500
1501 /* Release threads. */
1502 if (client->t_read)
1503 thread_cancel (client->t_read);
1504 if (client->t_write)
1505 thread_cancel (client->t_write);
ajs719e9742005-02-28 20:52:15 +00001506 if (client->t_suicide)
1507 thread_cancel (client->t_suicide);
paul718e3742002-12-13 20:15:29 +00001508
1509 /* Free client structure. */
paulb21b19c2003-06-15 01:28:29 +00001510 listnode_delete (zebrad.client_list, client);
paul718e3742002-12-13 20:15:29 +00001511 XFREE (0, client);
1512}
1513
1514/* Make new client. */
paulb9df2d22004-05-09 09:09:59 +00001515static void
paul718e3742002-12-13 20:15:29 +00001516zebra_client_create (int sock)
1517{
1518 struct zserv *client;
Feng Luc99f3482014-10-16 09:52:36 +08001519 int i;
paul718e3742002-12-13 20:15:29 +00001520
David Lamparter23757db2016-02-24 06:26:02 +01001521 client = XCALLOC (MTYPE_TMP, sizeof (struct zserv));
paul718e3742002-12-13 20:15:29 +00001522
1523 /* Make client input/output buffer. */
1524 client->sock = sock;
1525 client->ibuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
1526 client->obuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
ajs719e9742005-02-28 20:52:15 +00001527 client->wb = buffer_new(0);
paul718e3742002-12-13 20:15:29 +00001528
1529 /* Set table number. */
paulb21b19c2003-06-15 01:28:29 +00001530 client->rtm_table = zebrad.rtm_table_default;
paul718e3742002-12-13 20:15:29 +00001531
Feng Luc99f3482014-10-16 09:52:36 +08001532 /* Initialize flags */
1533 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
1534 client->redist[i] = vrf_bitmap_init ();
1535 client->redist_default = vrf_bitmap_init ();
1536 client->ifinfo = vrf_bitmap_init ();
1537 client->ridinfo = vrf_bitmap_init ();
Dinesh Dutt9ae85522015-05-19 17:47:22 -07001538 client->connect_time = quagga_time(NULL);
Feng Luc99f3482014-10-16 09:52:36 +08001539
paul718e3742002-12-13 20:15:29 +00001540 /* Add this client to linked list. */
paulb21b19c2003-06-15 01:28:29 +00001541 listnode_add (zebrad.client_list, client);
paul718e3742002-12-13 20:15:29 +00001542
1543 /* Make new read thread. */
1544 zebra_event (ZEBRA_READ, sock, client);
1545}
1546
1547/* Handler of zebra service request. */
paulb9df2d22004-05-09 09:09:59 +00001548static int
paul718e3742002-12-13 20:15:29 +00001549zebra_client_read (struct thread *thread)
1550{
1551 int sock;
1552 struct zserv *client;
ajs57a14772005-04-10 15:01:56 +00001553 size_t already;
paulc1b98002006-01-16 01:54:02 +00001554 uint16_t length, command;
1555 uint8_t marker, version;
Feng Luc99f3482014-10-16 09:52:36 +08001556 vrf_id_t vrf_id;
paul718e3742002-12-13 20:15:29 +00001557
1558 /* Get thread data. Reset reading thread because I'm running. */
1559 sock = THREAD_FD (thread);
1560 client = THREAD_ARG (thread);
1561 client->t_read = NULL;
1562
ajs719e9742005-02-28 20:52:15 +00001563 if (client->t_suicide)
paul718e3742002-12-13 20:15:29 +00001564 {
ajs719e9742005-02-28 20:52:15 +00001565 zebra_client_close(client);
paul718e3742002-12-13 20:15:29 +00001566 return -1;
1567 }
ajs719e9742005-02-28 20:52:15 +00001568
1569 /* Read length and command (if we don't have it already). */
ajs57a14772005-04-10 15:01:56 +00001570 if ((already = stream_get_endp(client->ibuf)) < ZEBRA_HEADER_SIZE)
ajs719e9742005-02-28 20:52:15 +00001571 {
ajs57a14772005-04-10 15:01:56 +00001572 ssize_t nbyte;
ajs719e9742005-02-28 20:52:15 +00001573 if (((nbyte = stream_read_try (client->ibuf, sock,
ajs57a14772005-04-10 15:01:56 +00001574 ZEBRA_HEADER_SIZE-already)) == 0) ||
ajs719e9742005-02-28 20:52:15 +00001575 (nbyte == -1))
1576 {
1577 if (IS_ZEBRA_DEBUG_EVENT)
1578 zlog_debug ("connection closed socket [%d]", sock);
1579 zebra_client_close (client);
1580 return -1;
1581 }
ajs57a14772005-04-10 15:01:56 +00001582 if (nbyte != (ssize_t)(ZEBRA_HEADER_SIZE-already))
ajs719e9742005-02-28 20:52:15 +00001583 {
1584 /* Try again later. */
1585 zebra_event (ZEBRA_READ, sock, client);
1586 return 0;
1587 }
ajs57a14772005-04-10 15:01:56 +00001588 already = ZEBRA_HEADER_SIZE;
ajs719e9742005-02-28 20:52:15 +00001589 }
1590
1591 /* Reset to read from the beginning of the incoming packet. */
1592 stream_set_getp(client->ibuf, 0);
1593
paulc1b98002006-01-16 01:54:02 +00001594 /* Fetch header values */
paul718e3742002-12-13 20:15:29 +00001595 length = stream_getw (client->ibuf);
paulc1b98002006-01-16 01:54:02 +00001596 marker = stream_getc (client->ibuf);
1597 version = stream_getc (client->ibuf);
Feng Luc99f3482014-10-16 09:52:36 +08001598 vrf_id = stream_getw (client->ibuf);
paulc1b98002006-01-16 01:54:02 +00001599 command = stream_getw (client->ibuf);
paul718e3742002-12-13 20:15:29 +00001600
paulc1b98002006-01-16 01:54:02 +00001601 if (marker != ZEBRA_HEADER_MARKER || version != ZSERV_VERSION)
1602 {
1603 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
1604 __func__, sock, marker, version);
1605 zebra_client_close (client);
1606 return -1;
1607 }
ajs719e9742005-02-28 20:52:15 +00001608 if (length < ZEBRA_HEADER_SIZE)
paul718e3742002-12-13 20:15:29 +00001609 {
ajs57a14772005-04-10 15:01:56 +00001610 zlog_warn("%s: socket %d message length %u is less than header size %d",
1611 __func__, sock, length, ZEBRA_HEADER_SIZE);
1612 zebra_client_close (client);
1613 return -1;
1614 }
1615 if (length > STREAM_SIZE(client->ibuf))
1616 {
1617 zlog_warn("%s: socket %d message length %u exceeds buffer size %lu",
1618 __func__, sock, length, (u_long)STREAM_SIZE(client->ibuf));
paul718e3742002-12-13 20:15:29 +00001619 zebra_client_close (client);
1620 return -1;
1621 }
1622
paul718e3742002-12-13 20:15:29 +00001623 /* Read rest of data. */
ajs57a14772005-04-10 15:01:56 +00001624 if (already < length)
paul718e3742002-12-13 20:15:29 +00001625 {
ajs57a14772005-04-10 15:01:56 +00001626 ssize_t nbyte;
1627 if (((nbyte = stream_read_try (client->ibuf, sock,
1628 length-already)) == 0) ||
1629 (nbyte == -1))
paul718e3742002-12-13 20:15:29 +00001630 {
1631 if (IS_ZEBRA_DEBUG_EVENT)
ajsb6178002004-12-07 21:12:56 +00001632 zlog_debug ("connection closed [%d] when reading zebra data", sock);
paul718e3742002-12-13 20:15:29 +00001633 zebra_client_close (client);
1634 return -1;
1635 }
ajs57a14772005-04-10 15:01:56 +00001636 if (nbyte != (ssize_t)(length-already))
ajs719e9742005-02-28 20:52:15 +00001637 {
1638 /* Try again later. */
1639 zebra_event (ZEBRA_READ, sock, client);
1640 return 0;
1641 }
paul718e3742002-12-13 20:15:29 +00001642 }
1643
ajs719e9742005-02-28 20:52:15 +00001644 length -= ZEBRA_HEADER_SIZE;
1645
paul718e3742002-12-13 20:15:29 +00001646 /* Debug packet information. */
1647 if (IS_ZEBRA_DEBUG_EVENT)
ajsb6178002004-12-07 21:12:56 +00001648 zlog_debug ("zebra message comes from socket [%d]", sock);
paul718e3742002-12-13 20:15:29 +00001649
1650 if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
Feng Luc99f3482014-10-16 09:52:36 +08001651 zlog_debug ("zebra message received [%s] %d in VRF %u",
1652 zserv_command_string (command), length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001653
Dinesh Dutt9ae85522015-05-19 17:47:22 -07001654 client->last_read_time = quagga_time(NULL);
1655 client->last_read_cmd = command;
1656
paul718e3742002-12-13 20:15:29 +00001657 switch (command)
1658 {
hasso18a6dce2004-10-03 18:18:34 +00001659 case ZEBRA_ROUTER_ID_ADD:
Feng Luc99f3482014-10-16 09:52:36 +08001660 zread_router_id_add (client, length, vrf_id);
hasso18a6dce2004-10-03 18:18:34 +00001661 break;
1662 case ZEBRA_ROUTER_ID_DELETE:
Feng Luc99f3482014-10-16 09:52:36 +08001663 zread_router_id_delete (client, length, vrf_id);
hasso18a6dce2004-10-03 18:18:34 +00001664 break;
paul718e3742002-12-13 20:15:29 +00001665 case ZEBRA_INTERFACE_ADD:
Feng Luc99f3482014-10-16 09:52:36 +08001666 zread_interface_add (client, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001667 break;
1668 case ZEBRA_INTERFACE_DELETE:
Feng Luc99f3482014-10-16 09:52:36 +08001669 zread_interface_delete (client, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001670 break;
1671 case ZEBRA_IPV4_ROUTE_ADD:
Feng Luc99f3482014-10-16 09:52:36 +08001672 zread_ipv4_add (client, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001673 break;
1674 case ZEBRA_IPV4_ROUTE_DELETE:
Feng Luc99f3482014-10-16 09:52:36 +08001675 zread_ipv4_delete (client, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001676 break;
1677#ifdef HAVE_IPV6
1678 case ZEBRA_IPV6_ROUTE_ADD:
Feng Luc99f3482014-10-16 09:52:36 +08001679 zread_ipv6_add (client, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001680 break;
1681 case ZEBRA_IPV6_ROUTE_DELETE:
Feng Luc99f3482014-10-16 09:52:36 +08001682 zread_ipv6_delete (client, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001683 break;
1684#endif /* HAVE_IPV6 */
1685 case ZEBRA_REDISTRIBUTE_ADD:
Feng Luc99f3482014-10-16 09:52:36 +08001686 zebra_redistribute_add (command, client, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001687 break;
1688 case ZEBRA_REDISTRIBUTE_DELETE:
Feng Luc99f3482014-10-16 09:52:36 +08001689 zebra_redistribute_delete (command, client, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001690 break;
1691 case ZEBRA_REDISTRIBUTE_DEFAULT_ADD:
Feng Luc99f3482014-10-16 09:52:36 +08001692 zebra_redistribute_default_add (command, client, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001693 break;
1694 case ZEBRA_REDISTRIBUTE_DEFAULT_DELETE:
Feng Luc99f3482014-10-16 09:52:36 +08001695 zebra_redistribute_default_delete (command, client, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001696 break;
1697 case ZEBRA_IPV4_NEXTHOP_LOOKUP:
Feng Luc99f3482014-10-16 09:52:36 +08001698 zread_ipv4_nexthop_lookup (client, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001699 break;
Everton Marques4e5275b2014-07-01 15:15:52 -03001700 case ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB:
Feng Luc99f3482014-10-16 09:52:36 +08001701 zread_ipv4_nexthop_lookup_mrib (client, length, vrf_id);
Everton Marques4e5275b2014-07-01 15:15:52 -03001702 break;
paul718e3742002-12-13 20:15:29 +00001703#ifdef HAVE_IPV6
1704 case ZEBRA_IPV6_NEXTHOP_LOOKUP:
Feng Luc99f3482014-10-16 09:52:36 +08001705 zread_ipv6_nexthop_lookup (client, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001706 break;
1707#endif /* HAVE_IPV6 */
1708 case ZEBRA_IPV4_IMPORT_LOOKUP:
Feng Luc99f3482014-10-16 09:52:36 +08001709 zread_ipv4_import_lookup (client, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001710 break;
Vyacheslav Trushkin2ea1ab12011-12-11 18:48:47 +04001711 case ZEBRA_HELLO:
1712 zread_hello (client);
1713 break;
Feng Luc99f3482014-10-16 09:52:36 +08001714 case ZEBRA_VRF_UNREGISTER:
1715 zread_vrf_unregister (client, length, vrf_id);
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -05001716 case ZEBRA_NEXTHOP_REGISTER:
1717 zserv_nexthop_register(client, sock, length, vrf_id);
1718 break;
1719 case ZEBRA_NEXTHOP_UNREGISTER:
1720 zserv_nexthop_unregister(client, sock, length);
Feng Luc99f3482014-10-16 09:52:36 +08001721 break;
paul718e3742002-12-13 20:15:29 +00001722 default:
1723 zlog_info ("Zebra received unknown command %d", command);
1724 break;
1725 }
1726
ajs719e9742005-02-28 20:52:15 +00001727 if (client->t_suicide)
1728 {
1729 /* No need to wait for thread callback, just kill immediately. */
1730 zebra_client_close(client);
1731 return -1;
1732 }
1733
paul718e3742002-12-13 20:15:29 +00001734 stream_reset (client->ibuf);
1735 zebra_event (ZEBRA_READ, sock, client);
paul718e3742002-12-13 20:15:29 +00001736 return 0;
1737}
1738
paul718e3742002-12-13 20:15:29 +00001739
1740/* Accept code of zebra server socket. */
paulb9df2d22004-05-09 09:09:59 +00001741static int
paul718e3742002-12-13 20:15:29 +00001742zebra_accept (struct thread *thread)
1743{
1744 int accept_sock;
1745 int client_sock;
1746 struct sockaddr_in client;
1747 socklen_t len;
1748
1749 accept_sock = THREAD_FD (thread);
1750
ajs719e9742005-02-28 20:52:15 +00001751 /* Reregister myself. */
1752 zebra_event (ZEBRA_SERV, accept_sock, NULL);
1753
paul718e3742002-12-13 20:15:29 +00001754 len = sizeof (struct sockaddr_in);
1755 client_sock = accept (accept_sock, (struct sockaddr *) &client, &len);
1756
1757 if (client_sock < 0)
1758 {
ajs6099b3b2004-11-20 02:06:59 +00001759 zlog_warn ("Can't accept zebra socket: %s", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001760 return -1;
1761 }
1762
paulccf35572003-03-01 11:42:20 +00001763 /* Make client socket non-blocking. */
ajs719e9742005-02-28 20:52:15 +00001764 set_nonblocking(client_sock);
paul865b8522005-01-05 08:30:35 +00001765
paul718e3742002-12-13 20:15:29 +00001766 /* Create new zebra client. */
1767 zebra_client_create (client_sock);
1768
paul718e3742002-12-13 20:15:29 +00001769 return 0;
1770}
1771
paulb9df2d22004-05-09 09:09:59 +00001772#ifdef HAVE_TCP_ZEBRA
paul718e3742002-12-13 20:15:29 +00001773/* Make zebra's server socket. */
paulb9df2d22004-05-09 09:09:59 +00001774static void
paul718e3742002-12-13 20:15:29 +00001775zebra_serv ()
1776{
1777 int ret;
1778 int accept_sock;
1779 struct sockaddr_in addr;
1780
1781 accept_sock = socket (AF_INET, SOCK_STREAM, 0);
1782
1783 if (accept_sock < 0)
1784 {
paul3d1dc852005-04-05 00:45:23 +00001785 zlog_warn ("Can't create zserv stream socket: %s",
1786 safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001787 zlog_warn ("zebra can't provice full functionality due to above error");
1788 return;
1789 }
1790
Vyacheslav Trushkin2ea1ab12011-12-11 18:48:47 +04001791 memset (&route_type_oaths, 0, sizeof (route_type_oaths));
paul718e3742002-12-13 20:15:29 +00001792 memset (&addr, 0, sizeof (struct sockaddr_in));
1793 addr.sin_family = AF_INET;
1794 addr.sin_port = htons (ZEBRA_PORT);
Paul Jakma6f0e3f62007-05-10 02:38:51 +00001795#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
paul718e3742002-12-13 20:15:29 +00001796 addr.sin_len = sizeof (struct sockaddr_in);
Paul Jakma6f0e3f62007-05-10 02:38:51 +00001797#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
paul718e3742002-12-13 20:15:29 +00001798 addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
1799
1800 sockopt_reuseaddr (accept_sock);
1801 sockopt_reuseport (accept_sock);
1802
pauledd7c242003-06-04 13:59:38 +00001803 if ( zserv_privs.change(ZPRIVS_RAISE) )
1804 zlog (NULL, LOG_ERR, "Can't raise privileges");
1805
paul718e3742002-12-13 20:15:29 +00001806 ret = bind (accept_sock, (struct sockaddr *)&addr,
1807 sizeof (struct sockaddr_in));
1808 if (ret < 0)
1809 {
paul3d1dc852005-04-05 00:45:23 +00001810 zlog_warn ("Can't bind to stream socket: %s",
1811 safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001812 zlog_warn ("zebra can't provice full functionality due to above error");
1813 close (accept_sock); /* Avoid sd leak. */
1814 return;
1815 }
pauledd7c242003-06-04 13:59:38 +00001816
1817 if ( zserv_privs.change(ZPRIVS_LOWER) )
1818 zlog (NULL, LOG_ERR, "Can't lower privileges");
paul718e3742002-12-13 20:15:29 +00001819
1820 ret = listen (accept_sock, 1);
1821 if (ret < 0)
1822 {
paul3d1dc852005-04-05 00:45:23 +00001823 zlog_warn ("Can't listen to stream socket: %s",
1824 safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001825 zlog_warn ("zebra can't provice full functionality due to above error");
1826 close (accept_sock); /* Avoid sd leak. */
1827 return;
1828 }
1829
1830 zebra_event (ZEBRA_SERV, accept_sock, NULL);
1831}
David Lamparter4b6c3322015-04-21 09:47:57 +02001832#else /* HAVE_TCP_ZEBRA */
paul718e3742002-12-13 20:15:29 +00001833
1834/* For sockaddr_un. */
1835#include <sys/un.h>
1836
1837/* zebra server UNIX domain socket. */
paulb9df2d22004-05-09 09:09:59 +00001838static void
hassofce954f2004-10-07 20:29:24 +00001839zebra_serv_un (const char *path)
paul718e3742002-12-13 20:15:29 +00001840{
1841 int ret;
1842 int sock, len;
1843 struct sockaddr_un serv;
1844 mode_t old_mask;
1845
1846 /* First of all, unlink existing socket */
1847 unlink (path);
1848
1849 /* Set umask */
1850 old_mask = umask (0077);
1851
1852 /* Make UNIX domain socket. */
1853 sock = socket (AF_UNIX, SOCK_STREAM, 0);
1854 if (sock < 0)
1855 {
paul3d1dc852005-04-05 00:45:23 +00001856 zlog_warn ("Can't create zserv unix socket: %s",
1857 safe_strerror (errno));
1858 zlog_warn ("zebra can't provide full functionality due to above error");
paul718e3742002-12-13 20:15:29 +00001859 return;
1860 }
1861
Vyacheslav Trushkin2ea1ab12011-12-11 18:48:47 +04001862 memset (&route_type_oaths, 0, sizeof (route_type_oaths));
1863
paul718e3742002-12-13 20:15:29 +00001864 /* Make server socket. */
1865 memset (&serv, 0, sizeof (struct sockaddr_un));
1866 serv.sun_family = AF_UNIX;
1867 strncpy (serv.sun_path, path, strlen (path));
Paul Jakma6f0e3f62007-05-10 02:38:51 +00001868#ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
paul718e3742002-12-13 20:15:29 +00001869 len = serv.sun_len = SUN_LEN(&serv);
1870#else
1871 len = sizeof (serv.sun_family) + strlen (serv.sun_path);
Paul Jakma6f0e3f62007-05-10 02:38:51 +00001872#endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
paul718e3742002-12-13 20:15:29 +00001873
1874 ret = bind (sock, (struct sockaddr *) &serv, len);
1875 if (ret < 0)
1876 {
paul3d1dc852005-04-05 00:45:23 +00001877 zlog_warn ("Can't bind to unix socket %s: %s",
1878 path, safe_strerror (errno));
1879 zlog_warn ("zebra can't provide full functionality due to above error");
paul718e3742002-12-13 20:15:29 +00001880 close (sock);
1881 return;
1882 }
1883
1884 ret = listen (sock, 5);
1885 if (ret < 0)
1886 {
paul3d1dc852005-04-05 00:45:23 +00001887 zlog_warn ("Can't listen to unix socket %s: %s",
1888 path, safe_strerror (errno));
1889 zlog_warn ("zebra can't provide full functionality due to above error");
paul718e3742002-12-13 20:15:29 +00001890 close (sock);
1891 return;
1892 }
1893
1894 umask (old_mask);
1895
1896 zebra_event (ZEBRA_SERV, sock, NULL);
1897}
David Lamparter4b6c3322015-04-21 09:47:57 +02001898#endif /* HAVE_TCP_ZEBRA */
David Lamparter6b0655a2014-06-04 06:53:35 +02001899
paul718e3742002-12-13 20:15:29 +00001900
paulb9df2d22004-05-09 09:09:59 +00001901static void
paul718e3742002-12-13 20:15:29 +00001902zebra_event (enum event event, int sock, struct zserv *client)
1903{
1904 switch (event)
1905 {
1906 case ZEBRA_SERV:
paulb21b19c2003-06-15 01:28:29 +00001907 thread_add_read (zebrad.master, zebra_accept, client, sock);
paul718e3742002-12-13 20:15:29 +00001908 break;
1909 case ZEBRA_READ:
1910 client->t_read =
paulb21b19c2003-06-15 01:28:29 +00001911 thread_add_read (zebrad.master, zebra_client_read, client, sock);
paul718e3742002-12-13 20:15:29 +00001912 break;
1913 case ZEBRA_WRITE:
1914 /**/
1915 break;
1916 }
1917}
David Lamparter6b0655a2014-06-04 06:53:35 +02001918
Dinesh Dutt9ae85522015-05-19 17:47:22 -07001919#define ZEBRA_TIME_BUF 32
1920static char *
1921zserv_time_buf(time_t *time1, char *buf, int buflen)
1922{
1923 struct tm *tm;
1924 time_t now;
1925
1926 assert (buf != NULL);
1927 assert (buflen >= ZEBRA_TIME_BUF);
1928 assert (time1 != NULL);
1929
1930 if (!*time1)
1931 {
1932 snprintf(buf, buflen, "never ");
1933 return (buf);
1934 }
1935
1936 now = quagga_time(NULL);
1937 now -= *time1;
1938 tm = gmtime(&now);
1939
1940 /* Making formatted timer strings. */
1941#define ONE_DAY_SECOND 60*60*24
1942#define ONE_WEEK_SECOND 60*60*24*7
1943
1944 if (now < ONE_DAY_SECOND)
1945 snprintf (buf, buflen, "%02d:%02d:%02d",
1946 tm->tm_hour, tm->tm_min, tm->tm_sec);
1947 else if (now < ONE_WEEK_SECOND)
1948 snprintf (buf, buflen, "%dd%02dh%02dm",
1949 tm->tm_yday, tm->tm_hour, tm->tm_min);
1950 else
1951 snprintf (buf, buflen, "%02dw%dd%02dh",
1952 tm->tm_yday/7, tm->tm_yday - ((tm->tm_yday/7) * 7), tm->tm_hour);
1953 return buf;
1954}
1955
1956static void
1957zebra_show_client_detail (struct vty *vty, struct zserv *client)
1958{
1959 char cbuf[ZEBRA_TIME_BUF], rbuf[ZEBRA_TIME_BUF];
1960 char wbuf[ZEBRA_TIME_BUF], nhbuf[ZEBRA_TIME_BUF], mbuf[ZEBRA_TIME_BUF];
1961
1962 vty_out (vty, "Client: %s %s",
1963 zebra_route_string(client->proto), VTY_NEWLINE);
1964 vty_out (vty, "------------------------ %s", VTY_NEWLINE);
1965 vty_out (vty, "FD: %d %s", client->sock, VTY_NEWLINE);
1966 vty_out (vty, "Route Table ID: %d %s", client->rtm_table, VTY_NEWLINE);
1967
1968 vty_out (vty, "Connect Time: %s %s",
1969 zserv_time_buf(&client->connect_time, cbuf, ZEBRA_TIME_BUF),
1970 VTY_NEWLINE);
1971 if (client->nh_reg_time)
1972 {
1973 vty_out (vty, "Nexthop Registry Time: %s %s",
1974 zserv_time_buf(&client->nh_reg_time, nhbuf, ZEBRA_TIME_BUF),
1975 VTY_NEWLINE);
1976 if (client->nh_last_upd_time)
1977 vty_out (vty, "Nexthop Last Update Time: %s %s",
1978 zserv_time_buf(&client->nh_last_upd_time, mbuf, ZEBRA_TIME_BUF),
1979 VTY_NEWLINE);
1980 else
1981 vty_out (vty, "No Nexthop Update sent%s", VTY_NEWLINE);
1982 }
1983 else
1984 vty_out (vty, "Not registered for Nexthop Updates%s", VTY_NEWLINE);
1985
1986 vty_out (vty, "Last Msg Rx Time: %s %s",
1987 zserv_time_buf(&client->last_read_time, rbuf, ZEBRA_TIME_BUF),
1988 VTY_NEWLINE);
1989 vty_out (vty, "Last Msg Tx Time: %s %s",
1990 zserv_time_buf(&client->last_write_time, wbuf, ZEBRA_TIME_BUF),
1991 VTY_NEWLINE);
1992 if (client->last_read_time)
1993 vty_out (vty, "Last Rcvd Cmd: %s %s",
1994 zserv_command_string(client->last_read_cmd), VTY_NEWLINE);
1995 if (client->last_write_time)
1996 vty_out (vty, "Last Sent Cmd: %s %s",
1997 zserv_command_string(client->last_write_cmd), VTY_NEWLINE);
1998 vty_out (vty, "%s", VTY_NEWLINE);
1999
2000 vty_out (vty, "Type Add Update Del %s", VTY_NEWLINE);
2001 vty_out (vty, "================================================== %s", VTY_NEWLINE);
2002 vty_out (vty, "IPv4 %-12d%-12d%-12d%s", client->v4_route_add_cnt,
2003 client->v4_route_upd8_cnt, client->v4_route_del_cnt, VTY_NEWLINE);
2004 vty_out (vty, "IPv6 %-12d%-12d%-12d%s", client->v6_route_add_cnt,
2005 client->v6_route_upd8_cnt, client->v6_route_del_cnt, VTY_NEWLINE);
2006 vty_out (vty, "Redist:v4 %-12d%-12d%-12d%s", client->redist_v4_add_cnt, 0,
2007 client->redist_v4_del_cnt, VTY_NEWLINE);
2008 vty_out (vty, "Redist:v6 %-12d%-12d%-12d%s", client->redist_v6_add_cnt, 0,
2009 client->redist_v6_del_cnt, VTY_NEWLINE);
2010 vty_out (vty, "Connected %-12d%-12d%-12d%s", client->ifadd_cnt, 0,
2011 client->ifdel_cnt, VTY_NEWLINE);
2012 vty_out (vty, "Interface Up Notifications: %d%s", client->ifup_cnt,
2013 VTY_NEWLINE);
2014 vty_out (vty, "Interface Down Notifications: %d%s", client->ifdown_cnt,
2015 VTY_NEWLINE);
2016
2017 vty_out (vty, "%s", VTY_NEWLINE);
2018 return;
2019}
2020
2021static void
2022zebra_show_client_brief (struct vty *vty, struct zserv *client)
2023{
2024 char cbuf[ZEBRA_TIME_BUF], rbuf[ZEBRA_TIME_BUF];
2025 char wbuf[ZEBRA_TIME_BUF];
2026
2027 vty_out (vty, "%-8s%12s %12s%12s%8d/%-8d%8d/%-8d%s",
2028 zebra_route_string(client->proto),
2029 zserv_time_buf(&client->connect_time, cbuf, ZEBRA_TIME_BUF),
2030 zserv_time_buf(&client->last_read_time, rbuf, ZEBRA_TIME_BUF),
2031 zserv_time_buf(&client->last_write_time, wbuf, ZEBRA_TIME_BUF),
2032 client->v4_route_add_cnt+client->v4_route_upd8_cnt,
2033 client->v4_route_del_cnt,
2034 client->v6_route_add_cnt+client->v6_route_upd8_cnt,
2035 client->v6_route_del_cnt, VTY_NEWLINE);
2036
2037}
2038
2039
paul718e3742002-12-13 20:15:29 +00002040/* Display default rtm_table for all clients. */
2041DEFUN (show_table,
2042 show_table_cmd,
2043 "show table",
2044 SHOW_STR
2045 "default routing table to use for all clients\n")
2046{
paulb21b19c2003-06-15 01:28:29 +00002047 vty_out (vty, "table %d%s", zebrad.rtm_table_default,
paul718e3742002-12-13 20:15:29 +00002048 VTY_NEWLINE);
2049 return CMD_SUCCESS;
2050}
2051
2052DEFUN (config_table,
2053 config_table_cmd,
2054 "table TABLENO",
2055 "Configure target kernel routing table\n"
2056 "TABLE integer\n")
2057{
paulb21b19c2003-06-15 01:28:29 +00002058 zebrad.rtm_table_default = strtol (argv[0], (char**)0, 10);
paul718e3742002-12-13 20:15:29 +00002059 return CMD_SUCCESS;
2060}
2061
hasso647e4f12003-05-25 11:43:52 +00002062DEFUN (ip_forwarding,
2063 ip_forwarding_cmd,
2064 "ip forwarding",
2065 IP_STR
2066 "Turn on IP forwarding")
2067{
2068 int ret;
2069
2070 ret = ipforward ();
hassob71f00f2004-10-13 12:20:35 +00002071 if (ret == 0)
2072 ret = ipforward_on ();
hasso647e4f12003-05-25 11:43:52 +00002073
hasso647e4f12003-05-25 11:43:52 +00002074 if (ret == 0)
2075 {
2076 vty_out (vty, "Can't turn on IP forwarding%s", VTY_NEWLINE);
2077 return CMD_WARNING;
2078 }
2079
2080 return CMD_SUCCESS;
2081}
2082
paul718e3742002-12-13 20:15:29 +00002083DEFUN (no_ip_forwarding,
2084 no_ip_forwarding_cmd,
2085 "no ip forwarding",
2086 NO_STR
2087 IP_STR
2088 "Turn off IP forwarding")
2089{
2090 int ret;
2091
2092 ret = ipforward ();
hassob71f00f2004-10-13 12:20:35 +00002093 if (ret != 0)
2094 ret = ipforward_off ();
paul718e3742002-12-13 20:15:29 +00002095
paul718e3742002-12-13 20:15:29 +00002096 if (ret != 0)
2097 {
2098 vty_out (vty, "Can't turn off IP forwarding%s", VTY_NEWLINE);
2099 return CMD_WARNING;
2100 }
2101
2102 return CMD_SUCCESS;
2103}
2104
2105/* This command is for debugging purpose. */
2106DEFUN (show_zebra_client,
2107 show_zebra_client_cmd,
2108 "show zebra client",
2109 SHOW_STR
2110 "Zebra information"
2111 "Client information")
2112{
hasso52dc7ee2004-09-23 19:18:23 +00002113 struct listnode *node;
paul718e3742002-12-13 20:15:29 +00002114 struct zserv *client;
2115
paul1eb8ef22005-04-07 07:30:20 +00002116 for (ALL_LIST_ELEMENTS_RO (zebrad.client_list, node, client))
Dinesh Dutt9ae85522015-05-19 17:47:22 -07002117 zebra_show_client_detail(vty, client);
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -05002118
paul718e3742002-12-13 20:15:29 +00002119 return CMD_SUCCESS;
2120}
2121
Dinesh Dutt9ae85522015-05-19 17:47:22 -07002122/* This command is for debugging purpose. */
2123DEFUN (show_zebra_client_summary,
2124 show_zebra_client_summary_cmd,
2125 "show zebra client summary",
2126 SHOW_STR
2127 "Zebra information brief"
2128 "Client information brief")
2129{
2130 struct listnode *node;
2131 struct zserv *client;
2132
2133 vty_out (vty, "Name Connect Time Last Read Last Write IPv4 Routes IPv6 Routes %s",
2134 VTY_NEWLINE);
2135 vty_out (vty,"--------------------------------------------------------------------------------%s",
2136 VTY_NEWLINE);
2137
2138 for (ALL_LIST_ELEMENTS_RO (zebrad.client_list, node, client))
2139 zebra_show_client_brief(vty, client);
2140
2141 vty_out (vty, "Routes column shows (added+updated)/deleted%s", VTY_NEWLINE);
2142 return CMD_SUCCESS;
2143}
2144
paul718e3742002-12-13 20:15:29 +00002145/* Table configuration write function. */
paulb9df2d22004-05-09 09:09:59 +00002146static int
paul718e3742002-12-13 20:15:29 +00002147config_write_table (struct vty *vty)
2148{
paulb21b19c2003-06-15 01:28:29 +00002149 if (zebrad.rtm_table_default)
2150 vty_out (vty, "table %d%s", zebrad.rtm_table_default,
paul718e3742002-12-13 20:15:29 +00002151 VTY_NEWLINE);
2152 return 0;
2153}
2154
2155/* table node for routing tables. */
Stephen Hemminger7fc626d2008-12-01 11:10:34 -08002156static struct cmd_node table_node =
paul718e3742002-12-13 20:15:29 +00002157{
2158 TABLE_NODE,
2159 "", /* This node has no interface. */
2160 1
2161};
David Lamparter6b0655a2014-06-04 06:53:35 +02002162
paul718e3742002-12-13 20:15:29 +00002163/* Only display ip forwarding is enabled or not. */
2164DEFUN (show_ip_forwarding,
2165 show_ip_forwarding_cmd,
2166 "show ip forwarding",
2167 SHOW_STR
2168 IP_STR
2169 "IP forwarding status\n")
2170{
2171 int ret;
2172
2173 ret = ipforward ();
2174
2175 if (ret == 0)
2176 vty_out (vty, "IP forwarding is off%s", VTY_NEWLINE);
2177 else
2178 vty_out (vty, "IP forwarding is on%s", VTY_NEWLINE);
2179 return CMD_SUCCESS;
2180}
2181
2182#ifdef HAVE_IPV6
2183/* Only display ipv6 forwarding is enabled or not. */
2184DEFUN (show_ipv6_forwarding,
2185 show_ipv6_forwarding_cmd,
2186 "show ipv6 forwarding",
2187 SHOW_STR
2188 "IPv6 information\n"
2189 "Forwarding status\n")
2190{
2191 int ret;
2192
2193 ret = ipforward_ipv6 ();
2194
2195 switch (ret)
2196 {
2197 case -1:
2198 vty_out (vty, "ipv6 forwarding is unknown%s", VTY_NEWLINE);
2199 break;
2200 case 0:
2201 vty_out (vty, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE);
2202 break;
2203 case 1:
2204 vty_out (vty, "ipv6 forwarding is %s%s", "on", VTY_NEWLINE);
2205 break;
2206 default:
2207 vty_out (vty, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE);
2208 break;
2209 }
2210 return CMD_SUCCESS;
2211}
2212
hasso55906722004-02-11 22:42:16 +00002213DEFUN (ipv6_forwarding,
2214 ipv6_forwarding_cmd,
2215 "ipv6 forwarding",
2216 IPV6_STR
2217 "Turn on IPv6 forwarding")
2218{
2219 int ret;
2220
hasso41d3fc92004-04-06 11:59:00 +00002221 ret = ipforward_ipv6 ();
hassob71f00f2004-10-13 12:20:35 +00002222 if (ret == 0)
2223 ret = ipforward_ipv6_on ();
hasso41d3fc92004-04-06 11:59:00 +00002224
hasso41d3fc92004-04-06 11:59:00 +00002225 if (ret == 0)
2226 {
hasso55906722004-02-11 22:42:16 +00002227 vty_out (vty, "Can't turn on IPv6 forwarding%s", VTY_NEWLINE);
2228 return CMD_WARNING;
2229 }
2230
2231 return CMD_SUCCESS;
2232}
2233
paul718e3742002-12-13 20:15:29 +00002234DEFUN (no_ipv6_forwarding,
2235 no_ipv6_forwarding_cmd,
2236 "no ipv6 forwarding",
2237 NO_STR
hasso55906722004-02-11 22:42:16 +00002238 IPV6_STR
2239 "Turn off IPv6 forwarding")
paul718e3742002-12-13 20:15:29 +00002240{
2241 int ret;
2242
hasso41d3fc92004-04-06 11:59:00 +00002243 ret = ipforward_ipv6 ();
hassob71f00f2004-10-13 12:20:35 +00002244 if (ret != 0)
2245 ret = ipforward_ipv6_off ();
hasso41d3fc92004-04-06 11:59:00 +00002246
paul718e3742002-12-13 20:15:29 +00002247 if (ret != 0)
2248 {
2249 vty_out (vty, "Can't turn off IPv6 forwarding%s", VTY_NEWLINE);
2250 return CMD_WARNING;
2251 }
2252
2253 return CMD_SUCCESS;
2254}
2255
2256#endif /* HAVE_IPV6 */
2257
2258/* IPForwarding configuration write function. */
ajs719e9742005-02-28 20:52:15 +00002259static int
paul718e3742002-12-13 20:15:29 +00002260config_write_forwarding (struct vty *vty)
2261{
hasso18a6dce2004-10-03 18:18:34 +00002262 /* FIXME: Find better place for that. */
2263 router_id_write (vty);
2264
paul3e0b3a52004-08-23 18:58:32 +00002265 if (ipforward ())
2266 vty_out (vty, "ip forwarding%s", VTY_NEWLINE);
paul718e3742002-12-13 20:15:29 +00002267#ifdef HAVE_IPV6
paul3e0b3a52004-08-23 18:58:32 +00002268 if (ipforward_ipv6 ())
2269 vty_out (vty, "ipv6 forwarding%s", VTY_NEWLINE);
paul718e3742002-12-13 20:15:29 +00002270#endif /* HAVE_IPV6 */
2271 vty_out (vty, "!%s", VTY_NEWLINE);
2272 return 0;
2273}
2274
2275/* table node for routing tables. */
Stephen Hemminger7fc626d2008-12-01 11:10:34 -08002276static struct cmd_node forwarding_node =
paul718e3742002-12-13 20:15:29 +00002277{
2278 FORWARDING_NODE,
2279 "", /* This node has no interface. */
2280 1
2281};
2282
Udaya Shankara KSd869dbd2016-02-11 21:42:29 +05302283#ifdef HAVE_FPM
2284/* function to write the fpm config info */
2285static int
2286config_write_fpm (struct vty *vty)
2287{
2288 return
2289 fpm_remote_srv_write (vty);
2290}
2291
2292/* Zebra node */
2293static struct cmd_node zebra_node =
2294{
2295 ZEBRA_NODE,
2296 "",
2297 1
2298};
2299#endif
2300
David Lamparter6b0655a2014-06-04 06:53:35 +02002301
paul718e3742002-12-13 20:15:29 +00002302/* Initialisation of zebra and installation of commands. */
2303void
paula1ac18c2005-06-28 17:17:12 +00002304zebra_init (void)
paul718e3742002-12-13 20:15:29 +00002305{
2306 /* Client list init. */
paulb21b19c2003-06-15 01:28:29 +00002307 zebrad.client_list = list_new ();
paul718e3742002-12-13 20:15:29 +00002308
paul718e3742002-12-13 20:15:29 +00002309 /* Install configuration write function. */
2310 install_node (&table_node, config_write_table);
2311 install_node (&forwarding_node, config_write_forwarding);
Udaya Shankara KSd869dbd2016-02-11 21:42:29 +05302312#ifdef HAVE_FPM
2313 install_node (&zebra_node, config_write_fpm);
2314#endif
paul718e3742002-12-13 20:15:29 +00002315
2316 install_element (VIEW_NODE, &show_ip_forwarding_cmd);
hasso647e4f12003-05-25 11:43:52 +00002317 install_element (CONFIG_NODE, &ip_forwarding_cmd);
paul718e3742002-12-13 20:15:29 +00002318 install_element (CONFIG_NODE, &no_ip_forwarding_cmd);
2319 install_element (ENABLE_NODE, &show_zebra_client_cmd);
Dinesh Dutt9ae85522015-05-19 17:47:22 -07002320 install_element (ENABLE_NODE, &show_zebra_client_summary_cmd);
paul718e3742002-12-13 20:15:29 +00002321
2322#ifdef HAVE_NETLINK
2323 install_element (VIEW_NODE, &show_table_cmd);
paul718e3742002-12-13 20:15:29 +00002324 install_element (CONFIG_NODE, &config_table_cmd);
2325#endif /* HAVE_NETLINK */
2326
2327#ifdef HAVE_IPV6
2328 install_element (VIEW_NODE, &show_ipv6_forwarding_cmd);
hasso55906722004-02-11 22:42:16 +00002329 install_element (CONFIG_NODE, &ipv6_forwarding_cmd);
paul718e3742002-12-13 20:15:29 +00002330 install_element (CONFIG_NODE, &no_ipv6_forwarding_cmd);
2331#endif /* HAVE_IPV6 */
Paul Jakma7514fb72007-05-02 16:05:35 +00002332
2333 /* Route-map */
2334 zebra_route_map_init ();
paul718e3742002-12-13 20:15:29 +00002335}
Denis Ovsienko97be79f2009-07-24 20:45:31 +04002336
2337/* Make zebra server socket, wiping any existing one (see bug #403). */
2338void
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +04002339zebra_zserv_socket_init (char *path)
Denis Ovsienko97be79f2009-07-24 20:45:31 +04002340{
2341#ifdef HAVE_TCP_ZEBRA
2342 zebra_serv ();
2343#else
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +04002344 zebra_serv_un (path ? path : ZEBRA_SERV_PATH);
Denis Ovsienko97be79f2009-07-24 20:45:31 +04002345#endif /* HAVE_TCP_ZEBRA */
2346}