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