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