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