1 /* 2 * Copyright (c) 1982, 1986, 1988 Regents of the University of California. 3 * All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 * 7 * @(#)ip_input.c 7.18 (Berkeley) 05/03/91 8 */ 9 10 #include "param.h" 11 #include "systm.h" 12 #include "malloc.h" 13 #include "mbuf.h" 14 #include "domain.h" 15 #include "protosw.h" 16 #include "socket.h" 17 #include "errno.h" 18 #include "time.h" 19 #include "kernel.h" 20 21 #include "../net/if.h" 22 #include "../net/route.h" 23 24 #include "in.h" 25 #include "in_systm.h" 26 #include "ip.h" 27 #include "in_pcb.h" 28 #include "in_var.h" 29 #include "ip_var.h" 30 #include "ip_icmp.h" 31 32 #ifndef IPFORWARDING 33 #ifdef GATEWAY 34 #define IPFORWARDING 1 /* forward IP packets not for us */ 35 #else /* GATEWAY */ 36 #define IPFORWARDING 0 /* don't forward IP packets not for us */ 37 #endif /* GATEWAY */ 38 #endif /* IPFORWARDING */ 39 #ifndef IPSENDREDIRECTS 40 #define IPSENDREDIRECTS 1 41 #endif 42 int ipforwarding = IPFORWARDING; 43 int ipsendredirects = IPSENDREDIRECTS; 44 #ifdef DIAGNOSTIC 45 int ipprintfs = 0; 46 #endif 47 48 extern struct domain inetdomain; 49 extern struct protosw inetsw[]; 50 u_char ip_protox[IPPROTO_MAX]; 51 int ipqmaxlen = IFQ_MAXLEN; 52 struct in_ifaddr *in_ifaddr; /* first inet address */ 53 54 /* 55 * We need to save the IP options in case a protocol wants to respond 56 * to an incoming packet over the same route if the packet got here 57 * using IP source routing. This allows connection establishment and 58 * maintenance when the remote end is on a network that is not known 59 * to us. 60 */ 61 int ip_nhops = 0; 62 static struct ip_srcrt { 63 struct in_addr dst; /* final destination */ 64 char nop; /* one NOP to align */ 65 char srcopt[IPOPT_OFFSET + 1]; /* OPTVAL, OLEN and OFFSET */ 66 struct in_addr route[MAX_IPOPTLEN/sizeof(struct in_addr)]; 67 } ip_srcrt; 68 69 #ifdef GATEWAY 70 extern int if_index; 71 u_long *ip_ifmatrix; 72 #endif 73 74 /* 75 * IP initialization: fill in IP protocol switch table. 76 * All protocols not implemented in kernel go to raw IP protocol handler. 77 */ 78 ip_init() 79 { 80 register struct protosw *pr; 81 register int i; 82 83 pr = pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW); 84 if (pr == 0) 85 panic("ip_init"); 86 for (i = 0; i < IPPROTO_MAX; i++) 87 ip_protox[i] = pr - inetsw; 88 for (pr = inetdomain.dom_protosw; 89 pr < inetdomain.dom_protoswNPROTOSW; pr++) 90 if (pr->pr_domain->dom_family == PF_INET && 91 pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW) 92 ip_protox[pr->pr_protocol] = pr - inetsw; 93 ipq.next = ipq.prev = &ipq; 94 ip_id = time.tv_sec & 0xffff; 95 ipintrq.ifq_maxlen = ipqmaxlen; 96 #ifdef GATEWAY 97 i = (if_index + 1) * (if_index + 1) * sizeof (u_long); 98 if ((ip_ifmatrix = (u_long *) malloc(i, M_RTABLE, M_WAITOK)) == 0) 99 panic("no memory for ip_ifmatrix"); 100 #endif 101 } 102 103 struct ip *ip_reass(); 104 struct sockaddr_in ipaddr = { sizeof(ipaddr), AF_INET }; 105 struct route ipforward_rt; 106 107 /* 108 * Ip input routine. Checksum and byte swap header. If fragmented 109 * try to reassemble. Process options. Pass to next level. 110 */ 111 ipintr() 112 { 113 register struct ip *ip; 114 register struct mbuf *m; 115 register struct ipq *fp; 116 register struct in_ifaddr *ia; 117 int hlen, s; 118 119 next: 120 /* 121 * Get next datagram off input queue and get IP header 122 * in first mbuf. 123 */ 124 s = splimp(); 125 IF_DEQUEUE(&ipintrq, m); 126 splx(s); 127 if (m == 0) 128 return; 129 #ifdef DIAGNOSTIC 130 if ((m->m_flags & M_PKTHDR) == 0) 131 panic("ipintr no HDR"); 132 #endif 133 /* 134 * If no IP addresses have been set yet but the interfaces 135 * are receiving, can't do anything with incoming packets yet. 136 */ 137 if (in_ifaddr == NULL) 138 goto bad; 139 ipstat.ips_total++; 140 if (m->m_len < sizeof (struct ip) && 141 (m = m_pullup(m, sizeof (struct ip))) == 0) { 142 ipstat.ips_toosmall++; 143 goto next; 144 } 145 ip = mtod(m, struct ip *); 146 hlen = ip->ip_hl << 2; 147 if (hlen < sizeof(struct ip)) { /* minimum header length */ 148 ipstat.ips_badhlen++; 149 goto bad; 150 } 151 if (hlen > m->m_len) { 152 if ((m = m_pullup(m, hlen)) == 0) { 153 ipstat.ips_badhlen++; 154 goto next; 155 } 156 ip = mtod(m, struct ip *); 157 } 158 if (ip->ip_sum = in_cksum(m, hlen)) { 159 ipstat.ips_badsum++; 160 goto bad; 161 } 162 163 /* 164 * Convert fields to host representation. 165 */ 166 NTOHS(ip->ip_len); 167 if (ip->ip_len < hlen) { 168 ipstat.ips_badlen++; 169 goto bad; 170 } 171 NTOHS(ip->ip_id); 172 NTOHS(ip->ip_off); 173 174 /* 175 * Check that the amount of data in the buffers 176 * is as at least much as the IP header would have us expect. 177 * Trim mbufs if longer than we expect. 178 * Drop packet if shorter than we expect. 179 */ 180 if (m->m_pkthdr.len < ip->ip_len) { 181 ipstat.ips_tooshort++; 182 goto bad; 183 } 184 if (m->m_pkthdr.len > ip->ip_len) { 185 if (m->m_len == m->m_pkthdr.len) { 186 m->m_len = ip->ip_len; 187 m->m_pkthdr.len = ip->ip_len; 188 } else 189 m_adj(m, ip->ip_len - m->m_pkthdr.len); 190 } 191 192 /* 193 * Process options and, if not destined for us, 194 * ship it on. ip_dooptions returns 1 when an 195 * error was detected (causing an icmp message 196 * to be sent and the original packet to be freed). 197 */ 198 ip_nhops = 0; /* for source routed packets */ 199 if (hlen > sizeof (struct ip) && ip_dooptions(m)) 200 goto next; 201 202 /* 203 * Check our list of addresses, to see if the packet is for us. 204 */ 205 for (ia = in_ifaddr; ia; ia = ia->ia_next) { 206 #define satosin(sa) ((struct sockaddr_in *)(sa)) 207 208 if (IA_SIN(ia)->sin_addr.s_addr == ip->ip_dst.s_addr) 209 goto ours; 210 if ( 211 #ifdef DIRECTED_BROADCAST 212 ia->ia_ifp == m->m_pkthdr.rcvif && 213 #endif 214 (ia->ia_ifp->if_flags & IFF_BROADCAST)) { 215 u_long t; 216 217 if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr == 218 ip->ip_dst.s_addr) 219 goto ours; 220 if (ip->ip_dst.s_addr == ia->ia_netbroadcast.s_addr) 221 goto ours; 222 /* 223 * Look for all-0's host part (old broadcast addr), 224 * either for subnet or net. 225 */ 226 t = ntohl(ip->ip_dst.s_addr); 227 if (t == ia->ia_subnet) 228 goto ours; 229 if (t == ia->ia_net) 230 goto ours; 231 } 232 } 233 if (ip->ip_dst.s_addr == (u_long)INADDR_BROADCAST) 234 goto ours; 235 if (ip->ip_dst.s_addr == INADDR_ANY) 236 goto ours; 237 238 /* 239 * Not for us; forward if possible and desirable. 240 */ 241 if (ipforwarding == 0) { 242 ipstat.ips_cantforward++; 243 m_freem(m); 244 } else 245 ip_forward(m, 0); 246 goto next; 247 248 ours: 249 /* 250 * If offset or IP_MF are set, must reassemble. 251 * Otherwise, nothing need be done. 252 * (We could look in the reassembly queue to see 253 * if the packet was previously fragmented, 254 * but it's not worth the time; just let them time out.) 255 */ 256 if (ip->ip_off &~ IP_DF) { 257 if (m->m_flags & M_EXT) { /* XXX */ 258 if ((m = m_pullup(m, sizeof (struct ip))) == 0) { 259 ipstat.ips_toosmall++; 260 goto next; 261 } 262 ip = mtod(m, struct ip *); 263 } 264 /* 265 * Look for queue of fragments 266 * of this datagram. 267 */ 268 for (fp = ipq.next; fp != &ipq; fp = fp->next) 269 if (ip->ip_id == fp->ipq_id && 270 ip->ip_src.s_addr == fp->ipq_src.s_addr && 271 ip->ip_dst.s_addr == fp->ipq_dst.s_addr && 272 ip->ip_p == fp->ipq_p) 273 goto found; 274 fp = 0; 275 found: 276 277 /* 278 * Adjust ip_len to not reflect header, 279 * set ip_mff if more fragments are expected, 280 * convert offset of this to bytes. 281 */ 282 ip->ip_len -= hlen; 283 ((struct ipasfrag *)ip)->ipf_mff = 0; 284 if (ip->ip_off & IP_MF) 285 ((struct ipasfrag *)ip)->ipf_mff = 1; 286 ip->ip_off <<= 3; 287 288 /* 289 * If datagram marked as having more fragments 290 * or if this is not the first fragment, 291 * attempt reassembly; if it succeeds, proceed. 292 */ 293 if (((struct ipasfrag *)ip)->ipf_mff || ip->ip_off) { 294 ipstat.ips_fragments++; 295 ip = ip_reass((struct ipasfrag *)ip, fp); 296 if (ip == 0) 297 goto next; 298 else 299 ipstat.ips_reassembled++; 300 m = dtom(ip); 301 } else 302 if (fp) 303 ip_freef(fp); 304 } else 305 ip->ip_len -= hlen; 306 307 /* 308 * Switch out to protocol's input routine. 309 */ 310 ipstat.ips_delivered++; 311 (*inetsw[ip_protox[ip->ip_p]].pr_input)(m, hlen); 312 goto next; 313 bad: 314 m_freem(m); 315 goto next; 316 } 317 318 /* 319 * Take incoming datagram fragment and try to 320 * reassemble it into whole datagram. If a chain for 321 * reassembly of this datagram already exists, then it 322 * is given as fp; otherwise have to make a chain. 323 */ 324 struct ip * 325 ip_reass(ip, fp) 326 register struct ipasfrag *ip; 327 register struct ipq *fp; 328 { 329 register struct mbuf *m = dtom(ip); 330 register struct ipasfrag *q; 331 struct mbuf *t; 332 int hlen = ip->ip_hl << 2; 333 int i, next; 334 335 /* 336 * Presence of header sizes in mbufs 337 * would confuse code below. 338 */ 339 m->m_data += hlen; 340 m->m_len -= hlen; 341 342 /* 343 * If first fragment to arrive, create a reassembly queue. 344 */ 345 if (fp == 0) { 346 if ((t = m_get(M_DONTWAIT, MT_FTABLE)) == NULL) 347 goto dropfrag; 348 fp = mtod(t, struct ipq *); 349 insque(fp, &ipq); 350 fp->ipq_ttl = IPFRAGTTL; 351 fp->ipq_p = ip->ip_p; 352 fp->ipq_id = ip->ip_id; 353 fp->ipq_next = fp->ipq_prev = (struct ipasfrag *)fp; 354 fp->ipq_src = ((struct ip *)ip)->ip_src; 355 fp->ipq_dst = ((struct ip *)ip)->ip_dst; 356 q = (struct ipasfrag *)fp; 357 goto insert; 358 } 359 360 /* 361 * Find a segment which begins after this one does. 362 */ 363 for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next) 364 if (q->ip_off > ip->ip_off) 365 break; 366 367 /* 368 * If there is a preceding segment, it may provide some of 369 * our data already. If so, drop the data from the incoming 370 * segment. If it provides all of our data, drop us. 371 */ 372 if (q->ipf_prev != (struct ipasfrag *)fp) { 373 i = q->ipf_prev->ip_off + q->ipf_prev->ip_len - ip->ip_off; 374 if (i > 0) { 375 if (i >= ip->ip_len) 376 goto dropfrag; 377 m_adj(dtom(ip), i); 378 ip->ip_off += i; 379 ip->ip_len -= i; 380 } 381 } 382 383 /* 384 * While we overlap succeeding segments trim them or, 385 * if they are completely covered, dequeue them. 386 */ 387 while (q != (struct ipasfrag *)fp && ip->ip_off + ip->ip_len > q->ip_off) { 388 i = (ip->ip_off + ip->ip_len) - q->ip_off; 389 if (i < q->ip_len) { 390 q->ip_len -= i; 391 q->ip_off += i; 392 m_adj(dtom(q), i); 393 break; 394 } 395 q = q->ipf_next; 396 m_freem(dtom(q->ipf_prev)); 397 ip_deq(q->ipf_prev); 398 } 399 400 insert: 401 /* 402 * Stick new segment in its place; 403 * check for complete reassembly. 404 */ 405 ip_enq(ip, q->ipf_prev); 406 next = 0; 407 for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = q->ipf_next) { 408 if (q->ip_off != next) 409 return (0); 410 next += q->ip_len; 411 } 412 if (q->ipf_prev->ipf_mff) 413 return (0); 414 415 /* 416 * Reassembly is complete; concatenate fragments. 417 */ 418 q = fp->ipq_next; 419 m = dtom(q); 420 t = m->m_next; 421 m->m_next = 0; 422 m_cat(m, t); 423 q = q->ipf_next; 424 while (q != (struct ipasfrag *)fp) { 425 t = dtom(q); 426 q = q->ipf_next; 427 m_cat(m, t); 428 } 429 430 /* 431 * Create header for new ip packet by 432 * modifying header of first packet; 433 * dequeue and discard fragment reassembly header. 434 * Make header visible. 435 */ 436 ip = fp->ipq_next; 437 ip->ip_len = next; 438 ((struct ip *)ip)->ip_src = fp->ipq_src; 439 ((struct ip *)ip)->ip_dst = fp->ipq_dst; 440 remque(fp); 441 (void) m_free(dtom(fp)); 442 m = dtom(ip); 443 m->m_len += (ip->ip_hl << 2); 444 m->m_data -= (ip->ip_hl << 2); 445 /* some debugging cruft by sklower, below, will go away soon */ 446 if (m->m_flags & M_PKTHDR) { /* XXX this should be done elsewhere */ 447 register int plen = 0; 448 for (t = m; m; m = m->m_next) 449 plen += m->m_len; 450 t->m_pkthdr.len = plen; 451 } 452 return ((struct ip *)ip); 453 454 dropfrag: 455 ipstat.ips_fragdropped++; 456 m_freem(m); 457 return (0); 458 } 459 460 /* 461 * Free a fragment reassembly header and all 462 * associated datagrams. 463 */ 464 ip_freef(fp) 465 struct ipq *fp; 466 { 467 register struct ipasfrag *q, *p; 468 469 for (q = fp->ipq_next; q != (struct ipasfrag *)fp; q = p) { 470 p = q->ipf_next; 471 ip_deq(q); 472 m_freem(dtom(q)); 473 } 474 remque(fp); 475 (void) m_free(dtom(fp)); 476 } 477 478 /* 479 * Put an ip fragment on a reassembly chain. 480 * Like insque, but pointers in middle of structure. 481 */ 482 ip_enq(p, prev) 483 register struct ipasfrag *p, *prev; 484 { 485 486 p->ipf_prev = prev; 487 p->ipf_next = prev->ipf_next; 488 prev->ipf_next->ipf_prev = p; 489 prev->ipf_next = p; 490 } 491 492 /* 493 * To ip_enq as remque is to insque. 494 */ 495 ip_deq(p) 496 register struct ipasfrag *p; 497 { 498 499 p->ipf_prev->ipf_next = p->ipf_next; 500 p->ipf_next->ipf_prev = p->ipf_prev; 501 } 502 503 /* 504 * IP timer processing; 505 * if a timer expires on a reassembly 506 * queue, discard it. 507 */ 508 ip_slowtimo() 509 { 510 register struct ipq *fp; 511 int s = splnet(); 512 513 fp = ipq.next; 514 if (fp == 0) { 515 splx(s); 516 return; 517 } 518 while (fp != &ipq) { 519 --fp->ipq_ttl; 520 fp = fp->next; 521 if (fp->prev->ipq_ttl == 0) { 522 ipstat.ips_fragtimeout++; 523 ip_freef(fp->prev); 524 } 525 } 526 splx(s); 527 } 528 529 /* 530 * Drain off all datagram fragments. 531 */ 532 ip_drain() 533 { 534 535 while (ipq.next != &ipq) { 536 ipstat.ips_fragdropped++; 537 ip_freef(ipq.next); 538 } 539 } 540 541 extern struct in_ifaddr *ifptoia(); 542 struct in_ifaddr *ip_rtaddr(); 543 544 /* 545 * Do option processing on a datagram, 546 * possibly discarding it if bad options are encountered, 547 * or forwarding it if source-routed. 548 * Returns 1 if packet has been forwarded/freed, 549 * 0 if the packet should be processed further. 550 */ 551 ip_dooptions(m) 552 struct mbuf *m; 553 { 554 register struct ip *ip = mtod(m, struct ip *); 555 register u_char *cp; 556 register struct ip_timestamp *ipt; 557 register struct in_ifaddr *ia; 558 int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0; 559 struct in_addr *sin; 560 n_time ntime; 561 562 cp = (u_char *)(ip + 1); 563 cnt = (ip->ip_hl << 2) - sizeof (struct ip); 564 for (; cnt > 0; cnt -= optlen, cp += optlen) { 565 opt = cp[IPOPT_OPTVAL]; 566 if (opt == IPOPT_EOL) 567 break; 568 if (opt == IPOPT_NOP) 569 optlen = 1; 570 else { 571 optlen = cp[IPOPT_OLEN]; 572 if (optlen <= 0 || optlen > cnt) { 573 code = &cp[IPOPT_OLEN] - (u_char *)ip; 574 goto bad; 575 } 576 } 577 switch (opt) { 578 579 default: 580 break; 581 582 /* 583 * Source routing with record. 584 * Find interface with current destination address. 585 * If none on this machine then drop if strictly routed, 586 * or do nothing if loosely routed. 587 * Record interface address and bring up next address 588 * component. If strictly routed make sure next 589 * address is on directly accessible net. 590 */ 591 case IPOPT_LSRR: 592 case IPOPT_SSRR: 593 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) { 594 code = &cp[IPOPT_OFFSET] - (u_char *)ip; 595 goto bad; 596 } 597 ipaddr.sin_addr = ip->ip_dst; 598 ia = (struct in_ifaddr *) 599 ifa_ifwithaddr((struct sockaddr *)&ipaddr); 600 if (ia == 0) { 601 if (opt == IPOPT_SSRR) { 602 type = ICMP_UNREACH; 603 code = ICMP_UNREACH_SRCFAIL; 604 goto bad; 605 } 606 /* 607 * Loose routing, and not at next destination 608 * yet; nothing to do except forward. 609 */ 610 break; 611 } 612 off--; /* 0 origin */ 613 if (off > optlen - sizeof(struct in_addr)) { 614 /* 615 * End of source route. Should be for us. 616 */ 617 save_rte(cp, ip->ip_src); 618 break; 619 } 620 /* 621 * locate outgoing interface 622 */ 623 bcopy((caddr_t)(cp + off), (caddr_t)&ipaddr.sin_addr, 624 sizeof(ipaddr.sin_addr)); 625 if (opt == IPOPT_SSRR) { 626 #define INA struct in_ifaddr * 627 #define SA struct sockaddr * 628 if ((ia = (INA)ifa_ifwithdstaddr((SA)&ipaddr)) == 0) 629 ia = in_iaonnetof(in_netof(ipaddr.sin_addr)); 630 } else 631 ia = ip_rtaddr(ipaddr.sin_addr); 632 if (ia == 0) { 633 type = ICMP_UNREACH; 634 code = ICMP_UNREACH_SRCFAIL; 635 goto bad; 636 } 637 ip->ip_dst = ipaddr.sin_addr; 638 bcopy((caddr_t)&(IA_SIN(ia)->sin_addr), 639 (caddr_t)(cp + off), sizeof(struct in_addr)); 640 cp[IPOPT_OFFSET] += sizeof(struct in_addr); 641 forward = 1; 642 break; 643 644 case IPOPT_RR: 645 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) { 646 code = &cp[IPOPT_OFFSET] - (u_char *)ip; 647 goto bad; 648 } 649 /* 650 * If no space remains, ignore. 651 */ 652 off--; /* 0 origin */ 653 if (off > optlen - sizeof(struct in_addr)) 654 break; 655 bcopy((caddr_t)(&ip->ip_dst), (caddr_t)&ipaddr.sin_addr, 656 sizeof(ipaddr.sin_addr)); 657 /* 658 * locate outgoing interface; if we're the destination, 659 * use the incoming interface (should be same). 660 */ 661 if ((ia = (INA)ifa_ifwithaddr((SA)&ipaddr)) == 0 && 662 (ia = ip_rtaddr(ipaddr.sin_addr)) == 0) { 663 type = ICMP_UNREACH; 664 code = ICMP_UNREACH_HOST; 665 goto bad; 666 } 667 bcopy((caddr_t)&(IA_SIN(ia)->sin_addr), 668 (caddr_t)(cp + off), sizeof(struct in_addr)); 669 cp[IPOPT_OFFSET] += sizeof(struct in_addr); 670 break; 671 672 case IPOPT_TS: 673 code = cp - (u_char *)ip; 674 ipt = (struct ip_timestamp *)cp; 675 if (ipt->ipt_len < 5) 676 goto bad; 677 if (ipt->ipt_ptr > ipt->ipt_len - sizeof (long)) { 678 if (++ipt->ipt_oflw == 0) 679 goto bad; 680 break; 681 } 682 sin = (struct in_addr *)(cp + ipt->ipt_ptr - 1); 683 switch (ipt->ipt_flg) { 684 685 case IPOPT_TS_TSONLY: 686 break; 687 688 case IPOPT_TS_TSANDADDR: 689 if (ipt->ipt_ptr + sizeof(n_time) + 690 sizeof(struct in_addr) > ipt->ipt_len) 691 goto bad; 692 ia = ifptoia(m->m_pkthdr.rcvif); 693 bcopy((caddr_t)&IA_SIN(ia)->sin_addr, 694 (caddr_t)sin, sizeof(struct in_addr)); 695 ipt->ipt_ptr += sizeof(struct in_addr); 696 break; 697 698 case IPOPT_TS_PRESPEC: 699 if (ipt->ipt_ptr + sizeof(n_time) + 700 sizeof(struct in_addr) > ipt->ipt_len) 701 goto bad; 702 bcopy((caddr_t)sin, (caddr_t)&ipaddr.sin_addr, 703 sizeof(struct in_addr)); 704 if (ifa_ifwithaddr((SA)&ipaddr) == 0) 705 continue; 706 ipt->ipt_ptr += sizeof(struct in_addr); 707 break; 708 709 default: 710 goto bad; 711 } 712 ntime = iptime(); 713 bcopy((caddr_t)&ntime, (caddr_t)cp + ipt->ipt_ptr - 1, 714 sizeof(n_time)); 715 ipt->ipt_ptr += sizeof(n_time); 716 } 717 } 718 if (forward) { 719 ip_forward(m, 1); 720 return (1); 721 } else 722 return (0); 723 bad: 724 icmp_error(m, type, code); 725 return (1); 726 } 727 728 /* 729 * Given address of next destination (final or next hop), 730 * return internet address info of interface to be used to get there. 731 */ 732 struct in_ifaddr * 733 ip_rtaddr(dst) 734 struct in_addr dst; 735 { 736 register struct sockaddr_in *sin; 737 738 sin = (struct sockaddr_in *) &ipforward_rt.ro_dst; 739 740 if (ipforward_rt.ro_rt == 0 || dst.s_addr != sin->sin_addr.s_addr) { 741 if (ipforward_rt.ro_rt) { 742 RTFREE(ipforward_rt.ro_rt); 743 ipforward_rt.ro_rt = 0; 744 } 745 sin->sin_family = AF_INET; 746 sin->sin_len = sizeof(*sin); 747 sin->sin_addr = dst; 748 749 rtalloc(&ipforward_rt); 750 } 751 if (ipforward_rt.ro_rt == 0) 752 return ((struct in_ifaddr *)0); 753 return ((struct in_ifaddr *) ipforward_rt.ro_rt->rt_ifa); 754 } 755 756 /* 757 * Save incoming source route for use in replies, 758 * to be picked up later by ip_srcroute if the receiver is interested. 759 */ 760 save_rte(option, dst) 761 u_char *option; 762 struct in_addr dst; 763 { 764 unsigned olen; 765 766 olen = option[IPOPT_OLEN]; 767 #ifdef DIAGNOSTIC 768 if (ipprintfs) 769 printf("save_rte: olen %d\n", olen); 770 #endif 771 if (olen > sizeof(ip_srcrt) - (1 + sizeof(dst))) 772 return; 773 bcopy((caddr_t)option, (caddr_t)ip_srcrt.srcopt, olen); 774 ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr); 775 ip_srcrt.dst = dst; 776 } 777 778 /* 779 * Retrieve incoming source route for use in replies, 780 * in the same form used by setsockopt. 781 * The first hop is placed before the options, will be removed later. 782 */ 783 struct mbuf * 784 ip_srcroute() 785 { 786 register struct in_addr *p, *q; 787 register struct mbuf *m; 788 789 if (ip_nhops == 0) 790 return ((struct mbuf *)0); 791 m = m_get(M_DONTWAIT, MT_SOOPTS); 792 if (m == 0) 793 return ((struct mbuf *)0); 794 795 #define OPTSIZ (sizeof(ip_srcrt.nop) + sizeof(ip_srcrt.srcopt)) 796 797 /* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */ 798 m->m_len = ip_nhops * sizeof(struct in_addr) + sizeof(struct in_addr) + 799 OPTSIZ; 800 #ifdef DIAGNOSTIC 801 if (ipprintfs) 802 printf("ip_srcroute: nhops %d mlen %d", ip_nhops, m->m_len); 803 #endif 804 805 /* 806 * First save first hop for return route 807 */ 808 p = &ip_srcrt.route[ip_nhops - 1]; 809 *(mtod(m, struct in_addr *)) = *p--; 810 #ifdef DIAGNOSTIC 811 if (ipprintfs) 812 printf(" hops %X", ntohl(mtod(m, struct in_addr *)->s_addr)); 813 #endif 814 815 /* 816 * Copy option fields and padding (nop) to mbuf. 817 */ 818 ip_srcrt.nop = IPOPT_NOP; 819 ip_srcrt.srcopt[IPOPT_OFFSET] = IPOPT_MINOFF; 820 bcopy((caddr_t)&ip_srcrt.nop, 821 mtod(m, caddr_t) + sizeof(struct in_addr), OPTSIZ); 822 q = (struct in_addr *)(mtod(m, caddr_t) + 823 sizeof(struct in_addr) + OPTSIZ); 824 #undef OPTSIZ 825 /* 826 * Record return path as an IP source route, 827 * reversing the path (pointers are now aligned). 828 */ 829 while (p >= ip_srcrt.route) { 830 #ifdef DIAGNOSTIC 831 if (ipprintfs) 832 printf(" %X", ntohl(q->s_addr)); 833 #endif 834 *q++ = *p--; 835 } 836 /* 837 * Last hop goes to final destination. 838 */ 839 *q = ip_srcrt.dst; 840 #ifdef DIAGNOSTIC 841 if (ipprintfs) 842 printf(" %X\n", ntohl(q->s_addr)); 843 #endif 844 return (m); 845 } 846 847 /* 848 * Strip out IP options, at higher 849 * level protocol in the kernel. 850 * Second argument is buffer to which options 851 * will be moved, and return value is their length. 852 * XXX should be deleted; last arg currently ignored. 853 */ 854 ip_stripoptions(m, mopt) 855 register struct mbuf *m; 856 struct mbuf *mopt; 857 { 858 register int i; 859 struct ip *ip = mtod(m, struct ip *); 860 register caddr_t opts; 861 int olen; 862 863 olen = (ip->ip_hl<<2) - sizeof (struct ip); 864 opts = (caddr_t)(ip + 1); 865 i = m->m_len - (sizeof (struct ip) + olen); 866 bcopy(opts + olen, opts, (unsigned)i); 867 m->m_len -= olen; 868 if (m->m_flags & M_PKTHDR) 869 m->m_pkthdr.len -= olen; 870 ip->ip_hl = sizeof(struct ip) >> 2; 871 } 872 873 u_char inetctlerrmap[PRC_NCMDS] = { 874 0, 0, 0, 0, 875 0, EMSGSIZE, EHOSTDOWN, EHOSTUNREACH, 876 EHOSTUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED, 877 EMSGSIZE, EHOSTUNREACH, 0, 0, 878 0, 0, 0, 0, 879 ENOPROTOOPT 880 }; 881 882 /* 883 * Forward a packet. If some error occurs return the sender 884 * an icmp packet. Note we can't always generate a meaningful 885 * icmp message because icmp doesn't have a large enough repertoire 886 * of codes and types. 887 * 888 * If not forwarding, just drop the packet. This could be confusing 889 * if ipforwarding was zero but some routing protocol was advancing 890 * us as a gateway to somewhere. However, we must let the routing 891 * protocol deal with that. 892 * 893 * The srcrt parameter indicates whether the packet is being forwarded 894 * via a source route. 895 */ 896 ip_forward(m, srcrt) 897 struct mbuf *m; 898 int srcrt; 899 { 900 register struct ip *ip = mtod(m, struct ip *); 901 register struct sockaddr_in *sin; 902 register struct rtentry *rt; 903 int error, type = 0, code; 904 struct mbuf *mcopy; 905 struct in_addr dest; 906 907 dest.s_addr = 0; 908 #ifdef DIAGNOSTIC 909 if (ipprintfs) 910 printf("forward: src %x dst %x ttl %x\n", ip->ip_src, 911 ip->ip_dst, ip->ip_ttl); 912 #endif 913 if (m->m_flags & M_BCAST || in_canforward(ip->ip_dst) == 0) { 914 ipstat.ips_cantforward++; 915 m_freem(m); 916 return; 917 } 918 HTONS(ip->ip_id); 919 if (ip->ip_ttl <= IPTTLDEC) { 920 icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, dest); 921 return; 922 } 923 ip->ip_ttl -= IPTTLDEC; 924 925 sin = (struct sockaddr_in *)&ipforward_rt.ro_dst; 926 if ((rt = ipforward_rt.ro_rt) == 0 || 927 ip->ip_dst.s_addr != sin->sin_addr.s_addr) { 928 if (ipforward_rt.ro_rt) { 929 RTFREE(ipforward_rt.ro_rt); 930 ipforward_rt.ro_rt = 0; 931 } 932 sin->sin_family = AF_INET; 933 sin->sin_len = sizeof(*sin); 934 sin->sin_addr = ip->ip_dst; 935 936 rtalloc(&ipforward_rt); 937 if (ipforward_rt.ro_rt == 0) { 938 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, dest); 939 return; 940 } 941 rt = ipforward_rt.ro_rt; 942 } 943 944 /* 945 * Save at most 64 bytes of the packet in case 946 * we need to generate an ICMP message to the src. 947 */ 948 mcopy = m_copy(m, 0, imin((int)ip->ip_len, 64)); 949 950 #ifdef GATEWAY 951 ip_ifmatrix[rt->rt_ifp->if_index + 952 if_index * m->m_pkthdr.rcvif->if_index]++; 953 #endif 954 /* 955 * If forwarding packet using same interface that it came in on, 956 * perhaps should send a redirect to sender to shortcut a hop. 957 * Only send redirect if source is sending directly to us, 958 * and if packet was not source routed (or has any options). 959 * Also, don't send redirect if forwarding using a default route 960 * or a route modified by a redirect. 961 */ 962 #define satosin(sa) ((struct sockaddr_in *)(sa)) 963 if (rt->rt_ifp == m->m_pkthdr.rcvif && 964 (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 && 965 satosin(rt_key(rt))->sin_addr.s_addr != 0 && 966 ipsendredirects && !srcrt) { 967 struct in_ifaddr *ia; 968 u_long src = ntohl(ip->ip_src.s_addr); 969 u_long dst = ntohl(ip->ip_dst.s_addr); 970 971 if ((ia = ifptoia(m->m_pkthdr.rcvif)) && 972 (src & ia->ia_subnetmask) == ia->ia_subnet) { 973 if (rt->rt_flags & RTF_GATEWAY) 974 dest = satosin(rt->rt_gateway)->sin_addr; 975 else 976 dest = ip->ip_dst; 977 /* 978 * If the destination is reached by a route to host, 979 * is on a subnet of a local net, or is directly 980 * on the attached net (!), use host redirect. 981 * (We may be the correct first hop for other subnets.) 982 */ 983 #define RTA(rt) ((struct in_ifaddr *)(rt->rt_ifa)) 984 type = ICMP_REDIRECT; 985 if ((rt->rt_flags & RTF_HOST) || 986 (rt->rt_flags & RTF_GATEWAY) == 0) 987 code = ICMP_REDIRECT_HOST; 988 else if (RTA(rt)->ia_subnetmask != RTA(rt)->ia_netmask && 989 (dst & RTA(rt)->ia_netmask) == RTA(rt)->ia_net) 990 code = ICMP_REDIRECT_HOST; 991 else 992 code = ICMP_REDIRECT_NET; 993 #ifdef DIAGNOSTIC 994 if (ipprintfs) 995 printf("redirect (%d) to %x\n", code, dest.s_addr); 996 #endif 997 } 998 } 999 1000 error = ip_output(m, (struct mbuf *)0, &ipforward_rt, IP_FORWARDING); 1001 if (error) 1002 ipstat.ips_cantforward++; 1003 else { 1004 ipstat.ips_forward++; 1005 if (type) 1006 ipstat.ips_redirectsent++; 1007 else { 1008 if (mcopy) 1009 m_freem(mcopy); 1010 return; 1011 } 1012 } 1013 if (mcopy == NULL) 1014 return; 1015 switch (error) { 1016 1017 case 0: /* forwarded, but need redirect */ 1018 /* type, code set above */ 1019 break; 1020 1021 case ENETUNREACH: /* shouldn't happen, checked above */ 1022 case EHOSTUNREACH: 1023 case ENETDOWN: 1024 case EHOSTDOWN: 1025 default: 1026 type = ICMP_UNREACH; 1027 code = ICMP_UNREACH_HOST; 1028 break; 1029 1030 case EMSGSIZE: 1031 type = ICMP_UNREACH; 1032 code = ICMP_UNREACH_NEEDFRAG; 1033 ipstat.ips_cantfrag++; 1034 break; 1035 1036 case ENOBUFS: 1037 type = ICMP_SOURCEQUENCH; 1038 code = 0; 1039 break; 1040 } 1041 icmp_error(mcopy, type, code, dest); 1042 } 1043