blob: 89232c27c85f3119a6353a69124801defdddac4f [file] [log] [blame]
paul718e3742002-12-13 20:15:29 +00001/* Zebra daemon server routine.
2 * Copyright (C) 1997, 98, 99 Kunihiro Ishiguro
3 *
4 * This file is part of GNU Zebra.
5 *
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
9 * later version.
10 *
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with GNU Zebra; see the file COPYING. If not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
20 */
21
22#include <zebra.h>
23
24#include "prefix.h"
25#include "command.h"
26#include "if.h"
27#include "thread.h"
28#include "stream.h"
29#include "memory.h"
30#include "table.h"
31#include "rib.h"
32#include "network.h"
33#include "sockunion.h"
34#include "log.h"
35#include "zclient.h"
pauledd7c242003-06-04 13:59:38 +000036#include "privs.h"
ajs719e9742005-02-28 20:52:15 +000037#include "network.h"
38#include "buffer.h"
Feng Lu0d0686f2015-05-22 11:40:02 +020039#include "vrf.h"
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -050040#include "nexthop.h"
paul718e3742002-12-13 20:15:29 +000041
42#include "zebra/zserv.h"
hasso18a6dce2004-10-03 18:18:34 +000043#include "zebra/router-id.h"
paul718e3742002-12-13 20:15:29 +000044#include "zebra/redistribute.h"
45#include "zebra/debug.h"
46#include "zebra/ipforward.h"
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -050047#include "zebra/zebra_rnh.h"
David Lamparter6b0655a2014-06-04 06:53:35 +020048
paul718e3742002-12-13 20:15:29 +000049/* Event list of zebra. */
50enum event { ZEBRA_SERV, ZEBRA_READ, ZEBRA_WRITE };
51
paulb21b19c2003-06-15 01:28:29 +000052extern struct zebra_t zebrad;
paul718e3742002-12-13 20:15:29 +000053
paulb9df2d22004-05-09 09:09:59 +000054static void zebra_event (enum event event, int sock, struct zserv *client);
paulccf35572003-03-01 11:42:20 +000055
pauledd7c242003-06-04 13:59:38 +000056extern struct zebra_privs_t zserv_privs;
David Lamparter6b0655a2014-06-04 06:53:35 +020057
ajs719e9742005-02-28 20:52:15 +000058static void zebra_client_close (struct zserv *client);
59
60static int
61zserv_delayed_close(struct thread *thread)
paulccf35572003-03-01 11:42:20 +000062{
ajs719e9742005-02-28 20:52:15 +000063 struct zserv *client = THREAD_ARG(thread);
paulccf35572003-03-01 11:42:20 +000064
ajs719e9742005-02-28 20:52:15 +000065 client->t_suicide = NULL;
66 zebra_client_close(client);
paulccf35572003-03-01 11:42:20 +000067 return 0;
68}
69
Vyacheslav Trushkin2ea1ab12011-12-11 18:48:47 +040070/* When client connects, it sends hello message
71 * with promise to send zebra routes of specific type.
72 * Zebra stores a socket fd of the client into
73 * this array. And use it to clean up routes that
74 * client didn't remove for some reasons after closing
75 * connection.
76 */
77static int route_type_oaths[ZEBRA_ROUTE_MAX];
78
ajs719e9742005-02-28 20:52:15 +000079static int
80zserv_flush_data(struct thread *thread)
paulccf35572003-03-01 11:42:20 +000081{
ajs719e9742005-02-28 20:52:15 +000082 struct zserv *client = THREAD_ARG(thread);
paulccf35572003-03-01 11:42:20 +000083
ajs719e9742005-02-28 20:52:15 +000084 client->t_write = NULL;
85 if (client->t_suicide)
86 {
87 zebra_client_close(client);
88 return -1;
89 }
90 switch (buffer_flush_available(client->wb, client->sock))
91 {
92 case BUFFER_ERROR:
93 zlog_warn("%s: buffer_flush_available failed on zserv client fd %d, "
94 "closing", __func__, client->sock);
95 zebra_client_close(client);
96 break;
97 case BUFFER_PENDING:
98 client->t_write = thread_add_write(zebrad.master, zserv_flush_data,
99 client, client->sock);
100 break;
101 case BUFFER_EMPTY:
102 break;
103 }
104 return 0;
paulccf35572003-03-01 11:42:20 +0000105}
106
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -0500107int
ajs719e9742005-02-28 20:52:15 +0000108zebra_server_send_message(struct zserv *client)
paulccf35572003-03-01 11:42:20 +0000109{
ajs719e9742005-02-28 20:52:15 +0000110 if (client->t_suicide)
111 return -1;
112 switch (buffer_write(client->wb, client->sock, STREAM_DATA(client->obuf),
113 stream_get_endp(client->obuf)))
paulccf35572003-03-01 11:42:20 +0000114 {
ajs719e9742005-02-28 20:52:15 +0000115 case BUFFER_ERROR:
116 zlog_warn("%s: buffer_write failed to zserv client fd %d, closing",
117 __func__, client->sock);
118 /* Schedule a delayed close since many of the functions that call this
119 one do not check the return code. They do not allow for the
120 possibility that an I/O error may have caused the client to be
121 deleted. */
122 client->t_suicide = thread_add_event(zebrad.master, zserv_delayed_close,
123 client, 0);
124 return -1;
ajs719e9742005-02-28 20:52:15 +0000125 case BUFFER_EMPTY:
126 THREAD_OFF(client->t_write);
127 break;
128 case BUFFER_PENDING:
129 THREAD_WRITE_ON(zebrad.master, client->t_write,
130 zserv_flush_data, client, client->sock);
131 break;
paulccf35572003-03-01 11:42:20 +0000132 }
paulccf35572003-03-01 11:42:20 +0000133 return 0;
134}
135
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -0500136void
Feng Luc99f3482014-10-16 09:52:36 +0800137zserv_create_header (struct stream *s, uint16_t cmd, vrf_id_t vrf_id)
paulc1b98002006-01-16 01:54:02 +0000138{
139 /* length placeholder, caller can update */
140 stream_putw (s, ZEBRA_HEADER_SIZE);
141 stream_putc (s, ZEBRA_HEADER_MARKER);
142 stream_putc (s, ZSERV_VERSION);
Feng Luc99f3482014-10-16 09:52:36 +0800143 stream_putw (s, vrf_id);
paulc1b98002006-01-16 01:54:02 +0000144 stream_putw (s, cmd);
145}
146
Josh Bailey51d4ef82012-03-21 17:13:39 -0700147static void
148zserv_encode_interface (struct stream *s, struct interface *ifp)
149{
150 /* Interface information. */
151 stream_put (s, ifp->name, INTERFACE_NAMSIZ);
152 stream_putl (s, ifp->ifindex);
153 stream_putc (s, ifp->status);
154 stream_putq (s, ifp->flags);
155 stream_putl (s, ifp->metric);
156 stream_putl (s, ifp->mtu);
157 stream_putl (s, ifp->mtu6);
158 stream_putl (s, ifp->bandwidth);
Timo Teräs954c7d62016-01-15 17:36:33 +0200159 stream_putl (s, ifp->ll_type);
Josh Bailey51d4ef82012-03-21 17:13:39 -0700160 stream_putl (s, ifp->hw_addr_len);
161 if (ifp->hw_addr_len)
162 stream_put (s, ifp->hw_addr, ifp->hw_addr_len);
Josh Bailey51d4ef82012-03-21 17:13:39 -0700163
Olivier Dugeon15773a82016-04-19 18:29:55 +0200164 zlog_info("Try to set TE Link Param");
165 /* Then, Traffic Engineering parameters if any */
166 if (HAS_LINK_PARAMS(ifp) && IS_LINK_PARAMS_SET(ifp->link_params))
167 {
168 stream_putc (s, 1);
169 zebra_interface_link_params_write (s, ifp);
170 }
171 else
172 stream_putc (s, 0);
173
Josh Bailey51d4ef82012-03-21 17:13:39 -0700174 /* Write packet size. */
175 stream_putw_at (s, 0, stream_get_endp (s));
176}
177
paul718e3742002-12-13 20:15:29 +0000178/* Interface is added. Send ZEBRA_INTERFACE_ADD to client. */
paulb9df2d22004-05-09 09:09:59 +0000179/*
180 * This function is called in the following situations:
181 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
182 * from the client.
183 * - at startup, when zebra figures out the available interfaces
184 * - when an interface is added (where support for
185 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
186 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
187 * received)
188 */
paul718e3742002-12-13 20:15:29 +0000189int
190zsend_interface_add (struct zserv *client, struct interface *ifp)
191{
192 struct stream *s;
193
194 /* Check this client need interface information. */
Feng Luc99f3482014-10-16 09:52:36 +0800195 if (! vrf_bitmap_check (client->ifinfo, ifp->vrf_id))
ajs719e9742005-02-28 20:52:15 +0000196 return 0;
paul718e3742002-12-13 20:15:29 +0000197
198 s = client->obuf;
199 stream_reset (s);
200
Feng Luc99f3482014-10-16 09:52:36 +0800201 zserv_create_header (s, ZEBRA_INTERFACE_ADD, ifp->vrf_id);
Josh Bailey51d4ef82012-03-21 17:13:39 -0700202 zserv_encode_interface (s, ifp);
paul718e3742002-12-13 20:15:29 +0000203
ajs719e9742005-02-28 20:52:15 +0000204 return zebra_server_send_message(client);
paul718e3742002-12-13 20:15:29 +0000205}
206
207/* Interface deletion from zebra daemon. */
208int
209zsend_interface_delete (struct zserv *client, struct interface *ifp)
210{
211 struct stream *s;
212
213 /* Check this client need interface information. */
Feng Luc99f3482014-10-16 09:52:36 +0800214 if (! vrf_bitmap_check (client->ifinfo, ifp->vrf_id))
ajs719e9742005-02-28 20:52:15 +0000215 return 0;
paul718e3742002-12-13 20:15:29 +0000216
217 s = client->obuf;
218 stream_reset (s);
paul718e3742002-12-13 20:15:29 +0000219
Feng Luc99f3482014-10-16 09:52:36 +0800220 zserv_create_header (s, ZEBRA_INTERFACE_DELETE, ifp->vrf_id);
Josh Bailey51d4ef82012-03-21 17:13:39 -0700221 zserv_encode_interface (s, ifp);
paul718e3742002-12-13 20:15:29 +0000222
ajs719e9742005-02-28 20:52:15 +0000223 return zebra_server_send_message (client);
paul718e3742002-12-13 20:15:29 +0000224}
225
Olivier Dugeon15773a82016-04-19 18:29:55 +0200226int
227zsend_interface_link_params (struct zserv *client, struct interface *ifp)
228{
229 struct stream *s;
230
231 /* Check this client need interface information. */
232 if (! client->ifinfo)
233 return 0;
234
235 if (!ifp->link_params)
236 return 0;
237 s = client->obuf;
238 stream_reset (s);
239
240 zserv_create_header (s, ZEBRA_INTERFACE_LINK_PARAMS, ifp->vrf_id);
241
242 /* Add Interface Index */
243 stream_putl (s, ifp->ifindex);
244
245 /* Then TE Link Parameters */
246 if (zebra_interface_link_params_write (s, ifp) == 0)
247 return 0;
248
249 /* Write packet size. */
250 stream_putw_at (s, 0, stream_get_endp (s));
251
252 return zebra_server_send_message (client);
253}
254
paulb9df2d22004-05-09 09:09:59 +0000255/* Interface address is added/deleted. Send ZEBRA_INTERFACE_ADDRESS_ADD or
256 * ZEBRA_INTERFACE_ADDRESS_DELETE to the client.
257 *
258 * A ZEBRA_INTERFACE_ADDRESS_ADD is sent in the following situations:
259 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
260 * from the client, after the ZEBRA_INTERFACE_ADD has been
261 * sent from zebra to the client
262 * - redistribute new address info to all clients in the following situations
263 * - at startup, when zebra figures out the available interfaces
264 * - when an interface is added (where support for
265 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
266 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
267 * received)
268 * - for the vty commands "ip address A.B.C.D/M [<secondary>|<label LINE>]"
269 * and "no bandwidth <1-10000000>", "ipv6 address X:X::X:X/M"
270 * - when an RTM_NEWADDR message is received from the kernel,
271 *
272 * The call tree that triggers ZEBRA_INTERFACE_ADDRESS_DELETE:
273 *
274 * zsend_interface_address(DELETE)
275 * ^
276 * |
277 * zebra_interface_address_delete_update
278 * ^ ^ ^
paul6eb88272005-07-29 14:36:00 +0000279 * | | if_delete_update
280 * | |
paulb9df2d22004-05-09 09:09:59 +0000281 * ip_address_uninstall connected_delete_ipv4
282 * [ipv6_addresss_uninstall] [connected_delete_ipv6]
283 * ^ ^
284 * | |
285 * | RTM_NEWADDR on routing/netlink socket
286 * |
287 * vty commands:
288 * "no ip address A.B.C.D/M [label LINE]"
289 * "no ip address A.B.C.D/M secondary"
290 * ["no ipv6 address X:X::X:X/M"]
291 *
292 */
paul718e3742002-12-13 20:15:29 +0000293int
paulb9df2d22004-05-09 09:09:59 +0000294zsend_interface_address (int cmd, struct zserv *client,
295 struct interface *ifp, struct connected *ifc)
paul718e3742002-12-13 20:15:29 +0000296{
297 int blen;
298 struct stream *s;
299 struct prefix *p;
300
301 /* Check this client need interface information. */
Feng Luc99f3482014-10-16 09:52:36 +0800302 if (! vrf_bitmap_check (client->ifinfo, ifp->vrf_id))
ajs719e9742005-02-28 20:52:15 +0000303 return 0;
paul718e3742002-12-13 20:15:29 +0000304
305 s = client->obuf;
306 stream_reset (s);
paulc1b98002006-01-16 01:54:02 +0000307
Feng Luc99f3482014-10-16 09:52:36 +0800308 zserv_create_header (s, cmd, ifp->vrf_id);
paul718e3742002-12-13 20:15:29 +0000309 stream_putl (s, ifp->ifindex);
310
311 /* Interface address flag. */
312 stream_putc (s, ifc->flags);
313
314 /* Prefix information. */
315 p = ifc->address;
316 stream_putc (s, p->family);
317 blen = prefix_blen (p);
318 stream_put (s, &p->u.prefix, blen);
paulb9df2d22004-05-09 09:09:59 +0000319
320 /*
321 * XXX gnu version does not send prefixlen for ZEBRA_INTERFACE_ADDRESS_DELETE
322 * but zebra_interface_address_delete_read() in the gnu version
323 * expects to find it
324 */
paul718e3742002-12-13 20:15:29 +0000325 stream_putc (s, p->prefixlen);
326
327 /* Destination. */
328 p = ifc->destination;
329 if (p)
330 stream_put (s, &p->u.prefix, blen);
331 else
332 stream_put (s, NULL, blen);
333
334 /* Write packet size. */
335 stream_putw_at (s, 0, stream_get_endp (s));
336
ajs719e9742005-02-28 20:52:15 +0000337 return zebra_server_send_message(client);
paul718e3742002-12-13 20:15:29 +0000338}
339
paulb9df2d22004-05-09 09:09:59 +0000340/*
341 * The cmd passed to zsend_interface_update may be ZEBRA_INTERFACE_UP or
342 * ZEBRA_INTERFACE_DOWN.
343 *
344 * The ZEBRA_INTERFACE_UP message is sent from the zebra server to
345 * the clients in one of 2 situations:
346 * - an if_up is detected e.g., as a result of an RTM_IFINFO message
347 * - a vty command modifying the bandwidth of an interface is received.
348 * The ZEBRA_INTERFACE_DOWN message is sent when an if_down is detected.
349 */
paul718e3742002-12-13 20:15:29 +0000350int
paulb9df2d22004-05-09 09:09:59 +0000351zsend_interface_update (int cmd, struct zserv *client, struct interface *ifp)
paul718e3742002-12-13 20:15:29 +0000352{
353 struct stream *s;
354
355 /* Check this client need interface information. */
Feng Luc99f3482014-10-16 09:52:36 +0800356 if (! vrf_bitmap_check (client->ifinfo, ifp->vrf_id))
ajs719e9742005-02-28 20:52:15 +0000357 return 0;
paul718e3742002-12-13 20:15:29 +0000358
359 s = client->obuf;
360 stream_reset (s);
361
Feng Luc99f3482014-10-16 09:52:36 +0800362 zserv_create_header (s, cmd, ifp->vrf_id);
Josh Bailey51d4ef82012-03-21 17:13:39 -0700363 zserv_encode_interface (s, ifp);
paul718e3742002-12-13 20:15:29 +0000364
ajs719e9742005-02-28 20:52:15 +0000365 return zebra_server_send_message(client);
paul718e3742002-12-13 20:15:29 +0000366}
367
paulb9df2d22004-05-09 09:09:59 +0000368/*
369 * The zebra server sends the clients a ZEBRA_IPV4_ROUTE_ADD or a
370 * ZEBRA_IPV6_ROUTE_ADD via zsend_route_multipath in the following
371 * situations:
372 * - when the client starts up, and requests default information
373 * by sending a ZEBRA_REDISTRIBUTE_DEFAULT_ADD to the zebra server, in the
374 * - case of rip, ripngd, ospfd and ospf6d, when the client sends a
375 * ZEBRA_REDISTRIBUTE_ADD as a result of the "redistribute" vty cmd,
376 * - when the zebra server redistributes routes after it updates its rib
377 *
378 * The zebra server sends clients a ZEBRA_IPV4_ROUTE_DELETE or a
379 * ZEBRA_IPV6_ROUTE_DELETE via zsend_route_multipath when:
380 * - a "ip route" or "ipv6 route" vty command is issued, a prefix is
381 * - deleted from zebra's rib, and this info
382 * has to be redistributed to the clients
383 *
384 * XXX The ZEBRA_IPV*_ROUTE_ADD message is also sent by the client to the
385 * zebra server when the client wants to tell the zebra server to add a
386 * route to the kernel (zapi_ipv4_add etc. ). Since it's essentially the
387 * same message being sent back and forth, this function and
388 * zapi_ipv{4,6}_{add, delete} should be re-written to avoid code
389 * duplication.
390 */
paul718e3742002-12-13 20:15:29 +0000391int
paulb9df2d22004-05-09 09:09:59 +0000392zsend_route_multipath (int cmd, struct zserv *client, struct prefix *p,
393 struct rib *rib)
paul718e3742002-12-13 20:15:29 +0000394{
395 int psize;
396 struct stream *s;
397 struct nexthop *nexthop;
paul1dcb5172005-05-31 08:38:50 +0000398 unsigned long nhnummark = 0, messmark = 0;
paulb9df2d22004-05-09 09:09:59 +0000399 int nhnum = 0;
paul1dcb5172005-05-31 08:38:50 +0000400 u_char zapi_flags = 0;
Feng Luc99f3482014-10-16 09:52:36 +0800401
402 /* Check this client need this route. */
403 if (!vrf_bitmap_check (client->redist[rib->type], rib->vrf_id) &&
404 !(is_default (p) &&
405 vrf_bitmap_check (client->redist_default, rib->vrf_id)))
406 return 0;
407
paul718e3742002-12-13 20:15:29 +0000408 s = client->obuf;
409 stream_reset (s);
paulc1b98002006-01-16 01:54:02 +0000410
Feng Luc99f3482014-10-16 09:52:36 +0800411 zserv_create_header (s, cmd, rib->vrf_id);
paulc1b98002006-01-16 01:54:02 +0000412
413 /* Put type and nexthop. */
paul718e3742002-12-13 20:15:29 +0000414 stream_putc (s, rib->type);
415 stream_putc (s, rib->flags);
paul1dcb5172005-05-31 08:38:50 +0000416
417 /* marker for message flags field */
418 messmark = stream_get_endp (s);
419 stream_putc (s, 0);
paul718e3742002-12-13 20:15:29 +0000420
421 /* Prefix. */
422 psize = PSIZE (p->prefixlen);
423 stream_putc (s, p->prefixlen);
paulb9df2d22004-05-09 09:09:59 +0000424 stream_write (s, (u_char *) & p->u.prefix, psize);
paul718e3742002-12-13 20:15:29 +0000425
paulb9df2d22004-05-09 09:09:59 +0000426 /*
427 * XXX The message format sent by zebra below does not match the format
428 * of the corresponding message expected by the zebra server
429 * itself (e.g., see zread_ipv4_add). The nexthop_num is not set correctly,
430 * (is there a bug on the client side if more than one segment is sent?)
431 * nexthop ZEBRA_NEXTHOP_IPV4 is never set, ZEBRA_NEXTHOP_IFINDEX
432 * is hard-coded.
433 */
paul718e3742002-12-13 20:15:29 +0000434 /* Nexthop */
paul1dcb5172005-05-31 08:38:50 +0000435
paul718e3742002-12-13 20:15:29 +0000436 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
437 {
Timo Teräs325823a2016-01-15 17:36:31 +0200438 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE))
paulb9df2d22004-05-09 09:09:59 +0000439 {
paul1dcb5172005-05-31 08:38:50 +0000440 SET_FLAG (zapi_flags, ZAPI_MESSAGE_NEXTHOP);
441 SET_FLAG (zapi_flags, ZAPI_MESSAGE_IFINDEX);
442
443 if (nhnummark == 0)
444 {
445 nhnummark = stream_get_endp (s);
446 stream_putc (s, 1); /* placeholder */
447 }
448
paulb9df2d22004-05-09 09:09:59 +0000449 nhnum++;
paul718e3742002-12-13 20:15:29 +0000450
paulb9df2d22004-05-09 09:09:59 +0000451 switch(nexthop->type)
452 {
453 case NEXTHOP_TYPE_IPV4:
454 case NEXTHOP_TYPE_IPV4_IFINDEX:
455 stream_put_in_addr (s, &nexthop->gate.ipv4);
456 break;
457#ifdef HAVE_IPV6
458 case NEXTHOP_TYPE_IPV6:
459 case NEXTHOP_TYPE_IPV6_IFINDEX:
460 case NEXTHOP_TYPE_IPV6_IFNAME:
461 stream_write (s, (u_char *) &nexthop->gate.ipv6, 16);
462 break;
463#endif
464 default:
465 if (cmd == ZEBRA_IPV4_ROUTE_ADD
466 || cmd == ZEBRA_IPV4_ROUTE_DELETE)
467 {
468 struct in_addr empty;
paul44983cf2004-09-22 13:15:58 +0000469 memset (&empty, 0, sizeof (struct in_addr));
paulb9df2d22004-05-09 09:09:59 +0000470 stream_write (s, (u_char *) &empty, IPV4_MAX_BYTELEN);
471 }
472 else
473 {
474 struct in6_addr empty;
475 memset (&empty, 0, sizeof (struct in6_addr));
476 stream_write (s, (u_char *) &empty, IPV6_MAX_BYTELEN);
477 }
478 }
paul718e3742002-12-13 20:15:29 +0000479
paulb9df2d22004-05-09 09:09:59 +0000480 /* Interface index. */
481 stream_putc (s, 1);
482 stream_putl (s, nexthop->ifindex);
paul718e3742002-12-13 20:15:29 +0000483
paulb9df2d22004-05-09 09:09:59 +0000484 break;
485 }
paul718e3742002-12-13 20:15:29 +0000486 }
487
488 /* Metric */
Stephen Hemmingercf8a8312010-08-18 15:56:46 -0700489 if (cmd == ZEBRA_IPV4_ROUTE_ADD || cmd == ZEBRA_IPV6_ROUTE_ADD)
paul1dcb5172005-05-31 08:38:50 +0000490 {
vincentfbf5d032005-09-29 11:25:50 +0000491 SET_FLAG (zapi_flags, ZAPI_MESSAGE_DISTANCE);
492 stream_putc (s, rib->distance);
paul1dcb5172005-05-31 08:38:50 +0000493 SET_FLAG (zapi_flags, ZAPI_MESSAGE_METRIC);
494 stream_putl (s, rib->metric);
Timo Teräsb11f3b52015-11-02 16:50:07 +0200495 SET_FLAG (zapi_flags, ZAPI_MESSAGE_MTU);
496 stream_putl (s, rib->mtu);
Piotr Chytłaeefddcc2015-12-01 09:48:02 -0500497 /* tag */
498 if (rib->tag)
499 {
500 SET_FLAG(zapi_flags, ZAPI_MESSAGE_TAG);
501 stream_putw(s, rib->tag);
502 }
paul1dcb5172005-05-31 08:38:50 +0000503 }
504
505 /* write real message flags value */
506 stream_putc_at (s, messmark, zapi_flags);
507
paulb9df2d22004-05-09 09:09:59 +0000508 /* Write next-hop number */
509 if (nhnummark)
hassoc1eaa442004-10-19 06:26:01 +0000510 stream_putc_at (s, nhnummark, nhnum);
paulb9df2d22004-05-09 09:09:59 +0000511
paul718e3742002-12-13 20:15:29 +0000512 /* Write packet size. */
513 stream_putw_at (s, 0, stream_get_endp (s));
514
ajs719e9742005-02-28 20:52:15 +0000515 return zebra_server_send_message(client);
paul718e3742002-12-13 20:15:29 +0000516}
517
paul718e3742002-12-13 20:15:29 +0000518#ifdef HAVE_IPV6
ajs719e9742005-02-28 20:52:15 +0000519static int
Feng Luc99f3482014-10-16 09:52:36 +0800520zsend_ipv6_nexthop_lookup (struct zserv *client, struct in6_addr *addr,
521 vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +0000522{
523 struct stream *s;
524 struct rib *rib;
525 unsigned long nump;
526 u_char num;
527 struct nexthop *nexthop;
528
529 /* Lookup nexthop. */
Feng Luc99f3482014-10-16 09:52:36 +0800530 rib = rib_match_ipv6 (addr, vrf_id);
paul718e3742002-12-13 20:15:29 +0000531
532 /* Get output stream. */
533 s = client->obuf;
534 stream_reset (s);
535
536 /* Fill in result. */
Feng Luc99f3482014-10-16 09:52:36 +0800537 zserv_create_header (s, ZEBRA_IPV6_NEXTHOP_LOOKUP, vrf_id);
Hiroshi Yokoi8ccd74c2015-09-08 11:52:20 +0900538 stream_put (s, addr, 16);
paul718e3742002-12-13 20:15:29 +0000539
540 if (rib)
541 {
542 stream_putl (s, rib->metric);
543 num = 0;
paul9985f832005-02-09 15:51:56 +0000544 nump = stream_get_endp(s);
paul718e3742002-12-13 20:15:29 +0000545 stream_putc (s, 0);
Christian Frankefa713d92013-07-05 15:35:37 +0000546 /* Only non-recursive routes are elegible to resolve nexthop we
547 * are looking up. Therefore, we will just iterate over the top
548 * chain of nexthops. */
paul718e3742002-12-13 20:15:29 +0000549 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
Timo Teräs325823a2016-01-15 17:36:31 +0200550 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE))
paul718e3742002-12-13 20:15:29 +0000551 {
552 stream_putc (s, nexthop->type);
553 switch (nexthop->type)
554 {
555 case ZEBRA_NEXTHOP_IPV6:
556 stream_put (s, &nexthop->gate.ipv6, 16);
557 break;
558 case ZEBRA_NEXTHOP_IPV6_IFINDEX:
559 case ZEBRA_NEXTHOP_IPV6_IFNAME:
560 stream_put (s, &nexthop->gate.ipv6, 16);
561 stream_putl (s, nexthop->ifindex);
562 break;
563 case ZEBRA_NEXTHOP_IFINDEX:
564 case ZEBRA_NEXTHOP_IFNAME:
565 stream_putl (s, nexthop->ifindex);
566 break;
hassofa2b17e2004-03-04 17:45:00 +0000567 default:
568 /* do nothing */
569 break;
paul718e3742002-12-13 20:15:29 +0000570 }
571 num++;
572 }
573 stream_putc_at (s, nump, num);
574 }
575 else
576 {
577 stream_putl (s, 0);
578 stream_putc (s, 0);
579 }
580
581 stream_putw_at (s, 0, stream_get_endp (s));
582
ajs719e9742005-02-28 20:52:15 +0000583 return zebra_server_send_message(client);
paul718e3742002-12-13 20:15:29 +0000584}
585#endif /* HAVE_IPV6 */
586
paulb9df2d22004-05-09 09:09:59 +0000587static int
Feng Luc99f3482014-10-16 09:52:36 +0800588zsend_ipv4_nexthop_lookup (struct zserv *client, struct in_addr addr,
589 vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +0000590{
591 struct stream *s;
592 struct rib *rib;
593 unsigned long nump;
594 u_char num;
595 struct nexthop *nexthop;
596
David Lamparterf598cf72014-11-22 14:44:20 -0800597 /* Lookup nexthop - eBGP excluded */
Feng Luc99f3482014-10-16 09:52:36 +0800598 rib = rib_match_ipv4_safi (addr, SAFI_UNICAST, 1, NULL, vrf_id);
paul718e3742002-12-13 20:15:29 +0000599
600 /* Get output stream. */
601 s = client->obuf;
602 stream_reset (s);
603
604 /* Fill in result. */
Feng Luc99f3482014-10-16 09:52:36 +0800605 zserv_create_header (s, ZEBRA_IPV4_NEXTHOP_LOOKUP, vrf_id);
paul718e3742002-12-13 20:15:29 +0000606 stream_put_in_addr (s, &addr);
607
608 if (rib)
609 {
Christian Frankebb97e462013-05-25 14:01:35 +0000610 if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
611 zlog_debug("%s: Matching rib entry found.", __func__);
paul718e3742002-12-13 20:15:29 +0000612 stream_putl (s, rib->metric);
613 num = 0;
paul9985f832005-02-09 15:51:56 +0000614 nump = stream_get_endp(s);
paul718e3742002-12-13 20:15:29 +0000615 stream_putc (s, 0);
Christian Frankefa713d92013-07-05 15:35:37 +0000616 /* Only non-recursive routes are elegible to resolve the nexthop we
617 * are looking up. Therefore, we will just iterate over the top
618 * chain of nexthops. */
paul718e3742002-12-13 20:15:29 +0000619 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
Timo Teräs325823a2016-01-15 17:36:31 +0200620 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE))
paul718e3742002-12-13 20:15:29 +0000621 {
622 stream_putc (s, nexthop->type);
623 switch (nexthop->type)
624 {
625 case ZEBRA_NEXTHOP_IPV4:
626 stream_put_in_addr (s, &nexthop->gate.ipv4);
627 break;
Christian Frankebb97e462013-05-25 14:01:35 +0000628 case ZEBRA_NEXTHOP_IPV4_IFINDEX:
629 stream_put_in_addr (s, &nexthop->gate.ipv4);
630 stream_putl (s, nexthop->ifindex);
631 break;
paul718e3742002-12-13 20:15:29 +0000632 case ZEBRA_NEXTHOP_IFINDEX:
633 case ZEBRA_NEXTHOP_IFNAME:
634 stream_putl (s, nexthop->ifindex);
635 break;
hassofa2b17e2004-03-04 17:45:00 +0000636 default:
637 /* do nothing */
638 break;
paul718e3742002-12-13 20:15:29 +0000639 }
640 num++;
641 }
642 stream_putc_at (s, nump, num);
643 }
644 else
645 {
Christian Frankebb97e462013-05-25 14:01:35 +0000646 if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
647 zlog_debug("%s: No matching rib entry found.", __func__);
paul718e3742002-12-13 20:15:29 +0000648 stream_putl (s, 0);
649 stream_putc (s, 0);
650 }
651
652 stream_putw_at (s, 0, stream_get_endp (s));
653
ajs719e9742005-02-28 20:52:15 +0000654 return zebra_server_send_message(client);
paul718e3742002-12-13 20:15:29 +0000655}
656
Everton Marques4e5275b2014-07-01 15:15:52 -0300657/*
658 Modified version of zsend_ipv4_nexthop_lookup():
659 Query unicast rib if nexthop is not found on mrib.
660 Returns both route metric and protocol distance.
661*/
662static int
David Lamparterbd078122015-01-06 19:53:24 +0100663zsend_ipv4_nexthop_lookup_mrib (struct zserv *client, struct in_addr addr,
664 struct rib *rib)
Everton Marques4e5275b2014-07-01 15:15:52 -0300665{
666 struct stream *s;
Everton Marques4e5275b2014-07-01 15:15:52 -0300667 unsigned long nump;
668 u_char num;
669 struct nexthop *nexthop;
Everton Marques4e5275b2014-07-01 15:15:52 -0300670
671 /* Get output stream. */
672 s = client->obuf;
673 stream_reset (s);
674
675 /* Fill in result. */
Jafar Al-Gharaibeh190591f2016-04-21 17:40:12 -0500676 zserv_create_header (s, ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB,
677 rib ? rib->vrf_id : VRF_DEFAULT);
Everton Marques4e5275b2014-07-01 15:15:52 -0300678 stream_put_in_addr (s, &addr);
679
680 if (rib)
681 {
682 stream_putc (s, rib->distance);
683 stream_putl (s, rib->metric);
684 num = 0;
685 nump = stream_get_endp(s); /* remember position for nexthop_num */
686 stream_putc (s, 0); /* reserve room for nexthop_num */
687 /* Only non-recursive routes are elegible to resolve the nexthop we
688 * are looking up. Therefore, we will just iterate over the top
689 * chain of nexthops. */
690 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
Timo Teräs325823a2016-01-15 17:36:31 +0200691 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE))
Everton Marques4e5275b2014-07-01 15:15:52 -0300692 {
693 stream_putc (s, nexthop->type);
694 switch (nexthop->type)
695 {
696 case ZEBRA_NEXTHOP_IPV4:
697 stream_put_in_addr (s, &nexthop->gate.ipv4);
698 break;
699 case ZEBRA_NEXTHOP_IPV4_IFINDEX:
700 stream_put_in_addr (s, &nexthop->gate.ipv4);
701 stream_putl (s, nexthop->ifindex);
702 break;
703 case ZEBRA_NEXTHOP_IFINDEX:
704 case ZEBRA_NEXTHOP_IFNAME:
705 stream_putl (s, nexthop->ifindex);
706 break;
707 default:
708 /* do nothing */
709 break;
710 }
711 num++;
712 }
713
714 stream_putc_at (s, nump, num); /* store nexthop_num */
715 }
716 else
717 {
718 stream_putc (s, 0); /* distance */
719 stream_putl (s, 0); /* metric */
720 stream_putc (s, 0); /* nexthop_num */
721 }
722
723 stream_putw_at (s, 0, stream_get_endp (s));
724
725 return zebra_server_send_message(client);
726}
727
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -0500728/* Nexthop register */
729static int
730zserv_nexthop_register (struct zserv *client, int sock, u_short length, vrf_id_t vrf_id)
731{
732 struct rnh *rnh;
733 struct stream *s;
734 struct prefix p;
735 u_short l = 0;
736
737 if (IS_ZEBRA_DEBUG_NHT)
738 zlog_debug("nexthop_register msg from client %s: length=%d\n",
739 zebra_route_string(client->proto), length);
740
741 s = client->ibuf;
742
743 while (l < length)
744 {
745 p.family = stream_getw(s);
746 p.prefixlen = stream_getc(s);
747 l += 3;
748 stream_get(&p.u.prefix, s, PSIZE(p.prefixlen));
749 l += PSIZE(p.prefixlen);
750 rnh = zebra_add_rnh(&p, 0);
751 zebra_add_rnh_client(rnh, client, vrf_id);
752 }
753 zebra_evaluate_rnh_table(0, AF_INET);
754 zebra_evaluate_rnh_table(0, AF_INET6);
755 return 0;
756}
757
758/* Nexthop register */
759static int
760zserv_nexthop_unregister (struct zserv *client, int sock, u_short length)
761{
762 struct rnh *rnh;
763 struct stream *s;
764 struct prefix p;
765 u_short l = 0;
766
767 if (IS_ZEBRA_DEBUG_NHT)
768 zlog_debug("nexthop_unregister msg from client %s: length=%d\n",
769 zebra_route_string(client->proto), length);
770
771 s = client->ibuf;
772
773 while (l < length)
774 {
775 p.family = stream_getw(s);
776 p.prefixlen = stream_getc(s);
777 l += 3;
778 stream_get(&p.u.prefix, s, PSIZE(p.prefixlen));
779 l += PSIZE(p.prefixlen);
780 rnh = zebra_lookup_rnh(&p, 0);
781 if (rnh)
782 zebra_remove_rnh_client(rnh, client);
783 }
784 return 0;
785}
786
paulb9df2d22004-05-09 09:09:59 +0000787static int
Feng Luc99f3482014-10-16 09:52:36 +0800788zsend_ipv4_import_lookup (struct zserv *client, struct prefix_ipv4 *p,
789 vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +0000790{
791 struct stream *s;
792 struct rib *rib;
793 unsigned long nump;
794 u_char num;
795 struct nexthop *nexthop;
796
797 /* Lookup nexthop. */
Feng Luc99f3482014-10-16 09:52:36 +0800798 rib = rib_lookup_ipv4 (p, vrf_id);
paul718e3742002-12-13 20:15:29 +0000799
800 /* Get output stream. */
801 s = client->obuf;
802 stream_reset (s);
803
804 /* Fill in result. */
Feng Luc99f3482014-10-16 09:52:36 +0800805 zserv_create_header (s, ZEBRA_IPV4_IMPORT_LOOKUP, vrf_id);
paul718e3742002-12-13 20:15:29 +0000806 stream_put_in_addr (s, &p->prefix);
807
808 if (rib)
809 {
810 stream_putl (s, rib->metric);
811 num = 0;
paul9985f832005-02-09 15:51:56 +0000812 nump = stream_get_endp(s);
paul718e3742002-12-13 20:15:29 +0000813 stream_putc (s, 0);
814 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
Timo Teräs325823a2016-01-15 17:36:31 +0200815 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE))
paul718e3742002-12-13 20:15:29 +0000816 {
817 stream_putc (s, nexthop->type);
818 switch (nexthop->type)
819 {
820 case ZEBRA_NEXTHOP_IPV4:
821 stream_put_in_addr (s, &nexthop->gate.ipv4);
822 break;
Christian Frankea12afd52013-05-25 14:01:36 +0000823 case ZEBRA_NEXTHOP_IPV4_IFINDEX:
824 stream_put_in_addr (s, &nexthop->gate.ipv4);
825 stream_putl (s, nexthop->ifindex);
826 break;
paul718e3742002-12-13 20:15:29 +0000827 case ZEBRA_NEXTHOP_IFINDEX:
828 case ZEBRA_NEXTHOP_IFNAME:
829 stream_putl (s, nexthop->ifindex);
830 break;
hassofa2b17e2004-03-04 17:45:00 +0000831 default:
832 /* do nothing */
833 break;
paul718e3742002-12-13 20:15:29 +0000834 }
835 num++;
836 }
837 stream_putc_at (s, nump, num);
838 }
839 else
840 {
841 stream_putl (s, 0);
842 stream_putc (s, 0);
843 }
844
845 stream_putw_at (s, 0, stream_get_endp (s));
846
ajs719e9742005-02-28 20:52:15 +0000847 return zebra_server_send_message(client);
paul718e3742002-12-13 20:15:29 +0000848}
David Lamparter6b0655a2014-06-04 06:53:35 +0200849
hasso18a6dce2004-10-03 18:18:34 +0000850/* Router-id is updated. Send ZEBRA_ROUTER_ID_ADD to client. */
851int
Feng Luac19a442015-05-22 11:40:07 +0200852zsend_router_id_update (struct zserv *client, struct prefix *p,
853 vrf_id_t vrf_id)
hasso18a6dce2004-10-03 18:18:34 +0000854{
855 struct stream *s;
856 int blen;
857
858 /* Check this client need interface information. */
Feng Luc99f3482014-10-16 09:52:36 +0800859 if (! vrf_bitmap_check (client->ridinfo, vrf_id))
ajs719e9742005-02-28 20:52:15 +0000860 return 0;
hasso18a6dce2004-10-03 18:18:34 +0000861
862 s = client->obuf;
863 stream_reset (s);
864
hasso18a6dce2004-10-03 18:18:34 +0000865 /* Message type. */
Feng Luc99f3482014-10-16 09:52:36 +0800866 zserv_create_header (s, ZEBRA_ROUTER_ID_UPDATE, vrf_id);
hasso18a6dce2004-10-03 18:18:34 +0000867
868 /* Prefix information. */
869 stream_putc (s, p->family);
870 blen = prefix_blen (p);
871 stream_put (s, &p->u.prefix, blen);
872 stream_putc (s, p->prefixlen);
873
874 /* Write packet size. */
875 stream_putw_at (s, 0, stream_get_endp (s));
876
ajs719e9742005-02-28 20:52:15 +0000877 return zebra_server_send_message(client);
hasso18a6dce2004-10-03 18:18:34 +0000878}
David Lamparter6b0655a2014-06-04 06:53:35 +0200879
paul718e3742002-12-13 20:15:29 +0000880/* Register zebra server interface information. Send current all
881 interface and address information. */
ajs719e9742005-02-28 20:52:15 +0000882static int
Feng Luc99f3482014-10-16 09:52:36 +0800883zread_interface_add (struct zserv *client, u_short length, vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +0000884{
paul1eb8ef22005-04-07 07:30:20 +0000885 struct listnode *ifnode, *ifnnode;
886 struct listnode *cnode, *cnnode;
paul718e3742002-12-13 20:15:29 +0000887 struct interface *ifp;
888 struct connected *c;
889
890 /* Interface information is needed. */
Feng Luc99f3482014-10-16 09:52:36 +0800891 vrf_bitmap_set (client->ifinfo, vrf_id);
paul718e3742002-12-13 20:15:29 +0000892
Feng Luc99f3482014-10-16 09:52:36 +0800893 for (ALL_LIST_ELEMENTS (vrf_iflist (vrf_id), ifnode, ifnnode, ifp))
paul718e3742002-12-13 20:15:29 +0000894 {
paul718e3742002-12-13 20:15:29 +0000895 /* Skip pseudo interface. */
896 if (! CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE))
897 continue;
898
ajs719e9742005-02-28 20:52:15 +0000899 if (zsend_interface_add (client, ifp) < 0)
900 return -1;
paul718e3742002-12-13 20:15:29 +0000901
paul1eb8ef22005-04-07 07:30:20 +0000902 for (ALL_LIST_ELEMENTS (ifp->connected, cnode, cnnode, c))
paul718e3742002-12-13 20:15:29 +0000903 {
ajs719e9742005-02-28 20:52:15 +0000904 if (CHECK_FLAG (c->conf, ZEBRA_IFC_REAL) &&
905 (zsend_interface_address (ZEBRA_INTERFACE_ADDRESS_ADD, client,
906 ifp, c) < 0))
907 return -1;
paul718e3742002-12-13 20:15:29 +0000908 }
909 }
ajs719e9742005-02-28 20:52:15 +0000910 return 0;
paul718e3742002-12-13 20:15:29 +0000911}
912
913/* Unregister zebra server interface information. */
ajs719e9742005-02-28 20:52:15 +0000914static int
Feng Luc99f3482014-10-16 09:52:36 +0800915zread_interface_delete (struct zserv *client, u_short length, vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +0000916{
Feng Luc99f3482014-10-16 09:52:36 +0800917 vrf_bitmap_unset (client->ifinfo, vrf_id);
ajs719e9742005-02-28 20:52:15 +0000918 return 0;
paul718e3742002-12-13 20:15:29 +0000919}
920
921/* This function support multiple nexthop. */
paulb9df2d22004-05-09 09:09:59 +0000922/*
923 * Parse the ZEBRA_IPV4_ROUTE_ADD sent from client. Update rib and
924 * add kernel route.
925 */
ajs719e9742005-02-28 20:52:15 +0000926static int
Feng Luc99f3482014-10-16 09:52:36 +0800927zread_ipv4_add (struct zserv *client, u_short length, vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +0000928{
929 int i;
930 struct rib *rib;
931 struct prefix_ipv4 p;
932 u_char message;
933 struct in_addr nexthop;
934 u_char nexthop_num;
935 u_char nexthop_type;
936 struct stream *s;
Paul Jakma9099f9b2016-01-18 10:12:10 +0000937 ifindex_t ifindex;
paul718e3742002-12-13 20:15:29 +0000938 u_char ifname_len;
G.Balajicddf3912011-11-26 21:59:32 +0400939 safi_t safi;
940
paul718e3742002-12-13 20:15:29 +0000941
942 /* Get input stream. */
943 s = client->ibuf;
944
945 /* Allocate new rib. */
paul4d38fdb2005-04-28 17:35:14 +0000946 rib = XCALLOC (MTYPE_RIB, sizeof (struct rib));
947
paul718e3742002-12-13 20:15:29 +0000948 /* Type, flags, message. */
949 rib->type = stream_getc (s);
950 rib->flags = stream_getc (s);
paulb9df2d22004-05-09 09:09:59 +0000951 message = stream_getc (s);
G.Balajicddf3912011-11-26 21:59:32 +0400952 safi = stream_getw (s);
paul718e3742002-12-13 20:15:29 +0000953 rib->uptime = time (NULL);
954
955 /* IPv4 prefix. */
956 memset (&p, 0, sizeof (struct prefix_ipv4));
957 p.family = AF_INET;
958 p.prefixlen = stream_getc (s);
959 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
960
Feng Lu0d0686f2015-05-22 11:40:02 +0200961 /* VRF ID */
Feng Luc99f3482014-10-16 09:52:36 +0800962 rib->vrf_id = vrf_id;
Feng Lu0d0686f2015-05-22 11:40:02 +0200963
paul718e3742002-12-13 20:15:29 +0000964 /* Nexthop parse. */
965 if (CHECK_FLAG (message, ZAPI_MESSAGE_NEXTHOP))
966 {
967 nexthop_num = stream_getc (s);
968
969 for (i = 0; i < nexthop_num; i++)
970 {
971 nexthop_type = stream_getc (s);
972
973 switch (nexthop_type)
974 {
975 case ZEBRA_NEXTHOP_IFINDEX:
976 ifindex = stream_getl (s);
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -0500977 rib_nexthop_ifindex_add (rib, ifindex);
paul718e3742002-12-13 20:15:29 +0000978 break;
979 case ZEBRA_NEXTHOP_IFNAME:
980 ifname_len = stream_getc (s);
paul9985f832005-02-09 15:51:56 +0000981 stream_forward_getp (s, ifname_len);
paul718e3742002-12-13 20:15:29 +0000982 break;
983 case ZEBRA_NEXTHOP_IPV4:
984 nexthop.s_addr = stream_get_ipv4 (s);
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -0500985 rib_nexthop_ipv4_add (rib, &nexthop, NULL);
paul718e3742002-12-13 20:15:29 +0000986 break;
Joakim Tjernlundc963c202012-07-07 17:06:13 +0200987 case ZEBRA_NEXTHOP_IPV4_IFINDEX:
988 nexthop.s_addr = stream_get_ipv4 (s);
989 ifindex = stream_getl (s);
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -0500990 rib_nexthop_ipv4_ifindex_add (rib, &nexthop, NULL, ifindex);
Joakim Tjernlundc963c202012-07-07 17:06:13 +0200991 break;
paul718e3742002-12-13 20:15:29 +0000992 case ZEBRA_NEXTHOP_IPV6:
paul9985f832005-02-09 15:51:56 +0000993 stream_forward_getp (s, IPV6_MAX_BYTELEN);
paul718e3742002-12-13 20:15:29 +0000994 break;
Subbaiah Venkata6902c692012-03-27 19:21:29 -0700995 case ZEBRA_NEXTHOP_BLACKHOLE:
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -0500996 rib_nexthop_blackhole_add (rib);
Subbaiah Venkata6902c692012-03-27 19:21:29 -0700997 break;
998 }
paul718e3742002-12-13 20:15:29 +0000999 }
1000 }
1001
1002 /* Distance. */
1003 if (CHECK_FLAG (message, ZAPI_MESSAGE_DISTANCE))
1004 rib->distance = stream_getc (s);
1005
1006 /* Metric. */
1007 if (CHECK_FLAG (message, ZAPI_MESSAGE_METRIC))
1008 rib->metric = stream_getl (s);
1009
Timo Teräsb11f3b52015-11-02 16:50:07 +02001010 if (CHECK_FLAG (message, ZAPI_MESSAGE_MTU))
1011 rib->mtu = stream_getl (s);
Piotr Chytłaeefddcc2015-12-01 09:48:02 -05001012 /* Tag */
1013 if (CHECK_FLAG (message, ZAPI_MESSAGE_TAG))
1014 rib->tag = stream_getw (s);
Timo Teräsb11f3b52015-11-02 16:50:07 +02001015
Paul Jakma171eee32006-07-27 16:11:02 +00001016 /* Table */
1017 rib->table=zebrad.rtm_table_default;
G.Balajicddf3912011-11-26 21:59:32 +04001018 rib_add_ipv4_multipath (&p, rib, safi);
ajs719e9742005-02-28 20:52:15 +00001019 return 0;
paul718e3742002-12-13 20:15:29 +00001020}
1021
1022/* Zebra server IPv4 prefix delete function. */
ajs719e9742005-02-28 20:52:15 +00001023static int
Feng Luc99f3482014-10-16 09:52:36 +08001024zread_ipv4_delete (struct zserv *client, u_short length, vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +00001025{
1026 int i;
1027 struct stream *s;
1028 struct zapi_ipv4 api;
Subbaiah Venkata6902c692012-03-27 19:21:29 -07001029 struct in_addr nexthop, *nexthop_p;
paul718e3742002-12-13 20:15:29 +00001030 unsigned long ifindex;
1031 struct prefix_ipv4 p;
1032 u_char nexthop_num;
1033 u_char nexthop_type;
1034 u_char ifname_len;
1035
1036 s = client->ibuf;
1037 ifindex = 0;
1038 nexthop.s_addr = 0;
Subbaiah Venkata6902c692012-03-27 19:21:29 -07001039 nexthop_p = NULL;
paul718e3742002-12-13 20:15:29 +00001040
1041 /* Type, flags, message. */
1042 api.type = stream_getc (s);
1043 api.flags = stream_getc (s);
1044 api.message = stream_getc (s);
G.Balajicddf3912011-11-26 21:59:32 +04001045 api.safi = stream_getw (s);
paul718e3742002-12-13 20:15:29 +00001046
1047 /* IPv4 prefix. */
1048 memset (&p, 0, sizeof (struct prefix_ipv4));
1049 p.family = AF_INET;
1050 p.prefixlen = stream_getc (s);
1051 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
1052
1053 /* Nexthop, ifindex, distance, metric. */
1054 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
1055 {
1056 nexthop_num = stream_getc (s);
1057
1058 for (i = 0; i < nexthop_num; i++)
1059 {
1060 nexthop_type = stream_getc (s);
1061
1062 switch (nexthop_type)
1063 {
1064 case ZEBRA_NEXTHOP_IFINDEX:
1065 ifindex = stream_getl (s);
1066 break;
1067 case ZEBRA_NEXTHOP_IFNAME:
1068 ifname_len = stream_getc (s);
paul9985f832005-02-09 15:51:56 +00001069 stream_forward_getp (s, ifname_len);
paul718e3742002-12-13 20:15:29 +00001070 break;
1071 case ZEBRA_NEXTHOP_IPV4:
1072 nexthop.s_addr = stream_get_ipv4 (s);
Subbaiah Venkata6902c692012-03-27 19:21:29 -07001073 nexthop_p = &nexthop;
paul718e3742002-12-13 20:15:29 +00001074 break;
Joakim Tjernlundc963c202012-07-07 17:06:13 +02001075 case ZEBRA_NEXTHOP_IPV4_IFINDEX:
1076 nexthop.s_addr = stream_get_ipv4 (s);
Christian Franke23f5f7c2013-11-27 17:06:14 +00001077 nexthop_p = &nexthop;
Joakim Tjernlundc963c202012-07-07 17:06:13 +02001078 ifindex = stream_getl (s);
1079 break;
paul718e3742002-12-13 20:15:29 +00001080 case ZEBRA_NEXTHOP_IPV6:
paul9985f832005-02-09 15:51:56 +00001081 stream_forward_getp (s, IPV6_MAX_BYTELEN);
paul718e3742002-12-13 20:15:29 +00001082 break;
1083 }
1084 }
1085 }
1086
1087 /* Distance. */
1088 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
1089 api.distance = stream_getc (s);
1090 else
1091 api.distance = 0;
1092
1093 /* Metric. */
1094 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
1095 api.metric = stream_getl (s);
1096 else
1097 api.metric = 0;
1098
Piotr Chytłaeefddcc2015-12-01 09:48:02 -05001099 /* tag */
1100 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_TAG))
1101 api.tag = stream_getw (s);
1102 else
1103 api.tag = 0;
1104
Subbaiah Venkata6902c692012-03-27 19:21:29 -07001105 rib_delete_ipv4 (api.type, api.flags, &p, nexthop_p, ifindex,
Feng Luc99f3482014-10-16 09:52:36 +08001106 vrf_id, api.safi);
ajs719e9742005-02-28 20:52:15 +00001107 return 0;
paul718e3742002-12-13 20:15:29 +00001108}
1109
1110/* Nexthop lookup for IPv4. */
ajs719e9742005-02-28 20:52:15 +00001111static int
Feng Luc99f3482014-10-16 09:52:36 +08001112zread_ipv4_nexthop_lookup (struct zserv *client, u_short length,
1113 vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +00001114{
1115 struct in_addr addr;
Christian Frankebb97e462013-05-25 14:01:35 +00001116 char buf[BUFSIZ];
paul718e3742002-12-13 20:15:29 +00001117
1118 addr.s_addr = stream_get_ipv4 (client->ibuf);
Christian Frankebb97e462013-05-25 14:01:35 +00001119 if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
1120 zlog_debug("%s: looking up %s", __func__,
1121 inet_ntop (AF_INET, &addr, buf, BUFSIZ));
Feng Luc99f3482014-10-16 09:52:36 +08001122 return zsend_ipv4_nexthop_lookup (client, addr, vrf_id);
paul718e3742002-12-13 20:15:29 +00001123}
1124
Everton Marques4e5275b2014-07-01 15:15:52 -03001125/* MRIB Nexthop lookup for IPv4. */
1126static int
Feng Luc99f3482014-10-16 09:52:36 +08001127zread_ipv4_nexthop_lookup_mrib (struct zserv *client, u_short length,
1128 vrf_id_t vrf_id)
Everton Marques4e5275b2014-07-01 15:15:52 -03001129{
1130 struct in_addr addr;
David Lamparterbd078122015-01-06 19:53:24 +01001131 struct rib *rib;
Everton Marques4e5275b2014-07-01 15:15:52 -03001132
1133 addr.s_addr = stream_get_ipv4 (client->ibuf);
Feng Luc99f3482014-10-16 09:52:36 +08001134 rib = rib_match_ipv4_multicast (addr, NULL, vrf_id);
David Lamparterbd078122015-01-06 19:53:24 +01001135 return zsend_ipv4_nexthop_lookup_mrib (client, addr, rib);
Everton Marques4e5275b2014-07-01 15:15:52 -03001136}
1137
paul718e3742002-12-13 20:15:29 +00001138/* Nexthop lookup for IPv4. */
ajs719e9742005-02-28 20:52:15 +00001139static int
Feng Luc99f3482014-10-16 09:52:36 +08001140zread_ipv4_import_lookup (struct zserv *client, u_short length,
1141 vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +00001142{
1143 struct prefix_ipv4 p;
1144
1145 p.family = AF_INET;
1146 p.prefixlen = stream_getc (client->ibuf);
1147 p.prefix.s_addr = stream_get_ipv4 (client->ibuf);
1148
Feng Luc99f3482014-10-16 09:52:36 +08001149 return zsend_ipv4_import_lookup (client, &p, vrf_id);
paul718e3742002-12-13 20:15:29 +00001150}
1151
1152#ifdef HAVE_IPV6
1153/* Zebra server IPv6 prefix add function. */
ajs719e9742005-02-28 20:52:15 +00001154static int
Feng Luc99f3482014-10-16 09:52:36 +08001155zread_ipv6_add (struct zserv *client, u_short length, vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +00001156{
1157 int i;
1158 struct stream *s;
paul718e3742002-12-13 20:15:29 +00001159 struct in6_addr nexthop;
Ayan Banerjee34c5d892015-11-09 20:14:53 -05001160 struct rib *rib;
1161 u_char message;
1162 u_char gateway_num;
1163 u_char nexthop_type;
paul718e3742002-12-13 20:15:29 +00001164 struct prefix_ipv6 p;
Ayan Banerjee34c5d892015-11-09 20:14:53 -05001165 safi_t safi;
1166 static struct in6_addr nexthops[MULTIPATH_NUM];
1167 static unsigned int ifindices[MULTIPATH_NUM];
1168
1169 /* Get input stream. */
paul718e3742002-12-13 20:15:29 +00001170 s = client->ibuf;
Ayan Banerjee34c5d892015-11-09 20:14:53 -05001171
paul718e3742002-12-13 20:15:29 +00001172 memset (&nexthop, 0, sizeof (struct in6_addr));
1173
Ayan Banerjee34c5d892015-11-09 20:14:53 -05001174 /* Allocate new rib. */
1175 rib = XCALLOC (MTYPE_RIB, sizeof (struct rib));
paul718e3742002-12-13 20:15:29 +00001176
Ayan Banerjee34c5d892015-11-09 20:14:53 -05001177 /* Type, flags, message. */
1178 rib->type = stream_getc (s);
1179 rib->flags = stream_getc (s);
1180 message = stream_getc (s);
1181 safi = stream_getw (s);
1182 rib->uptime = time (NULL);
1183
1184 /* IPv6 prefix. */
paul718e3742002-12-13 20:15:29 +00001185 memset (&p, 0, sizeof (struct prefix_ipv6));
1186 p.family = AF_INET6;
1187 p.prefixlen = stream_getc (s);
1188 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
1189
Ayan Banerjee34c5d892015-11-09 20:14:53 -05001190 /* We need to give nh-addr, nh-ifindex with the same next-hop object
1191 * to the rib to ensure that IPv6 multipathing works; need to coalesce
1192 * these. Clients should send the same number of paired set of
1193 * next-hop-addr/next-hop-ifindices. */
1194 if (CHECK_FLAG (message, ZAPI_MESSAGE_NEXTHOP))
paul718e3742002-12-13 20:15:29 +00001195 {
Ayan Banerjee34c5d892015-11-09 20:14:53 -05001196 int nh_count = 0;
1197 int if_count = 0;
1198 int max_nh_if = 0;
1199 unsigned int ifindex;
paul718e3742002-12-13 20:15:29 +00001200
Ayan Banerjee34c5d892015-11-09 20:14:53 -05001201 gateway_num = stream_getc (s);
1202 for (i = 0; i < gateway_num; i++)
paul718e3742002-12-13 20:15:29 +00001203 {
1204 nexthop_type = stream_getc (s);
1205
1206 switch (nexthop_type)
1207 {
1208 case ZEBRA_NEXTHOP_IPV6:
1209 stream_get (&nexthop, s, 16);
Ayan Banerjee34c5d892015-11-09 20:14:53 -05001210 if (nh_count < MULTIPATH_NUM) {
1211 nexthops[nh_count++] = nexthop;
1212 }
paul718e3742002-12-13 20:15:29 +00001213 break;
1214 case ZEBRA_NEXTHOP_IFINDEX:
1215 ifindex = stream_getl (s);
Ayan Banerjee34c5d892015-11-09 20:14:53 -05001216 if (if_count < MULTIPATH_NUM) {
1217 ifindices[if_count++] = ifindex;
1218 }
paul718e3742002-12-13 20:15:29 +00001219 break;
1220 }
1221 }
Ayan Banerjee34c5d892015-11-09 20:14:53 -05001222
1223 max_nh_if = (nh_count > if_count) ? nh_count : if_count;
1224 for (i = 0; i < max_nh_if; i++)
1225 {
1226 if ((i < nh_count) && !IN6_IS_ADDR_UNSPECIFIED (&nexthops[i]))
1227 {
1228 if ((i < if_count) && ifindices[i])
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -05001229 rib_nexthop_ipv6_ifindex_add (rib, &nexthops[i], ifindices[i]);
Ayan Banerjee34c5d892015-11-09 20:14:53 -05001230 else
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -05001231 rib_nexthop_ipv6_add (rib, &nexthops[i]);
Ayan Banerjee34c5d892015-11-09 20:14:53 -05001232 }
1233 else
1234 {
1235 if ((i < if_count) && ifindices[i])
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -05001236 rib_nexthop_ifindex_add (rib, ifindices[i]);
Ayan Banerjee34c5d892015-11-09 20:14:53 -05001237 }
1238 }
paul718e3742002-12-13 20:15:29 +00001239 }
1240
Ayan Banerjee34c5d892015-11-09 20:14:53 -05001241 /* Distance. */
1242 if (CHECK_FLAG (message, ZAPI_MESSAGE_DISTANCE))
1243 rib->distance = stream_getc (s);
paul718e3742002-12-13 20:15:29 +00001244
Ayan Banerjee34c5d892015-11-09 20:14:53 -05001245 /* Metric. */
1246 if (CHECK_FLAG (message, ZAPI_MESSAGE_METRIC))
1247 rib->metric = stream_getl (s);
Timo Teräsb11f3b52015-11-02 16:50:07 +02001248
Ayan Banerjee34c5d892015-11-09 20:14:53 -05001249 if (CHECK_FLAG (message, ZAPI_MESSAGE_MTU))
1250 rib->mtu = stream_getl (s);
1251
Piotr Chytłaeefddcc2015-12-01 09:48:02 -05001252 /* Tag */
1253 if (CHECK_FLAG (message, ZAPI_MESSAGE_TAG))
1254 rib->tag = stream_getw (s);
1255
Ayan Banerjee34c5d892015-11-09 20:14:53 -05001256 /* Table */
1257 rib->table=zebrad.rtm_table_default;
1258 rib_add_ipv6_multipath (&p, rib, safi);
ajs719e9742005-02-28 20:52:15 +00001259 return 0;
paul718e3742002-12-13 20:15:29 +00001260}
1261
1262/* Zebra server IPv6 prefix delete function. */
ajs719e9742005-02-28 20:52:15 +00001263static int
Feng Luc99f3482014-10-16 09:52:36 +08001264zread_ipv6_delete (struct zserv *client, u_short length, vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +00001265{
1266 int i;
1267 struct stream *s;
1268 struct zapi_ipv6 api;
1269 struct in6_addr nexthop;
1270 unsigned long ifindex;
1271 struct prefix_ipv6 p;
1272
1273 s = client->ibuf;
1274 ifindex = 0;
1275 memset (&nexthop, 0, sizeof (struct in6_addr));
1276
1277 /* Type, flags, message. */
1278 api.type = stream_getc (s);
1279 api.flags = stream_getc (s);
1280 api.message = stream_getc (s);
G.Balajif768f362011-11-26 22:10:39 +04001281 api.safi = stream_getw (s);
paul718e3742002-12-13 20:15:29 +00001282
1283 /* IPv4 prefix. */
1284 memset (&p, 0, sizeof (struct prefix_ipv6));
1285 p.family = AF_INET6;
1286 p.prefixlen = stream_getc (s);
1287 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
1288
1289 /* Nexthop, ifindex, distance, metric. */
1290 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
1291 {
1292 u_char nexthop_type;
1293
1294 api.nexthop_num = stream_getc (s);
1295 for (i = 0; i < api.nexthop_num; i++)
1296 {
1297 nexthop_type = stream_getc (s);
1298
1299 switch (nexthop_type)
1300 {
1301 case ZEBRA_NEXTHOP_IPV6:
1302 stream_get (&nexthop, s, 16);
1303 break;
1304 case ZEBRA_NEXTHOP_IFINDEX:
1305 ifindex = stream_getl (s);
1306 break;
1307 }
1308 }
1309 }
1310
Piotr Chytłaeefddcc2015-12-01 09:48:02 -05001311 /* Distance. */
paul718e3742002-12-13 20:15:29 +00001312 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
1313 api.distance = stream_getc (s);
1314 else
1315 api.distance = 0;
Piotr Chytłaeefddcc2015-12-01 09:48:02 -05001316
1317 /* Metric. */
paul718e3742002-12-13 20:15:29 +00001318 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
1319 api.metric = stream_getl (s);
1320 else
1321 api.metric = 0;
1322
Piotr Chytłaeefddcc2015-12-01 09:48:02 -05001323 /* tag */
1324 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_TAG))
1325 api.tag = stream_getw (s);
1326 else
1327 api.tag = 0;
1328
paul718e3742002-12-13 20:15:29 +00001329 if (IN6_IS_ADDR_UNSPECIFIED (&nexthop))
Feng Luc99f3482014-10-16 09:52:36 +08001330 rib_delete_ipv6 (api.type, api.flags, &p, NULL, ifindex, vrf_id,
Feng Lu0d0686f2015-05-22 11:40:02 +02001331 api.safi);
paul718e3742002-12-13 20:15:29 +00001332 else
Feng Luc99f3482014-10-16 09:52:36 +08001333 rib_delete_ipv6 (api.type, api.flags, &p, &nexthop, ifindex, vrf_id,
Feng Lu0d0686f2015-05-22 11:40:02 +02001334 api.safi);
ajs719e9742005-02-28 20:52:15 +00001335 return 0;
paul718e3742002-12-13 20:15:29 +00001336}
1337
ajs719e9742005-02-28 20:52:15 +00001338static int
Feng Luc99f3482014-10-16 09:52:36 +08001339zread_ipv6_nexthop_lookup (struct zserv *client, u_short length,
1340 vrf_id_t vrf_id)
paul718e3742002-12-13 20:15:29 +00001341{
1342 struct in6_addr addr;
1343 char buf[BUFSIZ];
1344
1345 stream_get (&addr, client->ibuf, 16);
Christian Frankea5207082013-04-11 08:24:29 +00001346 if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
1347 zlog_debug("%s: looking up %s", __func__,
1348 inet_ntop (AF_INET6, &addr, buf, BUFSIZ));
paul718e3742002-12-13 20:15:29 +00001349
Feng Luc99f3482014-10-16 09:52:36 +08001350 return zsend_ipv6_nexthop_lookup (client, &addr, vrf_id);
paul718e3742002-12-13 20:15:29 +00001351}
1352#endif /* HAVE_IPV6 */
1353
hasso18a6dce2004-10-03 18:18:34 +00001354/* Register zebra server router-id information. Send current router-id */
ajs719e9742005-02-28 20:52:15 +00001355static int
Feng Luc99f3482014-10-16 09:52:36 +08001356zread_router_id_add (struct zserv *client, u_short length, vrf_id_t vrf_id)
hasso18a6dce2004-10-03 18:18:34 +00001357{
1358 struct prefix p;
1359
1360 /* Router-id information is needed. */
Feng Luc99f3482014-10-16 09:52:36 +08001361 vrf_bitmap_set (client->ridinfo, vrf_id);
hasso18a6dce2004-10-03 18:18:34 +00001362
Feng Luc99f3482014-10-16 09:52:36 +08001363 router_id_get (&p, vrf_id);
hasso18a6dce2004-10-03 18:18:34 +00001364
Feng Luc99f3482014-10-16 09:52:36 +08001365 return zsend_router_id_update (client, &p, vrf_id);
hasso18a6dce2004-10-03 18:18:34 +00001366}
1367
1368/* Unregister zebra server router-id information. */
ajs719e9742005-02-28 20:52:15 +00001369static int
Feng Luc99f3482014-10-16 09:52:36 +08001370zread_router_id_delete (struct zserv *client, u_short length, vrf_id_t vrf_id)
hasso18a6dce2004-10-03 18:18:34 +00001371{
Feng Luc99f3482014-10-16 09:52:36 +08001372 vrf_bitmap_unset (client->ridinfo, vrf_id);
ajs719e9742005-02-28 20:52:15 +00001373 return 0;
hasso18a6dce2004-10-03 18:18:34 +00001374}
1375
Vyacheslav Trushkin2ea1ab12011-12-11 18:48:47 +04001376/* Tie up route-type and client->sock */
1377static void
1378zread_hello (struct zserv *client)
1379{
1380 /* type of protocol (lib/zebra.h) */
1381 u_char proto;
1382 proto = stream_getc (client->ibuf);
1383
1384 /* accept only dynamic routing protocols */
1385 if ((proto < ZEBRA_ROUTE_MAX)
1386 && (proto > ZEBRA_ROUTE_STATIC))
1387 {
1388 zlog_notice ("client %d says hello and bids fair to announce only %s routes",
1389 client->sock, zebra_route_string(proto));
1390
1391 /* if route-type was binded by other client */
1392 if (route_type_oaths[proto])
1393 zlog_warn ("sender of %s routes changed %c->%c",
1394 zebra_route_string(proto), route_type_oaths[proto],
1395 client->sock);
1396
1397 route_type_oaths[proto] = client->sock;
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -05001398 client->proto = proto;
Vyacheslav Trushkin2ea1ab12011-12-11 18:48:47 +04001399 }
1400}
1401
Feng Luc99f3482014-10-16 09:52:36 +08001402/* Unregister all information in a VRF. */
1403static int
1404zread_vrf_unregister (struct zserv *client, u_short length, vrf_id_t vrf_id)
1405{
1406 int i;
1407
1408 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
1409 vrf_bitmap_unset (client->redist[i], vrf_id);
1410 vrf_bitmap_unset (client->redist_default, vrf_id);
1411 vrf_bitmap_unset (client->ifinfo, vrf_id);
1412 vrf_bitmap_unset (client->ridinfo, vrf_id);
1413
1414 return 0;
1415}
1416
Vyacheslav Trushkin2ea1ab12011-12-11 18:48:47 +04001417/* If client sent routes of specific type, zebra removes it
1418 * and returns number of deleted routes.
1419 */
1420static void
1421zebra_score_rib (int client_sock)
1422{
1423 int i;
1424
1425 for (i = ZEBRA_ROUTE_RIP; i < ZEBRA_ROUTE_MAX; i++)
1426 if (client_sock == route_type_oaths[i])
1427 {
1428 zlog_notice ("client %d disconnected. %lu %s routes removed from the rib",
1429 client_sock, rib_score_proto (i), zebra_route_string (i));
1430 route_type_oaths[i] = 0;
1431 break;
1432 }
1433}
1434
paul718e3742002-12-13 20:15:29 +00001435/* Close zebra client. */
paulb9df2d22004-05-09 09:09:59 +00001436static void
paul718e3742002-12-13 20:15:29 +00001437zebra_client_close (struct zserv *client)
1438{
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -05001439 zebra_cleanup_rnh_client(0, AF_INET, client);
1440 zebra_cleanup_rnh_client(0, AF_INET6, client);
1441
paul718e3742002-12-13 20:15:29 +00001442 /* Close file descriptor. */
1443 if (client->sock)
1444 {
1445 close (client->sock);
Vyacheslav Trushkin2ea1ab12011-12-11 18:48:47 +04001446 zebra_score_rib (client->sock);
paul718e3742002-12-13 20:15:29 +00001447 client->sock = -1;
1448 }
1449
1450 /* Free stream buffers. */
1451 if (client->ibuf)
1452 stream_free (client->ibuf);
1453 if (client->obuf)
1454 stream_free (client->obuf);
ajs719e9742005-02-28 20:52:15 +00001455 if (client->wb)
1456 buffer_free(client->wb);
paul718e3742002-12-13 20:15:29 +00001457
1458 /* Release threads. */
1459 if (client->t_read)
1460 thread_cancel (client->t_read);
1461 if (client->t_write)
1462 thread_cancel (client->t_write);
ajs719e9742005-02-28 20:52:15 +00001463 if (client->t_suicide)
1464 thread_cancel (client->t_suicide);
paul718e3742002-12-13 20:15:29 +00001465
1466 /* Free client structure. */
paulb21b19c2003-06-15 01:28:29 +00001467 listnode_delete (zebrad.client_list, client);
paul718e3742002-12-13 20:15:29 +00001468 XFREE (0, client);
1469}
1470
1471/* Make new client. */
paulb9df2d22004-05-09 09:09:59 +00001472static void
paul718e3742002-12-13 20:15:29 +00001473zebra_client_create (int sock)
1474{
1475 struct zserv *client;
Feng Luc99f3482014-10-16 09:52:36 +08001476 int i;
paul718e3742002-12-13 20:15:29 +00001477
David Lamparter23757db2016-02-24 06:26:02 +01001478 client = XCALLOC (MTYPE_TMP, sizeof (struct zserv));
paul718e3742002-12-13 20:15:29 +00001479
1480 /* Make client input/output buffer. */
1481 client->sock = sock;
1482 client->ibuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
1483 client->obuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
ajs719e9742005-02-28 20:52:15 +00001484 client->wb = buffer_new(0);
paul718e3742002-12-13 20:15:29 +00001485
1486 /* Set table number. */
paulb21b19c2003-06-15 01:28:29 +00001487 client->rtm_table = zebrad.rtm_table_default;
paul718e3742002-12-13 20:15:29 +00001488
Feng Luc99f3482014-10-16 09:52:36 +08001489 /* Initialize flags */
1490 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
1491 client->redist[i] = vrf_bitmap_init ();
1492 client->redist_default = vrf_bitmap_init ();
1493 client->ifinfo = vrf_bitmap_init ();
1494 client->ridinfo = vrf_bitmap_init ();
1495
paul718e3742002-12-13 20:15:29 +00001496 /* Add this client to linked list. */
paulb21b19c2003-06-15 01:28:29 +00001497 listnode_add (zebrad.client_list, client);
paul718e3742002-12-13 20:15:29 +00001498
1499 /* Make new read thread. */
1500 zebra_event (ZEBRA_READ, sock, client);
1501}
1502
1503/* Handler of zebra service request. */
paulb9df2d22004-05-09 09:09:59 +00001504static int
paul718e3742002-12-13 20:15:29 +00001505zebra_client_read (struct thread *thread)
1506{
1507 int sock;
1508 struct zserv *client;
ajs57a14772005-04-10 15:01:56 +00001509 size_t already;
paulc1b98002006-01-16 01:54:02 +00001510 uint16_t length, command;
1511 uint8_t marker, version;
Feng Luc99f3482014-10-16 09:52:36 +08001512 vrf_id_t vrf_id;
paul718e3742002-12-13 20:15:29 +00001513
1514 /* Get thread data. Reset reading thread because I'm running. */
1515 sock = THREAD_FD (thread);
1516 client = THREAD_ARG (thread);
1517 client->t_read = NULL;
1518
ajs719e9742005-02-28 20:52:15 +00001519 if (client->t_suicide)
paul718e3742002-12-13 20:15:29 +00001520 {
ajs719e9742005-02-28 20:52:15 +00001521 zebra_client_close(client);
paul718e3742002-12-13 20:15:29 +00001522 return -1;
1523 }
ajs719e9742005-02-28 20:52:15 +00001524
1525 /* Read length and command (if we don't have it already). */
ajs57a14772005-04-10 15:01:56 +00001526 if ((already = stream_get_endp(client->ibuf)) < ZEBRA_HEADER_SIZE)
ajs719e9742005-02-28 20:52:15 +00001527 {
ajs57a14772005-04-10 15:01:56 +00001528 ssize_t nbyte;
ajs719e9742005-02-28 20:52:15 +00001529 if (((nbyte = stream_read_try (client->ibuf, sock,
ajs57a14772005-04-10 15:01:56 +00001530 ZEBRA_HEADER_SIZE-already)) == 0) ||
ajs719e9742005-02-28 20:52:15 +00001531 (nbyte == -1))
1532 {
1533 if (IS_ZEBRA_DEBUG_EVENT)
1534 zlog_debug ("connection closed socket [%d]", sock);
1535 zebra_client_close (client);
1536 return -1;
1537 }
ajs57a14772005-04-10 15:01:56 +00001538 if (nbyte != (ssize_t)(ZEBRA_HEADER_SIZE-already))
ajs719e9742005-02-28 20:52:15 +00001539 {
1540 /* Try again later. */
1541 zebra_event (ZEBRA_READ, sock, client);
1542 return 0;
1543 }
ajs57a14772005-04-10 15:01:56 +00001544 already = ZEBRA_HEADER_SIZE;
ajs719e9742005-02-28 20:52:15 +00001545 }
1546
1547 /* Reset to read from the beginning of the incoming packet. */
1548 stream_set_getp(client->ibuf, 0);
1549
paulc1b98002006-01-16 01:54:02 +00001550 /* Fetch header values */
paul718e3742002-12-13 20:15:29 +00001551 length = stream_getw (client->ibuf);
paulc1b98002006-01-16 01:54:02 +00001552 marker = stream_getc (client->ibuf);
1553 version = stream_getc (client->ibuf);
Feng Luc99f3482014-10-16 09:52:36 +08001554 vrf_id = stream_getw (client->ibuf);
paulc1b98002006-01-16 01:54:02 +00001555 command = stream_getw (client->ibuf);
paul718e3742002-12-13 20:15:29 +00001556
paulc1b98002006-01-16 01:54:02 +00001557 if (marker != ZEBRA_HEADER_MARKER || version != ZSERV_VERSION)
1558 {
1559 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
1560 __func__, sock, marker, version);
1561 zebra_client_close (client);
1562 return -1;
1563 }
ajs719e9742005-02-28 20:52:15 +00001564 if (length < ZEBRA_HEADER_SIZE)
paul718e3742002-12-13 20:15:29 +00001565 {
ajs57a14772005-04-10 15:01:56 +00001566 zlog_warn("%s: socket %d message length %u is less than header size %d",
1567 __func__, sock, length, ZEBRA_HEADER_SIZE);
1568 zebra_client_close (client);
1569 return -1;
1570 }
1571 if (length > STREAM_SIZE(client->ibuf))
1572 {
1573 zlog_warn("%s: socket %d message length %u exceeds buffer size %lu",
1574 __func__, sock, length, (u_long)STREAM_SIZE(client->ibuf));
paul718e3742002-12-13 20:15:29 +00001575 zebra_client_close (client);
1576 return -1;
1577 }
1578
paul718e3742002-12-13 20:15:29 +00001579 /* Read rest of data. */
ajs57a14772005-04-10 15:01:56 +00001580 if (already < length)
paul718e3742002-12-13 20:15:29 +00001581 {
ajs57a14772005-04-10 15:01:56 +00001582 ssize_t nbyte;
1583 if (((nbyte = stream_read_try (client->ibuf, sock,
1584 length-already)) == 0) ||
1585 (nbyte == -1))
paul718e3742002-12-13 20:15:29 +00001586 {
1587 if (IS_ZEBRA_DEBUG_EVENT)
ajsb6178002004-12-07 21:12:56 +00001588 zlog_debug ("connection closed [%d] when reading zebra data", sock);
paul718e3742002-12-13 20:15:29 +00001589 zebra_client_close (client);
1590 return -1;
1591 }
ajs57a14772005-04-10 15:01:56 +00001592 if (nbyte != (ssize_t)(length-already))
ajs719e9742005-02-28 20:52:15 +00001593 {
1594 /* Try again later. */
1595 zebra_event (ZEBRA_READ, sock, client);
1596 return 0;
1597 }
paul718e3742002-12-13 20:15:29 +00001598 }
1599
ajs719e9742005-02-28 20:52:15 +00001600 length -= ZEBRA_HEADER_SIZE;
1601
paul718e3742002-12-13 20:15:29 +00001602 /* Debug packet information. */
1603 if (IS_ZEBRA_DEBUG_EVENT)
ajsb6178002004-12-07 21:12:56 +00001604 zlog_debug ("zebra message comes from socket [%d]", sock);
paul718e3742002-12-13 20:15:29 +00001605
1606 if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
Feng Luc99f3482014-10-16 09:52:36 +08001607 zlog_debug ("zebra message received [%s] %d in VRF %u",
1608 zserv_command_string (command), length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001609
1610 switch (command)
1611 {
hasso18a6dce2004-10-03 18:18:34 +00001612 case ZEBRA_ROUTER_ID_ADD:
Feng Luc99f3482014-10-16 09:52:36 +08001613 zread_router_id_add (client, length, vrf_id);
hasso18a6dce2004-10-03 18:18:34 +00001614 break;
1615 case ZEBRA_ROUTER_ID_DELETE:
Feng Luc99f3482014-10-16 09:52:36 +08001616 zread_router_id_delete (client, length, vrf_id);
hasso18a6dce2004-10-03 18:18:34 +00001617 break;
paul718e3742002-12-13 20:15:29 +00001618 case ZEBRA_INTERFACE_ADD:
Feng Luc99f3482014-10-16 09:52:36 +08001619 zread_interface_add (client, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001620 break;
1621 case ZEBRA_INTERFACE_DELETE:
Feng Luc99f3482014-10-16 09:52:36 +08001622 zread_interface_delete (client, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001623 break;
1624 case ZEBRA_IPV4_ROUTE_ADD:
Feng Luc99f3482014-10-16 09:52:36 +08001625 zread_ipv4_add (client, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001626 break;
1627 case ZEBRA_IPV4_ROUTE_DELETE:
Feng Luc99f3482014-10-16 09:52:36 +08001628 zread_ipv4_delete (client, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001629 break;
1630#ifdef HAVE_IPV6
1631 case ZEBRA_IPV6_ROUTE_ADD:
Feng Luc99f3482014-10-16 09:52:36 +08001632 zread_ipv6_add (client, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001633 break;
1634 case ZEBRA_IPV6_ROUTE_DELETE:
Feng Luc99f3482014-10-16 09:52:36 +08001635 zread_ipv6_delete (client, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001636 break;
1637#endif /* HAVE_IPV6 */
1638 case ZEBRA_REDISTRIBUTE_ADD:
Feng Luc99f3482014-10-16 09:52:36 +08001639 zebra_redistribute_add (command, client, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001640 break;
1641 case ZEBRA_REDISTRIBUTE_DELETE:
Feng Luc99f3482014-10-16 09:52:36 +08001642 zebra_redistribute_delete (command, client, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001643 break;
1644 case ZEBRA_REDISTRIBUTE_DEFAULT_ADD:
Feng Luc99f3482014-10-16 09:52:36 +08001645 zebra_redistribute_default_add (command, client, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001646 break;
1647 case ZEBRA_REDISTRIBUTE_DEFAULT_DELETE:
Feng Luc99f3482014-10-16 09:52:36 +08001648 zebra_redistribute_default_delete (command, client, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001649 break;
1650 case ZEBRA_IPV4_NEXTHOP_LOOKUP:
Feng Luc99f3482014-10-16 09:52:36 +08001651 zread_ipv4_nexthop_lookup (client, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001652 break;
Everton Marques4e5275b2014-07-01 15:15:52 -03001653 case ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB:
Feng Luc99f3482014-10-16 09:52:36 +08001654 zread_ipv4_nexthop_lookup_mrib (client, length, vrf_id);
Everton Marques4e5275b2014-07-01 15:15:52 -03001655 break;
paul718e3742002-12-13 20:15:29 +00001656#ifdef HAVE_IPV6
1657 case ZEBRA_IPV6_NEXTHOP_LOOKUP:
Feng Luc99f3482014-10-16 09:52:36 +08001658 zread_ipv6_nexthop_lookup (client, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001659 break;
1660#endif /* HAVE_IPV6 */
1661 case ZEBRA_IPV4_IMPORT_LOOKUP:
Feng Luc99f3482014-10-16 09:52:36 +08001662 zread_ipv4_import_lookup (client, length, vrf_id);
paul718e3742002-12-13 20:15:29 +00001663 break;
Vyacheslav Trushkin2ea1ab12011-12-11 18:48:47 +04001664 case ZEBRA_HELLO:
1665 zread_hello (client);
1666 break;
Feng Luc99f3482014-10-16 09:52:36 +08001667 case ZEBRA_VRF_UNREGISTER:
1668 zread_vrf_unregister (client, length, vrf_id);
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -05001669 case ZEBRA_NEXTHOP_REGISTER:
1670 zserv_nexthop_register(client, sock, length, vrf_id);
1671 break;
1672 case ZEBRA_NEXTHOP_UNREGISTER:
1673 zserv_nexthop_unregister(client, sock, length);
Feng Luc99f3482014-10-16 09:52:36 +08001674 break;
paul718e3742002-12-13 20:15:29 +00001675 default:
1676 zlog_info ("Zebra received unknown command %d", command);
1677 break;
1678 }
1679
ajs719e9742005-02-28 20:52:15 +00001680 if (client->t_suicide)
1681 {
1682 /* No need to wait for thread callback, just kill immediately. */
1683 zebra_client_close(client);
1684 return -1;
1685 }
1686
paul718e3742002-12-13 20:15:29 +00001687 stream_reset (client->ibuf);
1688 zebra_event (ZEBRA_READ, sock, client);
paul718e3742002-12-13 20:15:29 +00001689 return 0;
1690}
1691
paul718e3742002-12-13 20:15:29 +00001692
1693/* Accept code of zebra server socket. */
paulb9df2d22004-05-09 09:09:59 +00001694static int
paul718e3742002-12-13 20:15:29 +00001695zebra_accept (struct thread *thread)
1696{
1697 int accept_sock;
1698 int client_sock;
1699 struct sockaddr_in client;
1700 socklen_t len;
1701
1702 accept_sock = THREAD_FD (thread);
1703
ajs719e9742005-02-28 20:52:15 +00001704 /* Reregister myself. */
1705 zebra_event (ZEBRA_SERV, accept_sock, NULL);
1706
paul718e3742002-12-13 20:15:29 +00001707 len = sizeof (struct sockaddr_in);
1708 client_sock = accept (accept_sock, (struct sockaddr *) &client, &len);
1709
1710 if (client_sock < 0)
1711 {
ajs6099b3b2004-11-20 02:06:59 +00001712 zlog_warn ("Can't accept zebra socket: %s", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001713 return -1;
1714 }
1715
paulccf35572003-03-01 11:42:20 +00001716 /* Make client socket non-blocking. */
ajs719e9742005-02-28 20:52:15 +00001717 set_nonblocking(client_sock);
paul865b8522005-01-05 08:30:35 +00001718
paul718e3742002-12-13 20:15:29 +00001719 /* Create new zebra client. */
1720 zebra_client_create (client_sock);
1721
paul718e3742002-12-13 20:15:29 +00001722 return 0;
1723}
1724
paulb9df2d22004-05-09 09:09:59 +00001725#ifdef HAVE_TCP_ZEBRA
paul718e3742002-12-13 20:15:29 +00001726/* Make zebra's server socket. */
paulb9df2d22004-05-09 09:09:59 +00001727static void
paul718e3742002-12-13 20:15:29 +00001728zebra_serv ()
1729{
1730 int ret;
1731 int accept_sock;
1732 struct sockaddr_in addr;
1733
1734 accept_sock = socket (AF_INET, SOCK_STREAM, 0);
1735
1736 if (accept_sock < 0)
1737 {
paul3d1dc852005-04-05 00:45:23 +00001738 zlog_warn ("Can't create zserv stream socket: %s",
1739 safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001740 zlog_warn ("zebra can't provice full functionality due to above error");
1741 return;
1742 }
1743
Vyacheslav Trushkin2ea1ab12011-12-11 18:48:47 +04001744 memset (&route_type_oaths, 0, sizeof (route_type_oaths));
paul718e3742002-12-13 20:15:29 +00001745 memset (&addr, 0, sizeof (struct sockaddr_in));
1746 addr.sin_family = AF_INET;
1747 addr.sin_port = htons (ZEBRA_PORT);
Paul Jakma6f0e3f62007-05-10 02:38:51 +00001748#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
paul718e3742002-12-13 20:15:29 +00001749 addr.sin_len = sizeof (struct sockaddr_in);
Paul Jakma6f0e3f62007-05-10 02:38:51 +00001750#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
paul718e3742002-12-13 20:15:29 +00001751 addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
1752
1753 sockopt_reuseaddr (accept_sock);
1754 sockopt_reuseport (accept_sock);
1755
pauledd7c242003-06-04 13:59:38 +00001756 if ( zserv_privs.change(ZPRIVS_RAISE) )
1757 zlog (NULL, LOG_ERR, "Can't raise privileges");
1758
paul718e3742002-12-13 20:15:29 +00001759 ret = bind (accept_sock, (struct sockaddr *)&addr,
1760 sizeof (struct sockaddr_in));
1761 if (ret < 0)
1762 {
paul3d1dc852005-04-05 00:45:23 +00001763 zlog_warn ("Can't bind to stream socket: %s",
1764 safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001765 zlog_warn ("zebra can't provice full functionality due to above error");
1766 close (accept_sock); /* Avoid sd leak. */
1767 return;
1768 }
pauledd7c242003-06-04 13:59:38 +00001769
1770 if ( zserv_privs.change(ZPRIVS_LOWER) )
1771 zlog (NULL, LOG_ERR, "Can't lower privileges");
paul718e3742002-12-13 20:15:29 +00001772
1773 ret = listen (accept_sock, 1);
1774 if (ret < 0)
1775 {
paul3d1dc852005-04-05 00:45:23 +00001776 zlog_warn ("Can't listen to stream socket: %s",
1777 safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001778 zlog_warn ("zebra can't provice full functionality due to above error");
1779 close (accept_sock); /* Avoid sd leak. */
1780 return;
1781 }
1782
1783 zebra_event (ZEBRA_SERV, accept_sock, NULL);
1784}
David Lamparter4b6c3322015-04-21 09:47:57 +02001785#else /* HAVE_TCP_ZEBRA */
paul718e3742002-12-13 20:15:29 +00001786
1787/* For sockaddr_un. */
1788#include <sys/un.h>
1789
1790/* zebra server UNIX domain socket. */
paulb9df2d22004-05-09 09:09:59 +00001791static void
hassofce954f2004-10-07 20:29:24 +00001792zebra_serv_un (const char *path)
paul718e3742002-12-13 20:15:29 +00001793{
1794 int ret;
1795 int sock, len;
1796 struct sockaddr_un serv;
1797 mode_t old_mask;
1798
1799 /* First of all, unlink existing socket */
1800 unlink (path);
1801
1802 /* Set umask */
1803 old_mask = umask (0077);
1804
1805 /* Make UNIX domain socket. */
1806 sock = socket (AF_UNIX, SOCK_STREAM, 0);
1807 if (sock < 0)
1808 {
paul3d1dc852005-04-05 00:45:23 +00001809 zlog_warn ("Can't create zserv unix socket: %s",
1810 safe_strerror (errno));
1811 zlog_warn ("zebra can't provide full functionality due to above error");
paul718e3742002-12-13 20:15:29 +00001812 return;
1813 }
1814
Vyacheslav Trushkin2ea1ab12011-12-11 18:48:47 +04001815 memset (&route_type_oaths, 0, sizeof (route_type_oaths));
1816
paul718e3742002-12-13 20:15:29 +00001817 /* Make server socket. */
1818 memset (&serv, 0, sizeof (struct sockaddr_un));
1819 serv.sun_family = AF_UNIX;
1820 strncpy (serv.sun_path, path, strlen (path));
Paul Jakma6f0e3f62007-05-10 02:38:51 +00001821#ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
paul718e3742002-12-13 20:15:29 +00001822 len = serv.sun_len = SUN_LEN(&serv);
1823#else
1824 len = sizeof (serv.sun_family) + strlen (serv.sun_path);
Paul Jakma6f0e3f62007-05-10 02:38:51 +00001825#endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
paul718e3742002-12-13 20:15:29 +00001826
1827 ret = bind (sock, (struct sockaddr *) &serv, len);
1828 if (ret < 0)
1829 {
paul3d1dc852005-04-05 00:45:23 +00001830 zlog_warn ("Can't bind to unix socket %s: %s",
1831 path, safe_strerror (errno));
1832 zlog_warn ("zebra can't provide full functionality due to above error");
paul718e3742002-12-13 20:15:29 +00001833 close (sock);
1834 return;
1835 }
1836
1837 ret = listen (sock, 5);
1838 if (ret < 0)
1839 {
paul3d1dc852005-04-05 00:45:23 +00001840 zlog_warn ("Can't listen to unix socket %s: %s",
1841 path, safe_strerror (errno));
1842 zlog_warn ("zebra can't provide full functionality due to above error");
paul718e3742002-12-13 20:15:29 +00001843 close (sock);
1844 return;
1845 }
1846
1847 umask (old_mask);
1848
1849 zebra_event (ZEBRA_SERV, sock, NULL);
1850}
David Lamparter4b6c3322015-04-21 09:47:57 +02001851#endif /* HAVE_TCP_ZEBRA */
David Lamparter6b0655a2014-06-04 06:53:35 +02001852
paul718e3742002-12-13 20:15:29 +00001853
paulb9df2d22004-05-09 09:09:59 +00001854static void
paul718e3742002-12-13 20:15:29 +00001855zebra_event (enum event event, int sock, struct zserv *client)
1856{
1857 switch (event)
1858 {
1859 case ZEBRA_SERV:
paulb21b19c2003-06-15 01:28:29 +00001860 thread_add_read (zebrad.master, zebra_accept, client, sock);
paul718e3742002-12-13 20:15:29 +00001861 break;
1862 case ZEBRA_READ:
1863 client->t_read =
paulb21b19c2003-06-15 01:28:29 +00001864 thread_add_read (zebrad.master, zebra_client_read, client, sock);
paul718e3742002-12-13 20:15:29 +00001865 break;
1866 case ZEBRA_WRITE:
1867 /**/
1868 break;
1869 }
1870}
David Lamparter6b0655a2014-06-04 06:53:35 +02001871
paul718e3742002-12-13 20:15:29 +00001872/* Display default rtm_table for all clients. */
1873DEFUN (show_table,
1874 show_table_cmd,
1875 "show table",
1876 SHOW_STR
1877 "default routing table to use for all clients\n")
1878{
paulb21b19c2003-06-15 01:28:29 +00001879 vty_out (vty, "table %d%s", zebrad.rtm_table_default,
paul718e3742002-12-13 20:15:29 +00001880 VTY_NEWLINE);
1881 return CMD_SUCCESS;
1882}
1883
1884DEFUN (config_table,
1885 config_table_cmd,
1886 "table TABLENO",
1887 "Configure target kernel routing table\n"
1888 "TABLE integer\n")
1889{
paulb21b19c2003-06-15 01:28:29 +00001890 zebrad.rtm_table_default = strtol (argv[0], (char**)0, 10);
paul718e3742002-12-13 20:15:29 +00001891 return CMD_SUCCESS;
1892}
1893
hasso647e4f12003-05-25 11:43:52 +00001894DEFUN (ip_forwarding,
1895 ip_forwarding_cmd,
1896 "ip forwarding",
1897 IP_STR
1898 "Turn on IP forwarding")
1899{
1900 int ret;
1901
1902 ret = ipforward ();
hassob71f00f2004-10-13 12:20:35 +00001903 if (ret == 0)
1904 ret = ipforward_on ();
hasso647e4f12003-05-25 11:43:52 +00001905
hasso647e4f12003-05-25 11:43:52 +00001906 if (ret == 0)
1907 {
1908 vty_out (vty, "Can't turn on IP forwarding%s", VTY_NEWLINE);
1909 return CMD_WARNING;
1910 }
1911
1912 return CMD_SUCCESS;
1913}
1914
paul718e3742002-12-13 20:15:29 +00001915DEFUN (no_ip_forwarding,
1916 no_ip_forwarding_cmd,
1917 "no ip forwarding",
1918 NO_STR
1919 IP_STR
1920 "Turn off IP forwarding")
1921{
1922 int ret;
1923
1924 ret = ipforward ();
hassob71f00f2004-10-13 12:20:35 +00001925 if (ret != 0)
1926 ret = ipforward_off ();
paul718e3742002-12-13 20:15:29 +00001927
paul718e3742002-12-13 20:15:29 +00001928 if (ret != 0)
1929 {
1930 vty_out (vty, "Can't turn off IP forwarding%s", VTY_NEWLINE);
1931 return CMD_WARNING;
1932 }
1933
1934 return CMD_SUCCESS;
1935}
1936
1937/* This command is for debugging purpose. */
1938DEFUN (show_zebra_client,
1939 show_zebra_client_cmd,
1940 "show zebra client",
1941 SHOW_STR
1942 "Zebra information"
1943 "Client information")
1944{
hasso52dc7ee2004-09-23 19:18:23 +00001945 struct listnode *node;
paul718e3742002-12-13 20:15:29 +00001946 struct zserv *client;
1947
paul1eb8ef22005-04-07 07:30:20 +00001948 for (ALL_LIST_ELEMENTS_RO (zebrad.client_list, node, client))
Pradosh Mohapatra60cc9592015-11-09 20:21:41 -05001949 vty_out (vty, "Client %s fd %d%s",
1950 zebra_route_string(client->proto), client->sock,
1951 VTY_NEWLINE);
1952
paul718e3742002-12-13 20:15:29 +00001953 return CMD_SUCCESS;
1954}
1955
1956/* Table configuration write function. */
paulb9df2d22004-05-09 09:09:59 +00001957static int
paul718e3742002-12-13 20:15:29 +00001958config_write_table (struct vty *vty)
1959{
paulb21b19c2003-06-15 01:28:29 +00001960 if (zebrad.rtm_table_default)
1961 vty_out (vty, "table %d%s", zebrad.rtm_table_default,
paul718e3742002-12-13 20:15:29 +00001962 VTY_NEWLINE);
1963 return 0;
1964}
1965
1966/* table node for routing tables. */
Stephen Hemminger7fc626d2008-12-01 11:10:34 -08001967static struct cmd_node table_node =
paul718e3742002-12-13 20:15:29 +00001968{
1969 TABLE_NODE,
1970 "", /* This node has no interface. */
1971 1
1972};
David Lamparter6b0655a2014-06-04 06:53:35 +02001973
paul718e3742002-12-13 20:15:29 +00001974/* Only display ip forwarding is enabled or not. */
1975DEFUN (show_ip_forwarding,
1976 show_ip_forwarding_cmd,
1977 "show ip forwarding",
1978 SHOW_STR
1979 IP_STR
1980 "IP forwarding status\n")
1981{
1982 int ret;
1983
1984 ret = ipforward ();
1985
1986 if (ret == 0)
1987 vty_out (vty, "IP forwarding is off%s", VTY_NEWLINE);
1988 else
1989 vty_out (vty, "IP forwarding is on%s", VTY_NEWLINE);
1990 return CMD_SUCCESS;
1991}
1992
1993#ifdef HAVE_IPV6
1994/* Only display ipv6 forwarding is enabled or not. */
1995DEFUN (show_ipv6_forwarding,
1996 show_ipv6_forwarding_cmd,
1997 "show ipv6 forwarding",
1998 SHOW_STR
1999 "IPv6 information\n"
2000 "Forwarding status\n")
2001{
2002 int ret;
2003
2004 ret = ipforward_ipv6 ();
2005
2006 switch (ret)
2007 {
2008 case -1:
2009 vty_out (vty, "ipv6 forwarding is unknown%s", VTY_NEWLINE);
2010 break;
2011 case 0:
2012 vty_out (vty, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE);
2013 break;
2014 case 1:
2015 vty_out (vty, "ipv6 forwarding is %s%s", "on", VTY_NEWLINE);
2016 break;
2017 default:
2018 vty_out (vty, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE);
2019 break;
2020 }
2021 return CMD_SUCCESS;
2022}
2023
hasso55906722004-02-11 22:42:16 +00002024DEFUN (ipv6_forwarding,
2025 ipv6_forwarding_cmd,
2026 "ipv6 forwarding",
2027 IPV6_STR
2028 "Turn on IPv6 forwarding")
2029{
2030 int ret;
2031
hasso41d3fc92004-04-06 11:59:00 +00002032 ret = ipforward_ipv6 ();
hassob71f00f2004-10-13 12:20:35 +00002033 if (ret == 0)
2034 ret = ipforward_ipv6_on ();
hasso41d3fc92004-04-06 11:59:00 +00002035
hasso41d3fc92004-04-06 11:59:00 +00002036 if (ret == 0)
2037 {
hasso55906722004-02-11 22:42:16 +00002038 vty_out (vty, "Can't turn on IPv6 forwarding%s", VTY_NEWLINE);
2039 return CMD_WARNING;
2040 }
2041
2042 return CMD_SUCCESS;
2043}
2044
paul718e3742002-12-13 20:15:29 +00002045DEFUN (no_ipv6_forwarding,
2046 no_ipv6_forwarding_cmd,
2047 "no ipv6 forwarding",
2048 NO_STR
hasso55906722004-02-11 22:42:16 +00002049 IPV6_STR
2050 "Turn off IPv6 forwarding")
paul718e3742002-12-13 20:15:29 +00002051{
2052 int ret;
2053
hasso41d3fc92004-04-06 11:59:00 +00002054 ret = ipforward_ipv6 ();
hassob71f00f2004-10-13 12:20:35 +00002055 if (ret != 0)
2056 ret = ipforward_ipv6_off ();
hasso41d3fc92004-04-06 11:59:00 +00002057
paul718e3742002-12-13 20:15:29 +00002058 if (ret != 0)
2059 {
2060 vty_out (vty, "Can't turn off IPv6 forwarding%s", VTY_NEWLINE);
2061 return CMD_WARNING;
2062 }
2063
2064 return CMD_SUCCESS;
2065}
2066
2067#endif /* HAVE_IPV6 */
2068
2069/* IPForwarding configuration write function. */
ajs719e9742005-02-28 20:52:15 +00002070static int
paul718e3742002-12-13 20:15:29 +00002071config_write_forwarding (struct vty *vty)
2072{
hasso18a6dce2004-10-03 18:18:34 +00002073 /* FIXME: Find better place for that. */
2074 router_id_write (vty);
2075
paul3e0b3a52004-08-23 18:58:32 +00002076 if (ipforward ())
2077 vty_out (vty, "ip forwarding%s", VTY_NEWLINE);
paul718e3742002-12-13 20:15:29 +00002078#ifdef HAVE_IPV6
paul3e0b3a52004-08-23 18:58:32 +00002079 if (ipforward_ipv6 ())
2080 vty_out (vty, "ipv6 forwarding%s", VTY_NEWLINE);
paul718e3742002-12-13 20:15:29 +00002081#endif /* HAVE_IPV6 */
2082 vty_out (vty, "!%s", VTY_NEWLINE);
2083 return 0;
2084}
2085
2086/* table node for routing tables. */
Stephen Hemminger7fc626d2008-12-01 11:10:34 -08002087static struct cmd_node forwarding_node =
paul718e3742002-12-13 20:15:29 +00002088{
2089 FORWARDING_NODE,
2090 "", /* This node has no interface. */
2091 1
2092};
2093
Udaya Shankara KSd869dbd2016-02-11 21:42:29 +05302094#ifdef HAVE_FPM
2095/* function to write the fpm config info */
2096static int
2097config_write_fpm (struct vty *vty)
2098{
2099 return
2100 fpm_remote_srv_write (vty);
2101}
2102
2103/* Zebra node */
2104static struct cmd_node zebra_node =
2105{
2106 ZEBRA_NODE,
2107 "",
2108 1
2109};
2110#endif
2111
David Lamparter6b0655a2014-06-04 06:53:35 +02002112
paul718e3742002-12-13 20:15:29 +00002113/* Initialisation of zebra and installation of commands. */
2114void
paula1ac18c2005-06-28 17:17:12 +00002115zebra_init (void)
paul718e3742002-12-13 20:15:29 +00002116{
2117 /* Client list init. */
paulb21b19c2003-06-15 01:28:29 +00002118 zebrad.client_list = list_new ();
paul718e3742002-12-13 20:15:29 +00002119
paul718e3742002-12-13 20:15:29 +00002120 /* Install configuration write function. */
2121 install_node (&table_node, config_write_table);
2122 install_node (&forwarding_node, config_write_forwarding);
Udaya Shankara KSd869dbd2016-02-11 21:42:29 +05302123#ifdef HAVE_FPM
2124 install_node (&zebra_node, config_write_fpm);
2125#endif
paul718e3742002-12-13 20:15:29 +00002126
2127 install_element (VIEW_NODE, &show_ip_forwarding_cmd);
hasso647e4f12003-05-25 11:43:52 +00002128 install_element (CONFIG_NODE, &ip_forwarding_cmd);
paul718e3742002-12-13 20:15:29 +00002129 install_element (CONFIG_NODE, &no_ip_forwarding_cmd);
2130 install_element (ENABLE_NODE, &show_zebra_client_cmd);
2131
2132#ifdef HAVE_NETLINK
2133 install_element (VIEW_NODE, &show_table_cmd);
paul718e3742002-12-13 20:15:29 +00002134 install_element (CONFIG_NODE, &config_table_cmd);
2135#endif /* HAVE_NETLINK */
2136
2137#ifdef HAVE_IPV6
2138 install_element (VIEW_NODE, &show_ipv6_forwarding_cmd);
hasso55906722004-02-11 22:42:16 +00002139 install_element (CONFIG_NODE, &ipv6_forwarding_cmd);
paul718e3742002-12-13 20:15:29 +00002140 install_element (CONFIG_NODE, &no_ipv6_forwarding_cmd);
2141#endif /* HAVE_IPV6 */
Paul Jakma7514fb72007-05-02 16:05:35 +00002142
2143 /* Route-map */
2144 zebra_route_map_init ();
paul718e3742002-12-13 20:15:29 +00002145}
Denis Ovsienko97be79f2009-07-24 20:45:31 +04002146
2147/* Make zebra server socket, wiping any existing one (see bug #403). */
2148void
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +04002149zebra_zserv_socket_init (char *path)
Denis Ovsienko97be79f2009-07-24 20:45:31 +04002150{
2151#ifdef HAVE_TCP_ZEBRA
2152 zebra_serv ();
2153#else
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +04002154 zebra_serv_un (path ? path : ZEBRA_SERV_PATH);
Denis Ovsienko97be79f2009-07-24 20:45:31 +04002155#endif /* HAVE_TCP_ZEBRA */
2156}