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