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