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