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