]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
6d2010ae | 2 | * Copyright (c) 2000-2011 Apple Inc. All rights reserved. |
5d5c5d0d | 3 | * |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
1c79356b | 5 | * |
2d21ac55 A |
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. | |
8f6c56a5 | 14 | * |
2d21ac55 A |
15 | * Please obtain a copy of the License at |
16 | * http://www.opensource.apple.com/apsl/ and read it before using this file. | |
17 | * | |
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 | |
8f6c56a5 A |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
2d21ac55 A |
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. | |
8f6c56a5 | 25 | * |
2d21ac55 | 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
1c79356b A |
27 | */ |
28 | /* | |
29 | * Copyright (c) 1982, 1986, 1988, 1993 | |
30 | * The Regents of the University of California. All rights reserved. | |
31 | * | |
32 | * Redistribution and use in source and binary forms, with or without | |
33 | * modification, are permitted provided that the following conditions | |
34 | * are met: | |
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. | |
47 | * | |
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 | |
58 | * SUCH DAMAGE. | |
59 | * | |
60 | * @(#)ip_input.c 8.2 (Berkeley) 1/4/94 | |
9bccf70c | 61 | * $FreeBSD: src/sys/netinet/ip_input.c,v 1.130.2.25 2001/08/29 21:41:37 jesper Exp $ |
1c79356b | 62 | */ |
2d21ac55 A |
63 | /* |
64 | * NOTICE: This file was modified by SPARTA, Inc. in 2007 to introduce | |
65 | * support for mandatory and extensible security protections. This notice | |
66 | * is included in support of clause 2.2 (b) of the Apple Public License, | |
67 | * Version 2.0. | |
68 | */ | |
1c79356b A |
69 | |
70 | #define _IP_VHL | |
71 | ||
1c79356b A |
72 | #include <sys/param.h> |
73 | #include <sys/systm.h> | |
74 | #include <sys/mbuf.h> | |
75 | #include <sys/malloc.h> | |
76 | #include <sys/domain.h> | |
77 | #include <sys/protosw.h> | |
78 | #include <sys/socket.h> | |
79 | #include <sys/time.h> | |
80 | #include <sys/kernel.h> | |
81 | #include <sys/syslog.h> | |
82 | #include <sys/sysctl.h> | |
6d2010ae A |
83 | #include <sys/mcache.h> |
84 | #include <mach/mach_time.h> | |
1c79356b | 85 | |
b0d623f7 A |
86 | #include <machine/endian.h> |
87 | ||
1c79356b | 88 | #include <kern/queue.h> |
91447636 | 89 | #include <kern/locks.h> |
1c79356b | 90 | |
2d21ac55 A |
91 | #include <pexpert/pexpert.h> |
92 | ||
1c79356b A |
93 | #include <net/if.h> |
94 | #include <net/if_var.h> | |
95 | #include <net/if_dl.h> | |
96 | #include <net/route.h> | |
91447636 | 97 | #include <net/kpi_protocol.h> |
6d2010ae | 98 | #include <net/ntstat.h> |
1c79356b A |
99 | |
100 | #include <netinet/in.h> | |
101 | #include <netinet/in_systm.h> | |
102 | #include <netinet/in_var.h> | |
b0d623f7 | 103 | #include <netinet/in_arp.h> |
1c79356b | 104 | #include <netinet/ip.h> |
1c79356b A |
105 | #include <netinet/in_pcb.h> |
106 | #include <netinet/ip_var.h> | |
107 | #include <netinet/ip_icmp.h> | |
108 | #include <sys/socketvar.h> | |
109 | ||
9bccf70c | 110 | #include <netinet/ip_fw.h> |
91447636 A |
111 | #include <netinet/ip_divert.h> |
112 | ||
113 | #include <netinet/kpi_ipfilter_var.h> | |
1c79356b | 114 | |
9bccf70c A |
115 | /* needed for AUTOCONFIGURING: */ |
116 | #include <netinet/udp.h> | |
117 | #include <netinet/udp_var.h> | |
118 | #include <netinet/bootp.h> | |
6d2010ae | 119 | #include <mach/sdt.h> |
9bccf70c | 120 | |
2d21ac55 A |
121 | #if CONFIG_MACF_NET |
122 | #include <security/mac_framework.h> | |
123 | #endif | |
124 | ||
9bccf70c | 125 | #include <sys/kdebug.h> |
2d21ac55 | 126 | #include <libkern/OSAtomic.h> |
1c79356b A |
127 | |
128 | #define DBG_LAYER_BEG NETDBG_CODE(DBG_NETIP, 0) | |
129 | #define DBG_LAYER_END NETDBG_CODE(DBG_NETIP, 2) | |
130 | #define DBG_FNC_IP_INPUT NETDBG_CODE(DBG_NETIP, (2 << 8)) | |
131 | ||
132 | ||
1c79356b A |
133 | #if IPSEC |
134 | #include <netinet6/ipsec.h> | |
135 | #include <netkey/key.h> | |
1c79356b A |
136 | #endif |
137 | ||
138 | #include "faith.h" | |
139 | #if defined(NFAITH) && NFAITH > 0 | |
140 | #include <net/if_types.h> | |
141 | #endif | |
142 | ||
143 | #if DUMMYNET | |
144 | #include <netinet/ip_dummynet.h> | |
145 | #endif | |
146 | ||
b0d623f7 A |
147 | #if PF |
148 | #include <net/pfvar.h> | |
149 | #endif /* PF */ | |
150 | ||
9bccf70c A |
151 | #if IPSEC |
152 | extern int ipsec_bypass; | |
91447636 | 153 | extern lck_mtx_t *sadb_mutex; |
b0d623f7 A |
154 | |
155 | lck_grp_t *sadb_stat_mutex_grp; | |
156 | lck_grp_attr_t *sadb_stat_mutex_grp_attr; | |
157 | lck_attr_t *sadb_stat_mutex_attr; | |
158 | lck_mtx_t *sadb_stat_mutex; | |
159 | ||
9bccf70c A |
160 | #endif |
161 | ||
1c79356b A |
162 | int rsvp_on = 0; |
163 | static int ip_rsvp_on; | |
164 | struct socket *ip_rsvpd; | |
165 | ||
b0d623f7 A |
166 | static int sysctl_ipforwarding SYSCTL_HANDLER_ARGS; |
167 | ||
1c79356b | 168 | int ipforwarding = 0; |
b0d623f7 A |
169 | SYSCTL_PROC(_net_inet_ip, IPCTL_FORWARDING, forwarding, |
170 | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED, &ipforwarding, 0, | |
171 | sysctl_ipforwarding, "I", "Enable IP forwarding between interfaces"); | |
1c79356b A |
172 | |
173 | static int ipsendredirects = 1; /* XXX */ | |
6d2010ae | 174 | SYSCTL_INT(_net_inet_ip, IPCTL_SENDREDIRECTS, redirect, CTLFLAG_RW | CTLFLAG_LOCKED, |
9bccf70c | 175 | &ipsendredirects, 0, "Enable sending IP redirects"); |
1c79356b A |
176 | |
177 | int ip_defttl = IPDEFTTL; | |
6d2010ae | 178 | SYSCTL_INT(_net_inet_ip, IPCTL_DEFTTL, ttl, CTLFLAG_RW | CTLFLAG_LOCKED, |
9bccf70c | 179 | &ip_defttl, 0, "Maximum TTL on IP packets"); |
1c79356b A |
180 | |
181 | static int ip_dosourceroute = 0; | |
6d2010ae | 182 | SYSCTL_INT(_net_inet_ip, IPCTL_SOURCEROUTE, sourceroute, CTLFLAG_RW | CTLFLAG_LOCKED, |
9bccf70c | 183 | &ip_dosourceroute, 0, "Enable forwarding source routed IP packets"); |
1c79356b A |
184 | |
185 | static int ip_acceptsourceroute = 0; | |
9bccf70c | 186 | SYSCTL_INT(_net_inet_ip, IPCTL_ACCEPTSOURCEROUTE, accept_sourceroute, |
6d2010ae | 187 | CTLFLAG_RW | CTLFLAG_LOCKED, &ip_acceptsourceroute, 0, |
9bccf70c | 188 | "Enable accepting source routed IP packets"); |
1c79356b A |
189 | |
190 | static int ip_keepfaith = 0; | |
6d2010ae | 191 | SYSCTL_INT(_net_inet_ip, IPCTL_KEEPFAITH, keepfaith, CTLFLAG_RW | CTLFLAG_LOCKED, |
9bccf70c A |
192 | &ip_keepfaith, 0, |
193 | "Enable packet capture for FAITH IPv4->IPv6 translater daemon"); | |
194 | ||
483a1d10 | 195 | static int nipq = 0; /* total # of reass queues */ |
91447636 | 196 | static int maxnipq; |
6d2010ae | 197 | SYSCTL_INT(_net_inet_ip, OID_AUTO, maxfragpackets, CTLFLAG_RW | CTLFLAG_LOCKED, |
483a1d10 | 198 | &maxnipq, 0, |
9bccf70c A |
199 | "Maximum number of IPv4 fragment reassembly queue entries"); |
200 | ||
483a1d10 | 201 | static int maxfragsperpacket; |
6d2010ae | 202 | SYSCTL_INT(_net_inet_ip, OID_AUTO, maxfragsperpacket, CTLFLAG_RW | CTLFLAG_LOCKED, |
483a1d10 A |
203 | &maxfragsperpacket, 0, |
204 | "Maximum number of IPv4 fragments allowed per packet"); | |
205 | ||
91447636 | 206 | static int maxfrags; |
6d2010ae | 207 | SYSCTL_INT(_net_inet_ip, OID_AUTO, maxfrags, CTLFLAG_RW | CTLFLAG_LOCKED, |
91447636 A |
208 | &maxfrags, 0, "Maximum number of IPv4 fragments allowed"); |
209 | ||
210 | static int currentfrags = 0; | |
211 | ||
c910b4d9 | 212 | int ip_doscopedroute = 1; |
6d2010ae | 213 | SYSCTL_INT(_net_inet_ip, OID_AUTO, scopedroute, CTLFLAG_RD | CTLFLAG_LOCKED, |
c910b4d9 A |
214 | &ip_doscopedroute, 0, "Enable IPv4 scoped routing"); |
215 | ||
9bccf70c A |
216 | /* |
217 | * XXX - Setting ip_checkinterface mostly implements the receive side of | |
218 | * the Strong ES model described in RFC 1122, but since the routing table | |
219 | * and transmit implementation do not implement the Strong ES model, | |
220 | * setting this to 1 results in an odd hybrid. | |
221 | * | |
222 | * XXX - ip_checkinterface currently must be disabled if you use ipnat | |
223 | * to translate the destination address to another local interface. | |
224 | * | |
225 | * XXX - ip_checkinterface must be disabled if you add IP aliases | |
226 | * to the loopback interface instead of the interface where the | |
227 | * packets for those addresses are received. | |
228 | */ | |
229 | static int ip_checkinterface = 0; | |
6d2010ae | 230 | SYSCTL_INT(_net_inet_ip, OID_AUTO, check_interface, CTLFLAG_RW | CTLFLAG_LOCKED, |
9bccf70c | 231 | &ip_checkinterface, 0, "Verify packet arrives on correct interface"); |
1c79356b | 232 | |
2d21ac55 | 233 | |
1c79356b A |
234 | #if DIAGNOSTIC |
235 | static int ipprintfs = 0; | |
236 | #endif | |
237 | ||
2d21ac55 | 238 | extern int in_proto_count; |
1c79356b A |
239 | extern struct domain inetdomain; |
240 | extern struct protosw inetsw[]; | |
241 | struct protosw *ip_protox[IPPROTO_MAX]; | |
242 | static int ipqmaxlen = IFQ_MAXLEN; | |
b0d623f7 A |
243 | |
244 | static lck_grp_attr_t *in_ifaddr_rwlock_grp_attr; | |
245 | static lck_grp_t *in_ifaddr_rwlock_grp; | |
246 | static lck_attr_t *in_ifaddr_rwlock_attr; | |
247 | lck_rw_t *in_ifaddr_rwlock; | |
248 | ||
249 | /* Protected by in_ifaddr_rwlock */ | |
250 | struct in_ifaddrhead in_ifaddrhead; /* first inet address */ | |
251 | struct in_ifaddrhashhead *in_ifaddrhashtbl; /* inet addr hash table */ | |
252 | ||
253 | #define INADDR_NHASH 61 | |
254 | static u_int32_t inaddr_nhash; /* hash table size */ | |
255 | static u_int32_t inaddr_hashp; /* next largest prime */ | |
256 | ||
1c79356b | 257 | struct ifqueue ipintrq; |
6d2010ae | 258 | SYSCTL_INT(_net_inet_ip, IPCTL_INTRQMAXLEN, intr_queue_maxlen, CTLFLAG_RW | CTLFLAG_LOCKED, |
9bccf70c | 259 | &ipintrq.ifq_maxlen, 0, "Maximum size of the IP input queue"); |
6d2010ae | 260 | SYSCTL_INT(_net_inet_ip, IPCTL_INTRQDROPS, intr_queue_drops, CTLFLAG_RD | CTLFLAG_LOCKED, |
9bccf70c | 261 | &ipintrq.ifq_drops, 0, "Number of packets dropped from the IP input queue"); |
1c79356b A |
262 | |
263 | struct ipstat ipstat; | |
6d2010ae | 264 | SYSCTL_STRUCT(_net_inet_ip, IPCTL_STATS, stats, CTLFLAG_RD | CTLFLAG_LOCKED, |
9bccf70c | 265 | &ipstat, ipstat, "IP statistics (struct ipstat, netinet/ip_var.h)"); |
1c79356b A |
266 | |
267 | /* Packet reassembly stuff */ | |
268 | #define IPREASS_NHASH_LOG2 6 | |
269 | #define IPREASS_NHASH (1 << IPREASS_NHASH_LOG2) | |
270 | #define IPREASS_HMASK (IPREASS_NHASH - 1) | |
271 | #define IPREASS_HASH(x,y) \ | |
9bccf70c | 272 | (((((x) & 0xF) | ((((x) >> 8) & 0xF) << 4)) ^ (y)) & IPREASS_HMASK) |
1c79356b A |
273 | |
274 | static struct ipq ipq[IPREASS_NHASH]; | |
91447636 A |
275 | static TAILQ_HEAD(ipq_list, ipq) ipq_list = |
276 | TAILQ_HEAD_INITIALIZER(ipq_list); | |
9bccf70c | 277 | const int ipintrq_present = 1; |
2d21ac55 | 278 | lck_mtx_t *ip_mutex; |
91447636 | 279 | lck_attr_t *ip_mutex_attr; |
2d21ac55 A |
280 | lck_grp_t *ip_mutex_grp; |
281 | lck_grp_attr_t *ip_mutex_grp_attr; | |
91447636 | 282 | lck_mtx_t *inet_domain_mutex; |
2d21ac55 | 283 | extern lck_mtx_t *domain_proto_mtx; |
1c79356b A |
284 | |
285 | #if IPCTL_DEFMTU | |
6d2010ae | 286 | SYSCTL_INT(_net_inet_ip, IPCTL_DEFMTU, mtu, CTLFLAG_RW | CTLFLAG_LOCKED, |
9bccf70c | 287 | &ip_mtu, 0, "Default MTU"); |
1c79356b A |
288 | #endif |
289 | ||
9bccf70c A |
290 | #if IPSTEALTH |
291 | static int ipstealth = 0; | |
6d2010ae | 292 | SYSCTL_INT(_net_inet_ip, OID_AUTO, stealth, CTLFLAG_RW | CTLFLAG_LOCKED, |
9bccf70c | 293 | &ipstealth, 0, ""); |
1c79356b A |
294 | #endif |
295 | ||
1c79356b A |
296 | |
297 | /* Firewall hooks */ | |
4a3eedf9 | 298 | #if IPFIREWALL |
1c79356b | 299 | ip_fw_chk_t *ip_fw_chk_ptr; |
2d21ac55 A |
300 | int fw_enable = 1; |
301 | int fw_bypass = 1; | |
302 | int fw_one_pass = 0; | |
1c79356b A |
303 | |
304 | #if DUMMYNET | |
91447636 | 305 | ip_dn_io_t *ip_dn_io_ptr; |
1c79356b A |
306 | #endif |
307 | ||
91447636 | 308 | int (*fr_checkp)(struct ip *, int, struct ifnet *, int, struct mbuf **) = NULL; |
4a3eedf9 | 309 | #endif /* IPFIREWALL */ |
9bccf70c | 310 | |
6d2010ae | 311 | SYSCTL_NODE(_net_inet_ip, OID_AUTO, linklocal, CTLFLAG_RW | CTLFLAG_LOCKED, 0, "link local"); |
9bccf70c A |
312 | |
313 | struct ip_linklocal_stat ip_linklocal_stat; | |
6d2010ae | 314 | SYSCTL_STRUCT(_net_inet_ip_linklocal, OID_AUTO, stat, CTLFLAG_RD | CTLFLAG_LOCKED, |
9bccf70c A |
315 | &ip_linklocal_stat, ip_linklocal_stat, |
316 | "Number of link local packets with TTL less than 255"); | |
317 | ||
6d2010ae | 318 | SYSCTL_NODE(_net_inet_ip_linklocal, OID_AUTO, in, CTLFLAG_RW | CTLFLAG_LOCKED, 0, "link local input"); |
9bccf70c | 319 | |
91447636 | 320 | int ip_linklocal_in_allowbadttl = 1; |
6d2010ae | 321 | SYSCTL_INT(_net_inet_ip_linklocal_in, OID_AUTO, allowbadttl, CTLFLAG_RW | CTLFLAG_LOCKED, |
9bccf70c A |
322 | &ip_linklocal_in_allowbadttl, 0, |
323 | "Allow incoming link local packets with TTL less than 255"); | |
324 | ||
1c79356b | 325 | |
1c79356b A |
326 | /* |
327 | * We need to save the IP options in case a protocol wants to respond | |
328 | * to an incoming packet over the same route if the packet got here | |
329 | * using IP source routing. This allows connection establishment and | |
330 | * maintenance when the remote end is on a network that is not known | |
331 | * to us. | |
332 | */ | |
333 | static int ip_nhops = 0; | |
334 | static struct ip_srcrt { | |
335 | struct in_addr dst; /* final destination */ | |
336 | char nop; /* one NOP to align */ | |
337 | char srcopt[IPOPT_OFFSET + 1]; /* OPTVAL, OLEN and OFFSET */ | |
338 | struct in_addr route[MAX_IPOPTLEN/sizeof(struct in_addr)]; | |
339 | } ip_srcrt; | |
340 | ||
b0d623f7 | 341 | static void in_ifaddrhashtbl_init(void); |
91447636 | 342 | static void save_rte(u_char *, struct in_addr); |
b0d623f7 A |
343 | static int ip_dooptions(struct mbuf *, int, struct sockaddr_in *); |
344 | static void ip_forward(struct mbuf *, int, struct sockaddr_in *); | |
91447636 | 345 | static void ip_freef(struct ipq *); |
9bccf70c A |
346 | #if IPDIVERT |
347 | #ifdef IPDIVERT_44 | |
91447636 A |
348 | static struct mbuf *ip_reass(struct mbuf *, |
349 | struct ipq *, struct ipq *, u_int32_t *, u_int16_t *); | |
9bccf70c | 350 | #else |
91447636 A |
351 | static struct mbuf *ip_reass(struct mbuf *, |
352 | struct ipq *, struct ipq *, u_int16_t *, u_int16_t *); | |
1c79356b | 353 | #endif |
9bccf70c | 354 | #else |
91447636 | 355 | static struct mbuf *ip_reass(struct mbuf *, struct ipq *, struct ipq *); |
9bccf70c | 356 | #endif |
b0d623f7 A |
357 | static void ip_fwd_route_copyout(struct ifnet *, struct route *); |
358 | static void ip_fwd_route_copyin(struct ifnet *, struct route *); | |
91447636 | 359 | void ipintr(void); |
2d21ac55 | 360 | void in_dinit(void); |
1c79356b | 361 | |
9bccf70c A |
362 | #if RANDOM_IP_ID |
363 | extern u_short ip_id; | |
2d21ac55 A |
364 | |
365 | int ip_use_randomid = 1; | |
6d2010ae | 366 | SYSCTL_INT(_net_inet_ip, OID_AUTO, random_id, CTLFLAG_RW | CTLFLAG_LOCKED, |
2d21ac55 | 367 | &ip_use_randomid, 0, "Randomize IP packets IDs"); |
9bccf70c | 368 | #endif |
1c79356b | 369 | |
b0d623f7 A |
370 | #define satosin(sa) ((struct sockaddr_in *)(sa)) |
371 | #define ifatoia(ifa) ((struct in_ifaddr *)(ifa)) | |
55e303ae | 372 | |
1c79356b A |
373 | /* |
374 | * IP initialization: fill in IP protocol switch table. | |
375 | * All protocols not implemented in kernel go to raw IP protocol handler. | |
376 | */ | |
377 | void | |
2d21ac55 | 378 | ip_init(void) |
1c79356b | 379 | { |
2d21ac55 A |
380 | struct protosw *pr; |
381 | int i; | |
382 | static int ip_initialized = 0; | |
91447636 | 383 | |
1c79356b A |
384 | if (!ip_initialized) |
385 | { | |
6d2010ae A |
386 | PE_parse_boot_argn("net.inet.ip.scopedroute", |
387 | &ip_doscopedroute, sizeof (ip_doscopedroute)); | |
388 | ||
b0d623f7 A |
389 | in_ifaddr_init(); |
390 | ||
391 | in_ifaddr_rwlock_grp_attr = lck_grp_attr_alloc_init(); | |
392 | in_ifaddr_rwlock_grp = lck_grp_alloc_init("in_ifaddr_rwlock", | |
393 | in_ifaddr_rwlock_grp_attr); | |
394 | in_ifaddr_rwlock_attr = lck_attr_alloc_init(); | |
395 | in_ifaddr_rwlock = lck_rw_alloc_init(in_ifaddr_rwlock_grp, | |
396 | in_ifaddr_rwlock_attr); | |
397 | ||
1c79356b | 398 | TAILQ_INIT(&in_ifaddrhead); |
b0d623f7 A |
399 | in_ifaddrhashtbl_init(); |
400 | ||
6d2010ae A |
401 | ip_moptions_init(); |
402 | ||
91447636 | 403 | pr = pffindproto_locked(PF_INET, IPPROTO_RAW, SOCK_RAW); |
1c79356b A |
404 | if (pr == 0) |
405 | panic("ip_init"); | |
406 | for (i = 0; i < IPPROTO_MAX; i++) | |
407 | ip_protox[i] = pr; | |
b0d623f7 A |
408 | for (pr = inetdomain.dom_protosw; pr; pr = pr->pr_next) { |
409 | if (pr->pr_domain == NULL) | |
410 | continue; /* If uninitialized, skip */ | |
1c79356b A |
411 | if (pr->pr_domain->dom_family == PF_INET && |
412 | pr->pr_protocol && pr->pr_protocol != IPPROTO_RAW) | |
413 | ip_protox[pr->pr_protocol] = pr; | |
414 | } | |
415 | for (i = 0; i < IPREASS_NHASH; i++) | |
416 | ipq[i].next = ipq[i].prev = &ipq[i]; | |
417 | ||
483a1d10 | 418 | maxnipq = nmbclusters / 32; |
91447636 A |
419 | maxfrags = maxnipq * 2; |
420 | maxfragsperpacket = 128; /* enough for 64k in 512 byte fragments */ | |
1c79356b | 421 | |
9bccf70c | 422 | #if RANDOM_IP_ID |
2d21ac55 A |
423 | { |
424 | struct timeval timenow; | |
425 | getmicrotime(&timenow); | |
426 | ip_id = timenow.tv_sec & 0xffff; | |
427 | } | |
1c79356b | 428 | #endif |
9bccf70c | 429 | ipintrq.ifq_maxlen = ipqmaxlen; |
91447636 A |
430 | |
431 | ipf_init(); | |
432 | ||
433 | ip_mutex_grp_attr = lck_grp_attr_alloc_init(); | |
91447636 A |
434 | |
435 | ip_mutex_grp = lck_grp_alloc_init("ip", ip_mutex_grp_attr); | |
436 | ||
437 | ip_mutex_attr = lck_attr_alloc_init(); | |
438 | ||
91447636 A |
439 | if ((ip_mutex = lck_mtx_alloc_init(ip_mutex_grp, ip_mutex_attr)) == NULL) { |
440 | printf("ip_init: can't alloc ip_mutex\n"); | |
441 | return; | |
442 | } | |
443 | ||
2d21ac55 A |
444 | #if IPSEC |
445 | ||
446 | sadb_stat_mutex_grp_attr = lck_grp_attr_alloc_init(); | |
447 | sadb_stat_mutex_grp = lck_grp_alloc_init("sadb_stat", sadb_stat_mutex_grp_attr); | |
448 | sadb_stat_mutex_attr = lck_attr_alloc_init(); | |
449 | ||
450 | if ((sadb_stat_mutex = lck_mtx_alloc_init(sadb_stat_mutex_grp, sadb_stat_mutex_attr)) == NULL) { | |
451 | printf("ip_init: can't alloc sadb_stat_mutex\n"); | |
452 | return; | |
453 | } | |
454 | ||
455 | #endif | |
b0d623f7 A |
456 | arp_init(); |
457 | ||
1c79356b A |
458 | ip_initialized = 1; |
459 | } | |
460 | } | |
461 | ||
b0d623f7 A |
462 | /* |
463 | * Initialize IPv4 source address hash table. | |
464 | */ | |
465 | static void | |
466 | in_ifaddrhashtbl_init(void) | |
467 | { | |
468 | int i, k, p; | |
469 | ||
470 | if (in_ifaddrhashtbl != NULL) | |
471 | return; | |
472 | ||
473 | PE_parse_boot_argn("inaddr_nhash", &inaddr_nhash, sizeof (inaddr_nhash)); | |
474 | if (inaddr_nhash == 0) | |
475 | inaddr_nhash = INADDR_NHASH; | |
476 | ||
477 | MALLOC(in_ifaddrhashtbl, struct in_ifaddrhashhead *, | |
478 | inaddr_nhash * sizeof (*in_ifaddrhashtbl), | |
479 | M_IFADDR, M_WAITOK | M_ZERO); | |
480 | if (in_ifaddrhashtbl == NULL) | |
481 | panic("in_ifaddrhashtbl_init allocation failed"); | |
482 | ||
483 | /* | |
484 | * Generate the next largest prime greater than inaddr_nhash. | |
485 | */ | |
486 | k = (inaddr_nhash % 2 == 0) ? inaddr_nhash + 1 : inaddr_nhash + 2; | |
487 | for (;;) { | |
488 | p = 1; | |
489 | for (i = 3; i * i <= k; i += 2) { | |
490 | if (k % i == 0) | |
491 | p = 0; | |
492 | } | |
493 | if (p == 1) | |
494 | break; | |
495 | k += 2; | |
496 | } | |
497 | inaddr_hashp = k; | |
498 | } | |
499 | ||
500 | u_int32_t | |
501 | inaddr_hashval(u_int32_t key) | |
502 | { | |
503 | /* | |
504 | * The hash index is the computed prime times the key modulo | |
505 | * the hash size, as documented in "Introduction to Algorithms" | |
506 | * (Cormen, Leiserson, Rivest). | |
507 | */ | |
508 | if (inaddr_nhash > 1) | |
509 | return ((key * inaddr_hashp) % inaddr_nhash); | |
510 | else | |
511 | return (0); | |
512 | } | |
513 | ||
91447636 A |
514 | static void |
515 | ip_proto_input( | |
2d21ac55 A |
516 | protocol_family_t __unused protocol, |
517 | mbuf_t packet_list) | |
91447636 | 518 | { |
2d21ac55 A |
519 | mbuf_t packet; |
520 | int how_many = 0 ; | |
521 | ||
522 | /* ip_input should handle a list of packets but does not yet */ | |
523 | ||
524 | for (packet = packet_list; packet; packet = packet_list) { | |
525 | how_many++; | |
526 | packet_list = mbuf_nextpkt(packet); | |
527 | mbuf_setnextpkt(packet, NULL); | |
528 | ip_input(packet); | |
529 | } | |
91447636 A |
530 | } |
531 | ||
1c79356b A |
532 | /* Initialize the PF_INET domain, and add in the pre-defined protos */ |
533 | void | |
2d21ac55 A |
534 | in_dinit(void) |
535 | { | |
536 | int i; | |
537 | struct protosw *pr; | |
538 | struct domain *dp; | |
539 | static int inetdomain_initted = 0; | |
1c79356b A |
540 | |
541 | if (!inetdomain_initted) | |
9bccf70c | 542 | { |
6d2010ae | 543 | /* kprintf("Initing %d protosw entries\n", in_proto_count); */ |
1c79356b | 544 | dp = &inetdomain; |
91447636 | 545 | dp->dom_flags = DOM_REENTRANT; |
1c79356b A |
546 | |
547 | for (i=0, pr = &inetsw[0]; i<in_proto_count; i++, pr++) | |
548 | net_add_proto(pr, dp); | |
91447636 | 549 | inet_domain_mutex = dp->dom_mtx; |
1c79356b | 550 | inetdomain_initted = 1; |
91447636 A |
551 | |
552 | lck_mtx_unlock(domain_proto_mtx); | |
2d21ac55 | 553 | proto_register_input(PF_INET, ip_proto_input, NULL, 1); |
91447636 | 554 | lck_mtx_lock(domain_proto_mtx); |
1c79356b A |
555 | } |
556 | } | |
557 | ||
91447636 A |
558 | __private_extern__ void |
559 | ip_proto_dispatch_in( | |
560 | struct mbuf *m, | |
561 | int hlen, | |
562 | u_int8_t proto, | |
563 | ipfilter_t inject_ipfref) | |
564 | { | |
565 | struct ipfilter *filter; | |
566 | int seen = (inject_ipfref == 0); | |
567 | int changed_header = 0; | |
568 | struct ip *ip; | |
0b4c1975 | 569 | void (*pr_input)(struct mbuf *, int len); |
91447636 A |
570 | |
571 | if (!TAILQ_EMPTY(&ipv4_filters)) { | |
572 | ipf_ref(); | |
573 | TAILQ_FOREACH(filter, &ipv4_filters, ipf_link) { | |
574 | if (seen == 0) { | |
575 | if ((struct ipfilter *)inject_ipfref == filter) | |
576 | seen = 1; | |
577 | } else if (filter->ipf_filter.ipf_input) { | |
578 | errno_t result; | |
579 | ||
580 | if (changed_header == 0) { | |
581 | changed_header = 1; | |
582 | ip = mtod(m, struct ip *); | |
583 | ip->ip_len = htons(ip->ip_len + hlen); | |
584 | ip->ip_off = htons(ip->ip_off); | |
585 | ip->ip_sum = 0; | |
586 | ip->ip_sum = in_cksum(m, hlen); | |
587 | } | |
588 | result = filter->ipf_filter.ipf_input( | |
589 | filter->ipf_filter.cookie, (mbuf_t*)&m, hlen, proto); | |
590 | if (result == EJUSTRETURN) { | |
591 | ipf_unref(); | |
592 | return; | |
593 | } | |
594 | if (result != 0) { | |
595 | ipf_unref(); | |
596 | m_freem(m); | |
597 | return; | |
598 | } | |
599 | } | |
600 | } | |
601 | ipf_unref(); | |
602 | } | |
603 | /* | |
604 | * If there isn't a specific lock for the protocol | |
605 | * we're about to call, use the generic lock for AF_INET. | |
606 | * otherwise let the protocol deal with its own locking | |
607 | */ | |
608 | ip = mtod(m, struct ip *); | |
0b4c1975 | 609 | |
91447636 A |
610 | if (changed_header) { |
611 | ip->ip_len = ntohs(ip->ip_len) - hlen; | |
612 | ip->ip_off = ntohs(ip->ip_off); | |
613 | } | |
0b4c1975 A |
614 | |
615 | if ((pr_input = ip_protox[ip->ip_p]->pr_input) == NULL) { | |
616 | m_freem(m); | |
617 | } else if (!(ip_protox[ip->ip_p]->pr_flags & PR_PROTOLOCK)) { | |
91447636 | 618 | lck_mtx_lock(inet_domain_mutex); |
0b4c1975 | 619 | pr_input(m, hlen); |
91447636 | 620 | lck_mtx_unlock(inet_domain_mutex); |
0b4c1975 A |
621 | } else { |
622 | pr_input(m, hlen); | |
623 | } | |
91447636 A |
624 | } |
625 | ||
1c79356b A |
626 | /* |
627 | * Ip input routine. Checksum and byte swap header. If fragmented | |
628 | * try to reassemble. Process options. Pass to next level. | |
629 | */ | |
630 | void | |
631 | ip_input(struct mbuf *m) | |
632 | { | |
633 | struct ip *ip; | |
634 | struct ipq *fp; | |
9bccf70c | 635 | struct in_ifaddr *ia = NULL; |
0b4c1975 | 636 | int hlen, checkif; |
1c79356b | 637 | u_short sum; |
9bccf70c | 638 | struct in_addr pkt_dst; |
4a3eedf9 | 639 | #if IPFIREWALL |
0b4c1975 A |
640 | int i; |
641 | u_int32_t div_info = 0; /* packet divert/tee info */ | |
91447636 | 642 | struct ip_fw_args args; |
0b4c1975 | 643 | struct m_tag *tag; |
4a3eedf9 | 644 | #endif |
91447636 | 645 | ipfilter_t inject_filter_ref = 0; |
b0d623f7 | 646 | |
6d2010ae A |
647 | /* Check if the mbuf is still valid after interface filter processing */ |
648 | MBUF_INPUT_CHECK(m, m->m_pkthdr.rcvif); | |
649 | ||
2d21ac55 | 650 | #if IPFIREWALL |
91447636 A |
651 | args.eh = NULL; |
652 | args.oif = NULL; | |
653 | args.rule = NULL; | |
654 | args.divert_rule = 0; /* divert cookie */ | |
655 | args.next_hop = NULL; | |
656 | ||
b0d623f7 A |
657 | /* |
658 | * Don't bother searching for tag(s) if there's none. | |
659 | */ | |
660 | if (SLIST_EMPTY(&m->m_pkthdr.tags)) | |
661 | goto ipfw_tags_done; | |
662 | ||
91447636 A |
663 | /* Grab info from mtags prepended to the chain */ |
664 | #if DUMMYNET | |
b0d623f7 A |
665 | if ((tag = m_tag_locate(m, KERNEL_MODULE_TAG_ID, |
666 | KERNEL_TAG_TYPE_DUMMYNET, NULL)) != NULL) { | |
91447636 | 667 | struct dn_pkt_tag *dn_tag; |
b0d623f7 | 668 | |
91447636 A |
669 | dn_tag = (struct dn_pkt_tag *)(tag+1); |
670 | args.rule = dn_tag->rule; | |
b0d623f7 | 671 | |
91447636 A |
672 | m_tag_delete(m, tag); |
673 | } | |
674 | #endif /* DUMMYNET */ | |
9bccf70c | 675 | |
4a3eedf9 | 676 | #if IPDIVERT |
b0d623f7 A |
677 | if ((tag = m_tag_locate(m, KERNEL_MODULE_TAG_ID, |
678 | KERNEL_TAG_TYPE_DIVERT, NULL)) != NULL) { | |
91447636 | 679 | struct divert_tag *div_tag; |
b0d623f7 | 680 | |
91447636 A |
681 | div_tag = (struct divert_tag *)(tag+1); |
682 | args.divert_rule = div_tag->cookie; | |
1c79356b | 683 | |
91447636 A |
684 | m_tag_delete(m, tag); |
685 | } | |
4a3eedf9 A |
686 | #endif |
687 | ||
b0d623f7 A |
688 | if ((tag = m_tag_locate(m, KERNEL_MODULE_TAG_ID, |
689 | KERNEL_TAG_TYPE_IPFORWARD, NULL)) != NULL) { | |
91447636 | 690 | struct ip_fwd_tag *ipfwd_tag; |
b0d623f7 | 691 | |
91447636 A |
692 | ipfwd_tag = (struct ip_fwd_tag *)(tag+1); |
693 | args.next_hop = ipfwd_tag->next_hop; | |
1c79356b | 694 | |
91447636 A |
695 | m_tag_delete(m, tag); |
696 | } | |
b0d623f7 | 697 | |
1c79356b A |
698 | #if DIAGNOSTIC |
699 | if (m == NULL || (m->m_flags & M_PKTHDR) == 0) | |
700 | panic("ip_input no HDR"); | |
701 | #endif | |
91447636 A |
702 | |
703 | if (args.rule) { /* dummynet already filtered us */ | |
b0d623f7 A |
704 | ip = mtod(m, struct ip *); |
705 | hlen = IP_VHL_HL(ip->ip_vhl) << 2; | |
706 | inject_filter_ref = ipf_get_inject_filter(m); | |
707 | goto iphack ; | |
91447636 | 708 | } |
b0d623f7 | 709 | ipfw_tags_done: |
2d21ac55 | 710 | #endif /* IPFIREWALL */ |
b0d623f7 | 711 | |
91447636 | 712 | /* |
b0d623f7 | 713 | * No need to proccess packet twice if we've already seen it. |
91447636 | 714 | */ |
b0d623f7 A |
715 | if (!SLIST_EMPTY(&m->m_pkthdr.tags)) |
716 | inject_filter_ref = ipf_get_inject_filter(m); | |
91447636 | 717 | if (inject_filter_ref != 0) { |
91447636 A |
718 | ip = mtod(m, struct ip *); |
719 | hlen = IP_VHL_HL(ip->ip_vhl) << 2; | |
6d2010ae A |
720 | |
721 | DTRACE_IP6(receive, struct mbuf *, m, struct inpcb *, NULL, | |
722 | struct ip *, ip, struct ifnet *, m->m_pkthdr.rcvif, | |
723 | struct ip *, ip, struct ip6_hdr *, NULL); | |
724 | ||
91447636 A |
725 | ip->ip_len = ntohs(ip->ip_len) - hlen; |
726 | ip->ip_off = ntohs(ip->ip_off); | |
727 | ip_proto_dispatch_in(m, hlen, ip->ip_p, inject_filter_ref); | |
728 | return; | |
729 | } | |
730 | ||
b0d623f7 | 731 | OSAddAtomic(1, &ipstat.ips_total); |
1c79356b A |
732 | |
733 | if (m->m_pkthdr.len < sizeof(struct ip)) | |
734 | goto tooshort; | |
735 | ||
736 | if (m->m_len < sizeof (struct ip) && | |
737 | (m = m_pullup(m, sizeof (struct ip))) == 0) { | |
b0d623f7 | 738 | OSAddAtomic(1, &ipstat.ips_toosmall); |
1c79356b A |
739 | return; |
740 | } | |
741 | ip = mtod(m, struct ip *); | |
742 | ||
743 | KERNEL_DEBUG(DBG_LAYER_BEG, ip->ip_dst.s_addr, | |
744 | ip->ip_src.s_addr, ip->ip_p, ip->ip_off, ip->ip_len); | |
745 | ||
746 | if (IP_VHL_V(ip->ip_vhl) != IPVERSION) { | |
b0d623f7 | 747 | OSAddAtomic(1, &ipstat.ips_badvers); |
1c79356b A |
748 | goto bad; |
749 | } | |
750 | ||
751 | hlen = IP_VHL_HL(ip->ip_vhl) << 2; | |
752 | if (hlen < sizeof(struct ip)) { /* minimum header length */ | |
b0d623f7 | 753 | OSAddAtomic(1, &ipstat.ips_badhlen); |
1c79356b A |
754 | goto bad; |
755 | } | |
756 | if (hlen > m->m_len) { | |
757 | if ((m = m_pullup(m, hlen)) == 0) { | |
b0d623f7 | 758 | OSAddAtomic(1, &ipstat.ips_badhlen); |
1c79356b A |
759 | return; |
760 | } | |
761 | ip = mtod(m, struct ip *); | |
762 | } | |
763 | ||
9bccf70c A |
764 | /* 127/8 must not appear on wire - RFC1122 */ |
765 | if ((ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET || | |
766 | (ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET) { | |
767 | if ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) { | |
b0d623f7 | 768 | OSAddAtomic(1, &ipstat.ips_badaddr); |
9bccf70c A |
769 | goto bad; |
770 | } | |
771 | } | |
772 | ||
773 | /* IPv4 Link-Local Addresses as defined in <draft-ietf-zeroconf-ipv4-linklocal-05.txt> */ | |
774 | if ((IN_LINKLOCAL(ntohl(ip->ip_dst.s_addr)) || | |
775 | IN_LINKLOCAL(ntohl(ip->ip_src.s_addr)))) { | |
776 | ip_linklocal_stat.iplls_in_total++; | |
777 | if (ip->ip_ttl != MAXTTL) { | |
b0d623f7 | 778 | OSAddAtomic(1, &ip_linklocal_stat.iplls_in_badttl); |
9bccf70c | 779 | /* Silently drop link local traffic with bad TTL */ |
91447636 | 780 | if (!ip_linklocal_in_allowbadttl) |
9bccf70c A |
781 | goto bad; |
782 | } | |
783 | } | |
4a249263 A |
784 | if ((IF_HWASSIST_CSUM_FLAGS(m->m_pkthdr.rcvif->if_hwassist) == 0) |
785 | || (apple_hwcksum_rx == 0) || | |
91447636 A |
786 | ((m->m_pkthdr.csum_flags & CSUM_TCP_SUM16) && ip->ip_p != IPPROTO_TCP)) { |
787 | m->m_pkthdr.csum_flags = 0; /* invalidate HW generated checksum flags */ | |
788 | } | |
1c79356b | 789 | |
9bccf70c A |
790 | if (m->m_pkthdr.csum_flags & CSUM_IP_CHECKED) { |
791 | sum = !(m->m_pkthdr.csum_flags & CSUM_IP_VALID); | |
2d21ac55 A |
792 | } else if (!(m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) || |
793 | apple_hwcksum_tx == 0) { | |
794 | /* | |
795 | * Either this is not loopback packet coming from an interface | |
796 | * that does not support checksum offloading, or it is loopback | |
797 | * packet that has undergone software checksumming at the send | |
798 | * side because apple_hwcksum_tx was set to 0. In this case, | |
799 | * calculate the checksum in software to validate the packet. | |
800 | */ | |
9bccf70c | 801 | sum = in_cksum(m, hlen); |
2d21ac55 A |
802 | } else { |
803 | /* | |
804 | * This is a loopback packet without any valid checksum since | |
805 | * the send side has bypassed it (apple_hwcksum_tx set to 1). | |
806 | * We get here because apple_hwcksum_rx was set to 0, and so | |
807 | * we pretend that all is well. | |
808 | */ | |
809 | sum = 0; | |
810 | m->m_pkthdr.csum_flags |= CSUM_DATA_VALID | CSUM_PSEUDO_HDR | | |
811 | CSUM_IP_CHECKED | CSUM_IP_VALID; | |
812 | m->m_pkthdr.csum_data = 0xffff; | |
9bccf70c | 813 | } |
1c79356b | 814 | if (sum) { |
b0d623f7 | 815 | OSAddAtomic(1, &ipstat.ips_badsum); |
1c79356b A |
816 | goto bad; |
817 | } | |
818 | ||
6d2010ae A |
819 | DTRACE_IP6(receive, struct mbuf *, m, struct inpcb *, NULL, |
820 | struct ip *, ip, struct ifnet *, m->m_pkthdr.rcvif, | |
821 | struct ip *, ip, struct ip6_hdr *, NULL); | |
822 | ||
823 | /* | |
824 | * Naively assume we can attribute inbound data to the route we would | |
825 | * use to send to this destination. Asymetric routing breaks this | |
826 | * assumption, but it still allows us to account for traffic from | |
827 | * a remote node in the routing table. | |
828 | * this has a very significant performance impact so we bypass | |
829 | * if nstat_collect is disabled. We may also bypass if the | |
830 | * protocol is tcp in the future because tcp will have a route that | |
831 | * we can use to attribute the data to. That does mean we would not | |
832 | * account for forwarded tcp traffic. | |
833 | */ | |
834 | if (nstat_collect) { | |
835 | struct rtentry *rt = | |
836 | ifnet_cached_rtlookup_inet(m->m_pkthdr.rcvif, ip->ip_src); | |
837 | if (rt != NULL) { | |
838 | nstat_route_rx(rt, 1, m->m_pkthdr.len, 0); | |
839 | rtfree(rt); | |
840 | } | |
841 | } | |
842 | ||
1c79356b A |
843 | /* |
844 | * Convert fields to host representation. | |
845 | */ | |
b0d623f7 | 846 | #if BYTE_ORDER != BIG_ENDIAN |
1c79356b | 847 | NTOHS(ip->ip_len); |
b0d623f7 A |
848 | #endif |
849 | ||
1c79356b | 850 | if (ip->ip_len < hlen) { |
b0d623f7 | 851 | OSAddAtomic(1, &ipstat.ips_badlen); |
1c79356b A |
852 | goto bad; |
853 | } | |
1c79356b | 854 | |
b0d623f7 A |
855 | #if BYTE_ORDER != BIG_ENDIAN |
856 | NTOHS(ip->ip_off); | |
857 | #endif | |
1c79356b A |
858 | /* |
859 | * Check that the amount of data in the buffers | |
860 | * is as at least much as the IP header would have us expect. | |
861 | * Trim mbufs if longer than we expect. | |
862 | * Drop packet if shorter than we expect. | |
863 | */ | |
864 | if (m->m_pkthdr.len < ip->ip_len) { | |
865 | tooshort: | |
b0d623f7 | 866 | OSAddAtomic(1, &ipstat.ips_tooshort); |
1c79356b A |
867 | goto bad; |
868 | } | |
869 | if (m->m_pkthdr.len > ip->ip_len) { | |
765c9de3 A |
870 | /* Invalidate hwcksuming */ |
871 | m->m_pkthdr.csum_flags = 0; | |
872 | m->m_pkthdr.csum_data = 0; | |
873 | ||
1c79356b A |
874 | if (m->m_len == m->m_pkthdr.len) { |
875 | m->m_len = ip->ip_len; | |
876 | m->m_pkthdr.len = ip->ip_len; | |
877 | } else | |
878 | m_adj(m, ip->ip_len - m->m_pkthdr.len); | |
879 | } | |
9bccf70c | 880 | |
b0d623f7 A |
881 | #if PF |
882 | /* Invoke inbound packet filter */ | |
6d2010ae A |
883 | if (PF_IS_ENABLED) { |
884 | int error; | |
885 | error = pf_af_hook(m->m_pkthdr.rcvif, NULL, &m, AF_INET, TRUE); | |
886 | if (error != 0) { | |
887 | if (m != NULL) { | |
888 | panic("%s: unexpected packet %p\n", __func__, m); | |
889 | /* NOTREACHED */ | |
890 | } | |
891 | /* Already freed by callee */ | |
892 | return; | |
893 | } | |
894 | ip = mtod(m, struct ip *); | |
895 | hlen = IP_VHL_HL(ip->ip_vhl) << 2; | |
b0d623f7 | 896 | } |
b0d623f7 | 897 | #endif /* PF */ |
1c79356b | 898 | |
6d2010ae A |
899 | #if IPSEC |
900 | if (ipsec_bypass == 0 && ipsec_gethist(m, NULL)) | |
901 | goto pass; | |
902 | #endif | |
903 | ||
2d21ac55 A |
904 | #if IPFIREWALL |
905 | #if DUMMYNET | |
1c79356b | 906 | iphack: |
2d21ac55 | 907 | #endif /* DUMMYNET */ |
9bccf70c A |
908 | /* |
909 | * Check if we want to allow this packet to be processed. | |
910 | * Consider it to be bad if not. | |
911 | */ | |
912 | if (fr_checkp) { | |
913 | struct mbuf *m1 = m; | |
914 | ||
3a60a9f5 | 915 | if (fr_checkp(ip, hlen, m->m_pkthdr.rcvif, 0, &m1) || !m1) { |
9bccf70c | 916 | return; |
3a60a9f5 | 917 | } |
9bccf70c A |
918 | ip = mtod(m = m1, struct ip *); |
919 | } | |
91447636 | 920 | if (fw_enable && IPFW_LOADED) { |
1c79356b A |
921 | #if IPFIREWALL_FORWARD |
922 | /* | |
923 | * If we've been forwarded from the output side, then | |
924 | * skip the firewall a second time | |
925 | */ | |
91447636 | 926 | if (args.next_hop) |
1c79356b A |
927 | goto ours; |
928 | #endif /* IPFIREWALL_FORWARD */ | |
91447636 A |
929 | |
930 | args.m = m; | |
3a60a9f5 | 931 | |
91447636 A |
932 | i = ip_fw_chk_ptr(&args); |
933 | m = args.m; | |
934 | ||
9bccf70c | 935 | if ( (i & IP_FW_PORT_DENY_FLAG) || m == NULL) { /* drop */ |
91447636 | 936 | if (m) |
3a60a9f5 | 937 | m_freem(m); |
9bccf70c | 938 | return; |
91447636 | 939 | } |
9bccf70c | 940 | ip = mtod(m, struct ip *); /* just in case m changed */ |
2d21ac55 | 941 | |
3a60a9f5 | 942 | if (i == 0 && args.next_hop == NULL) { /* common case */ |
9bccf70c | 943 | goto pass; |
3a60a9f5 | 944 | } |
1c79356b | 945 | #if DUMMYNET |
91447636 A |
946 | if (DUMMYNET_LOADED && (i & IP_FW_PORT_DYNT_FLAG) != 0) { |
947 | /* Send packet to the appropriate pipe */ | |
91447636 | 948 | ip_dn_io_ptr(m, i&0xffff, DN_TO_IP_IN, &args); |
9bccf70c | 949 | return; |
1c79356b | 950 | } |
91447636 | 951 | #endif /* DUMMYNET */ |
1c79356b | 952 | #if IPDIVERT |
9bccf70c A |
953 | if (i != 0 && (i & IP_FW_PORT_DYNT_FLAG) == 0) { |
954 | /* Divert or tee packet */ | |
91447636 | 955 | div_info = i; |
1c79356b A |
956 | goto ours; |
957 | } | |
958 | #endif | |
959 | #if IPFIREWALL_FORWARD | |
3a60a9f5 | 960 | if (i == 0 && args.next_hop != NULL) { |
9bccf70c | 961 | goto pass; |
3a60a9f5 | 962 | } |
1c79356b A |
963 | #endif |
964 | /* | |
965 | * if we get here, the packet must be dropped | |
966 | */ | |
1c79356b | 967 | m_freem(m); |
9bccf70c | 968 | return; |
1c79356b | 969 | } |
2d21ac55 | 970 | #endif /* IPFIREWALL */ |
9bccf70c | 971 | pass: |
1c79356b A |
972 | |
973 | /* | |
974 | * Process options and, if not destined for us, | |
975 | * ship it on. ip_dooptions returns 1 when an | |
976 | * error was detected (causing an icmp message | |
977 | * to be sent and the original packet to be freed). | |
978 | */ | |
979 | ip_nhops = 0; /* for source routed packets */ | |
4a3eedf9 | 980 | #if IPFIREWALL |
b0d623f7 | 981 | if (hlen > sizeof (struct ip) && ip_dooptions(m, 0, args.next_hop)) { |
4a3eedf9 | 982 | #else |
b0d623f7 | 983 | if (hlen > sizeof (struct ip) && ip_dooptions(m, 0, NULL)) { |
4a3eedf9 | 984 | #endif |
1c79356b A |
985 | return; |
986 | } | |
987 | ||
988 | /* greedy RSVP, snatches any PATH packet of the RSVP protocol and no | |
989 | * matter if it is destined to another node, or whether it is | |
990 | * a multicast one, RSVP wants it! and prevents it from being forwarded | |
991 | * anywhere else. Also checks if the rsvp daemon is running before | |
992 | * grabbing the packet. | |
993 | */ | |
994 | if (rsvp_on && ip->ip_p==IPPROTO_RSVP) | |
995 | goto ours; | |
996 | ||
997 | /* | |
998 | * Check our list of addresses, to see if the packet is for us. | |
9bccf70c A |
999 | * If we don't have any addresses, assume any unicast packet |
1000 | * we receive might be for us (and let the upper layers deal | |
1001 | * with it). | |
1c79356b | 1002 | */ |
9bccf70c A |
1003 | if (TAILQ_EMPTY(&in_ifaddrhead) && |
1004 | (m->m_flags & (M_MCAST|M_BCAST)) == 0) | |
1005 | goto ours; | |
1c79356b | 1006 | |
9bccf70c A |
1007 | /* |
1008 | * Cache the destination address of the packet; this may be | |
1009 | * changed by use of 'ipfw fwd'. | |
1010 | */ | |
4a3eedf9 | 1011 | #if IPFIREWALL |
91447636 A |
1012 | pkt_dst = args.next_hop == NULL ? |
1013 | ip->ip_dst : args.next_hop->sin_addr; | |
4a3eedf9 A |
1014 | #else |
1015 | pkt_dst = ip->ip_dst; | |
1016 | #endif | |
9bccf70c A |
1017 | |
1018 | /* | |
1019 | * Enable a consistency check between the destination address | |
1020 | * and the arrival interface for a unicast packet (the RFC 1122 | |
1021 | * strong ES model) if IP forwarding is disabled and the packet | |
1022 | * is not locally generated and the packet is not subject to | |
1023 | * 'ipfw fwd'. | |
1024 | * | |
1025 | * XXX - Checking also should be disabled if the destination | |
1026 | * address is ipnat'ed to a different interface. | |
1027 | * | |
1028 | * XXX - Checking is incompatible with IP aliases added | |
1029 | * to the loopback interface instead of the interface where | |
1030 | * the packets are received. | |
1031 | */ | |
1032 | checkif = ip_checkinterface && (ipforwarding == 0) && | |
4a3eedf9 A |
1033 | ((m->m_pkthdr.rcvif->if_flags & IFF_LOOPBACK) == 0) |
1034 | #if IPFIREWALL | |
1035 | && (args.next_hop == NULL); | |
1036 | #else | |
1037 | ; | |
1038 | #endif | |
9bccf70c | 1039 | |
b0d623f7 A |
1040 | /* |
1041 | * Check for exact addresses in the hash bucket. | |
1042 | */ | |
1043 | lck_rw_lock_shared(in_ifaddr_rwlock); | |
1044 | TAILQ_FOREACH(ia, INADDR_HASH(pkt_dst.s_addr), ia_hash) { | |
1c79356b | 1045 | /* |
9bccf70c A |
1046 | * If the address matches, verify that the packet |
1047 | * arrived via the correct interface if checking is | |
1048 | * enabled. | |
1c79356b | 1049 | */ |
6d2010ae | 1050 | IFA_LOCK_SPIN(&ia->ia_ifa); |
9bccf70c | 1051 | if (IA_SIN(ia)->sin_addr.s_addr == pkt_dst.s_addr && |
91447636 | 1052 | (!checkif || ia->ia_ifp == m->m_pkthdr.rcvif)) { |
6d2010ae | 1053 | IFA_UNLOCK(&ia->ia_ifa); |
b0d623f7 | 1054 | lck_rw_done(in_ifaddr_rwlock); |
1c79356b | 1055 | goto ours; |
91447636 | 1056 | } |
6d2010ae | 1057 | IFA_UNLOCK(&ia->ia_ifa); |
b0d623f7 A |
1058 | } |
1059 | lck_rw_done(in_ifaddr_rwlock); | |
1060 | ||
1061 | /* | |
1062 | * Check for broadcast addresses. | |
1063 | * | |
1064 | * Only accept broadcast packets that arrive via the matching | |
1065 | * interface. Reception of forwarded directed broadcasts would be | |
1066 | * handled via ip_forward() and ether_frameout() with the loopback | |
1067 | * into the stack for SIMPLEX interfaces handled by ether_frameout(). | |
1068 | */ | |
1069 | if (m->m_pkthdr.rcvif->if_flags & IFF_BROADCAST) { | |
1070 | struct ifaddr *ifa; | |
1071 | struct ifnet *ifp = m->m_pkthdr.rcvif; | |
1072 | ||
1073 | ifnet_lock_shared(ifp); | |
1074 | TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) { | |
6d2010ae A |
1075 | IFA_LOCK_SPIN(ifa); |
1076 | if (ifa->ifa_addr->sa_family != AF_INET) { | |
1077 | IFA_UNLOCK(ifa); | |
b0d623f7 | 1078 | continue; |
6d2010ae | 1079 | } |
b0d623f7 | 1080 | ia = ifatoia(ifa); |
1c79356b | 1081 | if (satosin(&ia->ia_broadaddr)->sin_addr.s_addr == |
b0d623f7 | 1082 | pkt_dst.s_addr || ia->ia_netbroadcast.s_addr == |
91447636 | 1083 | pkt_dst.s_addr) { |
6d2010ae | 1084 | IFA_UNLOCK(ifa); |
b0d623f7 | 1085 | ifnet_lock_done(ifp); |
1c79356b | 1086 | goto ours; |
91447636 | 1087 | } |
6d2010ae | 1088 | IFA_UNLOCK(ifa); |
1c79356b | 1089 | } |
b0d623f7 | 1090 | ifnet_lock_done(ifp); |
1c79356b | 1091 | } |
b0d623f7 | 1092 | |
1c79356b A |
1093 | if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) { |
1094 | struct in_multi *inm; | |
b0d623f7 | 1095 | struct ifnet *ifp = m->m_pkthdr.rcvif; |
2d21ac55 | 1096 | #if MROUTING |
1c79356b A |
1097 | if (ip_mrouter) { |
1098 | /* | |
1099 | * If we are acting as a multicast router, all | |
1100 | * incoming multicast packets are passed to the | |
1101 | * kernel-level multicast forwarding function. | |
1102 | * The packet is returned (relatively) intact; if | |
1103 | * ip_mforward() returns a non-zero value, the packet | |
1104 | * must be discarded, else it may be accepted below. | |
1c79356b | 1105 | */ |
2d21ac55 | 1106 | lck_mtx_lock(ip_mutex); |
b0d623f7 A |
1107 | if (ip_mforward && ip_mforward(ip, ifp, m, 0) != 0) { |
1108 | OSAddAtomic(1, &ipstat.ips_cantforward); | |
1c79356b | 1109 | m_freem(m); |
91447636 | 1110 | lck_mtx_unlock(ip_mutex); |
1c79356b A |
1111 | return; |
1112 | } | |
1c79356b A |
1113 | |
1114 | /* | |
55e303ae | 1115 | * The process-level routing daemon needs to receive |
1c79356b A |
1116 | * all multicast IGMP packets, whether or not this |
1117 | * host belongs to their destination groups. | |
1118 | */ | |
1119 | if (ip->ip_p == IPPROTO_IGMP) | |
1120 | goto ours; | |
b0d623f7 | 1121 | OSAddAtomic(1, &ipstat.ips_forward); |
1c79356b | 1122 | } |
2d21ac55 | 1123 | #endif /* MROUTING */ |
1c79356b A |
1124 | /* |
1125 | * See if we belong to the destination multicast group on the | |
1126 | * arrival interface. | |
1127 | */ | |
6d2010ae A |
1128 | in_multihead_lock_shared(); |
1129 | IN_LOOKUP_MULTI(&ip->ip_dst, ifp, inm); | |
1130 | in_multihead_lock_done(); | |
1c79356b | 1131 | if (inm == NULL) { |
b0d623f7 | 1132 | OSAddAtomic(1, &ipstat.ips_notmember); |
1c79356b A |
1133 | m_freem(m); |
1134 | return; | |
1135 | } | |
6d2010ae | 1136 | INM_REMREF(inm); |
1c79356b A |
1137 | goto ours; |
1138 | } | |
b0d623f7 | 1139 | if (ip->ip_dst.s_addr == (u_int32_t)INADDR_BROADCAST) |
1c79356b A |
1140 | goto ours; |
1141 | if (ip->ip_dst.s_addr == INADDR_ANY) | |
1142 | goto ours; | |
1143 | ||
9bccf70c A |
1144 | /* Allow DHCP/BootP responses through */ |
1145 | if (m->m_pkthdr.rcvif != NULL | |
0b4e3aa0 | 1146 | && (m->m_pkthdr.rcvif->if_eflags & IFEF_AUTOCONFIGURING) |
9bccf70c | 1147 | && hlen == sizeof(struct ip) |
0b4e3aa0 | 1148 | && ip->ip_p == IPPROTO_UDP) { |
9bccf70c A |
1149 | struct udpiphdr *ui; |
1150 | if (m->m_len < sizeof(struct udpiphdr) | |
1151 | && (m = m_pullup(m, sizeof(struct udpiphdr))) == 0) { | |
b0d623f7 | 1152 | OSAddAtomic(1, &udpstat.udps_hdrops); |
9bccf70c A |
1153 | return; |
1154 | } | |
1155 | ui = mtod(m, struct udpiphdr *); | |
1156 | if (ntohs(ui->ui_dport) == IPPORT_BOOTPC) { | |
1157 | goto ours; | |
1158 | } | |
1159 | ip = mtod(m, struct ip *); /* in case it changed */ | |
0b4e3aa0 A |
1160 | } |
1161 | ||
9bccf70c | 1162 | #if defined(NFAITH) && 0 < NFAITH |
1c79356b A |
1163 | /* |
1164 | * FAITH(Firewall Aided Internet Translator) | |
1165 | */ | |
1166 | if (m->m_pkthdr.rcvif && m->m_pkthdr.rcvif->if_type == IFT_FAITH) { | |
1167 | if (ip_keepfaith) { | |
1168 | if (ip->ip_p == IPPROTO_TCP || ip->ip_p == IPPROTO_ICMP) | |
1169 | goto ours; | |
1170 | } | |
1171 | m_freem(m); | |
1172 | return; | |
1173 | } | |
1174 | #endif | |
1175 | /* | |
1176 | * Not for us; forward if possible and desirable. | |
1177 | */ | |
1178 | if (ipforwarding == 0) { | |
b0d623f7 | 1179 | OSAddAtomic(1, &ipstat.ips_cantforward); |
1c79356b | 1180 | m_freem(m); |
91447636 | 1181 | } else { |
4a3eedf9 | 1182 | #if IPFIREWALL |
b0d623f7 | 1183 | ip_forward(m, 0, args.next_hop); |
4a3eedf9 | 1184 | #else |
b0d623f7 | 1185 | ip_forward(m, 0, NULL); |
4a3eedf9 | 1186 | #endif |
91447636 | 1187 | } |
1c79356b A |
1188 | return; |
1189 | ||
1190 | ours: | |
1c79356b A |
1191 | /* |
1192 | * If offset or IP_MF are set, must reassemble. | |
1193 | * Otherwise, nothing need be done. | |
1194 | * (We could look in the reassembly queue to see | |
1195 | * if the packet was previously fragmented, | |
1196 | * but it's not worth the time; just let them time out.) | |
1197 | */ | |
1198 | if (ip->ip_off & (IP_MF | IP_OFFMASK | IP_RF)) { | |
9bccf70c | 1199 | |
483a1d10 A |
1200 | /* If maxnipq is 0, never accept fragments. */ |
1201 | if (maxnipq == 0) { | |
2d21ac55 | 1202 | |
b0d623f7 A |
1203 | OSAddAtomic(1, &ipstat.ips_fragments); |
1204 | OSAddAtomic(1, &ipstat.ips_fragdropped); | |
483a1d10 | 1205 | goto bad; |
91447636 A |
1206 | } |
1207 | ||
1208 | /* | |
1209 | * If we will exceed the number of fragments in queues, timeout the | |
1210 | * oldest fragemented packet to make space. | |
1211 | */ | |
2d21ac55 | 1212 | lck_mtx_lock(ip_mutex); |
91447636 A |
1213 | if (currentfrags >= maxfrags) { |
1214 | fp = TAILQ_LAST(&ipq_list, ipq_list); | |
b0d623f7 | 1215 | OSAddAtomic(fp->ipq_nfrags, &ipstat.ips_fragtimeout); |
91447636 A |
1216 | |
1217 | if (ip->ip_id == fp->ipq_id && | |
1218 | ip->ip_src.s_addr == fp->ipq_src.s_addr && | |
1219 | ip->ip_dst.s_addr == fp->ipq_dst.s_addr && | |
1220 | ip->ip_p == fp->ipq_p) { | |
1221 | /* | |
1222 | * If we match the fragment queue we were going to | |
1223 | * discard, drop this packet too. | |
1224 | */ | |
b0d623f7 | 1225 | OSAddAtomic(1, &ipstat.ips_fragdropped); |
91447636 | 1226 | ip_freef(fp); |
2d21ac55 | 1227 | lck_mtx_unlock(ip_mutex); |
91447636 | 1228 | goto bad; |
1c79356b | 1229 | } |
91447636 A |
1230 | |
1231 | ip_freef(fp); | |
1232 | } | |
483a1d10 | 1233 | |
1c79356b A |
1234 | sum = IPREASS_HASH(ip->ip_src.s_addr, ip->ip_id); |
1235 | /* | |
1236 | * Look for queue of fragments | |
1237 | * of this datagram. | |
1238 | */ | |
1239 | for (fp = ipq[sum].next; fp != &ipq[sum]; fp = fp->next) | |
1240 | if (ip->ip_id == fp->ipq_id && | |
1241 | ip->ip_src.s_addr == fp->ipq_src.s_addr && | |
1242 | ip->ip_dst.s_addr == fp->ipq_dst.s_addr && | |
2d21ac55 A |
1243 | #if CONFIG_MACF_NET |
1244 | mac_ipq_label_compare(m, fp) && | |
1245 | #endif | |
1c79356b A |
1246 | ip->ip_p == fp->ipq_p) |
1247 | goto found; | |
1248 | ||
483a1d10 A |
1249 | /* |
1250 | * Enforce upper bound on number of fragmented packets | |
1251 | * for which we attempt reassembly; | |
1252 | * If maxnipq is -1, accept all fragments without limitation. | |
1253 | */ | |
1254 | if ((nipq > maxnipq) && (maxnipq > 0)) { | |
1c79356b | 1255 | /* |
91447636 | 1256 | * drop the oldest fragment before proceeding further |
1c79356b | 1257 | */ |
91447636 | 1258 | fp = TAILQ_LAST(&ipq_list, ipq_list); |
b0d623f7 | 1259 | OSAddAtomic(fp->ipq_nfrags, &ipstat.ips_fragtimeout); |
91447636 | 1260 | ip_freef(fp); |
483a1d10 | 1261 | } |
91447636 A |
1262 | |
1263 | fp = NULL; | |
1264 | ||
1c79356b A |
1265 | found: |
1266 | /* | |
1267 | * Adjust ip_len to not reflect header, | |
1c79356b A |
1268 | * convert offset of this to bytes. |
1269 | */ | |
1270 | ip->ip_len -= hlen; | |
483a1d10 | 1271 | if (ip->ip_off & IP_MF) { |
1c79356b A |
1272 | /* |
1273 | * Make sure that fragments have a data length | |
91447636 | 1274 | * that's a non-zero multiple of 8 bytes. |
1c79356b A |
1275 | */ |
1276 | if (ip->ip_len == 0 || (ip->ip_len & 0x7) != 0) { | |
b0d623f7 | 1277 | OSAddAtomic(1, &ipstat.ips_toosmall); |
2d21ac55 | 1278 | lck_mtx_unlock(ip_mutex); |
1c79356b A |
1279 | goto bad; |
1280 | } | |
1281 | m->m_flags |= M_FRAG; | |
91447636 A |
1282 | } else { |
1283 | /* Clear the flag in case packet comes from loopback */ | |
55e303ae | 1284 | m->m_flags &= ~M_FRAG; |
91447636 | 1285 | } |
1c79356b A |
1286 | ip->ip_off <<= 3; |
1287 | ||
1288 | /* | |
483a1d10 A |
1289 | * Attempt reassembly; if it succeeds, proceed. |
1290 | * ip_reass() will return a different mbuf, and update | |
91447636 | 1291 | * the divert info in div_info and args.divert_rule. |
1c79356b | 1292 | */ |
b0d623f7 | 1293 | OSAddAtomic(1, &ipstat.ips_fragments); |
1c79356b | 1294 | m->m_pkthdr.header = ip; |
9bccf70c | 1295 | #if IPDIVERT |
b0d623f7 A |
1296 | m = ip_reass(m, fp, &ipq[sum], |
1297 | (u_int16_t *)&div_info, &args.divert_rule); | |
9bccf70c A |
1298 | #else |
1299 | m = ip_reass(m, fp, &ipq[sum]); | |
1300 | #endif | |
1301 | if (m == 0) { | |
91447636 | 1302 | lck_mtx_unlock(ip_mutex); |
1c79356b A |
1303 | return; |
1304 | } | |
b0d623f7 | 1305 | OSAddAtomic(1, &ipstat.ips_reassembled); |
9bccf70c A |
1306 | ip = mtod(m, struct ip *); |
1307 | /* Get the header length of the reassembled packet */ | |
1308 | hlen = IP_VHL_HL(ip->ip_vhl) << 2; | |
b0d623f7 | 1309 | |
1c79356b | 1310 | #if IPDIVERT |
9bccf70c | 1311 | /* Restore original checksum before diverting packet */ |
91447636 | 1312 | if (div_info != 0) { |
1c79356b | 1313 | ip->ip_len += hlen; |
b0d623f7 A |
1314 | |
1315 | #if BYTE_ORDER != BIG_ENDIAN | |
1c79356b A |
1316 | HTONS(ip->ip_len); |
1317 | HTONS(ip->ip_off); | |
b0d623f7 A |
1318 | #endif |
1319 | ||
1c79356b | 1320 | ip->ip_sum = 0; |
9bccf70c | 1321 | ip->ip_sum = in_cksum(m, hlen); |
b0d623f7 A |
1322 | |
1323 | #if BYTE_ORDER != BIG_ENDIAN | |
1c79356b A |
1324 | NTOHS(ip->ip_off); |
1325 | NTOHS(ip->ip_len); | |
b0d623f7 A |
1326 | #endif |
1327 | ||
1c79356b A |
1328 | ip->ip_len -= hlen; |
1329 | } | |
1330 | #endif | |
2d21ac55 | 1331 | lck_mtx_unlock(ip_mutex); |
1c79356b | 1332 | } else |
1c79356b A |
1333 | ip->ip_len -= hlen; |
1334 | ||
1335 | #if IPDIVERT | |
1336 | /* | |
9bccf70c A |
1337 | * Divert or tee packet to the divert protocol if required. |
1338 | * | |
91447636 | 1339 | * If div_info is zero then cookie should be too, so we shouldn't |
9bccf70c | 1340 | * need to clear them here. Assume divert_packet() does so also. |
1c79356b | 1341 | */ |
91447636 | 1342 | if (div_info != 0) { |
9bccf70c A |
1343 | struct mbuf *clone = NULL; |
1344 | ||
1345 | /* Clone packet if we're doing a 'tee' */ | |
91447636 | 1346 | if ((div_info & IP_FW_PORT_TEE_FLAG) != 0) |
9bccf70c A |
1347 | clone = m_dup(m, M_DONTWAIT); |
1348 | ||
1349 | /* Restore packet header fields to original values */ | |
1350 | ip->ip_len += hlen; | |
b0d623f7 A |
1351 | |
1352 | #if BYTE_ORDER != BIG_ENDIAN | |
9bccf70c A |
1353 | HTONS(ip->ip_len); |
1354 | HTONS(ip->ip_off); | |
b0d623f7 | 1355 | #endif |
9bccf70c | 1356 | /* Deliver packet to divert input routine */ |
b0d623f7 | 1357 | OSAddAtomic(1, &ipstat.ips_delivered); |
91447636 | 1358 | divert_packet(m, 1, div_info & 0xffff, args.divert_rule); |
9bccf70c A |
1359 | |
1360 | /* If 'tee', continue with original packet */ | |
91447636 | 1361 | if (clone == NULL) { |
9bccf70c | 1362 | return; |
91447636 | 1363 | } |
9bccf70c A |
1364 | m = clone; |
1365 | ip = mtod(m, struct ip *); | |
1c79356b | 1366 | } |
9bccf70c | 1367 | #endif |
1c79356b | 1368 | |
9bccf70c A |
1369 | #if IPSEC |
1370 | /* | |
1371 | * enforce IPsec policy checking if we are seeing last header. | |
1372 | * note that we do not visit this with protocols with pcb layer | |
1373 | * code - like udp/tcp/raw ip. | |
1374 | */ | |
91447636 | 1375 | if (ipsec_bypass == 0 && (ip_protox[ip->ip_p]->pr_flags & PR_LASTHDR) != 0) { |
91447636 | 1376 | if (ipsec4_in_reject(m, NULL)) { |
2d21ac55 A |
1377 | IPSEC_STAT_INCREMENT(ipsecstat.in_polvio); |
1378 | goto bad; | |
91447636 | 1379 | } |
1c79356b | 1380 | } |
1c79356b A |
1381 | #endif |
1382 | ||
1383 | /* | |
1384 | * Switch out to protocol's input routine. | |
1385 | */ | |
b0d623f7 | 1386 | OSAddAtomic(1, &ipstat.ips_delivered); |
9bccf70c | 1387 | { |
4a3eedf9 | 1388 | #if IPFIREWALL |
91447636 A |
1389 | if (args.next_hop && ip->ip_p == IPPROTO_TCP) { |
1390 | /* TCP needs IPFORWARD info if available */ | |
1391 | struct m_tag *fwd_tag; | |
1392 | struct ip_fwd_tag *ipfwd_tag; | |
1393 | ||
6d2010ae | 1394 | fwd_tag = m_tag_create(KERNEL_MODULE_TAG_ID, |
b0d623f7 | 1395 | KERNEL_TAG_TYPE_IPFORWARD, sizeof (*ipfwd_tag), |
6d2010ae | 1396 | M_NOWAIT, m); |
91447636 A |
1397 | if (fwd_tag == NULL) { |
1398 | goto bad; | |
1399 | } | |
1400 | ||
1401 | ipfwd_tag = (struct ip_fwd_tag *)(fwd_tag+1); | |
1402 | ipfwd_tag->next_hop = args.next_hop; | |
1403 | ||
1404 | m_tag_prepend(m, fwd_tag); | |
1405 | ||
1406 | KERNEL_DEBUG(DBG_LAYER_END, ip->ip_dst.s_addr, | |
1407 | ip->ip_src.s_addr, ip->ip_p, ip->ip_off, ip->ip_len); | |
1408 | ||
91447636 A |
1409 | |
1410 | /* TCP deals with its own locking */ | |
1411 | ip_proto_dispatch_in(m, hlen, ip->ip_p, 0); | |
1412 | } else { | |
1413 | KERNEL_DEBUG(DBG_LAYER_END, ip->ip_dst.s_addr, | |
1414 | ip->ip_src.s_addr, ip->ip_p, ip->ip_off, ip->ip_len); | |
1415 | ||
91447636 A |
1416 | ip_proto_dispatch_in(m, hlen, ip->ip_p, 0); |
1417 | } | |
4a3eedf9 A |
1418 | #else |
1419 | ip_proto_dispatch_in(m, hlen, ip->ip_p, 0); | |
1420 | #endif | |
91447636 | 1421 | |
9bccf70c A |
1422 | return; |
1423 | } | |
1c79356b | 1424 | bad: |
1c79356b A |
1425 | KERNEL_DEBUG(DBG_LAYER_END, 0,0,0,0,0); |
1426 | m_freem(m); | |
1427 | } | |
1428 | ||
1c79356b | 1429 | /* |
9bccf70c A |
1430 | * Take incoming datagram fragment and try to reassemble it into |
1431 | * whole datagram. If a chain for reassembly of this datagram already | |
1432 | * exists, then it is given as fp; otherwise have to make a chain. | |
1433 | * | |
1434 | * When IPDIVERT enabled, keep additional state with each packet that | |
1435 | * tells us if we need to divert or tee the packet we're building. | |
1c79356b | 1436 | */ |
9bccf70c A |
1437 | |
1438 | static struct mbuf * | |
1439 | #if IPDIVERT | |
2d21ac55 | 1440 | ip_reass(struct mbuf *m, struct ipq *fp, struct ipq *where, |
9bccf70c | 1441 | #ifdef IPDIVERT_44 |
2d21ac55 A |
1442 | u_int32_t *divinfo, |
1443 | #else /* IPDIVERT_44 */ | |
1444 | u_int16_t *divinfo, | |
1445 | #endif /* IPDIVERT_44 */ | |
1446 | u_int16_t *divcookie) | |
1447 | #else /* IPDIVERT */ | |
1448 | ip_reass(struct mbuf *m, struct ipq *fp, struct ipq *where) | |
1449 | #endif /* IPDIVERT */ | |
1c79356b A |
1450 | { |
1451 | struct ip *ip = mtod(m, struct ip *); | |
2d21ac55 | 1452 | struct mbuf *p = 0, *q, *nq; |
1c79356b A |
1453 | struct mbuf *t; |
1454 | int hlen = IP_VHL_HL(ip->ip_vhl) << 2; | |
1455 | int i, next; | |
2d21ac55 | 1456 | u_int8_t ecn, ecn0; |
1c79356b | 1457 | |
2d21ac55 | 1458 | lck_mtx_assert(ip_mutex, LCK_MTX_ASSERT_OWNED); |
1c79356b A |
1459 | /* |
1460 | * Presence of header sizes in mbufs | |
1461 | * would confuse code below. | |
1462 | */ | |
1463 | m->m_data += hlen; | |
1464 | m->m_len -= hlen; | |
1465 | ||
0b4e3aa0 A |
1466 | if (m->m_pkthdr.csum_flags & CSUM_TCP_SUM16) |
1467 | m->m_pkthdr.csum_flags = 0; | |
1c79356b A |
1468 | /* |
1469 | * If first fragment to arrive, create a reassembly queue. | |
1470 | */ | |
1471 | if (fp == 0) { | |
1472 | if ((t = m_get(M_DONTWAIT, MT_FTABLE)) == NULL) | |
1473 | goto dropfrag; | |
1474 | fp = mtod(t, struct ipq *); | |
2d21ac55 A |
1475 | #if CONFIG_MACF_NET |
1476 | if (mac_ipq_label_init(fp, M_NOWAIT) != 0) { | |
1477 | m_free(t); | |
1478 | fp = NULL; | |
1479 | goto dropfrag; | |
1480 | } | |
1481 | mac_ipq_label_associate(m, fp); | |
1482 | #endif | |
9bccf70c | 1483 | insque((void*)fp, (void*)where); |
1c79356b | 1484 | nipq++; |
483a1d10 | 1485 | fp->ipq_nfrags = 1; |
1c79356b A |
1486 | fp->ipq_ttl = IPFRAGTTL; |
1487 | fp->ipq_p = ip->ip_p; | |
1488 | fp->ipq_id = ip->ip_id; | |
1489 | fp->ipq_src = ip->ip_src; | |
1490 | fp->ipq_dst = ip->ip_dst; | |
1491 | fp->ipq_frags = m; | |
1492 | m->m_nextpkt = NULL; | |
1493 | #if IPDIVERT | |
9bccf70c A |
1494 | #ifdef IPDIVERT_44 |
1495 | fp->ipq_div_info = 0; | |
1496 | #else | |
1c79356b | 1497 | fp->ipq_divert = 0; |
9bccf70c | 1498 | #endif |
1c79356b A |
1499 | fp->ipq_div_cookie = 0; |
1500 | #endif | |
91447636 | 1501 | TAILQ_INSERT_HEAD(&ipq_list, fp, ipq_list); |
1c79356b | 1502 | goto inserted; |
483a1d10 A |
1503 | } else { |
1504 | fp->ipq_nfrags++; | |
2d21ac55 A |
1505 | #if CONFIG_MACF_NET |
1506 | mac_ipq_label_update(m, fp); | |
1507 | #endif | |
1c79356b A |
1508 | } |
1509 | ||
1510 | #define GETIP(m) ((struct ip*)((m)->m_pkthdr.header)) | |
1511 | ||
2d21ac55 A |
1512 | /* |
1513 | * Handle ECN by comparing this segment with the first one; | |
1514 | * if CE is set, do not lose CE. | |
1515 | * drop if CE and not-ECT are mixed for the same packet. | |
1516 | */ | |
1517 | ecn = ip->ip_tos & IPTOS_ECN_MASK; | |
1518 | ecn0 = GETIP(fp->ipq_frags)->ip_tos & IPTOS_ECN_MASK; | |
1519 | if (ecn == IPTOS_ECN_CE) { | |
1520 | if (ecn0 == IPTOS_ECN_NOTECT) | |
1521 | goto dropfrag; | |
1522 | if (ecn0 != IPTOS_ECN_CE) | |
1523 | GETIP(fp->ipq_frags)->ip_tos |= IPTOS_ECN_CE; | |
1524 | } | |
1525 | if (ecn == IPTOS_ECN_NOTECT && ecn0 != IPTOS_ECN_NOTECT) | |
1526 | goto dropfrag; | |
1527 | ||
1c79356b A |
1528 | /* |
1529 | * Find a segment which begins after this one does. | |
1530 | */ | |
1531 | for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt) | |
1532 | if (GETIP(q)->ip_off > ip->ip_off) | |
1533 | break; | |
1534 | ||
1535 | /* | |
1536 | * If there is a preceding segment, it may provide some of | |
1537 | * our data already. If so, drop the data from the incoming | |
1538 | * segment. If it provides all of our data, drop us, otherwise | |
1539 | * stick new segment in the proper place. | |
9bccf70c A |
1540 | * |
1541 | * If some of the data is dropped from the the preceding | |
1542 | * segment, then it's checksum is invalidated. | |
1c79356b A |
1543 | */ |
1544 | if (p) { | |
1545 | i = GETIP(p)->ip_off + GETIP(p)->ip_len - ip->ip_off; | |
1546 | if (i > 0) { | |
1547 | if (i >= ip->ip_len) | |
1548 | goto dropfrag; | |
9bccf70c A |
1549 | m_adj(m, i); |
1550 | m->m_pkthdr.csum_flags = 0; | |
1c79356b A |
1551 | ip->ip_off += i; |
1552 | ip->ip_len -= i; | |
1553 | } | |
1554 | m->m_nextpkt = p->m_nextpkt; | |
1555 | p->m_nextpkt = m; | |
1556 | } else { | |
1557 | m->m_nextpkt = fp->ipq_frags; | |
1558 | fp->ipq_frags = m; | |
1559 | } | |
1560 | ||
1561 | /* | |
1562 | * While we overlap succeeding segments trim them or, | |
1563 | * if they are completely covered, dequeue them. | |
1564 | */ | |
1565 | for (; q != NULL && ip->ip_off + ip->ip_len > GETIP(q)->ip_off; | |
1566 | q = nq) { | |
1567 | i = (ip->ip_off + ip->ip_len) - | |
1568 | GETIP(q)->ip_off; | |
1569 | if (i < GETIP(q)->ip_len) { | |
1570 | GETIP(q)->ip_len -= i; | |
1571 | GETIP(q)->ip_off += i; | |
1572 | m_adj(q, i); | |
9bccf70c | 1573 | q->m_pkthdr.csum_flags = 0; |
1c79356b A |
1574 | break; |
1575 | } | |
1576 | nq = q->m_nextpkt; | |
1577 | m->m_nextpkt = nq; | |
b0d623f7 | 1578 | OSAddAtomic(1, &ipstat.ips_fragdropped); |
483a1d10 | 1579 | fp->ipq_nfrags--; |
1c79356b A |
1580 | m_freem(q); |
1581 | } | |
1582 | ||
1583 | inserted: | |
91447636 | 1584 | currentfrags++; |
1c79356b A |
1585 | |
1586 | #if IPDIVERT | |
1587 | /* | |
9bccf70c | 1588 | * Transfer firewall instructions to the fragment structure. |
483a1d10 | 1589 | * Only trust info in the fragment at offset 0. |
1c79356b | 1590 | */ |
483a1d10 | 1591 | if (ip->ip_off == 0) { |
9bccf70c A |
1592 | #ifdef IPDIVERT_44 |
1593 | fp->ipq_div_info = *divinfo; | |
1594 | #else | |
1595 | fp->ipq_divert = *divinfo; | |
1596 | #endif | |
1597 | fp->ipq_div_cookie = *divcookie; | |
483a1d10 | 1598 | } |
9bccf70c A |
1599 | *divinfo = 0; |
1600 | *divcookie = 0; | |
1c79356b A |
1601 | #endif |
1602 | ||
1603 | /* | |
483a1d10 A |
1604 | * Check for complete reassembly and perform frag per packet |
1605 | * limiting. | |
1606 | * | |
1607 | * Frag limiting is performed here so that the nth frag has | |
1608 | * a chance to complete the packet before we drop the packet. | |
1609 | * As a result, n+1 frags are actually allowed per packet, but | |
1610 | * only n will ever be stored. (n = maxfragsperpacket.) | |
1611 | * | |
1c79356b A |
1612 | */ |
1613 | next = 0; | |
1614 | for (p = NULL, q = fp->ipq_frags; q; p = q, q = q->m_nextpkt) { | |
483a1d10 A |
1615 | if (GETIP(q)->ip_off != next) { |
1616 | if (fp->ipq_nfrags > maxfragsperpacket) { | |
b0d623f7 | 1617 | OSAddAtomic(fp->ipq_nfrags, &ipstat.ips_fragdropped); |
483a1d10 A |
1618 | ip_freef(fp); |
1619 | } | |
1c79356b | 1620 | return (0); |
483a1d10 | 1621 | } |
1c79356b A |
1622 | next += GETIP(q)->ip_len; |
1623 | } | |
1624 | /* Make sure the last packet didn't have the IP_MF flag */ | |
483a1d10 A |
1625 | if (p->m_flags & M_FRAG) { |
1626 | if (fp->ipq_nfrags > maxfragsperpacket) { | |
b0d623f7 | 1627 | OSAddAtomic(fp->ipq_nfrags, &ipstat.ips_fragdropped); |
483a1d10 A |
1628 | ip_freef(fp); |
1629 | } | |
1c79356b | 1630 | return (0); |
483a1d10 | 1631 | } |
1c79356b A |
1632 | |
1633 | /* | |
1634 | * Reassembly is complete. Make sure the packet is a sane size. | |
1635 | */ | |
1636 | q = fp->ipq_frags; | |
1637 | ip = GETIP(q); | |
1638 | if (next + (IP_VHL_HL(ip->ip_vhl) << 2) > IP_MAXPACKET) { | |
b0d623f7 A |
1639 | OSAddAtomic(1, &ipstat.ips_toolong); |
1640 | OSAddAtomic(fp->ipq_nfrags, &ipstat.ips_fragdropped); | |
1c79356b A |
1641 | ip_freef(fp); |
1642 | return (0); | |
1643 | } | |
1644 | ||
1645 | /* | |
1646 | * Concatenate fragments. | |
1647 | */ | |
1648 | m = q; | |
1649 | t = m->m_next; | |
1650 | m->m_next = 0; | |
1651 | m_cat(m, t); | |
1652 | nq = q->m_nextpkt; | |
1653 | q->m_nextpkt = 0; | |
1654 | for (q = nq; q != NULL; q = nq) { | |
1655 | nq = q->m_nextpkt; | |
1656 | q->m_nextpkt = NULL; | |
91447636 A |
1657 | if (q->m_pkthdr.csum_flags & CSUM_TCP_SUM16) |
1658 | m->m_pkthdr.csum_flags = 0; | |
1659 | else { | |
9bccf70c A |
1660 | m->m_pkthdr.csum_flags &= q->m_pkthdr.csum_flags; |
1661 | m->m_pkthdr.csum_data += q->m_pkthdr.csum_data; | |
91447636 | 1662 | } |
1c79356b A |
1663 | m_cat(m, q); |
1664 | } | |
1665 | ||
1666 | #if IPDIVERT | |
1667 | /* | |
9bccf70c | 1668 | * Extract firewall instructions from the fragment structure. |
1c79356b | 1669 | */ |
9bccf70c A |
1670 | #ifdef IPDIVERT_44 |
1671 | *divinfo = fp->ipq_div_info; | |
1672 | #else | |
1673 | *divinfo = fp->ipq_divert; | |
1674 | #endif | |
1675 | *divcookie = fp->ipq_div_cookie; | |
1c79356b A |
1676 | #endif |
1677 | ||
2d21ac55 A |
1678 | #if CONFIG_MACF_NET |
1679 | mac_mbuf_label_associate_ipq(fp, m); | |
1680 | mac_ipq_label_destroy(fp); | |
1681 | #endif | |
1c79356b A |
1682 | /* |
1683 | * Create header for new ip packet by | |
1684 | * modifying header of first packet; | |
1685 | * dequeue and discard fragment reassembly header. | |
1686 | * Make header visible. | |
1687 | */ | |
1688 | ip->ip_len = next; | |
1689 | ip->ip_src = fp->ipq_src; | |
1690 | ip->ip_dst = fp->ipq_dst; | |
9bccf70c | 1691 | remque((void*)fp); |
91447636 A |
1692 | TAILQ_REMOVE(&ipq_list, fp, ipq_list); |
1693 | currentfrags -= fp->ipq_nfrags; | |
1c79356b A |
1694 | nipq--; |
1695 | (void) m_free(dtom(fp)); | |
1696 | m->m_len += (IP_VHL_HL(ip->ip_vhl) << 2); | |
1697 | m->m_data -= (IP_VHL_HL(ip->ip_vhl) << 2); | |
1698 | /* some debugging cruft by sklower, below, will go away soon */ | |
1699 | if (m->m_flags & M_PKTHDR) { /* XXX this should be done elsewhere */ | |
2d21ac55 | 1700 | int plen = 0; |
9bccf70c A |
1701 | for (t = m; t; t = t->m_next) |
1702 | plen += t->m_len; | |
1703 | m->m_pkthdr.len = plen; | |
1c79356b | 1704 | } |
9bccf70c | 1705 | return (m); |
1c79356b A |
1706 | |
1707 | dropfrag: | |
1708 | #if IPDIVERT | |
9bccf70c A |
1709 | *divinfo = 0; |
1710 | *divcookie = 0; | |
1c79356b | 1711 | #endif |
b0d623f7 | 1712 | OSAddAtomic(1, &ipstat.ips_fragdropped); |
483a1d10 A |
1713 | if (fp != 0) |
1714 | fp->ipq_nfrags--; | |
1c79356b A |
1715 | m_freem(m); |
1716 | return (0); | |
1717 | ||
1718 | #undef GETIP | |
1719 | } | |
1720 | ||
1721 | /* | |
1722 | * Free a fragment reassembly header and all | |
1723 | * associated datagrams. | |
1724 | */ | |
1725 | static void | |
2d21ac55 | 1726 | ip_freef(struct ipq *fp) |
1c79356b | 1727 | { |
2d21ac55 | 1728 | lck_mtx_assert(ip_mutex, LCK_MTX_ASSERT_OWNED); |
91447636 A |
1729 | currentfrags -= fp->ipq_nfrags; |
1730 | m_freem_list(fp->ipq_frags); | |
9bccf70c | 1731 | remque((void*)fp); |
91447636 | 1732 | TAILQ_REMOVE(&ipq_list, fp, ipq_list); |
1c79356b A |
1733 | (void) m_free(dtom(fp)); |
1734 | nipq--; | |
1735 | } | |
1736 | ||
1737 | /* | |
1738 | * IP timer processing; | |
1739 | * if a timer expires on a reassembly | |
1740 | * queue, discard it. | |
1741 | */ | |
1742 | void | |
2d21ac55 | 1743 | ip_slowtimo(void) |
1c79356b | 1744 | { |
2d21ac55 | 1745 | struct ipq *fp; |
1c79356b | 1746 | int i; |
91447636 | 1747 | lck_mtx_lock(ip_mutex); |
1c79356b A |
1748 | for (i = 0; i < IPREASS_NHASH; i++) { |
1749 | fp = ipq[i].next; | |
1750 | if (fp == 0) | |
1751 | continue; | |
1752 | while (fp != &ipq[i]) { | |
1753 | --fp->ipq_ttl; | |
1754 | fp = fp->next; | |
1755 | if (fp->prev->ipq_ttl == 0) { | |
b0d623f7 | 1756 | OSAddAtomic(fp->ipq_nfrags, &ipstat.ips_fragtimeout); |
1c79356b A |
1757 | ip_freef(fp->prev); |
1758 | } | |
1759 | } | |
1760 | } | |
9bccf70c A |
1761 | /* |
1762 | * If we are over the maximum number of fragments | |
1763 | * (due to the limit being lowered), drain off | |
1764 | * enough to get down to the new limit. | |
1765 | */ | |
483a1d10 | 1766 | if (maxnipq >= 0 && nipq > maxnipq) { |
9bccf70c | 1767 | for (i = 0; i < IPREASS_NHASH; i++) { |
483a1d10 | 1768 | while (nipq > maxnipq && |
9bccf70c | 1769 | (ipq[i].next != &ipq[i])) { |
b0d623f7 | 1770 | OSAddAtomic(ipq[i].next->ipq_nfrags, &ipstat.ips_fragdropped); |
9bccf70c A |
1771 | ip_freef(ipq[i].next); |
1772 | } | |
1773 | } | |
1774 | } | |
91447636 | 1775 | lck_mtx_unlock(ip_mutex); |
1c79356b A |
1776 | } |
1777 | ||
1778 | /* | |
1779 | * Drain off all datagram fragments. | |
1780 | */ | |
1781 | void | |
2d21ac55 | 1782 | ip_drain(void) |
1c79356b A |
1783 | { |
1784 | int i; | |
1785 | ||
91447636 | 1786 | lck_mtx_lock(ip_mutex); |
1c79356b A |
1787 | for (i = 0; i < IPREASS_NHASH; i++) { |
1788 | while (ipq[i].next != &ipq[i]) { | |
b0d623f7 | 1789 | OSAddAtomic(ipq[i].next->ipq_nfrags, &ipstat.ips_fragdropped); |
1c79356b A |
1790 | ip_freef(ipq[i].next); |
1791 | } | |
1792 | } | |
91447636 | 1793 | lck_mtx_unlock(ip_mutex); |
1c79356b A |
1794 | in_rtqdrain(); |
1795 | } | |
1796 | ||
1797 | /* | |
1798 | * Do option processing on a datagram, | |
1799 | * possibly discarding it if bad options are encountered, | |
1800 | * or forwarding it if source-routed. | |
91447636 A |
1801 | * The pass argument is used when operating in the IPSTEALTH |
1802 | * mode to tell what options to process: | |
1803 | * [LS]SRR (pass 0) or the others (pass 1). | |
1804 | * The reason for as many as two passes is that when doing IPSTEALTH, | |
1805 | * non-routing options should be processed only if the packet is for us. | |
1c79356b A |
1806 | * Returns 1 if packet has been forwarded/freed, |
1807 | * 0 if the packet should be processed further. | |
1808 | */ | |
1809 | static int | |
b0d623f7 | 1810 | ip_dooptions(struct mbuf *m, __unused int pass, struct sockaddr_in *next_hop) |
1c79356b | 1811 | { |
2d21ac55 A |
1812 | struct ip *ip = mtod(m, struct ip *); |
1813 | u_char *cp; | |
1814 | struct ip_timestamp *ipt; | |
1815 | struct in_ifaddr *ia; | |
1c79356b A |
1816 | int opt, optlen, cnt, off, code, type = ICMP_PARAMPROB, forward = 0; |
1817 | struct in_addr *sin, dst; | |
1818 | n_time ntime; | |
b0d623f7 A |
1819 | struct sockaddr_in ipaddr = { |
1820 | sizeof (ipaddr), AF_INET , 0 , { 0 }, { 0, } }; | |
1c79356b A |
1821 | |
1822 | dst = ip->ip_dst; | |
1823 | cp = (u_char *)(ip + 1); | |
1824 | cnt = (IP_VHL_HL(ip->ip_vhl) << 2) - sizeof (struct ip); | |
1825 | for (; cnt > 0; cnt -= optlen, cp += optlen) { | |
1826 | opt = cp[IPOPT_OPTVAL]; | |
1827 | if (opt == IPOPT_EOL) | |
1828 | break; | |
1829 | if (opt == IPOPT_NOP) | |
1830 | optlen = 1; | |
1831 | else { | |
1832 | if (cnt < IPOPT_OLEN + sizeof(*cp)) { | |
9bccf70c | 1833 | code = &cp[IPOPT_OLEN] - (u_char *)ip; |
1c79356b A |
1834 | goto bad; |
1835 | } | |
1836 | optlen = cp[IPOPT_OLEN]; | |
1837 | if (optlen < IPOPT_OLEN + sizeof(*cp) || optlen > cnt) { | |
1838 | code = &cp[IPOPT_OLEN] - (u_char *)ip; | |
1839 | goto bad; | |
1840 | } | |
1841 | } | |
1842 | switch (opt) { | |
1843 | ||
1844 | default: | |
1845 | break; | |
1846 | ||
1847 | /* | |
1848 | * Source routing with record. | |
1849 | * Find interface with current destination address. | |
1850 | * If none on this machine then drop if strictly routed, | |
1851 | * or do nothing if loosely routed. | |
1852 | * Record interface address and bring up next address | |
1853 | * component. If strictly routed make sure next | |
1854 | * address is on directly accessible net. | |
1855 | */ | |
1856 | case IPOPT_LSRR: | |
1857 | case IPOPT_SSRR: | |
9bccf70c A |
1858 | if (optlen < IPOPT_OFFSET + sizeof(*cp)) { |
1859 | code = &cp[IPOPT_OLEN] - (u_char *)ip; | |
1860 | goto bad; | |
1861 | } | |
1c79356b A |
1862 | if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) { |
1863 | code = &cp[IPOPT_OFFSET] - (u_char *)ip; | |
1864 | goto bad; | |
1865 | } | |
1866 | ipaddr.sin_addr = ip->ip_dst; | |
1867 | ia = (struct in_ifaddr *) | |
1868 | ifa_ifwithaddr((struct sockaddr *)&ipaddr); | |
1869 | if (ia == 0) { | |
1870 | if (opt == IPOPT_SSRR) { | |
1871 | type = ICMP_UNREACH; | |
1872 | code = ICMP_UNREACH_SRCFAIL; | |
1873 | goto bad; | |
1874 | } | |
1875 | if (!ip_dosourceroute) | |
1876 | goto nosourcerouting; | |
1877 | /* | |
1878 | * Loose routing, and not at next destination | |
1879 | * yet; nothing to do except forward. | |
1880 | */ | |
1881 | break; | |
1882 | } | |
91447636 | 1883 | else { |
6d2010ae | 1884 | IFA_REMREF(&ia->ia_ifa); |
91447636 A |
1885 | ia = NULL; |
1886 | } | |
1c79356b | 1887 | off--; /* 0 origin */ |
9bccf70c | 1888 | if (off > optlen - (int)sizeof(struct in_addr)) { |
1c79356b A |
1889 | /* |
1890 | * End of source route. Should be for us. | |
1891 | */ | |
1892 | if (!ip_acceptsourceroute) | |
1893 | goto nosourcerouting; | |
1894 | save_rte(cp, ip->ip_src); | |
1895 | break; | |
1896 | } | |
1897 | ||
1898 | if (!ip_dosourceroute) { | |
1899 | if (ipforwarding) { | |
91447636 A |
1900 | char buf[MAX_IPv4_STR_LEN]; |
1901 | char buf2[MAX_IPv4_STR_LEN]; | |
1c79356b A |
1902 | /* |
1903 | * Acting as a router, so generate ICMP | |
1904 | */ | |
1905 | nosourcerouting: | |
91447636 | 1906 | log(LOG_WARNING, |
1c79356b | 1907 | "attempted source route from %s to %s\n", |
91447636 A |
1908 | inet_ntop(AF_INET, &ip->ip_src, buf, sizeof(buf)), |
1909 | inet_ntop(AF_INET, &ip->ip_dst, buf2, sizeof(buf2))); | |
1c79356b A |
1910 | type = ICMP_UNREACH; |
1911 | code = ICMP_UNREACH_SRCFAIL; | |
1912 | goto bad; | |
1913 | } else { | |
1914 | /* | |
1915 | * Not acting as a router, so silently drop. | |
1916 | */ | |
b0d623f7 | 1917 | OSAddAtomic(1, &ipstat.ips_cantforward); |
1c79356b A |
1918 | m_freem(m); |
1919 | return (1); | |
1920 | } | |
1921 | } | |
1922 | ||
1923 | /* | |
1924 | * locate outgoing interface | |
1925 | */ | |
1926 | (void)memcpy(&ipaddr.sin_addr, cp + off, | |
1927 | sizeof(ipaddr.sin_addr)); | |
1928 | ||
1929 | if (opt == IPOPT_SSRR) { | |
1930 | #define INA struct in_ifaddr * | |
1931 | #define SA struct sockaddr * | |
91447636 A |
1932 | if ((ia = (INA)ifa_ifwithdstaddr((SA)&ipaddr)) == 0) { |
1933 | ia = (INA)ifa_ifwithnet((SA)&ipaddr); | |
1934 | } | |
1935 | } else { | |
b0d623f7 | 1936 | ia = ip_rtaddr(ipaddr.sin_addr); |
91447636 | 1937 | } |
1c79356b A |
1938 | if (ia == 0) { |
1939 | type = ICMP_UNREACH; | |
1940 | code = ICMP_UNREACH_SRCFAIL; | |
1941 | goto bad; | |
1942 | } | |
1943 | ip->ip_dst = ipaddr.sin_addr; | |
6d2010ae | 1944 | IFA_LOCK(&ia->ia_ifa); |
1c79356b A |
1945 | (void)memcpy(cp + off, &(IA_SIN(ia)->sin_addr), |
1946 | sizeof(struct in_addr)); | |
6d2010ae A |
1947 | IFA_UNLOCK(&ia->ia_ifa); |
1948 | IFA_REMREF(&ia->ia_ifa); | |
91447636 | 1949 | ia = NULL; |
1c79356b A |
1950 | cp[IPOPT_OFFSET] += sizeof(struct in_addr); |
1951 | /* | |
1952 | * Let ip_intr's mcast routing check handle mcast pkts | |
1953 | */ | |
1954 | forward = !IN_MULTICAST(ntohl(ip->ip_dst.s_addr)); | |
1955 | break; | |
1956 | ||
1957 | case IPOPT_RR: | |
1958 | if (optlen < IPOPT_OFFSET + sizeof(*cp)) { | |
1959 | code = &cp[IPOPT_OFFSET] - (u_char *)ip; | |
1960 | goto bad; | |
1961 | } | |
1962 | if ((off = cp[IPOPT_OFFSET]) < IPOPT_MINOFF) { | |
1963 | code = &cp[IPOPT_OFFSET] - (u_char *)ip; | |
1964 | goto bad; | |
1965 | } | |
1966 | /* | |
1967 | * If no space remains, ignore. | |
1968 | */ | |
1969 | off--; /* 0 origin */ | |
9bccf70c | 1970 | if (off > optlen - (int)sizeof(struct in_addr)) |
1c79356b A |
1971 | break; |
1972 | (void)memcpy(&ipaddr.sin_addr, &ip->ip_dst, | |
1973 | sizeof(ipaddr.sin_addr)); | |
1974 | /* | |
1975 | * locate outgoing interface; if we're the destination, | |
1976 | * use the incoming interface (should be same). | |
1977 | */ | |
cc9f6e38 | 1978 | if ((ia = (INA)ifa_ifwithaddr((SA)&ipaddr)) == 0) { |
b0d623f7 | 1979 | if ((ia = ip_rtaddr(ipaddr.sin_addr)) == 0) { |
91447636 A |
1980 | type = ICMP_UNREACH; |
1981 | code = ICMP_UNREACH_HOST; | |
1982 | goto bad; | |
1983 | } | |
1c79356b | 1984 | } |
6d2010ae | 1985 | IFA_LOCK(&ia->ia_ifa); |
1c79356b A |
1986 | (void)memcpy(cp + off, &(IA_SIN(ia)->sin_addr), |
1987 | sizeof(struct in_addr)); | |
6d2010ae A |
1988 | IFA_UNLOCK(&ia->ia_ifa); |
1989 | IFA_REMREF(&ia->ia_ifa); | |
91447636 | 1990 | ia = NULL; |
1c79356b A |
1991 | cp[IPOPT_OFFSET] += sizeof(struct in_addr); |
1992 | break; | |
1993 | ||
1994 | case IPOPT_TS: | |
1995 | code = cp - (u_char *)ip; | |
1996 | ipt = (struct ip_timestamp *)cp; | |
9bccf70c A |
1997 | if (ipt->ipt_len < 4 || ipt->ipt_len > 40) { |
1998 | code = (u_char *)&ipt->ipt_len - (u_char *)ip; | |
1c79356b | 1999 | goto bad; |
9bccf70c A |
2000 | } |
2001 | if (ipt->ipt_ptr < 5) { | |
2002 | code = (u_char *)&ipt->ipt_ptr - (u_char *)ip; | |
2003 | goto bad; | |
2004 | } | |
2005 | if (ipt->ipt_ptr > | |
2006 | ipt->ipt_len - (int)sizeof(int32_t)) { | |
2007 | if (++ipt->ipt_oflw == 0) { | |
2008 | code = (u_char *)&ipt->ipt_ptr - | |
2009 | (u_char *)ip; | |
1c79356b | 2010 | goto bad; |
9bccf70c | 2011 | } |
1c79356b A |
2012 | break; |
2013 | } | |
2014 | sin = (struct in_addr *)(cp + ipt->ipt_ptr - 1); | |
2015 | switch (ipt->ipt_flg) { | |
2016 | ||
2017 | case IPOPT_TS_TSONLY: | |
2018 | break; | |
2019 | ||
2020 | case IPOPT_TS_TSANDADDR: | |
2021 | if (ipt->ipt_ptr - 1 + sizeof(n_time) + | |
9bccf70c A |
2022 | sizeof(struct in_addr) > ipt->ipt_len) { |
2023 | code = (u_char *)&ipt->ipt_ptr - | |
2024 | (u_char *)ip; | |
1c79356b | 2025 | goto bad; |
9bccf70c | 2026 | } |
1c79356b A |
2027 | ipaddr.sin_addr = dst; |
2028 | ia = (INA)ifaof_ifpforaddr((SA)&ipaddr, | |
2029 | m->m_pkthdr.rcvif); | |
2030 | if (ia == 0) | |
2031 | continue; | |
6d2010ae | 2032 | IFA_LOCK(&ia->ia_ifa); |
1c79356b A |
2033 | (void)memcpy(sin, &IA_SIN(ia)->sin_addr, |
2034 | sizeof(struct in_addr)); | |
6d2010ae | 2035 | IFA_UNLOCK(&ia->ia_ifa); |
1c79356b | 2036 | ipt->ipt_ptr += sizeof(struct in_addr); |
6d2010ae | 2037 | IFA_REMREF(&ia->ia_ifa); |
91447636 | 2038 | ia = NULL; |
1c79356b A |
2039 | break; |
2040 | ||
2041 | case IPOPT_TS_PRESPEC: | |
2042 | if (ipt->ipt_ptr - 1 + sizeof(n_time) + | |
9bccf70c A |
2043 | sizeof(struct in_addr) > ipt->ipt_len) { |
2044 | code = (u_char *)&ipt->ipt_ptr - | |
2045 | (u_char *)ip; | |
1c79356b | 2046 | goto bad; |
9bccf70c | 2047 | } |
1c79356b A |
2048 | (void)memcpy(&ipaddr.sin_addr, sin, |
2049 | sizeof(struct in_addr)); | |
91447636 | 2050 | if ((ia = (struct in_ifaddr*)ifa_ifwithaddr((SA)&ipaddr)) == 0) |
1c79356b | 2051 | continue; |
6d2010ae | 2052 | IFA_REMREF(&ia->ia_ifa); |
91447636 | 2053 | ia = NULL; |
1c79356b A |
2054 | ipt->ipt_ptr += sizeof(struct in_addr); |
2055 | break; | |
2056 | ||
2057 | default: | |
9bccf70c A |
2058 | /* XXX can't take &ipt->ipt_flg */ |
2059 | code = (u_char *)&ipt->ipt_ptr - | |
2060 | (u_char *)ip + 1; | |
1c79356b A |
2061 | goto bad; |
2062 | } | |
2063 | ntime = iptime(); | |
2064 | (void)memcpy(cp + ipt->ipt_ptr - 1, &ntime, | |
2065 | sizeof(n_time)); | |
2066 | ipt->ipt_ptr += sizeof(n_time); | |
2067 | } | |
2068 | } | |
2069 | if (forward && ipforwarding) { | |
b0d623f7 | 2070 | ip_forward(m, 1, next_hop); |
1c79356b A |
2071 | return (1); |
2072 | } | |
2073 | return (0); | |
2074 | bad: | |
2075 | ip->ip_len -= IP_VHL_HL(ip->ip_vhl) << 2; /* XXX icmp_error adds in hdr length */ | |
2076 | icmp_error(m, type, code, 0, 0); | |
b0d623f7 | 2077 | OSAddAtomic(1, &ipstat.ips_badoptions); |
1c79356b A |
2078 | return (1); |
2079 | } | |
2080 | ||
2081 | /* | |
2082 | * Given address of next destination (final or next hop), | |
2083 | * return internet address info of interface to be used to get there. | |
2084 | */ | |
91447636 | 2085 | struct in_ifaddr * |
b0d623f7 | 2086 | ip_rtaddr(struct in_addr dst) |
1c79356b | 2087 | { |
2d21ac55 | 2088 | struct sockaddr_in *sin; |
b0d623f7 A |
2089 | struct ifaddr *rt_ifa; |
2090 | struct route ro; | |
2091 | ||
2092 | bzero(&ro, sizeof (ro)); | |
2093 | sin = (struct sockaddr_in *)&ro.ro_dst; | |
2094 | sin->sin_family = AF_INET; | |
2095 | sin->sin_len = sizeof (*sin); | |
2096 | sin->sin_addr = dst; | |
2097 | ||
2098 | rtalloc_ign(&ro, RTF_PRCLONING); | |
2099 | if (ro.ro_rt == NULL) | |
2100 | return (NULL); | |
2101 | ||
2102 | RT_LOCK(ro.ro_rt); | |
2103 | if ((rt_ifa = ro.ro_rt->rt_ifa) != NULL) | |
6d2010ae | 2104 | IFA_ADDREF(rt_ifa); |
b0d623f7 A |
2105 | RT_UNLOCK(ro.ro_rt); |
2106 | rtfree(ro.ro_rt); | |
2107 | ||
2108 | return ((struct in_ifaddr *)rt_ifa); | |
1c79356b A |
2109 | } |
2110 | ||
2111 | /* | |
2112 | * Save incoming source route for use in replies, | |
2113 | * to be picked up later by ip_srcroute if the receiver is interested. | |
2114 | */ | |
2115 | void | |
2d21ac55 | 2116 | save_rte(u_char *option, struct in_addr dst) |
1c79356b A |
2117 | { |
2118 | unsigned olen; | |
2119 | ||
2120 | olen = option[IPOPT_OLEN]; | |
2121 | #if DIAGNOSTIC | |
2122 | if (ipprintfs) | |
2123 | printf("save_rte: olen %d\n", olen); | |
2124 | #endif | |
2125 | if (olen > sizeof(ip_srcrt) - (1 + sizeof(dst))) | |
2126 | return; | |
2127 | bcopy(option, ip_srcrt.srcopt, olen); | |
2128 | ip_nhops = (olen - IPOPT_OFFSET - 1) / sizeof(struct in_addr); | |
2129 | ip_srcrt.dst = dst; | |
2130 | } | |
2131 | ||
2132 | /* | |
2133 | * Retrieve incoming source route for use in replies, | |
2134 | * in the same form used by setsockopt. | |
2135 | * The first hop is placed before the options, will be removed later. | |
2136 | */ | |
2137 | struct mbuf * | |
2d21ac55 | 2138 | ip_srcroute(void) |
1c79356b | 2139 | { |
2d21ac55 A |
2140 | struct in_addr *p, *q; |
2141 | struct mbuf *m; | |
1c79356b A |
2142 | |
2143 | if (ip_nhops == 0) | |
2144 | return ((struct mbuf *)0); | |
2145 | m = m_get(M_DONTWAIT, MT_HEADER); | |
2146 | if (m == 0) | |
2147 | return ((struct mbuf *)0); | |
2148 | ||
2149 | #define OPTSIZ (sizeof(ip_srcrt.nop) + sizeof(ip_srcrt.srcopt)) | |
2150 | ||
2151 | /* length is (nhops+1)*sizeof(addr) + sizeof(nop + srcrt header) */ | |
2152 | m->m_len = ip_nhops * sizeof(struct in_addr) + sizeof(struct in_addr) + | |
2153 | OPTSIZ; | |
2154 | #if DIAGNOSTIC | |
2155 | if (ipprintfs) | |
2156 | printf("ip_srcroute: nhops %d mlen %d", ip_nhops, m->m_len); | |
2157 | #endif | |
2158 | ||
2159 | /* | |
2160 | * First save first hop for return route | |
2161 | */ | |
2162 | p = &ip_srcrt.route[ip_nhops - 1]; | |
2163 | *(mtod(m, struct in_addr *)) = *p--; | |
2164 | #if DIAGNOSTIC | |
2165 | if (ipprintfs) | |
b0d623f7 | 2166 | printf(" hops %lx", (u_int32_t)ntohl(mtod(m, struct in_addr *)->s_addr)); |
1c79356b A |
2167 | #endif |
2168 | ||
2169 | /* | |
2170 | * Copy option fields and padding (nop) to mbuf. | |
2171 | */ | |
2172 | ip_srcrt.nop = IPOPT_NOP; | |
2173 | ip_srcrt.srcopt[IPOPT_OFFSET] = IPOPT_MINOFF; | |
2174 | (void)memcpy(mtod(m, caddr_t) + sizeof(struct in_addr), | |
2175 | &ip_srcrt.nop, OPTSIZ); | |
2176 | q = (struct in_addr *)(mtod(m, caddr_t) + | |
2177 | sizeof(struct in_addr) + OPTSIZ); | |
2178 | #undef OPTSIZ | |
2179 | /* | |
2180 | * Record return path as an IP source route, | |
2181 | * reversing the path (pointers are now aligned). | |
2182 | */ | |
2183 | while (p >= ip_srcrt.route) { | |
2184 | #if DIAGNOSTIC | |
2185 | if (ipprintfs) | |
b0d623f7 | 2186 | printf(" %lx", (u_int32_t)ntohl(q->s_addr)); |
1c79356b A |
2187 | #endif |
2188 | *q++ = *p--; | |
2189 | } | |
2190 | /* | |
2191 | * Last hop goes to final destination. | |
2192 | */ | |
2193 | *q = ip_srcrt.dst; | |
2194 | #if DIAGNOSTIC | |
2195 | if (ipprintfs) | |
b0d623f7 | 2196 | printf(" %lx\n", (u_int32_t)ntohl(q->s_addr)); |
1c79356b A |
2197 | #endif |
2198 | return (m); | |
2199 | } | |
2200 | ||
2201 | /* | |
2202 | * Strip out IP options, at higher | |
2203 | * level protocol in the kernel. | |
2204 | * Second argument is buffer to which options | |
2205 | * will be moved, and return value is their length. | |
2206 | * XXX should be deleted; last arg currently ignored. | |
2207 | */ | |
2208 | void | |
2d21ac55 | 2209 | ip_stripoptions(struct mbuf *m, __unused struct mbuf *mopt) |
1c79356b | 2210 | { |
2d21ac55 | 2211 | int i; |
1c79356b | 2212 | struct ip *ip = mtod(m, struct ip *); |
2d21ac55 | 2213 | caddr_t opts; |
1c79356b A |
2214 | int olen; |
2215 | ||
2216 | olen = (IP_VHL_HL(ip->ip_vhl) << 2) - sizeof (struct ip); | |
2217 | opts = (caddr_t)(ip + 1); | |
2218 | i = m->m_len - (sizeof (struct ip) + olen); | |
2219 | bcopy(opts + olen, opts, (unsigned)i); | |
2220 | m->m_len -= olen; | |
2221 | if (m->m_flags & M_PKTHDR) | |
2222 | m->m_pkthdr.len -= olen; | |
2223 | ip->ip_vhl = IP_MAKE_VHL(IPVERSION, sizeof(struct ip) >> 2); | |
2224 | } | |
2225 | ||
2226 | u_char inetctlerrmap[PRC_NCMDS] = { | |
2227 | 0, 0, 0, 0, | |
2228 | 0, EMSGSIZE, EHOSTDOWN, EHOSTUNREACH, | |
2d21ac55 | 2229 | ENETUNREACH, EHOSTUNREACH, ECONNREFUSED, ECONNREFUSED, |
1c79356b A |
2230 | EMSGSIZE, EHOSTUNREACH, 0, 0, |
2231 | 0, 0, 0, 0, | |
9bccf70c | 2232 | ENOPROTOOPT, ECONNREFUSED |
1c79356b A |
2233 | }; |
2234 | ||
b0d623f7 A |
2235 | static int |
2236 | sysctl_ipforwarding SYSCTL_HANDLER_ARGS | |
2237 | { | |
2238 | #pragma unused(arg1, arg2) | |
2239 | int i, was_ipforwarding = ipforwarding; | |
2240 | ||
2241 | i = sysctl_handle_int(oidp, oidp->oid_arg1, oidp->oid_arg2, req); | |
2242 | if (i != 0 || req->newptr == USER_ADDR_NULL) | |
2243 | return (i); | |
2244 | ||
2245 | if (was_ipforwarding && !ipforwarding) { | |
2246 | /* clean up IPv4 forwarding cached routes */ | |
2247 | ifnet_head_lock_shared(); | |
2248 | for (i = 0; i <= if_index; i++) { | |
2249 | struct ifnet *ifp = ifindex2ifnet[i]; | |
2250 | if (ifp != NULL) { | |
6d2010ae A |
2251 | lck_mtx_lock(&ifp->if_cached_route_lock); |
2252 | if (ifp->if_fwd_route.ro_rt != NULL) | |
b0d623f7 | 2253 | rtfree(ifp->if_fwd_route.ro_rt); |
6d2010ae A |
2254 | bzero(&ifp->if_fwd_route, |
2255 | sizeof (ifp->if_fwd_route)); | |
2256 | lck_mtx_unlock(&ifp->if_cached_route_lock); | |
b0d623f7 A |
2257 | } |
2258 | } | |
2259 | ifnet_head_done(); | |
2260 | } | |
2261 | ||
2262 | return (0); | |
2263 | } | |
2264 | ||
2265 | /* | |
2266 | * Similar to inp_route_{copyout,copyin} routines except that these copy | |
2267 | * out the cached IPv4 forwarding route from struct ifnet instead of the | |
2268 | * inpcb. See comments for those routines for explanations. | |
2269 | */ | |
2270 | static void | |
2271 | ip_fwd_route_copyout(struct ifnet *ifp, struct route *dst) | |
2272 | { | |
2273 | struct route *src = &ifp->if_fwd_route; | |
2274 | ||
6d2010ae A |
2275 | lck_mtx_lock_spin(&ifp->if_cached_route_lock); |
2276 | lck_mtx_convert_spin(&ifp->if_cached_route_lock); | |
b0d623f7 A |
2277 | |
2278 | /* Minor sanity check */ | |
2279 | if (src->ro_rt != NULL && rt_key(src->ro_rt)->sa_family != AF_INET) | |
2280 | panic("%s: wrong or corrupted route: %p", __func__, src); | |
2281 | ||
6d2010ae | 2282 | route_copyout(dst, src, sizeof(*dst)); |
b0d623f7 | 2283 | |
6d2010ae | 2284 | lck_mtx_unlock(&ifp->if_cached_route_lock); |
b0d623f7 A |
2285 | } |
2286 | ||
2287 | static void | |
2288 | ip_fwd_route_copyin(struct ifnet *ifp, struct route *src) | |
2289 | { | |
2290 | struct route *dst = &ifp->if_fwd_route; | |
2291 | ||
6d2010ae A |
2292 | lck_mtx_lock_spin(&ifp->if_cached_route_lock); |
2293 | lck_mtx_convert_spin(&ifp->if_cached_route_lock); | |
b0d623f7 A |
2294 | |
2295 | /* Minor sanity check */ | |
2296 | if (src->ro_rt != NULL && rt_key(src->ro_rt)->sa_family != AF_INET) | |
2297 | panic("%s: wrong or corrupted route: %p", __func__, src); | |
2298 | ||
6d2010ae A |
2299 | if (ifp->if_fwd_cacheok) |
2300 | route_copyin(src, dst, sizeof(*src)); | |
b0d623f7 | 2301 | |
6d2010ae | 2302 | lck_mtx_unlock(&ifp->if_cached_route_lock); |
b0d623f7 A |
2303 | } |
2304 | ||
1c79356b A |
2305 | /* |
2306 | * Forward a packet. If some error occurs return the sender | |
2307 | * an icmp packet. Note we can't always generate a meaningful | |
2308 | * icmp message because icmp doesn't have a large enough repertoire | |
2309 | * of codes and types. | |
2310 | * | |
2311 | * If not forwarding, just drop the packet. This could be confusing | |
2312 | * if ipforwarding was zero but some routing protocol was advancing | |
2313 | * us as a gateway to somewhere. However, we must let the routing | |
2314 | * protocol deal with that. | |
2315 | * | |
2316 | * The srcrt parameter indicates whether the packet is being forwarded | |
2317 | * via a source route. | |
2318 | */ | |
9bccf70c | 2319 | static void |
b0d623f7 | 2320 | ip_forward(struct mbuf *m, int srcrt, struct sockaddr_in *next_hop) |
1c79356b | 2321 | { |
b0d623f7 A |
2322 | #if !IPFIREWALL |
2323 | #pragma unused(next_hop) | |
2324 | #endif | |
2d21ac55 A |
2325 | struct ip *ip = mtod(m, struct ip *); |
2326 | struct sockaddr_in *sin; | |
2327 | struct rtentry *rt; | |
b0d623f7 | 2328 | struct route fwd_rt; |
1c79356b A |
2329 | int error, type = 0, code = 0; |
2330 | struct mbuf *mcopy; | |
2331 | n_long dest; | |
91447636 | 2332 | struct in_addr pkt_dst; |
b0d623f7 | 2333 | u_int32_t nextmtu = 0; |
6d2010ae | 2334 | struct ip_out_args ipoa = { IFSCOPE_NONE, 0 }; |
b0d623f7 A |
2335 | struct ifnet *ifp = m->m_pkthdr.rcvif; |
2336 | #if PF | |
2337 | struct pf_mtag *pf_mtag; | |
2338 | #endif /* PF */ | |
1c79356b A |
2339 | |
2340 | dest = 0; | |
b0d623f7 | 2341 | #if IPFIREWALL |
91447636 A |
2342 | /* |
2343 | * Cache the destination address of the packet; this may be | |
2344 | * changed by use of 'ipfw fwd'. | |
2345 | */ | |
2346 | pkt_dst = next_hop ? next_hop->sin_addr : ip->ip_dst; | |
b0d623f7 A |
2347 | #else |
2348 | pkt_dst = ip->ip_dst; | |
2349 | #endif | |
91447636 | 2350 | |
1c79356b A |
2351 | #if DIAGNOSTIC |
2352 | if (ipprintfs) | |
2353 | printf("forward: src %lx dst %lx ttl %x\n", | |
b0d623f7 | 2354 | (u_int32_t)ip->ip_src.s_addr, (u_int32_t)pkt_dst.s_addr, |
1c79356b A |
2355 | ip->ip_ttl); |
2356 | #endif | |
2357 | ||
91447636 | 2358 | if (m->m_flags & (M_BCAST|M_MCAST) || in_canforward(pkt_dst) == 0) { |
b0d623f7 | 2359 | OSAddAtomic(1, &ipstat.ips_cantforward); |
1c79356b A |
2360 | m_freem(m); |
2361 | return; | |
2362 | } | |
9bccf70c A |
2363 | #if IPSTEALTH |
2364 | if (!ipstealth) { | |
2365 | #endif | |
2366 | if (ip->ip_ttl <= IPTTLDEC) { | |
2367 | icmp_error(m, ICMP_TIMXCEED, ICMP_TIMXCEED_INTRANS, | |
2368 | dest, 0); | |
2369 | return; | |
2370 | } | |
2371 | #if IPSTEALTH | |
1c79356b A |
2372 | } |
2373 | #endif | |
2374 | ||
b0d623f7 A |
2375 | #if PF |
2376 | pf_mtag = pf_find_mtag(m); | |
2377 | if (pf_mtag != NULL && pf_mtag->rtableid != IFSCOPE_NONE) | |
6d2010ae | 2378 | ipoa.ipoa_boundif = pf_mtag->rtableid; |
b0d623f7 A |
2379 | #endif /* PF */ |
2380 | ||
2381 | ip_fwd_route_copyout(ifp, &fwd_rt); | |
2382 | ||
2383 | sin = (struct sockaddr_in *)&fwd_rt.ro_dst; | |
2384 | if (fwd_rt.ro_rt == NULL || | |
2385 | fwd_rt.ro_rt->generation_id != route_generation || | |
2386 | pkt_dst.s_addr != sin->sin_addr.s_addr) { | |
2387 | if (fwd_rt.ro_rt != NULL) { | |
2388 | rtfree(fwd_rt.ro_rt); | |
2389 | fwd_rt.ro_rt = NULL; | |
1c79356b A |
2390 | } |
2391 | sin->sin_family = AF_INET; | |
b0d623f7 | 2392 | sin->sin_len = sizeof (*sin); |
91447636 | 2393 | sin->sin_addr = pkt_dst; |
1c79356b | 2394 | |
6d2010ae | 2395 | rtalloc_scoped_ign(&fwd_rt, RTF_PRCLONING, ipoa.ipoa_boundif); |
b0d623f7 | 2396 | if (fwd_rt.ro_rt == NULL) { |
1c79356b | 2397 | icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_HOST, dest, 0); |
b0d623f7 | 2398 | goto done; |
1c79356b | 2399 | } |
1c79356b | 2400 | } |
b0d623f7 | 2401 | rt = fwd_rt.ro_rt; |
1c79356b A |
2402 | |
2403 | /* | |
9bccf70c A |
2404 | * Save the IP header and at most 8 bytes of the payload, |
2405 | * in case we need to generate an ICMP message to the src. | |
2406 | * | |
2407 | * We don't use m_copy() because it might return a reference | |
2408 | * to a shared cluster. Both this function and ip_output() | |
2409 | * assume exclusive access to the IP header in `m', so any | |
2410 | * data in a cluster may change before we reach icmp_error(). | |
1c79356b | 2411 | */ |
9bccf70c A |
2412 | MGET(mcopy, M_DONTWAIT, m->m_type); |
2413 | if (mcopy != NULL) { | |
2414 | M_COPY_PKTHDR(mcopy, m); | |
2415 | mcopy->m_len = imin((IP_VHL_HL(ip->ip_vhl) << 2) + 8, | |
2416 | (int)ip->ip_len); | |
2417 | m_copydata(m, 0, mcopy->m_len, mtod(mcopy, caddr_t)); | |
2418 | } | |
2419 | ||
2420 | #if IPSTEALTH | |
2421 | if (!ipstealth) { | |
2422 | #endif | |
2423 | ip->ip_ttl -= IPTTLDEC; | |
2424 | #if IPSTEALTH | |
2425 | } | |
2426 | #endif | |
1c79356b A |
2427 | |
2428 | /* | |
2429 | * If forwarding packet using same interface that it came in on, | |
2430 | * perhaps should send a redirect to sender to shortcut a hop. | |
2431 | * Only send redirect if source is sending directly to us, | |
2432 | * and if packet was not source routed (or has any options). | |
2433 | * Also, don't send redirect if forwarding using a default route | |
2434 | * or a route modified by a redirect. | |
2435 | */ | |
b0d623f7 | 2436 | RT_LOCK_SPIN(rt); |
1c79356b A |
2437 | if (rt->rt_ifp == m->m_pkthdr.rcvif && |
2438 | (rt->rt_flags & (RTF_DYNAMIC|RTF_MODIFIED)) == 0 && | |
2439 | satosin(rt_key(rt))->sin_addr.s_addr != 0 && | |
6d2010ae A |
2440 | ipsendredirects && !srcrt && rt->rt_ifa != NULL) { |
2441 | struct in_ifaddr *ia = (struct in_ifaddr *)rt->rt_ifa; | |
b0d623f7 | 2442 | u_int32_t src = ntohl(ip->ip_src.s_addr); |
1c79356b | 2443 | |
6d2010ae A |
2444 | /* Become a regular mutex */ |
2445 | RT_CONVERT_LOCK(rt); | |
2446 | IFA_LOCK_SPIN(&ia->ia_ifa); | |
2447 | if ((src & ia->ia_subnetmask) == ia->ia_subnet) { | |
2448 | if (rt->rt_flags & RTF_GATEWAY) | |
2449 | dest = satosin(rt->rt_gateway)->sin_addr.s_addr; | |
2450 | else | |
2451 | dest = pkt_dst.s_addr; | |
2452 | /* Router requirements says to only send host redirects */ | |
2453 | type = ICMP_REDIRECT; | |
2454 | code = ICMP_REDIRECT_HOST; | |
1c79356b | 2455 | #if DIAGNOSTIC |
6d2010ae A |
2456 | if (ipprintfs) |
2457 | printf("redirect (%d) to %lx\n", code, (u_int32_t)dest); | |
1c79356b A |
2458 | #endif |
2459 | } | |
6d2010ae | 2460 | IFA_UNLOCK(&ia->ia_ifa); |
1c79356b | 2461 | } |
b0d623f7 | 2462 | RT_UNLOCK(rt); |
1c79356b | 2463 | |
b0d623f7 | 2464 | #if IPFIREWALL |
91447636 A |
2465 | if (next_hop) { |
2466 | /* Pass IPFORWARD info if available */ | |
2467 | struct m_tag *tag; | |
2468 | struct ip_fwd_tag *ipfwd_tag; | |
b0d623f7 | 2469 | |
6d2010ae | 2470 | tag = m_tag_create(KERNEL_MODULE_TAG_ID, |
b0d623f7 | 2471 | KERNEL_TAG_TYPE_IPFORWARD, |
6d2010ae | 2472 | sizeof (*ipfwd_tag), M_NOWAIT, m); |
91447636 A |
2473 | if (tag == NULL) { |
2474 | error = ENOBUFS; | |
2475 | m_freem(m); | |
b0d623f7 | 2476 | goto done; |
91447636 | 2477 | } |
b0d623f7 | 2478 | |
91447636 A |
2479 | ipfwd_tag = (struct ip_fwd_tag *)(tag+1); |
2480 | ipfwd_tag->next_hop = next_hop; | |
2481 | ||
2482 | m_tag_prepend(m, tag); | |
2483 | } | |
b0d623f7 A |
2484 | #endif |
2485 | error = ip_output_list(m, 0, NULL, &fwd_rt, | |
2486 | IP_FORWARDING | IP_OUTARGS, 0, &ipoa); | |
2487 | ||
2488 | /* Refresh rt since the route could have changed while in IP */ | |
2489 | rt = fwd_rt.ro_rt; | |
2490 | ||
2491 | if (error) { | |
2492 | OSAddAtomic(1, &ipstat.ips_cantforward); | |
2493 | } else { | |
2494 | OSAddAtomic(1, &ipstat.ips_forward); | |
1c79356b | 2495 | if (type) |
b0d623f7 | 2496 | OSAddAtomic(1, &ipstat.ips_redirectsent); |
1c79356b A |
2497 | else { |
2498 | if (mcopy) { | |
b0d623f7 A |
2499 | /* |
2500 | * If we didn't have to go thru ipflow and | |
2501 | * the packet was successfully consumed by | |
2502 | * ip_output, the mcopy is rather a waste; | |
2503 | * this could be further optimized. | |
2504 | */ | |
1c79356b A |
2505 | m_freem(mcopy); |
2506 | } | |
b0d623f7 | 2507 | goto done; |
1c79356b A |
2508 | } |
2509 | } | |
2510 | if (mcopy == NULL) | |
b0d623f7 | 2511 | goto done; |
1c79356b A |
2512 | |
2513 | switch (error) { | |
2514 | ||
2515 | case 0: /* forwarded, but need redirect */ | |
2516 | /* type, code set above */ | |
2517 | break; | |
2518 | ||
2519 | case ENETUNREACH: /* shouldn't happen, checked above */ | |
2520 | case EHOSTUNREACH: | |
2521 | case ENETDOWN: | |
2522 | case EHOSTDOWN: | |
2523 | default: | |
2524 | type = ICMP_UNREACH; | |
2525 | code = ICMP_UNREACH_HOST; | |
2526 | break; | |
2527 | ||
2528 | case EMSGSIZE: | |
2529 | type = ICMP_UNREACH; | |
2530 | code = ICMP_UNREACH_NEEDFRAG; | |
2531 | #ifndef IPSEC | |
b0d623f7 A |
2532 | if (rt != NULL) { |
2533 | RT_LOCK_SPIN(rt); | |
2534 | if (rt->rt_ifp != NULL) | |
2535 | nextmtu = rt->rt_ifp->if_mtu; | |
2536 | RT_UNLOCK(rt); | |
2537 | } | |
1c79356b A |
2538 | #else |
2539 | /* | |
2540 | * If the packet is routed over IPsec tunnel, tell the | |
2541 | * originator the tunnel MTU. | |
2542 | * tunnel MTU = if MTU - sizeof(IP) - ESP/AH hdrsiz | |
2543 | * XXX quickhack!!! | |
2544 | */ | |
b0d623f7 | 2545 | if (rt != NULL) { |
1c79356b A |
2546 | struct secpolicy *sp = NULL; |
2547 | int ipsecerror; | |
2548 | int ipsechdr; | |
2549 | struct route *ro; | |
2550 | ||
b0d623f7 A |
2551 | RT_LOCK_SPIN(rt); |
2552 | if (rt->rt_ifp != NULL) | |
2553 | nextmtu = rt->rt_ifp->if_mtu; | |
2554 | RT_UNLOCK(rt); | |
2555 | ||
9bccf70c | 2556 | if (ipsec_bypass) { |
b0d623f7 | 2557 | OSAddAtomic(1, &ipstat.ips_cantfrag); |
9bccf70c A |
2558 | break; |
2559 | } | |
1c79356b | 2560 | sp = ipsec4_getpolicybyaddr(mcopy, |
9bccf70c | 2561 | IPSEC_DIR_OUTBOUND, |
1c79356b A |
2562 | IP_FORWARDING, |
2563 | &ipsecerror); | |
2564 | ||
b0d623f7 | 2565 | if (sp != NULL) { |
1c79356b | 2566 | /* count IPsec header size */ |
2d21ac55 | 2567 | ipsechdr = ipsec_hdrsiz(sp); |
1c79356b A |
2568 | |
2569 | /* | |
2570 | * find the correct route for outer IPv4 | |
2571 | * header, compute tunnel MTU. | |
1c79356b | 2572 | */ |
b0d623f7 | 2573 | nextmtu = 0; |
2d21ac55 A |
2574 | |
2575 | if (sp->req != NULL) { | |
2576 | if (sp->req->saidx.mode == IPSEC_MODE_TUNNEL) { | |
2577 | struct secasindex saidx; | |
2578 | struct ip *ipm; | |
2579 | struct secasvar *sav; | |
b0d623f7 | 2580 | |
2d21ac55 A |
2581 | ipm = mtod(mcopy, struct ip *); |
2582 | bcopy(&sp->req->saidx, &saidx, sizeof(saidx)); | |
2583 | saidx.mode = sp->req->saidx.mode; | |
2584 | saidx.reqid = sp->req->saidx.reqid; | |
2585 | sin = (struct sockaddr_in *)&saidx.src; | |
2586 | if (sin->sin_len == 0) { | |
2587 | sin->sin_len = sizeof(*sin); | |
2588 | sin->sin_family = AF_INET; | |
2589 | sin->sin_port = IPSEC_PORT_ANY; | |
2590 | bcopy(&ipm->ip_src, &sin->sin_addr, | |
2591 | sizeof(sin->sin_addr)); | |
2592 | } | |
2593 | sin = (struct sockaddr_in *)&saidx.dst; | |
2594 | if (sin->sin_len == 0) { | |
2595 | sin->sin_len = sizeof(*sin); | |
2596 | sin->sin_family = AF_INET; | |
2597 | sin->sin_port = IPSEC_PORT_ANY; | |
2598 | bcopy(&ipm->ip_dst, &sin->sin_addr, | |
2599 | sizeof(sin->sin_addr)); | |
2600 | } | |
2601 | sav = key_allocsa_policy(&saidx); | |
2602 | if (sav != NULL) { | |
6d2010ae | 2603 | lck_mtx_lock(sadb_mutex); |
2d21ac55 A |
2604 | if (sav->sah != NULL) { |
2605 | ro = &sav->sah->sa_route; | |
b0d623f7 A |
2606 | if (ro->ro_rt != NULL) { |
2607 | RT_LOCK(ro->ro_rt); | |
2608 | if (ro->ro_rt->rt_ifp != NULL) { | |
2609 | nextmtu = ro->ro_rt->rt_ifp->if_mtu; | |
2610 | nextmtu -= ipsechdr; | |
2611 | } | |
2612 | RT_UNLOCK(ro->ro_rt); | |
2d21ac55 A |
2613 | } |
2614 | } | |
6d2010ae A |
2615 | key_freesav(sav, KEY_SADB_LOCKED); |
2616 | lck_mtx_unlock(sadb_mutex); | |
2d21ac55 | 2617 | } |
1c79356b A |
2618 | } |
2619 | } | |
2d21ac55 | 2620 | key_freesp(sp, KEY_SADB_UNLOCKED); |
1c79356b A |
2621 | } |
2622 | } | |
2623 | #endif /*IPSEC*/ | |
b0d623f7 | 2624 | OSAddAtomic(1, &ipstat.ips_cantfrag); |
1c79356b A |
2625 | break; |
2626 | ||
2627 | case ENOBUFS: | |
2628 | type = ICMP_SOURCEQUENCH; | |
2629 | code = 0; | |
2630 | break; | |
9bccf70c A |
2631 | |
2632 | case EACCES: /* ipfw denied packet */ | |
2633 | m_freem(mcopy); | |
b0d623f7 | 2634 | goto done; |
1c79356b | 2635 | } |
b0d623f7 A |
2636 | |
2637 | icmp_error(mcopy, type, code, dest, nextmtu); | |
2638 | done: | |
2639 | ip_fwd_route_copyin(ifp, &fwd_rt); | |
1c79356b A |
2640 | } |
2641 | ||
6d2010ae | 2642 | int |
91447636 | 2643 | ip_savecontrol( |
2d21ac55 A |
2644 | struct inpcb *inp, |
2645 | struct mbuf **mp, | |
2646 | struct ip *ip, | |
2647 | struct mbuf *m) | |
1c79356b | 2648 | { |
6d2010ae | 2649 | *mp = NULL; |
1c79356b A |
2650 | if (inp->inp_socket->so_options & SO_TIMESTAMP) { |
2651 | struct timeval tv; | |
2652 | ||
2653 | microtime(&tv); | |
6d2010ae A |
2654 | mp = sbcreatecontrol_mbuf((caddr_t) &tv, sizeof(tv), |
2655 | SCM_TIMESTAMP, SOL_SOCKET, mp); | |
2656 | if (*mp == NULL) { | |
2657 | goto no_mbufs; | |
2658 | } | |
1c79356b | 2659 | } |
6d2010ae A |
2660 | if ((inp->inp_socket->so_options & SO_TIMESTAMP_MONOTONIC) != 0) { |
2661 | uint64_t time; | |
2662 | ||
2663 | time = mach_absolute_time(); | |
2664 | mp = sbcreatecontrol_mbuf((caddr_t) &time, sizeof(time), | |
2665 | SCM_TIMESTAMP_MONOTONIC, SOL_SOCKET, mp); | |
2666 | ||
2667 | if (*mp == NULL) { | |
2668 | goto no_mbufs; | |
2669 | } | |
2670 | } | |
1c79356b | 2671 | if (inp->inp_flags & INP_RECVDSTADDR) { |
6d2010ae A |
2672 | mp = sbcreatecontrol_mbuf((caddr_t) &ip->ip_dst, |
2673 | sizeof(struct in_addr), IP_RECVDSTADDR, IPPROTO_IP, mp); | |
2674 | if (*mp == NULL) { | |
2675 | goto no_mbufs; | |
2676 | } | |
1c79356b A |
2677 | } |
2678 | #ifdef notyet | |
2679 | /* XXX | |
2680 | * Moving these out of udp_input() made them even more broken | |
2681 | * than they already were. | |
2682 | */ | |
2683 | /* options were tossed already */ | |
2684 | if (inp->inp_flags & INP_RECVOPTS) { | |
6d2010ae A |
2685 | mp = sbcreatecontrol_mbuf((caddr_t) opts_deleted_above, |
2686 | sizeof(struct in_addr), IP_RECVOPTS, IPPROTO_IP, mp); | |
2687 | if (*mp == NULL) { | |
2688 | goto no_mbufs; | |
2689 | } | |
1c79356b A |
2690 | } |
2691 | /* ip_srcroute doesn't do what we want here, need to fix */ | |
2692 | if (inp->inp_flags & INP_RECVRETOPTS) { | |
6d2010ae A |
2693 | mp = sbcreatecontrol_mbuf((caddr_t) ip_srcroute(), |
2694 | sizeof(struct in_addr), IP_RECVRETOPTS, IPPROTO_IP, mp); | |
2695 | if (*mp == NULL) { | |
2696 | goto no_mbufs; | |
2697 | } | |
1c79356b A |
2698 | } |
2699 | #endif | |
2700 | if (inp->inp_flags & INP_RECVIF) { | |
2701 | struct ifnet *ifp; | |
2702 | struct sdlbuf { | |
2703 | struct sockaddr_dl sdl; | |
2704 | u_char pad[32]; | |
2705 | } sdlbuf; | |
2706 | struct sockaddr_dl *sdp; | |
2707 | struct sockaddr_dl *sdl2 = &sdlbuf.sdl; | |
2708 | ||
91447636 | 2709 | ifnet_head_lock_shared(); |
6d2010ae A |
2710 | if ((ifp = m->m_pkthdr.rcvif) != NULL && |
2711 | ifp->if_index && (ifp->if_index <= if_index)) { | |
13fec989 | 2712 | struct ifaddr *ifa = ifnet_addrs[ifp->if_index - 1]; |
2d21ac55 | 2713 | |
13fec989 A |
2714 | if (!ifa || !ifa->ifa_addr) |
2715 | goto makedummy; | |
2d21ac55 | 2716 | |
6d2010ae | 2717 | IFA_LOCK_SPIN(ifa); |
13fec989 | 2718 | sdp = (struct sockaddr_dl *)ifa->ifa_addr; |
1c79356b A |
2719 | /* |
2720 | * Change our mind and don't try copy. | |
2721 | */ | |
6d2010ae A |
2722 | if ((sdp->sdl_family != AF_LINK) || |
2723 | (sdp->sdl_len > sizeof(sdlbuf))) { | |
2724 | IFA_UNLOCK(ifa); | |
1c79356b A |
2725 | goto makedummy; |
2726 | } | |
2727 | bcopy(sdp, sdl2, sdp->sdl_len); | |
6d2010ae | 2728 | IFA_UNLOCK(ifa); |
1c79356b | 2729 | } else { |
6d2010ae | 2730 | makedummy: |
1c79356b A |
2731 | sdl2->sdl_len |
2732 | = offsetof(struct sockaddr_dl, sdl_data[0]); | |
2733 | sdl2->sdl_family = AF_LINK; | |
2734 | sdl2->sdl_index = 0; | |
2735 | sdl2->sdl_nlen = sdl2->sdl_alen = sdl2->sdl_slen = 0; | |
2736 | } | |
91447636 | 2737 | ifnet_head_done(); |
6d2010ae A |
2738 | mp = sbcreatecontrol_mbuf((caddr_t) sdl2, sdl2->sdl_len, |
2739 | IP_RECVIF, IPPROTO_IP, mp); | |
2740 | if (*mp == NULL) { | |
2741 | goto no_mbufs; | |
2742 | } | |
1c79356b | 2743 | } |
55e303ae | 2744 | if (inp->inp_flags & INP_RECVTTL) { |
6d2010ae A |
2745 | mp = sbcreatecontrol_mbuf((caddr_t)&ip->ip_ttl, sizeof(ip->ip_ttl), |
2746 | IP_RECVTTL, IPPROTO_IP, mp); | |
2747 | if (*mp == NULL) { | |
2748 | goto no_mbufs; | |
2749 | } | |
2750 | } | |
2751 | if ((inp->inp_socket->so_flags & SOF_RECV_TRAFFIC_CLASS) != 0) { | |
2752 | int tc = m->m_pkthdr.prio; | |
2753 | ||
2754 | mp = sbcreatecontrol_mbuf((caddr_t) &tc, sizeof(tc), | |
2755 | SO_TRAFFIC_CLASS, SOL_SOCKET, mp); | |
2756 | if (*mp == NULL) { | |
2757 | goto no_mbufs; | |
2758 | } | |
2759 | } | |
2760 | if (inp->inp_flags & INP_PKTINFO) { | |
2761 | struct in_pktinfo pi; | |
2762 | ||
2763 | bzero(&pi, sizeof(struct in_pktinfo)); | |
2764 | bcopy(&ip->ip_dst, &pi.ipi_addr, sizeof(struct in_addr)); | |
2765 | pi.ipi_ifindex = (m && m->m_pkthdr.rcvif) ? m->m_pkthdr.rcvif->if_index : 0; | |
2766 | ||
2767 | mp = sbcreatecontrol_mbuf((caddr_t)&pi, sizeof(struct in_pktinfo), | |
2768 | IP_RECVPKTINFO, IPPROTO_IP, mp); | |
2769 | if (*mp == NULL) { | |
2770 | goto no_mbufs; | |
2771 | } | |
55e303ae | 2772 | } |
6d2010ae A |
2773 | return 0; |
2774 | ||
2775 | no_mbufs: | |
2776 | ipstat.ips_pktdropcntrl++; | |
2777 | return ENOBUFS; | |
1c79356b A |
2778 | } |
2779 | ||
2780 | int | |
2781 | ip_rsvp_init(struct socket *so) | |
2782 | { | |
2783 | if (so->so_type != SOCK_RAW || | |
2784 | so->so_proto->pr_protocol != IPPROTO_RSVP) | |
2785 | return EOPNOTSUPP; | |
2786 | ||
2787 | if (ip_rsvpd != NULL) | |
2788 | return EADDRINUSE; | |
2789 | ||
2790 | ip_rsvpd = so; | |
2791 | /* | |
2792 | * This may seem silly, but we need to be sure we don't over-increment | |
2793 | * the RSVP counter, in case something slips up. | |
2794 | */ | |
2795 | if (!ip_rsvp_on) { | |
2796 | ip_rsvp_on = 1; | |
2797 | rsvp_on++; | |
2798 | } | |
2799 | ||
2800 | return 0; | |
2801 | } | |
2802 | ||
2803 | int | |
2804 | ip_rsvp_done(void) | |
2805 | { | |
2806 | ip_rsvpd = NULL; | |
2807 | /* | |
2808 | * This may seem silly, but we need to be sure we don't over-decrement | |
2809 | * the RSVP counter, in case something slips up. | |
2810 | */ | |
2811 | if (ip_rsvp_on) { | |
2812 | ip_rsvp_on = 0; | |
2813 | rsvp_on--; | |
2814 | } | |
2815 | return 0; | |
2816 | } |