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)
108 #ifndef offsetof /* XXX */
109 #define offsetof(type, member) ((size_t)(&((type *)0)->member))
114 * UDP protocol implementation.
115 * Per RFC 768, August, 1980.
118 static int udpcksum
= 1;
120 static int udpcksum
= 0; /* XXX */
122 SYSCTL_INT(_net_inet_udp
, UDPCTL_CHECKSUM
, checksum
, CTLFLAG_RW
,
126 SYSCTL_INT(_net_inet_udp
, OID_AUTO
, log_in_vain
, CTLFLAG_RW
,
127 &log_in_vain
, 0, "");
129 struct inpcbhead udb
; /* from udp_var.h */
130 #define udb6 udb /* for KAME src sync over BSD*'s */
131 struct inpcbinfo udbinfo
;
134 #define UDBHASHSIZE 16
137 extern int apple_hwcksum_rx
;
139 struct udpstat udpstat
; /* from udp_var.h */
140 SYSCTL_STRUCT(_net_inet_udp
, UDPCTL_STATS
, stats
, CTLFLAG_RD
,
141 &udpstat
, udpstat
, "");
143 static struct sockaddr_in udp_in
= { sizeof(udp_in
), AF_INET
};
146 struct sockaddr_in6 uin6_sin
;
147 u_char uin6_init_done
: 1;
149 { sizeof(udp_in6
.uin6_sin
), AF_INET6
},
153 struct ip6_hdr uip6_ip6
;
154 u_char uip6_init_done
: 1;
158 static void udp_append
__P((struct inpcb
*last
, struct ip
*ip
,
159 struct mbuf
*n
, int off
));
161 static void ip_2_ip6_hdr
__P((struct ip6_hdr
*ip6
, struct ip
*ip
));
164 static int udp_detach
__P((struct socket
*so
));
165 static int udp_output
__P((struct inpcb
*, struct mbuf
*, struct sockaddr
*,
166 struct mbuf
*, struct proc
*));
174 struct in_addr laddr
;
175 struct in_addr faddr
;
179 udbinfo
.listhead
= &udb
;
180 udbinfo
.hashbase
= hashinit(UDBHASHSIZE
, M_PCB
, &udbinfo
.hashmask
);
181 udbinfo
.porthashbase
= hashinit(UDBHASHSIZE
, M_PCB
,
182 &udbinfo
.porthashmask
);
184 udbinfo
.ipi_zone
= zinit("udpcb", sizeof(struct inpcb
), maxsockets
,
187 str_size
= (vm_size_t
) sizeof(struct inpcb
);
188 udbinfo
.ipi_zone
= (void *) zinit(str_size
, 80000*str_size
, 8192, "inpcb_zone");
191 udbinfo
.last_pcb
= 0;
192 in_pcb_nat_init(&udbinfo
, AF_INET
, IPPROTO_UDP
, SOCK_DGRAM
);
195 stat
= in_pcb_new_share_client(&udbinfo
, &fake_owner
);
196 kprintf("udp_init in_pcb_new_share_client - stat = %d\n", stat
);
198 laddr
.s_addr
= 0x11646464;
199 faddr
.s_addr
= 0x11646465;
202 in_pcb_grab_port(&udbinfo
, 0, laddr
, &lport
, faddr
, 1600, 0, fake_owner
);
203 kprintf("udp_init in_pcb_grab_port - stat = %d\n", stat
);
205 stat
= in_pcb_rem_share_client(&udbinfo
, fake_owner
);
206 kprintf("udp_init in_pcb_rem_share_client - stat = %d\n", stat
);
208 stat
= in_pcb_new_share_client(&udbinfo
, &fake_owner
);
209 kprintf("udp_init in_pcb_new_share_client(2) - stat = %d\n", stat
);
211 laddr
.s_addr
= 0x11646464;
212 faddr
.s_addr
= 0x11646465;
215 stat
= in_pcb_grab_port(&udbinfo
, 0, laddr
, &lport
, faddr
, 1600, 0, fake_owner
);
216 kprintf("udp_init in_pcb_grab_port(2) - stat = %d\n", stat
);
222 register struct mbuf
*m
;
225 register struct ip
*ip
;
226 register struct udphdr
*uh
;
227 register struct inpcb
*inp
;
228 struct mbuf
*opts
= 0;
230 struct ip6_recvpktopts opts6
;
234 struct sockaddr
*append_sa
;
236 udpstat
.udps_ipackets
++;
238 bzero(&opts6
, sizeof(opts6
));
242 KERNEL_DEBUG(DBG_FNC_UDP_INPUT
| DBG_FUNC_START
, 0,0,0,0,0);
245 * Strip IP options, if any; should skip this,
246 * make available to user, and use on returned packets,
247 * but we don't yet have a way to check the checksum
248 * with options still present.
250 if (iphlen
> sizeof (struct ip
)) {
251 ip_stripoptions(m
, (struct mbuf
*)0);
252 iphlen
= sizeof(struct ip
);
253 if (m
->m_pkthdr
.csum_flags
& CSUM_TCP_SUM16
)
254 m
->m_pkthdr
.csum_flags
= 0; /* invalidate hwcksum */
258 * Get IP and UDP header together in first mbuf.
260 ip
= mtod(m
, struct ip
*);
261 if (m
->m_len
< iphlen
+ sizeof(struct udphdr
)) {
262 if ((m
= m_pullup(m
, iphlen
+ sizeof(struct udphdr
))) == 0) {
263 udpstat
.udps_hdrops
++;
264 KERNEL_DEBUG(DBG_FNC_UDP_INPUT
| DBG_FUNC_END
, 0,0,0,0,0);
267 ip
= mtod(m
, struct ip
*);
269 uh
= (struct udphdr
*)((caddr_t
)ip
+ iphlen
);
271 KERNEL_DEBUG(DBG_LAYER_IN_BEG
, uh
->uh_dport
, uh
->uh_sport
,
272 ip
->ip_src
.s_addr
, ip
->ip_dst
.s_addr
, uh
->uh_ulen
);
275 * Make mbuf data length reflect UDP length.
276 * If not enough data to reflect UDP length, drop.
278 len
= ntohs((u_short
)uh
->uh_ulen
);
279 if (ip
->ip_len
!= len
) {
280 if (len
> ip
->ip_len
|| len
< sizeof(struct udphdr
)) {
281 udpstat
.udps_badlen
++;
284 m_adj(m
, len
- ip
->ip_len
);
285 /* ip->ip_len = len; */
288 * Save a copy of the IP header in case we want restore it
289 * for sending an ICMP error message in response.
294 * Checksum extended UDP header and data.
297 if (m
->m_pkthdr
.csum_flags
& CSUM_DATA_VALID
) {
298 if (m
->m_pkthdr
.csum_flags
& CSUM_PSEUDO_HDR
)
299 uh
->uh_sum
= m
->m_pkthdr
.csum_data
;
301 if (apple_hwcksum_rx
&& (m
->m_pkthdr
.csum_flags
& CSUM_TCP_SUM16
)) {
302 bzero(((struct ipovly
*)ip
)->ih_x1
, 9);
303 ((struct ipovly
*)ip
)->ih_len
= uh
->uh_ulen
;
304 uh
->uh_sum
= in_addword(in_cksum(m
, sizeof(struct ip
)),
305 m
->m_pkthdr
.csum_data
& 0xFFFF);
311 uh
->uh_sum
^= 0xffff;
314 bzero(((struct ipovly
*)ip
)->ih_x1
, 9);
315 ((struct ipovly
*)ip
)->ih_len
= uh
->uh_ulen
;
316 uh
->uh_sum
= in_cksum(m
, len
+ sizeof (struct ip
));
319 udpstat
.udps_badsum
++;
321 KERNEL_DEBUG(DBG_FNC_UDP_INPUT
| DBG_FUNC_END
, 0,0,0,0,0);
326 if (IN_MULTICAST(ntohl(ip
->ip_dst
.s_addr
)) ||
327 in_broadcast(ip
->ip_dst
, m
->m_pkthdr
.rcvif
)) {
330 * Deliver a multicast or broadcast datagram to *all* sockets
331 * for which the local and remote addresses and ports match
332 * those of the incoming datagram. This allows more than
333 * one process to receive multi/broadcasts on the same port.
334 * (This really ought to be done for unicast datagrams as
335 * well, but that would cause problems with existing
336 * applications that open both address-specific sockets and
337 * a wildcard socket listening to the same port -- they would
338 * end up receiving duplicates of every unicast datagram.
339 * Those applications open the multiple sockets to overcome an
340 * inadequacy of the UDP socket interface, but for backwards
341 * compatibility we avoid the problem here rather than
342 * fixing the interface. Maybe 4.5BSD will remedy this?)
346 * Construct sockaddr format source address.
348 udp_in
.sin_port
= uh
->uh_sport
;
349 udp_in
.sin_addr
= ip
->ip_src
;
351 * Locate pcb(s) for datagram.
352 * (Algorithm copied from raw_intr().)
356 udp_in6
.uin6_init_done
= udp_ip6
.uip6_init_done
= 0;
358 LIST_FOREACH(inp
, &udb
, inp_list
) {
360 if ((inp
->inp_vflag
& INP_IPV4
) == 0)
363 if (inp
->inp_lport
!= uh
->uh_dport
)
365 if (inp
->inp_laddr
.s_addr
!= INADDR_ANY
) {
366 if (inp
->inp_laddr
.s_addr
!=
370 if (inp
->inp_faddr
.s_addr
!= INADDR_ANY
) {
371 if (inp
->inp_faddr
.s_addr
!=
373 inp
->inp_fport
!= uh
->uh_sport
)
381 /* check AH/ESP integrity. */
382 if (ipsec4_in_reject_so(m
, last
->inp_socket
)) {
383 ipsecstat
.in_polvio
++;
384 /* do not inject data to pcb */
387 if ((n
= m_copy(m
, 0, M_COPYALL
)) != NULL
) {
388 udp_append(last
, ip
, n
, iphlen
+
389 sizeof (struct udphdr
));
394 * Don't look for additional matches if this one does
395 * not have either the SO_REUSEPORT or SO_REUSEADDR
396 * socket options set. This heuristic avoids searching
397 * through all pcbs in the common case of a non-shared
398 * port. It * assumes that an application will never
399 * clear these options after setting them.
401 if ((last
->inp_socket
->so_options
&(SO_REUSEPORT
|SO_REUSEADDR
)) == 0)
407 * No matching pcb found; discard datagram.
408 * (No need to send an ICMP Port Unreachable
409 * for a broadcast or multicast datgram.)
411 udpstat
.udps_noportbcast
++;
416 /* check AH/ESP integrity. */
417 if (m
&& ipsec4_in_reject_so(m
, last
->inp_socket
)) {
418 ipsecstat
.in_polvio
++;
422 udp_append(last
, ip
, m
, iphlen
+ sizeof (struct udphdr
));
426 * Locate pcb for datagram.
428 inp
= in_pcblookup_hash(&udbinfo
, ip
->ip_src
, uh
->uh_sport
,
429 ip
->ip_dst
, uh
->uh_dport
, 1, m
->m_pkthdr
.rcvif
);
432 char buf
[4*sizeof "123"];
434 strcpy(buf
, inet_ntoa(ip
->ip_dst
));
436 "Connection attempt to UDP %s:%d from %s:%d\n",
437 buf
, ntohs(uh
->uh_dport
), inet_ntoa(ip
->ip_src
),
438 ntohs(uh
->uh_sport
));
440 udpstat
.udps_noport
++;
441 if (m
->m_flags
& (M_BCAST
| M_MCAST
)) {
442 udpstat
.udps_noportbcast
++;
447 if (badport_bandlim(0) < 0)
450 icmp_error(m
, ICMP_UNREACH
, ICMP_UNREACH_PORT
, 0, 0);
451 KERNEL_DEBUG(DBG_FNC_UDP_INPUT
| DBG_FUNC_END
, 0,0,0,0,0);
455 if (inp
!= NULL
&& ipsec4_in_reject_so(m
, inp
->inp_socket
)) {
456 ipsecstat
.in_polvio
++;
462 * Construct sockaddr format source address.
463 * Stuff source address and datagram in user buffer.
465 udp_in
.sin_port
= uh
->uh_sport
;
466 udp_in
.sin_addr
= ip
->ip_src
;
467 if (inp
->inp_flags
& INP_CONTROLOPTS
468 || inp
->inp_socket
->so_options
& SO_TIMESTAMP
) {
470 if (inp
->inp_vflag
& INP_IPV6
) {
473 ip_2_ip6_hdr(&udp_ip6
.uip6_ip6
, ip
);
474 savedflags
= inp
->inp_flags
;
475 inp
->inp_flags
&= ~INP_UNMAPPABLEOPTS
;
476 ip6_savecontrol(inp
, &udp_ip6
.uip6_ip6
, m
,
479 inp
->inp_flags
= savedflags
;
482 ip_savecontrol(inp
, &opts
, ip
, m
);
484 m_adj(m
, iphlen
+ sizeof(struct udphdr
));
486 KERNEL_DEBUG(DBG_LAYER_IN_END
, uh
->uh_dport
, uh
->uh_sport
,
487 save_ip
.ip_src
.s_addr
, save_ip
.ip_dst
.s_addr
, uh
->uh_ulen
);
490 if (inp
->inp_vflag
& INP_IPV6
) {
491 in6_sin_2_v4mapsin6(&udp_in
, &udp_in6
.uin6_sin
);
492 append_sa
= (struct sockaddr
*)&udp_in6
;
496 append_sa
= (struct sockaddr
*)&udp_in
;
497 if (sbappendaddr(&inp
->inp_socket
->so_rcv
, append_sa
, m
, opts
) == 0) {
498 udpstat
.udps_fullsock
++;
501 sorwakeup(inp
->inp_socket
);
502 KERNEL_DEBUG(DBG_FNC_UDP_INPUT
| DBG_FUNC_END
, 0,0,0,0,0);
508 KERNEL_DEBUG(DBG_FNC_UDP_INPUT
| DBG_FUNC_END
, 0,0,0,0,0);
513 ip_2_ip6_hdr(ip6
, ip
)
517 bzero(ip6
, sizeof(*ip6
));
519 ip6
->ip6_vfc
= IPV6_VERSION
;
520 ip6
->ip6_plen
= ip
->ip_len
;
521 ip6
->ip6_nxt
= ip
->ip_p
;
522 ip6
->ip6_hlim
= ip
->ip_ttl
;
523 ip6
->ip6_src
.s6_addr32
[2] = ip6
->ip6_dst
.s6_addr32
[2] =
525 ip6
->ip6_src
.s6_addr32
[3] = ip
->ip_src
.s_addr
;
526 ip6
->ip6_dst
.s6_addr32
[3] = ip
->ip_dst
.s_addr
;
531 * subroutine of udp_input(), mainly for source code readability.
532 * caller must properly init udp_ip6 and udp_in6 beforehand.
535 udp_append(last
, ip
, n
, off
)
540 struct sockaddr
*append_sa
;
541 struct mbuf
*opts
= 0;
543 struct ip6_recvpktopts opts6
;
544 bzero(&opts6
, sizeof(opts6
));
548 if (last
->inp_flags
& INP_CONTROLOPTS
||
549 last
->inp_socket
->so_options
& SO_TIMESTAMP
) {
551 if (last
->inp_vflag
& INP_IPV6
) {
554 if (udp_ip6
.uip6_init_done
== 0) {
555 ip_2_ip6_hdr(&udp_ip6
.uip6_ip6
, ip
);
556 udp_ip6
.uip6_init_done
= 1;
558 savedflags
= last
->inp_flags
;
559 last
->inp_flags
&= ~INP_UNMAPPABLEOPTS
;
560 ip6_savecontrol(last
, &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
;
577 append_sa
= (struct sockaddr
*)&udp_in
;
580 if (sbappendaddr(&last
->inp_socket
->so_rcv
, append_sa
, n
, opts
) == 0) {
584 udpstat
.udps_fullsock
++;
586 sorwakeup(last
->inp_socket
);
592 * Notify a udp user of an asynchronous error;
593 * just wake up so that he can collect error status.
596 udp_notify(inp
, errno
)
597 register struct inpcb
*inp
;
600 inp
->inp_socket
->so_error
= errno
;
601 sorwakeup(inp
->inp_socket
);
602 sowwakeup(inp
->inp_socket
);
606 udp_ctlinput(cmd
, sa
, vip
)
611 register struct ip
*ip
= vip
;
612 register struct udphdr
*uh
;
614 if (!PRC_IS_REDIRECT(cmd
) &&
615 ((unsigned)cmd
>= PRC_NCMDS
|| inetctlerrmap
[cmd
] == 0))
618 uh
= (struct udphdr
*)((caddr_t
)ip
+ (ip
->ip_hl
<< 2));
619 in_pcbnotify(&udb
, sa
, uh
->uh_dport
, ip
->ip_src
, uh
->uh_sport
,
622 in_pcbnotify(&udb
, sa
, 0, zeroin_addr
, 0, cmd
, udp_notify
);
627 udp_pcblist SYSCTL_HANDLER_ARGS
630 struct inpcb
*inp
, **inp_list
;
635 * The process of preparing the TCB list is too time-consuming and
636 * resource-intensive to repeat twice on every request.
638 if (req
->oldptr
== 0) {
639 n
= udbinfo
.ipi_count
;
640 req
->oldidx
= 2 * (sizeof xig
)
641 + (n
+ n
/8) * sizeof(struct xinpcb
);
645 if (req
->newptr
!= 0)
649 * OK, now we're committed to doing something.
652 gencnt
= udbinfo
.ipi_gencnt
;
653 n
= udbinfo
.ipi_count
;
656 xig
.xig_len
= sizeof xig
;
658 xig
.xig_gen
= gencnt
;
659 xig
.xig_sogen
= so_gencnt
;
660 error
= SYSCTL_OUT(req
, &xig
, sizeof xig
);
664 * We are done if there is no pcb
669 inp_list
= _MALLOC(n
* sizeof *inp_list
, M_TEMP
, M_WAITOK
);
674 for (inp
= udbinfo
.listhead
->lh_first
, i
= 0; inp
&& i
< n
;
675 inp
= inp
->inp_list
.le_next
) {
676 if (inp
->inp_gencnt
<= gencnt
)
683 for (i
= 0; i
< n
; i
++) {
685 if (inp
->inp_gencnt
<= gencnt
) {
687 xi
.xi_len
= sizeof xi
;
688 /* XXX should avoid extra copy */
689 bcopy(inp
, &xi
.xi_inp
, sizeof *inp
);
691 sotoxsocket(inp
->inp_socket
, &xi
.xi_socket
);
692 error
= SYSCTL_OUT(req
, &xi
, sizeof xi
);
697 * Give the user an updated idea of our state.
698 * If the generation differs from what we told
699 * her before, she knows that something happened
700 * while we were processing this request, and it
701 * might be necessary to retry.
704 xig
.xig_gen
= udbinfo
.ipi_gencnt
;
705 xig
.xig_sogen
= so_gencnt
;
706 xig
.xig_count
= udbinfo
.ipi_count
;
708 error
= SYSCTL_OUT(req
, &xig
, sizeof xig
);
710 FREE(inp_list
, M_TEMP
);
714 SYSCTL_PROC(_net_inet_udp
, UDPCTL_PCBLIST
, pcblist
, CTLFLAG_RD
, 0, 0,
715 udp_pcblist
, "S,xinpcb", "List of active UDP sockets");
720 udp_output(inp
, m
, addr
, control
, p
)
721 register struct inpcb
*inp
;
722 register struct mbuf
*m
;
723 struct sockaddr
*addr
;
724 struct mbuf
*control
;
727 register struct udpiphdr
*ui
;
728 register int len
= m
->m_pkthdr
.len
;
729 struct in_addr laddr
;
730 int s
= 0, error
= 0;
732 KERNEL_DEBUG(DBG_FNC_UDP_OUTPUT
| DBG_FUNC_START
, 0,0,0,0,0);
735 m_freem(control
); /* XXX */
737 KERNEL_DEBUG(DBG_LAYER_OUT_BEG
, inp
->inp_fport
, inp
->inp_lport
,
738 inp
->inp_laddr
.s_addr
, inp
->inp_faddr
.s_addr
,
739 (htons((u_short
)len
+ sizeof (struct udphdr
))));
741 if (len
+ sizeof(struct udpiphdr
) > IP_MAXPACKET
) {
747 laddr
= inp
->inp_laddr
;
748 if (inp
->inp_faddr
.s_addr
!= INADDR_ANY
) {
753 * Must block input while temporarily connected.
756 error
= in_pcbconnect(inp
, addr
, p
);
762 if (inp
->inp_faddr
.s_addr
== INADDR_ANY
) {
768 * Calculate data length and get a mbuf
769 * for UDP and IP headers.
771 M_PREPEND(m
, sizeof(struct udpiphdr
), M_DONTWAIT
);
780 * Fill in mbuf with extended UDP header
781 * and addresses and length put into network format.
783 ui
= mtod(m
, struct udpiphdr
*);
784 bzero(ui
->ui_x1
, sizeof(ui
->ui_x1
));
785 ui
->ui_pr
= IPPROTO_UDP
;
786 ui
->ui_len
= htons((u_short
)len
+ sizeof (struct udphdr
));
787 ui
->ui_src
= inp
->inp_laddr
;
788 ui
->ui_dst
= inp
->inp_faddr
;
789 ui
->ui_sport
= inp
->inp_lport
;
790 ui
->ui_dport
= inp
->inp_fport
;
791 ui
->ui_ulen
= ui
->ui_len
;
794 * Stuff checksum and output datagram.
797 ui
->ui_sum
= in_pseudo(ui
->ui_src
.s_addr
, ui
->ui_dst
.s_addr
,
798 htons((u_short
)len
+ sizeof(struct udphdr
) + IPPROTO_UDP
));
799 m
->m_pkthdr
.csum_flags
= CSUM_UDP
;
800 m
->m_pkthdr
.csum_data
= offsetof(struct udphdr
, uh_sum
);
804 ((struct ip
*)ui
)->ip_len
= sizeof (struct udpiphdr
) + len
;
805 ((struct ip
*)ui
)->ip_ttl
= inp
->inp_ip_ttl
; /* XXX */
806 ((struct ip
*)ui
)->ip_tos
= inp
->inp_ip_tos
; /* XXX */
807 udpstat
.udps_opackets
++;
809 KERNEL_DEBUG(DBG_LAYER_OUT_END
, ui
->ui_dport
, ui
->ui_sport
,
810 ui
->ui_src
.s_addr
, ui
->ui_dst
.s_addr
, ui
->ui_ulen
);
814 ipsec_setsocket(m
, inp
->inp_socket
);
817 error
= ip_output(m
, inp
->inp_options
, &inp
->inp_route
,
818 inp
->inp_socket
->so_options
& (SO_DONTROUTE
| SO_BROADCAST
),
822 in_pcbdisconnect(inp
);
823 inp
->inp_laddr
= laddr
; /* XXX rehash? */
826 KERNEL_DEBUG(DBG_FNC_UDP_OUTPUT
| DBG_FUNC_END
, error
, 0,0,0,0);
831 KERNEL_DEBUG(DBG_FNC_UDP_OUTPUT
| DBG_FUNC_END
, error
, 0,0,0,0);
835 u_long udp_sendspace
= 9216; /* really max datagram size */
836 /* 40 1K datagrams */
837 SYSCTL_INT(_net_inet_udp
, UDPCTL_MAXDGRAM
, maxdgram
, CTLFLAG_RW
,
838 &udp_sendspace
, 0, "");
841 u_long udp_recvspace
= 40 * (1024 + /* 40 1K datagrams */
843 sizeof(struct sockaddr_in6
)
845 sizeof(struct sockaddr_in
)
848 SYSCTL_INT(_net_inet_udp
, UDPCTL_RECVSPACE
, recvspace
, CTLFLAG_RW
,
849 &udp_recvspace
, 0, "");
852 udp_abort(struct socket
*so
)
859 return EINVAL
; /* ??? possible? panic instead? */
860 soisdisconnected(so
);
868 udp_attach(struct socket
*so
, int proto
, struct proc
*p
)
873 if (so
->so_snd
.sb_hiwat
== 0 || so
->so_rcv
.sb_hiwat
== 0) {
874 error
= soreserve(so
, udp_sendspace
, udp_recvspace
);
879 error
= in_pcballoc(so
, &udbinfo
, p
);
883 error
= soreserve(so
, udp_sendspace
, udp_recvspace
);
886 inp
= (struct inpcb
*)so
->so_pcb
;
887 inp
->inp_vflag
|= INP_IPV4
;
888 inp
->inp_ip_ttl
= ip_defttl
;
890 error
= ipsec_init_policy(so
, &inp
->inp_sp
);
900 udp_bind(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
909 error
= in_pcbbind(inp
, nam
, p
);
915 udp_connect(struct socket
*so
, struct sockaddr
*nam
, struct proc
*p
)
923 if (inp
->inp_faddr
.s_addr
!= INADDR_ANY
)
926 error
= in_pcbconnect(inp
, nam
, p
);
934 udp_detach(struct socket
*so
)
949 udp_disconnect(struct socket
*so
)
957 if (inp
->inp_faddr
.s_addr
== INADDR_ANY
)
961 in_pcbdisconnect(inp
);
962 inp
->inp_laddr
.s_addr
= INADDR_ANY
;
964 so
->so_state
&= ~SS_ISCONNECTED
; /* XXX */
969 udp_send(struct socket
*so
, int flags
, struct mbuf
*m
, struct sockaddr
*addr
,
970 struct mbuf
*control
, struct proc
*p
)
979 return udp_output(inp
, m
, addr
, control
, p
);
983 udp_shutdown(struct socket
*so
)
994 struct pr_usrreqs udp_usrreqs
= {
995 udp_abort
, pru_accept_notsupp
, udp_attach
, udp_bind
, udp_connect
,
996 pru_connect2_notsupp
, in_control
, udp_detach
, udp_disconnect
,
997 pru_listen_notsupp
, in_setpeeraddr
, pru_rcvd_notsupp
,
998 pru_rcvoob_notsupp
, udp_send
, pru_sense_null
, udp_shutdown
,
999 in_setsockaddr
, sosend
, soreceive
, sopoll