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 | |
| 318 | if (*size && stream_read (s, sock, *size) != *size) |
| 319 | return -1; |
| 320 | |
| 321 | return 0; |
| 322 | } |
| 323 | |
ajs | 634f9ea | 2005-04-11 15:51:40 +0000 | [diff] [blame] | 324 | /* Send simple Zebra message. */ |
| 325 | static int |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 326 | zebra_message_send (struct zclient *zclient, int command, vrf_id_t vrf_id) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 327 | { |
| 328 | struct stream *s; |
| 329 | |
| 330 | /* Get zclient output buffer. */ |
| 331 | s = zclient->obuf; |
| 332 | stream_reset (s); |
| 333 | |
| 334 | /* Send very simple command only Zebra message. */ |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 335 | zclient_create_header (s, command, vrf_id); |
paul | c1b9800 | 2006-01-16 01:54:02 +0000 | [diff] [blame] | 336 | |
ajs | 634f9ea | 2005-04-11 15:51:40 +0000 | [diff] [blame] | 337 | return zclient_send_message(zclient); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 338 | } |
| 339 | |
Vyacheslav Trushkin | 2ea1ab1 | 2011-12-11 18:48:47 +0400 | [diff] [blame] | 340 | static int |
| 341 | zebra_hello_send (struct zclient *zclient) |
| 342 | { |
| 343 | struct stream *s; |
| 344 | |
| 345 | if (zclient->redist_default) |
| 346 | { |
| 347 | s = zclient->obuf; |
| 348 | stream_reset (s); |
| 349 | |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 350 | /* The VRF ID in the HELLO message is always 0. */ |
| 351 | zclient_create_header (s, ZEBRA_HELLO, VRF_DEFAULT); |
Vyacheslav Trushkin | 2ea1ab1 | 2011-12-11 18:48:47 +0400 | [diff] [blame] | 352 | stream_putc (s, zclient->redist_default); |
| 353 | stream_putw_at (s, 0, stream_get_endp (s)); |
| 354 | return zclient_send_message(zclient); |
| 355 | } |
| 356 | |
| 357 | return 0; |
| 358 | } |
| 359 | |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 360 | /* Send requests to zebra daemon for the information in a VRF. */ |
| 361 | void |
| 362 | zclient_send_requests (struct zclient *zclient, vrf_id_t vrf_id) |
| 363 | { |
| 364 | int i; |
| 365 | |
| 366 | /* zclient is disabled. */ |
| 367 | if (! zclient->enable) |
| 368 | return; |
| 369 | |
| 370 | /* If not connected to the zebra yet. */ |
| 371 | if (zclient->sock < 0) |
| 372 | return; |
| 373 | |
| 374 | if (zclient_debug) |
| 375 | zlog_debug ("%s: send messages for VRF %u", __func__, vrf_id); |
| 376 | |
| 377 | /* We need router-id information. */ |
| 378 | zebra_message_send (zclient, ZEBRA_ROUTER_ID_ADD, vrf_id); |
| 379 | |
| 380 | /* We need interface information. */ |
| 381 | zebra_message_send (zclient, ZEBRA_INTERFACE_ADD, vrf_id); |
| 382 | |
| 383 | /* Set unwanted redistribute route. */ |
| 384 | vrf_bitmap_set (zclient->redist[zclient->redist_default], vrf_id); |
| 385 | |
| 386 | /* Flush all redistribute request. */ |
| 387 | for (i = 0; i < ZEBRA_ROUTE_MAX; i++) |
| 388 | if (i != zclient->redist_default && |
| 389 | vrf_bitmap_check (zclient->redist[i], vrf_id)) |
| 390 | zebra_redistribute_send (ZEBRA_REDISTRIBUTE_ADD, zclient, i, vrf_id); |
| 391 | |
| 392 | /* If default information is needed. */ |
| 393 | if (vrf_bitmap_check (zclient->default_information, VRF_DEFAULT)) |
| 394 | zebra_message_send (zclient, ZEBRA_REDISTRIBUTE_DEFAULT_ADD, vrf_id); |
| 395 | } |
| 396 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 397 | /* Make connection to zebra daemon. */ |
| 398 | int |
| 399 | zclient_start (struct zclient *zclient) |
| 400 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 401 | if (zclient_debug) |
ajs | 8ddca70 | 2004-12-07 18:53:52 +0000 | [diff] [blame] | 402 | zlog_debug ("zclient_start is called"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 403 | |
| 404 | /* zclient is disabled. */ |
| 405 | if (! zclient->enable) |
| 406 | return 0; |
| 407 | |
| 408 | /* If already connected to the zebra. */ |
| 409 | if (zclient->sock >= 0) |
| 410 | return 0; |
| 411 | |
| 412 | /* Check connect thread. */ |
| 413 | if (zclient->t_connect) |
| 414 | return 0; |
| 415 | |
Vyacheslav Trushkin | b511468 | 2011-11-25 18:51:48 +0400 | [diff] [blame] | 416 | if (zclient_socket_connect(zclient) < 0) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 417 | { |
| 418 | if (zclient_debug) |
ajs | 8ddca70 | 2004-12-07 18:53:52 +0000 | [diff] [blame] | 419 | zlog_debug ("zclient connection fail"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 420 | zclient->fail++; |
| 421 | zclient_event (ZCLIENT_CONNECT, zclient); |
| 422 | return -1; |
| 423 | } |
| 424 | |
ajs | 634f9ea | 2005-04-11 15:51:40 +0000 | [diff] [blame] | 425 | if (set_nonblocking(zclient->sock) < 0) |
| 426 | zlog_warn("%s: set_nonblocking(%d) failed", __func__, zclient->sock); |
| 427 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 428 | /* Clear fail count. */ |
| 429 | zclient->fail = 0; |
| 430 | if (zclient_debug) |
ajs | 8ddca70 | 2004-12-07 18:53:52 +0000 | [diff] [blame] | 431 | zlog_debug ("zclient connect success with socket [%d]", zclient->sock); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 432 | |
| 433 | /* Create read thread. */ |
| 434 | zclient_event (ZCLIENT_READ, zclient); |
| 435 | |
Vyacheslav Trushkin | 2ea1ab1 | 2011-12-11 18:48:47 +0400 | [diff] [blame] | 436 | zebra_hello_send (zclient); |
| 437 | |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 438 | /* Inform the successful connection. */ |
| 439 | if (zclient->zebra_connected) |
| 440 | (*zclient->zebra_connected) (zclient); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 441 | |
| 442 | return 0; |
| 443 | } |
| 444 | |
| 445 | /* This function is a wrapper function for calling zclient_start from |
| 446 | timer or event thread. */ |
ajs | 634f9ea | 2005-04-11 15:51:40 +0000 | [diff] [blame] | 447 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 448 | zclient_connect (struct thread *t) |
| 449 | { |
| 450 | struct zclient *zclient; |
| 451 | |
| 452 | zclient = THREAD_ARG (t); |
| 453 | zclient->t_connect = NULL; |
| 454 | |
| 455 | if (zclient_debug) |
ajs | 8ddca70 | 2004-12-07 18:53:52 +0000 | [diff] [blame] | 456 | zlog_debug ("zclient_connect is called"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 457 | |
| 458 | return zclient_start (zclient); |
| 459 | } |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 460 | |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 461 | /* |
| 462 | * "xdr_encode"-like interface that allows daemon (client) to send |
| 463 | * a message to zebra server for a route that needs to be |
| 464 | * added/deleted to the kernel. Info about the route is specified |
| 465 | * by the caller in a struct zapi_ipv4. zapi_ipv4_read() then writes |
| 466 | * the info down the zclient socket using the stream_* functions. |
| 467 | * |
| 468 | * The corresponding read ("xdr_decode") function on the server |
| 469 | * side is zread_ipv4_add()/zread_ipv4_delete(). |
| 470 | * |
| 471 | * 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 |
| 472 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 473 | * | Length (2) | Command | Route Type | |
| 474 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 475 | * | ZEBRA Flags | Message Flags | Prefix length | |
| 476 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 477 | * | Destination IPv4 Prefix for route | |
| 478 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 479 | * | Nexthop count | |
| 480 | * +-+-+-+-+-+-+-+-+ |
| 481 | * |
| 482 | * |
| 483 | * A number of IPv4 nexthop(s) or nexthop interface index(es) are then |
| 484 | * described, as per the Nexthop count. Each nexthop described as: |
| 485 | * |
| 486 | * +-+-+-+-+-+-+-+-+ |
| 487 | * | Nexthop Type | Set to one of ZEBRA_NEXTHOP_* |
| 488 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 489 | * | IPv4 Nexthop address or Interface Index number | |
| 490 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 491 | * |
| 492 | * Alternatively, if the flags field has ZEBRA_FLAG_BLACKHOLE or |
| 493 | * ZEBRA_FLAG_REJECT is set then Nexthop count is set to 1, then _no_ |
| 494 | * nexthop information is provided, and the message describes a prefix |
| 495 | * to blackhole or reject route. |
| 496 | * |
| 497 | * If ZAPI_MESSAGE_DISTANCE is set, the distance value is written as a 1 |
| 498 | * byte value. |
| 499 | * |
| 500 | * If ZAPI_MESSAGE_METRIC is set, the metric value is written as an 8 |
| 501 | * byte value. |
| 502 | * |
| 503 | * XXX: No attention paid to alignment. |
| 504 | */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 505 | int |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 506 | zapi_ipv4_route (u_char cmd, struct zclient *zclient, struct prefix_ipv4 *p, |
| 507 | struct zapi_ipv4 *api) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 508 | { |
| 509 | int i; |
| 510 | int psize; |
| 511 | struct stream *s; |
| 512 | |
| 513 | /* Reset stream. */ |
| 514 | s = zclient->obuf; |
| 515 | stream_reset (s); |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 516 | |
| 517 | zclient_create_header (s, cmd, api->vrf_id); |
paul | c1b9800 | 2006-01-16 01:54:02 +0000 | [diff] [blame] | 518 | |
| 519 | /* Put type and nexthop. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 520 | stream_putc (s, api->type); |
| 521 | stream_putc (s, api->flags); |
| 522 | stream_putc (s, api->message); |
G.Balaji | 5a616c0 | 2011-11-26 21:58:42 +0400 | [diff] [blame] | 523 | stream_putw (s, api->safi); |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 524 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 525 | /* Put prefix information. */ |
| 526 | psize = PSIZE (p->prefixlen); |
| 527 | stream_putc (s, p->prefixlen); |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 528 | stream_write (s, (u_char *) & p->prefix, psize); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 529 | |
| 530 | /* Nexthop, ifindex, distance and metric information. */ |
| 531 | if (CHECK_FLAG (api->message, ZAPI_MESSAGE_NEXTHOP)) |
| 532 | { |
paul | 595db7f | 2003-05-25 21:35:06 +0000 | [diff] [blame] | 533 | if (CHECK_FLAG (api->flags, ZEBRA_FLAG_BLACKHOLE)) |
| 534 | { |
| 535 | stream_putc (s, 1); |
| 536 | stream_putc (s, ZEBRA_NEXTHOP_BLACKHOLE); |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 537 | /* XXX assert(api->nexthop_num == 0); */ |
| 538 | /* XXX assert(api->ifindex_num == 0); */ |
paul | 595db7f | 2003-05-25 21:35:06 +0000 | [diff] [blame] | 539 | } |
| 540 | else |
| 541 | stream_putc (s, api->nexthop_num + api->ifindex_num); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 542 | |
| 543 | for (i = 0; i < api->nexthop_num; i++) |
paul | 595db7f | 2003-05-25 21:35:06 +0000 | [diff] [blame] | 544 | { |
| 545 | stream_putc (s, ZEBRA_NEXTHOP_IPV4); |
| 546 | stream_put_in_addr (s, api->nexthop[i]); |
| 547 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 548 | for (i = 0; i < api->ifindex_num; i++) |
paul | 595db7f | 2003-05-25 21:35:06 +0000 | [diff] [blame] | 549 | { |
| 550 | stream_putc (s, ZEBRA_NEXTHOP_IFINDEX); |
| 551 | stream_putl (s, api->ifindex[i]); |
| 552 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | if (CHECK_FLAG (api->message, ZAPI_MESSAGE_DISTANCE)) |
| 556 | stream_putc (s, api->distance); |
| 557 | if (CHECK_FLAG (api->message, ZAPI_MESSAGE_METRIC)) |
| 558 | stream_putl (s, api->metric); |
| 559 | |
| 560 | /* Put length at the first point of the stream. */ |
| 561 | stream_putw_at (s, 0, stream_get_endp (s)); |
| 562 | |
ajs | 634f9ea | 2005-04-11 15:51:40 +0000 | [diff] [blame] | 563 | return zclient_send_message(zclient); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | #ifdef HAVE_IPV6 |
| 567 | int |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 568 | zapi_ipv6_route (u_char cmd, struct zclient *zclient, struct prefix_ipv6 *p, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 569 | struct zapi_ipv6 *api) |
| 570 | { |
| 571 | int i; |
| 572 | int psize; |
| 573 | struct stream *s; |
| 574 | |
| 575 | /* Reset stream. */ |
| 576 | s = zclient->obuf; |
| 577 | stream_reset (s); |
| 578 | |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 579 | zclient_create_header (s, cmd, api->vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 580 | |
paul | c1b9800 | 2006-01-16 01:54:02 +0000 | [diff] [blame] | 581 | /* Put type and nexthop. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 582 | stream_putc (s, api->type); |
| 583 | stream_putc (s, api->flags); |
| 584 | stream_putc (s, api->message); |
G.Balaji | c7ec179 | 2011-11-26 22:04:05 +0400 | [diff] [blame] | 585 | stream_putw (s, api->safi); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 586 | |
| 587 | /* Put prefix information. */ |
| 588 | psize = PSIZE (p->prefixlen); |
| 589 | stream_putc (s, p->prefixlen); |
| 590 | stream_write (s, (u_char *)&p->prefix, psize); |
| 591 | |
| 592 | /* Nexthop, ifindex, distance and metric information. */ |
| 593 | if (CHECK_FLAG (api->message, ZAPI_MESSAGE_NEXTHOP)) |
| 594 | { |
| 595 | stream_putc (s, api->nexthop_num + api->ifindex_num); |
| 596 | |
| 597 | for (i = 0; i < api->nexthop_num; i++) |
| 598 | { |
| 599 | stream_putc (s, ZEBRA_NEXTHOP_IPV6); |
| 600 | stream_write (s, (u_char *)api->nexthop[i], 16); |
| 601 | } |
| 602 | for (i = 0; i < api->ifindex_num; i++) |
| 603 | { |
| 604 | stream_putc (s, ZEBRA_NEXTHOP_IFINDEX); |
| 605 | stream_putl (s, api->ifindex[i]); |
| 606 | } |
| 607 | } |
| 608 | |
| 609 | if (CHECK_FLAG (api->message, ZAPI_MESSAGE_DISTANCE)) |
| 610 | stream_putc (s, api->distance); |
| 611 | if (CHECK_FLAG (api->message, ZAPI_MESSAGE_METRIC)) |
| 612 | stream_putl (s, api->metric); |
| 613 | |
| 614 | /* Put length at the first point of the stream. */ |
| 615 | stream_putw_at (s, 0, stream_get_endp (s)); |
| 616 | |
ajs | 634f9ea | 2005-04-11 15:51:40 +0000 | [diff] [blame] | 617 | return zclient_send_message(zclient); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 618 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 619 | #endif /* HAVE_IPV6 */ |
| 620 | |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 621 | /* |
| 622 | * send a ZEBRA_REDISTRIBUTE_ADD or ZEBRA_REDISTRIBUTE_DELETE |
| 623 | * for the route type (ZEBRA_ROUTE_KERNEL etc.). The zebra server will |
| 624 | * then set/unset redist[type] in the client handle (a struct zserv) for the |
| 625 | * sending client |
| 626 | */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 627 | int |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 628 | zebra_redistribute_send (int command, struct zclient *zclient, int type, |
| 629 | vrf_id_t vrf_id) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 630 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 631 | struct stream *s; |
| 632 | |
ajs | 634f9ea | 2005-04-11 15:51:40 +0000 | [diff] [blame] | 633 | s = zclient->obuf; |
| 634 | stream_reset(s); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 635 | |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 636 | zclient_create_header (s, command, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 637 | stream_putc (s, type); |
paul | c1b9800 | 2006-01-16 01:54:02 +0000 | [diff] [blame] | 638 | |
| 639 | stream_putw_at (s, 0, stream_get_endp (s)); |
| 640 | |
ajs | 634f9ea | 2005-04-11 15:51:40 +0000 | [diff] [blame] | 641 | return zclient_send_message(zclient); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 642 | } |
| 643 | |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 644 | /* Router-id update from zebra daemon. */ |
| 645 | void |
| 646 | zebra_router_id_update_read (struct stream *s, struct prefix *rid) |
| 647 | { |
| 648 | int plen; |
| 649 | |
| 650 | /* Fetch interface address. */ |
| 651 | rid->family = stream_getc (s); |
| 652 | |
| 653 | plen = prefix_blen (rid); |
| 654 | stream_get (&rid->u.prefix, s, plen); |
| 655 | rid->prefixlen = stream_getc (s); |
| 656 | } |
| 657 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 658 | /* Interface addition from zebra daemon. */ |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 659 | /* |
| 660 | * The format of the message sent with type ZEBRA_INTERFACE_ADD or |
| 661 | * ZEBRA_INTERFACE_DELETE from zebra to the client is: |
| 662 | * 0 1 2 3 |
| 663 | * 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 |
| 664 | * +-+-+-+-+-+-+-+-+ |
| 665 | * | type | |
| 666 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 667 | * | ifname | |
| 668 | * | | |
| 669 | * | | |
| 670 | * | | |
| 671 | * | | |
| 672 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 673 | * | ifindex | |
| 674 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 675 | * | if_flags | |
paul | c77d454 | 2006-01-11 01:59:04 +0000 | [diff] [blame] | 676 | * | | |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 677 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 678 | * | metric | |
| 679 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 680 | * | ifmtu | |
| 681 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 682 | * | ifmtu6 | |
| 683 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 684 | * | bandwidth | |
| 685 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 686 | * | sockaddr_dl | |
| 687 | * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ |
| 688 | */ |
| 689 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 690 | struct interface * |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 691 | zebra_interface_add_read (struct stream *s, vrf_id_t vrf_id) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 692 | { |
| 693 | struct interface *ifp; |
paul | 02ff83c | 2004-06-11 11:27:03 +0000 | [diff] [blame] | 694 | char ifname_tmp[INTERFACE_NAMSIZ]; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 695 | |
| 696 | /* Read interface name. */ |
| 697 | stream_get (ifname_tmp, s, INTERFACE_NAMSIZ); |
| 698 | |
ajs | a349198 | 2005-04-02 22:50:38 +0000 | [diff] [blame] | 699 | /* Lookup/create interface by name. */ |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 700 | ifp = if_get_by_name_len_vrf (ifname_tmp, |
| 701 | strnlen (ifname_tmp, INTERFACE_NAMSIZ), |
| 702 | vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 703 | |
Josh Bailey | 51d4ef8 | 2012-03-21 17:13:39 -0700 | [diff] [blame] | 704 | zebra_interface_if_set_value (s, ifp); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 705 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 706 | return ifp; |
| 707 | } |
| 708 | |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 709 | /* |
| 710 | * Read interface up/down msg (ZEBRA_INTERFACE_UP/ZEBRA_INTERFACE_DOWN) |
| 711 | * from zebra server. The format of this message is the same as |
| 712 | * that sent for ZEBRA_INTERFACE_ADD/ZEBRA_INTERFACE_DELETE (see |
| 713 | * comments for zebra_interface_add_read), except that no sockaddr_dl |
| 714 | * is sent at the tail of the message. |
| 715 | */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 716 | struct interface * |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 717 | zebra_interface_state_read (struct stream *s, vrf_id_t vrf_id) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 718 | { |
| 719 | struct interface *ifp; |
paul | 02ff83c | 2004-06-11 11:27:03 +0000 | [diff] [blame] | 720 | char ifname_tmp[INTERFACE_NAMSIZ]; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 721 | |
| 722 | /* Read interface name. */ |
| 723 | stream_get (ifname_tmp, s, INTERFACE_NAMSIZ); |
| 724 | |
| 725 | /* Lookup this by interface index. */ |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 726 | ifp = if_lookup_by_name_len_vrf (ifname_tmp, |
| 727 | strnlen (ifname_tmp, INTERFACE_NAMSIZ), |
| 728 | vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 729 | |
| 730 | /* If such interface does not exist, indicate an error */ |
| 731 | if (! ifp) |
| 732 | return NULL; |
| 733 | |
Josh Bailey | 51d4ef8 | 2012-03-21 17:13:39 -0700 | [diff] [blame] | 734 | zebra_interface_if_set_value (s, ifp); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 735 | |
| 736 | return ifp; |
| 737 | } |
| 738 | |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 739 | /* |
| 740 | * format of message for address additon is: |
| 741 | * 0 |
| 742 | * 0 1 2 3 4 5 6 7 |
| 743 | * +-+-+-+-+-+-+-+-+ |
| 744 | * | type | ZEBRA_INTERFACE_ADDRESS_ADD or |
| 745 | * +-+-+-+-+-+-+-+-+ ZEBRA_INTERFACE_ADDRES_DELETE |
| 746 | * | | |
| 747 | * + + |
| 748 | * | ifindex | |
| 749 | * + + |
| 750 | * | | |
| 751 | * + + |
| 752 | * | | |
| 753 | * +-+-+-+-+-+-+-+-+ |
| 754 | * | ifc_flags | flags for connected address |
| 755 | * +-+-+-+-+-+-+-+-+ |
| 756 | * | addr_family | |
| 757 | * +-+-+-+-+-+-+-+-+ |
| 758 | * | addr... | |
| 759 | * : : |
| 760 | * | | |
| 761 | * +-+-+-+-+-+-+-+-+ |
| 762 | * | addr_len | len of addr. E.g., addr_len = 4 for ipv4 addrs. |
| 763 | * +-+-+-+-+-+-+-+-+ |
| 764 | * | daddr.. | |
| 765 | * : : |
| 766 | * | | |
| 767 | * +-+-+-+-+-+-+-+-+ |
| 768 | * |
| 769 | */ |
| 770 | |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 771 | void |
| 772 | zebra_interface_if_set_value (struct stream *s, struct interface *ifp) |
| 773 | { |
| 774 | /* Read interface's index. */ |
| 775 | ifp->ifindex = stream_getl (s); |
hasso | 508ec91 | 2004-10-23 14:26:49 +0000 | [diff] [blame] | 776 | ifp->status = stream_getc (s); |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 777 | |
| 778 | /* Read interface's value. */ |
paul | c77d454 | 2006-01-11 01:59:04 +0000 | [diff] [blame] | 779 | ifp->flags = stream_getq (s); |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 780 | ifp->metric = stream_getl (s); |
| 781 | ifp->mtu = stream_getl (s); |
hasso | 508ec91 | 2004-10-23 14:26:49 +0000 | [diff] [blame] | 782 | ifp->mtu6 = stream_getl (s); |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 783 | ifp->bandwidth = stream_getl (s); |
Josh Bailey | 51d4ef8 | 2012-03-21 17:13:39 -0700 | [diff] [blame] | 784 | #ifdef HAVE_STRUCT_SOCKADDR_DL |
David Lamparter | ca3ccd8 | 2012-09-26 14:52:39 +0200 | [diff] [blame] | 785 | stream_get (&ifp->sdl, s, sizeof (ifp->sdl_storage)); |
Josh Bailey | 51d4ef8 | 2012-03-21 17:13:39 -0700 | [diff] [blame] | 786 | #else |
| 787 | ifp->hw_addr_len = stream_getl (s); |
| 788 | if (ifp->hw_addr_len) |
| 789 | stream_get (ifp->hw_addr, s, ifp->hw_addr_len); |
| 790 | #endif /* HAVE_STRUCT_SOCKADDR_DL */ |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 791 | } |
| 792 | |
hasso | 3fb9cd6 | 2004-10-19 19:44:43 +0000 | [diff] [blame] | 793 | static int |
| 794 | memconstant(const void *s, int c, size_t n) |
| 795 | { |
| 796 | const u_char *p = s; |
| 797 | |
| 798 | while (n-- > 0) |
| 799 | if (*p++ != c) |
| 800 | return 0; |
| 801 | return 1; |
| 802 | } |
| 803 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 804 | struct connected * |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 805 | 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] | 806 | { |
| 807 | unsigned int ifindex; |
| 808 | struct interface *ifp; |
| 809 | struct connected *ifc; |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 810 | struct prefix p, d; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 811 | int family; |
| 812 | int plen; |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 813 | u_char ifc_flags; |
| 814 | |
| 815 | memset (&p, 0, sizeof(p)); |
| 816 | memset (&d, 0, sizeof(d)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 817 | |
| 818 | /* Get interface index. */ |
| 819 | ifindex = stream_getl (s); |
| 820 | |
| 821 | /* Lookup index. */ |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 822 | ifp = if_lookup_by_index_vrf (ifindex, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 823 | if (ifp == NULL) |
| 824 | { |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 825 | zlog_warn ("zebra_interface_address_read(%s): " |
| 826 | "Can't find interface by ifindex: %d ", |
| 827 | (type == ZEBRA_INTERFACE_ADDRESS_ADD? "ADD" : "DELETE"), |
| 828 | ifindex); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 829 | return NULL; |
| 830 | } |
| 831 | |
| 832 | /* Fetch flag. */ |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 833 | ifc_flags = stream_getc (s); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 834 | |
| 835 | /* Fetch interface address. */ |
| 836 | family = p.family = stream_getc (s); |
| 837 | |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 838 | plen = prefix_blen (&p); |
| 839 | stream_get (&p.u.prefix, s, plen); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 840 | p.prefixlen = stream_getc (s); |
| 841 | |
| 842 | /* Fetch destination address. */ |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 843 | stream_get (&d.u.prefix, s, plen); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 844 | d.family = family; |
| 845 | |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 846 | if (type == ZEBRA_INTERFACE_ADDRESS_ADD) |
| 847 | { |
hasso | 3fb9cd6 | 2004-10-19 19:44:43 +0000 | [diff] [blame] | 848 | /* N.B. NULL destination pointers are encoded as all zeroes */ |
| 849 | ifc = connected_add_by_prefix(ifp, &p,(memconstant(&d.u.prefix,0,plen) ? |
| 850 | NULL : &d)); |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 851 | if (ifc != NULL) |
Andrew J. Schorr | e452963 | 2006-12-12 19:18:21 +0000 | [diff] [blame] | 852 | { |
| 853 | ifc->flags = ifc_flags; |
| 854 | if (ifc->destination) |
| 855 | ifc->destination->prefixlen = ifc->address->prefixlen; |
David Lamparter | 90444ca | 2014-07-01 16:14:05 +0200 | [diff] [blame] | 856 | else if (CHECK_FLAG(ifc->flags, ZEBRA_IFA_PEER)) |
| 857 | { |
| 858 | /* 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] | 859 | char buf[PREFIX_STRLEN]; |
David Lamparter | 90444ca | 2014-07-01 16:14:05 +0200 | [diff] [blame] | 860 | zlog_warn("warning: interface %s address %s " |
| 861 | "with peer flag set, but no peer address!", |
Timo Teräs | 41eb9a4 | 2015-05-23 11:08:39 +0300 | [diff] [blame] | 862 | ifp->name, |
| 863 | prefix2str (ifc->address, buf, sizeof buf)); |
David Lamparter | 90444ca | 2014-07-01 16:14:05 +0200 | [diff] [blame] | 864 | UNSET_FLAG(ifc->flags, ZEBRA_IFA_PEER); |
| 865 | } |
Andrew J. Schorr | e452963 | 2006-12-12 19:18:21 +0000 | [diff] [blame] | 866 | } |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 867 | } |
| 868 | else |
| 869 | { |
| 870 | assert (type == ZEBRA_INTERFACE_ADDRESS_DELETE); |
| 871 | ifc = connected_delete_by_prefix(ifp, &p); |
| 872 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 873 | |
| 874 | return ifc; |
| 875 | } |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 876 | |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 877 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 878 | /* Zebra client message read function. */ |
ajs | 634f9ea | 2005-04-11 15:51:40 +0000 | [diff] [blame] | 879 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 880 | zclient_read (struct thread *thread) |
| 881 | { |
ajs | 634f9ea | 2005-04-11 15:51:40 +0000 | [diff] [blame] | 882 | size_t already; |
paul | c1b9800 | 2006-01-16 01:54:02 +0000 | [diff] [blame] | 883 | uint16_t length, command; |
| 884 | uint8_t marker, version; |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 885 | vrf_id_t vrf_id; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 886 | struct zclient *zclient; |
| 887 | |
| 888 | /* Get socket to zebra. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 889 | zclient = THREAD_ARG (thread); |
| 890 | zclient->t_read = NULL; |
| 891 | |
ajs | 634f9ea | 2005-04-11 15:51:40 +0000 | [diff] [blame] | 892 | /* Read zebra header (if we don't have it already). */ |
| 893 | if ((already = stream_get_endp(zclient->ibuf)) < ZEBRA_HEADER_SIZE) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 894 | { |
ajs | 634f9ea | 2005-04-11 15:51:40 +0000 | [diff] [blame] | 895 | ssize_t nbyte; |
| 896 | if (((nbyte = stream_read_try(zclient->ibuf, zclient->sock, |
| 897 | ZEBRA_HEADER_SIZE-already)) == 0) || |
| 898 | (nbyte == -1)) |
| 899 | { |
| 900 | if (zclient_debug) |
| 901 | zlog_debug ("zclient connection closed socket [%d].", zclient->sock); |
| 902 | return zclient_failed(zclient); |
| 903 | } |
| 904 | if (nbyte != (ssize_t)(ZEBRA_HEADER_SIZE-already)) |
| 905 | { |
| 906 | /* Try again later. */ |
| 907 | zclient_event (ZCLIENT_READ, zclient); |
| 908 | return 0; |
| 909 | } |
| 910 | already = ZEBRA_HEADER_SIZE; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 911 | } |
| 912 | |
ajs | 634f9ea | 2005-04-11 15:51:40 +0000 | [diff] [blame] | 913 | /* Reset to read from the beginning of the incoming packet. */ |
| 914 | stream_set_getp(zclient->ibuf, 0); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 915 | |
paul | c1b9800 | 2006-01-16 01:54:02 +0000 | [diff] [blame] | 916 | /* Fetch header values. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 917 | length = stream_getw (zclient->ibuf); |
paul | c1b9800 | 2006-01-16 01:54:02 +0000 | [diff] [blame] | 918 | marker = stream_getc (zclient->ibuf); |
| 919 | version = stream_getc (zclient->ibuf); |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 920 | vrf_id = stream_getw (zclient->ibuf); |
paul | c1b9800 | 2006-01-16 01:54:02 +0000 | [diff] [blame] | 921 | command = stream_getw (zclient->ibuf); |
| 922 | |
| 923 | if (marker != ZEBRA_HEADER_MARKER || version != ZSERV_VERSION) |
| 924 | { |
| 925 | zlog_err("%s: socket %d version mismatch, marker %d, version %d", |
| 926 | __func__, zclient->sock, marker, version); |
| 927 | return zclient_failed(zclient); |
| 928 | } |
| 929 | |
ajs | 634f9ea | 2005-04-11 15:51:40 +0000 | [diff] [blame] | 930 | if (length < ZEBRA_HEADER_SIZE) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 931 | { |
ajs | 634f9ea | 2005-04-11 15:51:40 +0000 | [diff] [blame] | 932 | zlog_err("%s: socket %d message length %u is less than %d ", |
| 933 | __func__, zclient->sock, length, ZEBRA_HEADER_SIZE); |
| 934 | return zclient_failed(zclient); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 935 | } |
ajs | 634f9ea | 2005-04-11 15:51:40 +0000 | [diff] [blame] | 936 | |
| 937 | /* Length check. */ |
| 938 | if (length > STREAM_SIZE(zclient->ibuf)) |
| 939 | { |
| 940 | struct stream *ns; |
| 941 | zlog_warn("%s: message size %u exceeds buffer size %lu, expanding...", |
| 942 | __func__, length, (u_long)STREAM_SIZE(zclient->ibuf)); |
| 943 | ns = stream_new(length); |
| 944 | stream_copy(ns, zclient->ibuf); |
| 945 | stream_free (zclient->ibuf); |
| 946 | zclient->ibuf = ns; |
| 947 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 948 | |
| 949 | /* Read rest of zebra packet. */ |
ajs | 634f9ea | 2005-04-11 15:51:40 +0000 | [diff] [blame] | 950 | if (already < length) |
| 951 | { |
| 952 | ssize_t nbyte; |
| 953 | if (((nbyte = stream_read_try(zclient->ibuf, zclient->sock, |
| 954 | length-already)) == 0) || |
| 955 | (nbyte == -1)) |
| 956 | { |
| 957 | if (zclient_debug) |
| 958 | zlog_debug("zclient connection closed socket [%d].", zclient->sock); |
| 959 | return zclient_failed(zclient); |
| 960 | } |
| 961 | if (nbyte != (ssize_t)(length-already)) |
| 962 | { |
| 963 | /* Try again later. */ |
| 964 | zclient_event (ZCLIENT_READ, zclient); |
| 965 | return 0; |
| 966 | } |
| 967 | } |
| 968 | |
| 969 | length -= ZEBRA_HEADER_SIZE; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 970 | |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 971 | if (zclient_debug) |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 972 | 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] | 973 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 974 | switch (command) |
| 975 | { |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 976 | case ZEBRA_ROUTER_ID_UPDATE: |
| 977 | if (zclient->router_id_update) |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 978 | (*zclient->router_id_update) (command, zclient, length, vrf_id); |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 979 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 980 | case ZEBRA_INTERFACE_ADD: |
| 981 | if (zclient->interface_add) |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 982 | (*zclient->interface_add) (command, zclient, length, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 983 | break; |
| 984 | case ZEBRA_INTERFACE_DELETE: |
| 985 | if (zclient->interface_delete) |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 986 | (*zclient->interface_delete) (command, zclient, length, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 987 | break; |
| 988 | case ZEBRA_INTERFACE_ADDRESS_ADD: |
| 989 | if (zclient->interface_address_add) |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 990 | (*zclient->interface_address_add) (command, zclient, length, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 991 | break; |
| 992 | case ZEBRA_INTERFACE_ADDRESS_DELETE: |
| 993 | if (zclient->interface_address_delete) |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 994 | (*zclient->interface_address_delete) (command, zclient, length, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 995 | break; |
| 996 | case ZEBRA_INTERFACE_UP: |
| 997 | if (zclient->interface_up) |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 998 | (*zclient->interface_up) (command, zclient, length, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 999 | break; |
| 1000 | case ZEBRA_INTERFACE_DOWN: |
| 1001 | if (zclient->interface_down) |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1002 | (*zclient->interface_down) (command, zclient, length, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1003 | break; |
| 1004 | case ZEBRA_IPV4_ROUTE_ADD: |
| 1005 | if (zclient->ipv4_route_add) |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1006 | (*zclient->ipv4_route_add) (command, zclient, length, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1007 | break; |
| 1008 | case ZEBRA_IPV4_ROUTE_DELETE: |
| 1009 | if (zclient->ipv4_route_delete) |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1010 | (*zclient->ipv4_route_delete) (command, zclient, length, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1011 | break; |
| 1012 | case ZEBRA_IPV6_ROUTE_ADD: |
| 1013 | if (zclient->ipv6_route_add) |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1014 | (*zclient->ipv6_route_add) (command, zclient, length, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1015 | break; |
| 1016 | case ZEBRA_IPV6_ROUTE_DELETE: |
| 1017 | if (zclient->ipv6_route_delete) |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1018 | (*zclient->ipv6_route_delete) (command, zclient, length, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1019 | break; |
| 1020 | default: |
| 1021 | break; |
| 1022 | } |
| 1023 | |
ajs | 634f9ea | 2005-04-11 15:51:40 +0000 | [diff] [blame] | 1024 | if (zclient->sock < 0) |
| 1025 | /* Connection was closed during packet processing. */ |
| 1026 | return -1; |
| 1027 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1028 | /* Register read thread. */ |
ajs | 634f9ea | 2005-04-11 15:51:40 +0000 | [diff] [blame] | 1029 | stream_reset(zclient->ibuf); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1030 | zclient_event (ZCLIENT_READ, zclient); |
| 1031 | |
| 1032 | return 0; |
| 1033 | } |
| 1034 | |
| 1035 | void |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1036 | zclient_redistribute (int command, struct zclient *zclient, int type, |
| 1037 | vrf_id_t vrf_id) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1038 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1039 | |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 1040 | if (command == ZEBRA_REDISTRIBUTE_ADD) |
| 1041 | { |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1042 | if (vrf_bitmap_check (zclient->redist[type], vrf_id)) |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 1043 | return; |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1044 | vrf_bitmap_set (zclient->redist[type], vrf_id); |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 1045 | } |
| 1046 | else |
| 1047 | { |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1048 | if (!vrf_bitmap_check (zclient->redist[type], vrf_id)) |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 1049 | return; |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1050 | vrf_bitmap_unset (zclient->redist[type], vrf_id); |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 1051 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1052 | |
| 1053 | if (zclient->sock > 0) |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1054 | zebra_redistribute_send (command, zclient, type, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1055 | } |
| 1056 | |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 1057 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1058 | void |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1059 | zclient_redistribute_default (int command, struct zclient *zclient, |
| 1060 | vrf_id_t vrf_id) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1061 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1062 | |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 1063 | if (command == ZEBRA_REDISTRIBUTE_DEFAULT_ADD) |
| 1064 | { |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1065 | if (vrf_bitmap_check (zclient->default_information, vrf_id)) |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 1066 | return; |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1067 | vrf_bitmap_set (zclient->default_information, vrf_id); |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 1068 | } |
| 1069 | else |
| 1070 | { |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1071 | if (!vrf_bitmap_check (zclient->default_information, vrf_id)) |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 1072 | return; |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1073 | vrf_bitmap_unset (zclient->default_information, vrf_id); |
paul | 0a58935 | 2004-05-08 11:48:26 +0000 | [diff] [blame] | 1074 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1075 | |
| 1076 | if (zclient->sock > 0) |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1077 | zebra_message_send (zclient, command, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1078 | } |
| 1079 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1080 | static void |
| 1081 | zclient_event (enum event event, struct zclient *zclient) |
| 1082 | { |
| 1083 | switch (event) |
| 1084 | { |
| 1085 | case ZCLIENT_SCHEDULE: |
| 1086 | if (! zclient->t_connect) |
| 1087 | zclient->t_connect = |
| 1088 | thread_add_event (master, zclient_connect, zclient, 0); |
| 1089 | break; |
| 1090 | case ZCLIENT_CONNECT: |
| 1091 | if (zclient->fail >= 10) |
| 1092 | return; |
| 1093 | if (zclient_debug) |
ajs | 8ddca70 | 2004-12-07 18:53:52 +0000 | [diff] [blame] | 1094 | zlog_debug ("zclient connect schedule interval is %d", |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1095 | zclient->fail < 3 ? 10 : 60); |
| 1096 | if (! zclient->t_connect) |
| 1097 | zclient->t_connect = |
| 1098 | thread_add_timer (master, zclient_connect, zclient, |
| 1099 | zclient->fail < 3 ? 10 : 60); |
| 1100 | break; |
| 1101 | case ZCLIENT_READ: |
| 1102 | zclient->t_read = |
| 1103 | thread_add_read (master, zclient_read, zclient, zclient->sock); |
| 1104 | break; |
| 1105 | } |
| 1106 | } |
Vyacheslav Trushkin | b511468 | 2011-11-25 18:51:48 +0400 | [diff] [blame] | 1107 | |
David Lamparter | fd8f6eb | 2015-03-03 08:57:02 +0100 | [diff] [blame] | 1108 | const char *zclient_serv_path_get() |
Everton Marques | 1f29894 | 2014-08-21 15:47:28 -0300 | [diff] [blame] | 1109 | { |
| 1110 | return zclient_serv_path ? zclient_serv_path : ZEBRA_SERV_PATH; |
| 1111 | } |
| 1112 | |
Vyacheslav Trushkin | b511468 | 2011-11-25 18:51:48 +0400 | [diff] [blame] | 1113 | void |
| 1114 | zclient_serv_path_set (char *path) |
| 1115 | { |
| 1116 | struct stat sb; |
| 1117 | |
| 1118 | /* reset */ |
| 1119 | zclient_serv_path = NULL; |
| 1120 | |
| 1121 | /* test if `path' is socket. don't set it otherwise. */ |
| 1122 | if (stat(path, &sb) == -1) |
| 1123 | { |
| 1124 | zlog_warn ("%s: zebra socket `%s' does not exist", __func__, path); |
| 1125 | return; |
| 1126 | } |
| 1127 | |
| 1128 | if ((sb.st_mode & S_IFMT) != S_IFSOCK) |
| 1129 | { |
| 1130 | zlog_warn ("%s: `%s' is not unix socket, sir", __func__, path); |
| 1131 | return; |
| 1132 | } |
| 1133 | |
| 1134 | /* it seems that path is unix socket */ |
| 1135 | zclient_serv_path = path; |
| 1136 | } |
| 1137 | |