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); |
G.Balaji | f768f36 | 2011-11-26 22:10:39 +0400 | [diff] [blame] | 940 | api.safi = stream_getw (s); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 941 | |
| 942 | /* IPv4 prefix. */ |
| 943 | memset (&p, 0, sizeof (struct prefix_ipv6)); |
| 944 | p.family = AF_INET6; |
| 945 | p.prefixlen = stream_getc (s); |
| 946 | stream_get (&p.prefix, s, PSIZE (p.prefixlen)); |
| 947 | |
| 948 | /* Nexthop, ifindex, distance, metric. */ |
| 949 | if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP)) |
| 950 | { |
| 951 | u_char nexthop_type; |
| 952 | |
| 953 | api.nexthop_num = stream_getc (s); |
| 954 | for (i = 0; i < api.nexthop_num; i++) |
| 955 | { |
| 956 | nexthop_type = stream_getc (s); |
| 957 | |
| 958 | switch (nexthop_type) |
| 959 | { |
| 960 | case ZEBRA_NEXTHOP_IPV6: |
| 961 | stream_get (&nexthop, s, 16); |
| 962 | break; |
| 963 | case ZEBRA_NEXTHOP_IFINDEX: |
| 964 | ifindex = stream_getl (s); |
| 965 | break; |
| 966 | } |
| 967 | } |
| 968 | } |
| 969 | |
| 970 | if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE)) |
| 971 | api.distance = stream_getc (s); |
| 972 | else |
| 973 | api.distance = 0; |
| 974 | |
| 975 | if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC)) |
| 976 | api.metric = stream_getl (s); |
| 977 | else |
| 978 | api.metric = 0; |
| 979 | |
| 980 | if (IN6_IS_ADDR_UNSPECIFIED (&nexthop)) |
Mathieu Goessens | d13c3b4 | 2009-06-23 15:59:45 +0100 | [diff] [blame] | 981 | 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] | 982 | api.distance, api.safi); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 983 | else |
Mathieu Goessens | d13c3b4 | 2009-06-23 15:59:45 +0100 | [diff] [blame] | 984 | 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] | 985 | api.distance, api.safi); |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 986 | return 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 987 | } |
| 988 | |
| 989 | /* Zebra server IPv6 prefix delete function. */ |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 990 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 991 | zread_ipv6_delete (struct zserv *client, u_short length) |
| 992 | { |
| 993 | int i; |
| 994 | struct stream *s; |
| 995 | struct zapi_ipv6 api; |
| 996 | struct in6_addr nexthop; |
| 997 | unsigned long ifindex; |
| 998 | struct prefix_ipv6 p; |
| 999 | |
| 1000 | s = client->ibuf; |
| 1001 | ifindex = 0; |
| 1002 | memset (&nexthop, 0, sizeof (struct in6_addr)); |
| 1003 | |
| 1004 | /* Type, flags, message. */ |
| 1005 | api.type = stream_getc (s); |
| 1006 | api.flags = stream_getc (s); |
| 1007 | api.message = stream_getc (s); |
G.Balaji | f768f36 | 2011-11-26 22:10:39 +0400 | [diff] [blame] | 1008 | api.safi = stream_getw (s); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1009 | |
| 1010 | /* IPv4 prefix. */ |
| 1011 | memset (&p, 0, sizeof (struct prefix_ipv6)); |
| 1012 | p.family = AF_INET6; |
| 1013 | p.prefixlen = stream_getc (s); |
| 1014 | stream_get (&p.prefix, s, PSIZE (p.prefixlen)); |
| 1015 | |
| 1016 | /* Nexthop, ifindex, distance, metric. */ |
| 1017 | if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP)) |
| 1018 | { |
| 1019 | u_char nexthop_type; |
| 1020 | |
| 1021 | api.nexthop_num = stream_getc (s); |
| 1022 | for (i = 0; i < api.nexthop_num; i++) |
| 1023 | { |
| 1024 | nexthop_type = stream_getc (s); |
| 1025 | |
| 1026 | switch (nexthop_type) |
| 1027 | { |
| 1028 | case ZEBRA_NEXTHOP_IPV6: |
| 1029 | stream_get (&nexthop, s, 16); |
| 1030 | break; |
| 1031 | case ZEBRA_NEXTHOP_IFINDEX: |
| 1032 | ifindex = stream_getl (s); |
| 1033 | break; |
| 1034 | } |
| 1035 | } |
| 1036 | } |
| 1037 | |
| 1038 | if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE)) |
| 1039 | api.distance = stream_getc (s); |
| 1040 | else |
| 1041 | api.distance = 0; |
| 1042 | if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC)) |
| 1043 | api.metric = stream_getl (s); |
| 1044 | else |
| 1045 | api.metric = 0; |
| 1046 | |
| 1047 | if (IN6_IS_ADDR_UNSPECIFIED (&nexthop)) |
G.Balaji | f768f36 | 2011-11-26 22:10:39 +0400 | [diff] [blame] | 1048 | 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] | 1049 | else |
G.Balaji | f768f36 | 2011-11-26 22:10:39 +0400 | [diff] [blame] | 1050 | 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] | 1051 | return 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1052 | } |
| 1053 | |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1054 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1055 | zread_ipv6_nexthop_lookup (struct zserv *client, u_short length) |
| 1056 | { |
| 1057 | struct in6_addr addr; |
| 1058 | char buf[BUFSIZ]; |
| 1059 | |
| 1060 | stream_get (&addr, client->ibuf, 16); |
| 1061 | printf ("DEBUG %s\n", inet_ntop (AF_INET6, &addr, buf, BUFSIZ)); |
| 1062 | |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1063 | return zsend_ipv6_nexthop_lookup (client, &addr); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1064 | } |
| 1065 | #endif /* HAVE_IPV6 */ |
| 1066 | |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 1067 | /* Register zebra server router-id information. Send current router-id */ |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1068 | static int |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 1069 | zread_router_id_add (struct zserv *client, u_short length) |
| 1070 | { |
| 1071 | struct prefix p; |
| 1072 | |
| 1073 | /* Router-id information is needed. */ |
| 1074 | client->ridinfo = 1; |
| 1075 | |
| 1076 | router_id_get (&p); |
| 1077 | |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1078 | return zsend_router_id_update (client,&p); |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 1079 | } |
| 1080 | |
| 1081 | /* Unregister zebra server router-id information. */ |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1082 | static int |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 1083 | zread_router_id_delete (struct zserv *client, u_short length) |
| 1084 | { |
| 1085 | client->ridinfo = 0; |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1086 | return 0; |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 1087 | } |
| 1088 | |
Vyacheslav Trushkin | 2ea1ab1 | 2011-12-11 18:48:47 +0400 | [diff] [blame] | 1089 | /* Tie up route-type and client->sock */ |
| 1090 | static void |
| 1091 | zread_hello (struct zserv *client) |
| 1092 | { |
| 1093 | /* type of protocol (lib/zebra.h) */ |
| 1094 | u_char proto; |
| 1095 | proto = stream_getc (client->ibuf); |
| 1096 | |
| 1097 | /* accept only dynamic routing protocols */ |
| 1098 | if ((proto < ZEBRA_ROUTE_MAX) |
| 1099 | && (proto > ZEBRA_ROUTE_STATIC)) |
| 1100 | { |
| 1101 | zlog_notice ("client %d says hello and bids fair to announce only %s routes", |
| 1102 | client->sock, zebra_route_string(proto)); |
| 1103 | |
| 1104 | /* if route-type was binded by other client */ |
| 1105 | if (route_type_oaths[proto]) |
| 1106 | zlog_warn ("sender of %s routes changed %c->%c", |
| 1107 | zebra_route_string(proto), route_type_oaths[proto], |
| 1108 | client->sock); |
| 1109 | |
| 1110 | route_type_oaths[proto] = client->sock; |
| 1111 | } |
| 1112 | } |
| 1113 | |
| 1114 | /* If client sent routes of specific type, zebra removes it |
| 1115 | * and returns number of deleted routes. |
| 1116 | */ |
| 1117 | static void |
| 1118 | zebra_score_rib (int client_sock) |
| 1119 | { |
| 1120 | int i; |
| 1121 | |
| 1122 | for (i = ZEBRA_ROUTE_RIP; i < ZEBRA_ROUTE_MAX; i++) |
| 1123 | if (client_sock == route_type_oaths[i]) |
| 1124 | { |
| 1125 | zlog_notice ("client %d disconnected. %lu %s routes removed from the rib", |
| 1126 | client_sock, rib_score_proto (i), zebra_route_string (i)); |
| 1127 | route_type_oaths[i] = 0; |
| 1128 | break; |
| 1129 | } |
| 1130 | } |
| 1131 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1132 | /* Close zebra client. */ |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 1133 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1134 | zebra_client_close (struct zserv *client) |
| 1135 | { |
| 1136 | /* Close file descriptor. */ |
| 1137 | if (client->sock) |
| 1138 | { |
| 1139 | close (client->sock); |
Vyacheslav Trushkin | 2ea1ab1 | 2011-12-11 18:48:47 +0400 | [diff] [blame] | 1140 | zebra_score_rib (client->sock); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1141 | client->sock = -1; |
| 1142 | } |
| 1143 | |
| 1144 | /* Free stream buffers. */ |
| 1145 | if (client->ibuf) |
| 1146 | stream_free (client->ibuf); |
| 1147 | if (client->obuf) |
| 1148 | stream_free (client->obuf); |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1149 | if (client->wb) |
| 1150 | buffer_free(client->wb); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1151 | |
| 1152 | /* Release threads. */ |
| 1153 | if (client->t_read) |
| 1154 | thread_cancel (client->t_read); |
| 1155 | if (client->t_write) |
| 1156 | thread_cancel (client->t_write); |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1157 | if (client->t_suicide) |
| 1158 | thread_cancel (client->t_suicide); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1159 | |
| 1160 | /* Free client structure. */ |
paul | b21b19c | 2003-06-15 01:28:29 +0000 | [diff] [blame] | 1161 | listnode_delete (zebrad.client_list, client); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1162 | XFREE (0, client); |
| 1163 | } |
| 1164 | |
| 1165 | /* Make new client. */ |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 1166 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1167 | zebra_client_create (int sock) |
| 1168 | { |
| 1169 | struct zserv *client; |
| 1170 | |
| 1171 | client = XCALLOC (0, sizeof (struct zserv)); |
| 1172 | |
| 1173 | /* Make client input/output buffer. */ |
| 1174 | client->sock = sock; |
| 1175 | client->ibuf = stream_new (ZEBRA_MAX_PACKET_SIZ); |
| 1176 | client->obuf = stream_new (ZEBRA_MAX_PACKET_SIZ); |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1177 | client->wb = buffer_new(0); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1178 | |
| 1179 | /* Set table number. */ |
paul | b21b19c | 2003-06-15 01:28:29 +0000 | [diff] [blame] | 1180 | client->rtm_table = zebrad.rtm_table_default; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1181 | |
| 1182 | /* Add this client to linked list. */ |
paul | b21b19c | 2003-06-15 01:28:29 +0000 | [diff] [blame] | 1183 | listnode_add (zebrad.client_list, client); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1184 | |
| 1185 | /* Make new read thread. */ |
| 1186 | zebra_event (ZEBRA_READ, sock, client); |
| 1187 | } |
| 1188 | |
| 1189 | /* Handler of zebra service request. */ |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 1190 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1191 | zebra_client_read (struct thread *thread) |
| 1192 | { |
| 1193 | int sock; |
| 1194 | struct zserv *client; |
ajs | 57a1477 | 2005-04-10 15:01:56 +0000 | [diff] [blame] | 1195 | size_t already; |
paul | c1b9800 | 2006-01-16 01:54:02 +0000 | [diff] [blame] | 1196 | uint16_t length, command; |
| 1197 | uint8_t marker, version; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1198 | |
| 1199 | /* Get thread data. Reset reading thread because I'm running. */ |
| 1200 | sock = THREAD_FD (thread); |
| 1201 | client = THREAD_ARG (thread); |
| 1202 | client->t_read = NULL; |
| 1203 | |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1204 | if (client->t_suicide) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1205 | { |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1206 | zebra_client_close(client); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1207 | return -1; |
| 1208 | } |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1209 | |
| 1210 | /* Read length and command (if we don't have it already). */ |
ajs | 57a1477 | 2005-04-10 15:01:56 +0000 | [diff] [blame] | 1211 | if ((already = stream_get_endp(client->ibuf)) < ZEBRA_HEADER_SIZE) |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1212 | { |
ajs | 57a1477 | 2005-04-10 15:01:56 +0000 | [diff] [blame] | 1213 | ssize_t nbyte; |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1214 | if (((nbyte = stream_read_try (client->ibuf, sock, |
ajs | 57a1477 | 2005-04-10 15:01:56 +0000 | [diff] [blame] | 1215 | ZEBRA_HEADER_SIZE-already)) == 0) || |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1216 | (nbyte == -1)) |
| 1217 | { |
| 1218 | if (IS_ZEBRA_DEBUG_EVENT) |
| 1219 | zlog_debug ("connection closed socket [%d]", sock); |
| 1220 | zebra_client_close (client); |
| 1221 | return -1; |
| 1222 | } |
ajs | 57a1477 | 2005-04-10 15:01:56 +0000 | [diff] [blame] | 1223 | if (nbyte != (ssize_t)(ZEBRA_HEADER_SIZE-already)) |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1224 | { |
| 1225 | /* Try again later. */ |
| 1226 | zebra_event (ZEBRA_READ, sock, client); |
| 1227 | return 0; |
| 1228 | } |
ajs | 57a1477 | 2005-04-10 15:01:56 +0000 | [diff] [blame] | 1229 | already = ZEBRA_HEADER_SIZE; |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1230 | } |
| 1231 | |
| 1232 | /* Reset to read from the beginning of the incoming packet. */ |
| 1233 | stream_set_getp(client->ibuf, 0); |
| 1234 | |
paul | c1b9800 | 2006-01-16 01:54:02 +0000 | [diff] [blame] | 1235 | /* Fetch header values */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1236 | length = stream_getw (client->ibuf); |
paul | c1b9800 | 2006-01-16 01:54:02 +0000 | [diff] [blame] | 1237 | marker = stream_getc (client->ibuf); |
| 1238 | version = stream_getc (client->ibuf); |
| 1239 | command = stream_getw (client->ibuf); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1240 | |
paul | c1b9800 | 2006-01-16 01:54:02 +0000 | [diff] [blame] | 1241 | if (marker != ZEBRA_HEADER_MARKER || version != ZSERV_VERSION) |
| 1242 | { |
| 1243 | zlog_err("%s: socket %d version mismatch, marker %d, version %d", |
| 1244 | __func__, sock, marker, version); |
| 1245 | zebra_client_close (client); |
| 1246 | return -1; |
| 1247 | } |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1248 | if (length < ZEBRA_HEADER_SIZE) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1249 | { |
ajs | 57a1477 | 2005-04-10 15:01:56 +0000 | [diff] [blame] | 1250 | zlog_warn("%s: socket %d message length %u is less than header size %d", |
| 1251 | __func__, sock, length, ZEBRA_HEADER_SIZE); |
| 1252 | zebra_client_close (client); |
| 1253 | return -1; |
| 1254 | } |
| 1255 | if (length > STREAM_SIZE(client->ibuf)) |
| 1256 | { |
| 1257 | zlog_warn("%s: socket %d message length %u exceeds buffer size %lu", |
| 1258 | __func__, sock, length, (u_long)STREAM_SIZE(client->ibuf)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1259 | zebra_client_close (client); |
| 1260 | return -1; |
| 1261 | } |
| 1262 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1263 | /* Read rest of data. */ |
ajs | 57a1477 | 2005-04-10 15:01:56 +0000 | [diff] [blame] | 1264 | if (already < length) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1265 | { |
ajs | 57a1477 | 2005-04-10 15:01:56 +0000 | [diff] [blame] | 1266 | ssize_t nbyte; |
| 1267 | if (((nbyte = stream_read_try (client->ibuf, sock, |
| 1268 | length-already)) == 0) || |
| 1269 | (nbyte == -1)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1270 | { |
| 1271 | if (IS_ZEBRA_DEBUG_EVENT) |
ajs | b617800 | 2004-12-07 21:12:56 +0000 | [diff] [blame] | 1272 | zlog_debug ("connection closed [%d] when reading zebra data", sock); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1273 | zebra_client_close (client); |
| 1274 | return -1; |
| 1275 | } |
ajs | 57a1477 | 2005-04-10 15:01:56 +0000 | [diff] [blame] | 1276 | if (nbyte != (ssize_t)(length-already)) |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1277 | { |
| 1278 | /* Try again later. */ |
| 1279 | zebra_event (ZEBRA_READ, sock, client); |
| 1280 | return 0; |
| 1281 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1282 | } |
| 1283 | |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1284 | length -= ZEBRA_HEADER_SIZE; |
| 1285 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1286 | /* Debug packet information. */ |
| 1287 | if (IS_ZEBRA_DEBUG_EVENT) |
ajs | b617800 | 2004-12-07 21:12:56 +0000 | [diff] [blame] | 1288 | zlog_debug ("zebra message comes from socket [%d]", sock); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1289 | |
| 1290 | if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV) |
ajs | b617800 | 2004-12-07 21:12:56 +0000 | [diff] [blame] | 1291 | zlog_debug ("zebra message received [%s] %d", |
Paul Jakma | 6685978 | 2006-05-15 17:00:37 +0000 | [diff] [blame] | 1292 | zserv_command_string (command), length); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1293 | |
| 1294 | switch (command) |
| 1295 | { |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 1296 | case ZEBRA_ROUTER_ID_ADD: |
| 1297 | zread_router_id_add (client, length); |
| 1298 | break; |
| 1299 | case ZEBRA_ROUTER_ID_DELETE: |
| 1300 | zread_router_id_delete (client, length); |
| 1301 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1302 | case ZEBRA_INTERFACE_ADD: |
| 1303 | zread_interface_add (client, length); |
| 1304 | break; |
| 1305 | case ZEBRA_INTERFACE_DELETE: |
| 1306 | zread_interface_delete (client, length); |
| 1307 | break; |
| 1308 | case ZEBRA_IPV4_ROUTE_ADD: |
| 1309 | zread_ipv4_add (client, length); |
| 1310 | break; |
| 1311 | case ZEBRA_IPV4_ROUTE_DELETE: |
| 1312 | zread_ipv4_delete (client, length); |
| 1313 | break; |
| 1314 | #ifdef HAVE_IPV6 |
| 1315 | case ZEBRA_IPV6_ROUTE_ADD: |
| 1316 | zread_ipv6_add (client, length); |
| 1317 | break; |
| 1318 | case ZEBRA_IPV6_ROUTE_DELETE: |
| 1319 | zread_ipv6_delete (client, length); |
| 1320 | break; |
| 1321 | #endif /* HAVE_IPV6 */ |
| 1322 | case ZEBRA_REDISTRIBUTE_ADD: |
| 1323 | zebra_redistribute_add (command, client, length); |
| 1324 | break; |
| 1325 | case ZEBRA_REDISTRIBUTE_DELETE: |
| 1326 | zebra_redistribute_delete (command, client, length); |
| 1327 | break; |
| 1328 | case ZEBRA_REDISTRIBUTE_DEFAULT_ADD: |
| 1329 | zebra_redistribute_default_add (command, client, length); |
| 1330 | break; |
| 1331 | case ZEBRA_REDISTRIBUTE_DEFAULT_DELETE: |
| 1332 | zebra_redistribute_default_delete (command, client, length); |
| 1333 | break; |
| 1334 | case ZEBRA_IPV4_NEXTHOP_LOOKUP: |
| 1335 | zread_ipv4_nexthop_lookup (client, length); |
| 1336 | break; |
| 1337 | #ifdef HAVE_IPV6 |
| 1338 | case ZEBRA_IPV6_NEXTHOP_LOOKUP: |
| 1339 | zread_ipv6_nexthop_lookup (client, length); |
| 1340 | break; |
| 1341 | #endif /* HAVE_IPV6 */ |
| 1342 | case ZEBRA_IPV4_IMPORT_LOOKUP: |
| 1343 | zread_ipv4_import_lookup (client, length); |
| 1344 | break; |
Vyacheslav Trushkin | 2ea1ab1 | 2011-12-11 18:48:47 +0400 | [diff] [blame] | 1345 | case ZEBRA_HELLO: |
| 1346 | zread_hello (client); |
| 1347 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1348 | default: |
| 1349 | zlog_info ("Zebra received unknown command %d", command); |
| 1350 | break; |
| 1351 | } |
| 1352 | |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1353 | if (client->t_suicide) |
| 1354 | { |
| 1355 | /* No need to wait for thread callback, just kill immediately. */ |
| 1356 | zebra_client_close(client); |
| 1357 | return -1; |
| 1358 | } |
| 1359 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1360 | stream_reset (client->ibuf); |
| 1361 | zebra_event (ZEBRA_READ, sock, client); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1362 | return 0; |
| 1363 | } |
| 1364 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1365 | |
| 1366 | /* Accept code of zebra server socket. */ |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 1367 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1368 | zebra_accept (struct thread *thread) |
| 1369 | { |
| 1370 | int accept_sock; |
| 1371 | int client_sock; |
| 1372 | struct sockaddr_in client; |
| 1373 | socklen_t len; |
| 1374 | |
| 1375 | accept_sock = THREAD_FD (thread); |
| 1376 | |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1377 | /* Reregister myself. */ |
| 1378 | zebra_event (ZEBRA_SERV, accept_sock, NULL); |
| 1379 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1380 | len = sizeof (struct sockaddr_in); |
| 1381 | client_sock = accept (accept_sock, (struct sockaddr *) &client, &len); |
| 1382 | |
| 1383 | if (client_sock < 0) |
| 1384 | { |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 1385 | zlog_warn ("Can't accept zebra socket: %s", safe_strerror (errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1386 | return -1; |
| 1387 | } |
| 1388 | |
paul | ccf3557 | 2003-03-01 11:42:20 +0000 | [diff] [blame] | 1389 | /* Make client socket non-blocking. */ |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1390 | set_nonblocking(client_sock); |
paul | 865b852 | 2005-01-05 08:30:35 +0000 | [diff] [blame] | 1391 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1392 | /* Create new zebra client. */ |
| 1393 | zebra_client_create (client_sock); |
| 1394 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1395 | return 0; |
| 1396 | } |
| 1397 | |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 1398 | #ifdef HAVE_TCP_ZEBRA |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1399 | /* Make zebra's server socket. */ |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 1400 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1401 | zebra_serv () |
| 1402 | { |
| 1403 | int ret; |
| 1404 | int accept_sock; |
| 1405 | struct sockaddr_in addr; |
| 1406 | |
| 1407 | accept_sock = socket (AF_INET, SOCK_STREAM, 0); |
| 1408 | |
| 1409 | if (accept_sock < 0) |
| 1410 | { |
paul | 3d1dc85 | 2005-04-05 00:45:23 +0000 | [diff] [blame] | 1411 | zlog_warn ("Can't create zserv stream socket: %s", |
| 1412 | safe_strerror (errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1413 | zlog_warn ("zebra can't provice full functionality due to above error"); |
| 1414 | return; |
| 1415 | } |
| 1416 | |
Vyacheslav Trushkin | 2ea1ab1 | 2011-12-11 18:48:47 +0400 | [diff] [blame] | 1417 | memset (&route_type_oaths, 0, sizeof (route_type_oaths)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1418 | memset (&addr, 0, sizeof (struct sockaddr_in)); |
| 1419 | addr.sin_family = AF_INET; |
| 1420 | addr.sin_port = htons (ZEBRA_PORT); |
Paul Jakma | 6f0e3f6 | 2007-05-10 02:38:51 +0000 | [diff] [blame] | 1421 | #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1422 | addr.sin_len = sizeof (struct sockaddr_in); |
Paul Jakma | 6f0e3f6 | 2007-05-10 02:38:51 +0000 | [diff] [blame] | 1423 | #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1424 | addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK); |
| 1425 | |
| 1426 | sockopt_reuseaddr (accept_sock); |
| 1427 | sockopt_reuseport (accept_sock); |
| 1428 | |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 1429 | if ( zserv_privs.change(ZPRIVS_RAISE) ) |
| 1430 | zlog (NULL, LOG_ERR, "Can't raise privileges"); |
| 1431 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1432 | ret = bind (accept_sock, (struct sockaddr *)&addr, |
| 1433 | sizeof (struct sockaddr_in)); |
| 1434 | if (ret < 0) |
| 1435 | { |
paul | 3d1dc85 | 2005-04-05 00:45:23 +0000 | [diff] [blame] | 1436 | zlog_warn ("Can't bind to stream socket: %s", |
| 1437 | safe_strerror (errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1438 | zlog_warn ("zebra can't provice full functionality due to above error"); |
| 1439 | close (accept_sock); /* Avoid sd leak. */ |
| 1440 | return; |
| 1441 | } |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 1442 | |
| 1443 | if ( zserv_privs.change(ZPRIVS_LOWER) ) |
| 1444 | zlog (NULL, LOG_ERR, "Can't lower privileges"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1445 | |
| 1446 | ret = listen (accept_sock, 1); |
| 1447 | if (ret < 0) |
| 1448 | { |
paul | 3d1dc85 | 2005-04-05 00:45:23 +0000 | [diff] [blame] | 1449 | zlog_warn ("Can't listen to stream socket: %s", |
| 1450 | safe_strerror (errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1451 | zlog_warn ("zebra can't provice full functionality due to above error"); |
| 1452 | close (accept_sock); /* Avoid sd leak. */ |
| 1453 | return; |
| 1454 | } |
| 1455 | |
| 1456 | zebra_event (ZEBRA_SERV, accept_sock, NULL); |
| 1457 | } |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 1458 | #endif /* HAVE_TCP_ZEBRA */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1459 | |
| 1460 | /* For sockaddr_un. */ |
| 1461 | #include <sys/un.h> |
| 1462 | |
| 1463 | /* zebra server UNIX domain socket. */ |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 1464 | static void |
hasso | fce954f | 2004-10-07 20:29:24 +0000 | [diff] [blame] | 1465 | zebra_serv_un (const char *path) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1466 | { |
| 1467 | int ret; |
| 1468 | int sock, len; |
| 1469 | struct sockaddr_un serv; |
| 1470 | mode_t old_mask; |
| 1471 | |
| 1472 | /* First of all, unlink existing socket */ |
| 1473 | unlink (path); |
| 1474 | |
| 1475 | /* Set umask */ |
| 1476 | old_mask = umask (0077); |
| 1477 | |
| 1478 | /* Make UNIX domain socket. */ |
| 1479 | sock = socket (AF_UNIX, SOCK_STREAM, 0); |
| 1480 | if (sock < 0) |
| 1481 | { |
paul | 3d1dc85 | 2005-04-05 00:45:23 +0000 | [diff] [blame] | 1482 | zlog_warn ("Can't create zserv unix socket: %s", |
| 1483 | safe_strerror (errno)); |
| 1484 | zlog_warn ("zebra can't provide full functionality due to above error"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1485 | return; |
| 1486 | } |
| 1487 | |
Vyacheslav Trushkin | 2ea1ab1 | 2011-12-11 18:48:47 +0400 | [diff] [blame] | 1488 | memset (&route_type_oaths, 0, sizeof (route_type_oaths)); |
| 1489 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1490 | /* Make server socket. */ |
| 1491 | memset (&serv, 0, sizeof (struct sockaddr_un)); |
| 1492 | serv.sun_family = AF_UNIX; |
| 1493 | strncpy (serv.sun_path, path, strlen (path)); |
Paul Jakma | 6f0e3f6 | 2007-05-10 02:38:51 +0000 | [diff] [blame] | 1494 | #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1495 | len = serv.sun_len = SUN_LEN(&serv); |
| 1496 | #else |
| 1497 | len = sizeof (serv.sun_family) + strlen (serv.sun_path); |
Paul Jakma | 6f0e3f6 | 2007-05-10 02:38:51 +0000 | [diff] [blame] | 1498 | #endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1499 | |
| 1500 | ret = bind (sock, (struct sockaddr *) &serv, len); |
| 1501 | if (ret < 0) |
| 1502 | { |
paul | 3d1dc85 | 2005-04-05 00:45:23 +0000 | [diff] [blame] | 1503 | zlog_warn ("Can't bind to unix socket %s: %s", |
| 1504 | path, safe_strerror (errno)); |
| 1505 | zlog_warn ("zebra can't provide full functionality due to above error"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1506 | close (sock); |
| 1507 | return; |
| 1508 | } |
| 1509 | |
| 1510 | ret = listen (sock, 5); |
| 1511 | if (ret < 0) |
| 1512 | { |
paul | 3d1dc85 | 2005-04-05 00:45:23 +0000 | [diff] [blame] | 1513 | zlog_warn ("Can't listen to unix socket %s: %s", |
| 1514 | path, safe_strerror (errno)); |
| 1515 | zlog_warn ("zebra can't provide full functionality due to above error"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1516 | close (sock); |
| 1517 | return; |
| 1518 | } |
| 1519 | |
| 1520 | umask (old_mask); |
| 1521 | |
| 1522 | zebra_event (ZEBRA_SERV, sock, NULL); |
| 1523 | } |
| 1524 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1525 | |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 1526 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1527 | zebra_event (enum event event, int sock, struct zserv *client) |
| 1528 | { |
| 1529 | switch (event) |
| 1530 | { |
| 1531 | case ZEBRA_SERV: |
paul | b21b19c | 2003-06-15 01:28:29 +0000 | [diff] [blame] | 1532 | thread_add_read (zebrad.master, zebra_accept, client, sock); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1533 | break; |
| 1534 | case ZEBRA_READ: |
| 1535 | client->t_read = |
paul | b21b19c | 2003-06-15 01:28:29 +0000 | [diff] [blame] | 1536 | thread_add_read (zebrad.master, zebra_client_read, client, sock); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1537 | break; |
| 1538 | case ZEBRA_WRITE: |
| 1539 | /**/ |
| 1540 | break; |
| 1541 | } |
| 1542 | } |
| 1543 | |
| 1544 | /* Display default rtm_table for all clients. */ |
| 1545 | DEFUN (show_table, |
| 1546 | show_table_cmd, |
| 1547 | "show table", |
| 1548 | SHOW_STR |
| 1549 | "default routing table to use for all clients\n") |
| 1550 | { |
paul | b21b19c | 2003-06-15 01:28:29 +0000 | [diff] [blame] | 1551 | vty_out (vty, "table %d%s", zebrad.rtm_table_default, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1552 | VTY_NEWLINE); |
| 1553 | return CMD_SUCCESS; |
| 1554 | } |
| 1555 | |
| 1556 | DEFUN (config_table, |
| 1557 | config_table_cmd, |
| 1558 | "table TABLENO", |
| 1559 | "Configure target kernel routing table\n" |
| 1560 | "TABLE integer\n") |
| 1561 | { |
paul | b21b19c | 2003-06-15 01:28:29 +0000 | [diff] [blame] | 1562 | zebrad.rtm_table_default = strtol (argv[0], (char**)0, 10); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1563 | return CMD_SUCCESS; |
| 1564 | } |
| 1565 | |
hasso | 647e4f1 | 2003-05-25 11:43:52 +0000 | [diff] [blame] | 1566 | DEFUN (ip_forwarding, |
| 1567 | ip_forwarding_cmd, |
| 1568 | "ip forwarding", |
| 1569 | IP_STR |
| 1570 | "Turn on IP forwarding") |
| 1571 | { |
| 1572 | int ret; |
| 1573 | |
| 1574 | ret = ipforward (); |
hasso | b71f00f | 2004-10-13 12:20:35 +0000 | [diff] [blame] | 1575 | if (ret == 0) |
| 1576 | ret = ipforward_on (); |
hasso | 647e4f1 | 2003-05-25 11:43:52 +0000 | [diff] [blame] | 1577 | |
hasso | 647e4f1 | 2003-05-25 11:43:52 +0000 | [diff] [blame] | 1578 | if (ret == 0) |
| 1579 | { |
| 1580 | vty_out (vty, "Can't turn on IP forwarding%s", VTY_NEWLINE); |
| 1581 | return CMD_WARNING; |
| 1582 | } |
| 1583 | |
| 1584 | return CMD_SUCCESS; |
| 1585 | } |
| 1586 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1587 | DEFUN (no_ip_forwarding, |
| 1588 | no_ip_forwarding_cmd, |
| 1589 | "no ip forwarding", |
| 1590 | NO_STR |
| 1591 | IP_STR |
| 1592 | "Turn off IP forwarding") |
| 1593 | { |
| 1594 | int ret; |
| 1595 | |
| 1596 | ret = ipforward (); |
hasso | b71f00f | 2004-10-13 12:20:35 +0000 | [diff] [blame] | 1597 | if (ret != 0) |
| 1598 | ret = ipforward_off (); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1599 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1600 | if (ret != 0) |
| 1601 | { |
| 1602 | vty_out (vty, "Can't turn off IP forwarding%s", VTY_NEWLINE); |
| 1603 | return CMD_WARNING; |
| 1604 | } |
| 1605 | |
| 1606 | return CMD_SUCCESS; |
| 1607 | } |
| 1608 | |
| 1609 | /* This command is for debugging purpose. */ |
| 1610 | DEFUN (show_zebra_client, |
| 1611 | show_zebra_client_cmd, |
| 1612 | "show zebra client", |
| 1613 | SHOW_STR |
| 1614 | "Zebra information" |
| 1615 | "Client information") |
| 1616 | { |
hasso | 52dc7ee | 2004-09-23 19:18:23 +0000 | [diff] [blame] | 1617 | struct listnode *node; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1618 | struct zserv *client; |
| 1619 | |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 1620 | for (ALL_LIST_ELEMENTS_RO (zebrad.client_list, node, client)) |
| 1621 | vty_out (vty, "Client fd %d%s", client->sock, VTY_NEWLINE); |
| 1622 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1623 | return CMD_SUCCESS; |
| 1624 | } |
| 1625 | |
| 1626 | /* Table configuration write function. */ |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 1627 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1628 | config_write_table (struct vty *vty) |
| 1629 | { |
paul | b21b19c | 2003-06-15 01:28:29 +0000 | [diff] [blame] | 1630 | if (zebrad.rtm_table_default) |
| 1631 | vty_out (vty, "table %d%s", zebrad.rtm_table_default, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1632 | VTY_NEWLINE); |
| 1633 | return 0; |
| 1634 | } |
| 1635 | |
| 1636 | /* table node for routing tables. */ |
Stephen Hemminger | 7fc626d | 2008-12-01 11:10:34 -0800 | [diff] [blame] | 1637 | static struct cmd_node table_node = |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1638 | { |
| 1639 | TABLE_NODE, |
| 1640 | "", /* This node has no interface. */ |
| 1641 | 1 |
| 1642 | }; |
| 1643 | |
| 1644 | /* Only display ip forwarding is enabled or not. */ |
| 1645 | DEFUN (show_ip_forwarding, |
| 1646 | show_ip_forwarding_cmd, |
| 1647 | "show ip forwarding", |
| 1648 | SHOW_STR |
| 1649 | IP_STR |
| 1650 | "IP forwarding status\n") |
| 1651 | { |
| 1652 | int ret; |
| 1653 | |
| 1654 | ret = ipforward (); |
| 1655 | |
| 1656 | if (ret == 0) |
| 1657 | vty_out (vty, "IP forwarding is off%s", VTY_NEWLINE); |
| 1658 | else |
| 1659 | vty_out (vty, "IP forwarding is on%s", VTY_NEWLINE); |
| 1660 | return CMD_SUCCESS; |
| 1661 | } |
| 1662 | |
| 1663 | #ifdef HAVE_IPV6 |
| 1664 | /* Only display ipv6 forwarding is enabled or not. */ |
| 1665 | DEFUN (show_ipv6_forwarding, |
| 1666 | show_ipv6_forwarding_cmd, |
| 1667 | "show ipv6 forwarding", |
| 1668 | SHOW_STR |
| 1669 | "IPv6 information\n" |
| 1670 | "Forwarding status\n") |
| 1671 | { |
| 1672 | int ret; |
| 1673 | |
| 1674 | ret = ipforward_ipv6 (); |
| 1675 | |
| 1676 | switch (ret) |
| 1677 | { |
| 1678 | case -1: |
| 1679 | vty_out (vty, "ipv6 forwarding is unknown%s", VTY_NEWLINE); |
| 1680 | break; |
| 1681 | case 0: |
| 1682 | vty_out (vty, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE); |
| 1683 | break; |
| 1684 | case 1: |
| 1685 | vty_out (vty, "ipv6 forwarding is %s%s", "on", VTY_NEWLINE); |
| 1686 | break; |
| 1687 | default: |
| 1688 | vty_out (vty, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE); |
| 1689 | break; |
| 1690 | } |
| 1691 | return CMD_SUCCESS; |
| 1692 | } |
| 1693 | |
hasso | 5590672 | 2004-02-11 22:42:16 +0000 | [diff] [blame] | 1694 | DEFUN (ipv6_forwarding, |
| 1695 | ipv6_forwarding_cmd, |
| 1696 | "ipv6 forwarding", |
| 1697 | IPV6_STR |
| 1698 | "Turn on IPv6 forwarding") |
| 1699 | { |
| 1700 | int ret; |
| 1701 | |
hasso | 41d3fc9 | 2004-04-06 11:59:00 +0000 | [diff] [blame] | 1702 | ret = ipforward_ipv6 (); |
hasso | b71f00f | 2004-10-13 12:20:35 +0000 | [diff] [blame] | 1703 | if (ret == 0) |
| 1704 | ret = ipforward_ipv6_on (); |
hasso | 41d3fc9 | 2004-04-06 11:59:00 +0000 | [diff] [blame] | 1705 | |
hasso | 41d3fc9 | 2004-04-06 11:59:00 +0000 | [diff] [blame] | 1706 | if (ret == 0) |
| 1707 | { |
hasso | 5590672 | 2004-02-11 22:42:16 +0000 | [diff] [blame] | 1708 | vty_out (vty, "Can't turn on IPv6 forwarding%s", VTY_NEWLINE); |
| 1709 | return CMD_WARNING; |
| 1710 | } |
| 1711 | |
| 1712 | return CMD_SUCCESS; |
| 1713 | } |
| 1714 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1715 | DEFUN (no_ipv6_forwarding, |
| 1716 | no_ipv6_forwarding_cmd, |
| 1717 | "no ipv6 forwarding", |
| 1718 | NO_STR |
hasso | 5590672 | 2004-02-11 22:42:16 +0000 | [diff] [blame] | 1719 | IPV6_STR |
| 1720 | "Turn off IPv6 forwarding") |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1721 | { |
| 1722 | int ret; |
| 1723 | |
hasso | 41d3fc9 | 2004-04-06 11:59:00 +0000 | [diff] [blame] | 1724 | ret = ipforward_ipv6 (); |
hasso | b71f00f | 2004-10-13 12:20:35 +0000 | [diff] [blame] | 1725 | if (ret != 0) |
| 1726 | ret = ipforward_ipv6_off (); |
hasso | 41d3fc9 | 2004-04-06 11:59:00 +0000 | [diff] [blame] | 1727 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1728 | if (ret != 0) |
| 1729 | { |
| 1730 | vty_out (vty, "Can't turn off IPv6 forwarding%s", VTY_NEWLINE); |
| 1731 | return CMD_WARNING; |
| 1732 | } |
| 1733 | |
| 1734 | return CMD_SUCCESS; |
| 1735 | } |
| 1736 | |
| 1737 | #endif /* HAVE_IPV6 */ |
| 1738 | |
| 1739 | /* IPForwarding configuration write function. */ |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1740 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1741 | config_write_forwarding (struct vty *vty) |
| 1742 | { |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 1743 | /* FIXME: Find better place for that. */ |
| 1744 | router_id_write (vty); |
| 1745 | |
paul | 3e0b3a5 | 2004-08-23 18:58:32 +0000 | [diff] [blame] | 1746 | if (ipforward ()) |
| 1747 | vty_out (vty, "ip forwarding%s", VTY_NEWLINE); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1748 | #ifdef HAVE_IPV6 |
paul | 3e0b3a5 | 2004-08-23 18:58:32 +0000 | [diff] [blame] | 1749 | if (ipforward_ipv6 ()) |
| 1750 | vty_out (vty, "ipv6 forwarding%s", VTY_NEWLINE); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1751 | #endif /* HAVE_IPV6 */ |
| 1752 | vty_out (vty, "!%s", VTY_NEWLINE); |
| 1753 | return 0; |
| 1754 | } |
| 1755 | |
| 1756 | /* table node for routing tables. */ |
Stephen Hemminger | 7fc626d | 2008-12-01 11:10:34 -0800 | [diff] [blame] | 1757 | static struct cmd_node forwarding_node = |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1758 | { |
| 1759 | FORWARDING_NODE, |
| 1760 | "", /* This node has no interface. */ |
| 1761 | 1 |
| 1762 | }; |
| 1763 | |
| 1764 | |
| 1765 | /* Initialisation of zebra and installation of commands. */ |
| 1766 | void |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 1767 | zebra_init (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1768 | { |
| 1769 | /* Client list init. */ |
paul | b21b19c | 2003-06-15 01:28:29 +0000 | [diff] [blame] | 1770 | zebrad.client_list = list_new (); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1771 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1772 | /* Install configuration write function. */ |
| 1773 | install_node (&table_node, config_write_table); |
| 1774 | install_node (&forwarding_node, config_write_forwarding); |
| 1775 | |
| 1776 | install_element (VIEW_NODE, &show_ip_forwarding_cmd); |
| 1777 | install_element (ENABLE_NODE, &show_ip_forwarding_cmd); |
hasso | 647e4f1 | 2003-05-25 11:43:52 +0000 | [diff] [blame] | 1778 | install_element (CONFIG_NODE, &ip_forwarding_cmd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1779 | install_element (CONFIG_NODE, &no_ip_forwarding_cmd); |
| 1780 | install_element (ENABLE_NODE, &show_zebra_client_cmd); |
| 1781 | |
| 1782 | #ifdef HAVE_NETLINK |
| 1783 | install_element (VIEW_NODE, &show_table_cmd); |
| 1784 | install_element (ENABLE_NODE, &show_table_cmd); |
| 1785 | install_element (CONFIG_NODE, &config_table_cmd); |
| 1786 | #endif /* HAVE_NETLINK */ |
| 1787 | |
| 1788 | #ifdef HAVE_IPV6 |
| 1789 | install_element (VIEW_NODE, &show_ipv6_forwarding_cmd); |
| 1790 | install_element (ENABLE_NODE, &show_ipv6_forwarding_cmd); |
hasso | 5590672 | 2004-02-11 22:42:16 +0000 | [diff] [blame] | 1791 | install_element (CONFIG_NODE, &ipv6_forwarding_cmd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1792 | install_element (CONFIG_NODE, &no_ipv6_forwarding_cmd); |
| 1793 | #endif /* HAVE_IPV6 */ |
Paul Jakma | 7514fb7 | 2007-05-02 16:05:35 +0000 | [diff] [blame] | 1794 | |
| 1795 | /* Route-map */ |
| 1796 | zebra_route_map_init (); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1797 | } |
Denis Ovsienko | 97be79f | 2009-07-24 20:45:31 +0400 | [diff] [blame] | 1798 | |
| 1799 | /* Make zebra server socket, wiping any existing one (see bug #403). */ |
| 1800 | void |
Vyacheslav Trushkin | b511468 | 2011-11-25 18:51:48 +0400 | [diff] [blame] | 1801 | zebra_zserv_socket_init (char *path) |
Denis Ovsienko | 97be79f | 2009-07-24 20:45:31 +0400 | [diff] [blame] | 1802 | { |
| 1803 | #ifdef HAVE_TCP_ZEBRA |
| 1804 | zebra_serv (); |
| 1805 | #else |
Vyacheslav Trushkin | b511468 | 2011-11-25 18:51:48 +0400 | [diff] [blame] | 1806 | zebra_serv_un (path ? path : ZEBRA_SERV_PATH); |
Denis Ovsienko | 97be79f | 2009-07-24 20:45:31 +0400 | [diff] [blame] | 1807 | #endif /* HAVE_TCP_ZEBRA */ |
| 1808 | } |