2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
31 * Copyright (c) 1982, 1986, 1988, 1993
32 * The Regents of the University of California. All rights reserved.
34 * Redistribution and use in source and binary forms, with or without
35 * modification, are permitted provided that the following conditions
37 * 1. Redistributions of source code must retain the above copyright
38 * notice, this list of conditions and the following disclaimer.
39 * 2. Redistributions in binary form must reproduce the above copyright
40 * notice, this list of conditions and the following disclaimer in the
41 * documentation and/or other materials provided with the distribution.
42 * 3. All advertising materials mentioning features or use of this software
43 * must display the following acknowledgement:
44 * This product includes software developed by the University of
45 * California, Berkeley and its contributors.
46 * 4. Neither the name of the University nor the names of its contributors
47 * may be used to endorse or promote products derived from this software
48 * without specific prior written permission.
50 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
51 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
52 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
53 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
54 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
55 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
56 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
57 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
58 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
59 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * @(#)ip_input.c 8.2 (Berkeley) 1/4/94
63 * $FreeBSD: src/sys/netinet/ip_input.c,v 1.130.2.25 2001/08/29 21:41:37 jesper Exp $
68 #include <sys/param.h>
69 #include <sys/systm.h>
71 #include <sys/malloc.h>
72 #include <sys/domain.h>
73 #include <sys/protosw.h>
74 #include <sys/socket.h>
76 #include <sys/kernel.h>
77 #include <sys/syslog.h>
78 #include <sys/sysctl.h>
80 #include <kern/queue.h>
81 #include <kern/locks.h>
84 #include <net/if_var.h>
85 #include <net/if_dl.h>
86 #include <net/route.h>
87 #include <net/kpi_protocol.h>
89 #include <netinet/in.h>
90 #include <netinet/in_systm.h>
91 #include <netinet/in_var.h>
92 #include <netinet/ip.h>
93 #include <netinet/in_pcb.h>
94 #include <netinet/ip_var.h>
95 #include <netinet/ip_icmp.h>
96 #include <sys/socketvar.h>
98 #include <netinet/ip_fw.h>
99 #include <netinet/ip_divert.h>
101 #include <netinet/kpi_ipfilter_var.h>
103 /* needed for AUTOCONFIGURING: */
104 #include <netinet/udp.h>
105 #include <netinet/udp_var.h>
106 #include <netinet/bootp.h>
108 #include <sys/kdebug.h>
110 #define DBG_LAYER_BEG NETDBG_CODE(DBG_NETIP, 0)
111 #define DBG_LAYER_END NETDBG_CODE(DBG_NETIP, 2)
112 #define DBG_FNC_IP_INPUT NETDBG_CODE(DBG_NETIP, (2 << 8))
116 #include <netinet6/ipsec.h>
117 #include <netkey/key.h>
121 #if defined(NFAITH) && NFAITH > 0
122 #include <net/if_types.h>
126 #include <netinet/ip_dummynet.h>
130 extern int ipsec_bypass
;
131 extern lck_mtx_t
*sadb_mutex
;
135 static int ip_rsvp_on
;
136 struct socket
*ip_rsvpd
;
138 int ipforwarding
= 0;
139 SYSCTL_INT(_net_inet_ip
, IPCTL_FORWARDING
, forwarding
, CTLFLAG_RW
,
140 &ipforwarding
, 0, "Enable IP forwarding between interfaces");
142 static int ipsendredirects
= 1; /* XXX */
143 SYSCTL_INT(_net_inet_ip
, IPCTL_SENDREDIRECTS
, redirect
, CTLFLAG_RW
,
144 &ipsendredirects
, 0, "Enable sending IP redirects");
146 int ip_defttl
= IPDEFTTL
;
147 SYSCTL_INT(_net_inet_ip
, IPCTL_DEFTTL
, ttl
, CTLFLAG_RW
,
148 &ip_defttl
, 0, "Maximum TTL on IP packets");
150 static int ip_dosourceroute
= 0;
151 SYSCTL_INT(_net_inet_ip
, IPCTL_SOURCEROUTE
, sourceroute
, CTLFLAG_RW
,
152 &ip_dosourceroute
, 0, "Enable forwarding source routed IP packets");
154 static int ip_acceptsourceroute
= 0;
155 SYSCTL_INT(_net_inet_ip
, IPCTL_ACCEPTSOURCEROUTE
, accept_sourceroute
,
156 CTLFLAG_RW
, &ip_acceptsourceroute
, 0,
157 "Enable accepting source routed IP packets");
159 static int ip_keepfaith
= 0;
160 SYSCTL_INT(_net_inet_ip
, IPCTL_KEEPFAITH
, keepfaith
, CTLFLAG_RW
,
162 "Enable packet capture for FAITH IPv4->IPv6 translater daemon");
164 static int nipq
= 0; /* total # of reass queues */
166 SYSCTL_INT(_net_inet_ip
, OID_AUTO
, maxfragpackets
, CTLFLAG_RW
,
168 "Maximum number of IPv4 fragment reassembly queue entries");
170 static int maxfragsperpacket
;
171 SYSCTL_INT(_net_inet_ip
, OID_AUTO
, maxfragsperpacket
, CTLFLAG_RW
,
172 &maxfragsperpacket
, 0,
173 "Maximum number of IPv4 fragments allowed per packet");
176 SYSCTL_INT(_net_inet_ip
, OID_AUTO
, maxfrags
, CTLFLAG_RW
,
177 &maxfrags
, 0, "Maximum number of IPv4 fragments allowed");
179 static int currentfrags
= 0;
182 * XXX - Setting ip_checkinterface mostly implements the receive side of
183 * the Strong ES model described in RFC 1122, but since the routing table
184 * and transmit implementation do not implement the Strong ES model,
185 * setting this to 1 results in an odd hybrid.
187 * XXX - ip_checkinterface currently must be disabled if you use ipnat
188 * to translate the destination address to another local interface.
190 * XXX - ip_checkinterface must be disabled if you add IP aliases
191 * to the loopback interface instead of the interface where the
192 * packets for those addresses are received.
194 static int ip_checkinterface
= 0;
195 SYSCTL_INT(_net_inet_ip
, OID_AUTO
, check_interface
, CTLFLAG_RW
,
196 &ip_checkinterface
, 0, "Verify packet arrives on correct interface");
199 static int ipprintfs
= 0;
202 extern struct domain inetdomain
;
203 extern struct protosw inetsw
[];
204 struct protosw
*ip_protox
[IPPROTO_MAX
];
205 static int ipqmaxlen
= IFQ_MAXLEN
;
206 struct in_ifaddrhead in_ifaddrhead
; /* first inet address */
207 struct ifqueue ipintrq
;
208 SYSCTL_INT(_net_inet_ip
, IPCTL_INTRQMAXLEN
, intr_queue_maxlen
, CTLFLAG_RW
,
209 &ipintrq
.ifq_maxlen
, 0, "Maximum size of the IP input queue");
210 SYSCTL_INT(_net_inet_ip
, IPCTL_INTRQDROPS
, intr_queue_drops
, CTLFLAG_RD
,
211 &ipintrq
.ifq_drops
, 0, "Number of packets dropped from the IP input queue");
213 struct ipstat ipstat
;
214 SYSCTL_STRUCT(_net_inet_ip
, IPCTL_STATS
, stats
, CTLFLAG_RD
,
215 &ipstat
, ipstat
, "IP statistics (struct ipstat, netinet/ip_var.h)");
217 /* Packet reassembly stuff */
218 #define IPREASS_NHASH_LOG2 6
219 #define IPREASS_NHASH (1 << IPREASS_NHASH_LOG2)
220 #define IPREASS_HMASK (IPREASS_NHASH - 1)
221 #define IPREASS_HASH(x,y) \
222 (((((x) & 0xF) | ((((x) >> 8) & 0xF) << 4)) ^ (y)) & IPREASS_HMASK)
224 static struct ipq ipq
[IPREASS_NHASH
];
225 static TAILQ_HEAD(ipq_list
, ipq
) ipq_list
=
226 TAILQ_HEAD_INITIALIZER(ipq_list
);
227 const int ipintrq_present
= 1;
229 lck_attr_t
*ip_mutex_attr
;
230 lck_grp_t
*ip_mutex_grp
;
231 lck_grp_attr_t
*ip_mutex_grp_attr
;
232 lck_mtx_t
*inet_domain_mutex
;
233 extern lck_mtx_t
*domain_proto_mtx
;
236 SYSCTL_INT(_net_inet_ip
, IPCTL_DEFMTU
, mtu
, CTLFLAG_RW
,
237 &ip_mtu
, 0, "Default MTU");
241 static int ipstealth
= 0;
242 SYSCTL_INT(_net_inet_ip
, OID_AUTO
, stealth
, CTLFLAG_RW
,
248 ip_fw_chk_t
*ip_fw_chk_ptr
;
253 ip_dn_io_t
*ip_dn_io_ptr
;
256 int (*fr_checkp
)(struct ip
*, int, struct ifnet
*, int, struct mbuf
**) = NULL
;
258 SYSCTL_NODE(_net_inet_ip
, OID_AUTO
, linklocal
, CTLFLAG_RW
, 0, "link local");
260 struct ip_linklocal_stat ip_linklocal_stat
;
261 SYSCTL_STRUCT(_net_inet_ip_linklocal
, OID_AUTO
, stat
, CTLFLAG_RD
,
262 &ip_linklocal_stat
, ip_linklocal_stat
,
263 "Number of link local packets with TTL less than 255");
265 SYSCTL_NODE(_net_inet_ip_linklocal
, OID_AUTO
, in
, CTLFLAG_RW
, 0, "link local input");
267 int ip_linklocal_in_allowbadttl
= 1;
268 SYSCTL_INT(_net_inet_ip_linklocal_in
, OID_AUTO
, allowbadttl
, CTLFLAG_RW
,
269 &ip_linklocal_in_allowbadttl
, 0,
270 "Allow incoming link local packets with TTL less than 255");
274 * We need to save the IP options in case a protocol wants to respond
275 * to an incoming packet over the same route if the packet got here
276 * using IP source routing. This allows connection establishment and
277 * maintenance when the remote end is on a network that is not known
280 static int ip_nhops
= 0;
281 static struct ip_srcrt
{
282 struct in_addr dst
; /* final destination */
283 char nop
; /* one NOP to align */
284 char srcopt
[IPOPT_OFFSET
+ 1]; /* OPTVAL, OLEN and OFFSET */
285 struct in_addr route
[MAX_IPOPTLEN
/sizeof(struct in_addr
)];
289 extern struct mbuf
* m_dup(register struct mbuf
*m
, int how
);
292 static void save_rte(u_char
*, struct in_addr
);
293 static int ip_dooptions(struct mbuf
*, int, struct sockaddr_in
*, struct route
*ipforward_rt
);
294 static void ip_forward(struct mbuf
*, int, struct sockaddr_in
*, struct route
*ipforward_rt
);
295 static void ip_freef(struct ipq
*);
298 static struct mbuf
*ip_reass(struct mbuf
*,
299 struct ipq
*, struct ipq
*, u_int32_t
*, u_int16_t
*);
301 static struct mbuf
*ip_reass(struct mbuf
*,
302 struct ipq
*, struct ipq
*, u_int16_t
*, u_int16_t
*);
305 static struct mbuf
*ip_reass(struct mbuf
*, struct ipq
*, struct ipq
*);
310 extern u_short ip_id
;
313 extern u_long route_generation
;
314 extern int apple_hwcksum_rx
;
317 * IP initialization: fill in IP protocol switch table.
318 * All protocols not implemented in kernel go to raw IP protocol handler.
323 register struct protosw
*pr
;
325 static ip_initialized
= 0;
326 struct timeval timenow
;
331 TAILQ_INIT(&in_ifaddrhead
);
332 pr
= pffindproto_locked(PF_INET
, IPPROTO_RAW
, SOCK_RAW
);
335 for (i
= 0; i
< IPPROTO_MAX
; i
++)
337 for (pr
= inetdomain
.dom_protosw
; pr
; pr
= pr
->pr_next
)
338 { if(!((unsigned int)pr
->pr_domain
)) continue; /* If uninitialized, skip */
339 if (pr
->pr_domain
->dom_family
== PF_INET
&&
340 pr
->pr_protocol
&& pr
->pr_protocol
!= IPPROTO_RAW
)
341 ip_protox
[pr
->pr_protocol
] = pr
;
343 for (i
= 0; i
< IPREASS_NHASH
; i
++)
344 ipq
[i
].next
= ipq
[i
].prev
= &ipq
[i
];
346 maxnipq
= nmbclusters
/ 32;
347 maxfrags
= maxnipq
* 2;
348 maxfragsperpacket
= 128; /* enough for 64k in 512 byte fragments */
351 getmicrouptime(&timenow
);
352 ip_id
= timenow
.tv_sec
& 0xffff;
354 ipintrq
.ifq_maxlen
= ipqmaxlen
;
358 ip_mutex_grp_attr
= lck_grp_attr_alloc_init();
360 ip_mutex_grp
= lck_grp_alloc_init("ip", ip_mutex_grp_attr
);
362 ip_mutex_attr
= lck_attr_alloc_init();
364 if ((ip_mutex
= lck_mtx_alloc_init(ip_mutex_grp
, ip_mutex_attr
)) == NULL
) {
365 printf("ip_init: can't alloc ip_mutex\n");
375 protocol_family_t protocol
,
381 /* Initialize the PF_INET domain, and add in the pre-defined protos */
385 register struct protosw
*pr
;
386 register struct domain
*dp
;
387 static inetdomain_initted
= 0;
388 extern int in_proto_count
;
390 if (!inetdomain_initted
)
392 kprintf("Initing %d protosw entries\n", in_proto_count
);
394 dp
->dom_flags
= DOM_REENTRANT
;
396 for (i
=0, pr
= &inetsw
[0]; i
<in_proto_count
; i
++, pr
++)
397 net_add_proto(pr
, dp
);
398 inet_domain_mutex
= dp
->dom_mtx
;
399 inetdomain_initted
= 1;
401 lck_mtx_unlock(domain_proto_mtx
);
402 proto_register_input(PF_INET
, ip_proto_input
, NULL
);
403 lck_mtx_lock(domain_proto_mtx
);
407 __private_extern__
void
408 ip_proto_dispatch_in(
412 ipfilter_t inject_ipfref
)
414 struct ipfilter
*filter
;
415 int seen
= (inject_ipfref
== 0);
416 int changed_header
= 0;
419 if (!TAILQ_EMPTY(&ipv4_filters
)) {
421 TAILQ_FOREACH(filter
, &ipv4_filters
, ipf_link
) {
423 if ((struct ipfilter
*)inject_ipfref
== filter
)
425 } else if (filter
->ipf_filter
.ipf_input
) {
428 if (changed_header
== 0) {
430 ip
= mtod(m
, struct ip
*);
431 ip
->ip_len
= htons(ip
->ip_len
+ hlen
);
432 ip
->ip_off
= htons(ip
->ip_off
);
434 ip
->ip_sum
= in_cksum(m
, hlen
);
436 result
= filter
->ipf_filter
.ipf_input(
437 filter
->ipf_filter
.cookie
, (mbuf_t
*)&m
, hlen
, proto
);
438 if (result
== EJUSTRETURN
) {
452 * If there isn't a specific lock for the protocol
453 * we're about to call, use the generic lock for AF_INET.
454 * otherwise let the protocol deal with its own locking
456 ip
= mtod(m
, struct ip
*);
458 if (changed_header
) {
459 ip
->ip_len
= ntohs(ip
->ip_len
) - hlen
;
460 ip
->ip_off
= ntohs(ip
->ip_off
);
463 if (!(ip_protox
[ip
->ip_p
]->pr_flags
& PR_PROTOLOCK
)) {
464 lck_mtx_lock(inet_domain_mutex
);
465 (*ip_protox
[ip
->ip_p
]->pr_input
)(m
, hlen
);
466 lck_mtx_unlock(inet_domain_mutex
);
469 (*ip_protox
[ip
->ip_p
]->pr_input
)(m
, hlen
);
474 * ipforward_rt cleared in in_addroute()
475 * when a new route is successfully created.
477 static struct sockaddr_in ipaddr
= { sizeof(ipaddr
), AF_INET
};
480 * Ip input routine. Checksum and byte swap header. If fragmented
481 * try to reassemble. Process options. Pass to next level.
484 ip_input(struct mbuf
*m
)
488 struct in_ifaddr
*ia
= NULL
;
489 int i
, hlen
, mff
, checkif
;
491 struct in_addr pkt_dst
;
492 u_int32_t div_info
= 0; /* packet divert/tee info */
493 struct ip_fw_args args
;
494 ipfilter_t inject_filter_ref
= 0;
496 struct route ipforward_rt
= { 0 };
498 lck_mtx_lock(ip_mutex
);
503 args
.divert_rule
= 0; /* divert cookie */
504 args
.next_hop
= NULL
;
506 /* Grab info from mtags prepended to the chain */
508 if ((tag
= m_tag_locate(m
, KERNEL_MODULE_TAG_ID
, KERNEL_TAG_TYPE_DUMMYNET
, NULL
)) != NULL
) {
509 struct dn_pkt_tag
*dn_tag
;
511 dn_tag
= (struct dn_pkt_tag
*)(tag
+1);
512 args
.rule
= dn_tag
->rule
;
514 m_tag_delete(m
, tag
);
516 #endif /* DUMMYNET */
518 if ((tag
= m_tag_locate(m
, KERNEL_MODULE_TAG_ID
, KERNEL_TAG_TYPE_DIVERT
, NULL
)) != NULL
) {
519 struct divert_tag
*div_tag
;
521 div_tag
= (struct divert_tag
*)(tag
+1);
522 args
.divert_rule
= div_tag
->cookie
;
524 m_tag_delete(m
, tag
);
526 if ((tag
= m_tag_locate(m
, KERNEL_MODULE_TAG_ID
, KERNEL_TAG_TYPE_IPFORWARD
, NULL
)) != NULL
) {
527 struct ip_fwd_tag
*ipfwd_tag
;
529 ipfwd_tag
= (struct ip_fwd_tag
*)(tag
+1);
530 args
.next_hop
= ipfwd_tag
->next_hop
;
532 m_tag_delete(m
, tag
);
536 if (m
== NULL
|| (m
->m_flags
& M_PKTHDR
) == 0)
537 panic("ip_input no HDR");
540 if (args
.rule
) { /* dummynet already filtered us */
541 ip
= mtod(m
, struct ip
*);
542 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
543 inject_filter_ref
= ipf_get_inject_filter(m
);
548 * No need to proccess packet twice if we've
551 inject_filter_ref
= ipf_get_inject_filter(m
);
552 if (inject_filter_ref
!= 0) {
553 lck_mtx_unlock(ip_mutex
);
554 ip
= mtod(m
, struct ip
*);
555 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
556 ip
->ip_len
= ntohs(ip
->ip_len
) - hlen
;
557 ip
->ip_off
= ntohs(ip
->ip_off
);
558 ip_proto_dispatch_in(m
, hlen
, ip
->ip_p
, inject_filter_ref
);
564 if (m
->m_pkthdr
.len
< sizeof(struct ip
))
567 if (m
->m_len
< sizeof (struct ip
) &&
568 (m
= m_pullup(m
, sizeof (struct ip
))) == 0) {
569 ipstat
.ips_toosmall
++;
570 lck_mtx_unlock(ip_mutex
);
573 ip
= mtod(m
, struct ip
*);
575 KERNEL_DEBUG(DBG_LAYER_BEG
, ip
->ip_dst
.s_addr
,
576 ip
->ip_src
.s_addr
, ip
->ip_p
, ip
->ip_off
, ip
->ip_len
);
578 if (IP_VHL_V(ip
->ip_vhl
) != IPVERSION
) {
579 ipstat
.ips_badvers
++;
583 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
584 if (hlen
< sizeof(struct ip
)) { /* minimum header length */
585 ipstat
.ips_badhlen
++;
588 if (hlen
> m
->m_len
) {
589 if ((m
= m_pullup(m
, hlen
)) == 0) {
590 ipstat
.ips_badhlen
++;
591 lck_mtx_unlock(ip_mutex
);
594 ip
= mtod(m
, struct ip
*);
597 /* 127/8 must not appear on wire - RFC1122 */
598 if ((ntohl(ip
->ip_dst
.s_addr
) >> IN_CLASSA_NSHIFT
) == IN_LOOPBACKNET
||
599 (ntohl(ip
->ip_src
.s_addr
) >> IN_CLASSA_NSHIFT
) == IN_LOOPBACKNET
) {
600 if ((m
->m_pkthdr
.rcvif
->if_flags
& IFF_LOOPBACK
) == 0) {
601 ipstat
.ips_badaddr
++;
606 /* IPv4 Link-Local Addresses as defined in <draft-ietf-zeroconf-ipv4-linklocal-05.txt> */
607 if ((IN_LINKLOCAL(ntohl(ip
->ip_dst
.s_addr
)) ||
608 IN_LINKLOCAL(ntohl(ip
->ip_src
.s_addr
)))) {
609 ip_linklocal_stat
.iplls_in_total
++;
610 if (ip
->ip_ttl
!= MAXTTL
) {
611 ip_linklocal_stat
.iplls_in_badttl
++;
612 /* Silently drop link local traffic with bad TTL */
613 if (!ip_linklocal_in_allowbadttl
)
617 if ((IF_HWASSIST_CSUM_FLAGS(m
->m_pkthdr
.rcvif
->if_hwassist
) == 0)
618 || (apple_hwcksum_rx
== 0) ||
619 ((m
->m_pkthdr
.csum_flags
& CSUM_TCP_SUM16
) && ip
->ip_p
!= IPPROTO_TCP
)) {
620 m
->m_pkthdr
.csum_flags
= 0; /* invalidate HW generated checksum flags */
623 if (m
->m_pkthdr
.csum_flags
& CSUM_IP_CHECKED
) {
624 sum
= !(m
->m_pkthdr
.csum_flags
& CSUM_IP_VALID
);
626 sum
= in_cksum(m
, hlen
);
634 * Convert fields to host representation.
637 if (ip
->ip_len
< hlen
) {
644 * Check that the amount of data in the buffers
645 * is as at least much as the IP header would have us expect.
646 * Trim mbufs if longer than we expect.
647 * Drop packet if shorter than we expect.
649 if (m
->m_pkthdr
.len
< ip
->ip_len
) {
651 ipstat
.ips_tooshort
++;
654 if (m
->m_pkthdr
.len
> ip
->ip_len
) {
655 /* Invalidate hwcksuming */
656 m
->m_pkthdr
.csum_flags
= 0;
657 m
->m_pkthdr
.csum_data
= 0;
659 if (m
->m_len
== m
->m_pkthdr
.len
) {
660 m
->m_len
= ip
->ip_len
;
661 m
->m_pkthdr
.len
= ip
->ip_len
;
663 m_adj(m
, ip
->ip_len
- m
->m_pkthdr
.len
);
667 if (ipsec_bypass
== 0 && ipsec_gethist(m
, NULL
))
673 * Right now when no processing on packet has done
674 * and it is still fresh out of network we do our black
676 * - Firewall: deny/allow/divert
677 * - Xlate: translate packet's addr/port (NAT).
678 * - Pipe: pass pkt through dummynet.
679 * - Wrap: fake packet's addr/port <unimpl.>
680 * - Encapsulate: put it in another IP and send out. <unimp.>
683 #if defined(IPFIREWALL) && defined(DUMMYNET)
687 * Check if we want to allow this packet to be processed.
688 * Consider it to be bad if not.
693 if (fr_checkp(ip
, hlen
, m
->m_pkthdr
.rcvif
, 0, &m1
) || !m1
) {
694 lck_mtx_unlock(ip_mutex
);
697 ip
= mtod(m
= m1
, struct ip
*);
699 if (fw_enable
&& IPFW_LOADED
) {
700 #if IPFIREWALL_FORWARD
702 * If we've been forwarded from the output side, then
703 * skip the firewall a second time
707 #endif /* IPFIREWALL_FORWARD */
710 lck_mtx_unlock(ip_mutex
);
712 i
= ip_fw_chk_ptr(&args
);
715 if ( (i
& IP_FW_PORT_DENY_FLAG
) || m
== NULL
) { /* drop */
720 ip
= mtod(m
, struct ip
*); /* just in case m changed */
721 if (i
== 0 && args
.next_hop
== NULL
) { /* common case */
722 lck_mtx_lock(ip_mutex
);
726 if (DUMMYNET_LOADED
&& (i
& IP_FW_PORT_DYNT_FLAG
) != 0) {
727 /* Send packet to the appropriate pipe */
728 ip_dn_io_ptr(m
, i
&0xffff, DN_TO_IP_IN
, &args
);
731 #endif /* DUMMYNET */
733 if (i
!= 0 && (i
& IP_FW_PORT_DYNT_FLAG
) == 0) {
734 /* Divert or tee packet */
735 lck_mtx_lock(ip_mutex
);
740 #if IPFIREWALL_FORWARD
741 if (i
== 0 && args
.next_hop
!= NULL
) {
742 lck_mtx_lock(ip_mutex
);
747 * if we get here, the packet must be dropped
755 * Process options and, if not destined for us,
756 * ship it on. ip_dooptions returns 1 when an
757 * error was detected (causing an icmp message
758 * to be sent and the original packet to be freed).
760 ip_nhops
= 0; /* for source routed packets */
761 if (hlen
> sizeof (struct ip
) && ip_dooptions(m
, 0, args
.next_hop
, &ipforward_rt
)) {
762 lck_mtx_unlock(ip_mutex
);
766 /* greedy RSVP, snatches any PATH packet of the RSVP protocol and no
767 * matter if it is destined to another node, or whether it is
768 * a multicast one, RSVP wants it! and prevents it from being forwarded
769 * anywhere else. Also checks if the rsvp daemon is running before
770 * grabbing the packet.
772 if (rsvp_on
&& ip
->ip_p
==IPPROTO_RSVP
)
776 * Check our list of addresses, to see if the packet is for us.
777 * If we don't have any addresses, assume any unicast packet
778 * we receive might be for us (and let the upper layers deal
781 if (TAILQ_EMPTY(&in_ifaddrhead
) &&
782 (m
->m_flags
& (M_MCAST
|M_BCAST
)) == 0)
786 * Cache the destination address of the packet; this may be
787 * changed by use of 'ipfw fwd'.
789 pkt_dst
= args
.next_hop
== NULL
?
790 ip
->ip_dst
: args
.next_hop
->sin_addr
;
793 * Enable a consistency check between the destination address
794 * and the arrival interface for a unicast packet (the RFC 1122
795 * strong ES model) if IP forwarding is disabled and the packet
796 * is not locally generated and the packet is not subject to
799 * XXX - Checking also should be disabled if the destination
800 * address is ipnat'ed to a different interface.
802 * XXX - Checking is incompatible with IP aliases added
803 * to the loopback interface instead of the interface where
804 * the packets are received.
806 checkif
= ip_checkinterface
&& (ipforwarding
== 0) &&
807 ((m
->m_pkthdr
.rcvif
->if_flags
& IFF_LOOPBACK
) == 0) &&
808 (args
.next_hop
== NULL
);
810 lck_mtx_lock(rt_mtx
);
811 TAILQ_FOREACH(ia
, &in_ifaddrhead
, ia_link
) {
812 #define satosin(sa) ((struct sockaddr_in *)(sa))
814 if (IA_SIN(ia
)->sin_addr
.s_addr
== INADDR_ANY
) {
815 lck_mtx_unlock(rt_mtx
);
820 * If the address matches, verify that the packet
821 * arrived via the correct interface if checking is
824 if (IA_SIN(ia
)->sin_addr
.s_addr
== pkt_dst
.s_addr
&&
825 (!checkif
|| ia
->ia_ifp
== m
->m_pkthdr
.rcvif
)) {
826 lck_mtx_unlock(rt_mtx
);
830 * Only accept broadcast packets that arrive via the
831 * matching interface. Reception of forwarded directed
832 * broadcasts would be handled via ip_forward() and
833 * ether_output() with the loopback into the stack for
834 * SIMPLEX interfaces handled by ether_output().
836 if ((!checkif
|| ia
->ia_ifp
== m
->m_pkthdr
.rcvif
) &&
837 ia
->ia_ifp
&& ia
->ia_ifp
->if_flags
& IFF_BROADCAST
) {
838 if (satosin(&ia
->ia_broadaddr
)->sin_addr
.s_addr
==
840 lck_mtx_unlock(rt_mtx
);
843 if (ia
->ia_netbroadcast
.s_addr
== pkt_dst
.s_addr
) {
844 lck_mtx_unlock(rt_mtx
);
849 lck_mtx_unlock(rt_mtx
);
850 if (IN_MULTICAST(ntohl(ip
->ip_dst
.s_addr
))) {
851 struct in_multi
*inm
;
854 * If we are acting as a multicast router, all
855 * incoming multicast packets are passed to the
856 * kernel-level multicast forwarding function.
857 * The packet is returned (relatively) intact; if
858 * ip_mforward() returns a non-zero value, the packet
859 * must be discarded, else it may be accepted below.
862 ip_mforward(ip
, m
->m_pkthdr
.rcvif
, m
, 0) != 0) {
863 ipstat
.ips_cantforward
++;
865 lck_mtx_unlock(ip_mutex
);
870 * The process-level routing daemon needs to receive
871 * all multicast IGMP packets, whether or not this
872 * host belongs to their destination groups.
874 if (ip
->ip_p
== IPPROTO_IGMP
)
876 ipstat
.ips_forward
++;
879 * See if we belong to the destination multicast group on the
882 IN_LOOKUP_MULTI(ip
->ip_dst
, m
->m_pkthdr
.rcvif
, inm
);
884 ipstat
.ips_notmember
++;
886 lck_mtx_unlock(ip_mutex
);
891 if (ip
->ip_dst
.s_addr
== (u_long
)INADDR_BROADCAST
)
893 if (ip
->ip_dst
.s_addr
== INADDR_ANY
)
896 /* Allow DHCP/BootP responses through */
897 if (m
->m_pkthdr
.rcvif
!= NULL
898 && (m
->m_pkthdr
.rcvif
->if_eflags
& IFEF_AUTOCONFIGURING
)
899 && hlen
== sizeof(struct ip
)
900 && ip
->ip_p
== IPPROTO_UDP
) {
902 if (m
->m_len
< sizeof(struct udpiphdr
)
903 && (m
= m_pullup(m
, sizeof(struct udpiphdr
))) == 0) {
904 udpstat
.udps_hdrops
++;
905 lck_mtx_unlock(ip_mutex
);
908 ui
= mtod(m
, struct udpiphdr
*);
909 if (ntohs(ui
->ui_dport
) == IPPORT_BOOTPC
) {
912 ip
= mtod(m
, struct ip
*); /* in case it changed */
915 #if defined(NFAITH) && 0 < NFAITH
917 * FAITH(Firewall Aided Internet Translator)
919 if (m
->m_pkthdr
.rcvif
&& m
->m_pkthdr
.rcvif
->if_type
== IFT_FAITH
) {
921 if (ip
->ip_p
== IPPROTO_TCP
|| ip
->ip_p
== IPPROTO_ICMP
)
925 lck_mtx_unlock(ip_mutex
);
929 lck_mtx_unlock(ip_mutex
);
931 * Not for us; forward if possible and desirable.
933 if (ipforwarding
== 0) {
934 ipstat
.ips_cantforward
++;
937 ip_forward(m
, 0, args
.next_hop
, &ipforward_rt
);
943 /* Darwin does not have an if_data in ifaddr */
944 /* Count the packet in the ip address stats */
946 ia
->ia_ifa
.if_ipackets
++;
947 ia
->ia_ifa
.if_ibytes
+= m
->m_pkthdr
.len
;
952 * If offset or IP_MF are set, must reassemble.
953 * Otherwise, nothing need be done.
954 * (We could look in the reassembly queue to see
955 * if the packet was previously fragmented,
956 * but it's not worth the time; just let them time out.)
958 if (ip
->ip_off
& (IP_MF
| IP_OFFMASK
| IP_RF
)) {
960 /* If maxnipq is 0, never accept fragments. */
962 ipstat
.ips_fragments
++;
963 ipstat
.ips_fragdropped
++;
968 * If we will exceed the number of fragments in queues, timeout the
969 * oldest fragemented packet to make space.
971 if (currentfrags
>= maxfrags
) {
972 fp
= TAILQ_LAST(&ipq_list
, ipq_list
);
973 ipstat
.ips_fragtimeout
+= fp
->ipq_nfrags
;
975 if (ip
->ip_id
== fp
->ipq_id
&&
976 ip
->ip_src
.s_addr
== fp
->ipq_src
.s_addr
&&
977 ip
->ip_dst
.s_addr
== fp
->ipq_dst
.s_addr
&&
978 ip
->ip_p
== fp
->ipq_p
) {
980 * If we match the fragment queue we were going to
981 * discard, drop this packet too.
983 ipstat
.ips_fragdropped
++;
991 sum
= IPREASS_HASH(ip
->ip_src
.s_addr
, ip
->ip_id
);
993 * Look for queue of fragments
996 for (fp
= ipq
[sum
].next
; fp
!= &ipq
[sum
]; fp
= fp
->next
)
997 if (ip
->ip_id
== fp
->ipq_id
&&
998 ip
->ip_src
.s_addr
== fp
->ipq_src
.s_addr
&&
999 ip
->ip_dst
.s_addr
== fp
->ipq_dst
.s_addr
&&
1000 ip
->ip_p
== fp
->ipq_p
)
1004 * Enforce upper bound on number of fragmented packets
1005 * for which we attempt reassembly;
1006 * If maxnipq is -1, accept all fragments without limitation.
1008 if ((nipq
> maxnipq
) && (maxnipq
> 0)) {
1010 * drop the oldest fragment before proceeding further
1012 fp
= TAILQ_LAST(&ipq_list
, ipq_list
);
1013 ipstat
.ips_fragtimeout
+= fp
->ipq_nfrags
;
1021 * Adjust ip_len to not reflect header,
1022 * convert offset of this to bytes.
1025 if (ip
->ip_off
& IP_MF
) {
1027 * Make sure that fragments have a data length
1028 * that's a non-zero multiple of 8 bytes.
1030 if (ip
->ip_len
== 0 || (ip
->ip_len
& 0x7) != 0) {
1031 ipstat
.ips_toosmall
++; /* XXX */
1034 m
->m_flags
|= M_FRAG
;
1036 /* Clear the flag in case packet comes from loopback */
1037 m
->m_flags
&= ~M_FRAG
;
1042 * Attempt reassembly; if it succeeds, proceed.
1043 * ip_reass() will return a different mbuf, and update
1044 * the divert info in div_info and args.divert_rule.
1046 ipstat
.ips_fragments
++;
1047 m
->m_pkthdr
.header
= ip
;
1050 fp
, &ipq
[sum
], &div_info
, &args
.divert_rule
);
1052 m
= ip_reass(m
, fp
, &ipq
[sum
]);
1055 lck_mtx_unlock(ip_mutex
);
1058 ipstat
.ips_reassembled
++;
1059 ip
= mtod(m
, struct ip
*);
1060 /* Get the header length of the reassembled packet */
1061 hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
1063 /* Restore original checksum before diverting packet */
1064 if (div_info
!= 0) {
1069 ip
->ip_sum
= in_cksum(m
, hlen
);
1080 * Divert or tee packet to the divert protocol if required.
1082 * If div_info is zero then cookie should be too, so we shouldn't
1083 * need to clear them here. Assume divert_packet() does so also.
1085 if (div_info
!= 0) {
1086 struct mbuf
*clone
= NULL
;
1088 /* Clone packet if we're doing a 'tee' */
1089 if ((div_info
& IP_FW_PORT_TEE_FLAG
) != 0)
1090 clone
= m_dup(m
, M_DONTWAIT
);
1092 /* Restore packet header fields to original values */
1097 /* Deliver packet to divert input routine */
1098 ipstat
.ips_delivered
++;
1099 lck_mtx_unlock(ip_mutex
);
1100 divert_packet(m
, 1, div_info
& 0xffff, args
.divert_rule
);
1102 /* If 'tee', continue with original packet */
1103 if (clone
== NULL
) {
1106 lck_mtx_lock(ip_mutex
);
1108 ip
= mtod(m
, struct ip
*);
1114 * enforce IPsec policy checking if we are seeing last header.
1115 * note that we do not visit this with protocols with pcb layer
1116 * code - like udp/tcp/raw ip.
1118 if (ipsec_bypass
== 0 && (ip_protox
[ip
->ip_p
]->pr_flags
& PR_LASTHDR
) != 0) {
1119 lck_mtx_lock(sadb_mutex
);
1120 if (ipsec4_in_reject(m
, NULL
)) {
1121 ipsecstat
.in_polvio
++;
1122 lck_mtx_unlock(sadb_mutex
);
1125 lck_mtx_unlock(sadb_mutex
);
1130 * Switch out to protocol's input routine.
1132 ipstat
.ips_delivered
++;
1134 if (args
.next_hop
&& ip
->ip_p
== IPPROTO_TCP
) {
1135 /* TCP needs IPFORWARD info if available */
1136 struct m_tag
*fwd_tag
;
1137 struct ip_fwd_tag
*ipfwd_tag
;
1139 fwd_tag
= m_tag_alloc(KERNEL_MODULE_TAG_ID
, KERNEL_TAG_TYPE_IPFORWARD
,
1140 sizeof(struct sockaddr_in
), M_NOWAIT
);
1141 if (fwd_tag
== NULL
) {
1145 ipfwd_tag
= (struct ip_fwd_tag
*)(fwd_tag
+1);
1146 ipfwd_tag
->next_hop
= args
.next_hop
;
1148 m_tag_prepend(m
, fwd_tag
);
1150 KERNEL_DEBUG(DBG_LAYER_END
, ip
->ip_dst
.s_addr
,
1151 ip
->ip_src
.s_addr
, ip
->ip_p
, ip
->ip_off
, ip
->ip_len
);
1153 lck_mtx_unlock(ip_mutex
);
1155 /* TCP deals with its own locking */
1156 ip_proto_dispatch_in(m
, hlen
, ip
->ip_p
, 0);
1158 KERNEL_DEBUG(DBG_LAYER_END
, ip
->ip_dst
.s_addr
,
1159 ip
->ip_src
.s_addr
, ip
->ip_p
, ip
->ip_off
, ip
->ip_len
);
1161 lck_mtx_unlock(ip_mutex
);
1162 ip_proto_dispatch_in(m
, hlen
, ip
->ip_p
, 0);
1168 KERNEL_DEBUG(DBG_LAYER_END
, 0,0,0,0,0);
1169 lck_mtx_unlock(ip_mutex
);
1174 * Take incoming datagram fragment and try to reassemble it into
1175 * whole datagram. If a chain for reassembly of this datagram already
1176 * exists, then it is given as fp; otherwise have to make a chain.
1178 * When IPDIVERT enabled, keep additional state with each packet that
1179 * tells us if we need to divert or tee the packet we're building.
1182 static struct mbuf
*
1184 ip_reass(m
, fp
, where
, divinfo
, divcookie
)
1186 ip_reass(m
, fp
, where
)
1188 register struct mbuf
*m
;
1189 register struct ipq
*fp
;
1197 u_int16_t
*divcookie
;
1200 struct ip
*ip
= mtod(m
, struct ip
*);
1201 register struct mbuf
*p
= 0, *q
, *nq
;
1203 int hlen
= IP_VHL_HL(ip
->ip_vhl
) << 2;
1207 * Presence of header sizes in mbufs
1208 * would confuse code below.
1213 if (m
->m_pkthdr
.csum_flags
& CSUM_TCP_SUM16
)
1214 m
->m_pkthdr
.csum_flags
= 0;
1216 * If first fragment to arrive, create a reassembly queue.
1219 if ((t
= m_get(M_DONTWAIT
, MT_FTABLE
)) == NULL
)
1221 fp
= mtod(t
, struct ipq
*);
1222 insque((void*)fp
, (void*)where
);
1225 fp
->ipq_ttl
= IPFRAGTTL
;
1226 fp
->ipq_p
= ip
->ip_p
;
1227 fp
->ipq_id
= ip
->ip_id
;
1228 fp
->ipq_src
= ip
->ip_src
;
1229 fp
->ipq_dst
= ip
->ip_dst
;
1231 m
->m_nextpkt
= NULL
;
1234 fp
->ipq_div_info
= 0;
1238 fp
->ipq_div_cookie
= 0;
1240 TAILQ_INSERT_HEAD(&ipq_list
, fp
, ipq_list
);
1246 #define GETIP(m) ((struct ip*)((m)->m_pkthdr.header))
1249 * Find a segment which begins after this one does.
1251 for (p
= NULL
, q
= fp
->ipq_frags
; q
; p
= q
, q
= q
->m_nextpkt
)
1252 if (GETIP(q
)->ip_off
> ip
->ip_off
)
1256 * If there is a preceding segment, it may provide some of
1257 * our data already. If so, drop the data from the incoming
1258 * segment. If it provides all of our data, drop us, otherwise
1259 * stick new segment in the proper place.
1261 * If some of the data is dropped from the the preceding
1262 * segment, then it's checksum is invalidated.
1265 i
= GETIP(p
)->ip_off
+ GETIP(p
)->ip_len
- ip
->ip_off
;
1267 if (i
>= ip
->ip_len
)
1270 m
->m_pkthdr
.csum_flags
= 0;
1274 m
->m_nextpkt
= p
->m_nextpkt
;
1277 m
->m_nextpkt
= fp
->ipq_frags
;
1282 * While we overlap succeeding segments trim them or,
1283 * if they are completely covered, dequeue them.
1285 for (; q
!= NULL
&& ip
->ip_off
+ ip
->ip_len
> GETIP(q
)->ip_off
;
1287 i
= (ip
->ip_off
+ ip
->ip_len
) -
1289 if (i
< GETIP(q
)->ip_len
) {
1290 GETIP(q
)->ip_len
-= i
;
1291 GETIP(q
)->ip_off
+= i
;
1293 q
->m_pkthdr
.csum_flags
= 0;
1298 ipstat
.ips_fragdropped
++;
1308 * Transfer firewall instructions to the fragment structure.
1309 * Only trust info in the fragment at offset 0.
1311 if (ip
->ip_off
== 0) {
1313 fp
->ipq_div_info
= *divinfo
;
1315 fp
->ipq_divert
= *divinfo
;
1317 fp
->ipq_div_cookie
= *divcookie
;
1324 * Check for complete reassembly and perform frag per packet
1327 * Frag limiting is performed here so that the nth frag has
1328 * a chance to complete the packet before we drop the packet.
1329 * As a result, n+1 frags are actually allowed per packet, but
1330 * only n will ever be stored. (n = maxfragsperpacket.)
1334 for (p
= NULL
, q
= fp
->ipq_frags
; q
; p
= q
, q
= q
->m_nextpkt
) {
1335 if (GETIP(q
)->ip_off
!= next
) {
1336 if (fp
->ipq_nfrags
> maxfragsperpacket
) {
1337 ipstat
.ips_fragdropped
+= fp
->ipq_nfrags
;
1342 next
+= GETIP(q
)->ip_len
;
1344 /* Make sure the last packet didn't have the IP_MF flag */
1345 if (p
->m_flags
& M_FRAG
) {
1346 if (fp
->ipq_nfrags
> maxfragsperpacket
) {
1347 ipstat
.ips_fragdropped
+= fp
->ipq_nfrags
;
1354 * Reassembly is complete. Make sure the packet is a sane size.
1358 if (next
+ (IP_VHL_HL(ip
->ip_vhl
) << 2) > IP_MAXPACKET
) {
1359 ipstat
.ips_toolong
++;
1360 ipstat
.ips_fragdropped
+= fp
->ipq_nfrags
;
1366 * Concatenate fragments.
1374 for (q
= nq
; q
!= NULL
; q
= nq
) {
1376 q
->m_nextpkt
= NULL
;
1377 if (q
->m_pkthdr
.csum_flags
& CSUM_TCP_SUM16
)
1378 m
->m_pkthdr
.csum_flags
= 0;
1380 m
->m_pkthdr
.csum_flags
&= q
->m_pkthdr
.csum_flags
;
1381 m
->m_pkthdr
.csum_data
+= q
->m_pkthdr
.csum_data
;
1388 * Extract firewall instructions from the fragment structure.
1391 *divinfo
= fp
->ipq_div_info
;
1393 *divinfo
= fp
->ipq_divert
;
1395 *divcookie
= fp
->ipq_div_cookie
;
1399 * Create header for new ip packet by
1400 * modifying header of first packet;
1401 * dequeue and discard fragment reassembly header.
1402 * Make header visible.
1405 ip
->ip_src
= fp
->ipq_src
;
1406 ip
->ip_dst
= fp
->ipq_dst
;
1408 TAILQ_REMOVE(&ipq_list
, fp
, ipq_list
);
1409 currentfrags
-= fp
->ipq_nfrags
;
1411 (void) m_free(dtom(fp
));
1412 m
->m_len
+= (IP_VHL_HL(ip
->ip_vhl
) << 2);
1413 m
->m_data
-= (IP_VHL_HL(ip
->ip_vhl
) << 2);
1414 /* some debugging cruft by sklower, below, will go away soon */
1415 if (m
->m_flags
& M_PKTHDR
) { /* XXX this should be done elsewhere */
1416 register int plen
= 0;
1417 for (t
= m
; t
; t
= t
->m_next
)
1419 m
->m_pkthdr
.len
= plen
;
1428 ipstat
.ips_fragdropped
++;
1438 * Free a fragment reassembly header and all
1439 * associated datagrams.
1445 currentfrags
-= fp
->ipq_nfrags
;
1446 m_freem_list(fp
->ipq_frags
);
1448 TAILQ_REMOVE(&ipq_list
, fp
, ipq_list
);
1449 (void) m_free(dtom(fp
));
1454 * IP timer processing;
1455 * if a timer expires on a reassembly
1456 * queue, discard it.
1461 register struct ipq
*fp
;
1463 lck_mtx_lock(ip_mutex
);
1464 for (i
= 0; i
< IPREASS_NHASH
; i
++) {
1468 while (fp
!= &ipq
[i
]) {
1471 if (fp
->prev
->ipq_ttl
== 0) {
1472 ipstat
.ips_fragtimeout
+= fp
->prev
->ipq_nfrags
;
1478 * If we are over the maximum number of fragments
1479 * (due to the limit being lowered), drain off
1480 * enough to get down to the new limit.
1482 if (maxnipq
>= 0 && nipq
> maxnipq
) {
1483 for (i
= 0; i
< IPREASS_NHASH
; i
++) {
1484 while (nipq
> maxnipq
&&
1485 (ipq
[i
].next
!= &ipq
[i
])) {
1486 ipstat
.ips_fragdropped
+=
1487 ipq
[i
].next
->ipq_nfrags
;
1488 ip_freef(ipq
[i
].next
);
1493 lck_mtx_unlock(ip_mutex
);
1497 * Drain off all datagram fragments.
1504 lck_mtx_lock(ip_mutex
);
1505 for (i
= 0; i
< IPREASS_NHASH
; i
++) {
1506 while (ipq
[i
].next
!= &ipq
[i
]) {
1507 ipstat
.ips_fragdropped
+= ipq
[i
].next
->ipq_nfrags
;
1508 ip_freef(ipq
[i
].next
);
1511 lck_mtx_unlock(ip_mutex
);
1516 * Do option processing on a datagram,
1517 * possibly discarding it if bad options are encountered,
1518 * or forwarding it if source-routed.
1519 * The pass argument is used when operating in the IPSTEALTH
1520 * mode to tell what options to process:
1521 * [LS]SRR (pass 0) or the others (pass 1).
1522 * The reason for as many as two passes is that when doing IPSTEALTH,
1523 * non-routing options should be processed only if the packet is for us.
1524 * Returns 1 if packet has been forwarded/freed,
1525 * 0 if the packet should be processed further.
1528 ip_dooptions(struct mbuf
*m
, int pass
, struct sockaddr_in
*next_hop
, struct route
*ipforward_rt
)
1530 register struct ip
*ip
= mtod(m
, struct ip
*);
1531 register u_char
*cp
;
1532 register struct ip_timestamp
*ipt
;
1533 register struct in_ifaddr
*ia
;
1534 int opt
, optlen
, cnt
, off
, code
, type
= ICMP_PARAMPROB
, forward
= 0;
1535 struct in_addr
*sin
, dst
;
1539 cp
= (u_char
*)(ip
+ 1);
1540 cnt
= (IP_VHL_HL(ip
->ip_vhl
) << 2) - sizeof (struct ip
);
1541 for (; cnt
> 0; cnt
-= optlen
, cp
+= optlen
) {
1542 opt
= cp
[IPOPT_OPTVAL
];
1543 if (opt
== IPOPT_EOL
)
1545 if (opt
== IPOPT_NOP
)
1548 if (cnt
< IPOPT_OLEN
+ sizeof(*cp
)) {
1549 code
= &cp
[IPOPT_OLEN
] - (u_char
*)ip
;
1552 optlen
= cp
[IPOPT_OLEN
];
1553 if (optlen
< IPOPT_OLEN
+ sizeof(*cp
) || optlen
> cnt
) {
1554 code
= &cp
[IPOPT_OLEN
] - (u_char
*)ip
;
1564 * Source routing with record.
1565 * Find interface with current destination address.
1566 * If none on this machine then drop if strictly routed,
1567 * or do nothing if loosely routed.
1568 * Record interface address and bring up next address
1569 * component. If strictly routed make sure next
1570 * address is on directly accessible net.
1574 if (optlen
< IPOPT_OFFSET
+ sizeof(*cp
)) {
1575 code
= &cp
[IPOPT_OLEN
] - (u_char
*)ip
;
1578 if ((off
= cp
[IPOPT_OFFSET
]) < IPOPT_MINOFF
) {
1579 code
= &cp
[IPOPT_OFFSET
] - (u_char
*)ip
;
1582 ipaddr
.sin_addr
= ip
->ip_dst
;
1583 ia
= (struct in_ifaddr
*)
1584 ifa_ifwithaddr((struct sockaddr
*)&ipaddr
);
1586 if (opt
== IPOPT_SSRR
) {
1587 type
= ICMP_UNREACH
;
1588 code
= ICMP_UNREACH_SRCFAIL
;
1591 if (!ip_dosourceroute
)
1592 goto nosourcerouting
;
1594 * Loose routing, and not at next destination
1595 * yet; nothing to do except forward.
1600 ifafree(&ia
->ia_ifa
);
1603 off
--; /* 0 origin */
1604 if (off
> optlen
- (int)sizeof(struct in_addr
)) {
1606 * End of source route. Should be for us.
1608 if (!ip_acceptsourceroute
)
1609 goto nosourcerouting
;
1610 save_rte(cp
, ip
->ip_src
);
1614 if (!ip_dosourceroute
) {
1616 char buf
[MAX_IPv4_STR_LEN
];
1617 char buf2
[MAX_IPv4_STR_LEN
];
1619 * Acting as a router, so generate ICMP
1623 "attempted source route from %s to %s\n",
1624 inet_ntop(AF_INET
, &ip
->ip_src
, buf
, sizeof(buf
)),
1625 inet_ntop(AF_INET
, &ip
->ip_dst
, buf2
, sizeof(buf2
)));
1626 type
= ICMP_UNREACH
;
1627 code
= ICMP_UNREACH_SRCFAIL
;
1631 * Not acting as a router, so silently drop.
1633 ipstat
.ips_cantforward
++;
1640 * locate outgoing interface
1642 (void)memcpy(&ipaddr
.sin_addr
, cp
+ off
,
1643 sizeof(ipaddr
.sin_addr
));
1645 if (opt
== IPOPT_SSRR
) {
1646 #define INA struct in_ifaddr *
1647 #define SA struct sockaddr *
1648 if ((ia
= (INA
)ifa_ifwithdstaddr((SA
)&ipaddr
)) == 0) {
1649 ia
= (INA
)ifa_ifwithnet((SA
)&ipaddr
);
1652 ia
= ip_rtaddr(ipaddr
.sin_addr
, ipforward_rt
);
1655 type
= ICMP_UNREACH
;
1656 code
= ICMP_UNREACH_SRCFAIL
;
1659 ip
->ip_dst
= ipaddr
.sin_addr
;
1660 (void)memcpy(cp
+ off
, &(IA_SIN(ia
)->sin_addr
),
1661 sizeof(struct in_addr
));
1662 ifafree(&ia
->ia_ifa
);
1664 cp
[IPOPT_OFFSET
] += sizeof(struct in_addr
);
1666 * Let ip_intr's mcast routing check handle mcast pkts
1668 forward
= !IN_MULTICAST(ntohl(ip
->ip_dst
.s_addr
));
1672 if (optlen
< IPOPT_OFFSET
+ sizeof(*cp
)) {
1673 code
= &cp
[IPOPT_OFFSET
] - (u_char
*)ip
;
1676 if ((off
= cp
[IPOPT_OFFSET
]) < IPOPT_MINOFF
) {
1677 code
= &cp
[IPOPT_OFFSET
] - (u_char
*)ip
;
1681 * If no space remains, ignore.
1683 off
--; /* 0 origin */
1684 if (off
> optlen
- (int)sizeof(struct in_addr
))
1686 (void)memcpy(&ipaddr
.sin_addr
, &ip
->ip_dst
,
1687 sizeof(ipaddr
.sin_addr
));
1689 * locate outgoing interface; if we're the destination,
1690 * use the incoming interface (should be same).
1692 if ((ia
= (INA
)ifa_ifwithaddr((SA
)&ipaddr
)) == 0) {
1693 if ((ia
= ip_rtaddr(ipaddr
.sin_addr
, ipforward_rt
)) == 0) {
1694 type
= ICMP_UNREACH
;
1695 code
= ICMP_UNREACH_HOST
;
1699 (void)memcpy(cp
+ off
, &(IA_SIN(ia
)->sin_addr
),
1700 sizeof(struct in_addr
));
1701 ifafree(&ia
->ia_ifa
);
1703 cp
[IPOPT_OFFSET
] += sizeof(struct in_addr
);
1707 code
= cp
- (u_char
*)ip
;
1708 ipt
= (struct ip_timestamp
*)cp
;
1709 if (ipt
->ipt_len
< 4 || ipt
->ipt_len
> 40) {
1710 code
= (u_char
*)&ipt
->ipt_len
- (u_char
*)ip
;
1713 if (ipt
->ipt_ptr
< 5) {
1714 code
= (u_char
*)&ipt
->ipt_ptr
- (u_char
*)ip
;
1718 ipt
->ipt_len
- (int)sizeof(int32_t)) {
1719 if (++ipt
->ipt_oflw
== 0) {
1720 code
= (u_char
*)&ipt
->ipt_ptr
-
1726 sin
= (struct in_addr
*)(cp
+ ipt
->ipt_ptr
- 1);
1727 switch (ipt
->ipt_flg
) {
1729 case IPOPT_TS_TSONLY
:
1732 case IPOPT_TS_TSANDADDR
:
1733 if (ipt
->ipt_ptr
- 1 + sizeof(n_time
) +
1734 sizeof(struct in_addr
) > ipt
->ipt_len
) {
1735 code
= (u_char
*)&ipt
->ipt_ptr
-
1739 ipaddr
.sin_addr
= dst
;
1740 ia
= (INA
)ifaof_ifpforaddr((SA
)&ipaddr
,
1744 (void)memcpy(sin
, &IA_SIN(ia
)->sin_addr
,
1745 sizeof(struct in_addr
));
1746 ipt
->ipt_ptr
+= sizeof(struct in_addr
);
1747 ifafree(&ia
->ia_ifa
);
1751 case IPOPT_TS_PRESPEC
:
1752 if (ipt
->ipt_ptr
- 1 + sizeof(n_time
) +
1753 sizeof(struct in_addr
) > ipt
->ipt_len
) {
1754 code
= (u_char
*)&ipt
->ipt_ptr
-
1758 (void)memcpy(&ipaddr
.sin_addr
, sin
,
1759 sizeof(struct in_addr
));
1760 if ((ia
= (struct in_ifaddr
*)ifa_ifwithaddr((SA
)&ipaddr
)) == 0)
1762 ifafree(&ia
->ia_ifa
);
1764 ipt
->ipt_ptr
+= sizeof(struct in_addr
);
1768 /* XXX can't take &ipt->ipt_flg */
1769 code
= (u_char
*)&ipt
->ipt_ptr
-
1774 (void)memcpy(cp
+ ipt
->ipt_ptr
- 1, &ntime
,
1776 ipt
->ipt_ptr
+= sizeof(n_time
);
1779 if (forward
&& ipforwarding
) {
1780 ip_forward(m
, 1, next_hop
, ipforward_rt
);
1785 ip
->ip_len
-= IP_VHL_HL(ip
->ip_vhl
) << 2; /* XXX icmp_error adds in hdr length */
1786 lck_mtx_unlock(ip_mutex
);
1787 icmp_error(m
, type
, code
, 0, 0);
1788 lck_mtx_lock(ip_mutex
);
1789 ipstat
.ips_badoptions
++;
1794 * Given address of next destination (final or next hop),
1795 * return internet address info of interface to be used to get there.
1802 register struct sockaddr_in
*sin
;
1804 sin
= (struct sockaddr_in
*)&rt
->ro_dst
;
1806 lck_mtx_lock(rt_mtx
);
1807 if (rt
->ro_rt
== 0 || dst
.s_addr
!= sin
->sin_addr
.s_addr
||
1808 rt
->ro_rt
->generation_id
!= route_generation
) {
1810 rtfree_locked(rt
->ro_rt
);
1813 sin
->sin_family
= AF_INET
;
1814 sin
->sin_len
= sizeof(*sin
);
1815 sin
->sin_addr
= dst
;
1817 rtalloc_ign_locked(rt
, RTF_PRCLONING
);
1819 if (rt
->ro_rt
== 0) {
1820 lck_mtx_unlock(rt_mtx
);
1821 return ((struct in_ifaddr
*)0);
1824 if (rt
->ro_rt
->rt_ifa
)
1825 ifaref(rt
->ro_rt
->rt_ifa
);
1826 lck_mtx_unlock(rt_mtx
);
1827 return ((struct in_ifaddr
*) rt
->ro_rt
->rt_ifa
);
1831 * Save incoming source route for use in replies,
1832 * to be picked up later by ip_srcroute if the receiver is interested.
1835 save_rte(option
, dst
)
1841 olen
= option
[IPOPT_OLEN
];
1844 printf("save_rte: olen %d\n", olen
);
1846 if (olen
> sizeof(ip_srcrt
) - (1 + sizeof(dst
)))
1848 bcopy(option
, ip_srcrt
.srcopt
, olen
);
1849 ip_nhops
= (olen
- IPOPT_OFFSET
- 1) / sizeof(struct in_addr
);
1854 * Retrieve incoming source route for use in replies,
1855 * in the same form used by setsockopt.
1856 * The first hop is placed before the options, will be removed later.
1861 register struct in_addr
*p
, *q
;
1862 register struct mbuf
*m
;
1865 return ((struct mbuf
*)0);
1866 m
= m_get(M_DONTWAIT
, MT_HEADER
);
1868 return ((struct mbuf
*)0);
1870 #define OPTSIZ (sizeof(ip_srcrt.nop) + sizeof(ip_srcrt.srcopt))
1872 /* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */
1873 m
->m_len
= ip_nhops
* sizeof(struct in_addr
) + sizeof(struct in_addr
) +
1877 printf("ip_srcroute: nhops %d mlen %d", ip_nhops
, m
->m_len
);
1881 * First save first hop for return route
1883 p
= &ip_srcrt
.route
[ip_nhops
- 1];
1884 *(mtod(m
, struct in_addr
*)) = *p
--;
1887 printf(" hops %lx", (u_long
)ntohl(mtod(m
, struct in_addr
*)->s_addr
));
1891 * Copy option fields and padding (nop) to mbuf.
1893 ip_srcrt
.nop
= IPOPT_NOP
;
1894 ip_srcrt
.srcopt
[IPOPT_OFFSET
] = IPOPT_MINOFF
;
1895 (void)memcpy(mtod(m
, caddr_t
) + sizeof(struct in_addr
),
1896 &ip_srcrt
.nop
, OPTSIZ
);
1897 q
= (struct in_addr
*)(mtod(m
, caddr_t
) +
1898 sizeof(struct in_addr
) + OPTSIZ
);
1901 * Record return path as an IP source route,
1902 * reversing the path (pointers are now aligned).
1904 while (p
>= ip_srcrt
.route
) {
1907 printf(" %lx", (u_long
)ntohl(q
->s_addr
));
1912 * Last hop goes to final destination.
1917 printf(" %lx\n", (u_long
)ntohl(q
->s_addr
));
1923 * Strip out IP options, at higher
1924 * level protocol in the kernel.
1925 * Second argument is buffer to which options
1926 * will be moved, and return value is their length.
1927 * XXX should be deleted; last arg currently ignored.
1930 ip_stripoptions(m
, mopt
)
1931 register struct mbuf
*m
;
1935 struct ip
*ip
= mtod(m
, struct ip
*);
1936 register caddr_t opts
;
1939 olen
= (IP_VHL_HL(ip
->ip_vhl
) << 2) - sizeof (struct ip
);
1940 opts
= (caddr_t
)(ip
+ 1);
1941 i
= m
->m_len
- (sizeof (struct ip
) + olen
);
1942 bcopy(opts
+ olen
, opts
, (unsigned)i
);
1944 if (m
->m_flags
& M_PKTHDR
)
1945 m
->m_pkthdr
.len
-= olen
;
1946 ip
->ip_vhl
= IP_MAKE_VHL(IPVERSION
, sizeof(struct ip
) >> 2);
1949 u_char inetctlerrmap
[PRC_NCMDS
] = {
1951 0, EMSGSIZE
, EHOSTDOWN
, EHOSTUNREACH
,
1952 EHOSTUNREACH
, EHOSTUNREACH
, ECONNREFUSED
, ECONNREFUSED
,
1953 EMSGSIZE
, EHOSTUNREACH
, 0, 0,
1955 ENOPROTOOPT
, ECONNREFUSED
1959 * Forward a packet. If some error occurs return the sender
1960 * an icmp packet. Note we can't always generate a meaningful
1961 * icmp message because icmp doesn't have a large enough repertoire
1962 * of codes and types.
1964 * If not forwarding, just drop the packet. This could be confusing
1965 * if ipforwarding was zero but some routing protocol was advancing
1966 * us as a gateway to somewhere. However, we must let the routing
1967 * protocol deal with that.
1969 * The srcrt parameter indicates whether the packet is being forwarded
1970 * via a source route.
1973 ip_forward(struct mbuf
*m
, int srcrt
, struct sockaddr_in
*next_hop
, struct route
*ipforward_rt
)
1975 register struct ip
*ip
= mtod(m
, struct ip
*);
1976 register struct sockaddr_in
*sin
;
1977 register struct rtentry
*rt
;
1978 int error
, type
= 0, code
= 0;
1981 struct in_addr pkt_dst
;
1982 struct ifnet
*destifp
;
1984 struct ifnet dummyifp
;
1989 * Cache the destination address of the packet; this may be
1990 * changed by use of 'ipfw fwd'.
1992 pkt_dst
= next_hop
? next_hop
->sin_addr
: ip
->ip_dst
;
1996 printf("forward: src %lx dst %lx ttl %x\n",
1997 (u_long
)ip
->ip_src
.s_addr
, (u_long
)pkt_dst
.s_addr
,
2002 if (m
->m_flags
& (M_BCAST
|M_MCAST
) || in_canforward(pkt_dst
) == 0) {
2003 ipstat
.ips_cantforward
++;
2010 if (ip
->ip_ttl
<= IPTTLDEC
) {
2011 icmp_error(m
, ICMP_TIMXCEED
, ICMP_TIMXCEED_INTRANS
,
2019 sin
= (struct sockaddr_in
*)&ipforward_rt
->ro_dst
;
2020 if ((rt
= ipforward_rt
->ro_rt
) == 0 ||
2021 pkt_dst
.s_addr
!= sin
->sin_addr
.s_addr
||
2022 ipforward_rt
->ro_rt
->generation_id
!= route_generation
) {
2023 if (ipforward_rt
->ro_rt
) {
2024 rtfree(ipforward_rt
->ro_rt
);
2025 ipforward_rt
->ro_rt
= 0;
2027 sin
->sin_family
= AF_INET
;
2028 sin
->sin_len
= sizeof(*sin
);
2029 sin
->sin_addr
= pkt_dst
;
2031 rtalloc_ign(ipforward_rt
, RTF_PRCLONING
);
2032 if (ipforward_rt
->ro_rt
== 0) {
2033 icmp_error(m
, ICMP_UNREACH
, ICMP_UNREACH_HOST
, dest
, 0);
2036 rt
= ipforward_rt
->ro_rt
;
2040 * Save the IP header and at most 8 bytes of the payload,
2041 * in case we need to generate an ICMP message to the src.
2043 * We don't use m_copy() because it might return a reference
2044 * to a shared cluster. Both this function and ip_output()
2045 * assume exclusive access to the IP header in `m', so any
2046 * data in a cluster may change before we reach icmp_error().
2048 MGET(mcopy
, M_DONTWAIT
, m
->m_type
);
2049 if (mcopy
!= NULL
) {
2050 M_COPY_PKTHDR(mcopy
, m
);
2051 mcopy
->m_len
= imin((IP_VHL_HL(ip
->ip_vhl
) << 2) + 8,
2053 m_copydata(m
, 0, mcopy
->m_len
, mtod(mcopy
, caddr_t
));
2059 ip
->ip_ttl
-= IPTTLDEC
;
2065 * If forwarding packet using same interface that it came in on,
2066 * perhaps should send a redirect to sender to shortcut a hop.
2067 * Only send redirect if source is sending directly to us,
2068 * and if packet was not source routed (or has any options).
2069 * Also, don't send redirect if forwarding using a default route
2070 * or a route modified by a redirect.
2072 #define satosin(sa) ((struct sockaddr_in *)(sa))
2073 if (rt
->rt_ifp
== m
->m_pkthdr
.rcvif
&&
2074 (rt
->rt_flags
& (RTF_DYNAMIC
|RTF_MODIFIED
)) == 0 &&
2075 satosin(rt_key(rt
))->sin_addr
.s_addr
!= 0 &&
2076 ipsendredirects
&& !srcrt
) {
2077 #define RTA(rt) ((struct in_ifaddr *)(rt->rt_ifa))
2078 u_long src
= ntohl(ip
->ip_src
.s_addr
);
2081 (src
& RTA(rt
)->ia_subnetmask
) == RTA(rt
)->ia_subnet
) {
2082 if (rt
->rt_flags
& RTF_GATEWAY
)
2083 dest
= satosin(rt
->rt_gateway
)->sin_addr
.s_addr
;
2085 dest
= pkt_dst
.s_addr
;
2086 /* Router requirements says to only send host redirects */
2087 type
= ICMP_REDIRECT
;
2088 code
= ICMP_REDIRECT_HOST
;
2091 printf("redirect (%d) to %lx\n", code
, (u_long
)dest
);
2098 /* Pass IPFORWARD info if available */
2100 struct ip_fwd_tag
*ipfwd_tag
;
2102 tag
= m_tag_alloc(KERNEL_MODULE_TAG_ID
, KERNEL_TAG_TYPE_IPFORWARD
,
2103 sizeof(struct sockaddr_in
), M_NOWAIT
);
2110 ipfwd_tag
= (struct ip_fwd_tag
*)(tag
+1);
2111 ipfwd_tag
->next_hop
= next_hop
;
2113 m_tag_prepend(m
, tag
);
2115 error
= ip_output_list(m
, 0, (struct mbuf
*)0, ipforward_rt
,
2119 ipstat
.ips_cantforward
++;
2121 ipstat
.ips_forward
++;
2123 ipstat
.ips_redirectsent
++;
2126 ipflow_create(ipforward_rt
, mcopy
);
2138 case 0: /* forwarded, but need redirect */
2139 /* type, code set above */
2142 case ENETUNREACH
: /* shouldn't happen, checked above */
2147 type
= ICMP_UNREACH
;
2148 code
= ICMP_UNREACH_HOST
;
2152 type
= ICMP_UNREACH
;
2153 code
= ICMP_UNREACH_NEEDFRAG
;
2155 if (ipforward_rt
->ro_rt
)
2156 destifp
= ipforward_rt
->ro_rt
->rt_ifp
;
2159 * If the packet is routed over IPsec tunnel, tell the
2160 * originator the tunnel MTU.
2161 * tunnel MTU = if MTU - sizeof(IP) - ESP/AH hdrsiz
2164 if (ipforward_rt
->ro_rt
) {
2165 struct secpolicy
*sp
= NULL
;
2171 destifp
= ipforward_rt
->ro_rt
->rt_ifp
;
2172 ipstat
.ips_cantfrag
++;
2175 lck_mtx_lock(sadb_mutex
);
2176 sp
= ipsec4_getpolicybyaddr(mcopy
,
2182 destifp
= ipforward_rt
->ro_rt
->rt_ifp
;
2184 /* count IPsec header size */
2185 ipsechdr
= ipsec4_hdrsiz(mcopy
,
2190 * find the correct route for outer IPv4
2191 * header, compute tunnel MTU.
2194 * The "dummyifp" code relies upon the fact
2195 * that icmp_error() touches only ifp->if_mtu.
2200 && sp
->req
->sav
!= NULL
2201 && sp
->req
->sav
->sah
!= NULL
) {
2202 ro
= &sp
->req
->sav
->sah
->sa_route
;
2203 if (ro
->ro_rt
&& ro
->ro_rt
->rt_ifp
) {
2205 ro
->ro_rt
->rt_ifp
->if_mtu
;
2206 dummyifp
.if_mtu
-= ipsechdr
;
2207 destifp
= &dummyifp
;
2213 lck_mtx_unlock(sadb_mutex
);
2216 ipstat
.ips_cantfrag
++;
2220 type
= ICMP_SOURCEQUENCH
;
2224 case EACCES
: /* ipfw denied packet */
2228 icmp_error(mcopy
, type
, code
, dest
, destifp
);
2233 register struct inpcb
*inp
,
2234 register struct mbuf
**mp
,
2235 register struct ip
*ip
,
2236 register struct mbuf
*m
)
2238 if (inp
->inp_socket
->so_options
& SO_TIMESTAMP
) {
2242 *mp
= sbcreatecontrol((caddr_t
) &tv
, sizeof(tv
),
2243 SCM_TIMESTAMP
, SOL_SOCKET
);
2245 mp
= &(*mp
)->m_next
;
2247 if (inp
->inp_flags
& INP_RECVDSTADDR
) {
2248 *mp
= sbcreatecontrol((caddr_t
) &ip
->ip_dst
,
2249 sizeof(struct in_addr
), IP_RECVDSTADDR
, IPPROTO_IP
);
2251 mp
= &(*mp
)->m_next
;
2255 * Moving these out of udp_input() made them even more broken
2256 * than they already were.
2258 /* options were tossed already */
2259 if (inp
->inp_flags
& INP_RECVOPTS
) {
2260 *mp
= sbcreatecontrol((caddr_t
) opts_deleted_above
,
2261 sizeof(struct in_addr
), IP_RECVOPTS
, IPPROTO_IP
);
2263 mp
= &(*mp
)->m_next
;
2265 /* ip_srcroute doesn't do what we want here, need to fix */
2266 if (inp
->inp_flags
& INP_RECVRETOPTS
) {
2267 *mp
= sbcreatecontrol((caddr_t
) ip_srcroute(),
2268 sizeof(struct in_addr
), IP_RECVRETOPTS
, IPPROTO_IP
);
2270 mp
= &(*mp
)->m_next
;
2273 if (inp
->inp_flags
& INP_RECVIF
) {
2276 struct sockaddr_dl sdl
;
2279 struct sockaddr_dl
*sdp
;
2280 struct sockaddr_dl
*sdl2
= &sdlbuf
.sdl
;
2282 ifnet_head_lock_shared();
2283 if (((ifp
= m
->m_pkthdr
.rcvif
))
2284 && ( ifp
->if_index
&& (ifp
->if_index
<= if_index
))) {
2285 struct ifaddr
*ifa
= ifnet_addrs
[ifp
->if_index
- 1];
2287 if (!ifa
|| !ifa
->ifa_addr
)
2290 sdp
= (struct sockaddr_dl
*)ifa
->ifa_addr
;
2292 * Change our mind and don't try copy.
2294 if ((sdp
->sdl_family
!= AF_LINK
)
2295 || (sdp
->sdl_len
> sizeof(sdlbuf
))) {
2298 bcopy(sdp
, sdl2
, sdp
->sdl_len
);
2302 = offsetof(struct sockaddr_dl
, sdl_data
[0]);
2303 sdl2
->sdl_family
= AF_LINK
;
2304 sdl2
->sdl_index
= 0;
2305 sdl2
->sdl_nlen
= sdl2
->sdl_alen
= sdl2
->sdl_slen
= 0;
2308 *mp
= sbcreatecontrol((caddr_t
) sdl2
, sdl2
->sdl_len
,
2309 IP_RECVIF
, IPPROTO_IP
);
2311 mp
= &(*mp
)->m_next
;
2313 if (inp
->inp_flags
& INP_RECVTTL
) {
2314 *mp
= sbcreatecontrol((caddr_t
)&ip
->ip_ttl
, sizeof(ip
->ip_ttl
), IP_RECVTTL
, IPPROTO_IP
);
2315 if (*mp
) mp
= &(*mp
)->m_next
;
2320 ip_rsvp_init(struct socket
*so
)
2322 if (so
->so_type
!= SOCK_RAW
||
2323 so
->so_proto
->pr_protocol
!= IPPROTO_RSVP
)
2326 if (ip_rsvpd
!= NULL
)
2331 * This may seem silly, but we need to be sure we don't over-increment
2332 * the RSVP counter, in case something slips up.
2347 * This may seem silly, but we need to be sure we don't over-decrement
2348 * the RSVP counter, in case something slips up.