]> git.saurik.com Git - apple/xnu.git/blob - bsd/netinet6/ip6_output.c
xnu-2782.1.97.tar.gz
[apple/xnu.git] / bsd / netinet6 / ip6_output.c
1 /*
2 * Copyright (c) 2000-2014 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
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.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
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 */
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 */
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>
107 #include <sys/kernel.h>
108 #include <sys/proc.h>
109 #include <sys/kauth.h>
110 #include <sys/mcache.h>
111 #include <sys/sysctl.h>
112 #include <kern/zalloc.h>
113 #include <libkern/OSByteOrder.h>
114
115 #include <pexpert/pexpert.h>
116 #include <mach/sdt.h>
117
118 #include <net/if.h>
119 #include <net/route.h>
120 #include <net/dlil.h>
121 #include <net/net_osdep.h>
122
123 #include <netinet/in.h>
124 #include <netinet/in_var.h>
125 #include <netinet/ip_var.h>
126 #include <netinet6/in6_var.h>
127 #include <netinet/ip6.h>
128 #include <netinet/kpi_ipfilter_var.h>
129
130 #include <netinet6/ip6protosw.h>
131 #include <netinet/icmp6.h>
132 #include <netinet6/ip6_var.h>
133 #include <netinet/in_pcb.h>
134 #include <netinet6/nd6.h>
135 #include <netinet6/scope6_var.h>
136 #if IPSEC
137 #include <netinet6/ipsec.h>
138 #include <netinet6/ipsec6.h>
139 #include <netkey/key.h>
140 extern int ipsec_bypass;
141 #endif /* IPSEC */
142
143 #if NECP
144 #include <net/necp.h>
145 #endif /* NECP */
146
147 #if CONFIG_MACF_NET
148 #include <security/mac.h>
149 #endif /* CONFIG_MACF_NET */
150
151 #if DUMMYNET
152 #include <netinet6/ip6_fw.h>
153 #include <netinet/ip_fw.h>
154 #include <netinet/ip_dummynet.h>
155 #endif /* DUMMYNET */
156
157 #if PF
158 #include <net/pfvar.h>
159 #endif /* PF */
160
161 static int ip6_copyexthdr(struct mbuf **, caddr_t, int);
162 static void ip6_out_cksum_stats(int, u_int32_t);
163 static int ip6_insert_jumboopt(struct ip6_exthdrs *, u_int32_t);
164 static int ip6_insertfraghdr(struct mbuf *, struct mbuf *, int,
165 struct ip6_frag **);
166 static int ip6_getpmtu(struct route_in6 *, struct route_in6 *,
167 struct ifnet *, struct in6_addr *, u_int32_t *, boolean_t *);
168 static int ip6_pcbopts(struct ip6_pktopts **, struct mbuf *, struct socket *,
169 struct sockopt *sopt);
170 static int ip6_pcbopt(int, u_char *, int, struct ip6_pktopts **, int);
171 static int ip6_getpcbopt(struct ip6_pktopts *, int, struct sockopt *);
172 static int copypktopts(struct ip6_pktopts *, struct ip6_pktopts *, int);
173 static void im6o_trace(struct ip6_moptions *, int);
174 static int ip6_setpktopt(int, u_char *, int, struct ip6_pktopts *, int,
175 int, int);
176 static int ip6_splithdr(struct mbuf *, struct ip6_exthdrs *);
177 static void ip6_output_checksum(struct ifnet *, uint32_t, struct mbuf *,
178 int, uint32_t, uint32_t);
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
185 struct 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
202 static unsigned int im6o_debug = 1; /* debugging (enabled) */
203 #else
204 static unsigned int im6o_debug; /* debugging (disabled) */
205 #endif /* !DEBUG */
206
207 static unsigned int im6o_size; /* size of zone element */
208 static 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 */
212
213 SYSCTL_DECL(_net_inet6_ip6);
214
215 static int ip6_maxchainsent = 0;
216 SYSCTL_INT(_net_inet6_ip6, OID_AUTO, maxchainsent,
217 CTLFLAG_RW | CTLFLAG_LOCKED, &ip6_maxchainsent, 0,
218 "use dlil_output_list");
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 */
224 int
225 ip6_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)
228 {
229 #pragma unused(packetlist)
230 struct mbuf *m = m0, *nextpkt;
231 int error = 0;
232
233 while (m != NULL) {
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;
240 error = ip6_output(m, opt, ro, flags, im6o, ifpp, ip6oa);
241 if (error != 0) {
242 if (nextpkt != NULL)
243 m_freem_list(nextpkt);
244 return (error);
245 }
246 m = nextpkt;
247 }
248
249 return (error);
250 }
251
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.
258 *
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 *
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,
265 * which is rt_rmx.rmx_mtu.
266 */
267 int
268 ip6_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)
271 {
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;
278 struct rtentry *rt = NULL;
279 struct sockaddr_in6 *dst, src_sa, dst_sa;
280 int error = 0;
281 struct in6_ifaddr *ia = NULL, *src_ia = NULL;
282 u_int32_t mtu;
283 boolean_t alwaysfrag = FALSE;
284 u_int32_t optlen = 0, plen = 0, unfragpartlen = 0;
285 struct ip6_rthdr *rh;
286 struct in6_addr finaldst;
287 ipfilter_t inject_filter_ref;
288 struct ipf_pktopts *ippo = NULL;
289 struct flowadv *adv = NULL;
290 #if DUMMYNET
291 struct m_tag *tag;
292 struct ip6_out_args saved_ip6oa;
293 struct sockaddr_in6 dst_buf;
294 #endif /* DUMMYNET */
295 #if IPSEC
296 struct socket *so = NULL;
297 struct secpolicy *sp = NULL;
298 struct route_in6 *ipsec_saved_route = NULL;
299 boolean_t needipsectun = FALSE;
300 #endif /* IPSEC */
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 */
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 */
313 #if NECP
314 struct route_in6 necp_route;
315 #endif /* NECP */
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
326 #define necp_route ip6obz.necp_route
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 };
342
343 VERIFY(m0->m_flags & M_PKTHDR);
344
345 /* zero out {saved_route, saved_ro_pmtu, ip6route, exthdrs, args} */
346 bzero(&ip6obz, sizeof (ip6obz));
347
348 #if DUMMYNET
349 if (SLIST_EMPTY(&m0->m_pkthdr.tags))
350 goto tags_done;
351
352 /* Grab info from mtags prepended to the chain */
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
360 bcopy(&dn_tag->dn_dst6, &dst_buf, sizeof (dst_buf));
361 dst = &dst_buf;
362 ifp = dn_tag->dn_ifp;
363 if (ifp != NULL)
364 ifnet_reference(ifp);
365 flags = dn_tag->dn_flags;
366 if (dn_tag->dn_flags & IPV6_OUTARGS) {
367 saved_ip6oa = dn_tag->dn_ip6oa;
368 ip6oa = &saved_ip6oa;
369 }
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;
376 if (origifp != NULL)
377 ifnet_reference(origifp);
378 mtu = dn_tag->dn_mtu;
379 alwaysfrag = (dn_tag->dn_alwaysfrag != 0);
380 unfragpartlen = dn_tag->dn_unfragpartlen;
381
382 bcopy(&dn_tag->dn_exthdrs, &exthdrs, sizeof (exthdrs));
383
384 m_tag_delete(m0, tag);
385 }
386
387 tags_done:
388 #endif /* DUMMYNET */
389
390 m = m0;
391 m->m_pkthdr.pkt_flags &= ~(PKTF_LOOP|PKTF_IFAINFO);
392
393 #if IPSEC
394 if (ipsec_bypass == 0) {
395 so = ipsec_getsocket(m);
396 if (so != NULL) {
397 (void) ipsec_setsocket(m, NULL);
398 }
399 /* If packet is bound to an interface, check bound policies */
400 if ((flags & IPV6_OUTARGS) &&
401 (ip6oa->ip6oa_flags & IPOAF_BOUND_IF) &&
402 ip6oa->ip6oa_boundif != IFSCOPE_NONE) {
403 /* ip6obf.noipsec is a bitfield, use temp integer */
404 int noipsec = 0;
405
406 if (ipsec6_getpolicybyinterface(m, IPSEC_DIR_OUTBOUND,
407 flags, ip6oa, &noipsec, &sp) != 0)
408 goto bad;
409
410 ip6obf.noipsec = (noipsec != 0);
411 }
412 }
413 #endif /* IPSEC */
414
415 ip6 = mtod(m, struct ip6_hdr *);
416 nxt0 = ip6->ip6_nxt;
417 finaldst = ip6->ip6_dst;
418 inject_filter_ref = ipf_get_inject_filter(m);
419 ippo = &ipf_pktopts;
420
421 if (ip6_doscopedroute && (flags & IPV6_OUTARGS)) {
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 |
427 IPV6_UNSPECSRC | IPV6_FLAG_NOSRCIFSEL)) &&
428 (ip6oa->ip6oa_flags & IP6OAF_SELECT_SRCIF))))
429 ipf_pktopts.ippo_flags |= IPPOF_SELECT_SRCIF;
430
431 if ((ip6oa->ip6oa_flags & IP6OAF_BOUND_IF) &&
432 ip6oa->ip6oa_boundif != IFSCOPE_NONE) {
433 ipf_pktopts.ippo_flags |= (IPPOF_BOUND_IF |
434 (ip6oa->ip6oa_boundif << IPPOF_SHIFT_IFSCOPE));
435 }
436
437 if (ip6oa->ip6oa_flags & IP6OAF_BOUND_SRCADDR)
438 ipf_pktopts.ippo_flags |= IPPOF_BOUND_SRCADDR;
439 } else {
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 }
446 }
447
448 if (flags & IPV6_OUTARGS) {
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;
453 adv = &ip6oa->ip6oa_flowadv;
454 adv->code = FADV_SUCCESS;
455 ip6oa->ip6oa_retflags = 0;
456 }
457
458 #if DUMMYNET
459 if (args.fwa_pf_rule) {
460 ip6 = mtod(m, struct ip6_hdr *);
461 VERIFY(ro != NULL); /* ro == saved_route */
462 goto check_with_pf;
463 }
464 #endif /* DUMMYNET */
465
466 #define MAKE_EXTHDR(hp, mp) do { \
467 if (hp != NULL) { \
468 struct ip6_ext *eh = (struct ip6_ext *)(hp); \
469 error = ip6_copyexthdr((mp), (caddr_t)(hp), \
470 ((eh)->ip6e_len + 1) << 3); \
471 if (error) \
472 goto freehdrs; \
473 } \
474 } while (0)
475
476 if (opt != NULL) {
477 /* Hop-by-Hop options header */
478 MAKE_EXTHDR(opt->ip6po_hbh, &exthdrs.ip6e_hbh);
479 /* Destination options header(1st part) */
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 }
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
499 #undef MAKE_EXTHDR
500
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
546 #if IPSEC
547 if (ipsec_bypass != 0 || ip6obf.noipsec)
548 goto skip_ipsec;
549
550 if (sp == NULL) {
551 /* get a security policy for this packet */
552 if (so != NULL) {
553 sp = ipsec6_getpolicybysock(m, IPSEC_DIR_OUTBOUND,
554 so, &error);
555 } else {
556 sp = ipsec6_getpolicybyaddr(m, IPSEC_DIR_OUTBOUND,
557 0, &error);
558 }
559 if (sp == NULL) {
560 IPSEC_STAT_INCREMENT(ipsec6stat.out_inval);
561 goto freehdrs;
562 }
563 }
564
565 error = 0;
566
567 /* check policy */
568 switch (sp->policy) {
569 case IPSEC_POLICY_DISCARD:
570 case IPSEC_POLICY_GENERATE:
571 /*
572 * This packet is just discarded.
573 */
574 IPSEC_STAT_INCREMENT(ipsec6stat.out_polvio);
575 goto freehdrs;
576
577 case IPSEC_POLICY_BYPASS:
578 case IPSEC_POLICY_NONE:
579 /* no need to do IPsec. */
580 ip6obf.needipsec = FALSE;
581 break;
582
583 case IPSEC_POLICY_IPSEC:
584 if (sp->req == NULL) {
585 /* acquire a policy */
586 error = key_spdacquire(sp);
587 goto freehdrs;
588 }
589 if (sp->ipsec_if) {
590 goto skip_ipsec;
591 } else {
592 ip6obf.needipsec = TRUE;
593 }
594 break;
595
596 case IPSEC_POLICY_ENTRUST:
597 default:
598 printf("%s: Invalid policy found: %d\n", __func__, sp->policy);
599 break;
600 }
601 skip_ipsec:
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;
609 if (exthdrs.ip6e_hbh != NULL)
610 optlen += exthdrs.ip6e_hbh->m_len;
611 if (exthdrs.ip6e_dest1 != NULL)
612 optlen += exthdrs.ip6e_dest1->m_len;
613 if (exthdrs.ip6e_rthdr != NULL)
614 optlen += exthdrs.ip6e_rthdr->m_len;
615 unfragpartlen = optlen + sizeof (struct ip6_hdr);
616
617 /* NOTE: we don't add AH/ESP length here. do that later. */
618 if (exthdrs.ip6e_dest2 != NULL)
619 optlen += exthdrs.ip6e_dest2->m_len;
620
621 /*
622 * If we need IPsec, or there is at least one extension header,
623 * separate IP6 header from the payload.
624 */
625 if ((
626 #if IPSEC
627 ip6obf.needipsec ||
628 #endif /* IPSEC */
629 optlen) && !ip6obf.hdrsplit) {
630 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
631 m = NULL;
632 goto freehdrs;
633 }
634 m = exthdrs.ip6e_ip6;
635 ip6obf.hdrsplit = TRUE;
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;
643 plen = m->m_pkthdr.len - sizeof (*ip6);
644
645 /* If this is a jumbo payload, insert a jumbo payload option. */
646 if (plen > IPV6_MAXPACKET) {
647 if (!ip6obf.hdrsplit) {
648 if ((error = ip6_splithdr(m, &exthdrs)) != 0) {
649 m = NULL;
650 goto freehdrs;
651 }
652 m = exthdrs.ip6e_ip6;
653 ip6obf.hdrsplit = TRUE;
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;
660 } else {
661 ip6->ip6_plen = htons(plen);
662 }
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 */
673 nexthdrp = &ip6->ip6_nxt;
674 mprev = m;
675
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);
717
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;
769 }
770 }
771 }
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 }
779
780 #if IPSEC
781 if (ip6obf.needipsec) {
782 int segleft_org;
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
791 if (exthdrs.ip6e_rthdr != NULL) {
792 rh = mtod(exthdrs.ip6e_rthdr, struct ip6_rthdr *);
793 segleft_org = rh->ip6r_segleft;
794 rh->ip6r_segleft = 0;
795 } else {
796 rh = NULL;
797 segleft_org = 0;
798 }
799
800 ipsec_state.m = m;
801 error = ipsec6_output_trans(&ipsec_state, nexthdrp, mprev,
802 sp, flags, &needipsectun);
803 m = ipsec_state.m;
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:
815 printf("ip6_output (ipsec): error code %d\n",
816 error);
817 /* FALLTHRU */
818 case ENOENT:
819 /* don't show these error codes to the user */
820 error = 0;
821 break;
822 }
823 goto bad;
824 }
825 if (exthdrs.ip6e_rthdr != NULL) {
826 /* ah6_output doesn't modify mbuf chain */
827 rh->ip6r_segleft = segleft_org;
828 }
829 }
830 #endif /* IPSEC */
831
832 /*
833 * If there is a routing header, replace the destination address field
834 * with the first hop of the routing header.
835 */
836 if (exthdrs.ip6e_rthdr != NULL) {
837 struct ip6_rthdr0 *rh0;
838 struct in6_addr *addr;
839 struct sockaddr_in6 sa;
840
841 rh = (struct ip6_rthdr *)
842 (mtod(exthdrs.ip6e_rthdr, struct ip6_rthdr *));
843 switch (rh->ip6r_type) {
844 case IPV6_RTHDR_TYPE_0:
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;
873 default: /* is it possible? */
874 error = EINVAL;
875 goto bad;
876 }
877 }
878
879 /* Source address validation */
880 if (IN6_IS_ADDR_UNSPECIFIED(&ip6->ip6_src) &&
881 !(flags & IPV6_UNSPECSRC)) {
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 */
897 if (ro == NULL) {
898 ro = &ip6route;
899 bzero((caddr_t)ro, sizeof (*ro));
900 }
901 VERIFY(ro_pmtu == NULL); /* must not get here if dummynet */
902 ro_pmtu = ro;
903 if (opt != NULL && opt->ip6po_rthdr)
904 ro = &opt->ip6po_route;
905 dst = SIN6(&ro->ro_dst);
906
907 if (ro->ro_rt != NULL)
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 */
914 if (opt != NULL && opt->ip6po_tclass >= 0) {
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;
921 if (mask != 0) {
922 ip6->ip6_flow |=
923 htonl((opt->ip6po_tclass & mask) << 20);
924 }
925 }
926
927 /* fill in or override the hop limit field, if necessary. */
928 if (opt && opt->ip6po_hlim != -1) {
929 ip6->ip6_hlim = opt->ip6po_hlim & 0xff;
930 } else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
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
940 /*
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.
947 */
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
952 if (ro->ro_rt == NULL) {
953 bzero(dst, sizeof (*dst));
954 dst->sin6_family = AF_INET6;
955 dst->sin6_len = sizeof (struct sockaddr_in6);
956 dst->sin6_addr = ip6->ip6_dst;
957 }
958 #if IPSEC
959 if (ip6obf.needipsec && needipsectun) {
960 #if CONFIG_DTRACE
961 struct ifnet *trace_ifp = (ifpp != NULL) ? (*ifpp) : NULL;
962 #endif /* CONFIG_DTRACE */
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 */
971 bzero(&exthdrs, sizeof (exthdrs));
972 exthdrs.ip6e_ip6 = m;
973
974 ipsec_state.m = m;
975 route_copyout(&ipsec_state.ro, (struct route *)ro,
976 sizeof (ipsec_state.ro));
977 ipsec_state.dst = SA(dst);
978
979 /* So that we can see packets inside the tunnel */
980 DTRACE_IP6(send, struct mbuf *, m, struct inpcb *, NULL,
981 struct ip6_hdr *, ip6, struct ifnet *, trace_ifp,
982 struct ip *, NULL, struct ip6_hdr *, ip6);
983
984 error = ipsec6_output_tunnel(&ipsec_state, sp, flags);
985 /* tunneled in IPv4? packet is gone */
986 if (ipsec_state.tunneled == 4)
987 goto done;
988 m = ipsec_state.m;
989 ipsec_saved_route = ro;
990 ro = (struct route_in6 *)&ipsec_state.ro;
991 dst = SIN6(ipsec_state.dst);
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:
1004 printf("ip6_output (ipsec): error code %d\n",
1005 error);
1006 /* FALLTHRU */
1007 case ENOENT:
1008 /* don't show these error codes to the user */
1009 error = 0;
1010 break;
1011 }
1012 goto bad;
1013 }
1014 /*
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.
1018 */
1019 if (flags & IPV6_OUTARGS) {
1020 ip6oa->ip6oa_boundif = IFSCOPE_NONE;
1021 ip6oa->ip6oa_flags &= ~IP6OAF_BOUND_IF;
1022 }
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 }
1027 exthdrs.ip6e_ip6 = m;
1028 }
1029 #endif /* IPSEC */
1030
1031 /* for safety */
1032 if (ifp != NULL) {
1033 ifnet_release(ifp);
1034 ifp = NULL;
1035 }
1036
1037 /* adjust pointer */
1038 ip6 = mtod(m, struct ip6_hdr *);
1039
1040 if (ip6obf.select_srcif) {
1041 bzero(&src_sa, sizeof (src_sa));
1042 src_sa.sin6_family = AF_INET6;
1043 src_sa.sin6_len = sizeof (src_sa);
1044 src_sa.sin6_addr = ip6->ip6_src;
1045 }
1046 bzero(&dst_sa, sizeof (dst_sa));
1047 dst_sa.sin6_family = AF_INET6;
1048 dst_sa.sin6_len = sizeof (dst_sa);
1049 dst_sa.sin6_addr = ip6->ip6_dst;
1050
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.
1054 * ip6oa may be NULL if IPV6_OUTARGS isn't set.
1055 */
1056 if ((error = in6_selectroute(ip6obf.select_srcif ? &src_sa : NULL,
1057 &dst_sa, opt, im6o, &src_ia, ro, &ifp, &rt, 0, ip6oa)) != 0) {
1058 switch (error) {
1059 case EHOSTUNREACH:
1060 ip6stat.ip6s_noroute++;
1061 break;
1062 case EADDRNOTAVAIL:
1063 default:
1064 break; /* XXX statistics? */
1065 }
1066 if (ifp != NULL)
1067 in6_ifstat_inc(ifp, ifs6_out_discard);
1068 /* ifp (if non-NULL) will be released at the end */
1069 goto bad;
1070 }
1071 if (rt == NULL) {
1072 /*
1073 * If in6_selectroute() does not return a route entry,
1074 * dst may not have been updated.
1075 */
1076 *dst = dst_sa; /* XXX */
1077 }
1078
1079 /*
1080 * then rt (for unicast) and ifp must be non-NULL valid values.
1081 */
1082 if (!(flags & IPV6_FORWARDING)) {
1083 in6_ifstat_inc_na(ifp, ifs6_out_request);
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 }
1093
1094 /*
1095 * The outgoing interface must be in the zone of source and
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.
1099 */
1100 if (ia != NULL && ia->ia_ifp) {
1101 ifnet_reference(ia->ia_ifp); /* for origifp */
1102 if (origifp != NULL)
1103 ifnet_release(origifp);
1104 origifp = ia->ia_ifp;
1105 } else {
1106 if (ifp != NULL)
1107 ifnet_reference(ifp); /* for origifp */
1108 if (origifp != NULL)
1109 ifnet_release(origifp);
1110 origifp = ifp;
1111 }
1112
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;
1117
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
1144 badscope:
1145 ip6stat.ip6s_badscope++;
1146 in6_ifstat_inc(origifp, ifs6_out_discard);
1147 if (error == 0)
1148 error = EHOSTUNREACH; /* XXX */
1149 goto bad;
1150 }
1151
1152 routefound:
1153 if (rt != NULL && !IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) {
1154 if (opt != NULL && opt->ip6po_nextroute.ro_rt) {
1155 /*
1156 * The nexthop is explicitly specified by the
1157 * application. We assume the next hop is an IPv6
1158 * address.
1159 */
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);
1183 }
1184 }
1185
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;
1190
1191 m->m_flags = (m->m_flags & ~M_BCAST) | M_MCAST;
1192 in6_ifstat_inc_na(ifp, ifs6_out_mcast);
1193
1194 /*
1195 * Confirm that the outgoing interface supports multicast.
1196 */
1197 if (!(ifp->if_flags & IFF_MULTICAST)) {
1198 ip6stat.ip6s_noroute++;
1199 in6_ifstat_inc(ifp, ifs6_out_discard);
1200 error = ENETUNREACH;
1201 goto bad;
1202 }
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);
1208 if (in6m != NULL &&
1209 (im6o == NULL || im6o->im6o_multicast_loop)) {
1210 if (im6o != NULL)
1211 IM6O_UNLOCK(im6o);
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 */
1217 ip6_mloopback(NULL, ifp, m, dst, optlen, nxt0);
1218 } else if (im6o != NULL)
1219 IM6O_UNLOCK(im6o);
1220 if (in6m != NULL)
1221 IN6M_REMREF(in6m);
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 */
1230 if (ip6->ip6_hlim == 0 || (ifp->if_flags & IFF_LOOPBACK) ||
1231 IN6_IS_ADDR_MC_INTFACELOCAL(&ip6->ip6_dst)) {
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 */
1241 if (ifpp != NULL) {
1242 ifnet_reference(ifp); /* for caller */
1243 if (*ifpp != NULL)
1244 ifnet_release(*ifpp);
1245 *ifpp = ifp;
1246 }
1247
1248 /* Determine path MTU. */
1249 if ((error = ip6_getpmtu(ro_pmtu, ro, ifp, &finaldst, &mtu,
1250 &alwaysfrag)) != 0)
1251 goto bad;
1252
1253 /*
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.
1264 */
1265 if (mtu > IPV6_MMTU) {
1266 if ((flags & IPV6_MINMTU)) {
1267 mtu = IPV6_MMTU;
1268 } else if (opt && opt->ip6po_minmtu == IP6PO_MINMTU_ALL) {
1269 mtu = IPV6_MMTU;
1270 } else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
1271 (opt == NULL ||
1272 opt->ip6po_minmtu != IP6PO_MINMTU_DISABLE)) {
1273 mtu = IPV6_MMTU;
1274 }
1275 }
1276
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);
1283
1284 #if IPFW2
1285 /*
1286 * Check with the firewall...
1287 */
1288 if (ip6_fw_enable && ip6_fw_chk_ptr) {
1289 u_short port = 0;
1290 m->m_pkthdr.rcvif = NULL; /* XXX */
1291 /* If ipfw says divert, we have to just drop packet */
1292 if (ip6_fw_chk_ptr(&ip6, ifp, &port, &m)) {
1293 m_freem(m);
1294 goto done;
1295 }
1296 if (m == NULL) {
1297 error = EACCES;
1298 goto done;
1299 }
1300 }
1301 #endif /* IPFW2 */
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 */
1308 if (exthdrs.ip6e_hbh != NULL) {
1309 struct ip6_hbh *hbh = mtod(exthdrs.ip6e_hbh, struct ip6_hbh *);
1310 u_int32_t dummy; /* XXX unused */
1311 uint32_t oplen = 0; /* for ip6_process_hopopts() */
1312 #if DIAGNOSTIC
1313 if ((hbh->ip6h_len + 1) << 3 > exthdrs.ip6e_hbh->m_len)
1314 panic("ip6e_hbh is not continuous");
1315 #endif
1316 /*
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.
1321 */
1322 m->m_flags |= M_LOOP;
1323 m->m_pkthdr.rcvif = ifp;
1324 if (ip6_process_hopopts(m, (u_int8_t *)(hbh + 1),
1325 ((hbh->ip6h_len + 1) << 3) - sizeof (struct ip6_hbh),
1326 &dummy, &oplen) < 0) {
1327 /* m was already freed at this point */
1328 error = EINVAL; /* better error? */
1329 goto done;
1330 }
1331 m->m_flags &= ~M_LOOP; /* XXX */
1332 m->m_pkthdr.rcvif = NULL;
1333 }
1334
1335 #if DUMMYNET
1336 check_with_pf:
1337 #endif /* DUMMYNET */
1338 #if PF
1339 if (PF_IS_ENABLED) {
1340 #if DUMMYNET
1341 /*
1342 * TODO: Need to save opt->ip6po_flags for reinjection
1343 * rdar://10434993
1344 */
1345 args.fwa_m = m;
1346 args.fwa_oif = ifp;
1347 args.fwa_oflags = flags;
1348 if (flags & IPV6_OUTARGS)
1349 args.fwa_ip6oa = ip6oa;
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;
1358 /* Invoke outbound packet filter */
1359 error = pf_af_hook(ifp, NULL, &m, AF_INET6, FALSE, &args);
1360 #else /* !DUMMYNET */
1361 error = pf_af_hook(ifp, NULL, &m, AF_INET6, FALSE, NULL);
1362 #endif /* !DUMMYNET */
1363
1364 if (error != 0 || m == NULL) {
1365 /*
1366 * Note that if we ever handle packet chain, we will
1367 * have to restore the linkage from the previous
1368 * packet to the next like in ip_outout_list()
1369 */
1370 if (m != NULL) {
1371 panic("%s: unexpected packet %p\n",
1372 __func__, m);
1373 /* NOTREACHED */
1374 }
1375 /* Already freed by callee */
1376 goto done;
1377 }
1378 ip6 = mtod(m, struct ip6_hdr *);
1379 }
1380 #endif /* PF */
1381
1382 /*
1383 * Send the packet to the outgoing interface.
1384 * If necessary, do IPv6 fragmentation before sending.
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
1400 */
1401 tlen = m->m_pkthdr.len;
1402
1403 if (opt != NULL && (opt->ip6po_flags & IP6PO_DONTFRAG))
1404 ip6obf.dontfrag = TRUE;
1405 else
1406 ip6obf.dontfrag = FALSE;
1407 if (ip6obf.dontfrag && alwaysfrag) { /* case 4 */
1408 /* conflicting request - can't transmit */
1409 error = EMSGSIZE;
1410 goto bad;
1411 }
1412
1413 lck_rw_lock_shared(nd_if_rwlock);
1414 /* Access without acquiring nd_ifinfo lock for performance */
1415 if (ip6obf.dontfrag && tlen > IN6_LINKMTU(ifp)) { /* case 2-b */
1416 lck_rw_done(nd_if_rwlock);
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;
1429 bzero(&ip6cp, sizeof (ip6cp));
1430 ip6cp.ip6c_cmdarg = (void *)&mtu32;
1431 pfctlinput2(PRC_MSGSIZE, SA(&ro_pmtu->ro_dst), (void *)&ip6cp);
1432 error = EMSGSIZE;
1433 goto bad;
1434 } else {
1435 lck_rw_done(nd_if_rwlock);
1436 }
1437
1438 /*
1439 * transmit packet without fragmentation
1440 */
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)))) {
1444 #ifdef IPSEC
1445 /* clean ipsec history once it goes out of the node */
1446 ipsec_delaux(m);
1447 #endif /* IPSEC */
1448
1449 ip6_output_checksum(ifp, mtu, m, nxt0, tlen, optlen);
1450
1451 if (ro->ro_rt)
1452 RT_LOCK_ASSERT_NOTHELD(ro->ro_rt);
1453 error = nd6_output(ifp, origifp, m, dst, ro->ro_rt, adv);
1454 goto done;
1455 }
1456
1457 /*
1458 * try to fragment the packet. case 1-b and 3
1459 */
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) {
1466 /* path MTU cannot be less than IPV6_MMTU */
1467 error = EMSGSIZE;
1468 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1469 goto bad;
1470 } else if (ip6->ip6_plen == 0) {
1471 /* jumbo payload cannot be fragmented */
1472 error = EMSGSIZE;
1473 in6_ifstat_inc(ifp, ifs6_out_fragfail);
1474 goto bad;
1475 } else {
1476 struct mbuf **mnext, *m_frgpart;
1477 struct ip6_frag *ip6f;
1478 u_int32_t id = htonl(ip6_randomid());
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;
1489
1490 len = (mtu - hlen - sizeof (struct ip6_frag)) & ~7;
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 */
1503 if (exthdrs.ip6e_rthdr != NULL) {
1504 nextproto = *mtod(exthdrs.ip6e_rthdr, u_char *);
1505 *mtod(exthdrs.ip6e_rthdr, u_char *) = IPPROTO_FRAGMENT;
1506 } else if (exthdrs.ip6e_dest1 != NULL) {
1507 nextproto = *mtod(exthdrs.ip6e_dest1, u_char *);
1508 *mtod(exthdrs.ip6e_dest1, u_char *) = IPPROTO_FRAGMENT;
1509 } else if (exthdrs.ip6e_hbh != NULL) {
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
1517 if (m->m_pkthdr.csum_flags & CSUM_DELAY_IPV6_DATA)
1518 in6_delayed_cksum_offset(m, 0, optlen, nxt0);
1519
1520 /*
1521 * Loop through length of segment after first fragment,
1522 * make new header and copy data of each part and link onto
1523 * chain.
1524 */
1525 m0 = m;
1526 for (off = hlen; off < tlen; off += len) {
1527 struct ip6_hdr *mhip6;
1528
1529 MGETHDR(m, M_DONTWAIT, MT_HEADER); /* MAC-OK */
1530 if (m == NULL) {
1531 error = ENOBUFS;
1532 ip6stat.ip6s_odropped++;
1533 goto sendorfree;
1534 }
1535 m->m_pkthdr.rcvif = NULL;
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;
1542 m->m_len = sizeof (*mhip6);
1543 error = ip6_insertfraghdr(m0, m, hlen, &ip6f);
1544 if (error) {
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 +
1554 sizeof (*ip6f) - sizeof (struct ip6_hdr)));
1555 if ((m_frgpart = m_copy(m0, off, len)) == NULL) {
1556 error = ENOBUFS;
1557 ip6stat.ip6s_odropped++;
1558 goto sendorfree;
1559 }
1560 m_cat(m, m_frgpart);
1561 m->m_pkthdr.len = len + hlen + sizeof (*ip6f);
1562 m->m_pkthdr.rcvif = NULL;
1563
1564 M_COPY_CLASSIFIER(m, m0);
1565 M_COPY_PFTAG(m, m0);
1566
1567 #ifdef notyet
1568 #if CONFIG_MACF_NET
1569 mac_create_fragment(m0, m);
1570 #endif /* CONFIG_MACF_NET */
1571 #endif /* notyet */
1572
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 */
1586 sendorfree:
1587 m = m0->m_nextpkt;
1588 m0->m_nextpkt = NULL;
1589 m_freem(m0);
1590 for (m0 = m; m != NULL; m = m0) {
1591 m0 = m->m_nextpkt;
1592 m->m_nextpkt = NULL;
1593 if (error == 0) {
1594 #if IPSEC
1595 /* clean ipsec history once it goes out of the node */
1596 ipsec_delaux(m);
1597 #endif /* IPSEC */
1598 error = nd6_output(ifp, origifp, m, dst, ro->ro_rt,
1599 adv);
1600 } else {
1601 m_freem(m);
1602 }
1603 }
1604
1605 if (error == 0)
1606 ip6stat.ip6s_fragmented++;
1607
1608 done:
1609 ROUTE_RELEASE(&ip6route);
1610 #if IPSEC
1611 ROUTE_RELEASE(&ipsec_state.ro);
1612 if (sp != NULL)
1613 key_freesp(sp, KEY_SADB_UNLOCKED);
1614 #endif /* IPSEC */
1615 #if NECP
1616 ROUTE_RELEASE(&necp_route);
1617 #endif /* NECP */
1618 #if DUMMYNET
1619 ROUTE_RELEASE(&saved_route);
1620 ROUTE_RELEASE(&saved_ro_pmtu);
1621 #endif /* DUMMYNET */
1622
1623 if (ia != NULL)
1624 IFA_REMREF(&ia->ia_ifa);
1625 if (src_ia != NULL)
1626 IFA_REMREF(&src_ia->ia_ifa);
1627 if (ifp != NULL)
1628 ifnet_release(ifp);
1629 if (origifp != NULL)
1630 ifnet_release(origifp);
1631 return (error);
1632
1633 freehdrs:
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 */
1643 bad:
1644 if (m != NULL)
1645 m_freem(m);
1646 goto done;
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
1655 }
1656
1657 static int
1658 ip6_copyexthdr(struct mbuf **mp, caddr_t hdr, int hlen)
1659 {
1660 struct mbuf *m;
1661
1662 if (hlen > MCLBYTES)
1663 return (ENOBUFS); /* XXX */
1664
1665 MGET(m, M_DONTWAIT, MT_DATA);
1666 if (m == NULL)
1667 return (ENOBUFS);
1668
1669 if (hlen > MLEN) {
1670 MCLGET(m, M_DONTWAIT);
1671 if (!(m->m_flags & M_EXT)) {
1672 m_free(m);
1673 return (ENOBUFS);
1674 }
1675 }
1676 m->m_len = hlen;
1677 if (hdr != NULL)
1678 bcopy(hdr, mtod(m, caddr_t), hlen);
1679
1680 *mp = m;
1681 return (0);
1682 }
1683
1684 static void
1685 ip6_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
1700 /*
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.
1710 */
1711 uint32_t
1712 in6_finalize_cksum(struct mbuf *m, uint32_t hoff, int32_t optlen,
1713 int32_t nxt0, uint32_t csum_flags)
1714 {
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;
1720
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;
1782 }
1783
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)) {
1831 *(uint16_t *)(void *)(mtod(m, char *) + offset) = csum;
1832 } else {
1833 bcopy(&csum, (mtod(m, char *) + offset), sizeof (csum));
1834 }
1835 m->m_pkthdr.csum_flags &=
1836 ~(CSUM_DELAY_IPV6_DATA | CSUM_DATA_VALID | CSUM_PARTIAL);
1837
1838 done:
1839 return (sw_csum);
1840 }
1841
1842 /*
1843 * Insert jumbo payload option.
1844 */
1845 static int
1846 ip6_insert_jumboopt(struct ip6_exthdrs *exthdrs, u_int32_t plen)
1847 {
1848 struct mbuf *mopt;
1849 u_char *optbuf;
1850 u_int32_t v;
1851
1852 #define JUMBOOPTLEN 8 /* length of jumbo payload option and padding */
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 */
1860 if (exthdrs->ip6e_hbh == NULL) {
1861 MGET(mopt, M_DONTWAIT, MT_DATA);
1862 if (mopt == NULL)
1863 return (ENOBUFS);
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) {
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 */
1879 u_int32_t oldoptlen = mopt->m_len;
1880 struct mbuf *n;
1881
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)
1887 return (ENOBUFS);
1888
1889 /*
1890 * As a consequence, we must always prepare a cluster
1891 * at this point.
1892 */
1893 MGET(n, M_DONTWAIT, MT_DATA);
1894 if (n != NULL) {
1895 MCLGET(n, M_DONTWAIT);
1896 if (!(n->m_flags & M_EXT)) {
1897 m_freem(n);
1898 n = NULL;
1899 }
1900 }
1901 if (n == NULL)
1902 return (ENOBUFS);
1903 n->m_len = oldoptlen + JUMBOOPTLEN;
1904 bcopy(mtod(mopt, caddr_t), mtod(n, caddr_t),
1905 oldoptlen);
1906 optbuf = mtod(n, u_char *) + oldoptlen;
1907 m_freem(mopt);
1908 mopt = exthdrs->ip6e_hbh = n;
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;
1927 v = (u_int32_t)htonl(plen + JUMBOOPTLEN);
1928 bcopy(&v, &optbuf[4], sizeof (u_int32_t));
1929
1930 /* finally, adjust the packet header length */
1931 exthdrs->ip6e_ip6->m_pkthdr.len += JUMBOOPTLEN;
1932
1933 return (0);
1934 #undef JUMBOOPTLEN
1935 }
1936
1937 /*
1938 * Insert fragment header and copy unfragmentable header portions.
1939 */
1940 static int
1941 ip6_insertfraghdr(struct mbuf *m0, struct mbuf *m, int hlen,
1942 struct ip6_frag **frghdrp)
1943 {
1944 struct mbuf *n, *mlast;
1945
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)
1950 return (ENOBUFS);
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
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 */
1962 *frghdrp = (struct ip6_frag *)(mtod(mlast, caddr_t) +
1963 mlast->m_len);
1964 mlast->m_len += sizeof (struct ip6_frag);
1965 m->m_pkthdr.len += sizeof (struct ip6_frag);
1966 } else {
1967 /* allocate a new mbuf for the fragment header */
1968 struct mbuf *mfrg;
1969
1970 MGET(mfrg, M_DONTWAIT, MT_DATA);
1971 if (mfrg == NULL)
1972 return (ENOBUFS);
1973 mfrg->m_len = sizeof (struct ip6_frag);
1974 *frghdrp = mtod(mfrg, struct ip6_frag *);
1975 mlast->m_next = mfrg;
1976 }
1977
1978 return (0);
1979 }
1980
1981 static int
1982 ip6_getpmtu(struct route_in6 *ro_pmtu, struct route_in6 *ro,
1983 struct ifnet *ifp, struct in6_addr *dst, u_int32_t *mtup,
1984 boolean_t *alwaysfragp)
1985 {
1986 u_int32_t mtu = 0;
1987 boolean_t alwaysfrag = FALSE;
1988 int error = 0;
1989
1990 if (ro_pmtu != ro) {
1991 /* The first hop and the final destination may differ. */
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
1997 if (ro_pmtu->ro_rt == NULL) {
1998 bzero(sa6_dst, sizeof (*sa6_dst));
1999 sa6_dst->sin6_family = AF_INET6;
2000 sa6_dst->sin6_len = sizeof (struct sockaddr_in6);
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
2008 if (ro_pmtu->ro_rt != NULL) {
2009 u_int32_t ifmtu;
2010
2011 if (ifp == NULL)
2012 ifp = ro_pmtu->ro_rt->rt_ifp;
2013 lck_rw_lock_shared(nd_if_rwlock);
2014 /* Access without acquiring nd_ifinfo lock for performance */
2015 ifmtu = IN6_LINKMTU(ifp);
2016 lck_rw_done(nd_if_rwlock);
2017
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 */
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 */
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) {
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 */
2048 alwaysfrag = TRUE;
2049 mtu = IPV6_MMTU;
2050 }
2051 } else {
2052 if (ifp) {
2053 lck_rw_lock_shared(nd_if_rwlock);
2054 /* Don't hold nd_ifinfo lock for performance */
2055 mtu = IN6_LINKMTU(ifp);
2056 lck_rw_done(nd_if_rwlock);
2057 } else {
2058 error = EHOSTUNREACH; /* XXX */
2059 }
2060 }
2061
2062 *mtup = mtu;
2063 if (alwaysfragp != NULL)
2064 *alwaysfragp = alwaysfrag;
2065 return (error);
2066 }
2067
2068 /*
2069 * IP6 socket option processing.
2070 */
2071 int
2072 ip6_ctloutput(struct socket *so, struct sockopt *sopt)
2073 {
2074 int optdatalen, uproto;
2075 void *optdata;
2076 int privileged;
2077 struct inpcb *in6p = sotoinpcb(so);
2078 int error = 0, optval = 0;
2079 int level, op = -1, optname = 0;
2080 int optlen = 0;
2081 struct proc *p;
2082
2083 VERIFY(sopt != NULL);
2084
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;
2090 uproto = (int)SOCK_PROTO(so);
2091
2092 privileged = (proc_suser(p) == 0);
2093
2094 if (level == IPPROTO_IPV6) {
2095 switch (op) {
2096 case SOPT_SET:
2097 switch (optname) {
2098 case IPV6_2292PKTOPTIONS: {
2099 struct mbuf *m;
2100
2101 error = soopt_getm(sopt, &m);
2102 if (error != 0)
2103 break;
2104 error = soopt_mcopyin(sopt, m);
2105 if (error != 0)
2106 break;
2107 error = ip6_pcbopts(&in6p->in6p_outputopts,
2108 m, so, sopt);
2109 m_freem(m);
2110 break;
2111 }
2112
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 */
2126 case IPV6_RECVHOPOPTS:
2127 case IPV6_RECVDSTOPTS:
2128 case IPV6_RECVRTHDRDSTOPTS:
2129 if (!privileged)
2130 break;
2131 /* FALLTHROUGH */
2132 case IPV6_UNICAST_HOPS:
2133 case IPV6_HOPLIMIT:
2134 case IPV6_RECVPKTINFO:
2135 case IPV6_RECVHOPLIMIT:
2136 case IPV6_RECVRTHDR:
2137 case IPV6_RECVPATHMTU:
2138 case IPV6_RECVTCLASS:
2139 case IPV6_V6ONLY:
2140 case IPV6_AUTOFLOWLABEL:
2141 if (optlen != sizeof (int)) {
2142 error = EINVAL;
2143 break;
2144 }
2145 error = sooptcopyin(sopt, &optval,
2146 sizeof (optval), sizeof (optval));
2147 if (error)
2148 break;
2149
2150 switch (optname) {
2151 case IPV6_UNICAST_HOPS:
2152 if (optval < -1 || optval >= 256) {
2153 error = EINVAL;
2154 } else {
2155 /* -1 = kernel default */
2156 in6p->in6p_hops = optval;
2157 if (in6p->inp_vflag &
2158 INP_IPV4) {
2159 in6p->inp_ip_ttl =
2160 optval;
2161 }
2162 }
2163 break;
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)
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
2190 case IPV6_HOPLIMIT: {
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,
2200 (u_char *)&optval, sizeof (optval),
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;
2231
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);
2248 break;
2249
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);
2259 break;
2260
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 */
2267 if (in6p->inp_lport ||
2268 !IN6_IS_ADDR_UNSPECIFIED(
2269 &in6p->in6p_laddr)) {
2270 error = EINVAL;
2271 break;
2272 }
2273 OPTSET(IN6P_IPV6_V6ONLY);
2274 if (optval)
2275 in6p->inp_vflag &= ~INP_IPV4;
2276 else
2277 in6p->inp_vflag |= INP_IPV4;
2278 break;
2279
2280 case IPV6_RECVTCLASS:
2281 /* we can mix with RFC2292 */
2282 OPTSET(IN6P_TCLASS);
2283 break;
2284
2285 case IPV6_AUTOFLOWLABEL:
2286 OPTSET(IN6P_AUTOFLOWLABEL);
2287 break;
2288
2289 }
2290 break;
2291
2292 case IPV6_TCLASS:
2293 case IPV6_DONTFRAG:
2294 case IPV6_USE_MIN_MTU:
2295 case IPV6_PREFER_TEMPADDR: {
2296 struct ip6_pktopts **optp;
2297
2298 if (optlen != sizeof (optval)) {
2299 error = EINVAL;
2300 break;
2301 }
2302 error = sooptcopyin(sopt, &optval,
2303 sizeof (optval), sizeof (optval));
2304 if (error)
2305 break;
2306
2307 optp = &in6p->in6p_outputopts;
2308 error = ip6_pcbopt(optname, (u_char *)&optval,
2309 sizeof (optval), optp, uproto);
2310 break;
2311 }
2312
2313 case IPV6_2292PKTINFO:
2314 case IPV6_2292HOPLIMIT:
2315 case IPV6_2292HOPOPTS:
2316 case IPV6_2292DSTOPTS:
2317 case IPV6_2292RTHDR:
2318 /* RFC 2292 */
2319 if (optlen != sizeof (int)) {
2320 error = EINVAL;
2321 break;
2322 }
2323 error = sooptcopyin(sopt, &optval,
2324 sizeof (optval), sizeof (optval));
2325 if (error)
2326 break;
2327 switch (optname) {
2328 case IPV6_2292PKTINFO:
2329 OPTSET2292(IN6P_PKTINFO);
2330 break;
2331 case IPV6_2292HOPLIMIT:
2332 OPTSET2292(IN6P_HOPLIMIT);
2333 break;
2334 case IPV6_2292HOPOPTS:
2335 /*
2336 * Check super-user privilege.
2337 * See comments for IPV6_RECVHOPOPTS.
2338 */
2339 if (!privileged)
2340 return (EPERM);
2341 OPTSET2292(IN6P_HOPOPTS);
2342 break;
2343 case IPV6_2292DSTOPTS:
2344 if (!privileged)
2345 return (EPERM);
2346 OPTSET2292(IN6P_DSTOPTS|
2347 IN6P_RTHDRDSTOPTS); /* XXX */
2348 break;
2349 case IPV6_2292RTHDR:
2350 OPTSET2292(IN6P_RTHDR);
2351 break;
2352 }
2353 break;
2354
2355 case IPV6_3542PKTINFO:
2356 case IPV6_3542HOPOPTS:
2357 case IPV6_3542RTHDR:
2358 case IPV6_3542DSTOPTS:
2359 case IPV6_RTHDRDSTOPTS:
2360 case IPV6_3542NEXTHOP: {
2361 struct ip6_pktopts **optp;
2362 /* new advanced API (RFC3542) */
2363 struct mbuf *m;
2364
2365 /* cannot mix with RFC2292 */
2366 if (OPTBIT(IN6P_RFC2292)) {
2367 error = EINVAL;
2368 break;
2369 }
2370 error = soopt_getm(sopt, &m);
2371 if (error != 0)
2372 break;
2373 error = soopt_mcopyin(sopt, m);
2374 if (error != 0)
2375 break;
2376
2377 optp = &in6p->in6p_outputopts;
2378 error = ip6_pcbopt(optname, mtod(m, u_char *),
2379 m->m_len, optp, uproto);
2380 m_freem(m);
2381 break;
2382 }
2383 #undef OPTSET
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:
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);
2397 break;
2398
2399 case IPV6_PORTRANGE:
2400 error = sooptcopyin(sopt, &optval,
2401 sizeof (optval), sizeof (optval));
2402 if (error)
2403 break;
2404
2405 switch (optval) {
2406 case IPV6_PORTRANGE_DEFAULT:
2407 in6p->inp_flags &= ~(INP_LOWPORT);
2408 in6p->inp_flags &= ~(INP_HIGHPORT);
2409 break;
2410
2411 case IPV6_PORTRANGE_HIGH:
2412 in6p->inp_flags &= ~(INP_LOWPORT);
2413 in6p->inp_flags |= INP_HIGHPORT;
2414 break;
2415
2416 case IPV6_PORTRANGE_LOW:
2417 in6p->inp_flags &= ~(INP_HIGHPORT);
2418 in6p->inp_flags |= INP_LOWPORT;
2419 break;
2420
2421 default:
2422 error = EINVAL;
2423 break;
2424 }
2425 break;
2426 #if IPSEC
2427 case IPV6_IPSEC_POLICY: {
2428 caddr_t req = NULL;
2429 size_t len = 0;
2430 struct mbuf *m;
2431
2432 if ((error = soopt_getm(sopt, &m)) != 0)
2433 break;
2434 if ((error = soopt_mcopyin(sopt, m)) != 0)
2435 break;
2436
2437 req = mtod(m, caddr_t);
2438 len = m->m_len;
2439 error = ipsec6_set_policy(in6p, optname, req,
2440 len, privileged);
2441 m_freem(m);
2442 break;
2443 }
2444 #endif /* IPSEC */
2445 #if IPFIREWALL
2446 case IPV6_FW_ADD:
2447 case IPV6_FW_DEL:
2448 case IPV6_FW_FLUSH:
2449 case IPV6_FW_ZERO: {
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
2455 error = ENOPROTOOPT;
2456 break;
2457 }
2458 #endif /* IPFIREWALL */
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
2476 error = inp_bindif(in6p, optval, NULL);
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
2492 /* once set, it cannot be unset */
2493 if (!optval && INP_NO_CELLULAR(in6p)) {
2494 error = EINVAL;
2495 break;
2496 }
2497
2498 error = so_set_restrictions(so,
2499 SO_RESTRICT_DENY_CELLULAR);
2500 break;
2501
2502 case IPV6_OUT_IF:
2503 /* This option is not settable */
2504 error = EINVAL;
2505 break;
2506
2507 default:
2508 error = ENOPROTOOPT;
2509 break;
2510 }
2511 break;
2512
2513 case SOPT_GET:
2514 switch (optname) {
2515
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;
2527 break;
2528
2529 case IPV6_RECVHOPOPTS:
2530 case IPV6_RECVDSTOPTS:
2531 case IPV6_RECVRTHDRDSTOPTS:
2532 case IPV6_UNICAST_HOPS:
2533 case IPV6_RECVPKTINFO:
2534 case IPV6_RECVHOPLIMIT:
2535 case IPV6_RECVRTHDR:
2536 case IPV6_RECVPATHMTU:
2537 case IPV6_V6ONLY:
2538 case IPV6_PORTRANGE:
2539 case IPV6_RECVTCLASS:
2540 case IPV6_AUTOFLOWLABEL:
2541 switch (optname) {
2542
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
2555 case IPV6_UNICAST_HOPS:
2556 optval = in6p->in6p_hops;
2557 break;
2558
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);
2573 break;
2574
2575 case IPV6_V6ONLY:
2576 optval = OPTBIT(IN6P_IPV6_V6ONLY);
2577 break;
2578
2579 case IPV6_PORTRANGE: {
2580 int flags;
2581 flags = in6p->inp_flags;
2582 if (flags & INP_HIGHPORT)
2583 optval = IPV6_PORTRANGE_HIGH;
2584 else if (flags & INP_LOWPORT)
2585 optval = IPV6_PORTRANGE_LOW;
2586 else
2587 optval = 0;
2588 break;
2589 }
2590 case IPV6_RECVTCLASS:
2591 optval = OPTBIT(IN6P_TCLASS);
2592 break;
2593
2594 case IPV6_AUTOFLOWLABEL:
2595 optval = OPTBIT(IN6P_AUTOFLOWLABEL);
2596 break;
2597 }
2598 if (error)
2599 break;
2600 error = sooptcopyout(sopt, &optval,
2601 sizeof (optval));
2602 break;
2603
2604 case IPV6_PATHMTU: {
2605 u_int32_t pmtu = 0;
2606 struct ip6_mtuinfo mtuinfo;
2607 struct route_in6 sro;
2608
2609 bzero(&sro, sizeof (sro));
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);
2620 ROUTE_RELEASE(&sro);
2621 if (error)
2622 break;
2623 if (pmtu > IPV6_MAXPACKET)
2624 pmtu = IPV6_MAXPACKET;
2625
2626 bzero(&mtuinfo, sizeof (mtuinfo));
2627 mtuinfo.ip6m_mtu = (u_int32_t)pmtu;
2628 optdata = (void *)&mtuinfo;
2629 optdatalen = sizeof (mtuinfo);
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:
2640 switch (optname) {
2641 case IPV6_2292PKTINFO:
2642 optval = OPTBIT(IN6P_PKTINFO);
2643 break;
2644 case IPV6_2292HOPLIMIT:
2645 optval = OPTBIT(IN6P_HOPLIMIT);
2646 break;
2647 case IPV6_2292HOPOPTS:
2648 optval = OPTBIT(IN6P_HOPOPTS);
2649 break;
2650 case IPV6_2292RTHDR:
2651 optval = OPTBIT(IN6P_RTHDR);
2652 break;
2653 case IPV6_2292DSTOPTS:
2654 optval = OPTBIT(IN6P_DSTOPTS|
2655 IN6P_RTHDRDSTOPTS);
2656 break;
2657 }
2658 error = sooptcopyout(sopt, &optval,
2659 sizeof (optval));
2660 break;
2661
2662 case IPV6_PKTINFO:
2663 case IPV6_HOPOPTS:
2664 case IPV6_RTHDR:
2665 case IPV6_DSTOPTS:
2666 case IPV6_RTHDRDSTOPTS:
2667 case IPV6_NEXTHOP:
2668 case IPV6_TCLASS:
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);
2674 break;
2675
2676 case IPV6_MULTICAST_IF:
2677 case IPV6_MULTICAST_HOPS:
2678 case IPV6_MULTICAST_LOOP:
2679 case IPV6_MSFILTER:
2680 error = ip6_getmoptions(in6p, sopt);
2681 break;
2682 #if IPSEC
2683 case IPV6_IPSEC_POLICY: {
2684 error = 0; /* This option is no longer supported */
2685 break;
2686 }
2687 #endif /* IPSEC */
2688 #if IPFIREWALL
2689 case IPV6_FW_GET: {
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
2695 error = ENOPROTOOPT;
2696 break;
2697 }
2698 #endif /* IPFIREWALL */
2699 case IPV6_BOUND_IF:
2700 if (in6p->inp_flags & INP_BOUND_IF)
2701 optval = in6p->inp_boundifp->if_index;
2702 error = sooptcopyout(sopt, &optval,
2703 sizeof (optval));
2704 break;
2705
2706 case IPV6_NO_IFT_CELLULAR:
2707 optval = INP_NO_CELLULAR(in6p) ? 1 : 0;
2708 error = sooptcopyout(sopt, &optval,
2709 sizeof (optval));
2710 break;
2711
2712 case IPV6_OUT_IF:
2713 optval = (in6p->in6p_last_outifp != NULL) ?
2714 in6p->in6p_last_outifp->if_index : 0;
2715 error = sooptcopyout(sopt, &optval,
2716 sizeof (optval));
2717 break;
2718
2719 default:
2720 error = ENOPROTOOPT;
2721 break;
2722 }
2723 break;
2724 }
2725 } else {
2726 error = EINVAL;
2727 }
2728 return (error);
2729 }
2730
2731 int
2732 ip6_raw_ctloutput(struct socket *so, struct sockopt *sopt)
2733 {
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;
2738
2739 level = sopt->sopt_level;
2740 op = sopt->sopt_dir;
2741 optname = sopt->sopt_name;
2742 optlen = sopt->sopt_valsize;
2743
2744 if (level != IPPROTO_IPV6)
2745 return (EINVAL);
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:
2759 if (optlen != sizeof (int)) {
2760 error = EINVAL;
2761 break;
2762 }
2763 error = sooptcopyin(sopt, &optval, sizeof (optval),
2764 sizeof (optval));
2765 if (error)
2766 break;
2767 if ((optval % 2) != 0) {
2768 /* the API assumes even offset values */
2769 error = EINVAL;
2770 } else if (SOCK_PROTO(so) == IPPROTO_ICMPV6) {
2771 if (optval != icmp6off)
2772 error = EINVAL;
2773 } else {
2774 in6p->in6p_cksum = optval;
2775 }
2776 break;
2777
2778 case SOPT_GET:
2779 if (SOCK_PROTO(so) == IPPROTO_ICMPV6)
2780 optval = icmp6off;
2781 else
2782 optval = in6p->in6p_cksum;
2783
2784 error = sooptcopyout(sopt, &optval, sizeof (optval));
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 */
2805 static int
2806 ip6_pcbopts(struct ip6_pktopts **pktopt, struct mbuf *m, struct socket *so,
2807 struct sockopt *sopt)
2808 {
2809 #pragma unused(sopt)
2810 struct ip6_pktopts *opt = *pktopt;
2811 int error = 0;
2812
2813 /* turn off any old options. */
2814 if (opt != NULL) {
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)
2819 printf("%s: all specified options are cleared.\n",
2820 __func__);
2821 #endif
2822 ip6_clearpktopts(opt, -1);
2823 } else {
2824 opt = _MALLOC(sizeof (*opt), M_IP6OPT, M_WAITOK);
2825 if (opt == NULL)
2826 return (ENOBUFS);
2827 }
2828 *pktopt = NULL;
2829
2830 if (m == NULL || m->m_len == 0) {
2831 /*
2832 * Only turning off any previous options, regardless of
2833 * whether the opt is just created or given.
2834 */
2835 if (opt != NULL)
2836 FREE(opt, M_IP6OPT);
2837 return (0);
2838 }
2839
2840 /* set options specified by user. */
2841 if ((error = ip6_setpktopts(m, opt, NULL, SOCK_PROTO(so))) != 0) {
2842 ip6_clearpktopts(opt, -1); /* XXX: discard all options */
2843 FREE(opt, M_IP6OPT);
2844 return (error);
2845 }
2846 *pktopt = opt;
2847 return (0);
2848 }
2849
2850 /*
2851 * initialize ip6_pktopts. beware that there are non-zero default values in
2852 * the struct.
2853 */
2854 void
2855 ip6_initpktopts(struct ip6_pktopts *opt)
2856 {
2857
2858 bzero(opt, sizeof (*opt));
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
2865 static int
2866 ip6_pcbopt(int optname, u_char *buf, int len, struct ip6_pktopts **pktopt,
2867 int uproto)
2868 {
2869 struct ip6_pktopts *opt;
2870
2871 opt = *pktopt;
2872 if (opt == NULL) {
2873 opt = _MALLOC(sizeof (*opt), M_IP6OPT, M_WAITOK);
2874 if (opt == NULL)
2875 return (ENOBUFS);
2876 ip6_initpktopts(opt);
2877 *pktopt = opt;
2878 }
2879
2880 return (ip6_setpktopt(optname, buf, len, opt, 1, 0, uproto));
2881 }
2882
2883 static int
2884 ip6_getpcbopt(struct ip6_pktopts *pktopt, int optname, struct sockopt *sopt)
2885 {
2886 void *optdata = NULL;
2887 int optdatalen = 0;
2888 struct ip6_ext *ip6e;
2889 struct in6_pktinfo null_pktinfo;
2890 int deftclass = 0, on;
2891 int defminmtu = IP6PO_MINMTU_MCASTONLY;
2892 int defpreftemp = IP6PO_TEMPADDR_SYSTEM;
2893
2894
2895 switch (optname) {
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... */
2901 bzero(&null_pktinfo, sizeof (null_pktinfo));
2902 optdata = (void *)&null_pktinfo;
2903 }
2904 optdatalen = sizeof (struct in6_pktinfo);
2905 break;
2906
2907 case IPV6_TCLASS:
2908 if (pktopt && pktopt->ip6po_tclass >= 0)
2909 optdata = (void *)&pktopt->ip6po_tclass;
2910 else
2911 optdata = (void *)&deftclass;
2912 optdatalen = sizeof (int);
2913 break;
2914
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;
2922
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;
2930
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;
2938
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;
2946
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;
2953
2954 case IPV6_USE_MIN_MTU:
2955 if (pktopt)
2956 optdata = (void *)&pktopt->ip6po_minmtu;
2957 else
2958 optdata = (void *)&defminmtu;
2959 optdatalen = sizeof (int);
2960 break;
2961
2962 case IPV6_DONTFRAG:
2963 if (pktopt && ((pktopt->ip6po_flags) & IP6PO_DONTFRAG))
2964 on = 1;
2965 else
2966 on = 0;
2967 optdata = (void *)&on;
2968 optdatalen = sizeof (on);
2969 break;
2970
2971 case IPV6_PREFER_TEMPADDR:
2972 if (pktopt)
2973 optdata = (void *)&pktopt->ip6po_prefer_tempaddr;
2974 else
2975 optdata = (void *)&defpreftemp;
2976 optdatalen = sizeof (int);
2977 break;
2978
2979 default: /* should not happen */
2980 #ifdef DIAGNOSTIC
2981 panic("ip6_getpcbopt: unexpected option\n");
2982 #endif
2983 return (ENOPROTOOPT);
2984 }
2985
2986 return (sooptcopyout(sopt, optdata, optdatalen));
2987 }
2988
2989 void
2990 ip6_clearpktopts(struct ip6_pktopts *pktopt, int optname)
2991 {
2992 if (pktopt == NULL)
2993 return;
2994
2995 if (optname == -1 || optname == IPV6_PKTINFO) {
2996 if (pktopt->ip6po_pktinfo)
2997 FREE(pktopt->ip6po_pktinfo, M_IP6OPT);
2998 pktopt->ip6po_pktinfo = NULL;
2999 }
3000 if (optname == -1 || optname == IPV6_HOPLIMIT)
3001 pktopt->ip6po_hlim = -1;
3002 if (optname == -1 || optname == IPV6_TCLASS)
3003 pktopt->ip6po_tclass = -1;
3004 if (optname == -1 || optname == IPV6_NEXTHOP) {
3005 ROUTE_RELEASE(&pktopt->ip6po_nextroute);
3006 if (pktopt->ip6po_nexthop)
3007 FREE(pktopt->ip6po_nexthop, M_IP6OPT);
3008 pktopt->ip6po_nexthop = NULL;
3009 }
3010 if (optname == -1 || optname == IPV6_HOPOPTS) {
3011 if (pktopt->ip6po_hbh)
3012 FREE(pktopt->ip6po_hbh, M_IP6OPT);
3013 pktopt->ip6po_hbh = NULL;
3014 }
3015 if (optname == -1 || optname == IPV6_RTHDRDSTOPTS) {
3016 if (pktopt->ip6po_dest1)
3017 FREE(pktopt->ip6po_dest1, M_IP6OPT);
3018 pktopt->ip6po_dest1 = NULL;
3019 }
3020 if (optname == -1 || optname == IPV6_RTHDR) {
3021 if (pktopt->ip6po_rhinfo.ip6po_rhi_rthdr)
3022 FREE(pktopt->ip6po_rhinfo.ip6po_rhi_rthdr, M_IP6OPT);
3023 pktopt->ip6po_rhinfo.ip6po_rhi_rthdr = NULL;
3024 ROUTE_RELEASE(&pktopt->ip6po_route);
3025 }
3026 if (optname == -1 || optname == IPV6_DSTOPTS) {
3027 if (pktopt->ip6po_dest2)
3028 FREE(pktopt->ip6po_dest2, M_IP6OPT);
3029 pktopt->ip6po_dest2 = NULL;
3030 }
3031 }
3032
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 } \
3042 } while (0)
3043
3044 static int
3045 copypktopts(struct ip6_pktopts *dst, struct ip6_pktopts *src, int canwait)
3046 {
3047 if (dst == NULL || src == NULL) {
3048 printf("copypktopts: invalid argument\n");
3049 return (EINVAL);
3050 }
3051
3052 dst->ip6po_hlim = src->ip6po_hlim;
3053 dst->ip6po_tclass = src->ip6po_tclass;
3054 dst->ip6po_flags = src->ip6po_flags;
3055 if (src->ip6po_pktinfo) {
3056 dst->ip6po_pktinfo = _MALLOC(sizeof (*dst->ip6po_pktinfo),
3057 M_IP6OPT, canwait);
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,
3064 M_IP6OPT, canwait);
3065 if (dst->ip6po_nexthop == NULL && canwait == M_NOWAIT)
3066 goto bad;
3067 bcopy(src->ip6po_nexthop, dst->ip6po_nexthop,
3068 src->ip6po_nexthop->sa_len);
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 */
3074 return (0);
3075
3076 bad:
3077 ip6_clearpktopts(dst, -1);
3078 return (ENOBUFS);
3079 }
3080 #undef PKTOPT_EXTHDRCPY
3081
3082 struct ip6_pktopts *
3083 ip6_copypktopts(struct ip6_pktopts *src, int canwait)
3084 {
3085 int error;
3086 struct ip6_pktopts *dst;
3087
3088 dst = _MALLOC(sizeof (*dst), M_IP6OPT, canwait);
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
3101 void
3102 ip6_freepcbopts(struct ip6_pktopts *pktopt)
3103 {
3104 if (pktopt == NULL)
3105 return;
3106
3107 ip6_clearpktopts(pktopt, -1);
3108
3109 FREE(pktopt, M_IP6OPT);
3110 }
3111
3112 void
3113 ip6_moptions_init(void)
3114 {
3115 PE_parse_boot_argn("ifa_debug", &im6o_debug, sizeof (im6o_debug));
3116
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 */
3125 }
3126 zone_change(im6o_zone, Z_EXPAND, TRUE);
3127 }
3128
3129 void
3130 im6o_addref(struct ip6_moptions *im6o, int locked)
3131 {
3132 if (!locked)
3133 IM6O_LOCK(im6o);
3134 else
3135 IM6O_LOCK_ASSERT_HELD(im6o);
3136
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);
3146 }
3147
3148 void
3149 im6o_remref(struct ip6_moptions *im6o)
3150 {
3151 int i;
3152
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 }
3160
3161 --im6o->im6o_refcnt;
3162 if (im6o->im6o_refcnt > 0) {
3163 IM6O_UNLOCK(im6o);
3164 return;
3165 }
3166
3167 for (i = 0; i < im6o->im6o_num_memberships; ++i) {
3168 struct in6_mfilter *imf;
3169
3170 imf = im6o->im6o_mfilters ? &im6o->im6o_mfilters[i] : NULL;
3171 if (imf != NULL)
3172 im6f_leave(imf);
3173
3174 (void) in6_mc_leave(im6o->im6o_membership[i], imf);
3175
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 */
3198 }
3199 zfree(im6o_zone, im6o);
3200 }
3201
3202 static void
3203 im6o_trace(struct ip6_moptions *im6o, int refhold)
3204 {
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;
3209
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
3226 struct ip6_moptions *
3227 ip6_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);
3245 }
3246
3247 /*
3248 * Set IPv6 outgoing packet options based on advanced API.
3249 */
3250 int
3251 ip6_setpktopts(struct mbuf *control, struct ip6_pktopts *opt,
3252 struct ip6_pktopts *stickyopt, int uproto)
3253 {
3254 struct cmsghdr *cm = NULL;
3255
3256 if (control == NULL || opt == NULL)
3257 return (EINVAL);
3258
3259 ip6_initpktopts(opt);
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 }
3275
3276 /*
3277 * XXX: Currently, we assume all the optional information is stored
3278 * in a single mbuf.
3279 */
3280 if (control->m_next)
3281 return (EINVAL);
3282
3283 if (control->m_len < CMSG_LEN(0))
3284 return (EINVAL);
3285
3286 for (cm = M_FIRST_CMSGHDR(control); cm != NULL;
3287 cm = M_NXT_CMSGHDR(control, cm)) {
3288 int error;
3289
3290 if (cm->cmsg_len < sizeof (struct cmsghdr) ||
3291 cm->cmsg_len > control->m_len)
3292 return (EINVAL);
3293 if (cm->cmsg_level != IPPROTO_IPV6)
3294 continue;
3295
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 */
3313 static int
3314 ip6_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
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
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:
3369 case IPV6_PKTINFO: {
3370 struct ifnet *ifp = NULL;
3371 struct in6_pktinfo *pktinfo;
3372
3373 if (len != sizeof (struct in6_pktinfo))
3374 return (EINVAL);
3375
3376 pktinfo = (struct in6_pktinfo *)(void *)buf;
3377
3378 /*
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]
3383 */
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 }
3390
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 }
3403
3404 if (pktinfo->ipi6_ifindex) {
3405 ifp = ifindex2ifnet[pktinfo->ipi6_ifindex];
3406 if (ifp == NULL) {
3407 ifnet_head_done();
3408 return (ENXIO);
3409 }
3410 }
3411
3412 ifnet_head_done();
3413
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) {
3425 opt->ip6po_pktinfo = _MALLOC(sizeof (*pktinfo),
3426 M_IP6OPT, M_NOWAIT);
3427 if (opt->ip6po_pktinfo == NULL)
3428 return (ENOBUFS);
3429 }
3430 bcopy(pktinfo, opt->ip6po_pktinfo, sizeof (*pktinfo));
3431 break;
3432 }
3433
3434 case IPV6_2292HOPLIMIT:
3435 case IPV6_HOPLIMIT: {
3436 int *hlimp;
3437
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);
3444
3445 if (len != sizeof (int))
3446 return (EINVAL);
3447 hlimp = (int *)(void *)buf;
3448 if (*hlimp < -1 || *hlimp > 255)
3449 return (EINVAL);
3450
3451 opt->ip6po_hlim = *hlimp;
3452 break;
3453 }
3454
3455 case IPV6_TCLASS: {
3456 int tclass;
3457
3458 if (len != sizeof (int))
3459 return (EINVAL);
3460 tclass = *(int *)(void *)buf;
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);
3476 break;
3477 }
3478
3479 /* check if cmsg_len is large enough for sa_len */
3480 if (len < sizeof (struct sockaddr) || len < *buf)
3481 return (EINVAL);
3482
3483 switch (SA(buf)->sa_family) {
3484 case AF_INET6: {
3485 struct sockaddr_in6 *sa6 = SIN6(buf);
3486
3487 if (sa6->sin6_len != sizeof (struct sockaddr_in6))
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 }
3498 break;
3499 }
3500 case AF_LINK: /* should eventually be supported */
3501 default:
3502 return (EAFNOSUPPORT);
3503 }
3504
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:
3514 case IPV6_HOPOPTS: {
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 */
3533 if (len < sizeof (struct ip6_hbh))
3534 return (EINVAL);
3535 hbh = (struct ip6_hbh *)(void *)buf;
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:
3552 case IPV6_RTHDRDSTOPTS: {
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 */
3566 if (len < sizeof (struct ip6_dest))
3567 return (EINVAL);
3568 dest = (struct ip6_dest *)(void *)buf;
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.
3590 */
3591 if (opt->ip6po_rthdr == NULL)
3592 newdest = &opt->ip6po_dest1;
3593 else
3594 newdest = &opt->ip6po_dest2;
3595 break;
3596 case IPV6_RTHDRDSTOPTS:
3597 newdest = &opt->ip6po_dest1;
3598 break;
3599 case IPV6_DSTOPTS:
3600 newdest = &opt->ip6po_dest2;
3601 break;
3602 }
3603
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);
3610 break;
3611 }
3612
3613 case IPV6_2292RTHDR:
3614 case IPV6_RTHDR: {
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 */
3621 }
3622
3623 /* message length validation */
3624 if (len < sizeof (struct ip6_rthdr))
3625 return (EINVAL);
3626 rth = (struct ip6_rthdr *)(void *)buf;
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;
3640 default:
3641 return (EINVAL); /* not supported */
3642 }
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);
3650 break;
3651 }
3652
3653 case IPV6_USE_MIN_MTU:
3654 if (len != sizeof (int))
3655 return (EINVAL);
3656 minmtupolicy = *(int *)(void *)buf;
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:
3666 if (len != sizeof (int))
3667 return (EINVAL);
3668
3669 if (uproto == IPPROTO_TCP || *(int *)(void *)buf == 0) {
3670 /*
3671 * we ignore this option for TCP sockets.
3672 * (RFC3542 leaves this case unspecified.)
3673 */
3674 opt->ip6po_flags &= ~IP6PO_DONTFRAG;
3675 } else {
3676 opt->ip6po_flags |= IP6PO_DONTFRAG;
3677 }
3678 break;
3679
3680 case IPV6_PREFER_TEMPADDR:
3681 if (len != sizeof (int))
3682 return (EINVAL);
3683 preftemp = *(int *)(void *)buf;
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);
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 */
3705 void
3706 ip6_mloopback(struct ifnet *srcifp, struct ifnet *origifp, struct mbuf *m,
3707 struct sockaddr_in6 *dst, uint32_t optlen, int32_t nxt0)
3708 {
3709 struct mbuf *copym;
3710 struct ip6_hdr *ip6;
3711 struct in6_addr src;
3712
3713 if (lo_ifp == NULL)
3714 return;
3715
3716 /*
3717 * Copy the packet header as it's needed for the checksum.
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 */
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));
3726
3727 if (copym == NULL)
3728 return;
3729
3730 ip6 = mtod(copym, struct ip6_hdr *);
3731 src = ip6->ip6_src;
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);
3738
3739 if (copym->m_pkthdr.csum_flags & CSUM_DELAY_IPV6_DATA)
3740 in6_delayed_cksum_offset(copym, 0, optlen, nxt0);
3741
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;
3749
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);
3769 }
3770 if (srcifp != NULL)
3771 ip6_setsrcifaddr_info(copym, srcifp->if_index, NULL);
3772 ip6_setdstifaddr_info(copym, origifp->if_index, NULL);
3773
3774 dlil_output(lo_ifp, PF_INET6, copym, NULL, SA(dst), 0, NULL);
3775 }
3776
3777 /*
3778 * Chop IPv6 header off from the payload.
3779 */
3780 static int
3781 ip6_splithdr(struct mbuf *m, struct ip6_exthdrs *exthdrs)
3782 {
3783 struct mbuf *mh;
3784 struct ip6_hdr *ip6;
3785
3786 ip6 = mtod(m, struct ip6_hdr *);
3787 if (m->m_len > sizeof (*ip6)) {
3788 MGETHDR(mh, M_DONTWAIT, MT_HEADER); /* MAC-OK */
3789 if (mh == NULL) {
3790 m_freem(m);
3791 return (ENOBUFS);
3792 }
3793 M_COPY_PKTHDR(mh, m);
3794 MH_ALIGN(mh, sizeof (*ip6));
3795 m->m_flags &= ~M_PKTHDR;
3796 m->m_len -= sizeof (*ip6);
3797 m->m_data += sizeof (*ip6);
3798 mh->m_next = m;
3799 m = mh;
3800 m->m_len = sizeof (*ip6);
3801 bcopy((caddr_t)ip6, mtod(m, caddr_t), sizeof (*ip6));
3802 }
3803 exthdrs->ip6e_ip6 = m;
3804 return (0);
3805 }
3806
3807 static void
3808 ip6_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 }
3869 }
3870
3871 /*
3872 * Compute IPv6 extension header length.
3873 */
3874 int
3875 ip6_optlen(struct in6pcb *in6p)
3876 {
3877 int len;
3878
3879 if (!in6p->in6p_outputopts)
3880 return (0);
3881
3882 len = 0;
3883 #define elen(x) \
3884 (((struct ip6_ext *)(x)) ? \
3885 (((struct ip6_ext *)(x))->ip6e_len + 1) << 3 : 0)
3886
3887 len += elen(in6p->in6p_outputopts->ip6po_hbh);
3888 if (in6p->in6p_outputopts->ip6po_rthdr) {
3889 /* dest1 is valid with rthdr only */
3890 len += elen(in6p->in6p_outputopts->ip6po_dest1);
3891 }
3892 len += elen(in6p->in6p_outputopts->ip6po_rthdr);
3893 len += elen(in6p->in6p_outputopts->ip6po_dest2);
3894 return (len);
3895 #undef elen
3896 }