2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
23 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
24 * The Regents of the University of California. All rights reserved.
26 * Redistribution and use in source and binary forms, with or without
27 * modification, are permitted provided that the following conditions
29 * 1. Redistributions of source code must retain the above copyright
30 * notice, this list of conditions and the following disclaimer.
31 * 2. Redistributions in binary form must reproduce the above copyright
32 * notice, this list of conditions and the following disclaimer in the
33 * documentation and/or other materials provided with the distribution.
34 * 3. All advertising materials mentioning features or use of this software
35 * must display the following acknowledgement:
36 * This product includes software developed by the University of
37 * California, Berkeley and its contributors.
38 * 4. Neither the name of the University nor the names of its contributors
39 * may be used to endorse or promote products derived from this software
40 * without specific prior written permission.
42 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
43 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
46 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54 * @(#)udp_usrreq.c 8.6 (Berkeley) 5/23/95
55 * $FreeBSD: src/sys/netinet/udp_usrreq.c,v 1.64.2.13 2001/08/08 18:59:54 ghelmer Exp $
58 #include <sys/param.h>
59 #include <sys/systm.h>
60 #include <sys/kernel.h>
61 #include <sys/malloc.h>
63 #include <sys/domain.h>
64 #include <sys/protosw.h>
65 #include <sys/socket.h>
66 #include <sys/socketvar.h>
67 #include <sys/sysctl.h>
68 #include <sys/syslog.h>
71 #include <net/if_types.h>
72 #include <net/route.h>
74 #include <netinet/in.h>
75 #include <netinet/in_systm.h>
76 #include <netinet/ip.h>
78 #include <netinet/ip6.h>
80 #include <netinet/in_pcb.h>
81 #include <netinet/in_var.h>
82 #include <netinet/ip_var.h>
84 #include <netinet6/ip6_var.h>
86 #include <netinet/ip_icmp.h>
87 #include <netinet/icmp_var.h>
88 #include <netinet/udp.h>
89 #include <netinet/udp_var.h>
90 #include <sys/kdebug.h>
93 #include <netinet6/ipsec.h>
94 extern int ipsec_bypass
;
95 extern lck_mtx_t
*sadb_mutex
;
99 #define DBG_LAYER_IN_BEG NETDBG_CODE(DBG_NETUDP, 0)
100 #define DBG_LAYER_IN_END NETDBG_CODE(DBG_NETUDP, 2)
101 #define DBG_LAYER_OUT_BEG NETDBG_CODE(DBG_NETUDP, 1)
102 #define DBG_LAYER_OUT_END NETDBG_CODE(DBG_NETUDP, 3)
103 #define DBG_FNC_UDP_INPUT NETDBG_CODE(DBG_NETUDP, (5 << 8))
104 #define DBG_FNC_UDP_OUTPUT NETDBG_CODE(DBG_NETUDP, (6 << 8) | 1)
107 * UDP protocol implementation.
108 * Per RFC 768, August, 1980.
111 static int udpcksum
= 1;
113 static int udpcksum
= 0; /* XXX */
115 SYSCTL_INT(_net_inet_udp
, UDPCTL_CHECKSUM
, checksum
, CTLFLAG_RW
,
119 SYSCTL_INT(_net_inet_udp
, OID_AUTO
, log_in_vain
, CTLFLAG_RW
,
120 &log_in_vain
, 0, "Log all incoming UDP packets");
122 static int blackhole
= 0;
123 SYSCTL_INT(_net_inet_udp
, OID_AUTO
, blackhole
, CTLFLAG_RW
,
124 &blackhole
, 0, "Do not send port unreachables for refused connects");
126 struct inpcbhead udb
; /* from udp_var.h */
127 #define udb6 udb /* for KAME src sync over BSD*'s */
128 struct inpcbinfo udbinfo
;
131 #define UDBHASHSIZE 16
134 extern int apple_hwcksum_rx
;
135 extern int esp_udp_encap_port
;
136 extern u_long route_generation
;
138 extern void ipfwsyslog( int level
, char *format
,...);
140 extern int fw_verbose
;
142 #define log_in_vain_log( a ) { \
143 if ( (log_in_vain == 3 ) && (fw_verbose == 2)) { /* Apple logging, log to ipfw.log */ \
149 struct udpstat udpstat
; /* from udp_var.h */
150 SYSCTL_STRUCT(_net_inet_udp
, UDPCTL_STATS
, stats
, CTLFLAG_RD
,
151 &udpstat
, udpstat
, "UDP statistics (struct udpstat, netinet/udp_var.h)");
152 SYSCTL_INT(_net_inet_udp
, OID_AUTO
, pcbcount
, CTLFLAG_RD
,
153 &udbinfo
.ipi_count
, 0, "Number of active PCBs");
155 static struct sockaddr_in udp_in
= { sizeof(udp_in
), AF_INET
};
158 struct sockaddr_in6 uin6_sin
;
159 u_char uin6_init_done
: 1;
161 { sizeof(udp_in6
.uin6_sin
), AF_INET6
},
165 struct ip6_hdr uip6_ip6
;
166 u_char uip6_init_done
: 1;
170 static void udp_append(struct inpcb
*last
, struct ip
*ip
,
171 struct mbuf
*n
, int off
);
173 static void ip_2_ip6_hdr(struct ip6_hdr
*ip6
, struct ip
*ip
);
176 static int udp_detach(struct socket
*so
);
177 static int udp_output(struct inpcb
*, struct mbuf
*, struct sockaddr
*,
178 struct mbuf
*, struct proc
*);
179 extern int ChkAddressOK( __uint32_t dstaddr
, __uint32_t srcaddr
);
185 struct inpcbinfo
*pcbinfo
;
189 udbinfo
.listhead
= &udb
;
190 udbinfo
.hashbase
= hashinit(UDBHASHSIZE
, M_PCB
, &udbinfo
.hashmask
);
191 udbinfo
.porthashbase
= hashinit(UDBHASHSIZE
, M_PCB
,
192 &udbinfo
.porthashmask
);
194 str_size
= (vm_size_t
) sizeof(struct inpcb
);
195 udbinfo
.ipi_zone
= (void *) zinit(str_size
, 80000*str_size
, 8192, "udpcb");
199 * allocate lock group attribute and group for udp pcb mutexes
201 pcbinfo
->mtx_grp_attr
= lck_grp_attr_alloc_init();
202 lck_grp_attr_setdefault(pcbinfo
->mtx_grp_attr
);
204 pcbinfo
->mtx_grp
= lck_grp_alloc_init("udppcb", pcbinfo
->mtx_grp_attr
);
206 pcbinfo
->mtx_attr
= lck_attr_alloc_init();
207 lck_attr_setdefault(pcbinfo
->mtx_attr
);
209 if ((pcbinfo
->mtx
= lck_rw_alloc_init(pcbinfo
->mtx_grp
, pcbinfo
->mtx_attr
)) == NULL
)
210 return; /* pretty much dead if this fails... */
212 in_pcb_nat_init(&udbinfo
, AF_INET
, IPPROTO_UDP
, SOCK_DGRAM
);
214 udbinfo
.ipi_zone
= zinit("udpcb", sizeof(struct inpcb
), maxsockets
,
219 /* for pcb sharing testing only */
220 stat
= in_pcb_new_share_client(&udbinfo
, &fake_owner
);
221 kprintf("udp_init in_pcb_new_share_client - stat = %d\n", stat
);
223 laddr
.s_addr
= 0x11646464;
224 faddr
.s_addr
= 0x11646465;
227 in_pcb_grab_port(&udbinfo
, 0, laddr
, &lport
, faddr
, 1600, 0, fake_owner
);
228 kprintf("udp_init in_pcb_grab_port - stat = %d\n", stat
);
230 stat
= in_pcb_rem_share_client(&udbinfo
, fake_owner
);
231 kprintf("udp_init in_pcb_rem_share_client - stat = %d\n", stat
);
233 stat
= in_pcb_new_share_client(&udbinfo
, &fake_owner
);
234 kprintf("udp_init in_pcb_new_share_client(2) - stat = %d\n", stat
);
236 laddr
.s_addr
= 0x11646464;
237 faddr
.s_addr
= 0x11646465;
240 stat
= in_pcb_grab_port(&udbinfo
, 0, laddr
, &lport
, faddr
, 1600, 0, fake_owner
);
241 kprintf("udp_init in_pcb_grab_port(2) - stat = %d\n", stat
);
247 register struct mbuf
*m
;
250 register struct ip
*ip
;
251 register struct udphdr
*uh
;
252 register struct inpcb
*inp
;
253 struct mbuf
*opts
= 0;
256 struct sockaddr
*append_sa
;
257 struct inpcbinfo
*pcbinfo
= &udbinfo
;
259 udpstat
.udps_ipackets
++;
261 KERNEL_DEBUG(DBG_FNC_UDP_INPUT
| DBG_FUNC_START
, 0,0,0,0,0);
262 if (m
->m_pkthdr
.csum_flags
& CSUM_TCP_SUM16
)
263 m
->m_pkthdr
.csum_flags
= 0; /* invalidate hwcksum for UDP */
266 * Strip IP options, if any; should skip this,
267 * make available to user, and use on returned packets,
268 * but we don't yet have a way to check the checksum
269 * with options still present.
271 if (iphlen
> sizeof (struct ip
)) {
272 ip_stripoptions(m
, (struct mbuf
*)0);
273 iphlen
= sizeof(struct ip
);
277 * Get IP and UDP header together in first mbuf.
279 ip
= mtod(m
, struct ip
*);
280 if (m
->m_len
< iphlen
+ sizeof(struct udphdr
)) {
281 if ((m
= m_pullup(m
, iphlen
+ sizeof(struct udphdr
))) == 0) {
282 udpstat
.udps_hdrops
++;
283 KERNEL_DEBUG(DBG_FNC_UDP_INPUT
| DBG_FUNC_END
, 0,0,0,0,0);
286 ip
= mtod(m
, struct ip
*);
288 uh
= (struct udphdr
*)((caddr_t
)ip
+ iphlen
);
290 /* destination port of 0 is illegal, based on RFC768. */
291 if (uh
->uh_dport
== 0)
294 KERNEL_DEBUG(DBG_LAYER_IN_BEG
, uh
->uh_dport
, uh
->uh_sport
,
295 ip
->ip_src
.s_addr
, ip
->ip_dst
.s_addr
, uh
->uh_ulen
);
298 * Make mbuf data length reflect UDP length.
299 * If not enough data to reflect UDP length, drop.
301 len
= ntohs((u_short
)uh
->uh_ulen
);
302 if (ip
->ip_len
!= len
) {
303 if (len
> ip
->ip_len
|| len
< sizeof(struct udphdr
)) {
304 udpstat
.udps_badlen
++;
307 m_adj(m
, len
- ip
->ip_len
);
308 /* ip->ip_len = len; */
311 * Save a copy of the IP header in case we want restore it
312 * for sending an ICMP error message in response.
317 * Checksum extended UDP header and data.
320 if (m
->m_pkthdr
.csum_flags
& CSUM_DATA_VALID
) {
321 if (m
->m_pkthdr
.csum_flags
& CSUM_PSEUDO_HDR
)
322 uh
->uh_sum
= m
->m_pkthdr
.csum_data
;
325 uh
->uh_sum
^= 0xffff;
329 *(uint32_t*)&b
[0] = *(uint32_t*)&((struct ipovly
*)ip
)->ih_x1
[0];
330 *(uint32_t*)&b
[4] = *(uint32_t*)&((struct ipovly
*)ip
)->ih_x1
[4];
331 *(uint8_t*)&b
[8] = *(uint8_t*)&((struct ipovly
*)ip
)->ih_x1
[8];
333 bzero(((struct ipovly
*)ip
)->ih_x1
, 9);
334 ((struct ipovly
*)ip
)->ih_len
= uh
->uh_ulen
;
335 uh
->uh_sum
= in_cksum(m
, len
+ sizeof (struct ip
));
337 *(uint32_t*)&((struct ipovly
*)ip
)->ih_x1
[0] = *(uint32_t*)&b
[0];
338 *(uint32_t*)&((struct ipovly
*)ip
)->ih_x1
[4] = *(uint32_t*)&b
[4];
339 *(uint8_t*)&((struct ipovly
*)ip
)->ih_x1
[8] = *(uint8_t*)&b
[8];
342 udpstat
.udps_badsum
++;
344 KERNEL_DEBUG(DBG_FNC_UDP_INPUT
| DBG_FUNC_END
, 0,0,0,0,0);
350 udpstat
.udps_nosum
++;
353 if (IN_MULTICAST(ntohl(ip
->ip_dst
.s_addr
)) ||
354 in_broadcast(ip
->ip_dst
, m
->m_pkthdr
.rcvif
)) {
356 lck_rw_lock_shared(pcbinfo
->mtx
);
358 * Deliver a multicast or broadcast datagram to *all* sockets
359 * for which the local and remote addresses and ports match
360 * those of the incoming datagram. This allows more than
361 * one process to receive multi/broadcasts on the same port.
362 * (This really ought to be done for unicast datagrams as
363 * well, but that would cause problems with existing
364 * applications that open both address-specific sockets and
365 * a wildcard socket listening to the same port -- they would
366 * end up receiving duplicates of every unicast datagram.
367 * Those applications open the multiple sockets to overcome an
368 * inadequacy of the UDP socket interface, but for backwards
369 * compatibility we avoid the problem here rather than
370 * fixing the interface. Maybe 4.5BSD will remedy this?)
375 * Construct sockaddr format source address.
377 udp_in
.sin_port
= uh
->uh_sport
;
378 udp_in
.sin_addr
= ip
->ip_src
;
380 * Locate pcb(s) for datagram.
381 * (Algorithm copied from raw_intr().)
385 udp_in6
.uin6_init_done
= udp_ip6
.uip6_init_done
= 0;
387 LIST_FOREACH(inp
, &udb
, inp_list
) {
389 /* Ignore nat/SharedIP dummy pcbs */
390 if (inp
->inp_socket
== &udbinfo
.nat_dummy_socket
)
393 if (inp
->inp_socket
== NULL
)
395 if (inp
!= sotoinpcb(inp
->inp_socket
))
396 panic("udp_input: bad so back ptr inp=%x\n", inp
);
398 if ((inp
->inp_vflag
& INP_IPV4
) == 0)
401 if (in_pcb_checkstate(inp
, WNT_ACQUIRE
, 0) == WNT_STOPUSING
) {
405 udp_lock(inp
->inp_socket
, 1, 0);
407 if (in_pcb_checkstate(inp
, WNT_RELEASE
, 1) == WNT_STOPUSING
) {
408 udp_unlock(inp
->inp_socket
, 1, 0);
412 if (inp
->inp_lport
!= uh
->uh_dport
) {
413 udp_unlock(inp
->inp_socket
, 1, 0);
416 if (inp
->inp_laddr
.s_addr
!= INADDR_ANY
) {
417 if (inp
->inp_laddr
.s_addr
!=
419 udp_unlock(inp
->inp_socket
, 1, 0);
423 if (inp
->inp_faddr
.s_addr
!= INADDR_ANY
) {
424 if (inp
->inp_faddr
.s_addr
!=
426 inp
->inp_fport
!= uh
->uh_sport
) {
427 udp_unlock(inp
->inp_socket
, 1, 0);
436 /* check AH/ESP integrity. */
437 if (ipsec_bypass
== 0) {
438 lck_mtx_lock(sadb_mutex
);
439 if (ipsec4_in_reject_so(m
, last
->inp_socket
)) {
440 ipsecstat
.in_polvio
++;
441 /* do not inject data to pcb */
444 lck_mtx_unlock(sadb_mutex
);
448 if ((n
= m_copy(m
, 0, M_COPYALL
)) != NULL
) {
449 udp_append(last
, ip
, n
,
451 sizeof(struct udphdr
));
453 udp_unlock(last
->inp_socket
, 1, 0);
457 * Don't look for additional matches if this one does
458 * not have either the SO_REUSEPORT or SO_REUSEADDR
459 * socket options set. This heuristic avoids searching
460 * through all pcbs in the common case of a non-shared
461 * port. It * assumes that an application will never
462 * clear these options after setting them.
464 if ((last
->inp_socket
->so_options
&(SO_REUSEPORT
|SO_REUSEADDR
)) == 0)
467 lck_rw_done(pcbinfo
->mtx
);
471 * No matching pcb found; discard datagram.
472 * (No need to send an ICMP Port Unreachable
473 * for a broadcast or multicast datgram.)
475 udpstat
.udps_noportbcast
++;
479 /* check AH/ESP integrity. */
480 if (ipsec_bypass
== 0 && m
) {
481 lck_mtx_lock(sadb_mutex
);
482 if (ipsec4_in_reject_so(m
, last
->inp_socket
)) {
483 ipsecstat
.in_polvio
++;
484 lck_mtx_unlock(sadb_mutex
);
485 udp_unlock(last
->inp_socket
, 1, 0);
488 lck_mtx_unlock(sadb_mutex
);
491 udp_append(last
, ip
, m
, iphlen
+ sizeof(struct udphdr
));
492 udp_unlock(last
->inp_socket
, 1, 0);
498 * UDP to port 4500 with a payload where the first four bytes are
499 * not zero is a UDP encapsulated IPSec packet. Packets where
500 * the payload is one byte and that byte is 0xFF are NAT keepalive
501 * packets. Decapsulate the ESP packet and carry on with IPSec input
502 * or discard the NAT keep-alive.
504 if (ipsec_bypass
== 0 && (esp_udp_encap_port
& 0xFFFF) != 0 &&
505 uh
->uh_dport
== ntohs((u_short
)esp_udp_encap_port
)) {
506 int payload_len
= len
- sizeof(struct udphdr
) > 4 ? 4 : len
- sizeof(struct udphdr
);
507 if (m
->m_len
< iphlen
+ sizeof(struct udphdr
) + payload_len
) {
508 if ((m
= m_pullup(m
, iphlen
+ sizeof(struct udphdr
) + payload_len
)) == 0) {
509 udpstat
.udps_hdrops
++;
510 KERNEL_DEBUG(DBG_FNC_UDP_INPUT
| DBG_FUNC_END
, 0,0,0,0,0);
513 ip
= mtod(m
, struct ip
*);
514 uh
= (struct udphdr
*)((caddr_t
)ip
+ iphlen
);
516 /* Check for NAT keepalive packet */
517 if (payload_len
== 1 && *(u_int8_t
*)((caddr_t
)uh
+ sizeof(struct udphdr
)) == 0xFF) {
519 KERNEL_DEBUG(DBG_FNC_UDP_INPUT
| DBG_FUNC_END
, 0,0,0,0,0);
522 else if (payload_len
== 4 && *(u_int32_t
*)((caddr_t
)uh
+ sizeof(struct udphdr
)) != 0) {
523 /* UDP encapsulated IPSec packet to pass through NAT */
526 stripsiz
= sizeof(struct udphdr
);
528 ip
= mtod(m
, struct ip
*);
529 ovbcopy((caddr_t
)ip
, (caddr_t
)(((u_char
*)ip
) + stripsiz
), iphlen
);
530 m
->m_data
+= stripsiz
;
531 m
->m_len
-= stripsiz
;
532 m
->m_pkthdr
.len
-= stripsiz
;
533 ip
= mtod(m
, struct ip
*);
534 ip
->ip_len
= ip
->ip_len
- stripsiz
;
535 ip
->ip_p
= IPPROTO_ESP
;
537 KERNEL_DEBUG(DBG_FNC_UDP_INPUT
| DBG_FUNC_END
, 0,0,0,0,0);
538 esp4_input(m
, iphlen
);
545 * Locate pcb for datagram.
547 inp
= in_pcblookup_hash(&udbinfo
, ip
->ip_src
, uh
->uh_sport
,
548 ip
->ip_dst
, uh
->uh_dport
, 1, m
->m_pkthdr
.rcvif
);
551 char buf
[MAX_IPv4_STR_LEN
];
552 char buf2
[MAX_IPv4_STR_LEN
];
554 /* check src and dst address */
555 if (log_in_vain
!= 3)
557 "Connection attempt to UDP %s:%d from %s:%d\n",
558 inet_ntop(AF_INET
, &ip
->ip_dst
, buf
, sizeof(buf
)),
560 inet_ntop(AF_INET
, &ip
->ip_src
, buf2
, sizeof(buf2
)),
561 ntohs(uh
->uh_sport
));
562 else if (!(m
->m_flags
& (M_BCAST
| M_MCAST
)) &&
563 ip
->ip_dst
.s_addr
!= ip
->ip_src
.s_addr
)
564 log_in_vain_log((LOG_INFO
,
565 "Stealth Mode connection attempt to UDP %s:%d from %s:%d\n",
566 inet_ntop(AF_INET
, &ip
->ip_dst
, buf
, sizeof(buf
)),
568 inet_ntop(AF_INET
, &ip
->ip_src
, buf2
, sizeof(buf2
)),
569 ntohs(uh
->uh_sport
)))
571 udpstat
.udps_noport
++;
572 if (m
->m_flags
& (M_BCAST
| M_MCAST
)) {
573 udpstat
.udps_noportbcast
++;
577 if (badport_bandlim(BANDLIM_ICMP_UNREACH
) < 0)
581 if (m
->m_pkthdr
.rcvif
&& m
->m_pkthdr
.rcvif
->if_type
!= IFT_LOOP
)
584 ip
->ip_len
+= iphlen
;
585 icmp_error(m
, ICMP_UNREACH
, ICMP_UNREACH_PORT
, 0, 0);
586 KERNEL_DEBUG(DBG_FNC_UDP_INPUT
| DBG_FUNC_END
, 0,0,0,0,0);
589 udp_lock(inp
->inp_socket
, 1, 0);
591 if (in_pcb_checkstate(inp
, WNT_RELEASE
, 1) == WNT_STOPUSING
) {
592 udp_unlock(inp
->inp_socket
, 1, 0);
596 if (ipsec_bypass
== 0 && inp
!= NULL
) {
597 lck_mtx_lock(sadb_mutex
);
598 if (ipsec4_in_reject_so(m
, inp
->inp_socket
)) {
599 ipsecstat
.in_polvio
++;
600 lck_mtx_unlock(sadb_mutex
);
601 udp_unlock(inp
->inp_socket
, 1, 0);
604 lck_mtx_unlock(sadb_mutex
);
609 * Construct sockaddr format source address.
610 * Stuff source address and datagram in user buffer.
612 udp_in
.sin_port
= uh
->uh_sport
;
613 udp_in
.sin_addr
= ip
->ip_src
;
614 if (inp
->inp_flags
& INP_CONTROLOPTS
615 || inp
->inp_socket
->so_options
& SO_TIMESTAMP
) {
617 if (inp
->inp_vflag
& INP_IPV6
) {
620 ip_2_ip6_hdr(&udp_ip6
.uip6_ip6
, ip
);
621 savedflags
= inp
->inp_flags
;
622 inp
->inp_flags
&= ~INP_UNMAPPABLEOPTS
;
623 ip6_savecontrol(inp
, &opts
, &udp_ip6
.uip6_ip6
, m
);
624 inp
->inp_flags
= savedflags
;
627 ip_savecontrol(inp
, &opts
, ip
, m
);
629 m_adj(m
, iphlen
+ sizeof(struct udphdr
));
631 KERNEL_DEBUG(DBG_LAYER_IN_END
, uh
->uh_dport
, uh
->uh_sport
,
632 save_ip
.ip_src
.s_addr
, save_ip
.ip_dst
.s_addr
, uh
->uh_ulen
);
635 if (inp
->inp_vflag
& INP_IPV6
) {
636 in6_sin_2_v4mapsin6(&udp_in
, &udp_in6
.uin6_sin
);
637 append_sa
= (struct sockaddr
*)&udp_in6
;
640 append_sa
= (struct sockaddr
*)&udp_in
;
641 if (sbappendaddr(&inp
->inp_socket
->so_rcv
, append_sa
, m
, opts
, NULL
) == 0) {
642 udpstat
.udps_fullsock
++;
645 sorwakeup(inp
->inp_socket
);
647 udp_unlock(inp
->inp_socket
, 1, 0);
648 KERNEL_DEBUG(DBG_FNC_UDP_INPUT
| DBG_FUNC_END
, 0,0,0,0,0);
654 KERNEL_DEBUG(DBG_FNC_UDP_INPUT
| DBG_FUNC_END
, 0,0,0,0,0);
660 ip_2_ip6_hdr(ip6
, ip
)
664 bzero(ip6
, sizeof(*ip6
));
666 ip6
->ip6_vfc
= IPV6_VERSION
;
667 ip6
->ip6_plen
= ip
->ip_len
;
668 ip6
->ip6_nxt
= ip
->ip_p
;
669 ip6
->ip6_hlim
= ip
->ip_ttl
;
670 ip6
->ip6_src
.s6_addr32
[2] = ip6
->ip6_dst
.s6_addr32
[2] =
672 ip6
->ip6_src
.s6_addr32
[3] = ip
->ip_src
.s_addr
;
673 ip6
->ip6_dst
.s6_addr32
[3] = ip
->ip_dst
.s_addr
;
678 * subroutine of udp_input(), mainly for source code readability.
679 * caller must properly init udp_ip6 and udp_in6 beforehand.
682 udp_append(last
, ip
, n
, off
)
688 struct sockaddr
*append_sa
;
689 struct mbuf
*opts
= 0;
691 if (last
->inp_flags
& INP_CONTROLOPTS
||
692 last
->inp_socket
->so_options
& SO_TIMESTAMP
) {
694 if (last
->inp_vflag
& INP_IPV6
) {
697 if (udp_ip6
.uip6_init_done
== 0) {
698 ip_2_ip6_hdr(&udp_ip6
.uip6_ip6
, ip
);
699 udp_ip6
.uip6_init_done
= 1;
701 savedflags
= last
->inp_flags
;
702 last
->inp_flags
&= ~INP_UNMAPPABLEOPTS
;
703 ip6_savecontrol(last
, &opts
, &udp_ip6
.uip6_ip6
, n
);
704 last
->inp_flags
= savedflags
;
707 ip_savecontrol(last
, &opts
, ip
, n
);
710 if (last
->inp_vflag
& INP_IPV6
) {
711 if (udp_in6
.uin6_init_done
== 0) {
712 in6_sin_2_v4mapsin6(&udp_in
, &udp_in6
.uin6_sin
);
713 udp_in6
.uin6_init_done
= 1;
715 append_sa
= (struct sockaddr
*)&udp_in6
.uin6_sin
;
718 append_sa
= (struct sockaddr
*)&udp_in
;
720 if (sbappendaddr(&last
->inp_socket
->so_rcv
, append_sa
, n
, opts
, NULL
) == 0) {
721 udpstat
.udps_fullsock
++;
723 sorwakeup(last
->inp_socket
);
727 * Notify a udp user of an asynchronous error;
728 * just wake up so that he can collect error status.
731 udp_notify(inp
, errno
)
732 register struct inpcb
*inp
;
735 inp
->inp_socket
->so_error
= errno
;
736 sorwakeup(inp
->inp_socket
);
737 sowwakeup(inp
->inp_socket
);
741 udp_ctlinput(cmd
, sa
, vip
)
748 void (*notify
)(struct inpcb
*, int) = udp_notify
;
749 struct in_addr faddr
;
752 faddr
= ((struct sockaddr_in
*)sa
)->sin_addr
;
753 if (sa
->sa_family
!= AF_INET
|| faddr
.s_addr
== INADDR_ANY
)
756 if (PRC_IS_REDIRECT(cmd
)) {
758 notify
= in_rtchange
;
759 } else if (cmd
== PRC_HOSTDEAD
)
761 else if ((unsigned)cmd
>= PRC_NCMDS
|| inetctlerrmap
[cmd
] == 0)
764 uh
= (struct udphdr
*)((caddr_t
)ip
+ (ip
->ip_hl
<< 2));
765 inp
= in_pcblookup_hash(&udbinfo
, faddr
, uh
->uh_dport
,
766 ip
->ip_src
, uh
->uh_sport
, 0, NULL
);
767 if (inp
!= NULL
&& inp
->inp_socket
!= NULL
) {
768 udp_lock(inp
->inp_socket
, 1, 0);
769 if (in_pcb_checkstate(inp
, WNT_RELEASE
, 1) == WNT_STOPUSING
) {
770 udp_unlock(inp
->inp_socket
, 1, 0);
773 (*notify
)(inp
, inetctlerrmap
[cmd
]);
774 udp_unlock(inp
->inp_socket
, 1, 0);
777 in_pcbnotifyall(&udbinfo
, faddr
, inetctlerrmap
[cmd
], notify
);
781 udp_pcblist SYSCTL_HANDLER_ARGS
784 struct inpcb
*inp
, **inp_list
;
789 * The process of preparing the TCB list is too time-consuming and
790 * resource-intensive to repeat twice on every request.
792 lck_rw_lock_exclusive(udbinfo
.mtx
);
793 if (req
->oldptr
== USER_ADDR_NULL
) {
794 n
= udbinfo
.ipi_count
;
795 req
->oldidx
= 2 * (sizeof xig
)
796 + (n
+ n
/8) * sizeof(struct xinpcb
);
797 lck_rw_done(udbinfo
.mtx
);
801 if (req
->newptr
!= USER_ADDR_NULL
) {
802 lck_rw_done(udbinfo
.mtx
);
807 * OK, now we're committed to doing something.
809 gencnt
= udbinfo
.ipi_gencnt
;
810 n
= udbinfo
.ipi_count
;
812 xig
.xig_len
= sizeof xig
;
814 xig
.xig_gen
= gencnt
;
815 xig
.xig_sogen
= so_gencnt
;
816 error
= SYSCTL_OUT(req
, &xig
, sizeof xig
);
818 lck_rw_done(udbinfo
.mtx
);
822 * We are done if there is no pcb
825 lck_rw_done(udbinfo
.mtx
);
829 inp_list
= _MALLOC(n
* sizeof *inp_list
, M_TEMP
, M_WAITOK
);
831 lck_rw_done(udbinfo
.mtx
);
835 for (inp
= LIST_FIRST(udbinfo
.listhead
), i
= 0; inp
&& i
< n
;
836 inp
= LIST_NEXT(inp
, inp_list
)) {
837 if (inp
->inp_gencnt
<= gencnt
&& inp
->inp_state
!= INPCB_STATE_DEAD
)
843 for (i
= 0; i
< n
; i
++) {
845 if (inp
->inp_gencnt
<= gencnt
&& inp
->inp_state
!= INPCB_STATE_DEAD
) {
847 xi
.xi_len
= sizeof xi
;
848 /* XXX should avoid extra copy */
849 inpcb_to_compat(inp
, &xi
.xi_inp
);
851 sotoxsocket(inp
->inp_socket
, &xi
.xi_socket
);
852 error
= SYSCTL_OUT(req
, &xi
, sizeof xi
);
857 * Give the user an updated idea of our state.
858 * If the generation differs from what we told
859 * her before, she knows that something happened
860 * while we were processing this request, and it
861 * might be necessary to retry.
863 xig
.xig_gen
= udbinfo
.ipi_gencnt
;
864 xig
.xig_sogen
= so_gencnt
;
865 xig
.xig_count
= udbinfo
.ipi_count
;
866 error
= SYSCTL_OUT(req
, &xig
, sizeof xig
);
868 FREE(inp_list
, M_TEMP
);
869 lck_rw_done(udbinfo
.mtx
);
873 SYSCTL_PROC(_net_inet_udp
, UDPCTL_PCBLIST
, pcblist
, CTLFLAG_RD
, 0, 0,
874 udp_pcblist
, "S,xinpcb", "List of active UDP sockets");
878 static __inline__ u_int16_t
879 get_socket_id(struct socket
* s
)
886 val
= (u_int16_t
)(((u_int32_t
)s
) / sizeof(struct socket
));
894 udp_output(inp
, m
, addr
, control
, p
)
895 register struct inpcb
*inp
;
897 struct sockaddr
*addr
;
898 struct mbuf
*control
;
901 register struct udpiphdr
*ui
;
902 register int len
= m
->m_pkthdr
.len
;
903 struct sockaddr_in
*sin
, src
;
904 struct in_addr origladdr
, laddr
, faddr
;
905 u_short lport
, fport
;
906 struct sockaddr_in
*ifaddr
;
907 int error
= 0, udp_dodisconnect
= 0;
910 KERNEL_DEBUG(DBG_FNC_UDP_OUTPUT
| DBG_FUNC_START
, 0,0,0,0,0);
913 m_freem(control
); /* XXX */
915 KERNEL_DEBUG(DBG_LAYER_OUT_BEG
, inp
->inp_fport
, inp
->inp_lport
,
916 inp
->inp_laddr
.s_addr
, inp
->inp_faddr
.s_addr
,
917 (htons((u_short
)len
+ sizeof (struct udphdr
))));
919 if (len
+ sizeof(struct udpiphdr
) > IP_MAXPACKET
) {
924 /* If there was a routing change, discard cached route and check
925 * that we have a valid source address.
926 * Reacquire a new source address if INADDR_ANY was specified
930 lck_mtx_assert(inp
->inpcb_mtx
, LCK_MTX_ASSERT_OWNED
);
933 if (inp
->inp_route
.ro_rt
&& inp
->inp_route
.ro_rt
->generation_id
!= route_generation
) {
934 if (ifa_foraddr(inp
->inp_laddr
.s_addr
) == 0) { /* src address is gone */
935 if (inp
->inp_flags
& INP_INADDR_ANY
)
936 inp
->inp_faddr
.s_addr
= INADDR_ANY
; /* new src will be set later */
938 error
= EADDRNOTAVAIL
;
942 rtfree(inp
->inp_route
.ro_rt
);
943 inp
->inp_route
.ro_rt
= (struct rtentry
*)0;
946 origladdr
= laddr
= inp
->inp_laddr
;
947 faddr
= inp
->inp_faddr
;
948 lport
= inp
->inp_lport
;
949 fport
= inp
->inp_fport
;
952 sin
= (struct sockaddr_in
*)addr
;
953 if (faddr
.s_addr
!= INADDR_ANY
) {
959 * In case we don't have a local port set, go through the full connect.
960 * We don't have a local port yet (ie, we can't be looked up),
961 * so it's not an issue if the input runs at the same time we do this.
963 error
= in_pcbconnect(inp
, addr
, p
);
967 laddr
= inp
->inp_laddr
;
968 lport
= inp
->inp_lport
;
969 faddr
= inp
->inp_faddr
;
970 fport
= inp
->inp_fport
;
971 udp_dodisconnect
= 1;
975 * we have a full address and a local port.
976 * use those info to build the packet without changing the pcb
977 * and interfering with the input path. See 3851370
979 if (laddr
.s_addr
== INADDR_ANY
) {
980 if ((error
= in_pcbladdr(inp
, addr
, &ifaddr
)) != 0)
982 laddr
= ifaddr
->sin_addr
;
983 inp
->inp_flags
|= INP_INADDR_ANY
; /* from pcbconnect: remember we don't care about src addr.*/
986 faddr
= sin
->sin_addr
;
987 fport
= sin
->sin_port
;
990 if (faddr
.s_addr
== INADDR_ANY
) {
998 * Calculate data length and get a mbuf
999 * for UDP and IP headers.
1001 M_PREPEND(m
, sizeof(struct udpiphdr
), M_DONTWAIT
);
1008 * Fill in mbuf with extended UDP header
1009 * and addresses and length put into network format.
1011 ui
= mtod(m
, struct udpiphdr
*);
1012 bzero(ui
->ui_x1
, sizeof(ui
->ui_x1
)); /* XXX still needed? */
1013 ui
->ui_pr
= IPPROTO_UDP
;
1016 ui
->ui_sport
= lport
;
1017 ui
->ui_dport
= fport
;
1018 ui
->ui_ulen
= htons((u_short
)len
+ sizeof(struct udphdr
));
1021 * Set up checksum and output datagram.
1024 ui
->ui_sum
= in_pseudo(ui
->ui_src
.s_addr
, ui
->ui_dst
.s_addr
,
1025 htons((u_short
)len
+ sizeof(struct udphdr
) + IPPROTO_UDP
));
1026 m
->m_pkthdr
.csum_flags
= CSUM_UDP
;
1027 m
->m_pkthdr
.csum_data
= offsetof(struct udphdr
, uh_sum
);
1031 ((struct ip
*)ui
)->ip_len
= sizeof (struct udpiphdr
) + len
;
1032 ((struct ip
*)ui
)->ip_ttl
= inp
->inp_ip_ttl
; /* XXX */
1033 ((struct ip
*)ui
)->ip_tos
= inp
->inp_ip_tos
; /* XXX */
1034 udpstat
.udps_opackets
++;
1036 KERNEL_DEBUG(DBG_LAYER_OUT_END
, ui
->ui_dport
, ui
->ui_sport
,
1037 ui
->ui_src
.s_addr
, ui
->ui_dst
.s_addr
, ui
->ui_ulen
);
1040 if (ipsec_bypass
== 0 && ipsec_setsocket(m
, inp
->inp_socket
) != 0) {
1045 m
->m_pkthdr
.socket_id
= get_socket_id(inp
->inp_socket
);
1046 error
= ip_output_list(m
, 0, inp
->inp_options
, &inp
->inp_route
,
1047 (inp
->inp_socket
->so_options
& (SO_DONTROUTE
| SO_BROADCAST
)),
1050 if (udp_dodisconnect
) {
1051 in_pcbdisconnect(inp
);
1052 inp
->inp_laddr
= origladdr
; /* XXX rehash? */
1054 KERNEL_DEBUG(DBG_FNC_UDP_OUTPUT
| DBG_FUNC_END
, error
, 0,0,0,0);
1058 if (udp_dodisconnect
) {
1059 in_pcbdisconnect(inp
);
1060 inp
->inp_laddr
= origladdr
; /* XXX rehash? */
1065 KERNEL_DEBUG(DBG_FNC_UDP_OUTPUT
| DBG_FUNC_END
, error
, 0,0,0,0);
1069 u_long udp_sendspace
= 9216; /* really max datagram size */
1070 /* 40 1K datagrams */
1071 SYSCTL_INT(_net_inet_udp
, UDPCTL_MAXDGRAM
, maxdgram
, CTLFLAG_RW
,
1072 &udp_sendspace
, 0, "Maximum outgoing UDP datagram size");
1074 u_long udp_recvspace
= 40 * (1024 +
1076 sizeof(struct sockaddr_in6
)
1078 sizeof(struct sockaddr_in
)
1081 SYSCTL_INT(_net_inet_udp
, UDPCTL_RECVSPACE
, recvspace
, CTLFLAG_RW
,
1082 &udp_recvspace
, 0, "Maximum incoming UDP datagram size");
1085 udp_abort(struct socket
*so
)
1089 inp
= sotoinpcb(so
);
1091 panic("udp_abort: so=%x null inp\n", so
); /* ??? possible? panic instead? */
1092 soisdisconnected(so
);
1098 udp_attach(struct socket
*so
, int proto
, struct proc
*p
)
1103 inp
= sotoinpcb(so
);
1105 panic ("udp_attach so=%x inp=%x\n", so
, inp
);
1107 error
= in_pcballoc(so
, &udbinfo
, p
);
1110 error
= soreserve(so
, udp_sendspace
, udp_recvspace
);
1113 inp
= (struct inpcb
*)so
->so_pcb
;
1114 inp
->inp_vflag
|= INP_IPV4
;
1115 inp
->inp_ip_ttl
= ip_defttl
;
1120 udp_bind(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
1125 inp
= sotoinpcb(so
);
1128 error
= in_pcbbind(inp
, nam
, p
);
1133 udp_connect(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
1138 inp
= sotoinpcb(so
);
1141 if (inp
->inp_faddr
.s_addr
!= INADDR_ANY
)
1143 error
= in_pcbconnect(inp
, nam
, p
);
1150 udp_detach(struct socket
*so
)
1154 inp
= sotoinpcb(so
);
1156 panic("udp_detach: so=%x null inp\n", so
); /* ??? possible? panic instead? */
1158 inp
->inp_state
= INPCB_STATE_DEAD
;
1163 udp_disconnect(struct socket
*so
)
1167 inp
= sotoinpcb(so
);
1170 if (inp
->inp_faddr
.s_addr
== INADDR_ANY
)
1173 in_pcbdisconnect(inp
);
1174 inp
->inp_laddr
.s_addr
= INADDR_ANY
;
1175 so
->so_state
&= ~SS_ISCONNECTED
; /* XXX */
1180 udp_send(struct socket
*so
, int flags
, struct mbuf
*m
, struct sockaddr
*addr
,
1181 struct mbuf
*control
, struct proc
*p
)
1185 inp
= sotoinpcb(so
);
1190 return udp_output(inp
, m
, addr
, control
, p
);
1194 udp_shutdown(struct socket
*so
)
1198 inp
= sotoinpcb(so
);
1205 struct pr_usrreqs udp_usrreqs
= {
1206 udp_abort
, pru_accept_notsupp
, udp_attach
, udp_bind
, udp_connect
,
1207 pru_connect2_notsupp
, in_control
, udp_detach
, udp_disconnect
,
1208 pru_listen_notsupp
, in_setpeeraddr
, pru_rcvd_notsupp
,
1209 pru_rcvoob_notsupp
, udp_send
, pru_sense_null
, udp_shutdown
,
1210 in_setsockaddr
, sosend
, soreceive
, pru_sopoll_notsupp
1215 udp_lock(so
, refcount
, debug
)
1217 int refcount
, debug
;
1222 __asm__
volatile("mflr %0" : "=r" (lr_saved
));
1224 else lr_saved
= debug
;
1228 lck_mtx_assert(((struct inpcb
*)so
->so_pcb
)->inpcb_mtx
, LCK_MTX_ASSERT_NOTOWNED
);
1229 lck_mtx_lock(((struct inpcb
*)so
->so_pcb
)->inpcb_mtx
);
1232 panic("udp_lock: so=%x NO PCB! lr=%x\n", so
, lr_saved
);
1233 lck_mtx_assert(so
->so_proto
->pr_domain
->dom_mtx
, LCK_MTX_ASSERT_NOTOWNED
);
1234 lck_mtx_lock(so
->so_proto
->pr_domain
->dom_mtx
);
1240 so
->reserved3
= lr_saved
;
1245 udp_unlock(so
, refcount
, debug
)
1251 struct inpcb
*inp
= sotoinpcb(so
);
1252 struct inpcbinfo
*pcbinfo
= &udbinfo
;
1255 __asm__
volatile("mflr %0" : "=r" (lr_saved
));
1257 else lr_saved
= debug
;
1262 if (so
->so_usecount
== 0 && (inp
->inp_wantcnt
== WNT_STOPUSING
)) {
1263 if (lck_rw_try_lock_exclusive(pcbinfo
->mtx
)) {
1265 lck_rw_done(pcbinfo
->mtx
);
1271 if (so
->so_pcb
== NULL
) {
1272 panic("udp_unlock: so=%x NO PCB! lr=%x\n", so
, lr_saved
);
1273 lck_mtx_assert(so
->so_proto
->pr_domain
->dom_mtx
, LCK_MTX_ASSERT_OWNED
);
1274 lck_mtx_unlock(so
->so_proto
->pr_domain
->dom_mtx
);
1277 lck_mtx_assert(((struct inpcb
*)so
->so_pcb
)->inpcb_mtx
, LCK_MTX_ASSERT_OWNED
);
1278 lck_mtx_unlock(((struct inpcb
*)so
->so_pcb
)->inpcb_mtx
);
1282 so
->reserved4
= lr_saved
;
1287 udp_getlock(so
, locktype
)
1291 struct inpcb
*inp
= sotoinpcb(so
);
1295 return(inp
->inpcb_mtx
);
1297 panic("udp_getlock: so=%x NULL so_pcb\n", so
);
1298 return (so
->so_proto
->pr_domain
->dom_mtx
);
1305 struct inpcb
*inp
, *inpnxt
;
1307 struct inpcbinfo
*pcbinfo
= &udbinfo
;
1309 lck_rw_lock_exclusive(pcbinfo
->mtx
);
1311 for (inp
= udb
.lh_first
; inp
!= NULL
; inp
= inpnxt
) {
1312 inpnxt
= inp
->inp_list
.le_next
;
1314 /* Ignore nat/SharedIP dummy pcbs */
1315 if (inp
->inp_socket
== &udbinfo
.nat_dummy_socket
)
1318 if (inp
->inp_wantcnt
!= WNT_STOPUSING
)
1321 so
= inp
->inp_socket
;
1322 if (!lck_mtx_try_lock(inp
->inpcb_mtx
)) /* skip if busy, no hurry for cleanup... */
1325 if (so
->so_usecount
== 0)
1328 lck_mtx_unlock(inp
->inpcb_mtx
);
1330 lck_rw_done(pcbinfo
->mtx
);
1334 ChkAddressOK( __uint32_t dstaddr
, __uint32_t srcaddr
)
1336 if ( dstaddr
== srcaddr
){