2  * Copyright (c) 2000 Apple Computer, Inc. All rights reserved. 
   4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ 
   6  * This file contains Original Code and/or Modifications of Original Code 
   7  * as defined in and that are subject to the Apple Public Source License 
   8  * Version 2.0 (the 'License'). You may not use this file except in 
   9  * compliance with the License. The rights granted to you under the License 
  10  * may not be used to create, or enable the creation or redistribution of, 
  11  * unlawful or unlicensed copies of an Apple operating system, or to 
  12  * circumvent, violate, or enable the circumvention or violation of, any 
  13  * terms of an Apple operating system software license agreement. 
  15  * Please obtain a copy of the License at 
  16  * http://www.opensource.apple.com/apsl/ and read it before using this file. 
  18  * The Original Code and all software distributed under the License are 
  19  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER 
  20  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, 
  21  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, 
  22  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. 
  23  * Please see the License for the specific language governing rights and 
  24  * limitations under the License. 
  26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ 
  29  * Copyright (c) 1982, 1986, 1988, 1993 
  30  *      The Regents of the University of California.  All rights reserved. 
  32  * Redistribution and use in source and binary forms, with or without 
  33  * modification, are permitted provided that the following conditions 
  35  * 1. Redistributions of source code must retain the above copyright 
  36  *    notice, this list of conditions and the following disclaimer. 
  37  * 2. Redistributions in binary form must reproduce the above copyright 
  38  *    notice, this list of conditions and the following disclaimer in the 
  39  *    documentation and/or other materials provided with the distribution. 
  40  * 3. All advertising materials mentioning features or use of this software 
  41  *    must display the following acknowledgement: 
  42  *      This product includes software developed by the University of 
  43  *      California, Berkeley and its contributors. 
  44  * 4. Neither the name of the University nor the names of its contributors 
  45  *    may be used to endorse or promote products derived from this software 
  46  *    without specific prior written permission. 
  48  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 
  49  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
  50  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
  51  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 
  52  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
  53  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 
  54  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
  55  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
  56  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 
  57  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 
  60  *      @(#)ip_input.c  8.2 (Berkeley) 1/4/94 
  61  * $FreeBSD: src/sys/netinet/ip_input.c,v 1.130.2.25 2001/08/29 21:41:37 jesper Exp $ 
  66 #include <sys/param.h> 
  67 #include <sys/systm.h> 
  69 #include <sys/malloc.h> 
  70 #include <sys/domain.h> 
  71 #include <sys/protosw.h> 
  72 #include <sys/socket.h> 
  74 #include <sys/kernel.h> 
  75 #include <sys/syslog.h> 
  76 #include <sys/sysctl.h> 
  78 #include <kern/queue.h> 
  79 #include <kern/locks.h> 
  82 #include <net/if_var.h> 
  83 #include <net/if_dl.h> 
  84 #include <net/route.h> 
  85 #include <net/kpi_protocol.h> 
  87 #include <netinet/in.h> 
  88 #include <netinet/in_systm.h> 
  89 #include <netinet/in_var.h> 
  90 #include <netinet/ip.h> 
  91 #include <netinet/in_pcb.h> 
  92 #include <netinet/ip_var.h> 
  93 #include <netinet/ip_icmp.h> 
  94 #include <sys/socketvar.h> 
  96 #include <netinet/ip_fw.h> 
  97 #include <netinet/ip_divert.h> 
  99 #include <netinet/kpi_ipfilter_var.h> 
 101 /* needed for AUTOCONFIGURING: */ 
 102 #include <netinet/udp.h> 
 103 #include <netinet/udp_var.h> 
 104 #include <netinet/bootp.h> 
 106 #include <sys/kdebug.h> 
 108 #define DBG_LAYER_BEG           NETDBG_CODE(DBG_NETIP, 0) 
 109 #define DBG_LAYER_END           NETDBG_CODE(DBG_NETIP, 2) 
 110 #define DBG_FNC_IP_INPUT        NETDBG_CODE(DBG_NETIP, (2 << 8)) 
 114 #include <netinet6/ipsec.h> 
 115 #include <netkey/key.h> 
 119 #if defined(NFAITH) && NFAITH > 0 
 120 #include <net/if_types.h> 
 124 #include <netinet/ip_dummynet.h> 
 128 extern int ipsec_bypass
; 
 129 extern lck_mtx_t 
*sadb_mutex
; 
 133 static int ip_rsvp_on
; 
 134 struct socket 
*ip_rsvpd
; 
 136 int     ipforwarding 
= 0; 
 137 SYSCTL_INT(_net_inet_ip
, IPCTL_FORWARDING
, forwarding
, CTLFLAG_RW
, 
 138     &ipforwarding
, 0, "Enable IP forwarding between interfaces"); 
 140 static int      ipsendredirects 
= 1; /* XXX */ 
 141 SYSCTL_INT(_net_inet_ip
, IPCTL_SENDREDIRECTS
, redirect
, CTLFLAG_RW
, 
 142     &ipsendredirects
, 0, "Enable sending IP redirects"); 
 144 int     ip_defttl 
= IPDEFTTL
; 
 145 SYSCTL_INT(_net_inet_ip
, IPCTL_DEFTTL
, ttl
, CTLFLAG_RW
, 
 146     &ip_defttl
, 0, "Maximum TTL on IP packets"); 
 148 static int      ip_dosourceroute 
= 0; 
 149 SYSCTL_INT(_net_inet_ip
, IPCTL_SOURCEROUTE
, sourceroute
, CTLFLAG_RW
, 
 150     &ip_dosourceroute
, 0, "Enable forwarding source routed IP packets"); 
 152 static int      ip_acceptsourceroute 
= 0; 
 153 SYSCTL_INT(_net_inet_ip
, IPCTL_ACCEPTSOURCEROUTE
, accept_sourceroute
,  
 154     CTLFLAG_RW
, &ip_acceptsourceroute
, 0,  
 155     "Enable accepting source routed IP packets"); 
 157 static int      ip_keepfaith 
= 0; 
 158 SYSCTL_INT(_net_inet_ip
, IPCTL_KEEPFAITH
, keepfaith
, CTLFLAG_RW
, 
 160         "Enable packet capture for FAITH IPv4->IPv6 translater daemon"); 
 162 static int      nipq 
= 0;       /* total # of reass queues */ 
 164 SYSCTL_INT(_net_inet_ip
, OID_AUTO
, maxfragpackets
, CTLFLAG_RW
, 
 166         "Maximum number of IPv4 fragment reassembly queue entries"); 
 168 static int    maxfragsperpacket
; 
 169 SYSCTL_INT(_net_inet_ip
, OID_AUTO
, maxfragsperpacket
, CTLFLAG_RW
, 
 170         &maxfragsperpacket
, 0, 
 171         "Maximum number of IPv4 fragments allowed per packet"); 
 174 SYSCTL_INT(_net_inet_ip
, OID_AUTO
, maxfrags
, CTLFLAG_RW
, 
 175         &maxfrags
, 0, "Maximum number of IPv4 fragments allowed"); 
 177 static int    currentfrags 
= 0; 
 180  * XXX - Setting ip_checkinterface mostly implements the receive side of 
 181  * the Strong ES model described in RFC 1122, but since the routing table 
 182  * and transmit implementation do not implement the Strong ES model, 
 183  * setting this to 1 results in an odd hybrid. 
 185  * XXX - ip_checkinterface currently must be disabled if you use ipnat 
 186  * to translate the destination address to another local interface. 
 188  * XXX - ip_checkinterface must be disabled if you add IP aliases 
 189  * to the loopback interface instead of the interface where the 
 190  * packets for those addresses are received. 
 192 static int      ip_checkinterface 
= 0; 
 193 SYSCTL_INT(_net_inet_ip
, OID_AUTO
, check_interface
, CTLFLAG_RW
, 
 194     &ip_checkinterface
, 0, "Verify packet arrives on correct interface"); 
 197 static int      ipprintfs 
= 0; 
 200 extern  struct domain inetdomain
; 
 201 extern  struct protosw inetsw
[]; 
 202 struct protosw 
*ip_protox
[IPPROTO_MAX
]; 
 203 static int      ipqmaxlen 
= IFQ_MAXLEN
; 
 204 struct  in_ifaddrhead in_ifaddrhead
; /* first inet address */ 
 205 struct  ifqueue ipintrq
; 
 206 SYSCTL_INT(_net_inet_ip
, IPCTL_INTRQMAXLEN
, intr_queue_maxlen
, CTLFLAG_RW
, 
 207     &ipintrq
.ifq_maxlen
, 0, "Maximum size of the IP input queue"); 
 208 SYSCTL_INT(_net_inet_ip
, IPCTL_INTRQDROPS
, intr_queue_drops
, CTLFLAG_RD
, 
 209     &ipintrq
.ifq_drops
, 0, "Number of packets dropped from the IP input queue"); 
 211 struct ipstat ipstat
; 
 212 SYSCTL_STRUCT(_net_inet_ip
, IPCTL_STATS
, stats
, CTLFLAG_RD
, 
 213     &ipstat
, ipstat
, "IP statistics (struct ipstat, netinet/ip_var.h)"); 
 215 /* Packet reassembly stuff */ 
 216 #define IPREASS_NHASH_LOG2      6 
 217 #define IPREASS_NHASH           (1 << IPREASS_NHASH_LOG2) 
 218 #define IPREASS_HMASK           (IPREASS_NHASH - 1) 
 219 #define IPREASS_HASH(x,y) \ 
 220         (((((x) & 0xF) | ((((x) >> 8) & 0xF) << 4)) ^ (y)) & IPREASS_HMASK) 
 222 static struct ipq ipq
[IPREASS_NHASH
]; 
 223 static TAILQ_HEAD(ipq_list
, ipq
) ipq_list 
= 
 224         TAILQ_HEAD_INITIALIZER(ipq_list
); 
 225 const  int    ipintrq_present 
= 1; 
 227 lck_attr_t              
*ip_mutex_attr
; 
 228 lck_grp_t                       
*ip_mutex_grp
; 
 229 lck_grp_attr_t  
*ip_mutex_grp_attr
; 
 230 lck_mtx_t               
*inet_domain_mutex
; 
 231 extern lck_mtx_t                
*domain_proto_mtx
; 
 234 SYSCTL_INT(_net_inet_ip
, IPCTL_DEFMTU
, mtu
, CTLFLAG_RW
, 
 235     &ip_mtu
, 0, "Default MTU"); 
 239 static int      ipstealth 
= 0; 
 240 SYSCTL_INT(_net_inet_ip
, OID_AUTO
, stealth
, CTLFLAG_RW
, 
 246 ip_fw_chk_t 
*ip_fw_chk_ptr
; 
 251 ip_dn_io_t 
*ip_dn_io_ptr
; 
 254 int (*fr_checkp
)(struct ip 
*, int, struct ifnet 
*, int, struct mbuf 
**) = NULL
; 
 256 SYSCTL_NODE(_net_inet_ip
, OID_AUTO
, linklocal
, CTLFLAG_RW
, 0, "link local"); 
 258 struct ip_linklocal_stat ip_linklocal_stat
; 
 259 SYSCTL_STRUCT(_net_inet_ip_linklocal
, OID_AUTO
, stat
, CTLFLAG_RD
, 
 260         &ip_linklocal_stat
, ip_linklocal_stat
, 
 261         "Number of link local packets with TTL less than 255"); 
 263 SYSCTL_NODE(_net_inet_ip_linklocal
, OID_AUTO
, in
, CTLFLAG_RW
, 0, "link local input"); 
 265 int ip_linklocal_in_allowbadttl 
= 1; 
 266 SYSCTL_INT(_net_inet_ip_linklocal_in
, OID_AUTO
, allowbadttl
, CTLFLAG_RW
, 
 267         &ip_linklocal_in_allowbadttl
, 0, 
 268         "Allow incoming link local packets with TTL less than 255"); 
 272  * We need to save the IP options in case a protocol wants to respond 
 273  * to an incoming packet over the same route if the packet got here 
 274  * using IP source routing.  This allows connection establishment and 
 275  * maintenance when the remote end is on a network that is not known 
 278 static int      ip_nhops 
= 0; 
 279 static  struct ip_srcrt 
{ 
 280         struct  in_addr dst
;                    /* final destination */ 
 281         char    nop
;                            /* one NOP to align */ 
 282         char    srcopt
[IPOPT_OFFSET 
+ 1];       /* OPTVAL, OLEN and OFFSET */ 
 283         struct  in_addr route
[MAX_IPOPTLEN
/sizeof(struct in_addr
)]; 
 287 extern struct mbuf
*     m_dup(register struct mbuf 
*m
, int how
); 
 290 static void     save_rte(u_char 
*, struct in_addr
); 
 291 static int      ip_dooptions(struct mbuf 
*, int, struct sockaddr_in 
*, struct route 
*ipforward_rt
); 
 292 static void     ip_forward(struct mbuf 
*, int, struct sockaddr_in 
*, struct route 
*ipforward_rt
); 
 293 static void     ip_freef(struct ipq 
*); 
 296 static struct   mbuf 
*ip_reass(struct mbuf 
*, 
 297                         struct ipq 
*, struct ipq 
*, u_int32_t 
*, u_int16_t 
*); 
 299 static struct   mbuf 
*ip_reass(struct mbuf 
*, 
 300                         struct ipq 
*, struct ipq 
*, u_int16_t 
*, u_int16_t 
*); 
 303 static struct   mbuf 
*ip_reass(struct mbuf 
*, struct ipq 
*, struct ipq 
*); 
 308 extern u_short ip_id
; 
 311 extern u_long  route_generation
; 
 312 extern int apple_hwcksum_rx
; 
 315  * IP initialization: fill in IP protocol switch table. 
 316  * All protocols not implemented in kernel go to raw IP protocol handler. 
 321         register struct protosw 
*pr
; 
 323         static ip_initialized 
= 0; 
 324         struct timeval timenow
; 
 329                 TAILQ_INIT(&in_ifaddrhead
); 
 330                 pr 
= pffindproto_locked(PF_INET
, IPPROTO_RAW
, SOCK_RAW
); 
 333                 for (i 
= 0; i 
< IPPROTO_MAX
; i
++) 
 335                 for (pr 
= inetdomain
.dom_protosw
; pr
; pr 
= pr
->pr_next
) 
 336                 {       if(!((unsigned int)pr
->pr_domain
)) continue;    /* If uninitialized, skip */ 
 337                         if (pr
->pr_domain
->dom_family 
== PF_INET 
&& 
 338                             pr
->pr_protocol 
&& pr
->pr_protocol 
!= IPPROTO_RAW
) 
 339                                 ip_protox
[pr
->pr_protocol
] = pr
; 
 341                 for (i 
= 0; i 
< IPREASS_NHASH
; i
++) 
 342                     ipq
[i
].next 
= ipq
[i
].prev 
= &ipq
[i
]; 
 344         maxnipq 
= nmbclusters 
/ 32; 
 345         maxfrags 
= maxnipq 
* 2; 
 346         maxfragsperpacket 
= 128; /* enough for 64k in 512 byte fragments */ 
 349                 getmicrouptime(&timenow
); 
 350                 ip_id 
= timenow
.tv_sec 
& 0xffff; 
 352                 ipintrq
.ifq_maxlen 
= ipqmaxlen
; 
 356                 ip_mutex_grp_attr  
= lck_grp_attr_alloc_init(); 
 358                 ip_mutex_grp 
= lck_grp_alloc_init("ip", ip_mutex_grp_attr
); 
 360                 ip_mutex_attr 
= lck_attr_alloc_init(); 
 362                 if ((ip_mutex 
= lck_mtx_alloc_init(ip_mutex_grp
, ip_mutex_attr
)) == NULL
) { 
 363                         printf("ip_init: can't alloc ip_mutex\n"); 
 373         protocol_family_t       protocol
, 
 379 /* Initialize the PF_INET domain, and add in the pre-defined protos */ 
 383         register struct protosw 
*pr
; 
 384         register struct domain 
*dp
; 
 385         static inetdomain_initted 
= 0; 
 386         extern int in_proto_count
;  
 388         if (!inetdomain_initted
) 
 390                 kprintf("Initing %d protosw entries\n", in_proto_count
); 
 392                 dp
->dom_flags 
= DOM_REENTRANT
; 
 394                 for (i
=0, pr 
= &inetsw
[0]; i
<in_proto_count
; i
++, pr
++) 
 395                         net_add_proto(pr
, dp
); 
 396                 inet_domain_mutex 
= dp
->dom_mtx
; 
 397                 inetdomain_initted 
= 1; 
 399                 lck_mtx_unlock(domain_proto_mtx
);        
 400                 proto_register_input(PF_INET
, ip_proto_input
, NULL
); 
 401                 lck_mtx_lock(domain_proto_mtx
);  
 405 __private_extern__ 
void 
 406 ip_proto_dispatch_in( 
 410                                         ipfilter_t      inject_ipfref
) 
 412         struct ipfilter 
*filter
; 
 413         int seen 
= (inject_ipfref 
== 0); 
 414         int     changed_header 
= 0; 
 417         if (!TAILQ_EMPTY(&ipv4_filters
)) {       
 419                 TAILQ_FOREACH(filter
, &ipv4_filters
, ipf_link
) { 
 421                                 if ((struct ipfilter 
*)inject_ipfref 
== filter
) 
 423                         } else if (filter
->ipf_filter
.ipf_input
) { 
 426                                 if (changed_header 
== 0) { 
 428                                         ip 
= mtod(m
, struct ip 
*); 
 429                                         ip
->ip_len 
= htons(ip
->ip_len 
+ hlen
); 
 430                                         ip
->ip_off 
= htons(ip
->ip_off
); 
 432                                         ip
->ip_sum 
= in_cksum(m
, hlen
); 
 434                                 result 
= filter
->ipf_filter
.ipf_input( 
 435                                         filter
->ipf_filter
.cookie
, (mbuf_t
*)&m
, hlen
, proto
); 
 436                                 if (result 
== EJUSTRETURN
) { 
 450          * If there isn't a specific lock for the protocol 
 451          * we're about to call, use the generic lock for AF_INET. 
 452          * otherwise let the protocol deal with its own locking 
 454         ip 
= mtod(m
, struct ip 
*); 
 456         if (changed_header
) { 
 457                 ip
->ip_len 
= ntohs(ip
->ip_len
) - hlen
; 
 458                 ip
->ip_off 
= ntohs(ip
->ip_off
); 
 461         if (!(ip_protox
[ip
->ip_p
]->pr_flags 
& PR_PROTOLOCK
)) { 
 462                 lck_mtx_lock(inet_domain_mutex
); 
 463                 (*ip_protox
[ip
->ip_p
]->pr_input
)(m
, hlen
); 
 464                 lck_mtx_unlock(inet_domain_mutex
); 
 467                 (*ip_protox
[ip
->ip_p
]->pr_input
)(m
, hlen
); 
 472  * ipforward_rt cleared in in_addroute() 
 473  * when a new route is successfully created. 
 475 static struct   sockaddr_in ipaddr 
= { sizeof(ipaddr
), AF_INET 
}; 
 478  * Ip input routine.  Checksum and byte swap header.  If fragmented 
 479  * try to reassemble.  Process options.  Pass to next level. 
 482 ip_input(struct mbuf 
*m
) 
 486         struct in_ifaddr 
*ia 
= NULL
; 
 487         int    i
, hlen
, mff
, checkif
; 
 489         struct in_addr pkt_dst
; 
 490         u_int32_t div_info 
= 0;         /* packet divert/tee info */ 
 491         struct ip_fw_args args
; 
 492         ipfilter_t inject_filter_ref 
= 0; 
 494         struct route    ipforward_rt 
= { 0 }; 
 496         lck_mtx_lock(ip_mutex
); 
 501         args
.divert_rule 
= 0;                   /* divert cookie */ 
 502         args
.next_hop 
= NULL
; 
 504         /* Grab info from mtags prepended to the chain */ 
 506         if ((tag 
= m_tag_locate(m
, KERNEL_MODULE_TAG_ID
, KERNEL_TAG_TYPE_DUMMYNET
, NULL
)) != NULL
) { 
 507                 struct dn_pkt_tag       
*dn_tag
; 
 509                 dn_tag 
= (struct dn_pkt_tag 
*)(tag
+1); 
 510                 args
.rule 
= dn_tag
->rule
; 
 512                 m_tag_delete(m
, tag
); 
 514 #endif /* DUMMYNET */ 
 516         if ((tag 
= m_tag_locate(m
, KERNEL_MODULE_TAG_ID
, KERNEL_TAG_TYPE_DIVERT
, NULL
)) != NULL
) { 
 517                 struct divert_tag       
*div_tag
; 
 519                 div_tag 
= (struct divert_tag 
*)(tag
+1); 
 520                 args
.divert_rule 
= div_tag
->cookie
; 
 522                 m_tag_delete(m
, tag
); 
 524         if ((tag 
= m_tag_locate(m
, KERNEL_MODULE_TAG_ID
, KERNEL_TAG_TYPE_IPFORWARD
, NULL
)) != NULL
) { 
 525                 struct ip_fwd_tag       
*ipfwd_tag
; 
 527                 ipfwd_tag 
= (struct ip_fwd_tag 
*)(tag
+1); 
 528                 args
.next_hop 
= ipfwd_tag
->next_hop
; 
 530                 m_tag_delete(m
, tag
); 
 534         if (m 
== NULL 
|| (m
->m_flags 
& M_PKTHDR
) == 0) 
 535                 panic("ip_input no HDR"); 
 538         if (args
.rule
) {        /* dummynet already filtered us */ 
 539             ip 
= mtod(m
, struct ip 
*); 
 540             hlen 
= IP_VHL_HL(ip
->ip_vhl
) << 2; 
 541             inject_filter_ref 
= ipf_get_inject_filter(m
); 
 546          * No need to proccess packet twice if we've  
 549         inject_filter_ref 
= ipf_get_inject_filter(m
); 
 550         if (inject_filter_ref 
!= 0) { 
 551                 lck_mtx_unlock(ip_mutex
); 
 552                 ip 
= mtod(m
, struct ip 
*); 
 553                 hlen 
= IP_VHL_HL(ip
->ip_vhl
) << 2; 
 554                 ip
->ip_len 
= ntohs(ip
->ip_len
) - hlen
; 
 555                 ip
->ip_off 
= ntohs(ip
->ip_off
); 
 556                 ip_proto_dispatch_in(m
, hlen
, ip
->ip_p
, inject_filter_ref
); 
 562         if (m
->m_pkthdr
.len 
< sizeof(struct ip
)) 
 565         if (m
->m_len 
< sizeof (struct ip
) && 
 566             (m 
= m_pullup(m
, sizeof (struct ip
))) == 0) { 
 567                 ipstat
.ips_toosmall
++; 
 568                 lck_mtx_unlock(ip_mutex
); 
 571         ip 
= mtod(m
, struct ip 
*); 
 573         KERNEL_DEBUG(DBG_LAYER_BEG
, ip
->ip_dst
.s_addr
,  
 574                      ip
->ip_src
.s_addr
, ip
->ip_p
, ip
->ip_off
, ip
->ip_len
); 
 576         if (IP_VHL_V(ip
->ip_vhl
) != IPVERSION
) { 
 577                 ipstat
.ips_badvers
++; 
 581         hlen 
= IP_VHL_HL(ip
->ip_vhl
) << 2; 
 582         if (hlen 
< sizeof(struct ip
)) { /* minimum header length */ 
 583                 ipstat
.ips_badhlen
++; 
 586         if (hlen 
> m
->m_len
) { 
 587                 if ((m 
= m_pullup(m
, hlen
)) == 0) { 
 588                         ipstat
.ips_badhlen
++; 
 589                         lck_mtx_unlock(ip_mutex
); 
 592                 ip 
= mtod(m
, struct ip 
*); 
 595         /* 127/8 must not appear on wire - RFC1122 */ 
 596         if ((ntohl(ip
->ip_dst
.s_addr
) >> IN_CLASSA_NSHIFT
) == IN_LOOPBACKNET 
|| 
 597             (ntohl(ip
->ip_src
.s_addr
) >> IN_CLASSA_NSHIFT
) == IN_LOOPBACKNET
) { 
 598                 if ((m
->m_pkthdr
.rcvif
->if_flags 
& IFF_LOOPBACK
) == 0) { 
 599                         ipstat
.ips_badaddr
++; 
 604         /* IPv4 Link-Local Addresses as defined in <draft-ietf-zeroconf-ipv4-linklocal-05.txt> */ 
 605         if ((IN_LINKLOCAL(ntohl(ip
->ip_dst
.s_addr
)) ||  
 606             IN_LINKLOCAL(ntohl(ip
->ip_src
.s_addr
)))) { 
 607                 ip_linklocal_stat
.iplls_in_total
++; 
 608                 if (ip
->ip_ttl 
!= MAXTTL
) { 
 609                         ip_linklocal_stat
.iplls_in_badttl
++; 
 610                         /* Silently drop link local traffic with bad TTL */ 
 611                         if (!ip_linklocal_in_allowbadttl
) 
 615         if ((IF_HWASSIST_CSUM_FLAGS(m
->m_pkthdr
.rcvif
->if_hwassist
) == 0)  
 616             || (apple_hwcksum_rx 
== 0) || 
 617            ((m
->m_pkthdr
.csum_flags 
& CSUM_TCP_SUM16
) && ip
->ip_p 
!= IPPROTO_TCP
)) { 
 618                         m
->m_pkthdr
.csum_flags 
= 0; /* invalidate HW generated checksum flags */ 
 621         if (m
->m_pkthdr
.csum_flags 
& CSUM_IP_CHECKED
) { 
 622                 sum 
= !(m
->m_pkthdr
.csum_flags 
& CSUM_IP_VALID
); 
 624                 sum 
= in_cksum(m
, hlen
); 
 632          * Convert fields to host representation. 
 635         if (ip
->ip_len 
< hlen
) { 
 642          * Check that the amount of data in the buffers 
 643          * is as at least much as the IP header would have us expect. 
 644          * Trim mbufs if longer than we expect. 
 645          * Drop packet if shorter than we expect. 
 647         if (m
->m_pkthdr
.len 
< ip
->ip_len
) { 
 649                 ipstat
.ips_tooshort
++; 
 652         if (m
->m_pkthdr
.len 
> ip
->ip_len
) { 
 653                 /* Invalidate hwcksuming */ 
 654                 m
->m_pkthdr
.csum_flags 
= 0; 
 655                 m
->m_pkthdr
.csum_data 
= 0; 
 657                 if (m
->m_len 
== m
->m_pkthdr
.len
) { 
 658                         m
->m_len 
= ip
->ip_len
; 
 659                         m
->m_pkthdr
.len 
= ip
->ip_len
; 
 661                         m_adj(m
, ip
->ip_len 
- m
->m_pkthdr
.len
); 
 665         if (ipsec_bypass 
== 0 && ipsec_gethist(m
, NULL
)) 
 671          * Right now when no processing on packet has done 
 672          * and it is still fresh out of network we do our black 
 674          * - Firewall: deny/allow/divert 
 675          * - Xlate: translate packet's addr/port (NAT). 
 676          * - Pipe: pass pkt through dummynet. 
 677          * - Wrap: fake packet's addr/port <unimpl.> 
 678          * - Encapsulate: put it in another IP and send out. <unimp.> 
 681 #if defined(IPFIREWALL) && defined(DUMMYNET) 
 685          * Check if we want to allow this packet to be processed. 
 686          * Consider it to be bad if not. 
 691                 if (fr_checkp(ip
, hlen
, m
->m_pkthdr
.rcvif
, 0, &m1
) || !m1
) { 
 692                         lck_mtx_unlock(ip_mutex
); 
 695                 ip 
= mtod(m 
= m1
, struct ip 
*); 
 697         if (fw_enable 
&& IPFW_LOADED
) { 
 698 #if IPFIREWALL_FORWARD 
 700                  * If we've been forwarded from the output side, then 
 701                  * skip the firewall a second time 
 705 #endif  /* IPFIREWALL_FORWARD */ 
 708                 lck_mtx_unlock(ip_mutex
); 
 710                 i 
= ip_fw_chk_ptr(&args
); 
 713                 if ( (i 
& IP_FW_PORT_DENY_FLAG
) || m 
== NULL
) { /* drop */ 
 718                 ip 
= mtod(m
, struct ip 
*); /* just in case m changed */ 
 719                 if (i 
== 0 && args
.next_hop 
== NULL
) {  /* common case */ 
 720                         lck_mtx_lock(ip_mutex
); 
 724                 if (DUMMYNET_LOADED 
&& (i 
& IP_FW_PORT_DYNT_FLAG
) != 0) { 
 725                         /* Send packet to the appropriate pipe */ 
 726                         ip_dn_io_ptr(m
, i
&0xffff, DN_TO_IP_IN
, &args
); 
 729 #endif /* DUMMYNET */ 
 731                 if (i 
!= 0 && (i 
& IP_FW_PORT_DYNT_FLAG
) == 0) { 
 732                         /* Divert or tee packet */ 
 733                         lck_mtx_lock(ip_mutex
); 
 738 #if IPFIREWALL_FORWARD 
 739                 if (i 
== 0 && args
.next_hop 
!= NULL
) { 
 740                         lck_mtx_lock(ip_mutex
); 
 745                  * if we get here, the packet must be dropped 
 753          * Process options and, if not destined for us, 
 754          * ship it on.  ip_dooptions returns 1 when an 
 755          * error was detected (causing an icmp message 
 756          * to be sent and the original packet to be freed). 
 758         ip_nhops 
= 0;           /* for source routed packets */ 
 759         if (hlen 
> sizeof (struct ip
) && ip_dooptions(m
, 0, args
.next_hop
, &ipforward_rt
)) { 
 760                 lck_mtx_unlock(ip_mutex
); 
 764         /* greedy RSVP, snatches any PATH packet of the RSVP protocol and no 
 765          * matter if it is destined to another node, or whether it is  
 766          * a multicast one, RSVP wants it! and prevents it from being forwarded 
 767          * anywhere else. Also checks if the rsvp daemon is running before 
 768          * grabbing the packet. 
 770         if (rsvp_on 
&& ip
->ip_p
==IPPROTO_RSVP
)  
 774          * Check our list of addresses, to see if the packet is for us. 
 775          * If we don't have any addresses, assume any unicast packet 
 776          * we receive might be for us (and let the upper layers deal 
 779         if (TAILQ_EMPTY(&in_ifaddrhead
) && 
 780             (m
->m_flags 
& (M_MCAST
|M_BCAST
)) == 0) 
 784          * Cache the destination address of the packet; this may be 
 785          * changed by use of 'ipfw fwd'. 
 787         pkt_dst 
= args
.next_hop 
== NULL 
? 
 788             ip
->ip_dst 
: args
.next_hop
->sin_addr
; 
 791          * Enable a consistency check between the destination address 
 792          * and the arrival interface for a unicast packet (the RFC 1122 
 793          * strong ES model) if IP forwarding is disabled and the packet 
 794          * is not locally generated and the packet is not subject to 
 797          * XXX - Checking also should be disabled if the destination 
 798          * address is ipnat'ed to a different interface. 
 800          * XXX - Checking is incompatible with IP aliases added 
 801          * to the loopback interface instead of the interface where 
 802          * the packets are received. 
 804         checkif 
= ip_checkinterface 
&& (ipforwarding 
== 0) &&  
 805             ((m
->m_pkthdr
.rcvif
->if_flags 
& IFF_LOOPBACK
) == 0) && 
 806             (args
.next_hop 
== NULL
); 
 808         lck_mtx_lock(rt_mtx
); 
 809         TAILQ_FOREACH(ia
, &in_ifaddrhead
, ia_link
) { 
 810 #define satosin(sa)     ((struct sockaddr_in *)(sa)) 
 812                 if (IA_SIN(ia
)->sin_addr
.s_addr 
== INADDR_ANY
) { 
 813                         lck_mtx_unlock(rt_mtx
); 
 818                  * If the address matches, verify that the packet 
 819                  * arrived via the correct interface if checking is 
 822                 if (IA_SIN(ia
)->sin_addr
.s_addr 
== pkt_dst
.s_addr 
&&  
 823                     (!checkif 
|| ia
->ia_ifp 
== m
->m_pkthdr
.rcvif
)) { 
 824                         lck_mtx_unlock(rt_mtx
); 
 828                  * Only accept broadcast packets that arrive via the 
 829                  * matching interface.  Reception of forwarded directed 
 830                  * broadcasts would be handled via ip_forward() and 
 831                  * ether_output() with the loopback into the stack for 
 832                  * SIMPLEX interfaces handled by ether_output(). 
 834                 if ((!checkif 
|| ia
->ia_ifp 
== m
->m_pkthdr
.rcvif
) && 
 835                     ia
->ia_ifp 
&& ia
->ia_ifp
->if_flags 
& IFF_BROADCAST
) { 
 836                         if (satosin(&ia
->ia_broadaddr
)->sin_addr
.s_addr 
== 
 838                                 lck_mtx_unlock(rt_mtx
); 
 841                         if (ia
->ia_netbroadcast
.s_addr 
== pkt_dst
.s_addr
) { 
 842                                 lck_mtx_unlock(rt_mtx
); 
 847         lck_mtx_unlock(rt_mtx
); 
 848         if (IN_MULTICAST(ntohl(ip
->ip_dst
.s_addr
))) { 
 849                 struct in_multi 
*inm
; 
 852                          * If we are acting as a multicast router, all 
 853                          * incoming multicast packets are passed to the 
 854                          * kernel-level multicast forwarding function. 
 855                          * The packet is returned (relatively) intact; if 
 856                          * ip_mforward() returns a non-zero value, the packet 
 857                          * must be discarded, else it may be accepted below. 
 860                             ip_mforward(ip
, m
->m_pkthdr
.rcvif
, m
, 0) != 0) { 
 861                                 ipstat
.ips_cantforward
++; 
 863                                 lck_mtx_unlock(ip_mutex
); 
 868                          * The process-level routing daemon needs to receive 
 869                          * all multicast IGMP packets, whether or not this 
 870                          * host belongs to their destination groups. 
 872                         if (ip
->ip_p 
== IPPROTO_IGMP
) 
 874                         ipstat
.ips_forward
++; 
 877                  * See if we belong to the destination multicast group on the 
 880                 IN_LOOKUP_MULTI(ip
->ip_dst
, m
->m_pkthdr
.rcvif
, inm
); 
 882                         ipstat
.ips_notmember
++; 
 884                         lck_mtx_unlock(ip_mutex
); 
 889         if (ip
->ip_dst
.s_addr 
== (u_long
)INADDR_BROADCAST
) 
 891         if (ip
->ip_dst
.s_addr 
== INADDR_ANY
) 
 894         /* Allow DHCP/BootP responses through */ 
 895         if (m
->m_pkthdr
.rcvif 
!= NULL
 
 896             && (m
->m_pkthdr
.rcvif
->if_eflags 
& IFEF_AUTOCONFIGURING
) 
 897             && hlen 
== sizeof(struct ip
) 
 898             && ip
->ip_p 
== IPPROTO_UDP
) { 
 900                 if (m
->m_len 
< sizeof(struct udpiphdr
) 
 901                     && (m 
= m_pullup(m
, sizeof(struct udpiphdr
))) == 0) { 
 902                         udpstat
.udps_hdrops
++; 
 903                         lck_mtx_unlock(ip_mutex
); 
 906                 ui 
= mtod(m
, struct udpiphdr 
*); 
 907                 if (ntohs(ui
->ui_dport
) == IPPORT_BOOTPC
) { 
 910                 ip 
= mtod(m
, struct ip 
*); /* in case it changed */ 
 913 #if defined(NFAITH) && 0 < NFAITH 
 915          * FAITH(Firewall Aided Internet Translator) 
 917         if (m
->m_pkthdr
.rcvif 
&& m
->m_pkthdr
.rcvif
->if_type 
== IFT_FAITH
) { 
 919                         if (ip
->ip_p 
== IPPROTO_TCP 
|| ip
->ip_p 
== IPPROTO_ICMP
)  
 923                 lck_mtx_unlock(ip_mutex
); 
 927         lck_mtx_unlock(ip_mutex
); 
 929          * Not for us; forward if possible and desirable. 
 931         if (ipforwarding 
== 0) { 
 932                 ipstat
.ips_cantforward
++; 
 935                 ip_forward(m
, 0, args
.next_hop
, &ipforward_rt
); 
 941         /* Darwin does not have an if_data in ifaddr */ 
 942         /* Count the packet in the ip address stats */ 
 944                 ia
->ia_ifa
.if_ipackets
++; 
 945                 ia
->ia_ifa
.if_ibytes 
+= m
->m_pkthdr
.len
; 
 950          * If offset or IP_MF are set, must reassemble. 
 951          * Otherwise, nothing need be done. 
 952          * (We could look in the reassembly queue to see 
 953          * if the packet was previously fragmented, 
 954          * but it's not worth the time; just let them time out.) 
 956         if (ip
->ip_off 
& (IP_MF 
| IP_OFFMASK 
| IP_RF
)) { 
 958                 /* If maxnipq is 0, never accept fragments. */ 
 960                         ipstat
.ips_fragments
++; 
 961                         ipstat
.ips_fragdropped
++; 
 966                  * If we will exceed the number of fragments in queues, timeout the 
 967                  * oldest fragemented packet to make space. 
 969                 if (currentfrags 
>= maxfrags
) { 
 970                         fp 
= TAILQ_LAST(&ipq_list
, ipq_list
); 
 971                         ipstat
.ips_fragtimeout 
+= fp
->ipq_nfrags
; 
 973                         if (ip
->ip_id 
== fp
->ipq_id 
&& 
 974                                 ip
->ip_src
.s_addr 
== fp
->ipq_src
.s_addr 
&& 
 975                                 ip
->ip_dst
.s_addr 
== fp
->ipq_dst
.s_addr 
&& 
 976                                 ip
->ip_p 
== fp
->ipq_p
) { 
 978                                  * If we match the fragment queue we were going to 
 979                                  * discard, drop this packet too. 
 981                                 ipstat
.ips_fragdropped
++; 
 989                 sum 
= IPREASS_HASH(ip
->ip_src
.s_addr
, ip
->ip_id
); 
 991                  * Look for queue of fragments 
 994                 for (fp 
= ipq
[sum
].next
; fp 
!= &ipq
[sum
]; fp 
= fp
->next
) 
 995                         if (ip
->ip_id 
== fp
->ipq_id 
&& 
 996                             ip
->ip_src
.s_addr 
== fp
->ipq_src
.s_addr 
&& 
 997                             ip
->ip_dst
.s_addr 
== fp
->ipq_dst
.s_addr 
&& 
 998                             ip
->ip_p 
== fp
->ipq_p
) 
1002                  * Enforce upper bound on number of fragmented packets 
1003                  * for which we attempt reassembly; 
1004                  * If maxnipq is -1, accept all fragments without limitation. 
1006                 if ((nipq 
> maxnipq
) && (maxnipq 
> 0)) { 
1008                      * drop the oldest fragment before proceeding further 
1010                     fp 
= TAILQ_LAST(&ipq_list
, ipq_list
); 
1011                     ipstat
.ips_fragtimeout 
+= fp
->ipq_nfrags
; 
1019                  * Adjust ip_len to not reflect header, 
1020                  * convert offset of this to bytes. 
1023                 if (ip
->ip_off 
& IP_MF
) { 
1025                          * Make sure that fragments have a data length 
1026                                  * that's a non-zero multiple of 8 bytes. 
1028                         if (ip
->ip_len 
== 0 || (ip
->ip_len 
& 0x7) != 0) { 
1029                                 ipstat
.ips_toosmall
++; /* XXX */ 
1032                         m
->m_flags 
|= M_FRAG
; 
1034                         /* Clear the flag in case packet comes from loopback */ 
1035                         m
->m_flags 
&= ~M_FRAG
; 
1040                  * Attempt reassembly; if it succeeds, proceed. 
1041                  * ip_reass() will return a different mbuf, and update 
1042                  * the divert info in div_info and args.divert_rule. 
1044                         ipstat
.ips_fragments
++; 
1045                         m
->m_pkthdr
.header 
= ip
; 
1048                             fp
, &ipq
[sum
], &div_info
, &args
.divert_rule
); 
1050                         m 
= ip_reass(m
, fp
, &ipq
[sum
]); 
1053                                 lck_mtx_unlock(ip_mutex
); 
1056                         ipstat
.ips_reassembled
++; 
1057                         ip 
= mtod(m
, struct ip 
*); 
1058                         /* Get the header length of the reassembled packet */ 
1059                         hlen 
= IP_VHL_HL(ip
->ip_vhl
) << 2; 
1061                         /* Restore original checksum before diverting packet */ 
1062                         if (div_info 
!= 0) { 
1067                                 ip
->ip_sum 
= in_cksum(m
, hlen
); 
1078          * Divert or tee packet to the divert protocol if required. 
1080          * If div_info is zero then cookie should be too, so we shouldn't 
1081          * need to clear them here.  Assume divert_packet() does so also. 
1083         if (div_info 
!= 0) { 
1084                 struct mbuf 
*clone 
= NULL
; 
1086                 /* Clone packet if we're doing a 'tee' */ 
1087                 if ((div_info 
& IP_FW_PORT_TEE_FLAG
) != 0) 
1088                         clone 
= m_dup(m
, M_DONTWAIT
); 
1090                 /* Restore packet header fields to original values */ 
1095                 /* Deliver packet to divert input routine */ 
1096                 ipstat
.ips_delivered
++; 
1097                 lck_mtx_unlock(ip_mutex
); 
1098                 divert_packet(m
, 1, div_info 
& 0xffff, args
.divert_rule
); 
1100                 /* If 'tee', continue with original packet */ 
1101                 if (clone 
== NULL
) { 
1104                 lck_mtx_lock(ip_mutex
); 
1106                 ip 
= mtod(m
, struct ip 
*); 
1112          * enforce IPsec policy checking if we are seeing last header. 
1113          * note that we do not visit this with protocols with pcb layer 
1114          * code - like udp/tcp/raw ip. 
1116         if (ipsec_bypass 
== 0 && (ip_protox
[ip
->ip_p
]->pr_flags 
& PR_LASTHDR
) != 0) { 
1117                 lck_mtx_lock(sadb_mutex
); 
1118                 if (ipsec4_in_reject(m
, NULL
)) { 
1119                         ipsecstat
.in_polvio
++; 
1120                         lck_mtx_unlock(sadb_mutex
); 
1123                 lck_mtx_unlock(sadb_mutex
); 
1128          * Switch out to protocol's input routine. 
1130         ipstat
.ips_delivered
++; 
1132                 if (args
.next_hop 
&& ip
->ip_p 
== IPPROTO_TCP
) { 
1133                         /* TCP needs IPFORWARD info if available */ 
1134                         struct m_tag 
*fwd_tag
; 
1135                         struct ip_fwd_tag       
*ipfwd_tag
; 
1137                         fwd_tag 
= m_tag_alloc(KERNEL_MODULE_TAG_ID
, KERNEL_TAG_TYPE_IPFORWARD
, 
1138                                         sizeof(struct sockaddr_in
), M_NOWAIT
); 
1139                         if (fwd_tag 
== NULL
) { 
1143                         ipfwd_tag 
= (struct ip_fwd_tag 
*)(fwd_tag
+1); 
1144                         ipfwd_tag
->next_hop 
= args
.next_hop
; 
1146                         m_tag_prepend(m
, fwd_tag
); 
1148                         KERNEL_DEBUG(DBG_LAYER_END
, ip
->ip_dst
.s_addr
,  
1149                              ip
->ip_src
.s_addr
, ip
->ip_p
, ip
->ip_off
, ip
->ip_len
); 
1151                         lck_mtx_unlock(ip_mutex
); 
1153                         /* TCP deals with its own locking */ 
1154                         ip_proto_dispatch_in(m
, hlen
, ip
->ip_p
, 0); 
1156                         KERNEL_DEBUG(DBG_LAYER_END
, ip
->ip_dst
.s_addr
,  
1157                              ip
->ip_src
.s_addr
, ip
->ip_p
, ip
->ip_off
, ip
->ip_len
); 
1159                         lck_mtx_unlock(ip_mutex
); 
1160                         ip_proto_dispatch_in(m
, hlen
, ip
->ip_p
, 0); 
1166         KERNEL_DEBUG(DBG_LAYER_END
, 0,0,0,0,0); 
1167         lck_mtx_unlock(ip_mutex
); 
1172  * Take incoming datagram fragment and try to reassemble it into 
1173  * whole datagram.  If a chain for reassembly of this datagram already 
1174  * exists, then it is given as fp; otherwise have to make a chain. 
1176  * When IPDIVERT enabled, keep additional state with each packet that 
1177  * tells us if we need to divert or tee the packet we're building. 
1180 static struct mbuf 
* 
1182 ip_reass(m
, fp
, where
, divinfo
, divcookie
) 
1184 ip_reass(m
, fp
, where
) 
1186         register struct mbuf 
*m
; 
1187         register struct ipq 
*fp
; 
1195         u_int16_t 
*divcookie
; 
1198         struct ip 
*ip 
= mtod(m
, struct ip 
*); 
1199         register struct mbuf 
*p 
= 0, *q
, *nq
; 
1201         int hlen 
= IP_VHL_HL(ip
->ip_vhl
) << 2; 
1205          * Presence of header sizes in mbufs 
1206          * would confuse code below. 
1211         if (m
->m_pkthdr
.csum_flags 
& CSUM_TCP_SUM16
)  
1212                 m
->m_pkthdr
.csum_flags 
= 0; 
1214          * If first fragment to arrive, create a reassembly queue. 
1217                 if ((t 
= m_get(M_DONTWAIT
, MT_FTABLE
)) == NULL
) 
1219                 fp 
= mtod(t
, struct ipq 
*); 
1220                 insque((void*)fp
, (void*)where
); 
1223                 fp
->ipq_ttl 
= IPFRAGTTL
; 
1224                 fp
->ipq_p 
= ip
->ip_p
; 
1225                 fp
->ipq_id 
= ip
->ip_id
; 
1226                 fp
->ipq_src 
= ip
->ip_src
; 
1227                 fp
->ipq_dst 
= ip
->ip_dst
; 
1229                 m
->m_nextpkt 
= NULL
; 
1232                 fp
->ipq_div_info 
= 0; 
1236                 fp
->ipq_div_cookie 
= 0; 
1238                 TAILQ_INSERT_HEAD(&ipq_list
, fp
, ipq_list
); 
1244 #define GETIP(m)        ((struct ip*)((m)->m_pkthdr.header)) 
1247          * Find a segment which begins after this one does. 
1249         for (p 
= NULL
, q 
= fp
->ipq_frags
; q
; p 
= q
, q 
= q
->m_nextpkt
) 
1250                 if (GETIP(q
)->ip_off 
> ip
->ip_off
) 
1254          * If there is a preceding segment, it may provide some of 
1255          * our data already.  If so, drop the data from the incoming 
1256          * segment.  If it provides all of our data, drop us, otherwise 
1257          * stick new segment in the proper place. 
1259          * If some of the data is dropped from the the preceding 
1260          * segment, then it's checksum is invalidated. 
1263                 i 
= GETIP(p
)->ip_off 
+ GETIP(p
)->ip_len 
- ip
->ip_off
; 
1265                         if (i 
>= ip
->ip_len
) 
1268                         m
->m_pkthdr
.csum_flags 
= 0; 
1272                 m
->m_nextpkt 
= p
->m_nextpkt
; 
1275                 m
->m_nextpkt 
= fp
->ipq_frags
; 
1280          * While we overlap succeeding segments trim them or, 
1281          * if they are completely covered, dequeue them. 
1283         for (; q 
!= NULL 
&& ip
->ip_off 
+ ip
->ip_len 
> GETIP(q
)->ip_off
; 
1285                 i 
= (ip
->ip_off 
+ ip
->ip_len
) - 
1287                 if (i 
< GETIP(q
)->ip_len
) { 
1288                         GETIP(q
)->ip_len 
-= i
; 
1289                         GETIP(q
)->ip_off 
+= i
; 
1291                         q
->m_pkthdr
.csum_flags 
= 0; 
1296                 ipstat
.ips_fragdropped
++; 
1306          * Transfer firewall instructions to the fragment structure. 
1307          * Only trust info in the fragment at offset 0. 
1309         if (ip
->ip_off 
== 0) { 
1311         fp
->ipq_div_info 
= *divinfo
; 
1313         fp
->ipq_divert 
= *divinfo
; 
1315         fp
->ipq_div_cookie 
= *divcookie
; 
1322          * Check for complete reassembly and perform frag per packet 
1325          * Frag limiting is performed here so that the nth frag has 
1326          * a chance to complete the packet before we drop the packet. 
1327          * As a result, n+1 frags are actually allowed per packet, but 
1328          * only n will ever be stored. (n = maxfragsperpacket.) 
1332         for (p 
= NULL
, q 
= fp
->ipq_frags
; q
; p 
= q
, q 
= q
->m_nextpkt
) { 
1333                 if (GETIP(q
)->ip_off 
!= next
) { 
1334                         if (fp
->ipq_nfrags 
> maxfragsperpacket
) { 
1335                                 ipstat
.ips_fragdropped 
+= fp
->ipq_nfrags
; 
1340                 next 
+= GETIP(q
)->ip_len
; 
1342         /* Make sure the last packet didn't have the IP_MF flag */ 
1343         if (p
->m_flags 
& M_FRAG
) { 
1344                 if (fp
->ipq_nfrags 
> maxfragsperpacket
) { 
1345                         ipstat
.ips_fragdropped 
+= fp
->ipq_nfrags
; 
1352          * Reassembly is complete.  Make sure the packet is a sane size. 
1356         if (next 
+ (IP_VHL_HL(ip
->ip_vhl
) << 2) > IP_MAXPACKET
) { 
1357                 ipstat
.ips_toolong
++; 
1358                 ipstat
.ips_fragdropped 
+= fp
->ipq_nfrags
; 
1364          * Concatenate fragments. 
1372         for (q 
= nq
; q 
!= NULL
; q 
= nq
) { 
1374                 q
->m_nextpkt 
= NULL
; 
1375                 if (q
->m_pkthdr
.csum_flags 
& CSUM_TCP_SUM16
)  
1376                         m
->m_pkthdr
.csum_flags 
= 0; 
1378                         m
->m_pkthdr
.csum_flags 
&= q
->m_pkthdr
.csum_flags
; 
1379                         m
->m_pkthdr
.csum_data 
+= q
->m_pkthdr
.csum_data
; 
1386          * Extract firewall instructions from the fragment structure. 
1389         *divinfo 
= fp
->ipq_div_info
; 
1391         *divinfo 
= fp
->ipq_divert
; 
1393         *divcookie 
= fp
->ipq_div_cookie
; 
1397          * Create header for new ip packet by 
1398          * modifying header of first packet; 
1399          * dequeue and discard fragment reassembly header. 
1400          * Make header visible. 
1403         ip
->ip_src 
= fp
->ipq_src
; 
1404         ip
->ip_dst 
= fp
->ipq_dst
; 
1406         TAILQ_REMOVE(&ipq_list
, fp
, ipq_list
); 
1407         currentfrags 
-= fp
->ipq_nfrags
; 
1409         (void) m_free(dtom(fp
)); 
1410         m
->m_len 
+= (IP_VHL_HL(ip
->ip_vhl
) << 2); 
1411         m
->m_data 
-= (IP_VHL_HL(ip
->ip_vhl
) << 2); 
1412         /* some debugging cruft by sklower, below, will go away soon */ 
1413         if (m
->m_flags 
& M_PKTHDR
) { /* XXX this should be done elsewhere */ 
1414                 register int plen 
= 0; 
1415                 for (t 
= m
; t
; t 
= t
->m_next
) 
1417                 m
->m_pkthdr
.len 
= plen
; 
1426         ipstat
.ips_fragdropped
++; 
1436  * Free a fragment reassembly header and all 
1437  * associated datagrams. 
1443         currentfrags 
-= fp
->ipq_nfrags
; 
1444         m_freem_list(fp
->ipq_frags
); 
1446         TAILQ_REMOVE(&ipq_list
, fp
, ipq_list
); 
1447         (void) m_free(dtom(fp
)); 
1452  * IP timer processing; 
1453  * if a timer expires on a reassembly 
1454  * queue, discard it. 
1459         register struct ipq 
*fp
; 
1461         lck_mtx_lock(ip_mutex
); 
1462         for (i 
= 0; i 
< IPREASS_NHASH
; i
++) { 
1466                 while (fp 
!= &ipq
[i
]) { 
1469                         if (fp
->prev
->ipq_ttl 
== 0) { 
1470                                 ipstat
.ips_fragtimeout 
+= fp
->prev
->ipq_nfrags
; 
1476          * If we are over the maximum number of fragments 
1477          * (due to the limit being lowered), drain off 
1478          * enough to get down to the new limit. 
1480         if (maxnipq 
>= 0 && nipq 
> maxnipq
) { 
1481         for (i 
= 0; i 
< IPREASS_NHASH
; i
++) { 
1482                         while (nipq 
> maxnipq 
&& 
1483                                 (ipq
[i
].next 
!= &ipq
[i
])) { 
1484                                 ipstat
.ips_fragdropped 
+= 
1485                                     ipq
[i
].next
->ipq_nfrags
; 
1486                                 ip_freef(ipq
[i
].next
); 
1491         lck_mtx_unlock(ip_mutex
); 
1495  * Drain off all datagram fragments. 
1502         lck_mtx_lock(ip_mutex
); 
1503         for (i 
= 0; i 
< IPREASS_NHASH
; i
++) { 
1504                 while (ipq
[i
].next 
!= &ipq
[i
]) { 
1505                         ipstat
.ips_fragdropped 
+= ipq
[i
].next
->ipq_nfrags
; 
1506                         ip_freef(ipq
[i
].next
); 
1509         lck_mtx_unlock(ip_mutex
); 
1514  * Do option processing on a datagram, 
1515  * possibly discarding it if bad options are encountered, 
1516  * or forwarding it if source-routed. 
1517  * The pass argument is used when operating in the IPSTEALTH 
1518  * mode to tell what options to process: 
1519  * [LS]SRR (pass 0) or the others (pass 1). 
1520  * The reason for as many as two passes is that when doing IPSTEALTH, 
1521  * non-routing options should be processed only if the packet is for us. 
1522  * Returns 1 if packet has been forwarded/freed, 
1523  * 0 if the packet should be processed further. 
1526 ip_dooptions(struct mbuf 
*m
, int pass
, struct sockaddr_in 
*next_hop
, struct route 
*ipforward_rt
) 
1528         register struct ip 
*ip 
= mtod(m
, struct ip 
*); 
1529         register u_char 
*cp
; 
1530         register struct ip_timestamp 
*ipt
; 
1531         register struct in_ifaddr 
*ia
; 
1532         int opt
, optlen
, cnt
, off
, code
, type 
= ICMP_PARAMPROB
, forward 
= 0; 
1533         struct in_addr 
*sin
, dst
; 
1537         cp 
= (u_char 
*)(ip 
+ 1); 
1538         cnt 
= (IP_VHL_HL(ip
->ip_vhl
) << 2) - sizeof (struct ip
); 
1539         for (; cnt 
> 0; cnt 
-= optlen
, cp 
+= optlen
) { 
1540                 opt 
= cp
[IPOPT_OPTVAL
]; 
1541                 if (opt 
== IPOPT_EOL
) 
1543                 if (opt 
== IPOPT_NOP
) 
1546                         if (cnt 
< IPOPT_OLEN 
+ sizeof(*cp
)) { 
1547                                 code 
= &cp
[IPOPT_OLEN
] - (u_char 
*)ip
; 
1550                         optlen 
= cp
[IPOPT_OLEN
]; 
1551                         if (optlen 
< IPOPT_OLEN 
+ sizeof(*cp
) || optlen 
> cnt
) { 
1552                                 code 
= &cp
[IPOPT_OLEN
] - (u_char 
*)ip
; 
1562                  * Source routing with record. 
1563                  * Find interface with current destination address. 
1564                  * If none on this machine then drop if strictly routed, 
1565                  * or do nothing if loosely routed. 
1566                  * Record interface address and bring up next address 
1567                  * component.  If strictly routed make sure next 
1568                  * address is on directly accessible net. 
1572                         if (optlen 
< IPOPT_OFFSET 
+ sizeof(*cp
)) { 
1573                                 code 
= &cp
[IPOPT_OLEN
] - (u_char 
*)ip
; 
1576                         if ((off 
= cp
[IPOPT_OFFSET
]) < IPOPT_MINOFF
) { 
1577                                 code 
= &cp
[IPOPT_OFFSET
] - (u_char 
*)ip
; 
1580                         ipaddr
.sin_addr 
= ip
->ip_dst
; 
1581                         ia 
= (struct in_ifaddr 
*) 
1582                                 ifa_ifwithaddr((struct sockaddr 
*)&ipaddr
); 
1584                                 if (opt 
== IPOPT_SSRR
) { 
1585                                         type 
= ICMP_UNREACH
; 
1586                                         code 
= ICMP_UNREACH_SRCFAIL
; 
1589                                 if (!ip_dosourceroute
) 
1590                                         goto nosourcerouting
; 
1592                                  * Loose routing, and not at next destination 
1593                                  * yet; nothing to do except forward. 
1598                                 ifafree(&ia
->ia_ifa
); 
1601                         off
--;                  /* 0 origin */ 
1602                         if (off 
> optlen 
- (int)sizeof(struct in_addr
)) { 
1604                                  * End of source route.  Should be for us. 
1606                                 if (!ip_acceptsourceroute
) 
1607                                         goto nosourcerouting
; 
1608                                 save_rte(cp
, ip
->ip_src
); 
1612                         if (!ip_dosourceroute
) { 
1614                                         char buf
[MAX_IPv4_STR_LEN
]; 
1615                                         char buf2
[MAX_IPv4_STR_LEN
]; 
1617                                          * Acting as a router, so generate ICMP 
1621                                             "attempted source route from %s to %s\n", 
1622                                             inet_ntop(AF_INET
, &ip
->ip_src
, buf
, sizeof(buf
)), 
1623                                             inet_ntop(AF_INET
, &ip
->ip_dst
, buf2
, sizeof(buf2
))); 
1624                                         type 
= ICMP_UNREACH
; 
1625                                         code 
= ICMP_UNREACH_SRCFAIL
; 
1629                                          * Not acting as a router, so silently drop. 
1631                                         ipstat
.ips_cantforward
++; 
1638                          * locate outgoing interface 
1640                         (void)memcpy(&ipaddr
.sin_addr
, cp 
+ off
, 
1641                             sizeof(ipaddr
.sin_addr
)); 
1643                         if (opt 
== IPOPT_SSRR
) { 
1644 #define INA     struct in_ifaddr * 
1645 #define SA      struct sockaddr * 
1646                             if ((ia 
= (INA
)ifa_ifwithdstaddr((SA
)&ipaddr
)) == 0) { 
1647                                         ia 
= (INA
)ifa_ifwithnet((SA
)&ipaddr
); 
1650                                 ia 
= ip_rtaddr(ipaddr
.sin_addr
, ipforward_rt
); 
1653                                 type 
= ICMP_UNREACH
; 
1654                                 code 
= ICMP_UNREACH_SRCFAIL
; 
1657                         ip
->ip_dst 
= ipaddr
.sin_addr
; 
1658                         (void)memcpy(cp 
+ off
, &(IA_SIN(ia
)->sin_addr
), 
1659                             sizeof(struct in_addr
)); 
1660                         ifafree(&ia
->ia_ifa
); 
1662                         cp
[IPOPT_OFFSET
] += sizeof(struct in_addr
); 
1664                          * Let ip_intr's mcast routing check handle mcast pkts 
1666                         forward 
= !IN_MULTICAST(ntohl(ip
->ip_dst
.s_addr
)); 
1670                         if (optlen 
< IPOPT_OFFSET 
+ sizeof(*cp
)) { 
1671                                 code 
= &cp
[IPOPT_OFFSET
] - (u_char 
*)ip
; 
1674                         if ((off 
= cp
[IPOPT_OFFSET
]) < IPOPT_MINOFF
) { 
1675                                 code 
= &cp
[IPOPT_OFFSET
] - (u_char 
*)ip
; 
1679                          * If no space remains, ignore. 
1681                         off
--;                  /* 0 origin */ 
1682                         if (off 
> optlen 
- (int)sizeof(struct in_addr
)) 
1684                         (void)memcpy(&ipaddr
.sin_addr
, &ip
->ip_dst
, 
1685                             sizeof(ipaddr
.sin_addr
)); 
1687                          * locate outgoing interface; if we're the destination, 
1688                          * use the incoming interface (should be same). 
1690                         if ((ia 
= (INA
)ifa_ifwithaddr((SA
)&ipaddr
)) == 0) { 
1691                                 if ((ia 
= ip_rtaddr(ipaddr
.sin_addr
, ipforward_rt
)) == 0) { 
1692                                         type 
= ICMP_UNREACH
; 
1693                                         code 
= ICMP_UNREACH_HOST
; 
1697                         (void)memcpy(cp 
+ off
, &(IA_SIN(ia
)->sin_addr
), 
1698                             sizeof(struct in_addr
)); 
1699                         ifafree(&ia
->ia_ifa
); 
1701                         cp
[IPOPT_OFFSET
] += sizeof(struct in_addr
); 
1705                         code 
= cp 
- (u_char 
*)ip
; 
1706                         ipt 
= (struct ip_timestamp 
*)cp
; 
1707                         if (ipt
->ipt_len 
< 4 || ipt
->ipt_len 
> 40) { 
1708                                 code 
= (u_char 
*)&ipt
->ipt_len 
- (u_char 
*)ip
; 
1711                         if (ipt
->ipt_ptr 
< 5) { 
1712                                 code 
= (u_char 
*)&ipt
->ipt_ptr 
- (u_char 
*)ip
; 
1716                             ipt
->ipt_len 
- (int)sizeof(int32_t)) { 
1717                                 if (++ipt
->ipt_oflw 
== 0) { 
1718                                         code 
= (u_char 
*)&ipt
->ipt_ptr 
- 
1724                         sin 
= (struct in_addr 
*)(cp 
+ ipt
->ipt_ptr 
- 1); 
1725                         switch (ipt
->ipt_flg
) { 
1727                         case IPOPT_TS_TSONLY
: 
1730                         case IPOPT_TS_TSANDADDR
: 
1731                                 if (ipt
->ipt_ptr 
- 1 + sizeof(n_time
) + 
1732                                     sizeof(struct in_addr
) > ipt
->ipt_len
) { 
1733                                         code 
= (u_char 
*)&ipt
->ipt_ptr 
- 
1737                                 ipaddr
.sin_addr 
= dst
; 
1738                                 ia 
= (INA
)ifaof_ifpforaddr((SA
)&ipaddr
, 
1742                                 (void)memcpy(sin
, &IA_SIN(ia
)->sin_addr
, 
1743                                     sizeof(struct in_addr
)); 
1744                                 ipt
->ipt_ptr 
+= sizeof(struct in_addr
); 
1745                                 ifafree(&ia
->ia_ifa
); 
1749                         case IPOPT_TS_PRESPEC
: 
1750                                 if (ipt
->ipt_ptr 
- 1 + sizeof(n_time
) + 
1751                                     sizeof(struct in_addr
) > ipt
->ipt_len
) { 
1752                                         code 
= (u_char 
*)&ipt
->ipt_ptr 
- 
1756                                 (void)memcpy(&ipaddr
.sin_addr
, sin
, 
1757                                     sizeof(struct in_addr
)); 
1758                                 if ((ia 
= (struct in_ifaddr
*)ifa_ifwithaddr((SA
)&ipaddr
)) == 0) 
1760                                 ifafree(&ia
->ia_ifa
); 
1762                                 ipt
->ipt_ptr 
+= sizeof(struct in_addr
); 
1766                                 /* XXX can't take &ipt->ipt_flg */ 
1767                                 code 
= (u_char 
*)&ipt
->ipt_ptr 
- 
1772                         (void)memcpy(cp 
+ ipt
->ipt_ptr 
- 1, &ntime
, 
1774                         ipt
->ipt_ptr 
+= sizeof(n_time
); 
1777         if (forward 
&& ipforwarding
) { 
1778                 ip_forward(m
, 1, next_hop
, ipforward_rt
); 
1783         ip
->ip_len 
-= IP_VHL_HL(ip
->ip_vhl
) << 2;   /* XXX icmp_error adds in hdr length */ 
1784         lck_mtx_unlock(ip_mutex
); 
1785         icmp_error(m
, type
, code
, 0, 0); 
1786         lck_mtx_lock(ip_mutex
); 
1787         ipstat
.ips_badoptions
++; 
1792  * Given address of next destination (final or next hop), 
1793  * return internet address info of interface to be used to get there. 
1800         register struct sockaddr_in 
*sin
; 
1802         sin 
= (struct sockaddr_in 
*)&rt
->ro_dst
; 
1804         lck_mtx_lock(rt_mtx
); 
1805         if (rt
->ro_rt 
== 0 || dst
.s_addr 
!= sin
->sin_addr
.s_addr 
|| 
1806                 rt
->ro_rt
->generation_id 
!= route_generation
) { 
1808                         rtfree_locked(rt
->ro_rt
); 
1811                 sin
->sin_family 
= AF_INET
; 
1812                 sin
->sin_len 
= sizeof(*sin
); 
1813                 sin
->sin_addr 
= dst
; 
1815                 rtalloc_ign_locked(rt
, RTF_PRCLONING
); 
1817         if (rt
->ro_rt 
== 0) { 
1818                 lck_mtx_unlock(rt_mtx
); 
1819                 return ((struct in_ifaddr 
*)0); 
1822         if (rt
->ro_rt
->rt_ifa
) 
1823                 ifaref(rt
->ro_rt
->rt_ifa
); 
1824         lck_mtx_unlock(rt_mtx
); 
1825         return ((struct in_ifaddr 
*) rt
->ro_rt
->rt_ifa
); 
1829  * Save incoming source route for use in replies, 
1830  * to be picked up later by ip_srcroute if the receiver is interested. 
1833 save_rte(option
, dst
) 
1839         olen 
= option
[IPOPT_OLEN
]; 
1842                 printf("save_rte: olen %d\n", olen
); 
1844         if (olen 
> sizeof(ip_srcrt
) - (1 + sizeof(dst
))) 
1846         bcopy(option
, ip_srcrt
.srcopt
, olen
); 
1847         ip_nhops 
= (olen 
- IPOPT_OFFSET 
- 1) / sizeof(struct in_addr
); 
1852  * Retrieve incoming source route for use in replies, 
1853  * in the same form used by setsockopt. 
1854  * The first hop is placed before the options, will be removed later. 
1859         register struct in_addr 
*p
, *q
; 
1860         register struct mbuf 
*m
; 
1863                 return ((struct mbuf 
*)0); 
1864         m 
= m_get(M_DONTWAIT
, MT_HEADER
); 
1866                 return ((struct mbuf 
*)0); 
1868 #define OPTSIZ  (sizeof(ip_srcrt.nop) + sizeof(ip_srcrt.srcopt)) 
1870         /* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */ 
1871         m
->m_len 
= ip_nhops 
* sizeof(struct in_addr
) + sizeof(struct in_addr
) + 
1875                 printf("ip_srcroute: nhops %d mlen %d", ip_nhops
, m
->m_len
); 
1879          * First save first hop for return route 
1881         p 
= &ip_srcrt
.route
[ip_nhops 
- 1]; 
1882         *(mtod(m
, struct in_addr 
*)) = *p
--; 
1885                 printf(" hops %lx", (u_long
)ntohl(mtod(m
, struct in_addr 
*)->s_addr
)); 
1889          * Copy option fields and padding (nop) to mbuf. 
1891         ip_srcrt
.nop 
= IPOPT_NOP
; 
1892         ip_srcrt
.srcopt
[IPOPT_OFFSET
] = IPOPT_MINOFF
; 
1893         (void)memcpy(mtod(m
, caddr_t
) + sizeof(struct in_addr
), 
1894             &ip_srcrt
.nop
, OPTSIZ
); 
1895         q 
= (struct in_addr 
*)(mtod(m
, caddr_t
) + 
1896             sizeof(struct in_addr
) + OPTSIZ
); 
1899          * Record return path as an IP source route, 
1900          * reversing the path (pointers are now aligned). 
1902         while (p 
>= ip_srcrt
.route
) { 
1905                         printf(" %lx", (u_long
)ntohl(q
->s_addr
)); 
1910          * Last hop goes to final destination. 
1915                 printf(" %lx\n", (u_long
)ntohl(q
->s_addr
)); 
1921  * Strip out IP options, at higher 
1922  * level protocol in the kernel. 
1923  * Second argument is buffer to which options 
1924  * will be moved, and return value is their length. 
1925  * XXX should be deleted; last arg currently ignored. 
1928 ip_stripoptions(m
, mopt
) 
1929         register struct mbuf 
*m
; 
1933         struct ip 
*ip 
= mtod(m
, struct ip 
*); 
1934         register caddr_t opts
; 
1937         olen 
= (IP_VHL_HL(ip
->ip_vhl
) << 2) - sizeof (struct ip
); 
1938         opts 
= (caddr_t
)(ip 
+ 1); 
1939         i 
= m
->m_len 
- (sizeof (struct ip
) + olen
); 
1940         bcopy(opts 
+ olen
, opts
, (unsigned)i
); 
1942         if (m
->m_flags 
& M_PKTHDR
) 
1943                 m
->m_pkthdr
.len 
-= olen
; 
1944         ip
->ip_vhl 
= IP_MAKE_VHL(IPVERSION
, sizeof(struct ip
) >> 2); 
1947 u_char inetctlerrmap
[PRC_NCMDS
] = { 
1949         0,              EMSGSIZE
,       EHOSTDOWN
,      EHOSTUNREACH
, 
1950         EHOSTUNREACH
,   EHOSTUNREACH
,   ECONNREFUSED
,   ECONNREFUSED
, 
1951         EMSGSIZE
,       EHOSTUNREACH
,   0,              0, 
1953         ENOPROTOOPT
,    ECONNREFUSED
 
1957  * Forward a packet.  If some error occurs return the sender 
1958  * an icmp packet.  Note we can't always generate a meaningful 
1959  * icmp message because icmp doesn't have a large enough repertoire 
1960  * of codes and types. 
1962  * If not forwarding, just drop the packet.  This could be confusing 
1963  * if ipforwarding was zero but some routing protocol was advancing 
1964  * us as a gateway to somewhere.  However, we must let the routing 
1965  * protocol deal with that. 
1967  * The srcrt parameter indicates whether the packet is being forwarded 
1968  * via a source route. 
1971 ip_forward(struct mbuf 
*m
, int srcrt
, struct sockaddr_in 
*next_hop
, struct route 
*ipforward_rt
) 
1973         register struct ip 
*ip 
= mtod(m
, struct ip 
*); 
1974         register struct sockaddr_in 
*sin
; 
1975         register struct rtentry 
*rt
; 
1976         int error
, type 
= 0, code 
= 0; 
1979         struct in_addr pkt_dst
; 
1980         struct ifnet 
*destifp
; 
1982         struct ifnet dummyifp
; 
1987          * Cache the destination address of the packet; this may be 
1988          * changed by use of 'ipfw fwd'. 
1990         pkt_dst 
= next_hop 
? next_hop
->sin_addr 
: ip
->ip_dst
; 
1994                 printf("forward: src %lx dst %lx ttl %x\n", 
1995                     (u_long
)ip
->ip_src
.s_addr
, (u_long
)pkt_dst
.s_addr
, 
2000         if (m
->m_flags 
& (M_BCAST
|M_MCAST
) || in_canforward(pkt_dst
) == 0) { 
2001                 ipstat
.ips_cantforward
++; 
2008                 if (ip
->ip_ttl 
<= IPTTLDEC
) { 
2009                         icmp_error(m
, ICMP_TIMXCEED
, ICMP_TIMXCEED_INTRANS
, 
2017         sin 
= (struct sockaddr_in 
*)&ipforward_rt
->ro_dst
; 
2018         if ((rt 
= ipforward_rt
->ro_rt
) == 0 || 
2019             pkt_dst
.s_addr 
!= sin
->sin_addr
.s_addr 
|| 
2020             ipforward_rt
->ro_rt
->generation_id 
!= route_generation
) { 
2021                 if (ipforward_rt
->ro_rt
) { 
2022                         rtfree(ipforward_rt
->ro_rt
); 
2023                         ipforward_rt
->ro_rt 
= 0; 
2025                 sin
->sin_family 
= AF_INET
; 
2026                 sin
->sin_len 
= sizeof(*sin
); 
2027                 sin
->sin_addr 
= pkt_dst
; 
2029                 rtalloc_ign(ipforward_rt
, RTF_PRCLONING
); 
2030                 if (ipforward_rt
->ro_rt 
== 0) { 
2031                         icmp_error(m
, ICMP_UNREACH
, ICMP_UNREACH_HOST
, dest
, 0); 
2034                 rt 
= ipforward_rt
->ro_rt
; 
2038          * Save the IP header and at most 8 bytes of the payload, 
2039          * in case we need to generate an ICMP message to the src. 
2041          * We don't use m_copy() because it might return a reference 
2042          * to a shared cluster. Both this function and ip_output() 
2043          * assume exclusive access to the IP header in `m', so any 
2044          * data in a cluster may change before we reach icmp_error(). 
2046         MGET(mcopy
, M_DONTWAIT
, m
->m_type
); 
2047         if (mcopy 
!= NULL
) { 
2048                 M_COPY_PKTHDR(mcopy
, m
); 
2049                 mcopy
->m_len 
= imin((IP_VHL_HL(ip
->ip_vhl
) << 2) + 8, 
2051                 m_copydata(m
, 0, mcopy
->m_len
, mtod(mcopy
, caddr_t
)); 
2057                 ip
->ip_ttl 
-= IPTTLDEC
; 
2063          * If forwarding packet using same interface that it came in on, 
2064          * perhaps should send a redirect to sender to shortcut a hop. 
2065          * Only send redirect if source is sending directly to us, 
2066          * and if packet was not source routed (or has any options). 
2067          * Also, don't send redirect if forwarding using a default route 
2068          * or a route modified by a redirect. 
2070 #define satosin(sa)     ((struct sockaddr_in *)(sa)) 
2071         if (rt
->rt_ifp 
== m
->m_pkthdr
.rcvif 
&& 
2072             (rt
->rt_flags 
& (RTF_DYNAMIC
|RTF_MODIFIED
)) == 0 && 
2073             satosin(rt_key(rt
))->sin_addr
.s_addr 
!= 0 && 
2074             ipsendredirects 
&& !srcrt
) { 
2075 #define RTA(rt) ((struct in_ifaddr *)(rt->rt_ifa)) 
2076                 u_long src 
= ntohl(ip
->ip_src
.s_addr
); 
2079                     (src 
& RTA(rt
)->ia_subnetmask
) == RTA(rt
)->ia_subnet
) { 
2080                     if (rt
->rt_flags 
& RTF_GATEWAY
) 
2081                         dest 
= satosin(rt
->rt_gateway
)->sin_addr
.s_addr
; 
2083                         dest 
= pkt_dst
.s_addr
; 
2084                     /* Router requirements says to only send host redirects */ 
2085                     type 
= ICMP_REDIRECT
; 
2086                     code 
= ICMP_REDIRECT_HOST
; 
2089                         printf("redirect (%d) to %lx\n", code
, (u_long
)dest
); 
2096                 /* Pass IPFORWARD info if available */ 
2098                 struct ip_fwd_tag       
*ipfwd_tag
; 
2100                 tag 
= m_tag_alloc(KERNEL_MODULE_TAG_ID
, KERNEL_TAG_TYPE_IPFORWARD
, 
2101                                 sizeof(struct sockaddr_in
), M_NOWAIT
); 
2108                 ipfwd_tag 
= (struct ip_fwd_tag 
*)(tag
+1); 
2109                 ipfwd_tag
->next_hop 
= next_hop
; 
2111                 m_tag_prepend(m
, tag
); 
2113         error 
= ip_output_list(m
, 0, (struct mbuf 
*)0, ipforward_rt
,  
2117                 ipstat
.ips_cantforward
++; 
2119                 ipstat
.ips_forward
++; 
2121                         ipstat
.ips_redirectsent
++; 
2124                                 ipflow_create(ipforward_rt
, mcopy
); 
2136         case 0:                         /* forwarded, but need redirect */ 
2137                 /* type, code set above */ 
2140         case ENETUNREACH
:               /* shouldn't happen, checked above */ 
2145                 type 
= ICMP_UNREACH
; 
2146                 code 
= ICMP_UNREACH_HOST
; 
2150                 type 
= ICMP_UNREACH
; 
2151                 code 
= ICMP_UNREACH_NEEDFRAG
; 
2153                 if (ipforward_rt
->ro_rt
) 
2154                         destifp 
= ipforward_rt
->ro_rt
->rt_ifp
; 
2157                  * If the packet is routed over IPsec tunnel, tell the 
2158                  * originator the tunnel MTU. 
2159                  *      tunnel MTU = if MTU - sizeof(IP) - ESP/AH hdrsiz 
2162                 if (ipforward_rt
->ro_rt
) { 
2163                         struct secpolicy 
*sp 
= NULL
; 
2169                                 destifp 
= ipforward_rt
->ro_rt
->rt_ifp
; 
2170                                 ipstat
.ips_cantfrag
++; 
2173                         lck_mtx_lock(sadb_mutex
); 
2174                         sp 
= ipsec4_getpolicybyaddr(mcopy
, 
2180                                 destifp 
= ipforward_rt
->ro_rt
->rt_ifp
; 
2182                                 /* count IPsec header size */ 
2183                                 ipsechdr 
= ipsec4_hdrsiz(mcopy
, 
2188                                  * find the correct route for outer IPv4 
2189                                  * header, compute tunnel MTU. 
2192                                  * The "dummyifp" code relies upon the fact 
2193                                  * that icmp_error() touches only ifp->if_mtu. 
2198                                  && sp
->req
->sav 
!= NULL
 
2199                                  && sp
->req
->sav
->sah 
!= NULL
) { 
2200                                         ro 
= &sp
->req
->sav
->sah
->sa_route
; 
2201                                         if (ro
->ro_rt 
&& ro
->ro_rt
->rt_ifp
) { 
2203                                                     ro
->ro_rt
->rt_ifp
->if_mtu
; 
2204                                                 dummyifp
.if_mtu 
-= ipsechdr
; 
2205                                                 destifp 
= &dummyifp
; 
2211                         lck_mtx_unlock(sadb_mutex
); 
2214                 ipstat
.ips_cantfrag
++; 
2218                 type 
= ICMP_SOURCEQUENCH
; 
2222         case EACCES
:                    /* ipfw denied packet */ 
2226         icmp_error(mcopy
, type
, code
, dest
, destifp
); 
2231         register struct inpcb 
*inp
, 
2232         register struct mbuf 
**mp
, 
2233         register struct ip 
*ip
, 
2234         register struct mbuf 
*m
) 
2236         if (inp
->inp_socket
->so_options 
& SO_TIMESTAMP
) { 
2240                 *mp 
= sbcreatecontrol((caddr_t
) &tv
, sizeof(tv
), 
2241                         SCM_TIMESTAMP
, SOL_SOCKET
); 
2243                         mp 
= &(*mp
)->m_next
; 
2245         if (inp
->inp_flags 
& INP_RECVDSTADDR
) { 
2246                 *mp 
= sbcreatecontrol((caddr_t
) &ip
->ip_dst
, 
2247                     sizeof(struct in_addr
), IP_RECVDSTADDR
, IPPROTO_IP
); 
2249                         mp 
= &(*mp
)->m_next
; 
2253          * Moving these out of udp_input() made them even more broken 
2254          * than they already were. 
2256         /* options were tossed already */ 
2257         if (inp
->inp_flags 
& INP_RECVOPTS
) { 
2258                 *mp 
= sbcreatecontrol((caddr_t
) opts_deleted_above
, 
2259                     sizeof(struct in_addr
), IP_RECVOPTS
, IPPROTO_IP
); 
2261                         mp 
= &(*mp
)->m_next
; 
2263         /* ip_srcroute doesn't do what we want here, need to fix */ 
2264         if (inp
->inp_flags 
& INP_RECVRETOPTS
) { 
2265                 *mp 
= sbcreatecontrol((caddr_t
) ip_srcroute(), 
2266                     sizeof(struct in_addr
), IP_RECVRETOPTS
, IPPROTO_IP
); 
2268                         mp 
= &(*mp
)->m_next
; 
2271         if (inp
->inp_flags 
& INP_RECVIF
) { 
2274                         struct sockaddr_dl sdl
; 
2277                 struct sockaddr_dl 
*sdp
; 
2278                 struct sockaddr_dl 
*sdl2 
= &sdlbuf
.sdl
; 
2280                 ifnet_head_lock_shared(); 
2281                 if (((ifp 
= m
->m_pkthdr
.rcvif
))  
2282                 && ( ifp
->if_index 
&& (ifp
->if_index 
<= if_index
))) { 
2283                         struct ifaddr 
*ifa 
= ifnet_addrs
[ifp
->if_index 
- 1]; 
2285                         if (!ifa 
|| !ifa
->ifa_addr
) 
2288                         sdp 
= (struct sockaddr_dl 
*)ifa
->ifa_addr
; 
2290                          * Change our mind and don't try copy. 
2292                         if ((sdp
->sdl_family 
!= AF_LINK
) 
2293                         || (sdp
->sdl_len 
> sizeof(sdlbuf
))) { 
2296                         bcopy(sdp
, sdl2
, sdp
->sdl_len
); 
2300                                 = offsetof(struct sockaddr_dl
, sdl_data
[0]); 
2301                         sdl2
->sdl_family 
= AF_LINK
; 
2302                         sdl2
->sdl_index 
= 0; 
2303                         sdl2
->sdl_nlen 
= sdl2
->sdl_alen 
= sdl2
->sdl_slen 
= 0; 
2306                 *mp 
= sbcreatecontrol((caddr_t
) sdl2
, sdl2
->sdl_len
, 
2307                         IP_RECVIF
, IPPROTO_IP
); 
2309                         mp 
= &(*mp
)->m_next
; 
2311         if (inp
->inp_flags 
& INP_RECVTTL
) { 
2312                 *mp 
= sbcreatecontrol((caddr_t
)&ip
->ip_ttl
, sizeof(ip
->ip_ttl
), IP_RECVTTL
, IPPROTO_IP
); 
2313                 if (*mp
) mp 
= &(*mp
)->m_next
; 
2318 ip_rsvp_init(struct socket 
*so
) 
2320         if (so
->so_type 
!= SOCK_RAW 
|| 
2321             so
->so_proto
->pr_protocol 
!= IPPROTO_RSVP
) 
2324         if (ip_rsvpd 
!= NULL
) 
2329          * This may seem silly, but we need to be sure we don't over-increment 
2330          * the RSVP counter, in case something slips up. 
2345          * This may seem silly, but we need to be sure we don't over-decrement 
2346          * the RSVP counter, in case something slips up.