paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 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" |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 36 | #include "privs.h" |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 37 | |
| 38 | #include "zebra/zserv.h" |
| 39 | #include "zebra/redistribute.h" |
| 40 | #include "zebra/debug.h" |
| 41 | #include "zebra/ipforward.h" |
| 42 | |
| 43 | /* Event list of zebra. */ |
| 44 | enum event { ZEBRA_SERV, ZEBRA_READ, ZEBRA_WRITE }; |
| 45 | |
| 46 | /* Zebra client list. */ |
| 47 | list client_list; |
| 48 | |
| 49 | /* Default rtm_table for all clients */ |
| 50 | int rtm_table_default = 0; |
| 51 | |
| 52 | void zebra_event (enum event event, int sock, struct zserv *client); |
paul | ccf3557 | 2003-03-01 11:42:20 +0000 | [diff] [blame] | 53 | |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 54 | extern struct zebra_privs_t zserv_privs; |
| 55 | |
paul | ccf3557 | 2003-03-01 11:42:20 +0000 | [diff] [blame] | 56 | extern struct thread_master *master; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 57 | |
| 58 | /* For logging of zebra meesages. */ |
| 59 | char *zebra_command_str [] = |
| 60 | { |
| 61 | "NULL", |
| 62 | "ZEBRA_INTERFACE_ADD", |
| 63 | "ZEBRA_INTERFACE_DELETE", |
| 64 | "ZEBRA_INTERFACE_ADDRESS_ADD", |
| 65 | "ZEBRA_INTERFACE_ADDRESS_DELETE", |
| 66 | "ZEBRA_INTERFACE_UP", |
| 67 | "ZEBRA_INTERFACE_DOWN", |
| 68 | "ZEBRA_IPV4_ROUTE_ADD", |
| 69 | "ZEBRA_IPV4_ROUTE_DELETE", |
| 70 | "ZEBRA_IPV6_ROUTE_ADD", |
| 71 | "ZEBRA_IPV6_ROUTE_DELETE", |
| 72 | "ZEBRA_REDISTRIBUTE_ADD", |
| 73 | "ZEBRA_REDISTRIBUTE_DELETE", |
| 74 | "ZEBRA_REDISTRIBUTE_DEFAULT_ADD", |
| 75 | "ZEBRA_REDISTRIBUTE_DEFAULT_DELETE", |
| 76 | "ZEBRA_IPV4_NEXTHOP_LOOKUP", |
| 77 | "ZEBRA_IPV6_NEXTHOP_LOOKUP", |
| 78 | "ZEBRA_IPV4_IMPORT_LOOKUP", |
| 79 | "ZEBRA_IPV6_IMPORT_LOOKUP" |
| 80 | }; |
| 81 | |
paul | ccf3557 | 2003-03-01 11:42:20 +0000 | [diff] [blame] | 82 | struct zebra_message_queue |
| 83 | { |
| 84 | struct nsm_message_queue *next; |
| 85 | struct nsm_message_queue *prev; |
| 86 | |
| 87 | u_char *buf; |
| 88 | u_int16_t length; |
| 89 | u_int16_t written; |
| 90 | }; |
| 91 | |
| 92 | struct thread *t_write; |
| 93 | struct fifo message_queue; |
| 94 | |
| 95 | int |
| 96 | zebra_server_dequeue (struct thread *t) |
| 97 | { |
| 98 | int sock; |
| 99 | int nbytes; |
| 100 | struct zebra_message_queue *queue; |
| 101 | |
| 102 | sock = THREAD_FD (t); |
| 103 | t_write = NULL; |
| 104 | |
| 105 | queue = (struct zebra_message_queue *) FIFO_HEAD (&message_queue); |
| 106 | if (queue) |
| 107 | { |
| 108 | nbytes = write (sock, queue->buf + queue->written, |
| 109 | queue->length - queue->written); |
| 110 | |
| 111 | if (nbytes <= 0) |
| 112 | { |
| 113 | if (errno != EAGAIN) |
| 114 | return -1; |
| 115 | } |
| 116 | else if (nbytes != (queue->length - queue->written)) |
| 117 | { |
| 118 | queue->written += nbytes; |
| 119 | } |
| 120 | else |
| 121 | { |
| 122 | FIFO_DEL (queue); |
| 123 | XFREE (MTYPE_TMP, queue->buf); |
| 124 | XFREE (MTYPE_TMP, queue); |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | if (FIFO_TOP (&message_queue)) |
| 129 | THREAD_WRITE_ON (master, t_write, zebra_server_dequeue, NULL, sock); |
| 130 | |
| 131 | return 0; |
| 132 | } |
| 133 | |
| 134 | /* Enqueu message. */ |
| 135 | void |
| 136 | zebra_server_enqueue (int sock, u_char *buf, unsigned long length, |
| 137 | unsigned long written) |
| 138 | { |
| 139 | struct zebra_message_queue *queue; |
| 140 | |
| 141 | queue = XCALLOC (MTYPE_TMP, sizeof (struct zebra_message_queue)); |
| 142 | queue->buf = XMALLOC (MTYPE_TMP, length); |
| 143 | memcpy (queue->buf, buf, length); |
| 144 | queue->length = length; |
| 145 | queue->written = written; |
| 146 | |
| 147 | FIFO_ADD (&message_queue, queue); |
| 148 | |
| 149 | THREAD_WRITE_ON (master, t_write, zebra_server_dequeue, NULL, sock); |
| 150 | } |
| 151 | |
| 152 | int |
| 153 | zebra_server_send_message (int sock, u_char *buf, unsigned long length) |
| 154 | { |
| 155 | int nbytes; |
| 156 | |
| 157 | if (FIFO_TOP (&message_queue)) |
| 158 | { |
| 159 | zebra_server_enqueue (sock, buf, length, 0); |
| 160 | return 0; |
| 161 | } |
| 162 | |
| 163 | /* Send message. */ |
| 164 | nbytes = write (sock, buf, length); |
| 165 | |
| 166 | if (nbytes <= 0) |
| 167 | { |
| 168 | if (errno == EAGAIN) |
| 169 | zebra_server_enqueue (sock, buf, length, 0); |
| 170 | else |
| 171 | return -1; |
| 172 | } |
| 173 | else if (nbytes != length) |
| 174 | zebra_server_enqueue (sock, buf, length, nbytes); |
| 175 | |
| 176 | return 0; |
| 177 | } |
| 178 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 179 | /* Interface is added. Send ZEBRA_INTERFACE_ADD to client. */ |
| 180 | int |
| 181 | zsend_interface_add (struct zserv *client, struct interface *ifp) |
| 182 | { |
| 183 | struct stream *s; |
| 184 | |
| 185 | /* Check this client need interface information. */ |
| 186 | if (! client->ifinfo) |
| 187 | return -1; |
| 188 | |
| 189 | s = client->obuf; |
| 190 | stream_reset (s); |
| 191 | |
| 192 | /* Place holder for size. */ |
| 193 | stream_putw (s, 0); |
| 194 | |
| 195 | /* Message type. */ |
| 196 | stream_putc (s, ZEBRA_INTERFACE_ADD); |
| 197 | |
| 198 | /* Interface information. */ |
| 199 | stream_put (s, ifp->name, INTERFACE_NAMSIZ); |
| 200 | stream_putl (s, ifp->ifindex); |
paul | 2e3b2e4 | 2002-12-13 21:03:13 +0000 | [diff] [blame] | 201 | stream_putc (s, ifp->status); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 202 | stream_putl (s, ifp->flags); |
| 203 | stream_putl (s, ifp->metric); |
| 204 | stream_putl (s, ifp->mtu); |
| 205 | stream_putl (s, ifp->bandwidth); |
| 206 | #ifdef HAVE_SOCKADDR_DL |
| 207 | stream_put (s, &ifp->sdl, sizeof (ifp->sdl)); |
| 208 | #else |
| 209 | stream_putl (s, ifp->hw_addr_len); |
| 210 | if (ifp->hw_addr_len) |
| 211 | stream_put (s, ifp->hw_addr, ifp->hw_addr_len); |
| 212 | #endif /* HAVE_SOCKADDR_DL */ |
| 213 | |
| 214 | /* Write packet size. */ |
| 215 | stream_putw_at (s, 0, stream_get_endp (s)); |
| 216 | |
paul | ccf3557 | 2003-03-01 11:42:20 +0000 | [diff] [blame] | 217 | zebra_server_send_message (client->sock, s->data, stream_get_endp (s)); |
| 218 | |
| 219 | return 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | /* Interface deletion from zebra daemon. */ |
| 223 | int |
| 224 | zsend_interface_delete (struct zserv *client, struct interface *ifp) |
| 225 | { |
| 226 | struct stream *s; |
| 227 | |
| 228 | /* Check this client need interface information. */ |
| 229 | if (! client->ifinfo) |
| 230 | return -1; |
| 231 | |
| 232 | s = client->obuf; |
| 233 | stream_reset (s); |
| 234 | |
| 235 | /* Packet length placeholder. */ |
| 236 | stream_putw (s, 0); |
| 237 | |
| 238 | /* Interface information. */ |
| 239 | stream_putc (s, ZEBRA_INTERFACE_DELETE); |
| 240 | stream_put (s, ifp->name, INTERFACE_NAMSIZ); |
| 241 | stream_putl (s, ifp->ifindex); |
paul | 2e3b2e4 | 2002-12-13 21:03:13 +0000 | [diff] [blame] | 242 | stream_putc (s, ifp->status); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 243 | stream_putl (s, ifp->flags); |
| 244 | stream_putl (s, ifp->metric); |
| 245 | stream_putl (s, ifp->mtu); |
| 246 | stream_putl (s, ifp->bandwidth); |
| 247 | |
| 248 | /* Write packet length. */ |
| 249 | stream_putw_at (s, 0, stream_get_endp (s)); |
| 250 | |
paul | ccf3557 | 2003-03-01 11:42:20 +0000 | [diff] [blame] | 251 | zebra_server_send_message (client->sock, s->data, stream_get_endp (s)); |
| 252 | |
| 253 | return 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | /* Interface address is added. Send ZEBRA_INTERFACE_ADDRESS_ADD to the |
| 257 | client. */ |
| 258 | int |
| 259 | zsend_interface_address_add (struct zserv *client, struct interface *ifp, |
| 260 | struct connected *ifc) |
| 261 | { |
| 262 | int blen; |
| 263 | struct stream *s; |
| 264 | struct prefix *p; |
| 265 | |
| 266 | /* Check this client need interface information. */ |
| 267 | if (! client->ifinfo) |
| 268 | return -1; |
| 269 | |
| 270 | s = client->obuf; |
| 271 | stream_reset (s); |
| 272 | |
| 273 | /* Place holder for size. */ |
| 274 | stream_putw (s, 0); |
| 275 | |
| 276 | stream_putc (s, ZEBRA_INTERFACE_ADDRESS_ADD); |
| 277 | stream_putl (s, ifp->ifindex); |
| 278 | |
| 279 | /* Interface address flag. */ |
| 280 | stream_putc (s, ifc->flags); |
| 281 | |
| 282 | /* Prefix information. */ |
| 283 | p = ifc->address; |
| 284 | stream_putc (s, p->family); |
| 285 | blen = prefix_blen (p); |
| 286 | stream_put (s, &p->u.prefix, blen); |
| 287 | stream_putc (s, p->prefixlen); |
| 288 | |
| 289 | /* Destination. */ |
| 290 | p = ifc->destination; |
| 291 | if (p) |
| 292 | stream_put (s, &p->u.prefix, blen); |
| 293 | else |
| 294 | stream_put (s, NULL, blen); |
| 295 | |
| 296 | /* Write packet size. */ |
| 297 | stream_putw_at (s, 0, stream_get_endp (s)); |
| 298 | |
paul | ccf3557 | 2003-03-01 11:42:20 +0000 | [diff] [blame] | 299 | zebra_server_send_message (client->sock, s->data, stream_get_endp (s)); |
| 300 | |
| 301 | return 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 302 | } |
| 303 | |
| 304 | /* Interface address is deleted. Send ZEBRA_INTERFACE_ADDRESS_DELETE |
| 305 | to the client. */ |
| 306 | int |
| 307 | zsend_interface_address_delete (struct zserv *client, struct interface *ifp, |
| 308 | struct connected *ifc) |
| 309 | { |
| 310 | int blen; |
| 311 | struct stream *s; |
| 312 | struct prefix *p; |
| 313 | |
| 314 | /* Check this client need interface information. */ |
| 315 | if (! client->ifinfo) |
| 316 | return -1; |
| 317 | |
| 318 | s = client->obuf; |
| 319 | stream_reset (s); |
| 320 | |
| 321 | /* Place holder for size. */ |
| 322 | stream_putw (s, 0); |
| 323 | |
| 324 | stream_putc (s, ZEBRA_INTERFACE_ADDRESS_DELETE); |
| 325 | stream_putl (s, ifp->ifindex); |
| 326 | |
| 327 | /* Interface address flag. */ |
| 328 | stream_putc (s, ifc->flags); |
| 329 | |
| 330 | /* Prefix information. */ |
| 331 | p = ifc->address; |
| 332 | stream_putc (s, p->family); |
| 333 | blen = prefix_blen (p); |
| 334 | stream_put (s, &p->u.prefix, blen); |
| 335 | |
| 336 | p = ifc->destination; |
| 337 | if (p) |
| 338 | stream_put (s, &p->u.prefix, blen); |
| 339 | else |
| 340 | stream_put (s, NULL, blen); |
| 341 | |
| 342 | /* Write packet size. */ |
| 343 | stream_putw_at (s, 0, stream_get_endp (s)); |
| 344 | |
paul | ccf3557 | 2003-03-01 11:42:20 +0000 | [diff] [blame] | 345 | zebra_server_send_message (client->sock, s->data, stream_get_endp (s)); |
| 346 | |
| 347 | return 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | int |
| 351 | zsend_interface_up (struct zserv *client, struct interface *ifp) |
| 352 | { |
| 353 | struct stream *s; |
| 354 | |
| 355 | /* Check this client need interface information. */ |
| 356 | if (! client->ifinfo) |
| 357 | return -1; |
| 358 | |
| 359 | s = client->obuf; |
| 360 | stream_reset (s); |
| 361 | |
| 362 | /* Place holder for size. */ |
| 363 | stream_putw (s, 0); |
| 364 | |
| 365 | /* Zebra command. */ |
| 366 | stream_putc (s, ZEBRA_INTERFACE_UP); |
| 367 | |
| 368 | /* Interface information. */ |
| 369 | stream_put (s, ifp->name, INTERFACE_NAMSIZ); |
| 370 | stream_putl (s, ifp->ifindex); |
paul | 2e3b2e4 | 2002-12-13 21:03:13 +0000 | [diff] [blame] | 371 | stream_putc (s, ifp->status); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 372 | stream_putl (s, ifp->flags); |
| 373 | stream_putl (s, ifp->metric); |
| 374 | stream_putl (s, ifp->mtu); |
| 375 | stream_putl (s, ifp->bandwidth); |
| 376 | |
| 377 | /* Write packet size. */ |
| 378 | stream_putw_at (s, 0, stream_get_endp (s)); |
| 379 | |
paul | ccf3557 | 2003-03-01 11:42:20 +0000 | [diff] [blame] | 380 | zebra_server_send_message (client->sock, s->data, stream_get_endp (s)); |
| 381 | |
| 382 | return 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | int |
| 386 | zsend_interface_down (struct zserv *client, struct interface *ifp) |
| 387 | { |
| 388 | struct stream *s; |
| 389 | |
| 390 | /* Check this client need interface information. */ |
| 391 | if (! client->ifinfo) |
| 392 | return -1; |
| 393 | |
| 394 | s = client->obuf; |
| 395 | stream_reset (s); |
| 396 | |
| 397 | /* Place holder for size. */ |
| 398 | stream_putw (s, 0); |
| 399 | |
| 400 | /* Zebra command. */ |
| 401 | stream_putc (s, ZEBRA_INTERFACE_DOWN); |
| 402 | |
| 403 | /* Interface information. */ |
| 404 | stream_put (s, ifp->name, INTERFACE_NAMSIZ); |
| 405 | stream_putl (s, ifp->ifindex); |
paul | 2e3b2e4 | 2002-12-13 21:03:13 +0000 | [diff] [blame] | 406 | stream_putc (s, ifp->status); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 407 | stream_putl (s, ifp->flags); |
| 408 | stream_putl (s, ifp->metric); |
| 409 | stream_putl (s, ifp->mtu); |
| 410 | stream_putl (s, ifp->bandwidth); |
| 411 | |
| 412 | /* Write packet size. */ |
| 413 | stream_putw_at (s, 0, stream_get_endp (s)); |
| 414 | |
paul | ccf3557 | 2003-03-01 11:42:20 +0000 | [diff] [blame] | 415 | zebra_server_send_message (client->sock, s->data, stream_get_endp (s)); |
| 416 | |
| 417 | return 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | int |
| 421 | zsend_ipv4_add_multipath (struct zserv *client, struct prefix *p, |
| 422 | struct rib *rib) |
| 423 | { |
| 424 | int psize; |
| 425 | struct stream *s; |
| 426 | struct nexthop *nexthop; |
| 427 | struct in_addr empty; |
| 428 | |
| 429 | empty.s_addr = 0; |
| 430 | s = client->obuf; |
| 431 | stream_reset (s); |
| 432 | |
| 433 | /* Place holder for size. */ |
| 434 | stream_putw (s, 0); |
| 435 | |
| 436 | /* Put command, type and nexthop. */ |
| 437 | stream_putc (s, ZEBRA_IPV4_ROUTE_ADD); |
| 438 | stream_putc (s, rib->type); |
| 439 | stream_putc (s, rib->flags); |
| 440 | stream_putc (s, ZAPI_MESSAGE_NEXTHOP | ZAPI_MESSAGE_IFINDEX | ZAPI_MESSAGE_METRIC); |
| 441 | |
| 442 | /* Prefix. */ |
| 443 | psize = PSIZE (p->prefixlen); |
| 444 | stream_putc (s, p->prefixlen); |
| 445 | stream_write (s, (u_char *)&p->u.prefix, psize); |
| 446 | |
| 447 | /* Nexthop */ |
| 448 | for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next) |
| 449 | { |
| 450 | if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB)) |
| 451 | { |
| 452 | stream_putc (s, 1); |
| 453 | |
hasso | 726f9b2 | 2003-05-25 21:04:54 +0000 | [diff] [blame] | 454 | /* XXX: Waht's about NEXTHOP_TYPE_IPV4_IFNAME ? */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 455 | if (nexthop->type == NEXTHOP_TYPE_IPV4 |
| 456 | || nexthop->type == NEXTHOP_TYPE_IPV4_IFINDEX) |
| 457 | stream_put_in_addr (s, &nexthop->gate.ipv4); |
| 458 | else |
| 459 | stream_put_in_addr (s, &empty); |
| 460 | |
| 461 | /* Interface index. */ |
| 462 | stream_putc (s, 1); |
| 463 | stream_putl (s, nexthop->ifindex); |
| 464 | |
| 465 | break; |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | /* Metric */ |
| 470 | stream_putl (s, rib->metric); |
| 471 | |
| 472 | /* Write packet size. */ |
| 473 | stream_putw_at (s, 0, stream_get_endp (s)); |
| 474 | |
paul | ccf3557 | 2003-03-01 11:42:20 +0000 | [diff] [blame] | 475 | zebra_server_send_message (client->sock, s->data, stream_get_endp (s)); |
| 476 | |
| 477 | return 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | int |
| 481 | zsend_ipv4_delete_multipath (struct zserv *client, struct prefix *p, |
| 482 | struct rib *rib) |
| 483 | { |
| 484 | int psize; |
| 485 | struct stream *s; |
| 486 | struct nexthop *nexthop; |
| 487 | struct in_addr empty; |
| 488 | |
| 489 | empty.s_addr = 0; |
| 490 | |
| 491 | s = client->obuf; |
| 492 | stream_reset (s); |
| 493 | |
| 494 | /* Place holder for size. */ |
| 495 | stream_putw (s, 0); |
| 496 | |
| 497 | /* Put command, type and nexthop. */ |
| 498 | stream_putc (s, ZEBRA_IPV4_ROUTE_DELETE); |
| 499 | stream_putc (s, rib->type); |
| 500 | stream_putc (s, rib->flags); |
| 501 | stream_putc (s, ZAPI_MESSAGE_NEXTHOP|ZAPI_MESSAGE_IFINDEX); |
| 502 | |
| 503 | /* Prefix. */ |
| 504 | psize = PSIZE (p->prefixlen); |
| 505 | stream_putc (s, p->prefixlen); |
| 506 | stream_write (s, (u_char *)&p->u.prefix, psize); |
| 507 | |
| 508 | /* Nexthop */ |
| 509 | for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next) |
| 510 | { |
| 511 | if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB)) |
| 512 | { |
| 513 | stream_putc (s, 1); |
| 514 | |
| 515 | if (nexthop->type == NEXTHOP_TYPE_IPV4) |
| 516 | stream_put_in_addr (s, &nexthop->gate.ipv4); |
| 517 | else |
| 518 | stream_put_in_addr (s, &empty); |
| 519 | |
| 520 | /* Interface index. */ |
| 521 | stream_putc (s, 1); |
| 522 | stream_putl (s, nexthop->ifindex); |
| 523 | |
| 524 | break; |
| 525 | } |
| 526 | } |
| 527 | |
| 528 | /* Write packet size. */ |
| 529 | stream_putw_at (s, 0, stream_get_endp (s)); |
| 530 | |
paul | ccf3557 | 2003-03-01 11:42:20 +0000 | [diff] [blame] | 531 | zebra_server_send_message (client->sock, s->data, stream_get_endp (s)); |
| 532 | |
| 533 | return 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 534 | } |
| 535 | |
hasso | 726f9b2 | 2003-05-25 21:04:54 +0000 | [diff] [blame] | 536 | #if 0 |
| 537 | #warning oldies |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 538 | int |
| 539 | zsend_ipv4_add (struct zserv *client, int type, int flags, |
| 540 | struct prefix_ipv4 *p, struct in_addr *nexthop, |
| 541 | unsigned int ifindex) |
| 542 | { |
| 543 | int psize; |
| 544 | struct stream *s; |
| 545 | |
| 546 | s = client->obuf; |
| 547 | stream_reset (s); |
| 548 | |
| 549 | /* Place holder for size. */ |
| 550 | stream_putw (s, 0); |
| 551 | |
| 552 | /* Put command, type and nexthop. */ |
| 553 | stream_putc (s, ZEBRA_IPV4_ROUTE_ADD); |
| 554 | stream_putc (s, type); |
| 555 | stream_putc (s, flags); |
| 556 | stream_putc (s, ZAPI_MESSAGE_NEXTHOP|ZAPI_MESSAGE_IFINDEX); |
| 557 | |
| 558 | /* Prefix. */ |
| 559 | psize = PSIZE (p->prefixlen); |
| 560 | stream_putc (s, p->prefixlen); |
| 561 | stream_write (s, (u_char *)&p->prefix, psize); |
| 562 | |
| 563 | /* Nexthop */ |
| 564 | stream_putc (s, 1); |
| 565 | stream_put_in_addr (s, nexthop); |
| 566 | |
| 567 | /* Interface index. */ |
| 568 | stream_putc (s, 1); |
| 569 | stream_putl (s, ifindex); |
| 570 | |
| 571 | /* Write packet size. */ |
| 572 | stream_putw_at (s, 0, stream_get_endp (s)); |
| 573 | |
paul | ccf3557 | 2003-03-01 11:42:20 +0000 | [diff] [blame] | 574 | zebra_server_send_message (client->sock, s->data, stream_get_endp (s)); |
| 575 | |
| 576 | return 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | int |
| 580 | zsend_ipv4_delete (struct zserv *client, int type, int flags, |
| 581 | struct prefix_ipv4 *p, struct in_addr *nexthop, |
| 582 | unsigned int ifindex) |
| 583 | { |
| 584 | int psize; |
| 585 | struct stream *s; |
| 586 | |
| 587 | s = client->obuf; |
| 588 | stream_reset (s); |
| 589 | |
| 590 | /* Place holder for size. */ |
| 591 | stream_putw (s, 0); |
| 592 | |
| 593 | /* Put command, type and nexthop. */ |
| 594 | stream_putc (s, ZEBRA_IPV4_ROUTE_DELETE); |
| 595 | stream_putc (s, type); |
| 596 | stream_putc (s, flags); |
| 597 | stream_putc (s, ZAPI_MESSAGE_NEXTHOP|ZAPI_MESSAGE_IFINDEX); |
| 598 | |
| 599 | /* Prefix. */ |
| 600 | psize = PSIZE (p->prefixlen); |
| 601 | stream_putc (s, p->prefixlen); |
| 602 | stream_write (s, (u_char *)&p->prefix, psize); |
| 603 | |
| 604 | /* Nexthop */ |
| 605 | stream_putc (s, 1); |
| 606 | stream_put_in_addr (s, nexthop); |
| 607 | |
| 608 | /* Interface index. */ |
| 609 | stream_putc (s, 1); |
| 610 | stream_putl (s, ifindex); |
| 611 | |
| 612 | /* Write packet size. */ |
| 613 | stream_putw_at (s, 0, stream_get_endp (s)); |
| 614 | |
paul | ccf3557 | 2003-03-01 11:42:20 +0000 | [diff] [blame] | 615 | zebra_server_send_message (client->sock, s->data, stream_get_endp (s)); |
| 616 | |
| 617 | return 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 618 | } |
hasso | 726f9b2 | 2003-05-25 21:04:54 +0000 | [diff] [blame] | 619 | #endif /* oldies */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 620 | |
| 621 | #ifdef HAVE_IPV6 |
hasso | 726f9b2 | 2003-05-25 21:04:54 +0000 | [diff] [blame] | 622 | #if 0 |
| 623 | #warning oldies |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 624 | int |
| 625 | zsend_ipv6_add (struct zserv *client, int type, int flags, |
| 626 | struct prefix_ipv6 *p, struct in6_addr *nexthop, |
| 627 | unsigned int ifindex) |
| 628 | { |
| 629 | int psize; |
| 630 | struct stream *s; |
| 631 | |
| 632 | s = client->obuf; |
| 633 | stream_reset (s); |
| 634 | |
| 635 | /* Place holder for size. */ |
| 636 | stream_putw (s, 0); |
| 637 | |
| 638 | /* Put command, type and nexthop. */ |
| 639 | stream_putc (s, ZEBRA_IPV6_ROUTE_ADD); |
| 640 | stream_putc (s, type); |
| 641 | stream_putc (s, flags); |
| 642 | stream_putc (s, ZAPI_MESSAGE_NEXTHOP|ZAPI_MESSAGE_IFINDEX); |
| 643 | |
| 644 | /* Prefix. */ |
| 645 | psize = PSIZE (p->prefixlen); |
| 646 | stream_putc (s, p->prefixlen); |
| 647 | stream_write (s, (u_char *)&p->prefix, psize); |
| 648 | |
| 649 | /* Nexthop */ |
| 650 | stream_putc (s, 1); |
| 651 | stream_write (s, (u_char *)nexthop, 16); |
| 652 | |
| 653 | /* Interface index. */ |
| 654 | stream_putc (s, 1); |
| 655 | stream_putl (s, ifindex); |
| 656 | |
| 657 | /* Write packet size. */ |
| 658 | stream_putw_at (s, 0, stream_get_endp (s)); |
| 659 | |
paul | ccf3557 | 2003-03-01 11:42:20 +0000 | [diff] [blame] | 660 | zebra_server_send_message (client->sock, s->data, stream_get_endp (s)); |
| 661 | |
| 662 | return 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 663 | } |
hasso | 726f9b2 | 2003-05-25 21:04:54 +0000 | [diff] [blame] | 664 | #endif /* oldies */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 665 | |
| 666 | int |
| 667 | zsend_ipv6_add_multipath (struct zserv *client, struct prefix *p, |
| 668 | struct rib *rib) |
| 669 | { |
| 670 | int psize; |
| 671 | struct stream *s; |
| 672 | struct nexthop *nexthop; |
| 673 | struct in6_addr empty; |
| 674 | |
| 675 | memset (&empty, 0, sizeof (struct in6_addr)); |
| 676 | s = client->obuf; |
| 677 | stream_reset (s); |
| 678 | |
| 679 | /* Place holder for size. */ |
| 680 | stream_putw (s, 0); |
| 681 | |
| 682 | /* Put command, type and nexthop. */ |
| 683 | stream_putc (s, ZEBRA_IPV6_ROUTE_ADD); |
| 684 | stream_putc (s, rib->type); |
| 685 | stream_putc (s, rib->flags); |
| 686 | stream_putc (s, ZAPI_MESSAGE_NEXTHOP | ZAPI_MESSAGE_IFINDEX | ZAPI_MESSAGE_METRIC); |
| 687 | |
| 688 | /* Prefix. */ |
| 689 | psize = PSIZE (p->prefixlen); |
| 690 | stream_putc (s, p->prefixlen); |
| 691 | stream_write (s, (u_char *) &p->u.prefix, psize); |
| 692 | |
| 693 | /* Nexthop */ |
| 694 | for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next) |
| 695 | { |
| 696 | if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB)) |
| 697 | { |
| 698 | stream_putc (s, 1); |
| 699 | |
hasso | 726f9b2 | 2003-05-25 21:04:54 +0000 | [diff] [blame] | 700 | if (nexthop->type == NEXTHOP_TYPE_IPV6 |
| 701 | || nexthop->type == NEXTHOP_TYPE_IPV6_IFINDEX |
| 702 | || nexthop->type == NEXTHOP_TYPE_IPV6_IFNAME) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 703 | stream_write (s, (u_char *) &nexthop->gate.ipv6, 16); |
| 704 | else |
| 705 | stream_write (s, (u_char *) &empty, 16); |
| 706 | |
| 707 | /* Interface index. */ |
| 708 | stream_putc (s, 1); |
| 709 | stream_putl (s, nexthop->ifindex); |
| 710 | |
| 711 | break; |
| 712 | } |
| 713 | } |
| 714 | |
| 715 | /* Metric */ |
| 716 | stream_putl (s, rib->metric); |
| 717 | |
| 718 | /* Write packet size. */ |
| 719 | stream_putw_at (s, 0, stream_get_endp (s)); |
| 720 | |
paul | ccf3557 | 2003-03-01 11:42:20 +0000 | [diff] [blame] | 721 | zebra_server_send_message (client->sock, s->data, stream_get_endp (s)); |
| 722 | |
| 723 | return 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 724 | } |
| 725 | |
hasso | 726f9b2 | 2003-05-25 21:04:54 +0000 | [diff] [blame] | 726 | #if 0 |
| 727 | #warning oldies |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 728 | int |
| 729 | zsend_ipv6_delete (struct zserv *client, int type, int flags, |
| 730 | struct prefix_ipv6 *p, struct in6_addr *nexthop, |
| 731 | unsigned int ifindex) |
| 732 | { |
| 733 | int psize; |
| 734 | struct stream *s; |
| 735 | |
| 736 | s = client->obuf; |
| 737 | stream_reset (s); |
| 738 | |
| 739 | /* Place holder for size. */ |
| 740 | stream_putw (s, 0); |
| 741 | |
| 742 | /* Put command, type and nexthop. */ |
| 743 | stream_putc (s, ZEBRA_IPV6_ROUTE_DELETE); |
| 744 | stream_putc (s, type); |
| 745 | stream_putc (s, flags); |
| 746 | stream_putc (s, ZAPI_MESSAGE_NEXTHOP|ZAPI_MESSAGE_IFINDEX); |
| 747 | |
| 748 | /* Prefix. */ |
| 749 | psize = PSIZE (p->prefixlen); |
| 750 | stream_putc (s, p->prefixlen); |
| 751 | stream_write (s, (u_char *)&p->prefix, psize); |
| 752 | |
| 753 | /* Nexthop */ |
| 754 | stream_putc (s, 1); |
| 755 | stream_write (s, (u_char *)nexthop, 16); |
| 756 | |
| 757 | /* Interface index. */ |
| 758 | stream_putc (s, 1); |
| 759 | stream_putl (s, ifindex); |
| 760 | |
| 761 | /* Write packet size. */ |
| 762 | stream_putw_at (s, 0, stream_get_endp (s)); |
| 763 | |
paul | ccf3557 | 2003-03-01 11:42:20 +0000 | [diff] [blame] | 764 | zebra_server_send_message (client->sock, s->data, stream_get_endp (s)); |
| 765 | |
| 766 | return 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 767 | } |
hasso | 726f9b2 | 2003-05-25 21:04:54 +0000 | [diff] [blame] | 768 | #endif /* oldies */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 769 | |
| 770 | int |
| 771 | zsend_ipv6_delete_multipath (struct zserv *client, struct prefix *p, |
| 772 | struct rib *rib) |
| 773 | { |
| 774 | int psize; |
| 775 | struct stream *s; |
| 776 | struct nexthop *nexthop; |
| 777 | struct in6_addr empty; |
| 778 | |
| 779 | memset (&empty, 0, sizeof (struct in6_addr)); |
| 780 | s = client->obuf; |
| 781 | stream_reset (s); |
| 782 | |
| 783 | /* Place holder for size. */ |
| 784 | stream_putw (s, 0); |
| 785 | |
| 786 | /* Put command, type and nexthop. */ |
| 787 | stream_putc (s, ZEBRA_IPV6_ROUTE_DELETE); |
| 788 | stream_putc (s, rib->type); |
| 789 | stream_putc (s, rib->flags); |
| 790 | stream_putc (s, ZAPI_MESSAGE_NEXTHOP|ZAPI_MESSAGE_IFINDEX); |
| 791 | |
| 792 | /* Prefix. */ |
| 793 | psize = PSIZE (p->prefixlen); |
| 794 | stream_putc (s, p->prefixlen); |
| 795 | stream_write (s, (u_char *)&p->u.prefix, psize); |
| 796 | |
| 797 | /* Nexthop */ |
| 798 | for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next) |
| 799 | { |
| 800 | if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB)) |
| 801 | { |
| 802 | stream_putc (s, 1); |
| 803 | |
| 804 | if (nexthop->type == NEXTHOP_TYPE_IPV6) |
| 805 | stream_write (s, (u_char *) &nexthop->gate.ipv6, 16); |
| 806 | else |
| 807 | stream_write (s, (u_char *) &empty, 16); |
| 808 | |
| 809 | /* Interface index. */ |
| 810 | stream_putc (s, 1); |
| 811 | stream_putl (s, nexthop->ifindex); |
| 812 | |
| 813 | break; |
| 814 | } |
| 815 | } |
| 816 | |
| 817 | /* Write packet size. */ |
| 818 | stream_putw_at (s, 0, stream_get_endp (s)); |
| 819 | |
paul | ccf3557 | 2003-03-01 11:42:20 +0000 | [diff] [blame] | 820 | zebra_server_send_message (client->sock, s->data, stream_get_endp (s)); |
| 821 | |
| 822 | return 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 823 | } |
| 824 | |
| 825 | int |
| 826 | zsend_ipv6_nexthop_lookup (struct zserv *client, struct in6_addr *addr) |
| 827 | { |
| 828 | struct stream *s; |
| 829 | struct rib *rib; |
| 830 | unsigned long nump; |
| 831 | u_char num; |
| 832 | struct nexthop *nexthop; |
| 833 | |
| 834 | /* Lookup nexthop. */ |
| 835 | rib = rib_match_ipv6 (addr); |
| 836 | |
| 837 | /* Get output stream. */ |
| 838 | s = client->obuf; |
| 839 | stream_reset (s); |
| 840 | |
| 841 | /* Fill in result. */ |
| 842 | stream_putw (s, 0); |
| 843 | stream_putc (s, ZEBRA_IPV6_NEXTHOP_LOOKUP); |
| 844 | stream_put (s, &addr, 16); |
| 845 | |
| 846 | if (rib) |
| 847 | { |
| 848 | stream_putl (s, rib->metric); |
| 849 | num = 0; |
| 850 | nump = s->putp; |
| 851 | stream_putc (s, 0); |
| 852 | for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next) |
| 853 | if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB)) |
| 854 | { |
| 855 | stream_putc (s, nexthop->type); |
| 856 | switch (nexthop->type) |
| 857 | { |
| 858 | case ZEBRA_NEXTHOP_IPV6: |
| 859 | stream_put (s, &nexthop->gate.ipv6, 16); |
| 860 | break; |
| 861 | case ZEBRA_NEXTHOP_IPV6_IFINDEX: |
| 862 | case ZEBRA_NEXTHOP_IPV6_IFNAME: |
| 863 | stream_put (s, &nexthop->gate.ipv6, 16); |
| 864 | stream_putl (s, nexthop->ifindex); |
| 865 | break; |
| 866 | case ZEBRA_NEXTHOP_IFINDEX: |
| 867 | case ZEBRA_NEXTHOP_IFNAME: |
| 868 | stream_putl (s, nexthop->ifindex); |
| 869 | break; |
| 870 | } |
| 871 | num++; |
| 872 | } |
| 873 | stream_putc_at (s, nump, num); |
| 874 | } |
| 875 | else |
| 876 | { |
| 877 | stream_putl (s, 0); |
| 878 | stream_putc (s, 0); |
| 879 | } |
| 880 | |
| 881 | stream_putw_at (s, 0, stream_get_endp (s)); |
| 882 | |
paul | ccf3557 | 2003-03-01 11:42:20 +0000 | [diff] [blame] | 883 | zebra_server_send_message (client->sock, s->data, stream_get_endp (s)); |
| 884 | |
| 885 | return 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 886 | } |
| 887 | #endif /* HAVE_IPV6 */ |
| 888 | |
| 889 | int |
| 890 | zsend_ipv4_nexthop_lookup (struct zserv *client, struct in_addr addr) |
| 891 | { |
| 892 | struct stream *s; |
| 893 | struct rib *rib; |
| 894 | unsigned long nump; |
| 895 | u_char num; |
| 896 | struct nexthop *nexthop; |
| 897 | |
| 898 | /* Lookup nexthop. */ |
| 899 | rib = rib_match_ipv4 (addr); |
| 900 | |
| 901 | /* Get output stream. */ |
| 902 | s = client->obuf; |
| 903 | stream_reset (s); |
| 904 | |
| 905 | /* Fill in result. */ |
| 906 | stream_putw (s, 0); |
| 907 | stream_putc (s, ZEBRA_IPV4_NEXTHOP_LOOKUP); |
| 908 | stream_put_in_addr (s, &addr); |
| 909 | |
| 910 | if (rib) |
| 911 | { |
| 912 | stream_putl (s, rib->metric); |
| 913 | num = 0; |
| 914 | nump = s->putp; |
| 915 | stream_putc (s, 0); |
| 916 | for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next) |
| 917 | if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB)) |
| 918 | { |
| 919 | stream_putc (s, nexthop->type); |
| 920 | switch (nexthop->type) |
| 921 | { |
| 922 | case ZEBRA_NEXTHOP_IPV4: |
| 923 | stream_put_in_addr (s, &nexthop->gate.ipv4); |
| 924 | break; |
| 925 | case ZEBRA_NEXTHOP_IFINDEX: |
| 926 | case ZEBRA_NEXTHOP_IFNAME: |
| 927 | stream_putl (s, nexthop->ifindex); |
| 928 | break; |
| 929 | } |
| 930 | num++; |
| 931 | } |
| 932 | stream_putc_at (s, nump, num); |
| 933 | } |
| 934 | else |
| 935 | { |
| 936 | stream_putl (s, 0); |
| 937 | stream_putc (s, 0); |
| 938 | } |
| 939 | |
| 940 | stream_putw_at (s, 0, stream_get_endp (s)); |
| 941 | |
paul | ccf3557 | 2003-03-01 11:42:20 +0000 | [diff] [blame] | 942 | zebra_server_send_message (client->sock, s->data, stream_get_endp (s)); |
| 943 | |
| 944 | return 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 945 | } |
| 946 | |
| 947 | int |
| 948 | zsend_ipv4_import_lookup (struct zserv *client, struct prefix_ipv4 *p) |
| 949 | { |
| 950 | struct stream *s; |
| 951 | struct rib *rib; |
| 952 | unsigned long nump; |
| 953 | u_char num; |
| 954 | struct nexthop *nexthop; |
| 955 | |
| 956 | /* Lookup nexthop. */ |
| 957 | rib = rib_lookup_ipv4 (p); |
| 958 | |
| 959 | /* Get output stream. */ |
| 960 | s = client->obuf; |
| 961 | stream_reset (s); |
| 962 | |
| 963 | /* Fill in result. */ |
| 964 | stream_putw (s, 0); |
| 965 | stream_putc (s, ZEBRA_IPV4_IMPORT_LOOKUP); |
| 966 | stream_put_in_addr (s, &p->prefix); |
| 967 | |
| 968 | if (rib) |
| 969 | { |
| 970 | stream_putl (s, rib->metric); |
| 971 | num = 0; |
| 972 | nump = s->putp; |
| 973 | stream_putc (s, 0); |
| 974 | for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next) |
| 975 | if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB)) |
| 976 | { |
| 977 | stream_putc (s, nexthop->type); |
| 978 | switch (nexthop->type) |
| 979 | { |
| 980 | case ZEBRA_NEXTHOP_IPV4: |
| 981 | stream_put_in_addr (s, &nexthop->gate.ipv4); |
| 982 | break; |
| 983 | case ZEBRA_NEXTHOP_IFINDEX: |
| 984 | case ZEBRA_NEXTHOP_IFNAME: |
| 985 | stream_putl (s, nexthop->ifindex); |
| 986 | break; |
| 987 | } |
| 988 | num++; |
| 989 | } |
| 990 | stream_putc_at (s, nump, num); |
| 991 | } |
| 992 | else |
| 993 | { |
| 994 | stream_putl (s, 0); |
| 995 | stream_putc (s, 0); |
| 996 | } |
| 997 | |
| 998 | stream_putw_at (s, 0, stream_get_endp (s)); |
| 999 | |
paul | ccf3557 | 2003-03-01 11:42:20 +0000 | [diff] [blame] | 1000 | zebra_server_send_message (client->sock, s->data, stream_get_endp (s)); |
| 1001 | |
| 1002 | return 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1003 | } |
| 1004 | |
| 1005 | /* Register zebra server interface information. Send current all |
| 1006 | interface and address information. */ |
| 1007 | void |
| 1008 | zread_interface_add (struct zserv *client, u_short length) |
| 1009 | { |
| 1010 | listnode ifnode; |
| 1011 | listnode cnode; |
| 1012 | struct interface *ifp; |
| 1013 | struct connected *c; |
| 1014 | |
| 1015 | /* Interface information is needed. */ |
| 1016 | client->ifinfo = 1; |
| 1017 | |
| 1018 | for (ifnode = listhead (iflist); ifnode; ifnode = nextnode (ifnode)) |
| 1019 | { |
| 1020 | ifp = getdata (ifnode); |
| 1021 | |
| 1022 | /* Skip pseudo interface. */ |
| 1023 | if (! CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE)) |
| 1024 | continue; |
| 1025 | |
| 1026 | zsend_interface_add (client, ifp); |
| 1027 | |
| 1028 | for (cnode = listhead (ifp->connected); cnode; nextnode (cnode)) |
| 1029 | { |
| 1030 | c = getdata (cnode); |
| 1031 | if (CHECK_FLAG (c->conf, ZEBRA_IFC_REAL)) |
| 1032 | zsend_interface_address_add (client, ifp, c); |
| 1033 | } |
| 1034 | } |
| 1035 | } |
| 1036 | |
| 1037 | /* Unregister zebra server interface information. */ |
| 1038 | void |
| 1039 | zread_interface_delete (struct zserv *client, u_short length) |
| 1040 | { |
| 1041 | client->ifinfo = 0; |
| 1042 | } |
| 1043 | |
| 1044 | /* This function support multiple nexthop. */ |
| 1045 | void |
| 1046 | zread_ipv4_add (struct zserv *client, u_short length) |
| 1047 | { |
| 1048 | int i; |
| 1049 | struct rib *rib; |
| 1050 | struct prefix_ipv4 p; |
| 1051 | u_char message; |
| 1052 | struct in_addr nexthop; |
| 1053 | u_char nexthop_num; |
| 1054 | u_char nexthop_type; |
| 1055 | struct stream *s; |
| 1056 | unsigned int ifindex; |
| 1057 | u_char ifname_len; |
| 1058 | |
| 1059 | /* Get input stream. */ |
| 1060 | s = client->ibuf; |
| 1061 | |
| 1062 | /* Allocate new rib. */ |
| 1063 | rib = XMALLOC (MTYPE_RIB, sizeof (struct rib)); |
| 1064 | memset (rib, 0, sizeof (struct rib)); |
| 1065 | |
| 1066 | /* Type, flags, message. */ |
| 1067 | rib->type = stream_getc (s); |
| 1068 | rib->flags = stream_getc (s); |
| 1069 | message = stream_getc (s); |
| 1070 | rib->uptime = time (NULL); |
| 1071 | |
| 1072 | /* IPv4 prefix. */ |
| 1073 | memset (&p, 0, sizeof (struct prefix_ipv4)); |
| 1074 | p.family = AF_INET; |
| 1075 | p.prefixlen = stream_getc (s); |
| 1076 | stream_get (&p.prefix, s, PSIZE (p.prefixlen)); |
| 1077 | |
| 1078 | /* Nexthop parse. */ |
| 1079 | if (CHECK_FLAG (message, ZAPI_MESSAGE_NEXTHOP)) |
| 1080 | { |
| 1081 | nexthop_num = stream_getc (s); |
| 1082 | |
| 1083 | for (i = 0; i < nexthop_num; i++) |
| 1084 | { |
| 1085 | nexthop_type = stream_getc (s); |
| 1086 | |
| 1087 | switch (nexthop_type) |
| 1088 | { |
| 1089 | case ZEBRA_NEXTHOP_IFINDEX: |
| 1090 | ifindex = stream_getl (s); |
| 1091 | nexthop_ifindex_add (rib, ifindex); |
| 1092 | break; |
| 1093 | case ZEBRA_NEXTHOP_IFNAME: |
| 1094 | ifname_len = stream_getc (s); |
| 1095 | stream_forward (s, ifname_len); |
| 1096 | break; |
| 1097 | case ZEBRA_NEXTHOP_IPV4: |
| 1098 | nexthop.s_addr = stream_get_ipv4 (s); |
| 1099 | nexthop_ipv4_add (rib, &nexthop); |
| 1100 | break; |
| 1101 | case ZEBRA_NEXTHOP_IPV6: |
| 1102 | stream_forward (s, IPV6_MAX_BYTELEN); |
| 1103 | break; |
paul | 595db7f | 2003-05-25 21:35:06 +0000 | [diff] [blame] | 1104 | case ZEBRA_NEXTHOP_BLACKHOLE: |
| 1105 | nexthop_blackhole_add (rib); |
| 1106 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1107 | } |
| 1108 | } |
| 1109 | } |
| 1110 | |
| 1111 | /* Distance. */ |
| 1112 | if (CHECK_FLAG (message, ZAPI_MESSAGE_DISTANCE)) |
| 1113 | rib->distance = stream_getc (s); |
| 1114 | |
| 1115 | /* Metric. */ |
| 1116 | if (CHECK_FLAG (message, ZAPI_MESSAGE_METRIC)) |
| 1117 | rib->metric = stream_getl (s); |
| 1118 | |
| 1119 | rib_add_ipv4_multipath (&p, rib); |
| 1120 | } |
| 1121 | |
| 1122 | /* Zebra server IPv4 prefix delete function. */ |
| 1123 | void |
| 1124 | zread_ipv4_delete (struct zserv *client, u_short length) |
| 1125 | { |
| 1126 | int i; |
| 1127 | struct stream *s; |
| 1128 | struct zapi_ipv4 api; |
| 1129 | struct in_addr nexthop; |
| 1130 | unsigned long ifindex; |
| 1131 | struct prefix_ipv4 p; |
| 1132 | u_char nexthop_num; |
| 1133 | u_char nexthop_type; |
| 1134 | u_char ifname_len; |
| 1135 | |
| 1136 | s = client->ibuf; |
| 1137 | ifindex = 0; |
| 1138 | nexthop.s_addr = 0; |
| 1139 | |
| 1140 | /* Type, flags, message. */ |
| 1141 | api.type = stream_getc (s); |
| 1142 | api.flags = stream_getc (s); |
| 1143 | api.message = stream_getc (s); |
| 1144 | |
| 1145 | /* IPv4 prefix. */ |
| 1146 | memset (&p, 0, sizeof (struct prefix_ipv4)); |
| 1147 | p.family = AF_INET; |
| 1148 | p.prefixlen = stream_getc (s); |
| 1149 | stream_get (&p.prefix, s, PSIZE (p.prefixlen)); |
| 1150 | |
| 1151 | /* Nexthop, ifindex, distance, metric. */ |
| 1152 | if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP)) |
| 1153 | { |
| 1154 | nexthop_num = stream_getc (s); |
| 1155 | |
| 1156 | for (i = 0; i < nexthop_num; i++) |
| 1157 | { |
| 1158 | nexthop_type = stream_getc (s); |
| 1159 | |
| 1160 | switch (nexthop_type) |
| 1161 | { |
| 1162 | case ZEBRA_NEXTHOP_IFINDEX: |
| 1163 | ifindex = stream_getl (s); |
| 1164 | break; |
| 1165 | case ZEBRA_NEXTHOP_IFNAME: |
| 1166 | ifname_len = stream_getc (s); |
| 1167 | stream_forward (s, ifname_len); |
| 1168 | break; |
| 1169 | case ZEBRA_NEXTHOP_IPV4: |
| 1170 | nexthop.s_addr = stream_get_ipv4 (s); |
| 1171 | break; |
| 1172 | case ZEBRA_NEXTHOP_IPV6: |
| 1173 | stream_forward (s, IPV6_MAX_BYTELEN); |
| 1174 | break; |
| 1175 | } |
| 1176 | } |
| 1177 | } |
| 1178 | |
| 1179 | /* Distance. */ |
| 1180 | if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE)) |
| 1181 | api.distance = stream_getc (s); |
| 1182 | else |
| 1183 | api.distance = 0; |
| 1184 | |
| 1185 | /* Metric. */ |
| 1186 | if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC)) |
| 1187 | api.metric = stream_getl (s); |
| 1188 | else |
| 1189 | api.metric = 0; |
| 1190 | |
| 1191 | rib_delete_ipv4 (api.type, api.flags, &p, &nexthop, ifindex, |
| 1192 | client->rtm_table); |
| 1193 | } |
| 1194 | |
| 1195 | /* Nexthop lookup for IPv4. */ |
| 1196 | void |
| 1197 | zread_ipv4_nexthop_lookup (struct zserv *client, u_short length) |
| 1198 | { |
| 1199 | struct in_addr addr; |
| 1200 | |
| 1201 | addr.s_addr = stream_get_ipv4 (client->ibuf); |
| 1202 | zsend_ipv4_nexthop_lookup (client, addr); |
| 1203 | } |
| 1204 | |
| 1205 | /* Nexthop lookup for IPv4. */ |
| 1206 | void |
| 1207 | zread_ipv4_import_lookup (struct zserv *client, u_short length) |
| 1208 | { |
| 1209 | struct prefix_ipv4 p; |
| 1210 | |
| 1211 | p.family = AF_INET; |
| 1212 | p.prefixlen = stream_getc (client->ibuf); |
| 1213 | p.prefix.s_addr = stream_get_ipv4 (client->ibuf); |
| 1214 | |
| 1215 | zsend_ipv4_import_lookup (client, &p); |
| 1216 | } |
| 1217 | |
| 1218 | #ifdef HAVE_IPV6 |
| 1219 | /* Zebra server IPv6 prefix add function. */ |
| 1220 | void |
| 1221 | zread_ipv6_add (struct zserv *client, u_short length) |
| 1222 | { |
| 1223 | int i; |
| 1224 | struct stream *s; |
| 1225 | struct zapi_ipv6 api; |
| 1226 | struct in6_addr nexthop; |
| 1227 | unsigned long ifindex; |
| 1228 | struct prefix_ipv6 p; |
| 1229 | |
| 1230 | s = client->ibuf; |
| 1231 | ifindex = 0; |
| 1232 | memset (&nexthop, 0, sizeof (struct in6_addr)); |
| 1233 | |
| 1234 | /* Type, flags, message. */ |
| 1235 | api.type = stream_getc (s); |
| 1236 | api.flags = stream_getc (s); |
| 1237 | api.message = stream_getc (s); |
| 1238 | |
| 1239 | /* IPv4 prefix. */ |
| 1240 | memset (&p, 0, sizeof (struct prefix_ipv6)); |
| 1241 | p.family = AF_INET6; |
| 1242 | p.prefixlen = stream_getc (s); |
| 1243 | stream_get (&p.prefix, s, PSIZE (p.prefixlen)); |
| 1244 | |
| 1245 | /* Nexthop, ifindex, distance, metric. */ |
| 1246 | if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP)) |
| 1247 | { |
| 1248 | u_char nexthop_type; |
| 1249 | |
| 1250 | api.nexthop_num = stream_getc (s); |
| 1251 | for (i = 0; i < api.nexthop_num; i++) |
| 1252 | { |
| 1253 | nexthop_type = stream_getc (s); |
| 1254 | |
| 1255 | switch (nexthop_type) |
| 1256 | { |
| 1257 | case ZEBRA_NEXTHOP_IPV6: |
| 1258 | stream_get (&nexthop, s, 16); |
| 1259 | break; |
| 1260 | case ZEBRA_NEXTHOP_IFINDEX: |
| 1261 | ifindex = stream_getl (s); |
| 1262 | break; |
| 1263 | } |
| 1264 | } |
| 1265 | } |
| 1266 | |
| 1267 | if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE)) |
| 1268 | api.distance = stream_getc (s); |
| 1269 | else |
| 1270 | api.distance = 0; |
| 1271 | |
| 1272 | if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC)) |
| 1273 | api.metric = stream_getl (s); |
| 1274 | else |
| 1275 | api.metric = 0; |
| 1276 | |
| 1277 | if (IN6_IS_ADDR_UNSPECIFIED (&nexthop)) |
| 1278 | rib_add_ipv6 (api.type, api.flags, &p, NULL, ifindex, 0); |
| 1279 | else |
| 1280 | rib_add_ipv6 (api.type, api.flags, &p, &nexthop, ifindex, 0); |
| 1281 | } |
| 1282 | |
| 1283 | /* Zebra server IPv6 prefix delete function. */ |
| 1284 | void |
| 1285 | zread_ipv6_delete (struct zserv *client, u_short length) |
| 1286 | { |
| 1287 | int i; |
| 1288 | struct stream *s; |
| 1289 | struct zapi_ipv6 api; |
| 1290 | struct in6_addr nexthop; |
| 1291 | unsigned long ifindex; |
| 1292 | struct prefix_ipv6 p; |
| 1293 | |
| 1294 | s = client->ibuf; |
| 1295 | ifindex = 0; |
| 1296 | memset (&nexthop, 0, sizeof (struct in6_addr)); |
| 1297 | |
| 1298 | /* Type, flags, message. */ |
| 1299 | api.type = stream_getc (s); |
| 1300 | api.flags = stream_getc (s); |
| 1301 | api.message = stream_getc (s); |
| 1302 | |
| 1303 | /* IPv4 prefix. */ |
| 1304 | memset (&p, 0, sizeof (struct prefix_ipv6)); |
| 1305 | p.family = AF_INET6; |
| 1306 | p.prefixlen = stream_getc (s); |
| 1307 | stream_get (&p.prefix, s, PSIZE (p.prefixlen)); |
| 1308 | |
| 1309 | /* Nexthop, ifindex, distance, metric. */ |
| 1310 | if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP)) |
| 1311 | { |
| 1312 | u_char nexthop_type; |
| 1313 | |
| 1314 | api.nexthop_num = stream_getc (s); |
| 1315 | for (i = 0; i < api.nexthop_num; i++) |
| 1316 | { |
| 1317 | nexthop_type = stream_getc (s); |
| 1318 | |
| 1319 | switch (nexthop_type) |
| 1320 | { |
| 1321 | case ZEBRA_NEXTHOP_IPV6: |
| 1322 | stream_get (&nexthop, s, 16); |
| 1323 | break; |
| 1324 | case ZEBRA_NEXTHOP_IFINDEX: |
| 1325 | ifindex = stream_getl (s); |
| 1326 | break; |
| 1327 | } |
| 1328 | } |
| 1329 | } |
| 1330 | |
| 1331 | if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE)) |
| 1332 | api.distance = stream_getc (s); |
| 1333 | else |
| 1334 | api.distance = 0; |
| 1335 | if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC)) |
| 1336 | api.metric = stream_getl (s); |
| 1337 | else |
| 1338 | api.metric = 0; |
| 1339 | |
| 1340 | if (IN6_IS_ADDR_UNSPECIFIED (&nexthop)) |
| 1341 | rib_delete_ipv6 (api.type, api.flags, &p, NULL, ifindex, 0); |
| 1342 | else |
| 1343 | rib_delete_ipv6 (api.type, api.flags, &p, &nexthop, ifindex, 0); |
| 1344 | } |
| 1345 | |
| 1346 | void |
| 1347 | zebra_read_ipv6 (int command, struct zserv *client, u_short length) |
| 1348 | { |
| 1349 | u_char type; |
| 1350 | u_char flags; |
| 1351 | struct in6_addr nexthop, *gate; |
| 1352 | u_char *lim; |
| 1353 | u_char *pnt; |
| 1354 | unsigned int ifindex; |
| 1355 | |
| 1356 | pnt = stream_pnt (client->ibuf); |
| 1357 | lim = pnt + length; |
| 1358 | |
| 1359 | type = stream_getc (client->ibuf); |
| 1360 | flags = stream_getc (client->ibuf); |
| 1361 | stream_get (&nexthop, client->ibuf, sizeof (struct in6_addr)); |
| 1362 | |
| 1363 | while (stream_pnt (client->ibuf) < lim) |
| 1364 | { |
| 1365 | int size; |
| 1366 | struct prefix_ipv6 p; |
| 1367 | |
| 1368 | ifindex = stream_getl (client->ibuf); |
| 1369 | |
| 1370 | memset (&p, 0, sizeof (struct prefix_ipv6)); |
| 1371 | p.family = AF_INET6; |
| 1372 | p.prefixlen = stream_getc (client->ibuf); |
| 1373 | size = PSIZE(p.prefixlen); |
| 1374 | stream_get (&p.prefix, client->ibuf, size); |
| 1375 | |
| 1376 | if (IN6_IS_ADDR_UNSPECIFIED (&nexthop)) |
| 1377 | gate = NULL; |
| 1378 | else |
| 1379 | gate = &nexthop; |
| 1380 | |
| 1381 | if (command == ZEBRA_IPV6_ROUTE_ADD) |
| 1382 | rib_add_ipv6 (type, flags, &p, gate, ifindex, 0); |
| 1383 | else |
| 1384 | rib_delete_ipv6 (type, flags, &p, gate, ifindex, 0); |
| 1385 | } |
| 1386 | } |
| 1387 | |
| 1388 | void |
| 1389 | zread_ipv6_nexthop_lookup (struct zserv *client, u_short length) |
| 1390 | { |
| 1391 | struct in6_addr addr; |
| 1392 | char buf[BUFSIZ]; |
| 1393 | |
| 1394 | stream_get (&addr, client->ibuf, 16); |
| 1395 | printf ("DEBUG %s\n", inet_ntop (AF_INET6, &addr, buf, BUFSIZ)); |
| 1396 | |
| 1397 | zsend_ipv6_nexthop_lookup (client, &addr); |
| 1398 | } |
| 1399 | #endif /* HAVE_IPV6 */ |
| 1400 | |
| 1401 | /* Close zebra client. */ |
| 1402 | void |
| 1403 | zebra_client_close (struct zserv *client) |
| 1404 | { |
| 1405 | /* Close file descriptor. */ |
| 1406 | if (client->sock) |
| 1407 | { |
| 1408 | close (client->sock); |
| 1409 | client->sock = -1; |
| 1410 | } |
| 1411 | |
| 1412 | /* Free stream buffers. */ |
| 1413 | if (client->ibuf) |
| 1414 | stream_free (client->ibuf); |
| 1415 | if (client->obuf) |
| 1416 | stream_free (client->obuf); |
| 1417 | |
| 1418 | /* Release threads. */ |
| 1419 | if (client->t_read) |
| 1420 | thread_cancel (client->t_read); |
| 1421 | if (client->t_write) |
| 1422 | thread_cancel (client->t_write); |
| 1423 | |
| 1424 | /* Free client structure. */ |
| 1425 | listnode_delete (client_list, client); |
| 1426 | XFREE (0, client); |
| 1427 | } |
| 1428 | |
| 1429 | /* Make new client. */ |
| 1430 | void |
| 1431 | zebra_client_create (int sock) |
| 1432 | { |
| 1433 | struct zserv *client; |
| 1434 | |
| 1435 | client = XCALLOC (0, sizeof (struct zserv)); |
| 1436 | |
| 1437 | /* Make client input/output buffer. */ |
| 1438 | client->sock = sock; |
| 1439 | client->ibuf = stream_new (ZEBRA_MAX_PACKET_SIZ); |
| 1440 | client->obuf = stream_new (ZEBRA_MAX_PACKET_SIZ); |
| 1441 | |
| 1442 | /* Set table number. */ |
| 1443 | client->rtm_table = rtm_table_default; |
| 1444 | |
| 1445 | /* Add this client to linked list. */ |
| 1446 | listnode_add (client_list, client); |
| 1447 | |
| 1448 | /* Make new read thread. */ |
| 1449 | zebra_event (ZEBRA_READ, sock, client); |
| 1450 | } |
| 1451 | |
| 1452 | /* Handler of zebra service request. */ |
| 1453 | int |
| 1454 | zebra_client_read (struct thread *thread) |
| 1455 | { |
| 1456 | int sock; |
| 1457 | struct zserv *client; |
| 1458 | int nbyte; |
| 1459 | u_short length; |
| 1460 | u_char command; |
| 1461 | |
| 1462 | /* Get thread data. Reset reading thread because I'm running. */ |
| 1463 | sock = THREAD_FD (thread); |
| 1464 | client = THREAD_ARG (thread); |
| 1465 | client->t_read = NULL; |
| 1466 | |
| 1467 | /* Read length and command. */ |
| 1468 | nbyte = stream_read (client->ibuf, sock, 3); |
| 1469 | if (nbyte <= 0) |
| 1470 | { |
| 1471 | if (IS_ZEBRA_DEBUG_EVENT) |
| 1472 | zlog_info ("connection closed socket [%d]", sock); |
| 1473 | zebra_client_close (client); |
| 1474 | return -1; |
| 1475 | } |
| 1476 | length = stream_getw (client->ibuf); |
| 1477 | command = stream_getc (client->ibuf); |
| 1478 | |
| 1479 | if (length < 3) |
| 1480 | { |
| 1481 | if (IS_ZEBRA_DEBUG_EVENT) |
| 1482 | zlog_info ("length %d is less than 3 ", length); |
| 1483 | zebra_client_close (client); |
| 1484 | return -1; |
| 1485 | } |
| 1486 | |
| 1487 | length -= 3; |
| 1488 | |
| 1489 | /* Read rest of data. */ |
| 1490 | if (length) |
| 1491 | { |
| 1492 | nbyte = stream_read (client->ibuf, sock, length); |
| 1493 | if (nbyte <= 0) |
| 1494 | { |
| 1495 | if (IS_ZEBRA_DEBUG_EVENT) |
| 1496 | zlog_info ("connection closed [%d] when reading zebra data", sock); |
| 1497 | zebra_client_close (client); |
| 1498 | return -1; |
| 1499 | } |
| 1500 | } |
| 1501 | |
| 1502 | /* Debug packet information. */ |
| 1503 | if (IS_ZEBRA_DEBUG_EVENT) |
| 1504 | zlog_info ("zebra message comes from socket [%d]", sock); |
| 1505 | |
| 1506 | if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV) |
| 1507 | zlog_info ("zebra message received [%s] %d", |
| 1508 | zebra_command_str[command], length); |
| 1509 | |
| 1510 | switch (command) |
| 1511 | { |
| 1512 | case ZEBRA_INTERFACE_ADD: |
| 1513 | zread_interface_add (client, length); |
| 1514 | break; |
| 1515 | case ZEBRA_INTERFACE_DELETE: |
| 1516 | zread_interface_delete (client, length); |
| 1517 | break; |
| 1518 | case ZEBRA_IPV4_ROUTE_ADD: |
| 1519 | zread_ipv4_add (client, length); |
| 1520 | break; |
| 1521 | case ZEBRA_IPV4_ROUTE_DELETE: |
| 1522 | zread_ipv4_delete (client, length); |
| 1523 | break; |
| 1524 | #ifdef HAVE_IPV6 |
| 1525 | case ZEBRA_IPV6_ROUTE_ADD: |
| 1526 | zread_ipv6_add (client, length); |
| 1527 | break; |
| 1528 | case ZEBRA_IPV6_ROUTE_DELETE: |
| 1529 | zread_ipv6_delete (client, length); |
| 1530 | break; |
| 1531 | #endif /* HAVE_IPV6 */ |
| 1532 | case ZEBRA_REDISTRIBUTE_ADD: |
| 1533 | zebra_redistribute_add (command, client, length); |
| 1534 | break; |
| 1535 | case ZEBRA_REDISTRIBUTE_DELETE: |
| 1536 | zebra_redistribute_delete (command, client, length); |
| 1537 | break; |
| 1538 | case ZEBRA_REDISTRIBUTE_DEFAULT_ADD: |
| 1539 | zebra_redistribute_default_add (command, client, length); |
| 1540 | break; |
| 1541 | case ZEBRA_REDISTRIBUTE_DEFAULT_DELETE: |
| 1542 | zebra_redistribute_default_delete (command, client, length); |
| 1543 | break; |
| 1544 | case ZEBRA_IPV4_NEXTHOP_LOOKUP: |
| 1545 | zread_ipv4_nexthop_lookup (client, length); |
| 1546 | break; |
| 1547 | #ifdef HAVE_IPV6 |
| 1548 | case ZEBRA_IPV6_NEXTHOP_LOOKUP: |
| 1549 | zread_ipv6_nexthop_lookup (client, length); |
| 1550 | break; |
| 1551 | #endif /* HAVE_IPV6 */ |
| 1552 | case ZEBRA_IPV4_IMPORT_LOOKUP: |
| 1553 | zread_ipv4_import_lookup (client, length); |
| 1554 | break; |
| 1555 | default: |
| 1556 | zlog_info ("Zebra received unknown command %d", command); |
| 1557 | break; |
| 1558 | } |
| 1559 | |
| 1560 | stream_reset (client->ibuf); |
| 1561 | zebra_event (ZEBRA_READ, sock, client); |
| 1562 | |
| 1563 | return 0; |
| 1564 | } |
| 1565 | |
| 1566 | /* Write output buffer to the socket. */ |
| 1567 | void |
| 1568 | zebra_write (struct thread *thread) |
| 1569 | { |
| 1570 | int sock; |
| 1571 | struct zserv *client; |
| 1572 | |
| 1573 | /* Thread treatment. */ |
| 1574 | sock = THREAD_FD (thread); |
| 1575 | client = THREAD_ARG (thread); |
| 1576 | client->t_write = NULL; |
| 1577 | |
| 1578 | stream_flush (client->obuf, sock); |
| 1579 | } |
| 1580 | |
| 1581 | /* Accept code of zebra server socket. */ |
| 1582 | int |
| 1583 | zebra_accept (struct thread *thread) |
| 1584 | { |
paul | ccf3557 | 2003-03-01 11:42:20 +0000 | [diff] [blame] | 1585 | int val; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1586 | int accept_sock; |
| 1587 | int client_sock; |
| 1588 | struct sockaddr_in client; |
| 1589 | socklen_t len; |
| 1590 | |
| 1591 | accept_sock = THREAD_FD (thread); |
| 1592 | |
| 1593 | len = sizeof (struct sockaddr_in); |
| 1594 | client_sock = accept (accept_sock, (struct sockaddr *) &client, &len); |
| 1595 | |
| 1596 | if (client_sock < 0) |
| 1597 | { |
| 1598 | zlog_warn ("Can't accept zebra socket: %s", strerror (errno)); |
| 1599 | return -1; |
| 1600 | } |
| 1601 | |
paul | ccf3557 | 2003-03-01 11:42:20 +0000 | [diff] [blame] | 1602 | /* Make client socket non-blocking. */ |
| 1603 | |
| 1604 | val = fcntl (client_sock, F_GETFL, 0); |
| 1605 | fcntl (client_sock, F_SETFL, (val | O_NONBLOCK)); |
| 1606 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1607 | /* Create new zebra client. */ |
| 1608 | zebra_client_create (client_sock); |
| 1609 | |
| 1610 | /* Register myself. */ |
| 1611 | zebra_event (ZEBRA_SERV, accept_sock, NULL); |
| 1612 | |
| 1613 | return 0; |
| 1614 | } |
| 1615 | |
| 1616 | /* Make zebra's server socket. */ |
| 1617 | void |
| 1618 | zebra_serv () |
| 1619 | { |
| 1620 | int ret; |
| 1621 | int accept_sock; |
| 1622 | struct sockaddr_in addr; |
| 1623 | |
| 1624 | accept_sock = socket (AF_INET, SOCK_STREAM, 0); |
| 1625 | |
| 1626 | if (accept_sock < 0) |
| 1627 | { |
| 1628 | zlog_warn ("Can't bind to socket: %s", strerror (errno)); |
| 1629 | zlog_warn ("zebra can't provice full functionality due to above error"); |
| 1630 | return; |
| 1631 | } |
| 1632 | |
| 1633 | memset (&addr, 0, sizeof (struct sockaddr_in)); |
| 1634 | addr.sin_family = AF_INET; |
| 1635 | addr.sin_port = htons (ZEBRA_PORT); |
| 1636 | #ifdef HAVE_SIN_LEN |
| 1637 | addr.sin_len = sizeof (struct sockaddr_in); |
| 1638 | #endif /* HAVE_SIN_LEN */ |
| 1639 | addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK); |
| 1640 | |
| 1641 | sockopt_reuseaddr (accept_sock); |
| 1642 | sockopt_reuseport (accept_sock); |
| 1643 | |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 1644 | if ( zserv_privs.change(ZPRIVS_RAISE) ) |
| 1645 | zlog (NULL, LOG_ERR, "Can't raise privileges"); |
| 1646 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1647 | ret = bind (accept_sock, (struct sockaddr *)&addr, |
| 1648 | sizeof (struct sockaddr_in)); |
| 1649 | if (ret < 0) |
| 1650 | { |
| 1651 | zlog_warn ("Can't bind to socket: %s", strerror (errno)); |
| 1652 | zlog_warn ("zebra can't provice full functionality due to above error"); |
| 1653 | close (accept_sock); /* Avoid sd leak. */ |
| 1654 | return; |
| 1655 | } |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 1656 | |
| 1657 | if ( zserv_privs.change(ZPRIVS_LOWER) ) |
| 1658 | zlog (NULL, LOG_ERR, "Can't lower privileges"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1659 | |
| 1660 | ret = listen (accept_sock, 1); |
| 1661 | if (ret < 0) |
| 1662 | { |
| 1663 | zlog_warn ("Can't listen to socket: %s", strerror (errno)); |
| 1664 | zlog_warn ("zebra can't provice full functionality due to above error"); |
| 1665 | close (accept_sock); /* Avoid sd leak. */ |
| 1666 | return; |
| 1667 | } |
| 1668 | |
| 1669 | zebra_event (ZEBRA_SERV, accept_sock, NULL); |
| 1670 | } |
| 1671 | |
| 1672 | /* For sockaddr_un. */ |
| 1673 | #include <sys/un.h> |
| 1674 | |
| 1675 | /* zebra server UNIX domain socket. */ |
| 1676 | void |
| 1677 | zebra_serv_un (char *path) |
| 1678 | { |
| 1679 | int ret; |
| 1680 | int sock, len; |
| 1681 | struct sockaddr_un serv; |
| 1682 | mode_t old_mask; |
| 1683 | |
| 1684 | /* First of all, unlink existing socket */ |
| 1685 | unlink (path); |
| 1686 | |
| 1687 | /* Set umask */ |
| 1688 | old_mask = umask (0077); |
| 1689 | |
| 1690 | /* Make UNIX domain socket. */ |
| 1691 | sock = socket (AF_UNIX, SOCK_STREAM, 0); |
| 1692 | if (sock < 0) |
| 1693 | { |
| 1694 | perror ("sock"); |
| 1695 | return; |
| 1696 | } |
| 1697 | |
| 1698 | /* Make server socket. */ |
| 1699 | memset (&serv, 0, sizeof (struct sockaddr_un)); |
| 1700 | serv.sun_family = AF_UNIX; |
| 1701 | strncpy (serv.sun_path, path, strlen (path)); |
| 1702 | #ifdef HAVE_SUN_LEN |
| 1703 | len = serv.sun_len = SUN_LEN(&serv); |
| 1704 | #else |
| 1705 | len = sizeof (serv.sun_family) + strlen (serv.sun_path); |
| 1706 | #endif /* HAVE_SUN_LEN */ |
| 1707 | |
| 1708 | ret = bind (sock, (struct sockaddr *) &serv, len); |
| 1709 | if (ret < 0) |
| 1710 | { |
| 1711 | perror ("bind"); |
| 1712 | close (sock); |
| 1713 | return; |
| 1714 | } |
| 1715 | |
| 1716 | ret = listen (sock, 5); |
| 1717 | if (ret < 0) |
| 1718 | { |
| 1719 | perror ("listen"); |
| 1720 | close (sock); |
| 1721 | return; |
| 1722 | } |
| 1723 | |
| 1724 | umask (old_mask); |
| 1725 | |
| 1726 | zebra_event (ZEBRA_SERV, sock, NULL); |
| 1727 | } |
| 1728 | |
| 1729 | /* Zebra's event management function. */ |
| 1730 | extern struct thread_master *master; |
| 1731 | |
| 1732 | void |
| 1733 | zebra_event (enum event event, int sock, struct zserv *client) |
| 1734 | { |
| 1735 | switch (event) |
| 1736 | { |
| 1737 | case ZEBRA_SERV: |
| 1738 | thread_add_read (master, zebra_accept, client, sock); |
| 1739 | break; |
| 1740 | case ZEBRA_READ: |
| 1741 | client->t_read = |
| 1742 | thread_add_read (master, zebra_client_read, client, sock); |
| 1743 | break; |
| 1744 | case ZEBRA_WRITE: |
| 1745 | /**/ |
| 1746 | break; |
| 1747 | } |
| 1748 | } |
| 1749 | |
| 1750 | /* Display default rtm_table for all clients. */ |
| 1751 | DEFUN (show_table, |
| 1752 | show_table_cmd, |
| 1753 | "show table", |
| 1754 | SHOW_STR |
| 1755 | "default routing table to use for all clients\n") |
| 1756 | { |
| 1757 | vty_out (vty, "table %d%s", rtm_table_default, |
| 1758 | VTY_NEWLINE); |
| 1759 | return CMD_SUCCESS; |
| 1760 | } |
| 1761 | |
| 1762 | DEFUN (config_table, |
| 1763 | config_table_cmd, |
| 1764 | "table TABLENO", |
| 1765 | "Configure target kernel routing table\n" |
| 1766 | "TABLE integer\n") |
| 1767 | { |
| 1768 | rtm_table_default = strtol (argv[0], (char**)0, 10); |
| 1769 | return CMD_SUCCESS; |
| 1770 | } |
| 1771 | |
hasso | 647e4f1 | 2003-05-25 11:43:52 +0000 | [diff] [blame] | 1772 | DEFUN (ip_forwarding, |
| 1773 | ip_forwarding_cmd, |
| 1774 | "ip forwarding", |
| 1775 | IP_STR |
| 1776 | "Turn on IP forwarding") |
| 1777 | { |
| 1778 | int ret; |
| 1779 | |
| 1780 | ret = ipforward (); |
| 1781 | |
| 1782 | if (ret != 0) |
| 1783 | { |
| 1784 | vty_out (vty, "IP forwarding is already on%s", VTY_NEWLINE); |
| 1785 | return CMD_ERR_NOTHING_TODO; |
| 1786 | } |
| 1787 | |
| 1788 | ret = ipforward_on (); |
| 1789 | if (ret == 0) |
| 1790 | { |
| 1791 | vty_out (vty, "Can't turn on IP forwarding%s", VTY_NEWLINE); |
| 1792 | return CMD_WARNING; |
| 1793 | } |
| 1794 | |
| 1795 | return CMD_SUCCESS; |
| 1796 | } |
| 1797 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1798 | DEFUN (no_ip_forwarding, |
| 1799 | no_ip_forwarding_cmd, |
| 1800 | "no ip forwarding", |
| 1801 | NO_STR |
| 1802 | IP_STR |
| 1803 | "Turn off IP forwarding") |
| 1804 | { |
| 1805 | int ret; |
| 1806 | |
| 1807 | ret = ipforward (); |
| 1808 | |
| 1809 | if (ret == 0) |
| 1810 | { |
| 1811 | vty_out (vty, "IP forwarding is already off%s", VTY_NEWLINE); |
| 1812 | return CMD_ERR_NOTHING_TODO; |
| 1813 | } |
| 1814 | |
| 1815 | ret = ipforward_off (); |
| 1816 | if (ret != 0) |
| 1817 | { |
| 1818 | vty_out (vty, "Can't turn off IP forwarding%s", VTY_NEWLINE); |
| 1819 | return CMD_WARNING; |
| 1820 | } |
| 1821 | |
| 1822 | return CMD_SUCCESS; |
| 1823 | } |
| 1824 | |
| 1825 | /* This command is for debugging purpose. */ |
| 1826 | DEFUN (show_zebra_client, |
| 1827 | show_zebra_client_cmd, |
| 1828 | "show zebra client", |
| 1829 | SHOW_STR |
| 1830 | "Zebra information" |
| 1831 | "Client information") |
| 1832 | { |
| 1833 | listnode node; |
| 1834 | struct zserv *client; |
| 1835 | |
| 1836 | for (node = listhead (client_list); node; nextnode (node)) |
| 1837 | { |
| 1838 | client = getdata (node); |
| 1839 | vty_out (vty, "Client fd %d%s", client->sock, VTY_NEWLINE); |
| 1840 | } |
| 1841 | return CMD_SUCCESS; |
| 1842 | } |
| 1843 | |
| 1844 | /* Table configuration write function. */ |
| 1845 | int |
| 1846 | config_write_table (struct vty *vty) |
| 1847 | { |
| 1848 | if (rtm_table_default) |
| 1849 | vty_out (vty, "table %d%s", rtm_table_default, |
| 1850 | VTY_NEWLINE); |
| 1851 | return 0; |
| 1852 | } |
| 1853 | |
| 1854 | /* table node for routing tables. */ |
| 1855 | struct cmd_node table_node = |
| 1856 | { |
| 1857 | TABLE_NODE, |
| 1858 | "", /* This node has no interface. */ |
| 1859 | 1 |
| 1860 | }; |
| 1861 | |
| 1862 | /* Only display ip forwarding is enabled or not. */ |
| 1863 | DEFUN (show_ip_forwarding, |
| 1864 | show_ip_forwarding_cmd, |
| 1865 | "show ip forwarding", |
| 1866 | SHOW_STR |
| 1867 | IP_STR |
| 1868 | "IP forwarding status\n") |
| 1869 | { |
| 1870 | int ret; |
| 1871 | |
| 1872 | ret = ipforward (); |
| 1873 | |
| 1874 | if (ret == 0) |
| 1875 | vty_out (vty, "IP forwarding is off%s", VTY_NEWLINE); |
| 1876 | else |
| 1877 | vty_out (vty, "IP forwarding is on%s", VTY_NEWLINE); |
| 1878 | return CMD_SUCCESS; |
| 1879 | } |
| 1880 | |
| 1881 | #ifdef HAVE_IPV6 |
| 1882 | /* Only display ipv6 forwarding is enabled or not. */ |
| 1883 | DEFUN (show_ipv6_forwarding, |
| 1884 | show_ipv6_forwarding_cmd, |
| 1885 | "show ipv6 forwarding", |
| 1886 | SHOW_STR |
| 1887 | "IPv6 information\n" |
| 1888 | "Forwarding status\n") |
| 1889 | { |
| 1890 | int ret; |
| 1891 | |
| 1892 | ret = ipforward_ipv6 (); |
| 1893 | |
| 1894 | switch (ret) |
| 1895 | { |
| 1896 | case -1: |
| 1897 | vty_out (vty, "ipv6 forwarding is unknown%s", VTY_NEWLINE); |
| 1898 | break; |
| 1899 | case 0: |
| 1900 | vty_out (vty, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE); |
| 1901 | break; |
| 1902 | case 1: |
| 1903 | vty_out (vty, "ipv6 forwarding is %s%s", "on", VTY_NEWLINE); |
| 1904 | break; |
| 1905 | default: |
| 1906 | vty_out (vty, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE); |
| 1907 | break; |
| 1908 | } |
| 1909 | return CMD_SUCCESS; |
| 1910 | } |
| 1911 | |
| 1912 | DEFUN (no_ipv6_forwarding, |
| 1913 | no_ipv6_forwarding_cmd, |
| 1914 | "no ipv6 forwarding", |
| 1915 | NO_STR |
| 1916 | IP_STR |
| 1917 | "Doesn't forward IPv6 protocol packet") |
| 1918 | { |
| 1919 | int ret; |
| 1920 | |
| 1921 | ret = ipforward_ipv6_off (); |
| 1922 | if (ret != 0) |
| 1923 | { |
| 1924 | vty_out (vty, "Can't turn off IPv6 forwarding%s", VTY_NEWLINE); |
| 1925 | return CMD_WARNING; |
| 1926 | } |
| 1927 | |
| 1928 | return CMD_SUCCESS; |
| 1929 | } |
| 1930 | |
| 1931 | #endif /* HAVE_IPV6 */ |
| 1932 | |
| 1933 | /* IPForwarding configuration write function. */ |
| 1934 | int |
| 1935 | config_write_forwarding (struct vty *vty) |
| 1936 | { |
| 1937 | if (! ipforward ()) |
| 1938 | vty_out (vty, "no ip forwarding%s", VTY_NEWLINE); |
| 1939 | #ifdef HAVE_IPV6 |
| 1940 | if (! ipforward_ipv6 ()) |
| 1941 | vty_out (vty, "no ipv6 forwarding%s", VTY_NEWLINE); |
| 1942 | #endif /* HAVE_IPV6 */ |
| 1943 | vty_out (vty, "!%s", VTY_NEWLINE); |
| 1944 | return 0; |
| 1945 | } |
| 1946 | |
| 1947 | /* table node for routing tables. */ |
| 1948 | struct cmd_node forwarding_node = |
| 1949 | { |
| 1950 | FORWARDING_NODE, |
| 1951 | "", /* This node has no interface. */ |
| 1952 | 1 |
| 1953 | }; |
| 1954 | |
| 1955 | |
| 1956 | /* Initialisation of zebra and installation of commands. */ |
| 1957 | void |
| 1958 | zebra_init () |
| 1959 | { |
| 1960 | /* Client list init. */ |
| 1961 | client_list = list_new (); |
| 1962 | |
| 1963 | /* Forwarding is on by default. */ |
| 1964 | ipforward_on (); |
| 1965 | #ifdef HAVE_IPV6 |
| 1966 | ipforward_ipv6_on (); |
| 1967 | #endif /* HAVE_IPV6 */ |
| 1968 | |
| 1969 | /* Make zebra server socket. */ |
| 1970 | #ifdef HAVE_TCP_ZEBRA |
| 1971 | zebra_serv (); |
| 1972 | #else |
| 1973 | zebra_serv_un (ZEBRA_SERV_PATH); |
| 1974 | #endif /* HAVE_TCP_ZEBRA */ |
| 1975 | |
| 1976 | /* Install configuration write function. */ |
| 1977 | install_node (&table_node, config_write_table); |
| 1978 | install_node (&forwarding_node, config_write_forwarding); |
| 1979 | |
| 1980 | install_element (VIEW_NODE, &show_ip_forwarding_cmd); |
| 1981 | install_element (ENABLE_NODE, &show_ip_forwarding_cmd); |
hasso | 647e4f1 | 2003-05-25 11:43:52 +0000 | [diff] [blame] | 1982 | install_element (CONFIG_NODE, &ip_forwarding_cmd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1983 | install_element (CONFIG_NODE, &no_ip_forwarding_cmd); |
| 1984 | install_element (ENABLE_NODE, &show_zebra_client_cmd); |
| 1985 | |
| 1986 | #ifdef HAVE_NETLINK |
| 1987 | install_element (VIEW_NODE, &show_table_cmd); |
| 1988 | install_element (ENABLE_NODE, &show_table_cmd); |
| 1989 | install_element (CONFIG_NODE, &config_table_cmd); |
| 1990 | #endif /* HAVE_NETLINK */ |
| 1991 | |
| 1992 | #ifdef HAVE_IPV6 |
| 1993 | install_element (VIEW_NODE, &show_ipv6_forwarding_cmd); |
| 1994 | install_element (ENABLE_NODE, &show_ipv6_forwarding_cmd); |
| 1995 | install_element (CONFIG_NODE, &no_ipv6_forwarding_cmd); |
| 1996 | #endif /* HAVE_IPV6 */ |
paul | ccf3557 | 2003-03-01 11:42:20 +0000 | [diff] [blame] | 1997 | |
| 1998 | FIFO_INIT(&message_queue); |
| 1999 | t_write = NULL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2000 | } |