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