2 * Copyright (c) 2004 Jeffrey M. Hsu. All rights reserved.
3 * Copyright (c) 2004 The DragonFly Project. All rights reserved.
5 * This code is derived from software contributed to The DragonFly Project
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of The DragonFly Project nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific, prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
30 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * Copyright (c) 2004 Jeffrey M. Hsu. All rights reserved.
37 * License terms: all terms for the DragonFly license above plus the following:
39 * 4. All advertising materials mentioning features or use of this software
40 * must display the following acknowledgement:
42 * This product includes software developed by Jeffrey M. Hsu
43 * for the DragonFly Project.
45 * This requirement may be waived with permission from Jeffrey Hsu.
46 * This requirement will sunset and may be removed on July 8 2005,
47 * after which the standard DragonFly license (as shown above) will
52 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
53 * The Regents of the University of California. All rights reserved.
55 * Redistribution and use in source and binary forms, with or without
56 * modification, are permitted provided that the following conditions
58 * 1. Redistributions of source code must retain the above copyright
59 * notice, this list of conditions and the following disclaimer.
60 * 2. Redistributions in binary form must reproduce the above copyright
61 * notice, this list of conditions and the following disclaimer in the
62 * documentation and/or other materials provided with the distribution.
63 * 3. All advertising materials mentioning features or use of this software
64 * must display the following acknowledgement:
65 * This product includes software developed by the University of
66 * California, Berkeley and its contributors.
67 * 4. Neither the name of the University nor the names of its contributors
68 * may be used to endorse or promote products derived from this software
69 * without specific prior written permission.
71 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
72 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
73 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
74 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
75 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
76 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
77 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
78 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
79 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
80 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
83 * @(#)udp_usrreq.c 8.6 (Berkeley) 5/23/95
84 * $FreeBSD: src/sys/netinet/udp_usrreq.c,v 1.64.2.18 2003/01/24 05:11:34 sam Exp $
85 * $DragonFly: src/sys/netinet/udp_usrreq.c,v 1.36 2006/01/14 11:33:50 swildner Exp $
88 #include "opt_ipsec.h"
89 #include "opt_inet6.h"
91 #include <sys/param.h>
92 #include <sys/systm.h>
93 #include <sys/kernel.h>
94 #include <sys/malloc.h>
96 #include <sys/domain.h>
98 #include <sys/protosw.h>
99 #include <sys/socket.h>
100 #include <sys/socketvar.h>
101 #include <sys/sysctl.h>
102 #include <sys/syslog.h>
103 #include <sys/thread2.h>
104 #include <sys/in_cksum.h>
106 #include <machine/stdarg.h>
108 #include <vm/vm_zone.h>
111 #include <net/route.h>
113 #include <netinet/in.h>
114 #include <netinet/in_systm.h>
115 #include <netinet/ip.h>
117 #include <netinet/ip6.h>
119 #include <netinet/in_pcb.h>
120 #include <netinet/in_var.h>
121 #include <netinet/ip_var.h>
123 #include <netinet6/ip6_var.h>
125 #include <netinet/ip_icmp.h>
126 #include <netinet/icmp_var.h>
127 #include <netinet/udp.h>
128 #include <netinet/udp_var.h>
131 #include <netproto/ipsec/ipsec.h>
135 #include <netinet6/ipsec.h>
139 * UDP protocol implementation.
140 * Per RFC 768, August, 1980.
143 static int udpcksum = 1;
145 static int udpcksum = 0; /* XXX */
147 SYSCTL_INT(_net_inet_udp, UDPCTL_CHECKSUM, checksum, CTLFLAG_RW,
151 SYSCTL_INT(_net_inet_udp, OID_AUTO, log_in_vain, CTLFLAG_RW,
152 &log_in_vain, 0, "Log all incoming UDP packets");
154 static int blackhole = 0;
155 SYSCTL_INT(_net_inet_udp, OID_AUTO, blackhole, CTLFLAG_RW,
156 &blackhole, 0, "Do not send port unreachables for refused connects");
158 static int strict_mcast_mship = 1;
159 SYSCTL_INT(_net_inet_udp, OID_AUTO, strict_mcast_mship, CTLFLAG_RW,
160 &strict_mcast_mship, 0, "Only send multicast to member sockets");
162 struct inpcbinfo udbinfo;
165 #define UDBHASHSIZE 16
168 struct udpstat udpstat; /* from udp_var.h */
169 SYSCTL_STRUCT(_net_inet_udp, UDPCTL_STATS, stats, CTLFLAG_RW,
170 &udpstat, udpstat, "UDP statistics (struct udpstat, netinet/udp_var.h)");
172 static struct sockaddr_in udp_in = { sizeof udp_in, AF_INET };
175 struct sockaddr_in6 uin6_sin;
176 u_char uin6_init_done : 1;
178 { sizeof udp_in6.uin6_sin, AF_INET6 },
182 struct ip6_hdr uip6_ip6;
183 u_char uip6_init_done : 1;
187 static void udp_append (struct inpcb *last, struct ip *ip,
188 struct mbuf *n, int off);
190 static void ip_2_ip6_hdr (struct ip6_hdr *ip6, struct ip *ip);
193 static int udp_detach (struct socket *so);
194 static int udp_output (struct inpcb *, struct mbuf *, struct sockaddr *,
195 struct mbuf *, struct thread *);
200 in_pcbinfo_init(&udbinfo);
201 udbinfo.hashbase = hashinit(UDBHASHSIZE, M_PCB, &udbinfo.hashmask);
202 udbinfo.porthashbase = hashinit(UDBHASHSIZE, M_PCB,
203 &udbinfo.porthashmask);
204 udbinfo.wildcardhashbase = hashinit(UDBHASHSIZE, M_PCB,
205 &udbinfo.wildcardhashmask);
206 udbinfo.ipi_zone = zinit("udpcb", sizeof(struct inpcb), maxsockets,
212 * Check multicast packets to make sure they are only sent to sockets with
213 * multicast memberships for the packet's destination address and arrival
214 * interface. Multicast packets to multicast-unaware sockets are also
217 * Returns 0 if the packet is acceptable, -1 if it is not.
220 check_multicast_membership(struct ip *ip, struct inpcb *inp, struct mbuf *m)
223 struct ip_moptions *mopt;
225 if (strict_mcast_mship == 0 ||
226 !IN_MULTICAST(ntohl(ip->ip_dst.s_addr))) {
229 mopt = inp->inp_moptions;
232 for (mshipno = 0; mshipno <= mopt->imo_num_memberships; ++mshipno) {
233 struct in_multi *maddr = mopt->imo_membership[mshipno];
235 if (ip->ip_dst.s_addr == maddr->inm_addr.s_addr &&
236 m->m_pkthdr.rcvif == maddr->inm_ifp) {
244 udp_input(struct mbuf *m, ...)
250 struct mbuf *opts = NULL;
253 struct sockaddr *append_sa;
257 off = __va_arg(ap, int);
258 proto = __va_arg(ap, int);
262 udpstat.udps_ipackets++;
265 * Strip IP options, if any; should skip this,
266 * make available to user, and use on returned packets,
267 * but we don't yet have a way to check the checksum
268 * with options still present.
270 if (iphlen > sizeof(struct ip)) {
272 iphlen = sizeof(struct ip);
276 * IP and UDP headers are together in first mbuf.
277 * Already checked and pulled up in ip_demux().
279 KASSERT(m->m_len >= iphlen + sizeof(struct udphdr),
280 ("UDP header not in one mbuf"));
282 ip = mtod(m, struct ip *);
283 uh = (struct udphdr *)((caddr_t)ip + iphlen);
285 /* destination port of 0 is illegal, based on RFC768. */
286 if (uh->uh_dport == 0)
290 * Make mbuf data length reflect UDP length.
291 * If not enough data to reflect UDP length, drop.
293 len = ntohs((u_short)uh->uh_ulen);
294 if (ip->ip_len != len) {
295 if (len > ip->ip_len || len < sizeof(struct udphdr)) {
296 udpstat.udps_badlen++;
299 m_adj(m, len - ip->ip_len);
300 /* ip->ip_len = len; */
303 * Save a copy of the IP header in case we want restore it
304 * for sending an ICMP error message in response.
309 * Checksum extended UDP header and data.
312 if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
313 if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
314 uh->uh_sum = m->m_pkthdr.csum_data;
316 uh->uh_sum = in_pseudo(ip->ip_src.s_addr,
317 ip->ip_dst.s_addr, htonl((u_short)len +
318 m->m_pkthdr.csum_data + IPPROTO_UDP));
319 uh->uh_sum ^= 0xffff;
323 bcopy(((struct ipovly *)ip)->ih_x1, b, 9);
324 bzero(((struct ipovly *)ip)->ih_x1, 9);
325 ((struct ipovly *)ip)->ih_len = uh->uh_ulen;
326 uh->uh_sum = in_cksum(m, len + sizeof(struct ip));
327 bcopy(b, ((struct ipovly *)ip)->ih_x1, 9);
330 udpstat.udps_badsum++;
335 udpstat.udps_nosum++;
337 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
338 in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) {
342 * Deliver a multicast or broadcast datagram to *all* sockets
343 * for which the local and remote addresses and ports match
344 * those of the incoming datagram. This allows more than
345 * one process to receive multi/broadcasts on the same port.
346 * (This really ought to be done for unicast datagrams as
347 * well, but that would cause problems with existing
348 * applications that open both address-specific sockets and
349 * a wildcard socket listening to the same port -- they would
350 * end up receiving duplicates of every unicast datagram.
351 * Those applications open the multiple sockets to overcome an
352 * inadequacy of the UDP socket interface, but for backwards
353 * compatibility we avoid the problem here rather than
354 * fixing the interface. Maybe 4.5BSD will remedy this?)
358 * Construct sockaddr format source address.
360 udp_in.sin_port = uh->uh_sport;
361 udp_in.sin_addr = ip->ip_src;
363 * Locate pcb(s) for datagram.
364 * (Algorithm copied from raw_intr().)
368 udp_in6.uin6_init_done = udp_ip6.uip6_init_done = 0;
370 LIST_FOREACH(inp, &udbinfo.pcblisthead, inp_list) {
371 if (inp->inp_flags & INP_PLACEMARKER)
374 if (!(inp->inp_vflag & INP_IPV4))
377 if (inp->inp_lport != uh->uh_dport)
379 if (inp->inp_laddr.s_addr != INADDR_ANY) {
380 if (inp->inp_laddr.s_addr !=
384 if (inp->inp_faddr.s_addr != INADDR_ANY) {
385 if (inp->inp_faddr.s_addr !=
387 inp->inp_fport != uh->uh_sport)
391 if (check_multicast_membership(ip, inp, m) < 0)
398 /* check AH/ESP integrity. */
399 if (ipsec4_in_reject_so(m, last->inp_socket))
400 ipsecstat.in_polvio++;
401 /* do not inject data to pcb */
405 /* check AH/ESP integrity. */
406 if (ipsec4_in_reject(m, last))
409 #endif /*FAST_IPSEC*/
410 if ((n = m_copypacket(m, MB_DONTWAIT)) != NULL)
411 udp_append(last, ip, n,
413 sizeof(struct udphdr));
417 * Don't look for additional matches if this one does
418 * not have either the SO_REUSEPORT or SO_REUSEADDR
419 * socket options set. This heuristic avoids searching
420 * through all pcbs in the common case of a non-shared
421 * port. It * assumes that an application will never
422 * clear these options after setting them.
424 if (!(last->inp_socket->so_options &
425 (SO_REUSEPORT | SO_REUSEADDR)))
431 * No matching pcb found; discard datagram.
432 * (No need to send an ICMP Port Unreachable
433 * for a broadcast or multicast datgram.)
435 udpstat.udps_noportbcast++;
439 /* check AH/ESP integrity. */
440 if (ipsec4_in_reject_so(m, last->inp_socket)) {
441 ipsecstat.in_polvio++;
446 /* check AH/ESP integrity. */
447 if (ipsec4_in_reject(m, last))
449 #endif /*FAST_IPSEC*/
450 udp_append(last, ip, m, iphlen + sizeof(struct udphdr));
454 * Locate pcb for datagram.
456 inp = in_pcblookup_hash(&udbinfo, ip->ip_src, uh->uh_sport,
457 ip->ip_dst, uh->uh_dport, 1, m->m_pkthdr.rcvif);
460 char buf[sizeof "aaa.bbb.ccc.ddd"];
462 strcpy(buf, inet_ntoa(ip->ip_dst));
464 "Connection attempt to UDP %s:%d from %s:%d\n",
465 buf, ntohs(uh->uh_dport), inet_ntoa(ip->ip_src),
466 ntohs(uh->uh_sport));
468 udpstat.udps_noport++;
469 if (m->m_flags & (M_BCAST | M_MCAST)) {
470 udpstat.udps_noportbcast++;
476 if (badport_bandlim(BANDLIM_ICMP_UNREACH) < 0)
480 ip->ip_len += iphlen;
481 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
485 if (ipsec4_in_reject_so(m, inp->inp_socket)) {
486 ipsecstat.in_polvio++;
491 if (ipsec4_in_reject(m, inp))
493 #endif /*FAST_IPSEC*/
496 * Construct sockaddr format source address.
497 * Stuff source address and datagram in user buffer.
499 udp_in.sin_port = uh->uh_sport;
500 udp_in.sin_addr = ip->ip_src;
501 if ((inp->inp_flags & INP_CONTROLOPTS) ||
502 (inp->inp_socket->so_options & SO_TIMESTAMP)) {
504 if (inp->inp_vflag & INP_IPV6) {
507 ip_2_ip6_hdr(&udp_ip6.uip6_ip6, ip);
508 savedflags = inp->inp_flags;
509 inp->inp_flags &= ~INP_UNMAPPABLEOPTS;
510 ip6_savecontrol(inp, &opts, &udp_ip6.uip6_ip6, m);
511 inp->inp_flags = savedflags;
514 ip_savecontrol(inp, &opts, ip, m);
516 m_adj(m, iphlen + sizeof(struct udphdr));
518 if (inp->inp_vflag & INP_IPV6) {
519 in6_sin_2_v4mapsin6(&udp_in, &udp_in6.uin6_sin);
520 append_sa = (struct sockaddr *)&udp_in6;
523 append_sa = (struct sockaddr *)&udp_in;
524 if (sbappendaddr(&inp->inp_socket->so_rcv, append_sa, m, opts) == 0) {
525 udpstat.udps_fullsock++;
528 sorwakeup(inp->inp_socket);
539 ip_2_ip6_hdr(struct ip6_hdr *ip6, struct ip *ip)
541 bzero(ip6, sizeof *ip6);
543 ip6->ip6_vfc = IPV6_VERSION;
544 ip6->ip6_plen = ip->ip_len;
545 ip6->ip6_nxt = ip->ip_p;
546 ip6->ip6_hlim = ip->ip_ttl;
547 ip6->ip6_src.s6_addr32[2] = ip6->ip6_dst.s6_addr32[2] =
549 ip6->ip6_src.s6_addr32[3] = ip->ip_src.s_addr;
550 ip6->ip6_dst.s6_addr32[3] = ip->ip_dst.s_addr;
555 * subroutine of udp_input(), mainly for source code readability.
556 * caller must properly init udp_ip6 and udp_in6 beforehand.
559 udp_append(struct inpcb *last, struct ip *ip, struct mbuf *n, int off)
561 struct sockaddr *append_sa;
562 struct mbuf *opts = NULL;
564 if (last->inp_flags & INP_CONTROLOPTS ||
565 last->inp_socket->so_options & SO_TIMESTAMP) {
567 if (last->inp_vflag & INP_IPV6) {
570 if (udp_ip6.uip6_init_done == 0) {
571 ip_2_ip6_hdr(&udp_ip6.uip6_ip6, ip);
572 udp_ip6.uip6_init_done = 1;
574 savedflags = last->inp_flags;
575 last->inp_flags &= ~INP_UNMAPPABLEOPTS;
576 ip6_savecontrol(last, &opts, &udp_ip6.uip6_ip6, n);
577 last->inp_flags = savedflags;
580 ip_savecontrol(last, &opts, ip, n);
583 if (last->inp_vflag & INP_IPV6) {
584 if (udp_in6.uin6_init_done == 0) {
585 in6_sin_2_v4mapsin6(&udp_in, &udp_in6.uin6_sin);
586 udp_in6.uin6_init_done = 1;
588 append_sa = (struct sockaddr *)&udp_in6.uin6_sin;
591 append_sa = (struct sockaddr *)&udp_in;
593 if (sbappendaddr(&last->inp_socket->so_rcv, append_sa, n, opts) == 0) {
597 udpstat.udps_fullsock++;
599 sorwakeup(last->inp_socket);
603 * Notify a udp user of an asynchronous error;
604 * just wake up so that he can collect error status.
607 udp_notify(struct inpcb *inp, int errno)
609 inp->inp_socket->so_error = errno;
610 sorwakeup(inp->inp_socket);
611 sowwakeup(inp->inp_socket);
615 udp_ctlinput(int cmd, struct sockaddr *sa, void *vip)
619 void (*notify) (struct inpcb *, int) = udp_notify;
620 struct in_addr faddr;
623 faddr = ((struct sockaddr_in *)sa)->sin_addr;
624 if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY)
627 if (PRC_IS_REDIRECT(cmd)) {
629 notify = in_rtchange;
630 } else if (cmd == PRC_HOSTDEAD)
632 else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0)
636 uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
637 inp = in_pcblookup_hash(&udbinfo, faddr, uh->uh_dport,
638 ip->ip_src, uh->uh_sport, 0, NULL);
639 if (inp != NULL && inp->inp_socket != NULL)
640 (*notify)(inp, inetctlerrmap[cmd]);
643 in_pcbnotifyall(&udbinfo.pcblisthead, faddr, inetctlerrmap[cmd],
647 SYSCTL_PROC(_net_inet_udp, UDPCTL_PCBLIST, pcblist, CTLFLAG_RD, &udbinfo, 0,
648 in_pcblist_global, "S,xinpcb", "List of active UDP sockets");
651 udp_getcred(SYSCTL_HANDLER_ARGS)
653 struct sockaddr_in addrs[2];
657 error = suser(req->td);
660 error = SYSCTL_IN(req, addrs, sizeof addrs);
664 inp = in_pcblookup_hash(&udbinfo, addrs[1].sin_addr, addrs[1].sin_port,
665 addrs[0].sin_addr, addrs[0].sin_port, 1, NULL);
666 if (inp == NULL || inp->inp_socket == NULL) {
670 error = SYSCTL_OUT(req, inp->inp_socket->so_cred, sizeof(struct ucred));
676 SYSCTL_PROC(_net_inet_udp, OID_AUTO, getcred, CTLTYPE_OPAQUE|CTLFLAG_RW,
677 0, 0, udp_getcred, "S,ucred", "Get the ucred of a UDP connection");
680 udp_output(struct inpcb *inp, struct mbuf *m, struct sockaddr *dstaddr,
681 struct mbuf *control, struct thread *td)
684 int len = m->m_pkthdr.len;
685 struct sockaddr_in *sin; /* really is initialized before use */
688 if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) {
693 if (inp->inp_lport == 0) { /* unbound socket */
694 error = in_pcbbind(inp, (struct sockaddr *)NULL, td);
697 in_pcbinswildcardhash(inp);
700 if (dstaddr != NULL) { /* destination address specified */
701 if (inp->inp_faddr.s_addr != INADDR_ANY) {
702 /* already connected */
706 sin = (struct sockaddr_in *)dstaddr;
707 prison_remote_ip(td, 0, &sin->sin_addr.s_addr);
709 if (inp->inp_faddr.s_addr == INADDR_ANY) {
710 /* no destination specified and not already connected */
718 * Calculate data length and get a mbuf
719 * for UDP and IP headers.
721 M_PREPEND(m, sizeof(struct udpiphdr), MB_DONTWAIT);
728 * Fill in mbuf with extended UDP header
729 * and addresses and length put into network format.
731 ui = mtod(m, struct udpiphdr *);
732 bzero(ui->ui_x1, sizeof ui->ui_x1); /* XXX still needed? */
733 ui->ui_pr = IPPROTO_UDP;
736 * Set destination address.
738 if (dstaddr != NULL) { /* use specified destination */
739 ui->ui_dst = sin->sin_addr;
740 ui->ui_dport = sin->sin_port;
741 } else { /* use connected destination */
742 ui->ui_dst = inp->inp_faddr;
743 ui->ui_dport = inp->inp_fport;
747 * Set source address.
749 if (inp->inp_laddr.s_addr == INADDR_ANY) {
750 struct sockaddr_in *if_sin;
752 if (dstaddr == NULL) {
754 * connect() had (or should have) failed because
755 * the interface had no IP address, but the
756 * application proceeded to call send() anyways.
762 /* Look up outgoing interface. */
763 if ((error = in_pcbladdr(inp, dstaddr, &if_sin)))
765 ui->ui_src = if_sin->sin_addr; /* use address of interface */
767 ui->ui_src = inp->inp_laddr; /* use non-null bound address */
769 ui->ui_sport = inp->inp_lport;
770 KASSERT(inp->inp_lport != 0, ("inp lport should have been bound"));
772 ui->ui_ulen = htons((u_short)len + sizeof(struct udphdr));
775 * Set up checksum and output datagram.
778 ui->ui_sum = in_pseudo(ui->ui_src.s_addr, ui->ui_dst.s_addr,
779 htons((u_short)len + sizeof(struct udphdr) + IPPROTO_UDP));
780 m->m_pkthdr.csum_flags = CSUM_UDP;
781 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
785 ((struct ip *)ui)->ip_len = sizeof(struct udpiphdr) + len;
786 ((struct ip *)ui)->ip_ttl = inp->inp_ip_ttl; /* XXX */
787 ((struct ip *)ui)->ip_tos = inp->inp_ip_tos; /* XXX */
788 udpstat.udps_opackets++;
790 error = ip_output(m, inp->inp_options, &inp->inp_route,
791 (inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST)),
792 inp->inp_moptions, inp);
801 u_long udp_sendspace = 9216; /* really max datagram size */
802 /* 40 1K datagrams */
803 SYSCTL_INT(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram, CTLFLAG_RW,
804 &udp_sendspace, 0, "Maximum outgoing UDP datagram size");
806 u_long udp_recvspace = 40 * (1024 +
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, "Maximum incoming UDP datagram size");
817 udp_abort(struct socket *so)
823 return EINVAL; /* ??? possible? panic instead? */
824 soisdisconnected(so);
832 udp_attach(struct socket *so, int proto, struct pru_attach_info *ai)
841 error = soreserve(so, udp_sendspace, udp_recvspace, ai->sb_rlimit);
845 error = in_pcballoc(so, &udbinfo);
850 inp = (struct inpcb *)so->so_pcb;
851 inp->inp_vflag |= INP_IPV4;
852 inp->inp_ip_ttl = ip_defttl;
857 udp_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
859 struct sockaddr_in *sin = (struct sockaddr_in *)nam;
867 error = in_pcbbind(inp, nam, td);
870 if (sin->sin_addr.s_addr != INADDR_ANY)
871 inp->inp_flags |= INP_WASBOUND_NOTANY;
872 in_pcbinswildcardhash(inp);
878 udp_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
882 struct sockaddr_in *sin;
887 if (inp->inp_faddr.s_addr != INADDR_ANY)
891 if (td->td_proc && td->td_proc->p_ucred->cr_prison != NULL &&
892 inp->inp_laddr.s_addr == INADDR_ANY) {
893 error = in_pcbbind(inp, NULL, td);
896 sin = (struct sockaddr_in *)nam;
897 prison_remote_ip(td, 0, &sin->sin_addr.s_addr);
898 if (inp->inp_flags & INP_WILDCARD)
899 in_pcbremwildcardhash(inp);
900 error = in_pcbconnect(inp, nam, td);
905 else if (error == EAFNOSUPPORT) { /* connection dissolved */
907 * Follow traditional BSD behavior and retain
908 * the local port binding. But, fix the old misbehavior
909 * of overwriting any previously bound local address.
911 if (!(inp->inp_flags & INP_WASBOUND_NOTANY))
912 inp->inp_laddr.s_addr = INADDR_ANY;
913 in_pcbinswildcardhash(inp);
919 udp_detach(struct socket *so)
933 udp_disconnect(struct socket *so)
940 if (inp->inp_faddr.s_addr == INADDR_ANY)
944 in_pcbdisconnect(inp);
946 so->so_state &= ~SS_ISCONNECTED; /* XXX */
951 udp_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
952 struct mbuf *control, struct thread *td)
961 return udp_output(inp, m, addr, control, td);
965 udp_shutdown(struct socket *so)
976 struct pr_usrreqs udp_usrreqs = {
977 udp_abort, pru_accept_notsupp, udp_attach, udp_bind, udp_connect,
978 pru_connect2_notsupp, in_control, udp_detach, udp_disconnect,
979 pru_listen_notsupp, in_setpeeraddr, pru_rcvd_notsupp,
980 pru_rcvoob_notsupp, udp_send, pru_sense_null, udp_shutdown,
981 in_setsockaddr, sosendudp, soreceive, sopoll