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