blob: 86f141b3281452d77495ee4e9e16b21d82a48440 [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
Olivier Dugeon15773a82016-04-19 18:29:55 +0200162 zlog_info("Try to set TE Link Param");
163 /* Then, Traffic Engineering parameters if any */
164 if (HAS_LINK_PARAMS(ifp) && IS_LINK_PARAMS_SET(ifp->link_params))
165 {
166 stream_putc (s, 1);
167 zebra_interface_link_params_write (s, ifp);
168 }
169 else
170 stream_putc (s, 0);
171
Josh Bailey51d4ef82012-03-21 17:13:39 -0700172 /* Write packet size. */
173 stream_putw_at (s, 0, stream_get_endp (s));
174}
175
paul718e3742002-12-13 20:15:29 +0000176/* Interface is added. Send ZEBRA_INTERFACE_ADD to client. */
paulb9df2d22004-05-09 09:09:59 +0000177/*
178 * This function is called in the following situations:
179 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
180 * from the client.
181 * - at startup, when zebra figures out the available interfaces
182 * - when an interface is added (where support for
183 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
184 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
185 * received)
186 */
paul718e3742002-12-13 20:15:29 +0000187int
188zsend_interface_add (struct zserv *client, struct interface *ifp)
189{
190 struct stream *s;
191
192 /* Check this client need interface information. */
Feng Luc99f3482014-10-16 09:52:36 +0800193 if (! vrf_bitmap_check (client->ifinfo, ifp->vrf_id))
ajs719e9742005-02-28 20:52:15 +0000194 return 0;
paul718e3742002-12-13 20:15:29 +0000195
196 s = client->obuf;
197 stream_reset (s);
198
Feng Luc99f3482014-10-16 09:52:36 +0800199 zserv_create_header (s, ZEBRA_INTERFACE_ADD, ifp->vrf_id);
Josh Bailey51d4ef82012-03-21 17:13:39 -0700200 zserv_encode_interface (s, ifp);
paul718e3742002-12-13 20:15:29 +0000201
ajs719e9742005-02-28 20:52:15 +0000202 return zebra_server_send_message(client);
paul718e3742002-12-13 20:15:29 +0000203}
204
205/* Interface deletion from zebra daemon. */
206int
207zsend_interface_delete (struct zserv *client, struct interface *ifp)
208{
209 struct stream *s;
210
211 /* Check this client need interface information. */
Feng Luc99f3482014-10-16 09:52:36 +0800212 if (! vrf_bitmap_check (client->ifinfo, ifp->vrf_id))
ajs719e9742005-02-28 20:52:15 +0000213 return 0;
paul718e3742002-12-13 20:15:29 +0000214
215 s = client->obuf;
216 stream_reset (s);
paul718e3742002-12-13 20:15:29 +0000217
Feng Luc99f3482014-10-16 09:52:36 +0800218 zserv_create_header (s, ZEBRA_INTERFACE_DELETE, ifp->vrf_id);
Josh Bailey51d4ef82012-03-21 17:13:39 -0700219 zserv_encode_interface (s, ifp);
paul718e3742002-12-13 20:15:29 +0000220
ajs719e9742005-02-28 20:52:15 +0000221 return zebra_server_send_message (client);
paul718e3742002-12-13 20:15:29 +0000222}
223
Olivier Dugeon15773a82016-04-19 18:29:55 +0200224int
225zsend_interface_link_params (struct zserv *client, struct interface *ifp)
226{
227 struct stream *s;
228
229 /* Check this client need interface information. */
230 if (! client->ifinfo)
231 return 0;
232
233 if (!ifp->link_params)
234 return 0;
235 s = client->obuf;
236 stream_reset (s);
237
238 zserv_create_header (s, ZEBRA_INTERFACE_LINK_PARAMS, ifp->vrf_id);
239
240 /* Add Interface Index */
241 stream_putl (s, ifp->ifindex);
242
243 /* Then TE Link Parameters */
244 if (zebra_interface_link_params_write (s, ifp) == 0)
245 return 0;
246
247 /* Write packet size. */
248 stream_putw_at (s, 0, stream_get_endp (s));
249
250 return zebra_server_send_message (client);
251}
252
paulb9df2d22004-05-09 09:09:59 +0000253/* Interface address is added/deleted. Send ZEBRA_INTERFACE_ADDRESS_ADD or
254 * ZEBRA_INTERFACE_ADDRESS_DELETE to the client.
255 *
256 * A ZEBRA_INTERFACE_ADDRESS_ADD is sent in the following situations:
257 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
258 * from the client, after the ZEBRA_INTERFACE_ADD has been
259 * sent from zebra to the client
260 * - redistribute new address info to all clients in the following situations
261 * - at startup, when zebra figures out the available interfaces
262 * - when an interface is added (where support for
263 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
264 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
265 * received)
266 * - for the vty commands "ip address A.B.C.D/M [<secondary>|<label LINE>]"
267 * and "no bandwidth <1-10000000>", "ipv6 address X:X::X:X/M"
268 * - when an RTM_NEWADDR message is received from the kernel,
269 *
270 * The call tree that triggers ZEBRA_INTERFACE_ADDRESS_DELETE:
271 *
272 * zsend_interface_address(DELETE)
273 * ^
274 * |
275 * zebra_interface_address_delete_update
276 * ^ ^ ^
paul6eb88272005-07-29 14:36:00 +0000277 * | | if_delete_update
278 * | |
paulb9df2d22004-05-09 09:09:59 +0000279 * ip_address_uninstall connected_delete_ipv4
280 * [ipv6_addresss_uninstall] [connected_delete_ipv6]
281 * ^ ^
282 * | |
283 * | RTM_NEWADDR on routing/netlink socket
284 * |
285 * vty commands:
286 * "no ip address A.B.C.D/M [label LINE]"
287 * "no ip address A.B.C.D/M secondary"
288 * ["no ipv6 address X:X::X:X/M"]
289 *
290 */
paul718e3742002-12-13 20:15:29 +0000291int
paulb9df2d22004-05-09 09:09:59 +0000292zsend_interface_address (int cmd, struct zserv *client,
293 struct interface *ifp, struct connected *ifc)
paul718e3742002-12-13 20:15:29 +0000294{
295 int blen;
296 struct stream *s;
297 struct prefix *p;
298
299 /* Check this client need interface information. */
Feng Luc99f3482014-10-16 09:52:36 +0800300 if (! vrf_bitmap_check (client->ifinfo, ifp->vrf_id))
ajs719e9742005-02-28 20:52:15 +0000301 return 0;
paul718e3742002-12-13 20:15:29 +0000302
303 s = client->obuf;
304 stream_reset (s);
paulc1b98002006-01-16 01:54:02 +0000305
Feng Luc99f3482014-10-16 09:52:36 +0800306 zserv_create_header (s, cmd, ifp->vrf_id);
paul718e3742002-12-13 20:15:29 +0000307 stream_putl (s, ifp->ifindex);
308
309 /* Interface address flag. */
310 stream_putc (s, ifc->flags);
311
312 /* Prefix information. */
313 p = ifc->address;
314 stream_putc (s, p->family);
315 blen = prefix_blen (p);
316 stream_put (s, &p->u.prefix, blen);
paulb9df2d22004-05-09 09:09:59 +0000317
318 /*
319 * XXX gnu version does not send prefixlen for ZEBRA_INTERFACE_ADDRESS_DELETE
320 * but zebra_interface_address_delete_read() in the gnu version
321 * expects to find it
322 */
paul718e3742002-12-13 20:15:29 +0000323 stream_putc (s, p->prefixlen);
324
325 /* Destination. */
326 p = ifc->destination;
327 if (p)
328 stream_put (s, &p->u.prefix, blen);
329 else
330 stream_put (s, NULL, blen);
331
332 /* Write packet size. */
333 stream_putw_at (s, 0, stream_get_endp (s));
334
ajs719e9742005-02-28 20:52:15 +0000335 return zebra_server_send_message(client);
paul718e3742002-12-13 20:15:29 +0000336}
337
paulb9df2d22004-05-09 09:09:59 +0000338/*
339 * The cmd passed to zsend_interface_update may be ZEBRA_INTERFACE_UP or
340 * ZEBRA_INTERFACE_DOWN.
341 *
342 * The ZEBRA_INTERFACE_UP message is sent from the zebra server to
343 * the clients in one of 2 situations:
344 * - an if_up is detected e.g., as a result of an RTM_IFINFO message
345 * - a vty command modifying the bandwidth of an interface is received.
346 * The ZEBRA_INTERFACE_DOWN message is sent when an if_down is detected.
347 */
paul718e3742002-12-13 20:15:29 +0000348int
paulb9df2d22004-05-09 09:09:59 +0000349zsend_interface_update (int cmd, struct zserv *client, struct interface *ifp)
paul718e3742002-12-13 20:15:29 +0000350{
351 struct stream *s;
352
353 /* Check this client need interface information. */
Feng Luc99f3482014-10-16 09:52:36 +0800354 if (! vrf_bitmap_check (client->ifinfo, ifp->vrf_id))
ajs719e9742005-02-28 20:52:15 +0000355 return 0;
paul718e3742002-12-13 20:15:29 +0000356
357 s = client->obuf;
358 stream_reset (s);
359
Feng Luc99f3482014-10-16 09:52:36 +0800360 zserv_create_header (s, cmd, ifp->vrf_id);
Josh Bailey51d4ef82012-03-21 17:13:39 -0700361 zserv_encode_interface (s, ifp);
paul718e3742002-12-13 20:15:29 +0000362
ajs719e9742005-02-28 20:52:15 +0000363 return zebra_server_send_message(client);
paul718e3742002-12-13 20:15:29 +0000364}
365
paulb9df2d22004-05-09 09:09:59 +0000366/*
367 * The zebra server sends the clients a ZEBRA_IPV4_ROUTE_ADD or a
368 * ZEBRA_IPV6_ROUTE_ADD via zsend_route_multipath in the following
369 * situations:
370 * - when the client starts up, and requests default information
371 * by sending a ZEBRA_REDISTRIBUTE_DEFAULT_ADD to the zebra server, in the
372 * - case of rip, ripngd, ospfd and ospf6d, when the client sends a
373 * ZEBRA_REDISTRIBUTE_ADD as a result of the "redistribute" vty cmd,
374 * - when the zebra server redistributes routes after it updates its rib
375 *
376 * The zebra server sends clients a ZEBRA_IPV4_ROUTE_DELETE or a
377 * ZEBRA_IPV6_ROUTE_DELETE via zsend_route_multipath when:
378 * - a "ip route" or "ipv6 route" vty command is issued, a prefix is
379 * - deleted from zebra's rib, and this info
380 * has to be redistributed to the clients
381 *
382 * XXX The ZEBRA_IPV*_ROUTE_ADD message is also sent by the client to the
383 * zebra server when the client wants to tell the zebra server to add a
384 * route to the kernel (zapi_ipv4_add etc. ). Since it's essentially the
385 * same message being sent back and forth, this function and
386 * zapi_ipv{4,6}_{add, delete} should be re-written to avoid code
387 * duplication.
388 */
paul718e3742002-12-13 20:15:29 +0000389int
paulb9df2d22004-05-09 09:09:59 +0000390zsend_route_multipath (int cmd, struct zserv *client, struct prefix *p,
391 struct rib *rib)
paul718e3742002-12-13 20:15:29 +0000392{
393 int psize;
394 struct stream *s;
395 struct nexthop *nexthop;
paul1dcb5172005-05-31 08:38:50 +0000396 unsigned long nhnummark = 0, messmark = 0;
paulb9df2d22004-05-09 09:09:59 +0000397 int nhnum = 0;
paul1dcb5172005-05-31 08:38:50 +0000398 u_char zapi_flags = 0;
Feng Luc99f3482014-10-16 09:52:36 +0800399
400 /* Check this client need this route. */
401 if (!vrf_bitmap_check (client->redist[rib->type], rib->vrf_id) &&
402 !(is_default (p) &&
403 vrf_bitmap_check (client->redist_default, rib->vrf_id)))
404 return 0;
405
paul718e3742002-12-13 20:15:29 +0000406 s = client->obuf;
407 stream_reset (s);
paulc1b98002006-01-16 01:54:02 +0000408
Feng Luc99f3482014-10-16 09:52:36 +0800409 zserv_create_header (s, cmd, rib->vrf_id);
paulc1b98002006-01-16 01:54:02 +0000410
411 /* Put type and nexthop. */
paul718e3742002-12-13 20:15:29 +0000412 stream_putc (s, rib->type);
413 stream_putc (s, rib->flags);
paul1dcb5172005-05-31 08:38:50 +0000414
415 /* marker for message flags field */
416 messmark = stream_get_endp (s);
417 stream_putc (s, 0);
paul718e3742002-12-13 20:15:29 +0000418
419 /* Prefix. */
420 psize = PSIZE (p->prefixlen);
421 stream_putc (s, p->prefixlen);
paulb9df2d22004-05-09 09:09:59 +0000422 stream_write (s, (u_char *) & p->u.prefix, psize);
paul718e3742002-12-13 20:15:29 +0000423
paulb9df2d22004-05-09 09:09:59 +0000424 /*
425 * XXX The message format sent by zebra below does not match the format
426 * of the corresponding message expected by the zebra server
427 * itself (e.g., see zread_ipv4_add). The nexthop_num is not set correctly,
428 * (is there a bug on the client side if more than one segment is sent?)
429 * nexthop ZEBRA_NEXTHOP_IPV4 is never set, ZEBRA_NEXTHOP_IFINDEX
430 * is hard-coded.
431 */
paul718e3742002-12-13 20:15:29 +0000432 /* Nexthop */
paul1dcb5172005-05-31 08:38:50 +0000433
paul718e3742002-12-13 20:15:29 +0000434 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
435 {
Timo Teräs325823a2016-01-15 17:36:31 +0200436 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE))
paulb9df2d22004-05-09 09:09:59 +0000437 {
paul1dcb5172005-05-31 08:38:50 +0000438 SET_FLAG (zapi_flags, ZAPI_MESSAGE_NEXTHOP);
439 SET_FLAG (zapi_flags, ZAPI_MESSAGE_IFINDEX);
440
441 if (nhnummark == 0)
442 {
443 nhnummark = stream_get_endp (s);
444 stream_putc (s, 1); /* placeholder */
445 }
446
paulb9df2d22004-05-09 09:09:59 +0000447 nhnum++;
paul718e3742002-12-13 20:15:29 +0000448
paulb9df2d22004-05-09 09:09:59 +0000449 switch(nexthop->type)
450 {
451 case NEXTHOP_TYPE_IPV4:
452 case NEXTHOP_TYPE_IPV4_IFINDEX:
453 stream_put_in_addr (s, &nexthop->gate.ipv4);
454 break;
455#ifdef HAVE_IPV6
456 case NEXTHOP_TYPE_IPV6:
457 case NEXTHOP_TYPE_IPV6_IFINDEX:
458 case NEXTHOP_TYPE_IPV6_IFNAME:
459 stream_write (s, (u_char *) &nexthop->gate.ipv6, 16);
460 break;
461#endif
462 default:
463 if (cmd == ZEBRA_IPV4_ROUTE_ADD
464 || cmd == ZEBRA_IPV4_ROUTE_DELETE)
465 {
466 struct in_addr empty;
paul44983cf2004-09-22 13:15:58 +0000467 memset (&empty, 0, sizeof (struct in_addr));
paulb9df2d22004-05-09 09:09:59 +0000468 stream_write (s, (u_char *) &empty, IPV4_MAX_BYTELEN);
469 }
470 else
471 {
472 struct in6_addr empty;
473 memset (&empty, 0, sizeof (struct in6_addr));
474 stream_write (s, (u_char *) &empty, IPV6_MAX_BYTELEN);
475 }
476 }
paul718e3742002-12-13 20:15:29 +0000477
paulb9df2d22004-05-09 09:09:59 +0000478 /* Interface index. */
479 stream_putc (s, 1);
480 stream_putl (s, nexthop->ifindex);
paul718e3742002-12-13 20:15:29 +0000481
paulb9df2d22004-05-09 09:09:59 +0000482 break;
483 }
paul718e3742002-12-13 20:15:29 +0000484 }
485
486 /* Metric */
Stephen Hemmingercf8a8312010-08-18 15:56:46 -0700487 if (cmd == ZEBRA_IPV4_ROUTE_ADD || cmd == ZEBRA_IPV6_ROUTE_ADD)
paul1dcb5172005-05-31 08:38:50 +0000488 {
vincentfbf5d032005-09-29 11:25:50 +0000489 SET_FLAG (zapi_flags, ZAPI_MESSAGE_DISTANCE);
490 stream_putc (s, rib->distance);
paul1dcb5172005-05-31 08:38:50 +0000491 SET_FLAG (zapi_flags, ZAPI_MESSAGE_METRIC);
492 stream_putl (s, rib->metric);
Timo Teräsb11f3b52015-11-02 16:50:07 +0200493 SET_FLAG (zapi_flags, ZAPI_MESSAGE_MTU);
494 stream_putl (s, rib->mtu);
paul1dcb5172005-05-31 08:38:50 +0000495 }
496
497 /* write real message flags value */
498 stream_putc_at (s, messmark, zapi_flags);
499
paulb9df2d22004-05-09 09:09:59 +0000500 /* Write next-hop number */
501 if (nhnummark)
hassoc1eaa442004-10-19 06:26:01 +0000502 stream_putc_at (s, nhnummark, nhnum);
paulb9df2d22004-05-09 09:09:59 +0000503
paul718e3742002-12-13 20:15:29 +0000504 /* Write packet size. */
505 stream_putw_at (s, 0, stream_get_endp (s));
506
ajs719e9742005-02-28 20:52:15 +0000507 return zebra_server_send_message(client);
paul718e3742002-12-13 20:15:29 +0000508}
509
paul718e3742002-12-13 20:15:29 +0000510#ifdef HAVE_IPV6
ajs719e9742005-02-28 20:52:15 +0000511static int
Feng Luc99f3482014-10-16 09:52:36 +0800512zsend_ipv6_nexthop_lookup (struct zserv *client, struct in6_addr *addr,
513 vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +0000514{
515 struct stream *s;
516 struct rib *rib;
517 unsigned long nump;
518 u_char num;
519 struct nexthop *nexthop;
520
521 /* Lookup nexthop. */
Feng Luc99f3482014-10-16 09:52:36 +0800522 rib = rib_match_ipv6 (addr, vrf_id);
paul718e3742002-12-13 20:15:29 +0000523
524 /* Get output stream. */
525 s = client->obuf;
526 stream_reset (s);
527
528 /* Fill in result. */
Feng Luc99f3482014-10-16 09:52:36 +0800529 zserv_create_header (s, ZEBRA_IPV6_NEXTHOP_LOOKUP, vrf_id);
Hiroshi Yokoi8ccd74c2015-09-08 11:52:20 +0900530 stream_put (s, addr, 16);
paul718e3742002-12-13 20:15:29 +0000531
532 if (rib)
533 {
534 stream_putl (s, rib->metric);
535 num = 0;
paul9985f832005-02-09 15:51:56 +0000536 nump = stream_get_endp(s);
paul718e3742002-12-13 20:15:29 +0000537 stream_putc (s, 0);
Christian Frankefa713d92013-07-05 15:35:37 +0000538 /* Only non-recursive routes are elegible to resolve nexthop we
539 * are looking up. Therefore, we will just iterate over the top
540 * chain of nexthops. */
paul718e3742002-12-13 20:15:29 +0000541 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
Timo Teräs325823a2016-01-15 17:36:31 +0200542 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE))
paul718e3742002-12-13 20:15:29 +0000543 {
544 stream_putc (s, nexthop->type);
545 switch (nexthop->type)
546 {
547 case ZEBRA_NEXTHOP_IPV6:
548 stream_put (s, &nexthop->gate.ipv6, 16);
549 break;
550 case ZEBRA_NEXTHOP_IPV6_IFINDEX:
551 case ZEBRA_NEXTHOP_IPV6_IFNAME:
552 stream_put (s, &nexthop->gate.ipv6, 16);
553 stream_putl (s, nexthop->ifindex);
554 break;
555 case ZEBRA_NEXTHOP_IFINDEX:
556 case ZEBRA_NEXTHOP_IFNAME:
557 stream_putl (s, nexthop->ifindex);
558 break;
hassofa2b17e2004-03-04 17:45:00 +0000559 default:
560 /* do nothing */
561 break;
paul718e3742002-12-13 20:15:29 +0000562 }
563 num++;
564 }
565 stream_putc_at (s, nump, num);
566 }
567 else
568 {
569 stream_putl (s, 0);
570 stream_putc (s, 0);
571 }
572
573 stream_putw_at (s, 0, stream_get_endp (s));
574
ajs719e9742005-02-28 20:52:15 +0000575 return zebra_server_send_message(client);
paul718e3742002-12-13 20:15:29 +0000576}
577#endif /* HAVE_IPV6 */
578
paulb9df2d22004-05-09 09:09:59 +0000579static int
Feng Luc99f3482014-10-16 09:52:36 +0800580zsend_ipv4_nexthop_lookup (struct zserv *client, struct in_addr addr,
581 vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +0000582{
583 struct stream *s;
584 struct rib *rib;
585 unsigned long nump;
586 u_char num;
587 struct nexthop *nexthop;
588
David Lamparterf598cf72014-11-22 14:44:20 -0800589 /* Lookup nexthop - eBGP excluded */
Feng Luc99f3482014-10-16 09:52:36 +0800590 rib = rib_match_ipv4_safi (addr, SAFI_UNICAST, 1, NULL, vrf_id);
paul718e3742002-12-13 20:15:29 +0000591
592 /* Get output stream. */
593 s = client->obuf;
594 stream_reset (s);
595
596 /* Fill in result. */
Feng Luc99f3482014-10-16 09:52:36 +0800597 zserv_create_header (s, ZEBRA_IPV4_NEXTHOP_LOOKUP, vrf_id);
paul718e3742002-12-13 20:15:29 +0000598 stream_put_in_addr (s, &addr);
599
600 if (rib)
601 {
Christian Frankebb97e462013-05-25 14:01:35 +0000602 if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
603 zlog_debug("%s: Matching rib entry found.", __func__);
paul718e3742002-12-13 20:15:29 +0000604 stream_putl (s, rib->metric);
605 num = 0;
paul9985f832005-02-09 15:51:56 +0000606 nump = stream_get_endp(s);
paul718e3742002-12-13 20:15:29 +0000607 stream_putc (s, 0);
Christian Frankefa713d92013-07-05 15:35:37 +0000608 /* Only non-recursive routes are elegible to resolve the nexthop we
609 * are looking up. Therefore, we will just iterate over the top
610 * chain of nexthops. */
paul718e3742002-12-13 20:15:29 +0000611 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
Timo Teräs325823a2016-01-15 17:36:31 +0200612 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE))
paul718e3742002-12-13 20:15:29 +0000613 {
614 stream_putc (s, nexthop->type);
615 switch (nexthop->type)
616 {
617 case ZEBRA_NEXTHOP_IPV4:
618 stream_put_in_addr (s, &nexthop->gate.ipv4);
619 break;
Christian Frankebb97e462013-05-25 14:01:35 +0000620 case ZEBRA_NEXTHOP_IPV4_IFINDEX:
621 stream_put_in_addr (s, &nexthop->gate.ipv4);
622 stream_putl (s, nexthop->ifindex);
623 break;
paul718e3742002-12-13 20:15:29 +0000624 case ZEBRA_NEXTHOP_IFINDEX:
625 case ZEBRA_NEXTHOP_IFNAME:
626 stream_putl (s, nexthop->ifindex);
627 break;
hassofa2b17e2004-03-04 17:45:00 +0000628 default:
629 /* do nothing */
630 break;
paul718e3742002-12-13 20:15:29 +0000631 }
632 num++;
633 }
634 stream_putc_at (s, nump, num);
635 }
636 else
637 {
Christian Frankebb97e462013-05-25 14:01:35 +0000638 if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
639 zlog_debug("%s: No matching rib entry found.", __func__);
paul718e3742002-12-13 20:15:29 +0000640 stream_putl (s, 0);
641 stream_putc (s, 0);
642 }
643
644 stream_putw_at (s, 0, stream_get_endp (s));
645
ajs719e9742005-02-28 20:52:15 +0000646 return zebra_server_send_message(client);
paul718e3742002-12-13 20:15:29 +0000647}
648
Everton Marques4e5275b2014-07-01 15:15:52 -0300649/*
650 Modified version of zsend_ipv4_nexthop_lookup():
651 Query unicast rib if nexthop is not found on mrib.
652 Returns both route metric and protocol distance.
653*/
654static int
David Lamparterbd078122015-01-06 19:53:24 +0100655zsend_ipv4_nexthop_lookup_mrib (struct zserv *client, struct in_addr addr,
656 struct rib *rib)
Everton Marques4e5275b2014-07-01 15:15:52 -0300657{
658 struct stream *s;
Everton Marques4e5275b2014-07-01 15:15:52 -0300659 unsigned long nump;
660 u_char num;
661 struct nexthop *nexthop;
Everton Marques4e5275b2014-07-01 15:15:52 -0300662
663 /* Get output stream. */
664 s = client->obuf;
665 stream_reset (s);
666
667 /* Fill in result. */
Jafar Al-Gharaibeh190591f2016-04-21 17:40:12 -0500668 zserv_create_header (s, ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB,
669 rib ? rib->vrf_id : VRF_DEFAULT);
Everton Marques4e5275b2014-07-01 15:15:52 -0300670 stream_put_in_addr (s, &addr);
671
672 if (rib)
673 {
674 stream_putc (s, rib->distance);
675 stream_putl (s, rib->metric);
676 num = 0;
677 nump = stream_get_endp(s); /* remember position for nexthop_num */
678 stream_putc (s, 0); /* reserve room for nexthop_num */
679 /* Only non-recursive routes are elegible to resolve the nexthop we
680 * are looking up. Therefore, we will just iterate over the top
681 * chain of nexthops. */
682 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
Timo Teräs325823a2016-01-15 17:36:31 +0200683 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE))
Everton Marques4e5275b2014-07-01 15:15:52 -0300684 {
685 stream_putc (s, nexthop->type);
686 switch (nexthop->type)
687 {
688 case ZEBRA_NEXTHOP_IPV4:
689 stream_put_in_addr (s, &nexthop->gate.ipv4);
690 break;
691 case ZEBRA_NEXTHOP_IPV4_IFINDEX:
692 stream_put_in_addr (s, &nexthop->gate.ipv4);
693 stream_putl (s, nexthop->ifindex);
694 break;
695 case ZEBRA_NEXTHOP_IFINDEX:
696 case ZEBRA_NEXTHOP_IFNAME:
697 stream_putl (s, nexthop->ifindex);
698 break;
699 default:
700 /* do nothing */
701 break;
702 }
703 num++;
704 }
705
706 stream_putc_at (s, nump, num); /* store nexthop_num */
707 }
708 else
709 {
710 stream_putc (s, 0); /* distance */
711 stream_putl (s, 0); /* metric */
712 stream_putc (s, 0); /* nexthop_num */
713 }
714
715 stream_putw_at (s, 0, stream_get_endp (s));
716
717 return zebra_server_send_message(client);
718}
719
paulb9df2d22004-05-09 09:09:59 +0000720static int
Feng Luc99f3482014-10-16 09:52:36 +0800721zsend_ipv4_import_lookup (struct zserv *client, struct prefix_ipv4 *p,
722 vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +0000723{
724 struct stream *s;
725 struct rib *rib;
726 unsigned long nump;
727 u_char num;
728 struct nexthop *nexthop;
729
730 /* Lookup nexthop. */
Feng Luc99f3482014-10-16 09:52:36 +0800731 rib = rib_lookup_ipv4 (p, vrf_id);
paul718e3742002-12-13 20:15:29 +0000732
733 /* Get output stream. */
734 s = client->obuf;
735 stream_reset (s);
736
737 /* Fill in result. */
Feng Luc99f3482014-10-16 09:52:36 +0800738 zserv_create_header (s, ZEBRA_IPV4_IMPORT_LOOKUP, vrf_id);
paul718e3742002-12-13 20:15:29 +0000739 stream_put_in_addr (s, &p->prefix);
740
741 if (rib)
742 {
743 stream_putl (s, rib->metric);
744 num = 0;
paul9985f832005-02-09 15:51:56 +0000745 nump = stream_get_endp(s);
paul718e3742002-12-13 20:15:29 +0000746 stream_putc (s, 0);
747 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
Timo Teräs325823a2016-01-15 17:36:31 +0200748 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE))
paul718e3742002-12-13 20:15:29 +0000749 {
750 stream_putc (s, nexthop->type);
751 switch (nexthop->type)
752 {
753 case ZEBRA_NEXTHOP_IPV4:
754 stream_put_in_addr (s, &nexthop->gate.ipv4);
755 break;
Christian Frankea12afd52013-05-25 14:01:36 +0000756 case ZEBRA_NEXTHOP_IPV4_IFINDEX:
757 stream_put_in_addr (s, &nexthop->gate.ipv4);
758 stream_putl (s, nexthop->ifindex);
759 break;
paul718e3742002-12-13 20:15:29 +0000760 case ZEBRA_NEXTHOP_IFINDEX:
761 case ZEBRA_NEXTHOP_IFNAME:
762 stream_putl (s, nexthop->ifindex);
763 break;
hassofa2b17e2004-03-04 17:45:00 +0000764 default:
765 /* do nothing */
766 break;
paul718e3742002-12-13 20:15:29 +0000767 }
768 num++;
769 }
770 stream_putc_at (s, nump, num);
771 }
772 else
773 {
774 stream_putl (s, 0);
775 stream_putc (s, 0);
776 }
777
778 stream_putw_at (s, 0, stream_get_endp (s));
779
ajs719e9742005-02-28 20:52:15 +0000780 return zebra_server_send_message(client);
paul718e3742002-12-13 20:15:29 +0000781}
David Lamparter6b0655a2014-06-04 06:53:35 +0200782
hasso18a6dce2004-10-03 18:18:34 +0000783/* Router-id is updated. Send ZEBRA_ROUTER_ID_ADD to client. */
784int
Feng Luac19a442015-05-22 11:40:07 +0200785zsend_router_id_update (struct zserv *client, struct prefix *p,
786 vrf_id_t vrf_id)
hasso18a6dce2004-10-03 18:18:34 +0000787{
788 struct stream *s;
789 int blen;
790
791 /* Check this client need interface information. */
Feng Luc99f3482014-10-16 09:52:36 +0800792 if (! vrf_bitmap_check (client->ridinfo, vrf_id))
ajs719e9742005-02-28 20:52:15 +0000793 return 0;
hasso18a6dce2004-10-03 18:18:34 +0000794
795 s = client->obuf;
796 stream_reset (s);
797
hasso18a6dce2004-10-03 18:18:34 +0000798 /* Message type. */
Feng Luc99f3482014-10-16 09:52:36 +0800799 zserv_create_header (s, ZEBRA_ROUTER_ID_UPDATE, vrf_id);
hasso18a6dce2004-10-03 18:18:34 +0000800
801 /* Prefix information. */
802 stream_putc (s, p->family);
803 blen = prefix_blen (p);
804 stream_put (s, &p->u.prefix, blen);
805 stream_putc (s, p->prefixlen);
806
807 /* Write packet size. */
808 stream_putw_at (s, 0, stream_get_endp (s));
809
ajs719e9742005-02-28 20:52:15 +0000810 return zebra_server_send_message(client);
hasso18a6dce2004-10-03 18:18:34 +0000811}
David Lamparter6b0655a2014-06-04 06:53:35 +0200812
paul718e3742002-12-13 20:15:29 +0000813/* Register zebra server interface information. Send current all
814 interface and address information. */
ajs719e9742005-02-28 20:52:15 +0000815static int
Feng Luc99f3482014-10-16 09:52:36 +0800816zread_interface_add (struct zserv *client, u_short length, vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +0000817{
paul1eb8ef22005-04-07 07:30:20 +0000818 struct listnode *ifnode, *ifnnode;
819 struct listnode *cnode, *cnnode;
paul718e3742002-12-13 20:15:29 +0000820 struct interface *ifp;
821 struct connected *c;
822
823 /* Interface information is needed. */
Feng Luc99f3482014-10-16 09:52:36 +0800824 vrf_bitmap_set (client->ifinfo, vrf_id);
paul718e3742002-12-13 20:15:29 +0000825
Feng Luc99f3482014-10-16 09:52:36 +0800826 for (ALL_LIST_ELEMENTS (vrf_iflist (vrf_id), ifnode, ifnnode, ifp))
paul718e3742002-12-13 20:15:29 +0000827 {
paul718e3742002-12-13 20:15:29 +0000828 /* Skip pseudo interface. */
829 if (! CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE))
830 continue;
831
ajs719e9742005-02-28 20:52:15 +0000832 if (zsend_interface_add (client, ifp) < 0)
833 return -1;
paul718e3742002-12-13 20:15:29 +0000834
paul1eb8ef22005-04-07 07:30:20 +0000835 for (ALL_LIST_ELEMENTS (ifp->connected, cnode, cnnode, c))
paul718e3742002-12-13 20:15:29 +0000836 {
ajs719e9742005-02-28 20:52:15 +0000837 if (CHECK_FLAG (c->conf, ZEBRA_IFC_REAL) &&
838 (zsend_interface_address (ZEBRA_INTERFACE_ADDRESS_ADD, client,
839 ifp, c) < 0))
840 return -1;
paul718e3742002-12-13 20:15:29 +0000841 }
842 }
ajs719e9742005-02-28 20:52:15 +0000843 return 0;
paul718e3742002-12-13 20:15:29 +0000844}
845
846/* Unregister zebra server interface information. */
ajs719e9742005-02-28 20:52:15 +0000847static int
Feng Luc99f3482014-10-16 09:52:36 +0800848zread_interface_delete (struct zserv *client, u_short length, vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +0000849{
Feng Luc99f3482014-10-16 09:52:36 +0800850 vrf_bitmap_unset (client->ifinfo, vrf_id);
ajs719e9742005-02-28 20:52:15 +0000851 return 0;
paul718e3742002-12-13 20:15:29 +0000852}
853
854/* This function support multiple nexthop. */
paulb9df2d22004-05-09 09:09:59 +0000855/*
856 * Parse the ZEBRA_IPV4_ROUTE_ADD sent from client. Update rib and
857 * add kernel route.
858 */
ajs719e9742005-02-28 20:52:15 +0000859static int
Feng Luc99f3482014-10-16 09:52:36 +0800860zread_ipv4_add (struct zserv *client, u_short length, vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +0000861{
862 int i;
863 struct rib *rib;
864 struct prefix_ipv4 p;
865 u_char message;
866 struct in_addr nexthop;
867 u_char nexthop_num;
868 u_char nexthop_type;
869 struct stream *s;
Paul Jakma9099f9b2016-01-18 10:12:10 +0000870 ifindex_t ifindex;
paul718e3742002-12-13 20:15:29 +0000871 u_char ifname_len;
G.Balajicddf3912011-11-26 21:59:32 +0400872 safi_t safi;
873
paul718e3742002-12-13 20:15:29 +0000874
875 /* Get input stream. */
876 s = client->ibuf;
877
878 /* Allocate new rib. */
paul4d38fdb2005-04-28 17:35:14 +0000879 rib = XCALLOC (MTYPE_RIB, sizeof (struct rib));
880
paul718e3742002-12-13 20:15:29 +0000881 /* Type, flags, message. */
882 rib->type = stream_getc (s);
883 rib->flags = stream_getc (s);
paulb9df2d22004-05-09 09:09:59 +0000884 message = stream_getc (s);
G.Balajicddf3912011-11-26 21:59:32 +0400885 safi = stream_getw (s);
paul718e3742002-12-13 20:15:29 +0000886 rib->uptime = time (NULL);
887
888 /* IPv4 prefix. */
889 memset (&p, 0, sizeof (struct prefix_ipv4));
890 p.family = AF_INET;
891 p.prefixlen = stream_getc (s);
892 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
893
Feng Lu0d0686f2015-05-22 11:40:02 +0200894 /* VRF ID */
Feng Luc99f3482014-10-16 09:52:36 +0800895 rib->vrf_id = vrf_id;
Feng Lu0d0686f2015-05-22 11:40:02 +0200896
paul718e3742002-12-13 20:15:29 +0000897 /* Nexthop parse. */
898 if (CHECK_FLAG (message, ZAPI_MESSAGE_NEXTHOP))
899 {
900 nexthop_num = stream_getc (s);
901
902 for (i = 0; i < nexthop_num; i++)
903 {
904 nexthop_type = stream_getc (s);
905
906 switch (nexthop_type)
907 {
908 case ZEBRA_NEXTHOP_IFINDEX:
909 ifindex = stream_getl (s);
910 nexthop_ifindex_add (rib, ifindex);
911 break;
912 case ZEBRA_NEXTHOP_IFNAME:
913 ifname_len = stream_getc (s);
paul9985f832005-02-09 15:51:56 +0000914 stream_forward_getp (s, ifname_len);
paul718e3742002-12-13 20:15:29 +0000915 break;
916 case ZEBRA_NEXTHOP_IPV4:
917 nexthop.s_addr = stream_get_ipv4 (s);
Paul Jakma7514fb72007-05-02 16:05:35 +0000918 nexthop_ipv4_add (rib, &nexthop, NULL);
paul718e3742002-12-13 20:15:29 +0000919 break;
Joakim Tjernlundc963c202012-07-07 17:06:13 +0200920 case ZEBRA_NEXTHOP_IPV4_IFINDEX:
921 nexthop.s_addr = stream_get_ipv4 (s);
922 ifindex = stream_getl (s);
923 nexthop_ipv4_ifindex_add (rib, &nexthop, NULL, ifindex);
924 break;
paul718e3742002-12-13 20:15:29 +0000925 case ZEBRA_NEXTHOP_IPV6:
paul9985f832005-02-09 15:51:56 +0000926 stream_forward_getp (s, IPV6_MAX_BYTELEN);
paul718e3742002-12-13 20:15:29 +0000927 break;
Subbaiah Venkata6902c692012-03-27 19:21:29 -0700928 case ZEBRA_NEXTHOP_BLACKHOLE:
929 nexthop_blackhole_add (rib);
930 break;
931 }
paul718e3742002-12-13 20:15:29 +0000932 }
933 }
934
935 /* Distance. */
936 if (CHECK_FLAG (message, ZAPI_MESSAGE_DISTANCE))
937 rib->distance = stream_getc (s);
938
939 /* Metric. */
940 if (CHECK_FLAG (message, ZAPI_MESSAGE_METRIC))
941 rib->metric = stream_getl (s);
942
Timo Teräsb11f3b52015-11-02 16:50:07 +0200943 if (CHECK_FLAG (message, ZAPI_MESSAGE_MTU))
944 rib->mtu = stream_getl (s);
945
Paul Jakma171eee32006-07-27 16:11:02 +0000946 /* Table */
947 rib->table=zebrad.rtm_table_default;
G.Balajicddf3912011-11-26 21:59:32 +0400948 rib_add_ipv4_multipath (&p, rib, safi);
ajs719e9742005-02-28 20:52:15 +0000949 return 0;
paul718e3742002-12-13 20:15:29 +0000950}
951
952/* Zebra server IPv4 prefix delete function. */
ajs719e9742005-02-28 20:52:15 +0000953static int
Feng Luc99f3482014-10-16 09:52:36 +0800954zread_ipv4_delete (struct zserv *client, u_short length, vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +0000955{
956 int i;
957 struct stream *s;
958 struct zapi_ipv4 api;
Subbaiah Venkata6902c692012-03-27 19:21:29 -0700959 struct in_addr nexthop, *nexthop_p;
paul718e3742002-12-13 20:15:29 +0000960 unsigned long ifindex;
961 struct prefix_ipv4 p;
962 u_char nexthop_num;
963 u_char nexthop_type;
964 u_char ifname_len;
965
966 s = client->ibuf;
967 ifindex = 0;
968 nexthop.s_addr = 0;
Subbaiah Venkata6902c692012-03-27 19:21:29 -0700969 nexthop_p = NULL;
paul718e3742002-12-13 20:15:29 +0000970
971 /* Type, flags, message. */
972 api.type = stream_getc (s);
973 api.flags = stream_getc (s);
974 api.message = stream_getc (s);
G.Balajicddf3912011-11-26 21:59:32 +0400975 api.safi = stream_getw (s);
paul718e3742002-12-13 20:15:29 +0000976
977 /* IPv4 prefix. */
978 memset (&p, 0, sizeof (struct prefix_ipv4));
979 p.family = AF_INET;
980 p.prefixlen = stream_getc (s);
981 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
982
983 /* Nexthop, ifindex, distance, metric. */
984 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
985 {
986 nexthop_num = stream_getc (s);
987
988 for (i = 0; i < nexthop_num; i++)
989 {
990 nexthop_type = stream_getc (s);
991
992 switch (nexthop_type)
993 {
994 case ZEBRA_NEXTHOP_IFINDEX:
995 ifindex = stream_getl (s);
996 break;
997 case ZEBRA_NEXTHOP_IFNAME:
998 ifname_len = stream_getc (s);
paul9985f832005-02-09 15:51:56 +0000999 stream_forward_getp (s, ifname_len);
paul718e3742002-12-13 20:15:29 +00001000 break;
1001 case ZEBRA_NEXTHOP_IPV4:
1002 nexthop.s_addr = stream_get_ipv4 (s);
Subbaiah Venkata6902c692012-03-27 19:21:29 -07001003 nexthop_p = &nexthop;
paul718e3742002-12-13 20:15:29 +00001004 break;
Joakim Tjernlundc963c202012-07-07 17:06:13 +02001005 case ZEBRA_NEXTHOP_IPV4_IFINDEX:
1006 nexthop.s_addr = stream_get_ipv4 (s);
Christian Franke23f5f7c2013-11-27 17:06:14 +00001007 nexthop_p = &nexthop;
Joakim Tjernlundc963c202012-07-07 17:06:13 +02001008 ifindex = stream_getl (s);
1009 break;
paul718e3742002-12-13 20:15:29 +00001010 case ZEBRA_NEXTHOP_IPV6:
paul9985f832005-02-09 15:51:56 +00001011 stream_forward_getp (s, IPV6_MAX_BYTELEN);
paul718e3742002-12-13 20:15:29 +00001012 break;
1013 }
1014 }
1015 }
1016
1017 /* Distance. */
1018 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
1019 api.distance = stream_getc (s);
1020 else
1021 api.distance = 0;
1022
1023 /* Metric. */
1024 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
1025 api.metric = stream_getl (s);
1026 else
1027 api.metric = 0;
1028
Subbaiah Venkata6902c692012-03-27 19:21:29 -07001029 rib_delete_ipv4 (api.type, api.flags, &p, nexthop_p, ifindex,
Feng Luc99f3482014-10-16 09:52:36 +08001030 vrf_id, api.safi);
ajs719e9742005-02-28 20:52:15 +00001031 return 0;
paul718e3742002-12-13 20:15:29 +00001032}
1033
1034/* Nexthop lookup for IPv4. */
ajs719e9742005-02-28 20:52:15 +00001035static int
Feng Luc99f3482014-10-16 09:52:36 +08001036zread_ipv4_nexthop_lookup (struct zserv *client, u_short length,
1037 vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +00001038{
1039 struct in_addr addr;
Christian Frankebb97e462013-05-25 14:01:35 +00001040 char buf[BUFSIZ];
paul718e3742002-12-13 20:15:29 +00001041
1042 addr.s_addr = stream_get_ipv4 (client->ibuf);
Christian Frankebb97e462013-05-25 14:01:35 +00001043 if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
1044 zlog_debug("%s: looking up %s", __func__,
1045 inet_ntop (AF_INET, &addr, buf, BUFSIZ));
Feng Luc99f3482014-10-16 09:52:36 +08001046 return zsend_ipv4_nexthop_lookup (client, addr, vrf_id);
paul718e3742002-12-13 20:15:29 +00001047}
1048
Everton Marques4e5275b2014-07-01 15:15:52 -03001049/* MRIB Nexthop lookup for IPv4. */
1050static int
Feng Luc99f3482014-10-16 09:52:36 +08001051zread_ipv4_nexthop_lookup_mrib (struct zserv *client, u_short length,
1052 vrf_id_t vrf_id)
Everton Marques4e5275b2014-07-01 15:15:52 -03001053{
1054 struct in_addr addr;
David Lamparterbd078122015-01-06 19:53:24 +01001055 struct rib *rib;
Everton Marques4e5275b2014-07-01 15:15:52 -03001056
1057 addr.s_addr = stream_get_ipv4 (client->ibuf);
Feng Luc99f3482014-10-16 09:52:36 +08001058 rib = rib_match_ipv4_multicast (addr, NULL, vrf_id);
David Lamparterbd078122015-01-06 19:53:24 +01001059 return zsend_ipv4_nexthop_lookup_mrib (client, addr, rib);
Everton Marques4e5275b2014-07-01 15:15:52 -03001060}
1061
paul718e3742002-12-13 20:15:29 +00001062/* Nexthop lookup for IPv4. */
ajs719e9742005-02-28 20:52:15 +00001063static int
Feng Luc99f3482014-10-16 09:52:36 +08001064zread_ipv4_import_lookup (struct zserv *client, u_short length,
1065 vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +00001066{
1067 struct prefix_ipv4 p;
1068
1069 p.family = AF_INET;
1070 p.prefixlen = stream_getc (client->ibuf);
1071 p.prefix.s_addr = stream_get_ipv4 (client->ibuf);
1072
Feng Luc99f3482014-10-16 09:52:36 +08001073 return zsend_ipv4_import_lookup (client, &p, vrf_id);
paul718e3742002-12-13 20:15:29 +00001074}
1075
1076#ifdef HAVE_IPV6
1077/* Zebra server IPv6 prefix add function. */
ajs719e9742005-02-28 20:52:15 +00001078static int
Feng Luc99f3482014-10-16 09:52:36 +08001079zread_ipv6_add (struct zserv *client, u_short length, vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +00001080{
1081 int i;
1082 struct stream *s;
paul718e3742002-12-13 20:15:29 +00001083 struct in6_addr nexthop;
Ayan Banerjee34c5d892015-11-09 20:14:53 -05001084 struct rib *rib;
1085 u_char message;
1086 u_char gateway_num;
1087 u_char nexthop_type;
paul718e3742002-12-13 20:15:29 +00001088 struct prefix_ipv6 p;
Ayan Banerjee34c5d892015-11-09 20:14:53 -05001089 safi_t safi;
1090 static struct in6_addr nexthops[MULTIPATH_NUM];
1091 static unsigned int ifindices[MULTIPATH_NUM];
1092
1093 /* Get input stream. */
paul718e3742002-12-13 20:15:29 +00001094 s = client->ibuf;
Ayan Banerjee34c5d892015-11-09 20:14:53 -05001095
paul718e3742002-12-13 20:15:29 +00001096 memset (&nexthop, 0, sizeof (struct in6_addr));
1097
Ayan Banerjee34c5d892015-11-09 20:14:53 -05001098 /* Allocate new rib. */
1099 rib = XCALLOC (MTYPE_RIB, sizeof (struct rib));
paul718e3742002-12-13 20:15:29 +00001100
Ayan Banerjee34c5d892015-11-09 20:14:53 -05001101 /* Type, flags, message. */
1102 rib->type = stream_getc (s);
1103 rib->flags = stream_getc (s);
1104 message = stream_getc (s);
1105 safi = stream_getw (s);
1106 rib->uptime = time (NULL);
1107
1108 /* IPv6 prefix. */
paul718e3742002-12-13 20:15:29 +00001109 memset (&p, 0, sizeof (struct prefix_ipv6));
1110 p.family = AF_INET6;
1111 p.prefixlen = stream_getc (s);
1112 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
1113
Ayan Banerjee34c5d892015-11-09 20:14:53 -05001114 /* We need to give nh-addr, nh-ifindex with the same next-hop object
1115 * to the rib to ensure that IPv6 multipathing works; need to coalesce
1116 * these. Clients should send the same number of paired set of
1117 * next-hop-addr/next-hop-ifindices. */
1118 if (CHECK_FLAG (message, ZAPI_MESSAGE_NEXTHOP))
paul718e3742002-12-13 20:15:29 +00001119 {
Ayan Banerjee34c5d892015-11-09 20:14:53 -05001120 int nh_count = 0;
1121 int if_count = 0;
1122 int max_nh_if = 0;
1123 unsigned int ifindex;
paul718e3742002-12-13 20:15:29 +00001124
Ayan Banerjee34c5d892015-11-09 20:14:53 -05001125 gateway_num = stream_getc (s);
1126 for (i = 0; i < gateway_num; i++)
paul718e3742002-12-13 20:15:29 +00001127 {
1128 nexthop_type = stream_getc (s);
1129
1130 switch (nexthop_type)
1131 {
1132 case ZEBRA_NEXTHOP_IPV6:
1133 stream_get (&nexthop, s, 16);
Ayan Banerjee34c5d892015-11-09 20:14:53 -05001134 if (nh_count < MULTIPATH_NUM) {
1135 nexthops[nh_count++] = nexthop;
1136 }
paul718e3742002-12-13 20:15:29 +00001137 break;
1138 case ZEBRA_NEXTHOP_IFINDEX:
1139 ifindex = stream_getl (s);
Ayan Banerjee34c5d892015-11-09 20:14:53 -05001140 if (if_count < MULTIPATH_NUM) {
1141 ifindices[if_count++] = ifindex;
1142 }
paul718e3742002-12-13 20:15:29 +00001143 break;
1144 }
1145 }
Ayan Banerjee34c5d892015-11-09 20:14:53 -05001146
1147 max_nh_if = (nh_count > if_count) ? nh_count : if_count;
1148 for (i = 0; i < max_nh_if; i++)
1149 {
1150 if ((i < nh_count) && !IN6_IS_ADDR_UNSPECIFIED (&nexthops[i]))
1151 {
1152 if ((i < if_count) && ifindices[i])
1153 nexthop_ipv6_ifindex_add (rib, &nexthops[i], ifindices[i]);
1154 else
1155 nexthop_ipv6_add (rib, &nexthops[i]);
1156 }
1157 else
1158 {
1159 if ((i < if_count) && ifindices[i])
1160 nexthop_ifindex_add (rib, ifindices[i]);
1161 }
1162 }
paul718e3742002-12-13 20:15:29 +00001163 }
1164
Ayan Banerjee34c5d892015-11-09 20:14:53 -05001165 /* Distance. */
1166 if (CHECK_FLAG (message, ZAPI_MESSAGE_DISTANCE))
1167 rib->distance = stream_getc (s);
paul718e3742002-12-13 20:15:29 +00001168
Ayan Banerjee34c5d892015-11-09 20:14:53 -05001169 /* Metric. */
1170 if (CHECK_FLAG (message, ZAPI_MESSAGE_METRIC))
1171 rib->metric = stream_getl (s);
Timo Teräsb11f3b52015-11-02 16:50:07 +02001172
Ayan Banerjee34c5d892015-11-09 20:14:53 -05001173 if (CHECK_FLAG (message, ZAPI_MESSAGE_MTU))
1174 rib->mtu = stream_getl (s);
1175
1176 /* Table */
1177 rib->table=zebrad.rtm_table_default;
1178 rib_add_ipv6_multipath (&p, rib, safi);
ajs719e9742005-02-28 20:52:15 +00001179 return 0;
paul718e3742002-12-13 20:15:29 +00001180}
1181
1182/* Zebra server IPv6 prefix delete function. */
ajs719e9742005-02-28 20:52:15 +00001183static int
Feng Luc99f3482014-10-16 09:52:36 +08001184zread_ipv6_delete (struct zserv *client, u_short length, vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +00001185{
1186 int i;
1187 struct stream *s;
1188 struct zapi_ipv6 api;
1189 struct in6_addr nexthop;
1190 unsigned long ifindex;
1191 struct prefix_ipv6 p;
1192
1193 s = client->ibuf;
1194 ifindex = 0;
1195 memset (&nexthop, 0, sizeof (struct in6_addr));
1196
1197 /* Type, flags, message. */
1198 api.type = stream_getc (s);
1199 api.flags = stream_getc (s);
1200 api.message = stream_getc (s);
G.Balajif768f362011-11-26 22:10:39 +04001201 api.safi = stream_getw (s);
paul718e3742002-12-13 20:15:29 +00001202
1203 /* IPv4 prefix. */
1204 memset (&p, 0, sizeof (struct prefix_ipv6));
1205 p.family = AF_INET6;
1206 p.prefixlen = stream_getc (s);
1207 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
1208
1209 /* Nexthop, ifindex, distance, metric. */
1210 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
1211 {
1212 u_char nexthop_type;
1213
1214 api.nexthop_num = stream_getc (s);
1215 for (i = 0; i < api.nexthop_num; i++)
1216 {
1217 nexthop_type = stream_getc (s);
1218
1219 switch (nexthop_type)
1220 {
1221 case ZEBRA_NEXTHOP_IPV6:
1222 stream_get (&nexthop, s, 16);
1223 break;
1224 case ZEBRA_NEXTHOP_IFINDEX:
1225 ifindex = stream_getl (s);
1226 break;
1227 }
1228 }
1229 }
1230
1231 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
1232 api.distance = stream_getc (s);
1233 else
1234 api.distance = 0;
1235 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
1236 api.metric = stream_getl (s);
1237 else
1238 api.metric = 0;
1239
1240 if (IN6_IS_ADDR_UNSPECIFIED (&nexthop))
Feng Luc99f3482014-10-16 09:52:36 +08001241 rib_delete_ipv6 (api.type, api.flags, &p, NULL, ifindex, vrf_id,
Feng Lu0d0686f2015-05-22 11:40:02 +02001242 api.safi);
paul718e3742002-12-13 20:15:29 +00001243 else
Feng Luc99f3482014-10-16 09:52:36 +08001244 rib_delete_ipv6 (api.type, api.flags, &p, &nexthop, ifindex, vrf_id,
Feng Lu0d0686f2015-05-22 11:40:02 +02001245 api.safi);
ajs719e9742005-02-28 20:52:15 +00001246 return 0;
paul718e3742002-12-13 20:15:29 +00001247}
1248
ajs719e9742005-02-28 20:52:15 +00001249static int
Feng Luc99f3482014-10-16 09:52:36 +08001250zread_ipv6_nexthop_lookup (struct zserv *client, u_short length,
1251 vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +00001252{
1253 struct in6_addr addr;
1254 char buf[BUFSIZ];
1255
1256 stream_get (&addr, client->ibuf, 16);
Christian Frankea5207082013-04-11 08:24:29 +00001257 if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
1258 zlog_debug("%s: looking up %s", __func__,
1259 inet_ntop (AF_INET6, &addr, buf, BUFSIZ));
paul718e3742002-12-13 20:15:29 +00001260
Feng Luc99f3482014-10-16 09:52:36 +08001261 return zsend_ipv6_nexthop_lookup (client, &addr, vrf_id);
paul718e3742002-12-13 20:15:29 +00001262}
1263#endif /* HAVE_IPV6 */
1264
hasso18a6dce2004-10-03 18:18:34 +00001265/* Register zebra server router-id information. Send current router-id */
ajs719e9742005-02-28 20:52:15 +00001266static int
Feng Luc99f3482014-10-16 09:52:36 +08001267zread_router_id_add (struct zserv *client, u_short length, vrf_id_t vrf_id)
hasso18a6dce2004-10-03 18:18:34 +00001268{
1269 struct prefix p;
1270
1271 /* Router-id information is needed. */
Feng Luc99f3482014-10-16 09:52:36 +08001272 vrf_bitmap_set (client->ridinfo, vrf_id);
hasso18a6dce2004-10-03 18:18:34 +00001273
Feng Luc99f3482014-10-16 09:52:36 +08001274 router_id_get (&p, vrf_id);
hasso18a6dce2004-10-03 18:18:34 +00001275
Feng Luc99f3482014-10-16 09:52:36 +08001276 return zsend_router_id_update (client, &p, vrf_id);
hasso18a6dce2004-10-03 18:18:34 +00001277}
1278
1279/* Unregister zebra server router-id information. */
ajs719e9742005-02-28 20:52:15 +00001280static int
Feng Luc99f3482014-10-16 09:52:36 +08001281zread_router_id_delete (struct zserv *client, u_short length, vrf_id_t vrf_id)
hasso18a6dce2004-10-03 18:18:34 +00001282{
Feng Luc99f3482014-10-16 09:52:36 +08001283 vrf_bitmap_unset (client->ridinfo, vrf_id);
ajs719e9742005-02-28 20:52:15 +00001284 return 0;
hasso18a6dce2004-10-03 18:18:34 +00001285}
1286
Vyacheslav Trushkin2ea1ab12011-12-11 18:48:47 +04001287/* Tie up route-type and client->sock */
1288static void
1289zread_hello (struct zserv *client)
1290{
1291 /* type of protocol (lib/zebra.h) */
1292 u_char proto;
1293 proto = stream_getc (client->ibuf);
1294
1295 /* accept only dynamic routing protocols */
1296 if ((proto < ZEBRA_ROUTE_MAX)
1297 && (proto > ZEBRA_ROUTE_STATIC))
1298 {
1299 zlog_notice ("client %d says hello and bids fair to announce only %s routes",
1300 client->sock, zebra_route_string(proto));
1301
1302 /* if route-type was binded by other client */
1303 if (route_type_oaths[proto])
1304 zlog_warn ("sender of %s routes changed %c->%c",
1305 zebra_route_string(proto), route_type_oaths[proto],
1306 client->sock);
1307
1308 route_type_oaths[proto] = client->sock;
1309 }
1310}
1311
Feng Luc99f3482014-10-16 09:52:36 +08001312/* Unregister all information in a VRF. */
1313static int
1314zread_vrf_unregister (struct zserv *client, u_short length, vrf_id_t vrf_id)
1315{
1316 int i;
1317
1318 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
1319 vrf_bitmap_unset (client->redist[i], vrf_id);
1320 vrf_bitmap_unset (client->redist_default, vrf_id);
1321 vrf_bitmap_unset (client->ifinfo, vrf_id);
1322 vrf_bitmap_unset (client->ridinfo, vrf_id);
1323
1324 return 0;
1325}
1326
Vyacheslav Trushkin2ea1ab12011-12-11 18:48:47 +04001327/* If client sent routes of specific type, zebra removes it
1328 * and returns number of deleted routes.
1329 */
1330static void
1331zebra_score_rib (int client_sock)
1332{
1333 int i;
1334
1335 for (i = ZEBRA_ROUTE_RIP; i < ZEBRA_ROUTE_MAX; i++)
1336 if (client_sock == route_type_oaths[i])
1337 {
1338 zlog_notice ("client %d disconnected. %lu %s routes removed from the rib",
1339 client_sock, rib_score_proto (i), zebra_route_string (i));
1340 route_type_oaths[i] = 0;
1341 break;
1342 }
1343}
1344
paul718e3742002-12-13 20:15:29 +00001345/* Close zebra client. */
paulb9df2d22004-05-09 09:09:59 +00001346static void
paul718e3742002-12-13 20:15:29 +00001347zebra_client_close (struct zserv *client)
1348{
1349 /* Close file descriptor. */
1350 if (client->sock)
1351 {
1352 close (client->sock);
Vyacheslav Trushkin2ea1ab12011-12-11 18:48:47 +04001353 zebra_score_rib (client->sock);
paul718e3742002-12-13 20:15:29 +00001354 client->sock = -1;
1355 }
1356
1357 /* Free stream buffers. */
1358 if (client->ibuf)
1359 stream_free (client->ibuf);
1360 if (client->obuf)
1361 stream_free (client->obuf);
ajs719e9742005-02-28 20:52:15 +00001362 if (client->wb)
1363 buffer_free(client->wb);
paul718e3742002-12-13 20:15:29 +00001364
1365 /* Release threads. */
1366 if (client->t_read)
1367 thread_cancel (client->t_read);
1368 if (client->t_write)
1369 thread_cancel (client->t_write);
ajs719e9742005-02-28 20:52:15 +00001370 if (client->t_suicide)
1371 thread_cancel (client->t_suicide);
paul718e3742002-12-13 20:15:29 +00001372
1373 /* Free client structure. */
paulb21b19c2003-06-15 01:28:29 +00001374 listnode_delete (zebrad.client_list, client);
paul718e3742002-12-13 20:15:29 +00001375 XFREE (0, client);
1376}
1377
1378/* Make new client. */
paulb9df2d22004-05-09 09:09:59 +00001379static void
paul718e3742002-12-13 20:15:29 +00001380zebra_client_create (int sock)
1381{
1382 struct zserv *client;
Feng Luc99f3482014-10-16 09:52:36 +08001383 int i;
paul718e3742002-12-13 20:15:29 +00001384
David Lamparter23757db2016-02-24 06:26:02 +01001385 client = XCALLOC (MTYPE_TMP, sizeof (struct zserv));
paul718e3742002-12-13 20:15:29 +00001386
1387 /* Make client input/output buffer. */
1388 client->sock = sock;
1389 client->ibuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
1390 client->obuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
ajs719e9742005-02-28 20:52:15 +00001391 client->wb = buffer_new(0);
paul718e3742002-12-13 20:15:29 +00001392
1393 /* Set table number. */
paulb21b19c2003-06-15 01:28:29 +00001394 client->rtm_table = zebrad.rtm_table_default;
paul718e3742002-12-13 20:15:29 +00001395
Feng Luc99f3482014-10-16 09:52:36 +08001396 /* Initialize flags */
1397 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
1398 client->redist[i] = vrf_bitmap_init ();
1399 client->redist_default = vrf_bitmap_init ();
1400 client->ifinfo = vrf_bitmap_init ();
1401 client->ridinfo = vrf_bitmap_init ();
1402
paul718e3742002-12-13 20:15:29 +00001403 /* Add this client to linked list. */
paulb21b19c2003-06-15 01:28:29 +00001404 listnode_add (zebrad.client_list, client);
paul718e3742002-12-13 20:15:29 +00001405
1406 /* Make new read thread. */
1407 zebra_event (ZEBRA_READ, sock, client);
1408}
1409
1410/* Handler of zebra service request. */
paulb9df2d22004-05-09 09:09:59 +00001411static int
paul718e3742002-12-13 20:15:29 +00001412zebra_client_read (struct thread *thread)
1413{
1414 int sock;
1415 struct zserv *client;
ajs57a14772005-04-10 15:01:56 +00001416 size_t already;
paulc1b98002006-01-16 01:54:02 +00001417 uint16_t length, command;
1418 uint8_t marker, version;
Feng Luc99f3482014-10-16 09:52:36 +08001419 vrf_id_t vrf_id;
paul718e3742002-12-13 20:15:29 +00001420
1421 /* Get thread data. Reset reading thread because I'm running. */
1422 sock = THREAD_FD (thread);
1423 client = THREAD_ARG (thread);
1424 client->t_read = NULL;
1425
ajs719e9742005-02-28 20:52:15 +00001426 if (client->t_suicide)
paul718e3742002-12-13 20:15:29 +00001427 {
ajs719e9742005-02-28 20:52:15 +00001428 zebra_client_close(client);
paul718e3742002-12-13 20:15:29 +00001429 return -1;
1430 }
ajs719e9742005-02-28 20:52:15 +00001431
1432 /* Read length and command (if we don't have it already). */
ajs57a14772005-04-10 15:01:56 +00001433 if ((already = stream_get_endp(client->ibuf)) < ZEBRA_HEADER_SIZE)
ajs719e9742005-02-28 20:52:15 +00001434 {
ajs57a14772005-04-10 15:01:56 +00001435 ssize_t nbyte;
ajs719e9742005-02-28 20:52:15 +00001436 if (((nbyte = stream_read_try (client->ibuf, sock,
ajs57a14772005-04-10 15:01:56 +00001437 ZEBRA_HEADER_SIZE-already)) == 0) ||
ajs719e9742005-02-28 20:52:15 +00001438 (nbyte == -1))
1439 {
1440 if (IS_ZEBRA_DEBUG_EVENT)
1441 zlog_debug ("connection closed socket [%d]", sock);
1442 zebra_client_close (client);
1443 return -1;
1444 }
ajs57a14772005-04-10 15:01:56 +00001445 if (nbyte != (ssize_t)(ZEBRA_HEADER_SIZE-already))
ajs719e9742005-02-28 20:52:15 +00001446 {
1447 /* Try again later. */
1448 zebra_event (ZEBRA_READ, sock, client);
1449 return 0;
1450 }
ajs57a14772005-04-10 15:01:56 +00001451 already = ZEBRA_HEADER_SIZE;
ajs719e9742005-02-28 20:52:15 +00001452 }
1453
1454 /* Reset to read from the beginning of the incoming packet. */
1455 stream_set_getp(client->ibuf, 0);
1456
paulc1b98002006-01-16 01:54:02 +00001457 /* Fetch header values */
paul718e3742002-12-13 20:15:29 +00001458 length = stream_getw (client->ibuf);
paulc1b98002006-01-16 01:54:02 +00001459 marker = stream_getc (client->ibuf);
1460 version = stream_getc (client->ibuf);
Feng Luc99f3482014-10-16 09:52:36 +08001461 vrf_id = stream_getw (client->ibuf);
paulc1b98002006-01-16 01:54:02 +00001462 command = stream_getw (client->ibuf);
paul718e3742002-12-13 20:15:29 +00001463
paulc1b98002006-01-16 01:54:02 +00001464 if (marker != ZEBRA_HEADER_MARKER || version != ZSERV_VERSION)
1465 {
1466 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
1467 __func__, sock, marker, version);
1468 zebra_client_close (client);
1469 return -1;
1470 }
ajs719e9742005-02-28 20:52:15 +00001471 if (length < ZEBRA_HEADER_SIZE)
paul718e3742002-12-13 20:15:29 +00001472 {
ajs57a14772005-04-10 15:01:56 +00001473 zlog_warn("%s: socket %d message length %u is less than header size %d",
1474 __func__, sock, length, ZEBRA_HEADER_SIZE);
1475 zebra_client_close (client);
1476 return -1;
1477 }
1478 if (length > STREAM_SIZE(client->ibuf))
1479 {
1480 zlog_warn("%s: socket %d message length %u exceeds buffer size %lu",
1481 __func__, sock, length, (u_long)STREAM_SIZE(client->ibuf));
paul718e3742002-12-13 20:15:29 +00001482 zebra_client_close (client);
1483 return -1;
1484 }
1485
paul718e3742002-12-13 20:15:29 +00001486 /* Read rest of data. */
ajs57a14772005-04-10 15:01:56 +00001487 if (already < length)
paul718e3742002-12-13 20:15:29 +00001488 {
ajs57a14772005-04-10 15:01:56 +00001489 ssize_t nbyte;
1490 if (((nbyte = stream_read_try (client->ibuf, sock,
1491 length-already)) == 0) ||
1492 (nbyte == -1))
paul718e3742002-12-13 20:15:29 +00001493 {
1494 if (IS_ZEBRA_DEBUG_EVENT)
ajsb6178002004-12-07 21:12:56 +00001495 zlog_debug ("connection closed [%d] when reading zebra data", sock);
paul718e3742002-12-13 20:15:29 +00001496 zebra_client_close (client);
1497 return -1;
1498 }
ajs57a14772005-04-10 15:01:56 +00001499 if (nbyte != (ssize_t)(length-already))
ajs719e9742005-02-28 20:52:15 +00001500 {
1501 /* Try again later. */
1502 zebra_event (ZEBRA_READ, sock, client);
1503 return 0;
1504 }
paul718e3742002-12-13 20:15:29 +00001505 }
1506
ajs719e9742005-02-28 20:52:15 +00001507 length -= ZEBRA_HEADER_SIZE;
1508
paul718e3742002-12-13 20:15:29 +00001509 /* Debug packet information. */
1510 if (IS_ZEBRA_DEBUG_EVENT)
ajsb6178002004-12-07 21:12:56 +00001511 zlog_debug ("zebra message comes from socket [%d]", sock);
paul718e3742002-12-13 20:15:29 +00001512
1513 if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
Feng Luc99f3482014-10-16 09:52:36 +08001514 zlog_debug ("zebra message received [%s] %d in VRF %u",
1515 zserv_command_string (command), length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001516
1517 switch (command)
1518 {
hasso18a6dce2004-10-03 18:18:34 +00001519 case ZEBRA_ROUTER_ID_ADD:
Feng Luc99f3482014-10-16 09:52:36 +08001520 zread_router_id_add (client, length, vrf_id);
hasso18a6dce2004-10-03 18:18:34 +00001521 break;
1522 case ZEBRA_ROUTER_ID_DELETE:
Feng Luc99f3482014-10-16 09:52:36 +08001523 zread_router_id_delete (client, length, vrf_id);
hasso18a6dce2004-10-03 18:18:34 +00001524 break;
paul718e3742002-12-13 20:15:29 +00001525 case ZEBRA_INTERFACE_ADD:
Feng Luc99f3482014-10-16 09:52:36 +08001526 zread_interface_add (client, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001527 break;
1528 case ZEBRA_INTERFACE_DELETE:
Feng Luc99f3482014-10-16 09:52:36 +08001529 zread_interface_delete (client, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001530 break;
1531 case ZEBRA_IPV4_ROUTE_ADD:
Feng Luc99f3482014-10-16 09:52:36 +08001532 zread_ipv4_add (client, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001533 break;
1534 case ZEBRA_IPV4_ROUTE_DELETE:
Feng Luc99f3482014-10-16 09:52:36 +08001535 zread_ipv4_delete (client, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001536 break;
1537#ifdef HAVE_IPV6
1538 case ZEBRA_IPV6_ROUTE_ADD:
Feng Luc99f3482014-10-16 09:52:36 +08001539 zread_ipv6_add (client, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001540 break;
1541 case ZEBRA_IPV6_ROUTE_DELETE:
Feng Luc99f3482014-10-16 09:52:36 +08001542 zread_ipv6_delete (client, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001543 break;
1544#endif /* HAVE_IPV6 */
1545 case ZEBRA_REDISTRIBUTE_ADD:
Feng Luc99f3482014-10-16 09:52:36 +08001546 zebra_redistribute_add (command, client, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001547 break;
1548 case ZEBRA_REDISTRIBUTE_DELETE:
Feng Luc99f3482014-10-16 09:52:36 +08001549 zebra_redistribute_delete (command, client, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001550 break;
1551 case ZEBRA_REDISTRIBUTE_DEFAULT_ADD:
Feng Luc99f3482014-10-16 09:52:36 +08001552 zebra_redistribute_default_add (command, client, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001553 break;
1554 case ZEBRA_REDISTRIBUTE_DEFAULT_DELETE:
Feng Luc99f3482014-10-16 09:52:36 +08001555 zebra_redistribute_default_delete (command, client, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001556 break;
1557 case ZEBRA_IPV4_NEXTHOP_LOOKUP:
Feng Luc99f3482014-10-16 09:52:36 +08001558 zread_ipv4_nexthop_lookup (client, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001559 break;
Everton Marques4e5275b2014-07-01 15:15:52 -03001560 case ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB:
Feng Luc99f3482014-10-16 09:52:36 +08001561 zread_ipv4_nexthop_lookup_mrib (client, length, vrf_id);
Everton Marques4e5275b2014-07-01 15:15:52 -03001562 break;
paul718e3742002-12-13 20:15:29 +00001563#ifdef HAVE_IPV6
1564 case ZEBRA_IPV6_NEXTHOP_LOOKUP:
Feng Luc99f3482014-10-16 09:52:36 +08001565 zread_ipv6_nexthop_lookup (client, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001566 break;
1567#endif /* HAVE_IPV6 */
1568 case ZEBRA_IPV4_IMPORT_LOOKUP:
Feng Luc99f3482014-10-16 09:52:36 +08001569 zread_ipv4_import_lookup (client, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001570 break;
Vyacheslav Trushkin2ea1ab12011-12-11 18:48:47 +04001571 case ZEBRA_HELLO:
1572 zread_hello (client);
1573 break;
Feng Luc99f3482014-10-16 09:52:36 +08001574 case ZEBRA_VRF_UNREGISTER:
1575 zread_vrf_unregister (client, length, vrf_id);
1576 break;
paul718e3742002-12-13 20:15:29 +00001577 default:
1578 zlog_info ("Zebra received unknown command %d", command);
1579 break;
1580 }
1581
ajs719e9742005-02-28 20:52:15 +00001582 if (client->t_suicide)
1583 {
1584 /* No need to wait for thread callback, just kill immediately. */
1585 zebra_client_close(client);
1586 return -1;
1587 }
1588
paul718e3742002-12-13 20:15:29 +00001589 stream_reset (client->ibuf);
1590 zebra_event (ZEBRA_READ, sock, client);
paul718e3742002-12-13 20:15:29 +00001591 return 0;
1592}
1593
paul718e3742002-12-13 20:15:29 +00001594
1595/* Accept code of zebra server socket. */
paulb9df2d22004-05-09 09:09:59 +00001596static int
paul718e3742002-12-13 20:15:29 +00001597zebra_accept (struct thread *thread)
1598{
1599 int accept_sock;
1600 int client_sock;
1601 struct sockaddr_in client;
1602 socklen_t len;
1603
1604 accept_sock = THREAD_FD (thread);
1605
ajs719e9742005-02-28 20:52:15 +00001606 /* Reregister myself. */
1607 zebra_event (ZEBRA_SERV, accept_sock, NULL);
1608
paul718e3742002-12-13 20:15:29 +00001609 len = sizeof (struct sockaddr_in);
1610 client_sock = accept (accept_sock, (struct sockaddr *) &client, &len);
1611
1612 if (client_sock < 0)
1613 {
ajs6099b3b2004-11-20 02:06:59 +00001614 zlog_warn ("Can't accept zebra socket: %s", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001615 return -1;
1616 }
1617
paulccf35572003-03-01 11:42:20 +00001618 /* Make client socket non-blocking. */
ajs719e9742005-02-28 20:52:15 +00001619 set_nonblocking(client_sock);
paul865b8522005-01-05 08:30:35 +00001620
paul718e3742002-12-13 20:15:29 +00001621 /* Create new zebra client. */
1622 zebra_client_create (client_sock);
1623
paul718e3742002-12-13 20:15:29 +00001624 return 0;
1625}
1626
paulb9df2d22004-05-09 09:09:59 +00001627#ifdef HAVE_TCP_ZEBRA
paul718e3742002-12-13 20:15:29 +00001628/* Make zebra's server socket. */
paulb9df2d22004-05-09 09:09:59 +00001629static void
paul718e3742002-12-13 20:15:29 +00001630zebra_serv ()
1631{
1632 int ret;
1633 int accept_sock;
1634 struct sockaddr_in addr;
1635
1636 accept_sock = socket (AF_INET, SOCK_STREAM, 0);
1637
1638 if (accept_sock < 0)
1639 {
paul3d1dc852005-04-05 00:45:23 +00001640 zlog_warn ("Can't create zserv stream socket: %s",
1641 safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001642 zlog_warn ("zebra can't provice full functionality due to above error");
1643 return;
1644 }
1645
Vyacheslav Trushkin2ea1ab12011-12-11 18:48:47 +04001646 memset (&route_type_oaths, 0, sizeof (route_type_oaths));
paul718e3742002-12-13 20:15:29 +00001647 memset (&addr, 0, sizeof (struct sockaddr_in));
1648 addr.sin_family = AF_INET;
1649 addr.sin_port = htons (ZEBRA_PORT);
Paul Jakma6f0e3f62007-05-10 02:38:51 +00001650#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
paul718e3742002-12-13 20:15:29 +00001651 addr.sin_len = sizeof (struct sockaddr_in);
Paul Jakma6f0e3f62007-05-10 02:38:51 +00001652#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
paul718e3742002-12-13 20:15:29 +00001653 addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
1654
1655 sockopt_reuseaddr (accept_sock);
1656 sockopt_reuseport (accept_sock);
1657
pauledd7c242003-06-04 13:59:38 +00001658 if ( zserv_privs.change(ZPRIVS_RAISE) )
1659 zlog (NULL, LOG_ERR, "Can't raise privileges");
1660
paul718e3742002-12-13 20:15:29 +00001661 ret = bind (accept_sock, (struct sockaddr *)&addr,
1662 sizeof (struct sockaddr_in));
1663 if (ret < 0)
1664 {
paul3d1dc852005-04-05 00:45:23 +00001665 zlog_warn ("Can't bind to stream socket: %s",
1666 safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001667 zlog_warn ("zebra can't provice full functionality due to above error");
1668 close (accept_sock); /* Avoid sd leak. */
1669 return;
1670 }
pauledd7c242003-06-04 13:59:38 +00001671
1672 if ( zserv_privs.change(ZPRIVS_LOWER) )
1673 zlog (NULL, LOG_ERR, "Can't lower privileges");
paul718e3742002-12-13 20:15:29 +00001674
1675 ret = listen (accept_sock, 1);
1676 if (ret < 0)
1677 {
paul3d1dc852005-04-05 00:45:23 +00001678 zlog_warn ("Can't listen to stream socket: %s",
1679 safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001680 zlog_warn ("zebra can't provice full functionality due to above error");
1681 close (accept_sock); /* Avoid sd leak. */
1682 return;
1683 }
1684
1685 zebra_event (ZEBRA_SERV, accept_sock, NULL);
1686}
David Lamparter4b6c3322015-04-21 09:47:57 +02001687#else /* HAVE_TCP_ZEBRA */
paul718e3742002-12-13 20:15:29 +00001688
1689/* For sockaddr_un. */
1690#include <sys/un.h>
1691
1692/* zebra server UNIX domain socket. */
paulb9df2d22004-05-09 09:09:59 +00001693static void
hassofce954f2004-10-07 20:29:24 +00001694zebra_serv_un (const char *path)
paul718e3742002-12-13 20:15:29 +00001695{
1696 int ret;
1697 int sock, len;
1698 struct sockaddr_un serv;
1699 mode_t old_mask;
1700
1701 /* First of all, unlink existing socket */
1702 unlink (path);
1703
1704 /* Set umask */
1705 old_mask = umask (0077);
1706
1707 /* Make UNIX domain socket. */
1708 sock = socket (AF_UNIX, SOCK_STREAM, 0);
1709 if (sock < 0)
1710 {
paul3d1dc852005-04-05 00:45:23 +00001711 zlog_warn ("Can't create zserv unix socket: %s",
1712 safe_strerror (errno));
1713 zlog_warn ("zebra can't provide full functionality due to above error");
paul718e3742002-12-13 20:15:29 +00001714 return;
1715 }
1716
Vyacheslav Trushkin2ea1ab12011-12-11 18:48:47 +04001717 memset (&route_type_oaths, 0, sizeof (route_type_oaths));
1718
paul718e3742002-12-13 20:15:29 +00001719 /* Make server socket. */
1720 memset (&serv, 0, sizeof (struct sockaddr_un));
1721 serv.sun_family = AF_UNIX;
1722 strncpy (serv.sun_path, path, strlen (path));
Paul Jakma6f0e3f62007-05-10 02:38:51 +00001723#ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
paul718e3742002-12-13 20:15:29 +00001724 len = serv.sun_len = SUN_LEN(&serv);
1725#else
1726 len = sizeof (serv.sun_family) + strlen (serv.sun_path);
Paul Jakma6f0e3f62007-05-10 02:38:51 +00001727#endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
paul718e3742002-12-13 20:15:29 +00001728
1729 ret = bind (sock, (struct sockaddr *) &serv, len);
1730 if (ret < 0)
1731 {
paul3d1dc852005-04-05 00:45:23 +00001732 zlog_warn ("Can't bind to unix socket %s: %s",
1733 path, safe_strerror (errno));
1734 zlog_warn ("zebra can't provide full functionality due to above error");
paul718e3742002-12-13 20:15:29 +00001735 close (sock);
1736 return;
1737 }
1738
1739 ret = listen (sock, 5);
1740 if (ret < 0)
1741 {
paul3d1dc852005-04-05 00:45:23 +00001742 zlog_warn ("Can't listen to unix socket %s: %s",
1743 path, safe_strerror (errno));
1744 zlog_warn ("zebra can't provide full functionality due to above error");
paul718e3742002-12-13 20:15:29 +00001745 close (sock);
1746 return;
1747 }
1748
1749 umask (old_mask);
1750
1751 zebra_event (ZEBRA_SERV, sock, NULL);
1752}
David Lamparter4b6c3322015-04-21 09:47:57 +02001753#endif /* HAVE_TCP_ZEBRA */
David Lamparter6b0655a2014-06-04 06:53:35 +02001754
paul718e3742002-12-13 20:15:29 +00001755
paulb9df2d22004-05-09 09:09:59 +00001756static void
paul718e3742002-12-13 20:15:29 +00001757zebra_event (enum event event, int sock, struct zserv *client)
1758{
1759 switch (event)
1760 {
1761 case ZEBRA_SERV:
paulb21b19c2003-06-15 01:28:29 +00001762 thread_add_read (zebrad.master, zebra_accept, client, sock);
paul718e3742002-12-13 20:15:29 +00001763 break;
1764 case ZEBRA_READ:
1765 client->t_read =
paulb21b19c2003-06-15 01:28:29 +00001766 thread_add_read (zebrad.master, zebra_client_read, client, sock);
paul718e3742002-12-13 20:15:29 +00001767 break;
1768 case ZEBRA_WRITE:
1769 /**/
1770 break;
1771 }
1772}
David Lamparter6b0655a2014-06-04 06:53:35 +02001773
paul718e3742002-12-13 20:15:29 +00001774/* Display default rtm_table for all clients. */
1775DEFUN (show_table,
1776 show_table_cmd,
1777 "show table",
1778 SHOW_STR
1779 "default routing table to use for all clients\n")
1780{
paulb21b19c2003-06-15 01:28:29 +00001781 vty_out (vty, "table %d%s", zebrad.rtm_table_default,
paul718e3742002-12-13 20:15:29 +00001782 VTY_NEWLINE);
1783 return CMD_SUCCESS;
1784}
1785
1786DEFUN (config_table,
1787 config_table_cmd,
1788 "table TABLENO",
1789 "Configure target kernel routing table\n"
1790 "TABLE integer\n")
1791{
paulb21b19c2003-06-15 01:28:29 +00001792 zebrad.rtm_table_default = strtol (argv[0], (char**)0, 10);
paul718e3742002-12-13 20:15:29 +00001793 return CMD_SUCCESS;
1794}
1795
hasso647e4f12003-05-25 11:43:52 +00001796DEFUN (ip_forwarding,
1797 ip_forwarding_cmd,
1798 "ip forwarding",
1799 IP_STR
1800 "Turn on IP forwarding")
1801{
1802 int ret;
1803
1804 ret = ipforward ();
hassob71f00f2004-10-13 12:20:35 +00001805 if (ret == 0)
1806 ret = ipforward_on ();
hasso647e4f12003-05-25 11:43:52 +00001807
hasso647e4f12003-05-25 11:43:52 +00001808 if (ret == 0)
1809 {
1810 vty_out (vty, "Can't turn on IP forwarding%s", VTY_NEWLINE);
1811 return CMD_WARNING;
1812 }
1813
1814 return CMD_SUCCESS;
1815}
1816
paul718e3742002-12-13 20:15:29 +00001817DEFUN (no_ip_forwarding,
1818 no_ip_forwarding_cmd,
1819 "no ip forwarding",
1820 NO_STR
1821 IP_STR
1822 "Turn off IP forwarding")
1823{
1824 int ret;
1825
1826 ret = ipforward ();
hassob71f00f2004-10-13 12:20:35 +00001827 if (ret != 0)
1828 ret = ipforward_off ();
paul718e3742002-12-13 20:15:29 +00001829
paul718e3742002-12-13 20:15:29 +00001830 if (ret != 0)
1831 {
1832 vty_out (vty, "Can't turn off IP forwarding%s", VTY_NEWLINE);
1833 return CMD_WARNING;
1834 }
1835
1836 return CMD_SUCCESS;
1837}
1838
1839/* This command is for debugging purpose. */
1840DEFUN (show_zebra_client,
1841 show_zebra_client_cmd,
1842 "show zebra client",
1843 SHOW_STR
1844 "Zebra information"
1845 "Client information")
1846{
hasso52dc7ee2004-09-23 19:18:23 +00001847 struct listnode *node;
paul718e3742002-12-13 20:15:29 +00001848 struct zserv *client;
1849
paul1eb8ef22005-04-07 07:30:20 +00001850 for (ALL_LIST_ELEMENTS_RO (zebrad.client_list, node, client))
1851 vty_out (vty, "Client fd %d%s", client->sock, VTY_NEWLINE);
1852
paul718e3742002-12-13 20:15:29 +00001853 return CMD_SUCCESS;
1854}
1855
1856/* Table configuration write function. */
paulb9df2d22004-05-09 09:09:59 +00001857static int
paul718e3742002-12-13 20:15:29 +00001858config_write_table (struct vty *vty)
1859{
paulb21b19c2003-06-15 01:28:29 +00001860 if (zebrad.rtm_table_default)
1861 vty_out (vty, "table %d%s", zebrad.rtm_table_default,
paul718e3742002-12-13 20:15:29 +00001862 VTY_NEWLINE);
1863 return 0;
1864}
1865
1866/* table node for routing tables. */
Stephen Hemminger7fc626d2008-12-01 11:10:34 -08001867static struct cmd_node table_node =
paul718e3742002-12-13 20:15:29 +00001868{
1869 TABLE_NODE,
1870 "", /* This node has no interface. */
1871 1
1872};
David Lamparter6b0655a2014-06-04 06:53:35 +02001873
paul718e3742002-12-13 20:15:29 +00001874/* Only display ip forwarding is enabled or not. */
1875DEFUN (show_ip_forwarding,
1876 show_ip_forwarding_cmd,
1877 "show ip forwarding",
1878 SHOW_STR
1879 IP_STR
1880 "IP forwarding status\n")
1881{
1882 int ret;
1883
1884 ret = ipforward ();
1885
1886 if (ret == 0)
1887 vty_out (vty, "IP forwarding is off%s", VTY_NEWLINE);
1888 else
1889 vty_out (vty, "IP forwarding is on%s", VTY_NEWLINE);
1890 return CMD_SUCCESS;
1891}
1892
1893#ifdef HAVE_IPV6
1894/* Only display ipv6 forwarding is enabled or not. */
1895DEFUN (show_ipv6_forwarding,
1896 show_ipv6_forwarding_cmd,
1897 "show ipv6 forwarding",
1898 SHOW_STR
1899 "IPv6 information\n"
1900 "Forwarding status\n")
1901{
1902 int ret;
1903
1904 ret = ipforward_ipv6 ();
1905
1906 switch (ret)
1907 {
1908 case -1:
1909 vty_out (vty, "ipv6 forwarding is unknown%s", VTY_NEWLINE);
1910 break;
1911 case 0:
1912 vty_out (vty, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE);
1913 break;
1914 case 1:
1915 vty_out (vty, "ipv6 forwarding is %s%s", "on", VTY_NEWLINE);
1916 break;
1917 default:
1918 vty_out (vty, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE);
1919 break;
1920 }
1921 return CMD_SUCCESS;
1922}
1923
hasso55906722004-02-11 22:42:16 +00001924DEFUN (ipv6_forwarding,
1925 ipv6_forwarding_cmd,
1926 "ipv6 forwarding",
1927 IPV6_STR
1928 "Turn on IPv6 forwarding")
1929{
1930 int ret;
1931
hasso41d3fc92004-04-06 11:59:00 +00001932 ret = ipforward_ipv6 ();
hassob71f00f2004-10-13 12:20:35 +00001933 if (ret == 0)
1934 ret = ipforward_ipv6_on ();
hasso41d3fc92004-04-06 11:59:00 +00001935
hasso41d3fc92004-04-06 11:59:00 +00001936 if (ret == 0)
1937 {
hasso55906722004-02-11 22:42:16 +00001938 vty_out (vty, "Can't turn on IPv6 forwarding%s", VTY_NEWLINE);
1939 return CMD_WARNING;
1940 }
1941
1942 return CMD_SUCCESS;
1943}
1944
paul718e3742002-12-13 20:15:29 +00001945DEFUN (no_ipv6_forwarding,
1946 no_ipv6_forwarding_cmd,
1947 "no ipv6 forwarding",
1948 NO_STR
hasso55906722004-02-11 22:42:16 +00001949 IPV6_STR
1950 "Turn off IPv6 forwarding")
paul718e3742002-12-13 20:15:29 +00001951{
1952 int ret;
1953
hasso41d3fc92004-04-06 11:59:00 +00001954 ret = ipforward_ipv6 ();
hassob71f00f2004-10-13 12:20:35 +00001955 if (ret != 0)
1956 ret = ipforward_ipv6_off ();
hasso41d3fc92004-04-06 11:59:00 +00001957
paul718e3742002-12-13 20:15:29 +00001958 if (ret != 0)
1959 {
1960 vty_out (vty, "Can't turn off IPv6 forwarding%s", VTY_NEWLINE);
1961 return CMD_WARNING;
1962 }
1963
1964 return CMD_SUCCESS;
1965}
1966
1967#endif /* HAVE_IPV6 */
1968
1969/* IPForwarding configuration write function. */
ajs719e9742005-02-28 20:52:15 +00001970static int
paul718e3742002-12-13 20:15:29 +00001971config_write_forwarding (struct vty *vty)
1972{
hasso18a6dce2004-10-03 18:18:34 +00001973 /* FIXME: Find better place for that. */
1974 router_id_write (vty);
1975
paul3e0b3a52004-08-23 18:58:32 +00001976 if (ipforward ())
1977 vty_out (vty, "ip forwarding%s", VTY_NEWLINE);
paul718e3742002-12-13 20:15:29 +00001978#ifdef HAVE_IPV6
paul3e0b3a52004-08-23 18:58:32 +00001979 if (ipforward_ipv6 ())
1980 vty_out (vty, "ipv6 forwarding%s", VTY_NEWLINE);
paul718e3742002-12-13 20:15:29 +00001981#endif /* HAVE_IPV6 */
1982 vty_out (vty, "!%s", VTY_NEWLINE);
1983 return 0;
1984}
1985
1986/* table node for routing tables. */
Stephen Hemminger7fc626d2008-12-01 11:10:34 -08001987static struct cmd_node forwarding_node =
paul718e3742002-12-13 20:15:29 +00001988{
1989 FORWARDING_NODE,
1990 "", /* This node has no interface. */
1991 1
1992};
1993
Udaya Shankara KSd869dbd2016-02-11 21:42:29 +05301994#ifdef HAVE_FPM
1995/* function to write the fpm config info */
1996static int
1997config_write_fpm (struct vty *vty)
1998{
1999 return
2000 fpm_remote_srv_write (vty);
2001}
2002
2003/* Zebra node */
2004static struct cmd_node zebra_node =
2005{
2006 ZEBRA_NODE,
2007 "",
2008 1
2009};
2010#endif
2011
David Lamparter6b0655a2014-06-04 06:53:35 +02002012
paul718e3742002-12-13 20:15:29 +00002013/* Initialisation of zebra and installation of commands. */
2014void
paula1ac18c2005-06-28 17:17:12 +00002015zebra_init (void)
paul718e3742002-12-13 20:15:29 +00002016{
2017 /* Client list init. */
paulb21b19c2003-06-15 01:28:29 +00002018 zebrad.client_list = list_new ();
paul718e3742002-12-13 20:15:29 +00002019
paul718e3742002-12-13 20:15:29 +00002020 /* Install configuration write function. */
2021 install_node (&table_node, config_write_table);
2022 install_node (&forwarding_node, config_write_forwarding);
Udaya Shankara KSd869dbd2016-02-11 21:42:29 +05302023#ifdef HAVE_FPM
2024 install_node (&zebra_node, config_write_fpm);
2025#endif
paul718e3742002-12-13 20:15:29 +00002026
2027 install_element (VIEW_NODE, &show_ip_forwarding_cmd);
hasso647e4f12003-05-25 11:43:52 +00002028 install_element (CONFIG_NODE, &ip_forwarding_cmd);
paul718e3742002-12-13 20:15:29 +00002029 install_element (CONFIG_NODE, &no_ip_forwarding_cmd);
2030 install_element (ENABLE_NODE, &show_zebra_client_cmd);
2031
2032#ifdef HAVE_NETLINK
2033 install_element (VIEW_NODE, &show_table_cmd);
paul718e3742002-12-13 20:15:29 +00002034 install_element (CONFIG_NODE, &config_table_cmd);
2035#endif /* HAVE_NETLINK */
2036
2037#ifdef HAVE_IPV6
2038 install_element (VIEW_NODE, &show_ipv6_forwarding_cmd);
hasso55906722004-02-11 22:42:16 +00002039 install_element (CONFIG_NODE, &ipv6_forwarding_cmd);
paul718e3742002-12-13 20:15:29 +00002040 install_element (CONFIG_NODE, &no_ipv6_forwarding_cmd);
2041#endif /* HAVE_IPV6 */
Paul Jakma7514fb72007-05-02 16:05:35 +00002042
2043 /* Route-map */
2044 zebra_route_map_init ();
paul718e3742002-12-13 20:15:29 +00002045}
Denis Ovsienko97be79f2009-07-24 20:45:31 +04002046
2047/* Make zebra server socket, wiping any existing one (see bug #403). */
2048void
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +04002049zebra_zserv_socket_init (char *path)
Denis Ovsienko97be79f2009-07-24 20:45:31 +04002050{
2051#ifdef HAVE_TCP_ZEBRA
2052 zebra_serv ();
2053#else
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +04002054 zebra_serv_un (path ? path : ZEBRA_SERV_PATH);
Denis Ovsienko97be79f2009-07-24 20:45:31 +04002055#endif /* HAVE_TCP_ZEBRA */
2056}