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