paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1 | /* Zebra's client library. |
| 2 | * Copyright (C) 1999 Kunihiro Ishiguro |
ajs | 634f9ea | 2005-04-11 15:51:40 +0000 | [diff] [blame] | 3 | * Copyright (C) 2005 Andrew J. Schorr |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 4 | * |
| 5 | * This file is part of GNU Zebra. |
| 6 | * |
| 7 | * GNU Zebra is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published |
| 9 | * by the Free Software Foundation; either version 2, or (at your |
| 10 | * option) any later version. |
| 11 | * |
| 12 | * GNU Zebra is distributed in the hope that it will be useful, but |
| 13 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | * General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with GNU Zebra; see the file COPYING. If not, write to the |
| 19 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
| 20 | * MA 02111-1307, USA. |
| 21 | */ |
| 22 | |
| 23 | #include <zebra.h> |
| 24 | |
| 25 | #include "prefix.h" |
| 26 | #include "stream.h" |
ajs | 634f9ea | 2005-04-11 15:51:40 +0000 | [diff] [blame] | 27 | #include "buffer.h" |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 28 | #include "network.h" |
| 29 | #include "if.h" |
| 30 | #include "log.h" |
| 31 | #include "thread.h" |
| 32 | #include "zclient.h" |
| 33 | #include "memory.h" |
| 34 | #include "table.h" |
| 35 | |
| 36 | #include "zebra/rib.h" |
| 37 | #include "zebra/zserv.h" |
| 38 | |
| 39 | /* Zebra client events. */ |
| 40 | enum event {ZCLIENT_SCHEDULE, ZCLIENT_READ, ZCLIENT_CONNECT}; |
| 41 | |
| 42 | /* Prototype for event manager. */ |
| 43 | static void zclient_event (enum event, struct zclient *); |
| 44 | |
ajs | 634f9ea | 2005-04-11 15:51:40 +0000 | [diff] [blame] | 45 | extern struct thread_master *master; |
| 46 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 47 | /* This file local debug flag. */ |
| 48 | int zclient_debug = 0; |
| 49 | |
| 50 | /* Allocate zclient structure. */ |
| 51 | struct zclient * |
| 52 | zclient_new () |
| 53 | { |
| 54 | struct zclient *zclient; |
| 55 | zclient = XMALLOC (MTYPE_ZCLIENT, sizeof (struct zclient)); |
| 56 | memset (zclient, 0, sizeof (struct zclient)); |
| 57 | |
| 58 | zclient->ibuf = stream_new (ZEBRA_MAX_PACKET_SIZ); |
| 59 | zclient->obuf = stream_new (ZEBRA_MAX_PACKET_SIZ); |
ajs | 634f9ea | 2005-04-11 15:51:40 +0000 | [diff] [blame] | 60 | zclient->wb = buffer_new(0); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 61 | |
| 62 | return zclient; |
| 63 | } |
| 64 | |
ajs | 634f9ea | 2005-04-11 15:51:40 +0000 | [diff] [blame] | 65 | #if 0 |
| 66 | /* This function is never used. And it must not be used, because |
| 67 | many parts of the code do not check for I/O errors, so they could |
| 68 | reference an invalid pointer if the structure was ever freed. |
| 69 | */ |
| 70 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 71 | /* Free zclient structure. */ |
| 72 | void |
| 73 | zclient_free (struct zclient *zclient) |
| 74 | { |
ajs | 634f9ea | 2005-04-11 15:51:40 +0000 | [diff] [blame] | 75 | if (zclient->ibuf) |
| 76 | stream_free(zclient->ibuf); |
| 77 | if (zclient->obuf) |
| 78 | stream_free(zclient->obuf); |
| 79 | if (zclient->wb) |
| 80 | buffer_free(zclient->wb); |
| 81 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 82 | XFREE (MTYPE_ZCLIENT, zclient); |
| 83 | } |
ajs | 634f9ea | 2005-04-11 15:51:40 +0000 | [diff] [blame] | 84 | #endif |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 85 | |
| 86 | /* Initialize zebra client. Argument redist_default is unwanted |
| 87 | redistribute route type. */ |
| 88 | void |
| 89 | zclient_init (struct zclient *zclient, int redist_default) |
| 90 | { |
| 91 | int i; |
| 92 | |
| 93 | /* Enable zebra client connection by default. */ |
| 94 | zclient->enable = 1; |
| 95 | |
| 96 | /* Set -1 to the default socket value. */ |
| 97 | zclient->sock = -1; |
| 98 | |
| 99 | /* Clear redistribution flags. */ |
| 100 | for (i = 0; i < ZEBRA_ROUTE_MAX; i++) |
| 101 | zclient->redist[i] = 0; |
| 102 | |
| 103 | /* Set unwanted redistribute route. bgpd does not need BGP route |
| 104 | redistribution. */ |
| 105 | zclient->redist_default = redist_default; |
| 106 | zclient->redist[redist_default] = 1; |
| 107 | |
| 108 | /* Set default-information redistribute to zero. */ |
| 109 | zclient->default_information = 0; |
| 110 | |
| 111 | /* Schedule first zclient connection. */ |
| 112 | if (zclient_debug) |
ajs | 8ddca70 | 2004-12-07 18:53:52 +0000 | [diff] [blame] | 113 | zlog_debug ("zclient start scheduled"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 114 | |
| 115 | zclient_event (ZCLIENT_SCHEDULE, zclient); |
| 116 | } |
| 117 | |
| 118 | /* Stop zebra client services. */ |
| 119 | void |
| 120 | zclient_stop (struct zclient *zclient) |
| 121 | { |
| 122 | if (zclient_debug) |
ajs | 8ddca70 | 2004-12-07 18:53:52 +0000 | [diff] [blame] | 123 | zlog_debug ("zclient stopped"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 124 | |
| 125 | /* Stop threads. */ |
ajs | 634f9ea | 2005-04-11 15:51:40 +0000 | [diff] [blame] | 126 | THREAD_OFF(zclient->t_read); |
| 127 | THREAD_OFF(zclient->t_connect); |
| 128 | THREAD_OFF(zclient->t_write); |
| 129 | |
| 130 | /* Reset streams. */ |
| 131 | stream_reset(zclient->ibuf); |
| 132 | stream_reset(zclient->obuf); |
| 133 | |
| 134 | /* Empty the write buffer. */ |
| 135 | buffer_reset(zclient->wb); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 136 | |
| 137 | /* Close socket. */ |
| 138 | if (zclient->sock >= 0) |
| 139 | { |
| 140 | close (zclient->sock); |
| 141 | zclient->sock = -1; |
| 142 | } |
| 143 | zclient->fail = 0; |
| 144 | } |
| 145 | |
| 146 | void |
| 147 | zclient_reset (struct zclient *zclient) |
| 148 | { |
| 149 | zclient_stop (zclient); |
| 150 | zclient_init (zclient, zclient->redist_default); |
| 151 | } |
| 152 | |
| 153 | /* Make socket to zebra daemon. Return zebra socket. */ |
| 154 | int |
ajs | 634f9ea | 2005-04-11 15:51:40 +0000 | [diff] [blame] | 155 | zclient_socket(void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 156 | { |
| 157 | int sock; |
| 158 | int ret; |
| 159 | struct sockaddr_in serv; |
| 160 | |
| 161 | /* We should think about IPv6 connection. */ |
| 162 | sock = socket (AF_INET, SOCK_STREAM, 0); |
| 163 | if (sock < 0) |
| 164 | return -1; |
| 165 | |
| 166 | /* Make server socket. */ |
| 167 | memset (&serv, 0, sizeof (struct sockaddr_in)); |
| 168 | serv.sin_family = AF_INET; |
| 169 | serv.sin_port = htons (ZEBRA_PORT); |
| 170 | #ifdef HAVE_SIN_LEN |
| 171 | serv.sin_len = sizeof (struct sockaddr_in); |
| 172 | #endif /* HAVE_SIN_LEN */ |
| 173 | serv.sin_addr.s_addr = htonl (INADDR_LOOPBACK); |
| 174 | |
| 175 | /* Connect to zebra. */ |
| 176 | ret = connect (sock, (struct sockaddr *) &serv, sizeof (serv)); |
| 177 | if (ret < 0) |
| 178 | { |
| 179 | close (sock); |
| 180 | return -1; |
| 181 | } |
| 182 | return sock; |
| 183 | } |
| 184 | |
| 185 | /* For sockaddr_un. */ |
| 186 | #include <sys/un.h> |
| 187 | |
| 188 | int |
hasso | 8c328f1 | 2004-10-05 21:01:23 +0000 | [diff] [blame] | 189 | zclient_socket_un (const char *path) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 190 | { |
| 191 | int ret; |
| 192 | int sock, len; |
| 193 | struct sockaddr_un addr; |
| 194 | |
| 195 | sock = socket (AF_UNIX, SOCK_STREAM, 0); |
| 196 | if (sock < 0) |
| 197 | return -1; |
| 198 | |
| 199 | /* Make server socket. */ |
| 200 | memset (&addr, 0, sizeof (struct sockaddr_un)); |
| 201 | addr.sun_family = AF_UNIX; |
| 202 | strncpy (addr.sun_path, path, strlen (path)); |
| 203 | #ifdef HAVE_SUN_LEN |
| 204 | len = addr.sun_len = SUN_LEN(&addr); |
| 205 | #else |
| 206 | len = sizeof (addr.sun_family) + strlen (addr.sun_path); |
| 207 | #endif /* HAVE_SUN_LEN */ |
| 208 | |
| 209 | ret = connect (sock, (struct sockaddr *) &addr, len); |
| 210 | if (ret < 0) |
| 211 | { |
| 212 | close (sock); |
| 213 | return -1; |
| 214 | } |
| 215 | return sock; |
| 216 | } |
| 217 | |
ajs | 634f9ea | 2005-04-11 15:51:40 +0000 | [diff] [blame] | 218 | static int |
| 219 | zclient_failed(struct zclient *zclient) |
| 220 | { |
| 221 | zclient->fail++; |
| 222 | zclient_stop(zclient); |
| 223 | zclient_event(ZCLIENT_CONNECT, zclient); |
| 224 | return -1; |
| 225 | } |
| 226 | |
| 227 | static int |
| 228 | zclient_flush_data(struct thread *thread) |
| 229 | { |
| 230 | struct zclient *zclient = THREAD_ARG(thread); |
| 231 | |
| 232 | zclient->t_write = NULL; |
| 233 | if (zclient->sock < 0) |
| 234 | return -1; |
| 235 | switch (buffer_flush_available(zclient->wb, zclient->sock)) |
| 236 | { |
| 237 | case BUFFER_ERROR: |
| 238 | zlog_warn("%s: buffer_flush_available failed on zclient fd %d, closing", |
| 239 | __func__, zclient->sock); |
| 240 | return zclient_failed(zclient); |
| 241 | break; |
| 242 | case BUFFER_PENDING: |
| 243 | zclient->t_write = thread_add_write(master, zclient_flush_data, |
| 244 | zclient, zclient->sock); |
| 245 | break; |
| 246 | case BUFFER_EMPTY: |
| 247 | break; |
| 248 | } |
| 249 | return 0; |
| 250 | } |
| 251 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 252 | int |
ajs | 634f9ea | 2005-04-11 15:51:40 +0000 | [diff] [blame] | 253 | zclient_send_message(struct zclient *zclient) |
| 254 | { |
| 255 | if (zclient->sock < 0) |
| 256 | return -1; |
| 257 | switch (buffer_write(zclient->wb, zclient->sock, STREAM_DATA(zclient->obuf), |
| 258 | stream_get_endp(zclient->obuf))) |
| 259 | { |
| 260 | case BUFFER_ERROR: |
| 261 | zlog_warn("%s: buffer_write failed to zclient fd %d, closing", |
| 262 | __func__, zclient->sock); |
| 263 | return zclient_failed(zclient); |
| 264 | break; |
| 265 | case BUFFER_EMPTY: |
| 266 | THREAD_OFF(zclient->t_write); |
| 267 | break; |
| 268 | case BUFFER_PENDING: |
| 269 | THREAD_WRITE_ON(master, zclient->t_write, |
| 270 | zclient_flush_data, zclient, zclient->sock); |
| 271 | break; |
| 272 | } |
| 273 | return 0; |
| 274 | } |
| 275 | |
| 276 | /* Send simple Zebra message. */ |
| 277 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 278 | zebra_message_send (struct zclient *zclient, int command) |
| 279 | { |
| 280 | struct stream *s; |
| 281 | |
| 282 | /* Get zclient output buffer. */ |
| 283 | s = zclient->obuf; |
| 284 | stream_reset (s); |
| 285 | |
| 286 | /* Send very simple command only Zebra message. */ |
| 287 | stream_putw (s, 3); |
| 288 | stream_putc (s, command); |
| 289 | |
ajs | 634f9ea | 2005-04-11 15:51:40 +0000 | [diff] [blame] | 290 | return zclient_send_message(zclient); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | /* Make connection to zebra daemon. */ |
| 294 | int |
| 295 | zclient_start (struct zclient *zclient) |
| 296 | { |
| 297 | int i; |
| 298 | |
| 299 | if (zclient_debug) |
ajs | 8ddca70 | 2004-12-07 18:53:52 +0000 | [diff] [blame] | 300 | zlog_debug ("zclient_start is called"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 301 | |
| 302 | /* zclient is disabled. */ |
| 303 | if (! zclient->enable) |
| 304 | return 0; |
| 305 | |
| 306 | /* If already connected to the zebra. */ |
| 307 | if (zclient->sock >= 0) |
| 308 | return 0; |
| 309 | |
| 310 | /* Check connect thread. */ |
| 311 | if (zclient->t_connect) |
| 312 | return 0; |
| 313 | |
| 314 | /* Make socket. */ |
| 315 | #ifdef HAVE_TCP_ZEBRA |
| 316 | zclient->sock = zclient_socket (); |
| 317 | #else |
| 318 | zclient->sock = zclient_socket_un (ZEBRA_SERV_PATH); |
| 319 | #endif /* HAVE_TCP_ZEBRA */ |
| 320 | if (zclient->sock < 0) |
| 321 | { |
| 322 | if (zclient_debug) |
ajs | 8ddca70 | 2004-12-07 18:53:52 +0000 | [diff] [blame] | 323 | zlog_debug ("zclient connection fail"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 324 | zclient->fail++; |
| 325 | zclient_event (ZCLIENT_CONNECT, zclient); |
| 326 | return -1; |
| 327 | } |
| 328 | |
ajs | 634f9ea | 2005-04-11 15:51:40 +0000 | [diff] [blame] | 329 | if (set_nonblocking(zclient->sock) < 0) |
| 330 | zlog_warn("%s: set_nonblocking(%d) failed", __func__, zclient->sock); |
| 331 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 332 | /* Clear fail count. */ |
| 333 | zclient->fail = 0; |
| 334 | if (zclient_debug) |
ajs | 8ddca70 | 2004-12-07 18:53:52 +0000 | [diff] [blame] | 335 | zlog_debug ("zclient connect success with socket [%d]", zclient->sock); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 336 | |
| 337 | /* Create read thread. */ |
| 338 | zclient_event (ZCLIENT_READ, zclient); |
| 339 | |
| 340 | /* We need interface information. */ |
| 341 | zebra_message_send (zclient, ZEBRA_INTERFACE_ADD); |
| 342 | |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 343 | /* We need router-id information. */ |
| 344 | zebra_message_send (zclient, ZEBRA_ROUTER_ID_ADD); |
| 345 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 346 | /* Flush all redistribute request. */ |
| 347 | for (i = 0; i < ZEBRA_ROUTE_MAX; i++) |
| 348 | if (i != zclient->redist_default && zclient->redist[i]) |
ajs | 634f9ea | 2005-04-11 15:51:40 +0000 | [diff] [blame] | 349 | zebra_redistribute_send (ZEBRA_REDISTRIBUTE_ADD, zclient, i); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 350 | |
| 351 | /* If default information is needed. */ |
| 352 | if (zclient->default_information) |
| 353 | zebra_message_send (zclient, ZEBRA_REDISTRIBUTE_DEFAULT_ADD); |
| 354 | |
| 355 | return 0; |
| 356 | } |
| 357 | |
| 358 | /* This function is a wrapper function for calling zclient_start from |
| 359 | timer or event thread. */ |
ajs | 634f9ea | 2005-04-11 15:51:40 +0000 | [diff] [blame] | 360 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 361 | zclient_connect (struct thread *t) |
| 362 | { |
| 363 | struct zclient *zclient; |
| 364 | |
| 365 | zclient = THREAD_ARG (t); |
| 366 | zclient->t_connect = NULL; |
| 367 | |
| 368 | if (zclient_debug) |
ajs | 8ddca70 | 2004-12-07 18:53:52 +0000 | [diff] [blame] | 369 | zlog_debug ("zclient_connect is called"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 370 | |
| 371 | return zclient_start (zclient); |
| 372 | } |
| 373 | |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 374 | /* |
| 375 | * "xdr_encode"-like interface that allows daemon (client) to send |
| 376 | * a message to zebra server for a route that needs to be |
| 377 | * added/deleted to the kernel. Info about the route is specified |
| 378 | * by the caller in a struct zapi_ipv4. zapi_ipv4_read() then writes |
| 379 | * the info down the zclient socket using the stream_* functions. |
| 380 | * |
| 381 | * The corresponding read ("xdr_decode") function on the server |
| 382 | * side is zread_ipv4_add()/zread_ipv4_delete(). |
| 383 | * |
| 384 | * 0 1 2 3 4 5 6 7 8 9 A B C D E F 0 1 2 3 4 5 6 7 8 9 A B C D E F |
| 385 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 386 | * | Length (2) | Command | Route Type | |
| 387 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 388 | * | ZEBRA Flags | Message Flags | Prefix length | |
| 389 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 390 | * | Destination IPv4 Prefix for route | |
| 391 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 392 | * | Nexthop count | |
| 393 | * +-+-+-+-+-+-+-+-+ |
| 394 | * |
| 395 | * |
| 396 | * A number of IPv4 nexthop(s) or nexthop interface index(es) are then |
| 397 | * described, as per the Nexthop count. Each nexthop described as: |
| 398 | * |
| 399 | * +-+-+-+-+-+-+-+-+ |
| 400 | * | Nexthop Type | Set to one of ZEBRA_NEXTHOP_* |
| 401 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 402 | * | IPv4 Nexthop address or Interface Index number | |
| 403 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 404 | * |
| 405 | * Alternatively, if the flags field has ZEBRA_FLAG_BLACKHOLE or |
| 406 | * ZEBRA_FLAG_REJECT is set then Nexthop count is set to 1, then _no_ |
| 407 | * nexthop information is provided, and the message describes a prefix |
| 408 | * to blackhole or reject route. |
| 409 | * |
| 410 | * If ZAPI_MESSAGE_DISTANCE is set, the distance value is written as a 1 |
| 411 | * byte value. |
| 412 | * |
| 413 | * If ZAPI_MESSAGE_METRIC is set, the metric value is written as an 8 |
| 414 | * byte value. |
| 415 | * |
| 416 | * XXX: No attention paid to alignment. |
| 417 | */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 418 | int |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 419 | zapi_ipv4_route (u_char cmd, struct zclient *zclient, struct prefix_ipv4 *p, |
| 420 | struct zapi_ipv4 *api) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 421 | { |
| 422 | int i; |
| 423 | int psize; |
| 424 | struct stream *s; |
| 425 | |
| 426 | /* Reset stream. */ |
| 427 | s = zclient->obuf; |
| 428 | stream_reset (s); |
| 429 | |
| 430 | /* Length place holder. */ |
| 431 | stream_putw (s, 0); |
| 432 | |
| 433 | /* Put command, type and nexthop. */ |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 434 | stream_putc (s, cmd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 435 | stream_putc (s, api->type); |
| 436 | stream_putc (s, api->flags); |
| 437 | stream_putc (s, api->message); |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 438 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 439 | /* Put prefix information. */ |
| 440 | psize = PSIZE (p->prefixlen); |
| 441 | stream_putc (s, p->prefixlen); |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 442 | stream_write (s, (u_char *) & p->prefix, psize); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 443 | |
| 444 | /* Nexthop, ifindex, distance and metric information. */ |
| 445 | if (CHECK_FLAG (api->message, ZAPI_MESSAGE_NEXTHOP)) |
| 446 | { |
paul | 595db7f | 2003-05-25 21:35:06 +0000 | [diff] [blame] | 447 | if (CHECK_FLAG (api->flags, ZEBRA_FLAG_BLACKHOLE)) |
| 448 | { |
| 449 | stream_putc (s, 1); |
| 450 | stream_putc (s, ZEBRA_NEXTHOP_BLACKHOLE); |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 451 | /* XXX assert(api->nexthop_num == 0); */ |
| 452 | /* XXX assert(api->ifindex_num == 0); */ |
paul | 595db7f | 2003-05-25 21:35:06 +0000 | [diff] [blame] | 453 | } |
| 454 | else |
| 455 | stream_putc (s, api->nexthop_num + api->ifindex_num); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 456 | |
| 457 | for (i = 0; i < api->nexthop_num; i++) |
paul | 595db7f | 2003-05-25 21:35:06 +0000 | [diff] [blame] | 458 | { |
| 459 | stream_putc (s, ZEBRA_NEXTHOP_IPV4); |
| 460 | stream_put_in_addr (s, api->nexthop[i]); |
| 461 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 462 | for (i = 0; i < api->ifindex_num; i++) |
paul | 595db7f | 2003-05-25 21:35:06 +0000 | [diff] [blame] | 463 | { |
| 464 | stream_putc (s, ZEBRA_NEXTHOP_IFINDEX); |
| 465 | stream_putl (s, api->ifindex[i]); |
| 466 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | if (CHECK_FLAG (api->message, ZAPI_MESSAGE_DISTANCE)) |
| 470 | stream_putc (s, api->distance); |
| 471 | if (CHECK_FLAG (api->message, ZAPI_MESSAGE_METRIC)) |
| 472 | stream_putl (s, api->metric); |
| 473 | |
| 474 | /* Put length at the first point of the stream. */ |
| 475 | stream_putw_at (s, 0, stream_get_endp (s)); |
| 476 | |
ajs | 634f9ea | 2005-04-11 15:51:40 +0000 | [diff] [blame] | 477 | return zclient_send_message(zclient); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | #ifdef HAVE_IPV6 |
| 481 | int |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 482 | zapi_ipv6_route (u_char cmd, struct zclient *zclient, struct prefix_ipv6 *p, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 483 | struct zapi_ipv6 *api) |
| 484 | { |
| 485 | int i; |
| 486 | int psize; |
| 487 | struct stream *s; |
| 488 | |
| 489 | /* Reset stream. */ |
| 490 | s = zclient->obuf; |
| 491 | stream_reset (s); |
| 492 | |
| 493 | /* Length place holder. */ |
| 494 | stream_putw (s, 0); |
| 495 | |
| 496 | /* Put command, type and nexthop. */ |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 497 | stream_putc (s, cmd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 498 | stream_putc (s, api->type); |
| 499 | stream_putc (s, api->flags); |
| 500 | stream_putc (s, api->message); |
| 501 | |
| 502 | /* Put prefix information. */ |
| 503 | psize = PSIZE (p->prefixlen); |
| 504 | stream_putc (s, p->prefixlen); |
| 505 | stream_write (s, (u_char *)&p->prefix, psize); |
| 506 | |
| 507 | /* Nexthop, ifindex, distance and metric information. */ |
| 508 | if (CHECK_FLAG (api->message, ZAPI_MESSAGE_NEXTHOP)) |
| 509 | { |
| 510 | stream_putc (s, api->nexthop_num + api->ifindex_num); |
| 511 | |
| 512 | for (i = 0; i < api->nexthop_num; i++) |
| 513 | { |
| 514 | stream_putc (s, ZEBRA_NEXTHOP_IPV6); |
| 515 | stream_write (s, (u_char *)api->nexthop[i], 16); |
| 516 | } |
| 517 | for (i = 0; i < api->ifindex_num; i++) |
| 518 | { |
| 519 | stream_putc (s, ZEBRA_NEXTHOP_IFINDEX); |
| 520 | stream_putl (s, api->ifindex[i]); |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | if (CHECK_FLAG (api->message, ZAPI_MESSAGE_DISTANCE)) |
| 525 | stream_putc (s, api->distance); |
| 526 | if (CHECK_FLAG (api->message, ZAPI_MESSAGE_METRIC)) |
| 527 | stream_putl (s, api->metric); |
| 528 | |
| 529 | /* Put length at the first point of the stream. */ |
| 530 | stream_putw_at (s, 0, stream_get_endp (s)); |
| 531 | |
ajs | 634f9ea | 2005-04-11 15:51:40 +0000 | [diff] [blame] | 532 | return zclient_send_message(zclient); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 533 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 534 | #endif /* HAVE_IPV6 */ |
| 535 | |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 536 | /* |
| 537 | * send a ZEBRA_REDISTRIBUTE_ADD or ZEBRA_REDISTRIBUTE_DELETE |
| 538 | * for the route type (ZEBRA_ROUTE_KERNEL etc.). The zebra server will |
| 539 | * then set/unset redist[type] in the client handle (a struct zserv) for the |
| 540 | * sending client |
| 541 | */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 542 | int |
ajs | 634f9ea | 2005-04-11 15:51:40 +0000 | [diff] [blame] | 543 | zebra_redistribute_send (int command, struct zclient *zclient, int type) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 544 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 545 | struct stream *s; |
| 546 | |
ajs | 634f9ea | 2005-04-11 15:51:40 +0000 | [diff] [blame] | 547 | s = zclient->obuf; |
| 548 | stream_reset(s); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 549 | |
ajs | 634f9ea | 2005-04-11 15:51:40 +0000 | [diff] [blame] | 550 | /* Total length of the message. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 551 | stream_putw (s, 4); |
| 552 | |
| 553 | stream_putc (s, command); |
| 554 | stream_putc (s, type); |
| 555 | |
ajs | 634f9ea | 2005-04-11 15:51:40 +0000 | [diff] [blame] | 556 | return zclient_send_message(zclient); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 557 | } |
| 558 | |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 559 | /* Router-id update from zebra daemon. */ |
| 560 | void |
| 561 | zebra_router_id_update_read (struct stream *s, struct prefix *rid) |
| 562 | { |
| 563 | int plen; |
| 564 | |
| 565 | /* Fetch interface address. */ |
| 566 | rid->family = stream_getc (s); |
| 567 | |
| 568 | plen = prefix_blen (rid); |
| 569 | stream_get (&rid->u.prefix, s, plen); |
| 570 | rid->prefixlen = stream_getc (s); |
| 571 | } |
| 572 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 573 | /* Interface addition from zebra daemon. */ |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 574 | /* |
| 575 | * The format of the message sent with type ZEBRA_INTERFACE_ADD or |
| 576 | * ZEBRA_INTERFACE_DELETE from zebra to the client is: |
| 577 | * 0 1 2 3 |
| 578 | * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 |
| 579 | * +-+-+-+-+-+-+-+-+ |
| 580 | * | type | |
| 581 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 582 | * | ifname | |
| 583 | * | | |
| 584 | * | | |
| 585 | * | | |
| 586 | * | | |
| 587 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 588 | * | ifindex | |
| 589 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 590 | * | if_flags | |
| 591 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 592 | * | metric | |
| 593 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 594 | * | ifmtu | |
| 595 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 596 | * | ifmtu6 | |
| 597 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 598 | * | bandwidth | |
| 599 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 600 | * | sockaddr_dl | |
| 601 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 602 | */ |
| 603 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 604 | struct interface * |
| 605 | zebra_interface_add_read (struct stream *s) |
| 606 | { |
| 607 | struct interface *ifp; |
paul | 02ff83c | 2004-06-11 11:27:03 +0000 | [diff] [blame] | 608 | char ifname_tmp[INTERFACE_NAMSIZ]; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 609 | |
| 610 | /* Read interface name. */ |
| 611 | stream_get (ifname_tmp, s, INTERFACE_NAMSIZ); |
| 612 | |
ajs | a349198 | 2005-04-02 22:50:38 +0000 | [diff] [blame] | 613 | /* Lookup/create interface by name. */ |
| 614 | ifp = if_get_by_name_len (ifname_tmp, strnlen(ifname_tmp, INTERFACE_NAMSIZ)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 615 | |
| 616 | /* Read interface's index. */ |
| 617 | ifp->ifindex = stream_getl (s); |
| 618 | |
| 619 | /* Read interface's value. */ |
paul | 2e3b2e4 | 2002-12-13 21:03:13 +0000 | [diff] [blame] | 620 | ifp->status = stream_getc (s); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 621 | ifp->flags = stream_getl (s); |
| 622 | ifp->metric = stream_getl (s); |
| 623 | ifp->mtu = stream_getl (s); |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 624 | ifp->mtu6 = stream_getl (s); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 625 | ifp->bandwidth = stream_getl (s); |
| 626 | #ifdef HAVE_SOCKADDR_DL |
| 627 | stream_get (&ifp->sdl, s, sizeof (ifp->sdl)); |
| 628 | #else |
| 629 | ifp->hw_addr_len = stream_getl (s); |
| 630 | if (ifp->hw_addr_len) |
| 631 | stream_get (ifp->hw_addr, s, ifp->hw_addr_len); |
| 632 | #endif /* HAVE_SOCKADDR_DL */ |
| 633 | |
| 634 | return ifp; |
| 635 | } |
| 636 | |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 637 | /* |
| 638 | * Read interface up/down msg (ZEBRA_INTERFACE_UP/ZEBRA_INTERFACE_DOWN) |
| 639 | * from zebra server. The format of this message is the same as |
| 640 | * that sent for ZEBRA_INTERFACE_ADD/ZEBRA_INTERFACE_DELETE (see |
| 641 | * comments for zebra_interface_add_read), except that no sockaddr_dl |
| 642 | * is sent at the tail of the message. |
| 643 | */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 644 | struct interface * |
| 645 | zebra_interface_state_read (struct stream *s) |
| 646 | { |
| 647 | struct interface *ifp; |
paul | 02ff83c | 2004-06-11 11:27:03 +0000 | [diff] [blame] | 648 | char ifname_tmp[INTERFACE_NAMSIZ]; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 649 | |
| 650 | /* Read interface name. */ |
| 651 | stream_get (ifname_tmp, s, INTERFACE_NAMSIZ); |
| 652 | |
| 653 | /* Lookup this by interface index. */ |
ajs | a349198 | 2005-04-02 22:50:38 +0000 | [diff] [blame] | 654 | ifp = if_lookup_by_name_len (ifname_tmp, |
| 655 | strnlen(ifname_tmp, INTERFACE_NAMSIZ)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 656 | |
| 657 | /* If such interface does not exist, indicate an error */ |
| 658 | if (! ifp) |
| 659 | return NULL; |
| 660 | |
| 661 | /* Read interface's index. */ |
| 662 | ifp->ifindex = stream_getl (s); |
| 663 | |
| 664 | /* Read interface's value. */ |
paul | 2e3b2e4 | 2002-12-13 21:03:13 +0000 | [diff] [blame] | 665 | ifp->status = stream_getc (s); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 666 | ifp->flags = stream_getl (s); |
| 667 | ifp->metric = stream_getl (s); |
| 668 | ifp->mtu = stream_getl (s); |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 669 | ifp->mtu6 = stream_getl (s); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 670 | ifp->bandwidth = stream_getl (s); |
| 671 | |
| 672 | return ifp; |
| 673 | } |
| 674 | |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 675 | /* |
| 676 | * format of message for address additon is: |
| 677 | * 0 |
| 678 | * 0 1 2 3 4 5 6 7 |
| 679 | * +-+-+-+-+-+-+-+-+ |
| 680 | * | type | ZEBRA_INTERFACE_ADDRESS_ADD or |
| 681 | * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_ADDRES_DELETE |
| 682 | * | | |
| 683 | * + + |
| 684 | * | ifindex | |
| 685 | * + + |
| 686 | * | | |
| 687 | * + + |
| 688 | * | | |
| 689 | * +-+-+-+-+-+-+-+-+ |
| 690 | * | ifc_flags | flags for connected address |
| 691 | * +-+-+-+-+-+-+-+-+ |
| 692 | * | addr_family | |
| 693 | * +-+-+-+-+-+-+-+-+ |
| 694 | * | addr... | |
| 695 | * : : |
| 696 | * | | |
| 697 | * +-+-+-+-+-+-+-+-+ |
| 698 | * | addr_len | len of addr. E.g., addr_len = 4 for ipv4 addrs. |
| 699 | * +-+-+-+-+-+-+-+-+ |
| 700 | * | daddr.. | |
| 701 | * : : |
| 702 | * | | |
| 703 | * +-+-+-+-+-+-+-+-+ |
| 704 | * |
| 705 | */ |
| 706 | |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 707 | void |
| 708 | zebra_interface_if_set_value (struct stream *s, struct interface *ifp) |
| 709 | { |
| 710 | /* Read interface's index. */ |
| 711 | ifp->ifindex = stream_getl (s); |
hasso | 508ec91 | 2004-10-23 14:26:49 +0000 | [diff] [blame] | 712 | ifp->status = stream_getc (s); |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 713 | |
| 714 | /* Read interface's value. */ |
| 715 | ifp->flags = stream_getl (s); |
| 716 | ifp->metric = stream_getl (s); |
| 717 | ifp->mtu = stream_getl (s); |
hasso | 508ec91 | 2004-10-23 14:26:49 +0000 | [diff] [blame] | 718 | ifp->mtu6 = stream_getl (s); |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 719 | ifp->bandwidth = stream_getl (s); |
| 720 | } |
| 721 | |
hasso | 3fb9cd6 | 2004-10-19 19:44:43 +0000 | [diff] [blame] | 722 | static int |
| 723 | memconstant(const void *s, int c, size_t n) |
| 724 | { |
| 725 | const u_char *p = s; |
| 726 | |
| 727 | while (n-- > 0) |
| 728 | if (*p++ != c) |
| 729 | return 0; |
| 730 | return 1; |
| 731 | } |
| 732 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 733 | struct connected * |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 734 | zebra_interface_address_read (int type, struct stream *s) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 735 | { |
| 736 | unsigned int ifindex; |
| 737 | struct interface *ifp; |
| 738 | struct connected *ifc; |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 739 | struct prefix p, d; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 740 | int family; |
| 741 | int plen; |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 742 | u_char ifc_flags; |
| 743 | |
| 744 | memset (&p, 0, sizeof(p)); |
| 745 | memset (&d, 0, sizeof(d)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 746 | |
| 747 | /* Get interface index. */ |
| 748 | ifindex = stream_getl (s); |
| 749 | |
| 750 | /* Lookup index. */ |
| 751 | ifp = if_lookup_by_index (ifindex); |
| 752 | if (ifp == NULL) |
| 753 | { |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 754 | zlog_warn ("zebra_interface_address_read(%s): " |
| 755 | "Can't find interface by ifindex: %d ", |
| 756 | (type == ZEBRA_INTERFACE_ADDRESS_ADD? "ADD" : "DELETE"), |
| 757 | ifindex); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 758 | return NULL; |
| 759 | } |
| 760 | |
| 761 | /* Fetch flag. */ |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 762 | ifc_flags = stream_getc (s); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 763 | |
| 764 | /* Fetch interface address. */ |
| 765 | family = p.family = stream_getc (s); |
| 766 | |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 767 | plen = prefix_blen (&p); |
| 768 | stream_get (&p.u.prefix, s, plen); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 769 | p.prefixlen = stream_getc (s); |
| 770 | |
| 771 | /* Fetch destination address. */ |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 772 | stream_get (&d.u.prefix, s, plen); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 773 | d.family = family; |
| 774 | |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 775 | if (type == ZEBRA_INTERFACE_ADDRESS_ADD) |
| 776 | { |
hasso | 3fb9cd6 | 2004-10-19 19:44:43 +0000 | [diff] [blame] | 777 | /* N.B. NULL destination pointers are encoded as all zeroes */ |
| 778 | ifc = connected_add_by_prefix(ifp, &p,(memconstant(&d.u.prefix,0,plen) ? |
| 779 | NULL : &d)); |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 780 | if (ifc != NULL) |
| 781 | ifc->flags = ifc_flags; |
| 782 | } |
| 783 | else |
| 784 | { |
| 785 | assert (type == ZEBRA_INTERFACE_ADDRESS_DELETE); |
| 786 | ifc = connected_delete_by_prefix(ifp, &p); |
| 787 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 788 | |
| 789 | return ifc; |
| 790 | } |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 791 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 792 | |
| 793 | /* Zebra client message read function. */ |
ajs | 634f9ea | 2005-04-11 15:51:40 +0000 | [diff] [blame] | 794 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 795 | zclient_read (struct thread *thread) |
| 796 | { |
| 797 | int ret; |
ajs | 634f9ea | 2005-04-11 15:51:40 +0000 | [diff] [blame] | 798 | size_t already; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 799 | zebra_size_t length; |
| 800 | zebra_command_t command; |
| 801 | struct zclient *zclient; |
| 802 | |
| 803 | /* Get socket to zebra. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 804 | zclient = THREAD_ARG (thread); |
| 805 | zclient->t_read = NULL; |
| 806 | |
ajs | 634f9ea | 2005-04-11 15:51:40 +0000 | [diff] [blame] | 807 | /* Read zebra header (if we don't have it already). */ |
| 808 | if ((already = stream_get_endp(zclient->ibuf)) < ZEBRA_HEADER_SIZE) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 809 | { |
ajs | 634f9ea | 2005-04-11 15:51:40 +0000 | [diff] [blame] | 810 | ssize_t nbyte; |
| 811 | if (((nbyte = stream_read_try(zclient->ibuf, zclient->sock, |
| 812 | ZEBRA_HEADER_SIZE-already)) == 0) || |
| 813 | (nbyte == -1)) |
| 814 | { |
| 815 | if (zclient_debug) |
| 816 | zlog_debug ("zclient connection closed socket [%d].", zclient->sock); |
| 817 | return zclient_failed(zclient); |
| 818 | } |
| 819 | if (nbyte != (ssize_t)(ZEBRA_HEADER_SIZE-already)) |
| 820 | { |
| 821 | /* Try again later. */ |
| 822 | zclient_event (ZCLIENT_READ, zclient); |
| 823 | return 0; |
| 824 | } |
| 825 | already = ZEBRA_HEADER_SIZE; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 826 | } |
| 827 | |
ajs | 634f9ea | 2005-04-11 15:51:40 +0000 | [diff] [blame] | 828 | /* Reset to read from the beginning of the incoming packet. */ |
| 829 | stream_set_getp(zclient->ibuf, 0); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 830 | |
| 831 | /* Fetch length and command. */ |
| 832 | length = stream_getw (zclient->ibuf); |
| 833 | command = stream_getc (zclient->ibuf); |
| 834 | |
ajs | 634f9ea | 2005-04-11 15:51:40 +0000 | [diff] [blame] | 835 | if (length < ZEBRA_HEADER_SIZE) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 836 | { |
ajs | 634f9ea | 2005-04-11 15:51:40 +0000 | [diff] [blame] | 837 | zlog_err("%s: socket %d message length %u is less than %d ", |
| 838 | __func__, zclient->sock, length, ZEBRA_HEADER_SIZE); |
| 839 | return zclient_failed(zclient); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 840 | } |
ajs | 634f9ea | 2005-04-11 15:51:40 +0000 | [diff] [blame] | 841 | |
| 842 | /* Length check. */ |
| 843 | if (length > STREAM_SIZE(zclient->ibuf)) |
| 844 | { |
| 845 | struct stream *ns; |
| 846 | zlog_warn("%s: message size %u exceeds buffer size %lu, expanding...", |
| 847 | __func__, length, (u_long)STREAM_SIZE(zclient->ibuf)); |
| 848 | ns = stream_new(length); |
| 849 | stream_copy(ns, zclient->ibuf); |
| 850 | stream_free (zclient->ibuf); |
| 851 | zclient->ibuf = ns; |
| 852 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 853 | |
| 854 | /* Read rest of zebra packet. */ |
ajs | 634f9ea | 2005-04-11 15:51:40 +0000 | [diff] [blame] | 855 | if (already < length) |
| 856 | { |
| 857 | ssize_t nbyte; |
| 858 | if (((nbyte = stream_read_try(zclient->ibuf, zclient->sock, |
| 859 | length-already)) == 0) || |
| 860 | (nbyte == -1)) |
| 861 | { |
| 862 | if (zclient_debug) |
| 863 | zlog_debug("zclient connection closed socket [%d].", zclient->sock); |
| 864 | return zclient_failed(zclient); |
| 865 | } |
| 866 | if (nbyte != (ssize_t)(length-already)) |
| 867 | { |
| 868 | /* Try again later. */ |
| 869 | zclient_event (ZCLIENT_READ, zclient); |
| 870 | return 0; |
| 871 | } |
| 872 | } |
| 873 | |
| 874 | length -= ZEBRA_HEADER_SIZE; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 875 | |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 876 | if (zclient_debug) |
ajs | 8ddca70 | 2004-12-07 18:53:52 +0000 | [diff] [blame] | 877 | zlog_debug("zclient 0x%p command 0x%x \n", zclient, command); |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 878 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 879 | switch (command) |
| 880 | { |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 881 | case ZEBRA_ROUTER_ID_UPDATE: |
| 882 | if (zclient->router_id_update) |
| 883 | ret = (*zclient->router_id_update) (command, zclient, length); |
| 884 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 885 | case ZEBRA_INTERFACE_ADD: |
| 886 | if (zclient->interface_add) |
| 887 | ret = (*zclient->interface_add) (command, zclient, length); |
| 888 | break; |
| 889 | case ZEBRA_INTERFACE_DELETE: |
| 890 | if (zclient->interface_delete) |
| 891 | ret = (*zclient->interface_delete) (command, zclient, length); |
| 892 | break; |
| 893 | case ZEBRA_INTERFACE_ADDRESS_ADD: |
| 894 | if (zclient->interface_address_add) |
| 895 | ret = (*zclient->interface_address_add) (command, zclient, length); |
| 896 | break; |
| 897 | case ZEBRA_INTERFACE_ADDRESS_DELETE: |
| 898 | if (zclient->interface_address_delete) |
| 899 | ret = (*zclient->interface_address_delete) (command, zclient, length); |
| 900 | break; |
| 901 | case ZEBRA_INTERFACE_UP: |
| 902 | if (zclient->interface_up) |
| 903 | ret = (*zclient->interface_up) (command, zclient, length); |
| 904 | break; |
| 905 | case ZEBRA_INTERFACE_DOWN: |
| 906 | if (zclient->interface_down) |
| 907 | ret = (*zclient->interface_down) (command, zclient, length); |
| 908 | break; |
| 909 | case ZEBRA_IPV4_ROUTE_ADD: |
| 910 | if (zclient->ipv4_route_add) |
| 911 | ret = (*zclient->ipv4_route_add) (command, zclient, length); |
| 912 | break; |
| 913 | case ZEBRA_IPV4_ROUTE_DELETE: |
| 914 | if (zclient->ipv4_route_delete) |
| 915 | ret = (*zclient->ipv4_route_delete) (command, zclient, length); |
| 916 | break; |
| 917 | case ZEBRA_IPV6_ROUTE_ADD: |
| 918 | if (zclient->ipv6_route_add) |
| 919 | ret = (*zclient->ipv6_route_add) (command, zclient, length); |
| 920 | break; |
| 921 | case ZEBRA_IPV6_ROUTE_DELETE: |
| 922 | if (zclient->ipv6_route_delete) |
| 923 | ret = (*zclient->ipv6_route_delete) (command, zclient, length); |
| 924 | break; |
| 925 | default: |
| 926 | break; |
| 927 | } |
| 928 | |
ajs | 634f9ea | 2005-04-11 15:51:40 +0000 | [diff] [blame] | 929 | if (zclient->sock < 0) |
| 930 | /* Connection was closed during packet processing. */ |
| 931 | return -1; |
| 932 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 933 | /* Register read thread. */ |
ajs | 634f9ea | 2005-04-11 15:51:40 +0000 | [diff] [blame] | 934 | stream_reset(zclient->ibuf); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 935 | zclient_event (ZCLIENT_READ, zclient); |
| 936 | |
| 937 | return 0; |
| 938 | } |
| 939 | |
| 940 | void |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 941 | zclient_redistribute (int command, struct zclient *zclient, int type) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 942 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 943 | |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 944 | if (command == ZEBRA_REDISTRIBUTE_ADD) |
| 945 | { |
| 946 | if (zclient->redist[type]) |
| 947 | return; |
| 948 | zclient->redist[type] = 1; |
| 949 | } |
| 950 | else |
| 951 | { |
| 952 | if (!zclient->redist[type]) |
| 953 | return; |
| 954 | zclient->redist[type] = 0; |
| 955 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 956 | |
| 957 | if (zclient->sock > 0) |
ajs | 634f9ea | 2005-04-11 15:51:40 +0000 | [diff] [blame] | 958 | zebra_redistribute_send (command, zclient, type); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 959 | } |
| 960 | |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 961 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 962 | void |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 963 | zclient_redistribute_default (int command, struct zclient *zclient) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 964 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 965 | |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 966 | if (command == ZEBRA_REDISTRIBUTE_DEFAULT_ADD) |
| 967 | { |
| 968 | if (zclient->default_information) |
| 969 | return; |
| 970 | zclient->default_information = 1; |
| 971 | } |
| 972 | else |
| 973 | { |
| 974 | if (!zclient->default_information) |
| 975 | return; |
| 976 | zclient->default_information = 0; |
| 977 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 978 | |
| 979 | if (zclient->sock > 0) |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 980 | zebra_message_send (zclient, command); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 981 | } |
| 982 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 983 | static void |
| 984 | zclient_event (enum event event, struct zclient *zclient) |
| 985 | { |
| 986 | switch (event) |
| 987 | { |
| 988 | case ZCLIENT_SCHEDULE: |
| 989 | if (! zclient->t_connect) |
| 990 | zclient->t_connect = |
| 991 | thread_add_event (master, zclient_connect, zclient, 0); |
| 992 | break; |
| 993 | case ZCLIENT_CONNECT: |
| 994 | if (zclient->fail >= 10) |
| 995 | return; |
| 996 | if (zclient_debug) |
ajs | 8ddca70 | 2004-12-07 18:53:52 +0000 | [diff] [blame] | 997 | zlog_debug ("zclient connect schedule interval is %d", |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 998 | zclient->fail < 3 ? 10 : 60); |
| 999 | if (! zclient->t_connect) |
| 1000 | zclient->t_connect = |
| 1001 | thread_add_timer (master, zclient_connect, zclient, |
| 1002 | zclient->fail < 3 ? 10 : 60); |
| 1003 | break; |
| 1004 | case ZCLIENT_READ: |
| 1005 | zclient->t_read = |
| 1006 | thread_add_read (master, zclient_read, zclient, zclient->sock); |
| 1007 | break; |
| 1008 | } |
| 1009 | } |