]> git.saurik.com Git - apple/xnu.git/blame - bsd/netinet6/ip6_output.c
xnu-2782.40.9.tar.gz
[apple/xnu.git] / bsd / netinet6 / ip6_output.c
CommitLineData
b0d623f7 1/*
fe8ab488 2 * Copyright (c) 2000-2014 Apple Inc. All rights reserved.
b0d623f7
A
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
39236c6e 5 *
b0d623f7
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 *
b0d623f7
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 *
b0d623f7
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
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
39236c6e 25 *
b0d623f7
A
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
1c79356b
A
29/*
30 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
31 * All rights reserved.
32 *
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
35 * are met:
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. Neither the name of the project nor the names of its contributors
42 * may be used to endorse or promote products derived from this software
43 * without specific prior written permission.
44 *
45 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
46 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
47 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
48 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
49 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
50 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
51 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
53 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
54 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55 * SUCH DAMAGE.
56 */
57
58/*
59 * Copyright (c) 1982, 1986, 1988, 1990, 1993
60 * The Regents of the University of California. All rights reserved.
61 *
62 * Redistribution and use in source and binary forms, with or without
63 * modification, are permitted provided that the following conditions
64 * are met:
65 * 1. Redistributions of source code must retain the above copyright
66 * notice, this list of conditions and the following disclaimer.
67 * 2. Redistributions in binary form must reproduce the above copyright
68 * notice, this list of conditions and the following disclaimer in the
69 * documentation and/or other materials provided with the distribution.
70 * 3. All advertising materials mentioning features or use of this software
71 * must display the following acknowledgement:
72 * This product includes software developed by the University of
73 * California, Berkeley and its contributors.
74 * 4. Neither the name of the University nor the names of its contributors
75 * may be used to endorse or promote products derived from this software
76 * without specific prior written permission.
77 *
78 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
79 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
80 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
81 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
82 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
83 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
84 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
85 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
86 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
87 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
88 * SUCH DAMAGE.
89 *
90 * @(#)ip_output.c 8.3 (Berkeley) 1/21/94
91 */
2d21ac55
A
92/*
93 * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
94 * support for mandatory and extensible security protections. This notice
95 * is included in support of clause 2.2 (b) of the Apple Public License,
96 * Version 2.0.
97 */
1c79356b
A
98
99#include <sys/param.h>
100#include <sys/malloc.h>
101#include <sys/mbuf.h>
102#include <sys/errno.h>
103#include <sys/protosw.h>
104#include <sys/socket.h>
105#include <sys/socketvar.h>
106#include <sys/systm.h>
1c79356b 107#include <sys/kernel.h>
1c79356b 108#include <sys/proc.h>
91447636 109#include <sys/kauth.h>
6d2010ae
A
110#include <sys/mcache.h>
111#include <sys/sysctl.h>
112#include <kern/zalloc.h>
39236c6e 113#include <libkern/OSByteOrder.h>
6d2010ae
A
114
115#include <pexpert/pexpert.h>
39236c6e 116#include <mach/sdt.h>
1c79356b
A
117
118#include <net/if.h>
119#include <net/route.h>
2d21ac55 120#include <net/dlil.h>
39236c6e 121#include <net/net_osdep.h>
1c79356b
A
122
123#include <netinet/in.h>
124#include <netinet/in_var.h>
55e303ae 125#include <netinet/ip_var.h>
9bccf70c 126#include <netinet6/in6_var.h>
1c79356b 127#include <netinet/ip6.h>
39236c6e
A
128#include <netinet/kpi_ipfilter_var.h>
129
6d2010ae 130#include <netinet6/ip6protosw.h>
1c79356b
A
131#include <netinet/icmp6.h>
132#include <netinet6/ip6_var.h>
1c79356b 133#include <netinet/in_pcb.h>
1c79356b 134#include <netinet6/nd6.h>
6d2010ae 135#include <netinet6/scope6_var.h>
1c79356b
A
136#if IPSEC
137#include <netinet6/ipsec.h>
9bccf70c 138#include <netinet6/ipsec6.h>
1c79356b 139#include <netkey/key.h>
9bccf70c 140extern int ipsec_bypass;
1c79356b 141#endif /* IPSEC */
2d21ac55 142
fe8ab488
A
143#if NECP
144#include <net/necp.h>
145#endif /* NECP */
146
2d21ac55
A
147#if CONFIG_MACF_NET
148#include <security/mac.h>
39236c6e 149#endif /* CONFIG_MACF_NET */
1c79356b 150
316670eb 151#if DUMMYNET
39236c6e 152#include <netinet6/ip6_fw.h>
316670eb
A
153#include <netinet/ip_fw.h>
154#include <netinet/ip_dummynet.h>
155#endif /* DUMMYNET */
156
b0d623f7
A
157#if PF
158#include <net/pfvar.h>
159#endif /* PF */
160
91447636 161static int ip6_copyexthdr(struct mbuf **, caddr_t, int);
39236c6e 162static void ip6_out_cksum_stats(int, u_int32_t);
91447636 163static int ip6_insert_jumboopt(struct ip6_exthdrs *, u_int32_t);
39236c6e
A
164static int ip6_insertfraghdr(struct mbuf *, struct mbuf *, int,
165 struct ip6_frag **);
166static int ip6_getpmtu(struct route_in6 *, struct route_in6 *,
167 struct ifnet *, struct in6_addr *, u_int32_t *, boolean_t *);
168static int ip6_pcbopts(struct ip6_pktopts **, struct mbuf *, struct socket *,
169 struct sockopt *sopt);
170static int ip6_pcbopt(int, u_char *, int, struct ip6_pktopts **, int);
171static int ip6_getpcbopt(struct ip6_pktopts *, int, struct sockopt *);
172static int copypktopts(struct ip6_pktopts *, struct ip6_pktopts *, int);
173static void im6o_trace(struct ip6_moptions *, int);
174static int ip6_setpktopt(int, u_char *, int, struct ip6_pktopts *, int,
175 int, int);
91447636 176static int ip6_splithdr(struct mbuf *, struct ip6_exthdrs *);
39236c6e
A
177static void ip6_output_checksum(struct ifnet *, uint32_t, struct mbuf *,
178 int, uint32_t, uint32_t);
6d2010ae
A
179
180#define IM6O_TRACE_HIST_SIZE 32 /* size of trace history */
181
182/* For gdb */
183__private_extern__ unsigned int im6o_trace_hist_size = IM6O_TRACE_HIST_SIZE;
184
185struct ip6_moptions_dbg {
186 struct ip6_moptions im6o; /* ip6_moptions */
187 u_int16_t im6o_refhold_cnt; /* # of IM6O_ADDREF */
188 u_int16_t im6o_refrele_cnt; /* # of IM6O_REMREF */
189 /*
190 * Alloc and free callers.
191 */
192 ctrace_t im6o_alloc;
193 ctrace_t im6o_free;
194 /*
195 * Circular lists of IM6O_ADDREF and IM6O_REMREF callers.
196 */
197 ctrace_t im6o_refhold[IM6O_TRACE_HIST_SIZE];
198 ctrace_t im6o_refrele[IM6O_TRACE_HIST_SIZE];
199};
200
201#if DEBUG
202static unsigned int im6o_debug = 1; /* debugging (enabled) */
203#else
204static unsigned int im6o_debug; /* debugging (disabled) */
205#endif /* !DEBUG */
206
207static unsigned int im6o_size; /* size of zone element */
208static struct zone *im6o_zone; /* zone for ip6_moptions */
209
210#define IM6O_ZONE_MAX 64 /* maximum elements in zone */
211#define IM6O_ZONE_NAME "ip6_moptions" /* zone name */
1c79356b 212
316670eb
A
213SYSCTL_DECL(_net_inet6_ip6);
214
39236c6e
A
215static int ip6_maxchainsent = 0;
216SYSCTL_INT(_net_inet6_ip6, OID_AUTO, maxchainsent,
217 CTLFLAG_RW | CTLFLAG_LOCKED, &ip6_maxchainsent, 0,
218 "use dlil_output_list");
316670eb
A
219
220/*
221 * XXX we don't handle mbuf chains yet in nd6_output() so ip6_output_list() only
222 * walks through the packet chain and sends each mbuf separately.
223 */
224int
39236c6e
A
225ip6_output_list(struct mbuf *m0, int packetlist, struct ip6_pktopts *opt,
226 struct route_in6 *ro, int flags, struct ip6_moptions *im6o,
227 struct ifnet **ifpp, struct ip6_out_args *ip6oa)
316670eb
A
228{
229#pragma unused(packetlist)
230 struct mbuf *m = m0, *nextpkt;
231 int error = 0;
232
39236c6e 233 while (m != NULL) {
316670eb
A
234 /*
235 * Break the chain before calling ip6_output() and free the
236 * mbufs if there was an error.
237 */
238 nextpkt = m->m_nextpkt;
239 m->m_nextpkt = NULL;
39236c6e
A
240 error = ip6_output(m, opt, ro, flags, im6o, ifpp, ip6oa);
241 if (error != 0) {
242 if (nextpkt != NULL)
316670eb
A
243 m_freem_list(nextpkt);
244 return (error);
245 }
246 m = nextpkt;
247 }
248
249 return (error);
250}
55e303ae 251
1c79356b
A
252/*
253 * IP6 output. The packet in mbuf chain m contains a skeletal IP6
254 * header (with pri, len, nxt, hlim, src, dst).
255 * This function may modify ver and hlim only.
256 * The mbuf chain containing the packet will be freed.
257 * The mbuf opt, if present, will not be freed.
9bccf70c 258 *
39236c6e
A
259 * If ro is non-NULL and has valid ro->ro_rt, route lookup would be
260 * skipped and ro->ro_rt would be used. Otherwise the result of route
261 * lookup is stored in ro->ro_rt.
262 *
b0d623f7
A
263 * type of "mtu": rt_rmx.rmx_mtu is u_int32_t, ifnet.ifr_mtu is int, and
264 * nd_ifinfo.linkmtu is u_int32_t. so we use u_int32_t to hold largest one,
9bccf70c 265 * which is rt_rmx.rmx_mtu.
1c79356b
A
266 */
267int
39236c6e
A
268ip6_output(struct mbuf *m0, struct ip6_pktopts *opt, struct route_in6 *ro,
269 int flags, struct ip6_moptions *im6o, struct ifnet **ifpp,
270 struct ip6_out_args *ip6oa)
1c79356b 271{
39236c6e
A
272 struct ip6_hdr *ip6;
273 u_char *nexthdrp;
274 struct ifnet *ifp = NULL, *origifp = NULL; /* refcnt'd */
275 struct mbuf *m, *mprev;
276 int hlen, tlen, len, off, nxt0;
277 struct route_in6 *ro_pmtu = NULL;
6d2010ae
A
278 struct rtentry *rt = NULL;
279 struct sockaddr_in6 *dst, src_sa, dst_sa;
1c79356b 280 int error = 0;
39236c6e 281 struct in6_ifaddr *ia = NULL, *src_ia = NULL;
b0d623f7 282 u_int32_t mtu;
39236c6e 283 boolean_t alwaysfrag = FALSE;
1c79356b 284 u_int32_t optlen = 0, plen = 0, unfragpartlen = 0;
39236c6e
A
285 struct ip6_rthdr *rh;
286 struct in6_addr finaldst;
91447636 287 ipfilter_t inject_filter_ref;
39236c6e 288 struct ipf_pktopts *ippo = NULL;
316670eb 289 struct flowadv *adv = NULL;
316670eb
A
290#if DUMMYNET
291 struct m_tag *tag;
39236c6e 292 struct ip6_out_args saved_ip6oa;
316670eb 293 struct sockaddr_in6 dst_buf;
316670eb 294#endif /* DUMMYNET */
1c79356b 295#if IPSEC
9bccf70c 296 struct socket *so = NULL;
1c79356b 297 struct secpolicy *sp = NULL;
ebb1b9f4 298 struct route_in6 *ipsec_saved_route = NULL;
39236c6e 299 boolean_t needipsectun = FALSE;
1c79356b 300#endif /* IPSEC */
fe8ab488
A
301#if NECP
302 necp_kernel_policy_result necp_result = 0;
303 necp_kernel_policy_result_parameter necp_result_parameter;
304 necp_kernel_policy_id necp_matched_policy_id = 0;
305#endif /* NECP */
39236c6e
A
306 struct {
307 struct ipf_pktopts ipf_pktopts;
308 struct ip6_exthdrs exthdrs;
309 struct route_in6 ip6route;
310#if IPSEC
311 struct ipsec_output_state ipsec_state;
312#endif /* IPSEC */
fe8ab488
A
313#if NECP
314 struct route_in6 necp_route;
315#endif /* NECP */
39236c6e
A
316#if DUMMYNET
317 struct route_in6 saved_route;
318 struct route_in6 saved_ro_pmtu;
319 struct ip_fw_args args;
320#endif /* DUMMYNET */
321 } ip6obz;
322#define ipf_pktopts ip6obz.ipf_pktopts
323#define exthdrs ip6obz.exthdrs
324#define ip6route ip6obz.ip6route
325#define ipsec_state ip6obz.ipsec_state
fe8ab488 326#define necp_route ip6obz.necp_route
39236c6e
A
327#define saved_route ip6obz.saved_route
328#define saved_ro_pmtu ip6obz.saved_ro_pmtu
329#define args ip6obz.args
330 union {
331 struct {
332 boolean_t select_srcif : 1;
333 boolean_t hdrsplit : 1;
334 boolean_t dontfrag : 1;
335#if IPSEC
336 boolean_t needipsec : 1;
337 boolean_t noipsec : 1;
338#endif /* IPSEC */
339 };
340 uint32_t raw;
341 } ip6obf = { .raw = 0 };
1c79356b 342
39236c6e 343 VERIFY(m0->m_flags & M_PKTHDR);
6d2010ae 344
39236c6e
A
345 /* zero out {saved_route, saved_ro_pmtu, ip6route, exthdrs, args} */
346 bzero(&ip6obz, sizeof (ip6obz));
316670eb 347
316670eb 348#if DUMMYNET
39236c6e
A
349 if (SLIST_EMPTY(&m0->m_pkthdr.tags))
350 goto tags_done;
351
352 /* Grab info from mtags prepended to the chain */
316670eb
A
353 if ((tag = m_tag_locate(m0, KERNEL_MODULE_TAG_ID,
354 KERNEL_TAG_TYPE_DUMMYNET, NULL)) != NULL) {
355 struct dn_pkt_tag *dn_tag;
356
357 dn_tag = (struct dn_pkt_tag *)(tag+1);
358 args.fwa_pf_rule = dn_tag->dn_pf_rule;
359
39236c6e 360 bcopy(&dn_tag->dn_dst6, &dst_buf, sizeof (dst_buf));
316670eb
A
361 dst = &dst_buf;
362 ifp = dn_tag->dn_ifp;
39236c6e 363 if (ifp != NULL)
316670eb
A
364 ifnet_reference(ifp);
365 flags = dn_tag->dn_flags;
39236c6e
A
366 if (dn_tag->dn_flags & IPV6_OUTARGS) {
367 saved_ip6oa = dn_tag->dn_ip6oa;
368 ip6oa = &saved_ip6oa;
369 }
316670eb
A
370
371 saved_route = dn_tag->dn_ro6;
372 ro = &saved_route;
373 saved_ro_pmtu = dn_tag->dn_ro6_pmtu;
374 ro_pmtu = &saved_ro_pmtu;
375 origifp = dn_tag->dn_origifp;
39236c6e 376 if (origifp != NULL)
316670eb
A
377 ifnet_reference(origifp);
378 mtu = dn_tag->dn_mtu;
39236c6e 379 alwaysfrag = (dn_tag->dn_alwaysfrag != 0);
316670eb
A
380 unfragpartlen = dn_tag->dn_unfragpartlen;
381
39236c6e 382 bcopy(&dn_tag->dn_exthdrs, &exthdrs, sizeof (exthdrs));
316670eb
A
383
384 m_tag_delete(m0, tag);
385 }
39236c6e
A
386
387tags_done:
316670eb
A
388#endif /* DUMMYNET */
389
39236c6e
A
390 m = m0;
391 m->m_pkthdr.pkt_flags &= ~(PKTF_LOOP|PKTF_IFAINFO);
392
393#if IPSEC
39236c6e
A
394 if (ipsec_bypass == 0) {
395 so = ipsec_getsocket(m);
fe8ab488
A
396 if (so != NULL) {
397 (void) ipsec_setsocket(m, NULL);
398 }
39236c6e
A
399 /* If packet is bound to an interface, check bound policies */
400 if ((flags & IPV6_OUTARGS) &&
fe8ab488
A
401 (ip6oa->ip6oa_flags & IPOAF_BOUND_IF) &&
402 ip6oa->ip6oa_boundif != IFSCOPE_NONE) {
39236c6e
A
403 /* ip6obf.noipsec is a bitfield, use temp integer */
404 int noipsec = 0;
405
406 if (ipsec6_getpolicybyinterface(m, IPSEC_DIR_OUTBOUND,
fe8ab488 407 flags, ip6oa, &noipsec, &sp) != 0)
39236c6e
A
408 goto bad;
409
410 ip6obf.noipsec = (noipsec != 0);
411 }
412 }
413#endif /* IPSEC */
fe8ab488 414
39236c6e
A
415 ip6 = mtod(m, struct ip6_hdr *);
416 nxt0 = ip6->ip6_nxt;
d41d1dae 417 finaldst = ip6->ip6_dst;
39236c6e
A
418 inject_filter_ref = ipf_get_inject_filter(m);
419 ippo = &ipf_pktopts;
91447636 420
6d2010ae 421 if (ip6_doscopedroute && (flags & IPV6_OUTARGS)) {
39236c6e
A
422 /*
423 * In the forwarding case, only the ifscope value is used,
424 * as source interface selection doesn't take place.
425 */
426 if ((ip6obf.select_srcif = (!(flags & (IPV6_FORWARDING |
316670eb 427 IPV6_UNSPECSRC | IPV6_FLAG_NOSRCIFSEL)) &&
39236c6e 428 (ip6oa->ip6oa_flags & IP6OAF_SELECT_SRCIF))))
316670eb
A
429 ipf_pktopts.ippo_flags |= IPPOF_SELECT_SRCIF;
430
39236c6e
A
431 if ((ip6oa->ip6oa_flags & IP6OAF_BOUND_IF) &&
432 ip6oa->ip6oa_boundif != IFSCOPE_NONE) {
316670eb 433 ipf_pktopts.ippo_flags |= (IPPOF_BOUND_IF |
39236c6e 434 (ip6oa->ip6oa_boundif << IPPOF_SHIFT_IFSCOPE));
316670eb
A
435 }
436
39236c6e 437 if (ip6oa->ip6oa_flags & IP6OAF_BOUND_SRCADDR)
316670eb 438 ipf_pktopts.ippo_flags |= IPPOF_BOUND_SRCADDR;
6d2010ae 439 } else {
39236c6e
A
440 ip6obf.select_srcif = FALSE;
441 if (flags & IPV6_OUTARGS) {
442 ip6oa->ip6oa_boundif = IFSCOPE_NONE;
443 ip6oa->ip6oa_flags &= ~(IP6OAF_SELECT_SRCIF |
444 IP6OAF_BOUND_IF | IP6OAF_BOUND_SRCADDR);
445 }
6d2010ae
A
446 }
447
39236c6e 448 if (flags & IPV6_OUTARGS) {
fe8ab488
A
449 if (ip6oa->ip6oa_flags & IP6OAF_NO_CELLULAR)
450 ipf_pktopts.ippo_flags |= IPPOF_NO_IFT_CELLULAR;
451 if (ip6oa->ip6oa_flags & IP6OAF_NO_EXPENSIVE)
452 ipf_pktopts.ippo_flags |= IPPOF_NO_IFF_EXPENSIVE;
39236c6e
A
453 adv = &ip6oa->ip6oa_flowadv;
454 adv->code = FADV_SUCCESS;
455 ip6oa->ip6oa_retflags = 0;
456 }
457
316670eb
A
458#if DUMMYNET
459 if (args.fwa_pf_rule) {
460 ip6 = mtod(m, struct ip6_hdr *);
39236c6e 461 VERIFY(ro != NULL); /* ro == saved_route */
316670eb 462 goto check_with_pf;
6d2010ae 463 }
316670eb 464#endif /* DUMMYNET */
6d2010ae 465
39236c6e
A
466#define MAKE_EXTHDR(hp, mp) do { \
467 if (hp != NULL) { \
1c79356b 468 struct ip6_ext *eh = (struct ip6_ext *)(hp); \
39236c6e
A
469 error = ip6_copyexthdr((mp), (caddr_t)(hp), \
470 ((eh)->ip6e_len + 1) << 3); \
1c79356b
A
471 if (error) \
472 goto freehdrs; \
473 } \
39236c6e 474} while (0)
316670eb 475
39236c6e 476 if (opt != NULL) {
1c79356b
A
477 /* Hop-by-Hop options header */
478 MAKE_EXTHDR(opt->ip6po_hbh, &exthdrs.ip6e_hbh);
9bccf70c 479 /* Destination options header(1st part) */
6d2010ae
A
480 if (opt->ip6po_rthdr) {
481 /*
482 * Destination options header(1st part)
483 * This only makes sense with a routing header.
484 * See Section 9.2 of RFC 3542.
485 * Disabling this part just for MIP6 convenience is
486 * a bad idea. We need to think carefully about a
487 * way to make the advanced API coexist with MIP6
488 * options, which might automatically be inserted in
489 * the kernel.
490 */
491 MAKE_EXTHDR(opt->ip6po_dest1, &exthdrs.ip6e_dest1);
492 }
1c79356b
A
493 /* Routing header */
494 MAKE_EXTHDR(opt->ip6po_rthdr, &exthdrs.ip6e_rthdr);
495 /* Destination options header(2nd part) */
496 MAKE_EXTHDR(opt->ip6po_dest2, &exthdrs.ip6e_dest2);
497 }
498
39236c6e
A
499#undef MAKE_EXTHDR
500
fe8ab488
A
501#if NECP
502 necp_matched_policy_id = necp_ip6_output_find_policy_match (m, flags, (flags & IPV6_OUTARGS) ? ip6oa : NULL,
503 &necp_result, &necp_result_parameter);
504 if (necp_matched_policy_id) {
505 necp_mark_packet_from_ip(m, necp_matched_policy_id);
506 switch (necp_result) {
507 case NECP_KERNEL_POLICY_RESULT_PASS:
508 goto skip_ipsec;
509 case NECP_KERNEL_POLICY_RESULT_DROP:
510 case NECP_KERNEL_POLICY_RESULT_SOCKET_DIVERT:
511 /* Flow divert packets should be blocked at the IP layer */
512 error = EHOSTUNREACH;
513 goto bad;
514 case NECP_KERNEL_POLICY_RESULT_IP_TUNNEL: {
515 /* Verify that the packet is being routed to the tunnel */
516 struct ifnet *policy_ifp = necp_get_ifnet_from_result_parameter(&necp_result_parameter);
517 if (policy_ifp == ifp) {
518 goto skip_ipsec;
519 } else {
520 if (necp_packet_can_rebind_to_ifnet(m, policy_ifp, (struct route *)&necp_route, AF_INET6)) {
521 /* Set scoped index to the tunnel interface, since it is compatible with the packet */
522 /* This will only work for callers who pass IPV6_OUTARGS, but that covers all of the
523 clients we care about today */
524 if (flags & IPV6_OUTARGS) {
525 ip6oa->ip6oa_boundif = policy_ifp->if_index;
526 ip6oa->ip6oa_flags |= IP6OAF_BOUND_IF;
527 }
528 if (opt != NULL && opt->ip6po_pktinfo != NULL) {
529 opt->ip6po_pktinfo->ipi6_ifindex = policy_ifp->if_index;
530 }
531 ro = &necp_route;
532 goto skip_ipsec;
533 } else {
534 error = ENETUNREACH;
535 goto bad;
536 }
537 }
538 break;
539 }
540 default:
541 break;
542 }
543 }
544#endif /* NECP */
545
1c79356b 546#if IPSEC
39236c6e 547 if (ipsec_bypass != 0 || ip6obf.noipsec)
9bccf70c 548 goto skip_ipsec;
316670eb 549
1c79356b 550 if (sp == NULL) {
39236c6e 551 /* get a security policy for this packet */
fe8ab488 552 if (so != NULL) {
39236c6e 553 sp = ipsec6_getpolicybysock(m, IPSEC_DIR_OUTBOUND,
fe8ab488
A
554 so, &error);
555 } else {
556 sp = ipsec6_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND,
557 0, &error);
39236c6e
A
558 }
559 if (sp == NULL) {
560 IPSEC_STAT_INCREMENT(ipsec6stat.out_inval);
561 goto freehdrs;
562 }
1c79356b
A
563 }
564
565 error = 0;
566
567 /* check policy */
568 switch (sp->policy) {
569 case IPSEC_POLICY_DISCARD:
2d21ac55 570 case IPSEC_POLICY_GENERATE:
1c79356b
A
571 /*
572 * This packet is just discarded.
573 */
2d21ac55 574 IPSEC_STAT_INCREMENT(ipsec6stat.out_polvio);
9bccf70c 575 goto freehdrs;
1c79356b
A
576
577 case IPSEC_POLICY_BYPASS:
578 case IPSEC_POLICY_NONE:
579 /* no need to do IPsec. */
39236c6e 580 ip6obf.needipsec = FALSE;
1c79356b 581 break;
316670eb 582
1c79356b
A
583 case IPSEC_POLICY_IPSEC:
584 if (sp->req == NULL) {
585 /* acquire a policy */
586 error = key_spdacquire(sp);
9bccf70c 587 goto freehdrs;
1c79356b 588 }
39236c6e 589 if (sp->ipsec_if) {
fe8ab488 590 goto skip_ipsec;
39236c6e
A
591 } else {
592 ip6obf.needipsec = TRUE;
593 }
1c79356b
A
594 break;
595
596 case IPSEC_POLICY_ENTRUST:
597 default:
39236c6e
A
598 printf("%s: Invalid policy found: %d\n", __func__, sp->policy);
599 break;
1c79356b 600 }
39236c6e 601skip_ipsec:
1c79356b
A
602#endif /* IPSEC */
603
604 /*
605 * Calculate the total length of the extension header chain.
606 * Keep the length of the unfragmentable part for fragmentation.
607 */
608 optlen = 0;
39236c6e 609 if (exthdrs.ip6e_hbh != NULL)
6d2010ae 610 optlen += exthdrs.ip6e_hbh->m_len;
39236c6e 611 if (exthdrs.ip6e_dest1 != NULL)
6d2010ae 612 optlen += exthdrs.ip6e_dest1->m_len;
39236c6e 613 if (exthdrs.ip6e_rthdr != NULL)
6d2010ae 614 optlen += exthdrs.ip6e_rthdr->m_len;
39236c6e 615 unfragpartlen = optlen + sizeof (struct ip6_hdr);
6d2010ae 616
1c79356b 617 /* NOTE: we don't add AH/ESP length here. do that later. */
39236c6e 618 if (exthdrs.ip6e_dest2 != NULL)
6d2010ae
A
619 optlen += exthdrs.ip6e_dest2->m_len;
620
1c79356b
A
621 /*
622 * If we need IPsec, or there is at least one extension header,
623 * separate IP6 header from the payload.
624 */
39236c6e
A
625 if ((
626#if IPSEC
627 ip6obf.needipsec ||
628#endif /* IPSEC */
629 optlen) && !ip6obf.hdrsplit) {
1c79356b
A
630 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
631 m = NULL;
632 goto freehdrs;
633 }
634 m = exthdrs.ip6e_ip6;
39236c6e 635 ip6obf.hdrsplit = TRUE;
1c79356b
A
636 }
637
638 /* adjust pointer */
639 ip6 = mtod(m, struct ip6_hdr *);
640
641 /* adjust mbuf packet header length */
642 m->m_pkthdr.len += optlen;
39236c6e 643 plen = m->m_pkthdr.len - sizeof (*ip6);
1c79356b
A
644
645 /* If this is a jumbo payload, insert a jumbo payload option. */
646 if (plen > IPV6_MAXPACKET) {
39236c6e 647 if (!ip6obf.hdrsplit) {
1c79356b
A
648 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
649 m = NULL;
650 goto freehdrs;
651 }
652 m = exthdrs.ip6e_ip6;
39236c6e 653 ip6obf.hdrsplit = TRUE;
1c79356b
A
654 }
655 /* adjust pointer */
656 ip6 = mtod(m, struct ip6_hdr *);
657 if ((error = ip6_insert_jumboopt(&exthdrs, plen)) != 0)
658 goto freehdrs;
659 ip6->ip6_plen = 0;
39236c6e 660 } else {
1c79356b 661 ip6->ip6_plen = htons(plen);
39236c6e 662 }
1c79356b
A
663 /*
664 * Concatenate headers and fill in next header fields.
665 * Here we have, on "m"
666 * IPv6 payload
667 * and we insert headers accordingly. Finally, we should be getting:
668 * IPv6 hbh dest1 rthdr ah* [esp* dest2 payload]
669 *
670 * during the header composing process, "m" points to IPv6 header.
671 * "mprev" points to an extension header prior to esp.
672 */
39236c6e
A
673 nexthdrp = &ip6->ip6_nxt;
674 mprev = m;
1c79356b 675
39236c6e
A
676 /*
677 * we treat dest2 specially. this makes IPsec processing
678 * much easier. the goal here is to make mprev point the
679 * mbuf prior to dest2.
680 *
681 * result: IPv6 dest2 payload
682 * m and mprev will point to IPv6 header.
683 */
684 if (exthdrs.ip6e_dest2 != NULL) {
685 if (!ip6obf.hdrsplit) {
686 panic("assumption failed: hdr not split");
687 /* NOTREACHED */
688 }
689 exthdrs.ip6e_dest2->m_next = m->m_next;
690 m->m_next = exthdrs.ip6e_dest2;
691 *mtod(exthdrs.ip6e_dest2, u_char *) = ip6->ip6_nxt;
692 ip6->ip6_nxt = IPPROTO_DSTOPTS;
693 }
694
695#define MAKE_CHAIN(m, mp, p, i) do { \
696 if (m != NULL) { \
697 if (!ip6obf.hdrsplit) { \
698 panic("assumption failed: hdr not split"); \
699 /* NOTREACHED */ \
700 } \
701 *mtod((m), u_char *) = *(p); \
702 *(p) = (i); \
703 p = mtod((m), u_char *); \
704 (m)->m_next = (mp)->m_next; \
705 (mp)->m_next = (m); \
706 (mp) = (m); \
707 } \
708} while (0)
709 /*
710 * result: IPv6 hbh dest1 rthdr dest2 payload
711 * m will point to IPv6 header. mprev will point to the
712 * extension header prior to dest2 (rthdr in the above case).
713 */
714 MAKE_CHAIN(exthdrs.ip6e_hbh, mprev, nexthdrp, IPPROTO_HOPOPTS);
715 MAKE_CHAIN(exthdrs.ip6e_dest1, mprev, nexthdrp, IPPROTO_DSTOPTS);
716 MAKE_CHAIN(exthdrs.ip6e_rthdr, mprev, nexthdrp, IPPROTO_ROUTING);
91447636 717
39236c6e
A
718#undef MAKE_CHAIN
719
720#if IPSEC
721 if (ip6obf.needipsec && (m->m_pkthdr.csum_flags & CSUM_DELAY_IPV6_DATA))
722 in6_delayed_cksum_offset(m, 0, optlen, nxt0);
723#endif /* IPSEC */
724
725 if (!TAILQ_EMPTY(&ipv6_filters)) {
726 struct ipfilter *filter;
727 int seen = (inject_filter_ref == NULL);
728 int fixscope = 0;
729
730 if (im6o != NULL && IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
731 ippo->ippo_flags |= IPPOF_MCAST_OPTS;
732 IM6O_LOCK(im6o);
733 ippo->ippo_mcast_ifnet = im6o->im6o_multicast_ifp;
734 ippo->ippo_mcast_ttl = im6o->im6o_multicast_hlim;
735 ippo->ippo_mcast_loop = im6o->im6o_multicast_loop;
736 IM6O_UNLOCK(im6o);
737 }
738
739 /* Hack: embed the scope_id in the destination */
740 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst) &&
741 (ip6->ip6_dst.s6_addr16[1] == 0) && (ro != NULL)) {
742 fixscope = 1;
743 ip6->ip6_dst.s6_addr16[1] =
744 htons(ro->ro_dst.sin6_scope_id);
745 }
746
747 ipf_ref();
748 TAILQ_FOREACH(filter, &ipv6_filters, ipf_link) {
749 /*
750 * Don't process packet twice if we've already seen it.
751 */
752 if (seen == 0) {
753 if ((struct ipfilter *)inject_filter_ref ==
754 filter)
755 seen = 1;
756 } else if (filter->ipf_filter.ipf_output != NULL) {
757 errno_t result;
758
759 result = filter->ipf_filter.ipf_output(
760 filter->ipf_filter.cookie,
761 (mbuf_t *)&m, ippo);
762 if (result == EJUSTRETURN) {
763 ipf_unref();
764 goto done;
765 }
766 if (result != 0) {
767 ipf_unref();
768 goto bad;
91447636 769 }
91447636 770 }
91447636 771 }
39236c6e
A
772 ipf_unref();
773
774 ip6 = mtod(m, struct ip6_hdr *);
775 /* Hack: cleanup embedded scope_id if we put it there */
776 if (fixscope)
777 ip6->ip6_dst.s6_addr16[1] = 0;
778 }
91447636 779
1c79356b 780#if IPSEC
39236c6e
A
781 if (ip6obf.needipsec) {
782 int segleft_org;
1c79356b
A
783
784 /*
785 * pointers after IPsec headers are not valid any more.
786 * other pointers need a great care too.
787 * (IPsec routines should not mangle mbufs prior to AH/ESP)
788 */
789 exthdrs.ip6e_dest2 = NULL;
790
39236c6e 791 if (exthdrs.ip6e_rthdr != NULL) {
1c79356b
A
792 rh = mtod(exthdrs.ip6e_rthdr, struct ip6_rthdr *);
793 segleft_org = rh->ip6r_segleft;
794 rh->ip6r_segleft = 0;
39236c6e
A
795 } else {
796 rh = NULL;
797 segleft_org = 0;
1c79356b
A
798 }
799
ebb1b9f4 800 ipsec_state.m = m;
39236c6e
A
801 error = ipsec6_output_trans(&ipsec_state, nexthdrp, mprev,
802 sp, flags, &needipsectun);
ebb1b9f4 803 m = ipsec_state.m;
1c79356b
A
804 if (error) {
805 /* mbuf is already reclaimed in ipsec6_output_trans. */
806 m = NULL;
807 switch (error) {
808 case EHOSTUNREACH:
809 case ENETUNREACH:
810 case EMSGSIZE:
811 case ENOBUFS:
812 case ENOMEM:
813 break;
814 default:
39236c6e
A
815 printf("ip6_output (ipsec): error code %d\n",
816 error);
817 /* FALLTHRU */
1c79356b
A
818 case ENOENT:
819 /* don't show these error codes to the user */
820 error = 0;
821 break;
822 }
823 goto bad;
824 }
39236c6e 825 if (exthdrs.ip6e_rthdr != NULL) {
1c79356b
A
826 /* ah6_output doesn't modify mbuf chain */
827 rh->ip6r_segleft = segleft_org;
828 }
1c79356b 829 }
39236c6e 830#endif /* IPSEC */
1c79356b
A
831
832 /*
6d2010ae 833 * If there is a routing header, replace the destination address field
1c79356b
A
834 * with the first hop of the routing header.
835 */
39236c6e 836 if (exthdrs.ip6e_rthdr != NULL) {
1c79356b 837 struct ip6_rthdr0 *rh0;
6d2010ae
A
838 struct in6_addr *addr;
839 struct sockaddr_in6 sa;
1c79356b 840
39236c6e
A
841 rh = (struct ip6_rthdr *)
842 (mtod(exthdrs.ip6e_rthdr, struct ip6_rthdr *));
9bccf70c 843 switch (rh->ip6r_type) {
1c79356b 844 case IPV6_RTHDR_TYPE_0:
39236c6e
A
845 rh0 = (struct ip6_rthdr0 *)rh;
846 addr = (struct in6_addr *)(void *)(rh0 + 1);
847
848 /*
849 * construct a sockaddr_in6 form of
850 * the first hop.
851 *
852 * XXX: we may not have enough
853 * information about its scope zone;
854 * there is no standard API to pass
855 * the information from the
856 * application.
857 */
858 bzero(&sa, sizeof (sa));
859 sa.sin6_family = AF_INET6;
860 sa.sin6_len = sizeof (sa);
861 sa.sin6_addr = addr[0];
862 if ((error = sa6_embedscope(&sa,
863 ip6_use_defzone)) != 0) {
864 goto bad;
865 }
866 ip6->ip6_dst = sa.sin6_addr;
867 bcopy(&addr[1], &addr[0], sizeof (struct in6_addr) *
868 (rh0->ip6r0_segleft - 1));
869 addr[rh0->ip6r0_segleft - 1] = finaldst;
870 /* XXX */
871 in6_clearscope(addr + rh0->ip6r0_segleft - 1);
872 break;
1c79356b 873 default: /* is it possible? */
39236c6e
A
874 error = EINVAL;
875 goto bad;
1c79356b
A
876 }
877 }
878
879 /* Source address validation */
880 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) &&
39236c6e 881 !(flags & IPV6_UNSPECSRC)) {
1c79356b
A
882 error = EOPNOTSUPP;
883 ip6stat.ip6s_badscope++;
884 goto bad;
885 }
886 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_src)) {
887 error = EOPNOTSUPP;
888 ip6stat.ip6s_badscope++;
889 goto bad;
890 }
891
892 ip6stat.ip6s_localout++;
893
894 /*
895 * Route packet.
896 */
39236c6e 897 if (ro == NULL) {
1c79356b 898 ro = &ip6route;
39236c6e 899 bzero((caddr_t)ro, sizeof (*ro));
1c79356b 900 }
39236c6e 901 VERIFY(ro_pmtu == NULL); /* must not get here if dummynet */
1c79356b 902 ro_pmtu = ro;
39236c6e 903 if (opt != NULL && opt->ip6po_rthdr)
1c79356b 904 ro = &opt->ip6po_route;
39236c6e 905 dst = SIN6(&ro->ro_dst);
6d2010ae 906
39236c6e 907 if (ro->ro_rt != NULL)
6d2010ae
A
908 RT_LOCK_ASSERT_NOTHELD(ro->ro_rt);
909 /*
910 * if specified, try to fill in the traffic class field.
911 * do not override if a non-zero value is already set.
912 * we check the diffserv field and the ecn field separately.
913 */
39236c6e 914 if (opt != NULL && opt->ip6po_tclass >= 0) {
6d2010ae
A
915 int mask = 0;
916
917 if ((ip6->ip6_flow & htonl(0xfc << 20)) == 0)
918 mask |= 0xfc;
919 if ((ip6->ip6_flow & htonl(0x03 << 20)) == 0)
920 mask |= 0x03;
39236c6e
A
921 if (mask != 0) {
922 ip6->ip6_flow |=
923 htonl((opt->ip6po_tclass & mask) << 20);
924 }
6d2010ae
A
925 }
926
927 /* fill in or override the hop limit field, if necessary. */
39236c6e 928 if (opt && opt->ip6po_hlim != -1) {
6d2010ae 929 ip6->ip6_hlim = opt->ip6po_hlim & 0xff;
39236c6e 930 } else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
6d2010ae
A
931 if (im6o != NULL) {
932 IM6O_LOCK(im6o);
933 ip6->ip6_hlim = im6o->im6o_multicast_hlim;
934 IM6O_UNLOCK(im6o);
935 } else {
936 ip6->ip6_hlim = ip6_defmcasthlim;
937 }
938 }
939
1c79356b 940 /*
b0d623f7
A
941 * If there is a cached route, check that it is to the same
942 * destination and is still up. If not, free it and try again.
943 * Test rt_flags without holding rt_lock for performance reasons;
944 * if the route is down it will hopefully be caught by the layer
945 * below (since it uses this route as a hint) or during the
946 * next transmit.
1c79356b 947 */
39236c6e
A
948 if (ROUTE_UNUSABLE(ro) || dst->sin6_family != AF_INET6 ||
949 !IN6_ARE_ADDR_EQUAL(&dst->sin6_addr, &ip6->ip6_dst))
950 ROUTE_RELEASE(ro);
951
b0d623f7 952 if (ro->ro_rt == NULL) {
39236c6e 953 bzero(dst, sizeof (*dst));
1c79356b 954 dst->sin6_family = AF_INET6;
39236c6e 955 dst->sin6_len = sizeof (struct sockaddr_in6);
1c79356b
A
956 dst->sin6_addr = ip6->ip6_dst;
957 }
958#if IPSEC
39236c6e 959 if (ip6obf.needipsec && needipsectun) {
6d2010ae
A
960#if CONFIG_DTRACE
961 struct ifnet *trace_ifp = (ifpp != NULL) ? (*ifpp) : NULL;
39236c6e 962#endif /* CONFIG_DTRACE */
1c79356b
A
963 /*
964 * All the extension headers will become inaccessible
965 * (since they can be encrypted).
966 * Don't panic, we need no more updates to extension headers
967 * on inner IPv6 packet (since they are now encapsulated).
968 *
969 * IPv6 [ESP|AH] IPv6 [extension headers] payload
970 */
39236c6e 971 bzero(&exthdrs, sizeof (exthdrs));
1c79356b
A
972 exthdrs.ip6e_ip6 = m;
973
ebb1b9f4 974 ipsec_state.m = m;
39236c6e
A
975 route_copyout(&ipsec_state.ro, (struct route *)ro,
976 sizeof (ipsec_state.ro));
977 ipsec_state.dst = SA(dst);
6d2010ae 978
39236c6e 979 /* So that we can see packets inside the tunnel */
6d2010ae 980 DTRACE_IP6(send, struct mbuf *, m, struct inpcb *, NULL,
39236c6e
A
981 struct ip6_hdr *, ip6, struct ifnet *, trace_ifp,
982 struct ip *, NULL, struct ip6_hdr *, ip6);
6d2010ae 983
ebb1b9f4 984 error = ipsec6_output_tunnel(&ipsec_state, sp, flags);
39236c6e
A
985 /* tunneled in IPv4? packet is gone */
986 if (ipsec_state.tunneled == 4)
2d21ac55 987 goto done;
ebb1b9f4
A
988 m = ipsec_state.m;
989 ipsec_saved_route = ro;
990 ro = (struct route_in6 *)&ipsec_state.ro;
39236c6e 991 dst = SIN6(ipsec_state.dst);
1c79356b
A
992 if (error) {
993 /* mbuf is already reclaimed in ipsec6_output_tunnel. */
994 m0 = m = NULL;
995 m = NULL;
996 switch (error) {
997 case EHOSTUNREACH:
998 case ENETUNREACH:
999 case EMSGSIZE:
1000 case ENOBUFS:
1001 case ENOMEM:
1002 break;
1003 default:
39236c6e
A
1004 printf("ip6_output (ipsec): error code %d\n",
1005 error);
1006 /* FALLTHRU */
1c79356b
A
1007 case ENOENT:
1008 /* don't show these error codes to the user */
1009 error = 0;
1010 break;
1011 }
1012 goto bad;
1013 }
316670eb 1014 /*
39236c6e
A
1015 * The packet has been encapsulated so the ifscope
1016 * is no longer valid since it does not apply to the
1017 * outer address: ignore the ifscope.
6d2010ae 1018 */
39236c6e
A
1019 if (flags & IPV6_OUTARGS) {
1020 ip6oa->ip6oa_boundif = IFSCOPE_NONE;
1021 ip6oa->ip6oa_flags &= ~IP6OAF_BOUND_IF;
1022 }
6d2010ae
A
1023 if (opt != NULL && opt->ip6po_pktinfo != NULL) {
1024 if (opt->ip6po_pktinfo->ipi6_ifindex != IFSCOPE_NONE)
1025 opt->ip6po_pktinfo->ipi6_ifindex = IFSCOPE_NONE;
1026 }
1c79356b
A
1027 exthdrs.ip6e_ip6 = m;
1028 }
55e303ae 1029#endif /* IPSEC */
1c79356b 1030
6d2010ae
A
1031 /* for safety */
1032 if (ifp != NULL) {
1033 ifnet_release(ifp);
1034 ifp = NULL;
1035 }
1c79356b 1036
6d2010ae
A
1037 /* adjust pointer */
1038 ip6 = mtod(m, struct ip6_hdr *);
1c79356b 1039
39236c6e
A
1040 if (ip6obf.select_srcif) {
1041 bzero(&src_sa, sizeof (src_sa));
6d2010ae 1042 src_sa.sin6_family = AF_INET6;
39236c6e 1043 src_sa.sin6_len = sizeof (src_sa);
6d2010ae
A
1044 src_sa.sin6_addr = ip6->ip6_src;
1045 }
39236c6e 1046 bzero(&dst_sa, sizeof (dst_sa));
6d2010ae 1047 dst_sa.sin6_family = AF_INET6;
39236c6e 1048 dst_sa.sin6_len = sizeof (dst_sa);
6d2010ae
A
1049 dst_sa.sin6_addr = ip6->ip6_dst;
1050
316670eb
A
1051 /*
1052 * in6_selectroute() might return an ifp with its reference held
1053 * even in the error case, so make sure to release its reference.
39236c6e 1054 * ip6oa may be NULL if IPV6_OUTARGS isn't set.
316670eb 1055 */
39236c6e
A
1056 if ((error = in6_selectroute(ip6obf.select_srcif ? &src_sa : NULL,
1057 &dst_sa, opt, im6o, &src_ia, ro, &ifp, &rt, 0, ip6oa)) != 0) {
6d2010ae
A
1058 switch (error) {
1059 case EHOSTUNREACH:
1060 ip6stat.ip6s_noroute++;
1061 break;
1062 case EADDRNOTAVAIL:
1063 default:
1064 break; /* XXX statistics? */
1c79356b 1065 }
6d2010ae
A
1066 if (ifp != NULL)
1067 in6_ifstat_inc(ifp, ifs6_out_discard);
316670eb 1068 /* ifp (if non-NULL) will be released at the end */
6d2010ae
A
1069 goto bad;
1070 }
1071 if (rt == NULL) {
b0d623f7 1072 /*
6d2010ae
A
1073 * If in6_selectroute() does not return a route entry,
1074 * dst may not have been updated.
b0d623f7 1075 */
6d2010ae
A
1076 *dst = dst_sa; /* XXX */
1077 }
b0d623f7 1078
6d2010ae
A
1079 /*
1080 * then rt (for unicast) and ifp must be non-NULL valid values.
1081 */
39236c6e 1082 if (!(flags & IPV6_FORWARDING)) {
39236c6e 1083 in6_ifstat_inc_na(ifp, ifs6_out_request);
6d2010ae
A
1084 }
1085 if (rt != NULL) {
1086 RT_LOCK(rt);
1087 ia = (struct in6_ifaddr *)(rt->rt_ifa);
1088 if (ia != NULL)
1089 IFA_ADDREF(&ia->ia_ifa);
1090 rt->rt_use++;
1091 RT_UNLOCK(rt);
1092 }
1c79356b 1093
6d2010ae
A
1094 /*
1095 * The outgoing interface must be in the zone of source and
39236c6e
A
1096 * destination addresses (except local/loopback). We should
1097 * use ia_ifp to support the case of sending packets to an
1098 * address of our own.
6d2010ae
A
1099 */
1100 if (ia != NULL && ia->ia_ifp) {
316670eb 1101 ifnet_reference(ia->ia_ifp); /* for origifp */
6d2010ae
A
1102 if (origifp != NULL)
1103 ifnet_release(origifp);
1104 origifp = ia->ia_ifp;
1105 } else {
1106 if (ifp != NULL)
316670eb 1107 ifnet_reference(ifp); /* for origifp */
6d2010ae
A
1108 if (origifp != NULL)
1109 ifnet_release(origifp);
1110 origifp = ifp;
1111 }
1c79356b 1112
39236c6e
A
1113 /* skip scope enforcements for local/loopback route */
1114 if (rt == NULL || !(rt->rt_ifp->if_flags & IFF_LOOPBACK)) {
1115 struct in6_addr src0, dst0;
1116 u_int32_t zone;
1c79356b 1117
39236c6e
A
1118 src0 = ip6->ip6_src;
1119 if (in6_setscope(&src0, origifp, &zone))
1120 goto badscope;
1121 bzero(&src_sa, sizeof (src_sa));
1122 src_sa.sin6_family = AF_INET6;
1123 src_sa.sin6_len = sizeof (src_sa);
1124 src_sa.sin6_addr = ip6->ip6_src;
1125 if ((sa6_recoverscope(&src_sa, TRUE) ||
1126 zone != src_sa.sin6_scope_id))
1127 goto badscope;
1128
1129 dst0 = ip6->ip6_dst;
1130 if ((in6_setscope(&dst0, origifp, &zone)))
1131 goto badscope;
1132 /* re-initialize to be sure */
1133 bzero(&dst_sa, sizeof (dst_sa));
1134 dst_sa.sin6_family = AF_INET6;
1135 dst_sa.sin6_len = sizeof (dst_sa);
1136 dst_sa.sin6_addr = ip6->ip6_dst;
1137 if ((sa6_recoverscope(&dst_sa, TRUE) ||
1138 zone != dst_sa.sin6_scope_id))
1139 goto badscope;
1140
1141 /* scope check is done. */
1142 goto routefound;
1143
1144badscope:
1145 ip6stat.ip6s_badscope++;
1146 in6_ifstat_inc(origifp, ifs6_out_discard);
1147 if (error == 0)
1148 error = EHOSTUNREACH; /* XXX */
1149 goto bad;
1150 }
1c79356b 1151
39236c6e
A
1152routefound:
1153 if (rt != NULL && !IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
1154 if (opt != NULL && opt->ip6po_nextroute.ro_rt) {
1c79356b 1155 /*
6d2010ae
A
1156 * The nexthop is explicitly specified by the
1157 * application. We assume the next hop is an IPv6
1158 * address.
1c79356b 1159 */
39236c6e
A
1160 dst = SIN6(opt->ip6po_nexthop);
1161 } else if ((rt->rt_flags & RTF_GATEWAY)) {
1162 dst = SIN6(rt->rt_gateway);
1163 }
1164 /*
1165 * For packets destined to local/loopback, record the
1166 * source the source interface (which owns the source
1167 * address), as well as the output interface. This is
1168 * needed to reconstruct the embedded zone for the
1169 * link-local address case in ip6_input().
1170 */
1171 if (ia != NULL && (ifp->if_flags & IFF_LOOPBACK)) {
1172 uint32_t srcidx;
1173
1174 if (src_ia != NULL)
1175 srcidx = src_ia->ia_ifp->if_index;
1176 else if (ro->ro_srcia != NULL)
1177 srcidx = ro->ro_srcia->ifa_ifp->if_index;
1178 else
1179 srcidx = 0;
1180
1181 ip6_setsrcifaddr_info(m, srcidx, NULL);
1182 ip6_setdstifaddr_info(m, 0, ia);
b0d623f7 1183 }
6d2010ae 1184 }
b0d623f7 1185
6d2010ae
A
1186 if (!IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
1187 m->m_flags &= ~(M_BCAST | M_MCAST); /* just in case */
1188 } else {
1189 struct in6_multi *in6m;
1c79356b 1190
6d2010ae 1191 m->m_flags = (m->m_flags & ~M_BCAST) | M_MCAST;
39236c6e 1192 in6_ifstat_inc_na(ifp, ifs6_out_mcast);
1c79356b
A
1193
1194 /*
1195 * Confirm that the outgoing interface supports multicast.
1196 */
39236c6e 1197 if (!(ifp->if_flags & IFF_MULTICAST)) {
1c79356b
A
1198 ip6stat.ip6s_noroute++;
1199 in6_ifstat_inc(ifp, ifs6_out_discard);
1200 error = ENETUNREACH;
1201 goto bad;
1202 }
6d2010ae
A
1203 in6_multihead_lock_shared();
1204 IN6_LOOKUP_MULTI(&ip6->ip6_dst, ifp, in6m);
1205 in6_multihead_lock_done();
1206 if (im6o != NULL)
1207 IM6O_LOCK(im6o);
1c79356b 1208 if (in6m != NULL &&
39236c6e 1209 (im6o == NULL || im6o->im6o_multicast_loop)) {
6d2010ae
A
1210 if (im6o != NULL)
1211 IM6O_UNLOCK(im6o);
1c79356b
A
1212 /*
1213 * If we belong to the destination multicast group
1214 * on the outgoing interface, and the caller did not
1215 * forbid loopback, loop back a copy.
1216 */
39236c6e 1217 ip6_mloopback(NULL, ifp, m, dst, optlen, nxt0);
fe8ab488
A
1218 } else if (im6o != NULL)
1219 IM6O_UNLOCK(im6o);
6d2010ae
A
1220 if (in6m != NULL)
1221 IN6M_REMREF(in6m);
1c79356b
A
1222 /*
1223 * Multicasts with a hoplimit of zero may be looped back,
1224 * above, but must not be transmitted on a network.
1225 * Also, multicasts addressed to the loopback interface
1226 * are not sent -- the above call to ip6_mloopback() will
1227 * loop back a copy if this host actually belongs to the
1228 * destination group on the loopback interface.
1229 */
6d2010ae
A
1230 if (ip6->ip6_hlim == 0 || (ifp->if_flags & IFF_LOOPBACK) ||
1231 IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst)) {
1c79356b
A
1232 m_freem(m);
1233 goto done;
1234 }
1235 }
1236
1237 /*
1238 * Fill the outgoing inteface to tell the upper layer
1239 * to increment per-interface statistics.
1240 */
6d2010ae
A
1241 if (ifpp != NULL) {
1242 ifnet_reference(ifp); /* for caller */
1243 if (*ifpp != NULL)
1244 ifnet_release(*ifpp);
1c79356b 1245 *ifpp = ifp;
1c79356b 1246 }
b0d623f7 1247
6d2010ae
A
1248 /* Determine path MTU. */
1249 if ((error = ip6_getpmtu(ro_pmtu, ro, ifp, &finaldst, &mtu,
1250 &alwaysfrag)) != 0)
1251 goto bad;
1c79356b
A
1252
1253 /*
6d2010ae
A
1254 * The caller of this function may specify to use the minimum MTU
1255 * in some cases.
1256 * An advanced API option (IPV6_USE_MIN_MTU) can also override MTU
1257 * setting. The logic is a bit complicated; by default, unicast
1258 * packets will follow path MTU while multicast packets will be sent at
1259 * the minimum MTU. If IP6PO_MINMTU_ALL is specified, all packets
1260 * including unicast ones will be sent at the minimum MTU. Multicast
1261 * packets will always be sent at the minimum MTU unless
1262 * IP6PO_MINMTU_DISABLE is explicitly specified.
1263 * See RFC 3542 for more details.
1c79356b 1264 */
6d2010ae 1265 if (mtu > IPV6_MMTU) {
39236c6e 1266 if ((flags & IPV6_MINMTU)) {
6d2010ae 1267 mtu = IPV6_MMTU;
39236c6e 1268 } else if (opt && opt->ip6po_minmtu == IP6PO_MINMTU_ALL) {
6d2010ae 1269 mtu = IPV6_MMTU;
39236c6e
A
1270 } else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
1271 (opt == NULL ||
1272 opt->ip6po_minmtu != IP6PO_MINMTU_DISABLE)) {
6d2010ae 1273 mtu = IPV6_MMTU;
b0d623f7 1274 }
1c79356b 1275 }
6d2010ae 1276
9bccf70c
A
1277 /*
1278 * clear embedded scope identifiers if necessary.
1279 * in6_clearscope will touch the addresses only when necessary.
1280 */
1281 in6_clearscope(&ip6->ip6_src);
1282 in6_clearscope(&ip6->ip6_dst);
1c79356b 1283
7e4a7d39 1284#if IPFW2
1c79356b
A
1285 /*
1286 * Check with the firewall...
1287 */
39236c6e 1288 if (ip6_fw_enable && ip6_fw_chk_ptr) {
1c79356b 1289 u_short port = 0;
55e303ae 1290 m->m_pkthdr.rcvif = NULL; /* XXX */
1c79356b 1291 /* If ipfw says divert, we have to just drop packet */
91447636 1292 if (ip6_fw_chk_ptr(&ip6, ifp, &port, &m)) {
1c79356b
A
1293 m_freem(m);
1294 goto done;
1295 }
39236c6e 1296 if (m == NULL) {
1c79356b
A
1297 error = EACCES;
1298 goto done;
1299 }
1300 }
39236c6e 1301#endif /* IPFW2 */
1c79356b
A
1302
1303 /*
1304 * If the outgoing packet contains a hop-by-hop options header,
1305 * it must be examined and processed even by the source node.
1306 * (RFC 2460, section 4.)
1307 */
39236c6e 1308 if (exthdrs.ip6e_hbh != NULL) {
9bccf70c 1309 struct ip6_hbh *hbh = mtod(exthdrs.ip6e_hbh, struct ip6_hbh *);
6d2010ae 1310 u_int32_t dummy; /* XXX unused */
39236c6e 1311 uint32_t oplen = 0; /* for ip6_process_hopopts() */
9bccf70c
A
1312#if DIAGNOSTIC
1313 if ((hbh->ip6h_len + 1) << 3 > exthdrs.ip6e_hbh->m_len)
1314 panic("ip6e_hbh is not continuous");
1315#endif
1c79356b 1316 /*
39236c6e
A
1317 * XXX: If we have to send an ICMPv6 error to the sender,
1318 * we need the M_LOOP flag since icmp6_error() expects
1319 * the IPv6 and the hop-by-hop options header are
1320 * continuous unless the flag is set.
1c79356b
A
1321 */
1322 m->m_flags |= M_LOOP;
1323 m->m_pkthdr.rcvif = ifp;
6d2010ae 1324 if (ip6_process_hopopts(m, (u_int8_t *)(hbh + 1),
39236c6e
A
1325 ((hbh->ip6h_len + 1) << 3) - sizeof (struct ip6_hbh),
1326 &dummy, &oplen) < 0) {
1c79356b 1327 /* m was already freed at this point */
39236c6e 1328 error = EINVAL; /* better error? */
1c79356b
A
1329 goto done;
1330 }
1331 m->m_flags &= ~M_LOOP; /* XXX */
1332 m->m_pkthdr.rcvif = NULL;
1333 }
1334
316670eb
A
1335#if DUMMYNET
1336check_with_pf:
39236c6e 1337#endif /* DUMMYNET */
b0d623f7 1338#if PF
6d2010ae 1339 if (PF_IS_ENABLED) {
316670eb
A
1340#if DUMMYNET
1341 /*
39236c6e
A
1342 * TODO: Need to save opt->ip6po_flags for reinjection
1343 * rdar://10434993
316670eb
A
1344 */
1345 args.fwa_m = m;
1346 args.fwa_oif = ifp;
1347 args.fwa_oflags = flags;
39236c6e
A
1348 if (flags & IPV6_OUTARGS)
1349 args.fwa_ip6oa = ip6oa;
316670eb
A
1350 args.fwa_ro6 = ro;
1351 args.fwa_dst6 = dst;
1352 args.fwa_ro6_pmtu = ro_pmtu;
1353 args.fwa_origifp = origifp;
1354 args.fwa_mtu = mtu;
1355 args.fwa_alwaysfrag = alwaysfrag;
1356 args.fwa_unfragpartlen = unfragpartlen;
1357 args.fwa_exthdrs = &exthdrs;
6d2010ae 1358 /* Invoke outbound packet filter */
316670eb 1359 error = pf_af_hook(ifp, NULL, &m, AF_INET6, FALSE, &args);
39236c6e 1360#else /* !DUMMYNET */
316670eb 1361 error = pf_af_hook(ifp, NULL, &m, AF_INET6, FALSE, NULL);
39236c6e 1362#endif /* !DUMMYNET */
b0d623f7 1363
316670eb
A
1364 if (error != 0 || m == NULL) {
1365 /*
1366 * Note that if we ever handle packet chain, we will
39236c6e 1367 * have to restore the linkage from the previous
316670eb
A
1368 * packet to the next like in ip_outout_list()
1369 */
6d2010ae 1370 if (m != NULL) {
39236c6e
A
1371 panic("%s: unexpected packet %p\n",
1372 __func__, m);
6d2010ae
A
1373 /* NOTREACHED */
1374 }
1375 /* Already freed by callee */
1376 goto done;
b0d623f7 1377 }
6d2010ae 1378 ip6 = mtod(m, struct ip6_hdr *);
b0d623f7 1379 }
b0d623f7
A
1380#endif /* PF */
1381
1c79356b
A
1382 /*
1383 * Send the packet to the outgoing interface.
1384 * If necessary, do IPv6 fragmentation before sending.
6d2010ae
A
1385 *
1386 * the logic here is rather complex:
1387 * 1: normal case (dontfrag == 0, alwaysfrag == 0)
1388 * 1-a: send as is if tlen <= path mtu
1389 * 1-b: fragment if tlen > path mtu
1390 *
1391 * 2: if user asks us not to fragment (dontfrag == 1)
1392 * 2-a: send as is if tlen <= interface mtu
1393 * 2-b: error if tlen > interface mtu
1394 *
1395 * 3: if we always need to attach fragment header (alwaysfrag == 1)
1396 * always fragment
1397 *
1398 * 4: if dontfrag == 1 && alwaysfrag == 1
1399 * error, as we cannot handle this conflicting request
1c79356b
A
1400 */
1401 tlen = m->m_pkthdr.len;
6d2010ae 1402
39236c6e
A
1403 if (opt != NULL && (opt->ip6po_flags & IP6PO_DONTFRAG))
1404 ip6obf.dontfrag = TRUE;
6d2010ae 1405 else
39236c6e
A
1406 ip6obf.dontfrag = FALSE;
1407 if (ip6obf.dontfrag && alwaysfrag) { /* case 4 */
6d2010ae
A
1408 /* conflicting request - can't transmit */
1409 error = EMSGSIZE;
1410 goto bad;
1411 }
1412
1413 lck_rw_lock_shared(nd_if_rwlock);
316670eb 1414 /* Access without acquiring nd_ifinfo lock for performance */
39236c6e
A
1415 if (ip6obf.dontfrag && tlen > IN6_LINKMTU(ifp)) { /* case 2-b */
1416 lck_rw_done(nd_if_rwlock);
6d2010ae
A
1417 /*
1418 * Even if the DONTFRAG option is specified, we cannot send the
1419 * packet when the data length is larger than the MTU of the
1420 * outgoing interface.
1421 * Notify the error by sending IPV6_PATHMTU ancillary data as
1422 * well as returning an error code (the latter is not described
1423 * in the API spec.)
1424 */
1425 u_int32_t mtu32;
1426 struct ip6ctlparam ip6cp;
1427
1428 mtu32 = (u_int32_t)mtu;
39236c6e 1429 bzero(&ip6cp, sizeof (ip6cp));
6d2010ae 1430 ip6cp.ip6c_cmdarg = (void *)&mtu32;
39236c6e 1431 pfctlinput2(PRC_MSGSIZE, SA(&ro_pmtu->ro_dst), (void *)&ip6cp);
6d2010ae
A
1432 error = EMSGSIZE;
1433 goto bad;
39236c6e
A
1434 } else {
1435 lck_rw_done(nd_if_rwlock);
6d2010ae
A
1436 }
1437
1438 /*
1439 * transmit packet without fragmentation
1440 */
39236c6e
A
1441 if (ip6obf.dontfrag || (!alwaysfrag && /* case 1-a and 2-a */
1442 (tlen <= mtu || TSO_IPV6_OK(ifp, m) ||
1443 (ifp->if_hwassist & CSUM_FRAGMENT_IPV6)))) {
9bccf70c
A
1444#ifdef IPSEC
1445 /* clean ipsec history once it goes out of the node */
1446 ipsec_delaux(m);
39236c6e 1447#endif /* IPSEC */
9bccf70c 1448
39236c6e 1449 ip6_output_checksum(ifp, mtu, m, nxt0, tlen, optlen);
6d2010ae 1450
6d2010ae
A
1451 if (ro->ro_rt)
1452 RT_LOCK_ASSERT_NOTHELD(ro->ro_rt);
316670eb 1453 error = nd6_output(ifp, origifp, m, dst, ro->ro_rt, adv);
1c79356b 1454 goto done;
6d2010ae
A
1455 }
1456
1457 /*
1458 * try to fragment the packet. case 1-b and 3
1459 */
39236c6e
A
1460 if ((m->m_pkthdr.csum_flags & CSUM_TSO_IPV6)) {
1461 /* TSO and fragment aren't compatible */
1462 error = EMSGSIZE;
1463 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1464 goto bad;
1465 } else if (mtu < IPV6_MMTU) {
6d2010ae 1466 /* path MTU cannot be less than IPV6_MMTU */
1c79356b
A
1467 error = EMSGSIZE;
1468 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1469 goto bad;
6d2010ae
A
1470 } else if (ip6->ip6_plen == 0) {
1471 /* jumbo payload cannot be fragmented */
1c79356b
A
1472 error = EMSGSIZE;
1473 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1474 goto bad;
1475 } else {
1476 struct mbuf **mnext, *m_frgpart;
6d2010ae
A
1477 struct ip6_frag *ip6f;
1478 u_int32_t id = htonl(ip6_randomid());
1c79356b
A
1479 u_char nextproto;
1480
1481 /*
1482 * Too large for the destination or interface;
1483 * fragment if possible.
1484 * Must be able to put at least 8 bytes per fragment.
1485 */
1486 hlen = unfragpartlen;
1487 if (mtu > IPV6_MAXPACKET)
1488 mtu = IPV6_MAXPACKET;
9bccf70c 1489
39236c6e 1490 len = (mtu - hlen - sizeof (struct ip6_frag)) & ~7;
1c79356b
A
1491 if (len < 8) {
1492 error = EMSGSIZE;
1493 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1494 goto bad;
1495 }
1496
1497 mnext = &m->m_nextpkt;
1498
1499 /*
1500 * Change the next header field of the last header in the
1501 * unfragmentable part.
1502 */
39236c6e 1503 if (exthdrs.ip6e_rthdr != NULL) {
1c79356b
A
1504 nextproto = *mtod(exthdrs.ip6e_rthdr, u_char *);
1505 *mtod(exthdrs.ip6e_rthdr, u_char *) = IPPROTO_FRAGMENT;
39236c6e 1506 } else if (exthdrs.ip6e_dest1 != NULL) {
1c79356b
A
1507 nextproto = *mtod(exthdrs.ip6e_dest1, u_char *);
1508 *mtod(exthdrs.ip6e_dest1, u_char *) = IPPROTO_FRAGMENT;
39236c6e 1509 } else if (exthdrs.ip6e_hbh != NULL) {
1c79356b
A
1510 nextproto = *mtod(exthdrs.ip6e_hbh, u_char *);
1511 *mtod(exthdrs.ip6e_hbh, u_char *) = IPPROTO_FRAGMENT;
1512 } else {
1513 nextproto = ip6->ip6_nxt;
1514 ip6->ip6_nxt = IPPROTO_FRAGMENT;
1515 }
1516
39236c6e
A
1517 if (m->m_pkthdr.csum_flags & CSUM_DELAY_IPV6_DATA)
1518 in6_delayed_cksum_offset(m, 0, optlen, nxt0);
6d2010ae 1519
1c79356b
A
1520 /*
1521 * Loop through length of segment after first fragment,
55e303ae
A
1522 * make new header and copy data of each part and link onto
1523 * chain.
1c79356b
A
1524 */
1525 m0 = m;
1526 for (off = hlen; off < tlen; off += len) {
39236c6e
A
1527 struct ip6_hdr *mhip6;
1528
2d21ac55 1529 MGETHDR(m, M_DONTWAIT, MT_HEADER); /* MAC-OK */
39236c6e 1530 if (m == NULL) {
1c79356b
A
1531 error = ENOBUFS;
1532 ip6stat.ip6s_odropped++;
1533 goto sendorfree;
1534 }
9bccf70c 1535 m->m_pkthdr.rcvif = NULL;
1c79356b
A
1536 m->m_flags = m0->m_flags & M_COPYFLAGS;
1537 *mnext = m;
1538 mnext = &m->m_nextpkt;
1539 m->m_data += max_linkhdr;
1540 mhip6 = mtod(m, struct ip6_hdr *);
1541 *mhip6 = *ip6;
39236c6e
A
1542 m->m_len = sizeof (*mhip6);
1543 error = ip6_insertfraghdr(m0, m, hlen, &ip6f);
1544 if (error) {
1c79356b
A
1545 ip6stat.ip6s_odropped++;
1546 goto sendorfree;
1547 }
1548 ip6f->ip6f_offlg = htons((u_short)((off - hlen) & ~7));
1549 if (off + len >= tlen)
1550 len = tlen - off;
1551 else
1552 ip6f->ip6f_offlg |= IP6F_MORE_FRAG;
1553 mhip6->ip6_plen = htons((u_short)(len + hlen +
39236c6e
A
1554 sizeof (*ip6f) - sizeof (struct ip6_hdr)));
1555 if ((m_frgpart = m_copy(m0, off, len)) == NULL) {
1c79356b
A
1556 error = ENOBUFS;
1557 ip6stat.ip6s_odropped++;
1558 goto sendorfree;
1559 }
1560 m_cat(m, m_frgpart);
39236c6e
A
1561 m->m_pkthdr.len = len + hlen + sizeof (*ip6f);
1562 m->m_pkthdr.rcvif = NULL;
316670eb 1563
39236c6e 1564 M_COPY_CLASSIFIER(m, m0);
316670eb 1565 M_COPY_PFTAG(m, m0);
316670eb 1566
39236c6e 1567#ifdef notyet
2d21ac55
A
1568#if CONFIG_MACF_NET
1569 mac_create_fragment(m0, m);
39236c6e
A
1570#endif /* CONFIG_MACF_NET */
1571#endif /* notyet */
1572
1c79356b
A
1573 ip6f->ip6f_reserved = 0;
1574 ip6f->ip6f_ident = id;
1575 ip6f->ip6f_nxt = nextproto;
1576 ip6stat.ip6s_ofragments++;
1577 in6_ifstat_inc(ifp, ifs6_out_fragcreat);
1578 }
1579
1580 in6_ifstat_inc(ifp, ifs6_out_fragok);
1581 }
1582
1583 /*
1584 * Remove leading garbages.
1585 */
1586sendorfree:
1587 m = m0->m_nextpkt;
39236c6e 1588 m0->m_nextpkt = NULL;
1c79356b 1589 m_freem(m0);
39236c6e 1590 for (m0 = m; m != NULL; m = m0) {
1c79356b 1591 m0 = m->m_nextpkt;
39236c6e 1592 m->m_nextpkt = NULL;
1c79356b 1593 if (error == 0) {
9bccf70c
A
1594#if IPSEC
1595 /* clean ipsec history once it goes out of the node */
1596 ipsec_delaux(m);
39236c6e 1597#endif /* IPSEC */
316670eb
A
1598 error = nd6_output(ifp, origifp, m, dst, ro->ro_rt,
1599 adv);
39236c6e 1600 } else {
1c79356b 1601 m_freem(m);
39236c6e 1602 }
1c79356b
A
1603 }
1604
1605 if (error == 0)
1606 ip6stat.ip6s_fragmented++;
1607
1608done:
39236c6e 1609 ROUTE_RELEASE(&ip6route);
ebb1b9f4 1610#if IPSEC
39236c6e 1611 ROUTE_RELEASE(&ipsec_state.ro);
2d21ac55
A
1612 if (sp != NULL)
1613 key_freesp(sp, KEY_SADB_UNLOCKED);
1c79356b 1614#endif /* IPSEC */
fe8ab488
A
1615#if NECP
1616 ROUTE_RELEASE(&necp_route);
1617#endif /* NECP */
39236c6e
A
1618#if DUMMYNET
1619 ROUTE_RELEASE(&saved_route);
1620 ROUTE_RELEASE(&saved_ro_pmtu);
1621#endif /* DUMMYNET */
1c79356b 1622
b0d623f7 1623 if (ia != NULL)
6d2010ae 1624 IFA_REMREF(&ia->ia_ifa);
39236c6e
A
1625 if (src_ia != NULL)
1626 IFA_REMREF(&src_ia->ia_ifa);
6d2010ae
A
1627 if (ifp != NULL)
1628 ifnet_release(ifp);
1629 if (origifp != NULL)
1630 ifnet_release(origifp);
1631 return (error);
1c79356b
A
1632
1633freehdrs:
39236c6e
A
1634 if (exthdrs.ip6e_hbh != NULL)
1635 m_freem(exthdrs.ip6e_hbh);
1636 if (exthdrs.ip6e_dest1 != NULL)
1637 m_freem(exthdrs.ip6e_dest1);
1638 if (exthdrs.ip6e_rthdr != NULL)
1639 m_freem(exthdrs.ip6e_rthdr);
1640 if (exthdrs.ip6e_dest2 != NULL)
1641 m_freem(exthdrs.ip6e_dest2);
1642 /* FALLTHRU */
1c79356b 1643bad:
39236c6e
A
1644 if (m != NULL)
1645 m_freem(m);
1c79356b 1646 goto done;
39236c6e
A
1647
1648#undef ipf_pktopts
1649#undef exthdrs
1650#undef ip6route
1651#undef ipsec_state
1652#undef saved_route
1653#undef saved_ro_pmtu
1654#undef args
1c79356b
A
1655}
1656
1657static int
39236c6e 1658ip6_copyexthdr(struct mbuf **mp, caddr_t hdr, int hlen)
1c79356b
A
1659{
1660 struct mbuf *m;
1661
1662 if (hlen > MCLBYTES)
39236c6e 1663 return (ENOBUFS); /* XXX */
1c79356b
A
1664
1665 MGET(m, M_DONTWAIT, MT_DATA);
39236c6e
A
1666 if (m == NULL)
1667 return (ENOBUFS);
1c79356b
A
1668
1669 if (hlen > MLEN) {
1670 MCLGET(m, M_DONTWAIT);
39236c6e 1671 if (!(m->m_flags & M_EXT)) {
1c79356b 1672 m_free(m);
6d2010ae 1673 return (ENOBUFS);
1c79356b
A
1674 }
1675 }
1676 m->m_len = hlen;
39236c6e 1677 if (hdr != NULL)
1c79356b
A
1678 bcopy(hdr, mtod(m, caddr_t), hlen);
1679
1680 *mp = m;
6d2010ae 1681 return (0);
1c79356b
A
1682}
1683
39236c6e
A
1684static void
1685ip6_out_cksum_stats(int proto, u_int32_t len)
1686{
1687 switch (proto) {
1688 case IPPROTO_TCP:
1689 tcp_out6_cksum_stats(len);
1690 break;
1691 case IPPROTO_UDP:
1692 udp_out6_cksum_stats(len);
1693 break;
1694 default:
1695 /* keep only TCP or UDP stats for now */
1696 break;
1697 }
1698}
1699
6d2010ae 1700/*
39236c6e
A
1701 * Process a delayed payload checksum calculation (outbound path.)
1702 *
1703 * hoff is the number of bytes beyond the mbuf data pointer which
1704 * points to the IPv6 header. optlen is the number of bytes, if any,
1705 * between the end of IPv6 header and the beginning of the ULP payload
1706 * header, which represents the extension headers. If optlen is less
1707 * than zero, this routine will bail when it detects extension headers.
1708 *
1709 * Returns a bitmask representing all the work done in software.
6d2010ae 1710 */
39236c6e
A
1711uint32_t
1712in6_finalize_cksum(struct mbuf *m, uint32_t hoff, int32_t optlen,
1713 int32_t nxt0, uint32_t csum_flags)
6d2010ae 1714{
39236c6e
A
1715 unsigned char buf[sizeof (struct ip6_hdr)] __attribute__((aligned(8)));
1716 struct ip6_hdr *ip6;
1717 uint32_t offset, mlen, hlen, olen, sw_csum;
1718 uint16_t csum, ulpoff, plen;
1719 uint8_t nxt;
6d2010ae 1720
39236c6e
A
1721 _CASSERT(sizeof (csum) == sizeof (uint16_t));
1722 VERIFY(m->m_flags & M_PKTHDR);
1723
1724 sw_csum = (csum_flags & m->m_pkthdr.csum_flags);
1725
1726 if ((sw_csum &= CSUM_DELAY_IPV6_DATA) == 0)
1727 goto done;
1728
1729 mlen = m->m_pkthdr.len; /* total mbuf len */
1730 hlen = sizeof (*ip6); /* IPv6 header len */
1731
1732 /* sanity check (need at least IPv6 header) */
1733 if (mlen < (hoff + hlen)) {
1734 panic("%s: mbuf %p pkt len (%u) < hoff+ip6_hdr "
1735 "(%u+%u)\n", __func__, m, mlen, hoff, hlen);
1736 /* NOTREACHED */
1737 }
1738
1739 /*
1740 * In case the IPv6 header is not contiguous, or not 32-bit
1741 * aligned, copy it to a local buffer.
1742 */
1743 if ((hoff + hlen) > m->m_len ||
1744 !IP6_HDR_ALIGNED_P(mtod(m, caddr_t) + hoff)) {
1745 m_copydata(m, hoff, hlen, (caddr_t)buf);
1746 ip6 = (struct ip6_hdr *)(void *)buf;
1747 } else {
1748 ip6 = (struct ip6_hdr *)(void *)(m->m_data + hoff);
1749 }
1750
1751 nxt = ip6->ip6_nxt;
1752 plen = ntohs(ip6->ip6_plen);
1753 if (plen != (mlen - (hoff + hlen))) {
1754 plen = OSSwapInt16(plen);
1755 if (plen != (mlen - (hoff + hlen))) {
1756 /* Don't complain for jumbograms */
1757 if (plen != 0 || nxt != IPPROTO_HOPOPTS) {
1758 printf("%s: mbuf 0x%llx proto %d IPv6 "
1759 "plen %d (%x) [swapped %d (%x)] doesn't "
1760 "match actual packet length; %d is used "
1761 "instead\n", __func__,
1762 (uint64_t)VM_KERNEL_ADDRPERM(m), nxt,
1763 ip6->ip6_plen, ip6->ip6_plen, plen, plen,
1764 (mlen - (hoff + hlen)));
1765 }
1766 plen = mlen - (hoff + hlen);
1767 }
1768 }
1769
1770 if (optlen < 0) {
1771 /* next header isn't TCP/UDP and we don't know optlen, bail */
1772 if (nxt != IPPROTO_TCP && nxt != IPPROTO_UDP) {
1773 sw_csum = 0;
1774 goto done;
1775 }
1776 olen = 0;
1777 } else {
1778 /* caller supplied the original transport number; use it */
1779 if (nxt0 >= 0)
1780 nxt = nxt0;
1781 olen = optlen;
6d2010ae
A
1782 }
1783
39236c6e
A
1784 offset = hoff + hlen + olen; /* ULP header */
1785
1786 /* sanity check */
1787 if (mlen < offset) {
1788 panic("%s: mbuf %p pkt len (%u) < hoff+ip6_hdr+ext_hdr "
1789 "(%u+%u+%u)\n", __func__, m, mlen, hoff, hlen, olen);
1790 /* NOTREACHED */
1791 }
1792
1793 /*
1794 * offset is added to the lower 16-bit value of csum_data,
1795 * which is expected to contain the ULP offset; therefore
1796 * CSUM_PARTIAL offset adjustment must be undone.
1797 */
1798 if ((m->m_pkthdr.csum_flags & (CSUM_PARTIAL|CSUM_DATA_VALID)) ==
1799 (CSUM_PARTIAL|CSUM_DATA_VALID)) {
1800 /*
1801 * Get back the original ULP offset (this will
1802 * undo the CSUM_PARTIAL logic in ip6_output.)
1803 */
1804 m->m_pkthdr.csum_data = (m->m_pkthdr.csum_tx_stuff -
1805 m->m_pkthdr.csum_tx_start);
1806 }
1807
1808 ulpoff = (m->m_pkthdr.csum_data & 0xffff); /* ULP csum offset */
1809
1810 if (mlen < (ulpoff + sizeof (csum))) {
1811 panic("%s: mbuf %p pkt len (%u) proto %d invalid ULP "
1812 "cksum offset (%u) cksum flags 0x%x\n", __func__,
1813 m, mlen, nxt, ulpoff, m->m_pkthdr.csum_flags);
1814 /* NOTREACHED */
1815 }
1816
1817 csum = inet6_cksum(m, 0, offset, plen - olen);
1818
1819 /* Update stats */
1820 ip6_out_cksum_stats(nxt, plen - olen);
1821
1822 /* RFC1122 4.1.3.4 */
1823 if (csum == 0 && (m->m_pkthdr.csum_flags & CSUM_UDPIPV6))
1824 csum = 0xffff;
1825
1826 /* Insert the checksum in the ULP csum field */
1827 offset += ulpoff;
1828 if ((offset + sizeof (csum)) > m->m_len) {
1829 m_copyback(m, offset, sizeof (csum), &csum);
1830 } else if (IP6_HDR_ALIGNED_P(mtod(m, char *) + hoff)) {
316670eb 1831 *(uint16_t *)(void *)(mtod(m, char *) + offset) = csum;
6d2010ae 1832 } else {
316670eb 1833 bcopy(&csum, (mtod(m, char *) + offset), sizeof (csum));
6d2010ae 1834 }
39236c6e
A
1835 m->m_pkthdr.csum_flags &=
1836 ~(CSUM_DELAY_IPV6_DATA | CSUM_DATA_VALID | CSUM_PARTIAL);
1837
1838done:
1839 return (sw_csum);
6d2010ae 1840}
39236c6e 1841
1c79356b
A
1842/*
1843 * Insert jumbo payload option.
1844 */
1845static int
39236c6e 1846ip6_insert_jumboopt(struct ip6_exthdrs *exthdrs, u_int32_t plen)
1c79356b
A
1847{
1848 struct mbuf *mopt;
1849 u_char *optbuf;
9bccf70c 1850 u_int32_t v;
1c79356b 1851
39236c6e 1852#define JUMBOOPTLEN 8 /* length of jumbo payload option and padding */
1c79356b
A
1853
1854 /*
1855 * If there is no hop-by-hop options header, allocate new one.
1856 * If there is one but it doesn't have enough space to store the
1857 * jumbo payload option, allocate a cluster to store the whole options.
1858 * Otherwise, use it to store the options.
1859 */
39236c6e 1860 if (exthdrs->ip6e_hbh == NULL) {
1c79356b 1861 MGET(mopt, M_DONTWAIT, MT_DATA);
39236c6e 1862 if (mopt == NULL)
6d2010ae 1863 return (ENOBUFS);
1c79356b
A
1864 mopt->m_len = JUMBOOPTLEN;
1865 optbuf = mtod(mopt, u_char *);
1866 optbuf[1] = 0; /* = ((JUMBOOPTLEN) >> 3) - 1 */
1867 exthdrs->ip6e_hbh = mopt;
1868 } else {
1869 struct ip6_hbh *hbh;
1870
1871 mopt = exthdrs->ip6e_hbh;
1872 if (M_TRAILINGSPACE(mopt) < JUMBOOPTLEN) {
9bccf70c
A
1873 /*
1874 * XXX assumption:
1875 * - exthdrs->ip6e_hbh is not referenced from places
1876 * other than exthdrs.
1877 * - exthdrs->ip6e_hbh is not an mbuf chain.
1878 */
6d2010ae 1879 u_int32_t oldoptlen = mopt->m_len;
9bccf70c 1880 struct mbuf *n;
1c79356b 1881
9bccf70c
A
1882 /*
1883 * XXX: give up if the whole (new) hbh header does
1884 * not fit even in an mbuf cluster.
1885 */
1886 if (oldoptlen + JUMBOOPTLEN > MCLBYTES)
6d2010ae 1887 return (ENOBUFS);
1c79356b 1888
9bccf70c
A
1889 /*
1890 * As a consequence, we must always prepare a cluster
1891 * at this point.
1892 */
1893 MGET(n, M_DONTWAIT, MT_DATA);
39236c6e 1894 if (n != NULL) {
9bccf70c 1895 MCLGET(n, M_DONTWAIT);
39236c6e 1896 if (!(n->m_flags & M_EXT)) {
9bccf70c
A
1897 m_freem(n);
1898 n = NULL;
1899 }
1900 }
39236c6e 1901 if (n == NULL)
6d2010ae 1902 return (ENOBUFS);
9bccf70c
A
1903 n->m_len = oldoptlen + JUMBOOPTLEN;
1904 bcopy(mtod(mopt, caddr_t), mtod(n, caddr_t),
6d2010ae
A
1905 oldoptlen);
1906 optbuf = mtod(n, u_char *) + oldoptlen;
9bccf70c
A
1907 m_freem(mopt);
1908 mopt = exthdrs->ip6e_hbh = n;
1c79356b
A
1909 } else {
1910 optbuf = mtod(mopt, u_char *) + mopt->m_len;
1911 mopt->m_len += JUMBOOPTLEN;
1912 }
1913 optbuf[0] = IP6OPT_PADN;
1914 optbuf[1] = 1;
1915
1916 /*
1917 * Adjust the header length according to the pad and
1918 * the jumbo payload option.
1919 */
1920 hbh = mtod(mopt, struct ip6_hbh *);
1921 hbh->ip6h_len += (JUMBOOPTLEN >> 3);
1922 }
1923
1924 /* fill in the option. */
1925 optbuf[2] = IP6OPT_JUMBO;
1926 optbuf[3] = 4;
9bccf70c 1927 v = (u_int32_t)htonl(plen + JUMBOOPTLEN);
39236c6e 1928 bcopy(&v, &optbuf[4], sizeof (u_int32_t));
1c79356b
A
1929
1930 /* finally, adjust the packet header length */
1931 exthdrs->ip6e_ip6->m_pkthdr.len += JUMBOOPTLEN;
1932
6d2010ae 1933 return (0);
1c79356b
A
1934#undef JUMBOOPTLEN
1935}
1936
1937/*
1938 * Insert fragment header and copy unfragmentable header portions.
1939 */
1940static int
39236c6e
A
1941ip6_insertfraghdr(struct mbuf *m0, struct mbuf *m, int hlen,
1942 struct ip6_frag **frghdrp)
1c79356b
A
1943{
1944 struct mbuf *n, *mlast;
1945
39236c6e
A
1946 if (hlen > sizeof (struct ip6_hdr)) {
1947 n = m_copym(m0, sizeof (struct ip6_hdr),
1948 hlen - sizeof (struct ip6_hdr), M_DONTWAIT);
1949 if (n == NULL)
6d2010ae 1950 return (ENOBUFS);
1c79356b
A
1951 m->m_next = n;
1952 } else
1953 n = m;
1954
1955 /* Search for the last mbuf of unfragmentable part. */
1956 for (mlast = n; mlast->m_next; mlast = mlast->m_next)
1957 ;
1958
39236c6e
A
1959 if (!(mlast->m_flags & M_EXT) &&
1960 M_TRAILINGSPACE(mlast) >= sizeof (struct ip6_frag)) {
1961 /* use the trailing space of the last mbuf for the frag hdr */
6d2010ae
A
1962 *frghdrp = (struct ip6_frag *)(mtod(mlast, caddr_t) +
1963 mlast->m_len);
39236c6e
A
1964 mlast->m_len += sizeof (struct ip6_frag);
1965 m->m_pkthdr.len += sizeof (struct ip6_frag);
1c79356b
A
1966 } else {
1967 /* allocate a new mbuf for the fragment header */
1968 struct mbuf *mfrg;
1969
1970 MGET(mfrg, M_DONTWAIT, MT_DATA);
39236c6e 1971 if (mfrg == NULL)
6d2010ae 1972 return (ENOBUFS);
39236c6e 1973 mfrg->m_len = sizeof (struct ip6_frag);
1c79356b
A
1974 *frghdrp = mtod(mfrg, struct ip6_frag *);
1975 mlast->m_next = mfrg;
1976 }
1977
6d2010ae 1978 return (0);
1c79356b
A
1979}
1980
6d2010ae
A
1981static int
1982ip6_getpmtu(struct route_in6 *ro_pmtu, struct route_in6 *ro,
1983 struct ifnet *ifp, struct in6_addr *dst, u_int32_t *mtup,
39236c6e 1984 boolean_t *alwaysfragp)
6d2010ae
A
1985{
1986 u_int32_t mtu = 0;
39236c6e 1987 boolean_t alwaysfrag = FALSE;
6d2010ae 1988 int error = 0;
55e303ae 1989
6d2010ae
A
1990 if (ro_pmtu != ro) {
1991 /* The first hop and the final destination may differ. */
39236c6e
A
1992 struct sockaddr_in6 *sa6_dst = SIN6(&ro_pmtu->ro_dst);
1993 if (ROUTE_UNUSABLE(ro_pmtu) ||
1994 !IN6_ARE_ADDR_EQUAL(&sa6_dst->sin6_addr, dst))
1995 ROUTE_RELEASE(ro_pmtu);
1996
6d2010ae 1997 if (ro_pmtu->ro_rt == NULL) {
39236c6e 1998 bzero(sa6_dst, sizeof (*sa6_dst));
6d2010ae 1999 sa6_dst->sin6_family = AF_INET6;
39236c6e 2000 sa6_dst->sin6_len = sizeof (struct sockaddr_in6);
6d2010ae
A
2001 sa6_dst->sin6_addr = *dst;
2002
2003 rtalloc_scoped((struct route *)ro_pmtu,
2004 ifp != NULL ? ifp->if_index : IFSCOPE_NONE);
2005 }
2006 }
2007
6d2010ae
A
2008 if (ro_pmtu->ro_rt != NULL) {
2009 u_int32_t ifmtu;
2010
fe8ab488
A
2011 if (ifp == NULL)
2012 ifp = ro_pmtu->ro_rt->rt_ifp;
6d2010ae 2013 lck_rw_lock_shared(nd_if_rwlock);
316670eb 2014 /* Access without acquiring nd_ifinfo lock for performance */
6d2010ae
A
2015 ifmtu = IN6_LINKMTU(ifp);
2016 lck_rw_done(nd_if_rwlock);
2017
39236c6e
A
2018 /*
2019 * Access rmx_mtu without holding the route entry lock,
2020 * for performance; this isn't something that changes
2021 * often, so optimize.
2022 */
6d2010ae
A
2023 mtu = ro_pmtu->ro_rt->rt_rmx.rmx_mtu;
2024 if (mtu > ifmtu || mtu == 0) {
2025 /*
2026 * The MTU on the route is larger than the MTU on
2027 * the interface! This shouldn't happen, unless the
2028 * MTU of the interface has been changed after the
2029 * interface was brought up. Change the MTU in the
2030 * route to match the interface MTU (as long as the
2031 * field isn't locked).
2032 *
2033 * if MTU on the route is 0, we need to fix the MTU.
2034 * this case happens with path MTU discovery timeouts.
2035 */
39236c6e
A
2036 mtu = ifmtu;
2037 if (!(ro_pmtu->ro_rt->rt_rmx.rmx_locks & RTV_MTU))
2038 ro_pmtu->ro_rt->rt_rmx.rmx_mtu = mtu; /* XXX */
2039 } else if (mtu < IPV6_MMTU) {
6d2010ae
A
2040 /*
2041 * RFC2460 section 5, last paragraph:
2042 * if we record ICMPv6 too big message with
2043 * mtu < IPV6_MMTU, transmit packets sized IPV6_MMTU
2044 * or smaller, with framgent header attached.
2045 * (fragment header is needed regardless from the
2046 * packet size, for translators to identify packets)
2047 */
39236c6e 2048 alwaysfrag = TRUE;
6d2010ae 2049 mtu = IPV6_MMTU;
39236c6e 2050 }
6d2010ae
A
2051 } else {
2052 if (ifp) {
2053 lck_rw_lock_shared(nd_if_rwlock);
316670eb 2054 /* Don't hold nd_ifinfo lock for performance */
6d2010ae
A
2055 mtu = IN6_LINKMTU(ifp);
2056 lck_rw_done(nd_if_rwlock);
39236c6e 2057 } else {
6d2010ae 2058 error = EHOSTUNREACH; /* XXX */
39236c6e 2059 }
6d2010ae
A
2060 }
2061
2062 *mtup = mtu;
39236c6e 2063 if (alwaysfragp != NULL)
6d2010ae
A
2064 *alwaysfragp = alwaysfrag;
2065 return (error);
2066}
2067
2068/*
1c79356b
A
2069 * IP6 socket option processing.
2070 */
1c79356b 2071int
39236c6e 2072ip6_ctloutput(struct socket *so, struct sockopt *sopt)
1c79356b 2073{
6d2010ae
A
2074 int optdatalen, uproto;
2075 void *optdata;
9bccf70c
A
2076 int privileged;
2077 struct inpcb *in6p = sotoinpcb(so);
b0d623f7 2078 int error = 0, optval = 0;
2d21ac55
A
2079 int level, op = -1, optname = 0;
2080 int optlen = 0;
b0d623f7 2081 struct proc *p;
1c79356b 2082
39236c6e
A
2083 VERIFY(sopt != NULL);
2084
b0d623f7
A
2085 level = sopt->sopt_level;
2086 op = sopt->sopt_dir;
2087 optname = sopt->sopt_name;
2088 optlen = sopt->sopt_valsize;
2089 p = sopt->sopt_p;
39236c6e 2090 uproto = (int)SOCK_PROTO(so);
1c79356b 2091
b0d623f7 2092 privileged = (proc_suser(p) == 0);
1c79356b
A
2093
2094 if (level == IPPROTO_IPV6) {
2095 switch (op) {
1c79356b 2096 case SOPT_SET:
1c79356b 2097 switch (optname) {
39236c6e 2098 case IPV6_2292PKTOPTIONS: {
1c79356b
A
2099 struct mbuf *m;
2100
39236c6e 2101 error = soopt_getm(sopt, &m);
2d21ac55 2102 if (error != 0)
1c79356b 2103 break;
39236c6e 2104 error = soopt_mcopyin(sopt, m);
2d21ac55 2105 if (error != 0)
1c79356b
A
2106 break;
2107 error = ip6_pcbopts(&in6p->in6p_outputopts,
39236c6e
A
2108 m, so, sopt);
2109 m_freem(m);
1c79356b
A
2110 break;
2111 }
9bccf70c 2112
1c79356b
A
2113 /*
2114 * Use of some Hop-by-Hop options or some
2115 * Destination options, might require special
2116 * privilege. That is, normal applications
2117 * (without special privilege) might be forbidden
2118 * from setting certain options in outgoing packets,
2119 * and might never see certain options in received
2120 * packets. [RFC 2292 Section 6]
2121 * KAME specific note:
2122 * KAME prevents non-privileged users from sending or
2123 * receiving ANY hbh/dst options in order to avoid
2124 * overhead of parsing options in the kernel.
2125 */
6d2010ae
A
2126 case IPV6_RECVHOPOPTS:
2127 case IPV6_RECVDSTOPTS:
2128 case IPV6_RECVRTHDRDSTOPTS:
39236c6e
A
2129 if (!privileged)
2130 break;
6d2010ae 2131 /* FALLTHROUGH */
1c79356b 2132 case IPV6_UNICAST_HOPS:
6d2010ae 2133 case IPV6_HOPLIMIT:
6d2010ae
A
2134 case IPV6_RECVPKTINFO:
2135 case IPV6_RECVHOPLIMIT:
2136 case IPV6_RECVRTHDR:
2137 case IPV6_RECVPATHMTU:
b0d623f7 2138 case IPV6_RECVTCLASS:
9bccf70c 2139 case IPV6_V6ONLY:
6d2010ae 2140 case IPV6_AUTOFLOWLABEL:
39236c6e 2141 if (optlen != sizeof (int)) {
1c79356b 2142 error = EINVAL;
9bccf70c
A
2143 break;
2144 }
2145 error = sooptcopyin(sopt, &optval,
39236c6e 2146 sizeof (optval), sizeof (optval));
9bccf70c
A
2147 if (error)
2148 break;
1c79356b 2149
39236c6e 2150 switch (optname) {
9bccf70c 2151 case IPV6_UNICAST_HOPS:
39236c6e 2152 if (optval < -1 || optval >= 256) {
9bccf70c 2153 error = EINVAL;
39236c6e 2154 } else {
9bccf70c
A
2155 /* -1 = kernel default */
2156 in6p->in6p_hops = optval;
39236c6e
A
2157 if (in6p->inp_vflag &
2158 INP_IPV4) {
2159 in6p->inp_ip_ttl =
2160 optval;
2161 }
9bccf70c
A
2162 }
2163 break;
39236c6e
A
2164#define OPTSET(bit) do { \
2165 if (optval) \
2166 in6p->inp_flags |= (bit); \
2167 else \
2168 in6p->inp_flags &= ~(bit); \
2169} while (0)
2170
2171#define OPTSET2292(bit) do { \
2172 in6p->inp_flags |= IN6P_RFC2292; \
2173 if (optval) \
2174 in6p->inp_flags |= (bit); \
2175 else \
2176 in6p->inp_flags &= ~(bit); \
2177} while (0)
2178
2179#define OPTBIT(bit) (in6p->inp_flags & (bit) ? 1 : 0)
6d2010ae
A
2180
2181 case IPV6_RECVPKTINFO:
2182 /* cannot mix with RFC2292 */
2183 if (OPTBIT(IN6P_RFC2292)) {
2184 error = EINVAL;
2185 break;
2186 }
2187 OPTSET(IN6P_PKTINFO);
2188 break;
2189
39236c6e 2190 case IPV6_HOPLIMIT: {
6d2010ae
A
2191 struct ip6_pktopts **optp;
2192
2193 /* cannot mix with RFC2292 */
2194 if (OPTBIT(IN6P_RFC2292)) {
2195 error = EINVAL;
2196 break;
2197 }
2198 optp = &in6p->in6p_outputopts;
2199 error = ip6_pcbopt(IPV6_HOPLIMIT,
39236c6e 2200 (u_char *)&optval, sizeof (optval),
6d2010ae
A
2201 optp, uproto);
2202 break;
2203 }
2204
2205 case IPV6_RECVHOPLIMIT:
2206 /* cannot mix with RFC2292 */
2207 if (OPTBIT(IN6P_RFC2292)) {
2208 error = EINVAL;
2209 break;
2210 }
2211 OPTSET(IN6P_HOPLIMIT);
2212 break;
2213
2214 case IPV6_RECVHOPOPTS:
2215 /* cannot mix with RFC2292 */
2216 if (OPTBIT(IN6P_RFC2292)) {
2217 error = EINVAL;
2218 break;
2219 }
2220 OPTSET(IN6P_HOPOPTS);
2221 break;
2222
2223 case IPV6_RECVDSTOPTS:
2224 /* cannot mix with RFC2292 */
2225 if (OPTBIT(IN6P_RFC2292)) {
2226 error = EINVAL;
2227 break;
2228 }
2229 OPTSET(IN6P_DSTOPTS);
2230 break;
1c79356b 2231
6d2010ae
A
2232 case IPV6_RECVRTHDRDSTOPTS:
2233 /* cannot mix with RFC2292 */
2234 if (OPTBIT(IN6P_RFC2292)) {
2235 error = EINVAL;
2236 break;
2237 }
2238 OPTSET(IN6P_RTHDRDSTOPTS);
2239 break;
2240
2241 case IPV6_RECVRTHDR:
2242 /* cannot mix with RFC2292 */
2243 if (OPTBIT(IN6P_RFC2292)) {
2244 error = EINVAL;
2245 break;
2246 }
2247 OPTSET(IN6P_RTHDR);
9bccf70c 2248 break;
1c79356b 2249
6d2010ae
A
2250 case IPV6_RECVPATHMTU:
2251 /*
2252 * We ignore this option for TCP
2253 * sockets.
2254 * (RFC3542 leaves this case
2255 * unspecified.)
2256 */
2257 if (uproto != IPPROTO_TCP)
2258 OPTSET(IN6P_MTU);
9bccf70c 2259 break;
1c79356b 2260
9bccf70c
A
2261 case IPV6_V6ONLY:
2262 /*
2263 * make setsockopt(IPV6_V6ONLY)
2264 * available only prior to bind(2).
2265 * see ipng mailing list, Jun 22 2001.
2266 */
6d2010ae 2267 if (in6p->inp_lport ||
39236c6e
A
2268 !IN6_IS_ADDR_UNSPECIFIED(
2269 &in6p->in6p_laddr)) {
9bccf70c 2270 error = EINVAL;
1c79356b 2271 break;
1c79356b 2272 }
9bccf70c 2273 OPTSET(IN6P_IPV6_V6ONLY);
55e303ae 2274 if (optval)
6d2010ae 2275 in6p->inp_vflag &= ~INP_IPV4;
55e303ae 2276 else
6d2010ae 2277 in6p->inp_vflag |= INP_IPV4;
9bccf70c 2278 break;
39236c6e 2279
b0d623f7 2280 case IPV6_RECVTCLASS:
6d2010ae 2281 /* we can mix with RFC2292 */
b0d623f7
A
2282 OPTSET(IN6P_TCLASS);
2283 break;
39236c6e 2284
6d2010ae
A
2285 case IPV6_AUTOFLOWLABEL:
2286 OPTSET(IN6P_AUTOFLOWLABEL);
2287 break;
2288
1c79356b
A
2289 }
2290 break;
9bccf70c 2291
6d2010ae
A
2292 case IPV6_TCLASS:
2293 case IPV6_DONTFRAG:
2294 case IPV6_USE_MIN_MTU:
39236c6e
A
2295 case IPV6_PREFER_TEMPADDR: {
2296 struct ip6_pktopts **optp;
2297
2298 if (optlen != sizeof (optval)) {
6d2010ae
A
2299 error = EINVAL;
2300 break;
2301 }
2302 error = sooptcopyin(sopt, &optval,
39236c6e 2303 sizeof (optval), sizeof (optval));
6d2010ae
A
2304 if (error)
2305 break;
39236c6e
A
2306
2307 optp = &in6p->in6p_outputopts;
2308 error = ip6_pcbopt(optname, (u_char *)&optval,
2309 sizeof (optval), optp, uproto);
2310 break;
2311 }
6d2010ae
A
2312
2313 case IPV6_2292PKTINFO:
2314 case IPV6_2292HOPLIMIT:
2315 case IPV6_2292HOPOPTS:
2316 case IPV6_2292DSTOPTS:
2317 case IPV6_2292RTHDR:
9bccf70c 2318 /* RFC 2292 */
39236c6e 2319 if (optlen != sizeof (int)) {
9bccf70c
A
2320 error = EINVAL;
2321 break;
2322 }
2323 error = sooptcopyin(sopt, &optval,
39236c6e 2324 sizeof (optval), sizeof (optval));
9bccf70c
A
2325 if (error)
2326 break;
2327 switch (optname) {
6d2010ae
A
2328 case IPV6_2292PKTINFO:
2329 OPTSET2292(IN6P_PKTINFO);
9bccf70c 2330 break;
6d2010ae
A
2331 case IPV6_2292HOPLIMIT:
2332 OPTSET2292(IN6P_HOPLIMIT);
9bccf70c 2333 break;
6d2010ae 2334 case IPV6_2292HOPOPTS:
9bccf70c
A
2335 /*
2336 * Check super-user privilege.
2337 * See comments for IPV6_RECVHOPOPTS.
2338 */
2339 if (!privileged)
39236c6e 2340 return (EPERM);
6d2010ae 2341 OPTSET2292(IN6P_HOPOPTS);
9bccf70c 2342 break;
6d2010ae 2343 case IPV6_2292DSTOPTS:
9bccf70c 2344 if (!privileged)
39236c6e
A
2345 return (EPERM);
2346 OPTSET2292(IN6P_DSTOPTS|
2347 IN6P_RTHDRDSTOPTS); /* XXX */
9bccf70c 2348 break;
6d2010ae
A
2349 case IPV6_2292RTHDR:
2350 OPTSET2292(IN6P_RTHDR);
1c79356b 2351 break;
1c79356b
A
2352 }
2353 break;
39236c6e 2354
6d2010ae
A
2355 case IPV6_3542PKTINFO:
2356 case IPV6_3542HOPOPTS:
2357 case IPV6_3542RTHDR:
2358 case IPV6_3542DSTOPTS:
2359 case IPV6_RTHDRDSTOPTS:
39236c6e 2360 case IPV6_3542NEXTHOP: {
316670eb 2361 struct ip6_pktopts **optp;
6d2010ae
A
2362 /* new advanced API (RFC3542) */
2363 struct mbuf *m;
1c79356b 2364
6d2010ae
A
2365 /* cannot mix with RFC2292 */
2366 if (OPTBIT(IN6P_RFC2292)) {
b0d623f7
A
2367 error = EINVAL;
2368 break;
2369 }
6d2010ae
A
2370 error = soopt_getm(sopt, &m);
2371 if (error != 0)
2372 break;
2373 error = soopt_mcopyin(sopt, m);
39236c6e 2374 if (error != 0)
b0d623f7 2375 break;
39236c6e 2376
316670eb 2377 optp = &in6p->in6p_outputopts;
39236c6e
A
2378 error = ip6_pcbopt(optname, mtod(m, u_char *),
2379 m->m_len, optp, uproto);
6d2010ae 2380 m_freem(m);
b0d623f7 2381 break;
6d2010ae
A
2382 }
2383#undef OPTSET
1c79356b
A
2384 case IPV6_MULTICAST_IF:
2385 case IPV6_MULTICAST_HOPS:
2386 case IPV6_MULTICAST_LOOP:
2387 case IPV6_JOIN_GROUP:
2388 case IPV6_LEAVE_GROUP:
6d2010ae
A
2389 case IPV6_MSFILTER:
2390 case MCAST_BLOCK_SOURCE:
2391 case MCAST_UNBLOCK_SOURCE:
2392 case MCAST_JOIN_GROUP:
2393 case MCAST_LEAVE_GROUP:
2394 case MCAST_JOIN_SOURCE_GROUP:
2395 case MCAST_LEAVE_SOURCE_GROUP:
2396 error = ip6_setmoptions(in6p, sopt);
1c79356b
A
2397 break;
2398
9bccf70c
A
2399 case IPV6_PORTRANGE:
2400 error = sooptcopyin(sopt, &optval,
39236c6e 2401 sizeof (optval), sizeof (optval));
9bccf70c
A
2402 if (error)
2403 break;
1c79356b 2404
9bccf70c
A
2405 switch (optval) {
2406 case IPV6_PORTRANGE_DEFAULT:
6d2010ae
A
2407 in6p->inp_flags &= ~(INP_LOWPORT);
2408 in6p->inp_flags &= ~(INP_HIGHPORT);
9bccf70c 2409 break;
1c79356b 2410
9bccf70c 2411 case IPV6_PORTRANGE_HIGH:
6d2010ae
A
2412 in6p->inp_flags &= ~(INP_LOWPORT);
2413 in6p->inp_flags |= INP_HIGHPORT;
9bccf70c 2414 break;
1c79356b 2415
9bccf70c 2416 case IPV6_PORTRANGE_LOW:
6d2010ae
A
2417 in6p->inp_flags &= ~(INP_HIGHPORT);
2418 in6p->inp_flags |= INP_LOWPORT;
9bccf70c 2419 break;
1c79356b 2420
9bccf70c
A
2421 default:
2422 error = EINVAL;
2423 break;
2424 }
1c79356b 2425 break;
1c79356b 2426#if IPSEC
39236c6e 2427 case IPV6_IPSEC_POLICY: {
1c79356b
A
2428 caddr_t req = NULL;
2429 size_t len = 0;
1c79356b 2430 struct mbuf *m;
fe8ab488 2431
39236c6e 2432 if ((error = soopt_getm(sopt, &m)) != 0)
1c79356b 2433 break;
39236c6e 2434 if ((error = soopt_mcopyin(sopt, m)) != 0)
1c79356b 2435 break;
fe8ab488 2436
39236c6e
A
2437 req = mtod(m, caddr_t);
2438 len = m->m_len;
1c79356b 2439 error = ipsec6_set_policy(in6p, optname, req,
fe8ab488 2440 len, privileged);
1c79356b 2441 m_freem(m);
1c79356b 2442 break;
39236c6e
A
2443 }
2444#endif /* IPSEC */
2d21ac55 2445#if IPFIREWALL
1c79356b
A
2446 case IPV6_FW_ADD:
2447 case IPV6_FW_DEL:
2448 case IPV6_FW_FLUSH:
39236c6e 2449 case IPV6_FW_ZERO: {
060df5ea
A
2450 if (ip6_fw_ctl_ptr == NULL)
2451 load_ip6fw();
2452 if (ip6_fw_ctl_ptr != NULL)
2453 error = (*ip6_fw_ctl_ptr)(sopt);
2454 else
39236c6e 2455 error = ENOPROTOOPT;
1c79356b 2456 break;
39236c6e 2457 }
2d21ac55 2458#endif /* IPFIREWALL */
6d2010ae
A
2459 /*
2460 * IPv6 variant of IP_BOUND_IF; for details see
2461 * comments on IP_BOUND_IF in ip_ctloutput().
2462 */
2463 case IPV6_BOUND_IF:
2464 /* This option is settable only on IPv6 */
2465 if (!(in6p->inp_vflag & INP_IPV6)) {
2466 error = EINVAL;
2467 break;
2468 }
2469
2470 error = sooptcopyin(sopt, &optval,
2471 sizeof (optval), sizeof (optval));
2472
2473 if (error)
2474 break;
2475
39236c6e 2476 error = inp_bindif(in6p, optval, NULL);
6d2010ae
A
2477 break;
2478
2479 case IPV6_NO_IFT_CELLULAR:
2480 /* This option is settable only for IPv6 */
2481 if (!(in6p->inp_vflag & INP_IPV6)) {
2482 error = EINVAL;
2483 break;
2484 }
2485
2486 error = sooptcopyin(sopt, &optval,
2487 sizeof (optval), sizeof (optval));
2488
2489 if (error)
2490 break;
2491
39236c6e 2492 /* once set, it cannot be unset */
fe8ab488 2493 if (!optval && INP_NO_CELLULAR(in6p)) {
39236c6e
A
2494 error = EINVAL;
2495 break;
2496 }
2497
2498 error = so_set_restrictions(so,
2499 SO_RESTRICT_DENY_CELLULAR);
6d2010ae
A
2500 break;
2501
2502 case IPV6_OUT_IF:
2503 /* This option is not settable */
2504 error = EINVAL;
2505 break;
2506
1c79356b
A
2507 default:
2508 error = ENOPROTOOPT;
2509 break;
2510 }
1c79356b
A
2511 break;
2512
1c79356b 2513 case SOPT_GET:
1c79356b
A
2514 switch (optname) {
2515
6d2010ae
A
2516 case IPV6_2292PKTOPTIONS:
2517 /*
2518 * RFC3542 (effectively) deprecated the
2519 * semantics of the 2292-style pktoptions.
2520 * Since it was not reliable in nature (i.e.,
2521 * applications had to expect the lack of some
2522 * information after all), it would make sense
2523 * to simplify this part by always returning
2524 * empty data.
2525 */
2526 sopt->sopt_valsize = 0;
1c79356b
A
2527 break;
2528
6d2010ae
A
2529 case IPV6_RECVHOPOPTS:
2530 case IPV6_RECVDSTOPTS:
2531 case IPV6_RECVRTHDRDSTOPTS:
1c79356b 2532 case IPV6_UNICAST_HOPS:
6d2010ae
A
2533 case IPV6_RECVPKTINFO:
2534 case IPV6_RECVHOPLIMIT:
2535 case IPV6_RECVRTHDR:
2536 case IPV6_RECVPATHMTU:
9bccf70c 2537 case IPV6_V6ONLY:
1c79356b 2538 case IPV6_PORTRANGE:
b0d623f7 2539 case IPV6_RECVTCLASS:
6d2010ae 2540 case IPV6_AUTOFLOWLABEL:
1c79356b
A
2541 switch (optname) {
2542
6d2010ae
A
2543 case IPV6_RECVHOPOPTS:
2544 optval = OPTBIT(IN6P_HOPOPTS);
2545 break;
2546
2547 case IPV6_RECVDSTOPTS:
2548 optval = OPTBIT(IN6P_DSTOPTS);
2549 break;
2550
2551 case IPV6_RECVRTHDRDSTOPTS:
2552 optval = OPTBIT(IN6P_RTHDRDSTOPTS);
2553 break;
2554
1c79356b 2555 case IPV6_UNICAST_HOPS:
1c79356b 2556 optval = in6p->in6p_hops;
1c79356b
A
2557 break;
2558
6d2010ae
A
2559 case IPV6_RECVPKTINFO:
2560 optval = OPTBIT(IN6P_PKTINFO);
2561 break;
2562
2563 case IPV6_RECVHOPLIMIT:
2564 optval = OPTBIT(IN6P_HOPLIMIT);
2565 break;
2566
2567 case IPV6_RECVRTHDR:
2568 optval = OPTBIT(IN6P_RTHDR);
2569 break;
2570
2571 case IPV6_RECVPATHMTU:
2572 optval = OPTBIT(IN6P_MTU);
1c79356b
A
2573 break;
2574
9bccf70c 2575 case IPV6_V6ONLY:
55e303ae 2576 optval = OPTBIT(IN6P_IPV6_V6ONLY);
1c79356b 2577 break;
1c79356b 2578
39236c6e 2579 case IPV6_PORTRANGE: {
1c79356b 2580 int flags;
6d2010ae
A
2581 flags = in6p->inp_flags;
2582 if (flags & INP_HIGHPORT)
1c79356b 2583 optval = IPV6_PORTRANGE_HIGH;
6d2010ae 2584 else if (flags & INP_LOWPORT)
1c79356b
A
2585 optval = IPV6_PORTRANGE_LOW;
2586 else
2587 optval = 0;
2588 break;
39236c6e 2589 }
b0d623f7
A
2590 case IPV6_RECVTCLASS:
2591 optval = OPTBIT(IN6P_TCLASS);
2592 break;
2593
6d2010ae
A
2594 case IPV6_AUTOFLOWLABEL:
2595 optval = OPTBIT(IN6P_AUTOFLOWLABEL);
2596 break;
1c79356b 2597 }
6d2010ae
A
2598 if (error)
2599 break;
1c79356b 2600 error = sooptcopyout(sopt, &optval,
39236c6e 2601 sizeof (optval));
1c79356b
A
2602 break;
2603
39236c6e 2604 case IPV6_PATHMTU: {
6d2010ae
A
2605 u_int32_t pmtu = 0;
2606 struct ip6_mtuinfo mtuinfo;
2607 struct route_in6 sro;
2608
39236c6e 2609 bzero(&sro, sizeof (sro));
6d2010ae
A
2610
2611 if (!(so->so_state & SS_ISCONNECTED))
2612 return (ENOTCONN);
2613 /*
2614 * XXX: we dot not consider the case of source
2615 * routing, or optional information to specify
2616 * the outgoing interface.
2617 */
2618 error = ip6_getpmtu(&sro, NULL, NULL,
2619 &in6p->in6p_faddr, &pmtu, NULL);
39236c6e 2620 ROUTE_RELEASE(&sro);
6d2010ae
A
2621 if (error)
2622 break;
2623 if (pmtu > IPV6_MAXPACKET)
2624 pmtu = IPV6_MAXPACKET;
2625
39236c6e 2626 bzero(&mtuinfo, sizeof (mtuinfo));
6d2010ae
A
2627 mtuinfo.ip6m_mtu = (u_int32_t)pmtu;
2628 optdata = (void *)&mtuinfo;
39236c6e 2629 optdatalen = sizeof (mtuinfo);
6d2010ae
A
2630 error = sooptcopyout(sopt, optdata,
2631 optdatalen);
2632 break;
2633 }
2634
2635 case IPV6_2292PKTINFO:
2636 case IPV6_2292HOPLIMIT:
2637 case IPV6_2292HOPOPTS:
2638 case IPV6_2292RTHDR:
2639 case IPV6_2292DSTOPTS:
9bccf70c 2640 switch (optname) {
6d2010ae 2641 case IPV6_2292PKTINFO:
9bccf70c 2642 optval = OPTBIT(IN6P_PKTINFO);
1c79356b 2643 break;
6d2010ae 2644 case IPV6_2292HOPLIMIT:
1c79356b
A
2645 optval = OPTBIT(IN6P_HOPLIMIT);
2646 break;
6d2010ae 2647 case IPV6_2292HOPOPTS:
9bccf70c 2648 optval = OPTBIT(IN6P_HOPOPTS);
1c79356b 2649 break;
6d2010ae 2650 case IPV6_2292RTHDR:
9bccf70c 2651 optval = OPTBIT(IN6P_RTHDR);
1c79356b 2652 break;
6d2010ae 2653 case IPV6_2292DSTOPTS:
39236c6e
A
2654 optval = OPTBIT(IN6P_DSTOPTS|
2655 IN6P_RTHDRDSTOPTS);
1c79356b 2656 break;
1c79356b 2657 }
1c79356b 2658 error = sooptcopyout(sopt, &optval,
39236c6e 2659 sizeof (optval));
1c79356b 2660 break;
39236c6e 2661
6d2010ae
A
2662 case IPV6_PKTINFO:
2663 case IPV6_HOPOPTS:
2664 case IPV6_RTHDR:
2665 case IPV6_DSTOPTS:
2666 case IPV6_RTHDRDSTOPTS:
2667 case IPV6_NEXTHOP:
b0d623f7 2668 case IPV6_TCLASS:
6d2010ae
A
2669 case IPV6_DONTFRAG:
2670 case IPV6_USE_MIN_MTU:
2671 case IPV6_PREFER_TEMPADDR:
2672 error = ip6_getpcbopt(in6p->in6p_outputopts,
2673 optname, sopt);
b0d623f7
A
2674 break;
2675
1c79356b
A
2676 case IPV6_MULTICAST_IF:
2677 case IPV6_MULTICAST_HOPS:
2678 case IPV6_MULTICAST_LOOP:
6d2010ae
A
2679 case IPV6_MSFILTER:
2680 error = ip6_getmoptions(in6p, sopt);
1c79356b 2681 break;
1c79356b 2682#if IPSEC
39236c6e 2683 case IPV6_IPSEC_POLICY: {
fe8ab488 2684 error = 0; /* This option is no longer supported */
1c79356b 2685 break;
39236c6e
A
2686 }
2687#endif /* IPSEC */
2d21ac55 2688#if IPFIREWALL
39236c6e 2689 case IPV6_FW_GET: {
060df5ea
A
2690 if (ip6_fw_ctl_ptr == NULL)
2691 load_ip6fw();
2692 if (ip6_fw_ctl_ptr != NULL)
2693 error = (*ip6_fw_ctl_ptr)(sopt);
2694 else
39236c6e 2695 error = ENOPROTOOPT;
1c79356b 2696 break;
39236c6e 2697 }
2d21ac55 2698#endif /* IPFIREWALL */
6d2010ae
A
2699 case IPV6_BOUND_IF:
2700 if (in6p->inp_flags & INP_BOUND_IF)
316670eb 2701 optval = in6p->inp_boundifp->if_index;
6d2010ae
A
2702 error = sooptcopyout(sopt, &optval,
2703 sizeof (optval));
2704 break;
2705
2706 case IPV6_NO_IFT_CELLULAR:
fe8ab488 2707 optval = INP_NO_CELLULAR(in6p) ? 1 : 0;
6d2010ae
A
2708 error = sooptcopyout(sopt, &optval,
2709 sizeof (optval));
2710 break;
2711
2712 case IPV6_OUT_IF:
316670eb
A
2713 optval = (in6p->in6p_last_outifp != NULL) ?
2714 in6p->in6p_last_outifp->if_index : 0;
6d2010ae
A
2715 error = sooptcopyout(sopt, &optval,
2716 sizeof (optval));
2717 break;
2718
1c79356b
A
2719 default:
2720 error = ENOPROTOOPT;
2721 break;
2722 }
2723 break;
2724 }
2725 } else {
2726 error = EINVAL;
1c79356b 2727 }
39236c6e 2728 return (error);
1c79356b
A
2729}
2730
6d2010ae
A
2731int
2732ip6_raw_ctloutput(struct socket *so, struct sockopt *sopt)
1c79356b 2733{
6d2010ae
A
2734 int error = 0, optval, optlen;
2735 const int icmp6off = offsetof(struct icmp6_hdr, icmp6_cksum);
2736 struct inpcb *in6p = sotoinpcb(so);
2737 int level, op, optname;
1c79356b 2738
6d2010ae
A
2739 level = sopt->sopt_level;
2740 op = sopt->sopt_dir;
2741 optname = sopt->sopt_name;
2742 optlen = sopt->sopt_valsize;
2743
39236c6e 2744 if (level != IPPROTO_IPV6)
6d2010ae 2745 return (EINVAL);
6d2010ae
A
2746
2747 switch (optname) {
2748 case IPV6_CHECKSUM:
2749 /*
2750 * For ICMPv6 sockets, no modification allowed for checksum
2751 * offset, permit "no change" values to help existing apps.
2752 *
2753 * RFC3542 says: "An attempt to set IPV6_CHECKSUM
2754 * for an ICMPv6 socket will fail."
2755 * The current behavior does not meet RFC3542.
2756 */
2757 switch (op) {
2758 case SOPT_SET:
39236c6e 2759 if (optlen != sizeof (int)) {
6d2010ae
A
2760 error = EINVAL;
2761 break;
2762 }
39236c6e
A
2763 error = sooptcopyin(sopt, &optval, sizeof (optval),
2764 sizeof (optval));
6d2010ae
A
2765 if (error)
2766 break;
2767 if ((optval % 2) != 0) {
2768 /* the API assumes even offset values */
2769 error = EINVAL;
39236c6e 2770 } else if (SOCK_PROTO(so) == IPPROTO_ICMPV6) {
6d2010ae
A
2771 if (optval != icmp6off)
2772 error = EINVAL;
39236c6e 2773 } else {
6d2010ae 2774 in6p->in6p_cksum = optval;
39236c6e 2775 }
6d2010ae
A
2776 break;
2777
2778 case SOPT_GET:
39236c6e 2779 if (SOCK_PROTO(so) == IPPROTO_ICMPV6)
6d2010ae
A
2780 optval = icmp6off;
2781 else
2782 optval = in6p->in6p_cksum;
2783
39236c6e 2784 error = sooptcopyout(sopt, &optval, sizeof (optval));
6d2010ae
A
2785 break;
2786
2787 default:
2788 error = EINVAL;
2789 break;
2790 }
2791 break;
2792
2793 default:
2794 error = ENOPROTOOPT;
2795 break;
2796 }
2797
2798 return (error);
2799}
2800
2801/*
2802 * Set up IP6 options in pcb for insertion in output packets or
2803 * specifying behavior of outgoing packets.
2804 */
2805static int
39236c6e
A
2806ip6_pcbopts(struct ip6_pktopts **pktopt, struct mbuf *m, struct socket *so,
2807 struct sockopt *sopt)
6d2010ae 2808{
39236c6e 2809#pragma unused(sopt)
6d2010ae
A
2810 struct ip6_pktopts *opt = *pktopt;
2811 int error = 0;
2812
2813 /* turn off any old options. */
39236c6e 2814 if (opt != NULL) {
6d2010ae
A
2815#if DIAGNOSTIC
2816 if (opt->ip6po_pktinfo || opt->ip6po_nexthop ||
2817 opt->ip6po_hbh || opt->ip6po_dest1 || opt->ip6po_dest2 ||
2818 opt->ip6po_rhinfo.ip6po_rhi_rthdr)
39236c6e
A
2819 printf("%s: all specified options are cleared.\n",
2820 __func__);
6d2010ae
A
2821#endif
2822 ip6_clearpktopts(opt, -1);
2823 } else {
39236c6e 2824 opt = _MALLOC(sizeof (*opt), M_IP6OPT, M_WAITOK);
6d2010ae 2825 if (opt == NULL)
39236c6e 2826 return (ENOBUFS);
6d2010ae 2827 }
1c79356b
A
2828 *pktopt = NULL;
2829
39236c6e 2830 if (m == NULL || m->m_len == 0) {
1c79356b 2831 /*
55e303ae
A
2832 * Only turning off any previous options, regardless of
2833 * whether the opt is just created or given.
1c79356b 2834 */
39236c6e 2835 if (opt != NULL)
9bccf70c 2836 FREE(opt, M_IP6OPT);
39236c6e 2837 return (0);
1c79356b
A
2838 }
2839
2840 /* set options specified by user. */
39236c6e 2841 if ((error = ip6_setpktopts(m, opt, NULL, SOCK_PROTO(so))) != 0) {
6d2010ae 2842 ip6_clearpktopts(opt, -1); /* XXX: discard all options */
55e303ae 2843 FREE(opt, M_IP6OPT);
39236c6e 2844 return (error);
1c79356b
A
2845 }
2846 *pktopt = opt;
39236c6e 2847 return (0);
1c79356b
A
2848}
2849
6d2010ae
A
2850/*
2851 * initialize ip6_pktopts. beware that there are non-zero default values in
2852 * the struct.
2853 */
2854void
2855ip6_initpktopts(struct ip6_pktopts *opt)
2856{
2857
39236c6e 2858 bzero(opt, sizeof (*opt));
6d2010ae
A
2859 opt->ip6po_hlim = -1; /* -1 means default hop limit */
2860 opt->ip6po_tclass = -1; /* -1 means default traffic class */
2861 opt->ip6po_minmtu = IP6PO_MINMTU_MCASTONLY;
2862 opt->ip6po_prefer_tempaddr = IP6PO_TEMPADDR_SYSTEM;
2863}
2864
b0d623f7 2865static int
316670eb
A
2866ip6_pcbopt(int optname, u_char *buf, int len, struct ip6_pktopts **pktopt,
2867 int uproto)
b0d623f7
A
2868{
2869 struct ip6_pktopts *opt;
2870
2871 opt = *pktopt;
2872 if (opt == NULL) {
39236c6e 2873 opt = _MALLOC(sizeof (*opt), M_IP6OPT, M_WAITOK);
6d2010ae 2874 if (opt == NULL)
39236c6e 2875 return (ENOBUFS);
b0d623f7
A
2876 ip6_initpktopts(opt);
2877 *pktopt = opt;
2878 }
2879
6d2010ae 2880 return (ip6_setpktopt(optname, buf, len, opt, 1, 0, uproto));
b0d623f7
A
2881}
2882
2883static int
2884ip6_getpcbopt(struct ip6_pktopts *pktopt, int optname, struct sockopt *sopt)
2885{
2886 void *optdata = NULL;
2887 int optdatalen = 0;
6d2010ae 2888 struct ip6_ext *ip6e;
6d2010ae
A
2889 struct in6_pktinfo null_pktinfo;
2890 int deftclass = 0, on;
2891 int defminmtu = IP6PO_MINMTU_MCASTONLY;
2892 int defpreftemp = IP6PO_TEMPADDR_SYSTEM;
b0d623f7 2893
316670eb 2894
b0d623f7 2895 switch (optname) {
6d2010ae
A
2896 case IPV6_PKTINFO:
2897 if (pktopt && pktopt->ip6po_pktinfo)
2898 optdata = (void *)pktopt->ip6po_pktinfo;
2899 else {
2900 /* XXX: we don't have to do this every time... */
39236c6e 2901 bzero(&null_pktinfo, sizeof (null_pktinfo));
6d2010ae
A
2902 optdata = (void *)&null_pktinfo;
2903 }
39236c6e 2904 optdatalen = sizeof (struct in6_pktinfo);
6d2010ae 2905 break;
39236c6e 2906
b0d623f7
A
2907 case IPV6_TCLASS:
2908 if (pktopt && pktopt->ip6po_tclass >= 0)
6d2010ae
A
2909 optdata = (void *)&pktopt->ip6po_tclass;
2910 else
2911 optdata = (void *)&deftclass;
39236c6e 2912 optdatalen = sizeof (int);
6d2010ae 2913 break;
39236c6e 2914
6d2010ae
A
2915 case IPV6_HOPOPTS:
2916 if (pktopt && pktopt->ip6po_hbh) {
2917 optdata = (void *)pktopt->ip6po_hbh;
2918 ip6e = (struct ip6_ext *)pktopt->ip6po_hbh;
2919 optdatalen = (ip6e->ip6e_len + 1) << 3;
2920 }
2921 break;
39236c6e 2922
6d2010ae
A
2923 case IPV6_RTHDR:
2924 if (pktopt && pktopt->ip6po_rthdr) {
2925 optdata = (void *)pktopt->ip6po_rthdr;
2926 ip6e = (struct ip6_ext *)pktopt->ip6po_rthdr;
2927 optdatalen = (ip6e->ip6e_len + 1) << 3;
2928 }
2929 break;
39236c6e 2930
6d2010ae
A
2931 case IPV6_RTHDRDSTOPTS:
2932 if (pktopt && pktopt->ip6po_dest1) {
2933 optdata = (void *)pktopt->ip6po_dest1;
2934 ip6e = (struct ip6_ext *)pktopt->ip6po_dest1;
2935 optdatalen = (ip6e->ip6e_len + 1) << 3;
2936 }
2937 break;
39236c6e 2938
6d2010ae
A
2939 case IPV6_DSTOPTS:
2940 if (pktopt && pktopt->ip6po_dest2) {
2941 optdata = (void *)pktopt->ip6po_dest2;
2942 ip6e = (struct ip6_ext *)pktopt->ip6po_dest2;
2943 optdatalen = (ip6e->ip6e_len + 1) << 3;
2944 }
2945 break;
39236c6e 2946
6d2010ae
A
2947 case IPV6_NEXTHOP:
2948 if (pktopt && pktopt->ip6po_nexthop) {
2949 optdata = (void *)pktopt->ip6po_nexthop;
2950 optdatalen = pktopt->ip6po_nexthop->sa_len;
2951 }
2952 break;
39236c6e 2953
6d2010ae
A
2954 case IPV6_USE_MIN_MTU:
2955 if (pktopt)
2956 optdata = (void *)&pktopt->ip6po_minmtu;
b0d623f7 2957 else
6d2010ae 2958 optdata = (void *)&defminmtu;
39236c6e 2959 optdatalen = sizeof (int);
6d2010ae 2960 break;
39236c6e 2961
6d2010ae
A
2962 case IPV6_DONTFRAG:
2963 if (pktopt && ((pktopt->ip6po_flags) & IP6PO_DONTFRAG))
2964 on = 1;
2965 else
2966 on = 0;
2967 optdata = (void *)&on;
39236c6e 2968 optdatalen = sizeof (on);
6d2010ae 2969 break;
39236c6e 2970
6d2010ae
A
2971 case IPV6_PREFER_TEMPADDR:
2972 if (pktopt)
2973 optdata = (void *)&pktopt->ip6po_prefer_tempaddr;
2974 else
2975 optdata = (void *)&defpreftemp;
39236c6e 2976 optdatalen = sizeof (int);
b0d623f7 2977 break;
39236c6e 2978
b0d623f7
A
2979 default: /* should not happen */
2980#ifdef DIAGNOSTIC
2981 panic("ip6_getpcbopt: unexpected option\n");
2982#endif
2983 return (ENOPROTOOPT);
2984 }
2985
39236c6e 2986 return (sooptcopyout(sopt, optdata, optdatalen));
1c79356b
A
2987}
2988
2989void
316670eb 2990ip6_clearpktopts(struct ip6_pktopts *pktopt, int optname)
1c79356b
A
2991{
2992 if (pktopt == NULL)
2993 return;
2994
6d2010ae
A
2995 if (optname == -1 || optname == IPV6_PKTINFO) {
2996 if (pktopt->ip6po_pktinfo)
9bccf70c 2997 FREE(pktopt->ip6po_pktinfo, M_IP6OPT);
1c79356b
A
2998 pktopt->ip6po_pktinfo = NULL;
2999 }
6d2010ae 3000 if (optname == -1 || optname == IPV6_HOPLIMIT)
1c79356b 3001 pktopt->ip6po_hlim = -1;
6d2010ae 3002 if (optname == -1 || optname == IPV6_TCLASS)
b0d623f7 3003 pktopt->ip6po_tclass = -1;
6d2010ae 3004 if (optname == -1 || optname == IPV6_NEXTHOP) {
39236c6e 3005 ROUTE_RELEASE(&pktopt->ip6po_nextroute);
6d2010ae 3006 if (pktopt->ip6po_nexthop)
9bccf70c 3007 FREE(pktopt->ip6po_nexthop, M_IP6OPT);
1c79356b
A
3008 pktopt->ip6po_nexthop = NULL;
3009 }
6d2010ae
A
3010 if (optname == -1 || optname == IPV6_HOPOPTS) {
3011 if (pktopt->ip6po_hbh)
9bccf70c 3012 FREE(pktopt->ip6po_hbh, M_IP6OPT);
1c79356b
A
3013 pktopt->ip6po_hbh = NULL;
3014 }
6d2010ae
A
3015 if (optname == -1 || optname == IPV6_RTHDRDSTOPTS) {
3016 if (pktopt->ip6po_dest1)
9bccf70c 3017 FREE(pktopt->ip6po_dest1, M_IP6OPT);
1c79356b
A
3018 pktopt->ip6po_dest1 = NULL;
3019 }
6d2010ae
A
3020 if (optname == -1 || optname == IPV6_RTHDR) {
3021 if (pktopt->ip6po_rhinfo.ip6po_rhi_rthdr)
9bccf70c 3022 FREE(pktopt->ip6po_rhinfo.ip6po_rhi_rthdr, M_IP6OPT);
1c79356b 3023 pktopt->ip6po_rhinfo.ip6po_rhi_rthdr = NULL;
39236c6e 3024 ROUTE_RELEASE(&pktopt->ip6po_route);
1c79356b 3025 }
6d2010ae
A
3026 if (optname == -1 || optname == IPV6_DSTOPTS) {
3027 if (pktopt->ip6po_dest2)
9bccf70c 3028 FREE(pktopt->ip6po_dest2, M_IP6OPT);
1c79356b
A
3029 pktopt->ip6po_dest2 = NULL;
3030 }
3031}
3032
39236c6e
A
3033#define PKTOPT_EXTHDRCPY(type) do { \
3034 if (src->type) { \
3035 int hlen = \
3036 (((struct ip6_ext *)src->type)->ip6e_len + 1) << 3; \
3037 dst->type = _MALLOC(hlen, M_IP6OPT, canwait); \
3038 if (dst->type == NULL && canwait == M_NOWAIT) \
3039 goto bad; \
3040 bcopy(src->type, dst->type, hlen); \
3041 } \
9bccf70c 3042} while (0)
1c79356b 3043
6d2010ae
A
3044static int
3045copypktopts(struct ip6_pktopts *dst, struct ip6_pktopts *src, int canwait)
1c79356b 3046{
6d2010ae 3047 if (dst == NULL || src == NULL) {
316670eb 3048 printf("copypktopts: invalid argument\n");
6d2010ae 3049 return (EINVAL);
1c79356b
A
3050 }
3051
1c79356b 3052 dst->ip6po_hlim = src->ip6po_hlim;
b0d623f7 3053 dst->ip6po_tclass = src->ip6po_tclass;
6d2010ae 3054 dst->ip6po_flags = src->ip6po_flags;
1c79356b 3055 if (src->ip6po_pktinfo) {
39236c6e
A
3056 dst->ip6po_pktinfo = _MALLOC(sizeof (*dst->ip6po_pktinfo),
3057 M_IP6OPT, canwait);
1c79356b
A
3058 if (dst->ip6po_pktinfo == NULL && canwait == M_NOWAIT)
3059 goto bad;
3060 *dst->ip6po_pktinfo = *src->ip6po_pktinfo;
3061 }
3062 if (src->ip6po_nexthop) {
3063 dst->ip6po_nexthop = _MALLOC(src->ip6po_nexthop->sa_len,
39236c6e 3064 M_IP6OPT, canwait);
1c79356b
A
3065 if (dst->ip6po_nexthop == NULL && canwait == M_NOWAIT)
3066 goto bad;
3067 bcopy(src->ip6po_nexthop, dst->ip6po_nexthop,
39236c6e 3068 src->ip6po_nexthop->sa_len);
1c79356b
A
3069 }
3070 PKTOPT_EXTHDRCPY(ip6po_hbh);
3071 PKTOPT_EXTHDRCPY(ip6po_dest1);
3072 PKTOPT_EXTHDRCPY(ip6po_dest2);
3073 PKTOPT_EXTHDRCPY(ip6po_rthdr); /* not copy the cached route */
6d2010ae 3074 return (0);
1c79356b 3075
39236c6e 3076bad:
6d2010ae
A
3077 ip6_clearpktopts(dst, -1);
3078 return (ENOBUFS);
1c79356b
A
3079}
3080#undef PKTOPT_EXTHDRCPY
3081
6d2010ae
A
3082struct ip6_pktopts *
3083ip6_copypktopts(struct ip6_pktopts *src, int canwait)
3084{
3085 int error;
3086 struct ip6_pktopts *dst;
3087
39236c6e 3088 dst = _MALLOC(sizeof (*dst), M_IP6OPT, canwait);
6d2010ae
A
3089 if (dst == NULL)
3090 return (NULL);
3091 ip6_initpktopts(dst);
3092
3093 if ((error = copypktopts(dst, src, canwait)) != 0) {
3094 FREE(dst, M_IP6OPT);
3095 return (NULL);
3096 }
3097
3098 return (dst);
3099}
3100
1c79356b 3101void
316670eb 3102ip6_freepcbopts(struct ip6_pktopts *pktopt)
1c79356b
A
3103{
3104 if (pktopt == NULL)
3105 return;
3106
6d2010ae 3107 ip6_clearpktopts(pktopt, -1);
1c79356b 3108
9bccf70c 3109 FREE(pktopt, M_IP6OPT);
1c79356b
A
3110}
3111
6d2010ae
A
3112void
3113ip6_moptions_init(void)
1c79356b 3114{
6d2010ae 3115 PE_parse_boot_argn("ifa_debug", &im6o_debug, sizeof (im6o_debug));
1c79356b 3116
6d2010ae
A
3117 im6o_size = (im6o_debug == 0) ? sizeof (struct ip6_moptions) :
3118 sizeof (struct ip6_moptions_dbg);
3119
3120 im6o_zone = zinit(im6o_size, IM6O_ZONE_MAX * im6o_size, 0,
3121 IM6O_ZONE_NAME);
3122 if (im6o_zone == NULL) {
3123 panic("%s: failed allocating %s", __func__, IM6O_ZONE_NAME);
3124 /* NOTREACHED */
1c79356b 3125 }
6d2010ae
A
3126 zone_change(im6o_zone, Z_EXPAND, TRUE);
3127}
1c79356b 3128
6d2010ae
A
3129void
3130im6o_addref(struct ip6_moptions *im6o, int locked)
3131{
3132 if (!locked)
3133 IM6O_LOCK(im6o);
3134 else
3135 IM6O_LOCK_ASSERT_HELD(im6o);
1c79356b 3136
6d2010ae
A
3137 if (++im6o->im6o_refcnt == 0) {
3138 panic("%s: im6o %p wraparound refcnt\n", __func__, im6o);
3139 /* NOTREACHED */
3140 } else if (im6o->im6o_trace != NULL) {
3141 (*im6o->im6o_trace)(im6o, TRUE);
3142 }
3143
3144 if (!locked)
3145 IM6O_UNLOCK(im6o);
1c79356b
A
3146}
3147
6d2010ae
A
3148void
3149im6o_remref(struct ip6_moptions *im6o)
1c79356b 3150{
6d2010ae 3151 int i;
1c79356b 3152
6d2010ae
A
3153 IM6O_LOCK(im6o);
3154 if (im6o->im6o_refcnt == 0) {
3155 panic("%s: im6o %p negative refcnt", __func__, im6o);
3156 /* NOTREACHED */
3157 } else if (im6o->im6o_trace != NULL) {
3158 (*im6o->im6o_trace)(im6o, FALSE);
3159 }
1c79356b 3160
6d2010ae
A
3161 --im6o->im6o_refcnt;
3162 if (im6o->im6o_refcnt > 0) {
3163 IM6O_UNLOCK(im6o);
3164 return;
3165 }
1c79356b 3166
6d2010ae
A
3167 for (i = 0; i < im6o->im6o_num_memberships; ++i) {
3168 struct in6_mfilter *imf;
1c79356b 3169
6d2010ae
A
3170 imf = im6o->im6o_mfilters ? &im6o->im6o_mfilters[i] : NULL;
3171 if (imf != NULL)
3172 im6f_leave(imf);
1c79356b 3173
6d2010ae 3174 (void) in6_mc_leave(im6o->im6o_membership[i], imf);
1c79356b 3175
6d2010ae
A
3176 if (imf != NULL)
3177 im6f_purge(imf);
3178
3179 IN6M_REMREF(im6o->im6o_membership[i]);
3180 im6o->im6o_membership[i] = NULL;
3181 }
3182 im6o->im6o_num_memberships = 0;
3183 if (im6o->im6o_mfilters != NULL) {
3184 FREE(im6o->im6o_mfilters, M_IN6MFILTER);
3185 im6o->im6o_mfilters = NULL;
3186 }
3187 if (im6o->im6o_membership != NULL) {
3188 FREE(im6o->im6o_membership, M_IP6MOPTS);
3189 im6o->im6o_membership = NULL;
3190 }
3191 IM6O_UNLOCK(im6o);
3192
3193 lck_mtx_destroy(&im6o->im6o_lock, ifa_mtx_grp);
3194
3195 if (!(im6o->im6o_debug & IFD_ALLOC)) {
3196 panic("%s: im6o %p cannot be freed", __func__, im6o);
3197 /* NOTREACHED */
1c79356b 3198 }
6d2010ae 3199 zfree(im6o_zone, im6o);
1c79356b
A
3200}
3201
6d2010ae
A
3202static void
3203im6o_trace(struct ip6_moptions *im6o, int refhold)
1c79356b 3204{
6d2010ae
A
3205 struct ip6_moptions_dbg *im6o_dbg = (struct ip6_moptions_dbg *)im6o;
3206 ctrace_t *tr;
3207 u_int32_t idx;
3208 u_int16_t *cnt;
1c79356b 3209
6d2010ae
A
3210 if (!(im6o->im6o_debug & IFD_DEBUG)) {
3211 panic("%s: im6o %p has no debug structure", __func__, im6o);
3212 /* NOTREACHED */
3213 }
3214 if (refhold) {
3215 cnt = &im6o_dbg->im6o_refhold_cnt;
3216 tr = im6o_dbg->im6o_refhold;
3217 } else {
3218 cnt = &im6o_dbg->im6o_refrele_cnt;
3219 tr = im6o_dbg->im6o_refrele;
3220 }
3221
3222 idx = atomic_add_16_ov(cnt, 1) % IM6O_TRACE_HIST_SIZE;
3223 ctrace_record(&tr[idx]);
3224}
3225
3226struct ip6_moptions *
3227ip6_allocmoptions(int how)
3228{
3229 struct ip6_moptions *im6o;
3230
3231 im6o = (how == M_WAITOK) ?
3232 zalloc(im6o_zone) : zalloc_noblock(im6o_zone);
3233 if (im6o != NULL) {
3234 bzero(im6o, im6o_size);
3235 lck_mtx_init(&im6o->im6o_lock, ifa_mtx_grp, ifa_mtx_attr);
3236 im6o->im6o_debug |= IFD_ALLOC;
3237 if (im6o_debug != 0) {
3238 im6o->im6o_debug |= IFD_DEBUG;
3239 im6o->im6o_trace = im6o_trace;
3240 }
3241 IM6O_ADDREF(im6o);
3242 }
3243
3244 return (im6o);
1c79356b
A
3245}
3246
3247/*
3248 * Set IPv6 outgoing packet options based on advanced API.
3249 */
3250int
6d2010ae
A
3251ip6_setpktopts(struct mbuf *control, struct ip6_pktopts *opt,
3252 struct ip6_pktopts *stickyopt, int uproto)
1c79356b 3253{
39236c6e 3254 struct cmsghdr *cm = NULL;
1c79356b 3255
6d2010ae
A
3256 if (control == NULL || opt == NULL)
3257 return (EINVAL);
1c79356b 3258
b0d623f7 3259 ip6_initpktopts(opt);
6d2010ae
A
3260 if (stickyopt) {
3261 int error;
3262
3263 /*
3264 * If stickyopt is provided, make a local copy of the options
3265 * for this particular packet, then override them by ancillary
3266 * objects.
3267 * XXX: copypktopts() does not copy the cached route to a next
3268 * hop (if any). This is not very good in terms of efficiency,
3269 * but we can allow this since this option should be rarely
3270 * used.
3271 */
3272 if ((error = copypktopts(opt, stickyopt, M_NOWAIT)) != 0)
3273 return (error);
3274 }
1c79356b
A
3275
3276 /*
3277 * XXX: Currently, we assume all the optional information is stored
3278 * in a single mbuf.
3279 */
3280 if (control->m_next)
6d2010ae 3281 return (EINVAL);
1c79356b 3282
6d2010ae
A
3283 if (control->m_len < CMSG_LEN(0))
3284 return (EINVAL);
3285
39236c6e
A
3286 for (cm = M_FIRST_CMSGHDR(control); cm != NULL;
3287 cm = M_NXT_CMSGHDR(control, cm)) {
6d2010ae
A
3288 int error;
3289
39236c6e
A
3290 if (cm->cmsg_len < sizeof (struct cmsghdr) ||
3291 cm->cmsg_len > control->m_len)
6d2010ae 3292 return (EINVAL);
1c79356b
A
3293 if (cm->cmsg_level != IPPROTO_IPV6)
3294 continue;
3295
6d2010ae
A
3296 error = ip6_setpktopt(cm->cmsg_type, CMSG_DATA(cm),
3297 cm->cmsg_len - CMSG_LEN(0), opt, 0, 1, uproto);
3298 if (error)
3299 return (error);
3300 }
3301
3302 return (0);
3303}
3304/*
3305 * Set a particular packet option, as a sticky option or an ancillary data
3306 * item. "len" can be 0 only when it's a sticky option.
3307 * We have 4 cases of combination of "sticky" and "cmsg":
3308 * "sticky=0, cmsg=0": impossible
3309 * "sticky=0, cmsg=1": RFC2292 or RFC3542 ancillary data
3310 * "sticky=1, cmsg=0": RFC3542 socket option
3311 * "sticky=1, cmsg=1": RFC2292 socket option
3312 */
3313static int
3314ip6_setpktopt(int optname, u_char *buf, int len, struct ip6_pktopts *opt,
3315 int sticky, int cmsg, int uproto)
3316{
3317 int minmtupolicy, preftemp;
3318 int error;
3319
3320 if (!sticky && !cmsg) {
3321#ifdef DIAGNOSTIC
3322 printf("ip6_setpktopt: impossible case\n");
3323#endif
3324 return (EINVAL);
3325 }
3326
316670eb
A
3327 /*
3328 * Caller must have ensured that the buffer is at least
3329 * aligned on 32-bit boundary.
3330 */
3331 VERIFY(IS_P2ALIGNED(buf, sizeof (u_int32_t)));
3332
6d2010ae
A
3333 /*
3334 * IPV6_2292xxx is for backward compatibility to RFC2292, and should
3335 * not be specified in the context of RFC3542. Conversely,
3336 * RFC3542 types should not be specified in the context of RFC2292.
3337 */
3338 if (!cmsg) {
3339 switch (optname) {
3340 case IPV6_2292PKTINFO:
3341 case IPV6_2292HOPLIMIT:
3342 case IPV6_2292NEXTHOP:
3343 case IPV6_2292HOPOPTS:
3344 case IPV6_2292DSTOPTS:
3345 case IPV6_2292RTHDR:
3346 case IPV6_2292PKTOPTIONS:
3347 return (ENOPROTOOPT);
3348 }
3349 }
3350 if (sticky && cmsg) {
3351 switch (optname) {
3352 case IPV6_PKTINFO:
3353 case IPV6_HOPLIMIT:
3354 case IPV6_NEXTHOP:
3355 case IPV6_HOPOPTS:
3356 case IPV6_DSTOPTS:
3357 case IPV6_RTHDRDSTOPTS:
3358 case IPV6_RTHDR:
3359 case IPV6_USE_MIN_MTU:
3360 case IPV6_DONTFRAG:
3361 case IPV6_TCLASS:
3362 case IPV6_PREFER_TEMPADDR: /* XXX: not an RFC3542 option */
3363 return (ENOPROTOOPT);
3364 }
3365 }
3366
3367 switch (optname) {
3368 case IPV6_2292PKTINFO:
39236c6e 3369 case IPV6_PKTINFO: {
6d2010ae
A
3370 struct ifnet *ifp = NULL;
3371 struct in6_pktinfo *pktinfo;
3372
39236c6e 3373 if (len != sizeof (struct in6_pktinfo))
6d2010ae
A
3374 return (EINVAL);
3375
316670eb 3376 pktinfo = (struct in6_pktinfo *)(void *)buf;
6d2010ae 3377
9bccf70c 3378 /*
6d2010ae
A
3379 * An application can clear any sticky IPV6_PKTINFO option by
3380 * doing a "regular" setsockopt with ipi6_addr being
3381 * in6addr_any and ipi6_ifindex being zero.
3382 * [RFC 3542, Section 6]
9bccf70c 3383 */
6d2010ae
A
3384 if (optname == IPV6_PKTINFO && opt->ip6po_pktinfo &&
3385 pktinfo->ipi6_ifindex == 0 &&
3386 IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
3387 ip6_clearpktopts(opt, optname);
3388 break;
3389 }
1c79356b 3390
6d2010ae
A
3391 if (uproto == IPPROTO_TCP && optname == IPV6_PKTINFO &&
3392 sticky && !IN6_IS_ADDR_UNSPECIFIED(&pktinfo->ipi6_addr)) {
3393 return (EINVAL);
3394 }
3395
3396 /* validate the interface index if specified. */
3397 ifnet_head_lock_shared();
3398
3399 if (pktinfo->ipi6_ifindex > if_index) {
3400 ifnet_head_done();
3401 return (ENXIO);
3402 }
39236c6e 3403
6d2010ae
A
3404 if (pktinfo->ipi6_ifindex) {
3405 ifp = ifindex2ifnet[pktinfo->ipi6_ifindex];
3406 if (ifp == NULL) {
3407 ifnet_head_done();
3408 return (ENXIO);
1c79356b 3409 }
6d2010ae 3410 }
39236c6e 3411
6d2010ae 3412 ifnet_head_done();
1c79356b 3413
6d2010ae
A
3414 /*
3415 * We store the address anyway, and let in6_selectsrc()
3416 * validate the specified address. This is because ipi6_addr
3417 * may not have enough information about its scope zone, and
3418 * we may need additional information (such as outgoing
3419 * interface or the scope zone of a destination address) to
3420 * disambiguate the scope.
3421 * XXX: the delay of the validation may confuse the
3422 * application when it is used as a sticky option.
3423 */
3424 if (opt->ip6po_pktinfo == NULL) {
39236c6e 3425 opt->ip6po_pktinfo = _MALLOC(sizeof (*pktinfo),
6d2010ae
A
3426 M_IP6OPT, M_NOWAIT);
3427 if (opt->ip6po_pktinfo == NULL)
3428 return (ENOBUFS);
3429 }
39236c6e 3430 bcopy(pktinfo, opt->ip6po_pktinfo, sizeof (*pktinfo));
6d2010ae
A
3431 break;
3432 }
1c79356b 3433
6d2010ae 3434 case IPV6_2292HOPLIMIT:
39236c6e 3435 case IPV6_HOPLIMIT: {
6d2010ae 3436 int *hlimp;
1c79356b 3437
6d2010ae
A
3438 /*
3439 * RFC 3542 deprecated the usage of sticky IPV6_HOPLIMIT
3440 * to simplify the ordering among hoplimit options.
3441 */
3442 if (optname == IPV6_HOPLIMIT && sticky)
3443 return (ENOPROTOOPT);
b0d623f7 3444
39236c6e 3445 if (len != sizeof (int))
6d2010ae 3446 return (EINVAL);
316670eb 3447 hlimp = (int *)(void *)buf;
6d2010ae
A
3448 if (*hlimp < -1 || *hlimp > 255)
3449 return (EINVAL);
b0d623f7 3450
6d2010ae
A
3451 opt->ip6po_hlim = *hlimp;
3452 break;
3453 }
3454
39236c6e 3455 case IPV6_TCLASS: {
6d2010ae
A
3456 int tclass;
3457
39236c6e 3458 if (len != sizeof (int))
6d2010ae 3459 return (EINVAL);
316670eb 3460 tclass = *(int *)(void *)buf;
6d2010ae
A
3461 if (tclass < -1 || tclass > 255)
3462 return (EINVAL);
3463
3464 opt->ip6po_tclass = tclass;
3465 break;
3466 }
3467
3468 case IPV6_2292NEXTHOP:
3469 case IPV6_NEXTHOP:
3470 error = suser(kauth_cred_get(), 0);
3471 if (error)
3472 return (EACCES);
3473
3474 if (len == 0) { /* just remove the option */
3475 ip6_clearpktopts(opt, IPV6_NEXTHOP);
1c79356b 3476 break;
6d2010ae 3477 }
1c79356b 3478
6d2010ae 3479 /* check if cmsg_len is large enough for sa_len */
39236c6e 3480 if (len < sizeof (struct sockaddr) || len < *buf)
6d2010ae
A
3481 return (EINVAL);
3482
39236c6e
A
3483 switch (SA(buf)->sa_family) {
3484 case AF_INET6: {
3485 struct sockaddr_in6 *sa6 = SIN6(buf);
6d2010ae 3486
39236c6e 3487 if (sa6->sin6_len != sizeof (struct sockaddr_in6))
6d2010ae
A
3488 return (EINVAL);
3489
3490 if (IN6_IS_ADDR_UNSPECIFIED(&sa6->sin6_addr) ||
3491 IN6_IS_ADDR_MULTICAST(&sa6->sin6_addr)) {
3492 return (EINVAL);
3493 }
3494 if ((error = sa6_embedscope(sa6, ip6_use_defzone))
3495 != 0) {
3496 return (error);
3497 }
1c79356b
A
3498 break;
3499 }
6d2010ae
A
3500 case AF_LINK: /* should eventually be supported */
3501 default:
3502 return (EAFNOSUPPORT);
3503 }
1c79356b 3504
6d2010ae
A
3505 /* turn off the previous option, then set the new option. */
3506 ip6_clearpktopts(opt, IPV6_NEXTHOP);
3507 opt->ip6po_nexthop = _MALLOC(*buf, M_IP6OPT, M_NOWAIT);
3508 if (opt->ip6po_nexthop == NULL)
3509 return (ENOBUFS);
3510 bcopy(buf, opt->ip6po_nexthop, *buf);
3511 break;
3512
3513 case IPV6_2292HOPOPTS:
39236c6e 3514 case IPV6_HOPOPTS: {
6d2010ae
A
3515 struct ip6_hbh *hbh;
3516 int hbhlen;
3517
3518 /*
3519 * XXX: We don't allow a non-privileged user to set ANY HbH
3520 * options, since per-option restriction has too much
3521 * overhead.
3522 */
3523 error = suser(kauth_cred_get(), 0);
3524 if (error)
3525 return (EACCES);
3526
3527 if (len == 0) {
3528 ip6_clearpktopts(opt, IPV6_HOPOPTS);
3529 break; /* just remove the option */
3530 }
3531
3532 /* message length validation */
39236c6e 3533 if (len < sizeof (struct ip6_hbh))
6d2010ae 3534 return (EINVAL);
316670eb 3535 hbh = (struct ip6_hbh *)(void *)buf;
6d2010ae
A
3536 hbhlen = (hbh->ip6h_len + 1) << 3;
3537 if (len != hbhlen)
3538 return (EINVAL);
3539
3540 /* turn off the previous option, then set the new option. */
3541 ip6_clearpktopts(opt, IPV6_HOPOPTS);
3542 opt->ip6po_hbh = _MALLOC(hbhlen, M_IP6OPT, M_NOWAIT);
3543 if (opt->ip6po_hbh == NULL)
3544 return (ENOBUFS);
3545 bcopy(hbh, opt->ip6po_hbh, hbhlen);
3546
3547 break;
3548 }
3549
3550 case IPV6_2292DSTOPTS:
3551 case IPV6_DSTOPTS:
39236c6e 3552 case IPV6_RTHDRDSTOPTS: {
6d2010ae
A
3553 struct ip6_dest *dest, **newdest = NULL;
3554 int destlen;
3555
3556 error = suser(kauth_cred_get(), 0);
3557 if (error)
3558 return (EACCES);
3559
3560 if (len == 0) {
3561 ip6_clearpktopts(opt, optname);
3562 break; /* just remove the option */
3563 }
3564
3565 /* message length validation */
39236c6e 3566 if (len < sizeof (struct ip6_dest))
6d2010ae 3567 return (EINVAL);
316670eb 3568 dest = (struct ip6_dest *)(void *)buf;
6d2010ae
A
3569 destlen = (dest->ip6d_len + 1) << 3;
3570 if (len != destlen)
3571 return (EINVAL);
3572
3573 /*
3574 * Determine the position that the destination options header
3575 * should be inserted; before or after the routing header.
3576 */
3577 switch (optname) {
3578 case IPV6_2292DSTOPTS:
3579 /*
3580 * The old advacned API is ambiguous on this point.
3581 * Our approach is to determine the position based
3582 * according to the existence of a routing header.
3583 * Note, however, that this depends on the order of the
3584 * extension headers in the ancillary data; the 1st
3585 * part of the destination options header must appear
3586 * before the routing header in the ancillary data,
3587 * too.
3588 * RFC3542 solved the ambiguity by introducing
3589 * separate ancillary data or option types.
1c79356b 3590 */
9bccf70c
A
3591 if (opt->ip6po_rthdr == NULL)
3592 newdest = &opt->ip6po_dest1;
3593 else
3594 newdest = &opt->ip6po_dest2;
6d2010ae
A
3595 break;
3596 case IPV6_RTHDRDSTOPTS:
3597 newdest = &opt->ip6po_dest1;
3598 break;
3599 case IPV6_DSTOPTS:
3600 newdest = &opt->ip6po_dest2;
1c79356b
A
3601 break;
3602 }
3603
6d2010ae
A
3604 /* turn off the previous option, then set the new option. */
3605 ip6_clearpktopts(opt, optname);
3606 *newdest = _MALLOC(destlen, M_IP6OPT, M_NOWAIT);
3607 if (*newdest == NULL)
3608 return (ENOBUFS);
3609 bcopy(dest, *newdest, destlen);
6d2010ae
A
3610 break;
3611 }
1c79356b 3612
6d2010ae 3613 case IPV6_2292RTHDR:
39236c6e 3614 case IPV6_RTHDR: {
6d2010ae
A
3615 struct ip6_rthdr *rth;
3616 int rthlen;
3617
3618 if (len == 0) {
3619 ip6_clearpktopts(opt, IPV6_RTHDR);
3620 break; /* just remove the option */
1c79356b
A
3621 }
3622
6d2010ae 3623 /* message length validation */
39236c6e 3624 if (len < sizeof (struct ip6_rthdr))
6d2010ae 3625 return (EINVAL);
316670eb 3626 rth = (struct ip6_rthdr *)(void *)buf;
6d2010ae
A
3627 rthlen = (rth->ip6r_len + 1) << 3;
3628 if (len != rthlen)
3629 return (EINVAL);
3630
3631 switch (rth->ip6r_type) {
3632 case IPV6_RTHDR_TYPE_0:
3633 if (rth->ip6r_len == 0) /* must contain one addr */
3634 return (EINVAL);
3635 if (rth->ip6r_len % 2) /* length must be even */
3636 return (EINVAL);
3637 if (rth->ip6r_len / 2 != rth->ip6r_segleft)
3638 return (EINVAL);
3639 break;
1c79356b 3640 default:
6d2010ae 3641 return (EINVAL); /* not supported */
1c79356b 3642 }
6d2010ae
A
3643
3644 /* turn off the previous option */
3645 ip6_clearpktopts(opt, IPV6_RTHDR);
3646 opt->ip6po_rthdr = _MALLOC(rthlen, M_IP6OPT, M_NOWAIT);
3647 if (opt->ip6po_rthdr == NULL)
3648 return (ENOBUFS);
3649 bcopy(rth, opt->ip6po_rthdr, rthlen);
6d2010ae 3650 break;
1c79356b
A
3651 }
3652
6d2010ae 3653 case IPV6_USE_MIN_MTU:
39236c6e 3654 if (len != sizeof (int))
6d2010ae 3655 return (EINVAL);
316670eb 3656 minmtupolicy = *(int *)(void *)buf;
6d2010ae
A
3657 if (minmtupolicy != IP6PO_MINMTU_MCASTONLY &&
3658 minmtupolicy != IP6PO_MINMTU_DISABLE &&
3659 minmtupolicy != IP6PO_MINMTU_ALL) {
3660 return (EINVAL);
3661 }
3662 opt->ip6po_minmtu = minmtupolicy;
3663 break;
3664
3665 case IPV6_DONTFRAG:
39236c6e 3666 if (len != sizeof (int))
6d2010ae
A
3667 return (EINVAL);
3668
316670eb 3669 if (uproto == IPPROTO_TCP || *(int *)(void *)buf == 0) {
6d2010ae
A
3670 /*
3671 * we ignore this option for TCP sockets.
3672 * (RFC3542 leaves this case unspecified.)
3673 */
3674 opt->ip6po_flags &= ~IP6PO_DONTFRAG;
39236c6e 3675 } else {
6d2010ae 3676 opt->ip6po_flags |= IP6PO_DONTFRAG;
39236c6e 3677 }
6d2010ae
A
3678 break;
3679
3680 case IPV6_PREFER_TEMPADDR:
39236c6e 3681 if (len != sizeof (int))
6d2010ae 3682 return (EINVAL);
316670eb 3683 preftemp = *(int *)(void *)buf;
6d2010ae
A
3684 if (preftemp != IP6PO_TEMPADDR_SYSTEM &&
3685 preftemp != IP6PO_TEMPADDR_NOTPREFER &&
3686 preftemp != IP6PO_TEMPADDR_PREFER) {
3687 return (EINVAL);
3688 }
3689 opt->ip6po_prefer_tempaddr = preftemp;
3690 break;
3691
3692 default:
3693 return (ENOPROTOOPT);
3694 } /* end of switch */
3695
3696 return (0);
1c79356b
A
3697}
3698
3699/*
3700 * Routine called from ip6_output() to loop back a copy of an IP6 multicast
3701 * packet to the input queue of a specified interface. Note that this
3702 * calls the output routine of the loopback "driver", but with an interface
3703 * pointer that might NOT be &loif -- easier than replicating that code here.
3704 */
3705void
39236c6e
A
3706ip6_mloopback(struct ifnet *srcifp, struct ifnet *origifp, struct mbuf *m,
3707 struct sockaddr_in6 *dst, uint32_t optlen, int32_t nxt0)
1c79356b 3708{
9bccf70c
A
3709 struct mbuf *copym;
3710 struct ip6_hdr *ip6;
39236c6e 3711 struct in6_addr src;
1c79356b 3712
39236c6e 3713 if (lo_ifp == NULL)
9bccf70c
A
3714 return;
3715
3716 /*
39236c6e 3717 * Copy the packet header as it's needed for the checksum.
9bccf70c
A
3718 * Make sure to deep-copy IPv6 header portion in case the data
3719 * is in an mbuf cluster, so that we can safely override the IPv6
3720 * header portion later.
3721 */
39236c6e
A
3722 copym = m_copym_mode(m, 0, M_COPYALL, M_DONTWAIT, M_COPYM_COPY_HDR);
3723 if (copym != NULL && ((copym->m_flags & M_EXT) ||
3724 copym->m_len < sizeof (struct ip6_hdr)))
3725 copym = m_pullup(copym, sizeof (struct ip6_hdr));
9bccf70c 3726
39236c6e 3727 if (copym == NULL)
9bccf70c 3728 return;
9bccf70c
A
3729
3730 ip6 = mtod(copym, struct ip6_hdr *);
39236c6e 3731 src = ip6->ip6_src;
9bccf70c
A
3732 /*
3733 * clear embedded scope identifiers if necessary.
3734 * in6_clearscope will touch the addresses only when necessary.
3735 */
3736 in6_clearscope(&ip6->ip6_src);
3737 in6_clearscope(&ip6->ip6_dst);
9bccf70c 3738
39236c6e
A
3739 if (copym->m_pkthdr.csum_flags & CSUM_DELAY_IPV6_DATA)
3740 in6_delayed_cksum_offset(copym, 0, optlen, nxt0);
9bccf70c 3741
39236c6e
A
3742 /*
3743 * Stuff the 'real' ifp into the pkthdr, to be used in matching
3744 * in ip6_input(); we need the loopback ifp/dl_tag passed as args
3745 * to make the loopback driver compliant with the data link
3746 * requirements.
3747 */
3748 copym->m_pkthdr.rcvif = origifp;
9bccf70c 3749
39236c6e
A
3750 /*
3751 * Also record the source interface (which owns the source address).
3752 * This is basically a stripped down version of ifa_foraddr6().
3753 */
3754 if (srcifp == NULL) {
3755 struct in6_ifaddr *ia;
3756
3757 lck_rw_lock_shared(&in6_ifaddr_rwlock);
3758 for (ia = in6_ifaddrs; ia != NULL; ia = ia->ia_next) {
3759 IFA_LOCK_SPIN(&ia->ia_ifa);
3760 /* compare against src addr with embedded scope */
3761 if (IN6_ARE_ADDR_EQUAL(&ia->ia_addr.sin6_addr, &src)) {
3762 srcifp = ia->ia_ifp;
3763 IFA_UNLOCK(&ia->ia_ifa);
3764 break;
3765 }
3766 IFA_UNLOCK(&ia->ia_ifa);
3767 }
3768 lck_rw_done(&in6_ifaddr_rwlock);
6d2010ae 3769 }
39236c6e
A
3770 if (srcifp != NULL)
3771 ip6_setsrcifaddr_info(copym, srcifp->if_index, NULL);
3772 ip6_setdstifaddr_info(copym, origifp->if_index, NULL);
9bccf70c 3773
39236c6e 3774 dlil_output(lo_ifp, PF_INET6, copym, NULL, SA(dst), 0, NULL);
1c79356b
A
3775}
3776
3777/*
3778 * Chop IPv6 header off from the payload.
3779 */
3780static int
39236c6e 3781ip6_splithdr(struct mbuf *m, struct ip6_exthdrs *exthdrs)
1c79356b
A
3782{
3783 struct mbuf *mh;
3784 struct ip6_hdr *ip6;
3785
3786 ip6 = mtod(m, struct ip6_hdr *);
39236c6e 3787 if (m->m_len > sizeof (*ip6)) {
2d21ac55 3788 MGETHDR(mh, M_DONTWAIT, MT_HEADER); /* MAC-OK */
39236c6e 3789 if (mh == NULL) {
1c79356b 3790 m_freem(m);
39236c6e 3791 return (ENOBUFS);
1c79356b
A
3792 }
3793 M_COPY_PKTHDR(mh, m);
39236c6e 3794 MH_ALIGN(mh, sizeof (*ip6));
1c79356b 3795 m->m_flags &= ~M_PKTHDR;
39236c6e
A
3796 m->m_len -= sizeof (*ip6);
3797 m->m_data += sizeof (*ip6);
1c79356b
A
3798 mh->m_next = m;
3799 m = mh;
39236c6e
A
3800 m->m_len = sizeof (*ip6);
3801 bcopy((caddr_t)ip6, mtod(m, caddr_t), sizeof (*ip6));
1c79356b
A
3802 }
3803 exthdrs->ip6e_ip6 = m;
39236c6e
A
3804 return (0);
3805}
3806
3807static void
3808ip6_output_checksum(struct ifnet *ifp, uint32_t mtu, struct mbuf *m,
3809 int nxt0, uint32_t tlen, uint32_t optlen)
3810{
3811 uint32_t sw_csum, hwcap = ifp->if_hwassist;
3812 int tso = TSO_IPV6_OK(ifp, m);
3813
3814 if (!hwcksum_tx) {
3815 /* do all in software; checksum offload is disabled */
3816 sw_csum = CSUM_DELAY_IPV6_DATA & m->m_pkthdr.csum_flags;
3817 } else {
3818 /* do in software what the hardware cannot */
3819 sw_csum = m->m_pkthdr.csum_flags &
3820 ~IF_HWASSIST_CSUM_FLAGS(hwcap);
3821 }
3822
3823 if (optlen != 0) {
3824 sw_csum |= (CSUM_DELAY_IPV6_DATA &
3825 m->m_pkthdr.csum_flags);
3826 } else if (!(sw_csum & CSUM_DELAY_IPV6_DATA) &&
3827 (hwcap & CSUM_PARTIAL)) {
3828 /*
3829 * Partial checksum offload, ere), if no extension
3830 * headers, and TCP only (no UDP support, as the
3831 * hardware may not be able to convert +0 to
3832 * -0 (0xffff) per RFC1122 4.1.3.4.)
3833 */
3834 if (hwcksum_tx && !tso &&
3835 (m->m_pkthdr.csum_flags & CSUM_TCPIPV6) &&
3836 tlen <= mtu) {
3837 uint16_t start = sizeof (struct ip6_hdr);
3838 uint16_t ulpoff =
3839 m->m_pkthdr.csum_data & 0xffff;
3840 m->m_pkthdr.csum_flags |=
3841 (CSUM_DATA_VALID | CSUM_PARTIAL);
3842 m->m_pkthdr.csum_tx_stuff = (ulpoff + start);
3843 m->m_pkthdr.csum_tx_start = start;
3844 sw_csum = 0;
3845 } else {
3846 sw_csum |= (CSUM_DELAY_IPV6_DATA &
3847 m->m_pkthdr.csum_flags);
3848 }
3849 }
3850
3851 if (sw_csum & CSUM_DELAY_IPV6_DATA) {
3852 in6_delayed_cksum_offset(m, 0, optlen, nxt0);
3853 sw_csum &= ~CSUM_DELAY_IPV6_DATA;
3854 }
3855
3856 if (hwcksum_tx) {
3857 /*
3858 * Drop off bits that aren't supported by hardware;
3859 * also make sure to preserve non-checksum related bits.
3860 */
3861 m->m_pkthdr.csum_flags =
3862 ((m->m_pkthdr.csum_flags &
3863 (IF_HWASSIST_CSUM_FLAGS(hwcap) | CSUM_DATA_VALID)) |
3864 (m->m_pkthdr.csum_flags & ~IF_HWASSIST_CSUM_MASK));
3865 } else {
3866 /* drop all bits; checksum offload is disabled */
3867 m->m_pkthdr.csum_flags = 0;
3868 }
1c79356b
A
3869}
3870
3871/*
3872 * Compute IPv6 extension header length.
3873 */
1c79356b 3874int
39236c6e 3875ip6_optlen(struct in6pcb *in6p)
1c79356b
A
3876{
3877 int len;
3878
3879 if (!in6p->in6p_outputopts)
39236c6e 3880 return (0);
1c79356b
A
3881
3882 len = 0;
39236c6e
A
3883#define elen(x) \
3884 (((struct ip6_ext *)(x)) ? \
3885 (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
1c79356b
A
3886
3887 len += elen(in6p->in6p_outputopts->ip6po_hbh);
39236c6e 3888 if (in6p->in6p_outputopts->ip6po_rthdr) {
1c79356b
A
3889 /* dest1 is valid with rthdr only */
3890 len += elen(in6p->in6p_outputopts->ip6po_dest1);
39236c6e 3891 }
1c79356b
A
3892 len += elen(in6p->in6p_outputopts->ip6po_rthdr);
3893 len += elen(in6p->in6p_outputopts->ip6po_dest2);
39236c6e 3894 return (len);
1c79356b
A
3895#undef elen
3896}