blob: a96bcb1d85eb159effe9b8b0301566a2f6a625d5 [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 }
Jafar Al-Gharaibeh56ae5c42016-06-17 16:01:12 -0500588
paul718e3742002-12-13 20:15:29 +0000589 stream_putc_at (s, nump, num);
590 }
591 else
592 {
593 stream_putl (s, 0);
594 stream_putc (s, 0);
595 }
596
597 stream_putw_at (s, 0, stream_get_endp (s));
Dinesh Dutt9ae85522015-05-19 17:47:22 -0700598
ajs719e9742005-02-28 20:52:15 +0000599 return zebra_server_send_message(client);
paul718e3742002-12-13 20:15:29 +0000600}
601#endif /* HAVE_IPV6 */
602
Jafar Al-Gharaibeh56ae5c42016-06-17 16:01:12 -0500603/*
604 In the case of ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB:
605 query unicast rib if nexthop is not found on mrib
606 and return both route metric and protocol distance.
607*/
paulb9df2d22004-05-09 09:09:59 +0000608static int
Feng Luc99f3482014-10-16 09:52:36 +0800609zsend_ipv4_nexthop_lookup (struct zserv *client, struct in_addr addr,
Jafar Al-Gharaibeh56ae5c42016-06-17 16:01:12 -0500610 int cmd, vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +0000611{
612 struct stream *s;
613 struct rib *rib;
614 unsigned long nump;
615 u_char num;
616 struct nexthop *nexthop;
617
paul718e3742002-12-13 20:15:29 +0000618 /* Get output stream. */
619 s = client->obuf;
620 stream_reset (s);
621
622 /* Fill in result. */
Jafar Al-Gharaibeh56ae5c42016-06-17 16:01:12 -0500623 zserv_create_header (s, cmd, vrf_id);
paul718e3742002-12-13 20:15:29 +0000624 stream_put_in_addr (s, &addr);
625
Jafar Al-Gharaibeh56ae5c42016-06-17 16:01:12 -0500626 /* Lookup nexthop - eBGP excluded */
627 if (cmd == ZEBRA_IPV4_NEXTHOP_LOOKUP)
628 rib = rib_match_ipv4_safi (addr, SAFI_UNICAST, 1, NULL, vrf_id);
629 else /* ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB */
630 {
631 rib = rib_match_ipv4_multicast (addr, NULL, vrf_id);
632 /* handle the distance field here since
633 * it is only needed for MRIB command */
634 if (rib)
635 stream_putc (s, rib->distance);
636 else
637 stream_putc (s, 0); /* distance */
638 }
639
paul718e3742002-12-13 20:15:29 +0000640 if (rib)
641 {
Christian Frankebb97e462013-05-25 14:01:35 +0000642 if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
643 zlog_debug("%s: Matching rib entry found.", __func__);
paul718e3742002-12-13 20:15:29 +0000644 stream_putl (s, rib->metric);
645 num = 0;
Everton Marques4e5275b2014-07-01 15:15:52 -0300646 nump = stream_get_endp(s); /* remember position for nexthop_num */
647 stream_putc (s, 0); /* reserve room for nexthop_num */
648 /* Only non-recursive routes are elegible to resolve the nexthop we
649 * are looking up. Therefore, we will just iterate over the top
650 * chain of nexthops. */
651 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
Timo Teräs325823a2016-01-15 17:36:31 +0200652 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE))
Everton Marques4e5275b2014-07-01 15:15:52 -0300653 {
654 stream_putc (s, nexthop->type);
655 switch (nexthop->type)
656 {
657 case ZEBRA_NEXTHOP_IPV4:
658 stream_put_in_addr (s, &nexthop->gate.ipv4);
659 break;
660 case ZEBRA_NEXTHOP_IPV4_IFINDEX:
661 stream_put_in_addr (s, &nexthop->gate.ipv4);
662 stream_putl (s, nexthop->ifindex);
663 break;
664 case ZEBRA_NEXTHOP_IFINDEX:
665 case ZEBRA_NEXTHOP_IFNAME:
666 stream_putl (s, nexthop->ifindex);
667 break;
668 default:
669 /* do nothing */
670 break;
671 }
672 num++;
673 }
Jafar Al-Gharaibeh56ae5c42016-06-17 16:01:12 -0500674
Everton Marques4e5275b2014-07-01 15:15:52 -0300675 stream_putc_at (s, nump, num); /* store nexthop_num */
676 }
677 else
678 {
Jafar Al-Gharaibeh56ae5c42016-06-17 16:01:12 -0500679 if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
680 zlog_debug("%s: No matching rib entry found.", __func__);
Everton Marques4e5275b2014-07-01 15:15:52 -0300681 stream_putl (s, 0); /* metric */
682 stream_putc (s, 0); /* nexthop_num */
683 }
684
685 stream_putw_at (s, 0, stream_get_endp (s));
686
687 return zebra_server_send_message(client);
688}
689
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -0500690/* Nexthop register */
691static int
692zserv_nexthop_register (struct zserv *client, int sock, u_short length, vrf_id_t vrf_id)
693{
694 struct rnh *rnh;
695 struct stream *s;
696 struct prefix p;
697 u_short l = 0;
Dinesh Duttd9ab53a2015-05-19 17:47:21 -0700698 u_char connected;
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -0500699
700 if (IS_ZEBRA_DEBUG_NHT)
701 zlog_debug("nexthop_register msg from client %s: length=%d\n",
702 zebra_route_string(client->proto), length);
703
704 s = client->ibuf;
705
706 while (l < length)
707 {
Dinesh Duttd9ab53a2015-05-19 17:47:21 -0700708 connected = stream_getc(s);
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -0500709 p.family = stream_getw(s);
710 p.prefixlen = stream_getc(s);
Dinesh Duttd9ab53a2015-05-19 17:47:21 -0700711 l += 4;
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -0500712 stream_get(&p.u.prefix, s, PSIZE(p.prefixlen));
713 l += PSIZE(p.prefixlen);
714 rnh = zebra_add_rnh(&p, 0);
Dinesh Dutt9ae85522015-05-19 17:47:22 -0700715
716 client->nh_reg_time = quagga_time(NULL);
Dinesh Duttd9ab53a2015-05-19 17:47:21 -0700717
718 if (connected)
719 SET_FLAG(rnh->flags, ZEBRA_NHT_CONNECTED);
720
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -0500721 zebra_add_rnh_client(rnh, client, vrf_id);
722 }
723 zebra_evaluate_rnh_table(0, AF_INET);
724 zebra_evaluate_rnh_table(0, AF_INET6);
725 return 0;
726}
727
728/* Nexthop register */
729static int
730zserv_nexthop_unregister (struct zserv *client, int sock, u_short length)
731{
732 struct rnh *rnh;
733 struct stream *s;
734 struct prefix p;
735 u_short l = 0;
736
737 if (IS_ZEBRA_DEBUG_NHT)
738 zlog_debug("nexthop_unregister msg from client %s: length=%d\n",
739 zebra_route_string(client->proto), length);
740
741 s = client->ibuf;
742
743 while (l < length)
744 {
Dinesh Duttd9ab53a2015-05-19 17:47:21 -0700745 (void)stream_getc(s);
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -0500746 p.family = stream_getw(s);
747 p.prefixlen = stream_getc(s);
Dinesh Duttd9ab53a2015-05-19 17:47:21 -0700748 l += 4;
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -0500749 stream_get(&p.u.prefix, s, PSIZE(p.prefixlen));
750 l += PSIZE(p.prefixlen);
751 rnh = zebra_lookup_rnh(&p, 0);
752 if (rnh)
Dinesh Dutt9ae85522015-05-19 17:47:22 -0700753 {
754 client->nh_dereg_time = quagga_time(NULL);
755 zebra_remove_rnh_client(rnh, client);
756 }
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -0500757 }
758 return 0;
759}
760
paulb9df2d22004-05-09 09:09:59 +0000761static int
Feng Luc99f3482014-10-16 09:52:36 +0800762zsend_ipv4_import_lookup (struct zserv *client, struct prefix_ipv4 *p,
763 vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +0000764{
765 struct stream *s;
766 struct rib *rib;
767 unsigned long nump;
768 u_char num;
769 struct nexthop *nexthop;
770
771 /* Lookup nexthop. */
Feng Luc99f3482014-10-16 09:52:36 +0800772 rib = rib_lookup_ipv4 (p, vrf_id);
paul718e3742002-12-13 20:15:29 +0000773
774 /* Get output stream. */
775 s = client->obuf;
776 stream_reset (s);
777
778 /* Fill in result. */
Feng Luc99f3482014-10-16 09:52:36 +0800779 zserv_create_header (s, ZEBRA_IPV4_IMPORT_LOOKUP, vrf_id);
paul718e3742002-12-13 20:15:29 +0000780 stream_put_in_addr (s, &p->prefix);
781
782 if (rib)
783 {
784 stream_putl (s, rib->metric);
785 num = 0;
paul9985f832005-02-09 15:51:56 +0000786 nump = stream_get_endp(s);
paul718e3742002-12-13 20:15:29 +0000787 stream_putc (s, 0);
788 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
Timo Teräs325823a2016-01-15 17:36:31 +0200789 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE))
paul718e3742002-12-13 20:15:29 +0000790 {
791 stream_putc (s, nexthop->type);
792 switch (nexthop->type)
793 {
794 case ZEBRA_NEXTHOP_IPV4:
795 stream_put_in_addr (s, &nexthop->gate.ipv4);
796 break;
Christian Frankea12afd52013-05-25 14:01:36 +0000797 case ZEBRA_NEXTHOP_IPV4_IFINDEX:
798 stream_put_in_addr (s, &nexthop->gate.ipv4);
799 stream_putl (s, nexthop->ifindex);
800 break;
paul718e3742002-12-13 20:15:29 +0000801 case ZEBRA_NEXTHOP_IFINDEX:
802 case ZEBRA_NEXTHOP_IFNAME:
803 stream_putl (s, nexthop->ifindex);
804 break;
hassofa2b17e2004-03-04 17:45:00 +0000805 default:
806 /* do nothing */
807 break;
paul718e3742002-12-13 20:15:29 +0000808 }
809 num++;
810 }
811 stream_putc_at (s, nump, num);
812 }
813 else
814 {
815 stream_putl (s, 0);
816 stream_putc (s, 0);
817 }
818
819 stream_putw_at (s, 0, stream_get_endp (s));
Dinesh Dutt9ae85522015-05-19 17:47:22 -0700820
ajs719e9742005-02-28 20:52:15 +0000821 return zebra_server_send_message(client);
paul718e3742002-12-13 20:15:29 +0000822}
David Lamparter6b0655a2014-06-04 06:53:35 +0200823
hasso18a6dce2004-10-03 18:18:34 +0000824/* Router-id is updated. Send ZEBRA_ROUTER_ID_ADD to client. */
825int
Feng Luac19a442015-05-22 11:40:07 +0200826zsend_router_id_update (struct zserv *client, struct prefix *p,
827 vrf_id_t vrf_id)
hasso18a6dce2004-10-03 18:18:34 +0000828{
829 struct stream *s;
830 int blen;
831
832 /* Check this client need interface information. */
Feng Luc99f3482014-10-16 09:52:36 +0800833 if (! vrf_bitmap_check (client->ridinfo, vrf_id))
ajs719e9742005-02-28 20:52:15 +0000834 return 0;
hasso18a6dce2004-10-03 18:18:34 +0000835
836 s = client->obuf;
837 stream_reset (s);
838
hasso18a6dce2004-10-03 18:18:34 +0000839 /* Message type. */
Feng Luc99f3482014-10-16 09:52:36 +0800840 zserv_create_header (s, ZEBRA_ROUTER_ID_UPDATE, vrf_id);
hasso18a6dce2004-10-03 18:18:34 +0000841
842 /* Prefix information. */
843 stream_putc (s, p->family);
844 blen = prefix_blen (p);
845 stream_put (s, &p->u.prefix, blen);
846 stream_putc (s, p->prefixlen);
847
848 /* Write packet size. */
849 stream_putw_at (s, 0, stream_get_endp (s));
850
ajs719e9742005-02-28 20:52:15 +0000851 return zebra_server_send_message(client);
hasso18a6dce2004-10-03 18:18:34 +0000852}
David Lamparter6b0655a2014-06-04 06:53:35 +0200853
paul718e3742002-12-13 20:15:29 +0000854/* Register zebra server interface information. Send current all
855 interface and address information. */
ajs719e9742005-02-28 20:52:15 +0000856static int
Feng Luc99f3482014-10-16 09:52:36 +0800857zread_interface_add (struct zserv *client, u_short length, vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +0000858{
paul1eb8ef22005-04-07 07:30:20 +0000859 struct listnode *ifnode, *ifnnode;
860 struct listnode *cnode, *cnnode;
paul718e3742002-12-13 20:15:29 +0000861 struct interface *ifp;
862 struct connected *c;
863
864 /* Interface information is needed. */
Feng Luc99f3482014-10-16 09:52:36 +0800865 vrf_bitmap_set (client->ifinfo, vrf_id);
paul718e3742002-12-13 20:15:29 +0000866
Feng Luc99f3482014-10-16 09:52:36 +0800867 for (ALL_LIST_ELEMENTS (vrf_iflist (vrf_id), ifnode, ifnnode, ifp))
paul718e3742002-12-13 20:15:29 +0000868 {
paul718e3742002-12-13 20:15:29 +0000869 /* Skip pseudo interface. */
870 if (! CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE))
871 continue;
872
ajs719e9742005-02-28 20:52:15 +0000873 if (zsend_interface_add (client, ifp) < 0)
874 return -1;
paul718e3742002-12-13 20:15:29 +0000875
paul1eb8ef22005-04-07 07:30:20 +0000876 for (ALL_LIST_ELEMENTS (ifp->connected, cnode, cnnode, c))
paul718e3742002-12-13 20:15:29 +0000877 {
ajs719e9742005-02-28 20:52:15 +0000878 if (CHECK_FLAG (c->conf, ZEBRA_IFC_REAL) &&
879 (zsend_interface_address (ZEBRA_INTERFACE_ADDRESS_ADD, client,
880 ifp, c) < 0))
881 return -1;
paul718e3742002-12-13 20:15:29 +0000882 }
883 }
ajs719e9742005-02-28 20:52:15 +0000884 return 0;
paul718e3742002-12-13 20:15:29 +0000885}
886
887/* Unregister zebra server interface information. */
ajs719e9742005-02-28 20:52:15 +0000888static int
Feng Luc99f3482014-10-16 09:52:36 +0800889zread_interface_delete (struct zserv *client, u_short length, vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +0000890{
Feng Luc99f3482014-10-16 09:52:36 +0800891 vrf_bitmap_unset (client->ifinfo, vrf_id);
ajs719e9742005-02-28 20:52:15 +0000892 return 0;
paul718e3742002-12-13 20:15:29 +0000893}
894
895/* This function support multiple nexthop. */
paulb9df2d22004-05-09 09:09:59 +0000896/*
897 * Parse the ZEBRA_IPV4_ROUTE_ADD sent from client. Update rib and
898 * add kernel route.
899 */
ajs719e9742005-02-28 20:52:15 +0000900static int
Feng Luc99f3482014-10-16 09:52:36 +0800901zread_ipv4_add (struct zserv *client, u_short length, vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +0000902{
903 int i;
904 struct rib *rib;
905 struct prefix_ipv4 p;
906 u_char message;
907 struct in_addr nexthop;
908 u_char nexthop_num;
909 u_char nexthop_type;
910 struct stream *s;
Paul Jakma9099f9b2016-01-18 10:12:10 +0000911 ifindex_t ifindex;
paul718e3742002-12-13 20:15:29 +0000912 u_char ifname_len;
G.Balajicddf3912011-11-26 21:59:32 +0400913 safi_t safi;
Dinesh Dutt9ae85522015-05-19 17:47:22 -0700914 int ret;
paul718e3742002-12-13 20:15:29 +0000915
916 /* Get input stream. */
917 s = client->ibuf;
918
919 /* Allocate new rib. */
paul4d38fdb2005-04-28 17:35:14 +0000920 rib = XCALLOC (MTYPE_RIB, sizeof (struct rib));
921
paul718e3742002-12-13 20:15:29 +0000922 /* Type, flags, message. */
923 rib->type = stream_getc (s);
924 rib->flags = stream_getc (s);
paulb9df2d22004-05-09 09:09:59 +0000925 message = stream_getc (s);
G.Balajicddf3912011-11-26 21:59:32 +0400926 safi = stream_getw (s);
paul718e3742002-12-13 20:15:29 +0000927 rib->uptime = time (NULL);
928
929 /* IPv4 prefix. */
930 memset (&p, 0, sizeof (struct prefix_ipv4));
931 p.family = AF_INET;
932 p.prefixlen = stream_getc (s);
933 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
934
Feng Lu0d0686f2015-05-22 11:40:02 +0200935 /* VRF ID */
Feng Luc99f3482014-10-16 09:52:36 +0800936 rib->vrf_id = vrf_id;
Feng Lu0d0686f2015-05-22 11:40:02 +0200937
paul718e3742002-12-13 20:15:29 +0000938 /* Nexthop parse. */
939 if (CHECK_FLAG (message, ZAPI_MESSAGE_NEXTHOP))
940 {
941 nexthop_num = stream_getc (s);
942
943 for (i = 0; i < nexthop_num; i++)
944 {
945 nexthop_type = stream_getc (s);
946
947 switch (nexthop_type)
948 {
949 case ZEBRA_NEXTHOP_IFINDEX:
950 ifindex = stream_getl (s);
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -0500951 rib_nexthop_ifindex_add (rib, ifindex);
paul718e3742002-12-13 20:15:29 +0000952 break;
953 case ZEBRA_NEXTHOP_IFNAME:
954 ifname_len = stream_getc (s);
paul9985f832005-02-09 15:51:56 +0000955 stream_forward_getp (s, ifname_len);
paul718e3742002-12-13 20:15:29 +0000956 break;
957 case ZEBRA_NEXTHOP_IPV4:
958 nexthop.s_addr = stream_get_ipv4 (s);
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -0500959 rib_nexthop_ipv4_add (rib, &nexthop, NULL);
paul718e3742002-12-13 20:15:29 +0000960 break;
Joakim Tjernlundc963c202012-07-07 17:06:13 +0200961 case ZEBRA_NEXTHOP_IPV4_IFINDEX:
962 nexthop.s_addr = stream_get_ipv4 (s);
963 ifindex = stream_getl (s);
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -0500964 rib_nexthop_ipv4_ifindex_add (rib, &nexthop, NULL, ifindex);
Joakim Tjernlundc963c202012-07-07 17:06:13 +0200965 break;
paul718e3742002-12-13 20:15:29 +0000966 case ZEBRA_NEXTHOP_IPV6:
paul9985f832005-02-09 15:51:56 +0000967 stream_forward_getp (s, IPV6_MAX_BYTELEN);
paul718e3742002-12-13 20:15:29 +0000968 break;
Subbaiah Venkata6902c692012-03-27 19:21:29 -0700969 case ZEBRA_NEXTHOP_BLACKHOLE:
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -0500970 rib_nexthop_blackhole_add (rib);
Subbaiah Venkata6902c692012-03-27 19:21:29 -0700971 break;
972 }
paul718e3742002-12-13 20:15:29 +0000973 }
974 }
975
976 /* Distance. */
977 if (CHECK_FLAG (message, ZAPI_MESSAGE_DISTANCE))
978 rib->distance = stream_getc (s);
979
980 /* Metric. */
981 if (CHECK_FLAG (message, ZAPI_MESSAGE_METRIC))
982 rib->metric = stream_getl (s);
983
Timo Teräsb11f3b52015-11-02 16:50:07 +0200984 if (CHECK_FLAG (message, ZAPI_MESSAGE_MTU))
985 rib->mtu = stream_getl (s);
Piotr Chytłaeefddcc2015-12-01 09:48:02 -0500986 /* Tag */
987 if (CHECK_FLAG (message, ZAPI_MESSAGE_TAG))
Paul Jakma96d10602016-07-01 14:23:45 +0100988 rib->tag = stream_getl (s);
Christian Frankeddc160c2016-10-01 20:42:34 +0200989 else
990 rib->tag = 0;
991
Paul Jakma171eee32006-07-27 16:11:02 +0000992 /* Table */
993 rib->table=zebrad.rtm_table_default;
Dinesh Dutt9ae85522015-05-19 17:47:22 -0700994 ret = rib_add_ipv4_multipath (&p, rib, safi);
995
996 /* Stats */
997 if (ret > 0)
998 client->v4_route_add_cnt++;
999 else if (ret < 0)
1000 client->v4_route_upd8_cnt++;
ajs719e9742005-02-28 20:52:15 +00001001 return 0;
paul718e3742002-12-13 20:15:29 +00001002}
1003
1004/* Zebra server IPv4 prefix delete function. */
ajs719e9742005-02-28 20:52:15 +00001005static int
Feng Luc99f3482014-10-16 09:52:36 +08001006zread_ipv4_delete (struct zserv *client, u_short length, vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +00001007{
1008 int i;
1009 struct stream *s;
1010 struct zapi_ipv4 api;
Subbaiah Venkata6902c692012-03-27 19:21:29 -07001011 struct in_addr nexthop, *nexthop_p;
paul718e3742002-12-13 20:15:29 +00001012 unsigned long ifindex;
1013 struct prefix_ipv4 p;
1014 u_char nexthop_num;
1015 u_char nexthop_type;
1016 u_char ifname_len;
1017
1018 s = client->ibuf;
1019 ifindex = 0;
1020 nexthop.s_addr = 0;
Subbaiah Venkata6902c692012-03-27 19:21:29 -07001021 nexthop_p = NULL;
paul718e3742002-12-13 20:15:29 +00001022
1023 /* Type, flags, message. */
1024 api.type = stream_getc (s);
1025 api.flags = stream_getc (s);
1026 api.message = stream_getc (s);
G.Balajicddf3912011-11-26 21:59:32 +04001027 api.safi = stream_getw (s);
paul718e3742002-12-13 20:15:29 +00001028
1029 /* IPv4 prefix. */
1030 memset (&p, 0, sizeof (struct prefix_ipv4));
1031 p.family = AF_INET;
1032 p.prefixlen = stream_getc (s);
1033 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
1034
1035 /* Nexthop, ifindex, distance, metric. */
1036 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
1037 {
1038 nexthop_num = stream_getc (s);
1039
1040 for (i = 0; i < nexthop_num; i++)
1041 {
1042 nexthop_type = stream_getc (s);
1043
1044 switch (nexthop_type)
1045 {
1046 case ZEBRA_NEXTHOP_IFINDEX:
1047 ifindex = stream_getl (s);
1048 break;
1049 case ZEBRA_NEXTHOP_IFNAME:
1050 ifname_len = stream_getc (s);
paul9985f832005-02-09 15:51:56 +00001051 stream_forward_getp (s, ifname_len);
paul718e3742002-12-13 20:15:29 +00001052 break;
1053 case ZEBRA_NEXTHOP_IPV4:
1054 nexthop.s_addr = stream_get_ipv4 (s);
Subbaiah Venkata6902c692012-03-27 19:21:29 -07001055 nexthop_p = &nexthop;
paul718e3742002-12-13 20:15:29 +00001056 break;
Joakim Tjernlundc963c202012-07-07 17:06:13 +02001057 case ZEBRA_NEXTHOP_IPV4_IFINDEX:
1058 nexthop.s_addr = stream_get_ipv4 (s);
Christian Franke23f5f7c2013-11-27 17:06:14 +00001059 nexthop_p = &nexthop;
Joakim Tjernlundc963c202012-07-07 17:06:13 +02001060 ifindex = stream_getl (s);
1061 break;
paul718e3742002-12-13 20:15:29 +00001062 case ZEBRA_NEXTHOP_IPV6:
paul9985f832005-02-09 15:51:56 +00001063 stream_forward_getp (s, IPV6_MAX_BYTELEN);
paul718e3742002-12-13 20:15:29 +00001064 break;
1065 }
1066 }
1067 }
1068
1069 /* Distance. */
1070 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
1071 api.distance = stream_getc (s);
1072 else
1073 api.distance = 0;
1074
1075 /* Metric. */
1076 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
1077 api.metric = stream_getl (s);
1078 else
1079 api.metric = 0;
1080
Piotr Chytłaeefddcc2015-12-01 09:48:02 -05001081 /* tag */
1082 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_TAG))
Paul Jakma96d10602016-07-01 14:23:45 +01001083 api.tag = stream_getl (s);
Piotr Chytłaeefddcc2015-12-01 09:48:02 -05001084 else
1085 api.tag = 0;
1086
Subbaiah Venkata6902c692012-03-27 19:21:29 -07001087 rib_delete_ipv4 (api.type, api.flags, &p, nexthop_p, ifindex,
Feng Luc99f3482014-10-16 09:52:36 +08001088 vrf_id, api.safi);
Dinesh Dutt9ae85522015-05-19 17:47:22 -07001089 client->v4_route_del_cnt++;
ajs719e9742005-02-28 20:52:15 +00001090 return 0;
paul718e3742002-12-13 20:15:29 +00001091}
1092
1093/* Nexthop lookup for IPv4. */
ajs719e9742005-02-28 20:52:15 +00001094static int
Jafar Al-Gharaibeh56ae5c42016-06-17 16:01:12 -05001095zread_ipv4_nexthop_lookup (int cmd, struct zserv *client, u_short length,
Feng Luc99f3482014-10-16 09:52:36 +08001096 vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +00001097{
1098 struct in_addr addr;
Christian Frankebb97e462013-05-25 14:01:35 +00001099 char buf[BUFSIZ];
paul718e3742002-12-13 20:15:29 +00001100
1101 addr.s_addr = stream_get_ipv4 (client->ibuf);
Christian Frankebb97e462013-05-25 14:01:35 +00001102 if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
1103 zlog_debug("%s: looking up %s", __func__,
1104 inet_ntop (AF_INET, &addr, buf, BUFSIZ));
paul718e3742002-12-13 20:15:29 +00001105
Jafar Al-Gharaibeh56ae5c42016-06-17 16:01:12 -05001106 return zsend_ipv4_nexthop_lookup (client, addr, cmd, vrf_id);
Everton Marques4e5275b2014-07-01 15:15:52 -03001107}
1108
paul718e3742002-12-13 20:15:29 +00001109/* Nexthop lookup for IPv4. */
ajs719e9742005-02-28 20:52:15 +00001110static int
Feng Luc99f3482014-10-16 09:52:36 +08001111zread_ipv4_import_lookup (struct zserv *client, u_short length,
1112 vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +00001113{
1114 struct prefix_ipv4 p;
1115
1116 p.family = AF_INET;
1117 p.prefixlen = stream_getc (client->ibuf);
1118 p.prefix.s_addr = stream_get_ipv4 (client->ibuf);
1119
Feng Luc99f3482014-10-16 09:52:36 +08001120 return zsend_ipv4_import_lookup (client, &p, vrf_id);
paul718e3742002-12-13 20:15:29 +00001121}
1122
1123#ifdef HAVE_IPV6
1124/* Zebra server IPv6 prefix add function. */
ajs719e9742005-02-28 20:52:15 +00001125static int
Feng Luc99f3482014-10-16 09:52:36 +08001126zread_ipv6_add (struct zserv *client, u_short length, vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +00001127{
1128 int i;
1129 struct stream *s;
paul718e3742002-12-13 20:15:29 +00001130 struct in6_addr nexthop;
Ayan Banerjee34c5d892015-11-09 20:14:53 -05001131 struct rib *rib;
1132 u_char message;
1133 u_char gateway_num;
1134 u_char nexthop_type;
paul718e3742002-12-13 20:15:29 +00001135 struct prefix_ipv6 p;
Ayan Banerjee34c5d892015-11-09 20:14:53 -05001136 safi_t safi;
1137 static struct in6_addr nexthops[MULTIPATH_NUM];
1138 static unsigned int ifindices[MULTIPATH_NUM];
Dinesh Dutt9ae85522015-05-19 17:47:22 -07001139 int ret;
Ayan Banerjee34c5d892015-11-09 20:14:53 -05001140
1141 /* Get input stream. */
paul718e3742002-12-13 20:15:29 +00001142 s = client->ibuf;
Ayan Banerjee34c5d892015-11-09 20:14:53 -05001143
paul718e3742002-12-13 20:15:29 +00001144 memset (&nexthop, 0, sizeof (struct in6_addr));
1145
Ayan Banerjee34c5d892015-11-09 20:14:53 -05001146 /* Allocate new rib. */
1147 rib = XCALLOC (MTYPE_RIB, sizeof (struct rib));
paul718e3742002-12-13 20:15:29 +00001148
Ayan Banerjee34c5d892015-11-09 20:14:53 -05001149 /* Type, flags, message. */
1150 rib->type = stream_getc (s);
1151 rib->flags = stream_getc (s);
1152 message = stream_getc (s);
1153 safi = stream_getw (s);
1154 rib->uptime = time (NULL);
1155
1156 /* IPv6 prefix. */
paul718e3742002-12-13 20:15:29 +00001157 memset (&p, 0, sizeof (struct prefix_ipv6));
1158 p.family = AF_INET6;
1159 p.prefixlen = stream_getc (s);
1160 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
1161
Ayan Banerjee34c5d892015-11-09 20:14:53 -05001162 /* We need to give nh-addr, nh-ifindex with the same next-hop object
1163 * to the rib to ensure that IPv6 multipathing works; need to coalesce
1164 * these. Clients should send the same number of paired set of
1165 * next-hop-addr/next-hop-ifindices. */
1166 if (CHECK_FLAG (message, ZAPI_MESSAGE_NEXTHOP))
paul718e3742002-12-13 20:15:29 +00001167 {
Ayan Banerjee34c5d892015-11-09 20:14:53 -05001168 int nh_count = 0;
1169 int if_count = 0;
1170 int max_nh_if = 0;
1171 unsigned int ifindex;
paul718e3742002-12-13 20:15:29 +00001172
Ayan Banerjee34c5d892015-11-09 20:14:53 -05001173 gateway_num = stream_getc (s);
1174 for (i = 0; i < gateway_num; i++)
paul718e3742002-12-13 20:15:29 +00001175 {
1176 nexthop_type = stream_getc (s);
1177
1178 switch (nexthop_type)
1179 {
1180 case ZEBRA_NEXTHOP_IPV6:
1181 stream_get (&nexthop, s, 16);
Ayan Banerjee34c5d892015-11-09 20:14:53 -05001182 if (nh_count < MULTIPATH_NUM) {
1183 nexthops[nh_count++] = nexthop;
1184 }
paul718e3742002-12-13 20:15:29 +00001185 break;
1186 case ZEBRA_NEXTHOP_IFINDEX:
1187 ifindex = stream_getl (s);
Ayan Banerjee34c5d892015-11-09 20:14:53 -05001188 if (if_count < MULTIPATH_NUM) {
1189 ifindices[if_count++] = ifindex;
1190 }
paul718e3742002-12-13 20:15:29 +00001191 break;
1192 }
1193 }
Ayan Banerjee34c5d892015-11-09 20:14:53 -05001194
1195 max_nh_if = (nh_count > if_count) ? nh_count : if_count;
1196 for (i = 0; i < max_nh_if; i++)
1197 {
1198 if ((i < nh_count) && !IN6_IS_ADDR_UNSPECIFIED (&nexthops[i]))
1199 {
1200 if ((i < if_count) && ifindices[i])
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -05001201 rib_nexthop_ipv6_ifindex_add (rib, &nexthops[i], ifindices[i]);
Ayan Banerjee34c5d892015-11-09 20:14:53 -05001202 else
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -05001203 rib_nexthop_ipv6_add (rib, &nexthops[i]);
Ayan Banerjee34c5d892015-11-09 20:14:53 -05001204 }
1205 else
1206 {
1207 if ((i < if_count) && ifindices[i])
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -05001208 rib_nexthop_ifindex_add (rib, ifindices[i]);
Ayan Banerjee34c5d892015-11-09 20:14:53 -05001209 }
1210 }
paul718e3742002-12-13 20:15:29 +00001211 }
1212
Ayan Banerjee34c5d892015-11-09 20:14:53 -05001213 /* Distance. */
1214 if (CHECK_FLAG (message, ZAPI_MESSAGE_DISTANCE))
1215 rib->distance = stream_getc (s);
paul718e3742002-12-13 20:15:29 +00001216
Ayan Banerjee34c5d892015-11-09 20:14:53 -05001217 /* Metric. */
1218 if (CHECK_FLAG (message, ZAPI_MESSAGE_METRIC))
1219 rib->metric = stream_getl (s);
Timo Teräsb11f3b52015-11-02 16:50:07 +02001220
Ayan Banerjee34c5d892015-11-09 20:14:53 -05001221 if (CHECK_FLAG (message, ZAPI_MESSAGE_MTU))
1222 rib->mtu = stream_getl (s);
1223
Piotr Chytłaeefddcc2015-12-01 09:48:02 -05001224 /* Tag */
1225 if (CHECK_FLAG (message, ZAPI_MESSAGE_TAG))
Paul Jakma96d10602016-07-01 14:23:45 +01001226 rib->tag = stream_getl (s);
Christian Frankeddc160c2016-10-01 20:42:34 +02001227 else
1228 rib->tag = 0;
1229
Ayan Banerjee34c5d892015-11-09 20:14:53 -05001230 /* Table */
1231 rib->table=zebrad.rtm_table_default;
Dinesh Dutt9ae85522015-05-19 17:47:22 -07001232 ret = rib_add_ipv6_multipath (&p, rib, safi);
1233 /* Stats */
1234 if (ret > 0)
1235 client->v6_route_add_cnt++;
1236 else if (ret < 0)
1237 client->v6_route_upd8_cnt++;
1238
ajs719e9742005-02-28 20:52:15 +00001239 return 0;
paul718e3742002-12-13 20:15:29 +00001240}
1241
1242/* Zebra server IPv6 prefix delete function. */
ajs719e9742005-02-28 20:52:15 +00001243static int
Feng Luc99f3482014-10-16 09:52:36 +08001244zread_ipv6_delete (struct zserv *client, u_short length, vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +00001245{
1246 int i;
1247 struct stream *s;
1248 struct zapi_ipv6 api;
1249 struct in6_addr nexthop;
1250 unsigned long ifindex;
1251 struct prefix_ipv6 p;
1252
1253 s = client->ibuf;
1254 ifindex = 0;
1255 memset (&nexthop, 0, sizeof (struct in6_addr));
1256
1257 /* Type, flags, message. */
1258 api.type = stream_getc (s);
1259 api.flags = stream_getc (s);
1260 api.message = stream_getc (s);
G.Balajif768f362011-11-26 22:10:39 +04001261 api.safi = stream_getw (s);
paul718e3742002-12-13 20:15:29 +00001262
1263 /* IPv4 prefix. */
1264 memset (&p, 0, sizeof (struct prefix_ipv6));
1265 p.family = AF_INET6;
1266 p.prefixlen = stream_getc (s);
1267 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
1268
1269 /* Nexthop, ifindex, distance, metric. */
1270 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
1271 {
1272 u_char nexthop_type;
1273
1274 api.nexthop_num = stream_getc (s);
1275 for (i = 0; i < api.nexthop_num; i++)
1276 {
1277 nexthop_type = stream_getc (s);
1278
1279 switch (nexthop_type)
1280 {
1281 case ZEBRA_NEXTHOP_IPV6:
1282 stream_get (&nexthop, s, 16);
1283 break;
1284 case ZEBRA_NEXTHOP_IFINDEX:
1285 ifindex = stream_getl (s);
1286 break;
1287 }
1288 }
1289 }
1290
Piotr Chytłaeefddcc2015-12-01 09:48:02 -05001291 /* Distance. */
paul718e3742002-12-13 20:15:29 +00001292 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
1293 api.distance = stream_getc (s);
1294 else
1295 api.distance = 0;
Piotr Chytłaeefddcc2015-12-01 09:48:02 -05001296
1297 /* Metric. */
paul718e3742002-12-13 20:15:29 +00001298 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
1299 api.metric = stream_getl (s);
1300 else
1301 api.metric = 0;
1302
Piotr Chytłaeefddcc2015-12-01 09:48:02 -05001303 /* tag */
1304 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_TAG))
Paul Jakma96d10602016-07-01 14:23:45 +01001305 api.tag = stream_getl (s);
Piotr Chytłaeefddcc2015-12-01 09:48:02 -05001306 else
1307 api.tag = 0;
1308
paul718e3742002-12-13 20:15:29 +00001309 if (IN6_IS_ADDR_UNSPECIFIED (&nexthop))
Feng Luc99f3482014-10-16 09:52:36 +08001310 rib_delete_ipv6 (api.type, api.flags, &p, NULL, ifindex, vrf_id,
Feng Lu0d0686f2015-05-22 11:40:02 +02001311 api.safi);
paul718e3742002-12-13 20:15:29 +00001312 else
Feng Luc99f3482014-10-16 09:52:36 +08001313 rib_delete_ipv6 (api.type, api.flags, &p, &nexthop, ifindex, vrf_id,
Feng Lu0d0686f2015-05-22 11:40:02 +02001314 api.safi);
Dinesh Dutt9ae85522015-05-19 17:47:22 -07001315
1316 client->v6_route_del_cnt++;
ajs719e9742005-02-28 20:52:15 +00001317 return 0;
paul718e3742002-12-13 20:15:29 +00001318}
1319
ajs719e9742005-02-28 20:52:15 +00001320static int
Feng Luc99f3482014-10-16 09:52:36 +08001321zread_ipv6_nexthop_lookup (struct zserv *client, u_short length,
1322 vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +00001323{
1324 struct in6_addr addr;
1325 char buf[BUFSIZ];
1326
1327 stream_get (&addr, client->ibuf, 16);
Christian Frankea5207082013-04-11 08:24:29 +00001328 if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
1329 zlog_debug("%s: looking up %s", __func__,
1330 inet_ntop (AF_INET6, &addr, buf, BUFSIZ));
paul718e3742002-12-13 20:15:29 +00001331
Feng Luc99f3482014-10-16 09:52:36 +08001332 return zsend_ipv6_nexthop_lookup (client, &addr, vrf_id);
paul718e3742002-12-13 20:15:29 +00001333}
1334#endif /* HAVE_IPV6 */
1335
hasso18a6dce2004-10-03 18:18:34 +00001336/* Register zebra server router-id information. Send current router-id */
ajs719e9742005-02-28 20:52:15 +00001337static int
Feng Luc99f3482014-10-16 09:52:36 +08001338zread_router_id_add (struct zserv *client, u_short length, vrf_id_t vrf_id)
hasso18a6dce2004-10-03 18:18:34 +00001339{
1340 struct prefix p;
1341
1342 /* Router-id information is needed. */
Feng Luc99f3482014-10-16 09:52:36 +08001343 vrf_bitmap_set (client->ridinfo, vrf_id);
hasso18a6dce2004-10-03 18:18:34 +00001344
Feng Luc99f3482014-10-16 09:52:36 +08001345 router_id_get (&p, vrf_id);
hasso18a6dce2004-10-03 18:18:34 +00001346
Feng Luc99f3482014-10-16 09:52:36 +08001347 return zsend_router_id_update (client, &p, vrf_id);
hasso18a6dce2004-10-03 18:18:34 +00001348}
1349
1350/* Unregister zebra server router-id information. */
ajs719e9742005-02-28 20:52:15 +00001351static int
Feng Luc99f3482014-10-16 09:52:36 +08001352zread_router_id_delete (struct zserv *client, u_short length, vrf_id_t vrf_id)
hasso18a6dce2004-10-03 18:18:34 +00001353{
Feng Luc99f3482014-10-16 09:52:36 +08001354 vrf_bitmap_unset (client->ridinfo, vrf_id);
ajs719e9742005-02-28 20:52:15 +00001355 return 0;
hasso18a6dce2004-10-03 18:18:34 +00001356}
1357
Vyacheslav Trushkin2ea1ab12011-12-11 18:48:47 +04001358/* Tie up route-type and client->sock */
1359static void
1360zread_hello (struct zserv *client)
1361{
1362 /* type of protocol (lib/zebra.h) */
1363 u_char proto;
1364 proto = stream_getc (client->ibuf);
1365
1366 /* accept only dynamic routing protocols */
1367 if ((proto < ZEBRA_ROUTE_MAX)
1368 && (proto > ZEBRA_ROUTE_STATIC))
1369 {
1370 zlog_notice ("client %d says hello and bids fair to announce only %s routes",
1371 client->sock, zebra_route_string(proto));
1372
1373 /* if route-type was binded by other client */
1374 if (route_type_oaths[proto])
1375 zlog_warn ("sender of %s routes changed %c->%c",
1376 zebra_route_string(proto), route_type_oaths[proto],
1377 client->sock);
1378
1379 route_type_oaths[proto] = client->sock;
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -05001380 client->proto = proto;
Vyacheslav Trushkin2ea1ab12011-12-11 18:48:47 +04001381 }
1382}
1383
Feng Luc99f3482014-10-16 09:52:36 +08001384/* Unregister all information in a VRF. */
1385static int
1386zread_vrf_unregister (struct zserv *client, u_short length, vrf_id_t vrf_id)
1387{
1388 int i;
1389
1390 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
1391 vrf_bitmap_unset (client->redist[i], vrf_id);
1392 vrf_bitmap_unset (client->redist_default, vrf_id);
1393 vrf_bitmap_unset (client->ifinfo, vrf_id);
1394 vrf_bitmap_unset (client->ridinfo, vrf_id);
1395
1396 return 0;
1397}
1398
Vyacheslav Trushkin2ea1ab12011-12-11 18:48:47 +04001399/* If client sent routes of specific type, zebra removes it
1400 * and returns number of deleted routes.
1401 */
1402static void
1403zebra_score_rib (int client_sock)
1404{
1405 int i;
1406
1407 for (i = ZEBRA_ROUTE_RIP; i < ZEBRA_ROUTE_MAX; i++)
1408 if (client_sock == route_type_oaths[i])
1409 {
1410 zlog_notice ("client %d disconnected. %lu %s routes removed from the rib",
1411 client_sock, rib_score_proto (i), zebra_route_string (i));
1412 route_type_oaths[i] = 0;
1413 break;
1414 }
1415}
1416
paul718e3742002-12-13 20:15:29 +00001417/* Close zebra client. */
paulb9df2d22004-05-09 09:09:59 +00001418static void
paul718e3742002-12-13 20:15:29 +00001419zebra_client_close (struct zserv *client)
1420{
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -05001421 zebra_cleanup_rnh_client(0, AF_INET, client);
1422 zebra_cleanup_rnh_client(0, AF_INET6, client);
1423
paul718e3742002-12-13 20:15:29 +00001424 /* Close file descriptor. */
1425 if (client->sock)
1426 {
1427 close (client->sock);
Vyacheslav Trushkin2ea1ab12011-12-11 18:48:47 +04001428 zebra_score_rib (client->sock);
paul718e3742002-12-13 20:15:29 +00001429 client->sock = -1;
1430 }
1431
1432 /* Free stream buffers. */
1433 if (client->ibuf)
1434 stream_free (client->ibuf);
1435 if (client->obuf)
1436 stream_free (client->obuf);
ajs719e9742005-02-28 20:52:15 +00001437 if (client->wb)
1438 buffer_free(client->wb);
paul718e3742002-12-13 20:15:29 +00001439
1440 /* Release threads. */
1441 if (client->t_read)
1442 thread_cancel (client->t_read);
1443 if (client->t_write)
1444 thread_cancel (client->t_write);
ajs719e9742005-02-28 20:52:15 +00001445 if (client->t_suicide)
1446 thread_cancel (client->t_suicide);
paul718e3742002-12-13 20:15:29 +00001447
1448 /* Free client structure. */
paulb21b19c2003-06-15 01:28:29 +00001449 listnode_delete (zebrad.client_list, client);
paul718e3742002-12-13 20:15:29 +00001450 XFREE (0, client);
1451}
1452
1453/* Make new client. */
paulb9df2d22004-05-09 09:09:59 +00001454static void
paul718e3742002-12-13 20:15:29 +00001455zebra_client_create (int sock)
1456{
1457 struct zserv *client;
Feng Luc99f3482014-10-16 09:52:36 +08001458 int i;
paul718e3742002-12-13 20:15:29 +00001459
David Lamparter23757db2016-02-24 06:26:02 +01001460 client = XCALLOC (MTYPE_TMP, sizeof (struct zserv));
paul718e3742002-12-13 20:15:29 +00001461
1462 /* Make client input/output buffer. */
1463 client->sock = sock;
1464 client->ibuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
1465 client->obuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
ajs719e9742005-02-28 20:52:15 +00001466 client->wb = buffer_new(0);
paul718e3742002-12-13 20:15:29 +00001467
1468 /* Set table number. */
paulb21b19c2003-06-15 01:28:29 +00001469 client->rtm_table = zebrad.rtm_table_default;
paul718e3742002-12-13 20:15:29 +00001470
Feng Luc99f3482014-10-16 09:52:36 +08001471 /* Initialize flags */
1472 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
1473 client->redist[i] = vrf_bitmap_init ();
1474 client->redist_default = vrf_bitmap_init ();
1475 client->ifinfo = vrf_bitmap_init ();
1476 client->ridinfo = vrf_bitmap_init ();
Dinesh Dutt9ae85522015-05-19 17:47:22 -07001477 client->connect_time = quagga_time(NULL);
Feng Luc99f3482014-10-16 09:52:36 +08001478
paul718e3742002-12-13 20:15:29 +00001479 /* Add this client to linked list. */
paulb21b19c2003-06-15 01:28:29 +00001480 listnode_add (zebrad.client_list, client);
paul718e3742002-12-13 20:15:29 +00001481
1482 /* Make new read thread. */
1483 zebra_event (ZEBRA_READ, sock, client);
1484}
1485
1486/* Handler of zebra service request. */
paulb9df2d22004-05-09 09:09:59 +00001487static int
paul718e3742002-12-13 20:15:29 +00001488zebra_client_read (struct thread *thread)
1489{
1490 int sock;
1491 struct zserv *client;
ajs57a14772005-04-10 15:01:56 +00001492 size_t already;
paulc1b98002006-01-16 01:54:02 +00001493 uint16_t length, command;
1494 uint8_t marker, version;
Feng Luc99f3482014-10-16 09:52:36 +08001495 vrf_id_t vrf_id;
paul718e3742002-12-13 20:15:29 +00001496
1497 /* Get thread data. Reset reading thread because I'm running. */
1498 sock = THREAD_FD (thread);
1499 client = THREAD_ARG (thread);
1500 client->t_read = NULL;
1501
ajs719e9742005-02-28 20:52:15 +00001502 if (client->t_suicide)
paul718e3742002-12-13 20:15:29 +00001503 {
ajs719e9742005-02-28 20:52:15 +00001504 zebra_client_close(client);
paul718e3742002-12-13 20:15:29 +00001505 return -1;
1506 }
ajs719e9742005-02-28 20:52:15 +00001507
1508 /* Read length and command (if we don't have it already). */
ajs57a14772005-04-10 15:01:56 +00001509 if ((already = stream_get_endp(client->ibuf)) < ZEBRA_HEADER_SIZE)
ajs719e9742005-02-28 20:52:15 +00001510 {
ajs57a14772005-04-10 15:01:56 +00001511 ssize_t nbyte;
ajs719e9742005-02-28 20:52:15 +00001512 if (((nbyte = stream_read_try (client->ibuf, sock,
ajs57a14772005-04-10 15:01:56 +00001513 ZEBRA_HEADER_SIZE-already)) == 0) ||
ajs719e9742005-02-28 20:52:15 +00001514 (nbyte == -1))
1515 {
1516 if (IS_ZEBRA_DEBUG_EVENT)
1517 zlog_debug ("connection closed socket [%d]", sock);
1518 zebra_client_close (client);
1519 return -1;
1520 }
ajs57a14772005-04-10 15:01:56 +00001521 if (nbyte != (ssize_t)(ZEBRA_HEADER_SIZE-already))
ajs719e9742005-02-28 20:52:15 +00001522 {
1523 /* Try again later. */
1524 zebra_event (ZEBRA_READ, sock, client);
1525 return 0;
1526 }
ajs57a14772005-04-10 15:01:56 +00001527 already = ZEBRA_HEADER_SIZE;
ajs719e9742005-02-28 20:52:15 +00001528 }
1529
1530 /* Reset to read from the beginning of the incoming packet. */
1531 stream_set_getp(client->ibuf, 0);
1532
paulc1b98002006-01-16 01:54:02 +00001533 /* Fetch header values */
paul718e3742002-12-13 20:15:29 +00001534 length = stream_getw (client->ibuf);
paulc1b98002006-01-16 01:54:02 +00001535 marker = stream_getc (client->ibuf);
1536 version = stream_getc (client->ibuf);
Feng Luc99f3482014-10-16 09:52:36 +08001537 vrf_id = stream_getw (client->ibuf);
paulc1b98002006-01-16 01:54:02 +00001538 command = stream_getw (client->ibuf);
paul718e3742002-12-13 20:15:29 +00001539
paulc1b98002006-01-16 01:54:02 +00001540 if (marker != ZEBRA_HEADER_MARKER || version != ZSERV_VERSION)
1541 {
1542 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
1543 __func__, sock, marker, version);
1544 zebra_client_close (client);
1545 return -1;
1546 }
ajs719e9742005-02-28 20:52:15 +00001547 if (length < ZEBRA_HEADER_SIZE)
paul718e3742002-12-13 20:15:29 +00001548 {
ajs57a14772005-04-10 15:01:56 +00001549 zlog_warn("%s: socket %d message length %u is less than header size %d",
1550 __func__, sock, length, ZEBRA_HEADER_SIZE);
1551 zebra_client_close (client);
1552 return -1;
1553 }
1554 if (length > STREAM_SIZE(client->ibuf))
1555 {
1556 zlog_warn("%s: socket %d message length %u exceeds buffer size %lu",
1557 __func__, sock, length, (u_long)STREAM_SIZE(client->ibuf));
paul718e3742002-12-13 20:15:29 +00001558 zebra_client_close (client);
1559 return -1;
1560 }
1561
paul718e3742002-12-13 20:15:29 +00001562 /* Read rest of data. */
ajs57a14772005-04-10 15:01:56 +00001563 if (already < length)
paul718e3742002-12-13 20:15:29 +00001564 {
ajs57a14772005-04-10 15:01:56 +00001565 ssize_t nbyte;
1566 if (((nbyte = stream_read_try (client->ibuf, sock,
1567 length-already)) == 0) ||
1568 (nbyte == -1))
paul718e3742002-12-13 20:15:29 +00001569 {
1570 if (IS_ZEBRA_DEBUG_EVENT)
ajsb6178002004-12-07 21:12:56 +00001571 zlog_debug ("connection closed [%d] when reading zebra data", sock);
paul718e3742002-12-13 20:15:29 +00001572 zebra_client_close (client);
1573 return -1;
1574 }
ajs57a14772005-04-10 15:01:56 +00001575 if (nbyte != (ssize_t)(length-already))
ajs719e9742005-02-28 20:52:15 +00001576 {
1577 /* Try again later. */
1578 zebra_event (ZEBRA_READ, sock, client);
1579 return 0;
1580 }
paul718e3742002-12-13 20:15:29 +00001581 }
1582
ajs719e9742005-02-28 20:52:15 +00001583 length -= ZEBRA_HEADER_SIZE;
1584
paul718e3742002-12-13 20:15:29 +00001585 /* Debug packet information. */
1586 if (IS_ZEBRA_DEBUG_EVENT)
ajsb6178002004-12-07 21:12:56 +00001587 zlog_debug ("zebra message comes from socket [%d]", sock);
paul718e3742002-12-13 20:15:29 +00001588
1589 if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
Feng Luc99f3482014-10-16 09:52:36 +08001590 zlog_debug ("zebra message received [%s] %d in VRF %u",
1591 zserv_command_string (command), length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001592
Dinesh Dutt9ae85522015-05-19 17:47:22 -07001593 client->last_read_time = quagga_time(NULL);
1594 client->last_read_cmd = command;
1595
paul718e3742002-12-13 20:15:29 +00001596 switch (command)
1597 {
hasso18a6dce2004-10-03 18:18:34 +00001598 case ZEBRA_ROUTER_ID_ADD:
Feng Luc99f3482014-10-16 09:52:36 +08001599 zread_router_id_add (client, length, vrf_id);
hasso18a6dce2004-10-03 18:18:34 +00001600 break;
1601 case ZEBRA_ROUTER_ID_DELETE:
Feng Luc99f3482014-10-16 09:52:36 +08001602 zread_router_id_delete (client, length, vrf_id);
hasso18a6dce2004-10-03 18:18:34 +00001603 break;
paul718e3742002-12-13 20:15:29 +00001604 case ZEBRA_INTERFACE_ADD:
Feng Luc99f3482014-10-16 09:52:36 +08001605 zread_interface_add (client, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001606 break;
1607 case ZEBRA_INTERFACE_DELETE:
Feng Luc99f3482014-10-16 09:52:36 +08001608 zread_interface_delete (client, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001609 break;
1610 case ZEBRA_IPV4_ROUTE_ADD:
Feng Luc99f3482014-10-16 09:52:36 +08001611 zread_ipv4_add (client, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001612 break;
1613 case ZEBRA_IPV4_ROUTE_DELETE:
Feng Luc99f3482014-10-16 09:52:36 +08001614 zread_ipv4_delete (client, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001615 break;
1616#ifdef HAVE_IPV6
1617 case ZEBRA_IPV6_ROUTE_ADD:
Feng Luc99f3482014-10-16 09:52:36 +08001618 zread_ipv6_add (client, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001619 break;
1620 case ZEBRA_IPV6_ROUTE_DELETE:
Feng Luc99f3482014-10-16 09:52:36 +08001621 zread_ipv6_delete (client, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001622 break;
1623#endif /* HAVE_IPV6 */
1624 case ZEBRA_REDISTRIBUTE_ADD:
Feng Luc99f3482014-10-16 09:52:36 +08001625 zebra_redistribute_add (command, client, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001626 break;
1627 case ZEBRA_REDISTRIBUTE_DELETE:
Feng Luc99f3482014-10-16 09:52:36 +08001628 zebra_redistribute_delete (command, client, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001629 break;
1630 case ZEBRA_REDISTRIBUTE_DEFAULT_ADD:
Feng Luc99f3482014-10-16 09:52:36 +08001631 zebra_redistribute_default_add (command, client, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001632 break;
1633 case ZEBRA_REDISTRIBUTE_DEFAULT_DELETE:
Feng Luc99f3482014-10-16 09:52:36 +08001634 zebra_redistribute_default_delete (command, client, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001635 break;
1636 case ZEBRA_IPV4_NEXTHOP_LOOKUP:
Everton Marques4e5275b2014-07-01 15:15:52 -03001637 case ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB:
Jafar Al-Gharaibeh56ae5c42016-06-17 16:01:12 -05001638 zread_ipv4_nexthop_lookup (command, client, length, vrf_id);
Everton Marques4e5275b2014-07-01 15:15:52 -03001639 break;
paul718e3742002-12-13 20:15:29 +00001640#ifdef HAVE_IPV6
1641 case ZEBRA_IPV6_NEXTHOP_LOOKUP:
Feng Luc99f3482014-10-16 09:52:36 +08001642 zread_ipv6_nexthop_lookup (client, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001643 break;
1644#endif /* HAVE_IPV6 */
1645 case ZEBRA_IPV4_IMPORT_LOOKUP:
Feng Luc99f3482014-10-16 09:52:36 +08001646 zread_ipv4_import_lookup (client, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001647 break;
Vyacheslav Trushkin2ea1ab12011-12-11 18:48:47 +04001648 case ZEBRA_HELLO:
1649 zread_hello (client);
1650 break;
Feng Luc99f3482014-10-16 09:52:36 +08001651 case ZEBRA_VRF_UNREGISTER:
1652 zread_vrf_unregister (client, length, vrf_id);
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -05001653 case ZEBRA_NEXTHOP_REGISTER:
1654 zserv_nexthop_register(client, sock, length, vrf_id);
1655 break;
1656 case ZEBRA_NEXTHOP_UNREGISTER:
1657 zserv_nexthop_unregister(client, sock, length);
Feng Luc99f3482014-10-16 09:52:36 +08001658 break;
paul718e3742002-12-13 20:15:29 +00001659 default:
1660 zlog_info ("Zebra received unknown command %d", command);
1661 break;
1662 }
1663
ajs719e9742005-02-28 20:52:15 +00001664 if (client->t_suicide)
1665 {
1666 /* No need to wait for thread callback, just kill immediately. */
1667 zebra_client_close(client);
1668 return -1;
1669 }
1670
paul718e3742002-12-13 20:15:29 +00001671 stream_reset (client->ibuf);
1672 zebra_event (ZEBRA_READ, sock, client);
paul718e3742002-12-13 20:15:29 +00001673 return 0;
1674}
1675
paul718e3742002-12-13 20:15:29 +00001676
1677/* Accept code of zebra server socket. */
paulb9df2d22004-05-09 09:09:59 +00001678static int
paul718e3742002-12-13 20:15:29 +00001679zebra_accept (struct thread *thread)
1680{
1681 int accept_sock;
1682 int client_sock;
1683 struct sockaddr_in client;
1684 socklen_t len;
1685
1686 accept_sock = THREAD_FD (thread);
1687
ajs719e9742005-02-28 20:52:15 +00001688 /* Reregister myself. */
1689 zebra_event (ZEBRA_SERV, accept_sock, NULL);
1690
paul718e3742002-12-13 20:15:29 +00001691 len = sizeof (struct sockaddr_in);
1692 client_sock = accept (accept_sock, (struct sockaddr *) &client, &len);
1693
1694 if (client_sock < 0)
1695 {
ajs6099b3b2004-11-20 02:06:59 +00001696 zlog_warn ("Can't accept zebra socket: %s", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001697 return -1;
1698 }
1699
paulccf35572003-03-01 11:42:20 +00001700 /* Make client socket non-blocking. */
ajs719e9742005-02-28 20:52:15 +00001701 set_nonblocking(client_sock);
paul865b8522005-01-05 08:30:35 +00001702
paul718e3742002-12-13 20:15:29 +00001703 /* Create new zebra client. */
1704 zebra_client_create (client_sock);
1705
paul718e3742002-12-13 20:15:29 +00001706 return 0;
1707}
1708
paulb9df2d22004-05-09 09:09:59 +00001709#ifdef HAVE_TCP_ZEBRA
paul718e3742002-12-13 20:15:29 +00001710/* Make zebra's server socket. */
paulb9df2d22004-05-09 09:09:59 +00001711static void
paul718e3742002-12-13 20:15:29 +00001712zebra_serv ()
1713{
1714 int ret;
1715 int accept_sock;
1716 struct sockaddr_in addr;
1717
1718 accept_sock = socket (AF_INET, SOCK_STREAM, 0);
1719
1720 if (accept_sock < 0)
1721 {
paul3d1dc852005-04-05 00:45:23 +00001722 zlog_warn ("Can't create zserv stream socket: %s",
1723 safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001724 zlog_warn ("zebra can't provice full functionality due to above error");
1725 return;
1726 }
1727
Vyacheslav Trushkin2ea1ab12011-12-11 18:48:47 +04001728 memset (&route_type_oaths, 0, sizeof (route_type_oaths));
paul718e3742002-12-13 20:15:29 +00001729 memset (&addr, 0, sizeof (struct sockaddr_in));
1730 addr.sin_family = AF_INET;
1731 addr.sin_port = htons (ZEBRA_PORT);
Paul Jakma6f0e3f62007-05-10 02:38:51 +00001732#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
paul718e3742002-12-13 20:15:29 +00001733 addr.sin_len = sizeof (struct sockaddr_in);
Paul Jakma6f0e3f62007-05-10 02:38:51 +00001734#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
paul718e3742002-12-13 20:15:29 +00001735 addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
1736
1737 sockopt_reuseaddr (accept_sock);
1738 sockopt_reuseport (accept_sock);
1739
pauledd7c242003-06-04 13:59:38 +00001740 if ( zserv_privs.change(ZPRIVS_RAISE) )
1741 zlog (NULL, LOG_ERR, "Can't raise privileges");
1742
paul718e3742002-12-13 20:15:29 +00001743 ret = bind (accept_sock, (struct sockaddr *)&addr,
1744 sizeof (struct sockaddr_in));
1745 if (ret < 0)
1746 {
paul3d1dc852005-04-05 00:45:23 +00001747 zlog_warn ("Can't bind to stream socket: %s",
1748 safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001749 zlog_warn ("zebra can't provice full functionality due to above error");
1750 close (accept_sock); /* Avoid sd leak. */
1751 return;
1752 }
pauledd7c242003-06-04 13:59:38 +00001753
1754 if ( zserv_privs.change(ZPRIVS_LOWER) )
1755 zlog (NULL, LOG_ERR, "Can't lower privileges");
paul718e3742002-12-13 20:15:29 +00001756
1757 ret = listen (accept_sock, 1);
1758 if (ret < 0)
1759 {
paul3d1dc852005-04-05 00:45:23 +00001760 zlog_warn ("Can't listen to stream socket: %s",
1761 safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001762 zlog_warn ("zebra can't provice full functionality due to above error");
1763 close (accept_sock); /* Avoid sd leak. */
1764 return;
1765 }
1766
1767 zebra_event (ZEBRA_SERV, accept_sock, NULL);
1768}
David Lamparter4b6c3322015-04-21 09:47:57 +02001769#else /* HAVE_TCP_ZEBRA */
paul718e3742002-12-13 20:15:29 +00001770
1771/* For sockaddr_un. */
1772#include <sys/un.h>
1773
1774/* zebra server UNIX domain socket. */
paulb9df2d22004-05-09 09:09:59 +00001775static void
hassofce954f2004-10-07 20:29:24 +00001776zebra_serv_un (const char *path)
paul718e3742002-12-13 20:15:29 +00001777{
1778 int ret;
1779 int sock, len;
1780 struct sockaddr_un serv;
1781 mode_t old_mask;
1782
1783 /* First of all, unlink existing socket */
1784 unlink (path);
1785
1786 /* Set umask */
1787 old_mask = umask (0077);
1788
1789 /* Make UNIX domain socket. */
1790 sock = socket (AF_UNIX, SOCK_STREAM, 0);
1791 if (sock < 0)
1792 {
paul3d1dc852005-04-05 00:45:23 +00001793 zlog_warn ("Can't create zserv unix socket: %s",
1794 safe_strerror (errno));
1795 zlog_warn ("zebra can't provide full functionality due to above error");
paul718e3742002-12-13 20:15:29 +00001796 return;
1797 }
1798
Vyacheslav Trushkin2ea1ab12011-12-11 18:48:47 +04001799 memset (&route_type_oaths, 0, sizeof (route_type_oaths));
1800
paul718e3742002-12-13 20:15:29 +00001801 /* Make server socket. */
1802 memset (&serv, 0, sizeof (struct sockaddr_un));
1803 serv.sun_family = AF_UNIX;
1804 strncpy (serv.sun_path, path, strlen (path));
Paul Jakma6f0e3f62007-05-10 02:38:51 +00001805#ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
paul718e3742002-12-13 20:15:29 +00001806 len = serv.sun_len = SUN_LEN(&serv);
1807#else
1808 len = sizeof (serv.sun_family) + strlen (serv.sun_path);
Paul Jakma6f0e3f62007-05-10 02:38:51 +00001809#endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
paul718e3742002-12-13 20:15:29 +00001810
1811 ret = bind (sock, (struct sockaddr *) &serv, len);
1812 if (ret < 0)
1813 {
paul3d1dc852005-04-05 00:45:23 +00001814 zlog_warn ("Can't bind to unix socket %s: %s",
1815 path, safe_strerror (errno));
1816 zlog_warn ("zebra can't provide full functionality due to above error");
paul718e3742002-12-13 20:15:29 +00001817 close (sock);
1818 return;
1819 }
1820
1821 ret = listen (sock, 5);
1822 if (ret < 0)
1823 {
paul3d1dc852005-04-05 00:45:23 +00001824 zlog_warn ("Can't listen to unix socket %s: %s",
1825 path, safe_strerror (errno));
1826 zlog_warn ("zebra can't provide full functionality due to above error");
paul718e3742002-12-13 20:15:29 +00001827 close (sock);
1828 return;
1829 }
1830
1831 umask (old_mask);
1832
1833 zebra_event (ZEBRA_SERV, sock, NULL);
1834}
David Lamparter4b6c3322015-04-21 09:47:57 +02001835#endif /* HAVE_TCP_ZEBRA */
David Lamparter6b0655a2014-06-04 06:53:35 +02001836
paul718e3742002-12-13 20:15:29 +00001837
paulb9df2d22004-05-09 09:09:59 +00001838static void
paul718e3742002-12-13 20:15:29 +00001839zebra_event (enum event event, int sock, struct zserv *client)
1840{
1841 switch (event)
1842 {
1843 case ZEBRA_SERV:
paulb21b19c2003-06-15 01:28:29 +00001844 thread_add_read (zebrad.master, zebra_accept, client, sock);
paul718e3742002-12-13 20:15:29 +00001845 break;
1846 case ZEBRA_READ:
1847 client->t_read =
paulb21b19c2003-06-15 01:28:29 +00001848 thread_add_read (zebrad.master, zebra_client_read, client, sock);
paul718e3742002-12-13 20:15:29 +00001849 break;
1850 case ZEBRA_WRITE:
1851 /**/
1852 break;
1853 }
1854}
David Lamparter6b0655a2014-06-04 06:53:35 +02001855
Dinesh Dutt9ae85522015-05-19 17:47:22 -07001856#define ZEBRA_TIME_BUF 32
1857static char *
1858zserv_time_buf(time_t *time1, char *buf, int buflen)
1859{
1860 struct tm *tm;
1861 time_t now;
1862
1863 assert (buf != NULL);
1864 assert (buflen >= ZEBRA_TIME_BUF);
1865 assert (time1 != NULL);
1866
1867 if (!*time1)
1868 {
1869 snprintf(buf, buflen, "never ");
1870 return (buf);
1871 }
1872
1873 now = quagga_time(NULL);
1874 now -= *time1;
1875 tm = gmtime(&now);
1876
1877 /* Making formatted timer strings. */
1878#define ONE_DAY_SECOND 60*60*24
1879#define ONE_WEEK_SECOND 60*60*24*7
1880
1881 if (now < ONE_DAY_SECOND)
1882 snprintf (buf, buflen, "%02d:%02d:%02d",
1883 tm->tm_hour, tm->tm_min, tm->tm_sec);
1884 else if (now < ONE_WEEK_SECOND)
1885 snprintf (buf, buflen, "%dd%02dh%02dm",
1886 tm->tm_yday, tm->tm_hour, tm->tm_min);
1887 else
1888 snprintf (buf, buflen, "%02dw%dd%02dh",
1889 tm->tm_yday/7, tm->tm_yday - ((tm->tm_yday/7) * 7), tm->tm_hour);
1890 return buf;
1891}
1892
1893static void
1894zebra_show_client_detail (struct vty *vty, struct zserv *client)
1895{
1896 char cbuf[ZEBRA_TIME_BUF], rbuf[ZEBRA_TIME_BUF];
1897 char wbuf[ZEBRA_TIME_BUF], nhbuf[ZEBRA_TIME_BUF], mbuf[ZEBRA_TIME_BUF];
1898
1899 vty_out (vty, "Client: %s %s",
1900 zebra_route_string(client->proto), VTY_NEWLINE);
1901 vty_out (vty, "------------------------ %s", VTY_NEWLINE);
1902 vty_out (vty, "FD: %d %s", client->sock, VTY_NEWLINE);
1903 vty_out (vty, "Route Table ID: %d %s", client->rtm_table, VTY_NEWLINE);
1904
1905 vty_out (vty, "Connect Time: %s %s",
1906 zserv_time_buf(&client->connect_time, cbuf, ZEBRA_TIME_BUF),
1907 VTY_NEWLINE);
1908 if (client->nh_reg_time)
1909 {
1910 vty_out (vty, "Nexthop Registry Time: %s %s",
1911 zserv_time_buf(&client->nh_reg_time, nhbuf, ZEBRA_TIME_BUF),
1912 VTY_NEWLINE);
1913 if (client->nh_last_upd_time)
1914 vty_out (vty, "Nexthop Last Update Time: %s %s",
1915 zserv_time_buf(&client->nh_last_upd_time, mbuf, ZEBRA_TIME_BUF),
1916 VTY_NEWLINE);
1917 else
1918 vty_out (vty, "No Nexthop Update sent%s", VTY_NEWLINE);
1919 }
1920 else
1921 vty_out (vty, "Not registered for Nexthop Updates%s", VTY_NEWLINE);
1922
1923 vty_out (vty, "Last Msg Rx Time: %s %s",
1924 zserv_time_buf(&client->last_read_time, rbuf, ZEBRA_TIME_BUF),
1925 VTY_NEWLINE);
1926 vty_out (vty, "Last Msg Tx Time: %s %s",
1927 zserv_time_buf(&client->last_write_time, wbuf, ZEBRA_TIME_BUF),
1928 VTY_NEWLINE);
1929 if (client->last_read_time)
1930 vty_out (vty, "Last Rcvd Cmd: %s %s",
1931 zserv_command_string(client->last_read_cmd), VTY_NEWLINE);
1932 if (client->last_write_time)
1933 vty_out (vty, "Last Sent Cmd: %s %s",
1934 zserv_command_string(client->last_write_cmd), VTY_NEWLINE);
1935 vty_out (vty, "%s", VTY_NEWLINE);
1936
1937 vty_out (vty, "Type Add Update Del %s", VTY_NEWLINE);
1938 vty_out (vty, "================================================== %s", VTY_NEWLINE);
1939 vty_out (vty, "IPv4 %-12d%-12d%-12d%s", client->v4_route_add_cnt,
1940 client->v4_route_upd8_cnt, client->v4_route_del_cnt, VTY_NEWLINE);
1941 vty_out (vty, "IPv6 %-12d%-12d%-12d%s", client->v6_route_add_cnt,
1942 client->v6_route_upd8_cnt, client->v6_route_del_cnt, VTY_NEWLINE);
1943 vty_out (vty, "Redist:v4 %-12d%-12d%-12d%s", client->redist_v4_add_cnt, 0,
1944 client->redist_v4_del_cnt, VTY_NEWLINE);
1945 vty_out (vty, "Redist:v6 %-12d%-12d%-12d%s", client->redist_v6_add_cnt, 0,
1946 client->redist_v6_del_cnt, VTY_NEWLINE);
1947 vty_out (vty, "Connected %-12d%-12d%-12d%s", client->ifadd_cnt, 0,
1948 client->ifdel_cnt, VTY_NEWLINE);
1949 vty_out (vty, "Interface Up Notifications: %d%s", client->ifup_cnt,
1950 VTY_NEWLINE);
1951 vty_out (vty, "Interface Down Notifications: %d%s", client->ifdown_cnt,
1952 VTY_NEWLINE);
1953
1954 vty_out (vty, "%s", VTY_NEWLINE);
1955 return;
1956}
1957
1958static void
1959zebra_show_client_brief (struct vty *vty, struct zserv *client)
1960{
1961 char cbuf[ZEBRA_TIME_BUF], rbuf[ZEBRA_TIME_BUF];
1962 char wbuf[ZEBRA_TIME_BUF];
1963
1964 vty_out (vty, "%-8s%12s %12s%12s%8d/%-8d%8d/%-8d%s",
1965 zebra_route_string(client->proto),
1966 zserv_time_buf(&client->connect_time, cbuf, ZEBRA_TIME_BUF),
1967 zserv_time_buf(&client->last_read_time, rbuf, ZEBRA_TIME_BUF),
1968 zserv_time_buf(&client->last_write_time, wbuf, ZEBRA_TIME_BUF),
1969 client->v4_route_add_cnt+client->v4_route_upd8_cnt,
1970 client->v4_route_del_cnt,
1971 client->v6_route_add_cnt+client->v6_route_upd8_cnt,
1972 client->v6_route_del_cnt, VTY_NEWLINE);
1973
1974}
1975
1976
paul718e3742002-12-13 20:15:29 +00001977/* Display default rtm_table for all clients. */
1978DEFUN (show_table,
1979 show_table_cmd,
1980 "show table",
1981 SHOW_STR
1982 "default routing table to use for all clients\n")
1983{
paulb21b19c2003-06-15 01:28:29 +00001984 vty_out (vty, "table %d%s", zebrad.rtm_table_default,
paul718e3742002-12-13 20:15:29 +00001985 VTY_NEWLINE);
1986 return CMD_SUCCESS;
1987}
1988
1989DEFUN (config_table,
1990 config_table_cmd,
1991 "table TABLENO",
1992 "Configure target kernel routing table\n"
1993 "TABLE integer\n")
1994{
paulb21b19c2003-06-15 01:28:29 +00001995 zebrad.rtm_table_default = strtol (argv[0], (char**)0, 10);
paul718e3742002-12-13 20:15:29 +00001996 return CMD_SUCCESS;
1997}
1998
hasso647e4f12003-05-25 11:43:52 +00001999DEFUN (ip_forwarding,
2000 ip_forwarding_cmd,
2001 "ip forwarding",
2002 IP_STR
2003 "Turn on IP forwarding")
2004{
2005 int ret;
2006
2007 ret = ipforward ();
hassob71f00f2004-10-13 12:20:35 +00002008 if (ret == 0)
2009 ret = ipforward_on ();
hasso647e4f12003-05-25 11:43:52 +00002010
hasso647e4f12003-05-25 11:43:52 +00002011 if (ret == 0)
2012 {
2013 vty_out (vty, "Can't turn on IP forwarding%s", VTY_NEWLINE);
2014 return CMD_WARNING;
2015 }
2016
2017 return CMD_SUCCESS;
2018}
2019
paul718e3742002-12-13 20:15:29 +00002020DEFUN (no_ip_forwarding,
2021 no_ip_forwarding_cmd,
2022 "no ip forwarding",
2023 NO_STR
2024 IP_STR
2025 "Turn off IP forwarding")
2026{
2027 int ret;
2028
2029 ret = ipforward ();
hassob71f00f2004-10-13 12:20:35 +00002030 if (ret != 0)
2031 ret = ipforward_off ();
paul718e3742002-12-13 20:15:29 +00002032
paul718e3742002-12-13 20:15:29 +00002033 if (ret != 0)
2034 {
2035 vty_out (vty, "Can't turn off IP forwarding%s", VTY_NEWLINE);
2036 return CMD_WARNING;
2037 }
2038
2039 return CMD_SUCCESS;
2040}
2041
2042/* This command is for debugging purpose. */
2043DEFUN (show_zebra_client,
2044 show_zebra_client_cmd,
2045 "show zebra client",
2046 SHOW_STR
2047 "Zebra information"
2048 "Client information")
2049{
hasso52dc7ee2004-09-23 19:18:23 +00002050 struct listnode *node;
paul718e3742002-12-13 20:15:29 +00002051 struct zserv *client;
2052
paul1eb8ef22005-04-07 07:30:20 +00002053 for (ALL_LIST_ELEMENTS_RO (zebrad.client_list, node, client))
Dinesh Dutt9ae85522015-05-19 17:47:22 -07002054 zebra_show_client_detail(vty, client);
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -05002055
paul718e3742002-12-13 20:15:29 +00002056 return CMD_SUCCESS;
2057}
2058
Dinesh Dutt9ae85522015-05-19 17:47:22 -07002059/* This command is for debugging purpose. */
2060DEFUN (show_zebra_client_summary,
2061 show_zebra_client_summary_cmd,
2062 "show zebra client summary",
2063 SHOW_STR
2064 "Zebra information brief"
2065 "Client information brief")
2066{
2067 struct listnode *node;
2068 struct zserv *client;
2069
2070 vty_out (vty, "Name Connect Time Last Read Last Write IPv4 Routes IPv6 Routes %s",
2071 VTY_NEWLINE);
2072 vty_out (vty,"--------------------------------------------------------------------------------%s",
2073 VTY_NEWLINE);
2074
2075 for (ALL_LIST_ELEMENTS_RO (zebrad.client_list, node, client))
2076 zebra_show_client_brief(vty, client);
2077
2078 vty_out (vty, "Routes column shows (added+updated)/deleted%s", VTY_NEWLINE);
2079 return CMD_SUCCESS;
2080}
2081
paul718e3742002-12-13 20:15:29 +00002082/* Table configuration write function. */
paulb9df2d22004-05-09 09:09:59 +00002083static int
paul718e3742002-12-13 20:15:29 +00002084config_write_table (struct vty *vty)
2085{
paulb21b19c2003-06-15 01:28:29 +00002086 if (zebrad.rtm_table_default)
2087 vty_out (vty, "table %d%s", zebrad.rtm_table_default,
paul718e3742002-12-13 20:15:29 +00002088 VTY_NEWLINE);
2089 return 0;
2090}
2091
2092/* table node for routing tables. */
Stephen Hemminger7fc626d2008-12-01 11:10:34 -08002093static struct cmd_node table_node =
paul718e3742002-12-13 20:15:29 +00002094{
2095 TABLE_NODE,
2096 "", /* This node has no interface. */
2097 1
2098};
David Lamparter6b0655a2014-06-04 06:53:35 +02002099
paul718e3742002-12-13 20:15:29 +00002100/* Only display ip forwarding is enabled or not. */
2101DEFUN (show_ip_forwarding,
2102 show_ip_forwarding_cmd,
2103 "show ip forwarding",
2104 SHOW_STR
2105 IP_STR
2106 "IP forwarding status\n")
2107{
2108 int ret;
2109
2110 ret = ipforward ();
2111
2112 if (ret == 0)
2113 vty_out (vty, "IP forwarding is off%s", VTY_NEWLINE);
2114 else
2115 vty_out (vty, "IP forwarding is on%s", VTY_NEWLINE);
2116 return CMD_SUCCESS;
2117}
2118
2119#ifdef HAVE_IPV6
2120/* Only display ipv6 forwarding is enabled or not. */
2121DEFUN (show_ipv6_forwarding,
2122 show_ipv6_forwarding_cmd,
2123 "show ipv6 forwarding",
2124 SHOW_STR
2125 "IPv6 information\n"
2126 "Forwarding status\n")
2127{
2128 int ret;
2129
2130 ret = ipforward_ipv6 ();
2131
2132 switch (ret)
2133 {
2134 case -1:
2135 vty_out (vty, "ipv6 forwarding is unknown%s", VTY_NEWLINE);
2136 break;
2137 case 0:
2138 vty_out (vty, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE);
2139 break;
2140 case 1:
2141 vty_out (vty, "ipv6 forwarding is %s%s", "on", VTY_NEWLINE);
2142 break;
2143 default:
2144 vty_out (vty, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE);
2145 break;
2146 }
2147 return CMD_SUCCESS;
2148}
2149
hasso55906722004-02-11 22:42:16 +00002150DEFUN (ipv6_forwarding,
2151 ipv6_forwarding_cmd,
2152 "ipv6 forwarding",
2153 IPV6_STR
2154 "Turn on IPv6 forwarding")
2155{
2156 int ret;
2157
hasso41d3fc92004-04-06 11:59:00 +00002158 ret = ipforward_ipv6 ();
hassob71f00f2004-10-13 12:20:35 +00002159 if (ret == 0)
2160 ret = ipforward_ipv6_on ();
hasso41d3fc92004-04-06 11:59:00 +00002161
hasso41d3fc92004-04-06 11:59:00 +00002162 if (ret == 0)
2163 {
hasso55906722004-02-11 22:42:16 +00002164 vty_out (vty, "Can't turn on IPv6 forwarding%s", VTY_NEWLINE);
2165 return CMD_WARNING;
2166 }
2167
2168 return CMD_SUCCESS;
2169}
2170
paul718e3742002-12-13 20:15:29 +00002171DEFUN (no_ipv6_forwarding,
2172 no_ipv6_forwarding_cmd,
2173 "no ipv6 forwarding",
2174 NO_STR
hasso55906722004-02-11 22:42:16 +00002175 IPV6_STR
2176 "Turn off IPv6 forwarding")
paul718e3742002-12-13 20:15:29 +00002177{
2178 int ret;
2179
hasso41d3fc92004-04-06 11:59:00 +00002180 ret = ipforward_ipv6 ();
hassob71f00f2004-10-13 12:20:35 +00002181 if (ret != 0)
2182 ret = ipforward_ipv6_off ();
hasso41d3fc92004-04-06 11:59:00 +00002183
paul718e3742002-12-13 20:15:29 +00002184 if (ret != 0)
2185 {
2186 vty_out (vty, "Can't turn off IPv6 forwarding%s", VTY_NEWLINE);
2187 return CMD_WARNING;
2188 }
2189
2190 return CMD_SUCCESS;
2191}
2192
2193#endif /* HAVE_IPV6 */
2194
2195/* IPForwarding configuration write function. */
ajs719e9742005-02-28 20:52:15 +00002196static int
paul718e3742002-12-13 20:15:29 +00002197config_write_forwarding (struct vty *vty)
2198{
hasso18a6dce2004-10-03 18:18:34 +00002199 /* FIXME: Find better place for that. */
2200 router_id_write (vty);
2201
paul3e0b3a52004-08-23 18:58:32 +00002202 if (ipforward ())
2203 vty_out (vty, "ip forwarding%s", VTY_NEWLINE);
paul718e3742002-12-13 20:15:29 +00002204#ifdef HAVE_IPV6
paul3e0b3a52004-08-23 18:58:32 +00002205 if (ipforward_ipv6 ())
2206 vty_out (vty, "ipv6 forwarding%s", VTY_NEWLINE);
paul718e3742002-12-13 20:15:29 +00002207#endif /* HAVE_IPV6 */
2208 vty_out (vty, "!%s", VTY_NEWLINE);
2209 return 0;
2210}
2211
2212/* table node for routing tables. */
Stephen Hemminger7fc626d2008-12-01 11:10:34 -08002213static struct cmd_node forwarding_node =
paul718e3742002-12-13 20:15:29 +00002214{
2215 FORWARDING_NODE,
2216 "", /* This node has no interface. */
2217 1
2218};
2219
Udaya Shankara KSd869dbd2016-02-11 21:42:29 +05302220#ifdef HAVE_FPM
2221/* function to write the fpm config info */
2222static int
2223config_write_fpm (struct vty *vty)
2224{
2225 return
2226 fpm_remote_srv_write (vty);
2227}
2228
2229/* Zebra node */
2230static struct cmd_node zebra_node =
2231{
2232 ZEBRA_NODE,
2233 "",
2234 1
2235};
2236#endif
2237
David Lamparter6b0655a2014-06-04 06:53:35 +02002238
paul718e3742002-12-13 20:15:29 +00002239/* Initialisation of zebra and installation of commands. */
2240void
paula1ac18c2005-06-28 17:17:12 +00002241zebra_init (void)
paul718e3742002-12-13 20:15:29 +00002242{
2243 /* Client list init. */
paulb21b19c2003-06-15 01:28:29 +00002244 zebrad.client_list = list_new ();
paul718e3742002-12-13 20:15:29 +00002245
paul718e3742002-12-13 20:15:29 +00002246 /* Install configuration write function. */
2247 install_node (&table_node, config_write_table);
2248 install_node (&forwarding_node, config_write_forwarding);
Udaya Shankara KSd869dbd2016-02-11 21:42:29 +05302249#ifdef HAVE_FPM
2250 install_node (&zebra_node, config_write_fpm);
2251#endif
paul718e3742002-12-13 20:15:29 +00002252
2253 install_element (VIEW_NODE, &show_ip_forwarding_cmd);
hasso647e4f12003-05-25 11:43:52 +00002254 install_element (CONFIG_NODE, &ip_forwarding_cmd);
paul718e3742002-12-13 20:15:29 +00002255 install_element (CONFIG_NODE, &no_ip_forwarding_cmd);
2256 install_element (ENABLE_NODE, &show_zebra_client_cmd);
Dinesh Dutt9ae85522015-05-19 17:47:22 -07002257 install_element (ENABLE_NODE, &show_zebra_client_summary_cmd);
paul718e3742002-12-13 20:15:29 +00002258
2259#ifdef HAVE_NETLINK
2260 install_element (VIEW_NODE, &show_table_cmd);
paul718e3742002-12-13 20:15:29 +00002261 install_element (CONFIG_NODE, &config_table_cmd);
2262#endif /* HAVE_NETLINK */
2263
2264#ifdef HAVE_IPV6
2265 install_element (VIEW_NODE, &show_ipv6_forwarding_cmd);
hasso55906722004-02-11 22:42:16 +00002266 install_element (CONFIG_NODE, &ipv6_forwarding_cmd);
paul718e3742002-12-13 20:15:29 +00002267 install_element (CONFIG_NODE, &no_ipv6_forwarding_cmd);
2268#endif /* HAVE_IPV6 */
Paul Jakma7514fb72007-05-02 16:05:35 +00002269
2270 /* Route-map */
2271 zebra_route_map_init ();
paul718e3742002-12-13 20:15:29 +00002272}
Denis Ovsienko97be79f2009-07-24 20:45:31 +04002273
2274/* Make zebra server socket, wiping any existing one (see bug #403). */
2275void
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +04002276zebra_zserv_socket_init (char *path)
Denis Ovsienko97be79f2009-07-24 20:45:31 +04002277{
2278#ifdef HAVE_TCP_ZEBRA
2279 zebra_serv ();
2280#else
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +04002281 zebra_serv_un (path ? path : ZEBRA_SERV_PATH);
Denis Ovsienko97be79f2009-07-24 20:45:31 +04002282#endif /* HAVE_TCP_ZEBRA */
2283}