1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1983, 1988, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #include "defs.h" 33 34 __RCSID("$FreeBSD$"); 35 36 37 u_int update_seqno; 38 39 40 /* walk the tree of routes with this for output 41 */ 42 static struct { 43 struct sockaddr_in to; 44 naddr to_mask; 45 naddr to_net; 46 naddr to_std_mask; 47 naddr to_std_net; 48 struct interface *ifp; /* usually output interface */ 49 struct auth *a; 50 char metric; /* adjust metrics by interface */ 51 int npackets; 52 int gen_limit; 53 u_int state; 54 #define WS_ST_FLASH 0x001 /* send only changed routes */ 55 #define WS_ST_RIP2_ALL 0x002 /* send full featured RIPv2 */ 56 #define WS_ST_AG 0x004 /* ok to aggregate subnets */ 57 #define WS_ST_SUPER_AG 0x008 /* ok to aggregate networks */ 58 #define WS_ST_QUERY 0x010 /* responding to a query */ 59 #define WS_ST_TO_ON_NET 0x020 /* sending onto one of our nets */ 60 #define WS_ST_DEFAULT 0x040 /* faking a default */ 61 } ws; 62 63 /* A buffer for what can be heard by both RIPv1 and RIPv2 listeners */ 64 struct ws_buf v12buf; 65 static union pkt_buf ripv12_buf; 66 67 /* Another for only RIPv2 listeners */ 68 static struct ws_buf v2buf; 69 static union pkt_buf rip_v2_buf; 70 71 72 73 void 74 bufinit(void) 75 { 76 ripv12_buf.rip.rip_cmd = RIPCMD_RESPONSE; 77 v12buf.buf = &ripv12_buf.rip; 78 v12buf.base = &v12buf.buf->rip_nets[0]; 79 80 rip_v2_buf.rip.rip_cmd = RIPCMD_RESPONSE; 81 rip_v2_buf.rip.rip_vers = RIPv2; 82 v2buf.buf = &rip_v2_buf.rip; 83 v2buf.base = &v2buf.buf->rip_nets[0]; 84 } 85 86 87 /* Send the contents of the global buffer via the non-multicast socket 88 */ 89 int /* <0 on failure */ 90 output(enum output_type type, 91 struct sockaddr_in *dst, /* send to here */ 92 struct interface *ifp, 93 struct rip *buf, 94 int size) /* this many bytes */ 95 { 96 struct sockaddr_in osin; 97 int flags; 98 const char *msg; 99 int res; 100 int soc; 101 int serrno; 102 103 assert(ifp != NULL); 104 osin = *dst; 105 if (osin.sin_port == 0) 106 osin.sin_port = htons(RIP_PORT); 107 #ifdef _HAVE_SIN_LEN 108 if (osin.sin_len == 0) 109 osin.sin_len = sizeof(osin); 110 #endif 111 112 soc = rip_sock; 113 flags = 0; 114 115 switch (type) { 116 case OUT_QUERY: 117 msg = "Answer Query"; 118 if (soc < 0) 119 soc = ifp->int_rip_sock; 120 break; 121 case OUT_UNICAST: 122 msg = "Send"; 123 if (soc < 0) 124 soc = ifp->int_rip_sock; 125 flags = MSG_DONTROUTE; 126 break; 127 case OUT_BROADCAST: 128 if (ifp->int_if_flags & IFF_POINTOPOINT) { 129 msg = "Send"; 130 } else { 131 msg = "Send bcast"; 132 } 133 flags = MSG_DONTROUTE; 134 break; 135 case OUT_MULTICAST: 136 if ((ifp->int_if_flags & (IFF_POINTOPOINT|IFF_MULTICAST)) == 137 IFF_POINTOPOINT) { 138 msg = "Send pt-to-pt"; 139 } else if (ifp->int_state & IS_DUP) { 140 trace_act("abort multicast output via %s" 141 " with duplicate address", 142 ifp->int_name); 143 return 0; 144 } else { 145 msg = "Send mcast"; 146 if (rip_sock_mcast != ifp) { 147 struct ip_mreqn mreqn; 148 149 memset(&mreqn, 0, sizeof(struct ip_mreqn)); 150 mreqn.imr_ifindex = ifp->int_index; 151 if (0 > setsockopt(rip_sock, 152 IPPROTO_IP, 153 IP_MULTICAST_IF, 154 &mreqn, 155 sizeof(mreqn))) { 156 serrno = errno; 157 LOGERR("setsockopt(rip_sock, " 158 "IP_MULTICAST_IF)"); 159 errno = serrno; 160 ifp = NULL; 161 return -1; 162 } 163 rip_sock_mcast = ifp; 164 } 165 osin.sin_addr.s_addr = htonl(INADDR_RIP_GROUP); 166 } 167 break; 168 169 case NO_OUT_MULTICAST: 170 case NO_OUT_RIPV2: 171 default: 172 #ifdef DEBUG 173 abort(); 174 #endif 175 return -1; 176 } 177 178 trace_rip(msg, "to", &osin, ifp, buf, size); 179 180 res = sendto(soc, buf, size, flags, 181 (struct sockaddr *)&osin, sizeof(osin)); 182 if (res < 0 183 && (ifp == NULL || !(ifp->int_state & IS_BROKE))) { 184 serrno = errno; 185 msglog("%s sendto(%s%s%s.%d): %s", msg, 186 ifp != NULL ? ifp->int_name : "", 187 ifp != NULL ? ", " : "", 188 inet_ntoa(osin.sin_addr), 189 ntohs(osin.sin_port), 190 strerror(errno)); 191 errno = serrno; 192 } 193 194 return res; 195 } 196 197 198 /* Find the first key for a packet to send. 199 * Try for a key that is eligible and has not expired, but settle for 200 * the last key if they have all expired. 201 * If no key is ready yet, give up. 202 */ 203 struct auth * 204 find_auth(struct interface *ifp) 205 { 206 struct auth *ap, *res; 207 int i; 208 209 210 if (ifp == NULL) 211 return 0; 212 213 res = NULL; 214 ap = ifp->int_auth; 215 for (i = 0; i < MAX_AUTH_KEYS; i++, ap++) { 216 /* stop looking after the last key */ 217 if (ap->type == RIP_AUTH_NONE) 218 break; 219 220 /* ignore keys that are not ready yet */ 221 if ((u_long)ap->start > (u_long)clk.tv_sec) 222 continue; 223 224 if ((u_long)ap->end < (u_long)clk.tv_sec) { 225 /* note best expired password as a fall-back */ 226 if (res == NULL || (u_long)ap->end > (u_long)res->end) 227 res = ap; 228 continue; 229 } 230 231 /* note key with the best future */ 232 if (res == NULL || (u_long)res->end < (u_long)ap->end) 233 res = ap; 234 } 235 return res; 236 } 237 238 239 void 240 clr_ws_buf(struct ws_buf *wb, 241 struct auth *ap) 242 { 243 struct netauth *na; 244 245 wb->lim = wb->base + NETS_LEN; 246 wb->n = wb->base; 247 memset(wb->n, 0, NETS_LEN*sizeof(*wb->n)); 248 249 /* (start to) install authentication if appropriate 250 */ 251 if (ap == NULL) 252 return; 253 254 na = (struct netauth*)wb->n; 255 if (ap->type == RIP_AUTH_PW) { 256 na->a_family = RIP_AF_AUTH; 257 na->a_type = RIP_AUTH_PW; 258 memcpy(na->au.au_pw, ap->key, sizeof(na->au.au_pw)); 259 wb->n++; 260 261 } else if (ap->type == RIP_AUTH_MD5) { 262 na->a_family = RIP_AF_AUTH; 263 na->a_type = RIP_AUTH_MD5; 264 na->au.a_md5.md5_keyid = ap->keyid; 265 na->au.a_md5.md5_auth_len = RIP_AUTH_MD5_KEY_LEN; 266 na->au.a_md5.md5_seqno = htonl(clk.tv_sec); 267 wb->n++; 268 wb->lim--; /* make room for trailer */ 269 } 270 } 271 272 273 void 274 end_md5_auth(struct ws_buf *wb, 275 struct auth *ap) 276 { 277 struct netauth *na, *na2; 278 MD5_CTX md5_ctx; 279 int len; 280 281 282 na = (struct netauth*)wb->base; 283 na2 = (struct netauth*)wb->n; 284 len = (char *)na2-(char *)wb->buf; 285 na2->a_family = RIP_AF_AUTH; 286 na2->a_type = htons(1); 287 na->au.a_md5.md5_pkt_len = htons(len); 288 MD5Init(&md5_ctx); 289 MD5Update(&md5_ctx, (u_char *)wb->buf, len + RIP_AUTH_MD5_HASH_XTRA); 290 MD5Update(&md5_ctx, ap->key, RIP_AUTH_MD5_KEY_LEN); 291 MD5Final(na2->au.au_pw, &md5_ctx); 292 wb->n++; 293 } 294 295 296 /* Send the buffer 297 */ 298 static void 299 supply_write(struct ws_buf *wb) 300 { 301 /* Output multicast only if legal. 302 * If we would multicast and it would be illegal, then discard the 303 * packet. 304 */ 305 switch (wb->type) { 306 case NO_OUT_MULTICAST: 307 trace_pkt("skip multicast to %s because impossible", 308 naddr_ntoa(ws.to.sin_addr.s_addr)); 309 break; 310 case NO_OUT_RIPV2: 311 break; 312 default: 313 if (ws.a != NULL && ws.a->type == RIP_AUTH_MD5) 314 end_md5_auth(wb,ws.a); 315 if (output(wb->type, &ws.to, ws.ifp, wb->buf, 316 ((char *)wb->n - (char*)wb->buf)) < 0 317 && ws.ifp != NULL) 318 if_sick(ws.ifp); 319 ws.npackets++; 320 break; 321 } 322 323 clr_ws_buf(wb,ws.a); 324 } 325 326 327 /* put an entry into the packet 328 */ 329 static void 330 supply_out(struct ag_info *ag) 331 { 332 int i; 333 naddr mask, v1_mask, dst_h, ddst_h = 0; 334 struct ws_buf *wb; 335 336 337 /* Skip this route if doing a flash update and it and the routes 338 * it aggregates have not changed recently. 339 */ 340 if (ag->ag_seqno < update_seqno 341 && (ws.state & WS_ST_FLASH)) 342 return; 343 344 dst_h = ag->ag_dst_h; 345 mask = ag->ag_mask; 346 v1_mask = ripv1_mask_host(htonl(dst_h), 347 (ws.state & WS_ST_TO_ON_NET) ? ws.ifp : 0); 348 i = 0; 349 350 /* If we are sending RIPv2 packets that cannot (or must not) be 351 * heard by RIPv1 listeners, do not worry about sub- or supernets. 352 * Subnets (from other networks) can only be sent via multicast. 353 * A pair of subnet routes might have been promoted so that they 354 * are legal to send by RIPv1. 355 * If RIPv1 is off, use the multicast buffer. 356 */ 357 if ((ws.state & WS_ST_RIP2_ALL) 358 || ((ag->ag_state & AGS_RIPV2) && v1_mask != mask)) { 359 /* use the RIPv2-only buffer */ 360 wb = &v2buf; 361 362 } else { 363 /* use the RIPv1-or-RIPv2 buffer */ 364 wb = &v12buf; 365 366 /* Convert supernet route into corresponding set of network 367 * routes for RIPv1, but leave non-contiguous netmasks 368 * to ag_check(). 369 */ 370 if (v1_mask > mask 371 && mask + (mask & -mask) == 0) { 372 ddst_h = v1_mask & -v1_mask; 373 i = (v1_mask & ~mask)/ddst_h; 374 375 if (i > ws.gen_limit) { 376 /* Punt if we would have to generate an 377 * unreasonable number of routes. 378 */ 379 if (TRACECONTENTS) 380 trace_misc("sending %s-->%s as 1" 381 " instead of %d routes", 382 addrname(htonl(dst_h), mask, 383 1), 384 naddr_ntoa(ws.to.sin_addr 385 .s_addr), 386 i+1); 387 i = 0; 388 389 } else { 390 mask = v1_mask; 391 ws.gen_limit -= i; 392 } 393 } 394 } 395 396 do { 397 wb->n->n_family = RIP_AF_INET; 398 wb->n->n_dst = htonl(dst_h); 399 /* If the route is from router-discovery or we are 400 * shutting down, admit only a bad metric. 401 */ 402 wb->n->n_metric = ((stopint || ag->ag_metric < 1) 403 ? HOPCNT_INFINITY 404 : ag->ag_metric); 405 wb->n->n_metric = htonl(wb->n->n_metric); 406 /* Any non-zero bits in the supposedly unused RIPv1 fields 407 * cause the old `routed` to ignore the route. 408 * That means the mask and so forth cannot be sent 409 * in the hybrid RIPv1/RIPv2 mode. 410 */ 411 if (ws.state & WS_ST_RIP2_ALL) { 412 if (ag->ag_nhop != 0 413 && ((ws.state & WS_ST_QUERY) 414 || (ag->ag_nhop != ws.ifp->int_addr 415 && on_net(ag->ag_nhop, 416 ws.ifp->int_net, 417 ws.ifp->int_mask)))) 418 wb->n->n_nhop = ag->ag_nhop; 419 wb->n->n_mask = htonl(mask); 420 wb->n->n_tag = ag->ag_tag; 421 } 422 dst_h += ddst_h; 423 424 if (++wb->n >= wb->lim) 425 supply_write(wb); 426 } while (i-- != 0); 427 } 428 429 430 /* supply one route from the table 431 */ 432 /* ARGSUSED */ 433 static int 434 walk_supply(struct radix_node *rn, 435 struct walkarg *argp UNUSED) 436 { 437 #define RT ((struct rt_entry *)rn) 438 u_short ags; 439 char metric, pref; 440 naddr dst, nhop; 441 struct rt_spare *rts; 442 int i; 443 444 445 /* Do not advertise external remote interfaces or passive interfaces. 446 */ 447 if ((RT->rt_state & RS_IF) 448 && RT->rt_ifp != 0 449 && (RT->rt_ifp->int_state & IS_PASSIVE) 450 && !(RT->rt_state & RS_MHOME)) 451 return 0; 452 453 /* If being quiet about our ability to forward, then 454 * do not say anything unless responding to a query, 455 * except about our main interface. 456 */ 457 if (!supplier && !(ws.state & WS_ST_QUERY) 458 && !(RT->rt_state & RS_MHOME)) 459 return 0; 460 461 dst = RT->rt_dst; 462 463 /* do not collide with the fake default route */ 464 if (dst == RIP_DEFAULT 465 && (ws.state & WS_ST_DEFAULT)) 466 return 0; 467 468 if (RT->rt_state & RS_NET_SYN) { 469 if (RT->rt_state & RS_NET_INT) { 470 /* Do not send manual synthetic network routes 471 * into the subnet. 472 */ 473 if (on_net(ws.to.sin_addr.s_addr, 474 ntohl(dst), RT->rt_mask)) 475 return 0; 476 477 } else { 478 /* Do not send automatic synthetic network routes 479 * if they are not needed because no RIPv1 listeners 480 * can hear them. 481 */ 482 if (ws.state & WS_ST_RIP2_ALL) 483 return 0; 484 485 /* Do not send automatic synthetic network routes to 486 * the real subnet. 487 */ 488 if (on_net(ws.to.sin_addr.s_addr, 489 ntohl(dst), RT->rt_mask)) 490 return 0; 491 } 492 nhop = 0; 493 494 } else { 495 /* Advertise the next hop if this is not a route for one 496 * of our interfaces and the next hop is on the same 497 * network as the target. 498 * The final determination is made by supply_out(). 499 */ 500 if (!(RT->rt_state & RS_IF) 501 && RT->rt_gate != myaddr 502 && RT->rt_gate != loopaddr) 503 nhop = RT->rt_gate; 504 else 505 nhop = 0; 506 } 507 508 metric = RT->rt_metric; 509 ags = 0; 510 511 if (RT->rt_state & RS_MHOME) { 512 /* retain host route of multi-homed servers */ 513 ; 514 515 } else if (RT_ISHOST(RT)) { 516 /* We should always suppress (into existing network routes) 517 * the host routes for the local end of our point-to-point 518 * links. 519 * If we are suppressing host routes in general, then do so. 520 * Avoid advertising host routes onto their own network, 521 * where they should be handled by proxy-ARP. 522 */ 523 if ((RT->rt_state & RS_LOCAL) 524 || ridhosts 525 || on_net(dst, ws.to_net, ws.to_mask)) 526 ags |= AGS_SUPPRESS; 527 528 /* Aggregate stray host routes into network routes if allowed. 529 * We cannot aggregate host routes into small network routes 530 * without confusing RIPv1 listeners into thinking the 531 * network routes are host routes. 532 */ 533 if ((ws.state & WS_ST_AG) && (ws.state & WS_ST_RIP2_ALL)) 534 ags |= AGS_AGGREGATE; 535 536 } else { 537 /* Always suppress network routes into other, existing 538 * network routes 539 */ 540 ags |= AGS_SUPPRESS; 541 542 /* Generate supernets if allowed. 543 * If we can be heard by RIPv1 systems, we will 544 * later convert back to ordinary nets. 545 * This unifies dealing with received supernets. 546 */ 547 if ((ws.state & WS_ST_AG) 548 && ((RT->rt_state & RS_SUBNET) 549 || (ws.state & WS_ST_SUPER_AG))) 550 ags |= AGS_AGGREGATE; 551 } 552 553 /* Do not send RIPv1 advertisements of subnets to other 554 * networks. If possible, multicast them by RIPv2. 555 */ 556 if ((RT->rt_state & RS_SUBNET) 557 && !(ws.state & WS_ST_RIP2_ALL) 558 && !on_net(dst, ws.to_std_net, ws.to_std_mask)) 559 ags |= AGS_RIPV2 | AGS_AGGREGATE; 560 561 562 /* Do not send a route back to where it came from, except in 563 * response to a query. This is "split-horizon". That means not 564 * advertising back to the same network and so via the same interface. 565 * 566 * We want to suppress routes that might have been fragmented 567 * from this route by a RIPv1 router and sent back to us, and so we 568 * cannot forget this route here. Let the split-horizon route 569 * suppress the fragmented routes and then itself be forgotten. 570 * 571 * Include the routes for both ends of point-to-point interfaces 572 * among those suppressed by split-horizon, since the other side 573 * should knows them as well as we do. 574 * 575 * Notice spare routes with the same metric that we are about to 576 * advertise, to split the horizon on redundant, inactive paths. 577 * 578 * Do not suppress advertisements of interface-related addresses on 579 * non-point-to-point interfaces. This ensures that we have something 580 * to say every 30 seconds to help detect broken Ethernets or 581 * other interfaces where one packet every 30 seconds costs nothing. 582 */ 583 if (ws.ifp != NULL 584 && !(ws.state & WS_ST_QUERY) 585 && (ws.state & WS_ST_TO_ON_NET) 586 && (!(RT->rt_state & RS_IF) 587 || ws.ifp->int_if_flags & IFF_POINTOPOINT)) { 588 for (rts = RT->rt_spares, i = NUM_SPARES; i != 0; i--, rts++) { 589 if (rts->rts_metric > metric 590 || rts->rts_ifp != ws.ifp) 591 continue; 592 593 /* If we do not mark the route with AGS_SPLIT_HZ here, 594 * it will be poisoned-reverse, or advertised back 595 * toward its source with an infinite metric. 596 * If we have recently advertised the route with a 597 * better metric than we now have, then we should 598 * poison-reverse the route before suppressing it for 599 * split-horizon. 600 * 601 * In almost all cases, if there is no spare for the 602 * route then it is either old and dead or a brand 603 * new route. If it is brand new, there is no need 604 * for poison-reverse. If it is old and dead, it 605 * is already poisoned. 606 */ 607 if (RT->rt_poison_time < now_expire 608 || RT->rt_poison_metric >= metric 609 || RT->rt_spares[1].rts_gate == 0) { 610 ags |= AGS_SPLIT_HZ; 611 ags &= ~AGS_SUPPRESS; 612 } 613 metric = HOPCNT_INFINITY; 614 break; 615 } 616 } 617 618 /* Keep track of the best metric with which the 619 * route has been advertised recently. 620 */ 621 if (RT->rt_poison_metric >= metric 622 || RT->rt_poison_time < now_expire) { 623 RT->rt_poison_time = now.tv_sec; 624 RT->rt_poison_metric = metric; 625 } 626 627 /* Adjust the outgoing metric by the cost of the link. 628 * Avoid aggregation when a route is counting to infinity. 629 */ 630 pref = RT->rt_poison_metric + ws.metric; 631 metric += ws.metric; 632 633 /* Do not advertise stable routes that will be ignored, 634 * unless we are answering a query. 635 * If the route recently was advertised with a metric that 636 * would have been less than infinity through this interface, 637 * we need to continue to advertise it in order to poison it. 638 */ 639 if (metric >= HOPCNT_INFINITY) { 640 if (!(ws.state & WS_ST_QUERY) 641 && (pref >= HOPCNT_INFINITY 642 || RT->rt_poison_time < now_garbage)) 643 return 0; 644 645 metric = HOPCNT_INFINITY; 646 } 647 648 ag_check(dst, RT->rt_mask, 0, nhop, metric, pref, 649 RT->rt_seqno, RT->rt_tag, ags, supply_out); 650 return 0; 651 #undef RT 652 } 653 654 655 /* Supply dst with the contents of the routing tables. 656 * If this won't fit in one packet, chop it up into several. 657 */ 658 void 659 supply(struct sockaddr_in *dst, 660 struct interface *ifp, /* output interface */ 661 enum output_type type, 662 int flash, /* 1=flash update */ 663 int vers, /* RIP version */ 664 int passwd_ok) /* OK to include cleartext password */ 665 { 666 struct rt_entry *rt; 667 int def_metric; 668 669 ws.state = 0; 670 ws.gen_limit = 1024; 671 672 ws.to = *dst; 673 ws.to_std_mask = std_mask(ws.to.sin_addr.s_addr); 674 ws.to_std_net = ntohl(ws.to.sin_addr.s_addr) & ws.to_std_mask; 675 676 if (ifp != NULL) { 677 ws.to_mask = ifp->int_mask; 678 ws.to_net = ifp->int_net; 679 if (on_net(ws.to.sin_addr.s_addr, ws.to_net, ws.to_mask)) 680 ws.state |= WS_ST_TO_ON_NET; 681 682 } else { 683 ws.to_mask = ripv1_mask_net(ws.to.sin_addr.s_addr, 0); 684 ws.to_net = ntohl(ws.to.sin_addr.s_addr) & ws.to_mask; 685 rt = rtfind(dst->sin_addr.s_addr); 686 if (rt) 687 ifp = rt->rt_ifp; 688 } 689 690 ws.npackets = 0; 691 if (flash) 692 ws.state |= WS_ST_FLASH; 693 694 if ((ws.ifp = ifp) == NULL) { 695 ws.metric = 1; 696 } else { 697 /* Adjust the advertised metric by the outgoing interface 698 * metric. 699 */ 700 ws.metric = ifp->int_metric + 1 + ifp->int_adj_outmetric; 701 } 702 703 ripv12_buf.rip.rip_vers = vers; 704 705 switch (type) { 706 case OUT_MULTICAST: 707 if (ifp != NULL && ifp->int_if_flags & IFF_MULTICAST) 708 v2buf.type = OUT_MULTICAST; 709 else 710 v2buf.type = NO_OUT_MULTICAST; 711 v12buf.type = OUT_BROADCAST; 712 break; 713 714 case OUT_QUERY: 715 ws.state |= WS_ST_QUERY; 716 /* FALLTHROUGH */ 717 case OUT_BROADCAST: 718 case OUT_UNICAST: 719 v2buf.type = (vers == RIPv2) ? type : NO_OUT_RIPV2; 720 v12buf.type = type; 721 break; 722 723 case NO_OUT_MULTICAST: 724 case NO_OUT_RIPV2: 725 break; /* no output */ 726 } 727 728 if (vers == RIPv2) { 729 /* full RIPv2 only if cannot be heard by RIPv1 listeners */ 730 if (type != OUT_BROADCAST) 731 ws.state |= WS_ST_RIP2_ALL; 732 if ((ws.state & WS_ST_QUERY) 733 || !(ws.state & WS_ST_TO_ON_NET)) { 734 ws.state |= (WS_ST_AG | WS_ST_SUPER_AG); 735 } else if (ifp == NULL || !(ifp->int_state & IS_NO_AG)) { 736 ws.state |= WS_ST_AG; 737 if (type != OUT_BROADCAST 738 && (ifp == NULL 739 || !(ifp->int_state & IS_NO_SUPER_AG))) 740 ws.state |= WS_ST_SUPER_AG; 741 } 742 } 743 744 ws.a = (vers == RIPv2) ? find_auth(ifp) : 0; 745 if (!passwd_ok && ws.a != NULL && ws.a->type == RIP_AUTH_PW) 746 ws.a = NULL; 747 clr_ws_buf(&v12buf,ws.a); 748 clr_ws_buf(&v2buf,ws.a); 749 750 /* Fake a default route if asked and if there is not already 751 * a better, real default route. 752 */ 753 if (supplier && ifp && (def_metric = ifp->int_d_metric) != 0) { 754 if ((rt = rtget(RIP_DEFAULT, 0)) == NULL 755 || rt->rt_metric+ws.metric >= def_metric) { 756 ws.state |= WS_ST_DEFAULT; 757 ag_check(0, 0, 0, 0, def_metric, def_metric, 758 0, 0, 0, supply_out); 759 } else { 760 def_metric = rt->rt_metric+ws.metric; 761 } 762 763 /* If both RIPv2 and the poor-man's router discovery 764 * kludge are on, arrange to advertise an extra 765 * default route via RIPv1. 766 */ 767 if ((ws.state & WS_ST_RIP2_ALL) 768 && (ifp->int_state & IS_PM_RDISC)) { 769 ripv12_buf.rip.rip_vers = RIPv1; 770 v12buf.n->n_family = RIP_AF_INET; 771 v12buf.n->n_dst = htonl(RIP_DEFAULT); 772 v12buf.n->n_metric = htonl(def_metric); 773 v12buf.n++; 774 } 775 } 776 777 (void)rn_walktree(rhead, walk_supply, 0); 778 ag_flush(0,0,supply_out); 779 780 /* Flush the packet buffers, provided they are not empty and 781 * do not contain only the password. 782 */ 783 if (v12buf.n != v12buf.base 784 && (v12buf.n > v12buf.base+1 785 || v12buf.base->n_family != RIP_AF_AUTH)) 786 supply_write(&v12buf); 787 if (v2buf.n != v2buf.base 788 && (v2buf.n > v2buf.base+1 789 || v2buf.base->n_family != RIP_AF_AUTH)) 790 supply_write(&v2buf); 791 792 /* If we sent nothing and this is an answer to a query, send 793 * an empty buffer. 794 */ 795 if (ws.npackets == 0 796 && (ws.state & WS_ST_QUERY)) 797 supply_write(&v12buf); 798 } 799 800 801 /* send all of the routing table or just do a flash update 802 */ 803 void 804 rip_bcast(int flash) 805 { 806 #ifdef _HAVE_SIN_LEN 807 static struct sockaddr_in dst = {sizeof(dst), AF_INET, 0, {0}, {0}}; 808 #else 809 static struct sockaddr_in dst = {AF_INET}; 810 #endif 811 struct interface *ifp; 812 enum output_type type; 813 int vers; 814 struct timeval rtime; 815 816 817 need_flash = 0; 818 intvl_random(&rtime, MIN_WAITTIME, MAX_WAITTIME); 819 no_flash = rtime; 820 timevaladd(&no_flash, &now); 821 822 if (rip_sock < 0) 823 return; 824 825 trace_act("send %s and inhibit dynamic updates for %.3f sec", 826 flash ? "dynamic update" : "all routes", 827 rtime.tv_sec + ((float)rtime.tv_usec)/1000000.0); 828 829 LIST_FOREACH(ifp, &ifnet, int_list) { 830 /* Skip interfaces not doing RIP. 831 * Do try broken interfaces to see if they have healed. 832 */ 833 if (IS_RIP_OUT_OFF(ifp->int_state)) 834 continue; 835 836 /* skip turned off interfaces */ 837 if (!iff_up(ifp->int_if_flags)) 838 continue; 839 840 vers = (ifp->int_state & IS_NO_RIPV1_OUT) ? RIPv2 : RIPv1; 841 842 if (ifp->int_if_flags & IFF_BROADCAST) { 843 /* ordinary, hardware interface */ 844 dst.sin_addr.s_addr = ifp->int_brdaddr; 845 846 if (vers == RIPv2 847 && !(ifp->int_state & IS_NO_RIP_MCAST)) { 848 type = OUT_MULTICAST; 849 } else { 850 type = OUT_BROADCAST; 851 } 852 853 } else if (ifp->int_if_flags & IFF_POINTOPOINT) { 854 /* point-to-point hardware interface */ 855 dst.sin_addr.s_addr = ifp->int_dstaddr; 856 if (vers == RIPv2 && 857 ifp->int_if_flags & IFF_MULTICAST && 858 !(ifp->int_state & IS_NO_RIP_MCAST)) { 859 type = OUT_MULTICAST; 860 } else { 861 type = OUT_UNICAST; 862 } 863 864 } else if (ifp->int_state & IS_REMOTE) { 865 /* remote interface */ 866 dst.sin_addr.s_addr = ifp->int_addr; 867 type = OUT_UNICAST; 868 869 } else { 870 /* ATM, HIPPI, etc. */ 871 continue; 872 } 873 874 supply(&dst, ifp, type, flash, vers, 1); 875 } 876 877 update_seqno++; /* all routes are up to date */ 878 } 879 880 881 /* Ask for routes 882 * Do it only once to an interface, and not even after the interface 883 * was broken and recovered. 884 */ 885 void 886 rip_query(void) 887 { 888 #ifdef _HAVE_SIN_LEN 889 static struct sockaddr_in dst = {sizeof(dst), AF_INET, 0, {0}, {0}}; 890 #else 891 static struct sockaddr_in dst = {AF_INET}; 892 #endif 893 struct interface *ifp; 894 struct rip buf; 895 enum output_type type; 896 897 898 if (rip_sock < 0) 899 return; 900 901 memset(&buf, 0, sizeof(buf)); 902 903 LIST_FOREACH(ifp, &ifnet, int_list) { 904 /* Skip interfaces those already queried. 905 * Do not ask via interfaces through which we don't 906 * accept input. Do not ask via interfaces that cannot 907 * send RIP packets. 908 * Do try broken interfaces to see if they have healed. 909 */ 910 if (IS_RIP_IN_OFF(ifp->int_state) 911 || ifp->int_query_time != NEVER) 912 continue; 913 914 /* skip turned off interfaces */ 915 if (!iff_up(ifp->int_if_flags)) 916 continue; 917 918 buf.rip_vers = (ifp->int_state&IS_NO_RIPV1_OUT) ? RIPv2:RIPv1; 919 buf.rip_cmd = RIPCMD_REQUEST; 920 buf.rip_nets[0].n_family = RIP_AF_UNSPEC; 921 buf.rip_nets[0].n_metric = htonl(HOPCNT_INFINITY); 922 923 /* Send a RIPv1 query only if allowed and if we will 924 * listen to RIPv1 routers. 925 */ 926 if ((ifp->int_state & IS_NO_RIPV1_OUT) 927 || (ifp->int_state & IS_NO_RIPV1_IN)) { 928 buf.rip_vers = RIPv2; 929 } else { 930 buf.rip_vers = RIPv1; 931 } 932 933 if (ifp->int_if_flags & IFF_BROADCAST) { 934 /* ordinary, hardware interface */ 935 dst.sin_addr.s_addr = ifp->int_brdaddr; 936 937 /* Broadcast RIPv1 queries and RIPv2 queries 938 * when the hardware cannot multicast. 939 */ 940 if (buf.rip_vers == RIPv2 941 && (ifp->int_if_flags & IFF_MULTICAST) 942 && !(ifp->int_state & IS_NO_RIP_MCAST)) { 943 type = OUT_MULTICAST; 944 } else { 945 type = OUT_BROADCAST; 946 } 947 948 } else if (ifp->int_if_flags & IFF_POINTOPOINT) { 949 /* point-to-point hardware interface */ 950 dst.sin_addr.s_addr = ifp->int_dstaddr; 951 type = OUT_UNICAST; 952 953 } else if (ifp->int_state & IS_REMOTE) { 954 /* remote interface */ 955 dst.sin_addr.s_addr = ifp->int_addr; 956 type = OUT_UNICAST; 957 958 } else { 959 /* ATM, HIPPI, etc. */ 960 continue; 961 } 962 963 ifp->int_query_time = now.tv_sec+SUPPLY_INTERVAL; 964 if (output(type, &dst, ifp, &buf, sizeof(buf)) < 0) 965 if_sick(ifp); 966 } 967 } 968