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