]>
Commit | Line | Data |
---|---|---|
1c79356b | 1 | /* |
2d21ac55 | 2 | * Copyright (c) 2000-2007 Apple Inc. All rights reserved. |
5d5c5d0d | 3 | * |
2d21ac55 | 4 | * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ |
1c79356b | 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. | |
8f6c56a5 | 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. | |
17 | * | |
18 | * The Original Code and all software distributed under the License are | |
19 | * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER | |
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. | |
8f6c56a5 | 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 | |
9bccf70c | 61 | * $FreeBSD: src/sys/netinet/udp_usrreq.c,v 1.64.2.13 2001/08/08 18:59:54 ghelmer Exp $ |
1c79356b A |
62 | */ |
63 | ||
64 | #include <sys/param.h> | |
65 | #include <sys/systm.h> | |
66 | #include <sys/kernel.h> | |
67 | #include <sys/malloc.h> | |
68 | #include <sys/mbuf.h> | |
1c79356b | 69 | #include <sys/domain.h> |
1c79356b A |
70 | #include <sys/protosw.h> |
71 | #include <sys/socket.h> | |
72 | #include <sys/socketvar.h> | |
73 | #include <sys/sysctl.h> | |
74 | #include <sys/syslog.h> | |
75 | ||
2d21ac55 A |
76 | #include <kern/zalloc.h> |
77 | ||
1c79356b | 78 | #include <net/if.h> |
91447636 | 79 | #include <net/if_types.h> |
1c79356b A |
80 | #include <net/route.h> |
81 | ||
82 | #include <netinet/in.h> | |
83 | #include <netinet/in_systm.h> | |
84 | #include <netinet/ip.h> | |
9bccf70c A |
85 | #if INET6 |
86 | #include <netinet/ip6.h> | |
87 | #endif | |
1c79356b A |
88 | #include <netinet/in_pcb.h> |
89 | #include <netinet/in_var.h> | |
90 | #include <netinet/ip_var.h> | |
1c79356b | 91 | #if INET6 |
1c79356b A |
92 | #include <netinet6/ip6_var.h> |
93 | #endif | |
9bccf70c A |
94 | #include <netinet/ip_icmp.h> |
95 | #include <netinet/icmp_var.h> | |
1c79356b A |
96 | #include <netinet/udp.h> |
97 | #include <netinet/udp_var.h> | |
98 | #include <sys/kdebug.h> | |
99 | ||
100 | #if IPSEC | |
101 | #include <netinet6/ipsec.h> | |
2d21ac55 | 102 | #include <netinet6/esp.h> |
9bccf70c | 103 | extern int ipsec_bypass; |
1c79356b A |
104 | #endif /*IPSEC*/ |
105 | ||
106 | ||
107 | #define DBG_LAYER_IN_BEG NETDBG_CODE(DBG_NETUDP, 0) | |
108 | #define DBG_LAYER_IN_END NETDBG_CODE(DBG_NETUDP, 2) | |
109 | #define DBG_LAYER_OUT_BEG NETDBG_CODE(DBG_NETUDP, 1) | |
110 | #define DBG_LAYER_OUT_END NETDBG_CODE(DBG_NETUDP, 3) | |
111 | #define DBG_FNC_UDP_INPUT NETDBG_CODE(DBG_NETUDP, (5 << 8)) | |
112 | #define DBG_FNC_UDP_OUTPUT NETDBG_CODE(DBG_NETUDP, (6 << 8) | 1) | |
113 | ||
1c79356b A |
114 | /* |
115 | * UDP protocol implementation. | |
116 | * Per RFC 768, August, 1980. | |
117 | */ | |
118 | #ifndef COMPAT_42 | |
119 | static int udpcksum = 1; | |
120 | #else | |
121 | static int udpcksum = 0; /* XXX */ | |
122 | #endif | |
123 | SYSCTL_INT(_net_inet_udp, UDPCTL_CHECKSUM, checksum, CTLFLAG_RW, | |
124 | &udpcksum, 0, ""); | |
125 | ||
2d21ac55 A |
126 | static u_int32_t udps_in_sw_cksum; |
127 | SYSCTL_UINT(_net_inet_udp, OID_AUTO, in_sw_cksum, CTLFLAG_RD, | |
128 | &udps_in_sw_cksum, 0, | |
129 | "Number of received packets checksummed in software"); | |
130 | ||
131 | static u_int64_t udps_in_sw_cksum_bytes; | |
132 | SYSCTL_QUAD(_net_inet_udp, OID_AUTO, in_sw_cksum_bytes, CTLFLAG_RD, | |
133 | &udps_in_sw_cksum_bytes, | |
134 | "Amount of received data checksummed in software"); | |
135 | ||
136 | static u_int32_t udps_out_sw_cksum; | |
137 | SYSCTL_UINT(_net_inet_udp, OID_AUTO, out_sw_cksum, CTLFLAG_RD, | |
138 | &udps_out_sw_cksum, 0, | |
139 | "Number of transmitted packets checksummed in software"); | |
140 | ||
141 | static u_int64_t udps_out_sw_cksum_bytes; | |
142 | SYSCTL_QUAD(_net_inet_udp, OID_AUTO, out_sw_cksum_bytes, CTLFLAG_RD, | |
143 | &udps_out_sw_cksum_bytes, | |
144 | "Amount of transmitted data checksummed in software"); | |
145 | ||
9bccf70c | 146 | int log_in_vain = 0; |
1c79356b | 147 | SYSCTL_INT(_net_inet_udp, OID_AUTO, log_in_vain, CTLFLAG_RW, |
9bccf70c A |
148 | &log_in_vain, 0, "Log all incoming UDP packets"); |
149 | ||
150 | static int blackhole = 0; | |
151 | SYSCTL_INT(_net_inet_udp, OID_AUTO, blackhole, CTLFLAG_RW, | |
152 | &blackhole, 0, "Do not send port unreachables for refused connects"); | |
1c79356b A |
153 | |
154 | struct inpcbhead udb; /* from udp_var.h */ | |
155 | #define udb6 udb /* for KAME src sync over BSD*'s */ | |
156 | struct inpcbinfo udbinfo; | |
157 | ||
158 | #ifndef UDBHASHSIZE | |
159 | #define UDBHASHSIZE 16 | |
160 | #endif | |
161 | ||
55e303ae A |
162 | extern int esp_udp_encap_port; |
163 | extern u_long route_generation; | |
0b4e3aa0 | 164 | |
2d21ac55 | 165 | extern void ipfwsyslog( int level, const char *format,...); |
91447636 A |
166 | |
167 | extern int fw_verbose; | |
2d21ac55 | 168 | static int udp_gc_done = FALSE; /* Garbage collection performed last slowtimo */ |
91447636 | 169 | |
2d21ac55 | 170 | #if IPFIREWALL |
91447636 A |
171 | #define log_in_vain_log( a ) { \ |
172 | if ( (log_in_vain == 3 ) && (fw_verbose == 2)) { /* Apple logging, log to ipfw.log */ \ | |
173 | ipfwsyslog a ; \ | |
174 | } \ | |
175 | else log a ; \ | |
176 | } | |
2d21ac55 A |
177 | #else |
178 | #define log_in_vain_log( a ) { log a; } | |
179 | #endif | |
91447636 | 180 | |
1c79356b A |
181 | struct udpstat udpstat; /* from udp_var.h */ |
182 | SYSCTL_STRUCT(_net_inet_udp, UDPCTL_STATS, stats, CTLFLAG_RD, | |
9bccf70c | 183 | &udpstat, udpstat, "UDP statistics (struct udpstat, netinet/udp_var.h)"); |
91447636 A |
184 | SYSCTL_INT(_net_inet_udp, OID_AUTO, pcbcount, CTLFLAG_RD, |
185 | &udbinfo.ipi_count, 0, "Number of active PCBs"); | |
1c79356b | 186 | |
1c79356b A |
187 | #if INET6 |
188 | struct udp_in6 { | |
189 | struct sockaddr_in6 uin6_sin; | |
190 | u_char uin6_init_done : 1; | |
1c79356b A |
191 | }; |
192 | struct udp_ip6 { | |
193 | struct ip6_hdr uip6_ip6; | |
194 | u_char uip6_init_done : 1; | |
2d21ac55 | 195 | }; |
91447636 | 196 | static void ip_2_ip6_hdr(struct ip6_hdr *ip6, struct ip *ip); |
2d21ac55 A |
197 | static void udp_append(struct inpcb *last, struct ip *ip, |
198 | struct mbuf *n, int off, struct sockaddr_in *pudp_in, | |
199 | struct udp_in6 *pudp_in6, struct udp_ip6 *pudp_ip6); | |
200 | #else | |
201 | static void udp_append(struct inpcb *last, struct ip *ip, | |
202 | struct mbuf *n, int off, struct sockaddr_in *pudp_in); | |
1c79356b A |
203 | #endif |
204 | ||
91447636 A |
205 | static int udp_detach(struct socket *so); |
206 | static int udp_output(struct inpcb *, struct mbuf *, struct sockaddr *, | |
207 | struct mbuf *, struct proc *); | |
208 | extern int ChkAddressOK( __uint32_t dstaddr, __uint32_t srcaddr ); | |
1c79356b A |
209 | |
210 | void | |
211 | udp_init() | |
212 | { | |
91447636 A |
213 | vm_size_t str_size; |
214 | struct inpcbinfo *pcbinfo; | |
215 | ||
1c79356b A |
216 | |
217 | LIST_INIT(&udb); | |
218 | udbinfo.listhead = &udb; | |
219 | udbinfo.hashbase = hashinit(UDBHASHSIZE, M_PCB, &udbinfo.hashmask); | |
220 | udbinfo.porthashbase = hashinit(UDBHASHSIZE, M_PCB, | |
221 | &udbinfo.porthashmask); | |
9bccf70c | 222 | #ifdef __APPLE__ |
1c79356b | 223 | str_size = (vm_size_t) sizeof(struct inpcb); |
9bccf70c | 224 | udbinfo.ipi_zone = (void *) zinit(str_size, 80000*str_size, 8192, "udpcb"); |
1c79356b | 225 | |
91447636 A |
226 | pcbinfo = &udbinfo; |
227 | /* | |
228 | * allocate lock group attribute and group for udp pcb mutexes | |
229 | */ | |
230 | pcbinfo->mtx_grp_attr = lck_grp_attr_alloc_init(); | |
91447636 A |
231 | |
232 | pcbinfo->mtx_grp = lck_grp_alloc_init("udppcb", pcbinfo->mtx_grp_attr); | |
233 | ||
234 | pcbinfo->mtx_attr = lck_attr_alloc_init(); | |
91447636 A |
235 | |
236 | if ((pcbinfo->mtx = lck_rw_alloc_init(pcbinfo->mtx_grp, pcbinfo->mtx_attr)) == NULL) | |
237 | return; /* pretty much dead if this fails... */ | |
238 | ||
1c79356b | 239 | in_pcb_nat_init(&udbinfo, AF_INET, IPPROTO_UDP, SOCK_DGRAM); |
9bccf70c A |
240 | #else |
241 | udbinfo.ipi_zone = zinit("udpcb", sizeof(struct inpcb), maxsockets, | |
242 | ZONE_INTERRUPT, 0); | |
243 | #endif | |
1c79356b A |
244 | |
245 | #if 0 | |
9bccf70c | 246 | /* for pcb sharing testing only */ |
1c79356b A |
247 | stat = in_pcb_new_share_client(&udbinfo, &fake_owner); |
248 | kprintf("udp_init in_pcb_new_share_client - stat = %d\n", stat); | |
249 | ||
250 | laddr.s_addr = 0x11646464; | |
251 | faddr.s_addr = 0x11646465; | |
252 | ||
253 | lport = 1500; | |
254 | in_pcb_grab_port(&udbinfo, 0, laddr, &lport, faddr, 1600, 0, fake_owner); | |
255 | kprintf("udp_init in_pcb_grab_port - stat = %d\n", stat); | |
256 | ||
257 | stat = in_pcb_rem_share_client(&udbinfo, fake_owner); | |
258 | kprintf("udp_init in_pcb_rem_share_client - stat = %d\n", stat); | |
259 | ||
260 | stat = in_pcb_new_share_client(&udbinfo, &fake_owner); | |
261 | kprintf("udp_init in_pcb_new_share_client(2) - stat = %d\n", stat); | |
262 | ||
263 | laddr.s_addr = 0x11646464; | |
264 | faddr.s_addr = 0x11646465; | |
265 | ||
266 | lport = 1500; | |
267 | stat = in_pcb_grab_port(&udbinfo, 0, laddr, &lport, faddr, 1600, 0, fake_owner); | |
268 | kprintf("udp_init in_pcb_grab_port(2) - stat = %d\n", stat); | |
269 | #endif | |
270 | } | |
271 | ||
272 | void | |
273 | udp_input(m, iphlen) | |
274 | register struct mbuf *m; | |
275 | int iphlen; | |
276 | { | |
277 | register struct ip *ip; | |
278 | register struct udphdr *uh; | |
279 | register struct inpcb *inp; | |
280 | struct mbuf *opts = 0; | |
1c79356b A |
281 | int len; |
282 | struct ip save_ip; | |
283 | struct sockaddr *append_sa; | |
91447636 | 284 | struct inpcbinfo *pcbinfo = &udbinfo; |
2d21ac55 A |
285 | struct sockaddr_in udp_in = { |
286 | sizeof (udp_in), AF_INET, 0, { 0 }, { 0, 0, 0, 0, 0, 0, 0, 0 } | |
287 | }; | |
288 | #if INET6 | |
289 | struct udp_in6 udp_in6 = { | |
290 | { sizeof (udp_in6.uin6_sin), AF_INET6, 0, 0, | |
291 | IN6ADDR_ANY_INIT, 0 }, | |
292 | 0 | |
293 | }; | |
294 | struct udp_ip6 udp_ip6; | |
295 | #endif /* INET6 */ | |
1c79356b A |
296 | |
297 | udpstat.udps_ipackets++; | |
1c79356b A |
298 | |
299 | KERNEL_DEBUG(DBG_FNC_UDP_INPUT | DBG_FUNC_START, 0,0,0,0,0); | |
9bccf70c A |
300 | if (m->m_pkthdr.csum_flags & CSUM_TCP_SUM16) |
301 | m->m_pkthdr.csum_flags = 0; /* invalidate hwcksum for UDP */ | |
1c79356b A |
302 | |
303 | /* | |
304 | * Strip IP options, if any; should skip this, | |
305 | * make available to user, and use on returned packets, | |
306 | * but we don't yet have a way to check the checksum | |
307 | * with options still present. | |
308 | */ | |
309 | if (iphlen > sizeof (struct ip)) { | |
310 | ip_stripoptions(m, (struct mbuf *)0); | |
311 | iphlen = sizeof(struct ip); | |
312 | } | |
313 | ||
314 | /* | |
315 | * Get IP and UDP header together in first mbuf. | |
316 | */ | |
317 | ip = mtod(m, struct ip *); | |
318 | if (m->m_len < iphlen + sizeof(struct udphdr)) { | |
319 | if ((m = m_pullup(m, iphlen + sizeof(struct udphdr))) == 0) { | |
320 | udpstat.udps_hdrops++; | |
321 | KERNEL_DEBUG(DBG_FNC_UDP_INPUT | DBG_FUNC_END, 0,0,0,0,0); | |
322 | return; | |
323 | } | |
324 | ip = mtod(m, struct ip *); | |
325 | } | |
326 | uh = (struct udphdr *)((caddr_t)ip + iphlen); | |
327 | ||
9bccf70c A |
328 | /* destination port of 0 is illegal, based on RFC768. */ |
329 | if (uh->uh_dport == 0) | |
330 | goto bad; | |
331 | ||
1c79356b A |
332 | KERNEL_DEBUG(DBG_LAYER_IN_BEG, uh->uh_dport, uh->uh_sport, |
333 | ip->ip_src.s_addr, ip->ip_dst.s_addr, uh->uh_ulen); | |
334 | ||
335 | /* | |
336 | * Make mbuf data length reflect UDP length. | |
337 | * If not enough data to reflect UDP length, drop. | |
338 | */ | |
339 | len = ntohs((u_short)uh->uh_ulen); | |
340 | if (ip->ip_len != len) { | |
341 | if (len > ip->ip_len || len < sizeof(struct udphdr)) { | |
342 | udpstat.udps_badlen++; | |
343 | goto bad; | |
344 | } | |
345 | m_adj(m, len - ip->ip_len); | |
346 | /* ip->ip_len = len; */ | |
347 | } | |
348 | /* | |
349 | * Save a copy of the IP header in case we want restore it | |
350 | * for sending an ICMP error message in response. | |
351 | */ | |
352 | save_ip = *ip; | |
353 | ||
354 | /* | |
355 | * Checksum extended UDP header and data. | |
356 | */ | |
357 | if (uh->uh_sum) { | |
ac5ea4a9 A |
358 | if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) { |
359 | if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR) | |
360 | uh->uh_sum = m->m_pkthdr.csum_data; | |
361 | else | |
362 | goto doudpcksum; | |
363 | uh->uh_sum ^= 0xffff; | |
364 | } else { | |
365 | char b[9]; | |
0b4e3aa0 | 366 | doudpcksum: |
ac5ea4a9 A |
367 | *(uint32_t*)&b[0] = *(uint32_t*)&((struct ipovly *)ip)->ih_x1[0]; |
368 | *(uint32_t*)&b[4] = *(uint32_t*)&((struct ipovly *)ip)->ih_x1[4]; | |
369 | *(uint8_t*)&b[8] = *(uint8_t*)&((struct ipovly *)ip)->ih_x1[8]; | |
370 | ||
0b4e3aa0 A |
371 | bzero(((struct ipovly *)ip)->ih_x1, 9); |
372 | ((struct ipovly *)ip)->ih_len = uh->uh_ulen; | |
373 | uh->uh_sum = in_cksum(m, len + sizeof (struct ip)); | |
ac5ea4a9 A |
374 | |
375 | *(uint32_t*)&((struct ipovly *)ip)->ih_x1[0] = *(uint32_t*)&b[0]; | |
376 | *(uint32_t*)&((struct ipovly *)ip)->ih_x1[4] = *(uint32_t*)&b[4]; | |
377 | *(uint8_t*)&((struct ipovly *)ip)->ih_x1[8] = *(uint8_t*)&b[8]; | |
2d21ac55 | 378 | udp_in_cksum_stats(len); |
0b4e3aa0 | 379 | } |
1c79356b A |
380 | if (uh->uh_sum) { |
381 | udpstat.udps_badsum++; | |
382 | m_freem(m); | |
383 | KERNEL_DEBUG(DBG_FNC_UDP_INPUT | DBG_FUNC_END, 0,0,0,0,0); | |
384 | return; | |
385 | } | |
386 | } | |
9bccf70c A |
387 | #ifndef __APPLE__ |
388 | else | |
389 | udpstat.udps_nosum++; | |
390 | #endif | |
1c79356b A |
391 | |
392 | if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) || | |
393 | in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) { | |
2d21ac55 A |
394 | |
395 | int reuse_sock = 0, mcast_delivered = 0; | |
396 | struct mbuf *n = NULL; | |
397 | ||
91447636 | 398 | lck_rw_lock_shared(pcbinfo->mtx); |
1c79356b A |
399 | /* |
400 | * Deliver a multicast or broadcast datagram to *all* sockets | |
401 | * for which the local and remote addresses and ports match | |
402 | * those of the incoming datagram. This allows more than | |
403 | * one process to receive multi/broadcasts on the same port. | |
404 | * (This really ought to be done for unicast datagrams as | |
405 | * well, but that would cause problems with existing | |
406 | * applications that open both address-specific sockets and | |
407 | * a wildcard socket listening to the same port -- they would | |
408 | * end up receiving duplicates of every unicast datagram. | |
409 | * Those applications open the multiple sockets to overcome an | |
410 | * inadequacy of the UDP socket interface, but for backwards | |
411 | * compatibility we avoid the problem here rather than | |
412 | * fixing the interface. Maybe 4.5BSD will remedy this?) | |
413 | */ | |
414 | ||
91447636 | 415 | |
1c79356b A |
416 | /* |
417 | * Construct sockaddr format source address. | |
418 | */ | |
419 | udp_in.sin_port = uh->uh_sport; | |
420 | udp_in.sin_addr = ip->ip_src; | |
421 | /* | |
422 | * Locate pcb(s) for datagram. | |
423 | * (Algorithm copied from raw_intr().) | |
424 | */ | |
1c79356b A |
425 | #if INET6 |
426 | udp_in6.uin6_init_done = udp_ip6.uip6_init_done = 0; | |
427 | #endif | |
428 | LIST_FOREACH(inp, &udb, inp_list) { | |
9bccf70c A |
429 | #ifdef __APPLE__ |
430 | /* Ignore nat/SharedIP dummy pcbs */ | |
431 | if (inp->inp_socket == &udbinfo.nat_dummy_socket) | |
432 | continue; | |
433 | #endif | |
91447636 | 434 | if (inp->inp_socket == NULL) |
1c79356b | 435 | continue; |
91447636 | 436 | if (inp != sotoinpcb(inp->inp_socket)) |
2d21ac55 | 437 | panic("udp_input: bad so back ptr inp=%p\n", inp); |
91447636 A |
438 | #if INET6 |
439 | if ((inp->inp_vflag & INP_IPV4) == 0) | |
440 | continue; | |
1c79356b | 441 | #endif |
2d21ac55 | 442 | |
91447636 A |
443 | if (in_pcb_checkstate(inp, WNT_ACQUIRE, 0) == WNT_STOPUSING) { |
444 | continue; | |
445 | } | |
446 | ||
447 | udp_lock(inp->inp_socket, 1, 0); | |
448 | ||
449 | if (in_pcb_checkstate(inp, WNT_RELEASE, 1) == WNT_STOPUSING) { | |
450 | udp_unlock(inp->inp_socket, 1, 0); | |
1c79356b | 451 | continue; |
91447636 A |
452 | } |
453 | ||
454 | if (inp->inp_lport != uh->uh_dport) { | |
455 | udp_unlock(inp->inp_socket, 1, 0); | |
456 | continue; | |
457 | } | |
1c79356b A |
458 | if (inp->inp_laddr.s_addr != INADDR_ANY) { |
459 | if (inp->inp_laddr.s_addr != | |
91447636 A |
460 | ip->ip_dst.s_addr) { |
461 | udp_unlock(inp->inp_socket, 1, 0); | |
1c79356b | 462 | continue; |
91447636 | 463 | } |
1c79356b A |
464 | } |
465 | if (inp->inp_faddr.s_addr != INADDR_ANY) { | |
466 | if (inp->inp_faddr.s_addr != | |
467 | ip->ip_src.s_addr || | |
91447636 A |
468 | inp->inp_fport != uh->uh_sport) { |
469 | udp_unlock(inp->inp_socket, 1, 0); | |
1c79356b | 470 | continue; |
91447636 | 471 | } |
1c79356b A |
472 | } |
473 | ||
2d21ac55 A |
474 | reuse_sock = inp->inp_socket->so_options& (SO_REUSEPORT|SO_REUSEADDR); |
475 | { | |
1c79356b | 476 | #if IPSEC |
91447636 | 477 | int skipit = 0; |
1c79356b | 478 | /* check AH/ESP integrity. */ |
91447636 | 479 | if (ipsec_bypass == 0) { |
2d21ac55 A |
480 | if (ipsec4_in_reject_so(m, inp->inp_socket)) { |
481 | IPSEC_STAT_INCREMENT(ipsecstat.in_polvio); | |
91447636 A |
482 | /* do not inject data to pcb */ |
483 | skipit = 1; | |
484 | } | |
91447636 | 485 | } |
2d21ac55 | 486 | if (skipit == 0) |
1c79356b | 487 | #endif /*IPSEC*/ |
2d21ac55 A |
488 | { |
489 | if (reuse_sock) | |
490 | n = m_copy(m, 0, M_COPYALL); | |
491 | #if INET6 | |
492 | udp_append(inp, ip, m, | |
493 | iphlen + sizeof(struct udphdr), | |
494 | &udp_in, &udp_in6, &udp_ip6); | |
495 | #else | |
496 | udp_append(inp, ip, m, | |
497 | iphlen + sizeof(struct udphdr), | |
498 | &udp_in); | |
499 | #endif /* INET6 */ | |
500 | mcast_delivered++; | |
1c79356b | 501 | } |
2d21ac55 | 502 | udp_unlock(inp->inp_socket, 1, 0); |
1c79356b | 503 | } |
1c79356b A |
504 | /* |
505 | * Don't look for additional matches if this one does | |
506 | * not have either the SO_REUSEPORT or SO_REUSEADDR | |
507 | * socket options set. This heuristic avoids searching | |
508 | * through all pcbs in the common case of a non-shared | |
2d21ac55 | 509 | * port. It assumes that an application will never |
1c79356b A |
510 | * clear these options after setting them. |
511 | */ | |
2d21ac55 | 512 | if (reuse_sock == 0 || ((m = n) == NULL)) |
1c79356b A |
513 | break; |
514 | } | |
91447636 | 515 | lck_rw_done(pcbinfo->mtx); |
1c79356b | 516 | |
2d21ac55 | 517 | if (mcast_delivered == 0) { |
1c79356b A |
518 | /* |
519 | * No matching pcb found; discard datagram. | |
520 | * (No need to send an ICMP Port Unreachable | |
521 | * for a broadcast or multicast datgram.) | |
522 | */ | |
523 | udpstat.udps_noportbcast++; | |
524 | goto bad; | |
525 | } | |
2d21ac55 A |
526 | |
527 | if (reuse_sock != 0) /* free the extra copy of mbuf */ | |
528 | m_freem(m); | |
1c79356b A |
529 | return; |
530 | } | |
55e303ae | 531 | |
91447636 | 532 | #if IPSEC |
55e303ae A |
533 | /* |
534 | * UDP to port 4500 with a payload where the first four bytes are | |
535 | * not zero is a UDP encapsulated IPSec packet. Packets where | |
536 | * the payload is one byte and that byte is 0xFF are NAT keepalive | |
537 | * packets. Decapsulate the ESP packet and carry on with IPSec input | |
538 | * or discard the NAT keep-alive. | |
539 | */ | |
540 | if (ipsec_bypass == 0 && (esp_udp_encap_port & 0xFFFF) != 0 && | |
541 | uh->uh_dport == ntohs((u_short)esp_udp_encap_port)) { | |
542 | int payload_len = len - sizeof(struct udphdr) > 4 ? 4 : len - sizeof(struct udphdr); | |
543 | if (m->m_len < iphlen + sizeof(struct udphdr) + payload_len) { | |
544 | if ((m = m_pullup(m, iphlen + sizeof(struct udphdr) + payload_len)) == 0) { | |
545 | udpstat.udps_hdrops++; | |
546 | KERNEL_DEBUG(DBG_FNC_UDP_INPUT | DBG_FUNC_END, 0,0,0,0,0); | |
547 | return; | |
548 | } | |
549 | ip = mtod(m, struct ip *); | |
550 | uh = (struct udphdr *)((caddr_t)ip + iphlen); | |
551 | } | |
552 | /* Check for NAT keepalive packet */ | |
553 | if (payload_len == 1 && *(u_int8_t*)((caddr_t)uh + sizeof(struct udphdr)) == 0xFF) { | |
554 | m_freem(m); | |
555 | KERNEL_DEBUG(DBG_FNC_UDP_INPUT | DBG_FUNC_END, 0,0,0,0,0); | |
556 | return; | |
557 | } | |
558 | else if (payload_len == 4 && *(u_int32_t*)((caddr_t)uh + sizeof(struct udphdr)) != 0) { | |
559 | /* UDP encapsulated IPSec packet to pass through NAT */ | |
560 | size_t stripsiz; | |
561 | ||
562 | stripsiz = sizeof(struct udphdr); | |
563 | ||
564 | ip = mtod(m, struct ip *); | |
565 | ovbcopy((caddr_t)ip, (caddr_t)(((u_char *)ip) + stripsiz), iphlen); | |
566 | m->m_data += stripsiz; | |
567 | m->m_len -= stripsiz; | |
568 | m->m_pkthdr.len -= stripsiz; | |
569 | ip = mtod(m, struct ip *); | |
570 | ip->ip_len = ip->ip_len - stripsiz; | |
571 | ip->ip_p = IPPROTO_ESP; | |
572 | ||
573 | KERNEL_DEBUG(DBG_FNC_UDP_INPUT | DBG_FUNC_END, 0,0,0,0,0); | |
574 | esp4_input(m, iphlen); | |
575 | return; | |
576 | } | |
577 | } | |
91447636 | 578 | #endif |
55e303ae | 579 | |
1c79356b A |
580 | /* |
581 | * Locate pcb for datagram. | |
582 | */ | |
583 | inp = in_pcblookup_hash(&udbinfo, ip->ip_src, uh->uh_sport, | |
584 | ip->ip_dst, uh->uh_dport, 1, m->m_pkthdr.rcvif); | |
585 | if (inp == NULL) { | |
586 | if (log_in_vain) { | |
91447636 A |
587 | char buf[MAX_IPv4_STR_LEN]; |
588 | char buf2[MAX_IPv4_STR_LEN]; | |
589 | ||
590 | /* check src and dst address */ | |
591 | if (log_in_vain != 3) | |
592 | log(LOG_INFO, | |
593 | "Connection attempt to UDP %s:%d from %s:%d\n", | |
594 | inet_ntop(AF_INET, &ip->ip_dst, buf, sizeof(buf)), | |
595 | ntohs(uh->uh_dport), | |
596 | inet_ntop(AF_INET, &ip->ip_src, buf2, sizeof(buf2)), | |
597 | ntohs(uh->uh_sport)); | |
598 | else if (!(m->m_flags & (M_BCAST | M_MCAST)) && | |
599 | ip->ip_dst.s_addr != ip->ip_src.s_addr) | |
600 | log_in_vain_log((LOG_INFO, | |
601 | "Stealth Mode connection attempt to UDP %s:%d from %s:%d\n", | |
602 | inet_ntop(AF_INET, &ip->ip_dst, buf, sizeof(buf)), | |
603 | ntohs(uh->uh_dport), | |
604 | inet_ntop(AF_INET, &ip->ip_src, buf2, sizeof(buf2)), | |
605 | ntohs(uh->uh_sport))) | |
1c79356b A |
606 | } |
607 | udpstat.udps_noport++; | |
608 | if (m->m_flags & (M_BCAST | M_MCAST)) { | |
609 | udpstat.udps_noportbcast++; | |
610 | goto bad; | |
611 | } | |
1c79356b | 612 | #if ICMP_BANDLIM |
9bccf70c | 613 | if (badport_bandlim(BANDLIM_ICMP_UNREACH) < 0) |
1c79356b A |
614 | goto bad; |
615 | #endif | |
9bccf70c | 616 | if (blackhole) |
91447636 A |
617 | if (m->m_pkthdr.rcvif && m->m_pkthdr.rcvif->if_type != IFT_LOOP) |
618 | goto bad; | |
9bccf70c A |
619 | *ip = save_ip; |
620 | ip->ip_len += iphlen; | |
1c79356b A |
621 | icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0); |
622 | KERNEL_DEBUG(DBG_FNC_UDP_INPUT | DBG_FUNC_END, 0,0,0,0,0); | |
623 | return; | |
624 | } | |
91447636 A |
625 | udp_lock(inp->inp_socket, 1, 0); |
626 | ||
627 | if (in_pcb_checkstate(inp, WNT_RELEASE, 1) == WNT_STOPUSING) { | |
628 | udp_unlock(inp->inp_socket, 1, 0); | |
1c79356b A |
629 | goto bad; |
630 | } | |
91447636 A |
631 | #if IPSEC |
632 | if (ipsec_bypass == 0 && inp != NULL) { | |
91447636 | 633 | if (ipsec4_in_reject_so(m, inp->inp_socket)) { |
2d21ac55 | 634 | IPSEC_STAT_INCREMENT(ipsecstat.in_polvio); |
91447636 A |
635 | udp_unlock(inp->inp_socket, 1, 0); |
636 | goto bad; | |
637 | } | |
91447636 | 638 | } |
1c79356b A |
639 | #endif /*IPSEC*/ |
640 | ||
641 | /* | |
642 | * Construct sockaddr format source address. | |
643 | * Stuff source address and datagram in user buffer. | |
644 | */ | |
645 | udp_in.sin_port = uh->uh_sport; | |
646 | udp_in.sin_addr = ip->ip_src; | |
647 | if (inp->inp_flags & INP_CONTROLOPTS | |
648 | || inp->inp_socket->so_options & SO_TIMESTAMP) { | |
649 | #if INET6 | |
650 | if (inp->inp_vflag & INP_IPV6) { | |
651 | int savedflags; | |
652 | ||
653 | ip_2_ip6_hdr(&udp_ip6.uip6_ip6, ip); | |
654 | savedflags = inp->inp_flags; | |
655 | inp->inp_flags &= ~INP_UNMAPPABLEOPTS; | |
9bccf70c | 656 | ip6_savecontrol(inp, &opts, &udp_ip6.uip6_ip6, m); |
1c79356b A |
657 | inp->inp_flags = savedflags; |
658 | } else | |
659 | #endif | |
660 | ip_savecontrol(inp, &opts, ip, m); | |
661 | } | |
9bccf70c | 662 | m_adj(m, iphlen + sizeof(struct udphdr)); |
1c79356b A |
663 | |
664 | KERNEL_DEBUG(DBG_LAYER_IN_END, uh->uh_dport, uh->uh_sport, | |
665 | save_ip.ip_src.s_addr, save_ip.ip_dst.s_addr, uh->uh_ulen); | |
666 | ||
667 | #if INET6 | |
668 | if (inp->inp_vflag & INP_IPV6) { | |
669 | in6_sin_2_v4mapsin6(&udp_in, &udp_in6.uin6_sin); | |
2d21ac55 | 670 | append_sa = (struct sockaddr *)&udp_in6.uin6_sin; |
1c79356b A |
671 | } else |
672 | #endif | |
673 | append_sa = (struct sockaddr *)&udp_in; | |
91447636 | 674 | if (sbappendaddr(&inp->inp_socket->so_rcv, append_sa, m, opts, NULL) == 0) { |
1c79356b | 675 | udpstat.udps_fullsock++; |
1c79356b | 676 | } |
91447636 A |
677 | else { |
678 | sorwakeup(inp->inp_socket); | |
679 | } | |
680 | udp_unlock(inp->inp_socket, 1, 0); | |
1c79356b A |
681 | KERNEL_DEBUG(DBG_FNC_UDP_INPUT | DBG_FUNC_END, 0,0,0,0,0); |
682 | return; | |
683 | bad: | |
684 | m_freem(m); | |
685 | if (opts) | |
686 | m_freem(opts); | |
687 | KERNEL_DEBUG(DBG_FNC_UDP_INPUT | DBG_FUNC_END, 0,0,0,0,0); | |
9bccf70c | 688 | return; |
1c79356b A |
689 | } |
690 | ||
691 | #if INET6 | |
692 | static void | |
693 | ip_2_ip6_hdr(ip6, ip) | |
694 | struct ip6_hdr *ip6; | |
695 | struct ip *ip; | |
696 | { | |
697 | bzero(ip6, sizeof(*ip6)); | |
698 | ||
699 | ip6->ip6_vfc = IPV6_VERSION; | |
700 | ip6->ip6_plen = ip->ip_len; | |
701 | ip6->ip6_nxt = ip->ip_p; | |
702 | ip6->ip6_hlim = ip->ip_ttl; | |
703 | ip6->ip6_src.s6_addr32[2] = ip6->ip6_dst.s6_addr32[2] = | |
704 | IPV6_ADDR_INT32_SMP; | |
705 | ip6->ip6_src.s6_addr32[3] = ip->ip_src.s_addr; | |
706 | ip6->ip6_dst.s6_addr32[3] = ip->ip_dst.s_addr; | |
707 | } | |
708 | #endif | |
709 | ||
710 | /* | |
711 | * subroutine of udp_input(), mainly for source code readability. | |
1c79356b A |
712 | */ |
713 | static void | |
2d21ac55 A |
714 | #if INET6 |
715 | udp_append(struct inpcb *last, struct ip *ip, struct mbuf *n, int off, | |
716 | struct sockaddr_in *pudp_in, struct udp_in6 *pudp_in6, | |
717 | struct udp_ip6 *pudp_ip6) | |
718 | #else | |
719 | udp_append(struct inpcb *last, struct ip *ip, struct mbuf *n, int off, | |
720 | struct sockaddr_in *pudp_in) | |
721 | #endif | |
1c79356b A |
722 | { |
723 | struct sockaddr *append_sa; | |
724 | struct mbuf *opts = 0; | |
1c79356b | 725 | |
2d21ac55 A |
726 | #if CONFIG_MACF_NET |
727 | if (mac_inpcb_check_deliver(last, n, AF_INET, SOCK_DGRAM) != 0) { | |
728 | m_freem(n); | |
729 | return; | |
730 | } | |
731 | #endif | |
1c79356b A |
732 | if (last->inp_flags & INP_CONTROLOPTS || |
733 | last->inp_socket->so_options & SO_TIMESTAMP) { | |
734 | #if INET6 | |
735 | if (last->inp_vflag & INP_IPV6) { | |
736 | int savedflags; | |
737 | ||
2d21ac55 A |
738 | if (pudp_ip6->uip6_init_done == 0) { |
739 | ip_2_ip6_hdr(&pudp_ip6->uip6_ip6, ip); | |
740 | pudp_ip6->uip6_init_done = 1; | |
1c79356b A |
741 | } |
742 | savedflags = last->inp_flags; | |
743 | last->inp_flags &= ~INP_UNMAPPABLEOPTS; | |
2d21ac55 | 744 | ip6_savecontrol(last, &opts, &pudp_ip6->uip6_ip6, n); |
1c79356b A |
745 | last->inp_flags = savedflags; |
746 | } else | |
747 | #endif | |
748 | ip_savecontrol(last, &opts, ip, n); | |
749 | } | |
750 | #if INET6 | |
751 | if (last->inp_vflag & INP_IPV6) { | |
2d21ac55 A |
752 | if (pudp_in6->uin6_init_done == 0) { |
753 | in6_sin_2_v4mapsin6(pudp_in, &pudp_in6->uin6_sin); | |
754 | pudp_in6->uin6_init_done = 1; | |
1c79356b | 755 | } |
2d21ac55 | 756 | append_sa = (struct sockaddr *)&pudp_in6->uin6_sin; |
1c79356b A |
757 | } else |
758 | #endif | |
2d21ac55 | 759 | append_sa = (struct sockaddr *)pudp_in; |
1c79356b | 760 | m_adj(n, off); |
91447636 | 761 | if (sbappendaddr(&last->inp_socket->so_rcv, append_sa, n, opts, NULL) == 0) { |
1c79356b A |
762 | udpstat.udps_fullsock++; |
763 | } else | |
764 | sorwakeup(last->inp_socket); | |
765 | } | |
766 | ||
1c79356b A |
767 | /* |
768 | * Notify a udp user of an asynchronous error; | |
769 | * just wake up so that he can collect error status. | |
770 | */ | |
771 | void | |
772 | udp_notify(inp, errno) | |
773 | register struct inpcb *inp; | |
774 | int errno; | |
775 | { | |
776 | inp->inp_socket->so_error = errno; | |
777 | sorwakeup(inp->inp_socket); | |
778 | sowwakeup(inp->inp_socket); | |
779 | } | |
780 | ||
781 | void | |
782 | udp_ctlinput(cmd, sa, vip) | |
783 | int cmd; | |
784 | struct sockaddr *sa; | |
785 | void *vip; | |
786 | { | |
9bccf70c A |
787 | struct ip *ip = vip; |
788 | struct udphdr *uh; | |
91447636 | 789 | void (*notify)(struct inpcb *, int) = udp_notify; |
9bccf70c A |
790 | struct in_addr faddr; |
791 | struct inpcb *inp; | |
9bccf70c A |
792 | |
793 | faddr = ((struct sockaddr_in *)sa)->sin_addr; | |
794 | if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY) | |
795 | return; | |
1c79356b | 796 | |
9bccf70c A |
797 | if (PRC_IS_REDIRECT(cmd)) { |
798 | ip = 0; | |
799 | notify = in_rtchange; | |
800 | } else if (cmd == PRC_HOSTDEAD) | |
801 | ip = 0; | |
802 | else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0) | |
1c79356b A |
803 | return; |
804 | if (ip) { | |
805 | uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2)); | |
9bccf70c A |
806 | inp = in_pcblookup_hash(&udbinfo, faddr, uh->uh_dport, |
807 | ip->ip_src, uh->uh_sport, 0, NULL); | |
91447636 A |
808 | if (inp != NULL && inp->inp_socket != NULL) { |
809 | udp_lock(inp->inp_socket, 1, 0); | |
810 | if (in_pcb_checkstate(inp, WNT_RELEASE, 1) == WNT_STOPUSING) { | |
811 | udp_unlock(inp->inp_socket, 1, 0); | |
812 | return; | |
813 | } | |
9bccf70c | 814 | (*notify)(inp, inetctlerrmap[cmd]); |
91447636 A |
815 | udp_unlock(inp->inp_socket, 1, 0); |
816 | } | |
1c79356b | 817 | } else |
91447636 | 818 | in_pcbnotifyall(&udbinfo, faddr, inetctlerrmap[cmd], notify); |
1c79356b A |
819 | } |
820 | ||
2d21ac55 A |
821 | int |
822 | udp_ctloutput(struct socket *so, struct sockopt *sopt) | |
823 | { | |
824 | int error, optval; | |
825 | struct inpcb *inp; | |
826 | ||
827 | if (sopt->sopt_level != IPPROTO_UDP) | |
828 | return (ip_ctloutput(so, sopt)); | |
829 | ||
830 | error = 0; | |
831 | inp = sotoinpcb(so); | |
832 | ||
833 | switch (sopt->sopt_dir) { | |
834 | case SOPT_SET: | |
835 | switch (sopt->sopt_name) { | |
836 | case UDP_NOCKSUM: | |
837 | /* This option is settable only for UDP over IPv4 */ | |
838 | if (!(inp->inp_vflag & INP_IPV4)) { | |
839 | error = EINVAL; | |
840 | break; | |
841 | } | |
842 | ||
843 | if ((error = sooptcopyin(sopt, &optval, sizeof (optval), | |
844 | sizeof (optval))) != 0) | |
845 | break; | |
846 | ||
847 | if (optval != 0) | |
848 | inp->inp_flags |= INP_UDP_NOCKSUM; | |
849 | else | |
850 | inp->inp_flags &= ~INP_UDP_NOCKSUM; | |
851 | break; | |
852 | ||
853 | default: | |
854 | error = ENOPROTOOPT; | |
855 | break; | |
856 | } | |
857 | break; | |
858 | ||
859 | case SOPT_GET: | |
860 | switch (sopt->sopt_name) { | |
861 | case UDP_NOCKSUM: | |
862 | optval = inp->inp_flags & INP_UDP_NOCKSUM; | |
863 | break; | |
864 | ||
865 | default: | |
866 | error = ENOPROTOOPT; | |
867 | break; | |
868 | } | |
869 | if (error == 0) | |
870 | error = sooptcopyout(sopt, &optval, sizeof (optval)); | |
871 | break; | |
872 | } | |
873 | return (error); | |
874 | } | |
875 | ||
1c79356b A |
876 | static int |
877 | udp_pcblist SYSCTL_HANDLER_ARGS | |
878 | { | |
2d21ac55 | 879 | #pragma unused(oidp, arg1, arg2) |
91447636 | 880 | int error, i, n; |
1c79356b A |
881 | struct inpcb *inp, **inp_list; |
882 | inp_gen_t gencnt; | |
883 | struct xinpgen xig; | |
884 | ||
885 | /* | |
886 | * The process of preparing the TCB list is too time-consuming and | |
887 | * resource-intensive to repeat twice on every request. | |
888 | */ | |
91447636 A |
889 | lck_rw_lock_exclusive(udbinfo.mtx); |
890 | if (req->oldptr == USER_ADDR_NULL) { | |
1c79356b A |
891 | n = udbinfo.ipi_count; |
892 | req->oldidx = 2 * (sizeof xig) | |
893 | + (n + n/8) * sizeof(struct xinpcb); | |
91447636 | 894 | lck_rw_done(udbinfo.mtx); |
1c79356b A |
895 | return 0; |
896 | } | |
897 | ||
91447636 A |
898 | if (req->newptr != USER_ADDR_NULL) { |
899 | lck_rw_done(udbinfo.mtx); | |
1c79356b | 900 | return EPERM; |
91447636 | 901 | } |
1c79356b A |
902 | |
903 | /* | |
904 | * OK, now we're committed to doing something. | |
905 | */ | |
1c79356b A |
906 | gencnt = udbinfo.ipi_gencnt; |
907 | n = udbinfo.ipi_count; | |
1c79356b | 908 | |
3a60a9f5 | 909 | bzero(&xig, sizeof(xig)); |
1c79356b A |
910 | xig.xig_len = sizeof xig; |
911 | xig.xig_count = n; | |
912 | xig.xig_gen = gencnt; | |
913 | xig.xig_sogen = so_gencnt; | |
914 | error = SYSCTL_OUT(req, &xig, sizeof xig); | |
91447636 A |
915 | if (error) { |
916 | lck_rw_done(udbinfo.mtx); | |
1c79356b | 917 | return error; |
91447636 | 918 | } |
9bccf70c A |
919 | /* |
920 | * We are done if there is no pcb | |
921 | */ | |
91447636 A |
922 | if (n == 0) { |
923 | lck_rw_done(udbinfo.mtx); | |
9bccf70c | 924 | return 0; |
91447636 | 925 | } |
1c79356b A |
926 | |
927 | inp_list = _MALLOC(n * sizeof *inp_list, M_TEMP, M_WAITOK); | |
928 | if (inp_list == 0) { | |
91447636 | 929 | lck_rw_done(udbinfo.mtx); |
1c79356b A |
930 | return ENOMEM; |
931 | } | |
9bccf70c A |
932 | |
933 | for (inp = LIST_FIRST(udbinfo.listhead), i = 0; inp && i < n; | |
934 | inp = LIST_NEXT(inp, inp_list)) { | |
91447636 | 935 | if (inp->inp_gencnt <= gencnt && inp->inp_state != INPCB_STATE_DEAD) |
1c79356b A |
936 | inp_list[i++] = inp; |
937 | } | |
1c79356b A |
938 | n = i; |
939 | ||
940 | error = 0; | |
941 | for (i = 0; i < n; i++) { | |
942 | inp = inp_list[i]; | |
91447636 | 943 | if (inp->inp_gencnt <= gencnt && inp->inp_state != INPCB_STATE_DEAD) { |
1c79356b | 944 | struct xinpcb xi; |
3a60a9f5 A |
945 | |
946 | bzero(&xi, sizeof(xi)); | |
1c79356b A |
947 | xi.xi_len = sizeof xi; |
948 | /* XXX should avoid extra copy */ | |
91447636 | 949 | inpcb_to_compat(inp, &xi.xi_inp); |
1c79356b A |
950 | if (inp->inp_socket) |
951 | sotoxsocket(inp->inp_socket, &xi.xi_socket); | |
952 | error = SYSCTL_OUT(req, &xi, sizeof xi); | |
953 | } | |
954 | } | |
955 | if (!error) { | |
956 | /* | |
957 | * Give the user an updated idea of our state. | |
958 | * If the generation differs from what we told | |
959 | * her before, she knows that something happened | |
960 | * while we were processing this request, and it | |
961 | * might be necessary to retry. | |
962 | */ | |
3a60a9f5 A |
963 | bzero(&xig, sizeof(xig)); |
964 | xig.xig_len = sizeof xig; | |
1c79356b A |
965 | xig.xig_gen = udbinfo.ipi_gencnt; |
966 | xig.xig_sogen = so_gencnt; | |
967 | xig.xig_count = udbinfo.ipi_count; | |
1c79356b A |
968 | error = SYSCTL_OUT(req, &xig, sizeof xig); |
969 | } | |
970 | FREE(inp_list, M_TEMP); | |
91447636 | 971 | lck_rw_done(udbinfo.mtx); |
1c79356b A |
972 | return error; |
973 | } | |
974 | ||
975 | SYSCTL_PROC(_net_inet_udp, UDPCTL_PCBLIST, pcblist, CTLFLAG_RD, 0, 0, | |
976 | udp_pcblist, "S,xinpcb", "List of active UDP sockets"); | |
977 | ||
978 | ||
979 | ||
91447636 A |
980 | static __inline__ u_int16_t |
981 | get_socket_id(struct socket * s) | |
982 | { | |
983 | u_int16_t val; | |
984 | ||
985 | if (s == NULL) { | |
986 | return (0); | |
987 | } | |
988 | val = (u_int16_t)(((u_int32_t)s) / sizeof(struct socket)); | |
989 | if (val == 0) { | |
990 | val = 0xffff; | |
991 | } | |
992 | return (val); | |
993 | } | |
994 | ||
1c79356b A |
995 | static int |
996 | udp_output(inp, m, addr, control, p) | |
997 | register struct inpcb *inp; | |
9bccf70c | 998 | struct mbuf *m; |
1c79356b A |
999 | struct sockaddr *addr; |
1000 | struct mbuf *control; | |
1001 | struct proc *p; | |
1002 | { | |
1003 | register struct udpiphdr *ui; | |
1004 | register int len = m->m_pkthdr.len; | |
2d21ac55 | 1005 | struct sockaddr_in *sin; |
91447636 A |
1006 | struct in_addr origladdr, laddr, faddr; |
1007 | u_short lport, fport; | |
1008 | struct sockaddr_in *ifaddr; | |
1009 | int error = 0, udp_dodisconnect = 0; | |
2d21ac55 A |
1010 | struct socket *so = inp->inp_socket; |
1011 | int soopts; | |
1012 | struct mbuf *inpopts; | |
1013 | struct ip_moptions *mopts; | |
1014 | struct route ro; | |
1c79356b A |
1015 | |
1016 | KERNEL_DEBUG(DBG_FNC_UDP_OUTPUT | DBG_FUNC_START, 0,0,0,0,0); | |
1017 | ||
1018 | if (control) | |
1019 | m_freem(control); /* XXX */ | |
1020 | ||
1021 | KERNEL_DEBUG(DBG_LAYER_OUT_BEG, inp->inp_fport, inp->inp_lport, | |
1022 | inp->inp_laddr.s_addr, inp->inp_faddr.s_addr, | |
1023 | (htons((u_short)len + sizeof (struct udphdr)))); | |
1024 | ||
1025 | if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) { | |
1026 | error = EMSGSIZE; | |
1027 | goto release; | |
1028 | } | |
1029 | ||
55e303ae A |
1030 | /* If there was a routing change, discard cached route and check |
1031 | * that we have a valid source address. | |
1032 | * Reacquire a new source address if INADDR_ANY was specified | |
1033 | */ | |
1034 | ||
91447636 A |
1035 | #if 1 |
1036 | lck_mtx_assert(inp->inpcb_mtx, LCK_MTX_ASSERT_OWNED); | |
1037 | #endif | |
1038 | ||
55e303ae | 1039 | if (inp->inp_route.ro_rt && inp->inp_route.ro_rt->generation_id != route_generation) { |
91447636 | 1040 | if (ifa_foraddr(inp->inp_laddr.s_addr) == 0) { /* src address is gone */ |
55e303ae | 1041 | if (inp->inp_flags & INP_INADDR_ANY) |
6601e61a | 1042 | inp->inp_laddr.s_addr = INADDR_ANY; /* new src will be set later */ |
55e303ae A |
1043 | else { |
1044 | error = EADDRNOTAVAIL; | |
1045 | goto release; | |
1046 | } | |
1047 | } | |
1048 | rtfree(inp->inp_route.ro_rt); | |
1049 | inp->inp_route.ro_rt = (struct rtentry *)0; | |
1050 | } | |
1051 | ||
91447636 A |
1052 | origladdr= laddr = inp->inp_laddr; |
1053 | faddr = inp->inp_faddr; | |
1054 | lport = inp->inp_lport; | |
1055 | fport = inp->inp_fport; | |
1056 | ||
1c79356b | 1057 | if (addr) { |
91447636 A |
1058 | sin = (struct sockaddr_in *)addr; |
1059 | if (faddr.s_addr != INADDR_ANY) { | |
1c79356b A |
1060 | error = EISCONN; |
1061 | goto release; | |
1062 | } | |
91447636 A |
1063 | if (lport == 0) { |
1064 | /* | |
1065 | * In case we don't have a local port set, go through the full connect. | |
1066 | * We don't have a local port yet (ie, we can't be looked up), | |
1067 | * so it's not an issue if the input runs at the same time we do this. | |
1068 | */ | |
1069 | error = in_pcbconnect(inp, addr, p); | |
1070 | if (error) { | |
1071 | goto release; | |
1072 | } | |
1073 | laddr = inp->inp_laddr; | |
1074 | lport = inp->inp_lport; | |
1075 | faddr = inp->inp_faddr; | |
1076 | fport = inp->inp_fport; | |
1077 | udp_dodisconnect = 1; | |
1078 | } | |
1079 | else { | |
1080 | /* Fast path case | |
1081 | * we have a full address and a local port. | |
1082 | * use those info to build the packet without changing the pcb | |
1083 | * and interfering with the input path. See 3851370 | |
1084 | */ | |
1085 | if (laddr.s_addr == INADDR_ANY) { | |
1086 | if ((error = in_pcbladdr(inp, addr, &ifaddr)) != 0) | |
1087 | goto release; | |
1088 | laddr = ifaddr->sin_addr; | |
1089 | inp->inp_flags |= INP_INADDR_ANY; /* from pcbconnect: remember we don't care about src addr.*/ | |
1090 | } | |
1091 | ||
1092 | faddr = sin->sin_addr; | |
1093 | fport = sin->sin_port; | |
1c79356b A |
1094 | } |
1095 | } else { | |
91447636 | 1096 | if (faddr.s_addr == INADDR_ANY) { |
1c79356b A |
1097 | error = ENOTCONN; |
1098 | goto release; | |
1099 | } | |
1100 | } | |
55e303ae | 1101 | |
2d21ac55 A |
1102 | #if CONFIG_MACF_NET |
1103 | mac_mbuf_label_associate_inpcb(inp, m); | |
1104 | #endif | |
1105 | ||
55e303ae | 1106 | |
1c79356b A |
1107 | /* |
1108 | * Calculate data length and get a mbuf | |
1109 | * for UDP and IP headers. | |
1110 | */ | |
1111 | M_PREPEND(m, sizeof(struct udpiphdr), M_DONTWAIT); | |
1112 | if (m == 0) { | |
1113 | error = ENOBUFS; | |
55e303ae | 1114 | goto abort; |
1c79356b A |
1115 | } |
1116 | ||
1117 | /* | |
1118 | * Fill in mbuf with extended UDP header | |
1119 | * and addresses and length put into network format. | |
1120 | */ | |
1121 | ui = mtod(m, struct udpiphdr *); | |
9bccf70c | 1122 | bzero(ui->ui_x1, sizeof(ui->ui_x1)); /* XXX still needed? */ |
1c79356b | 1123 | ui->ui_pr = IPPROTO_UDP; |
91447636 A |
1124 | ui->ui_src = laddr; |
1125 | ui->ui_dst = faddr; | |
1126 | ui->ui_sport = lport; | |
1127 | ui->ui_dport = fport; | |
9bccf70c | 1128 | ui->ui_ulen = htons((u_short)len + sizeof(struct udphdr)); |
1c79356b A |
1129 | |
1130 | /* | |
9bccf70c | 1131 | * Set up checksum and output datagram. |
1c79356b | 1132 | */ |
2d21ac55 | 1133 | if (udpcksum && !(inp->inp_flags & INP_UDP_NOCKSUM)) { |
9bccf70c A |
1134 | ui->ui_sum = in_pseudo(ui->ui_src.s_addr, ui->ui_dst.s_addr, |
1135 | htons((u_short)len + sizeof(struct udphdr) + IPPROTO_UDP)); | |
1136 | m->m_pkthdr.csum_flags = CSUM_UDP; | |
1137 | m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum); | |
1138 | } else { | |
0b4e3aa0 | 1139 | ui->ui_sum = 0; |
9bccf70c | 1140 | } |
1c79356b A |
1141 | ((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len; |
1142 | ((struct ip *)ui)->ip_ttl = inp->inp_ip_ttl; /* XXX */ | |
1143 | ((struct ip *)ui)->ip_tos = inp->inp_ip_tos; /* XXX */ | |
1144 | udpstat.udps_opackets++; | |
1145 | ||
1146 | KERNEL_DEBUG(DBG_LAYER_OUT_END, ui->ui_dport, ui->ui_sport, | |
1147 | ui->ui_src.s_addr, ui->ui_dst.s_addr, ui->ui_ulen); | |
1148 | ||
1c79356b | 1149 | #if IPSEC |
9bccf70c A |
1150 | if (ipsec_bypass == 0 && ipsec_setsocket(m, inp->inp_socket) != 0) { |
1151 | error = ENOBUFS; | |
55e303ae | 1152 | goto abort; |
9bccf70c | 1153 | } |
1c79356b | 1154 | #endif /*IPSEC*/ |
91447636 | 1155 | m->m_pkthdr.socket_id = get_socket_id(inp->inp_socket); |
2d21ac55 A |
1156 | |
1157 | inpopts = inp->inp_options; | |
1158 | soopts = (inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST)); | |
1159 | mopts = inp->inp_moptions; | |
1160 | ||
1161 | /* We don't want to cache the route for non-connected UDP */ | |
1162 | if (udp_dodisconnect) { | |
1163 | bcopy(&inp->inp_route, &ro, sizeof (ro)); | |
1164 | ro.ro_rt = NULL; | |
1165 | } | |
1166 | ||
1167 | socket_unlock(so, 0); | |
1168 | /* XXX jgraessley please look at XXX */ | |
1169 | error = ip_output_list(m, 0, inpopts, | |
1170 | udp_dodisconnect ? &ro : &inp->inp_route, soopts, mopts, NULL); | |
1171 | socket_lock(so, 0); | |
1c79356b | 1172 | |
91447636 | 1173 | if (udp_dodisconnect) { |
2d21ac55 A |
1174 | /* Discard the cached route, if there is one */ |
1175 | if (ro.ro_rt != NULL) | |
1176 | rtfree(ro.ro_rt); | |
1c79356b | 1177 | in_pcbdisconnect(inp); |
91447636 | 1178 | inp->inp_laddr = origladdr; /* XXX rehash? */ |
1c79356b A |
1179 | } |
1180 | KERNEL_DEBUG(DBG_FNC_UDP_OUTPUT | DBG_FUNC_END, error, 0,0,0,0); | |
1181 | return (error); | |
1182 | ||
55e303ae | 1183 | abort: |
91447636 | 1184 | if (udp_dodisconnect) { |
55e303ae | 1185 | in_pcbdisconnect(inp); |
91447636 | 1186 | inp->inp_laddr = origladdr; /* XXX rehash? */ |
55e303ae A |
1187 | } |
1188 | ||
1c79356b A |
1189 | release: |
1190 | m_freem(m); | |
1191 | KERNEL_DEBUG(DBG_FNC_UDP_OUTPUT | DBG_FUNC_END, error, 0,0,0,0); | |
1192 | return (error); | |
1193 | } | |
1194 | ||
1195 | u_long udp_sendspace = 9216; /* really max datagram size */ | |
1196 | /* 40 1K datagrams */ | |
1197 | SYSCTL_INT(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram, CTLFLAG_RW, | |
9bccf70c | 1198 | &udp_sendspace, 0, "Maximum outgoing UDP datagram size"); |
1c79356b | 1199 | |
9bccf70c | 1200 | u_long udp_recvspace = 40 * (1024 + |
1c79356b A |
1201 | #if INET6 |
1202 | sizeof(struct sockaddr_in6) | |
9bccf70c | 1203 | #else |
1c79356b | 1204 | sizeof(struct sockaddr_in) |
9bccf70c | 1205 | #endif |
1c79356b A |
1206 | ); |
1207 | SYSCTL_INT(_net_inet_udp, UDPCTL_RECVSPACE, recvspace, CTLFLAG_RW, | |
9bccf70c | 1208 | &udp_recvspace, 0, "Maximum incoming UDP datagram size"); |
1c79356b A |
1209 | |
1210 | static int | |
1211 | udp_abort(struct socket *so) | |
1212 | { | |
1213 | struct inpcb *inp; | |
1c79356b A |
1214 | |
1215 | inp = sotoinpcb(so); | |
1216 | if (inp == 0) | |
2d21ac55 | 1217 | panic("udp_abort: so=%p null inp\n", so); /* ??? possible? panic instead? */ |
1c79356b | 1218 | soisdisconnected(so); |
1c79356b | 1219 | in_pcbdetach(inp); |
1c79356b A |
1220 | return 0; |
1221 | } | |
1222 | ||
1223 | static int | |
2d21ac55 | 1224 | udp_attach(struct socket *so, __unused int proto, struct proc *p) |
1c79356b A |
1225 | { |
1226 | struct inpcb *inp; | |
91447636 | 1227 | int error; |
1c79356b | 1228 | |
9bccf70c A |
1229 | inp = sotoinpcb(so); |
1230 | if (inp != 0) | |
2d21ac55 | 1231 | panic ("udp_attach so=%p inp=%p\n", so, inp); |
9bccf70c | 1232 | |
1c79356b | 1233 | error = in_pcballoc(so, &udbinfo, p); |
1c79356b A |
1234 | if (error) |
1235 | return error; | |
91447636 A |
1236 | error = soreserve(so, udp_sendspace, udp_recvspace); |
1237 | if (error) | |
1238 | return error; | |
1c79356b A |
1239 | inp = (struct inpcb *)so->so_pcb; |
1240 | inp->inp_vflag |= INP_IPV4; | |
1241 | inp->inp_ip_ttl = ip_defttl; | |
1c79356b A |
1242 | return 0; |
1243 | } | |
1244 | ||
1245 | static int | |
1246 | udp_bind(struct socket *so, struct sockaddr *nam, struct proc *p) | |
1247 | { | |
1248 | struct inpcb *inp; | |
91447636 | 1249 | int error; |
1c79356b | 1250 | |
2d21ac55 A |
1251 | if (nam->sa_family != 0 && nam->sa_family != AF_INET |
1252 | && nam->sa_family != AF_INET6) { | |
1253 | return EAFNOSUPPORT; | |
1254 | } | |
1c79356b A |
1255 | inp = sotoinpcb(so); |
1256 | if (inp == 0) | |
1257 | return EINVAL; | |
1c79356b | 1258 | error = in_pcbbind(inp, nam, p); |
1c79356b A |
1259 | return error; |
1260 | } | |
1261 | ||
1262 | static int | |
1263 | udp_connect(struct socket *so, struct sockaddr *nam, struct proc *p) | |
1264 | { | |
1265 | struct inpcb *inp; | |
91447636 | 1266 | int error; |
1c79356b A |
1267 | |
1268 | inp = sotoinpcb(so); | |
1269 | if (inp == 0) | |
1270 | return EINVAL; | |
1271 | if (inp->inp_faddr.s_addr != INADDR_ANY) | |
1272 | return EISCONN; | |
1c79356b | 1273 | error = in_pcbconnect(inp, nam, p); |
91447636 | 1274 | if (error == 0) |
1c79356b A |
1275 | soisconnected(so); |
1276 | return error; | |
1277 | } | |
1278 | ||
1279 | static int | |
1280 | udp_detach(struct socket *so) | |
1281 | { | |
1282 | struct inpcb *inp; | |
1c79356b A |
1283 | |
1284 | inp = sotoinpcb(so); | |
1285 | if (inp == 0) | |
2d21ac55 | 1286 | panic("udp_detach: so=%p null inp\n", so); /* ??? possible? panic instead? */ |
1c79356b | 1287 | in_pcbdetach(inp); |
91447636 | 1288 | inp->inp_state = INPCB_STATE_DEAD; |
1c79356b A |
1289 | return 0; |
1290 | } | |
1291 | ||
1292 | static int | |
1293 | udp_disconnect(struct socket *so) | |
1294 | { | |
1295 | struct inpcb *inp; | |
1c79356b A |
1296 | |
1297 | inp = sotoinpcb(so); | |
1298 | if (inp == 0) | |
1299 | return EINVAL; | |
1300 | if (inp->inp_faddr.s_addr == INADDR_ANY) | |
1301 | return ENOTCONN; | |
1302 | ||
1c79356b A |
1303 | in_pcbdisconnect(inp); |
1304 | inp->inp_laddr.s_addr = INADDR_ANY; | |
1c79356b A |
1305 | so->so_state &= ~SS_ISCONNECTED; /* XXX */ |
1306 | return 0; | |
1307 | } | |
1308 | ||
1309 | static int | |
2d21ac55 | 1310 | udp_send(struct socket *so, __unused int flags, struct mbuf *m, struct sockaddr *addr, |
1c79356b A |
1311 | struct mbuf *control, struct proc *p) |
1312 | { | |
1313 | struct inpcb *inp; | |
1314 | ||
1315 | inp = sotoinpcb(so); | |
1316 | if (inp == 0) { | |
1317 | m_freem(m); | |
1318 | return EINVAL; | |
1319 | } | |
1320 | return udp_output(inp, m, addr, control, p); | |
1321 | } | |
1322 | ||
1323 | int | |
1324 | udp_shutdown(struct socket *so) | |
1325 | { | |
1326 | struct inpcb *inp; | |
1327 | ||
1328 | inp = sotoinpcb(so); | |
1329 | if (inp == 0) | |
1330 | return EINVAL; | |
1331 | socantsendmore(so); | |
1332 | return 0; | |
1333 | } | |
1334 | ||
1335 | struct pr_usrreqs udp_usrreqs = { | |
1336 | udp_abort, pru_accept_notsupp, udp_attach, udp_bind, udp_connect, | |
1337 | pru_connect2_notsupp, in_control, udp_detach, udp_disconnect, | |
1338 | pru_listen_notsupp, in_setpeeraddr, pru_rcvd_notsupp, | |
1339 | pru_rcvoob_notsupp, udp_send, pru_sense_null, udp_shutdown, | |
91447636 | 1340 | in_setsockaddr, sosend, soreceive, pru_sopoll_notsupp |
1c79356b A |
1341 | }; |
1342 | ||
91447636 A |
1343 | |
1344 | int | |
2d21ac55 | 1345 | udp_lock(struct socket *so, int refcount, int debug) |
91447636 A |
1346 | { |
1347 | int lr_saved; | |
0c530ab8 A |
1348 | if (debug == 0) |
1349 | lr_saved = (unsigned int) __builtin_return_address(0); | |
91447636 | 1350 | else lr_saved = debug; |
91447636 A |
1351 | |
1352 | if (so->so_pcb) { | |
1353 | lck_mtx_assert(((struct inpcb *)so->so_pcb)->inpcb_mtx, LCK_MTX_ASSERT_NOTOWNED); | |
1354 | lck_mtx_lock(((struct inpcb *)so->so_pcb)->inpcb_mtx); | |
1355 | } | |
0c530ab8 | 1356 | else |
2d21ac55 | 1357 | panic("udp_lock: so=%p NO PCB! lr=%x\n", so, lr_saved); |
91447636 A |
1358 | |
1359 | if (refcount) | |
1360 | so->so_usecount++; | |
1361 | ||
2d21ac55 | 1362 | so->lock_lr[so->next_lock_lr] = (u_int32_t)lr_saved; |
0c530ab8 | 1363 | so->next_lock_lr = (so->next_lock_lr+1) % SO_LCKDBG_MAX; |
91447636 A |
1364 | return (0); |
1365 | } | |
1366 | ||
1367 | int | |
2d21ac55 | 1368 | udp_unlock(struct socket *so, int refcount, int debug) |
91447636 A |
1369 | { |
1370 | int lr_saved; | |
0c530ab8 A |
1371 | |
1372 | if (debug == 0) | |
1373 | lr_saved = (unsigned int) __builtin_return_address(0); | |
91447636 | 1374 | else lr_saved = debug; |
0c530ab8 | 1375 | |
91447636 A |
1376 | if (refcount) { |
1377 | so->so_usecount--; | |
1378 | #if 0 | |
2d21ac55 A |
1379 | { |
1380 | struct inpcb *inp = sotoinpcb(so); | |
1381 | struct inpcbinfo *pcbinfo = &udbinfo; | |
1382 | ||
1383 | if (so->so_usecount == 0 && (inp->inp_wantcnt == WNT_STOPUSING)) { | |
1384 | ||
1385 | if (lck_rw_try_lock_exclusive(pcbinfo->mtx)) { | |
1386 | in_pcbdispose(inp); | |
1387 | lck_rw_done(pcbinfo->mtx); | |
1388 | return(0); | |
1389 | } | |
91447636 A |
1390 | } |
1391 | } | |
1392 | #endif | |
1393 | } | |
0c530ab8 | 1394 | if (so->so_pcb == NULL) |
2d21ac55 | 1395 | panic("udp_unlock: so=%p NO PCB! lr=%x\n", so, lr_saved); |
91447636 A |
1396 | else { |
1397 | lck_mtx_assert(((struct inpcb *)so->so_pcb)->inpcb_mtx, LCK_MTX_ASSERT_OWNED); | |
2d21ac55 | 1398 | so->unlock_lr[so->next_unlock_lr] = (u_int32_t)lr_saved; |
0c530ab8 | 1399 | so->next_unlock_lr = (so->next_unlock_lr+1) % SO_LCKDBG_MAX; |
91447636 A |
1400 | lck_mtx_unlock(((struct inpcb *)so->so_pcb)->inpcb_mtx); |
1401 | } | |
1402 | ||
1403 | ||
91447636 A |
1404 | return (0); |
1405 | } | |
1406 | ||
1407 | lck_mtx_t * | |
2d21ac55 | 1408 | udp_getlock(struct socket *so, __unused int locktype) |
91447636 A |
1409 | { |
1410 | struct inpcb *inp = sotoinpcb(so); | |
1411 | ||
1412 | ||
1413 | if (so->so_pcb) | |
1414 | return(inp->inpcb_mtx); | |
1415 | else { | |
2d21ac55 | 1416 | panic("udp_getlock: so=%p NULL so_pcb\n", so); |
91447636 A |
1417 | return (so->so_proto->pr_domain->dom_mtx); |
1418 | } | |
1419 | } | |
1420 | ||
1421 | void | |
1422 | udp_slowtimo() | |
1423 | { | |
1424 | struct inpcb *inp, *inpnxt; | |
1425 | struct socket *so; | |
1426 | struct inpcbinfo *pcbinfo = &udbinfo; | |
1427 | ||
2d21ac55 A |
1428 | if (lck_rw_try_lock_exclusive(pcbinfo->mtx) == FALSE) { |
1429 | if (udp_gc_done == TRUE) { | |
1430 | udp_gc_done = FALSE; | |
1431 | return; /* couldn't get the lock, better lock next time */ | |
1432 | } | |
1433 | lck_rw_lock_exclusive(pcbinfo->mtx); | |
1434 | } | |
1435 | ||
1436 | udp_gc_done = TRUE; | |
91447636 A |
1437 | |
1438 | for (inp = udb.lh_first; inp != NULL; inp = inpnxt) { | |
1439 | inpnxt = inp->inp_list.le_next; | |
1440 | ||
1441 | /* Ignore nat/SharedIP dummy pcbs */ | |
1442 | if (inp->inp_socket == &udbinfo.nat_dummy_socket) | |
1443 | continue; | |
1444 | ||
1445 | if (inp->inp_wantcnt != WNT_STOPUSING) | |
1446 | continue; | |
1447 | ||
1448 | so = inp->inp_socket; | |
1449 | if (!lck_mtx_try_lock(inp->inpcb_mtx)) /* skip if busy, no hurry for cleanup... */ | |
1450 | continue; | |
1451 | ||
1452 | if (so->so_usecount == 0) | |
1453 | in_pcbdispose(inp); | |
1454 | else | |
1455 | lck_mtx_unlock(inp->inpcb_mtx); | |
1456 | } | |
1457 | lck_rw_done(pcbinfo->mtx); | |
1458 | } | |
1459 | ||
1460 | int | |
1461 | ChkAddressOK( __uint32_t dstaddr, __uint32_t srcaddr ) | |
1462 | { | |
1463 | if ( dstaddr == srcaddr ){ | |
1464 | return 0; | |
1465 | } | |
1466 | return 1; | |
1467 | } | |
1468 | ||
2d21ac55 A |
1469 | void |
1470 | udp_in_cksum_stats(u_int32_t len) | |
1471 | { | |
1472 | udps_in_sw_cksum++; | |
1473 | udps_in_sw_cksum_bytes += len; | |
1474 | } | |
1475 | ||
1476 | void | |
1477 | udp_out_cksum_stats(u_int32_t len) | |
1478 | { | |
1479 | udps_out_sw_cksum++; | |
1480 | udps_out_sw_cksum_bytes += len; | |
1481 | } |