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