2 * Copyright (c) 2006 Apple Computer, Inc. All Rights Reserved.
4 * @APPLE_LICENSE_OSREFERENCE_HEADER_START@
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
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
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.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
31 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
32 * The Regents of the University of California. All rights reserved.
34 * Redistribution and use in source and binary forms, with or without
35 * modification, are permitted provided that the following conditions
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.
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
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 $
66 #include <sys/param.h>
67 #include <sys/systm.h>
68 #include <sys/kernel.h>
69 #include <sys/malloc.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>
79 #include <net/if_types.h>
80 #include <net/route.h>
82 #include <netinet/in.h>
83 #include <netinet/in_systm.h>
84 #include <netinet/ip.h>
86 #include <netinet/ip6.h>
88 #include <netinet/in_pcb.h>
89 #include <netinet/in_var.h>
90 #include <netinet/ip_var.h>
92 #include <netinet6/ip6_var.h>
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>
101 #include <netinet6/ipsec.h>
102 extern int ipsec_bypass
;
103 extern lck_mtx_t
*sadb_mutex
;
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)
115 * UDP protocol implementation.
116 * Per RFC 768, August, 1980.
119 static int udpcksum
= 1;
121 static int udpcksum
= 0; /* XXX */
123 SYSCTL_INT(_net_inet_udp
, UDPCTL_CHECKSUM
, checksum
, CTLFLAG_RW
,
127 SYSCTL_INT(_net_inet_udp
, OID_AUTO
, log_in_vain
, CTLFLAG_RW
,
128 &log_in_vain
, 0, "Log all incoming UDP packets");
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");
134 struct inpcbhead udb
; /* from udp_var.h */
135 #define udb6 udb /* for KAME src sync over BSD*'s */
136 struct inpcbinfo udbinfo
;
139 #define UDBHASHSIZE 16
142 extern int apple_hwcksum_rx
;
143 extern int esp_udp_encap_port
;
144 extern u_long route_generation
;
146 extern void ipfwsyslog( int level
, char *format
,...);
148 extern int fw_verbose
;
150 #define log_in_vain_log( a ) { \
151 if ( (log_in_vain == 3 ) && (fw_verbose == 2)) { /* Apple logging, log to ipfw.log */ \
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");
163 static struct sockaddr_in udp_in
= { sizeof(udp_in
), AF_INET
};
166 struct sockaddr_in6 uin6_sin
;
167 u_char uin6_init_done
: 1;
169 { sizeof(udp_in6
.uin6_sin
), AF_INET6
},
173 struct ip6_hdr uip6_ip6
;
174 u_char uip6_init_done
: 1;
178 static void udp_append(struct inpcb
*last
, struct ip
*ip
,
179 struct mbuf
*n
, int off
);
181 static void ip_2_ip6_hdr(struct ip6_hdr
*ip6
, struct ip
*ip
);
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
);
193 struct inpcbinfo
*pcbinfo
;
197 udbinfo
.listhead
= &udb
;
198 udbinfo
.hashbase
= hashinit(UDBHASHSIZE
, M_PCB
, &udbinfo
.hashmask
);
199 udbinfo
.porthashbase
= hashinit(UDBHASHSIZE
, M_PCB
,
200 &udbinfo
.porthashmask
);
202 str_size
= (vm_size_t
) sizeof(struct inpcb
);
203 udbinfo
.ipi_zone
= (void *) zinit(str_size
, 80000*str_size
, 8192, "udpcb");
207 * allocate lock group attribute and group for udp pcb mutexes
209 pcbinfo
->mtx_grp_attr
= lck_grp_attr_alloc_init();
210 lck_grp_attr_setdefault(pcbinfo
->mtx_grp_attr
);
212 pcbinfo
->mtx_grp
= lck_grp_alloc_init("udppcb", pcbinfo
->mtx_grp_attr
);
214 pcbinfo
->mtx_attr
= lck_attr_alloc_init();
215 lck_attr_setdefault(pcbinfo
->mtx_attr
);
217 if ((pcbinfo
->mtx
= lck_rw_alloc_init(pcbinfo
->mtx_grp
, pcbinfo
->mtx_attr
)) == NULL
)
218 return; /* pretty much dead if this fails... */
220 in_pcb_nat_init(&udbinfo
, AF_INET
, IPPROTO_UDP
, SOCK_DGRAM
);
222 udbinfo
.ipi_zone
= zinit("udpcb", sizeof(struct inpcb
), maxsockets
,
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
);
231 laddr
.s_addr
= 0x11646464;
232 faddr
.s_addr
= 0x11646465;
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
);
238 stat
= in_pcb_rem_share_client(&udbinfo
, fake_owner
);
239 kprintf("udp_init in_pcb_rem_share_client - stat = %d\n", stat
);
241 stat
= in_pcb_new_share_client(&udbinfo
, &fake_owner
);
242 kprintf("udp_init in_pcb_new_share_client(2) - stat = %d\n", stat
);
244 laddr
.s_addr
= 0x11646464;
245 faddr
.s_addr
= 0x11646465;
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
);
255 register struct mbuf
*m
;
258 register struct ip
*ip
;
259 register struct udphdr
*uh
;
260 register struct inpcb
*inp
;
261 struct mbuf
*opts
= 0;
264 struct sockaddr
*append_sa
;
265 struct inpcbinfo
*pcbinfo
= &udbinfo
;
267 udpstat
.udps_ipackets
++;
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 */
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.
279 if (iphlen
> sizeof (struct ip
)) {
280 ip_stripoptions(m
, (struct mbuf
*)0);
281 iphlen
= sizeof(struct ip
);
285 * Get IP and UDP header together in first mbuf.
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);
294 ip
= mtod(m
, struct ip
*);
296 uh
= (struct udphdr
*)((caddr_t
)ip
+ iphlen
);
298 /* destination port of 0 is illegal, based on RFC768. */
299 if (uh
->uh_dport
== 0)
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
);
306 * Make mbuf data length reflect UDP length.
307 * If not enough data to reflect UDP length, drop.
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
++;
315 m_adj(m
, len
- ip
->ip_len
);
316 /* ip->ip_len = len; */
319 * Save a copy of the IP header in case we want restore it
320 * for sending an ICMP error message in response.
325 * Checksum extended UDP header and data.
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
;
333 uh
->uh_sum
^= 0xffff;
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];
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
));
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];
350 udpstat
.udps_badsum
++;
352 KERNEL_DEBUG(DBG_FNC_UDP_INPUT
| DBG_FUNC_END
, 0,0,0,0,0);
358 udpstat
.udps_nosum
++;
361 if (IN_MULTICAST(ntohl(ip
->ip_dst
.s_addr
)) ||
362 in_broadcast(ip
->ip_dst
, m
->m_pkthdr
.rcvif
)) {
364 lck_rw_lock_shared(pcbinfo
->mtx
);
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?)
383 * Construct sockaddr format source address.
385 udp_in
.sin_port
= uh
->uh_sport
;
386 udp_in
.sin_addr
= ip
->ip_src
;
388 * Locate pcb(s) for datagram.
389 * (Algorithm copied from raw_intr().)
393 udp_in6
.uin6_init_done
= udp_ip6
.uip6_init_done
= 0;
395 LIST_FOREACH(inp
, &udb
, inp_list
) {
397 /* Ignore nat/SharedIP dummy pcbs */
398 if (inp
->inp_socket
== &udbinfo
.nat_dummy_socket
)
401 if (inp
->inp_socket
== NULL
)
403 if (inp
!= sotoinpcb(inp
->inp_socket
))
404 panic("udp_input: bad so back ptr inp=%x\n", inp
);
406 if ((inp
->inp_vflag
& INP_IPV4
) == 0)
409 if (in_pcb_checkstate(inp
, WNT_ACQUIRE
, 0) == WNT_STOPUSING
) {
413 udp_lock(inp
->inp_socket
, 1, 0);
415 if (in_pcb_checkstate(inp
, WNT_RELEASE
, 1) == WNT_STOPUSING
) {
416 udp_unlock(inp
->inp_socket
, 1, 0);
420 if (inp
->inp_lport
!= uh
->uh_dport
) {
421 udp_unlock(inp
->inp_socket
, 1, 0);
424 if (inp
->inp_laddr
.s_addr
!= INADDR_ANY
) {
425 if (inp
->inp_laddr
.s_addr
!=
427 udp_unlock(inp
->inp_socket
, 1, 0);
431 if (inp
->inp_faddr
.s_addr
!= INADDR_ANY
) {
432 if (inp
->inp_faddr
.s_addr
!=
434 inp
->inp_fport
!= uh
->uh_sport
) {
435 udp_unlock(inp
->inp_socket
, 1, 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 */
452 lck_mtx_unlock(sadb_mutex
);
456 if ((n
= m_copy(m
, 0, M_COPYALL
)) != NULL
) {
457 udp_append(last
, ip
, n
,
459 sizeof(struct udphdr
));
461 udp_unlock(last
->inp_socket
, 1, 0);
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.
472 if ((last
->inp_socket
->so_options
&(SO_REUSEPORT
|SO_REUSEADDR
)) == 0)
475 lck_rw_done(pcbinfo
->mtx
);
479 * No matching pcb found; discard datagram.
480 * (No need to send an ICMP Port Unreachable
481 * for a broadcast or multicast datgram.)
483 udpstat
.udps_noportbcast
++;
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);
496 lck_mtx_unlock(sadb_mutex
);
499 udp_append(last
, ip
, m
, iphlen
+ sizeof(struct udphdr
));
500 udp_unlock(last
->inp_socket
, 1, 0);
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.
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);
521 ip
= mtod(m
, struct ip
*);
522 uh
= (struct udphdr
*)((caddr_t
)ip
+ iphlen
);
524 /* Check for NAT keepalive packet */
525 if (payload_len
== 1 && *(u_int8_t
*)((caddr_t
)uh
+ sizeof(struct udphdr
)) == 0xFF) {
527 KERNEL_DEBUG(DBG_FNC_UDP_INPUT
| DBG_FUNC_END
, 0,0,0,0,0);
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 */
534 stripsiz
= sizeof(struct udphdr
);
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
;
545 KERNEL_DEBUG(DBG_FNC_UDP_INPUT
| DBG_FUNC_END
, 0,0,0,0,0);
546 esp4_input(m
, iphlen
);
553 * Locate pcb for datagram.
555 inp
= in_pcblookup_hash(&udbinfo
, ip
->ip_src
, uh
->uh_sport
,
556 ip
->ip_dst
, uh
->uh_dport
, 1, m
->m_pkthdr
.rcvif
);
559 char buf
[MAX_IPv4_STR_LEN
];
560 char buf2
[MAX_IPv4_STR_LEN
];
562 /* check src and dst address */
563 if (log_in_vain
!= 3)
565 "Connection attempt to UDP %s:%d from %s:%d\n",
566 inet_ntop(AF_INET
, &ip
->ip_dst
, buf
, sizeof(buf
)),
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
)),
576 inet_ntop(AF_INET
, &ip
->ip_src
, buf2
, sizeof(buf2
)),
577 ntohs(uh
->uh_sport
)))
579 udpstat
.udps_noport
++;
580 if (m
->m_flags
& (M_BCAST
| M_MCAST
)) {
581 udpstat
.udps_noportbcast
++;
585 if (badport_bandlim(BANDLIM_ICMP_UNREACH
) < 0)
589 if (m
->m_pkthdr
.rcvif
&& m
->m_pkthdr
.rcvif
->if_type
!= IFT_LOOP
)
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);
597 udp_lock(inp
->inp_socket
, 1, 0);
599 if (in_pcb_checkstate(inp
, WNT_RELEASE
, 1) == WNT_STOPUSING
) {
600 udp_unlock(inp
->inp_socket
, 1, 0);
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);
612 lck_mtx_unlock(sadb_mutex
);
617 * Construct sockaddr format source address.
618 * Stuff source address and datagram in user buffer.
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
) {
625 if (inp
->inp_vflag
& INP_IPV6
) {
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
;
635 ip_savecontrol(inp
, &opts
, ip
, m
);
637 m_adj(m
, iphlen
+ sizeof(struct udphdr
));
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
);
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
;
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
++;
653 sorwakeup(inp
->inp_socket
);
655 udp_unlock(inp
->inp_socket
, 1, 0);
656 KERNEL_DEBUG(DBG_FNC_UDP_INPUT
| DBG_FUNC_END
, 0,0,0,0,0);
662 KERNEL_DEBUG(DBG_FNC_UDP_INPUT
| DBG_FUNC_END
, 0,0,0,0,0);
668 ip_2_ip6_hdr(ip6
, ip
)
672 bzero(ip6
, sizeof(*ip6
));
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] =
680 ip6
->ip6_src
.s6_addr32
[3] = ip
->ip_src
.s_addr
;
681 ip6
->ip6_dst
.s6_addr32
[3] = ip
->ip_dst
.s_addr
;
686 * subroutine of udp_input(), mainly for source code readability.
687 * caller must properly init udp_ip6 and udp_in6 beforehand.
690 udp_append(last
, ip
, n
, off
)
696 struct sockaddr
*append_sa
;
697 struct mbuf
*opts
= 0;
699 if (last
->inp_flags
& INP_CONTROLOPTS
||
700 last
->inp_socket
->so_options
& SO_TIMESTAMP
) {
702 if (last
->inp_vflag
& INP_IPV6
) {
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;
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
;
715 ip_savecontrol(last
, &opts
, ip
, n
);
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;
723 append_sa
= (struct sockaddr
*)&udp_in6
.uin6_sin
;
726 append_sa
= (struct sockaddr
*)&udp_in
;
728 if (sbappendaddr(&last
->inp_socket
->so_rcv
, append_sa
, n
, opts
, NULL
) == 0) {
729 udpstat
.udps_fullsock
++;
731 sorwakeup(last
->inp_socket
);
735 * Notify a udp user of an asynchronous error;
736 * just wake up so that he can collect error status.
739 udp_notify(inp
, errno
)
740 register struct inpcb
*inp
;
743 inp
->inp_socket
->so_error
= errno
;
744 sorwakeup(inp
->inp_socket
);
745 sowwakeup(inp
->inp_socket
);
749 udp_ctlinput(cmd
, sa
, vip
)
756 void (*notify
)(struct inpcb
*, int) = udp_notify
;
757 struct in_addr faddr
;
760 faddr
= ((struct sockaddr_in
*)sa
)->sin_addr
;
761 if (sa
->sa_family
!= AF_INET
|| faddr
.s_addr
== INADDR_ANY
)
764 if (PRC_IS_REDIRECT(cmd
)) {
766 notify
= in_rtchange
;
767 } else if (cmd
== PRC_HOSTDEAD
)
769 else if ((unsigned)cmd
>= PRC_NCMDS
|| inetctlerrmap
[cmd
] == 0)
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);
781 (*notify
)(inp
, inetctlerrmap
[cmd
]);
782 udp_unlock(inp
->inp_socket
, 1, 0);
785 in_pcbnotifyall(&udbinfo
, faddr
, inetctlerrmap
[cmd
], notify
);
789 udp_pcblist SYSCTL_HANDLER_ARGS
792 struct inpcb
*inp
, **inp_list
;
797 * The process of preparing the TCB list is too time-consuming and
798 * resource-intensive to repeat twice on every request.
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
);
809 if (req
->newptr
!= USER_ADDR_NULL
) {
810 lck_rw_done(udbinfo
.mtx
);
815 * OK, now we're committed to doing something.
817 gencnt
= udbinfo
.ipi_gencnt
;
818 n
= udbinfo
.ipi_count
;
820 bzero(&xig
, sizeof(xig
));
821 xig
.xig_len
= sizeof xig
;
823 xig
.xig_gen
= gencnt
;
824 xig
.xig_sogen
= so_gencnt
;
825 error
= SYSCTL_OUT(req
, &xig
, sizeof xig
);
827 lck_rw_done(udbinfo
.mtx
);
831 * We are done if there is no pcb
834 lck_rw_done(udbinfo
.mtx
);
838 inp_list
= _MALLOC(n
* sizeof *inp_list
, M_TEMP
, M_WAITOK
);
840 lck_rw_done(udbinfo
.mtx
);
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
)
852 for (i
= 0; i
< n
; i
++) {
854 if (inp
->inp_gencnt
<= gencnt
&& inp
->inp_state
!= INPCB_STATE_DEAD
) {
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
);
862 sotoxsocket(inp
->inp_socket
, &xi
.xi_socket
);
863 error
= SYSCTL_OUT(req
, &xi
, sizeof xi
);
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.
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
);
881 FREE(inp_list
, M_TEMP
);
882 lck_rw_done(udbinfo
.mtx
);
886 SYSCTL_PROC(_net_inet_udp
, UDPCTL_PCBLIST
, pcblist
, CTLFLAG_RD
, 0, 0,
887 udp_pcblist
, "S,xinpcb", "List of active UDP sockets");
891 static __inline__ u_int16_t
892 get_socket_id(struct socket
* s
)
899 val
= (u_int16_t
)(((u_int32_t
)s
) / sizeof(struct socket
));
907 udp_output(inp
, m
, addr
, control
, p
)
908 register struct inpcb
*inp
;
910 struct sockaddr
*addr
;
911 struct mbuf
*control
;
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;
923 KERNEL_DEBUG(DBG_FNC_UDP_OUTPUT
| DBG_FUNC_START
, 0,0,0,0,0);
926 m_freem(control
); /* XXX */
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
))));
932 if (len
+ sizeof(struct udpiphdr
) > IP_MAXPACKET
) {
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
943 lck_mtx_assert(inp
->inpcb_mtx
, LCK_MTX_ASSERT_OWNED
);
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 */
951 error
= EADDRNOTAVAIL
;
955 rtfree(inp
->inp_route
.ro_rt
);
956 inp
->inp_route
.ro_rt
= (struct rtentry
*)0;
959 origladdr
= laddr
= inp
->inp_laddr
;
960 faddr
= inp
->inp_faddr
;
961 lport
= inp
->inp_lport
;
962 fport
= inp
->inp_fport
;
965 sin
= (struct sockaddr_in
*)addr
;
966 if (faddr
.s_addr
!= INADDR_ANY
) {
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.
976 error
= in_pcbconnect(inp
, addr
, p
);
980 laddr
= inp
->inp_laddr
;
981 lport
= inp
->inp_lport
;
982 faddr
= inp
->inp_faddr
;
983 fport
= inp
->inp_fport
;
984 udp_dodisconnect
= 1;
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
992 if (laddr
.s_addr
== INADDR_ANY
) {
993 if ((error
= in_pcbladdr(inp
, addr
, &ifaddr
)) != 0)
995 laddr
= ifaddr
->sin_addr
;
996 inp
->inp_flags
|= INP_INADDR_ANY
; /* from pcbconnect: remember we don't care about src addr.*/
999 faddr
= sin
->sin_addr
;
1000 fport
= sin
->sin_port
;
1003 if (faddr
.s_addr
== INADDR_ANY
) {
1011 * Calculate data length and get a mbuf
1012 * for UDP and IP headers.
1014 M_PREPEND(m
, sizeof(struct udpiphdr
), M_DONTWAIT
);
1021 * Fill in mbuf with extended UDP header
1022 * and addresses and length put into network format.
1024 ui
= mtod(m
, struct udpiphdr
*);
1025 bzero(ui
->ui_x1
, sizeof(ui
->ui_x1
)); /* XXX still needed? */
1026 ui
->ui_pr
= IPPROTO_UDP
;
1029 ui
->ui_sport
= lport
;
1030 ui
->ui_dport
= fport
;
1031 ui
->ui_ulen
= htons((u_short
)len
+ sizeof(struct udphdr
));
1034 * Set up checksum and output datagram.
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
);
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
++;
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
);
1053 if (ipsec_bypass
== 0 && ipsec_setsocket(m
, inp
->inp_socket
) != 0) {
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
)),
1063 if (udp_dodisconnect
) {
1064 in_pcbdisconnect(inp
);
1065 inp
->inp_laddr
= origladdr
; /* XXX rehash? */
1067 KERNEL_DEBUG(DBG_FNC_UDP_OUTPUT
| DBG_FUNC_END
, error
, 0,0,0,0);
1071 if (udp_dodisconnect
) {
1072 in_pcbdisconnect(inp
);
1073 inp
->inp_laddr
= origladdr
; /* XXX rehash? */
1078 KERNEL_DEBUG(DBG_FNC_UDP_OUTPUT
| DBG_FUNC_END
, error
, 0,0,0,0);
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");
1087 u_long udp_recvspace
= 40 * (1024 +
1089 sizeof(struct sockaddr_in6
)
1091 sizeof(struct sockaddr_in
)
1094 SYSCTL_INT(_net_inet_udp
, UDPCTL_RECVSPACE
, recvspace
, CTLFLAG_RW
,
1095 &udp_recvspace
, 0, "Maximum incoming UDP datagram size");
1098 udp_abort(struct socket
*so
)
1102 inp
= sotoinpcb(so
);
1104 panic("udp_abort: so=%x null inp\n", so
); /* ??? possible? panic instead? */
1105 soisdisconnected(so
);
1111 udp_attach(struct socket
*so
, int proto
, struct proc
*p
)
1116 inp
= sotoinpcb(so
);
1118 panic ("udp_attach so=%x inp=%x\n", so
, inp
);
1120 error
= in_pcballoc(so
, &udbinfo
, p
);
1123 error
= soreserve(so
, udp_sendspace
, udp_recvspace
);
1126 inp
= (struct inpcb
*)so
->so_pcb
;
1127 inp
->inp_vflag
|= INP_IPV4
;
1128 inp
->inp_ip_ttl
= ip_defttl
;
1133 udp_bind(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
1138 inp
= sotoinpcb(so
);
1141 error
= in_pcbbind(inp
, nam
, p
);
1146 udp_connect(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
1151 inp
= sotoinpcb(so
);
1154 if (inp
->inp_faddr
.s_addr
!= INADDR_ANY
)
1156 error
= in_pcbconnect(inp
, nam
, p
);
1163 udp_detach(struct socket
*so
)
1167 inp
= sotoinpcb(so
);
1169 panic("udp_detach: so=%x null inp\n", so
); /* ??? possible? panic instead? */
1171 inp
->inp_state
= INPCB_STATE_DEAD
;
1176 udp_disconnect(struct socket
*so
)
1180 inp
= sotoinpcb(so
);
1183 if (inp
->inp_faddr
.s_addr
== INADDR_ANY
)
1186 in_pcbdisconnect(inp
);
1187 inp
->inp_laddr
.s_addr
= INADDR_ANY
;
1188 so
->so_state
&= ~SS_ISCONNECTED
; /* XXX */
1193 udp_send(struct socket
*so
, int flags
, struct mbuf
*m
, struct sockaddr
*addr
,
1194 struct mbuf
*control
, struct proc
*p
)
1198 inp
= sotoinpcb(so
);
1203 return udp_output(inp
, m
, addr
, control
, p
);
1207 udp_shutdown(struct socket
*so
)
1211 inp
= sotoinpcb(so
);
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
1228 udp_lock(so
, refcount
, debug
)
1230 int refcount
, debug
;
1235 __asm__
volatile("mflr %0" : "=r" (lr_saved
));
1237 else lr_saved
= debug
;
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
);
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
);
1253 so
->reserved3
= lr_saved
;
1258 udp_unlock(so
, refcount
, debug
)
1264 struct inpcb
*inp
= sotoinpcb(so
);
1265 struct inpcbinfo
*pcbinfo
= &udbinfo
;
1268 __asm__
volatile("mflr %0" : "=r" (lr_saved
));
1270 else lr_saved
= debug
;
1275 if (so
->so_usecount
== 0 && (inp
->inp_wantcnt
== WNT_STOPUSING
)) {
1276 if (lck_rw_try_lock_exclusive(pcbinfo
->mtx
)) {
1278 lck_rw_done(pcbinfo
->mtx
);
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
);
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
);
1295 so
->reserved4
= lr_saved
;
1300 udp_getlock(so
, locktype
)
1304 struct inpcb
*inp
= sotoinpcb(so
);
1308 return(inp
->inpcb_mtx
);
1310 panic("udp_getlock: so=%x NULL so_pcb\n", so
);
1311 return (so
->so_proto
->pr_domain
->dom_mtx
);
1318 struct inpcb
*inp
, *inpnxt
;
1320 struct inpcbinfo
*pcbinfo
= &udbinfo
;
1322 lck_rw_lock_exclusive(pcbinfo
->mtx
);
1324 for (inp
= udb
.lh_first
; inp
!= NULL
; inp
= inpnxt
) {
1325 inpnxt
= inp
->inp_list
.le_next
;
1327 /* Ignore nat/SharedIP dummy pcbs */
1328 if (inp
->inp_socket
== &udbinfo
.nat_dummy_socket
)
1331 if (inp
->inp_wantcnt
!= WNT_STOPUSING
)
1334 so
= inp
->inp_socket
;
1335 if (!lck_mtx_try_lock(inp
->inpcb_mtx
)) /* skip if busy, no hurry for cleanup... */
1338 if (so
->so_usecount
== 0)
1341 lck_mtx_unlock(inp
->inpcb_mtx
);
1343 lck_rw_done(pcbinfo
->mtx
);
1347 ChkAddressOK( __uint32_t dstaddr
, __uint32_t srcaddr
)
1349 if ( dstaddr
== srcaddr
){