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