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