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