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