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