]> git.saurik.com Git - apple/xnu.git/blame - bsd/netinet/ip_output.c
xnu-4570.51.1.tar.gz
[apple/xnu.git] / bsd / netinet / ip_output.c
CommitLineData
1c79356b 1/*
5ba3f43e 2 * Copyright (c) 2000-2017 Apple Inc. All rights reserved.
5d5c5d0d 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
39236c6e 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.
39236c6e 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.
39236c6e 17 *
2d21ac55
A
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.
39236c6e 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
27 */
28/*
29 * Copyright (c) 1982, 1986, 1988, 1990, 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_output.c 8.3 (Berkeley) 1/21/94
61 */
2d21ac55
A
62/*
63 * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
64 * support for mandatory and extensible security protections. This notice
65 * is included in support of clause 2.2 (b) of the Apple Public License,
66 * Version 2.0.
67 */
1c79356b 68
39236c6e 69#define _IP_VHL
1c79356b 70
1c79356b
A
71#include <sys/param.h>
72#include <sys/systm.h>
73#include <sys/kernel.h>
74#include <sys/malloc.h>
75#include <sys/mbuf.h>
76#include <sys/protosw.h>
77#include <sys/socket.h>
78#include <sys/socketvar.h>
91447636
A
79#include <kern/locks.h>
80#include <sys/sysctl.h>
6d2010ae 81#include <sys/mcache.h>
39236c6e 82#include <sys/kdebug.h>
1c79356b 83
b0d623f7 84#include <machine/endian.h>
6d2010ae 85#include <pexpert/pexpert.h>
39236c6e
A
86#include <mach/sdt.h>
87
88#include <libkern/OSAtomic.h>
89#include <libkern/OSByteOrder.h>
b0d623f7 90
1c79356b 91#include <net/if.h>
c910b4d9 92#include <net/if_dl.h>
6d2010ae 93#include <net/if_types.h>
1c79356b 94#include <net/route.h>
6d2010ae
A
95#include <net/ntstat.h>
96#include <net/net_osdep.h>
39236c6e 97#include <net/dlil.h>
3e170ce0 98#include <net/net_perf.h>
1c79356b
A
99
100#include <netinet/in.h>
101#include <netinet/in_systm.h>
102#include <netinet/ip.h>
1c79356b
A
103#include <netinet/in_pcb.h>
104#include <netinet/in_var.h>
105#include <netinet/ip_var.h>
91447636 106#include <netinet/kpi_ipfilter_var.h>
39037602 107#include <netinet/in_tclass.h>
91447636 108
2d21ac55
A
109#if CONFIG_MACF_NET
110#include <security/mac_framework.h>
39236c6e 111#endif /* CONFIG_MACF_NET */
1c79356b 112
39236c6e
A
113#define DBG_LAYER_BEG NETDBG_CODE(DBG_NETIP, 1)
114#define DBG_LAYER_END NETDBG_CODE(DBG_NETIP, 3)
115#define DBG_FNC_IP_OUTPUT NETDBG_CODE(DBG_NETIP, (1 << 8) | 1)
116#define DBG_FNC_IPSEC4_OUTPUT NETDBG_CODE(DBG_NETIP, (2 << 8) | 1)
1c79356b 117
1c79356b
A
118#if IPSEC
119#include <netinet6/ipsec.h>
120#include <netkey/key.h>
9bccf70c 121#if IPSEC_DEBUG
1c79356b 122#include <netkey/key_debug.h>
1c79356b 123#else
39236c6e 124#define KEYDEBUG(lev, arg)
1c79356b 125#endif
39236c6e 126#endif /* IPSEC */
1c79356b 127
fe8ab488
A
128#if NECP
129#include <net/necp.h>
130#endif /* NECP */
131
39236c6e 132#if IPFIREWALL
1c79356b 133#include <netinet/ip_fw.h>
39236c6e 134#if IPDIVERT
91447636 135#include <netinet/ip_divert.h>
39236c6e
A
136#endif /* IPDIVERT */
137#endif /* IPFIREWALL */
1c79356b
A
138
139#if DUMMYNET
140#include <netinet/ip_dummynet.h>
141#endif
142
b0d623f7
A
143#if PF
144#include <net/pfvar.h>
145#endif /* PF */
146
39236c6e
A
147#if IPFIREWALL_FORWARD && IPFIREWALL_FORWARD_DEBUG
148#define print_ip(a) \
149 printf("%ld.%ld.%ld.%ld", (ntohl(a.s_addr) >> 24) & 0xFF, \
150 (ntohl(a.s_addr) >> 16) & 0xFF, \
151 (ntohl(a.s_addr) >> 8) & 0xFF, \
152 (ntohl(a.s_addr)) & 0xFF);
153#endif /* IPFIREWALL_FORWARD && IPFIREWALL_FORWARD_DEBUG */
1c79356b
A
154
155u_short ip_id;
156
3e170ce0
A
157static int sysctl_reset_ip_output_stats SYSCTL_HANDLER_ARGS;
158static int sysctl_ip_output_measure_bins SYSCTL_HANDLER_ARGS;
159static int sysctl_ip_output_getperf SYSCTL_HANDLER_ARGS;
2d21ac55 160static void ip_out_cksum_stats(int, u_int32_t);
39236c6e
A
161static struct mbuf *ip_insertoptions(struct mbuf *, struct mbuf *, int *);
162static int ip_optcopy(struct ip *, struct ip *);
163static int ip_pcbopts(int, struct mbuf **, struct mbuf *);
164static void imo_trace(struct ip_moptions *, int);
165static void ip_mloopback(struct ifnet *, struct ifnet *, struct mbuf *,
166 struct sockaddr_in *, int);
c910b4d9 167static struct ifaddr *in_selectsrcif(struct ip *, struct route *, unsigned int);
2d21ac55 168
9bccf70c
A
169extern struct ip_linklocal_stat ip_linklocal_stat;
170
171/* temporary: for testing */
172#if IPSEC
173extern int ipsec_bypass;
174#endif
175
39236c6e
A
176static int ip_maxchainsent = 0;
177SYSCTL_INT(_net_inet_ip, OID_AUTO, maxchainsent,
178 CTLFLAG_RW | CTLFLAG_LOCKED, &ip_maxchainsent, 0,
179 "use dlil_output_list");
2d21ac55
A
180#if DEBUG
181static int forge_ce = 0;
39236c6e
A
182SYSCTL_INT(_net_inet_ip, OID_AUTO, forge_ce,
183 CTLFLAG_RW | CTLFLAG_LOCKED, &forge_ce, 0,
184 "Forge ECN CE");
2d21ac55 185#endif /* DEBUG */
c910b4d9
A
186
187static int ip_select_srcif_debug = 0;
39236c6e
A
188SYSCTL_INT(_net_inet_ip, OID_AUTO, select_srcif_debug,
189 CTLFLAG_RW | CTLFLAG_LOCKED, &ip_select_srcif_debug, 0,
190 "log source interface selection debug info");
c910b4d9 191
3e170ce0
A
192static int ip_output_measure = 0;
193SYSCTL_PROC(_net_inet_ip, OID_AUTO, output_perf,
194 CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED,
195 &ip_output_measure, 0, sysctl_reset_ip_output_stats, "I",
196 "Do time measurement");
197
198static uint64_t ip_output_measure_bins = 0;
199SYSCTL_PROC(_net_inet_ip, OID_AUTO, output_perf_bins,
200 CTLTYPE_QUAD | CTLFLAG_RW | CTLFLAG_LOCKED, &ip_output_measure_bins, 0,
201 sysctl_ip_output_measure_bins, "I",
202 "bins for chaining performance data histogram");
203
204static net_perf_t net_perf;
205SYSCTL_PROC(_net_inet_ip, OID_AUTO, output_perf_data,
206 CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_LOCKED,
207 0, 0, sysctl_ip_output_getperf, "S,net_perf",
208 "IP output performance data (struct net_perf, net/net_perf.h)");
209
5ba3f43e
A
210__private_extern__ int rfc6864 = 1;
211SYSCTL_INT(_net_inet_ip, OID_AUTO, rfc6864, CTLFLAG_RW | CTLFLAG_LOCKED,
212 &rfc6864, 0, "updated ip id field behavior");
213
6d2010ae
A
214#define IMO_TRACE_HIST_SIZE 32 /* size of trace history */
215
216/* For gdb */
217__private_extern__ unsigned int imo_trace_hist_size = IMO_TRACE_HIST_SIZE;
218
219struct ip_moptions_dbg {
220 struct ip_moptions imo; /* ip_moptions */
221 u_int16_t imo_refhold_cnt; /* # of IMO_ADDREF */
222 u_int16_t imo_refrele_cnt; /* # of IMO_REMREF */
223 /*
224 * Alloc and free callers.
225 */
226 ctrace_t imo_alloc;
227 ctrace_t imo_free;
228 /*
229 * Circular lists of IMO_ADDREF and IMO_REMREF callers.
230 */
231 ctrace_t imo_refhold[IMO_TRACE_HIST_SIZE];
232 ctrace_t imo_refrele[IMO_TRACE_HIST_SIZE];
233};
234
235#if DEBUG
236static unsigned int imo_debug = 1; /* debugging (enabled) */
237#else
238static unsigned int imo_debug; /* debugging (disabled) */
239#endif /* !DEBUG */
240static unsigned int imo_size; /* size of zone element */
241static struct zone *imo_zone; /* zone for ip_moptions */
242
243#define IMO_ZONE_MAX 64 /* maximum elements in zone */
244#define IMO_ZONE_NAME "ip_moptions" /* zone name */
245
1c79356b
A
246/*
247 * IP output. The packet in mbuf chain m contains a skeletal IP
248 * header (with len, off, ttl, proto, tos, src, dst).
249 * The mbuf chain containing the packet will be freed.
250 * The mbuf opt, if present, will not be freed.
251 */
252int
39236c6e
A
253ip_output(struct mbuf *m0, struct mbuf *opt, struct route *ro, int flags,
254 struct ip_moptions *imo, struct ip_out_args *ipoa)
91447636 255{
39236c6e 256 return (ip_output_list(m0, 0, opt, ro, flags, imo, ipoa));
91447636
A
257}
258
2d21ac55 259/*
39236c6e
A
260 * IP output. The packet in mbuf chain m contains a skeletal IP
261 * header (with len, off, ttl, proto, tos, src, dst).
262 * The mbuf chain containing the packet will be freed.
263 * The mbuf opt, if present, will not be freed.
264 *
265 * Route ro MUST be non-NULL; if ro->ro_rt is valid, route lookup would be
266 * skipped and ro->ro_rt would be used. Otherwise the result of route
267 * lookup is stored in ro->ro_rt.
2d21ac55 268 *
39236c6e
A
269 * In the IP forwarding case, the packet will arrive with options already
270 * inserted, so must have a NULL opt pointer.
2d21ac55 271 */
91447636 272int
39236c6e
A
273ip_output_list(struct mbuf *m0, int packetchain, struct mbuf *opt,
274 struct route *ro, int flags, struct ip_moptions *imo,
275 struct ip_out_args *ipoa)
1c79356b 276{
b0d623f7 277 struct ip *ip;
39236c6e 278 struct ifnet *ifp = NULL; /* not refcnt'd */
316670eb 279 struct mbuf *m = m0, *prevnxt = NULL, **mppn = &prevnxt;
1c79356b 280 int hlen = sizeof (struct ip);
6d2010ae 281 int len = 0, error = 0;
55e303ae 282 struct sockaddr_in *dst = NULL;
b0d623f7 283 struct in_ifaddr *ia = NULL, *src_ia = NULL;
91447636 284 struct in_addr pkt_dst;
39236c6e
A
285 struct ipf_pktopts *ippo = NULL;
286 ipfilter_t inject_filter_ref = NULL;
287 struct mbuf *packetlist;
288 uint32_t sw_csum, pktcnt = 0, scnt = 0, bytecnt = 0;
3e170ce0 289 uint32_t packets_processed = 0;
39236c6e
A
290 unsigned int ifscope = IFSCOPE_NONE;
291 struct flowadv *adv = NULL;
3e170ce0 292 struct timeval start_tv;
1c79356b 293#if IPSEC
9bccf70c 294 struct socket *so = NULL;
1c79356b 295 struct secpolicy *sp = NULL;
39236c6e 296#endif /* IPSEC */
fe8ab488
A
297#if NECP
298 necp_kernel_policy_result necp_result = 0;
299 necp_kernel_policy_result_parameter necp_result_parameter;
300 necp_kernel_policy_id necp_matched_policy_id = 0;
301#endif /* NECP */
4a3eedf9 302#if IPFIREWALL
39236c6e 303 int ipfwoff;
316670eb 304 struct sockaddr_in *next_hop_from_ipfwd_tag = NULL;
39236c6e 305#endif /* IPFIREWALL */
316670eb 306#if IPFIREWALL || DUMMYNET
39236c6e
A
307 struct m_tag *tag;
308#endif /* IPFIREWALL || DUMMYNET */
6d2010ae 309#if DUMMYNET
c910b4d9 310 struct ip_out_args saved_ipoa;
6d2010ae
A
311 struct sockaddr_in dst_buf;
312#endif /* DUMMYNET */
39236c6e 313 struct {
ebb1b9f4 314#if IPSEC
39236c6e 315 struct ipsec_output_state ipsec_state;
ebb1b9f4 316#endif /* IPSEC */
fe8ab488
A
317#if NECP
318 struct route necp_route;
319#endif /* NECP */
39236c6e
A
320#if IPFIREWALL || DUMMYNET
321 struct ip_fw_args args;
322#endif /* IPFIREWALL || DUMMYNET */
323#if IPFIREWALL_FORWARD
324 struct route sro_fwd;
325#endif /* IPFIREWALL_FORWARD */
326#if DUMMYNET
327 struct route saved_route;
328#endif /* DUMMYNET */
329 struct ipf_pktopts ipf_pktopts;
330 } ipobz;
331#define ipsec_state ipobz.ipsec_state
fe8ab488 332#define necp_route ipobz.necp_route
39236c6e
A
333#define args ipobz.args
334#define sro_fwd ipobz.sro_fwd
335#define saved_route ipobz.saved_route
336#define ipf_pktopts ipobz.ipf_pktopts
337 union {
338 struct {
339 boolean_t select_srcif : 1; /* set once */
340 boolean_t srcbound : 1; /* set once */
341 boolean_t nocell : 1; /* set once */
342 boolean_t isbroadcast : 1;
343 boolean_t didfilter : 1;
fe8ab488
A
344 boolean_t noexpensive : 1; /* set once */
345 boolean_t awdl_unrestricted : 1; /* set once */
39236c6e
A
346#if IPFIREWALL_FORWARD
347 boolean_t fwd_rewrite_src : 1;
348#endif /* IPFIREWALL_FORWARD */
349 };
350 uint32_t raw;
351 } ipobf = { .raw = 0 };
352
39037602
A
353/*
354 * Here we check for restrictions when sending frames.
355 * N.B.: IPv4 over internal co-processor interfaces is not allowed.
356 */
fe8ab488
A
357#define IP_CHECK_RESTRICTIONS(_ifp, _ipobf) \
358 (((_ipobf).nocell && IFNET_IS_CELLULAR(_ifp)) || \
359 ((_ipobf).noexpensive && IFNET_IS_EXPENSIVE(_ifp)) || \
39037602 360 (IFNET_IS_INTCOPROC(_ifp)) || \
fe8ab488
A
361 (!(_ipobf).awdl_unrestricted && IFNET_IS_AWDL_RESTRICTED(_ifp)))
362
3e170ce0
A
363 if (ip_output_measure)
364 net_perf_start_time(&net_perf, &start_tv);
39236c6e 365 KERNEL_DEBUG(DBG_FNC_IP_OUTPUT | DBG_FUNC_START, 0, 0, 0, 0, 0);
ebb1b9f4 366
39236c6e 367 VERIFY(m0->m_flags & M_PKTHDR);
91447636 368 packetlist = m0;
b0d623f7 369
39236c6e
A
370 /* zero out {ipsec_state, args, sro_fwd, saved_route, ipf_pktops} */
371 bzero(&ipobz, sizeof (ipobz));
372 ippo = &ipf_pktopts;
373
374#if IPFIREWALL || DUMMYNET
b0d623f7
A
375 if (SLIST_EMPTY(&m0->m_pkthdr.tags))
376 goto ipfw_tags_done;
377
91447636
A
378 /* Grab info from mtags prepended to the chain */
379#if DUMMYNET
b0d623f7
A
380 if ((tag = m_tag_locate(m0, KERNEL_MODULE_TAG_ID,
381 KERNEL_TAG_TYPE_DUMMYNET, NULL)) != NULL) {
91447636 382 struct dn_pkt_tag *dn_tag;
b0d623f7 383
91447636 384 dn_tag = (struct dn_pkt_tag *)(tag+1);
316670eb
A
385 args.fwa_ipfw_rule = dn_tag->dn_ipfw_rule;
386 args.fwa_pf_rule = dn_tag->dn_pf_rule;
91447636 387 opt = NULL;
316670eb 388 saved_route = dn_tag->dn_ro;
2d21ac55 389 ro = &saved_route;
b0d623f7 390
91447636 391 imo = NULL;
39236c6e 392 bcopy(&dn_tag->dn_dst, &dst_buf, sizeof (dst_buf));
6d2010ae 393 dst = &dst_buf;
316670eb
A
394 ifp = dn_tag->dn_ifp;
395 flags = dn_tag->dn_flags;
396 if ((dn_tag->dn_flags & IP_OUTARGS)) {
397 saved_ipoa = dn_tag->dn_ipoa;
398 ipoa = &saved_ipoa;
399 }
b0d623f7 400
91447636
A
401 m_tag_delete(m0, tag);
402 }
403#endif /* DUMMYNET */
404
2d21ac55 405#if IPDIVERT
b0d623f7
A
406 if ((tag = m_tag_locate(m0, KERNEL_MODULE_TAG_ID,
407 KERNEL_TAG_TYPE_DIVERT, NULL)) != NULL) {
91447636 408 struct divert_tag *div_tag;
b0d623f7 409
91447636 410 div_tag = (struct divert_tag *)(tag+1);
316670eb 411 args.fwa_divert_rule = div_tag->cookie;
91447636
A
412
413 m_tag_delete(m0, tag);
414 }
2d21ac55 415#endif /* IPDIVERT */
2d21ac55 416
316670eb 417#if IPFIREWALL
b0d623f7
A
418 if ((tag = m_tag_locate(m0, KERNEL_MODULE_TAG_ID,
419 KERNEL_TAG_TYPE_IPFORWARD, NULL)) != NULL) {
91447636 420 struct ip_fwd_tag *ipfwd_tag;
b0d623f7 421
91447636 422 ipfwd_tag = (struct ip_fwd_tag *)(tag+1);
6d2010ae 423 next_hop_from_ipfwd_tag = ipfwd_tag->next_hop;
316670eb 424
91447636
A
425 m_tag_delete(m0, tag);
426 }
4a3eedf9 427#endif /* IPFIREWALL */
91447636 428
316670eb
A
429ipfw_tags_done:
430#endif /* IPFIREWALL || DUMMYNET */
431
91447636 432 m = m0;
39236c6e 433 m->m_pkthdr.pkt_flags &= ~(PKTF_LOOP|PKTF_IFAINFO);
b0d623f7 434
39236c6e
A
435#if IPSEC
436 if (ipsec_bypass == 0 && !(flags & IP_NOIPSEC)) {
437 /* If packet is bound to an interface, check bound policies */
438 if ((flags & IP_OUTARGS) && (ipoa != NULL) &&
fe8ab488
A
439 (ipoa->ipoa_flags & IPOAF_BOUND_IF) &&
440 ipoa->ipoa_boundif != IFSCOPE_NONE) {
39236c6e 441 if (ipsec4_getpolicybyinterface(m, IPSEC_DIR_OUTBOUND,
fe8ab488 442 &flags, ipoa, &sp) != 0)
39236c6e
A
443 goto bad;
444 }
445 }
446#endif /* IPSEC */
39037602 447
39236c6e 448 VERIFY(ro != NULL);
6d2010ae 449
39037602 450 if (flags & IP_OUTARGS) {
316670eb
A
451 /*
452 * In the forwarding case, only the ifscope value is used,
453 * as source interface selection doesn't take place.
454 */
39236c6e 455 if ((ipobf.select_srcif = (!(flags & IP_FORWARDING) &&
316670eb
A
456 (ipoa->ipoa_flags & IPOAF_SELECT_SRCIF)))) {
457 ipf_pktopts.ippo_flags |= IPPOF_SELECT_SRCIF;
458 }
459
460 if ((ipoa->ipoa_flags & IPOAF_BOUND_IF) &&
461 ipoa->ipoa_boundif != IFSCOPE_NONE) {
462 ifscope = ipoa->ipoa_boundif;
463 ipf_pktopts.ippo_flags |=
464 (IPPOF_BOUND_IF | (ifscope << IPPOF_SHIFT_IFSCOPE));
465 }
466
39236c6e
A
467 /* double negation needed for bool bit field */
468 ipobf.srcbound = !!(ipoa->ipoa_flags & IPOAF_BOUND_SRCADDR);
469 if (ipobf.srcbound)
316670eb 470 ipf_pktopts.ippo_flags |= IPPOF_BOUND_SRCADDR;
c910b4d9 471 } else {
39236c6e
A
472 ipobf.select_srcif = FALSE;
473 ipobf.srcbound = FALSE;
c910b4d9 474 ifscope = IFSCOPE_NONE;
39236c6e
A
475 if (flags & IP_OUTARGS) {
476 ipoa->ipoa_boundif = IFSCOPE_NONE;
477 ipoa->ipoa_flags &= ~(IPOAF_SELECT_SRCIF |
478 IPOAF_BOUND_IF | IPOAF_BOUND_SRCADDR);
479 }
c910b4d9
A
480 }
481
6d2010ae 482 if (flags & IP_OUTARGS) {
fe8ab488
A
483 if (ipoa->ipoa_flags & IPOAF_NO_CELLULAR) {
484 ipobf.nocell = TRUE;
485 ipf_pktopts.ippo_flags |= IPPOF_NO_IFT_CELLULAR;
486 }
487 if (ipoa->ipoa_flags & IPOAF_NO_EXPENSIVE) {
488 ipobf.noexpensive = TRUE;
489 ipf_pktopts.ippo_flags |= IPPOF_NO_IFF_EXPENSIVE;
490 }
491 if (ipoa->ipoa_flags & IPOAF_AWDL_UNRESTRICTED)
492 ipobf.awdl_unrestricted = TRUE;
316670eb
A
493 adv = &ipoa->ipoa_flowadv;
494 adv->code = FADV_SUCCESS;
39236c6e 495 ipoa->ipoa_retflags = 0;
6d2010ae 496 }
39037602 497
fe8ab488
A
498#if IPSEC
499 if (ipsec_bypass == 0 && !(flags & IP_NOIPSEC)) {
500 so = ipsec_getsocket(m);
501 if (so != NULL) {
502 (void) ipsec_setsocket(m, NULL);
503 }
504 }
505#endif /* IPSEC */
6d2010ae 506
316670eb
A
507#if DUMMYNET
508 if (args.fwa_ipfw_rule != NULL || args.fwa_pf_rule != NULL) {
509 /* dummynet already saw us */
b0d623f7 510 ip = mtod(m, struct ip *);
316670eb
A
511 hlen = IP_VHL_HL(ip->ip_vhl) << 2;
512 pkt_dst = ip->ip_dst;
b0d623f7
A
513 if (ro->ro_rt != NULL) {
514 RT_LOCK_SPIN(ro->ro_rt);
515 ia = (struct in_ifaddr *)ro->ro_rt->rt_ifa;
6d2010ae
A
516 if (ia) {
517 /* Become a regular mutex */
518 RT_CONVERT_LOCK(ro->ro_rt);
519 IFA_ADDREF(&ia->ia_ifa);
520 }
b0d623f7
A
521 RT_UNLOCK(ro->ro_rt);
522 }
fe8ab488 523
39236c6e 524#if IPFIREWALL
316670eb
A
525 if (args.fwa_ipfw_rule != NULL)
526 goto skip_ipsec;
39236c6e 527#endif /* IPFIREWALL */
316670eb
A
528 if (args.fwa_pf_rule != NULL)
529 goto sendit;
91447636 530 }
316670eb 531#endif /* DUMMYNET */
91447636 532
39236c6e 533loopit:
3e170ce0 534 packets_processed++;
39236c6e
A
535 ipobf.isbroadcast = FALSE;
536 ipobf.didfilter = FALSE;
537#if IPFIREWALL_FORWARD
538 ipobf.fwd_rewrite_src = FALSE;
539#endif /* IPFIREWALL_FORWARD */
540
541 VERIFY(m->m_flags & M_PKTHDR);
91447636 542 /*
39236c6e 543 * No need to proccess packet twice if we've already seen it.
91447636 544 */
b0d623f7
A
545 if (!SLIST_EMPTY(&m->m_pkthdr.tags))
546 inject_filter_ref = ipf_get_inject_filter(m);
547 else
39236c6e 548 inject_filter_ref = NULL;
1c79356b 549
1c79356b
A
550 if (opt) {
551 m = ip_insertoptions(m, opt, &len);
552 hlen = len;
316670eb
A
553 /* Update the chain */
554 if (m != m0) {
555 if (m0 == packetlist)
556 packetlist = m;
557 m0 = m;
558 }
1c79356b
A
559 }
560 ip = mtod(m, struct ip *);
39236c6e 561
4a3eedf9 562#if IPFIREWALL
6d2010ae
A
563 /*
564 * rdar://8542331
565 *
39236c6e
A
566 * When dealing with a packet chain, we need to reset "next_hop"
567 * because "dst" may have been changed to the gateway address below
568 * for the previous packet of the chain. This could cause the route
569 * to be inavertandly changed to the route to the gateway address
570 * (instead of the route to the destination).
6d2010ae 571 */
316670eb
A
572 args.fwa_next_hop = next_hop_from_ipfwd_tag;
573 pkt_dst = args.fwa_next_hop ? args.fwa_next_hop->sin_addr : ip->ip_dst;
39236c6e 574#else /* !IPFIREWALL */
4a3eedf9 575 pkt_dst = ip->ip_dst;
39236c6e 576#endif /* !IPFIREWALL */
91447636 577
6d2010ae
A
578 /*
579 * We must not send if the packet is destined to network zero.
580 * RFC1122 3.2.1.3 (a) and (b).
581 */
582 if (IN_ZERONET(ntohl(pkt_dst.s_addr))) {
583 error = EHOSTUNREACH;
584 goto bad;
585 }
586
1c79356b
A
587 /*
588 * Fill in IP header.
589 */
39236c6e 590 if (!(flags & (IP_FORWARDING|IP_RAWOUTPUT))) {
1c79356b
A
591 ip->ip_vhl = IP_MAKE_VHL(IPVERSION, hlen >> 2);
592 ip->ip_off &= IP_DF;
5ba3f43e
A
593 if (rfc6864 && IP_OFF_IS_ATOMIC(ip->ip_off)) {
594 // Per RFC6864, value of ip_id is undefined for atomic ip packets
595 ip->ip_id = 0;
596 } else {
597 ip->ip_id = ip_randomid();
598 }
b0d623f7 599 OSAddAtomic(1, &ipstat.ips_localout);
1c79356b
A
600 } else {
601 hlen = IP_VHL_HL(ip->ip_vhl) << 2;
602 }
316670eb 603
2d21ac55
A
604#if DEBUG
605 /* For debugging, we let the stack forge congestion */
606 if (forge_ce != 0 &&
39236c6e
A
607 ((ip->ip_tos & IPTOS_ECN_MASK) == IPTOS_ECN_ECT1 ||
608 (ip->ip_tos & IPTOS_ECN_MASK) == IPTOS_ECN_ECT0)) {
2d21ac55
A
609 ip->ip_tos = (ip->ip_tos & ~IPTOS_ECN_MASK) | IPTOS_ECN_CE;
610 forge_ce--;
611 }
612#endif /* DEBUG */
1c79356b 613
39236c6e
A
614 KERNEL_DEBUG(DBG_LAYER_BEG, ip->ip_dst.s_addr, ip->ip_src.s_addr,
615 ip->ip_p, ip->ip_off, ip->ip_len);
316670eb 616
39236c6e 617 dst = SIN(&ro->ro_dst);
55e303ae 618
1c79356b
A
619 /*
620 * If there is a cached route,
621 * check that it is to the same destination
622 * and is still up. If not, free it and try again.
55e303ae
A
623 * The address family should also be checked in case of sharing the
624 * cache with IPv6.
1c79356b 625 */
55e303ae 626
2d21ac55 627 if (ro->ro_rt != NULL) {
39236c6e
A
628 if (ROUTE_UNUSABLE(ro) && ip->ip_src.s_addr != INADDR_ANY &&
629 !(flags & (IP_ROUTETOIF | IP_FORWARDING))) {
b0d623f7
A
630 src_ia = ifa_foraddr(ip->ip_src.s_addr);
631 if (src_ia == NULL) {
632 error = EADDRNOTAVAIL;
633 goto bad;
634 }
6d2010ae 635 IFA_REMREF(&src_ia->ia_ifa);
39236c6e 636 src_ia = NULL;
91447636 637 }
b0d623f7
A
638 /*
639 * Test rt_flags without holding rt_lock for performance
640 * reasons; if the route is down it will hopefully be
641 * caught by the layer below (since it uses this route
642 * as a hint) or during the next transmit.
643 */
39236c6e
A
644 if (ROUTE_UNUSABLE(ro) || dst->sin_family != AF_INET ||
645 dst->sin_addr.s_addr != pkt_dst.s_addr)
646 ROUTE_RELEASE(ro);
647
c910b4d9
A
648 /*
649 * If we're doing source interface selection, we may not
650 * want to use this route; only synch up the generation
651 * count otherwise.
652 */
39236c6e
A
653 if (!ipobf.select_srcif && ro->ro_rt != NULL &&
654 RT_GENID_OUTOFSYNC(ro->ro_rt))
655 RT_GENID_SYNC(ro->ro_rt);
ab86ba33 656 }
2d21ac55 657 if (ro->ro_rt == NULL) {
39236c6e 658 bzero(dst, sizeof (*dst));
1c79356b 659 dst->sin_family = AF_INET;
39236c6e 660 dst->sin_len = sizeof (*dst);
91447636 661 dst->sin_addr = pkt_dst;
1c79356b
A
662 }
663 /*
664 * If routing to interface only,
665 * short circuit routing lookup.
666 */
1c79356b 667 if (flags & IP_ROUTETOIF) {
39236c6e 668 if (ia != NULL)
6d2010ae 669 IFA_REMREF(&ia->ia_ifa);
39236c6e
A
670 if ((ia = ifatoia(ifa_ifwithdstaddr(sintosa(dst)))) == NULL) {
671 ia = ifatoia(ifa_ifwithnet(sintosa(dst)));
672 if (ia == NULL) {
b0d623f7 673 OSAddAtomic(1, &ipstat.ips_noroute);
91447636 674 error = ENETUNREACH;
39037602 675 /* XXX IPv6 APN fallback notification?? */
91447636
A
676 goto bad;
677 }
1c79356b
A
678 }
679 ifp = ia->ia_ifp;
1c79356b 680 ip->ip_ttl = 1;
39236c6e
A
681 ipobf.isbroadcast = in_broadcast(dst->sin_addr, ifp);
682 /*
683 * For consistency with other cases below. Loopback
684 * multicast case is handled separately by ip_mloopback().
685 */
686 if ((ifp->if_flags & IFF_LOOPBACK) &&
687 !IN_MULTICAST(ntohl(pkt_dst.s_addr))) {
688 m->m_pkthdr.rcvif = ifp;
689 ip_setsrcifaddr_info(m, ifp->if_index, NULL);
690 ip_setdstifaddr_info(m, ifp->if_index, NULL);
691 }
c910b4d9 692 } else if (IN_MULTICAST(ntohl(pkt_dst.s_addr)) &&
6d2010ae 693 imo != NULL && (ifp = imo->imo_multicast_ifp) != NULL) {
c910b4d9
A
694 /*
695 * Bypass the normal routing lookup for multicast
696 * packets if the interface is specified.
697 */
39236c6e 698 ipobf.isbroadcast = FALSE;
c910b4d9 699 if (ia != NULL)
6d2010ae 700 IFA_REMREF(&ia->ia_ifa);
c910b4d9 701
b0d623f7
A
702 /* Macro takes reference on ia */
703 IFP_TO_IA(ifp, ia);
1c79356b 704 } else {
39236c6e 705 struct ifaddr *ia0 = NULL;
c910b4d9
A
706 boolean_t cloneok = FALSE;
707 /*
708 * Perform source interface selection; the source IP address
709 * must belong to one of the addresses of the interface used
710 * by the route. For performance reasons, do this only if
711 * there is no route, or if the routing table has changed,
712 * or if we haven't done source interface selection on this
713 * route (for this PCB instance) before.
714 */
39236c6e
A
715 if (ipobf.select_srcif &&
716 ip->ip_src.s_addr != INADDR_ANY && (ROUTE_UNUSABLE(ro) ||
c910b4d9 717 !(ro->ro_flags & ROF_SRCIF_SELECTED))) {
c910b4d9 718 /* Find the source interface */
39236c6e 719 ia0 = in_selectsrcif(ip, ro, ifscope);
c910b4d9 720
6d2010ae 721 /*
fe8ab488 722 * If the source address belongs to a restricted
39037602 723 * interface and the caller forbids our using
fe8ab488
A
724 * interfaces of such type, pretend that there is no
725 * route.
6d2010ae 726 */
39037602 727 if (ia0 != NULL &&
fe8ab488 728 IP_CHECK_RESTRICTIONS(ia0->ifa_ifp, ipobf)) {
39236c6e
A
729 IFA_REMREF(ia0);
730 ia0 = NULL;
731 error = EHOSTUNREACH;
732 if (flags & IP_OUTARGS)
733 ipoa->ipoa_retflags |= IPOARF_IFDENIED;
6d2010ae
A
734 goto bad;
735 }
736
c910b4d9 737 /*
316670eb
A
738 * If the source address is spoofed (in the case of
739 * IP_RAWOUTPUT on an unbounded socket), or if this
740 * is destined for local/loopback, just let it go out
741 * using the interface of the route. Otherwise,
742 * there's no interface having such an address,
743 * so bail out.
c910b4d9 744 */
39236c6e
A
745 if (ia0 == NULL && (!(flags & IP_RAWOUTPUT) ||
746 ipobf.srcbound) && ifscope != lo_ifp->if_index) {
c910b4d9 747 error = EADDRNOTAVAIL;
2d21ac55
A
748 goto bad;
749 }
c910b4d9
A
750
751 /*
752 * If the caller didn't explicitly specify the scope,
753 * pick it up from the source interface. If the cached
754 * route was wrong and was blown away as part of source
755 * interface selection, don't mask out RTF_PRCLONING
756 * since that route may have been allocated by the ULP,
757 * unless the IP header was created by the caller or
758 * the destination is IPv4 LLA. The check for the
759 * latter is needed because IPv4 LLAs are never scoped
760 * in the current implementation, and we don't want to
761 * replace the resolved IPv4 LLA route with one whose
762 * gateway points to that of the default gateway on
763 * the primary interface of the system.
764 */
39236c6e 765 if (ia0 != NULL) {
c910b4d9 766 if (ifscope == IFSCOPE_NONE)
39236c6e 767 ifscope = ia0->ifa_ifp->if_index;
c910b4d9
A
768 cloneok = (!(flags & IP_RAWOUTPUT) &&
769 !(IN_LINKLOCAL(ntohl(ip->ip_dst.s_addr))));
770 }
2d21ac55 771 }
c910b4d9 772
1c79356b
A
773 /*
774 * If this is the case, we probably don't want to allocate
775 * a protocol-cloned route since we didn't get one from the
776 * ULP. This lets TCP do its thing, while not burdening
777 * forwarding or ICMP with the overhead of cloning a route.
778 * Of course, we still want to do any cloning requested by
779 * the link layer, as this is probably required in all cases
780 * for correct operation (as it is for ARP).
781 */
c910b4d9 782 if (ro->ro_rt == NULL) {
2d21ac55
A
783 unsigned long ign = RTF_PRCLONING;
784 /*
785 * We make an exception here: if the destination
786 * address is INADDR_BROADCAST, allocate a protocol-
787 * cloned host route so that we end up with a route
788 * marked with the RTF_BROADCAST flag. Otherwise,
789 * we would end up referring to the default route,
790 * instead of creating a cloned host route entry.
791 * That would introduce inconsistencies between ULPs
792 * that allocate a route and those that don't. The
793 * RTF_BROADCAST route is important since we'd want
794 * to send out undirected IP broadcast packets using
c910b4d9
A
795 * link-level broadcast address. Another exception
796 * is for ULP-created routes that got blown away by
797 * source interface selection (see above).
2d21ac55 798 *
c910b4d9 799 * These exceptions will no longer be necessary when
2d21ac55
A
800 * the RTF_PRCLONING scheme is no longer present.
801 */
c910b4d9 802 if (cloneok || dst->sin_addr.s_addr == INADDR_BROADCAST)
2d21ac55
A
803 ign &= ~RTF_PRCLONING;
804
b0d623f7
A
805 /*
806 * Loosen the route lookup criteria if the ifscope
807 * corresponds to the loopback interface; this is
808 * needed to support Application Layer Gateways
809 * listening on loopback, in conjunction with packet
810 * filter redirection rules. The final source IP
811 * address will be rewritten by the packet filter
812 * prior to the RFC1122 loopback check below.
813 */
814 if (ifscope == lo_ifp->if_index)
815 rtalloc_ign(ro, ign);
816 else
817 rtalloc_scoped_ign(ro, ign, ifscope);
6d2010ae
A
818
819 /*
39037602 820 * If the route points to a cellular/expensive interface
fe8ab488 821 * and the caller forbids our using interfaces of such type,
6d2010ae
A
822 * pretend that there is no route.
823 */
fe8ab488 824 if (ro->ro_rt != NULL) {
6d2010ae 825 RT_LOCK_SPIN(ro->ro_rt);
fe8ab488
A
826 if (IP_CHECK_RESTRICTIONS(ro->ro_rt->rt_ifp,
827 ipobf)) {
6d2010ae 828 RT_UNLOCK(ro->ro_rt);
39236c6e
A
829 ROUTE_RELEASE(ro);
830 if (flags & IP_OUTARGS) {
831 ipoa->ipoa_retflags |=
832 IPOARF_IFDENIED;
833 }
6d2010ae
A
834 } else {
835 RT_UNLOCK(ro->ro_rt);
836 }
837 }
2d21ac55 838 }
c910b4d9
A
839
840 if (ro->ro_rt == NULL) {
b0d623f7 841 OSAddAtomic(1, &ipstat.ips_noroute);
1c79356b 842 error = EHOSTUNREACH;
39236c6e
A
843 if (ia0 != NULL) {
844 IFA_REMREF(ia0);
845 ia0 = NULL;
846 }
1c79356b
A
847 goto bad;
848 }
c910b4d9 849
39236c6e 850 if (ia != NULL)
6d2010ae 851 IFA_REMREF(&ia->ia_ifa);
b0d623f7 852 RT_LOCK_SPIN(ro->ro_rt);
1c79356b 853 ia = ifatoia(ro->ro_rt->rt_ifa);
39236c6e 854 if (ia != NULL) {
6d2010ae
A
855 /* Become a regular mutex */
856 RT_CONVERT_LOCK(ro->ro_rt);
857 IFA_ADDREF(&ia->ia_ifa);
858 }
39236c6e
A
859 /*
860 * Note: ia_ifp may not be the same as rt_ifp; the latter
861 * is what we use for determining outbound i/f, mtu, etc.
862 */
1c79356b 863 ifp = ro->ro_rt->rt_ifp;
1c79356b 864 ro->ro_rt->rt_use++;
316670eb 865 if (ro->ro_rt->rt_flags & RTF_GATEWAY) {
39236c6e 866 dst = SIN(ro->ro_rt->rt_gateway);
316670eb 867 }
7e4a7d39 868 if (ro->ro_rt->rt_flags & RTF_HOST) {
39236c6e
A
869 /* double negation needed for bool bit field */
870 ipobf.isbroadcast =
871 !!(ro->ro_rt->rt_flags & RTF_BROADCAST);
7e4a7d39
A
872 } else {
873 /* Become a regular mutex */
874 RT_CONVERT_LOCK(ro->ro_rt);
39236c6e
A
875 ipobf.isbroadcast = in_broadcast(dst->sin_addr, ifp);
876 }
877 /*
878 * For consistency with IPv6, as well as to ensure that
879 * IP_RECVIF is set correctly for packets that are sent
880 * to one of the local addresses. ia (rt_ifa) would have
881 * been fixed up by rt_setif for local routes. This
882 * would make it appear as if the packet arrives on the
883 * interface which owns the local address. Loopback
884 * multicast case is handled separately by ip_mloopback().
885 */
886 if (ia != NULL && (ifp->if_flags & IFF_LOOPBACK) &&
887 !IN_MULTICAST(ntohl(pkt_dst.s_addr))) {
888 uint32_t srcidx;
889
890 m->m_pkthdr.rcvif = ia->ia_ifa.ifa_ifp;
891
892 if (ia0 != NULL)
893 srcidx = ia0->ifa_ifp->if_index;
894 else if ((ro->ro_flags & ROF_SRCIF_SELECTED) &&
895 ro->ro_srcia != NULL)
896 srcidx = ro->ro_srcia->ifa_ifp->if_index;
897 else
898 srcidx = 0;
899
900 ip_setsrcifaddr_info(m, srcidx, NULL);
901 ip_setdstifaddr_info(m, 0, ia);
7e4a7d39 902 }
b0d623f7 903 RT_UNLOCK(ro->ro_rt);
39236c6e
A
904 if (ia0 != NULL) {
905 IFA_REMREF(ia0);
906 ia0 = NULL;
907 }
1c79356b 908 }
b0d623f7 909
91447636 910 if (IN_MULTICAST(ntohl(pkt_dst.s_addr))) {
39236c6e 911 struct ifnet *srcifp = NULL;
1c79356b 912 struct in_multi *inm;
5ba3f43e 913 u_int32_t vif = 0;
6d2010ae
A
914 u_int8_t ttl = IP_DEFAULT_MULTICAST_TTL;
915 u_int8_t loop = IP_DEFAULT_MULTICAST_LOOP;
1c79356b
A
916
917 m->m_flags |= M_MCAST;
918 /*
919 * IP destination address is multicast. Make sure "dst"
920 * still points to the address in "ro". (It may have been
921 * changed to point to a gateway address, above.)
922 */
39236c6e 923 dst = SIN(&ro->ro_dst);
1c79356b
A
924 /*
925 * See if the caller provided any multicast options
926 */
927 if (imo != NULL) {
6d2010ae
A
928 IMO_LOCK(imo);
929 vif = imo->imo_multicast_vif;
930 ttl = imo->imo_multicast_ttl;
931 loop = imo->imo_multicast_loop;
39236c6e 932 if (!(flags & IP_RAWOUTPUT))
6d2010ae
A
933 ip->ip_ttl = ttl;
934 if (imo->imo_multicast_ifp != NULL)
1c79356b 935 ifp = imo->imo_multicast_ifp;
6d2010ae 936 IMO_UNLOCK(imo);
39236c6e 937 } else if (!(flags & IP_RAWOUTPUT)) {
6d2010ae
A
938 vif = -1;
939 ip->ip_ttl = ttl;
940 }
1c79356b
A
941 /*
942 * Confirm that the outgoing interface supports multicast.
943 */
6d2010ae 944 if (imo == NULL || vif == -1) {
39236c6e 945 if (!(ifp->if_flags & IFF_MULTICAST)) {
b0d623f7 946 OSAddAtomic(1, &ipstat.ips_noroute);
1c79356b
A
947 error = ENETUNREACH;
948 goto bad;
949 }
950 }
951 /*
952 * If source address not specified yet, use address
953 * of outgoing interface.
954 */
955 if (ip->ip_src.s_addr == INADDR_ANY) {
b0d623f7
A
956 struct in_ifaddr *ia1;
957 lck_rw_lock_shared(in_ifaddr_rwlock);
6d2010ae
A
958 TAILQ_FOREACH(ia1, &in_ifaddrhead, ia_link) {
959 IFA_LOCK_SPIN(&ia1->ia_ifa);
1c79356b
A
960 if (ia1->ia_ifp == ifp) {
961 ip->ip_src = IA_SIN(ia1)->sin_addr;
39236c6e 962 srcifp = ifp;
6d2010ae 963 IFA_UNLOCK(&ia1->ia_ifa);
1c79356b
A
964 break;
965 }
6d2010ae
A
966 IFA_UNLOCK(&ia1->ia_ifa);
967 }
b0d623f7 968 lck_rw_done(in_ifaddr_rwlock);
55e303ae
A
969 if (ip->ip_src.s_addr == INADDR_ANY) {
970 error = ENETUNREACH;
971 goto bad;
972 }
1c79356b
A
973 }
974
6d2010ae
A
975 in_multihead_lock_shared();
976 IN_LOOKUP_MULTI(&pkt_dst, ifp, inm);
977 in_multihead_lock_done();
978 if (inm != NULL && (imo == NULL || loop)) {
1c79356b
A
979 /*
980 * If we belong to the destination multicast group
981 * on the outgoing interface, and the caller did not
982 * forbid loopback, loop back a copy.
983 */
91447636
A
984 if (!TAILQ_EMPTY(&ipv4_filters)) {
985 struct ipfilter *filter;
39236c6e 986 int seen = (inject_filter_ref == NULL);
91447636 987
6d2010ae 988 if (imo != NULL) {
39236c6e
A
989 ipf_pktopts.ippo_flags |=
990 IPPOF_MCAST_OPTS;
6d2010ae
A
991 ipf_pktopts.ippo_mcast_ifnet = ifp;
992 ipf_pktopts.ippo_mcast_ttl = ttl;
993 ipf_pktopts.ippo_mcast_loop = loop;
91447636 994 }
6d2010ae 995
91447636 996 ipf_ref();
6d2010ae 997
39236c6e
A
998 /*
999 * 4135317 - always pass network byte
1000 * order to filter
1001 */
b0d623f7 1002#if BYTE_ORDER != BIG_ENDIAN
0c530ab8
A
1003 HTONS(ip->ip_len);
1004 HTONS(ip->ip_off);
b0d623f7 1005#endif
91447636
A
1006 TAILQ_FOREACH(filter, &ipv4_filters, ipf_link) {
1007 if (seen == 0) {
39236c6e
A
1008 if ((struct ipfilter *)
1009 inject_filter_ref == filter)
91447636 1010 seen = 1;
39236c6e
A
1011 } else if (filter->ipf_filter.
1012 ipf_output != NULL) {
91447636 1013 errno_t result;
39236c6e
A
1014 result = filter->ipf_filter.
1015 ipf_output(filter->
1016 ipf_filter.cookie,
1017 (mbuf_t *)&m, ippo);
91447636
A
1018 if (result == EJUSTRETURN) {
1019 ipf_unref();
6d2010ae 1020 INM_REMREF(inm);
91447636
A
1021 goto done;
1022 }
1023 if (result != 0) {
1024 ipf_unref();
6d2010ae 1025 INM_REMREF(inm);
91447636
A
1026 goto bad;
1027 }
1028 }
1029 }
6d2010ae 1030
0c530ab8 1031 /* set back to host byte order */
6601e61a 1032 ip = mtod(m, struct ip *);
b0d623f7 1033#if BYTE_ORDER != BIG_ENDIAN
0c530ab8
A
1034 NTOHS(ip->ip_len);
1035 NTOHS(ip->ip_off);
b0d623f7 1036#endif
91447636 1037 ipf_unref();
39236c6e 1038 ipobf.didfilter = TRUE;
91447636 1039 }
39236c6e 1040 ip_mloopback(srcifp, ifp, m, dst, hlen);
1c79356b 1041 }
6d2010ae
A
1042 if (inm != NULL)
1043 INM_REMREF(inm);
1c79356b
A
1044 /*
1045 * Multicasts with a time-to-live of zero may be looped-
1046 * back, above, but must not be transmitted on a network.
1047 * Also, multicasts addressed to the loopback interface
1048 * are not sent -- the above call to ip_mloopback() will
1049 * loop back a copy if this host actually belongs to the
1050 * destination group on the loopback interface.
1051 */
1052 if (ip->ip_ttl == 0 || ifp->if_flags & IFF_LOOPBACK) {
1053 m_freem(m);
1054 goto done;
1055 }
1056
1057 goto sendit;
1058 }
1c79356b
A
1059 /*
1060 * If source address not specified yet, use address
1061 * of outgoing interface.
1062 */
1063 if (ip->ip_src.s_addr == INADDR_ANY) {
6d2010ae 1064 IFA_LOCK_SPIN(&ia->ia_ifa);
1c79356b 1065 ip->ip_src = IA_SIN(ia)->sin_addr;
6d2010ae 1066 IFA_UNLOCK(&ia->ia_ifa);
1c79356b 1067#if IPFIREWALL_FORWARD
39236c6e
A
1068 /*
1069 * Keep note that we did this - if the firewall changes
1c79356b
A
1070 * the next-hop, our interface may change, changing the
1071 * default source IP. It's a shame so much effort happens
39236c6e 1072 * twice. Oh well.
1c79356b 1073 */
39236c6e 1074 ipobf.fwd_rewrite_src = TRUE;
1c79356b
A
1075#endif /* IPFIREWALL_FORWARD */
1076 }
1c79356b
A
1077
1078 /*
1079 * Look for broadcast address and
1080 * and verify user is allowed to send
1081 * such a packet.
1082 */
39236c6e
A
1083 if (ipobf.isbroadcast) {
1084 if (!(ifp->if_flags & IFF_BROADCAST)) {
1c79356b
A
1085 error = EADDRNOTAVAIL;
1086 goto bad;
1087 }
39236c6e 1088 if (!(flags & IP_ALLOWBROADCAST)) {
1c79356b
A
1089 error = EACCES;
1090 goto bad;
1091 }
1092 /* don't allow broadcast messages to be fragmented */
1093 if ((u_short)ip->ip_len > ifp->if_mtu) {
1094 error = EMSGSIZE;
1095 goto bad;
1096 }
1097 m->m_flags |= M_BCAST;
1098 } else {
1099 m->m_flags &= ~M_BCAST;
1100 }
1101
1102sendit:
b0d623f7
A
1103#if PF
1104 /* Invoke outbound packet filter */
316670eb 1105 if (PF_IS_ENABLED) {
6d2010ae 1106 int rc;
316670eb 1107
39236c6e 1108 m0 = m; /* Save for later */
316670eb
A
1109#if DUMMYNET
1110 args.fwa_m = m;
1111 args.fwa_next_hop = dst;
1112 args.fwa_oif = ifp;
1113 args.fwa_ro = ro;
1114 args.fwa_dst = dst;
1115 args.fwa_oflags = flags;
1116 if (flags & IP_OUTARGS)
1117 args.fwa_ipoa = ipoa;
1118 rc = pf_af_hook(ifp, mppn, &m, AF_INET, FALSE, &args);
1119#else /* DUMMYNET */
1120 rc = pf_af_hook(ifp, mppn, &m, AF_INET, FALSE, NULL);
1121#endif /* DUMMYNET */
1122 if (rc != 0 || m == NULL) {
1123 /* Move to the next packet */
1124 m = *mppn;
1125
1126 /* Skip ahead if first packet in list got dropped */
1127 if (packetlist == m0)
6d2010ae 1128 packetlist = m;
316670eb 1129
6d2010ae
A
1130 if (m != NULL) {
1131 m0 = m;
1132 /* Next packet in the chain */
1133 goto loopit;
1134 } else if (packetlist != NULL) {
1135 /* No more packet; send down the chain */
1136 goto sendchain;
1137 }
1138 /* Nothing left; we're done */
1139 goto done;
b0d623f7 1140 }
6d2010ae
A
1141 m0 = m;
1142 ip = mtod(m, struct ip *);
1143 pkt_dst = ip->ip_dst;
1144 hlen = IP_VHL_HL(ip->ip_vhl) << 2;
b0d623f7 1145 }
b0d623f7 1146#endif /* PF */
39236c6e
A
1147 /*
1148 * Force IP TTL to 255 following draft-ietf-zeroconf-ipv4-linklocal.txt
1149 */
1150 if (IN_LINKLOCAL(ntohl(ip->ip_src.s_addr)) ||
1151 IN_LINKLOCAL(ntohl(ip->ip_dst.s_addr))) {
9bccf70c
A
1152 ip_linklocal_stat.iplls_out_total++;
1153 if (ip->ip_ttl != MAXTTL) {
1154 ip_linklocal_stat.iplls_out_badttl++;
316670eb 1155 ip->ip_ttl = MAXTTL;
9bccf70c 1156 }
39236c6e 1157 }
9bccf70c 1158
39236c6e 1159 if (!ipobf.didfilter && !TAILQ_EMPTY(&ipv4_filters)) {
91447636 1160 struct ipfilter *filter;
39236c6e 1161 int seen = (inject_filter_ref == NULL);
6d2010ae
A
1162 ipf_pktopts.ippo_flags &= ~IPPOF_MCAST_OPTS;
1163
39236c6e
A
1164 /*
1165 * Check that a TSO frame isn't passed to a filter.
b0d623f7
A
1166 * This could happen if a filter is inserted while
1167 * TCP is sending the TSO packet.
1168 */
1169 if (m->m_pkthdr.csum_flags & CSUM_TSO_IPV4) {
1170 error = EMSGSIZE;
1171 goto bad;
1172 }
1173
91447636 1174 ipf_ref();
316670eb 1175
0c530ab8 1176 /* 4135317 - always pass network byte order to filter */
b0d623f7 1177#if BYTE_ORDER != BIG_ENDIAN
0c530ab8
A
1178 HTONS(ip->ip_len);
1179 HTONS(ip->ip_off);
b0d623f7 1180#endif
91447636
A
1181 TAILQ_FOREACH(filter, &ipv4_filters, ipf_link) {
1182 if (seen == 0) {
39236c6e
A
1183 if ((struct ipfilter *)inject_filter_ref ==
1184 filter)
91447636
A
1185 seen = 1;
1186 } else if (filter->ipf_filter.ipf_output) {
1187 errno_t result;
39236c6e
A
1188 result = filter->ipf_filter.
1189 ipf_output(filter->ipf_filter.cookie,
1190 (mbuf_t *)&m, ippo);
91447636
A
1191 if (result == EJUSTRETURN) {
1192 ipf_unref();
1193 goto done;
1194 }
1195 if (result != 0) {
1196 ipf_unref();
91447636
A
1197 goto bad;
1198 }
1199 }
1200 }
0c530ab8 1201 /* set back to host byte order */
6601e61a 1202 ip = mtod(m, struct ip *);
b0d623f7 1203#if BYTE_ORDER != BIG_ENDIAN
0c530ab8
A
1204 NTOHS(ip->ip_len);
1205 NTOHS(ip->ip_off);
b0d623f7 1206#endif
91447636 1207 ipf_unref();
91447636
A
1208 }
1209
fe8ab488
A
1210#if NECP
1211 /* Process Network Extension Policy. Will Pass, Drop, or Rebind packet. */
1212 necp_matched_policy_id = necp_ip_output_find_policy_match (m,
1213 flags, (flags & IP_OUTARGS) ? ipoa : NULL, &necp_result, &necp_result_parameter);
1214 if (necp_matched_policy_id) {
1215 necp_mark_packet_from_ip(m, necp_matched_policy_id);
1216 switch (necp_result) {
1217 case NECP_KERNEL_POLICY_RESULT_PASS:
3e170ce0
A
1218 /* Check if the interface is allowed */
1219 if (!necp_packet_is_allowed_over_interface(m, ifp)) {
1220 error = EHOSTUNREACH;
5ba3f43e 1221 OSAddAtomic(1, &ipstat.ips_necp_policy_drop);
3e170ce0
A
1222 goto bad;
1223 }
fe8ab488
A
1224 goto skip_ipsec;
1225 case NECP_KERNEL_POLICY_RESULT_DROP:
1226 case NECP_KERNEL_POLICY_RESULT_SOCKET_DIVERT:
1227 /* Flow divert packets should be blocked at the IP layer */
1228 error = EHOSTUNREACH;
5ba3f43e 1229 OSAddAtomic(1, &ipstat.ips_necp_policy_drop);
fe8ab488
A
1230 goto bad;
1231 case NECP_KERNEL_POLICY_RESULT_IP_TUNNEL: {
1232 /* Verify that the packet is being routed to the tunnel */
1233 struct ifnet *policy_ifp = necp_get_ifnet_from_result_parameter(&necp_result_parameter);
1234 if (policy_ifp == ifp) {
3e170ce0
A
1235 /* Check if the interface is allowed */
1236 if (!necp_packet_is_allowed_over_interface(m, ifp)) {
1237 error = EHOSTUNREACH;
5ba3f43e 1238 OSAddAtomic(1, &ipstat.ips_necp_policy_drop);
3e170ce0
A
1239 goto bad;
1240 }
fe8ab488
A
1241 goto skip_ipsec;
1242 } else {
1243 if (necp_packet_can_rebind_to_ifnet(m, policy_ifp, &necp_route, AF_INET)) {
3e170ce0
A
1244 /* Check if the interface is allowed */
1245 if (!necp_packet_is_allowed_over_interface(m, policy_ifp)) {
1246 error = EHOSTUNREACH;
5ba3f43e 1247 OSAddAtomic(1, &ipstat.ips_necp_policy_drop);
3e170ce0
A
1248 goto bad;
1249 }
1250
fe8ab488
A
1251 /* Set ifp to the tunnel interface, since it is compatible with the packet */
1252 ifp = policy_ifp;
1253 ro = &necp_route;
1254 goto skip_ipsec;
1255 } else {
1256 error = ENETUNREACH;
5ba3f43e 1257 OSAddAtomic(1, &ipstat.ips_necp_policy_drop);
fe8ab488
A
1258 goto bad;
1259 }
1260 }
fe8ab488
A
1261 }
1262 default:
1263 break;
1264 }
1265 }
3e170ce0
A
1266 /* Catch-all to check if the interface is allowed */
1267 if (!necp_packet_is_allowed_over_interface(m, ifp)) {
1268 error = EHOSTUNREACH;
5ba3f43e 1269 OSAddAtomic(1, &ipstat.ips_necp_policy_drop);
3e170ce0
A
1270 goto bad;
1271 }
fe8ab488 1272#endif /* NECP */
3e170ce0 1273
9bccf70c 1274#if IPSEC
39236c6e 1275 if (ipsec_bypass != 0 || (flags & IP_NOIPSEC))
9bccf70c
A
1276 goto skip_ipsec;
1277
39236c6e 1278 KERNEL_DEBUG(DBG_FNC_IPSEC4_OUTPUT | DBG_FUNC_START, 0, 0, 0, 0, 0);
9bccf70c
A
1279
1280 if (sp == NULL) {
39236c6e 1281 /* get SP for this packet */
fe8ab488 1282 if (so != NULL) {
39236c6e 1283 sp = ipsec4_getpolicybysock(m, IPSEC_DIR_OUTBOUND,
fe8ab488
A
1284 so, &error);
1285 } else {
1286 sp = ipsec4_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND,
1287 flags, &error);
1288 }
39236c6e
A
1289 if (sp == NULL) {
1290 IPSEC_STAT_INCREMENT(ipsecstat.out_inval);
1291 KERNEL_DEBUG(DBG_FNC_IPSEC4_OUTPUT | DBG_FUNC_END,
fe8ab488 1292 0, 0, 0, 0, 0);
39236c6e
A
1293 goto bad;
1294 }
9bccf70c
A
1295 }
1296
1297 error = 0;
1298
1299 /* check policy */
1300 switch (sp->policy) {
1301 case IPSEC_POLICY_DISCARD:
2d21ac55 1302 case IPSEC_POLICY_GENERATE:
9bccf70c
A
1303 /*
1304 * This packet is just discarded.
1305 */
2d21ac55 1306 IPSEC_STAT_INCREMENT(ipsecstat.out_polvio);
39236c6e
A
1307 KERNEL_DEBUG(DBG_FNC_IPSEC4_OUTPUT | DBG_FUNC_END,
1308 1, 0, 0, 0, 0);
9bccf70c
A
1309 goto bad;
1310
1311 case IPSEC_POLICY_BYPASS:
1312 case IPSEC_POLICY_NONE:
1313 /* no need to do IPsec. */
39236c6e
A
1314 KERNEL_DEBUG(DBG_FNC_IPSEC4_OUTPUT | DBG_FUNC_END,
1315 2, 0, 0, 0, 0);
9bccf70c 1316 goto skip_ipsec;
316670eb 1317
9bccf70c
A
1318 case IPSEC_POLICY_IPSEC:
1319 if (sp->req == NULL) {
1320 /* acquire a policy */
1321 error = key_spdacquire(sp);
39236c6e
A
1322 KERNEL_DEBUG(DBG_FNC_IPSEC4_OUTPUT | DBG_FUNC_END,
1323 3, 0, 0, 0, 0);
1324 goto bad;
1325 }
1326 if (sp->ipsec_if) {
1327 /* Verify the redirect to ipsec interface */
1328 if (sp->ipsec_if == ifp) {
39236c6e
A
1329 goto skip_ipsec;
1330 }
9bccf70c
A
1331 goto bad;
1332 }
1333 break;
1334
1335 case IPSEC_POLICY_ENTRUST:
1336 default:
1337 printf("ip_output: Invalid policy found. %d\n", sp->policy);
1338 }
39236c6e 1339 {
ebb1b9f4 1340 ipsec_state.m = m;
9bccf70c 1341 if (flags & IP_ROUTETOIF) {
39236c6e
A
1342 bzero(&ipsec_state.ro, sizeof (ipsec_state.ro));
1343 } else {
5c9f4661 1344 route_copyout((struct route *)&ipsec_state.ro, ro, sizeof (struct route));
39236c6e
A
1345 }
1346 ipsec_state.dst = SA(dst);
9bccf70c
A
1347
1348 ip->ip_sum = 0;
1349
1350 /*
1351 * XXX
1352 * delayed checksums are not currently compatible with IPsec
1353 */
39236c6e 1354 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA)
9bccf70c 1355 in_delayed_cksum(m);
b0d623f7
A
1356
1357#if BYTE_ORDER != BIG_ENDIAN
9bccf70c
A
1358 HTONS(ip->ip_len);
1359 HTONS(ip->ip_off);
b0d623f7 1360#endif
9bccf70c 1361
6d2010ae 1362 DTRACE_IP6(send, struct mbuf *, m, struct inpcb *, NULL,
39236c6e
A
1363 struct ip *, ip, struct ifnet *, ifp,
1364 struct ip *, ip, struct ip6_hdr *, NULL);
6d2010ae 1365
ebb1b9f4 1366 error = ipsec4_output(&ipsec_state, sp, flags);
490019cf
A
1367 if (ipsec_state.tunneled == 6) {
1368 m0 = m = NULL;
1369 error = 0;
1370 goto bad;
1371 }
316670eb 1372
ebb1b9f4 1373 m0 = m = ipsec_state.m;
316670eb 1374
39236c6e
A
1375#if DUMMYNET
1376 /*
1377 * If we're about to use the route in ipsec_state
1378 * and this came from dummynet, cleaup now.
1379 */
1380 if (ro == &saved_route &&
1381 (!(flags & IP_ROUTETOIF) || ipsec_state.tunneled))
1382 ROUTE_RELEASE(ro);
1383#endif /* DUMMYNET */
1384
9bccf70c
A
1385 if (flags & IP_ROUTETOIF) {
1386 /*
1387 * if we have tunnel mode SA, we may need to ignore
1388 * IP_ROUTETOIF.
1389 */
ebb1b9f4 1390 if (ipsec_state.tunneled) {
9bccf70c 1391 flags &= ~IP_ROUTETOIF;
5c9f4661 1392 ro = (struct route *)&ipsec_state.ro;
9bccf70c 1393 }
ebb1b9f4 1394 } else {
5c9f4661 1395 ro = (struct route *)&ipsec_state.ro;
ebb1b9f4 1396 }
39236c6e 1397 dst = SIN(ipsec_state.dst);
9bccf70c
A
1398 if (error) {
1399 /* mbuf is already reclaimed in ipsec4_output. */
1400 m0 = NULL;
1401 switch (error) {
1402 case EHOSTUNREACH:
1403 case ENETUNREACH:
1404 case EMSGSIZE:
1405 case ENOBUFS:
1406 case ENOMEM:
1407 break;
1408 default:
1409 printf("ip4_output (ipsec): error code %d\n", error);
39236c6e 1410 /* FALLTHRU */
9bccf70c
A
1411 case ENOENT:
1412 /* don't show these error codes to the user */
1413 error = 0;
1414 break;
1415 }
39236c6e
A
1416 KERNEL_DEBUG(DBG_FNC_IPSEC4_OUTPUT | DBG_FUNC_END,
1417 4, 0, 0, 0, 0);
9bccf70c
A
1418 goto bad;
1419 }
39236c6e 1420 }
9bccf70c
A
1421
1422 /* be sure to update variables that are affected by ipsec4_output() */
1423 ip = mtod(m, struct ip *);
316670eb 1424
9bccf70c
A
1425#ifdef _IP_VHL
1426 hlen = IP_VHL_HL(ip->ip_vhl) << 2;
39236c6e 1427#else /* !_IP_VHL */
9bccf70c 1428 hlen = ip->ip_hl << 2;
39236c6e 1429#endif /* !_IP_VHL */
55e303ae 1430 /* Check that there wasn't a route change and src is still valid */
39236c6e
A
1431 if (ROUTE_UNUSABLE(ro)) {
1432 ROUTE_RELEASE(ro);
1433 VERIFY(src_ia == NULL);
1434 if (ip->ip_src.s_addr != INADDR_ANY &&
1435 !(flags & (IP_ROUTETOIF | IP_FORWARDING)) &&
1436 (src_ia = ifa_foraddr(ip->ip_src.s_addr)) == NULL) {
b0d623f7
A
1437 error = EADDRNOTAVAIL;
1438 KERNEL_DEBUG(DBG_FNC_IPSEC4_OUTPUT | DBG_FUNC_END,
39236c6e 1439 5, 0, 0, 0, 0);
55e303ae
A
1440 goto bad;
1441 }
39236c6e 1442 if (src_ia != NULL) {
6d2010ae 1443 IFA_REMREF(&src_ia->ia_ifa);
39236c6e
A
1444 src_ia = NULL;
1445 }
55e303ae
A
1446 }
1447
9bccf70c 1448 if (ro->ro_rt == NULL) {
39236c6e
A
1449 if (!(flags & IP_ROUTETOIF)) {
1450 printf("%s: can't update route after "
1451 "IPsec processing\n", __func__);
1452 error = EHOSTUNREACH; /* XXX */
b0d623f7 1453 KERNEL_DEBUG(DBG_FNC_IPSEC4_OUTPUT | DBG_FUNC_END,
39236c6e 1454 6, 0, 0, 0, 0);
9bccf70c
A
1455 goto bad;
1456 }
1457 } else {
39236c6e 1458 if (ia != NULL)
6d2010ae 1459 IFA_REMREF(&ia->ia_ifa);
b0d623f7 1460 RT_LOCK_SPIN(ro->ro_rt);
9bccf70c 1461 ia = ifatoia(ro->ro_rt->rt_ifa);
39236c6e 1462 if (ia != NULL) {
6d2010ae
A
1463 /* Become a regular mutex */
1464 RT_CONVERT_LOCK(ro->ro_rt);
1465 IFA_ADDREF(&ia->ia_ifa);
1466 }
9bccf70c 1467 ifp = ro->ro_rt->rt_ifp;
b0d623f7 1468 RT_UNLOCK(ro->ro_rt);
9bccf70c
A
1469 }
1470
1471 /* make it flipped, again. */
b0d623f7 1472#if BYTE_ORDER != BIG_ENDIAN
9bccf70c
A
1473 NTOHS(ip->ip_len);
1474 NTOHS(ip->ip_off);
b0d623f7 1475#endif
39236c6e
A
1476 KERNEL_DEBUG(DBG_FNC_IPSEC4_OUTPUT | DBG_FUNC_END,
1477 7, 0xff, 0xff, 0xff, 0xff);
316670eb 1478
91447636
A
1479 /* Pass to filters again */
1480 if (!TAILQ_EMPTY(&ipv4_filters)) {
1481 struct ipfilter *filter;
316670eb 1482
6d2010ae
A
1483 ipf_pktopts.ippo_flags &= ~IPPOF_MCAST_OPTS;
1484
39236c6e
A
1485 /*
1486 * Check that a TSO frame isn't passed to a filter.
b0d623f7
A
1487 * This could happen if a filter is inserted while
1488 * TCP is sending the TSO packet.
1489 */
1490 if (m->m_pkthdr.csum_flags & CSUM_TSO_IPV4) {
1491 error = EMSGSIZE;
1492 goto bad;
1493 }
1494
91447636 1495 ipf_ref();
316670eb 1496
0c530ab8 1497 /* 4135317 - always pass network byte order to filter */
b0d623f7 1498#if BYTE_ORDER != BIG_ENDIAN
0c530ab8
A
1499 HTONS(ip->ip_len);
1500 HTONS(ip->ip_off);
b0d623f7 1501#endif
91447636
A
1502 TAILQ_FOREACH(filter, &ipv4_filters, ipf_link) {
1503 if (filter->ipf_filter.ipf_output) {
1504 errno_t result;
39236c6e
A
1505 result = filter->ipf_filter.
1506 ipf_output(filter->ipf_filter.cookie,
1507 (mbuf_t *)&m, ippo);
91447636
A
1508 if (result == EJUSTRETURN) {
1509 ipf_unref();
1510 goto done;
1511 }
1512 if (result != 0) {
1513 ipf_unref();
91447636
A
1514 goto bad;
1515 }
1516 }
1517 }
0c530ab8 1518 /* set back to host byte order */
6601e61a 1519 ip = mtod(m, struct ip *);
b0d623f7 1520#if BYTE_ORDER != BIG_ENDIAN
0c530ab8
A
1521 NTOHS(ip->ip_len);
1522 NTOHS(ip->ip_off);
b0d623f7 1523#endif
91447636 1524 ipf_unref();
91447636 1525 }
9bccf70c 1526skip_ipsec:
39236c6e 1527#endif /* IPSEC */
9bccf70c 1528
2d21ac55 1529#if IPFIREWALL
1c79356b
A
1530 /*
1531 * Check with the firewall...
91447636 1532 * but not if we are already being fwd'd from a firewall.
1c79356b 1533 */
316670eb 1534 if (fw_enable && IPFW_LOADED && !args.fwa_next_hop) {
1c79356b
A
1535 struct sockaddr_in *old = dst;
1536
316670eb
A
1537 args.fwa_m = m;
1538 args.fwa_next_hop = dst;
1539 args.fwa_oif = ifp;
39236c6e 1540 ipfwoff = ip_fw_chk_ptr(&args);
316670eb
A
1541 m = args.fwa_m;
1542 dst = args.fwa_next_hop;
91447636 1543
39236c6e
A
1544 /*
1545 * On return we must do the following:
1546 * IP_FW_PORT_DENY_FLAG -> drop the pkt (XXX new)
1547 * 1<=off<= 0xffff -> DIVERT
1548 * (off & IP_FW_PORT_DYNT_FLAG) -> send to a DUMMYNET pipe
1549 * (off & IP_FW_PORT_TEE_FLAG) -> TEE the packet
1550 * dst != old -> IPFIREWALL_FORWARD
1551 * off==0, dst==old -> accept
1552 * If some of the above modules is not compiled in, then
1553 * we should't have to check the corresponding condition
1554 * (because the ipfw control socket should not accept
1555 * unsupported rules), but better play safe and drop
1556 * packets in case of doubt.
1557 */
55e303ae 1558 m0 = m;
39236c6e 1559 if ((ipfwoff & IP_FW_PORT_DENY_FLAG) || m == NULL) {
9bccf70c
A
1560 if (m)
1561 m_freem(m);
39236c6e
A
1562 error = EACCES;
1563 goto done;
1c79356b 1564 }
9bccf70c 1565 ip = mtod(m, struct ip *);
316670eb 1566
39236c6e
A
1567 if (ipfwoff == 0 && dst == old) { /* common case */
1568 goto pass;
3a60a9f5 1569 }
1c79356b 1570#if DUMMYNET
39236c6e 1571 if (DUMMYNET_LOADED && (ipfwoff & IP_FW_PORT_DYNT_FLAG) != 0) {
c910b4d9
A
1572 /*
1573 * pass the pkt to dummynet. Need to include
1574 * pipe number, m, ifp, ro, dst because these are
1575 * not recomputed in the next pass.
1576 * All other parameters have been already used and
1577 * so they are not needed anymore.
1578 * XXX note: if the ifp or ro entry are deleted
1579 * while a pkt is in dummynet, we are in trouble!
1580 */
316670eb
A
1581 args.fwa_ro = ro;
1582 args.fwa_dst = dst;
1583 args.fwa_oflags = flags;
c910b4d9 1584 if (flags & IP_OUTARGS)
316670eb 1585 args.fwa_ipoa = ipoa;
c910b4d9 1586
39236c6e 1587 error = ip_dn_io_ptr(m, ipfwoff & 0xffff, DN_TO_IP_OUT,
316670eb 1588 &args, DN_CLIENT_IPFW);
c910b4d9 1589 goto done;
1c79356b 1590 }
91447636 1591#endif /* DUMMYNET */
1c79356b 1592#if IPDIVERT
39236c6e 1593 if (ipfwoff != 0 && (ipfwoff & IP_FW_PORT_DYNT_FLAG) == 0) {
9bccf70c
A
1594 struct mbuf *clone = NULL;
1595
1596 /* Clone packet if we're doing a 'tee' */
39236c6e 1597 if ((ipfwoff & IP_FW_PORT_TEE_FLAG) != 0)
9bccf70c
A
1598 clone = m_dup(m, M_DONTWAIT);
1599 /*
1600 * XXX
1601 * delayed checksums are not currently compatible
1602 * with divert sockets.
1603 */
39236c6e 1604 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA)
9bccf70c 1605 in_delayed_cksum(m);
9bccf70c
A
1606
1607 /* Restore packet header fields to original values */
b0d623f7
A
1608
1609#if BYTE_ORDER != BIG_ENDIAN
9bccf70c
A
1610 HTONS(ip->ip_len);
1611 HTONS(ip->ip_off);
b0d623f7 1612#endif
9bccf70c
A
1613
1614 /* Deliver packet to divert input routine */
39236c6e
A
1615 divert_packet(m, 0, ipfwoff & 0xffff,
1616 args.fwa_divert_rule);
9bccf70c
A
1617
1618 /* If 'tee', continue with original packet */
1619 if (clone != NULL) {
55e303ae 1620 m0 = m = clone;
9bccf70c
A
1621 ip = mtod(m, struct ip *);
1622 goto pass;
1623 }
1c79356b
A
1624 goto done;
1625 }
39236c6e 1626#endif /* IPDIVERT */
1c79356b 1627#if IPFIREWALL_FORWARD
39236c6e
A
1628 /*
1629 * Here we check dst to make sure it's directly reachable on
1630 * the interface we previously thought it was.
1c79356b
A
1631 * If it isn't (which may be likely in some situations) we have
1632 * to re-route it (ie, find a route for the next-hop and the
1633 * associated interface) and set them here. This is nested
1634 * forwarding which in most cases is undesirable, except where
1635 * such control is nigh impossible. So we do it here.
1636 * And I'm babbling.
1637 */
39236c6e 1638 if (ipfwoff == 0 && old != dst) {
91447636 1639 struct in_ifaddr *ia_fw;
39236c6e 1640 struct route *ro_fwd = &sro_fwd;
1c79356b 1641
1c79356b
A
1642#if IPFIREWALL_FORWARD_DEBUG
1643 printf("IPFIREWALL_FORWARD: New dst ip: ");
1644 print_ip(dst->sin_addr);
1645 printf("\n");
39236c6e 1646#endif /* IPFIREWALL_FORWARD_DEBUG */
1c79356b
A
1647 /*
1648 * We need to figure out if we have been forwarded
39236c6e 1649 * to a local socket. If so then we should somehow
1c79356b
A
1650 * "loop back" to ip_input, and get directed to the
1651 * PCB as if we had received this packet. This is
1652 * because it may be dificult to identify the packets
1653 * you want to forward until they are being output
1654 * and have selected an interface. (e.g. locally
1655 * initiated packets) If we used the loopback inteface,
39236c6e 1656 * we would not be able to control what happens
1c79356b
A
1657 * as the packet runs through ip_input() as
1658 * it is done through a ISR.
1659 */
b0d623f7 1660 lck_rw_lock_shared(in_ifaddr_rwlock);
91447636 1661 TAILQ_FOREACH(ia_fw, &in_ifaddrhead, ia_link) {
1c79356b
A
1662 /*
1663 * If the addr to forward to is one
1664 * of ours, we pretend to
1665 * be the destination for this packet.
1666 */
6d2010ae 1667 IFA_LOCK_SPIN(&ia_fw->ia_ifa);
91447636 1668 if (IA_SIN(ia_fw)->sin_addr.s_addr ==
6d2010ae
A
1669 dst->sin_addr.s_addr) {
1670 IFA_UNLOCK(&ia_fw->ia_ifa);
1c79356b 1671 break;
6d2010ae
A
1672 }
1673 IFA_UNLOCK(&ia_fw->ia_ifa);
1c79356b 1674 }
b0d623f7
A
1675 lck_rw_done(in_ifaddr_rwlock);
1676 if (ia_fw) {
1c79356b 1677 /* tell ip_input "dont filter" */
39236c6e 1678 struct m_tag *fwd_tag;
91447636 1679 struct ip_fwd_tag *ipfwd_tag;
6d2010ae
A
1680
1681 fwd_tag = m_tag_create(KERNEL_MODULE_TAG_ID,
b0d623f7 1682 KERNEL_TAG_TYPE_IPFORWARD,
6d2010ae 1683 sizeof (*ipfwd_tag), M_NOWAIT, m);
91447636
A
1684 if (fwd_tag == NULL) {
1685 error = ENOBUFS;
1686 goto bad;
1687 }
6d2010ae 1688
91447636 1689 ipfwd_tag = (struct ip_fwd_tag *)(fwd_tag+1);
316670eb 1690 ipfwd_tag->next_hop = args.fwa_next_hop;
91447636
A
1691
1692 m_tag_prepend(m, fwd_tag);
1693
1c79356b 1694 if (m->m_pkthdr.rcvif == NULL)
6d2010ae 1695 m->m_pkthdr.rcvif = lo_ifp;
b0d623f7
A
1696
1697#if BYTE_ORDER != BIG_ENDIAN
9bccf70c
A
1698 HTONS(ip->ip_len);
1699 HTONS(ip->ip_off);
316670eb 1700#endif
39236c6e 1701 mbuf_outbound_finalize(m, PF_INET, 0);
316670eb 1702
39236c6e
A
1703 /*
1704 * we need to call dlil_output to run filters
1705 * and resync to avoid recursion loops.
91447636
A
1706 */
1707 if (lo_ifp) {
39236c6e
A
1708 dlil_output(lo_ifp, PF_INET, m, NULL,
1709 SA(dst), 0, adv);
1710 } else {
1711 printf("%s: no loopback ifp for "
1712 "forwarding!!!\n", __func__);
91447636 1713 }
1c79356b
A
1714 goto done;
1715 }
39236c6e
A
1716 /*
1717 * Some of the logic for this was nicked from above.
1c79356b
A
1718 *
1719 * This rewrites the cached route in a local PCB.
1720 * Is this what we want to do?
1721 */
39236c6e
A
1722 ROUTE_RELEASE(ro_fwd);
1723 bcopy(dst, &ro_fwd->ro_dst, sizeof (*dst));
1c79356b 1724
39037602 1725 rtalloc_ign(ro_fwd, RTF_PRCLONING, false);
1c79356b 1726
b0d623f7
A
1727 if (ro_fwd->ro_rt == NULL) {
1728 OSAddAtomic(1, &ipstat.ips_noroute);
1c79356b
A
1729 error = EHOSTUNREACH;
1730 goto bad;
1731 }
1732
b0d623f7 1733 RT_LOCK_SPIN(ro_fwd->ro_rt);
91447636 1734 ia_fw = ifatoia(ro_fwd->ro_rt->rt_ifa);
6d2010ae
A
1735 if (ia_fw != NULL) {
1736 /* Become a regular mutex */
1737 RT_CONVERT_LOCK(ro_fwd->ro_rt);
1738 IFA_ADDREF(&ia_fw->ia_ifa);
1739 }
1c79356b 1740 ifp = ro_fwd->ro_rt->rt_ifp;
1c79356b
A
1741 ro_fwd->ro_rt->rt_use++;
1742 if (ro_fwd->ro_rt->rt_flags & RTF_GATEWAY)
39236c6e 1743 dst = SIN(ro_fwd->ro_rt->rt_gateway);
7e4a7d39 1744 if (ro_fwd->ro_rt->rt_flags & RTF_HOST) {
39236c6e
A
1745 /* double negation needed for bool bit field */
1746 ipobf.isbroadcast =
1747 !!(ro_fwd->ro_rt->rt_flags & RTF_BROADCAST);
7e4a7d39
A
1748 } else {
1749 /* Become a regular mutex */
1750 RT_CONVERT_LOCK(ro_fwd->ro_rt);
39236c6e
A
1751 ipobf.isbroadcast =
1752 in_broadcast(dst->sin_addr, ifp);
7e4a7d39 1753 }
b0d623f7 1754 RT_UNLOCK(ro_fwd->ro_rt);
39236c6e 1755 ROUTE_RELEASE(ro);
1c79356b 1756 ro->ro_rt = ro_fwd->ro_rt;
39236c6e
A
1757 ro_fwd->ro_rt = NULL;
1758 dst = SIN(&ro_fwd->ro_dst);
1c79356b
A
1759
1760 /*
1761 * If we added a default src ip earlier,
1762 * which would have been gotten from the-then
1763 * interface, do it again, from the new one.
1764 */
b0d623f7 1765 if (ia_fw != NULL) {
39236c6e 1766 if (ipobf.fwd_rewrite_src) {
6d2010ae 1767 IFA_LOCK_SPIN(&ia_fw->ia_ifa);
b0d623f7 1768 ip->ip_src = IA_SIN(ia_fw)->sin_addr;
6d2010ae
A
1769 IFA_UNLOCK(&ia_fw->ia_ifa);
1770 }
1771 IFA_REMREF(&ia_fw->ia_ifa);
b0d623f7 1772 }
39236c6e 1773 goto pass;
1c79356b
A
1774 }
1775#endif /* IPFIREWALL_FORWARD */
39236c6e
A
1776 /*
1777 * if we get here, none of the above matches, and
1778 * we have to drop the pkt
1779 */
1c79356b 1780 m_freem(m);
91447636 1781 error = EACCES; /* not sure this is the right error msg */
91447636 1782 goto done;
1c79356b 1783 }
1c79356b
A
1784
1785pass:
6d2010ae 1786#endif /* IPFIREWALL */
39236c6e
A
1787
1788 /* 127/8 must not appear on wire - RFC1122 */
1789 if (!(ifp->if_flags & IFF_LOOPBACK) &&
1790 ((ntohl(ip->ip_src.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET ||
1791 (ntohl(ip->ip_dst.s_addr) >> IN_CLASSA_NSHIFT) == IN_LOOPBACKNET)) {
b0d623f7 1792 OSAddAtomic(1, &ipstat.ips_badaddr);
39236c6e 1793 error = EADDRNOTAVAIL;
fe8ab488 1794 goto bad;
e5568f75 1795 }
316670eb 1796
39037602
A
1797 if (ipoa != NULL) {
1798 u_int8_t dscp = ip->ip_tos >> IPTOS_DSCP_SHIFT;
1799
1800 error = set_packet_qos(m, ifp,
1801 ipoa->ipoa_flags & IPOAF_QOSMARKING_ALLOWED ? TRUE : FALSE,
1802 ipoa->ipoa_sotc, ipoa->ipoa_netsvctype, &dscp);
1803 if (error == 0) {
1804 ip->ip_tos &= IPTOS_ECN_MASK;
1805 ip->ip_tos |= dscp << IPTOS_DSCP_SHIFT;
1806 } else {
1807 printf("%s if_dscp_for_mbuf() error %d\n", __func__, error);
1808 error = 0;
1809 }
1810 }
1811
1812 /*
1813 * Some Wi-Fi AP implementations do not correctly handle multicast IP
1814 * packets with DSCP bits set -- see radr://9331522 -- so as a
1815 * workaround we clear the DSCP bits and set the service class to BE
1816 */
1817 if (IN_MULTICAST(ntohl(pkt_dst.s_addr)) && IFNET_IS_WIFI_INFRA(ifp)) {
1818 ip->ip_tos &= IPTOS_ECN_MASK;
1819 mbuf_set_service_class(m, MBUF_SC_BE);
1820 }
1821
39236c6e
A
1822 ip_output_checksum(ifp, m, (IP_VHL_HL(ip->ip_vhl) << 2),
1823 ip->ip_len, &sw_csum);
0b4e3aa0 1824
1c79356b 1825 /*
9bccf70c 1826 * If small enough for interface, or the interface will take
0b4e3aa0 1827 * care of the fragmentation for us, can just send directly.
1c79356b 1828 */
39236c6e
A
1829 if ((u_short)ip->ip_len <= ifp->if_mtu || TSO_IPV4_OK(ifp, m) ||
1830 (!(ip->ip_off & IP_DF) && (ifp->if_hwassist & CSUM_FRAGMENT))) {
b0d623f7 1831#if BYTE_ORDER != BIG_ENDIAN
9bccf70c
A
1832 HTONS(ip->ip_len);
1833 HTONS(ip->ip_off);
b0d623f7 1834#endif
316670eb 1835
1c79356b 1836 ip->ip_sum = 0;
9bccf70c 1837 if (sw_csum & CSUM_DELAY_IP) {
39236c6e
A
1838 ip->ip_sum = ip_cksum_hdr_out(m, hlen);
1839 sw_csum &= ~CSUM_DELAY_IP;
1840 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_IP;
9bccf70c 1841 }
316670eb 1842
9bccf70c
A
1843#if IPSEC
1844 /* clean ipsec history once it goes out of the node */
39236c6e 1845 if (ipsec_bypass == 0 && !(flags & IP_NOIPSEC))
9bccf70c 1846 ipsec_delaux(m);
39236c6e
A
1847#endif /* IPSEC */
1848 if ((m->m_pkthdr.csum_flags & CSUM_TSO_IPV4) &&
1849 (m->m_pkthdr.tso_segsz > 0))
1850 scnt += m->m_pkthdr.len / m->m_pkthdr.tso_segsz;
1851 else
1852 scnt++;
1853
91447636 1854 if (packetchain == 0) {
39236c6e
A
1855 if (ro->ro_rt != NULL && nstat_collect)
1856 nstat_route_tx(ro->ro_rt, scnt,
1857 m->m_pkthdr.len, 0);
1858
316670eb 1859 error = dlil_output(ifp, PF_INET, m, ro->ro_rt,
39236c6e 1860 SA(dst), 0, adv);
fe8ab488
A
1861 if (dlil_verbose && error) {
1862 printf("dlil_output error on interface %s: %d\n",
1863 ifp->if_xname, error);
1864 }
39236c6e 1865 scnt = 0;
2d21ac55 1866 goto done;
39236c6e
A
1867 } else {
1868 /*
1869 * packet chaining allows us to reuse the
1870 * route for all packets
1871 */
6d2010ae 1872 bytecnt += m->m_pkthdr.len;
b0d623f7 1873 mppn = &m->m_nextpkt;
91447636
A
1874 m = m->m_nextpkt;
1875 if (m == NULL) {
b0d623f7
A
1876#if PF
1877sendchain:
1878#endif /* PF */
91447636
A
1879 if (pktcnt > ip_maxchainsent)
1880 ip_maxchainsent = pktcnt;
39236c6e
A
1881 if (ro->ro_rt != NULL && nstat_collect)
1882 nstat_route_tx(ro->ro_rt, scnt,
1883 bytecnt, 0);
1884
316670eb 1885 error = dlil_output(ifp, PF_INET, packetlist,
39236c6e 1886 ro->ro_rt, SA(dst), 0, adv);
fe8ab488
A
1887 if (dlil_verbose && error) {
1888 printf("dlil_output error on interface %s: %d\n",
1889 ifp->if_xname, error);
1890 }
91447636 1891 pktcnt = 0;
39236c6e 1892 scnt = 0;
6d2010ae 1893 bytecnt = 0;
91447636 1894 goto done;
316670eb 1895
91447636
A
1896 }
1897 m0 = m;
1898 pktcnt++;
1899 goto loopit;
1900 }
1c79356b
A
1901 }
1902 /*
1903 * Too large for interface; fragment if possible.
1904 * Must be able to put at least 8 bytes per fragment.
39236c6e 1905 * Balk when DF bit is set or the interface didn't support TSO.
1c79356b 1906 */
39236c6e
A
1907 if ((ip->ip_off & IP_DF) || pktcnt > 0 ||
1908 (m->m_pkthdr.csum_flags & CSUM_TSO_IPV4)) {
1c79356b
A
1909 error = EMSGSIZE;
1910 /*
1911 * This case can happen if the user changed the MTU
1912 * of an interface after enabling IP on it. Because
1913 * most netifs don't keep track of routes pointing to
1914 * them, there is no way for one to update all its
1915 * routes when the MTU is changed.
1916 */
6d2010ae
A
1917 if (ro->ro_rt) {
1918 RT_LOCK_SPIN(ro->ro_rt);
39236c6e
A
1919 if ((ro->ro_rt->rt_flags & (RTF_UP | RTF_HOST)) &&
1920 !(ro->ro_rt->rt_rmx.rmx_locks & RTV_MTU) &&
1921 (ro->ro_rt->rt_rmx.rmx_mtu > ifp->if_mtu)) {
6d2010ae
A
1922 ro->ro_rt->rt_rmx.rmx_mtu = ifp->if_mtu;
1923 }
1924 RT_UNLOCK(ro->ro_rt);
1925 }
1926 if (pktcnt > 0) {
1927 m0 = packetlist;
1c79356b 1928 }
b0d623f7 1929 OSAddAtomic(1, &ipstat.ips_cantfrag);
1c79356b
A
1930 goto bad;
1931 }
b0d623f7
A
1932
1933 error = ip_fragment(m, ifp, ifp->if_mtu, sw_csum);
1934 if (error != 0) {
1935 m0 = m = NULL;
1c79356b
A
1936 goto bad;
1937 }
1938
39236c6e
A
1939 KERNEL_DEBUG(DBG_LAYER_END, ip->ip_dst.s_addr,
1940 ip->ip_src.s_addr, ip->ip_p, ip->ip_off, ip->ip_len);
b0d623f7
A
1941
1942 for (m = m0; m; m = m0) {
1943 m0 = m->m_nextpkt;
1944 m->m_nextpkt = 0;
1945#if IPSEC
1946 /* clean ipsec history once it goes out of the node */
39236c6e 1947 if (ipsec_bypass == 0 && !(flags & IP_NOIPSEC))
b0d623f7 1948 ipsec_delaux(m);
39236c6e 1949#endif /* IPSEC */
b0d623f7 1950 if (error == 0) {
39236c6e
A
1951 if ((packetchain != 0) && (pktcnt > 0)) {
1952 panic("%s: mix of packet in packetlist is "
1953 "wrong=%p", __func__, packetlist);
1954 /* NOTREACHED */
1955 }
1956 if (ro->ro_rt != NULL && nstat_collect) {
1957 nstat_route_tx(ro->ro_rt, 1,
1958 m->m_pkthdr.len, 0);
b0d623f7 1959 }
316670eb 1960 error = dlil_output(ifp, PF_INET, m, ro->ro_rt,
39236c6e 1961 SA(dst), 0, adv);
fe8ab488
A
1962 if (dlil_verbose && error) {
1963 printf("dlil_output error on interface %s: %d\n",
1964 ifp->if_xname, error);
1965 }
39236c6e 1966 } else {
b0d623f7 1967 m_freem(m);
39236c6e 1968 }
b0d623f7
A
1969 }
1970
1971 if (error == 0)
1972 OSAddAtomic(1, &ipstat.ips_fragmented);
1973
1974done:
39236c6e 1975 if (ia != NULL) {
6d2010ae 1976 IFA_REMREF(&ia->ia_ifa);
b0d623f7
A
1977 ia = NULL;
1978 }
1979#if IPSEC
39236c6e 1980 ROUTE_RELEASE(&ipsec_state.ro);
b0d623f7
A
1981 if (sp != NULL) {
1982 KEYDEBUG(KEYDEBUG_IPSEC_STAMP,
39236c6e 1983 printf("DP ip_output call free SP:%x\n", sp));
b0d623f7
A
1984 key_freesp(sp, KEY_SADB_UNLOCKED);
1985 }
b0d623f7 1986#endif /* IPSEC */
fe8ab488
A
1987#if NECP
1988 ROUTE_RELEASE(&necp_route);
1989#endif /* NECP */
39236c6e
A
1990#if DUMMYNET
1991 ROUTE_RELEASE(&saved_route);
1992#endif /* DUMMYNET */
1993#if IPFIREWALL_FORWARD
1994 ROUTE_RELEASE(&sro_fwd);
1995#endif /* IPFIREWALL_FORWARD */
b0d623f7 1996
39236c6e 1997 KERNEL_DEBUG(DBG_FNC_IP_OUTPUT | DBG_FUNC_END, error, 0, 0, 0, 0);
3e170ce0
A
1998 if (ip_output_measure) {
1999 net_perf_measure_time(&net_perf, &start_tv, packets_processed);
2000 net_perf_histogram(&net_perf, packets_processed);
2001 }
b0d623f7
A
2002 return (error);
2003bad:
fe8ab488
A
2004 if (pktcnt > 0)
2005 m0 = packetlist;
2006 m_freem_list(m0);
b0d623f7 2007 goto done;
39236c6e
A
2008
2009#undef ipsec_state
2010#undef args
2011#undef sro_fwd
2012#undef saved_route
2013#undef ipf_pktopts
fe8ab488 2014#undef IP_CHECK_RESTRICTIONS
b0d623f7
A
2015}
2016
2017int
2018ip_fragment(struct mbuf *m, struct ifnet *ifp, unsigned long mtu, int sw_csum)
2019{
2020 struct ip *ip, *mhip;
2021 int len, hlen, mhlen, firstlen, off, error = 0;
2022 struct mbuf **mnext = &m->m_nextpkt, *m0;
2023 int nfrags = 1;
2024
2025 ip = mtod(m, struct ip *);
2026#ifdef _IP_VHL
2027 hlen = IP_VHL_HL(ip->ip_vhl) << 2;
39236c6e 2028#else /* !_IP_VHL */
b0d623f7 2029 hlen = ip->ip_hl << 2;
39236c6e 2030#endif /* !_IP_VHL */
b0d623f7
A
2031
2032 firstlen = len = (mtu - hlen) &~ 7;
2033 if (len < 8) {
2034 m_freem(m);
2035 return (EMSGSIZE);
2036 }
2037
9bccf70c
A
2038 /*
2039 * if the interface will not calculate checksums on
2040 * fragmented packets, then do it here.
2041 */
39236c6e
A
2042 if ((m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) &&
2043 !(ifp->if_hwassist & CSUM_IP_FRAGS))
9bccf70c 2044 in_delayed_cksum(m);
0b4e3aa0 2045
1c79356b
A
2046 /*
2047 * Loop through length of segment after first fragment,
2048 * make new header and copy data of each part and link onto chain.
2049 */
2050 m0 = m;
2051 mhlen = sizeof (struct ip);
2052 for (off = hlen + len; off < (u_short)ip->ip_len; off += len) {
2d21ac55 2053 MGETHDR(m, M_DONTWAIT, MT_HEADER); /* MAC-OK */
39236c6e 2054 if (m == NULL) {
1c79356b 2055 error = ENOBUFS;
b0d623f7 2056 OSAddAtomic(1, &ipstat.ips_odropped);
1c79356b
A
2057 goto sendorfree;
2058 }
0b4e3aa0 2059 m->m_flags |= (m0->m_flags & M_MCAST) | M_FRAG;
1c79356b
A
2060 m->m_data += max_linkhdr;
2061 mhip = mtod(m, struct ip *);
2062 *mhip = *ip;
2063 if (hlen > sizeof (struct ip)) {
2064 mhlen = ip_optcopy(ip, mhip) + sizeof (struct ip);
2065 mhip->ip_vhl = IP_MAKE_VHL(IPVERSION, mhlen >> 2);
2066 }
2067 m->m_len = mhlen;
2068 mhip->ip_off = ((off - hlen) >> 3) + (ip->ip_off & ~IP_MF);
2069 if (ip->ip_off & IP_MF)
2070 mhip->ip_off |= IP_MF;
2071 if (off + len >= (u_short)ip->ip_len)
2072 len = (u_short)ip->ip_len - off;
2073 else
2074 mhip->ip_off |= IP_MF;
2075 mhip->ip_len = htons((u_short)(len + mhlen));
2076 m->m_next = m_copy(m0, off, len);
39236c6e 2077 if (m->m_next == NULL) {
1c79356b
A
2078 (void) m_free(m);
2079 error = ENOBUFS; /* ??? */
b0d623f7 2080 OSAddAtomic(1, &ipstat.ips_odropped);
1c79356b
A
2081 goto sendorfree;
2082 }
2083 m->m_pkthdr.len = mhlen + len;
39236c6e 2084 m->m_pkthdr.rcvif = NULL;
9bccf70c 2085 m->m_pkthdr.csum_flags = m0->m_pkthdr.csum_flags;
316670eb 2086
39236c6e 2087 M_COPY_CLASSIFIER(m, m0);
316670eb 2088 M_COPY_PFTAG(m, m0);
316670eb 2089
2d21ac55
A
2090#if CONFIG_MACF_NET
2091 mac_netinet_fragment(m0, m);
39236c6e 2092#endif /* CONFIG_MACF_NET */
b0d623f7
A
2093
2094#if BYTE_ORDER != BIG_ENDIAN
9bccf70c 2095 HTONS(mhip->ip_off);
b0d623f7
A
2096#endif
2097
1c79356b 2098 mhip->ip_sum = 0;
9bccf70c 2099 if (sw_csum & CSUM_DELAY_IP) {
39236c6e
A
2100 mhip->ip_sum = ip_cksum_hdr_out(m, mhlen);
2101 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_IP;
9bccf70c 2102 }
1c79356b
A
2103 *mnext = m;
2104 mnext = &m->m_nextpkt;
0b4e3aa0 2105 nfrags++;
1c79356b 2106 }
b0d623f7 2107 OSAddAtomic(nfrags, &ipstat.ips_ofragments);
0b4e3aa0
A
2108
2109 /* set first/last markers for fragment chain */
9bccf70c
A
2110 m->m_flags |= M_LASTFRAG;
2111 m0->m_flags |= M_FIRSTFRAG | M_FRAG;
0b4e3aa0
A
2112 m0->m_pkthdr.csum_data = nfrags;
2113
1c79356b
A
2114 /*
2115 * Update first fragment by trimming what's been copied out
2116 * and updating header, then send each fragment (in order).
2117 */
2118 m = m0;
2119 m_adj(m, hlen + firstlen - (u_short)ip->ip_len);
2120 m->m_pkthdr.len = hlen + firstlen;
2121 ip->ip_len = htons((u_short)m->m_pkthdr.len);
9bccf70c 2122 ip->ip_off |= IP_MF;
b0d623f7
A
2123
2124#if BYTE_ORDER != BIG_ENDIAN
9bccf70c 2125 HTONS(ip->ip_off);
b0d623f7 2126#endif
316670eb 2127
1c79356b 2128 ip->ip_sum = 0;
9bccf70c 2129 if (sw_csum & CSUM_DELAY_IP) {
39236c6e
A
2130 ip->ip_sum = ip_cksum_hdr_out(m, hlen);
2131 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_IP;
9bccf70c 2132 }
1c79356b 2133sendorfree:
b0d623f7
A
2134 if (error)
2135 m_freem_list(m0);
1c79356b 2136
1c79356b 2137 return (error);
1c79356b
A
2138}
2139
2d21ac55
A
2140static void
2141ip_out_cksum_stats(int proto, u_int32_t len)
2142{
2143 switch (proto) {
2144 case IPPROTO_TCP:
2145 tcp_out_cksum_stats(len);
2146 break;
2147 case IPPROTO_UDP:
2148 udp_out_cksum_stats(len);
2149 break;
2150 default:
2151 /* keep only TCP or UDP stats for now */
2152 break;
2153 }
2154}
2155
39236c6e
A
2156/*
2157 * Process a delayed payload checksum calculation (outbound path.)
2158 *
2159 * hoff is the number of bytes beyond the mbuf data pointer which
2160 * points to the IP header.
2161 *
2162 * Returns a bitmask representing all the work done in software.
2163 */
2164uint32_t
2165in_finalize_cksum(struct mbuf *m, uint32_t hoff, uint32_t csum_flags)
0b4e3aa0 2166{
39236c6e 2167 unsigned char buf[15 << 2] __attribute__((aligned(8)));
9bccf70c 2168 struct ip *ip;
39236c6e
A
2169 uint32_t offset, _hlen, mlen, hlen, len, sw_csum;
2170 uint16_t csum, ip_len;
6d2010ae 2171
39236c6e
A
2172 _CASSERT(sizeof (csum) == sizeof (uint16_t));
2173 VERIFY(m->m_flags & M_PKTHDR);
6d2010ae 2174
39236c6e
A
2175 sw_csum = (csum_flags & m->m_pkthdr.csum_flags);
2176
2177 if ((sw_csum &= (CSUM_DELAY_IP | CSUM_DELAY_DATA)) == 0)
2178 goto done;
2179
2180 mlen = m->m_pkthdr.len; /* total mbuf len */
2181
2182 /* sanity check (need at least simple IP header) */
2183 if (mlen < (hoff + sizeof (*ip))) {
2184 panic("%s: mbuf %p pkt len (%u) < hoff+ip_hdr "
2185 "(%u+%u)\n", __func__, m, mlen, hoff,
2186 (uint32_t)sizeof (*ip));
2187 /* NOTREACHED */
91447636 2188 }
316670eb
A
2189
2190 /*
39236c6e
A
2191 * In case the IP header is not contiguous, or not 32-bit aligned,
2192 * or if we're computing the IP header checksum, copy it to a local
2193 * buffer. Copy only the simple IP header here (IP options case
2194 * is handled below.)
316670eb 2195 */
39236c6e
A
2196 if ((sw_csum & CSUM_DELAY_IP) || (hoff + sizeof (*ip)) > m->m_len ||
2197 !IP_HDR_ALIGNED_P(mtod(m, caddr_t) + hoff)) {
2198 m_copydata(m, hoff, sizeof (*ip), (caddr_t)buf);
316670eb 2199 ip = (struct ip *)(void *)buf;
39236c6e 2200 _hlen = sizeof (*ip);
8f6c56a5 2201 } else {
39236c6e
A
2202 ip = (struct ip *)(void *)(m->m_data + hoff);
2203 _hlen = 0;
91447636 2204 }
316670eb 2205
39236c6e 2206 hlen = IP_VHL_HL(ip->ip_vhl) << 2; /* IP header len */
316670eb 2207
39236c6e
A
2208 /* sanity check */
2209 if (mlen < (hoff + hlen)) {
2210 panic("%s: mbuf %p pkt too short (%d) for IP header (%u), "
2211 "hoff %u", __func__, m, mlen, hlen, hoff);
2212 /* NOTREACHED */
2213 }
8f6c56a5
A
2214
2215 /*
2216 * We could be in the context of an IP or interface filter; in the
2217 * former case, ip_len would be in host (correct) order while for
2218 * the latter it would be in network order. Because of this, we
2219 * attempt to interpret the length field by comparing it against
2220 * the actual packet length. If the comparison fails, byte swap
39236c6e
A
2221 * the length and check again. If it still fails, use the actual
2222 * packet length. This also covers the trailing bytes case.
8f6c56a5
A
2223 */
2224 ip_len = ip->ip_len;
39236c6e
A
2225 if (ip_len != (mlen - hoff)) {
2226 ip_len = OSSwapInt16(ip_len);
2227 if (ip_len != (mlen - hoff)) {
2228 printf("%s: mbuf 0x%llx proto %d IP len %d (%x) "
2229 "[swapped %d (%x)] doesn't match actual packet "
2230 "length; %d is used instead\n", __func__,
2231 (uint64_t)VM_KERNEL_ADDRPERM(m), ip->ip_p,
2232 ip->ip_len, ip->ip_len, ip_len, ip_len,
2233 (mlen - hoff));
2234 ip_len = mlen - hoff;
8f6c56a5
A
2235 }
2236 }
2237
39236c6e 2238 len = ip_len - hlen; /* csum span */
8f6c56a5 2239
39236c6e
A
2240 if (sw_csum & CSUM_DELAY_DATA) {
2241 uint16_t ulpoff;
2d21ac55 2242
39236c6e
A
2243 /*
2244 * offset is added to the lower 16-bit value of csum_data,
2245 * which is expected to contain the ULP offset; therefore
2246 * CSUM_PARTIAL offset adjustment must be undone.
2247 */
2248 if ((m->m_pkthdr.csum_flags & (CSUM_PARTIAL|CSUM_DATA_VALID)) ==
2249 (CSUM_PARTIAL|CSUM_DATA_VALID)) {
2250 /*
2251 * Get back the original ULP offset (this will
2252 * undo the CSUM_PARTIAL logic in ip_output.)
2253 */
2254 m->m_pkthdr.csum_data = (m->m_pkthdr.csum_tx_stuff -
2255 m->m_pkthdr.csum_tx_start);
2256 }
8f6c56a5 2257
39236c6e
A
2258 ulpoff = (m->m_pkthdr.csum_data & 0xffff); /* ULP csum offset */
2259 offset = hoff + hlen; /* ULP header */
0b4e3aa0 2260
39236c6e
A
2261 if (mlen < (ulpoff + sizeof (csum))) {
2262 panic("%s: mbuf %p pkt len (%u) proto %d invalid ULP "
2263 "cksum offset (%u) cksum flags 0x%x\n", __func__,
2264 m, mlen, ip->ip_p, ulpoff, m->m_pkthdr.csum_flags);
2265 /* NOTREACHED */
2266 }
0b4e3aa0 2267
39236c6e 2268 csum = inet_cksum(m, 0, offset, len);
316670eb 2269
39236c6e
A
2270 /* Update stats */
2271 ip_out_cksum_stats(ip->ip_p, len);
91447636 2272
39236c6e 2273 /* RFC1122 4.1.3.4 */
5ba3f43e
A
2274 if (csum == 0 &&
2275 (m->m_pkthdr.csum_flags & (CSUM_UDP|CSUM_ZERO_INVERT)))
39236c6e 2276 csum = 0xffff;
91447636 2277
39236c6e
A
2278 /* Insert the checksum in the ULP csum field */
2279 offset += ulpoff;
2280 if (offset + sizeof (csum) > m->m_len) {
2281 m_copyback(m, offset, sizeof (csum), &csum);
2282 } else if (IP_HDR_ALIGNED_P(mtod(m, char *) + hoff)) {
2283 *(uint16_t *)(void *)(mtod(m, char *) + offset) = csum;
2284 } else {
2285 bcopy(&csum, (mtod(m, char *) + offset), sizeof (csum));
91447636 2286 }
5ba3f43e
A
2287 m->m_pkthdr.csum_flags &= ~(CSUM_DELAY_DATA | CSUM_DATA_VALID |
2288 CSUM_PARTIAL | CSUM_ZERO_INVERT);
91447636 2289 }
8f6c56a5 2290
39236c6e
A
2291 if (sw_csum & CSUM_DELAY_IP) {
2292 /* IP header must be in the local buffer */
2293 VERIFY(_hlen == sizeof (*ip));
2294 if (_hlen != hlen) {
2295 VERIFY(hlen <= sizeof (buf));
2296 m_copydata(m, hoff, hlen, (caddr_t)buf);
2297 ip = (struct ip *)(void *)buf;
2298 _hlen = hlen;
2299 }
8f6c56a5 2300
39236c6e
A
2301 /*
2302 * Compute the IP header checksum as if the IP length
2303 * is the length which we believe is "correct"; see
2304 * how ip_len gets calculated above. Note that this
2305 * is done on the local copy and not on the real one.
2306 */
2307 ip->ip_len = htons(ip_len);
8f6c56a5 2308 ip->ip_sum = 0;
39236c6e 2309 csum = in_cksum_hdr_opt(ip);
316670eb 2310
39236c6e
A
2311 /* Update stats */
2312 ipstat.ips_snd_swcsum++;
2313 ipstat.ips_snd_swcsum_bytes += hlen;
91447636 2314
39236c6e
A
2315 /*
2316 * Insert only the checksum in the existing IP header
2317 * csum field; all other fields are left unchanged.
2318 */
2319 offset = hoff + offsetof(struct ip, ip_sum);
2320 if (offset + sizeof (csum) > m->m_len) {
2321 m_copyback(m, offset, sizeof (csum), &csum);
2322 } else if (IP_HDR_ALIGNED_P(mtod(m, char *) + hoff)) {
2323 *(uint16_t *)(void *)(mtod(m, char *) + offset) = csum;
2324 } else {
2325 bcopy(&csum, (mtod(m, char *) + offset), sizeof (csum));
8f6c56a5 2326 }
39236c6e 2327 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_IP;
8f6c56a5
A
2328 }
2329
39236c6e
A
2330done:
2331 return (sw_csum);
0b4e3aa0
A
2332}
2333
1c79356b
A
2334/*
2335 * Insert IP options into preformed packet.
2336 * Adjust IP destination as required for IP source routing,
2337 * as indicated by a non-zero in_addr at the start of the options.
2338 *
2339 * XXX This routine assumes that the packet has no options in place.
2340 */
2341static struct mbuf *
39236c6e 2342ip_insertoptions(struct mbuf *m, struct mbuf *opt, int *phlen)
1c79356b 2343{
39236c6e 2344 struct ipoption *p = mtod(opt, struct ipoption *);
1c79356b 2345 struct mbuf *n;
39236c6e 2346 struct ip *ip = mtod(m, struct ip *);
1c79356b
A
2347 unsigned optlen;
2348
39236c6e 2349 optlen = opt->m_len - sizeof (p->ipopt_dst);
1c79356b
A
2350 if (optlen + (u_short)ip->ip_len > IP_MAXPACKET)
2351 return (m); /* XXX should fail */
2352 if (p->ipopt_dst.s_addr)
2353 ip->ip_dst = p->ipopt_dst;
2354 if (m->m_flags & M_EXT || m->m_data - optlen < m->m_pktdat) {
2d21ac55 2355 MGETHDR(n, M_DONTWAIT, MT_HEADER); /* MAC-OK */
39236c6e 2356 if (n == NULL)
1c79356b 2357 return (m);
91447636 2358 n->m_pkthdr.rcvif = 0;
2d21ac55
A
2359#if CONFIG_MACF_NET
2360 mac_mbuf_label_copy(m, n);
39236c6e 2361#endif /* CONFIG_MACF_NET */
1c79356b 2362 n->m_pkthdr.len = m->m_pkthdr.len + optlen;
39236c6e
A
2363 m->m_len -= sizeof (struct ip);
2364 m->m_data += sizeof (struct ip);
1c79356b
A
2365 n->m_next = m;
2366 m = n;
39236c6e 2367 m->m_len = optlen + sizeof (struct ip);
1c79356b 2368 m->m_data += max_linkhdr;
39236c6e 2369 (void) memcpy(mtod(m, void *), ip, sizeof (struct ip));
1c79356b
A
2370 } else {
2371 m->m_data -= optlen;
2372 m->m_len += optlen;
2373 m->m_pkthdr.len += optlen;
39236c6e 2374 ovbcopy((caddr_t)ip, mtod(m, caddr_t), sizeof (struct ip));
1c79356b
A
2375 }
2376 ip = mtod(m, struct ip *);
2377 bcopy(p->ipopt_list, ip + 1, optlen);
39236c6e 2378 *phlen = sizeof (struct ip) + optlen;
1c79356b
A
2379 ip->ip_vhl = IP_MAKE_VHL(IPVERSION, *phlen >> 2);
2380 ip->ip_len += optlen;
2381 return (m);
2382}
2383
2384/*
2385 * Copy options from ip to jp,
2386 * omitting those not copied during fragmentation.
2387 */
39236c6e
A
2388static int
2389ip_optcopy(struct ip *ip, struct ip *jp)
1c79356b 2390{
39236c6e 2391 u_char *cp, *dp;
1c79356b
A
2392 int opt, optlen, cnt;
2393
2394 cp = (u_char *)(ip + 1);
2395 dp = (u_char *)(jp + 1);
2396 cnt = (IP_VHL_HL(ip->ip_vhl) << 2) - sizeof (struct ip);
2397 for (; cnt > 0; cnt -= optlen, cp += optlen) {
2398 opt = cp[0];
2399 if (opt == IPOPT_EOL)
2400 break;
2401 if (opt == IPOPT_NOP) {
2402 /* Preserve for IP mcast tunnel's LSRR alignment. */
2403 *dp++ = IPOPT_NOP;
2404 optlen = 1;
2405 continue;
9bccf70c
A
2406 }
2407#if DIAGNOSTIC
39236c6e 2408 if (cnt < IPOPT_OLEN + sizeof (*cp)) {
9bccf70c 2409 panic("malformed IPv4 option passed to ip_optcopy");
39236c6e
A
2410 /* NOTREACHED */
2411 }
9bccf70c
A
2412#endif
2413 optlen = cp[IPOPT_OLEN];
2414#if DIAGNOSTIC
39236c6e 2415 if (optlen < IPOPT_OLEN + sizeof (*cp) || optlen > cnt) {
9bccf70c 2416 panic("malformed IPv4 option passed to ip_optcopy");
39236c6e
A
2417 /* NOTREACHED */
2418 }
9bccf70c 2419#endif
1c79356b
A
2420 /* bogus lengths should have been caught by ip_dooptions */
2421 if (optlen > cnt)
2422 optlen = cnt;
2423 if (IPOPT_COPIED(opt)) {
2424 bcopy(cp, dp, optlen);
2425 dp += optlen;
2426 }
2427 }
2428 for (optlen = dp - (u_char *)(jp+1); optlen & 0x3; optlen++)
2429 *dp++ = IPOPT_EOL;
2430 return (optlen);
2431}
2432
2433/*
2434 * IP socket option processing.
2435 */
2436int
39236c6e 2437ip_ctloutput(struct socket *so, struct sockopt *sopt)
1c79356b
A
2438{
2439 struct inpcb *inp = sotoinpcb(so);
2440 int error, optval;
2441
2442 error = optval = 0;
39236c6e 2443 if (sopt->sopt_level != IPPROTO_IP)
1c79356b 2444 return (EINVAL);
1c79356b
A
2445
2446 switch (sopt->sopt_dir) {
2447 case SOPT_SET:
2448 switch (sopt->sopt_name) {
1c79356b
A
2449#ifdef notyet
2450 case IP_RETOPTS:
2451#endif
39236c6e 2452 case IP_OPTIONS: {
1c79356b 2453 struct mbuf *m;
39236c6e 2454
1c79356b
A
2455 if (sopt->sopt_valsize > MLEN) {
2456 error = EMSGSIZE;
2457 break;
2458 }
b0d623f7
A
2459 MGET(m, sopt->sopt_p != kernproc ? M_WAIT : M_DONTWAIT,
2460 MT_HEADER);
39236c6e 2461 if (m == NULL) {
1c79356b
A
2462 error = ENOBUFS;
2463 break;
2464 }
2465 m->m_len = sopt->sopt_valsize;
39236c6e
A
2466 error = sooptcopyin(sopt, mtod(m, char *),
2467 m->m_len, m->m_len);
5ba3f43e
A
2468 if (error) {
2469 m_freem(m);
1c79356b 2470 break;
5ba3f43e 2471 }
39236c6e
A
2472
2473 return (ip_pcbopts(sopt->sopt_name,
2474 &inp->inp_options, m));
1c79356b
A
2475 }
2476
2477 case IP_TOS:
2478 case IP_TTL:
2479 case IP_RECVOPTS:
2480 case IP_RECVRETOPTS:
2481 case IP_RECVDSTADDR:
2482 case IP_RECVIF:
55e303ae 2483 case IP_RECVTTL:
6d2010ae 2484 case IP_RECVPKTINFO:
813fb2f6 2485 case IP_RECVTOS:
39236c6e
A
2486 error = sooptcopyin(sopt, &optval, sizeof (optval),
2487 sizeof (optval));
1c79356b
A
2488 if (error)
2489 break;
2490
2491 switch (sopt->sopt_name) {
2492 case IP_TOS:
2493 inp->inp_ip_tos = optval;
2494 break;
2495
2496 case IP_TTL:
2497 inp->inp_ip_ttl = optval;
2498 break;
2499#define OPTSET(bit) \
2500 if (optval) \
2501 inp->inp_flags |= bit; \
2502 else \
2503 inp->inp_flags &= ~bit;
2504
2505 case IP_RECVOPTS:
2506 OPTSET(INP_RECVOPTS);
2507 break;
2508
2509 case IP_RECVRETOPTS:
2510 OPTSET(INP_RECVRETOPTS);
2511 break;
2512
2513 case IP_RECVDSTADDR:
2514 OPTSET(INP_RECVDSTADDR);
2515 break;
2516
2517 case IP_RECVIF:
2518 OPTSET(INP_RECVIF);
2519 break;
2520
55e303ae
A
2521 case IP_RECVTTL:
2522 OPTSET(INP_RECVTTL);
2523 break;
2524
6d2010ae
A
2525 case IP_RECVPKTINFO:
2526 OPTSET(INP_PKTINFO);
2527 break;
813fb2f6
A
2528
2529 case IP_RECVTOS:
2530 OPTSET(INP_RECVTOS);
2531 break;
2532 #undef OPTSET
1c79356b
A
2533 }
2534 break;
6d2010ae
A
2535 /*
2536 * Multicast socket options are processed by the in_mcast
2537 * module.
2538 */
1c79356b 2539 case IP_MULTICAST_IF:
6d2010ae 2540 case IP_MULTICAST_IFINDEX:
1c79356b
A
2541 case IP_MULTICAST_VIF:
2542 case IP_MULTICAST_TTL:
2543 case IP_MULTICAST_LOOP:
2544 case IP_ADD_MEMBERSHIP:
2545 case IP_DROP_MEMBERSHIP:
6d2010ae
A
2546 case IP_ADD_SOURCE_MEMBERSHIP:
2547 case IP_DROP_SOURCE_MEMBERSHIP:
2548 case IP_BLOCK_SOURCE:
2549 case IP_UNBLOCK_SOURCE:
2550 case IP_MSFILTER:
2551 case MCAST_JOIN_GROUP:
2552 case MCAST_LEAVE_GROUP:
2553 case MCAST_JOIN_SOURCE_GROUP:
2554 case MCAST_LEAVE_SOURCE_GROUP:
2555 case MCAST_BLOCK_SOURCE:
2556 case MCAST_UNBLOCK_SOURCE:
2557 error = inp_setmoptions(inp, sopt);
1c79356b
A
2558 break;
2559
2560 case IP_PORTRANGE:
39236c6e
A
2561 error = sooptcopyin(sopt, &optval, sizeof (optval),
2562 sizeof (optval));
1c79356b
A
2563 if (error)
2564 break;
2565
2566 switch (optval) {
2567 case IP_PORTRANGE_DEFAULT:
2568 inp->inp_flags &= ~(INP_LOWPORT);
2569 inp->inp_flags &= ~(INP_HIGHPORT);
2570 break;
2571
2572 case IP_PORTRANGE_HIGH:
2573 inp->inp_flags &= ~(INP_LOWPORT);
2574 inp->inp_flags |= INP_HIGHPORT;
2575 break;
2576
2577 case IP_PORTRANGE_LOW:
2578 inp->inp_flags &= ~(INP_HIGHPORT);
2579 inp->inp_flags |= INP_LOWPORT;
2580 break;
2581
2582 default:
2583 error = EINVAL;
2584 break;
2585 }
2586 break;
2587
2588#if IPSEC
39236c6e 2589 case IP_IPSEC_POLICY: {
1c79356b
A
2590 caddr_t req = NULL;
2591 size_t len = 0;
2592 int priv;
2593 struct mbuf *m;
2594 int optname;
39037602 2595
9bccf70c 2596 if ((error = soopt_getm(sopt, &m)) != 0) /* XXX */
1c79356b 2597 break;
9bccf70c 2598 if ((error = soopt_mcopyin(sopt, m)) != 0) /* XXX */
1c79356b 2599 break;
b0d623f7 2600 priv = (proc_suser(sopt->sopt_p) == 0);
1c79356b
A
2601 if (m) {
2602 req = mtod(m, caddr_t);
2603 len = m->m_len;
2604 }
2605 optname = sopt->sopt_name;
2606 error = ipsec4_set_policy(inp, optname, req, len, priv);
2607 m_freem(m);
2608 break;
2609 }
39236c6e 2610#endif /* IPSEC */
1c79356b 2611
2d21ac55 2612#if TRAFFIC_MGT
39236c6e
A
2613 case IP_TRAFFIC_MGT_BACKGROUND: {
2614 unsigned background = 0;
2615
2616 error = sooptcopyin(sopt, &background,
2617 sizeof (background), sizeof (background));
2618 if (error)
2d21ac55
A
2619 break;
2620
d1ecb069 2621 if (background) {
6d2010ae
A
2622 socket_set_traffic_mgt_flags_locked(so,
2623 TRAFFIC_MGT_SO_BACKGROUND);
d1ecb069 2624 } else {
6d2010ae
A
2625 socket_clear_traffic_mgt_flags_locked(so,
2626 TRAFFIC_MGT_SO_BACKGROUND);
d1ecb069 2627 }
2d21ac55
A
2628
2629 break;
2630 }
2631#endif /* TRAFFIC_MGT */
2632
c910b4d9
A
2633 /*
2634 * On a multihomed system, scoped routing can be used to
2635 * restrict the source interface used for sending packets.
2636 * The socket option IP_BOUND_IF binds a particular AF_INET
2637 * socket to an interface such that data sent on the socket
2638 * is restricted to that interface. This is unlike the
2639 * SO_DONTROUTE option where the routing table is bypassed;
2640 * therefore it allows for a greater flexibility and control
2641 * over the system behavior, and does not place any restriction
2642 * on the destination address type (e.g. unicast, multicast,
2643 * or broadcast if applicable) or whether or not the host is
2644 * directly reachable. Note that in the multicast transmit
6d2010ae
A
2645 * case, IP_MULTICAST_{IF,IFINDEX} takes precedence over
2646 * IP_BOUND_IF, since the former practically bypasses the
2647 * routing table; in this case, IP_BOUND_IF sets the default
2648 * interface used for sending multicast packets in the absence
2649 * of an explicit multicast transmit interface.
c910b4d9
A
2650 */
2651 case IP_BOUND_IF:
2652 /* This option is settable only for IPv4 */
2653 if (!(inp->inp_vflag & INP_IPV4)) {
2654 error = EINVAL;
2655 break;
2656 }
2657
2658 error = sooptcopyin(sopt, &optval, sizeof (optval),
2659 sizeof (optval));
2660
2661 if (error)
2662 break;
2663
39236c6e 2664 error = inp_bindif(inp, optval, NULL);
6d2010ae
A
2665 break;
2666
2667 case IP_NO_IFT_CELLULAR:
2668 /* This option is settable only for IPv4 */
2669 if (!(inp->inp_vflag & INP_IPV4)) {
2670 error = EINVAL;
2671 break;
2672 }
2673
2674 error = sooptcopyin(sopt, &optval, sizeof (optval),
2675 sizeof (optval));
2676
2677 if (error)
2678 break;
2679
39236c6e 2680 /* once set, it cannot be unset */
fe8ab488 2681 if (!optval && INP_NO_CELLULAR(inp)) {
39236c6e
A
2682 error = EINVAL;
2683 break;
2684 }
2685
2686 error = so_set_restrictions(so,
2687 SO_RESTRICT_DENY_CELLULAR);
6d2010ae
A
2688 break;
2689
2690 case IP_OUT_IF:
2691 /* This option is not settable */
2692 error = EINVAL;
c910b4d9
A
2693 break;
2694
1c79356b
A
2695 default:
2696 error = ENOPROTOOPT;
2697 break;
2698 }
2699 break;
2700
2701 case SOPT_GET:
2702 switch (sopt->sopt_name) {
2703 case IP_OPTIONS:
2704 case IP_RETOPTS:
39236c6e
A
2705 if (inp->inp_options) {
2706 error = sooptcopyout(sopt,
2707 mtod(inp->inp_options, char *),
2708 inp->inp_options->m_len);
2709 } else {
1c79356b 2710 sopt->sopt_valsize = 0;
39236c6e 2711 }
1c79356b
A
2712 break;
2713
2714 case IP_TOS:
2715 case IP_TTL:
2716 case IP_RECVOPTS:
2717 case IP_RECVRETOPTS:
2718 case IP_RECVDSTADDR:
2719 case IP_RECVIF:
55e303ae 2720 case IP_RECVTTL:
1c79356b 2721 case IP_PORTRANGE:
6d2010ae 2722 case IP_RECVPKTINFO:
813fb2f6 2723 case IP_RECVTOS:
1c79356b 2724 switch (sopt->sopt_name) {
1c79356b
A
2725 case IP_TOS:
2726 optval = inp->inp_ip_tos;
2727 break;
2728
2729 case IP_TTL:
2730 optval = inp->inp_ip_ttl;
2731 break;
2732
2733#define OPTBIT(bit) (inp->inp_flags & bit ? 1 : 0)
2734
2735 case IP_RECVOPTS:
2736 optval = OPTBIT(INP_RECVOPTS);
2737 break;
2738
2739 case IP_RECVRETOPTS:
2740 optval = OPTBIT(INP_RECVRETOPTS);
2741 break;
2742
2743 case IP_RECVDSTADDR:
2744 optval = OPTBIT(INP_RECVDSTADDR);
2745 break;
2746
2747 case IP_RECVIF:
2748 optval = OPTBIT(INP_RECVIF);
2749 break;
2750
55e303ae
A
2751 case IP_RECVTTL:
2752 optval = OPTBIT(INP_RECVTTL);
2753 break;
2754
1c79356b
A
2755 case IP_PORTRANGE:
2756 if (inp->inp_flags & INP_HIGHPORT)
2757 optval = IP_PORTRANGE_HIGH;
2758 else if (inp->inp_flags & INP_LOWPORT)
2759 optval = IP_PORTRANGE_LOW;
2760 else
2761 optval = 0;
2762 break;
2763
6d2010ae
A
2764 case IP_RECVPKTINFO:
2765 optval = OPTBIT(INP_PKTINFO);
2766 break;
813fb2f6
A
2767
2768 case IP_RECVTOS:
2769 optval = OPTBIT(INP_RECVTOS);
2770 break;
1c79356b 2771 }
39236c6e 2772 error = sooptcopyout(sopt, &optval, sizeof (optval));
1c79356b
A
2773 break;
2774
2775 case IP_MULTICAST_IF:
6d2010ae 2776 case IP_MULTICAST_IFINDEX:
1c79356b
A
2777 case IP_MULTICAST_VIF:
2778 case IP_MULTICAST_TTL:
2779 case IP_MULTICAST_LOOP:
6d2010ae
A
2780 case IP_MSFILTER:
2781 error = inp_getmoptions(inp, sopt);
1c79356b
A
2782 break;
2783
2784#if IPSEC
39236c6e 2785 case IP_IPSEC_POLICY: {
fe8ab488 2786 error = 0; /* This option is no longer supported */
1c79356b
A
2787 break;
2788 }
39236c6e 2789#endif /* IPSEC */
1c79356b 2790
2d21ac55 2791#if TRAFFIC_MGT
39236c6e 2792 case IP_TRAFFIC_MGT_BACKGROUND: {
39037602
A
2793 unsigned background = (so->so_flags1 &
2794 SOF1_TRAFFIC_MGT_SO_BACKGROUND) ? 1 : 0;
39236c6e
A
2795 return (sooptcopyout(sopt, &background,
2796 sizeof (background)));
2d21ac55
A
2797 }
2798#endif /* TRAFFIC_MGT */
2799
c910b4d9
A
2800 case IP_BOUND_IF:
2801 if (inp->inp_flags & INP_BOUND_IF)
316670eb 2802 optval = inp->inp_boundifp->if_index;
c910b4d9
A
2803 error = sooptcopyout(sopt, &optval, sizeof (optval));
2804 break;
2805
6d2010ae 2806 case IP_NO_IFT_CELLULAR:
fe8ab488 2807 optval = INP_NO_CELLULAR(inp) ? 1 : 0;
6d2010ae
A
2808 error = sooptcopyout(sopt, &optval, sizeof (optval));
2809 break;
2810
2811 case IP_OUT_IF:
316670eb
A
2812 optval = (inp->inp_last_outifp != NULL) ?
2813 inp->inp_last_outifp->if_index : 0;
6d2010ae
A
2814 error = sooptcopyout(sopt, &optval, sizeof (optval));
2815 break;
2816
1c79356b
A
2817 default:
2818 error = ENOPROTOOPT;
2819 break;
2820 }
2821 break;
2822 }
2823 return (error);
2824}
2825
2826/*
2827 * Set up IP options in pcb for insertion in output packets.
2828 * Store in mbuf with pointer in pcbopt, adding pseudo-option
2829 * with destination address if source routed.
2830 */
2831static int
39236c6e 2832ip_pcbopts(int optname, struct mbuf **pcbopt, struct mbuf *m)
1c79356b 2833{
39236c6e
A
2834#pragma unused(optname)
2835 int cnt, optlen;
2836 u_char *cp;
1c79356b
A
2837 u_char opt;
2838
2839 /* turn off any old options */
2840 if (*pcbopt)
39236c6e 2841 (void) m_free(*pcbopt);
1c79356b
A
2842 *pcbopt = 0;
2843 if (m == (struct mbuf *)0 || m->m_len == 0) {
2844 /*
2845 * Only turning off any previous options.
2846 */
2847 if (m)
39236c6e 2848 (void) m_free(m);
1c79356b
A
2849 return (0);
2850 }
2851
39236c6e 2852 if (m->m_len % sizeof (int32_t))
1c79356b 2853 goto bad;
39236c6e 2854
1c79356b
A
2855 /*
2856 * IP first-hop destination address will be stored before
2857 * actual options; move other options back
2858 * and clear it when none present.
2859 */
39236c6e 2860 if (m->m_data + m->m_len + sizeof (struct in_addr) >= &m->m_dat[MLEN])
1c79356b
A
2861 goto bad;
2862 cnt = m->m_len;
39236c6e
A
2863 m->m_len += sizeof (struct in_addr);
2864 cp = mtod(m, u_char *) + sizeof (struct in_addr);
1c79356b 2865 ovbcopy(mtod(m, caddr_t), (caddr_t)cp, (unsigned)cnt);
39236c6e 2866 bzero(mtod(m, caddr_t), sizeof (struct in_addr));
1c79356b
A
2867
2868 for (; cnt > 0; cnt -= optlen, cp += optlen) {
2869 opt = cp[IPOPT_OPTVAL];
2870 if (opt == IPOPT_EOL)
2871 break;
2872 if (opt == IPOPT_NOP)
2873 optlen = 1;
2874 else {
39236c6e 2875 if (cnt < IPOPT_OLEN + sizeof (*cp))
1c79356b
A
2876 goto bad;
2877 optlen = cp[IPOPT_OLEN];
39236c6e 2878 if (optlen < IPOPT_OLEN + sizeof (*cp) || optlen > cnt)
1c79356b
A
2879 goto bad;
2880 }
2881 switch (opt) {
2882
2883 default:
2884 break;
2885
2886 case IPOPT_LSRR:
2887 case IPOPT_SSRR:
2888 /*
2889 * user process specifies route as:
2890 * ->A->B->C->D
2891 * D must be our final destination (but we can't
2892 * check that since we may not have connected yet).
2893 * A is first hop destination, which doesn't appear in
2894 * actual IP option, but is stored before the options.
2895 */
39236c6e 2896 if (optlen < IPOPT_MINOFF - 1 + sizeof (struct in_addr))
1c79356b 2897 goto bad;
39236c6e
A
2898 m->m_len -= sizeof (struct in_addr);
2899 cnt -= sizeof (struct in_addr);
2900 optlen -= sizeof (struct in_addr);
1c79356b
A
2901 cp[IPOPT_OLEN] = optlen;
2902 /*
2903 * Move first hop before start of options.
2904 */
2905 bcopy((caddr_t)&cp[IPOPT_OFFSET+1], mtod(m, caddr_t),
39236c6e 2906 sizeof (struct in_addr));
1c79356b
A
2907 /*
2908 * Then copy rest of options back
2909 * to close up the deleted entry.
2910 */
2911 ovbcopy((caddr_t)(&cp[IPOPT_OFFSET+1] +
39236c6e 2912 sizeof (struct in_addr)),
1c79356b 2913 (caddr_t)&cp[IPOPT_OFFSET+1],
39236c6e 2914 (unsigned)cnt + sizeof (struct in_addr));
1c79356b
A
2915 break;
2916 }
2917 }
39236c6e 2918 if (m->m_len > MAX_IPOPTLEN + sizeof (struct in_addr))
1c79356b
A
2919 goto bad;
2920 *pcbopt = m;
2921 return (0);
2922
2923bad:
39236c6e 2924 (void) m_free(m);
1c79356b
A
2925 return (EINVAL);
2926}
2927
6d2010ae
A
2928void
2929ip_moptions_init(void)
9bccf70c 2930{
6d2010ae 2931 PE_parse_boot_argn("ifa_debug", &imo_debug, sizeof (imo_debug));
9bccf70c 2932
6d2010ae
A
2933 imo_size = (imo_debug == 0) ? sizeof (struct ip_moptions) :
2934 sizeof (struct ip_moptions_dbg);
2935
2936 imo_zone = zinit(imo_size, IMO_ZONE_MAX * imo_size, 0,
2937 IMO_ZONE_NAME);
2938 if (imo_zone == NULL) {
2939 panic("%s: failed allocating %s", __func__, IMO_ZONE_NAME);
2940 /* NOTREACHED */
9bccf70c 2941 }
6d2010ae 2942 zone_change(imo_zone, Z_EXPAND, TRUE);
9bccf70c
A
2943}
2944
6d2010ae
A
2945void
2946imo_addref(struct ip_moptions *imo, int locked)
1c79356b 2947{
6d2010ae
A
2948 if (!locked)
2949 IMO_LOCK(imo);
2950 else
2951 IMO_LOCK_ASSERT_HELD(imo);
1c79356b 2952
6d2010ae
A
2953 if (++imo->imo_refcnt == 0) {
2954 panic("%s: imo %p wraparound refcnt\n", __func__, imo);
2955 /* NOTREACHED */
2956 } else if (imo->imo_trace != NULL) {
2957 (*imo->imo_trace)(imo, TRUE);
1c79356b
A
2958 }
2959
6d2010ae
A
2960 if (!locked)
2961 IMO_UNLOCK(imo);
1c79356b
A
2962}
2963
6d2010ae
A
2964void
2965imo_remref(struct ip_moptions *imo)
55e303ae 2966{
55e303ae 2967 int i;
b0d623f7 2968
6d2010ae
A
2969 IMO_LOCK(imo);
2970 if (imo->imo_refcnt == 0) {
2971 panic("%s: imo %p negative refcnt", __func__, imo);
2972 /* NOTREACHED */
2973 } else if (imo->imo_trace != NULL) {
2974 (*imo->imo_trace)(imo, FALSE);
55e303ae 2975 }
b0d623f7 2976
6d2010ae
A
2977 --imo->imo_refcnt;
2978 if (imo->imo_refcnt > 0) {
2979 IMO_UNLOCK(imo);
2980 return;
55e303ae
A
2981 }
2982
55e303ae 2983 for (i = 0; i < imo->imo_num_memberships; ++i) {
6d2010ae 2984 struct in_mfilter *imf;
b0d623f7 2985
6d2010ae
A
2986 imf = imo->imo_mfilters ? &imo->imo_mfilters[i] : NULL;
2987 if (imf != NULL)
2988 imf_leave(imf);
b0d623f7 2989
6d2010ae 2990 (void) in_leavegroup(imo->imo_membership[i], imf);
55e303ae 2991
6d2010ae
A
2992 if (imf != NULL)
2993 imf_purge(imf);
55e303ae 2994
6d2010ae
A
2995 INM_REMREF(imo->imo_membership[i]);
2996 imo->imo_membership[i] = NULL;
55e303ae 2997 }
6d2010ae
A
2998 imo->imo_num_memberships = 0;
2999 if (imo->imo_mfilters != NULL) {
3000 FREE(imo->imo_mfilters, M_INMFILTER);
3001 imo->imo_mfilters = NULL;
55e303ae 3002 }
6d2010ae
A
3003 if (imo->imo_membership != NULL) {
3004 FREE(imo->imo_membership, M_IPMOPTS);
3005 imo->imo_membership = NULL;
55e303ae 3006 }
6d2010ae 3007 IMO_UNLOCK(imo);
1c79356b 3008
6d2010ae 3009 lck_mtx_destroy(&imo->imo_lock, ifa_mtx_grp);
1c79356b 3010
6d2010ae
A
3011 if (!(imo->imo_debug & IFD_ALLOC)) {
3012 panic("%s: imo %p cannot be freed", __func__, imo);
3013 /* NOTREACHED */
3014 }
3015 zfree(imo_zone, imo);
3016}
1c79356b 3017
6d2010ae
A
3018static void
3019imo_trace(struct ip_moptions *imo, int refhold)
3020{
3021 struct ip_moptions_dbg *imo_dbg = (struct ip_moptions_dbg *)imo;
3022 ctrace_t *tr;
3023 u_int32_t idx;
3024 u_int16_t *cnt;
3025
3026 if (!(imo->imo_debug & IFD_DEBUG)) {
3027 panic("%s: imo %p has no debug structure", __func__, imo);
3028 /* NOTREACHED */
3029 }
3030 if (refhold) {
3031 cnt = &imo_dbg->imo_refhold_cnt;
3032 tr = imo_dbg->imo_refhold;
3033 } else {
3034 cnt = &imo_dbg->imo_refrele_cnt;
3035 tr = imo_dbg->imo_refrele;
1c79356b 3036 }
6d2010ae
A
3037
3038 idx = atomic_add_16_ov(cnt, 1) % IMO_TRACE_HIST_SIZE;
3039 ctrace_record(&tr[idx]);
1c79356b
A
3040}
3041
6d2010ae
A
3042struct ip_moptions *
3043ip_allocmoptions(int how)
1c79356b 3044{
6d2010ae 3045 struct ip_moptions *imo;
1c79356b 3046
6d2010ae 3047 imo = (how == M_WAITOK) ? zalloc(imo_zone) : zalloc_noblock(imo_zone);
1c79356b 3048 if (imo != NULL) {
6d2010ae
A
3049 bzero(imo, imo_size);
3050 lck_mtx_init(&imo->imo_lock, ifa_mtx_grp, ifa_mtx_attr);
3051 imo->imo_debug |= IFD_ALLOC;
3052 if (imo_debug != 0) {
3053 imo->imo_debug |= IFD_DEBUG;
3054 imo->imo_trace = imo_trace;
3055 }
3056 IMO_ADDREF(imo);
1c79356b 3057 }
6d2010ae
A
3058
3059 return (imo);
1c79356b
A
3060}
3061
3062/*
3063 * Routine called from ip_output() to loop back a copy of an IP multicast
3064 * packet to the input queue of a specified interface. Note that this
3065 * calls the output routine of the loopback "driver", but with an interface
3066 * pointer that might NOT be a loopback interface -- evil, but easier than
3067 * replicating that code here.
3068 */
3069static void
39236c6e
A
3070ip_mloopback(struct ifnet *srcifp, struct ifnet *origifp, struct mbuf *m,
3071 struct sockaddr_in *dst, int hlen)
1c79356b 3072{
1c79356b 3073 struct mbuf *copym;
39236c6e 3074 struct ip *ip;
1c79356b 3075
39236c6e
A
3076 if (lo_ifp == NULL)
3077 return;
3078
3079 /*
3080 * Copy the packet header as it's needed for the checksum
3081 * Make sure to deep-copy IP header portion in case the data
3082 * is in an mbuf cluster, so that we can safely override the IP
3083 * header portion later.
3084 */
3085 copym = m_copym_mode(m, 0, M_COPYALL, M_DONTWAIT, M_COPYM_COPY_HDR);
3086 if (copym != NULL && ((copym->m_flags & M_EXT) || copym->m_len < hlen))
1c79356b 3087 copym = m_pullup(copym, hlen);
2d21ac55
A
3088
3089 if (copym == NULL)
3090 return;
3091
3092 /*
3093 * We don't bother to fragment if the IP length is greater
3094 * than the interface's MTU. Can this possibly matter?
3095 */
3096 ip = mtod(copym, struct ip *);
b0d623f7 3097#if BYTE_ORDER != BIG_ENDIAN
2d21ac55
A
3098 HTONS(ip->ip_len);
3099 HTONS(ip->ip_off);
b0d623f7 3100#endif
2d21ac55 3101 ip->ip_sum = 0;
39236c6e
A
3102 ip->ip_sum = ip_cksum_hdr_out(copym, hlen);
3103
2d21ac55 3104 /*
39236c6e
A
3105 * Mark checksum as valid unless receive checksum offload is
3106 * disabled; if so, compute checksum in software. If the
3107 * interface itself is lo0, this will be overridden by if_loop.
2d21ac55 3108 */
39236c6e 3109 if (hwcksum_rx) {
5ba3f43e 3110 copym->m_pkthdr.csum_flags &= ~(CSUM_PARTIAL|CSUM_ZERO_INVERT);
39236c6e
A
3111 copym->m_pkthdr.csum_flags |=
3112 CSUM_DATA_VALID | CSUM_PSEUDO_HDR;
3113 copym->m_pkthdr.csum_data = 0xffff;
3114 } else if (copym->m_pkthdr.csum_flags & CSUM_DELAY_DATA) {
b0d623f7 3115#if BYTE_ORDER != BIG_ENDIAN
39236c6e 3116 NTOHS(ip->ip_len);
b0d623f7 3117#endif
39236c6e 3118 in_delayed_cksum(copym);
b0d623f7 3119#if BYTE_ORDER != BIG_ENDIAN
39236c6e 3120 HTONS(ip->ip_len);
b0d623f7 3121#endif
39236c6e 3122 }
1c79356b 3123
2d21ac55 3124 /*
39236c6e
A
3125 * Stuff the 'real' ifp into the pkthdr, to be used in matching
3126 * in ip_input(); we need the loopback ifp/dl_tag passed as args
3127 * to make the loopback driver compliant with the data link
3128 * requirements.
2d21ac55 3129 */
39236c6e 3130 copym->m_pkthdr.rcvif = origifp;
2d21ac55
A
3131
3132 /*
39236c6e
A
3133 * Also record the source interface (which owns the source address).
3134 * This is basically a stripped down version of ifa_foraddr().
2d21ac55 3135 */
39236c6e
A
3136 if (srcifp == NULL) {
3137 struct in_ifaddr *ia;
3138
3139 lck_rw_lock_shared(in_ifaddr_rwlock);
3140 TAILQ_FOREACH(ia, INADDR_HASH(ip->ip_src.s_addr), ia_hash) {
3141 IFA_LOCK_SPIN(&ia->ia_ifa);
3142 if (IA_SIN(ia)->sin_addr.s_addr == ip->ip_src.s_addr) {
3143 srcifp = ia->ia_ifp;
3144 IFA_UNLOCK(&ia->ia_ifa);
3145 break;
3146 }
3147 IFA_UNLOCK(&ia->ia_ifa);
3148 }
3149 lck_rw_done(in_ifaddr_rwlock);
1c79356b 3150 }
39236c6e
A
3151 if (srcifp != NULL)
3152 ip_setsrcifaddr_info(copym, srcifp->if_index, NULL);
3153 ip_setdstifaddr_info(copym, origifp->if_index, NULL);
3154
3155 dlil_output(lo_ifp, PF_INET, copym, NULL, SA(dst), 0, NULL);
1c79356b 3156}
c910b4d9
A
3157
3158/*
3159 * Given a source IP address (and route, if available), determine the best
b0d623f7
A
3160 * interface to send the packet from. Checking for (and updating) the
3161 * ROF_SRCIF_SELECTED flag in the pcb-supplied route placeholder is done
3162 * without any locks based on the assumption that ip_output() is single-
3163 * threaded per-pcb, i.e. for any given pcb there can only be one thread
3164 * performing output at the IP layer.
6d2010ae
A
3165 *
3166 * This routine is analogous to in6_selectroute() for IPv6.
c910b4d9
A
3167 */
3168static struct ifaddr *
3169in_selectsrcif(struct ip *ip, struct route *ro, unsigned int ifscope)
3170{
3171 struct ifaddr *ifa = NULL;
b0d623f7
A
3172 struct in_addr src = ip->ip_src;
3173 struct in_addr dst = ip->ip_dst;
c910b4d9 3174 struct ifnet *rt_ifp;
0b4c1975 3175 char s_src[MAX_IPv4_STR_LEN], s_dst[MAX_IPv4_STR_LEN];
c910b4d9 3176
39236c6e
A
3177 VERIFY(src.s_addr != INADDR_ANY);
3178
c910b4d9 3179 if (ip_select_srcif_debug) {
b0d623f7
A
3180 (void) inet_ntop(AF_INET, &src.s_addr, s_src, sizeof (s_src));
3181 (void) inet_ntop(AF_INET, &dst.s_addr, s_dst, sizeof (s_dst));
c910b4d9
A
3182 }
3183
b0d623f7
A
3184 if (ro->ro_rt != NULL)
3185 RT_LOCK(ro->ro_rt);
c910b4d9 3186
c910b4d9
A
3187 rt_ifp = (ro->ro_rt != NULL) ? ro->ro_rt->rt_ifp : NULL;
3188
3189 /*
3190 * Given the source IP address, find a suitable source interface
3191 * to use for transmission; if the caller has specified a scope,
3192 * optimize the search by looking at the addresses only for that
3193 * interface. This is still suboptimal, however, as we need to
3194 * traverse the per-interface list.
3195 */
3196 if (ifscope != IFSCOPE_NONE || ro->ro_rt != NULL) {
3197 unsigned int scope = ifscope;
3198
3199 /*
3200 * If no scope is specified and the route is stale (pointing
3201 * to a defunct interface) use the current primary interface;
3202 * this happens when switching between interfaces configured
3203 * with the same IP address. Otherwise pick up the scope
3204 * information from the route; the ULP may have looked up a
3205 * correct route and we just need to verify it here and mark
3206 * it with the ROF_SRCIF_SELECTED flag below.
3207 */
3208 if (scope == IFSCOPE_NONE) {
3209 scope = rt_ifp->if_index;
6d2010ae 3210 if (scope != get_primary_ifscope(AF_INET) &&
39236c6e 3211 ROUTE_UNUSABLE(ro))
6d2010ae 3212 scope = get_primary_ifscope(AF_INET);
c910b4d9
A
3213 }
3214
b0d623f7 3215 ifa = (struct ifaddr *)ifa_foraddr_scoped(src.s_addr, scope);
c910b4d9 3216
0b4c1975
A
3217 if (ifa == NULL && ip->ip_p != IPPROTO_UDP &&
3218 ip->ip_p != IPPROTO_TCP && ipforwarding) {
3219 /*
3220 * If forwarding is enabled, and if the packet isn't
3221 * TCP or UDP, check if the source address belongs
3222 * to one of our own interfaces; if so, demote the
3223 * interface scope and do a route lookup right below.
3224 */
3225 ifa = (struct ifaddr *)ifa_foraddr(src.s_addr);
3226 if (ifa != NULL) {
6d2010ae 3227 IFA_REMREF(ifa);
0b4c1975
A
3228 ifa = NULL;
3229 ifscope = IFSCOPE_NONE;
3230 }
3231 }
3232
c910b4d9
A
3233 if (ip_select_srcif_debug && ifa != NULL) {
3234 if (ro->ro_rt != NULL) {
6d2010ae
A
3235 printf("%s->%s ifscope %d->%d ifa_if %s "
3236 "ro_if %s\n", s_src, s_dst, ifscope,
3237 scope, if_name(ifa->ifa_ifp),
3238 if_name(rt_ifp));
c910b4d9 3239 } else {
6d2010ae 3240 printf("%s->%s ifscope %d->%d ifa_if %s\n",
b0d623f7 3241 s_src, s_dst, ifscope, scope,
6d2010ae 3242 if_name(ifa->ifa_ifp));
c910b4d9
A
3243 }
3244 }
3245 }
3246
3247 /*
3248 * Slow path; search for an interface having the corresponding source
3249 * IP address if the scope was not specified by the caller, and:
3250 *
3251 * 1) There currently isn't any route, or,
3252 * 2) The interface used by the route does not own that source
3253 * IP address; in this case, the route will get blown away
3254 * and we'll do a more specific scoped search using the newly
3255 * found interface.
3256 */
3257 if (ifa == NULL && ifscope == IFSCOPE_NONE) {
b0d623f7 3258 ifa = (struct ifaddr *)ifa_foraddr(src.s_addr);
c910b4d9 3259
0b4c1975
A
3260 /*
3261 * If we have the IP address, but not the route, we don't
3262 * really know whether or not it belongs to the correct
3263 * interface (it could be shared across multiple interfaces.)
3264 * The only way to find out is to do a route lookup.
3265 */
3266 if (ifa != NULL && ro->ro_rt == NULL) {
3267 struct rtentry *rt;
3268 struct sockaddr_in sin;
3269 struct ifaddr *oifa = NULL;
3270
3271 bzero(&sin, sizeof (sin));
3272 sin.sin_family = AF_INET;
3273 sin.sin_len = sizeof (sin);
3274 sin.sin_addr = dst;
3275
3276 lck_mtx_lock(rnh_lock);
39236c6e 3277 if ((rt = rt_lookup(TRUE, SA(&sin), NULL,
0b4c1975
A
3278 rt_tables[AF_INET], IFSCOPE_NONE)) != NULL) {
3279 RT_LOCK(rt);
3280 /*
3281 * If the route uses a different interface,
3282 * use that one instead. The IP address of
3283 * the ifaddr that we pick up here is not
3284 * relevant.
3285 */
3286 if (ifa->ifa_ifp != rt->rt_ifp) {
3287 oifa = ifa;
3288 ifa = rt->rt_ifa;
6d2010ae 3289 IFA_ADDREF(ifa);
0b4c1975
A
3290 RT_UNLOCK(rt);
3291 } else {
3292 RT_UNLOCK(rt);
3293 }
3294 rtfree_locked(rt);
3295 }
3296 lck_mtx_unlock(rnh_lock);
3297
3298 if (oifa != NULL) {
3299 struct ifaddr *iifa;
3300
3301 /*
3302 * See if the interface pointed to by the
3303 * route is configured with the source IP
3304 * address of the packet.
3305 */
3306 iifa = (struct ifaddr *)ifa_foraddr_scoped(
3307 src.s_addr, ifa->ifa_ifp->if_index);
3308
3309 if (iifa != NULL) {
3310 /*
3311 * Found it; drop the original one
3312 * as well as the route interface
3313 * address, and use this instead.
3314 */
6d2010ae
A
3315 IFA_REMREF(oifa);
3316 IFA_REMREF(ifa);
0b4c1975
A
3317 ifa = iifa;
3318 } else if (!ipforwarding ||
3319 (rt->rt_flags & RTF_GATEWAY)) {
3320 /*
3321 * This interface doesn't have that
3322 * source IP address; drop the route
3323 * interface address and just use the
3324 * original one, and let the caller
3325 * do a scoped route lookup.
3326 */
6d2010ae 3327 IFA_REMREF(ifa);
0b4c1975
A
3328 ifa = oifa;
3329 } else {
3330 /*
3331 * Forwarding is enabled and the source
3332 * address belongs to one of our own
3333 * interfaces which isn't the outgoing
3334 * interface, and we have a route, and
3335 * the destination is on a network that
3336 * is directly attached (onlink); drop
3337 * the original one and use the route
3338 * interface address instead.
3339 */
6d2010ae 3340 IFA_REMREF(oifa);
0b4c1975
A
3341 }
3342 }
3343 } else if (ifa != NULL && ro->ro_rt != NULL &&
3344 !(ro->ro_rt->rt_flags & RTF_GATEWAY) &&
3345 ifa->ifa_ifp != ro->ro_rt->rt_ifp && ipforwarding) {
3346 /*
3347 * Forwarding is enabled and the source address belongs
3348 * to one of our own interfaces which isn't the same
3349 * as the interface used by the known route; drop the
3350 * original one and use the route interface address.
3351 */
6d2010ae 3352 IFA_REMREF(ifa);
0b4c1975 3353 ifa = ro->ro_rt->rt_ifa;
6d2010ae 3354 IFA_ADDREF(ifa);
0b4c1975
A
3355 }
3356
c910b4d9 3357 if (ip_select_srcif_debug && ifa != NULL) {
6d2010ae
A
3358 printf("%s->%s ifscope %d ifa_if %s\n",
3359 s_src, s_dst, ifscope, if_name(ifa->ifa_ifp));
c910b4d9
A
3360 }
3361 }
3362
b0d623f7
A
3363 if (ro->ro_rt != NULL)
3364 RT_LOCK_ASSERT_HELD(ro->ro_rt);
c910b4d9
A
3365 /*
3366 * If there is a non-loopback route with the wrong interface, or if
3367 * there is no interface configured with such an address, blow it
3368 * away. Except for local/loopback, we look for one with a matching
3369 * interface scope/index.
3370 */
3371 if (ro->ro_rt != NULL &&
3372 (ifa == NULL || (ifa->ifa_ifp != rt_ifp && rt_ifp != lo_ifp) ||
3373 !(ro->ro_rt->rt_flags & RTF_UP))) {
3374 if (ip_select_srcif_debug) {
3375 if (ifa != NULL) {
6d2010ae
A
3376 printf("%s->%s ifscope %d ro_if %s != "
3377 "ifa_if %s (cached route cleared)\n",
3378 s_src, s_dst, ifscope, if_name(rt_ifp),
3379 if_name(ifa->ifa_ifp));
c910b4d9 3380 } else {
6d2010ae 3381 printf("%s->%s ifscope %d ro_if %s "
c910b4d9 3382 "(no ifa_if found)\n",
6d2010ae 3383 s_src, s_dst, ifscope, if_name(rt_ifp));
c910b4d9
A
3384 }
3385 }
3386
b0d623f7 3387 RT_UNLOCK(ro->ro_rt);
39236c6e 3388 ROUTE_RELEASE(ro);
c910b4d9
A
3389
3390 /*
3391 * If the destination is IPv4 LLA and the route's interface
3392 * doesn't match the source interface, then the source IP
3393 * address is wrong; it most likely belongs to the primary
3394 * interface associated with the IPv4 LL subnet. Drop the
3395 * packet rather than letting it go out and return an error
3396 * to the ULP. This actually applies not only to IPv4 LL
3397 * but other shared subnets; for now we explicitly test only
3398 * for the former case and save the latter for future.
3399 */
b0d623f7
A
3400 if (IN_LINKLOCAL(ntohl(dst.s_addr)) &&
3401 !IN_LINKLOCAL(ntohl(src.s_addr)) && ifa != NULL) {
6d2010ae 3402 IFA_REMREF(ifa);
c910b4d9
A
3403 ifa = NULL;
3404 }
3405 }
3406
3407 if (ip_select_srcif_debug && ifa == NULL) {
3408 printf("%s->%s ifscope %d (neither ro_if/ifa_if found)\n",
b0d623f7 3409 s_src, s_dst, ifscope);
c910b4d9
A
3410 }
3411
3412 /*
3413 * If there is a route, mark it accordingly. If there isn't one,
3414 * we'll get here again during the next transmit (possibly with a
3415 * route) and the flag will get set at that point. For IPv4 LLA
3416 * destination, mark it only if the route has been fully resolved;
3417 * otherwise we want to come back here again when the route points
3418 * to the interface over which the ARP reply arrives on.
3419 */
b0d623f7 3420 if (ro->ro_rt != NULL && (!IN_LINKLOCAL(ntohl(dst.s_addr)) ||
c910b4d9
A
3421 (ro->ro_rt->rt_gateway->sa_family == AF_LINK &&
3422 SDL(ro->ro_rt->rt_gateway)->sdl_alen != 0))) {
39236c6e
A
3423 if (ifa != NULL)
3424 IFA_ADDREF(ifa); /* for route */
3425 if (ro->ro_srcia != NULL)
3426 IFA_REMREF(ro->ro_srcia);
3427 ro->ro_srcia = ifa;
c910b4d9 3428 ro->ro_flags |= ROF_SRCIF_SELECTED;
39236c6e 3429 RT_GENID_SYNC(ro->ro_rt);
c910b4d9
A
3430 }
3431
b0d623f7
A
3432 if (ro->ro_rt != NULL)
3433 RT_UNLOCK(ro->ro_rt);
3434
c910b4d9
A
3435 return (ifa);
3436}
39236c6e
A
3437
3438void
3439ip_output_checksum(struct ifnet *ifp, struct mbuf *m, int hlen, int ip_len,
3440 uint32_t *sw_csum)
3441{
3442 int tso = TSO_IPV4_OK(ifp, m);
3443 uint32_t hwcap = ifp->if_hwassist;
3444
3445 m->m_pkthdr.csum_flags |= CSUM_IP;
3446
3447 if (!hwcksum_tx) {
3448 /* do all in software; hardware checksum offload is disabled */
3449 *sw_csum = (CSUM_DELAY_DATA | CSUM_DELAY_IP) &
3450 m->m_pkthdr.csum_flags;
3451 } else {
3452 /* do in software what the hardware cannot */
3453 *sw_csum = m->m_pkthdr.csum_flags &
3454 ~IF_HWASSIST_CSUM_FLAGS(hwcap);
3455 }
3456
3457 if (hlen != sizeof (struct ip)) {
3458 *sw_csum |= ((CSUM_DELAY_DATA | CSUM_DELAY_IP) &
3459 m->m_pkthdr.csum_flags);
3460 } else if (!(*sw_csum & CSUM_DELAY_DATA) && (hwcap & CSUM_PARTIAL)) {
3461 /*
3462 * Partial checksum offload, if non-IP fragment, and TCP only
3463 * (no UDP support, as the hardware may not be able to convert
5ba3f43e
A
3464 * +0 to -0 (0xffff) per RFC1122 4.1.3.4. unless the interface
3465 * supports "invert zero" capability.)
39236c6e
A
3466 */
3467 if (hwcksum_tx && !tso &&
5ba3f43e
A
3468 ((m->m_pkthdr.csum_flags & CSUM_TCP) ||
3469 ((hwcap & CSUM_ZERO_INVERT) &&
3470 (m->m_pkthdr.csum_flags & CSUM_ZERO_INVERT))) &&
39236c6e
A
3471 ip_len <= ifp->if_mtu) {
3472 uint16_t start = sizeof (struct ip);
3473 uint16_t ulpoff = m->m_pkthdr.csum_data & 0xffff;
3474 m->m_pkthdr.csum_flags |=
3475 (CSUM_DATA_VALID | CSUM_PARTIAL);
3476 m->m_pkthdr.csum_tx_stuff = (ulpoff + start);
3477 m->m_pkthdr.csum_tx_start = start;
3478 /* do IP hdr chksum in software */
3479 *sw_csum = CSUM_DELAY_IP;
3480 } else {
3481 *sw_csum |= (CSUM_DELAY_DATA & m->m_pkthdr.csum_flags);
3482 }
3483 }
3484
3485 if (*sw_csum & CSUM_DELAY_DATA) {
3486 in_delayed_cksum(m);
3487 *sw_csum &= ~CSUM_DELAY_DATA;
3488 }
3489
3490 if (hwcksum_tx) {
3491 /*
3492 * Drop off bits that aren't supported by hardware;
3493 * also make sure to preserve non-checksum related bits.
3494 */
3495 m->m_pkthdr.csum_flags =
3496 ((m->m_pkthdr.csum_flags &
3497 (IF_HWASSIST_CSUM_FLAGS(hwcap) | CSUM_DATA_VALID)) |
3498 (m->m_pkthdr.csum_flags & ~IF_HWASSIST_CSUM_MASK));
3499 } else {
3500 /* drop all bits; hardware checksum offload is disabled */
3501 m->m_pkthdr.csum_flags = 0;
3502 }
3503}
3504
3505/*
3506 * GRE protocol output for PPP/PPTP
3507 */
3508int
3509ip_gre_output(struct mbuf *m)
3510{
3511 struct route ro;
3512 int error;
3513
3514 bzero(&ro, sizeof (ro));
3515
3516 error = ip_output(m, NULL, &ro, 0, NULL, NULL);
3517
3518 ROUTE_RELEASE(&ro);
3519
3520 return (error);
3521}
3e170ce0
A
3522
3523static int
3524sysctl_reset_ip_output_stats SYSCTL_HANDLER_ARGS
3525{
3526#pragma unused(arg1, arg2)
3527 int error, i;
3528
3529 i = ip_output_measure;
3530 error = sysctl_handle_int(oidp, &i, 0, req);
3531 if (error || req->newptr == USER_ADDR_NULL)
3532 goto done;
3533 /* impose bounds */
3534 if (i < 0 || i > 1) {
3535 error = EINVAL;
3536 goto done;
3537 }
3538 if (ip_output_measure != i && i == 1) {
3539 net_perf_initialize(&net_perf, ip_output_measure_bins);
3540 }
3541 ip_output_measure = i;
3542done:
3543 return (error);
3544}
3545
3546static int
3547sysctl_ip_output_measure_bins SYSCTL_HANDLER_ARGS
3548{
3549#pragma unused(arg1, arg2)
3550 int error;
3551 uint64_t i;
3552
3553 i = ip_output_measure_bins;
3554 error = sysctl_handle_quad(oidp, &i, 0, req);
3555 if (error || req->newptr == USER_ADDR_NULL)
3556 goto done;
3557 /* validate data */
3558 if (!net_perf_validate_bins(i)) {
3559 error = EINVAL;
3560 goto done;
3561 }
3562 ip_output_measure_bins = i;
3563done:
3564 return (error);
3565}
3566
3567static int
3568sysctl_ip_output_getperf SYSCTL_HANDLER_ARGS
3569{
3570#pragma unused(oidp, arg1, arg2)
3571 if (req->oldptr == USER_ADDR_NULL)
3572 req->oldlen = (size_t)sizeof (struct ipstat);
3573
3574 return (SYSCTL_OUT(req, &net_perf, MIN(sizeof (net_perf), req->oldlen)));
3575}