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