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