2 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
3 * The Regents of the University of California.
4 * Copyright (c) 2008 Robert N. M. Watson
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 4. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * @(#)udp_usrreq.c 8.6 (Berkeley) 5/23/95
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
38 #include "opt_inet6.h"
39 #include "opt_ipsec.h"
42 #include <sys/param.h>
43 #include <sys/domain.h>
44 #include <sys/eventhandler.h>
46 #include <sys/kernel.h>
48 #include <sys/malloc.h>
52 #include <sys/protosw.h>
53 #include <sys/signalvar.h>
54 #include <sys/socket.h>
55 #include <sys/socketvar.h>
57 #include <sys/sysctl.h>
58 #include <sys/syslog.h>
59 #include <sys/systm.h>
60 #include <sys/vimage.h>
65 #include <net/route.h>
67 #include <netinet/in.h>
68 #include <netinet/in_pcb.h>
69 #include <netinet/in_systm.h>
70 #include <netinet/in_var.h>
71 #include <netinet/ip.h>
73 #include <netinet/ip6.h>
75 #include <netinet/ip_icmp.h>
76 #include <netinet/icmp_var.h>
77 #include <netinet/ip_var.h>
78 #include <netinet/ip_options.h>
80 #include <netinet6/ip6_var.h>
82 #include <netinet/udp.h>
83 #include <netinet/udp_var.h>
86 #include <netipsec/ipsec.h>
89 #include <machine/in_cksum.h>
91 #include <security/mac/mac_framework.h>
94 * UDP protocol implementation.
95 * Per RFC 768, August, 1980.
99 * BSD 4.2 defaulted the udp checksum to be off. Turning off udp checksums
100 * removes the only data integrity mechanism for packets and malformed
101 * packets that would otherwise be discarded due to bad checksums, and may
102 * cause problems (especially for NFS data blocks).
104 static int udp_cksum = 1;
105 SYSCTL_INT(_net_inet_udp, UDPCTL_CHECKSUM, checksum, CTLFLAG_RW, &udp_cksum,
106 0, "compute udp checksum");
108 int udp_log_in_vain = 0;
109 SYSCTL_INT(_net_inet_udp, OID_AUTO, log_in_vain, CTLFLAG_RW,
110 &udp_log_in_vain, 0, "Log all incoming UDP packets");
112 int udp_blackhole = 0;
113 SYSCTL_INT(_net_inet_udp, OID_AUTO, blackhole, CTLFLAG_RW, &udp_blackhole, 0,
114 "Do not send port unreachables for refused connects");
116 u_long udp_sendspace = 9216; /* really max datagram size */
117 /* 40 1K datagrams */
118 SYSCTL_ULONG(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram, CTLFLAG_RW,
119 &udp_sendspace, 0, "Maximum outgoing UDP datagram size");
121 u_long udp_recvspace = 40 * (1024 +
123 sizeof(struct sockaddr_in6)
125 sizeof(struct sockaddr_in)
129 SYSCTL_ULONG(_net_inet_udp, UDPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
130 &udp_recvspace, 0, "Maximum space for incoming UDP datagrams");
132 struct inpcbhead udb; /* from udp_var.h */
133 struct inpcbinfo udbinfo;
136 #define UDBHASHSIZE 128
139 struct udpstat udpstat; /* from udp_var.h */
140 SYSCTL_STRUCT(_net_inet_udp, UDPCTL_STATS, stats, CTLFLAG_RW, &udpstat,
141 udpstat, "UDP statistics (struct udpstat, netinet/udp_var.h)");
143 static void udp_detach(struct socket *so);
144 static int udp_output(struct inpcb *, struct mbuf *, struct sockaddr *,
145 struct mbuf *, struct thread *);
148 udp_zone_change(void *tag)
151 uma_zone_set_max(V_udbinfo.ipi_zone, maxsockets);
155 udp_inpcb_init(void *mem, int size, int flags)
160 INP_LOCK_INIT(inp, "inp", "udpinp");
168 INP_INFO_LOCK_INIT(&V_udbinfo, "udp");
170 V_udbinfo.ipi_listhead = &udb;
171 V_udbinfo.ipi_hashbase = hashinit(UDBHASHSIZE, M_PCB,
172 &V_udbinfo.ipi_hashmask);
173 V_udbinfo.ipi_porthashbase = hashinit(UDBHASHSIZE, M_PCB,
174 &V_udbinfo.ipi_porthashmask);
175 V_udbinfo.ipi_zone = uma_zcreate("udpcb", sizeof(struct inpcb), NULL,
176 NULL, udp_inpcb_init, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
177 uma_zone_set_max(V_udbinfo.ipi_zone, maxsockets);
178 EVENTHANDLER_REGISTER(maxsockets_change, udp_zone_change, NULL,
179 EVENTHANDLER_PRI_ANY);
183 * Subroutine of udp_input(), which appends the provided mbuf chain to the
184 * passed pcb/socket. The caller must provide a sockaddr_in via udp_in that
185 * contains the source address. If the socket ends up being an IPv6 socket,
186 * udp_append() will convert to a sockaddr_in6 before passing the address
187 * into the socket code.
190 udp_append(struct inpcb *inp, struct ip *ip, struct mbuf *n, int off,
191 struct sockaddr_in *udp_in)
193 struct sockaddr *append_sa;
195 struct mbuf *opts = 0;
197 struct sockaddr_in6 udp_in6;
200 INP_RLOCK_ASSERT(inp);
203 /* Check AH/ESP integrity. */
204 if (ipsec4_in_reject(n, inp)) {
206 V_ipsec4stat.in_polvio++;
211 if (mac_inpcb_check_deliver(inp, n) != 0) {
216 if (inp->inp_flags & INP_CONTROLOPTS ||
217 inp->inp_socket->so_options & (SO_TIMESTAMP | SO_BINTIME)) {
219 if (inp->inp_vflag & INP_IPV6)
220 (void)ip6_savecontrol_v4(inp, n, &opts, NULL);
223 ip_savecontrol(inp, &opts, ip, n);
226 if (inp->inp_vflag & INP_IPV6) {
227 bzero(&udp_in6, sizeof(udp_in6));
228 udp_in6.sin6_len = sizeof(udp_in6);
229 udp_in6.sin6_family = AF_INET6;
230 in6_sin_2_v4mapsin6(udp_in, &udp_in6);
231 append_sa = (struct sockaddr *)&udp_in6;
234 append_sa = (struct sockaddr *)udp_in;
237 so = inp->inp_socket;
238 SOCKBUF_LOCK(&so->so_rcv);
239 if (sbappendaddr_locked(&so->so_rcv, append_sa, n, opts) == 0) {
240 SOCKBUF_UNLOCK(&so->so_rcv);
244 V_udpstat.udps_fullsock++;
246 sorwakeup_locked(so);
250 udp_input(struct mbuf *m, int off)
259 struct sockaddr_in udp_in;
260 #ifdef IPFIREWALL_FORWARD
261 struct m_tag *fwd_tag;
264 ifp = m->m_pkthdr.rcvif;
265 V_udpstat.udps_ipackets++;
268 * Strip IP options, if any; should skip this, make available to
269 * user, and use on returned packets, but we don't yet have a way to
270 * check the checksum with options still present.
272 if (iphlen > sizeof (struct ip)) {
273 ip_stripoptions(m, (struct mbuf *)0);
274 iphlen = sizeof(struct ip);
278 * Get IP and UDP header together in first mbuf.
280 ip = mtod(m, struct ip *);
281 if (m->m_len < iphlen + sizeof(struct udphdr)) {
282 if ((m = m_pullup(m, iphlen + sizeof(struct udphdr))) == 0) {
283 V_udpstat.udps_hdrops++;
286 ip = mtod(m, struct ip *);
288 uh = (struct udphdr *)((caddr_t)ip + iphlen);
291 * Destination port of 0 is illegal, based on RFC768.
293 if (uh->uh_dport == 0)
297 * Construct sockaddr format source address. Stuff source address
298 * and datagram in user buffer.
300 bzero(&udp_in, sizeof(udp_in));
301 udp_in.sin_len = sizeof(udp_in);
302 udp_in.sin_family = AF_INET;
303 udp_in.sin_port = uh->uh_sport;
304 udp_in.sin_addr = ip->ip_src;
307 * Make mbuf data length reflect UDP length. If not enough data to
308 * reflect UDP length, drop.
310 len = ntohs((u_short)uh->uh_ulen);
311 if (ip->ip_len != len) {
312 if (len > ip->ip_len || len < sizeof(struct udphdr)) {
313 V_udpstat.udps_badlen++;
316 m_adj(m, len - ip->ip_len);
317 /* ip->ip_len = len; */
321 * Save a copy of the IP header in case we want restore it for
322 * sending an ICMP error message in response.
324 if (!V_udp_blackhole)
327 memset(&save_ip, 0, sizeof(save_ip));
330 * Checksum extended UDP header and data.
335 if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
336 if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
337 uh_sum = m->m_pkthdr.csum_data;
339 uh_sum = in_pseudo(ip->ip_src.s_addr,
340 ip->ip_dst.s_addr, htonl((u_short)len +
341 m->m_pkthdr.csum_data + IPPROTO_UDP));
346 bcopy(((struct ipovly *)ip)->ih_x1, b, 9);
347 bzero(((struct ipovly *)ip)->ih_x1, 9);
348 ((struct ipovly *)ip)->ih_len = uh->uh_ulen;
349 uh_sum = in_cksum(m, len + sizeof (struct ip));
350 bcopy(b, ((struct ipovly *)ip)->ih_x1, 9);
353 V_udpstat.udps_badsum++;
358 V_udpstat.udps_nosum++;
360 #ifdef IPFIREWALL_FORWARD
362 * Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain.
364 fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL);
365 if (fwd_tag != NULL) {
366 struct sockaddr_in *next_hop;
371 next_hop = (struct sockaddr_in *)(fwd_tag + 1);
372 ip->ip_dst = next_hop->sin_addr;
373 uh->uh_dport = ntohs(next_hop->sin_port);
376 * Remove the tag from the packet. We don't need it anymore.
378 m_tag_delete(m, fwd_tag);
382 INP_INFO_RLOCK(&V_udbinfo);
383 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
384 in_broadcast(ip->ip_dst, ifp)) {
386 struct ip_moptions *imo;
389 LIST_FOREACH(inp, &V_udb, inp_list) {
390 if (inp->inp_lport != uh->uh_dport)
393 if ((inp->inp_vflag & INP_IPV4) == 0)
396 if (inp->inp_laddr.s_addr != INADDR_ANY &&
397 inp->inp_laddr.s_addr != ip->ip_dst.s_addr)
399 if (inp->inp_faddr.s_addr != INADDR_ANY &&
400 inp->inp_faddr.s_addr != ip->ip_src.s_addr)
403 * XXX: Do not check source port of incoming datagram
404 * unless inp_connect() has been called to bind the
405 * fport part of the 4-tuple; the source could be
406 * trying to talk to us with an ephemeral port.
408 if (inp->inp_fport != 0 &&
409 inp->inp_fport != uh->uh_sport)
415 * Handle socket delivery policy for any-source
416 * and source-specific multicast. [RFC3678]
418 imo = inp->inp_moptions;
419 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) &&
421 struct sockaddr_in sin;
422 struct in_msource *ims;
426 bzero(&sin, sizeof(struct sockaddr_in));
427 sin.sin_len = sizeof(struct sockaddr_in);
428 sin.sin_family = AF_INET;
429 sin.sin_addr = ip->ip_dst;
432 idx = imo_match_group(imo, ifp,
433 (struct sockaddr *)&sin);
436 * No group membership for this socket.
437 * Do not bump udps_noportbcast, as
438 * this will happen further down.
443 * Check for a multicast source filter
444 * entry on this socket for this group.
445 * MCAST_EXCLUDE is the default
446 * behaviour. It means default accept;
447 * entries, if present, denote sources
448 * to be excluded from delivery.
450 ims = imo_match_source(imo, idx,
451 (struct sockaddr *)&udp_in);
452 mode = imo->imo_mfilters[idx].imf_fmode;
454 mode == MCAST_EXCLUDE) ||
456 mode == MCAST_INCLUDE)) {
459 printf("%s: blocked by"
464 V_udpstat.udps_filtermcast++;
476 n = m_copy(m, 0, M_COPYALL);
478 udp_append(last, ip, n, iphlen +
479 sizeof(struct udphdr), &udp_in);
484 * Don't look for additional matches if this one does
485 * not have either the SO_REUSEPORT or SO_REUSEADDR
486 * socket options set. This heuristic avoids
487 * searching through all pcbs in the common case of a
488 * non-shared port. It assumes that an application
489 * will never clear these options after setting them.
491 if ((last->inp_socket->so_options &
492 (SO_REUSEPORT|SO_REUSEADDR)) == 0)
498 * No matching pcb found; discard datagram. (No need
499 * to send an ICMP Port Unreachable for a broadcast
500 * or multicast datgram.)
502 V_udpstat.udps_noportbcast++;
505 udp_append(last, ip, m, iphlen + sizeof(struct udphdr),
508 INP_INFO_RUNLOCK(&V_udbinfo);
513 * Locate pcb for datagram.
515 inp = in_pcblookup_hash(&V_udbinfo, ip->ip_src, uh->uh_sport,
516 ip->ip_dst, uh->uh_dport, 1, ifp);
518 if (udp_log_in_vain) {
519 char buf[4*sizeof "123"];
521 strcpy(buf, inet_ntoa(ip->ip_dst));
523 "Connection attempt to UDP %s:%d from %s:%d\n",
524 buf, ntohs(uh->uh_dport), inet_ntoa(ip->ip_src),
525 ntohs(uh->uh_sport));
527 V_udpstat.udps_noport++;
528 if (m->m_flags & (M_BCAST | M_MCAST)) {
529 V_udpstat.udps_noportbcast++;
534 if (badport_bandlim(BANDLIM_ICMP_UNREACH) < 0)
537 ip->ip_len += iphlen;
538 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
539 INP_INFO_RUNLOCK(&V_udbinfo);
544 * Check the minimum TTL for socket.
547 INP_INFO_RUNLOCK(&V_udbinfo);
548 if (inp->inp_ip_minttl && inp->inp_ip_minttl > ip->ip_ttl) {
552 udp_append(inp, ip, m, iphlen + sizeof(struct udphdr), &udp_in);
559 INP_INFO_RUNLOCK(&V_udbinfo);
565 * Notify a udp user of an asynchronous error; just wake up so that they can
566 * collect error status.
569 udp_notify(struct inpcb *inp, int errno)
573 * While udp_ctlinput() always calls udp_notify() with a read lock
574 * when invoking it directly, in_pcbnotifyall() currently uses write
575 * locks due to sharing code with TCP. For now, accept either a read
576 * or a write lock, but a read lock is sufficient.
578 INP_LOCK_ASSERT(inp);
580 inp->inp_socket->so_error = errno;
581 sorwakeup(inp->inp_socket);
582 sowwakeup(inp->inp_socket);
587 udp_ctlinput(int cmd, struct sockaddr *sa, void *vip)
591 struct in_addr faddr;
594 faddr = ((struct sockaddr_in *)sa)->sin_addr;
595 if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY)
599 * Redirects don't need to be handled up here.
601 if (PRC_IS_REDIRECT(cmd))
605 * Hostdead is ugly because it goes linearly through all PCBs.
607 * XXX: We never get this from ICMP, otherwise it makes an excellent
608 * DoS attack on machines with many connections.
610 if (cmd == PRC_HOSTDEAD)
612 else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0)
615 uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
616 INP_INFO_RLOCK(&V_udbinfo);
617 inp = in_pcblookup_hash(&V_udbinfo, faddr, uh->uh_dport,
618 ip->ip_src, uh->uh_sport, 0, NULL);
621 if (inp->inp_socket != NULL) {
622 udp_notify(inp, inetctlerrmap[cmd]);
626 INP_INFO_RUNLOCK(&V_udbinfo);
628 in_pcbnotifyall(&V_udbinfo, faddr, inetctlerrmap[cmd],
633 udp_pcblist(SYSCTL_HANDLER_ARGS)
636 struct inpcb *inp, **inp_list;
641 * The process of preparing the PCB list is too time-consuming and
642 * resource-intensive to repeat twice on every request.
644 if (req->oldptr == 0) {
645 n = V_udbinfo.ipi_count;
646 req->oldidx = 2 * (sizeof xig)
647 + (n + n/8) * sizeof(struct xinpcb);
651 if (req->newptr != 0)
655 * OK, now we're committed to doing something.
657 INP_INFO_RLOCK(&V_udbinfo);
658 gencnt = V_udbinfo.ipi_gencnt;
659 n = V_udbinfo.ipi_count;
660 INP_INFO_RUNLOCK(&V_udbinfo);
662 error = sysctl_wire_old_buffer(req, 2 * (sizeof xig)
663 + n * sizeof(struct xinpcb));
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 inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK);
679 INP_INFO_RLOCK(&V_udbinfo);
680 for (inp = LIST_FIRST(V_udbinfo.ipi_listhead), i = 0; inp && i < n;
681 inp = LIST_NEXT(inp, inp_list)) {
683 if (inp->inp_gencnt <= gencnt &&
684 cr_canseesocket(req->td->td_ucred, inp->inp_socket) == 0)
688 INP_INFO_RUNLOCK(&V_udbinfo);
692 for (i = 0; i < n; i++) {
695 if (inp->inp_gencnt <= gencnt) {
697 bzero(&xi, sizeof(xi));
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 xi.xi_inp.inp_gencnt = inp->inp_gencnt;
705 error = SYSCTL_OUT(req, &xi, sizeof xi);
711 * Give the user an updated idea of our state. If the
712 * generation differs from what we told her before, she knows
713 * that something happened while we were processing this
714 * request, and it might be necessary to retry.
716 INP_INFO_RLOCK(&V_udbinfo);
717 xig.xig_gen = V_udbinfo.ipi_gencnt;
718 xig.xig_sogen = so_gencnt;
719 xig.xig_count = V_udbinfo.ipi_count;
720 INP_INFO_RUNLOCK(&V_udbinfo);
721 error = SYSCTL_OUT(req, &xig, sizeof xig);
723 free(inp_list, M_TEMP);
727 SYSCTL_PROC(_net_inet_udp, UDPCTL_PCBLIST, pcblist, CTLFLAG_RD, 0, 0,
728 udp_pcblist, "S,xinpcb", "List of active UDP sockets");
731 udp_getcred(SYSCTL_HANDLER_ARGS)
734 struct sockaddr_in addrs[2];
738 error = priv_check(req->td, PRIV_NETINET_GETCRED);
741 error = SYSCTL_IN(req, addrs, sizeof(addrs));
744 INP_INFO_RLOCK(&V_udbinfo);
745 inp = in_pcblookup_hash(&V_udbinfo, addrs[1].sin_addr, addrs[1].sin_port,
746 addrs[0].sin_addr, addrs[0].sin_port, 1, NULL);
749 INP_INFO_RUNLOCK(&V_udbinfo);
750 if (inp->inp_socket == NULL)
753 error = cr_canseesocket(req->td->td_ucred,
756 cru2x(inp->inp_socket->so_cred, &xuc);
759 INP_INFO_RUNLOCK(&V_udbinfo);
763 error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
767 SYSCTL_PROC(_net_inet_udp, OID_AUTO, getcred,
768 CTLTYPE_OPAQUE|CTLFLAG_RW|CTLFLAG_PRISON, 0, 0,
769 udp_getcred, "S,xucred", "Get the xucred of a UDP connection");
772 udp_output(struct inpcb *inp, struct mbuf *m, struct sockaddr *addr,
773 struct mbuf *control, struct thread *td)
776 int len = m->m_pkthdr.len;
777 struct in_addr faddr, laddr;
779 struct sockaddr_in *sin, src;
782 u_short fport, lport;
786 * udp_output() may need to temporarily bind or connect the current
787 * inpcb. As such, we don't know up front whether we will need the
788 * pcbinfo lock or not. Do any work to decide what is needed up
789 * front before acquiring any locks.
791 if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) {
799 if (control != NULL) {
801 * XXX: Currently, we assume all the optional information is
802 * stored in a single mbuf.
804 if (control->m_next) {
809 for (; control->m_len > 0;
810 control->m_data += CMSG_ALIGN(cm->cmsg_len),
811 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
812 cm = mtod(control, struct cmsghdr *);
813 if (control->m_len < sizeof(*cm) || cm->cmsg_len == 0
814 || cm->cmsg_len > control->m_len) {
818 if (cm->cmsg_level != IPPROTO_IP)
821 switch (cm->cmsg_type) {
824 CMSG_LEN(sizeof(struct in_addr))) {
828 bzero(&src, sizeof(src));
829 src.sin_family = AF_INET;
830 src.sin_len = sizeof(src);
831 src.sin_port = inp->inp_lport;
833 *(struct in_addr *)CMSG_DATA(cm);
851 * Depending on whether or not the application has bound or connected
852 * the socket, we may have to do varying levels of work. The optimal
853 * case is for a connected UDP socket, as a global lock isn't
856 * In order to decide which we need, we require stability of the
857 * inpcb binding, which we ensure by acquiring a read lock on the
858 * inpcb. This doesn't strictly follow the lock order, so we play
859 * the trylock and retry game; note that we may end up with more
860 * conservative locks than required the second time around, so later
861 * assertions have to accept that. Further analysis of the number of
862 * misses under contention is required.
864 sin = (struct sockaddr_in *)addr;
867 (inp->inp_laddr.s_addr == INADDR_ANY && inp->inp_lport == 0)) {
869 INP_INFO_WLOCK(&V_udbinfo);
872 } else if ((sin != NULL && (
873 (sin->sin_addr.s_addr == INADDR_ANY) ||
874 (sin->sin_addr.s_addr == INADDR_BROADCAST) ||
875 (inp->inp_laddr.s_addr == INADDR_ANY) ||
876 (inp->inp_lport == 0))) ||
877 (src.sin_family == AF_INET)) {
878 if (!INP_INFO_TRY_RLOCK(&V_udbinfo)) {
880 INP_INFO_RLOCK(&V_udbinfo);
888 * If the IP_SENDSRCADDR control message was specified, override the
889 * source address for this datagram. Its use is invalidated if the
890 * address thus specified is incomplete or clobbers other inpcbs.
892 laddr = inp->inp_laddr;
893 lport = inp->inp_lport;
894 if (src.sin_family == AF_INET) {
895 INP_INFO_LOCK_ASSERT(&V_udbinfo);
897 (laddr.s_addr == INADDR_ANY &&
898 src.sin_addr.s_addr == INADDR_ANY)) {
902 error = in_pcbbind_setup(inp, (struct sockaddr *)&src,
903 &laddr.s_addr, &lport, td->td_ucred);
909 * If a UDP socket has been connected, then a local address/port will
910 * have been selected and bound.
912 * If a UDP socket has not been connected to, then an explicit
913 * destination address must be used, in which case a local
914 * address/port may not have been selected and bound.
917 INP_LOCK_ASSERT(inp);
918 if (inp->inp_faddr.s_addr != INADDR_ANY) {
924 * Jail may rewrite the destination address, so let it do
925 * that before we use it.
927 if (jailed(td->td_ucred))
928 prison_remote_ip(td->td_ucred, 0,
929 &sin->sin_addr.s_addr);
932 * If a local address or port hasn't yet been selected, or if
933 * the destination address needs to be rewritten due to using
934 * a special INADDR_ constant, invoke in_pcbconnect_setup()
935 * to do the heavy lifting. Once a port is selected, we
936 * commit the binding back to the socket; we also commit the
937 * binding of the address if in jail.
939 * If we already have a valid binding and we're not
940 * requesting a destination address rewrite, use a fast path.
942 if (inp->inp_laddr.s_addr == INADDR_ANY ||
943 inp->inp_lport == 0 ||
944 sin->sin_addr.s_addr == INADDR_ANY ||
945 sin->sin_addr.s_addr == INADDR_BROADCAST) {
946 INP_INFO_LOCK_ASSERT(&V_udbinfo);
947 error = in_pcbconnect_setup(inp, addr, &laddr.s_addr,
948 &lport, &faddr.s_addr, &fport, NULL,
954 * XXXRW: Why not commit the port if the address is
957 /* Commit the local port if newly assigned. */
958 if (inp->inp_laddr.s_addr == INADDR_ANY &&
959 inp->inp_lport == 0) {
960 INP_INFO_WLOCK_ASSERT(&V_udbinfo);
961 INP_WLOCK_ASSERT(inp);
963 * Remember addr if jailed, to prevent
966 if (jailed(td->td_ucred))
967 inp->inp_laddr = laddr;
968 inp->inp_lport = lport;
969 if (in_pcbinshash(inp) != 0) {
974 inp->inp_flags |= INP_ANONPORT;
977 faddr = sin->sin_addr;
978 fport = sin->sin_port;
981 INP_LOCK_ASSERT(inp);
982 faddr = inp->inp_faddr;
983 fport = inp->inp_fport;
984 if (faddr.s_addr == INADDR_ANY) {
991 * Calculate data length and get a mbuf for UDP, IP, and possible
992 * link-layer headers. Immediate slide the data pointer back forward
993 * since we won't use that space at this layer.
995 M_PREPEND(m, sizeof(struct udpiphdr) + max_linkhdr, M_DONTWAIT);
1000 m->m_data += max_linkhdr;
1001 m->m_len -= max_linkhdr;
1002 m->m_pkthdr.len -= max_linkhdr;
1005 * Fill in mbuf with extended UDP header and addresses and length put
1006 * into network format.
1008 ui = mtod(m, struct udpiphdr *);
1009 bzero(ui->ui_x1, sizeof(ui->ui_x1)); /* XXX still needed? */
1010 ui->ui_pr = IPPROTO_UDP;
1013 ui->ui_sport = lport;
1014 ui->ui_dport = fport;
1015 ui->ui_ulen = htons((u_short)len + sizeof(struct udphdr));
1018 * Set the Don't Fragment bit in the IP header.
1020 if (inp->inp_flags & INP_DONTFRAG) {
1023 ip = (struct ip *)&ui->ui_i;
1024 ip->ip_off |= IP_DF;
1028 if (inp->inp_socket->so_options & SO_DONTROUTE)
1029 ipflags |= IP_ROUTETOIF;
1030 if (inp->inp_socket->so_options & SO_BROADCAST)
1031 ipflags |= IP_ALLOWBROADCAST;
1032 if (inp->inp_flags & INP_ONESBCAST)
1033 ipflags |= IP_SENDONES;
1036 mac_inpcb_create_mbuf(inp, m);
1040 * Set up checksum and output datagram.
1043 if (inp->inp_flags & INP_ONESBCAST)
1044 faddr.s_addr = INADDR_BROADCAST;
1045 ui->ui_sum = in_pseudo(ui->ui_src.s_addr, faddr.s_addr,
1046 htons((u_short)len + sizeof(struct udphdr) + IPPROTO_UDP));
1047 m->m_pkthdr.csum_flags = CSUM_UDP;
1048 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
1051 ((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len;
1052 ((struct ip *)ui)->ip_ttl = inp->inp_ip_ttl; /* XXX */
1053 ((struct ip *)ui)->ip_tos = inp->inp_ip_tos; /* XXX */
1054 V_udpstat.udps_opackets++;
1056 if (unlock_udbinfo == 2)
1057 INP_INFO_WUNLOCK(&V_udbinfo);
1058 else if (unlock_udbinfo == 1)
1059 INP_INFO_RUNLOCK(&V_udbinfo);
1060 error = ip_output(m, inp->inp_options, NULL, ipflags,
1061 inp->inp_moptions, inp);
1062 if (unlock_udbinfo == 2)
1069 if (unlock_udbinfo == 2) {
1071 INP_INFO_WUNLOCK(&V_udbinfo);
1072 } else if (unlock_udbinfo == 1) {
1074 INP_INFO_RUNLOCK(&V_udbinfo);
1082 udp_abort(struct socket *so)
1086 inp = sotoinpcb(so);
1087 KASSERT(inp != NULL, ("udp_abort: inp == NULL"));
1088 INP_INFO_WLOCK(&V_udbinfo);
1090 if (inp->inp_faddr.s_addr != INADDR_ANY) {
1091 in_pcbdisconnect(inp);
1092 inp->inp_laddr.s_addr = INADDR_ANY;
1093 soisdisconnected(so);
1096 INP_INFO_WUNLOCK(&V_udbinfo);
1100 udp_attach(struct socket *so, int proto, struct thread *td)
1105 inp = sotoinpcb(so);
1106 KASSERT(inp == NULL, ("udp_attach: inp != NULL"));
1107 error = soreserve(so, udp_sendspace, udp_recvspace);
1110 INP_INFO_WLOCK(&V_udbinfo);
1111 error = in_pcballoc(so, &V_udbinfo);
1113 INP_INFO_WUNLOCK(&V_udbinfo);
1117 inp = (struct inpcb *)so->so_pcb;
1118 INP_INFO_WUNLOCK(&V_udbinfo);
1119 inp->inp_vflag |= INP_IPV4;
1120 inp->inp_ip_ttl = V_ip_defttl;
1126 udp_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
1131 inp = sotoinpcb(so);
1132 KASSERT(inp != NULL, ("udp_bind: inp == NULL"));
1133 INP_INFO_WLOCK(&V_udbinfo);
1135 error = in_pcbbind(inp, nam, td->td_ucred);
1137 INP_INFO_WUNLOCK(&V_udbinfo);
1142 udp_close(struct socket *so)
1146 inp = sotoinpcb(so);
1147 KASSERT(inp != NULL, ("udp_close: inp == NULL"));
1148 INP_INFO_WLOCK(&V_udbinfo);
1150 if (inp->inp_faddr.s_addr != INADDR_ANY) {
1151 in_pcbdisconnect(inp);
1152 inp->inp_laddr.s_addr = INADDR_ANY;
1153 soisdisconnected(so);
1156 INP_INFO_WUNLOCK(&V_udbinfo);
1160 udp_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
1164 struct sockaddr_in *sin;
1166 inp = sotoinpcb(so);
1167 KASSERT(inp != NULL, ("udp_connect: inp == NULL"));
1168 INP_INFO_WLOCK(&V_udbinfo);
1170 if (inp->inp_faddr.s_addr != INADDR_ANY) {
1172 INP_INFO_WUNLOCK(&V_udbinfo);
1175 sin = (struct sockaddr_in *)nam;
1176 if (jailed(td->td_ucred))
1177 prison_remote_ip(td->td_ucred, 0, &sin->sin_addr.s_addr);
1178 error = in_pcbconnect(inp, nam, td->td_ucred);
1182 INP_INFO_WUNLOCK(&V_udbinfo);
1187 udp_detach(struct socket *so)
1191 inp = sotoinpcb(so);
1192 KASSERT(inp != NULL, ("udp_detach: inp == NULL"));
1193 KASSERT(inp->inp_faddr.s_addr == INADDR_ANY,
1194 ("udp_detach: not disconnected"));
1195 INP_INFO_WLOCK(&V_udbinfo);
1199 INP_INFO_WUNLOCK(&V_udbinfo);
1203 udp_disconnect(struct socket *so)
1207 inp = sotoinpcb(so);
1208 KASSERT(inp != NULL, ("udp_disconnect: inp == NULL"));
1209 INP_INFO_WLOCK(&V_udbinfo);
1211 if (inp->inp_faddr.s_addr == INADDR_ANY) {
1213 INP_INFO_WUNLOCK(&V_udbinfo);
1217 in_pcbdisconnect(inp);
1218 inp->inp_laddr.s_addr = INADDR_ANY;
1220 so->so_state &= ~SS_ISCONNECTED; /* XXX */
1223 INP_INFO_WUNLOCK(&V_udbinfo);
1228 udp_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
1229 struct mbuf *control, struct thread *td)
1233 inp = sotoinpcb(so);
1234 KASSERT(inp != NULL, ("udp_send: inp == NULL"));
1235 return (udp_output(inp, m, addr, control, td));
1239 udp_shutdown(struct socket *so)
1243 inp = sotoinpcb(so);
1244 KASSERT(inp != NULL, ("udp_shutdown: inp == NULL"));
1251 struct pr_usrreqs udp_usrreqs = {
1252 .pru_abort = udp_abort,
1253 .pru_attach = udp_attach,
1254 .pru_bind = udp_bind,
1255 .pru_connect = udp_connect,
1256 .pru_control = in_control,
1257 .pru_detach = udp_detach,
1258 .pru_disconnect = udp_disconnect,
1259 .pru_peeraddr = in_getpeeraddr,
1260 .pru_send = udp_send,
1261 .pru_soreceive = soreceive_dgram,
1262 .pru_sosend = sosend_dgram,
1263 .pru_shutdown = udp_shutdown,
1264 .pru_sockaddr = in_getsockaddr,
1265 .pru_sosetlabel = in_pcbsosetlabel,
1266 .pru_close = udp_close,