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