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
57 #include <sys/param.h>
58 #include <sys/systm.h>
59 #include <sys/kernel.h>
60 #include <sys/malloc.h>
63 #include <sys/domain.h>
65 #include <sys/protosw.h>
66 #include <sys/socket.h>
67 #include <sys/socketvar.h>
68 #include <sys/sysctl.h>
69 #include <sys/syslog.h>
72 #include <vm/vm_zone.h>
77 #include <net/route.h>
79 #include <netinet/in.h>
80 #include <netinet/in_systm.h>
81 #include <netinet/ip.h>
82 #include <netinet/in_pcb.h>
83 #include <netinet/in_var.h>
84 #include <netinet/ip_var.h>
85 #include <netinet/ip_icmp.h>
86 #include <netinet/icmp_var.h>
88 #include <netinet/ip6.h>
89 #include <netinet6/ip6_var.h>
91 #include <netinet/udp.h>
92 #include <netinet/udp_var.h>
93 #include <sys/kdebug.h>
96 #include <netinet6/ipsec.h>
100 #define DBG_LAYER_IN_BEG NETDBG_CODE(DBG_NETUDP, 0)
101 #define DBG_LAYER_IN_END NETDBG_CODE(DBG_NETUDP, 2)
102 #define DBG_LAYER_OUT_BEG NETDBG_CODE(DBG_NETUDP, 1)
103 #define DBG_LAYER_OUT_END NETDBG_CODE(DBG_NETUDP, 3)
104 #define DBG_FNC_UDP_INPUT NETDBG_CODE(DBG_NETUDP, (5 << 8))
105 #define DBG_FNC_UDP_OUTPUT NETDBG_CODE(DBG_NETUDP, (6 << 8) | 1)
109 * UDP protocol implementation.
110 * Per RFC 768, August, 1980.
113 static int udpcksum
= 1;
115 static int udpcksum
= 0; /* XXX */
117 SYSCTL_INT(_net_inet_udp
, UDPCTL_CHECKSUM
, checksum
, CTLFLAG_RW
,
121 SYSCTL_INT(_net_inet_udp
, OID_AUTO
, log_in_vain
, CTLFLAG_RW
,
122 &log_in_vain
, 0, "");
124 struct inpcbhead udb
; /* from udp_var.h */
125 #define udb6 udb /* for KAME src sync over BSD*'s */
126 struct inpcbinfo udbinfo
;
129 #define UDBHASHSIZE 16
132 struct udpstat udpstat
; /* from udp_var.h */
133 SYSCTL_STRUCT(_net_inet_udp
, UDPCTL_STATS
, stats
, CTLFLAG_RD
,
134 &udpstat
, udpstat
, "");
136 static struct sockaddr_in udp_in
= { sizeof(udp_in
), AF_INET
};
139 struct sockaddr_in6 uin6_sin
;
140 u_char uin6_init_done
: 1;
142 { sizeof(udp_in6
.uin6_sin
), AF_INET6
},
146 struct ip6_hdr uip6_ip6
;
147 u_char uip6_init_done
: 1;
151 static void udp_append
__P((struct inpcb
*last
, struct ip
*ip
,
152 struct mbuf
*n
, int off
));
154 static void ip_2_ip6_hdr
__P((struct ip6_hdr
*ip6
, struct ip
*ip
));
157 static int udp_detach
__P((struct socket
*so
));
158 static int udp_output
__P((struct inpcb
*, struct mbuf
*, struct sockaddr
*,
159 struct mbuf
*, struct proc
*));
167 struct in_addr laddr
;
168 struct in_addr faddr
;
172 udbinfo
.listhead
= &udb
;
173 udbinfo
.hashbase
= hashinit(UDBHASHSIZE
, M_PCB
, &udbinfo
.hashmask
);
174 udbinfo
.porthashbase
= hashinit(UDBHASHSIZE
, M_PCB
,
175 &udbinfo
.porthashmask
);
177 udbinfo
.ipi_zone
= zinit("udpcb", sizeof(struct inpcb
), maxsockets
,
180 str_size
= (vm_size_t
) sizeof(struct inpcb
);
181 udbinfo
.ipi_zone
= (void *) zinit(str_size
, 80000*str_size
, 8192, "inpcb_zone");
184 udbinfo
.last_pcb
= 0;
185 in_pcb_nat_init(&udbinfo
, AF_INET
, IPPROTO_UDP
, SOCK_DGRAM
);
188 stat
= in_pcb_new_share_client(&udbinfo
, &fake_owner
);
189 kprintf("udp_init in_pcb_new_share_client - stat = %d\n", stat
);
191 laddr
.s_addr
= 0x11646464;
192 faddr
.s_addr
= 0x11646465;
195 in_pcb_grab_port(&udbinfo
, 0, laddr
, &lport
, faddr
, 1600, 0, fake_owner
);
196 kprintf("udp_init in_pcb_grab_port - stat = %d\n", stat
);
198 stat
= in_pcb_rem_share_client(&udbinfo
, fake_owner
);
199 kprintf("udp_init in_pcb_rem_share_client - stat = %d\n", stat
);
201 stat
= in_pcb_new_share_client(&udbinfo
, &fake_owner
);
202 kprintf("udp_init in_pcb_new_share_client(2) - stat = %d\n", stat
);
204 laddr
.s_addr
= 0x11646464;
205 faddr
.s_addr
= 0x11646465;
208 stat
= in_pcb_grab_port(&udbinfo
, 0, laddr
, &lport
, faddr
, 1600, 0, fake_owner
);
209 kprintf("udp_init in_pcb_grab_port(2) - stat = %d\n", stat
);
215 register struct mbuf
*m
;
218 register struct ip
*ip
;
219 register struct udphdr
*uh
;
220 register struct inpcb
*inp
;
221 struct mbuf
*opts
= 0;
223 struct ip6_recvpktopts opts6
;
227 struct sockaddr
*append_sa
;
229 udpstat
.udps_ipackets
++;
231 bzero(&opts6
, sizeof(opts6
));
235 KERNEL_DEBUG(DBG_FNC_UDP_INPUT
| DBG_FUNC_START
, 0,0,0,0,0);
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 KERNEL_DEBUG(DBG_LAYER_IN_BEG
, uh
->uh_dport
, uh
->uh_sport
,
263 ip
->ip_src
.s_addr
, ip
->ip_dst
.s_addr
, uh
->uh_ulen
);
266 * Make mbuf data length reflect UDP length.
267 * If not enough data to reflect UDP length, drop.
269 len
= ntohs((u_short
)uh
->uh_ulen
);
270 if (ip
->ip_len
!= len
) {
271 if (len
> ip
->ip_len
|| len
< sizeof(struct udphdr
)) {
272 udpstat
.udps_badlen
++;
275 m_adj(m
, len
- ip
->ip_len
);
276 /* ip->ip_len = len; */
279 * Save a copy of the IP header in case we want restore it
280 * for sending an ICMP error message in response.
285 * Checksum extended UDP header and data.
288 bzero(((struct ipovly
*)ip
)->ih_x1
, 9);
289 ((struct ipovly
*)ip
)->ih_len
= uh
->uh_ulen
;
290 uh
->uh_sum
= in_cksum(m
, len
+ sizeof (struct ip
));
292 udpstat
.udps_badsum
++;
294 KERNEL_DEBUG(DBG_FNC_UDP_INPUT
| DBG_FUNC_END
, 0,0,0,0,0);
299 if (IN_MULTICAST(ntohl(ip
->ip_dst
.s_addr
)) ||
300 in_broadcast(ip
->ip_dst
, m
->m_pkthdr
.rcvif
)) {
303 * Deliver a multicast or broadcast datagram to *all* sockets
304 * for which the local and remote addresses and ports match
305 * those of the incoming datagram. This allows more than
306 * one process to receive multi/broadcasts on the same port.
307 * (This really ought to be done for unicast datagrams as
308 * well, but that would cause problems with existing
309 * applications that open both address-specific sockets and
310 * a wildcard socket listening to the same port -- they would
311 * end up receiving duplicates of every unicast datagram.
312 * Those applications open the multiple sockets to overcome an
313 * inadequacy of the UDP socket interface, but for backwards
314 * compatibility we avoid the problem here rather than
315 * fixing the interface. Maybe 4.5BSD will remedy this?)
319 * Construct sockaddr format source address.
321 udp_in
.sin_port
= uh
->uh_sport
;
322 udp_in
.sin_addr
= ip
->ip_src
;
324 * Locate pcb(s) for datagram.
325 * (Algorithm copied from raw_intr().)
329 udp_in6
.uin6_init_done
= udp_ip6
.uip6_init_done
= 0;
331 LIST_FOREACH(inp
, &udb
, inp_list
) {
333 if ((inp
->inp_vflag
& INP_IPV4
) == 0)
336 if (inp
->inp_lport
!= uh
->uh_dport
)
338 if (inp
->inp_laddr
.s_addr
!= INADDR_ANY
) {
339 if (inp
->inp_laddr
.s_addr
!=
343 if (inp
->inp_faddr
.s_addr
!= INADDR_ANY
) {
344 if (inp
->inp_faddr
.s_addr
!=
346 inp
->inp_fport
!= uh
->uh_sport
)
354 /* check AH/ESP integrity. */
355 if (ipsec4_in_reject_so(m
, last
->inp_socket
)) {
356 ipsecstat
.in_polvio
++;
357 /* do not inject data to pcb */
360 if ((n
= m_copy(m
, 0, M_COPYALL
)) != NULL
) {
361 udp_append(last
, ip
, n
, iphlen
+
362 sizeof (struct udphdr
));
367 * Don't look for additional matches if this one does
368 * not have either the SO_REUSEPORT or SO_REUSEADDR
369 * socket options set. This heuristic avoids searching
370 * through all pcbs in the common case of a non-shared
371 * port. It * assumes that an application will never
372 * clear these options after setting them.
374 if ((last
->inp_socket
->so_options
&(SO_REUSEPORT
|SO_REUSEADDR
)) == 0)
380 * No matching pcb found; discard datagram.
381 * (No need to send an ICMP Port Unreachable
382 * for a broadcast or multicast datgram.)
384 udpstat
.udps_noportbcast
++;
389 /* check AH/ESP integrity. */
390 if (m
&& ipsec4_in_reject_so(m
, last
->inp_socket
)) {
391 ipsecstat
.in_polvio
++;
395 udp_append(last
, ip
, m
, iphlen
+ sizeof (struct udphdr
));
399 * Locate pcb for datagram.
401 inp
= in_pcblookup_hash(&udbinfo
, ip
->ip_src
, uh
->uh_sport
,
402 ip
->ip_dst
, uh
->uh_dport
, 1, m
->m_pkthdr
.rcvif
);
405 char buf
[4*sizeof "123"];
407 strcpy(buf
, inet_ntoa(ip
->ip_dst
));
409 "Connection attempt to UDP %s:%d from %s:%d\n",
410 buf
, ntohs(uh
->uh_dport
), inet_ntoa(ip
->ip_src
),
411 ntohs(uh
->uh_sport
));
413 udpstat
.udps_noport
++;
414 if (m
->m_flags
& (M_BCAST
| M_MCAST
)) {
415 udpstat
.udps_noportbcast
++;
420 if (badport_bandlim(0) < 0)
423 icmp_error(m
, ICMP_UNREACH
, ICMP_UNREACH_PORT
, 0, 0);
424 KERNEL_DEBUG(DBG_FNC_UDP_INPUT
| DBG_FUNC_END
, 0,0,0,0,0);
428 if (inp
!= NULL
&& ipsec4_in_reject_so(m
, inp
->inp_socket
)) {
429 ipsecstat
.in_polvio
++;
435 * Construct sockaddr format source address.
436 * Stuff source address and datagram in user buffer.
438 udp_in
.sin_port
= uh
->uh_sport
;
439 udp_in
.sin_addr
= ip
->ip_src
;
440 if (inp
->inp_flags
& INP_CONTROLOPTS
441 || inp
->inp_socket
->so_options
& SO_TIMESTAMP
) {
443 if (inp
->inp_vflag
& INP_IPV6
) {
446 ip_2_ip6_hdr(&udp_ip6
.uip6_ip6
, ip
);
447 savedflags
= inp
->inp_flags
;
448 inp
->inp_flags
&= ~INP_UNMAPPABLEOPTS
;
449 ip6_savecontrol(inp
, &udp_ip6
.uip6_ip6
, m
,
452 inp
->inp_flags
= savedflags
;
455 ip_savecontrol(inp
, &opts
, ip
, m
);
457 m_adj(m
, iphlen
+ sizeof(struct udphdr
));
459 KERNEL_DEBUG(DBG_LAYER_IN_END
, uh
->uh_dport
, uh
->uh_sport
,
460 save_ip
.ip_src
.s_addr
, save_ip
.ip_dst
.s_addr
, uh
->uh_ulen
);
463 if (inp
->inp_vflag
& INP_IPV6
) {
464 in6_sin_2_v4mapsin6(&udp_in
, &udp_in6
.uin6_sin
);
465 append_sa
= (struct sockaddr
*)&udp_in6
;
469 append_sa
= (struct sockaddr
*)&udp_in
;
470 if (sbappendaddr(&inp
->inp_socket
->so_rcv
, append_sa
, m
, opts
) == 0) {
471 udpstat
.udps_fullsock
++;
474 sorwakeup(inp
->inp_socket
);
475 KERNEL_DEBUG(DBG_FNC_UDP_INPUT
| DBG_FUNC_END
, 0,0,0,0,0);
481 KERNEL_DEBUG(DBG_FNC_UDP_INPUT
| DBG_FUNC_END
, 0,0,0,0,0);
486 ip_2_ip6_hdr(ip6
, ip
)
490 bzero(ip6
, sizeof(*ip6
));
492 ip6
->ip6_vfc
= IPV6_VERSION
;
493 ip6
->ip6_plen
= ip
->ip_len
;
494 ip6
->ip6_nxt
= ip
->ip_p
;
495 ip6
->ip6_hlim
= ip
->ip_ttl
;
496 ip6
->ip6_src
.s6_addr32
[2] = ip6
->ip6_dst
.s6_addr32
[2] =
498 ip6
->ip6_src
.s6_addr32
[3] = ip
->ip_src
.s_addr
;
499 ip6
->ip6_dst
.s6_addr32
[3] = ip
->ip_dst
.s_addr
;
504 * subroutine of udp_input(), mainly for source code readability.
505 * caller must properly init udp_ip6 and udp_in6 beforehand.
508 udp_append(last
, ip
, n
, off
)
513 struct sockaddr
*append_sa
;
514 struct mbuf
*opts
= 0;
516 struct ip6_recvpktopts opts6
;
517 bzero(&opts6
, sizeof(opts6
));
521 if (last
->inp_flags
& INP_CONTROLOPTS
||
522 last
->inp_socket
->so_options
& SO_TIMESTAMP
) {
524 if (last
->inp_vflag
& INP_IPV6
) {
527 if (udp_ip6
.uip6_init_done
== 0) {
528 ip_2_ip6_hdr(&udp_ip6
.uip6_ip6
, ip
);
529 udp_ip6
.uip6_init_done
= 1;
531 savedflags
= last
->inp_flags
;
532 last
->inp_flags
&= ~INP_UNMAPPABLEOPTS
;
533 ip6_savecontrol(last
, &udp_ip6
.uip6_ip6
, n
,
535 last
->inp_flags
= savedflags
;
538 ip_savecontrol(last
, &opts
, ip
, n
);
541 if (last
->inp_vflag
& INP_IPV6
) {
542 if (udp_in6
.uin6_init_done
== 0) {
543 in6_sin_2_v4mapsin6(&udp_in
, &udp_in6
.uin6_sin
);
544 udp_in6
.uin6_init_done
= 1;
546 append_sa
= (struct sockaddr
*)&udp_in6
.uin6_sin
;
550 append_sa
= (struct sockaddr
*)&udp_in
;
553 if (sbappendaddr(&last
->inp_socket
->so_rcv
, append_sa
, n
, opts
) == 0) {
557 udpstat
.udps_fullsock
++;
559 sorwakeup(last
->inp_socket
);
565 * Notify a udp user of an asynchronous error;
566 * just wake up so that he can collect error status.
569 udp_notify(inp
, errno
)
570 register struct inpcb
*inp
;
573 inp
->inp_socket
->so_error
= errno
;
574 sorwakeup(inp
->inp_socket
);
575 sowwakeup(inp
->inp_socket
);
579 udp_ctlinput(cmd
, sa
, vip
)
584 register struct ip
*ip
= vip
;
585 register struct udphdr
*uh
;
587 if (!PRC_IS_REDIRECT(cmd
) &&
588 ((unsigned)cmd
>= PRC_NCMDS
|| inetctlerrmap
[cmd
] == 0))
591 uh
= (struct udphdr
*)((caddr_t
)ip
+ (ip
->ip_hl
<< 2));
592 in_pcbnotify(&udb
, sa
, uh
->uh_dport
, ip
->ip_src
, uh
->uh_sport
,
595 in_pcbnotify(&udb
, sa
, 0, zeroin_addr
, 0, cmd
, udp_notify
);
600 udp_pcblist SYSCTL_HANDLER_ARGS
603 struct inpcb
*inp
, **inp_list
;
608 * The process of preparing the TCB list is too time-consuming and
609 * resource-intensive to repeat twice on every request.
611 if (req
->oldptr
== 0) {
612 n
= udbinfo
.ipi_count
;
613 req
->oldidx
= 2 * (sizeof xig
)
614 + (n
+ n
/8) * sizeof(struct xinpcb
);
618 if (req
->newptr
!= 0)
622 * OK, now we're committed to doing something.
625 gencnt
= udbinfo
.ipi_gencnt
;
626 n
= udbinfo
.ipi_count
;
629 xig
.xig_len
= sizeof xig
;
631 xig
.xig_gen
= gencnt
;
632 xig
.xig_sogen
= so_gencnt
;
633 error
= SYSCTL_OUT(req
, &xig
, sizeof xig
);
637 inp_list
= _MALLOC(n
* sizeof *inp_list
, M_TEMP
, M_WAITOK
);
642 for (inp
= udbinfo
.listhead
->lh_first
, i
= 0; inp
&& i
< n
;
643 inp
= inp
->inp_list
.le_next
) {
644 if (inp
->inp_gencnt
<= gencnt
)
651 for (i
= 0; i
< n
; i
++) {
653 if (inp
->inp_gencnt
<= gencnt
) {
655 xi
.xi_len
= sizeof xi
;
656 /* XXX should avoid extra copy */
657 bcopy(inp
, &xi
.xi_inp
, sizeof *inp
);
659 sotoxsocket(inp
->inp_socket
, &xi
.xi_socket
);
660 error
= SYSCTL_OUT(req
, &xi
, sizeof xi
);
665 * Give the user an updated idea of our state.
666 * If the generation differs from what we told
667 * her before, she knows that something happened
668 * while we were processing this request, and it
669 * might be necessary to retry.
672 xig
.xig_gen
= udbinfo
.ipi_gencnt
;
673 xig
.xig_sogen
= so_gencnt
;
674 xig
.xig_count
= udbinfo
.ipi_count
;
676 error
= SYSCTL_OUT(req
, &xig
, sizeof xig
);
678 FREE(inp_list
, M_TEMP
);
682 SYSCTL_PROC(_net_inet_udp
, UDPCTL_PCBLIST
, pcblist
, CTLFLAG_RD
, 0, 0,
683 udp_pcblist
, "S,xinpcb", "List of active UDP sockets");
688 udp_output(inp
, m
, addr
, control
, p
)
689 register struct inpcb
*inp
;
690 register struct mbuf
*m
;
691 struct sockaddr
*addr
;
692 struct mbuf
*control
;
695 register struct udpiphdr
*ui
;
696 register int len
= m
->m_pkthdr
.len
;
697 struct in_addr laddr
;
698 int s
= 0, error
= 0;
700 KERNEL_DEBUG(DBG_FNC_UDP_OUTPUT
| DBG_FUNC_START
, 0,0,0,0,0);
703 m_freem(control
); /* XXX */
705 KERNEL_DEBUG(DBG_LAYER_OUT_BEG
, inp
->inp_fport
, inp
->inp_lport
,
706 inp
->inp_laddr
.s_addr
, inp
->inp_faddr
.s_addr
,
707 (htons((u_short
)len
+ sizeof (struct udphdr
))));
709 if (len
+ sizeof(struct udpiphdr
) > IP_MAXPACKET
) {
715 laddr
= inp
->inp_laddr
;
716 if (inp
->inp_faddr
.s_addr
!= INADDR_ANY
) {
721 * Must block input while temporarily connected.
724 error
= in_pcbconnect(inp
, addr
, p
);
730 if (inp
->inp_faddr
.s_addr
== INADDR_ANY
) {
736 * Calculate data length and get a mbuf
737 * for UDP and IP headers.
739 M_PREPEND(m
, sizeof(struct udpiphdr
), M_DONTWAIT
);
748 * Fill in mbuf with extended UDP header
749 * and addresses and length put into network format.
751 ui
= mtod(m
, struct udpiphdr
*);
752 bzero(ui
->ui_x1
, sizeof(ui
->ui_x1
));
753 ui
->ui_pr
= IPPROTO_UDP
;
754 ui
->ui_len
= htons((u_short
)len
+ sizeof (struct udphdr
));
755 ui
->ui_src
= inp
->inp_laddr
;
756 ui
->ui_dst
= inp
->inp_faddr
;
757 ui
->ui_sport
= inp
->inp_lport
;
758 ui
->ui_dport
= inp
->inp_fport
;
759 ui
->ui_ulen
= ui
->ui_len
;
762 * Stuff checksum and output datagram.
766 if ((ui
->ui_sum
= in_cksum(m
, sizeof (struct udpiphdr
) + len
)) == 0)
769 ((struct ip
*)ui
)->ip_len
= sizeof (struct udpiphdr
) + len
;
770 ((struct ip
*)ui
)->ip_ttl
= inp
->inp_ip_ttl
; /* XXX */
771 ((struct ip
*)ui
)->ip_tos
= inp
->inp_ip_tos
; /* XXX */
772 udpstat
.udps_opackets
++;
774 KERNEL_DEBUG(DBG_LAYER_OUT_END
, ui
->ui_dport
, ui
->ui_sport
,
775 ui
->ui_src
.s_addr
, ui
->ui_dst
.s_addr
, ui
->ui_ulen
);
779 ipsec_setsocket(m
, inp
->inp_socket
);
782 error
= ip_output(m
, inp
->inp_options
, &inp
->inp_route
,
783 inp
->inp_socket
->so_options
& (SO_DONTROUTE
| SO_BROADCAST
),
787 in_pcbdisconnect(inp
);
788 inp
->inp_laddr
= laddr
; /* XXX rehash? */
791 KERNEL_DEBUG(DBG_FNC_UDP_OUTPUT
| DBG_FUNC_END
, error
, 0,0,0,0);
796 KERNEL_DEBUG(DBG_FNC_UDP_OUTPUT
| DBG_FUNC_END
, error
, 0,0,0,0);
800 u_long udp_sendspace
= 9216; /* really max datagram size */
801 /* 40 1K datagrams */
802 SYSCTL_INT(_net_inet_udp
, UDPCTL_MAXDGRAM
, maxdgram
, CTLFLAG_RW
,
803 &udp_sendspace
, 0, "");
806 u_long udp_recvspace
= 40 * (1024 + /* 40 1K datagrams */
808 sizeof(struct sockaddr_in6
)
810 sizeof(struct sockaddr_in
)
813 SYSCTL_INT(_net_inet_udp
, UDPCTL_RECVSPACE
, recvspace
, CTLFLAG_RW
,
814 &udp_recvspace
, 0, "");
817 udp_abort(struct socket
*so
)
824 return EINVAL
; /* ??? possible? panic instead? */
825 soisdisconnected(so
);
833 udp_attach(struct socket
*so
, int proto
, struct proc
*p
)
838 if (so
->so_snd
.sb_hiwat
== 0 || so
->so_rcv
.sb_hiwat
== 0) {
839 error
= soreserve(so
, udp_sendspace
, udp_recvspace
);
844 error
= in_pcballoc(so
, &udbinfo
, p
);
848 error
= soreserve(so
, udp_sendspace
, udp_recvspace
);
851 inp
= (struct inpcb
*)so
->so_pcb
;
852 inp
->inp_vflag
|= INP_IPV4
;
853 inp
->inp_ip_ttl
= ip_defttl
;
855 error
= ipsec_init_policy(so
, &inp
->inp_sp
);
865 udp_bind(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
874 error
= in_pcbbind(inp
, nam
, p
);
880 udp_connect(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
888 if (inp
->inp_faddr
.s_addr
!= INADDR_ANY
)
891 error
= in_pcbconnect(inp
, nam
, p
);
899 udp_detach(struct socket
*so
)
914 udp_disconnect(struct socket
*so
)
922 if (inp
->inp_faddr
.s_addr
== INADDR_ANY
)
926 in_pcbdisconnect(inp
);
927 inp
->inp_laddr
.s_addr
= INADDR_ANY
;
929 so
->so_state
&= ~SS_ISCONNECTED
; /* XXX */
934 udp_send(struct socket
*so
, int flags
, struct mbuf
*m
, struct sockaddr
*addr
,
935 struct mbuf
*control
, struct proc
*p
)
944 return udp_output(inp
, m
, addr
, control
, p
);
948 udp_shutdown(struct socket
*so
)
959 struct pr_usrreqs udp_usrreqs
= {
960 udp_abort
, pru_accept_notsupp
, udp_attach
, udp_bind
, udp_connect
,
961 pru_connect2_notsupp
, in_control
, udp_detach
, udp_disconnect
,
962 pru_listen_notsupp
, in_setpeeraddr
, pru_rcvd_notsupp
,
963 pru_rcvoob_notsupp
, udp_send
, pru_sense_null
, udp_shutdown
,
964 in_setsockaddr
, sosend
, soreceive
, sopoll