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