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