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" |
Feng Lu | 0d0686f | 2015-05-22 11:40:02 +0200 | [diff] [blame] | 39 | #include "vrf.h" |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 40 | |
| 41 | #include "zebra/zserv.h" |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 42 | #include "zebra/router-id.h" |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 43 | #include "zebra/redistribute.h" |
| 44 | #include "zebra/debug.h" |
| 45 | #include "zebra/ipforward.h" |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 46 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 47 | /* Event list of zebra. */ |
| 48 | enum event { ZEBRA_SERV, ZEBRA_READ, ZEBRA_WRITE }; |
| 49 | |
paul | b21b19c | 2003-06-15 01:28:29 +0000 | [diff] [blame] | 50 | extern struct zebra_t zebrad; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 51 | |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 52 | static void zebra_event (enum event event, int sock, struct zserv *client); |
paul | ccf3557 | 2003-03-01 11:42:20 +0000 | [diff] [blame] | 53 | |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 54 | extern struct zebra_privs_t zserv_privs; |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 55 | |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 56 | static void zebra_client_close (struct zserv *client); |
| 57 | |
| 58 | static int |
| 59 | zserv_delayed_close(struct thread *thread) |
paul | ccf3557 | 2003-03-01 11:42:20 +0000 | [diff] [blame] | 60 | { |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 61 | struct zserv *client = THREAD_ARG(thread); |
paul | ccf3557 | 2003-03-01 11:42:20 +0000 | [diff] [blame] | 62 | |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 63 | client->t_suicide = NULL; |
| 64 | zebra_client_close(client); |
paul | ccf3557 | 2003-03-01 11:42:20 +0000 | [diff] [blame] | 65 | return 0; |
| 66 | } |
| 67 | |
Vyacheslav Trushkin | 2ea1ab1 | 2011-12-11 18:48:47 +0400 | [diff] [blame] | 68 | /* When client connects, it sends hello message |
| 69 | * with promise to send zebra routes of specific type. |
| 70 | * Zebra stores a socket fd of the client into |
| 71 | * this array. And use it to clean up routes that |
| 72 | * client didn't remove for some reasons after closing |
| 73 | * connection. |
| 74 | */ |
| 75 | static int route_type_oaths[ZEBRA_ROUTE_MAX]; |
| 76 | |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 77 | static int |
| 78 | zserv_flush_data(struct thread *thread) |
paul | ccf3557 | 2003-03-01 11:42:20 +0000 | [diff] [blame] | 79 | { |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 80 | struct zserv *client = THREAD_ARG(thread); |
paul | ccf3557 | 2003-03-01 11:42:20 +0000 | [diff] [blame] | 81 | |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 82 | client->t_write = NULL; |
| 83 | if (client->t_suicide) |
| 84 | { |
| 85 | zebra_client_close(client); |
| 86 | return -1; |
| 87 | } |
| 88 | switch (buffer_flush_available(client->wb, client->sock)) |
| 89 | { |
| 90 | case BUFFER_ERROR: |
| 91 | zlog_warn("%s: buffer_flush_available failed on zserv client fd %d, " |
| 92 | "closing", __func__, client->sock); |
| 93 | zebra_client_close(client); |
| 94 | break; |
| 95 | case BUFFER_PENDING: |
| 96 | client->t_write = thread_add_write(zebrad.master, zserv_flush_data, |
| 97 | client, client->sock); |
| 98 | break; |
| 99 | case BUFFER_EMPTY: |
| 100 | break; |
| 101 | } |
| 102 | return 0; |
paul | ccf3557 | 2003-03-01 11:42:20 +0000 | [diff] [blame] | 103 | } |
| 104 | |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 105 | static int |
| 106 | zebra_server_send_message(struct zserv *client) |
paul | ccf3557 | 2003-03-01 11:42:20 +0000 | [diff] [blame] | 107 | { |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 108 | if (client->t_suicide) |
| 109 | return -1; |
| 110 | switch (buffer_write(client->wb, client->sock, STREAM_DATA(client->obuf), |
| 111 | stream_get_endp(client->obuf))) |
paul | ccf3557 | 2003-03-01 11:42:20 +0000 | [diff] [blame] | 112 | { |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 113 | case BUFFER_ERROR: |
| 114 | zlog_warn("%s: buffer_write failed to zserv client fd %d, closing", |
| 115 | __func__, client->sock); |
| 116 | /* Schedule a delayed close since many of the functions that call this |
| 117 | one do not check the return code. They do not allow for the |
| 118 | possibility that an I/O error may have caused the client to be |
| 119 | deleted. */ |
| 120 | client->t_suicide = thread_add_event(zebrad.master, zserv_delayed_close, |
| 121 | client, 0); |
| 122 | return -1; |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 123 | case BUFFER_EMPTY: |
| 124 | THREAD_OFF(client->t_write); |
| 125 | break; |
| 126 | case BUFFER_PENDING: |
| 127 | THREAD_WRITE_ON(zebrad.master, client->t_write, |
| 128 | zserv_flush_data, client, client->sock); |
| 129 | break; |
paul | ccf3557 | 2003-03-01 11:42:20 +0000 | [diff] [blame] | 130 | } |
paul | ccf3557 | 2003-03-01 11:42:20 +0000 | [diff] [blame] | 131 | return 0; |
| 132 | } |
| 133 | |
paul | c1b9800 | 2006-01-16 01:54:02 +0000 | [diff] [blame] | 134 | static void |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 135 | zserv_create_header (struct stream *s, uint16_t cmd, vrf_id_t vrf_id) |
paul | c1b9800 | 2006-01-16 01:54:02 +0000 | [diff] [blame] | 136 | { |
| 137 | /* length placeholder, caller can update */ |
| 138 | stream_putw (s, ZEBRA_HEADER_SIZE); |
| 139 | stream_putc (s, ZEBRA_HEADER_MARKER); |
| 140 | stream_putc (s, ZSERV_VERSION); |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 141 | stream_putw (s, vrf_id); |
paul | c1b9800 | 2006-01-16 01:54:02 +0000 | [diff] [blame] | 142 | stream_putw (s, cmd); |
| 143 | } |
| 144 | |
Josh Bailey | 51d4ef8 | 2012-03-21 17:13:39 -0700 | [diff] [blame] | 145 | static void |
| 146 | zserv_encode_interface (struct stream *s, struct interface *ifp) |
| 147 | { |
| 148 | /* Interface information. */ |
| 149 | stream_put (s, ifp->name, INTERFACE_NAMSIZ); |
| 150 | stream_putl (s, ifp->ifindex); |
| 151 | stream_putc (s, ifp->status); |
| 152 | stream_putq (s, ifp->flags); |
| 153 | stream_putl (s, ifp->metric); |
| 154 | stream_putl (s, ifp->mtu); |
| 155 | stream_putl (s, ifp->mtu6); |
| 156 | stream_putl (s, ifp->bandwidth); |
Timo Teräs | 954c7d6 | 2016-01-15 17:36:33 +0200 | [diff] [blame] | 157 | stream_putl (s, ifp->ll_type); |
Josh Bailey | 51d4ef8 | 2012-03-21 17:13:39 -0700 | [diff] [blame] | 158 | stream_putl (s, ifp->hw_addr_len); |
| 159 | if (ifp->hw_addr_len) |
| 160 | stream_put (s, ifp->hw_addr, ifp->hw_addr_len); |
Josh Bailey | 51d4ef8 | 2012-03-21 17:13:39 -0700 | [diff] [blame] | 161 | |
Olivier Dugeon | 15773a8 | 2016-04-19 18:29:55 +0200 | [diff] [blame] | 162 | zlog_info("Try to set TE Link Param"); |
| 163 | /* Then, Traffic Engineering parameters if any */ |
| 164 | if (HAS_LINK_PARAMS(ifp) && IS_LINK_PARAMS_SET(ifp->link_params)) |
| 165 | { |
| 166 | stream_putc (s, 1); |
| 167 | zebra_interface_link_params_write (s, ifp); |
| 168 | } |
| 169 | else |
| 170 | stream_putc (s, 0); |
| 171 | |
Josh Bailey | 51d4ef8 | 2012-03-21 17:13:39 -0700 | [diff] [blame] | 172 | /* Write packet size. */ |
| 173 | stream_putw_at (s, 0, stream_get_endp (s)); |
| 174 | } |
| 175 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 176 | /* Interface is added. Send ZEBRA_INTERFACE_ADD to client. */ |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 177 | /* |
| 178 | * This function is called in the following situations: |
| 179 | * - in response to a 3-byte ZEBRA_INTERFACE_ADD request |
| 180 | * from the client. |
| 181 | * - at startup, when zebra figures out the available interfaces |
| 182 | * - when an interface is added (where support for |
| 183 | * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when |
| 184 | * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is |
| 185 | * received) |
| 186 | */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 187 | int |
| 188 | zsend_interface_add (struct zserv *client, struct interface *ifp) |
| 189 | { |
| 190 | struct stream *s; |
| 191 | |
| 192 | /* Check this client need interface information. */ |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 193 | if (! vrf_bitmap_check (client->ifinfo, ifp->vrf_id)) |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 194 | return 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 195 | |
| 196 | s = client->obuf; |
| 197 | stream_reset (s); |
| 198 | |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 199 | zserv_create_header (s, ZEBRA_INTERFACE_ADD, ifp->vrf_id); |
Josh Bailey | 51d4ef8 | 2012-03-21 17:13:39 -0700 | [diff] [blame] | 200 | zserv_encode_interface (s, ifp); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 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. */ |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 212 | if (! vrf_bitmap_check (client->ifinfo, ifp->vrf_id)) |
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); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 217 | |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 218 | zserv_create_header (s, ZEBRA_INTERFACE_DELETE, ifp->vrf_id); |
Josh Bailey | 51d4ef8 | 2012-03-21 17:13:39 -0700 | [diff] [blame] | 219 | zserv_encode_interface (s, ifp); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 220 | |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 221 | return zebra_server_send_message (client); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 222 | } |
| 223 | |
Olivier Dugeon | 15773a8 | 2016-04-19 18:29:55 +0200 | [diff] [blame] | 224 | int |
| 225 | zsend_interface_link_params (struct zserv *client, struct interface *ifp) |
| 226 | { |
| 227 | struct stream *s; |
| 228 | |
| 229 | /* Check this client need interface information. */ |
| 230 | if (! client->ifinfo) |
| 231 | return 0; |
| 232 | |
| 233 | if (!ifp->link_params) |
| 234 | return 0; |
| 235 | s = client->obuf; |
| 236 | stream_reset (s); |
| 237 | |
| 238 | zserv_create_header (s, ZEBRA_INTERFACE_LINK_PARAMS, ifp->vrf_id); |
| 239 | |
| 240 | /* Add Interface Index */ |
| 241 | stream_putl (s, ifp->ifindex); |
| 242 | |
| 243 | /* Then TE Link Parameters */ |
| 244 | if (zebra_interface_link_params_write (s, ifp) == 0) |
| 245 | return 0; |
| 246 | |
| 247 | /* Write packet size. */ |
| 248 | stream_putw_at (s, 0, stream_get_endp (s)); |
| 249 | |
| 250 | return zebra_server_send_message (client); |
| 251 | } |
| 252 | |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 253 | /* Interface address is added/deleted. Send ZEBRA_INTERFACE_ADDRESS_ADD or |
| 254 | * ZEBRA_INTERFACE_ADDRESS_DELETE to the client. |
| 255 | * |
| 256 | * A ZEBRA_INTERFACE_ADDRESS_ADD is sent in the following situations: |
| 257 | * - in response to a 3-byte ZEBRA_INTERFACE_ADD request |
| 258 | * from the client, after the ZEBRA_INTERFACE_ADD has been |
| 259 | * sent from zebra to the client |
| 260 | * - redistribute new address info to all clients in the following situations |
| 261 | * - at startup, when zebra figures out the available interfaces |
| 262 | * - when an interface is added (where support for |
| 263 | * RTM_IFANNOUNCE or AF_NETLINK sockets is available), or when |
| 264 | * an interface is marked IFF_UP (i.e., an RTM_IFINFO message is |
| 265 | * received) |
| 266 | * - for the vty commands "ip address A.B.C.D/M [<secondary>|<label LINE>]" |
| 267 | * and "no bandwidth <1-10000000>", "ipv6 address X:X::X:X/M" |
| 268 | * - when an RTM_NEWADDR message is received from the kernel, |
| 269 | * |
| 270 | * The call tree that triggers ZEBRA_INTERFACE_ADDRESS_DELETE: |
| 271 | * |
| 272 | * zsend_interface_address(DELETE) |
| 273 | * ^ |
| 274 | * | |
| 275 | * zebra_interface_address_delete_update |
| 276 | * ^ ^ ^ |
paul | 6eb8827 | 2005-07-29 14:36:00 +0000 | [diff] [blame] | 277 | * | | if_delete_update |
| 278 | * | | |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 279 | * ip_address_uninstall connected_delete_ipv4 |
| 280 | * [ipv6_addresss_uninstall] [connected_delete_ipv6] |
| 281 | * ^ ^ |
| 282 | * | | |
| 283 | * | RTM_NEWADDR on routing/netlink socket |
| 284 | * | |
| 285 | * vty commands: |
| 286 | * "no ip address A.B.C.D/M [label LINE]" |
| 287 | * "no ip address A.B.C.D/M secondary" |
| 288 | * ["no ipv6 address X:X::X:X/M"] |
| 289 | * |
| 290 | */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 291 | int |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 292 | zsend_interface_address (int cmd, struct zserv *client, |
| 293 | struct interface *ifp, struct connected *ifc) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 294 | { |
| 295 | int blen; |
| 296 | struct stream *s; |
| 297 | struct prefix *p; |
| 298 | |
| 299 | /* Check this client need interface information. */ |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 300 | if (! vrf_bitmap_check (client->ifinfo, ifp->vrf_id)) |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 301 | return 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 302 | |
| 303 | s = client->obuf; |
| 304 | stream_reset (s); |
paul | c1b9800 | 2006-01-16 01:54:02 +0000 | [diff] [blame] | 305 | |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 306 | zserv_create_header (s, cmd, ifp->vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 307 | stream_putl (s, ifp->ifindex); |
| 308 | |
| 309 | /* Interface address flag. */ |
| 310 | stream_putc (s, ifc->flags); |
| 311 | |
| 312 | /* Prefix information. */ |
| 313 | p = ifc->address; |
| 314 | stream_putc (s, p->family); |
| 315 | blen = prefix_blen (p); |
| 316 | stream_put (s, &p->u.prefix, blen); |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 317 | |
| 318 | /* |
| 319 | * XXX gnu version does not send prefixlen for ZEBRA_INTERFACE_ADDRESS_DELETE |
| 320 | * but zebra_interface_address_delete_read() in the gnu version |
| 321 | * expects to find it |
| 322 | */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 323 | stream_putc (s, p->prefixlen); |
| 324 | |
| 325 | /* Destination. */ |
| 326 | p = ifc->destination; |
| 327 | if (p) |
| 328 | stream_put (s, &p->u.prefix, blen); |
| 329 | else |
| 330 | stream_put (s, NULL, blen); |
| 331 | |
| 332 | /* Write packet size. */ |
| 333 | stream_putw_at (s, 0, stream_get_endp (s)); |
| 334 | |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 335 | return zebra_server_send_message(client); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 336 | } |
| 337 | |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 338 | /* |
| 339 | * The cmd passed to zsend_interface_update may be ZEBRA_INTERFACE_UP or |
| 340 | * ZEBRA_INTERFACE_DOWN. |
| 341 | * |
| 342 | * The ZEBRA_INTERFACE_UP message is sent from the zebra server to |
| 343 | * the clients in one of 2 situations: |
| 344 | * - an if_up is detected e.g., as a result of an RTM_IFINFO message |
| 345 | * - a vty command modifying the bandwidth of an interface is received. |
| 346 | * The ZEBRA_INTERFACE_DOWN message is sent when an if_down is detected. |
| 347 | */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 348 | int |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 349 | zsend_interface_update (int cmd, struct zserv *client, struct interface *ifp) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 350 | { |
| 351 | struct stream *s; |
| 352 | |
| 353 | /* Check this client need interface information. */ |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 354 | if (! vrf_bitmap_check (client->ifinfo, ifp->vrf_id)) |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 355 | return 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 356 | |
| 357 | s = client->obuf; |
| 358 | stream_reset (s); |
| 359 | |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 360 | zserv_create_header (s, cmd, ifp->vrf_id); |
Josh Bailey | 51d4ef8 | 2012-03-21 17:13:39 -0700 | [diff] [blame] | 361 | zserv_encode_interface (s, ifp); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 362 | |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 363 | return zebra_server_send_message(client); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 364 | } |
| 365 | |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 366 | /* |
| 367 | * The zebra server sends the clients a ZEBRA_IPV4_ROUTE_ADD or a |
| 368 | * ZEBRA_IPV6_ROUTE_ADD via zsend_route_multipath in the following |
| 369 | * situations: |
| 370 | * - when the client starts up, and requests default information |
| 371 | * by sending a ZEBRA_REDISTRIBUTE_DEFAULT_ADD to the zebra server, in the |
| 372 | * - case of rip, ripngd, ospfd and ospf6d, when the client sends a |
| 373 | * ZEBRA_REDISTRIBUTE_ADD as a result of the "redistribute" vty cmd, |
| 374 | * - when the zebra server redistributes routes after it updates its rib |
| 375 | * |
| 376 | * The zebra server sends clients a ZEBRA_IPV4_ROUTE_DELETE or a |
| 377 | * ZEBRA_IPV6_ROUTE_DELETE via zsend_route_multipath when: |
| 378 | * - a "ip route" or "ipv6 route" vty command is issued, a prefix is |
| 379 | * - deleted from zebra's rib, and this info |
| 380 | * has to be redistributed to the clients |
| 381 | * |
| 382 | * XXX The ZEBRA_IPV*_ROUTE_ADD message is also sent by the client to the |
| 383 | * zebra server when the client wants to tell the zebra server to add a |
| 384 | * route to the kernel (zapi_ipv4_add etc. ). Since it's essentially the |
| 385 | * same message being sent back and forth, this function and |
| 386 | * zapi_ipv{4,6}_{add, delete} should be re-written to avoid code |
| 387 | * duplication. |
| 388 | */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 389 | int |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 390 | zsend_route_multipath (int cmd, struct zserv *client, struct prefix *p, |
| 391 | struct rib *rib) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 392 | { |
| 393 | int psize; |
| 394 | struct stream *s; |
| 395 | struct nexthop *nexthop; |
paul | 1dcb517 | 2005-05-31 08:38:50 +0000 | [diff] [blame] | 396 | unsigned long nhnummark = 0, messmark = 0; |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 397 | int nhnum = 0; |
paul | 1dcb517 | 2005-05-31 08:38:50 +0000 | [diff] [blame] | 398 | u_char zapi_flags = 0; |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 399 | |
| 400 | /* Check this client need this route. */ |
| 401 | if (!vrf_bitmap_check (client->redist[rib->type], rib->vrf_id) && |
| 402 | !(is_default (p) && |
| 403 | vrf_bitmap_check (client->redist_default, rib->vrf_id))) |
| 404 | return 0; |
| 405 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 406 | s = client->obuf; |
| 407 | stream_reset (s); |
paul | c1b9800 | 2006-01-16 01:54:02 +0000 | [diff] [blame] | 408 | |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 409 | zserv_create_header (s, cmd, rib->vrf_id); |
paul | c1b9800 | 2006-01-16 01:54:02 +0000 | [diff] [blame] | 410 | |
| 411 | /* Put type and nexthop. */ |
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 | { |
Timo Teräs | 325823a | 2016-01-15 17:36:31 +0200 | [diff] [blame] | 436 | if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE)) |
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 */ |
Stephen Hemminger | cf8a831 | 2010-08-18 15:56:46 -0700 | [diff] [blame] | 487 | if (cmd == ZEBRA_IPV4_ROUTE_ADD || cmd == ZEBRA_IPV6_ROUTE_ADD) |
paul | 1dcb517 | 2005-05-31 08:38:50 +0000 | [diff] [blame] | 488 | { |
vincent | fbf5d03 | 2005-09-29 11:25:50 +0000 | [diff] [blame] | 489 | SET_FLAG (zapi_flags, ZAPI_MESSAGE_DISTANCE); |
| 490 | stream_putc (s, rib->distance); |
paul | 1dcb517 | 2005-05-31 08:38:50 +0000 | [diff] [blame] | 491 | SET_FLAG (zapi_flags, ZAPI_MESSAGE_METRIC); |
| 492 | stream_putl (s, rib->metric); |
Timo Teräs | b11f3b5 | 2015-11-02 16:50:07 +0200 | [diff] [blame] | 493 | SET_FLAG (zapi_flags, ZAPI_MESSAGE_MTU); |
| 494 | stream_putl (s, rib->mtu); |
paul | 1dcb517 | 2005-05-31 08:38:50 +0000 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | /* write real message flags value */ |
| 498 | stream_putc_at (s, messmark, zapi_flags); |
| 499 | |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 500 | /* Write next-hop number */ |
| 501 | if (nhnummark) |
hasso | c1eaa44 | 2004-10-19 06:26:01 +0000 | [diff] [blame] | 502 | stream_putc_at (s, nhnummark, nhnum); |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 503 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 504 | /* Write packet size. */ |
| 505 | stream_putw_at (s, 0, stream_get_endp (s)); |
| 506 | |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 507 | return zebra_server_send_message(client); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 508 | } |
| 509 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 510 | #ifdef HAVE_IPV6 |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 511 | static int |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 512 | zsend_ipv6_nexthop_lookup (struct zserv *client, struct in6_addr *addr, |
| 513 | vrf_id_t vrf_id) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 514 | { |
| 515 | struct stream *s; |
| 516 | struct rib *rib; |
| 517 | unsigned long nump; |
| 518 | u_char num; |
| 519 | struct nexthop *nexthop; |
| 520 | |
| 521 | /* Lookup nexthop. */ |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 522 | rib = rib_match_ipv6 (addr, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 523 | |
| 524 | /* Get output stream. */ |
| 525 | s = client->obuf; |
| 526 | stream_reset (s); |
| 527 | |
| 528 | /* Fill in result. */ |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 529 | zserv_create_header (s, ZEBRA_IPV6_NEXTHOP_LOOKUP, vrf_id); |
Hiroshi Yokoi | 8ccd74c | 2015-09-08 11:52:20 +0900 | [diff] [blame] | 530 | stream_put (s, addr, 16); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 531 | |
| 532 | if (rib) |
| 533 | { |
| 534 | stream_putl (s, rib->metric); |
| 535 | num = 0; |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 536 | nump = stream_get_endp(s); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 537 | stream_putc (s, 0); |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 538 | /* Only non-recursive routes are elegible to resolve nexthop we |
| 539 | * are looking up. Therefore, we will just iterate over the top |
| 540 | * chain of nexthops. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 541 | for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next) |
Timo Teräs | 325823a | 2016-01-15 17:36:31 +0200 | [diff] [blame] | 542 | if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 543 | { |
| 544 | stream_putc (s, nexthop->type); |
| 545 | switch (nexthop->type) |
| 546 | { |
| 547 | case ZEBRA_NEXTHOP_IPV6: |
| 548 | stream_put (s, &nexthop->gate.ipv6, 16); |
| 549 | break; |
| 550 | case ZEBRA_NEXTHOP_IPV6_IFINDEX: |
| 551 | case ZEBRA_NEXTHOP_IPV6_IFNAME: |
| 552 | stream_put (s, &nexthop->gate.ipv6, 16); |
| 553 | stream_putl (s, nexthop->ifindex); |
| 554 | break; |
| 555 | case ZEBRA_NEXTHOP_IFINDEX: |
| 556 | case ZEBRA_NEXTHOP_IFNAME: |
| 557 | stream_putl (s, nexthop->ifindex); |
| 558 | break; |
hasso | fa2b17e | 2004-03-04 17:45:00 +0000 | [diff] [blame] | 559 | default: |
| 560 | /* do nothing */ |
| 561 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 562 | } |
| 563 | num++; |
| 564 | } |
| 565 | stream_putc_at (s, nump, num); |
| 566 | } |
| 567 | else |
| 568 | { |
| 569 | stream_putl (s, 0); |
| 570 | stream_putc (s, 0); |
| 571 | } |
| 572 | |
| 573 | stream_putw_at (s, 0, stream_get_endp (s)); |
| 574 | |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 575 | return zebra_server_send_message(client); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 576 | } |
| 577 | #endif /* HAVE_IPV6 */ |
| 578 | |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 579 | static int |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 580 | zsend_ipv4_nexthop_lookup (struct zserv *client, struct in_addr addr, |
| 581 | vrf_id_t vrf_id) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 582 | { |
| 583 | struct stream *s; |
| 584 | struct rib *rib; |
| 585 | unsigned long nump; |
| 586 | u_char num; |
| 587 | struct nexthop *nexthop; |
| 588 | |
David Lamparter | f598cf7 | 2014-11-22 14:44:20 -0800 | [diff] [blame] | 589 | /* Lookup nexthop - eBGP excluded */ |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 590 | rib = rib_match_ipv4_safi (addr, SAFI_UNICAST, 1, NULL, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 591 | |
| 592 | /* Get output stream. */ |
| 593 | s = client->obuf; |
| 594 | stream_reset (s); |
| 595 | |
| 596 | /* Fill in result. */ |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 597 | zserv_create_header (s, ZEBRA_IPV4_NEXTHOP_LOOKUP, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 598 | stream_put_in_addr (s, &addr); |
| 599 | |
| 600 | if (rib) |
| 601 | { |
Christian Franke | bb97e46 | 2013-05-25 14:01:35 +0000 | [diff] [blame] | 602 | if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV) |
| 603 | zlog_debug("%s: Matching rib entry found.", __func__); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 604 | stream_putl (s, rib->metric); |
| 605 | num = 0; |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 606 | nump = stream_get_endp(s); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 607 | stream_putc (s, 0); |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 608 | /* Only non-recursive routes are elegible to resolve the nexthop we |
| 609 | * are looking up. Therefore, we will just iterate over the top |
| 610 | * chain of nexthops. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 611 | for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next) |
Timo Teräs | 325823a | 2016-01-15 17:36:31 +0200 | [diff] [blame] | 612 | if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 613 | { |
| 614 | stream_putc (s, nexthop->type); |
| 615 | switch (nexthop->type) |
| 616 | { |
| 617 | case ZEBRA_NEXTHOP_IPV4: |
| 618 | stream_put_in_addr (s, &nexthop->gate.ipv4); |
| 619 | break; |
Christian Franke | bb97e46 | 2013-05-25 14:01:35 +0000 | [diff] [blame] | 620 | case ZEBRA_NEXTHOP_IPV4_IFINDEX: |
| 621 | stream_put_in_addr (s, &nexthop->gate.ipv4); |
| 622 | stream_putl (s, nexthop->ifindex); |
| 623 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 624 | case ZEBRA_NEXTHOP_IFINDEX: |
| 625 | case ZEBRA_NEXTHOP_IFNAME: |
| 626 | stream_putl (s, nexthop->ifindex); |
| 627 | break; |
hasso | fa2b17e | 2004-03-04 17:45:00 +0000 | [diff] [blame] | 628 | default: |
| 629 | /* do nothing */ |
| 630 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 631 | } |
| 632 | num++; |
| 633 | } |
| 634 | stream_putc_at (s, nump, num); |
| 635 | } |
| 636 | else |
| 637 | { |
Christian Franke | bb97e46 | 2013-05-25 14:01:35 +0000 | [diff] [blame] | 638 | if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV) |
| 639 | zlog_debug("%s: No matching rib entry found.", __func__); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 640 | stream_putl (s, 0); |
| 641 | stream_putc (s, 0); |
| 642 | } |
| 643 | |
| 644 | stream_putw_at (s, 0, stream_get_endp (s)); |
| 645 | |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 646 | return zebra_server_send_message(client); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 647 | } |
| 648 | |
Everton Marques | 4e5275b | 2014-07-01 15:15:52 -0300 | [diff] [blame] | 649 | /* |
| 650 | Modified version of zsend_ipv4_nexthop_lookup(): |
| 651 | Query unicast rib if nexthop is not found on mrib. |
| 652 | Returns both route metric and protocol distance. |
| 653 | */ |
| 654 | static int |
David Lamparter | bd07812 | 2015-01-06 19:53:24 +0100 | [diff] [blame] | 655 | zsend_ipv4_nexthop_lookup_mrib (struct zserv *client, struct in_addr addr, |
| 656 | struct rib *rib) |
Everton Marques | 4e5275b | 2014-07-01 15:15:52 -0300 | [diff] [blame] | 657 | { |
| 658 | struct stream *s; |
Everton Marques | 4e5275b | 2014-07-01 15:15:52 -0300 | [diff] [blame] | 659 | unsigned long nump; |
| 660 | u_char num; |
| 661 | struct nexthop *nexthop; |
Everton Marques | 4e5275b | 2014-07-01 15:15:52 -0300 | [diff] [blame] | 662 | |
| 663 | /* Get output stream. */ |
| 664 | s = client->obuf; |
| 665 | stream_reset (s); |
| 666 | |
| 667 | /* Fill in result. */ |
Jafar Al-Gharaibeh | 190591f | 2016-04-21 17:40:12 -0500 | [diff] [blame] | 668 | zserv_create_header (s, ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB, |
| 669 | rib ? rib->vrf_id : VRF_DEFAULT); |
Everton Marques | 4e5275b | 2014-07-01 15:15:52 -0300 | [diff] [blame] | 670 | stream_put_in_addr (s, &addr); |
| 671 | |
| 672 | if (rib) |
| 673 | { |
| 674 | stream_putc (s, rib->distance); |
| 675 | stream_putl (s, rib->metric); |
| 676 | num = 0; |
| 677 | nump = stream_get_endp(s); /* remember position for nexthop_num */ |
| 678 | stream_putc (s, 0); /* reserve room for nexthop_num */ |
| 679 | /* Only non-recursive routes are elegible to resolve the nexthop we |
| 680 | * are looking up. Therefore, we will just iterate over the top |
| 681 | * chain of nexthops. */ |
| 682 | for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next) |
Timo Teräs | 325823a | 2016-01-15 17:36:31 +0200 | [diff] [blame] | 683 | if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE)) |
Everton Marques | 4e5275b | 2014-07-01 15:15:52 -0300 | [diff] [blame] | 684 | { |
| 685 | stream_putc (s, nexthop->type); |
| 686 | switch (nexthop->type) |
| 687 | { |
| 688 | case ZEBRA_NEXTHOP_IPV4: |
| 689 | stream_put_in_addr (s, &nexthop->gate.ipv4); |
| 690 | break; |
| 691 | case ZEBRA_NEXTHOP_IPV4_IFINDEX: |
| 692 | stream_put_in_addr (s, &nexthop->gate.ipv4); |
| 693 | stream_putl (s, nexthop->ifindex); |
| 694 | break; |
| 695 | case ZEBRA_NEXTHOP_IFINDEX: |
| 696 | case ZEBRA_NEXTHOP_IFNAME: |
| 697 | stream_putl (s, nexthop->ifindex); |
| 698 | break; |
| 699 | default: |
| 700 | /* do nothing */ |
| 701 | break; |
| 702 | } |
| 703 | num++; |
| 704 | } |
| 705 | |
| 706 | stream_putc_at (s, nump, num); /* store nexthop_num */ |
| 707 | } |
| 708 | else |
| 709 | { |
| 710 | stream_putc (s, 0); /* distance */ |
| 711 | stream_putl (s, 0); /* metric */ |
| 712 | stream_putc (s, 0); /* nexthop_num */ |
| 713 | } |
| 714 | |
| 715 | stream_putw_at (s, 0, stream_get_endp (s)); |
| 716 | |
| 717 | return zebra_server_send_message(client); |
| 718 | } |
| 719 | |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 720 | static int |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 721 | zsend_ipv4_import_lookup (struct zserv *client, struct prefix_ipv4 *p, |
| 722 | vrf_id_t vrf_id) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 723 | { |
| 724 | struct stream *s; |
| 725 | struct rib *rib; |
| 726 | unsigned long nump; |
| 727 | u_char num; |
| 728 | struct nexthop *nexthop; |
| 729 | |
| 730 | /* Lookup nexthop. */ |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 731 | rib = rib_lookup_ipv4 (p, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 732 | |
| 733 | /* Get output stream. */ |
| 734 | s = client->obuf; |
| 735 | stream_reset (s); |
| 736 | |
| 737 | /* Fill in result. */ |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 738 | zserv_create_header (s, ZEBRA_IPV4_IMPORT_LOOKUP, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 739 | stream_put_in_addr (s, &p->prefix); |
| 740 | |
| 741 | if (rib) |
| 742 | { |
| 743 | stream_putl (s, rib->metric); |
| 744 | num = 0; |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 745 | nump = stream_get_endp(s); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 746 | stream_putc (s, 0); |
| 747 | for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next) |
Timo Teräs | 325823a | 2016-01-15 17:36:31 +0200 | [diff] [blame] | 748 | if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 749 | { |
| 750 | stream_putc (s, nexthop->type); |
| 751 | switch (nexthop->type) |
| 752 | { |
| 753 | case ZEBRA_NEXTHOP_IPV4: |
| 754 | stream_put_in_addr (s, &nexthop->gate.ipv4); |
| 755 | break; |
Christian Franke | a12afd5 | 2013-05-25 14:01:36 +0000 | [diff] [blame] | 756 | case ZEBRA_NEXTHOP_IPV4_IFINDEX: |
| 757 | stream_put_in_addr (s, &nexthop->gate.ipv4); |
| 758 | stream_putl (s, nexthop->ifindex); |
| 759 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 760 | case ZEBRA_NEXTHOP_IFINDEX: |
| 761 | case ZEBRA_NEXTHOP_IFNAME: |
| 762 | stream_putl (s, nexthop->ifindex); |
| 763 | break; |
hasso | fa2b17e | 2004-03-04 17:45:00 +0000 | [diff] [blame] | 764 | default: |
| 765 | /* do nothing */ |
| 766 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 767 | } |
| 768 | num++; |
| 769 | } |
| 770 | stream_putc_at (s, nump, num); |
| 771 | } |
| 772 | else |
| 773 | { |
| 774 | stream_putl (s, 0); |
| 775 | stream_putc (s, 0); |
| 776 | } |
| 777 | |
| 778 | stream_putw_at (s, 0, stream_get_endp (s)); |
| 779 | |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 780 | return zebra_server_send_message(client); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 781 | } |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 782 | |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 783 | /* Router-id is updated. Send ZEBRA_ROUTER_ID_ADD to client. */ |
| 784 | int |
Feng Lu | ac19a44 | 2015-05-22 11:40:07 +0200 | [diff] [blame] | 785 | zsend_router_id_update (struct zserv *client, struct prefix *p, |
| 786 | vrf_id_t vrf_id) |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 787 | { |
| 788 | struct stream *s; |
| 789 | int blen; |
| 790 | |
| 791 | /* Check this client need interface information. */ |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 792 | if (! vrf_bitmap_check (client->ridinfo, vrf_id)) |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 793 | return 0; |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 794 | |
| 795 | s = client->obuf; |
| 796 | stream_reset (s); |
| 797 | |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 798 | /* Message type. */ |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 799 | zserv_create_header (s, ZEBRA_ROUTER_ID_UPDATE, vrf_id); |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 800 | |
| 801 | /* Prefix information. */ |
| 802 | stream_putc (s, p->family); |
| 803 | blen = prefix_blen (p); |
| 804 | stream_put (s, &p->u.prefix, blen); |
| 805 | stream_putc (s, p->prefixlen); |
| 806 | |
| 807 | /* Write packet size. */ |
| 808 | stream_putw_at (s, 0, stream_get_endp (s)); |
| 809 | |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 810 | return zebra_server_send_message(client); |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 811 | } |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 812 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 813 | /* Register zebra server interface information. Send current all |
| 814 | interface and address information. */ |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 815 | static int |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 816 | zread_interface_add (struct zserv *client, u_short length, vrf_id_t vrf_id) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 817 | { |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 818 | struct listnode *ifnode, *ifnnode; |
| 819 | struct listnode *cnode, *cnnode; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 820 | struct interface *ifp; |
| 821 | struct connected *c; |
| 822 | |
| 823 | /* Interface information is needed. */ |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 824 | vrf_bitmap_set (client->ifinfo, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 825 | |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 826 | for (ALL_LIST_ELEMENTS (vrf_iflist (vrf_id), ifnode, ifnnode, ifp)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 827 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 828 | /* Skip pseudo interface. */ |
| 829 | if (! CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE)) |
| 830 | continue; |
| 831 | |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 832 | if (zsend_interface_add (client, ifp) < 0) |
| 833 | return -1; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 834 | |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 835 | for (ALL_LIST_ELEMENTS (ifp->connected, cnode, cnnode, c)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 836 | { |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 837 | if (CHECK_FLAG (c->conf, ZEBRA_IFC_REAL) && |
| 838 | (zsend_interface_address (ZEBRA_INTERFACE_ADDRESS_ADD, client, |
| 839 | ifp, c) < 0)) |
| 840 | return -1; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 841 | } |
| 842 | } |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 843 | return 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 844 | } |
| 845 | |
| 846 | /* Unregister zebra server interface information. */ |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 847 | static int |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 848 | zread_interface_delete (struct zserv *client, u_short length, vrf_id_t vrf_id) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 849 | { |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 850 | vrf_bitmap_unset (client->ifinfo, vrf_id); |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 851 | return 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 852 | } |
| 853 | |
| 854 | /* This function support multiple nexthop. */ |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 855 | /* |
| 856 | * Parse the ZEBRA_IPV4_ROUTE_ADD sent from client. Update rib and |
| 857 | * add kernel route. |
| 858 | */ |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 859 | static int |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 860 | zread_ipv4_add (struct zserv *client, u_short length, vrf_id_t vrf_id) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 861 | { |
| 862 | int i; |
| 863 | struct rib *rib; |
| 864 | struct prefix_ipv4 p; |
| 865 | u_char message; |
| 866 | struct in_addr nexthop; |
| 867 | u_char nexthop_num; |
| 868 | u_char nexthop_type; |
| 869 | struct stream *s; |
Paul Jakma | 9099f9b | 2016-01-18 10:12:10 +0000 | [diff] [blame] | 870 | ifindex_t ifindex; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 871 | u_char ifname_len; |
G.Balaji | cddf391 | 2011-11-26 21:59:32 +0400 | [diff] [blame] | 872 | safi_t safi; |
| 873 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 874 | |
| 875 | /* Get input stream. */ |
| 876 | s = client->ibuf; |
| 877 | |
| 878 | /* Allocate new rib. */ |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 879 | rib = XCALLOC (MTYPE_RIB, sizeof (struct rib)); |
| 880 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 881 | /* Type, flags, message. */ |
| 882 | rib->type = stream_getc (s); |
| 883 | rib->flags = stream_getc (s); |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 884 | message = stream_getc (s); |
G.Balaji | cddf391 | 2011-11-26 21:59:32 +0400 | [diff] [blame] | 885 | safi = stream_getw (s); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 886 | rib->uptime = time (NULL); |
| 887 | |
| 888 | /* IPv4 prefix. */ |
| 889 | memset (&p, 0, sizeof (struct prefix_ipv4)); |
| 890 | p.family = AF_INET; |
| 891 | p.prefixlen = stream_getc (s); |
| 892 | stream_get (&p.prefix, s, PSIZE (p.prefixlen)); |
| 893 | |
Feng Lu | 0d0686f | 2015-05-22 11:40:02 +0200 | [diff] [blame] | 894 | /* VRF ID */ |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 895 | rib->vrf_id = vrf_id; |
Feng Lu | 0d0686f | 2015-05-22 11:40:02 +0200 | [diff] [blame] | 896 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 897 | /* Nexthop parse. */ |
| 898 | if (CHECK_FLAG (message, ZAPI_MESSAGE_NEXTHOP)) |
| 899 | { |
| 900 | nexthop_num = stream_getc (s); |
| 901 | |
| 902 | for (i = 0; i < nexthop_num; i++) |
| 903 | { |
| 904 | nexthop_type = stream_getc (s); |
| 905 | |
| 906 | switch (nexthop_type) |
| 907 | { |
| 908 | case ZEBRA_NEXTHOP_IFINDEX: |
| 909 | ifindex = stream_getl (s); |
| 910 | nexthop_ifindex_add (rib, ifindex); |
| 911 | break; |
| 912 | case ZEBRA_NEXTHOP_IFNAME: |
| 913 | ifname_len = stream_getc (s); |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 914 | stream_forward_getp (s, ifname_len); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 915 | break; |
| 916 | case ZEBRA_NEXTHOP_IPV4: |
| 917 | nexthop.s_addr = stream_get_ipv4 (s); |
Paul Jakma | 7514fb7 | 2007-05-02 16:05:35 +0000 | [diff] [blame] | 918 | nexthop_ipv4_add (rib, &nexthop, NULL); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 919 | break; |
Joakim Tjernlund | c963c20 | 2012-07-07 17:06:13 +0200 | [diff] [blame] | 920 | case ZEBRA_NEXTHOP_IPV4_IFINDEX: |
| 921 | nexthop.s_addr = stream_get_ipv4 (s); |
| 922 | ifindex = stream_getl (s); |
| 923 | nexthop_ipv4_ifindex_add (rib, &nexthop, NULL, ifindex); |
| 924 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 925 | case ZEBRA_NEXTHOP_IPV6: |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 926 | stream_forward_getp (s, IPV6_MAX_BYTELEN); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 927 | break; |
Subbaiah Venkata | 6902c69 | 2012-03-27 19:21:29 -0700 | [diff] [blame] | 928 | case ZEBRA_NEXTHOP_BLACKHOLE: |
| 929 | nexthop_blackhole_add (rib); |
| 930 | break; |
| 931 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 932 | } |
| 933 | } |
| 934 | |
| 935 | /* Distance. */ |
| 936 | if (CHECK_FLAG (message, ZAPI_MESSAGE_DISTANCE)) |
| 937 | rib->distance = stream_getc (s); |
| 938 | |
| 939 | /* Metric. */ |
| 940 | if (CHECK_FLAG (message, ZAPI_MESSAGE_METRIC)) |
| 941 | rib->metric = stream_getl (s); |
| 942 | |
Timo Teräs | b11f3b5 | 2015-11-02 16:50:07 +0200 | [diff] [blame] | 943 | if (CHECK_FLAG (message, ZAPI_MESSAGE_MTU)) |
| 944 | rib->mtu = stream_getl (s); |
| 945 | |
Paul Jakma | 171eee3 | 2006-07-27 16:11:02 +0000 | [diff] [blame] | 946 | /* Table */ |
| 947 | rib->table=zebrad.rtm_table_default; |
G.Balaji | cddf391 | 2011-11-26 21:59:32 +0400 | [diff] [blame] | 948 | rib_add_ipv4_multipath (&p, rib, safi); |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 949 | return 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 950 | } |
| 951 | |
| 952 | /* Zebra server IPv4 prefix delete function. */ |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 953 | static int |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 954 | zread_ipv4_delete (struct zserv *client, u_short length, vrf_id_t vrf_id) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 955 | { |
| 956 | int i; |
| 957 | struct stream *s; |
| 958 | struct zapi_ipv4 api; |
Subbaiah Venkata | 6902c69 | 2012-03-27 19:21:29 -0700 | [diff] [blame] | 959 | struct in_addr nexthop, *nexthop_p; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 960 | unsigned long ifindex; |
| 961 | struct prefix_ipv4 p; |
| 962 | u_char nexthop_num; |
| 963 | u_char nexthop_type; |
| 964 | u_char ifname_len; |
| 965 | |
| 966 | s = client->ibuf; |
| 967 | ifindex = 0; |
| 968 | nexthop.s_addr = 0; |
Subbaiah Venkata | 6902c69 | 2012-03-27 19:21:29 -0700 | [diff] [blame] | 969 | nexthop_p = NULL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 970 | |
| 971 | /* Type, flags, message. */ |
| 972 | api.type = stream_getc (s); |
| 973 | api.flags = stream_getc (s); |
| 974 | api.message = stream_getc (s); |
G.Balaji | cddf391 | 2011-11-26 21:59:32 +0400 | [diff] [blame] | 975 | api.safi = stream_getw (s); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 976 | |
| 977 | /* IPv4 prefix. */ |
| 978 | memset (&p, 0, sizeof (struct prefix_ipv4)); |
| 979 | p.family = AF_INET; |
| 980 | p.prefixlen = stream_getc (s); |
| 981 | stream_get (&p.prefix, s, PSIZE (p.prefixlen)); |
| 982 | |
| 983 | /* Nexthop, ifindex, distance, metric. */ |
| 984 | if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP)) |
| 985 | { |
| 986 | nexthop_num = stream_getc (s); |
| 987 | |
| 988 | for (i = 0; i < nexthop_num; i++) |
| 989 | { |
| 990 | nexthop_type = stream_getc (s); |
| 991 | |
| 992 | switch (nexthop_type) |
| 993 | { |
| 994 | case ZEBRA_NEXTHOP_IFINDEX: |
| 995 | ifindex = stream_getl (s); |
| 996 | break; |
| 997 | case ZEBRA_NEXTHOP_IFNAME: |
| 998 | ifname_len = stream_getc (s); |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 999 | stream_forward_getp (s, ifname_len); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1000 | break; |
| 1001 | case ZEBRA_NEXTHOP_IPV4: |
| 1002 | nexthop.s_addr = stream_get_ipv4 (s); |
Subbaiah Venkata | 6902c69 | 2012-03-27 19:21:29 -0700 | [diff] [blame] | 1003 | nexthop_p = &nexthop; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1004 | break; |
Joakim Tjernlund | c963c20 | 2012-07-07 17:06:13 +0200 | [diff] [blame] | 1005 | case ZEBRA_NEXTHOP_IPV4_IFINDEX: |
| 1006 | nexthop.s_addr = stream_get_ipv4 (s); |
Christian Franke | 23f5f7c | 2013-11-27 17:06:14 +0000 | [diff] [blame] | 1007 | nexthop_p = &nexthop; |
Joakim Tjernlund | c963c20 | 2012-07-07 17:06:13 +0200 | [diff] [blame] | 1008 | ifindex = stream_getl (s); |
| 1009 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1010 | case ZEBRA_NEXTHOP_IPV6: |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 1011 | stream_forward_getp (s, IPV6_MAX_BYTELEN); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1012 | break; |
| 1013 | } |
| 1014 | } |
| 1015 | } |
| 1016 | |
| 1017 | /* Distance. */ |
| 1018 | if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE)) |
| 1019 | api.distance = stream_getc (s); |
| 1020 | else |
| 1021 | api.distance = 0; |
| 1022 | |
| 1023 | /* Metric. */ |
| 1024 | if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC)) |
| 1025 | api.metric = stream_getl (s); |
| 1026 | else |
| 1027 | api.metric = 0; |
| 1028 | |
Subbaiah Venkata | 6902c69 | 2012-03-27 19:21:29 -0700 | [diff] [blame] | 1029 | rib_delete_ipv4 (api.type, api.flags, &p, nexthop_p, ifindex, |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1030 | vrf_id, api.safi); |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1031 | return 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1032 | } |
| 1033 | |
| 1034 | /* Nexthop lookup for IPv4. */ |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1035 | static int |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1036 | zread_ipv4_nexthop_lookup (struct zserv *client, u_short length, |
| 1037 | vrf_id_t vrf_id) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1038 | { |
| 1039 | struct in_addr addr; |
Christian Franke | bb97e46 | 2013-05-25 14:01:35 +0000 | [diff] [blame] | 1040 | char buf[BUFSIZ]; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1041 | |
| 1042 | addr.s_addr = stream_get_ipv4 (client->ibuf); |
Christian Franke | bb97e46 | 2013-05-25 14:01:35 +0000 | [diff] [blame] | 1043 | if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV) |
| 1044 | zlog_debug("%s: looking up %s", __func__, |
| 1045 | inet_ntop (AF_INET, &addr, buf, BUFSIZ)); |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1046 | return zsend_ipv4_nexthop_lookup (client, addr, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1047 | } |
| 1048 | |
Everton Marques | 4e5275b | 2014-07-01 15:15:52 -0300 | [diff] [blame] | 1049 | /* MRIB Nexthop lookup for IPv4. */ |
| 1050 | static int |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1051 | zread_ipv4_nexthop_lookup_mrib (struct zserv *client, u_short length, |
| 1052 | vrf_id_t vrf_id) |
Everton Marques | 4e5275b | 2014-07-01 15:15:52 -0300 | [diff] [blame] | 1053 | { |
| 1054 | struct in_addr addr; |
David Lamparter | bd07812 | 2015-01-06 19:53:24 +0100 | [diff] [blame] | 1055 | struct rib *rib; |
Everton Marques | 4e5275b | 2014-07-01 15:15:52 -0300 | [diff] [blame] | 1056 | |
| 1057 | addr.s_addr = stream_get_ipv4 (client->ibuf); |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1058 | rib = rib_match_ipv4_multicast (addr, NULL, vrf_id); |
David Lamparter | bd07812 | 2015-01-06 19:53:24 +0100 | [diff] [blame] | 1059 | return zsend_ipv4_nexthop_lookup_mrib (client, addr, rib); |
Everton Marques | 4e5275b | 2014-07-01 15:15:52 -0300 | [diff] [blame] | 1060 | } |
| 1061 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1062 | /* Nexthop lookup for IPv4. */ |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1063 | static int |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1064 | zread_ipv4_import_lookup (struct zserv *client, u_short length, |
| 1065 | vrf_id_t vrf_id) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1066 | { |
| 1067 | struct prefix_ipv4 p; |
| 1068 | |
| 1069 | p.family = AF_INET; |
| 1070 | p.prefixlen = stream_getc (client->ibuf); |
| 1071 | p.prefix.s_addr = stream_get_ipv4 (client->ibuf); |
| 1072 | |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1073 | return zsend_ipv4_import_lookup (client, &p, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1074 | } |
| 1075 | |
| 1076 | #ifdef HAVE_IPV6 |
| 1077 | /* Zebra server IPv6 prefix add function. */ |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1078 | static int |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1079 | zread_ipv6_add (struct zserv *client, u_short length, vrf_id_t vrf_id) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1080 | { |
| 1081 | int i; |
| 1082 | struct stream *s; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1083 | struct in6_addr nexthop; |
Ayan Banerjee | 34c5d89 | 2015-11-09 20:14:53 -0500 | [diff] [blame] | 1084 | struct rib *rib; |
| 1085 | u_char message; |
| 1086 | u_char gateway_num; |
| 1087 | u_char nexthop_type; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1088 | struct prefix_ipv6 p; |
Ayan Banerjee | 34c5d89 | 2015-11-09 20:14:53 -0500 | [diff] [blame] | 1089 | safi_t safi; |
| 1090 | static struct in6_addr nexthops[MULTIPATH_NUM]; |
| 1091 | static unsigned int ifindices[MULTIPATH_NUM]; |
| 1092 | |
| 1093 | /* Get input stream. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1094 | s = client->ibuf; |
Ayan Banerjee | 34c5d89 | 2015-11-09 20:14:53 -0500 | [diff] [blame] | 1095 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1096 | memset (&nexthop, 0, sizeof (struct in6_addr)); |
| 1097 | |
Ayan Banerjee | 34c5d89 | 2015-11-09 20:14:53 -0500 | [diff] [blame] | 1098 | /* Allocate new rib. */ |
| 1099 | rib = XCALLOC (MTYPE_RIB, sizeof (struct rib)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1100 | |
Ayan Banerjee | 34c5d89 | 2015-11-09 20:14:53 -0500 | [diff] [blame] | 1101 | /* Type, flags, message. */ |
| 1102 | rib->type = stream_getc (s); |
| 1103 | rib->flags = stream_getc (s); |
| 1104 | message = stream_getc (s); |
| 1105 | safi = stream_getw (s); |
| 1106 | rib->uptime = time (NULL); |
| 1107 | |
| 1108 | /* IPv6 prefix. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1109 | memset (&p, 0, sizeof (struct prefix_ipv6)); |
| 1110 | p.family = AF_INET6; |
| 1111 | p.prefixlen = stream_getc (s); |
| 1112 | stream_get (&p.prefix, s, PSIZE (p.prefixlen)); |
| 1113 | |
Ayan Banerjee | 34c5d89 | 2015-11-09 20:14:53 -0500 | [diff] [blame] | 1114 | /* We need to give nh-addr, nh-ifindex with the same next-hop object |
| 1115 | * to the rib to ensure that IPv6 multipathing works; need to coalesce |
| 1116 | * these. Clients should send the same number of paired set of |
| 1117 | * next-hop-addr/next-hop-ifindices. */ |
| 1118 | if (CHECK_FLAG (message, ZAPI_MESSAGE_NEXTHOP)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1119 | { |
Ayan Banerjee | 34c5d89 | 2015-11-09 20:14:53 -0500 | [diff] [blame] | 1120 | int nh_count = 0; |
| 1121 | int if_count = 0; |
| 1122 | int max_nh_if = 0; |
| 1123 | unsigned int ifindex; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1124 | |
Ayan Banerjee | 34c5d89 | 2015-11-09 20:14:53 -0500 | [diff] [blame] | 1125 | gateway_num = stream_getc (s); |
| 1126 | for (i = 0; i < gateway_num; i++) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1127 | { |
| 1128 | nexthop_type = stream_getc (s); |
| 1129 | |
| 1130 | switch (nexthop_type) |
| 1131 | { |
| 1132 | case ZEBRA_NEXTHOP_IPV6: |
| 1133 | stream_get (&nexthop, s, 16); |
Ayan Banerjee | 34c5d89 | 2015-11-09 20:14:53 -0500 | [diff] [blame] | 1134 | if (nh_count < MULTIPATH_NUM) { |
| 1135 | nexthops[nh_count++] = nexthop; |
| 1136 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1137 | break; |
| 1138 | case ZEBRA_NEXTHOP_IFINDEX: |
| 1139 | ifindex = stream_getl (s); |
Ayan Banerjee | 34c5d89 | 2015-11-09 20:14:53 -0500 | [diff] [blame] | 1140 | if (if_count < MULTIPATH_NUM) { |
| 1141 | ifindices[if_count++] = ifindex; |
| 1142 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1143 | break; |
| 1144 | } |
| 1145 | } |
Ayan Banerjee | 34c5d89 | 2015-11-09 20:14:53 -0500 | [diff] [blame] | 1146 | |
| 1147 | max_nh_if = (nh_count > if_count) ? nh_count : if_count; |
| 1148 | for (i = 0; i < max_nh_if; i++) |
| 1149 | { |
| 1150 | if ((i < nh_count) && !IN6_IS_ADDR_UNSPECIFIED (&nexthops[i])) |
| 1151 | { |
| 1152 | if ((i < if_count) && ifindices[i]) |
| 1153 | nexthop_ipv6_ifindex_add (rib, &nexthops[i], ifindices[i]); |
| 1154 | else |
| 1155 | nexthop_ipv6_add (rib, &nexthops[i]); |
| 1156 | } |
| 1157 | else |
| 1158 | { |
| 1159 | if ((i < if_count) && ifindices[i]) |
| 1160 | nexthop_ifindex_add (rib, ifindices[i]); |
| 1161 | } |
| 1162 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1163 | } |
| 1164 | |
Ayan Banerjee | 34c5d89 | 2015-11-09 20:14:53 -0500 | [diff] [blame] | 1165 | /* Distance. */ |
| 1166 | if (CHECK_FLAG (message, ZAPI_MESSAGE_DISTANCE)) |
| 1167 | rib->distance = stream_getc (s); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1168 | |
Ayan Banerjee | 34c5d89 | 2015-11-09 20:14:53 -0500 | [diff] [blame] | 1169 | /* Metric. */ |
| 1170 | if (CHECK_FLAG (message, ZAPI_MESSAGE_METRIC)) |
| 1171 | rib->metric = stream_getl (s); |
Timo Teräs | b11f3b5 | 2015-11-02 16:50:07 +0200 | [diff] [blame] | 1172 | |
Ayan Banerjee | 34c5d89 | 2015-11-09 20:14:53 -0500 | [diff] [blame] | 1173 | if (CHECK_FLAG (message, ZAPI_MESSAGE_MTU)) |
| 1174 | rib->mtu = stream_getl (s); |
| 1175 | |
| 1176 | /* Table */ |
| 1177 | rib->table=zebrad.rtm_table_default; |
| 1178 | rib_add_ipv6_multipath (&p, rib, safi); |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1179 | return 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1180 | } |
| 1181 | |
| 1182 | /* Zebra server IPv6 prefix delete function. */ |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1183 | static int |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1184 | zread_ipv6_delete (struct zserv *client, u_short length, vrf_id_t vrf_id) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1185 | { |
| 1186 | int i; |
| 1187 | struct stream *s; |
| 1188 | struct zapi_ipv6 api; |
| 1189 | struct in6_addr nexthop; |
| 1190 | unsigned long ifindex; |
| 1191 | struct prefix_ipv6 p; |
| 1192 | |
| 1193 | s = client->ibuf; |
| 1194 | ifindex = 0; |
| 1195 | memset (&nexthop, 0, sizeof (struct in6_addr)); |
| 1196 | |
| 1197 | /* Type, flags, message. */ |
| 1198 | api.type = stream_getc (s); |
| 1199 | api.flags = stream_getc (s); |
| 1200 | api.message = stream_getc (s); |
G.Balaji | f768f36 | 2011-11-26 22:10:39 +0400 | [diff] [blame] | 1201 | api.safi = stream_getw (s); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1202 | |
| 1203 | /* IPv4 prefix. */ |
| 1204 | memset (&p, 0, sizeof (struct prefix_ipv6)); |
| 1205 | p.family = AF_INET6; |
| 1206 | p.prefixlen = stream_getc (s); |
| 1207 | stream_get (&p.prefix, s, PSIZE (p.prefixlen)); |
| 1208 | |
| 1209 | /* Nexthop, ifindex, distance, metric. */ |
| 1210 | if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP)) |
| 1211 | { |
| 1212 | u_char nexthop_type; |
| 1213 | |
| 1214 | api.nexthop_num = stream_getc (s); |
| 1215 | for (i = 0; i < api.nexthop_num; i++) |
| 1216 | { |
| 1217 | nexthop_type = stream_getc (s); |
| 1218 | |
| 1219 | switch (nexthop_type) |
| 1220 | { |
| 1221 | case ZEBRA_NEXTHOP_IPV6: |
| 1222 | stream_get (&nexthop, s, 16); |
| 1223 | break; |
| 1224 | case ZEBRA_NEXTHOP_IFINDEX: |
| 1225 | ifindex = stream_getl (s); |
| 1226 | break; |
| 1227 | } |
| 1228 | } |
| 1229 | } |
| 1230 | |
| 1231 | if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE)) |
| 1232 | api.distance = stream_getc (s); |
| 1233 | else |
| 1234 | api.distance = 0; |
| 1235 | if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC)) |
| 1236 | api.metric = stream_getl (s); |
| 1237 | else |
| 1238 | api.metric = 0; |
| 1239 | |
| 1240 | if (IN6_IS_ADDR_UNSPECIFIED (&nexthop)) |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1241 | rib_delete_ipv6 (api.type, api.flags, &p, NULL, ifindex, vrf_id, |
Feng Lu | 0d0686f | 2015-05-22 11:40:02 +0200 | [diff] [blame] | 1242 | api.safi); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1243 | else |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1244 | rib_delete_ipv6 (api.type, api.flags, &p, &nexthop, ifindex, vrf_id, |
Feng Lu | 0d0686f | 2015-05-22 11:40:02 +0200 | [diff] [blame] | 1245 | api.safi); |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1246 | return 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1247 | } |
| 1248 | |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1249 | static int |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1250 | zread_ipv6_nexthop_lookup (struct zserv *client, u_short length, |
| 1251 | vrf_id_t vrf_id) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1252 | { |
| 1253 | struct in6_addr addr; |
| 1254 | char buf[BUFSIZ]; |
| 1255 | |
| 1256 | stream_get (&addr, client->ibuf, 16); |
Christian Franke | a520708 | 2013-04-11 08:24:29 +0000 | [diff] [blame] | 1257 | if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV) |
| 1258 | zlog_debug("%s: looking up %s", __func__, |
| 1259 | inet_ntop (AF_INET6, &addr, buf, BUFSIZ)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1260 | |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1261 | return zsend_ipv6_nexthop_lookup (client, &addr, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1262 | } |
| 1263 | #endif /* HAVE_IPV6 */ |
| 1264 | |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 1265 | /* Register zebra server router-id information. Send current router-id */ |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1266 | static int |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1267 | zread_router_id_add (struct zserv *client, u_short length, vrf_id_t vrf_id) |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 1268 | { |
| 1269 | struct prefix p; |
| 1270 | |
| 1271 | /* Router-id information is needed. */ |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1272 | vrf_bitmap_set (client->ridinfo, vrf_id); |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 1273 | |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1274 | router_id_get (&p, vrf_id); |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 1275 | |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1276 | return zsend_router_id_update (client, &p, vrf_id); |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 1277 | } |
| 1278 | |
| 1279 | /* Unregister zebra server router-id information. */ |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1280 | static int |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1281 | zread_router_id_delete (struct zserv *client, u_short length, vrf_id_t vrf_id) |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 1282 | { |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1283 | vrf_bitmap_unset (client->ridinfo, vrf_id); |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1284 | return 0; |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 1285 | } |
| 1286 | |
Vyacheslav Trushkin | 2ea1ab1 | 2011-12-11 18:48:47 +0400 | [diff] [blame] | 1287 | /* Tie up route-type and client->sock */ |
| 1288 | static void |
| 1289 | zread_hello (struct zserv *client) |
| 1290 | { |
| 1291 | /* type of protocol (lib/zebra.h) */ |
| 1292 | u_char proto; |
| 1293 | proto = stream_getc (client->ibuf); |
| 1294 | |
| 1295 | /* accept only dynamic routing protocols */ |
| 1296 | if ((proto < ZEBRA_ROUTE_MAX) |
| 1297 | && (proto > ZEBRA_ROUTE_STATIC)) |
| 1298 | { |
| 1299 | zlog_notice ("client %d says hello and bids fair to announce only %s routes", |
| 1300 | client->sock, zebra_route_string(proto)); |
| 1301 | |
| 1302 | /* if route-type was binded by other client */ |
| 1303 | if (route_type_oaths[proto]) |
| 1304 | zlog_warn ("sender of %s routes changed %c->%c", |
| 1305 | zebra_route_string(proto), route_type_oaths[proto], |
| 1306 | client->sock); |
| 1307 | |
| 1308 | route_type_oaths[proto] = client->sock; |
| 1309 | } |
| 1310 | } |
| 1311 | |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1312 | /* Unregister all information in a VRF. */ |
| 1313 | static int |
| 1314 | zread_vrf_unregister (struct zserv *client, u_short length, vrf_id_t vrf_id) |
| 1315 | { |
| 1316 | int i; |
| 1317 | |
| 1318 | for (i = 0; i < ZEBRA_ROUTE_MAX; i++) |
| 1319 | vrf_bitmap_unset (client->redist[i], vrf_id); |
| 1320 | vrf_bitmap_unset (client->redist_default, vrf_id); |
| 1321 | vrf_bitmap_unset (client->ifinfo, vrf_id); |
| 1322 | vrf_bitmap_unset (client->ridinfo, vrf_id); |
| 1323 | |
| 1324 | return 0; |
| 1325 | } |
| 1326 | |
Vyacheslav Trushkin | 2ea1ab1 | 2011-12-11 18:48:47 +0400 | [diff] [blame] | 1327 | /* If client sent routes of specific type, zebra removes it |
| 1328 | * and returns number of deleted routes. |
| 1329 | */ |
| 1330 | static void |
| 1331 | zebra_score_rib (int client_sock) |
| 1332 | { |
| 1333 | int i; |
| 1334 | |
| 1335 | for (i = ZEBRA_ROUTE_RIP; i < ZEBRA_ROUTE_MAX; i++) |
| 1336 | if (client_sock == route_type_oaths[i]) |
| 1337 | { |
| 1338 | zlog_notice ("client %d disconnected. %lu %s routes removed from the rib", |
| 1339 | client_sock, rib_score_proto (i), zebra_route_string (i)); |
| 1340 | route_type_oaths[i] = 0; |
| 1341 | break; |
| 1342 | } |
| 1343 | } |
| 1344 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1345 | /* Close zebra client. */ |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 1346 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1347 | zebra_client_close (struct zserv *client) |
| 1348 | { |
| 1349 | /* Close file descriptor. */ |
| 1350 | if (client->sock) |
| 1351 | { |
| 1352 | close (client->sock); |
Vyacheslav Trushkin | 2ea1ab1 | 2011-12-11 18:48:47 +0400 | [diff] [blame] | 1353 | zebra_score_rib (client->sock); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1354 | client->sock = -1; |
| 1355 | } |
| 1356 | |
| 1357 | /* Free stream buffers. */ |
| 1358 | if (client->ibuf) |
| 1359 | stream_free (client->ibuf); |
| 1360 | if (client->obuf) |
| 1361 | stream_free (client->obuf); |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1362 | if (client->wb) |
| 1363 | buffer_free(client->wb); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1364 | |
| 1365 | /* Release threads. */ |
| 1366 | if (client->t_read) |
| 1367 | thread_cancel (client->t_read); |
| 1368 | if (client->t_write) |
| 1369 | thread_cancel (client->t_write); |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1370 | if (client->t_suicide) |
| 1371 | thread_cancel (client->t_suicide); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1372 | |
| 1373 | /* Free client structure. */ |
paul | b21b19c | 2003-06-15 01:28:29 +0000 | [diff] [blame] | 1374 | listnode_delete (zebrad.client_list, client); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1375 | XFREE (0, client); |
| 1376 | } |
| 1377 | |
| 1378 | /* Make new client. */ |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 1379 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1380 | zebra_client_create (int sock) |
| 1381 | { |
| 1382 | struct zserv *client; |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1383 | int i; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1384 | |
David Lamparter | 23757db | 2016-02-24 06:26:02 +0100 | [diff] [blame] | 1385 | client = XCALLOC (MTYPE_TMP, sizeof (struct zserv)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1386 | |
| 1387 | /* Make client input/output buffer. */ |
| 1388 | client->sock = sock; |
| 1389 | client->ibuf = stream_new (ZEBRA_MAX_PACKET_SIZ); |
| 1390 | client->obuf = stream_new (ZEBRA_MAX_PACKET_SIZ); |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1391 | client->wb = buffer_new(0); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1392 | |
| 1393 | /* Set table number. */ |
paul | b21b19c | 2003-06-15 01:28:29 +0000 | [diff] [blame] | 1394 | client->rtm_table = zebrad.rtm_table_default; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1395 | |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1396 | /* Initialize flags */ |
| 1397 | for (i = 0; i < ZEBRA_ROUTE_MAX; i++) |
| 1398 | client->redist[i] = vrf_bitmap_init (); |
| 1399 | client->redist_default = vrf_bitmap_init (); |
| 1400 | client->ifinfo = vrf_bitmap_init (); |
| 1401 | client->ridinfo = vrf_bitmap_init (); |
| 1402 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1403 | /* Add this client to linked list. */ |
paul | b21b19c | 2003-06-15 01:28:29 +0000 | [diff] [blame] | 1404 | listnode_add (zebrad.client_list, client); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1405 | |
| 1406 | /* Make new read thread. */ |
| 1407 | zebra_event (ZEBRA_READ, sock, client); |
| 1408 | } |
| 1409 | |
| 1410 | /* Handler of zebra service request. */ |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 1411 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1412 | zebra_client_read (struct thread *thread) |
| 1413 | { |
| 1414 | int sock; |
| 1415 | struct zserv *client; |
ajs | 57a1477 | 2005-04-10 15:01:56 +0000 | [diff] [blame] | 1416 | size_t already; |
paul | c1b9800 | 2006-01-16 01:54:02 +0000 | [diff] [blame] | 1417 | uint16_t length, command; |
| 1418 | uint8_t marker, version; |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1419 | vrf_id_t vrf_id; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1420 | |
| 1421 | /* Get thread data. Reset reading thread because I'm running. */ |
| 1422 | sock = THREAD_FD (thread); |
| 1423 | client = THREAD_ARG (thread); |
| 1424 | client->t_read = NULL; |
| 1425 | |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1426 | if (client->t_suicide) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1427 | { |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1428 | zebra_client_close(client); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1429 | return -1; |
| 1430 | } |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1431 | |
| 1432 | /* Read length and command (if we don't have it already). */ |
ajs | 57a1477 | 2005-04-10 15:01:56 +0000 | [diff] [blame] | 1433 | if ((already = stream_get_endp(client->ibuf)) < ZEBRA_HEADER_SIZE) |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1434 | { |
ajs | 57a1477 | 2005-04-10 15:01:56 +0000 | [diff] [blame] | 1435 | ssize_t nbyte; |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1436 | if (((nbyte = stream_read_try (client->ibuf, sock, |
ajs | 57a1477 | 2005-04-10 15:01:56 +0000 | [diff] [blame] | 1437 | ZEBRA_HEADER_SIZE-already)) == 0) || |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1438 | (nbyte == -1)) |
| 1439 | { |
| 1440 | if (IS_ZEBRA_DEBUG_EVENT) |
| 1441 | zlog_debug ("connection closed socket [%d]", sock); |
| 1442 | zebra_client_close (client); |
| 1443 | return -1; |
| 1444 | } |
ajs | 57a1477 | 2005-04-10 15:01:56 +0000 | [diff] [blame] | 1445 | if (nbyte != (ssize_t)(ZEBRA_HEADER_SIZE-already)) |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1446 | { |
| 1447 | /* Try again later. */ |
| 1448 | zebra_event (ZEBRA_READ, sock, client); |
| 1449 | return 0; |
| 1450 | } |
ajs | 57a1477 | 2005-04-10 15:01:56 +0000 | [diff] [blame] | 1451 | already = ZEBRA_HEADER_SIZE; |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1452 | } |
| 1453 | |
| 1454 | /* Reset to read from the beginning of the incoming packet. */ |
| 1455 | stream_set_getp(client->ibuf, 0); |
| 1456 | |
paul | c1b9800 | 2006-01-16 01:54:02 +0000 | [diff] [blame] | 1457 | /* Fetch header values */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1458 | length = stream_getw (client->ibuf); |
paul | c1b9800 | 2006-01-16 01:54:02 +0000 | [diff] [blame] | 1459 | marker = stream_getc (client->ibuf); |
| 1460 | version = stream_getc (client->ibuf); |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1461 | vrf_id = stream_getw (client->ibuf); |
paul | c1b9800 | 2006-01-16 01:54:02 +0000 | [diff] [blame] | 1462 | command = stream_getw (client->ibuf); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1463 | |
paul | c1b9800 | 2006-01-16 01:54:02 +0000 | [diff] [blame] | 1464 | if (marker != ZEBRA_HEADER_MARKER || version != ZSERV_VERSION) |
| 1465 | { |
| 1466 | zlog_err("%s: socket %d version mismatch, marker %d, version %d", |
| 1467 | __func__, sock, marker, version); |
| 1468 | zebra_client_close (client); |
| 1469 | return -1; |
| 1470 | } |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1471 | if (length < ZEBRA_HEADER_SIZE) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1472 | { |
ajs | 57a1477 | 2005-04-10 15:01:56 +0000 | [diff] [blame] | 1473 | zlog_warn("%s: socket %d message length %u is less than header size %d", |
| 1474 | __func__, sock, length, ZEBRA_HEADER_SIZE); |
| 1475 | zebra_client_close (client); |
| 1476 | return -1; |
| 1477 | } |
| 1478 | if (length > STREAM_SIZE(client->ibuf)) |
| 1479 | { |
| 1480 | zlog_warn("%s: socket %d message length %u exceeds buffer size %lu", |
| 1481 | __func__, sock, length, (u_long)STREAM_SIZE(client->ibuf)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1482 | zebra_client_close (client); |
| 1483 | return -1; |
| 1484 | } |
| 1485 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1486 | /* Read rest of data. */ |
ajs | 57a1477 | 2005-04-10 15:01:56 +0000 | [diff] [blame] | 1487 | if (already < length) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1488 | { |
ajs | 57a1477 | 2005-04-10 15:01:56 +0000 | [diff] [blame] | 1489 | ssize_t nbyte; |
| 1490 | if (((nbyte = stream_read_try (client->ibuf, sock, |
| 1491 | length-already)) == 0) || |
| 1492 | (nbyte == -1)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1493 | { |
| 1494 | if (IS_ZEBRA_DEBUG_EVENT) |
ajs | b617800 | 2004-12-07 21:12:56 +0000 | [diff] [blame] | 1495 | zlog_debug ("connection closed [%d] when reading zebra data", sock); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1496 | zebra_client_close (client); |
| 1497 | return -1; |
| 1498 | } |
ajs | 57a1477 | 2005-04-10 15:01:56 +0000 | [diff] [blame] | 1499 | if (nbyte != (ssize_t)(length-already)) |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1500 | { |
| 1501 | /* Try again later. */ |
| 1502 | zebra_event (ZEBRA_READ, sock, client); |
| 1503 | return 0; |
| 1504 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1505 | } |
| 1506 | |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1507 | length -= ZEBRA_HEADER_SIZE; |
| 1508 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1509 | /* Debug packet information. */ |
| 1510 | if (IS_ZEBRA_DEBUG_EVENT) |
ajs | b617800 | 2004-12-07 21:12:56 +0000 | [diff] [blame] | 1511 | zlog_debug ("zebra message comes from socket [%d]", sock); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1512 | |
| 1513 | if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV) |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1514 | zlog_debug ("zebra message received [%s] %d in VRF %u", |
| 1515 | zserv_command_string (command), length, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1516 | |
| 1517 | switch (command) |
| 1518 | { |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 1519 | case ZEBRA_ROUTER_ID_ADD: |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1520 | zread_router_id_add (client, length, vrf_id); |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 1521 | break; |
| 1522 | case ZEBRA_ROUTER_ID_DELETE: |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1523 | zread_router_id_delete (client, length, vrf_id); |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 1524 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1525 | case ZEBRA_INTERFACE_ADD: |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1526 | zread_interface_add (client, length, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1527 | break; |
| 1528 | case ZEBRA_INTERFACE_DELETE: |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1529 | zread_interface_delete (client, length, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1530 | break; |
| 1531 | case ZEBRA_IPV4_ROUTE_ADD: |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1532 | zread_ipv4_add (client, length, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1533 | break; |
| 1534 | case ZEBRA_IPV4_ROUTE_DELETE: |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1535 | zread_ipv4_delete (client, length, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1536 | break; |
| 1537 | #ifdef HAVE_IPV6 |
| 1538 | case ZEBRA_IPV6_ROUTE_ADD: |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1539 | zread_ipv6_add (client, length, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1540 | break; |
| 1541 | case ZEBRA_IPV6_ROUTE_DELETE: |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1542 | zread_ipv6_delete (client, length, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1543 | break; |
| 1544 | #endif /* HAVE_IPV6 */ |
| 1545 | case ZEBRA_REDISTRIBUTE_ADD: |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1546 | zebra_redistribute_add (command, client, length, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1547 | break; |
| 1548 | case ZEBRA_REDISTRIBUTE_DELETE: |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1549 | zebra_redistribute_delete (command, client, length, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1550 | break; |
| 1551 | case ZEBRA_REDISTRIBUTE_DEFAULT_ADD: |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1552 | zebra_redistribute_default_add (command, client, length, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1553 | break; |
| 1554 | case ZEBRA_REDISTRIBUTE_DEFAULT_DELETE: |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1555 | zebra_redistribute_default_delete (command, client, length, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1556 | break; |
| 1557 | case ZEBRA_IPV4_NEXTHOP_LOOKUP: |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1558 | zread_ipv4_nexthop_lookup (client, length, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1559 | break; |
Everton Marques | 4e5275b | 2014-07-01 15:15:52 -0300 | [diff] [blame] | 1560 | case ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB: |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1561 | zread_ipv4_nexthop_lookup_mrib (client, length, vrf_id); |
Everton Marques | 4e5275b | 2014-07-01 15:15:52 -0300 | [diff] [blame] | 1562 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1563 | #ifdef HAVE_IPV6 |
| 1564 | case ZEBRA_IPV6_NEXTHOP_LOOKUP: |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1565 | zread_ipv6_nexthop_lookup (client, length, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1566 | break; |
| 1567 | #endif /* HAVE_IPV6 */ |
| 1568 | case ZEBRA_IPV4_IMPORT_LOOKUP: |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1569 | zread_ipv4_import_lookup (client, length, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1570 | break; |
Vyacheslav Trushkin | 2ea1ab1 | 2011-12-11 18:48:47 +0400 | [diff] [blame] | 1571 | case ZEBRA_HELLO: |
| 1572 | zread_hello (client); |
| 1573 | break; |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1574 | case ZEBRA_VRF_UNREGISTER: |
| 1575 | zread_vrf_unregister (client, length, vrf_id); |
| 1576 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1577 | default: |
| 1578 | zlog_info ("Zebra received unknown command %d", command); |
| 1579 | break; |
| 1580 | } |
| 1581 | |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1582 | if (client->t_suicide) |
| 1583 | { |
| 1584 | /* No need to wait for thread callback, just kill immediately. */ |
| 1585 | zebra_client_close(client); |
| 1586 | return -1; |
| 1587 | } |
| 1588 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1589 | stream_reset (client->ibuf); |
| 1590 | zebra_event (ZEBRA_READ, sock, client); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1591 | return 0; |
| 1592 | } |
| 1593 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1594 | |
| 1595 | /* Accept code of zebra server socket. */ |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 1596 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1597 | zebra_accept (struct thread *thread) |
| 1598 | { |
| 1599 | int accept_sock; |
| 1600 | int client_sock; |
| 1601 | struct sockaddr_in client; |
| 1602 | socklen_t len; |
| 1603 | |
| 1604 | accept_sock = THREAD_FD (thread); |
| 1605 | |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1606 | /* Reregister myself. */ |
| 1607 | zebra_event (ZEBRA_SERV, accept_sock, NULL); |
| 1608 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1609 | len = sizeof (struct sockaddr_in); |
| 1610 | client_sock = accept (accept_sock, (struct sockaddr *) &client, &len); |
| 1611 | |
| 1612 | if (client_sock < 0) |
| 1613 | { |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 1614 | zlog_warn ("Can't accept zebra socket: %s", safe_strerror (errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1615 | return -1; |
| 1616 | } |
| 1617 | |
paul | ccf3557 | 2003-03-01 11:42:20 +0000 | [diff] [blame] | 1618 | /* Make client socket non-blocking. */ |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1619 | set_nonblocking(client_sock); |
paul | 865b852 | 2005-01-05 08:30:35 +0000 | [diff] [blame] | 1620 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1621 | /* Create new zebra client. */ |
| 1622 | zebra_client_create (client_sock); |
| 1623 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1624 | return 0; |
| 1625 | } |
| 1626 | |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 1627 | #ifdef HAVE_TCP_ZEBRA |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1628 | /* Make zebra's server socket. */ |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 1629 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1630 | zebra_serv () |
| 1631 | { |
| 1632 | int ret; |
| 1633 | int accept_sock; |
| 1634 | struct sockaddr_in addr; |
| 1635 | |
| 1636 | accept_sock = socket (AF_INET, SOCK_STREAM, 0); |
| 1637 | |
| 1638 | if (accept_sock < 0) |
| 1639 | { |
paul | 3d1dc85 | 2005-04-05 00:45:23 +0000 | [diff] [blame] | 1640 | zlog_warn ("Can't create zserv stream socket: %s", |
| 1641 | safe_strerror (errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1642 | zlog_warn ("zebra can't provice full functionality due to above error"); |
| 1643 | return; |
| 1644 | } |
| 1645 | |
Vyacheslav Trushkin | 2ea1ab1 | 2011-12-11 18:48:47 +0400 | [diff] [blame] | 1646 | memset (&route_type_oaths, 0, sizeof (route_type_oaths)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1647 | memset (&addr, 0, sizeof (struct sockaddr_in)); |
| 1648 | addr.sin_family = AF_INET; |
| 1649 | addr.sin_port = htons (ZEBRA_PORT); |
Paul Jakma | 6f0e3f6 | 2007-05-10 02:38:51 +0000 | [diff] [blame] | 1650 | #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1651 | addr.sin_len = sizeof (struct sockaddr_in); |
Paul Jakma | 6f0e3f6 | 2007-05-10 02:38:51 +0000 | [diff] [blame] | 1652 | #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1653 | addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK); |
| 1654 | |
| 1655 | sockopt_reuseaddr (accept_sock); |
| 1656 | sockopt_reuseport (accept_sock); |
| 1657 | |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 1658 | if ( zserv_privs.change(ZPRIVS_RAISE) ) |
| 1659 | zlog (NULL, LOG_ERR, "Can't raise privileges"); |
| 1660 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1661 | ret = bind (accept_sock, (struct sockaddr *)&addr, |
| 1662 | sizeof (struct sockaddr_in)); |
| 1663 | if (ret < 0) |
| 1664 | { |
paul | 3d1dc85 | 2005-04-05 00:45:23 +0000 | [diff] [blame] | 1665 | zlog_warn ("Can't bind to stream socket: %s", |
| 1666 | safe_strerror (errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1667 | zlog_warn ("zebra can't provice full functionality due to above error"); |
| 1668 | close (accept_sock); /* Avoid sd leak. */ |
| 1669 | return; |
| 1670 | } |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 1671 | |
| 1672 | if ( zserv_privs.change(ZPRIVS_LOWER) ) |
| 1673 | zlog (NULL, LOG_ERR, "Can't lower privileges"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1674 | |
| 1675 | ret = listen (accept_sock, 1); |
| 1676 | if (ret < 0) |
| 1677 | { |
paul | 3d1dc85 | 2005-04-05 00:45:23 +0000 | [diff] [blame] | 1678 | zlog_warn ("Can't listen to stream socket: %s", |
| 1679 | safe_strerror (errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1680 | zlog_warn ("zebra can't provice full functionality due to above error"); |
| 1681 | close (accept_sock); /* Avoid sd leak. */ |
| 1682 | return; |
| 1683 | } |
| 1684 | |
| 1685 | zebra_event (ZEBRA_SERV, accept_sock, NULL); |
| 1686 | } |
David Lamparter | 4b6c332 | 2015-04-21 09:47:57 +0200 | [diff] [blame] | 1687 | #else /* HAVE_TCP_ZEBRA */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1688 | |
| 1689 | /* For sockaddr_un. */ |
| 1690 | #include <sys/un.h> |
| 1691 | |
| 1692 | /* zebra server UNIX domain socket. */ |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 1693 | static void |
hasso | fce954f | 2004-10-07 20:29:24 +0000 | [diff] [blame] | 1694 | zebra_serv_un (const char *path) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1695 | { |
| 1696 | int ret; |
| 1697 | int sock, len; |
| 1698 | struct sockaddr_un serv; |
| 1699 | mode_t old_mask; |
| 1700 | |
| 1701 | /* First of all, unlink existing socket */ |
| 1702 | unlink (path); |
| 1703 | |
| 1704 | /* Set umask */ |
| 1705 | old_mask = umask (0077); |
| 1706 | |
| 1707 | /* Make UNIX domain socket. */ |
| 1708 | sock = socket (AF_UNIX, SOCK_STREAM, 0); |
| 1709 | if (sock < 0) |
| 1710 | { |
paul | 3d1dc85 | 2005-04-05 00:45:23 +0000 | [diff] [blame] | 1711 | zlog_warn ("Can't create zserv unix socket: %s", |
| 1712 | safe_strerror (errno)); |
| 1713 | zlog_warn ("zebra can't provide full functionality due to above error"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1714 | return; |
| 1715 | } |
| 1716 | |
Vyacheslav Trushkin | 2ea1ab1 | 2011-12-11 18:48:47 +0400 | [diff] [blame] | 1717 | memset (&route_type_oaths, 0, sizeof (route_type_oaths)); |
| 1718 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1719 | /* Make server socket. */ |
| 1720 | memset (&serv, 0, sizeof (struct sockaddr_un)); |
| 1721 | serv.sun_family = AF_UNIX; |
| 1722 | strncpy (serv.sun_path, path, strlen (path)); |
Paul Jakma | 6f0e3f6 | 2007-05-10 02:38:51 +0000 | [diff] [blame] | 1723 | #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1724 | len = serv.sun_len = SUN_LEN(&serv); |
| 1725 | #else |
| 1726 | len = sizeof (serv.sun_family) + strlen (serv.sun_path); |
Paul Jakma | 6f0e3f6 | 2007-05-10 02:38:51 +0000 | [diff] [blame] | 1727 | #endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1728 | |
| 1729 | ret = bind (sock, (struct sockaddr *) &serv, len); |
| 1730 | if (ret < 0) |
| 1731 | { |
paul | 3d1dc85 | 2005-04-05 00:45:23 +0000 | [diff] [blame] | 1732 | zlog_warn ("Can't bind to unix socket %s: %s", |
| 1733 | path, safe_strerror (errno)); |
| 1734 | zlog_warn ("zebra can't provide full functionality due to above error"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1735 | close (sock); |
| 1736 | return; |
| 1737 | } |
| 1738 | |
| 1739 | ret = listen (sock, 5); |
| 1740 | if (ret < 0) |
| 1741 | { |
paul | 3d1dc85 | 2005-04-05 00:45:23 +0000 | [diff] [blame] | 1742 | zlog_warn ("Can't listen to unix socket %s: %s", |
| 1743 | path, safe_strerror (errno)); |
| 1744 | zlog_warn ("zebra can't provide full functionality due to above error"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1745 | close (sock); |
| 1746 | return; |
| 1747 | } |
| 1748 | |
| 1749 | umask (old_mask); |
| 1750 | |
| 1751 | zebra_event (ZEBRA_SERV, sock, NULL); |
| 1752 | } |
David Lamparter | 4b6c332 | 2015-04-21 09:47:57 +0200 | [diff] [blame] | 1753 | #endif /* HAVE_TCP_ZEBRA */ |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 1754 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1755 | |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 1756 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1757 | zebra_event (enum event event, int sock, struct zserv *client) |
| 1758 | { |
| 1759 | switch (event) |
| 1760 | { |
| 1761 | case ZEBRA_SERV: |
paul | b21b19c | 2003-06-15 01:28:29 +0000 | [diff] [blame] | 1762 | thread_add_read (zebrad.master, zebra_accept, client, sock); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1763 | break; |
| 1764 | case ZEBRA_READ: |
| 1765 | client->t_read = |
paul | b21b19c | 2003-06-15 01:28:29 +0000 | [diff] [blame] | 1766 | thread_add_read (zebrad.master, zebra_client_read, client, sock); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1767 | break; |
| 1768 | case ZEBRA_WRITE: |
| 1769 | /**/ |
| 1770 | break; |
| 1771 | } |
| 1772 | } |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 1773 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1774 | /* Display default rtm_table for all clients. */ |
| 1775 | DEFUN (show_table, |
| 1776 | show_table_cmd, |
| 1777 | "show table", |
| 1778 | SHOW_STR |
| 1779 | "default routing table to use for all clients\n") |
| 1780 | { |
paul | b21b19c | 2003-06-15 01:28:29 +0000 | [diff] [blame] | 1781 | vty_out (vty, "table %d%s", zebrad.rtm_table_default, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1782 | VTY_NEWLINE); |
| 1783 | return CMD_SUCCESS; |
| 1784 | } |
| 1785 | |
| 1786 | DEFUN (config_table, |
| 1787 | config_table_cmd, |
| 1788 | "table TABLENO", |
| 1789 | "Configure target kernel routing table\n" |
| 1790 | "TABLE integer\n") |
| 1791 | { |
paul | b21b19c | 2003-06-15 01:28:29 +0000 | [diff] [blame] | 1792 | zebrad.rtm_table_default = strtol (argv[0], (char**)0, 10); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1793 | return CMD_SUCCESS; |
| 1794 | } |
| 1795 | |
hasso | 647e4f1 | 2003-05-25 11:43:52 +0000 | [diff] [blame] | 1796 | DEFUN (ip_forwarding, |
| 1797 | ip_forwarding_cmd, |
| 1798 | "ip forwarding", |
| 1799 | IP_STR |
| 1800 | "Turn on IP forwarding") |
| 1801 | { |
| 1802 | int ret; |
| 1803 | |
| 1804 | ret = ipforward (); |
hasso | b71f00f | 2004-10-13 12:20:35 +0000 | [diff] [blame] | 1805 | if (ret == 0) |
| 1806 | ret = ipforward_on (); |
hasso | 647e4f1 | 2003-05-25 11:43:52 +0000 | [diff] [blame] | 1807 | |
hasso | 647e4f1 | 2003-05-25 11:43:52 +0000 | [diff] [blame] | 1808 | if (ret == 0) |
| 1809 | { |
| 1810 | vty_out (vty, "Can't turn on IP forwarding%s", VTY_NEWLINE); |
| 1811 | return CMD_WARNING; |
| 1812 | } |
| 1813 | |
| 1814 | return CMD_SUCCESS; |
| 1815 | } |
| 1816 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1817 | DEFUN (no_ip_forwarding, |
| 1818 | no_ip_forwarding_cmd, |
| 1819 | "no ip forwarding", |
| 1820 | NO_STR |
| 1821 | IP_STR |
| 1822 | "Turn off IP forwarding") |
| 1823 | { |
| 1824 | int ret; |
| 1825 | |
| 1826 | ret = ipforward (); |
hasso | b71f00f | 2004-10-13 12:20:35 +0000 | [diff] [blame] | 1827 | if (ret != 0) |
| 1828 | ret = ipforward_off (); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1829 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1830 | if (ret != 0) |
| 1831 | { |
| 1832 | vty_out (vty, "Can't turn off IP forwarding%s", VTY_NEWLINE); |
| 1833 | return CMD_WARNING; |
| 1834 | } |
| 1835 | |
| 1836 | return CMD_SUCCESS; |
| 1837 | } |
| 1838 | |
| 1839 | /* This command is for debugging purpose. */ |
| 1840 | DEFUN (show_zebra_client, |
| 1841 | show_zebra_client_cmd, |
| 1842 | "show zebra client", |
| 1843 | SHOW_STR |
| 1844 | "Zebra information" |
| 1845 | "Client information") |
| 1846 | { |
hasso | 52dc7ee | 2004-09-23 19:18:23 +0000 | [diff] [blame] | 1847 | struct listnode *node; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1848 | struct zserv *client; |
| 1849 | |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 1850 | for (ALL_LIST_ELEMENTS_RO (zebrad.client_list, node, client)) |
| 1851 | vty_out (vty, "Client fd %d%s", client->sock, VTY_NEWLINE); |
| 1852 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1853 | return CMD_SUCCESS; |
| 1854 | } |
| 1855 | |
| 1856 | /* Table configuration write function. */ |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 1857 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1858 | config_write_table (struct vty *vty) |
| 1859 | { |
paul | b21b19c | 2003-06-15 01:28:29 +0000 | [diff] [blame] | 1860 | if (zebrad.rtm_table_default) |
| 1861 | vty_out (vty, "table %d%s", zebrad.rtm_table_default, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1862 | VTY_NEWLINE); |
| 1863 | return 0; |
| 1864 | } |
| 1865 | |
| 1866 | /* table node for routing tables. */ |
Stephen Hemminger | 7fc626d | 2008-12-01 11:10:34 -0800 | [diff] [blame] | 1867 | static struct cmd_node table_node = |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1868 | { |
| 1869 | TABLE_NODE, |
| 1870 | "", /* This node has no interface. */ |
| 1871 | 1 |
| 1872 | }; |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 1873 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1874 | /* Only display ip forwarding is enabled or not. */ |
| 1875 | DEFUN (show_ip_forwarding, |
| 1876 | show_ip_forwarding_cmd, |
| 1877 | "show ip forwarding", |
| 1878 | SHOW_STR |
| 1879 | IP_STR |
| 1880 | "IP forwarding status\n") |
| 1881 | { |
| 1882 | int ret; |
| 1883 | |
| 1884 | ret = ipforward (); |
| 1885 | |
| 1886 | if (ret == 0) |
| 1887 | vty_out (vty, "IP forwarding is off%s", VTY_NEWLINE); |
| 1888 | else |
| 1889 | vty_out (vty, "IP forwarding is on%s", VTY_NEWLINE); |
| 1890 | return CMD_SUCCESS; |
| 1891 | } |
| 1892 | |
| 1893 | #ifdef HAVE_IPV6 |
| 1894 | /* Only display ipv6 forwarding is enabled or not. */ |
| 1895 | DEFUN (show_ipv6_forwarding, |
| 1896 | show_ipv6_forwarding_cmd, |
| 1897 | "show ipv6 forwarding", |
| 1898 | SHOW_STR |
| 1899 | "IPv6 information\n" |
| 1900 | "Forwarding status\n") |
| 1901 | { |
| 1902 | int ret; |
| 1903 | |
| 1904 | ret = ipforward_ipv6 (); |
| 1905 | |
| 1906 | switch (ret) |
| 1907 | { |
| 1908 | case -1: |
| 1909 | vty_out (vty, "ipv6 forwarding is unknown%s", VTY_NEWLINE); |
| 1910 | break; |
| 1911 | case 0: |
| 1912 | vty_out (vty, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE); |
| 1913 | break; |
| 1914 | case 1: |
| 1915 | vty_out (vty, "ipv6 forwarding is %s%s", "on", VTY_NEWLINE); |
| 1916 | break; |
| 1917 | default: |
| 1918 | vty_out (vty, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE); |
| 1919 | break; |
| 1920 | } |
| 1921 | return CMD_SUCCESS; |
| 1922 | } |
| 1923 | |
hasso | 5590672 | 2004-02-11 22:42:16 +0000 | [diff] [blame] | 1924 | DEFUN (ipv6_forwarding, |
| 1925 | ipv6_forwarding_cmd, |
| 1926 | "ipv6 forwarding", |
| 1927 | IPV6_STR |
| 1928 | "Turn on IPv6 forwarding") |
| 1929 | { |
| 1930 | int ret; |
| 1931 | |
hasso | 41d3fc9 | 2004-04-06 11:59:00 +0000 | [diff] [blame] | 1932 | ret = ipforward_ipv6 (); |
hasso | b71f00f | 2004-10-13 12:20:35 +0000 | [diff] [blame] | 1933 | if (ret == 0) |
| 1934 | ret = ipforward_ipv6_on (); |
hasso | 41d3fc9 | 2004-04-06 11:59:00 +0000 | [diff] [blame] | 1935 | |
hasso | 41d3fc9 | 2004-04-06 11:59:00 +0000 | [diff] [blame] | 1936 | if (ret == 0) |
| 1937 | { |
hasso | 5590672 | 2004-02-11 22:42:16 +0000 | [diff] [blame] | 1938 | vty_out (vty, "Can't turn on IPv6 forwarding%s", VTY_NEWLINE); |
| 1939 | return CMD_WARNING; |
| 1940 | } |
| 1941 | |
| 1942 | return CMD_SUCCESS; |
| 1943 | } |
| 1944 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1945 | DEFUN (no_ipv6_forwarding, |
| 1946 | no_ipv6_forwarding_cmd, |
| 1947 | "no ipv6 forwarding", |
| 1948 | NO_STR |
hasso | 5590672 | 2004-02-11 22:42:16 +0000 | [diff] [blame] | 1949 | IPV6_STR |
| 1950 | "Turn off IPv6 forwarding") |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1951 | { |
| 1952 | int ret; |
| 1953 | |
hasso | 41d3fc9 | 2004-04-06 11:59:00 +0000 | [diff] [blame] | 1954 | ret = ipforward_ipv6 (); |
hasso | b71f00f | 2004-10-13 12:20:35 +0000 | [diff] [blame] | 1955 | if (ret != 0) |
| 1956 | ret = ipforward_ipv6_off (); |
hasso | 41d3fc9 | 2004-04-06 11:59:00 +0000 | [diff] [blame] | 1957 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1958 | if (ret != 0) |
| 1959 | { |
| 1960 | vty_out (vty, "Can't turn off IPv6 forwarding%s", VTY_NEWLINE); |
| 1961 | return CMD_WARNING; |
| 1962 | } |
| 1963 | |
| 1964 | return CMD_SUCCESS; |
| 1965 | } |
| 1966 | |
| 1967 | #endif /* HAVE_IPV6 */ |
| 1968 | |
| 1969 | /* IPForwarding configuration write function. */ |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1970 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1971 | config_write_forwarding (struct vty *vty) |
| 1972 | { |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 1973 | /* FIXME: Find better place for that. */ |
| 1974 | router_id_write (vty); |
| 1975 | |
paul | 3e0b3a5 | 2004-08-23 18:58:32 +0000 | [diff] [blame] | 1976 | if (ipforward ()) |
| 1977 | vty_out (vty, "ip forwarding%s", VTY_NEWLINE); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1978 | #ifdef HAVE_IPV6 |
paul | 3e0b3a5 | 2004-08-23 18:58:32 +0000 | [diff] [blame] | 1979 | if (ipforward_ipv6 ()) |
| 1980 | vty_out (vty, "ipv6 forwarding%s", VTY_NEWLINE); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1981 | #endif /* HAVE_IPV6 */ |
| 1982 | vty_out (vty, "!%s", VTY_NEWLINE); |
| 1983 | return 0; |
| 1984 | } |
| 1985 | |
| 1986 | /* table node for routing tables. */ |
Stephen Hemminger | 7fc626d | 2008-12-01 11:10:34 -0800 | [diff] [blame] | 1987 | static struct cmd_node forwarding_node = |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1988 | { |
| 1989 | FORWARDING_NODE, |
| 1990 | "", /* This node has no interface. */ |
| 1991 | 1 |
| 1992 | }; |
| 1993 | |
Udaya Shankara KS | d869dbd | 2016-02-11 21:42:29 +0530 | [diff] [blame] | 1994 | #ifdef HAVE_FPM |
| 1995 | /* function to write the fpm config info */ |
| 1996 | static int |
| 1997 | config_write_fpm (struct vty *vty) |
| 1998 | { |
| 1999 | return |
| 2000 | fpm_remote_srv_write (vty); |
| 2001 | } |
| 2002 | |
| 2003 | /* Zebra node */ |
| 2004 | static struct cmd_node zebra_node = |
| 2005 | { |
| 2006 | ZEBRA_NODE, |
| 2007 | "", |
| 2008 | 1 |
| 2009 | }; |
| 2010 | #endif |
| 2011 | |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 2012 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2013 | /* Initialisation of zebra and installation of commands. */ |
| 2014 | void |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 2015 | zebra_init (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2016 | { |
| 2017 | /* Client list init. */ |
paul | b21b19c | 2003-06-15 01:28:29 +0000 | [diff] [blame] | 2018 | zebrad.client_list = list_new (); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2019 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2020 | /* Install configuration write function. */ |
| 2021 | install_node (&table_node, config_write_table); |
| 2022 | install_node (&forwarding_node, config_write_forwarding); |
Udaya Shankara KS | d869dbd | 2016-02-11 21:42:29 +0530 | [diff] [blame] | 2023 | #ifdef HAVE_FPM |
| 2024 | install_node (&zebra_node, config_write_fpm); |
| 2025 | #endif |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2026 | |
| 2027 | install_element (VIEW_NODE, &show_ip_forwarding_cmd); |
hasso | 647e4f1 | 2003-05-25 11:43:52 +0000 | [diff] [blame] | 2028 | install_element (CONFIG_NODE, &ip_forwarding_cmd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2029 | install_element (CONFIG_NODE, &no_ip_forwarding_cmd); |
| 2030 | install_element (ENABLE_NODE, &show_zebra_client_cmd); |
| 2031 | |
| 2032 | #ifdef HAVE_NETLINK |
| 2033 | install_element (VIEW_NODE, &show_table_cmd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2034 | install_element (CONFIG_NODE, &config_table_cmd); |
| 2035 | #endif /* HAVE_NETLINK */ |
| 2036 | |
| 2037 | #ifdef HAVE_IPV6 |
| 2038 | install_element (VIEW_NODE, &show_ipv6_forwarding_cmd); |
hasso | 5590672 | 2004-02-11 22:42:16 +0000 | [diff] [blame] | 2039 | install_element (CONFIG_NODE, &ipv6_forwarding_cmd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2040 | install_element (CONFIG_NODE, &no_ipv6_forwarding_cmd); |
| 2041 | #endif /* HAVE_IPV6 */ |
Paul Jakma | 7514fb7 | 2007-05-02 16:05:35 +0000 | [diff] [blame] | 2042 | |
| 2043 | /* Route-map */ |
| 2044 | zebra_route_map_init (); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2045 | } |
Denis Ovsienko | 97be79f | 2009-07-24 20:45:31 +0400 | [diff] [blame] | 2046 | |
| 2047 | /* Make zebra server socket, wiping any existing one (see bug #403). */ |
| 2048 | void |
Vyacheslav Trushkin | b511468 | 2011-11-25 18:51:48 +0400 | [diff] [blame] | 2049 | zebra_zserv_socket_init (char *path) |
Denis Ovsienko | 97be79f | 2009-07-24 20:45:31 +0400 | [diff] [blame] | 2050 | { |
| 2051 | #ifdef HAVE_TCP_ZEBRA |
| 2052 | zebra_serv (); |
| 2053 | #else |
Vyacheslav Trushkin | b511468 | 2011-11-25 18:51:48 +0400 | [diff] [blame] | 2054 | zebra_serv_un (path ? path : ZEBRA_SERV_PATH); |
Denis Ovsienko | 97be79f | 2009-07-24 20:45:31 +0400 | [diff] [blame] | 2055 | #endif /* HAVE_TCP_ZEBRA */ |
| 2056 | } |