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 * $FreeBSD$ 35 */ 36 37 #define _IP_VHL 38 39 #include "opt_bootp.h" 40 #include "opt_ipfw.h" 41 #include "opt_ipdn.h" 42 #include "opt_ipdivert.h" 43 #include "opt_ipfilter.h" 44 #include "opt_ipstealth.h" 45 #include "opt_ipsec.h" 46 47 #include <stddef.h> 48 49 #include <sys/param.h> 50 #include <sys/systm.h> 51 #include <sys/mbuf.h> 52 #include <sys/malloc.h> 53 #include <sys/domain.h> 54 #include <sys/protosw.h> 55 #include <sys/socket.h> 56 #include <sys/time.h> 57 #include <sys/kernel.h> 58 #include <sys/syslog.h> 59 #include <sys/sysctl.h> 60 61 #include <net/if.h> 62 #include <net/if_var.h> 63 #include <net/if_dl.h> 64 #include <net/route.h> 65 #include <net/netisr.h> 66 67 #include <netinet/in.h> 68 #include <netinet/in_systm.h> 69 #include <netinet/in_var.h> 70 #include <netinet/ip.h> 71 #include <netinet/in_pcb.h> 72 #include <netinet/ip_var.h> 73 #include <netinet/ip_icmp.h> 74 #include <machine/in_cksum.h> 75 76 #include <netinet/ipprotosw.h> 77 78 #include <sys/socketvar.h> 79 80 #include <netinet/ip_fw.h> 81 82 #ifdef IPSEC 83 #include <netinet6/ipsec.h> 84 #include <netkey/key.h> 85 #ifdef IPSEC_DEBUG 86 #include <netkey/key_debug.h> 87 #else 88 #define KEYDEBUG(lev,arg) 89 #endif 90 #endif 91 92 #include "faith.h" 93 #if defined(NFAITH) && NFAITH > 0 94 #include <net/if_types.h> 95 #endif 96 97 #ifdef DUMMYNET 98 #include <netinet/ip_dummynet.h> 99 #endif 100 101 int rsvp_on = 0; 102 static int ip_rsvp_on; 103 struct socket *ip_rsvpd; 104 105 int ipforwarding = 0; 106 SYSCTL_INT(_net_inet_ip, IPCTL_FORWARDING, forwarding, CTLFLAG_RW, 107 &ipforwarding, 0, "Enable IP forwarding between interfaces"); 108 109 static int ipsendredirects = 1; /* XXX */ 110 SYSCTL_INT(_net_inet_ip, IPCTL_SENDREDIRECTS, redirect, CTLFLAG_RW, 111 &ipsendredirects, 0, "Enable sending IP redirects"); 112 113 int ip_defttl = IPDEFTTL; 114 SYSCTL_INT(_net_inet_ip, IPCTL_DEFTTL, ttl, CTLFLAG_RW, 115 &ip_defttl, 0, "Maximum TTL on IP packets"); 116 117 static int ip_dosourceroute = 0; 118 SYSCTL_INT(_net_inet_ip, IPCTL_SOURCEROUTE, sourceroute, CTLFLAG_RW, 119 &ip_dosourceroute, 0, "Enable forwarding source routed IP packets"); 120 121 static int ip_acceptsourceroute = 0; 122 SYSCTL_INT(_net_inet_ip, IPCTL_ACCEPTSOURCEROUTE, accept_sourceroute, 123 CTLFLAG_RW, &ip_acceptsourceroute, 0, 124 "Enable accepting source routed IP packets"); 125 126 static int ip_keepfaith = 0; 127 SYSCTL_INT(_net_inet_ip, IPCTL_KEEPFAITH, keepfaith, CTLFLAG_RW, 128 &ip_keepfaith, 0, 129 "Enable packet capture for FAITH IPv4->IPv6 translater daemon"); 130 131 #ifdef DIAGNOSTIC 132 static int ipprintfs = 0; 133 #endif 134 135 extern struct domain inetdomain; 136 extern struct ipprotosw inetsw[]; 137 u_char ip_protox[IPPROTO_MAX]; 138 static int ipqmaxlen = IFQ_MAXLEN; 139 struct in_ifaddrhead in_ifaddrhead; /* first inet address */ 140 struct ifqueue ipintrq; 141 SYSCTL_INT(_net_inet_ip, IPCTL_INTRQMAXLEN, intr_queue_maxlen, CTLFLAG_RW, 142 &ipintrq.ifq_maxlen, 0, "Maximum size of the IP input queue"); 143 SYSCTL_INT(_net_inet_ip, IPCTL_INTRQDROPS, intr_queue_drops, CTLFLAG_RD, 144 &ipintrq.ifq_drops, 0, "Number of packets dropped from the IP input queue"); 145 146 struct ipstat ipstat; 147 SYSCTL_STRUCT(_net_inet_ip, IPCTL_STATS, stats, CTLFLAG_RD, 148 &ipstat, ipstat, "IP statistics (struct ipstat, netinet/ip_var.h)"); 149 150 /* Packet reassembly stuff */ 151 #define IPREASS_NHASH_LOG2 6 152 #define IPREASS_NHASH (1 << IPREASS_NHASH_LOG2) 153 #define IPREASS_HMASK (IPREASS_NHASH - 1) 154 #define IPREASS_HASH(x,y) \ 155 (((((x) & 0xF) | ((((x) >> 8) & 0xF) << 4)) ^ (y)) & IPREASS_HMASK) 156 157 static struct ipq ipq[IPREASS_NHASH]; 158 static int nipq = 0; /* total # of reass queues */ 159 static int maxnipq; 160 161 #ifdef IPCTL_DEFMTU 162 SYSCTL_INT(_net_inet_ip, IPCTL_DEFMTU, mtu, CTLFLAG_RW, 163 &ip_mtu, 0, "Default MTU"); 164 #endif 165 166 #ifdef IPSTEALTH 167 static int ipstealth = 0; 168 SYSCTL_INT(_net_inet_ip, OID_AUTO, stealth, CTLFLAG_RW, 169 &ipstealth, 0, ""); 170 #endif 171 172 173 /* Firewall hooks */ 174 ip_fw_chk_t *ip_fw_chk_ptr; 175 ip_fw_ctl_t *ip_fw_ctl_ptr; 176 177 #ifdef DUMMYNET 178 ip_dn_ctl_t *ip_dn_ctl_ptr; 179 #endif 180 181 #if defined(IPFILTER_LKM) || defined(IPFILTER) 182 int (*fr_checkp) __P((struct ip *, int, struct ifnet *, int, struct mbuf **)) = NULL; 183 #endif 184 185 186 /* 187 * We need to save the IP options in case a protocol wants to respond 188 * to an incoming packet over the same route if the packet got here 189 * using IP source routing. This allows connection establishment and 190 * maintenance when the remote end is on a network that is not known 191 * to us. 192 */ 193 static int ip_nhops = 0; 194 static struct ip_srcrt { 195 struct in_addr dst; /* final destination */ 196 char nop; /* one NOP to align */ 197 char srcopt[IPOPT_OFFSET + 1]; /* OPTVAL, OLEN and OFFSET */ 198 struct in_addr route[MAX_IPOPTLEN/sizeof(struct in_addr)]; 199 } ip_srcrt; 200 201 struct sockaddr_in *ip_fw_fwd_addr; 202 203 static void save_rte __P((u_char *, struct in_addr)); 204 static int ip_dooptions __P((struct mbuf *)); 205 static void ip_forward __P((struct mbuf *, int)); 206 static void ip_freef __P((struct ipq *)); 207 #ifdef IPDIVERT 208 static struct mbuf *ip_reass __P((struct mbuf *, 209 struct ipq *, struct ipq *, u_int32_t *, u_int16_t *)); 210 #else 211 static struct mbuf *ip_reass __P((struct mbuf *, struct ipq *, struct ipq *)); 212 #endif 213 static struct in_ifaddr *ip_rtaddr __P((struct in_addr)); 214 static void ipintr __P((void)); 215 216 /* 217 * IP initialization: fill in IP protocol switch table. 218 * All protocols not implemented in kernel go to raw IP protocol handler. 219 */ 220 void 221 ip_init() 222 { 223 register struct ipprotosw *pr; 224 register int i; 225 226 TAILQ_INIT(&in_ifaddrhead); 227 pr = (struct ipprotosw *)pffindproto(PF_INET, IPPROTO_RAW, SOCK_RAW); 228 if (pr == 0) 229 panic("ip_init"); 230 for (i = 0; i < IPPROTO_MAX; i++) 231 ip_protox[i] = pr - inetsw; 232 for (pr = (struct ipprotosw *)inetdomain.dom_protosw; 233 pr < (struct ipprotosw *)inetdomain.dom_protoswNPROTOSW; pr++) 234 if (pr->pr_domain->dom_family == PF_INET && 235 pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW) 236 ip_protox[pr->pr_protocol] = pr - inetsw; 237 238 for (i = 0; i < IPREASS_NHASH; i++) 239 ipq[i].next = ipq[i].prev = &ipq[i]; 240 241 maxnipq = nmbclusters/4; 242 243 ip_id = time_second & 0xffff; 244 ipintrq.ifq_maxlen = ipqmaxlen; 245 } 246 247 static struct sockaddr_in ipaddr = { sizeof(ipaddr), AF_INET }; 248 static struct route ipforward_rt; 249 250 /* 251 * Ip input routine. Checksum and byte swap header. If fragmented 252 * try to reassemble. Process options. Pass to next level. 253 */ 254 void 255 ip_input(struct mbuf *m) 256 { 257 struct ip *ip; 258 struct ipq *fp; 259 struct in_ifaddr *ia; 260 int i, hlen, mff; 261 u_short sum; 262 u_int16_t divert_cookie; /* firewall cookie */ 263 #ifdef IPDIVERT 264 u_int32_t divert_info = 0; /* packet divert/tee info */ 265 #endif 266 struct ip_fw_chain *rule = NULL; 267 268 #ifdef IPDIVERT 269 /* Get and reset firewall cookie */ 270 divert_cookie = ip_divert_cookie; 271 ip_divert_cookie = 0; 272 #else 273 divert_cookie = 0; 274 #endif 275 276 #if defined(IPFIREWALL) && defined(DUMMYNET) 277 /* 278 * dummynet packet are prepended a vestigial mbuf with 279 * m_type = MT_DUMMYNET and m_data pointing to the matching 280 * rule. 281 */ 282 if (m->m_type == MT_DUMMYNET) { 283 rule = (struct ip_fw_chain *)(m->m_data) ; 284 m = m->m_next ; 285 ip = mtod(m, struct ip *); 286 hlen = IP_VHL_HL(ip->ip_vhl) << 2; 287 goto iphack ; 288 } else 289 rule = NULL ; 290 #endif 291 292 #ifdef DIAGNOSTIC 293 if (m == NULL || (m->m_flags & M_PKTHDR) == 0) 294 panic("ip_input no HDR"); 295 #endif 296 ipstat.ips_total++; 297 298 if (m->m_pkthdr.len < sizeof(struct ip)) 299 goto tooshort; 300 301 if (m->m_len < sizeof (struct ip) && 302 (m = m_pullup(m, sizeof (struct ip))) == 0) { 303 ipstat.ips_toosmall++; 304 return; 305 } 306 ip = mtod(m, struct ip *); 307 308 if (IP_VHL_V(ip->ip_vhl) != IPVERSION) { 309 ipstat.ips_badvers++; 310 goto bad; 311 } 312 313 hlen = IP_VHL_HL(ip->ip_vhl) << 2; 314 if (hlen < sizeof(struct ip)) { /* minimum header length */ 315 ipstat.ips_badhlen++; 316 goto bad; 317 } 318 if (hlen > m->m_len) { 319 if ((m = m_pullup(m, hlen)) == 0) { 320 ipstat.ips_badhlen++; 321 return; 322 } 323 ip = mtod(m, struct ip *); 324 } 325 if (hlen == sizeof(struct ip)) { 326 sum = in_cksum_hdr(ip); 327 } else { 328 sum = in_cksum(m, hlen); 329 } 330 if (sum) { 331 ipstat.ips_badsum++; 332 goto bad; 333 } 334 335 /* 336 * Convert fields to host representation. 337 */ 338 NTOHS(ip->ip_len); 339 if (ip->ip_len < hlen) { 340 ipstat.ips_badlen++; 341 goto bad; 342 } 343 NTOHS(ip->ip_id); 344 NTOHS(ip->ip_off); 345 346 /* 347 * Check that the amount of data in the buffers 348 * is as at least much as the IP header would have us expect. 349 * Trim mbufs if longer than we expect. 350 * Drop packet if shorter than we expect. 351 */ 352 if (m->m_pkthdr.len < ip->ip_len) { 353 tooshort: 354 ipstat.ips_tooshort++; 355 goto bad; 356 } 357 if (m->m_pkthdr.len > ip->ip_len) { 358 if (m->m_len == m->m_pkthdr.len) { 359 m->m_len = ip->ip_len; 360 m->m_pkthdr.len = ip->ip_len; 361 } else 362 m_adj(m, ip->ip_len - m->m_pkthdr.len); 363 } 364 /* 365 * IpHack's section. 366 * Right now when no processing on packet has done 367 * and it is still fresh out of network we do our black 368 * deals with it. 369 * - Firewall: deny/allow/divert 370 * - Xlate: translate packet's addr/port (NAT). 371 * - Pipe: pass pkt through dummynet. 372 * - Wrap: fake packet's addr/port <unimpl.> 373 * - Encapsulate: put it in another IP and send out. <unimp.> 374 */ 375 376 #if defined(IPFIREWALL) && defined(DUMMYNET) 377 iphack: 378 #endif 379 #if defined(IPFILTER) || defined(IPFILTER_LKM) 380 /* 381 * Check if we want to allow this packet to be processed. 382 * Consider it to be bad if not. 383 */ 384 if (fr_checkp) { 385 struct mbuf *m1 = m; 386 387 if ((*fr_checkp)(ip, hlen, m->m_pkthdr.rcvif, 0, &m1) || !m1) 388 return; 389 ip = mtod(m = m1, struct ip *); 390 } 391 #endif 392 if (ip_fw_chk_ptr) { 393 #ifdef IPFIREWALL_FORWARD 394 /* 395 * If we've been forwarded from the output side, then 396 * skip the firewall a second time 397 */ 398 if (ip_fw_fwd_addr) 399 goto ours; 400 #endif /* IPFIREWALL_FORWARD */ 401 /* 402 * See the comment in ip_output for the return values 403 * produced by the firewall. 404 */ 405 i = (*ip_fw_chk_ptr)(&ip, 406 hlen, NULL, &divert_cookie, &m, &rule, &ip_fw_fwd_addr); 407 if (m == NULL) /* Packet discarded by firewall */ 408 return; 409 if (i == 0 && ip_fw_fwd_addr == NULL) /* common case */ 410 goto pass; 411 #ifdef DUMMYNET 412 if ((i & IP_FW_PORT_DYNT_FLAG) != 0) { 413 /* Send packet to the appropriate pipe */ 414 dummynet_io(i&0xffff,DN_TO_IP_IN,m,NULL,NULL,0, rule, 415 0); 416 return; 417 } 418 #endif 419 #ifdef IPDIVERT 420 if (i != 0 && (i & IP_FW_PORT_DYNT_FLAG) == 0) { 421 /* Divert or tee packet */ 422 divert_info = i; 423 goto ours; 424 } 425 #endif 426 #ifdef IPFIREWALL_FORWARD 427 if (i == 0 && ip_fw_fwd_addr != NULL) 428 goto pass; 429 #endif 430 /* 431 * if we get here, the packet must be dropped 432 */ 433 m_freem(m); 434 return; 435 } 436 pass: 437 438 /* 439 * Process options and, if not destined for us, 440 * ship it on. ip_dooptions returns 1 when an 441 * error was detected (causing an icmp message 442 * to be sent and the original packet to be freed). 443 */ 444 ip_nhops = 0; /* for source routed packets */ 445 if (hlen > sizeof (struct ip) && ip_dooptions(m)) { 446 #ifdef IPFIREWALL_FORWARD 447 ip_fw_fwd_addr = NULL; 448 #endif 449 return; 450 } 451 452 /* greedy RSVP, snatches any PATH packet of the RSVP protocol and no 453 * matter if it is destined to another node, or whether it is 454 * a multicast one, RSVP wants it! and prevents it from being forwarded 455 * anywhere else. Also checks if the rsvp daemon is running before 456 * grabbing the packet. 457 */ 458 if (rsvp_on && ip->ip_p==IPPROTO_RSVP) 459 goto ours; 460 461 /* 462 * Check our list of addresses, to see if the packet is for us. 463 * If we don't have any addresses, assume any unicast packet 464 * we receive might be for us (and let the upper layers deal 465 * with it). 466 */ 467 if (TAILQ_EMPTY(&in_ifaddrhead) && 468 (m->m_flags & (M_MCAST|M_BCAST)) == 0) 469 goto ours; 470 471 for (ia = TAILQ_FIRST(&in_ifaddrhead); ia; 472 ia = TAILQ_NEXT(ia, ia_link)) { 473 #define satosin(sa) ((struct sockaddr_in *)(sa)) 474 475 #ifdef BOOTP_COMPAT 476 if (IA_SIN(ia)->sin_addr.s_addr == INADDR_ANY) 477 goto ours; 478 #endif 479 #ifdef IPFIREWALL_FORWARD 480 /* 481 * If the addr to forward to is one of ours, we pretend to 482 * be the destination for this packet. 483 */ 484 if (ip_fw_fwd_addr == NULL) { 485 if (IA_SIN(ia)->sin_addr.s_addr == ip->ip_dst.s_addr) 486 goto ours; 487 } else if (IA_SIN(ia)->sin_addr.s_addr == 488 ip_fw_fwd_addr->sin_addr.s_addr) 489 goto ours; 490 #else 491 if (IA_SIN(ia)->sin_addr.s_addr == ip->ip_dst.s_addr) 492 goto ours; 493 #endif 494 if (ia->ia_ifp && ia->ia_ifp->if_flags & IFF_BROADCAST) { 495 if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr == 496 ip->ip_dst.s_addr) 497 goto ours; 498 if (ip->ip_dst.s_addr == ia->ia_netbroadcast.s_addr) 499 goto ours; 500 } 501 } 502 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) { 503 struct in_multi *inm; 504 if (ip_mrouter) { 505 /* 506 * If we are acting as a multicast router, all 507 * incoming multicast packets are passed to the 508 * kernel-level multicast forwarding function. 509 * The packet is returned (relatively) intact; if 510 * ip_mforward() returns a non-zero value, the packet 511 * must be discarded, else it may be accepted below. 512 * 513 * (The IP ident field is put in the same byte order 514 * as expected when ip_mforward() is called from 515 * ip_output().) 516 */ 517 ip->ip_id = htons(ip->ip_id); 518 if (ip_mforward(ip, m->m_pkthdr.rcvif, m, 0) != 0) { 519 ipstat.ips_cantforward++; 520 m_freem(m); 521 return; 522 } 523 ip->ip_id = ntohs(ip->ip_id); 524 525 /* 526 * The process-level routing demon needs to receive 527 * all multicast IGMP packets, whether or not this 528 * host belongs to their destination groups. 529 */ 530 if (ip->ip_p == IPPROTO_IGMP) 531 goto ours; 532 ipstat.ips_forward++; 533 } 534 /* 535 * See if we belong to the destination multicast group on the 536 * arrival interface. 537 */ 538 IN_LOOKUP_MULTI(ip->ip_dst, m->m_pkthdr.rcvif, inm); 539 if (inm == NULL) { 540 ipstat.ips_notmember++; 541 m_freem(m); 542 return; 543 } 544 goto ours; 545 } 546 if (ip->ip_dst.s_addr == (u_long)INADDR_BROADCAST) 547 goto ours; 548 if (ip->ip_dst.s_addr == INADDR_ANY) 549 goto ours; 550 551 #if defined(NFAITH) && 0 < NFAITH 552 /* 553 * FAITH(Firewall Aided Internet Translator) 554 */ 555 if (m->m_pkthdr.rcvif && m->m_pkthdr.rcvif->if_type == IFT_FAITH) { 556 if (ip_keepfaith) { 557 if (ip->ip_p == IPPROTO_TCP || ip->ip_p == IPPROTO_ICMP) 558 goto ours; 559 } 560 m_freem(m); 561 return; 562 } 563 #endif 564 /* 565 * Not for us; forward if possible and desirable. 566 */ 567 if (ipforwarding == 0) { 568 ipstat.ips_cantforward++; 569 m_freem(m); 570 } else 571 ip_forward(m, 0); 572 #ifdef IPFIREWALL_FORWARD 573 ip_fw_fwd_addr = NULL; 574 #endif 575 return; 576 577 ours: 578 579 /* 580 * If offset or IP_MF are set, must reassemble. 581 * Otherwise, nothing need be done. 582 * (We could look in the reassembly queue to see 583 * if the packet was previously fragmented, 584 * but it's not worth the time; just let them time out.) 585 */ 586 if (ip->ip_off & (IP_MF | IP_OFFMASK | IP_RF)) { 587 588 #if 0 /* 589 * Reassembly should be able to treat a mbuf cluster, for later 590 * operation of contiguous protocol headers on the cluster. (KAME) 591 */ 592 if (m->m_flags & M_EXT) { /* XXX */ 593 if ((m = m_pullup(m, hlen)) == 0) { 594 ipstat.ips_toosmall++; 595 #ifdef IPFIREWALL_FORWARD 596 ip_fw_fwd_addr = NULL; 597 #endif 598 return; 599 } 600 ip = mtod(m, struct ip *); 601 } 602 #endif 603 sum = IPREASS_HASH(ip->ip_src.s_addr, ip->ip_id); 604 /* 605 * Look for queue of fragments 606 * of this datagram. 607 */ 608 for (fp = ipq[sum].next; fp != &ipq[sum]; fp = fp->next) 609 if (ip->ip_id == fp->ipq_id && 610 ip->ip_src.s_addr == fp->ipq_src.s_addr && 611 ip->ip_dst.s_addr == fp->ipq_dst.s_addr && 612 ip->ip_p == fp->ipq_p) 613 goto found; 614 615 fp = 0; 616 617 /* check if there's a place for the new queue */ 618 if (nipq > maxnipq) { 619 /* 620 * drop something from the tail of the current queue 621 * before proceeding further 622 */ 623 if (ipq[sum].prev == &ipq[sum]) { /* gak */ 624 for (i = 0; i < IPREASS_NHASH; i++) { 625 if (ipq[i].prev != &ipq[i]) { 626 ip_freef(ipq[i].prev); 627 break; 628 } 629 } 630 } else 631 ip_freef(ipq[sum].prev); 632 } 633 found: 634 /* 635 * Adjust ip_len to not reflect header, 636 * set ip_mff if more fragments are expected, 637 * convert offset of this to bytes. 638 */ 639 ip->ip_len -= hlen; 640 mff = (ip->ip_off & IP_MF) != 0; 641 if (mff) { 642 /* 643 * Make sure that fragments have a data length 644 * that's a non-zero multiple of 8 bytes. 645 */ 646 if (ip->ip_len == 0 || (ip->ip_len & 0x7) != 0) { 647 ipstat.ips_toosmall++; /* XXX */ 648 goto bad; 649 } 650 m->m_flags |= M_FRAG; 651 } 652 ip->ip_off <<= 3; 653 654 /* 655 * If datagram marked as having more fragments 656 * or if this is not the first fragment, 657 * attempt reassembly; if it succeeds, proceed. 658 */ 659 if (mff || ip->ip_off) { 660 ipstat.ips_fragments++; 661 m->m_pkthdr.header = ip; 662 #ifdef IPDIVERT 663 m = ip_reass(m, 664 fp, &ipq[sum], &divert_info, &divert_cookie); 665 #else 666 m = ip_reass(m, fp, &ipq[sum]); 667 #endif 668 if (m == 0) { 669 #ifdef IPFIREWALL_FORWARD 670 ip_fw_fwd_addr = NULL; 671 #endif 672 return; 673 } 674 /* Get the length of the reassembled packets header */ 675 hlen = IP_VHL_HL(ip->ip_vhl) << 2; 676 ipstat.ips_reassembled++; 677 ip = mtod(m, struct ip *); 678 #ifdef IPDIVERT 679 /* Restore original checksum before diverting packet */ 680 if (divert_info != 0) { 681 ip->ip_len += hlen; 682 HTONS(ip->ip_len); 683 HTONS(ip->ip_off); 684 HTONS(ip->ip_id); 685 ip->ip_sum = 0; 686 ip->ip_sum = in_cksum_hdr(ip); 687 NTOHS(ip->ip_id); 688 NTOHS(ip->ip_off); 689 NTOHS(ip->ip_len); 690 ip->ip_len -= hlen; 691 } 692 #endif 693 } else 694 if (fp) 695 ip_freef(fp); 696 } else 697 ip->ip_len -= hlen; 698 699 #ifdef IPDIVERT 700 /* 701 * Divert or tee packet to the divert protocol if required. 702 * 703 * If divert_info is zero then cookie should be too, so we shouldn't 704 * need to clear them here. Assume divert_packet() does so also. 705 */ 706 if (divert_info != 0) { 707 struct mbuf *clone = NULL; 708 709 /* Clone packet if we're doing a 'tee' */ 710 if ((divert_info & IP_FW_PORT_TEE_FLAG) != 0) 711 clone = m_dup(m, M_DONTWAIT); 712 713 /* Restore packet header fields to original values */ 714 ip->ip_len += hlen; 715 HTONS(ip->ip_len); 716 HTONS(ip->ip_off); 717 HTONS(ip->ip_id); 718 719 /* Deliver packet to divert input routine */ 720 ip_divert_cookie = divert_cookie; 721 divert_packet(m, 1, divert_info & 0xffff); 722 ipstat.ips_delivered++; 723 724 /* If 'tee', continue with original packet */ 725 if (clone == NULL) 726 return; 727 m = clone; 728 ip = mtod(m, struct ip *); 729 } 730 #endif 731 732 /* 733 * Switch out to protocol's input routine. 734 */ 735 ipstat.ips_delivered++; 736 { 737 int off = hlen, nh = ip->ip_p; 738 739 (*inetsw[ip_protox[ip->ip_p]].pr_input)(m, off, nh); 740 #ifdef IPFIREWALL_FORWARD 741 ip_fw_fwd_addr = NULL; /* tcp needed it */ 742 #endif 743 return; 744 } 745 bad: 746 #ifdef IPFIREWALL_FORWARD 747 ip_fw_fwd_addr = NULL; 748 #endif 749 m_freem(m); 750 } 751 752 /* 753 * IP software interrupt routine - to go away sometime soon 754 */ 755 static void 756 ipintr(void) 757 { 758 int s; 759 struct mbuf *m; 760 761 while(1) { 762 s = splimp(); 763 IF_DEQUEUE(&ipintrq, m); 764 splx(s); 765 if (m == 0) 766 return; 767 ip_input(m); 768 } 769 } 770 771 NETISR_SET(NETISR_IP, ipintr); 772 773 /* 774 * Take incoming datagram fragment and try to reassemble it into 775 * whole datagram. If a chain for reassembly of this datagram already 776 * exists, then it is given as fp; otherwise have to make a chain. 777 * 778 * When IPDIVERT enabled, keep additional state with each packet that 779 * tells us if we need to divert or tee the packet we're building. 780 */ 781 782 static struct mbuf * 783 #ifdef IPDIVERT 784 ip_reass(m, fp, where, divinfo, divcookie) 785 #else 786 ip_reass(m, fp, where) 787 #endif 788 register struct mbuf *m; 789 register struct ipq *fp; 790 struct ipq *where; 791 #ifdef IPDIVERT 792 u_int32_t *divinfo; 793 u_int16_t *divcookie; 794 #endif 795 { 796 struct ip *ip = mtod(m, struct ip *); 797 register struct mbuf *p = 0, *q, *nq; 798 struct mbuf *t; 799 int hlen = IP_VHL_HL(ip->ip_vhl) << 2; 800 int i, next; 801 802 /* 803 * Presence of header sizes in mbufs 804 * would confuse code below. 805 */ 806 m->m_data += hlen; 807 m->m_len -= hlen; 808 809 /* 810 * If first fragment to arrive, create a reassembly queue. 811 */ 812 if (fp == 0) { 813 if ((t = m_get(M_DONTWAIT, MT_FTABLE)) == NULL) 814 goto dropfrag; 815 fp = mtod(t, struct ipq *); 816 insque(fp, where); 817 nipq++; 818 fp->ipq_ttl = IPFRAGTTL; 819 fp->ipq_p = ip->ip_p; 820 fp->ipq_id = ip->ip_id; 821 fp->ipq_src = ip->ip_src; 822 fp->ipq_dst = ip->ip_dst; 823 fp->ipq_frags = m; 824 m->m_nextpkt = NULL; 825 #ifdef IPDIVERT 826 fp->ipq_div_info = 0; 827 fp->ipq_div_cookie = 0; 828 #endif 829 goto inserted; 830 } 831 832 #define GETIP(m) ((struct ip*)((m)->m_pkthdr.header)) 833 834 /* 835 * Find a segment which begins after this one does. 836 */ 837 for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt) 838 if (GETIP(q)->ip_off > ip->ip_off) 839 break; 840 841 /* 842 * If there is a preceding segment, it may provide some of 843 * our data already. If so, drop the data from the incoming 844 * segment. If it provides all of our data, drop us, otherwise 845 * stick new segment in the proper place. 846 */ 847 if (p) { 848 i = GETIP(p)->ip_off + GETIP(p)->ip_len - ip->ip_off; 849 if (i > 0) { 850 if (i >= ip->ip_len) 851 goto dropfrag; 852 m_adj(m, i); 853 ip->ip_off += i; 854 ip->ip_len -= i; 855 } 856 m->m_nextpkt = p->m_nextpkt; 857 p->m_nextpkt = m; 858 } else { 859 m->m_nextpkt = fp->ipq_frags; 860 fp->ipq_frags = m; 861 } 862 863 /* 864 * While we overlap succeeding segments trim them or, 865 * if they are completely covered, dequeue them. 866 */ 867 for (; q != NULL && ip->ip_off + ip->ip_len > GETIP(q)->ip_off; 868 q = nq) { 869 i = (ip->ip_off + ip->ip_len) - 870 GETIP(q)->ip_off; 871 if (i < GETIP(q)->ip_len) { 872 GETIP(q)->ip_len -= i; 873 GETIP(q)->ip_off += i; 874 m_adj(q, i); 875 break; 876 } 877 nq = q->m_nextpkt; 878 m->m_nextpkt = nq; 879 m_freem(q); 880 } 881 882 inserted: 883 884 #ifdef IPDIVERT 885 /* 886 * Transfer firewall instructions to the fragment structure. 887 * Any fragment diverting causes the whole packet to divert. 888 */ 889 fp->ipq_div_info = *divinfo; 890 fp->ipq_div_cookie = *divcookie; 891 *divinfo = 0; 892 *divcookie = 0; 893 #endif 894 895 /* 896 * Check for complete reassembly. 897 */ 898 next = 0; 899 for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt) { 900 if (GETIP(q)->ip_off != next) 901 return (0); 902 next += GETIP(q)->ip_len; 903 } 904 /* Make sure the last packet didn't have the IP_MF flag */ 905 if (p->m_flags & M_FRAG) 906 return (0); 907 908 /* 909 * Reassembly is complete. Make sure the packet is a sane size. 910 */ 911 q = fp->ipq_frags; 912 ip = GETIP(q); 913 if (next + (IP_VHL_HL(ip->ip_vhl) << 2) > IP_MAXPACKET) { 914 ipstat.ips_toolong++; 915 ip_freef(fp); 916 return (0); 917 } 918 919 /* 920 * Concatenate fragments. 921 */ 922 m = q; 923 t = m->m_next; 924 m->m_next = 0; 925 m_cat(m, t); 926 nq = q->m_nextpkt; 927 q->m_nextpkt = 0; 928 for (q = nq; q != NULL; q = nq) { 929 nq = q->m_nextpkt; 930 q->m_nextpkt = NULL; 931 m_cat(m, q); 932 } 933 934 #ifdef IPDIVERT 935 /* 936 * Extract firewall instructions from the fragment structure. 937 */ 938 *divinfo = fp->ipq_div_info; 939 *divcookie = fp->ipq_div_cookie; 940 #endif 941 942 /* 943 * Create header for new ip packet by 944 * modifying header of first packet; 945 * dequeue and discard fragment reassembly header. 946 * Make header visible. 947 */ 948 ip->ip_len = next; 949 ip->ip_src = fp->ipq_src; 950 ip->ip_dst = fp->ipq_dst; 951 remque(fp); 952 nipq--; 953 (void) m_free(dtom(fp)); 954 m->m_len += (IP_VHL_HL(ip->ip_vhl) << 2); 955 m->m_data -= (IP_VHL_HL(ip->ip_vhl) << 2); 956 /* some debugging cruft by sklower, below, will go away soon */ 957 if (m->m_flags & M_PKTHDR) { /* XXX this should be done elsewhere */ 958 register int plen = 0; 959 for (t = m; t; t = t->m_next) 960 plen += t->m_len; 961 m->m_pkthdr.len = plen; 962 } 963 return (m); 964 965 dropfrag: 966 #ifdef IPDIVERT 967 *divinfo = 0; 968 *divcookie = 0; 969 #endif 970 ipstat.ips_fragdropped++; 971 m_freem(m); 972 return (0); 973 974 #undef GETIP 975 } 976 977 /* 978 * Free a fragment reassembly header and all 979 * associated datagrams. 980 */ 981 static void 982 ip_freef(fp) 983 struct ipq *fp; 984 { 985 register struct mbuf *q; 986 987 while (fp->ipq_frags) { 988 q = fp->ipq_frags; 989 fp->ipq_frags = q->m_nextpkt; 990 m_freem(q); 991 } 992 remque(fp); 993 (void) m_free(dtom(fp)); 994 nipq--; 995 } 996 997 /* 998 * IP timer processing; 999 * if a timer expires on a reassembly 1000 * queue, discard it. 1001 */ 1002 void 1003 ip_slowtimo() 1004 { 1005 register struct ipq *fp; 1006 int s = splnet(); 1007 int i; 1008 1009 for (i = 0; i < IPREASS_NHASH; i++) { 1010 fp = ipq[i].next; 1011 if (fp == 0) 1012 continue; 1013 while (fp != &ipq[i]) { 1014 --fp->ipq_ttl; 1015 fp = fp->next; 1016 if (fp->prev->ipq_ttl == 0) { 1017 ipstat.ips_fragtimeout++; 1018 ip_freef(fp->prev); 1019 } 1020 } 1021 } 1022 ipflow_slowtimo(); 1023 splx(s); 1024 } 1025 1026 /* 1027 * Drain off all datagram fragments. 1028 */ 1029 void 1030 ip_drain() 1031 { 1032 int i; 1033 1034 for (i = 0; i < IPREASS_NHASH; i++) { 1035 while (ipq[i].next != &ipq[i]) { 1036 ipstat.ips_fragdropped++; 1037 ip_freef(ipq[i].next); 1038 } 1039 } 1040 in_rtqdrain(); 1041 } 1042 1043 /* 1044 * Do option processing on a datagram, 1045 * possibly discarding it if bad options are encountered, 1046 * or forwarding it if source-routed. 1047 * Returns 1 if packet has been forwarded/freed, 1048 * 0 if the packet should be processed further. 1049 */ 1050 static int 1051 ip_dooptions(m) 1052 struct mbuf *m; 1053 { 1054 register struct ip *ip = mtod(m, struct ip *); 1055 register u_char *cp; 1056 register struct ip_timestamp *ipt; 1057 register struct in_ifaddr *ia; 1058 int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0; 1059 struct in_addr *sin, dst; 1060 n_time ntime; 1061 1062 dst = ip->ip_dst; 1063 cp = (u_char *)(ip + 1); 1064 cnt = (IP_VHL_HL(ip->ip_vhl) << 2) - sizeof (struct ip); 1065 for (; cnt > 0; cnt -= optlen, cp += optlen) { 1066 opt = cp[IPOPT_OPTVAL]; 1067 if (opt == IPOPT_EOL) 1068 break; 1069 if (opt == IPOPT_NOP) 1070 optlen = 1; 1071 else { 1072 optlen = cp[IPOPT_OLEN]; 1073 if (optlen <= 0 || optlen > cnt) { 1074 code = &cp[IPOPT_OLEN] - (u_char *)ip; 1075 goto bad; 1076 } 1077 } 1078 switch (opt) { 1079 1080 default: 1081 break; 1082 1083 /* 1084 * Source routing with record. 1085 * Find interface with current destination address. 1086 * If none on this machine then drop if strictly routed, 1087 * or do nothing if loosely routed. 1088 * Record interface address and bring up next address 1089 * component. If strictly routed make sure next 1090 * address is on directly accessible net. 1091 */ 1092 case IPOPT_LSRR: 1093 case IPOPT_SSRR: 1094 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) { 1095 code = &cp[IPOPT_OFFSET] - (u_char *)ip; 1096 goto bad; 1097 } 1098 ipaddr.sin_addr = ip->ip_dst; 1099 ia = (struct in_ifaddr *) 1100 ifa_ifwithaddr((struct sockaddr *)&ipaddr); 1101 if (ia == 0) { 1102 if (opt == IPOPT_SSRR) { 1103 type = ICMP_UNREACH; 1104 code = ICMP_UNREACH_SRCFAIL; 1105 goto bad; 1106 } 1107 if (!ip_dosourceroute) 1108 goto nosourcerouting; 1109 /* 1110 * Loose routing, and not at next destination 1111 * yet; nothing to do except forward. 1112 */ 1113 break; 1114 } 1115 off--; /* 0 origin */ 1116 if (off > optlen - sizeof(struct in_addr)) { 1117 /* 1118 * End of source route. Should be for us. 1119 */ 1120 if (!ip_acceptsourceroute) 1121 goto nosourcerouting; 1122 save_rte(cp, ip->ip_src); 1123 break; 1124 } 1125 1126 if (!ip_dosourceroute) { 1127 if (ipforwarding) { 1128 char buf[16]; /* aaa.bbb.ccc.ddd\0 */ 1129 /* 1130 * Acting as a router, so generate ICMP 1131 */ 1132 nosourcerouting: 1133 strcpy(buf, inet_ntoa(ip->ip_dst)); 1134 log(LOG_WARNING, 1135 "attempted source route from %s to %s\n", 1136 inet_ntoa(ip->ip_src), buf); 1137 type = ICMP_UNREACH; 1138 code = ICMP_UNREACH_SRCFAIL; 1139 goto bad; 1140 } else { 1141 /* 1142 * Not acting as a router, so silently drop. 1143 */ 1144 ipstat.ips_cantforward++; 1145 m_freem(m); 1146 return (1); 1147 } 1148 } 1149 1150 /* 1151 * locate outgoing interface 1152 */ 1153 (void)memcpy(&ipaddr.sin_addr, cp + off, 1154 sizeof(ipaddr.sin_addr)); 1155 1156 if (opt == IPOPT_SSRR) { 1157 #define INA struct in_ifaddr * 1158 #define SA struct sockaddr * 1159 if ((ia = (INA)ifa_ifwithdstaddr((SA)&ipaddr)) == 0) 1160 ia = (INA)ifa_ifwithnet((SA)&ipaddr); 1161 } else 1162 ia = ip_rtaddr(ipaddr.sin_addr); 1163 if (ia == 0) { 1164 type = ICMP_UNREACH; 1165 code = ICMP_UNREACH_SRCFAIL; 1166 goto bad; 1167 } 1168 ip->ip_dst = ipaddr.sin_addr; 1169 (void)memcpy(cp + off, &(IA_SIN(ia)->sin_addr), 1170 sizeof(struct in_addr)); 1171 cp[IPOPT_OFFSET] += sizeof(struct in_addr); 1172 /* 1173 * Let ip_intr's mcast routing check handle mcast pkts 1174 */ 1175 forward = !IN_MULTICAST(ntohl(ip->ip_dst.s_addr)); 1176 break; 1177 1178 case IPOPT_RR: 1179 if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) { 1180 code = &cp[IPOPT_OFFSET] - (u_char *)ip; 1181 goto bad; 1182 } 1183 /* 1184 * If no space remains, ignore. 1185 */ 1186 off--; /* 0 origin */ 1187 if (off > optlen - sizeof(struct in_addr)) 1188 break; 1189 (void)memcpy(&ipaddr.sin_addr, &ip->ip_dst, 1190 sizeof(ipaddr.sin_addr)); 1191 /* 1192 * locate outgoing interface; if we're the destination, 1193 * use the incoming interface (should be same). 1194 */ 1195 if ((ia = (INA)ifa_ifwithaddr((SA)&ipaddr)) == 0 && 1196 (ia = ip_rtaddr(ipaddr.sin_addr)) == 0) { 1197 type = ICMP_UNREACH; 1198 code = ICMP_UNREACH_HOST; 1199 goto bad; 1200 } 1201 (void)memcpy(cp + off, &(IA_SIN(ia)->sin_addr), 1202 sizeof(struct in_addr)); 1203 cp[IPOPT_OFFSET] += sizeof(struct in_addr); 1204 break; 1205 1206 case IPOPT_TS: 1207 code = cp - (u_char *)ip; 1208 ipt = (struct ip_timestamp *)cp; 1209 if (ipt->ipt_len < 5) 1210 goto bad; 1211 if (ipt->ipt_ptr > ipt->ipt_len - sizeof(int32_t)) { 1212 if (++ipt->ipt_oflw == 0) 1213 goto bad; 1214 break; 1215 } 1216 sin = (struct in_addr *)(cp + ipt->ipt_ptr - 1); 1217 switch (ipt->ipt_flg) { 1218 1219 case IPOPT_TS_TSONLY: 1220 break; 1221 1222 case IPOPT_TS_TSANDADDR: 1223 if (ipt->ipt_ptr - 1 + sizeof(n_time) + 1224 sizeof(struct in_addr) > ipt->ipt_len) 1225 goto bad; 1226 ipaddr.sin_addr = dst; 1227 ia = (INA)ifaof_ifpforaddr((SA)&ipaddr, 1228 m->m_pkthdr.rcvif); 1229 if (ia == 0) 1230 continue; 1231 (void)memcpy(sin, &IA_SIN(ia)->sin_addr, 1232 sizeof(struct in_addr)); 1233 ipt->ipt_ptr += sizeof(struct in_addr); 1234 break; 1235 1236 case IPOPT_TS_PRESPEC: 1237 if (ipt->ipt_ptr - 1 + sizeof(n_time) + 1238 sizeof(struct in_addr) > ipt->ipt_len) 1239 goto bad; 1240 (void)memcpy(&ipaddr.sin_addr, sin, 1241 sizeof(struct in_addr)); 1242 if (ifa_ifwithaddr((SA)&ipaddr) == 0) 1243 continue; 1244 ipt->ipt_ptr += sizeof(struct in_addr); 1245 break; 1246 1247 default: 1248 goto bad; 1249 } 1250 ntime = iptime(); 1251 (void)memcpy(cp + ipt->ipt_ptr - 1, &ntime, 1252 sizeof(n_time)); 1253 ipt->ipt_ptr += sizeof(n_time); 1254 } 1255 } 1256 if (forward && ipforwarding) { 1257 ip_forward(m, 1); 1258 return (1); 1259 } 1260 return (0); 1261 bad: 1262 ip->ip_len -= IP_VHL_HL(ip->ip_vhl) << 2; /* XXX icmp_error adds in hdr length */ 1263 icmp_error(m, type, code, 0, 0); 1264 ipstat.ips_badoptions++; 1265 return (1); 1266 } 1267 1268 /* 1269 * Given address of next destination (final or next hop), 1270 * return internet address info of interface to be used to get there. 1271 */ 1272 static struct in_ifaddr * 1273 ip_rtaddr(dst) 1274 struct in_addr dst; 1275 { 1276 register struct sockaddr_in *sin; 1277 1278 sin = (struct sockaddr_in *) &ipforward_rt.ro_dst; 1279 1280 if (ipforward_rt.ro_rt == 0 || dst.s_addr != sin->sin_addr.s_addr) { 1281 if (ipforward_rt.ro_rt) { 1282 RTFREE(ipforward_rt.ro_rt); 1283 ipforward_rt.ro_rt = 0; 1284 } 1285 sin->sin_family = AF_INET; 1286 sin->sin_len = sizeof(*sin); 1287 sin->sin_addr = dst; 1288 1289 rtalloc_ign(&ipforward_rt, RTF_PRCLONING); 1290 } 1291 if (ipforward_rt.ro_rt == 0) 1292 return ((struct in_ifaddr *)0); 1293 return ((struct in_ifaddr *) ipforward_rt.ro_rt->rt_ifa); 1294 } 1295 1296 /* 1297 * Save incoming source route for use in replies, 1298 * to be picked up later by ip_srcroute if the receiver is interested. 1299 */ 1300 void 1301 save_rte(option, dst) 1302 u_char *option; 1303 struct in_addr dst; 1304 { 1305 unsigned olen; 1306 1307 olen = option[IPOPT_OLEN]; 1308 #ifdef DIAGNOSTIC 1309 if (ipprintfs) 1310 printf("save_rte: olen %d\n", olen); 1311 #endif 1312 if (olen > sizeof(ip_srcrt) - (1 + sizeof(dst))) 1313 return; 1314 bcopy(option, ip_srcrt.srcopt, olen); 1315 ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr); 1316 ip_srcrt.dst = dst; 1317 } 1318 1319 /* 1320 * Retrieve incoming source route for use in replies, 1321 * in the same form used by setsockopt. 1322 * The first hop is placed before the options, will be removed later. 1323 */ 1324 struct mbuf * 1325 ip_srcroute() 1326 { 1327 register struct in_addr *p, *q; 1328 register struct mbuf *m; 1329 1330 if (ip_nhops == 0) 1331 return ((struct mbuf *)0); 1332 m = m_get(M_DONTWAIT, MT_HEADER); 1333 if (m == 0) 1334 return ((struct mbuf *)0); 1335 1336 #define OPTSIZ (sizeof(ip_srcrt.nop) + sizeof(ip_srcrt.srcopt)) 1337 1338 /* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */ 1339 m->m_len = ip_nhops * sizeof(struct in_addr) + sizeof(struct in_addr) + 1340 OPTSIZ; 1341 #ifdef DIAGNOSTIC 1342 if (ipprintfs) 1343 printf("ip_srcroute: nhops %d mlen %d", ip_nhops, m->m_len); 1344 #endif 1345 1346 /* 1347 * First save first hop for return route 1348 */ 1349 p = &ip_srcrt.route[ip_nhops - 1]; 1350 *(mtod(m, struct in_addr *)) = *p--; 1351 #ifdef DIAGNOSTIC 1352 if (ipprintfs) 1353 printf(" hops %lx", (u_long)ntohl(mtod(m, struct in_addr *)->s_addr)); 1354 #endif 1355 1356 /* 1357 * Copy option fields and padding (nop) to mbuf. 1358 */ 1359 ip_srcrt.nop = IPOPT_NOP; 1360 ip_srcrt.srcopt[IPOPT_OFFSET] = IPOPT_MINOFF; 1361 (void)memcpy(mtod(m, caddr_t) + sizeof(struct in_addr), 1362 &ip_srcrt.nop, OPTSIZ); 1363 q = (struct in_addr *)(mtod(m, caddr_t) + 1364 sizeof(struct in_addr) + OPTSIZ); 1365 #undef OPTSIZ 1366 /* 1367 * Record return path as an IP source route, 1368 * reversing the path (pointers are now aligned). 1369 */ 1370 while (p >= ip_srcrt.route) { 1371 #ifdef DIAGNOSTIC 1372 if (ipprintfs) 1373 printf(" %lx", (u_long)ntohl(q->s_addr)); 1374 #endif 1375 *q++ = *p--; 1376 } 1377 /* 1378 * Last hop goes to final destination. 1379 */ 1380 *q = ip_srcrt.dst; 1381 #ifdef DIAGNOSTIC 1382 if (ipprintfs) 1383 printf(" %lx\n", (u_long)ntohl(q->s_addr)); 1384 #endif 1385 return (m); 1386 } 1387 1388 /* 1389 * Strip out IP options, at higher 1390 * level protocol in the kernel. 1391 * Second argument is buffer to which options 1392 * will be moved, and return value is their length. 1393 * XXX should be deleted; last arg currently ignored. 1394 */ 1395 void 1396 ip_stripoptions(m, mopt) 1397 register struct mbuf *m; 1398 struct mbuf *mopt; 1399 { 1400 register int i; 1401 struct ip *ip = mtod(m, struct ip *); 1402 register caddr_t opts; 1403 int olen; 1404 1405 olen = (IP_VHL_HL(ip->ip_vhl) << 2) - sizeof (struct ip); 1406 opts = (caddr_t)(ip + 1); 1407 i = m->m_len - (sizeof (struct ip) + olen); 1408 bcopy(opts + olen, opts, (unsigned)i); 1409 m->m_len -= olen; 1410 if (m->m_flags & M_PKTHDR) 1411 m->m_pkthdr.len -= olen; 1412 ip->ip_vhl = IP_MAKE_VHL(IPVERSION, sizeof(struct ip) >> 2); 1413 } 1414 1415 u_char inetctlerrmap[PRC_NCMDS] = { 1416 0, 0, 0, 0, 1417 0, EMSGSIZE, EHOSTDOWN, EHOSTUNREACH, 1418 EHOSTUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED, 1419 EMSGSIZE, EHOSTUNREACH, 0, 0, 1420 0, 0, 0, 0, 1421 ENOPROTOOPT 1422 }; 1423 1424 /* 1425 * Forward a packet. If some error occurs return the sender 1426 * an icmp packet. Note we can't always generate a meaningful 1427 * icmp message because icmp doesn't have a large enough repertoire 1428 * of codes and types. 1429 * 1430 * If not forwarding, just drop the packet. This could be confusing 1431 * if ipforwarding was zero but some routing protocol was advancing 1432 * us as a gateway to somewhere. However, we must let the routing 1433 * protocol deal with that. 1434 * 1435 * The srcrt parameter indicates whether the packet is being forwarded 1436 * via a source route. 1437 */ 1438 static void 1439 ip_forward(m, srcrt) 1440 struct mbuf *m; 1441 int srcrt; 1442 { 1443 register struct ip *ip = mtod(m, struct ip *); 1444 register struct sockaddr_in *sin; 1445 register struct rtentry *rt; 1446 int error, type = 0, code = 0; 1447 struct mbuf *mcopy; 1448 n_long dest; 1449 struct ifnet *destifp; 1450 #ifdef IPSEC 1451 struct ifnet dummyifp; 1452 #endif 1453 1454 dest = 0; 1455 #ifdef DIAGNOSTIC 1456 if (ipprintfs) 1457 printf("forward: src %lx dst %lx ttl %x\n", 1458 (u_long)ip->ip_src.s_addr, (u_long)ip->ip_dst.s_addr, 1459 ip->ip_ttl); 1460 #endif 1461 1462 1463 if (m->m_flags & (M_BCAST|M_MCAST) || in_canforward(ip->ip_dst) == 0) { 1464 ipstat.ips_cantforward++; 1465 m_freem(m); 1466 return; 1467 } 1468 HTONS(ip->ip_id); 1469 #ifdef IPSTEALTH 1470 if (!ipstealth) { 1471 #endif 1472 if (ip->ip_ttl <= IPTTLDEC) { 1473 icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, 1474 dest, 0); 1475 return; 1476 } 1477 ip->ip_ttl -= IPTTLDEC; 1478 #ifdef IPSTEALTH 1479 } 1480 #endif 1481 1482 sin = (struct sockaddr_in *)&ipforward_rt.ro_dst; 1483 if ((rt = ipforward_rt.ro_rt) == 0 || 1484 ip->ip_dst.s_addr != sin->sin_addr.s_addr) { 1485 if (ipforward_rt.ro_rt) { 1486 RTFREE(ipforward_rt.ro_rt); 1487 ipforward_rt.ro_rt = 0; 1488 } 1489 sin->sin_family = AF_INET; 1490 sin->sin_len = sizeof(*sin); 1491 sin->sin_addr = ip->ip_dst; 1492 1493 rtalloc_ign(&ipforward_rt, RTF_PRCLONING); 1494 if (ipforward_rt.ro_rt == 0) { 1495 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, dest, 0); 1496 return; 1497 } 1498 rt = ipforward_rt.ro_rt; 1499 } 1500 1501 /* 1502 * Save at most 64 bytes of the packet in case 1503 * we need to generate an ICMP message to the src. 1504 */ 1505 mcopy = m_copy(m, 0, imin((int)ip->ip_len, 64)); 1506 1507 /* 1508 * If forwarding packet using same interface that it came in on, 1509 * perhaps should send a redirect to sender to shortcut a hop. 1510 * Only send redirect if source is sending directly to us, 1511 * and if packet was not source routed (or has any options). 1512 * Also, don't send redirect if forwarding using a default route 1513 * or a route modified by a redirect. 1514 */ 1515 #define satosin(sa) ((struct sockaddr_in *)(sa)) 1516 if (rt->rt_ifp == m->m_pkthdr.rcvif && 1517 (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 && 1518 satosin(rt_key(rt))->sin_addr.s_addr != 0 && 1519 ipsendredirects && !srcrt) { 1520 #define RTA(rt) ((struct in_ifaddr *)(rt->rt_ifa)) 1521 u_long src = ntohl(ip->ip_src.s_addr); 1522 1523 if (RTA(rt) && 1524 (src & RTA(rt)->ia_subnetmask) == RTA(rt)->ia_subnet) { 1525 if (rt->rt_flags & RTF_GATEWAY) 1526 dest = satosin(rt->rt_gateway)->sin_addr.s_addr; 1527 else 1528 dest = ip->ip_dst.s_addr; 1529 /* Router requirements says to only send host redirects */ 1530 type = ICMP_REDIRECT; 1531 code = ICMP_REDIRECT_HOST; 1532 #ifdef DIAGNOSTIC 1533 if (ipprintfs) 1534 printf("redirect (%d) to %lx\n", code, (u_long)dest); 1535 #endif 1536 } 1537 } 1538 1539 error = ip_output(m, (struct mbuf *)0, &ipforward_rt, 1540 IP_FORWARDING, 0); 1541 if (error) 1542 ipstat.ips_cantforward++; 1543 else { 1544 ipstat.ips_forward++; 1545 if (type) 1546 ipstat.ips_redirectsent++; 1547 else { 1548 if (mcopy) { 1549 ipflow_create(&ipforward_rt, mcopy); 1550 m_freem(mcopy); 1551 } 1552 return; 1553 } 1554 } 1555 if (mcopy == NULL) 1556 return; 1557 destifp = NULL; 1558 1559 switch (error) { 1560 1561 case 0: /* forwarded, but need redirect */ 1562 /* type, code set above */ 1563 break; 1564 1565 case ENETUNREACH: /* shouldn't happen, checked above */ 1566 case EHOSTUNREACH: 1567 case ENETDOWN: 1568 case EHOSTDOWN: 1569 default: 1570 type = ICMP_UNREACH; 1571 code = ICMP_UNREACH_HOST; 1572 break; 1573 1574 case EMSGSIZE: 1575 type = ICMP_UNREACH; 1576 code = ICMP_UNREACH_NEEDFRAG; 1577 #ifndef IPSEC 1578 if (ipforward_rt.ro_rt) 1579 destifp = ipforward_rt.ro_rt->rt_ifp; 1580 #else 1581 /* 1582 * If the packet is routed over IPsec tunnel, tell the 1583 * originator the tunnel MTU. 1584 * tunnel MTU = if MTU - sizeof(IP) - ESP/AH hdrsiz 1585 * XXX quickhack!!! 1586 */ 1587 if (ipforward_rt.ro_rt) { 1588 struct secpolicy *sp = NULL; 1589 int ipsecerror; 1590 int ipsechdr; 1591 struct route *ro; 1592 1593 sp = ipsec4_getpolicybyaddr(mcopy, 1594 IPSEC_DIR_OUTBOUND, 1595 IP_FORWARDING, 1596 &ipsecerror); 1597 1598 if (sp == NULL) 1599 destifp = ipforward_rt.ro_rt->rt_ifp; 1600 else { 1601 /* count IPsec header size */ 1602 ipsechdr = ipsec4_hdrsiz(mcopy, 1603 IPSEC_DIR_OUTBOUND, 1604 NULL); 1605 1606 /* 1607 * find the correct route for outer IPv4 1608 * header, compute tunnel MTU. 1609 * 1610 * XXX BUG ALERT 1611 * The "dummyifp" code relies upon the fact 1612 * that icmp_error() touches only ifp->if_mtu. 1613 */ 1614 /*XXX*/ 1615 destifp = NULL; 1616 if (sp->req != NULL 1617 && sp->req->sav != NULL 1618 && sp->req->sav->sah != NULL) { 1619 ro = &sp->req->sav->sah->sa_route; 1620 if (ro->ro_rt && ro->ro_rt->rt_ifp) { 1621 dummyifp.if_mtu = 1622 ro->ro_rt->rt_ifp->if_mtu; 1623 dummyifp.if_mtu -= ipsechdr; 1624 destifp = &dummyifp; 1625 } 1626 } 1627 1628 key_freesp(sp); 1629 } 1630 } 1631 #endif /*IPSEC*/ 1632 ipstat.ips_cantfrag++; 1633 break; 1634 1635 case ENOBUFS: 1636 type = ICMP_SOURCEQUENCH; 1637 code = 0; 1638 break; 1639 } 1640 icmp_error(mcopy, type, code, dest, destifp); 1641 } 1642 1643 void 1644 ip_savecontrol(inp, mp, ip, m) 1645 register struct inpcb *inp; 1646 register struct mbuf **mp; 1647 register struct ip *ip; 1648 register struct mbuf *m; 1649 { 1650 if (inp->inp_socket->so_options & SO_TIMESTAMP) { 1651 struct timeval tv; 1652 1653 microtime(&tv); 1654 *mp = sbcreatecontrol((caddr_t) &tv, sizeof(tv), 1655 SCM_TIMESTAMP, SOL_SOCKET); 1656 if (*mp) 1657 mp = &(*mp)->m_next; 1658 } 1659 if (inp->inp_flags & INP_RECVDSTADDR) { 1660 *mp = sbcreatecontrol((caddr_t) &ip->ip_dst, 1661 sizeof(struct in_addr), IP_RECVDSTADDR, IPPROTO_IP); 1662 if (*mp) 1663 mp = &(*mp)->m_next; 1664 } 1665 #ifdef notyet 1666 /* XXX 1667 * Moving these out of udp_input() made them even more broken 1668 * than they already were. 1669 */ 1670 /* options were tossed already */ 1671 if (inp->inp_flags & INP_RECVOPTS) { 1672 *mp = sbcreatecontrol((caddr_t) opts_deleted_above, 1673 sizeof(struct in_addr), IP_RECVOPTS, IPPROTO_IP); 1674 if (*mp) 1675 mp = &(*mp)->m_next; 1676 } 1677 /* ip_srcroute doesn't do what we want here, need to fix */ 1678 if (inp->inp_flags & INP_RECVRETOPTS) { 1679 *mp = sbcreatecontrol((caddr_t) ip_srcroute(), 1680 sizeof(struct in_addr), IP_RECVRETOPTS, IPPROTO_IP); 1681 if (*mp) 1682 mp = &(*mp)->m_next; 1683 } 1684 #endif 1685 if (inp->inp_flags & INP_RECVIF) { 1686 struct ifnet *ifp; 1687 struct sdlbuf { 1688 struct sockaddr_dl sdl; 1689 u_char pad[32]; 1690 } sdlbuf; 1691 struct sockaddr_dl *sdp; 1692 struct sockaddr_dl *sdl2 = &sdlbuf.sdl; 1693 1694 if (((ifp = m->m_pkthdr.rcvif)) 1695 && ( ifp->if_index && (ifp->if_index <= if_index))) { 1696 sdp = (struct sockaddr_dl *)(ifnet_addrs 1697 [ifp->if_index - 1]->ifa_addr); 1698 /* 1699 * Change our mind and don't try copy. 1700 */ 1701 if ((sdp->sdl_family != AF_LINK) 1702 || (sdp->sdl_len > sizeof(sdlbuf))) { 1703 goto makedummy; 1704 } 1705 bcopy(sdp, sdl2, sdp->sdl_len); 1706 } else { 1707 makedummy: 1708 sdl2->sdl_len 1709 = offsetof(struct sockaddr_dl, sdl_data[0]); 1710 sdl2->sdl_family = AF_LINK; 1711 sdl2->sdl_index = 0; 1712 sdl2->sdl_nlen = sdl2->sdl_alen = sdl2->sdl_slen = 0; 1713 } 1714 *mp = sbcreatecontrol((caddr_t) sdl2, sdl2->sdl_len, 1715 IP_RECVIF, IPPROTO_IP); 1716 if (*mp) 1717 mp = &(*mp)->m_next; 1718 } 1719 } 1720 1721 int 1722 ip_rsvp_init(struct socket *so) 1723 { 1724 if (so->so_type != SOCK_RAW || 1725 so->so_proto->pr_protocol != IPPROTO_RSVP) 1726 return EOPNOTSUPP; 1727 1728 if (ip_rsvpd != NULL) 1729 return EADDRINUSE; 1730 1731 ip_rsvpd = so; 1732 /* 1733 * This may seem silly, but we need to be sure we don't over-increment 1734 * the RSVP counter, in case something slips up. 1735 */ 1736 if (!ip_rsvp_on) { 1737 ip_rsvp_on = 1; 1738 rsvp_on++; 1739 } 1740 1741 return 0; 1742 } 1743 1744 int 1745 ip_rsvp_done(void) 1746 { 1747 ip_rsvpd = NULL; 1748 /* 1749 * This may seem silly, but we need to be sure we don't over-decrement 1750 * the RSVP counter, in case something slips up. 1751 */ 1752 if (ip_rsvp_on) { 1753 ip_rsvp_on = 0; 1754 rsvp_on--; 1755 } 1756 return 0; 1757 } 1758