]> git.proxmox.com Git - mirror_frr.git/blame - zebra/zserv.c
zebra: static label binding
[mirror_frr.git] / zebra / zserv.c
CommitLineData
718e3744 1/* 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"
4a1ab8e4 30#include "zebra_memory.h"
718e3744 31#include "table.h"
32#include "rib.h"
33#include "network.h"
34#include "sockunion.h"
35#include "log.h"
36#include "zclient.h"
edd7c245 37#include "privs.h"
719e9741 38#include "network.h"
39#include "buffer.h"
fb018d25 40#include "nexthop.h"
78104b9b 41#include "vrf.h"
718e3744 42
43#include "zebra/zserv.h"
7c551956
DS
44#include "zebra/zebra_ns.h"
45#include "zebra/zebra_vrf.h"
18a6dce6 46#include "zebra/router-id.h"
718e3744 47#include "zebra/redistribute.h"
48#include "zebra/debug.h"
49#include "zebra/ipforward.h"
fb018d25 50#include "zebra/zebra_rnh.h"
5c610faf 51#include "zebra/rt_netlink.h"
88177fe3
DS
52#include "zebra/interface.h"
53#include "zebra/zebra_ptm.h"
4a04e5f7 54#include "zebra/rtadv.h"
ce549947 55#include "zebra/zebra_mpls.h"
e3be0432 56#include "zebra/zebra_mroute.h"
fea12efb 57#include "zebra/label_manager.h"
6b0655a2 58
718e3744 59/* Event list of zebra. */
60enum event { ZEBRA_SERV, ZEBRA_READ, ZEBRA_WRITE };
61
b9df2d25 62static void zebra_event (enum event event, int sock, struct zserv *client);
ccf3557b 63
edd7c245 64extern struct zebra_privs_t zserv_privs;
6b0655a2 65
719e9741 66static void zebra_client_close (struct zserv *client);
ccf3557b 67
719e9741 68static int
69zserv_delayed_close(struct thread *thread)
ccf3557b 70{
719e9741 71 struct zserv *client = THREAD_ARG(thread);
ccf3557b 72
719e9741 73 client->t_suicide = NULL;
74 zebra_client_close(client);
ccf3557b 75 return 0;
76}
77
719e9741 78static int
79zserv_flush_data(struct thread *thread)
ccf3557b 80{
719e9741 81 struct zserv *client = THREAD_ARG(thread);
ccf3557b 82
719e9741 83 client->t_write = NULL;
84 if (client->t_suicide)
ccf3557b 85 {
719e9741 86 zebra_client_close(client);
87 return -1;
ccf3557b 88 }
719e9741 89 switch (buffer_flush_available(client->wb, client->sock))
ccf3557b 90 {
719e9741 91 case BUFFER_ERROR:
92 zlog_warn("%s: buffer_flush_available failed on zserv client fd %d, "
93 "closing", __func__, client->sock);
94 zebra_client_close(client);
4b21f878 95 client = NULL;
719e9741 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;
ccf3557b 103 }
04b02fda 104
4b21f878
DS
105 if (client)
106 client->last_write_time = monotime(NULL);
719e9741 107 return 0;
108}
ccf3557b 109
fb018d25 110int
719e9741 111zebra_server_send_message(struct zserv *client)
112{
113 if (client->t_suicide)
114 return -1;
04b02fda 115
fea12efb 116 if (client->is_synchronous)
117 return 0;
118
04b02fda 119 stream_set_getp(client->obuf, 0);
80ab3edf 120 client->last_write_cmd = stream_getw_from(client->obuf, 6);
719e9741 121 switch (buffer_write(client->wb, client->sock, STREAM_DATA(client->obuf),
122 stream_get_endp(client->obuf)))
123 {
124 case BUFFER_ERROR:
125 zlog_warn("%s: buffer_write failed to zserv client fd %d, closing",
126 __func__, client->sock);
127 /* Schedule a delayed close since many of the functions that call this
128 one do not check the return code. They do not allow for the
129 possibility that an I/O error may have caused the client to be
130 deleted. */
131 client->t_suicide = thread_add_event(zebrad.master, zserv_delayed_close,
132 client, 0);
133 return -1;
719e9741 134 case BUFFER_EMPTY:
135 THREAD_OFF(client->t_write);
136 break;
137 case BUFFER_PENDING:
138 THREAD_WRITE_ON(zebrad.master, client->t_write,
139 zserv_flush_data, client, client->sock);
140 break;
141 }
04b02fda 142
cf672a86 143 client->last_write_time = monotime(NULL);
ccf3557b 144 return 0;
145}
146
fb018d25 147void
7076bb2f 148zserv_create_header (struct stream *s, uint16_t cmd, vrf_id_t vrf_id)
c1b9800a 149{
150 /* length placeholder, caller can update */
151 stream_putw (s, ZEBRA_HEADER_SIZE);
152 stream_putc (s, ZEBRA_HEADER_MARKER);
153 stream_putc (s, ZSERV_VERSION);
7076bb2f 154 stream_putw (s, vrf_id);
c1b9800a 155 stream_putw (s, cmd);
156}
157
51d4ef83
JB
158static void
159zserv_encode_interface (struct stream *s, struct interface *ifp)
160{
161 /* Interface information. */
162 stream_put (s, ifp->name, INTERFACE_NAMSIZ);
163 stream_putl (s, ifp->ifindex);
164 stream_putc (s, ifp->status);
165 stream_putq (s, ifp->flags);
244c1cdc
DS
166 stream_putc (s, ifp->ptm_enable);
167 stream_putc (s, ifp->ptm_status);
51d4ef83 168 stream_putl (s, ifp->metric);
2d7f0d76 169 stream_putl (s, ifp->speed);
51d4ef83
JB
170 stream_putl (s, ifp->mtu);
171 stream_putl (s, ifp->mtu6);
172 stream_putl (s, ifp->bandwidth);
8ccc7e80 173 stream_putl (s, ifp->ll_type);
51d4ef83
JB
174 stream_putl (s, ifp->hw_addr_len);
175 if (ifp->hw_addr_len)
176 stream_put (s, ifp->hw_addr, ifp->hw_addr_len);
51d4ef83 177
16f1b9ee
OD
178 /* Then, Traffic Engineering parameters if any */
179 if (HAS_LINK_PARAMS(ifp) && IS_LINK_PARAMS_SET(ifp->link_params))
180 {
181 stream_putc (s, 1);
182 zebra_interface_link_params_write (s, ifp);
183 }
184 else
185 stream_putc (s, 0);
186
51d4ef83
JB
187 /* Write packet size. */
188 stream_putw_at (s, 0, stream_get_endp (s));
189}
190
12f6fb97 191static void
9e1bf607 192zserv_encode_vrf (struct stream *s, struct zebra_vrf *zvrf)
12f6fb97
DS
193{
194 /* Interface information. */
661512bf 195 stream_put (s, zvrf_name (zvrf), VRF_NAMSIZ);
12f6fb97
DS
196
197 /* Write packet size. */
198 stream_putw_at (s, 0, stream_get_endp (s));
199}
200
718e3744 201/* Interface is added. Send ZEBRA_INTERFACE_ADD to client. */
b9df2d25 202/*
203 * This function is called in the following situations:
204 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
205 * from the client.
206 * - at startup, when zebra figures out the available interfaces
207 * - when an interface is added (where support for
208 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
209 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
210 * received)
211 */
718e3744 212int
213zsend_interface_add (struct zserv *client, struct interface *ifp)
214{
215 struct stream *s;
216
718e3744 217 s = client->obuf;
218 stream_reset (s);
219
7076bb2f 220 zserv_create_header (s, ZEBRA_INTERFACE_ADD, ifp->vrf_id);
51d4ef83 221 zserv_encode_interface (s, ifp);
718e3744 222
04b02fda 223 client->ifadd_cnt++;
719e9741 224 return zebra_server_send_message(client);
718e3744 225}
226
227/* Interface deletion from zebra daemon. */
228int
229zsend_interface_delete (struct zserv *client, struct interface *ifp)
230{
231 struct stream *s;
232
718e3744 233 s = client->obuf;
234 stream_reset (s);
718e3744 235
7076bb2f 236 zserv_create_header (s, ZEBRA_INTERFACE_DELETE, ifp->vrf_id);
51d4ef83 237 zserv_encode_interface (s, ifp);
718e3744 238
04b02fda 239 client->ifdel_cnt++;
719e9741 240 return zebra_server_send_message (client);
718e3744 241}
242
12f6fb97 243int
9e1bf607 244zsend_vrf_add (struct zserv *client, struct zebra_vrf *zvrf)
12f6fb97
DS
245{
246 struct stream *s;
247
248 s = client->obuf;
249 stream_reset (s);
250
661512bf 251 zserv_create_header (s, ZEBRA_VRF_ADD, zvrf_id (zvrf));
9e1bf607 252 zserv_encode_vrf (s, zvrf);
12f6fb97
DS
253
254 client->vrfadd_cnt++;
255 return zebra_server_send_message(client);
256}
257
258/* VRF deletion from zebra daemon. */
259int
9e1bf607 260zsend_vrf_delete (struct zserv *client, struct zebra_vrf *zvrf)
12f6fb97
DS
261{
262 struct stream *s;
263
264 s = client->obuf;
265 stream_reset (s);
266
661512bf 267 zserv_create_header (s, ZEBRA_VRF_DELETE, zvrf_id (zvrf));
9e1bf607 268 zserv_encode_vrf (s, zvrf);
12f6fb97
DS
269
270 client->vrfdel_cnt++;
271 return zebra_server_send_message (client);
272}
273
16f1b9ee
OD
274int
275zsend_interface_link_params (struct zserv *client, struct interface *ifp)
276{
277 struct stream *s;
278
279 /* Check this client need interface information. */
280 if (! client->ifinfo)
281 return 0;
282
283 if (!ifp->link_params)
284 return 0;
285 s = client->obuf;
286 stream_reset (s);
287
288 zserv_create_header (s, ZEBRA_INTERFACE_LINK_PARAMS, ifp->vrf_id);
289
290 /* Add Interface Index */
291 stream_putl (s, ifp->ifindex);
292
293 /* Then TE Link Parameters */
294 if (zebra_interface_link_params_write (s, ifp) == 0)
295 return 0;
296
297 /* Write packet size. */
298 stream_putw_at (s, 0, stream_get_endp (s));
299
300 return zebra_server_send_message (client);
301}
302
b9df2d25 303/* Interface address is added/deleted. Send ZEBRA_INTERFACE_ADDRESS_ADD or
304 * ZEBRA_INTERFACE_ADDRESS_DELETE to the client.
305 *
306 * A ZEBRA_INTERFACE_ADDRESS_ADD is sent in the following situations:
307 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request
308 * from the client, after the ZEBRA_INTERFACE_ADD has been
309 * sent from zebra to the client
310 * - redistribute new address info to all clients in the following situations
311 * - at startup, when zebra figures out the available interfaces
312 * - when an interface is added (where support for
313 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when
314 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is
315 * received)
316 * - for the vty commands "ip address A.B.C.D/M [<secondary>|<label LINE>]"
317 * and "no bandwidth <1-10000000>", "ipv6 address X:X::X:X/M"
318 * - when an RTM_NEWADDR message is received from the kernel,
319 *
320 * The call tree that triggers ZEBRA_INTERFACE_ADDRESS_DELETE:
321 *
322 * zsend_interface_address(DELETE)
323 * ^
324 * |
325 * zebra_interface_address_delete_update
326 * ^ ^ ^
6eb8827d 327 * | | if_delete_update
328 * | |
b9df2d25 329 * ip_address_uninstall connected_delete_ipv4
330 * [ipv6_addresss_uninstall] [connected_delete_ipv6]
331 * ^ ^
332 * | |
333 * | RTM_NEWADDR on routing/netlink socket
334 * |
335 * vty commands:
336 * "no ip address A.B.C.D/M [label LINE]"
337 * "no ip address A.B.C.D/M secondary"
338 * ["no ipv6 address X:X::X:X/M"]
339 *
340 */
718e3744 341int
b9df2d25 342zsend_interface_address (int cmd, struct zserv *client,
343 struct interface *ifp, struct connected *ifc)
718e3744 344{
345 int blen;
346 struct stream *s;
347 struct prefix *p;
348
718e3744 349 s = client->obuf;
350 stream_reset (s);
c1b9800a 351
7076bb2f 352 zserv_create_header (s, cmd, ifp->vrf_id);
718e3744 353 stream_putl (s, ifp->ifindex);
354
355 /* Interface address flag. */
356 stream_putc (s, ifc->flags);
357
358 /* Prefix information. */
359 p = ifc->address;
360 stream_putc (s, p->family);
361 blen = prefix_blen (p);
362 stream_put (s, &p->u.prefix, blen);
b9df2d25 363
364 /*
365 * XXX gnu version does not send prefixlen for ZEBRA_INTERFACE_ADDRESS_DELETE
366 * but zebra_interface_address_delete_read() in the gnu version
367 * expects to find it
368 */
718e3744 369 stream_putc (s, p->prefixlen);
370
371 /* Destination. */
372 p = ifc->destination;
373 if (p)
374 stream_put (s, &p->u.prefix, blen);
375 else
376 stream_put (s, NULL, blen);
377
378 /* Write packet size. */
379 stream_putw_at (s, 0, stream_get_endp (s));
380
04b02fda 381 client->connected_rt_add_cnt++;
719e9741 382 return zebra_server_send_message(client);
718e3744 383}
384
a80beece
DS
385static int
386zsend_interface_nbr_address (int cmd, struct zserv *client,
387 struct interface *ifp, struct nbr_connected *ifc)
388{
389 int blen;
390 struct stream *s;
391 struct prefix *p;
392
a80beece
DS
393 s = client->obuf;
394 stream_reset (s);
395
7076bb2f 396 zserv_create_header (s, cmd, ifp->vrf_id);
a80beece
DS
397 stream_putl (s, ifp->ifindex);
398
399 /* Prefix information. */
400 p = ifc->address;
401 stream_putc (s, p->family);
402 blen = prefix_blen (p);
403 stream_put (s, &p->u.prefix, blen);
404
405 /*
406 * XXX gnu version does not send prefixlen for ZEBRA_INTERFACE_ADDRESS_DELETE
407 * but zebra_interface_address_delete_read() in the gnu version
408 * expects to find it
409 */
410 stream_putc (s, p->prefixlen);
411
412 /* Write packet size. */
413 stream_putw_at (s, 0, stream_get_endp (s));
414
415 return zebra_server_send_message(client);
416}
417
418/* Interface address addition. */
419static void
420zebra_interface_nbr_address_add_update (struct interface *ifp,
421 struct nbr_connected *ifc)
422{
423 struct listnode *node, *nnode;
424 struct zserv *client;
425 struct prefix *p;
426
427 if (IS_ZEBRA_DEBUG_EVENT)
428 {
429 char buf[INET6_ADDRSTRLEN];
430
431 p = ifc->address;
432 zlog_debug ("MESSAGE: ZEBRA_INTERFACE_NBR_ADDRESS_ADD %s/%d on %s",
433 inet_ntop (p->family, &p->u.prefix, buf, INET6_ADDRSTRLEN),
434 p->prefixlen, ifc->ifp->name);
435 }
436
437 for (ALL_LIST_ELEMENTS (zebrad.client_list, node, nnode, client))
4fe51714 438 zsend_interface_nbr_address (ZEBRA_INTERFACE_NBR_ADDRESS_ADD, client, ifp, ifc);
a80beece
DS
439}
440
441/* Interface address deletion. */
442static void
443zebra_interface_nbr_address_delete_update (struct interface *ifp,
444 struct nbr_connected *ifc)
445{
446 struct listnode *node, *nnode;
447 struct zserv *client;
448 struct prefix *p;
449
450 if (IS_ZEBRA_DEBUG_EVENT)
451 {
452 char buf[INET6_ADDRSTRLEN];
453
454 p = ifc->address;
455 zlog_debug ("MESSAGE: ZEBRA_INTERFACE_NBR_ADDRESS_DELETE %s/%d on %s",
456 inet_ntop (p->family, &p->u.prefix, buf, INET6_ADDRSTRLEN),
457 p->prefixlen, ifc->ifp->name);
458 }
459
460 for (ALL_LIST_ELEMENTS (zebrad.client_list, node, nnode, client))
4fe51714 461 zsend_interface_nbr_address (ZEBRA_INTERFACE_NBR_ADDRESS_DELETE, client, ifp, ifc);
a80beece
DS
462}
463
c8e264b6 464/* Send addresses on interface to client */
465int
466zsend_interface_addresses (struct zserv *client, struct interface *ifp)
467{
468 struct listnode *cnode, *cnnode;
469 struct connected *c;
470 struct nbr_connected *nc;
471
472 /* Send interface addresses. */
473 for (ALL_LIST_ELEMENTS (ifp->connected, cnode, cnnode, c))
474 {
475 if (!CHECK_FLAG (c->conf, ZEBRA_IFC_REAL))
476 continue;
477
478 if (zsend_interface_address (ZEBRA_INTERFACE_ADDRESS_ADD, client,
479 ifp, c) < 0)
480 return -1;
481 }
482
483 /* Send interface neighbors. */
484 for (ALL_LIST_ELEMENTS (ifp->nbr_connected, cnode, cnnode, nc))
485 {
486 if (zsend_interface_nbr_address (ZEBRA_INTERFACE_NBR_ADDRESS_ADD,
487 client, ifp, nc) < 0)
488 return -1;
489 }
490
491 return 0;
492}
493
494/* Notify client about interface moving from one VRF to another.
495 * Whether client is interested in old and new VRF is checked by caller.
496 */
497int
498zsend_interface_vrf_update (struct zserv *client, struct interface *ifp,
499 vrf_id_t vrf_id)
500{
501 struct stream *s;
502
503 s = client->obuf;
504 stream_reset (s);
505
506 zserv_create_header (s, ZEBRA_INTERFACE_VRF_UPDATE, ifp->vrf_id);
507
508 /* Fill in the ifIndex of the interface and its new VRF (id) */
509 stream_putl (s, ifp->ifindex);
510 stream_putw (s, vrf_id);
511
512 /* Write packet size. */
513 stream_putw_at (s, 0, stream_get_endp (s));
514
515 client->if_vrfchg_cnt++;
516 return zebra_server_send_message(client);
517}
518
1d20ccf3 519/* Add new nbr connected IPv6 address */
a80beece 520void
1d20ccf3 521nbr_connected_add_ipv6 (struct interface *ifp, struct in6_addr *address)
a80beece
DS
522{
523 struct nbr_connected *ifc;
524 struct prefix p;
525
526 p.family = AF_INET6;
527 IPV6_ADDR_COPY (&p.u.prefix, address);
1d20ccf3 528 p.prefixlen = IPV6_MAX_PREFIXLEN;
a80beece
DS
529
530 if (!(ifc = listnode_head(ifp->nbr_connected)))
531 {
532 /* new addition */
533 ifc = nbr_connected_new ();
534 ifc->address = prefix_new();
535 ifc->ifp = ifp;
536 listnode_add (ifp->nbr_connected, ifc);
537 }
538
539 prefix_copy(ifc->address, &p);
540
541 zebra_interface_nbr_address_add_update (ifp, ifc);
5c610faf
DS
542
543 if_nbr_ipv6ll_to_ipv4ll_neigh_update (ifp, address, 1);
a80beece
DS
544}
545
546void
1d20ccf3 547nbr_connected_delete_ipv6 (struct interface *ifp, struct in6_addr *address)
a80beece
DS
548{
549 struct nbr_connected *ifc;
550 struct prefix p;
551
552 p.family = AF_INET6;
553 IPV6_ADDR_COPY (&p.u.prefix, address);
1d20ccf3 554 p.prefixlen = IPV6_MAX_PREFIXLEN;
a80beece
DS
555
556 ifc = nbr_connected_check(ifp, &p);
557 if (!ifc)
558 return;
559
560 listnode_delete (ifp->nbr_connected, ifc);
561
562 zebra_interface_nbr_address_delete_update (ifp, ifc);
563
5c610faf
DS
564 if_nbr_ipv6ll_to_ipv4ll_neigh_update (ifp, address, 0);
565
a80beece
DS
566 nbr_connected_free (ifc);
567}
568
b9df2d25 569/*
570 * The cmd passed to zsend_interface_update may be ZEBRA_INTERFACE_UP or
571 * ZEBRA_INTERFACE_DOWN.
572 *
573 * The ZEBRA_INTERFACE_UP message is sent from the zebra server to
574 * the clients in one of 2 situations:
575 * - an if_up is detected e.g., as a result of an RTM_IFINFO message
576 * - a vty command modifying the bandwidth of an interface is received.
577 * The ZEBRA_INTERFACE_DOWN message is sent when an if_down is detected.
578 */
718e3744 579int
b9df2d25 580zsend_interface_update (int cmd, struct zserv *client, struct interface *ifp)
718e3744 581{
582 struct stream *s;
583
718e3744 584 s = client->obuf;
585 stream_reset (s);
586
7076bb2f 587 zserv_create_header (s, cmd, ifp->vrf_id);
51d4ef83 588 zserv_encode_interface (s, ifp);
718e3744 589
04b02fda
DS
590 if (cmd == ZEBRA_INTERFACE_UP)
591 client->ifup_cnt++;
592 else
593 client->ifdown_cnt++;
594
719e9741 595 return zebra_server_send_message(client);
718e3744 596}
597
b9df2d25 598/*
5048fe14 599 * This is the new function to announce and withdraw redistributed routes, used
600 * by Zebra. This is the old zsend_route_multipath() function. That function
601 * was duplicating code to send a lot of information that was essentially thrown
602 * away or ignored by the receiver. This is the leaner function that is not a
603 * duplicate of the zapi_ipv4_route_add/del.
b9df2d25 604 *
5048fe14 605 * The primary difference is that this function merely sends a single NH instead of
606 * all the nexthops.
b9df2d25 607 */
718e3744 608int
9c6060d4 609zsend_redistribute_route (int add, struct zserv *client, struct prefix *p,
05737783 610 struct prefix *src_p, struct rib *rib)
718e3744 611{
9c6060d4
RW
612 afi_t afi;
613 int cmd;
718e3744 614 int psize;
615 struct stream *s;
616 struct nexthop *nexthop;
1dcb5172 617 unsigned long nhnummark = 0, messmark = 0;
b9df2d25 618 int nhnum = 0;
1dcb5172 619 u_char zapi_flags = 0;
5048fe14 620 struct nexthop dummy_nh;
621
9c6060d4
RW
622 afi = family2afi (p->family);
623 if (add)
624 {
625 switch (afi)
626 {
627 case AFI_IP:
628 cmd = ZEBRA_REDISTRIBUTE_IPV4_ADD;
629 client->redist_v4_add_cnt++;
630 break;
631 case AFI_IP6:
632 cmd = ZEBRA_REDISTRIBUTE_IPV6_ADD;
633 client->redist_v6_add_cnt++;
634 break;
635 default:
636 return -1;
637 }
638 }
639 else
640 {
641 switch (afi)
642 {
643 case AFI_IP:
644 cmd = ZEBRA_REDISTRIBUTE_IPV4_DEL;
645 client->redist_v4_del_cnt++;
646 break;
647 case AFI_IP6:
648 cmd = ZEBRA_REDISTRIBUTE_IPV6_DEL;
649 client->redist_v6_del_cnt++;
650 break;
651 default:
652 return -1;
653 }
654 }
7076bb2f 655
718e3744 656 s = client->obuf;
657 stream_reset (s);
5048fe14 658 memset(&dummy_nh, 0, sizeof(struct nexthop));
659
7076bb2f
FL
660 zserv_create_header (s, cmd, rib->vrf_id);
661
c1b9800a 662 /* Put type and nexthop. */
718e3744 663 stream_putc (s, rib->type);
7c8ff89e 664 stream_putw (s, rib->instance);
0fc452dc 665 stream_putl (s, rib->flags);
5048fe14 666
1dcb5172 667 /* marker for message flags field */
668 messmark = stream_get_endp (s);
669 stream_putc (s, 0);
718e3744 670
671 /* Prefix. */
672 psize = PSIZE (p->prefixlen);
673 stream_putc (s, p->prefixlen);
b9df2d25 674 stream_write (s, (u_char *) & p->u.prefix, psize);
675
05737783
CF
676 if (src_p)
677 {
678 SET_FLAG (zapi_flags, ZAPI_MESSAGE_SRCPFX);
679 psize = PSIZE (src_p->prefixlen);
680 stream_putc (s, src_p->prefixlen);
681 stream_write (s, (u_char *) & src_p->u.prefix, psize);
682 }
683
718e3744 684 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
685 {
5048fe14 686 /* We don't send any nexthops when there's a multipath */
eac6e3f0 687 if (rib->nexthop_active_num > 1 && client->proto != ZEBRA_ROUTE_LDP)
5048fe14 688 {
689 SET_FLAG (zapi_flags, ZAPI_MESSAGE_NEXTHOP);
690 SET_FLAG (zapi_flags, ZAPI_MESSAGE_IFINDEX);
691
692 stream_putc(s, 1);
693 if (p->family == AF_INET)
694 {
695 stream_put_in_addr (s, &dummy_nh.gate.ipv4);
696 }
697 else if (p->family == AF_INET6)
698 {
699 stream_write (s, (u_char *) &dummy_nh.gate.ipv6, 16);
700 }
701 else
702 {
703 /* We don't handle anything else now, abort */
704 zlog_err("%s: Unable to redistribute route of unknown family, %d\n",
705 __func__, p->family);
706 return -1;
707 }
708 stream_putc (s, 1);
709 stream_putl (s, 0); /* dummy ifindex */
710 break;
711 }
712
446bb95e 713 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE))
b9df2d25 714 {
1dcb5172 715 SET_FLAG (zapi_flags, ZAPI_MESSAGE_NEXTHOP);
716 SET_FLAG (zapi_flags, ZAPI_MESSAGE_IFINDEX);
1dcb5172 717 if (nhnummark == 0)
718 {
719 nhnummark = stream_get_endp (s);
720 stream_putc (s, 1); /* placeholder */
721 }
b9df2d25 722 nhnum++;
723
724 switch(nexthop->type)
725 {
726 case NEXTHOP_TYPE_IPV4:
727 case NEXTHOP_TYPE_IPV4_IFINDEX:
728 stream_put_in_addr (s, &nexthop->gate.ipv4);
729 break;
b9df2d25 730 case NEXTHOP_TYPE_IPV6:
731 case NEXTHOP_TYPE_IPV6_IFINDEX:
5048fe14 732 /* Only BGP supports IPv4 prefix with IPv6 NH, so kill this */
733 if (p->family == AF_INET)
734 stream_put_in_addr(s, &dummy_nh.gate.ipv4);
735 else
736 stream_write (s, (u_char *) &nexthop->gate.ipv6, 16);
b9df2d25 737 break;
b9df2d25 738 default:
5048fe14 739 if (cmd == ZEBRA_REDISTRIBUTE_IPV4_ADD
740 || cmd == ZEBRA_REDISTRIBUTE_IPV4_DEL)
b9df2d25 741 {
742 struct in_addr empty;
44983cf8 743 memset (&empty, 0, sizeof (struct in_addr));
b9df2d25 744 stream_write (s, (u_char *) &empty, IPV4_MAX_BYTELEN);
745 }
746 else
747 {
748 struct in6_addr empty;
749 memset (&empty, 0, sizeof (struct in6_addr));
750 stream_write (s, (u_char *) &empty, IPV6_MAX_BYTELEN);
751 }
752 }
753
754 /* Interface index. */
755 stream_putc (s, 1);
756 stream_putl (s, nexthop->ifindex);
757
eac6e3f0
RW
758 /* ldpd needs all nexthops */
759 if (client->proto != ZEBRA_ROUTE_LDP)
a16d0e7b 760 break;
b9df2d25 761 }
718e3744 762 }
763
7fe041ac 764 /* Distance */
a16d0e7b
RW
765 SET_FLAG (zapi_flags, ZAPI_MESSAGE_DISTANCE);
766 stream_putc (s, rib->distance);
7fe041ac 767
718e3744 768 /* Metric */
a16d0e7b
RW
769 SET_FLAG (zapi_flags, ZAPI_MESSAGE_METRIC);
770 stream_putl (s, rib->metric);
0d9551dc 771
7fe041ac 772 /* Tag */
a16d0e7b
RW
773 if (rib->tag)
774 {
775 SET_FLAG(zapi_flags, ZAPI_MESSAGE_TAG);
dc9ffce8 776 stream_putl(s, rib->tag);
a16d0e7b 777 }
5048fe14 778
7fe041ac 779 /* MTU */
a16d0e7b
RW
780 SET_FLAG (zapi_flags, ZAPI_MESSAGE_MTU);
781 stream_putl (s, rib->mtu);
5048fe14 782
1dcb5172 783 /* write real message flags value */
784 stream_putc_at (s, messmark, zapi_flags);
5048fe14 785
b9df2d25 786 /* Write next-hop number */
787 if (nhnummark)
c1eaa442 788 stream_putc_at (s, nhnummark, nhnum);
5048fe14 789
718e3744 790 /* Write packet size. */
791 stream_putw_at (s, 0, stream_get_endp (s));
792
719e9741 793 return zebra_server_send_message(client);
718e3744 794}
795
10fbd59a
DS
796static int
797zsend_write_nexthop (struct stream *s, struct nexthop *nexthop)
798{
799 stream_putc (s, nexthop->type);
800 switch (nexthop->type)
801 {
802 case NEXTHOP_TYPE_IPV4:
10fbd59a
DS
803 case NEXTHOP_TYPE_IPV4_IFINDEX:
804 stream_put_in_addr (s, &nexthop->gate.ipv4);
805 stream_putl (s, nexthop->ifindex);
806 break;
807 case NEXTHOP_TYPE_IPV6:
808 stream_put (s, &nexthop->gate.ipv6, 16);
809 break;
810 case NEXTHOP_TYPE_IPV6_IFINDEX:
811 stream_put (s, &nexthop->gate.ipv6, 16);
812 stream_putl (s, nexthop->ifindex);
813 break;
814 case NEXTHOP_TYPE_IFINDEX:
815 stream_putl (s, nexthop->ifindex);
816 break;
817 default:
818 /* do nothing */
819 break;
820 }
821 return 1;
822}
823
fb018d25
DS
824/* Nexthop register */
825static int
078430f6 826zserv_rnh_register (struct zserv *client, int sock, u_short length,
d651649e 827 rnh_type_t type, struct zebra_vrf *zvrf)
fb018d25
DS
828{
829 struct rnh *rnh;
830 struct stream *s;
831 struct prefix p;
832 u_short l = 0;
078430f6 833 u_char flags = 0;
fb018d25
DS
834
835 if (IS_ZEBRA_DEBUG_NHT)
078430f6
DS
836 zlog_debug("rnh_register msg from client %s: length=%d, type=%s\n",
837 zebra_route_string(client->proto), length,
838 (type == RNH_NEXTHOP_TYPE) ? "nexthop" : "route");
fb018d25
DS
839
840 s = client->ibuf;
841
cf672a86 842 client->nh_reg_time = monotime(NULL);
078430f6 843
fb018d25
DS
844 while (l < length)
845 {
078430f6 846 flags = stream_getc(s);
fb018d25
DS
847 p.family = stream_getw(s);
848 p.prefixlen = stream_getc(s);
fc9a856f 849 l += 4;
078430f6
DS
850 if (p.family == AF_INET)
851 {
852 p.u.prefix4.s_addr = stream_get_ipv4(s);
853 l += IPV4_MAX_BYTELEN;
854 }
855 else if (p.family == AF_INET6)
856 {
857 stream_get(&p.u.prefix6, s, IPV6_MAX_BYTELEN);
858 l += IPV6_MAX_BYTELEN;
859 }
860 else
861 {
862 zlog_err("rnh_register: Received unknown family type %d\n",
863 p.family);
864 return -1;
865 }
661512bf 866 rnh = zebra_add_rnh(&p, zvrf_id (zvrf), type);
078430f6
DS
867 if (type == RNH_NEXTHOP_TYPE)
868 {
869 if (flags && !CHECK_FLAG(rnh->flags, ZEBRA_NHT_CONNECTED))
870 SET_FLAG(rnh->flags, ZEBRA_NHT_CONNECTED);
871 else if (!flags && CHECK_FLAG(rnh->flags, ZEBRA_NHT_CONNECTED))
872 UNSET_FLAG(rnh->flags, ZEBRA_NHT_CONNECTED);
873 }
874 else if (type == RNH_IMPORT_CHECK_TYPE)
875 {
876 if (flags && !CHECK_FLAG(rnh->flags, ZEBRA_NHT_EXACT_MATCH))
877 SET_FLAG(rnh->flags, ZEBRA_NHT_EXACT_MATCH);
878 else if (!flags && CHECK_FLAG(rnh->flags, ZEBRA_NHT_EXACT_MATCH))
879 UNSET_FLAG(rnh->flags, ZEBRA_NHT_EXACT_MATCH);
880 }
881
661512bf 882 zebra_add_rnh_client(rnh, client, type, zvrf_id (zvrf));
078430f6 883 /* Anything not AF_INET/INET6 has been filtered out above */
661512bf 884 zebra_evaluate_rnh(zvrf_id (zvrf), p.family, 1, type, &p);
fb018d25 885 }
fb018d25
DS
886 return 0;
887}
888
889/* Nexthop register */
890static int
078430f6 891zserv_rnh_unregister (struct zserv *client, int sock, u_short length,
d651649e 892 rnh_type_t type, struct zebra_vrf *zvrf)
fb018d25
DS
893{
894 struct rnh *rnh;
895 struct stream *s;
896 struct prefix p;
897 u_short l = 0;
898
899 if (IS_ZEBRA_DEBUG_NHT)
078430f6 900 zlog_debug("rnh_unregister msg from client %s: length=%d\n",
fb018d25
DS
901 zebra_route_string(client->proto), length);
902
903 s = client->ibuf;
904
905 while (l < length)
906 {
4e3afb14 907 (void)stream_getc(s); //Connected or not. Not used in this function
fb018d25
DS
908 p.family = stream_getw(s);
909 p.prefixlen = stream_getc(s);
fc9a856f 910 l += 4;
078430f6
DS
911 if (p.family == AF_INET)
912 {
913 p.u.prefix4.s_addr = stream_get_ipv4(s);
914 l += IPV4_MAX_BYTELEN;
915 }
916 else if (p.family == AF_INET6)
917 {
918 stream_get(&p.u.prefix6, s, IPV6_MAX_BYTELEN);
919 l += IPV6_MAX_BYTELEN;
920 }
921 else
922 {
923 zlog_err("rnh_register: Received unknown family type %d\n",
924 p.family);
925 return -1;
926 }
661512bf 927 rnh = zebra_lookup_rnh(&p, zvrf_id (zvrf), type);
fb018d25 928 if (rnh)
04b02fda 929 {
cf672a86 930 client->nh_dereg_time = monotime(NULL);
078430f6 931 zebra_remove_rnh_client(rnh, client, type);
04b02fda 932 }
fb018d25
DS
933 }
934 return 0;
935}
936
6f61a5a3
EM
937/*
938 Modified version of zsend_ipv4_nexthop_lookup():
939 Query unicast rib if nexthop is not found on mrib.
940 Returns both route metric and protocol distance.
941*/
942static int
4623d897 943zsend_ipv4_nexthop_lookup_mrib (struct zserv *client, struct in_addr addr, struct rib *rib, struct zebra_vrf *zvrf)
6f61a5a3
EM
944{
945 struct stream *s;
6f61a5a3
EM
946 unsigned long nump;
947 u_char num;
948 struct nexthop *nexthop;
949
6f61a5a3
EM
950 /* Get output stream. */
951 s = client->obuf;
952 stream_reset (s);
953
954 /* Fill in result. */
661512bf 955 zserv_create_header (s, ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB, zvrf_id (zvrf));
6f61a5a3
EM
956 stream_put_in_addr (s, &addr);
957
958 if (rib)
959 {
960 stream_putc (s, rib->distance);
961 stream_putl (s, rib->metric);
962 num = 0;
963 nump = stream_get_endp(s); /* remember position for nexthop_num */
964 stream_putc (s, 0); /* reserve room for nexthop_num */
965 /* Only non-recursive routes are elegible to resolve the nexthop we
966 * are looking up. Therefore, we will just iterate over the top
967 * chain of nexthops. */
968 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next)
446bb95e 969 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE))
10fbd59a 970 num += zsend_write_nexthop (s, nexthop);
6f61a5a3
EM
971
972 stream_putc_at (s, nump, num); /* store nexthop_num */
973 }
974 else
975 {
976 stream_putc (s, 0); /* distance */
977 stream_putl (s, 0); /* metric */
978 stream_putc (s, 0); /* nexthop_num */
979 }
980
981 stream_putw_at (s, 0, stream_get_endp (s));
982
983 return zebra_server_send_message(client);
984}
985
18a6dce6 986/* Router-id is updated. Send ZEBRA_ROUTER_ID_ADD to client. */
987int
c6ffe645
FL
988zsend_router_id_update (struct zserv *client, struct prefix *p,
989 vrf_id_t vrf_id)
18a6dce6 990{
991 struct stream *s;
992 int blen;
993
994 /* Check this client need interface information. */
7076bb2f 995 if (! vrf_bitmap_check (client->ridinfo, vrf_id))
719e9741 996 return 0;
18a6dce6 997
998 s = client->obuf;
999 stream_reset (s);
1000
18a6dce6 1001 /* Message type. */
7076bb2f 1002 zserv_create_header (s, ZEBRA_ROUTER_ID_UPDATE, vrf_id);
18a6dce6 1003
1004 /* Prefix information. */
1005 stream_putc (s, p->family);
1006 blen = prefix_blen (p);
1007 stream_put (s, &p->u.prefix, blen);
1008 stream_putc (s, p->prefixlen);
1009
1010 /* Write packet size. */
1011 stream_putw_at (s, 0, stream_get_endp (s));
1012
719e9741 1013 return zebra_server_send_message(client);
18a6dce6 1014}
6b0655a2 1015
718e3744 1016/* Register zebra server interface information. Send current all
1017 interface and address information. */
719e9741 1018static int
d651649e 1019zread_interface_add (struct zserv *client, u_short length, struct zebra_vrf *zvrf)
718e3744 1020{
1a1a7065 1021 struct vrf *vrf;
1eb8ef25 1022 struct listnode *ifnode, *ifnnode;
718e3744 1023 struct interface *ifp;
718e3744 1024
1025 /* Interface information is needed. */
661512bf 1026 vrf_bitmap_set (client->ifinfo, zvrf_id (zvrf));
718e3744 1027
1a1a7065 1028 RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id)
718e3744 1029 {
1a1a7065 1030 for (ALL_LIST_ELEMENTS (vrf->iflist, ifnode, ifnnode, ifp))
4fe51714
DS
1031 {
1032 /* Skip pseudo interface. */
1033 if (! CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE))
1034 continue;
718e3744 1035
4fe51714
DS
1036 if (zsend_interface_add (client, ifp) < 0)
1037 return -1;
718e3744 1038
4fe51714
DS
1039 if (zsend_interface_addresses (client, ifp) < 0)
1040 return -1;
1041 }
718e3744 1042 }
719e9741 1043 return 0;
718e3744 1044}
1045
1046/* Unregister zebra server interface information. */
719e9741 1047static int
d651649e 1048zread_interface_delete (struct zserv *client, u_short length, struct zebra_vrf *zvrf)
718e3744 1049{
661512bf 1050 vrf_bitmap_unset (client->ifinfo, zvrf_id (zvrf));
719e9741 1051 return 0;
718e3744 1052}
1053
6878b9db 1054void
4f87aceb 1055zserv_nexthop_num_warn (const char *caller, const struct prefix *p, const unsigned int nexthop_num)
6878b9db 1056{
37fe7731 1057 if (nexthop_num > multipath_num)
6878b9db 1058 {
4690c7d7
DS
1059 char buff[PREFIX2STR_BUFFER];
1060 prefix2str(p, buff, sizeof (buff));
6878b9db 1061 zlog_warn("%s: Prefix %s has %d nexthops, but we can only use the first %d",
37fe7731 1062 caller, buff, nexthop_num, multipath_num);
6878b9db
DS
1063 }
1064}
1065
718e3744 1066/* This function support multiple nexthop. */
b9df2d25 1067/*
1068 * Parse the ZEBRA_IPV4_ROUTE_ADD sent from client. Update rib and
1069 * add kernel route.
1070 */
719e9741 1071static int
d651649e 1072zread_ipv4_add (struct zserv *client, u_short length, struct zebra_vrf *zvrf)
718e3744 1073{
1074 int i;
1075 struct rib *rib;
b4c034b0 1076 struct prefix p;
718e3744 1077 u_char message;
1078 struct in_addr nexthop;
1079 u_char nexthop_num;
1080 u_char nexthop_type;
1081 struct stream *s;
b892f1dd 1082 ifindex_t ifindex;
d44ca835 1083 safi_t safi;
04b02fda 1084 int ret;
718e3744 1085
1086 /* Get input stream. */
1087 s = client->ibuf;
1088
1089 /* Allocate new rib. */
4d38fdb4 1090 rib = XCALLOC (MTYPE_RIB, sizeof (struct rib));
1091
718e3744 1092 /* Type, flags, message. */
1093 rib->type = stream_getc (s);
7c8ff89e 1094 rib->instance = stream_getw (s);
0fc452dc 1095 rib->flags = stream_getl (s);
b9df2d25 1096 message = stream_getc (s);
cddf391b 1097 safi = stream_getw (s);
718e3744 1098 rib->uptime = time (NULL);
1099
1100 /* IPv4 prefix. */
1101 memset (&p, 0, sizeof (struct prefix_ipv4));
1102 p.family = AF_INET;
1103 p.prefixlen = stream_getc (s);
b4c034b0 1104 stream_get (&p.u.prefix4, s, PSIZE (p.prefixlen));
718e3744 1105
78104b9b 1106 /* VRF ID */
661512bf 1107 rib->vrf_id = zvrf_id (zvrf);
78104b9b 1108
718e3744 1109 /* Nexthop parse. */
1110 if (CHECK_FLAG (message, ZAPI_MESSAGE_NEXTHOP))
1111 {
1112 nexthop_num = stream_getc (s);
6878b9db 1113 zserv_nexthop_num_warn(__func__, (const struct prefix *)&p, nexthop_num);
718e3744 1114
1115 for (i = 0; i < nexthop_num; i++)
1116 {
1117 nexthop_type = stream_getc (s);
1118
1119 switch (nexthop_type)
1120 {
5b30316e 1121 case NEXTHOP_TYPE_IFINDEX:
718e3744 1122 ifindex = stream_getl (s);
a399694f 1123 rib_nexthop_ifindex_add (rib, ifindex);
718e3744 1124 break;
5b30316e 1125 case NEXTHOP_TYPE_IPV4:
718e3744 1126 nexthop.s_addr = stream_get_ipv4 (s);
a399694f 1127 rib_nexthop_ipv4_add (rib, &nexthop, NULL);
718e3744 1128 break;
5b30316e 1129 case NEXTHOP_TYPE_IPV4_IFINDEX:
c963c203
JT
1130 nexthop.s_addr = stream_get_ipv4 (s);
1131 ifindex = stream_getl (s);
a399694f 1132 rib_nexthop_ipv4_ifindex_add (rib, &nexthop, NULL, ifindex);
c963c203 1133 break;
5b30316e 1134 case NEXTHOP_TYPE_IPV6:
9985f83c 1135 stream_forward_getp (s, IPV6_MAX_BYTELEN);
718e3744 1136 break;
5b30316e 1137 case NEXTHOP_TYPE_BLACKHOLE:
a399694f 1138 rib_nexthop_blackhole_add (rib);
6902c69a
SV
1139 break;
1140 }
718e3744 1141 }
1142 }
1143
1144 /* Distance. */
1145 if (CHECK_FLAG (message, ZAPI_MESSAGE_DISTANCE))
1146 rib->distance = stream_getc (s);
1147
1148 /* Metric. */
1149 if (CHECK_FLAG (message, ZAPI_MESSAGE_METRIC))
1150 rib->metric = stream_getl (s);
1151
0d9551dc
DS
1152 /* Tag */
1153 if (CHECK_FLAG (message, ZAPI_MESSAGE_TAG))
dc9ffce8 1154 rib->tag = stream_getl (s);
0d9551dc
DS
1155 else
1156 rib->tag = 0;
1157
c50ca33a
TT
1158 if (CHECK_FLAG (message, ZAPI_MESSAGE_MTU))
1159 rib->mtu = stream_getl (s);
1160 else
1161 rib->mtu = 0;
1162
171eee31 1163 /* Table */
d651649e 1164 rib->table = zvrf->table_id;
12f6fb97 1165
3c7c91d0 1166 ret = rib_add_multipath (AFI_IP, safi, &p, NULL, rib);
04b02fda
DS
1167
1168 /* Stats */
1169 if (ret > 0)
1170 client->v4_route_add_cnt++;
1171 else if (ret < 0)
1172 client->v4_route_upd8_cnt++;
719e9741 1173 return 0;
718e3744 1174}
1175
1176/* Zebra server IPv4 prefix delete function. */
719e9741 1177static int
d651649e 1178zread_ipv4_delete (struct zserv *client, u_short length, struct zebra_vrf *zvrf)
718e3744 1179{
1180 int i;
1181 struct stream *s;
1182 struct zapi_ipv4 api;
616368ed
DS
1183 struct in_addr nexthop;
1184 union g_addr *nexthop_p;
718e3744 1185 unsigned long ifindex;
616368ed 1186 struct prefix p;
718e3744 1187 u_char nexthop_num;
1188 u_char nexthop_type;
12f6fb97 1189 u_int32_t table_id;
12f6fb97 1190
718e3744 1191 s = client->ibuf;
1192 ifindex = 0;
1193 nexthop.s_addr = 0;
6902c69a 1194 nexthop_p = NULL;
718e3744 1195
1196 /* Type, flags, message. */
1197 api.type = stream_getc (s);
7c8ff89e 1198 api.instance = stream_getw (s);
0fc452dc 1199 api.flags = stream_getl (s);
718e3744 1200 api.message = stream_getc (s);
cddf391b 1201 api.safi = stream_getw (s);
718e3744 1202
1203 /* IPv4 prefix. */
4fdeb6b0 1204 memset (&p, 0, sizeof (struct prefix));
718e3744 1205 p.family = AF_INET;
1206 p.prefixlen = stream_getc (s);
616368ed 1207 stream_get (&p.u.prefix4, s, PSIZE (p.prefixlen));
718e3744 1208
1209 /* Nexthop, ifindex, distance, metric. */
1210 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
1211 {
1212 nexthop_num = stream_getc (s);
1213
1214 for (i = 0; i < nexthop_num; i++)
1215 {
1216 nexthop_type = stream_getc (s);
1217
1218 switch (nexthop_type)
1219 {
5b30316e 1220 case NEXTHOP_TYPE_IFINDEX:
718e3744 1221 ifindex = stream_getl (s);
1222 break;
5b30316e 1223 case NEXTHOP_TYPE_IPV4:
718e3744 1224 nexthop.s_addr = stream_get_ipv4 (s);
616368ed 1225 nexthop_p = (union g_addr *)&nexthop;
718e3744 1226 break;
5b30316e 1227 case NEXTHOP_TYPE_IPV4_IFINDEX:
c963c203 1228 nexthop.s_addr = stream_get_ipv4 (s);
616368ed 1229 nexthop_p = (union g_addr *)&nexthop;
c963c203
JT
1230 ifindex = stream_getl (s);
1231 break;
5b30316e 1232 case NEXTHOP_TYPE_IPV6:
9985f83c 1233 stream_forward_getp (s, IPV6_MAX_BYTELEN);
718e3744 1234 break;
1235 }
1236 }
1237 }
1238
1239 /* Distance. */
1240 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
1241 api.distance = stream_getc (s);
1242 else
1243 api.distance = 0;
1244
1245 /* Metric. */
1246 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
1247 api.metric = stream_getl (s);
1248 else
1249 api.metric = 0;
1250
0d9551dc
DS
1251 /* tag */
1252 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_TAG))
dc9ffce8 1253 api.tag = stream_getl (s);
0d9551dc
DS
1254 else
1255 api.tag = 0;
1256
d651649e 1257 table_id = zvrf->table_id;
12f6fb97 1258
661512bf 1259 rib_delete (AFI_IP, api.safi, zvrf_id (zvrf), api.type, api.instance,
3c7c91d0 1260 api.flags, &p, NULL, nexthop_p, ifindex, table_id);
04b02fda 1261 client->v4_route_del_cnt++;
719e9741 1262 return 0;
718e3744 1263}
1264
6f61a5a3
EM
1265/* MRIB Nexthop lookup for IPv4. */
1266static int
1267zread_ipv4_nexthop_lookup_mrib (struct zserv *client, u_short length, struct zebra_vrf *zvrf)
1268{
1269 struct in_addr addr;
4623d897 1270 struct rib *rib;
6f61a5a3
EM
1271
1272 addr.s_addr = stream_get_ipv4 (client->ibuf);
661512bf 1273 rib = rib_match_ipv4_multicast (zvrf_id (zvrf), addr, NULL);
4623d897 1274 return zsend_ipv4_nexthop_lookup_mrib (client, addr, rib, zvrf);
6f61a5a3
EM
1275}
1276
8a92a8a0
DS
1277/* Zebra server IPv6 prefix add function. */
1278static int
d651649e 1279zread_ipv4_route_ipv6_nexthop_add (struct zserv *client, u_short length, struct zebra_vrf *zvrf)
8a92a8a0 1280{
37fe7731 1281 unsigned int i;
8a92a8a0
DS
1282 struct stream *s;
1283 struct in6_addr nexthop;
1284 struct rib *rib;
1285 u_char message;
1286 u_char nexthop_num;
1287 u_char nexthop_type;
b4c034b0 1288 struct prefix p;
8a92a8a0
DS
1289 safi_t safi;
1290 static struct in6_addr nexthops[MULTIPATH_NUM];
1291 static unsigned int ifindices[MULTIPATH_NUM];
1292 int ret;
1293
1294 /* Get input stream. */
1295 s = client->ibuf;
1296
8a92a8a0
DS
1297 memset (&nexthop, 0, sizeof (struct in6_addr));
1298
1299 /* Allocate new rib. */
1300 rib = XCALLOC (MTYPE_RIB, sizeof (struct rib));
1301
1302 /* Type, flags, message. */
1303 rib->type = stream_getc (s);
1304 rib->instance = stream_getw (s);
0fc452dc 1305 rib->flags = stream_getl (s);
8a92a8a0
DS
1306 message = stream_getc (s);
1307 safi = stream_getw (s);
1308 rib->uptime = time (NULL);
1309
1310 /* IPv4 prefix. */
1311 memset (&p, 0, sizeof (struct prefix_ipv4));
1312 p.family = AF_INET;
1313 p.prefixlen = stream_getc (s);
b4c034b0 1314 stream_get (&p.u.prefix4, s, PSIZE (p.prefixlen));
8a92a8a0 1315
154caaed 1316 /* VRF ID */
661512bf 1317 rib->vrf_id = zvrf_id (zvrf);
154caaed 1318
8a92a8a0
DS
1319 /* We need to give nh-addr, nh-ifindex with the same next-hop object
1320 * to the rib to ensure that IPv6 multipathing works; need to coalesce
1321 * these. Clients should send the same number of paired set of
1322 * next-hop-addr/next-hop-ifindices. */
1323 if (CHECK_FLAG (message, ZAPI_MESSAGE_NEXTHOP))
1324 {
37fe7731
DS
1325 unsigned int nh_count = 0;
1326 unsigned int if_count = 0;
1327 unsigned int max_nh_if = 0;
8a92a8a0
DS
1328
1329 nexthop_num = stream_getc (s);
6878b9db 1330 zserv_nexthop_num_warn(__func__, (const struct prefix *)&p, nexthop_num);
8a92a8a0
DS
1331 for (i = 0; i < nexthop_num; i++)
1332 {
1333 nexthop_type = stream_getc (s);
1334
1335 switch (nexthop_type)
1336 {
5b30316e 1337 case NEXTHOP_TYPE_IPV6:
8a92a8a0 1338 stream_get (&nexthop, s, 16);
37fe7731 1339 if (nh_count < multipath_num) {
8a92a8a0
DS
1340 nexthops[nh_count++] = nexthop;
1341 }
1342 break;
5b30316e 1343 case NEXTHOP_TYPE_IFINDEX:
37fe7731 1344 if (if_count < multipath_num) {
8a92a8a0
DS
1345 ifindices[if_count++] = stream_getl (s);
1346 }
1347 break;
5b30316e 1348 case NEXTHOP_TYPE_BLACKHOLE:
a399694f 1349 rib_nexthop_blackhole_add (rib);
8a92a8a0
DS
1350 break;
1351 }
1352 }
1353
1354 max_nh_if = (nh_count > if_count) ? nh_count : if_count;
1355 for (i = 0; i < max_nh_if; i++)
1356 {
1357 if ((i < nh_count) && !IN6_IS_ADDR_UNSPECIFIED (&nexthops[i])) {
1358 if ((i < if_count) && ifindices[i]) {
a399694f 1359 rib_nexthop_ipv6_ifindex_add (rib, &nexthops[i], ifindices[i]);
8a92a8a0
DS
1360 }
1361 else {
a399694f 1362 rib_nexthop_ipv6_add (rib, &nexthops[i]);
8a92a8a0
DS
1363 }
1364 }
1365 else {
1366 if ((i < if_count) && ifindices[i]) {
a399694f 1367 rib_nexthop_ifindex_add (rib, ifindices[i]);
8a92a8a0
DS
1368 }
1369 }
1370 }
1371 }
1372
1373 /* Distance. */
1374 if (CHECK_FLAG (message, ZAPI_MESSAGE_DISTANCE))
1375 rib->distance = stream_getc (s);
1376
1377 /* Metric. */
1378 if (CHECK_FLAG (message, ZAPI_MESSAGE_METRIC))
1379 rib->metric = stream_getl (s);
1380
1381 /* Tag */
1382 if (CHECK_FLAG (message, ZAPI_MESSAGE_TAG))
dc9ffce8 1383 rib->tag = stream_getl (s);
8a92a8a0
DS
1384 else
1385 rib->tag = 0;
1386
c50ca33a
TT
1387 if (CHECK_FLAG (message, ZAPI_MESSAGE_MTU))
1388 rib->mtu = stream_getl (s);
1389 else
1390 rib->mtu = 0;
1391
8a92a8a0 1392 /* Table */
d651649e 1393 rib->table = zvrf->table_id;
12f6fb97 1394
3c7c91d0 1395 ret = rib_add_multipath (AFI_IP6, safi, &p, NULL, rib);
8a92a8a0
DS
1396 /* Stats */
1397 if (ret > 0)
1398 client->v4_route_add_cnt++;
1399 else if (ret < 0)
1400 client->v4_route_upd8_cnt++;
1401
1402 return 0;
1403}
1404
719e9741 1405static int
d651649e 1406zread_ipv6_add (struct zserv *client, u_short length, struct zebra_vrf *zvrf)
718e3744 1407{
37fe7731 1408 unsigned int i;
718e3744 1409 struct stream *s;
718e3744 1410 struct in6_addr nexthop;
41fc2714
DS
1411 struct rib *rib;
1412 u_char message;
1413 u_char nexthop_num;
1414 u_char nexthop_type;
b4c034b0 1415 struct prefix p;
3c7c91d0 1416 struct prefix_ipv6 src_p, *src_pp;
41fc2714
DS
1417 safi_t safi;
1418 static struct in6_addr nexthops[MULTIPATH_NUM];
1419 static unsigned int ifindices[MULTIPATH_NUM];
04b02fda 1420 int ret;
41fc2714
DS
1421
1422 /* Get input stream. */
718e3744 1423 s = client->ibuf;
41fc2714 1424
718e3744 1425 memset (&nexthop, 0, sizeof (struct in6_addr));
1426
41fc2714
DS
1427 /* Allocate new rib. */
1428 rib = XCALLOC (MTYPE_RIB, sizeof (struct rib));
1429
718e3744 1430 /* Type, flags, message. */
41fc2714 1431 rib->type = stream_getc (s);
7c8ff89e 1432 rib->instance = stream_getw (s);
0fc452dc 1433 rib->flags = stream_getl (s);
41fc2714
DS
1434 message = stream_getc (s);
1435 safi = stream_getw (s);
1436 rib->uptime = time (NULL);
718e3744 1437
41fc2714 1438 /* IPv6 prefix. */
718e3744 1439 memset (&p, 0, sizeof (struct prefix_ipv6));
1440 p.family = AF_INET6;
1441 p.prefixlen = stream_getc (s);
b4c034b0 1442 stream_get (&p.u.prefix6, s, PSIZE (p.prefixlen));
718e3744 1443
3c7c91d0
DL
1444 if (CHECK_FLAG (message, ZAPI_MESSAGE_SRCPFX))
1445 {
1446 memset (&src_p, 0, sizeof (struct prefix_ipv6));
1447 src_p.family = AF_INET6;
1448 src_p.prefixlen = stream_getc (s);
1449 stream_get (&src_p.prefix, s, PSIZE (src_p.prefixlen));
1450 src_pp = &src_p;
1451 }
1452 else
1453 src_pp = NULL;
1454
41fc2714
DS
1455 /* We need to give nh-addr, nh-ifindex with the same next-hop object
1456 * to the rib to ensure that IPv6 multipathing works; need to coalesce
1457 * these. Clients should send the same number of paired set of
1458 * next-hop-addr/next-hop-ifindices. */
1459 if (CHECK_FLAG (message, ZAPI_MESSAGE_NEXTHOP))
718e3744 1460 {
37fe7731
DS
1461 unsigned int nh_count = 0;
1462 unsigned int if_count = 0;
1463 unsigned int max_nh_if = 0;
718e3744 1464
41fc2714 1465 nexthop_num = stream_getc (s);
6878b9db
DS
1466 zserv_nexthop_num_warn(__func__, (const struct prefix *)&p, nexthop_num);
1467 for (i = 0; i < nexthop_num; i++)
718e3744 1468 {
1469 nexthop_type = stream_getc (s);
1470
1471 switch (nexthop_type)
1472 {
5b30316e 1473 case NEXTHOP_TYPE_IPV6:
718e3744 1474 stream_get (&nexthop, s, 16);
37fe7731 1475 if (nh_count < multipath_num) {
41fc2714
DS
1476 nexthops[nh_count++] = nexthop;
1477 }
718e3744 1478 break;
5b30316e 1479 case NEXTHOP_TYPE_IFINDEX:
37fe7731 1480 if (if_count < multipath_num) {
41fc2714
DS
1481 ifindices[if_count++] = stream_getl (s);
1482 }
718e3744 1483 break;
5b30316e 1484 case NEXTHOP_TYPE_BLACKHOLE:
a399694f 1485 rib_nexthop_blackhole_add (rib);
c3c0ac83 1486 break;
718e3744 1487 }
1488 }
41fc2714
DS
1489
1490 max_nh_if = (nh_count > if_count) ? nh_count : if_count;
1491 for (i = 0; i < max_nh_if; i++)
1492 {
1493 if ((i < nh_count) && !IN6_IS_ADDR_UNSPECIFIED (&nexthops[i])) {
6f20b80d 1494 if ((i < if_count) && ifindices[i])
a399694f 1495 rib_nexthop_ipv6_ifindex_add (rib, &nexthops[i], ifindices[i]);
6f20b80d 1496 else
a399694f 1497 rib_nexthop_ipv6_add (rib, &nexthops[i]);
41fc2714
DS
1498 }
1499 else {
6f20b80d 1500 if ((i < if_count) && ifindices[i])
a399694f 1501 rib_nexthop_ifindex_add (rib, ifindices[i]);
41fc2714
DS
1502 }
1503 }
718e3744 1504 }
1505
41fc2714
DS
1506 /* Distance. */
1507 if (CHECK_FLAG (message, ZAPI_MESSAGE_DISTANCE))
1508 rib->distance = stream_getc (s);
718e3744 1509
41fc2714
DS
1510 /* Metric. */
1511 if (CHECK_FLAG (message, ZAPI_MESSAGE_METRIC))
1512 rib->metric = stream_getl (s);
718e3744 1513
0d9551dc
DS
1514 /* Tag */
1515 if (CHECK_FLAG (message, ZAPI_MESSAGE_TAG))
dc9ffce8 1516 rib->tag = stream_getl (s);
0d9551dc
DS
1517 else
1518 rib->tag = 0;
c50ca33a
TT
1519
1520 if (CHECK_FLAG (message, ZAPI_MESSAGE_MTU))
1521 rib->mtu = stream_getl (s);
1522 else
1523 rib->mtu = 0;
0d9551dc 1524
154caaed 1525 /* VRF ID */
661512bf 1526 rib->vrf_id = zvrf_id (zvrf);
d651649e 1527 rib->table = zvrf->table_id;
154caaed 1528
3c7c91d0 1529 ret = rib_add_multipath (AFI_IP6, safi, &p, src_pp, rib);
04b02fda
DS
1530 /* Stats */
1531 if (ret > 0)
1532 client->v6_route_add_cnt++;
1533 else if (ret < 0)
1534 client->v6_route_upd8_cnt++;
1535
719e9741 1536 return 0;
718e3744 1537}
1538
1539/* Zebra server IPv6 prefix delete function. */
719e9741 1540static int
d651649e 1541zread_ipv6_delete (struct zserv *client, u_short length, struct zebra_vrf *zvrf)
718e3744 1542{
1543 int i;
1544 struct stream *s;
1545 struct zapi_ipv6 api;
1546 struct in6_addr nexthop;
4b2792b5 1547 union g_addr *pnexthop = NULL;
718e3744 1548 unsigned long ifindex;
616368ed 1549 struct prefix p;
3c7c91d0 1550 struct prefix_ipv6 src_p, *src_pp;
718e3744 1551
1552 s = client->ibuf;
1553 ifindex = 0;
1554 memset (&nexthop, 0, sizeof (struct in6_addr));
1555
1556 /* Type, flags, message. */
1557 api.type = stream_getc (s);
7c8ff89e 1558 api.instance = stream_getw (s);
0fc452dc 1559 api.flags = stream_getl (s);
718e3744 1560 api.message = stream_getc (s);
f768f367 1561 api.safi = stream_getw (s);
718e3744 1562
1563 /* IPv4 prefix. */
1564 memset (&p, 0, sizeof (struct prefix_ipv6));
1565 p.family = AF_INET6;
1566 p.prefixlen = stream_getc (s);
616368ed 1567 stream_get (&p.u.prefix6, s, PSIZE (p.prefixlen));
718e3744 1568
3c7c91d0
DL
1569 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_SRCPFX))
1570 {
1571 memset (&src_p, 0, sizeof (struct prefix_ipv6));
1572 src_p.family = AF_INET6;
1573 src_p.prefixlen = stream_getc (s);
1574 stream_get (&src_p.prefix, s, PSIZE (src_p.prefixlen));
1575 src_pp = &src_p;
1576 }
1577 else
1578 src_pp = NULL;
1579
718e3744 1580 /* Nexthop, ifindex, distance, metric. */
1581 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP))
1582 {
1583 u_char nexthop_type;
1584
1585 api.nexthop_num = stream_getc (s);
1586 for (i = 0; i < api.nexthop_num; i++)
1587 {
1588 nexthop_type = stream_getc (s);
1589
1590 switch (nexthop_type)
1591 {
5b30316e 1592 case NEXTHOP_TYPE_IPV6:
718e3744 1593 stream_get (&nexthop, s, 16);
616368ed 1594 pnexthop = (union g_addr *)&nexthop;
718e3744 1595 break;
5b30316e 1596 case NEXTHOP_TYPE_IFINDEX:
718e3744 1597 ifindex = stream_getl (s);
1598 break;
1599 }
1600 }
1601 }
1602
0d9551dc 1603 /* Distance. */
718e3744 1604 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE))
1605 api.distance = stream_getc (s);
1606 else
1607 api.distance = 0;
0d9551dc
DS
1608
1609 /* Metric. */
718e3744 1610 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC))
1611 api.metric = stream_getl (s);
1612 else
1613 api.metric = 0;
1614
0d9551dc
DS
1615 /* tag */
1616 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_TAG))
dc9ffce8 1617 api.tag = stream_getl (s);
0d9551dc
DS
1618 else
1619 api.tag = 0;
1620
718e3744 1621 if (IN6_IS_ADDR_UNSPECIFIED (&nexthop))
661512bf 1622 rib_delete (AFI_IP6, api.safi, zvrf_id (zvrf), api.type, api.instance,
3c7c91d0 1623 api.flags, &p, src_pp, NULL, ifindex, client->rtm_table);
718e3744 1624 else
661512bf 1625 rib_delete (AFI_IP6, api.safi, zvrf_id (zvrf), api.type, api.instance,
3c7c91d0 1626 api.flags, &p, src_pp, pnexthop, ifindex, client->rtm_table);
04b02fda
DS
1627
1628 client->v6_route_del_cnt++;
719e9741 1629 return 0;
718e3744 1630}
1631
18a6dce6 1632/* Register zebra server router-id information. Send current router-id */
719e9741 1633static int
d651649e 1634zread_router_id_add (struct zserv *client, u_short length, struct zebra_vrf *zvrf)
18a6dce6 1635{
1636 struct prefix p;
1637
1638 /* Router-id information is needed. */
661512bf 1639 vrf_bitmap_set (client->ridinfo, zvrf_id (zvrf));
18a6dce6 1640
661512bf 1641 router_id_get (&p, zvrf_id (zvrf));
18a6dce6 1642
661512bf 1643 return zsend_router_id_update (client, &p, zvrf_id (zvrf));
18a6dce6 1644}
1645
1646/* Unregister zebra server router-id information. */
719e9741 1647static int
d651649e 1648zread_router_id_delete (struct zserv *client, u_short length, struct zebra_vrf *zvrf)
18a6dce6 1649{
661512bf 1650 vrf_bitmap_unset (client->ridinfo, zvrf_id (zvrf));
719e9741 1651 return 0;
18a6dce6 1652}
1653
2ea1ab1c
VT
1654/* Tie up route-type and client->sock */
1655static void
1656zread_hello (struct zserv *client)
1657{
1658 /* type of protocol (lib/zebra.h) */
1659 u_char proto;
7c8ff89e
DS
1660 u_short instance;
1661
2ea1ab1c 1662 proto = stream_getc (client->ibuf);
7c8ff89e 1663 instance = stream_getw (client->ibuf);
2ea1ab1c
VT
1664
1665 /* accept only dynamic routing protocols */
1666 if ((proto < ZEBRA_ROUTE_MAX)
1667 && (proto > ZEBRA_ROUTE_STATIC))
1668 {
1669 zlog_notice ("client %d says hello and bids fair to announce only %s routes",
1670 client->sock, zebra_route_string(proto));
7c8ff89e
DS
1671 if (instance)
1672 zlog_notice ("client protocol instance %d", instance);
2ea1ab1c 1673
fb018d25 1674 client->proto = proto;
7c8ff89e 1675 client->instance = instance;
2ea1ab1c
VT
1676 }
1677}
1678
7076bb2f
FL
1679/* Unregister all information in a VRF. */
1680static int
d651649e 1681zread_vrf_unregister (struct zserv *client, u_short length, struct zebra_vrf *zvrf)
7076bb2f
FL
1682{
1683 int i;
1684 afi_t afi;
1685
1686 for (afi = AFI_IP; afi < AFI_MAX; afi++)
1687 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
661512bf
RW
1688 vrf_bitmap_unset (client->redist[afi][i], zvrf_id (zvrf));
1689 vrf_bitmap_unset (client->redist_default, zvrf_id (zvrf));
1690 vrf_bitmap_unset (client->ifinfo, zvrf_id (zvrf));
1691 vrf_bitmap_unset (client->ridinfo, zvrf_id (zvrf));
7076bb2f
FL
1692
1693 return 0;
1694}
1695
ce549947
RW
1696static void
1697zread_mpls_labels (int command, struct zserv *client, u_short length,
1698 vrf_id_t vrf_id)
1699{
1700 struct stream *s;
1701 enum lsp_types_t type;
1702 struct prefix prefix;
1703 enum nexthop_types_t gtype;
1704 union g_addr gate;
88d88a9c 1705 ifindex_t ifindex;
ce549947
RW
1706 mpls_label_t in_label, out_label;
1707 u_int8_t distance;
1708 struct zebra_vrf *zvrf;
1709
1710 zvrf = vrf_info_lookup (vrf_id);
1711 if (!zvrf)
1712 return;
1713
1714 /* Get input stream. */
1715 s = client->ibuf;
1716
1717 /* Get data. */
1718 type = stream_getc (s);
1719 prefix.family = stream_getl (s);
1720 switch (prefix.family)
1721 {
1722 case AF_INET:
1723 prefix.u.prefix4.s_addr = stream_get_ipv4 (s);
1724 prefix.prefixlen = stream_getc (s);
ce549947
RW
1725 gate.ipv4.s_addr = stream_get_ipv4 (s);
1726 break;
1727 case AF_INET6:
1728 stream_get (&prefix.u.prefix6, s, 16);
1729 prefix.prefixlen = stream_getc (s);
ce549947
RW
1730 stream_get (&gate.ipv6, s, 16);
1731 break;
1732 default:
1733 return;
1734 }
88d88a9c 1735 ifindex = stream_getl (s);
ce549947
RW
1736 distance = stream_getc (s);
1737 in_label = stream_getl (s);
1738 out_label = stream_getl (s);
1739
88d88a9c
RW
1740 switch (prefix.family)
1741 {
1742 case AF_INET:
1743 if (ifindex)
1744 gtype = NEXTHOP_TYPE_IPV4_IFINDEX;
1745 else
1746 gtype = NEXTHOP_TYPE_IPV4;
1747 break;
1748 case AF_INET6:
1749 if (ifindex)
1750 gtype = NEXTHOP_TYPE_IPV6_IFINDEX;
1751 else
1752 gtype = NEXTHOP_TYPE_IPV6;
1753 break;
1754 default:
1755 return;
1756 }
1757
fe6c7157
RW
1758 if (! mpls_enabled)
1759 return;
1760
ce549947
RW
1761 if (command == ZEBRA_MPLS_LABELS_ADD)
1762 {
1763 mpls_lsp_install (zvrf, type, in_label, out_label, gtype, &gate,
88d88a9c 1764 NULL, ifindex);
ce549947 1765 if (out_label != MPLS_IMP_NULL_LABEL)
88d88a9c
RW
1766 mpls_ftn_update (1, zvrf, type, &prefix, gtype, &gate, ifindex,
1767 distance, out_label);
ce549947
RW
1768 }
1769 else if (command == ZEBRA_MPLS_LABELS_DELETE)
1770 {
88d88a9c 1771 mpls_lsp_uninstall (zvrf, type, in_label, gtype, &gate, NULL, ifindex);
ce549947 1772 if (out_label != MPLS_IMP_NULL_LABEL)
88d88a9c
RW
1773 mpls_ftn_update (0, zvrf, type, &prefix, gtype, &gate, ifindex,
1774 distance, out_label);
ce549947
RW
1775 }
1776}
fea12efb 1777/* Send response to a label manager connect request to client */
1778static int
1779zsend_label_manager_connect_response (struct zserv *client, vrf_id_t vrf_id, u_short result)
1780{
1781 struct stream *s;
1782
1783 s = client->obuf;
1784 stream_reset (s);
1785
1786 zserv_create_header (s, ZEBRA_LABEL_MANAGER_CONNECT, vrf_id);
1787
1788 /* result */
1789 stream_putc (s, result);
1790
1791 /* Write packet size. */
1792 stream_putw_at (s, 0, stream_get_endp (s));
1793
1794 return writen (client->sock, s->data, stream_get_endp (s));
1795}
1796
1797static void
1798zread_label_manager_connect (struct zserv *client, vrf_id_t vrf_id)
1799{
1800 struct stream *s;
1801 /* type of protocol (lib/zebra.h) */
1802 u_char proto;
1803 u_short instance;
1804
1805 /* Get input stream. */
1806 s = client->ibuf;
1807
1808 /* Get data. */
1809 proto = stream_getc (s);
1810 instance = stream_getw (s);
1811
1812 /* accept only dynamic routing protocols */
1813 if ((proto >= ZEBRA_ROUTE_MAX)
1814 || (proto <= ZEBRA_ROUTE_STATIC))
1815 {
1816 zlog_err ("client %d has wrong protocol %s",
1817 client->sock, zebra_route_string(proto));
1818 zsend_label_manager_connect_response (client, vrf_id, 1);
1819 return;
1820 }
1821 zlog_notice ("client %d with instance %u connected as %s",
1822 client->sock, instance, zebra_route_string(proto));
1823 client->proto = proto;
1824 client->instance = instance;
1825
1826 /*
1827 Release previous labels of same protocol and instance.
1828 This is done in case it restarted from an unexpected shutdown.
1829 */
1830 release_daemon_chunks (proto, instance);
1831
1832 zlog_debug (" Label Manager client connected: sock %d, proto %s, instance %u",
1833 client->sock, zebra_route_string(proto), instance);
1834 /* send response back */
1835 zsend_label_manager_connect_response (client, vrf_id, 0);
1836}
1837/* Send response to a get label chunk request to client */
1838static int
1839zsend_assign_label_chunk_response (struct zserv *client, vrf_id_t vrf_id,
1840 struct label_manager_chunk *lmc)
1841{
1842 struct stream *s;
1843
1844 s = client->obuf;
1845 stream_reset (s);
1846
1847 zserv_create_header (s, ZEBRA_GET_LABEL_CHUNK, vrf_id);
1848
1849 if (lmc)
1850 {
1851 /* keep */
1852 stream_putc (s, lmc->keep);
1853 /* start and end labels */
1854 stream_putl (s, lmc->start);
1855 stream_putl (s, lmc->end);
1856
1857 }
1858
1859 /* Write packet size. */
1860 stream_putw_at (s, 0, stream_get_endp (s));
1861
1862 return writen (client->sock, s->data, stream_get_endp (s));
1863}
1864
1865static void
1866zread_get_label_chunk (struct zserv *client, vrf_id_t vrf_id)
1867{
1868 struct stream *s;
1869 u_char keep;
1870 uint32_t size;
1871 struct label_manager_chunk *lmc;
1872
1873 /* Get input stream. */
1874 s = client->ibuf;
1875
1876 /* Get data. */
1877 keep = stream_getc (s);
1878 size = stream_getl (s);
1879
1880 lmc = assign_label_chunk (client->proto, client->instance, keep, size);
1881 if (!lmc)
1882 zlog_err ("%s: Unable to assign Label Chunk of size %u", __func__, size);
1883 else
1884 zlog_debug ("Assigned Label Chunk %u - %u to %u",
1885 lmc->start, lmc->end, keep);
1886 /* send response back */
1887 zsend_assign_label_chunk_response (client, vrf_id, lmc);
1888}
1889
1890static void
1891zread_release_label_chunk (struct zserv *client)
1892{
1893 struct stream *s;
1894 uint32_t start, end;
1895
1896 /* Get input stream. */
1897 s = client->ibuf;
1898
1899 /* Get data. */
1900 start = stream_getl (s);
1901 end = stream_getl (s);
1902
1903 release_label_chunk (client->proto, client->instance, start, end);
1904}
1905static void
1906zread_label_manager_request (int cmd, struct zserv *client, vrf_id_t vrf_id)
1907{
1908 /* to avoid sending other messages like ZERBA_INTERFACE_UP */
1909 if (cmd == ZEBRA_LABEL_MANAGER_CONNECT)
1910 client->is_synchronous = 1;
1911
1912 /* external label manager */
1913 if (lm_is_external)
1914 {
1915 if (zread_relay_label_manager_request (cmd, client) != 0)
1916 zsend_label_manager_connect_response (client, vrf_id, 1);
1917 }
1918 /* this is a label manager */
1919 else
1920 {
1921 if (cmd == ZEBRA_LABEL_MANAGER_CONNECT)
1922 zread_label_manager_connect (client, vrf_id);
1923 else
1924 {
1925 /* Sanity: don't allow 'unidentified' requests */
1926 if (!client->proto)
1927 {
1928 zlog_err ("Got label request from an unidentified client");
1929 return;
1930 }
1931 if (cmd == ZEBRA_GET_LABEL_CHUNK)
1932 zread_get_label_chunk (client, vrf_id);
1933 else if (cmd == ZEBRA_RELEASE_LABEL_CHUNK)
1934 zread_release_label_chunk (client);
1935 }
1936 }
1937}
ce549947 1938
4b33b75a 1939/* Cleanup registered nexthops (across VRFs) upon client disconnect. */
1940static void
1941zebra_client_close_cleanup_rnh (struct zserv *client)
1942{
1a1a7065 1943 struct vrf *vrf;
4b33b75a 1944 struct zebra_vrf *zvrf;
1945
1a1a7065 1946 RB_FOREACH (vrf, vrf_id_head, &vrfs_by_id)
4b33b75a 1947 {
1a1a7065 1948 if ((zvrf = vrf->info) != NULL)
4b33b75a 1949 {
661512bf
RW
1950 zebra_cleanup_rnh_client(zvrf_id (zvrf), AF_INET, client, RNH_NEXTHOP_TYPE);
1951 zebra_cleanup_rnh_client(zvrf_id (zvrf), AF_INET6, client, RNH_NEXTHOP_TYPE);
1952 zebra_cleanup_rnh_client(zvrf_id (zvrf), AF_INET, client, RNH_IMPORT_CHECK_TYPE);
1953 zebra_cleanup_rnh_client(zvrf_id (zvrf), AF_INET6, client, RNH_IMPORT_CHECK_TYPE);
ce549947
RW
1954 if (client->proto == ZEBRA_ROUTE_LDP)
1955 {
1956 hash_iterate(zvrf->lsp_table, mpls_ldp_lsp_uninstall_all,
1957 zvrf->lsp_table);
1958 mpls_ldp_ftn_uninstall_all (zvrf, AFI_IP);
1959 mpls_ldp_ftn_uninstall_all (zvrf, AFI_IP6);
1960 }
4b33b75a 1961 }
1962 }
1963}
1964
718e3744 1965/* Close zebra client. */
b9df2d25 1966static void
718e3744 1967zebra_client_close (struct zserv *client)
1968{
567b877d 1969 /* Send client de-registration to BFD */
2376c3f2 1970 zebra_ptm_bfd_client_deregister(client->proto);
567b877d 1971
4b33b75a 1972 /* Cleanup any registered nexthops - across all VRFs. */
1973 zebra_client_close_cleanup_rnh (client);
fb018d25 1974
fea12efb 1975 /* Release Label Manager chunks */
1976 release_daemon_chunks (client->proto, client->instance);
1977
718e3744 1978 /* Close file descriptor. */
1979 if (client->sock)
1980 {
7c8ff89e
DS
1981 unsigned long nroutes;
1982
718e3744 1983 close (client->sock);
7c8ff89e
DS
1984 nroutes = rib_score_proto (client->proto, client->instance);
1985 zlog_notice ("client %d disconnected. %lu %s routes removed from the rib",
1986 client->sock, nroutes, zebra_route_string (client->proto));
718e3744 1987 client->sock = -1;
1988 }
1989
1990 /* Free stream buffers. */
1991 if (client->ibuf)
1992 stream_free (client->ibuf);
1993 if (client->obuf)
1994 stream_free (client->obuf);
719e9741 1995 if (client->wb)
1996 buffer_free(client->wb);
718e3744 1997
1998 /* Release threads. */
1999 if (client->t_read)
2000 thread_cancel (client->t_read);
2001 if (client->t_write)
2002 thread_cancel (client->t_write);
719e9741 2003 if (client->t_suicide)
2004 thread_cancel (client->t_suicide);
718e3744 2005
2006 /* Free client structure. */
b21b19c5 2007 listnode_delete (zebrad.client_list, client);
c66f9c61 2008 XFREE (MTYPE_TMP, client);
718e3744 2009}
2010
2011/* Make new client. */
b9df2d25 2012static void
718e3744 2013zebra_client_create (int sock)
2014{
2015 struct zserv *client;
7076bb2f
FL
2016 int i;
2017 afi_t afi;
718e3744 2018
c66f9c61 2019 client = XCALLOC (MTYPE_TMP, sizeof (struct zserv));
718e3744 2020
2021 /* Make client input/output buffer. */
2022 client->sock = sock;
2023 client->ibuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
2024 client->obuf = stream_new (ZEBRA_MAX_PACKET_SIZ);
719e9741 2025 client->wb = buffer_new(0);
718e3744 2026
2027 /* Set table number. */
b21b19c5 2028 client->rtm_table = zebrad.rtm_table_default;
718e3744 2029
cf672a86 2030 client->connect_time = monotime(NULL);
7076bb2f
FL
2031 /* Initialize flags */
2032 for (afi = AFI_IP; afi < AFI_MAX; afi++)
2033 for (i = 0; i < ZEBRA_ROUTE_MAX; i++)
2034 client->redist[afi][i] = vrf_bitmap_init ();
2035 client->redist_default = vrf_bitmap_init ();
2036 client->ifinfo = vrf_bitmap_init ();
2037 client->ridinfo = vrf_bitmap_init ();
04b02fda 2038
fea12efb 2039 /* by default, it's not a synchronous client */
2040 client->is_synchronous = 0;
2041
718e3744 2042 /* Add this client to linked list. */
b21b19c5 2043 listnode_add (zebrad.client_list, client);
718e3744 2044
2045 /* Make new read thread. */
2046 zebra_event (ZEBRA_READ, sock, client);
12f6fb97
DS
2047
2048 zebra_vrf_update_all (client);
718e3744 2049}
2050
2051/* Handler of zebra service request. */
b9df2d25 2052static int
718e3744 2053zebra_client_read (struct thread *thread)
2054{
2055 int sock;
2056 struct zserv *client;
57a1477b 2057 size_t already;
c1b9800a 2058 uint16_t length, command;
2059 uint8_t marker, version;
7076bb2f 2060 vrf_id_t vrf_id;
d651649e 2061 struct zebra_vrf *zvrf;
718e3744 2062
2063 /* Get thread data. Reset reading thread because I'm running. */
2064 sock = THREAD_FD (thread);
2065 client = THREAD_ARG (thread);
2066 client->t_read = NULL;
2067
719e9741 2068 if (client->t_suicide)
718e3744 2069 {
719e9741 2070 zebra_client_close(client);
718e3744 2071 return -1;
2072 }
719e9741 2073
2074 /* Read length and command (if we don't have it already). */
57a1477b 2075 if ((already = stream_get_endp(client->ibuf)) < ZEBRA_HEADER_SIZE)
719e9741 2076 {
57a1477b 2077 ssize_t nbyte;
719e9741 2078 if (((nbyte = stream_read_try (client->ibuf, sock,
57a1477b 2079 ZEBRA_HEADER_SIZE-already)) == 0) ||
719e9741 2080 (nbyte == -1))
2081 {
2082 if (IS_ZEBRA_DEBUG_EVENT)
2083 zlog_debug ("connection closed socket [%d]", sock);
2084 zebra_client_close (client);
2085 return -1;
2086 }
57a1477b 2087 if (nbyte != (ssize_t)(ZEBRA_HEADER_SIZE-already))
719e9741 2088 {
2089 /* Try again later. */
2090 zebra_event (ZEBRA_READ, sock, client);
2091 return 0;
2092 }
57a1477b 2093 already = ZEBRA_HEADER_SIZE;
719e9741 2094 }
2095
2096 /* Reset to read from the beginning of the incoming packet. */
2097 stream_set_getp(client->ibuf, 0);
2098
c1b9800a 2099 /* Fetch header values */
718e3744 2100 length = stream_getw (client->ibuf);
c1b9800a 2101 marker = stream_getc (client->ibuf);
2102 version = stream_getc (client->ibuf);
7076bb2f 2103 vrf_id = stream_getw (client->ibuf);
c1b9800a 2104 command = stream_getw (client->ibuf);
718e3744 2105
c1b9800a 2106 if (marker != ZEBRA_HEADER_MARKER || version != ZSERV_VERSION)
2107 {
2108 zlog_err("%s: socket %d version mismatch, marker %d, version %d",
2109 __func__, sock, marker, version);
2110 zebra_client_close (client);
2111 return -1;
2112 }
719e9741 2113 if (length < ZEBRA_HEADER_SIZE)
718e3744 2114 {
57a1477b 2115 zlog_warn("%s: socket %d message length %u is less than header size %d",
2116 __func__, sock, length, ZEBRA_HEADER_SIZE);
2117 zebra_client_close (client);
2118 return -1;
2119 }
2120 if (length > STREAM_SIZE(client->ibuf))
2121 {
2122 zlog_warn("%s: socket %d message length %u exceeds buffer size %lu",
2123 __func__, sock, length, (u_long)STREAM_SIZE(client->ibuf));
718e3744 2124 zebra_client_close (client);
2125 return -1;
2126 }
2127
718e3744 2128 /* Read rest of data. */
57a1477b 2129 if (already < length)
718e3744 2130 {
57a1477b 2131 ssize_t nbyte;
2132 if (((nbyte = stream_read_try (client->ibuf, sock,
2133 length-already)) == 0) ||
2134 (nbyte == -1))
718e3744 2135 {
2136 if (IS_ZEBRA_DEBUG_EVENT)
b6178002 2137 zlog_debug ("connection closed [%d] when reading zebra data", sock);
718e3744 2138 zebra_client_close (client);
2139 return -1;
2140 }
57a1477b 2141 if (nbyte != (ssize_t)(length-already))
719e9741 2142 {
2143 /* Try again later. */
2144 zebra_event (ZEBRA_READ, sock, client);
2145 return 0;
2146 }
718e3744 2147 }
2148
719e9741 2149 length -= ZEBRA_HEADER_SIZE;
2150
718e3744 2151 /* Debug packet information. */
2152 if (IS_ZEBRA_DEBUG_EVENT)
b6178002 2153 zlog_debug ("zebra message comes from socket [%d]", sock);
718e3744 2154
2155 if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
7076bb2f
FL
2156 zlog_debug ("zebra message received [%s] %d in VRF %u",
2157 zserv_command_string (command), length, vrf_id);
718e3744 2158
cf672a86 2159 client->last_read_time = monotime(NULL);
04b02fda
DS
2160 client->last_read_cmd = command;
2161
5f3d1bdf 2162 zvrf = zebra_vrf_lookup_by_id (vrf_id);
d651649e
DS
2163 if (!zvrf)
2164 {
2165 if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV)
2166 zlog_debug ("zebra received unknown VRF[%u]", vrf_id);
2167 goto zclient_read_out;
2168 }
2169
718e3744 2170 switch (command)
2171 {
18a6dce6 2172 case ZEBRA_ROUTER_ID_ADD:
d651649e 2173 zread_router_id_add (client, length, zvrf);
18a6dce6 2174 break;
2175 case ZEBRA_ROUTER_ID_DELETE:
d651649e 2176 zread_router_id_delete (client, length, zvrf);
18a6dce6 2177 break;
718e3744 2178 case ZEBRA_INTERFACE_ADD:
d651649e 2179 zread_interface_add (client, length, zvrf);
718e3744 2180 break;
2181 case ZEBRA_INTERFACE_DELETE:
d651649e 2182 zread_interface_delete (client, length, zvrf);
718e3744 2183 break;
2184 case ZEBRA_IPV4_ROUTE_ADD:
d651649e 2185 zread_ipv4_add (client, length, zvrf);
718e3744 2186 break;
2187 case ZEBRA_IPV4_ROUTE_DELETE:
d651649e 2188 zread_ipv4_delete (client, length, zvrf);
718e3744 2189 break;
8a92a8a0 2190 case ZEBRA_IPV4_ROUTE_IPV6_NEXTHOP_ADD:
d651649e 2191 zread_ipv4_route_ipv6_nexthop_add (client, length, zvrf);
8a92a8a0 2192 break;
65efcfce
LB
2193 case ZEBRA_IPV4_NEXTHOP_ADD:
2194 zread_ipv4_add(client, length, zvrf); /* LB: r1.0 merge - id was 1 */
2195 break;
2196 case ZEBRA_IPV4_NEXTHOP_DELETE:
2197 zread_ipv4_delete(client, length, zvrf); /* LB: r1.0 merge - id was 1 */
2198 break;
718e3744 2199 case ZEBRA_IPV6_ROUTE_ADD:
d651649e 2200 zread_ipv6_add (client, length, zvrf);
718e3744 2201 break;
2202 case ZEBRA_IPV6_ROUTE_DELETE:
d651649e 2203 zread_ipv6_delete (client, length, zvrf);
718e3744 2204 break;
718e3744 2205 case ZEBRA_REDISTRIBUTE_ADD:
d651649e 2206 zebra_redistribute_add (command, client, length, zvrf);
718e3744 2207 break;
2208 case ZEBRA_REDISTRIBUTE_DELETE:
d651649e 2209 zebra_redistribute_delete (command, client, length, zvrf);
718e3744 2210 break;
2211 case ZEBRA_REDISTRIBUTE_DEFAULT_ADD:
d651649e 2212 zebra_redistribute_default_add (command, client, length, zvrf);
718e3744 2213 break;
2214 case ZEBRA_REDISTRIBUTE_DEFAULT_DELETE:
d651649e 2215 zebra_redistribute_default_delete (command, client, length, zvrf);
718e3744 2216 break;
6f61a5a3
EM
2217 case ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB:
2218 zread_ipv4_nexthop_lookup_mrib (client, length, zvrf);
718e3744 2219 break;
2ea1ab1c
VT
2220 case ZEBRA_HELLO:
2221 zread_hello (client);
2222 break;
fb018d25 2223 case ZEBRA_NEXTHOP_REGISTER:
d651649e 2224 zserv_rnh_register(client, sock, length, RNH_NEXTHOP_TYPE, zvrf);
fb018d25
DS
2225 break;
2226 case ZEBRA_NEXTHOP_UNREGISTER:
d651649e 2227 zserv_rnh_unregister(client, sock, length, RNH_NEXTHOP_TYPE, zvrf);
078430f6
DS
2228 break;
2229 case ZEBRA_IMPORT_ROUTE_REGISTER:
d651649e 2230 zserv_rnh_register(client, sock, length, RNH_IMPORT_CHECK_TYPE, zvrf);
078430f6
DS
2231 break;
2232 case ZEBRA_IMPORT_ROUTE_UNREGISTER:
d651649e 2233 zserv_rnh_unregister(client, sock, length, RNH_IMPORT_CHECK_TYPE, zvrf);
fb018d25 2234 break;
c43ed2e4
DS
2235 case ZEBRA_BFD_DEST_UPDATE:
2236 case ZEBRA_BFD_DEST_REGISTER:
d651649e 2237 zebra_ptm_bfd_dst_register(client, sock, length, command, zvrf);
c43ed2e4
DS
2238 break;
2239 case ZEBRA_BFD_DEST_DEREGISTER:
d651649e 2240 zebra_ptm_bfd_dst_deregister(client, sock, length, zvrf);
c43ed2e4 2241 break;
7076bb2f 2242 case ZEBRA_VRF_UNREGISTER:
d651649e 2243 zread_vrf_unregister (client, length, zvrf);
7076bb2f 2244 break;
055c4dfc 2245 case ZEBRA_BFD_CLIENT_REGISTER:
2246 zebra_ptm_bfd_client_register(client, sock, length);
2247 break;
4a04e5f7 2248 case ZEBRA_INTERFACE_ENABLE_RADV:
2249 zebra_interface_radv_set (client, sock, length, zvrf, 1);
2250 break;
2251 case ZEBRA_INTERFACE_DISABLE_RADV:
2252 zebra_interface_radv_set (client, sock, length, zvrf, 0);
2253 break;
ce549947
RW
2254 case ZEBRA_MPLS_LABELS_ADD:
2255 case ZEBRA_MPLS_LABELS_DELETE:
2256 zread_mpls_labels (command, client, length, vrf_id);
2257 break;
e3be0432
DS
2258 case ZEBRA_IPMR_ROUTE_STATS:
2259 zebra_ipmr_route_stats (client, sock, length, zvrf);
2260 break;
fea12efb 2261 case ZEBRA_LABEL_MANAGER_CONNECT:
2262 case ZEBRA_GET_LABEL_CHUNK:
2263 case ZEBRA_RELEASE_LABEL_CHUNK:
2264 zread_label_manager_request (command, client, vrf_id);
2265 break;
718e3744 2266 default:
2267 zlog_info ("Zebra received unknown command %d", command);
2268 break;
2269 }
2270
719e9741 2271 if (client->t_suicide)
2272 {
2273 /* No need to wait for thread callback, just kill immediately. */
2274 zebra_client_close(client);
2275 return -1;
2276 }
2277
d651649e 2278 zclient_read_out:
718e3744 2279 stream_reset (client->ibuf);
2280 zebra_event (ZEBRA_READ, sock, client);
718e3744 2281 return 0;
2282}
2283
718e3744 2284
2285/* Accept code of zebra server socket. */
b9df2d25 2286static int
718e3744 2287zebra_accept (struct thread *thread)
2288{
2289 int accept_sock;
2290 int client_sock;
2291 struct sockaddr_in client;
2292 socklen_t len;
2293
2294 accept_sock = THREAD_FD (thread);
2295
719e9741 2296 /* Reregister myself. */
2297 zebra_event (ZEBRA_SERV, accept_sock, NULL);
2298
718e3744 2299 len = sizeof (struct sockaddr_in);
2300 client_sock = accept (accept_sock, (struct sockaddr *) &client, &len);
2301
2302 if (client_sock < 0)
2303 {
6099b3b5 2304 zlog_warn ("Can't accept zebra socket: %s", safe_strerror (errno));
718e3744 2305 return -1;
2306 }
2307
ccf3557b 2308 /* Make client socket non-blocking. */
719e9741 2309 set_nonblocking(client_sock);
865b852c 2310
718e3744 2311 /* Create new zebra client. */
2312 zebra_client_create (client_sock);
2313
718e3744 2314 return 0;
2315}
2316
b9df2d25 2317#ifdef HAVE_TCP_ZEBRA
718e3744 2318/* Make zebra's server socket. */
b9df2d25 2319static void
718e3744 2320zebra_serv ()
2321{
2322 int ret;
2323 int accept_sock;
2324 struct sockaddr_in addr;
2325
2326 accept_sock = socket (AF_INET, SOCK_STREAM, 0);
2327
2328 if (accept_sock < 0)
2329 {
3d1dc857 2330 zlog_warn ("Can't create zserv stream socket: %s",
2331 safe_strerror (errno));
718e3744 2332 zlog_warn ("zebra can't provice full functionality due to above error");
2333 return;
2334 }
2335
2336 memset (&addr, 0, sizeof (struct sockaddr_in));
2337 addr.sin_family = AF_INET;
2338 addr.sin_port = htons (ZEBRA_PORT);
6f0e3f6e 2339#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN
718e3744 2340 addr.sin_len = sizeof (struct sockaddr_in);
6f0e3f6e 2341#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */
718e3744 2342 addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
2343
2344 sockopt_reuseaddr (accept_sock);
2345 sockopt_reuseport (accept_sock);
2346
edd7c245 2347 if ( zserv_privs.change(ZPRIVS_RAISE) )
4525281a 2348 zlog_err("Can't raise privileges");
edd7c245 2349
718e3744 2350 ret = bind (accept_sock, (struct sockaddr *)&addr,
2351 sizeof (struct sockaddr_in));
2352 if (ret < 0)
2353 {
3d1dc857 2354 zlog_warn ("Can't bind to stream socket: %s",
2355 safe_strerror (errno));
718e3744 2356 zlog_warn ("zebra can't provice full functionality due to above error");
2357 close (accept_sock); /* Avoid sd leak. */
2358 return;
2359 }
edd7c245 2360
2361 if ( zserv_privs.change(ZPRIVS_LOWER) )
4525281a 2362 zlog_err("Can't lower privileges");
718e3744 2363
2364 ret = listen (accept_sock, 1);
2365 if (ret < 0)
2366 {
3d1dc857 2367 zlog_warn ("Can't listen to stream socket: %s",
2368 safe_strerror (errno));
718e3744 2369 zlog_warn ("zebra can't provice full functionality due to above error");
2370 close (accept_sock); /* Avoid sd leak. */
2371 return;
2372 }
2373
2374 zebra_event (ZEBRA_SERV, accept_sock, NULL);
2375}
fbedba64 2376#else /* HAVE_TCP_ZEBRA */
718e3744 2377
2378/* For sockaddr_un. */
2379#include <sys/un.h>
2380
2381/* zebra server UNIX domain socket. */
b9df2d25 2382static void
fce954f8 2383zebra_serv_un (const char *path)
718e3744 2384{
2385 int ret;
2386 int sock, len;
2387 struct sockaddr_un serv;
2388 mode_t old_mask;
2389
2390 /* First of all, unlink existing socket */
2391 unlink (path);
2392
2393 /* Set umask */
2394 old_mask = umask (0077);
2395
2396 /* Make UNIX domain socket. */
2397 sock = socket (AF_UNIX, SOCK_STREAM, 0);
2398 if (sock < 0)
2399 {
3d1dc857 2400 zlog_warn ("Can't create zserv unix socket: %s",
2401 safe_strerror (errno));
2402 zlog_warn ("zebra can't provide full functionality due to above error");
718e3744 2403 return;
2404 }
2405
2406 /* Make server socket. */
2407 memset (&serv, 0, sizeof (struct sockaddr_un));
2408 serv.sun_family = AF_UNIX;
2409 strncpy (serv.sun_path, path, strlen (path));
6f0e3f6e 2410#ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
718e3744 2411 len = serv.sun_len = SUN_LEN(&serv);
2412#else
2413 len = sizeof (serv.sun_family) + strlen (serv.sun_path);
6f0e3f6e 2414#endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
718e3744 2415
2416 ret = bind (sock, (struct sockaddr *) &serv, len);
2417 if (ret < 0)
2418 {
3d1dc857 2419 zlog_warn ("Can't bind to unix socket %s: %s",
2420 path, safe_strerror (errno));
2421 zlog_warn ("zebra can't provide full functionality due to above error");
718e3744 2422 close (sock);
2423 return;
2424 }
2425
2426 ret = listen (sock, 5);
2427 if (ret < 0)
2428 {
3d1dc857 2429 zlog_warn ("Can't listen to unix socket %s: %s",
2430 path, safe_strerror (errno));
2431 zlog_warn ("zebra can't provide full functionality due to above error");
718e3744 2432 close (sock);
2433 return;
2434 }
2435
2436 umask (old_mask);
2437
2438 zebra_event (ZEBRA_SERV, sock, NULL);
2439}
fbedba64 2440#endif /* HAVE_TCP_ZEBRA */
6b0655a2 2441
718e3744 2442
b9df2d25 2443static void
718e3744 2444zebra_event (enum event event, int sock, struct zserv *client)
2445{
2446 switch (event)
2447 {
2448 case ZEBRA_SERV:
b21b19c5 2449 thread_add_read (zebrad.master, zebra_accept, client, sock);
718e3744 2450 break;
2451 case ZEBRA_READ:
2452 client->t_read =
b21b19c5 2453 thread_add_read (zebrad.master, zebra_client_read, client, sock);
718e3744 2454 break;
2455 case ZEBRA_WRITE:
2456 /**/
2457 break;
2458 }
2459}
6b0655a2 2460
04b02fda
DS
2461#define ZEBRA_TIME_BUF 32
2462static char *
2463zserv_time_buf(time_t *time1, char *buf, int buflen)
2464{
2465 struct tm *tm;
2466 time_t now;
2467
2468 assert (buf != NULL);
2469 assert (buflen >= ZEBRA_TIME_BUF);
2470 assert (time1 != NULL);
2471
2472 if (!*time1)
2473 {
2474 snprintf(buf, buflen, "never ");
2475 return (buf);
2476 }
2477
cf672a86 2478 now = monotime(NULL);
04b02fda
DS
2479 now -= *time1;
2480 tm = gmtime(&now);
2481
2482 /* Making formatted timer strings. */
2483#define ONE_DAY_SECOND 60*60*24
2484#define ONE_WEEK_SECOND 60*60*24*7
2485
2486 if (now < ONE_DAY_SECOND)
2487 snprintf (buf, buflen, "%02d:%02d:%02d",
2488 tm->tm_hour, tm->tm_min, tm->tm_sec);
2489 else if (now < ONE_WEEK_SECOND)
2490 snprintf (buf, buflen, "%dd%02dh%02dm",
2491 tm->tm_yday, tm->tm_hour, tm->tm_min);
2492 else
2493 snprintf (buf, buflen, "%02dw%dd%02dh",
2494 tm->tm_yday/7, tm->tm_yday - ((tm->tm_yday/7) * 7), tm->tm_hour);
2495 return buf;
2496}
2497
2498static void
2499zebra_show_client_detail (struct vty *vty, struct zserv *client)
2500{
2501 char cbuf[ZEBRA_TIME_BUF], rbuf[ZEBRA_TIME_BUF];
2502 char wbuf[ZEBRA_TIME_BUF], nhbuf[ZEBRA_TIME_BUF], mbuf[ZEBRA_TIME_BUF];
2503
7c8ff89e
DS
2504 vty_out (vty, "Client: %s", zebra_route_string(client->proto));
2505 if (client->instance)
2506 vty_out (vty, " Instance: %d", client->instance);
2507 vty_out (vty, "%s", VTY_NEWLINE);
2508
04b02fda
DS
2509 vty_out (vty, "------------------------ %s", VTY_NEWLINE);
2510 vty_out (vty, "FD: %d %s", client->sock, VTY_NEWLINE);
2511 vty_out (vty, "Route Table ID: %d %s", client->rtm_table, VTY_NEWLINE);
2512
2513 vty_out (vty, "Connect Time: %s %s",
2514 zserv_time_buf(&client->connect_time, cbuf, ZEBRA_TIME_BUF),
2515 VTY_NEWLINE);
2516 if (client->nh_reg_time)
2517 {
2518 vty_out (vty, "Nexthop Registry Time: %s %s",
2519 zserv_time_buf(&client->nh_reg_time, nhbuf, ZEBRA_TIME_BUF),
2520 VTY_NEWLINE);
2521 if (client->nh_last_upd_time)
2522 vty_out (vty, "Nexthop Last Update Time: %s %s",
2523 zserv_time_buf(&client->nh_last_upd_time, mbuf, ZEBRA_TIME_BUF),
2524 VTY_NEWLINE);
2525 else
2526 vty_out (vty, "No Nexthop Update sent%s", VTY_NEWLINE);
2527 }
2528 else
2529 vty_out (vty, "Not registered for Nexthop Updates%s", VTY_NEWLINE);
2530
2531 vty_out (vty, "Last Msg Rx Time: %s %s",
2532 zserv_time_buf(&client->last_read_time, rbuf, ZEBRA_TIME_BUF),
2533 VTY_NEWLINE);
2534 vty_out (vty, "Last Msg Tx Time: %s %s",
2535 zserv_time_buf(&client->last_write_time, wbuf, ZEBRA_TIME_BUF),
2536 VTY_NEWLINE);
2537 if (client->last_read_time)
2538 vty_out (vty, "Last Rcvd Cmd: %s %s",
2539 zserv_command_string(client->last_read_cmd), VTY_NEWLINE);
2540 if (client->last_write_time)
2541 vty_out (vty, "Last Sent Cmd: %s %s",
2542 zserv_command_string(client->last_write_cmd), VTY_NEWLINE);
2543 vty_out (vty, "%s", VTY_NEWLINE);
2544
2545 vty_out (vty, "Type Add Update Del %s", VTY_NEWLINE);
2546 vty_out (vty, "================================================== %s", VTY_NEWLINE);
2547 vty_out (vty, "IPv4 %-12d%-12d%-12d%s", client->v4_route_add_cnt,
2548 client->v4_route_upd8_cnt, client->v4_route_del_cnt, VTY_NEWLINE);
2549 vty_out (vty, "IPv6 %-12d%-12d%-12d%s", client->v6_route_add_cnt,
2550 client->v6_route_upd8_cnt, client->v6_route_del_cnt, VTY_NEWLINE);
2551 vty_out (vty, "Redist:v4 %-12d%-12d%-12d%s", client->redist_v4_add_cnt, 0,
2552 client->redist_v4_del_cnt, VTY_NEWLINE);
2553 vty_out (vty, "Redist:v6 %-12d%-12d%-12d%s", client->redist_v6_add_cnt, 0,
2554 client->redist_v6_del_cnt, VTY_NEWLINE);
2555 vty_out (vty, "Connected %-12d%-12d%-12d%s", client->ifadd_cnt, 0,
2556 client->ifdel_cnt, VTY_NEWLINE);
c43ed2e4
DS
2557 vty_out (vty, "BFD peer %-12d%-12d%-12d%s", client->bfd_peer_add_cnt,
2558 client->bfd_peer_upd8_cnt, client->bfd_peer_del_cnt, VTY_NEWLINE);
04b02fda
DS
2559 vty_out (vty, "Interface Up Notifications: %d%s", client->ifup_cnt,
2560 VTY_NEWLINE);
2561 vty_out (vty, "Interface Down Notifications: %d%s", client->ifdown_cnt,
2562 VTY_NEWLINE);
2563
2564 vty_out (vty, "%s", VTY_NEWLINE);
2565 return;
2566}
2567
2568static void
2569zebra_show_client_brief (struct vty *vty, struct zserv *client)
2570{
2571 char cbuf[ZEBRA_TIME_BUF], rbuf[ZEBRA_TIME_BUF];
2572 char wbuf[ZEBRA_TIME_BUF];
2573
2574 vty_out (vty, "%-8s%12s %12s%12s%8d/%-8d%8d/%-8d%s",
2575 zebra_route_string(client->proto),
2576 zserv_time_buf(&client->connect_time, cbuf, ZEBRA_TIME_BUF),
2577 zserv_time_buf(&client->last_read_time, rbuf, ZEBRA_TIME_BUF),
2578 zserv_time_buf(&client->last_write_time, wbuf, ZEBRA_TIME_BUF),
2579 client->v4_route_add_cnt+client->v4_route_upd8_cnt,
2580 client->v4_route_del_cnt,
2581 client->v6_route_add_cnt+client->v6_route_upd8_cnt,
2582 client->v6_route_del_cnt, VTY_NEWLINE);
2583
2584}
2585
8ed6821e 2586struct zserv *
2587zebra_find_client (u_char proto)
2588{
2589 struct listnode *node, *nnode;
2590 struct zserv *client;
2591
2592 for (ALL_LIST_ELEMENTS (zebrad.client_list, node, nnode, client))
2593 {
2594 if (client->proto == proto)
2595 return client;
2596 }
2597
2598 return NULL;
2599}
2600
9bf75362 2601#ifdef HAVE_NETLINK
718e3744 2602/* Display default rtm_table for all clients. */
2603DEFUN (show_table,
2604 show_table_cmd,
2605 "show table",
2606 SHOW_STR
2607 "default routing table to use for all clients\n")
2608{
b21b19c5 2609 vty_out (vty, "table %d%s", zebrad.rtm_table_default,
718e3744 2610 VTY_NEWLINE);
2611 return CMD_SUCCESS;
2612}
2613
6147e2c6 2614DEFUN (config_table,
718e3744 2615 config_table_cmd,
2616 "table TABLENO",
2617 "Configure target kernel routing table\n"
2618 "TABLE integer\n")
2619{
6af6be86 2620 zebrad.rtm_table_default = strtol (argv[1]->arg, (char**)0, 10);
718e3744 2621 return CMD_SUCCESS;
2622}
2623
813d4307
DW
2624DEFUN (no_config_table,
2625 no_config_table_cmd,
6af6be86 2626 "no table [TABLENO]",
813d4307
DW
2627 NO_STR
2628 "Configure target kernel routing table\n"
2629 "TABLE integer\n")
2630{
2631 zebrad.rtm_table_default = 0;
2632 return CMD_SUCCESS;
2633}
9bf75362 2634#endif
813d4307 2635
647e4f1f 2636DEFUN (ip_forwarding,
2637 ip_forwarding_cmd,
2638 "ip forwarding",
2639 IP_STR
2640 "Turn on IP forwarding")
2641{
2642 int ret;
2643
2644 ret = ipforward ();
b71f00f2 2645 if (ret == 0)
2646 ret = ipforward_on ();
647e4f1f 2647
647e4f1f 2648 if (ret == 0)
2649 {
2650 vty_out (vty, "Can't turn on IP forwarding%s", VTY_NEWLINE);
2651 return CMD_WARNING;
2652 }
2653
2654 return CMD_SUCCESS;
2655}
2656
718e3744 2657DEFUN (no_ip_forwarding,
2658 no_ip_forwarding_cmd,
2659 "no ip forwarding",
2660 NO_STR
2661 IP_STR
2662 "Turn off IP forwarding")
2663{
2664 int ret;
2665
2666 ret = ipforward ();
b71f00f2 2667 if (ret != 0)
2668 ret = ipforward_off ();
718e3744 2669
718e3744 2670 if (ret != 0)
2671 {
2672 vty_out (vty, "Can't turn off IP forwarding%s", VTY_NEWLINE);
2673 return CMD_WARNING;
2674 }
2675
2676 return CMD_SUCCESS;
2677}
2678
2679/* This command is for debugging purpose. */
2680DEFUN (show_zebra_client,
2681 show_zebra_client_cmd,
2682 "show zebra client",
2683 SHOW_STR
49d73233 2684 "Zebra information\n"
b9ee4999 2685 "Client information\n")
718e3744 2686{
52dc7ee6 2687 struct listnode *node;
718e3744 2688 struct zserv *client;
2689
1eb8ef25 2690 for (ALL_LIST_ELEMENTS_RO (zebrad.client_list, node, client))
04b02fda
DS
2691 zebra_show_client_detail(vty, client);
2692
2693 return CMD_SUCCESS;
2694}
2695
2696/* This command is for debugging purpose. */
2697DEFUN (show_zebra_client_summary,
2698 show_zebra_client_summary_cmd,
2699 "show zebra client summary",
2700 SHOW_STR
b9ee4999
DS
2701 "Zebra information brief\n"
2702 "Client information brief\n"
2703 "Brief Summary\n")
04b02fda
DS
2704{
2705 struct listnode *node;
2706 struct zserv *client;
2707
2708 vty_out (vty, "Name Connect Time Last Read Last Write IPv4 Routes IPv6 Routes %s",
2709 VTY_NEWLINE);
2710 vty_out (vty,"--------------------------------------------------------------------------------%s",
2711 VTY_NEWLINE);
2712
2713 for (ALL_LIST_ELEMENTS_RO (zebrad.client_list, node, client))
2714 zebra_show_client_brief(vty, client);
fb018d25 2715
04b02fda 2716 vty_out (vty, "Routes column shows (added+updated)/deleted%s", VTY_NEWLINE);
718e3744 2717 return CMD_SUCCESS;
2718}
2719
2720/* Table configuration write function. */
b9df2d25 2721static int
718e3744 2722config_write_table (struct vty *vty)
2723{
b21b19c5 2724 if (zebrad.rtm_table_default)
2725 vty_out (vty, "table %d%s", zebrad.rtm_table_default,
718e3744 2726 VTY_NEWLINE);
2727 return 0;
2728}
2729
2730/* table node for routing tables. */
7fc626de 2731static struct cmd_node table_node =
718e3744 2732{
2733 TABLE_NODE,
2734 "", /* This node has no interface. */
2735 1
2736};
6b0655a2 2737
718e3744 2738/* Only display ip forwarding is enabled or not. */
2739DEFUN (show_ip_forwarding,
2740 show_ip_forwarding_cmd,
2741 "show ip forwarding",
2742 SHOW_STR
2743 IP_STR
2744 "IP forwarding status\n")
2745{
2746 int ret;
2747
2748 ret = ipforward ();
2749
2750 if (ret == 0)
2751 vty_out (vty, "IP forwarding is off%s", VTY_NEWLINE);
2752 else
2753 vty_out (vty, "IP forwarding is on%s", VTY_NEWLINE);
2754 return CMD_SUCCESS;
2755}
2756
718e3744 2757/* Only display ipv6 forwarding is enabled or not. */
2758DEFUN (show_ipv6_forwarding,
2759 show_ipv6_forwarding_cmd,
2760 "show ipv6 forwarding",
2761 SHOW_STR
2762 "IPv6 information\n"
2763 "Forwarding status\n")
2764{
2765 int ret;
2766
2767 ret = ipforward_ipv6 ();
2768
2769 switch (ret)
2770 {
2771 case -1:
2772 vty_out (vty, "ipv6 forwarding is unknown%s", VTY_NEWLINE);
2773 break;
2774 case 0:
2775 vty_out (vty, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE);
2776 break;
2777 case 1:
2778 vty_out (vty, "ipv6 forwarding is %s%s", "on", VTY_NEWLINE);
2779 break;
2780 default:
2781 vty_out (vty, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE);
2782 break;
2783 }
2784 return CMD_SUCCESS;
2785}
2786
55906724 2787DEFUN (ipv6_forwarding,
2788 ipv6_forwarding_cmd,
2789 "ipv6 forwarding",
2790 IPV6_STR
2791 "Turn on IPv6 forwarding")
2792{
2793 int ret;
2794
41d3fc96 2795 ret = ipforward_ipv6 ();
b71f00f2 2796 if (ret == 0)
2797 ret = ipforward_ipv6_on ();
41d3fc96 2798
41d3fc96 2799 if (ret == 0)
55906724 2800 {
2801 vty_out (vty, "Can't turn on IPv6 forwarding%s", VTY_NEWLINE);
2802 return CMD_WARNING;
2803 }
2804
2805 return CMD_SUCCESS;
2806}
2807
718e3744 2808DEFUN (no_ipv6_forwarding,
2809 no_ipv6_forwarding_cmd,
2810 "no ipv6 forwarding",
2811 NO_STR
55906724 2812 IPV6_STR
2813 "Turn off IPv6 forwarding")
718e3744 2814{
2815 int ret;
2816
41d3fc96 2817 ret = ipforward_ipv6 ();
b71f00f2 2818 if (ret != 0)
2819 ret = ipforward_ipv6_off ();
41d3fc96 2820
718e3744 2821 if (ret != 0)
2822 {
2823 vty_out (vty, "Can't turn off IPv6 forwarding%s", VTY_NEWLINE);
2824 return CMD_WARNING;
2825 }
2826
2827 return CMD_SUCCESS;
2828}
2829
718e3744 2830/* IPForwarding configuration write function. */
719e9741 2831static int
718e3744 2832config_write_forwarding (struct vty *vty)
2833{
18a6dce6 2834 /* FIXME: Find better place for that. */
2835 router_id_write (vty);
2836
f40dd648
QY
2837 if (!ipforward ())
2838 vty_out (vty, "no ip forwarding%s", VTY_NEWLINE);
f40dd648
QY
2839 if (!ipforward_ipv6 ())
2840 vty_out (vty, "no ipv6 forwarding%s", VTY_NEWLINE);
718e3744 2841 vty_out (vty, "!%s", VTY_NEWLINE);
2842 return 0;
2843}
2844
2845/* table node for routing tables. */
7fc626de 2846static struct cmd_node forwarding_node =
718e3744 2847{
2848 FORWARDING_NODE,
2849 "", /* This node has no interface. */
2850 1
2851};
2852
718e3744 2853/* Initialisation of zebra and installation of commands. */
2854void
a1ac18c4 2855zebra_init (void)
718e3744 2856{
2857 /* Client list init. */
b21b19c5 2858 zebrad.client_list = list_new ();
718e3744 2859
718e3744 2860 /* Install configuration write function. */
2861 install_node (&table_node, config_write_table);
2862 install_node (&forwarding_node, config_write_forwarding);
2863
2864 install_element (VIEW_NODE, &show_ip_forwarding_cmd);
647e4f1f 2865 install_element (CONFIG_NODE, &ip_forwarding_cmd);
718e3744 2866 install_element (CONFIG_NODE, &no_ip_forwarding_cmd);
2867 install_element (ENABLE_NODE, &show_zebra_client_cmd);
04b02fda 2868 install_element (ENABLE_NODE, &show_zebra_client_summary_cmd);
718e3744 2869
2870#ifdef HAVE_NETLINK
2871 install_element (VIEW_NODE, &show_table_cmd);
718e3744 2872 install_element (CONFIG_NODE, &config_table_cmd);
813d4307 2873 install_element (CONFIG_NODE, &no_config_table_cmd);
718e3744 2874#endif /* HAVE_NETLINK */
2875
718e3744 2876 install_element (VIEW_NODE, &show_ipv6_forwarding_cmd);
55906724 2877 install_element (CONFIG_NODE, &ipv6_forwarding_cmd);
718e3744 2878 install_element (CONFIG_NODE, &no_ipv6_forwarding_cmd);
7514fb77
PJ
2879
2880 /* Route-map */
2881 zebra_route_map_init ();
718e3744 2882}
97be79f9
DO
2883
2884/* Make zebra server socket, wiping any existing one (see bug #403). */
2885void
b5114685 2886zebra_zserv_socket_init (char *path)
97be79f9
DO
2887{
2888#ifdef HAVE_TCP_ZEBRA
2889 zebra_serv ();
2890#else
b5114685 2891 zebra_serv_un (path ? path : ZEBRA_SERV_PATH);
97be79f9
DO
2892#endif /* HAVE_TCP_ZEBRA */
2893}