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