blob: 85f448c420f0c2cd1fc48395eb15aaf38b73c9ab [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
135zserv_create_header (struct stream *s, uint16_t cmd)
136{
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);
141 stream_putw (s, cmd);
142}
143
Josh Bailey51d4ef82012-03-21 17:13:39 -0700144static void
145zserv_encode_interface (struct stream *s, struct interface *ifp)
146{
147 /* Interface information. */
148 stream_put (s, ifp->name, INTERFACE_NAMSIZ);
149 stream_putl (s, ifp->ifindex);
150 stream_putc (s, ifp->status);
151 stream_putq (s, ifp->flags);
152 stream_putl (s, ifp->metric);
153 stream_putl (s, ifp->mtu);
154 stream_putl (s, ifp->mtu6);
155 stream_putl (s, ifp->bandwidth);
156#ifdef HAVE_STRUCT_SOCKADDR_DL
David Lamparterca3ccd82012-09-26 14:52:39 +0200157 stream_put (s, &ifp->sdl, sizeof (ifp->sdl_storage));
Josh Bailey51d4ef82012-03-21 17:13:39 -0700158#else
159 stream_putl (s, ifp->hw_addr_len);
160 if (ifp->hw_addr_len)
161 stream_put (s, ifp->hw_addr, ifp->hw_addr_len);
162#endif /* HAVE_STRUCT_SOCKADDR_DL */
163
164 /* Write packet size. */
165 stream_putw_at (s, 0, stream_get_endp (s));
166}
167
paul718e3742002-12-13 20:15:29 +0000168/* Interface is added. Send ZEBRA_INTERFACE_ADD to client. */
paulb9df2d22004-05-09 09:09:59 +0000169/*
170 * This function is called in the following situations:
171 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
172 * from the client.
173 * - at startup, when zebra figures out the available interfaces
174 * - when an interface is added (where support for
175 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
176 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
177 * received)
178 */
paul718e3742002-12-13 20:15:29 +0000179int
180zsend_interface_add (struct zserv *client, struct interface *ifp)
181{
182 struct stream *s;
183
184 /* Check this client need interface information. */
185 if (! client->ifinfo)
ajs719e9742005-02-28 20:52:15 +0000186 return 0;
paul718e3742002-12-13 20:15:29 +0000187
188 s = client->obuf;
189 stream_reset (s);
190
paulc1b98002006-01-16 01:54:02 +0000191 zserv_create_header (s, ZEBRA_INTERFACE_ADD);
Josh Bailey51d4ef82012-03-21 17:13:39 -0700192 zserv_encode_interface (s, ifp);
paul718e3742002-12-13 20:15:29 +0000193
ajs719e9742005-02-28 20:52:15 +0000194 return zebra_server_send_message(client);
paul718e3742002-12-13 20:15:29 +0000195}
196
197/* Interface deletion from zebra daemon. */
198int
199zsend_interface_delete (struct zserv *client, struct interface *ifp)
200{
201 struct stream *s;
202
203 /* Check this client need interface information. */
204 if (! client->ifinfo)
ajs719e9742005-02-28 20:52:15 +0000205 return 0;
paul718e3742002-12-13 20:15:29 +0000206
207 s = client->obuf;
208 stream_reset (s);
paul718e3742002-12-13 20:15:29 +0000209
Josh Bailey51d4ef82012-03-21 17:13:39 -0700210 zserv_create_header (s, ZEBRA_INTERFACE_DELETE);
211 zserv_encode_interface (s, ifp);
paul718e3742002-12-13 20:15:29 +0000212
ajs719e9742005-02-28 20:52:15 +0000213 return zebra_server_send_message (client);
paul718e3742002-12-13 20:15:29 +0000214}
215
paulb9df2d22004-05-09 09:09:59 +0000216/* Interface address is added/deleted. Send ZEBRA_INTERFACE_ADDRESS_ADD or
217 * ZEBRA_INTERFACE_ADDRESS_DELETE to the client.
218 *
219 * A ZEBRA_INTERFACE_ADDRESS_ADD is sent in the following situations:
220 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
221 * from the client, after the ZEBRA_INTERFACE_ADD has been
222 * sent from zebra to the client
223 * - redistribute new address info to all clients in the following situations
224 * - at startup, when zebra figures out the available interfaces
225 * - when an interface is added (where support for
226 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
227 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
228 * received)
229 * - for the vty commands "ip address A.B.C.D/M [<secondary>|<label LINE>]"
230 * and "no bandwidth <1-10000000>", "ipv6 address X:X::X:X/M"
231 * - when an RTM_NEWADDR message is received from the kernel,
232 *
233 * The call tree that triggers ZEBRA_INTERFACE_ADDRESS_DELETE:
234 *
235 * zsend_interface_address(DELETE)
236 * ^
237 * |
238 * zebra_interface_address_delete_update
239 * ^ ^ ^
paul6eb88272005-07-29 14:36:00 +0000240 * | | if_delete_update
241 * | |
paulb9df2d22004-05-09 09:09:59 +0000242 * ip_address_uninstall connected_delete_ipv4
243 * [ipv6_addresss_uninstall] [connected_delete_ipv6]
244 * ^ ^
245 * | |
246 * | RTM_NEWADDR on routing/netlink socket
247 * |
248 * vty commands:
249 * "no ip address A.B.C.D/M [label LINE]"
250 * "no ip address A.B.C.D/M secondary"
251 * ["no ipv6 address X:X::X:X/M"]
252 *
253 */
paul718e3742002-12-13 20:15:29 +0000254int
paulb9df2d22004-05-09 09:09:59 +0000255zsend_interface_address (int cmd, struct zserv *client,
256 struct interface *ifp, struct connected *ifc)
paul718e3742002-12-13 20:15:29 +0000257{
258 int blen;
259 struct stream *s;
260 struct prefix *p;
261
262 /* Check this client need interface information. */
263 if (! client->ifinfo)
ajs719e9742005-02-28 20:52:15 +0000264 return 0;
paul718e3742002-12-13 20:15:29 +0000265
266 s = client->obuf;
267 stream_reset (s);
paulc1b98002006-01-16 01:54:02 +0000268
269 zserv_create_header (s, cmd);
paul718e3742002-12-13 20:15:29 +0000270 stream_putl (s, ifp->ifindex);
271
272 /* Interface address flag. */
273 stream_putc (s, ifc->flags);
274
275 /* Prefix information. */
276 p = ifc->address;
277 stream_putc (s, p->family);
278 blen = prefix_blen (p);
279 stream_put (s, &p->u.prefix, blen);
paulb9df2d22004-05-09 09:09:59 +0000280
281 /*
282 * XXX gnu version does not send prefixlen for ZEBRA_INTERFACE_ADDRESS_DELETE
283 * but zebra_interface_address_delete_read() in the gnu version
284 * expects to find it
285 */
paul718e3742002-12-13 20:15:29 +0000286 stream_putc (s, p->prefixlen);
287
288 /* Destination. */
289 p = ifc->destination;
290 if (p)
291 stream_put (s, &p->u.prefix, blen);
292 else
293 stream_put (s, NULL, blen);
294
295 /* Write packet size. */
296 stream_putw_at (s, 0, stream_get_endp (s));
297
ajs719e9742005-02-28 20:52:15 +0000298 return zebra_server_send_message(client);
paul718e3742002-12-13 20:15:29 +0000299}
300
paulb9df2d22004-05-09 09:09:59 +0000301/*
302 * The cmd passed to zsend_interface_update may be ZEBRA_INTERFACE_UP or
303 * ZEBRA_INTERFACE_DOWN.
304 *
305 * The ZEBRA_INTERFACE_UP message is sent from the zebra server to
306 * the clients in one of 2 situations:
307 * - an if_up is detected e.g., as a result of an RTM_IFINFO message
308 * - a vty command modifying the bandwidth of an interface is received.
309 * The ZEBRA_INTERFACE_DOWN message is sent when an if_down is detected.
310 */
paul718e3742002-12-13 20:15:29 +0000311int
paulb9df2d22004-05-09 09:09:59 +0000312zsend_interface_update (int cmd, struct zserv *client, struct interface *ifp)
paul718e3742002-12-13 20:15:29 +0000313{
314 struct stream *s;
315
316 /* Check this client need interface information. */
317 if (! client->ifinfo)
ajs719e9742005-02-28 20:52:15 +0000318 return 0;
paul718e3742002-12-13 20:15:29 +0000319
320 s = client->obuf;
321 stream_reset (s);
322
paulc1b98002006-01-16 01:54:02 +0000323 zserv_create_header (s, cmd);
Josh Bailey51d4ef82012-03-21 17:13:39 -0700324 zserv_encode_interface (s, ifp);
paul718e3742002-12-13 20:15:29 +0000325
ajs719e9742005-02-28 20:52:15 +0000326 return zebra_server_send_message(client);
paul718e3742002-12-13 20:15:29 +0000327}
328
paulb9df2d22004-05-09 09:09:59 +0000329/*
330 * The zebra server sends the clients a ZEBRA_IPV4_ROUTE_ADD or a
331 * ZEBRA_IPV6_ROUTE_ADD via zsend_route_multipath in the following
332 * situations:
333 * - when the client starts up, and requests default information
334 * by sending a ZEBRA_REDISTRIBUTE_DEFAULT_ADD to the zebra server, in the
335 * - case of rip, ripngd, ospfd and ospf6d, when the client sends a
336 * ZEBRA_REDISTRIBUTE_ADD as a result of the "redistribute" vty cmd,
337 * - when the zebra server redistributes routes after it updates its rib
338 *
339 * The zebra server sends clients a ZEBRA_IPV4_ROUTE_DELETE or a
340 * ZEBRA_IPV6_ROUTE_DELETE via zsend_route_multipath when:
341 * - a "ip route" or "ipv6 route" vty command is issued, a prefix is
342 * - deleted from zebra's rib, and this info
343 * has to be redistributed to the clients
344 *
345 * XXX The ZEBRA_IPV*_ROUTE_ADD message is also sent by the client to the
346 * zebra server when the client wants to tell the zebra server to add a
347 * route to the kernel (zapi_ipv4_add etc. ). Since it's essentially the
348 * same message being sent back and forth, this function and
349 * zapi_ipv{4,6}_{add, delete} should be re-written to avoid code
350 * duplication.
351 */
paul718e3742002-12-13 20:15:29 +0000352int
paulb9df2d22004-05-09 09:09:59 +0000353zsend_route_multipath (int cmd, struct zserv *client, struct prefix *p,
354 struct rib *rib)
paul718e3742002-12-13 20:15:29 +0000355{
356 int psize;
357 struct stream *s;
358 struct nexthop *nexthop;
paul1dcb5172005-05-31 08:38:50 +0000359 unsigned long nhnummark = 0, messmark = 0;
paulb9df2d22004-05-09 09:09:59 +0000360 int nhnum = 0;
paul1dcb5172005-05-31 08:38:50 +0000361 u_char zapi_flags = 0;
paulb9df2d22004-05-09 09:09:59 +0000362
paul718e3742002-12-13 20:15:29 +0000363 s = client->obuf;
364 stream_reset (s);
paulc1b98002006-01-16 01:54:02 +0000365
366 zserv_create_header (s, cmd);
367
368 /* Put type and nexthop. */
paul718e3742002-12-13 20:15:29 +0000369 stream_putc (s, rib->type);
370 stream_putc (s, rib->flags);
paul1dcb5172005-05-31 08:38:50 +0000371
372 /* marker for message flags field */
373 messmark = stream_get_endp (s);
374 stream_putc (s, 0);
paul718e3742002-12-13 20:15:29 +0000375
376 /* Prefix. */
377 psize = PSIZE (p->prefixlen);
378 stream_putc (s, p->prefixlen);
paulb9df2d22004-05-09 09:09:59 +0000379 stream_write (s, (u_char *) & p->u.prefix, psize);
paul718e3742002-12-13 20:15:29 +0000380
paulb9df2d22004-05-09 09:09:59 +0000381 /*
382 * XXX The message format sent by zebra below does not match the format
383 * of the corresponding message expected by the zebra server
384 * itself (e.g., see zread_ipv4_add). The nexthop_num is not set correctly,
385 * (is there a bug on the client side if more than one segment is sent?)
386 * nexthop ZEBRA_NEXTHOP_IPV4 is never set, ZEBRA_NEXTHOP_IFINDEX
387 * is hard-coded.
388 */
paul718e3742002-12-13 20:15:29 +0000389 /* Nexthop */
paul1dcb5172005-05-31 08:38:50 +0000390
paul718e3742002-12-13 20:15:29 +0000391 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
392 {
Christian Frankefa713d92013-07-05 15:35:37 +0000393 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB)
394 || nexthop_has_fib_child(nexthop))
paulb9df2d22004-05-09 09:09:59 +0000395 {
paul1dcb5172005-05-31 08:38:50 +0000396 SET_FLAG (zapi_flags, ZAPI_MESSAGE_NEXTHOP);
397 SET_FLAG (zapi_flags, ZAPI_MESSAGE_IFINDEX);
398
399 if (nhnummark == 0)
400 {
401 nhnummark = stream_get_endp (s);
402 stream_putc (s, 1); /* placeholder */
403 }
404
paulb9df2d22004-05-09 09:09:59 +0000405 nhnum++;
paul718e3742002-12-13 20:15:29 +0000406
paulb9df2d22004-05-09 09:09:59 +0000407 switch(nexthop->type)
408 {
409 case NEXTHOP_TYPE_IPV4:
410 case NEXTHOP_TYPE_IPV4_IFINDEX:
411 stream_put_in_addr (s, &nexthop->gate.ipv4);
412 break;
413#ifdef HAVE_IPV6
414 case NEXTHOP_TYPE_IPV6:
415 case NEXTHOP_TYPE_IPV6_IFINDEX:
416 case NEXTHOP_TYPE_IPV6_IFNAME:
417 stream_write (s, (u_char *) &nexthop->gate.ipv6, 16);
418 break;
419#endif
420 default:
421 if (cmd == ZEBRA_IPV4_ROUTE_ADD
422 || cmd == ZEBRA_IPV4_ROUTE_DELETE)
423 {
424 struct in_addr empty;
paul44983cf2004-09-22 13:15:58 +0000425 memset (&empty, 0, sizeof (struct in_addr));
paulb9df2d22004-05-09 09:09:59 +0000426 stream_write (s, (u_char *) &empty, IPV4_MAX_BYTELEN);
427 }
428 else
429 {
430 struct in6_addr empty;
431 memset (&empty, 0, sizeof (struct in6_addr));
432 stream_write (s, (u_char *) &empty, IPV6_MAX_BYTELEN);
433 }
434 }
paul718e3742002-12-13 20:15:29 +0000435
paulb9df2d22004-05-09 09:09:59 +0000436 /* Interface index. */
437 stream_putc (s, 1);
438 stream_putl (s, nexthop->ifindex);
paul718e3742002-12-13 20:15:29 +0000439
paulb9df2d22004-05-09 09:09:59 +0000440 break;
441 }
paul718e3742002-12-13 20:15:29 +0000442 }
443
444 /* Metric */
Stephen Hemmingercf8a8312010-08-18 15:56:46 -0700445 if (cmd == ZEBRA_IPV4_ROUTE_ADD || cmd == ZEBRA_IPV6_ROUTE_ADD)
paul1dcb5172005-05-31 08:38:50 +0000446 {
vincentfbf5d032005-09-29 11:25:50 +0000447 SET_FLAG (zapi_flags, ZAPI_MESSAGE_DISTANCE);
448 stream_putc (s, rib->distance);
paul1dcb5172005-05-31 08:38:50 +0000449 SET_FLAG (zapi_flags, ZAPI_MESSAGE_METRIC);
450 stream_putl (s, rib->metric);
451 }
452
453 /* write real message flags value */
454 stream_putc_at (s, messmark, zapi_flags);
455
paulb9df2d22004-05-09 09:09:59 +0000456 /* Write next-hop number */
457 if (nhnummark)
hassoc1eaa442004-10-19 06:26:01 +0000458 stream_putc_at (s, nhnummark, nhnum);
paulb9df2d22004-05-09 09:09:59 +0000459
paul718e3742002-12-13 20:15:29 +0000460 /* Write packet size. */
461 stream_putw_at (s, 0, stream_get_endp (s));
462
ajs719e9742005-02-28 20:52:15 +0000463 return zebra_server_send_message(client);
paul718e3742002-12-13 20:15:29 +0000464}
465
paul718e3742002-12-13 20:15:29 +0000466#ifdef HAVE_IPV6
ajs719e9742005-02-28 20:52:15 +0000467static int
paul718e3742002-12-13 20:15:29 +0000468zsend_ipv6_nexthop_lookup (struct zserv *client, struct in6_addr *addr)
469{
470 struct stream *s;
471 struct rib *rib;
472 unsigned long nump;
473 u_char num;
474 struct nexthop *nexthop;
475
476 /* Lookup nexthop. */
Feng Lu0d0686f2015-05-22 11:40:02 +0200477 rib = rib_match_ipv6 (addr, VRF_DEFAULT);
paul718e3742002-12-13 20:15:29 +0000478
479 /* Get output stream. */
480 s = client->obuf;
481 stream_reset (s);
482
483 /* Fill in result. */
paulc1b98002006-01-16 01:54:02 +0000484 zserv_create_header (s, ZEBRA_IPV6_NEXTHOP_LOOKUP);
paul718e3742002-12-13 20:15:29 +0000485 stream_put (s, &addr, 16);
486
487 if (rib)
488 {
489 stream_putl (s, rib->metric);
490 num = 0;
paul9985f832005-02-09 15:51:56 +0000491 nump = stream_get_endp(s);
paul718e3742002-12-13 20:15:29 +0000492 stream_putc (s, 0);
Christian Frankefa713d92013-07-05 15:35:37 +0000493 /* Only non-recursive routes are elegible to resolve nexthop we
494 * are looking up. Therefore, we will just iterate over the top
495 * chain of nexthops. */
paul718e3742002-12-13 20:15:29 +0000496 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
497 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB))
498 {
499 stream_putc (s, nexthop->type);
500 switch (nexthop->type)
501 {
502 case ZEBRA_NEXTHOP_IPV6:
503 stream_put (s, &nexthop->gate.ipv6, 16);
504 break;
505 case ZEBRA_NEXTHOP_IPV6_IFINDEX:
506 case ZEBRA_NEXTHOP_IPV6_IFNAME:
507 stream_put (s, &nexthop->gate.ipv6, 16);
508 stream_putl (s, nexthop->ifindex);
509 break;
510 case ZEBRA_NEXTHOP_IFINDEX:
511 case ZEBRA_NEXTHOP_IFNAME:
512 stream_putl (s, nexthop->ifindex);
513 break;
hassofa2b17e2004-03-04 17:45:00 +0000514 default:
515 /* do nothing */
516 break;
paul718e3742002-12-13 20:15:29 +0000517 }
518 num++;
519 }
520 stream_putc_at (s, nump, num);
521 }
522 else
523 {
524 stream_putl (s, 0);
525 stream_putc (s, 0);
526 }
527
528 stream_putw_at (s, 0, stream_get_endp (s));
529
ajs719e9742005-02-28 20:52:15 +0000530 return zebra_server_send_message(client);
paul718e3742002-12-13 20:15:29 +0000531}
532#endif /* HAVE_IPV6 */
533
paulb9df2d22004-05-09 09:09:59 +0000534static int
paul718e3742002-12-13 20:15:29 +0000535zsend_ipv4_nexthop_lookup (struct zserv *client, struct in_addr addr)
536{
537 struct stream *s;
538 struct rib *rib;
539 unsigned long nump;
540 u_char num;
541 struct nexthop *nexthop;
542
David Lamparterf598cf72014-11-22 14:44:20 -0800543 /* Lookup nexthop - eBGP excluded */
Feng Lu0d0686f2015-05-22 11:40:02 +0200544 rib = rib_match_ipv4_safi (addr, SAFI_UNICAST, 1, NULL, VRF_DEFAULT);
paul718e3742002-12-13 20:15:29 +0000545
546 /* Get output stream. */
547 s = client->obuf;
548 stream_reset (s);
549
550 /* Fill in result. */
paulc1b98002006-01-16 01:54:02 +0000551 zserv_create_header (s, ZEBRA_IPV4_NEXTHOP_LOOKUP);
paul718e3742002-12-13 20:15:29 +0000552 stream_put_in_addr (s, &addr);
553
554 if (rib)
555 {
Christian Frankebb97e462013-05-25 14:01:35 +0000556 if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
557 zlog_debug("%s: Matching rib entry found.", __func__);
paul718e3742002-12-13 20:15:29 +0000558 stream_putl (s, rib->metric);
559 num = 0;
paul9985f832005-02-09 15:51:56 +0000560 nump = stream_get_endp(s);
paul718e3742002-12-13 20:15:29 +0000561 stream_putc (s, 0);
Christian Frankefa713d92013-07-05 15:35:37 +0000562 /* Only non-recursive routes are elegible to resolve the nexthop we
563 * are looking up. Therefore, we will just iterate over the top
564 * chain of nexthops. */
paul718e3742002-12-13 20:15:29 +0000565 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
566 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB))
567 {
568 stream_putc (s, nexthop->type);
569 switch (nexthop->type)
570 {
571 case ZEBRA_NEXTHOP_IPV4:
572 stream_put_in_addr (s, &nexthop->gate.ipv4);
573 break;
Christian Frankebb97e462013-05-25 14:01:35 +0000574 case ZEBRA_NEXTHOP_IPV4_IFINDEX:
575 stream_put_in_addr (s, &nexthop->gate.ipv4);
576 stream_putl (s, nexthop->ifindex);
577 break;
paul718e3742002-12-13 20:15:29 +0000578 case ZEBRA_NEXTHOP_IFINDEX:
579 case ZEBRA_NEXTHOP_IFNAME:
580 stream_putl (s, nexthop->ifindex);
581 break;
hassofa2b17e2004-03-04 17:45:00 +0000582 default:
583 /* do nothing */
584 break;
paul718e3742002-12-13 20:15:29 +0000585 }
586 num++;
587 }
588 stream_putc_at (s, nump, num);
589 }
590 else
591 {
Christian Frankebb97e462013-05-25 14:01:35 +0000592 if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
593 zlog_debug("%s: No matching rib entry found.", __func__);
paul718e3742002-12-13 20:15:29 +0000594 stream_putl (s, 0);
595 stream_putc (s, 0);
596 }
597
598 stream_putw_at (s, 0, stream_get_endp (s));
599
ajs719e9742005-02-28 20:52:15 +0000600 return zebra_server_send_message(client);
paul718e3742002-12-13 20:15:29 +0000601}
602
Everton Marques4e5275b2014-07-01 15:15:52 -0300603/*
604 Modified version of zsend_ipv4_nexthop_lookup():
605 Query unicast rib if nexthop is not found on mrib.
606 Returns both route metric and protocol distance.
607*/
608static int
David Lamparterbd078122015-01-06 19:53:24 +0100609zsend_ipv4_nexthop_lookup_mrib (struct zserv *client, struct in_addr addr,
610 struct rib *rib)
Everton Marques4e5275b2014-07-01 15:15:52 -0300611{
612 struct stream *s;
Everton Marques4e5275b2014-07-01 15:15:52 -0300613 unsigned long nump;
614 u_char num;
615 struct nexthop *nexthop;
Everton Marques4e5275b2014-07-01 15:15:52 -0300616
617 /* Get output stream. */
618 s = client->obuf;
619 stream_reset (s);
620
621 /* Fill in result. */
622 zserv_create_header (s, ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB);
623 stream_put_in_addr (s, &addr);
624
625 if (rib)
626 {
627 stream_putc (s, rib->distance);
628 stream_putl (s, rib->metric);
629 num = 0;
630 nump = stream_get_endp(s); /* remember position for nexthop_num */
631 stream_putc (s, 0); /* reserve room for nexthop_num */
632 /* Only non-recursive routes are elegible to resolve the nexthop we
633 * are looking up. Therefore, we will just iterate over the top
634 * chain of nexthops. */
635 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
636 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB))
637 {
638 stream_putc (s, nexthop->type);
639 switch (nexthop->type)
640 {
641 case ZEBRA_NEXTHOP_IPV4:
642 stream_put_in_addr (s, &nexthop->gate.ipv4);
643 break;
644 case ZEBRA_NEXTHOP_IPV4_IFINDEX:
645 stream_put_in_addr (s, &nexthop->gate.ipv4);
646 stream_putl (s, nexthop->ifindex);
647 break;
648 case ZEBRA_NEXTHOP_IFINDEX:
649 case ZEBRA_NEXTHOP_IFNAME:
650 stream_putl (s, nexthop->ifindex);
651 break;
652 default:
653 /* do nothing */
654 break;
655 }
656 num++;
657 }
658
659 stream_putc_at (s, nump, num); /* store nexthop_num */
660 }
661 else
662 {
663 stream_putc (s, 0); /* distance */
664 stream_putl (s, 0); /* metric */
665 stream_putc (s, 0); /* nexthop_num */
666 }
667
668 stream_putw_at (s, 0, stream_get_endp (s));
669
670 return zebra_server_send_message(client);
671}
672
paulb9df2d22004-05-09 09:09:59 +0000673static int
paul718e3742002-12-13 20:15:29 +0000674zsend_ipv4_import_lookup (struct zserv *client, struct prefix_ipv4 *p)
675{
676 struct stream *s;
677 struct rib *rib;
678 unsigned long nump;
679 u_char num;
680 struct nexthop *nexthop;
681
682 /* Lookup nexthop. */
Feng Lu0d0686f2015-05-22 11:40:02 +0200683 rib = rib_lookup_ipv4 (p, VRF_DEFAULT);
paul718e3742002-12-13 20:15:29 +0000684
685 /* Get output stream. */
686 s = client->obuf;
687 stream_reset (s);
688
689 /* Fill in result. */
paulc1b98002006-01-16 01:54:02 +0000690 zserv_create_header (s, ZEBRA_IPV4_IMPORT_LOOKUP);
paul718e3742002-12-13 20:15:29 +0000691 stream_put_in_addr (s, &p->prefix);
692
693 if (rib)
694 {
695 stream_putl (s, rib->metric);
696 num = 0;
paul9985f832005-02-09 15:51:56 +0000697 nump = stream_get_endp(s);
paul718e3742002-12-13 20:15:29 +0000698 stream_putc (s, 0);
699 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
Christian Frankefa713d92013-07-05 15:35:37 +0000700 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB)
701 || nexthop_has_fib_child(nexthop))
paul718e3742002-12-13 20:15:29 +0000702 {
703 stream_putc (s, nexthop->type);
704 switch (nexthop->type)
705 {
706 case ZEBRA_NEXTHOP_IPV4:
707 stream_put_in_addr (s, &nexthop->gate.ipv4);
708 break;
Christian Frankea12afd52013-05-25 14:01:36 +0000709 case ZEBRA_NEXTHOP_IPV4_IFINDEX:
710 stream_put_in_addr (s, &nexthop->gate.ipv4);
711 stream_putl (s, nexthop->ifindex);
712 break;
paul718e3742002-12-13 20:15:29 +0000713 case ZEBRA_NEXTHOP_IFINDEX:
714 case ZEBRA_NEXTHOP_IFNAME:
715 stream_putl (s, nexthop->ifindex);
716 break;
hassofa2b17e2004-03-04 17:45:00 +0000717 default:
718 /* do nothing */
719 break;
paul718e3742002-12-13 20:15:29 +0000720 }
721 num++;
722 }
723 stream_putc_at (s, nump, num);
724 }
725 else
726 {
727 stream_putl (s, 0);
728 stream_putc (s, 0);
729 }
730
731 stream_putw_at (s, 0, stream_get_endp (s));
732
ajs719e9742005-02-28 20:52:15 +0000733 return zebra_server_send_message(client);
paul718e3742002-12-13 20:15:29 +0000734}
David Lamparter6b0655a2014-06-04 06:53:35 +0200735
hasso18a6dce2004-10-03 18:18:34 +0000736/* Router-id is updated. Send ZEBRA_ROUTER_ID_ADD to client. */
737int
738zsend_router_id_update (struct zserv *client, struct prefix *p)
739{
740 struct stream *s;
741 int blen;
742
743 /* Check this client need interface information. */
744 if (!client->ridinfo)
ajs719e9742005-02-28 20:52:15 +0000745 return 0;
hasso18a6dce2004-10-03 18:18:34 +0000746
747 s = client->obuf;
748 stream_reset (s);
749
hasso18a6dce2004-10-03 18:18:34 +0000750 /* Message type. */
paulc1b98002006-01-16 01:54:02 +0000751 zserv_create_header (s, ZEBRA_ROUTER_ID_UPDATE);
hasso18a6dce2004-10-03 18:18:34 +0000752
753 /* Prefix information. */
754 stream_putc (s, p->family);
755 blen = prefix_blen (p);
756 stream_put (s, &p->u.prefix, blen);
757 stream_putc (s, p->prefixlen);
758
759 /* Write packet size. */
760 stream_putw_at (s, 0, stream_get_endp (s));
761
ajs719e9742005-02-28 20:52:15 +0000762 return zebra_server_send_message(client);
hasso18a6dce2004-10-03 18:18:34 +0000763}
David Lamparter6b0655a2014-06-04 06:53:35 +0200764
paul718e3742002-12-13 20:15:29 +0000765/* Register zebra server interface information. Send current all
766 interface and address information. */
ajs719e9742005-02-28 20:52:15 +0000767static int
paul718e3742002-12-13 20:15:29 +0000768zread_interface_add (struct zserv *client, u_short length)
769{
paul1eb8ef22005-04-07 07:30:20 +0000770 struct listnode *ifnode, *ifnnode;
771 struct listnode *cnode, *cnnode;
paul718e3742002-12-13 20:15:29 +0000772 struct interface *ifp;
773 struct connected *c;
774
775 /* Interface information is needed. */
776 client->ifinfo = 1;
777
paul1eb8ef22005-04-07 07:30:20 +0000778 for (ALL_LIST_ELEMENTS (iflist, ifnode, ifnnode, ifp))
paul718e3742002-12-13 20:15:29 +0000779 {
paul718e3742002-12-13 20:15:29 +0000780 /* Skip pseudo interface. */
781 if (! CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE))
782 continue;
783
ajs719e9742005-02-28 20:52:15 +0000784 if (zsend_interface_add (client, ifp) < 0)
785 return -1;
paul718e3742002-12-13 20:15:29 +0000786
paul1eb8ef22005-04-07 07:30:20 +0000787 for (ALL_LIST_ELEMENTS (ifp->connected, cnode, cnnode, c))
paul718e3742002-12-13 20:15:29 +0000788 {
ajs719e9742005-02-28 20:52:15 +0000789 if (CHECK_FLAG (c->conf, ZEBRA_IFC_REAL) &&
790 (zsend_interface_address (ZEBRA_INTERFACE_ADDRESS_ADD, client,
791 ifp, c) < 0))
792 return -1;
paul718e3742002-12-13 20:15:29 +0000793 }
794 }
ajs719e9742005-02-28 20:52:15 +0000795 return 0;
paul718e3742002-12-13 20:15:29 +0000796}
797
798/* Unregister zebra server interface information. */
ajs719e9742005-02-28 20:52:15 +0000799static int
paul718e3742002-12-13 20:15:29 +0000800zread_interface_delete (struct zserv *client, u_short length)
801{
802 client->ifinfo = 0;
ajs719e9742005-02-28 20:52:15 +0000803 return 0;
paul718e3742002-12-13 20:15:29 +0000804}
805
806/* This function support multiple nexthop. */
paulb9df2d22004-05-09 09:09:59 +0000807/*
808 * Parse the ZEBRA_IPV4_ROUTE_ADD sent from client. Update rib and
809 * add kernel route.
810 */
ajs719e9742005-02-28 20:52:15 +0000811static int
paul718e3742002-12-13 20:15:29 +0000812zread_ipv4_add (struct zserv *client, u_short length)
813{
814 int i;
815 struct rib *rib;
816 struct prefix_ipv4 p;
817 u_char message;
818 struct in_addr nexthop;
819 u_char nexthop_num;
820 u_char nexthop_type;
821 struct stream *s;
822 unsigned int ifindex;
823 u_char ifname_len;
G.Balajicddf3912011-11-26 21:59:32 +0400824 safi_t safi;
825
paul718e3742002-12-13 20:15:29 +0000826
827 /* Get input stream. */
828 s = client->ibuf;
829
830 /* Allocate new rib. */
paul4d38fdb2005-04-28 17:35:14 +0000831 rib = XCALLOC (MTYPE_RIB, sizeof (struct rib));
832
paul718e3742002-12-13 20:15:29 +0000833 /* Type, flags, message. */
834 rib->type = stream_getc (s);
835 rib->flags = stream_getc (s);
paulb9df2d22004-05-09 09:09:59 +0000836 message = stream_getc (s);
G.Balajicddf3912011-11-26 21:59:32 +0400837 safi = stream_getw (s);
paul718e3742002-12-13 20:15:29 +0000838 rib->uptime = time (NULL);
839
840 /* IPv4 prefix. */
841 memset (&p, 0, sizeof (struct prefix_ipv4));
842 p.family = AF_INET;
843 p.prefixlen = stream_getc (s);
844 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
845
Feng Lu0d0686f2015-05-22 11:40:02 +0200846 /* VRF ID */
847 rib->vrf_id = VRF_DEFAULT;
848
paul718e3742002-12-13 20:15:29 +0000849 /* Nexthop parse. */
850 if (CHECK_FLAG (message, ZAPI_MESSAGE_NEXTHOP))
851 {
852 nexthop_num = stream_getc (s);
853
854 for (i = 0; i < nexthop_num; i++)
855 {
856 nexthop_type = stream_getc (s);
857
858 switch (nexthop_type)
859 {
860 case ZEBRA_NEXTHOP_IFINDEX:
861 ifindex = stream_getl (s);
862 nexthop_ifindex_add (rib, ifindex);
863 break;
864 case ZEBRA_NEXTHOP_IFNAME:
865 ifname_len = stream_getc (s);
paul9985f832005-02-09 15:51:56 +0000866 stream_forward_getp (s, ifname_len);
paul718e3742002-12-13 20:15:29 +0000867 break;
868 case ZEBRA_NEXTHOP_IPV4:
869 nexthop.s_addr = stream_get_ipv4 (s);
Paul Jakma7514fb72007-05-02 16:05:35 +0000870 nexthop_ipv4_add (rib, &nexthop, NULL);
paul718e3742002-12-13 20:15:29 +0000871 break;
Joakim Tjernlundc963c202012-07-07 17:06:13 +0200872 case ZEBRA_NEXTHOP_IPV4_IFINDEX:
873 nexthop.s_addr = stream_get_ipv4 (s);
874 ifindex = stream_getl (s);
875 nexthop_ipv4_ifindex_add (rib, &nexthop, NULL, ifindex);
876 break;
paul718e3742002-12-13 20:15:29 +0000877 case ZEBRA_NEXTHOP_IPV6:
paul9985f832005-02-09 15:51:56 +0000878 stream_forward_getp (s, IPV6_MAX_BYTELEN);
paul718e3742002-12-13 20:15:29 +0000879 break;
Subbaiah Venkata6902c692012-03-27 19:21:29 -0700880 case ZEBRA_NEXTHOP_BLACKHOLE:
881 nexthop_blackhole_add (rib);
882 break;
883 }
paul718e3742002-12-13 20:15:29 +0000884 }
885 }
886
887 /* Distance. */
888 if (CHECK_FLAG (message, ZAPI_MESSAGE_DISTANCE))
889 rib->distance = stream_getc (s);
890
891 /* Metric. */
892 if (CHECK_FLAG (message, ZAPI_MESSAGE_METRIC))
893 rib->metric = stream_getl (s);
894
Paul Jakma171eee32006-07-27 16:11:02 +0000895 /* Table */
896 rib->table=zebrad.rtm_table_default;
G.Balajicddf3912011-11-26 21:59:32 +0400897 rib_add_ipv4_multipath (&p, rib, safi);
ajs719e9742005-02-28 20:52:15 +0000898 return 0;
paul718e3742002-12-13 20:15:29 +0000899}
900
901/* Zebra server IPv4 prefix delete function. */
ajs719e9742005-02-28 20:52:15 +0000902static int
paul718e3742002-12-13 20:15:29 +0000903zread_ipv4_delete (struct zserv *client, u_short length)
904{
905 int i;
906 struct stream *s;
907 struct zapi_ipv4 api;
Subbaiah Venkata6902c692012-03-27 19:21:29 -0700908 struct in_addr nexthop, *nexthop_p;
paul718e3742002-12-13 20:15:29 +0000909 unsigned long ifindex;
910 struct prefix_ipv4 p;
911 u_char nexthop_num;
912 u_char nexthop_type;
913 u_char ifname_len;
914
915 s = client->ibuf;
916 ifindex = 0;
917 nexthop.s_addr = 0;
Subbaiah Venkata6902c692012-03-27 19:21:29 -0700918 nexthop_p = NULL;
paul718e3742002-12-13 20:15:29 +0000919
920 /* Type, flags, message. */
921 api.type = stream_getc (s);
922 api.flags = stream_getc (s);
923 api.message = stream_getc (s);
G.Balajicddf3912011-11-26 21:59:32 +0400924 api.safi = stream_getw (s);
paul718e3742002-12-13 20:15:29 +0000925
926 /* IPv4 prefix. */
927 memset (&p, 0, sizeof (struct prefix_ipv4));
928 p.family = AF_INET;
929 p.prefixlen = stream_getc (s);
930 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
931
932 /* Nexthop, ifindex, distance, metric. */
933 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
934 {
935 nexthop_num = stream_getc (s);
936
937 for (i = 0; i < nexthop_num; i++)
938 {
939 nexthop_type = stream_getc (s);
940
941 switch (nexthop_type)
942 {
943 case ZEBRA_NEXTHOP_IFINDEX:
944 ifindex = stream_getl (s);
945 break;
946 case ZEBRA_NEXTHOP_IFNAME:
947 ifname_len = stream_getc (s);
paul9985f832005-02-09 15:51:56 +0000948 stream_forward_getp (s, ifname_len);
paul718e3742002-12-13 20:15:29 +0000949 break;
950 case ZEBRA_NEXTHOP_IPV4:
951 nexthop.s_addr = stream_get_ipv4 (s);
Subbaiah Venkata6902c692012-03-27 19:21:29 -0700952 nexthop_p = &nexthop;
paul718e3742002-12-13 20:15:29 +0000953 break;
Joakim Tjernlundc963c202012-07-07 17:06:13 +0200954 case ZEBRA_NEXTHOP_IPV4_IFINDEX:
955 nexthop.s_addr = stream_get_ipv4 (s);
Christian Franke23f5f7c2013-11-27 17:06:14 +0000956 nexthop_p = &nexthop;
Joakim Tjernlundc963c202012-07-07 17:06:13 +0200957 ifindex = stream_getl (s);
958 break;
paul718e3742002-12-13 20:15:29 +0000959 case ZEBRA_NEXTHOP_IPV6:
paul9985f832005-02-09 15:51:56 +0000960 stream_forward_getp (s, IPV6_MAX_BYTELEN);
paul718e3742002-12-13 20:15:29 +0000961 break;
962 }
963 }
964 }
965
966 /* Distance. */
967 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
968 api.distance = stream_getc (s);
969 else
970 api.distance = 0;
971
972 /* Metric. */
973 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
974 api.metric = stream_getl (s);
975 else
976 api.metric = 0;
977
Subbaiah Venkata6902c692012-03-27 19:21:29 -0700978 rib_delete_ipv4 (api.type, api.flags, &p, nexthop_p, ifindex,
Feng Lu0d0686f2015-05-22 11:40:02 +0200979 VRF_DEFAULT, api.safi);
ajs719e9742005-02-28 20:52:15 +0000980 return 0;
paul718e3742002-12-13 20:15:29 +0000981}
982
983/* Nexthop lookup for IPv4. */
ajs719e9742005-02-28 20:52:15 +0000984static int
paul718e3742002-12-13 20:15:29 +0000985zread_ipv4_nexthop_lookup (struct zserv *client, u_short length)
986{
987 struct in_addr addr;
Christian Frankebb97e462013-05-25 14:01:35 +0000988 char buf[BUFSIZ];
paul718e3742002-12-13 20:15:29 +0000989
990 addr.s_addr = stream_get_ipv4 (client->ibuf);
Christian Frankebb97e462013-05-25 14:01:35 +0000991 if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
992 zlog_debug("%s: looking up %s", __func__,
993 inet_ntop (AF_INET, &addr, buf, BUFSIZ));
ajs719e9742005-02-28 20:52:15 +0000994 return zsend_ipv4_nexthop_lookup (client, addr);
paul718e3742002-12-13 20:15:29 +0000995}
996
Everton Marques4e5275b2014-07-01 15:15:52 -0300997/* MRIB Nexthop lookup for IPv4. */
998static int
999zread_ipv4_nexthop_lookup_mrib (struct zserv *client, u_short length)
1000{
1001 struct in_addr addr;
David Lamparterbd078122015-01-06 19:53:24 +01001002 struct rib *rib;
Everton Marques4e5275b2014-07-01 15:15:52 -03001003
1004 addr.s_addr = stream_get_ipv4 (client->ibuf);
Feng Lu0d0686f2015-05-22 11:40:02 +02001005 rib = rib_match_ipv4_multicast (addr, NULL, VRF_DEFAULT);
David Lamparterbd078122015-01-06 19:53:24 +01001006 return zsend_ipv4_nexthop_lookup_mrib (client, addr, rib);
Everton Marques4e5275b2014-07-01 15:15:52 -03001007}
1008
paul718e3742002-12-13 20:15:29 +00001009/* Nexthop lookup for IPv4. */
ajs719e9742005-02-28 20:52:15 +00001010static int
paul718e3742002-12-13 20:15:29 +00001011zread_ipv4_import_lookup (struct zserv *client, u_short length)
1012{
1013 struct prefix_ipv4 p;
1014
1015 p.family = AF_INET;
1016 p.prefixlen = stream_getc (client->ibuf);
1017 p.prefix.s_addr = stream_get_ipv4 (client->ibuf);
1018
ajs719e9742005-02-28 20:52:15 +00001019 return zsend_ipv4_import_lookup (client, &p);
paul718e3742002-12-13 20:15:29 +00001020}
1021
1022#ifdef HAVE_IPV6
1023/* Zebra server IPv6 prefix add function. */
ajs719e9742005-02-28 20:52:15 +00001024static int
paul718e3742002-12-13 20:15:29 +00001025zread_ipv6_add (struct zserv *client, u_short length)
1026{
1027 int i;
1028 struct stream *s;
1029 struct zapi_ipv6 api;
1030 struct in6_addr nexthop;
1031 unsigned long ifindex;
1032 struct prefix_ipv6 p;
1033
1034 s = client->ibuf;
1035 ifindex = 0;
1036 memset (&nexthop, 0, sizeof (struct in6_addr));
1037
1038 /* Type, flags, message. */
1039 api.type = stream_getc (s);
1040 api.flags = stream_getc (s);
1041 api.message = stream_getc (s);
G.Balajif768f362011-11-26 22:10:39 +04001042 api.safi = stream_getw (s);
paul718e3742002-12-13 20:15:29 +00001043
1044 /* IPv4 prefix. */
1045 memset (&p, 0, sizeof (struct prefix_ipv6));
1046 p.family = AF_INET6;
1047 p.prefixlen = stream_getc (s);
1048 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
1049
1050 /* Nexthop, ifindex, distance, metric. */
1051 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
1052 {
1053 u_char nexthop_type;
1054
1055 api.nexthop_num = stream_getc (s);
1056 for (i = 0; i < api.nexthop_num; i++)
1057 {
1058 nexthop_type = stream_getc (s);
1059
1060 switch (nexthop_type)
1061 {
1062 case ZEBRA_NEXTHOP_IPV6:
1063 stream_get (&nexthop, s, 16);
1064 break;
1065 case ZEBRA_NEXTHOP_IFINDEX:
1066 ifindex = stream_getl (s);
1067 break;
1068 }
1069 }
1070 }
1071
1072 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
1073 api.distance = stream_getc (s);
1074 else
1075 api.distance = 0;
1076
1077 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
1078 api.metric = stream_getl (s);
1079 else
1080 api.metric = 0;
1081
1082 if (IN6_IS_ADDR_UNSPECIFIED (&nexthop))
Feng Lu0d0686f2015-05-22 11:40:02 +02001083 rib_add_ipv6 (api.type, api.flags, &p, NULL, ifindex,
1084 VRF_DEFAULT, zebrad.rtm_table_default, api.metric,
1085 api.distance, api.safi);
paul718e3742002-12-13 20:15:29 +00001086 else
Feng Lu0d0686f2015-05-22 11:40:02 +02001087 rib_add_ipv6 (api.type, api.flags, &p, &nexthop, ifindex,
1088 VRF_DEFAULT, zebrad.rtm_table_default, api.metric,
1089 api.distance, api.safi);
ajs719e9742005-02-28 20:52:15 +00001090 return 0;
paul718e3742002-12-13 20:15:29 +00001091}
1092
1093/* Zebra server IPv6 prefix delete function. */
ajs719e9742005-02-28 20:52:15 +00001094static int
paul718e3742002-12-13 20:15:29 +00001095zread_ipv6_delete (struct zserv *client, u_short length)
1096{
1097 int i;
1098 struct stream *s;
1099 struct zapi_ipv6 api;
1100 struct in6_addr nexthop;
1101 unsigned long ifindex;
1102 struct prefix_ipv6 p;
1103
1104 s = client->ibuf;
1105 ifindex = 0;
1106 memset (&nexthop, 0, sizeof (struct in6_addr));
1107
1108 /* Type, flags, message. */
1109 api.type = stream_getc (s);
1110 api.flags = stream_getc (s);
1111 api.message = stream_getc (s);
G.Balajif768f362011-11-26 22:10:39 +04001112 api.safi = stream_getw (s);
paul718e3742002-12-13 20:15:29 +00001113
1114 /* IPv4 prefix. */
1115 memset (&p, 0, sizeof (struct prefix_ipv6));
1116 p.family = AF_INET6;
1117 p.prefixlen = stream_getc (s);
1118 stream_get (&p.prefix, s, PSIZE (p.prefixlen));
1119
1120 /* Nexthop, ifindex, distance, metric. */
1121 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
1122 {
1123 u_char nexthop_type;
1124
1125 api.nexthop_num = stream_getc (s);
1126 for (i = 0; i < api.nexthop_num; i++)
1127 {
1128 nexthop_type = stream_getc (s);
1129
1130 switch (nexthop_type)
1131 {
1132 case ZEBRA_NEXTHOP_IPV6:
1133 stream_get (&nexthop, s, 16);
1134 break;
1135 case ZEBRA_NEXTHOP_IFINDEX:
1136 ifindex = stream_getl (s);
1137 break;
1138 }
1139 }
1140 }
1141
1142 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
1143 api.distance = stream_getc (s);
1144 else
1145 api.distance = 0;
1146 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
1147 api.metric = stream_getl (s);
1148 else
1149 api.metric = 0;
1150
1151 if (IN6_IS_ADDR_UNSPECIFIED (&nexthop))
Feng Lu0d0686f2015-05-22 11:40:02 +02001152 rib_delete_ipv6 (api.type, api.flags, &p, NULL, ifindex, VRF_DEFAULT,
1153 api.safi);
paul718e3742002-12-13 20:15:29 +00001154 else
Feng Lu0d0686f2015-05-22 11:40:02 +02001155 rib_delete_ipv6 (api.type, api.flags, &p, &nexthop, ifindex, VRF_DEFAULT,
1156 api.safi);
ajs719e9742005-02-28 20:52:15 +00001157 return 0;
paul718e3742002-12-13 20:15:29 +00001158}
1159
ajs719e9742005-02-28 20:52:15 +00001160static int
paul718e3742002-12-13 20:15:29 +00001161zread_ipv6_nexthop_lookup (struct zserv *client, u_short length)
1162{
1163 struct in6_addr addr;
1164 char buf[BUFSIZ];
1165
1166 stream_get (&addr, client->ibuf, 16);
Christian Frankea5207082013-04-11 08:24:29 +00001167 if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
1168 zlog_debug("%s: looking up %s", __func__,
1169 inet_ntop (AF_INET6, &addr, buf, BUFSIZ));
paul718e3742002-12-13 20:15:29 +00001170
ajs719e9742005-02-28 20:52:15 +00001171 return zsend_ipv6_nexthop_lookup (client, &addr);
paul718e3742002-12-13 20:15:29 +00001172}
1173#endif /* HAVE_IPV6 */
1174
hasso18a6dce2004-10-03 18:18:34 +00001175/* Register zebra server router-id information. Send current router-id */
ajs719e9742005-02-28 20:52:15 +00001176static int
hasso18a6dce2004-10-03 18:18:34 +00001177zread_router_id_add (struct zserv *client, u_short length)
1178{
1179 struct prefix p;
1180
1181 /* Router-id information is needed. */
1182 client->ridinfo = 1;
1183
1184 router_id_get (&p);
1185
ajs719e9742005-02-28 20:52:15 +00001186 return zsend_router_id_update (client,&p);
hasso18a6dce2004-10-03 18:18:34 +00001187}
1188
1189/* Unregister zebra server router-id information. */
ajs719e9742005-02-28 20:52:15 +00001190static int
hasso18a6dce2004-10-03 18:18:34 +00001191zread_router_id_delete (struct zserv *client, u_short length)
1192{
1193 client->ridinfo = 0;
ajs719e9742005-02-28 20:52:15 +00001194 return 0;
hasso18a6dce2004-10-03 18:18:34 +00001195}
1196
Vyacheslav Trushkin2ea1ab12011-12-11 18:48:47 +04001197/* Tie up route-type and client->sock */
1198static void
1199zread_hello (struct zserv *client)
1200{
1201 /* type of protocol (lib/zebra.h) */
1202 u_char proto;
1203 proto = stream_getc (client->ibuf);
1204
1205 /* accept only dynamic routing protocols */
1206 if ((proto < ZEBRA_ROUTE_MAX)
1207 && (proto > ZEBRA_ROUTE_STATIC))
1208 {
1209 zlog_notice ("client %d says hello and bids fair to announce only %s routes",
1210 client->sock, zebra_route_string(proto));
1211
1212 /* if route-type was binded by other client */
1213 if (route_type_oaths[proto])
1214 zlog_warn ("sender of %s routes changed %c->%c",
1215 zebra_route_string(proto), route_type_oaths[proto],
1216 client->sock);
1217
1218 route_type_oaths[proto] = client->sock;
1219 }
1220}
1221
1222/* If client sent routes of specific type, zebra removes it
1223 * and returns number of deleted routes.
1224 */
1225static void
1226zebra_score_rib (int client_sock)
1227{
1228 int i;
1229
1230 for (i = ZEBRA_ROUTE_RIP; i < ZEBRA_ROUTE_MAX; i++)
1231 if (client_sock == route_type_oaths[i])
1232 {
1233 zlog_notice ("client %d disconnected. %lu %s routes removed from the rib",
1234 client_sock, rib_score_proto (i), zebra_route_string (i));
1235 route_type_oaths[i] = 0;
1236 break;
1237 }
1238}
1239
paul718e3742002-12-13 20:15:29 +00001240/* Close zebra client. */
paulb9df2d22004-05-09 09:09:59 +00001241static void
paul718e3742002-12-13 20:15:29 +00001242zebra_client_close (struct zserv *client)
1243{
1244 /* Close file descriptor. */
1245 if (client->sock)
1246 {
1247 close (client->sock);
Vyacheslav Trushkin2ea1ab12011-12-11 18:48:47 +04001248 zebra_score_rib (client->sock);
paul718e3742002-12-13 20:15:29 +00001249 client->sock = -1;
1250 }
1251
1252 /* Free stream buffers. */
1253 if (client->ibuf)
1254 stream_free (client->ibuf);
1255 if (client->obuf)
1256 stream_free (client->obuf);
ajs719e9742005-02-28 20:52:15 +00001257 if (client->wb)
1258 buffer_free(client->wb);
paul718e3742002-12-13 20:15:29 +00001259
1260 /* Release threads. */
1261 if (client->t_read)
1262 thread_cancel (client->t_read);
1263 if (client->t_write)
1264 thread_cancel (client->t_write);
ajs719e9742005-02-28 20:52:15 +00001265 if (client->t_suicide)
1266 thread_cancel (client->t_suicide);
paul718e3742002-12-13 20:15:29 +00001267
1268 /* Free client structure. */
paulb21b19c2003-06-15 01:28:29 +00001269 listnode_delete (zebrad.client_list, client);
paul718e3742002-12-13 20:15:29 +00001270 XFREE (0, client);
1271}
1272
1273/* Make new client. */
paulb9df2d22004-05-09 09:09:59 +00001274static void
paul718e3742002-12-13 20:15:29 +00001275zebra_client_create (int sock)
1276{
1277 struct zserv *client;
1278
1279 client = XCALLOC (0, sizeof (struct zserv));
1280
1281 /* Make client input/output buffer. */
1282 client->sock = sock;
1283 client->ibuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
1284 client->obuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
ajs719e9742005-02-28 20:52:15 +00001285 client->wb = buffer_new(0);
paul718e3742002-12-13 20:15:29 +00001286
1287 /* Set table number. */
paulb21b19c2003-06-15 01:28:29 +00001288 client->rtm_table = zebrad.rtm_table_default;
paul718e3742002-12-13 20:15:29 +00001289
1290 /* Add this client to linked list. */
paulb21b19c2003-06-15 01:28:29 +00001291 listnode_add (zebrad.client_list, client);
paul718e3742002-12-13 20:15:29 +00001292
1293 /* Make new read thread. */
1294 zebra_event (ZEBRA_READ, sock, client);
1295}
1296
1297/* Handler of zebra service request. */
paulb9df2d22004-05-09 09:09:59 +00001298static int
paul718e3742002-12-13 20:15:29 +00001299zebra_client_read (struct thread *thread)
1300{
1301 int sock;
1302 struct zserv *client;
ajs57a14772005-04-10 15:01:56 +00001303 size_t already;
paulc1b98002006-01-16 01:54:02 +00001304 uint16_t length, command;
1305 uint8_t marker, version;
paul718e3742002-12-13 20:15:29 +00001306
1307 /* Get thread data. Reset reading thread because I'm running. */
1308 sock = THREAD_FD (thread);
1309 client = THREAD_ARG (thread);
1310 client->t_read = NULL;
1311
ajs719e9742005-02-28 20:52:15 +00001312 if (client->t_suicide)
paul718e3742002-12-13 20:15:29 +00001313 {
ajs719e9742005-02-28 20:52:15 +00001314 zebra_client_close(client);
paul718e3742002-12-13 20:15:29 +00001315 return -1;
1316 }
ajs719e9742005-02-28 20:52:15 +00001317
1318 /* Read length and command (if we don't have it already). */
ajs57a14772005-04-10 15:01:56 +00001319 if ((already = stream_get_endp(client->ibuf)) < ZEBRA_HEADER_SIZE)
ajs719e9742005-02-28 20:52:15 +00001320 {
ajs57a14772005-04-10 15:01:56 +00001321 ssize_t nbyte;
ajs719e9742005-02-28 20:52:15 +00001322 if (((nbyte = stream_read_try (client->ibuf, sock,
ajs57a14772005-04-10 15:01:56 +00001323 ZEBRA_HEADER_SIZE-already)) == 0) ||
ajs719e9742005-02-28 20:52:15 +00001324 (nbyte == -1))
1325 {
1326 if (IS_ZEBRA_DEBUG_EVENT)
1327 zlog_debug ("connection closed socket [%d]", sock);
1328 zebra_client_close (client);
1329 return -1;
1330 }
ajs57a14772005-04-10 15:01:56 +00001331 if (nbyte != (ssize_t)(ZEBRA_HEADER_SIZE-already))
ajs719e9742005-02-28 20:52:15 +00001332 {
1333 /* Try again later. */
1334 zebra_event (ZEBRA_READ, sock, client);
1335 return 0;
1336 }
ajs57a14772005-04-10 15:01:56 +00001337 already = ZEBRA_HEADER_SIZE;
ajs719e9742005-02-28 20:52:15 +00001338 }
1339
1340 /* Reset to read from the beginning of the incoming packet. */
1341 stream_set_getp(client->ibuf, 0);
1342
paulc1b98002006-01-16 01:54:02 +00001343 /* Fetch header values */
paul718e3742002-12-13 20:15:29 +00001344 length = stream_getw (client->ibuf);
paulc1b98002006-01-16 01:54:02 +00001345 marker = stream_getc (client->ibuf);
1346 version = stream_getc (client->ibuf);
1347 command = stream_getw (client->ibuf);
paul718e3742002-12-13 20:15:29 +00001348
paulc1b98002006-01-16 01:54:02 +00001349 if (marker != ZEBRA_HEADER_MARKER || version != ZSERV_VERSION)
1350 {
1351 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
1352 __func__, sock, marker, version);
1353 zebra_client_close (client);
1354 return -1;
1355 }
ajs719e9742005-02-28 20:52:15 +00001356 if (length < ZEBRA_HEADER_SIZE)
paul718e3742002-12-13 20:15:29 +00001357 {
ajs57a14772005-04-10 15:01:56 +00001358 zlog_warn("%s: socket %d message length %u is less than header size %d",
1359 __func__, sock, length, ZEBRA_HEADER_SIZE);
1360 zebra_client_close (client);
1361 return -1;
1362 }
1363 if (length > STREAM_SIZE(client->ibuf))
1364 {
1365 zlog_warn("%s: socket %d message length %u exceeds buffer size %lu",
1366 __func__, sock, length, (u_long)STREAM_SIZE(client->ibuf));
paul718e3742002-12-13 20:15:29 +00001367 zebra_client_close (client);
1368 return -1;
1369 }
1370
paul718e3742002-12-13 20:15:29 +00001371 /* Read rest of data. */
ajs57a14772005-04-10 15:01:56 +00001372 if (already < length)
paul718e3742002-12-13 20:15:29 +00001373 {
ajs57a14772005-04-10 15:01:56 +00001374 ssize_t nbyte;
1375 if (((nbyte = stream_read_try (client->ibuf, sock,
1376 length-already)) == 0) ||
1377 (nbyte == -1))
paul718e3742002-12-13 20:15:29 +00001378 {
1379 if (IS_ZEBRA_DEBUG_EVENT)
ajsb6178002004-12-07 21:12:56 +00001380 zlog_debug ("connection closed [%d] when reading zebra data", sock);
paul718e3742002-12-13 20:15:29 +00001381 zebra_client_close (client);
1382 return -1;
1383 }
ajs57a14772005-04-10 15:01:56 +00001384 if (nbyte != (ssize_t)(length-already))
ajs719e9742005-02-28 20:52:15 +00001385 {
1386 /* Try again later. */
1387 zebra_event (ZEBRA_READ, sock, client);
1388 return 0;
1389 }
paul718e3742002-12-13 20:15:29 +00001390 }
1391
ajs719e9742005-02-28 20:52:15 +00001392 length -= ZEBRA_HEADER_SIZE;
1393
paul718e3742002-12-13 20:15:29 +00001394 /* Debug packet information. */
1395 if (IS_ZEBRA_DEBUG_EVENT)
ajsb6178002004-12-07 21:12:56 +00001396 zlog_debug ("zebra message comes from socket [%d]", sock);
paul718e3742002-12-13 20:15:29 +00001397
1398 if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
ajsb6178002004-12-07 21:12:56 +00001399 zlog_debug ("zebra message received [%s] %d",
Paul Jakma66859782006-05-15 17:00:37 +00001400 zserv_command_string (command), length);
paul718e3742002-12-13 20:15:29 +00001401
1402 switch (command)
1403 {
hasso18a6dce2004-10-03 18:18:34 +00001404 case ZEBRA_ROUTER_ID_ADD:
1405 zread_router_id_add (client, length);
1406 break;
1407 case ZEBRA_ROUTER_ID_DELETE:
1408 zread_router_id_delete (client, length);
1409 break;
paul718e3742002-12-13 20:15:29 +00001410 case ZEBRA_INTERFACE_ADD:
1411 zread_interface_add (client, length);
1412 break;
1413 case ZEBRA_INTERFACE_DELETE:
1414 zread_interface_delete (client, length);
1415 break;
1416 case ZEBRA_IPV4_ROUTE_ADD:
1417 zread_ipv4_add (client, length);
1418 break;
1419 case ZEBRA_IPV4_ROUTE_DELETE:
1420 zread_ipv4_delete (client, length);
1421 break;
1422#ifdef HAVE_IPV6
1423 case ZEBRA_IPV6_ROUTE_ADD:
1424 zread_ipv6_add (client, length);
1425 break;
1426 case ZEBRA_IPV6_ROUTE_DELETE:
1427 zread_ipv6_delete (client, length);
1428 break;
1429#endif /* HAVE_IPV6 */
1430 case ZEBRA_REDISTRIBUTE_ADD:
1431 zebra_redistribute_add (command, client, length);
1432 break;
1433 case ZEBRA_REDISTRIBUTE_DELETE:
1434 zebra_redistribute_delete (command, client, length);
1435 break;
1436 case ZEBRA_REDISTRIBUTE_DEFAULT_ADD:
1437 zebra_redistribute_default_add (command, client, length);
1438 break;
1439 case ZEBRA_REDISTRIBUTE_DEFAULT_DELETE:
1440 zebra_redistribute_default_delete (command, client, length);
1441 break;
1442 case ZEBRA_IPV4_NEXTHOP_LOOKUP:
1443 zread_ipv4_nexthop_lookup (client, length);
1444 break;
Everton Marques4e5275b2014-07-01 15:15:52 -03001445 case ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB:
1446 zread_ipv4_nexthop_lookup_mrib (client, length);
1447 break;
paul718e3742002-12-13 20:15:29 +00001448#ifdef HAVE_IPV6
1449 case ZEBRA_IPV6_NEXTHOP_LOOKUP:
1450 zread_ipv6_nexthop_lookup (client, length);
1451 break;
1452#endif /* HAVE_IPV6 */
1453 case ZEBRA_IPV4_IMPORT_LOOKUP:
1454 zread_ipv4_import_lookup (client, length);
1455 break;
Vyacheslav Trushkin2ea1ab12011-12-11 18:48:47 +04001456 case ZEBRA_HELLO:
1457 zread_hello (client);
1458 break;
paul718e3742002-12-13 20:15:29 +00001459 default:
1460 zlog_info ("Zebra received unknown command %d", command);
1461 break;
1462 }
1463
ajs719e9742005-02-28 20:52:15 +00001464 if (client->t_suicide)
1465 {
1466 /* No need to wait for thread callback, just kill immediately. */
1467 zebra_client_close(client);
1468 return -1;
1469 }
1470
paul718e3742002-12-13 20:15:29 +00001471 stream_reset (client->ibuf);
1472 zebra_event (ZEBRA_READ, sock, client);
paul718e3742002-12-13 20:15:29 +00001473 return 0;
1474}
1475
paul718e3742002-12-13 20:15:29 +00001476
1477/* Accept code of zebra server socket. */
paulb9df2d22004-05-09 09:09:59 +00001478static int
paul718e3742002-12-13 20:15:29 +00001479zebra_accept (struct thread *thread)
1480{
1481 int accept_sock;
1482 int client_sock;
1483 struct sockaddr_in client;
1484 socklen_t len;
1485
1486 accept_sock = THREAD_FD (thread);
1487
ajs719e9742005-02-28 20:52:15 +00001488 /* Reregister myself. */
1489 zebra_event (ZEBRA_SERV, accept_sock, NULL);
1490
paul718e3742002-12-13 20:15:29 +00001491 len = sizeof (struct sockaddr_in);
1492 client_sock = accept (accept_sock, (struct sockaddr *) &client, &len);
1493
1494 if (client_sock < 0)
1495 {
ajs6099b3b2004-11-20 02:06:59 +00001496 zlog_warn ("Can't accept zebra socket: %s", safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001497 return -1;
1498 }
1499
paulccf35572003-03-01 11:42:20 +00001500 /* Make client socket non-blocking. */
ajs719e9742005-02-28 20:52:15 +00001501 set_nonblocking(client_sock);
paul865b8522005-01-05 08:30:35 +00001502
paul718e3742002-12-13 20:15:29 +00001503 /* Create new zebra client. */
1504 zebra_client_create (client_sock);
1505
paul718e3742002-12-13 20:15:29 +00001506 return 0;
1507}
1508
paulb9df2d22004-05-09 09:09:59 +00001509#ifdef HAVE_TCP_ZEBRA
paul718e3742002-12-13 20:15:29 +00001510/* Make zebra's server socket. */
paulb9df2d22004-05-09 09:09:59 +00001511static void
paul718e3742002-12-13 20:15:29 +00001512zebra_serv ()
1513{
1514 int ret;
1515 int accept_sock;
1516 struct sockaddr_in addr;
1517
1518 accept_sock = socket (AF_INET, SOCK_STREAM, 0);
1519
1520 if (accept_sock < 0)
1521 {
paul3d1dc852005-04-05 00:45:23 +00001522 zlog_warn ("Can't create zserv stream socket: %s",
1523 safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001524 zlog_warn ("zebra can't provice full functionality due to above error");
1525 return;
1526 }
1527
Vyacheslav Trushkin2ea1ab12011-12-11 18:48:47 +04001528 memset (&route_type_oaths, 0, sizeof (route_type_oaths));
paul718e3742002-12-13 20:15:29 +00001529 memset (&addr, 0, sizeof (struct sockaddr_in));
1530 addr.sin_family = AF_INET;
1531 addr.sin_port = htons (ZEBRA_PORT);
Paul Jakma6f0e3f62007-05-10 02:38:51 +00001532#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
paul718e3742002-12-13 20:15:29 +00001533 addr.sin_len = sizeof (struct sockaddr_in);
Paul Jakma6f0e3f62007-05-10 02:38:51 +00001534#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
paul718e3742002-12-13 20:15:29 +00001535 addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
1536
1537 sockopt_reuseaddr (accept_sock);
1538 sockopt_reuseport (accept_sock);
1539
pauledd7c242003-06-04 13:59:38 +00001540 if ( zserv_privs.change(ZPRIVS_RAISE) )
1541 zlog (NULL, LOG_ERR, "Can't raise privileges");
1542
paul718e3742002-12-13 20:15:29 +00001543 ret = bind (accept_sock, (struct sockaddr *)&addr,
1544 sizeof (struct sockaddr_in));
1545 if (ret < 0)
1546 {
paul3d1dc852005-04-05 00:45:23 +00001547 zlog_warn ("Can't bind to stream socket: %s",
1548 safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001549 zlog_warn ("zebra can't provice full functionality due to above error");
1550 close (accept_sock); /* Avoid sd leak. */
1551 return;
1552 }
pauledd7c242003-06-04 13:59:38 +00001553
1554 if ( zserv_privs.change(ZPRIVS_LOWER) )
1555 zlog (NULL, LOG_ERR, "Can't lower privileges");
paul718e3742002-12-13 20:15:29 +00001556
1557 ret = listen (accept_sock, 1);
1558 if (ret < 0)
1559 {
paul3d1dc852005-04-05 00:45:23 +00001560 zlog_warn ("Can't listen to stream socket: %s",
1561 safe_strerror (errno));
paul718e3742002-12-13 20:15:29 +00001562 zlog_warn ("zebra can't provice full functionality due to above error");
1563 close (accept_sock); /* Avoid sd leak. */
1564 return;
1565 }
1566
1567 zebra_event (ZEBRA_SERV, accept_sock, NULL);
1568}
David Lamparter4b6c3322015-04-21 09:47:57 +02001569#else /* HAVE_TCP_ZEBRA */
paul718e3742002-12-13 20:15:29 +00001570
1571/* For sockaddr_un. */
1572#include <sys/un.h>
1573
1574/* zebra server UNIX domain socket. */
paulb9df2d22004-05-09 09:09:59 +00001575static void
hassofce954f2004-10-07 20:29:24 +00001576zebra_serv_un (const char *path)
paul718e3742002-12-13 20:15:29 +00001577{
1578 int ret;
1579 int sock, len;
1580 struct sockaddr_un serv;
1581 mode_t old_mask;
1582
1583 /* First of all, unlink existing socket */
1584 unlink (path);
1585
1586 /* Set umask */
1587 old_mask = umask (0077);
1588
1589 /* Make UNIX domain socket. */
1590 sock = socket (AF_UNIX, SOCK_STREAM, 0);
1591 if (sock < 0)
1592 {
paul3d1dc852005-04-05 00:45:23 +00001593 zlog_warn ("Can't create zserv unix socket: %s",
1594 safe_strerror (errno));
1595 zlog_warn ("zebra can't provide full functionality due to above error");
paul718e3742002-12-13 20:15:29 +00001596 return;
1597 }
1598
Vyacheslav Trushkin2ea1ab12011-12-11 18:48:47 +04001599 memset (&route_type_oaths, 0, sizeof (route_type_oaths));
1600
paul718e3742002-12-13 20:15:29 +00001601 /* Make server socket. */
1602 memset (&serv, 0, sizeof (struct sockaddr_un));
1603 serv.sun_family = AF_UNIX;
1604 strncpy (serv.sun_path, path, strlen (path));
Paul Jakma6f0e3f62007-05-10 02:38:51 +00001605#ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
paul718e3742002-12-13 20:15:29 +00001606 len = serv.sun_len = SUN_LEN(&serv);
1607#else
1608 len = sizeof (serv.sun_family) + strlen (serv.sun_path);
Paul Jakma6f0e3f62007-05-10 02:38:51 +00001609#endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
paul718e3742002-12-13 20:15:29 +00001610
1611 ret = bind (sock, (struct sockaddr *) &serv, len);
1612 if (ret < 0)
1613 {
paul3d1dc852005-04-05 00:45:23 +00001614 zlog_warn ("Can't bind to unix socket %s: %s",
1615 path, safe_strerror (errno));
1616 zlog_warn ("zebra can't provide full functionality due to above error");
paul718e3742002-12-13 20:15:29 +00001617 close (sock);
1618 return;
1619 }
1620
1621 ret = listen (sock, 5);
1622 if (ret < 0)
1623 {
paul3d1dc852005-04-05 00:45:23 +00001624 zlog_warn ("Can't listen to unix socket %s: %s",
1625 path, safe_strerror (errno));
1626 zlog_warn ("zebra can't provide full functionality due to above error");
paul718e3742002-12-13 20:15:29 +00001627 close (sock);
1628 return;
1629 }
1630
1631 umask (old_mask);
1632
1633 zebra_event (ZEBRA_SERV, sock, NULL);
1634}
David Lamparter4b6c3322015-04-21 09:47:57 +02001635#endif /* HAVE_TCP_ZEBRA */
David Lamparter6b0655a2014-06-04 06:53:35 +02001636
paul718e3742002-12-13 20:15:29 +00001637
paulb9df2d22004-05-09 09:09:59 +00001638static void
paul718e3742002-12-13 20:15:29 +00001639zebra_event (enum event event, int sock, struct zserv *client)
1640{
1641 switch (event)
1642 {
1643 case ZEBRA_SERV:
paulb21b19c2003-06-15 01:28:29 +00001644 thread_add_read (zebrad.master, zebra_accept, client, sock);
paul718e3742002-12-13 20:15:29 +00001645 break;
1646 case ZEBRA_READ:
1647 client->t_read =
paulb21b19c2003-06-15 01:28:29 +00001648 thread_add_read (zebrad.master, zebra_client_read, client, sock);
paul718e3742002-12-13 20:15:29 +00001649 break;
1650 case ZEBRA_WRITE:
1651 /**/
1652 break;
1653 }
1654}
David Lamparter6b0655a2014-06-04 06:53:35 +02001655
paul718e3742002-12-13 20:15:29 +00001656/* Display default rtm_table for all clients. */
1657DEFUN (show_table,
1658 show_table_cmd,
1659 "show table",
1660 SHOW_STR
1661 "default routing table to use for all clients\n")
1662{
paulb21b19c2003-06-15 01:28:29 +00001663 vty_out (vty, "table %d%s", zebrad.rtm_table_default,
paul718e3742002-12-13 20:15:29 +00001664 VTY_NEWLINE);
1665 return CMD_SUCCESS;
1666}
1667
1668DEFUN (config_table,
1669 config_table_cmd,
1670 "table TABLENO",
1671 "Configure target kernel routing table\n"
1672 "TABLE integer\n")
1673{
paulb21b19c2003-06-15 01:28:29 +00001674 zebrad.rtm_table_default = strtol (argv[0], (char**)0, 10);
paul718e3742002-12-13 20:15:29 +00001675 return CMD_SUCCESS;
1676}
1677
hasso647e4f12003-05-25 11:43:52 +00001678DEFUN (ip_forwarding,
1679 ip_forwarding_cmd,
1680 "ip forwarding",
1681 IP_STR
1682 "Turn on IP forwarding")
1683{
1684 int ret;
1685
1686 ret = ipforward ();
hassob71f00f2004-10-13 12:20:35 +00001687 if (ret == 0)
1688 ret = ipforward_on ();
hasso647e4f12003-05-25 11:43:52 +00001689
hasso647e4f12003-05-25 11:43:52 +00001690 if (ret == 0)
1691 {
1692 vty_out (vty, "Can't turn on IP forwarding%s", VTY_NEWLINE);
1693 return CMD_WARNING;
1694 }
1695
1696 return CMD_SUCCESS;
1697}
1698
paul718e3742002-12-13 20:15:29 +00001699DEFUN (no_ip_forwarding,
1700 no_ip_forwarding_cmd,
1701 "no ip forwarding",
1702 NO_STR
1703 IP_STR
1704 "Turn off IP forwarding")
1705{
1706 int ret;
1707
1708 ret = ipforward ();
hassob71f00f2004-10-13 12:20:35 +00001709 if (ret != 0)
1710 ret = ipforward_off ();
paul718e3742002-12-13 20:15:29 +00001711
paul718e3742002-12-13 20:15:29 +00001712 if (ret != 0)
1713 {
1714 vty_out (vty, "Can't turn off IP forwarding%s", VTY_NEWLINE);
1715 return CMD_WARNING;
1716 }
1717
1718 return CMD_SUCCESS;
1719}
1720
1721/* This command is for debugging purpose. */
1722DEFUN (show_zebra_client,
1723 show_zebra_client_cmd,
1724 "show zebra client",
1725 SHOW_STR
1726 "Zebra information"
1727 "Client information")
1728{
hasso52dc7ee2004-09-23 19:18:23 +00001729 struct listnode *node;
paul718e3742002-12-13 20:15:29 +00001730 struct zserv *client;
1731
paul1eb8ef22005-04-07 07:30:20 +00001732 for (ALL_LIST_ELEMENTS_RO (zebrad.client_list, node, client))
1733 vty_out (vty, "Client fd %d%s", client->sock, VTY_NEWLINE);
1734
paul718e3742002-12-13 20:15:29 +00001735 return CMD_SUCCESS;
1736}
1737
1738/* Table configuration write function. */
paulb9df2d22004-05-09 09:09:59 +00001739static int
paul718e3742002-12-13 20:15:29 +00001740config_write_table (struct vty *vty)
1741{
paulb21b19c2003-06-15 01:28:29 +00001742 if (zebrad.rtm_table_default)
1743 vty_out (vty, "table %d%s", zebrad.rtm_table_default,
paul718e3742002-12-13 20:15:29 +00001744 VTY_NEWLINE);
1745 return 0;
1746}
1747
1748/* table node for routing tables. */
Stephen Hemminger7fc626d2008-12-01 11:10:34 -08001749static struct cmd_node table_node =
paul718e3742002-12-13 20:15:29 +00001750{
1751 TABLE_NODE,
1752 "", /* This node has no interface. */
1753 1
1754};
David Lamparter6b0655a2014-06-04 06:53:35 +02001755
paul718e3742002-12-13 20:15:29 +00001756/* Only display ip forwarding is enabled or not. */
1757DEFUN (show_ip_forwarding,
1758 show_ip_forwarding_cmd,
1759 "show ip forwarding",
1760 SHOW_STR
1761 IP_STR
1762 "IP forwarding status\n")
1763{
1764 int ret;
1765
1766 ret = ipforward ();
1767
1768 if (ret == 0)
1769 vty_out (vty, "IP forwarding is off%s", VTY_NEWLINE);
1770 else
1771 vty_out (vty, "IP forwarding is on%s", VTY_NEWLINE);
1772 return CMD_SUCCESS;
1773}
1774
1775#ifdef HAVE_IPV6
1776/* Only display ipv6 forwarding is enabled or not. */
1777DEFUN (show_ipv6_forwarding,
1778 show_ipv6_forwarding_cmd,
1779 "show ipv6 forwarding",
1780 SHOW_STR
1781 "IPv6 information\n"
1782 "Forwarding status\n")
1783{
1784 int ret;
1785
1786 ret = ipforward_ipv6 ();
1787
1788 switch (ret)
1789 {
1790 case -1:
1791 vty_out (vty, "ipv6 forwarding is unknown%s", VTY_NEWLINE);
1792 break;
1793 case 0:
1794 vty_out (vty, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE);
1795 break;
1796 case 1:
1797 vty_out (vty, "ipv6 forwarding is %s%s", "on", VTY_NEWLINE);
1798 break;
1799 default:
1800 vty_out (vty, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE);
1801 break;
1802 }
1803 return CMD_SUCCESS;
1804}
1805
hasso55906722004-02-11 22:42:16 +00001806DEFUN (ipv6_forwarding,
1807 ipv6_forwarding_cmd,
1808 "ipv6 forwarding",
1809 IPV6_STR
1810 "Turn on IPv6 forwarding")
1811{
1812 int ret;
1813
hasso41d3fc92004-04-06 11:59:00 +00001814 ret = ipforward_ipv6 ();
hassob71f00f2004-10-13 12:20:35 +00001815 if (ret == 0)
1816 ret = ipforward_ipv6_on ();
hasso41d3fc92004-04-06 11:59:00 +00001817
hasso41d3fc92004-04-06 11:59:00 +00001818 if (ret == 0)
1819 {
hasso55906722004-02-11 22:42:16 +00001820 vty_out (vty, "Can't turn on IPv6 forwarding%s", VTY_NEWLINE);
1821 return CMD_WARNING;
1822 }
1823
1824 return CMD_SUCCESS;
1825}
1826
paul718e3742002-12-13 20:15:29 +00001827DEFUN (no_ipv6_forwarding,
1828 no_ipv6_forwarding_cmd,
1829 "no ipv6 forwarding",
1830 NO_STR
hasso55906722004-02-11 22:42:16 +00001831 IPV6_STR
1832 "Turn off IPv6 forwarding")
paul718e3742002-12-13 20:15:29 +00001833{
1834 int ret;
1835
hasso41d3fc92004-04-06 11:59:00 +00001836 ret = ipforward_ipv6 ();
hassob71f00f2004-10-13 12:20:35 +00001837 if (ret != 0)
1838 ret = ipforward_ipv6_off ();
hasso41d3fc92004-04-06 11:59:00 +00001839
paul718e3742002-12-13 20:15:29 +00001840 if (ret != 0)
1841 {
1842 vty_out (vty, "Can't turn off IPv6 forwarding%s", VTY_NEWLINE);
1843 return CMD_WARNING;
1844 }
1845
1846 return CMD_SUCCESS;
1847}
1848
1849#endif /* HAVE_IPV6 */
1850
1851/* IPForwarding configuration write function. */
ajs719e9742005-02-28 20:52:15 +00001852static int
paul718e3742002-12-13 20:15:29 +00001853config_write_forwarding (struct vty *vty)
1854{
hasso18a6dce2004-10-03 18:18:34 +00001855 /* FIXME: Find better place for that. */
1856 router_id_write (vty);
1857
paul3e0b3a52004-08-23 18:58:32 +00001858 if (ipforward ())
1859 vty_out (vty, "ip forwarding%s", VTY_NEWLINE);
paul718e3742002-12-13 20:15:29 +00001860#ifdef HAVE_IPV6
paul3e0b3a52004-08-23 18:58:32 +00001861 if (ipforward_ipv6 ())
1862 vty_out (vty, "ipv6 forwarding%s", VTY_NEWLINE);
paul718e3742002-12-13 20:15:29 +00001863#endif /* HAVE_IPV6 */
1864 vty_out (vty, "!%s", VTY_NEWLINE);
1865 return 0;
1866}
1867
1868/* table node for routing tables. */
Stephen Hemminger7fc626d2008-12-01 11:10:34 -08001869static struct cmd_node forwarding_node =
paul718e3742002-12-13 20:15:29 +00001870{
1871 FORWARDING_NODE,
1872 "", /* This node has no interface. */
1873 1
1874};
1875
David Lamparter6b0655a2014-06-04 06:53:35 +02001876
paul718e3742002-12-13 20:15:29 +00001877/* Initialisation of zebra and installation of commands. */
1878void
paula1ac18c2005-06-28 17:17:12 +00001879zebra_init (void)
paul718e3742002-12-13 20:15:29 +00001880{
1881 /* Client list init. */
paulb21b19c2003-06-15 01:28:29 +00001882 zebrad.client_list = list_new ();
paul718e3742002-12-13 20:15:29 +00001883
paul718e3742002-12-13 20:15:29 +00001884 /* Install configuration write function. */
1885 install_node (&table_node, config_write_table);
1886 install_node (&forwarding_node, config_write_forwarding);
1887
1888 install_element (VIEW_NODE, &show_ip_forwarding_cmd);
1889 install_element (ENABLE_NODE, &show_ip_forwarding_cmd);
hasso647e4f12003-05-25 11:43:52 +00001890 install_element (CONFIG_NODE, &ip_forwarding_cmd);
paul718e3742002-12-13 20:15:29 +00001891 install_element (CONFIG_NODE, &no_ip_forwarding_cmd);
1892 install_element (ENABLE_NODE, &show_zebra_client_cmd);
1893
1894#ifdef HAVE_NETLINK
1895 install_element (VIEW_NODE, &show_table_cmd);
1896 install_element (ENABLE_NODE, &show_table_cmd);
1897 install_element (CONFIG_NODE, &config_table_cmd);
1898#endif /* HAVE_NETLINK */
1899
1900#ifdef HAVE_IPV6
1901 install_element (VIEW_NODE, &show_ipv6_forwarding_cmd);
1902 install_element (ENABLE_NODE, &show_ipv6_forwarding_cmd);
hasso55906722004-02-11 22:42:16 +00001903 install_element (CONFIG_NODE, &ipv6_forwarding_cmd);
paul718e3742002-12-13 20:15:29 +00001904 install_element (CONFIG_NODE, &no_ipv6_forwarding_cmd);
1905#endif /* HAVE_IPV6 */
Paul Jakma7514fb72007-05-02 16:05:35 +00001906
1907 /* Route-map */
1908 zebra_route_map_init ();
paul718e3742002-12-13 20:15:29 +00001909}
Denis Ovsienko97be79f2009-07-24 20:45:31 +04001910
1911/* Make zebra server socket, wiping any existing one (see bug #403). */
1912void
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +04001913zebra_zserv_socket_init (char *path)
Denis Ovsienko97be79f2009-07-24 20:45:31 +04001914{
1915#ifdef HAVE_TCP_ZEBRA
1916 zebra_serv ();
1917#else
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +04001918 zebra_serv_un (path ? path : ZEBRA_SERV_PATH);
Denis Ovsienko97be79f2009-07-24 20:45:31 +04001919#endif /* HAVE_TCP_ZEBRA */
1920}