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