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