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