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