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