]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
a39ff7e2 | 2 | * Copyright (c) 2000-2018 Apple Inc. All rights reserved. |
5d5c5d0d | 3 | * |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
39037602 | 5 | * |
2d21ac55 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 | * |
2d21ac55 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 | * |
2d21ac55 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 | |
8f6c56a5 A |
20 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
21 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
2d21ac55 A |
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 | * |
2d21ac55 | 26 | * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ |
1c79356b A |
27 | */ |
28 | /* | |
29 | * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995 | |
30 | * The Regents of the University of California. All rights reserved. | |
31 | * | |
32 | * Redistribution and use in source and binary forms, with or without | |
33 | * modification, are permitted provided that the following conditions | |
34 | * are met: | |
35 | * 1. Redistributions of source code must retain the above copyright | |
36 | * notice, this list of conditions and the following disclaimer. | |
37 | * 2. Redistributions in binary form must reproduce the above copyright | |
38 | * notice, this list of conditions and the following disclaimer in the | |
39 | * documentation and/or other materials provided with the distribution. | |
40 | * 3. All advertising materials mentioning features or use of this software | |
41 | * must display the following acknowledgement: | |
42 | * This product includes software developed by the University of | |
43 | * California, Berkeley and its contributors. | |
44 | * 4. Neither the name of the University 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 REGENTS 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 REGENTS 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 | * @(#)udp_usrreq.c 8.6 (Berkeley) 5/23/95 | |
61 | */ | |
62 | ||
63 | #include <sys/param.h> | |
64 | #include <sys/systm.h> | |
65 | #include <sys/kernel.h> | |
66 | #include <sys/malloc.h> | |
67 | #include <sys/mbuf.h> | |
1c79356b | 68 | #include <sys/domain.h> |
1c79356b A |
69 | #include <sys/protosw.h> |
70 | #include <sys/socket.h> | |
71 | #include <sys/socketvar.h> | |
72 | #include <sys/sysctl.h> | |
73 | #include <sys/syslog.h> | |
6d2010ae A |
74 | #include <sys/mcache.h> |
75 | #include <net/ntstat.h> | |
1c79356b | 76 | |
2d21ac55 | 77 | #include <kern/zalloc.h> |
39236c6e | 78 | #include <mach/boolean.h> |
2d21ac55 | 79 | |
1c79356b | 80 | #include <net/if.h> |
91447636 | 81 | #include <net/if_types.h> |
1c79356b | 82 | #include <net/route.h> |
39236c6e | 83 | #include <net/dlil.h> |
5ba3f43e | 84 | #include <net/net_api_stats.h> |
1c79356b A |
85 | |
86 | #include <netinet/in.h> | |
87 | #include <netinet/in_systm.h> | |
39037602 | 88 | #include <netinet/in_tclass.h> |
1c79356b | 89 | #include <netinet/ip.h> |
9bccf70c A |
90 | #if INET6 |
91 | #include <netinet/ip6.h> | |
39236c6e | 92 | #endif /* INET6 */ |
1c79356b A |
93 | #include <netinet/in_pcb.h> |
94 | #include <netinet/in_var.h> | |
95 | #include <netinet/ip_var.h> | |
1c79356b | 96 | #if INET6 |
b0d623f7 | 97 | #include <netinet6/in6_pcb.h> |
1c79356b | 98 | #include <netinet6/ip6_var.h> |
39236c6e A |
99 | #include <netinet6/udp6_var.h> |
100 | #endif /* INET6 */ | |
9bccf70c A |
101 | #include <netinet/ip_icmp.h> |
102 | #include <netinet/icmp_var.h> | |
1c79356b A |
103 | #include <netinet/udp.h> |
104 | #include <netinet/udp_var.h> | |
105 | #include <sys/kdebug.h> | |
106 | ||
107 | #if IPSEC | |
108 | #include <netinet6/ipsec.h> | |
2d21ac55 | 109 | #include <netinet6/esp.h> |
9bccf70c | 110 | extern int ipsec_bypass; |
39236c6e A |
111 | extern int esp_udp_encap_port; |
112 | #endif /* IPSEC */ | |
1c79356b | 113 | |
fe8ab488 A |
114 | #if NECP |
115 | #include <net/necp.h> | |
116 | #endif /* NECP */ | |
117 | ||
3e170ce0 A |
118 | #if FLOW_DIVERT |
119 | #include <netinet/flow_divert.h> | |
120 | #endif /* FLOW_DIVERT */ | |
121 | ||
39236c6e A |
122 | #define DBG_LAYER_IN_BEG NETDBG_CODE(DBG_NETUDP, 0) |
123 | #define DBG_LAYER_IN_END NETDBG_CODE(DBG_NETUDP, 2) | |
124 | #define DBG_LAYER_OUT_BEG NETDBG_CODE(DBG_NETUDP, 1) | |
125 | #define DBG_LAYER_OUT_END NETDBG_CODE(DBG_NETUDP, 3) | |
126 | #define DBG_FNC_UDP_INPUT NETDBG_CODE(DBG_NETUDP, (5 << 8)) | |
127 | #define DBG_FNC_UDP_OUTPUT NETDBG_CODE(DBG_NETUDP, (6 << 8) | 1) | |
1c79356b | 128 | |
1c79356b A |
129 | /* |
130 | * UDP protocol implementation. | |
131 | * Per RFC 768, August, 1980. | |
132 | */ | |
133 | #ifndef COMPAT_42 | |
39236c6e | 134 | static int udpcksum = 1; |
1c79356b | 135 | #else |
39236c6e | 136 | static int udpcksum = 0; /* XXX */ |
1c79356b | 137 | #endif |
39236c6e A |
138 | SYSCTL_INT(_net_inet_udp, UDPCTL_CHECKSUM, checksum, |
139 | CTLFLAG_RW | CTLFLAG_LOCKED, &udpcksum, 0, ""); | |
140 | ||
141 | int udp_log_in_vain = 0; | |
6d2010ae | 142 | SYSCTL_INT(_net_inet_udp, OID_AUTO, log_in_vain, CTLFLAG_RW | CTLFLAG_LOCKED, |
39037602 | 143 | &udp_log_in_vain, 0, "Log all incoming UDP packets"); |
9bccf70c | 144 | |
39236c6e | 145 | static int blackhole = 0; |
6d2010ae | 146 | SYSCTL_INT(_net_inet_udp, OID_AUTO, blackhole, CTLFLAG_RW | CTLFLAG_LOCKED, |
39037602 | 147 | &blackhole, 0, "Do not send port unreachables for refused connects"); |
1c79356b | 148 | |
39236c6e | 149 | struct inpcbhead udb; /* from udp_var.h */ |
1c79356b | 150 | #define udb6 udb /* for KAME src sync over BSD*'s */ |
39236c6e | 151 | struct inpcbinfo udbinfo; |
1c79356b A |
152 | |
153 | #ifndef UDBHASHSIZE | |
39037602 | 154 | #define UDBHASHSIZE 16 |
1c79356b A |
155 | #endif |
156 | ||
39236c6e A |
157 | /* Garbage collection performed during most recent udp_gc() run */ |
158 | static boolean_t udp_gc_done = FALSE; | |
91447636 | 159 | |
2d21ac55 | 160 | #if IPFIREWALL |
39236c6e | 161 | extern int fw_verbose; |
39037602 | 162 | extern void ipfwsyslog(int level, const char *format, ...); |
39236c6e A |
163 | extern void ipfw_stealth_stats_incr_udp(void); |
164 | ||
165 | /* Apple logging, log to ipfw.log */ | |
39037602 | 166 | #define log_in_vain_log(a) { \ |
39236c6e A |
167 | if ((udp_log_in_vain == 3) && (fw_verbose == 2)) { \ |
168 | ipfwsyslog a; \ | |
169 | } else if ((udp_log_in_vain == 4) && (fw_verbose == 2)) { \ | |
39037602 | 170 | ipfw_stealth_stats_incr_udp(); \ |
39236c6e A |
171 | } else { \ |
172 | log a; \ | |
173 | } \ | |
91447636 | 174 | } |
39236c6e | 175 | #else /* !IPFIREWALL */ |
39037602 | 176 | #define log_in_vain_log(a) { log a; } |
39236c6e | 177 | #endif /* !IPFIREWALL */ |
91447636 | 178 | |
39236c6e | 179 | static int udp_getstat SYSCTL_HANDLER_ARGS; |
1c79356b | 180 | struct udpstat udpstat; /* from udp_var.h */ |
fe8ab488 | 181 | SYSCTL_PROC(_net_inet_udp, UDPCTL_STATS, stats, |
39037602 A |
182 | CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_LOCKED, |
183 | 0, 0, udp_getstat, "S,udpstat", | |
184 | "UDP statistics (struct udpstat, netinet/udp_var.h)"); | |
39236c6e A |
185 | |
186 | SYSCTL_INT(_net_inet_udp, OID_AUTO, pcbcount, | |
39037602 A |
187 | CTLFLAG_RD | CTLFLAG_LOCKED, &udbinfo.ipi_count, 0, |
188 | "Number of active PCBs"); | |
1c79356b | 189 | |
b0d623f7 | 190 | __private_extern__ int udp_use_randomport = 1; |
39236c6e | 191 | SYSCTL_INT(_net_inet_udp, OID_AUTO, randomize_ports, |
39037602 A |
192 | CTLFLAG_RW | CTLFLAG_LOCKED, &udp_use_randomport, 0, |
193 | "Randomize UDP port numbers"); | |
b0d623f7 | 194 | |
1c79356b A |
195 | #if INET6 |
196 | struct udp_in6 { | |
197 | struct sockaddr_in6 uin6_sin; | |
198 | u_char uin6_init_done : 1; | |
1c79356b A |
199 | }; |
200 | struct udp_ip6 { | |
201 | struct ip6_hdr uip6_ip6; | |
202 | u_char uip6_init_done : 1; | |
2d21ac55 | 203 | }; |
39236c6e | 204 | |
5ba3f43e A |
205 | int udp_abort(struct socket *); |
206 | int udp_attach(struct socket *, int, struct proc *); | |
207 | int udp_bind(struct socket *, struct sockaddr *, struct proc *); | |
208 | int udp_connect(struct socket *, struct sockaddr *, struct proc *); | |
209 | int udp_connectx(struct socket *, struct sockaddr *, | |
813fb2f6 | 210 | struct sockaddr *, struct proc *, uint32_t, sae_associd_t, |
3e170ce0 | 211 | sae_connid_t *, uint32_t, void *, uint32_t, struct uio *, user_ssize_t *); |
5ba3f43e A |
212 | int udp_detach(struct socket *); |
213 | int udp_disconnect(struct socket *); | |
214 | int udp_disconnectx(struct socket *, sae_associd_t, sae_connid_t); | |
215 | int udp_send(struct socket *, int, struct mbuf *, struct sockaddr *, | |
39236c6e A |
216 | struct mbuf *, struct proc *); |
217 | static void udp_append(struct inpcb *, struct ip *, struct mbuf *, int, | |
218 | struct sockaddr_in *, struct udp_in6 *, struct udp_ip6 *, struct ifnet *); | |
219 | #else /* !INET6 */ | |
220 | static void udp_append(struct inpcb *, struct ip *, struct mbuf *, int, | |
221 | struct sockaddr_in *, struct ifnet *); | |
222 | #endif /* !INET6 */ | |
223 | static int udp_input_checksum(struct mbuf *, struct udphdr *, int, int); | |
5ba3f43e | 224 | int udp_output(struct inpcb *, struct mbuf *, struct sockaddr *, |
39236c6e | 225 | struct mbuf *, struct proc *); |
91447636 | 226 | static void ip_2_ip6_hdr(struct ip6_hdr *ip6, struct ip *ip); |
39236c6e | 227 | static void udp_gc(struct inpcbinfo *); |
1c79356b | 228 | |
39236c6e A |
229 | struct pr_usrreqs udp_usrreqs = { |
230 | .pru_abort = udp_abort, | |
231 | .pru_attach = udp_attach, | |
232 | .pru_bind = udp_bind, | |
233 | .pru_connect = udp_connect, | |
234 | .pru_connectx = udp_connectx, | |
235 | .pru_control = in_control, | |
236 | .pru_detach = udp_detach, | |
237 | .pru_disconnect = udp_disconnect, | |
238 | .pru_disconnectx = udp_disconnectx, | |
239 | .pru_peeraddr = in_getpeeraddr, | |
240 | .pru_send = udp_send, | |
241 | .pru_shutdown = udp_shutdown, | |
242 | .pru_sockaddr = in_getsockaddr, | |
243 | .pru_sosend = sosend, | |
244 | .pru_soreceive = soreceive, | |
3e170ce0 | 245 | .pru_soreceive_list = soreceive_list, |
39236c6e | 246 | }; |
1c79356b A |
247 | |
248 | void | |
39236c6e | 249 | udp_init(struct protosw *pp, struct domain *dp) |
1c79356b | 250 | { |
39236c6e A |
251 | #pragma unused(dp) |
252 | static int udp_initialized = 0; | |
253 | vm_size_t str_size; | |
254 | struct inpcbinfo *pcbinfo; | |
255 | ||
256 | VERIFY((pp->pr_flags & (PR_INITIALIZED|PR_ATTACHED)) == PR_ATTACHED); | |
257 | ||
258 | if (udp_initialized) | |
259 | return; | |
260 | udp_initialized = 1; | |
1c79356b A |
261 | |
262 | LIST_INIT(&udb); | |
39236c6e A |
263 | udbinfo.ipi_listhead = &udb; |
264 | udbinfo.ipi_hashbase = hashinit(UDBHASHSIZE, M_PCB, | |
265 | &udbinfo.ipi_hashmask); | |
266 | udbinfo.ipi_porthashbase = hashinit(UDBHASHSIZE, M_PCB, | |
267 | &udbinfo.ipi_porthashmask); | |
268 | str_size = (vm_size_t) sizeof (struct inpcb); | |
269 | udbinfo.ipi_zone = zinit(str_size, 80000*str_size, 8192, "udpcb"); | |
270 | ||
271 | pcbinfo = &udbinfo; | |
91447636 A |
272 | /* |
273 | * allocate lock group attribute and group for udp pcb mutexes | |
274 | */ | |
39236c6e A |
275 | pcbinfo->ipi_lock_grp_attr = lck_grp_attr_alloc_init(); |
276 | pcbinfo->ipi_lock_grp = lck_grp_alloc_init("udppcb", | |
277 | pcbinfo->ipi_lock_grp_attr); | |
278 | pcbinfo->ipi_lock_attr = lck_attr_alloc_init(); | |
279 | if ((pcbinfo->ipi_lock = lck_rw_alloc_init(pcbinfo->ipi_lock_grp, | |
280 | pcbinfo->ipi_lock_attr)) == NULL) { | |
281 | panic("%s: unable to allocate PCB lock\n", __func__); | |
282 | /* NOTREACHED */ | |
283 | } | |
91447636 | 284 | |
39236c6e A |
285 | udbinfo.ipi_gc = udp_gc; |
286 | in_pcbinfo_attach(&udbinfo); | |
1c79356b A |
287 | } |
288 | ||
289 | void | |
39236c6e | 290 | udp_input(struct mbuf *m, int iphlen) |
1c79356b | 291 | { |
39236c6e A |
292 | struct ip *ip; |
293 | struct udphdr *uh; | |
294 | struct inpcb *inp; | |
295 | struct mbuf *opts = NULL; | |
6d2010ae | 296 | int len, isbroadcast; |
1c79356b A |
297 | struct ip save_ip; |
298 | struct sockaddr *append_sa; | |
91447636 | 299 | struct inpcbinfo *pcbinfo = &udbinfo; |
39236c6e | 300 | struct sockaddr_in udp_in; |
6d2010ae A |
301 | struct ip_moptions *imo = NULL; |
302 | int foundmembership = 0, ret = 0; | |
2d21ac55 | 303 | #if INET6 |
39236c6e | 304 | struct udp_in6 udp_in6; |
2d21ac55 A |
305 | struct udp_ip6 udp_ip6; |
306 | #endif /* INET6 */ | |
39236c6e A |
307 | struct ifnet *ifp = m->m_pkthdr.rcvif; |
308 | boolean_t cell = IFNET_IS_CELLULAR(ifp); | |
309 | boolean_t wifi = (!cell && IFNET_IS_WIFI(ifp)); | |
fe8ab488 | 310 | boolean_t wired = (!wifi && IFNET_IS_WIRED(ifp)); |
39236c6e A |
311 | |
312 | bzero(&udp_in, sizeof (udp_in)); | |
313 | udp_in.sin_len = sizeof (struct sockaddr_in); | |
314 | udp_in.sin_family = AF_INET; | |
315 | #if INET6 | |
316 | bzero(&udp_in6, sizeof (udp_in6)); | |
317 | udp_in6.uin6_sin.sin6_len = sizeof (struct sockaddr_in6); | |
318 | udp_in6.uin6_sin.sin6_family = AF_INET6; | |
319 | #endif /* INET6 */ | |
1c79356b A |
320 | |
321 | udpstat.udps_ipackets++; | |
1c79356b | 322 | |
39037602 | 323 | KERNEL_DEBUG(DBG_FNC_UDP_INPUT | DBG_FUNC_START, 0, 0, 0, 0, 0); |
1c79356b | 324 | |
316670eb A |
325 | /* Expect 32-bit aligned data pointer on strict-align platforms */ |
326 | MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m); | |
327 | ||
1c79356b A |
328 | /* |
329 | * Strip IP options, if any; should skip this, | |
330 | * make available to user, and use on returned packets, | |
331 | * but we don't yet have a way to check the checksum | |
332 | * with options still present. | |
333 | */ | |
334 | if (iphlen > sizeof (struct ip)) { | |
5ba3f43e | 335 | ip_stripoptions(m); |
39236c6e | 336 | iphlen = sizeof (struct ip); |
1c79356b A |
337 | } |
338 | ||
339 | /* | |
340 | * Get IP and UDP header together in first mbuf. | |
341 | */ | |
342 | ip = mtod(m, struct ip *); | |
39236c6e A |
343 | if (m->m_len < iphlen + sizeof (struct udphdr)) { |
344 | m = m_pullup(m, iphlen + sizeof (struct udphdr)); | |
345 | if (m == NULL) { | |
1c79356b | 346 | udpstat.udps_hdrops++; |
39236c6e | 347 | KERNEL_DEBUG(DBG_FNC_UDP_INPUT | DBG_FUNC_END, |
39037602 | 348 | 0, 0, 0, 0, 0); |
1c79356b A |
349 | return; |
350 | } | |
351 | ip = mtod(m, struct ip *); | |
352 | } | |
316670eb | 353 | uh = (struct udphdr *)(void *)((caddr_t)ip + iphlen); |
1c79356b | 354 | |
9bccf70c | 355 | /* destination port of 0 is illegal, based on RFC768. */ |
316670eb | 356 | if (uh->uh_dport == 0) { |
39236c6e | 357 | IF_UDP_STATINC(ifp, port0); |
9bccf70c | 358 | goto bad; |
316670eb | 359 | } |
9bccf70c | 360 | |
1c79356b | 361 | KERNEL_DEBUG(DBG_LAYER_IN_BEG, uh->uh_dport, uh->uh_sport, |
39236c6e | 362 | ip->ip_src.s_addr, ip->ip_dst.s_addr, uh->uh_ulen); |
1c79356b A |
363 | |
364 | /* | |
365 | * Make mbuf data length reflect UDP length. | |
366 | * If not enough data to reflect UDP length, drop. | |
367 | */ | |
368 | len = ntohs((u_short)uh->uh_ulen); | |
369 | if (ip->ip_len != len) { | |
39236c6e | 370 | if (len > ip->ip_len || len < sizeof (struct udphdr)) { |
1c79356b | 371 | udpstat.udps_badlen++; |
39236c6e | 372 | IF_UDP_STATINC(ifp, badlength); |
1c79356b A |
373 | goto bad; |
374 | } | |
375 | m_adj(m, len - ip->ip_len); | |
376 | /* ip->ip_len = len; */ | |
377 | } | |
378 | /* | |
379 | * Save a copy of the IP header in case we want restore it | |
380 | * for sending an ICMP error message in response. | |
381 | */ | |
382 | save_ip = *ip; | |
383 | ||
384 | /* | |
385 | * Checksum extended UDP header and data. | |
386 | */ | |
39236c6e A |
387 | if (udp_input_checksum(m, uh, iphlen, len)) |
388 | goto bad; | |
1c79356b | 389 | |
316670eb | 390 | isbroadcast = in_broadcast(ip->ip_dst, ifp); |
6d2010ae A |
391 | |
392 | if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || isbroadcast) { | |
2d21ac55 | 393 | int reuse_sock = 0, mcast_delivered = 0; |
2d21ac55 | 394 | |
39236c6e | 395 | lck_rw_lock_shared(pcbinfo->ipi_lock); |
1c79356b A |
396 | /* |
397 | * Deliver a multicast or broadcast datagram to *all* sockets | |
398 | * for which the local and remote addresses and ports match | |
399 | * those of the incoming datagram. This allows more than | |
400 | * one process to receive multi/broadcasts on the same port. | |
401 | * (This really ought to be done for unicast datagrams as | |
402 | * well, but that would cause problems with existing | |
403 | * applications that open both address-specific sockets and | |
404 | * a wildcard socket listening to the same port -- they would | |
405 | * end up receiving duplicates of every unicast datagram. | |
406 | * Those applications open the multiple sockets to overcome an | |
407 | * inadequacy of the UDP socket interface, but for backwards | |
408 | * compatibility we avoid the problem here rather than | |
409 | * fixing the interface. Maybe 4.5BSD will remedy this?) | |
410 | */ | |
411 | ||
412 | /* | |
413 | * Construct sockaddr format source address. | |
414 | */ | |
415 | udp_in.sin_port = uh->uh_sport; | |
416 | udp_in.sin_addr = ip->ip_src; | |
417 | /* | |
418 | * Locate pcb(s) for datagram. | |
419 | * (Algorithm copied from raw_intr().) | |
420 | */ | |
1c79356b A |
421 | #if INET6 |
422 | udp_in6.uin6_init_done = udp_ip6.uip6_init_done = 0; | |
39236c6e | 423 | #endif /* INET6 */ |
1c79356b | 424 | LIST_FOREACH(inp, &udb, inp_list) { |
39236c6e A |
425 | #if IPSEC |
426 | int skipit; | |
427 | #endif /* IPSEC */ | |
428 | ||
429 | if (inp->inp_socket == NULL) | |
1c79356b | 430 | continue; |
39236c6e A |
431 | if (inp != sotoinpcb(inp->inp_socket)) { |
432 | panic("%s: bad so back ptr inp=%p\n", | |
433 | __func__, inp); | |
434 | /* NOTREACHED */ | |
435 | } | |
91447636 | 436 | #if INET6 |
39037602 A |
437 | if ((inp->inp_vflag & INP_IPV4) == 0) |
438 | continue; | |
39236c6e | 439 | #endif /* INET6 */ |
fe8ab488 | 440 | if (inp_restricted_recv(inp, ifp)) |
6d2010ae A |
441 | continue; |
442 | ||
39236c6e A |
443 | if ((inp->inp_moptions == NULL) && |
444 | (ntohl(ip->ip_dst.s_addr) != | |
445 | INADDR_ALLHOSTS_GROUP) && (isbroadcast == 0)) | |
446 | continue; | |
2d21ac55 | 447 | |
39236c6e A |
448 | if (in_pcb_checkstate(inp, WNT_ACQUIRE, 0) == |
449 | WNT_STOPUSING) | |
91447636 | 450 | continue; |
91447636 | 451 | |
39236c6e A |
452 | udp_lock(inp->inp_socket, 1, 0); |
453 | ||
454 | if (in_pcb_checkstate(inp, WNT_RELEASE, 1) == | |
455 | WNT_STOPUSING) { | |
91447636 | 456 | udp_unlock(inp->inp_socket, 1, 0); |
1c79356b | 457 | continue; |
91447636 A |
458 | } |
459 | ||
460 | if (inp->inp_lport != uh->uh_dport) { | |
461 | udp_unlock(inp->inp_socket, 1, 0); | |
462 | continue; | |
463 | } | |
1c79356b A |
464 | if (inp->inp_laddr.s_addr != INADDR_ANY) { |
465 | if (inp->inp_laddr.s_addr != | |
91447636 A |
466 | ip->ip_dst.s_addr) { |
467 | udp_unlock(inp->inp_socket, 1, 0); | |
1c79356b | 468 | continue; |
91447636 | 469 | } |
1c79356b A |
470 | } |
471 | if (inp->inp_faddr.s_addr != INADDR_ANY) { | |
472 | if (inp->inp_faddr.s_addr != | |
473 | ip->ip_src.s_addr || | |
91447636 A |
474 | inp->inp_fport != uh->uh_sport) { |
475 | udp_unlock(inp->inp_socket, 1, 0); | |
1c79356b | 476 | continue; |
91447636 | 477 | } |
1c79356b A |
478 | } |
479 | ||
39236c6e A |
480 | if (isbroadcast == 0 && (ntohl(ip->ip_dst.s_addr) != |
481 | INADDR_ALLHOSTS_GROUP)) { | |
482 | struct sockaddr_in group; | |
483 | int blocked; | |
484 | ||
485 | if ((imo = inp->inp_moptions) == NULL) { | |
6d2010ae A |
486 | udp_unlock(inp->inp_socket, 1, 0); |
487 | continue; | |
6d2010ae | 488 | } |
39236c6e A |
489 | IMO_LOCK(imo); |
490 | ||
491 | bzero(&group, sizeof (struct sockaddr_in)); | |
492 | group.sin_len = sizeof (struct sockaddr_in); | |
493 | group.sin_family = AF_INET; | |
494 | group.sin_addr = ip->ip_dst; | |
495 | ||
496 | blocked = imo_multi_filter(imo, ifp, | |
5ba3f43e | 497 | &group, &udp_in); |
39236c6e A |
498 | if (blocked == MCAST_PASS) |
499 | foundmembership = 1; | |
500 | ||
501 | IMO_UNLOCK(imo); | |
502 | if (!foundmembership) { | |
503 | udp_unlock(inp->inp_socket, 1, 0); | |
504 | if (blocked == MCAST_NOTSMEMBER || | |
505 | blocked == MCAST_MUTED) | |
506 | udpstat.udps_filtermcast++; | |
507 | continue; | |
508 | } | |
509 | foundmembership = 0; | |
6d2010ae | 510 | } |
39236c6e A |
511 | |
512 | reuse_sock = (inp->inp_socket->so_options & | |
513 | (SO_REUSEPORT|SO_REUSEADDR)); | |
514 | ||
fe8ab488 | 515 | #if NECP |
39236c6e | 516 | skipit = 0; |
39037602 | 517 | if (!necp_socket_is_allowed_to_send_recv_v4(inp, |
3e170ce0 A |
518 | uh->uh_dport, uh->uh_sport, &ip->ip_dst, |
519 | &ip->ip_src, ifp, NULL, NULL)) { | |
39236c6e A |
520 | /* do not inject data to pcb */ |
521 | skipit = 1; | |
522 | } | |
523 | if (skipit == 0) | |
fe8ab488 | 524 | #endif /* NECP */ |
39236c6e A |
525 | { |
526 | struct mbuf *n = NULL; | |
b0d623f7 | 527 | |
39236c6e A |
528 | if (reuse_sock) |
529 | n = m_copy(m, 0, M_COPYALL); | |
530 | #if INET6 | |
531 | udp_append(inp, ip, m, | |
532 | iphlen + sizeof (struct udphdr), | |
533 | &udp_in, &udp_in6, &udp_ip6, ifp); | |
534 | #else /* !INET6 */ | |
535 | udp_append(inp, ip, m, | |
536 | iphlen + sizeof (struct udphdr), | |
537 | &udp_in, ifp); | |
538 | #endif /* !INET6 */ | |
539 | mcast_delivered++; | |
540 | ||
541 | m = n; | |
1c79356b | 542 | } |
39236c6e A |
543 | udp_unlock(inp->inp_socket, 1, 0); |
544 | ||
1c79356b A |
545 | /* |
546 | * Don't look for additional matches if this one does | |
547 | * not have either the SO_REUSEPORT or SO_REUSEADDR | |
548 | * socket options set. This heuristic avoids searching | |
549 | * through all pcbs in the common case of a non-shared | |
2d21ac55 | 550 | * port. It assumes that an application will never |
1c79356b A |
551 | * clear these options after setting them. |
552 | */ | |
b0d623f7 | 553 | if (reuse_sock == 0 || m == NULL) |
1c79356b | 554 | break; |
316670eb A |
555 | |
556 | /* | |
557 | * Expect 32-bit aligned data pointer on strict-align | |
558 | * platforms. | |
559 | */ | |
560 | MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m); | |
b0d623f7 A |
561 | /* |
562 | * Recompute IP and UDP header pointers for new mbuf | |
563 | */ | |
564 | ip = mtod(m, struct ip *); | |
316670eb | 565 | uh = (struct udphdr *)(void *)((caddr_t)ip + iphlen); |
1c79356b | 566 | } |
39236c6e | 567 | lck_rw_done(pcbinfo->ipi_lock); |
1c79356b | 568 | |
2d21ac55 | 569 | if (mcast_delivered == 0) { |
1c79356b A |
570 | /* |
571 | * No matching pcb found; discard datagram. | |
572 | * (No need to send an ICMP Port Unreachable | |
573 | * for a broadcast or multicast datgram.) | |
574 | */ | |
575 | udpstat.udps_noportbcast++; | |
39236c6e | 576 | IF_UDP_STATINC(ifp, port_unreach); |
1c79356b A |
577 | goto bad; |
578 | } | |
2d21ac55 | 579 | |
39236c6e A |
580 | /* free the extra copy of mbuf or skipped by IPSec */ |
581 | if (m != NULL) | |
2d21ac55 | 582 | m_freem(m); |
39037602 | 583 | KERNEL_DEBUG(DBG_FNC_UDP_INPUT | DBG_FUNC_END, 0, 0, 0, 0, 0); |
1c79356b A |
584 | return; |
585 | } | |
55e303ae | 586 | |
91447636 | 587 | #if IPSEC |
55e303ae A |
588 | /* |
589 | * UDP to port 4500 with a payload where the first four bytes are | |
590 | * not zero is a UDP encapsulated IPSec packet. Packets where | |
591 | * the payload is one byte and that byte is 0xFF are NAT keepalive | |
592 | * packets. Decapsulate the ESP packet and carry on with IPSec input | |
593 | * or discard the NAT keep-alive. | |
594 | */ | |
595 | if (ipsec_bypass == 0 && (esp_udp_encap_port & 0xFFFF) != 0 && | |
39236c6e A |
596 | uh->uh_dport == ntohs((u_short)esp_udp_encap_port)) { |
597 | int payload_len = len - sizeof (struct udphdr) > 4 ? 4 : | |
598 | len - sizeof (struct udphdr); | |
599 | ||
600 | if (m->m_len < iphlen + sizeof (struct udphdr) + payload_len) { | |
601 | if ((m = m_pullup(m, iphlen + sizeof (struct udphdr) + | |
602 | payload_len)) == NULL) { | |
55e303ae | 603 | udpstat.udps_hdrops++; |
39236c6e | 604 | KERNEL_DEBUG(DBG_FNC_UDP_INPUT | DBG_FUNC_END, |
39037602 | 605 | 0, 0, 0, 0, 0); |
55e303ae A |
606 | return; |
607 | } | |
316670eb A |
608 | /* |
609 | * Expect 32-bit aligned data pointer on strict-align | |
610 | * platforms. | |
611 | */ | |
612 | MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m); | |
613 | ||
55e303ae | 614 | ip = mtod(m, struct ip *); |
316670eb | 615 | uh = (struct udphdr *)(void *)((caddr_t)ip + iphlen); |
55e303ae A |
616 | } |
617 | /* Check for NAT keepalive packet */ | |
39037602 | 618 | if (payload_len == 1 && *(u_int8_t *) |
39236c6e | 619 | ((caddr_t)uh + sizeof (struct udphdr)) == 0xFF) { |
55e303ae | 620 | m_freem(m); |
39236c6e | 621 | KERNEL_DEBUG(DBG_FNC_UDP_INPUT | DBG_FUNC_END, |
39037602 | 622 | 0, 0, 0, 0, 0); |
55e303ae | 623 | return; |
39037602 | 624 | } else if (payload_len == 4 && *(u_int32_t *)(void *) |
39236c6e | 625 | ((caddr_t)uh + sizeof (struct udphdr)) != 0) { |
55e303ae | 626 | /* UDP encapsulated IPSec packet to pass through NAT */ |
39236c6e | 627 | KERNEL_DEBUG(DBG_FNC_UDP_INPUT | DBG_FUNC_END, |
39037602 | 628 | 0, 0, 0, 0, 0); |
6d2010ae | 629 | /* preserve the udp header */ |
39236c6e | 630 | esp4_input(m, iphlen + sizeof (struct udphdr)); |
55e303ae A |
631 | return; |
632 | } | |
633 | } | |
39236c6e | 634 | #endif /* IPSEC */ |
55e303ae | 635 | |
1c79356b A |
636 | /* |
637 | * Locate pcb for datagram. | |
638 | */ | |
639 | inp = in_pcblookup_hash(&udbinfo, ip->ip_src, uh->uh_sport, | |
316670eb | 640 | ip->ip_dst, uh->uh_dport, 1, ifp); |
1c79356b | 641 | if (inp == NULL) { |
39236c6e A |
642 | IF_UDP_STATINC(ifp, port_unreach); |
643 | ||
644 | if (udp_log_in_vain) { | |
91447636 A |
645 | char buf[MAX_IPv4_STR_LEN]; |
646 | char buf2[MAX_IPv4_STR_LEN]; | |
647 | ||
648 | /* check src and dst address */ | |
39236c6e A |
649 | if (udp_log_in_vain < 3) { |
650 | log(LOG_INFO, "Connection attempt to " | |
651 | "UDP %s:%d from %s:%d\n", inet_ntop(AF_INET, | |
39037602 | 652 | &ip->ip_dst, buf, sizeof (buf)), |
39236c6e A |
653 | ntohs(uh->uh_dport), inet_ntop(AF_INET, |
654 | &ip->ip_src, buf2, sizeof (buf2)), | |
91447636 | 655 | ntohs(uh->uh_sport)); |
39236c6e A |
656 | } else if (!(m->m_flags & (M_BCAST | M_MCAST)) && |
657 | ip->ip_dst.s_addr != ip->ip_src.s_addr) { | |
91447636 | 658 | log_in_vain_log((LOG_INFO, |
39236c6e A |
659 | "Stealth Mode connection attempt to " |
660 | "UDP %s:%d from %s:%d\n", inet_ntop(AF_INET, | |
661 | &ip->ip_dst, buf, sizeof (buf)), | |
662 | ntohs(uh->uh_dport), inet_ntop(AF_INET, | |
663 | &ip->ip_src, buf2, sizeof (buf2)), | |
664 | ntohs(uh->uh_sport))) | |
665 | } | |
1c79356b A |
666 | } |
667 | udpstat.udps_noport++; | |
668 | if (m->m_flags & (M_BCAST | M_MCAST)) { | |
669 | udpstat.udps_noportbcast++; | |
670 | goto bad; | |
671 | } | |
1c79356b | 672 | #if ICMP_BANDLIM |
9bccf70c | 673 | if (badport_bandlim(BANDLIM_ICMP_UNREACH) < 0) |
1c79356b | 674 | goto bad; |
39236c6e | 675 | #endif /* ICMP_BANDLIM */ |
9bccf70c | 676 | if (blackhole) |
316670eb | 677 | if (ifp && ifp->if_type != IFT_LOOP) |
91447636 | 678 | goto bad; |
9bccf70c A |
679 | *ip = save_ip; |
680 | ip->ip_len += iphlen; | |
1c79356b | 681 | icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0); |
39037602 | 682 | KERNEL_DEBUG(DBG_FNC_UDP_INPUT | DBG_FUNC_END, 0, 0, 0, 0, 0); |
1c79356b A |
683 | return; |
684 | } | |
91447636 A |
685 | udp_lock(inp->inp_socket, 1, 0); |
686 | ||
687 | if (in_pcb_checkstate(inp, WNT_RELEASE, 1) == WNT_STOPUSING) { | |
688 | udp_unlock(inp->inp_socket, 1, 0); | |
39236c6e | 689 | IF_UDP_STATINC(ifp, cleanup); |
1c79356b A |
690 | goto bad; |
691 | } | |
fe8ab488 | 692 | #if NECP |
3e170ce0 A |
693 | if (!necp_socket_is_allowed_to_send_recv_v4(inp, uh->uh_dport, |
694 | uh->uh_sport, &ip->ip_dst, &ip->ip_src, ifp, NULL, NULL)) { | |
fe8ab488 A |
695 | udp_unlock(inp->inp_socket, 1, 0); |
696 | IF_UDP_STATINC(ifp, badipsec); | |
697 | goto bad; | |
91447636 | 698 | } |
fe8ab488 | 699 | #endif /* NECP */ |
1c79356b A |
700 | |
701 | /* | |
702 | * Construct sockaddr format source address. | |
703 | * Stuff source address and datagram in user buffer. | |
704 | */ | |
705 | udp_in.sin_port = uh->uh_sport; | |
706 | udp_in.sin_addr = ip->ip_src; | |
39236c6e A |
707 | if ((inp->inp_flags & INP_CONTROLOPTS) != 0 || |
708 | (inp->inp_socket->so_options & SO_TIMESTAMP) != 0 || | |
709 | (inp->inp_socket->so_options & SO_TIMESTAMP_MONOTONIC) != 0) { | |
1c79356b A |
710 | #if INET6 |
711 | if (inp->inp_vflag & INP_IPV6) { | |
712 | int savedflags; | |
713 | ||
714 | ip_2_ip6_hdr(&udp_ip6.uip6_ip6, ip); | |
715 | savedflags = inp->inp_flags; | |
716 | inp->inp_flags &= ~INP_UNMAPPABLEOPTS; | |
6d2010ae | 717 | ret = ip6_savecontrol(inp, m, &opts); |
1c79356b A |
718 | inp->inp_flags = savedflags; |
719 | } else | |
39236c6e | 720 | #endif /* INET6 */ |
6d2010ae A |
721 | { |
722 | ret = ip_savecontrol(inp, &opts, ip, m); | |
723 | } | |
724 | if (ret != 0) { | |
725 | udp_unlock(inp->inp_socket, 1, 0); | |
726 | goto bad; | |
727 | } | |
1c79356b | 728 | } |
39236c6e | 729 | m_adj(m, iphlen + sizeof (struct udphdr)); |
1c79356b A |
730 | |
731 | KERNEL_DEBUG(DBG_LAYER_IN_END, uh->uh_dport, uh->uh_sport, | |
39236c6e | 732 | save_ip.ip_src.s_addr, save_ip.ip_dst.s_addr, uh->uh_ulen); |
1c79356b A |
733 | |
734 | #if INET6 | |
735 | if (inp->inp_vflag & INP_IPV6) { | |
736 | in6_sin_2_v4mapsin6(&udp_in, &udp_in6.uin6_sin); | |
2d21ac55 | 737 | append_sa = (struct sockaddr *)&udp_in6.uin6_sin; |
1c79356b | 738 | } else |
39236c6e A |
739 | #endif /* INET6 */ |
740 | { | |
741 | append_sa = (struct sockaddr *)&udp_in; | |
742 | } | |
6d2010ae | 743 | if (nstat_collect) { |
fe8ab488 A |
744 | INP_ADD_STAT(inp, cell, wifi, wired, rxpackets, 1); |
745 | INP_ADD_STAT(inp, cell, wifi, wired, rxbytes, m->m_pkthdr.len); | |
5ba3f43e | 746 | inp_set_activity_bitmap(inp); |
6d2010ae A |
747 | } |
748 | so_recv_data_stat(inp->inp_socket, m, 0); | |
39236c6e A |
749 | if (sbappendaddr(&inp->inp_socket->so_rcv, append_sa, |
750 | m, opts, NULL) == 0) { | |
1c79356b | 751 | udpstat.udps_fullsock++; |
6d2010ae | 752 | } else { |
91447636 A |
753 | sorwakeup(inp->inp_socket); |
754 | } | |
755 | udp_unlock(inp->inp_socket, 1, 0); | |
39037602 | 756 | KERNEL_DEBUG(DBG_FNC_UDP_INPUT | DBG_FUNC_END, 0, 0, 0, 0, 0); |
1c79356b A |
757 | return; |
758 | bad: | |
759 | m_freem(m); | |
760 | if (opts) | |
761 | m_freem(opts); | |
39037602 | 762 | KERNEL_DEBUG(DBG_FNC_UDP_INPUT | DBG_FUNC_END, 0, 0, 0, 0, 0); |
1c79356b A |
763 | } |
764 | ||
765 | #if INET6 | |
766 | static void | |
39236c6e | 767 | ip_2_ip6_hdr(struct ip6_hdr *ip6, struct ip *ip) |
1c79356b | 768 | { |
39236c6e | 769 | bzero(ip6, sizeof (*ip6)); |
1c79356b A |
770 | |
771 | ip6->ip6_vfc = IPV6_VERSION; | |
772 | ip6->ip6_plen = ip->ip_len; | |
773 | ip6->ip6_nxt = ip->ip_p; | |
774 | ip6->ip6_hlim = ip->ip_ttl; | |
316670eb A |
775 | if (ip->ip_src.s_addr) { |
776 | ip6->ip6_src.s6_addr32[2] = IPV6_ADDR_INT32_SMP; | |
777 | ip6->ip6_src.s6_addr32[3] = ip->ip_src.s_addr; | |
778 | } | |
779 | if (ip->ip_dst.s_addr) { | |
780 | ip6->ip6_dst.s6_addr32[2] = IPV6_ADDR_INT32_SMP; | |
781 | ip6->ip6_dst.s6_addr32[3] = ip->ip_dst.s_addr; | |
782 | } | |
1c79356b | 783 | } |
39236c6e | 784 | #endif /* INET6 */ |
1c79356b A |
785 | |
786 | /* | |
787 | * subroutine of udp_input(), mainly for source code readability. | |
1c79356b A |
788 | */ |
789 | static void | |
2d21ac55 A |
790 | #if INET6 |
791 | udp_append(struct inpcb *last, struct ip *ip, struct mbuf *n, int off, | |
792 | struct sockaddr_in *pudp_in, struct udp_in6 *pudp_in6, | |
39236c6e A |
793 | struct udp_ip6 *pudp_ip6, struct ifnet *ifp) |
794 | #else /* !INET6 */ | |
2d21ac55 | 795 | udp_append(struct inpcb *last, struct ip *ip, struct mbuf *n, int off, |
39236c6e A |
796 | struct sockaddr_in *pudp_in, struct ifnet *ifp) |
797 | #endif /* !INET6 */ | |
1c79356b A |
798 | { |
799 | struct sockaddr *append_sa; | |
800 | struct mbuf *opts = 0; | |
39236c6e A |
801 | boolean_t cell = IFNET_IS_CELLULAR(ifp); |
802 | boolean_t wifi = (!cell && IFNET_IS_WIFI(ifp)); | |
fe8ab488 | 803 | boolean_t wired = (!wifi && IFNET_IS_WIRED(ifp)); |
6d2010ae | 804 | int ret = 0; |
1c79356b | 805 | |
2d21ac55 A |
806 | #if CONFIG_MACF_NET |
807 | if (mac_inpcb_check_deliver(last, n, AF_INET, SOCK_DGRAM) != 0) { | |
808 | m_freem(n); | |
809 | return; | |
810 | } | |
39236c6e | 811 | #endif /* CONFIG_MACF_NET */ |
6d2010ae A |
812 | if ((last->inp_flags & INP_CONTROLOPTS) != 0 || |
813 | (last->inp_socket->so_options & SO_TIMESTAMP) != 0 || | |
814 | (last->inp_socket->so_options & SO_TIMESTAMP_MONOTONIC) != 0) { | |
1c79356b A |
815 | #if INET6 |
816 | if (last->inp_vflag & INP_IPV6) { | |
817 | int savedflags; | |
818 | ||
2d21ac55 A |
819 | if (pudp_ip6->uip6_init_done == 0) { |
820 | ip_2_ip6_hdr(&pudp_ip6->uip6_ip6, ip); | |
821 | pudp_ip6->uip6_init_done = 1; | |
1c79356b A |
822 | } |
823 | savedflags = last->inp_flags; | |
824 | last->inp_flags &= ~INP_UNMAPPABLEOPTS; | |
6d2010ae A |
825 | ret = ip6_savecontrol(last, n, &opts); |
826 | if (ret != 0) { | |
827 | last->inp_flags = savedflags; | |
828 | goto error; | |
829 | } | |
1c79356b A |
830 | last->inp_flags = savedflags; |
831 | } else | |
39236c6e | 832 | #endif /* INET6 */ |
6d2010ae A |
833 | { |
834 | ret = ip_savecontrol(last, &opts, ip, n); | |
835 | if (ret != 0) { | |
836 | goto error; | |
837 | } | |
838 | } | |
1c79356b A |
839 | } |
840 | #if INET6 | |
841 | if (last->inp_vflag & INP_IPV6) { | |
2d21ac55 A |
842 | if (pudp_in6->uin6_init_done == 0) { |
843 | in6_sin_2_v4mapsin6(pudp_in, &pudp_in6->uin6_sin); | |
844 | pudp_in6->uin6_init_done = 1; | |
1c79356b | 845 | } |
2d21ac55 | 846 | append_sa = (struct sockaddr *)&pudp_in6->uin6_sin; |
1c79356b | 847 | } else |
39236c6e | 848 | #endif /* INET6 */ |
2d21ac55 | 849 | append_sa = (struct sockaddr *)pudp_in; |
6d2010ae | 850 | if (nstat_collect) { |
fe8ab488 | 851 | INP_ADD_STAT(last, cell, wifi, wired, rxpackets, 1); |
39037602 | 852 | INP_ADD_STAT(last, cell, wifi, wired, rxbytes, |
fe8ab488 | 853 | n->m_pkthdr.len); |
5ba3f43e | 854 | inp_set_activity_bitmap(last); |
6d2010ae A |
855 | } |
856 | so_recv_data_stat(last->inp_socket, n, 0); | |
1c79356b | 857 | m_adj(n, off); |
39236c6e A |
858 | if (sbappendaddr(&last->inp_socket->so_rcv, append_sa, |
859 | n, opts, NULL) == 0) { | |
1c79356b | 860 | udpstat.udps_fullsock++; |
6d2010ae | 861 | } else { |
1c79356b | 862 | sorwakeup(last->inp_socket); |
6d2010ae A |
863 | } |
864 | return; | |
865 | error: | |
866 | m_freem(n); | |
867 | m_freem(opts); | |
1c79356b A |
868 | } |
869 | ||
1c79356b A |
870 | /* |
871 | * Notify a udp user of an asynchronous error; | |
872 | * just wake up so that he can collect error status. | |
873 | */ | |
874 | void | |
39236c6e | 875 | udp_notify(struct inpcb *inp, int errno) |
1c79356b A |
876 | { |
877 | inp->inp_socket->so_error = errno; | |
878 | sorwakeup(inp->inp_socket); | |
879 | sowwakeup(inp->inp_socket); | |
880 | } | |
881 | ||
882 | void | |
5ba3f43e | 883 | udp_ctlinput(int cmd, struct sockaddr *sa, void *vip, __unused struct ifnet * ifp) |
1c79356b | 884 | { |
9bccf70c | 885 | struct ip *ip = vip; |
91447636 | 886 | void (*notify)(struct inpcb *, int) = udp_notify; |
39037602 | 887 | struct in_addr faddr; |
5ba3f43e | 888 | struct inpcb *inp = NULL; |
9bccf70c | 889 | |
316670eb | 890 | faddr = ((struct sockaddr_in *)(void *)sa)->sin_addr; |
9bccf70c | 891 | if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY) |
316670eb | 892 | return; |
1c79356b | 893 | |
9bccf70c A |
894 | if (PRC_IS_REDIRECT(cmd)) { |
895 | ip = 0; | |
896 | notify = in_rtchange; | |
39236c6e | 897 | } else if (cmd == PRC_HOSTDEAD) { |
9bccf70c | 898 | ip = 0; |
39236c6e | 899 | } else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0) { |
1c79356b | 900 | return; |
39236c6e | 901 | } |
1c79356b | 902 | if (ip) { |
316670eb A |
903 | struct udphdr uh; |
904 | ||
905 | bcopy(((caddr_t)ip + (ip->ip_hl << 2)), &uh, sizeof (uh)); | |
906 | inp = in_pcblookup_hash(&udbinfo, faddr, uh.uh_dport, | |
39037602 | 907 | ip->ip_src, uh.uh_sport, 0, NULL); |
91447636 A |
908 | if (inp != NULL && inp->inp_socket != NULL) { |
909 | udp_lock(inp->inp_socket, 1, 0); | |
316670eb A |
910 | if (in_pcb_checkstate(inp, WNT_RELEASE, 1) == |
911 | WNT_STOPUSING) { | |
91447636 A |
912 | udp_unlock(inp->inp_socket, 1, 0); |
913 | return; | |
914 | } | |
9bccf70c | 915 | (*notify)(inp, inetctlerrmap[cmd]); |
91447636 A |
916 | udp_unlock(inp->inp_socket, 1, 0); |
917 | } | |
39236c6e | 918 | } else { |
91447636 | 919 | in_pcbnotifyall(&udbinfo, faddr, inetctlerrmap[cmd], notify); |
39236c6e | 920 | } |
1c79356b A |
921 | } |
922 | ||
2d21ac55 A |
923 | int |
924 | udp_ctloutput(struct socket *so, struct sockopt *sopt) | |
925 | { | |
527f9951 | 926 | int error = 0, optval = 0; |
2d21ac55 A |
927 | struct inpcb *inp; |
928 | ||
316670eb A |
929 | /* Allow <SOL_SOCKET,SO_FLUSH> at this level */ |
930 | if (sopt->sopt_level != IPPROTO_UDP && | |
931 | !(sopt->sopt_level == SOL_SOCKET && sopt->sopt_name == SO_FLUSH)) | |
2d21ac55 A |
932 | return (ip_ctloutput(so, sopt)); |
933 | ||
2d21ac55 A |
934 | inp = sotoinpcb(so); |
935 | ||
936 | switch (sopt->sopt_dir) { | |
937 | case SOPT_SET: | |
938 | switch (sopt->sopt_name) { | |
939 | case UDP_NOCKSUM: | |
940 | /* This option is settable only for UDP over IPv4 */ | |
941 | if (!(inp->inp_vflag & INP_IPV4)) { | |
942 | error = EINVAL; | |
943 | break; | |
944 | } | |
945 | ||
946 | if ((error = sooptcopyin(sopt, &optval, sizeof (optval), | |
947 | sizeof (optval))) != 0) | |
948 | break; | |
949 | ||
950 | if (optval != 0) | |
951 | inp->inp_flags |= INP_UDP_NOCKSUM; | |
952 | else | |
953 | inp->inp_flags &= ~INP_UDP_NOCKSUM; | |
954 | break; | |
3e170ce0 A |
955 | case UDP_KEEPALIVE_OFFLOAD: |
956 | { | |
957 | struct udp_keepalive_offload ka; | |
958 | /* | |
959 | * If the socket is not connected, the stack will | |
960 | * not know the destination address to put in the | |
961 | * keepalive datagram. Return an error now instead | |
962 | * of failing later. | |
963 | */ | |
964 | if (!(so->so_state & SS_ISCONNECTED)) { | |
965 | error = EINVAL; | |
966 | break; | |
967 | } | |
968 | if (sopt->sopt_valsize != sizeof(ka)) { | |
969 | error = EINVAL; | |
970 | break; | |
971 | } | |
972 | if ((error = sooptcopyin(sopt, &ka, sizeof(ka), | |
973 | sizeof(ka))) != 0) | |
974 | break; | |
975 | ||
976 | /* application should specify the type */ | |
977 | if (ka.ka_type == 0) | |
978 | return (EINVAL); | |
979 | ||
980 | if (ka.ka_interval == 0) { | |
981 | /* | |
982 | * if interval is 0, disable the offload | |
983 | * mechanism | |
984 | */ | |
985 | if (inp->inp_keepalive_data != NULL) | |
986 | FREE(inp->inp_keepalive_data, | |
987 | M_TEMP); | |
988 | inp->inp_keepalive_data = NULL; | |
989 | inp->inp_keepalive_datalen = 0; | |
990 | inp->inp_keepalive_interval = 0; | |
991 | inp->inp_keepalive_type = 0; | |
992 | inp->inp_flags2 &= ~INP2_KEEPALIVE_OFFLOAD; | |
993 | } else { | |
994 | if (inp->inp_keepalive_data != NULL) { | |
995 | FREE(inp->inp_keepalive_data, | |
996 | M_TEMP); | |
997 | inp->inp_keepalive_data = NULL; | |
998 | } | |
2d21ac55 | 999 | |
3e170ce0 A |
1000 | inp->inp_keepalive_datalen = min( |
1001 | ka.ka_data_len, | |
1002 | UDP_KEEPALIVE_OFFLOAD_DATA_SIZE); | |
1003 | if (inp->inp_keepalive_datalen > 0) { | |
1004 | MALLOC(inp->inp_keepalive_data, | |
39037602 | 1005 | u_int8_t *, |
3e170ce0 A |
1006 | inp->inp_keepalive_datalen, |
1007 | M_TEMP, M_WAITOK); | |
1008 | if (inp->inp_keepalive_data == NULL) { | |
1009 | inp->inp_keepalive_datalen = 0; | |
1010 | error = ENOMEM; | |
1011 | break; | |
1012 | } | |
1013 | bcopy(ka.ka_data, | |
1014 | inp->inp_keepalive_data, | |
1015 | inp->inp_keepalive_datalen); | |
1016 | } else { | |
1017 | inp->inp_keepalive_datalen = 0; | |
1018 | } | |
1019 | inp->inp_keepalive_interval = | |
1020 | min(UDP_KEEPALIVE_INTERVAL_MAX_SECONDS, | |
1021 | ka.ka_interval); | |
1022 | inp->inp_keepalive_type = ka.ka_type; | |
1023 | inp->inp_flags2 |= INP2_KEEPALIVE_OFFLOAD; | |
1024 | } | |
1025 | break; | |
1026 | } | |
316670eb A |
1027 | case SO_FLUSH: |
1028 | if ((error = sooptcopyin(sopt, &optval, sizeof (optval), | |
1029 | sizeof (optval))) != 0) | |
1030 | break; | |
1031 | ||
1032 | error = inp_flush(inp, optval); | |
1033 | break; | |
1034 | ||
2d21ac55 A |
1035 | default: |
1036 | error = ENOPROTOOPT; | |
1037 | break; | |
1038 | } | |
1039 | break; | |
1040 | ||
1041 | case SOPT_GET: | |
1042 | switch (sopt->sopt_name) { | |
1043 | case UDP_NOCKSUM: | |
1044 | optval = inp->inp_flags & INP_UDP_NOCKSUM; | |
1045 | break; | |
1046 | ||
1047 | default: | |
1048 | error = ENOPROTOOPT; | |
1049 | break; | |
1050 | } | |
1051 | if (error == 0) | |
1052 | error = sooptcopyout(sopt, &optval, sizeof (optval)); | |
1053 | break; | |
1054 | } | |
1055 | return (error); | |
1056 | } | |
1057 | ||
1c79356b A |
1058 | static int |
1059 | udp_pcblist SYSCTL_HANDLER_ARGS | |
1060 | { | |
2d21ac55 | 1061 | #pragma unused(oidp, arg1, arg2) |
91447636 | 1062 | int error, i, n; |
1c79356b A |
1063 | struct inpcb *inp, **inp_list; |
1064 | inp_gen_t gencnt; | |
1065 | struct xinpgen xig; | |
1066 | ||
1067 | /* | |
1068 | * The process of preparing the TCB list is too time-consuming and | |
1069 | * resource-intensive to repeat twice on every request. | |
1070 | */ | |
39236c6e | 1071 | lck_rw_lock_exclusive(udbinfo.ipi_lock); |
91447636 | 1072 | if (req->oldptr == USER_ADDR_NULL) { |
1c79356b | 1073 | n = udbinfo.ipi_count; |
39236c6e A |
1074 | req->oldidx = 2 * (sizeof (xig)) |
1075 | + (n + n/8) * sizeof (struct xinpcb); | |
1076 | lck_rw_done(udbinfo.ipi_lock); | |
1077 | return (0); | |
1c79356b A |
1078 | } |
1079 | ||
91447636 | 1080 | if (req->newptr != USER_ADDR_NULL) { |
39236c6e A |
1081 | lck_rw_done(udbinfo.ipi_lock); |
1082 | return (EPERM); | |
91447636 | 1083 | } |
1c79356b A |
1084 | |
1085 | /* | |
1086 | * OK, now we're committed to doing something. | |
1087 | */ | |
1c79356b A |
1088 | gencnt = udbinfo.ipi_gencnt; |
1089 | n = udbinfo.ipi_count; | |
1c79356b | 1090 | |
39236c6e A |
1091 | bzero(&xig, sizeof (xig)); |
1092 | xig.xig_len = sizeof (xig); | |
1c79356b A |
1093 | xig.xig_count = n; |
1094 | xig.xig_gen = gencnt; | |
1095 | xig.xig_sogen = so_gencnt; | |
39236c6e | 1096 | error = SYSCTL_OUT(req, &xig, sizeof (xig)); |
91447636 | 1097 | if (error) { |
39236c6e A |
1098 | lck_rw_done(udbinfo.ipi_lock); |
1099 | return (error); | |
91447636 | 1100 | } |
39236c6e A |
1101 | /* |
1102 | * We are done if there is no pcb | |
1103 | */ | |
1104 | if (n == 0) { | |
1105 | lck_rw_done(udbinfo.ipi_lock); | |
1106 | return (0); | |
1107 | } | |
1108 | ||
1109 | inp_list = _MALLOC(n * sizeof (*inp_list), M_TEMP, M_WAITOK); | |
1c79356b | 1110 | if (inp_list == 0) { |
39236c6e A |
1111 | lck_rw_done(udbinfo.ipi_lock); |
1112 | return (ENOMEM); | |
1c79356b | 1113 | } |
39236c6e A |
1114 | |
1115 | for (inp = LIST_FIRST(udbinfo.ipi_listhead), i = 0; inp && i < n; | |
39037602 | 1116 | inp = LIST_NEXT(inp, inp_list)) { |
39236c6e A |
1117 | if (inp->inp_gencnt <= gencnt && |
1118 | inp->inp_state != INPCB_STATE_DEAD) | |
1c79356b A |
1119 | inp_list[i++] = inp; |
1120 | } | |
1c79356b A |
1121 | n = i; |
1122 | ||
1123 | error = 0; | |
1124 | for (i = 0; i < n; i++) { | |
39037602 A |
1125 | struct xinpcb xi; |
1126 | ||
1c79356b | 1127 | inp = inp_list[i]; |
39037602 A |
1128 | |
1129 | if (in_pcb_checkstate(inp, WNT_ACQUIRE, 0) == WNT_STOPUSING) | |
1130 | continue; | |
1131 | udp_lock(inp->inp_socket, 1, 0); | |
1132 | if (in_pcb_checkstate(inp, WNT_RELEASE, 1) == WNT_STOPUSING) { | |
1133 | udp_unlock(inp->inp_socket, 1, 0); | |
1134 | continue; | |
1135 | } | |
1136 | if (inp->inp_gencnt > gencnt) { | |
1137 | udp_unlock(inp->inp_socket, 1, 0); | |
1138 | continue; | |
1c79356b | 1139 | } |
39037602 A |
1140 | |
1141 | bzero(&xi, sizeof (xi)); | |
1142 | xi.xi_len = sizeof (xi); | |
1143 | /* XXX should avoid extra copy */ | |
1144 | inpcb_to_compat(inp, &xi.xi_inp); | |
1145 | if (inp->inp_socket) | |
1146 | sotoxsocket(inp->inp_socket, &xi.xi_socket); | |
1147 | ||
1148 | udp_unlock(inp->inp_socket, 1, 0); | |
1149 | ||
1150 | error = SYSCTL_OUT(req, &xi, sizeof (xi)); | |
1c79356b A |
1151 | } |
1152 | if (!error) { | |
1153 | /* | |
1154 | * Give the user an updated idea of our state. | |
1155 | * If the generation differs from what we told | |
1156 | * her before, she knows that something happened | |
1157 | * while we were processing this request, and it | |
1158 | * might be necessary to retry. | |
1159 | */ | |
39236c6e A |
1160 | bzero(&xig, sizeof (xig)); |
1161 | xig.xig_len = sizeof (xig); | |
1c79356b A |
1162 | xig.xig_gen = udbinfo.ipi_gencnt; |
1163 | xig.xig_sogen = so_gencnt; | |
1164 | xig.xig_count = udbinfo.ipi_count; | |
39236c6e | 1165 | error = SYSCTL_OUT(req, &xig, sizeof (xig)); |
1c79356b A |
1166 | } |
1167 | FREE(inp_list, M_TEMP); | |
39236c6e A |
1168 | lck_rw_done(udbinfo.ipi_lock); |
1169 | return (error); | |
1c79356b A |
1170 | } |
1171 | ||
39236c6e | 1172 | SYSCTL_PROC(_net_inet_udp, UDPCTL_PCBLIST, pcblist, |
39037602 A |
1173 | CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_LOCKED, 0, 0, udp_pcblist, |
1174 | "S,xinpcb", "List of active UDP sockets"); | |
1c79356b | 1175 | |
5ba3f43e | 1176 | #if !CONFIG_EMBEDDED |
1c79356b | 1177 | |
b0d623f7 A |
1178 | static int |
1179 | udp_pcblist64 SYSCTL_HANDLER_ARGS | |
1180 | { | |
1181 | #pragma unused(oidp, arg1, arg2) | |
39037602 A |
1182 | int error, i, n; |
1183 | struct inpcb *inp, **inp_list; | |
1184 | inp_gen_t gencnt; | |
1185 | struct xinpgen xig; | |
1186 | ||
1187 | /* | |
1188 | * The process of preparing the TCB list is too time-consuming and | |
1189 | * resource-intensive to repeat twice on every request. | |
1190 | */ | |
1191 | lck_rw_lock_shared(udbinfo.ipi_lock); | |
1192 | if (req->oldptr == USER_ADDR_NULL) { | |
1193 | n = udbinfo.ipi_count; | |
1194 | req->oldidx = | |
39236c6e | 1195 | 2 * (sizeof (xig)) + (n + n/8) * sizeof (struct xinpcb64); |
39037602 A |
1196 | lck_rw_done(udbinfo.ipi_lock); |
1197 | return (0); | |
1198 | } | |
1199 | ||
1200 | if (req->newptr != USER_ADDR_NULL) { | |
1201 | lck_rw_done(udbinfo.ipi_lock); | |
1202 | return (EPERM); | |
1203 | } | |
1204 | ||
1205 | /* | |
1206 | * OK, now we're committed to doing something. | |
1207 | */ | |
1208 | gencnt = udbinfo.ipi_gencnt; | |
1209 | n = udbinfo.ipi_count; | |
1210 | ||
1211 | bzero(&xig, sizeof (xig)); | |
1212 | xig.xig_len = sizeof (xig); | |
1213 | xig.xig_count = n; | |
1214 | xig.xig_gen = gencnt; | |
1215 | xig.xig_sogen = so_gencnt; | |
1216 | error = SYSCTL_OUT(req, &xig, sizeof (xig)); | |
1217 | if (error) { | |
1218 | lck_rw_done(udbinfo.ipi_lock); | |
1219 | return (error); | |
1220 | } | |
39236c6e A |
1221 | /* |
1222 | * We are done if there is no pcb | |
1223 | */ | |
1224 | if (n == 0) { | |
1225 | lck_rw_done(udbinfo.ipi_lock); | |
1226 | return (0); | |
1227 | } | |
1228 | ||
39037602 A |
1229 | inp_list = _MALLOC(n * sizeof (*inp_list), M_TEMP, M_WAITOK); |
1230 | if (inp_list == 0) { | |
1231 | lck_rw_done(udbinfo.ipi_lock); | |
1232 | return (ENOMEM); | |
1233 | } | |
b0d623f7 | 1234 | |
39037602 A |
1235 | for (inp = LIST_FIRST(udbinfo.ipi_listhead), i = 0; inp && i < n; |
1236 | inp = LIST_NEXT(inp, inp_list)) { | |
1237 | if (inp->inp_gencnt <= gencnt && | |
39236c6e | 1238 | inp->inp_state != INPCB_STATE_DEAD) |
39037602 A |
1239 | inp_list[i++] = inp; |
1240 | } | |
1241 | n = i; | |
1242 | ||
1243 | error = 0; | |
1244 | for (i = 0; i < n; i++) { | |
1245 | struct xinpcb64 xi; | |
1246 | ||
1247 | inp = inp_list[i]; | |
1248 | ||
1249 | if (in_pcb_checkstate(inp, WNT_ACQUIRE, 0) == WNT_STOPUSING) | |
1250 | continue; | |
1251 | udp_lock(inp->inp_socket, 1, 0); | |
1252 | if (in_pcb_checkstate(inp, WNT_RELEASE, 1) == WNT_STOPUSING) { | |
1253 | udp_unlock(inp->inp_socket, 1, 0); | |
1254 | continue; | |
1255 | } | |
1256 | if (inp->inp_gencnt > gencnt) { | |
1257 | udp_unlock(inp->inp_socket, 1, 0); | |
1258 | continue; | |
1259 | } | |
1260 | ||
1261 | bzero(&xi, sizeof (xi)); | |
1262 | xi.xi_len = sizeof (xi); | |
1263 | inpcb_to_xinpcb64(inp, &xi); | |
1264 | if (inp->inp_socket) | |
1265 | sotoxsocket64(inp->inp_socket, &xi.xi_socket); | |
1266 | ||
1267 | udp_unlock(inp->inp_socket, 1, 0); | |
1268 | ||
1269 | error = SYSCTL_OUT(req, &xi, sizeof (xi)); | |
1270 | } | |
1271 | if (!error) { | |
1272 | /* | |
1273 | * Give the user an updated idea of our state. | |
1274 | * If the generation differs from what we told | |
1275 | * her before, she knows that something happened | |
1276 | * while we were processing this request, and it | |
1277 | * might be necessary to retry. | |
1278 | */ | |
1279 | bzero(&xig, sizeof (xig)); | |
1280 | xig.xig_len = sizeof (xig); | |
1281 | xig.xig_gen = udbinfo.ipi_gencnt; | |
1282 | xig.xig_sogen = so_gencnt; | |
1283 | xig.xig_count = udbinfo.ipi_count; | |
1284 | error = SYSCTL_OUT(req, &xig, sizeof (xig)); | |
1285 | } | |
1286 | FREE(inp_list, M_TEMP); | |
1287 | lck_rw_done(udbinfo.ipi_lock); | |
1288 | return (error); | |
b0d623f7 A |
1289 | } |
1290 | ||
39236c6e | 1291 | SYSCTL_PROC(_net_inet_udp, OID_AUTO, pcblist64, |
39037602 A |
1292 | CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_LOCKED, 0, 0, udp_pcblist64, |
1293 | "S,xinpcb64", "List of active UDP sockets"); | |
b0d623f7 | 1294 | |
5ba3f43e | 1295 | #endif /* !CONFIG_EMBEDDED */ |
1c79356b | 1296 | |
6d2010ae A |
1297 | static int |
1298 | udp_pcblist_n SYSCTL_HANDLER_ARGS | |
1299 | { | |
1300 | #pragma unused(oidp, arg1, arg2) | |
39236c6e | 1301 | return (get_pcblist_n(IPPROTO_UDP, req, &udbinfo)); |
6d2010ae A |
1302 | } |
1303 | ||
39236c6e | 1304 | SYSCTL_PROC(_net_inet_udp, OID_AUTO, pcblist_n, |
39037602 A |
1305 | CTLTYPE_STRUCT | CTLFLAG_RD | CTLFLAG_LOCKED, 0, 0, udp_pcblist_n, |
1306 | "S,xinpcb_n", "List of active UDP sockets"); | |
6d2010ae | 1307 | |
316670eb | 1308 | __private_extern__ void |
fe8ab488 | 1309 | udp_get_ports_used(uint32_t ifindex, int protocol, uint32_t flags, |
39236c6e | 1310 | bitstr_t *bitfield) |
316670eb | 1311 | { |
5ba3f43e A |
1312 | inpcb_get_ports_used(ifindex, protocol, flags, bitfield, |
1313 | &udbinfo); | |
a39ff7e2 | 1314 | } |
316670eb A |
1315 | |
1316 | __private_extern__ uint32_t | |
1317 | udp_count_opportunistic(unsigned int ifindex, u_int32_t flags) | |
1318 | { | |
39236c6e | 1319 | return (inpcb_count_opportunistic(ifindex, &udbinfo, flags)); |
316670eb A |
1320 | } |
1321 | ||
39236c6e A |
1322 | __private_extern__ uint32_t |
1323 | udp_find_anypcb_byaddr(struct ifaddr *ifa) | |
91447636 | 1324 | { |
5ba3f43e | 1325 | return (inpcb_find_anypcb_byaddr(ifa, &udbinfo)); |
91447636 A |
1326 | } |
1327 | ||
6d2010ae | 1328 | static int |
39236c6e A |
1329 | udp_check_pktinfo(struct mbuf *control, struct ifnet **outif, |
1330 | struct in_addr *laddr) | |
6d2010ae A |
1331 | { |
1332 | struct cmsghdr *cm = 0; | |
316670eb | 1333 | struct in_pktinfo *pktinfo; |
6d2010ae A |
1334 | struct ifnet *ifp; |
1335 | ||
39236c6e A |
1336 | if (outif != NULL) |
1337 | *outif = NULL; | |
1338 | ||
6d2010ae A |
1339 | /* |
1340 | * XXX: Currently, we assume all the optional information is stored | |
1341 | * in a single mbuf. | |
1342 | */ | |
1343 | if (control->m_next) | |
1344 | return (EINVAL); | |
1345 | ||
1346 | if (control->m_len < CMSG_LEN(0)) | |
1347 | return (EINVAL); | |
1348 | ||
39236c6e A |
1349 | for (cm = M_FIRST_CMSGHDR(control); cm; |
1350 | cm = M_NXT_CMSGHDR(control, cm)) { | |
1351 | if (cm->cmsg_len < sizeof (struct cmsghdr) || | |
1352 | cm->cmsg_len > control->m_len) | |
6d2010ae | 1353 | return (EINVAL); |
316670eb | 1354 | |
6d2010ae A |
1355 | if (cm->cmsg_level != IPPROTO_IP || cm->cmsg_type != IP_PKTINFO) |
1356 | continue; | |
1357 | ||
39236c6e | 1358 | if (cm->cmsg_len != CMSG_LEN(sizeof (struct in_pktinfo))) |
6d2010ae A |
1359 | return (EINVAL); |
1360 | ||
316670eb | 1361 | pktinfo = (struct in_pktinfo *)(void *)CMSG_DATA(cm); |
6d2010ae A |
1362 | |
1363 | /* Check for a valid ifindex in pktinfo */ | |
1364 | ifnet_head_lock_shared(); | |
1365 | ||
1366 | if (pktinfo->ipi_ifindex > if_index) { | |
1367 | ifnet_head_done(); | |
1368 | return (ENXIO); | |
1369 | } | |
1370 | ||
39236c6e A |
1371 | /* |
1372 | * If ipi_ifindex is specified it takes precedence | |
1373 | * over ipi_spec_dst. | |
1374 | */ | |
6d2010ae A |
1375 | if (pktinfo->ipi_ifindex) { |
1376 | ifp = ifindex2ifnet[pktinfo->ipi_ifindex]; | |
1377 | if (ifp == NULL) { | |
1378 | ifnet_head_done(); | |
1379 | return (ENXIO); | |
1380 | } | |
39236c6e A |
1381 | if (outif != NULL) { |
1382 | ifnet_reference(ifp); | |
316670eb | 1383 | *outif = ifp; |
39236c6e A |
1384 | } |
1385 | ifnet_head_done(); | |
6d2010ae A |
1386 | laddr->s_addr = INADDR_ANY; |
1387 | break; | |
1388 | } | |
1389 | ||
1390 | ifnet_head_done(); | |
1391 | ||
39236c6e A |
1392 | /* |
1393 | * Use the provided ipi_spec_dst address for temp | |
1394 | * source address. | |
1395 | */ | |
6d2010ae A |
1396 | *laddr = pktinfo->ipi_spec_dst; |
1397 | break; | |
1398 | } | |
1399 | return (0); | |
1400 | } | |
1401 | ||
5ba3f43e | 1402 | int |
39236c6e A |
1403 | udp_output(struct inpcb *inp, struct mbuf *m, struct sockaddr *addr, |
1404 | struct mbuf *control, struct proc *p) | |
1c79356b | 1405 | { |
39236c6e A |
1406 | struct udpiphdr *ui; |
1407 | int len = m->m_pkthdr.len; | |
2d21ac55 | 1408 | struct sockaddr_in *sin; |
6d2010ae | 1409 | struct in_addr origladdr, laddr, faddr, pi_laddr; |
91447636 | 1410 | u_short lport, fport; |
6d2010ae | 1411 | int error = 0, udp_dodisconnect = 0, pktinfo = 0; |
2d21ac55 | 1412 | struct socket *so = inp->inp_socket; |
c910b4d9 | 1413 | int soopts = 0; |
2d21ac55 A |
1414 | struct mbuf *inpopts; |
1415 | struct ip_moptions *mopts; | |
1416 | struct route ro; | |
a39ff7e2 A |
1417 | struct ip_out_args ipoa; |
1418 | ||
1419 | bzero(&ipoa, sizeof(ipoa)); | |
1420 | ipoa.ipoa_boundif = IFSCOPE_NONE; | |
1421 | ipoa.ipoa_flags = IPOAF_SELECT_SRCIF; | |
1422 | ||
316670eb A |
1423 | struct ifnet *outif = NULL; |
1424 | struct flowadv *adv = &ipoa.ipoa_flowadv; | |
39037602 A |
1425 | int sotc = SO_TC_UNSPEC; |
1426 | int netsvctype = _NET_SERVICE_TYPE_UNSPEC; | |
fe8ab488 | 1427 | struct ifnet *origoutifp = NULL; |
316670eb A |
1428 | int flowadv = 0; |
1429 | ||
1430 | /* Enable flow advisory only when connected */ | |
1431 | flowadv = (so->so_state & SS_ISCONNECTED) ? 1 : 0; | |
6d2010ae | 1432 | pi_laddr.s_addr = INADDR_ANY; |
1c79356b | 1433 | |
39037602 | 1434 | KERNEL_DEBUG(DBG_FNC_UDP_OUTPUT | DBG_FUNC_START, 0, 0, 0, 0, 0); |
1c79356b | 1435 | |
5ba3f43e | 1436 | socket_lock_assert_owned(so); |
d41d1dae | 1437 | if (control != NULL) { |
39037602 | 1438 | sotc = so_tc_from_control(control, &netsvctype); |
39236c6e | 1439 | VERIFY(outif == NULL); |
316670eb | 1440 | error = udp_check_pktinfo(control, &outif, &pi_laddr); |
d41d1dae | 1441 | m_freem(control); |
39236c6e | 1442 | control = NULL; |
6d2010ae A |
1443 | if (error) |
1444 | goto release; | |
1445 | pktinfo++; | |
316670eb A |
1446 | if (outif != NULL) |
1447 | ipoa.ipoa_boundif = outif->if_index; | |
d41d1dae | 1448 | } |
39037602 A |
1449 | if (sotc == SO_TC_UNSPEC) { |
1450 | sotc = so->so_traffic_class; | |
1451 | netsvctype = so->so_netsvctype; | |
1452 | } | |
6d2010ae | 1453 | |
1c79356b | 1454 | KERNEL_DEBUG(DBG_LAYER_OUT_BEG, inp->inp_fport, inp->inp_lport, |
39236c6e A |
1455 | inp->inp_laddr.s_addr, inp->inp_faddr.s_addr, |
1456 | (htons((u_short)len + sizeof (struct udphdr)))); | |
1c79356b | 1457 | |
39236c6e | 1458 | if (len + sizeof (struct udpiphdr) > IP_MAXPACKET) { |
1c79356b A |
1459 | error = EMSGSIZE; |
1460 | goto release; | |
1461 | } | |
1462 | ||
316670eb A |
1463 | if (flowadv && INP_WAIT_FOR_IF_FEEDBACK(inp)) { |
1464 | /* | |
1465 | * The socket is flow-controlled, drop the packets | |
1466 | * until the inp is not flow controlled | |
1467 | */ | |
1468 | error = ENOBUFS; | |
1469 | goto release; | |
1470 | } | |
6d2010ae A |
1471 | /* |
1472 | * If socket was bound to an ifindex, tell ip_output about it. | |
1473 | * If the ancillary IP_PKTINFO option contains an interface index, | |
1474 | * it takes precedence over the one specified by IP_BOUND_IF. | |
1475 | */ | |
39236c6e A |
1476 | if (ipoa.ipoa_boundif == IFSCOPE_NONE && |
1477 | (inp->inp_flags & INP_BOUND_IF)) { | |
1478 | VERIFY(inp->inp_boundifp != NULL); | |
1479 | ifnet_reference(inp->inp_boundifp); /* for this routine */ | |
1480 | if (outif != NULL) | |
1481 | ifnet_release(outif); | |
316670eb A |
1482 | outif = inp->inp_boundifp; |
1483 | ipoa.ipoa_boundif = outif->if_index; | |
1484 | } | |
fe8ab488 | 1485 | if (INP_NO_CELLULAR(inp)) |
316670eb | 1486 | ipoa.ipoa_flags |= IPOAF_NO_CELLULAR; |
fe8ab488 A |
1487 | if (INP_NO_EXPENSIVE(inp)) |
1488 | ipoa.ipoa_flags |= IPOAF_NO_EXPENSIVE; | |
1489 | if (INP_AWDL_UNRESTRICTED(inp)) | |
1490 | ipoa.ipoa_flags |= IPOAF_AWDL_UNRESTRICTED; | |
39037602 A |
1491 | ipoa.ipoa_sotc = sotc; |
1492 | ipoa.ipoa_netsvctype = netsvctype; | |
c910b4d9 A |
1493 | soopts |= IP_OUTARGS; |
1494 | ||
39236c6e A |
1495 | /* |
1496 | * If there was a routing change, discard cached route and check | |
1497 | * that we have a valid source address. Reacquire a new source | |
1498 | * address if INADDR_ANY was specified. | |
55e303ae | 1499 | */ |
39236c6e A |
1500 | if (ROUTE_UNUSABLE(&inp->inp_route)) { |
1501 | struct in_ifaddr *ia = NULL; | |
1502 | ||
1503 | ROUTE_RELEASE(&inp->inp_route); | |
b0d623f7 A |
1504 | |
1505 | /* src address is gone? */ | |
39236c6e A |
1506 | if (inp->inp_laddr.s_addr != INADDR_ANY && |
1507 | (ia = ifa_foraddr(inp->inp_laddr.s_addr)) == NULL) { | |
1508 | if (!(inp->inp_flags & INP_INADDR_ANY) || | |
316670eb | 1509 | (so->so_state & SS_ISCONNECTED)) { |
39236c6e A |
1510 | /* |
1511 | * Rdar://5448998 | |
1512 | * If the source address is gone, return an | |
1513 | * error if: | |
6d2010ae A |
1514 | * - the source was specified |
1515 | * - the socket was already connected | |
1516 | */ | |
39236c6e | 1517 | soevent(so, (SO_FILT_HINT_LOCKED | |
316670eb | 1518 | SO_FILT_HINT_NOSRCADDR)); |
55e303ae A |
1519 | error = EADDRNOTAVAIL; |
1520 | goto release; | |
6d2010ae A |
1521 | } else { |
1522 | /* new src will be set later */ | |
1523 | inp->inp_laddr.s_addr = INADDR_ANY; | |
316670eb | 1524 | inp->inp_last_outifp = NULL; |
55e303ae A |
1525 | } |
1526 | } | |
b0d623f7 | 1527 | if (ia != NULL) |
6d2010ae | 1528 | IFA_REMREF(&ia->ia_ifa); |
55e303ae A |
1529 | } |
1530 | ||
39236c6e A |
1531 | /* |
1532 | * IP_PKTINFO option check. If a temporary scope or src address | |
1533 | * is provided, use it for this packet only and make sure we forget | |
1534 | * it after sending this datagram. | |
6d2010ae | 1535 | */ |
6d2010ae A |
1536 | if (pi_laddr.s_addr != INADDR_ANY || |
1537 | (ipoa.ipoa_boundif != IFSCOPE_NONE && pktinfo)) { | |
39236c6e A |
1538 | /* temp src address for this datagram only */ |
1539 | laddr = pi_laddr; | |
6d2010ae | 1540 | origladdr.s_addr = INADDR_ANY; |
39236c6e A |
1541 | /* we don't want to keep the laddr or route */ |
1542 | udp_dodisconnect = 1; | |
39037602 | 1543 | /* remember we don't care about src addr */ |
39236c6e | 1544 | inp->inp_flags |= INP_INADDR_ANY; |
6d2010ae A |
1545 | } else { |
1546 | origladdr = laddr = inp->inp_laddr; | |
1547 | } | |
1548 | ||
316670eb | 1549 | origoutifp = inp->inp_last_outifp; |
91447636 A |
1550 | faddr = inp->inp_faddr; |
1551 | lport = inp->inp_lport; | |
1552 | fport = inp->inp_fport; | |
1553 | ||
1c79356b | 1554 | if (addr) { |
316670eb | 1555 | sin = (struct sockaddr_in *)(void *)addr; |
91447636 | 1556 | if (faddr.s_addr != INADDR_ANY) { |
1c79356b A |
1557 | error = EISCONN; |
1558 | goto release; | |
1559 | } | |
91447636 A |
1560 | if (lport == 0) { |
1561 | /* | |
39236c6e A |
1562 | * In case we don't have a local port set, go through |
1563 | * the full connect. We don't have a local port yet | |
1564 | * (i.e., we can't be looked up), so it's not an issue | |
1565 | * if the input runs at the same time we do this. | |
6d2010ae | 1566 | */ |
39236c6e A |
1567 | /* if we have a source address specified, use that */ |
1568 | if (pi_laddr.s_addr != INADDR_ANY) | |
6d2010ae | 1569 | inp->inp_laddr = pi_laddr; |
39236c6e A |
1570 | /* |
1571 | * If a scope is specified, use it. Scope from | |
1572 | * IP_PKTINFO takes precendence over the the scope | |
1573 | * set via INP_BOUND_IF. | |
1574 | */ | |
1575 | error = in_pcbconnect(inp, addr, p, ipoa.ipoa_boundif, | |
1576 | &outif); | |
1577 | if (error) | |
91447636 | 1578 | goto release; |
39236c6e | 1579 | |
91447636 A |
1580 | laddr = inp->inp_laddr; |
1581 | lport = inp->inp_lport; | |
1582 | faddr = inp->inp_faddr; | |
1583 | fport = inp->inp_fport; | |
1584 | udp_dodisconnect = 1; | |
39236c6e A |
1585 | |
1586 | /* synch up in case in_pcbladdr() overrides */ | |
1587 | if (outif != NULL && ipoa.ipoa_boundif != IFSCOPE_NONE) | |
1588 | ipoa.ipoa_boundif = outif->if_index; | |
39037602 | 1589 | } else { |
39236c6e A |
1590 | /* |
1591 | * Fast path case | |
1592 | * | |
1593 | * We have a full address and a local port; use those | |
1594 | * info to build the packet without changing the pcb | |
1595 | * and interfering with the input path. See 3851370. | |
1596 | * | |
1597 | * Scope from IP_PKTINFO takes precendence over the | |
1598 | * the scope set via INP_BOUND_IF. | |
91447636 A |
1599 | */ |
1600 | if (laddr.s_addr == INADDR_ANY) { | |
39236c6e | 1601 | if ((error = in_pcbladdr(inp, addr, &laddr, |
39037602 | 1602 | ipoa.ipoa_boundif, &outif, 0)) != 0) |
316670eb | 1603 | goto release; |
39236c6e A |
1604 | /* |
1605 | * from pcbconnect: remember we don't | |
1606 | * care about src addr. | |
1607 | */ | |
1608 | inp->inp_flags |= INP_INADDR_ANY; | |
1609 | ||
1610 | /* synch up in case in_pcbladdr() overrides */ | |
1611 | if (outif != NULL && | |
1612 | ipoa.ipoa_boundif != IFSCOPE_NONE) | |
1613 | ipoa.ipoa_boundif = outif->if_index; | |
91447636 | 1614 | } |
6d2010ae | 1615 | |
91447636 A |
1616 | faddr = sin->sin_addr; |
1617 | fport = sin->sin_port; | |
1c79356b A |
1618 | } |
1619 | } else { | |
91447636 | 1620 | if (faddr.s_addr == INADDR_ANY) { |
1c79356b A |
1621 | error = ENOTCONN; |
1622 | goto release; | |
1623 | } | |
1624 | } | |
55e303ae | 1625 | |
2d21ac55 A |
1626 | #if CONFIG_MACF_NET |
1627 | mac_mbuf_label_associate_inpcb(inp, m); | |
39236c6e A |
1628 | #endif /* CONFIG_MACF_NET */ |
1629 | ||
316670eb A |
1630 | if (inp->inp_flowhash == 0) |
1631 | inp->inp_flowhash = inp_calc_flowhash(inp); | |
55e303ae | 1632 | |
5ba3f43e A |
1633 | if (fport == htons(53) && !(so->so_flags1 & SOF1_DNS_COUNTED)) { |
1634 | so->so_flags1 |= SOF1_DNS_COUNTED; | |
1635 | INC_ATOMIC_INT64_LIM(net_api_stats.nas_socket_inet_dgram_dns); | |
1636 | } | |
1637 | ||
1c79356b A |
1638 | /* |
1639 | * Calculate data length and get a mbuf | |
1640 | * for UDP and IP headers. | |
1641 | */ | |
3e170ce0 | 1642 | M_PREPEND(m, sizeof (struct udpiphdr), M_DONTWAIT, 1); |
1c79356b A |
1643 | if (m == 0) { |
1644 | error = ENOBUFS; | |
55e303ae | 1645 | goto abort; |
1c79356b A |
1646 | } |
1647 | ||
1648 | /* | |
1649 | * Fill in mbuf with extended UDP header | |
1650 | * and addresses and length put into network format. | |
1651 | */ | |
1652 | ui = mtod(m, struct udpiphdr *); | |
39236c6e | 1653 | bzero(ui->ui_x1, sizeof (ui->ui_x1)); /* XXX still needed? */ |
1c79356b | 1654 | ui->ui_pr = IPPROTO_UDP; |
91447636 A |
1655 | ui->ui_src = laddr; |
1656 | ui->ui_dst = faddr; | |
1657 | ui->ui_sport = lport; | |
1658 | ui->ui_dport = fport; | |
39236c6e | 1659 | ui->ui_ulen = htons((u_short)len + sizeof (struct udphdr)); |
1c79356b A |
1660 | |
1661 | /* | |
9bccf70c | 1662 | * Set up checksum and output datagram. |
1c79356b | 1663 | */ |
2d21ac55 | 1664 | if (udpcksum && !(inp->inp_flags & INP_UDP_NOCKSUM)) { |
39236c6e A |
1665 | ui->ui_sum = in_pseudo(ui->ui_src.s_addr, ui->ui_dst.s_addr, |
1666 | htons((u_short)len + sizeof (struct udphdr) + IPPROTO_UDP)); | |
5ba3f43e | 1667 | m->m_pkthdr.csum_flags = (CSUM_UDP|CSUM_ZERO_INVERT); |
9bccf70c A |
1668 | m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum); |
1669 | } else { | |
0b4e3aa0 | 1670 | ui->ui_sum = 0; |
9bccf70c | 1671 | } |
1c79356b A |
1672 | ((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len; |
1673 | ((struct ip *)ui)->ip_ttl = inp->inp_ip_ttl; /* XXX */ | |
1674 | ((struct ip *)ui)->ip_tos = inp->inp_ip_tos; /* XXX */ | |
1675 | udpstat.udps_opackets++; | |
1676 | ||
1677 | KERNEL_DEBUG(DBG_LAYER_OUT_END, ui->ui_dport, ui->ui_sport, | |
39037602 | 1678 | ui->ui_src.s_addr, ui->ui_dst.s_addr, ui->ui_ulen); |
3e170ce0 | 1679 | |
fe8ab488 A |
1680 | #if NECP |
1681 | { | |
1682 | necp_kernel_policy_id policy_id; | |
3e170ce0 | 1683 | u_int32_t route_rule_id; |
39037602 A |
1684 | |
1685 | /* | |
1686 | * We need a route to perform NECP route rule checks | |
1687 | */ | |
1688 | if (net_qos_policy_restricted != 0 && | |
1689 | ROUTE_UNUSABLE(&inp->inp_route)) { | |
1690 | struct sockaddr_in to; | |
1691 | struct sockaddr_in from; | |
1692 | ||
1693 | ROUTE_RELEASE(&inp->inp_route); | |
1694 | ||
1695 | bzero(&from, sizeof(struct sockaddr_in)); | |
1696 | from.sin_family = AF_INET; | |
1697 | from.sin_len = sizeof(struct sockaddr_in); | |
1698 | from.sin_addr = laddr; | |
1699 | ||
1700 | bzero(&to, sizeof(struct sockaddr_in)); | |
1701 | to.sin_family = AF_INET; | |
1702 | to.sin_len = sizeof(struct sockaddr_in); | |
1703 | to.sin_addr = faddr; | |
1704 | ||
1705 | inp->inp_route.ro_dst.sa_family = AF_INET; | |
1706 | inp->inp_route.ro_dst.sa_len = sizeof(struct sockaddr_in); | |
1707 | ((struct sockaddr_in *)(void *)&inp->inp_route.ro_dst)->sin_addr = | |
1708 | faddr; | |
1709 | ||
1710 | rtalloc_scoped(&inp->inp_route, ipoa.ipoa_boundif); | |
1711 | ||
1712 | inp_update_necp_policy(inp, (struct sockaddr *)&from, | |
1713 | (struct sockaddr *)&to, ipoa.ipoa_boundif); | |
1714 | inp->inp_policyresult.results.qos_marking_gencount = 0; | |
1715 | } | |
1716 | ||
3e170ce0 A |
1717 | if (!necp_socket_is_allowed_to_send_recv_v4(inp, lport, fport, |
1718 | &laddr, &faddr, NULL, &policy_id, &route_rule_id)) { | |
fe8ab488 A |
1719 | error = EHOSTUNREACH; |
1720 | goto abort; | |
1721 | } | |
1c79356b | 1722 | |
3e170ce0 | 1723 | necp_mark_packet_from_socket(m, inp, policy_id, route_rule_id); |
39037602 A |
1724 | |
1725 | if (net_qos_policy_restricted != 0) { | |
1726 | necp_socket_update_qos_marking(inp, | |
1727 | inp->inp_route.ro_rt, NULL, route_rule_id); | |
1728 | } | |
fe8ab488 A |
1729 | } |
1730 | #endif /* NECP */ | |
39037602 A |
1731 | if ((so->so_flags1 & SOF1_QOSMARKING_ALLOWED)) |
1732 | ipoa.ipoa_flags |= IPOAF_QOSMARKING_ALLOWED; | |
3e170ce0 | 1733 | |
1c79356b | 1734 | #if IPSEC |
fe8ab488 | 1735 | if (inp->inp_sp != NULL && ipsec_setsocket(m, inp->inp_socket) != 0) { |
9bccf70c | 1736 | error = ENOBUFS; |
55e303ae | 1737 | goto abort; |
9bccf70c | 1738 | } |
39236c6e | 1739 | #endif /* IPSEC */ |
2d21ac55 A |
1740 | |
1741 | inpopts = inp->inp_options; | |
c910b4d9 | 1742 | soopts |= (inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST)); |
2d21ac55 | 1743 | mopts = inp->inp_moptions; |
316670eb A |
1744 | if (mopts != NULL) { |
1745 | IMO_LOCK(mopts); | |
1746 | IMO_ADDREF_LOCKED(mopts); | |
1747 | if (IN_MULTICAST(ntohl(ui->ui_dst.s_addr)) && | |
39236c6e A |
1748 | mopts->imo_multicast_ifp != NULL) { |
1749 | /* no reference needed */ | |
316670eb | 1750 | inp->inp_last_outifp = mopts->imo_multicast_ifp; |
5ba3f43e | 1751 | |
316670eb A |
1752 | } |
1753 | IMO_UNLOCK(mopts); | |
1754 | } | |
2d21ac55 | 1755 | |
b0d623f7 A |
1756 | /* Copy the cached route and take an extra reference */ |
1757 | inp_route_copyout(inp, &ro); | |
2d21ac55 | 1758 | |
39037602 | 1759 | set_packet_service_class(m, so, sotc, 0); |
39236c6e A |
1760 | m->m_pkthdr.pkt_flowsrc = FLOWSRC_INPCB; |
1761 | m->m_pkthdr.pkt_flowid = inp->inp_flowhash; | |
1762 | m->m_pkthdr.pkt_proto = IPPROTO_UDP; | |
1763 | m->m_pkthdr.pkt_flags |= (PKTF_FLOW_ID | PKTF_FLOW_LOCALSRC); | |
316670eb | 1764 | if (flowadv) |
39236c6e | 1765 | m->m_pkthdr.pkt_flags |= PKTF_FLOW_ADV; |
316670eb A |
1766 | |
1767 | if (ipoa.ipoa_boundif != IFSCOPE_NONE) | |
1768 | ipoa.ipoa_flags |= IPOAF_BOUND_IF; | |
1769 | ||
1770 | if (laddr.s_addr != INADDR_ANY) | |
1771 | ipoa.ipoa_flags |= IPOAF_BOUND_SRCADDR; | |
1772 | ||
1773 | inp->inp_sndinprog_cnt++; | |
d1ecb069 | 1774 | |
2d21ac55 | 1775 | socket_unlock(so, 0); |
39236c6e | 1776 | error = ip_output(m, inpopts, &ro, soopts, mopts, &ipoa); |
6d2010ae | 1777 | m = NULL; |
2d21ac55 | 1778 | socket_lock(so, 0); |
6d2010ae A |
1779 | if (mopts != NULL) |
1780 | IMO_REMREF(mopts); | |
1c79356b | 1781 | |
6d2010ae | 1782 | if (error == 0 && nstat_collect) { |
fe8ab488 | 1783 | boolean_t cell, wifi, wired; |
39236c6e A |
1784 | |
1785 | if (ro.ro_rt != NULL) { | |
1786 | cell = IFNET_IS_CELLULAR(ro.ro_rt->rt_ifp); | |
1787 | wifi = (!cell && IFNET_IS_WIFI(ro.ro_rt->rt_ifp)); | |
fe8ab488 | 1788 | wired = (!wifi && IFNET_IS_WIRED(ro.ro_rt->rt_ifp)); |
39236c6e | 1789 | } else { |
fe8ab488 | 1790 | cell = wifi = wired = FALSE; |
39236c6e | 1791 | } |
fe8ab488 A |
1792 | INP_ADD_STAT(inp, cell, wifi, wired, txpackets, 1); |
1793 | INP_ADD_STAT(inp, cell, wifi, wired, txbytes, len); | |
5ba3f43e | 1794 | inp_set_activity_bitmap(inp); |
6d2010ae | 1795 | } |
316670eb A |
1796 | |
1797 | if (flowadv && (adv->code == FADV_FLOW_CONTROLLED || | |
1798 | adv->code == FADV_SUSPENDED)) { | |
39037602 A |
1799 | /* |
1800 | * return a hint to the application that | |
316670eb A |
1801 | * the packet has been dropped |
1802 | */ | |
1803 | error = ENOBUFS; | |
1804 | inp_set_fc_state(inp, adv->code); | |
1805 | } | |
1806 | ||
1807 | VERIFY(inp->inp_sndinprog_cnt > 0); | |
1808 | if ( --inp->inp_sndinprog_cnt == 0) | |
1809 | inp->inp_flags &= ~(INP_FC_FEEDBACK); | |
1810 | ||
b0d623f7 A |
1811 | /* Synchronize PCB cached route */ |
1812 | inp_route_copyin(inp, &ro); | |
1813 | ||
6d2010ae | 1814 | abort: |
91447636 | 1815 | if (udp_dodisconnect) { |
d1ecb069 | 1816 | /* Always discard the cached route for unconnected socket */ |
39236c6e | 1817 | ROUTE_RELEASE(&inp->inp_route); |
1c79356b | 1818 | in_pcbdisconnect(inp); |
91447636 | 1819 | inp->inp_laddr = origladdr; /* XXX rehash? */ |
39236c6e | 1820 | /* no reference needed */ |
316670eb | 1821 | inp->inp_last_outifp = origoutifp; |
5ba3f43e | 1822 | |
6d2010ae A |
1823 | } else if (inp->inp_route.ro_rt != NULL) { |
1824 | struct rtentry *rt = inp->inp_route.ro_rt; | |
316670eb | 1825 | struct ifnet *outifp; |
d1ecb069 | 1826 | |
6d2010ae A |
1827 | if (rt->rt_flags & (RTF_MULTICAST|RTF_BROADCAST)) |
1828 | rt = NULL; /* unusable */ | |
1829 | /* | |
1830 | * Always discard if it is a multicast or broadcast route. | |
1831 | */ | |
39236c6e A |
1832 | if (rt == NULL) |
1833 | ROUTE_RELEASE(&inp->inp_route); | |
1834 | ||
6d2010ae | 1835 | /* |
316670eb A |
1836 | * If the destination route is unicast, update outifp with |
1837 | * that of the route interface used by IP. | |
6d2010ae | 1838 | */ |
3e170ce0 A |
1839 | if (rt != NULL && |
1840 | (outifp = rt->rt_ifp) != inp->inp_last_outifp) { | |
1841 | inp->inp_last_outifp = outifp; /* no reference needed */ | |
1842 | ||
1843 | so->so_pktheadroom = P2ROUNDUP( | |
1844 | sizeof(struct udphdr) + | |
1845 | sizeof(struct ip) + | |
1846 | ifnet_hdrlen(outifp) + | |
5ba3f43e | 1847 | ifnet_mbuf_packetpreamblelen(outifp), |
3e170ce0 A |
1848 | sizeof(u_int32_t)); |
1849 | } | |
39236c6e A |
1850 | } else { |
1851 | ROUTE_RELEASE(&inp->inp_route); | |
d1ecb069 | 1852 | } |
55e303ae | 1853 | |
39236c6e | 1854 | /* |
fe8ab488 A |
1855 | * If output interface was cellular/expensive, and this socket is |
1856 | * denied access to it, generate an event. | |
39236c6e A |
1857 | */ |
1858 | if (error != 0 && (ipoa.ipoa_retflags & IPOARF_IFDENIED) && | |
fe8ab488 | 1859 | (INP_NO_CELLULAR(inp) || INP_NO_EXPENSIVE(inp))) |
39236c6e A |
1860 | soevent(so, (SO_FILT_HINT_LOCKED|SO_FILT_HINT_IFDENIED)); |
1861 | ||
1c79356b | 1862 | release: |
39236c6e A |
1863 | KERNEL_DEBUG(DBG_FNC_UDP_OUTPUT | DBG_FUNC_END, error, 0, 0, 0, 0); |
1864 | ||
6d2010ae A |
1865 | if (m != NULL) |
1866 | m_freem(m); | |
39236c6e A |
1867 | |
1868 | if (outif != NULL) | |
1869 | ifnet_release(outif); | |
1870 | ||
1c79356b A |
1871 | return (error); |
1872 | } | |
1873 | ||
b0d623f7 | 1874 | u_int32_t udp_sendspace = 9216; /* really max datagram size */ |
316670eb A |
1875 | /* 187 1K datagrams (approx 192 KB) */ |
1876 | u_int32_t udp_recvspace = 187 * (1024 + | |
1c79356b | 1877 | #if INET6 |
39236c6e A |
1878 | sizeof (struct sockaddr_in6) |
1879 | #else /* !INET6 */ | |
1880 | sizeof (struct sockaddr_in) | |
1881 | #endif /* !INET6 */ | |
1882 | ); | |
b0d623f7 A |
1883 | |
1884 | /* Check that the values of udp send and recv space do not exceed sb_max */ | |
1885 | static int | |
39236c6e A |
1886 | sysctl_udp_sospace(struct sysctl_oid *oidp, void *arg1, int arg2, |
1887 | struct sysctl_req *req) | |
1888 | { | |
1889 | #pragma unused(arg1, arg2) | |
b0d623f7 A |
1890 | u_int32_t new_value = 0, *space_p = NULL; |
1891 | int changed = 0, error = 0; | |
39236c6e | 1892 | u_quad_t sb_effective_max = (sb_max/(MSIZE+MCLBYTES)) * MCLBYTES; |
b0d623f7 A |
1893 | |
1894 | switch (oidp->oid_number) { | |
39236c6e A |
1895 | case UDPCTL_RECVSPACE: |
1896 | space_p = &udp_recvspace; | |
1897 | break; | |
1898 | case UDPCTL_MAXDGRAM: | |
1899 | space_p = &udp_sendspace; | |
1900 | break; | |
1901 | default: | |
39037602 | 1902 | return (EINVAL); |
b0d623f7 | 1903 | } |
39037602 | 1904 | error = sysctl_io_number(req, *space_p, sizeof (u_int32_t), |
39236c6e | 1905 | &new_value, &changed); |
39037602 A |
1906 | if (changed) { |
1907 | if (new_value > 0 && new_value <= sb_effective_max) | |
1908 | *space_p = new_value; | |
1909 | else | |
1910 | error = ERANGE; | |
1911 | } | |
1912 | return (error); | |
b0d623f7 A |
1913 | } |
1914 | ||
39236c6e | 1915 | SYSCTL_PROC(_net_inet_udp, UDPCTL_RECVSPACE, recvspace, |
39037602 A |
1916 | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED, &udp_recvspace, 0, |
1917 | &sysctl_udp_sospace, "IU", "Maximum incoming UDP datagram size"); | |
b0d623f7 | 1918 | |
39236c6e | 1919 | SYSCTL_PROC(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram, |
39037602 A |
1920 | CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED, &udp_sendspace, 0, |
1921 | &sysctl_udp_sospace, "IU", "Maximum outgoing UDP datagram size"); | |
1c79356b | 1922 | |
5ba3f43e | 1923 | int |
1c79356b A |
1924 | udp_abort(struct socket *so) |
1925 | { | |
1926 | struct inpcb *inp; | |
1c79356b A |
1927 | |
1928 | inp = sotoinpcb(so); | |
39236c6e A |
1929 | if (inp == NULL) { |
1930 | panic("%s: so=%p null inp\n", __func__, so); | |
1931 | /* NOTREACHED */ | |
1932 | } | |
1c79356b | 1933 | soisdisconnected(so); |
1c79356b | 1934 | in_pcbdetach(inp); |
39236c6e | 1935 | return (0); |
1c79356b A |
1936 | } |
1937 | ||
5ba3f43e | 1938 | int |
39236c6e | 1939 | udp_attach(struct socket *so, int proto, struct proc *p) |
1c79356b | 1940 | { |
39236c6e | 1941 | #pragma unused(proto) |
1c79356b | 1942 | struct inpcb *inp; |
91447636 | 1943 | int error; |
1c79356b | 1944 | |
9bccf70c | 1945 | inp = sotoinpcb(so); |
39236c6e | 1946 | if (inp != NULL) { |
39037602 | 1947 | panic("%s so=%p inp=%p\n", __func__, so, inp); |
39236c6e A |
1948 | /* NOTREACHED */ |
1949 | } | |
1c79356b | 1950 | error = in_pcballoc(so, &udbinfo, p); |
39236c6e A |
1951 | if (error != 0) |
1952 | return (error); | |
91447636 | 1953 | error = soreserve(so, udp_sendspace, udp_recvspace); |
39236c6e A |
1954 | if (error != 0) |
1955 | return (error); | |
1c79356b A |
1956 | inp = (struct inpcb *)so->so_pcb; |
1957 | inp->inp_vflag |= INP_IPV4; | |
1958 | inp->inp_ip_ttl = ip_defttl; | |
316670eb A |
1959 | if (nstat_collect) |
1960 | nstat_udp_new_pcb(inp); | |
39236c6e | 1961 | return (0); |
1c79356b A |
1962 | } |
1963 | ||
5ba3f43e | 1964 | int |
1c79356b A |
1965 | udp_bind(struct socket *so, struct sockaddr *nam, struct proc *p) |
1966 | { | |
1967 | struct inpcb *inp; | |
91447636 | 1968 | int error; |
1c79356b | 1969 | |
39236c6e A |
1970 | if (nam->sa_family != 0 && nam->sa_family != AF_INET && |
1971 | nam->sa_family != AF_INET6) | |
1972 | return (EAFNOSUPPORT); | |
1973 | ||
1c79356b | 1974 | inp = sotoinpcb(so); |
3e170ce0 A |
1975 | if (inp == NULL) |
1976 | return (EINVAL); | |
1c79356b | 1977 | error = in_pcbbind(inp, nam, p); |
5ba3f43e A |
1978 | |
1979 | #if NECP | |
1980 | /* Update NECP client with bind result if not in middle of connect */ | |
1981 | if (error == 0 && | |
1982 | (inp->inp_flags2 & INP2_CONNECT_IN_PROGRESS) && | |
1983 | !uuid_is_null(inp->necp_client_uuid)) { | |
1984 | socket_unlock(so, 0); | |
1985 | necp_client_assign_from_socket(so->last_pid, inp->necp_client_uuid, inp); | |
1986 | socket_lock(so, 0); | |
1987 | } | |
1988 | #endif /* NECP */ | |
1989 | ||
39236c6e | 1990 | return (error); |
1c79356b A |
1991 | } |
1992 | ||
5ba3f43e | 1993 | int |
1c79356b A |
1994 | udp_connect(struct socket *so, struct sockaddr *nam, struct proc *p) |
1995 | { | |
1996 | struct inpcb *inp; | |
91447636 | 1997 | int error; |
1c79356b A |
1998 | |
1999 | inp = sotoinpcb(so); | |
3e170ce0 A |
2000 | if (inp == NULL) |
2001 | return (EINVAL); | |
1c79356b | 2002 | if (inp->inp_faddr.s_addr != INADDR_ANY) |
39236c6e | 2003 | return (EISCONN); |
3e170ce0 | 2004 | |
5ba3f43e A |
2005 | if (!(so->so_flags1 & SOF1_CONNECT_COUNTED)) { |
2006 | so->so_flags1 |= SOF1_CONNECT_COUNTED; | |
2007 | INC_ATOMIC_INT64_LIM(net_api_stats.nas_socket_inet_dgram_connected); | |
2008 | } | |
2009 | ||
3e170ce0 A |
2010 | #if NECP |
2011 | #if FLOW_DIVERT | |
2012 | if (necp_socket_should_use_flow_divert(inp)) { | |
2013 | uint32_t fd_ctl_unit = | |
2014 | necp_socket_get_flow_divert_control_unit(inp); | |
2015 | if (fd_ctl_unit > 0) { | |
2016 | error = flow_divert_pcb_init(so, fd_ctl_unit); | |
2017 | if (error == 0) { | |
2018 | error = flow_divert_connect_out(so, nam, p); | |
2019 | } | |
2020 | } else { | |
2021 | error = ENETDOWN; | |
2022 | } | |
2023 | return (error); | |
2024 | } | |
2025 | #endif /* FLOW_DIVERT */ | |
2026 | #endif /* NECP */ | |
2027 | ||
39236c6e | 2028 | error = in_pcbconnect(inp, nam, p, IFSCOPE_NONE, NULL); |
316670eb | 2029 | if (error == 0) { |
5ba3f43e A |
2030 | #if NECP |
2031 | /* Update NECP client with connected five-tuple */ | |
2032 | if (!uuid_is_null(inp->necp_client_uuid)) { | |
2033 | socket_unlock(so, 0); | |
2034 | necp_client_assign_from_socket(so->last_pid, inp->necp_client_uuid, inp); | |
2035 | socket_lock(so, 0); | |
2036 | } | |
2037 | #endif /* NECP */ | |
2038 | ||
1c79356b | 2039 | soisconnected(so); |
316670eb A |
2040 | if (inp->inp_flowhash == 0) |
2041 | inp->inp_flowhash = inp_calc_flowhash(inp); | |
2042 | } | |
39236c6e A |
2043 | return (error); |
2044 | } | |
2045 | ||
2046 | int | |
813fb2f6 | 2047 | udp_connectx_common(struct socket *so, int af, struct sockaddr *src, struct sockaddr *dst, |
3e170ce0 A |
2048 | struct proc *p, uint32_t ifscope, sae_associd_t aid, sae_connid_t *pcid, |
2049 | uint32_t flags, void *arg, uint32_t arglen, | |
2050 | struct uio *uio, user_ssize_t *bytes_written) | |
39236c6e A |
2051 | { |
2052 | #pragma unused(aid, flags, arg, arglen) | |
39236c6e | 2053 | struct inpcb *inp = sotoinpcb(so); |
5ba3f43e | 2054 | int error = 0; |
3e170ce0 | 2055 | user_ssize_t datalen = 0; |
39236c6e A |
2056 | |
2057 | if (inp == NULL) | |
2058 | return (EINVAL); | |
2059 | ||
813fb2f6 | 2060 | VERIFY(dst != NULL); |
39236c6e | 2061 | |
5ba3f43e A |
2062 | ASSERT(!(inp->inp_flags2 & INP2_CONNECT_IN_PROGRESS)); |
2063 | inp->inp_flags2 |= INP2_CONNECT_IN_PROGRESS; | |
2064 | ||
fe8ab488 | 2065 | #if NECP |
813fb2f6 | 2066 | inp_update_necp_policy(inp, src, dst, ifscope); |
fe8ab488 | 2067 | #endif /* NECP */ |
39037602 | 2068 | |
39236c6e A |
2069 | /* bind socket to the specified interface, if requested */ |
2070 | if (ifscope != IFSCOPE_NONE && | |
5ba3f43e A |
2071 | (error = inp_bindif(inp, ifscope, NULL)) != 0) { |
2072 | goto done; | |
2073 | } | |
39236c6e A |
2074 | |
2075 | /* if source address and/or port is specified, bind to it */ | |
813fb2f6 A |
2076 | if (src != NULL) { |
2077 | error = sobindlock(so, src, 0); /* already locked */ | |
5ba3f43e A |
2078 | if (error != 0) { |
2079 | goto done; | |
2080 | } | |
39236c6e A |
2081 | } |
2082 | ||
2083 | switch (af) { | |
2084 | case AF_INET: | |
813fb2f6 | 2085 | error = udp_connect(so, dst, p); |
39236c6e A |
2086 | break; |
2087 | #if INET6 | |
2088 | case AF_INET6: | |
813fb2f6 | 2089 | error = udp6_connect(so, dst, p); |
39236c6e A |
2090 | break; |
2091 | #endif /* INET6 */ | |
2092 | default: | |
2093 | VERIFY(0); | |
2094 | /* NOTREACHED */ | |
2095 | } | |
2096 | ||
5ba3f43e A |
2097 | if (error != 0) { |
2098 | goto done; | |
2099 | } | |
3e170ce0 A |
2100 | |
2101 | /* | |
2102 | * If there is data, copy it. DATA_IDEMPOTENT is ignored. | |
39037602 | 2103 | * CONNECT_RESUME_ON_READ_WRITE is ignored. |
3e170ce0 A |
2104 | */ |
2105 | if (uio != NULL) { | |
2106 | socket_unlock(so, 0); | |
2107 | ||
2108 | VERIFY(bytes_written != NULL); | |
2109 | ||
2110 | datalen = uio_resid(uio); | |
2111 | error = so->so_proto->pr_usrreqs->pru_sosend(so, NULL, | |
39037602 | 2112 | (uio_t)uio, NULL, NULL, 0); |
3e170ce0 A |
2113 | socket_lock(so, 0); |
2114 | ||
2115 | /* If error returned is EMSGSIZE, for example, disconnect */ | |
2116 | if (error == 0 || error == EWOULDBLOCK) | |
2117 | *bytes_written = datalen - uio_resid(uio); | |
2118 | else | |
39037602 | 2119 | (void) so->so_proto->pr_usrreqs->pru_disconnectx(so, |
3e170ce0 A |
2120 | SAE_ASSOCID_ANY, SAE_CONNID_ANY); |
2121 | /* | |
2122 | * mask the EWOULDBLOCK error so that the caller | |
2123 | * knows that atleast the connect was successful. | |
2124 | */ | |
2125 | if (error == EWOULDBLOCK) | |
2126 | error = 0; | |
2127 | } | |
2128 | ||
39236c6e | 2129 | if (error == 0 && pcid != NULL) |
3e170ce0 | 2130 | *pcid = 1; /* there is only 1 connection for UDP */ |
39236c6e | 2131 | |
5ba3f43e A |
2132 | done: |
2133 | inp->inp_flags2 &= ~INP2_CONNECT_IN_PROGRESS; | |
39236c6e A |
2134 | return (error); |
2135 | } | |
2136 | ||
5ba3f43e | 2137 | int |
813fb2f6 A |
2138 | udp_connectx(struct socket *so, struct sockaddr *src, |
2139 | struct sockaddr *dst, struct proc *p, uint32_t ifscope, | |
3e170ce0 A |
2140 | sae_associd_t aid, sae_connid_t *pcid, uint32_t flags, void *arg, |
2141 | uint32_t arglen, struct uio *uio, user_ssize_t *bytes_written) | |
39236c6e | 2142 | { |
813fb2f6 | 2143 | return (udp_connectx_common(so, AF_INET, src, dst, |
3e170ce0 | 2144 | p, ifscope, aid, pcid, flags, arg, arglen, uio, bytes_written)); |
1c79356b A |
2145 | } |
2146 | ||
5ba3f43e | 2147 | int |
1c79356b A |
2148 | udp_detach(struct socket *so) |
2149 | { | |
2150 | struct inpcb *inp; | |
1c79356b A |
2151 | |
2152 | inp = sotoinpcb(so); | |
39236c6e A |
2153 | if (inp == NULL) { |
2154 | panic("%s: so=%p null inp\n", __func__, so); | |
2155 | /* NOTREACHED */ | |
2156 | } | |
fe8ab488 A |
2157 | |
2158 | /* | |
2159 | * If this is a socket that does not want to wakeup the device | |
39037602 A |
2160 | * for it's traffic, the application might be waiting for |
2161 | * close to complete before going to sleep. Send a notification | |
fe8ab488 A |
2162 | * for this kind of sockets |
2163 | */ | |
2164 | if (so->so_options & SO_NOWAKEFROMSLEEP) | |
2165 | socket_post_kev_msg_closed(so); | |
2166 | ||
1c79356b | 2167 | in_pcbdetach(inp); |
91447636 | 2168 | inp->inp_state = INPCB_STATE_DEAD; |
39236c6e | 2169 | return (0); |
1c79356b A |
2170 | } |
2171 | ||
5ba3f43e | 2172 | int |
1c79356b A |
2173 | udp_disconnect(struct socket *so) |
2174 | { | |
2175 | struct inpcb *inp; | |
1c79356b A |
2176 | |
2177 | inp = sotoinpcb(so); | |
fe8ab488 A |
2178 | if (inp == NULL |
2179 | #if NECP | |
2180 | || (necp_socket_should_use_flow_divert(inp)) | |
2181 | #endif /* NECP */ | |
2182 | ) | |
39236c6e | 2183 | return (inp == NULL ? EINVAL : EPROTOTYPE); |
1c79356b | 2184 | if (inp->inp_faddr.s_addr == INADDR_ANY) |
39236c6e | 2185 | return (ENOTCONN); |
1c79356b | 2186 | |
1c79356b | 2187 | in_pcbdisconnect(inp); |
39236c6e | 2188 | |
316670eb A |
2189 | /* reset flow controlled state, just in case */ |
2190 | inp_reset_fc_state(inp); | |
2191 | ||
1c79356b | 2192 | inp->inp_laddr.s_addr = INADDR_ANY; |
1c79356b | 2193 | so->so_state &= ~SS_ISCONNECTED; /* XXX */ |
316670eb | 2194 | inp->inp_last_outifp = NULL; |
5ba3f43e | 2195 | |
39236c6e | 2196 | return (0); |
1c79356b A |
2197 | } |
2198 | ||
5ba3f43e | 2199 | int |
3e170ce0 | 2200 | udp_disconnectx(struct socket *so, sae_associd_t aid, sae_connid_t cid) |
1c79356b | 2201 | { |
39236c6e | 2202 | #pragma unused(cid) |
3e170ce0 | 2203 | if (aid != SAE_ASSOCID_ANY && aid != SAE_ASSOCID_ALL) |
39236c6e A |
2204 | return (EINVAL); |
2205 | ||
2206 | return (udp_disconnect(so)); | |
2207 | } | |
2208 | ||
5ba3f43e | 2209 | int |
39236c6e A |
2210 | udp_send(struct socket *so, int flags, struct mbuf *m, |
2211 | struct sockaddr *addr, struct mbuf *control, struct proc *p) | |
2212 | { | |
3e170ce0 | 2213 | #ifndef FLOW_DIVERT |
39236c6e | 2214 | #pragma unused(flags) |
3e170ce0 | 2215 | #endif /* !(FLOW_DIVERT) */ |
1c79356b A |
2216 | struct inpcb *inp; |
2217 | ||
2218 | inp = sotoinpcb(so); | |
3e170ce0 | 2219 | if (inp == NULL) { |
39236c6e A |
2220 | if (m != NULL) |
2221 | m_freem(m); | |
2222 | if (control != NULL) | |
2223 | m_freem(control); | |
3e170ce0 | 2224 | return (EINVAL); |
1c79356b | 2225 | } |
39236c6e | 2226 | |
3e170ce0 A |
2227 | #if NECP |
2228 | #if FLOW_DIVERT | |
2229 | if (necp_socket_should_use_flow_divert(inp)) { | |
2230 | /* Implicit connect */ | |
39037602 A |
2231 | return (flow_divert_implicit_data_out(so, flags, m, addr, |
2232 | control, p)); | |
3e170ce0 A |
2233 | } |
2234 | #endif /* FLOW_DIVERT */ | |
2235 | #endif /* NECP */ | |
2236 | ||
39236c6e | 2237 | return (udp_output(inp, m, addr, control, p)); |
1c79356b A |
2238 | } |
2239 | ||
2240 | int | |
2241 | udp_shutdown(struct socket *so) | |
2242 | { | |
2243 | struct inpcb *inp; | |
2244 | ||
2245 | inp = sotoinpcb(so); | |
39236c6e A |
2246 | if (inp == NULL) |
2247 | return (EINVAL); | |
1c79356b | 2248 | socantsendmore(so); |
39236c6e | 2249 | return (0); |
1c79356b A |
2250 | } |
2251 | ||
91447636 | 2252 | int |
b0d623f7 | 2253 | udp_lock(struct socket *so, int refcount, void *debug) |
91447636 | 2254 | { |
b0d623f7 A |
2255 | void *lr_saved; |
2256 | ||
2257 | if (debug == NULL) | |
2258 | lr_saved = __builtin_return_address(0); | |
2259 | else | |
2260 | lr_saved = debug; | |
91447636 | 2261 | |
39236c6e | 2262 | if (so->so_pcb != NULL) { |
5ba3f43e | 2263 | LCK_MTX_ASSERT(&((struct inpcb *)so->so_pcb)->inpcb_mtx, |
b0d623f7 | 2264 | LCK_MTX_ASSERT_NOTOWNED); |
6d2010ae | 2265 | lck_mtx_lock(&((struct inpcb *)so->so_pcb)->inpcb_mtx); |
b0d623f7 | 2266 | } else { |
39236c6e | 2267 | panic("%s: so=%p NO PCB! lr=%p lrh= %s\n", __func__, |
b0d623f7 A |
2268 | so, lr_saved, solockhistory_nr(so)); |
2269 | /* NOTREACHED */ | |
91447636 | 2270 | } |
b0d623f7 | 2271 | if (refcount) |
91447636 A |
2272 | so->so_usecount++; |
2273 | ||
b0d623f7 | 2274 | so->lock_lr[so->next_lock_lr] = lr_saved; |
0c530ab8 | 2275 | so->next_lock_lr = (so->next_lock_lr+1) % SO_LCKDBG_MAX; |
91447636 A |
2276 | return (0); |
2277 | } | |
2278 | ||
2279 | int | |
b0d623f7 | 2280 | udp_unlock(struct socket *so, int refcount, void *debug) |
91447636 | 2281 | { |
b0d623f7 | 2282 | void *lr_saved; |
0c530ab8 | 2283 | |
b0d623f7 A |
2284 | if (debug == NULL) |
2285 | lr_saved = __builtin_return_address(0); | |
2286 | else | |
2287 | lr_saved = debug; | |
0c530ab8 | 2288 | |
d190cdc3 A |
2289 | if (refcount) { |
2290 | VERIFY(so->so_usecount > 0); | |
91447636 | 2291 | so->so_usecount--; |
d190cdc3 | 2292 | } |
b0d623f7 | 2293 | if (so->so_pcb == NULL) { |
39236c6e | 2294 | panic("%s: so=%p NO PCB! lr=%p lrh= %s\n", __func__, |
b0d623f7 A |
2295 | so, lr_saved, solockhistory_nr(so)); |
2296 | /* NOTREACHED */ | |
2297 | } else { | |
5ba3f43e | 2298 | LCK_MTX_ASSERT(&((struct inpcb *)so->so_pcb)->inpcb_mtx, |
b0d623f7 A |
2299 | LCK_MTX_ASSERT_OWNED); |
2300 | so->unlock_lr[so->next_unlock_lr] = lr_saved; | |
0c530ab8 | 2301 | so->next_unlock_lr = (so->next_unlock_lr+1) % SO_LCKDBG_MAX; |
6d2010ae | 2302 | lck_mtx_unlock(&((struct inpcb *)so->so_pcb)->inpcb_mtx); |
91447636 | 2303 | } |
91447636 A |
2304 | return (0); |
2305 | } | |
2306 | ||
2307 | lck_mtx_t * | |
5ba3f43e | 2308 | udp_getlock(struct socket *so, int flags) |
91447636 | 2309 | { |
5ba3f43e | 2310 | #pragma unused(flags) |
91447636 A |
2311 | struct inpcb *inp = sotoinpcb(so); |
2312 | ||
39236c6e A |
2313 | if (so->so_pcb == NULL) { |
2314 | panic("%s: so=%p NULL so_pcb lrh= %s\n", __func__, | |
2315 | so, solockhistory_nr(so)); | |
2316 | /* NOTREACHED */ | |
91447636 | 2317 | } |
39236c6e | 2318 | return (&inp->inpcb_mtx); |
91447636 A |
2319 | } |
2320 | ||
39236c6e A |
2321 | /* |
2322 | * UDP garbage collector callback (inpcb_timer_func_t). | |
2323 | * | |
2324 | * Returns > 0 to keep timer active. | |
2325 | */ | |
2326 | static void | |
2327 | udp_gc(struct inpcbinfo *ipi) | |
91447636 A |
2328 | { |
2329 | struct inpcb *inp, *inpnxt; | |
2330 | struct socket *so; | |
91447636 | 2331 | |
39236c6e | 2332 | if (lck_rw_try_lock_exclusive(ipi->ipi_lock) == FALSE) { |
2d21ac55 A |
2333 | if (udp_gc_done == TRUE) { |
2334 | udp_gc_done = FALSE; | |
39236c6e A |
2335 | /* couldn't get the lock, must lock next time */ |
2336 | atomic_add_32(&ipi->ipi_gc_req.intimer_fast, 1); | |
2337 | return; | |
2d21ac55 | 2338 | } |
39236c6e | 2339 | lck_rw_lock_exclusive(ipi->ipi_lock); |
2d21ac55 A |
2340 | } |
2341 | ||
2342 | udp_gc_done = TRUE; | |
91447636 A |
2343 | |
2344 | for (inp = udb.lh_first; inp != NULL; inp = inpnxt) { | |
2345 | inpnxt = inp->inp_list.le_next; | |
91447636 | 2346 | |
39236c6e A |
2347 | /* |
2348 | * Skip unless it's STOPUSING; garbage collector will | |
2349 | * be triggered by in_pcb_checkstate() upon setting | |
2350 | * wantcnt to that value. If the PCB is already dead, | |
2351 | * keep gc active to anticipate wantcnt changing. | |
2352 | */ | |
2353 | if (inp->inp_wantcnt != WNT_STOPUSING) | |
91447636 A |
2354 | continue; |
2355 | ||
39236c6e A |
2356 | /* |
2357 | * Skip if busy, no hurry for cleanup. Keep gc active | |
2358 | * and try the lock again during next round. | |
2359 | */ | |
5ba3f43e | 2360 | if (!socket_try_lock(inp->inp_socket)) { |
39236c6e | 2361 | atomic_add_32(&ipi->ipi_gc_req.intimer_fast, 1); |
91447636 | 2362 | continue; |
39236c6e | 2363 | } |
91447636 | 2364 | |
39236c6e A |
2365 | /* |
2366 | * Keep gc active unless usecount is 0. | |
2367 | */ | |
2368 | so = inp->inp_socket; | |
b0d623f7 A |
2369 | if (so->so_usecount == 0) { |
2370 | if (inp->inp_state != INPCB_STATE_DEAD) { | |
2371 | #if INET6 | |
39236c6e | 2372 | if (SOCK_CHECK_DOM(so, PF_INET6)) |
b0d623f7 A |
2373 | in6_pcbdetach(inp); |
2374 | else | |
2375 | #endif /* INET6 */ | |
39236c6e | 2376 | in_pcbdetach(inp); |
b0d623f7 | 2377 | } |
91447636 | 2378 | in_pcbdispose(inp); |
b0d623f7 | 2379 | } else { |
5ba3f43e | 2380 | socket_unlock(so, 0); |
39236c6e | 2381 | atomic_add_32(&ipi->ipi_gc_req.intimer_fast, 1); |
b0d623f7 | 2382 | } |
91447636 | 2383 | } |
39236c6e | 2384 | lck_rw_done(ipi->ipi_lock); |
91447636 A |
2385 | } |
2386 | ||
39236c6e A |
2387 | static int |
2388 | udp_getstat SYSCTL_HANDLER_ARGS | |
91447636 | 2389 | { |
39236c6e A |
2390 | #pragma unused(oidp, arg1, arg2) |
2391 | if (req->oldptr == USER_ADDR_NULL) | |
2392 | req->oldlen = (size_t)sizeof (struct udpstat); | |
2393 | ||
2394 | return (SYSCTL_OUT(req, &udpstat, MIN(sizeof (udpstat), req->oldlen))); | |
91447636 A |
2395 | } |
2396 | ||
2d21ac55 A |
2397 | void |
2398 | udp_in_cksum_stats(u_int32_t len) | |
2399 | { | |
39236c6e A |
2400 | udpstat.udps_rcv_swcsum++; |
2401 | udpstat.udps_rcv_swcsum_bytes += len; | |
2d21ac55 A |
2402 | } |
2403 | ||
2404 | void | |
2405 | udp_out_cksum_stats(u_int32_t len) | |
2406 | { | |
39236c6e A |
2407 | udpstat.udps_snd_swcsum++; |
2408 | udpstat.udps_snd_swcsum_bytes += len; | |
2409 | } | |
2410 | ||
2411 | #if INET6 | |
2412 | void | |
2413 | udp_in6_cksum_stats(u_int32_t len) | |
2414 | { | |
2415 | udpstat.udps_rcv6_swcsum++; | |
2416 | udpstat.udps_rcv6_swcsum_bytes += len; | |
2417 | } | |
2418 | ||
2419 | void | |
2420 | udp_out6_cksum_stats(u_int32_t len) | |
2421 | { | |
2422 | udpstat.udps_snd6_swcsum++; | |
2423 | udpstat.udps_snd6_swcsum_bytes += len; | |
2424 | } | |
2425 | #endif /* INET6 */ | |
2426 | ||
2427 | /* | |
2428 | * Checksum extended UDP header and data. | |
2429 | */ | |
2430 | static int | |
2431 | udp_input_checksum(struct mbuf *m, struct udphdr *uh, int off, int ulen) | |
2432 | { | |
2433 | struct ifnet *ifp = m->m_pkthdr.rcvif; | |
2434 | struct ip *ip = mtod(m, struct ip *); | |
2435 | struct ipovly *ipov = (struct ipovly *)ip; | |
2436 | ||
2437 | if (uh->uh_sum == 0) { | |
2438 | udpstat.udps_nosum++; | |
2439 | return (0); | |
2440 | } | |
2441 | ||
5ba3f43e A |
2442 | /* ip_stripoptions() must have been called before we get here */ |
2443 | ASSERT((ip->ip_hl << 2) == sizeof (*ip)); | |
2444 | ||
39236c6e A |
2445 | if ((hwcksum_rx || (ifp->if_flags & IFF_LOOPBACK) || |
2446 | (m->m_pkthdr.pkt_flags & PKTF_LOOP)) && | |
2447 | (m->m_pkthdr.csum_flags & CSUM_DATA_VALID)) { | |
2448 | if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) { | |
2449 | uh->uh_sum = m->m_pkthdr.csum_rx_val; | |
2450 | } else { | |
5ba3f43e A |
2451 | uint32_t sum = m->m_pkthdr.csum_rx_val; |
2452 | uint32_t start = m->m_pkthdr.csum_rx_start; | |
2453 | int32_t trailer = (m_pktlen(m) - (off + ulen)); | |
39236c6e A |
2454 | |
2455 | /* | |
2456 | * Perform 1's complement adjustment of octets | |
2457 | * that got included/excluded in the hardware- | |
2458 | * calculated checksum value. Ignore cases | |
5ba3f43e | 2459 | * where the value already includes the entire |
39236c6e | 2460 | * IP header span, as the sum for those octets |
5ba3f43e A |
2461 | * would already be 0 by the time we get here; |
2462 | * IP has already performed its header checksum | |
2463 | * checks. If we do need to adjust, restore | |
2464 | * the original fields in the IP header when | |
2465 | * computing the adjustment value. Also take | |
2466 | * care of any trailing bytes and subtract out | |
2467 | * their partial sum. | |
39236c6e | 2468 | */ |
5ba3f43e | 2469 | ASSERT(trailer >= 0); |
39236c6e | 2470 | if ((m->m_pkthdr.csum_flags & CSUM_PARTIAL) && |
5ba3f43e A |
2471 | ((start != 0 && start != off) || trailer != 0)) { |
2472 | uint32_t swbytes = (uint32_t)trailer; | |
2473 | ||
39236c6e | 2474 | if (start < off) { |
5ba3f43e A |
2475 | ip->ip_len += sizeof (*ip); |
2476 | #if BYTE_ORDER != BIG_ENDIAN | |
39236c6e A |
2477 | HTONS(ip->ip_len); |
2478 | HTONS(ip->ip_off); | |
39236c6e | 2479 | #endif /* BYTE_ORDER != BIG_ENDIAN */ |
5ba3f43e | 2480 | } |
39236c6e | 2481 | /* callee folds in sum */ |
5ba3f43e A |
2482 | sum = m_adj_sum16(m, start, off, ulen, sum); |
2483 | if (off > start) | |
2484 | swbytes += (off - start); | |
2485 | else | |
2486 | swbytes += (start - off); | |
2487 | ||
39236c6e | 2488 | if (start < off) { |
5ba3f43e | 2489 | #if BYTE_ORDER != BIG_ENDIAN |
39236c6e A |
2490 | NTOHS(ip->ip_off); |
2491 | NTOHS(ip->ip_len); | |
39236c6e | 2492 | #endif /* BYTE_ORDER != BIG_ENDIAN */ |
5ba3f43e A |
2493 | ip->ip_len -= sizeof (*ip); |
2494 | } | |
2495 | ||
2496 | if (swbytes != 0) | |
2497 | udp_in_cksum_stats(swbytes); | |
2498 | if (trailer != 0) | |
2499 | m_adj(m, -trailer); | |
39236c6e A |
2500 | } |
2501 | ||
2502 | /* callee folds in sum */ | |
2503 | uh->uh_sum = in_pseudo(ip->ip_src.s_addr, | |
2504 | ip->ip_dst.s_addr, sum + htonl(ulen + IPPROTO_UDP)); | |
2505 | } | |
2506 | uh->uh_sum ^= 0xffff; | |
2507 | } else { | |
2508 | uint16_t ip_sum; | |
2509 | char b[9]; | |
2510 | ||
2511 | bcopy(ipov->ih_x1, b, sizeof (ipov->ih_x1)); | |
2512 | bzero(ipov->ih_x1, sizeof (ipov->ih_x1)); | |
2513 | ip_sum = ipov->ih_len; | |
2514 | ipov->ih_len = uh->uh_ulen; | |
2515 | uh->uh_sum = in_cksum(m, ulen + sizeof (struct ip)); | |
2516 | bcopy(b, ipov->ih_x1, sizeof (ipov->ih_x1)); | |
2517 | ipov->ih_len = ip_sum; | |
2518 | ||
2519 | udp_in_cksum_stats(ulen); | |
2520 | } | |
2521 | ||
2522 | if (uh->uh_sum != 0) { | |
2523 | udpstat.udps_badsum++; | |
2524 | IF_UDP_STATINC(ifp, badchksum); | |
2525 | return (-1); | |
2526 | } | |
2527 | ||
2528 | return (0); | |
2d21ac55 | 2529 | } |
3e170ce0 | 2530 | |
3e170ce0 A |
2531 | void |
2532 | udp_fill_keepalive_offload_frames(ifnet_t ifp, | |
2533 | struct ifnet_keepalive_offload_frame *frames_array, | |
2534 | u_int32_t frames_array_count, size_t frame_data_offset, | |
2535 | u_int32_t *used_frames_count) | |
2536 | { | |
2537 | struct inpcb *inp; | |
2538 | inp_gen_t gencnt; | |
2539 | u_int32_t frame_index = *used_frames_count; | |
2540 | ||
2541 | if (ifp == NULL || frames_array == NULL || | |
2542 | frames_array_count == 0 || | |
2543 | frame_index >= frames_array_count || | |
2544 | frame_data_offset >= IFNET_KEEPALIVE_OFFLOAD_FRAME_DATA_SIZE) | |
2545 | return; | |
2546 | ||
2547 | lck_rw_lock_shared(udbinfo.ipi_lock); | |
2548 | gencnt = udbinfo.ipi_gencnt; | |
2549 | LIST_FOREACH(inp, udbinfo.ipi_listhead, inp_list) { | |
2550 | struct socket *so; | |
2551 | u_int8_t *data; | |
2552 | struct ifnet_keepalive_offload_frame *frame; | |
2553 | struct mbuf *m = NULL; | |
2554 | ||
2555 | if (frame_index >= frames_array_count) | |
2556 | break; | |
2557 | ||
2558 | if (inp->inp_gencnt > gencnt || | |
2559 | inp->inp_state == INPCB_STATE_DEAD) | |
2560 | continue; | |
2561 | ||
2562 | if ((so = inp->inp_socket) == NULL || | |
2563 | (so->so_state & SS_DEFUNCT)) | |
2564 | continue; | |
2565 | /* | |
2566 | * check for keepalive offload flag without socket | |
2567 | * lock to avoid a deadlock | |
2568 | */ | |
2569 | if (!(inp->inp_flags2 & INP2_KEEPALIVE_OFFLOAD)) { | |
2570 | continue; | |
2571 | } | |
2572 | ||
2573 | udp_lock(so, 1, 0); | |
2574 | if (!(inp->inp_vflag & (INP_IPV4 | INP_IPV6))) { | |
2575 | udp_unlock(so, 1, 0); | |
2576 | continue; | |
2577 | } | |
2578 | if ((inp->inp_vflag & INP_IPV4) && | |
2579 | (inp->inp_laddr.s_addr == INADDR_ANY || | |
2580 | inp->inp_faddr.s_addr == INADDR_ANY)) { | |
2581 | udp_unlock(so, 1, 0); | |
2582 | continue; | |
2583 | } | |
2584 | if ((inp->inp_vflag & INP_IPV6) && | |
2585 | (IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr) || | |
2586 | IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_faddr))) { | |
2587 | udp_unlock(so, 1, 0); | |
2588 | continue; | |
2589 | } | |
2590 | if (inp->inp_lport == 0 || inp->inp_fport == 0) { | |
2591 | udp_unlock(so, 1, 0); | |
2592 | continue; | |
2593 | } | |
2594 | if (inp->inp_last_outifp == NULL || | |
2595 | inp->inp_last_outifp->if_index != ifp->if_index) { | |
2596 | udp_unlock(so, 1, 0); | |
2597 | continue; | |
2598 | } | |
2599 | if ((inp->inp_vflag & INP_IPV4)) { | |
39037602 | 2600 | if ((frame_data_offset + sizeof(struct udpiphdr) + |
3e170ce0 A |
2601 | inp->inp_keepalive_datalen) > |
2602 | IFNET_KEEPALIVE_OFFLOAD_FRAME_DATA_SIZE) { | |
2603 | udp_unlock(so, 1, 0); | |
2604 | continue; | |
2605 | } | |
2606 | if ((sizeof(struct udpiphdr) + | |
2607 | inp->inp_keepalive_datalen) > _MHLEN) { | |
2608 | udp_unlock(so, 1, 0); | |
2609 | continue; | |
2610 | } | |
2611 | } else { | |
2612 | if ((frame_data_offset + sizeof(struct ip6_hdr) + | |
2613 | sizeof(struct udphdr) + | |
2614 | inp->inp_keepalive_datalen) > | |
2615 | IFNET_KEEPALIVE_OFFLOAD_FRAME_DATA_SIZE) { | |
2616 | udp_unlock(so, 1, 0); | |
2617 | continue; | |
2618 | } | |
2619 | if ((sizeof(struct ip6_hdr) + sizeof(struct udphdr) + | |
2620 | inp->inp_keepalive_datalen) > _MHLEN) { | |
2621 | udp_unlock(so, 1, 0); | |
2622 | continue; | |
2623 | } | |
2624 | } | |
2625 | MGETHDR(m, M_WAIT, MT_HEADER); | |
2626 | if (m == NULL) { | |
2627 | udp_unlock(so, 1, 0); | |
2628 | continue; | |
2629 | } | |
2630 | /* | |
2631 | * This inp has all the information that is needed to | |
2632 | * generate an offload frame. | |
2633 | */ | |
2634 | if (inp->inp_vflag & INP_IPV4) { | |
2635 | struct ip *ip; | |
2636 | struct udphdr *udp; | |
2637 | ||
2638 | frame = &frames_array[frame_index]; | |
2639 | frame->length = frame_data_offset + | |
2640 | sizeof(struct udpiphdr) + | |
2641 | inp->inp_keepalive_datalen; | |
2642 | frame->ether_type = | |
2643 | IFNET_KEEPALIVE_OFFLOAD_FRAME_ETHERTYPE_IPV4; | |
2644 | frame->interval = inp->inp_keepalive_interval; | |
2645 | switch (inp->inp_keepalive_type) { | |
2646 | case UDP_KEEPALIVE_OFFLOAD_TYPE_AIRPLAY: | |
2647 | frame->type = | |
2648 | IFNET_KEEPALIVE_OFFLOAD_FRAME_AIRPLAY; | |
2649 | break; | |
2650 | default: | |
2651 | break; | |
2652 | } | |
2653 | data = mtod(m, u_int8_t *); | |
2654 | bzero(data, sizeof(struct udpiphdr)); | |
2655 | ip = (__typeof__(ip))(void *)data; | |
2656 | udp = (__typeof__(udp))(void *) (data + | |
2657 | sizeof(struct ip)); | |
2658 | m->m_len = sizeof(struct udpiphdr); | |
2659 | data = data + sizeof(struct udpiphdr); | |
2660 | if (inp->inp_keepalive_datalen > 0 && | |
2661 | inp->inp_keepalive_data != NULL) { | |
2662 | bcopy(inp->inp_keepalive_data, data, | |
2663 | inp->inp_keepalive_datalen); | |
2664 | m->m_len += inp->inp_keepalive_datalen; | |
2665 | } | |
2666 | m->m_pkthdr.len = m->m_len; | |
2667 | ||
2668 | ip->ip_v = IPVERSION; | |
2669 | ip->ip_hl = (sizeof(struct ip) >> 2); | |
2670 | ip->ip_p = IPPROTO_UDP; | |
2671 | ip->ip_len = htons(sizeof(struct udpiphdr) + | |
2672 | (u_short)inp->inp_keepalive_datalen); | |
2673 | ip->ip_ttl = inp->inp_ip_ttl; | |
813fb2f6 | 2674 | ip->ip_tos |= (inp->inp_ip_tos & ~IPTOS_ECN_MASK); |
3e170ce0 A |
2675 | ip->ip_src = inp->inp_laddr; |
2676 | ip->ip_dst = inp->inp_faddr; | |
2677 | ip->ip_sum = in_cksum_hdr_opt(ip); | |
2678 | ||
2679 | udp->uh_sport = inp->inp_lport; | |
2680 | udp->uh_dport = inp->inp_fport; | |
2681 | udp->uh_ulen = htons(sizeof(struct udphdr) + | |
2682 | (u_short)inp->inp_keepalive_datalen); | |
2683 | ||
2684 | if (!(inp->inp_flags & INP_UDP_NOCKSUM)) { | |
2685 | udp->uh_sum = in_pseudo(ip->ip_src.s_addr, | |
2686 | ip->ip_dst.s_addr, | |
2687 | htons(sizeof(struct udphdr) + | |
2688 | (u_short)inp->inp_keepalive_datalen + | |
2689 | IPPROTO_UDP)); | |
5ba3f43e A |
2690 | m->m_pkthdr.csum_flags = |
2691 | (CSUM_UDP|CSUM_ZERO_INVERT); | |
3e170ce0 A |
2692 | m->m_pkthdr.csum_data = offsetof(struct udphdr, |
2693 | uh_sum); | |
2694 | } | |
2695 | m->m_pkthdr.pkt_proto = IPPROTO_UDP; | |
2696 | in_delayed_cksum(m); | |
2697 | bcopy(m->m_data, frame->data + frame_data_offset, | |
2698 | m->m_len); | |
2699 | } else { | |
2700 | struct ip6_hdr *ip6; | |
2701 | struct udphdr *udp6; | |
2702 | ||
2703 | VERIFY(inp->inp_vflag & INP_IPV6); | |
2704 | frame = &frames_array[frame_index]; | |
2705 | frame->length = frame_data_offset + | |
2706 | sizeof(struct ip6_hdr) + | |
2707 | sizeof(struct udphdr) + | |
2708 | inp->inp_keepalive_datalen; | |
2709 | frame->ether_type = | |
2710 | IFNET_KEEPALIVE_OFFLOAD_FRAME_ETHERTYPE_IPV6; | |
2711 | frame->interval = inp->inp_keepalive_interval; | |
2712 | switch (inp->inp_keepalive_type) { | |
2713 | case UDP_KEEPALIVE_OFFLOAD_TYPE_AIRPLAY: | |
2714 | frame->type = | |
2715 | IFNET_KEEPALIVE_OFFLOAD_FRAME_AIRPLAY; | |
2716 | break; | |
2717 | default: | |
2718 | break; | |
2719 | } | |
2720 | data = mtod(m, u_int8_t *); | |
2721 | bzero(data, sizeof(struct ip6_hdr) + sizeof(struct udphdr)); | |
2722 | ip6 = (__typeof__(ip6))(void *)data; | |
2723 | udp6 = (__typeof__(udp6))(void *)(data + | |
2724 | sizeof(struct ip6_hdr)); | |
2725 | m->m_len = sizeof(struct ip6_hdr) + | |
2726 | sizeof(struct udphdr); | |
2727 | data = data + (sizeof(struct ip6_hdr) + | |
2728 | sizeof(struct udphdr)); | |
2729 | if (inp->inp_keepalive_datalen > 0 && | |
2730 | inp->inp_keepalive_data != NULL) { | |
2731 | bcopy(inp->inp_keepalive_data, data, | |
2732 | inp->inp_keepalive_datalen); | |
2733 | m->m_len += inp->inp_keepalive_datalen; | |
2734 | } | |
2735 | m->m_pkthdr.len = m->m_len; | |
2736 | ip6->ip6_flow = inp->inp_flow & IPV6_FLOWINFO_MASK; | |
813fb2f6 | 2737 | ip6->ip6_flow = ip6->ip6_flow & ~IPV6_FLOW_ECN_MASK; |
3e170ce0 A |
2738 | ip6->ip6_vfc &= ~IPV6_VERSION_MASK; |
2739 | ip6->ip6_vfc |= IPV6_VERSION; | |
2740 | ip6->ip6_nxt = IPPROTO_UDP; | |
2741 | ip6->ip6_hlim = ip6_defhlim; | |
2742 | ip6->ip6_plen = htons(sizeof(struct udphdr) + | |
2743 | (u_short)inp->inp_keepalive_datalen); | |
2744 | ip6->ip6_src = inp->in6p_laddr; | |
2745 | if (IN6_IS_SCOPE_EMBED(&ip6->ip6_src)) | |
2746 | ip6->ip6_src.s6_addr16[1] = 0; | |
2747 | ||
2748 | ip6->ip6_dst = inp->in6p_faddr; | |
2749 | if (IN6_IS_SCOPE_EMBED(&ip6->ip6_dst)) | |
2750 | ip6->ip6_dst.s6_addr16[1] = 0; | |
2751 | ||
2752 | udp6->uh_sport = inp->in6p_lport; | |
2753 | udp6->uh_dport = inp->in6p_fport; | |
2754 | udp6->uh_ulen = htons(sizeof(struct udphdr) + | |
2755 | (u_short)inp->inp_keepalive_datalen); | |
2756 | if (!(inp->inp_flags & INP_UDP_NOCKSUM)) { | |
2757 | udp6->uh_sum = in6_pseudo(&ip6->ip6_src, | |
2758 | &ip6->ip6_dst, | |
2759 | htonl(sizeof(struct udphdr) + | |
2760 | (u_short)inp->inp_keepalive_datalen + | |
2761 | IPPROTO_UDP)); | |
5ba3f43e A |
2762 | m->m_pkthdr.csum_flags = |
2763 | (CSUM_UDPIPV6|CSUM_ZERO_INVERT); | |
3e170ce0 A |
2764 | m->m_pkthdr.csum_data = offsetof(struct udphdr, |
2765 | uh_sum); | |
2766 | } | |
2767 | m->m_pkthdr.pkt_proto = IPPROTO_UDP; | |
2768 | in6_delayed_cksum(m); | |
2769 | bcopy(m->m_data, frame->data + frame_data_offset, | |
2770 | m->m_len); | |
2771 | } | |
2772 | if (m != NULL) { | |
2773 | m_freem(m); | |
2774 | m = NULL; | |
2775 | } | |
2776 | frame_index++; | |
2777 | udp_unlock(so, 1, 0); | |
2778 | } | |
2779 | lck_rw_done(udbinfo.ipi_lock); | |
2780 | *used_frames_count = frame_index; | |
2781 | } |