paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1 | /* Zebra daemon server routine. |
| 2 | * Copyright (C) 1997, 98, 99 Kunihiro Ishiguro |
| 3 | * |
| 4 | * This file is part of GNU Zebra. |
| 5 | * |
| 6 | * GNU Zebra is free software; you can redistribute it and/or modify it |
| 7 | * under the terms of the GNU General Public License as published by the |
| 8 | * Free Software Foundation; either version 2, or (at your option) any |
| 9 | * later version. |
| 10 | * |
| 11 | * GNU Zebra is distributed in the hope that it will be useful, but |
| 12 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | * General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with GNU Zebra; see the file COPYING. If not, write to the |
| 18 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 19 | * Boston, MA 02111-1307, USA. |
| 20 | */ |
| 21 | |
| 22 | #include <zebra.h> |
| 23 | |
| 24 | #include "prefix.h" |
| 25 | #include "command.h" |
| 26 | #include "if.h" |
| 27 | #include "thread.h" |
| 28 | #include "stream.h" |
| 29 | #include "memory.h" |
| 30 | #include "table.h" |
| 31 | #include "rib.h" |
| 32 | #include "network.h" |
| 33 | #include "sockunion.h" |
| 34 | #include "log.h" |
| 35 | #include "zclient.h" |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 36 | #include "privs.h" |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 37 | #include "network.h" |
| 38 | #include "buffer.h" |
Feng Lu | 0d0686f | 2015-05-22 11:40:02 +0200 | [diff] [blame] | 39 | #include "vrf.h" |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 40 | |
| 41 | #include "zebra/zserv.h" |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 42 | #include "zebra/router-id.h" |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 43 | #include "zebra/redistribute.h" |
| 44 | #include "zebra/debug.h" |
| 45 | #include "zebra/ipforward.h" |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 46 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 47 | /* Event list of zebra. */ |
| 48 | enum event { ZEBRA_SERV, ZEBRA_READ, ZEBRA_WRITE }; |
| 49 | |
paul | b21b19c | 2003-06-15 01:28:29 +0000 | [diff] [blame] | 50 | extern struct zebra_t zebrad; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 51 | |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 52 | static void zebra_event (enum event event, int sock, struct zserv *client); |
paul | ccf3557 | 2003-03-01 11:42:20 +0000 | [diff] [blame] | 53 | |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 54 | extern struct zebra_privs_t zserv_privs; |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 55 | |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 56 | static void zebra_client_close (struct zserv *client); |
| 57 | |
| 58 | static int |
| 59 | zserv_delayed_close(struct thread *thread) |
paul | ccf3557 | 2003-03-01 11:42:20 +0000 | [diff] [blame] | 60 | { |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 61 | struct zserv *client = THREAD_ARG(thread); |
paul | ccf3557 | 2003-03-01 11:42:20 +0000 | [diff] [blame] | 62 | |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 63 | client->t_suicide = NULL; |
| 64 | zebra_client_close(client); |
paul | ccf3557 | 2003-03-01 11:42:20 +0000 | [diff] [blame] | 65 | return 0; |
| 66 | } |
| 67 | |
Vyacheslav Trushkin | 2ea1ab1 | 2011-12-11 18:48:47 +0400 | [diff] [blame] | 68 | /* When client connects, it sends hello message |
| 69 | * with promise to send zebra routes of specific type. |
| 70 | * Zebra stores a socket fd of the client into |
| 71 | * this array. And use it to clean up routes that |
| 72 | * client didn't remove for some reasons after closing |
| 73 | * connection. |
| 74 | */ |
| 75 | static int route_type_oaths[ZEBRA_ROUTE_MAX]; |
| 76 | |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 77 | static int |
| 78 | zserv_flush_data(struct thread *thread) |
paul | ccf3557 | 2003-03-01 11:42:20 +0000 | [diff] [blame] | 79 | { |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 80 | struct zserv *client = THREAD_ARG(thread); |
paul | ccf3557 | 2003-03-01 11:42:20 +0000 | [diff] [blame] | 81 | |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 82 | client->t_write = NULL; |
| 83 | if (client->t_suicide) |
| 84 | { |
| 85 | zebra_client_close(client); |
| 86 | return -1; |
| 87 | } |
| 88 | switch (buffer_flush_available(client->wb, client->sock)) |
| 89 | { |
| 90 | case BUFFER_ERROR: |
| 91 | zlog_warn("%s: buffer_flush_available failed on zserv client fd %d, " |
| 92 | "closing", __func__, client->sock); |
| 93 | zebra_client_close(client); |
| 94 | break; |
| 95 | case BUFFER_PENDING: |
| 96 | client->t_write = thread_add_write(zebrad.master, zserv_flush_data, |
| 97 | client, client->sock); |
| 98 | break; |
| 99 | case BUFFER_EMPTY: |
| 100 | break; |
| 101 | } |
| 102 | return 0; |
paul | ccf3557 | 2003-03-01 11:42:20 +0000 | [diff] [blame] | 103 | } |
| 104 | |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 105 | static int |
| 106 | zebra_server_send_message(struct zserv *client) |
paul | ccf3557 | 2003-03-01 11:42:20 +0000 | [diff] [blame] | 107 | { |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 108 | if (client->t_suicide) |
| 109 | return -1; |
| 110 | switch (buffer_write(client->wb, client->sock, STREAM_DATA(client->obuf), |
| 111 | stream_get_endp(client->obuf))) |
paul | ccf3557 | 2003-03-01 11:42:20 +0000 | [diff] [blame] | 112 | { |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 113 | case BUFFER_ERROR: |
| 114 | zlog_warn("%s: buffer_write failed to zserv client fd %d, closing", |
| 115 | __func__, client->sock); |
| 116 | /* Schedule a delayed close since many of the functions that call this |
| 117 | one do not check the return code. They do not allow for the |
| 118 | possibility that an I/O error may have caused the client to be |
| 119 | deleted. */ |
| 120 | client->t_suicide = thread_add_event(zebrad.master, zserv_delayed_close, |
| 121 | client, 0); |
| 122 | return -1; |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 123 | case BUFFER_EMPTY: |
| 124 | THREAD_OFF(client->t_write); |
| 125 | break; |
| 126 | case BUFFER_PENDING: |
| 127 | THREAD_WRITE_ON(zebrad.master, client->t_write, |
| 128 | zserv_flush_data, client, client->sock); |
| 129 | break; |
paul | ccf3557 | 2003-03-01 11:42:20 +0000 | [diff] [blame] | 130 | } |
paul | ccf3557 | 2003-03-01 11:42:20 +0000 | [diff] [blame] | 131 | return 0; |
| 132 | } |
| 133 | |
paul | c1b9800 | 2006-01-16 01:54:02 +0000 | [diff] [blame] | 134 | static void |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 135 | zserv_create_header (struct stream *s, uint16_t cmd, vrf_id_t vrf_id) |
paul | c1b9800 | 2006-01-16 01:54:02 +0000 | [diff] [blame] | 136 | { |
| 137 | /* length placeholder, caller can update */ |
| 138 | stream_putw (s, ZEBRA_HEADER_SIZE); |
| 139 | stream_putc (s, ZEBRA_HEADER_MARKER); |
| 140 | stream_putc (s, ZSERV_VERSION); |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 141 | stream_putw (s, vrf_id); |
paul | c1b9800 | 2006-01-16 01:54:02 +0000 | [diff] [blame] | 142 | stream_putw (s, cmd); |
| 143 | } |
| 144 | |
Josh Bailey | 51d4ef8 | 2012-03-21 17:13:39 -0700 | [diff] [blame] | 145 | static void |
| 146 | zserv_encode_interface (struct stream *s, struct interface *ifp) |
| 147 | { |
| 148 | /* Interface information. */ |
| 149 | stream_put (s, ifp->name, INTERFACE_NAMSIZ); |
| 150 | stream_putl (s, ifp->ifindex); |
| 151 | stream_putc (s, ifp->status); |
| 152 | stream_putq (s, ifp->flags); |
| 153 | stream_putl (s, ifp->metric); |
| 154 | stream_putl (s, ifp->mtu); |
| 155 | stream_putl (s, ifp->mtu6); |
| 156 | stream_putl (s, ifp->bandwidth); |
| 157 | #ifdef HAVE_STRUCT_SOCKADDR_DL |
David Lamparter | ca3ccd8 | 2012-09-26 14:52:39 +0200 | [diff] [blame] | 158 | stream_put (s, &ifp->sdl, sizeof (ifp->sdl_storage)); |
Josh Bailey | 51d4ef8 | 2012-03-21 17:13:39 -0700 | [diff] [blame] | 159 | #else |
| 160 | stream_putl (s, ifp->hw_addr_len); |
| 161 | if (ifp->hw_addr_len) |
| 162 | stream_put (s, ifp->hw_addr, ifp->hw_addr_len); |
| 163 | #endif /* HAVE_STRUCT_SOCKADDR_DL */ |
| 164 | |
| 165 | /* Write packet size. */ |
| 166 | stream_putw_at (s, 0, stream_get_endp (s)); |
| 167 | } |
| 168 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 169 | /* Interface is added. Send ZEBRA_INTERFACE_ADD to client. */ |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 170 | /* |
| 171 | * This function is called in the following situations: |
| 172 | * - in response to a 3-byte ZEBRA_INTERFACE_ADD request |
| 173 | * from the client. |
| 174 | * - at startup, when zebra figures out the available interfaces |
| 175 | * - when an interface is added (where support for |
| 176 | * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when |
| 177 | * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is |
| 178 | * received) |
| 179 | */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 180 | int |
| 181 | zsend_interface_add (struct zserv *client, struct interface *ifp) |
| 182 | { |
| 183 | struct stream *s; |
| 184 | |
| 185 | /* Check this client need interface information. */ |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 186 | if (! vrf_bitmap_check (client->ifinfo, ifp->vrf_id)) |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 187 | return 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 188 | |
| 189 | s = client->obuf; |
| 190 | stream_reset (s); |
| 191 | |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 192 | zserv_create_header (s, ZEBRA_INTERFACE_ADD, ifp->vrf_id); |
Josh Bailey | 51d4ef8 | 2012-03-21 17:13:39 -0700 | [diff] [blame] | 193 | zserv_encode_interface (s, ifp); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 194 | |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 195 | return zebra_server_send_message(client); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | /* Interface deletion from zebra daemon. */ |
| 199 | int |
| 200 | zsend_interface_delete (struct zserv *client, struct interface *ifp) |
| 201 | { |
| 202 | struct stream *s; |
| 203 | |
| 204 | /* Check this client need interface information. */ |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 205 | if (! vrf_bitmap_check (client->ifinfo, ifp->vrf_id)) |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 206 | return 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 207 | |
| 208 | s = client->obuf; |
| 209 | stream_reset (s); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 210 | |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 211 | zserv_create_header (s, ZEBRA_INTERFACE_DELETE, ifp->vrf_id); |
Josh Bailey | 51d4ef8 | 2012-03-21 17:13:39 -0700 | [diff] [blame] | 212 | zserv_encode_interface (s, ifp); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 213 | |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 214 | return zebra_server_send_message (client); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 215 | } |
| 216 | |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 217 | /* Interface address is added/deleted. Send ZEBRA_INTERFACE_ADDRESS_ADD or |
| 218 | * ZEBRA_INTERFACE_ADDRESS_DELETE to the client. |
| 219 | * |
| 220 | * A ZEBRA_INTERFACE_ADDRESS_ADD is sent in the following situations: |
| 221 | * - in response to a 3-byte ZEBRA_INTERFACE_ADD request |
| 222 | * from the client, after the ZEBRA_INTERFACE_ADD has been |
| 223 | * sent from zebra to the client |
| 224 | * - redistribute new address info to all clients in the following situations |
| 225 | * - at startup, when zebra figures out the available interfaces |
| 226 | * - when an interface is added (where support for |
| 227 | * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when |
| 228 | * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is |
| 229 | * received) |
| 230 | * - for the vty commands "ip address A.B.C.D/M [<secondary>|<label LINE>]" |
| 231 | * and "no bandwidth <1-10000000>", "ipv6 address X:X::X:X/M" |
| 232 | * - when an RTM_NEWADDR message is received from the kernel, |
| 233 | * |
| 234 | * The call tree that triggers ZEBRA_INTERFACE_ADDRESS_DELETE: |
| 235 | * |
| 236 | * zsend_interface_address(DELETE) |
| 237 | * ^ |
| 238 | * | |
| 239 | * zebra_interface_address_delete_update |
| 240 | * ^ ^ ^ |
paul | 6eb8827 | 2005-07-29 14:36:00 +0000 | [diff] [blame] | 241 | * | | if_delete_update |
| 242 | * | | |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 243 | * ip_address_uninstall connected_delete_ipv4 |
| 244 | * [ipv6_addresss_uninstall] [connected_delete_ipv6] |
| 245 | * ^ ^ |
| 246 | * | | |
| 247 | * | RTM_NEWADDR on routing/netlink socket |
| 248 | * | |
| 249 | * vty commands: |
| 250 | * "no ip address A.B.C.D/M [label LINE]" |
| 251 | * "no ip address A.B.C.D/M secondary" |
| 252 | * ["no ipv6 address X:X::X:X/M"] |
| 253 | * |
| 254 | */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 255 | int |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 256 | zsend_interface_address (int cmd, struct zserv *client, |
| 257 | struct interface *ifp, struct connected *ifc) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 258 | { |
| 259 | int blen; |
| 260 | struct stream *s; |
| 261 | struct prefix *p; |
| 262 | |
| 263 | /* Check this client need interface information. */ |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 264 | if (! vrf_bitmap_check (client->ifinfo, ifp->vrf_id)) |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 265 | return 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 266 | |
| 267 | s = client->obuf; |
| 268 | stream_reset (s); |
paul | c1b9800 | 2006-01-16 01:54:02 +0000 | [diff] [blame] | 269 | |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 270 | zserv_create_header (s, cmd, ifp->vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 271 | stream_putl (s, ifp->ifindex); |
| 272 | |
| 273 | /* Interface address flag. */ |
| 274 | stream_putc (s, ifc->flags); |
| 275 | |
| 276 | /* Prefix information. */ |
| 277 | p = ifc->address; |
| 278 | stream_putc (s, p->family); |
| 279 | blen = prefix_blen (p); |
| 280 | stream_put (s, &p->u.prefix, blen); |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 281 | |
| 282 | /* |
| 283 | * XXX gnu version does not send prefixlen for ZEBRA_INTERFACE_ADDRESS_DELETE |
| 284 | * but zebra_interface_address_delete_read() in the gnu version |
| 285 | * expects to find it |
| 286 | */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 287 | stream_putc (s, p->prefixlen); |
| 288 | |
| 289 | /* Destination. */ |
| 290 | p = ifc->destination; |
| 291 | if (p) |
| 292 | stream_put (s, &p->u.prefix, blen); |
| 293 | else |
| 294 | stream_put (s, NULL, blen); |
| 295 | |
| 296 | /* Write packet size. */ |
| 297 | stream_putw_at (s, 0, stream_get_endp (s)); |
| 298 | |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 299 | return zebra_server_send_message(client); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 300 | } |
| 301 | |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 302 | /* |
| 303 | * The cmd passed to zsend_interface_update may be ZEBRA_INTERFACE_UP or |
| 304 | * ZEBRA_INTERFACE_DOWN. |
| 305 | * |
| 306 | * The ZEBRA_INTERFACE_UP message is sent from the zebra server to |
| 307 | * the clients in one of 2 situations: |
| 308 | * - an if_up is detected e.g., as a result of an RTM_IFINFO message |
| 309 | * - a vty command modifying the bandwidth of an interface is received. |
| 310 | * The ZEBRA_INTERFACE_DOWN message is sent when an if_down is detected. |
| 311 | */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 312 | int |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 313 | zsend_interface_update (int cmd, struct zserv *client, struct interface *ifp) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 314 | { |
| 315 | struct stream *s; |
| 316 | |
| 317 | /* Check this client need interface information. */ |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 318 | if (! vrf_bitmap_check (client->ifinfo, ifp->vrf_id)) |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 319 | return 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 320 | |
| 321 | s = client->obuf; |
| 322 | stream_reset (s); |
| 323 | |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 324 | zserv_create_header (s, cmd, ifp->vrf_id); |
Josh Bailey | 51d4ef8 | 2012-03-21 17:13:39 -0700 | [diff] [blame] | 325 | zserv_encode_interface (s, ifp); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 326 | |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 327 | return zebra_server_send_message(client); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 328 | } |
| 329 | |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 330 | /* |
| 331 | * The zebra server sends the clients a ZEBRA_IPV4_ROUTE_ADD or a |
| 332 | * ZEBRA_IPV6_ROUTE_ADD via zsend_route_multipath in the following |
| 333 | * situations: |
| 334 | * - when the client starts up, and requests default information |
| 335 | * by sending a ZEBRA_REDISTRIBUTE_DEFAULT_ADD to the zebra server, in the |
| 336 | * - case of rip, ripngd, ospfd and ospf6d, when the client sends a |
| 337 | * ZEBRA_REDISTRIBUTE_ADD as a result of the "redistribute" vty cmd, |
| 338 | * - when the zebra server redistributes routes after it updates its rib |
| 339 | * |
| 340 | * The zebra server sends clients a ZEBRA_IPV4_ROUTE_DELETE or a |
| 341 | * ZEBRA_IPV6_ROUTE_DELETE via zsend_route_multipath when: |
| 342 | * - a "ip route" or "ipv6 route" vty command is issued, a prefix is |
| 343 | * - deleted from zebra's rib, and this info |
| 344 | * has to be redistributed to the clients |
| 345 | * |
| 346 | * XXX The ZEBRA_IPV*_ROUTE_ADD message is also sent by the client to the |
| 347 | * zebra server when the client wants to tell the zebra server to add a |
| 348 | * route to the kernel (zapi_ipv4_add etc. ). Since it's essentially the |
| 349 | * same message being sent back and forth, this function and |
| 350 | * zapi_ipv{4,6}_{add, delete} should be re-written to avoid code |
| 351 | * duplication. |
| 352 | */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 353 | int |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 354 | zsend_route_multipath (int cmd, struct zserv *client, struct prefix *p, |
| 355 | struct rib *rib) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 356 | { |
| 357 | int psize; |
| 358 | struct stream *s; |
| 359 | struct nexthop *nexthop; |
paul | 1dcb517 | 2005-05-31 08:38:50 +0000 | [diff] [blame] | 360 | unsigned long nhnummark = 0, messmark = 0; |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 361 | int nhnum = 0; |
paul | 1dcb517 | 2005-05-31 08:38:50 +0000 | [diff] [blame] | 362 | u_char zapi_flags = 0; |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 363 | |
| 364 | /* Check this client need this route. */ |
| 365 | if (!vrf_bitmap_check (client->redist[rib->type], rib->vrf_id) && |
| 366 | !(is_default (p) && |
| 367 | vrf_bitmap_check (client->redist_default, rib->vrf_id))) |
| 368 | return 0; |
| 369 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 370 | s = client->obuf; |
| 371 | stream_reset (s); |
paul | c1b9800 | 2006-01-16 01:54:02 +0000 | [diff] [blame] | 372 | |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 373 | zserv_create_header (s, cmd, rib->vrf_id); |
paul | c1b9800 | 2006-01-16 01:54:02 +0000 | [diff] [blame] | 374 | |
| 375 | /* Put type and nexthop. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 376 | stream_putc (s, rib->type); |
| 377 | stream_putc (s, rib->flags); |
paul | 1dcb517 | 2005-05-31 08:38:50 +0000 | [diff] [blame] | 378 | |
| 379 | /* marker for message flags field */ |
| 380 | messmark = stream_get_endp (s); |
| 381 | stream_putc (s, 0); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 382 | |
| 383 | /* Prefix. */ |
| 384 | psize = PSIZE (p->prefixlen); |
| 385 | stream_putc (s, p->prefixlen); |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 386 | stream_write (s, (u_char *) & p->u.prefix, psize); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 387 | |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 388 | /* |
| 389 | * XXX The message format sent by zebra below does not match the format |
| 390 | * of the corresponding message expected by the zebra server |
| 391 | * itself (e.g., see zread_ipv4_add). The nexthop_num is not set correctly, |
| 392 | * (is there a bug on the client side if more than one segment is sent?) |
| 393 | * nexthop ZEBRA_NEXTHOP_IPV4 is never set, ZEBRA_NEXTHOP_IFINDEX |
| 394 | * is hard-coded. |
| 395 | */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 396 | /* Nexthop */ |
paul | 1dcb517 | 2005-05-31 08:38:50 +0000 | [diff] [blame] | 397 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 398 | for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next) |
| 399 | { |
Timo Teräs | 325823a | 2016-01-15 17:36:31 +0200 | [diff] [blame^] | 400 | if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE)) |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 401 | { |
paul | 1dcb517 | 2005-05-31 08:38:50 +0000 | [diff] [blame] | 402 | SET_FLAG (zapi_flags, ZAPI_MESSAGE_NEXTHOP); |
| 403 | SET_FLAG (zapi_flags, ZAPI_MESSAGE_IFINDEX); |
| 404 | |
| 405 | if (nhnummark == 0) |
| 406 | { |
| 407 | nhnummark = stream_get_endp (s); |
| 408 | stream_putc (s, 1); /* placeholder */ |
| 409 | } |
| 410 | |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 411 | nhnum++; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 412 | |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 413 | switch(nexthop->type) |
| 414 | { |
| 415 | case NEXTHOP_TYPE_IPV4: |
| 416 | case NEXTHOP_TYPE_IPV4_IFINDEX: |
| 417 | stream_put_in_addr (s, &nexthop->gate.ipv4); |
| 418 | break; |
| 419 | #ifdef HAVE_IPV6 |
| 420 | case NEXTHOP_TYPE_IPV6: |
| 421 | case NEXTHOP_TYPE_IPV6_IFINDEX: |
| 422 | case NEXTHOP_TYPE_IPV6_IFNAME: |
| 423 | stream_write (s, (u_char *) &nexthop->gate.ipv6, 16); |
| 424 | break; |
| 425 | #endif |
| 426 | default: |
| 427 | if (cmd == ZEBRA_IPV4_ROUTE_ADD |
| 428 | || cmd == ZEBRA_IPV4_ROUTE_DELETE) |
| 429 | { |
| 430 | struct in_addr empty; |
paul | 44983cf | 2004-09-22 13:15:58 +0000 | [diff] [blame] | 431 | memset (&empty, 0, sizeof (struct in_addr)); |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 432 | stream_write (s, (u_char *) &empty, IPV4_MAX_BYTELEN); |
| 433 | } |
| 434 | else |
| 435 | { |
| 436 | struct in6_addr empty; |
| 437 | memset (&empty, 0, sizeof (struct in6_addr)); |
| 438 | stream_write (s, (u_char *) &empty, IPV6_MAX_BYTELEN); |
| 439 | } |
| 440 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 441 | |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 442 | /* Interface index. */ |
| 443 | stream_putc (s, 1); |
| 444 | stream_putl (s, nexthop->ifindex); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 445 | |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 446 | break; |
| 447 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | /* Metric */ |
Stephen Hemminger | cf8a831 | 2010-08-18 15:56:46 -0700 | [diff] [blame] | 451 | if (cmd == ZEBRA_IPV4_ROUTE_ADD || cmd == ZEBRA_IPV6_ROUTE_ADD) |
paul | 1dcb517 | 2005-05-31 08:38:50 +0000 | [diff] [blame] | 452 | { |
vincent | fbf5d03 | 2005-09-29 11:25:50 +0000 | [diff] [blame] | 453 | SET_FLAG (zapi_flags, ZAPI_MESSAGE_DISTANCE); |
| 454 | stream_putc (s, rib->distance); |
paul | 1dcb517 | 2005-05-31 08:38:50 +0000 | [diff] [blame] | 455 | SET_FLAG (zapi_flags, ZAPI_MESSAGE_METRIC); |
| 456 | stream_putl (s, rib->metric); |
Timo Teräs | b11f3b5 | 2015-11-02 16:50:07 +0200 | [diff] [blame] | 457 | SET_FLAG (zapi_flags, ZAPI_MESSAGE_MTU); |
| 458 | stream_putl (s, rib->mtu); |
paul | 1dcb517 | 2005-05-31 08:38:50 +0000 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | /* write real message flags value */ |
| 462 | stream_putc_at (s, messmark, zapi_flags); |
| 463 | |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 464 | /* Write next-hop number */ |
| 465 | if (nhnummark) |
hasso | c1eaa44 | 2004-10-19 06:26:01 +0000 | [diff] [blame] | 466 | stream_putc_at (s, nhnummark, nhnum); |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 467 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 468 | /* Write packet size. */ |
| 469 | stream_putw_at (s, 0, stream_get_endp (s)); |
| 470 | |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 471 | return zebra_server_send_message(client); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 472 | } |
| 473 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 474 | #ifdef HAVE_IPV6 |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 475 | static int |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 476 | zsend_ipv6_nexthop_lookup (struct zserv *client, struct in6_addr *addr, |
| 477 | vrf_id_t vrf_id) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 478 | { |
| 479 | struct stream *s; |
| 480 | struct rib *rib; |
| 481 | unsigned long nump; |
| 482 | u_char num; |
| 483 | struct nexthop *nexthop; |
| 484 | |
| 485 | /* Lookup nexthop. */ |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 486 | rib = rib_match_ipv6 (addr, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 487 | |
| 488 | /* Get output stream. */ |
| 489 | s = client->obuf; |
| 490 | stream_reset (s); |
| 491 | |
| 492 | /* Fill in result. */ |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 493 | zserv_create_header (s, ZEBRA_IPV6_NEXTHOP_LOOKUP, vrf_id); |
Hiroshi Yokoi | 8ccd74c | 2015-09-08 11:52:20 +0900 | [diff] [blame] | 494 | stream_put (s, addr, 16); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 495 | |
| 496 | if (rib) |
| 497 | { |
| 498 | stream_putl (s, rib->metric); |
| 499 | num = 0; |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 500 | nump = stream_get_endp(s); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 501 | stream_putc (s, 0); |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 502 | /* Only non-recursive routes are elegible to resolve nexthop we |
| 503 | * are looking up. Therefore, we will just iterate over the top |
| 504 | * chain of nexthops. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 505 | for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next) |
Timo Teräs | 325823a | 2016-01-15 17:36:31 +0200 | [diff] [blame^] | 506 | if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 507 | { |
| 508 | stream_putc (s, nexthop->type); |
| 509 | switch (nexthop->type) |
| 510 | { |
| 511 | case ZEBRA_NEXTHOP_IPV6: |
| 512 | stream_put (s, &nexthop->gate.ipv6, 16); |
| 513 | break; |
| 514 | case ZEBRA_NEXTHOP_IPV6_IFINDEX: |
| 515 | case ZEBRA_NEXTHOP_IPV6_IFNAME: |
| 516 | stream_put (s, &nexthop->gate.ipv6, 16); |
| 517 | stream_putl (s, nexthop->ifindex); |
| 518 | break; |
| 519 | case ZEBRA_NEXTHOP_IFINDEX: |
| 520 | case ZEBRA_NEXTHOP_IFNAME: |
| 521 | stream_putl (s, nexthop->ifindex); |
| 522 | break; |
hasso | fa2b17e | 2004-03-04 17:45:00 +0000 | [diff] [blame] | 523 | default: |
| 524 | /* do nothing */ |
| 525 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 526 | } |
| 527 | num++; |
| 528 | } |
| 529 | stream_putc_at (s, nump, num); |
| 530 | } |
| 531 | else |
| 532 | { |
| 533 | stream_putl (s, 0); |
| 534 | stream_putc (s, 0); |
| 535 | } |
| 536 | |
| 537 | stream_putw_at (s, 0, stream_get_endp (s)); |
| 538 | |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 539 | return zebra_server_send_message(client); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 540 | } |
| 541 | #endif /* HAVE_IPV6 */ |
| 542 | |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 543 | static int |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 544 | zsend_ipv4_nexthop_lookup (struct zserv *client, struct in_addr addr, |
| 545 | vrf_id_t vrf_id) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 546 | { |
| 547 | struct stream *s; |
| 548 | struct rib *rib; |
| 549 | unsigned long nump; |
| 550 | u_char num; |
| 551 | struct nexthop *nexthop; |
| 552 | |
David Lamparter | f598cf7 | 2014-11-22 14:44:20 -0800 | [diff] [blame] | 553 | /* Lookup nexthop - eBGP excluded */ |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 554 | rib = rib_match_ipv4_safi (addr, SAFI_UNICAST, 1, NULL, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 555 | |
| 556 | /* Get output stream. */ |
| 557 | s = client->obuf; |
| 558 | stream_reset (s); |
| 559 | |
| 560 | /* Fill in result. */ |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 561 | zserv_create_header (s, ZEBRA_IPV4_NEXTHOP_LOOKUP, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 562 | stream_put_in_addr (s, &addr); |
| 563 | |
| 564 | if (rib) |
| 565 | { |
Christian Franke | bb97e46 | 2013-05-25 14:01:35 +0000 | [diff] [blame] | 566 | if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV) |
| 567 | zlog_debug("%s: Matching rib entry found.", __func__); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 568 | stream_putl (s, rib->metric); |
| 569 | num = 0; |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 570 | nump = stream_get_endp(s); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 571 | stream_putc (s, 0); |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 572 | /* Only non-recursive routes are elegible to resolve the nexthop we |
| 573 | * are looking up. Therefore, we will just iterate over the top |
| 574 | * chain of nexthops. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 575 | for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next) |
Timo Teräs | 325823a | 2016-01-15 17:36:31 +0200 | [diff] [blame^] | 576 | if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 577 | { |
| 578 | stream_putc (s, nexthop->type); |
| 579 | switch (nexthop->type) |
| 580 | { |
| 581 | case ZEBRA_NEXTHOP_IPV4: |
| 582 | stream_put_in_addr (s, &nexthop->gate.ipv4); |
| 583 | break; |
Christian Franke | bb97e46 | 2013-05-25 14:01:35 +0000 | [diff] [blame] | 584 | case ZEBRA_NEXTHOP_IPV4_IFINDEX: |
| 585 | stream_put_in_addr (s, &nexthop->gate.ipv4); |
| 586 | stream_putl (s, nexthop->ifindex); |
| 587 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 588 | case ZEBRA_NEXTHOP_IFINDEX: |
| 589 | case ZEBRA_NEXTHOP_IFNAME: |
| 590 | stream_putl (s, nexthop->ifindex); |
| 591 | break; |
hasso | fa2b17e | 2004-03-04 17:45:00 +0000 | [diff] [blame] | 592 | default: |
| 593 | /* do nothing */ |
| 594 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 595 | } |
| 596 | num++; |
| 597 | } |
| 598 | stream_putc_at (s, nump, num); |
| 599 | } |
| 600 | else |
| 601 | { |
Christian Franke | bb97e46 | 2013-05-25 14:01:35 +0000 | [diff] [blame] | 602 | if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV) |
| 603 | zlog_debug("%s: No matching rib entry found.", __func__); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 604 | stream_putl (s, 0); |
| 605 | stream_putc (s, 0); |
| 606 | } |
| 607 | |
| 608 | stream_putw_at (s, 0, stream_get_endp (s)); |
| 609 | |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 610 | return zebra_server_send_message(client); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 611 | } |
| 612 | |
Everton Marques | 4e5275b | 2014-07-01 15:15:52 -0300 | [diff] [blame] | 613 | /* |
| 614 | Modified version of zsend_ipv4_nexthop_lookup(): |
| 615 | Query unicast rib if nexthop is not found on mrib. |
| 616 | Returns both route metric and protocol distance. |
| 617 | */ |
| 618 | static int |
David Lamparter | bd07812 | 2015-01-06 19:53:24 +0100 | [diff] [blame] | 619 | zsend_ipv4_nexthop_lookup_mrib (struct zserv *client, struct in_addr addr, |
| 620 | struct rib *rib) |
Everton Marques | 4e5275b | 2014-07-01 15:15:52 -0300 | [diff] [blame] | 621 | { |
| 622 | struct stream *s; |
Everton Marques | 4e5275b | 2014-07-01 15:15:52 -0300 | [diff] [blame] | 623 | unsigned long nump; |
| 624 | u_char num; |
| 625 | struct nexthop *nexthop; |
Everton Marques | 4e5275b | 2014-07-01 15:15:52 -0300 | [diff] [blame] | 626 | |
| 627 | /* Get output stream. */ |
| 628 | s = client->obuf; |
| 629 | stream_reset (s); |
| 630 | |
| 631 | /* Fill in result. */ |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 632 | zserv_create_header (s, ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB, rib->vrf_id); |
Everton Marques | 4e5275b | 2014-07-01 15:15:52 -0300 | [diff] [blame] | 633 | stream_put_in_addr (s, &addr); |
| 634 | |
| 635 | if (rib) |
| 636 | { |
| 637 | stream_putc (s, rib->distance); |
| 638 | stream_putl (s, rib->metric); |
| 639 | num = 0; |
| 640 | nump = stream_get_endp(s); /* remember position for nexthop_num */ |
| 641 | stream_putc (s, 0); /* reserve room for nexthop_num */ |
| 642 | /* Only non-recursive routes are elegible to resolve the nexthop we |
| 643 | * are looking up. Therefore, we will just iterate over the top |
| 644 | * chain of nexthops. */ |
| 645 | for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next) |
Timo Teräs | 325823a | 2016-01-15 17:36:31 +0200 | [diff] [blame^] | 646 | if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE)) |
Everton Marques | 4e5275b | 2014-07-01 15:15:52 -0300 | [diff] [blame] | 647 | { |
| 648 | stream_putc (s, nexthop->type); |
| 649 | switch (nexthop->type) |
| 650 | { |
| 651 | case ZEBRA_NEXTHOP_IPV4: |
| 652 | stream_put_in_addr (s, &nexthop->gate.ipv4); |
| 653 | break; |
| 654 | case ZEBRA_NEXTHOP_IPV4_IFINDEX: |
| 655 | stream_put_in_addr (s, &nexthop->gate.ipv4); |
| 656 | stream_putl (s, nexthop->ifindex); |
| 657 | break; |
| 658 | case ZEBRA_NEXTHOP_IFINDEX: |
| 659 | case ZEBRA_NEXTHOP_IFNAME: |
| 660 | stream_putl (s, nexthop->ifindex); |
| 661 | break; |
| 662 | default: |
| 663 | /* do nothing */ |
| 664 | break; |
| 665 | } |
| 666 | num++; |
| 667 | } |
| 668 | |
| 669 | stream_putc_at (s, nump, num); /* store nexthop_num */ |
| 670 | } |
| 671 | else |
| 672 | { |
| 673 | stream_putc (s, 0); /* distance */ |
| 674 | stream_putl (s, 0); /* metric */ |
| 675 | stream_putc (s, 0); /* nexthop_num */ |
| 676 | } |
| 677 | |
| 678 | stream_putw_at (s, 0, stream_get_endp (s)); |
| 679 | |
| 680 | return zebra_server_send_message(client); |
| 681 | } |
| 682 | |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 683 | static int |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 684 | zsend_ipv4_import_lookup (struct zserv *client, struct prefix_ipv4 *p, |
| 685 | vrf_id_t vrf_id) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 686 | { |
| 687 | struct stream *s; |
| 688 | struct rib *rib; |
| 689 | unsigned long nump; |
| 690 | u_char num; |
| 691 | struct nexthop *nexthop; |
| 692 | |
| 693 | /* Lookup nexthop. */ |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 694 | rib = rib_lookup_ipv4 (p, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 695 | |
| 696 | /* Get output stream. */ |
| 697 | s = client->obuf; |
| 698 | stream_reset (s); |
| 699 | |
| 700 | /* Fill in result. */ |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 701 | zserv_create_header (s, ZEBRA_IPV4_IMPORT_LOOKUP, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 702 | stream_put_in_addr (s, &p->prefix); |
| 703 | |
| 704 | if (rib) |
| 705 | { |
| 706 | stream_putl (s, rib->metric); |
| 707 | num = 0; |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 708 | nump = stream_get_endp(s); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 709 | stream_putc (s, 0); |
| 710 | for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next) |
Timo Teräs | 325823a | 2016-01-15 17:36:31 +0200 | [diff] [blame^] | 711 | if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 712 | { |
| 713 | stream_putc (s, nexthop->type); |
| 714 | switch (nexthop->type) |
| 715 | { |
| 716 | case ZEBRA_NEXTHOP_IPV4: |
| 717 | stream_put_in_addr (s, &nexthop->gate.ipv4); |
| 718 | break; |
Christian Franke | a12afd5 | 2013-05-25 14:01:36 +0000 | [diff] [blame] | 719 | case ZEBRA_NEXTHOP_IPV4_IFINDEX: |
| 720 | stream_put_in_addr (s, &nexthop->gate.ipv4); |
| 721 | stream_putl (s, nexthop->ifindex); |
| 722 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 723 | case ZEBRA_NEXTHOP_IFINDEX: |
| 724 | case ZEBRA_NEXTHOP_IFNAME: |
| 725 | stream_putl (s, nexthop->ifindex); |
| 726 | break; |
hasso | fa2b17e | 2004-03-04 17:45:00 +0000 | [diff] [blame] | 727 | default: |
| 728 | /* do nothing */ |
| 729 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 730 | } |
| 731 | num++; |
| 732 | } |
| 733 | stream_putc_at (s, nump, num); |
| 734 | } |
| 735 | else |
| 736 | { |
| 737 | stream_putl (s, 0); |
| 738 | stream_putc (s, 0); |
| 739 | } |
| 740 | |
| 741 | stream_putw_at (s, 0, stream_get_endp (s)); |
| 742 | |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 743 | return zebra_server_send_message(client); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 744 | } |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 745 | |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 746 | /* Router-id is updated. Send ZEBRA_ROUTER_ID_ADD to client. */ |
| 747 | int |
Feng Lu | ac19a44 | 2015-05-22 11:40:07 +0200 | [diff] [blame] | 748 | zsend_router_id_update (struct zserv *client, struct prefix *p, |
| 749 | vrf_id_t vrf_id) |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 750 | { |
| 751 | struct stream *s; |
| 752 | int blen; |
| 753 | |
| 754 | /* Check this client need interface information. */ |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 755 | if (! vrf_bitmap_check (client->ridinfo, vrf_id)) |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 756 | return 0; |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 757 | |
| 758 | s = client->obuf; |
| 759 | stream_reset (s); |
| 760 | |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 761 | /* Message type. */ |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 762 | zserv_create_header (s, ZEBRA_ROUTER_ID_UPDATE, vrf_id); |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 763 | |
| 764 | /* Prefix information. */ |
| 765 | stream_putc (s, p->family); |
| 766 | blen = prefix_blen (p); |
| 767 | stream_put (s, &p->u.prefix, blen); |
| 768 | stream_putc (s, p->prefixlen); |
| 769 | |
| 770 | /* Write packet size. */ |
| 771 | stream_putw_at (s, 0, stream_get_endp (s)); |
| 772 | |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 773 | return zebra_server_send_message(client); |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 774 | } |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 775 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 776 | /* Register zebra server interface information. Send current all |
| 777 | interface and address information. */ |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 778 | static int |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 779 | zread_interface_add (struct zserv *client, u_short length, vrf_id_t vrf_id) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 780 | { |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 781 | struct listnode *ifnode, *ifnnode; |
| 782 | struct listnode *cnode, *cnnode; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 783 | struct interface *ifp; |
| 784 | struct connected *c; |
| 785 | |
| 786 | /* Interface information is needed. */ |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 787 | vrf_bitmap_set (client->ifinfo, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 788 | |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 789 | for (ALL_LIST_ELEMENTS (vrf_iflist (vrf_id), ifnode, ifnnode, ifp)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 790 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 791 | /* Skip pseudo interface. */ |
| 792 | if (! CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE)) |
| 793 | continue; |
| 794 | |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 795 | if (zsend_interface_add (client, ifp) < 0) |
| 796 | return -1; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 797 | |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 798 | for (ALL_LIST_ELEMENTS (ifp->connected, cnode, cnnode, c)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 799 | { |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 800 | if (CHECK_FLAG (c->conf, ZEBRA_IFC_REAL) && |
| 801 | (zsend_interface_address (ZEBRA_INTERFACE_ADDRESS_ADD, client, |
| 802 | ifp, c) < 0)) |
| 803 | return -1; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 804 | } |
| 805 | } |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 806 | return 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 807 | } |
| 808 | |
| 809 | /* Unregister zebra server interface information. */ |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 810 | static int |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 811 | zread_interface_delete (struct zserv *client, u_short length, vrf_id_t vrf_id) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 812 | { |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 813 | vrf_bitmap_unset (client->ifinfo, vrf_id); |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 814 | return 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 815 | } |
| 816 | |
| 817 | /* This function support multiple nexthop. */ |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 818 | /* |
| 819 | * Parse the ZEBRA_IPV4_ROUTE_ADD sent from client. Update rib and |
| 820 | * add kernel route. |
| 821 | */ |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 822 | static int |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 823 | zread_ipv4_add (struct zserv *client, u_short length, vrf_id_t vrf_id) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 824 | { |
| 825 | int i; |
| 826 | struct rib *rib; |
| 827 | struct prefix_ipv4 p; |
| 828 | u_char message; |
| 829 | struct in_addr nexthop; |
| 830 | u_char nexthop_num; |
| 831 | u_char nexthop_type; |
| 832 | struct stream *s; |
| 833 | unsigned int ifindex; |
| 834 | u_char ifname_len; |
G.Balaji | cddf391 | 2011-11-26 21:59:32 +0400 | [diff] [blame] | 835 | safi_t safi; |
| 836 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 837 | |
| 838 | /* Get input stream. */ |
| 839 | s = client->ibuf; |
| 840 | |
| 841 | /* Allocate new rib. */ |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 842 | rib = XCALLOC (MTYPE_RIB, sizeof (struct rib)); |
| 843 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 844 | /* Type, flags, message. */ |
| 845 | rib->type = stream_getc (s); |
| 846 | rib->flags = stream_getc (s); |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 847 | message = stream_getc (s); |
G.Balaji | cddf391 | 2011-11-26 21:59:32 +0400 | [diff] [blame] | 848 | safi = stream_getw (s); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 849 | rib->uptime = time (NULL); |
| 850 | |
| 851 | /* IPv4 prefix. */ |
| 852 | memset (&p, 0, sizeof (struct prefix_ipv4)); |
| 853 | p.family = AF_INET; |
| 854 | p.prefixlen = stream_getc (s); |
| 855 | stream_get (&p.prefix, s, PSIZE (p.prefixlen)); |
| 856 | |
Feng Lu | 0d0686f | 2015-05-22 11:40:02 +0200 | [diff] [blame] | 857 | /* VRF ID */ |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 858 | rib->vrf_id = vrf_id; |
Feng Lu | 0d0686f | 2015-05-22 11:40:02 +0200 | [diff] [blame] | 859 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 860 | /* Nexthop parse. */ |
| 861 | if (CHECK_FLAG (message, ZAPI_MESSAGE_NEXTHOP)) |
| 862 | { |
| 863 | nexthop_num = stream_getc (s); |
| 864 | |
| 865 | for (i = 0; i < nexthop_num; i++) |
| 866 | { |
| 867 | nexthop_type = stream_getc (s); |
| 868 | |
| 869 | switch (nexthop_type) |
| 870 | { |
| 871 | case ZEBRA_NEXTHOP_IFINDEX: |
| 872 | ifindex = stream_getl (s); |
| 873 | nexthop_ifindex_add (rib, ifindex); |
| 874 | break; |
| 875 | case ZEBRA_NEXTHOP_IFNAME: |
| 876 | ifname_len = stream_getc (s); |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 877 | stream_forward_getp (s, ifname_len); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 878 | break; |
| 879 | case ZEBRA_NEXTHOP_IPV4: |
| 880 | nexthop.s_addr = stream_get_ipv4 (s); |
Paul Jakma | 7514fb7 | 2007-05-02 16:05:35 +0000 | [diff] [blame] | 881 | nexthop_ipv4_add (rib, &nexthop, NULL); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 882 | break; |
Joakim Tjernlund | c963c20 | 2012-07-07 17:06:13 +0200 | [diff] [blame] | 883 | case ZEBRA_NEXTHOP_IPV4_IFINDEX: |
| 884 | nexthop.s_addr = stream_get_ipv4 (s); |
| 885 | ifindex = stream_getl (s); |
| 886 | nexthop_ipv4_ifindex_add (rib, &nexthop, NULL, ifindex); |
| 887 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 888 | case ZEBRA_NEXTHOP_IPV6: |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 889 | stream_forward_getp (s, IPV6_MAX_BYTELEN); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 890 | break; |
Subbaiah Venkata | 6902c69 | 2012-03-27 19:21:29 -0700 | [diff] [blame] | 891 | case ZEBRA_NEXTHOP_BLACKHOLE: |
| 892 | nexthop_blackhole_add (rib); |
| 893 | break; |
| 894 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 895 | } |
| 896 | } |
| 897 | |
| 898 | /* Distance. */ |
| 899 | if (CHECK_FLAG (message, ZAPI_MESSAGE_DISTANCE)) |
| 900 | rib->distance = stream_getc (s); |
| 901 | |
| 902 | /* Metric. */ |
| 903 | if (CHECK_FLAG (message, ZAPI_MESSAGE_METRIC)) |
| 904 | rib->metric = stream_getl (s); |
| 905 | |
Timo Teräs | b11f3b5 | 2015-11-02 16:50:07 +0200 | [diff] [blame] | 906 | if (CHECK_FLAG (message, ZAPI_MESSAGE_MTU)) |
| 907 | rib->mtu = stream_getl (s); |
| 908 | |
Paul Jakma | 171eee3 | 2006-07-27 16:11:02 +0000 | [diff] [blame] | 909 | /* Table */ |
| 910 | rib->table=zebrad.rtm_table_default; |
G.Balaji | cddf391 | 2011-11-26 21:59:32 +0400 | [diff] [blame] | 911 | rib_add_ipv4_multipath (&p, rib, safi); |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 912 | return 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 913 | } |
| 914 | |
| 915 | /* Zebra server IPv4 prefix delete function. */ |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 916 | static int |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 917 | zread_ipv4_delete (struct zserv *client, u_short length, vrf_id_t vrf_id) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 918 | { |
| 919 | int i; |
| 920 | struct stream *s; |
| 921 | struct zapi_ipv4 api; |
Subbaiah Venkata | 6902c69 | 2012-03-27 19:21:29 -0700 | [diff] [blame] | 922 | struct in_addr nexthop, *nexthop_p; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 923 | unsigned long ifindex; |
| 924 | struct prefix_ipv4 p; |
| 925 | u_char nexthop_num; |
| 926 | u_char nexthop_type; |
| 927 | u_char ifname_len; |
| 928 | |
| 929 | s = client->ibuf; |
| 930 | ifindex = 0; |
| 931 | nexthop.s_addr = 0; |
Subbaiah Venkata | 6902c69 | 2012-03-27 19:21:29 -0700 | [diff] [blame] | 932 | nexthop_p = NULL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 933 | |
| 934 | /* Type, flags, message. */ |
| 935 | api.type = stream_getc (s); |
| 936 | api.flags = stream_getc (s); |
| 937 | api.message = stream_getc (s); |
G.Balaji | cddf391 | 2011-11-26 21:59:32 +0400 | [diff] [blame] | 938 | api.safi = stream_getw (s); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 939 | |
| 940 | /* IPv4 prefix. */ |
| 941 | memset (&p, 0, sizeof (struct prefix_ipv4)); |
| 942 | p.family = AF_INET; |
| 943 | p.prefixlen = stream_getc (s); |
| 944 | stream_get (&p.prefix, s, PSIZE (p.prefixlen)); |
| 945 | |
| 946 | /* Nexthop, ifindex, distance, metric. */ |
| 947 | if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP)) |
| 948 | { |
| 949 | nexthop_num = stream_getc (s); |
| 950 | |
| 951 | for (i = 0; i < nexthop_num; i++) |
| 952 | { |
| 953 | nexthop_type = stream_getc (s); |
| 954 | |
| 955 | switch (nexthop_type) |
| 956 | { |
| 957 | case ZEBRA_NEXTHOP_IFINDEX: |
| 958 | ifindex = stream_getl (s); |
| 959 | break; |
| 960 | case ZEBRA_NEXTHOP_IFNAME: |
| 961 | ifname_len = stream_getc (s); |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 962 | stream_forward_getp (s, ifname_len); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 963 | break; |
| 964 | case ZEBRA_NEXTHOP_IPV4: |
| 965 | nexthop.s_addr = stream_get_ipv4 (s); |
Subbaiah Venkata | 6902c69 | 2012-03-27 19:21:29 -0700 | [diff] [blame] | 966 | nexthop_p = &nexthop; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 967 | break; |
Joakim Tjernlund | c963c20 | 2012-07-07 17:06:13 +0200 | [diff] [blame] | 968 | case ZEBRA_NEXTHOP_IPV4_IFINDEX: |
| 969 | nexthop.s_addr = stream_get_ipv4 (s); |
Christian Franke | 23f5f7c | 2013-11-27 17:06:14 +0000 | [diff] [blame] | 970 | nexthop_p = &nexthop; |
Joakim Tjernlund | c963c20 | 2012-07-07 17:06:13 +0200 | [diff] [blame] | 971 | ifindex = stream_getl (s); |
| 972 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 973 | case ZEBRA_NEXTHOP_IPV6: |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 974 | stream_forward_getp (s, IPV6_MAX_BYTELEN); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 975 | break; |
| 976 | } |
| 977 | } |
| 978 | } |
| 979 | |
| 980 | /* Distance. */ |
| 981 | if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE)) |
| 982 | api.distance = stream_getc (s); |
| 983 | else |
| 984 | api.distance = 0; |
| 985 | |
| 986 | /* Metric. */ |
| 987 | if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC)) |
| 988 | api.metric = stream_getl (s); |
| 989 | else |
| 990 | api.metric = 0; |
| 991 | |
Subbaiah Venkata | 6902c69 | 2012-03-27 19:21:29 -0700 | [diff] [blame] | 992 | rib_delete_ipv4 (api.type, api.flags, &p, nexthop_p, ifindex, |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 993 | vrf_id, api.safi); |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 994 | return 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 995 | } |
| 996 | |
| 997 | /* Nexthop lookup for IPv4. */ |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 998 | static int |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 999 | zread_ipv4_nexthop_lookup (struct zserv *client, u_short length, |
| 1000 | vrf_id_t vrf_id) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1001 | { |
| 1002 | struct in_addr addr; |
Christian Franke | bb97e46 | 2013-05-25 14:01:35 +0000 | [diff] [blame] | 1003 | char buf[BUFSIZ]; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1004 | |
| 1005 | addr.s_addr = stream_get_ipv4 (client->ibuf); |
Christian Franke | bb97e46 | 2013-05-25 14:01:35 +0000 | [diff] [blame] | 1006 | if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV) |
| 1007 | zlog_debug("%s: looking up %s", __func__, |
| 1008 | inet_ntop (AF_INET, &addr, buf, BUFSIZ)); |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1009 | return zsend_ipv4_nexthop_lookup (client, addr, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1010 | } |
| 1011 | |
Everton Marques | 4e5275b | 2014-07-01 15:15:52 -0300 | [diff] [blame] | 1012 | /* MRIB Nexthop lookup for IPv4. */ |
| 1013 | static int |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1014 | zread_ipv4_nexthop_lookup_mrib (struct zserv *client, u_short length, |
| 1015 | vrf_id_t vrf_id) |
Everton Marques | 4e5275b | 2014-07-01 15:15:52 -0300 | [diff] [blame] | 1016 | { |
| 1017 | struct in_addr addr; |
David Lamparter | bd07812 | 2015-01-06 19:53:24 +0100 | [diff] [blame] | 1018 | struct rib *rib; |
Everton Marques | 4e5275b | 2014-07-01 15:15:52 -0300 | [diff] [blame] | 1019 | |
| 1020 | addr.s_addr = stream_get_ipv4 (client->ibuf); |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1021 | rib = rib_match_ipv4_multicast (addr, NULL, vrf_id); |
David Lamparter | bd07812 | 2015-01-06 19:53:24 +0100 | [diff] [blame] | 1022 | return zsend_ipv4_nexthop_lookup_mrib (client, addr, rib); |
Everton Marques | 4e5275b | 2014-07-01 15:15:52 -0300 | [diff] [blame] | 1023 | } |
| 1024 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1025 | /* Nexthop lookup for IPv4. */ |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1026 | static int |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1027 | zread_ipv4_import_lookup (struct zserv *client, u_short length, |
| 1028 | vrf_id_t vrf_id) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1029 | { |
| 1030 | struct prefix_ipv4 p; |
| 1031 | |
| 1032 | p.family = AF_INET; |
| 1033 | p.prefixlen = stream_getc (client->ibuf); |
| 1034 | p.prefix.s_addr = stream_get_ipv4 (client->ibuf); |
| 1035 | |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1036 | return zsend_ipv4_import_lookup (client, &p, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1037 | } |
| 1038 | |
| 1039 | #ifdef HAVE_IPV6 |
| 1040 | /* Zebra server IPv6 prefix add function. */ |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1041 | static int |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1042 | zread_ipv6_add (struct zserv *client, u_short length, vrf_id_t vrf_id) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1043 | { |
| 1044 | int i; |
| 1045 | struct stream *s; |
| 1046 | struct zapi_ipv6 api; |
| 1047 | struct in6_addr nexthop; |
| 1048 | unsigned long ifindex; |
| 1049 | struct prefix_ipv6 p; |
| 1050 | |
| 1051 | s = client->ibuf; |
| 1052 | ifindex = 0; |
| 1053 | memset (&nexthop, 0, sizeof (struct in6_addr)); |
| 1054 | |
| 1055 | /* Type, flags, message. */ |
| 1056 | api.type = stream_getc (s); |
| 1057 | api.flags = stream_getc (s); |
| 1058 | api.message = stream_getc (s); |
G.Balaji | f768f36 | 2011-11-26 22:10:39 +0400 | [diff] [blame] | 1059 | api.safi = stream_getw (s); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1060 | |
| 1061 | /* IPv4 prefix. */ |
| 1062 | memset (&p, 0, sizeof (struct prefix_ipv6)); |
| 1063 | p.family = AF_INET6; |
| 1064 | p.prefixlen = stream_getc (s); |
| 1065 | stream_get (&p.prefix, s, PSIZE (p.prefixlen)); |
| 1066 | |
| 1067 | /* Nexthop, ifindex, distance, metric. */ |
| 1068 | if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP)) |
| 1069 | { |
| 1070 | u_char nexthop_type; |
| 1071 | |
| 1072 | api.nexthop_num = stream_getc (s); |
| 1073 | for (i = 0; i < api.nexthop_num; i++) |
| 1074 | { |
| 1075 | nexthop_type = stream_getc (s); |
| 1076 | |
| 1077 | switch (nexthop_type) |
| 1078 | { |
| 1079 | case ZEBRA_NEXTHOP_IPV6: |
| 1080 | stream_get (&nexthop, s, 16); |
| 1081 | break; |
| 1082 | case ZEBRA_NEXTHOP_IFINDEX: |
| 1083 | ifindex = stream_getl (s); |
| 1084 | break; |
| 1085 | } |
| 1086 | } |
| 1087 | } |
| 1088 | |
| 1089 | if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE)) |
| 1090 | api.distance = stream_getc (s); |
| 1091 | else |
| 1092 | api.distance = 0; |
| 1093 | |
| 1094 | if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC)) |
| 1095 | api.metric = stream_getl (s); |
| 1096 | else |
| 1097 | api.metric = 0; |
Timo Teräs | b11f3b5 | 2015-11-02 16:50:07 +0200 | [diff] [blame] | 1098 | |
| 1099 | if (CHECK_FLAG (api.message, ZAPI_MESSAGE_MTU)) |
| 1100 | api.mtu = stream_getl (s); |
| 1101 | else |
| 1102 | api.mtu = 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1103 | |
| 1104 | if (IN6_IS_ADDR_UNSPECIFIED (&nexthop)) |
Feng Lu | 0d0686f | 2015-05-22 11:40:02 +0200 | [diff] [blame] | 1105 | rib_add_ipv6 (api.type, api.flags, &p, NULL, ifindex, |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1106 | vrf_id, zebrad.rtm_table_default, api.metric, |
Timo Teräs | b11f3b5 | 2015-11-02 16:50:07 +0200 | [diff] [blame] | 1107 | api.mtu, api.distance, api.safi); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1108 | else |
Feng Lu | 0d0686f | 2015-05-22 11:40:02 +0200 | [diff] [blame] | 1109 | rib_add_ipv6 (api.type, api.flags, &p, &nexthop, ifindex, |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1110 | vrf_id, zebrad.rtm_table_default, api.metric, |
Timo Teräs | b11f3b5 | 2015-11-02 16:50:07 +0200 | [diff] [blame] | 1111 | api.mtu, api.distance, api.safi); |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1112 | return 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1113 | } |
| 1114 | |
| 1115 | /* Zebra server IPv6 prefix delete function. */ |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1116 | static int |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1117 | zread_ipv6_delete (struct zserv *client, u_short length, vrf_id_t vrf_id) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1118 | { |
| 1119 | int i; |
| 1120 | struct stream *s; |
| 1121 | struct zapi_ipv6 api; |
| 1122 | struct in6_addr nexthop; |
| 1123 | unsigned long ifindex; |
| 1124 | struct prefix_ipv6 p; |
| 1125 | |
| 1126 | s = client->ibuf; |
| 1127 | ifindex = 0; |
| 1128 | memset (&nexthop, 0, sizeof (struct in6_addr)); |
| 1129 | |
| 1130 | /* Type, flags, message. */ |
| 1131 | api.type = stream_getc (s); |
| 1132 | api.flags = stream_getc (s); |
| 1133 | api.message = stream_getc (s); |
G.Balaji | f768f36 | 2011-11-26 22:10:39 +0400 | [diff] [blame] | 1134 | api.safi = stream_getw (s); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1135 | |
| 1136 | /* IPv4 prefix. */ |
| 1137 | memset (&p, 0, sizeof (struct prefix_ipv6)); |
| 1138 | p.family = AF_INET6; |
| 1139 | p.prefixlen = stream_getc (s); |
| 1140 | stream_get (&p.prefix, s, PSIZE (p.prefixlen)); |
| 1141 | |
| 1142 | /* Nexthop, ifindex, distance, metric. */ |
| 1143 | if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP)) |
| 1144 | { |
| 1145 | u_char nexthop_type; |
| 1146 | |
| 1147 | api.nexthop_num = stream_getc (s); |
| 1148 | for (i = 0; i < api.nexthop_num; i++) |
| 1149 | { |
| 1150 | nexthop_type = stream_getc (s); |
| 1151 | |
| 1152 | switch (nexthop_type) |
| 1153 | { |
| 1154 | case ZEBRA_NEXTHOP_IPV6: |
| 1155 | stream_get (&nexthop, s, 16); |
| 1156 | break; |
| 1157 | case ZEBRA_NEXTHOP_IFINDEX: |
| 1158 | ifindex = stream_getl (s); |
| 1159 | break; |
| 1160 | } |
| 1161 | } |
| 1162 | } |
| 1163 | |
| 1164 | if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE)) |
| 1165 | api.distance = stream_getc (s); |
| 1166 | else |
| 1167 | api.distance = 0; |
| 1168 | if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC)) |
| 1169 | api.metric = stream_getl (s); |
| 1170 | else |
| 1171 | api.metric = 0; |
| 1172 | |
| 1173 | if (IN6_IS_ADDR_UNSPECIFIED (&nexthop)) |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1174 | rib_delete_ipv6 (api.type, api.flags, &p, NULL, ifindex, vrf_id, |
Feng Lu | 0d0686f | 2015-05-22 11:40:02 +0200 | [diff] [blame] | 1175 | api.safi); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1176 | else |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1177 | rib_delete_ipv6 (api.type, api.flags, &p, &nexthop, ifindex, vrf_id, |
Feng Lu | 0d0686f | 2015-05-22 11:40:02 +0200 | [diff] [blame] | 1178 | api.safi); |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1179 | return 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1180 | } |
| 1181 | |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1182 | static int |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1183 | zread_ipv6_nexthop_lookup (struct zserv *client, u_short length, |
| 1184 | vrf_id_t vrf_id) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1185 | { |
| 1186 | struct in6_addr addr; |
| 1187 | char buf[BUFSIZ]; |
| 1188 | |
| 1189 | stream_get (&addr, client->ibuf, 16); |
Christian Franke | a520708 | 2013-04-11 08:24:29 +0000 | [diff] [blame] | 1190 | if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV) |
| 1191 | zlog_debug("%s: looking up %s", __func__, |
| 1192 | inet_ntop (AF_INET6, &addr, buf, BUFSIZ)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1193 | |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1194 | return zsend_ipv6_nexthop_lookup (client, &addr, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1195 | } |
| 1196 | #endif /* HAVE_IPV6 */ |
| 1197 | |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 1198 | /* Register zebra server router-id information. Send current router-id */ |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1199 | static int |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1200 | zread_router_id_add (struct zserv *client, u_short length, vrf_id_t vrf_id) |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 1201 | { |
| 1202 | struct prefix p; |
| 1203 | |
| 1204 | /* Router-id information is needed. */ |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1205 | vrf_bitmap_set (client->ridinfo, vrf_id); |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 1206 | |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1207 | router_id_get (&p, vrf_id); |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 1208 | |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1209 | return zsend_router_id_update (client, &p, vrf_id); |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 1210 | } |
| 1211 | |
| 1212 | /* Unregister zebra server router-id information. */ |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1213 | static int |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1214 | zread_router_id_delete (struct zserv *client, u_short length, vrf_id_t vrf_id) |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 1215 | { |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1216 | vrf_bitmap_unset (client->ridinfo, vrf_id); |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1217 | return 0; |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 1218 | } |
| 1219 | |
Vyacheslav Trushkin | 2ea1ab1 | 2011-12-11 18:48:47 +0400 | [diff] [blame] | 1220 | /* Tie up route-type and client->sock */ |
| 1221 | static void |
| 1222 | zread_hello (struct zserv *client) |
| 1223 | { |
| 1224 | /* type of protocol (lib/zebra.h) */ |
| 1225 | u_char proto; |
| 1226 | proto = stream_getc (client->ibuf); |
| 1227 | |
| 1228 | /* accept only dynamic routing protocols */ |
| 1229 | if ((proto < ZEBRA_ROUTE_MAX) |
| 1230 | && (proto > ZEBRA_ROUTE_STATIC)) |
| 1231 | { |
| 1232 | zlog_notice ("client %d says hello and bids fair to announce only %s routes", |
| 1233 | client->sock, zebra_route_string(proto)); |
| 1234 | |
| 1235 | /* if route-type was binded by other client */ |
| 1236 | if (route_type_oaths[proto]) |
| 1237 | zlog_warn ("sender of %s routes changed %c->%c", |
| 1238 | zebra_route_string(proto), route_type_oaths[proto], |
| 1239 | client->sock); |
| 1240 | |
| 1241 | route_type_oaths[proto] = client->sock; |
| 1242 | } |
| 1243 | } |
| 1244 | |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1245 | /* Unregister all information in a VRF. */ |
| 1246 | static int |
| 1247 | zread_vrf_unregister (struct zserv *client, u_short length, vrf_id_t vrf_id) |
| 1248 | { |
| 1249 | int i; |
| 1250 | |
| 1251 | for (i = 0; i < ZEBRA_ROUTE_MAX; i++) |
| 1252 | vrf_bitmap_unset (client->redist[i], vrf_id); |
| 1253 | vrf_bitmap_unset (client->redist_default, vrf_id); |
| 1254 | vrf_bitmap_unset (client->ifinfo, vrf_id); |
| 1255 | vrf_bitmap_unset (client->ridinfo, vrf_id); |
| 1256 | |
| 1257 | return 0; |
| 1258 | } |
| 1259 | |
Vyacheslav Trushkin | 2ea1ab1 | 2011-12-11 18:48:47 +0400 | [diff] [blame] | 1260 | /* If client sent routes of specific type, zebra removes it |
| 1261 | * and returns number of deleted routes. |
| 1262 | */ |
| 1263 | static void |
| 1264 | zebra_score_rib (int client_sock) |
| 1265 | { |
| 1266 | int i; |
| 1267 | |
| 1268 | for (i = ZEBRA_ROUTE_RIP; i < ZEBRA_ROUTE_MAX; i++) |
| 1269 | if (client_sock == route_type_oaths[i]) |
| 1270 | { |
| 1271 | zlog_notice ("client %d disconnected. %lu %s routes removed from the rib", |
| 1272 | client_sock, rib_score_proto (i), zebra_route_string (i)); |
| 1273 | route_type_oaths[i] = 0; |
| 1274 | break; |
| 1275 | } |
| 1276 | } |
| 1277 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1278 | /* Close zebra client. */ |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 1279 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1280 | zebra_client_close (struct zserv *client) |
| 1281 | { |
| 1282 | /* Close file descriptor. */ |
| 1283 | if (client->sock) |
| 1284 | { |
| 1285 | close (client->sock); |
Vyacheslav Trushkin | 2ea1ab1 | 2011-12-11 18:48:47 +0400 | [diff] [blame] | 1286 | zebra_score_rib (client->sock); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1287 | client->sock = -1; |
| 1288 | } |
| 1289 | |
| 1290 | /* Free stream buffers. */ |
| 1291 | if (client->ibuf) |
| 1292 | stream_free (client->ibuf); |
| 1293 | if (client->obuf) |
| 1294 | stream_free (client->obuf); |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1295 | if (client->wb) |
| 1296 | buffer_free(client->wb); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1297 | |
| 1298 | /* Release threads. */ |
| 1299 | if (client->t_read) |
| 1300 | thread_cancel (client->t_read); |
| 1301 | if (client->t_write) |
| 1302 | thread_cancel (client->t_write); |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1303 | if (client->t_suicide) |
| 1304 | thread_cancel (client->t_suicide); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1305 | |
| 1306 | /* Free client structure. */ |
paul | b21b19c | 2003-06-15 01:28:29 +0000 | [diff] [blame] | 1307 | listnode_delete (zebrad.client_list, client); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1308 | XFREE (0, client); |
| 1309 | } |
| 1310 | |
| 1311 | /* Make new client. */ |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 1312 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1313 | zebra_client_create (int sock) |
| 1314 | { |
| 1315 | struct zserv *client; |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1316 | int i; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1317 | |
| 1318 | client = XCALLOC (0, sizeof (struct zserv)); |
| 1319 | |
| 1320 | /* Make client input/output buffer. */ |
| 1321 | client->sock = sock; |
| 1322 | client->ibuf = stream_new (ZEBRA_MAX_PACKET_SIZ); |
| 1323 | client->obuf = stream_new (ZEBRA_MAX_PACKET_SIZ); |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1324 | client->wb = buffer_new(0); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1325 | |
| 1326 | /* Set table number. */ |
paul | b21b19c | 2003-06-15 01:28:29 +0000 | [diff] [blame] | 1327 | client->rtm_table = zebrad.rtm_table_default; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1328 | |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1329 | /* Initialize flags */ |
| 1330 | for (i = 0; i < ZEBRA_ROUTE_MAX; i++) |
| 1331 | client->redist[i] = vrf_bitmap_init (); |
| 1332 | client->redist_default = vrf_bitmap_init (); |
| 1333 | client->ifinfo = vrf_bitmap_init (); |
| 1334 | client->ridinfo = vrf_bitmap_init (); |
| 1335 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1336 | /* Add this client to linked list. */ |
paul | b21b19c | 2003-06-15 01:28:29 +0000 | [diff] [blame] | 1337 | listnode_add (zebrad.client_list, client); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1338 | |
| 1339 | /* Make new read thread. */ |
| 1340 | zebra_event (ZEBRA_READ, sock, client); |
| 1341 | } |
| 1342 | |
| 1343 | /* Handler of zebra service request. */ |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 1344 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1345 | zebra_client_read (struct thread *thread) |
| 1346 | { |
| 1347 | int sock; |
| 1348 | struct zserv *client; |
ajs | 57a1477 | 2005-04-10 15:01:56 +0000 | [diff] [blame] | 1349 | size_t already; |
paul | c1b9800 | 2006-01-16 01:54:02 +0000 | [diff] [blame] | 1350 | uint16_t length, command; |
| 1351 | uint8_t marker, version; |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1352 | vrf_id_t vrf_id; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1353 | |
| 1354 | /* Get thread data. Reset reading thread because I'm running. */ |
| 1355 | sock = THREAD_FD (thread); |
| 1356 | client = THREAD_ARG (thread); |
| 1357 | client->t_read = NULL; |
| 1358 | |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1359 | if (client->t_suicide) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1360 | { |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1361 | zebra_client_close(client); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1362 | return -1; |
| 1363 | } |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1364 | |
| 1365 | /* Read length and command (if we don't have it already). */ |
ajs | 57a1477 | 2005-04-10 15:01:56 +0000 | [diff] [blame] | 1366 | if ((already = stream_get_endp(client->ibuf)) < ZEBRA_HEADER_SIZE) |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1367 | { |
ajs | 57a1477 | 2005-04-10 15:01:56 +0000 | [diff] [blame] | 1368 | ssize_t nbyte; |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1369 | if (((nbyte = stream_read_try (client->ibuf, sock, |
ajs | 57a1477 | 2005-04-10 15:01:56 +0000 | [diff] [blame] | 1370 | ZEBRA_HEADER_SIZE-already)) == 0) || |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1371 | (nbyte == -1)) |
| 1372 | { |
| 1373 | if (IS_ZEBRA_DEBUG_EVENT) |
| 1374 | zlog_debug ("connection closed socket [%d]", sock); |
| 1375 | zebra_client_close (client); |
| 1376 | return -1; |
| 1377 | } |
ajs | 57a1477 | 2005-04-10 15:01:56 +0000 | [diff] [blame] | 1378 | if (nbyte != (ssize_t)(ZEBRA_HEADER_SIZE-already)) |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1379 | { |
| 1380 | /* Try again later. */ |
| 1381 | zebra_event (ZEBRA_READ, sock, client); |
| 1382 | return 0; |
| 1383 | } |
ajs | 57a1477 | 2005-04-10 15:01:56 +0000 | [diff] [blame] | 1384 | already = ZEBRA_HEADER_SIZE; |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1385 | } |
| 1386 | |
| 1387 | /* Reset to read from the beginning of the incoming packet. */ |
| 1388 | stream_set_getp(client->ibuf, 0); |
| 1389 | |
paul | c1b9800 | 2006-01-16 01:54:02 +0000 | [diff] [blame] | 1390 | /* Fetch header values */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1391 | length = stream_getw (client->ibuf); |
paul | c1b9800 | 2006-01-16 01:54:02 +0000 | [diff] [blame] | 1392 | marker = stream_getc (client->ibuf); |
| 1393 | version = stream_getc (client->ibuf); |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1394 | vrf_id = stream_getw (client->ibuf); |
paul | c1b9800 | 2006-01-16 01:54:02 +0000 | [diff] [blame] | 1395 | command = stream_getw (client->ibuf); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1396 | |
paul | c1b9800 | 2006-01-16 01:54:02 +0000 | [diff] [blame] | 1397 | if (marker != ZEBRA_HEADER_MARKER || version != ZSERV_VERSION) |
| 1398 | { |
| 1399 | zlog_err("%s: socket %d version mismatch, marker %d, version %d", |
| 1400 | __func__, sock, marker, version); |
| 1401 | zebra_client_close (client); |
| 1402 | return -1; |
| 1403 | } |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1404 | if (length < ZEBRA_HEADER_SIZE) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1405 | { |
ajs | 57a1477 | 2005-04-10 15:01:56 +0000 | [diff] [blame] | 1406 | zlog_warn("%s: socket %d message length %u is less than header size %d", |
| 1407 | __func__, sock, length, ZEBRA_HEADER_SIZE); |
| 1408 | zebra_client_close (client); |
| 1409 | return -1; |
| 1410 | } |
| 1411 | if (length > STREAM_SIZE(client->ibuf)) |
| 1412 | { |
| 1413 | zlog_warn("%s: socket %d message length %u exceeds buffer size %lu", |
| 1414 | __func__, sock, length, (u_long)STREAM_SIZE(client->ibuf)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1415 | zebra_client_close (client); |
| 1416 | return -1; |
| 1417 | } |
| 1418 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1419 | /* Read rest of data. */ |
ajs | 57a1477 | 2005-04-10 15:01:56 +0000 | [diff] [blame] | 1420 | if (already < length) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1421 | { |
ajs | 57a1477 | 2005-04-10 15:01:56 +0000 | [diff] [blame] | 1422 | ssize_t nbyte; |
| 1423 | if (((nbyte = stream_read_try (client->ibuf, sock, |
| 1424 | length-already)) == 0) || |
| 1425 | (nbyte == -1)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1426 | { |
| 1427 | if (IS_ZEBRA_DEBUG_EVENT) |
ajs | b617800 | 2004-12-07 21:12:56 +0000 | [diff] [blame] | 1428 | zlog_debug ("connection closed [%d] when reading zebra data", sock); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1429 | zebra_client_close (client); |
| 1430 | return -1; |
| 1431 | } |
ajs | 57a1477 | 2005-04-10 15:01:56 +0000 | [diff] [blame] | 1432 | if (nbyte != (ssize_t)(length-already)) |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1433 | { |
| 1434 | /* Try again later. */ |
| 1435 | zebra_event (ZEBRA_READ, sock, client); |
| 1436 | return 0; |
| 1437 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1438 | } |
| 1439 | |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1440 | length -= ZEBRA_HEADER_SIZE; |
| 1441 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1442 | /* Debug packet information. */ |
| 1443 | if (IS_ZEBRA_DEBUG_EVENT) |
ajs | b617800 | 2004-12-07 21:12:56 +0000 | [diff] [blame] | 1444 | zlog_debug ("zebra message comes from socket [%d]", sock); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1445 | |
| 1446 | if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV) |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1447 | zlog_debug ("zebra message received [%s] %d in VRF %u", |
| 1448 | zserv_command_string (command), length, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1449 | |
| 1450 | switch (command) |
| 1451 | { |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 1452 | case ZEBRA_ROUTER_ID_ADD: |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1453 | zread_router_id_add (client, length, vrf_id); |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 1454 | break; |
| 1455 | case ZEBRA_ROUTER_ID_DELETE: |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1456 | zread_router_id_delete (client, length, vrf_id); |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 1457 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1458 | case ZEBRA_INTERFACE_ADD: |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1459 | zread_interface_add (client, length, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1460 | break; |
| 1461 | case ZEBRA_INTERFACE_DELETE: |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1462 | zread_interface_delete (client, length, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1463 | break; |
| 1464 | case ZEBRA_IPV4_ROUTE_ADD: |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1465 | zread_ipv4_add (client, length, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1466 | break; |
| 1467 | case ZEBRA_IPV4_ROUTE_DELETE: |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1468 | zread_ipv4_delete (client, length, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1469 | break; |
| 1470 | #ifdef HAVE_IPV6 |
| 1471 | case ZEBRA_IPV6_ROUTE_ADD: |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1472 | zread_ipv6_add (client, length, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1473 | break; |
| 1474 | case ZEBRA_IPV6_ROUTE_DELETE: |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1475 | zread_ipv6_delete (client, length, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1476 | break; |
| 1477 | #endif /* HAVE_IPV6 */ |
| 1478 | case ZEBRA_REDISTRIBUTE_ADD: |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1479 | zebra_redistribute_add (command, client, length, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1480 | break; |
| 1481 | case ZEBRA_REDISTRIBUTE_DELETE: |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1482 | zebra_redistribute_delete (command, client, length, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1483 | break; |
| 1484 | case ZEBRA_REDISTRIBUTE_DEFAULT_ADD: |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1485 | zebra_redistribute_default_add (command, client, length, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1486 | break; |
| 1487 | case ZEBRA_REDISTRIBUTE_DEFAULT_DELETE: |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1488 | zebra_redistribute_default_delete (command, client, length, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1489 | break; |
| 1490 | case ZEBRA_IPV4_NEXTHOP_LOOKUP: |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1491 | zread_ipv4_nexthop_lookup (client, length, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1492 | break; |
Everton Marques | 4e5275b | 2014-07-01 15:15:52 -0300 | [diff] [blame] | 1493 | case ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB: |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1494 | zread_ipv4_nexthop_lookup_mrib (client, length, vrf_id); |
Everton Marques | 4e5275b | 2014-07-01 15:15:52 -0300 | [diff] [blame] | 1495 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1496 | #ifdef HAVE_IPV6 |
| 1497 | case ZEBRA_IPV6_NEXTHOP_LOOKUP: |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1498 | zread_ipv6_nexthop_lookup (client, length, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1499 | break; |
| 1500 | #endif /* HAVE_IPV6 */ |
| 1501 | case ZEBRA_IPV4_IMPORT_LOOKUP: |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1502 | zread_ipv4_import_lookup (client, length, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1503 | break; |
Vyacheslav Trushkin | 2ea1ab1 | 2011-12-11 18:48:47 +0400 | [diff] [blame] | 1504 | case ZEBRA_HELLO: |
| 1505 | zread_hello (client); |
| 1506 | break; |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1507 | case ZEBRA_VRF_UNREGISTER: |
| 1508 | zread_vrf_unregister (client, length, vrf_id); |
| 1509 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1510 | default: |
| 1511 | zlog_info ("Zebra received unknown command %d", command); |
| 1512 | break; |
| 1513 | } |
| 1514 | |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1515 | if (client->t_suicide) |
| 1516 | { |
| 1517 | /* No need to wait for thread callback, just kill immediately. */ |
| 1518 | zebra_client_close(client); |
| 1519 | return -1; |
| 1520 | } |
| 1521 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1522 | stream_reset (client->ibuf); |
| 1523 | zebra_event (ZEBRA_READ, sock, client); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1524 | return 0; |
| 1525 | } |
| 1526 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1527 | |
| 1528 | /* Accept code of zebra server socket. */ |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 1529 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1530 | zebra_accept (struct thread *thread) |
| 1531 | { |
| 1532 | int accept_sock; |
| 1533 | int client_sock; |
| 1534 | struct sockaddr_in client; |
| 1535 | socklen_t len; |
| 1536 | |
| 1537 | accept_sock = THREAD_FD (thread); |
| 1538 | |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1539 | /* Reregister myself. */ |
| 1540 | zebra_event (ZEBRA_SERV, accept_sock, NULL); |
| 1541 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1542 | len = sizeof (struct sockaddr_in); |
| 1543 | client_sock = accept (accept_sock, (struct sockaddr *) &client, &len); |
| 1544 | |
| 1545 | if (client_sock < 0) |
| 1546 | { |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 1547 | zlog_warn ("Can't accept zebra socket: %s", safe_strerror (errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1548 | return -1; |
| 1549 | } |
| 1550 | |
paul | ccf3557 | 2003-03-01 11:42:20 +0000 | [diff] [blame] | 1551 | /* Make client socket non-blocking. */ |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1552 | set_nonblocking(client_sock); |
paul | 865b852 | 2005-01-05 08:30:35 +0000 | [diff] [blame] | 1553 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1554 | /* Create new zebra client. */ |
| 1555 | zebra_client_create (client_sock); |
| 1556 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1557 | return 0; |
| 1558 | } |
| 1559 | |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 1560 | #ifdef HAVE_TCP_ZEBRA |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1561 | /* Make zebra's server socket. */ |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 1562 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1563 | zebra_serv () |
| 1564 | { |
| 1565 | int ret; |
| 1566 | int accept_sock; |
| 1567 | struct sockaddr_in addr; |
| 1568 | |
| 1569 | accept_sock = socket (AF_INET, SOCK_STREAM, 0); |
| 1570 | |
| 1571 | if (accept_sock < 0) |
| 1572 | { |
paul | 3d1dc85 | 2005-04-05 00:45:23 +0000 | [diff] [blame] | 1573 | zlog_warn ("Can't create zserv stream socket: %s", |
| 1574 | safe_strerror (errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1575 | zlog_warn ("zebra can't provice full functionality due to above error"); |
| 1576 | return; |
| 1577 | } |
| 1578 | |
Vyacheslav Trushkin | 2ea1ab1 | 2011-12-11 18:48:47 +0400 | [diff] [blame] | 1579 | memset (&route_type_oaths, 0, sizeof (route_type_oaths)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1580 | memset (&addr, 0, sizeof (struct sockaddr_in)); |
| 1581 | addr.sin_family = AF_INET; |
| 1582 | addr.sin_port = htons (ZEBRA_PORT); |
Paul Jakma | 6f0e3f6 | 2007-05-10 02:38:51 +0000 | [diff] [blame] | 1583 | #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1584 | addr.sin_len = sizeof (struct sockaddr_in); |
Paul Jakma | 6f0e3f6 | 2007-05-10 02:38:51 +0000 | [diff] [blame] | 1585 | #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1586 | addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK); |
| 1587 | |
| 1588 | sockopt_reuseaddr (accept_sock); |
| 1589 | sockopt_reuseport (accept_sock); |
| 1590 | |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 1591 | if ( zserv_privs.change(ZPRIVS_RAISE) ) |
| 1592 | zlog (NULL, LOG_ERR, "Can't raise privileges"); |
| 1593 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1594 | ret = bind (accept_sock, (struct sockaddr *)&addr, |
| 1595 | sizeof (struct sockaddr_in)); |
| 1596 | if (ret < 0) |
| 1597 | { |
paul | 3d1dc85 | 2005-04-05 00:45:23 +0000 | [diff] [blame] | 1598 | zlog_warn ("Can't bind to stream socket: %s", |
| 1599 | safe_strerror (errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1600 | zlog_warn ("zebra can't provice full functionality due to above error"); |
| 1601 | close (accept_sock); /* Avoid sd leak. */ |
| 1602 | return; |
| 1603 | } |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 1604 | |
| 1605 | if ( zserv_privs.change(ZPRIVS_LOWER) ) |
| 1606 | zlog (NULL, LOG_ERR, "Can't lower privileges"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1607 | |
| 1608 | ret = listen (accept_sock, 1); |
| 1609 | if (ret < 0) |
| 1610 | { |
paul | 3d1dc85 | 2005-04-05 00:45:23 +0000 | [diff] [blame] | 1611 | zlog_warn ("Can't listen to stream socket: %s", |
| 1612 | safe_strerror (errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1613 | zlog_warn ("zebra can't provice full functionality due to above error"); |
| 1614 | close (accept_sock); /* Avoid sd leak. */ |
| 1615 | return; |
| 1616 | } |
| 1617 | |
| 1618 | zebra_event (ZEBRA_SERV, accept_sock, NULL); |
| 1619 | } |
David Lamparter | 4b6c332 | 2015-04-21 09:47:57 +0200 | [diff] [blame] | 1620 | #else /* HAVE_TCP_ZEBRA */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1621 | |
| 1622 | /* For sockaddr_un. */ |
| 1623 | #include <sys/un.h> |
| 1624 | |
| 1625 | /* zebra server UNIX domain socket. */ |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 1626 | static void |
hasso | fce954f | 2004-10-07 20:29:24 +0000 | [diff] [blame] | 1627 | zebra_serv_un (const char *path) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1628 | { |
| 1629 | int ret; |
| 1630 | int sock, len; |
| 1631 | struct sockaddr_un serv; |
| 1632 | mode_t old_mask; |
| 1633 | |
| 1634 | /* First of all, unlink existing socket */ |
| 1635 | unlink (path); |
| 1636 | |
| 1637 | /* Set umask */ |
| 1638 | old_mask = umask (0077); |
| 1639 | |
| 1640 | /* Make UNIX domain socket. */ |
| 1641 | sock = socket (AF_UNIX, SOCK_STREAM, 0); |
| 1642 | if (sock < 0) |
| 1643 | { |
paul | 3d1dc85 | 2005-04-05 00:45:23 +0000 | [diff] [blame] | 1644 | zlog_warn ("Can't create zserv unix socket: %s", |
| 1645 | safe_strerror (errno)); |
| 1646 | zlog_warn ("zebra can't provide full functionality due to above error"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1647 | return; |
| 1648 | } |
| 1649 | |
Vyacheslav Trushkin | 2ea1ab1 | 2011-12-11 18:48:47 +0400 | [diff] [blame] | 1650 | memset (&route_type_oaths, 0, sizeof (route_type_oaths)); |
| 1651 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1652 | /* Make server socket. */ |
| 1653 | memset (&serv, 0, sizeof (struct sockaddr_un)); |
| 1654 | serv.sun_family = AF_UNIX; |
| 1655 | strncpy (serv.sun_path, path, strlen (path)); |
Paul Jakma | 6f0e3f6 | 2007-05-10 02:38:51 +0000 | [diff] [blame] | 1656 | #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1657 | len = serv.sun_len = SUN_LEN(&serv); |
| 1658 | #else |
| 1659 | len = sizeof (serv.sun_family) + strlen (serv.sun_path); |
Paul Jakma | 6f0e3f6 | 2007-05-10 02:38:51 +0000 | [diff] [blame] | 1660 | #endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1661 | |
| 1662 | ret = bind (sock, (struct sockaddr *) &serv, len); |
| 1663 | if (ret < 0) |
| 1664 | { |
paul | 3d1dc85 | 2005-04-05 00:45:23 +0000 | [diff] [blame] | 1665 | zlog_warn ("Can't bind to unix socket %s: %s", |
| 1666 | path, safe_strerror (errno)); |
| 1667 | zlog_warn ("zebra can't provide full functionality due to above error"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1668 | close (sock); |
| 1669 | return; |
| 1670 | } |
| 1671 | |
| 1672 | ret = listen (sock, 5); |
| 1673 | if (ret < 0) |
| 1674 | { |
paul | 3d1dc85 | 2005-04-05 00:45:23 +0000 | [diff] [blame] | 1675 | zlog_warn ("Can't listen to unix socket %s: %s", |
| 1676 | path, safe_strerror (errno)); |
| 1677 | zlog_warn ("zebra can't provide full functionality due to above error"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1678 | close (sock); |
| 1679 | return; |
| 1680 | } |
| 1681 | |
| 1682 | umask (old_mask); |
| 1683 | |
| 1684 | zebra_event (ZEBRA_SERV, sock, NULL); |
| 1685 | } |
David Lamparter | 4b6c332 | 2015-04-21 09:47:57 +0200 | [diff] [blame] | 1686 | #endif /* HAVE_TCP_ZEBRA */ |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 1687 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1688 | |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 1689 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1690 | zebra_event (enum event event, int sock, struct zserv *client) |
| 1691 | { |
| 1692 | switch (event) |
| 1693 | { |
| 1694 | case ZEBRA_SERV: |
paul | b21b19c | 2003-06-15 01:28:29 +0000 | [diff] [blame] | 1695 | thread_add_read (zebrad.master, zebra_accept, client, sock); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1696 | break; |
| 1697 | case ZEBRA_READ: |
| 1698 | client->t_read = |
paul | b21b19c | 2003-06-15 01:28:29 +0000 | [diff] [blame] | 1699 | thread_add_read (zebrad.master, zebra_client_read, client, sock); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1700 | break; |
| 1701 | case ZEBRA_WRITE: |
| 1702 | /**/ |
| 1703 | break; |
| 1704 | } |
| 1705 | } |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 1706 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1707 | /* Display default rtm_table for all clients. */ |
| 1708 | DEFUN (show_table, |
| 1709 | show_table_cmd, |
| 1710 | "show table", |
| 1711 | SHOW_STR |
| 1712 | "default routing table to use for all clients\n") |
| 1713 | { |
paul | b21b19c | 2003-06-15 01:28:29 +0000 | [diff] [blame] | 1714 | vty_out (vty, "table %d%s", zebrad.rtm_table_default, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1715 | VTY_NEWLINE); |
| 1716 | return CMD_SUCCESS; |
| 1717 | } |
| 1718 | |
| 1719 | DEFUN (config_table, |
| 1720 | config_table_cmd, |
| 1721 | "table TABLENO", |
| 1722 | "Configure target kernel routing table\n" |
| 1723 | "TABLE integer\n") |
| 1724 | { |
paul | b21b19c | 2003-06-15 01:28:29 +0000 | [diff] [blame] | 1725 | zebrad.rtm_table_default = strtol (argv[0], (char**)0, 10); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1726 | return CMD_SUCCESS; |
| 1727 | } |
| 1728 | |
hasso | 647e4f1 | 2003-05-25 11:43:52 +0000 | [diff] [blame] | 1729 | DEFUN (ip_forwarding, |
| 1730 | ip_forwarding_cmd, |
| 1731 | "ip forwarding", |
| 1732 | IP_STR |
| 1733 | "Turn on IP forwarding") |
| 1734 | { |
| 1735 | int ret; |
| 1736 | |
| 1737 | ret = ipforward (); |
hasso | b71f00f | 2004-10-13 12:20:35 +0000 | [diff] [blame] | 1738 | if (ret == 0) |
| 1739 | ret = ipforward_on (); |
hasso | 647e4f1 | 2003-05-25 11:43:52 +0000 | [diff] [blame] | 1740 | |
hasso | 647e4f1 | 2003-05-25 11:43:52 +0000 | [diff] [blame] | 1741 | if (ret == 0) |
| 1742 | { |
| 1743 | vty_out (vty, "Can't turn on IP forwarding%s", VTY_NEWLINE); |
| 1744 | return CMD_WARNING; |
| 1745 | } |
| 1746 | |
| 1747 | return CMD_SUCCESS; |
| 1748 | } |
| 1749 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1750 | DEFUN (no_ip_forwarding, |
| 1751 | no_ip_forwarding_cmd, |
| 1752 | "no ip forwarding", |
| 1753 | NO_STR |
| 1754 | IP_STR |
| 1755 | "Turn off IP forwarding") |
| 1756 | { |
| 1757 | int ret; |
| 1758 | |
| 1759 | ret = ipforward (); |
hasso | b71f00f | 2004-10-13 12:20:35 +0000 | [diff] [blame] | 1760 | if (ret != 0) |
| 1761 | ret = ipforward_off (); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1762 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1763 | if (ret != 0) |
| 1764 | { |
| 1765 | vty_out (vty, "Can't turn off IP forwarding%s", VTY_NEWLINE); |
| 1766 | return CMD_WARNING; |
| 1767 | } |
| 1768 | |
| 1769 | return CMD_SUCCESS; |
| 1770 | } |
| 1771 | |
| 1772 | /* This command is for debugging purpose. */ |
| 1773 | DEFUN (show_zebra_client, |
| 1774 | show_zebra_client_cmd, |
| 1775 | "show zebra client", |
| 1776 | SHOW_STR |
| 1777 | "Zebra information" |
| 1778 | "Client information") |
| 1779 | { |
hasso | 52dc7ee | 2004-09-23 19:18:23 +0000 | [diff] [blame] | 1780 | struct listnode *node; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1781 | struct zserv *client; |
| 1782 | |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 1783 | for (ALL_LIST_ELEMENTS_RO (zebrad.client_list, node, client)) |
| 1784 | vty_out (vty, "Client fd %d%s", client->sock, VTY_NEWLINE); |
| 1785 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1786 | return CMD_SUCCESS; |
| 1787 | } |
| 1788 | |
| 1789 | /* Table configuration write function. */ |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 1790 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1791 | config_write_table (struct vty *vty) |
| 1792 | { |
paul | b21b19c | 2003-06-15 01:28:29 +0000 | [diff] [blame] | 1793 | if (zebrad.rtm_table_default) |
| 1794 | vty_out (vty, "table %d%s", zebrad.rtm_table_default, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1795 | VTY_NEWLINE); |
| 1796 | return 0; |
| 1797 | } |
| 1798 | |
| 1799 | /* table node for routing tables. */ |
Stephen Hemminger | 7fc626d | 2008-12-01 11:10:34 -0800 | [diff] [blame] | 1800 | static struct cmd_node table_node = |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1801 | { |
| 1802 | TABLE_NODE, |
| 1803 | "", /* This node has no interface. */ |
| 1804 | 1 |
| 1805 | }; |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 1806 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1807 | /* Only display ip forwarding is enabled or not. */ |
| 1808 | DEFUN (show_ip_forwarding, |
| 1809 | show_ip_forwarding_cmd, |
| 1810 | "show ip forwarding", |
| 1811 | SHOW_STR |
| 1812 | IP_STR |
| 1813 | "IP forwarding status\n") |
| 1814 | { |
| 1815 | int ret; |
| 1816 | |
| 1817 | ret = ipforward (); |
| 1818 | |
| 1819 | if (ret == 0) |
| 1820 | vty_out (vty, "IP forwarding is off%s", VTY_NEWLINE); |
| 1821 | else |
| 1822 | vty_out (vty, "IP forwarding is on%s", VTY_NEWLINE); |
| 1823 | return CMD_SUCCESS; |
| 1824 | } |
| 1825 | |
| 1826 | #ifdef HAVE_IPV6 |
| 1827 | /* Only display ipv6 forwarding is enabled or not. */ |
| 1828 | DEFUN (show_ipv6_forwarding, |
| 1829 | show_ipv6_forwarding_cmd, |
| 1830 | "show ipv6 forwarding", |
| 1831 | SHOW_STR |
| 1832 | "IPv6 information\n" |
| 1833 | "Forwarding status\n") |
| 1834 | { |
| 1835 | int ret; |
| 1836 | |
| 1837 | ret = ipforward_ipv6 (); |
| 1838 | |
| 1839 | switch (ret) |
| 1840 | { |
| 1841 | case -1: |
| 1842 | vty_out (vty, "ipv6 forwarding is unknown%s", VTY_NEWLINE); |
| 1843 | break; |
| 1844 | case 0: |
| 1845 | vty_out (vty, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE); |
| 1846 | break; |
| 1847 | case 1: |
| 1848 | vty_out (vty, "ipv6 forwarding is %s%s", "on", VTY_NEWLINE); |
| 1849 | break; |
| 1850 | default: |
| 1851 | vty_out (vty, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE); |
| 1852 | break; |
| 1853 | } |
| 1854 | return CMD_SUCCESS; |
| 1855 | } |
| 1856 | |
hasso | 5590672 | 2004-02-11 22:42:16 +0000 | [diff] [blame] | 1857 | DEFUN (ipv6_forwarding, |
| 1858 | ipv6_forwarding_cmd, |
| 1859 | "ipv6 forwarding", |
| 1860 | IPV6_STR |
| 1861 | "Turn on IPv6 forwarding") |
| 1862 | { |
| 1863 | int ret; |
| 1864 | |
hasso | 41d3fc9 | 2004-04-06 11:59:00 +0000 | [diff] [blame] | 1865 | ret = ipforward_ipv6 (); |
hasso | b71f00f | 2004-10-13 12:20:35 +0000 | [diff] [blame] | 1866 | if (ret == 0) |
| 1867 | ret = ipforward_ipv6_on (); |
hasso | 41d3fc9 | 2004-04-06 11:59:00 +0000 | [diff] [blame] | 1868 | |
hasso | 41d3fc9 | 2004-04-06 11:59:00 +0000 | [diff] [blame] | 1869 | if (ret == 0) |
| 1870 | { |
hasso | 5590672 | 2004-02-11 22:42:16 +0000 | [diff] [blame] | 1871 | vty_out (vty, "Can't turn on IPv6 forwarding%s", VTY_NEWLINE); |
| 1872 | return CMD_WARNING; |
| 1873 | } |
| 1874 | |
| 1875 | return CMD_SUCCESS; |
| 1876 | } |
| 1877 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1878 | DEFUN (no_ipv6_forwarding, |
| 1879 | no_ipv6_forwarding_cmd, |
| 1880 | "no ipv6 forwarding", |
| 1881 | NO_STR |
hasso | 5590672 | 2004-02-11 22:42:16 +0000 | [diff] [blame] | 1882 | IPV6_STR |
| 1883 | "Turn off IPv6 forwarding") |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1884 | { |
| 1885 | int ret; |
| 1886 | |
hasso | 41d3fc9 | 2004-04-06 11:59:00 +0000 | [diff] [blame] | 1887 | ret = ipforward_ipv6 (); |
hasso | b71f00f | 2004-10-13 12:20:35 +0000 | [diff] [blame] | 1888 | if (ret != 0) |
| 1889 | ret = ipforward_ipv6_off (); |
hasso | 41d3fc9 | 2004-04-06 11:59:00 +0000 | [diff] [blame] | 1890 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1891 | if (ret != 0) |
| 1892 | { |
| 1893 | vty_out (vty, "Can't turn off IPv6 forwarding%s", VTY_NEWLINE); |
| 1894 | return CMD_WARNING; |
| 1895 | } |
| 1896 | |
| 1897 | return CMD_SUCCESS; |
| 1898 | } |
| 1899 | |
| 1900 | #endif /* HAVE_IPV6 */ |
| 1901 | |
| 1902 | /* IPForwarding configuration write function. */ |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1903 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1904 | config_write_forwarding (struct vty *vty) |
| 1905 | { |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 1906 | /* FIXME: Find better place for that. */ |
| 1907 | router_id_write (vty); |
| 1908 | |
paul | 3e0b3a5 | 2004-08-23 18:58:32 +0000 | [diff] [blame] | 1909 | if (ipforward ()) |
| 1910 | vty_out (vty, "ip forwarding%s", VTY_NEWLINE); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1911 | #ifdef HAVE_IPV6 |
paul | 3e0b3a5 | 2004-08-23 18:58:32 +0000 | [diff] [blame] | 1912 | if (ipforward_ipv6 ()) |
| 1913 | vty_out (vty, "ipv6 forwarding%s", VTY_NEWLINE); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1914 | #endif /* HAVE_IPV6 */ |
| 1915 | vty_out (vty, "!%s", VTY_NEWLINE); |
| 1916 | return 0; |
| 1917 | } |
| 1918 | |
| 1919 | /* table node for routing tables. */ |
Stephen Hemminger | 7fc626d | 2008-12-01 11:10:34 -0800 | [diff] [blame] | 1920 | static struct cmd_node forwarding_node = |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1921 | { |
| 1922 | FORWARDING_NODE, |
| 1923 | "", /* This node has no interface. */ |
| 1924 | 1 |
| 1925 | }; |
| 1926 | |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 1927 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1928 | /* Initialisation of zebra and installation of commands. */ |
| 1929 | void |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 1930 | zebra_init (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1931 | { |
| 1932 | /* Client list init. */ |
paul | b21b19c | 2003-06-15 01:28:29 +0000 | [diff] [blame] | 1933 | zebrad.client_list = list_new (); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1934 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1935 | /* Install configuration write function. */ |
| 1936 | install_node (&table_node, config_write_table); |
| 1937 | install_node (&forwarding_node, config_write_forwarding); |
| 1938 | |
| 1939 | install_element (VIEW_NODE, &show_ip_forwarding_cmd); |
| 1940 | install_element (ENABLE_NODE, &show_ip_forwarding_cmd); |
hasso | 647e4f1 | 2003-05-25 11:43:52 +0000 | [diff] [blame] | 1941 | install_element (CONFIG_NODE, &ip_forwarding_cmd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1942 | install_element (CONFIG_NODE, &no_ip_forwarding_cmd); |
| 1943 | install_element (ENABLE_NODE, &show_zebra_client_cmd); |
| 1944 | |
| 1945 | #ifdef HAVE_NETLINK |
| 1946 | install_element (VIEW_NODE, &show_table_cmd); |
| 1947 | install_element (ENABLE_NODE, &show_table_cmd); |
| 1948 | install_element (CONFIG_NODE, &config_table_cmd); |
| 1949 | #endif /* HAVE_NETLINK */ |
| 1950 | |
| 1951 | #ifdef HAVE_IPV6 |
| 1952 | install_element (VIEW_NODE, &show_ipv6_forwarding_cmd); |
| 1953 | install_element (ENABLE_NODE, &show_ipv6_forwarding_cmd); |
hasso | 5590672 | 2004-02-11 22:42:16 +0000 | [diff] [blame] | 1954 | install_element (CONFIG_NODE, &ipv6_forwarding_cmd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1955 | install_element (CONFIG_NODE, &no_ipv6_forwarding_cmd); |
| 1956 | #endif /* HAVE_IPV6 */ |
Paul Jakma | 7514fb7 | 2007-05-02 16:05:35 +0000 | [diff] [blame] | 1957 | |
| 1958 | /* Route-map */ |
| 1959 | zebra_route_map_init (); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1960 | } |
Denis Ovsienko | 97be79f | 2009-07-24 20:45:31 +0400 | [diff] [blame] | 1961 | |
| 1962 | /* Make zebra server socket, wiping any existing one (see bug #403). */ |
| 1963 | void |
Vyacheslav Trushkin | b511468 | 2011-11-25 18:51:48 +0400 | [diff] [blame] | 1964 | zebra_zserv_socket_init (char *path) |
Denis Ovsienko | 97be79f | 2009-07-24 20:45:31 +0400 | [diff] [blame] | 1965 | { |
| 1966 | #ifdef HAVE_TCP_ZEBRA |
| 1967 | zebra_serv (); |
| 1968 | #else |
Vyacheslav Trushkin | b511468 | 2011-11-25 18:51:48 +0400 | [diff] [blame] | 1969 | zebra_serv_un (path ? path : ZEBRA_SERV_PATH); |
Denis Ovsienko | 97be79f | 2009-07-24 20:45:31 +0400 | [diff] [blame] | 1970 | #endif /* HAVE_TCP_ZEBRA */ |
| 1971 | } |