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