2 * Copyright (c) 2000-2012 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_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 License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
30 * The Regents of the University of California. All rights reserved.
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * @(#)udp_usrreq.c 8.6 (Berkeley) 5/23/95
61 * $FreeBSD: src/sys/netinet/udp_usrreq.c,v 1.64.2.13 2001/08/08 18:59:54 ghelmer Exp $
64 #include <sys/param.h>
65 #include <sys/systm.h>
66 #include <sys/kernel.h>
67 #include <sys/malloc.h>
69 #include <sys/domain.h>
70 #include <sys/protosw.h>
71 #include <sys/socket.h>
72 #include <sys/socketvar.h>
73 #include <sys/sysctl.h>
74 #include <sys/syslog.h>
75 #include <sys/mcache.h>
76 #include <net/ntstat.h>
78 #include <kern/zalloc.h>
81 #include <net/if_types.h>
82 #include <net/route.h>
84 #include <netinet/in.h>
85 #include <netinet/in_systm.h>
86 #include <netinet/ip.h>
88 #include <netinet/ip6.h>
90 #include <netinet/in_pcb.h>
91 #include <netinet/in_var.h>
92 #include <netinet/ip_var.h>
94 #include <netinet6/in6_pcb.h>
95 #include <netinet6/ip6_var.h>
97 #include <netinet/ip_icmp.h>
98 #include <netinet/icmp_var.h>
99 #include <netinet/udp.h>
100 #include <netinet/udp_var.h>
101 #include <sys/kdebug.h>
104 #include <netinet6/ipsec.h>
105 #include <netinet6/esp.h>
106 extern int ipsec_bypass
;
110 #define DBG_LAYER_IN_BEG NETDBG_CODE(DBG_NETUDP, 0)
111 #define DBG_LAYER_IN_END NETDBG_CODE(DBG_NETUDP, 2)
112 #define DBG_LAYER_OUT_BEG NETDBG_CODE(DBG_NETUDP, 1)
113 #define DBG_LAYER_OUT_END NETDBG_CODE(DBG_NETUDP, 3)
114 #define DBG_FNC_UDP_INPUT NETDBG_CODE(DBG_NETUDP, (5 << 8))
115 #define DBG_FNC_UDP_OUTPUT NETDBG_CODE(DBG_NETUDP, (6 << 8) | 1)
118 * UDP protocol implementation.
119 * Per RFC 768, August, 1980.
122 static int udpcksum
= 1;
124 static int udpcksum
= 0; /* XXX */
126 SYSCTL_INT(_net_inet_udp
, UDPCTL_CHECKSUM
, checksum
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
129 static u_int32_t udps_in_sw_cksum
;
130 SYSCTL_UINT(_net_inet_udp
, OID_AUTO
, in_sw_cksum
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
131 &udps_in_sw_cksum
, 0,
132 "Number of received packets checksummed in software");
134 static u_int64_t udps_in_sw_cksum_bytes
;
135 SYSCTL_QUAD(_net_inet_udp
, OID_AUTO
, in_sw_cksum_bytes
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
136 &udps_in_sw_cksum_bytes
,
137 "Amount of received data checksummed in software");
139 static u_int32_t udps_out_sw_cksum
;
140 SYSCTL_UINT(_net_inet_udp
, OID_AUTO
, out_sw_cksum
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
141 &udps_out_sw_cksum
, 0,
142 "Number of transmitted packets checksummed in software");
144 static u_int64_t udps_out_sw_cksum_bytes
;
145 SYSCTL_QUAD(_net_inet_udp
, OID_AUTO
, out_sw_cksum_bytes
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
146 &udps_out_sw_cksum_bytes
,
147 "Amount of transmitted data checksummed in software");
150 SYSCTL_INT(_net_inet_udp
, OID_AUTO
, log_in_vain
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
151 &log_in_vain
, 0, "Log all incoming UDP packets");
153 static int blackhole
= 0;
154 SYSCTL_INT(_net_inet_udp
, OID_AUTO
, blackhole
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
155 &blackhole
, 0, "Do not send port unreachables for refused connects");
157 struct inpcbhead udb
; /* from udp_var.h */
158 #define udb6 udb /* for KAME src sync over BSD*'s */
159 struct inpcbinfo udbinfo
;
162 #define UDBHASHSIZE 16
165 extern int esp_udp_encap_port
;
167 extern void ipfwsyslog( int level
, const char *format
,...);
169 extern int fw_verbose
;
170 static int udp_gc_done
= FALSE
; /* Garbage collection performed last slowtimo */
173 #define log_in_vain_log( a ) { \
174 if ( (log_in_vain == 3 ) && (fw_verbose == 2)) { /* Apple logging, log to ipfw.log */ \
180 #define log_in_vain_log( a ) { log a; }
183 struct udpstat udpstat
; /* from udp_var.h */
184 SYSCTL_STRUCT(_net_inet_udp
, UDPCTL_STATS
, stats
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
185 &udpstat
, udpstat
, "UDP statistics (struct udpstat, netinet/udp_var.h)");
186 SYSCTL_INT(_net_inet_udp
, OID_AUTO
, pcbcount
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
187 &udbinfo
.ipi_count
, 0, "Number of active PCBs");
189 __private_extern__
int udp_use_randomport
= 1;
190 SYSCTL_INT(_net_inet_udp
, OID_AUTO
, randomize_ports
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
191 &udp_use_randomport
, 0, "Randomize UDP port numbers");
195 struct sockaddr_in6 uin6_sin
;
196 u_char uin6_init_done
: 1;
199 struct ip6_hdr uip6_ip6
;
200 u_char uip6_init_done
: 1;
202 static void ip_2_ip6_hdr(struct ip6_hdr
*ip6
, struct ip
*ip
);
203 static void udp_append(struct inpcb
*last
, struct ip
*ip
,
204 struct mbuf
*n
, int off
, struct sockaddr_in
*pudp_in
,
205 struct udp_in6
*pudp_in6
, struct udp_ip6
*pudp_ip6
);
207 static void udp_append(struct inpcb
*last
, struct ip
*ip
,
208 struct mbuf
*n
, int off
, struct sockaddr_in
*pudp_in
);
211 static int udp_detach(struct socket
*so
);
212 static int udp_output(struct inpcb
*, struct mbuf
*, struct sockaddr
*,
213 struct mbuf
*, struct proc
*);
214 extern int ChkAddressOK( __uint32_t dstaddr
, __uint32_t srcaddr
);
220 struct inpcbinfo
*pcbinfo
;
224 udbinfo
.listhead
= &udb
;
225 udbinfo
.hashbase
= hashinit(UDBHASHSIZE
, M_PCB
, &udbinfo
.hashmask
);
226 udbinfo
.porthashbase
= hashinit(UDBHASHSIZE
, M_PCB
,
227 &udbinfo
.porthashmask
);
229 str_size
= (vm_size_t
) sizeof(struct inpcb
);
230 udbinfo
.ipi_zone
= (void *) zinit(str_size
, 80000*str_size
, 8192, "udpcb");
234 * allocate lock group attribute and group for udp pcb mutexes
236 pcbinfo
->mtx_grp_attr
= lck_grp_attr_alloc_init();
238 pcbinfo
->mtx_grp
= lck_grp_alloc_init("udppcb", pcbinfo
->mtx_grp_attr
);
240 pcbinfo
->mtx_attr
= lck_attr_alloc_init();
242 if ((pcbinfo
->mtx
= lck_rw_alloc_init(pcbinfo
->mtx_grp
, pcbinfo
->mtx_attr
)) == NULL
)
243 return; /* pretty much dead if this fails... */
245 udbinfo
.ipi_zone
= zinit("udpcb", sizeof(struct inpcb
), maxsockets
,
252 register struct mbuf
*m
;
255 register struct ip
*ip
;
256 register struct udphdr
*uh
;
257 register struct inpcb
*inp
;
258 struct mbuf
*opts
= 0;
259 int len
, isbroadcast
;
261 struct sockaddr
*append_sa
;
262 struct inpcbinfo
*pcbinfo
= &udbinfo
;
263 struct sockaddr_in udp_in
= {
264 sizeof (udp_in
), AF_INET
, 0, { 0 }, { 0, 0, 0, 0, 0, 0, 0, 0 }
266 struct ip_moptions
*imo
= NULL
;
267 int foundmembership
= 0, ret
= 0;
269 struct udp_in6 udp_in6
= {
270 { sizeof (udp_in6
.uin6_sin
), AF_INET6
, 0, 0,
271 IN6ADDR_ANY_INIT
, 0 },
274 struct udp_ip6 udp_ip6
;
276 struct ifnet
*ifp
= (m
->m_pkthdr
.rcvif
!= NULL
) ? m
->m_pkthdr
.rcvif
: NULL
;
278 udpstat
.udps_ipackets
++;
280 KERNEL_DEBUG(DBG_FNC_UDP_INPUT
| DBG_FUNC_START
, 0,0,0,0,0);
281 if (m
->m_pkthdr
.csum_flags
& CSUM_TCP_SUM16
)
282 m
->m_pkthdr
.csum_flags
= 0; /* invalidate hwcksum for UDP */
284 /* Expect 32-bit aligned data pointer on strict-align platforms */
285 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m
);
288 * Strip IP options, if any; should skip this,
289 * make available to user, and use on returned packets,
290 * but we don't yet have a way to check the checksum
291 * with options still present.
293 if (iphlen
> sizeof (struct ip
)) {
294 ip_stripoptions(m
, (struct mbuf
*)0);
295 iphlen
= sizeof(struct ip
);
299 * Get IP and UDP header together in first mbuf.
301 ip
= mtod(m
, struct ip
*);
302 if (m
->m_len
< iphlen
+ sizeof(struct udphdr
)) {
303 if ((m
= m_pullup(m
, iphlen
+ sizeof(struct udphdr
))) == 0) {
304 udpstat
.udps_hdrops
++;
305 KERNEL_DEBUG(DBG_FNC_UDP_INPUT
| DBG_FUNC_END
, 0,0,0,0,0);
308 ip
= mtod(m
, struct ip
*);
310 uh
= (struct udphdr
*)(void *)((caddr_t
)ip
+ iphlen
);
312 /* destination port of 0 is illegal, based on RFC768. */
313 if (uh
->uh_dport
== 0) {
315 if (ifp
->if_udp_stat
!= NULL
)
316 atomic_add_64(&ifp
->if_udp_stat
->port0
, 1);
321 KERNEL_DEBUG(DBG_LAYER_IN_BEG
, uh
->uh_dport
, uh
->uh_sport
,
322 ip
->ip_src
.s_addr
, ip
->ip_dst
.s_addr
, uh
->uh_ulen
);
325 * Make mbuf data length reflect UDP length.
326 * If not enough data to reflect UDP length, drop.
328 len
= ntohs((u_short
)uh
->uh_ulen
);
329 if (ip
->ip_len
!= len
) {
330 if (len
> ip
->ip_len
|| len
< sizeof(struct udphdr
)) {
331 udpstat
.udps_badlen
++;
333 if (ifp
->if_udp_stat
!= NULL
)
334 atomic_add_64(&ifp
->if_udp_stat
->badlength
, 1);
338 m_adj(m
, len
- ip
->ip_len
);
339 /* ip->ip_len = len; */
342 * Save a copy of the IP header in case we want restore it
343 * for sending an ICMP error message in response.
348 * Checksum extended UDP header and data.
351 if (m
->m_pkthdr
.csum_flags
& CSUM_DATA_VALID
) {
352 if (m
->m_pkthdr
.csum_flags
& CSUM_PSEUDO_HDR
)
353 uh
->uh_sum
= m
->m_pkthdr
.csum_data
;
356 uh
->uh_sum
^= 0xffff;
360 bcopy(((struct ipovly
*)ip
)->ih_x1
, b
,
361 sizeof (((struct ipovly
*)ip
)->ih_x1
));
362 bzero(((struct ipovly
*)ip
)->ih_x1
,
363 sizeof (((struct ipovly
*)ip
)->ih_x1
));
364 ((struct ipovly
*)ip
)->ih_len
= uh
->uh_ulen
;
365 uh
->uh_sum
= in_cksum(m
, len
+ sizeof (struct ip
));
366 bcopy(b
, ((struct ipovly
*)ip
)->ih_x1
,
367 sizeof (((struct ipovly
*)ip
)->ih_x1
));
369 udp_in_cksum_stats(len
);
372 udpstat
.udps_badsum
++;
374 if (ifp
->if_udp_stat
!= NULL
)
375 atomic_add_64(&ifp
->if_udp_stat
->badchksum
, 1);
378 KERNEL_DEBUG(DBG_FNC_UDP_INPUT
| DBG_FUNC_END
, 0,0,0,0,0);
384 udpstat
.udps_nosum
++;
387 isbroadcast
= in_broadcast(ip
->ip_dst
, ifp
);
389 if (IN_MULTICAST(ntohl(ip
->ip_dst
.s_addr
)) || isbroadcast
) {
391 int reuse_sock
= 0, mcast_delivered
= 0;
393 lck_rw_lock_shared(pcbinfo
->mtx
);
395 * Deliver a multicast or broadcast datagram to *all* sockets
396 * for which the local and remote addresses and ports match
397 * those of the incoming datagram. This allows more than
398 * one process to receive multi/broadcasts on the same port.
399 * (This really ought to be done for unicast datagrams as
400 * well, but that would cause problems with existing
401 * applications that open both address-specific sockets and
402 * a wildcard socket listening to the same port -- they would
403 * end up receiving duplicates of every unicast datagram.
404 * Those applications open the multiple sockets to overcome an
405 * inadequacy of the UDP socket interface, but for backwards
406 * compatibility we avoid the problem here rather than
407 * fixing the interface. Maybe 4.5BSD will remedy this?)
412 * Construct sockaddr format source address.
414 udp_in
.sin_port
= uh
->uh_sport
;
415 udp_in
.sin_addr
= ip
->ip_src
;
417 * Locate pcb(s) for datagram.
418 * (Algorithm copied from raw_intr().)
421 udp_in6
.uin6_init_done
= udp_ip6
.uip6_init_done
= 0;
423 LIST_FOREACH(inp
, &udb
, inp_list
) {
424 if (inp
->inp_socket
== NULL
)
426 if (inp
!= sotoinpcb(inp
->inp_socket
))
427 panic("udp_input: bad so back ptr inp=%p\n", inp
);
429 if ((inp
->inp_vflag
& INP_IPV4
) == 0)
432 if (ip_restrictrecvif
&& ifp
!= NULL
&&
433 (ifp
->if_eflags
& IFEF_RESTRICTED_RECV
) &&
434 !(inp
->inp_flags
& INP_RECV_ANYIF
))
437 if ((inp
->inp_moptions
== NULL
) &&
438 (ntohl(ip
->ip_dst
.s_addr
) != INADDR_ALLHOSTS_GROUP
) &&
443 if (in_pcb_checkstate(inp
, WNT_ACQUIRE
, 0) == WNT_STOPUSING
) {
447 udp_lock(inp
->inp_socket
, 1, 0);
449 if (in_pcb_checkstate(inp
, WNT_RELEASE
, 1) == WNT_STOPUSING
) {
450 udp_unlock(inp
->inp_socket
, 1, 0);
454 if (inp
->inp_lport
!= uh
->uh_dport
) {
455 udp_unlock(inp
->inp_socket
, 1, 0);
458 if (inp
->inp_laddr
.s_addr
!= INADDR_ANY
) {
459 if (inp
->inp_laddr
.s_addr
!=
461 udp_unlock(inp
->inp_socket
, 1, 0);
465 if (inp
->inp_faddr
.s_addr
!= INADDR_ANY
) {
466 if (inp
->inp_faddr
.s_addr
!=
468 inp
->inp_fport
!= uh
->uh_sport
) {
469 udp_unlock(inp
->inp_socket
, 1, 0);
474 if (isbroadcast
== 0 && (ntohl(ip
->ip_dst
.s_addr
) != INADDR_ALLHOSTS_GROUP
)) {
475 if((imo
= inp
->inp_moptions
) == NULL
) {
476 udp_unlock(inp
->inp_socket
, 1, 0);
479 struct sockaddr_in group
;
484 bzero(&group
, sizeof(struct sockaddr_in
));
485 group
.sin_len
= sizeof(struct sockaddr_in
);
486 group
.sin_family
= AF_INET
;
487 group
.sin_addr
= ip
->ip_dst
;
489 blocked
= imo_multi_filter(imo
, ifp
,
490 (struct sockaddr
*)&group
,
491 (struct sockaddr
*)&udp_in
);
492 if (blocked
== MCAST_PASS
)
496 if (!foundmembership
) {
497 udp_unlock(inp
->inp_socket
, 1, 0);
503 reuse_sock
= inp
->inp_socket
->so_options
& (SO_REUSEPORT
|SO_REUSEADDR
);
507 /* check AH/ESP integrity. */
508 if (ipsec_bypass
== 0) {
509 if (ipsec4_in_reject_so(m
, inp
->inp_socket
)) {
510 IPSEC_STAT_INCREMENT(ipsecstat
.in_polvio
);
511 /* do not inject data to pcb */
518 struct mbuf
*n
= NULL
;
521 n
= m_copy(m
, 0, M_COPYALL
);
523 udp_append(inp
, ip
, m
,
524 iphlen
+ sizeof(struct udphdr
),
525 &udp_in
, &udp_in6
, &udp_ip6
);
527 udp_append(inp
, ip
, m
,
528 iphlen
+ sizeof(struct udphdr
),
535 udp_unlock(inp
->inp_socket
, 1, 0);
538 * Don't look for additional matches if this one does
539 * not have either the SO_REUSEPORT or SO_REUSEADDR
540 * socket options set. This heuristic avoids searching
541 * through all pcbs in the common case of a non-shared
542 * port. It assumes that an application will never
543 * clear these options after setting them.
545 if (reuse_sock
== 0 || m
== NULL
)
549 * Expect 32-bit aligned data pointer on strict-align
552 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m
);
554 * Recompute IP and UDP header pointers for new mbuf
556 ip
= mtod(m
, struct ip
*);
557 uh
= (struct udphdr
*)(void *)((caddr_t
)ip
+ iphlen
);
559 lck_rw_done(pcbinfo
->mtx
);
561 if (mcast_delivered
== 0) {
563 * No matching pcb found; discard datagram.
564 * (No need to send an ICMP Port Unreachable
565 * for a broadcast or multicast datgram.)
567 udpstat
.udps_noportbcast
++;
569 if (ifp
->if_udp_stat
!= NULL
)
570 atomic_add_64(&ifp
->if_udp_stat
->port_unreach
, 1);
575 if (m
!= NULL
) /* free the extra copy of mbuf or skipped by IPSec */
577 KERNEL_DEBUG(DBG_FNC_UDP_INPUT
| DBG_FUNC_END
, 0,0,0,0,0);
583 * UDP to port 4500 with a payload where the first four bytes are
584 * not zero is a UDP encapsulated IPSec packet. Packets where
585 * the payload is one byte and that byte is 0xFF are NAT keepalive
586 * packets. Decapsulate the ESP packet and carry on with IPSec input
587 * or discard the NAT keep-alive.
589 if (ipsec_bypass
== 0 && (esp_udp_encap_port
& 0xFFFF) != 0 &&
590 uh
->uh_dport
== ntohs((u_short
)esp_udp_encap_port
)) {
591 int payload_len
= len
- sizeof(struct udphdr
) > 4 ? 4 : len
- sizeof(struct udphdr
);
592 if (m
->m_len
< iphlen
+ sizeof(struct udphdr
) + payload_len
) {
593 if ((m
= m_pullup(m
, iphlen
+ sizeof(struct udphdr
) + payload_len
)) == 0) {
594 udpstat
.udps_hdrops
++;
595 KERNEL_DEBUG(DBG_FNC_UDP_INPUT
| DBG_FUNC_END
, 0,0,0,0,0);
599 * Expect 32-bit aligned data pointer on strict-align
602 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m
);
604 ip
= mtod(m
, struct ip
*);
605 uh
= (struct udphdr
*)(void *)((caddr_t
)ip
+ iphlen
);
607 /* Check for NAT keepalive packet */
608 if (payload_len
== 1 && *(u_int8_t
*)((caddr_t
)uh
+ sizeof(struct udphdr
)) == 0xFF) {
610 KERNEL_DEBUG(DBG_FNC_UDP_INPUT
| DBG_FUNC_END
, 0,0,0,0,0);
613 else if (payload_len
== 4 && *(u_int32_t
*)(void *)((caddr_t
)uh
+ sizeof(struct udphdr
)) != 0) {
614 /* UDP encapsulated IPSec packet to pass through NAT */
615 KERNEL_DEBUG(DBG_FNC_UDP_INPUT
| DBG_FUNC_END
, 0,0,0,0,0);
616 /* preserve the udp header */
617 esp4_input(m
, iphlen
+ sizeof(struct udphdr
));
624 * Locate pcb for datagram.
626 inp
= in_pcblookup_hash(&udbinfo
, ip
->ip_src
, uh
->uh_sport
,
627 ip
->ip_dst
, uh
->uh_dport
, 1, ifp
);
630 if (ifp
->if_udp_stat
!= NULL
)
631 atomic_add_64(&ifp
->if_udp_stat
->port_unreach
, 1);
634 char buf
[MAX_IPv4_STR_LEN
];
635 char buf2
[MAX_IPv4_STR_LEN
];
637 /* check src and dst address */
638 if (log_in_vain
!= 3)
640 "Connection attempt to UDP %s:%d from %s:%d\n",
641 inet_ntop(AF_INET
, &ip
->ip_dst
, buf
, sizeof(buf
)),
643 inet_ntop(AF_INET
, &ip
->ip_src
, buf2
, sizeof(buf2
)),
644 ntohs(uh
->uh_sport
));
645 else if (!(m
->m_flags
& (M_BCAST
| M_MCAST
)) &&
646 ip
->ip_dst
.s_addr
!= ip
->ip_src
.s_addr
)
647 log_in_vain_log((LOG_INFO
,
648 "Stealth Mode connection attempt to UDP %s:%d from %s:%d\n",
649 inet_ntop(AF_INET
, &ip
->ip_dst
, buf
, sizeof(buf
)),
651 inet_ntop(AF_INET
, &ip
->ip_src
, buf2
, sizeof(buf2
)),
652 ntohs(uh
->uh_sport
)))
654 udpstat
.udps_noport
++;
655 if (m
->m_flags
& (M_BCAST
| M_MCAST
)) {
656 udpstat
.udps_noportbcast
++;
660 if (badport_bandlim(BANDLIM_ICMP_UNREACH
) < 0)
664 if (ifp
&& ifp
->if_type
!= IFT_LOOP
)
667 ip
->ip_len
+= iphlen
;
668 icmp_error(m
, ICMP_UNREACH
, ICMP_UNREACH_PORT
, 0, 0);
669 KERNEL_DEBUG(DBG_FNC_UDP_INPUT
| DBG_FUNC_END
, 0,0,0,0,0);
672 udp_lock(inp
->inp_socket
, 1, 0);
674 if (in_pcb_checkstate(inp
, WNT_RELEASE
, 1) == WNT_STOPUSING
) {
675 udp_unlock(inp
->inp_socket
, 1, 0);
677 if (ifp
->if_udp_stat
!= NULL
)
678 atomic_add_64(&ifp
->if_udp_stat
->cleanup
, 1);
683 if (ipsec_bypass
== 0 && inp
!= NULL
) {
684 if (ipsec4_in_reject_so(m
, inp
->inp_socket
)) {
685 IPSEC_STAT_INCREMENT(ipsecstat
.in_polvio
);
686 udp_unlock(inp
->inp_socket
, 1, 0);
688 if (ifp
->if_udp_stat
!= NULL
)
689 atomic_add_64(&ifp
->if_udp_stat
->badipsec
, 1);
697 * Construct sockaddr format source address.
698 * Stuff source address and datagram in user buffer.
700 udp_in
.sin_port
= uh
->uh_sport
;
701 udp_in
.sin_addr
= ip
->ip_src
;
702 if ((inp
->inp_flags
& INP_CONTROLOPTS
) != 0
703 || (inp
->inp_socket
->so_options
& SO_TIMESTAMP
) != 0
704 || (inp
->inp_socket
->so_options
& SO_TIMESTAMP_MONOTONIC
) != 0) {
706 if (inp
->inp_vflag
& INP_IPV6
) {
709 ip_2_ip6_hdr(&udp_ip6
.uip6_ip6
, ip
);
710 savedflags
= inp
->inp_flags
;
711 inp
->inp_flags
&= ~INP_UNMAPPABLEOPTS
;
712 ret
= ip6_savecontrol(inp
, m
, &opts
);
713 inp
->inp_flags
= savedflags
;
717 ret
= ip_savecontrol(inp
, &opts
, ip
, m
);
720 udp_unlock(inp
->inp_socket
, 1, 0);
724 m_adj(m
, iphlen
+ sizeof(struct udphdr
));
726 KERNEL_DEBUG(DBG_LAYER_IN_END
, uh
->uh_dport
, uh
->uh_sport
,
727 save_ip
.ip_src
.s_addr
, save_ip
.ip_dst
.s_addr
, uh
->uh_ulen
);
730 if (inp
->inp_vflag
& INP_IPV6
) {
731 in6_sin_2_v4mapsin6(&udp_in
, &udp_in6
.uin6_sin
);
732 append_sa
= (struct sockaddr
*)&udp_in6
.uin6_sin
;
735 append_sa
= (struct sockaddr
*)&udp_in
;
737 locked_add_64(&inp
->inp_stat
->rxpackets
, 1);
738 locked_add_64(&inp
->inp_stat
->rxbytes
, m
->m_pkthdr
.len
);
740 so_recv_data_stat(inp
->inp_socket
, m
, 0);
741 if (sbappendaddr(&inp
->inp_socket
->so_rcv
, append_sa
, m
, opts
, NULL
) == 0) {
742 udpstat
.udps_fullsock
++;
744 sorwakeup(inp
->inp_socket
);
746 udp_unlock(inp
->inp_socket
, 1, 0);
747 KERNEL_DEBUG(DBG_FNC_UDP_INPUT
| DBG_FUNC_END
, 0,0,0,0,0);
753 KERNEL_DEBUG(DBG_FNC_UDP_INPUT
| DBG_FUNC_END
, 0,0,0,0,0);
759 ip_2_ip6_hdr(ip6
, ip
)
763 bzero(ip6
, sizeof(*ip6
));
765 ip6
->ip6_vfc
= IPV6_VERSION
;
766 ip6
->ip6_plen
= ip
->ip_len
;
767 ip6
->ip6_nxt
= ip
->ip_p
;
768 ip6
->ip6_hlim
= ip
->ip_ttl
;
769 if (ip
->ip_src
.s_addr
) {
770 ip6
->ip6_src
.s6_addr32
[2] = IPV6_ADDR_INT32_SMP
;
771 ip6
->ip6_src
.s6_addr32
[3] = ip
->ip_src
.s_addr
;
773 if (ip
->ip_dst
.s_addr
) {
774 ip6
->ip6_dst
.s6_addr32
[2] = IPV6_ADDR_INT32_SMP
;
775 ip6
->ip6_dst
.s6_addr32
[3] = ip
->ip_dst
.s_addr
;
781 * subroutine of udp_input(), mainly for source code readability.
785 udp_append(struct inpcb
*last
, struct ip
*ip
, struct mbuf
*n
, int off
,
786 struct sockaddr_in
*pudp_in
, struct udp_in6
*pudp_in6
,
787 struct udp_ip6
*pudp_ip6
)
789 udp_append(struct inpcb
*last
, struct ip
*ip
, struct mbuf
*n
, int off
,
790 struct sockaddr_in
*pudp_in
)
793 struct sockaddr
*append_sa
;
794 struct mbuf
*opts
= 0;
798 if (mac_inpcb_check_deliver(last
, n
, AF_INET
, SOCK_DGRAM
) != 0) {
803 if ((last
->inp_flags
& INP_CONTROLOPTS
) != 0 ||
804 (last
->inp_socket
->so_options
& SO_TIMESTAMP
) != 0 ||
805 (last
->inp_socket
->so_options
& SO_TIMESTAMP_MONOTONIC
) != 0) {
807 if (last
->inp_vflag
& INP_IPV6
) {
810 if (pudp_ip6
->uip6_init_done
== 0) {
811 ip_2_ip6_hdr(&pudp_ip6
->uip6_ip6
, ip
);
812 pudp_ip6
->uip6_init_done
= 1;
814 savedflags
= last
->inp_flags
;
815 last
->inp_flags
&= ~INP_UNMAPPABLEOPTS
;
816 ret
= ip6_savecontrol(last
, n
, &opts
);
818 last
->inp_flags
= savedflags
;
821 last
->inp_flags
= savedflags
;
825 ret
= ip_savecontrol(last
, &opts
, ip
, n
);
832 if (last
->inp_vflag
& INP_IPV6
) {
833 if (pudp_in6
->uin6_init_done
== 0) {
834 in6_sin_2_v4mapsin6(pudp_in
, &pudp_in6
->uin6_sin
);
835 pudp_in6
->uin6_init_done
= 1;
837 append_sa
= (struct sockaddr
*)&pudp_in6
->uin6_sin
;
840 append_sa
= (struct sockaddr
*)pudp_in
;
842 locked_add_64(&last
->inp_stat
->rxpackets
, 1);
843 locked_add_64(&last
->inp_stat
->rxbytes
, n
->m_pkthdr
.len
);
845 so_recv_data_stat(last
->inp_socket
, n
, 0);
847 if (sbappendaddr(&last
->inp_socket
->so_rcv
, append_sa
, n
, opts
, NULL
) == 0) {
848 udpstat
.udps_fullsock
++;
850 sorwakeup(last
->inp_socket
);
860 * Notify a udp user of an asynchronous error;
861 * just wake up so that he can collect error status.
864 udp_notify(inp
, errno
)
865 register struct inpcb
*inp
;
868 inp
->inp_socket
->so_error
= errno
;
869 sorwakeup(inp
->inp_socket
);
870 sowwakeup(inp
->inp_socket
);
874 udp_ctlinput(cmd
, sa
, vip
)
880 void (*notify
)(struct inpcb
*, int) = udp_notify
;
881 struct in_addr faddr
;
884 faddr
= ((struct sockaddr_in
*)(void *)sa
)->sin_addr
;
885 if (sa
->sa_family
!= AF_INET
|| faddr
.s_addr
== INADDR_ANY
)
888 if (PRC_IS_REDIRECT(cmd
)) {
890 notify
= in_rtchange
;
891 } else if (cmd
== PRC_HOSTDEAD
)
893 else if ((unsigned)cmd
>= PRC_NCMDS
|| inetctlerrmap
[cmd
] == 0)
898 bcopy(((caddr_t
)ip
+ (ip
->ip_hl
<< 2)), &uh
, sizeof (uh
));
899 inp
= in_pcblookup_hash(&udbinfo
, faddr
, uh
.uh_dport
,
900 ip
->ip_src
, uh
.uh_sport
, 0, NULL
);
901 if (inp
!= NULL
&& inp
->inp_socket
!= NULL
) {
902 udp_lock(inp
->inp_socket
, 1, 0);
903 if (in_pcb_checkstate(inp
, WNT_RELEASE
, 1) ==
905 udp_unlock(inp
->inp_socket
, 1, 0);
908 (*notify
)(inp
, inetctlerrmap
[cmd
]);
909 udp_unlock(inp
->inp_socket
, 1, 0);
912 in_pcbnotifyall(&udbinfo
, faddr
, inetctlerrmap
[cmd
], notify
);
916 udp_ctloutput(struct socket
*so
, struct sockopt
*sopt
)
921 /* Allow <SOL_SOCKET,SO_FLUSH> at this level */
922 if (sopt
->sopt_level
!= IPPROTO_UDP
&&
923 !(sopt
->sopt_level
== SOL_SOCKET
&& sopt
->sopt_name
== SO_FLUSH
))
924 return (ip_ctloutput(so
, sopt
));
929 switch (sopt
->sopt_dir
) {
931 switch (sopt
->sopt_name
) {
933 /* This option is settable only for UDP over IPv4 */
934 if (!(inp
->inp_vflag
& INP_IPV4
)) {
939 if ((error
= sooptcopyin(sopt
, &optval
, sizeof (optval
),
940 sizeof (optval
))) != 0)
944 inp
->inp_flags
|= INP_UDP_NOCKSUM
;
946 inp
->inp_flags
&= ~INP_UDP_NOCKSUM
;
950 if ((error
= sooptcopyin(sopt
, &optval
, sizeof (optval
),
951 sizeof (optval
))) != 0)
954 error
= inp_flush(inp
, optval
);
964 switch (sopt
->sopt_name
) {
966 optval
= inp
->inp_flags
& INP_UDP_NOCKSUM
;
974 error
= sooptcopyout(sopt
, &optval
, sizeof (optval
));
981 udp_pcblist SYSCTL_HANDLER_ARGS
983 #pragma unused(oidp, arg1, arg2)
985 struct inpcb
*inp
, **inp_list
;
990 * The process of preparing the TCB list is too time-consuming and
991 * resource-intensive to repeat twice on every request.
993 lck_rw_lock_exclusive(udbinfo
.mtx
);
994 if (req
->oldptr
== USER_ADDR_NULL
) {
995 n
= udbinfo
.ipi_count
;
996 req
->oldidx
= 2 * (sizeof xig
)
997 + (n
+ n
/8) * sizeof(struct xinpcb
);
998 lck_rw_done(udbinfo
.mtx
);
1002 if (req
->newptr
!= USER_ADDR_NULL
) {
1003 lck_rw_done(udbinfo
.mtx
);
1008 * OK, now we're committed to doing something.
1010 gencnt
= udbinfo
.ipi_gencnt
;
1011 n
= udbinfo
.ipi_count
;
1013 bzero(&xig
, sizeof(xig
));
1014 xig
.xig_len
= sizeof xig
;
1016 xig
.xig_gen
= gencnt
;
1017 xig
.xig_sogen
= so_gencnt
;
1018 error
= SYSCTL_OUT(req
, &xig
, sizeof xig
);
1020 lck_rw_done(udbinfo
.mtx
);
1024 * We are done if there is no pcb
1027 lck_rw_done(udbinfo
.mtx
);
1031 inp_list
= _MALLOC(n
* sizeof *inp_list
, M_TEMP
, M_WAITOK
);
1032 if (inp_list
== 0) {
1033 lck_rw_done(udbinfo
.mtx
);
1037 for (inp
= LIST_FIRST(udbinfo
.listhead
), i
= 0; inp
&& i
< n
;
1038 inp
= LIST_NEXT(inp
, inp_list
)) {
1039 if (inp
->inp_gencnt
<= gencnt
&& inp
->inp_state
!= INPCB_STATE_DEAD
)
1040 inp_list
[i
++] = inp
;
1045 for (i
= 0; i
< n
; i
++) {
1047 if (inp
->inp_gencnt
<= gencnt
&& inp
->inp_state
!= INPCB_STATE_DEAD
) {
1050 bzero(&xi
, sizeof(xi
));
1051 xi
.xi_len
= sizeof xi
;
1052 /* XXX should avoid extra copy */
1053 inpcb_to_compat(inp
, &xi
.xi_inp
);
1054 if (inp
->inp_socket
)
1055 sotoxsocket(inp
->inp_socket
, &xi
.xi_socket
);
1056 error
= SYSCTL_OUT(req
, &xi
, sizeof xi
);
1061 * Give the user an updated idea of our state.
1062 * If the generation differs from what we told
1063 * her before, she knows that something happened
1064 * while we were processing this request, and it
1065 * might be necessary to retry.
1067 bzero(&xig
, sizeof(xig
));
1068 xig
.xig_len
= sizeof xig
;
1069 xig
.xig_gen
= udbinfo
.ipi_gencnt
;
1070 xig
.xig_sogen
= so_gencnt
;
1071 xig
.xig_count
= udbinfo
.ipi_count
;
1072 error
= SYSCTL_OUT(req
, &xig
, sizeof xig
);
1074 FREE(inp_list
, M_TEMP
);
1075 lck_rw_done(udbinfo
.mtx
);
1079 SYSCTL_PROC(_net_inet_udp
, UDPCTL_PCBLIST
, pcblist
, CTLFLAG_RD
| CTLFLAG_LOCKED
, 0, 0,
1080 udp_pcblist
, "S,xinpcb", "List of active UDP sockets");
1082 #if !CONFIG_EMBEDDED
1085 udp_pcblist64 SYSCTL_HANDLER_ARGS
1087 #pragma unused(oidp, arg1, arg2)
1089 struct inpcb
*inp
, **inp_list
;
1094 * The process of preparing the TCB list is too time-consuming and
1095 * resource-intensive to repeat twice on every request.
1097 lck_rw_lock_shared(udbinfo
.mtx
);
1098 if (req
->oldptr
== USER_ADDR_NULL
) {
1099 n
= udbinfo
.ipi_count
;
1100 req
->oldidx
= 2 * (sizeof xig
)
1101 + (n
+ n
/8) * sizeof(struct xinpcb64
);
1102 lck_rw_done(udbinfo
.mtx
);
1106 if (req
->newptr
!= USER_ADDR_NULL
) {
1107 lck_rw_done(udbinfo
.mtx
);
1112 * OK, now we're committed to doing something.
1114 gencnt
= udbinfo
.ipi_gencnt
;
1115 n
= udbinfo
.ipi_count
;
1117 bzero(&xig
, sizeof(xig
));
1118 xig
.xig_len
= sizeof xig
;
1120 xig
.xig_gen
= gencnt
;
1121 xig
.xig_sogen
= so_gencnt
;
1122 error
= SYSCTL_OUT(req
, &xig
, sizeof xig
);
1124 lck_rw_done(udbinfo
.mtx
);
1128 * We are done if there is no pcb
1131 lck_rw_done(udbinfo
.mtx
);
1135 inp_list
= _MALLOC(n
* sizeof *inp_list
, M_TEMP
, M_WAITOK
);
1136 if (inp_list
== 0) {
1137 lck_rw_done(udbinfo
.mtx
);
1141 for (inp
= LIST_FIRST(udbinfo
.listhead
), i
= 0; inp
&& i
< n
;
1142 inp
= LIST_NEXT(inp
, inp_list
)) {
1143 if (inp
->inp_gencnt
<= gencnt
&& inp
->inp_state
!= INPCB_STATE_DEAD
)
1144 inp_list
[i
++] = inp
;
1149 for (i
= 0; i
< n
; i
++) {
1151 if (inp
->inp_gencnt
<= gencnt
&& inp
->inp_state
!= INPCB_STATE_DEAD
) {
1154 bzero(&xi
, sizeof(xi
));
1155 xi
.xi_len
= sizeof xi
;
1156 inpcb_to_xinpcb64(inp
, &xi
);
1157 if (inp
->inp_socket
)
1158 sotoxsocket64(inp
->inp_socket
, &xi
.xi_socket
);
1159 error
= SYSCTL_OUT(req
, &xi
, sizeof xi
);
1164 * Give the user an updated idea of our state.
1165 * If the generation differs from what we told
1166 * her before, she knows that something happened
1167 * while we were processing this request, and it
1168 * might be necessary to retry.
1170 bzero(&xig
, sizeof(xig
));
1171 xig
.xig_len
= sizeof xig
;
1172 xig
.xig_gen
= udbinfo
.ipi_gencnt
;
1173 xig
.xig_sogen
= so_gencnt
;
1174 xig
.xig_count
= udbinfo
.ipi_count
;
1175 error
= SYSCTL_OUT(req
, &xig
, sizeof xig
);
1177 FREE(inp_list
, M_TEMP
);
1178 lck_rw_done(udbinfo
.mtx
);
1182 SYSCTL_PROC(_net_inet_udp
, OID_AUTO
, pcblist64
, CTLFLAG_RD
| CTLFLAG_LOCKED
, 0, 0,
1183 udp_pcblist64
, "S,xinpcb64", "List of active UDP sockets");
1185 #endif /* !CONFIG_EMBEDDED */
1188 udp_pcblist_n SYSCTL_HANDLER_ARGS
1190 #pragma unused(oidp, arg1, arg2)
1193 error
= get_pcblist_n(IPPROTO_UDP
, req
, &udbinfo
);
1198 SYSCTL_PROC(_net_inet_udp
, OID_AUTO
, pcblist_n
, CTLFLAG_RD
| CTLFLAG_LOCKED
, 0, 0,
1199 udp_pcblist_n
, "S,xinpcb_n", "List of active UDP sockets");
1202 __private_extern__
void
1203 udp_get_ports_used(unsigned int ifindex
, uint8_t *bitfield
)
1205 inpcb_get_ports_used(ifindex
, bitfield
, &udbinfo
);
1208 __private_extern__
uint32_t
1209 udp_count_opportunistic(unsigned int ifindex
, u_int32_t flags
)
1211 return inpcb_count_opportunistic(ifindex
, &udbinfo
, flags
);
1214 static __inline__ u_int16_t
1215 get_socket_id(struct socket
* s
)
1222 val
= (u_int16_t
)(((uintptr_t)s
) / sizeof(struct socket
));
1230 udp_check_pktinfo(struct mbuf
*control
, struct ifnet
**outif
, struct in_addr
*laddr
)
1232 struct cmsghdr
*cm
= 0;
1233 struct in_pktinfo
*pktinfo
;
1237 * XXX: Currently, we assume all the optional information is stored
1240 if (control
->m_next
)
1243 if (control
->m_len
< CMSG_LEN(0))
1246 for (cm
= M_FIRST_CMSGHDR(control
); cm
; cm
= M_NXT_CMSGHDR(control
, cm
)) {
1247 if (cm
->cmsg_len
< sizeof(struct cmsghdr
) || cm
->cmsg_len
> control
->m_len
)
1250 if (cm
->cmsg_level
!= IPPROTO_IP
|| cm
->cmsg_type
!= IP_PKTINFO
)
1253 if (cm
->cmsg_len
!= CMSG_LEN(sizeof(struct in_pktinfo
)))
1256 pktinfo
= (struct in_pktinfo
*)(void *)CMSG_DATA(cm
);
1258 /* Check for a valid ifindex in pktinfo */
1259 ifnet_head_lock_shared();
1261 if (pktinfo
->ipi_ifindex
> if_index
) {
1266 /* If ipi_ifindex is specified it takes precedence over ipi_spec_dst */
1268 if (pktinfo
->ipi_ifindex
) {
1269 ifp
= ifindex2ifnet
[pktinfo
->ipi_ifindex
];
1279 laddr
->s_addr
= INADDR_ANY
;
1285 /* Use the provided ipi_spec_dst address for temp source address */
1288 *laddr
= pktinfo
->ipi_spec_dst
;
1295 udp_output(inp
, m
, addr
, control
, p
)
1296 register struct inpcb
*inp
;
1298 struct sockaddr
*addr
;
1299 struct mbuf
*control
;
1302 register struct udpiphdr
*ui
;
1303 register int len
= m
->m_pkthdr
.len
;
1304 struct sockaddr_in
*sin
;
1305 struct in_addr origladdr
, laddr
, faddr
, pi_laddr
;
1306 u_short lport
, fport
;
1307 struct sockaddr_in ifaddr
;
1308 int error
= 0, udp_dodisconnect
= 0, pktinfo
= 0;
1309 struct socket
*so
= inp
->inp_socket
;
1311 struct mbuf
*inpopts
;
1312 struct ip_moptions
*mopts
;
1314 struct ip_out_args ipoa
= { IFSCOPE_NONE
, { 0 }, IPOAF_SELECT_SRCIF
};
1315 struct ifnet
*outif
= NULL
;
1316 struct flowadv
*adv
= &ipoa
.ipoa_flowadv
;
1317 mbuf_svc_class_t msc
= MBUF_SC_UNSPEC
;
1318 struct ifnet
*origoutifp
;
1321 /* Enable flow advisory only when connected */
1322 flowadv
= (so
->so_state
& SS_ISCONNECTED
) ? 1 : 0;
1324 pi_laddr
.s_addr
= INADDR_ANY
;
1326 KERNEL_DEBUG(DBG_FNC_UDP_OUTPUT
| DBG_FUNC_START
, 0,0,0,0,0);
1328 lck_mtx_assert(&inp
->inpcb_mtx
, LCK_MTX_ASSERT_OWNED
);
1329 if (control
!= NULL
) {
1330 msc
= mbuf_service_class_from_control(control
);
1332 error
= udp_check_pktinfo(control
, &outif
, &pi_laddr
);
1339 ipoa
.ipoa_boundif
= outif
->if_index
;
1342 KERNEL_DEBUG(DBG_LAYER_OUT_BEG
, inp
->inp_fport
, inp
->inp_lport
,
1343 inp
->inp_laddr
.s_addr
, inp
->inp_faddr
.s_addr
,
1344 (htons((u_short
)len
+ sizeof (struct udphdr
))));
1346 if (len
+ sizeof(struct udpiphdr
) > IP_MAXPACKET
) {
1351 if (flowadv
&& INP_WAIT_FOR_IF_FEEDBACK(inp
)) {
1353 * The socket is flow-controlled, drop the packets
1354 * until the inp is not flow controlled
1360 * If socket was bound to an ifindex, tell ip_output about it.
1361 * If the ancillary IP_PKTINFO option contains an interface index,
1362 * it takes precedence over the one specified by IP_BOUND_IF.
1364 if (ipoa
.ipoa_boundif
== IFSCOPE_NONE
&&
1365 (inp
->inp_flags
& INP_BOUND_IF
)) {
1366 outif
= inp
->inp_boundifp
;
1367 ipoa
.ipoa_boundif
= outif
->if_index
;
1369 if (inp
->inp_flags
& INP_NO_IFT_CELLULAR
)
1370 ipoa
.ipoa_flags
|= IPOAF_NO_CELLULAR
;
1371 soopts
|= IP_OUTARGS
;
1373 /* If there was a routing change, discard cached route and check
1374 * that we have a valid source address.
1375 * Reacquire a new source address if INADDR_ANY was specified
1377 if (inp
->inp_route
.ro_rt
!= NULL
&&
1378 inp
->inp_route
.ro_rt
->generation_id
!= route_generation
) {
1379 struct in_ifaddr
*ia
;
1381 /* src address is gone? */
1382 if ((ia
= ifa_foraddr(inp
->inp_laddr
.s_addr
)) == NULL
) {
1383 if (((inp
->inp_flags
& INP_INADDR_ANY
) == 0) ||
1384 (so
->so_state
& SS_ISCONNECTED
)) {
1386 * If the source address is gone, return an error if:
1387 * - the source was specified
1388 * - the socket was already connected
1391 (SO_FILT_HINT_LOCKED
|
1392 SO_FILT_HINT_NOSRCADDR
));
1393 error
= EADDRNOTAVAIL
;
1396 /* new src will be set later */
1397 inp
->inp_laddr
.s_addr
= INADDR_ANY
;
1398 inp
->inp_last_outifp
= NULL
;
1402 IFA_REMREF(&ia
->ia_ifa
);
1403 if (inp
->inp_route
.ro_rt
!= NULL
)
1404 rtfree(inp
->inp_route
.ro_rt
);
1405 inp
->inp_route
.ro_rt
= NULL
;
1408 origoutifp
= inp
->inp_last_outifp
;
1410 /* IP_PKTINFO option check.
1411 * If a temporary scope or src address is provided, use it for this packet only
1412 * and make sure we forget it after sending this datagram.
1415 if (pi_laddr
.s_addr
!= INADDR_ANY
||
1416 (ipoa
.ipoa_boundif
!= IFSCOPE_NONE
&& pktinfo
)) {
1417 laddr
= pi_laddr
; /* temp src address for this datagram only */
1418 origladdr
.s_addr
= INADDR_ANY
;
1419 udp_dodisconnect
= 1; /* we don't want to keep the laddr or route */
1420 inp
->inp_flags
|= INP_INADDR_ANY
; /* remember we don't care about src addr.*/
1422 origladdr
= laddr
= inp
->inp_laddr
;
1425 origoutifp
= inp
->inp_last_outifp
;
1426 faddr
= inp
->inp_faddr
;
1427 lport
= inp
->inp_lport
;
1428 fport
= inp
->inp_fport
;
1431 sin
= (struct sockaddr_in
*)(void *)addr
;
1432 if (faddr
.s_addr
!= INADDR_ANY
) {
1438 * In case we don't have a local port set, go through the full connect.
1439 * We don't have a local port yet (ie, we can't be looked up),
1440 * so it's not an issue if the input runs at the same time we do this.
1443 if (pi_laddr
.s_addr
!= INADDR_ANY
) /* if we have a source address specified, use that */
1444 inp
->inp_laddr
= pi_laddr
;
1445 error
= in_pcbconnect(inp
, addr
, p
, &outif
); /* if a scope is specified, use it */
1449 laddr
= inp
->inp_laddr
;
1450 lport
= inp
->inp_lport
;
1451 faddr
= inp
->inp_faddr
;
1452 fport
= inp
->inp_fport
;
1453 udp_dodisconnect
= 1;
1454 ipoa
.ipoa_boundif
= (outif
!= NULL
) ?
1455 outif
->if_index
: IFSCOPE_NONE
;
1459 * we have a full address and a local port.
1460 * use those info to build the packet without changing the pcb
1461 * and interfering with the input path. See 3851370
1462 * Note: if we may have a scope from IP_PKTINFO but the
1463 * priority is always given to the scope provided by INP_BOUND_IF.
1465 if (laddr
.s_addr
== INADDR_ANY
) {
1466 if ((error
= in_pcbladdr(inp
, addr
, &ifaddr
, &outif
)) != 0)
1468 laddr
= ifaddr
.sin_addr
;
1469 inp
->inp_flags
|= INP_INADDR_ANY
; /* from pcbconnect: remember we don't care about src addr.*/
1470 ipoa
.ipoa_boundif
= (outif
!= NULL
) ?
1471 outif
->if_index
: IFSCOPE_NONE
;
1474 faddr
= sin
->sin_addr
;
1475 fport
= sin
->sin_port
;
1478 if (faddr
.s_addr
== INADDR_ANY
) {
1485 mac_mbuf_label_associate_inpcb(inp
, m
);
1487 if (inp
->inp_flowhash
== 0)
1488 inp
->inp_flowhash
= inp_calc_flowhash(inp
);
1491 * Calculate data length and get a mbuf
1492 * for UDP and IP headers.
1494 M_PREPEND(m
, sizeof(struct udpiphdr
), M_DONTWAIT
);
1501 * Fill in mbuf with extended UDP header
1502 * and addresses and length put into network format.
1504 ui
= mtod(m
, struct udpiphdr
*);
1505 bzero(ui
->ui_x1
, sizeof(ui
->ui_x1
)); /* XXX still needed? */
1506 ui
->ui_pr
= IPPROTO_UDP
;
1509 ui
->ui_sport
= lport
;
1510 ui
->ui_dport
= fport
;
1511 ui
->ui_ulen
= htons((u_short
)len
+ sizeof(struct udphdr
));
1514 * Set up checksum and output datagram.
1516 if (udpcksum
&& !(inp
->inp_flags
& INP_UDP_NOCKSUM
)) {
1517 ui
->ui_sum
= in_pseudo(ui
->ui_src
.s_addr
, ui
->ui_dst
.s_addr
,
1518 htons((u_short
)len
+ sizeof(struct udphdr
) + IPPROTO_UDP
));
1519 m
->m_pkthdr
.csum_flags
= CSUM_UDP
;
1520 m
->m_pkthdr
.csum_data
= offsetof(struct udphdr
, uh_sum
);
1524 ((struct ip
*)ui
)->ip_len
= sizeof (struct udpiphdr
) + len
;
1525 ((struct ip
*)ui
)->ip_ttl
= inp
->inp_ip_ttl
; /* XXX */
1526 ((struct ip
*)ui
)->ip_tos
= inp
->inp_ip_tos
; /* XXX */
1527 udpstat
.udps_opackets
++;
1529 KERNEL_DEBUG(DBG_LAYER_OUT_END
, ui
->ui_dport
, ui
->ui_sport
,
1530 ui
->ui_src
.s_addr
, ui
->ui_dst
.s_addr
, ui
->ui_ulen
);
1533 if (ipsec_bypass
== 0 && ipsec_setsocket(m
, inp
->inp_socket
) != 0) {
1539 inpopts
= inp
->inp_options
;
1540 soopts
|= (inp
->inp_socket
->so_options
& (SO_DONTROUTE
| SO_BROADCAST
));
1541 mopts
= inp
->inp_moptions
;
1542 if (mopts
!= NULL
) {
1544 IMO_ADDREF_LOCKED(mopts
);
1545 if (IN_MULTICAST(ntohl(ui
->ui_dst
.s_addr
)) &&
1546 mopts
->imo_multicast_ifp
!= NULL
) {
1547 inp
->inp_last_outifp
= mopts
->imo_multicast_ifp
;
1552 /* Copy the cached route and take an extra reference */
1553 inp_route_copyout(inp
, &ro
);
1555 set_packet_service_class(m
, so
, msc
, 0);
1556 m
->m_pkthdr
.socket_id
= get_socket_id(inp
->inp_socket
);
1557 m
->m_pkthdr
.m_flowhash
= inp
->inp_flowhash
;
1558 m
->m_pkthdr
.m_fhflags
|= PF_TAG_FLOWHASH
;
1560 m
->m_pkthdr
.m_fhflags
|= PF_TAG_FLOWADV
;
1562 if (ipoa
.ipoa_boundif
!= IFSCOPE_NONE
)
1563 ipoa
.ipoa_flags
|= IPOAF_BOUND_IF
;
1565 if (laddr
.s_addr
!= INADDR_ANY
)
1566 ipoa
.ipoa_flags
|= IPOAF_BOUND_SRCADDR
;
1568 inp
->inp_sndinprog_cnt
++;
1570 socket_unlock(so
, 0);
1571 error
= ip_output_list(m
, 0, inpopts
, &ro
, soopts
, mopts
, &ipoa
);
1577 if (error
== 0 && nstat_collect
) {
1578 locked_add_64(&inp
->inp_stat
->txpackets
, 1);
1579 locked_add_64(&inp
->inp_stat
->txbytes
, len
);
1582 if (flowadv
&& (adv
->code
== FADV_FLOW_CONTROLLED
||
1583 adv
->code
== FADV_SUSPENDED
)) {
1584 /* return a hint to the application that
1585 * the packet has been dropped
1588 inp_set_fc_state(inp
, adv
->code
);
1591 VERIFY(inp
->inp_sndinprog_cnt
> 0);
1592 if ( --inp
->inp_sndinprog_cnt
== 0)
1593 inp
->inp_flags
&= ~(INP_FC_FEEDBACK
);
1595 /* Synchronize PCB cached route */
1596 inp_route_copyin(inp
, &ro
);
1599 if (udp_dodisconnect
) {
1600 /* Always discard the cached route for unconnected socket */
1601 if (inp
->inp_route
.ro_rt
!= NULL
) {
1602 rtfree(inp
->inp_route
.ro_rt
);
1603 inp
->inp_route
.ro_rt
= NULL
;
1605 in_pcbdisconnect(inp
);
1606 inp
->inp_laddr
= origladdr
; /* XXX rehash? */
1607 inp
->inp_last_outifp
= origoutifp
;
1608 } else if (inp
->inp_route
.ro_rt
!= NULL
) {
1609 struct rtentry
*rt
= inp
->inp_route
.ro_rt
;
1610 struct ifnet
*outifp
;
1612 if (rt
->rt_flags
& (RTF_MULTICAST
|RTF_BROADCAST
))
1613 rt
= NULL
; /* unusable */
1615 * Always discard if it is a multicast or broadcast route.
1618 rtfree(inp
->inp_route
.ro_rt
);
1619 inp
->inp_route
.ro_rt
= NULL
;
1622 * If the destination route is unicast, update outifp with
1623 * that of the route interface used by IP.
1625 if (rt
!= NULL
&& (outifp
= rt
->rt_ifp
) != inp
->inp_last_outifp
)
1626 inp
->inp_last_outifp
= outifp
;
1632 KERNEL_DEBUG(DBG_FNC_UDP_OUTPUT
| DBG_FUNC_END
, error
, 0,0,0,0);
1636 u_int32_t udp_sendspace
= 9216; /* really max datagram size */
1637 /* 187 1K datagrams (approx 192 KB) */
1638 u_int32_t udp_recvspace
= 187 * (1024 +
1640 sizeof(struct sockaddr_in6
)
1642 sizeof(struct sockaddr_in
)
1646 /* Check that the values of udp send and recv space do not exceed sb_max */
1648 sysctl_udp_sospace(struct sysctl_oid
*oidp
, __unused
void *arg1
,
1649 __unused
int arg2
, struct sysctl_req
*req
) {
1650 u_int32_t new_value
= 0, *space_p
= NULL
;
1651 int changed
= 0, error
= 0;
1652 u_quad_t sb_effective_max
= (sb_max
/ (MSIZE
+MCLBYTES
)) * MCLBYTES
;
1654 switch (oidp
->oid_number
) {
1655 case UDPCTL_RECVSPACE
:
1656 space_p
= &udp_recvspace
;
1658 case UDPCTL_MAXDGRAM
:
1659 space_p
= &udp_sendspace
;
1664 error
= sysctl_io_number(req
, *space_p
, sizeof(u_int32_t
),
1665 &new_value
, &changed
);
1667 if (new_value
> 0 && new_value
<= sb_effective_max
) {
1668 *space_p
= new_value
;
1676 SYSCTL_PROC(_net_inet_udp
, UDPCTL_RECVSPACE
, recvspace
, CTLTYPE_INT
| CTLFLAG_RW
| CTLFLAG_LOCKED
,
1677 &udp_recvspace
, 0, &sysctl_udp_sospace
, "IU", "Maximum incoming UDP datagram size");
1679 SYSCTL_PROC(_net_inet_udp
, UDPCTL_MAXDGRAM
, maxdgram
, CTLTYPE_INT
| CTLFLAG_RW
| CTLFLAG_LOCKED
,
1680 &udp_sendspace
, 0, &sysctl_udp_sospace
, "IU", "Maximum outgoing UDP datagram size");
1683 udp_abort(struct socket
*so
)
1687 inp
= sotoinpcb(so
);
1689 panic("udp_abort: so=%p null inp\n", so
); /* ??? possible? panic instead? */
1690 soisdisconnected(so
);
1696 udp_attach(struct socket
*so
, __unused
int proto
, struct proc
*p
)
1701 inp
= sotoinpcb(so
);
1703 panic ("udp_attach so=%p inp=%p\n", so
, inp
);
1705 error
= in_pcballoc(so
, &udbinfo
, p
);
1708 error
= soreserve(so
, udp_sendspace
, udp_recvspace
);
1711 inp
= (struct inpcb
*)so
->so_pcb
;
1712 inp
->inp_vflag
|= INP_IPV4
;
1713 inp
->inp_ip_ttl
= ip_defttl
;
1715 nstat_udp_new_pcb(inp
);
1720 udp_bind(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
1725 if (nam
->sa_family
!= 0 && nam
->sa_family
!= AF_INET
1726 && nam
->sa_family
!= AF_INET6
) {
1727 return EAFNOSUPPORT
;
1729 inp
= sotoinpcb(so
);
1732 error
= in_pcbbind(inp
, nam
, p
);
1737 udp_connect(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
1742 inp
= sotoinpcb(so
);
1745 if (inp
->inp_faddr
.s_addr
!= INADDR_ANY
)
1747 error
= in_pcbconnect(inp
, nam
, p
, NULL
);
1750 if (inp
->inp_flowhash
== 0)
1751 inp
->inp_flowhash
= inp_calc_flowhash(inp
);
1757 udp_detach(struct socket
*so
)
1761 inp
= sotoinpcb(so
);
1763 panic("udp_detach: so=%p null inp\n", so
); /* ??? possible? panic instead? */
1765 inp
->inp_state
= INPCB_STATE_DEAD
;
1770 udp_disconnect(struct socket
*so
)
1774 inp
= sotoinpcb(so
);
1777 if (inp
->inp_faddr
.s_addr
== INADDR_ANY
)
1780 in_pcbdisconnect(inp
);
1782 /* reset flow controlled state, just in case */
1783 inp_reset_fc_state(inp
);
1785 inp
->inp_laddr
.s_addr
= INADDR_ANY
;
1786 so
->so_state
&= ~SS_ISCONNECTED
; /* XXX */
1787 inp
->inp_last_outifp
= NULL
;
1792 udp_send(struct socket
*so
, __unused
int flags
, struct mbuf
*m
, struct sockaddr
*addr
,
1793 struct mbuf
*control
, struct proc
*p
)
1797 inp
= sotoinpcb(so
);
1803 return udp_output(inp
, m
, addr
, control
, p
);
1807 udp_shutdown(struct socket
*so
)
1811 inp
= sotoinpcb(so
);
1818 struct pr_usrreqs udp_usrreqs
= {
1819 udp_abort
, pru_accept_notsupp
, udp_attach
, udp_bind
, udp_connect
,
1820 pru_connect2_notsupp
, in_control
, udp_detach
, udp_disconnect
,
1821 pru_listen_notsupp
, in_setpeeraddr
, pru_rcvd_notsupp
,
1822 pru_rcvoob_notsupp
, udp_send
, pru_sense_null
, udp_shutdown
,
1823 in_setsockaddr
, sosend
, soreceive
, pru_sopoll_notsupp
1828 udp_lock(struct socket
*so
, int refcount
, void *debug
)
1833 lr_saved
= __builtin_return_address(0);
1838 lck_mtx_assert(&((struct inpcb
*)so
->so_pcb
)->inpcb_mtx
,
1839 LCK_MTX_ASSERT_NOTOWNED
);
1840 lck_mtx_lock(&((struct inpcb
*)so
->so_pcb
)->inpcb_mtx
);
1842 panic("udp_lock: so=%p NO PCB! lr=%p lrh= %s\n",
1843 so
, lr_saved
, solockhistory_nr(so
));
1849 so
->lock_lr
[so
->next_lock_lr
] = lr_saved
;
1850 so
->next_lock_lr
= (so
->next_lock_lr
+1) % SO_LCKDBG_MAX
;
1855 udp_unlock(struct socket
*so
, int refcount
, void *debug
)
1860 lr_saved
= __builtin_return_address(0);
1867 if (so
->so_pcb
== NULL
) {
1868 panic("udp_unlock: so=%p NO PCB! lr=%p lrh= %s\n",
1869 so
, lr_saved
, solockhistory_nr(so
));
1872 lck_mtx_assert(&((struct inpcb
*)so
->so_pcb
)->inpcb_mtx
,
1873 LCK_MTX_ASSERT_OWNED
);
1874 so
->unlock_lr
[so
->next_unlock_lr
] = lr_saved
;
1875 so
->next_unlock_lr
= (so
->next_unlock_lr
+1) % SO_LCKDBG_MAX
;
1876 lck_mtx_unlock(&((struct inpcb
*)so
->so_pcb
)->inpcb_mtx
);
1884 udp_getlock(struct socket
*so
, __unused
int locktype
)
1886 struct inpcb
*inp
= sotoinpcb(so
);
1890 return(&inp
->inpcb_mtx
);
1892 panic("udp_getlock: so=%p NULL so_pcb lrh= %s\n",
1893 so
, solockhistory_nr(so
));
1894 return (so
->so_proto
->pr_domain
->dom_mtx
);
1901 struct inpcb
*inp
, *inpnxt
;
1903 struct inpcbinfo
*pcbinfo
= &udbinfo
;
1905 if (lck_rw_try_lock_exclusive(pcbinfo
->mtx
) == FALSE
) {
1906 if (udp_gc_done
== TRUE
) {
1907 udp_gc_done
= FALSE
;
1908 return; /* couldn't get the lock, better lock next time */
1910 lck_rw_lock_exclusive(pcbinfo
->mtx
);
1915 for (inp
= udb
.lh_first
; inp
!= NULL
; inp
= inpnxt
) {
1916 inpnxt
= inp
->inp_list
.le_next
;
1918 if (inp
->inp_wantcnt
!= WNT_STOPUSING
)
1921 so
= inp
->inp_socket
;
1922 if (!lck_mtx_try_lock(&inp
->inpcb_mtx
)) /* skip if busy, no hurry for cleanup... */
1925 if (so
->so_usecount
== 0) {
1926 if (inp
->inp_state
!= INPCB_STATE_DEAD
) {
1928 if (INP_CHECK_SOCKAF(so
, AF_INET6
))
1936 lck_mtx_unlock(&inp
->inpcb_mtx
);
1939 lck_rw_done(pcbinfo
->mtx
);
1943 ChkAddressOK( __uint32_t dstaddr
, __uint32_t srcaddr
)
1945 if ( dstaddr
== srcaddr
){
1952 udp_in_cksum_stats(u_int32_t len
)
1955 udps_in_sw_cksum_bytes
+= len
;
1959 udp_out_cksum_stats(u_int32_t len
)
1961 udps_out_sw_cksum
++;
1962 udps_out_sw_cksum_bytes
+= len
;