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