blob: d4c5db906e69e1db194cd37dca322c9fbcb57644 [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;
1083 struct zapi_ipv6 api;
1084 struct in6_addr nexthop;
1085 unsigned long ifindex;
1086 struct prefix_ipv6 p;
1087
1088 s = client->ibuf;
1089 ifindex = 0;
1090 memset (&nexthop, 0, sizeof (struct in6_addr));
1091
1092 /* Type, flags, message. */
1093 api.type = stream_getc (s);
1094 api.flags = stream_getc (s);
1095 api.message = stream_getc (s);
G.Balajif768f362011-11-26 22:10:39 +04001096 api.safi = stream_getw (s);
paul718e3742002-12-13 20:15:29 +00001097
1098 /* IPv4 prefix. */
1099 memset (&p, 0, sizeof (struct prefix_ipv6));
1100 p.family = AF_INET6;
1101 p.prefixlen = stream_getc (s);
1102 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
1103
1104 /* Nexthop, ifindex, distance, metric. */
1105 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
1106 {
1107 u_char nexthop_type;
1108
1109 api.nexthop_num = stream_getc (s);
1110 for (i = 0; i < api.nexthop_num; i++)
1111 {
1112 nexthop_type = stream_getc (s);
1113
1114 switch (nexthop_type)
1115 {
1116 case ZEBRA_NEXTHOP_IPV6:
1117 stream_get (&nexthop, s, 16);
1118 break;
1119 case ZEBRA_NEXTHOP_IFINDEX:
1120 ifindex = stream_getl (s);
1121 break;
1122 }
1123 }
1124 }
1125
1126 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
1127 api.distance = stream_getc (s);
1128 else
1129 api.distance = 0;
1130
1131 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
1132 api.metric = stream_getl (s);
1133 else
1134 api.metric = 0;
Timo Teräsb11f3b52015-11-02 16:50:07 +02001135
1136 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_MTU))
1137 api.mtu = stream_getl (s);
1138 else
1139 api.mtu = 0;
paul718e3742002-12-13 20:15:29 +00001140
1141 if (IN6_IS_ADDR_UNSPECIFIED (&nexthop))
Feng Lu0d0686f2015-05-22 11:40:02 +02001142 rib_add_ipv6 (api.type, api.flags, &p, NULL, ifindex,
Feng Luc99f3482014-10-16 09:52:36 +08001143 vrf_id, zebrad.rtm_table_default, api.metric,
Timo Teräsb11f3b52015-11-02 16:50:07 +02001144 api.mtu, api.distance, api.safi);
paul718e3742002-12-13 20:15:29 +00001145 else
Feng Lu0d0686f2015-05-22 11:40:02 +02001146 rib_add_ipv6 (api.type, api.flags, &p, &nexthop, ifindex,
Feng Luc99f3482014-10-16 09:52:36 +08001147 vrf_id, zebrad.rtm_table_default, api.metric,
Timo Teräsb11f3b52015-11-02 16:50:07 +02001148 api.mtu, api.distance, api.safi);
ajs719e9742005-02-28 20:52:15 +00001149 return 0;
paul718e3742002-12-13 20:15:29 +00001150}
1151
1152/* Zebra server IPv6 prefix delete function. */
ajs719e9742005-02-28 20:52:15 +00001153static int
Feng Luc99f3482014-10-16 09:52:36 +08001154zread_ipv6_delete (struct zserv *client, u_short length, vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +00001155{
1156 int i;
1157 struct stream *s;
1158 struct zapi_ipv6 api;
1159 struct in6_addr nexthop;
1160 unsigned long ifindex;
1161 struct prefix_ipv6 p;
1162
1163 s = client->ibuf;
1164 ifindex = 0;
1165 memset (&nexthop, 0, sizeof (struct in6_addr));
1166
1167 /* Type, flags, message. */
1168 api.type = stream_getc (s);
1169 api.flags = stream_getc (s);
1170 api.message = stream_getc (s);
G.Balajif768f362011-11-26 22:10:39 +04001171 api.safi = stream_getw (s);
paul718e3742002-12-13 20:15:29 +00001172
1173 /* IPv4 prefix. */
1174 memset (&p, 0, sizeof (struct prefix_ipv6));
1175 p.family = AF_INET6;
1176 p.prefixlen = stream_getc (s);
1177 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
1178
1179 /* Nexthop, ifindex, distance, metric. */
1180 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
1181 {
1182 u_char nexthop_type;
1183
1184 api.nexthop_num = stream_getc (s);
1185 for (i = 0; i < api.nexthop_num; i++)
1186 {
1187 nexthop_type = stream_getc (s);
1188
1189 switch (nexthop_type)
1190 {
1191 case ZEBRA_NEXTHOP_IPV6:
1192 stream_get (&nexthop, s, 16);
1193 break;
1194 case ZEBRA_NEXTHOP_IFINDEX:
1195 ifindex = stream_getl (s);
1196 break;
1197 }
1198 }
1199 }
1200
1201 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
1202 api.distance = stream_getc (s);
1203 else
1204 api.distance = 0;
1205 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
1206 api.metric = stream_getl (s);
1207 else
1208 api.metric = 0;
1209
1210 if (IN6_IS_ADDR_UNSPECIFIED (&nexthop))
Feng Luc99f3482014-10-16 09:52:36 +08001211 rib_delete_ipv6 (api.type, api.flags, &p, NULL, ifindex, vrf_id,
Feng Lu0d0686f2015-05-22 11:40:02 +02001212 api.safi);
paul718e3742002-12-13 20:15:29 +00001213 else
Feng Luc99f3482014-10-16 09:52:36 +08001214 rib_delete_ipv6 (api.type, api.flags, &p, &nexthop, ifindex, vrf_id,
Feng Lu0d0686f2015-05-22 11:40:02 +02001215 api.safi);
ajs719e9742005-02-28 20:52:15 +00001216 return 0;
paul718e3742002-12-13 20:15:29 +00001217}
1218
ajs719e9742005-02-28 20:52:15 +00001219static int
Feng Luc99f3482014-10-16 09:52:36 +08001220zread_ipv6_nexthop_lookup (struct zserv *client, u_short length,
1221 vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +00001222{
1223 struct in6_addr addr;
1224 char buf[BUFSIZ];
1225
1226 stream_get (&addr, client->ibuf, 16);
Christian Frankea5207082013-04-11 08:24:29 +00001227 if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
1228 zlog_debug("%s: looking up %s", __func__,
1229 inet_ntop (AF_INET6, &addr, buf, BUFSIZ));
paul718e3742002-12-13 20:15:29 +00001230
Feng Luc99f3482014-10-16 09:52:36 +08001231 return zsend_ipv6_nexthop_lookup (client, &addr, vrf_id);
paul718e3742002-12-13 20:15:29 +00001232}
1233#endif /* HAVE_IPV6 */
1234
hasso18a6dce2004-10-03 18:18:34 +00001235/* Register zebra server router-id information. Send current router-id */
ajs719e9742005-02-28 20:52:15 +00001236static int
Feng Luc99f3482014-10-16 09:52:36 +08001237zread_router_id_add (struct zserv *client, u_short length, vrf_id_t vrf_id)
hasso18a6dce2004-10-03 18:18:34 +00001238{
1239 struct prefix p;
1240
1241 /* Router-id information is needed. */
Feng Luc99f3482014-10-16 09:52:36 +08001242 vrf_bitmap_set (client->ridinfo, vrf_id);
hasso18a6dce2004-10-03 18:18:34 +00001243
Feng Luc99f3482014-10-16 09:52:36 +08001244 router_id_get (&p, vrf_id);
hasso18a6dce2004-10-03 18:18:34 +00001245
Feng Luc99f3482014-10-16 09:52:36 +08001246 return zsend_router_id_update (client, &p, vrf_id);
hasso18a6dce2004-10-03 18:18:34 +00001247}
1248
1249/* Unregister zebra server router-id information. */
ajs719e9742005-02-28 20:52:15 +00001250static int
Feng Luc99f3482014-10-16 09:52:36 +08001251zread_router_id_delete (struct zserv *client, u_short length, vrf_id_t vrf_id)
hasso18a6dce2004-10-03 18:18:34 +00001252{
Feng Luc99f3482014-10-16 09:52:36 +08001253 vrf_bitmap_unset (client->ridinfo, vrf_id);
ajs719e9742005-02-28 20:52:15 +00001254 return 0;
hasso18a6dce2004-10-03 18:18:34 +00001255}
1256
Vyacheslav Trushkin2ea1ab12011-12-11 18:48:47 +04001257/* Tie up route-type and client->sock */
1258static void
1259zread_hello (struct zserv *client)
1260{
1261 /* type of protocol (lib/zebra.h) */
1262 u_char proto;
1263 proto = stream_getc (client->ibuf);
1264
1265 /* accept only dynamic routing protocols */
1266 if ((proto < ZEBRA_ROUTE_MAX)
1267 && (proto > ZEBRA_ROUTE_STATIC))
1268 {
1269 zlog_notice ("client %d says hello and bids fair to announce only %s routes",
1270 client->sock, zebra_route_string(proto));
1271
1272 /* if route-type was binded by other client */
1273 if (route_type_oaths[proto])
1274 zlog_warn ("sender of %s routes changed %c->%c",
1275 zebra_route_string(proto), route_type_oaths[proto],
1276 client->sock);
1277
1278 route_type_oaths[proto] = client->sock;
1279 }
1280}
1281
Feng Luc99f3482014-10-16 09:52:36 +08001282/* Unregister all information in a VRF. */
1283static int
1284zread_vrf_unregister (struct zserv *client, u_short length, vrf_id_t vrf_id)
1285{
1286 int i;
1287
1288 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
1289 vrf_bitmap_unset (client->redist[i], vrf_id);
1290 vrf_bitmap_unset (client->redist_default, vrf_id);
1291 vrf_bitmap_unset (client->ifinfo, vrf_id);
1292 vrf_bitmap_unset (client->ridinfo, vrf_id);
1293
1294 return 0;
1295}
1296
Vyacheslav Trushkin2ea1ab12011-12-11 18:48:47 +04001297/* If client sent routes of specific type, zebra removes it
1298 * and returns number of deleted routes.
1299 */
1300static void
1301zebra_score_rib (int client_sock)
1302{
1303 int i;
1304
1305 for (i = ZEBRA_ROUTE_RIP; i < ZEBRA_ROUTE_MAX; i++)
1306 if (client_sock == route_type_oaths[i])
1307 {
1308 zlog_notice ("client %d disconnected. %lu %s routes removed from the rib",
1309 client_sock, rib_score_proto (i), zebra_route_string (i));
1310 route_type_oaths[i] = 0;
1311 break;
1312 }
1313}
1314
paul718e3742002-12-13 20:15:29 +00001315/* Close zebra client. */
paulb9df2d22004-05-09 09:09:59 +00001316static void
paul718e3742002-12-13 20:15:29 +00001317zebra_client_close (struct zserv *client)
1318{
1319 /* Close file descriptor. */
1320 if (client->sock)
1321 {
1322 close (client->sock);
Vyacheslav Trushkin2ea1ab12011-12-11 18:48:47 +04001323 zebra_score_rib (client->sock);
paul718e3742002-12-13 20:15:29 +00001324 client->sock = -1;
1325 }
1326
1327 /* Free stream buffers. */
1328 if (client->ibuf)
1329 stream_free (client->ibuf);
1330 if (client->obuf)
1331 stream_free (client->obuf);
ajs719e9742005-02-28 20:52:15 +00001332 if (client->wb)
1333 buffer_free(client->wb);
paul718e3742002-12-13 20:15:29 +00001334
1335 /* Release threads. */
1336 if (client->t_read)
1337 thread_cancel (client->t_read);
1338 if (client->t_write)
1339 thread_cancel (client->t_write);
ajs719e9742005-02-28 20:52:15 +00001340 if (client->t_suicide)
1341 thread_cancel (client->t_suicide);
paul718e3742002-12-13 20:15:29 +00001342
1343 /* Free client structure. */
paulb21b19c2003-06-15 01:28:29 +00001344 listnode_delete (zebrad.client_list, client);
paul718e3742002-12-13 20:15:29 +00001345 XFREE (0, client);
1346}
1347
1348/* Make new client. */
paulb9df2d22004-05-09 09:09:59 +00001349static void
paul718e3742002-12-13 20:15:29 +00001350zebra_client_create (int sock)
1351{
1352 struct zserv *client;
Feng Luc99f3482014-10-16 09:52:36 +08001353 int i;
paul718e3742002-12-13 20:15:29 +00001354
David Lamparter23757db2016-02-24 06:26:02 +01001355 client = XCALLOC (MTYPE_TMP, sizeof (struct zserv));
paul718e3742002-12-13 20:15:29 +00001356
1357 /* Make client input/output buffer. */
1358 client->sock = sock;
1359 client->ibuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
1360 client->obuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
ajs719e9742005-02-28 20:52:15 +00001361 client->wb = buffer_new(0);
paul718e3742002-12-13 20:15:29 +00001362
1363 /* Set table number. */
paulb21b19c2003-06-15 01:28:29 +00001364 client->rtm_table = zebrad.rtm_table_default;
paul718e3742002-12-13 20:15:29 +00001365
Feng Luc99f3482014-10-16 09:52:36 +08001366 /* Initialize flags */
1367 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
1368 client->redist[i] = vrf_bitmap_init ();
1369 client->redist_default = vrf_bitmap_init ();
1370 client->ifinfo = vrf_bitmap_init ();
1371 client->ridinfo = vrf_bitmap_init ();
1372
paul718e3742002-12-13 20:15:29 +00001373 /* Add this client to linked list. */
paulb21b19c2003-06-15 01:28:29 +00001374 listnode_add (zebrad.client_list, client);
paul718e3742002-12-13 20:15:29 +00001375
1376 /* Make new read thread. */
1377 zebra_event (ZEBRA_READ, sock, client);
1378}
1379
1380/* Handler of zebra service request. */
paulb9df2d22004-05-09 09:09:59 +00001381static int
paul718e3742002-12-13 20:15:29 +00001382zebra_client_read (struct thread *thread)
1383{
1384 int sock;
1385 struct zserv *client;
ajs57a14772005-04-10 15:01:56 +00001386 size_t already;
paulc1b98002006-01-16 01:54:02 +00001387 uint16_t length, command;
1388 uint8_t marker, version;
Feng Luc99f3482014-10-16 09:52:36 +08001389 vrf_id_t vrf_id;
paul718e3742002-12-13 20:15:29 +00001390
1391 /* Get thread data. Reset reading thread because I'm running. */
1392 sock = THREAD_FD (thread);
1393 client = THREAD_ARG (thread);
1394 client->t_read = NULL;
1395
ajs719e9742005-02-28 20:52:15 +00001396 if (client->t_suicide)
paul718e3742002-12-13 20:15:29 +00001397 {
ajs719e9742005-02-28 20:52:15 +00001398 zebra_client_close(client);
paul718e3742002-12-13 20:15:29 +00001399 return -1;
1400 }
ajs719e9742005-02-28 20:52:15 +00001401
1402 /* Read length and command (if we don't have it already). */
ajs57a14772005-04-10 15:01:56 +00001403 if ((already = stream_get_endp(client->ibuf)) < ZEBRA_HEADER_SIZE)
ajs719e9742005-02-28 20:52:15 +00001404 {
ajs57a14772005-04-10 15:01:56 +00001405 ssize_t nbyte;
ajs719e9742005-02-28 20:52:15 +00001406 if (((nbyte = stream_read_try (client->ibuf, sock,
ajs57a14772005-04-10 15:01:56 +00001407 ZEBRA_HEADER_SIZE-already)) == 0) ||
ajs719e9742005-02-28 20:52:15 +00001408 (nbyte == -1))
1409 {
1410 if (IS_ZEBRA_DEBUG_EVENT)
1411 zlog_debug ("connection closed socket [%d]", sock);
1412 zebra_client_close (client);
1413 return -1;
1414 }
ajs57a14772005-04-10 15:01:56 +00001415 if (nbyte != (ssize_t)(ZEBRA_HEADER_SIZE-already))
ajs719e9742005-02-28 20:52:15 +00001416 {
1417 /* Try again later. */
1418 zebra_event (ZEBRA_READ, sock, client);
1419 return 0;
1420 }
ajs57a14772005-04-10 15:01:56 +00001421 already = ZEBRA_HEADER_SIZE;
ajs719e9742005-02-28 20:52:15 +00001422 }
1423
1424 /* Reset to read from the beginning of the incoming packet. */
1425 stream_set_getp(client->ibuf, 0);
1426
paulc1b98002006-01-16 01:54:02 +00001427 /* Fetch header values */
paul718e3742002-12-13 20:15:29 +00001428 length = stream_getw (client->ibuf);
paulc1b98002006-01-16 01:54:02 +00001429 marker = stream_getc (client->ibuf);
1430 version = stream_getc (client->ibuf);
Feng Luc99f3482014-10-16 09:52:36 +08001431 vrf_id = stream_getw (client->ibuf);
paulc1b98002006-01-16 01:54:02 +00001432 command = stream_getw (client->ibuf);
paul718e3742002-12-13 20:15:29 +00001433
paulc1b98002006-01-16 01:54:02 +00001434 if (marker != ZEBRA_HEADER_MARKER || version != ZSERV_VERSION)
1435 {
1436 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
1437 __func__, sock, marker, version);
1438 zebra_client_close (client);
1439 return -1;
1440 }
ajs719e9742005-02-28 20:52:15 +00001441 if (length < ZEBRA_HEADER_SIZE)
paul718e3742002-12-13 20:15:29 +00001442 {
ajs57a14772005-04-10 15:01:56 +00001443 zlog_warn("%s: socket %d message length %u is less than header size %d",
1444 __func__, sock, length, ZEBRA_HEADER_SIZE);
1445 zebra_client_close (client);
1446 return -1;
1447 }
1448 if (length > STREAM_SIZE(client->ibuf))
1449 {
1450 zlog_warn("%s: socket %d message length %u exceeds buffer size %lu",
1451 __func__, sock, length, (u_long)STREAM_SIZE(client->ibuf));
paul718e3742002-12-13 20:15:29 +00001452 zebra_client_close (client);
1453 return -1;
1454 }
1455
paul718e3742002-12-13 20:15:29 +00001456 /* Read rest of data. */
ajs57a14772005-04-10 15:01:56 +00001457 if (already < length)
paul718e3742002-12-13 20:15:29 +00001458 {
ajs57a14772005-04-10 15:01:56 +00001459 ssize_t nbyte;
1460 if (((nbyte = stream_read_try (client->ibuf, sock,
1461 length-already)) == 0) ||
1462 (nbyte == -1))
paul718e3742002-12-13 20:15:29 +00001463 {
1464 if (IS_ZEBRA_DEBUG_EVENT)
ajsb6178002004-12-07 21:12:56 +00001465 zlog_debug ("connection closed [%d] when reading zebra data", sock);
paul718e3742002-12-13 20:15:29 +00001466 zebra_client_close (client);
1467 return -1;
1468 }
ajs57a14772005-04-10 15:01:56 +00001469 if (nbyte != (ssize_t)(length-already))
ajs719e9742005-02-28 20:52:15 +00001470 {
1471 /* Try again later. */
1472 zebra_event (ZEBRA_READ, sock, client);
1473 return 0;
1474 }
paul718e3742002-12-13 20:15:29 +00001475 }
1476
ajs719e9742005-02-28 20:52:15 +00001477 length -= ZEBRA_HEADER_SIZE;
1478
paul718e3742002-12-13 20:15:29 +00001479 /* Debug packet information. */
1480 if (IS_ZEBRA_DEBUG_EVENT)
ajsb6178002004-12-07 21:12:56 +00001481 zlog_debug ("zebra message comes from socket [%d]", sock);
paul718e3742002-12-13 20:15:29 +00001482
1483 if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
Feng Luc99f3482014-10-16 09:52:36 +08001484 zlog_debug ("zebra message received [%s] %d in VRF %u",
1485 zserv_command_string (command), length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001486
1487 switch (command)
1488 {
hasso18a6dce2004-10-03 18:18:34 +00001489 case ZEBRA_ROUTER_ID_ADD:
Feng Luc99f3482014-10-16 09:52:36 +08001490 zread_router_id_add (client, length, vrf_id);
hasso18a6dce2004-10-03 18:18:34 +00001491 break;
1492 case ZEBRA_ROUTER_ID_DELETE:
Feng Luc99f3482014-10-16 09:52:36 +08001493 zread_router_id_delete (client, length, vrf_id);
hasso18a6dce2004-10-03 18:18:34 +00001494 break;
paul718e3742002-12-13 20:15:29 +00001495 case ZEBRA_INTERFACE_ADD:
Feng Luc99f3482014-10-16 09:52:36 +08001496 zread_interface_add (client, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001497 break;
1498 case ZEBRA_INTERFACE_DELETE:
Feng Luc99f3482014-10-16 09:52:36 +08001499 zread_interface_delete (client, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001500 break;
1501 case ZEBRA_IPV4_ROUTE_ADD:
Feng Luc99f3482014-10-16 09:52:36 +08001502 zread_ipv4_add (client, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001503 break;
1504 case ZEBRA_IPV4_ROUTE_DELETE:
Feng Luc99f3482014-10-16 09:52:36 +08001505 zread_ipv4_delete (client, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001506 break;
1507#ifdef HAVE_IPV6
1508 case ZEBRA_IPV6_ROUTE_ADD:
Feng Luc99f3482014-10-16 09:52:36 +08001509 zread_ipv6_add (client, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001510 break;
1511 case ZEBRA_IPV6_ROUTE_DELETE:
Feng Luc99f3482014-10-16 09:52:36 +08001512 zread_ipv6_delete (client, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001513 break;
1514#endif /* HAVE_IPV6 */
1515 case ZEBRA_REDISTRIBUTE_ADD:
Feng Luc99f3482014-10-16 09:52:36 +08001516 zebra_redistribute_add (command, client, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001517 break;
1518 case ZEBRA_REDISTRIBUTE_DELETE:
Feng Luc99f3482014-10-16 09:52:36 +08001519 zebra_redistribute_delete (command, client, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001520 break;
1521 case ZEBRA_REDISTRIBUTE_DEFAULT_ADD:
Feng Luc99f3482014-10-16 09:52:36 +08001522 zebra_redistribute_default_add (command, client, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001523 break;
1524 case ZEBRA_REDISTRIBUTE_DEFAULT_DELETE:
Feng Luc99f3482014-10-16 09:52:36 +08001525 zebra_redistribute_default_delete (command, client, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001526 break;
1527 case ZEBRA_IPV4_NEXTHOP_LOOKUP:
Feng Luc99f3482014-10-16 09:52:36 +08001528 zread_ipv4_nexthop_lookup (client, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001529 break;
Everton Marques4e5275b2014-07-01 15:15:52 -03001530 case ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB:
Feng Luc99f3482014-10-16 09:52:36 +08001531 zread_ipv4_nexthop_lookup_mrib (client, length, vrf_id);
Everton Marques4e5275b2014-07-01 15:15:52 -03001532 break;
paul718e3742002-12-13 20:15:29 +00001533#ifdef HAVE_IPV6
1534 case ZEBRA_IPV6_NEXTHOP_LOOKUP:
Feng Luc99f3482014-10-16 09:52:36 +08001535 zread_ipv6_nexthop_lookup (client, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001536 break;
1537#endif /* HAVE_IPV6 */
1538 case ZEBRA_IPV4_IMPORT_LOOKUP:
Feng Luc99f3482014-10-16 09:52:36 +08001539 zread_ipv4_import_lookup (client, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001540 break;
Vyacheslav Trushkin2ea1ab12011-12-11 18:48:47 +04001541 case ZEBRA_HELLO:
1542 zread_hello (client);
1543 break;
Feng Luc99f3482014-10-16 09:52:36 +08001544 case ZEBRA_VRF_UNREGISTER:
1545 zread_vrf_unregister (client, length, vrf_id);
1546 break;
paul718e3742002-12-13 20:15:29 +00001547 default:
1548 zlog_info ("Zebra received unknown command %d", command);
1549 break;
1550 }
1551
ajs719e9742005-02-28 20:52:15 +00001552 if (client->t_suicide)
1553 {
1554 /* No need to wait for thread callback, just kill immediately. */
1555 zebra_client_close(client);
1556 return -1;
1557 }
1558
paul718e3742002-12-13 20:15:29 +00001559 stream_reset (client->ibuf);
1560 zebra_event (ZEBRA_READ, sock, client);
paul718e3742002-12-13 20:15:29 +00001561 return 0;
1562}
1563
paul718e3742002-12-13 20:15:29 +00001564
1565/* Accept code of zebra server socket. */
paulb9df2d22004-05-09 09:09:59 +00001566static int
paul718e3742002-12-13 20:15:29 +00001567zebra_accept (struct thread *thread)
1568{
1569 int accept_sock;
1570 int client_sock;
1571 struct sockaddr_in client;
1572 socklen_t len;
1573
1574 accept_sock = THREAD_FD (thread);
1575
ajs719e9742005-02-28 20:52:15 +00001576 /* Reregister myself. */
1577 zebra_event (ZEBRA_SERV, accept_sock, NULL);
1578
paul718e3742002-12-13 20:15:29 +00001579 len = sizeof (struct sockaddr_in);
1580 client_sock = accept (accept_sock, (struct sockaddr *) &client, &len);
1581
1582 if (client_sock < 0)
1583 {
ajs6099b3b2004-11-20 02:06:59 +00001584 zlog_warn ("Can't accept zebra socket: %s", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001585 return -1;
1586 }
1587
paulccf35572003-03-01 11:42:20 +00001588 /* Make client socket non-blocking. */
ajs719e9742005-02-28 20:52:15 +00001589 set_nonblocking(client_sock);
paul865b8522005-01-05 08:30:35 +00001590
paul718e3742002-12-13 20:15:29 +00001591 /* Create new zebra client. */
1592 zebra_client_create (client_sock);
1593
paul718e3742002-12-13 20:15:29 +00001594 return 0;
1595}
1596
paulb9df2d22004-05-09 09:09:59 +00001597#ifdef HAVE_TCP_ZEBRA
paul718e3742002-12-13 20:15:29 +00001598/* Make zebra's server socket. */
paulb9df2d22004-05-09 09:09:59 +00001599static void
paul718e3742002-12-13 20:15:29 +00001600zebra_serv ()
1601{
1602 int ret;
1603 int accept_sock;
1604 struct sockaddr_in addr;
1605
1606 accept_sock = socket (AF_INET, SOCK_STREAM, 0);
1607
1608 if (accept_sock < 0)
1609 {
paul3d1dc852005-04-05 00:45:23 +00001610 zlog_warn ("Can't create zserv stream socket: %s",
1611 safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001612 zlog_warn ("zebra can't provice full functionality due to above error");
1613 return;
1614 }
1615
Vyacheslav Trushkin2ea1ab12011-12-11 18:48:47 +04001616 memset (&route_type_oaths, 0, sizeof (route_type_oaths));
paul718e3742002-12-13 20:15:29 +00001617 memset (&addr, 0, sizeof (struct sockaddr_in));
1618 addr.sin_family = AF_INET;
1619 addr.sin_port = htons (ZEBRA_PORT);
Paul Jakma6f0e3f62007-05-10 02:38:51 +00001620#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
paul718e3742002-12-13 20:15:29 +00001621 addr.sin_len = sizeof (struct sockaddr_in);
Paul Jakma6f0e3f62007-05-10 02:38:51 +00001622#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
paul718e3742002-12-13 20:15:29 +00001623 addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
1624
1625 sockopt_reuseaddr (accept_sock);
1626 sockopt_reuseport (accept_sock);
1627
pauledd7c242003-06-04 13:59:38 +00001628 if ( zserv_privs.change(ZPRIVS_RAISE) )
1629 zlog (NULL, LOG_ERR, "Can't raise privileges");
1630
paul718e3742002-12-13 20:15:29 +00001631 ret = bind (accept_sock, (struct sockaddr *)&addr,
1632 sizeof (struct sockaddr_in));
1633 if (ret < 0)
1634 {
paul3d1dc852005-04-05 00:45:23 +00001635 zlog_warn ("Can't bind to stream socket: %s",
1636 safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001637 zlog_warn ("zebra can't provice full functionality due to above error");
1638 close (accept_sock); /* Avoid sd leak. */
1639 return;
1640 }
pauledd7c242003-06-04 13:59:38 +00001641
1642 if ( zserv_privs.change(ZPRIVS_LOWER) )
1643 zlog (NULL, LOG_ERR, "Can't lower privileges");
paul718e3742002-12-13 20:15:29 +00001644
1645 ret = listen (accept_sock, 1);
1646 if (ret < 0)
1647 {
paul3d1dc852005-04-05 00:45:23 +00001648 zlog_warn ("Can't listen to stream socket: %s",
1649 safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001650 zlog_warn ("zebra can't provice full functionality due to above error");
1651 close (accept_sock); /* Avoid sd leak. */
1652 return;
1653 }
1654
1655 zebra_event (ZEBRA_SERV, accept_sock, NULL);
1656}
David Lamparter4b6c3322015-04-21 09:47:57 +02001657#else /* HAVE_TCP_ZEBRA */
paul718e3742002-12-13 20:15:29 +00001658
1659/* For sockaddr_un. */
1660#include <sys/un.h>
1661
1662/* zebra server UNIX domain socket. */
paulb9df2d22004-05-09 09:09:59 +00001663static void
hassofce954f2004-10-07 20:29:24 +00001664zebra_serv_un (const char *path)
paul718e3742002-12-13 20:15:29 +00001665{
1666 int ret;
1667 int sock, len;
1668 struct sockaddr_un serv;
1669 mode_t old_mask;
1670
1671 /* First of all, unlink existing socket */
1672 unlink (path);
1673
1674 /* Set umask */
1675 old_mask = umask (0077);
1676
1677 /* Make UNIX domain socket. */
1678 sock = socket (AF_UNIX, SOCK_STREAM, 0);
1679 if (sock < 0)
1680 {
paul3d1dc852005-04-05 00:45:23 +00001681 zlog_warn ("Can't create zserv unix socket: %s",
1682 safe_strerror (errno));
1683 zlog_warn ("zebra can't provide full functionality due to above error");
paul718e3742002-12-13 20:15:29 +00001684 return;
1685 }
1686
Vyacheslav Trushkin2ea1ab12011-12-11 18:48:47 +04001687 memset (&route_type_oaths, 0, sizeof (route_type_oaths));
1688
paul718e3742002-12-13 20:15:29 +00001689 /* Make server socket. */
1690 memset (&serv, 0, sizeof (struct sockaddr_un));
1691 serv.sun_family = AF_UNIX;
1692 strncpy (serv.sun_path, path, strlen (path));
Paul Jakma6f0e3f62007-05-10 02:38:51 +00001693#ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
paul718e3742002-12-13 20:15:29 +00001694 len = serv.sun_len = SUN_LEN(&serv);
1695#else
1696 len = sizeof (serv.sun_family) + strlen (serv.sun_path);
Paul Jakma6f0e3f62007-05-10 02:38:51 +00001697#endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
paul718e3742002-12-13 20:15:29 +00001698
1699 ret = bind (sock, (struct sockaddr *) &serv, len);
1700 if (ret < 0)
1701 {
paul3d1dc852005-04-05 00:45:23 +00001702 zlog_warn ("Can't bind to unix socket %s: %s",
1703 path, safe_strerror (errno));
1704 zlog_warn ("zebra can't provide full functionality due to above error");
paul718e3742002-12-13 20:15:29 +00001705 close (sock);
1706 return;
1707 }
1708
1709 ret = listen (sock, 5);
1710 if (ret < 0)
1711 {
paul3d1dc852005-04-05 00:45:23 +00001712 zlog_warn ("Can't listen to unix socket %s: %s",
1713 path, safe_strerror (errno));
1714 zlog_warn ("zebra can't provide full functionality due to above error");
paul718e3742002-12-13 20:15:29 +00001715 close (sock);
1716 return;
1717 }
1718
1719 umask (old_mask);
1720
1721 zebra_event (ZEBRA_SERV, sock, NULL);
1722}
David Lamparter4b6c3322015-04-21 09:47:57 +02001723#endif /* HAVE_TCP_ZEBRA */
David Lamparter6b0655a2014-06-04 06:53:35 +02001724
paul718e3742002-12-13 20:15:29 +00001725
paulb9df2d22004-05-09 09:09:59 +00001726static void
paul718e3742002-12-13 20:15:29 +00001727zebra_event (enum event event, int sock, struct zserv *client)
1728{
1729 switch (event)
1730 {
1731 case ZEBRA_SERV:
paulb21b19c2003-06-15 01:28:29 +00001732 thread_add_read (zebrad.master, zebra_accept, client, sock);
paul718e3742002-12-13 20:15:29 +00001733 break;
1734 case ZEBRA_READ:
1735 client->t_read =
paulb21b19c2003-06-15 01:28:29 +00001736 thread_add_read (zebrad.master, zebra_client_read, client, sock);
paul718e3742002-12-13 20:15:29 +00001737 break;
1738 case ZEBRA_WRITE:
1739 /**/
1740 break;
1741 }
1742}
David Lamparter6b0655a2014-06-04 06:53:35 +02001743
paul718e3742002-12-13 20:15:29 +00001744/* Display default rtm_table for all clients. */
1745DEFUN (show_table,
1746 show_table_cmd,
1747 "show table",
1748 SHOW_STR
1749 "default routing table to use for all clients\n")
1750{
paulb21b19c2003-06-15 01:28:29 +00001751 vty_out (vty, "table %d%s", zebrad.rtm_table_default,
paul718e3742002-12-13 20:15:29 +00001752 VTY_NEWLINE);
1753 return CMD_SUCCESS;
1754}
1755
1756DEFUN (config_table,
1757 config_table_cmd,
1758 "table TABLENO",
1759 "Configure target kernel routing table\n"
1760 "TABLE integer\n")
1761{
paulb21b19c2003-06-15 01:28:29 +00001762 zebrad.rtm_table_default = strtol (argv[0], (char**)0, 10);
paul718e3742002-12-13 20:15:29 +00001763 return CMD_SUCCESS;
1764}
1765
hasso647e4f12003-05-25 11:43:52 +00001766DEFUN (ip_forwarding,
1767 ip_forwarding_cmd,
1768 "ip forwarding",
1769 IP_STR
1770 "Turn on IP forwarding")
1771{
1772 int ret;
1773
1774 ret = ipforward ();
hassob71f00f2004-10-13 12:20:35 +00001775 if (ret == 0)
1776 ret = ipforward_on ();
hasso647e4f12003-05-25 11:43:52 +00001777
hasso647e4f12003-05-25 11:43:52 +00001778 if (ret == 0)
1779 {
1780 vty_out (vty, "Can't turn on IP forwarding%s", VTY_NEWLINE);
1781 return CMD_WARNING;
1782 }
1783
1784 return CMD_SUCCESS;
1785}
1786
paul718e3742002-12-13 20:15:29 +00001787DEFUN (no_ip_forwarding,
1788 no_ip_forwarding_cmd,
1789 "no ip forwarding",
1790 NO_STR
1791 IP_STR
1792 "Turn off IP forwarding")
1793{
1794 int ret;
1795
1796 ret = ipforward ();
hassob71f00f2004-10-13 12:20:35 +00001797 if (ret != 0)
1798 ret = ipforward_off ();
paul718e3742002-12-13 20:15:29 +00001799
paul718e3742002-12-13 20:15:29 +00001800 if (ret != 0)
1801 {
1802 vty_out (vty, "Can't turn off IP forwarding%s", VTY_NEWLINE);
1803 return CMD_WARNING;
1804 }
1805
1806 return CMD_SUCCESS;
1807}
1808
1809/* This command is for debugging purpose. */
1810DEFUN (show_zebra_client,
1811 show_zebra_client_cmd,
1812 "show zebra client",
1813 SHOW_STR
1814 "Zebra information"
1815 "Client information")
1816{
hasso52dc7ee2004-09-23 19:18:23 +00001817 struct listnode *node;
paul718e3742002-12-13 20:15:29 +00001818 struct zserv *client;
1819
paul1eb8ef22005-04-07 07:30:20 +00001820 for (ALL_LIST_ELEMENTS_RO (zebrad.client_list, node, client))
1821 vty_out (vty, "Client fd %d%s", client->sock, VTY_NEWLINE);
1822
paul718e3742002-12-13 20:15:29 +00001823 return CMD_SUCCESS;
1824}
1825
1826/* Table configuration write function. */
paulb9df2d22004-05-09 09:09:59 +00001827static int
paul718e3742002-12-13 20:15:29 +00001828config_write_table (struct vty *vty)
1829{
paulb21b19c2003-06-15 01:28:29 +00001830 if (zebrad.rtm_table_default)
1831 vty_out (vty, "table %d%s", zebrad.rtm_table_default,
paul718e3742002-12-13 20:15:29 +00001832 VTY_NEWLINE);
1833 return 0;
1834}
1835
1836/* table node for routing tables. */
Stephen Hemminger7fc626d2008-12-01 11:10:34 -08001837static struct cmd_node table_node =
paul718e3742002-12-13 20:15:29 +00001838{
1839 TABLE_NODE,
1840 "", /* This node has no interface. */
1841 1
1842};
David Lamparter6b0655a2014-06-04 06:53:35 +02001843
paul718e3742002-12-13 20:15:29 +00001844/* Only display ip forwarding is enabled or not. */
1845DEFUN (show_ip_forwarding,
1846 show_ip_forwarding_cmd,
1847 "show ip forwarding",
1848 SHOW_STR
1849 IP_STR
1850 "IP forwarding status\n")
1851{
1852 int ret;
1853
1854 ret = ipforward ();
1855
1856 if (ret == 0)
1857 vty_out (vty, "IP forwarding is off%s", VTY_NEWLINE);
1858 else
1859 vty_out (vty, "IP forwarding is on%s", VTY_NEWLINE);
1860 return CMD_SUCCESS;
1861}
1862
1863#ifdef HAVE_IPV6
1864/* Only display ipv6 forwarding is enabled or not. */
1865DEFUN (show_ipv6_forwarding,
1866 show_ipv6_forwarding_cmd,
1867 "show ipv6 forwarding",
1868 SHOW_STR
1869 "IPv6 information\n"
1870 "Forwarding status\n")
1871{
1872 int ret;
1873
1874 ret = ipforward_ipv6 ();
1875
1876 switch (ret)
1877 {
1878 case -1:
1879 vty_out (vty, "ipv6 forwarding is unknown%s", VTY_NEWLINE);
1880 break;
1881 case 0:
1882 vty_out (vty, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE);
1883 break;
1884 case 1:
1885 vty_out (vty, "ipv6 forwarding is %s%s", "on", VTY_NEWLINE);
1886 break;
1887 default:
1888 vty_out (vty, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE);
1889 break;
1890 }
1891 return CMD_SUCCESS;
1892}
1893
hasso55906722004-02-11 22:42:16 +00001894DEFUN (ipv6_forwarding,
1895 ipv6_forwarding_cmd,
1896 "ipv6 forwarding",
1897 IPV6_STR
1898 "Turn on IPv6 forwarding")
1899{
1900 int ret;
1901
hasso41d3fc92004-04-06 11:59:00 +00001902 ret = ipforward_ipv6 ();
hassob71f00f2004-10-13 12:20:35 +00001903 if (ret == 0)
1904 ret = ipforward_ipv6_on ();
hasso41d3fc92004-04-06 11:59:00 +00001905
hasso41d3fc92004-04-06 11:59:00 +00001906 if (ret == 0)
1907 {
hasso55906722004-02-11 22:42:16 +00001908 vty_out (vty, "Can't turn on IPv6 forwarding%s", VTY_NEWLINE);
1909 return CMD_WARNING;
1910 }
1911
1912 return CMD_SUCCESS;
1913}
1914
paul718e3742002-12-13 20:15:29 +00001915DEFUN (no_ipv6_forwarding,
1916 no_ipv6_forwarding_cmd,
1917 "no ipv6 forwarding",
1918 NO_STR
hasso55906722004-02-11 22:42:16 +00001919 IPV6_STR
1920 "Turn off IPv6 forwarding")
paul718e3742002-12-13 20:15:29 +00001921{
1922 int ret;
1923
hasso41d3fc92004-04-06 11:59:00 +00001924 ret = ipforward_ipv6 ();
hassob71f00f2004-10-13 12:20:35 +00001925 if (ret != 0)
1926 ret = ipforward_ipv6_off ();
hasso41d3fc92004-04-06 11:59:00 +00001927
paul718e3742002-12-13 20:15:29 +00001928 if (ret != 0)
1929 {
1930 vty_out (vty, "Can't turn off IPv6 forwarding%s", VTY_NEWLINE);
1931 return CMD_WARNING;
1932 }
1933
1934 return CMD_SUCCESS;
1935}
1936
1937#endif /* HAVE_IPV6 */
1938
1939/* IPForwarding configuration write function. */
ajs719e9742005-02-28 20:52:15 +00001940static int
paul718e3742002-12-13 20:15:29 +00001941config_write_forwarding (struct vty *vty)
1942{
hasso18a6dce2004-10-03 18:18:34 +00001943 /* FIXME: Find better place for that. */
1944 router_id_write (vty);
1945
paul3e0b3a52004-08-23 18:58:32 +00001946 if (ipforward ())
1947 vty_out (vty, "ip forwarding%s", VTY_NEWLINE);
paul718e3742002-12-13 20:15:29 +00001948#ifdef HAVE_IPV6
paul3e0b3a52004-08-23 18:58:32 +00001949 if (ipforward_ipv6 ())
1950 vty_out (vty, "ipv6 forwarding%s", VTY_NEWLINE);
paul718e3742002-12-13 20:15:29 +00001951#endif /* HAVE_IPV6 */
1952 vty_out (vty, "!%s", VTY_NEWLINE);
1953 return 0;
1954}
1955
1956/* table node for routing tables. */
Stephen Hemminger7fc626d2008-12-01 11:10:34 -08001957static struct cmd_node forwarding_node =
paul718e3742002-12-13 20:15:29 +00001958{
1959 FORWARDING_NODE,
1960 "", /* This node has no interface. */
1961 1
1962};
1963
Udaya Shankara KSd869dbd2016-02-11 21:42:29 +05301964#ifdef HAVE_FPM
1965/* function to write the fpm config info */
1966static int
1967config_write_fpm (struct vty *vty)
1968{
1969 return
1970 fpm_remote_srv_write (vty);
1971}
1972
1973/* Zebra node */
1974static struct cmd_node zebra_node =
1975{
1976 ZEBRA_NODE,
1977 "",
1978 1
1979};
1980#endif
1981
David Lamparter6b0655a2014-06-04 06:53:35 +02001982
paul718e3742002-12-13 20:15:29 +00001983/* Initialisation of zebra and installation of commands. */
1984void
paula1ac18c2005-06-28 17:17:12 +00001985zebra_init (void)
paul718e3742002-12-13 20:15:29 +00001986{
1987 /* Client list init. */
paulb21b19c2003-06-15 01:28:29 +00001988 zebrad.client_list = list_new ();
paul718e3742002-12-13 20:15:29 +00001989
paul718e3742002-12-13 20:15:29 +00001990 /* Install configuration write function. */
1991 install_node (&table_node, config_write_table);
1992 install_node (&forwarding_node, config_write_forwarding);
Udaya Shankara KSd869dbd2016-02-11 21:42:29 +05301993#ifdef HAVE_FPM
1994 install_node (&zebra_node, config_write_fpm);
1995#endif
paul718e3742002-12-13 20:15:29 +00001996
1997 install_element (VIEW_NODE, &show_ip_forwarding_cmd);
hasso647e4f12003-05-25 11:43:52 +00001998 install_element (CONFIG_NODE, &ip_forwarding_cmd);
paul718e3742002-12-13 20:15:29 +00001999 install_element (CONFIG_NODE, &no_ip_forwarding_cmd);
2000 install_element (ENABLE_NODE, &show_zebra_client_cmd);
2001
2002#ifdef HAVE_NETLINK
2003 install_element (VIEW_NODE, &show_table_cmd);
paul718e3742002-12-13 20:15:29 +00002004 install_element (CONFIG_NODE, &config_table_cmd);
2005#endif /* HAVE_NETLINK */
2006
2007#ifdef HAVE_IPV6
2008 install_element (VIEW_NODE, &show_ipv6_forwarding_cmd);
hasso55906722004-02-11 22:42:16 +00002009 install_element (CONFIG_NODE, &ipv6_forwarding_cmd);
paul718e3742002-12-13 20:15:29 +00002010 install_element (CONFIG_NODE, &no_ipv6_forwarding_cmd);
2011#endif /* HAVE_IPV6 */
Paul Jakma7514fb72007-05-02 16:05:35 +00002012
2013 /* Route-map */
2014 zebra_route_map_init ();
paul718e3742002-12-13 20:15:29 +00002015}
Denis Ovsienko97be79f2009-07-24 20:45:31 +04002016
2017/* Make zebra server socket, wiping any existing one (see bug #403). */
2018void
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +04002019zebra_zserv_socket_init (char *path)
Denis Ovsienko97be79f2009-07-24 20:45:31 +04002020{
2021#ifdef HAVE_TCP_ZEBRA
2022 zebra_serv ();
2023#else
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +04002024 zebra_serv_un (path ? path : ZEBRA_SERV_PATH);
Denis Ovsienko97be79f2009-07-24 20:45:31 +04002025#endif /* HAVE_TCP_ZEBRA */
2026}