]>
Commit | Line | Data |
---|---|---|
6d2010ae | 1 | /* |
5ba3f43e | 2 | * Copyright (c) 2000-2017 Apple Inc. All rights reserved. |
6d2010ae A |
3 | * |
4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ | |
39037602 | 5 | * |
6d2010ae 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 | * |
6d2010ae 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 | * |
6d2010ae 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 | * |
6d2010ae A |
26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
27 | */ | |
28 | ||
9bccf70c A |
29 | /* $FreeBSD: src/sys/netinet6/udp6_usrreq.c,v 1.6.2.6 2001/07/29 19:32:40 ume Exp $ */ |
30 | /* $KAME: udp6_usrreq.c,v 1.27 2001/05/21 05:45:10 jinmei Exp $ */ | |
1c79356b A |
31 | |
32 | /* | |
33 | * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. | |
34 | * All rights reserved. | |
35 | * | |
36 | * Redistribution and use in source and binary forms, with or without | |
37 | * modification, are permitted provided that the following conditions | |
38 | * are met: | |
39 | * 1. Redistributions of source code must retain the above copyright | |
40 | * notice, this list of conditions and the following disclaimer. | |
41 | * 2. Redistributions in binary form must reproduce the above copyright | |
42 | * notice, this list of conditions and the following disclaimer in the | |
43 | * documentation and/or other materials provided with the distribution. | |
44 | * 3. Neither the name of the project nor the names of its contributors | |
45 | * may be used to endorse or promote products derived from this software | |
46 | * without specific prior written permission. | |
47 | * | |
48 | * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND | |
49 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
50 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
51 | * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE | |
52 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
53 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
54 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
55 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
56 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
57 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
58 | * SUCH DAMAGE. | |
59 | */ | |
60 | ||
61 | /* | |
62 | * Copyright (c) 1982, 1986, 1989, 1993 | |
63 | * The Regents of the University of California. All rights reserved. | |
64 | * | |
65 | * Redistribution and use in source and binary forms, with or without | |
66 | * modification, are permitted provided that the following conditions | |
67 | * are met: | |
68 | * 1. Redistributions of source code must retain the above copyright | |
69 | * notice, this list of conditions and the following disclaimer. | |
70 | * 2. Redistributions in binary form must reproduce the above copyright | |
71 | * notice, this list of conditions and the following disclaimer in the | |
72 | * documentation and/or other materials provided with the distribution. | |
73 | * 3. All advertising materials mentioning features or use of this software | |
74 | * must display the following acknowledgement: | |
75 | * This product includes software developed by the University of | |
76 | * California, Berkeley and its contributors. | |
77 | * 4. Neither the name of the University nor the names of its contributors | |
78 | * may be used to endorse or promote products derived from this software | |
79 | * without specific prior written permission. | |
80 | * | |
81 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
82 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
83 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
84 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
85 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
86 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
87 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
88 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
89 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
90 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
91 | * SUCH DAMAGE. | |
92 | * | |
93 | * @(#)udp_var.h 8.1 (Berkeley) 6/10/93 | |
94 | */ | |
1c79356b A |
95 | #include <sys/kernel.h> |
96 | #include <sys/malloc.h> | |
97 | #include <sys/mbuf.h> | |
5ba3f43e | 98 | #include <sys/param.h> |
1c79356b A |
99 | #include <sys/protosw.h> |
100 | #include <sys/socket.h> | |
101 | #include <sys/socketvar.h> | |
102 | #include <sys/sysctl.h> | |
103 | #include <sys/errno.h> | |
104 | #include <sys/stat.h> | |
105 | #include <sys/systm.h> | |
106 | #include <sys/syslog.h> | |
107 | #include <sys/proc.h> | |
91447636 | 108 | #include <sys/kauth.h> |
1c79356b A |
109 | |
110 | #include <net/if.h> | |
111 | #include <net/route.h> | |
112 | #include <net/if_types.h> | |
6d2010ae | 113 | #include <net/ntstat.h> |
39236c6e | 114 | #include <net/dlil.h> |
5ba3f43e | 115 | #include <net/net_api_stats.h> |
1c79356b A |
116 | |
117 | #include <netinet/in.h> | |
118 | #include <netinet/in_systm.h> | |
119 | #include <netinet/ip.h> | |
120 | #include <netinet/in_pcb.h> | |
121 | #include <netinet/in_var.h> | |
122 | #include <netinet/ip_var.h> | |
123 | #include <netinet/udp.h> | |
124 | #include <netinet/udp_var.h> | |
125 | #include <netinet/ip6.h> | |
126 | #include <netinet6/ip6_var.h> | |
127 | #include <netinet6/in6_pcb.h> | |
128 | #include <netinet/icmp6.h> | |
129 | #include <netinet6/udp6_var.h> | |
130 | #include <netinet6/ip6protosw.h> | |
131 | ||
132 | #if IPSEC | |
133 | #include <netinet6/ipsec.h> | |
9bccf70c | 134 | #include <netinet6/ipsec6.h> |
3e170ce0 A |
135 | #include <netinet6/esp6.h> |
136 | extern int ipsec_bypass; | |
137 | extern int esp_udp_encap_port; | |
39236c6e | 138 | #endif /* IPSEC */ |
1c79356b | 139 | |
fe8ab488 A |
140 | #if NECP |
141 | #include <net/necp.h> | |
142 | #endif /* NECP */ | |
143 | ||
3e170ce0 A |
144 | #if FLOW_DIVERT |
145 | #include <netinet/flow_divert.h> | |
146 | #endif /* FLOW_DIVERT */ | |
147 | ||
d9a64523 A |
148 | #if CONTENT_FILTER |
149 | #include <net/content_filter.h> | |
150 | #endif /* CONTENT_FILTER */ | |
151 | ||
1c79356b A |
152 | /* |
153 | * UDP protocol inplementation. | |
154 | * Per RFC 768, August, 1980. | |
155 | */ | |
156 | ||
39236c6e A |
157 | static int udp6_abort(struct socket *); |
158 | static int udp6_attach(struct socket *, int, struct proc *); | |
159 | static int udp6_bind(struct socket *, struct sockaddr *, struct proc *); | |
813fb2f6 A |
160 | static int udp6_connectx(struct socket *, struct sockaddr *, |
161 | struct sockaddr *, struct proc *, uint32_t, sae_associd_t, | |
3e170ce0 | 162 | sae_connid_t *, uint32_t, void *, uint32_t, struct uio *, user_ssize_t *); |
39236c6e A |
163 | static int udp6_detach(struct socket *); |
164 | static int udp6_disconnect(struct socket *); | |
3e170ce0 | 165 | static int udp6_disconnectx(struct socket *, sae_associd_t, sae_connid_t); |
39236c6e A |
166 | static int udp6_send(struct socket *, int, struct mbuf *, struct sockaddr *, |
167 | struct mbuf *, struct proc *); | |
2d21ac55 | 168 | static void udp6_append(struct inpcb *, struct ip6_hdr *, |
39236c6e A |
169 | struct sockaddr_in6 *, struct mbuf *, int, struct ifnet *); |
170 | static int udp6_input_checksum(struct mbuf *, struct udphdr *, int, int); | |
91447636 | 171 | |
39236c6e A |
172 | struct pr_usrreqs udp6_usrreqs = { |
173 | .pru_abort = udp6_abort, | |
174 | .pru_attach = udp6_attach, | |
175 | .pru_bind = udp6_bind, | |
176 | .pru_connect = udp6_connect, | |
177 | .pru_connectx = udp6_connectx, | |
178 | .pru_control = in6_control, | |
179 | .pru_detach = udp6_detach, | |
180 | .pru_disconnect = udp6_disconnect, | |
181 | .pru_disconnectx = udp6_disconnectx, | |
182 | .pru_peeraddr = in6_mapped_peeraddr, | |
183 | .pru_send = udp6_send, | |
184 | .pru_shutdown = udp_shutdown, | |
185 | .pru_sockaddr = in6_mapped_sockaddr, | |
186 | .pru_sosend = sosend, | |
187 | .pru_soreceive = soreceive, | |
3e170ce0 | 188 | .pru_soreceive_list = soreceive_list, |
39236c6e | 189 | }; |
1c79356b | 190 | |
2d21ac55 A |
191 | /* |
192 | * subroutine of udp6_input(), mainly for source code readability. | |
193 | */ | |
194 | static void | |
39236c6e A |
195 | udp6_append(struct inpcb *last, struct ip6_hdr *ip6, |
196 | struct sockaddr_in6 *udp_in6, struct mbuf *n, int off, struct ifnet *ifp) | |
2d21ac55 | 197 | { |
39236c6e | 198 | #pragma unused(ip6) |
2d21ac55 | 199 | struct mbuf *opts = NULL; |
6d2010ae | 200 | int ret = 0; |
39236c6e A |
201 | boolean_t cell = IFNET_IS_CELLULAR(ifp); |
202 | boolean_t wifi = (!cell && IFNET_IS_WIFI(ifp)); | |
fe8ab488 | 203 | boolean_t wired = (!wifi && IFNET_IS_WIRED(ifp)); |
39236c6e | 204 | |
2d21ac55 A |
205 | #if CONFIG_MACF_NET |
206 | if (mac_inpcb_check_deliver(last, n, AF_INET6, SOCK_DGRAM) != 0) { | |
207 | m_freem(n); | |
208 | return; | |
209 | } | |
39236c6e A |
210 | #endif /* CONFIG_MACF_NET */ |
211 | if ((last->in6p_flags & INP_CONTROLOPTS) != 0 || | |
6d2010ae | 212 | (last->in6p_socket->so_options & SO_TIMESTAMP) != 0 || |
d9a64523 A |
213 | (last->in6p_socket->so_options & SO_TIMESTAMP_MONOTONIC) != 0 || |
214 | (last->in6p_socket->so_options & SO_TIMESTAMP_CONTINUOUS) != 0) { | |
6d2010ae A |
215 | ret = ip6_savecontrol(last, n, &opts); |
216 | if (ret != 0) { | |
217 | m_freem(n); | |
218 | m_freem(opts); | |
219 | return; | |
220 | } | |
221 | } | |
2d21ac55 | 222 | m_adj(n, off); |
6d2010ae | 223 | if (nstat_collect) { |
fe8ab488 A |
224 | INP_ADD_STAT(last, cell, wifi, wired, rxpackets, 1); |
225 | INP_ADD_STAT(last, cell, wifi, wired, rxbytes, n->m_pkthdr.len); | |
5ba3f43e | 226 | inp_set_activity_bitmap(last); |
6d2010ae A |
227 | } |
228 | so_recv_data_stat(last->in6p_socket, n, 0); | |
2d21ac55 A |
229 | if (sbappendaddr(&last->in6p_socket->so_rcv, |
230 | (struct sockaddr *)udp_in6, n, opts, NULL) == 0) | |
231 | udpstat.udps_fullsock++; | |
232 | else | |
233 | sorwakeup(last->in6p_socket); | |
234 | } | |
235 | ||
1c79356b | 236 | int |
39236c6e | 237 | udp6_input(struct mbuf **mp, int *offp, int proto) |
1c79356b | 238 | { |
6d2010ae | 239 | #pragma unused(proto) |
1c79356b | 240 | struct mbuf *m = *mp; |
6d2010ae | 241 | struct ifnet *ifp; |
39236c6e A |
242 | struct ip6_hdr *ip6; |
243 | struct udphdr *uh; | |
244 | struct inpcb *in6p; | |
9bccf70c | 245 | struct mbuf *opts = NULL; |
1c79356b | 246 | int off = *offp; |
6d2010ae | 247 | int plen, ulen, ret = 0; |
fe8ab488 | 248 | boolean_t cell, wifi, wired; |
1c79356b | 249 | struct sockaddr_in6 udp_in6; |
91447636 | 250 | struct inpcbinfo *pcbinfo = &udbinfo; |
6d2010ae | 251 | struct sockaddr_in6 fromsa; |
1c79356b | 252 | |
39236c6e | 253 | IP6_EXTHDR_CHECK(m, off, sizeof (struct udphdr), return IPPROTO_DONE); |
9bccf70c | 254 | |
316670eb A |
255 | /* Expect 32-bit aligned data pointer on strict-align platforms */ |
256 | MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m); | |
257 | ||
6d2010ae | 258 | ifp = m->m_pkthdr.rcvif; |
9bccf70c | 259 | ip6 = mtod(m, struct ip6_hdr *); |
39236c6e A |
260 | cell = IFNET_IS_CELLULAR(ifp); |
261 | wifi = (!cell && IFNET_IS_WIFI(ifp)); | |
fe8ab488 | 262 | wired = (!wifi && IFNET_IS_WIRED(ifp)); |
9bccf70c | 263 | |
9bccf70c | 264 | udpstat.udps_ipackets++; |
1c79356b | 265 | |
39236c6e | 266 | plen = ntohs(ip6->ip6_plen) - off + sizeof (*ip6); |
316670eb | 267 | uh = (struct udphdr *)(void *)((caddr_t)ip6 + off); |
1c79356b A |
268 | ulen = ntohs((u_short)uh->uh_ulen); |
269 | ||
270 | if (plen != ulen) { | |
271 | udpstat.udps_badlen++; | |
39236c6e | 272 | IF_UDP_STATINC(ifp, badlength); |
1c79356b A |
273 | goto bad; |
274 | } | |
275 | ||
6d2010ae | 276 | /* destination port of 0 is illegal, based on RFC768. */ |
316670eb | 277 | if (uh->uh_dport == 0) { |
39236c6e | 278 | IF_UDP_STATINC(ifp, port0); |
6d2010ae | 279 | goto bad; |
316670eb | 280 | } |
6d2010ae | 281 | |
1c79356b A |
282 | /* |
283 | * Checksum extended UDP header and data. | |
284 | */ | |
39236c6e A |
285 | if (udp6_input_checksum(m, uh, off, ulen)) |
286 | goto bad; | |
6d2010ae A |
287 | |
288 | /* | |
289 | * Construct sockaddr format source address. | |
290 | */ | |
291 | init_sin6(&fromsa, m); | |
292 | fromsa.sin6_port = uh->uh_sport; | |
293 | ||
1c79356b | 294 | if (IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { |
2d21ac55 | 295 | int reuse_sock = 0, mcast_delivered = 0; |
6d2010ae | 296 | struct ip6_moptions *imo; |
1c79356b A |
297 | |
298 | /* | |
299 | * Deliver a multicast datagram to all sockets | |
300 | * for which the local and remote addresses and ports match | |
301 | * those of the incoming datagram. This allows more than | |
302 | * one process to receive multicasts on the same port. | |
303 | * (This really ought to be done for unicast datagrams as | |
304 | * well, but that would cause problems with existing | |
305 | * applications that open both address-specific sockets and | |
306 | * a wildcard socket listening to the same port -- they would | |
307 | * end up receiving duplicates of every unicast datagram. | |
308 | * Those applications open the multiple sockets to overcome an | |
309 | * inadequacy of the UDP socket interface, but for backwards | |
310 | * compatibility we avoid the problem here rather than | |
311 | * fixing the interface. Maybe 4.5BSD will remedy this?) | |
312 | */ | |
313 | ||
314 | /* | |
315 | * In a case that laddr should be set to the link-local | |
316 | * address (this happens in RIPng), the multicast address | |
317 | * specified in the received packet does not match with | |
318 | * laddr. To cure this situation, the matching is relaxed | |
319 | * if the receiving interface is the same as one specified | |
320 | * in the socket and if the destination multicast address | |
321 | * matches one of the multicast groups specified in the socket. | |
322 | */ | |
323 | ||
324 | /* | |
325 | * Construct sockaddr format source address. | |
326 | */ | |
327 | init_sin6(&udp_in6, m); /* general init */ | |
328 | udp_in6.sin6_port = uh->uh_sport; | |
329 | /* | |
330 | * KAME note: usually we drop udphdr from mbuf here. | |
331 | * We need udphdr for IPsec processing so we do that later. | |
332 | */ | |
333 | ||
334 | /* | |
335 | * Locate pcb(s) for datagram. | |
336 | * (Algorithm copied from raw_intr().) | |
337 | */ | |
39236c6e | 338 | lck_rw_lock_shared(pcbinfo->ipi_lock); |
91447636 | 339 | |
1c79356b | 340 | LIST_FOREACH(in6p, &udb, inp_list) { |
39236c6e A |
341 | #if IPSEC |
342 | int skipit; | |
343 | #endif /* IPSEC */ | |
91447636 | 344 | |
1c79356b A |
345 | if ((in6p->inp_vflag & INP_IPV6) == 0) |
346 | continue; | |
91447636 | 347 | |
fe8ab488 | 348 | if (inp_restricted_recv(in6p, ifp)) |
316670eb A |
349 | continue; |
350 | ||
39236c6e A |
351 | if (in_pcb_checkstate(in6p, WNT_ACQUIRE, 0) == |
352 | WNT_STOPUSING) | |
91447636 A |
353 | continue; |
354 | ||
316670eb | 355 | udp_lock(in6p->in6p_socket, 1, 0); |
91447636 | 356 | |
39236c6e A |
357 | if (in_pcb_checkstate(in6p, WNT_RELEASE, 1) == |
358 | WNT_STOPUSING) { | |
91447636 A |
359 | udp_unlock(in6p->in6p_socket, 1, 0); |
360 | continue; | |
361 | } | |
362 | if (in6p->in6p_lport != uh->uh_dport) { | |
363 | udp_unlock(in6p->in6p_socket, 1, 0); | |
1c79356b | 364 | continue; |
91447636 | 365 | } |
6d2010ae A |
366 | |
367 | /* | |
368 | * Handle socket delivery policy for any-source | |
369 | * and source-specific multicast. [RFC3678] | |
370 | */ | |
371 | imo = in6p->in6p_moptions; | |
372 | if (imo && IN6_IS_ADDR_MULTICAST(&ip6->ip6_dst)) { | |
39236c6e A |
373 | struct sockaddr_in6 mcaddr; |
374 | int blocked; | |
6d2010ae | 375 | |
316670eb | 376 | IM6O_LOCK(imo); |
39236c6e A |
377 | bzero(&mcaddr, sizeof (struct sockaddr_in6)); |
378 | mcaddr.sin6_len = sizeof (struct sockaddr_in6); | |
6d2010ae A |
379 | mcaddr.sin6_family = AF_INET6; |
380 | mcaddr.sin6_addr = ip6->ip6_dst; | |
381 | ||
382 | blocked = im6o_mc_filter(imo, ifp, | |
5ba3f43e | 383 | &mcaddr, &fromsa); |
316670eb | 384 | IM6O_UNLOCK(imo); |
6d2010ae | 385 | if (blocked != MCAST_PASS) { |
91447636 | 386 | udp_unlock(in6p->in6p_socket, 1, 0); |
39236c6e A |
387 | if (blocked == MCAST_NOTSMEMBER || |
388 | blocked == MCAST_MUTED) | |
389 | udpstat.udps_filtermcast++; | |
1c79356b | 390 | continue; |
91447636 | 391 | } |
1c79356b | 392 | } |
39236c6e A |
393 | if (!IN6_IS_ADDR_UNSPECIFIED(&in6p->in6p_faddr) && |
394 | (!IN6_ARE_ADDR_EQUAL(&in6p->in6p_faddr, | |
395 | &ip6->ip6_src) || | |
396 | in6p->in6p_fport != uh->uh_sport)) { | |
397 | udp_unlock(in6p->in6p_socket, 1, 0); | |
398 | continue; | |
1c79356b A |
399 | } |
400 | ||
2d21ac55 A |
401 | reuse_sock = in6p->inp_socket->so_options & |
402 | (SO_REUSEPORT | SO_REUSEADDR); | |
1c79356b | 403 | |
fe8ab488 | 404 | #if NECP |
39236c6e | 405 | skipit = 0; |
fe8ab488 A |
406 | if (!necp_socket_is_allowed_to_send_recv_v6(in6p, |
407 | uh->uh_dport, uh->uh_sport, &ip6->ip6_dst, | |
d9a64523 | 408 | &ip6->ip6_src, ifp, NULL, NULL, NULL)) { |
39236c6e A |
409 | /* do not inject data to pcb */ |
410 | skipit = 1; | |
411 | } | |
412 | if (skipit == 0) | |
fe8ab488 | 413 | #endif /* NECP */ |
39236c6e | 414 | { |
fe8ab488 | 415 | struct mbuf *n = NULL; |
39236c6e A |
416 | /* |
417 | * KAME NOTE: do not | |
418 | * m_copy(m, offset, ...) below. | |
419 | * sbappendaddr() expects M_PKTHDR, | |
420 | * and m_copy() will copy M_PKTHDR | |
421 | * only if offset is 0. | |
422 | */ | |
423 | if (reuse_sock) | |
424 | n = m_copy(m, 0, M_COPYALL); | |
425 | udp6_append(in6p, ip6, &udp_in6, m, | |
426 | off + sizeof (struct udphdr), ifp); | |
427 | mcast_delivered++; | |
fe8ab488 | 428 | m = n; |
1c79356b | 429 | } |
39236c6e A |
430 | udp_unlock(in6p->in6p_socket, 1, 0); |
431 | ||
1c79356b A |
432 | /* |
433 | * Don't look for additional matches if this one does | |
434 | * not have either the SO_REUSEPORT or SO_REUSEADDR | |
435 | * socket options set. This heuristic avoids searching | |
436 | * through all pcbs in the common case of a non-shared | |
437 | * port. It assumes that an application will never | |
438 | * clear these options after setting them. | |
439 | */ | |
fe8ab488 | 440 | if (reuse_sock == 0 || m == NULL) |
1c79356b | 441 | break; |
316670eb A |
442 | |
443 | /* | |
444 | * Expect 32-bit aligned data pointer on strict-align | |
445 | * platforms. | |
446 | */ | |
447 | MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m); | |
448 | ||
b0d623f7 A |
449 | /* |
450 | * Recompute IP and UDP header pointers for new mbuf | |
451 | */ | |
452 | ip6 = mtod(m, struct ip6_hdr *); | |
316670eb | 453 | uh = (struct udphdr *)(void *)((caddr_t)ip6 + off); |
1c79356b | 454 | } |
39236c6e | 455 | lck_rw_done(pcbinfo->ipi_lock); |
1c79356b | 456 | |
2d21ac55 | 457 | if (mcast_delivered == 0) { |
1c79356b A |
458 | /* |
459 | * No matching pcb found; discard datagram. | |
460 | * (No need to send an ICMP Port Unreachable | |
461 | * for a broadcast or multicast datgram.) | |
462 | */ | |
463 | udpstat.udps_noport++; | |
9bccf70c | 464 | udpstat.udps_noportmcast++; |
39236c6e | 465 | IF_UDP_STATINC(ifp, port_unreach); |
1c79356b A |
466 | goto bad; |
467 | } | |
2d21ac55 | 468 | |
fe8ab488 A |
469 | /* free the extra copy of mbuf or skipped by NECP */ |
470 | if (m != NULL) | |
2d21ac55 | 471 | m_freem(m); |
39236c6e | 472 | return (IPPROTO_DONE); |
1c79356b | 473 | } |
3e170ce0 A |
474 | |
475 | #if IPSEC | |
476 | /* | |
477 | * UDP to port 4500 with a payload where the first four bytes are | |
478 | * not zero is a UDP encapsulated IPSec packet. Packets where | |
479 | * the payload is one byte and that byte is 0xFF are NAT keepalive | |
480 | * packets. Decapsulate the ESP packet and carry on with IPSec input | |
481 | * or discard the NAT keep-alive. | |
482 | */ | |
483 | if (ipsec_bypass == 0 && (esp_udp_encap_port & 0xFFFF) != 0 && | |
484 | uh->uh_dport == ntohs((u_short)esp_udp_encap_port)) { | |
485 | int payload_len = ulen - sizeof (struct udphdr) > 4 ? 4 : | |
486 | ulen - sizeof (struct udphdr); | |
487 | ||
488 | if (m->m_len < off + sizeof (struct udphdr) + payload_len) { | |
489 | if ((m = m_pullup(m, off + sizeof (struct udphdr) + | |
490 | payload_len)) == NULL) { | |
491 | udpstat.udps_hdrops++; | |
492 | goto bad; | |
493 | } | |
494 | /* | |
495 | * Expect 32-bit aligned data pointer on strict-align | |
496 | * platforms. | |
497 | */ | |
498 | MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m); | |
499 | ||
500 | ip6 = mtod(m, struct ip6_hdr *); | |
501 | uh = (struct udphdr *)(void *)((caddr_t)ip6 + off); | |
502 | } | |
503 | /* Check for NAT keepalive packet */ | |
504 | if (payload_len == 1 && *(u_int8_t*) | |
505 | ((caddr_t)uh + sizeof (struct udphdr)) == 0xFF) { | |
506 | goto bad; | |
507 | } else if (payload_len == 4 && *(u_int32_t*)(void *) | |
508 | ((caddr_t)uh + sizeof (struct udphdr)) != 0) { | |
509 | /* UDP encapsulated IPSec packet to pass through NAT */ | |
510 | /* preserve the udp header */ | |
511 | *offp = off + sizeof (struct udphdr); | |
512 | return (esp6_input(mp, offp, IPPROTO_UDP)); | |
513 | } | |
514 | } | |
515 | #endif /* IPSEC */ | |
516 | ||
1c79356b A |
517 | /* |
518 | * Locate pcb for datagram. | |
519 | */ | |
520 | in6p = in6_pcblookup_hash(&udbinfo, &ip6->ip6_src, uh->uh_sport, | |
39236c6e | 521 | &ip6->ip6_dst, uh->uh_dport, 1, m->m_pkthdr.rcvif); |
316670eb | 522 | if (in6p == NULL) { |
39236c6e | 523 | IF_UDP_STATINC(ifp, port_unreach); |
316670eb | 524 | |
39236c6e | 525 | if (udp_log_in_vain) { |
1c79356b A |
526 | char buf[INET6_ADDRSTRLEN]; |
527 | ||
39236c6e A |
528 | strlcpy(buf, ip6_sprintf(&ip6->ip6_dst), sizeof (buf)); |
529 | if (udp_log_in_vain < 3) { | |
530 | log(LOG_INFO, "Connection attempt to UDP " | |
531 | "%s:%d from %s:%d\n", buf, | |
532 | ntohs(uh->uh_dport), | |
533 | ip6_sprintf(&ip6->ip6_src), | |
534 | ntohs(uh->uh_sport)); | |
535 | } else if (!(m->m_flags & (M_BCAST | M_MCAST)) && | |
536 | !IN6_ARE_ADDR_EQUAL(&ip6->ip6_dst, &ip6->ip6_src)) { | |
39037602 | 537 | log(LOG_INFO, "Connection attempt " |
39236c6e A |
538 | "to UDP %s:%d from %s:%d\n", buf, |
539 | ntohs(uh->uh_dport), | |
540 | ip6_sprintf(&ip6->ip6_src), | |
39037602 | 541 | ntohs(uh->uh_sport)); |
39236c6e | 542 | } |
1c79356b A |
543 | } |
544 | udpstat.udps_noport++; | |
545 | if (m->m_flags & M_MCAST) { | |
546 | printf("UDP6: M_MCAST is set in a unicast packet.\n"); | |
9bccf70c | 547 | udpstat.udps_noportmcast++; |
39236c6e | 548 | IF_UDP_STATINC(ifp, badmcast); |
1c79356b A |
549 | goto bad; |
550 | } | |
551 | icmp6_error(m, ICMP6_DST_UNREACH, ICMP6_DST_UNREACH_NOPORT, 0); | |
39236c6e | 552 | return (IPPROTO_DONE); |
1c79356b | 553 | } |
fe8ab488 A |
554 | #if NECP |
555 | if (!necp_socket_is_allowed_to_send_recv_v6(in6p, uh->uh_dport, | |
d9a64523 | 556 | uh->uh_sport, &ip6->ip6_dst, &ip6->ip6_src, ifp, NULL, NULL, NULL)) { |
fe8ab488 A |
557 | in_pcb_checkstate(in6p, WNT_RELEASE, 0); |
558 | IF_UDP_STATINC(ifp, badipsec); | |
559 | goto bad; | |
1c79356b | 560 | } |
fe8ab488 | 561 | #endif /* NECP */ |
1c79356b A |
562 | |
563 | /* | |
564 | * Construct sockaddr format source address. | |
565 | * Stuff source address and datagram in user buffer. | |
566 | */ | |
91447636 A |
567 | udp_lock(in6p->in6p_socket, 1, 0); |
568 | ||
569 | if (in_pcb_checkstate(in6p, WNT_RELEASE, 1) == WNT_STOPUSING) { | |
570 | udp_unlock(in6p->in6p_socket, 1, 0); | |
39236c6e | 571 | IF_UDP_STATINC(ifp, cleanup); |
91447636 A |
572 | goto bad; |
573 | } | |
316670eb | 574 | |
1c79356b A |
575 | init_sin6(&udp_in6, m); /* general init */ |
576 | udp_in6.sin6_port = uh->uh_sport; | |
39236c6e A |
577 | if ((in6p->in6p_flags & INP_CONTROLOPTS) != 0 || |
578 | (in6p->in6p_socket->so_options & SO_TIMESTAMP) != 0 || | |
d9a64523 A |
579 | (in6p->in6p_socket->so_options & SO_TIMESTAMP_MONOTONIC) != 0 || |
580 | (in6p->in6p_socket->so_options & SO_TIMESTAMP_CONTINUOUS) != 0) { | |
6d2010ae A |
581 | ret = ip6_savecontrol(in6p, m, &opts); |
582 | if (ret != 0) { | |
583 | udp_unlock(in6p->in6p_socket, 1, 0); | |
584 | goto bad; | |
585 | } | |
586 | } | |
39236c6e | 587 | m_adj(m, off + sizeof (struct udphdr)); |
6d2010ae | 588 | if (nstat_collect) { |
fe8ab488 A |
589 | INP_ADD_STAT(in6p, cell, wifi, wired, rxpackets, 1); |
590 | INP_ADD_STAT(in6p, cell, wifi, wired, rxbytes, m->m_pkthdr.len); | |
5ba3f43e | 591 | inp_set_activity_bitmap(in6p); |
6d2010ae A |
592 | } |
593 | so_recv_data_stat(in6p->in6p_socket, m, 0); | |
1c79356b | 594 | if (sbappendaddr(&in6p->in6p_socket->so_rcv, |
39236c6e | 595 | (struct sockaddr *)&udp_in6, m, opts, NULL) == 0) { |
91447636 A |
596 | m = NULL; |
597 | opts = NULL; | |
1c79356b | 598 | udpstat.udps_fullsock++; |
91447636 | 599 | udp_unlock(in6p->in6p_socket, 1, 0); |
1c79356b A |
600 | goto bad; |
601 | } | |
602 | sorwakeup(in6p->in6p_socket); | |
91447636 | 603 | udp_unlock(in6p->in6p_socket, 1, 0); |
39236c6e | 604 | return (IPPROTO_DONE); |
1c79356b | 605 | bad: |
39236c6e | 606 | if (m != NULL) |
1c79356b | 607 | m_freem(m); |
39236c6e | 608 | if (opts != NULL) |
9bccf70c | 609 | m_freem(opts); |
39236c6e | 610 | return (IPPROTO_DONE); |
1c79356b A |
611 | } |
612 | ||
613 | void | |
5ba3f43e | 614 | udp6_ctlinput(int cmd, struct sockaddr *sa, void *d, __unused struct ifnet *ifp) |
1c79356b | 615 | { |
1c79356b | 616 | struct udphdr uh; |
1c79356b A |
617 | struct ip6_hdr *ip6; |
618 | struct mbuf *m; | |
619 | int off = 0; | |
9bccf70c | 620 | struct ip6ctlparam *ip6cp = NULL; |
5ba3f43e | 621 | struct icmp6_hdr *icmp6 = NULL; |
9bccf70c | 622 | const struct sockaddr_in6 *sa6_src = NULL; |
91447636 | 623 | void (*notify)(struct inpcb *, int) = udp_notify; |
9bccf70c A |
624 | struct udp_portonly { |
625 | u_int16_t uh_sport; | |
626 | u_int16_t uh_dport; | |
627 | } *uhp; | |
1c79356b A |
628 | |
629 | if (sa->sa_family != AF_INET6 || | |
39236c6e | 630 | sa->sa_len != sizeof (struct sockaddr_in6)) |
1c79356b A |
631 | return; |
632 | ||
633 | if ((unsigned)cmd >= PRC_NCMDS) | |
634 | return; | |
39037602 A |
635 | if (PRC_IS_REDIRECT(cmd)) { |
636 | notify = in6_rtchange; | |
637 | d = NULL; | |
638 | } else if (cmd == PRC_HOSTDEAD) | |
1c79356b A |
639 | d = NULL; |
640 | else if (inet6ctlerrmap[cmd] == 0) | |
641 | return; | |
642 | ||
643 | /* if the parameter is from icmp6, decode it. */ | |
644 | if (d != NULL) { | |
9bccf70c | 645 | ip6cp = (struct ip6ctlparam *)d; |
5ba3f43e | 646 | icmp6 = ip6cp->ip6c_icmp6; |
1c79356b A |
647 | m = ip6cp->ip6c_m; |
648 | ip6 = ip6cp->ip6c_ip6; | |
649 | off = ip6cp->ip6c_off; | |
9bccf70c | 650 | sa6_src = ip6cp->ip6c_src; |
1c79356b A |
651 | } else { |
652 | m = NULL; | |
653 | ip6 = NULL; | |
9bccf70c | 654 | sa6_src = &sa6_any; |
1c79356b A |
655 | } |
656 | ||
39236c6e | 657 | if (ip6 != NULL) { |
1c79356b A |
658 | /* |
659 | * XXX: We assume that when IPV6 is non NULL, | |
660 | * M and OFF are valid. | |
661 | */ | |
9bccf70c | 662 | /* check if we can safely examine src and dst ports */ |
39236c6e | 663 | if (m->m_pkthdr.len < off + sizeof (*uhp)) |
9bccf70c | 664 | return; |
1c79356b | 665 | |
39236c6e A |
666 | bzero(&uh, sizeof (uh)); |
667 | m_copydata(m, off, sizeof (*uhp), (caddr_t)&uh); | |
9bccf70c | 668 | |
91447636 | 669 | (void) in6_pcbnotify(&udbinfo, sa, uh.uh_dport, |
39236c6e A |
670 | (struct sockaddr*)ip6cp->ip6c_src, uh.uh_sport, |
671 | cmd, NULL, notify); | |
672 | } else { | |
673 | (void) in6_pcbnotify(&udbinfo, sa, 0, | |
674 | (struct sockaddr *)&sa6_src, 0, cmd, NULL, notify); | |
1c79356b | 675 | } |
1c79356b A |
676 | } |
677 | ||
1c79356b A |
678 | static int |
679 | udp6_abort(struct socket *so) | |
680 | { | |
681 | struct inpcb *inp; | |
1c79356b A |
682 | |
683 | inp = sotoinpcb(so); | |
39236c6e A |
684 | if (inp == NULL) { |
685 | panic("%s: so=%p null inp\n", __func__, so); | |
686 | /* NOTREACHED */ | |
687 | } | |
1c79356b | 688 | soisdisconnected(so); |
1c79356b | 689 | in6_pcbdetach(inp); |
39236c6e | 690 | return (0); |
1c79356b A |
691 | } |
692 | ||
693 | static int | |
39236c6e | 694 | udp6_attach(struct socket *so, int proto, struct proc *p) |
1c79356b | 695 | { |
39236c6e | 696 | #pragma unused(proto) |
1c79356b | 697 | struct inpcb *inp; |
91447636 | 698 | int error; |
1c79356b A |
699 | |
700 | inp = sotoinpcb(so); | |
39236c6e A |
701 | if (inp != NULL) |
702 | return (EINVAL); | |
1c79356b | 703 | |
91447636 A |
704 | error = in_pcballoc(so, &udbinfo, p); |
705 | if (error) | |
39236c6e | 706 | return (error); |
91447636 | 707 | |
1c79356b A |
708 | if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) { |
709 | error = soreserve(so, udp_sendspace, udp_recvspace); | |
710 | if (error) | |
39236c6e | 711 | return (error); |
1c79356b | 712 | } |
1c79356b A |
713 | inp = (struct inpcb *)so->so_pcb; |
714 | inp->inp_vflag |= INP_IPV6; | |
55e303ae A |
715 | if (ip6_mapped_addr_on) |
716 | inp->inp_vflag |= INP_IPV4; | |
1c79356b A |
717 | inp->in6p_hops = -1; /* use kernel default */ |
718 | inp->in6p_cksum = -1; /* just to be sure */ | |
9bccf70c A |
719 | /* |
720 | * XXX: ugly!! | |
721 | * IPv4 TTL initialization is necessary for an IPv6 socket as well, | |
722 | * because the socket may be bound to an IPv6 wildcard address, | |
723 | * which may match an IPv4-mapped IPv6 address. | |
724 | */ | |
725 | inp->inp_ip_ttl = ip_defttl; | |
316670eb A |
726 | if (nstat_collect) |
727 | nstat_udp_new_pcb(inp); | |
39236c6e | 728 | return (0); |
1c79356b A |
729 | } |
730 | ||
731 | static int | |
732 | udp6_bind(struct socket *so, struct sockaddr *nam, struct proc *p) | |
733 | { | |
734 | struct inpcb *inp; | |
91447636 | 735 | int error; |
1c79356b A |
736 | |
737 | inp = sotoinpcb(so); | |
3e170ce0 A |
738 | if (inp == NULL) |
739 | return (EINVAL); | |
1c79356b A |
740 | |
741 | inp->inp_vflag &= ~INP_IPV4; | |
742 | inp->inp_vflag |= INP_IPV6; | |
9bccf70c | 743 | if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) { |
1c79356b A |
744 | struct sockaddr_in6 *sin6_p; |
745 | ||
316670eb | 746 | sin6_p = (struct sockaddr_in6 *)(void *)nam; |
1c79356b | 747 | |
39236c6e | 748 | if (IN6_IS_ADDR_UNSPECIFIED(&sin6_p->sin6_addr)) { |
1c79356b | 749 | inp->inp_vflag |= INP_IPV4; |
39236c6e | 750 | } else if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) { |
1c79356b A |
751 | struct sockaddr_in sin; |
752 | ||
753 | in6_sin6_2_sin(&sin, sin6_p); | |
754 | inp->inp_vflag |= INP_IPV4; | |
755 | inp->inp_vflag &= ~INP_IPV6; | |
1c79356b | 756 | error = in_pcbbind(inp, (struct sockaddr *)&sin, p); |
39236c6e | 757 | return (error); |
1c79356b A |
758 | } |
759 | } | |
760 | ||
1c79356b | 761 | error = in6_pcbbind(inp, nam, p); |
39236c6e | 762 | return (error); |
1c79356b A |
763 | } |
764 | ||
39236c6e | 765 | int |
1c79356b A |
766 | udp6_connect(struct socket *so, struct sockaddr *nam, struct proc *p) |
767 | { | |
768 | struct inpcb *inp; | |
91447636 | 769 | int error; |
3e170ce0 A |
770 | #if defined(NECP) && defined(FLOW_DIVERT) |
771 | int should_use_flow_divert = 0; | |
772 | #endif /* defined(NECP) && defined(FLOW_DIVERT) */ | |
1c79356b A |
773 | |
774 | inp = sotoinpcb(so); | |
3e170ce0 A |
775 | if (inp == NULL) |
776 | return (EINVAL); | |
777 | ||
778 | #if defined(NECP) && defined(FLOW_DIVERT) | |
779 | should_use_flow_divert = necp_socket_should_use_flow_divert(inp); | |
780 | #endif /* defined(NECP) && defined(FLOW_DIVERT) */ | |
1c79356b | 781 | |
9bccf70c | 782 | if ((inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) { |
1c79356b A |
783 | struct sockaddr_in6 *sin6_p; |
784 | ||
316670eb | 785 | sin6_p = (struct sockaddr_in6 *)(void *)nam; |
1c79356b A |
786 | if (IN6_IS_ADDR_V4MAPPED(&sin6_p->sin6_addr)) { |
787 | struct sockaddr_in sin; | |
788 | ||
789 | if (inp->inp_faddr.s_addr != INADDR_ANY) | |
39236c6e | 790 | return (EISCONN); |
5ba3f43e A |
791 | |
792 | if (!(so->so_flags1 & SOF1_CONNECT_COUNTED)) { | |
793 | so->so_flags1 |= SOF1_CONNECT_COUNTED; | |
794 | INC_ATOMIC_INT64_LIM(net_api_stats.nas_socket_inet_dgram_connected); | |
795 | } | |
796 | ||
1c79356b | 797 | in6_sin6_2_sin(&sin, sin6_p); |
3e170ce0 A |
798 | #if defined(NECP) && defined(FLOW_DIVERT) |
799 | if (should_use_flow_divert) { | |
800 | goto do_flow_divert; | |
801 | } | |
802 | #endif /* defined(NECP) && defined(FLOW_DIVERT) */ | |
39236c6e A |
803 | error = in_pcbconnect(inp, (struct sockaddr *)&sin, |
804 | p, IFSCOPE_NONE, NULL); | |
1c79356b | 805 | if (error == 0) { |
5ba3f43e A |
806 | #if NECP |
807 | /* Update NECP client with connected five-tuple */ | |
808 | if (!uuid_is_null(inp->necp_client_uuid)) { | |
809 | socket_unlock(so, 0); | |
810 | necp_client_assign_from_socket(so->last_pid, inp->necp_client_uuid, inp); | |
811 | socket_lock(so, 0); | |
812 | } | |
813 | #endif /* NECP */ | |
1c79356b A |
814 | inp->inp_vflag |= INP_IPV4; |
815 | inp->inp_vflag &= ~INP_IPV6; | |
816 | soisconnected(so); | |
817 | } | |
39236c6e | 818 | return (error); |
1c79356b A |
819 | } |
820 | } | |
821 | ||
822 | if (!IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) | |
39236c6e | 823 | return (EISCONN); |
3e170ce0 | 824 | |
5ba3f43e A |
825 | if (!(so->so_flags1 & SOF1_CONNECT_COUNTED)) { |
826 | so->so_flags1 |= SOF1_CONNECT_COUNTED; | |
827 | INC_ATOMIC_INT64_LIM(net_api_stats.nas_socket_inet6_dgram_connected); | |
828 | } | |
829 | ||
3e170ce0 A |
830 | #if defined(NECP) && defined(FLOW_DIVERT) |
831 | do_flow_divert: | |
832 | if (should_use_flow_divert) { | |
833 | uint32_t fd_ctl_unit = necp_socket_get_flow_divert_control_unit(inp); | |
834 | if (fd_ctl_unit > 0) { | |
835 | error = flow_divert_pcb_init(so, fd_ctl_unit); | |
836 | if (error == 0) { | |
837 | error = flow_divert_connect_out(so, nam, p); | |
838 | } | |
839 | } else { | |
840 | error = ENETDOWN; | |
841 | } | |
842 | return (error); | |
843 | } | |
844 | #endif /* defined(NECP) && defined(FLOW_DIVERT) */ | |
845 | ||
1c79356b | 846 | error = in6_pcbconnect(inp, nam, p); |
1c79356b | 847 | if (error == 0) { |
39236c6e A |
848 | /* should be non mapped addr */ |
849 | if (ip6_mapped_addr_on || | |
850 | (inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) { | |
1c79356b A |
851 | inp->inp_vflag &= ~INP_IPV4; |
852 | inp->inp_vflag |= INP_IPV6; | |
853 | } | |
5ba3f43e A |
854 | #if NECP |
855 | /* Update NECP client with connected five-tuple */ | |
856 | if (!uuid_is_null(inp->necp_client_uuid)) { | |
857 | socket_unlock(so, 0); | |
858 | necp_client_assign_from_socket(so->last_pid, inp->necp_client_uuid, inp); | |
859 | socket_lock(so, 0); | |
860 | } | |
861 | #endif /* NECP */ | |
1c79356b | 862 | soisconnected(so); |
316670eb A |
863 | if (inp->inp_flowhash == 0) |
864 | inp->inp_flowhash = inp_calc_flowhash(inp); | |
39236c6e A |
865 | /* update flowinfo - RFC 6437 */ |
866 | if (inp->inp_flow == 0 && | |
867 | inp->in6p_flags & IN6P_AUTOFLOWLABEL) { | |
868 | inp->inp_flow &= ~IPV6_FLOWLABEL_MASK; | |
869 | inp->inp_flow |= | |
870 | (htonl(inp->inp_flowhash) & IPV6_FLOWLABEL_MASK); | |
871 | } | |
1c79356b | 872 | } |
39236c6e A |
873 | return (error); |
874 | } | |
875 | ||
876 | static int | |
813fb2f6 A |
877 | udp6_connectx(struct socket *so, struct sockaddr *src, |
878 | struct sockaddr *dst, struct proc *p, uint32_t ifscope, | |
3e170ce0 A |
879 | sae_associd_t aid, sae_connid_t *pcid, uint32_t flags, void *arg, |
880 | uint32_t arglen, struct uio *uio, user_ssize_t *bytes_written) | |
39236c6e | 881 | { |
813fb2f6 | 882 | return (udp_connectx_common(so, AF_INET6, src, dst, |
3e170ce0 | 883 | p, ifscope, aid, pcid, flags, arg, arglen, uio, bytes_written)); |
1c79356b A |
884 | } |
885 | ||
886 | static int | |
887 | udp6_detach(struct socket *so) | |
888 | { | |
889 | struct inpcb *inp; | |
1c79356b A |
890 | |
891 | inp = sotoinpcb(so); | |
39236c6e A |
892 | if (inp == NULL) |
893 | return (EINVAL); | |
1c79356b | 894 | in6_pcbdetach(inp); |
39236c6e | 895 | return (0); |
1c79356b A |
896 | } |
897 | ||
898 | static int | |
899 | udp6_disconnect(struct socket *so) | |
900 | { | |
901 | struct inpcb *inp; | |
1c79356b A |
902 | |
903 | inp = sotoinpcb(so); | |
fe8ab488 A |
904 | if (inp == NULL |
905 | #if NECP | |
906 | || (necp_socket_should_use_flow_divert(inp)) | |
907 | #endif /* NECP */ | |
908 | ) | |
39236c6e | 909 | return (inp == NULL ? EINVAL : EPROTOTYPE); |
1c79356b A |
910 | |
911 | if (inp->inp_vflag & INP_IPV4) { | |
912 | struct pr_usrreqs *pru; | |
913 | ||
914 | pru = ip_protox[IPPROTO_UDP]->pr_usrreqs; | |
915 | return ((*pru->pru_disconnect)(so)); | |
916 | } | |
917 | ||
918 | if (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) | |
39236c6e | 919 | return (ENOTCONN); |
1c79356b | 920 | |
1c79356b | 921 | in6_pcbdisconnect(inp); |
316670eb A |
922 | |
923 | /* reset flow-controlled state, just in case */ | |
924 | inp_reset_fc_state(inp); | |
925 | ||
1c79356b | 926 | inp->in6p_laddr = in6addr_any; |
316670eb | 927 | inp->in6p_last_outifp = NULL; |
5ba3f43e | 928 | |
1c79356b | 929 | so->so_state &= ~SS_ISCONNECTED; /* XXX */ |
39236c6e A |
930 | return (0); |
931 | } | |
932 | ||
933 | static int | |
3e170ce0 | 934 | udp6_disconnectx(struct socket *so, sae_associd_t aid, sae_connid_t cid) |
39236c6e A |
935 | { |
936 | #pragma unused(cid) | |
3e170ce0 | 937 | if (aid != SAE_ASSOCID_ANY && aid != SAE_ASSOCID_ALL) |
39236c6e A |
938 | return (EINVAL); |
939 | ||
940 | return (udp6_disconnect(so)); | |
1c79356b A |
941 | } |
942 | ||
943 | static int | |
944 | udp6_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr, | |
39236c6e | 945 | struct mbuf *control, struct proc *p) |
1c79356b A |
946 | { |
947 | struct inpcb *inp; | |
9bccf70c | 948 | int error = 0; |
3e170ce0 A |
949 | #if defined(NECP) && defined(FLOW_DIVERT) |
950 | int should_use_flow_divert = 0; | |
951 | #endif /* defined(NECP) && defined(FLOW_DIVERT) */ | |
d9a64523 A |
952 | #if CONTENT_FILTER |
953 | struct m_tag *cfil_tag = NULL; | |
954 | struct sockaddr *cfil_faddr = NULL; | |
955 | #endif | |
1c79356b A |
956 | |
957 | inp = sotoinpcb(so); | |
3e170ce0 A |
958 | if (inp == NULL) { |
959 | error = EINVAL; | |
9bccf70c A |
960 | goto bad; |
961 | } | |
962 | ||
d9a64523 A |
963 | #if CONTENT_FILTER |
964 | //If socket is subject to UDP Content Filter and unconnected, get addr from tag. | |
965 | if (so->so_cfil_db && !addr && IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr)) { | |
966 | cfil_tag = cfil_udp_get_socket_state(m, NULL, NULL, &cfil_faddr); | |
967 | if (cfil_tag) { | |
968 | addr = (struct sockaddr *)cfil_faddr; | |
969 | } | |
970 | } | |
971 | #endif | |
972 | ||
3e170ce0 A |
973 | #if defined(NECP) && defined(FLOW_DIVERT) |
974 | should_use_flow_divert = necp_socket_should_use_flow_divert(inp); | |
975 | #endif /* defined(NECP) && defined(FLOW_DIVERT) */ | |
976 | ||
39236c6e A |
977 | if (addr != NULL) { |
978 | if (addr->sa_len != sizeof (struct sockaddr_in6)) { | |
9bccf70c A |
979 | error = EINVAL; |
980 | goto bad; | |
981 | } | |
982 | if (addr->sa_family != AF_INET6) { | |
983 | error = EAFNOSUPPORT; | |
984 | goto bad; | |
985 | } | |
1c79356b A |
986 | } |
987 | ||
55e303ae | 988 | if (ip6_mapped_addr_on || (inp->inp_flags & IN6P_IPV6_V6ONLY) == 0) { |
1c79356b | 989 | int hasv4addr; |
39236c6e | 990 | struct sockaddr_in6 *sin6 = NULL; |
1c79356b | 991 | |
39236c6e | 992 | if (addr == NULL) { |
1c79356b | 993 | hasv4addr = (inp->inp_vflag & INP_IPV4); |
39236c6e | 994 | } else { |
316670eb | 995 | sin6 = (struct sockaddr_in6 *)(void *)addr; |
39236c6e A |
996 | hasv4addr = |
997 | IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr) ? 1 : 0; | |
1c79356b A |
998 | } |
999 | if (hasv4addr) { | |
1000 | struct pr_usrreqs *pru; | |
1c79356b | 1001 | |
39236c6e | 1002 | if (sin6 != NULL) |
1c79356b | 1003 | in6_sin6_2_sin_in_sock(addr); |
3e170ce0 A |
1004 | #if defined(NECP) && defined(FLOW_DIVERT) |
1005 | if (should_use_flow_divert) { | |
1006 | goto do_flow_divert; | |
1007 | } | |
1008 | #endif /* defined(NECP) && defined(FLOW_DIVERT) */ | |
1c79356b | 1009 | pru = ip_protox[IPPROTO_UDP]->pr_usrreqs; |
39236c6e A |
1010 | error = ((*pru->pru_send)(so, flags, m, addr, |
1011 | control, p)); | |
d9a64523 A |
1012 | #if CONTENT_FILTER |
1013 | if (cfil_tag) | |
1014 | m_tag_free(cfil_tag); | |
1015 | #endif | |
1c79356b | 1016 | /* addr will just be freed in sendit(). */ |
39236c6e | 1017 | return (error); |
1c79356b A |
1018 | } |
1019 | } | |
3e170ce0 A |
1020 | |
1021 | #if defined(NECP) && defined(FLOW_DIVERT) | |
1022 | do_flow_divert: | |
1023 | if (should_use_flow_divert) { | |
1024 | /* Implicit connect */ | |
d9a64523 A |
1025 | error = flow_divert_implicit_data_out(so, flags, m, addr, control, p); |
1026 | #if CONTENT_FILTER | |
1027 | if (cfil_tag) | |
1028 | m_tag_free(cfil_tag); | |
1029 | #endif | |
1030 | return error; | |
3e170ce0 A |
1031 | } |
1032 | #endif /* defined(NECP) && defined(FLOW_DIVERT) */ | |
1033 | ||
d9a64523 A |
1034 | error = udp6_output(inp, m, addr, control, p); |
1035 | #if CONTENT_FILTER | |
1036 | if (cfil_tag) | |
1037 | m_tag_free(cfil_tag); | |
1038 | #endif | |
1039 | return error; | |
39236c6e A |
1040 | |
1041 | bad: | |
1042 | VERIFY(error != 0); | |
1c79356b | 1043 | |
39236c6e A |
1044 | if (m != NULL) |
1045 | m_freem(m); | |
1046 | if (control != NULL) | |
1047 | m_freem(control); | |
d9a64523 A |
1048 | #if CONTENT_FILTER |
1049 | if (cfil_tag) | |
1050 | m_tag_free(cfil_tag); | |
1051 | #endif | |
39236c6e | 1052 | return (error); |
1c79356b A |
1053 | } |
1054 | ||
39236c6e A |
1055 | /* |
1056 | * Checksum extended UDP header and data. | |
1057 | */ | |
1058 | static int | |
1059 | udp6_input_checksum(struct mbuf *m, struct udphdr *uh, int off, int ulen) | |
1060 | { | |
1061 | struct ifnet *ifp = m->m_pkthdr.rcvif; | |
1062 | struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *); | |
1063 | ||
490019cf A |
1064 | if (!(m->m_pkthdr.csum_flags & CSUM_DATA_VALID) && |
1065 | uh->uh_sum == 0) { | |
39236c6e | 1066 | /* UDP/IPv6 checksum is mandatory (RFC2460) */ |
490019cf | 1067 | |
39037602 | 1068 | /* |
490019cf | 1069 | * If checksum was already validated, ignore this check. |
39037602 | 1070 | * This is necessary for transport-mode ESP, which may be |
490019cf A |
1071 | * getting UDP payloads without checksums when the network |
1072 | * has a NAT64. | |
1073 | */ | |
39236c6e A |
1074 | udpstat.udps_nosum++; |
1075 | goto badsum; | |
1076 | } | |
1077 | ||
1078 | if ((hwcksum_rx || (ifp->if_flags & IFF_LOOPBACK) || | |
1079 | (m->m_pkthdr.pkt_flags & PKTF_LOOP)) && | |
1080 | (m->m_pkthdr.csum_flags & CSUM_DATA_VALID)) { | |
1081 | if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) { | |
1082 | uh->uh_sum = m->m_pkthdr.csum_rx_val; | |
1083 | } else { | |
5ba3f43e A |
1084 | uint32_t sum = m->m_pkthdr.csum_rx_val; |
1085 | uint32_t start = m->m_pkthdr.csum_rx_start; | |
1086 | int32_t trailer = (m_pktlen(m) - (off + ulen)); | |
39236c6e A |
1087 | |
1088 | /* | |
1089 | * Perform 1's complement adjustment of octets | |
1090 | * that got included/excluded in the hardware- | |
5ba3f43e A |
1091 | * calculated checksum value. Also take care |
1092 | * of any trailing bytes and subtract out | |
1093 | * their partial sum. | |
39236c6e | 1094 | */ |
5ba3f43e | 1095 | ASSERT(trailer >= 0); |
39236c6e | 1096 | if ((m->m_pkthdr.csum_flags & CSUM_PARTIAL) && |
5ba3f43e A |
1097 | (start != off || trailer != 0)) { |
1098 | uint32_t swbytes = (uint32_t)trailer; | |
1099 | uint16_t s = 0, d = 0; | |
39236c6e A |
1100 | |
1101 | if (IN6_IS_SCOPE_EMBED(&ip6->ip6_src)) { | |
1102 | s = ip6->ip6_src.s6_addr16[1]; | |
1103 | ip6->ip6_src.s6_addr16[1] = 0 ; | |
1104 | } | |
1105 | if (IN6_IS_SCOPE_EMBED(&ip6->ip6_dst)) { | |
1106 | d = ip6->ip6_dst.s6_addr16[1]; | |
1107 | ip6->ip6_dst.s6_addr16[1] = 0; | |
1108 | } | |
1109 | ||
1110 | /* callee folds in sum */ | |
5ba3f43e A |
1111 | sum = m_adj_sum16(m, start, off, ulen, sum); |
1112 | if (off > start) | |
1113 | swbytes += (off - start); | |
1114 | else | |
1115 | swbytes += (start - off); | |
39236c6e A |
1116 | |
1117 | if (IN6_IS_SCOPE_EMBED(&ip6->ip6_src)) | |
1118 | ip6->ip6_src.s6_addr16[1] = s; | |
1119 | if (IN6_IS_SCOPE_EMBED(&ip6->ip6_dst)) | |
1120 | ip6->ip6_dst.s6_addr16[1] = d; | |
5ba3f43e A |
1121 | |
1122 | if (swbytes != 0) | |
1123 | udp_in_cksum_stats(swbytes); | |
1124 | if (trailer != 0) | |
1125 | m_adj(m, -trailer); | |
39236c6e A |
1126 | } |
1127 | ||
1128 | uh->uh_sum = in6_pseudo(&ip6->ip6_src, &ip6->ip6_dst, | |
1129 | sum + htonl(ulen + IPPROTO_UDP)); | |
1130 | } | |
1131 | uh->uh_sum ^= 0xffff; | |
1132 | } else { | |
1133 | udp_in6_cksum_stats(ulen); | |
1134 | uh->uh_sum = in6_cksum(m, IPPROTO_UDP, off, ulen); | |
1135 | } | |
1136 | ||
1137 | if (uh->uh_sum != 0) { | |
1138 | badsum: | |
1139 | udpstat.udps_badsum++; | |
1140 | IF_UDP_STATINC(ifp, badchksum); | |
1141 | return (-1); | |
1142 | } | |
1143 | ||
1144 | return (0); | |
1145 | } |