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); |
Piotr Chytła | eefddcc | 2015-12-01 09:48:02 -0500 | [diff] [blame] | 497 | /* tag */ |
| 498 | if (rib->tag) |
| 499 | { |
| 500 | SET_FLAG(zapi_flags, ZAPI_MESSAGE_TAG); |
Paul Jakma | 96d1060 | 2016-07-01 14:23:45 +0100 | [diff] [blame^] | 501 | stream_putl (s, rib->tag); |
Piotr Chytła | eefddcc | 2015-12-01 09:48:02 -0500 | [diff] [blame] | 502 | } |
paul | 1dcb517 | 2005-05-31 08:38:50 +0000 | [diff] [blame] | 503 | } |
| 504 | |
| 505 | /* write real message flags value */ |
| 506 | stream_putc_at (s, messmark, zapi_flags); |
| 507 | |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 508 | /* Write next-hop number */ |
| 509 | if (nhnummark) |
hasso | c1eaa44 | 2004-10-19 06:26:01 +0000 | [diff] [blame] | 510 | stream_putc_at (s, nhnummark, nhnum); |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 511 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 512 | /* Write packet size. */ |
| 513 | stream_putw_at (s, 0, stream_get_endp (s)); |
| 514 | |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 515 | return zebra_server_send_message(client); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 516 | } |
| 517 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 518 | #ifdef HAVE_IPV6 |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 519 | static int |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 520 | zsend_ipv6_nexthop_lookup (struct zserv *client, struct in6_addr *addr, |
| 521 | vrf_id_t vrf_id) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 522 | { |
| 523 | struct stream *s; |
| 524 | struct rib *rib; |
| 525 | unsigned long nump; |
| 526 | u_char num; |
| 527 | struct nexthop *nexthop; |
| 528 | |
| 529 | /* Lookup nexthop. */ |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 530 | rib = rib_match_ipv6 (addr, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 531 | |
| 532 | /* Get output stream. */ |
| 533 | s = client->obuf; |
| 534 | stream_reset (s); |
| 535 | |
| 536 | /* Fill in result. */ |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 537 | zserv_create_header (s, ZEBRA_IPV6_NEXTHOP_LOOKUP, vrf_id); |
Hiroshi Yokoi | 8ccd74c | 2015-09-08 11:52:20 +0900 | [diff] [blame] | 538 | stream_put (s, addr, 16); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 539 | |
| 540 | if (rib) |
| 541 | { |
| 542 | stream_putl (s, rib->metric); |
| 543 | num = 0; |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 544 | nump = stream_get_endp(s); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 545 | stream_putc (s, 0); |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 546 | /* Only non-recursive routes are elegible to resolve nexthop we |
| 547 | * are looking up. Therefore, we will just iterate over the top |
| 548 | * chain of nexthops. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 549 | for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next) |
Timo Teräs | 325823a | 2016-01-15 17:36:31 +0200 | [diff] [blame] | 550 | if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 551 | { |
| 552 | stream_putc (s, nexthop->type); |
| 553 | switch (nexthop->type) |
| 554 | { |
| 555 | case ZEBRA_NEXTHOP_IPV6: |
| 556 | stream_put (s, &nexthop->gate.ipv6, 16); |
| 557 | break; |
| 558 | case ZEBRA_NEXTHOP_IPV6_IFINDEX: |
| 559 | case ZEBRA_NEXTHOP_IPV6_IFNAME: |
| 560 | stream_put (s, &nexthop->gate.ipv6, 16); |
| 561 | stream_putl (s, nexthop->ifindex); |
| 562 | break; |
| 563 | case ZEBRA_NEXTHOP_IFINDEX: |
| 564 | case ZEBRA_NEXTHOP_IFNAME: |
| 565 | stream_putl (s, nexthop->ifindex); |
| 566 | break; |
hasso | fa2b17e | 2004-03-04 17:45:00 +0000 | [diff] [blame] | 567 | default: |
| 568 | /* do nothing */ |
| 569 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 570 | } |
| 571 | num++; |
| 572 | } |
| 573 | stream_putc_at (s, nump, num); |
| 574 | } |
| 575 | else |
| 576 | { |
| 577 | stream_putl (s, 0); |
| 578 | stream_putc (s, 0); |
| 579 | } |
| 580 | |
| 581 | stream_putw_at (s, 0, stream_get_endp (s)); |
| 582 | |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 583 | return zebra_server_send_message(client); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 584 | } |
| 585 | #endif /* HAVE_IPV6 */ |
| 586 | |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 587 | static int |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 588 | zsend_ipv4_nexthop_lookup (struct zserv *client, struct in_addr addr, |
| 589 | vrf_id_t vrf_id) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 590 | { |
| 591 | struct stream *s; |
| 592 | struct rib *rib; |
| 593 | unsigned long nump; |
| 594 | u_char num; |
| 595 | struct nexthop *nexthop; |
| 596 | |
David Lamparter | f598cf7 | 2014-11-22 14:44:20 -0800 | [diff] [blame] | 597 | /* Lookup nexthop - eBGP excluded */ |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 598 | rib = rib_match_ipv4_safi (addr, SAFI_UNICAST, 1, NULL, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 599 | |
| 600 | /* Get output stream. */ |
| 601 | s = client->obuf; |
| 602 | stream_reset (s); |
| 603 | |
| 604 | /* Fill in result. */ |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 605 | zserv_create_header (s, ZEBRA_IPV4_NEXTHOP_LOOKUP, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 606 | stream_put_in_addr (s, &addr); |
| 607 | |
| 608 | if (rib) |
| 609 | { |
Christian Franke | bb97e46 | 2013-05-25 14:01:35 +0000 | [diff] [blame] | 610 | if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV) |
| 611 | zlog_debug("%s: Matching rib entry found.", __func__); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 612 | stream_putl (s, rib->metric); |
| 613 | num = 0; |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 614 | nump = stream_get_endp(s); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 615 | stream_putc (s, 0); |
Christian Franke | fa713d9 | 2013-07-05 15:35:37 +0000 | [diff] [blame] | 616 | /* Only non-recursive routes are elegible to resolve the nexthop we |
| 617 | * are looking up. Therefore, we will just iterate over the top |
| 618 | * chain of nexthops. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 619 | for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next) |
Timo Teräs | 325823a | 2016-01-15 17:36:31 +0200 | [diff] [blame] | 620 | if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 621 | { |
| 622 | stream_putc (s, nexthop->type); |
| 623 | switch (nexthop->type) |
| 624 | { |
| 625 | case ZEBRA_NEXTHOP_IPV4: |
| 626 | stream_put_in_addr (s, &nexthop->gate.ipv4); |
| 627 | break; |
Christian Franke | bb97e46 | 2013-05-25 14:01:35 +0000 | [diff] [blame] | 628 | case ZEBRA_NEXTHOP_IPV4_IFINDEX: |
| 629 | stream_put_in_addr (s, &nexthop->gate.ipv4); |
| 630 | stream_putl (s, nexthop->ifindex); |
| 631 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 632 | case ZEBRA_NEXTHOP_IFINDEX: |
| 633 | case ZEBRA_NEXTHOP_IFNAME: |
| 634 | stream_putl (s, nexthop->ifindex); |
| 635 | break; |
hasso | fa2b17e | 2004-03-04 17:45:00 +0000 | [diff] [blame] | 636 | default: |
| 637 | /* do nothing */ |
| 638 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 639 | } |
| 640 | num++; |
| 641 | } |
| 642 | stream_putc_at (s, nump, num); |
| 643 | } |
| 644 | else |
| 645 | { |
Christian Franke | bb97e46 | 2013-05-25 14:01:35 +0000 | [diff] [blame] | 646 | if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV) |
| 647 | zlog_debug("%s: No matching rib entry found.", __func__); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 648 | stream_putl (s, 0); |
| 649 | stream_putc (s, 0); |
| 650 | } |
| 651 | |
| 652 | stream_putw_at (s, 0, stream_get_endp (s)); |
| 653 | |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 654 | return zebra_server_send_message(client); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 655 | } |
| 656 | |
Everton Marques | 4e5275b | 2014-07-01 15:15:52 -0300 | [diff] [blame] | 657 | /* |
| 658 | Modified version of zsend_ipv4_nexthop_lookup(): |
| 659 | Query unicast rib if nexthop is not found on mrib. |
| 660 | Returns both route metric and protocol distance. |
| 661 | */ |
| 662 | static int |
David Lamparter | bd07812 | 2015-01-06 19:53:24 +0100 | [diff] [blame] | 663 | zsend_ipv4_nexthop_lookup_mrib (struct zserv *client, struct in_addr addr, |
| 664 | struct rib *rib) |
Everton Marques | 4e5275b | 2014-07-01 15:15:52 -0300 | [diff] [blame] | 665 | { |
| 666 | struct stream *s; |
Everton Marques | 4e5275b | 2014-07-01 15:15:52 -0300 | [diff] [blame] | 667 | unsigned long nump; |
| 668 | u_char num; |
| 669 | struct nexthop *nexthop; |
Everton Marques | 4e5275b | 2014-07-01 15:15:52 -0300 | [diff] [blame] | 670 | |
| 671 | /* Get output stream. */ |
| 672 | s = client->obuf; |
| 673 | stream_reset (s); |
| 674 | |
| 675 | /* Fill in result. */ |
Jafar Al-Gharaibeh | 190591f | 2016-04-21 17:40:12 -0500 | [diff] [blame] | 676 | zserv_create_header (s, ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB, |
| 677 | rib ? rib->vrf_id : VRF_DEFAULT); |
Everton Marques | 4e5275b | 2014-07-01 15:15:52 -0300 | [diff] [blame] | 678 | stream_put_in_addr (s, &addr); |
| 679 | |
| 680 | if (rib) |
| 681 | { |
| 682 | stream_putc (s, rib->distance); |
| 683 | stream_putl (s, rib->metric); |
| 684 | num = 0; |
| 685 | nump = stream_get_endp(s); /* remember position for nexthop_num */ |
| 686 | stream_putc (s, 0); /* reserve room for nexthop_num */ |
| 687 | /* Only non-recursive routes are elegible to resolve the nexthop we |
| 688 | * are looking up. Therefore, we will just iterate over the top |
| 689 | * chain of nexthops. */ |
| 690 | for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next) |
Timo Teräs | 325823a | 2016-01-15 17:36:31 +0200 | [diff] [blame] | 691 | if (CHECK_FLAG (nexthop->flags, NEXTHOP_FLAG_ACTIVE)) |
Everton Marques | 4e5275b | 2014-07-01 15:15:52 -0300 | [diff] [blame] | 692 | { |
| 693 | stream_putc (s, nexthop->type); |
| 694 | switch (nexthop->type) |
| 695 | { |
| 696 | case ZEBRA_NEXTHOP_IPV4: |
| 697 | stream_put_in_addr (s, &nexthop->gate.ipv4); |
| 698 | break; |
| 699 | case ZEBRA_NEXTHOP_IPV4_IFINDEX: |
| 700 | stream_put_in_addr (s, &nexthop->gate.ipv4); |
| 701 | stream_putl (s, nexthop->ifindex); |
| 702 | break; |
| 703 | case ZEBRA_NEXTHOP_IFINDEX: |
| 704 | case ZEBRA_NEXTHOP_IFNAME: |
| 705 | stream_putl (s, nexthop->ifindex); |
| 706 | break; |
| 707 | default: |
| 708 | /* do nothing */ |
| 709 | break; |
| 710 | } |
| 711 | num++; |
| 712 | } |
| 713 | |
| 714 | stream_putc_at (s, nump, num); /* store nexthop_num */ |
| 715 | } |
| 716 | else |
| 717 | { |
| 718 | stream_putc (s, 0); /* distance */ |
| 719 | stream_putl (s, 0); /* metric */ |
| 720 | stream_putc (s, 0); /* nexthop_num */ |
| 721 | } |
| 722 | |
| 723 | stream_putw_at (s, 0, stream_get_endp (s)); |
| 724 | |
| 725 | return zebra_server_send_message(client); |
| 726 | } |
| 727 | |
Pradosh Mohapatra | 60cc959 | 2015-11-09 20:21:41 -0500 | [diff] [blame] | 728 | /* Nexthop register */ |
| 729 | static int |
| 730 | zserv_nexthop_register (struct zserv *client, int sock, u_short length, vrf_id_t vrf_id) |
| 731 | { |
| 732 | struct rnh *rnh; |
| 733 | struct stream *s; |
| 734 | struct prefix p; |
| 735 | u_short l = 0; |
| 736 | |
| 737 | if (IS_ZEBRA_DEBUG_NHT) |
| 738 | zlog_debug("nexthop_register msg from client %s: length=%d\n", |
| 739 | zebra_route_string(client->proto), length); |
| 740 | |
| 741 | s = client->ibuf; |
| 742 | |
| 743 | while (l < length) |
| 744 | { |
| 745 | p.family = stream_getw(s); |
| 746 | p.prefixlen = stream_getc(s); |
| 747 | l += 3; |
| 748 | stream_get(&p.u.prefix, s, PSIZE(p.prefixlen)); |
| 749 | l += PSIZE(p.prefixlen); |
| 750 | rnh = zebra_add_rnh(&p, 0); |
| 751 | zebra_add_rnh_client(rnh, client, vrf_id); |
| 752 | } |
| 753 | zebra_evaluate_rnh_table(0, AF_INET); |
| 754 | zebra_evaluate_rnh_table(0, AF_INET6); |
| 755 | return 0; |
| 756 | } |
| 757 | |
| 758 | /* Nexthop register */ |
| 759 | static int |
| 760 | zserv_nexthop_unregister (struct zserv *client, int sock, u_short length) |
| 761 | { |
| 762 | struct rnh *rnh; |
| 763 | struct stream *s; |
| 764 | struct prefix p; |
| 765 | u_short l = 0; |
| 766 | |
| 767 | if (IS_ZEBRA_DEBUG_NHT) |
| 768 | zlog_debug("nexthop_unregister msg from client %s: length=%d\n", |
| 769 | zebra_route_string(client->proto), length); |
| 770 | |
| 771 | s = client->ibuf; |
| 772 | |
| 773 | while (l < length) |
| 774 | { |
| 775 | p.family = stream_getw(s); |
| 776 | p.prefixlen = stream_getc(s); |
| 777 | l += 3; |
| 778 | stream_get(&p.u.prefix, s, PSIZE(p.prefixlen)); |
| 779 | l += PSIZE(p.prefixlen); |
| 780 | rnh = zebra_lookup_rnh(&p, 0); |
| 781 | if (rnh) |
| 782 | zebra_remove_rnh_client(rnh, client); |
| 783 | } |
| 784 | return 0; |
| 785 | } |
| 786 | |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 787 | static int |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 788 | zsend_ipv4_import_lookup (struct zserv *client, struct prefix_ipv4 *p, |
| 789 | vrf_id_t vrf_id) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 790 | { |
| 791 | struct stream *s; |
| 792 | struct rib *rib; |
| 793 | unsigned long nump; |
| 794 | u_char num; |
| 795 | struct nexthop *nexthop; |
| 796 | |
| 797 | /* Lookup nexthop. */ |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 798 | rib = rib_lookup_ipv4 (p, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 799 | |
| 800 | /* Get output stream. */ |
| 801 | s = client->obuf; |
| 802 | stream_reset (s); |
| 803 | |
| 804 | /* Fill in result. */ |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 805 | zserv_create_header (s, ZEBRA_IPV4_IMPORT_LOOKUP, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 806 | stream_put_in_addr (s, &p->prefix); |
| 807 | |
| 808 | if (rib) |
| 809 | { |
| 810 | stream_putl (s, rib->metric); |
| 811 | num = 0; |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 812 | nump = stream_get_endp(s); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 813 | stream_putc (s, 0); |
| 814 | for (nexthop = rib->nexthop; nexthop; nexthop = nexthop->next) |
Timo Teräs | 325823a | 2016-01-15 17:36:31 +0200 | [diff] [blame] | 815 | if (CHECK_FLAG(nexthop->flags, NEXTHOP_FLAG_ACTIVE)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 816 | { |
| 817 | stream_putc (s, nexthop->type); |
| 818 | switch (nexthop->type) |
| 819 | { |
| 820 | case ZEBRA_NEXTHOP_IPV4: |
| 821 | stream_put_in_addr (s, &nexthop->gate.ipv4); |
| 822 | break; |
Christian Franke | a12afd5 | 2013-05-25 14:01:36 +0000 | [diff] [blame] | 823 | case ZEBRA_NEXTHOP_IPV4_IFINDEX: |
| 824 | stream_put_in_addr (s, &nexthop->gate.ipv4); |
| 825 | stream_putl (s, nexthop->ifindex); |
| 826 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 827 | case ZEBRA_NEXTHOP_IFINDEX: |
| 828 | case ZEBRA_NEXTHOP_IFNAME: |
| 829 | stream_putl (s, nexthop->ifindex); |
| 830 | break; |
hasso | fa2b17e | 2004-03-04 17:45:00 +0000 | [diff] [blame] | 831 | default: |
| 832 | /* do nothing */ |
| 833 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 834 | } |
| 835 | num++; |
| 836 | } |
| 837 | stream_putc_at (s, nump, num); |
| 838 | } |
| 839 | else |
| 840 | { |
| 841 | stream_putl (s, 0); |
| 842 | stream_putc (s, 0); |
| 843 | } |
| 844 | |
| 845 | stream_putw_at (s, 0, stream_get_endp (s)); |
| 846 | |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 847 | return zebra_server_send_message(client); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 848 | } |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 849 | |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 850 | /* Router-id is updated. Send ZEBRA_ROUTER_ID_ADD to client. */ |
| 851 | int |
Feng Lu | ac19a44 | 2015-05-22 11:40:07 +0200 | [diff] [blame] | 852 | zsend_router_id_update (struct zserv *client, struct prefix *p, |
| 853 | vrf_id_t vrf_id) |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 854 | { |
| 855 | struct stream *s; |
| 856 | int blen; |
| 857 | |
| 858 | /* Check this client need interface information. */ |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 859 | if (! vrf_bitmap_check (client->ridinfo, vrf_id)) |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 860 | return 0; |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 861 | |
| 862 | s = client->obuf; |
| 863 | stream_reset (s); |
| 864 | |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 865 | /* Message type. */ |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 866 | zserv_create_header (s, ZEBRA_ROUTER_ID_UPDATE, vrf_id); |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 867 | |
| 868 | /* Prefix information. */ |
| 869 | stream_putc (s, p->family); |
| 870 | blen = prefix_blen (p); |
| 871 | stream_put (s, &p->u.prefix, blen); |
| 872 | stream_putc (s, p->prefixlen); |
| 873 | |
| 874 | /* Write packet size. */ |
| 875 | stream_putw_at (s, 0, stream_get_endp (s)); |
| 876 | |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 877 | return zebra_server_send_message(client); |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 878 | } |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 879 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 880 | /* Register zebra server interface information. Send current all |
| 881 | interface and address information. */ |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 882 | static int |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 883 | 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] | 884 | { |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 885 | struct listnode *ifnode, *ifnnode; |
| 886 | struct listnode *cnode, *cnnode; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 887 | struct interface *ifp; |
| 888 | struct connected *c; |
| 889 | |
| 890 | /* Interface information is needed. */ |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 891 | vrf_bitmap_set (client->ifinfo, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 892 | |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 893 | for (ALL_LIST_ELEMENTS (vrf_iflist (vrf_id), ifnode, ifnnode, ifp)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 894 | { |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 895 | /* Skip pseudo interface. */ |
| 896 | if (! CHECK_FLAG (ifp->status, ZEBRA_INTERFACE_ACTIVE)) |
| 897 | continue; |
| 898 | |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 899 | if (zsend_interface_add (client, ifp) < 0) |
| 900 | return -1; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 901 | |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 902 | for (ALL_LIST_ELEMENTS (ifp->connected, cnode, cnnode, c)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 903 | { |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 904 | if (CHECK_FLAG (c->conf, ZEBRA_IFC_REAL) && |
| 905 | (zsend_interface_address (ZEBRA_INTERFACE_ADDRESS_ADD, client, |
| 906 | ifp, c) < 0)) |
| 907 | return -1; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 908 | } |
| 909 | } |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 910 | return 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 911 | } |
| 912 | |
| 913 | /* Unregister zebra server interface information. */ |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 914 | static int |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 915 | 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] | 916 | { |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 917 | vrf_bitmap_unset (client->ifinfo, vrf_id); |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 918 | return 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 919 | } |
| 920 | |
| 921 | /* This function support multiple nexthop. */ |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 922 | /* |
| 923 | * Parse the ZEBRA_IPV4_ROUTE_ADD sent from client. Update rib and |
| 924 | * add kernel route. |
| 925 | */ |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 926 | static int |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 927 | 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] | 928 | { |
| 929 | int i; |
| 930 | struct rib *rib; |
| 931 | struct prefix_ipv4 p; |
| 932 | u_char message; |
| 933 | struct in_addr nexthop; |
| 934 | u_char nexthop_num; |
| 935 | u_char nexthop_type; |
| 936 | struct stream *s; |
Paul Jakma | 9099f9b | 2016-01-18 10:12:10 +0000 | [diff] [blame] | 937 | ifindex_t ifindex; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 938 | u_char ifname_len; |
G.Balaji | cddf391 | 2011-11-26 21:59:32 +0400 | [diff] [blame] | 939 | safi_t safi; |
| 940 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 941 | |
| 942 | /* Get input stream. */ |
| 943 | s = client->ibuf; |
| 944 | |
| 945 | /* Allocate new rib. */ |
paul | 4d38fdb | 2005-04-28 17:35:14 +0000 | [diff] [blame] | 946 | rib = XCALLOC (MTYPE_RIB, sizeof (struct rib)); |
| 947 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 948 | /* Type, flags, message. */ |
| 949 | rib->type = stream_getc (s); |
| 950 | rib->flags = stream_getc (s); |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 951 | message = stream_getc (s); |
G.Balaji | cddf391 | 2011-11-26 21:59:32 +0400 | [diff] [blame] | 952 | safi = stream_getw (s); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 953 | rib->uptime = time (NULL); |
| 954 | |
| 955 | /* IPv4 prefix. */ |
| 956 | memset (&p, 0, sizeof (struct prefix_ipv4)); |
| 957 | p.family = AF_INET; |
| 958 | p.prefixlen = stream_getc (s); |
| 959 | stream_get (&p.prefix, s, PSIZE (p.prefixlen)); |
| 960 | |
Feng Lu | 0d0686f | 2015-05-22 11:40:02 +0200 | [diff] [blame] | 961 | /* VRF ID */ |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 962 | rib->vrf_id = vrf_id; |
Feng Lu | 0d0686f | 2015-05-22 11:40:02 +0200 | [diff] [blame] | 963 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 964 | /* Nexthop parse. */ |
| 965 | if (CHECK_FLAG (message, ZAPI_MESSAGE_NEXTHOP)) |
| 966 | { |
| 967 | nexthop_num = stream_getc (s); |
| 968 | |
| 969 | for (i = 0; i < nexthop_num; i++) |
| 970 | { |
| 971 | nexthop_type = stream_getc (s); |
| 972 | |
| 973 | switch (nexthop_type) |
| 974 | { |
| 975 | case ZEBRA_NEXTHOP_IFINDEX: |
| 976 | ifindex = stream_getl (s); |
Pradosh Mohapatra | 60cc959 | 2015-11-09 20:21:41 -0500 | [diff] [blame] | 977 | rib_nexthop_ifindex_add (rib, ifindex); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 978 | break; |
| 979 | case ZEBRA_NEXTHOP_IFNAME: |
| 980 | ifname_len = stream_getc (s); |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 981 | stream_forward_getp (s, ifname_len); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 982 | break; |
| 983 | case ZEBRA_NEXTHOP_IPV4: |
| 984 | nexthop.s_addr = stream_get_ipv4 (s); |
Pradosh Mohapatra | 60cc959 | 2015-11-09 20:21:41 -0500 | [diff] [blame] | 985 | rib_nexthop_ipv4_add (rib, &nexthop, NULL); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 986 | break; |
Joakim Tjernlund | c963c20 | 2012-07-07 17:06:13 +0200 | [diff] [blame] | 987 | case ZEBRA_NEXTHOP_IPV4_IFINDEX: |
| 988 | nexthop.s_addr = stream_get_ipv4 (s); |
| 989 | ifindex = stream_getl (s); |
Pradosh Mohapatra | 60cc959 | 2015-11-09 20:21:41 -0500 | [diff] [blame] | 990 | rib_nexthop_ipv4_ifindex_add (rib, &nexthop, NULL, ifindex); |
Joakim Tjernlund | c963c20 | 2012-07-07 17:06:13 +0200 | [diff] [blame] | 991 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 992 | case ZEBRA_NEXTHOP_IPV6: |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 993 | stream_forward_getp (s, IPV6_MAX_BYTELEN); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 994 | break; |
Subbaiah Venkata | 6902c69 | 2012-03-27 19:21:29 -0700 | [diff] [blame] | 995 | case ZEBRA_NEXTHOP_BLACKHOLE: |
Pradosh Mohapatra | 60cc959 | 2015-11-09 20:21:41 -0500 | [diff] [blame] | 996 | rib_nexthop_blackhole_add (rib); |
Subbaiah Venkata | 6902c69 | 2012-03-27 19:21:29 -0700 | [diff] [blame] | 997 | break; |
| 998 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 999 | } |
| 1000 | } |
| 1001 | |
| 1002 | /* Distance. */ |
| 1003 | if (CHECK_FLAG (message, ZAPI_MESSAGE_DISTANCE)) |
| 1004 | rib->distance = stream_getc (s); |
| 1005 | |
| 1006 | /* Metric. */ |
| 1007 | if (CHECK_FLAG (message, ZAPI_MESSAGE_METRIC)) |
| 1008 | rib->metric = stream_getl (s); |
| 1009 | |
Timo Teräs | b11f3b5 | 2015-11-02 16:50:07 +0200 | [diff] [blame] | 1010 | if (CHECK_FLAG (message, ZAPI_MESSAGE_MTU)) |
| 1011 | rib->mtu = stream_getl (s); |
Piotr Chytła | eefddcc | 2015-12-01 09:48:02 -0500 | [diff] [blame] | 1012 | /* Tag */ |
| 1013 | if (CHECK_FLAG (message, ZAPI_MESSAGE_TAG)) |
Paul Jakma | 96d1060 | 2016-07-01 14:23:45 +0100 | [diff] [blame^] | 1014 | rib->tag = stream_getl (s); |
Timo Teräs | b11f3b5 | 2015-11-02 16:50:07 +0200 | [diff] [blame] | 1015 | |
Paul Jakma | 171eee3 | 2006-07-27 16:11:02 +0000 | [diff] [blame] | 1016 | /* Table */ |
| 1017 | rib->table=zebrad.rtm_table_default; |
G.Balaji | cddf391 | 2011-11-26 21:59:32 +0400 | [diff] [blame] | 1018 | rib_add_ipv4_multipath (&p, rib, safi); |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1019 | return 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1020 | } |
| 1021 | |
| 1022 | /* Zebra server IPv4 prefix delete function. */ |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1023 | static int |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1024 | 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] | 1025 | { |
| 1026 | int i; |
| 1027 | struct stream *s; |
| 1028 | struct zapi_ipv4 api; |
Subbaiah Venkata | 6902c69 | 2012-03-27 19:21:29 -0700 | [diff] [blame] | 1029 | struct in_addr nexthop, *nexthop_p; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1030 | unsigned long ifindex; |
| 1031 | struct prefix_ipv4 p; |
| 1032 | u_char nexthop_num; |
| 1033 | u_char nexthop_type; |
| 1034 | u_char ifname_len; |
| 1035 | |
| 1036 | s = client->ibuf; |
| 1037 | ifindex = 0; |
| 1038 | nexthop.s_addr = 0; |
Subbaiah Venkata | 6902c69 | 2012-03-27 19:21:29 -0700 | [diff] [blame] | 1039 | nexthop_p = NULL; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1040 | |
| 1041 | /* Type, flags, message. */ |
| 1042 | api.type = stream_getc (s); |
| 1043 | api.flags = stream_getc (s); |
| 1044 | api.message = stream_getc (s); |
G.Balaji | cddf391 | 2011-11-26 21:59:32 +0400 | [diff] [blame] | 1045 | api.safi = stream_getw (s); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1046 | |
| 1047 | /* IPv4 prefix. */ |
| 1048 | memset (&p, 0, sizeof (struct prefix_ipv4)); |
| 1049 | p.family = AF_INET; |
| 1050 | p.prefixlen = stream_getc (s); |
| 1051 | stream_get (&p.prefix, s, PSIZE (p.prefixlen)); |
| 1052 | |
| 1053 | /* Nexthop, ifindex, distance, metric. */ |
| 1054 | if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP)) |
| 1055 | { |
| 1056 | nexthop_num = stream_getc (s); |
| 1057 | |
| 1058 | for (i = 0; i < nexthop_num; i++) |
| 1059 | { |
| 1060 | nexthop_type = stream_getc (s); |
| 1061 | |
| 1062 | switch (nexthop_type) |
| 1063 | { |
| 1064 | case ZEBRA_NEXTHOP_IFINDEX: |
| 1065 | ifindex = stream_getl (s); |
| 1066 | break; |
| 1067 | case ZEBRA_NEXTHOP_IFNAME: |
| 1068 | ifname_len = stream_getc (s); |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 1069 | stream_forward_getp (s, ifname_len); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1070 | break; |
| 1071 | case ZEBRA_NEXTHOP_IPV4: |
| 1072 | nexthop.s_addr = stream_get_ipv4 (s); |
Subbaiah Venkata | 6902c69 | 2012-03-27 19:21:29 -0700 | [diff] [blame] | 1073 | nexthop_p = &nexthop; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1074 | break; |
Joakim Tjernlund | c963c20 | 2012-07-07 17:06:13 +0200 | [diff] [blame] | 1075 | case ZEBRA_NEXTHOP_IPV4_IFINDEX: |
| 1076 | nexthop.s_addr = stream_get_ipv4 (s); |
Christian Franke | 23f5f7c | 2013-11-27 17:06:14 +0000 | [diff] [blame] | 1077 | nexthop_p = &nexthop; |
Joakim Tjernlund | c963c20 | 2012-07-07 17:06:13 +0200 | [diff] [blame] | 1078 | ifindex = stream_getl (s); |
| 1079 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1080 | case ZEBRA_NEXTHOP_IPV6: |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 1081 | stream_forward_getp (s, IPV6_MAX_BYTELEN); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1082 | break; |
| 1083 | } |
| 1084 | } |
| 1085 | } |
| 1086 | |
| 1087 | /* Distance. */ |
| 1088 | if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE)) |
| 1089 | api.distance = stream_getc (s); |
| 1090 | else |
| 1091 | api.distance = 0; |
| 1092 | |
| 1093 | /* Metric. */ |
| 1094 | if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC)) |
| 1095 | api.metric = stream_getl (s); |
| 1096 | else |
| 1097 | api.metric = 0; |
| 1098 | |
Piotr Chytła | eefddcc | 2015-12-01 09:48:02 -0500 | [diff] [blame] | 1099 | /* tag */ |
| 1100 | if (CHECK_FLAG (api.message, ZAPI_MESSAGE_TAG)) |
Paul Jakma | 96d1060 | 2016-07-01 14:23:45 +0100 | [diff] [blame^] | 1101 | api.tag = stream_getl (s); |
Piotr Chytła | eefddcc | 2015-12-01 09:48:02 -0500 | [diff] [blame] | 1102 | else |
| 1103 | api.tag = 0; |
| 1104 | |
Subbaiah Venkata | 6902c69 | 2012-03-27 19:21:29 -0700 | [diff] [blame] | 1105 | rib_delete_ipv4 (api.type, api.flags, &p, nexthop_p, ifindex, |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1106 | vrf_id, api.safi); |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1107 | return 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1108 | } |
| 1109 | |
| 1110 | /* Nexthop lookup for IPv4. */ |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1111 | static int |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1112 | zread_ipv4_nexthop_lookup (struct zserv *client, u_short length, |
| 1113 | vrf_id_t vrf_id) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1114 | { |
| 1115 | struct in_addr addr; |
Christian Franke | bb97e46 | 2013-05-25 14:01:35 +0000 | [diff] [blame] | 1116 | char buf[BUFSIZ]; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1117 | |
| 1118 | addr.s_addr = stream_get_ipv4 (client->ibuf); |
Christian Franke | bb97e46 | 2013-05-25 14:01:35 +0000 | [diff] [blame] | 1119 | if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV) |
| 1120 | zlog_debug("%s: looking up %s", __func__, |
| 1121 | inet_ntop (AF_INET, &addr, buf, BUFSIZ)); |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1122 | return zsend_ipv4_nexthop_lookup (client, addr, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1123 | } |
| 1124 | |
Everton Marques | 4e5275b | 2014-07-01 15:15:52 -0300 | [diff] [blame] | 1125 | /* MRIB Nexthop lookup for IPv4. */ |
| 1126 | static int |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1127 | zread_ipv4_nexthop_lookup_mrib (struct zserv *client, u_short length, |
| 1128 | vrf_id_t vrf_id) |
Everton Marques | 4e5275b | 2014-07-01 15:15:52 -0300 | [diff] [blame] | 1129 | { |
| 1130 | struct in_addr addr; |
David Lamparter | bd07812 | 2015-01-06 19:53:24 +0100 | [diff] [blame] | 1131 | struct rib *rib; |
Everton Marques | 4e5275b | 2014-07-01 15:15:52 -0300 | [diff] [blame] | 1132 | |
| 1133 | addr.s_addr = stream_get_ipv4 (client->ibuf); |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1134 | rib = rib_match_ipv4_multicast (addr, NULL, vrf_id); |
David Lamparter | bd07812 | 2015-01-06 19:53:24 +0100 | [diff] [blame] | 1135 | return zsend_ipv4_nexthop_lookup_mrib (client, addr, rib); |
Everton Marques | 4e5275b | 2014-07-01 15:15:52 -0300 | [diff] [blame] | 1136 | } |
| 1137 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1138 | /* Nexthop lookup for IPv4. */ |
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_ipv4_import_lookup (struct zserv *client, u_short length, |
| 1141 | vrf_id_t vrf_id) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1142 | { |
| 1143 | struct prefix_ipv4 p; |
| 1144 | |
| 1145 | p.family = AF_INET; |
| 1146 | p.prefixlen = stream_getc (client->ibuf); |
| 1147 | p.prefix.s_addr = stream_get_ipv4 (client->ibuf); |
| 1148 | |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1149 | return zsend_ipv4_import_lookup (client, &p, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1150 | } |
| 1151 | |
| 1152 | #ifdef HAVE_IPV6 |
| 1153 | /* Zebra server IPv6 prefix add function. */ |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1154 | static int |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1155 | 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] | 1156 | { |
| 1157 | int i; |
| 1158 | struct stream *s; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1159 | struct in6_addr nexthop; |
Ayan Banerjee | 34c5d89 | 2015-11-09 20:14:53 -0500 | [diff] [blame] | 1160 | struct rib *rib; |
| 1161 | u_char message; |
| 1162 | u_char gateway_num; |
| 1163 | u_char nexthop_type; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1164 | struct prefix_ipv6 p; |
Ayan Banerjee | 34c5d89 | 2015-11-09 20:14:53 -0500 | [diff] [blame] | 1165 | safi_t safi; |
| 1166 | static struct in6_addr nexthops[MULTIPATH_NUM]; |
| 1167 | static unsigned int ifindices[MULTIPATH_NUM]; |
| 1168 | |
| 1169 | /* Get input stream. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1170 | s = client->ibuf; |
Ayan Banerjee | 34c5d89 | 2015-11-09 20:14:53 -0500 | [diff] [blame] | 1171 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1172 | memset (&nexthop, 0, sizeof (struct in6_addr)); |
| 1173 | |
Ayan Banerjee | 34c5d89 | 2015-11-09 20:14:53 -0500 | [diff] [blame] | 1174 | /* Allocate new rib. */ |
| 1175 | rib = XCALLOC (MTYPE_RIB, sizeof (struct rib)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1176 | |
Ayan Banerjee | 34c5d89 | 2015-11-09 20:14:53 -0500 | [diff] [blame] | 1177 | /* Type, flags, message. */ |
| 1178 | rib->type = stream_getc (s); |
| 1179 | rib->flags = stream_getc (s); |
| 1180 | message = stream_getc (s); |
| 1181 | safi = stream_getw (s); |
| 1182 | rib->uptime = time (NULL); |
| 1183 | |
| 1184 | /* IPv6 prefix. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1185 | memset (&p, 0, sizeof (struct prefix_ipv6)); |
| 1186 | p.family = AF_INET6; |
| 1187 | p.prefixlen = stream_getc (s); |
| 1188 | stream_get (&p.prefix, s, PSIZE (p.prefixlen)); |
| 1189 | |
Ayan Banerjee | 34c5d89 | 2015-11-09 20:14:53 -0500 | [diff] [blame] | 1190 | /* We need to give nh-addr, nh-ifindex with the same next-hop object |
| 1191 | * to the rib to ensure that IPv6 multipathing works; need to coalesce |
| 1192 | * these. Clients should send the same number of paired set of |
| 1193 | * next-hop-addr/next-hop-ifindices. */ |
| 1194 | if (CHECK_FLAG (message, ZAPI_MESSAGE_NEXTHOP)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1195 | { |
Ayan Banerjee | 34c5d89 | 2015-11-09 20:14:53 -0500 | [diff] [blame] | 1196 | int nh_count = 0; |
| 1197 | int if_count = 0; |
| 1198 | int max_nh_if = 0; |
| 1199 | unsigned int ifindex; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1200 | |
Ayan Banerjee | 34c5d89 | 2015-11-09 20:14:53 -0500 | [diff] [blame] | 1201 | gateway_num = stream_getc (s); |
| 1202 | for (i = 0; i < gateway_num; i++) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1203 | { |
| 1204 | nexthop_type = stream_getc (s); |
| 1205 | |
| 1206 | switch (nexthop_type) |
| 1207 | { |
| 1208 | case ZEBRA_NEXTHOP_IPV6: |
| 1209 | stream_get (&nexthop, s, 16); |
Ayan Banerjee | 34c5d89 | 2015-11-09 20:14:53 -0500 | [diff] [blame] | 1210 | if (nh_count < MULTIPATH_NUM) { |
| 1211 | nexthops[nh_count++] = nexthop; |
| 1212 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1213 | break; |
| 1214 | case ZEBRA_NEXTHOP_IFINDEX: |
| 1215 | ifindex = stream_getl (s); |
Ayan Banerjee | 34c5d89 | 2015-11-09 20:14:53 -0500 | [diff] [blame] | 1216 | if (if_count < MULTIPATH_NUM) { |
| 1217 | ifindices[if_count++] = ifindex; |
| 1218 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1219 | break; |
| 1220 | } |
| 1221 | } |
Ayan Banerjee | 34c5d89 | 2015-11-09 20:14:53 -0500 | [diff] [blame] | 1222 | |
| 1223 | max_nh_if = (nh_count > if_count) ? nh_count : if_count; |
| 1224 | for (i = 0; i < max_nh_if; i++) |
| 1225 | { |
| 1226 | if ((i < nh_count) && !IN6_IS_ADDR_UNSPECIFIED (&nexthops[i])) |
| 1227 | { |
| 1228 | if ((i < if_count) && ifindices[i]) |
Pradosh Mohapatra | 60cc959 | 2015-11-09 20:21:41 -0500 | [diff] [blame] | 1229 | rib_nexthop_ipv6_ifindex_add (rib, &nexthops[i], ifindices[i]); |
Ayan Banerjee | 34c5d89 | 2015-11-09 20:14:53 -0500 | [diff] [blame] | 1230 | else |
Pradosh Mohapatra | 60cc959 | 2015-11-09 20:21:41 -0500 | [diff] [blame] | 1231 | rib_nexthop_ipv6_add (rib, &nexthops[i]); |
Ayan Banerjee | 34c5d89 | 2015-11-09 20:14:53 -0500 | [diff] [blame] | 1232 | } |
| 1233 | else |
| 1234 | { |
| 1235 | if ((i < if_count) && ifindices[i]) |
Pradosh Mohapatra | 60cc959 | 2015-11-09 20:21:41 -0500 | [diff] [blame] | 1236 | rib_nexthop_ifindex_add (rib, ifindices[i]); |
Ayan Banerjee | 34c5d89 | 2015-11-09 20:14:53 -0500 | [diff] [blame] | 1237 | } |
| 1238 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1239 | } |
| 1240 | |
Ayan Banerjee | 34c5d89 | 2015-11-09 20:14:53 -0500 | [diff] [blame] | 1241 | /* Distance. */ |
| 1242 | if (CHECK_FLAG (message, ZAPI_MESSAGE_DISTANCE)) |
| 1243 | rib->distance = stream_getc (s); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1244 | |
Ayan Banerjee | 34c5d89 | 2015-11-09 20:14:53 -0500 | [diff] [blame] | 1245 | /* Metric. */ |
| 1246 | if (CHECK_FLAG (message, ZAPI_MESSAGE_METRIC)) |
| 1247 | rib->metric = stream_getl (s); |
Timo Teräs | b11f3b5 | 2015-11-02 16:50:07 +0200 | [diff] [blame] | 1248 | |
Ayan Banerjee | 34c5d89 | 2015-11-09 20:14:53 -0500 | [diff] [blame] | 1249 | if (CHECK_FLAG (message, ZAPI_MESSAGE_MTU)) |
| 1250 | rib->mtu = stream_getl (s); |
| 1251 | |
Piotr Chytła | eefddcc | 2015-12-01 09:48:02 -0500 | [diff] [blame] | 1252 | /* Tag */ |
| 1253 | if (CHECK_FLAG (message, ZAPI_MESSAGE_TAG)) |
Paul Jakma | 96d1060 | 2016-07-01 14:23:45 +0100 | [diff] [blame^] | 1254 | rib->tag = stream_getl (s); |
Piotr Chytła | eefddcc | 2015-12-01 09:48:02 -0500 | [diff] [blame] | 1255 | |
Ayan Banerjee | 34c5d89 | 2015-11-09 20:14:53 -0500 | [diff] [blame] | 1256 | /* Table */ |
| 1257 | rib->table=zebrad.rtm_table_default; |
| 1258 | rib_add_ipv6_multipath (&p, rib, safi); |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1259 | return 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1260 | } |
| 1261 | |
| 1262 | /* Zebra server IPv6 prefix delete function. */ |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1263 | static int |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1264 | 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] | 1265 | { |
| 1266 | int i; |
| 1267 | struct stream *s; |
| 1268 | struct zapi_ipv6 api; |
| 1269 | struct in6_addr nexthop; |
| 1270 | unsigned long ifindex; |
| 1271 | struct prefix_ipv6 p; |
| 1272 | |
| 1273 | s = client->ibuf; |
| 1274 | ifindex = 0; |
| 1275 | memset (&nexthop, 0, sizeof (struct in6_addr)); |
| 1276 | |
| 1277 | /* Type, flags, message. */ |
| 1278 | api.type = stream_getc (s); |
| 1279 | api.flags = stream_getc (s); |
| 1280 | api.message = stream_getc (s); |
G.Balaji | f768f36 | 2011-11-26 22:10:39 +0400 | [diff] [blame] | 1281 | api.safi = stream_getw (s); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1282 | |
| 1283 | /* IPv4 prefix. */ |
| 1284 | memset (&p, 0, sizeof (struct prefix_ipv6)); |
| 1285 | p.family = AF_INET6; |
| 1286 | p.prefixlen = stream_getc (s); |
| 1287 | stream_get (&p.prefix, s, PSIZE (p.prefixlen)); |
| 1288 | |
| 1289 | /* Nexthop, ifindex, distance, metric. */ |
| 1290 | if (CHECK_FLAG (api.message, ZAPI_MESSAGE_NEXTHOP)) |
| 1291 | { |
| 1292 | u_char nexthop_type; |
| 1293 | |
| 1294 | api.nexthop_num = stream_getc (s); |
| 1295 | for (i = 0; i < api.nexthop_num; i++) |
| 1296 | { |
| 1297 | nexthop_type = stream_getc (s); |
| 1298 | |
| 1299 | switch (nexthop_type) |
| 1300 | { |
| 1301 | case ZEBRA_NEXTHOP_IPV6: |
| 1302 | stream_get (&nexthop, s, 16); |
| 1303 | break; |
| 1304 | case ZEBRA_NEXTHOP_IFINDEX: |
| 1305 | ifindex = stream_getl (s); |
| 1306 | break; |
| 1307 | } |
| 1308 | } |
| 1309 | } |
| 1310 | |
Piotr Chytła | eefddcc | 2015-12-01 09:48:02 -0500 | [diff] [blame] | 1311 | /* Distance. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1312 | if (CHECK_FLAG (api.message, ZAPI_MESSAGE_DISTANCE)) |
| 1313 | api.distance = stream_getc (s); |
| 1314 | else |
| 1315 | api.distance = 0; |
Piotr Chytła | eefddcc | 2015-12-01 09:48:02 -0500 | [diff] [blame] | 1316 | |
| 1317 | /* Metric. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1318 | if (CHECK_FLAG (api.message, ZAPI_MESSAGE_METRIC)) |
| 1319 | api.metric = stream_getl (s); |
| 1320 | else |
| 1321 | api.metric = 0; |
| 1322 | |
Piotr Chytła | eefddcc | 2015-12-01 09:48:02 -0500 | [diff] [blame] | 1323 | /* tag */ |
| 1324 | if (CHECK_FLAG (api.message, ZAPI_MESSAGE_TAG)) |
Paul Jakma | 96d1060 | 2016-07-01 14:23:45 +0100 | [diff] [blame^] | 1325 | api.tag = stream_getl (s); |
Piotr Chytła | eefddcc | 2015-12-01 09:48:02 -0500 | [diff] [blame] | 1326 | else |
| 1327 | api.tag = 0; |
| 1328 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1329 | if (IN6_IS_ADDR_UNSPECIFIED (&nexthop)) |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1330 | rib_delete_ipv6 (api.type, api.flags, &p, NULL, ifindex, vrf_id, |
Feng Lu | 0d0686f | 2015-05-22 11:40:02 +0200 | [diff] [blame] | 1331 | api.safi); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1332 | else |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1333 | rib_delete_ipv6 (api.type, api.flags, &p, &nexthop, ifindex, vrf_id, |
Feng Lu | 0d0686f | 2015-05-22 11:40:02 +0200 | [diff] [blame] | 1334 | api.safi); |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1335 | return 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1336 | } |
| 1337 | |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1338 | static int |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1339 | zread_ipv6_nexthop_lookup (struct zserv *client, u_short length, |
| 1340 | vrf_id_t vrf_id) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1341 | { |
| 1342 | struct in6_addr addr; |
| 1343 | char buf[BUFSIZ]; |
| 1344 | |
| 1345 | stream_get (&addr, client->ibuf, 16); |
Christian Franke | a520708 | 2013-04-11 08:24:29 +0000 | [diff] [blame] | 1346 | if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV) |
| 1347 | zlog_debug("%s: looking up %s", __func__, |
| 1348 | inet_ntop (AF_INET6, &addr, buf, BUFSIZ)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1349 | |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1350 | return zsend_ipv6_nexthop_lookup (client, &addr, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1351 | } |
| 1352 | #endif /* HAVE_IPV6 */ |
| 1353 | |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 1354 | /* Register zebra server router-id information. Send current router-id */ |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1355 | static int |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1356 | 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] | 1357 | { |
| 1358 | struct prefix p; |
| 1359 | |
| 1360 | /* Router-id information is needed. */ |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1361 | vrf_bitmap_set (client->ridinfo, vrf_id); |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 1362 | |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1363 | router_id_get (&p, vrf_id); |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 1364 | |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1365 | return zsend_router_id_update (client, &p, vrf_id); |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 1366 | } |
| 1367 | |
| 1368 | /* Unregister zebra server router-id information. */ |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1369 | static int |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1370 | 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] | 1371 | { |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1372 | vrf_bitmap_unset (client->ridinfo, vrf_id); |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1373 | return 0; |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 1374 | } |
| 1375 | |
Vyacheslav Trushkin | 2ea1ab1 | 2011-12-11 18:48:47 +0400 | [diff] [blame] | 1376 | /* Tie up route-type and client->sock */ |
| 1377 | static void |
| 1378 | zread_hello (struct zserv *client) |
| 1379 | { |
| 1380 | /* type of protocol (lib/zebra.h) */ |
| 1381 | u_char proto; |
| 1382 | proto = stream_getc (client->ibuf); |
| 1383 | |
| 1384 | /* accept only dynamic routing protocols */ |
| 1385 | if ((proto < ZEBRA_ROUTE_MAX) |
| 1386 | && (proto > ZEBRA_ROUTE_STATIC)) |
| 1387 | { |
| 1388 | zlog_notice ("client %d says hello and bids fair to announce only %s routes", |
| 1389 | client->sock, zebra_route_string(proto)); |
| 1390 | |
| 1391 | /* if route-type was binded by other client */ |
| 1392 | if (route_type_oaths[proto]) |
| 1393 | zlog_warn ("sender of %s routes changed %c->%c", |
| 1394 | zebra_route_string(proto), route_type_oaths[proto], |
| 1395 | client->sock); |
| 1396 | |
| 1397 | route_type_oaths[proto] = client->sock; |
Pradosh Mohapatra | 60cc959 | 2015-11-09 20:21:41 -0500 | [diff] [blame] | 1398 | client->proto = proto; |
Vyacheslav Trushkin | 2ea1ab1 | 2011-12-11 18:48:47 +0400 | [diff] [blame] | 1399 | } |
| 1400 | } |
| 1401 | |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1402 | /* Unregister all information in a VRF. */ |
| 1403 | static int |
| 1404 | zread_vrf_unregister (struct zserv *client, u_short length, vrf_id_t vrf_id) |
| 1405 | { |
| 1406 | int i; |
| 1407 | |
| 1408 | for (i = 0; i < ZEBRA_ROUTE_MAX; i++) |
| 1409 | vrf_bitmap_unset (client->redist[i], vrf_id); |
| 1410 | vrf_bitmap_unset (client->redist_default, vrf_id); |
| 1411 | vrf_bitmap_unset (client->ifinfo, vrf_id); |
| 1412 | vrf_bitmap_unset (client->ridinfo, vrf_id); |
| 1413 | |
| 1414 | return 0; |
| 1415 | } |
| 1416 | |
Vyacheslav Trushkin | 2ea1ab1 | 2011-12-11 18:48:47 +0400 | [diff] [blame] | 1417 | /* If client sent routes of specific type, zebra removes it |
| 1418 | * and returns number of deleted routes. |
| 1419 | */ |
| 1420 | static void |
| 1421 | zebra_score_rib (int client_sock) |
| 1422 | { |
| 1423 | int i; |
| 1424 | |
| 1425 | for (i = ZEBRA_ROUTE_RIP; i < ZEBRA_ROUTE_MAX; i++) |
| 1426 | if (client_sock == route_type_oaths[i]) |
| 1427 | { |
| 1428 | zlog_notice ("client %d disconnected. %lu %s routes removed from the rib", |
| 1429 | client_sock, rib_score_proto (i), zebra_route_string (i)); |
| 1430 | route_type_oaths[i] = 0; |
| 1431 | break; |
| 1432 | } |
| 1433 | } |
| 1434 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1435 | /* Close zebra client. */ |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 1436 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1437 | zebra_client_close (struct zserv *client) |
| 1438 | { |
Pradosh Mohapatra | 60cc959 | 2015-11-09 20:21:41 -0500 | [diff] [blame] | 1439 | zebra_cleanup_rnh_client(0, AF_INET, client); |
| 1440 | zebra_cleanup_rnh_client(0, AF_INET6, client); |
| 1441 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1442 | /* Close file descriptor. */ |
| 1443 | if (client->sock) |
| 1444 | { |
| 1445 | close (client->sock); |
Vyacheslav Trushkin | 2ea1ab1 | 2011-12-11 18:48:47 +0400 | [diff] [blame] | 1446 | zebra_score_rib (client->sock); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1447 | client->sock = -1; |
| 1448 | } |
| 1449 | |
| 1450 | /* Free stream buffers. */ |
| 1451 | if (client->ibuf) |
| 1452 | stream_free (client->ibuf); |
| 1453 | if (client->obuf) |
| 1454 | stream_free (client->obuf); |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1455 | if (client->wb) |
| 1456 | buffer_free(client->wb); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1457 | |
| 1458 | /* Release threads. */ |
| 1459 | if (client->t_read) |
| 1460 | thread_cancel (client->t_read); |
| 1461 | if (client->t_write) |
| 1462 | thread_cancel (client->t_write); |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1463 | if (client->t_suicide) |
| 1464 | thread_cancel (client->t_suicide); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1465 | |
| 1466 | /* Free client structure. */ |
paul | b21b19c | 2003-06-15 01:28:29 +0000 | [diff] [blame] | 1467 | listnode_delete (zebrad.client_list, client); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1468 | XFREE (0, client); |
| 1469 | } |
| 1470 | |
| 1471 | /* Make new client. */ |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 1472 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1473 | zebra_client_create (int sock) |
| 1474 | { |
| 1475 | struct zserv *client; |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1476 | int i; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1477 | |
David Lamparter | 23757db | 2016-02-24 06:26:02 +0100 | [diff] [blame] | 1478 | client = XCALLOC (MTYPE_TMP, sizeof (struct zserv)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1479 | |
| 1480 | /* Make client input/output buffer. */ |
| 1481 | client->sock = sock; |
| 1482 | client->ibuf = stream_new (ZEBRA_MAX_PACKET_SIZ); |
| 1483 | client->obuf = stream_new (ZEBRA_MAX_PACKET_SIZ); |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1484 | client->wb = buffer_new(0); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1485 | |
| 1486 | /* Set table number. */ |
paul | b21b19c | 2003-06-15 01:28:29 +0000 | [diff] [blame] | 1487 | client->rtm_table = zebrad.rtm_table_default; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1488 | |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1489 | /* Initialize flags */ |
| 1490 | for (i = 0; i < ZEBRA_ROUTE_MAX; i++) |
| 1491 | client->redist[i] = vrf_bitmap_init (); |
| 1492 | client->redist_default = vrf_bitmap_init (); |
| 1493 | client->ifinfo = vrf_bitmap_init (); |
| 1494 | client->ridinfo = vrf_bitmap_init (); |
| 1495 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1496 | /* Add this client to linked list. */ |
paul | b21b19c | 2003-06-15 01:28:29 +0000 | [diff] [blame] | 1497 | listnode_add (zebrad.client_list, client); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1498 | |
| 1499 | /* Make new read thread. */ |
| 1500 | zebra_event (ZEBRA_READ, sock, client); |
| 1501 | } |
| 1502 | |
| 1503 | /* Handler of zebra service request. */ |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 1504 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1505 | zebra_client_read (struct thread *thread) |
| 1506 | { |
| 1507 | int sock; |
| 1508 | struct zserv *client; |
ajs | 57a1477 | 2005-04-10 15:01:56 +0000 | [diff] [blame] | 1509 | size_t already; |
paul | c1b9800 | 2006-01-16 01:54:02 +0000 | [diff] [blame] | 1510 | uint16_t length, command; |
| 1511 | uint8_t marker, version; |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1512 | vrf_id_t vrf_id; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1513 | |
| 1514 | /* Get thread data. Reset reading thread because I'm running. */ |
| 1515 | sock = THREAD_FD (thread); |
| 1516 | client = THREAD_ARG (thread); |
| 1517 | client->t_read = NULL; |
| 1518 | |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1519 | if (client->t_suicide) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1520 | { |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1521 | zebra_client_close(client); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1522 | return -1; |
| 1523 | } |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1524 | |
| 1525 | /* Read length and command (if we don't have it already). */ |
ajs | 57a1477 | 2005-04-10 15:01:56 +0000 | [diff] [blame] | 1526 | if ((already = stream_get_endp(client->ibuf)) < ZEBRA_HEADER_SIZE) |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1527 | { |
ajs | 57a1477 | 2005-04-10 15:01:56 +0000 | [diff] [blame] | 1528 | ssize_t nbyte; |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1529 | if (((nbyte = stream_read_try (client->ibuf, sock, |
ajs | 57a1477 | 2005-04-10 15:01:56 +0000 | [diff] [blame] | 1530 | ZEBRA_HEADER_SIZE-already)) == 0) || |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1531 | (nbyte == -1)) |
| 1532 | { |
| 1533 | if (IS_ZEBRA_DEBUG_EVENT) |
| 1534 | zlog_debug ("connection closed socket [%d]", sock); |
| 1535 | zebra_client_close (client); |
| 1536 | return -1; |
| 1537 | } |
ajs | 57a1477 | 2005-04-10 15:01:56 +0000 | [diff] [blame] | 1538 | if (nbyte != (ssize_t)(ZEBRA_HEADER_SIZE-already)) |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1539 | { |
| 1540 | /* Try again later. */ |
| 1541 | zebra_event (ZEBRA_READ, sock, client); |
| 1542 | return 0; |
| 1543 | } |
ajs | 57a1477 | 2005-04-10 15:01:56 +0000 | [diff] [blame] | 1544 | already = ZEBRA_HEADER_SIZE; |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1545 | } |
| 1546 | |
| 1547 | /* Reset to read from the beginning of the incoming packet. */ |
| 1548 | stream_set_getp(client->ibuf, 0); |
| 1549 | |
paul | c1b9800 | 2006-01-16 01:54:02 +0000 | [diff] [blame] | 1550 | /* Fetch header values */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1551 | length = stream_getw (client->ibuf); |
paul | c1b9800 | 2006-01-16 01:54:02 +0000 | [diff] [blame] | 1552 | marker = stream_getc (client->ibuf); |
| 1553 | version = stream_getc (client->ibuf); |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1554 | vrf_id = stream_getw (client->ibuf); |
paul | c1b9800 | 2006-01-16 01:54:02 +0000 | [diff] [blame] | 1555 | command = stream_getw (client->ibuf); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1556 | |
paul | c1b9800 | 2006-01-16 01:54:02 +0000 | [diff] [blame] | 1557 | if (marker != ZEBRA_HEADER_MARKER || version != ZSERV_VERSION) |
| 1558 | { |
| 1559 | zlog_err("%s: socket %d version mismatch, marker %d, version %d", |
| 1560 | __func__, sock, marker, version); |
| 1561 | zebra_client_close (client); |
| 1562 | return -1; |
| 1563 | } |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1564 | if (length < ZEBRA_HEADER_SIZE) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1565 | { |
ajs | 57a1477 | 2005-04-10 15:01:56 +0000 | [diff] [blame] | 1566 | zlog_warn("%s: socket %d message length %u is less than header size %d", |
| 1567 | __func__, sock, length, ZEBRA_HEADER_SIZE); |
| 1568 | zebra_client_close (client); |
| 1569 | return -1; |
| 1570 | } |
| 1571 | if (length > STREAM_SIZE(client->ibuf)) |
| 1572 | { |
| 1573 | zlog_warn("%s: socket %d message length %u exceeds buffer size %lu", |
| 1574 | __func__, sock, length, (u_long)STREAM_SIZE(client->ibuf)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1575 | zebra_client_close (client); |
| 1576 | return -1; |
| 1577 | } |
| 1578 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1579 | /* Read rest of data. */ |
ajs | 57a1477 | 2005-04-10 15:01:56 +0000 | [diff] [blame] | 1580 | if (already < length) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1581 | { |
ajs | 57a1477 | 2005-04-10 15:01:56 +0000 | [diff] [blame] | 1582 | ssize_t nbyte; |
| 1583 | if (((nbyte = stream_read_try (client->ibuf, sock, |
| 1584 | length-already)) == 0) || |
| 1585 | (nbyte == -1)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1586 | { |
| 1587 | if (IS_ZEBRA_DEBUG_EVENT) |
ajs | b617800 | 2004-12-07 21:12:56 +0000 | [diff] [blame] | 1588 | zlog_debug ("connection closed [%d] when reading zebra data", sock); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1589 | zebra_client_close (client); |
| 1590 | return -1; |
| 1591 | } |
ajs | 57a1477 | 2005-04-10 15:01:56 +0000 | [diff] [blame] | 1592 | if (nbyte != (ssize_t)(length-already)) |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1593 | { |
| 1594 | /* Try again later. */ |
| 1595 | zebra_event (ZEBRA_READ, sock, client); |
| 1596 | return 0; |
| 1597 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1598 | } |
| 1599 | |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1600 | length -= ZEBRA_HEADER_SIZE; |
| 1601 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1602 | /* Debug packet information. */ |
| 1603 | if (IS_ZEBRA_DEBUG_EVENT) |
ajs | b617800 | 2004-12-07 21:12:56 +0000 | [diff] [blame] | 1604 | zlog_debug ("zebra message comes from socket [%d]", sock); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1605 | |
| 1606 | if (IS_ZEBRA_DEBUG_PACKET && IS_ZEBRA_DEBUG_RECV) |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1607 | zlog_debug ("zebra message received [%s] %d in VRF %u", |
| 1608 | zserv_command_string (command), length, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1609 | |
| 1610 | switch (command) |
| 1611 | { |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 1612 | case ZEBRA_ROUTER_ID_ADD: |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1613 | zread_router_id_add (client, length, vrf_id); |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 1614 | break; |
| 1615 | case ZEBRA_ROUTER_ID_DELETE: |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1616 | zread_router_id_delete (client, length, vrf_id); |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 1617 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1618 | case ZEBRA_INTERFACE_ADD: |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1619 | zread_interface_add (client, length, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1620 | break; |
| 1621 | case ZEBRA_INTERFACE_DELETE: |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1622 | zread_interface_delete (client, length, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1623 | break; |
| 1624 | case ZEBRA_IPV4_ROUTE_ADD: |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1625 | zread_ipv4_add (client, length, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1626 | break; |
| 1627 | case ZEBRA_IPV4_ROUTE_DELETE: |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1628 | zread_ipv4_delete (client, length, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1629 | break; |
| 1630 | #ifdef HAVE_IPV6 |
| 1631 | case ZEBRA_IPV6_ROUTE_ADD: |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1632 | zread_ipv6_add (client, length, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1633 | break; |
| 1634 | case ZEBRA_IPV6_ROUTE_DELETE: |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1635 | zread_ipv6_delete (client, length, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1636 | break; |
| 1637 | #endif /* HAVE_IPV6 */ |
| 1638 | case ZEBRA_REDISTRIBUTE_ADD: |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1639 | zebra_redistribute_add (command, client, length, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1640 | break; |
| 1641 | case ZEBRA_REDISTRIBUTE_DELETE: |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1642 | zebra_redistribute_delete (command, client, length, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1643 | break; |
| 1644 | case ZEBRA_REDISTRIBUTE_DEFAULT_ADD: |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1645 | zebra_redistribute_default_add (command, client, length, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1646 | break; |
| 1647 | case ZEBRA_REDISTRIBUTE_DEFAULT_DELETE: |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1648 | zebra_redistribute_default_delete (command, client, length, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1649 | break; |
| 1650 | case ZEBRA_IPV4_NEXTHOP_LOOKUP: |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1651 | zread_ipv4_nexthop_lookup (client, length, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1652 | break; |
Everton Marques | 4e5275b | 2014-07-01 15:15:52 -0300 | [diff] [blame] | 1653 | case ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB: |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1654 | zread_ipv4_nexthop_lookup_mrib (client, length, vrf_id); |
Everton Marques | 4e5275b | 2014-07-01 15:15:52 -0300 | [diff] [blame] | 1655 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1656 | #ifdef HAVE_IPV6 |
| 1657 | case ZEBRA_IPV6_NEXTHOP_LOOKUP: |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1658 | zread_ipv6_nexthop_lookup (client, length, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1659 | break; |
| 1660 | #endif /* HAVE_IPV6 */ |
| 1661 | case ZEBRA_IPV4_IMPORT_LOOKUP: |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1662 | zread_ipv4_import_lookup (client, length, vrf_id); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1663 | break; |
Vyacheslav Trushkin | 2ea1ab1 | 2011-12-11 18:48:47 +0400 | [diff] [blame] | 1664 | case ZEBRA_HELLO: |
| 1665 | zread_hello (client); |
| 1666 | break; |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1667 | case ZEBRA_VRF_UNREGISTER: |
| 1668 | zread_vrf_unregister (client, length, vrf_id); |
Pradosh Mohapatra | 60cc959 | 2015-11-09 20:21:41 -0500 | [diff] [blame] | 1669 | case ZEBRA_NEXTHOP_REGISTER: |
| 1670 | zserv_nexthop_register(client, sock, length, vrf_id); |
| 1671 | break; |
| 1672 | case ZEBRA_NEXTHOP_UNREGISTER: |
| 1673 | zserv_nexthop_unregister(client, sock, length); |
Feng Lu | c99f348 | 2014-10-16 09:52:36 +0800 | [diff] [blame] | 1674 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1675 | default: |
| 1676 | zlog_info ("Zebra received unknown command %d", command); |
| 1677 | break; |
| 1678 | } |
| 1679 | |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1680 | if (client->t_suicide) |
| 1681 | { |
| 1682 | /* No need to wait for thread callback, just kill immediately. */ |
| 1683 | zebra_client_close(client); |
| 1684 | return -1; |
| 1685 | } |
| 1686 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1687 | stream_reset (client->ibuf); |
| 1688 | zebra_event (ZEBRA_READ, sock, client); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1689 | return 0; |
| 1690 | } |
| 1691 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1692 | |
| 1693 | /* Accept code of zebra server socket. */ |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 1694 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1695 | zebra_accept (struct thread *thread) |
| 1696 | { |
| 1697 | int accept_sock; |
| 1698 | int client_sock; |
| 1699 | struct sockaddr_in client; |
| 1700 | socklen_t len; |
| 1701 | |
| 1702 | accept_sock = THREAD_FD (thread); |
| 1703 | |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1704 | /* Reregister myself. */ |
| 1705 | zebra_event (ZEBRA_SERV, accept_sock, NULL); |
| 1706 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1707 | len = sizeof (struct sockaddr_in); |
| 1708 | client_sock = accept (accept_sock, (struct sockaddr *) &client, &len); |
| 1709 | |
| 1710 | if (client_sock < 0) |
| 1711 | { |
ajs | 6099b3b | 2004-11-20 02:06:59 +0000 | [diff] [blame] | 1712 | zlog_warn ("Can't accept zebra socket: %s", safe_strerror (errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1713 | return -1; |
| 1714 | } |
| 1715 | |
paul | ccf3557 | 2003-03-01 11:42:20 +0000 | [diff] [blame] | 1716 | /* Make client socket non-blocking. */ |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 1717 | set_nonblocking(client_sock); |
paul | 865b852 | 2005-01-05 08:30:35 +0000 | [diff] [blame] | 1718 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1719 | /* Create new zebra client. */ |
| 1720 | zebra_client_create (client_sock); |
| 1721 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1722 | return 0; |
| 1723 | } |
| 1724 | |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 1725 | #ifdef HAVE_TCP_ZEBRA |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1726 | /* Make zebra's server socket. */ |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 1727 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1728 | zebra_serv () |
| 1729 | { |
| 1730 | int ret; |
| 1731 | int accept_sock; |
| 1732 | struct sockaddr_in addr; |
| 1733 | |
| 1734 | accept_sock = socket (AF_INET, SOCK_STREAM, 0); |
| 1735 | |
| 1736 | if (accept_sock < 0) |
| 1737 | { |
paul | 3d1dc85 | 2005-04-05 00:45:23 +0000 | [diff] [blame] | 1738 | zlog_warn ("Can't create zserv stream socket: %s", |
| 1739 | safe_strerror (errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1740 | zlog_warn ("zebra can't provice full functionality due to above error"); |
| 1741 | return; |
| 1742 | } |
| 1743 | |
Vyacheslav Trushkin | 2ea1ab1 | 2011-12-11 18:48:47 +0400 | [diff] [blame] | 1744 | memset (&route_type_oaths, 0, sizeof (route_type_oaths)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1745 | memset (&addr, 0, sizeof (struct sockaddr_in)); |
| 1746 | addr.sin_family = AF_INET; |
| 1747 | addr.sin_port = htons (ZEBRA_PORT); |
Paul Jakma | 6f0e3f6 | 2007-05-10 02:38:51 +0000 | [diff] [blame] | 1748 | #ifdef HAVE_STRUCT_SOCKADDR_IN_SIN_LEN |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1749 | addr.sin_len = sizeof (struct sockaddr_in); |
Paul Jakma | 6f0e3f6 | 2007-05-10 02:38:51 +0000 | [diff] [blame] | 1750 | #endif /* HAVE_STRUCT_SOCKADDR_IN_SIN_LEN */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1751 | addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK); |
| 1752 | |
| 1753 | sockopt_reuseaddr (accept_sock); |
| 1754 | sockopt_reuseport (accept_sock); |
| 1755 | |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 1756 | if ( zserv_privs.change(ZPRIVS_RAISE) ) |
| 1757 | zlog (NULL, LOG_ERR, "Can't raise privileges"); |
| 1758 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1759 | ret = bind (accept_sock, (struct sockaddr *)&addr, |
| 1760 | sizeof (struct sockaddr_in)); |
| 1761 | if (ret < 0) |
| 1762 | { |
paul | 3d1dc85 | 2005-04-05 00:45:23 +0000 | [diff] [blame] | 1763 | zlog_warn ("Can't bind to stream socket: %s", |
| 1764 | safe_strerror (errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1765 | zlog_warn ("zebra can't provice full functionality due to above error"); |
| 1766 | close (accept_sock); /* Avoid sd leak. */ |
| 1767 | return; |
| 1768 | } |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 1769 | |
| 1770 | if ( zserv_privs.change(ZPRIVS_LOWER) ) |
| 1771 | zlog (NULL, LOG_ERR, "Can't lower privileges"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1772 | |
| 1773 | ret = listen (accept_sock, 1); |
| 1774 | if (ret < 0) |
| 1775 | { |
paul | 3d1dc85 | 2005-04-05 00:45:23 +0000 | [diff] [blame] | 1776 | zlog_warn ("Can't listen to stream socket: %s", |
| 1777 | safe_strerror (errno)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1778 | zlog_warn ("zebra can't provice full functionality due to above error"); |
| 1779 | close (accept_sock); /* Avoid sd leak. */ |
| 1780 | return; |
| 1781 | } |
| 1782 | |
| 1783 | zebra_event (ZEBRA_SERV, accept_sock, NULL); |
| 1784 | } |
David Lamparter | 4b6c332 | 2015-04-21 09:47:57 +0200 | [diff] [blame] | 1785 | #else /* HAVE_TCP_ZEBRA */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1786 | |
| 1787 | /* For sockaddr_un. */ |
| 1788 | #include <sys/un.h> |
| 1789 | |
| 1790 | /* zebra server UNIX domain socket. */ |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 1791 | static void |
hasso | fce954f | 2004-10-07 20:29:24 +0000 | [diff] [blame] | 1792 | zebra_serv_un (const char *path) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1793 | { |
| 1794 | int ret; |
| 1795 | int sock, len; |
| 1796 | struct sockaddr_un serv; |
| 1797 | mode_t old_mask; |
| 1798 | |
| 1799 | /* First of all, unlink existing socket */ |
| 1800 | unlink (path); |
| 1801 | |
| 1802 | /* Set umask */ |
| 1803 | old_mask = umask (0077); |
| 1804 | |
| 1805 | /* Make UNIX domain socket. */ |
| 1806 | sock = socket (AF_UNIX, SOCK_STREAM, 0); |
| 1807 | if (sock < 0) |
| 1808 | { |
paul | 3d1dc85 | 2005-04-05 00:45:23 +0000 | [diff] [blame] | 1809 | zlog_warn ("Can't create zserv unix socket: %s", |
| 1810 | safe_strerror (errno)); |
| 1811 | zlog_warn ("zebra can't provide full functionality due to above error"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1812 | return; |
| 1813 | } |
| 1814 | |
Vyacheslav Trushkin | 2ea1ab1 | 2011-12-11 18:48:47 +0400 | [diff] [blame] | 1815 | memset (&route_type_oaths, 0, sizeof (route_type_oaths)); |
| 1816 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1817 | /* Make server socket. */ |
| 1818 | memset (&serv, 0, sizeof (struct sockaddr_un)); |
| 1819 | serv.sun_family = AF_UNIX; |
| 1820 | strncpy (serv.sun_path, path, strlen (path)); |
Paul Jakma | 6f0e3f6 | 2007-05-10 02:38:51 +0000 | [diff] [blame] | 1821 | #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1822 | len = serv.sun_len = SUN_LEN(&serv); |
| 1823 | #else |
| 1824 | len = sizeof (serv.sun_family) + strlen (serv.sun_path); |
Paul Jakma | 6f0e3f6 | 2007-05-10 02:38:51 +0000 | [diff] [blame] | 1825 | #endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1826 | |
| 1827 | ret = bind (sock, (struct sockaddr *) &serv, len); |
| 1828 | if (ret < 0) |
| 1829 | { |
paul | 3d1dc85 | 2005-04-05 00:45:23 +0000 | [diff] [blame] | 1830 | zlog_warn ("Can't bind to unix socket %s: %s", |
| 1831 | path, safe_strerror (errno)); |
| 1832 | zlog_warn ("zebra can't provide full functionality due to above error"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1833 | close (sock); |
| 1834 | return; |
| 1835 | } |
| 1836 | |
| 1837 | ret = listen (sock, 5); |
| 1838 | if (ret < 0) |
| 1839 | { |
paul | 3d1dc85 | 2005-04-05 00:45:23 +0000 | [diff] [blame] | 1840 | zlog_warn ("Can't listen to unix socket %s: %s", |
| 1841 | path, safe_strerror (errno)); |
| 1842 | zlog_warn ("zebra can't provide full functionality due to above error"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1843 | close (sock); |
| 1844 | return; |
| 1845 | } |
| 1846 | |
| 1847 | umask (old_mask); |
| 1848 | |
| 1849 | zebra_event (ZEBRA_SERV, sock, NULL); |
| 1850 | } |
David Lamparter | 4b6c332 | 2015-04-21 09:47:57 +0200 | [diff] [blame] | 1851 | #endif /* HAVE_TCP_ZEBRA */ |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 1852 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1853 | |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 1854 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1855 | zebra_event (enum event event, int sock, struct zserv *client) |
| 1856 | { |
| 1857 | switch (event) |
| 1858 | { |
| 1859 | case ZEBRA_SERV: |
paul | b21b19c | 2003-06-15 01:28:29 +0000 | [diff] [blame] | 1860 | thread_add_read (zebrad.master, zebra_accept, client, sock); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1861 | break; |
| 1862 | case ZEBRA_READ: |
| 1863 | client->t_read = |
paul | b21b19c | 2003-06-15 01:28:29 +0000 | [diff] [blame] | 1864 | thread_add_read (zebrad.master, zebra_client_read, client, sock); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1865 | break; |
| 1866 | case ZEBRA_WRITE: |
| 1867 | /**/ |
| 1868 | break; |
| 1869 | } |
| 1870 | } |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 1871 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1872 | /* Display default rtm_table for all clients. */ |
| 1873 | DEFUN (show_table, |
| 1874 | show_table_cmd, |
| 1875 | "show table", |
| 1876 | SHOW_STR |
| 1877 | "default routing table to use for all clients\n") |
| 1878 | { |
paul | b21b19c | 2003-06-15 01:28:29 +0000 | [diff] [blame] | 1879 | vty_out (vty, "table %d%s", zebrad.rtm_table_default, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1880 | VTY_NEWLINE); |
| 1881 | return CMD_SUCCESS; |
| 1882 | } |
| 1883 | |
| 1884 | DEFUN (config_table, |
| 1885 | config_table_cmd, |
| 1886 | "table TABLENO", |
| 1887 | "Configure target kernel routing table\n" |
| 1888 | "TABLE integer\n") |
| 1889 | { |
paul | b21b19c | 2003-06-15 01:28:29 +0000 | [diff] [blame] | 1890 | zebrad.rtm_table_default = strtol (argv[0], (char**)0, 10); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1891 | return CMD_SUCCESS; |
| 1892 | } |
| 1893 | |
hasso | 647e4f1 | 2003-05-25 11:43:52 +0000 | [diff] [blame] | 1894 | DEFUN (ip_forwarding, |
| 1895 | ip_forwarding_cmd, |
| 1896 | "ip forwarding", |
| 1897 | IP_STR |
| 1898 | "Turn on IP forwarding") |
| 1899 | { |
| 1900 | int ret; |
| 1901 | |
| 1902 | ret = ipforward (); |
hasso | b71f00f | 2004-10-13 12:20:35 +0000 | [diff] [blame] | 1903 | if (ret == 0) |
| 1904 | ret = ipforward_on (); |
hasso | 647e4f1 | 2003-05-25 11:43:52 +0000 | [diff] [blame] | 1905 | |
hasso | 647e4f1 | 2003-05-25 11:43:52 +0000 | [diff] [blame] | 1906 | if (ret == 0) |
| 1907 | { |
| 1908 | vty_out (vty, "Can't turn on IP forwarding%s", VTY_NEWLINE); |
| 1909 | return CMD_WARNING; |
| 1910 | } |
| 1911 | |
| 1912 | return CMD_SUCCESS; |
| 1913 | } |
| 1914 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1915 | DEFUN (no_ip_forwarding, |
| 1916 | no_ip_forwarding_cmd, |
| 1917 | "no ip forwarding", |
| 1918 | NO_STR |
| 1919 | IP_STR |
| 1920 | "Turn off IP forwarding") |
| 1921 | { |
| 1922 | int ret; |
| 1923 | |
| 1924 | ret = ipforward (); |
hasso | b71f00f | 2004-10-13 12:20:35 +0000 | [diff] [blame] | 1925 | if (ret != 0) |
| 1926 | ret = ipforward_off (); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1927 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1928 | if (ret != 0) |
| 1929 | { |
| 1930 | vty_out (vty, "Can't turn off IP forwarding%s", VTY_NEWLINE); |
| 1931 | return CMD_WARNING; |
| 1932 | } |
| 1933 | |
| 1934 | return CMD_SUCCESS; |
| 1935 | } |
| 1936 | |
| 1937 | /* This command is for debugging purpose. */ |
| 1938 | DEFUN (show_zebra_client, |
| 1939 | show_zebra_client_cmd, |
| 1940 | "show zebra client", |
| 1941 | SHOW_STR |
| 1942 | "Zebra information" |
| 1943 | "Client information") |
| 1944 | { |
hasso | 52dc7ee | 2004-09-23 19:18:23 +0000 | [diff] [blame] | 1945 | struct listnode *node; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1946 | struct zserv *client; |
| 1947 | |
paul | 1eb8ef2 | 2005-04-07 07:30:20 +0000 | [diff] [blame] | 1948 | for (ALL_LIST_ELEMENTS_RO (zebrad.client_list, node, client)) |
Pradosh Mohapatra | 60cc959 | 2015-11-09 20:21:41 -0500 | [diff] [blame] | 1949 | vty_out (vty, "Client %s fd %d%s", |
| 1950 | zebra_route_string(client->proto), client->sock, |
| 1951 | VTY_NEWLINE); |
| 1952 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1953 | return CMD_SUCCESS; |
| 1954 | } |
| 1955 | |
| 1956 | /* Table configuration write function. */ |
paul | b9df2d2 | 2004-05-09 09:09:59 +0000 | [diff] [blame] | 1957 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1958 | config_write_table (struct vty *vty) |
| 1959 | { |
paul | b21b19c | 2003-06-15 01:28:29 +0000 | [diff] [blame] | 1960 | if (zebrad.rtm_table_default) |
| 1961 | vty_out (vty, "table %d%s", zebrad.rtm_table_default, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1962 | VTY_NEWLINE); |
| 1963 | return 0; |
| 1964 | } |
| 1965 | |
| 1966 | /* table node for routing tables. */ |
Stephen Hemminger | 7fc626d | 2008-12-01 11:10:34 -0800 | [diff] [blame] | 1967 | static struct cmd_node table_node = |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1968 | { |
| 1969 | TABLE_NODE, |
| 1970 | "", /* This node has no interface. */ |
| 1971 | 1 |
| 1972 | }; |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 1973 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1974 | /* Only display ip forwarding is enabled or not. */ |
| 1975 | DEFUN (show_ip_forwarding, |
| 1976 | show_ip_forwarding_cmd, |
| 1977 | "show ip forwarding", |
| 1978 | SHOW_STR |
| 1979 | IP_STR |
| 1980 | "IP forwarding status\n") |
| 1981 | { |
| 1982 | int ret; |
| 1983 | |
| 1984 | ret = ipforward (); |
| 1985 | |
| 1986 | if (ret == 0) |
| 1987 | vty_out (vty, "IP forwarding is off%s", VTY_NEWLINE); |
| 1988 | else |
| 1989 | vty_out (vty, "IP forwarding is on%s", VTY_NEWLINE); |
| 1990 | return CMD_SUCCESS; |
| 1991 | } |
| 1992 | |
| 1993 | #ifdef HAVE_IPV6 |
| 1994 | /* Only display ipv6 forwarding is enabled or not. */ |
| 1995 | DEFUN (show_ipv6_forwarding, |
| 1996 | show_ipv6_forwarding_cmd, |
| 1997 | "show ipv6 forwarding", |
| 1998 | SHOW_STR |
| 1999 | "IPv6 information\n" |
| 2000 | "Forwarding status\n") |
| 2001 | { |
| 2002 | int ret; |
| 2003 | |
| 2004 | ret = ipforward_ipv6 (); |
| 2005 | |
| 2006 | switch (ret) |
| 2007 | { |
| 2008 | case -1: |
| 2009 | vty_out (vty, "ipv6 forwarding is unknown%s", VTY_NEWLINE); |
| 2010 | break; |
| 2011 | case 0: |
| 2012 | vty_out (vty, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE); |
| 2013 | break; |
| 2014 | case 1: |
| 2015 | vty_out (vty, "ipv6 forwarding is %s%s", "on", VTY_NEWLINE); |
| 2016 | break; |
| 2017 | default: |
| 2018 | vty_out (vty, "ipv6 forwarding is %s%s", "off", VTY_NEWLINE); |
| 2019 | break; |
| 2020 | } |
| 2021 | return CMD_SUCCESS; |
| 2022 | } |
| 2023 | |
hasso | 5590672 | 2004-02-11 22:42:16 +0000 | [diff] [blame] | 2024 | DEFUN (ipv6_forwarding, |
| 2025 | ipv6_forwarding_cmd, |
| 2026 | "ipv6 forwarding", |
| 2027 | IPV6_STR |
| 2028 | "Turn on IPv6 forwarding") |
| 2029 | { |
| 2030 | int ret; |
| 2031 | |
hasso | 41d3fc9 | 2004-04-06 11:59:00 +0000 | [diff] [blame] | 2032 | ret = ipforward_ipv6 (); |
hasso | b71f00f | 2004-10-13 12:20:35 +0000 | [diff] [blame] | 2033 | if (ret == 0) |
| 2034 | ret = ipforward_ipv6_on (); |
hasso | 41d3fc9 | 2004-04-06 11:59:00 +0000 | [diff] [blame] | 2035 | |
hasso | 41d3fc9 | 2004-04-06 11:59:00 +0000 | [diff] [blame] | 2036 | if (ret == 0) |
| 2037 | { |
hasso | 5590672 | 2004-02-11 22:42:16 +0000 | [diff] [blame] | 2038 | vty_out (vty, "Can't turn on IPv6 forwarding%s", VTY_NEWLINE); |
| 2039 | return CMD_WARNING; |
| 2040 | } |
| 2041 | |
| 2042 | return CMD_SUCCESS; |
| 2043 | } |
| 2044 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2045 | DEFUN (no_ipv6_forwarding, |
| 2046 | no_ipv6_forwarding_cmd, |
| 2047 | "no ipv6 forwarding", |
| 2048 | NO_STR |
hasso | 5590672 | 2004-02-11 22:42:16 +0000 | [diff] [blame] | 2049 | IPV6_STR |
| 2050 | "Turn off IPv6 forwarding") |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2051 | { |
| 2052 | int ret; |
| 2053 | |
hasso | 41d3fc9 | 2004-04-06 11:59:00 +0000 | [diff] [blame] | 2054 | ret = ipforward_ipv6 (); |
hasso | b71f00f | 2004-10-13 12:20:35 +0000 | [diff] [blame] | 2055 | if (ret != 0) |
| 2056 | ret = ipforward_ipv6_off (); |
hasso | 41d3fc9 | 2004-04-06 11:59:00 +0000 | [diff] [blame] | 2057 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2058 | if (ret != 0) |
| 2059 | { |
| 2060 | vty_out (vty, "Can't turn off IPv6 forwarding%s", VTY_NEWLINE); |
| 2061 | return CMD_WARNING; |
| 2062 | } |
| 2063 | |
| 2064 | return CMD_SUCCESS; |
| 2065 | } |
| 2066 | |
| 2067 | #endif /* HAVE_IPV6 */ |
| 2068 | |
| 2069 | /* IPForwarding configuration write function. */ |
ajs | 719e974 | 2005-02-28 20:52:15 +0000 | [diff] [blame] | 2070 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2071 | config_write_forwarding (struct vty *vty) |
| 2072 | { |
hasso | 18a6dce | 2004-10-03 18:18:34 +0000 | [diff] [blame] | 2073 | /* FIXME: Find better place for that. */ |
| 2074 | router_id_write (vty); |
| 2075 | |
paul | 3e0b3a5 | 2004-08-23 18:58:32 +0000 | [diff] [blame] | 2076 | if (ipforward ()) |
| 2077 | vty_out (vty, "ip forwarding%s", VTY_NEWLINE); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2078 | #ifdef HAVE_IPV6 |
paul | 3e0b3a5 | 2004-08-23 18:58:32 +0000 | [diff] [blame] | 2079 | if (ipforward_ipv6 ()) |
| 2080 | vty_out (vty, "ipv6 forwarding%s", VTY_NEWLINE); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2081 | #endif /* HAVE_IPV6 */ |
| 2082 | vty_out (vty, "!%s", VTY_NEWLINE); |
| 2083 | return 0; |
| 2084 | } |
| 2085 | |
| 2086 | /* table node for routing tables. */ |
Stephen Hemminger | 7fc626d | 2008-12-01 11:10:34 -0800 | [diff] [blame] | 2087 | static struct cmd_node forwarding_node = |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2088 | { |
| 2089 | FORWARDING_NODE, |
| 2090 | "", /* This node has no interface. */ |
| 2091 | 1 |
| 2092 | }; |
| 2093 | |
Udaya Shankara KS | d869dbd | 2016-02-11 21:42:29 +0530 | [diff] [blame] | 2094 | #ifdef HAVE_FPM |
| 2095 | /* function to write the fpm config info */ |
| 2096 | static int |
| 2097 | config_write_fpm (struct vty *vty) |
| 2098 | { |
| 2099 | return |
| 2100 | fpm_remote_srv_write (vty); |
| 2101 | } |
| 2102 | |
| 2103 | /* Zebra node */ |
| 2104 | static struct cmd_node zebra_node = |
| 2105 | { |
| 2106 | ZEBRA_NODE, |
| 2107 | "", |
| 2108 | 1 |
| 2109 | }; |
| 2110 | #endif |
| 2111 | |
David Lamparter | 6b0655a | 2014-06-04 06:53:35 +0200 | [diff] [blame] | 2112 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2113 | /* Initialisation of zebra and installation of commands. */ |
| 2114 | void |
paul | a1ac18c | 2005-06-28 17:17:12 +0000 | [diff] [blame] | 2115 | zebra_init (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2116 | { |
| 2117 | /* Client list init. */ |
paul | b21b19c | 2003-06-15 01:28:29 +0000 | [diff] [blame] | 2118 | zebrad.client_list = list_new (); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2119 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2120 | /* Install configuration write function. */ |
| 2121 | install_node (&table_node, config_write_table); |
| 2122 | install_node (&forwarding_node, config_write_forwarding); |
Udaya Shankara KS | d869dbd | 2016-02-11 21:42:29 +0530 | [diff] [blame] | 2123 | #ifdef HAVE_FPM |
| 2124 | install_node (&zebra_node, config_write_fpm); |
| 2125 | #endif |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2126 | |
| 2127 | install_element (VIEW_NODE, &show_ip_forwarding_cmd); |
hasso | 647e4f1 | 2003-05-25 11:43:52 +0000 | [diff] [blame] | 2128 | install_element (CONFIG_NODE, &ip_forwarding_cmd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2129 | install_element (CONFIG_NODE, &no_ip_forwarding_cmd); |
| 2130 | install_element (ENABLE_NODE, &show_zebra_client_cmd); |
| 2131 | |
| 2132 | #ifdef HAVE_NETLINK |
| 2133 | install_element (VIEW_NODE, &show_table_cmd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2134 | install_element (CONFIG_NODE, &config_table_cmd); |
| 2135 | #endif /* HAVE_NETLINK */ |
| 2136 | |
| 2137 | #ifdef HAVE_IPV6 |
| 2138 | install_element (VIEW_NODE, &show_ipv6_forwarding_cmd); |
hasso | 5590672 | 2004-02-11 22:42:16 +0000 | [diff] [blame] | 2139 | install_element (CONFIG_NODE, &ipv6_forwarding_cmd); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2140 | install_element (CONFIG_NODE, &no_ipv6_forwarding_cmd); |
| 2141 | #endif /* HAVE_IPV6 */ |
Paul Jakma | 7514fb7 | 2007-05-02 16:05:35 +0000 | [diff] [blame] | 2142 | |
| 2143 | /* Route-map */ |
| 2144 | zebra_route_map_init (); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 2145 | } |
Denis Ovsienko | 97be79f | 2009-07-24 20:45:31 +0400 | [diff] [blame] | 2146 | |
| 2147 | /* Make zebra server socket, wiping any existing one (see bug #403). */ |
| 2148 | void |
Vyacheslav Trushkin | b511468 | 2011-11-25 18:51:48 +0400 | [diff] [blame] | 2149 | zebra_zserv_socket_init (char *path) |
Denis Ovsienko | 97be79f | 2009-07-24 20:45:31 +0400 | [diff] [blame] | 2150 | { |
| 2151 | #ifdef HAVE_TCP_ZEBRA |
| 2152 | zebra_serv (); |
| 2153 | #else |
Vyacheslav Trushkin | b511468 | 2011-11-25 18:51:48 +0400 | [diff] [blame] | 2154 | zebra_serv_un (path ? path : ZEBRA_SERV_PATH); |
Denis Ovsienko | 97be79f | 2009-07-24 20:45:31 +0400 | [diff] [blame] | 2155 | #endif /* HAVE_TCP_ZEBRA */ |
| 2156 | } |