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