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