2 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
3 * The Regents of the University of California.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 4. Neither the name of the University nor the names of its contributors
15 * may be used to endorse or promote products derived from this software
16 * without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * @(#)udp_usrreq.c 8.6 (Berkeley) 5/23/95
35 #include "opt_ipsec.h"
36 #include "opt_inet6.h"
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/domain.h>
42 #include <sys/eventhandler.h>
44 #include <sys/kernel.h>
46 #include <sys/malloc.h>
50 #include <sys/protosw.h>
51 #include <sys/signalvar.h>
52 #include <sys/socket.h>
53 #include <sys/socketvar.h>
55 #include <sys/sysctl.h>
56 #include <sys/syslog.h>
61 #include <net/route.h>
63 #include <netinet/in.h>
64 #include <netinet/in_systm.h>
65 #include <netinet/in_pcb.h>
66 #include <netinet/in_var.h>
67 #include <netinet/ip.h>
69 #include <netinet/ip6.h>
71 #include <netinet/ip_icmp.h>
72 #include <netinet/icmp_var.h>
73 #include <netinet/ip_var.h>
74 #include <netinet/ip_options.h>
76 #include <netinet6/ip6_var.h>
78 #include <netinet/udp.h>
79 #include <netinet/udp_var.h>
82 #include <netipsec/ipsec.h>
86 #include <netinet6/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 by bad checksums may cause
102 * problems (especially for NFS data blocks).
104 static int udpcksum = 1;
105 SYSCTL_INT(_net_inet_udp, UDPCTL_CHECKSUM, checksum, CTLFLAG_RW, &udpcksum,
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 static int blackhole = 0;
113 SYSCTL_INT(_net_inet_udp, OID_AUTO, blackhole, CTLFLAG_RW, &blackhole, 0,
114 "Do not send port unreachables for refused connects");
116 static int strict_mcast_mship = 0;
117 SYSCTL_INT(_net_inet_udp, OID_AUTO, strict_mcast_mship, CTLFLAG_RW,
118 &strict_mcast_mship, 0, "Only send multicast to member sockets");
120 struct inpcbhead udb; /* from udp_var.h */
121 struct inpcbinfo udbinfo;
124 #define UDBHASHSIZE 16
127 struct udpstat udpstat; /* from udp_var.h */
128 SYSCTL_STRUCT(_net_inet_udp, UDPCTL_STATS, stats, CTLFLAG_RW, &udpstat,
129 udpstat, "UDP statistics (struct udpstat, netinet/udp_var.h)");
131 static void udp_append(struct inpcb *last, struct ip *ip, struct mbuf *n,
132 int off, struct sockaddr_in *udp_in);
134 static void udp_detach(struct socket *so);
135 static int udp_output(struct inpcb *, struct mbuf *, struct sockaddr *,
136 struct mbuf *, struct thread *);
139 udp_zone_change(void *tag)
142 uma_zone_set_max(udbinfo.ipi_zone, maxsockets);
146 udp_inpcb_init(void *mem, int size, int flags)
148 struct inpcb *inp = mem;
150 INP_LOCK_INIT(inp, "inp", "udpinp");
157 INP_INFO_LOCK_INIT(&udbinfo, "udp");
159 udbinfo.listhead = &udb;
160 udbinfo.hashbase = hashinit(UDBHASHSIZE, M_PCB, &udbinfo.hashmask);
161 udbinfo.porthashbase = hashinit(UDBHASHSIZE, M_PCB,
162 &udbinfo.porthashmask);
163 udbinfo.ipi_zone = uma_zcreate("udpcb", sizeof(struct inpcb), NULL,
164 NULL, udp_inpcb_init, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
165 uma_zone_set_max(udbinfo.ipi_zone, maxsockets);
166 EVENTHANDLER_REGISTER(maxsockets_change, udp_zone_change, NULL,
167 EVENTHANDLER_PRI_ANY);
171 udp_input(struct mbuf *m, int off)
179 struct sockaddr_in udp_in;
180 #ifdef IPFIREWALL_FORWARD
181 struct m_tag *fwd_tag;
184 udpstat.udps_ipackets++;
187 * Strip IP options, if any; should skip this, make available to
188 * user, and use on returned packets, but we don't yet have a way to
189 * check the checksum with options still present.
191 if (iphlen > sizeof (struct ip)) {
192 ip_stripoptions(m, (struct mbuf *)0);
193 iphlen = sizeof(struct ip);
197 * Get IP and UDP header together in first mbuf.
199 ip = mtod(m, struct ip *);
200 if (m->m_len < iphlen + sizeof(struct udphdr)) {
201 if ((m = m_pullup(m, iphlen + sizeof(struct udphdr))) == 0) {
202 udpstat.udps_hdrops++;
205 ip = mtod(m, struct ip *);
207 uh = (struct udphdr *)((caddr_t)ip + iphlen);
210 * Destination port of 0 is illegal, based on RFC768.
212 if (uh->uh_dport == 0)
216 * Construct sockaddr format source address. Stuff source address
217 * and datagram in user buffer.
219 bzero(&udp_in, sizeof(udp_in));
220 udp_in.sin_len = sizeof(udp_in);
221 udp_in.sin_family = AF_INET;
222 udp_in.sin_port = uh->uh_sport;
223 udp_in.sin_addr = ip->ip_src;
226 * Make mbuf data length reflect UDP length.
227 * If not enough data to reflect UDP length, drop.
229 len = ntohs((u_short)uh->uh_ulen);
230 if (ip->ip_len != len) {
231 if (len > ip->ip_len || len < sizeof(struct udphdr)) {
232 udpstat.udps_badlen++;
235 m_adj(m, len - ip->ip_len);
236 /* ip->ip_len = len; */
240 * Save a copy of the IP header in case we want restore it for
241 * sending an ICMP error message in response.
247 * Checksum extended UDP header and data.
250 if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
251 if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
252 uh->uh_sum = m->m_pkthdr.csum_data;
254 uh->uh_sum = in_pseudo(ip->ip_src.s_addr,
255 ip->ip_dst.s_addr, htonl((u_short)len +
256 m->m_pkthdr.csum_data + IPPROTO_UDP));
257 uh->uh_sum ^= 0xffff;
260 bcopy(((struct ipovly *)ip)->ih_x1, b, 9);
261 bzero(((struct ipovly *)ip)->ih_x1, 9);
262 ((struct ipovly *)ip)->ih_len = uh->uh_ulen;
263 uh->uh_sum = in_cksum(m, len + sizeof (struct ip));
264 bcopy(b, ((struct ipovly *)ip)->ih_x1, 9);
267 udpstat.udps_badsum++;
272 udpstat.udps_nosum++;
274 #ifdef IPFIREWALL_FORWARD
276 * Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain.
278 fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL);
279 if (fwd_tag != NULL) {
280 struct sockaddr_in *next_hop;
285 next_hop = (struct sockaddr_in *)(fwd_tag + 1);
286 ip->ip_dst = next_hop->sin_addr;
287 uh->uh_dport = ntohs(next_hop->sin_port);
290 * Remove the tag from the packet. We don't need it anymore.
292 m_tag_delete(m, fwd_tag);
296 INP_INFO_RLOCK(&udbinfo);
298 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
299 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 one
306 * 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 a
310 * 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
313 * an inadequacy of the UDP socket interface, but for
314 * backwards compatibility we avoid the problem here rather
315 * than fixing the interface. Maybe 4.5BSD will remedy
319 LIST_FOREACH(inp, &udb, inp_list) {
320 if (inp->inp_lport != uh->uh_dport)
323 if ((inp->inp_vflag & INP_IPV4) == 0)
326 if (inp->inp_laddr.s_addr != INADDR_ANY) {
327 if (inp->inp_laddr.s_addr != ip->ip_dst.s_addr)
330 if (inp->inp_faddr.s_addr != INADDR_ANY) {
331 if (inp->inp_faddr.s_addr !=
333 inp->inp_fport != uh->uh_sport)
338 * Check multicast packets to make sure they are only
339 * sent to sockets with multicast memberships for the
340 * packet's destination address and arrival interface
342 #define MSHIP(_inp, n) ((_inp)->inp_moptions->imo_membership[(n)])
343 #define NMSHIPS(_inp) ((_inp)->inp_moptions->imo_num_memberships)
345 if (strict_mcast_mship && inp->inp_moptions != NULL) {
346 int mship, foundmship = 0;
348 for (mship = 0; mship < NMSHIPS(inp);
350 if (MSHIP(inp, mship)->inm_addr.s_addr
351 == ip->ip_dst.s_addr &&
352 MSHIP(inp, mship)->inm_ifp
353 == m->m_pkthdr.rcvif) {
358 if (foundmship == 0) {
368 n = m_copy(m, 0, M_COPYALL);
370 udp_append(last, ip, n, iphlen +
371 sizeof(struct udphdr), &udp_in);
376 * Don't look for additional matches if this one does
377 * not have either the SO_REUSEPORT or SO_REUSEADDR
378 * socket options set. This heuristic avoids
379 * searching through all pcbs in the common case of a
380 * non-shared port. It assumes that an application
381 * will never clear these options after setting them.
383 if ((last->inp_socket->so_options &
384 (SO_REUSEPORT|SO_REUSEADDR)) == 0)
390 * No matching pcb found; discard datagram. (No need
391 * to send an ICMP Port Unreachable for a broadcast
392 * or multicast datgram.)
394 udpstat.udps_noportbcast++;
397 udp_append(last, ip, m, iphlen + sizeof(struct udphdr),
400 INP_INFO_RUNLOCK(&udbinfo);
405 * Locate pcb for datagram.
407 inp = in_pcblookup_hash(&udbinfo, ip->ip_src, uh->uh_sport,
408 ip->ip_dst, uh->uh_dport, 1, m->m_pkthdr.rcvif);
410 if (udp_log_in_vain) {
411 char buf[4*sizeof "123"];
413 strcpy(buf, inet_ntoa(ip->ip_dst));
415 "Connection attempt to UDP %s:%d from %s:%d\n",
416 buf, ntohs(uh->uh_dport), inet_ntoa(ip->ip_src),
417 ntohs(uh->uh_sport));
419 udpstat.udps_noport++;
420 if (m->m_flags & (M_BCAST | M_MCAST)) {
421 udpstat.udps_noportbcast++;
426 if (badport_bandlim(BANDLIM_ICMP_UNREACH) < 0)
429 ip->ip_len += iphlen;
430 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
431 INP_INFO_RUNLOCK(&udbinfo);
436 * Check the minimum TTL for socket.
439 if (inp->inp_ip_minttl && inp->inp_ip_minttl > ip->ip_ttl)
441 udp_append(inp, ip, m, iphlen + sizeof(struct udphdr), &udp_in);
443 INP_INFO_RUNLOCK(&udbinfo);
449 INP_INFO_RUNLOCK(&udbinfo);
455 * Subroutine of udp_input(), which appends the provided mbuf chain to the
456 * passed pcb/socket. The caller must provide a sockaddr_in via udp_in that
457 * contains the source address. If the socket ends up being an IPv6 socket,
458 * udp_append() will convert to a sockaddr_in6 before passing the address
459 * into the socket code.
462 udp_append(struct inpcb *inp, struct ip *ip, struct mbuf *n, int off,
463 struct sockaddr_in *udp_in)
465 struct sockaddr *append_sa;
467 struct mbuf *opts = 0;
469 struct sockaddr_in6 udp_in6;
472 INP_LOCK_ASSERT(inp);
474 #if defined(IPSEC) || defined(FAST_IPSEC)
475 /* check AH/ESP integrity. */
476 if (ipsec4_in_reject(n, inp)) {
478 ipsecstat.in_polvio++;
483 #endif /*IPSEC || FAST_IPSEC*/
485 if (mac_check_inpcb_deliver(inp, n) != 0) {
490 if (inp->inp_flags & INP_CONTROLOPTS ||
491 inp->inp_socket->so_options & (SO_TIMESTAMP | SO_BINTIME)) {
493 if (inp->inp_vflag & INP_IPV6) {
496 savedflags = inp->inp_flags;
497 inp->inp_flags &= ~INP_UNMAPPABLEOPTS;
498 ip6_savecontrol(inp, n, &opts);
499 inp->inp_flags = savedflags;
502 ip_savecontrol(inp, &opts, ip, n);
505 if (inp->inp_vflag & INP_IPV6) {
506 bzero(&udp_in6, sizeof(udp_in6));
507 udp_in6.sin6_len = sizeof(udp_in6);
508 udp_in6.sin6_family = AF_INET6;
509 in6_sin_2_v4mapsin6(udp_in, &udp_in6);
510 append_sa = (struct sockaddr *)&udp_in6;
513 append_sa = (struct sockaddr *)udp_in;
516 so = inp->inp_socket;
517 SOCKBUF_LOCK(&so->so_rcv);
518 if (sbappendaddr_locked(&so->so_rcv, append_sa, n, opts) == 0) {
522 udpstat.udps_fullsock++;
523 SOCKBUF_UNLOCK(&so->so_rcv);
525 sorwakeup_locked(so);
529 * Notify a udp user of an asynchronous error; just wake up so that they can
530 * collect error status.
533 udp_notify(struct inpcb *inp, int errno)
536 inp->inp_socket->so_error = errno;
537 sorwakeup(inp->inp_socket);
538 sowwakeup(inp->inp_socket);
543 udp_ctlinput(int cmd, struct sockaddr *sa, void *vip)
547 struct inpcb *(*notify)(struct inpcb *, int) = udp_notify;
548 struct in_addr faddr;
551 faddr = ((struct sockaddr_in *)sa)->sin_addr;
552 if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY)
556 * Redirects don't need to be handled up here.
558 if (PRC_IS_REDIRECT(cmd))
562 * Hostdead is ugly because it goes linearly through all PCBs.
564 * XXX: We never get this from ICMP, otherwise it makes an excellent
565 * DoS attack on machines with many connections.
567 if (cmd == PRC_HOSTDEAD)
569 else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0)
572 uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
573 INP_INFO_RLOCK(&udbinfo);
574 inp = in_pcblookup_hash(&udbinfo, faddr, uh->uh_dport,
575 ip->ip_src, uh->uh_sport, 0, NULL);
578 if (inp->inp_socket != NULL) {
579 (*notify)(inp, inetctlerrmap[cmd]);
583 INP_INFO_RUNLOCK(&udbinfo);
585 in_pcbnotifyall(&udbinfo, faddr, inetctlerrmap[cmd], notify);
589 udp_pcblist(SYSCTL_HANDLER_ARGS)
592 struct inpcb *inp, **inp_list;
597 * The process of preparing the TCB list is too time-consuming and
598 * resource-intensive to repeat twice on every request.
600 if (req->oldptr == 0) {
601 n = udbinfo.ipi_count;
602 req->oldidx = 2 * (sizeof xig)
603 + (n + n/8) * sizeof(struct xinpcb);
607 if (req->newptr != 0)
611 * OK, now we're committed to doing something.
613 INP_INFO_RLOCK(&udbinfo);
614 gencnt = udbinfo.ipi_gencnt;
615 n = udbinfo.ipi_count;
616 INP_INFO_RUNLOCK(&udbinfo);
618 error = sysctl_wire_old_buffer(req, 2 * (sizeof xig)
619 + n * sizeof(struct xinpcb));
623 xig.xig_len = sizeof xig;
625 xig.xig_gen = gencnt;
626 xig.xig_sogen = so_gencnt;
627 error = SYSCTL_OUT(req, &xig, sizeof xig);
631 inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK);
635 INP_INFO_RLOCK(&udbinfo);
636 for (inp = LIST_FIRST(udbinfo.listhead), i = 0; inp && i < n;
637 inp = LIST_NEXT(inp, inp_list)) {
639 if (inp->inp_gencnt <= gencnt &&
640 cr_canseesocket(req->td->td_ucred, inp->inp_socket) == 0)
644 INP_INFO_RUNLOCK(&udbinfo);
648 for (i = 0; i < n; i++) {
651 if (inp->inp_gencnt <= gencnt) {
653 bzero(&xi, sizeof(xi));
654 xi.xi_len = sizeof xi;
655 /* XXX should avoid extra copy */
656 bcopy(inp, &xi.xi_inp, sizeof *inp);
658 sotoxsocket(inp->inp_socket, &xi.xi_socket);
659 xi.xi_inp.inp_gencnt = inp->inp_gencnt;
661 error = SYSCTL_OUT(req, &xi, sizeof xi);
667 * Give the user an updated idea of our state. If the
668 * generation differs from what we told her before, she knows
669 * that something happened while we were processing this
670 * request, and it might be necessary to retry.
672 INP_INFO_RLOCK(&udbinfo);
673 xig.xig_gen = udbinfo.ipi_gencnt;
674 xig.xig_sogen = so_gencnt;
675 xig.xig_count = udbinfo.ipi_count;
676 INP_INFO_RUNLOCK(&udbinfo);
677 error = SYSCTL_OUT(req, &xig, sizeof xig);
679 free(inp_list, M_TEMP);
683 SYSCTL_PROC(_net_inet_udp, UDPCTL_PCBLIST, pcblist, CTLFLAG_RD, 0, 0,
684 udp_pcblist, "S,xinpcb", "List of active UDP sockets");
687 udp_getcred(SYSCTL_HANDLER_ARGS)
690 struct sockaddr_in addrs[2];
694 error = priv_check_cred(req->td->td_ucred, PRIV_NETINET_GETCRED,
698 error = SYSCTL_IN(req, addrs, sizeof(addrs));
701 INP_INFO_RLOCK(&udbinfo);
702 inp = in_pcblookup_hash(&udbinfo, addrs[1].sin_addr, addrs[1].sin_port,
703 addrs[0].sin_addr, addrs[0].sin_port, 1, NULL);
704 if (inp == NULL || inp->inp_socket == NULL) {
708 error = cr_canseesocket(req->td->td_ucred, inp->inp_socket);
711 cru2x(inp->inp_socket->so_cred, &xuc);
713 INP_INFO_RUNLOCK(&udbinfo);
715 error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
719 SYSCTL_PROC(_net_inet_udp, OID_AUTO, getcred,
720 CTLTYPE_OPAQUE|CTLFLAG_RW|CTLFLAG_PRISON, 0, 0,
721 udp_getcred, "S,xucred", "Get the xucred of a UDP connection");
724 udp_output(struct inpcb *inp, struct mbuf *m, struct sockaddr *addr,
725 struct mbuf *control, struct thread *td)
728 int len = m->m_pkthdr.len;
729 struct in_addr faddr, laddr;
731 struct sockaddr_in *sin, src;
734 u_short fport, lport;
738 * udp_output() may need to temporarily bind or connect the current
739 * inpcb. As such, we don't know up front what inpcb locks we will
740 * need. Do any work to decide what is needed up front before
743 if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) {
751 if (control != NULL) {
753 * XXX: Currently, we assume all the optional information is
754 * stored in a single mbuf.
756 if (control->m_next) {
761 for (; control->m_len > 0;
762 control->m_data += CMSG_ALIGN(cm->cmsg_len),
763 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
764 cm = mtod(control, struct cmsghdr *);
765 if (control->m_len < sizeof(*cm) || cm->cmsg_len == 0 ||
766 cm->cmsg_len > control->m_len) {
770 if (cm->cmsg_level != IPPROTO_IP)
773 switch (cm->cmsg_type) {
776 CMSG_LEN(sizeof(struct in_addr))) {
780 bzero(&src, sizeof(src));
781 src.sin_family = AF_INET;
782 src.sin_len = sizeof(src);
783 src.sin_port = inp->inp_lport;
784 src.sin_addr = *(struct in_addr *)CMSG_DATA(cm);
800 if (src.sin_family == AF_INET || addr != NULL) {
801 INP_INFO_WLOCK(&udbinfo);
808 mac_create_mbuf_from_inpcb(inp, m);
812 * If the IP_SENDSRCADDR control message was specified, override the
813 * source address for this datagram. Its use is invalidated if the
814 * address thus specified is incomplete or clobbers other inpcbs.
816 laddr = inp->inp_laddr;
817 lport = inp->inp_lport;
818 if (src.sin_family == AF_INET) {
820 (laddr.s_addr == INADDR_ANY &&
821 src.sin_addr.s_addr == INADDR_ANY)) {
825 error = in_pcbbind_setup(inp, (struct sockaddr *)&src,
826 &laddr.s_addr, &lport, td->td_ucred);
832 sin = (struct sockaddr_in *)addr;
833 if (jailed(td->td_ucred))
834 prison_remote_ip(td->td_ucred, 0,
835 &sin->sin_addr.s_addr);
836 if (inp->inp_faddr.s_addr != INADDR_ANY) {
840 error = in_pcbconnect_setup(inp, addr, &laddr.s_addr, &lport,
841 &faddr.s_addr, &fport, NULL, td->td_ucred);
845 /* Commit the local port if newly assigned. */
846 if (inp->inp_laddr.s_addr == INADDR_ANY &&
847 inp->inp_lport == 0) {
849 * Remember addr if jailed, to prevent rebinding.
851 if (jailed(td->td_ucred))
852 inp->inp_laddr = laddr;
853 inp->inp_lport = lport;
854 if (in_pcbinshash(inp) != 0) {
859 inp->inp_flags |= INP_ANONPORT;
862 faddr = inp->inp_faddr;
863 fport = inp->inp_fport;
864 if (faddr.s_addr == INADDR_ANY) {
871 * Calculate data length and get a mbuf for UDP, IP, and possible
872 * link-layer headers. Immediate slide the data pointer back forward
873 * since we won't use that space at this layer.
875 M_PREPEND(m, sizeof(struct udpiphdr) + max_linkhdr, M_DONTWAIT);
880 m->m_data += max_linkhdr;
881 m->m_len -= max_linkhdr;
882 m->m_pkthdr.len -= max_linkhdr;
885 * Fill in mbuf with extended UDP header and addresses and length put
886 * into network format.
888 ui = mtod(m, struct udpiphdr *);
889 bzero(ui->ui_x1, sizeof(ui->ui_x1)); /* XXX still needed? */
890 ui->ui_pr = IPPROTO_UDP;
893 ui->ui_sport = lport;
894 ui->ui_dport = fport;
895 ui->ui_ulen = htons((u_short)len + sizeof(struct udphdr));
898 * Set the Don't Fragment bit in the IP header.
900 if (inp->inp_flags & INP_DONTFRAG) {
903 ip = (struct ip *)&ui->ui_i;
908 if (inp->inp_socket->so_options & SO_DONTROUTE)
909 ipflags |= IP_ROUTETOIF;
910 if (inp->inp_socket->so_options & SO_BROADCAST)
911 ipflags |= IP_ALLOWBROADCAST;
912 if (inp->inp_flags & INP_ONESBCAST)
913 ipflags |= IP_SENDONES;
916 * Set up checksum and output datagram.
919 if (inp->inp_flags & INP_ONESBCAST)
920 faddr.s_addr = INADDR_BROADCAST;
921 ui->ui_sum = in_pseudo(ui->ui_src.s_addr, faddr.s_addr,
922 htons((u_short)len + sizeof(struct udphdr) + IPPROTO_UDP));
923 m->m_pkthdr.csum_flags = CSUM_UDP;
924 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
927 ((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len;
928 ((struct ip *)ui)->ip_ttl = inp->inp_ip_ttl; /* XXX */
929 ((struct ip *)ui)->ip_tos = inp->inp_ip_tos; /* XXX */
930 udpstat.udps_opackets++;
933 INP_INFO_WUNLOCK(&udbinfo);
934 error = ip_output(m, inp->inp_options, NULL, ipflags,
935 inp->inp_moptions, inp);
942 INP_INFO_WUNLOCK(&udbinfo);
947 u_long udp_sendspace = 9216; /* really max datagram size */
948 /* 40 1K datagrams */
949 SYSCTL_ULONG(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram, CTLFLAG_RW,
950 &udp_sendspace, 0, "Maximum outgoing UDP datagram size");
952 u_long udp_recvspace = 40 * (1024 +
954 sizeof(struct sockaddr_in6)
956 sizeof(struct sockaddr_in)
959 SYSCTL_ULONG(_net_inet_udp, UDPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
960 &udp_recvspace, 0, "Maximum space for incoming UDP datagrams");
963 udp_abort(struct socket *so)
968 KASSERT(inp != NULL, ("udp_abort: inp == NULL"));
969 INP_INFO_WLOCK(&udbinfo);
971 if (inp->inp_faddr.s_addr != INADDR_ANY) {
972 in_pcbdisconnect(inp);
973 inp->inp_laddr.s_addr = INADDR_ANY;
974 soisdisconnected(so);
977 INP_INFO_WUNLOCK(&udbinfo);
981 udp_attach(struct socket *so, int proto, struct thread *td)
987 KASSERT(inp == NULL, ("udp_attach: inp != NULL"));
988 error = soreserve(so, udp_sendspace, udp_recvspace);
991 INP_INFO_WLOCK(&udbinfo);
992 error = in_pcballoc(so, &udbinfo);
994 INP_INFO_WUNLOCK(&udbinfo);
998 inp = (struct inpcb *)so->so_pcb;
999 INP_INFO_WUNLOCK(&udbinfo);
1000 inp->inp_vflag |= INP_IPV4;
1001 inp->inp_ip_ttl = ip_defttl;
1007 udp_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
1012 inp = sotoinpcb(so);
1013 KASSERT(inp != NULL, ("udp_bind: inp == NULL"));
1014 INP_INFO_WLOCK(&udbinfo);
1016 error = in_pcbbind(inp, nam, td->td_ucred);
1018 INP_INFO_WUNLOCK(&udbinfo);
1023 udp_close(struct socket *so)
1027 inp = sotoinpcb(so);
1028 KASSERT(inp != NULL, ("udp_close: inp == NULL"));
1029 INP_INFO_WLOCK(&udbinfo);
1031 if (inp->inp_faddr.s_addr != INADDR_ANY) {
1032 in_pcbdisconnect(inp);
1033 inp->inp_laddr.s_addr = INADDR_ANY;
1034 soisdisconnected(so);
1037 INP_INFO_WUNLOCK(&udbinfo);
1041 udp_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
1045 struct sockaddr_in *sin;
1047 inp = sotoinpcb(so);
1048 KASSERT(inp != NULL, ("udp_connect: inp == NULL"));
1049 INP_INFO_WLOCK(&udbinfo);
1051 if (inp->inp_faddr.s_addr != INADDR_ANY) {
1053 INP_INFO_WUNLOCK(&udbinfo);
1056 sin = (struct sockaddr_in *)nam;
1057 if (jailed(td->td_ucred))
1058 prison_remote_ip(td->td_ucred, 0, &sin->sin_addr.s_addr);
1059 error = in_pcbconnect(inp, nam, td->td_ucred);
1063 INP_INFO_WUNLOCK(&udbinfo);
1068 udp_detach(struct socket *so)
1072 inp = sotoinpcb(so);
1073 KASSERT(inp != NULL, ("udp_detach: inp == NULL"));
1074 KASSERT(inp->inp_faddr.s_addr == INADDR_ANY,
1075 ("udp_detach: not disconnected"));
1076 INP_INFO_WLOCK(&udbinfo);
1080 INP_INFO_WUNLOCK(&udbinfo);
1084 udp_disconnect(struct socket *so)
1088 inp = sotoinpcb(so);
1089 KASSERT(inp != NULL, ("udp_disconnect: inp == NULL"));
1090 INP_INFO_WLOCK(&udbinfo);
1092 if (inp->inp_faddr.s_addr == INADDR_ANY) {
1093 INP_INFO_WUNLOCK(&udbinfo);
1098 in_pcbdisconnect(inp);
1099 inp->inp_laddr.s_addr = INADDR_ANY;
1101 so->so_state &= ~SS_ISCONNECTED; /* XXX */
1104 INP_INFO_WUNLOCK(&udbinfo);
1109 udp_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
1110 struct mbuf *control, struct thread *td)
1114 inp = sotoinpcb(so);
1115 KASSERT(inp != NULL, ("udp_send: inp == NULL"));
1116 return (udp_output(inp, m, addr, control, td));
1120 udp_shutdown(struct socket *so)
1124 inp = sotoinpcb(so);
1125 KASSERT(inp != NULL, ("udp_shutdown: inp == NULL"));
1133 * This is the wrapper function for in_setsockaddr. We just pass down the
1134 * pcbinfo for in_setsockaddr() to lock. We don't want to do the locking
1135 * here because in_setsockaddr() will call malloc and might block.
1138 udp_sockaddr(struct socket *so, struct sockaddr **nam)
1141 return (in_setsockaddr(so, nam, &udbinfo));
1145 * This is the wrapper function for in_setpeeraddr(). We just pass down the
1146 * pcbinfo for in_setpeeraddr() to lock.
1149 udp_peeraddr(struct socket *so, struct sockaddr **nam)
1152 return (in_setpeeraddr(so, nam, &udbinfo));
1155 struct pr_usrreqs udp_usrreqs = {
1156 .pru_abort = udp_abort,
1157 .pru_attach = udp_attach,
1158 .pru_bind = udp_bind,
1159 .pru_connect = udp_connect,
1160 .pru_control = in_control,
1161 .pru_detach = udp_detach,
1162 .pru_disconnect = udp_disconnect,
1163 .pru_peeraddr = udp_peeraddr,
1164 .pru_send = udp_send,
1165 .pru_sosend = sosend_dgram,
1166 .pru_shutdown = udp_shutdown,
1167 .pru_sockaddr = udp_sockaddr,
1168 .pru_sosetlabel = in_pcbsosetlabel,
1169 .pru_close = udp_close,