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)) |
hasso | be61c4e | 2005-08-27 06:05:47 +0000 | [diff] [blame] | 1002 | rib_add_ipv6 (api.type, api.flags, &p, NULL, ifindex, 0, api.metric, |
| 1003 | api.distance); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1004 | else |
hasso | be61c4e | 2005-08-27 06:05:47 +0000 | [diff] [blame] | 1005 | rib_add_ipv6 (api.type, api.flags, &p, &nexthop, ifindex, 0, api.metric, |
| 1006 | api.distance); |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1007 | return 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1008 | } |
| 1009 | |
| 1010 | /* Zebra server IPv6 prefix delete function. */ |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1011 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1012 | zread_ipv6_delete (struct zserv *client, u_short length) |
| 1013 | { |
| 1014 | int i; |
| 1015 | struct stream *s; |
| 1016 | struct zapi_ipv6 api; |
| 1017 | struct in6_addr nexthop; |
| 1018 | unsigned long ifindex; |
| 1019 | struct prefix_ipv6 p; |
| 1020 | |
| 1021 | s = client->ibuf; |
| 1022 | ifindex = 0; |
| 1023 | memset (&nexthop, 0, sizeof (struct in6_addr)); |
| 1024 | |
| 1025 | /* Type, flags, message. */ |
| 1026 | api.type = stream_getc (s); |
| 1027 | api.flags = stream_getc (s); |
| 1028 | api.message = stream_getc (s); |
| 1029 | |
| 1030 | /* IPv4 prefix. */ |
| 1031 | memset (&p, 0, sizeof (struct prefix_ipv6)); |
| 1032 | p.family = AF_INET6; |
| 1033 | p.prefixlen = stream_getc (s); |
| 1034 | stream_get (&p.prefix, s, PSIZE (p.prefixlen)); |
| 1035 | |
| 1036 | /* Nexthop, ifindex, distance, metric. */ |
| 1037 | if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP)) |
| 1038 | { |
| 1039 | u_char nexthop_type; |
| 1040 | |
| 1041 | api.nexthop_num = stream_getc (s); |
| 1042 | for (i = 0; i < api.nexthop_num; i++) |
| 1043 | { |
| 1044 | nexthop_type = stream_getc (s); |
| 1045 | |
| 1046 | switch (nexthop_type) |
| 1047 | { |
| 1048 | case ZEBRA_NEXTHOP_IPV6: |
| 1049 | stream_get (&nexthop, s, 16); |
| 1050 | break; |
| 1051 | case ZEBRA_NEXTHOP_IFINDEX: |
| 1052 | ifindex = stream_getl (s); |
| 1053 | break; |
| 1054 | } |
| 1055 | } |
| 1056 | } |
| 1057 | |
| 1058 | if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE)) |
| 1059 | api.distance = stream_getc (s); |
| 1060 | else |
| 1061 | api.distance = 0; |
| 1062 | if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC)) |
| 1063 | api.metric = stream_getl (s); |
| 1064 | else |
| 1065 | api.metric = 0; |
| 1066 | |
| 1067 | if (IN6_IS_ADDR_UNSPECIFIED (&nexthop)) |
| 1068 | rib_delete_ipv6 (api.type, api.flags, &p, NULL, ifindex, 0); |
| 1069 | else |
| 1070 | rib_delete_ipv6 (api.type, api.flags, &p, &nexthop, ifindex, 0); |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1071 | return 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1072 | } |
| 1073 | |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1074 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1075 | zread_ipv6_nexthop_lookup (struct zserv *client, u_short length) |
| 1076 | { |
| 1077 | struct in6_addr addr; |
| 1078 | char buf[BUFSIZ]; |
| 1079 | |
| 1080 | stream_get (&addr, client->ibuf, 16); |
| 1081 | printf ("DEBUG %s\n", inet_ntop (AF_INET6, &addr, buf, BUFSIZ)); |
| 1082 | |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1083 | return zsend_ipv6_nexthop_lookup (client, &addr); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1084 | } |
| 1085 | #endif /* HAVE_IPV6 */ |
| 1086 | |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 1087 | /* Register zebra server router-id information. Send current router-id */ |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1088 | static int |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 1089 | zread_router_id_add (struct zserv *client, u_short length) |
| 1090 | { |
| 1091 | struct prefix p; |
| 1092 | |
| 1093 | /* Router-id information is needed. */ |
| 1094 | client->ridinfo = 1; |
| 1095 | |
| 1096 | router_id_get (&p); |
| 1097 | |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1098 | return zsend_router_id_update (client,&p); |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 1099 | } |
| 1100 | |
| 1101 | /* Unregister zebra server router-id information. */ |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1102 | static int |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 1103 | zread_router_id_delete (struct zserv *client, u_short length) |
| 1104 | { |
| 1105 | client->ridinfo = 0; |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1106 | return 0; |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 1107 | } |
| 1108 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1109 | /* Close zebra client. */ |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 1110 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1111 | zebra_client_close (struct zserv *client) |
| 1112 | { |
| 1113 | /* Close file descriptor. */ |
| 1114 | if (client->sock) |
| 1115 | { |
| 1116 | close (client->sock); |
| 1117 | client->sock = -1; |
| 1118 | } |
| 1119 | |
| 1120 | /* Free stream buffers. */ |
| 1121 | if (client->ibuf) |
| 1122 | stream_free (client->ibuf); |
| 1123 | if (client->obuf) |
| 1124 | stream_free (client->obuf); |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1125 | if (client->wb) |
| 1126 | buffer_free(client->wb); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1127 | |
| 1128 | /* Release threads. */ |
| 1129 | if (client->t_read) |
| 1130 | thread_cancel (client->t_read); |
| 1131 | if (client->t_write) |
| 1132 | thread_cancel (client->t_write); |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1133 | if (client->t_suicide) |
| 1134 | thread_cancel (client->t_suicide); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1135 | |
| 1136 | /* Free client structure. */ |
paul | b21b19c | 2003-06-15 01:28:29 +0000 | [diff] [blame] | 1137 | listnode_delete (zebrad.client_list, client); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1138 | XFREE (0, client); |
| 1139 | } |
| 1140 | |
| 1141 | /* Make new client. */ |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 1142 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1143 | zebra_client_create (int sock) |
| 1144 | { |
| 1145 | struct zserv *client; |
| 1146 | |
| 1147 | client = XCALLOC (0, sizeof (struct zserv)); |
| 1148 | |
| 1149 | /* Make client input/output buffer. */ |
| 1150 | client->sock = sock; |
| 1151 | client->ibuf = stream_new (ZEBRA_MAX_PACKET_SIZ); |
| 1152 | client->obuf = stream_new (ZEBRA_MAX_PACKET_SIZ); |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1153 | client->wb = buffer_new(0); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1154 | |
| 1155 | /* Set table number. */ |
paul | b21b19c | 2003-06-15 01:28:29 +0000 | [diff] [blame] | 1156 | client->rtm_table = zebrad.rtm_table_default; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1157 | |
| 1158 | /* Add this client to linked list. */ |
paul | b21b19c | 2003-06-15 01:28:29 +0000 | [diff] [blame] | 1159 | listnode_add (zebrad.client_list, client); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1160 | |
| 1161 | /* Make new read thread. */ |
| 1162 | zebra_event (ZEBRA_READ, sock, client); |
| 1163 | } |
| 1164 | |
| 1165 | /* Handler of zebra service request. */ |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 1166 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1167 | zebra_client_read (struct thread *thread) |
| 1168 | { |
| 1169 | int sock; |
| 1170 | struct zserv *client; |
ajs | 57a1477 | 2005-04-10 15:01:56 +0000 | [diff] [blame] | 1171 | size_t already; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1172 | u_short length; |
| 1173 | u_char command; |
| 1174 | |
| 1175 | /* Get thread data. Reset reading thread because I'm running. */ |
| 1176 | sock = THREAD_FD (thread); |
| 1177 | client = THREAD_ARG (thread); |
| 1178 | client->t_read = NULL; |
| 1179 | |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1180 | if (client->t_suicide) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1181 | { |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1182 | zebra_client_close(client); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1183 | return -1; |
| 1184 | } |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1185 | |
| 1186 | /* Read length and command (if we don't have it already). */ |
ajs | 57a1477 | 2005-04-10 15:01:56 +0000 | [diff] [blame] | 1187 | if ((already = stream_get_endp(client->ibuf)) < ZEBRA_HEADER_SIZE) |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1188 | { |
ajs | 57a1477 | 2005-04-10 15:01:56 +0000 | [diff] [blame] | 1189 | ssize_t nbyte; |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1190 | if (((nbyte = stream_read_try (client->ibuf, sock, |
ajs | 57a1477 | 2005-04-10 15:01:56 +0000 | [diff] [blame] | 1191 | ZEBRA_HEADER_SIZE-already)) == 0) || |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1192 | (nbyte == -1)) |
| 1193 | { |
| 1194 | if (IS_ZEBRA_DEBUG_EVENT) |
| 1195 | zlog_debug ("connection closed socket [%d]", sock); |
| 1196 | zebra_client_close (client); |
| 1197 | return -1; |
| 1198 | } |
ajs | 57a1477 | 2005-04-10 15:01:56 +0000 | [diff] [blame] | 1199 | if (nbyte != (ssize_t)(ZEBRA_HEADER_SIZE-already)) |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1200 | { |
| 1201 | /* Try again later. */ |
| 1202 | zebra_event (ZEBRA_READ, sock, client); |
| 1203 | return 0; |
| 1204 | } |
ajs | 57a1477 | 2005-04-10 15:01:56 +0000 | [diff] [blame] | 1205 | already = ZEBRA_HEADER_SIZE; |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1206 | } |
| 1207 | |
| 1208 | /* Reset to read from the beginning of the incoming packet. */ |
| 1209 | stream_set_getp(client->ibuf, 0); |
| 1210 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1211 | length = stream_getw (client->ibuf); |
| 1212 | command = stream_getc (client->ibuf); |
| 1213 | |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1214 | if (length < ZEBRA_HEADER_SIZE) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1215 | { |
ajs | 57a1477 | 2005-04-10 15:01:56 +0000 | [diff] [blame] | 1216 | zlog_warn("%s: socket %d message length %u is less than header size %d", |
| 1217 | __func__, sock, length, ZEBRA_HEADER_SIZE); |
| 1218 | zebra_client_close (client); |
| 1219 | return -1; |
| 1220 | } |
| 1221 | if (length > STREAM_SIZE(client->ibuf)) |
| 1222 | { |
| 1223 | zlog_warn("%s: socket %d message length %u exceeds buffer size %lu", |
| 1224 | __func__, sock, length, (u_long)STREAM_SIZE(client->ibuf)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1225 | zebra_client_close (client); |
| 1226 | return -1; |
| 1227 | } |
| 1228 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1229 | /* Read rest of data. */ |
ajs | 57a1477 | 2005-04-10 15:01:56 +0000 | [diff] [blame] | 1230 | if (already < length) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1231 | { |
ajs | 57a1477 | 2005-04-10 15:01:56 +0000 | [diff] [blame] | 1232 | ssize_t nbyte; |
| 1233 | if (((nbyte = stream_read_try (client->ibuf, sock, |
| 1234 | length-already)) == 0) || |
| 1235 | (nbyte == -1)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1236 | { |
| 1237 | if (IS_ZEBRA_DEBUG_EVENT) |
ajs | b617800 | 2004-12-07 21:12:56 +0000 | [diff] [blame] | 1238 | zlog_debug ("connection closed [%d] when reading zebra data", sock); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1239 | zebra_client_close (client); |
| 1240 | return -1; |
| 1241 | } |
ajs | 57a1477 | 2005-04-10 15:01:56 +0000 | [diff] [blame] | 1242 | if (nbyte != (ssize_t)(length-already)) |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1243 | { |
| 1244 | /* Try again later. */ |
| 1245 | zebra_event (ZEBRA_READ, sock, client); |
| 1246 | return 0; |
| 1247 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1248 | } |
| 1249 | |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1250 | length -= ZEBRA_HEADER_SIZE; |
| 1251 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1252 | /* Debug packet information. */ |
| 1253 | if (IS_ZEBRA_DEBUG_EVENT) |
ajs | b617800 | 2004-12-07 21:12:56 +0000 | [diff] [blame] | 1254 | zlog_debug ("zebra message comes from socket [%d]", sock); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1255 | |
| 1256 | if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV) |
ajs | b617800 | 2004-12-07 21:12:56 +0000 | [diff] [blame] | 1257 | zlog_debug ("zebra message received [%s] %d", |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1258 | zebra_command_str[command], length); |
| 1259 | |
| 1260 | switch (command) |
| 1261 | { |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 1262 | case ZEBRA_ROUTER_ID_ADD: |
| 1263 | zread_router_id_add (client, length); |
| 1264 | break; |
| 1265 | case ZEBRA_ROUTER_ID_DELETE: |
| 1266 | zread_router_id_delete (client, length); |
| 1267 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1268 | case ZEBRA_INTERFACE_ADD: |
| 1269 | zread_interface_add (client, length); |
| 1270 | break; |
| 1271 | case ZEBRA_INTERFACE_DELETE: |
| 1272 | zread_interface_delete (client, length); |
| 1273 | break; |
| 1274 | case ZEBRA_IPV4_ROUTE_ADD: |
| 1275 | zread_ipv4_add (client, length); |
| 1276 | break; |
| 1277 | case ZEBRA_IPV4_ROUTE_DELETE: |
| 1278 | zread_ipv4_delete (client, length); |
| 1279 | break; |
| 1280 | #ifdef HAVE_IPV6 |
| 1281 | case ZEBRA_IPV6_ROUTE_ADD: |
| 1282 | zread_ipv6_add (client, length); |
| 1283 | break; |
| 1284 | case ZEBRA_IPV6_ROUTE_DELETE: |
| 1285 | zread_ipv6_delete (client, length); |
| 1286 | break; |
| 1287 | #endif /* HAVE_IPV6 */ |
| 1288 | case ZEBRA_REDISTRIBUTE_ADD: |
| 1289 | zebra_redistribute_add (command, client, length); |
| 1290 | break; |
| 1291 | case ZEBRA_REDISTRIBUTE_DELETE: |
| 1292 | zebra_redistribute_delete (command, client, length); |
| 1293 | break; |
| 1294 | case ZEBRA_REDISTRIBUTE_DEFAULT_ADD: |
| 1295 | zebra_redistribute_default_add (command, client, length); |
| 1296 | break; |
| 1297 | case ZEBRA_REDISTRIBUTE_DEFAULT_DELETE: |
| 1298 | zebra_redistribute_default_delete (command, client, length); |
| 1299 | break; |
| 1300 | case ZEBRA_IPV4_NEXTHOP_LOOKUP: |
| 1301 | zread_ipv4_nexthop_lookup (client, length); |
| 1302 | break; |
| 1303 | #ifdef HAVE_IPV6 |
| 1304 | case ZEBRA_IPV6_NEXTHOP_LOOKUP: |
| 1305 | zread_ipv6_nexthop_lookup (client, length); |
| 1306 | break; |
| 1307 | #endif /* HAVE_IPV6 */ |
| 1308 | case ZEBRA_IPV4_IMPORT_LOOKUP: |
| 1309 | zread_ipv4_import_lookup (client, length); |
| 1310 | break; |
| 1311 | default: |
| 1312 | zlog_info ("Zebra received unknown command %d", command); |
| 1313 | break; |
| 1314 | } |
| 1315 | |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1316 | if (client->t_suicide) |
| 1317 | { |
| 1318 | /* No need to wait for thread callback, just kill immediately. */ |
| 1319 | zebra_client_close(client); |
| 1320 | return -1; |
| 1321 | } |
| 1322 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1323 | stream_reset (client->ibuf); |
| 1324 | zebra_event (ZEBRA_READ, sock, client); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1325 | return 0; |
| 1326 | } |
| 1327 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1328 | |
| 1329 | /* Accept code of zebra server socket. */ |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 1330 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1331 | zebra_accept (struct thread *thread) |
| 1332 | { |
| 1333 | int accept_sock; |
| 1334 | int client_sock; |
| 1335 | struct sockaddr_in client; |
| 1336 | socklen_t len; |
| 1337 | |
| 1338 | accept_sock = THREAD_FD (thread); |
| 1339 | |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1340 | /* Reregister myself. */ |
| 1341 | zebra_event (ZEBRA_SERV, accept_sock, NULL); |
| 1342 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1343 | len = sizeof (struct sockaddr_in); |
| 1344 | client_sock = accept (accept_sock, (struct sockaddr *) &client, &len); |
| 1345 | |
| 1346 | if (client_sock < 0) |
| 1347 | { |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 1348 | zlog_warn ("Can't accept zebra socket: %s", safe_strerror (errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1349 | return -1; |
| 1350 | } |
| 1351 | |
paul | ccf3557 | 2003-03-01 11:42:20 +0000 | [diff] [blame] | 1352 | /* Make client socket non-blocking. */ |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1353 | set_nonblocking(client_sock); |
paul | 865b852 | 2005-01-05 08:30:35 +0000 | [diff] [blame] | 1354 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1355 | /* Create new zebra client. */ |
| 1356 | zebra_client_create (client_sock); |
| 1357 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1358 | return 0; |
| 1359 | } |
| 1360 | |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 1361 | #ifdef HAVE_TCP_ZEBRA |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1362 | /* Make zebra's server socket. */ |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 1363 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1364 | zebra_serv () |
| 1365 | { |
| 1366 | int ret; |
| 1367 | int accept_sock; |
| 1368 | struct sockaddr_in addr; |
| 1369 | |
| 1370 | accept_sock = socket (AF_INET, SOCK_STREAM, 0); |
| 1371 | |
| 1372 | if (accept_sock < 0) |
| 1373 | { |
paul | 3d1dc85 | 2005-04-05 00:45:23 +0000 | [diff] [blame] | 1374 | zlog_warn ("Can't create zserv stream socket: %s", |
| 1375 | safe_strerror (errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1376 | zlog_warn ("zebra can't provice full functionality due to above error"); |
| 1377 | return; |
| 1378 | } |
| 1379 | |
| 1380 | memset (&addr, 0, sizeof (struct sockaddr_in)); |
| 1381 | addr.sin_family = AF_INET; |
| 1382 | addr.sin_port = htons (ZEBRA_PORT); |
| 1383 | #ifdef HAVE_SIN_LEN |
| 1384 | addr.sin_len = sizeof (struct sockaddr_in); |
| 1385 | #endif /* HAVE_SIN_LEN */ |
| 1386 | addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK); |
| 1387 | |
| 1388 | sockopt_reuseaddr (accept_sock); |
| 1389 | sockopt_reuseport (accept_sock); |
| 1390 | |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 1391 | if ( zserv_privs.change(ZPRIVS_RAISE) ) |
| 1392 | zlog (NULL, LOG_ERR, "Can't raise privileges"); |
| 1393 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1394 | ret = bind (accept_sock, (struct sockaddr *)&addr, |
| 1395 | sizeof (struct sockaddr_in)); |
| 1396 | if (ret < 0) |
| 1397 | { |
paul | 3d1dc85 | 2005-04-05 00:45:23 +0000 | [diff] [blame] | 1398 | zlog_warn ("Can't bind to stream socket: %s", |
| 1399 | safe_strerror (errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1400 | zlog_warn ("zebra can't provice full functionality due to above error"); |
| 1401 | close (accept_sock); /* Avoid sd leak. */ |
| 1402 | return; |
| 1403 | } |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 1404 | |
| 1405 | if ( zserv_privs.change(ZPRIVS_LOWER) ) |
| 1406 | zlog (NULL, LOG_ERR, "Can't lower privileges"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1407 | |
| 1408 | ret = listen (accept_sock, 1); |
| 1409 | if (ret < 0) |
| 1410 | { |
paul | 3d1dc85 | 2005-04-05 00:45:23 +0000 | [diff] [blame] | 1411 | zlog_warn ("Can't listen to stream socket: %s", |
| 1412 | safe_strerror (errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1413 | zlog_warn ("zebra can't provice full functionality due to above error"); |
| 1414 | close (accept_sock); /* Avoid sd leak. */ |
| 1415 | return; |
| 1416 | } |
| 1417 | |
| 1418 | zebra_event (ZEBRA_SERV, accept_sock, NULL); |
| 1419 | } |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 1420 | #endif /* HAVE_TCP_ZEBRA */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1421 | |
| 1422 | /* For sockaddr_un. */ |
| 1423 | #include <sys/un.h> |
| 1424 | |
| 1425 | /* zebra server UNIX domain socket. */ |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 1426 | static void |
hasso | fce954f | 2004-10-07 20:29:24 +0000 | [diff] [blame] | 1427 | zebra_serv_un (const char *path) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1428 | { |
| 1429 | int ret; |
| 1430 | int sock, len; |
| 1431 | struct sockaddr_un serv; |
| 1432 | mode_t old_mask; |
| 1433 | |
| 1434 | /* First of all, unlink existing socket */ |
| 1435 | unlink (path); |
| 1436 | |
| 1437 | /* Set umask */ |
| 1438 | old_mask = umask (0077); |
| 1439 | |
| 1440 | /* Make UNIX domain socket. */ |
| 1441 | sock = socket (AF_UNIX, SOCK_STREAM, 0); |
| 1442 | if (sock < 0) |
| 1443 | { |
paul | 3d1dc85 | 2005-04-05 00:45:23 +0000 | [diff] [blame] | 1444 | zlog_warn ("Can't create zserv unix socket: %s", |
| 1445 | safe_strerror (errno)); |
| 1446 | zlog_warn ("zebra can't provide full functionality due to above error"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1447 | return; |
| 1448 | } |
| 1449 | |
| 1450 | /* Make server socket. */ |
| 1451 | memset (&serv, 0, sizeof (struct sockaddr_un)); |
| 1452 | serv.sun_family = AF_UNIX; |
| 1453 | strncpy (serv.sun_path, path, strlen (path)); |
| 1454 | #ifdef HAVE_SUN_LEN |
| 1455 | len = serv.sun_len = SUN_LEN(&serv); |
| 1456 | #else |
| 1457 | len = sizeof (serv.sun_family) + strlen (serv.sun_path); |
| 1458 | #endif /* HAVE_SUN_LEN */ |
| 1459 | |
| 1460 | ret = bind (sock, (struct sockaddr *) &serv, len); |
| 1461 | if (ret < 0) |
| 1462 | { |
paul | 3d1dc85 | 2005-04-05 00:45:23 +0000 | [diff] [blame] | 1463 | zlog_warn ("Can't bind to unix socket %s: %s", |
| 1464 | path, safe_strerror (errno)); |
| 1465 | zlog_warn ("zebra can't provide full functionality due to above error"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1466 | close (sock); |
| 1467 | return; |
| 1468 | } |
| 1469 | |
| 1470 | ret = listen (sock, 5); |
| 1471 | if (ret < 0) |
| 1472 | { |
paul | 3d1dc85 | 2005-04-05 00:45:23 +0000 | [diff] [blame] | 1473 | zlog_warn ("Can't listen to unix socket %s: %s", |
| 1474 | path, safe_strerror (errno)); |
| 1475 | zlog_warn ("zebra can't provide full functionality due to above error"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1476 | close (sock); |
| 1477 | return; |
| 1478 | } |
| 1479 | |
| 1480 | umask (old_mask); |
| 1481 | |
| 1482 | zebra_event (ZEBRA_SERV, sock, NULL); |
| 1483 | } |
| 1484 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1485 | |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 1486 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1487 | zebra_event (enum event event, int sock, struct zserv *client) |
| 1488 | { |
| 1489 | switch (event) |
| 1490 | { |
| 1491 | case ZEBRA_SERV: |
paul | b21b19c | 2003-06-15 01:28:29 +0000 | [diff] [blame] | 1492 | thread_add_read (zebrad.master, zebra_accept, client, sock); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1493 | break; |
| 1494 | case ZEBRA_READ: |
| 1495 | client->t_read = |
paul | b21b19c | 2003-06-15 01:28:29 +0000 | [diff] [blame] | 1496 | thread_add_read (zebrad.master, zebra_client_read, client, sock); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1497 | break; |
| 1498 | case ZEBRA_WRITE: |
| 1499 | /**/ |
| 1500 | break; |
| 1501 | } |
| 1502 | } |
| 1503 | |
| 1504 | /* Display default rtm_table for all clients. */ |
| 1505 | DEFUN (show_table, |
| 1506 | show_table_cmd, |
| 1507 | "show table", |
| 1508 | SHOW_STR |
| 1509 | "default routing table to use for all clients\n") |
| 1510 | { |
paul | b21b19c | 2003-06-15 01:28:29 +0000 | [diff] [blame] | 1511 | vty_out (vty, "table %d%s", zebrad.rtm_table_default, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1512 | VTY_NEWLINE); |
| 1513 | return CMD_SUCCESS; |
| 1514 | } |
| 1515 | |
| 1516 | DEFUN (config_table, |
| 1517 | config_table_cmd, |
| 1518 | "table TABLENO", |
| 1519 | "Configure target kernel routing table\n" |
| 1520 | "TABLE integer\n") |
| 1521 | { |
paul | b21b19c | 2003-06-15 01:28:29 +0000 | [diff] [blame] | 1522 | zebrad.rtm_table_default = strtol (argv[0], (char**)0, 10); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1523 | return CMD_SUCCESS; |
| 1524 | } |
| 1525 | |
hasso | 647e4f1 | 2003-05-25 11:43:52 +0000 | [diff] [blame] | 1526 | DEFUN (ip_forwarding, |
| 1527 | ip_forwarding_cmd, |
| 1528 | "ip forwarding", |
| 1529 | IP_STR |
| 1530 | "Turn on IP forwarding") |
| 1531 | { |
| 1532 | int ret; |
| 1533 | |
| 1534 | ret = ipforward (); |
hasso | b71f00f | 2004-10-13 12:20:35 +0000 | [diff] [blame] | 1535 | if (ret == 0) |
| 1536 | ret = ipforward_on (); |
hasso | 647e4f1 | 2003-05-25 11:43:52 +0000 | [diff] [blame] | 1537 | |
hasso | 647e4f1 | 2003-05-25 11:43:52 +0000 | [diff] [blame] | 1538 | if (ret == 0) |
| 1539 | { |
| 1540 | vty_out (vty, "Can't turn on IP forwarding%s", VTY_NEWLINE); |
| 1541 | return CMD_WARNING; |
| 1542 | } |
| 1543 | |
| 1544 | return CMD_SUCCESS; |
| 1545 | } |
| 1546 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1547 | DEFUN (no_ip_forwarding, |
| 1548 | no_ip_forwarding_cmd, |
| 1549 | "no ip forwarding", |
| 1550 | NO_STR |
| 1551 | IP_STR |
| 1552 | "Turn off IP forwarding") |
| 1553 | { |
| 1554 | int ret; |
| 1555 | |
| 1556 | ret = ipforward (); |
hasso | b71f00f | 2004-10-13 12:20:35 +0000 | [diff] [blame] | 1557 | if (ret != 0) |
| 1558 | ret = ipforward_off (); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1559 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1560 | if (ret != 0) |
| 1561 | { |
| 1562 | vty_out (vty, "Can't turn off IP forwarding%s", VTY_NEWLINE); |
| 1563 | return CMD_WARNING; |
| 1564 | } |
| 1565 | |
| 1566 | return CMD_SUCCESS; |
| 1567 | } |
| 1568 | |
| 1569 | /* This command is for debugging purpose. */ |
| 1570 | DEFUN (show_zebra_client, |
| 1571 | show_zebra_client_cmd, |
| 1572 | "show zebra client", |
| 1573 | SHOW_STR |
| 1574 | "Zebra information" |
| 1575 | "Client information") |
| 1576 | { |
hasso | 52dc7ee | 2004-09-23 19:18:23 +0000 | [diff] [blame] | 1577 | struct listnode *node; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1578 | struct zserv *client; |
| 1579 | |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 1580 | for (ALL_LIST_ELEMENTS_RO (zebrad.client_list, node, client)) |
| 1581 | vty_out (vty, "Client fd %d%s", client->sock, VTY_NEWLINE); |
| 1582 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1583 | return CMD_SUCCESS; |
| 1584 | } |
| 1585 | |
| 1586 | /* Table configuration write function. */ |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 1587 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1588 | config_write_table (struct vty *vty) |
| 1589 | { |
paul | b21b19c | 2003-06-15 01:28:29 +0000 | [diff] [blame] | 1590 | if (zebrad.rtm_table_default) |
| 1591 | vty_out (vty, "table %d%s", zebrad.rtm_table_default, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1592 | VTY_NEWLINE); |
| 1593 | return 0; |
| 1594 | } |
| 1595 | |
| 1596 | /* table node for routing tables. */ |
| 1597 | struct cmd_node table_node = |
| 1598 | { |
| 1599 | TABLE_NODE, |
| 1600 | "", /* This node has no interface. */ |
| 1601 | 1 |
| 1602 | }; |
| 1603 | |
| 1604 | /* Only display ip forwarding is enabled or not. */ |
| 1605 | DEFUN (show_ip_forwarding, |
| 1606 | show_ip_forwarding_cmd, |
| 1607 | "show ip forwarding", |
| 1608 | SHOW_STR |
| 1609 | IP_STR |
| 1610 | "IP forwarding status\n") |
| 1611 | { |
| 1612 | int ret; |
| 1613 | |
| 1614 | ret = ipforward (); |
| 1615 | |
| 1616 | if (ret == 0) |
| 1617 | vty_out (vty, "IP forwarding is off%s", VTY_NEWLINE); |
| 1618 | else |
| 1619 | vty_out (vty, "IP forwarding is on%s", VTY_NEWLINE); |
| 1620 | return CMD_SUCCESS; |
| 1621 | } |
| 1622 | |
| 1623 | #ifdef HAVE_IPV6 |
| 1624 | /* Only display ipv6 forwarding is enabled or not. */ |
| 1625 | DEFUN (show_ipv6_forwarding, |
| 1626 | show_ipv6_forwarding_cmd, |
| 1627 | "show ipv6 forwarding", |
| 1628 | SHOW_STR |
| 1629 | "IPv6 information\n" |
| 1630 | "Forwarding status\n") |
| 1631 | { |
| 1632 | int ret; |
| 1633 | |
| 1634 | ret = ipforward_ipv6 (); |
| 1635 | |
| 1636 | switch (ret) |
| 1637 | { |
| 1638 | case -1: |
| 1639 | vty_out (vty, "ipv6 forwarding is unknown%s", VTY_NEWLINE); |
| 1640 | break; |
| 1641 | case 0: |
| 1642 | vty_out (vty, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE); |
| 1643 | break; |
| 1644 | case 1: |
| 1645 | vty_out (vty, "ipv6 forwarding is %s%s", "on", VTY_NEWLINE); |
| 1646 | break; |
| 1647 | default: |
| 1648 | vty_out (vty, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE); |
| 1649 | break; |
| 1650 | } |
| 1651 | return CMD_SUCCESS; |
| 1652 | } |
| 1653 | |
hasso | 5590672 | 2004-02-11 22:42:16 +0000 | [diff] [blame] | 1654 | DEFUN (ipv6_forwarding, |
| 1655 | ipv6_forwarding_cmd, |
| 1656 | "ipv6 forwarding", |
| 1657 | IPV6_STR |
| 1658 | "Turn on IPv6 forwarding") |
| 1659 | { |
| 1660 | int ret; |
| 1661 | |
hasso | 41d3fc9 | 2004-04-06 11:59:00 +0000 | [diff] [blame] | 1662 | ret = ipforward_ipv6 (); |
hasso | b71f00f | 2004-10-13 12:20:35 +0000 | [diff] [blame] | 1663 | if (ret == 0) |
| 1664 | ret = ipforward_ipv6_on (); |
hasso | 41d3fc9 | 2004-04-06 11:59:00 +0000 | [diff] [blame] | 1665 | |
hasso | 41d3fc9 | 2004-04-06 11:59:00 +0000 | [diff] [blame] | 1666 | if (ret == 0) |
| 1667 | { |
hasso | 5590672 | 2004-02-11 22:42:16 +0000 | [diff] [blame] | 1668 | vty_out (vty, "Can't turn on IPv6 forwarding%s", VTY_NEWLINE); |
| 1669 | return CMD_WARNING; |
| 1670 | } |
| 1671 | |
| 1672 | return CMD_SUCCESS; |
| 1673 | } |
| 1674 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1675 | DEFUN (no_ipv6_forwarding, |
| 1676 | no_ipv6_forwarding_cmd, |
| 1677 | "no ipv6 forwarding", |
| 1678 | NO_STR |
hasso | 5590672 | 2004-02-11 22:42:16 +0000 | [diff] [blame] | 1679 | IPV6_STR |
| 1680 | "Turn off IPv6 forwarding") |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1681 | { |
| 1682 | int ret; |
| 1683 | |
hasso | 41d3fc9 | 2004-04-06 11:59:00 +0000 | [diff] [blame] | 1684 | ret = ipforward_ipv6 (); |
hasso | b71f00f | 2004-10-13 12:20:35 +0000 | [diff] [blame] | 1685 | if (ret != 0) |
| 1686 | ret = ipforward_ipv6_off (); |
hasso | 41d3fc9 | 2004-04-06 11:59:00 +0000 | [diff] [blame] | 1687 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1688 | if (ret != 0) |
| 1689 | { |
| 1690 | vty_out (vty, "Can't turn off IPv6 forwarding%s", VTY_NEWLINE); |
| 1691 | return CMD_WARNING; |
| 1692 | } |
| 1693 | |
| 1694 | return CMD_SUCCESS; |
| 1695 | } |
| 1696 | |
| 1697 | #endif /* HAVE_IPV6 */ |
| 1698 | |
| 1699 | /* IPForwarding configuration write function. */ |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1700 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1701 | config_write_forwarding (struct vty *vty) |
| 1702 | { |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 1703 | /* FIXME: Find better place for that. */ |
| 1704 | router_id_write (vty); |
| 1705 | |
paul | 3e0b3a5 | 2004-08-23 18:58:32 +0000 | [diff] [blame] | 1706 | if (ipforward ()) |
| 1707 | vty_out (vty, "ip forwarding%s", VTY_NEWLINE); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1708 | #ifdef HAVE_IPV6 |
paul | 3e0b3a5 | 2004-08-23 18:58:32 +0000 | [diff] [blame] | 1709 | if (ipforward_ipv6 ()) |
| 1710 | vty_out (vty, "ipv6 forwarding%s", VTY_NEWLINE); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1711 | #endif /* HAVE_IPV6 */ |
| 1712 | vty_out (vty, "!%s", VTY_NEWLINE); |
| 1713 | return 0; |
| 1714 | } |
| 1715 | |
| 1716 | /* table node for routing tables. */ |
| 1717 | struct cmd_node forwarding_node = |
| 1718 | { |
| 1719 | FORWARDING_NODE, |
| 1720 | "", /* This node has no interface. */ |
| 1721 | 1 |
| 1722 | }; |
| 1723 | |
| 1724 | |
| 1725 | /* Initialisation of zebra and installation of commands. */ |
| 1726 | void |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 1727 | zebra_init (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1728 | { |
| 1729 | /* Client list init. */ |
paul | b21b19c | 2003-06-15 01:28:29 +0000 | [diff] [blame] | 1730 | zebrad.client_list = list_new (); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1731 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1732 | /* Make zebra server socket. */ |
| 1733 | #ifdef HAVE_TCP_ZEBRA |
| 1734 | zebra_serv (); |
| 1735 | #else |
| 1736 | zebra_serv_un (ZEBRA_SERV_PATH); |
| 1737 | #endif /* HAVE_TCP_ZEBRA */ |
| 1738 | |
| 1739 | /* Install configuration write function. */ |
| 1740 | install_node (&table_node, config_write_table); |
| 1741 | install_node (&forwarding_node, config_write_forwarding); |
| 1742 | |
| 1743 | install_element (VIEW_NODE, &show_ip_forwarding_cmd); |
| 1744 | install_element (ENABLE_NODE, &show_ip_forwarding_cmd); |
hasso | 647e4f1 | 2003-05-25 11:43:52 +0000 | [diff] [blame] | 1745 | install_element (CONFIG_NODE, &ip_forwarding_cmd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1746 | install_element (CONFIG_NODE, &no_ip_forwarding_cmd); |
| 1747 | install_element (ENABLE_NODE, &show_zebra_client_cmd); |
| 1748 | |
| 1749 | #ifdef HAVE_NETLINK |
| 1750 | install_element (VIEW_NODE, &show_table_cmd); |
| 1751 | install_element (ENABLE_NODE, &show_table_cmd); |
| 1752 | install_element (CONFIG_NODE, &config_table_cmd); |
| 1753 | #endif /* HAVE_NETLINK */ |
| 1754 | |
| 1755 | #ifdef HAVE_IPV6 |
| 1756 | install_element (VIEW_NODE, &show_ipv6_forwarding_cmd); |
| 1757 | install_element (ENABLE_NODE, &show_ipv6_forwarding_cmd); |
hasso | 5590672 | 2004-02-11 22:42:16 +0000 | [diff] [blame] | 1758 | install_element (CONFIG_NODE, &ipv6_forwarding_cmd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1759 | install_element (CONFIG_NODE, &no_ipv6_forwarding_cmd); |
| 1760 | #endif /* HAVE_IPV6 */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1761 | } |