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" |
| 45 | |
| 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; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [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 | { |
| 392 | if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB)) |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 393 | { |
paul | 1dcb517 | 2005-05-31 08:38:50 +0000 | [diff] [blame] | 394 | SET_FLAG (zapi_flags, ZAPI_MESSAGE_NEXTHOP); |
| 395 | SET_FLAG (zapi_flags, ZAPI_MESSAGE_IFINDEX); |
| 396 | |
| 397 | if (nhnummark == 0) |
| 398 | { |
| 399 | nhnummark = stream_get_endp (s); |
| 400 | stream_putc (s, 1); /* placeholder */ |
| 401 | } |
| 402 | |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 403 | nhnum++; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 404 | |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 405 | switch(nexthop->type) |
| 406 | { |
| 407 | case NEXTHOP_TYPE_IPV4: |
| 408 | case NEXTHOP_TYPE_IPV4_IFINDEX: |
| 409 | stream_put_in_addr (s, &nexthop->gate.ipv4); |
| 410 | break; |
| 411 | #ifdef HAVE_IPV6 |
| 412 | case NEXTHOP_TYPE_IPV6: |
| 413 | case NEXTHOP_TYPE_IPV6_IFINDEX: |
| 414 | case NEXTHOP_TYPE_IPV6_IFNAME: |
| 415 | stream_write (s, (u_char *) &nexthop->gate.ipv6, 16); |
| 416 | break; |
| 417 | #endif |
| 418 | default: |
| 419 | if (cmd == ZEBRA_IPV4_ROUTE_ADD |
| 420 | || cmd == ZEBRA_IPV4_ROUTE_DELETE) |
| 421 | { |
| 422 | struct in_addr empty; |
paul | 44983cf | 2004-09-22 13:15:58 +0000 | [diff] [blame] | 423 | memset (&empty, 0, sizeof (struct in_addr)); |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 424 | stream_write (s, (u_char *) &empty, IPV4_MAX_BYTELEN); |
| 425 | } |
| 426 | else |
| 427 | { |
| 428 | struct in6_addr empty; |
| 429 | memset (&empty, 0, sizeof (struct in6_addr)); |
| 430 | stream_write (s, (u_char *) &empty, IPV6_MAX_BYTELEN); |
| 431 | } |
| 432 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 433 | |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 434 | /* Interface index. */ |
| 435 | stream_putc (s, 1); |
| 436 | stream_putl (s, nexthop->ifindex); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 437 | |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 438 | break; |
| 439 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | /* Metric */ |
Stephen Hemminger | cf8a831 | 2010-08-18 15:56:46 -0700 | [diff] [blame] | 443 | if (cmd == ZEBRA_IPV4_ROUTE_ADD || cmd == ZEBRA_IPV6_ROUTE_ADD) |
paul | 1dcb517 | 2005-05-31 08:38:50 +0000 | [diff] [blame] | 444 | { |
vincent | fbf5d03 | 2005-09-29 11:25:50 +0000 | [diff] [blame] | 445 | SET_FLAG (zapi_flags, ZAPI_MESSAGE_DISTANCE); |
| 446 | stream_putc (s, rib->distance); |
paul | 1dcb517 | 2005-05-31 08:38:50 +0000 | [diff] [blame] | 447 | SET_FLAG (zapi_flags, ZAPI_MESSAGE_METRIC); |
| 448 | stream_putl (s, rib->metric); |
| 449 | } |
| 450 | |
| 451 | /* write real message flags value */ |
| 452 | stream_putc_at (s, messmark, zapi_flags); |
| 453 | |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 454 | /* Write next-hop number */ |
| 455 | if (nhnummark) |
hasso | c1eaa44 | 2004-10-19 06:26:01 +0000 | [diff] [blame] | 456 | stream_putc_at (s, nhnummark, nhnum); |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 457 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 458 | /* Write packet size. */ |
| 459 | stream_putw_at (s, 0, stream_get_endp (s)); |
| 460 | |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 461 | return zebra_server_send_message(client); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 462 | } |
| 463 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 464 | #ifdef HAVE_IPV6 |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 465 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 466 | zsend_ipv6_nexthop_lookup (struct zserv *client, struct in6_addr *addr) |
| 467 | { |
| 468 | struct stream *s; |
| 469 | struct rib *rib; |
| 470 | unsigned long nump; |
| 471 | u_char num; |
| 472 | struct nexthop *nexthop; |
| 473 | |
| 474 | /* Lookup nexthop. */ |
| 475 | rib = rib_match_ipv6 (addr); |
| 476 | |
| 477 | /* Get output stream. */ |
| 478 | s = client->obuf; |
| 479 | stream_reset (s); |
| 480 | |
| 481 | /* Fill in result. */ |
paul | c1b9800 | 2006-01-16 01:54:02 +0000 | [diff] [blame] | 482 | zserv_create_header (s, ZEBRA_IPV6_NEXTHOP_LOOKUP); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 483 | stream_put (s, &addr, 16); |
| 484 | |
| 485 | if (rib) |
| 486 | { |
| 487 | stream_putl (s, rib->metric); |
| 488 | num = 0; |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 489 | nump = stream_get_endp(s); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 490 | stream_putc (s, 0); |
| 491 | for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next) |
| 492 | if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB)) |
| 493 | { |
| 494 | stream_putc (s, nexthop->type); |
| 495 | switch (nexthop->type) |
| 496 | { |
| 497 | case ZEBRA_NEXTHOP_IPV6: |
| 498 | stream_put (s, &nexthop->gate.ipv6, 16); |
| 499 | break; |
| 500 | case ZEBRA_NEXTHOP_IPV6_IFINDEX: |
| 501 | case ZEBRA_NEXTHOP_IPV6_IFNAME: |
| 502 | stream_put (s, &nexthop->gate.ipv6, 16); |
| 503 | stream_putl (s, nexthop->ifindex); |
| 504 | break; |
| 505 | case ZEBRA_NEXTHOP_IFINDEX: |
| 506 | case ZEBRA_NEXTHOP_IFNAME: |
| 507 | stream_putl (s, nexthop->ifindex); |
| 508 | break; |
hasso | fa2b17e | 2004-03-04 17:45:00 +0000 | [diff] [blame] | 509 | default: |
| 510 | /* do nothing */ |
| 511 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 512 | } |
| 513 | num++; |
| 514 | } |
| 515 | stream_putc_at (s, nump, num); |
| 516 | } |
| 517 | else |
| 518 | { |
| 519 | stream_putl (s, 0); |
| 520 | stream_putc (s, 0); |
| 521 | } |
| 522 | |
| 523 | stream_putw_at (s, 0, stream_get_endp (s)); |
| 524 | |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 525 | return zebra_server_send_message(client); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 526 | } |
| 527 | #endif /* HAVE_IPV6 */ |
| 528 | |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 529 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 530 | zsend_ipv4_nexthop_lookup (struct zserv *client, struct in_addr addr) |
| 531 | { |
| 532 | struct stream *s; |
| 533 | struct rib *rib; |
| 534 | unsigned long nump; |
| 535 | u_char num; |
| 536 | struct nexthop *nexthop; |
| 537 | |
| 538 | /* Lookup nexthop. */ |
| 539 | rib = rib_match_ipv4 (addr); |
| 540 | |
| 541 | /* Get output stream. */ |
| 542 | s = client->obuf; |
| 543 | stream_reset (s); |
| 544 | |
| 545 | /* Fill in result. */ |
paul | c1b9800 | 2006-01-16 01:54:02 +0000 | [diff] [blame] | 546 | zserv_create_header (s, ZEBRA_IPV4_NEXTHOP_LOOKUP); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 547 | stream_put_in_addr (s, &addr); |
| 548 | |
| 549 | if (rib) |
| 550 | { |
Christian Franke | bb97e46 | 2013-05-25 14:01:35 +0000 | [diff] [blame^] | 551 | if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV) |
| 552 | zlog_debug("%s: Matching rib entry found.", __func__); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 553 | stream_putl (s, rib->metric); |
| 554 | num = 0; |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 555 | nump = stream_get_endp(s); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 556 | stream_putc (s, 0); |
| 557 | for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next) |
| 558 | if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB)) |
| 559 | { |
| 560 | stream_putc (s, nexthop->type); |
| 561 | switch (nexthop->type) |
| 562 | { |
| 563 | case ZEBRA_NEXTHOP_IPV4: |
| 564 | stream_put_in_addr (s, &nexthop->gate.ipv4); |
| 565 | break; |
Christian Franke | bb97e46 | 2013-05-25 14:01:35 +0000 | [diff] [blame^] | 566 | case ZEBRA_NEXTHOP_IPV4_IFINDEX: |
| 567 | stream_put_in_addr (s, &nexthop->gate.ipv4); |
| 568 | stream_putl (s, nexthop->ifindex); |
| 569 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 570 | case ZEBRA_NEXTHOP_IFINDEX: |
| 571 | case ZEBRA_NEXTHOP_IFNAME: |
| 572 | stream_putl (s, nexthop->ifindex); |
| 573 | break; |
hasso | fa2b17e | 2004-03-04 17:45:00 +0000 | [diff] [blame] | 574 | default: |
| 575 | /* do nothing */ |
| 576 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 577 | } |
| 578 | num++; |
| 579 | } |
| 580 | stream_putc_at (s, nump, num); |
| 581 | } |
| 582 | else |
| 583 | { |
Christian Franke | bb97e46 | 2013-05-25 14:01:35 +0000 | [diff] [blame^] | 584 | if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV) |
| 585 | zlog_debug("%s: No matching rib entry found.", __func__); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 586 | stream_putl (s, 0); |
| 587 | stream_putc (s, 0); |
| 588 | } |
| 589 | |
| 590 | stream_putw_at (s, 0, stream_get_endp (s)); |
| 591 | |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 592 | return zebra_server_send_message(client); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 593 | } |
| 594 | |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 595 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 596 | zsend_ipv4_import_lookup (struct zserv *client, struct prefix_ipv4 *p) |
| 597 | { |
| 598 | struct stream *s; |
| 599 | struct rib *rib; |
| 600 | unsigned long nump; |
| 601 | u_char num; |
| 602 | struct nexthop *nexthop; |
| 603 | |
| 604 | /* Lookup nexthop. */ |
| 605 | rib = rib_lookup_ipv4 (p); |
| 606 | |
| 607 | /* Get output stream. */ |
| 608 | s = client->obuf; |
| 609 | stream_reset (s); |
| 610 | |
| 611 | /* Fill in result. */ |
paul | c1b9800 | 2006-01-16 01:54:02 +0000 | [diff] [blame] | 612 | zserv_create_header (s, ZEBRA_IPV4_IMPORT_LOOKUP); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 613 | stream_put_in_addr (s, &p->prefix); |
| 614 | |
| 615 | if (rib) |
| 616 | { |
| 617 | stream_putl (s, rib->metric); |
| 618 | num = 0; |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 619 | nump = stream_get_endp(s); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 620 | stream_putc (s, 0); |
| 621 | for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next) |
| 622 | if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_FIB)) |
| 623 | { |
| 624 | stream_putc (s, nexthop->type); |
| 625 | switch (nexthop->type) |
| 626 | { |
| 627 | case ZEBRA_NEXTHOP_IPV4: |
| 628 | stream_put_in_addr (s, &nexthop->gate.ipv4); |
| 629 | break; |
| 630 | case ZEBRA_NEXTHOP_IFINDEX: |
| 631 | case ZEBRA_NEXTHOP_IFNAME: |
| 632 | stream_putl (s, nexthop->ifindex); |
| 633 | break; |
hasso | fa2b17e | 2004-03-04 17:45:00 +0000 | [diff] [blame] | 634 | default: |
| 635 | /* do nothing */ |
| 636 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 637 | } |
| 638 | num++; |
| 639 | } |
| 640 | stream_putc_at (s, nump, num); |
| 641 | } |
| 642 | else |
| 643 | { |
| 644 | stream_putl (s, 0); |
| 645 | stream_putc (s, 0); |
| 646 | } |
| 647 | |
| 648 | stream_putw_at (s, 0, stream_get_endp (s)); |
| 649 | |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 650 | return zebra_server_send_message(client); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 651 | } |
| 652 | |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 653 | /* Router-id is updated. Send ZEBRA_ROUTER_ID_ADD to client. */ |
| 654 | int |
| 655 | zsend_router_id_update (struct zserv *client, struct prefix *p) |
| 656 | { |
| 657 | struct stream *s; |
| 658 | int blen; |
| 659 | |
| 660 | /* Check this client need interface information. */ |
| 661 | if (!client->ridinfo) |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 662 | return 0; |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 663 | |
| 664 | s = client->obuf; |
| 665 | stream_reset (s); |
| 666 | |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 667 | /* Message type. */ |
paul | c1b9800 | 2006-01-16 01:54:02 +0000 | [diff] [blame] | 668 | zserv_create_header (s, ZEBRA_ROUTER_ID_UPDATE); |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 669 | |
| 670 | /* Prefix information. */ |
| 671 | stream_putc (s, p->family); |
| 672 | blen = prefix_blen (p); |
| 673 | stream_put (s, &p->u.prefix, blen); |
| 674 | stream_putc (s, p->prefixlen); |
| 675 | |
| 676 | /* Write packet size. */ |
| 677 | stream_putw_at (s, 0, stream_get_endp (s)); |
| 678 | |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 679 | return zebra_server_send_message(client); |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 680 | } |
| 681 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 682 | /* Register zebra server interface information. Send current all |
| 683 | interface and address information. */ |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 684 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 685 | zread_interface_add (struct zserv *client, u_short length) |
| 686 | { |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 687 | struct listnode *ifnode, *ifnnode; |
| 688 | struct listnode *cnode, *cnnode; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 689 | struct interface *ifp; |
| 690 | struct connected *c; |
| 691 | |
| 692 | /* Interface information is needed. */ |
| 693 | client->ifinfo = 1; |
| 694 | |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 695 | for (ALL_LIST_ELEMENTS (iflist, ifnode, ifnnode, ifp)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 696 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 697 | /* Skip pseudo interface. */ |
| 698 | if (! CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE)) |
| 699 | continue; |
| 700 | |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 701 | if (zsend_interface_add (client, ifp) < 0) |
| 702 | return -1; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 703 | |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 704 | for (ALL_LIST_ELEMENTS (ifp->connected, cnode, cnnode, c)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 705 | { |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 706 | if (CHECK_FLAG (c->conf, ZEBRA_IFC_REAL) && |
| 707 | (zsend_interface_address (ZEBRA_INTERFACE_ADDRESS_ADD, client, |
| 708 | ifp, c) < 0)) |
| 709 | return -1; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 710 | } |
| 711 | } |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 712 | return 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 713 | } |
| 714 | |
| 715 | /* Unregister zebra server interface information. */ |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 716 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 717 | zread_interface_delete (struct zserv *client, u_short length) |
| 718 | { |
| 719 | client->ifinfo = 0; |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 720 | return 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 721 | } |
| 722 | |
| 723 | /* This function support multiple nexthop. */ |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 724 | /* |
| 725 | * Parse the ZEBRA_IPV4_ROUTE_ADD sent from client. Update rib and |
| 726 | * add kernel route. |
| 727 | */ |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 728 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 729 | zread_ipv4_add (struct zserv *client, u_short length) |
| 730 | { |
| 731 | int i; |
| 732 | struct rib *rib; |
| 733 | struct prefix_ipv4 p; |
| 734 | u_char message; |
| 735 | struct in_addr nexthop; |
| 736 | u_char nexthop_num; |
| 737 | u_char nexthop_type; |
| 738 | struct stream *s; |
| 739 | unsigned int ifindex; |
| 740 | u_char ifname_len; |
G.Balaji | cddf391 | 2011-11-26 21:59:32 +0400 | [diff] [blame] | 741 | safi_t safi; |
| 742 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 743 | |
| 744 | /* Get input stream. */ |
| 745 | s = client->ibuf; |
| 746 | |
| 747 | /* Allocate new rib. */ |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 748 | rib = XCALLOC (MTYPE_RIB, sizeof (struct rib)); |
| 749 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 750 | /* Type, flags, message. */ |
| 751 | rib->type = stream_getc (s); |
| 752 | rib->flags = stream_getc (s); |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 753 | message = stream_getc (s); |
G.Balaji | cddf391 | 2011-11-26 21:59:32 +0400 | [diff] [blame] | 754 | safi = stream_getw (s); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 755 | rib->uptime = time (NULL); |
| 756 | |
| 757 | /* IPv4 prefix. */ |
| 758 | memset (&p, 0, sizeof (struct prefix_ipv4)); |
| 759 | p.family = AF_INET; |
| 760 | p.prefixlen = stream_getc (s); |
| 761 | stream_get (&p.prefix, s, PSIZE (p.prefixlen)); |
| 762 | |
| 763 | /* Nexthop parse. */ |
| 764 | if (CHECK_FLAG (message, ZAPI_MESSAGE_NEXTHOP)) |
| 765 | { |
| 766 | nexthop_num = stream_getc (s); |
| 767 | |
| 768 | for (i = 0; i < nexthop_num; i++) |
| 769 | { |
| 770 | nexthop_type = stream_getc (s); |
| 771 | |
| 772 | switch (nexthop_type) |
| 773 | { |
| 774 | case ZEBRA_NEXTHOP_IFINDEX: |
| 775 | ifindex = stream_getl (s); |
| 776 | nexthop_ifindex_add (rib, ifindex); |
| 777 | break; |
| 778 | case ZEBRA_NEXTHOP_IFNAME: |
| 779 | ifname_len = stream_getc (s); |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 780 | stream_forward_getp (s, ifname_len); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 781 | break; |
| 782 | case ZEBRA_NEXTHOP_IPV4: |
| 783 | nexthop.s_addr = stream_get_ipv4 (s); |
Paul Jakma | 7514fb7 | 2007-05-02 16:05:35 +0000 | [diff] [blame] | 784 | nexthop_ipv4_add (rib, &nexthop, NULL); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 785 | break; |
Joakim Tjernlund | c963c20 | 2012-07-07 17:06:13 +0200 | [diff] [blame] | 786 | case ZEBRA_NEXTHOP_IPV4_IFINDEX: |
| 787 | nexthop.s_addr = stream_get_ipv4 (s); |
| 788 | ifindex = stream_getl (s); |
| 789 | nexthop_ipv4_ifindex_add (rib, &nexthop, NULL, ifindex); |
| 790 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 791 | case ZEBRA_NEXTHOP_IPV6: |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 792 | stream_forward_getp (s, IPV6_MAX_BYTELEN); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 793 | break; |
Subbaiah Venkata | 6902c69 | 2012-03-27 19:21:29 -0700 | [diff] [blame] | 794 | case ZEBRA_NEXTHOP_BLACKHOLE: |
| 795 | nexthop_blackhole_add (rib); |
| 796 | break; |
| 797 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 798 | } |
| 799 | } |
| 800 | |
| 801 | /* Distance. */ |
| 802 | if (CHECK_FLAG (message, ZAPI_MESSAGE_DISTANCE)) |
| 803 | rib->distance = stream_getc (s); |
| 804 | |
| 805 | /* Metric. */ |
| 806 | if (CHECK_FLAG (message, ZAPI_MESSAGE_METRIC)) |
| 807 | rib->metric = stream_getl (s); |
| 808 | |
Paul Jakma | 171eee3 | 2006-07-27 16:11:02 +0000 | [diff] [blame] | 809 | /* Table */ |
| 810 | rib->table=zebrad.rtm_table_default; |
G.Balaji | cddf391 | 2011-11-26 21:59:32 +0400 | [diff] [blame] | 811 | rib_add_ipv4_multipath (&p, rib, safi); |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 812 | return 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 813 | } |
| 814 | |
| 815 | /* Zebra server IPv4 prefix delete function. */ |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 816 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 817 | zread_ipv4_delete (struct zserv *client, u_short length) |
| 818 | { |
| 819 | int i; |
| 820 | struct stream *s; |
| 821 | struct zapi_ipv4 api; |
Subbaiah Venkata | 6902c69 | 2012-03-27 19:21:29 -0700 | [diff] [blame] | 822 | struct in_addr nexthop, *nexthop_p; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 823 | unsigned long ifindex; |
| 824 | struct prefix_ipv4 p; |
| 825 | u_char nexthop_num; |
| 826 | u_char nexthop_type; |
| 827 | u_char ifname_len; |
| 828 | |
| 829 | s = client->ibuf; |
| 830 | ifindex = 0; |
| 831 | nexthop.s_addr = 0; |
Subbaiah Venkata | 6902c69 | 2012-03-27 19:21:29 -0700 | [diff] [blame] | 832 | nexthop_p = NULL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 833 | |
| 834 | /* Type, flags, message. */ |
| 835 | api.type = stream_getc (s); |
| 836 | api.flags = stream_getc (s); |
| 837 | api.message = stream_getc (s); |
G.Balaji | cddf391 | 2011-11-26 21:59:32 +0400 | [diff] [blame] | 838 | api.safi = stream_getw (s); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 839 | |
| 840 | /* IPv4 prefix. */ |
| 841 | memset (&p, 0, sizeof (struct prefix_ipv4)); |
| 842 | p.family = AF_INET; |
| 843 | p.prefixlen = stream_getc (s); |
| 844 | stream_get (&p.prefix, s, PSIZE (p.prefixlen)); |
| 845 | |
| 846 | /* Nexthop, ifindex, distance, metric. */ |
| 847 | if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP)) |
| 848 | { |
| 849 | nexthop_num = stream_getc (s); |
| 850 | |
| 851 | for (i = 0; i < nexthop_num; i++) |
| 852 | { |
| 853 | nexthop_type = stream_getc (s); |
| 854 | |
| 855 | switch (nexthop_type) |
| 856 | { |
| 857 | case ZEBRA_NEXTHOP_IFINDEX: |
| 858 | ifindex = stream_getl (s); |
| 859 | break; |
| 860 | case ZEBRA_NEXTHOP_IFNAME: |
| 861 | ifname_len = stream_getc (s); |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 862 | stream_forward_getp (s, ifname_len); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 863 | break; |
| 864 | case ZEBRA_NEXTHOP_IPV4: |
| 865 | nexthop.s_addr = stream_get_ipv4 (s); |
Subbaiah Venkata | 6902c69 | 2012-03-27 19:21:29 -0700 | [diff] [blame] | 866 | nexthop_p = &nexthop; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 867 | break; |
Joakim Tjernlund | c963c20 | 2012-07-07 17:06:13 +0200 | [diff] [blame] | 868 | case ZEBRA_NEXTHOP_IPV4_IFINDEX: |
| 869 | nexthop.s_addr = stream_get_ipv4 (s); |
| 870 | ifindex = stream_getl (s); |
| 871 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 872 | case ZEBRA_NEXTHOP_IPV6: |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 873 | stream_forward_getp (s, IPV6_MAX_BYTELEN); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 874 | break; |
| 875 | } |
| 876 | } |
| 877 | } |
| 878 | |
| 879 | /* Distance. */ |
| 880 | if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE)) |
| 881 | api.distance = stream_getc (s); |
| 882 | else |
| 883 | api.distance = 0; |
| 884 | |
| 885 | /* Metric. */ |
| 886 | if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC)) |
| 887 | api.metric = stream_getl (s); |
| 888 | else |
| 889 | api.metric = 0; |
| 890 | |
Subbaiah Venkata | 6902c69 | 2012-03-27 19:21:29 -0700 | [diff] [blame] | 891 | rib_delete_ipv4 (api.type, api.flags, &p, nexthop_p, ifindex, |
G.Balaji | cddf391 | 2011-11-26 21:59:32 +0400 | [diff] [blame] | 892 | client->rtm_table, api.safi); |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 893 | return 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 894 | } |
| 895 | |
| 896 | /* Nexthop lookup for IPv4. */ |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 897 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 898 | zread_ipv4_nexthop_lookup (struct zserv *client, u_short length) |
| 899 | { |
| 900 | struct in_addr addr; |
Christian Franke | bb97e46 | 2013-05-25 14:01:35 +0000 | [diff] [blame^] | 901 | char buf[BUFSIZ]; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 902 | |
| 903 | addr.s_addr = stream_get_ipv4 (client->ibuf); |
Christian Franke | bb97e46 | 2013-05-25 14:01:35 +0000 | [diff] [blame^] | 904 | if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV) |
| 905 | zlog_debug("%s: looking up %s", __func__, |
| 906 | inet_ntop (AF_INET, &addr, buf, BUFSIZ)); |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 907 | return zsend_ipv4_nexthop_lookup (client, addr); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 908 | } |
| 909 | |
| 910 | /* Nexthop lookup for IPv4. */ |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 911 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 912 | zread_ipv4_import_lookup (struct zserv *client, u_short length) |
| 913 | { |
| 914 | struct prefix_ipv4 p; |
| 915 | |
| 916 | p.family = AF_INET; |
| 917 | p.prefixlen = stream_getc (client->ibuf); |
| 918 | p.prefix.s_addr = stream_get_ipv4 (client->ibuf); |
| 919 | |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 920 | return zsend_ipv4_import_lookup (client, &p); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 921 | } |
| 922 | |
| 923 | #ifdef HAVE_IPV6 |
| 924 | /* Zebra server IPv6 prefix add function. */ |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 925 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 926 | zread_ipv6_add (struct zserv *client, u_short length) |
| 927 | { |
| 928 | int i; |
| 929 | struct stream *s; |
| 930 | struct zapi_ipv6 api; |
| 931 | struct in6_addr nexthop; |
| 932 | unsigned long ifindex; |
| 933 | struct prefix_ipv6 p; |
| 934 | |
| 935 | s = client->ibuf; |
| 936 | ifindex = 0; |
| 937 | memset (&nexthop, 0, sizeof (struct in6_addr)); |
| 938 | |
| 939 | /* Type, flags, message. */ |
| 940 | api.type = stream_getc (s); |
| 941 | api.flags = stream_getc (s); |
| 942 | api.message = stream_getc (s); |
G.Balaji | f768f36 | 2011-11-26 22:10:39 +0400 | [diff] [blame] | 943 | api.safi = stream_getw (s); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 944 | |
| 945 | /* IPv4 prefix. */ |
| 946 | memset (&p, 0, sizeof (struct prefix_ipv6)); |
| 947 | p.family = AF_INET6; |
| 948 | p.prefixlen = stream_getc (s); |
| 949 | stream_get (&p.prefix, s, PSIZE (p.prefixlen)); |
| 950 | |
| 951 | /* Nexthop, ifindex, distance, metric. */ |
| 952 | if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP)) |
| 953 | { |
| 954 | u_char nexthop_type; |
| 955 | |
| 956 | api.nexthop_num = stream_getc (s); |
| 957 | for (i = 0; i < api.nexthop_num; i++) |
| 958 | { |
| 959 | nexthop_type = stream_getc (s); |
| 960 | |
| 961 | switch (nexthop_type) |
| 962 | { |
| 963 | case ZEBRA_NEXTHOP_IPV6: |
| 964 | stream_get (&nexthop, s, 16); |
| 965 | break; |
| 966 | case ZEBRA_NEXTHOP_IFINDEX: |
| 967 | ifindex = stream_getl (s); |
| 968 | break; |
| 969 | } |
| 970 | } |
| 971 | } |
| 972 | |
| 973 | if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE)) |
| 974 | api.distance = stream_getc (s); |
| 975 | else |
| 976 | api.distance = 0; |
| 977 | |
| 978 | if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC)) |
| 979 | api.metric = stream_getl (s); |
| 980 | else |
| 981 | api.metric = 0; |
| 982 | |
| 983 | if (IN6_IS_ADDR_UNSPECIFIED (&nexthop)) |
Mathieu Goessens | d13c3b4 | 2009-06-23 15:59:45 +0100 | [diff] [blame] | 984 | 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] | 985 | api.distance, api.safi); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 986 | else |
Mathieu Goessens | d13c3b4 | 2009-06-23 15:59:45 +0100 | [diff] [blame] | 987 | 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] | 988 | api.distance, api.safi); |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 989 | return 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 990 | } |
| 991 | |
| 992 | /* Zebra server IPv6 prefix delete function. */ |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 993 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 994 | zread_ipv6_delete (struct zserv *client, u_short length) |
| 995 | { |
| 996 | int i; |
| 997 | struct stream *s; |
| 998 | struct zapi_ipv6 api; |
| 999 | struct in6_addr nexthop; |
| 1000 | unsigned long ifindex; |
| 1001 | struct prefix_ipv6 p; |
| 1002 | |
| 1003 | s = client->ibuf; |
| 1004 | ifindex = 0; |
| 1005 | memset (&nexthop, 0, sizeof (struct in6_addr)); |
| 1006 | |
| 1007 | /* Type, flags, message. */ |
| 1008 | api.type = stream_getc (s); |
| 1009 | api.flags = stream_getc (s); |
| 1010 | api.message = stream_getc (s); |
G.Balaji | f768f36 | 2011-11-26 22:10:39 +0400 | [diff] [blame] | 1011 | api.safi = stream_getw (s); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1012 | |
| 1013 | /* IPv4 prefix. */ |
| 1014 | memset (&p, 0, sizeof (struct prefix_ipv6)); |
| 1015 | p.family = AF_INET6; |
| 1016 | p.prefixlen = stream_getc (s); |
| 1017 | stream_get (&p.prefix, s, PSIZE (p.prefixlen)); |
| 1018 | |
| 1019 | /* Nexthop, ifindex, distance, metric. */ |
| 1020 | if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP)) |
| 1021 | { |
| 1022 | u_char nexthop_type; |
| 1023 | |
| 1024 | api.nexthop_num = stream_getc (s); |
| 1025 | for (i = 0; i < api.nexthop_num; i++) |
| 1026 | { |
| 1027 | nexthop_type = stream_getc (s); |
| 1028 | |
| 1029 | switch (nexthop_type) |
| 1030 | { |
| 1031 | case ZEBRA_NEXTHOP_IPV6: |
| 1032 | stream_get (&nexthop, s, 16); |
| 1033 | break; |
| 1034 | case ZEBRA_NEXTHOP_IFINDEX: |
| 1035 | ifindex = stream_getl (s); |
| 1036 | break; |
| 1037 | } |
| 1038 | } |
| 1039 | } |
| 1040 | |
| 1041 | if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE)) |
| 1042 | api.distance = stream_getc (s); |
| 1043 | else |
| 1044 | api.distance = 0; |
| 1045 | if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC)) |
| 1046 | api.metric = stream_getl (s); |
| 1047 | else |
| 1048 | api.metric = 0; |
| 1049 | |
| 1050 | if (IN6_IS_ADDR_UNSPECIFIED (&nexthop)) |
G.Balaji | f768f36 | 2011-11-26 22:10:39 +0400 | [diff] [blame] | 1051 | 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] | 1052 | else |
G.Balaji | f768f36 | 2011-11-26 22:10:39 +0400 | [diff] [blame] | 1053 | 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] | 1054 | return 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1055 | } |
| 1056 | |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1057 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1058 | zread_ipv6_nexthop_lookup (struct zserv *client, u_short length) |
| 1059 | { |
| 1060 | struct in6_addr addr; |
| 1061 | char buf[BUFSIZ]; |
| 1062 | |
| 1063 | stream_get (&addr, client->ibuf, 16); |
Christian Franke | a520708 | 2013-04-11 08:24:29 +0000 | [diff] [blame] | 1064 | if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV) |
| 1065 | zlog_debug("%s: looking up %s", __func__, |
| 1066 | inet_ntop (AF_INET6, &addr, buf, BUFSIZ)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1067 | |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1068 | return zsend_ipv6_nexthop_lookup (client, &addr); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1069 | } |
| 1070 | #endif /* HAVE_IPV6 */ |
| 1071 | |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 1072 | /* Register zebra server router-id information. Send current router-id */ |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1073 | static int |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 1074 | zread_router_id_add (struct zserv *client, u_short length) |
| 1075 | { |
| 1076 | struct prefix p; |
| 1077 | |
| 1078 | /* Router-id information is needed. */ |
| 1079 | client->ridinfo = 1; |
| 1080 | |
| 1081 | router_id_get (&p); |
| 1082 | |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1083 | return zsend_router_id_update (client,&p); |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 1084 | } |
| 1085 | |
| 1086 | /* Unregister zebra server router-id information. */ |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1087 | static int |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 1088 | zread_router_id_delete (struct zserv *client, u_short length) |
| 1089 | { |
| 1090 | client->ridinfo = 0; |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1091 | return 0; |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 1092 | } |
| 1093 | |
Vyacheslav Trushkin | 2ea1ab1 | 2011-12-11 18:48:47 +0400 | [diff] [blame] | 1094 | /* Tie up route-type and client->sock */ |
| 1095 | static void |
| 1096 | zread_hello (struct zserv *client) |
| 1097 | { |
| 1098 | /* type of protocol (lib/zebra.h) */ |
| 1099 | u_char proto; |
| 1100 | proto = stream_getc (client->ibuf); |
| 1101 | |
| 1102 | /* accept only dynamic routing protocols */ |
| 1103 | if ((proto < ZEBRA_ROUTE_MAX) |
| 1104 | && (proto > ZEBRA_ROUTE_STATIC)) |
| 1105 | { |
| 1106 | zlog_notice ("client %d says hello and bids fair to announce only %s routes", |
| 1107 | client->sock, zebra_route_string(proto)); |
| 1108 | |
| 1109 | /* if route-type was binded by other client */ |
| 1110 | if (route_type_oaths[proto]) |
| 1111 | zlog_warn ("sender of %s routes changed %c->%c", |
| 1112 | zebra_route_string(proto), route_type_oaths[proto], |
| 1113 | client->sock); |
| 1114 | |
| 1115 | route_type_oaths[proto] = client->sock; |
| 1116 | } |
| 1117 | } |
| 1118 | |
| 1119 | /* If client sent routes of specific type, zebra removes it |
| 1120 | * and returns number of deleted routes. |
| 1121 | */ |
| 1122 | static void |
| 1123 | zebra_score_rib (int client_sock) |
| 1124 | { |
| 1125 | int i; |
| 1126 | |
| 1127 | for (i = ZEBRA_ROUTE_RIP; i < ZEBRA_ROUTE_MAX; i++) |
| 1128 | if (client_sock == route_type_oaths[i]) |
| 1129 | { |
| 1130 | zlog_notice ("client %d disconnected. %lu %s routes removed from the rib", |
| 1131 | client_sock, rib_score_proto (i), zebra_route_string (i)); |
| 1132 | route_type_oaths[i] = 0; |
| 1133 | break; |
| 1134 | } |
| 1135 | } |
| 1136 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1137 | /* Close zebra client. */ |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 1138 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1139 | zebra_client_close (struct zserv *client) |
| 1140 | { |
| 1141 | /* Close file descriptor. */ |
| 1142 | if (client->sock) |
| 1143 | { |
| 1144 | close (client->sock); |
Vyacheslav Trushkin | 2ea1ab1 | 2011-12-11 18:48:47 +0400 | [diff] [blame] | 1145 | zebra_score_rib (client->sock); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1146 | client->sock = -1; |
| 1147 | } |
| 1148 | |
| 1149 | /* Free stream buffers. */ |
| 1150 | if (client->ibuf) |
| 1151 | stream_free (client->ibuf); |
| 1152 | if (client->obuf) |
| 1153 | stream_free (client->obuf); |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1154 | if (client->wb) |
| 1155 | buffer_free(client->wb); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1156 | |
| 1157 | /* Release threads. */ |
| 1158 | if (client->t_read) |
| 1159 | thread_cancel (client->t_read); |
| 1160 | if (client->t_write) |
| 1161 | thread_cancel (client->t_write); |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1162 | if (client->t_suicide) |
| 1163 | thread_cancel (client->t_suicide); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1164 | |
| 1165 | /* Free client structure. */ |
paul | b21b19c | 2003-06-15 01:28:29 +0000 | [diff] [blame] | 1166 | listnode_delete (zebrad.client_list, client); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1167 | XFREE (0, client); |
| 1168 | } |
| 1169 | |
| 1170 | /* Make new client. */ |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 1171 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1172 | zebra_client_create (int sock) |
| 1173 | { |
| 1174 | struct zserv *client; |
| 1175 | |
| 1176 | client = XCALLOC (0, sizeof (struct zserv)); |
| 1177 | |
| 1178 | /* Make client input/output buffer. */ |
| 1179 | client->sock = sock; |
| 1180 | client->ibuf = stream_new (ZEBRA_MAX_PACKET_SIZ); |
| 1181 | client->obuf = stream_new (ZEBRA_MAX_PACKET_SIZ); |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1182 | client->wb = buffer_new(0); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1183 | |
| 1184 | /* Set table number. */ |
paul | b21b19c | 2003-06-15 01:28:29 +0000 | [diff] [blame] | 1185 | client->rtm_table = zebrad.rtm_table_default; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1186 | |
| 1187 | /* Add this client to linked list. */ |
paul | b21b19c | 2003-06-15 01:28:29 +0000 | [diff] [blame] | 1188 | listnode_add (zebrad.client_list, client); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1189 | |
| 1190 | /* Make new read thread. */ |
| 1191 | zebra_event (ZEBRA_READ, sock, client); |
| 1192 | } |
| 1193 | |
| 1194 | /* Handler of zebra service request. */ |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 1195 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1196 | zebra_client_read (struct thread *thread) |
| 1197 | { |
| 1198 | int sock; |
| 1199 | struct zserv *client; |
ajs | 57a1477 | 2005-04-10 15:01:56 +0000 | [diff] [blame] | 1200 | size_t already; |
paul | c1b9800 | 2006-01-16 01:54:02 +0000 | [diff] [blame] | 1201 | uint16_t length, command; |
| 1202 | uint8_t marker, version; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1203 | |
| 1204 | /* Get thread data. Reset reading thread because I'm running. */ |
| 1205 | sock = THREAD_FD (thread); |
| 1206 | client = THREAD_ARG (thread); |
| 1207 | client->t_read = NULL; |
| 1208 | |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1209 | if (client->t_suicide) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1210 | { |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1211 | zebra_client_close(client); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1212 | return -1; |
| 1213 | } |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1214 | |
| 1215 | /* Read length and command (if we don't have it already). */ |
ajs | 57a1477 | 2005-04-10 15:01:56 +0000 | [diff] [blame] | 1216 | if ((already = stream_get_endp(client->ibuf)) < ZEBRA_HEADER_SIZE) |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1217 | { |
ajs | 57a1477 | 2005-04-10 15:01:56 +0000 | [diff] [blame] | 1218 | ssize_t nbyte; |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1219 | if (((nbyte = stream_read_try (client->ibuf, sock, |
ajs | 57a1477 | 2005-04-10 15:01:56 +0000 | [diff] [blame] | 1220 | ZEBRA_HEADER_SIZE-already)) == 0) || |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1221 | (nbyte == -1)) |
| 1222 | { |
| 1223 | if (IS_ZEBRA_DEBUG_EVENT) |
| 1224 | zlog_debug ("connection closed socket [%d]", sock); |
| 1225 | zebra_client_close (client); |
| 1226 | return -1; |
| 1227 | } |
ajs | 57a1477 | 2005-04-10 15:01:56 +0000 | [diff] [blame] | 1228 | if (nbyte != (ssize_t)(ZEBRA_HEADER_SIZE-already)) |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1229 | { |
| 1230 | /* Try again later. */ |
| 1231 | zebra_event (ZEBRA_READ, sock, client); |
| 1232 | return 0; |
| 1233 | } |
ajs | 57a1477 | 2005-04-10 15:01:56 +0000 | [diff] [blame] | 1234 | already = ZEBRA_HEADER_SIZE; |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1235 | } |
| 1236 | |
| 1237 | /* Reset to read from the beginning of the incoming packet. */ |
| 1238 | stream_set_getp(client->ibuf, 0); |
| 1239 | |
paul | c1b9800 | 2006-01-16 01:54:02 +0000 | [diff] [blame] | 1240 | /* Fetch header values */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1241 | length = stream_getw (client->ibuf); |
paul | c1b9800 | 2006-01-16 01:54:02 +0000 | [diff] [blame] | 1242 | marker = stream_getc (client->ibuf); |
| 1243 | version = stream_getc (client->ibuf); |
| 1244 | command = stream_getw (client->ibuf); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1245 | |
paul | c1b9800 | 2006-01-16 01:54:02 +0000 | [diff] [blame] | 1246 | if (marker != ZEBRA_HEADER_MARKER || version != ZSERV_VERSION) |
| 1247 | { |
| 1248 | zlog_err("%s: socket %d version mismatch, marker %d, version %d", |
| 1249 | __func__, sock, marker, version); |
| 1250 | zebra_client_close (client); |
| 1251 | return -1; |
| 1252 | } |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1253 | if (length < ZEBRA_HEADER_SIZE) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1254 | { |
ajs | 57a1477 | 2005-04-10 15:01:56 +0000 | [diff] [blame] | 1255 | zlog_warn("%s: socket %d message length %u is less than header size %d", |
| 1256 | __func__, sock, length, ZEBRA_HEADER_SIZE); |
| 1257 | zebra_client_close (client); |
| 1258 | return -1; |
| 1259 | } |
| 1260 | if (length > STREAM_SIZE(client->ibuf)) |
| 1261 | { |
| 1262 | zlog_warn("%s: socket %d message length %u exceeds buffer size %lu", |
| 1263 | __func__, sock, length, (u_long)STREAM_SIZE(client->ibuf)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1264 | zebra_client_close (client); |
| 1265 | return -1; |
| 1266 | } |
| 1267 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1268 | /* Read rest of data. */ |
ajs | 57a1477 | 2005-04-10 15:01:56 +0000 | [diff] [blame] | 1269 | if (already < length) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1270 | { |
ajs | 57a1477 | 2005-04-10 15:01:56 +0000 | [diff] [blame] | 1271 | ssize_t nbyte; |
| 1272 | if (((nbyte = stream_read_try (client->ibuf, sock, |
| 1273 | length-already)) == 0) || |
| 1274 | (nbyte == -1)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1275 | { |
| 1276 | if (IS_ZEBRA_DEBUG_EVENT) |
ajs | b617800 | 2004-12-07 21:12:56 +0000 | [diff] [blame] | 1277 | zlog_debug ("connection closed [%d] when reading zebra data", sock); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1278 | zebra_client_close (client); |
| 1279 | return -1; |
| 1280 | } |
ajs | 57a1477 | 2005-04-10 15:01:56 +0000 | [diff] [blame] | 1281 | if (nbyte != (ssize_t)(length-already)) |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1282 | { |
| 1283 | /* Try again later. */ |
| 1284 | zebra_event (ZEBRA_READ, sock, client); |
| 1285 | return 0; |
| 1286 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1287 | } |
| 1288 | |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1289 | length -= ZEBRA_HEADER_SIZE; |
| 1290 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1291 | /* Debug packet information. */ |
| 1292 | if (IS_ZEBRA_DEBUG_EVENT) |
ajs | b617800 | 2004-12-07 21:12:56 +0000 | [diff] [blame] | 1293 | zlog_debug ("zebra message comes from socket [%d]", sock); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1294 | |
| 1295 | if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV) |
ajs | b617800 | 2004-12-07 21:12:56 +0000 | [diff] [blame] | 1296 | zlog_debug ("zebra message received [%s] %d", |
Paul Jakma | 6685978 | 2006-05-15 17:00:37 +0000 | [diff] [blame] | 1297 | zserv_command_string (command), length); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1298 | |
| 1299 | switch (command) |
| 1300 | { |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 1301 | case ZEBRA_ROUTER_ID_ADD: |
| 1302 | zread_router_id_add (client, length); |
| 1303 | break; |
| 1304 | case ZEBRA_ROUTER_ID_DELETE: |
| 1305 | zread_router_id_delete (client, length); |
| 1306 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1307 | case ZEBRA_INTERFACE_ADD: |
| 1308 | zread_interface_add (client, length); |
| 1309 | break; |
| 1310 | case ZEBRA_INTERFACE_DELETE: |
| 1311 | zread_interface_delete (client, length); |
| 1312 | break; |
| 1313 | case ZEBRA_IPV4_ROUTE_ADD: |
| 1314 | zread_ipv4_add (client, length); |
| 1315 | break; |
| 1316 | case ZEBRA_IPV4_ROUTE_DELETE: |
| 1317 | zread_ipv4_delete (client, length); |
| 1318 | break; |
| 1319 | #ifdef HAVE_IPV6 |
| 1320 | case ZEBRA_IPV6_ROUTE_ADD: |
| 1321 | zread_ipv6_add (client, length); |
| 1322 | break; |
| 1323 | case ZEBRA_IPV6_ROUTE_DELETE: |
| 1324 | zread_ipv6_delete (client, length); |
| 1325 | break; |
| 1326 | #endif /* HAVE_IPV6 */ |
| 1327 | case ZEBRA_REDISTRIBUTE_ADD: |
| 1328 | zebra_redistribute_add (command, client, length); |
| 1329 | break; |
| 1330 | case ZEBRA_REDISTRIBUTE_DELETE: |
| 1331 | zebra_redistribute_delete (command, client, length); |
| 1332 | break; |
| 1333 | case ZEBRA_REDISTRIBUTE_DEFAULT_ADD: |
| 1334 | zebra_redistribute_default_add (command, client, length); |
| 1335 | break; |
| 1336 | case ZEBRA_REDISTRIBUTE_DEFAULT_DELETE: |
| 1337 | zebra_redistribute_default_delete (command, client, length); |
| 1338 | break; |
| 1339 | case ZEBRA_IPV4_NEXTHOP_LOOKUP: |
| 1340 | zread_ipv4_nexthop_lookup (client, length); |
| 1341 | break; |
| 1342 | #ifdef HAVE_IPV6 |
| 1343 | case ZEBRA_IPV6_NEXTHOP_LOOKUP: |
| 1344 | zread_ipv6_nexthop_lookup (client, length); |
| 1345 | break; |
| 1346 | #endif /* HAVE_IPV6 */ |
| 1347 | case ZEBRA_IPV4_IMPORT_LOOKUP: |
| 1348 | zread_ipv4_import_lookup (client, length); |
| 1349 | break; |
Vyacheslav Trushkin | 2ea1ab1 | 2011-12-11 18:48:47 +0400 | [diff] [blame] | 1350 | case ZEBRA_HELLO: |
| 1351 | zread_hello (client); |
| 1352 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1353 | default: |
| 1354 | zlog_info ("Zebra received unknown command %d", command); |
| 1355 | break; |
| 1356 | } |
| 1357 | |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1358 | if (client->t_suicide) |
| 1359 | { |
| 1360 | /* No need to wait for thread callback, just kill immediately. */ |
| 1361 | zebra_client_close(client); |
| 1362 | return -1; |
| 1363 | } |
| 1364 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1365 | stream_reset (client->ibuf); |
| 1366 | zebra_event (ZEBRA_READ, sock, client); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1367 | return 0; |
| 1368 | } |
| 1369 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1370 | |
| 1371 | /* Accept code of zebra server socket. */ |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 1372 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1373 | zebra_accept (struct thread *thread) |
| 1374 | { |
| 1375 | int accept_sock; |
| 1376 | int client_sock; |
| 1377 | struct sockaddr_in client; |
| 1378 | socklen_t len; |
| 1379 | |
| 1380 | accept_sock = THREAD_FD (thread); |
| 1381 | |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1382 | /* Reregister myself. */ |
| 1383 | zebra_event (ZEBRA_SERV, accept_sock, NULL); |
| 1384 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1385 | len = sizeof (struct sockaddr_in); |
| 1386 | client_sock = accept (accept_sock, (struct sockaddr *) &client, &len); |
| 1387 | |
| 1388 | if (client_sock < 0) |
| 1389 | { |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 1390 | zlog_warn ("Can't accept zebra socket: %s", safe_strerror (errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1391 | return -1; |
| 1392 | } |
| 1393 | |
paul | ccf3557 | 2003-03-01 11:42:20 +0000 | [diff] [blame] | 1394 | /* Make client socket non-blocking. */ |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1395 | set_nonblocking(client_sock); |
paul | 865b852 | 2005-01-05 08:30:35 +0000 | [diff] [blame] | 1396 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1397 | /* Create new zebra client. */ |
| 1398 | zebra_client_create (client_sock); |
| 1399 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1400 | return 0; |
| 1401 | } |
| 1402 | |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 1403 | #ifdef HAVE_TCP_ZEBRA |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1404 | /* Make zebra's server socket. */ |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 1405 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1406 | zebra_serv () |
| 1407 | { |
| 1408 | int ret; |
| 1409 | int accept_sock; |
| 1410 | struct sockaddr_in addr; |
| 1411 | |
| 1412 | accept_sock = socket (AF_INET, SOCK_STREAM, 0); |
| 1413 | |
| 1414 | if (accept_sock < 0) |
| 1415 | { |
paul | 3d1dc85 | 2005-04-05 00:45:23 +0000 | [diff] [blame] | 1416 | zlog_warn ("Can't create zserv stream socket: %s", |
| 1417 | safe_strerror (errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1418 | zlog_warn ("zebra can't provice full functionality due to above error"); |
| 1419 | return; |
| 1420 | } |
| 1421 | |
Vyacheslav Trushkin | 2ea1ab1 | 2011-12-11 18:48:47 +0400 | [diff] [blame] | 1422 | memset (&route_type_oaths, 0, sizeof (route_type_oaths)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1423 | memset (&addr, 0, sizeof (struct sockaddr_in)); |
| 1424 | addr.sin_family = AF_INET; |
| 1425 | addr.sin_port = htons (ZEBRA_PORT); |
Paul Jakma | 6f0e3f6 | 2007-05-10 02:38:51 +0000 | [diff] [blame] | 1426 | #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1427 | addr.sin_len = sizeof (struct sockaddr_in); |
Paul Jakma | 6f0e3f6 | 2007-05-10 02:38:51 +0000 | [diff] [blame] | 1428 | #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1429 | addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK); |
| 1430 | |
| 1431 | sockopt_reuseaddr (accept_sock); |
| 1432 | sockopt_reuseport (accept_sock); |
| 1433 | |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 1434 | if ( zserv_privs.change(ZPRIVS_RAISE) ) |
| 1435 | zlog (NULL, LOG_ERR, "Can't raise privileges"); |
| 1436 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1437 | ret = bind (accept_sock, (struct sockaddr *)&addr, |
| 1438 | sizeof (struct sockaddr_in)); |
| 1439 | if (ret < 0) |
| 1440 | { |
paul | 3d1dc85 | 2005-04-05 00:45:23 +0000 | [diff] [blame] | 1441 | zlog_warn ("Can't bind to stream socket: %s", |
| 1442 | safe_strerror (errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1443 | zlog_warn ("zebra can't provice full functionality due to above error"); |
| 1444 | close (accept_sock); /* Avoid sd leak. */ |
| 1445 | return; |
| 1446 | } |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 1447 | |
| 1448 | if ( zserv_privs.change(ZPRIVS_LOWER) ) |
| 1449 | zlog (NULL, LOG_ERR, "Can't lower privileges"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1450 | |
| 1451 | ret = listen (accept_sock, 1); |
| 1452 | if (ret < 0) |
| 1453 | { |
paul | 3d1dc85 | 2005-04-05 00:45:23 +0000 | [diff] [blame] | 1454 | zlog_warn ("Can't listen to stream socket: %s", |
| 1455 | safe_strerror (errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1456 | zlog_warn ("zebra can't provice full functionality due to above error"); |
| 1457 | close (accept_sock); /* Avoid sd leak. */ |
| 1458 | return; |
| 1459 | } |
| 1460 | |
| 1461 | zebra_event (ZEBRA_SERV, accept_sock, NULL); |
| 1462 | } |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 1463 | #endif /* HAVE_TCP_ZEBRA */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1464 | |
| 1465 | /* For sockaddr_un. */ |
| 1466 | #include <sys/un.h> |
| 1467 | |
| 1468 | /* zebra server UNIX domain socket. */ |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 1469 | static void |
hasso | fce954f | 2004-10-07 20:29:24 +0000 | [diff] [blame] | 1470 | zebra_serv_un (const char *path) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1471 | { |
| 1472 | int ret; |
| 1473 | int sock, len; |
| 1474 | struct sockaddr_un serv; |
| 1475 | mode_t old_mask; |
| 1476 | |
| 1477 | /* First of all, unlink existing socket */ |
| 1478 | unlink (path); |
| 1479 | |
| 1480 | /* Set umask */ |
| 1481 | old_mask = umask (0077); |
| 1482 | |
| 1483 | /* Make UNIX domain socket. */ |
| 1484 | sock = socket (AF_UNIX, SOCK_STREAM, 0); |
| 1485 | if (sock < 0) |
| 1486 | { |
paul | 3d1dc85 | 2005-04-05 00:45:23 +0000 | [diff] [blame] | 1487 | zlog_warn ("Can't create zserv unix socket: %s", |
| 1488 | safe_strerror (errno)); |
| 1489 | zlog_warn ("zebra can't provide full functionality due to above error"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1490 | return; |
| 1491 | } |
| 1492 | |
Vyacheslav Trushkin | 2ea1ab1 | 2011-12-11 18:48:47 +0400 | [diff] [blame] | 1493 | memset (&route_type_oaths, 0, sizeof (route_type_oaths)); |
| 1494 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1495 | /* Make server socket. */ |
| 1496 | memset (&serv, 0, sizeof (struct sockaddr_un)); |
| 1497 | serv.sun_family = AF_UNIX; |
| 1498 | strncpy (serv.sun_path, path, strlen (path)); |
Paul Jakma | 6f0e3f6 | 2007-05-10 02:38:51 +0000 | [diff] [blame] | 1499 | #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1500 | len = serv.sun_len = SUN_LEN(&serv); |
| 1501 | #else |
| 1502 | len = sizeof (serv.sun_family) + strlen (serv.sun_path); |
Paul Jakma | 6f0e3f6 | 2007-05-10 02:38:51 +0000 | [diff] [blame] | 1503 | #endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1504 | |
| 1505 | ret = bind (sock, (struct sockaddr *) &serv, len); |
| 1506 | if (ret < 0) |
| 1507 | { |
paul | 3d1dc85 | 2005-04-05 00:45:23 +0000 | [diff] [blame] | 1508 | zlog_warn ("Can't bind to unix socket %s: %s", |
| 1509 | path, safe_strerror (errno)); |
| 1510 | zlog_warn ("zebra can't provide full functionality due to above error"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1511 | close (sock); |
| 1512 | return; |
| 1513 | } |
| 1514 | |
| 1515 | ret = listen (sock, 5); |
| 1516 | if (ret < 0) |
| 1517 | { |
paul | 3d1dc85 | 2005-04-05 00:45:23 +0000 | [diff] [blame] | 1518 | zlog_warn ("Can't listen to unix socket %s: %s", |
| 1519 | path, safe_strerror (errno)); |
| 1520 | zlog_warn ("zebra can't provide full functionality due to above error"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1521 | close (sock); |
| 1522 | return; |
| 1523 | } |
| 1524 | |
| 1525 | umask (old_mask); |
| 1526 | |
| 1527 | zebra_event (ZEBRA_SERV, sock, NULL); |
| 1528 | } |
| 1529 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1530 | |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 1531 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1532 | zebra_event (enum event event, int sock, struct zserv *client) |
| 1533 | { |
| 1534 | switch (event) |
| 1535 | { |
| 1536 | case ZEBRA_SERV: |
paul | b21b19c | 2003-06-15 01:28:29 +0000 | [diff] [blame] | 1537 | thread_add_read (zebrad.master, zebra_accept, client, sock); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1538 | break; |
| 1539 | case ZEBRA_READ: |
| 1540 | client->t_read = |
paul | b21b19c | 2003-06-15 01:28:29 +0000 | [diff] [blame] | 1541 | thread_add_read (zebrad.master, zebra_client_read, client, sock); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1542 | break; |
| 1543 | case ZEBRA_WRITE: |
| 1544 | /**/ |
| 1545 | break; |
| 1546 | } |
| 1547 | } |
| 1548 | |
| 1549 | /* Display default rtm_table for all clients. */ |
| 1550 | DEFUN (show_table, |
| 1551 | show_table_cmd, |
| 1552 | "show table", |
| 1553 | SHOW_STR |
| 1554 | "default routing table to use for all clients\n") |
| 1555 | { |
paul | b21b19c | 2003-06-15 01:28:29 +0000 | [diff] [blame] | 1556 | vty_out (vty, "table %d%s", zebrad.rtm_table_default, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1557 | VTY_NEWLINE); |
| 1558 | return CMD_SUCCESS; |
| 1559 | } |
| 1560 | |
| 1561 | DEFUN (config_table, |
| 1562 | config_table_cmd, |
| 1563 | "table TABLENO", |
| 1564 | "Configure target kernel routing table\n" |
| 1565 | "TABLE integer\n") |
| 1566 | { |
paul | b21b19c | 2003-06-15 01:28:29 +0000 | [diff] [blame] | 1567 | zebrad.rtm_table_default = strtol (argv[0], (char**)0, 10); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1568 | return CMD_SUCCESS; |
| 1569 | } |
| 1570 | |
hasso | 647e4f1 | 2003-05-25 11:43:52 +0000 | [diff] [blame] | 1571 | DEFUN (ip_forwarding, |
| 1572 | ip_forwarding_cmd, |
| 1573 | "ip forwarding", |
| 1574 | IP_STR |
| 1575 | "Turn on IP forwarding") |
| 1576 | { |
| 1577 | int ret; |
| 1578 | |
| 1579 | ret = ipforward (); |
hasso | b71f00f | 2004-10-13 12:20:35 +0000 | [diff] [blame] | 1580 | if (ret == 0) |
| 1581 | ret = ipforward_on (); |
hasso | 647e4f1 | 2003-05-25 11:43:52 +0000 | [diff] [blame] | 1582 | |
hasso | 647e4f1 | 2003-05-25 11:43:52 +0000 | [diff] [blame] | 1583 | if (ret == 0) |
| 1584 | { |
| 1585 | vty_out (vty, "Can't turn on IP forwarding%s", VTY_NEWLINE); |
| 1586 | return CMD_WARNING; |
| 1587 | } |
| 1588 | |
| 1589 | return CMD_SUCCESS; |
| 1590 | } |
| 1591 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1592 | DEFUN (no_ip_forwarding, |
| 1593 | no_ip_forwarding_cmd, |
| 1594 | "no ip forwarding", |
| 1595 | NO_STR |
| 1596 | IP_STR |
| 1597 | "Turn off IP forwarding") |
| 1598 | { |
| 1599 | int ret; |
| 1600 | |
| 1601 | ret = ipforward (); |
hasso | b71f00f | 2004-10-13 12:20:35 +0000 | [diff] [blame] | 1602 | if (ret != 0) |
| 1603 | ret = ipforward_off (); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1604 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1605 | if (ret != 0) |
| 1606 | { |
| 1607 | vty_out (vty, "Can't turn off IP forwarding%s", VTY_NEWLINE); |
| 1608 | return CMD_WARNING; |
| 1609 | } |
| 1610 | |
| 1611 | return CMD_SUCCESS; |
| 1612 | } |
| 1613 | |
| 1614 | /* This command is for debugging purpose. */ |
| 1615 | DEFUN (show_zebra_client, |
| 1616 | show_zebra_client_cmd, |
| 1617 | "show zebra client", |
| 1618 | SHOW_STR |
| 1619 | "Zebra information" |
| 1620 | "Client information") |
| 1621 | { |
hasso | 52dc7ee | 2004-09-23 19:18:23 +0000 | [diff] [blame] | 1622 | struct listnode *node; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1623 | struct zserv *client; |
| 1624 | |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 1625 | for (ALL_LIST_ELEMENTS_RO (zebrad.client_list, node, client)) |
| 1626 | vty_out (vty, "Client fd %d%s", client->sock, VTY_NEWLINE); |
| 1627 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1628 | return CMD_SUCCESS; |
| 1629 | } |
| 1630 | |
| 1631 | /* Table configuration write function. */ |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 1632 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1633 | config_write_table (struct vty *vty) |
| 1634 | { |
paul | b21b19c | 2003-06-15 01:28:29 +0000 | [diff] [blame] | 1635 | if (zebrad.rtm_table_default) |
| 1636 | vty_out (vty, "table %d%s", zebrad.rtm_table_default, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1637 | VTY_NEWLINE); |
| 1638 | return 0; |
| 1639 | } |
| 1640 | |
| 1641 | /* table node for routing tables. */ |
Stephen Hemminger | 7fc626d | 2008-12-01 11:10:34 -0800 | [diff] [blame] | 1642 | static struct cmd_node table_node = |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1643 | { |
| 1644 | TABLE_NODE, |
| 1645 | "", /* This node has no interface. */ |
| 1646 | 1 |
| 1647 | }; |
| 1648 | |
| 1649 | /* Only display ip forwarding is enabled or not. */ |
| 1650 | DEFUN (show_ip_forwarding, |
| 1651 | show_ip_forwarding_cmd, |
| 1652 | "show ip forwarding", |
| 1653 | SHOW_STR |
| 1654 | IP_STR |
| 1655 | "IP forwarding status\n") |
| 1656 | { |
| 1657 | int ret; |
| 1658 | |
| 1659 | ret = ipforward (); |
| 1660 | |
| 1661 | if (ret == 0) |
| 1662 | vty_out (vty, "IP forwarding is off%s", VTY_NEWLINE); |
| 1663 | else |
| 1664 | vty_out (vty, "IP forwarding is on%s", VTY_NEWLINE); |
| 1665 | return CMD_SUCCESS; |
| 1666 | } |
| 1667 | |
| 1668 | #ifdef HAVE_IPV6 |
| 1669 | /* Only display ipv6 forwarding is enabled or not. */ |
| 1670 | DEFUN (show_ipv6_forwarding, |
| 1671 | show_ipv6_forwarding_cmd, |
| 1672 | "show ipv6 forwarding", |
| 1673 | SHOW_STR |
| 1674 | "IPv6 information\n" |
| 1675 | "Forwarding status\n") |
| 1676 | { |
| 1677 | int ret; |
| 1678 | |
| 1679 | ret = ipforward_ipv6 (); |
| 1680 | |
| 1681 | switch (ret) |
| 1682 | { |
| 1683 | case -1: |
| 1684 | vty_out (vty, "ipv6 forwarding is unknown%s", VTY_NEWLINE); |
| 1685 | break; |
| 1686 | case 0: |
| 1687 | vty_out (vty, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE); |
| 1688 | break; |
| 1689 | case 1: |
| 1690 | vty_out (vty, "ipv6 forwarding is %s%s", "on", VTY_NEWLINE); |
| 1691 | break; |
| 1692 | default: |
| 1693 | vty_out (vty, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE); |
| 1694 | break; |
| 1695 | } |
| 1696 | return CMD_SUCCESS; |
| 1697 | } |
| 1698 | |
hasso | 5590672 | 2004-02-11 22:42:16 +0000 | [diff] [blame] | 1699 | DEFUN (ipv6_forwarding, |
| 1700 | ipv6_forwarding_cmd, |
| 1701 | "ipv6 forwarding", |
| 1702 | IPV6_STR |
| 1703 | "Turn on IPv6 forwarding") |
| 1704 | { |
| 1705 | int ret; |
| 1706 | |
hasso | 41d3fc9 | 2004-04-06 11:59:00 +0000 | [diff] [blame] | 1707 | ret = ipforward_ipv6 (); |
hasso | b71f00f | 2004-10-13 12:20:35 +0000 | [diff] [blame] | 1708 | if (ret == 0) |
| 1709 | ret = ipforward_ipv6_on (); |
hasso | 41d3fc9 | 2004-04-06 11:59:00 +0000 | [diff] [blame] | 1710 | |
hasso | 41d3fc9 | 2004-04-06 11:59:00 +0000 | [diff] [blame] | 1711 | if (ret == 0) |
| 1712 | { |
hasso | 5590672 | 2004-02-11 22:42:16 +0000 | [diff] [blame] | 1713 | vty_out (vty, "Can't turn on IPv6 forwarding%s", VTY_NEWLINE); |
| 1714 | return CMD_WARNING; |
| 1715 | } |
| 1716 | |
| 1717 | return CMD_SUCCESS; |
| 1718 | } |
| 1719 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1720 | DEFUN (no_ipv6_forwarding, |
| 1721 | no_ipv6_forwarding_cmd, |
| 1722 | "no ipv6 forwarding", |
| 1723 | NO_STR |
hasso | 5590672 | 2004-02-11 22:42:16 +0000 | [diff] [blame] | 1724 | IPV6_STR |
| 1725 | "Turn off IPv6 forwarding") |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1726 | { |
| 1727 | int ret; |
| 1728 | |
hasso | 41d3fc9 | 2004-04-06 11:59:00 +0000 | [diff] [blame] | 1729 | ret = ipforward_ipv6 (); |
hasso | b71f00f | 2004-10-13 12:20:35 +0000 | [diff] [blame] | 1730 | if (ret != 0) |
| 1731 | ret = ipforward_ipv6_off (); |
hasso | 41d3fc9 | 2004-04-06 11:59:00 +0000 | [diff] [blame] | 1732 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1733 | if (ret != 0) |
| 1734 | { |
| 1735 | vty_out (vty, "Can't turn off IPv6 forwarding%s", VTY_NEWLINE); |
| 1736 | return CMD_WARNING; |
| 1737 | } |
| 1738 | |
| 1739 | return CMD_SUCCESS; |
| 1740 | } |
| 1741 | |
| 1742 | #endif /* HAVE_IPV6 */ |
| 1743 | |
| 1744 | /* IPForwarding configuration write function. */ |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1745 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1746 | config_write_forwarding (struct vty *vty) |
| 1747 | { |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 1748 | /* FIXME: Find better place for that. */ |
| 1749 | router_id_write (vty); |
| 1750 | |
paul | 3e0b3a5 | 2004-08-23 18:58:32 +0000 | [diff] [blame] | 1751 | if (ipforward ()) |
| 1752 | vty_out (vty, "ip forwarding%s", VTY_NEWLINE); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1753 | #ifdef HAVE_IPV6 |
paul | 3e0b3a5 | 2004-08-23 18:58:32 +0000 | [diff] [blame] | 1754 | if (ipforward_ipv6 ()) |
| 1755 | vty_out (vty, "ipv6 forwarding%s", VTY_NEWLINE); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1756 | #endif /* HAVE_IPV6 */ |
| 1757 | vty_out (vty, "!%s", VTY_NEWLINE); |
| 1758 | return 0; |
| 1759 | } |
| 1760 | |
| 1761 | /* table node for routing tables. */ |
Stephen Hemminger | 7fc626d | 2008-12-01 11:10:34 -0800 | [diff] [blame] | 1762 | static struct cmd_node forwarding_node = |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1763 | { |
| 1764 | FORWARDING_NODE, |
| 1765 | "", /* This node has no interface. */ |
| 1766 | 1 |
| 1767 | }; |
| 1768 | |
| 1769 | |
| 1770 | /* Initialisation of zebra and installation of commands. */ |
| 1771 | void |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 1772 | zebra_init (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1773 | { |
| 1774 | /* Client list init. */ |
paul | b21b19c | 2003-06-15 01:28:29 +0000 | [diff] [blame] | 1775 | zebrad.client_list = list_new (); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1776 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1777 | /* Install configuration write function. */ |
| 1778 | install_node (&table_node, config_write_table); |
| 1779 | install_node (&forwarding_node, config_write_forwarding); |
| 1780 | |
| 1781 | install_element (VIEW_NODE, &show_ip_forwarding_cmd); |
| 1782 | install_element (ENABLE_NODE, &show_ip_forwarding_cmd); |
hasso | 647e4f1 | 2003-05-25 11:43:52 +0000 | [diff] [blame] | 1783 | install_element (CONFIG_NODE, &ip_forwarding_cmd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1784 | install_element (CONFIG_NODE, &no_ip_forwarding_cmd); |
| 1785 | install_element (ENABLE_NODE, &show_zebra_client_cmd); |
| 1786 | |
| 1787 | #ifdef HAVE_NETLINK |
| 1788 | install_element (VIEW_NODE, &show_table_cmd); |
| 1789 | install_element (ENABLE_NODE, &show_table_cmd); |
| 1790 | install_element (CONFIG_NODE, &config_table_cmd); |
| 1791 | #endif /* HAVE_NETLINK */ |
| 1792 | |
| 1793 | #ifdef HAVE_IPV6 |
| 1794 | install_element (VIEW_NODE, &show_ipv6_forwarding_cmd); |
| 1795 | install_element (ENABLE_NODE, &show_ipv6_forwarding_cmd); |
hasso | 5590672 | 2004-02-11 22:42:16 +0000 | [diff] [blame] | 1796 | install_element (CONFIG_NODE, &ipv6_forwarding_cmd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1797 | install_element (CONFIG_NODE, &no_ipv6_forwarding_cmd); |
| 1798 | #endif /* HAVE_IPV6 */ |
Paul Jakma | 7514fb7 | 2007-05-02 16:05:35 +0000 | [diff] [blame] | 1799 | |
| 1800 | /* Route-map */ |
| 1801 | zebra_route_map_init (); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1802 | } |
Denis Ovsienko | 97be79f | 2009-07-24 20:45:31 +0400 | [diff] [blame] | 1803 | |
| 1804 | /* Make zebra server socket, wiping any existing one (see bug #403). */ |
| 1805 | void |
Vyacheslav Trushkin | b511468 | 2011-11-25 18:51:48 +0400 | [diff] [blame] | 1806 | zebra_zserv_socket_init (char *path) |
Denis Ovsienko | 97be79f | 2009-07-24 20:45:31 +0400 | [diff] [blame] | 1807 | { |
| 1808 | #ifdef HAVE_TCP_ZEBRA |
| 1809 | zebra_serv (); |
| 1810 | #else |
Vyacheslav Trushkin | b511468 | 2011-11-25 18:51:48 +0400 | [diff] [blame] | 1811 | zebra_serv_un (path ? path : ZEBRA_SERV_PATH); |
Denis Ovsienko | 97be79f | 2009-07-24 20:45:31 +0400 | [diff] [blame] | 1812 | #endif /* HAVE_TCP_ZEBRA */ |
| 1813 | } |