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 | |
paul | d211086 | 2006-01-17 17:43:18 +0000 | [diff] [blame] | 273 | void |
paul | c1b9800 | 2006-01-16 01:54:02 +0000 | [diff] [blame] | 274 | zclient_create_header (struct stream *s, uint16_t command) |
| 275 | { |
| 276 | /* length placeholder, caller can update */ |
| 277 | stream_putw (s, ZEBRA_HEADER_SIZE); |
| 278 | stream_putc (s, ZEBRA_HEADER_MARKER); |
| 279 | stream_putc (s, ZSERV_VERSION); |
| 280 | stream_putw (s, command); |
| 281 | } |
| 282 | |
ajs | 634f9ea | 2005-04-11 15:51:40 +0000 | [diff] [blame] | 283 | /* Send simple Zebra message. */ |
| 284 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 285 | zebra_message_send (struct zclient *zclient, int command) |
| 286 | { |
| 287 | struct stream *s; |
| 288 | |
| 289 | /* Get zclient output buffer. */ |
| 290 | s = zclient->obuf; |
| 291 | stream_reset (s); |
| 292 | |
| 293 | /* Send very simple command only Zebra message. */ |
paul | c1b9800 | 2006-01-16 01:54:02 +0000 | [diff] [blame] | 294 | zclient_create_header (s, command); |
| 295 | |
ajs | 634f9ea | 2005-04-11 15:51:40 +0000 | [diff] [blame] | 296 | return zclient_send_message(zclient); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | /* Make connection to zebra daemon. */ |
| 300 | int |
| 301 | zclient_start (struct zclient *zclient) |
| 302 | { |
| 303 | int i; |
| 304 | |
| 305 | if (zclient_debug) |
ajs | 8ddca70 | 2004-12-07 18:53:52 +0000 | [diff] [blame] | 306 | zlog_debug ("zclient_start is called"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 307 | |
| 308 | /* zclient is disabled. */ |
| 309 | if (! zclient->enable) |
| 310 | return 0; |
| 311 | |
| 312 | /* If already connected to the zebra. */ |
| 313 | if (zclient->sock >= 0) |
| 314 | return 0; |
| 315 | |
| 316 | /* Check connect thread. */ |
| 317 | if (zclient->t_connect) |
| 318 | return 0; |
| 319 | |
| 320 | /* Make socket. */ |
| 321 | #ifdef HAVE_TCP_ZEBRA |
| 322 | zclient->sock = zclient_socket (); |
| 323 | #else |
| 324 | zclient->sock = zclient_socket_un (ZEBRA_SERV_PATH); |
| 325 | #endif /* HAVE_TCP_ZEBRA */ |
| 326 | if (zclient->sock < 0) |
| 327 | { |
| 328 | if (zclient_debug) |
ajs | 8ddca70 | 2004-12-07 18:53:52 +0000 | [diff] [blame] | 329 | zlog_debug ("zclient connection fail"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 330 | zclient->fail++; |
| 331 | zclient_event (ZCLIENT_CONNECT, zclient); |
| 332 | return -1; |
| 333 | } |
| 334 | |
ajs | 634f9ea | 2005-04-11 15:51:40 +0000 | [diff] [blame] | 335 | if (set_nonblocking(zclient->sock) < 0) |
| 336 | zlog_warn("%s: set_nonblocking(%d) failed", __func__, zclient->sock); |
| 337 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 338 | /* Clear fail count. */ |
| 339 | zclient->fail = 0; |
| 340 | if (zclient_debug) |
ajs | 8ddca70 | 2004-12-07 18:53:52 +0000 | [diff] [blame] | 341 | zlog_debug ("zclient connect success with socket [%d]", zclient->sock); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 342 | |
| 343 | /* Create read thread. */ |
| 344 | zclient_event (ZCLIENT_READ, zclient); |
| 345 | |
| 346 | /* We need interface information. */ |
| 347 | zebra_message_send (zclient, ZEBRA_INTERFACE_ADD); |
| 348 | |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 349 | /* We need router-id information. */ |
| 350 | zebra_message_send (zclient, ZEBRA_ROUTER_ID_ADD); |
| 351 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 352 | /* Flush all redistribute request. */ |
| 353 | for (i = 0; i < ZEBRA_ROUTE_MAX; i++) |
| 354 | if (i != zclient->redist_default && zclient->redist[i]) |
ajs | 634f9ea | 2005-04-11 15:51:40 +0000 | [diff] [blame] | 355 | zebra_redistribute_send (ZEBRA_REDISTRIBUTE_ADD, zclient, i); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 356 | |
| 357 | /* If default information is needed. */ |
| 358 | if (zclient->default_information) |
| 359 | zebra_message_send (zclient, ZEBRA_REDISTRIBUTE_DEFAULT_ADD); |
| 360 | |
| 361 | return 0; |
| 362 | } |
| 363 | |
| 364 | /* This function is a wrapper function for calling zclient_start from |
| 365 | timer or event thread. */ |
ajs | 634f9ea | 2005-04-11 15:51:40 +0000 | [diff] [blame] | 366 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 367 | zclient_connect (struct thread *t) |
| 368 | { |
| 369 | struct zclient *zclient; |
| 370 | |
| 371 | zclient = THREAD_ARG (t); |
| 372 | zclient->t_connect = NULL; |
| 373 | |
| 374 | if (zclient_debug) |
ajs | 8ddca70 | 2004-12-07 18:53:52 +0000 | [diff] [blame] | 375 | zlog_debug ("zclient_connect is called"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 376 | |
| 377 | return zclient_start (zclient); |
| 378 | } |
| 379 | |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 380 | /* |
| 381 | * "xdr_encode"-like interface that allows daemon (client) to send |
| 382 | * a message to zebra server for a route that needs to be |
| 383 | * added/deleted to the kernel. Info about the route is specified |
| 384 | * by the caller in a struct zapi_ipv4. zapi_ipv4_read() then writes |
| 385 | * the info down the zclient socket using the stream_* functions. |
| 386 | * |
| 387 | * The corresponding read ("xdr_decode") function on the server |
| 388 | * side is zread_ipv4_add()/zread_ipv4_delete(). |
| 389 | * |
| 390 | * 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 |
| 391 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 392 | * | Length (2) | Command | Route Type | |
| 393 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 394 | * | ZEBRA Flags | Message Flags | Prefix length | |
| 395 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 396 | * | Destination IPv4 Prefix for route | |
| 397 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 398 | * | Nexthop count | |
| 399 | * +-+-+-+-+-+-+-+-+ |
| 400 | * |
| 401 | * |
| 402 | * A number of IPv4 nexthop(s) or nexthop interface index(es) are then |
| 403 | * described, as per the Nexthop count. Each nexthop described as: |
| 404 | * |
| 405 | * +-+-+-+-+-+-+-+-+ |
| 406 | * | Nexthop Type | Set to one of ZEBRA_NEXTHOP_* |
| 407 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 408 | * | IPv4 Nexthop address or Interface Index number | |
| 409 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 410 | * |
| 411 | * Alternatively, if the flags field has ZEBRA_FLAG_BLACKHOLE or |
| 412 | * ZEBRA_FLAG_REJECT is set then Nexthop count is set to 1, then _no_ |
| 413 | * nexthop information is provided, and the message describes a prefix |
| 414 | * to blackhole or reject route. |
| 415 | * |
| 416 | * If ZAPI_MESSAGE_DISTANCE is set, the distance value is written as a 1 |
| 417 | * byte value. |
| 418 | * |
| 419 | * If ZAPI_MESSAGE_METRIC is set, the metric value is written as an 8 |
| 420 | * byte value. |
| 421 | * |
| 422 | * XXX: No attention paid to alignment. |
| 423 | */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 424 | int |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 425 | zapi_ipv4_route (u_char cmd, struct zclient *zclient, struct prefix_ipv4 *p, |
| 426 | struct zapi_ipv4 *api) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 427 | { |
| 428 | int i; |
| 429 | int psize; |
| 430 | struct stream *s; |
| 431 | |
| 432 | /* Reset stream. */ |
| 433 | s = zclient->obuf; |
| 434 | stream_reset (s); |
paul | c1b9800 | 2006-01-16 01:54:02 +0000 | [diff] [blame] | 435 | |
| 436 | zclient_create_header (s, cmd); |
| 437 | |
| 438 | /* Put type and nexthop. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 439 | stream_putc (s, api->type); |
| 440 | stream_putc (s, api->flags); |
| 441 | stream_putc (s, api->message); |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 442 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 443 | /* Put prefix information. */ |
| 444 | psize = PSIZE (p->prefixlen); |
| 445 | stream_putc (s, p->prefixlen); |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 446 | stream_write (s, (u_char *) & p->prefix, psize); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 447 | |
| 448 | /* Nexthop, ifindex, distance and metric information. */ |
| 449 | if (CHECK_FLAG (api->message, ZAPI_MESSAGE_NEXTHOP)) |
| 450 | { |
paul | 595db7f | 2003-05-25 21:35:06 +0000 | [diff] [blame] | 451 | if (CHECK_FLAG (api->flags, ZEBRA_FLAG_BLACKHOLE)) |
| 452 | { |
| 453 | stream_putc (s, 1); |
| 454 | stream_putc (s, ZEBRA_NEXTHOP_BLACKHOLE); |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 455 | /* XXX assert(api->nexthop_num == 0); */ |
| 456 | /* XXX assert(api->ifindex_num == 0); */ |
paul | 595db7f | 2003-05-25 21:35:06 +0000 | [diff] [blame] | 457 | } |
| 458 | else |
| 459 | stream_putc (s, api->nexthop_num + api->ifindex_num); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 460 | |
| 461 | for (i = 0; i < api->nexthop_num; i++) |
paul | 595db7f | 2003-05-25 21:35:06 +0000 | [diff] [blame] | 462 | { |
| 463 | stream_putc (s, ZEBRA_NEXTHOP_IPV4); |
| 464 | stream_put_in_addr (s, api->nexthop[i]); |
| 465 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 466 | for (i = 0; i < api->ifindex_num; i++) |
paul | 595db7f | 2003-05-25 21:35:06 +0000 | [diff] [blame] | 467 | { |
| 468 | stream_putc (s, ZEBRA_NEXTHOP_IFINDEX); |
| 469 | stream_putl (s, api->ifindex[i]); |
| 470 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | if (CHECK_FLAG (api->message, ZAPI_MESSAGE_DISTANCE)) |
| 474 | stream_putc (s, api->distance); |
| 475 | if (CHECK_FLAG (api->message, ZAPI_MESSAGE_METRIC)) |
| 476 | stream_putl (s, api->metric); |
| 477 | |
| 478 | /* Put length at the first point of the stream. */ |
| 479 | stream_putw_at (s, 0, stream_get_endp (s)); |
| 480 | |
ajs | 634f9ea | 2005-04-11 15:51:40 +0000 | [diff] [blame] | 481 | return zclient_send_message(zclient); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | #ifdef HAVE_IPV6 |
| 485 | int |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 486 | zapi_ipv6_route (u_char cmd, struct zclient *zclient, struct prefix_ipv6 *p, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 487 | struct zapi_ipv6 *api) |
| 488 | { |
| 489 | int i; |
| 490 | int psize; |
| 491 | struct stream *s; |
| 492 | |
| 493 | /* Reset stream. */ |
| 494 | s = zclient->obuf; |
| 495 | stream_reset (s); |
| 496 | |
paul | c1b9800 | 2006-01-16 01:54:02 +0000 | [diff] [blame] | 497 | zclient_create_header (s, cmd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 498 | |
paul | c1b9800 | 2006-01-16 01:54:02 +0000 | [diff] [blame] | 499 | /* Put type and nexthop. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 500 | stream_putc (s, api->type); |
| 501 | stream_putc (s, api->flags); |
| 502 | stream_putc (s, api->message); |
| 503 | |
| 504 | /* Put prefix information. */ |
| 505 | psize = PSIZE (p->prefixlen); |
| 506 | stream_putc (s, p->prefixlen); |
| 507 | stream_write (s, (u_char *)&p->prefix, psize); |
| 508 | |
| 509 | /* Nexthop, ifindex, distance and metric information. */ |
| 510 | if (CHECK_FLAG (api->message, ZAPI_MESSAGE_NEXTHOP)) |
| 511 | { |
| 512 | stream_putc (s, api->nexthop_num + api->ifindex_num); |
| 513 | |
| 514 | for (i = 0; i < api->nexthop_num; i++) |
| 515 | { |
| 516 | stream_putc (s, ZEBRA_NEXTHOP_IPV6); |
| 517 | stream_write (s, (u_char *)api->nexthop[i], 16); |
| 518 | } |
| 519 | for (i = 0; i < api->ifindex_num; i++) |
| 520 | { |
| 521 | stream_putc (s, ZEBRA_NEXTHOP_IFINDEX); |
| 522 | stream_putl (s, api->ifindex[i]); |
| 523 | } |
| 524 | } |
| 525 | |
| 526 | if (CHECK_FLAG (api->message, ZAPI_MESSAGE_DISTANCE)) |
| 527 | stream_putc (s, api->distance); |
| 528 | if (CHECK_FLAG (api->message, ZAPI_MESSAGE_METRIC)) |
| 529 | stream_putl (s, api->metric); |
| 530 | |
| 531 | /* Put length at the first point of the stream. */ |
| 532 | stream_putw_at (s, 0, stream_get_endp (s)); |
| 533 | |
ajs | 634f9ea | 2005-04-11 15:51:40 +0000 | [diff] [blame] | 534 | return zclient_send_message(zclient); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 535 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 536 | #endif /* HAVE_IPV6 */ |
| 537 | |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 538 | /* |
| 539 | * send a ZEBRA_REDISTRIBUTE_ADD or ZEBRA_REDISTRIBUTE_DELETE |
| 540 | * for the route type (ZEBRA_ROUTE_KERNEL etc.). The zebra server will |
| 541 | * then set/unset redist[type] in the client handle (a struct zserv) for the |
| 542 | * sending client |
| 543 | */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 544 | int |
ajs | 634f9ea | 2005-04-11 15:51:40 +0000 | [diff] [blame] | 545 | zebra_redistribute_send (int command, struct zclient *zclient, int type) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 546 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 547 | struct stream *s; |
| 548 | |
ajs | 634f9ea | 2005-04-11 15:51:40 +0000 | [diff] [blame] | 549 | s = zclient->obuf; |
| 550 | stream_reset(s); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 551 | |
paul | c1b9800 | 2006-01-16 01:54:02 +0000 | [diff] [blame] | 552 | zclient_create_header (s, command); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 553 | stream_putc (s, type); |
paul | c1b9800 | 2006-01-16 01:54:02 +0000 | [diff] [blame] | 554 | |
| 555 | stream_putw_at (s, 0, stream_get_endp (s)); |
| 556 | |
ajs | 634f9ea | 2005-04-11 15:51:40 +0000 | [diff] [blame] | 557 | return zclient_send_message(zclient); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 558 | } |
| 559 | |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 560 | /* Router-id update from zebra daemon. */ |
| 561 | void |
| 562 | zebra_router_id_update_read (struct stream *s, struct prefix *rid) |
| 563 | { |
| 564 | int plen; |
| 565 | |
| 566 | /* Fetch interface address. */ |
| 567 | rid->family = stream_getc (s); |
| 568 | |
| 569 | plen = prefix_blen (rid); |
| 570 | stream_get (&rid->u.prefix, s, plen); |
| 571 | rid->prefixlen = stream_getc (s); |
| 572 | } |
| 573 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 574 | /* Interface addition from zebra daemon. */ |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 575 | /* |
| 576 | * The format of the message sent with type ZEBRA_INTERFACE_ADD or |
| 577 | * ZEBRA_INTERFACE_DELETE from zebra to the client is: |
| 578 | * 0 1 2 3 |
| 579 | * 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 |
| 580 | * +-+-+-+-+-+-+-+-+ |
| 581 | * | type | |
| 582 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 583 | * | ifname | |
| 584 | * | | |
| 585 | * | | |
| 586 | * | | |
| 587 | * | | |
| 588 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 589 | * | ifindex | |
| 590 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 591 | * | if_flags | |
paul | c77d454 | 2006-01-11 01:59:04 +0000 | [diff] [blame] | 592 | * | | |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 593 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 594 | * | metric | |
| 595 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 596 | * | ifmtu | |
| 597 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 598 | * | ifmtu6 | |
| 599 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 600 | * | bandwidth | |
| 601 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 602 | * | sockaddr_dl | |
| 603 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 604 | */ |
| 605 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 606 | struct interface * |
| 607 | zebra_interface_add_read (struct stream *s) |
| 608 | { |
| 609 | struct interface *ifp; |
paul | 02ff83c | 2004-06-11 11:27:03 +0000 | [diff] [blame] | 610 | char ifname_tmp[INTERFACE_NAMSIZ]; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 611 | |
| 612 | /* Read interface name. */ |
| 613 | stream_get (ifname_tmp, s, INTERFACE_NAMSIZ); |
| 614 | |
ajs | a349198 | 2005-04-02 22:50:38 +0000 | [diff] [blame] | 615 | /* Lookup/create interface by name. */ |
| 616 | ifp = if_get_by_name_len (ifname_tmp, strnlen(ifname_tmp, INTERFACE_NAMSIZ)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 617 | |
| 618 | /* Read interface's index. */ |
| 619 | ifp->ifindex = stream_getl (s); |
| 620 | |
| 621 | /* Read interface's value. */ |
paul | 2e3b2e4 | 2002-12-13 21:03:13 +0000 | [diff] [blame] | 622 | ifp->status = stream_getc (s); |
paul | c77d454 | 2006-01-11 01:59:04 +0000 | [diff] [blame] | 623 | ifp->flags = stream_getq (s); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 624 | ifp->metric = stream_getl (s); |
| 625 | ifp->mtu = stream_getl (s); |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 626 | ifp->mtu6 = stream_getl (s); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 627 | ifp->bandwidth = stream_getl (s); |
| 628 | #ifdef HAVE_SOCKADDR_DL |
| 629 | stream_get (&ifp->sdl, s, sizeof (ifp->sdl)); |
| 630 | #else |
| 631 | ifp->hw_addr_len = stream_getl (s); |
| 632 | if (ifp->hw_addr_len) |
| 633 | stream_get (ifp->hw_addr, s, ifp->hw_addr_len); |
| 634 | #endif /* HAVE_SOCKADDR_DL */ |
| 635 | |
| 636 | return ifp; |
| 637 | } |
| 638 | |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 639 | /* |
| 640 | * Read interface up/down msg (ZEBRA_INTERFACE_UP/ZEBRA_INTERFACE_DOWN) |
| 641 | * from zebra server. The format of this message is the same as |
| 642 | * that sent for ZEBRA_INTERFACE_ADD/ZEBRA_INTERFACE_DELETE (see |
| 643 | * comments for zebra_interface_add_read), except that no sockaddr_dl |
| 644 | * is sent at the tail of the message. |
| 645 | */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 646 | struct interface * |
| 647 | zebra_interface_state_read (struct stream *s) |
| 648 | { |
| 649 | struct interface *ifp; |
paul | 02ff83c | 2004-06-11 11:27:03 +0000 | [diff] [blame] | 650 | char ifname_tmp[INTERFACE_NAMSIZ]; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 651 | |
| 652 | /* Read interface name. */ |
| 653 | stream_get (ifname_tmp, s, INTERFACE_NAMSIZ); |
| 654 | |
| 655 | /* Lookup this by interface index. */ |
ajs | a349198 | 2005-04-02 22:50:38 +0000 | [diff] [blame] | 656 | ifp = if_lookup_by_name_len (ifname_tmp, |
| 657 | strnlen(ifname_tmp, INTERFACE_NAMSIZ)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 658 | |
| 659 | /* If such interface does not exist, indicate an error */ |
| 660 | if (! ifp) |
| 661 | return NULL; |
| 662 | |
| 663 | /* Read interface's index. */ |
| 664 | ifp->ifindex = stream_getl (s); |
| 665 | |
| 666 | /* Read interface's value. */ |
paul | 2e3b2e4 | 2002-12-13 21:03:13 +0000 | [diff] [blame] | 667 | ifp->status = stream_getc (s); |
paul | c77d454 | 2006-01-11 01:59:04 +0000 | [diff] [blame] | 668 | ifp->flags = stream_getq (s); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 669 | ifp->metric = stream_getl (s); |
| 670 | ifp->mtu = stream_getl (s); |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 671 | ifp->mtu6 = stream_getl (s); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 672 | ifp->bandwidth = stream_getl (s); |
| 673 | |
| 674 | return ifp; |
| 675 | } |
| 676 | |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 677 | /* |
| 678 | * format of message for address additon is: |
| 679 | * 0 |
| 680 | * 0 1 2 3 4 5 6 7 |
| 681 | * +-+-+-+-+-+-+-+-+ |
| 682 | * | type | ZEBRA_INTERFACE_ADDRESS_ADD or |
| 683 | * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_ADDRES_DELETE |
| 684 | * | | |
| 685 | * + + |
| 686 | * | ifindex | |
| 687 | * + + |
| 688 | * | | |
| 689 | * + + |
| 690 | * | | |
| 691 | * +-+-+-+-+-+-+-+-+ |
| 692 | * | ifc_flags | flags for connected address |
| 693 | * +-+-+-+-+-+-+-+-+ |
| 694 | * | addr_family | |
| 695 | * +-+-+-+-+-+-+-+-+ |
| 696 | * | addr... | |
| 697 | * : : |
| 698 | * | | |
| 699 | * +-+-+-+-+-+-+-+-+ |
| 700 | * | addr_len | len of addr. E.g., addr_len = 4 for ipv4 addrs. |
| 701 | * +-+-+-+-+-+-+-+-+ |
| 702 | * | daddr.. | |
| 703 | * : : |
| 704 | * | | |
| 705 | * +-+-+-+-+-+-+-+-+ |
| 706 | * |
| 707 | */ |
| 708 | |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 709 | void |
| 710 | zebra_interface_if_set_value (struct stream *s, struct interface *ifp) |
| 711 | { |
| 712 | /* Read interface's index. */ |
| 713 | ifp->ifindex = stream_getl (s); |
hasso | 508ec91 | 2004-10-23 14:26:49 +0000 | [diff] [blame] | 714 | ifp->status = stream_getc (s); |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 715 | |
| 716 | /* Read interface's value. */ |
paul | c77d454 | 2006-01-11 01:59:04 +0000 | [diff] [blame] | 717 | ifp->flags = stream_getq (s); |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 718 | ifp->metric = stream_getl (s); |
| 719 | ifp->mtu = stream_getl (s); |
hasso | 508ec91 | 2004-10-23 14:26:49 +0000 | [diff] [blame] | 720 | ifp->mtu6 = stream_getl (s); |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 721 | ifp->bandwidth = stream_getl (s); |
| 722 | } |
| 723 | |
hasso | 3fb9cd6 | 2004-10-19 19:44:43 +0000 | [diff] [blame] | 724 | static int |
| 725 | memconstant(const void *s, int c, size_t n) |
| 726 | { |
| 727 | const u_char *p = s; |
| 728 | |
| 729 | while (n-- > 0) |
| 730 | if (*p++ != c) |
| 731 | return 0; |
| 732 | return 1; |
| 733 | } |
| 734 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 735 | struct connected * |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 736 | zebra_interface_address_read (int type, struct stream *s) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 737 | { |
| 738 | unsigned int ifindex; |
| 739 | struct interface *ifp; |
| 740 | struct connected *ifc; |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 741 | struct prefix p, d; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 742 | int family; |
| 743 | int plen; |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 744 | u_char ifc_flags; |
| 745 | |
| 746 | memset (&p, 0, sizeof(p)); |
| 747 | memset (&d, 0, sizeof(d)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 748 | |
| 749 | /* Get interface index. */ |
| 750 | ifindex = stream_getl (s); |
| 751 | |
| 752 | /* Lookup index. */ |
| 753 | ifp = if_lookup_by_index (ifindex); |
| 754 | if (ifp == NULL) |
| 755 | { |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 756 | zlog_warn ("zebra_interface_address_read(%s): " |
| 757 | "Can't find interface by ifindex: %d ", |
| 758 | (type == ZEBRA_INTERFACE_ADDRESS_ADD? "ADD" : "DELETE"), |
| 759 | ifindex); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 760 | return NULL; |
| 761 | } |
| 762 | |
| 763 | /* Fetch flag. */ |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 764 | ifc_flags = stream_getc (s); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 765 | |
| 766 | /* Fetch interface address. */ |
| 767 | family = p.family = stream_getc (s); |
| 768 | |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 769 | plen = prefix_blen (&p); |
| 770 | stream_get (&p.u.prefix, s, plen); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 771 | p.prefixlen = stream_getc (s); |
| 772 | |
| 773 | /* Fetch destination address. */ |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 774 | stream_get (&d.u.prefix, s, plen); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 775 | d.family = family; |
| 776 | |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 777 | if (type == ZEBRA_INTERFACE_ADDRESS_ADD) |
| 778 | { |
hasso | 3fb9cd6 | 2004-10-19 19:44:43 +0000 | [diff] [blame] | 779 | /* N.B. NULL destination pointers are encoded as all zeroes */ |
| 780 | ifc = connected_add_by_prefix(ifp, &p,(memconstant(&d.u.prefix,0,plen) ? |
| 781 | NULL : &d)); |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 782 | if (ifc != NULL) |
Andrew J. Schorr | e452963 | 2006-12-12 19:18:21 +0000 | [diff] [blame] | 783 | { |
| 784 | ifc->flags = ifc_flags; |
| 785 | if (ifc->destination) |
| 786 | ifc->destination->prefixlen = ifc->address->prefixlen; |
| 787 | } |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 788 | } |
| 789 | else |
| 790 | { |
| 791 | assert (type == ZEBRA_INTERFACE_ADDRESS_DELETE); |
| 792 | ifc = connected_delete_by_prefix(ifp, &p); |
| 793 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 794 | |
| 795 | return ifc; |
| 796 | } |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 797 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 798 | |
| 799 | /* Zebra client message read function. */ |
ajs | 634f9ea | 2005-04-11 15:51:40 +0000 | [diff] [blame] | 800 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 801 | zclient_read (struct thread *thread) |
| 802 | { |
| 803 | int ret; |
ajs | 634f9ea | 2005-04-11 15:51:40 +0000 | [diff] [blame] | 804 | size_t already; |
paul | c1b9800 | 2006-01-16 01:54:02 +0000 | [diff] [blame] | 805 | uint16_t length, command; |
| 806 | uint8_t marker, version; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 807 | struct zclient *zclient; |
| 808 | |
| 809 | /* Get socket to zebra. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 810 | zclient = THREAD_ARG (thread); |
| 811 | zclient->t_read = NULL; |
| 812 | |
ajs | 634f9ea | 2005-04-11 15:51:40 +0000 | [diff] [blame] | 813 | /* Read zebra header (if we don't have it already). */ |
| 814 | if ((already = stream_get_endp(zclient->ibuf)) < ZEBRA_HEADER_SIZE) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 815 | { |
ajs | 634f9ea | 2005-04-11 15:51:40 +0000 | [diff] [blame] | 816 | ssize_t nbyte; |
| 817 | if (((nbyte = stream_read_try(zclient->ibuf, zclient->sock, |
| 818 | ZEBRA_HEADER_SIZE-already)) == 0) || |
| 819 | (nbyte == -1)) |
| 820 | { |
| 821 | if (zclient_debug) |
| 822 | zlog_debug ("zclient connection closed socket [%d].", zclient->sock); |
| 823 | return zclient_failed(zclient); |
| 824 | } |
| 825 | if (nbyte != (ssize_t)(ZEBRA_HEADER_SIZE-already)) |
| 826 | { |
| 827 | /* Try again later. */ |
| 828 | zclient_event (ZCLIENT_READ, zclient); |
| 829 | return 0; |
| 830 | } |
| 831 | already = ZEBRA_HEADER_SIZE; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 832 | } |
| 833 | |
ajs | 634f9ea | 2005-04-11 15:51:40 +0000 | [diff] [blame] | 834 | /* Reset to read from the beginning of the incoming packet. */ |
| 835 | stream_set_getp(zclient->ibuf, 0); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 836 | |
paul | c1b9800 | 2006-01-16 01:54:02 +0000 | [diff] [blame] | 837 | /* Fetch header values. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 838 | length = stream_getw (zclient->ibuf); |
paul | c1b9800 | 2006-01-16 01:54:02 +0000 | [diff] [blame] | 839 | marker = stream_getc (zclient->ibuf); |
| 840 | version = stream_getc (zclient->ibuf); |
| 841 | command = stream_getw (zclient->ibuf); |
| 842 | |
| 843 | if (marker != ZEBRA_HEADER_MARKER || version != ZSERV_VERSION) |
| 844 | { |
| 845 | zlog_err("%s: socket %d version mismatch, marker %d, version %d", |
| 846 | __func__, zclient->sock, marker, version); |
| 847 | return zclient_failed(zclient); |
| 848 | } |
| 849 | |
ajs | 634f9ea | 2005-04-11 15:51:40 +0000 | [diff] [blame] | 850 | if (length < ZEBRA_HEADER_SIZE) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 851 | { |
ajs | 634f9ea | 2005-04-11 15:51:40 +0000 | [diff] [blame] | 852 | zlog_err("%s: socket %d message length %u is less than %d ", |
| 853 | __func__, zclient->sock, length, ZEBRA_HEADER_SIZE); |
| 854 | return zclient_failed(zclient); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 855 | } |
ajs | 634f9ea | 2005-04-11 15:51:40 +0000 | [diff] [blame] | 856 | |
| 857 | /* Length check. */ |
| 858 | if (length > STREAM_SIZE(zclient->ibuf)) |
| 859 | { |
| 860 | struct stream *ns; |
| 861 | zlog_warn("%s: message size %u exceeds buffer size %lu, expanding...", |
| 862 | __func__, length, (u_long)STREAM_SIZE(zclient->ibuf)); |
| 863 | ns = stream_new(length); |
| 864 | stream_copy(ns, zclient->ibuf); |
| 865 | stream_free (zclient->ibuf); |
| 866 | zclient->ibuf = ns; |
| 867 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 868 | |
| 869 | /* Read rest of zebra packet. */ |
ajs | 634f9ea | 2005-04-11 15:51:40 +0000 | [diff] [blame] | 870 | if (already < length) |
| 871 | { |
| 872 | ssize_t nbyte; |
| 873 | if (((nbyte = stream_read_try(zclient->ibuf, zclient->sock, |
| 874 | length-already)) == 0) || |
| 875 | (nbyte == -1)) |
| 876 | { |
| 877 | if (zclient_debug) |
| 878 | zlog_debug("zclient connection closed socket [%d].", zclient->sock); |
| 879 | return zclient_failed(zclient); |
| 880 | } |
| 881 | if (nbyte != (ssize_t)(length-already)) |
| 882 | { |
| 883 | /* Try again later. */ |
| 884 | zclient_event (ZCLIENT_READ, zclient); |
| 885 | return 0; |
| 886 | } |
| 887 | } |
| 888 | |
| 889 | length -= ZEBRA_HEADER_SIZE; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 890 | |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 891 | if (zclient_debug) |
ajs | 8ddca70 | 2004-12-07 18:53:52 +0000 | [diff] [blame] | 892 | zlog_debug("zclient 0x%p command 0x%x \n", zclient, command); |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 893 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 894 | switch (command) |
| 895 | { |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 896 | case ZEBRA_ROUTER_ID_UPDATE: |
| 897 | if (zclient->router_id_update) |
| 898 | ret = (*zclient->router_id_update) (command, zclient, length); |
| 899 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 900 | case ZEBRA_INTERFACE_ADD: |
| 901 | if (zclient->interface_add) |
| 902 | ret = (*zclient->interface_add) (command, zclient, length); |
| 903 | break; |
| 904 | case ZEBRA_INTERFACE_DELETE: |
| 905 | if (zclient->interface_delete) |
| 906 | ret = (*zclient->interface_delete) (command, zclient, length); |
| 907 | break; |
| 908 | case ZEBRA_INTERFACE_ADDRESS_ADD: |
| 909 | if (zclient->interface_address_add) |
| 910 | ret = (*zclient->interface_address_add) (command, zclient, length); |
| 911 | break; |
| 912 | case ZEBRA_INTERFACE_ADDRESS_DELETE: |
| 913 | if (zclient->interface_address_delete) |
| 914 | ret = (*zclient->interface_address_delete) (command, zclient, length); |
| 915 | break; |
| 916 | case ZEBRA_INTERFACE_UP: |
| 917 | if (zclient->interface_up) |
| 918 | ret = (*zclient->interface_up) (command, zclient, length); |
| 919 | break; |
| 920 | case ZEBRA_INTERFACE_DOWN: |
| 921 | if (zclient->interface_down) |
| 922 | ret = (*zclient->interface_down) (command, zclient, length); |
| 923 | break; |
| 924 | case ZEBRA_IPV4_ROUTE_ADD: |
| 925 | if (zclient->ipv4_route_add) |
| 926 | ret = (*zclient->ipv4_route_add) (command, zclient, length); |
| 927 | break; |
| 928 | case ZEBRA_IPV4_ROUTE_DELETE: |
| 929 | if (zclient->ipv4_route_delete) |
| 930 | ret = (*zclient->ipv4_route_delete) (command, zclient, length); |
| 931 | break; |
| 932 | case ZEBRA_IPV6_ROUTE_ADD: |
| 933 | if (zclient->ipv6_route_add) |
| 934 | ret = (*zclient->ipv6_route_add) (command, zclient, length); |
| 935 | break; |
| 936 | case ZEBRA_IPV6_ROUTE_DELETE: |
| 937 | if (zclient->ipv6_route_delete) |
| 938 | ret = (*zclient->ipv6_route_delete) (command, zclient, length); |
| 939 | break; |
| 940 | default: |
| 941 | break; |
| 942 | } |
| 943 | |
ajs | 634f9ea | 2005-04-11 15:51:40 +0000 | [diff] [blame] | 944 | if (zclient->sock < 0) |
| 945 | /* Connection was closed during packet processing. */ |
| 946 | return -1; |
| 947 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 948 | /* Register read thread. */ |
ajs | 634f9ea | 2005-04-11 15:51:40 +0000 | [diff] [blame] | 949 | stream_reset(zclient->ibuf); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 950 | zclient_event (ZCLIENT_READ, zclient); |
| 951 | |
| 952 | return 0; |
| 953 | } |
| 954 | |
| 955 | void |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 956 | zclient_redistribute (int command, struct zclient *zclient, int type) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 957 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 958 | |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 959 | if (command == ZEBRA_REDISTRIBUTE_ADD) |
| 960 | { |
| 961 | if (zclient->redist[type]) |
| 962 | return; |
| 963 | zclient->redist[type] = 1; |
| 964 | } |
| 965 | else |
| 966 | { |
| 967 | if (!zclient->redist[type]) |
| 968 | return; |
| 969 | zclient->redist[type] = 0; |
| 970 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 971 | |
| 972 | if (zclient->sock > 0) |
ajs | 634f9ea | 2005-04-11 15:51:40 +0000 | [diff] [blame] | 973 | zebra_redistribute_send (command, zclient, type); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 974 | } |
| 975 | |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 976 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 977 | void |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 978 | zclient_redistribute_default (int command, struct zclient *zclient) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 979 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 980 | |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 981 | if (command == ZEBRA_REDISTRIBUTE_DEFAULT_ADD) |
| 982 | { |
| 983 | if (zclient->default_information) |
| 984 | return; |
| 985 | zclient->default_information = 1; |
| 986 | } |
| 987 | else |
| 988 | { |
| 989 | if (!zclient->default_information) |
| 990 | return; |
| 991 | zclient->default_information = 0; |
| 992 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 993 | |
| 994 | if (zclient->sock > 0) |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 995 | zebra_message_send (zclient, command); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 996 | } |
| 997 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 998 | static void |
| 999 | zclient_event (enum event event, struct zclient *zclient) |
| 1000 | { |
| 1001 | switch (event) |
| 1002 | { |
| 1003 | case ZCLIENT_SCHEDULE: |
| 1004 | if (! zclient->t_connect) |
| 1005 | zclient->t_connect = |
| 1006 | thread_add_event (master, zclient_connect, zclient, 0); |
| 1007 | break; |
| 1008 | case ZCLIENT_CONNECT: |
| 1009 | if (zclient->fail >= 10) |
| 1010 | return; |
| 1011 | if (zclient_debug) |
ajs | 8ddca70 | 2004-12-07 18:53:52 +0000 | [diff] [blame] | 1012 | zlog_debug ("zclient connect schedule interval is %d", |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1013 | zclient->fail < 3 ? 10 : 60); |
| 1014 | if (! zclient->t_connect) |
| 1015 | zclient->t_connect = |
| 1016 | thread_add_timer (master, zclient_connect, zclient, |
| 1017 | zclient->fail < 3 ? 10 : 60); |
| 1018 | break; |
| 1019 | case ZCLIENT_READ: |
| 1020 | zclient->t_read = |
| 1021 | thread_add_read (master, zclient_read, zclient, zclient->sock); |
| 1022 | break; |
| 1023 | } |
| 1024 | } |