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/route.h>
73 #include <netinet/in.h>
74 #include <netinet/in_systm.h>
75 #include <netinet/ip.h>
77 #include <netinet/ip6.h>
79 #include <netinet/in_pcb.h>
80 #include <netinet/in_var.h>
81 #include <netinet/ip_var.h>
83 #include <netinet6/ip6_var.h>
85 #include <netinet/ip_icmp.h>
86 #include <netinet/icmp_var.h>
87 #include <netinet/udp.h>
88 #include <netinet/udp_var.h>
89 #include <sys/kdebug.h>
92 #include <netinet6/ipsec.h>
93 extern int ipsec_bypass
;
97 #define DBG_LAYER_IN_BEG NETDBG_CODE(DBG_NETUDP, 0)
98 #define DBG_LAYER_IN_END NETDBG_CODE(DBG_NETUDP, 2)
99 #define DBG_LAYER_OUT_BEG NETDBG_CODE(DBG_NETUDP, 1)
100 #define DBG_LAYER_OUT_END NETDBG_CODE(DBG_NETUDP, 3)
101 #define DBG_FNC_UDP_INPUT NETDBG_CODE(DBG_NETUDP, (5 << 8))
102 #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
;
136 struct udpstat udpstat
; /* from udp_var.h */
137 SYSCTL_STRUCT(_net_inet_udp
, UDPCTL_STATS
, stats
, CTLFLAG_RD
,
138 &udpstat
, udpstat
, "UDP statistics (struct udpstat, netinet/udp_var.h)");
140 static struct sockaddr_in udp_in
= { sizeof(udp_in
), AF_INET
};
143 struct sockaddr_in6 uin6_sin
;
144 u_char uin6_init_done
: 1;
146 { sizeof(udp_in6
.uin6_sin
), AF_INET6
},
150 struct ip6_hdr uip6_ip6
;
151 u_char uip6_init_done
: 1;
155 static void udp_append
__P((struct inpcb
*last
, struct ip
*ip
,
156 struct mbuf
*n
, int off
));
158 static void ip_2_ip6_hdr
__P((struct ip6_hdr
*ip6
, struct ip
*ip
));
161 static int udp_detach
__P((struct socket
*so
));
162 static int udp_output
__P((struct inpcb
*, struct mbuf
*, struct sockaddr
*,
163 struct mbuf
*, struct proc
*));
171 struct in_addr laddr
;
172 struct in_addr faddr
;
176 udbinfo
.listhead
= &udb
;
177 udbinfo
.hashbase
= hashinit(UDBHASHSIZE
, M_PCB
, &udbinfo
.hashmask
);
178 udbinfo
.porthashbase
= hashinit(UDBHASHSIZE
, M_PCB
,
179 &udbinfo
.porthashmask
);
181 str_size
= (vm_size_t
) sizeof(struct inpcb
);
182 udbinfo
.ipi_zone
= (void *) zinit(str_size
, 80000*str_size
, 8192, "udpcb");
184 udbinfo
.last_pcb
= 0;
185 in_pcb_nat_init(&udbinfo
, AF_INET
, IPPROTO_UDP
, SOCK_DGRAM
);
187 udbinfo
.ipi_zone
= zinit("udpcb", sizeof(struct inpcb
), maxsockets
,
192 /* for pcb sharing testing only */
193 stat
= in_pcb_new_share_client(&udbinfo
, &fake_owner
);
194 kprintf("udp_init in_pcb_new_share_client - stat = %d\n", stat
);
196 laddr
.s_addr
= 0x11646464;
197 faddr
.s_addr
= 0x11646465;
200 in_pcb_grab_port(&udbinfo
, 0, laddr
, &lport
, faddr
, 1600, 0, fake_owner
);
201 kprintf("udp_init in_pcb_grab_port - stat = %d\n", stat
);
203 stat
= in_pcb_rem_share_client(&udbinfo
, fake_owner
);
204 kprintf("udp_init in_pcb_rem_share_client - stat = %d\n", stat
);
206 stat
= in_pcb_new_share_client(&udbinfo
, &fake_owner
);
207 kprintf("udp_init in_pcb_new_share_client(2) - stat = %d\n", stat
);
209 laddr
.s_addr
= 0x11646464;
210 faddr
.s_addr
= 0x11646465;
213 stat
= in_pcb_grab_port(&udbinfo
, 0, laddr
, &lport
, faddr
, 1600, 0, fake_owner
);
214 kprintf("udp_init in_pcb_grab_port(2) - stat = %d\n", stat
);
220 register struct mbuf
*m
;
223 register struct ip
*ip
;
224 register struct udphdr
*uh
;
225 register struct inpcb
*inp
;
226 struct mbuf
*opts
= 0;
229 struct sockaddr
*append_sa
;
231 udpstat
.udps_ipackets
++;
233 KERNEL_DEBUG(DBG_FNC_UDP_INPUT
| DBG_FUNC_START
, 0,0,0,0,0);
234 if (m
->m_pkthdr
.csum_flags
& CSUM_TCP_SUM16
)
235 m
->m_pkthdr
.csum_flags
= 0; /* invalidate hwcksum for UDP */
238 * Strip IP options, if any; should skip this,
239 * make available to user, and use on returned packets,
240 * but we don't yet have a way to check the checksum
241 * with options still present.
243 if (iphlen
> sizeof (struct ip
)) {
244 ip_stripoptions(m
, (struct mbuf
*)0);
245 iphlen
= sizeof(struct ip
);
249 * Get IP and UDP header together in first mbuf.
251 ip
= mtod(m
, struct ip
*);
252 if (m
->m_len
< iphlen
+ sizeof(struct udphdr
)) {
253 if ((m
= m_pullup(m
, iphlen
+ sizeof(struct udphdr
))) == 0) {
254 udpstat
.udps_hdrops
++;
255 KERNEL_DEBUG(DBG_FNC_UDP_INPUT
| DBG_FUNC_END
, 0,0,0,0,0);
258 ip
= mtod(m
, struct ip
*);
260 uh
= (struct udphdr
*)((caddr_t
)ip
+ iphlen
);
262 /* destination port of 0 is illegal, based on RFC768. */
263 if (uh
->uh_dport
== 0)
266 KERNEL_DEBUG(DBG_LAYER_IN_BEG
, uh
->uh_dport
, uh
->uh_sport
,
267 ip
->ip_src
.s_addr
, ip
->ip_dst
.s_addr
, uh
->uh_ulen
);
270 * Make mbuf data length reflect UDP length.
271 * If not enough data to reflect UDP length, drop.
273 len
= ntohs((u_short
)uh
->uh_ulen
);
274 if (ip
->ip_len
!= len
) {
275 if (len
> ip
->ip_len
|| len
< sizeof(struct udphdr
)) {
276 udpstat
.udps_badlen
++;
279 m_adj(m
, len
- ip
->ip_len
);
280 /* ip->ip_len = len; */
283 * Save a copy of the IP header in case we want restore it
284 * for sending an ICMP error message in response.
289 * Checksum extended UDP header and data.
292 if (m
->m_pkthdr
.csum_flags
& CSUM_DATA_VALID
) {
293 if (m
->m_pkthdr
.csum_flags
& CSUM_PSEUDO_HDR
)
294 uh
->uh_sum
= m
->m_pkthdr
.csum_data
;
297 uh
->uh_sum
^= 0xffff;
301 *(uint32_t*)&b
[0] = *(uint32_t*)&((struct ipovly
*)ip
)->ih_x1
[0];
302 *(uint32_t*)&b
[4] = *(uint32_t*)&((struct ipovly
*)ip
)->ih_x1
[4];
303 *(uint8_t*)&b
[8] = *(uint8_t*)&((struct ipovly
*)ip
)->ih_x1
[8];
305 bzero(((struct ipovly
*)ip
)->ih_x1
, 9);
306 ((struct ipovly
*)ip
)->ih_len
= uh
->uh_ulen
;
307 uh
->uh_sum
= in_cksum(m
, len
+ sizeof (struct ip
));
309 *(uint32_t*)&((struct ipovly
*)ip
)->ih_x1
[0] = *(uint32_t*)&b
[0];
310 *(uint32_t*)&((struct ipovly
*)ip
)->ih_x1
[4] = *(uint32_t*)&b
[4];
311 *(uint8_t*)&((struct ipovly
*)ip
)->ih_x1
[8] = *(uint8_t*)&b
[8];
314 udpstat
.udps_badsum
++;
316 KERNEL_DEBUG(DBG_FNC_UDP_INPUT
| DBG_FUNC_END
, 0,0,0,0,0);
322 udpstat
.udps_nosum
++;
325 if (IN_MULTICAST(ntohl(ip
->ip_dst
.s_addr
)) ||
326 in_broadcast(ip
->ip_dst
, m
->m_pkthdr
.rcvif
)) {
329 * Deliver a multicast or broadcast datagram to *all* sockets
330 * for which the local and remote addresses and ports match
331 * those of the incoming datagram. This allows more than
332 * one process to receive multi/broadcasts on the same port.
333 * (This really ought to be done for unicast datagrams as
334 * well, but that would cause problems with existing
335 * applications that open both address-specific sockets and
336 * a wildcard socket listening to the same port -- they would
337 * end up receiving duplicates of every unicast datagram.
338 * Those applications open the multiple sockets to overcome an
339 * inadequacy of the UDP socket interface, but for backwards
340 * compatibility we avoid the problem here rather than
341 * fixing the interface. Maybe 4.5BSD will remedy this?)
345 * Construct sockaddr format source address.
347 udp_in
.sin_port
= uh
->uh_sport
;
348 udp_in
.sin_addr
= ip
->ip_src
;
350 * Locate pcb(s) for datagram.
351 * (Algorithm copied from raw_intr().)
355 udp_in6
.uin6_init_done
= udp_ip6
.uip6_init_done
= 0;
357 LIST_FOREACH(inp
, &udb
, inp_list
) {
359 /* Ignore nat/SharedIP dummy pcbs */
360 if (inp
->inp_socket
== &udbinfo
.nat_dummy_socket
)
364 if ((inp
->inp_vflag
& INP_IPV4
) == 0)
367 if (inp
->inp_lport
!= uh
->uh_dport
)
369 if (inp
->inp_laddr
.s_addr
!= INADDR_ANY
) {
370 if (inp
->inp_laddr
.s_addr
!=
374 if (inp
->inp_faddr
.s_addr
!= INADDR_ANY
) {
375 if (inp
->inp_faddr
.s_addr
!=
377 inp
->inp_fport
!= uh
->uh_sport
)
385 /* check AH/ESP integrity. */
386 if (ipsec_bypass
== 0 && ipsec4_in_reject_so(m
, last
->inp_socket
)) {
387 ipsecstat
.in_polvio
++;
388 /* do not inject data to pcb */
391 if ((n
= m_copy(m
, 0, M_COPYALL
)) != NULL
) {
392 udp_append(last
, ip
, n
,
394 sizeof(struct udphdr
));
399 * Don't look for additional matches if this one does
400 * not have either the SO_REUSEPORT or SO_REUSEADDR
401 * socket options set. This heuristic avoids searching
402 * through all pcbs in the common case of a non-shared
403 * port. It * assumes that an application will never
404 * clear these options after setting them.
406 if ((last
->inp_socket
->so_options
&(SO_REUSEPORT
|SO_REUSEADDR
)) == 0)
412 * No matching pcb found; discard datagram.
413 * (No need to send an ICMP Port Unreachable
414 * for a broadcast or multicast datgram.)
416 udpstat
.udps_noportbcast
++;
420 /* check AH/ESP integrity. */
421 if (ipsec_bypass
== 0 && m
&& ipsec4_in_reject_so(m
, last
->inp_socket
)) {
422 ipsecstat
.in_polvio
++;
426 udp_append(last
, ip
, m
, iphlen
+ sizeof(struct udphdr
));
430 * Locate pcb for datagram.
432 inp
= in_pcblookup_hash(&udbinfo
, ip
->ip_src
, uh
->uh_sport
,
433 ip
->ip_dst
, uh
->uh_dport
, 1, m
->m_pkthdr
.rcvif
);
436 char buf
[4*sizeof "123"];
438 strcpy(buf
, inet_ntoa(ip
->ip_dst
));
440 "Connection attempt to UDP %s:%d from %s:%d\n",
441 buf
, ntohs(uh
->uh_dport
), inet_ntoa(ip
->ip_src
),
442 ntohs(uh
->uh_sport
));
444 udpstat
.udps_noport
++;
445 if (m
->m_flags
& (M_BCAST
| M_MCAST
)) {
446 udpstat
.udps_noportbcast
++;
450 if (badport_bandlim(BANDLIM_ICMP_UNREACH
) < 0)
456 ip
->ip_len
+= iphlen
;
457 icmp_error(m
, ICMP_UNREACH
, ICMP_UNREACH_PORT
, 0, 0);
458 KERNEL_DEBUG(DBG_FNC_UDP_INPUT
| DBG_FUNC_END
, 0,0,0,0,0);
462 if (ipsec_bypass
== 0 && inp
!= NULL
&& ipsec4_in_reject_so(m
, inp
->inp_socket
)) {
463 ipsecstat
.in_polvio
++;
469 * Construct sockaddr format source address.
470 * Stuff source address and datagram in user buffer.
472 udp_in
.sin_port
= uh
->uh_sport
;
473 udp_in
.sin_addr
= ip
->ip_src
;
474 if (inp
->inp_flags
& INP_CONTROLOPTS
475 || inp
->inp_socket
->so_options
& SO_TIMESTAMP
) {
477 if (inp
->inp_vflag
& INP_IPV6
) {
480 ip_2_ip6_hdr(&udp_ip6
.uip6_ip6
, ip
);
481 savedflags
= inp
->inp_flags
;
482 inp
->inp_flags
&= ~INP_UNMAPPABLEOPTS
;
483 ip6_savecontrol(inp
, &opts
, &udp_ip6
.uip6_ip6
, m
);
484 inp
->inp_flags
= savedflags
;
487 ip_savecontrol(inp
, &opts
, ip
, m
);
489 m_adj(m
, iphlen
+ sizeof(struct udphdr
));
491 KERNEL_DEBUG(DBG_LAYER_IN_END
, uh
->uh_dport
, uh
->uh_sport
,
492 save_ip
.ip_src
.s_addr
, save_ip
.ip_dst
.s_addr
, uh
->uh_ulen
);
495 if (inp
->inp_vflag
& INP_IPV6
) {
496 in6_sin_2_v4mapsin6(&udp_in
, &udp_in6
.uin6_sin
);
497 append_sa
= (struct sockaddr
*)&udp_in6
;
500 append_sa
= (struct sockaddr
*)&udp_in
;
501 if (sbappendaddr(&inp
->inp_socket
->so_rcv
, append_sa
, m
, opts
) == 0) {
502 udpstat
.udps_fullsock
++;
505 sorwakeup(inp
->inp_socket
);
506 KERNEL_DEBUG(DBG_FNC_UDP_INPUT
| DBG_FUNC_END
, 0,0,0,0,0);
512 KERNEL_DEBUG(DBG_FNC_UDP_INPUT
| DBG_FUNC_END
, 0,0,0,0,0);
518 ip_2_ip6_hdr(ip6
, ip
)
522 bzero(ip6
, sizeof(*ip6
));
524 ip6
->ip6_vfc
= IPV6_VERSION
;
525 ip6
->ip6_plen
= ip
->ip_len
;
526 ip6
->ip6_nxt
= ip
->ip_p
;
527 ip6
->ip6_hlim
= ip
->ip_ttl
;
528 ip6
->ip6_src
.s6_addr32
[2] = ip6
->ip6_dst
.s6_addr32
[2] =
530 ip6
->ip6_src
.s6_addr32
[3] = ip
->ip_src
.s_addr
;
531 ip6
->ip6_dst
.s6_addr32
[3] = ip
->ip_dst
.s_addr
;
536 * subroutine of udp_input(), mainly for source code readability.
537 * caller must properly init udp_ip6 and udp_in6 beforehand.
540 udp_append(last
, ip
, n
, off
)
546 struct sockaddr
*append_sa
;
547 struct mbuf
*opts
= 0;
549 if (last
->inp_flags
& INP_CONTROLOPTS
||
550 last
->inp_socket
->so_options
& SO_TIMESTAMP
) {
552 if (last
->inp_vflag
& INP_IPV6
) {
555 if (udp_ip6
.uip6_init_done
== 0) {
556 ip_2_ip6_hdr(&udp_ip6
.uip6_ip6
, ip
);
557 udp_ip6
.uip6_init_done
= 1;
559 savedflags
= last
->inp_flags
;
560 last
->inp_flags
&= ~INP_UNMAPPABLEOPTS
;
561 ip6_savecontrol(last
, &opts
, &udp_ip6
.uip6_ip6
, n
);
562 last
->inp_flags
= savedflags
;
565 ip_savecontrol(last
, &opts
, ip
, n
);
568 if (last
->inp_vflag
& INP_IPV6
) {
569 if (udp_in6
.uin6_init_done
== 0) {
570 in6_sin_2_v4mapsin6(&udp_in
, &udp_in6
.uin6_sin
);
571 udp_in6
.uin6_init_done
= 1;
573 append_sa
= (struct sockaddr
*)&udp_in6
.uin6_sin
;
576 append_sa
= (struct sockaddr
*)&udp_in
;
578 if (sbappendaddr(&last
->inp_socket
->so_rcv
, append_sa
, n
, opts
) == 0) {
582 udpstat
.udps_fullsock
++;
584 sorwakeup(last
->inp_socket
);
588 * Notify a udp user of an asynchronous error;
589 * just wake up so that he can collect error status.
592 udp_notify(inp
, errno
)
593 register struct inpcb
*inp
;
596 inp
->inp_socket
->so_error
= errno
;
597 sorwakeup(inp
->inp_socket
);
598 sowwakeup(inp
->inp_socket
);
602 udp_ctlinput(cmd
, sa
, vip
)
609 void (*notify
) __P((struct inpcb
*, int)) = udp_notify
;
610 struct in_addr faddr
;
614 faddr
= ((struct sockaddr_in
*)sa
)->sin_addr
;
615 if (sa
->sa_family
!= AF_INET
|| faddr
.s_addr
== INADDR_ANY
)
618 if (PRC_IS_REDIRECT(cmd
)) {
620 notify
= in_rtchange
;
621 } else if (cmd
== PRC_HOSTDEAD
)
623 else if ((unsigned)cmd
>= PRC_NCMDS
|| inetctlerrmap
[cmd
] == 0)
627 uh
= (struct udphdr
*)((caddr_t
)ip
+ (ip
->ip_hl
<< 2));
628 inp
= in_pcblookup_hash(&udbinfo
, faddr
, uh
->uh_dport
,
629 ip
->ip_src
, uh
->uh_sport
, 0, NULL
);
630 if (inp
!= NULL
&& inp
->inp_socket
!= NULL
)
631 (*notify
)(inp
, inetctlerrmap
[cmd
]);
634 in_pcbnotifyall(&udb
, faddr
, inetctlerrmap
[cmd
], notify
);
638 udp_pcblist SYSCTL_HANDLER_ARGS
641 struct inpcb
*inp
, **inp_list
;
646 * The process of preparing the TCB list is too time-consuming and
647 * resource-intensive to repeat twice on every request.
649 if (req
->oldptr
== 0) {
650 n
= udbinfo
.ipi_count
;
651 req
->oldidx
= 2 * (sizeof xig
)
652 + (n
+ n
/8) * sizeof(struct xinpcb
);
656 if (req
->newptr
!= 0)
660 * OK, now we're committed to doing something.
663 gencnt
= udbinfo
.ipi_gencnt
;
664 n
= udbinfo
.ipi_count
;
667 xig
.xig_len
= sizeof xig
;
669 xig
.xig_gen
= gencnt
;
670 xig
.xig_sogen
= so_gencnt
;
671 error
= SYSCTL_OUT(req
, &xig
, sizeof xig
);
675 * We are done if there is no pcb
680 inp_list
= _MALLOC(n
* sizeof *inp_list
, M_TEMP
, M_WAITOK
);
685 for (inp
= LIST_FIRST(udbinfo
.listhead
), i
= 0; inp
&& i
< n
;
686 inp
= LIST_NEXT(inp
, inp_list
)) {
687 if (inp
->inp_gencnt
<= gencnt
)
694 for (i
= 0; i
< n
; i
++) {
696 if (inp
->inp_gencnt
<= gencnt
) {
698 xi
.xi_len
= sizeof xi
;
699 /* XXX should avoid extra copy */
700 bcopy(inp
, &xi
.xi_inp
, sizeof *inp
);
702 sotoxsocket(inp
->inp_socket
, &xi
.xi_socket
);
703 error
= SYSCTL_OUT(req
, &xi
, sizeof xi
);
708 * Give the user an updated idea of our state.
709 * If the generation differs from what we told
710 * her before, she knows that something happened
711 * while we were processing this request, and it
712 * might be necessary to retry.
715 xig
.xig_gen
= udbinfo
.ipi_gencnt
;
716 xig
.xig_sogen
= so_gencnt
;
717 xig
.xig_count
= udbinfo
.ipi_count
;
719 error
= SYSCTL_OUT(req
, &xig
, sizeof xig
);
721 FREE(inp_list
, M_TEMP
);
725 SYSCTL_PROC(_net_inet_udp
, UDPCTL_PCBLIST
, pcblist
, CTLFLAG_RD
, 0, 0,
726 udp_pcblist
, "S,xinpcb", "List of active UDP sockets");
731 udp_output(inp
, m
, addr
, control
, p
)
732 register struct inpcb
*inp
;
734 struct sockaddr
*addr
;
735 struct mbuf
*control
;
738 register struct udpiphdr
*ui
;
739 register int len
= m
->m_pkthdr
.len
;
740 struct in_addr laddr
;
741 int s
= 0, error
= 0;
743 KERNEL_DEBUG(DBG_FNC_UDP_OUTPUT
| DBG_FUNC_START
, 0,0,0,0,0);
746 m_freem(control
); /* XXX */
748 KERNEL_DEBUG(DBG_LAYER_OUT_BEG
, inp
->inp_fport
, inp
->inp_lport
,
749 inp
->inp_laddr
.s_addr
, inp
->inp_faddr
.s_addr
,
750 (htons((u_short
)len
+ sizeof (struct udphdr
))));
752 if (len
+ sizeof(struct udpiphdr
) > IP_MAXPACKET
) {
758 laddr
= inp
->inp_laddr
;
759 if (inp
->inp_faddr
.s_addr
!= INADDR_ANY
) {
764 * Must block input while temporarily connected.
767 error
= in_pcbconnect(inp
, addr
, p
);
773 if (inp
->inp_faddr
.s_addr
== INADDR_ANY
) {
779 * Calculate data length and get a mbuf
780 * for UDP and IP headers.
782 M_PREPEND(m
, sizeof(struct udpiphdr
), M_DONTWAIT
);
791 * Fill in mbuf with extended UDP header
792 * and addresses and length put into network format.
794 ui
= mtod(m
, struct udpiphdr
*);
795 bzero(ui
->ui_x1
, sizeof(ui
->ui_x1
)); /* XXX still needed? */
796 ui
->ui_pr
= IPPROTO_UDP
;
797 ui
->ui_src
= inp
->inp_laddr
;
798 ui
->ui_dst
= inp
->inp_faddr
;
799 ui
->ui_sport
= inp
->inp_lport
;
800 ui
->ui_dport
= inp
->inp_fport
;
801 ui
->ui_ulen
= htons((u_short
)len
+ sizeof(struct udphdr
));
804 * Set up checksum and output datagram.
807 ui
->ui_sum
= in_pseudo(ui
->ui_src
.s_addr
, ui
->ui_dst
.s_addr
,
808 htons((u_short
)len
+ sizeof(struct udphdr
) + IPPROTO_UDP
));
809 m
->m_pkthdr
.csum_flags
= CSUM_UDP
;
810 m
->m_pkthdr
.csum_data
= offsetof(struct udphdr
, uh_sum
);
814 ((struct ip
*)ui
)->ip_len
= sizeof (struct udpiphdr
) + len
;
815 ((struct ip
*)ui
)->ip_ttl
= inp
->inp_ip_ttl
; /* XXX */
816 ((struct ip
*)ui
)->ip_tos
= inp
->inp_ip_tos
; /* XXX */
817 udpstat
.udps_opackets
++;
819 KERNEL_DEBUG(DBG_LAYER_OUT_END
, ui
->ui_dport
, ui
->ui_sport
,
820 ui
->ui_src
.s_addr
, ui
->ui_dst
.s_addr
, ui
->ui_ulen
);
823 if (ipsec_bypass
== 0 && ipsec_setsocket(m
, inp
->inp_socket
) != 0) {
828 error
= ip_output(m
, inp
->inp_options
, &inp
->inp_route
,
829 (inp
->inp_socket
->so_options
& (SO_DONTROUTE
| SO_BROADCAST
)),
833 in_pcbdisconnect(inp
);
834 inp
->inp_laddr
= laddr
; /* XXX rehash? */
837 KERNEL_DEBUG(DBG_FNC_UDP_OUTPUT
| DBG_FUNC_END
, error
, 0,0,0,0);
842 KERNEL_DEBUG(DBG_FNC_UDP_OUTPUT
| DBG_FUNC_END
, error
, 0,0,0,0);
846 u_long udp_sendspace
= 9216; /* really max datagram size */
847 /* 40 1K datagrams */
848 SYSCTL_INT(_net_inet_udp
, UDPCTL_MAXDGRAM
, maxdgram
, CTLFLAG_RW
,
849 &udp_sendspace
, 0, "Maximum outgoing UDP datagram size");
851 u_long udp_recvspace
= 40 * (1024 +
853 sizeof(struct sockaddr_in6
)
855 sizeof(struct sockaddr_in
)
858 SYSCTL_INT(_net_inet_udp
, UDPCTL_RECVSPACE
, recvspace
, CTLFLAG_RW
,
859 &udp_recvspace
, 0, "Maximum incoming UDP datagram size");
862 udp_abort(struct socket
*so
)
869 return EINVAL
; /* ??? possible? panic instead? */
870 soisdisconnected(so
);
878 udp_attach(struct socket
*so
, int proto
, struct proc
*p
)
887 error
= soreserve(so
, udp_sendspace
, udp_recvspace
);
891 error
= in_pcballoc(so
, &udbinfo
, p
);
895 inp
= (struct inpcb
*)so
->so_pcb
;
896 inp
->inp_vflag
|= INP_IPV4
;
897 inp
->inp_ip_ttl
= ip_defttl
;
902 udp_bind(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
911 error
= in_pcbbind(inp
, nam
, p
);
917 udp_connect(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
925 if (inp
->inp_faddr
.s_addr
!= INADDR_ANY
)
928 error
= in_pcbconnect(inp
, nam
, p
);
936 udp_detach(struct socket
*so
)
951 udp_disconnect(struct socket
*so
)
959 if (inp
->inp_faddr
.s_addr
== INADDR_ANY
)
963 in_pcbdisconnect(inp
);
964 inp
->inp_laddr
.s_addr
= INADDR_ANY
;
966 so
->so_state
&= ~SS_ISCONNECTED
; /* XXX */
971 udp_send(struct socket
*so
, int flags
, struct mbuf
*m
, struct sockaddr
*addr
,
972 struct mbuf
*control
, struct proc
*p
)
981 return udp_output(inp
, m
, addr
, control
, p
);
985 udp_shutdown(struct socket
*so
)
996 struct pr_usrreqs udp_usrreqs
= {
997 udp_abort
, pru_accept_notsupp
, udp_attach
, udp_bind
, udp_connect
,
998 pru_connect2_notsupp
, in_control
, udp_detach
, udp_disconnect
,
999 pru_listen_notsupp
, in_setpeeraddr
, pru_rcvd_notsupp
,
1000 pru_rcvoob_notsupp
, udp_send
, pru_sense_null
, udp_shutdown
,
1001 in_setsockaddr
, sosend
, soreceive
, sopoll