blob: 4a8b55f9788dd0b15f96dbc107d644b47e385af8 [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;
751
752 if (IS_ZEBRA_DEBUG_NHT)
753 zlog_debug("nexthop_register msg from client %s: length=%d\n",
754 zebra_route_string(client->proto), length);
755
756 s = client->ibuf;
757
758 while (l < length)
759 {
760 p.family = stream_getw(s);
761 p.prefixlen = stream_getc(s);
762 l += 3;
763 stream_get(&p.u.prefix, s, PSIZE(p.prefixlen));
764 l += PSIZE(p.prefixlen);
765 rnh = zebra_add_rnh(&p, 0);
Dinesh Dutt9ae85522015-05-19 17:47:22 -0700766
767 client->nh_reg_time = quagga_time(NULL);
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -0500768 zebra_add_rnh_client(rnh, client, vrf_id);
769 }
770 zebra_evaluate_rnh_table(0, AF_INET);
771 zebra_evaluate_rnh_table(0, AF_INET6);
772 return 0;
773}
774
775/* Nexthop register */
776static int
777zserv_nexthop_unregister (struct zserv *client, int sock, u_short length)
778{
779 struct rnh *rnh;
780 struct stream *s;
781 struct prefix p;
782 u_short l = 0;
783
784 if (IS_ZEBRA_DEBUG_NHT)
785 zlog_debug("nexthop_unregister msg from client %s: length=%d\n",
786 zebra_route_string(client->proto), length);
787
788 s = client->ibuf;
789
790 while (l < length)
791 {
792 p.family = stream_getw(s);
793 p.prefixlen = stream_getc(s);
794 l += 3;
795 stream_get(&p.u.prefix, s, PSIZE(p.prefixlen));
796 l += PSIZE(p.prefixlen);
797 rnh = zebra_lookup_rnh(&p, 0);
798 if (rnh)
Dinesh Dutt9ae85522015-05-19 17:47:22 -0700799 {
800 client->nh_dereg_time = quagga_time(NULL);
801 zebra_remove_rnh_client(rnh, client);
802 }
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -0500803 }
804 return 0;
805}
806
paulb9df2d22004-05-09 09:09:59 +0000807static int
Feng Luc99f3482014-10-16 09:52:36 +0800808zsend_ipv4_import_lookup (struct zserv *client, struct prefix_ipv4 *p,
809 vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +0000810{
811 struct stream *s;
812 struct rib *rib;
813 unsigned long nump;
814 u_char num;
815 struct nexthop *nexthop;
816
817 /* Lookup nexthop. */
Feng Luc99f3482014-10-16 09:52:36 +0800818 rib = rib_lookup_ipv4 (p, vrf_id);
paul718e3742002-12-13 20:15:29 +0000819
820 /* Get output stream. */
821 s = client->obuf;
822 stream_reset (s);
823
824 /* Fill in result. */
Feng Luc99f3482014-10-16 09:52:36 +0800825 zserv_create_header (s, ZEBRA_IPV4_IMPORT_LOOKUP, vrf_id);
paul718e3742002-12-13 20:15:29 +0000826 stream_put_in_addr (s, &p->prefix);
827
828 if (rib)
829 {
830 stream_putl (s, rib->metric);
831 num = 0;
paul9985f832005-02-09 15:51:56 +0000832 nump = stream_get_endp(s);
paul718e3742002-12-13 20:15:29 +0000833 stream_putc (s, 0);
834 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
Timo Teräs325823a2016-01-15 17:36:31 +0200835 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE))
paul718e3742002-12-13 20:15:29 +0000836 {
837 stream_putc (s, nexthop->type);
838 switch (nexthop->type)
839 {
840 case ZEBRA_NEXTHOP_IPV4:
841 stream_put_in_addr (s, &nexthop->gate.ipv4);
842 break;
Christian Frankea12afd52013-05-25 14:01:36 +0000843 case ZEBRA_NEXTHOP_IPV4_IFINDEX:
844 stream_put_in_addr (s, &nexthop->gate.ipv4);
845 stream_putl (s, nexthop->ifindex);
846 break;
paul718e3742002-12-13 20:15:29 +0000847 case ZEBRA_NEXTHOP_IFINDEX:
848 case ZEBRA_NEXTHOP_IFNAME:
849 stream_putl (s, nexthop->ifindex);
850 break;
hassofa2b17e2004-03-04 17:45:00 +0000851 default:
852 /* do nothing */
853 break;
paul718e3742002-12-13 20:15:29 +0000854 }
855 num++;
856 }
857 stream_putc_at (s, nump, num);
858 }
859 else
860 {
861 stream_putl (s, 0);
862 stream_putc (s, 0);
863 }
864
865 stream_putw_at (s, 0, stream_get_endp (s));
Dinesh Dutt9ae85522015-05-19 17:47:22 -0700866
ajs719e9742005-02-28 20:52:15 +0000867 return zebra_server_send_message(client);
paul718e3742002-12-13 20:15:29 +0000868}
David Lamparter6b0655a2014-06-04 06:53:35 +0200869
hasso18a6dce2004-10-03 18:18:34 +0000870/* Router-id is updated. Send ZEBRA_ROUTER_ID_ADD to client. */
871int
Feng Luac19a442015-05-22 11:40:07 +0200872zsend_router_id_update (struct zserv *client, struct prefix *p,
873 vrf_id_t vrf_id)
hasso18a6dce2004-10-03 18:18:34 +0000874{
875 struct stream *s;
876 int blen;
877
878 /* Check this client need interface information. */
Feng Luc99f3482014-10-16 09:52:36 +0800879 if (! vrf_bitmap_check (client->ridinfo, vrf_id))
ajs719e9742005-02-28 20:52:15 +0000880 return 0;
hasso18a6dce2004-10-03 18:18:34 +0000881
882 s = client->obuf;
883 stream_reset (s);
884
hasso18a6dce2004-10-03 18:18:34 +0000885 /* Message type. */
Feng Luc99f3482014-10-16 09:52:36 +0800886 zserv_create_header (s, ZEBRA_ROUTER_ID_UPDATE, vrf_id);
hasso18a6dce2004-10-03 18:18:34 +0000887
888 /* Prefix information. */
889 stream_putc (s, p->family);
890 blen = prefix_blen (p);
891 stream_put (s, &p->u.prefix, blen);
892 stream_putc (s, p->prefixlen);
893
894 /* Write packet size. */
895 stream_putw_at (s, 0, stream_get_endp (s));
896
ajs719e9742005-02-28 20:52:15 +0000897 return zebra_server_send_message(client);
hasso18a6dce2004-10-03 18:18:34 +0000898}
David Lamparter6b0655a2014-06-04 06:53:35 +0200899
paul718e3742002-12-13 20:15:29 +0000900/* Register zebra server interface information. Send current all
901 interface and address information. */
ajs719e9742005-02-28 20:52:15 +0000902static int
Feng Luc99f3482014-10-16 09:52:36 +0800903zread_interface_add (struct zserv *client, u_short length, vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +0000904{
paul1eb8ef22005-04-07 07:30:20 +0000905 struct listnode *ifnode, *ifnnode;
906 struct listnode *cnode, *cnnode;
paul718e3742002-12-13 20:15:29 +0000907 struct interface *ifp;
908 struct connected *c;
909
910 /* Interface information is needed. */
Feng Luc99f3482014-10-16 09:52:36 +0800911 vrf_bitmap_set (client->ifinfo, vrf_id);
paul718e3742002-12-13 20:15:29 +0000912
Feng Luc99f3482014-10-16 09:52:36 +0800913 for (ALL_LIST_ELEMENTS (vrf_iflist (vrf_id), ifnode, ifnnode, ifp))
paul718e3742002-12-13 20:15:29 +0000914 {
paul718e3742002-12-13 20:15:29 +0000915 /* Skip pseudo interface. */
916 if (! CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE))
917 continue;
918
ajs719e9742005-02-28 20:52:15 +0000919 if (zsend_interface_add (client, ifp) < 0)
920 return -1;
paul718e3742002-12-13 20:15:29 +0000921
paul1eb8ef22005-04-07 07:30:20 +0000922 for (ALL_LIST_ELEMENTS (ifp->connected, cnode, cnnode, c))
paul718e3742002-12-13 20:15:29 +0000923 {
ajs719e9742005-02-28 20:52:15 +0000924 if (CHECK_FLAG (c->conf, ZEBRA_IFC_REAL) &&
925 (zsend_interface_address (ZEBRA_INTERFACE_ADDRESS_ADD, client,
926 ifp, c) < 0))
927 return -1;
paul718e3742002-12-13 20:15:29 +0000928 }
929 }
ajs719e9742005-02-28 20:52:15 +0000930 return 0;
paul718e3742002-12-13 20:15:29 +0000931}
932
933/* Unregister zebra server interface information. */
ajs719e9742005-02-28 20:52:15 +0000934static int
Feng Luc99f3482014-10-16 09:52:36 +0800935zread_interface_delete (struct zserv *client, u_short length, vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +0000936{
Feng Luc99f3482014-10-16 09:52:36 +0800937 vrf_bitmap_unset (client->ifinfo, vrf_id);
ajs719e9742005-02-28 20:52:15 +0000938 return 0;
paul718e3742002-12-13 20:15:29 +0000939}
940
941/* This function support multiple nexthop. */
paulb9df2d22004-05-09 09:09:59 +0000942/*
943 * Parse the ZEBRA_IPV4_ROUTE_ADD sent from client. Update rib and
944 * add kernel route.
945 */
ajs719e9742005-02-28 20:52:15 +0000946static int
Feng Luc99f3482014-10-16 09:52:36 +0800947zread_ipv4_add (struct zserv *client, u_short length, vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +0000948{
949 int i;
950 struct rib *rib;
951 struct prefix_ipv4 p;
952 u_char message;
953 struct in_addr nexthop;
954 u_char nexthop_num;
955 u_char nexthop_type;
956 struct stream *s;
Paul Jakma9099f9b2016-01-18 10:12:10 +0000957 ifindex_t ifindex;
paul718e3742002-12-13 20:15:29 +0000958 u_char ifname_len;
G.Balajicddf3912011-11-26 21:59:32 +0400959 safi_t safi;
Dinesh Dutt9ae85522015-05-19 17:47:22 -0700960 int ret;
paul718e3742002-12-13 20:15:29 +0000961
962 /* Get input stream. */
963 s = client->ibuf;
964
965 /* Allocate new rib. */
paul4d38fdb2005-04-28 17:35:14 +0000966 rib = XCALLOC (MTYPE_RIB, sizeof (struct rib));
967
paul718e3742002-12-13 20:15:29 +0000968 /* Type, flags, message. */
969 rib->type = stream_getc (s);
970 rib->flags = stream_getc (s);
paulb9df2d22004-05-09 09:09:59 +0000971 message = stream_getc (s);
G.Balajicddf3912011-11-26 21:59:32 +0400972 safi = stream_getw (s);
paul718e3742002-12-13 20:15:29 +0000973 rib->uptime = time (NULL);
974
975 /* IPv4 prefix. */
976 memset (&p, 0, sizeof (struct prefix_ipv4));
977 p.family = AF_INET;
978 p.prefixlen = stream_getc (s);
979 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
980
Feng Lu0d0686f2015-05-22 11:40:02 +0200981 /* VRF ID */
Feng Luc99f3482014-10-16 09:52:36 +0800982 rib->vrf_id = vrf_id;
Feng Lu0d0686f2015-05-22 11:40:02 +0200983
paul718e3742002-12-13 20:15:29 +0000984 /* Nexthop parse. */
985 if (CHECK_FLAG (message, ZAPI_MESSAGE_NEXTHOP))
986 {
987 nexthop_num = stream_getc (s);
988
989 for (i = 0; i < nexthop_num; i++)
990 {
991 nexthop_type = stream_getc (s);
992
993 switch (nexthop_type)
994 {
995 case ZEBRA_NEXTHOP_IFINDEX:
996 ifindex = stream_getl (s);
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -0500997 rib_nexthop_ifindex_add (rib, ifindex);
paul718e3742002-12-13 20:15:29 +0000998 break;
999 case ZEBRA_NEXTHOP_IFNAME:
1000 ifname_len = stream_getc (s);
paul9985f832005-02-09 15:51:56 +00001001 stream_forward_getp (s, ifname_len);
paul718e3742002-12-13 20:15:29 +00001002 break;
1003 case ZEBRA_NEXTHOP_IPV4:
1004 nexthop.s_addr = stream_get_ipv4 (s);
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -05001005 rib_nexthop_ipv4_add (rib, &nexthop, NULL);
paul718e3742002-12-13 20:15:29 +00001006 break;
Joakim Tjernlundc963c202012-07-07 17:06:13 +02001007 case ZEBRA_NEXTHOP_IPV4_IFINDEX:
1008 nexthop.s_addr = stream_get_ipv4 (s);
1009 ifindex = stream_getl (s);
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -05001010 rib_nexthop_ipv4_ifindex_add (rib, &nexthop, NULL, ifindex);
Joakim Tjernlundc963c202012-07-07 17:06:13 +02001011 break;
paul718e3742002-12-13 20:15:29 +00001012 case ZEBRA_NEXTHOP_IPV6:
paul9985f832005-02-09 15:51:56 +00001013 stream_forward_getp (s, IPV6_MAX_BYTELEN);
paul718e3742002-12-13 20:15:29 +00001014 break;
Subbaiah Venkata6902c692012-03-27 19:21:29 -07001015 case ZEBRA_NEXTHOP_BLACKHOLE:
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -05001016 rib_nexthop_blackhole_add (rib);
Subbaiah Venkata6902c692012-03-27 19:21:29 -07001017 break;
1018 }
paul718e3742002-12-13 20:15:29 +00001019 }
1020 }
1021
1022 /* Distance. */
1023 if (CHECK_FLAG (message, ZAPI_MESSAGE_DISTANCE))
1024 rib->distance = stream_getc (s);
1025
1026 /* Metric. */
1027 if (CHECK_FLAG (message, ZAPI_MESSAGE_METRIC))
1028 rib->metric = stream_getl (s);
1029
Timo Teräsb11f3b52015-11-02 16:50:07 +02001030 if (CHECK_FLAG (message, ZAPI_MESSAGE_MTU))
1031 rib->mtu = stream_getl (s);
Piotr Chytłaeefddcc2015-12-01 09:48:02 -05001032 /* Tag */
1033 if (CHECK_FLAG (message, ZAPI_MESSAGE_TAG))
Paul Jakma96d10602016-07-01 14:23:45 +01001034 rib->tag = stream_getl (s);
Timo Teräsb11f3b52015-11-02 16:50:07 +02001035
Paul Jakma171eee32006-07-27 16:11:02 +00001036 /* Table */
1037 rib->table=zebrad.rtm_table_default;
Dinesh Dutt9ae85522015-05-19 17:47:22 -07001038 ret = rib_add_ipv4_multipath (&p, rib, safi);
1039
1040 /* Stats */
1041 if (ret > 0)
1042 client->v4_route_add_cnt++;
1043 else if (ret < 0)
1044 client->v4_route_upd8_cnt++;
ajs719e9742005-02-28 20:52:15 +00001045 return 0;
paul718e3742002-12-13 20:15:29 +00001046}
1047
1048/* Zebra server IPv4 prefix delete function. */
ajs719e9742005-02-28 20:52:15 +00001049static int
Feng Luc99f3482014-10-16 09:52:36 +08001050zread_ipv4_delete (struct zserv *client, u_short length, vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +00001051{
1052 int i;
1053 struct stream *s;
1054 struct zapi_ipv4 api;
Subbaiah Venkata6902c692012-03-27 19:21:29 -07001055 struct in_addr nexthop, *nexthop_p;
paul718e3742002-12-13 20:15:29 +00001056 unsigned long ifindex;
1057 struct prefix_ipv4 p;
1058 u_char nexthop_num;
1059 u_char nexthop_type;
1060 u_char ifname_len;
1061
1062 s = client->ibuf;
1063 ifindex = 0;
1064 nexthop.s_addr = 0;
Subbaiah Venkata6902c692012-03-27 19:21:29 -07001065 nexthop_p = NULL;
paul718e3742002-12-13 20:15:29 +00001066
1067 /* Type, flags, message. */
1068 api.type = stream_getc (s);
1069 api.flags = stream_getc (s);
1070 api.message = stream_getc (s);
G.Balajicddf3912011-11-26 21:59:32 +04001071 api.safi = stream_getw (s);
paul718e3742002-12-13 20:15:29 +00001072
1073 /* IPv4 prefix. */
1074 memset (&p, 0, sizeof (struct prefix_ipv4));
1075 p.family = AF_INET;
1076 p.prefixlen = stream_getc (s);
1077 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
1078
1079 /* Nexthop, ifindex, distance, metric. */
1080 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
1081 {
1082 nexthop_num = stream_getc (s);
1083
1084 for (i = 0; i < nexthop_num; i++)
1085 {
1086 nexthop_type = stream_getc (s);
1087
1088 switch (nexthop_type)
1089 {
1090 case ZEBRA_NEXTHOP_IFINDEX:
1091 ifindex = stream_getl (s);
1092 break;
1093 case ZEBRA_NEXTHOP_IFNAME:
1094 ifname_len = stream_getc (s);
paul9985f832005-02-09 15:51:56 +00001095 stream_forward_getp (s, ifname_len);
paul718e3742002-12-13 20:15:29 +00001096 break;
1097 case ZEBRA_NEXTHOP_IPV4:
1098 nexthop.s_addr = stream_get_ipv4 (s);
Subbaiah Venkata6902c692012-03-27 19:21:29 -07001099 nexthop_p = &nexthop;
paul718e3742002-12-13 20:15:29 +00001100 break;
Joakim Tjernlundc963c202012-07-07 17:06:13 +02001101 case ZEBRA_NEXTHOP_IPV4_IFINDEX:
1102 nexthop.s_addr = stream_get_ipv4 (s);
Christian Franke23f5f7c2013-11-27 17:06:14 +00001103 nexthop_p = &nexthop;
Joakim Tjernlundc963c202012-07-07 17:06:13 +02001104 ifindex = stream_getl (s);
1105 break;
paul718e3742002-12-13 20:15:29 +00001106 case ZEBRA_NEXTHOP_IPV6:
paul9985f832005-02-09 15:51:56 +00001107 stream_forward_getp (s, IPV6_MAX_BYTELEN);
paul718e3742002-12-13 20:15:29 +00001108 break;
1109 }
1110 }
1111 }
1112
1113 /* Distance. */
1114 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
1115 api.distance = stream_getc (s);
1116 else
1117 api.distance = 0;
1118
1119 /* Metric. */
1120 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
1121 api.metric = stream_getl (s);
1122 else
1123 api.metric = 0;
1124
Piotr Chytłaeefddcc2015-12-01 09:48:02 -05001125 /* tag */
1126 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_TAG))
Paul Jakma96d10602016-07-01 14:23:45 +01001127 api.tag = stream_getl (s);
Piotr Chytłaeefddcc2015-12-01 09:48:02 -05001128 else
1129 api.tag = 0;
1130
Subbaiah Venkata6902c692012-03-27 19:21:29 -07001131 rib_delete_ipv4 (api.type, api.flags, &p, nexthop_p, ifindex,
Feng Luc99f3482014-10-16 09:52:36 +08001132 vrf_id, api.safi);
Dinesh Dutt9ae85522015-05-19 17:47:22 -07001133 client->v4_route_del_cnt++;
ajs719e9742005-02-28 20:52:15 +00001134 return 0;
paul718e3742002-12-13 20:15:29 +00001135}
1136
1137/* Nexthop lookup for IPv4. */
ajs719e9742005-02-28 20:52:15 +00001138static int
Feng Luc99f3482014-10-16 09:52:36 +08001139zread_ipv4_nexthop_lookup (struct zserv *client, u_short length,
1140 vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +00001141{
1142 struct in_addr addr;
Christian Frankebb97e462013-05-25 14:01:35 +00001143 char buf[BUFSIZ];
paul718e3742002-12-13 20:15:29 +00001144
1145 addr.s_addr = stream_get_ipv4 (client->ibuf);
Christian Frankebb97e462013-05-25 14:01:35 +00001146 if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
1147 zlog_debug("%s: looking up %s", __func__,
1148 inet_ntop (AF_INET, &addr, buf, BUFSIZ));
Feng Luc99f3482014-10-16 09:52:36 +08001149 return zsend_ipv4_nexthop_lookup (client, addr, vrf_id);
paul718e3742002-12-13 20:15:29 +00001150}
1151
Everton Marques4e5275b2014-07-01 15:15:52 -03001152/* MRIB Nexthop lookup for IPv4. */
1153static int
Feng Luc99f3482014-10-16 09:52:36 +08001154zread_ipv4_nexthop_lookup_mrib (struct zserv *client, u_short length,
1155 vrf_id_t vrf_id)
Everton Marques4e5275b2014-07-01 15:15:52 -03001156{
1157 struct in_addr addr;
David Lamparterbd078122015-01-06 19:53:24 +01001158 struct rib *rib;
Everton Marques4e5275b2014-07-01 15:15:52 -03001159
1160 addr.s_addr = stream_get_ipv4 (client->ibuf);
Feng Luc99f3482014-10-16 09:52:36 +08001161 rib = rib_match_ipv4_multicast (addr, NULL, vrf_id);
David Lamparterbd078122015-01-06 19:53:24 +01001162 return zsend_ipv4_nexthop_lookup_mrib (client, addr, rib);
Everton Marques4e5275b2014-07-01 15:15:52 -03001163}
1164
paul718e3742002-12-13 20:15:29 +00001165/* Nexthop lookup for IPv4. */
ajs719e9742005-02-28 20:52:15 +00001166static int
Feng Luc99f3482014-10-16 09:52:36 +08001167zread_ipv4_import_lookup (struct zserv *client, u_short length,
1168 vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +00001169{
1170 struct prefix_ipv4 p;
1171
1172 p.family = AF_INET;
1173 p.prefixlen = stream_getc (client->ibuf);
1174 p.prefix.s_addr = stream_get_ipv4 (client->ibuf);
1175
Feng Luc99f3482014-10-16 09:52:36 +08001176 return zsend_ipv4_import_lookup (client, &p, vrf_id);
paul718e3742002-12-13 20:15:29 +00001177}
1178
1179#ifdef HAVE_IPV6
1180/* Zebra server IPv6 prefix add function. */
ajs719e9742005-02-28 20:52:15 +00001181static int
Feng Luc99f3482014-10-16 09:52:36 +08001182zread_ipv6_add (struct zserv *client, u_short length, vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +00001183{
1184 int i;
1185 struct stream *s;
paul718e3742002-12-13 20:15:29 +00001186 struct in6_addr nexthop;
Ayan Banerjee34c5d892015-11-09 20:14:53 -05001187 struct rib *rib;
1188 u_char message;
1189 u_char gateway_num;
1190 u_char nexthop_type;
paul718e3742002-12-13 20:15:29 +00001191 struct prefix_ipv6 p;
Ayan Banerjee34c5d892015-11-09 20:14:53 -05001192 safi_t safi;
1193 static struct in6_addr nexthops[MULTIPATH_NUM];
1194 static unsigned int ifindices[MULTIPATH_NUM];
Dinesh Dutt9ae85522015-05-19 17:47:22 -07001195 int ret;
Ayan Banerjee34c5d892015-11-09 20:14:53 -05001196
1197 /* Get input stream. */
paul718e3742002-12-13 20:15:29 +00001198 s = client->ibuf;
Ayan Banerjee34c5d892015-11-09 20:14:53 -05001199
paul718e3742002-12-13 20:15:29 +00001200 memset (&nexthop, 0, sizeof (struct in6_addr));
1201
Ayan Banerjee34c5d892015-11-09 20:14:53 -05001202 /* Allocate new rib. */
1203 rib = XCALLOC (MTYPE_RIB, sizeof (struct rib));
paul718e3742002-12-13 20:15:29 +00001204
Ayan Banerjee34c5d892015-11-09 20:14:53 -05001205 /* Type, flags, message. */
1206 rib->type = stream_getc (s);
1207 rib->flags = stream_getc (s);
1208 message = stream_getc (s);
1209 safi = stream_getw (s);
1210 rib->uptime = time (NULL);
1211
1212 /* IPv6 prefix. */
paul718e3742002-12-13 20:15:29 +00001213 memset (&p, 0, sizeof (struct prefix_ipv6));
1214 p.family = AF_INET6;
1215 p.prefixlen = stream_getc (s);
1216 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
1217
Ayan Banerjee34c5d892015-11-09 20:14:53 -05001218 /* We need to give nh-addr, nh-ifindex with the same next-hop object
1219 * to the rib to ensure that IPv6 multipathing works; need to coalesce
1220 * these. Clients should send the same number of paired set of
1221 * next-hop-addr/next-hop-ifindices. */
1222 if (CHECK_FLAG (message, ZAPI_MESSAGE_NEXTHOP))
paul718e3742002-12-13 20:15:29 +00001223 {
Ayan Banerjee34c5d892015-11-09 20:14:53 -05001224 int nh_count = 0;
1225 int if_count = 0;
1226 int max_nh_if = 0;
1227 unsigned int ifindex;
paul718e3742002-12-13 20:15:29 +00001228
Ayan Banerjee34c5d892015-11-09 20:14:53 -05001229 gateway_num = stream_getc (s);
1230 for (i = 0; i < gateway_num; i++)
paul718e3742002-12-13 20:15:29 +00001231 {
1232 nexthop_type = stream_getc (s);
1233
1234 switch (nexthop_type)
1235 {
1236 case ZEBRA_NEXTHOP_IPV6:
1237 stream_get (&nexthop, s, 16);
Ayan Banerjee34c5d892015-11-09 20:14:53 -05001238 if (nh_count < MULTIPATH_NUM) {
1239 nexthops[nh_count++] = nexthop;
1240 }
paul718e3742002-12-13 20:15:29 +00001241 break;
1242 case ZEBRA_NEXTHOP_IFINDEX:
1243 ifindex = stream_getl (s);
Ayan Banerjee34c5d892015-11-09 20:14:53 -05001244 if (if_count < MULTIPATH_NUM) {
1245 ifindices[if_count++] = ifindex;
1246 }
paul718e3742002-12-13 20:15:29 +00001247 break;
1248 }
1249 }
Ayan Banerjee34c5d892015-11-09 20:14:53 -05001250
1251 max_nh_if = (nh_count > if_count) ? nh_count : if_count;
1252 for (i = 0; i < max_nh_if; i++)
1253 {
1254 if ((i < nh_count) && !IN6_IS_ADDR_UNSPECIFIED (&nexthops[i]))
1255 {
1256 if ((i < if_count) && ifindices[i])
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -05001257 rib_nexthop_ipv6_ifindex_add (rib, &nexthops[i], ifindices[i]);
Ayan Banerjee34c5d892015-11-09 20:14:53 -05001258 else
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -05001259 rib_nexthop_ipv6_add (rib, &nexthops[i]);
Ayan Banerjee34c5d892015-11-09 20:14:53 -05001260 }
1261 else
1262 {
1263 if ((i < if_count) && ifindices[i])
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -05001264 rib_nexthop_ifindex_add (rib, ifindices[i]);
Ayan Banerjee34c5d892015-11-09 20:14:53 -05001265 }
1266 }
paul718e3742002-12-13 20:15:29 +00001267 }
1268
Ayan Banerjee34c5d892015-11-09 20:14:53 -05001269 /* Distance. */
1270 if (CHECK_FLAG (message, ZAPI_MESSAGE_DISTANCE))
1271 rib->distance = stream_getc (s);
paul718e3742002-12-13 20:15:29 +00001272
Ayan Banerjee34c5d892015-11-09 20:14:53 -05001273 /* Metric. */
1274 if (CHECK_FLAG (message, ZAPI_MESSAGE_METRIC))
1275 rib->metric = stream_getl (s);
Timo Teräsb11f3b52015-11-02 16:50:07 +02001276
Ayan Banerjee34c5d892015-11-09 20:14:53 -05001277 if (CHECK_FLAG (message, ZAPI_MESSAGE_MTU))
1278 rib->mtu = stream_getl (s);
1279
Piotr Chytłaeefddcc2015-12-01 09:48:02 -05001280 /* Tag */
1281 if (CHECK_FLAG (message, ZAPI_MESSAGE_TAG))
Paul Jakma96d10602016-07-01 14:23:45 +01001282 rib->tag = stream_getl (s);
Piotr Chytłaeefddcc2015-12-01 09:48:02 -05001283
Ayan Banerjee34c5d892015-11-09 20:14:53 -05001284 /* Table */
1285 rib->table=zebrad.rtm_table_default;
Dinesh Dutt9ae85522015-05-19 17:47:22 -07001286 ret = rib_add_ipv6_multipath (&p, rib, safi);
1287 /* Stats */
1288 if (ret > 0)
1289 client->v6_route_add_cnt++;
1290 else if (ret < 0)
1291 client->v6_route_upd8_cnt++;
1292
ajs719e9742005-02-28 20:52:15 +00001293 return 0;
paul718e3742002-12-13 20:15:29 +00001294}
1295
1296/* Zebra server IPv6 prefix delete function. */
ajs719e9742005-02-28 20:52:15 +00001297static int
Feng Luc99f3482014-10-16 09:52:36 +08001298zread_ipv6_delete (struct zserv *client, u_short length, vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +00001299{
1300 int i;
1301 struct stream *s;
1302 struct zapi_ipv6 api;
1303 struct in6_addr nexthop;
1304 unsigned long ifindex;
1305 struct prefix_ipv6 p;
1306
1307 s = client->ibuf;
1308 ifindex = 0;
1309 memset (&nexthop, 0, sizeof (struct in6_addr));
1310
1311 /* Type, flags, message. */
1312 api.type = stream_getc (s);
1313 api.flags = stream_getc (s);
1314 api.message = stream_getc (s);
G.Balajif768f362011-11-26 22:10:39 +04001315 api.safi = stream_getw (s);
paul718e3742002-12-13 20:15:29 +00001316
1317 /* IPv4 prefix. */
1318 memset (&p, 0, sizeof (struct prefix_ipv6));
1319 p.family = AF_INET6;
1320 p.prefixlen = stream_getc (s);
1321 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
1322
1323 /* Nexthop, ifindex, distance, metric. */
1324 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
1325 {
1326 u_char nexthop_type;
1327
1328 api.nexthop_num = stream_getc (s);
1329 for (i = 0; i < api.nexthop_num; i++)
1330 {
1331 nexthop_type = stream_getc (s);
1332
1333 switch (nexthop_type)
1334 {
1335 case ZEBRA_NEXTHOP_IPV6:
1336 stream_get (&nexthop, s, 16);
1337 break;
1338 case ZEBRA_NEXTHOP_IFINDEX:
1339 ifindex = stream_getl (s);
1340 break;
1341 }
1342 }
1343 }
1344
Piotr Chytłaeefddcc2015-12-01 09:48:02 -05001345 /* Distance. */
paul718e3742002-12-13 20:15:29 +00001346 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
1347 api.distance = stream_getc (s);
1348 else
1349 api.distance = 0;
Piotr Chytłaeefddcc2015-12-01 09:48:02 -05001350
1351 /* Metric. */
paul718e3742002-12-13 20:15:29 +00001352 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
1353 api.metric = stream_getl (s);
1354 else
1355 api.metric = 0;
1356
Piotr Chytłaeefddcc2015-12-01 09:48:02 -05001357 /* tag */
1358 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_TAG))
Paul Jakma96d10602016-07-01 14:23:45 +01001359 api.tag = stream_getl (s);
Piotr Chytłaeefddcc2015-12-01 09:48:02 -05001360 else
1361 api.tag = 0;
1362
paul718e3742002-12-13 20:15:29 +00001363 if (IN6_IS_ADDR_UNSPECIFIED (&nexthop))
Feng Luc99f3482014-10-16 09:52:36 +08001364 rib_delete_ipv6 (api.type, api.flags, &p, NULL, ifindex, vrf_id,
Feng Lu0d0686f2015-05-22 11:40:02 +02001365 api.safi);
paul718e3742002-12-13 20:15:29 +00001366 else
Feng Luc99f3482014-10-16 09:52:36 +08001367 rib_delete_ipv6 (api.type, api.flags, &p, &nexthop, ifindex, vrf_id,
Feng Lu0d0686f2015-05-22 11:40:02 +02001368 api.safi);
Dinesh Dutt9ae85522015-05-19 17:47:22 -07001369
1370 client->v6_route_del_cnt++;
ajs719e9742005-02-28 20:52:15 +00001371 return 0;
paul718e3742002-12-13 20:15:29 +00001372}
1373
ajs719e9742005-02-28 20:52:15 +00001374static int
Feng Luc99f3482014-10-16 09:52:36 +08001375zread_ipv6_nexthop_lookup (struct zserv *client, u_short length,
1376 vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +00001377{
1378 struct in6_addr addr;
1379 char buf[BUFSIZ];
1380
1381 stream_get (&addr, client->ibuf, 16);
Christian Frankea5207082013-04-11 08:24:29 +00001382 if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
1383 zlog_debug("%s: looking up %s", __func__,
1384 inet_ntop (AF_INET6, &addr, buf, BUFSIZ));
paul718e3742002-12-13 20:15:29 +00001385
Feng Luc99f3482014-10-16 09:52:36 +08001386 return zsend_ipv6_nexthop_lookup (client, &addr, vrf_id);
paul718e3742002-12-13 20:15:29 +00001387}
1388#endif /* HAVE_IPV6 */
1389
hasso18a6dce2004-10-03 18:18:34 +00001390/* Register zebra server router-id information. Send current router-id */
ajs719e9742005-02-28 20:52:15 +00001391static int
Feng Luc99f3482014-10-16 09:52:36 +08001392zread_router_id_add (struct zserv *client, u_short length, vrf_id_t vrf_id)
hasso18a6dce2004-10-03 18:18:34 +00001393{
1394 struct prefix p;
1395
1396 /* Router-id information is needed. */
Feng Luc99f3482014-10-16 09:52:36 +08001397 vrf_bitmap_set (client->ridinfo, vrf_id);
hasso18a6dce2004-10-03 18:18:34 +00001398
Feng Luc99f3482014-10-16 09:52:36 +08001399 router_id_get (&p, vrf_id);
hasso18a6dce2004-10-03 18:18:34 +00001400
Feng Luc99f3482014-10-16 09:52:36 +08001401 return zsend_router_id_update (client, &p, vrf_id);
hasso18a6dce2004-10-03 18:18:34 +00001402}
1403
1404/* Unregister zebra server router-id information. */
ajs719e9742005-02-28 20:52:15 +00001405static int
Feng Luc99f3482014-10-16 09:52:36 +08001406zread_router_id_delete (struct zserv *client, u_short length, vrf_id_t vrf_id)
hasso18a6dce2004-10-03 18:18:34 +00001407{
Feng Luc99f3482014-10-16 09:52:36 +08001408 vrf_bitmap_unset (client->ridinfo, vrf_id);
ajs719e9742005-02-28 20:52:15 +00001409 return 0;
hasso18a6dce2004-10-03 18:18:34 +00001410}
1411
Vyacheslav Trushkin2ea1ab12011-12-11 18:48:47 +04001412/* Tie up route-type and client->sock */
1413static void
1414zread_hello (struct zserv *client)
1415{
1416 /* type of protocol (lib/zebra.h) */
1417 u_char proto;
1418 proto = stream_getc (client->ibuf);
1419
1420 /* accept only dynamic routing protocols */
1421 if ((proto < ZEBRA_ROUTE_MAX)
1422 && (proto > ZEBRA_ROUTE_STATIC))
1423 {
1424 zlog_notice ("client %d says hello and bids fair to announce only %s routes",
1425 client->sock, zebra_route_string(proto));
1426
1427 /* if route-type was binded by other client */
1428 if (route_type_oaths[proto])
1429 zlog_warn ("sender of %s routes changed %c->%c",
1430 zebra_route_string(proto), route_type_oaths[proto],
1431 client->sock);
1432
1433 route_type_oaths[proto] = client->sock;
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -05001434 client->proto = proto;
Vyacheslav Trushkin2ea1ab12011-12-11 18:48:47 +04001435 }
1436}
1437
Feng Luc99f3482014-10-16 09:52:36 +08001438/* Unregister all information in a VRF. */
1439static int
1440zread_vrf_unregister (struct zserv *client, u_short length, vrf_id_t vrf_id)
1441{
1442 int i;
1443
1444 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
1445 vrf_bitmap_unset (client->redist[i], vrf_id);
1446 vrf_bitmap_unset (client->redist_default, vrf_id);
1447 vrf_bitmap_unset (client->ifinfo, vrf_id);
1448 vrf_bitmap_unset (client->ridinfo, vrf_id);
1449
1450 return 0;
1451}
1452
Vyacheslav Trushkin2ea1ab12011-12-11 18:48:47 +04001453/* If client sent routes of specific type, zebra removes it
1454 * and returns number of deleted routes.
1455 */
1456static void
1457zebra_score_rib (int client_sock)
1458{
1459 int i;
1460
1461 for (i = ZEBRA_ROUTE_RIP; i < ZEBRA_ROUTE_MAX; i++)
1462 if (client_sock == route_type_oaths[i])
1463 {
1464 zlog_notice ("client %d disconnected. %lu %s routes removed from the rib",
1465 client_sock, rib_score_proto (i), zebra_route_string (i));
1466 route_type_oaths[i] = 0;
1467 break;
1468 }
1469}
1470
paul718e3742002-12-13 20:15:29 +00001471/* Close zebra client. */
paulb9df2d22004-05-09 09:09:59 +00001472static void
paul718e3742002-12-13 20:15:29 +00001473zebra_client_close (struct zserv *client)
1474{
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -05001475 zebra_cleanup_rnh_client(0, AF_INET, client);
1476 zebra_cleanup_rnh_client(0, AF_INET6, client);
1477
paul718e3742002-12-13 20:15:29 +00001478 /* Close file descriptor. */
1479 if (client->sock)
1480 {
1481 close (client->sock);
Vyacheslav Trushkin2ea1ab12011-12-11 18:48:47 +04001482 zebra_score_rib (client->sock);
paul718e3742002-12-13 20:15:29 +00001483 client->sock = -1;
1484 }
1485
1486 /* Free stream buffers. */
1487 if (client->ibuf)
1488 stream_free (client->ibuf);
1489 if (client->obuf)
1490 stream_free (client->obuf);
ajs719e9742005-02-28 20:52:15 +00001491 if (client->wb)
1492 buffer_free(client->wb);
paul718e3742002-12-13 20:15:29 +00001493
1494 /* Release threads. */
1495 if (client->t_read)
1496 thread_cancel (client->t_read);
1497 if (client->t_write)
1498 thread_cancel (client->t_write);
ajs719e9742005-02-28 20:52:15 +00001499 if (client->t_suicide)
1500 thread_cancel (client->t_suicide);
paul718e3742002-12-13 20:15:29 +00001501
1502 /* Free client structure. */
paulb21b19c2003-06-15 01:28:29 +00001503 listnode_delete (zebrad.client_list, client);
paul718e3742002-12-13 20:15:29 +00001504 XFREE (0, client);
1505}
1506
1507/* Make new client. */
paulb9df2d22004-05-09 09:09:59 +00001508static void
paul718e3742002-12-13 20:15:29 +00001509zebra_client_create (int sock)
1510{
1511 struct zserv *client;
Feng Luc99f3482014-10-16 09:52:36 +08001512 int i;
paul718e3742002-12-13 20:15:29 +00001513
David Lamparter23757db2016-02-24 06:26:02 +01001514 client = XCALLOC (MTYPE_TMP, sizeof (struct zserv));
paul718e3742002-12-13 20:15:29 +00001515
1516 /* Make client input/output buffer. */
1517 client->sock = sock;
1518 client->ibuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
1519 client->obuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
ajs719e9742005-02-28 20:52:15 +00001520 client->wb = buffer_new(0);
paul718e3742002-12-13 20:15:29 +00001521
1522 /* Set table number. */
paulb21b19c2003-06-15 01:28:29 +00001523 client->rtm_table = zebrad.rtm_table_default;
paul718e3742002-12-13 20:15:29 +00001524
Feng Luc99f3482014-10-16 09:52:36 +08001525 /* Initialize flags */
1526 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
1527 client->redist[i] = vrf_bitmap_init ();
1528 client->redist_default = vrf_bitmap_init ();
1529 client->ifinfo = vrf_bitmap_init ();
1530 client->ridinfo = vrf_bitmap_init ();
Dinesh Dutt9ae85522015-05-19 17:47:22 -07001531 client->connect_time = quagga_time(NULL);
Feng Luc99f3482014-10-16 09:52:36 +08001532
paul718e3742002-12-13 20:15:29 +00001533 /* Add this client to linked list. */
paulb21b19c2003-06-15 01:28:29 +00001534 listnode_add (zebrad.client_list, client);
paul718e3742002-12-13 20:15:29 +00001535
1536 /* Make new read thread. */
1537 zebra_event (ZEBRA_READ, sock, client);
1538}
1539
1540/* Handler of zebra service request. */
paulb9df2d22004-05-09 09:09:59 +00001541static int
paul718e3742002-12-13 20:15:29 +00001542zebra_client_read (struct thread *thread)
1543{
1544 int sock;
1545 struct zserv *client;
ajs57a14772005-04-10 15:01:56 +00001546 size_t already;
paulc1b98002006-01-16 01:54:02 +00001547 uint16_t length, command;
1548 uint8_t marker, version;
Feng Luc99f3482014-10-16 09:52:36 +08001549 vrf_id_t vrf_id;
paul718e3742002-12-13 20:15:29 +00001550
1551 /* Get thread data. Reset reading thread because I'm running. */
1552 sock = THREAD_FD (thread);
1553 client = THREAD_ARG (thread);
1554 client->t_read = NULL;
1555
ajs719e9742005-02-28 20:52:15 +00001556 if (client->t_suicide)
paul718e3742002-12-13 20:15:29 +00001557 {
ajs719e9742005-02-28 20:52:15 +00001558 zebra_client_close(client);
paul718e3742002-12-13 20:15:29 +00001559 return -1;
1560 }
ajs719e9742005-02-28 20:52:15 +00001561
1562 /* Read length and command (if we don't have it already). */
ajs57a14772005-04-10 15:01:56 +00001563 if ((already = stream_get_endp(client->ibuf)) < ZEBRA_HEADER_SIZE)
ajs719e9742005-02-28 20:52:15 +00001564 {
ajs57a14772005-04-10 15:01:56 +00001565 ssize_t nbyte;
ajs719e9742005-02-28 20:52:15 +00001566 if (((nbyte = stream_read_try (client->ibuf, sock,
ajs57a14772005-04-10 15:01:56 +00001567 ZEBRA_HEADER_SIZE-already)) == 0) ||
ajs719e9742005-02-28 20:52:15 +00001568 (nbyte == -1))
1569 {
1570 if (IS_ZEBRA_DEBUG_EVENT)
1571 zlog_debug ("connection closed socket [%d]", sock);
1572 zebra_client_close (client);
1573 return -1;
1574 }
ajs57a14772005-04-10 15:01:56 +00001575 if (nbyte != (ssize_t)(ZEBRA_HEADER_SIZE-already))
ajs719e9742005-02-28 20:52:15 +00001576 {
1577 /* Try again later. */
1578 zebra_event (ZEBRA_READ, sock, client);
1579 return 0;
1580 }
ajs57a14772005-04-10 15:01:56 +00001581 already = ZEBRA_HEADER_SIZE;
ajs719e9742005-02-28 20:52:15 +00001582 }
1583
1584 /* Reset to read from the beginning of the incoming packet. */
1585 stream_set_getp(client->ibuf, 0);
1586
paulc1b98002006-01-16 01:54:02 +00001587 /* Fetch header values */
paul718e3742002-12-13 20:15:29 +00001588 length = stream_getw (client->ibuf);
paulc1b98002006-01-16 01:54:02 +00001589 marker = stream_getc (client->ibuf);
1590 version = stream_getc (client->ibuf);
Feng Luc99f3482014-10-16 09:52:36 +08001591 vrf_id = stream_getw (client->ibuf);
paulc1b98002006-01-16 01:54:02 +00001592 command = stream_getw (client->ibuf);
paul718e3742002-12-13 20:15:29 +00001593
paulc1b98002006-01-16 01:54:02 +00001594 if (marker != ZEBRA_HEADER_MARKER || version != ZSERV_VERSION)
1595 {
1596 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
1597 __func__, sock, marker, version);
1598 zebra_client_close (client);
1599 return -1;
1600 }
ajs719e9742005-02-28 20:52:15 +00001601 if (length < ZEBRA_HEADER_SIZE)
paul718e3742002-12-13 20:15:29 +00001602 {
ajs57a14772005-04-10 15:01:56 +00001603 zlog_warn("%s: socket %d message length %u is less than header size %d",
1604 __func__, sock, length, ZEBRA_HEADER_SIZE);
1605 zebra_client_close (client);
1606 return -1;
1607 }
1608 if (length > STREAM_SIZE(client->ibuf))
1609 {
1610 zlog_warn("%s: socket %d message length %u exceeds buffer size %lu",
1611 __func__, sock, length, (u_long)STREAM_SIZE(client->ibuf));
paul718e3742002-12-13 20:15:29 +00001612 zebra_client_close (client);
1613 return -1;
1614 }
1615
paul718e3742002-12-13 20:15:29 +00001616 /* Read rest of data. */
ajs57a14772005-04-10 15:01:56 +00001617 if (already < length)
paul718e3742002-12-13 20:15:29 +00001618 {
ajs57a14772005-04-10 15:01:56 +00001619 ssize_t nbyte;
1620 if (((nbyte = stream_read_try (client->ibuf, sock,
1621 length-already)) == 0) ||
1622 (nbyte == -1))
paul718e3742002-12-13 20:15:29 +00001623 {
1624 if (IS_ZEBRA_DEBUG_EVENT)
ajsb6178002004-12-07 21:12:56 +00001625 zlog_debug ("connection closed [%d] when reading zebra data", sock);
paul718e3742002-12-13 20:15:29 +00001626 zebra_client_close (client);
1627 return -1;
1628 }
ajs57a14772005-04-10 15:01:56 +00001629 if (nbyte != (ssize_t)(length-already))
ajs719e9742005-02-28 20:52:15 +00001630 {
1631 /* Try again later. */
1632 zebra_event (ZEBRA_READ, sock, client);
1633 return 0;
1634 }
paul718e3742002-12-13 20:15:29 +00001635 }
1636
ajs719e9742005-02-28 20:52:15 +00001637 length -= ZEBRA_HEADER_SIZE;
1638
paul718e3742002-12-13 20:15:29 +00001639 /* Debug packet information. */
1640 if (IS_ZEBRA_DEBUG_EVENT)
ajsb6178002004-12-07 21:12:56 +00001641 zlog_debug ("zebra message comes from socket [%d]", sock);
paul718e3742002-12-13 20:15:29 +00001642
1643 if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
Feng Luc99f3482014-10-16 09:52:36 +08001644 zlog_debug ("zebra message received [%s] %d in VRF %u",
1645 zserv_command_string (command), length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001646
Dinesh Dutt9ae85522015-05-19 17:47:22 -07001647 client->last_read_time = quagga_time(NULL);
1648 client->last_read_cmd = command;
1649
paul718e3742002-12-13 20:15:29 +00001650 switch (command)
1651 {
hasso18a6dce2004-10-03 18:18:34 +00001652 case ZEBRA_ROUTER_ID_ADD:
Feng Luc99f3482014-10-16 09:52:36 +08001653 zread_router_id_add (client, length, vrf_id);
hasso18a6dce2004-10-03 18:18:34 +00001654 break;
1655 case ZEBRA_ROUTER_ID_DELETE:
Feng Luc99f3482014-10-16 09:52:36 +08001656 zread_router_id_delete (client, length, vrf_id);
hasso18a6dce2004-10-03 18:18:34 +00001657 break;
paul718e3742002-12-13 20:15:29 +00001658 case ZEBRA_INTERFACE_ADD:
Feng Luc99f3482014-10-16 09:52:36 +08001659 zread_interface_add (client, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001660 break;
1661 case ZEBRA_INTERFACE_DELETE:
Feng Luc99f3482014-10-16 09:52:36 +08001662 zread_interface_delete (client, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001663 break;
1664 case ZEBRA_IPV4_ROUTE_ADD:
Feng Luc99f3482014-10-16 09:52:36 +08001665 zread_ipv4_add (client, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001666 break;
1667 case ZEBRA_IPV4_ROUTE_DELETE:
Feng Luc99f3482014-10-16 09:52:36 +08001668 zread_ipv4_delete (client, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001669 break;
1670#ifdef HAVE_IPV6
1671 case ZEBRA_IPV6_ROUTE_ADD:
Feng Luc99f3482014-10-16 09:52:36 +08001672 zread_ipv6_add (client, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001673 break;
1674 case ZEBRA_IPV6_ROUTE_DELETE:
Feng Luc99f3482014-10-16 09:52:36 +08001675 zread_ipv6_delete (client, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001676 break;
1677#endif /* HAVE_IPV6 */
1678 case ZEBRA_REDISTRIBUTE_ADD:
Feng Luc99f3482014-10-16 09:52:36 +08001679 zebra_redistribute_add (command, client, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001680 break;
1681 case ZEBRA_REDISTRIBUTE_DELETE:
Feng Luc99f3482014-10-16 09:52:36 +08001682 zebra_redistribute_delete (command, client, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001683 break;
1684 case ZEBRA_REDISTRIBUTE_DEFAULT_ADD:
Feng Luc99f3482014-10-16 09:52:36 +08001685 zebra_redistribute_default_add (command, client, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001686 break;
1687 case ZEBRA_REDISTRIBUTE_DEFAULT_DELETE:
Feng Luc99f3482014-10-16 09:52:36 +08001688 zebra_redistribute_default_delete (command, client, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001689 break;
1690 case ZEBRA_IPV4_NEXTHOP_LOOKUP:
Feng Luc99f3482014-10-16 09:52:36 +08001691 zread_ipv4_nexthop_lookup (client, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001692 break;
Everton Marques4e5275b2014-07-01 15:15:52 -03001693 case ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB:
Feng Luc99f3482014-10-16 09:52:36 +08001694 zread_ipv4_nexthop_lookup_mrib (client, length, vrf_id);
Everton Marques4e5275b2014-07-01 15:15:52 -03001695 break;
paul718e3742002-12-13 20:15:29 +00001696#ifdef HAVE_IPV6
1697 case ZEBRA_IPV6_NEXTHOP_LOOKUP:
Feng Luc99f3482014-10-16 09:52:36 +08001698 zread_ipv6_nexthop_lookup (client, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001699 break;
1700#endif /* HAVE_IPV6 */
1701 case ZEBRA_IPV4_IMPORT_LOOKUP:
Feng Luc99f3482014-10-16 09:52:36 +08001702 zread_ipv4_import_lookup (client, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001703 break;
Vyacheslav Trushkin2ea1ab12011-12-11 18:48:47 +04001704 case ZEBRA_HELLO:
1705 zread_hello (client);
1706 break;
Feng Luc99f3482014-10-16 09:52:36 +08001707 case ZEBRA_VRF_UNREGISTER:
1708 zread_vrf_unregister (client, length, vrf_id);
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -05001709 case ZEBRA_NEXTHOP_REGISTER:
1710 zserv_nexthop_register(client, sock, length, vrf_id);
1711 break;
1712 case ZEBRA_NEXTHOP_UNREGISTER:
1713 zserv_nexthop_unregister(client, sock, length);
Feng Luc99f3482014-10-16 09:52:36 +08001714 break;
paul718e3742002-12-13 20:15:29 +00001715 default:
1716 zlog_info ("Zebra received unknown command %d", command);
1717 break;
1718 }
1719
ajs719e9742005-02-28 20:52:15 +00001720 if (client->t_suicide)
1721 {
1722 /* No need to wait for thread callback, just kill immediately. */
1723 zebra_client_close(client);
1724 return -1;
1725 }
1726
paul718e3742002-12-13 20:15:29 +00001727 stream_reset (client->ibuf);
1728 zebra_event (ZEBRA_READ, sock, client);
paul718e3742002-12-13 20:15:29 +00001729 return 0;
1730}
1731
paul718e3742002-12-13 20:15:29 +00001732
1733/* Accept code of zebra server socket. */
paulb9df2d22004-05-09 09:09:59 +00001734static int
paul718e3742002-12-13 20:15:29 +00001735zebra_accept (struct thread *thread)
1736{
1737 int accept_sock;
1738 int client_sock;
1739 struct sockaddr_in client;
1740 socklen_t len;
1741
1742 accept_sock = THREAD_FD (thread);
1743
ajs719e9742005-02-28 20:52:15 +00001744 /* Reregister myself. */
1745 zebra_event (ZEBRA_SERV, accept_sock, NULL);
1746
paul718e3742002-12-13 20:15:29 +00001747 len = sizeof (struct sockaddr_in);
1748 client_sock = accept (accept_sock, (struct sockaddr *) &client, &len);
1749
1750 if (client_sock < 0)
1751 {
ajs6099b3b2004-11-20 02:06:59 +00001752 zlog_warn ("Can't accept zebra socket: %s", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001753 return -1;
1754 }
1755
paulccf35572003-03-01 11:42:20 +00001756 /* Make client socket non-blocking. */
ajs719e9742005-02-28 20:52:15 +00001757 set_nonblocking(client_sock);
paul865b8522005-01-05 08:30:35 +00001758
paul718e3742002-12-13 20:15:29 +00001759 /* Create new zebra client. */
1760 zebra_client_create (client_sock);
1761
paul718e3742002-12-13 20:15:29 +00001762 return 0;
1763}
1764
paulb9df2d22004-05-09 09:09:59 +00001765#ifdef HAVE_TCP_ZEBRA
paul718e3742002-12-13 20:15:29 +00001766/* Make zebra's server socket. */
paulb9df2d22004-05-09 09:09:59 +00001767static void
paul718e3742002-12-13 20:15:29 +00001768zebra_serv ()
1769{
1770 int ret;
1771 int accept_sock;
1772 struct sockaddr_in addr;
1773
1774 accept_sock = socket (AF_INET, SOCK_STREAM, 0);
1775
1776 if (accept_sock < 0)
1777 {
paul3d1dc852005-04-05 00:45:23 +00001778 zlog_warn ("Can't create zserv stream socket: %s",
1779 safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001780 zlog_warn ("zebra can't provice full functionality due to above error");
1781 return;
1782 }
1783
Vyacheslav Trushkin2ea1ab12011-12-11 18:48:47 +04001784 memset (&route_type_oaths, 0, sizeof (route_type_oaths));
paul718e3742002-12-13 20:15:29 +00001785 memset (&addr, 0, sizeof (struct sockaddr_in));
1786 addr.sin_family = AF_INET;
1787 addr.sin_port = htons (ZEBRA_PORT);
Paul Jakma6f0e3f62007-05-10 02:38:51 +00001788#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
paul718e3742002-12-13 20:15:29 +00001789 addr.sin_len = sizeof (struct sockaddr_in);
Paul Jakma6f0e3f62007-05-10 02:38:51 +00001790#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
paul718e3742002-12-13 20:15:29 +00001791 addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
1792
1793 sockopt_reuseaddr (accept_sock);
1794 sockopt_reuseport (accept_sock);
1795
pauledd7c242003-06-04 13:59:38 +00001796 if ( zserv_privs.change(ZPRIVS_RAISE) )
1797 zlog (NULL, LOG_ERR, "Can't raise privileges");
1798
paul718e3742002-12-13 20:15:29 +00001799 ret = bind (accept_sock, (struct sockaddr *)&addr,
1800 sizeof (struct sockaddr_in));
1801 if (ret < 0)
1802 {
paul3d1dc852005-04-05 00:45:23 +00001803 zlog_warn ("Can't bind to stream socket: %s",
1804 safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001805 zlog_warn ("zebra can't provice full functionality due to above error");
1806 close (accept_sock); /* Avoid sd leak. */
1807 return;
1808 }
pauledd7c242003-06-04 13:59:38 +00001809
1810 if ( zserv_privs.change(ZPRIVS_LOWER) )
1811 zlog (NULL, LOG_ERR, "Can't lower privileges");
paul718e3742002-12-13 20:15:29 +00001812
1813 ret = listen (accept_sock, 1);
1814 if (ret < 0)
1815 {
paul3d1dc852005-04-05 00:45:23 +00001816 zlog_warn ("Can't listen to stream socket: %s",
1817 safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001818 zlog_warn ("zebra can't provice full functionality due to above error");
1819 close (accept_sock); /* Avoid sd leak. */
1820 return;
1821 }
1822
1823 zebra_event (ZEBRA_SERV, accept_sock, NULL);
1824}
David Lamparter4b6c3322015-04-21 09:47:57 +02001825#else /* HAVE_TCP_ZEBRA */
paul718e3742002-12-13 20:15:29 +00001826
1827/* For sockaddr_un. */
1828#include <sys/un.h>
1829
1830/* zebra server UNIX domain socket. */
paulb9df2d22004-05-09 09:09:59 +00001831static void
hassofce954f2004-10-07 20:29:24 +00001832zebra_serv_un (const char *path)
paul718e3742002-12-13 20:15:29 +00001833{
1834 int ret;
1835 int sock, len;
1836 struct sockaddr_un serv;
1837 mode_t old_mask;
1838
1839 /* First of all, unlink existing socket */
1840 unlink (path);
1841
1842 /* Set umask */
1843 old_mask = umask (0077);
1844
1845 /* Make UNIX domain socket. */
1846 sock = socket (AF_UNIX, SOCK_STREAM, 0);
1847 if (sock < 0)
1848 {
paul3d1dc852005-04-05 00:45:23 +00001849 zlog_warn ("Can't create zserv unix socket: %s",
1850 safe_strerror (errno));
1851 zlog_warn ("zebra can't provide full functionality due to above error");
paul718e3742002-12-13 20:15:29 +00001852 return;
1853 }
1854
Vyacheslav Trushkin2ea1ab12011-12-11 18:48:47 +04001855 memset (&route_type_oaths, 0, sizeof (route_type_oaths));
1856
paul718e3742002-12-13 20:15:29 +00001857 /* Make server socket. */
1858 memset (&serv, 0, sizeof (struct sockaddr_un));
1859 serv.sun_family = AF_UNIX;
1860 strncpy (serv.sun_path, path, strlen (path));
Paul Jakma6f0e3f62007-05-10 02:38:51 +00001861#ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
paul718e3742002-12-13 20:15:29 +00001862 len = serv.sun_len = SUN_LEN(&serv);
1863#else
1864 len = sizeof (serv.sun_family) + strlen (serv.sun_path);
Paul Jakma6f0e3f62007-05-10 02:38:51 +00001865#endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
paul718e3742002-12-13 20:15:29 +00001866
1867 ret = bind (sock, (struct sockaddr *) &serv, len);
1868 if (ret < 0)
1869 {
paul3d1dc852005-04-05 00:45:23 +00001870 zlog_warn ("Can't bind to unix socket %s: %s",
1871 path, safe_strerror (errno));
1872 zlog_warn ("zebra can't provide full functionality due to above error");
paul718e3742002-12-13 20:15:29 +00001873 close (sock);
1874 return;
1875 }
1876
1877 ret = listen (sock, 5);
1878 if (ret < 0)
1879 {
paul3d1dc852005-04-05 00:45:23 +00001880 zlog_warn ("Can't listen to unix socket %s: %s",
1881 path, safe_strerror (errno));
1882 zlog_warn ("zebra can't provide full functionality due to above error");
paul718e3742002-12-13 20:15:29 +00001883 close (sock);
1884 return;
1885 }
1886
1887 umask (old_mask);
1888
1889 zebra_event (ZEBRA_SERV, sock, NULL);
1890}
David Lamparter4b6c3322015-04-21 09:47:57 +02001891#endif /* HAVE_TCP_ZEBRA */
David Lamparter6b0655a2014-06-04 06:53:35 +02001892
paul718e3742002-12-13 20:15:29 +00001893
paulb9df2d22004-05-09 09:09:59 +00001894static void
paul718e3742002-12-13 20:15:29 +00001895zebra_event (enum event event, int sock, struct zserv *client)
1896{
1897 switch (event)
1898 {
1899 case ZEBRA_SERV:
paulb21b19c2003-06-15 01:28:29 +00001900 thread_add_read (zebrad.master, zebra_accept, client, sock);
paul718e3742002-12-13 20:15:29 +00001901 break;
1902 case ZEBRA_READ:
1903 client->t_read =
paulb21b19c2003-06-15 01:28:29 +00001904 thread_add_read (zebrad.master, zebra_client_read, client, sock);
paul718e3742002-12-13 20:15:29 +00001905 break;
1906 case ZEBRA_WRITE:
1907 /**/
1908 break;
1909 }
1910}
David Lamparter6b0655a2014-06-04 06:53:35 +02001911
Dinesh Dutt9ae85522015-05-19 17:47:22 -07001912#define ZEBRA_TIME_BUF 32
1913static char *
1914zserv_time_buf(time_t *time1, char *buf, int buflen)
1915{
1916 struct tm *tm;
1917 time_t now;
1918
1919 assert (buf != NULL);
1920 assert (buflen >= ZEBRA_TIME_BUF);
1921 assert (time1 != NULL);
1922
1923 if (!*time1)
1924 {
1925 snprintf(buf, buflen, "never ");
1926 return (buf);
1927 }
1928
1929 now = quagga_time(NULL);
1930 now -= *time1;
1931 tm = gmtime(&now);
1932
1933 /* Making formatted timer strings. */
1934#define ONE_DAY_SECOND 60*60*24
1935#define ONE_WEEK_SECOND 60*60*24*7
1936
1937 if (now < ONE_DAY_SECOND)
1938 snprintf (buf, buflen, "%02d:%02d:%02d",
1939 tm->tm_hour, tm->tm_min, tm->tm_sec);
1940 else if (now < ONE_WEEK_SECOND)
1941 snprintf (buf, buflen, "%dd%02dh%02dm",
1942 tm->tm_yday, tm->tm_hour, tm->tm_min);
1943 else
1944 snprintf (buf, buflen, "%02dw%dd%02dh",
1945 tm->tm_yday/7, tm->tm_yday - ((tm->tm_yday/7) * 7), tm->tm_hour);
1946 return buf;
1947}
1948
1949static void
1950zebra_show_client_detail (struct vty *vty, struct zserv *client)
1951{
1952 char cbuf[ZEBRA_TIME_BUF], rbuf[ZEBRA_TIME_BUF];
1953 char wbuf[ZEBRA_TIME_BUF], nhbuf[ZEBRA_TIME_BUF], mbuf[ZEBRA_TIME_BUF];
1954
1955 vty_out (vty, "Client: %s %s",
1956 zebra_route_string(client->proto), VTY_NEWLINE);
1957 vty_out (vty, "------------------------ %s", VTY_NEWLINE);
1958 vty_out (vty, "FD: %d %s", client->sock, VTY_NEWLINE);
1959 vty_out (vty, "Route Table ID: %d %s", client->rtm_table, VTY_NEWLINE);
1960
1961 vty_out (vty, "Connect Time: %s %s",
1962 zserv_time_buf(&client->connect_time, cbuf, ZEBRA_TIME_BUF),
1963 VTY_NEWLINE);
1964 if (client->nh_reg_time)
1965 {
1966 vty_out (vty, "Nexthop Registry Time: %s %s",
1967 zserv_time_buf(&client->nh_reg_time, nhbuf, ZEBRA_TIME_BUF),
1968 VTY_NEWLINE);
1969 if (client->nh_last_upd_time)
1970 vty_out (vty, "Nexthop Last Update Time: %s %s",
1971 zserv_time_buf(&client->nh_last_upd_time, mbuf, ZEBRA_TIME_BUF),
1972 VTY_NEWLINE);
1973 else
1974 vty_out (vty, "No Nexthop Update sent%s", VTY_NEWLINE);
1975 }
1976 else
1977 vty_out (vty, "Not registered for Nexthop Updates%s", VTY_NEWLINE);
1978
1979 vty_out (vty, "Last Msg Rx Time: %s %s",
1980 zserv_time_buf(&client->last_read_time, rbuf, ZEBRA_TIME_BUF),
1981 VTY_NEWLINE);
1982 vty_out (vty, "Last Msg Tx Time: %s %s",
1983 zserv_time_buf(&client->last_write_time, wbuf, ZEBRA_TIME_BUF),
1984 VTY_NEWLINE);
1985 if (client->last_read_time)
1986 vty_out (vty, "Last Rcvd Cmd: %s %s",
1987 zserv_command_string(client->last_read_cmd), VTY_NEWLINE);
1988 if (client->last_write_time)
1989 vty_out (vty, "Last Sent Cmd: %s %s",
1990 zserv_command_string(client->last_write_cmd), VTY_NEWLINE);
1991 vty_out (vty, "%s", VTY_NEWLINE);
1992
1993 vty_out (vty, "Type Add Update Del %s", VTY_NEWLINE);
1994 vty_out (vty, "================================================== %s", VTY_NEWLINE);
1995 vty_out (vty, "IPv4 %-12d%-12d%-12d%s", client->v4_route_add_cnt,
1996 client->v4_route_upd8_cnt, client->v4_route_del_cnt, VTY_NEWLINE);
1997 vty_out (vty, "IPv6 %-12d%-12d%-12d%s", client->v6_route_add_cnt,
1998 client->v6_route_upd8_cnt, client->v6_route_del_cnt, VTY_NEWLINE);
1999 vty_out (vty, "Redist:v4 %-12d%-12d%-12d%s", client->redist_v4_add_cnt, 0,
2000 client->redist_v4_del_cnt, VTY_NEWLINE);
2001 vty_out (vty, "Redist:v6 %-12d%-12d%-12d%s", client->redist_v6_add_cnt, 0,
2002 client->redist_v6_del_cnt, VTY_NEWLINE);
2003 vty_out (vty, "Connected %-12d%-12d%-12d%s", client->ifadd_cnt, 0,
2004 client->ifdel_cnt, VTY_NEWLINE);
2005 vty_out (vty, "Interface Up Notifications: %d%s", client->ifup_cnt,
2006 VTY_NEWLINE);
2007 vty_out (vty, "Interface Down Notifications: %d%s", client->ifdown_cnt,
2008 VTY_NEWLINE);
2009
2010 vty_out (vty, "%s", VTY_NEWLINE);
2011 return;
2012}
2013
2014static void
2015zebra_show_client_brief (struct vty *vty, struct zserv *client)
2016{
2017 char cbuf[ZEBRA_TIME_BUF], rbuf[ZEBRA_TIME_BUF];
2018 char wbuf[ZEBRA_TIME_BUF];
2019
2020 vty_out (vty, "%-8s%12s %12s%12s%8d/%-8d%8d/%-8d%s",
2021 zebra_route_string(client->proto),
2022 zserv_time_buf(&client->connect_time, cbuf, ZEBRA_TIME_BUF),
2023 zserv_time_buf(&client->last_read_time, rbuf, ZEBRA_TIME_BUF),
2024 zserv_time_buf(&client->last_write_time, wbuf, ZEBRA_TIME_BUF),
2025 client->v4_route_add_cnt+client->v4_route_upd8_cnt,
2026 client->v4_route_del_cnt,
2027 client->v6_route_add_cnt+client->v6_route_upd8_cnt,
2028 client->v6_route_del_cnt, VTY_NEWLINE);
2029
2030}
2031
2032
paul718e3742002-12-13 20:15:29 +00002033/* Display default rtm_table for all clients. */
2034DEFUN (show_table,
2035 show_table_cmd,
2036 "show table",
2037 SHOW_STR
2038 "default routing table to use for all clients\n")
2039{
paulb21b19c2003-06-15 01:28:29 +00002040 vty_out (vty, "table %d%s", zebrad.rtm_table_default,
paul718e3742002-12-13 20:15:29 +00002041 VTY_NEWLINE);
2042 return CMD_SUCCESS;
2043}
2044
2045DEFUN (config_table,
2046 config_table_cmd,
2047 "table TABLENO",
2048 "Configure target kernel routing table\n"
2049 "TABLE integer\n")
2050{
paulb21b19c2003-06-15 01:28:29 +00002051 zebrad.rtm_table_default = strtol (argv[0], (char**)0, 10);
paul718e3742002-12-13 20:15:29 +00002052 return CMD_SUCCESS;
2053}
2054
hasso647e4f12003-05-25 11:43:52 +00002055DEFUN (ip_forwarding,
2056 ip_forwarding_cmd,
2057 "ip forwarding",
2058 IP_STR
2059 "Turn on IP forwarding")
2060{
2061 int ret;
2062
2063 ret = ipforward ();
hassob71f00f2004-10-13 12:20:35 +00002064 if (ret == 0)
2065 ret = ipforward_on ();
hasso647e4f12003-05-25 11:43:52 +00002066
hasso647e4f12003-05-25 11:43:52 +00002067 if (ret == 0)
2068 {
2069 vty_out (vty, "Can't turn on IP forwarding%s", VTY_NEWLINE);
2070 return CMD_WARNING;
2071 }
2072
2073 return CMD_SUCCESS;
2074}
2075
paul718e3742002-12-13 20:15:29 +00002076DEFUN (no_ip_forwarding,
2077 no_ip_forwarding_cmd,
2078 "no ip forwarding",
2079 NO_STR
2080 IP_STR
2081 "Turn off IP forwarding")
2082{
2083 int ret;
2084
2085 ret = ipforward ();
hassob71f00f2004-10-13 12:20:35 +00002086 if (ret != 0)
2087 ret = ipforward_off ();
paul718e3742002-12-13 20:15:29 +00002088
paul718e3742002-12-13 20:15:29 +00002089 if (ret != 0)
2090 {
2091 vty_out (vty, "Can't turn off IP forwarding%s", VTY_NEWLINE);
2092 return CMD_WARNING;
2093 }
2094
2095 return CMD_SUCCESS;
2096}
2097
2098/* This command is for debugging purpose. */
2099DEFUN (show_zebra_client,
2100 show_zebra_client_cmd,
2101 "show zebra client",
2102 SHOW_STR
2103 "Zebra information"
2104 "Client information")
2105{
hasso52dc7ee2004-09-23 19:18:23 +00002106 struct listnode *node;
paul718e3742002-12-13 20:15:29 +00002107 struct zserv *client;
2108
paul1eb8ef22005-04-07 07:30:20 +00002109 for (ALL_LIST_ELEMENTS_RO (zebrad.client_list, node, client))
Dinesh Dutt9ae85522015-05-19 17:47:22 -07002110 zebra_show_client_detail(vty, client);
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -05002111
paul718e3742002-12-13 20:15:29 +00002112 return CMD_SUCCESS;
2113}
2114
Dinesh Dutt9ae85522015-05-19 17:47:22 -07002115/* This command is for debugging purpose. */
2116DEFUN (show_zebra_client_summary,
2117 show_zebra_client_summary_cmd,
2118 "show zebra client summary",
2119 SHOW_STR
2120 "Zebra information brief"
2121 "Client information brief")
2122{
2123 struct listnode *node;
2124 struct zserv *client;
2125
2126 vty_out (vty, "Name Connect Time Last Read Last Write IPv4 Routes IPv6 Routes %s",
2127 VTY_NEWLINE);
2128 vty_out (vty,"--------------------------------------------------------------------------------%s",
2129 VTY_NEWLINE);
2130
2131 for (ALL_LIST_ELEMENTS_RO (zebrad.client_list, node, client))
2132 zebra_show_client_brief(vty, client);
2133
2134 vty_out (vty, "Routes column shows (added+updated)/deleted%s", VTY_NEWLINE);
2135 return CMD_SUCCESS;
2136}
2137
paul718e3742002-12-13 20:15:29 +00002138/* Table configuration write function. */
paulb9df2d22004-05-09 09:09:59 +00002139static int
paul718e3742002-12-13 20:15:29 +00002140config_write_table (struct vty *vty)
2141{
paulb21b19c2003-06-15 01:28:29 +00002142 if (zebrad.rtm_table_default)
2143 vty_out (vty, "table %d%s", zebrad.rtm_table_default,
paul718e3742002-12-13 20:15:29 +00002144 VTY_NEWLINE);
2145 return 0;
2146}
2147
2148/* table node for routing tables. */
Stephen Hemminger7fc626d2008-12-01 11:10:34 -08002149static struct cmd_node table_node =
paul718e3742002-12-13 20:15:29 +00002150{
2151 TABLE_NODE,
2152 "", /* This node has no interface. */
2153 1
2154};
David Lamparter6b0655a2014-06-04 06:53:35 +02002155
paul718e3742002-12-13 20:15:29 +00002156/* Only display ip forwarding is enabled or not. */
2157DEFUN (show_ip_forwarding,
2158 show_ip_forwarding_cmd,
2159 "show ip forwarding",
2160 SHOW_STR
2161 IP_STR
2162 "IP forwarding status\n")
2163{
2164 int ret;
2165
2166 ret = ipforward ();
2167
2168 if (ret == 0)
2169 vty_out (vty, "IP forwarding is off%s", VTY_NEWLINE);
2170 else
2171 vty_out (vty, "IP forwarding is on%s", VTY_NEWLINE);
2172 return CMD_SUCCESS;
2173}
2174
2175#ifdef HAVE_IPV6
2176/* Only display ipv6 forwarding is enabled or not. */
2177DEFUN (show_ipv6_forwarding,
2178 show_ipv6_forwarding_cmd,
2179 "show ipv6 forwarding",
2180 SHOW_STR
2181 "IPv6 information\n"
2182 "Forwarding status\n")
2183{
2184 int ret;
2185
2186 ret = ipforward_ipv6 ();
2187
2188 switch (ret)
2189 {
2190 case -1:
2191 vty_out (vty, "ipv6 forwarding is unknown%s", VTY_NEWLINE);
2192 break;
2193 case 0:
2194 vty_out (vty, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE);
2195 break;
2196 case 1:
2197 vty_out (vty, "ipv6 forwarding is %s%s", "on", VTY_NEWLINE);
2198 break;
2199 default:
2200 vty_out (vty, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE);
2201 break;
2202 }
2203 return CMD_SUCCESS;
2204}
2205
hasso55906722004-02-11 22:42:16 +00002206DEFUN (ipv6_forwarding,
2207 ipv6_forwarding_cmd,
2208 "ipv6 forwarding",
2209 IPV6_STR
2210 "Turn on IPv6 forwarding")
2211{
2212 int ret;
2213
hasso41d3fc92004-04-06 11:59:00 +00002214 ret = ipforward_ipv6 ();
hassob71f00f2004-10-13 12:20:35 +00002215 if (ret == 0)
2216 ret = ipforward_ipv6_on ();
hasso41d3fc92004-04-06 11:59:00 +00002217
hasso41d3fc92004-04-06 11:59:00 +00002218 if (ret == 0)
2219 {
hasso55906722004-02-11 22:42:16 +00002220 vty_out (vty, "Can't turn on IPv6 forwarding%s", VTY_NEWLINE);
2221 return CMD_WARNING;
2222 }
2223
2224 return CMD_SUCCESS;
2225}
2226
paul718e3742002-12-13 20:15:29 +00002227DEFUN (no_ipv6_forwarding,
2228 no_ipv6_forwarding_cmd,
2229 "no ipv6 forwarding",
2230 NO_STR
hasso55906722004-02-11 22:42:16 +00002231 IPV6_STR
2232 "Turn off IPv6 forwarding")
paul718e3742002-12-13 20:15:29 +00002233{
2234 int ret;
2235
hasso41d3fc92004-04-06 11:59:00 +00002236 ret = ipforward_ipv6 ();
hassob71f00f2004-10-13 12:20:35 +00002237 if (ret != 0)
2238 ret = ipforward_ipv6_off ();
hasso41d3fc92004-04-06 11:59:00 +00002239
paul718e3742002-12-13 20:15:29 +00002240 if (ret != 0)
2241 {
2242 vty_out (vty, "Can't turn off IPv6 forwarding%s", VTY_NEWLINE);
2243 return CMD_WARNING;
2244 }
2245
2246 return CMD_SUCCESS;
2247}
2248
2249#endif /* HAVE_IPV6 */
2250
2251/* IPForwarding configuration write function. */
ajs719e9742005-02-28 20:52:15 +00002252static int
paul718e3742002-12-13 20:15:29 +00002253config_write_forwarding (struct vty *vty)
2254{
hasso18a6dce2004-10-03 18:18:34 +00002255 /* FIXME: Find better place for that. */
2256 router_id_write (vty);
2257
paul3e0b3a52004-08-23 18:58:32 +00002258 if (ipforward ())
2259 vty_out (vty, "ip forwarding%s", VTY_NEWLINE);
paul718e3742002-12-13 20:15:29 +00002260#ifdef HAVE_IPV6
paul3e0b3a52004-08-23 18:58:32 +00002261 if (ipforward_ipv6 ())
2262 vty_out (vty, "ipv6 forwarding%s", VTY_NEWLINE);
paul718e3742002-12-13 20:15:29 +00002263#endif /* HAVE_IPV6 */
2264 vty_out (vty, "!%s", VTY_NEWLINE);
2265 return 0;
2266}
2267
2268/* table node for routing tables. */
Stephen Hemminger7fc626d2008-12-01 11:10:34 -08002269static struct cmd_node forwarding_node =
paul718e3742002-12-13 20:15:29 +00002270{
2271 FORWARDING_NODE,
2272 "", /* This node has no interface. */
2273 1
2274};
2275
Udaya Shankara KSd869dbd2016-02-11 21:42:29 +05302276#ifdef HAVE_FPM
2277/* function to write the fpm config info */
2278static int
2279config_write_fpm (struct vty *vty)
2280{
2281 return
2282 fpm_remote_srv_write (vty);
2283}
2284
2285/* Zebra node */
2286static struct cmd_node zebra_node =
2287{
2288 ZEBRA_NODE,
2289 "",
2290 1
2291};
2292#endif
2293
David Lamparter6b0655a2014-06-04 06:53:35 +02002294
paul718e3742002-12-13 20:15:29 +00002295/* Initialisation of zebra and installation of commands. */
2296void
paula1ac18c2005-06-28 17:17:12 +00002297zebra_init (void)
paul718e3742002-12-13 20:15:29 +00002298{
2299 /* Client list init. */
paulb21b19c2003-06-15 01:28:29 +00002300 zebrad.client_list = list_new ();
paul718e3742002-12-13 20:15:29 +00002301
paul718e3742002-12-13 20:15:29 +00002302 /* Install configuration write function. */
2303 install_node (&table_node, config_write_table);
2304 install_node (&forwarding_node, config_write_forwarding);
Udaya Shankara KSd869dbd2016-02-11 21:42:29 +05302305#ifdef HAVE_FPM
2306 install_node (&zebra_node, config_write_fpm);
2307#endif
paul718e3742002-12-13 20:15:29 +00002308
2309 install_element (VIEW_NODE, &show_ip_forwarding_cmd);
hasso647e4f12003-05-25 11:43:52 +00002310 install_element (CONFIG_NODE, &ip_forwarding_cmd);
paul718e3742002-12-13 20:15:29 +00002311 install_element (CONFIG_NODE, &no_ip_forwarding_cmd);
2312 install_element (ENABLE_NODE, &show_zebra_client_cmd);
Dinesh Dutt9ae85522015-05-19 17:47:22 -07002313 install_element (ENABLE_NODE, &show_zebra_client_summary_cmd);
paul718e3742002-12-13 20:15:29 +00002314
2315#ifdef HAVE_NETLINK
2316 install_element (VIEW_NODE, &show_table_cmd);
paul718e3742002-12-13 20:15:29 +00002317 install_element (CONFIG_NODE, &config_table_cmd);
2318#endif /* HAVE_NETLINK */
2319
2320#ifdef HAVE_IPV6
2321 install_element (VIEW_NODE, &show_ipv6_forwarding_cmd);
hasso55906722004-02-11 22:42:16 +00002322 install_element (CONFIG_NODE, &ipv6_forwarding_cmd);
paul718e3742002-12-13 20:15:29 +00002323 install_element (CONFIG_NODE, &no_ipv6_forwarding_cmd);
2324#endif /* HAVE_IPV6 */
Paul Jakma7514fb72007-05-02 16:05:35 +00002325
2326 /* Route-map */
2327 zebra_route_map_init ();
paul718e3742002-12-13 20:15:29 +00002328}
Denis Ovsienko97be79f2009-07-24 20:45:31 +04002329
2330/* Make zebra server socket, wiping any existing one (see bug #403). */
2331void
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +04002332zebra_zserv_socket_init (char *path)
Denis Ovsienko97be79f2009-07-24 20:45:31 +04002333{
2334#ifdef HAVE_TCP_ZEBRA
2335 zebra_serv ();
2336#else
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +04002337 zebra_serv_un (path ? path : ZEBRA_SERV_PATH);
Denis Ovsienko97be79f2009-07-24 20:45:31 +04002338#endif /* HAVE_TCP_ZEBRA */
2339}