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