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