1/* Zebra daemon server routine. 2 * Copyright (C) 1997, 98, 99 Kunihiro Ishiguro 3 * 4 * This file is part of GNU Zebra. 5 * 6 * GNU Zebra is free software; you can redistribute it and/or modify it 7 * under the terms of the GNU General Public License as published by the 8 * Free Software Foundation; either version 2, or (at your option) any 9 * later version. 10 * 11 * GNU Zebra is distributed in the hope that it will be useful, but 12 * WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 * General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with GNU Zebra; see the file COPYING. If not, write to the 18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330, 19 * Boston, MA 02111-1307, USA. 20 */ 21 22#include <zebra.h> 23 24#include "prefix.h" 25#include "command.h" 26#include "if.h" 27#include "thread.h" 28#include "stream.h" 29#include "memory.h" 30#include "table.h" 31#include "rib.h" 32#include "network.h" 33#include "sockunion.h" 34#include "log.h" 35#include "zclient.h" 36#include "privs.h" 37#include "network.h" 38#include "buffer.h" 39 40#include "zebra/zserv.h" 41#include "zebra/router-id.h" 42#include "zebra/redistribute.h" 43#include "zebra/debug.h" 44#include "zebra/ipforward.h" 45 46/* Event list of zebra. */ 47enum event { ZEBRA_SERV, ZEBRA_READ, ZEBRA_WRITE }; 48 49extern struct zebra_t zebrad; 50 51static void zebra_event (enum event event, int sock, struct zserv *client); 52 53extern struct zebra_privs_t zserv_privs; 54 55static void zebra_client_close (struct zserv *client); 56 57static int 58zserv_delayed_close(struct thread *thread) 59{ 60 struct zserv *client = THREAD_ARG(thread); 61 62 client->t_suicide = NULL; 63 zebra_client_close(client); 64 return 0; 65} 66 67/* When client connects, it sends hello message 68 * with promise to send zebra routes of specific type. 69 * Zebra stores a socket fd of the client into 70 * this array. And use it to clean up routes that 71 * client didn't remove for some reasons after closing 72 * connection. 73 */ 74static int route_type_oaths[ZEBRA_ROUTE_MAX]; 75 76static int 77zserv_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 { 84 zebra_client_close(client); 85 return -1; 86 } 87 switch (buffer_flush_available(client->wb, client->sock)) 88 { 89 case BUFFER_ERROR: 90 zlog_warn("%s: buffer_flush_available failed on zserv client fd %d, " 91 "closing", __func__, client->sock); 92 zebra_client_close(client); 93 break; 94 case BUFFER_PENDING: 95 client->t_write = thread_add_write(zebrad.master, zserv_flush_data, 96 client, client->sock); 97 break; 98 case BUFFER_EMPTY: 99 break; 100 } 101 return 0; 102} 103 104static int 105zebra_server_send_message(struct zserv *client) 106{ 107 if (client->t_suicide) 108 return -1; 109 switch (buffer_write(client->wb, client->sock, STREAM_DATA(client->obuf), 110 stream_get_endp(client->obuf))) 111 { 112 case BUFFER_ERROR: 113 zlog_warn("%s: buffer_write failed to zserv client fd %d, closing", 114 __func__, client->sock); 115 /* Schedule a delayed close since many of the functions that call this 116 one do not check the return code. They do not allow for the 117 possibility that an I/O error may have caused the client to be 118 deleted. */ 119 client->t_suicide = thread_add_event(zebrad.master, zserv_delayed_close, 120 client, 0); 121 return -1; 122 case BUFFER_EMPTY: 123 THREAD_OFF(client->t_write); 124 break; 125 case BUFFER_PENDING: 126 THREAD_WRITE_ON(zebrad.master, client->t_write, 127 zserv_flush_data, client, client->sock); 128 break; 129 } 130 return 0; 131} 132 133static void 134zserv_create_header (struct stream *s, uint16_t cmd) 135{ 136 /* length placeholder, caller can update */ 137 stream_putw (s, ZEBRA_HEADER_SIZE); 138 stream_putc (s, ZEBRA_HEADER_MARKER); 139 stream_putc (s, ZSERV_VERSION); 140 stream_putw (s, cmd); 141} 142 143static void 144zserv_encode_interface (struct stream *s, struct interface *ifp) 145{ 146 /* Interface information. */ 147 stream_put (s, ifp->name, INTERFACE_NAMSIZ); 148 stream_putl (s, ifp->ifindex); 149 stream_putc (s, ifp->status); 150 stream_putq (s, ifp->flags); 151 stream_putl (s, ifp->metric); 152 stream_putl (s, ifp->mtu); 153 stream_putl (s, ifp->mtu6); 154 stream_putl (s, ifp->bandwidth); 155#ifdef HAVE_STRUCT_SOCKADDR_DL 156 stream_put (s, &ifp->sdl, sizeof (ifp->sdl_storage)); 157#else 158 stream_putl (s, ifp->hw_addr_len); 159 if (ifp->hw_addr_len) 160 stream_put (s, ifp->hw_addr, ifp->hw_addr_len); 161#endif /* HAVE_STRUCT_SOCKADDR_DL */ 162 163 /* Write packet size. */ 164 stream_putw_at (s, 0, stream_get_endp (s)); 165} 166 167/* Interface is added. Send ZEBRA_INTERFACE_ADD to client. */ 168/* 169 * This function is called in the following situations: 170 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request 171 * from the client. 172 * - at startup, when zebra figures out the available interfaces 173 * - when an interface is added (where support for 174 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when 175 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is 176 * received) 177 */ 178int 179zsend_interface_add (struct zserv *client, struct interface *ifp) 180{ 181 struct stream *s; 182 183 /* Check this client need interface information. */ 184 if (! client->ifinfo) 185 return 0; 186 187 s = client->obuf; 188 stream_reset (s); 189 190 zserv_create_header (s, ZEBRA_INTERFACE_ADD); 191 zserv_encode_interface (s, ifp); 192 193 return zebra_server_send_message(client); 194} 195 196/* Interface deletion from zebra daemon. */ 197int 198zsend_interface_delete (struct zserv *client, struct interface *ifp) 199{ 200 struct stream *s; 201 202 /* Check this client need interface information. */ 203 if (! client->ifinfo) 204 return 0; 205 206 s = client->obuf; 207 stream_reset (s); 208 209 zserv_create_header (s, ZEBRA_INTERFACE_DELETE); 210 zserv_encode_interface (s, ifp); 211 212 return zebra_server_send_message (client); 213} 214 215/* Interface address is added/deleted. Send ZEBRA_INTERFACE_ADDRESS_ADD or 216 * ZEBRA_INTERFACE_ADDRESS_DELETE to the client. 217 * 218 * A ZEBRA_INTERFACE_ADDRESS_ADD is sent in the following situations: 219 * - in response to a 3-byte ZEBRA_INTERFACE_ADD request 220 * from the client, after the ZEBRA_INTERFACE_ADD has been 221 * sent from zebra to the client 222 * - redistribute new address info to all clients in the following situations 223 * - at startup, when zebra figures out the available interfaces 224 * - when an interface is added (where support for 225 * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when 226 * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is 227 * received) 228 * - for the vty commands "ip address A.B.C.D/M [<secondary>|<label LINE>]" 229 * and "no bandwidth <1-10000000>", "ipv6 address X:X::X:X/M" 230 * - when an RTM_NEWADDR message is received from the kernel, 231 * 232 * The call tree that triggers ZEBRA_INTERFACE_ADDRESS_DELETE: 233 * 234 * zsend_interface_address(DELETE) 235 * ^ 236 * | 237 * zebra_interface_address_delete_update 238 * ^ ^ ^ 239 * | | if_delete_update 240 * | | 241 * ip_address_uninstall connected_delete_ipv4 242 * [ipv6_addresss_uninstall] [connected_delete_ipv6] 243 * ^ ^ 244 * | | 245 * | RTM_NEWADDR on routing/netlink socket 246 * | 247 * vty commands: 248 * "no ip address A.B.C.D/M [label LINE]" 249 * "no ip address A.B.C.D/M secondary" 250 * ["no ipv6 address X:X::X:X/M"] 251 * 252 */ 253int 254zsend_interface_address (int cmd, struct zserv *client, 255 struct interface *ifp, struct connected *ifc) 256{ 257 int blen; 258 struct stream *s; 259 struct prefix *p; 260 261 /* Check this client need interface information. */ 262 if (! client->ifinfo) 263 return 0; 264 265 s = client->obuf; 266 stream_reset (s); 267 268 zserv_create_header (s, cmd); 269 stream_putl (s, ifp->ifindex); 270 271 /* Interface address flag. */ 272 stream_putc (s, ifc->flags); 273 274 /* Prefix information. */ 275 p = ifc->address; 276 stream_putc (s, p->family); 277 blen = prefix_blen (p); 278 stream_put (s, &p->u.prefix, blen); 279 280 /* 281 * XXX gnu version does not send prefixlen for ZEBRA_INTERFACE_ADDRESS_DELETE 282 * but zebra_interface_address_delete_read() in the gnu version 283 * expects to find it 284 */ 285 stream_putc (s, p->prefixlen); 286 287 /* Destination. */ 288 p = ifc->destination; 289 if (p) 290 stream_put (s, &p->u.prefix, blen); 291 else 292 stream_put (s, NULL, blen); 293 294 /* Write packet size. */ 295 stream_putw_at (s, 0, stream_get_endp (s)); 296 297 return zebra_server_send_message(client); 298} 299 300/* 301 * The cmd passed to zsend_interface_update may be ZEBRA_INTERFACE_UP or 302 * ZEBRA_INTERFACE_DOWN. 303 * 304 * The ZEBRA_INTERFACE_UP message is sent from the zebra server to 305 * the clients in one of 2 situations: 306 * - an if_up is detected e.g., as a result of an RTM_IFINFO message 307 * - a vty command modifying the bandwidth of an interface is received. 308 * The ZEBRA_INTERFACE_DOWN message is sent when an if_down is detected. 309 */ 310int 311zsend_interface_update (int cmd, struct zserv *client, struct interface *ifp) 312{ 313 struct stream *s; 314 315 /* Check this client need interface information. */ 316 if (! client->ifinfo) 317 return 0; 318 319 s = client->obuf; 320 stream_reset (s); 321 322 zserv_create_header (s, cmd); 323 zserv_encode_interface (s, ifp); 324 325 return zebra_server_send_message(client); 326} 327 328/* 329 * The zebra server sends the clients a ZEBRA_IPV4_ROUTE_ADD or a 330 * ZEBRA_IPV6_ROUTE_ADD via zsend_route_multipath in the following 331 * situations: 332 * - when the client starts up, and requests default information 333 * by sending a ZEBRA_REDISTRIBUTE_DEFAULT_ADD to the zebra server, in the 334 * - case of rip, ripngd, ospfd and ospf6d, when the client sends a 335 * ZEBRA_REDISTRIBUTE_ADD as a result of the "redistribute" vty cmd, 336 * - when the zebra server redistributes routes after it updates its rib 337 * 338 * The zebra server sends clients a ZEBRA_IPV4_ROUTE_DELETE or a 339 * ZEBRA_IPV6_ROUTE_DELETE via zsend_route_multipath when: 340 * - a "ip route" or "ipv6 route" vty command is issued, a prefix is 341 * - deleted from zebra's rib, and this info 342 * has to be redistributed to the clients 343 * 344 * XXX The ZEBRA_IPV*_ROUTE_ADD message is also sent by the client to the 345 * zebra server when the client wants to tell the zebra server to add a 346 * route to the kernel (zapi_ipv4_add etc. ). Since it's essentially the 347 * same message being sent back and forth, this function and 348 * zapi_ipv{4,6}_{add, delete} should be re-written to avoid code 349 * duplication. 350 */ 351int 352zsend_route_multipath (int cmd, struct zserv *client, struct prefix *p, 353 struct rib *rib) 354{ 355 int psize; 356 struct stream *s; 357 struct nexthop *nexthop; 358 unsigned long nhnummark = 0, messmark = 0; 359 int nhnum = 0; 360 u_char zapi_flags = 0; 361 362 s = client->obuf; 363 stream_reset (s); 364 365 zserv_create_header (s, cmd); 366 367 /* Put type and nexthop. */ 368 stream_putc (s, rib->type); 369 stream_putc (s, rib->flags); 370 371 /* marker for message flags field */ 372 messmark = stream_get_endp (s); 373 stream_putc (s, 0); 374 375 /* Prefix. */ 376 psize = PSIZE (p->prefixlen); 377 stream_putc (s, p->prefixlen); 378 stream_write (s, (u_char *) & p->u.prefix, psize); 379 380 /* 381 * XXX The message format sent by zebra below does not match the format 382 * of the corresponding message expected by the zebra server 383 * itself (e.g., see zread_ipv4_add). The nexthop_num is not set correctly, 384 * (is there a bug on the client side if more than one segment is sent?) 385 * nexthop ZEBRA_NEXTHOP_IPV4 is never set, ZEBRA_NEXTHOP_IFINDEX 386 * is hard-coded. 387 */ 388 /* Nexthop */ 389 390 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next) 391 { 392 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB) 393 || nexthop_has_fib_child(nexthop)) 394 { 395 SET_FLAG (zapi_flags, ZAPI_MESSAGE_NEXTHOP); 396 SET_FLAG (zapi_flags, ZAPI_MESSAGE_IFINDEX); 397 398 if (nhnummark == 0) 399 { 400 nhnummark = stream_get_endp (s); 401 stream_putc (s, 1); /* placeholder */ 402 } 403 404 nhnum++; 405 406 switch(nexthop->type) 407 { 408 case NEXTHOP_TYPE_IPV4: 409 case NEXTHOP_TYPE_IPV4_IFINDEX: 410 stream_put_in_addr (s, &nexthop->gate.ipv4); 411 break; 412#ifdef HAVE_IPV6 413 case NEXTHOP_TYPE_IPV6: 414 case NEXTHOP_TYPE_IPV6_IFINDEX: 415 case NEXTHOP_TYPE_IPV6_IFNAME: 416 stream_write (s, (u_char *) &nexthop->gate.ipv6, 16); 417 break; 418#endif 419 default: 420 if (cmd == ZEBRA_IPV4_ROUTE_ADD 421 || cmd == ZEBRA_IPV4_ROUTE_DELETE) 422 { 423 struct in_addr empty; 424 memset (&empty, 0, sizeof (struct in_addr)); 425 stream_write (s, (u_char *) &empty, IPV4_MAX_BYTELEN); 426 } 427 else 428 { 429 struct in6_addr empty; 430 memset (&empty, 0, sizeof (struct in6_addr)); 431 stream_write (s, (u_char *) &empty, IPV6_MAX_BYTELEN); 432 } 433 } 434 435 /* Interface index. */ 436 stream_putc (s, 1); 437 stream_putl (s, nexthop->ifindex); 438 439 break; 440 } 441 } 442 443 /* Metric */ 444 if (cmd == ZEBRA_IPV4_ROUTE_ADD || cmd == ZEBRA_IPV6_ROUTE_ADD) 445 { 446 SET_FLAG (zapi_flags, ZAPI_MESSAGE_DISTANCE); 447 stream_putc (s, rib->distance); 448 SET_FLAG (zapi_flags, ZAPI_MESSAGE_METRIC); 449 stream_putl (s, rib->metric); 450 } 451 452 /* write real message flags value */ 453 stream_putc_at (s, messmark, zapi_flags); 454 455 /* Write next-hop number */ 456 if (nhnummark) 457 stream_putc_at (s, nhnummark, nhnum); 458 459 /* Write packet size. */ 460 stream_putw_at (s, 0, stream_get_endp (s)); 461 462 return zebra_server_send_message(client); 463} 464 465#ifdef HAVE_IPV6 466static int 467zsend_ipv6_nexthop_lookup (struct zserv *client, struct in6_addr *addr) 468{ 469 struct stream *s; 470 struct rib *rib; 471 unsigned long nump; 472 u_char num; 473 struct nexthop *nexthop; 474 475 /* Lookup nexthop. */ 476 rib = rib_match_ipv6 (addr); 477 478 /* Get output stream. */ 479 s = client->obuf; 480 stream_reset (s); 481 482 /* Fill in result. */ 483 zserv_create_header (s, ZEBRA_IPV6_NEXTHOP_LOOKUP); 484 stream_put (s, &addr, 16); 485 486 if (rib) 487 { 488 stream_putl (s, rib->metric); 489 num = 0; 490 nump = stream_get_endp(s); 491 stream_putc (s, 0); 492 /* Only non-recursive routes are elegible to resolve nexthop we 493 * are looking up. Therefore, we will just iterate over the top 494 * chain of nexthops. */ 495 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next) 496 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB)) 497 { 498 stream_putc (s, nexthop->type); 499 switch (nexthop->type) 500 { 501 case ZEBRA_NEXTHOP_IPV6: 502 stream_put (s, &nexthop->gate.ipv6, 16); 503 break; 504 case ZEBRA_NEXTHOP_IPV6_IFINDEX: 505 case ZEBRA_NEXTHOP_IPV6_IFNAME: 506 stream_put (s, &nexthop->gate.ipv6, 16); 507 stream_putl (s, nexthop->ifindex); 508 break; 509 case ZEBRA_NEXTHOP_IFINDEX: 510 case ZEBRA_NEXTHOP_IFNAME: 511 stream_putl (s, nexthop->ifindex); 512 break; 513 default: 514 /* do nothing */ 515 break; 516 } 517 num++; 518 } 519 stream_putc_at (s, nump, num); 520 } 521 else 522 { 523 stream_putl (s, 0); 524 stream_putc (s, 0); 525 } 526 527 stream_putw_at (s, 0, stream_get_endp (s)); 528 529 return zebra_server_send_message(client); 530} 531#endif /* HAVE_IPV6 */ 532 533static int 534zsend_ipv4_nexthop_lookup (struct zserv *client, struct in_addr addr) 535{ 536 struct stream *s; 537 struct rib *rib; 538 unsigned long nump; 539 u_char num; 540 struct nexthop *nexthop; 541 542 /* Lookup nexthop - eBGP excluded */ 543 rib = rib_match_ipv4_safi (addr, SAFI_UNICAST, 1, NULL); 544 545 /* Get output stream. */ 546 s = client->obuf; 547 stream_reset (s); 548 549 /* Fill in result. */ 550 zserv_create_header (s, ZEBRA_IPV4_NEXTHOP_LOOKUP); 551 stream_put_in_addr (s, &addr); 552 553 if (rib) 554 { 555 if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV) 556 zlog_debug("%s: Matching rib entry found.", __func__); 557 stream_putl (s, rib->metric); 558 num = 0; 559 nump = stream_get_endp(s); 560 stream_putc (s, 0); 561 /* Only non-recursive routes are elegible to resolve the nexthop we 562 * are looking up. Therefore, we will just iterate over the top 563 * chain of nexthops. */ 564 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next) 565 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB)) 566 { 567 stream_putc (s, nexthop->type); 568 switch (nexthop->type) 569 { 570 case ZEBRA_NEXTHOP_IPV4: 571 stream_put_in_addr (s, &nexthop->gate.ipv4); 572 break; 573 case ZEBRA_NEXTHOP_IPV4_IFINDEX: 574 stream_put_in_addr (s, &nexthop->gate.ipv4); 575 stream_putl (s, nexthop->ifindex); 576 break; 577 case ZEBRA_NEXTHOP_IFINDEX: 578 case ZEBRA_NEXTHOP_IFNAME: 579 stream_putl (s, nexthop->ifindex); 580 break; 581 default: 582 /* do nothing */ 583 break; 584 } 585 num++; 586 } 587 stream_putc_at (s, nump, num); 588 } 589 else 590 { 591 if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV) 592 zlog_debug("%s: No matching rib entry found.", __func__); 593 stream_putl (s, 0); 594 stream_putc (s, 0); 595 } 596 597 stream_putw_at (s, 0, stream_get_endp (s)); 598 599 return zebra_server_send_message(client); 600} 601 602/* 603 Modified version of zsend_ipv4_nexthop_lookup(): 604 Query unicast rib if nexthop is not found on mrib. 605 Returns both route metric and protocol distance. 606*/ 607static int 608zsend_ipv4_nexthop_lookup_mrib (struct zserv *client, struct in_addr addr, 609 struct rib *rib) 610{ 611 struct stream *s; 612 unsigned long nump; 613 u_char num; 614 struct nexthop *nexthop; 615 616 /* Get output stream. */ 617 s = client->obuf; 618 stream_reset (s); 619 620 /* Fill in result. */ 621 zserv_create_header (s, ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB); 622 stream_put_in_addr (s, &addr); 623 624 if (rib) 625 { 626 stream_putc (s, rib->distance); 627 stream_putl (s, rib->metric); 628 num = 0; 629 nump = stream_get_endp(s); /* remember position for nexthop_num */ 630 stream_putc (s, 0); /* reserve room for nexthop_num */ 631 /* Only non-recursive routes are elegible to resolve the nexthop we 632 * are looking up. Therefore, we will just iterate over the top 633 * chain of nexthops. */ 634 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next) 635 if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB)) 636 { 637 stream_putc (s, nexthop->type); 638 switch (nexthop->type) 639 { 640 case ZEBRA_NEXTHOP_IPV4: 641 stream_put_in_addr (s, &nexthop->gate.ipv4); 642 break; 643 case ZEBRA_NEXTHOP_IPV4_IFINDEX: 644 stream_put_in_addr (s, &nexthop->gate.ipv4); 645 stream_putl (s, nexthop->ifindex); 646 break; 647 case ZEBRA_NEXTHOP_IFINDEX: 648 case ZEBRA_NEXTHOP_IFNAME: 649 stream_putl (s, nexthop->ifindex); 650 break; 651 default: 652 /* do nothing */ 653 break; 654 } 655 num++; 656 } 657 658 stream_putc_at (s, nump, num); /* store nexthop_num */ 659 } 660 else 661 { 662 stream_putc (s, 0); /* distance */ 663 stream_putl (s, 0); /* metric */ 664 stream_putc (s, 0); /* nexthop_num */ 665 } 666 667 stream_putw_at (s, 0, stream_get_endp (s)); 668 669 return zebra_server_send_message(client); 670} 671 672static int 673zsend_ipv4_import_lookup (struct zserv *client, struct prefix_ipv4 *p) 674{ 675 struct stream *s; 676 struct rib *rib; 677 unsigned long nump; 678 u_char num; 679 struct nexthop *nexthop; 680 681 /* Lookup nexthop. */ 682 rib = rib_lookup_ipv4 (p); 683 684 /* Get output stream. */ 685 s = client->obuf; 686 stream_reset (s); 687 688 /* Fill in result. */ 689 zserv_create_header (s, ZEBRA_IPV4_IMPORT_LOOKUP); 690 stream_put_in_addr (s, &p->prefix); 691 692 if (rib) 693 { 694 stream_putl (s, rib->metric); 695 num = 0; 696 nump = stream_get_endp(s); 697 stream_putc (s, 0); 698 for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next) 699 if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_FIB) 700 || nexthop_has_fib_child(nexthop)) 701 { 702 stream_putc (s, nexthop->type); 703 switch (nexthop->type) 704 { 705 case ZEBRA_NEXTHOP_IPV4: 706 stream_put_in_addr (s, &nexthop->gate.ipv4); 707 break; 708 case ZEBRA_NEXTHOP_IPV4_IFINDEX: 709 stream_put_in_addr (s, &nexthop->gate.ipv4); 710 stream_putl (s, nexthop->ifindex); 711 break; 712 case ZEBRA_NEXTHOP_IFINDEX: 713 case ZEBRA_NEXTHOP_IFNAME: 714 stream_putl (s, nexthop->ifindex); 715 break; 716 default: 717 /* do nothing */ 718 break; 719 } 720 num++; 721 } 722 stream_putc_at (s, nump, num); 723 } 724 else 725 { 726 stream_putl (s, 0); 727 stream_putc (s, 0); 728 } 729 730 stream_putw_at (s, 0, stream_get_endp (s)); 731 732 return zebra_server_send_message(client); 733} 734 735/* Router-id is updated. Send ZEBRA_ROUTER_ID_ADD to client. */ 736int 737zsend_router_id_update (struct zserv *client, struct prefix *p) 738{ 739 struct stream *s; 740 int blen; 741 742 /* Check this client need interface information. */ 743 if (!client->ridinfo) 744 return 0; 745 746 s = client->obuf; 747 stream_reset (s); 748 749 /* Message type. */ 750 zserv_create_header (s, ZEBRA_ROUTER_ID_UPDATE); 751 752 /* Prefix information. */ 753 stream_putc (s, p->family); 754 blen = prefix_blen (p); 755 stream_put (s, &p->u.prefix, blen); 756 stream_putc (s, p->prefixlen); 757 758 /* Write packet size. */ 759 stream_putw_at (s, 0, stream_get_endp (s)); 760 761 return zebra_server_send_message(client); 762} 763 764/* Register zebra server interface information. Send current all 765 interface and address information. */ 766static int 767zread_interface_add (struct zserv *client, u_short length) 768{ 769 struct listnode *ifnode, *ifnnode; 770 struct listnode *cnode, *cnnode; 771 struct interface *ifp; 772 struct connected *c; 773 774 /* Interface information is needed. */ 775 client->ifinfo = 1; 776 777 for (ALL_LIST_ELEMENTS (iflist, ifnode, ifnnode, ifp)) 778 { 779 /* Skip pseudo interface. */ 780 if (! CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE)) 781 continue; 782 783 if (zsend_interface_add (client, ifp) < 0) 784 return -1; 785 786 for (ALL_LIST_ELEMENTS (ifp->connected, cnode, cnnode, c)) 787 { 788 if (CHECK_FLAG (c->conf, ZEBRA_IFC_REAL) && 789 (zsend_interface_address (ZEBRA_INTERFACE_ADDRESS_ADD, client, 790 ifp, c) < 0)) 791 return -1; 792 } 793 } 794 return 0; 795} 796 797/* Unregister zebra server interface information. */ 798static int 799zread_interface_delete (struct zserv *client, u_short length) 800{ 801 client->ifinfo = 0; 802 return 0; 803} 804 805/* This function support multiple nexthop. */ 806/* 807 * Parse the ZEBRA_IPV4_ROUTE_ADD sent from client. Update rib and 808 * add kernel route. 809 */ 810static int 811zread_ipv4_add (struct zserv *client, u_short length) 812{ 813 int i; 814 struct rib *rib; 815 struct prefix_ipv4 p; 816 u_char message; 817 struct in_addr nexthop; 818 u_char nexthop_num; 819 u_char nexthop_type; 820 struct stream *s; 821 unsigned int ifindex; 822 u_char ifname_len; 823 safi_t safi; 824 825 826 /* Get input stream. */ 827 s = client->ibuf; 828 829 /* Allocate new rib. */ 830 rib = XCALLOC (MTYPE_RIB, sizeof (struct rib)); 831 832 /* Type, flags, message. */ 833 rib->type = stream_getc (s); 834 rib->flags = stream_getc (s); 835 message = stream_getc (s); 836 safi = stream_getw (s); 837 rib->uptime = time (NULL); 838 839 /* IPv4 prefix. */ 840 memset (&p, 0, sizeof (struct prefix_ipv4)); 841 p.family = AF_INET; 842 p.prefixlen = stream_getc (s); 843 stream_get (&p.prefix, s, PSIZE (p.prefixlen)); 844 845 /* Nexthop parse. */ 846 if (CHECK_FLAG (message, ZAPI_MESSAGE_NEXTHOP)) 847 { 848 nexthop_num = stream_getc (s); 849 850 for (i = 0; i < nexthop_num; i++) 851 { 852 nexthop_type = stream_getc (s); 853 854 switch (nexthop_type) 855 { 856 case ZEBRA_NEXTHOP_IFINDEX: 857 ifindex = stream_getl (s); 858 nexthop_ifindex_add (rib, ifindex); 859 break; 860 case ZEBRA_NEXTHOP_IFNAME: 861 ifname_len = stream_getc (s); 862 stream_forward_getp (s, ifname_len); 863 break; 864 case ZEBRA_NEXTHOP_IPV4: 865 nexthop.s_addr = stream_get_ipv4 (s); 866 nexthop_ipv4_add (rib, &nexthop, NULL); 867 break; 868 case ZEBRA_NEXTHOP_IPV4_IFINDEX: 869 nexthop.s_addr = stream_get_ipv4 (s); 870 ifindex = stream_getl (s); 871 nexthop_ipv4_ifindex_add (rib, &nexthop, NULL, ifindex); 872 break; 873 case ZEBRA_NEXTHOP_IPV6: 874 stream_forward_getp (s, IPV6_MAX_BYTELEN); 875 break; 876 case ZEBRA_NEXTHOP_BLACKHOLE: 877 nexthop_blackhole_add (rib); 878 break; 879 } 880 } 881 } 882 883 /* Distance. */ 884 if (CHECK_FLAG (message, ZAPI_MESSAGE_DISTANCE)) 885 rib->distance = stream_getc (s); 886 887 /* Metric. */ 888 if (CHECK_FLAG (message, ZAPI_MESSAGE_METRIC)) 889 rib->metric = stream_getl (s); 890 891 /* Table */ 892 rib->table=zebrad.rtm_table_default; 893 rib_add_ipv4_multipath (&p, rib, safi); 894 return 0; 895} 896 897/* Zebra server IPv4 prefix delete function. */ 898static int 899zread_ipv4_delete (struct zserv *client, u_short length) 900{ 901 int i; 902 struct stream *s; 903 struct zapi_ipv4 api; 904 struct in_addr nexthop, *nexthop_p; 905 unsigned long ifindex; 906 struct prefix_ipv4 p; 907 u_char nexthop_num; 908 u_char nexthop_type; 909 u_char ifname_len; 910 911 s = client->ibuf; 912 ifindex = 0; 913 nexthop.s_addr = 0; 914 nexthop_p = NULL; 915 916 /* Type, flags, message. */ 917 api.type = stream_getc (s); 918 api.flags = stream_getc (s); 919 api.message = stream_getc (s); 920 api.safi = stream_getw (s); 921 922 /* IPv4 prefix. */ 923 memset (&p, 0, sizeof (struct prefix_ipv4)); 924 p.family = AF_INET; 925 p.prefixlen = stream_getc (s); 926 stream_get (&p.prefix, s, PSIZE (p.prefixlen)); 927 928 /* Nexthop, ifindex, distance, metric. */ 929 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP)) 930 { 931 nexthop_num = stream_getc (s); 932 933 for (i = 0; i < nexthop_num; i++) 934 { 935 nexthop_type = stream_getc (s); 936 937 switch (nexthop_type) 938 { 939 case ZEBRA_NEXTHOP_IFINDEX: 940 ifindex = stream_getl (s); 941 break; 942 case ZEBRA_NEXTHOP_IFNAME: 943 ifname_len = stream_getc (s); 944 stream_forward_getp (s, ifname_len); 945 break; 946 case ZEBRA_NEXTHOP_IPV4: 947 nexthop.s_addr = stream_get_ipv4 (s); 948 nexthop_p = &nexthop; 949 break; 950 case ZEBRA_NEXTHOP_IPV4_IFINDEX: 951 nexthop.s_addr = stream_get_ipv4 (s); 952 nexthop_p = &nexthop; 953 ifindex = stream_getl (s); 954 break; 955 case ZEBRA_NEXTHOP_IPV6: 956 stream_forward_getp (s, IPV6_MAX_BYTELEN); 957 break; 958 } 959 } 960 } 961 962 /* Distance. */ 963 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE)) 964 api.distance = stream_getc (s); 965 else 966 api.distance = 0; 967 968 /* Metric. */ 969 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC)) 970 api.metric = stream_getl (s); 971 else 972 api.metric = 0; 973 974 rib_delete_ipv4 (api.type, api.flags, &p, nexthop_p, ifindex, 975 client->rtm_table, api.safi); 976 return 0; 977} 978 979/* Nexthop lookup for IPv4. */ 980static int 981zread_ipv4_nexthop_lookup (struct zserv *client, u_short length) 982{ 983 struct in_addr addr; 984 char buf[BUFSIZ]; 985 986 addr.s_addr = stream_get_ipv4 (client->ibuf); 987 if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV) 988 zlog_debug("%s: looking up %s", __func__, 989 inet_ntop (AF_INET, &addr, buf, BUFSIZ)); 990 return zsend_ipv4_nexthop_lookup (client, addr); 991} 992 993/* MRIB Nexthop lookup for IPv4. */ 994static int 995zread_ipv4_nexthop_lookup_mrib (struct zserv *client, u_short length) 996{ 997 struct in_addr addr; 998 struct rib *rib; 999 1000 addr.s_addr = stream_get_ipv4 (client->ibuf); 1001 rib = rib_match_ipv4_multicast (addr, NULL); 1002 return zsend_ipv4_nexthop_lookup_mrib (client, addr, rib); 1003} 1004 1005/* Nexthop lookup for IPv4. */ 1006static int 1007zread_ipv4_import_lookup (struct zserv *client, u_short length) 1008{ 1009 struct prefix_ipv4 p; 1010 1011 p.family = AF_INET; 1012 p.prefixlen = stream_getc (client->ibuf); 1013 p.prefix.s_addr = stream_get_ipv4 (client->ibuf); 1014 1015 return zsend_ipv4_import_lookup (client, &p); 1016} 1017 1018#ifdef HAVE_IPV6 1019/* Zebra server IPv6 prefix add function. */ 1020static int 1021zread_ipv6_add (struct zserv *client, u_short length) 1022{ 1023 int i; 1024 struct stream *s; 1025 struct zapi_ipv6 api; 1026 struct in6_addr nexthop; 1027 unsigned long ifindex; 1028 struct prefix_ipv6 p; 1029 1030 s = client->ibuf; 1031 ifindex = 0; 1032 memset (&nexthop, 0, sizeof (struct in6_addr)); 1033 1034 /* Type, flags, message. */ 1035 api.type = stream_getc (s); 1036 api.flags = stream_getc (s); 1037 api.message = stream_getc (s); 1038 api.safi = stream_getw (s); 1039 1040 /* IPv4 prefix. */ 1041 memset (&p, 0, sizeof (struct prefix_ipv6)); 1042 p.family = AF_INET6; 1043 p.prefixlen = stream_getc (s); 1044 stream_get (&p.prefix, s, PSIZE (p.prefixlen)); 1045 1046 /* Nexthop, ifindex, distance, metric. */ 1047 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP)) 1048 { 1049 u_char nexthop_type; 1050 1051 api.nexthop_num = stream_getc (s); 1052 for (i = 0; i < api.nexthop_num; i++) 1053 { 1054 nexthop_type = stream_getc (s); 1055 1056 switch (nexthop_type) 1057 { 1058 case ZEBRA_NEXTHOP_IPV6: 1059 stream_get (&nexthop, s, 16); 1060 break; 1061 case ZEBRA_NEXTHOP_IFINDEX: 1062 ifindex = stream_getl (s); 1063 break; 1064 } 1065 } 1066 } 1067 1068 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE)) 1069 api.distance = stream_getc (s); 1070 else 1071 api.distance = 0; 1072 1073 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC)) 1074 api.metric = stream_getl (s); 1075 else 1076 api.metric = 0; 1077 1078 if (IN6_IS_ADDR_UNSPECIFIED (&nexthop)) 1079 rib_add_ipv6 (api.type, api.flags, &p, NULL, ifindex, zebrad.rtm_table_default, api.metric, 1080 api.distance, api.safi); 1081 else 1082 rib_add_ipv6 (api.type, api.flags, &p, &nexthop, ifindex, zebrad.rtm_table_default, api.metric, 1083 api.distance, api.safi); 1084 return 0; 1085} 1086 1087/* Zebra server IPv6 prefix delete function. */ 1088static int 1089zread_ipv6_delete (struct zserv *client, u_short length) 1090{ 1091 int i; 1092 struct stream *s; 1093 struct zapi_ipv6 api; 1094 struct in6_addr nexthop; 1095 unsigned long ifindex; 1096 struct prefix_ipv6 p; 1097 1098 s = client->ibuf; 1099 ifindex = 0; 1100 memset (&nexthop, 0, sizeof (struct in6_addr)); 1101 1102 /* Type, flags, message. */ 1103 api.type = stream_getc (s); 1104 api.flags = stream_getc (s); 1105 api.message = stream_getc (s); 1106 api.safi = stream_getw (s); 1107 1108 /* IPv4 prefix. */ 1109 memset (&p, 0, sizeof (struct prefix_ipv6)); 1110 p.family = AF_INET6; 1111 p.prefixlen = stream_getc (s); 1112 stream_get (&p.prefix, s, PSIZE (p.prefixlen)); 1113 1114 /* Nexthop, ifindex, distance, metric. */ 1115 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP)) 1116 { 1117 u_char nexthop_type; 1118 1119 api.nexthop_num = stream_getc (s); 1120 for (i = 0; i < api.nexthop_num; i++) 1121 { 1122 nexthop_type = stream_getc (s); 1123 1124 switch (nexthop_type) 1125 { 1126 case ZEBRA_NEXTHOP_IPV6: 1127 stream_get (&nexthop, s, 16); 1128 break; 1129 case ZEBRA_NEXTHOP_IFINDEX: 1130 ifindex = stream_getl (s); 1131 break; 1132 } 1133 } 1134 } 1135 1136 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE)) 1137 api.distance = stream_getc (s); 1138 else 1139 api.distance = 0; 1140 if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC)) 1141 api.metric = stream_getl (s); 1142 else 1143 api.metric = 0; 1144 1145 if (IN6_IS_ADDR_UNSPECIFIED (&nexthop)) 1146 rib_delete_ipv6 (api.type, api.flags, &p, NULL, ifindex, client->rtm_table, api.safi); 1147 else 1148 rib_delete_ipv6 (api.type, api.flags, &p, &nexthop, ifindex, client->rtm_table, api.safi); 1149 return 0; 1150} 1151 1152static int 1153zread_ipv6_nexthop_lookup (struct zserv *client, u_short length) 1154{ 1155 struct in6_addr addr; 1156 char buf[BUFSIZ]; 1157 1158 stream_get (&addr, client->ibuf, 16); 1159 if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV) 1160 zlog_debug("%s: looking up %s", __func__, 1161 inet_ntop (AF_INET6, &addr, buf, BUFSIZ)); 1162 1163 return zsend_ipv6_nexthop_lookup (client, &addr); 1164} 1165#endif /* HAVE_IPV6 */ 1166 1167/* Register zebra server router-id information. Send current router-id */ 1168static int 1169zread_router_id_add (struct zserv *client, u_short length) 1170{ 1171 struct prefix p; 1172 1173 /* Router-id information is needed. */ 1174 client->ridinfo = 1; 1175 1176 router_id_get (&p); 1177 1178 return zsend_router_id_update (client,&p); 1179} 1180 1181/* Unregister zebra server router-id information. */ 1182static int 1183zread_router_id_delete (struct zserv *client, u_short length) 1184{ 1185 client->ridinfo = 0; 1186 return 0; 1187} 1188 1189/* Tie up route-type and client->sock */ 1190static void 1191zread_hello (struct zserv *client) 1192{ 1193 /* type of protocol (lib/zebra.h) */ 1194 u_char proto; 1195 proto = stream_getc (client->ibuf); 1196 1197 /* accept only dynamic routing protocols */ 1198 if ((proto < ZEBRA_ROUTE_MAX) 1199 && (proto > ZEBRA_ROUTE_STATIC)) 1200 { 1201 zlog_notice ("client %d says hello and bids fair to announce only %s routes", 1202 client->sock, zebra_route_string(proto)); 1203 1204 /* if route-type was binded by other client */ 1205 if (route_type_oaths[proto]) 1206 zlog_warn ("sender of %s routes changed %c->%c", 1207 zebra_route_string(proto), route_type_oaths[proto], 1208 client->sock); 1209 1210 route_type_oaths[proto] = client->sock; 1211 } 1212} 1213 1214/* If client sent routes of specific type, zebra removes it 1215 * and returns number of deleted routes. 1216 */ 1217static void 1218zebra_score_rib (int client_sock) 1219{ 1220 int i; 1221 1222 for (i = ZEBRA_ROUTE_RIP; i < ZEBRA_ROUTE_MAX; i++) 1223 if (client_sock == route_type_oaths[i]) 1224 { 1225 zlog_notice ("client %d disconnected. %lu %s routes removed from the rib", 1226 client_sock, rib_score_proto (i), zebra_route_string (i)); 1227 route_type_oaths[i] = 0; 1228 break; 1229 } 1230} 1231 1232/* Close zebra client. */ 1233static void 1234zebra_client_close (struct zserv *client) 1235{ 1236 /* Close file descriptor. */ 1237 if (client->sock) 1238 { 1239 close (client->sock); 1240 zebra_score_rib (client->sock); 1241 client->sock = -1; 1242 } 1243 1244 /* Free stream buffers. */ 1245 if (client->ibuf) 1246 stream_free (client->ibuf); 1247 if (client->obuf) 1248 stream_free (client->obuf); 1249 if (client->wb) 1250 buffer_free(client->wb); 1251 1252 /* Release threads. */ 1253 if (client->t_read) 1254 thread_cancel (client->t_read); 1255 if (client->t_write) 1256 thread_cancel (client->t_write); 1257 if (client->t_suicide) 1258 thread_cancel (client->t_suicide); 1259 1260 /* Free client structure. */ 1261 listnode_delete (zebrad.client_list, client); 1262 XFREE (0, client); 1263} 1264 1265/* Make new client. */ 1266static void 1267zebra_client_create (int sock) 1268{ 1269 struct zserv *client; 1270 1271 client = XCALLOC (0, sizeof (struct zserv)); 1272 1273 /* Make client input/output buffer. */ 1274 client->sock = sock; 1275 client->ibuf = stream_new (ZEBRA_MAX_PACKET_SIZ); 1276 client->obuf = stream_new (ZEBRA_MAX_PACKET_SIZ); 1277 client->wb = buffer_new(0); 1278 1279 /* Set table number. */ 1280 client->rtm_table = zebrad.rtm_table_default; 1281 1282 /* Add this client to linked list. */ 1283 listnode_add (zebrad.client_list, client); 1284 1285 /* Make new read thread. */ 1286 zebra_event (ZEBRA_READ, sock, client); 1287} 1288 1289/* Handler of zebra service request. */ 1290static int 1291zebra_client_read (struct thread *thread) 1292{ 1293 int sock; 1294 struct zserv *client; 1295 size_t already; 1296 uint16_t length, command; 1297 uint8_t marker, version; 1298 1299 /* Get thread data. Reset reading thread because I'm running. */ 1300 sock = THREAD_FD (thread); 1301 client = THREAD_ARG (thread); 1302 client->t_read = NULL; 1303 1304 if (client->t_suicide) 1305 { 1306 zebra_client_close(client); 1307 return -1; 1308 } 1309 1310 /* Read length and command (if we don't have it already). */ 1311 if ((already = stream_get_endp(client->ibuf)) < ZEBRA_HEADER_SIZE) 1312 { 1313 ssize_t nbyte; 1314 if (((nbyte = stream_read_try (client->ibuf, sock, 1315 ZEBRA_HEADER_SIZE-already)) == 0) || 1316 (nbyte == -1)) 1317 { 1318 if (IS_ZEBRA_DEBUG_EVENT) 1319 zlog_debug ("connection closed socket [%d]", sock); 1320 zebra_client_close (client); 1321 return -1; 1322 } 1323 if (nbyte != (ssize_t)(ZEBRA_HEADER_SIZE-already)) 1324 { 1325 /* Try again later. */ 1326 zebra_event (ZEBRA_READ, sock, client); 1327 return 0; 1328 } 1329 already = ZEBRA_HEADER_SIZE; 1330 } 1331 1332 /* Reset to read from the beginning of the incoming packet. */ 1333 stream_set_getp(client->ibuf, 0); 1334 1335 /* Fetch header values */ 1336 length = stream_getw (client->ibuf); 1337 marker = stream_getc (client->ibuf); 1338 version = stream_getc (client->ibuf); 1339 command = stream_getw (client->ibuf); 1340 1341 if (marker != ZEBRA_HEADER_MARKER || version != ZSERV_VERSION) 1342 { 1343 zlog_err("%s: socket %d version mismatch, marker %d, version %d", 1344 __func__, sock, marker, version); 1345 zebra_client_close (client); 1346 return -1; 1347 } 1348 if (length < ZEBRA_HEADER_SIZE) 1349 { 1350 zlog_warn("%s: socket %d message length %u is less than header size %d", 1351 __func__, sock, length, ZEBRA_HEADER_SIZE); 1352 zebra_client_close (client); 1353 return -1; 1354 } 1355 if (length > STREAM_SIZE(client->ibuf)) 1356 { 1357 zlog_warn("%s: socket %d message length %u exceeds buffer size %lu", 1358 __func__, sock, length, (u_long)STREAM_SIZE(client->ibuf)); 1359 zebra_client_close (client); 1360 return -1; 1361 } 1362 1363 /* Read rest of data. */ 1364 if (already < length) 1365 { 1366 ssize_t nbyte; 1367 if (((nbyte = stream_read_try (client->ibuf, sock, 1368 length-already)) == 0) || 1369 (nbyte == -1)) 1370 { 1371 if (IS_ZEBRA_DEBUG_EVENT) 1372 zlog_debug ("connection closed [%d] when reading zebra data", sock); 1373 zebra_client_close (client); 1374 return -1; 1375 } 1376 if (nbyte != (ssize_t)(length-already)) 1377 { 1378 /* Try again later. */ 1379 zebra_event (ZEBRA_READ, sock, client); 1380 return 0; 1381 } 1382 } 1383 1384 length -= ZEBRA_HEADER_SIZE; 1385 1386 /* Debug packet information. */ 1387 if (IS_ZEBRA_DEBUG_EVENT) 1388 zlog_debug ("zebra message comes from socket [%d]", sock); 1389 1390 if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV) 1391 zlog_debug ("zebra message received [%s] %d", 1392 zserv_command_string (command), length); 1393 1394 switch (command) 1395 { 1396 case ZEBRA_ROUTER_ID_ADD: 1397 zread_router_id_add (client, length); 1398 break; 1399 case ZEBRA_ROUTER_ID_DELETE: 1400 zread_router_id_delete (client, length); 1401 break; 1402 case ZEBRA_INTERFACE_ADD: 1403 zread_interface_add (client, length); 1404 break; 1405 case ZEBRA_INTERFACE_DELETE: 1406 zread_interface_delete (client, length); 1407 break; 1408 case ZEBRA_IPV4_ROUTE_ADD: 1409 zread_ipv4_add (client, length); 1410 break; 1411 case ZEBRA_IPV4_ROUTE_DELETE: 1412 zread_ipv4_delete (client, length); 1413 break; 1414#ifdef HAVE_IPV6 1415 case ZEBRA_IPV6_ROUTE_ADD: 1416 zread_ipv6_add (client, length); 1417 break; 1418 case ZEBRA_IPV6_ROUTE_DELETE: 1419 zread_ipv6_delete (client, length); 1420 break; 1421#endif /* HAVE_IPV6 */ 1422 case ZEBRA_REDISTRIBUTE_ADD: 1423 zebra_redistribute_add (command, client, length); 1424 break; 1425 case ZEBRA_REDISTRIBUTE_DELETE: 1426 zebra_redistribute_delete (command, client, length); 1427 break; 1428 case ZEBRA_REDISTRIBUTE_DEFAULT_ADD: 1429 zebra_redistribute_default_add (command, client, length); 1430 break; 1431 case ZEBRA_REDISTRIBUTE_DEFAULT_DELETE: 1432 zebra_redistribute_default_delete (command, client, length); 1433 break; 1434 case ZEBRA_IPV4_NEXTHOP_LOOKUP: 1435 zread_ipv4_nexthop_lookup (client, length); 1436 break; 1437 case ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB: 1438 zread_ipv4_nexthop_lookup_mrib (client, length); 1439 break; 1440#ifdef HAVE_IPV6 1441 case ZEBRA_IPV6_NEXTHOP_LOOKUP: 1442 zread_ipv6_nexthop_lookup (client, length); 1443 break; 1444#endif /* HAVE_IPV6 */ 1445 case ZEBRA_IPV4_IMPORT_LOOKUP: 1446 zread_ipv4_import_lookup (client, length); 1447 break; 1448 case ZEBRA_HELLO: 1449 zread_hello (client); 1450 break; 1451 default: 1452 zlog_info ("Zebra received unknown command %d", command); 1453 break; 1454 } 1455 1456 if (client->t_suicide) 1457 { 1458 /* No need to wait for thread callback, just kill immediately. */ 1459 zebra_client_close(client); 1460 return -1; 1461 } 1462 1463 stream_reset (client->ibuf); 1464 zebra_event (ZEBRA_READ, sock, client); 1465 return 0; 1466} 1467 1468 1469/* Accept code of zebra server socket. */ 1470static int 1471zebra_accept (struct thread *thread) 1472{ 1473 int accept_sock; 1474 int client_sock; 1475 struct sockaddr_in client; 1476 socklen_t len; 1477 1478 accept_sock = THREAD_FD (thread); 1479 1480 /* Reregister myself. */ 1481 zebra_event (ZEBRA_SERV, accept_sock, NULL); 1482 1483 len = sizeof (struct sockaddr_in); 1484 client_sock = accept (accept_sock, (struct sockaddr *) &client, &len); 1485 1486 if (client_sock < 0) 1487 { 1488 zlog_warn ("Can't accept zebra socket: %s", safe_strerror (errno)); 1489 return -1; 1490 } 1491 1492 /* Make client socket non-blocking. */ 1493 set_nonblocking(client_sock); 1494 1495 /* Create new zebra client. */ 1496 zebra_client_create (client_sock); 1497 1498 return 0; 1499} 1500 1501#ifdef HAVE_TCP_ZEBRA 1502/* Make zebra's server socket. */ 1503static void 1504zebra_serv () 1505{ 1506 int ret; 1507 int accept_sock; 1508 struct sockaddr_in addr; 1509 1510 accept_sock = socket (AF_INET, SOCK_STREAM, 0); 1511 1512 if (accept_sock < 0) 1513 { 1514 zlog_warn ("Can't create zserv stream socket: %s", 1515 safe_strerror (errno)); 1516 zlog_warn ("zebra can't provice full functionality due to above error"); 1517 return; 1518 } 1519 1520 memset (&route_type_oaths, 0, sizeof (route_type_oaths)); 1521 memset (&addr, 0, sizeof (struct sockaddr_in)); 1522 addr.sin_family = AF_INET; 1523 addr.sin_port = htons (ZEBRA_PORT); 1524#ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN 1525 addr.sin_len = sizeof (struct sockaddr_in); 1526#endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */ 1527 addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK); 1528 1529 sockopt_reuseaddr (accept_sock); 1530 sockopt_reuseport (accept_sock); 1531 1532 if ( zserv_privs.change(ZPRIVS_RAISE) ) 1533 zlog (NULL, LOG_ERR, "Can't raise privileges"); 1534 1535 ret = bind (accept_sock, (struct sockaddr *)&addr, 1536 sizeof (struct sockaddr_in)); 1537 if (ret < 0) 1538 { 1539 zlog_warn ("Can't bind to stream socket: %s", 1540 safe_strerror (errno)); 1541 zlog_warn ("zebra can't provice full functionality due to above error"); 1542 close (accept_sock); /* Avoid sd leak. */ 1543 return; 1544 } 1545 1546 if ( zserv_privs.change(ZPRIVS_LOWER) ) 1547 zlog (NULL, LOG_ERR, "Can't lower privileges"); 1548 1549 ret = listen (accept_sock, 1); 1550 if (ret < 0) 1551 { 1552 zlog_warn ("Can't listen to stream socket: %s", 1553 safe_strerror (errno)); 1554 zlog_warn ("zebra can't provice full functionality due to above error"); 1555 close (accept_sock); /* Avoid sd leak. */ 1556 return; 1557 } 1558 1559 zebra_event (ZEBRA_SERV, accept_sock, NULL); 1560} 1561#endif /* HAVE_TCP_ZEBRA */ 1562 1563/* For sockaddr_un. */ 1564#include <sys/un.h> 1565 1566/* zebra server UNIX domain socket. */ 1567static void 1568zebra_serv_un (const char *path) 1569{ 1570 int ret; 1571 int sock, len; 1572 struct sockaddr_un serv; 1573 mode_t old_mask; 1574 1575 /* First of all, unlink existing socket */ 1576 unlink (path); 1577 1578 /* Set umask */ 1579 old_mask = umask (0077); 1580 1581 /* Make UNIX domain socket. */ 1582 sock = socket (AF_UNIX, SOCK_STREAM, 0); 1583 if (sock < 0) 1584 { 1585 zlog_warn ("Can't create zserv unix socket: %s", 1586 safe_strerror (errno)); 1587 zlog_warn ("zebra can't provide full functionality due to above error"); 1588 return; 1589 } 1590 1591 memset (&route_type_oaths, 0, sizeof (route_type_oaths)); 1592 1593 /* Make server socket. */ 1594 memset (&serv, 0, sizeof (struct sockaddr_un)); 1595 serv.sun_family = AF_UNIX; 1596 strncpy (serv.sun_path, path, strlen (path)); 1597#ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN 1598 len = serv.sun_len = SUN_LEN(&serv); 1599#else 1600 len = sizeof (serv.sun_family) + strlen (serv.sun_path); 1601#endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */ 1602 1603 ret = bind (sock, (struct sockaddr *) &serv, len); 1604 if (ret < 0) 1605 { 1606 zlog_warn ("Can't bind to unix socket %s: %s", 1607 path, safe_strerror (errno)); 1608 zlog_warn ("zebra can't provide full functionality due to above error"); 1609 close (sock); 1610 return; 1611 } 1612 1613 ret = listen (sock, 5); 1614 if (ret < 0) 1615 { 1616 zlog_warn ("Can't listen to unix socket %s: %s", 1617 path, safe_strerror (errno)); 1618 zlog_warn ("zebra can't provide full functionality due to above error"); 1619 close (sock); 1620 return; 1621 } 1622 1623 umask (old_mask); 1624 1625 zebra_event (ZEBRA_SERV, sock, NULL); 1626} 1627 1628 1629static void 1630zebra_event (enum event event, int sock, struct zserv *client) 1631{ 1632 switch (event) 1633 { 1634 case ZEBRA_SERV: 1635 thread_add_read (zebrad.master, zebra_accept, client, sock); 1636 break; 1637 case ZEBRA_READ: 1638 client->t_read = 1639 thread_add_read (zebrad.master, zebra_client_read, client, sock); 1640 break; 1641 case ZEBRA_WRITE: 1642 /**/ 1643 break; 1644 } 1645} 1646 1647/* Display default rtm_table for all clients. */ 1648DEFUN (show_table, 1649 show_table_cmd, 1650 "show table", 1651 SHOW_STR 1652 "default routing table to use for all clients\n") 1653{ 1654 vty_out (vty, "table %d%s", zebrad.rtm_table_default, 1655 VTY_NEWLINE); 1656 return CMD_SUCCESS; 1657} 1658 1659DEFUN (config_table, 1660 config_table_cmd, 1661 "table TABLENO", 1662 "Configure target kernel routing table\n" 1663 "TABLE integer\n") 1664{ 1665 zebrad.rtm_table_default = strtol (argv[0], (char**)0, 10); 1666 return CMD_SUCCESS; 1667} 1668 1669DEFUN (ip_forwarding, 1670 ip_forwarding_cmd, 1671 "ip forwarding", 1672 IP_STR 1673 "Turn on IP forwarding") 1674{ 1675 int ret; 1676 1677 ret = ipforward (); 1678 if (ret == 0) 1679 ret = ipforward_on (); 1680 1681 if (ret == 0) 1682 { 1683 vty_out (vty, "Can't turn on IP forwarding%s", VTY_NEWLINE); 1684 return CMD_WARNING; 1685 } 1686 1687 return CMD_SUCCESS; 1688} 1689 1690DEFUN (no_ip_forwarding, 1691 no_ip_forwarding_cmd, 1692 "no ip forwarding", 1693 NO_STR 1694 IP_STR 1695 "Turn off IP forwarding") 1696{ 1697 int ret; 1698 1699 ret = ipforward (); 1700 if (ret != 0) 1701 ret = ipforward_off (); 1702 1703 if (ret != 0) 1704 { 1705 vty_out (vty, "Can't turn off IP forwarding%s", VTY_NEWLINE); 1706 return CMD_WARNING; 1707 } 1708 1709 return CMD_SUCCESS; 1710} 1711 1712/* This command is for debugging purpose. */ 1713DEFUN (show_zebra_client, 1714 show_zebra_client_cmd, 1715 "show zebra client", 1716 SHOW_STR 1717 "Zebra information" 1718 "Client information") 1719{ 1720 struct listnode *node; 1721 struct zserv *client; 1722 1723 for (ALL_LIST_ELEMENTS_RO (zebrad.client_list, node, client)) 1724 vty_out (vty, "Client fd %d%s", client->sock, VTY_NEWLINE); 1725 1726 return CMD_SUCCESS; 1727} 1728 1729/* Table configuration write function. */ 1730static int 1731config_write_table (struct vty *vty) 1732{ 1733 if (zebrad.rtm_table_default) 1734 vty_out (vty, "table %d%s", zebrad.rtm_table_default, 1735 VTY_NEWLINE); 1736 return 0; 1737} 1738 1739/* table node for routing tables. */ 1740static struct cmd_node table_node = 1741{ 1742 TABLE_NODE, 1743 "", /* This node has no interface. */ 1744 1 1745}; 1746 1747/* Only display ip forwarding is enabled or not. */ 1748DEFUN (show_ip_forwarding, 1749 show_ip_forwarding_cmd, 1750 "show ip forwarding", 1751 SHOW_STR 1752 IP_STR 1753 "IP forwarding status\n") 1754{ 1755 int ret; 1756 1757 ret = ipforward (); 1758 1759 if (ret == 0) 1760 vty_out (vty, "IP forwarding is off%s", VTY_NEWLINE); 1761 else 1762 vty_out (vty, "IP forwarding is on%s", VTY_NEWLINE); 1763 return CMD_SUCCESS; 1764} 1765 1766#ifdef HAVE_IPV6 1767/* Only display ipv6 forwarding is enabled or not. */ 1768DEFUN (show_ipv6_forwarding, 1769 show_ipv6_forwarding_cmd, 1770 "show ipv6 forwarding", 1771 SHOW_STR 1772 "IPv6 information\n" 1773 "Forwarding status\n") 1774{ 1775 int ret; 1776 1777 ret = ipforward_ipv6 (); 1778 1779 switch (ret) 1780 { 1781 case -1: 1782 vty_out (vty, "ipv6 forwarding is unknown%s", VTY_NEWLINE); 1783 break; 1784 case 0: 1785 vty_out (vty, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE); 1786 break; 1787 case 1: 1788 vty_out (vty, "ipv6 forwarding is %s%s", "on", VTY_NEWLINE); 1789 break; 1790 default: 1791 vty_out (vty, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE); 1792 break; 1793 } 1794 return CMD_SUCCESS; 1795} 1796 1797DEFUN (ipv6_forwarding, 1798 ipv6_forwarding_cmd, 1799 "ipv6 forwarding", 1800 IPV6_STR 1801 "Turn on IPv6 forwarding") 1802{ 1803 int ret; 1804 1805 ret = ipforward_ipv6 (); 1806 if (ret == 0) 1807 ret = ipforward_ipv6_on (); 1808 1809 if (ret == 0) 1810 { 1811 vty_out (vty, "Can't turn on IPv6 forwarding%s", VTY_NEWLINE); 1812 return CMD_WARNING; 1813 } 1814 1815 return CMD_SUCCESS; 1816} 1817 1818DEFUN (no_ipv6_forwarding, 1819 no_ipv6_forwarding_cmd, 1820 "no ipv6 forwarding", 1821 NO_STR 1822 IPV6_STR 1823 "Turn off IPv6 forwarding") 1824{ 1825 int ret; 1826 1827 ret = ipforward_ipv6 (); 1828 if (ret != 0) 1829 ret = ipforward_ipv6_off (); 1830 1831 if (ret != 0) 1832 { 1833 vty_out (vty, "Can't turn off IPv6 forwarding%s", VTY_NEWLINE); 1834 return CMD_WARNING; 1835 } 1836 1837 return CMD_SUCCESS; 1838} 1839 1840#endif /* HAVE_IPV6 */ 1841 1842/* IPForwarding configuration write function. */ 1843static int 1844config_write_forwarding (struct vty *vty) 1845{ 1846 /* FIXME: Find better place for that. */ 1847 router_id_write (vty); 1848 1849 if (ipforward ()) 1850 vty_out (vty, "ip forwarding%s", VTY_NEWLINE); 1851#ifdef HAVE_IPV6 1852 if (ipforward_ipv6 ()) 1853 vty_out (vty, "ipv6 forwarding%s", VTY_NEWLINE); 1854#endif /* HAVE_IPV6 */ 1855 vty_out (vty, "!%s", VTY_NEWLINE); 1856 return 0; 1857} 1858 1859/* table node for routing tables. */ 1860static struct cmd_node forwarding_node = 1861{ 1862 FORWARDING_NODE, 1863 "", /* This node has no interface. */ 1864 1 1865}; 1866 1867 1868/* Initialisation of zebra and installation of commands. */ 1869void 1870zebra_init (void) 1871{ 1872 /* Client list init. */ 1873 zebrad.client_list = list_new (); 1874 1875 /* Install configuration write function. */ 1876 install_node (&table_node, config_write_table); 1877 install_node (&forwarding_node, config_write_forwarding); 1878 1879 install_element (VIEW_NODE, &show_ip_forwarding_cmd); 1880 install_element (ENABLE_NODE, &show_ip_forwarding_cmd); 1881 install_element (CONFIG_NODE, &ip_forwarding_cmd); 1882 install_element (CONFIG_NODE, &no_ip_forwarding_cmd); 1883 install_element (ENABLE_NODE, &show_zebra_client_cmd); 1884 1885#ifdef HAVE_NETLINK 1886 install_element (VIEW_NODE, &show_table_cmd); 1887 install_element (ENABLE_NODE, &show_table_cmd); 1888 install_element (CONFIG_NODE, &config_table_cmd); 1889#endif /* HAVE_NETLINK */ 1890 1891#ifdef HAVE_IPV6 1892 install_element (VIEW_NODE, &show_ipv6_forwarding_cmd); 1893 install_element (ENABLE_NODE, &show_ipv6_forwarding_cmd); 1894 install_element (CONFIG_NODE, &ipv6_forwarding_cmd); 1895 install_element (CONFIG_NODE, &no_ipv6_forwarding_cmd); 1896#endif /* HAVE_IPV6 */ 1897 1898 /* Route-map */ 1899 zebra_route_map_init (); 1900} 1901 1902/* Make zebra server socket, wiping any existing one (see bug #403). */ 1903void 1904zebra_zserv_socket_init (char *path) 1905{ 1906#ifdef HAVE_TCP_ZEBRA 1907 zebra_serv (); 1908#else 1909 zebra_serv_un (path ? path : ZEBRA_SERV_PATH); 1910#endif /* HAVE_TCP_ZEBRA */ 1911} 1912