blob: e0e32ab078996a1c634dd9566848771dde9f208c [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"
paul718e3742002-12-13 20:15:29 +000040
41#include "zebra/zserv.h"
hasso18a6dce2004-10-03 18:18:34 +000042#include "zebra/router-id.h"
paul718e3742002-12-13 20:15:29 +000043#include "zebra/redistribute.h"
44#include "zebra/debug.h"
45#include "zebra/ipforward.h"
David Lamparter6b0655a2014-06-04 06:53:35 +020046
paul718e3742002-12-13 20:15:29 +000047/* Event list of zebra. */
48enum event { ZEBRA_SERV, ZEBRA_READ, ZEBRA_WRITE };
49
paulb21b19c2003-06-15 01:28:29 +000050extern struct zebra_t zebrad;
paul718e3742002-12-13 20:15:29 +000051
paulb9df2d22004-05-09 09:09:59 +000052static void zebra_event (enum event event, int sock, struct zserv *client);
paulccf35572003-03-01 11:42:20 +000053
pauledd7c242003-06-04 13:59:38 +000054extern struct zebra_privs_t zserv_privs;
David Lamparter6b0655a2014-06-04 06:53:35 +020055
ajs719e9742005-02-28 20:52:15 +000056static void zebra_client_close (struct zserv *client);
57
58static int
59zserv_delayed_close(struct thread *thread)
paulccf35572003-03-01 11:42:20 +000060{
ajs719e9742005-02-28 20:52:15 +000061 struct zserv *client = THREAD_ARG(thread);
paulccf35572003-03-01 11:42:20 +000062
ajs719e9742005-02-28 20:52:15 +000063 client->t_suicide = NULL;
64 zebra_client_close(client);
paulccf35572003-03-01 11:42:20 +000065 return 0;
66}
67
Vyacheslav Trushkin2ea1ab12011-12-11 18:48:47 +040068/* When client connects, it sends hello message
69 * with promise to send zebra routes of specific type.
70 * Zebra stores a socket fd of the client into
71 * this array. And use it to clean up routes that
72 * client didn't remove for some reasons after closing
73 * connection.
74 */
75static int route_type_oaths[ZEBRA_ROUTE_MAX];
76
ajs719e9742005-02-28 20:52:15 +000077static int
78zserv_flush_data(struct thread *thread)
paulccf35572003-03-01 11:42:20 +000079{
ajs719e9742005-02-28 20:52:15 +000080 struct zserv *client = THREAD_ARG(thread);
paulccf35572003-03-01 11:42:20 +000081
ajs719e9742005-02-28 20:52:15 +000082 client->t_write = NULL;
83 if (client->t_suicide)
84 {
85 zebra_client_close(client);
86 return -1;
87 }
88 switch (buffer_flush_available(client->wb, client->sock))
89 {
90 case BUFFER_ERROR:
91 zlog_warn("%s: buffer_flush_available failed on zserv client fd %d, "
92 "closing", __func__, client->sock);
93 zebra_client_close(client);
94 break;
95 case BUFFER_PENDING:
96 client->t_write = thread_add_write(zebrad.master, zserv_flush_data,
97 client, client->sock);
98 break;
99 case BUFFER_EMPTY:
100 break;
101 }
102 return 0;
paulccf35572003-03-01 11:42:20 +0000103}
104
ajs719e9742005-02-28 20:52:15 +0000105static int
106zebra_server_send_message(struct zserv *client)
paulccf35572003-03-01 11:42:20 +0000107{
ajs719e9742005-02-28 20:52:15 +0000108 if (client->t_suicide)
109 return -1;
110 switch (buffer_write(client->wb, client->sock, STREAM_DATA(client->obuf),
111 stream_get_endp(client->obuf)))
paulccf35572003-03-01 11:42:20 +0000112 {
ajs719e9742005-02-28 20:52:15 +0000113 case BUFFER_ERROR:
114 zlog_warn("%s: buffer_write failed to zserv client fd %d, closing",
115 __func__, client->sock);
116 /* Schedule a delayed close since many of the functions that call this
117 one do not check the return code. They do not allow for the
118 possibility that an I/O error may have caused the client to be
119 deleted. */
120 client->t_suicide = thread_add_event(zebrad.master, zserv_delayed_close,
121 client, 0);
122 return -1;
ajs719e9742005-02-28 20:52:15 +0000123 case BUFFER_EMPTY:
124 THREAD_OFF(client->t_write);
125 break;
126 case BUFFER_PENDING:
127 THREAD_WRITE_ON(zebrad.master, client->t_write,
128 zserv_flush_data, client, client->sock);
129 break;
paulccf35572003-03-01 11:42:20 +0000130 }
paulccf35572003-03-01 11:42:20 +0000131 return 0;
132}
133
paulc1b98002006-01-16 01:54:02 +0000134static void
Feng Luc99f3482014-10-16 09:52:36 +0800135zserv_create_header (struct stream *s, uint16_t cmd, vrf_id_t vrf_id)
paulc1b98002006-01-16 01:54:02 +0000136{
137 /* length placeholder, caller can update */
138 stream_putw (s, ZEBRA_HEADER_SIZE);
139 stream_putc (s, ZEBRA_HEADER_MARKER);
140 stream_putc (s, ZSERV_VERSION);
Feng Luc99f3482014-10-16 09:52:36 +0800141 stream_putw (s, vrf_id);
paulc1b98002006-01-16 01:54:02 +0000142 stream_putw (s, cmd);
143}
144
Josh Bailey51d4ef82012-03-21 17:13:39 -0700145static void
146zserv_encode_interface (struct stream *s, struct interface *ifp)
147{
148 /* Interface information. */
149 stream_put (s, ifp->name, INTERFACE_NAMSIZ);
150 stream_putl (s, ifp->ifindex);
151 stream_putc (s, ifp->status);
152 stream_putq (s, ifp->flags);
153 stream_putl (s, ifp->metric);
154 stream_putl (s, ifp->mtu);
155 stream_putl (s, ifp->mtu6);
156 stream_putl (s, ifp->bandwidth);
Timo Teräs954c7d62016-01-15 17:36:33 +0200157 stream_putl (s, ifp->ll_type);
Josh Bailey51d4ef82012-03-21 17:13:39 -0700158 stream_putl (s, ifp->hw_addr_len);
159 if (ifp->hw_addr_len)
160 stream_put (s, ifp->hw_addr, ifp->hw_addr_len);
Josh Bailey51d4ef82012-03-21 17:13:39 -0700161
162 /* Write packet size. */
163 stream_putw_at (s, 0, stream_get_endp (s));
164}
165
paul718e3742002-12-13 20:15:29 +0000166/* Interface is added. Send ZEBRA_INTERFACE_ADD to client. */
paulb9df2d22004-05-09 09:09:59 +0000167/*
168 * This function is called in the following situations:
169 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
170 * from the client.
171 * - at startup, when zebra figures out the available interfaces
172 * - when an interface is added (where support for
173 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
174 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
175 * received)
176 */
paul718e3742002-12-13 20:15:29 +0000177int
178zsend_interface_add (struct zserv *client, struct interface *ifp)
179{
180 struct stream *s;
181
182 /* Check this client need interface information. */
Feng Luc99f3482014-10-16 09:52:36 +0800183 if (! vrf_bitmap_check (client->ifinfo, ifp->vrf_id))
ajs719e9742005-02-28 20:52:15 +0000184 return 0;
paul718e3742002-12-13 20:15:29 +0000185
186 s = client->obuf;
187 stream_reset (s);
188
Feng Luc99f3482014-10-16 09:52:36 +0800189 zserv_create_header (s, ZEBRA_INTERFACE_ADD, ifp->vrf_id);
Josh Bailey51d4ef82012-03-21 17:13:39 -0700190 zserv_encode_interface (s, ifp);
paul718e3742002-12-13 20:15:29 +0000191
ajs719e9742005-02-28 20:52:15 +0000192 return zebra_server_send_message(client);
paul718e3742002-12-13 20:15:29 +0000193}
194
195/* Interface deletion from zebra daemon. */
196int
197zsend_interface_delete (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);
paul718e3742002-12-13 20:15:29 +0000207
Feng Luc99f3482014-10-16 09:52:36 +0800208 zserv_create_header (s, ZEBRA_INTERFACE_DELETE, ifp->vrf_id);
Josh Bailey51d4ef82012-03-21 17:13:39 -0700209 zserv_encode_interface (s, ifp);
paul718e3742002-12-13 20:15:29 +0000210
ajs719e9742005-02-28 20:52:15 +0000211 return zebra_server_send_message (client);
paul718e3742002-12-13 20:15:29 +0000212}
213
paulb9df2d22004-05-09 09:09:59 +0000214/* Interface address is added/deleted. Send ZEBRA_INTERFACE_ADDRESS_ADD or
215 * ZEBRA_INTERFACE_ADDRESS_DELETE to the client.
216 *
217 * A ZEBRA_INTERFACE_ADDRESS_ADD is sent in the following situations:
218 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
219 * from the client, after the ZEBRA_INTERFACE_ADD has been
220 * sent from zebra to the client
221 * - redistribute new address info to all clients in the following situations
222 * - at startup, when zebra figures out the available interfaces
223 * - when an interface is added (where support for
224 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
225 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
226 * received)
227 * - for the vty commands "ip address A.B.C.D/M [<secondary>|<label LINE>]"
228 * and "no bandwidth <1-10000000>", "ipv6 address X:X::X:X/M"
229 * - when an RTM_NEWADDR message is received from the kernel,
230 *
231 * The call tree that triggers ZEBRA_INTERFACE_ADDRESS_DELETE:
232 *
233 * zsend_interface_address(DELETE)
234 * ^
235 * |
236 * zebra_interface_address_delete_update
237 * ^ ^ ^
paul6eb88272005-07-29 14:36:00 +0000238 * | | if_delete_update
239 * | |
paulb9df2d22004-05-09 09:09:59 +0000240 * ip_address_uninstall connected_delete_ipv4
241 * [ipv6_addresss_uninstall] [connected_delete_ipv6]
242 * ^ ^
243 * | |
244 * | RTM_NEWADDR on routing/netlink socket
245 * |
246 * vty commands:
247 * "no ip address A.B.C.D/M [label LINE]"
248 * "no ip address A.B.C.D/M secondary"
249 * ["no ipv6 address X:X::X:X/M"]
250 *
251 */
paul718e3742002-12-13 20:15:29 +0000252int
paulb9df2d22004-05-09 09:09:59 +0000253zsend_interface_address (int cmd, struct zserv *client,
254 struct interface *ifp, struct connected *ifc)
paul718e3742002-12-13 20:15:29 +0000255{
256 int blen;
257 struct stream *s;
258 struct prefix *p;
259
260 /* Check this client need interface information. */
Feng Luc99f3482014-10-16 09:52:36 +0800261 if (! vrf_bitmap_check (client->ifinfo, ifp->vrf_id))
ajs719e9742005-02-28 20:52:15 +0000262 return 0;
paul718e3742002-12-13 20:15:29 +0000263
264 s = client->obuf;
265 stream_reset (s);
paulc1b98002006-01-16 01:54:02 +0000266
Feng Luc99f3482014-10-16 09:52:36 +0800267 zserv_create_header (s, cmd, ifp->vrf_id);
paul718e3742002-12-13 20:15:29 +0000268 stream_putl (s, ifp->ifindex);
269
270 /* Interface address flag. */
271 stream_putc (s, ifc->flags);
272
273 /* Prefix information. */
274 p = ifc->address;
275 stream_putc (s, p->family);
276 blen = prefix_blen (p);
277 stream_put (s, &p->u.prefix, blen);
paulb9df2d22004-05-09 09:09:59 +0000278
279 /*
280 * XXX gnu version does not send prefixlen for ZEBRA_INTERFACE_ADDRESS_DELETE
281 * but zebra_interface_address_delete_read() in the gnu version
282 * expects to find it
283 */
paul718e3742002-12-13 20:15:29 +0000284 stream_putc (s, p->prefixlen);
285
286 /* Destination. */
287 p = ifc->destination;
288 if (p)
289 stream_put (s, &p->u.prefix, blen);
290 else
291 stream_put (s, NULL, blen);
292
293 /* Write packet size. */
294 stream_putw_at (s, 0, stream_get_endp (s));
295
ajs719e9742005-02-28 20:52:15 +0000296 return zebra_server_send_message(client);
paul718e3742002-12-13 20:15:29 +0000297}
298
paulb9df2d22004-05-09 09:09:59 +0000299/*
300 * The cmd passed to zsend_interface_update may be ZEBRA_INTERFACE_UP or
301 * ZEBRA_INTERFACE_DOWN.
302 *
303 * The ZEBRA_INTERFACE_UP message is sent from the zebra server to
304 * the clients in one of 2 situations:
305 * - an if_up is detected e.g., as a result of an RTM_IFINFO message
306 * - a vty command modifying the bandwidth of an interface is received.
307 * The ZEBRA_INTERFACE_DOWN message is sent when an if_down is detected.
308 */
paul718e3742002-12-13 20:15:29 +0000309int
paulb9df2d22004-05-09 09:09:59 +0000310zsend_interface_update (int cmd, struct zserv *client, struct interface *ifp)
paul718e3742002-12-13 20:15:29 +0000311{
312 struct stream *s;
313
314 /* Check this client need interface information. */
Feng Luc99f3482014-10-16 09:52:36 +0800315 if (! vrf_bitmap_check (client->ifinfo, ifp->vrf_id))
ajs719e9742005-02-28 20:52:15 +0000316 return 0;
paul718e3742002-12-13 20:15:29 +0000317
318 s = client->obuf;
319 stream_reset (s);
320
Feng Luc99f3482014-10-16 09:52:36 +0800321 zserv_create_header (s, cmd, ifp->vrf_id);
Josh Bailey51d4ef82012-03-21 17:13:39 -0700322 zserv_encode_interface (s, ifp);
paul718e3742002-12-13 20:15:29 +0000323
ajs719e9742005-02-28 20:52:15 +0000324 return zebra_server_send_message(client);
paul718e3742002-12-13 20:15:29 +0000325}
326
paulb9df2d22004-05-09 09:09:59 +0000327/*
328 * The zebra server sends the clients a ZEBRA_IPV4_ROUTE_ADD or a
329 * ZEBRA_IPV6_ROUTE_ADD via zsend_route_multipath in the following
330 * situations:
331 * - when the client starts up, and requests default information
332 * by sending a ZEBRA_REDISTRIBUTE_DEFAULT_ADD to the zebra server, in the
333 * - case of rip, ripngd, ospfd and ospf6d, when the client sends a
334 * ZEBRA_REDISTRIBUTE_ADD as a result of the "redistribute" vty cmd,
335 * - when the zebra server redistributes routes after it updates its rib
336 *
337 * The zebra server sends clients a ZEBRA_IPV4_ROUTE_DELETE or a
338 * ZEBRA_IPV6_ROUTE_DELETE via zsend_route_multipath when:
339 * - a "ip route" or "ipv6 route" vty command is issued, a prefix is
340 * - deleted from zebra's rib, and this info
341 * has to be redistributed to the clients
342 *
343 * XXX The ZEBRA_IPV*_ROUTE_ADD message is also sent by the client to the
344 * zebra server when the client wants to tell the zebra server to add a
345 * route to the kernel (zapi_ipv4_add etc. ). Since it's essentially the
346 * same message being sent back and forth, this function and
347 * zapi_ipv{4,6}_{add, delete} should be re-written to avoid code
348 * duplication.
349 */
paul718e3742002-12-13 20:15:29 +0000350int
paulb9df2d22004-05-09 09:09:59 +0000351zsend_route_multipath (int cmd, struct zserv *client, struct prefix *p,
352 struct rib *rib)
paul718e3742002-12-13 20:15:29 +0000353{
354 int psize;
355 struct stream *s;
356 struct nexthop *nexthop;
paul1dcb5172005-05-31 08:38:50 +0000357 unsigned long nhnummark = 0, messmark = 0;
paulb9df2d22004-05-09 09:09:59 +0000358 int nhnum = 0;
paul1dcb5172005-05-31 08:38:50 +0000359 u_char zapi_flags = 0;
Feng Luc99f3482014-10-16 09:52:36 +0800360
361 /* Check this client need this route. */
362 if (!vrf_bitmap_check (client->redist[rib->type], rib->vrf_id) &&
363 !(is_default (p) &&
364 vrf_bitmap_check (client->redist_default, rib->vrf_id)))
365 return 0;
366
paul718e3742002-12-13 20:15:29 +0000367 s = client->obuf;
368 stream_reset (s);
paulc1b98002006-01-16 01:54:02 +0000369
Feng Luc99f3482014-10-16 09:52:36 +0800370 zserv_create_header (s, cmd, rib->vrf_id);
paulc1b98002006-01-16 01:54:02 +0000371
372 /* Put type and nexthop. */
paul718e3742002-12-13 20:15:29 +0000373 stream_putc (s, rib->type);
374 stream_putc (s, rib->flags);
paul1dcb5172005-05-31 08:38:50 +0000375
376 /* marker for message flags field */
377 messmark = stream_get_endp (s);
378 stream_putc (s, 0);
paul718e3742002-12-13 20:15:29 +0000379
380 /* Prefix. */
381 psize = PSIZE (p->prefixlen);
382 stream_putc (s, p->prefixlen);
paulb9df2d22004-05-09 09:09:59 +0000383 stream_write (s, (u_char *) & p->u.prefix, psize);
paul718e3742002-12-13 20:15:29 +0000384
paulb9df2d22004-05-09 09:09:59 +0000385 /*
386 * XXX The message format sent by zebra below does not match the format
387 * of the corresponding message expected by the zebra server
388 * itself (e.g., see zread_ipv4_add). The nexthop_num is not set correctly,
389 * (is there a bug on the client side if more than one segment is sent?)
390 * nexthop ZEBRA_NEXTHOP_IPV4 is never set, ZEBRA_NEXTHOP_IFINDEX
391 * is hard-coded.
392 */
paul718e3742002-12-13 20:15:29 +0000393 /* Nexthop */
paul1dcb5172005-05-31 08:38:50 +0000394
paul718e3742002-12-13 20:15:29 +0000395 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
396 {
Timo Teräs325823a2016-01-15 17:36:31 +0200397 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE))
paulb9df2d22004-05-09 09:09:59 +0000398 {
paul1dcb5172005-05-31 08:38:50 +0000399 SET_FLAG (zapi_flags, ZAPI_MESSAGE_NEXTHOP);
400 SET_FLAG (zapi_flags, ZAPI_MESSAGE_IFINDEX);
401
402 if (nhnummark == 0)
403 {
404 nhnummark = stream_get_endp (s);
405 stream_putc (s, 1); /* placeholder */
406 }
407
paulb9df2d22004-05-09 09:09:59 +0000408 nhnum++;
paul718e3742002-12-13 20:15:29 +0000409
paulb9df2d22004-05-09 09:09:59 +0000410 switch(nexthop->type)
411 {
412 case NEXTHOP_TYPE_IPV4:
413 case NEXTHOP_TYPE_IPV4_IFINDEX:
414 stream_put_in_addr (s, &nexthop->gate.ipv4);
415 break;
416#ifdef HAVE_IPV6
417 case NEXTHOP_TYPE_IPV6:
418 case NEXTHOP_TYPE_IPV6_IFINDEX:
419 case NEXTHOP_TYPE_IPV6_IFNAME:
420 stream_write (s, (u_char *) &nexthop->gate.ipv6, 16);
421 break;
422#endif
423 default:
424 if (cmd == ZEBRA_IPV4_ROUTE_ADD
425 || cmd == ZEBRA_IPV4_ROUTE_DELETE)
426 {
427 struct in_addr empty;
paul44983cf2004-09-22 13:15:58 +0000428 memset (&empty, 0, sizeof (struct in_addr));
paulb9df2d22004-05-09 09:09:59 +0000429 stream_write (s, (u_char *) &empty, IPV4_MAX_BYTELEN);
430 }
431 else
432 {
433 struct in6_addr empty;
434 memset (&empty, 0, sizeof (struct in6_addr));
435 stream_write (s, (u_char *) &empty, IPV6_MAX_BYTELEN);
436 }
437 }
paul718e3742002-12-13 20:15:29 +0000438
paulb9df2d22004-05-09 09:09:59 +0000439 /* Interface index. */
440 stream_putc (s, 1);
441 stream_putl (s, nexthop->ifindex);
paul718e3742002-12-13 20:15:29 +0000442
paulb9df2d22004-05-09 09:09:59 +0000443 break;
444 }
paul718e3742002-12-13 20:15:29 +0000445 }
446
447 /* Metric */
Stephen Hemmingercf8a8312010-08-18 15:56:46 -0700448 if (cmd == ZEBRA_IPV4_ROUTE_ADD || cmd == ZEBRA_IPV6_ROUTE_ADD)
paul1dcb5172005-05-31 08:38:50 +0000449 {
vincentfbf5d032005-09-29 11:25:50 +0000450 SET_FLAG (zapi_flags, ZAPI_MESSAGE_DISTANCE);
451 stream_putc (s, rib->distance);
paul1dcb5172005-05-31 08:38:50 +0000452 SET_FLAG (zapi_flags, ZAPI_MESSAGE_METRIC);
453 stream_putl (s, rib->metric);
Timo Teräsb11f3b52015-11-02 16:50:07 +0200454 SET_FLAG (zapi_flags, ZAPI_MESSAGE_MTU);
455 stream_putl (s, rib->mtu);
paul1dcb5172005-05-31 08:38:50 +0000456 }
457
458 /* write real message flags value */
459 stream_putc_at (s, messmark, zapi_flags);
460
paulb9df2d22004-05-09 09:09:59 +0000461 /* Write next-hop number */
462 if (nhnummark)
hassoc1eaa442004-10-19 06:26:01 +0000463 stream_putc_at (s, nhnummark, nhnum);
paulb9df2d22004-05-09 09:09:59 +0000464
paul718e3742002-12-13 20:15:29 +0000465 /* Write packet size. */
466 stream_putw_at (s, 0, stream_get_endp (s));
467
ajs719e9742005-02-28 20:52:15 +0000468 return zebra_server_send_message(client);
paul718e3742002-12-13 20:15:29 +0000469}
470
paul718e3742002-12-13 20:15:29 +0000471#ifdef HAVE_IPV6
ajs719e9742005-02-28 20:52:15 +0000472static int
Feng Luc99f3482014-10-16 09:52:36 +0800473zsend_ipv6_nexthop_lookup (struct zserv *client, struct in6_addr *addr,
474 vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +0000475{
476 struct stream *s;
477 struct rib *rib;
478 unsigned long nump;
479 u_char num;
480 struct nexthop *nexthop;
481
482 /* Lookup nexthop. */
Feng Luc99f3482014-10-16 09:52:36 +0800483 rib = rib_match_ipv6 (addr, vrf_id);
paul718e3742002-12-13 20:15:29 +0000484
485 /* Get output stream. */
486 s = client->obuf;
487 stream_reset (s);
488
489 /* Fill in result. */
Feng Luc99f3482014-10-16 09:52:36 +0800490 zserv_create_header (s, ZEBRA_IPV6_NEXTHOP_LOOKUP, vrf_id);
Hiroshi Yokoi8ccd74c2015-09-08 11:52:20 +0900491 stream_put (s, addr, 16);
paul718e3742002-12-13 20:15:29 +0000492
493 if (rib)
494 {
495 stream_putl (s, rib->metric);
496 num = 0;
paul9985f832005-02-09 15:51:56 +0000497 nump = stream_get_endp(s);
paul718e3742002-12-13 20:15:29 +0000498 stream_putc (s, 0);
Christian Frankefa713d92013-07-05 15:35:37 +0000499 /* Only non-recursive routes are elegible to resolve nexthop we
500 * are looking up. Therefore, we will just iterate over the top
501 * chain of nexthops. */
paul718e3742002-12-13 20:15:29 +0000502 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
Timo Teräs325823a2016-01-15 17:36:31 +0200503 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE))
paul718e3742002-12-13 20:15:29 +0000504 {
505 stream_putc (s, nexthop->type);
506 switch (nexthop->type)
507 {
508 case ZEBRA_NEXTHOP_IPV6:
509 stream_put (s, &nexthop->gate.ipv6, 16);
510 break;
511 case ZEBRA_NEXTHOP_IPV6_IFINDEX:
512 case ZEBRA_NEXTHOP_IPV6_IFNAME:
513 stream_put (s, &nexthop->gate.ipv6, 16);
514 stream_putl (s, nexthop->ifindex);
515 break;
516 case ZEBRA_NEXTHOP_IFINDEX:
517 case ZEBRA_NEXTHOP_IFNAME:
518 stream_putl (s, nexthop->ifindex);
519 break;
hassofa2b17e2004-03-04 17:45:00 +0000520 default:
521 /* do nothing */
522 break;
paul718e3742002-12-13 20:15:29 +0000523 }
524 num++;
525 }
526 stream_putc_at (s, nump, num);
527 }
528 else
529 {
530 stream_putl (s, 0);
531 stream_putc (s, 0);
532 }
533
534 stream_putw_at (s, 0, stream_get_endp (s));
535
ajs719e9742005-02-28 20:52:15 +0000536 return zebra_server_send_message(client);
paul718e3742002-12-13 20:15:29 +0000537}
538#endif /* HAVE_IPV6 */
539
paulb9df2d22004-05-09 09:09:59 +0000540static int
Feng Luc99f3482014-10-16 09:52:36 +0800541zsend_ipv4_nexthop_lookup (struct zserv *client, struct in_addr addr,
542 vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +0000543{
544 struct stream *s;
545 struct rib *rib;
546 unsigned long nump;
547 u_char num;
548 struct nexthop *nexthop;
549
David Lamparterf598cf72014-11-22 14:44:20 -0800550 /* Lookup nexthop - eBGP excluded */
Feng Luc99f3482014-10-16 09:52:36 +0800551 rib = rib_match_ipv4_safi (addr, SAFI_UNICAST, 1, NULL, vrf_id);
paul718e3742002-12-13 20:15:29 +0000552
553 /* Get output stream. */
554 s = client->obuf;
555 stream_reset (s);
556
557 /* Fill in result. */
Feng Luc99f3482014-10-16 09:52:36 +0800558 zserv_create_header (s, ZEBRA_IPV4_NEXTHOP_LOOKUP, vrf_id);
paul718e3742002-12-13 20:15:29 +0000559 stream_put_in_addr (s, &addr);
560
561 if (rib)
562 {
Christian Frankebb97e462013-05-25 14:01:35 +0000563 if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
564 zlog_debug("%s: Matching rib entry found.", __func__);
paul718e3742002-12-13 20:15:29 +0000565 stream_putl (s, rib->metric);
566 num = 0;
paul9985f832005-02-09 15:51:56 +0000567 nump = stream_get_endp(s);
paul718e3742002-12-13 20:15:29 +0000568 stream_putc (s, 0);
Christian Frankefa713d92013-07-05 15:35:37 +0000569 /* Only non-recursive routes are elegible to resolve the nexthop we
570 * are looking up. Therefore, we will just iterate over the top
571 * chain of nexthops. */
paul718e3742002-12-13 20:15:29 +0000572 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
Timo Teräs325823a2016-01-15 17:36:31 +0200573 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE))
paul718e3742002-12-13 20:15:29 +0000574 {
575 stream_putc (s, nexthop->type);
576 switch (nexthop->type)
577 {
578 case ZEBRA_NEXTHOP_IPV4:
579 stream_put_in_addr (s, &nexthop->gate.ipv4);
580 break;
Christian Frankebb97e462013-05-25 14:01:35 +0000581 case ZEBRA_NEXTHOP_IPV4_IFINDEX:
582 stream_put_in_addr (s, &nexthop->gate.ipv4);
583 stream_putl (s, nexthop->ifindex);
584 break;
paul718e3742002-12-13 20:15:29 +0000585 case ZEBRA_NEXTHOP_IFINDEX:
586 case ZEBRA_NEXTHOP_IFNAME:
587 stream_putl (s, nexthop->ifindex);
588 break;
hassofa2b17e2004-03-04 17:45:00 +0000589 default:
590 /* do nothing */
591 break;
paul718e3742002-12-13 20:15:29 +0000592 }
593 num++;
594 }
595 stream_putc_at (s, nump, num);
596 }
597 else
598 {
Christian Frankebb97e462013-05-25 14:01:35 +0000599 if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
600 zlog_debug("%s: No matching rib entry found.", __func__);
paul718e3742002-12-13 20:15:29 +0000601 stream_putl (s, 0);
602 stream_putc (s, 0);
603 }
604
605 stream_putw_at (s, 0, stream_get_endp (s));
606
ajs719e9742005-02-28 20:52:15 +0000607 return zebra_server_send_message(client);
paul718e3742002-12-13 20:15:29 +0000608}
609
Everton Marques4e5275b2014-07-01 15:15:52 -0300610/*
611 Modified version of zsend_ipv4_nexthop_lookup():
612 Query unicast rib if nexthop is not found on mrib.
613 Returns both route metric and protocol distance.
614*/
615static int
David Lamparterbd078122015-01-06 19:53:24 +0100616zsend_ipv4_nexthop_lookup_mrib (struct zserv *client, struct in_addr addr,
617 struct rib *rib)
Everton Marques4e5275b2014-07-01 15:15:52 -0300618{
619 struct stream *s;
Everton Marques4e5275b2014-07-01 15:15:52 -0300620 unsigned long nump;
621 u_char num;
622 struct nexthop *nexthop;
Everton Marques4e5275b2014-07-01 15:15:52 -0300623
624 /* Get output stream. */
625 s = client->obuf;
626 stream_reset (s);
627
628 /* Fill in result. */
Jafar Al-Gharaibeh190591f2016-04-21 17:40:12 -0500629 zserv_create_header (s, ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB,
630 rib ? rib->vrf_id : VRF_DEFAULT);
Everton Marques4e5275b2014-07-01 15:15:52 -0300631 stream_put_in_addr (s, &addr);
632
633 if (rib)
634 {
635 stream_putc (s, rib->distance);
636 stream_putl (s, rib->metric);
637 num = 0;
638 nump = stream_get_endp(s); /* remember position for nexthop_num */
639 stream_putc (s, 0); /* reserve room for nexthop_num */
640 /* Only non-recursive routes are elegible to resolve the nexthop we
641 * are looking up. Therefore, we will just iterate over the top
642 * chain of nexthops. */
643 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
Timo Teräs325823a2016-01-15 17:36:31 +0200644 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE))
Everton Marques4e5275b2014-07-01 15:15:52 -0300645 {
646 stream_putc (s, nexthop->type);
647 switch (nexthop->type)
648 {
649 case ZEBRA_NEXTHOP_IPV4:
650 stream_put_in_addr (s, &nexthop->gate.ipv4);
651 break;
652 case ZEBRA_NEXTHOP_IPV4_IFINDEX:
653 stream_put_in_addr (s, &nexthop->gate.ipv4);
654 stream_putl (s, nexthop->ifindex);
655 break;
656 case ZEBRA_NEXTHOP_IFINDEX:
657 case ZEBRA_NEXTHOP_IFNAME:
658 stream_putl (s, nexthop->ifindex);
659 break;
660 default:
661 /* do nothing */
662 break;
663 }
664 num++;
665 }
666
667 stream_putc_at (s, nump, num); /* store nexthop_num */
668 }
669 else
670 {
671 stream_putc (s, 0); /* distance */
672 stream_putl (s, 0); /* metric */
673 stream_putc (s, 0); /* nexthop_num */
674 }
675
676 stream_putw_at (s, 0, stream_get_endp (s));
677
678 return zebra_server_send_message(client);
679}
680
paulb9df2d22004-05-09 09:09:59 +0000681static int
Feng Luc99f3482014-10-16 09:52:36 +0800682zsend_ipv4_import_lookup (struct zserv *client, struct prefix_ipv4 *p,
683 vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +0000684{
685 struct stream *s;
686 struct rib *rib;
687 unsigned long nump;
688 u_char num;
689 struct nexthop *nexthop;
690
691 /* Lookup nexthop. */
Feng Luc99f3482014-10-16 09:52:36 +0800692 rib = rib_lookup_ipv4 (p, vrf_id);
paul718e3742002-12-13 20:15:29 +0000693
694 /* Get output stream. */
695 s = client->obuf;
696 stream_reset (s);
697
698 /* Fill in result. */
Feng Luc99f3482014-10-16 09:52:36 +0800699 zserv_create_header (s, ZEBRA_IPV4_IMPORT_LOOKUP, vrf_id);
paul718e3742002-12-13 20:15:29 +0000700 stream_put_in_addr (s, &p->prefix);
701
702 if (rib)
703 {
704 stream_putl (s, rib->metric);
705 num = 0;
paul9985f832005-02-09 15:51:56 +0000706 nump = stream_get_endp(s);
paul718e3742002-12-13 20:15:29 +0000707 stream_putc (s, 0);
708 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
Timo Teräs325823a2016-01-15 17:36:31 +0200709 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE))
paul718e3742002-12-13 20:15:29 +0000710 {
711 stream_putc (s, nexthop->type);
712 switch (nexthop->type)
713 {
714 case ZEBRA_NEXTHOP_IPV4:
715 stream_put_in_addr (s, &nexthop->gate.ipv4);
716 break;
Christian Frankea12afd52013-05-25 14:01:36 +0000717 case ZEBRA_NEXTHOP_IPV4_IFINDEX:
718 stream_put_in_addr (s, &nexthop->gate.ipv4);
719 stream_putl (s, nexthop->ifindex);
720 break;
paul718e3742002-12-13 20:15:29 +0000721 case ZEBRA_NEXTHOP_IFINDEX:
722 case ZEBRA_NEXTHOP_IFNAME:
723 stream_putl (s, nexthop->ifindex);
724 break;
hassofa2b17e2004-03-04 17:45:00 +0000725 default:
726 /* do nothing */
727 break;
paul718e3742002-12-13 20:15:29 +0000728 }
729 num++;
730 }
731 stream_putc_at (s, nump, num);
732 }
733 else
734 {
735 stream_putl (s, 0);
736 stream_putc (s, 0);
737 }
738
739 stream_putw_at (s, 0, stream_get_endp (s));
740
ajs719e9742005-02-28 20:52:15 +0000741 return zebra_server_send_message(client);
paul718e3742002-12-13 20:15:29 +0000742}
David Lamparter6b0655a2014-06-04 06:53:35 +0200743
hasso18a6dce2004-10-03 18:18:34 +0000744/* Router-id is updated. Send ZEBRA_ROUTER_ID_ADD to client. */
745int
Feng Luac19a442015-05-22 11:40:07 +0200746zsend_router_id_update (struct zserv *client, struct prefix *p,
747 vrf_id_t vrf_id)
hasso18a6dce2004-10-03 18:18:34 +0000748{
749 struct stream *s;
750 int blen;
751
752 /* Check this client need interface information. */
Feng Luc99f3482014-10-16 09:52:36 +0800753 if (! vrf_bitmap_check (client->ridinfo, vrf_id))
ajs719e9742005-02-28 20:52:15 +0000754 return 0;
hasso18a6dce2004-10-03 18:18:34 +0000755
756 s = client->obuf;
757 stream_reset (s);
758
hasso18a6dce2004-10-03 18:18:34 +0000759 /* Message type. */
Feng Luc99f3482014-10-16 09:52:36 +0800760 zserv_create_header (s, ZEBRA_ROUTER_ID_UPDATE, vrf_id);
hasso18a6dce2004-10-03 18:18:34 +0000761
762 /* Prefix information. */
763 stream_putc (s, p->family);
764 blen = prefix_blen (p);
765 stream_put (s, &p->u.prefix, blen);
766 stream_putc (s, p->prefixlen);
767
768 /* Write packet size. */
769 stream_putw_at (s, 0, stream_get_endp (s));
770
ajs719e9742005-02-28 20:52:15 +0000771 return zebra_server_send_message(client);
hasso18a6dce2004-10-03 18:18:34 +0000772}
David Lamparter6b0655a2014-06-04 06:53:35 +0200773
paul718e3742002-12-13 20:15:29 +0000774/* Register zebra server interface information. Send current all
775 interface and address information. */
ajs719e9742005-02-28 20:52:15 +0000776static int
Feng Luc99f3482014-10-16 09:52:36 +0800777zread_interface_add (struct zserv *client, u_short length, vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +0000778{
paul1eb8ef22005-04-07 07:30:20 +0000779 struct listnode *ifnode, *ifnnode;
780 struct listnode *cnode, *cnnode;
paul718e3742002-12-13 20:15:29 +0000781 struct interface *ifp;
782 struct connected *c;
783
784 /* Interface information is needed. */
Feng Luc99f3482014-10-16 09:52:36 +0800785 vrf_bitmap_set (client->ifinfo, vrf_id);
paul718e3742002-12-13 20:15:29 +0000786
Feng Luc99f3482014-10-16 09:52:36 +0800787 for (ALL_LIST_ELEMENTS (vrf_iflist (vrf_id), ifnode, ifnnode, ifp))
paul718e3742002-12-13 20:15:29 +0000788 {
paul718e3742002-12-13 20:15:29 +0000789 /* Skip pseudo interface. */
790 if (! CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE))
791 continue;
792
ajs719e9742005-02-28 20:52:15 +0000793 if (zsend_interface_add (client, ifp) < 0)
794 return -1;
paul718e3742002-12-13 20:15:29 +0000795
paul1eb8ef22005-04-07 07:30:20 +0000796 for (ALL_LIST_ELEMENTS (ifp->connected, cnode, cnnode, c))
paul718e3742002-12-13 20:15:29 +0000797 {
ajs719e9742005-02-28 20:52:15 +0000798 if (CHECK_FLAG (c->conf, ZEBRA_IFC_REAL) &&
799 (zsend_interface_address (ZEBRA_INTERFACE_ADDRESS_ADD, client,
800 ifp, c) < 0))
801 return -1;
paul718e3742002-12-13 20:15:29 +0000802 }
803 }
ajs719e9742005-02-28 20:52:15 +0000804 return 0;
paul718e3742002-12-13 20:15:29 +0000805}
806
807/* Unregister zebra server interface information. */
ajs719e9742005-02-28 20:52:15 +0000808static int
Feng Luc99f3482014-10-16 09:52:36 +0800809zread_interface_delete (struct zserv *client, u_short length, vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +0000810{
Feng Luc99f3482014-10-16 09:52:36 +0800811 vrf_bitmap_unset (client->ifinfo, vrf_id);
ajs719e9742005-02-28 20:52:15 +0000812 return 0;
paul718e3742002-12-13 20:15:29 +0000813}
814
815/* This function support multiple nexthop. */
paulb9df2d22004-05-09 09:09:59 +0000816/*
817 * Parse the ZEBRA_IPV4_ROUTE_ADD sent from client. Update rib and
818 * add kernel route.
819 */
ajs719e9742005-02-28 20:52:15 +0000820static int
Feng Luc99f3482014-10-16 09:52:36 +0800821zread_ipv4_add (struct zserv *client, u_short length, vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +0000822{
823 int i;
824 struct rib *rib;
825 struct prefix_ipv4 p;
826 u_char message;
827 struct in_addr nexthop;
828 u_char nexthop_num;
829 u_char nexthop_type;
830 struct stream *s;
Paul Jakma9099f9b2016-01-18 10:12:10 +0000831 ifindex_t ifindex;
paul718e3742002-12-13 20:15:29 +0000832 u_char ifname_len;
G.Balajicddf3912011-11-26 21:59:32 +0400833 safi_t safi;
834
paul718e3742002-12-13 20:15:29 +0000835
836 /* Get input stream. */
837 s = client->ibuf;
838
839 /* Allocate new rib. */
paul4d38fdb2005-04-28 17:35:14 +0000840 rib = XCALLOC (MTYPE_RIB, sizeof (struct rib));
841
paul718e3742002-12-13 20:15:29 +0000842 /* Type, flags, message. */
843 rib->type = stream_getc (s);
844 rib->flags = stream_getc (s);
paulb9df2d22004-05-09 09:09:59 +0000845 message = stream_getc (s);
G.Balajicddf3912011-11-26 21:59:32 +0400846 safi = stream_getw (s);
paul718e3742002-12-13 20:15:29 +0000847 rib->uptime = time (NULL);
848
849 /* IPv4 prefix. */
850 memset (&p, 0, sizeof (struct prefix_ipv4));
851 p.family = AF_INET;
852 p.prefixlen = stream_getc (s);
853 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
854
Feng Lu0d0686f2015-05-22 11:40:02 +0200855 /* VRF ID */
Feng Luc99f3482014-10-16 09:52:36 +0800856 rib->vrf_id = vrf_id;
Feng Lu0d0686f2015-05-22 11:40:02 +0200857
paul718e3742002-12-13 20:15:29 +0000858 /* Nexthop parse. */
859 if (CHECK_FLAG (message, ZAPI_MESSAGE_NEXTHOP))
860 {
861 nexthop_num = stream_getc (s);
862
863 for (i = 0; i < nexthop_num; i++)
864 {
865 nexthop_type = stream_getc (s);
866
867 switch (nexthop_type)
868 {
869 case ZEBRA_NEXTHOP_IFINDEX:
870 ifindex = stream_getl (s);
871 nexthop_ifindex_add (rib, ifindex);
872 break;
873 case ZEBRA_NEXTHOP_IFNAME:
874 ifname_len = stream_getc (s);
paul9985f832005-02-09 15:51:56 +0000875 stream_forward_getp (s, ifname_len);
paul718e3742002-12-13 20:15:29 +0000876 break;
877 case ZEBRA_NEXTHOP_IPV4:
878 nexthop.s_addr = stream_get_ipv4 (s);
Paul Jakma7514fb72007-05-02 16:05:35 +0000879 nexthop_ipv4_add (rib, &nexthop, NULL);
paul718e3742002-12-13 20:15:29 +0000880 break;
Joakim Tjernlundc963c202012-07-07 17:06:13 +0200881 case ZEBRA_NEXTHOP_IPV4_IFINDEX:
882 nexthop.s_addr = stream_get_ipv4 (s);
883 ifindex = stream_getl (s);
884 nexthop_ipv4_ifindex_add (rib, &nexthop, NULL, ifindex);
885 break;
paul718e3742002-12-13 20:15:29 +0000886 case ZEBRA_NEXTHOP_IPV6:
paul9985f832005-02-09 15:51:56 +0000887 stream_forward_getp (s, IPV6_MAX_BYTELEN);
paul718e3742002-12-13 20:15:29 +0000888 break;
Subbaiah Venkata6902c692012-03-27 19:21:29 -0700889 case ZEBRA_NEXTHOP_BLACKHOLE:
890 nexthop_blackhole_add (rib);
891 break;
892 }
paul718e3742002-12-13 20:15:29 +0000893 }
894 }
895
896 /* Distance. */
897 if (CHECK_FLAG (message, ZAPI_MESSAGE_DISTANCE))
898 rib->distance = stream_getc (s);
899
900 /* Metric. */
901 if (CHECK_FLAG (message, ZAPI_MESSAGE_METRIC))
902 rib->metric = stream_getl (s);
903
Timo Teräsb11f3b52015-11-02 16:50:07 +0200904 if (CHECK_FLAG (message, ZAPI_MESSAGE_MTU))
905 rib->mtu = stream_getl (s);
906
Paul Jakma171eee32006-07-27 16:11:02 +0000907 /* Table */
908 rib->table=zebrad.rtm_table_default;
G.Balajicddf3912011-11-26 21:59:32 +0400909 rib_add_ipv4_multipath (&p, rib, safi);
ajs719e9742005-02-28 20:52:15 +0000910 return 0;
paul718e3742002-12-13 20:15:29 +0000911}
912
913/* Zebra server IPv4 prefix delete function. */
ajs719e9742005-02-28 20:52:15 +0000914static int
Feng Luc99f3482014-10-16 09:52:36 +0800915zread_ipv4_delete (struct zserv *client, u_short length, vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +0000916{
917 int i;
918 struct stream *s;
919 struct zapi_ipv4 api;
Subbaiah Venkata6902c692012-03-27 19:21:29 -0700920 struct in_addr nexthop, *nexthop_p;
paul718e3742002-12-13 20:15:29 +0000921 unsigned long ifindex;
922 struct prefix_ipv4 p;
923 u_char nexthop_num;
924 u_char nexthop_type;
925 u_char ifname_len;
926
927 s = client->ibuf;
928 ifindex = 0;
929 nexthop.s_addr = 0;
Subbaiah Venkata6902c692012-03-27 19:21:29 -0700930 nexthop_p = NULL;
paul718e3742002-12-13 20:15:29 +0000931
932 /* Type, flags, message. */
933 api.type = stream_getc (s);
934 api.flags = stream_getc (s);
935 api.message = stream_getc (s);
G.Balajicddf3912011-11-26 21:59:32 +0400936 api.safi = stream_getw (s);
paul718e3742002-12-13 20:15:29 +0000937
938 /* IPv4 prefix. */
939 memset (&p, 0, sizeof (struct prefix_ipv4));
940 p.family = AF_INET;
941 p.prefixlen = stream_getc (s);
942 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
943
944 /* Nexthop, ifindex, distance, metric. */
945 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
946 {
947 nexthop_num = stream_getc (s);
948
949 for (i = 0; i < nexthop_num; i++)
950 {
951 nexthop_type = stream_getc (s);
952
953 switch (nexthop_type)
954 {
955 case ZEBRA_NEXTHOP_IFINDEX:
956 ifindex = stream_getl (s);
957 break;
958 case ZEBRA_NEXTHOP_IFNAME:
959 ifname_len = stream_getc (s);
paul9985f832005-02-09 15:51:56 +0000960 stream_forward_getp (s, ifname_len);
paul718e3742002-12-13 20:15:29 +0000961 break;
962 case ZEBRA_NEXTHOP_IPV4:
963 nexthop.s_addr = stream_get_ipv4 (s);
Subbaiah Venkata6902c692012-03-27 19:21:29 -0700964 nexthop_p = &nexthop;
paul718e3742002-12-13 20:15:29 +0000965 break;
Joakim Tjernlundc963c202012-07-07 17:06:13 +0200966 case ZEBRA_NEXTHOP_IPV4_IFINDEX:
967 nexthop.s_addr = stream_get_ipv4 (s);
Christian Franke23f5f7c2013-11-27 17:06:14 +0000968 nexthop_p = &nexthop;
Joakim Tjernlundc963c202012-07-07 17:06:13 +0200969 ifindex = stream_getl (s);
970 break;
paul718e3742002-12-13 20:15:29 +0000971 case ZEBRA_NEXTHOP_IPV6:
paul9985f832005-02-09 15:51:56 +0000972 stream_forward_getp (s, IPV6_MAX_BYTELEN);
paul718e3742002-12-13 20:15:29 +0000973 break;
974 }
975 }
976 }
977
978 /* Distance. */
979 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
980 api.distance = stream_getc (s);
981 else
982 api.distance = 0;
983
984 /* Metric. */
985 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
986 api.metric = stream_getl (s);
987 else
988 api.metric = 0;
989
Subbaiah Venkata6902c692012-03-27 19:21:29 -0700990 rib_delete_ipv4 (api.type, api.flags, &p, nexthop_p, ifindex,
Feng Luc99f3482014-10-16 09:52:36 +0800991 vrf_id, api.safi);
ajs719e9742005-02-28 20:52:15 +0000992 return 0;
paul718e3742002-12-13 20:15:29 +0000993}
994
995/* Nexthop lookup for IPv4. */
ajs719e9742005-02-28 20:52:15 +0000996static int
Feng Luc99f3482014-10-16 09:52:36 +0800997zread_ipv4_nexthop_lookup (struct zserv *client, u_short length,
998 vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +0000999{
1000 struct in_addr addr;
Christian Frankebb97e462013-05-25 14:01:35 +00001001 char buf[BUFSIZ];
paul718e3742002-12-13 20:15:29 +00001002
1003 addr.s_addr = stream_get_ipv4 (client->ibuf);
Christian Frankebb97e462013-05-25 14:01:35 +00001004 if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
1005 zlog_debug("%s: looking up %s", __func__,
1006 inet_ntop (AF_INET, &addr, buf, BUFSIZ));
Feng Luc99f3482014-10-16 09:52:36 +08001007 return zsend_ipv4_nexthop_lookup (client, addr, vrf_id);
paul718e3742002-12-13 20:15:29 +00001008}
1009
Everton Marques4e5275b2014-07-01 15:15:52 -03001010/* MRIB Nexthop lookup for IPv4. */
1011static int
Feng Luc99f3482014-10-16 09:52:36 +08001012zread_ipv4_nexthop_lookup_mrib (struct zserv *client, u_short length,
1013 vrf_id_t vrf_id)
Everton Marques4e5275b2014-07-01 15:15:52 -03001014{
1015 struct in_addr addr;
David Lamparterbd078122015-01-06 19:53:24 +01001016 struct rib *rib;
Everton Marques4e5275b2014-07-01 15:15:52 -03001017
1018 addr.s_addr = stream_get_ipv4 (client->ibuf);
Feng Luc99f3482014-10-16 09:52:36 +08001019 rib = rib_match_ipv4_multicast (addr, NULL, vrf_id);
David Lamparterbd078122015-01-06 19:53:24 +01001020 return zsend_ipv4_nexthop_lookup_mrib (client, addr, rib);
Everton Marques4e5275b2014-07-01 15:15:52 -03001021}
1022
paul718e3742002-12-13 20:15:29 +00001023/* Nexthop lookup for IPv4. */
ajs719e9742005-02-28 20:52:15 +00001024static int
Feng Luc99f3482014-10-16 09:52:36 +08001025zread_ipv4_import_lookup (struct zserv *client, u_short length,
1026 vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +00001027{
1028 struct prefix_ipv4 p;
1029
1030 p.family = AF_INET;
1031 p.prefixlen = stream_getc (client->ibuf);
1032 p.prefix.s_addr = stream_get_ipv4 (client->ibuf);
1033
Feng Luc99f3482014-10-16 09:52:36 +08001034 return zsend_ipv4_import_lookup (client, &p, vrf_id);
paul718e3742002-12-13 20:15:29 +00001035}
1036
1037#ifdef HAVE_IPV6
1038/* Zebra server IPv6 prefix add function. */
ajs719e9742005-02-28 20:52:15 +00001039static int
Feng Luc99f3482014-10-16 09:52:36 +08001040zread_ipv6_add (struct zserv *client, u_short length, vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +00001041{
1042 int i;
1043 struct stream *s;
1044 struct zapi_ipv6 api;
1045 struct in6_addr nexthop;
1046 unsigned long ifindex;
1047 struct prefix_ipv6 p;
1048
1049 s = client->ibuf;
1050 ifindex = 0;
1051 memset (&nexthop, 0, sizeof (struct in6_addr));
1052
1053 /* Type, flags, message. */
1054 api.type = stream_getc (s);
1055 api.flags = stream_getc (s);
1056 api.message = stream_getc (s);
G.Balajif768f362011-11-26 22:10:39 +04001057 api.safi = stream_getw (s);
paul718e3742002-12-13 20:15:29 +00001058
1059 /* IPv4 prefix. */
1060 memset (&p, 0, sizeof (struct prefix_ipv6));
1061 p.family = AF_INET6;
1062 p.prefixlen = stream_getc (s);
1063 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
1064
1065 /* Nexthop, ifindex, distance, metric. */
1066 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
1067 {
1068 u_char nexthop_type;
1069
1070 api.nexthop_num = stream_getc (s);
1071 for (i = 0; i < api.nexthop_num; i++)
1072 {
1073 nexthop_type = stream_getc (s);
1074
1075 switch (nexthop_type)
1076 {
1077 case ZEBRA_NEXTHOP_IPV6:
1078 stream_get (&nexthop, s, 16);
1079 break;
1080 case ZEBRA_NEXTHOP_IFINDEX:
1081 ifindex = stream_getl (s);
1082 break;
1083 }
1084 }
1085 }
1086
1087 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
1088 api.distance = stream_getc (s);
1089 else
1090 api.distance = 0;
1091
1092 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
1093 api.metric = stream_getl (s);
1094 else
1095 api.metric = 0;
Timo Teräsb11f3b52015-11-02 16:50:07 +02001096
1097 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_MTU))
1098 api.mtu = stream_getl (s);
1099 else
1100 api.mtu = 0;
paul718e3742002-12-13 20:15:29 +00001101
1102 if (IN6_IS_ADDR_UNSPECIFIED (&nexthop))
Feng Lu0d0686f2015-05-22 11:40:02 +02001103 rib_add_ipv6 (api.type, api.flags, &p, NULL, ifindex,
Feng Luc99f3482014-10-16 09:52:36 +08001104 vrf_id, zebrad.rtm_table_default, api.metric,
Timo Teräsb11f3b52015-11-02 16:50:07 +02001105 api.mtu, api.distance, api.safi);
paul718e3742002-12-13 20:15:29 +00001106 else
Feng Lu0d0686f2015-05-22 11:40:02 +02001107 rib_add_ipv6 (api.type, api.flags, &p, &nexthop, ifindex,
Feng Luc99f3482014-10-16 09:52:36 +08001108 vrf_id, zebrad.rtm_table_default, api.metric,
Timo Teräsb11f3b52015-11-02 16:50:07 +02001109 api.mtu, api.distance, api.safi);
ajs719e9742005-02-28 20:52:15 +00001110 return 0;
paul718e3742002-12-13 20:15:29 +00001111}
1112
1113/* Zebra server IPv6 prefix delete function. */
ajs719e9742005-02-28 20:52:15 +00001114static int
Feng Luc99f3482014-10-16 09:52:36 +08001115zread_ipv6_delete (struct zserv *client, u_short length, vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +00001116{
1117 int i;
1118 struct stream *s;
1119 struct zapi_ipv6 api;
1120 struct in6_addr nexthop;
1121 unsigned long ifindex;
1122 struct prefix_ipv6 p;
1123
1124 s = client->ibuf;
1125 ifindex = 0;
1126 memset (&nexthop, 0, sizeof (struct in6_addr));
1127
1128 /* Type, flags, message. */
1129 api.type = stream_getc (s);
1130 api.flags = stream_getc (s);
1131 api.message = stream_getc (s);
G.Balajif768f362011-11-26 22:10:39 +04001132 api.safi = stream_getw (s);
paul718e3742002-12-13 20:15:29 +00001133
1134 /* IPv4 prefix. */
1135 memset (&p, 0, sizeof (struct prefix_ipv6));
1136 p.family = AF_INET6;
1137 p.prefixlen = stream_getc (s);
1138 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
1139
1140 /* Nexthop, ifindex, distance, metric. */
1141 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
1142 {
1143 u_char nexthop_type;
1144
1145 api.nexthop_num = stream_getc (s);
1146 for (i = 0; i < api.nexthop_num; i++)
1147 {
1148 nexthop_type = stream_getc (s);
1149
1150 switch (nexthop_type)
1151 {
1152 case ZEBRA_NEXTHOP_IPV6:
1153 stream_get (&nexthop, s, 16);
1154 break;
1155 case ZEBRA_NEXTHOP_IFINDEX:
1156 ifindex = stream_getl (s);
1157 break;
1158 }
1159 }
1160 }
1161
1162 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
1163 api.distance = stream_getc (s);
1164 else
1165 api.distance = 0;
1166 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
1167 api.metric = stream_getl (s);
1168 else
1169 api.metric = 0;
1170
1171 if (IN6_IS_ADDR_UNSPECIFIED (&nexthop))
Feng Luc99f3482014-10-16 09:52:36 +08001172 rib_delete_ipv6 (api.type, api.flags, &p, NULL, ifindex, vrf_id,
Feng Lu0d0686f2015-05-22 11:40:02 +02001173 api.safi);
paul718e3742002-12-13 20:15:29 +00001174 else
Feng Luc99f3482014-10-16 09:52:36 +08001175 rib_delete_ipv6 (api.type, api.flags, &p, &nexthop, ifindex, vrf_id,
Feng Lu0d0686f2015-05-22 11:40:02 +02001176 api.safi);
ajs719e9742005-02-28 20:52:15 +00001177 return 0;
paul718e3742002-12-13 20:15:29 +00001178}
1179
ajs719e9742005-02-28 20:52:15 +00001180static int
Feng Luc99f3482014-10-16 09:52:36 +08001181zread_ipv6_nexthop_lookup (struct zserv *client, u_short length,
1182 vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +00001183{
1184 struct in6_addr addr;
1185 char buf[BUFSIZ];
1186
1187 stream_get (&addr, client->ibuf, 16);
Christian Frankea5207082013-04-11 08:24:29 +00001188 if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
1189 zlog_debug("%s: looking up %s", __func__,
1190 inet_ntop (AF_INET6, &addr, buf, BUFSIZ));
paul718e3742002-12-13 20:15:29 +00001191
Feng Luc99f3482014-10-16 09:52:36 +08001192 return zsend_ipv6_nexthop_lookup (client, &addr, vrf_id);
paul718e3742002-12-13 20:15:29 +00001193}
1194#endif /* HAVE_IPV6 */
1195
hasso18a6dce2004-10-03 18:18:34 +00001196/* Register zebra server router-id information. Send current router-id */
ajs719e9742005-02-28 20:52:15 +00001197static int
Feng Luc99f3482014-10-16 09:52:36 +08001198zread_router_id_add (struct zserv *client, u_short length, vrf_id_t vrf_id)
hasso18a6dce2004-10-03 18:18:34 +00001199{
1200 struct prefix p;
1201
1202 /* Router-id information is needed. */
Feng Luc99f3482014-10-16 09:52:36 +08001203 vrf_bitmap_set (client->ridinfo, vrf_id);
hasso18a6dce2004-10-03 18:18:34 +00001204
Feng Luc99f3482014-10-16 09:52:36 +08001205 router_id_get (&p, vrf_id);
hasso18a6dce2004-10-03 18:18:34 +00001206
Feng Luc99f3482014-10-16 09:52:36 +08001207 return zsend_router_id_update (client, &p, vrf_id);
hasso18a6dce2004-10-03 18:18:34 +00001208}
1209
1210/* Unregister zebra server router-id information. */
ajs719e9742005-02-28 20:52:15 +00001211static int
Feng Luc99f3482014-10-16 09:52:36 +08001212zread_router_id_delete (struct zserv *client, u_short length, vrf_id_t vrf_id)
hasso18a6dce2004-10-03 18:18:34 +00001213{
Feng Luc99f3482014-10-16 09:52:36 +08001214 vrf_bitmap_unset (client->ridinfo, vrf_id);
ajs719e9742005-02-28 20:52:15 +00001215 return 0;
hasso18a6dce2004-10-03 18:18:34 +00001216}
1217
Vyacheslav Trushkin2ea1ab12011-12-11 18:48:47 +04001218/* Tie up route-type and client->sock */
1219static void
1220zread_hello (struct zserv *client)
1221{
1222 /* type of protocol (lib/zebra.h) */
1223 u_char proto;
1224 proto = stream_getc (client->ibuf);
1225
1226 /* accept only dynamic routing protocols */
1227 if ((proto < ZEBRA_ROUTE_MAX)
1228 && (proto > ZEBRA_ROUTE_STATIC))
1229 {
1230 zlog_notice ("client %d says hello and bids fair to announce only %s routes",
1231 client->sock, zebra_route_string(proto));
1232
1233 /* if route-type was binded by other client */
1234 if (route_type_oaths[proto])
1235 zlog_warn ("sender of %s routes changed %c->%c",
1236 zebra_route_string(proto), route_type_oaths[proto],
1237 client->sock);
1238
1239 route_type_oaths[proto] = client->sock;
1240 }
1241}
1242
Feng Luc99f3482014-10-16 09:52:36 +08001243/* Unregister all information in a VRF. */
1244static int
1245zread_vrf_unregister (struct zserv *client, u_short length, vrf_id_t vrf_id)
1246{
1247 int i;
1248
1249 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
1250 vrf_bitmap_unset (client->redist[i], vrf_id);
1251 vrf_bitmap_unset (client->redist_default, vrf_id);
1252 vrf_bitmap_unset (client->ifinfo, vrf_id);
1253 vrf_bitmap_unset (client->ridinfo, vrf_id);
1254
1255 return 0;
1256}
1257
Vyacheslav Trushkin2ea1ab12011-12-11 18:48:47 +04001258/* If client sent routes of specific type, zebra removes it
1259 * and returns number of deleted routes.
1260 */
1261static void
1262zebra_score_rib (int client_sock)
1263{
1264 int i;
1265
1266 for (i = ZEBRA_ROUTE_RIP; i < ZEBRA_ROUTE_MAX; i++)
1267 if (client_sock == route_type_oaths[i])
1268 {
1269 zlog_notice ("client %d disconnected. %lu %s routes removed from the rib",
1270 client_sock, rib_score_proto (i), zebra_route_string (i));
1271 route_type_oaths[i] = 0;
1272 break;
1273 }
1274}
1275
paul718e3742002-12-13 20:15:29 +00001276/* Close zebra client. */
paulb9df2d22004-05-09 09:09:59 +00001277static void
paul718e3742002-12-13 20:15:29 +00001278zebra_client_close (struct zserv *client)
1279{
1280 /* Close file descriptor. */
1281 if (client->sock)
1282 {
1283 close (client->sock);
Vyacheslav Trushkin2ea1ab12011-12-11 18:48:47 +04001284 zebra_score_rib (client->sock);
paul718e3742002-12-13 20:15:29 +00001285 client->sock = -1;
1286 }
1287
1288 /* Free stream buffers. */
1289 if (client->ibuf)
1290 stream_free (client->ibuf);
1291 if (client->obuf)
1292 stream_free (client->obuf);
ajs719e9742005-02-28 20:52:15 +00001293 if (client->wb)
1294 buffer_free(client->wb);
paul718e3742002-12-13 20:15:29 +00001295
1296 /* Release threads. */
1297 if (client->t_read)
1298 thread_cancel (client->t_read);
1299 if (client->t_write)
1300 thread_cancel (client->t_write);
ajs719e9742005-02-28 20:52:15 +00001301 if (client->t_suicide)
1302 thread_cancel (client->t_suicide);
paul718e3742002-12-13 20:15:29 +00001303
1304 /* Free client structure. */
paulb21b19c2003-06-15 01:28:29 +00001305 listnode_delete (zebrad.client_list, client);
paul718e3742002-12-13 20:15:29 +00001306 XFREE (0, client);
1307}
1308
1309/* Make new client. */
paulb9df2d22004-05-09 09:09:59 +00001310static void
paul718e3742002-12-13 20:15:29 +00001311zebra_client_create (int sock)
1312{
1313 struct zserv *client;
Feng Luc99f3482014-10-16 09:52:36 +08001314 int i;
paul718e3742002-12-13 20:15:29 +00001315
1316 client = XCALLOC (0, sizeof (struct zserv));
1317
1318 /* Make client input/output buffer. */
1319 client->sock = sock;
1320 client->ibuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
1321 client->obuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
ajs719e9742005-02-28 20:52:15 +00001322 client->wb = buffer_new(0);
paul718e3742002-12-13 20:15:29 +00001323
1324 /* Set table number. */
paulb21b19c2003-06-15 01:28:29 +00001325 client->rtm_table = zebrad.rtm_table_default;
paul718e3742002-12-13 20:15:29 +00001326
Feng Luc99f3482014-10-16 09:52:36 +08001327 /* Initialize flags */
1328 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
1329 client->redist[i] = vrf_bitmap_init ();
1330 client->redist_default = vrf_bitmap_init ();
1331 client->ifinfo = vrf_bitmap_init ();
1332 client->ridinfo = vrf_bitmap_init ();
1333
paul718e3742002-12-13 20:15:29 +00001334 /* Add this client to linked list. */
paulb21b19c2003-06-15 01:28:29 +00001335 listnode_add (zebrad.client_list, client);
paul718e3742002-12-13 20:15:29 +00001336
1337 /* Make new read thread. */
1338 zebra_event (ZEBRA_READ, sock, client);
1339}
1340
1341/* Handler of zebra service request. */
paulb9df2d22004-05-09 09:09:59 +00001342static int
paul718e3742002-12-13 20:15:29 +00001343zebra_client_read (struct thread *thread)
1344{
1345 int sock;
1346 struct zserv *client;
ajs57a14772005-04-10 15:01:56 +00001347 size_t already;
paulc1b98002006-01-16 01:54:02 +00001348 uint16_t length, command;
1349 uint8_t marker, version;
Feng Luc99f3482014-10-16 09:52:36 +08001350 vrf_id_t vrf_id;
paul718e3742002-12-13 20:15:29 +00001351
1352 /* Get thread data. Reset reading thread because I'm running. */
1353 sock = THREAD_FD (thread);
1354 client = THREAD_ARG (thread);
1355 client->t_read = NULL;
1356
ajs719e9742005-02-28 20:52:15 +00001357 if (client->t_suicide)
paul718e3742002-12-13 20:15:29 +00001358 {
ajs719e9742005-02-28 20:52:15 +00001359 zebra_client_close(client);
paul718e3742002-12-13 20:15:29 +00001360 return -1;
1361 }
ajs719e9742005-02-28 20:52:15 +00001362
1363 /* Read length and command (if we don't have it already). */
ajs57a14772005-04-10 15:01:56 +00001364 if ((already = stream_get_endp(client->ibuf)) < ZEBRA_HEADER_SIZE)
ajs719e9742005-02-28 20:52:15 +00001365 {
ajs57a14772005-04-10 15:01:56 +00001366 ssize_t nbyte;
ajs719e9742005-02-28 20:52:15 +00001367 if (((nbyte = stream_read_try (client->ibuf, sock,
ajs57a14772005-04-10 15:01:56 +00001368 ZEBRA_HEADER_SIZE-already)) == 0) ||
ajs719e9742005-02-28 20:52:15 +00001369 (nbyte == -1))
1370 {
1371 if (IS_ZEBRA_DEBUG_EVENT)
1372 zlog_debug ("connection closed socket [%d]", sock);
1373 zebra_client_close (client);
1374 return -1;
1375 }
ajs57a14772005-04-10 15:01:56 +00001376 if (nbyte != (ssize_t)(ZEBRA_HEADER_SIZE-already))
ajs719e9742005-02-28 20:52:15 +00001377 {
1378 /* Try again later. */
1379 zebra_event (ZEBRA_READ, sock, client);
1380 return 0;
1381 }
ajs57a14772005-04-10 15:01:56 +00001382 already = ZEBRA_HEADER_SIZE;
ajs719e9742005-02-28 20:52:15 +00001383 }
1384
1385 /* Reset to read from the beginning of the incoming packet. */
1386 stream_set_getp(client->ibuf, 0);
1387
paulc1b98002006-01-16 01:54:02 +00001388 /* Fetch header values */
paul718e3742002-12-13 20:15:29 +00001389 length = stream_getw (client->ibuf);
paulc1b98002006-01-16 01:54:02 +00001390 marker = stream_getc (client->ibuf);
1391 version = stream_getc (client->ibuf);
Feng Luc99f3482014-10-16 09:52:36 +08001392 vrf_id = stream_getw (client->ibuf);
paulc1b98002006-01-16 01:54:02 +00001393 command = stream_getw (client->ibuf);
paul718e3742002-12-13 20:15:29 +00001394
paulc1b98002006-01-16 01:54:02 +00001395 if (marker != ZEBRA_HEADER_MARKER || version != ZSERV_VERSION)
1396 {
1397 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
1398 __func__, sock, marker, version);
1399 zebra_client_close (client);
1400 return -1;
1401 }
ajs719e9742005-02-28 20:52:15 +00001402 if (length < ZEBRA_HEADER_SIZE)
paul718e3742002-12-13 20:15:29 +00001403 {
ajs57a14772005-04-10 15:01:56 +00001404 zlog_warn("%s: socket %d message length %u is less than header size %d",
1405 __func__, sock, length, ZEBRA_HEADER_SIZE);
1406 zebra_client_close (client);
1407 return -1;
1408 }
1409 if (length > STREAM_SIZE(client->ibuf))
1410 {
1411 zlog_warn("%s: socket %d message length %u exceeds buffer size %lu",
1412 __func__, sock, length, (u_long)STREAM_SIZE(client->ibuf));
paul718e3742002-12-13 20:15:29 +00001413 zebra_client_close (client);
1414 return -1;
1415 }
1416
paul718e3742002-12-13 20:15:29 +00001417 /* Read rest of data. */
ajs57a14772005-04-10 15:01:56 +00001418 if (already < length)
paul718e3742002-12-13 20:15:29 +00001419 {
ajs57a14772005-04-10 15:01:56 +00001420 ssize_t nbyte;
1421 if (((nbyte = stream_read_try (client->ibuf, sock,
1422 length-already)) == 0) ||
1423 (nbyte == -1))
paul718e3742002-12-13 20:15:29 +00001424 {
1425 if (IS_ZEBRA_DEBUG_EVENT)
ajsb6178002004-12-07 21:12:56 +00001426 zlog_debug ("connection closed [%d] when reading zebra data", sock);
paul718e3742002-12-13 20:15:29 +00001427 zebra_client_close (client);
1428 return -1;
1429 }
ajs57a14772005-04-10 15:01:56 +00001430 if (nbyte != (ssize_t)(length-already))
ajs719e9742005-02-28 20:52:15 +00001431 {
1432 /* Try again later. */
1433 zebra_event (ZEBRA_READ, sock, client);
1434 return 0;
1435 }
paul718e3742002-12-13 20:15:29 +00001436 }
1437
ajs719e9742005-02-28 20:52:15 +00001438 length -= ZEBRA_HEADER_SIZE;
1439
paul718e3742002-12-13 20:15:29 +00001440 /* Debug packet information. */
1441 if (IS_ZEBRA_DEBUG_EVENT)
ajsb6178002004-12-07 21:12:56 +00001442 zlog_debug ("zebra message comes from socket [%d]", sock);
paul718e3742002-12-13 20:15:29 +00001443
1444 if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
Feng Luc99f3482014-10-16 09:52:36 +08001445 zlog_debug ("zebra message received [%s] %d in VRF %u",
1446 zserv_command_string (command), length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001447
1448 switch (command)
1449 {
hasso18a6dce2004-10-03 18:18:34 +00001450 case ZEBRA_ROUTER_ID_ADD:
Feng Luc99f3482014-10-16 09:52:36 +08001451 zread_router_id_add (client, length, vrf_id);
hasso18a6dce2004-10-03 18:18:34 +00001452 break;
1453 case ZEBRA_ROUTER_ID_DELETE:
Feng Luc99f3482014-10-16 09:52:36 +08001454 zread_router_id_delete (client, length, vrf_id);
hasso18a6dce2004-10-03 18:18:34 +00001455 break;
paul718e3742002-12-13 20:15:29 +00001456 case ZEBRA_INTERFACE_ADD:
Feng Luc99f3482014-10-16 09:52:36 +08001457 zread_interface_add (client, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001458 break;
1459 case ZEBRA_INTERFACE_DELETE:
Feng Luc99f3482014-10-16 09:52:36 +08001460 zread_interface_delete (client, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001461 break;
1462 case ZEBRA_IPV4_ROUTE_ADD:
Feng Luc99f3482014-10-16 09:52:36 +08001463 zread_ipv4_add (client, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001464 break;
1465 case ZEBRA_IPV4_ROUTE_DELETE:
Feng Luc99f3482014-10-16 09:52:36 +08001466 zread_ipv4_delete (client, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001467 break;
1468#ifdef HAVE_IPV6
1469 case ZEBRA_IPV6_ROUTE_ADD:
Feng Luc99f3482014-10-16 09:52:36 +08001470 zread_ipv6_add (client, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001471 break;
1472 case ZEBRA_IPV6_ROUTE_DELETE:
Feng Luc99f3482014-10-16 09:52:36 +08001473 zread_ipv6_delete (client, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001474 break;
1475#endif /* HAVE_IPV6 */
1476 case ZEBRA_REDISTRIBUTE_ADD:
Feng Luc99f3482014-10-16 09:52:36 +08001477 zebra_redistribute_add (command, client, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001478 break;
1479 case ZEBRA_REDISTRIBUTE_DELETE:
Feng Luc99f3482014-10-16 09:52:36 +08001480 zebra_redistribute_delete (command, client, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001481 break;
1482 case ZEBRA_REDISTRIBUTE_DEFAULT_ADD:
Feng Luc99f3482014-10-16 09:52:36 +08001483 zebra_redistribute_default_add (command, client, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001484 break;
1485 case ZEBRA_REDISTRIBUTE_DEFAULT_DELETE:
Feng Luc99f3482014-10-16 09:52:36 +08001486 zebra_redistribute_default_delete (command, client, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001487 break;
1488 case ZEBRA_IPV4_NEXTHOP_LOOKUP:
Feng Luc99f3482014-10-16 09:52:36 +08001489 zread_ipv4_nexthop_lookup (client, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001490 break;
Everton Marques4e5275b2014-07-01 15:15:52 -03001491 case ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB:
Feng Luc99f3482014-10-16 09:52:36 +08001492 zread_ipv4_nexthop_lookup_mrib (client, length, vrf_id);
Everton Marques4e5275b2014-07-01 15:15:52 -03001493 break;
paul718e3742002-12-13 20:15:29 +00001494#ifdef HAVE_IPV6
1495 case ZEBRA_IPV6_NEXTHOP_LOOKUP:
Feng Luc99f3482014-10-16 09:52:36 +08001496 zread_ipv6_nexthop_lookup (client, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001497 break;
1498#endif /* HAVE_IPV6 */
1499 case ZEBRA_IPV4_IMPORT_LOOKUP:
Feng Luc99f3482014-10-16 09:52:36 +08001500 zread_ipv4_import_lookup (client, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001501 break;
Vyacheslav Trushkin2ea1ab12011-12-11 18:48:47 +04001502 case ZEBRA_HELLO:
1503 zread_hello (client);
1504 break;
Feng Luc99f3482014-10-16 09:52:36 +08001505 case ZEBRA_VRF_UNREGISTER:
1506 zread_vrf_unregister (client, length, vrf_id);
1507 break;
paul718e3742002-12-13 20:15:29 +00001508 default:
1509 zlog_info ("Zebra received unknown command %d", command);
1510 break;
1511 }
1512
ajs719e9742005-02-28 20:52:15 +00001513 if (client->t_suicide)
1514 {
1515 /* No need to wait for thread callback, just kill immediately. */
1516 zebra_client_close(client);
1517 return -1;
1518 }
1519
paul718e3742002-12-13 20:15:29 +00001520 stream_reset (client->ibuf);
1521 zebra_event (ZEBRA_READ, sock, client);
paul718e3742002-12-13 20:15:29 +00001522 return 0;
1523}
1524
paul718e3742002-12-13 20:15:29 +00001525
1526/* Accept code of zebra server socket. */
paulb9df2d22004-05-09 09:09:59 +00001527static int
paul718e3742002-12-13 20:15:29 +00001528zebra_accept (struct thread *thread)
1529{
1530 int accept_sock;
1531 int client_sock;
1532 struct sockaddr_in client;
1533 socklen_t len;
1534
1535 accept_sock = THREAD_FD (thread);
1536
ajs719e9742005-02-28 20:52:15 +00001537 /* Reregister myself. */
1538 zebra_event (ZEBRA_SERV, accept_sock, NULL);
1539
paul718e3742002-12-13 20:15:29 +00001540 len = sizeof (struct sockaddr_in);
1541 client_sock = accept (accept_sock, (struct sockaddr *) &client, &len);
1542
1543 if (client_sock < 0)
1544 {
ajs6099b3b2004-11-20 02:06:59 +00001545 zlog_warn ("Can't accept zebra socket: %s", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001546 return -1;
1547 }
1548
paulccf35572003-03-01 11:42:20 +00001549 /* Make client socket non-blocking. */
ajs719e9742005-02-28 20:52:15 +00001550 set_nonblocking(client_sock);
paul865b8522005-01-05 08:30:35 +00001551
paul718e3742002-12-13 20:15:29 +00001552 /* Create new zebra client. */
1553 zebra_client_create (client_sock);
1554
paul718e3742002-12-13 20:15:29 +00001555 return 0;
1556}
1557
paulb9df2d22004-05-09 09:09:59 +00001558#ifdef HAVE_TCP_ZEBRA
paul718e3742002-12-13 20:15:29 +00001559/* Make zebra's server socket. */
paulb9df2d22004-05-09 09:09:59 +00001560static void
paul718e3742002-12-13 20:15:29 +00001561zebra_serv ()
1562{
1563 int ret;
1564 int accept_sock;
1565 struct sockaddr_in addr;
1566
1567 accept_sock = socket (AF_INET, SOCK_STREAM, 0);
1568
1569 if (accept_sock < 0)
1570 {
paul3d1dc852005-04-05 00:45:23 +00001571 zlog_warn ("Can't create zserv stream socket: %s",
1572 safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001573 zlog_warn ("zebra can't provice full functionality due to above error");
1574 return;
1575 }
1576
Vyacheslav Trushkin2ea1ab12011-12-11 18:48:47 +04001577 memset (&route_type_oaths, 0, sizeof (route_type_oaths));
paul718e3742002-12-13 20:15:29 +00001578 memset (&addr, 0, sizeof (struct sockaddr_in));
1579 addr.sin_family = AF_INET;
1580 addr.sin_port = htons (ZEBRA_PORT);
Paul Jakma6f0e3f62007-05-10 02:38:51 +00001581#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
paul718e3742002-12-13 20:15:29 +00001582 addr.sin_len = sizeof (struct sockaddr_in);
Paul Jakma6f0e3f62007-05-10 02:38:51 +00001583#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
paul718e3742002-12-13 20:15:29 +00001584 addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
1585
1586 sockopt_reuseaddr (accept_sock);
1587 sockopt_reuseport (accept_sock);
1588
pauledd7c242003-06-04 13:59:38 +00001589 if ( zserv_privs.change(ZPRIVS_RAISE) )
1590 zlog (NULL, LOG_ERR, "Can't raise privileges");
1591
paul718e3742002-12-13 20:15:29 +00001592 ret = bind (accept_sock, (struct sockaddr *)&addr,
1593 sizeof (struct sockaddr_in));
1594 if (ret < 0)
1595 {
paul3d1dc852005-04-05 00:45:23 +00001596 zlog_warn ("Can't bind to stream socket: %s",
1597 safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001598 zlog_warn ("zebra can't provice full functionality due to above error");
1599 close (accept_sock); /* Avoid sd leak. */
1600 return;
1601 }
pauledd7c242003-06-04 13:59:38 +00001602
1603 if ( zserv_privs.change(ZPRIVS_LOWER) )
1604 zlog (NULL, LOG_ERR, "Can't lower privileges");
paul718e3742002-12-13 20:15:29 +00001605
1606 ret = listen (accept_sock, 1);
1607 if (ret < 0)
1608 {
paul3d1dc852005-04-05 00:45:23 +00001609 zlog_warn ("Can't listen to stream socket: %s",
1610 safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001611 zlog_warn ("zebra can't provice full functionality due to above error");
1612 close (accept_sock); /* Avoid sd leak. */
1613 return;
1614 }
1615
1616 zebra_event (ZEBRA_SERV, accept_sock, NULL);
1617}
David Lamparter4b6c3322015-04-21 09:47:57 +02001618#else /* HAVE_TCP_ZEBRA */
paul718e3742002-12-13 20:15:29 +00001619
1620/* For sockaddr_un. */
1621#include <sys/un.h>
1622
1623/* zebra server UNIX domain socket. */
paulb9df2d22004-05-09 09:09:59 +00001624static void
hassofce954f2004-10-07 20:29:24 +00001625zebra_serv_un (const char *path)
paul718e3742002-12-13 20:15:29 +00001626{
1627 int ret;
1628 int sock, len;
1629 struct sockaddr_un serv;
1630 mode_t old_mask;
1631
1632 /* First of all, unlink existing socket */
1633 unlink (path);
1634
1635 /* Set umask */
1636 old_mask = umask (0077);
1637
1638 /* Make UNIX domain socket. */
1639 sock = socket (AF_UNIX, SOCK_STREAM, 0);
1640 if (sock < 0)
1641 {
paul3d1dc852005-04-05 00:45:23 +00001642 zlog_warn ("Can't create zserv unix socket: %s",
1643 safe_strerror (errno));
1644 zlog_warn ("zebra can't provide full functionality due to above error");
paul718e3742002-12-13 20:15:29 +00001645 return;
1646 }
1647
Vyacheslav Trushkin2ea1ab12011-12-11 18:48:47 +04001648 memset (&route_type_oaths, 0, sizeof (route_type_oaths));
1649
paul718e3742002-12-13 20:15:29 +00001650 /* Make server socket. */
1651 memset (&serv, 0, sizeof (struct sockaddr_un));
1652 serv.sun_family = AF_UNIX;
1653 strncpy (serv.sun_path, path, strlen (path));
Paul Jakma6f0e3f62007-05-10 02:38:51 +00001654#ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
paul718e3742002-12-13 20:15:29 +00001655 len = serv.sun_len = SUN_LEN(&serv);
1656#else
1657 len = sizeof (serv.sun_family) + strlen (serv.sun_path);
Paul Jakma6f0e3f62007-05-10 02:38:51 +00001658#endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
paul718e3742002-12-13 20:15:29 +00001659
1660 ret = bind (sock, (struct sockaddr *) &serv, len);
1661 if (ret < 0)
1662 {
paul3d1dc852005-04-05 00:45:23 +00001663 zlog_warn ("Can't bind to unix socket %s: %s",
1664 path, safe_strerror (errno));
1665 zlog_warn ("zebra can't provide full functionality due to above error");
paul718e3742002-12-13 20:15:29 +00001666 close (sock);
1667 return;
1668 }
1669
1670 ret = listen (sock, 5);
1671 if (ret < 0)
1672 {
paul3d1dc852005-04-05 00:45:23 +00001673 zlog_warn ("Can't listen to unix socket %s: %s",
1674 path, safe_strerror (errno));
1675 zlog_warn ("zebra can't provide full functionality due to above error");
paul718e3742002-12-13 20:15:29 +00001676 close (sock);
1677 return;
1678 }
1679
1680 umask (old_mask);
1681
1682 zebra_event (ZEBRA_SERV, sock, NULL);
1683}
David Lamparter4b6c3322015-04-21 09:47:57 +02001684#endif /* HAVE_TCP_ZEBRA */
David Lamparter6b0655a2014-06-04 06:53:35 +02001685
paul718e3742002-12-13 20:15:29 +00001686
paulb9df2d22004-05-09 09:09:59 +00001687static void
paul718e3742002-12-13 20:15:29 +00001688zebra_event (enum event event, int sock, struct zserv *client)
1689{
1690 switch (event)
1691 {
1692 case ZEBRA_SERV:
paulb21b19c2003-06-15 01:28:29 +00001693 thread_add_read (zebrad.master, zebra_accept, client, sock);
paul718e3742002-12-13 20:15:29 +00001694 break;
1695 case ZEBRA_READ:
1696 client->t_read =
paulb21b19c2003-06-15 01:28:29 +00001697 thread_add_read (zebrad.master, zebra_client_read, client, sock);
paul718e3742002-12-13 20:15:29 +00001698 break;
1699 case ZEBRA_WRITE:
1700 /**/
1701 break;
1702 }
1703}
David Lamparter6b0655a2014-06-04 06:53:35 +02001704
paul718e3742002-12-13 20:15:29 +00001705/* Display default rtm_table for all clients. */
1706DEFUN (show_table,
1707 show_table_cmd,
1708 "show table",
1709 SHOW_STR
1710 "default routing table to use for all clients\n")
1711{
paulb21b19c2003-06-15 01:28:29 +00001712 vty_out (vty, "table %d%s", zebrad.rtm_table_default,
paul718e3742002-12-13 20:15:29 +00001713 VTY_NEWLINE);
1714 return CMD_SUCCESS;
1715}
1716
1717DEFUN (config_table,
1718 config_table_cmd,
1719 "table TABLENO",
1720 "Configure target kernel routing table\n"
1721 "TABLE integer\n")
1722{
paulb21b19c2003-06-15 01:28:29 +00001723 zebrad.rtm_table_default = strtol (argv[0], (char**)0, 10);
paul718e3742002-12-13 20:15:29 +00001724 return CMD_SUCCESS;
1725}
1726
hasso647e4f12003-05-25 11:43:52 +00001727DEFUN (ip_forwarding,
1728 ip_forwarding_cmd,
1729 "ip forwarding",
1730 IP_STR
1731 "Turn on IP forwarding")
1732{
1733 int ret;
1734
1735 ret = ipforward ();
hassob71f00f2004-10-13 12:20:35 +00001736 if (ret == 0)
1737 ret = ipforward_on ();
hasso647e4f12003-05-25 11:43:52 +00001738
hasso647e4f12003-05-25 11:43:52 +00001739 if (ret == 0)
1740 {
1741 vty_out (vty, "Can't turn on IP forwarding%s", VTY_NEWLINE);
1742 return CMD_WARNING;
1743 }
1744
1745 return CMD_SUCCESS;
1746}
1747
paul718e3742002-12-13 20:15:29 +00001748DEFUN (no_ip_forwarding,
1749 no_ip_forwarding_cmd,
1750 "no ip forwarding",
1751 NO_STR
1752 IP_STR
1753 "Turn off IP forwarding")
1754{
1755 int ret;
1756
1757 ret = ipforward ();
hassob71f00f2004-10-13 12:20:35 +00001758 if (ret != 0)
1759 ret = ipforward_off ();
paul718e3742002-12-13 20:15:29 +00001760
paul718e3742002-12-13 20:15:29 +00001761 if (ret != 0)
1762 {
1763 vty_out (vty, "Can't turn off IP forwarding%s", VTY_NEWLINE);
1764 return CMD_WARNING;
1765 }
1766
1767 return CMD_SUCCESS;
1768}
1769
1770/* This command is for debugging purpose. */
1771DEFUN (show_zebra_client,
1772 show_zebra_client_cmd,
1773 "show zebra client",
1774 SHOW_STR
1775 "Zebra information"
1776 "Client information")
1777{
hasso52dc7ee2004-09-23 19:18:23 +00001778 struct listnode *node;
paul718e3742002-12-13 20:15:29 +00001779 struct zserv *client;
1780
paul1eb8ef22005-04-07 07:30:20 +00001781 for (ALL_LIST_ELEMENTS_RO (zebrad.client_list, node, client))
1782 vty_out (vty, "Client fd %d%s", client->sock, VTY_NEWLINE);
1783
paul718e3742002-12-13 20:15:29 +00001784 return CMD_SUCCESS;
1785}
1786
1787/* Table configuration write function. */
paulb9df2d22004-05-09 09:09:59 +00001788static int
paul718e3742002-12-13 20:15:29 +00001789config_write_table (struct vty *vty)
1790{
paulb21b19c2003-06-15 01:28:29 +00001791 if (zebrad.rtm_table_default)
1792 vty_out (vty, "table %d%s", zebrad.rtm_table_default,
paul718e3742002-12-13 20:15:29 +00001793 VTY_NEWLINE);
1794 return 0;
1795}
1796
1797/* table node for routing tables. */
Stephen Hemminger7fc626d2008-12-01 11:10:34 -08001798static struct cmd_node table_node =
paul718e3742002-12-13 20:15:29 +00001799{
1800 TABLE_NODE,
1801 "", /* This node has no interface. */
1802 1
1803};
David Lamparter6b0655a2014-06-04 06:53:35 +02001804
paul718e3742002-12-13 20:15:29 +00001805/* Only display ip forwarding is enabled or not. */
1806DEFUN (show_ip_forwarding,
1807 show_ip_forwarding_cmd,
1808 "show ip forwarding",
1809 SHOW_STR
1810 IP_STR
1811 "IP forwarding status\n")
1812{
1813 int ret;
1814
1815 ret = ipforward ();
1816
1817 if (ret == 0)
1818 vty_out (vty, "IP forwarding is off%s", VTY_NEWLINE);
1819 else
1820 vty_out (vty, "IP forwarding is on%s", VTY_NEWLINE);
1821 return CMD_SUCCESS;
1822}
1823
1824#ifdef HAVE_IPV6
1825/* Only display ipv6 forwarding is enabled or not. */
1826DEFUN (show_ipv6_forwarding,
1827 show_ipv6_forwarding_cmd,
1828 "show ipv6 forwarding",
1829 SHOW_STR
1830 "IPv6 information\n"
1831 "Forwarding status\n")
1832{
1833 int ret;
1834
1835 ret = ipforward_ipv6 ();
1836
1837 switch (ret)
1838 {
1839 case -1:
1840 vty_out (vty, "ipv6 forwarding is unknown%s", VTY_NEWLINE);
1841 break;
1842 case 0:
1843 vty_out (vty, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE);
1844 break;
1845 case 1:
1846 vty_out (vty, "ipv6 forwarding is %s%s", "on", VTY_NEWLINE);
1847 break;
1848 default:
1849 vty_out (vty, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE);
1850 break;
1851 }
1852 return CMD_SUCCESS;
1853}
1854
hasso55906722004-02-11 22:42:16 +00001855DEFUN (ipv6_forwarding,
1856 ipv6_forwarding_cmd,
1857 "ipv6 forwarding",
1858 IPV6_STR
1859 "Turn on IPv6 forwarding")
1860{
1861 int ret;
1862
hasso41d3fc92004-04-06 11:59:00 +00001863 ret = ipforward_ipv6 ();
hassob71f00f2004-10-13 12:20:35 +00001864 if (ret == 0)
1865 ret = ipforward_ipv6_on ();
hasso41d3fc92004-04-06 11:59:00 +00001866
hasso41d3fc92004-04-06 11:59:00 +00001867 if (ret == 0)
1868 {
hasso55906722004-02-11 22:42:16 +00001869 vty_out (vty, "Can't turn on IPv6 forwarding%s", VTY_NEWLINE);
1870 return CMD_WARNING;
1871 }
1872
1873 return CMD_SUCCESS;
1874}
1875
paul718e3742002-12-13 20:15:29 +00001876DEFUN (no_ipv6_forwarding,
1877 no_ipv6_forwarding_cmd,
1878 "no ipv6 forwarding",
1879 NO_STR
hasso55906722004-02-11 22:42:16 +00001880 IPV6_STR
1881 "Turn off IPv6 forwarding")
paul718e3742002-12-13 20:15:29 +00001882{
1883 int ret;
1884
hasso41d3fc92004-04-06 11:59:00 +00001885 ret = ipforward_ipv6 ();
hassob71f00f2004-10-13 12:20:35 +00001886 if (ret != 0)
1887 ret = ipforward_ipv6_off ();
hasso41d3fc92004-04-06 11:59:00 +00001888
paul718e3742002-12-13 20:15:29 +00001889 if (ret != 0)
1890 {
1891 vty_out (vty, "Can't turn off IPv6 forwarding%s", VTY_NEWLINE);
1892 return CMD_WARNING;
1893 }
1894
1895 return CMD_SUCCESS;
1896}
1897
1898#endif /* HAVE_IPV6 */
1899
1900/* IPForwarding configuration write function. */
ajs719e9742005-02-28 20:52:15 +00001901static int
paul718e3742002-12-13 20:15:29 +00001902config_write_forwarding (struct vty *vty)
1903{
hasso18a6dce2004-10-03 18:18:34 +00001904 /* FIXME: Find better place for that. */
1905 router_id_write (vty);
1906
paul3e0b3a52004-08-23 18:58:32 +00001907 if (ipforward ())
1908 vty_out (vty, "ip forwarding%s", VTY_NEWLINE);
paul718e3742002-12-13 20:15:29 +00001909#ifdef HAVE_IPV6
paul3e0b3a52004-08-23 18:58:32 +00001910 if (ipforward_ipv6 ())
1911 vty_out (vty, "ipv6 forwarding%s", VTY_NEWLINE);
paul718e3742002-12-13 20:15:29 +00001912#endif /* HAVE_IPV6 */
1913 vty_out (vty, "!%s", VTY_NEWLINE);
1914 return 0;
1915}
1916
1917/* table node for routing tables. */
Stephen Hemminger7fc626d2008-12-01 11:10:34 -08001918static struct cmd_node forwarding_node =
paul718e3742002-12-13 20:15:29 +00001919{
1920 FORWARDING_NODE,
1921 "", /* This node has no interface. */
1922 1
1923};
1924
Udaya Shankara KSd869dbd2016-02-11 21:42:29 +05301925#ifdef HAVE_FPM
1926/* function to write the fpm config info */
1927static int
1928config_write_fpm (struct vty *vty)
1929{
1930 return
1931 fpm_remote_srv_write (vty);
1932}
1933
1934/* Zebra node */
1935static struct cmd_node zebra_node =
1936{
1937 ZEBRA_NODE,
1938 "",
1939 1
1940};
1941#endif
1942
David Lamparter6b0655a2014-06-04 06:53:35 +02001943
paul718e3742002-12-13 20:15:29 +00001944/* Initialisation of zebra and installation of commands. */
1945void
paula1ac18c2005-06-28 17:17:12 +00001946zebra_init (void)
paul718e3742002-12-13 20:15:29 +00001947{
1948 /* Client list init. */
paulb21b19c2003-06-15 01:28:29 +00001949 zebrad.client_list = list_new ();
paul718e3742002-12-13 20:15:29 +00001950
paul718e3742002-12-13 20:15:29 +00001951 /* Install configuration write function. */
1952 install_node (&table_node, config_write_table);
1953 install_node (&forwarding_node, config_write_forwarding);
Udaya Shankara KSd869dbd2016-02-11 21:42:29 +05301954#ifdef HAVE_FPM
1955 install_node (&zebra_node, config_write_fpm);
1956#endif
paul718e3742002-12-13 20:15:29 +00001957
1958 install_element (VIEW_NODE, &show_ip_forwarding_cmd);
1959 install_element (ENABLE_NODE, &show_ip_forwarding_cmd);
hasso647e4f12003-05-25 11:43:52 +00001960 install_element (CONFIG_NODE, &ip_forwarding_cmd);
paul718e3742002-12-13 20:15:29 +00001961 install_element (CONFIG_NODE, &no_ip_forwarding_cmd);
1962 install_element (ENABLE_NODE, &show_zebra_client_cmd);
1963
1964#ifdef HAVE_NETLINK
1965 install_element (VIEW_NODE, &show_table_cmd);
1966 install_element (ENABLE_NODE, &show_table_cmd);
1967 install_element (CONFIG_NODE, &config_table_cmd);
1968#endif /* HAVE_NETLINK */
1969
1970#ifdef HAVE_IPV6
1971 install_element (VIEW_NODE, &show_ipv6_forwarding_cmd);
1972 install_element (ENABLE_NODE, &show_ipv6_forwarding_cmd);
hasso55906722004-02-11 22:42:16 +00001973 install_element (CONFIG_NODE, &ipv6_forwarding_cmd);
paul718e3742002-12-13 20:15:29 +00001974 install_element (CONFIG_NODE, &no_ipv6_forwarding_cmd);
1975#endif /* HAVE_IPV6 */
Paul Jakma7514fb72007-05-02 16:05:35 +00001976
1977 /* Route-map */
1978 zebra_route_map_init ();
paul718e3742002-12-13 20:15:29 +00001979}
Denis Ovsienko97be79f2009-07-24 20:45:31 +04001980
1981/* Make zebra server socket, wiping any existing one (see bug #403). */
1982void
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +04001983zebra_zserv_socket_init (char *path)
Denis Ovsienko97be79f2009-07-24 20:45:31 +04001984{
1985#ifdef HAVE_TCP_ZEBRA
1986 zebra_serv ();
1987#else
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +04001988 zebra_serv_un (path ? path : ZEBRA_SERV_PATH);
Denis Ovsienko97be79f2009-07-24 20:45:31 +04001989#endif /* HAVE_TCP_ZEBRA */
1990}