Everton Marques | 871dbcf | 2009-08-11 15:43:05 -0300 | [diff] [blame] | 1 | /* |
| 2 | PIM for Quagga |
| 3 | Copyright (C) 2008 Everton da Silva Marques |
| 4 | |
| 5 | This program is free software; you can redistribute it and/or modify |
| 6 | it under the terms of the GNU General Public License as published by |
| 7 | the Free Software Foundation; either version 2 of the License, or |
| 8 | (at your option) any later version. |
| 9 | |
| 10 | This program is distributed in the hope that it will be useful, but |
| 11 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU General Public License |
| 16 | along with this program; see the file COPYING; if not, write to the |
| 17 | Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, |
| 18 | MA 02110-1301 USA |
| 19 | |
| 20 | $QuaggaId: $Format:%an, %ai, %h$ $ |
| 21 | */ |
| 22 | |
| 23 | #include <zebra.h> |
| 24 | #include "zebra/rib.h" |
| 25 | |
| 26 | #include "log.h" |
| 27 | #include "prefix.h" |
| 28 | #include "zclient.h" |
| 29 | #include "stream.h" |
| 30 | #include "network.h" |
| 31 | #include "thread.h" |
| 32 | |
| 33 | #include "pimd.h" |
| 34 | #include "pim_pim.h" |
| 35 | #include "pim_str.h" |
| 36 | #include "pim_zlookup.h" |
| 37 | |
| 38 | extern int zclient_debug; |
| 39 | |
| 40 | static void zclient_lookup_sched(struct zclient *zlookup, int delay); |
| 41 | |
| 42 | /* Connect to zebra for nexthop lookup. */ |
| 43 | static int zclient_lookup_connect(struct thread *t) |
| 44 | { |
| 45 | struct zclient *zlookup; |
| 46 | |
| 47 | zlookup = THREAD_ARG(t); |
| 48 | zlookup->t_connect = NULL; |
| 49 | |
| 50 | if (zlookup->sock >= 0) { |
| 51 | return 0; |
| 52 | } |
| 53 | |
Everton Marques | c77f01b | 2014-02-13 14:54:43 -0200 | [diff] [blame] | 54 | #ifdef PIM_ZCLIENT_DEBUG |
| 55 | |
Everton Marques | 871dbcf | 2009-08-11 15:43:05 -0300 | [diff] [blame] | 56 | #ifdef HAVE_TCP_ZEBRA |
| 57 | zlog_debug("%s: FIXME blocking connect: zclient_socket()", |
| 58 | __PRETTY_FUNCTION__); |
| 59 | zlookup->sock = zclient_socket(); |
| 60 | if (zlookup->sock < 0) { |
| 61 | zlog_warn("%s: failure connecting TCP socket %s,%d", |
| 62 | __PRETTY_FUNCTION__, "127.0.0.1", ZEBRA_PORT); |
| 63 | } |
| 64 | else if (zclient_debug) { |
| 65 | zlog_notice("%s: connected TCP socket %s,%d", |
| 66 | __PRETTY_FUNCTION__, "127.0.0.1", ZEBRA_PORT); |
| 67 | } |
| 68 | #else |
| 69 | zlog_debug("%s: FIXME blocking connect: zclient_socket_un()", |
| 70 | __PRETTY_FUNCTION__); |
| 71 | zlookup->sock = zclient_socket_un(ZEBRA_SERV_PATH); |
| 72 | if (zlookup->sock < 0) { |
| 73 | zlog_warn("%s: failure connecting UNIX socket %s", |
| 74 | __PRETTY_FUNCTION__, ZEBRA_SERV_PATH); |
| 75 | } |
| 76 | else if (zclient_debug) { |
| 77 | zlog_notice("%s: connected UNIX socket %s", |
| 78 | __PRETTY_FUNCTION__, ZEBRA_SERV_PATH); |
| 79 | } |
| 80 | #endif /* HAVE_TCP_ZEBRA */ |
| 81 | |
Everton Marques | c77f01b | 2014-02-13 14:54:43 -0200 | [diff] [blame] | 82 | #else |
| 83 | |
| 84 | zlog_debug("%s: FIXME blocking connect: zclient_socket_connect()", |
| 85 | __PRETTY_FUNCTION__); |
| 86 | if (zclient_socket_connect(zlookup) < 0) { |
| 87 | zlog_warn("%s: failure connecting zclient socket", |
| 88 | __PRETTY_FUNCTION__); |
| 89 | } |
| 90 | |
| 91 | #endif /* PIM_ZCLIENT_DEBUG */ |
| 92 | |
Everton Marques | 871dbcf | 2009-08-11 15:43:05 -0300 | [diff] [blame] | 93 | zassert(!zlookup->t_connect); |
| 94 | if (zlookup->sock < 0) { |
| 95 | /* Since last connect failed, retry within 10 secs */ |
| 96 | zclient_lookup_sched(zlookup, 10); |
| 97 | return -1; |
| 98 | } |
| 99 | |
| 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | /* Schedule connection with delay. */ |
| 104 | static void zclient_lookup_sched(struct zclient *zlookup, int delay) |
| 105 | { |
| 106 | zassert(!zlookup->t_connect); |
| 107 | |
| 108 | THREAD_TIMER_ON(master, zlookup->t_connect, |
| 109 | zclient_lookup_connect, |
| 110 | zlookup, delay); |
| 111 | |
| 112 | zlog_notice("%s: zclient lookup connection scheduled for %d seconds", |
| 113 | __PRETTY_FUNCTION__, delay); |
| 114 | } |
| 115 | |
| 116 | /* Schedule connection for now. */ |
| 117 | static void zclient_lookup_sched_now(struct zclient *zlookup) |
| 118 | { |
| 119 | zassert(!zlookup->t_connect); |
| 120 | |
| 121 | zlookup->t_connect = thread_add_event(master, zclient_lookup_connect, |
| 122 | zlookup, 0); |
| 123 | |
| 124 | zlog_notice("%s: zclient lookup immediate connection scheduled", |
| 125 | __PRETTY_FUNCTION__); |
| 126 | } |
| 127 | |
| 128 | /* Schedule reconnection, if needed. */ |
| 129 | static void zclient_lookup_reconnect(struct zclient *zlookup) |
| 130 | { |
| 131 | if (zlookup->t_connect) { |
| 132 | return; |
| 133 | } |
| 134 | |
| 135 | zclient_lookup_sched_now(zlookup); |
| 136 | } |
| 137 | |
| 138 | struct zclient *zclient_lookup_new() |
| 139 | { |
| 140 | struct zclient *zlookup; |
| 141 | |
| 142 | zlookup = zclient_new(); |
| 143 | if (!zlookup) { |
| 144 | zlog_err("%s: zclient_new() failure", |
| 145 | __PRETTY_FUNCTION__); |
| 146 | return 0; |
| 147 | } |
| 148 | |
| 149 | zlookup->sock = -1; |
| 150 | zlookup->ibuf = stream_new(ZEBRA_MAX_PACKET_SIZ); |
| 151 | zlookup->obuf = stream_new(ZEBRA_MAX_PACKET_SIZ); |
| 152 | zlookup->t_connect = 0; |
| 153 | |
| 154 | zclient_lookup_sched_now(zlookup); |
| 155 | |
| 156 | zlog_notice("%s: zclient lookup socket initialized", |
| 157 | __PRETTY_FUNCTION__); |
| 158 | |
| 159 | return zlookup; |
| 160 | } |
| 161 | |
| 162 | static int zclient_read_nexthop(struct zclient *zlookup, |
| 163 | struct pim_zlookup_nexthop nexthop_tab[], |
| 164 | const int tab_size, |
| 165 | struct in_addr addr) |
| 166 | { |
| 167 | int num_ifindex = 0; |
| 168 | struct stream *s; |
| 169 | const uint16_t MIN_LEN = 14; /* getc=1 getc=1 getw=2 getipv4=4 getc=1 getl=4 getc=1 */ |
| 170 | uint16_t length, len; |
| 171 | u_char marker; |
| 172 | u_char version; |
| 173 | uint16_t command; |
| 174 | int nbytes; |
| 175 | struct in_addr raddr; |
| 176 | uint8_t distance; |
| 177 | uint32_t metric; |
| 178 | int nexthop_num; |
| 179 | int i; |
| 180 | |
| 181 | if (PIM_DEBUG_ZEBRA) { |
| 182 | char addr_str[100]; |
| 183 | pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str)); |
| 184 | zlog_debug("%s: addr=%s", |
| 185 | __PRETTY_FUNCTION__, |
| 186 | addr_str); |
| 187 | } |
| 188 | |
| 189 | s = zlookup->ibuf; |
| 190 | stream_reset(s); |
| 191 | |
| 192 | nbytes = stream_read(s, zlookup->sock, 2); |
| 193 | if (nbytes < 2) { |
| 194 | zlog_err("%s %s: failure reading zclient lookup socket: nbytes=%d", |
| 195 | __FILE__, __PRETTY_FUNCTION__, nbytes); |
| 196 | close(zlookup->sock); |
| 197 | zlookup->sock = -1; |
| 198 | zclient_lookup_reconnect(zlookup); |
| 199 | return -1; |
| 200 | } |
| 201 | length = stream_getw(s); |
| 202 | |
| 203 | len = length - 2; |
| 204 | |
| 205 | if (len < MIN_LEN) { |
| 206 | zlog_err("%s %s: failure reading zclient lookup socket: len=%d < MIN_LEN=%d", |
| 207 | __FILE__, __PRETTY_FUNCTION__, len, MIN_LEN); |
| 208 | close(zlookup->sock); |
| 209 | zlookup->sock = -1; |
| 210 | zclient_lookup_reconnect(zlookup); |
| 211 | return -2; |
| 212 | } |
| 213 | |
| 214 | nbytes = stream_read(s, zlookup->sock, len); |
| 215 | if (nbytes < (length - 2)) { |
| 216 | zlog_err("%s %s: failure reading zclient lookup socket: nbytes=%d < len=%d", |
| 217 | __FILE__, __PRETTY_FUNCTION__, nbytes, len); |
| 218 | close(zlookup->sock); |
| 219 | zlookup->sock = -1; |
| 220 | zclient_lookup_reconnect(zlookup); |
| 221 | return -3; |
| 222 | } |
| 223 | marker = stream_getc(s); |
| 224 | version = stream_getc(s); |
| 225 | |
| 226 | if (version != ZSERV_VERSION || marker != ZEBRA_HEADER_MARKER) { |
| 227 | zlog_err("%s: socket %d version mismatch, marker %d, version %d", |
| 228 | __func__, zlookup->sock, marker, version); |
| 229 | return -4; |
| 230 | } |
| 231 | |
| 232 | command = stream_getw(s); |
| 233 | if (command != ZEBRA_IPV4_NEXTHOP_LOOKUP_V2) { |
| 234 | zlog_err("%s: socket %d command mismatch: %d", |
| 235 | __func__, zlookup->sock, command); |
| 236 | return -5; |
| 237 | } |
| 238 | |
| 239 | raddr.s_addr = stream_get_ipv4(s); |
| 240 | |
| 241 | if (raddr.s_addr != addr.s_addr) { |
| 242 | char addr_str[100]; |
| 243 | char raddr_str[100]; |
| 244 | pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str)); |
| 245 | pim_inet4_dump("<raddr?>", raddr, raddr_str, sizeof(raddr_str)); |
| 246 | zlog_warn("%s: address mismatch: addr=%s raddr=%s", |
| 247 | __PRETTY_FUNCTION__, |
| 248 | addr_str, raddr_str); |
| 249 | /* warning only */ |
| 250 | } |
| 251 | |
| 252 | distance = stream_getc(s); |
| 253 | metric = stream_getl(s); |
| 254 | nexthop_num = stream_getc(s); |
| 255 | |
| 256 | if (nexthop_num < 1) { |
| 257 | zlog_err("%s: socket %d bad nexthop_num=%d", |
| 258 | __func__, zlookup->sock, nexthop_num); |
| 259 | return -6; |
| 260 | } |
| 261 | |
| 262 | len -= MIN_LEN; |
| 263 | |
| 264 | for (i = 0; i < nexthop_num; ++i) { |
| 265 | enum nexthop_types_t nexthop_type; |
| 266 | |
| 267 | if (len < 1) { |
| 268 | zlog_err("%s: socket %d empty input expecting nexthop_type: len=%d", |
| 269 | __func__, zlookup->sock, len); |
| 270 | return -7; |
| 271 | } |
| 272 | |
| 273 | nexthop_type = stream_getc(s); |
| 274 | --len; |
| 275 | |
| 276 | switch (nexthop_type) { |
| 277 | case ZEBRA_NEXTHOP_IFINDEX: |
| 278 | case ZEBRA_NEXTHOP_IFNAME: |
| 279 | case ZEBRA_NEXTHOP_IPV4_IFINDEX: |
| 280 | if (num_ifindex >= tab_size) { |
| 281 | char addr_str[100]; |
| 282 | pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str)); |
| 283 | zlog_warn("%s %s: found too many nexthop ifindexes (%d > %d) for address %s", |
| 284 | __FILE__, __PRETTY_FUNCTION__, |
| 285 | (num_ifindex + 1), tab_size, addr_str); |
| 286 | return num_ifindex; |
| 287 | } |
| 288 | if (nexthop_type == ZEBRA_NEXTHOP_IPV4_IFINDEX) { |
| 289 | if (len < 4) { |
| 290 | zlog_err("%s: socket %d short input expecting nexthop IPv4-addr: len=%d", |
| 291 | __func__, zlookup->sock, len); |
| 292 | return -8; |
| 293 | } |
| 294 | nexthop_tab[num_ifindex].nexthop_addr.s_addr = stream_get_ipv4(s); |
| 295 | len -= 4; |
| 296 | } |
| 297 | else { |
| 298 | nexthop_tab[num_ifindex].nexthop_addr.s_addr = PIM_NET_INADDR_ANY; |
| 299 | } |
| 300 | nexthop_tab[num_ifindex].ifindex = stream_getl(s); |
| 301 | nexthop_tab[num_ifindex].protocol_distance = distance; |
| 302 | nexthop_tab[num_ifindex].route_metric = metric; |
| 303 | ++num_ifindex; |
| 304 | break; |
| 305 | case ZEBRA_NEXTHOP_IPV4: |
| 306 | if (num_ifindex >= tab_size) { |
| 307 | char addr_str[100]; |
| 308 | pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str)); |
| 309 | zlog_warn("%s %s: found too many nexthop ifindexes (%d > %d) for address %s", |
| 310 | __FILE__, __PRETTY_FUNCTION__, |
| 311 | (num_ifindex + 1), tab_size, addr_str); |
| 312 | return num_ifindex; |
| 313 | } |
| 314 | nexthop_tab[num_ifindex].nexthop_addr.s_addr = stream_get_ipv4(s); |
| 315 | len -= 4; |
| 316 | nexthop_tab[num_ifindex].ifindex = 0; |
| 317 | nexthop_tab[num_ifindex].protocol_distance = distance; |
| 318 | nexthop_tab[num_ifindex].route_metric = metric; |
| 319 | { |
| 320 | char addr_str[100]; |
| 321 | char nexthop_str[100]; |
| 322 | pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str)); |
| 323 | pim_inet4_dump("<nexthop?>", nexthop_tab[num_ifindex].nexthop_addr, nexthop_str, sizeof(nexthop_str)); |
| 324 | zlog_warn("%s %s: zebra returned recursive nexthop %s for address %s", |
| 325 | __FILE__, __PRETTY_FUNCTION__, |
| 326 | nexthop_str, addr_str); |
| 327 | } |
| 328 | ++num_ifindex; |
| 329 | break; |
| 330 | default: |
| 331 | /* do nothing */ |
| 332 | { |
| 333 | char addr_str[100]; |
| 334 | pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str)); |
| 335 | zlog_warn("%s %s: found non-ifindex nexthop type=%d for address %s", |
| 336 | __FILE__, __PRETTY_FUNCTION__, |
| 337 | nexthop_type, addr_str); |
| 338 | } |
| 339 | break; |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | return num_ifindex; |
| 344 | } |
| 345 | |
| 346 | static int zclient_lookup_nexthop_once(struct zclient *zlookup, |
| 347 | struct pim_zlookup_nexthop nexthop_tab[], |
| 348 | const int tab_size, |
| 349 | struct in_addr addr) |
| 350 | { |
| 351 | struct stream *s; |
| 352 | int ret; |
| 353 | |
| 354 | if (PIM_DEBUG_ZEBRA) { |
| 355 | char addr_str[100]; |
| 356 | pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str)); |
| 357 | zlog_debug("%s: addr=%s", |
| 358 | __PRETTY_FUNCTION__, |
| 359 | addr_str); |
| 360 | } |
| 361 | |
| 362 | /* Check socket. */ |
| 363 | if (zlookup->sock < 0) { |
| 364 | zlog_err("%s %s: zclient lookup socket is not connected", |
| 365 | __FILE__, __PRETTY_FUNCTION__); |
| 366 | zclient_lookup_reconnect(zlookup); |
| 367 | return -1; |
| 368 | } |
| 369 | |
| 370 | s = zlookup->obuf; |
| 371 | stream_reset(s); |
| 372 | zclient_create_header(s, ZEBRA_IPV4_NEXTHOP_LOOKUP_V2); |
| 373 | stream_put_in_addr(s, &addr); |
| 374 | stream_putw_at(s, 0, stream_get_endp(s)); |
| 375 | |
| 376 | ret = writen(zlookup->sock, s->data, stream_get_endp(s)); |
| 377 | if (ret < 0) { |
| 378 | zlog_err("%s %s: writen() failure writing to zclient lookup socket", |
| 379 | __FILE__, __PRETTY_FUNCTION__); |
| 380 | close(zlookup->sock); |
| 381 | zlookup->sock = -1; |
| 382 | zclient_lookup_reconnect(zlookup); |
| 383 | return -2; |
| 384 | } |
| 385 | if (ret == 0) { |
| 386 | zlog_err("%s %s: connection closed on zclient lookup socket", |
| 387 | __FILE__, __PRETTY_FUNCTION__); |
| 388 | close(zlookup->sock); |
| 389 | zlookup->sock = -1; |
| 390 | zclient_lookup_reconnect(zlookup); |
| 391 | return -3; |
| 392 | } |
| 393 | |
| 394 | return zclient_read_nexthop(zlookup, nexthop_tab, |
| 395 | tab_size, addr); |
| 396 | } |
| 397 | |
| 398 | int zclient_lookup_nexthop(struct zclient *zlookup, |
| 399 | struct pim_zlookup_nexthop nexthop_tab[], |
| 400 | const int tab_size, |
| 401 | struct in_addr addr, |
| 402 | int max_lookup) |
| 403 | { |
| 404 | int lookup; |
| 405 | |
| 406 | for (lookup = 0; lookup < max_lookup; ++lookup) { |
| 407 | int num_ifindex; |
| 408 | int first_ifindex; |
| 409 | struct in_addr nexthop_addr; |
| 410 | |
| 411 | num_ifindex = zclient_lookup_nexthop_once(qpim_zclient_lookup, nexthop_tab, |
| 412 | PIM_NEXTHOP_IFINDEX_TAB_SIZE, addr); |
| 413 | if (num_ifindex < 1) { |
| 414 | char addr_str[100]; |
| 415 | pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str)); |
| 416 | zlog_warn("%s %s: lookup=%d/%d: could not find nexthop ifindex for address %s", |
| 417 | __FILE__, __PRETTY_FUNCTION__, |
| 418 | lookup, max_lookup, addr_str); |
| 419 | return -1; |
| 420 | } |
| 421 | |
| 422 | /* |
| 423 | FIXME: Non-recursive nexthop ensured only for first ifindex. |
| 424 | However, recursive route lookup should really be fixed in zebra daemon. |
| 425 | See also TODO T24. |
| 426 | */ |
| 427 | first_ifindex = nexthop_tab[0].ifindex; |
| 428 | nexthop_addr = nexthop_tab[0].nexthop_addr; |
| 429 | if (first_ifindex > 0) { |
| 430 | /* found: first ifindex is non-recursive nexthop */ |
| 431 | |
| 432 | if (lookup > 0) { |
| 433 | /* Report non-recursive success after first lookup */ |
| 434 | char addr_str[100]; |
| 435 | pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str)); |
| 436 | zlog_info("%s %s: lookup=%d/%d: found non-recursive ifindex=%d for address %s", |
| 437 | __FILE__, __PRETTY_FUNCTION__, |
| 438 | lookup, max_lookup, first_ifindex, addr_str); |
| 439 | |
| 440 | /* use last address as nexthop address */ |
| 441 | nexthop_tab[0].nexthop_addr = addr; |
| 442 | } |
| 443 | |
| 444 | return num_ifindex; |
| 445 | } |
| 446 | |
| 447 | { |
| 448 | char addr_str[100]; |
| 449 | char nexthop_str[100]; |
| 450 | pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str)); |
| 451 | pim_inet4_dump("<nexthop?>", nexthop_addr, nexthop_str, sizeof(nexthop_str)); |
| 452 | zlog_warn("%s %s: lookup=%d/%d: zebra returned recursive nexthop %s for address %s", |
| 453 | __FILE__, __PRETTY_FUNCTION__, |
| 454 | lookup, max_lookup, nexthop_str, addr_str); |
| 455 | } |
| 456 | |
| 457 | addr = nexthop_addr; /* use nexthop addr for recursive lookup */ |
| 458 | |
| 459 | } /* for (max_lookup) */ |
| 460 | |
| 461 | char addr_str[100]; |
| 462 | pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str)); |
| 463 | zlog_warn("%s %s: lookup=%d/%d: failure searching recursive nexthop ifindex for address %s", |
| 464 | __FILE__, __PRETTY_FUNCTION__, |
| 465 | lookup, max_lookup, addr_str); |
| 466 | |
| 467 | return -2; |
| 468 | } |