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