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