]> git.saurik.com Git - apple/xnu.git/blob - bsd/netinet6/raw_ip6.c
xnu-6153.141.1.tar.gz
[apple/xnu.git] / bsd / netinet6 / raw_ip6.c
1 /*
2 * Copyright (c) 2000-2019 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 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
30 * All rights reserved.
31 *
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
34 * are met:
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. Neither the name of the project nor the names of its contributors
41 * may be used to endorse or promote products derived from this software
42 * without specific prior written permission.
43 *
44 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
45 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
48 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54 * SUCH DAMAGE.
55 *
56 * $FreeBSD: src/sys/netinet6/raw_ip6.c,v 1.7.2.4 2001/07/29 19:32:40 ume Exp $
57 */
58
59 /*
60 * Copyright (c) 1982, 1986, 1988, 1993
61 * The Regents of the University of California. All rights reserved.
62 *
63 * Redistribution and use in source and binary forms, with or without
64 * modification, are permitted provided that the following conditions
65 * are met:
66 * 1. Redistributions of source code must retain the above copyright
67 * notice, this list of conditions and the following disclaimer.
68 * 2. Redistributions in binary form must reproduce the above copyright
69 * notice, this list of conditions and the following disclaimer in the
70 * documentation and/or other materials provided with the distribution.
71 * 3. All advertising materials mentioning features or use of this software
72 * must display the following acknowledgement:
73 * This product includes software developed by the University of
74 * California, Berkeley and its contributors.
75 * 4. Neither the name of the University nor the names of its contributors
76 * may be used to endorse or promote products derived from this software
77 * without specific prior written permission.
78 *
79 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
80 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
81 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
82 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
83 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
84 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
85 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
86 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
87 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
88 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
89 * SUCH DAMAGE.
90 *
91 * @(#)raw_ip.c 8.2 (Berkeley) 1/4/94
92 */
93 #include <sys/param.h>
94 #include <sys/malloc.h>
95 #include <sys/proc.h>
96 #include <sys/mcache.h>
97 #include <sys/mbuf.h>
98 #include <sys/socket.h>
99 #include <sys/protosw.h>
100 #include <sys/socketvar.h>
101 #include <sys/errno.h>
102 #include <sys/systm.h>
103
104 #include <net/if.h>
105 #include <net/net_api_stats.h>
106 #include <net/route.h>
107 #include <net/if_types.h>
108 #include <net/content_filter.h>
109
110 #include <netinet/in.h>
111 #include <netinet/in_var.h>
112 #include <netinet/in_systm.h>
113 #include <netinet/in_tclass.h>
114 #include <netinet/ip6.h>
115 #include <netinet6/ip6_var.h>
116 #include <netinet/icmp6.h>
117 #include <netinet/in_pcb.h>
118 #include <netinet6/in6_pcb.h>
119 #include <netinet6/nd6.h>
120 #include <netinet6/ip6protosw.h>
121 #include <netinet6/scope6_var.h>
122 #include <netinet6/raw_ip6.h>
123
124 #if IPSEC
125 #include <netinet6/ipsec.h>
126 #include <netinet6/ipsec6.h>
127 #endif /*IPSEC*/
128
129 #if NECP
130 #include <net/necp.h>
131 #endif
132
133 /*
134 * Raw interface to IP6 protocol.
135 */
136
137 extern struct inpcbhead ripcb;
138 extern struct inpcbinfo ripcbinfo;
139 extern u_int32_t rip_sendspace;
140 extern u_int32_t rip_recvspace;
141
142 struct rip6stat rip6stat;
143
144 /*
145 * Setup generic address and protocol structures
146 * for raw_input routine, then pass them along with
147 * mbuf chain.
148 */
149 int
150 rip6_input(
151 struct mbuf **mp,
152 int *offp,
153 int proto)
154 {
155 struct mbuf *m = *mp;
156 struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
157 struct inpcb *in6p;
158 struct inpcb *last = 0;
159 struct mbuf *opts = NULL;
160 struct sockaddr_in6 rip6src;
161 int ret;
162 struct ifnet *ifp = m->m_pkthdr.rcvif;
163
164 /* Expect 32-bit aligned data pointer on strict-align platforms */
165 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m);
166
167 rip6stat.rip6s_ipackets++;
168
169 init_sin6(&rip6src, m); /* general init */
170
171 lck_rw_lock_shared(ripcbinfo.ipi_lock);
172 LIST_FOREACH(in6p, &ripcb, inp_list) {
173 if ((in6p->in6p_vflag & INP_IPV6) == 0) {
174 continue;
175 }
176 if (in6p->in6p_ip6_nxt &&
177 in6p->in6p_ip6_nxt != proto) {
178 continue;
179 }
180 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_laddr) &&
181 !IN6_ARE_ADDR_EQUAL(&in6p->in6p_laddr, &ip6->ip6_dst)) {
182 continue;
183 }
184 if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) &&
185 !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &ip6->ip6_src)) {
186 continue;
187 }
188
189 if (inp_restricted_recv(in6p, ifp)) {
190 continue;
191 }
192
193 if (proto == IPPROTO_ICMPV6 || in6p->in6p_cksum != -1) {
194 rip6stat.rip6s_isum++;
195 if (in6_cksum(m, ip6->ip6_nxt, *offp,
196 m->m_pkthdr.len - *offp)) {
197 rip6stat.rip6s_badsum++;
198 continue;
199 }
200 }
201 if (last) {
202 struct mbuf *n = m_copy(m, 0, (int)M_COPYALL);
203
204 #if NECP
205 if (n && !necp_socket_is_allowed_to_send_recv_v6(in6p, 0, 0,
206 &ip6->ip6_dst, &ip6->ip6_src, ifp, NULL, NULL, NULL)) {
207 m_freem(n);
208 /* do not inject data into pcb */
209 } else
210 #endif /* NECP */
211 if (n) {
212 if ((last->in6p_flags & INP_CONTROLOPTS) != 0 ||
213 (last->in6p_socket->so_options & SO_TIMESTAMP) != 0 ||
214 (last->in6p_socket->so_options & SO_TIMESTAMP_MONOTONIC) != 0 ||
215 (last->in6p_socket->so_options & SO_TIMESTAMP_CONTINUOUS) != 0) {
216 ret = ip6_savecontrol(last, n, &opts);
217 if (ret != 0) {
218 m_freem(n);
219 m_freem(opts);
220 last = in6p;
221 continue;
222 }
223 }
224 /* strip intermediate headers */
225 m_adj(n, *offp);
226 so_recv_data_stat(last->in6p_socket, m, 0);
227 if (sbappendaddr(&last->in6p_socket->so_rcv,
228 (struct sockaddr *)&rip6src,
229 n, opts, NULL) == 0) {
230 rip6stat.rip6s_fullsock++;
231 } else {
232 sorwakeup(last->in6p_socket);
233 }
234 opts = NULL;
235 }
236 }
237 last = in6p;
238 }
239
240 #if NECP
241 if (last && !necp_socket_is_allowed_to_send_recv_v6(in6p, 0, 0,
242 &ip6->ip6_dst, &ip6->ip6_src, ifp, NULL, NULL, NULL)) {
243 m_freem(m);
244 ip6stat.ip6s_delivered--;
245 /* do not inject data into pcb */
246 } else
247 #endif /* NECP */
248 if (last) {
249 if ((last->in6p_flags & INP_CONTROLOPTS) != 0 ||
250 (last->in6p_socket->so_options & SO_TIMESTAMP) != 0 ||
251 (last->in6p_socket->so_options & SO_TIMESTAMP_MONOTONIC) != 0 ||
252 (last->in6p_socket->so_options & SO_TIMESTAMP_CONTINUOUS) != 0) {
253 ret = ip6_savecontrol(last, m, &opts);
254 if (ret != 0) {
255 m_freem(m);
256 m_freem(opts);
257 ip6stat.ip6s_delivered--;
258 goto unlock;
259 }
260 }
261 /* strip intermediate headers */
262 m_adj(m, *offp);
263 so_recv_data_stat(last->in6p_socket, m, 0);
264 if (sbappendaddr(&last->in6p_socket->so_rcv,
265 (struct sockaddr *)&rip6src, m, opts, NULL) == 0) {
266 rip6stat.rip6s_fullsock++;
267 } else {
268 sorwakeup(last->in6p_socket);
269 }
270 } else {
271 rip6stat.rip6s_nosock++;
272 if (m->m_flags & M_MCAST) {
273 rip6stat.rip6s_nosockmcast++;
274 }
275 if (proto == IPPROTO_NONE) {
276 m_freem(m);
277 } else {
278 char *prvnxtp = ip6_get_prevhdr(m, *offp); /* XXX */
279 icmp6_error(m, ICMP6_PARAM_PROB,
280 ICMP6_PARAMPROB_NEXTHEADER,
281 prvnxtp - mtod(m, char *));
282 }
283 ip6stat.ip6s_delivered--;
284 }
285
286 unlock:
287 lck_rw_done(ripcbinfo.ipi_lock);
288
289 return IPPROTO_DONE;
290 }
291
292 void
293 rip6_ctlinput(
294 int cmd,
295 struct sockaddr *sa,
296 void *d,
297 __unused struct ifnet *ifp)
298 {
299 struct ip6_hdr *ip6;
300 struct mbuf *m;
301 void *cmdarg = NULL;
302 int off = 0;
303 struct ip6ctlparam *ip6cp = NULL;
304 const struct sockaddr_in6 *sa6_src = NULL;
305 void (*notify)(struct inpcb *, int) = in6_rtchange;
306
307 if (sa->sa_family != AF_INET6 ||
308 sa->sa_len != sizeof(struct sockaddr_in6)) {
309 return;
310 }
311
312 if ((unsigned)cmd >= PRC_NCMDS) {
313 return;
314 }
315 if (PRC_IS_REDIRECT(cmd)) {
316 notify = in6_rtchange;
317 d = NULL;
318 } else if (cmd == PRC_HOSTDEAD) {
319 d = NULL;
320 } else if (inet6ctlerrmap[cmd] == 0) {
321 return;
322 }
323
324 /* if the parameter is from icmp6, decode it. */
325 if (d != NULL) {
326 ip6cp = (struct ip6ctlparam *)d;
327 m = ip6cp->ip6c_m;
328 ip6 = ip6cp->ip6c_ip6;
329 off = ip6cp->ip6c_off;
330 cmdarg = ip6cp->ip6c_cmdarg;
331 sa6_src = ip6cp->ip6c_src;
332 } else {
333 m = NULL;
334 ip6 = NULL;
335 sa6_src = &sa6_any;
336 }
337
338 (void) in6_pcbnotify(&ripcbinfo, sa, 0, (const struct sockaddr *)sa6_src,
339 0, cmd, cmdarg, notify);
340 }
341
342 /*
343 * Generate IPv6 header and pass packet to ip6_output.
344 * Tack on options user may have setup with control call.
345 */
346 int
347 rip6_output(
348 struct mbuf *m,
349 struct socket *so,
350 struct sockaddr_in6 *dstsock,
351 struct mbuf *control,
352 int israw)
353 {
354 struct in6_addr *dst;
355 struct ip6_hdr *ip6;
356 struct inpcb *in6p;
357 u_int plen = m->m_pkthdr.len;
358 int error = 0;
359 struct ip6_pktopts opt, *optp = NULL;
360 struct ip6_moptions *im6o = NULL;
361 struct ifnet *oifp = NULL;
362 int type = 0, code = 0; /* for ICMPv6 output statistics only */
363 int sotc = SO_TC_UNSPEC;
364 int netsvctype = _NET_SERVICE_TYPE_UNSPEC;
365 struct ip6_out_args ip6oa;
366 int flags = IPV6_OUTARGS;
367 struct sockaddr_in6 tmp;
368 #if CONTENT_FILTER
369 struct m_tag *cfil_tag = NULL;
370 bool cfil_faddr_use = false;
371 uint32_t cfil_so_state_change_cnt = 0;
372 short cfil_so_options = 0;
373 struct sockaddr *cfil_faddr = NULL;
374 struct sockaddr_in6 *cfil_sin6 = NULL;
375 #endif
376
377 in6p = sotoin6pcb(so);
378 if (in6p == NULL) {
379 error = EINVAL;
380 goto bad;
381 }
382
383 #if CONTENT_FILTER
384 /*
385 * If socket is subject to Content Filter and no addr is passed in,
386 * retrieve CFIL saved state from mbuf and use it if necessary.
387 */
388 if (so->so_cfil_db && !dstsock) {
389 cfil_tag = cfil_dgram_get_socket_state(m, &cfil_so_state_change_cnt, &cfil_so_options, &cfil_faddr, NULL);
390 if (cfil_tag) {
391 cfil_sin6 = SIN6(cfil_faddr);
392 if (IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr)) {
393 /*
394 * Socket is unconnected, simply use the saved faddr as 'addr' to go through
395 * the connect/disconnect logic.
396 */
397 dstsock = cfil_sin6;
398 } else if ((so->so_state_change_cnt != cfil_so_state_change_cnt) &&
399 (in6p->in6p_fport != cfil_sin6->sin6_port ||
400 !IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, &cfil_sin6->sin6_addr))) {
401 /*
402 * Socket is connected but socket state and dest addr/port changed.
403 * We need to use the saved faddr and socket options.
404 */
405 cfil_faddr_use = true;
406 }
407 }
408 }
409 #endif
410
411 /* always copy sockaddr to avoid overwrites */
412 if (so->so_state & SS_ISCONNECTED) {
413 if (dstsock != NULL) {
414 error = EISCONN;
415 goto bad;
416 }
417 /* XXX */
418 bzero(&tmp, sizeof(tmp));
419 tmp.sin6_family = AF_INET6;
420 tmp.sin6_len = sizeof(struct sockaddr_in6);
421 bcopy(
422 #if CONTENT_FILTER
423 cfil_faddr_use ? &cfil_sin6->sin6_addr :
424 #endif
425 &in6p->in6p_faddr, &tmp.sin6_addr, sizeof(struct in6_addr));
426 dstsock = &tmp;
427 } else {
428 if (dstsock == NULL) {
429 error = ENOTCONN;
430 goto bad;
431 }
432 tmp = *dstsock;
433 dstsock = &tmp;
434 }
435
436 #if ENABLE_DEFAULT_SCOPE
437 if (dstsock->sin6_scope_id == 0) { /* not change if specified */
438 dstsock->sin6_scope_id = scope6_addr2default(&dstsock->sin6_addr);
439 }
440 #endif
441
442 bzero(&ip6oa, sizeof(ip6oa));
443 ip6oa.ip6oa_boundif = IFSCOPE_NONE;
444 ip6oa.ip6oa_flags = IP6OAF_SELECT_SRCIF;
445
446 if (in6p == NULL
447 #if NECP
448 || (necp_socket_should_use_flow_divert(in6p))
449 #endif /* NECP */
450 ) {
451 if (in6p == NULL) {
452 error = EINVAL;
453 } else {
454 error = EPROTOTYPE;
455 }
456 goto bad;
457 }
458 if (dstsock != NULL && IN6_IS_ADDR_V4MAPPED(&dstsock->sin6_addr)) {
459 error = EINVAL;
460 goto bad;
461 }
462
463 if (in6p->inp_flags & INP_BOUND_IF) {
464 ip6oa.ip6oa_boundif = in6p->inp_boundifp->if_index;
465 ip6oa.ip6oa_flags |= IP6OAF_BOUND_IF;
466 }
467 if (INP_NO_CELLULAR(in6p)) {
468 ip6oa.ip6oa_flags |= IP6OAF_NO_CELLULAR;
469 }
470 if (INP_NO_EXPENSIVE(in6p)) {
471 ip6oa.ip6oa_flags |= IP6OAF_NO_EXPENSIVE;
472 }
473 if (INP_NO_CONSTRAINED(in6p)) {
474 ip6oa.ip6oa_flags |= IP6OAF_NO_CONSTRAINED;
475 }
476 if (INP_AWDL_UNRESTRICTED(in6p)) {
477 ip6oa.ip6oa_flags |= IP6OAF_AWDL_UNRESTRICTED;
478 }
479 if (INP_INTCOPROC_ALLOWED(in6p)) {
480 ip6oa.ip6oa_flags |= IP6OAF_INTCOPROC_ALLOWED;
481 }
482
483 dst = &dstsock->sin6_addr;
484 if (control) {
485 sotc = so_tc_from_control(control, &netsvctype);
486
487 if ((error = ip6_setpktopts(control, &opt, NULL,
488 SOCK_PROTO(so))) != 0) {
489 goto bad;
490 }
491 optp = &opt;
492 } else {
493 optp = in6p->in6p_outputopts;
494 }
495 if (sotc == SO_TC_UNSPEC) {
496 sotc = so->so_traffic_class;
497 netsvctype = so->so_netsvctype;
498 }
499 ip6oa.ip6oa_sotc = sotc;
500 ip6oa.ip6oa_netsvctype = netsvctype;
501
502 /*
503 * For an ICMPv6 packet, we should know its type and code
504 * to update statistics.
505 */
506 if (SOCK_PROTO(so) == IPPROTO_ICMPV6) {
507 struct icmp6_hdr *icmp6;
508 if (m->m_len < sizeof(struct icmp6_hdr) &&
509 (m = m_pullup(m, sizeof(struct icmp6_hdr))) == NULL) {
510 error = ENOBUFS;
511 goto bad;
512 }
513 icmp6 = mtod(m, struct icmp6_hdr *);
514 type = icmp6->icmp6_type;
515 code = icmp6->icmp6_code;
516 }
517
518 if (in6p->inp_flowhash == 0) {
519 in6p->inp_flowhash = inp_calc_flowhash(in6p);
520 }
521 /* update flowinfo - RFC 6437 */
522 if (in6p->inp_flow == 0 && in6p->in6p_flags & IN6P_AUTOFLOWLABEL) {
523 in6p->inp_flow &= ~IPV6_FLOWLABEL_MASK;
524 in6p->inp_flow |=
525 (htonl(in6p->inp_flowhash) & IPV6_FLOWLABEL_MASK);
526 }
527
528 M_PREPEND(m, sizeof(*ip6), M_WAIT, 1);
529 if (m == NULL) {
530 error = ENOBUFS;
531 goto bad;
532 }
533 ip6 = mtod(m, struct ip6_hdr *);
534
535 /*
536 * Next header might not be ICMP6 but use its pseudo header anyway.
537 */
538 ip6->ip6_dst = *dst;
539
540 im6o = in6p->in6p_moptions;
541
542 /*
543 * If the scope of the destination is link-local, embed the interface
544 * index in the address.
545 *
546 * XXX advanced-api value overrides sin6_scope_id
547 */
548 if (IN6_IS_SCOPE_LINKLOCAL(&ip6->ip6_dst)) {
549 struct in6_pktinfo *pi;
550 struct ifnet *im6o_multicast_ifp = NULL;
551
552 if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) && im6o != NULL) {
553 IM6O_LOCK(im6o);
554 im6o_multicast_ifp = im6o->im6o_multicast_ifp;
555 IM6O_UNLOCK(im6o);
556 }
557 /*
558 * XXX Boundary check is assumed to be already done in
559 * ip6_setpktoptions().
560 */
561 ifnet_head_lock_shared();
562 if (optp && (pi = optp->ip6po_pktinfo) && pi->ipi6_ifindex) {
563 ip6->ip6_dst.s6_addr16[1] = htons(pi->ipi6_ifindex);
564 oifp = ifindex2ifnet[pi->ipi6_ifindex];
565 if (oifp != NULL) {
566 ifnet_reference(oifp);
567 }
568 } else if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst) &&
569 im6o != NULL && im6o_multicast_ifp != NULL) {
570 oifp = im6o_multicast_ifp;
571 ifnet_reference(oifp);
572 ip6->ip6_dst.s6_addr16[1] = htons(oifp->if_index);
573 } else if (dstsock->sin6_scope_id) {
574 /*
575 * boundary check
576 *
577 * Sinced stsock->sin6_scope_id is unsigned, we don't
578 * need to check if it's < 0
579 */
580 if (if_index < dstsock->sin6_scope_id) {
581 error = ENXIO; /* XXX EINVAL? */
582 ifnet_head_done();
583 goto bad;
584 }
585 ip6->ip6_dst.s6_addr16[1]
586 = htons(dstsock->sin6_scope_id & 0xffff);/*XXX*/
587 }
588 ifnet_head_done();
589 }
590
591 /*
592 * Source address selection.
593 */
594 {
595 struct in6_addr *in6a;
596 struct in6_addr storage;
597 u_short index = 0;
598
599 if (israw != 0 && optp && optp->ip6po_pktinfo && !IN6_IS_ADDR_UNSPECIFIED(&optp->ip6po_pktinfo->ipi6_addr)) {
600 in6a = &optp->ip6po_pktinfo->ipi6_addr;
601 flags |= IPV6_FLAG_NOSRCIFSEL;
602 } else if ((in6a = in6_selectsrc(dstsock, optp, in6p,
603 &in6p->in6p_route, NULL, &storage, ip6oa.ip6oa_boundif,
604 &error)) == 0) {
605 if (error == 0) {
606 error = EADDRNOTAVAIL;
607 }
608 goto bad;
609 } else {
610 ip6oa.ip6oa_flags |= IP6OAF_BOUND_SRCADDR;
611 }
612 ip6->ip6_src = *in6a;
613 if (in6p->in6p_route.ro_rt != NULL) {
614 RT_LOCK(in6p->in6p_route.ro_rt);
615 if (in6p->in6p_route.ro_rt->rt_ifp != NULL) {
616 index = in6p->in6p_route.ro_rt->rt_ifp->if_index;
617 }
618 RT_UNLOCK(in6p->in6p_route.ro_rt);
619 if (oifp != NULL) {
620 ifnet_release(oifp);
621 }
622 ifnet_head_lock_shared();
623 if (index == 0 || if_index < index) {
624 panic("bad if_index on interface from route");
625 }
626 oifp = ifindex2ifnet[index];
627 if (oifp != NULL) {
628 ifnet_reference(oifp);
629 }
630 ifnet_head_done();
631 }
632 }
633 ip6->ip6_flow = (ip6->ip6_flow & ~IPV6_FLOWINFO_MASK) |
634 (in6p->inp_flow & IPV6_FLOWINFO_MASK);
635 ip6->ip6_vfc = (ip6->ip6_vfc & ~IPV6_VERSION_MASK) |
636 (IPV6_VERSION & IPV6_VERSION_MASK);
637 /* ip6_plen will be filled in ip6_output, so not fill it here. */
638 ip6->ip6_nxt = in6p->in6p_ip6_nxt;
639 ip6->ip6_hlim = in6_selecthlim(in6p, oifp);
640
641 if (SOCK_PROTO(so) == IPPROTO_ICMPV6 || in6p->in6p_cksum != -1) {
642 struct mbuf *n;
643 int off;
644 u_int16_t *p;
645
646 /* compute checksum */
647 if (SOCK_PROTO(so) == IPPROTO_ICMPV6) {
648 off = offsetof(struct icmp6_hdr, icmp6_cksum);
649 } else {
650 off = in6p->in6p_cksum;
651 }
652 if (plen < (unsigned int)(off + 1)) {
653 error = EINVAL;
654 goto bad;
655 }
656 off += sizeof(struct ip6_hdr);
657
658 n = m;
659 while (n && n->m_len <= off) {
660 off -= n->m_len;
661 n = n->m_next;
662 }
663 if (!n) {
664 goto bad;
665 }
666 p = (u_int16_t *)(void *)(mtod(n, caddr_t) + off);
667 *p = 0;
668 *p = in6_cksum(m, ip6->ip6_nxt, sizeof(*ip6), plen);
669 }
670
671 #if NECP
672 {
673 necp_kernel_policy_id policy_id;
674 necp_kernel_policy_id skip_policy_id;
675 u_int32_t route_rule_id;
676
677 /*
678 * We need a route to perform NECP route rule checks
679 */
680 if ((net_qos_policy_restricted != 0 &&
681 ROUTE_UNUSABLE(&in6p->in6p_route))
682 #if CONTENT_FILTER
683 || cfil_faddr_use
684 #endif
685 ) {
686 struct sockaddr_in6 to;
687 struct sockaddr_in6 from;
688
689 ROUTE_RELEASE(&in6p->in6p_route);
690
691 bzero(&from, sizeof(struct sockaddr_in6));
692 from.sin6_family = AF_INET6;
693 from.sin6_len = sizeof(struct sockaddr_in6);
694 from.sin6_addr = ip6->ip6_src;
695
696 bzero(&to, sizeof(struct sockaddr_in6));
697 to.sin6_family = AF_INET6;
698 to.sin6_len = sizeof(struct sockaddr_in6);
699 to.sin6_addr = ip6->ip6_dst;
700
701 in6p->in6p_route.ro_dst.sin6_family = AF_INET6;
702 in6p->in6p_route.ro_dst.sin6_len = sizeof(struct sockaddr_in6);
703 ((struct sockaddr_in6 *)(void *)&in6p->in6p_route.ro_dst)->sin6_addr =
704 ip6->ip6_dst;
705
706 rtalloc_scoped((struct route *)&in6p->in6p_route, ip6oa.ip6oa_boundif);
707
708 inp_update_necp_policy(in6p, (struct sockaddr *)&from,
709 (struct sockaddr *)&to, ip6oa.ip6oa_boundif);
710 in6p->inp_policyresult.results.qos_marking_gencount = 0;
711 }
712
713 if (!necp_socket_is_allowed_to_send_recv_v6(in6p, 0, 0,
714 &ip6->ip6_src, &ip6->ip6_dst, NULL, &policy_id, &route_rule_id, &skip_policy_id)) {
715 error = EHOSTUNREACH;
716 goto bad;
717 }
718
719 necp_mark_packet_from_socket(m, in6p, policy_id, route_rule_id, skip_policy_id);
720
721 if (net_qos_policy_restricted != 0) {
722 necp_socket_update_qos_marking(in6p, in6p->in6p_route.ro_rt,
723 NULL, route_rule_id);
724 }
725 }
726 #endif /* NECP */
727 if ((so->so_flags1 & SOF1_QOSMARKING_ALLOWED)) {
728 ip6oa.ip6oa_flags |= IP6OAF_QOSMARKING_ALLOWED;
729 }
730
731 #if IPSEC
732 if (in6p->in6p_sp != NULL && ipsec_setsocket(m, so) != 0) {
733 error = ENOBUFS;
734 goto bad;
735 }
736 #endif /*IPSEC*/
737
738 if (ROUTE_UNUSABLE(&in6p->in6p_route)) {
739 ROUTE_RELEASE(&in6p->in6p_route);
740 }
741
742 if (oifp != NULL) {
743 ifnet_release(oifp);
744 oifp = NULL;
745 }
746
747 set_packet_service_class(m, so, sotc, PKT_SCF_IPV6);
748 m->m_pkthdr.pkt_flowsrc = FLOWSRC_INPCB;
749 m->m_pkthdr.pkt_flowid = in6p->inp_flowhash;
750 m->m_pkthdr.pkt_flags |= (PKTF_FLOW_ID | PKTF_FLOW_LOCALSRC |
751 PKTF_FLOW_RAWSOCK);
752 m->m_pkthdr.pkt_proto = in6p->in6p_ip6_nxt;
753 m->m_pkthdr.tx_rawip_pid = so->last_pid;
754 if (so->so_flags & SOF_DELEGATED) {
755 m->m_pkthdr.tx_rawip_e_pid = so->e_pid;
756 } else {
757 m->m_pkthdr.tx_rawip_e_pid = 0;
758 }
759
760 if (im6o != NULL) {
761 IM6O_ADDREF(im6o);
762 }
763
764 error = ip6_output(m, optp, &in6p->in6p_route, flags, im6o,
765 &oifp, &ip6oa);
766
767 if (im6o != NULL) {
768 IM6O_REMREF(im6o);
769 }
770
771 if (in6p->in6p_route.ro_rt != NULL) {
772 struct rtentry *rt = in6p->in6p_route.ro_rt;
773 struct ifnet *outif;
774
775 if ((rt->rt_flags & RTF_MULTICAST) ||
776 in6p->in6p_socket == NULL ||
777 #if CONTENT_FILTER
778 /* Discard temporary route for cfil case */
779 cfil_faddr_use ||
780 #endif
781 !(in6p->in6p_socket->so_state & SS_ISCONNECTED)) {
782 rt = NULL; /* unusable */
783 }
784 /*
785 * Always discard the cached route for unconnected
786 * socket or if it is a multicast route.
787 */
788 if (rt == NULL) {
789 ROUTE_RELEASE(&in6p->in6p_route);
790 }
791
792 /*
793 * If this is a connected socket and the destination
794 * route is not multicast, update outif with that of
795 * the route interface index used by IP.
796 */
797 if (rt != NULL) {
798 /*
799 * When an NECP IP tunnel policy forces the outbound interface,
800 * ip6_output_list() informs the transport layer what is the actual
801 * outgoing interface
802 */
803 if (ip6oa.ip6oa_flags & IP6OAF_BOUND_IF) {
804 outif = ifindex2ifnet[ip6oa.ip6oa_boundif];
805 } else {
806 outif = rt->rt_ifp;
807 }
808 if (outif != NULL) {
809 in6p->in6p_last_outifp = outif;
810 }
811 }
812 } else {
813 ROUTE_RELEASE(&in6p->in6p_route);
814 }
815
816 /*
817 * If output interface was cellular/expensive, and this socket is
818 * denied access to it, generate an event.
819 */
820 if (error != 0 && (ip6oa.ip6oa_retflags & IP6OARF_IFDENIED) &&
821 (INP_NO_CELLULAR(in6p) || INP_NO_EXPENSIVE(in6p) || INP_NO_CONSTRAINED(in6p))) {
822 soevent(in6p->inp_socket, (SO_FILT_HINT_LOCKED |
823 SO_FILT_HINT_IFDENIED));
824 }
825
826 if (SOCK_PROTO(so) == IPPROTO_ICMPV6) {
827 if (oifp) {
828 icmp6_ifoutstat_inc(oifp, type, code);
829 }
830 icmp6stat.icp6s_outhist[type]++;
831 } else {
832 rip6stat.rip6s_opackets++;
833 }
834
835 goto freectl;
836
837 bad:
838 if (m != NULL) {
839 m_freem(m);
840 }
841
842 freectl:
843 if (optp == &opt && optp->ip6po_rthdr) {
844 ROUTE_RELEASE(&optp->ip6po_route);
845 }
846
847 if (control != NULL) {
848 if (optp == &opt) {
849 ip6_clearpktopts(optp, -1);
850 }
851 m_freem(control);
852 }
853 if (oifp != NULL) {
854 ifnet_release(oifp);
855 }
856 #if CONTENT_FILTER
857 if (cfil_tag) {
858 m_tag_free(cfil_tag);
859 }
860 #endif
861
862 return error;
863 }
864
865 /*
866 * Raw IPv6 socket option processing.
867 */
868 int
869 rip6_ctloutput(
870 struct socket *so,
871 struct sockopt *sopt)
872 {
873 int error, optval;
874
875 /* Allow <SOL_SOCKET,SO_FLUSH> at this level */
876 if (sopt->sopt_level == IPPROTO_ICMPV6) {
877 /*
878 * XXX: is it better to call icmp6_ctloutput() directly
879 * from protosw?
880 */
881 return icmp6_ctloutput(so, sopt);
882 } else if (sopt->sopt_level != IPPROTO_IPV6 &&
883 !(sopt->sopt_level == SOL_SOCKET && sopt->sopt_name == SO_FLUSH)) {
884 return EINVAL;
885 }
886
887 error = 0;
888
889 switch (sopt->sopt_dir) {
890 case SOPT_GET:
891 switch (sopt->sopt_name) {
892 case IPV6_CHECKSUM:
893 error = ip6_raw_ctloutput(so, sopt);
894 break;
895 default:
896 error = ip6_ctloutput(so, sopt);
897 break;
898 }
899 break;
900
901 case SOPT_SET:
902 switch (sopt->sopt_name) {
903 case IPV6_CHECKSUM:
904 error = ip6_raw_ctloutput(so, sopt);
905 break;
906
907 case SO_FLUSH:
908 if ((error = sooptcopyin(sopt, &optval, sizeof(optval),
909 sizeof(optval))) != 0) {
910 break;
911 }
912
913 error = inp_flush(sotoinpcb(so), optval);
914 break;
915
916 default:
917 error = ip6_ctloutput(so, sopt);
918 break;
919 }
920 break;
921 }
922
923 return error;
924 }
925
926 static int
927 rip6_attach(struct socket *so, int proto, struct proc *p)
928 {
929 struct inpcb *inp;
930 int error;
931
932 inp = sotoinpcb(so);
933 if (inp) {
934 panic("rip6_attach");
935 }
936 if ((error = proc_suser(p)) != 0) {
937 return error;
938 }
939
940 error = soreserve(so, rip_sendspace, rip_recvspace);
941 if (error) {
942 return error;
943 }
944 error = in_pcballoc(so, &ripcbinfo, p);
945 if (error) {
946 return error;
947 }
948 inp = (struct inpcb *)so->so_pcb;
949 inp->inp_vflag |= INP_IPV6;
950 inp->in6p_ip6_nxt = (char)proto;
951 inp->in6p_hops = -1; /* use kernel default */
952 inp->in6p_cksum = -1;
953 MALLOC(inp->in6p_icmp6filt, struct icmp6_filter *,
954 sizeof(struct icmp6_filter), M_PCB, M_WAITOK);
955 if (inp->in6p_icmp6filt == NULL) {
956 return ENOMEM;
957 }
958 ICMP6_FILTER_SETPASSALL(inp->in6p_icmp6filt);
959 return 0;
960 }
961
962 static int
963 rip6_detach(struct socket *so)
964 {
965 struct inpcb *inp;
966
967 inp = sotoinpcb(so);
968 if (inp == 0) {
969 panic("rip6_detach");
970 }
971 /* xxx: RSVP */
972 if (inp->in6p_icmp6filt) {
973 FREE(inp->in6p_icmp6filt, M_PCB);
974 inp->in6p_icmp6filt = NULL;
975 }
976 in6_pcbdetach(inp);
977 return 0;
978 }
979
980 static int
981 rip6_abort(struct socket *so)
982 {
983 soisdisconnected(so);
984 return rip6_detach(so);
985 }
986
987 static int
988 rip6_disconnect(struct socket *so)
989 {
990 struct inpcb *inp = sotoinpcb(so);
991
992 if ((so->so_state & SS_ISCONNECTED) == 0) {
993 return ENOTCONN;
994 }
995 inp->in6p_faddr = in6addr_any;
996 return rip6_abort(so);
997 }
998
999 static int
1000 rip6_bind(struct socket *so, struct sockaddr *nam, struct proc *p)
1001 {
1002 #pragma unused(p)
1003 struct inpcb *inp = sotoinpcb(so);
1004 struct sockaddr_in6 sin6;
1005 struct ifaddr *ifa = NULL;
1006 struct ifnet *outif = NULL;
1007 int error;
1008
1009 if (inp == NULL
1010 #if NECP
1011 || (necp_socket_should_use_flow_divert(inp))
1012 #endif /* NECP */
1013 ) {
1014 return inp == NULL ? EINVAL : EPROTOTYPE;
1015 }
1016
1017 if (nam->sa_len != sizeof(struct sockaddr_in6)) {
1018 return EINVAL;
1019 }
1020
1021 if (TAILQ_EMPTY(&ifnet_head) || SIN6(nam)->sin6_family != AF_INET6) {
1022 return EADDRNOTAVAIL;
1023 }
1024
1025 bzero(&sin6, sizeof(sin6));
1026 *(&sin6) = *SIN6(nam);
1027
1028 if ((error = sa6_embedscope(&sin6, ip6_use_defzone)) != 0) {
1029 return error;
1030 }
1031
1032 /* Sanitize local copy for address searches */
1033 sin6.sin6_flowinfo = 0;
1034 sin6.sin6_scope_id = 0;
1035 sin6.sin6_port = 0;
1036
1037 if (!IN6_IS_ADDR_UNSPECIFIED(&sin6.sin6_addr) &&
1038 (ifa = ifa_ifwithaddr(SA(&sin6))) == 0) {
1039 return EADDRNOTAVAIL;
1040 }
1041 if (ifa != NULL) {
1042 IFA_LOCK(ifa);
1043 if (((struct in6_ifaddr *)ifa)->ia6_flags &
1044 (IN6_IFF_ANYCAST | IN6_IFF_NOTREADY | IN6_IFF_CLAT46 |
1045 IN6_IFF_DETACHED | IN6_IFF_DEPRECATED)) {
1046 IFA_UNLOCK(ifa);
1047 IFA_REMREF(ifa);
1048 return EADDRNOTAVAIL;
1049 }
1050 outif = ifa->ifa_ifp;
1051 IFA_UNLOCK(ifa);
1052 IFA_REMREF(ifa);
1053 }
1054 inp->in6p_laddr = sin6.sin6_addr;
1055 inp->in6p_last_outifp = outif;
1056
1057 return 0;
1058 }
1059
1060 static int
1061 rip6_connect(struct socket *so, struct sockaddr *nam, __unused struct proc *p)
1062 {
1063 struct inpcb *inp = sotoinpcb(so);
1064 struct sockaddr_in6 *addr = (struct sockaddr_in6 *)(void *)nam;
1065 struct in6_addr *in6a = NULL;
1066 struct in6_addr storage;
1067 int error = 0;
1068 #if ENABLE_DEFAULT_SCOPE
1069 struct sockaddr_in6 tmp;
1070 #endif
1071 unsigned int ifscope;
1072 struct ifnet *outif = NULL;
1073
1074 if (inp == NULL
1075 #if NECP
1076 || (necp_socket_should_use_flow_divert(inp))
1077 #endif /* NECP */
1078 ) {
1079 return inp == NULL ? EINVAL : EPROTOTYPE;
1080 }
1081 if (nam->sa_len != sizeof(*addr)) {
1082 return EINVAL;
1083 }
1084 if (TAILQ_EMPTY(&ifnet_head)) {
1085 return EADDRNOTAVAIL;
1086 }
1087 if (addr->sin6_family != AF_INET6) {
1088 return EAFNOSUPPORT;
1089 }
1090
1091 if (!(so->so_flags1 & SOF1_CONNECT_COUNTED)) {
1092 so->so_flags1 |= SOF1_CONNECT_COUNTED;
1093 INC_ATOMIC_INT64_LIM(net_api_stats.nas_socket_inet6_dgram_connected);
1094 }
1095
1096 #if ENABLE_DEFAULT_SCOPE
1097 if (addr->sin6_scope_id == 0) { /* not change if specified */
1098 /* avoid overwrites */
1099 tmp = *addr;
1100 addr = &tmp;
1101 addr->sin6_scope_id = scope6_addr2default(&addr->sin6_addr);
1102 }
1103 #endif
1104
1105 /* KAME hack: embed scopeid */
1106 if (in6_embedscope(&SIN6(nam)->sin6_addr, SIN6(nam), inp, NULL, NULL) != 0) {
1107 return EINVAL;
1108 }
1109
1110 ifscope = (inp->inp_flags & INP_BOUND_IF) ?
1111 inp->inp_boundifp->if_index : IFSCOPE_NONE;
1112
1113 /* Source address selection. XXX: need pcblookup? */
1114 in6a = in6_selectsrc(addr, inp->in6p_outputopts, inp, &inp->in6p_route,
1115 NULL, &storage, ifscope, &error);
1116 if (in6a == NULL) {
1117 return error ? error : EADDRNOTAVAIL;
1118 }
1119 inp->in6p_laddr = *in6a;
1120 inp->in6p_faddr = addr->sin6_addr;
1121 if (inp->in6p_route.ro_rt != NULL) {
1122 outif = inp->in6p_route.ro_rt->rt_ifp;
1123 }
1124 inp->in6p_last_outifp = outif;
1125
1126 soisconnected(so);
1127 return 0;
1128 }
1129
1130 static int
1131 rip6_shutdown(struct socket *so)
1132 {
1133 socantsendmore(so);
1134 return 0;
1135 }
1136
1137 static int
1138 rip6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *nam,
1139 struct mbuf *control, struct proc *p)
1140 {
1141 #pragma unused(flags, p)
1142 struct inpcb *inp = sotoinpcb(so);
1143 int error = 0;
1144
1145 if (inp == NULL
1146 #if NECP
1147 || (necp_socket_should_use_flow_divert(inp))
1148 #endif /* NECP */
1149 ) {
1150 if (inp == NULL) {
1151 error = EINVAL;
1152 } else {
1153 error = EPROTOTYPE;
1154 }
1155 goto bad;
1156 }
1157
1158 return rip6_output(m, so, SIN6(nam), control, 1);
1159
1160 bad:
1161 VERIFY(error != 0);
1162
1163 if (m != NULL) {
1164 m_freem(m);
1165 }
1166 if (control != NULL) {
1167 m_freem(control);
1168 }
1169
1170 return error;
1171 }
1172
1173 struct pr_usrreqs rip6_usrreqs = {
1174 .pru_abort = rip6_abort,
1175 .pru_attach = rip6_attach,
1176 .pru_bind = rip6_bind,
1177 .pru_connect = rip6_connect,
1178 .pru_control = in6_control,
1179 .pru_detach = rip6_detach,
1180 .pru_disconnect = rip6_disconnect,
1181 .pru_peeraddr = in6_getpeeraddr,
1182 .pru_send = rip6_send,
1183 .pru_shutdown = rip6_shutdown,
1184 .pru_sockaddr = in6_getsockaddr,
1185 .pru_sosend = sosend,
1186 .pru_soreceive = soreceive,
1187 };
1188
1189 __private_extern__ struct pr_usrreqs icmp6_dgram_usrreqs = {
1190 .pru_abort = rip6_abort,
1191 .pru_attach = icmp6_dgram_attach,
1192 .pru_bind = rip6_bind,
1193 .pru_connect = rip6_connect,
1194 .pru_control = in6_control,
1195 .pru_detach = rip6_detach,
1196 .pru_disconnect = rip6_disconnect,
1197 .pru_peeraddr = in6_getpeeraddr,
1198 .pru_send = icmp6_dgram_send,
1199 .pru_shutdown = rip6_shutdown,
1200 .pru_sockaddr = in6_getsockaddr,
1201 .pru_sosend = sosend,
1202 .pru_soreceive = soreceive,
1203 };