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