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 | if (zclient_socket_connect(zlookup) < 0) { |
| 55 | zlog_warn("%s: failure connecting zclient socket", |
| 56 | __PRETTY_FUNCTION__); |
| 57 | } |
| 58 | |
Everton Marques | 871dbcf | 2009-08-11 15:43:05 -0300 | [diff] [blame] | 59 | zassert(!zlookup->t_connect); |
| 60 | if (zlookup->sock < 0) { |
| 61 | /* Since last connect failed, retry within 10 secs */ |
| 62 | zclient_lookup_sched(zlookup, 10); |
| 63 | return -1; |
| 64 | } |
| 65 | |
| 66 | return 0; |
| 67 | } |
| 68 | |
| 69 | /* Schedule connection with delay. */ |
| 70 | static void zclient_lookup_sched(struct zclient *zlookup, int delay) |
| 71 | { |
| 72 | zassert(!zlookup->t_connect); |
| 73 | |
| 74 | THREAD_TIMER_ON(master, zlookup->t_connect, |
| 75 | zclient_lookup_connect, |
| 76 | zlookup, delay); |
| 77 | |
| 78 | zlog_notice("%s: zclient lookup connection scheduled for %d seconds", |
| 79 | __PRETTY_FUNCTION__, delay); |
| 80 | } |
| 81 | |
| 82 | /* Schedule connection for now. */ |
| 83 | static void zclient_lookup_sched_now(struct zclient *zlookup) |
| 84 | { |
| 85 | zassert(!zlookup->t_connect); |
| 86 | |
| 87 | zlookup->t_connect = thread_add_event(master, zclient_lookup_connect, |
| 88 | zlookup, 0); |
| 89 | |
| 90 | zlog_notice("%s: zclient lookup immediate connection scheduled", |
| 91 | __PRETTY_FUNCTION__); |
| 92 | } |
| 93 | |
| 94 | /* Schedule reconnection, if needed. */ |
| 95 | static void zclient_lookup_reconnect(struct zclient *zlookup) |
| 96 | { |
| 97 | if (zlookup->t_connect) { |
| 98 | return; |
| 99 | } |
| 100 | |
| 101 | zclient_lookup_sched_now(zlookup); |
| 102 | } |
| 103 | |
| 104 | struct zclient *zclient_lookup_new() |
| 105 | { |
| 106 | struct zclient *zlookup; |
| 107 | |
| 108 | zlookup = zclient_new(); |
| 109 | if (!zlookup) { |
| 110 | zlog_err("%s: zclient_new() failure", |
| 111 | __PRETTY_FUNCTION__); |
| 112 | return 0; |
| 113 | } |
| 114 | |
| 115 | zlookup->sock = -1; |
| 116 | zlookup->ibuf = stream_new(ZEBRA_MAX_PACKET_SIZ); |
| 117 | zlookup->obuf = stream_new(ZEBRA_MAX_PACKET_SIZ); |
| 118 | zlookup->t_connect = 0; |
| 119 | |
| 120 | zclient_lookup_sched_now(zlookup); |
| 121 | |
| 122 | zlog_notice("%s: zclient lookup socket initialized", |
| 123 | __PRETTY_FUNCTION__); |
| 124 | |
| 125 | return zlookup; |
| 126 | } |
| 127 | |
| 128 | static int zclient_read_nexthop(struct zclient *zlookup, |
| 129 | struct pim_zlookup_nexthop nexthop_tab[], |
| 130 | const int tab_size, |
| 131 | struct in_addr addr) |
| 132 | { |
| 133 | int num_ifindex = 0; |
| 134 | struct stream *s; |
| 135 | const uint16_t MIN_LEN = 14; /* getc=1 getc=1 getw=2 getipv4=4 getc=1 getl=4 getc=1 */ |
| 136 | uint16_t length, len; |
| 137 | u_char marker; |
| 138 | u_char version; |
| 139 | uint16_t command; |
| 140 | int nbytes; |
| 141 | struct in_addr raddr; |
| 142 | uint8_t distance; |
| 143 | uint32_t metric; |
| 144 | int nexthop_num; |
| 145 | int i; |
| 146 | |
| 147 | if (PIM_DEBUG_ZEBRA) { |
| 148 | char addr_str[100]; |
| 149 | pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str)); |
| 150 | zlog_debug("%s: addr=%s", |
| 151 | __PRETTY_FUNCTION__, |
| 152 | addr_str); |
| 153 | } |
| 154 | |
| 155 | s = zlookup->ibuf; |
| 156 | stream_reset(s); |
| 157 | |
| 158 | nbytes = stream_read(s, zlookup->sock, 2); |
| 159 | if (nbytes < 2) { |
| 160 | zlog_err("%s %s: failure reading zclient lookup socket: nbytes=%d", |
| 161 | __FILE__, __PRETTY_FUNCTION__, nbytes); |
| 162 | close(zlookup->sock); |
| 163 | zlookup->sock = -1; |
| 164 | zclient_lookup_reconnect(zlookup); |
| 165 | return -1; |
| 166 | } |
| 167 | length = stream_getw(s); |
| 168 | |
| 169 | len = length - 2; |
| 170 | |
| 171 | if (len < MIN_LEN) { |
| 172 | zlog_err("%s %s: failure reading zclient lookup socket: len=%d < MIN_LEN=%d", |
| 173 | __FILE__, __PRETTY_FUNCTION__, len, MIN_LEN); |
| 174 | close(zlookup->sock); |
| 175 | zlookup->sock = -1; |
| 176 | zclient_lookup_reconnect(zlookup); |
| 177 | return -2; |
| 178 | } |
| 179 | |
| 180 | nbytes = stream_read(s, zlookup->sock, len); |
| 181 | if (nbytes < (length - 2)) { |
| 182 | zlog_err("%s %s: failure reading zclient lookup socket: nbytes=%d < len=%d", |
| 183 | __FILE__, __PRETTY_FUNCTION__, nbytes, len); |
| 184 | close(zlookup->sock); |
| 185 | zlookup->sock = -1; |
| 186 | zclient_lookup_reconnect(zlookup); |
| 187 | return -3; |
| 188 | } |
| 189 | marker = stream_getc(s); |
| 190 | version = stream_getc(s); |
| 191 | |
| 192 | if (version != ZSERV_VERSION || marker != ZEBRA_HEADER_MARKER) { |
| 193 | zlog_err("%s: socket %d version mismatch, marker %d, version %d", |
| 194 | __func__, zlookup->sock, marker, version); |
| 195 | return -4; |
| 196 | } |
| 197 | |
| 198 | command = stream_getw(s); |
Everton Marques | 04c833a | 2014-09-22 19:35:24 -0300 | [diff] [blame] | 199 | if (command != ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB) { |
Everton Marques | 871dbcf | 2009-08-11 15:43:05 -0300 | [diff] [blame] | 200 | zlog_err("%s: socket %d command mismatch: %d", |
| 201 | __func__, zlookup->sock, command); |
| 202 | return -5; |
| 203 | } |
| 204 | |
| 205 | raddr.s_addr = stream_get_ipv4(s); |
| 206 | |
| 207 | if (raddr.s_addr != addr.s_addr) { |
| 208 | char addr_str[100]; |
| 209 | char raddr_str[100]; |
| 210 | pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str)); |
| 211 | pim_inet4_dump("<raddr?>", raddr, raddr_str, sizeof(raddr_str)); |
| 212 | zlog_warn("%s: address mismatch: addr=%s raddr=%s", |
| 213 | __PRETTY_FUNCTION__, |
| 214 | addr_str, raddr_str); |
| 215 | /* warning only */ |
| 216 | } |
| 217 | |
| 218 | distance = stream_getc(s); |
| 219 | metric = stream_getl(s); |
| 220 | nexthop_num = stream_getc(s); |
| 221 | |
| 222 | if (nexthop_num < 1) { |
| 223 | zlog_err("%s: socket %d bad nexthop_num=%d", |
| 224 | __func__, zlookup->sock, nexthop_num); |
| 225 | return -6; |
| 226 | } |
| 227 | |
| 228 | len -= MIN_LEN; |
| 229 | |
| 230 | for (i = 0; i < nexthop_num; ++i) { |
| 231 | enum nexthop_types_t nexthop_type; |
| 232 | |
| 233 | if (len < 1) { |
| 234 | zlog_err("%s: socket %d empty input expecting nexthop_type: len=%d", |
| 235 | __func__, zlookup->sock, len); |
| 236 | return -7; |
| 237 | } |
| 238 | |
| 239 | nexthop_type = stream_getc(s); |
| 240 | --len; |
| 241 | |
| 242 | switch (nexthop_type) { |
| 243 | case ZEBRA_NEXTHOP_IFINDEX: |
| 244 | case ZEBRA_NEXTHOP_IFNAME: |
| 245 | case ZEBRA_NEXTHOP_IPV4_IFINDEX: |
| 246 | if (num_ifindex >= tab_size) { |
| 247 | char addr_str[100]; |
| 248 | pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str)); |
| 249 | zlog_warn("%s %s: found too many nexthop ifindexes (%d > %d) for address %s", |
| 250 | __FILE__, __PRETTY_FUNCTION__, |
| 251 | (num_ifindex + 1), tab_size, addr_str); |
| 252 | return num_ifindex; |
| 253 | } |
| 254 | if (nexthop_type == ZEBRA_NEXTHOP_IPV4_IFINDEX) { |
| 255 | if (len < 4) { |
| 256 | zlog_err("%s: socket %d short input expecting nexthop IPv4-addr: len=%d", |
| 257 | __func__, zlookup->sock, len); |
| 258 | return -8; |
| 259 | } |
| 260 | nexthop_tab[num_ifindex].nexthop_addr.s_addr = stream_get_ipv4(s); |
| 261 | len -= 4; |
| 262 | } |
| 263 | else { |
| 264 | nexthop_tab[num_ifindex].nexthop_addr.s_addr = PIM_NET_INADDR_ANY; |
| 265 | } |
| 266 | nexthop_tab[num_ifindex].ifindex = stream_getl(s); |
| 267 | nexthop_tab[num_ifindex].protocol_distance = distance; |
| 268 | nexthop_tab[num_ifindex].route_metric = metric; |
| 269 | ++num_ifindex; |
| 270 | break; |
| 271 | case ZEBRA_NEXTHOP_IPV4: |
| 272 | if (num_ifindex >= tab_size) { |
| 273 | char addr_str[100]; |
| 274 | pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str)); |
| 275 | zlog_warn("%s %s: found too many nexthop ifindexes (%d > %d) for address %s", |
| 276 | __FILE__, __PRETTY_FUNCTION__, |
| 277 | (num_ifindex + 1), tab_size, addr_str); |
| 278 | return num_ifindex; |
| 279 | } |
| 280 | nexthop_tab[num_ifindex].nexthop_addr.s_addr = stream_get_ipv4(s); |
| 281 | len -= 4; |
| 282 | nexthop_tab[num_ifindex].ifindex = 0; |
| 283 | nexthop_tab[num_ifindex].protocol_distance = distance; |
| 284 | nexthop_tab[num_ifindex].route_metric = metric; |
| 285 | { |
| 286 | char addr_str[100]; |
| 287 | char nexthop_str[100]; |
| 288 | pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str)); |
| 289 | pim_inet4_dump("<nexthop?>", nexthop_tab[num_ifindex].nexthop_addr, nexthop_str, sizeof(nexthop_str)); |
| 290 | zlog_warn("%s %s: zebra returned recursive nexthop %s for address %s", |
| 291 | __FILE__, __PRETTY_FUNCTION__, |
| 292 | nexthop_str, addr_str); |
| 293 | } |
| 294 | ++num_ifindex; |
| 295 | break; |
| 296 | default: |
| 297 | /* do nothing */ |
| 298 | { |
| 299 | char addr_str[100]; |
| 300 | pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str)); |
| 301 | zlog_warn("%s %s: found non-ifindex nexthop type=%d for address %s", |
| 302 | __FILE__, __PRETTY_FUNCTION__, |
| 303 | nexthop_type, addr_str); |
| 304 | } |
| 305 | break; |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | return num_ifindex; |
| 310 | } |
| 311 | |
| 312 | static int zclient_lookup_nexthop_once(struct zclient *zlookup, |
| 313 | struct pim_zlookup_nexthop nexthop_tab[], |
| 314 | const int tab_size, |
| 315 | struct in_addr addr) |
| 316 | { |
| 317 | struct stream *s; |
| 318 | int ret; |
| 319 | |
| 320 | if (PIM_DEBUG_ZEBRA) { |
| 321 | char addr_str[100]; |
| 322 | pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str)); |
| 323 | zlog_debug("%s: addr=%s", |
| 324 | __PRETTY_FUNCTION__, |
| 325 | addr_str); |
| 326 | } |
| 327 | |
| 328 | /* Check socket. */ |
| 329 | if (zlookup->sock < 0) { |
| 330 | zlog_err("%s %s: zclient lookup socket is not connected", |
| 331 | __FILE__, __PRETTY_FUNCTION__); |
| 332 | zclient_lookup_reconnect(zlookup); |
| 333 | return -1; |
| 334 | } |
| 335 | |
| 336 | s = zlookup->obuf; |
| 337 | stream_reset(s); |
Everton Marques | 04c833a | 2014-09-22 19:35:24 -0300 | [diff] [blame] | 338 | zclient_create_header(s, ZEBRA_IPV4_NEXTHOP_LOOKUP_MRIB); |
Everton Marques | 871dbcf | 2009-08-11 15:43:05 -0300 | [diff] [blame] | 339 | stream_put_in_addr(s, &addr); |
| 340 | stream_putw_at(s, 0, stream_get_endp(s)); |
| 341 | |
| 342 | ret = writen(zlookup->sock, s->data, stream_get_endp(s)); |
| 343 | if (ret < 0) { |
| 344 | zlog_err("%s %s: writen() failure writing to zclient lookup socket", |
| 345 | __FILE__, __PRETTY_FUNCTION__); |
| 346 | close(zlookup->sock); |
| 347 | zlookup->sock = -1; |
| 348 | zclient_lookup_reconnect(zlookup); |
| 349 | return -2; |
| 350 | } |
| 351 | if (ret == 0) { |
| 352 | zlog_err("%s %s: connection closed on zclient lookup socket", |
| 353 | __FILE__, __PRETTY_FUNCTION__); |
| 354 | close(zlookup->sock); |
| 355 | zlookup->sock = -1; |
| 356 | zclient_lookup_reconnect(zlookup); |
| 357 | return -3; |
| 358 | } |
| 359 | |
| 360 | return zclient_read_nexthop(zlookup, nexthop_tab, |
| 361 | tab_size, addr); |
| 362 | } |
| 363 | |
| 364 | int zclient_lookup_nexthop(struct zclient *zlookup, |
| 365 | struct pim_zlookup_nexthop nexthop_tab[], |
| 366 | const int tab_size, |
| 367 | struct in_addr addr, |
| 368 | int max_lookup) |
| 369 | { |
| 370 | int lookup; |
Everton Marques | e4f2a2b | 2014-07-16 12:19:40 -0300 | [diff] [blame] | 371 | uint32_t route_metric = 0xFFFFFFFF; |
| 372 | uint8_t protocol_distance = 0xFF; |
Everton Marques | 871dbcf | 2009-08-11 15:43:05 -0300 | [diff] [blame] | 373 | |
| 374 | for (lookup = 0; lookup < max_lookup; ++lookup) { |
| 375 | int num_ifindex; |
| 376 | int first_ifindex; |
| 377 | struct in_addr nexthop_addr; |
| 378 | |
| 379 | num_ifindex = zclient_lookup_nexthop_once(qpim_zclient_lookup, nexthop_tab, |
| 380 | PIM_NEXTHOP_IFINDEX_TAB_SIZE, addr); |
| 381 | if (num_ifindex < 1) { |
| 382 | char addr_str[100]; |
| 383 | pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str)); |
| 384 | zlog_warn("%s %s: lookup=%d/%d: could not find nexthop ifindex for address %s", |
| 385 | __FILE__, __PRETTY_FUNCTION__, |
| 386 | lookup, max_lookup, addr_str); |
| 387 | return -1; |
| 388 | } |
Everton Marques | e4f2a2b | 2014-07-16 12:19:40 -0300 | [diff] [blame] | 389 | |
| 390 | if (lookup < 1) { |
| 391 | /* this is the non-recursive lookup - save original metric/distance */ |
| 392 | route_metric = nexthop_tab[0].route_metric; |
| 393 | protocol_distance = nexthop_tab[0].protocol_distance; |
| 394 | } |
Everton Marques | 871dbcf | 2009-08-11 15:43:05 -0300 | [diff] [blame] | 395 | |
| 396 | /* |
| 397 | FIXME: Non-recursive nexthop ensured only for first ifindex. |
| 398 | However, recursive route lookup should really be fixed in zebra daemon. |
| 399 | See also TODO T24. |
| 400 | */ |
| 401 | first_ifindex = nexthop_tab[0].ifindex; |
| 402 | nexthop_addr = nexthop_tab[0].nexthop_addr; |
| 403 | if (first_ifindex > 0) { |
| 404 | /* found: first ifindex is non-recursive nexthop */ |
| 405 | |
| 406 | if (lookup > 0) { |
| 407 | /* Report non-recursive success after first lookup */ |
| 408 | char addr_str[100]; |
| 409 | pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str)); |
Everton Marques | e4f2a2b | 2014-07-16 12:19:40 -0300 | [diff] [blame] | 410 | zlog_info("%s %s: lookup=%d/%d: found non-recursive ifindex=%d for address %s dist=%d met=%d", |
Everton Marques | 871dbcf | 2009-08-11 15:43:05 -0300 | [diff] [blame] | 411 | __FILE__, __PRETTY_FUNCTION__, |
Everton Marques | e4f2a2b | 2014-07-16 12:19:40 -0300 | [diff] [blame] | 412 | lookup, max_lookup, first_ifindex, addr_str, |
| 413 | nexthop_tab[0].protocol_distance, |
| 414 | nexthop_tab[0].route_metric); |
Everton Marques | 871dbcf | 2009-08-11 15:43:05 -0300 | [diff] [blame] | 415 | |
| 416 | /* use last address as nexthop address */ |
| 417 | nexthop_tab[0].nexthop_addr = addr; |
Everton Marques | e4f2a2b | 2014-07-16 12:19:40 -0300 | [diff] [blame] | 418 | |
| 419 | /* report original route metric/distance */ |
| 420 | nexthop_tab[0].route_metric = route_metric; |
| 421 | nexthop_tab[0].protocol_distance = protocol_distance; |
Everton Marques | 871dbcf | 2009-08-11 15:43:05 -0300 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | return num_ifindex; |
| 425 | } |
| 426 | |
| 427 | { |
| 428 | char addr_str[100]; |
| 429 | char nexthop_str[100]; |
| 430 | pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str)); |
| 431 | pim_inet4_dump("<nexthop?>", nexthop_addr, nexthop_str, sizeof(nexthop_str)); |
Everton Marques | e4f2a2b | 2014-07-16 12:19:40 -0300 | [diff] [blame] | 432 | zlog_warn("%s %s: lookup=%d/%d: zebra returned recursive nexthop %s for address %s dist=%d met=%d", |
Everton Marques | 871dbcf | 2009-08-11 15:43:05 -0300 | [diff] [blame] | 433 | __FILE__, __PRETTY_FUNCTION__, |
Everton Marques | e4f2a2b | 2014-07-16 12:19:40 -0300 | [diff] [blame] | 434 | lookup, max_lookup, nexthop_str, addr_str, |
| 435 | nexthop_tab[0].protocol_distance, |
| 436 | nexthop_tab[0].route_metric); |
Everton Marques | 871dbcf | 2009-08-11 15:43:05 -0300 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | addr = nexthop_addr; /* use nexthop addr for recursive lookup */ |
| 440 | |
| 441 | } /* for (max_lookup) */ |
| 442 | |
| 443 | char addr_str[100]; |
| 444 | pim_inet4_dump("<addr?>", addr, addr_str, sizeof(addr_str)); |
| 445 | zlog_warn("%s %s: lookup=%d/%d: failure searching recursive nexthop ifindex for address %s", |
| 446 | __FILE__, __PRETTY_FUNCTION__, |
| 447 | lookup, max_lookup, addr_str); |
| 448 | |
| 449 | return -2; |
| 450 | } |