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