1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 1995 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 #include <netinet/in_systm.h> 34 #include <netinet/ip.h> 35 #include <netinet/ip_icmp.h> 36 37 __RCSID("$FreeBSD$"); 38 39 /* router advertisement ICMP packet */ 40 struct icmp_ad { 41 u_int8_t icmp_type; /* type of message */ 42 u_int8_t icmp_code; /* type sub code */ 43 u_int16_t icmp_cksum; /* ones complement cksum of struct */ 44 u_int8_t icmp_ad_num; /* # of following router addresses */ 45 u_int8_t icmp_ad_asize; /* 2--words in each advertisement */ 46 u_int16_t icmp_ad_life; /* seconds of validity */ 47 struct icmp_ad_info { 48 n_long icmp_ad_addr; 49 n_long icmp_ad_pref; 50 } icmp_ad_info[1]; 51 }; 52 53 /* router solicitation ICMP packet */ 54 struct icmp_so { 55 u_int8_t icmp_type; /* type of message */ 56 u_int8_t icmp_code; /* type sub code */ 57 u_int16_t icmp_cksum; /* ones complement cksum of struct */ 58 n_long icmp_so_rsvd; 59 }; 60 61 union ad_u { 62 struct icmp icmp; 63 struct icmp_ad ad; 64 struct icmp_so so; 65 }; 66 67 68 int rdisc_sock = -1; /* router-discovery raw socket */ 69 static const struct interface *rdisc_sock_mcast; /* current multicast interface */ 70 71 struct timeval rdisc_timer; 72 int rdisc_ok; /* using solicited route */ 73 74 75 #define MAX_ADS 16 /* at least one per interface */ 76 struct dr { /* accumulated advertisements */ 77 struct interface *dr_ifp; 78 naddr dr_gate; /* gateway */ 79 time_t dr_ts; /* when received */ 80 time_t dr_life; /* lifetime in host byte order */ 81 n_long dr_recv_pref; /* received but biased preference */ 82 n_long dr_pref; /* preference adjusted by metric */ 83 }; 84 static const struct dr *cur_drp; 85 static struct dr drs[MAX_ADS]; 86 87 /* convert between signed, balanced around zero, 88 * and unsigned zero-based preferences */ 89 #define SIGN_PREF(p) ((p) ^ MIN_PreferenceLevel) 90 #define UNSIGN_PREF(p) SIGN_PREF(p) 91 /* adjust unsigned preference by interface metric, 92 * without driving it to infinity */ 93 #define PREF(p, ifp) ((int)(p) <= ((ifp)->int_metric+(ifp)->int_adj_outmetric)\ 94 ? ((p) != 0 ? 1 : 0) \ 95 : (p) - ((ifp)->int_metric+(ifp)->int_adj_outmetric)) 96 97 static void rdisc_sort(void); 98 99 100 /* dump an ICMP Router Discovery Advertisement Message 101 */ 102 static void 103 trace_rdisc(const char *act, 104 naddr from, 105 naddr to, 106 struct interface *ifp, 107 union ad_u *p, 108 u_int len) 109 { 110 int i; 111 n_long *wp, *lim; 112 113 114 if (!TRACEPACKETS || ftrace == NULL) 115 return; 116 117 lastlog(); 118 119 if (p->icmp.icmp_type == ICMP_ROUTERADVERT) { 120 (void)fprintf(ftrace, "%s Router Ad" 121 " from %s to %s via %s life=%d\n", 122 act, naddr_ntoa(from), naddr_ntoa(to), 123 ifp ? ifp->int_name : "?", 124 ntohs(p->ad.icmp_ad_life)); 125 if (!TRACECONTENTS) 126 return; 127 128 wp = &p->ad.icmp_ad_info[0].icmp_ad_addr; 129 lim = &wp[(len - sizeof(p->ad)) / sizeof(*wp)]; 130 for (i = 0; i < p->ad.icmp_ad_num && wp <= lim; i++) { 131 (void)fprintf(ftrace, "\t%s preference=%d", 132 naddr_ntoa(wp[0]), (int)ntohl(wp[1])); 133 wp += p->ad.icmp_ad_asize; 134 } 135 (void)fputc('\n',ftrace); 136 137 } else { 138 trace_act("%s Router Solic. from %s to %s via %s value=%#x", 139 act, naddr_ntoa(from), naddr_ntoa(to), 140 ifp ? ifp->int_name : "?", 141 (int)ntohl(p->so.icmp_so_rsvd)); 142 } 143 } 144 145 /* prepare Router Discovery socket. 146 */ 147 static void 148 get_rdisc_sock(void) 149 { 150 if (rdisc_sock < 0) { 151 rdisc_sock = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP); 152 if (rdisc_sock < 0) 153 BADERR(1,"rdisc_sock = socket()"); 154 fix_sock(rdisc_sock,"rdisc_sock"); 155 fix_select(); 156 } 157 } 158 159 160 /* Pick multicast group for router-discovery socket 161 */ 162 void 163 set_rdisc_mg(struct interface *ifp, 164 int on) /* 0=turn it off */ 165 { 166 struct group_req gr; 167 struct sockaddr_in *sin; 168 169 assert(ifp != NULL); 170 171 if (rdisc_sock < 0) { 172 /* Create the raw socket so that we can hear at least 173 * broadcast router discovery packets. 174 */ 175 if ((ifp->int_state & IS_NO_RDISC) == IS_NO_RDISC 176 || !on) 177 return; 178 get_rdisc_sock(); 179 } 180 181 if (!(ifp->int_if_flags & IFF_MULTICAST)) { 182 ifp->int_state &= ~(IS_ALL_HOSTS | IS_ALL_ROUTERS); 183 return; 184 } 185 186 memset(&gr, 0, sizeof(gr)); 187 gr.gr_interface = ifp->int_index; 188 sin = (struct sockaddr_in *)&gr.gr_group; 189 sin->sin_family = AF_INET; 190 #ifdef _HAVE_SIN_LEN 191 sin->sin_len = sizeof(struct sockaddr_in); 192 #endif 193 194 if (supplier 195 || (ifp->int_state & IS_NO_ADV_IN) 196 || !on) { 197 /* stop listening to advertisements 198 */ 199 if (ifp->int_state & IS_ALL_HOSTS) { 200 sin->sin_addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP); 201 if (setsockopt(rdisc_sock, IPPROTO_IP, 202 MCAST_LEAVE_GROUP, 203 &gr, sizeof(gr)) < 0) 204 LOGERR("MCAST_LEAVE_GROUP ALLHOSTS"); 205 ifp->int_state &= ~IS_ALL_HOSTS; 206 } 207 208 } else if (!(ifp->int_state & IS_ALL_HOSTS)) { 209 /* start listening to advertisements 210 */ 211 sin->sin_addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP); 212 if (setsockopt(rdisc_sock, IPPROTO_IP, MCAST_JOIN_GROUP, 213 &gr, sizeof(gr)) < 0) { 214 LOGERR("MCAST_JOIN_GROUP ALLHOSTS"); 215 } else { 216 ifp->int_state |= IS_ALL_HOSTS; 217 } 218 } 219 220 if (!supplier 221 || (ifp->int_state & IS_NO_ADV_OUT) 222 || !on) { 223 /* stop listening to solicitations 224 */ 225 if (ifp->int_state & IS_ALL_ROUTERS) { 226 sin->sin_addr.s_addr = htonl(INADDR_ALLROUTERS_GROUP); 227 if (setsockopt(rdisc_sock, IPPROTO_IP, 228 MCAST_LEAVE_GROUP, 229 &gr, sizeof(gr)) < 0) 230 LOGERR("MCAST_LEAVE_GROUP ALLROUTERS"); 231 ifp->int_state &= ~IS_ALL_ROUTERS; 232 } 233 234 } else if (!(ifp->int_state & IS_ALL_ROUTERS)) { 235 /* start hearing solicitations 236 */ 237 sin->sin_addr.s_addr = htonl(INADDR_ALLROUTERS_GROUP); 238 if (setsockopt(rdisc_sock, IPPROTO_IP, MCAST_JOIN_GROUP, 239 &gr, sizeof(gr)) < 0) { 240 LOGERR("MCAST_JOIN_GROUP ALLROUTERS"); 241 } else { 242 ifp->int_state |= IS_ALL_ROUTERS; 243 } 244 } 245 } 246 247 248 /* start supplying routes 249 */ 250 void 251 set_supplier(void) 252 { 253 struct interface *ifp; 254 struct dr *drp; 255 256 if (supplier_set) 257 return; 258 259 trace_act("start supplying routes"); 260 261 /* Forget discovered routes. 262 */ 263 for (drp = drs; drp < &drs[MAX_ADS]; drp++) { 264 drp->dr_recv_pref = 0; 265 drp->dr_life = 0; 266 } 267 rdisc_age(0); 268 269 supplier_set = 1; 270 supplier = 1; 271 272 /* Do not start advertising until we have heard some RIP routes */ 273 LIM_SEC(rdisc_timer, now.tv_sec+MIN_WAITTIME); 274 275 /* Switch router discovery multicast groups from soliciting 276 * to advertising. 277 */ 278 LIST_FOREACH(ifp, &ifnet, int_list) { 279 if (ifp->int_state & IS_BROKE) 280 continue; 281 ifp->int_rdisc_cnt = 0; 282 ifp->int_rdisc_timer.tv_usec = rdisc_timer.tv_usec; 283 ifp->int_rdisc_timer.tv_sec = now.tv_sec+MIN_WAITTIME; 284 set_rdisc_mg(ifp, 1); 285 } 286 287 /* get rid of any redirects */ 288 del_redirects(0,0); 289 } 290 291 292 /* age discovered routes and find the best one 293 */ 294 void 295 rdisc_age(naddr bad_gate) 296 { 297 time_t sec; 298 struct dr *drp; 299 300 301 /* If only advertising, then do only that. */ 302 if (supplier) { 303 /* If switching from client to server, get rid of old 304 * default routes. 305 */ 306 if (cur_drp != NULL) 307 rdisc_sort(); 308 rdisc_adv(); 309 return; 310 } 311 312 /* If we are being told about a bad router, 313 * then age the discovered default route, and if there is 314 * no alternative, solicit a replacement. 315 */ 316 if (bad_gate != 0) { 317 /* Look for the bad discovered default route. 318 * Age it and note its interface. 319 */ 320 for (drp = drs; drp < &drs[MAX_ADS]; drp++) { 321 if (drp->dr_ts == 0) 322 continue; 323 324 /* When we find the bad router, then age the route 325 * to at most SUPPLY_INTERVAL. 326 * This is contrary to RFC 1256, but defends against 327 * black holes. 328 */ 329 if (drp->dr_gate == bad_gate) { 330 sec = (now.tv_sec - drp->dr_life 331 + SUPPLY_INTERVAL); 332 if (drp->dr_ts > sec) { 333 trace_act("age 0.0.0.0 --> %s via %s", 334 naddr_ntoa(drp->dr_gate), 335 drp->dr_ifp->int_name); 336 drp->dr_ts = sec; 337 } 338 break; 339 } 340 } 341 } 342 343 rdisc_sol(); 344 rdisc_sort(); 345 346 /* Delete old redirected routes to keep the kernel table small, 347 * and to prevent black holes. Check that the kernel table 348 * matches the daemon table (i.e. has the default route). 349 * But only if RIP is not running and we are not dealing with 350 * a bad gateway, since otherwise age() will be called. 351 */ 352 if (rip_sock < 0 && bad_gate == 0) 353 age(0); 354 } 355 356 357 /* Zap all routes discovered via an interface that has gone bad 358 * This should only be called when !(ifp->int_state & IS_ALIAS) 359 */ 360 void 361 if_bad_rdisc(struct interface *ifp) 362 { 363 struct dr *drp; 364 365 for (drp = drs; drp < &drs[MAX_ADS]; drp++) { 366 if (drp->dr_ifp != ifp) 367 continue; 368 drp->dr_recv_pref = 0; 369 drp->dr_ts = 0; 370 drp->dr_life = 0; 371 } 372 373 /* make a note to re-solicit, turn RIP on or off, etc. */ 374 rdisc_timer.tv_sec = 0; 375 } 376 377 378 /* mark an interface ok for router discovering. 379 */ 380 void 381 if_ok_rdisc(struct interface *ifp) 382 { 383 set_rdisc_mg(ifp, 1); 384 385 ifp->int_rdisc_cnt = 0; 386 ifp->int_rdisc_timer.tv_sec = now.tv_sec + (supplier 387 ? MIN_WAITTIME 388 : MAX_SOLICITATION_DELAY); 389 if (timercmp(&rdisc_timer, &ifp->int_rdisc_timer, >)) 390 rdisc_timer = ifp->int_rdisc_timer; 391 } 392 393 394 /* get rid of a dead discovered router 395 */ 396 static void 397 del_rdisc(struct dr *drp) 398 { 399 struct interface *ifp; 400 naddr gate; 401 int i; 402 403 404 del_redirects(gate = drp->dr_gate, 0); 405 drp->dr_ts = 0; 406 drp->dr_life = 0; 407 408 409 /* Count the other discovered routes on the interface. 410 */ 411 i = 0; 412 ifp = drp->dr_ifp; 413 for (drp = drs; drp < &drs[MAX_ADS]; drp++) { 414 if (drp->dr_ts != 0 415 && drp->dr_ifp == ifp) 416 i++; 417 } 418 419 /* If that was the last good discovered router on the interface, 420 * then solicit a new one. 421 * This is contrary to RFC 1256, but defends against black holes. 422 */ 423 if (i != 0) { 424 trace_act("discovered router %s via %s" 425 " is bad--have %d remaining", 426 naddr_ntoa(gate), ifp->int_name, i); 427 } else if (ifp->int_rdisc_cnt >= MAX_SOLICITATIONS) { 428 trace_act("last discovered router %s via %s" 429 " is bad--re-solicit", 430 naddr_ntoa(gate), ifp->int_name); 431 ifp->int_rdisc_cnt = 0; 432 ifp->int_rdisc_timer.tv_sec = 0; 433 rdisc_sol(); 434 } else { 435 trace_act("last discovered router %s via %s" 436 " is bad--wait to solicit", 437 naddr_ntoa(gate), ifp->int_name); 438 } 439 } 440 441 442 /* Find the best discovered route, 443 * and discard stale routers. 444 */ 445 static void 446 rdisc_sort(void) 447 { 448 struct dr *drp, *new_drp; 449 struct rt_entry *rt; 450 struct rt_spare new; 451 struct interface *ifp; 452 u_int new_st = 0; 453 n_long new_pref = 0; 454 455 456 /* Find the best discovered route. 457 */ 458 new_drp = NULL; 459 for (drp = drs; drp < &drs[MAX_ADS]; drp++) { 460 if (drp->dr_ts == 0) 461 continue; 462 ifp = drp->dr_ifp; 463 464 /* Get rid of expired discovered routers. 465 */ 466 if (drp->dr_ts + drp->dr_life <= now.tv_sec) { 467 del_rdisc(drp); 468 continue; 469 } 470 471 LIM_SEC(rdisc_timer, drp->dr_ts+drp->dr_life+1); 472 473 /* Update preference with possibly changed interface 474 * metric. 475 */ 476 drp->dr_pref = PREF(drp->dr_recv_pref, ifp); 477 478 /* Prefer the current route to prevent thrashing. 479 * Prefer shorter lifetimes to speed the detection of 480 * bad routers. 481 * Avoid sick interfaces. 482 */ 483 if (new_drp == NULL 484 || (!((new_st ^ drp->dr_ifp->int_state) & IS_SICK) 485 && (new_pref < drp->dr_pref 486 || (new_pref == drp->dr_pref 487 && (drp == cur_drp 488 || (new_drp != cur_drp 489 && new_drp->dr_life > drp->dr_life))))) 490 || ((new_st & IS_SICK) 491 && !(drp->dr_ifp->int_state & IS_SICK))) { 492 new_drp = drp; 493 new_st = drp->dr_ifp->int_state; 494 new_pref = drp->dr_pref; 495 } 496 } 497 498 /* switch to a better default route 499 */ 500 if (new_drp != cur_drp) { 501 rt = rtget(RIP_DEFAULT, 0); 502 503 /* Stop using discovered routes if they are all bad 504 */ 505 if (new_drp == NULL) { 506 trace_act("turn off Router Discovery client"); 507 rdisc_ok = 0; 508 509 if (rt != NULL 510 && (rt->rt_state & RS_RDISC)) { 511 new = rt->rt_spares[0]; 512 new.rts_metric = HOPCNT_INFINITY; 513 new.rts_time = now.tv_sec - GARBAGE_TIME; 514 rtchange(rt, rt->rt_state & ~RS_RDISC, 515 &new, 0); 516 rtswitch(rt, 0); 517 } 518 519 } else { 520 if (cur_drp == NULL) { 521 trace_act("turn on Router Discovery client" 522 " using %s via %s", 523 naddr_ntoa(new_drp->dr_gate), 524 new_drp->dr_ifp->int_name); 525 rdisc_ok = 1; 526 527 } else { 528 trace_act("switch Router Discovery from" 529 " %s via %s to %s via %s", 530 naddr_ntoa(cur_drp->dr_gate), 531 cur_drp->dr_ifp->int_name, 532 naddr_ntoa(new_drp->dr_gate), 533 new_drp->dr_ifp->int_name); 534 } 535 536 memset(&new, 0, sizeof(new)); 537 new.rts_ifp = new_drp->dr_ifp; 538 new.rts_gate = new_drp->dr_gate; 539 new.rts_router = new_drp->dr_gate; 540 new.rts_metric = HOPCNT_INFINITY-1; 541 new.rts_time = now.tv_sec; 542 if (rt != NULL) { 543 rtchange(rt, rt->rt_state | RS_RDISC, &new, 0); 544 } else { 545 rtadd(RIP_DEFAULT, 0, RS_RDISC, &new); 546 } 547 } 548 549 cur_drp = new_drp; 550 } 551 552 /* turn RIP on or off */ 553 if (!rdisc_ok || rip_interfaces > 1) { 554 rip_on(0); 555 } else { 556 rip_off(); 557 } 558 } 559 560 561 /* handle a single address in an advertisement 562 */ 563 static void 564 parse_ad(naddr from, 565 naddr gate, 566 n_long pref, /* signed and in network order */ 567 u_short life, /* in host byte order */ 568 struct interface *ifp) 569 { 570 static struct msg_limit bad_gate; 571 struct dr *drp, *new_drp; 572 573 574 if (gate == RIP_DEFAULT 575 || !check_dst(gate)) { 576 msglim(&bad_gate, from,"router %s advertising bad gateway %s", 577 naddr_ntoa(from), 578 naddr_ntoa(gate)); 579 return; 580 } 581 582 /* ignore pointers to ourself and routes via unreachable networks 583 */ 584 if (ifwithaddr(gate, 1, 0) != NULL) { 585 trace_pkt(" discard Router Discovery Ad pointing at us"); 586 return; 587 } 588 if (!on_net(gate, ifp->int_net, ifp->int_mask)) { 589 trace_pkt(" discard Router Discovery Ad" 590 " toward unreachable net"); 591 return; 592 } 593 594 /* Convert preference to an unsigned value 595 * and later bias it by the metric of the interface. 596 */ 597 pref = UNSIGN_PREF(ntohl(pref)); 598 599 if (pref == 0 || life < MinMaxAdvertiseInterval) { 600 pref = 0; 601 life = 0; 602 } 603 604 for (new_drp = NULL, drp = drs; drp < &drs[MAX_ADS]; drp++) { 605 /* accept new info for a familiar entry 606 */ 607 if (drp->dr_gate == gate) { 608 new_drp = drp; 609 break; 610 } 611 612 if (life == 0) 613 continue; /* do not worry about dead ads */ 614 615 if (drp->dr_ts == 0) { 616 new_drp = drp; /* use unused entry */ 617 618 } else if (new_drp == NULL) { 619 /* look for an entry worse than the new one to 620 * reuse. 621 */ 622 if ((!(ifp->int_state & IS_SICK) 623 && (drp->dr_ifp->int_state & IS_SICK)) 624 || (pref > drp->dr_pref 625 && !((ifp->int_state ^ drp->dr_ifp->int_state) 626 & IS_SICK))) 627 new_drp = drp; 628 629 } else if (new_drp->dr_ts != 0) { 630 /* look for the least valuable entry to reuse 631 */ 632 if ((!(new_drp->dr_ifp->int_state & IS_SICK) 633 && (drp->dr_ifp->int_state & IS_SICK)) 634 || (new_drp->dr_pref > drp->dr_pref 635 && !((new_drp->dr_ifp->int_state 636 ^ drp->dr_ifp->int_state) 637 & IS_SICK))) 638 new_drp = drp; 639 } 640 } 641 642 /* forget it if all of the current entries are better */ 643 if (new_drp == NULL) 644 return; 645 646 new_drp->dr_ifp = ifp; 647 new_drp->dr_gate = gate; 648 new_drp->dr_ts = now.tv_sec; 649 new_drp->dr_life = life; 650 new_drp->dr_recv_pref = pref; 651 /* bias functional preference by metric of the interface */ 652 new_drp->dr_pref = PREF(pref,ifp); 653 654 /* after hearing a good advertisement, stop asking 655 */ 656 if (!(ifp->int_state & IS_SICK)) 657 ifp->int_rdisc_cnt = MAX_SOLICITATIONS; 658 } 659 660 661 /* Compute the IP checksum 662 * This assumes the packet is less than 32K long. 663 */ 664 static u_short 665 in_cksum(u_short *p, 666 u_int len) 667 { 668 u_int sum = 0; 669 int nwords = len >> 1; 670 671 while (nwords-- != 0) 672 sum += *p++; 673 674 if (len & 1) 675 sum += *(u_char *)p; 676 677 /* end-around-carry */ 678 sum = (sum >> 16) + (sum & 0xffff); 679 sum += (sum >> 16); 680 return (~sum); 681 } 682 683 684 /* Send a router discovery advertisement or solicitation ICMP packet. 685 */ 686 static void 687 send_rdisc(union ad_u *p, 688 int p_size, 689 struct interface *ifp, 690 naddr dst, /* 0 or unicast destination */ 691 int type) /* 0=unicast, 1=bcast, 2=mcast */ 692 { 693 struct sockaddr_in rsin; 694 int flags; 695 const char *msg; 696 697 698 memset(&rsin, 0, sizeof(rsin)); 699 rsin.sin_addr.s_addr = dst; 700 rsin.sin_family = AF_INET; 701 #ifdef _HAVE_SIN_LEN 702 rsin.sin_len = sizeof(rsin); 703 #endif 704 flags = MSG_DONTROUTE; 705 706 switch (type) { 707 case 0: /* unicast */ 708 default: 709 msg = "Send"; 710 break; 711 712 case 1: /* broadcast */ 713 if (ifp->int_if_flags & IFF_POINTOPOINT) { 714 msg = "Send pt-to-pt"; 715 rsin.sin_addr.s_addr = ifp->int_dstaddr; 716 } else { 717 msg = "Send broadcast"; 718 rsin.sin_addr.s_addr = ifp->int_brdaddr; 719 } 720 break; 721 722 case 2: /* multicast */ 723 msg = "Send multicast"; 724 if (ifp->int_state & IS_DUP) { 725 trace_act("abort multicast output via %s" 726 " with duplicate address", 727 ifp->int_name); 728 return; 729 } 730 if (rdisc_sock_mcast != ifp) { 731 /* select the right interface. */ 732 struct ip_mreqn mreqn; 733 734 memset(&mreqn, 0, sizeof(struct ip_mreqn)); 735 mreqn.imr_ifindex = ifp->int_index; 736 if (0 > setsockopt(rdisc_sock, 737 IPPROTO_IP, IP_MULTICAST_IF, 738 &mreqn, 739 sizeof(mreqn))) { 740 LOGERR("setsockopt(rdisc_sock," 741 "IP_MULTICAST_IF)"); 742 rdisc_sock_mcast = NULL; 743 return; 744 } 745 rdisc_sock_mcast = ifp; 746 } 747 flags = 0; 748 break; 749 } 750 751 if (rdisc_sock < 0) 752 get_rdisc_sock(); 753 754 trace_rdisc(msg, (ifp ? ifp->int_addr : 0), rsin.sin_addr.s_addr, ifp, 755 p, p_size); 756 757 if (0 > sendto(rdisc_sock, p, p_size, flags, 758 (struct sockaddr *)&rsin, sizeof(rsin))) { 759 if (ifp == NULL || !(ifp->int_state & IS_BROKE)) 760 msglog("sendto(%s%s%s): %s", 761 ifp != NULL ? ifp->int_name : "", 762 ifp != NULL ? ", " : "", 763 inet_ntoa(rsin.sin_addr), 764 strerror(errno)); 765 if (ifp != NULL) 766 if_sick(ifp); 767 } 768 } 769 770 771 /* Send an advertisement 772 */ 773 static void 774 send_adv(struct interface *ifp, 775 naddr dst, /* 0 or unicast destination */ 776 int type) /* 0=unicast, 1=bcast, 2=mcast */ 777 { 778 union ad_u u; 779 n_long pref; 780 781 782 memset(&u, 0, sizeof(u.ad)); 783 784 u.ad.icmp_type = ICMP_ROUTERADVERT; 785 u.ad.icmp_ad_num = 1; 786 u.ad.icmp_ad_asize = sizeof(u.ad.icmp_ad_info[0])/4; 787 788 u.ad.icmp_ad_life = stopint ? 0 : htons(ifp->int_rdisc_int*3); 789 790 /* Convert the configured preference to an unsigned value, 791 * bias it by the interface metric, and then send it as a 792 * signed, network byte order value. 793 */ 794 pref = UNSIGN_PREF(ifp->int_rdisc_pref); 795 u.ad.icmp_ad_info[0].icmp_ad_pref = htonl(SIGN_PREF(PREF(pref, ifp))); 796 797 u.ad.icmp_ad_info[0].icmp_ad_addr = ifp->int_addr; 798 799 u.ad.icmp_cksum = in_cksum((u_short*)&u.ad, sizeof(u.ad)); 800 801 send_rdisc(&u, sizeof(u.ad), ifp, dst, type); 802 } 803 804 805 /* Advertise for Router Discovery 806 */ 807 void 808 rdisc_adv(void) 809 { 810 struct interface *ifp; 811 812 if (!supplier) 813 return; 814 815 rdisc_timer.tv_sec = now.tv_sec + NEVER; 816 817 LIST_FOREACH(ifp, &ifnet, int_list) { 818 if (0 != (ifp->int_state & (IS_NO_ADV_OUT | IS_BROKE))) 819 continue; 820 821 if (!timercmp(&ifp->int_rdisc_timer, &now, >) 822 || stopint) { 823 send_adv(ifp, htonl(INADDR_ALLHOSTS_GROUP), 824 (ifp->int_state&IS_BCAST_RDISC) ? 1 : 2); 825 ifp->int_rdisc_cnt++; 826 827 intvl_random(&ifp->int_rdisc_timer, 828 (ifp->int_rdisc_int*3)/4, 829 ifp->int_rdisc_int); 830 if (ifp->int_rdisc_cnt < MAX_INITIAL_ADVERTS 831 && (ifp->int_rdisc_timer.tv_sec 832 > MAX_INITIAL_ADVERT_INTERVAL)) { 833 ifp->int_rdisc_timer.tv_sec 834 = MAX_INITIAL_ADVERT_INTERVAL; 835 } 836 timevaladd(&ifp->int_rdisc_timer, &now); 837 } 838 839 if (timercmp(&rdisc_timer, &ifp->int_rdisc_timer, >)) 840 rdisc_timer = ifp->int_rdisc_timer; 841 } 842 } 843 844 845 /* Solicit for Router Discovery 846 */ 847 void 848 rdisc_sol(void) 849 { 850 struct interface *ifp; 851 union ad_u u; 852 853 854 if (supplier) 855 return; 856 857 rdisc_timer.tv_sec = now.tv_sec + NEVER; 858 859 LIST_FOREACH(ifp, &ifnet, int_list) { 860 if (0 != (ifp->int_state & (IS_NO_SOL_OUT | IS_BROKE)) 861 || ifp->int_rdisc_cnt >= MAX_SOLICITATIONS) 862 continue; 863 864 if (!timercmp(&ifp->int_rdisc_timer, &now, >)) { 865 memset(&u, 0, sizeof(u.so)); 866 u.so.icmp_type = ICMP_ROUTERSOLICIT; 867 u.so.icmp_cksum = in_cksum((u_short*)&u.so, 868 sizeof(u.so)); 869 send_rdisc(&u, sizeof(u.so), ifp, 870 htonl(INADDR_ALLROUTERS_GROUP), 871 ((ifp->int_state&IS_BCAST_RDISC) ? 1 : 2)); 872 873 if (++ifp->int_rdisc_cnt >= MAX_SOLICITATIONS) 874 continue; 875 876 ifp->int_rdisc_timer.tv_sec = SOLICITATION_INTERVAL; 877 ifp->int_rdisc_timer.tv_usec = 0; 878 timevaladd(&ifp->int_rdisc_timer, &now); 879 } 880 881 if (timercmp(&rdisc_timer, &ifp->int_rdisc_timer, >)) 882 rdisc_timer = ifp->int_rdisc_timer; 883 } 884 } 885 886 887 /* check the IP header of a possible Router Discovery ICMP packet */ 888 static struct interface * /* 0 if bad */ 889 ck_icmp(const char *act, 890 naddr from, 891 struct interface *ifp, 892 naddr to, 893 union ad_u *p, 894 u_int len) 895 { 896 const char *type; 897 898 899 if (p->icmp.icmp_type == ICMP_ROUTERADVERT) { 900 type = "advertisement"; 901 } else if (p->icmp.icmp_type == ICMP_ROUTERSOLICIT) { 902 type = "solicitation"; 903 } else { 904 return 0; 905 } 906 907 if (p->icmp.icmp_code != 0) { 908 trace_pkt("unrecognized ICMP Router %s code=%d from %s to %s", 909 type, p->icmp.icmp_code, 910 naddr_ntoa(from), naddr_ntoa(to)); 911 return 0; 912 } 913 914 trace_rdisc(act, from, to, ifp, p, len); 915 916 if (ifp == NULL) 917 trace_pkt("unknown interface for router-discovery %s" 918 " from %s to %s", 919 type, naddr_ntoa(from), naddr_ntoa(to)); 920 921 return ifp; 922 } 923 924 925 /* read packets from the router discovery socket 926 */ 927 void 928 read_d(void) 929 { 930 static struct msg_limit bad_asize, bad_len; 931 #ifdef USE_PASSIFNAME 932 static struct msg_limit bad_name; 933 #endif 934 struct sockaddr_in from; 935 int n, fromlen, cc, hlen; 936 struct { 937 #ifdef USE_PASSIFNAME 938 char ifname[IFNAMSIZ]; 939 #endif 940 union { 941 struct ip ip; 942 u_char b[512]; 943 } pkt; 944 } buf; 945 union ad_u *p; 946 n_long *wp; 947 struct interface *ifp; 948 949 950 for (;;) { 951 fromlen = sizeof(from); 952 cc = recvfrom(rdisc_sock, &buf, sizeof(buf), 0, 953 (struct sockaddr*)&from, 954 &fromlen); 955 if (cc <= 0) { 956 if (cc < 0 && errno != EWOULDBLOCK) 957 LOGERR("recvfrom(rdisc_sock)"); 958 break; 959 } 960 if (fromlen != sizeof(struct sockaddr_in)) 961 logbad(1,"impossible recvfrom(rdisc_sock) fromlen=%d", 962 fromlen); 963 #ifdef USE_PASSIFNAME 964 if ((cc -= sizeof(buf.ifname)) < 0) 965 logbad(0,"missing USE_PASSIFNAME; only %d bytes", 966 cc+sizeof(buf.ifname)); 967 #endif 968 969 hlen = buf.pkt.ip.ip_hl << 2; 970 if (cc < hlen + ICMP_MINLEN) 971 continue; 972 p = (union ad_u *)&buf.pkt.b[hlen]; 973 cc -= hlen; 974 975 #ifdef USE_PASSIFNAME 976 ifp = ifwithname(buf.ifname, 0); 977 if (ifp == NULL) 978 msglim(&bad_name, from.sin_addr.s_addr, 979 "impossible rdisc if_ name %.*s", 980 IFNAMSIZ, buf.ifname); 981 #else 982 /* If we could tell the interface on which a packet from 983 * address 0 arrived, we could deal with such solicitations. 984 */ 985 ifp = ((from.sin_addr.s_addr == 0) 986 ? 0 : iflookup(from.sin_addr.s_addr)); 987 #endif 988 ifp = ck_icmp("Recv", from.sin_addr.s_addr, ifp, 989 buf.pkt.ip.ip_dst.s_addr, p, cc); 990 if (ifp == NULL) 991 continue; 992 if (ifwithaddr(from.sin_addr.s_addr, 0, 0)) { 993 trace_pkt(" " 994 "discard our own Router Discovery message"); 995 continue; 996 } 997 998 switch (p->icmp.icmp_type) { 999 case ICMP_ROUTERADVERT: 1000 if (p->ad.icmp_ad_asize*4 1001 < (int)sizeof(p->ad.icmp_ad_info[0])) { 1002 msglim(&bad_asize, from.sin_addr.s_addr, 1003 "intolerable rdisc address size=%d", 1004 p->ad.icmp_ad_asize); 1005 continue; 1006 } 1007 if (p->ad.icmp_ad_num == 0) { 1008 trace_pkt(" empty?"); 1009 continue; 1010 } 1011 if (cc != (int)(sizeof(p->ad) 1012 - sizeof(p->ad.icmp_ad_info) 1013 + (p->ad.icmp_ad_num 1014 * sizeof(p->ad.icmp_ad_info[0])))) { 1015 msglim(&bad_len, from.sin_addr.s_addr, 1016 "rdisc length %d does not match ad_num" 1017 " %d", cc, p->ad.icmp_ad_num); 1018 continue; 1019 } 1020 if (supplier) 1021 continue; 1022 if (ifp->int_state & IS_NO_ADV_IN) 1023 continue; 1024 1025 wp = &p->ad.icmp_ad_info[0].icmp_ad_addr; 1026 for (n = 0; n < p->ad.icmp_ad_num; n++) { 1027 parse_ad(from.sin_addr.s_addr, 1028 wp[0], wp[1], 1029 ntohs(p->ad.icmp_ad_life), 1030 ifp); 1031 wp += p->ad.icmp_ad_asize; 1032 } 1033 break; 1034 1035 1036 case ICMP_ROUTERSOLICIT: 1037 if (!supplier) 1038 continue; 1039 if (ifp->int_state & IS_NO_ADV_OUT) 1040 continue; 1041 if (stopint) 1042 continue; 1043 1044 /* XXX 1045 * We should handle messages from address 0. 1046 */ 1047 1048 /* Respond with a point-to-point advertisement */ 1049 send_adv(ifp, from.sin_addr.s_addr, 0); 1050 break; 1051 } 1052 } 1053 1054 rdisc_sort(); 1055 } 1056