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