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