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