2 * Copyright (c) 2000-2013 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>
79 #include <mach/boolean.h>
82 #include <net/if_types.h>
83 #include <net/route.h>
86 #include <netinet/in.h>
87 #include <netinet/in_systm.h>
88 #include <netinet/ip.h>
90 #include <netinet/ip6.h>
92 #include <netinet/in_pcb.h>
93 #include <netinet/in_var.h>
94 #include <netinet/ip_var.h>
96 #include <netinet6/in6_pcb.h>
97 #include <netinet6/ip6_var.h>
98 #include <netinet6/udp6_var.h>
100 #include <netinet/ip_icmp.h>
101 #include <netinet/icmp_var.h>
102 #include <netinet/udp.h>
103 #include <netinet/udp_var.h>
104 #include <sys/kdebug.h>
107 #include <netinet6/ipsec.h>
108 #include <netinet6/esp.h>
109 extern int ipsec_bypass
;
110 extern int esp_udp_encap_port
;
113 #define DBG_LAYER_IN_BEG NETDBG_CODE(DBG_NETUDP, 0)
114 #define DBG_LAYER_IN_END NETDBG_CODE(DBG_NETUDP, 2)
115 #define DBG_LAYER_OUT_BEG NETDBG_CODE(DBG_NETUDP, 1)
116 #define DBG_LAYER_OUT_END NETDBG_CODE(DBG_NETUDP, 3)
117 #define DBG_FNC_UDP_INPUT NETDBG_CODE(DBG_NETUDP, (5 << 8))
118 #define DBG_FNC_UDP_OUTPUT NETDBG_CODE(DBG_NETUDP, (6 << 8) | 1)
121 * UDP protocol implementation.
122 * Per RFC 768, August, 1980.
125 static int udpcksum
= 1;
127 static int udpcksum
= 0; /* XXX */
129 SYSCTL_INT(_net_inet_udp
, UDPCTL_CHECKSUM
, checksum
,
130 CTLFLAG_RW
| CTLFLAG_LOCKED
, &udpcksum
, 0, "");
132 int udp_log_in_vain
= 0;
133 SYSCTL_INT(_net_inet_udp
, OID_AUTO
, log_in_vain
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
134 &udp_log_in_vain
, 0, "Log all incoming UDP packets");
136 static int blackhole
= 0;
137 SYSCTL_INT(_net_inet_udp
, OID_AUTO
, blackhole
, CTLFLAG_RW
| CTLFLAG_LOCKED
,
138 &blackhole
, 0, "Do not send port unreachables for refused connects");
140 struct inpcbhead udb
; /* from udp_var.h */
141 #define udb6 udb /* for KAME src sync over BSD*'s */
142 struct inpcbinfo udbinfo
;
145 #define UDBHASHSIZE 16
148 /* Garbage collection performed during most recent udp_gc() run */
149 static boolean_t udp_gc_done
= FALSE
;
152 extern int fw_verbose
;
153 extern void ipfwsyslog( int level
, const char *format
,...);
154 extern void ipfw_stealth_stats_incr_udp(void);
156 /* Apple logging, log to ipfw.log */
157 #define log_in_vain_log(a) { \
158 if ((udp_log_in_vain == 3) && (fw_verbose == 2)) { \
160 } else if ((udp_log_in_vain == 4) && (fw_verbose == 2)) { \
161 ipfw_stealth_stats_incr_udp(); \
166 #else /* !IPFIREWALL */
167 #define log_in_vain_log( a ) { log a; }
168 #endif /* !IPFIREWALL */
170 static int udp_getstat SYSCTL_HANDLER_ARGS
;
171 struct udpstat udpstat
; /* from udp_var.h */
172 SYSCTL_PROC(_net_inet_udp
, UDPCTL_STATS
, stats
, CTLFLAG_RD
| CTLFLAG_LOCKED
,
173 0, 0, udp_getstat
, "S,udpstat",
174 "UDP statistics (struct udpstat, netinet/udp_var.h)");
176 SYSCTL_INT(_net_inet_udp
, OID_AUTO
, pcbcount
,
177 CTLFLAG_RD
| CTLFLAG_LOCKED
, &udbinfo
.ipi_count
, 0,
178 "Number of active PCBs");
180 __private_extern__
int udp_use_randomport
= 1;
181 SYSCTL_INT(_net_inet_udp
, OID_AUTO
, randomize_ports
,
182 CTLFLAG_RW
| CTLFLAG_LOCKED
, &udp_use_randomport
, 0,
183 "Randomize UDP port numbers");
187 struct sockaddr_in6 uin6_sin
;
188 u_char uin6_init_done
: 1;
191 struct ip6_hdr uip6_ip6
;
192 u_char uip6_init_done
: 1;
195 static int udp_abort(struct socket
*);
196 static int udp_attach(struct socket
*, int, struct proc
*);
197 static int udp_bind(struct socket
*, struct sockaddr
*, struct proc
*);
198 static int udp_connect(struct socket
*, struct sockaddr
*, struct proc
*);
199 static int udp_connectx(struct socket
*, struct sockaddr_list
**,
200 struct sockaddr_list
**, struct proc
*, uint32_t, associd_t
, connid_t
*,
201 uint32_t, void *, uint32_t);
202 static int udp_detach(struct socket
*);
203 static int udp_disconnect(struct socket
*);
204 static int udp_disconnectx(struct socket
*, associd_t
, connid_t
);
205 static int udp_send(struct socket
*, int, struct mbuf
*, struct sockaddr
*,
206 struct mbuf
*, struct proc
*);
207 static void udp_append(struct inpcb
*, struct ip
*, struct mbuf
*, int,
208 struct sockaddr_in
*, struct udp_in6
*, struct udp_ip6
*, struct ifnet
*);
210 static void udp_append(struct inpcb
*, struct ip
*, struct mbuf
*, int,
211 struct sockaddr_in
*, struct ifnet
*);
213 static int udp_input_checksum(struct mbuf
*, struct udphdr
*, int, int);
214 static int udp_output(struct inpcb
*, struct mbuf
*, struct sockaddr
*,
215 struct mbuf
*, struct proc
*);
216 static void ip_2_ip6_hdr(struct ip6_hdr
*ip6
, struct ip
*ip
);
217 static void udp_gc(struct inpcbinfo
*);
219 struct pr_usrreqs udp_usrreqs
= {
220 .pru_abort
= udp_abort
,
221 .pru_attach
= udp_attach
,
222 .pru_bind
= udp_bind
,
223 .pru_connect
= udp_connect
,
224 .pru_connectx
= udp_connectx
,
225 .pru_control
= in_control
,
226 .pru_detach
= udp_detach
,
227 .pru_disconnect
= udp_disconnect
,
228 .pru_disconnectx
= udp_disconnectx
,
229 .pru_peeraddr
= in_getpeeraddr
,
230 .pru_send
= udp_send
,
231 .pru_shutdown
= udp_shutdown
,
232 .pru_sockaddr
= in_getsockaddr
,
233 .pru_sosend
= sosend
,
234 .pru_soreceive
= soreceive
,
238 udp_init(struct protosw
*pp
, struct domain
*dp
)
241 static int udp_initialized
= 0;
243 struct inpcbinfo
*pcbinfo
;
245 VERIFY((pp
->pr_flags
& (PR_INITIALIZED
|PR_ATTACHED
)) == PR_ATTACHED
);
252 udbinfo
.ipi_listhead
= &udb
;
253 udbinfo
.ipi_hashbase
= hashinit(UDBHASHSIZE
, M_PCB
,
254 &udbinfo
.ipi_hashmask
);
255 udbinfo
.ipi_porthashbase
= hashinit(UDBHASHSIZE
, M_PCB
,
256 &udbinfo
.ipi_porthashmask
);
257 str_size
= (vm_size_t
) sizeof (struct inpcb
);
258 udbinfo
.ipi_zone
= zinit(str_size
, 80000*str_size
, 8192, "udpcb");
262 * allocate lock group attribute and group for udp pcb mutexes
264 pcbinfo
->ipi_lock_grp_attr
= lck_grp_attr_alloc_init();
265 pcbinfo
->ipi_lock_grp
= lck_grp_alloc_init("udppcb",
266 pcbinfo
->ipi_lock_grp_attr
);
267 pcbinfo
->ipi_lock_attr
= lck_attr_alloc_init();
268 if ((pcbinfo
->ipi_lock
= lck_rw_alloc_init(pcbinfo
->ipi_lock_grp
,
269 pcbinfo
->ipi_lock_attr
)) == NULL
) {
270 panic("%s: unable to allocate PCB lock\n", __func__
);
274 udbinfo
.ipi_gc
= udp_gc
;
275 in_pcbinfo_attach(&udbinfo
);
279 udp_input(struct mbuf
*m
, int iphlen
)
284 struct mbuf
*opts
= NULL
;
285 int len
, isbroadcast
;
287 struct sockaddr
*append_sa
;
288 struct inpcbinfo
*pcbinfo
= &udbinfo
;
289 struct sockaddr_in udp_in
;
290 struct ip_moptions
*imo
= NULL
;
291 int foundmembership
= 0, ret
= 0;
293 struct udp_in6 udp_in6
;
294 struct udp_ip6 udp_ip6
;
296 struct ifnet
*ifp
= m
->m_pkthdr
.rcvif
;
297 boolean_t cell
= IFNET_IS_CELLULAR(ifp
);
298 boolean_t wifi
= (!cell
&& IFNET_IS_WIFI(ifp
));
300 bzero(&udp_in
, sizeof (udp_in
));
301 udp_in
.sin_len
= sizeof (struct sockaddr_in
);
302 udp_in
.sin_family
= AF_INET
;
304 bzero(&udp_in6
, sizeof (udp_in6
));
305 udp_in6
.uin6_sin
.sin6_len
= sizeof (struct sockaddr_in6
);
306 udp_in6
.uin6_sin
.sin6_family
= AF_INET6
;
309 udpstat
.udps_ipackets
++;
311 KERNEL_DEBUG(DBG_FNC_UDP_INPUT
| DBG_FUNC_START
, 0,0,0,0,0);
313 /* Expect 32-bit aligned data pointer on strict-align platforms */
314 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m
);
317 * Strip IP options, if any; should skip this,
318 * make available to user, and use on returned packets,
319 * but we don't yet have a way to check the checksum
320 * with options still present.
322 if (iphlen
> sizeof (struct ip
)) {
323 ip_stripoptions(m
, (struct mbuf
*)0);
324 iphlen
= sizeof (struct ip
);
328 * Get IP and UDP header together in first mbuf.
330 ip
= mtod(m
, struct ip
*);
331 if (m
->m_len
< iphlen
+ sizeof (struct udphdr
)) {
332 m
= m_pullup(m
, iphlen
+ sizeof (struct udphdr
));
334 udpstat
.udps_hdrops
++;
335 KERNEL_DEBUG(DBG_FNC_UDP_INPUT
| DBG_FUNC_END
,
339 ip
= mtod(m
, struct ip
*);
341 uh
= (struct udphdr
*)(void *)((caddr_t
)ip
+ iphlen
);
343 /* destination port of 0 is illegal, based on RFC768. */
344 if (uh
->uh_dport
== 0) {
345 IF_UDP_STATINC(ifp
, port0
);
349 KERNEL_DEBUG(DBG_LAYER_IN_BEG
, uh
->uh_dport
, uh
->uh_sport
,
350 ip
->ip_src
.s_addr
, ip
->ip_dst
.s_addr
, uh
->uh_ulen
);
353 * Make mbuf data length reflect UDP length.
354 * If not enough data to reflect UDP length, drop.
356 len
= ntohs((u_short
)uh
->uh_ulen
);
357 if (ip
->ip_len
!= len
) {
358 if (len
> ip
->ip_len
|| len
< sizeof (struct udphdr
)) {
359 udpstat
.udps_badlen
++;
360 IF_UDP_STATINC(ifp
, badlength
);
363 m_adj(m
, len
- ip
->ip_len
);
364 /* ip->ip_len = len; */
367 * Save a copy of the IP header in case we want restore it
368 * for sending an ICMP error message in response.
373 * Checksum extended UDP header and data.
375 if (udp_input_checksum(m
, uh
, iphlen
, len
))
378 isbroadcast
= in_broadcast(ip
->ip_dst
, ifp
);
380 if (IN_MULTICAST(ntohl(ip
->ip_dst
.s_addr
)) || isbroadcast
) {
381 int reuse_sock
= 0, mcast_delivered
= 0;
383 lck_rw_lock_shared(pcbinfo
->ipi_lock
);
385 * Deliver a multicast or broadcast datagram to *all* sockets
386 * for which the local and remote addresses and ports match
387 * those of the incoming datagram. This allows more than
388 * one process to receive multi/broadcasts on the same port.
389 * (This really ought to be done for unicast datagrams as
390 * well, but that would cause problems with existing
391 * applications that open both address-specific sockets and
392 * a wildcard socket listening to the same port -- they would
393 * end up receiving duplicates of every unicast datagram.
394 * Those applications open the multiple sockets to overcome an
395 * inadequacy of the UDP socket interface, but for backwards
396 * compatibility we avoid the problem here rather than
397 * fixing the interface. Maybe 4.5BSD will remedy this?)
401 * Construct sockaddr format source address.
403 udp_in
.sin_port
= uh
->uh_sport
;
404 udp_in
.sin_addr
= ip
->ip_src
;
406 * Locate pcb(s) for datagram.
407 * (Algorithm copied from raw_intr().)
410 udp_in6
.uin6_init_done
= udp_ip6
.uip6_init_done
= 0;
412 LIST_FOREACH(inp
, &udb
, inp_list
) {
417 if (inp
->inp_socket
== NULL
)
419 if (inp
!= sotoinpcb(inp
->inp_socket
)) {
420 panic("%s: bad so back ptr inp=%p\n",
425 if ((inp
->inp_vflag
& INP_IPV4
) == 0)
428 if (inp_restricted(inp
, ifp
))
431 if (IFNET_IS_CELLULAR(ifp
) &&
432 (inp
->inp_flags
& INP_NO_IFT_CELLULAR
))
435 if ((inp
->inp_moptions
== NULL
) &&
436 (ntohl(ip
->ip_dst
.s_addr
) !=
437 INADDR_ALLHOSTS_GROUP
) && (isbroadcast
== 0))
440 if (in_pcb_checkstate(inp
, WNT_ACQUIRE
, 0) ==
444 udp_lock(inp
->inp_socket
, 1, 0);
446 if (in_pcb_checkstate(inp
, WNT_RELEASE
, 1) ==
448 udp_unlock(inp
->inp_socket
, 1, 0);
452 if (inp
->inp_lport
!= uh
->uh_dport
) {
453 udp_unlock(inp
->inp_socket
, 1, 0);
456 if (inp
->inp_laddr
.s_addr
!= INADDR_ANY
) {
457 if (inp
->inp_laddr
.s_addr
!=
459 udp_unlock(inp
->inp_socket
, 1, 0);
463 if (inp
->inp_faddr
.s_addr
!= INADDR_ANY
) {
464 if (inp
->inp_faddr
.s_addr
!=
466 inp
->inp_fport
!= uh
->uh_sport
) {
467 udp_unlock(inp
->inp_socket
, 1, 0);
472 if (isbroadcast
== 0 && (ntohl(ip
->ip_dst
.s_addr
) !=
473 INADDR_ALLHOSTS_GROUP
)) {
474 struct sockaddr_in group
;
477 if ((imo
= inp
->inp_moptions
) == NULL
) {
478 udp_unlock(inp
->inp_socket
, 1, 0);
483 bzero(&group
, sizeof (struct sockaddr_in
));
484 group
.sin_len
= sizeof (struct sockaddr_in
);
485 group
.sin_family
= AF_INET
;
486 group
.sin_addr
= ip
->ip_dst
;
488 blocked
= imo_multi_filter(imo
, ifp
,
489 (struct sockaddr
*)&group
,
490 (struct sockaddr
*)&udp_in
);
491 if (blocked
== MCAST_PASS
)
495 if (!foundmembership
) {
496 udp_unlock(inp
->inp_socket
, 1, 0);
497 if (blocked
== MCAST_NOTSMEMBER
||
498 blocked
== MCAST_MUTED
)
499 udpstat
.udps_filtermcast
++;
505 reuse_sock
= (inp
->inp_socket
->so_options
&
506 (SO_REUSEPORT
|SO_REUSEADDR
));
510 /* check AH/ESP integrity. */
511 if (ipsec_bypass
== 0 &&
512 ipsec4_in_reject_so(m
, inp
->inp_socket
)) {
513 IPSEC_STAT_INCREMENT(ipsecstat
.in_polvio
);
514 /* do not inject data to pcb */
520 struct mbuf
*n
= NULL
;
523 n
= m_copy(m
, 0, M_COPYALL
);
525 udp_append(inp
, ip
, m
,
526 iphlen
+ sizeof (struct udphdr
),
527 &udp_in
, &udp_in6
, &udp_ip6
, ifp
);
529 udp_append(inp
, ip
, m
,
530 iphlen
+ sizeof (struct udphdr
),
537 udp_unlock(inp
->inp_socket
, 1, 0);
540 * Don't look for additional matches if this one does
541 * not have either the SO_REUSEPORT or SO_REUSEADDR
542 * socket options set. This heuristic avoids searching
543 * through all pcbs in the common case of a non-shared
544 * port. It assumes that an application will never
545 * clear these options after setting them.
547 if (reuse_sock
== 0 || m
== NULL
)
551 * Expect 32-bit aligned data pointer on strict-align
554 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m
);
556 * Recompute IP and UDP header pointers for new mbuf
558 ip
= mtod(m
, struct ip
*);
559 uh
= (struct udphdr
*)(void *)((caddr_t
)ip
+ iphlen
);
561 lck_rw_done(pcbinfo
->ipi_lock
);
563 if (mcast_delivered
== 0) {
565 * No matching pcb found; discard datagram.
566 * (No need to send an ICMP Port Unreachable
567 * for a broadcast or multicast datgram.)
569 udpstat
.udps_noportbcast
++;
570 IF_UDP_STATINC(ifp
, port_unreach
);
574 /* 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 :
592 len
- sizeof (struct udphdr
);
594 if (m
->m_len
< iphlen
+ sizeof (struct udphdr
) + payload_len
) {
595 if ((m
= m_pullup(m
, iphlen
+ sizeof (struct udphdr
) +
596 payload_len
)) == NULL
) {
597 udpstat
.udps_hdrops
++;
598 KERNEL_DEBUG(DBG_FNC_UDP_INPUT
| DBG_FUNC_END
,
603 * Expect 32-bit aligned data pointer on strict-align
606 MBUF_STRICT_DATA_ALIGNMENT_CHECK_32(m
);
608 ip
= mtod(m
, struct ip
*);
609 uh
= (struct udphdr
*)(void *)((caddr_t
)ip
+ iphlen
);
611 /* Check for NAT keepalive packet */
612 if (payload_len
== 1 && *(u_int8_t
*)
613 ((caddr_t
)uh
+ sizeof (struct udphdr
)) == 0xFF) {
615 KERNEL_DEBUG(DBG_FNC_UDP_INPUT
| DBG_FUNC_END
,
618 } else if (payload_len
== 4 && *(u_int32_t
*)(void *)
619 ((caddr_t
)uh
+ sizeof (struct udphdr
)) != 0) {
620 /* UDP encapsulated IPSec packet to pass through NAT */
621 KERNEL_DEBUG(DBG_FNC_UDP_INPUT
| DBG_FUNC_END
,
623 /* preserve the udp header */
624 esp4_input(m
, iphlen
+ sizeof (struct udphdr
));
631 * Locate pcb for datagram.
633 inp
= in_pcblookup_hash(&udbinfo
, ip
->ip_src
, uh
->uh_sport
,
634 ip
->ip_dst
, uh
->uh_dport
, 1, ifp
);
636 IF_UDP_STATINC(ifp
, port_unreach
);
638 if (udp_log_in_vain
) {
639 char buf
[MAX_IPv4_STR_LEN
];
640 char buf2
[MAX_IPv4_STR_LEN
];
642 /* check src and dst address */
643 if (udp_log_in_vain
< 3) {
644 log(LOG_INFO
, "Connection attempt to "
645 "UDP %s:%d from %s:%d\n", inet_ntop(AF_INET
,
646 &ip
->ip_dst
, buf
, sizeof (buf
)),
647 ntohs(uh
->uh_dport
), inet_ntop(AF_INET
,
648 &ip
->ip_src
, buf2
, sizeof (buf2
)),
649 ntohs(uh
->uh_sport
));
650 } else if (!(m
->m_flags
& (M_BCAST
| M_MCAST
)) &&
651 ip
->ip_dst
.s_addr
!= ip
->ip_src
.s_addr
) {
652 log_in_vain_log((LOG_INFO
,
653 "Stealth Mode connection attempt to "
654 "UDP %s:%d from %s:%d\n", inet_ntop(AF_INET
,
655 &ip
->ip_dst
, buf
, sizeof (buf
)),
656 ntohs(uh
->uh_dport
), inet_ntop(AF_INET
,
657 &ip
->ip_src
, buf2
, sizeof (buf2
)),
658 ntohs(uh
->uh_sport
)))
661 udpstat
.udps_noport
++;
662 if (m
->m_flags
& (M_BCAST
| M_MCAST
)) {
663 udpstat
.udps_noportbcast
++;
667 if (badport_bandlim(BANDLIM_ICMP_UNREACH
) < 0)
669 #endif /* ICMP_BANDLIM */
671 if (ifp
&& ifp
->if_type
!= IFT_LOOP
)
674 ip
->ip_len
+= iphlen
;
675 icmp_error(m
, ICMP_UNREACH
, ICMP_UNREACH_PORT
, 0, 0);
676 KERNEL_DEBUG(DBG_FNC_UDP_INPUT
| DBG_FUNC_END
, 0,0,0,0,0);
679 udp_lock(inp
->inp_socket
, 1, 0);
681 if (in_pcb_checkstate(inp
, WNT_RELEASE
, 1) == WNT_STOPUSING
) {
682 udp_unlock(inp
->inp_socket
, 1, 0);
683 IF_UDP_STATINC(ifp
, cleanup
);
687 if (ipsec_bypass
== 0 && inp
!= NULL
) {
688 if (ipsec4_in_reject_so(m
, inp
->inp_socket
)) {
689 IPSEC_STAT_INCREMENT(ipsecstat
.in_polvio
);
690 udp_unlock(inp
->inp_socket
, 1, 0);
691 IF_UDP_STATINC(ifp
, badipsec
);
698 * Construct sockaddr format source address.
699 * Stuff source address and datagram in user buffer.
701 udp_in
.sin_port
= uh
->uh_sport
;
702 udp_in
.sin_addr
= ip
->ip_src
;
703 if ((inp
->inp_flags
& INP_CONTROLOPTS
) != 0 ||
704 (inp
->inp_socket
->so_options
& SO_TIMESTAMP
) != 0 ||
705 (inp
->inp_socket
->so_options
& SO_TIMESTAMP_MONOTONIC
) != 0) {
707 if (inp
->inp_vflag
& INP_IPV6
) {
710 ip_2_ip6_hdr(&udp_ip6
.uip6_ip6
, ip
);
711 savedflags
= inp
->inp_flags
;
712 inp
->inp_flags
&= ~INP_UNMAPPABLEOPTS
;
713 ret
= ip6_savecontrol(inp
, m
, &opts
);
714 inp
->inp_flags
= savedflags
;
718 ret
= ip_savecontrol(inp
, &opts
, ip
, m
);
721 udp_unlock(inp
->inp_socket
, 1, 0);
725 m_adj(m
, iphlen
+ sizeof (struct udphdr
));
727 KERNEL_DEBUG(DBG_LAYER_IN_END
, uh
->uh_dport
, uh
->uh_sport
,
728 save_ip
.ip_src
.s_addr
, save_ip
.ip_dst
.s_addr
, uh
->uh_ulen
);
731 if (inp
->inp_vflag
& INP_IPV6
) {
732 in6_sin_2_v4mapsin6(&udp_in
, &udp_in6
.uin6_sin
);
733 append_sa
= (struct sockaddr
*)&udp_in6
.uin6_sin
;
737 append_sa
= (struct sockaddr
*)&udp_in
;
740 INP_ADD_STAT(inp
, cell
, wifi
, rxpackets
, 1);
741 INP_ADD_STAT(inp
, cell
, wifi
, rxbytes
, m
->m_pkthdr
.len
);
743 so_recv_data_stat(inp
->inp_socket
, m
, 0);
744 if (sbappendaddr(&inp
->inp_socket
->so_rcv
, append_sa
,
745 m
, opts
, NULL
) == 0) {
746 udpstat
.udps_fullsock
++;
748 sorwakeup(inp
->inp_socket
);
750 udp_unlock(inp
->inp_socket
, 1, 0);
751 KERNEL_DEBUG(DBG_FNC_UDP_INPUT
| DBG_FUNC_END
, 0,0,0,0,0);
757 KERNEL_DEBUG(DBG_FNC_UDP_INPUT
| DBG_FUNC_END
, 0,0,0,0,0);
762 ip_2_ip6_hdr(struct ip6_hdr
*ip6
, struct ip
*ip
)
764 bzero(ip6
, sizeof (*ip6
));
766 ip6
->ip6_vfc
= IPV6_VERSION
;
767 ip6
->ip6_plen
= ip
->ip_len
;
768 ip6
->ip6_nxt
= ip
->ip_p
;
769 ip6
->ip6_hlim
= ip
->ip_ttl
;
770 if (ip
->ip_src
.s_addr
) {
771 ip6
->ip6_src
.s6_addr32
[2] = IPV6_ADDR_INT32_SMP
;
772 ip6
->ip6_src
.s6_addr32
[3] = ip
->ip_src
.s_addr
;
774 if (ip
->ip_dst
.s_addr
) {
775 ip6
->ip6_dst
.s6_addr32
[2] = IPV6_ADDR_INT32_SMP
;
776 ip6
->ip6_dst
.s6_addr32
[3] = ip
->ip_dst
.s_addr
;
782 * subroutine of udp_input(), mainly for source code readability.
786 udp_append(struct inpcb
*last
, struct ip
*ip
, struct mbuf
*n
, int off
,
787 struct sockaddr_in
*pudp_in
, struct udp_in6
*pudp_in6
,
788 struct udp_ip6
*pudp_ip6
, struct ifnet
*ifp
)
790 udp_append(struct inpcb
*last
, struct ip
*ip
, struct mbuf
*n
, int off
,
791 struct sockaddr_in
*pudp_in
, struct ifnet
*ifp
)
794 struct sockaddr
*append_sa
;
795 struct mbuf
*opts
= 0;
796 boolean_t cell
= IFNET_IS_CELLULAR(ifp
);
797 boolean_t wifi
= (!cell
&& IFNET_IS_WIFI(ifp
));
801 if (mac_inpcb_check_deliver(last
, n
, AF_INET
, SOCK_DGRAM
) != 0) {
805 #endif /* CONFIG_MACF_NET */
806 if ((last
->inp_flags
& INP_CONTROLOPTS
) != 0 ||
807 (last
->inp_socket
->so_options
& SO_TIMESTAMP
) != 0 ||
808 (last
->inp_socket
->so_options
& SO_TIMESTAMP_MONOTONIC
) != 0) {
810 if (last
->inp_vflag
& INP_IPV6
) {
813 if (pudp_ip6
->uip6_init_done
== 0) {
814 ip_2_ip6_hdr(&pudp_ip6
->uip6_ip6
, ip
);
815 pudp_ip6
->uip6_init_done
= 1;
817 savedflags
= last
->inp_flags
;
818 last
->inp_flags
&= ~INP_UNMAPPABLEOPTS
;
819 ret
= ip6_savecontrol(last
, n
, &opts
);
821 last
->inp_flags
= savedflags
;
824 last
->inp_flags
= savedflags
;
828 ret
= ip_savecontrol(last
, &opts
, ip
, n
);
835 if (last
->inp_vflag
& INP_IPV6
) {
836 if (pudp_in6
->uin6_init_done
== 0) {
837 in6_sin_2_v4mapsin6(pudp_in
, &pudp_in6
->uin6_sin
);
838 pudp_in6
->uin6_init_done
= 1;
840 append_sa
= (struct sockaddr
*)&pudp_in6
->uin6_sin
;
843 append_sa
= (struct sockaddr
*)pudp_in
;
845 INP_ADD_STAT(last
, cell
, wifi
, rxpackets
, 1);
846 INP_ADD_STAT(last
, cell
, wifi
, rxbytes
, n
->m_pkthdr
.len
);
848 so_recv_data_stat(last
->inp_socket
, n
, 0);
850 if (sbappendaddr(&last
->inp_socket
->so_rcv
, append_sa
,
851 n
, opts
, NULL
) == 0) {
852 udpstat
.udps_fullsock
++;
854 sorwakeup(last
->inp_socket
);
864 * Notify a udp user of an asynchronous error;
865 * just wake up so that he can collect error status.
868 udp_notify(struct inpcb
*inp
, int errno
)
870 inp
->inp_socket
->so_error
= errno
;
871 sorwakeup(inp
->inp_socket
);
872 sowwakeup(inp
->inp_socket
);
876 udp_ctlinput(int cmd
, struct sockaddr
*sa
, void *vip
)
879 void (*notify
)(struct inpcb
*, int) = udp_notify
;
880 struct in_addr faddr
;
883 faddr
= ((struct sockaddr_in
*)(void *)sa
)->sin_addr
;
884 if (sa
->sa_family
!= AF_INET
|| faddr
.s_addr
== INADDR_ANY
)
887 if (PRC_IS_REDIRECT(cmd
)) {
889 notify
= in_rtchange
;
890 } else if (cmd
== PRC_HOSTDEAD
) {
892 } 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
);
917 udp_ctloutput(struct socket
*so
, struct sockopt
*sopt
)
922 /* Allow <SOL_SOCKET,SO_FLUSH> at this level */
923 if (sopt
->sopt_level
!= IPPROTO_UDP
&&
924 !(sopt
->sopt_level
== SOL_SOCKET
&& sopt
->sopt_name
== SO_FLUSH
))
925 return (ip_ctloutput(so
, sopt
));
930 switch (sopt
->sopt_dir
) {
932 switch (sopt
->sopt_name
) {
934 /* This option is settable only for UDP over IPv4 */
935 if (!(inp
->inp_vflag
& INP_IPV4
)) {
940 if ((error
= sooptcopyin(sopt
, &optval
, sizeof (optval
),
941 sizeof (optval
))) != 0)
945 inp
->inp_flags
|= INP_UDP_NOCKSUM
;
947 inp
->inp_flags
&= ~INP_UDP_NOCKSUM
;
951 if ((error
= sooptcopyin(sopt
, &optval
, sizeof (optval
),
952 sizeof (optval
))) != 0)
955 error
= inp_flush(inp
, optval
);
965 switch (sopt
->sopt_name
) {
967 optval
= inp
->inp_flags
& INP_UDP_NOCKSUM
;
975 error
= sooptcopyout(sopt
, &optval
, sizeof (optval
));
982 udp_pcblist SYSCTL_HANDLER_ARGS
984 #pragma unused(oidp, arg1, arg2)
986 struct inpcb
*inp
, **inp_list
;
991 * The process of preparing the TCB list is too time-consuming and
992 * resource-intensive to repeat twice on every request.
994 lck_rw_lock_exclusive(udbinfo
.ipi_lock
);
995 if (req
->oldptr
== USER_ADDR_NULL
) {
996 n
= udbinfo
.ipi_count
;
997 req
->oldidx
= 2 * (sizeof (xig
))
998 + (n
+ n
/8) * sizeof (struct xinpcb
);
999 lck_rw_done(udbinfo
.ipi_lock
);
1003 if (req
->newptr
!= USER_ADDR_NULL
) {
1004 lck_rw_done(udbinfo
.ipi_lock
);
1009 * OK, now we're committed to doing something.
1011 gencnt
= udbinfo
.ipi_gencnt
;
1012 n
= udbinfo
.ipi_count
;
1014 bzero(&xig
, sizeof (xig
));
1015 xig
.xig_len
= sizeof (xig
);
1017 xig
.xig_gen
= gencnt
;
1018 xig
.xig_sogen
= so_gencnt
;
1019 error
= SYSCTL_OUT(req
, &xig
, sizeof (xig
));
1021 lck_rw_done(udbinfo
.ipi_lock
);
1025 * We are done if there is no pcb
1028 lck_rw_done(udbinfo
.ipi_lock
);
1032 inp_list
= _MALLOC(n
* sizeof (*inp_list
), M_TEMP
, M_WAITOK
);
1033 if (inp_list
== 0) {
1034 lck_rw_done(udbinfo
.ipi_lock
);
1038 for (inp
= LIST_FIRST(udbinfo
.ipi_listhead
), i
= 0; inp
&& i
< n
;
1039 inp
= LIST_NEXT(inp
, inp_list
)) {
1040 if (inp
->inp_gencnt
<= gencnt
&&
1041 inp
->inp_state
!= INPCB_STATE_DEAD
)
1042 inp_list
[i
++] = inp
;
1047 for (i
= 0; i
< n
; i
++) {
1049 if (inp
->inp_gencnt
<= gencnt
&&
1050 inp
->inp_state
!= INPCB_STATE_DEAD
) {
1053 bzero(&xi
, sizeof (xi
));
1054 xi
.xi_len
= sizeof (xi
);
1055 /* XXX should avoid extra copy */
1056 inpcb_to_compat(inp
, &xi
.xi_inp
);
1057 if (inp
->inp_socket
)
1058 sotoxsocket(inp
->inp_socket
, &xi
.xi_socket
);
1059 error
= SYSCTL_OUT(req
, &xi
, sizeof (xi
));
1064 * Give the user an updated idea of our state.
1065 * If the generation differs from what we told
1066 * her before, she knows that something happened
1067 * while we were processing this request, and it
1068 * might be necessary to retry.
1070 bzero(&xig
, sizeof (xig
));
1071 xig
.xig_len
= sizeof (xig
);
1072 xig
.xig_gen
= udbinfo
.ipi_gencnt
;
1073 xig
.xig_sogen
= so_gencnt
;
1074 xig
.xig_count
= udbinfo
.ipi_count
;
1075 error
= SYSCTL_OUT(req
, &xig
, sizeof (xig
));
1077 FREE(inp_list
, M_TEMP
);
1078 lck_rw_done(udbinfo
.ipi_lock
);
1082 SYSCTL_PROC(_net_inet_udp
, UDPCTL_PCBLIST
, pcblist
,
1083 CTLFLAG_RD
| CTLFLAG_LOCKED
, 0, 0, udp_pcblist
,
1084 "S,xinpcb", "List of active UDP sockets");
1088 udp_pcblist64 SYSCTL_HANDLER_ARGS
1090 #pragma unused(oidp, arg1, arg2)
1092 struct inpcb
*inp
, **inp_list
;
1097 * The process of preparing the TCB list is too time-consuming and
1098 * resource-intensive to repeat twice on every request.
1100 lck_rw_lock_shared(udbinfo
.ipi_lock
);
1101 if (req
->oldptr
== USER_ADDR_NULL
) {
1102 n
= udbinfo
.ipi_count
;
1104 2 * (sizeof (xig
)) + (n
+ n
/8) * sizeof (struct xinpcb64
);
1105 lck_rw_done(udbinfo
.ipi_lock
);
1109 if (req
->newptr
!= USER_ADDR_NULL
) {
1110 lck_rw_done(udbinfo
.ipi_lock
);
1115 * OK, now we're committed to doing something.
1117 gencnt
= udbinfo
.ipi_gencnt
;
1118 n
= udbinfo
.ipi_count
;
1120 bzero(&xig
, sizeof (xig
));
1121 xig
.xig_len
= sizeof (xig
);
1123 xig
.xig_gen
= gencnt
;
1124 xig
.xig_sogen
= so_gencnt
;
1125 error
= SYSCTL_OUT(req
, &xig
, sizeof (xig
));
1127 lck_rw_done(udbinfo
.ipi_lock
);
1131 * We are done if there is no pcb
1134 lck_rw_done(udbinfo
.ipi_lock
);
1138 inp_list
= _MALLOC(n
* sizeof (*inp_list
), M_TEMP
, M_WAITOK
);
1139 if (inp_list
== 0) {
1140 lck_rw_done(udbinfo
.ipi_lock
);
1144 for (inp
= LIST_FIRST(udbinfo
.ipi_listhead
), i
= 0; inp
&& i
< n
;
1145 inp
= LIST_NEXT(inp
, inp_list
)) {
1146 if (inp
->inp_gencnt
<= gencnt
&&
1147 inp
->inp_state
!= INPCB_STATE_DEAD
)
1148 inp_list
[i
++] = inp
;
1153 for (i
= 0; i
< n
; i
++) {
1155 if (inp
->inp_gencnt
<= gencnt
&&
1156 inp
->inp_state
!= INPCB_STATE_DEAD
) {
1159 bzero(&xi
, sizeof (xi
));
1160 xi
.xi_len
= sizeof (xi
);
1161 inpcb_to_xinpcb64(inp
, &xi
);
1162 if (inp
->inp_socket
)
1163 sotoxsocket64(inp
->inp_socket
, &xi
.xi_socket
);
1164 error
= SYSCTL_OUT(req
, &xi
, sizeof (xi
));
1169 * Give the user an updated idea of our state.
1170 * If the generation differs from what we told
1171 * her before, she knows that something happened
1172 * while we were processing this request, and it
1173 * might be necessary to retry.
1175 bzero(&xig
, sizeof (xig
));
1176 xig
.xig_len
= sizeof (xig
);
1177 xig
.xig_gen
= udbinfo
.ipi_gencnt
;
1178 xig
.xig_sogen
= so_gencnt
;
1179 xig
.xig_count
= udbinfo
.ipi_count
;
1180 error
= SYSCTL_OUT(req
, &xig
, sizeof (xig
));
1182 FREE(inp_list
, M_TEMP
);
1183 lck_rw_done(udbinfo
.ipi_lock
);
1187 SYSCTL_PROC(_net_inet_udp
, OID_AUTO
, pcblist64
,
1188 CTLFLAG_RD
| CTLFLAG_LOCKED
, 0, 0, udp_pcblist64
,
1189 "S,xinpcb64", "List of active UDP sockets");
1193 udp_pcblist_n SYSCTL_HANDLER_ARGS
1195 #pragma unused(oidp, arg1, arg2)
1196 return (get_pcblist_n(IPPROTO_UDP
, req
, &udbinfo
));
1199 SYSCTL_PROC(_net_inet_udp
, OID_AUTO
, pcblist_n
,
1200 CTLFLAG_RD
| CTLFLAG_LOCKED
, 0, 0, udp_pcblist_n
,
1201 "S,xinpcb_n", "List of active UDP sockets");
1203 __private_extern__
void
1204 udp_get_ports_used(uint32_t ifindex
, int protocol
, uint32_t wildcardok
,
1207 inpcb_get_ports_used(ifindex
, protocol
, wildcardok
, bitfield
, &udbinfo
);
1210 __private_extern__
uint32_t
1211 udp_count_opportunistic(unsigned int ifindex
, u_int32_t flags
)
1213 return (inpcb_count_opportunistic(ifindex
, &udbinfo
, flags
));
1216 __private_extern__
uint32_t
1217 udp_find_anypcb_byaddr(struct ifaddr
*ifa
)
1219 return (inpcb_find_anypcb_byaddr(ifa
, &udbinfo
));
1223 udp_check_pktinfo(struct mbuf
*control
, struct ifnet
**outif
,
1224 struct in_addr
*laddr
)
1226 struct cmsghdr
*cm
= 0;
1227 struct in_pktinfo
*pktinfo
;
1234 * XXX: Currently, we assume all the optional information is stored
1237 if (control
->m_next
)
1240 if (control
->m_len
< CMSG_LEN(0))
1243 for (cm
= M_FIRST_CMSGHDR(control
); cm
;
1244 cm
= M_NXT_CMSGHDR(control
, cm
)) {
1245 if (cm
->cmsg_len
< sizeof (struct cmsghdr
) ||
1246 cm
->cmsg_len
> control
->m_len
)
1249 if (cm
->cmsg_level
!= IPPROTO_IP
|| cm
->cmsg_type
!= IP_PKTINFO
)
1252 if (cm
->cmsg_len
!= CMSG_LEN(sizeof (struct in_pktinfo
)))
1255 pktinfo
= (struct in_pktinfo
*)(void *)CMSG_DATA(cm
);
1257 /* Check for a valid ifindex in pktinfo */
1258 ifnet_head_lock_shared();
1260 if (pktinfo
->ipi_ifindex
> if_index
) {
1266 * If ipi_ifindex is specified it takes precedence
1267 * over ipi_spec_dst.
1269 if (pktinfo
->ipi_ifindex
) {
1270 ifp
= ifindex2ifnet
[pktinfo
->ipi_ifindex
];
1275 if (outif
!= NULL
) {
1276 ifnet_reference(ifp
);
1280 laddr
->s_addr
= INADDR_ANY
;
1287 * Use the provided ipi_spec_dst address for temp
1290 *laddr
= pktinfo
->ipi_spec_dst
;
1297 udp_output(struct inpcb
*inp
, struct mbuf
*m
, struct sockaddr
*addr
,
1298 struct mbuf
*control
, struct proc
*p
)
1300 struct udpiphdr
*ui
;
1301 int len
= m
->m_pkthdr
.len
;
1302 struct sockaddr_in
*sin
;
1303 struct in_addr origladdr
, laddr
, faddr
, pi_laddr
;
1304 u_short lport
, fport
;
1305 int error
= 0, udp_dodisconnect
= 0, pktinfo
= 0;
1306 struct socket
*so
= inp
->inp_socket
;
1308 struct mbuf
*inpopts
;
1309 struct ip_moptions
*mopts
;
1311 struct ip_out_args ipoa
=
1312 { IFSCOPE_NONE
, { 0 }, IPOAF_SELECT_SRCIF
, 0 };
1313 struct ifnet
*outif
= NULL
;
1314 struct flowadv
*adv
= &ipoa
.ipoa_flowadv
;
1315 mbuf_svc_class_t msc
= MBUF_SC_UNSPEC
;
1316 struct ifnet
*origoutifp
;
1319 /* Enable flow advisory only when connected */
1320 flowadv
= (so
->so_state
& SS_ISCONNECTED
) ? 1 : 0;
1321 pi_laddr
.s_addr
= INADDR_ANY
;
1323 KERNEL_DEBUG(DBG_FNC_UDP_OUTPUT
| DBG_FUNC_START
, 0,0,0,0,0);
1325 lck_mtx_assert(&inp
->inpcb_mtx
, LCK_MTX_ASSERT_OWNED
);
1326 if (control
!= NULL
) {
1327 msc
= mbuf_service_class_from_control(control
);
1328 VERIFY(outif
== NULL
);
1329 error
= udp_check_pktinfo(control
, &outif
, &pi_laddr
);
1336 ipoa
.ipoa_boundif
= outif
->if_index
;
1339 KERNEL_DEBUG(DBG_LAYER_OUT_BEG
, inp
->inp_fport
, inp
->inp_lport
,
1340 inp
->inp_laddr
.s_addr
, inp
->inp_faddr
.s_addr
,
1341 (htons((u_short
)len
+ sizeof (struct udphdr
))));
1343 if (len
+ sizeof (struct udpiphdr
) > IP_MAXPACKET
) {
1348 if (flowadv
&& INP_WAIT_FOR_IF_FEEDBACK(inp
)) {
1350 * The socket is flow-controlled, drop the packets
1351 * until the inp is not flow controlled
1357 * If socket was bound to an ifindex, tell ip_output about it.
1358 * If the ancillary IP_PKTINFO option contains an interface index,
1359 * it takes precedence over the one specified by IP_BOUND_IF.
1361 if (ipoa
.ipoa_boundif
== IFSCOPE_NONE
&&
1362 (inp
->inp_flags
& INP_BOUND_IF
)) {
1363 VERIFY(inp
->inp_boundifp
!= NULL
);
1364 ifnet_reference(inp
->inp_boundifp
); /* for this routine */
1366 ifnet_release(outif
);
1367 outif
= inp
->inp_boundifp
;
1368 ipoa
.ipoa_boundif
= outif
->if_index
;
1370 if (inp
->inp_flags
& INP_NO_IFT_CELLULAR
)
1371 ipoa
.ipoa_flags
|= IPOAF_NO_CELLULAR
;
1372 soopts
|= IP_OUTARGS
;
1375 * If there was a routing change, discard cached route and check
1376 * that we have a valid source address. Reacquire a new source
1377 * address if INADDR_ANY was specified.
1379 if (ROUTE_UNUSABLE(&inp
->inp_route
)) {
1380 struct in_ifaddr
*ia
= NULL
;
1382 ROUTE_RELEASE(&inp
->inp_route
);
1384 /* src address is gone? */
1385 if (inp
->inp_laddr
.s_addr
!= INADDR_ANY
&&
1386 (ia
= ifa_foraddr(inp
->inp_laddr
.s_addr
)) == NULL
) {
1387 if (!(inp
->inp_flags
& INP_INADDR_ANY
) ||
1388 (so
->so_state
& SS_ISCONNECTED
)) {
1391 * If the source address is gone, return an
1393 * - the source was specified
1394 * - the socket was already connected
1396 soevent(so
, (SO_FILT_HINT_LOCKED
|
1397 SO_FILT_HINT_NOSRCADDR
));
1398 error
= EADDRNOTAVAIL
;
1401 /* new src will be set later */
1402 inp
->inp_laddr
.s_addr
= INADDR_ANY
;
1403 inp
->inp_last_outifp
= NULL
;
1407 IFA_REMREF(&ia
->ia_ifa
);
1410 origoutifp
= inp
->inp_last_outifp
;
1413 * IP_PKTINFO option check. If a temporary scope or src address
1414 * is provided, use it for this packet only and make sure we forget
1415 * it after sending this datagram.
1417 if (pi_laddr
.s_addr
!= INADDR_ANY
||
1418 (ipoa
.ipoa_boundif
!= IFSCOPE_NONE
&& pktinfo
)) {
1419 /* temp src address for this datagram only */
1421 origladdr
.s_addr
= INADDR_ANY
;
1422 /* we don't want to keep the laddr or route */
1423 udp_dodisconnect
= 1;
1424 /* remember we don't care about src addr.*/
1425 inp
->inp_flags
|= INP_INADDR_ANY
;
1427 origladdr
= laddr
= inp
->inp_laddr
;
1430 origoutifp
= inp
->inp_last_outifp
;
1431 faddr
= inp
->inp_faddr
;
1432 lport
= inp
->inp_lport
;
1433 fport
= inp
->inp_fport
;
1436 sin
= (struct sockaddr_in
*)(void *)addr
;
1437 if (faddr
.s_addr
!= INADDR_ANY
) {
1443 * In case we don't have a local port set, go through
1444 * the full connect. We don't have a local port yet
1445 * (i.e., we can't be looked up), so it's not an issue
1446 * if the input runs at the same time we do this.
1448 /* if we have a source address specified, use that */
1449 if (pi_laddr
.s_addr
!= INADDR_ANY
)
1450 inp
->inp_laddr
= pi_laddr
;
1452 * If a scope is specified, use it. Scope from
1453 * IP_PKTINFO takes precendence over the the scope
1454 * set via INP_BOUND_IF.
1456 error
= in_pcbconnect(inp
, addr
, p
, ipoa
.ipoa_boundif
,
1461 laddr
= inp
->inp_laddr
;
1462 lport
= inp
->inp_lport
;
1463 faddr
= inp
->inp_faddr
;
1464 fport
= inp
->inp_fport
;
1465 udp_dodisconnect
= 1;
1467 /* synch up in case in_pcbladdr() overrides */
1468 if (outif
!= NULL
&& ipoa
.ipoa_boundif
!= IFSCOPE_NONE
)
1469 ipoa
.ipoa_boundif
= outif
->if_index
;
1475 * We have a full address and a local port; use those
1476 * info to build the packet without changing the pcb
1477 * and interfering with the input path. See 3851370.
1479 * Scope from IP_PKTINFO takes precendence over the
1480 * the scope set via INP_BOUND_IF.
1482 if (laddr
.s_addr
== INADDR_ANY
) {
1483 if ((error
= in_pcbladdr(inp
, addr
, &laddr
,
1484 ipoa
.ipoa_boundif
, &outif
)) != 0)
1487 * from pcbconnect: remember we don't
1488 * care about src addr.
1490 inp
->inp_flags
|= INP_INADDR_ANY
;
1492 /* synch up in case in_pcbladdr() overrides */
1493 if (outif
!= NULL
&&
1494 ipoa
.ipoa_boundif
!= IFSCOPE_NONE
)
1495 ipoa
.ipoa_boundif
= outif
->if_index
;
1498 faddr
= sin
->sin_addr
;
1499 fport
= sin
->sin_port
;
1502 if (faddr
.s_addr
== INADDR_ANY
) {
1509 mac_mbuf_label_associate_inpcb(inp
, m
);
1510 #endif /* CONFIG_MACF_NET */
1512 if (inp
->inp_flowhash
== 0)
1513 inp
->inp_flowhash
= inp_calc_flowhash(inp
);
1516 * Calculate data length and get a mbuf
1517 * for UDP and IP headers.
1519 M_PREPEND(m
, sizeof (struct udpiphdr
), M_DONTWAIT
);
1526 * Fill in mbuf with extended UDP header
1527 * and addresses and length put into network format.
1529 ui
= mtod(m
, struct udpiphdr
*);
1530 bzero(ui
->ui_x1
, sizeof (ui
->ui_x1
)); /* XXX still needed? */
1531 ui
->ui_pr
= IPPROTO_UDP
;
1534 ui
->ui_sport
= lport
;
1535 ui
->ui_dport
= fport
;
1536 ui
->ui_ulen
= htons((u_short
)len
+ sizeof (struct udphdr
));
1539 * Set up checksum and output datagram.
1541 if (udpcksum
&& !(inp
->inp_flags
& INP_UDP_NOCKSUM
)) {
1542 ui
->ui_sum
= in_pseudo(ui
->ui_src
.s_addr
, ui
->ui_dst
.s_addr
,
1543 htons((u_short
)len
+ sizeof (struct udphdr
) + IPPROTO_UDP
));
1544 m
->m_pkthdr
.csum_flags
= CSUM_UDP
;
1545 m
->m_pkthdr
.csum_data
= offsetof(struct udphdr
, uh_sum
);
1549 ((struct ip
*)ui
)->ip_len
= sizeof (struct udpiphdr
) + len
;
1550 ((struct ip
*)ui
)->ip_ttl
= inp
->inp_ip_ttl
; /* XXX */
1551 ((struct ip
*)ui
)->ip_tos
= inp
->inp_ip_tos
; /* XXX */
1552 udpstat
.udps_opackets
++;
1554 KERNEL_DEBUG(DBG_LAYER_OUT_END
, ui
->ui_dport
, ui
->ui_sport
,
1555 ui
->ui_src
.s_addr
, ui
->ui_dst
.s_addr
, ui
->ui_ulen
);
1558 if (ipsec_bypass
== 0 && ipsec_setsocket(m
, inp
->inp_socket
) != 0) {
1564 inpopts
= inp
->inp_options
;
1565 soopts
|= (inp
->inp_socket
->so_options
& (SO_DONTROUTE
| SO_BROADCAST
));
1566 mopts
= inp
->inp_moptions
;
1567 if (mopts
!= NULL
) {
1569 IMO_ADDREF_LOCKED(mopts
);
1570 if (IN_MULTICAST(ntohl(ui
->ui_dst
.s_addr
)) &&
1571 mopts
->imo_multicast_ifp
!= NULL
) {
1572 /* no reference needed */
1573 inp
->inp_last_outifp
= mopts
->imo_multicast_ifp
;
1578 /* Copy the cached route and take an extra reference */
1579 inp_route_copyout(inp
, &ro
);
1581 set_packet_service_class(m
, so
, msc
, 0);
1582 m
->m_pkthdr
.pkt_flowsrc
= FLOWSRC_INPCB
;
1583 m
->m_pkthdr
.pkt_flowid
= inp
->inp_flowhash
;
1584 m
->m_pkthdr
.pkt_proto
= IPPROTO_UDP
;
1585 m
->m_pkthdr
.pkt_flags
|= (PKTF_FLOW_ID
| PKTF_FLOW_LOCALSRC
);
1587 m
->m_pkthdr
.pkt_flags
|= PKTF_FLOW_ADV
;
1589 if (ipoa
.ipoa_boundif
!= IFSCOPE_NONE
)
1590 ipoa
.ipoa_flags
|= IPOAF_BOUND_IF
;
1592 if (laddr
.s_addr
!= INADDR_ANY
)
1593 ipoa
.ipoa_flags
|= IPOAF_BOUND_SRCADDR
;
1595 inp
->inp_sndinprog_cnt
++;
1597 socket_unlock(so
, 0);
1598 error
= ip_output(m
, inpopts
, &ro
, soopts
, mopts
, &ipoa
);
1604 if (error
== 0 && nstat_collect
) {
1605 boolean_t cell
, wifi
;
1607 if (ro
.ro_rt
!= NULL
) {
1608 cell
= IFNET_IS_CELLULAR(ro
.ro_rt
->rt_ifp
);
1609 wifi
= (!cell
&& IFNET_IS_WIFI(ro
.ro_rt
->rt_ifp
));
1611 cell
= wifi
= FALSE
;
1613 INP_ADD_STAT(inp
, cell
, wifi
, txpackets
, 1);
1614 INP_ADD_STAT(inp
, cell
, wifi
, txbytes
, len
);
1617 if (flowadv
&& (adv
->code
== FADV_FLOW_CONTROLLED
||
1618 adv
->code
== FADV_SUSPENDED
)) {
1619 /* return a hint to the application that
1620 * the packet has been dropped
1623 inp_set_fc_state(inp
, adv
->code
);
1626 VERIFY(inp
->inp_sndinprog_cnt
> 0);
1627 if ( --inp
->inp_sndinprog_cnt
== 0)
1628 inp
->inp_flags
&= ~(INP_FC_FEEDBACK
);
1630 /* Synchronize PCB cached route */
1631 inp_route_copyin(inp
, &ro
);
1634 if (udp_dodisconnect
) {
1635 /* Always discard the cached route for unconnected socket */
1636 ROUTE_RELEASE(&inp
->inp_route
);
1637 in_pcbdisconnect(inp
);
1638 inp
->inp_laddr
= origladdr
; /* XXX rehash? */
1639 /* no reference needed */
1640 inp
->inp_last_outifp
= origoutifp
;
1641 } else if (inp
->inp_route
.ro_rt
!= NULL
) {
1642 struct rtentry
*rt
= inp
->inp_route
.ro_rt
;
1643 struct ifnet
*outifp
;
1645 if (rt
->rt_flags
& (RTF_MULTICAST
|RTF_BROADCAST
))
1646 rt
= NULL
; /* unusable */
1648 * Always discard if it is a multicast or broadcast route.
1651 ROUTE_RELEASE(&inp
->inp_route
);
1654 * If the destination route is unicast, update outifp with
1655 * that of the route interface used by IP.
1657 if (rt
!= NULL
&& (outifp
= rt
->rt_ifp
) != inp
->inp_last_outifp
)
1658 inp
->inp_last_outifp
= outifp
; /* no reference needed */
1660 ROUTE_RELEASE(&inp
->inp_route
);
1664 * If output interface was cellular, and this socket is denied
1665 * access to it, generate an event.
1667 if (error
!= 0 && (ipoa
.ipoa_retflags
& IPOARF_IFDENIED
) &&
1668 (inp
->inp_flags
& INP_NO_IFT_CELLULAR
))
1669 soevent(so
, (SO_FILT_HINT_LOCKED
|SO_FILT_HINT_IFDENIED
));
1672 KERNEL_DEBUG(DBG_FNC_UDP_OUTPUT
| DBG_FUNC_END
, error
, 0, 0, 0, 0);
1678 ifnet_release(outif
);
1683 u_int32_t udp_sendspace
= 9216; /* really max datagram size */
1684 /* 187 1K datagrams (approx 192 KB) */
1685 u_int32_t udp_recvspace
= 187 * (1024 +
1687 sizeof (struct sockaddr_in6
)
1689 sizeof (struct sockaddr_in
)
1693 /* Check that the values of udp send and recv space do not exceed sb_max */
1695 sysctl_udp_sospace(struct sysctl_oid
*oidp
, void *arg1
, int arg2
,
1696 struct sysctl_req
*req
)
1698 #pragma unused(arg1, arg2)
1699 u_int32_t new_value
= 0, *space_p
= NULL
;
1700 int changed
= 0, error
= 0;
1701 u_quad_t sb_effective_max
= (sb_max
/(MSIZE
+MCLBYTES
)) * MCLBYTES
;
1703 switch (oidp
->oid_number
) {
1704 case UDPCTL_RECVSPACE
:
1705 space_p
= &udp_recvspace
;
1707 case UDPCTL_MAXDGRAM
:
1708 space_p
= &udp_sendspace
;
1713 error
= sysctl_io_number(req
, *space_p
, sizeof (u_int32_t
),
1714 &new_value
, &changed
);
1716 if (new_value
> 0 && new_value
<= sb_effective_max
)
1717 *space_p
= new_value
;
1724 SYSCTL_PROC(_net_inet_udp
, UDPCTL_RECVSPACE
, recvspace
,
1725 CTLTYPE_INT
| CTLFLAG_RW
| CTLFLAG_LOCKED
, &udp_recvspace
, 0,
1726 &sysctl_udp_sospace
, "IU", "Maximum incoming UDP datagram size");
1728 SYSCTL_PROC(_net_inet_udp
, UDPCTL_MAXDGRAM
, maxdgram
,
1729 CTLTYPE_INT
| CTLFLAG_RW
| CTLFLAG_LOCKED
, &udp_sendspace
, 0,
1730 &sysctl_udp_sospace
, "IU", "Maximum outgoing UDP datagram size");
1733 udp_abort(struct socket
*so
)
1737 inp
= sotoinpcb(so
);
1739 panic("%s: so=%p null inp\n", __func__
, so
);
1742 soisdisconnected(so
);
1748 udp_attach(struct socket
*so
, int proto
, struct proc
*p
)
1750 #pragma unused(proto)
1754 inp
= sotoinpcb(so
);
1756 panic ("%s so=%p inp=%p\n", __func__
, so
, inp
);
1759 error
= in_pcballoc(so
, &udbinfo
, p
);
1762 error
= soreserve(so
, udp_sendspace
, udp_recvspace
);
1765 inp
= (struct inpcb
*)so
->so_pcb
;
1766 inp
->inp_vflag
|= INP_IPV4
;
1767 inp
->inp_ip_ttl
= ip_defttl
;
1769 nstat_udp_new_pcb(inp
);
1774 udp_bind(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
1779 if (nam
->sa_family
!= 0 && nam
->sa_family
!= AF_INET
&&
1780 nam
->sa_family
!= AF_INET6
)
1781 return (EAFNOSUPPORT
);
1783 inp
= sotoinpcb(so
);
1784 if (inp
== NULL
|| (inp
->inp_flags2
& INP2_WANT_FLOW_DIVERT
))
1785 return (inp
== NULL
? EINVAL
: EPROTOTYPE
);
1786 error
= in_pcbbind(inp
, nam
, p
);
1791 udp_connect(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
1796 inp
= sotoinpcb(so
);
1797 if (inp
== NULL
|| (inp
->inp_flags2
& INP2_WANT_FLOW_DIVERT
))
1798 return (inp
== NULL
? EINVAL
: EPROTOTYPE
);
1799 if (inp
->inp_faddr
.s_addr
!= INADDR_ANY
)
1801 error
= in_pcbconnect(inp
, nam
, p
, IFSCOPE_NONE
, NULL
);
1804 if (inp
->inp_flowhash
== 0)
1805 inp
->inp_flowhash
= inp_calc_flowhash(inp
);
1811 udp_connectx_common(struct socket
*so
, int af
,
1812 struct sockaddr_list
**src_sl
, struct sockaddr_list
**dst_sl
,
1813 struct proc
*p
, uint32_t ifscope
, associd_t aid
, connid_t
*pcid
,
1814 uint32_t flags
, void *arg
, uint32_t arglen
)
1816 #pragma unused(aid, flags, arg, arglen)
1817 struct sockaddr_entry
*src_se
= NULL
, *dst_se
= NULL
;
1818 struct inpcb
*inp
= sotoinpcb(so
);
1824 VERIFY(dst_sl
!= NULL
);
1826 /* select source (if specified) and destination addresses */
1827 error
= in_selectaddrs(af
, src_sl
, &src_se
, dst_sl
, &dst_se
);
1831 VERIFY(*dst_sl
!= NULL
&& dst_se
!= NULL
);
1832 VERIFY(src_se
== NULL
|| *src_sl
!= NULL
);
1833 VERIFY(dst_se
->se_addr
->sa_family
== af
);
1834 VERIFY(src_se
== NULL
|| src_se
->se_addr
->sa_family
== af
);
1836 /* bind socket to the specified interface, if requested */
1837 if (ifscope
!= IFSCOPE_NONE
&&
1838 (error
= inp_bindif(inp
, ifscope
, NULL
)) != 0)
1841 /* if source address and/or port is specified, bind to it */
1842 if (src_se
!= NULL
) {
1843 struct sockaddr
*sa
= src_se
->se_addr
;
1844 error
= sobindlock(so
, sa
, 0); /* already locked */
1851 error
= udp_connect(so
, dst_se
->se_addr
, p
);
1855 error
= udp6_connect(so
, dst_se
->se_addr
, p
);
1863 if (error
== 0 && pcid
!= NULL
)
1864 *pcid
= 1; /* there is only 1 connection for a UDP */
1870 udp_connectx(struct socket
*so
, struct sockaddr_list
**src_sl
,
1871 struct sockaddr_list
**dst_sl
, struct proc
*p
, uint32_t ifscope
,
1872 associd_t aid
, connid_t
*pcid
, uint32_t flags
, void *arg
,
1875 return (udp_connectx_common(so
, AF_INET
, src_sl
, dst_sl
,
1876 p
, ifscope
, aid
, pcid
, flags
, arg
, arglen
));
1880 udp_detach(struct socket
*so
)
1884 inp
= sotoinpcb(so
);
1886 panic("%s: so=%p null inp\n", __func__
, so
);
1890 inp
->inp_state
= INPCB_STATE_DEAD
;
1895 udp_disconnect(struct socket
*so
)
1899 inp
= sotoinpcb(so
);
1900 if (inp
== NULL
|| (inp
->inp_flags2
& INP2_WANT_FLOW_DIVERT
))
1901 return (inp
== NULL
? EINVAL
: EPROTOTYPE
);
1902 if (inp
->inp_faddr
.s_addr
== INADDR_ANY
)
1905 in_pcbdisconnect(inp
);
1907 /* reset flow controlled state, just in case */
1908 inp_reset_fc_state(inp
);
1910 inp
->inp_laddr
.s_addr
= INADDR_ANY
;
1911 so
->so_state
&= ~SS_ISCONNECTED
; /* XXX */
1912 inp
->inp_last_outifp
= NULL
;
1917 udp_disconnectx(struct socket
*so
, associd_t aid
, connid_t cid
)
1920 if (aid
!= ASSOCID_ANY
&& aid
!= ASSOCID_ALL
)
1923 return (udp_disconnect(so
));
1927 udp_send(struct socket
*so
, int flags
, struct mbuf
*m
,
1928 struct sockaddr
*addr
, struct mbuf
*control
, struct proc
*p
)
1930 #pragma unused(flags)
1933 inp
= sotoinpcb(so
);
1934 if (inp
== NULL
|| (inp
->inp_flags2
& INP2_WANT_FLOW_DIVERT
)) {
1937 if (control
!= NULL
)
1939 return (inp
== NULL
? EINVAL
: EPROTOTYPE
);
1942 return (udp_output(inp
, m
, addr
, control
, p
));
1946 udp_shutdown(struct socket
*so
)
1950 inp
= sotoinpcb(so
);
1958 udp_lock(struct socket
*so
, int refcount
, void *debug
)
1963 lr_saved
= __builtin_return_address(0);
1967 if (so
->so_pcb
!= NULL
) {
1968 lck_mtx_assert(&((struct inpcb
*)so
->so_pcb
)->inpcb_mtx
,
1969 LCK_MTX_ASSERT_NOTOWNED
);
1970 lck_mtx_lock(&((struct inpcb
*)so
->so_pcb
)->inpcb_mtx
);
1972 panic("%s: so=%p NO PCB! lr=%p lrh= %s\n", __func__
,
1973 so
, lr_saved
, solockhistory_nr(so
));
1979 so
->lock_lr
[so
->next_lock_lr
] = lr_saved
;
1980 so
->next_lock_lr
= (so
->next_lock_lr
+1) % SO_LCKDBG_MAX
;
1985 udp_unlock(struct socket
*so
, int refcount
, void *debug
)
1990 lr_saved
= __builtin_return_address(0);
1997 if (so
->so_pcb
== NULL
) {
1998 panic("%s: so=%p NO PCB! lr=%p lrh= %s\n", __func__
,
1999 so
, lr_saved
, solockhistory_nr(so
));
2002 lck_mtx_assert(&((struct inpcb
*)so
->so_pcb
)->inpcb_mtx
,
2003 LCK_MTX_ASSERT_OWNED
);
2004 so
->unlock_lr
[so
->next_unlock_lr
] = lr_saved
;
2005 so
->next_unlock_lr
= (so
->next_unlock_lr
+1) % SO_LCKDBG_MAX
;
2006 lck_mtx_unlock(&((struct inpcb
*)so
->so_pcb
)->inpcb_mtx
);
2012 udp_getlock(struct socket
*so
, int locktype
)
2014 #pragma unused(locktype)
2015 struct inpcb
*inp
= sotoinpcb(so
);
2017 if (so
->so_pcb
== NULL
) {
2018 panic("%s: so=%p NULL so_pcb lrh= %s\n", __func__
,
2019 so
, solockhistory_nr(so
));
2022 return (&inp
->inpcb_mtx
);
2026 * UDP garbage collector callback (inpcb_timer_func_t).
2028 * Returns > 0 to keep timer active.
2031 udp_gc(struct inpcbinfo
*ipi
)
2033 struct inpcb
*inp
, *inpnxt
;
2036 if (lck_rw_try_lock_exclusive(ipi
->ipi_lock
) == FALSE
) {
2037 if (udp_gc_done
== TRUE
) {
2038 udp_gc_done
= FALSE
;
2039 /* couldn't get the lock, must lock next time */
2040 atomic_add_32(&ipi
->ipi_gc_req
.intimer_fast
, 1);
2043 lck_rw_lock_exclusive(ipi
->ipi_lock
);
2048 for (inp
= udb
.lh_first
; inp
!= NULL
; inp
= inpnxt
) {
2049 inpnxt
= inp
->inp_list
.le_next
;
2052 * Skip unless it's STOPUSING; garbage collector will
2053 * be triggered by in_pcb_checkstate() upon setting
2054 * wantcnt to that value. If the PCB is already dead,
2055 * keep gc active to anticipate wantcnt changing.
2057 if (inp
->inp_wantcnt
!= WNT_STOPUSING
)
2061 * Skip if busy, no hurry for cleanup. Keep gc active
2062 * and try the lock again during next round.
2064 if (!lck_mtx_try_lock(&inp
->inpcb_mtx
)) {
2065 atomic_add_32(&ipi
->ipi_gc_req
.intimer_fast
, 1);
2070 * Keep gc active unless usecount is 0.
2072 so
= inp
->inp_socket
;
2073 if (so
->so_usecount
== 0) {
2074 if (inp
->inp_state
!= INPCB_STATE_DEAD
) {
2076 if (SOCK_CHECK_DOM(so
, PF_INET6
))
2084 lck_mtx_unlock(&inp
->inpcb_mtx
);
2085 atomic_add_32(&ipi
->ipi_gc_req
.intimer_fast
, 1);
2088 lck_rw_done(ipi
->ipi_lock
);
2094 udp_getstat SYSCTL_HANDLER_ARGS
2096 #pragma unused(oidp, arg1, arg2)
2097 if (req
->oldptr
== USER_ADDR_NULL
)
2098 req
->oldlen
= (size_t)sizeof (struct udpstat
);
2100 return (SYSCTL_OUT(req
, &udpstat
, MIN(sizeof (udpstat
), req
->oldlen
)));
2104 udp_in_cksum_stats(u_int32_t len
)
2106 udpstat
.udps_rcv_swcsum
++;
2107 udpstat
.udps_rcv_swcsum_bytes
+= len
;
2111 udp_out_cksum_stats(u_int32_t len
)
2113 udpstat
.udps_snd_swcsum
++;
2114 udpstat
.udps_snd_swcsum_bytes
+= len
;
2119 udp_in6_cksum_stats(u_int32_t len
)
2121 udpstat
.udps_rcv6_swcsum
++;
2122 udpstat
.udps_rcv6_swcsum_bytes
+= len
;
2126 udp_out6_cksum_stats(u_int32_t len
)
2128 udpstat
.udps_snd6_swcsum
++;
2129 udpstat
.udps_snd6_swcsum_bytes
+= len
;
2134 * Checksum extended UDP header and data.
2137 udp_input_checksum(struct mbuf
*m
, struct udphdr
*uh
, int off
, int ulen
)
2139 struct ifnet
*ifp
= m
->m_pkthdr
.rcvif
;
2140 struct ip
*ip
= mtod(m
, struct ip
*);
2141 struct ipovly
*ipov
= (struct ipovly
*)ip
;
2143 if (uh
->uh_sum
== 0) {
2144 udpstat
.udps_nosum
++;
2148 if ((hwcksum_rx
|| (ifp
->if_flags
& IFF_LOOPBACK
) ||
2149 (m
->m_pkthdr
.pkt_flags
& PKTF_LOOP
)) &&
2150 (m
->m_pkthdr
.csum_flags
& CSUM_DATA_VALID
)) {
2151 if (m
->m_pkthdr
.csum_flags
& CSUM_PSEUDO_HDR
) {
2152 uh
->uh_sum
= m
->m_pkthdr
.csum_rx_val
;
2154 uint16_t sum
= m
->m_pkthdr
.csum_rx_val
;
2155 uint16_t start
= m
->m_pkthdr
.csum_rx_start
;
2158 * Perform 1's complement adjustment of octets
2159 * that got included/excluded in the hardware-
2160 * calculated checksum value. Ignore cases
2161 * where the value includes or excludes the
2162 * IP header span, as the sum for those octets
2163 * would already be 0xffff and thus no-op.
2165 if ((m
->m_pkthdr
.csum_flags
& CSUM_PARTIAL
) &&
2166 start
!= 0 && (off
- start
) != off
) {
2167 #if BYTE_ORDER != BIG_ENDIAN
2172 #endif /* BYTE_ORDER != BIG_ENDIAN */
2173 /* callee folds in sum */
2174 sum
= m_adj_sum16(m
, start
, off
, sum
);
2175 #if BYTE_ORDER != BIG_ENDIAN
2180 #endif /* BYTE_ORDER != BIG_ENDIAN */
2183 /* callee folds in sum */
2184 uh
->uh_sum
= in_pseudo(ip
->ip_src
.s_addr
,
2185 ip
->ip_dst
.s_addr
, sum
+ htonl(ulen
+ IPPROTO_UDP
));
2187 uh
->uh_sum
^= 0xffff;
2192 bcopy(ipov
->ih_x1
, b
, sizeof (ipov
->ih_x1
));
2193 bzero(ipov
->ih_x1
, sizeof (ipov
->ih_x1
));
2194 ip_sum
= ipov
->ih_len
;
2195 ipov
->ih_len
= uh
->uh_ulen
;
2196 uh
->uh_sum
= in_cksum(m
, ulen
+ sizeof (struct ip
));
2197 bcopy(b
, ipov
->ih_x1
, sizeof (ipov
->ih_x1
));
2198 ipov
->ih_len
= ip_sum
;
2200 udp_in_cksum_stats(ulen
);
2203 if (uh
->uh_sum
!= 0) {
2204 udpstat
.udps_badsum
++;
2205 IF_UDP_STATINC(ifp
, badchksum
);