2 * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)udp_usrreq.c 8.6 (Berkeley) 5/23/95
34 #include "opt_ipsec.h"
35 #include "opt_inet6.h"
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/domain.h>
41 #include <sys/eventhandler.h>
43 #include <sys/kernel.h>
45 #include <sys/malloc.h>
49 #include <sys/protosw.h>
50 #include <sys/signalvar.h>
51 #include <sys/socket.h>
52 #include <sys/socketvar.h>
54 #include <sys/sysctl.h>
55 #include <sys/syslog.h>
60 #include <net/route.h>
62 #include <netinet/in.h>
63 #include <netinet/in_systm.h>
64 #include <netinet/in_pcb.h>
65 #include <netinet/in_var.h>
66 #include <netinet/ip.h>
68 #include <netinet/ip6.h>
70 #include <netinet/ip_icmp.h>
71 #include <netinet/icmp_var.h>
72 #include <netinet/ip_var.h>
73 #include <netinet/ip_options.h>
75 #include <netinet6/ip6_var.h>
77 #include <netinet/udp.h>
78 #include <netinet/udp_var.h>
81 #include <netipsec/ipsec.h>
85 #include <netinet6/ipsec.h>
88 #include <machine/in_cksum.h>
90 #include <security/mac/mac_framework.h>
93 * UDP protocol implementation.
94 * Per RFC 768, August, 1980.
98 * BSD 4.2 defaulted the udp checksum to be off. Turning off udp
99 * checksums removes the only data integrety mechanism for packets and
100 * malformed packets that would otherwise be discarded by bad checksums
101 * may cause problems (especially for NFS data blocks).
104 static int udpcksum = 1;
106 static int udpcksum = 0;
108 SYSCTL_INT(_net_inet_udp, UDPCTL_CHECKSUM, checksum, CTLFLAG_RW,
112 SYSCTL_INT(_net_inet_udp, OID_AUTO, log_in_vain, CTLFLAG_RW,
113 &log_in_vain, 0, "Log all incoming UDP packets");
115 static int blackhole = 0;
116 SYSCTL_INT(_net_inet_udp, OID_AUTO, blackhole, CTLFLAG_RW,
117 &blackhole, 0, "Do not send port unreachables for refused connects");
119 static int strict_mcast_mship = 0;
120 SYSCTL_INT(_net_inet_udp, OID_AUTO, strict_mcast_mship, CTLFLAG_RW,
121 &strict_mcast_mship, 0, "Only send multicast to member sockets");
123 struct inpcbhead udb; /* from udp_var.h */
124 #define udb6 udb /* for KAME src sync over BSD*'s */
125 struct inpcbinfo udbinfo;
128 #define UDBHASHSIZE 16
131 struct udpstat udpstat; /* from udp_var.h */
132 SYSCTL_STRUCT(_net_inet_udp, UDPCTL_STATS, stats, CTLFLAG_RW,
133 &udpstat, udpstat, "UDP statistics (struct udpstat, netinet/udp_var.h)");
135 static void udp_append(struct inpcb *last, struct ip *ip, struct mbuf *n,
136 int off, struct sockaddr_in *udp_in);
138 static void udp_detach(struct socket *so);
139 static int udp_output(struct inpcb *, struct mbuf *, struct sockaddr *,
140 struct mbuf *, struct thread *);
143 udp_zone_change(void *tag)
146 uma_zone_set_max(udbinfo.ipi_zone, maxsockets);
150 udp_inpcb_init(void *mem, int size, int flags)
152 struct inpcb *inp = mem;
154 INP_LOCK_INIT(inp, "inp", "udpinp");
161 INP_INFO_LOCK_INIT(&udbinfo, "udp");
163 udbinfo.listhead = &udb;
164 udbinfo.hashbase = hashinit(UDBHASHSIZE, M_PCB, &udbinfo.hashmask);
165 udbinfo.porthashbase = hashinit(UDBHASHSIZE, M_PCB,
166 &udbinfo.porthashmask);
167 udbinfo.ipi_zone = uma_zcreate("udpcb", sizeof(struct inpcb), NULL,
168 NULL, udp_inpcb_init, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
169 uma_zone_set_max(udbinfo.ipi_zone, maxsockets);
170 EVENTHANDLER_REGISTER(maxsockets_change, udp_zone_change, NULL,
171 EVENTHANDLER_PRI_ANY);
176 register struct mbuf *m;
180 register struct ip *ip;
181 register struct udphdr *uh;
182 register struct inpcb *inp;
185 struct sockaddr_in udp_in;
186 #ifdef IPFIREWALL_FORWARD
187 struct m_tag *fwd_tag;
190 udpstat.udps_ipackets++;
193 * Strip IP options, if any; should skip this,
194 * make available to user, and use on returned packets,
195 * but we don't yet have a way to check the checksum
196 * with options still present.
198 if (iphlen > sizeof (struct ip)) {
199 ip_stripoptions(m, (struct mbuf *)0);
200 iphlen = sizeof(struct ip);
204 * Get IP and UDP header together in first mbuf.
206 ip = mtod(m, struct ip *);
207 if (m->m_len < iphlen + sizeof(struct udphdr)) {
208 if ((m = m_pullup(m, iphlen + sizeof(struct udphdr))) == 0) {
209 udpstat.udps_hdrops++;
212 ip = mtod(m, struct ip *);
214 uh = (struct udphdr *)((caddr_t)ip + iphlen);
216 /* destination port of 0 is illegal, based on RFC768. */
217 if (uh->uh_dport == 0)
221 * Construct sockaddr format source address.
222 * Stuff source address and datagram in user buffer.
224 bzero(&udp_in, sizeof(udp_in));
225 udp_in.sin_len = sizeof(udp_in);
226 udp_in.sin_family = AF_INET;
227 udp_in.sin_port = uh->uh_sport;
228 udp_in.sin_addr = ip->ip_src;
231 * Make mbuf data length reflect UDP length.
232 * If not enough data to reflect UDP length, drop.
234 len = ntohs((u_short)uh->uh_ulen);
235 if (ip->ip_len != len) {
236 if (len > ip->ip_len || len < sizeof(struct udphdr)) {
237 udpstat.udps_badlen++;
240 m_adj(m, len - ip->ip_len);
241 /* ip->ip_len = len; */
244 * Save a copy of the IP header in case we want restore it
245 * for sending an ICMP error message in response.
251 * Checksum extended UDP header and data.
254 if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
255 if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
256 uh->uh_sum = m->m_pkthdr.csum_data;
258 uh->uh_sum = in_pseudo(ip->ip_src.s_addr,
259 ip->ip_dst.s_addr, htonl((u_short)len +
260 m->m_pkthdr.csum_data + IPPROTO_UDP));
261 uh->uh_sum ^= 0xffff;
264 bcopy(((struct ipovly *)ip)->ih_x1, b, 9);
265 bzero(((struct ipovly *)ip)->ih_x1, 9);
266 ((struct ipovly *)ip)->ih_len = uh->uh_ulen;
267 uh->uh_sum = in_cksum(m, len + sizeof (struct ip));
268 bcopy(b, ((struct ipovly *)ip)->ih_x1, 9);
271 udpstat.udps_badsum++;
276 udpstat.udps_nosum++;
278 #ifdef IPFIREWALL_FORWARD
279 /* Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain. */
280 fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL);
282 if (fwd_tag != NULL) {
283 struct sockaddr_in *next_hop;
286 next_hop = (struct sockaddr_in *)(fwd_tag + 1);
287 ip->ip_dst = next_hop->sin_addr;
288 uh->uh_dport = ntohs(next_hop->sin_port);
289 /* Remove the tag from the packet. We don't need it anymore. */
290 m_tag_delete(m, fwd_tag);
294 INP_INFO_RLOCK(&udbinfo);
296 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
297 in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) {
300 * Deliver a multicast or broadcast datagram to *all* sockets
301 * for which the local and remote addresses and ports match
302 * those of the incoming datagram. This allows more than
303 * one process to receive multi/broadcasts on the same port.
304 * (This really ought to be done for unicast datagrams as
305 * well, but that would cause problems with existing
306 * applications that open both address-specific sockets and
307 * a wildcard socket listening to the same port -- they would
308 * end up receiving duplicates of every unicast datagram.
309 * Those applications open the multiple sockets to overcome an
310 * inadequacy of the UDP socket interface, but for backwards
311 * compatibility we avoid the problem here rather than
312 * fixing the interface. Maybe 4.5BSD will remedy this?)
316 * Locate pcb(s) for datagram.
317 * (Algorithm copied from raw_intr().)
320 LIST_FOREACH(inp, &udb, inp_list) {
321 if (inp->inp_lport != uh->uh_dport)
324 if ((inp->inp_vflag & INP_IPV4) == 0)
327 if (inp->inp_laddr.s_addr != INADDR_ANY) {
328 if (inp->inp_laddr.s_addr != ip->ip_dst.s_addr)
331 if (inp->inp_faddr.s_addr != INADDR_ANY) {
332 if (inp->inp_faddr.s_addr !=
334 inp->inp_fport != uh->uh_sport)
340 * Check multicast packets to make sure they are only
341 * sent to sockets with multicast memberships for the
342 * packet's destination address and arrival interface
344 #define MSHIP(_inp, n) ((_inp)->inp_moptions->imo_membership[(n)])
345 #define NMSHIPS(_inp) ((_inp)->inp_moptions->imo_num_memberships)
346 if (strict_mcast_mship && inp->inp_moptions != NULL) {
347 int mship, foundmship = 0;
349 for (mship = 0; mship < NMSHIPS(inp); mship++) {
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,
372 sizeof(struct udphdr),
378 * Don't look for additional matches if this one does
379 * not have either the SO_REUSEPORT or SO_REUSEADDR
380 * socket options set. This heuristic avoids searching
381 * through all pcbs in the common case of a non-shared
382 * port. It assumes that an application will never
383 * clear these options after setting them.
385 if ((last->inp_socket->so_options&(SO_REUSEPORT|SO_REUSEADDR)) == 0)
391 * No matching pcb found; discard datagram.
392 * (No need to send an ICMP Port Unreachable
393 * for a broadcast or multicast datgram.)
395 udpstat.udps_noportbcast++;
398 udp_append(last, ip, m, iphlen + sizeof(struct udphdr),
401 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);
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);
435 /* Check the minimum TTL for socket. */
436 if (inp->inp_ip_minttl && inp->inp_ip_minttl > ip->ip_ttl)
438 udp_append(inp, ip, m, iphlen + sizeof(struct udphdr), &udp_in);
440 INP_INFO_RUNLOCK(&udbinfo);
446 INP_INFO_RUNLOCK(&udbinfo);
453 * Subroutine of udp_input(), which appends the provided mbuf chain to the
454 * passed pcb/socket. The caller must provide a sockaddr_in via udp_in that
455 * contains the source address. If the socket ends up being an IPv6 socket,
456 * udp_append() will convert to a sockaddr_in6 before passing the address
457 * into the socket code.
460 udp_append(inp, ip, n, off, udp_in)
465 struct sockaddr_in *udp_in;
467 struct sockaddr *append_sa;
469 struct mbuf *opts = 0;
471 struct sockaddr_in6 udp_in6;
474 INP_LOCK_ASSERT(inp);
476 #if defined(IPSEC) || defined(FAST_IPSEC)
477 /* check AH/ESP integrity. */
478 if (ipsec4_in_reject(n, inp)) {
480 ipsecstat.in_polvio++;
485 #endif /*IPSEC || FAST_IPSEC*/
487 if (mac_check_inpcb_deliver(inp, n) != 0) {
492 if (inp->inp_flags & INP_CONTROLOPTS ||
493 inp->inp_socket->so_options & (SO_TIMESTAMP | SO_BINTIME)) {
495 if (inp->inp_vflag & INP_IPV6) {
498 savedflags = inp->inp_flags;
499 inp->inp_flags &= ~INP_UNMAPPABLEOPTS;
500 ip6_savecontrol(inp, n, &opts);
501 inp->inp_flags = savedflags;
504 ip_savecontrol(inp, &opts, ip, n);
507 if (inp->inp_vflag & INP_IPV6) {
508 bzero(&udp_in6, sizeof(udp_in6));
509 udp_in6.sin6_len = sizeof(udp_in6);
510 udp_in6.sin6_family = AF_INET6;
511 in6_sin_2_v4mapsin6(udp_in, &udp_in6);
512 append_sa = (struct sockaddr *)&udp_in6;
515 append_sa = (struct sockaddr *)udp_in;
518 so = inp->inp_socket;
519 SOCKBUF_LOCK(&so->so_rcv);
520 if (sbappendaddr_locked(&so->so_rcv, append_sa, n, opts) == 0) {
524 udpstat.udps_fullsock++;
525 SOCKBUF_UNLOCK(&so->so_rcv);
527 sorwakeup_locked(so);
531 * Notify a udp user of an asynchronous error;
532 * just wake up so that he can collect error status.
535 udp_notify(inp, errno)
536 register struct inpcb *inp;
539 inp->inp_socket->so_error = errno;
540 sorwakeup(inp->inp_socket);
541 sowwakeup(inp->inp_socket);
546 udp_ctlinput(cmd, sa, vip)
553 struct inpcb *(*notify)(struct inpcb *, int) = udp_notify;
554 struct in_addr faddr;
557 faddr = ((struct sockaddr_in *)sa)->sin_addr;
558 if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY)
562 * Redirects don't need to be handled up here.
564 if (PRC_IS_REDIRECT(cmd))
567 * Hostdead is ugly because it goes linearly through all PCBs.
568 * XXX: We never get this from ICMP, otherwise it makes an
569 * excellent DoS attack on machines with many connections.
571 if (cmd == PRC_HOSTDEAD)
573 else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0)
576 uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
577 INP_INFO_RLOCK(&udbinfo);
578 inp = in_pcblookup_hash(&udbinfo, faddr, uh->uh_dport,
579 ip->ip_src, uh->uh_sport, 0, NULL);
582 if (inp->inp_socket != NULL) {
583 (*notify)(inp, inetctlerrmap[cmd]);
587 INP_INFO_RUNLOCK(&udbinfo);
589 in_pcbnotifyall(&udbinfo, faddr, inetctlerrmap[cmd], notify);
593 udp_pcblist(SYSCTL_HANDLER_ARGS)
596 struct inpcb *inp, **inp_list;
601 * The process of preparing the TCB list is too time-consuming and
602 * resource-intensive to repeat twice on every request.
604 if (req->oldptr == 0) {
605 n = udbinfo.ipi_count;
606 req->oldidx = 2 * (sizeof xig)
607 + (n + n/8) * sizeof(struct xinpcb);
611 if (req->newptr != 0)
615 * OK, now we're committed to doing something.
617 INP_INFO_RLOCK(&udbinfo);
618 gencnt = udbinfo.ipi_gencnt;
619 n = udbinfo.ipi_count;
620 INP_INFO_RUNLOCK(&udbinfo);
622 error = sysctl_wire_old_buffer(req, 2 * (sizeof xig)
623 + n * sizeof(struct xinpcb));
627 xig.xig_len = sizeof xig;
629 xig.xig_gen = gencnt;
630 xig.xig_sogen = so_gencnt;
631 error = SYSCTL_OUT(req, &xig, sizeof xig);
635 inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK);
639 INP_INFO_RLOCK(&udbinfo);
640 for (inp = LIST_FIRST(udbinfo.listhead), i = 0; inp && i < n;
641 inp = LIST_NEXT(inp, inp_list)) {
643 if (inp->inp_gencnt <= gencnt &&
644 cr_canseesocket(req->td->td_ucred, inp->inp_socket) == 0)
648 INP_INFO_RUNLOCK(&udbinfo);
652 for (i = 0; i < n; i++) {
655 if (inp->inp_gencnt <= gencnt) {
657 bzero(&xi, sizeof(xi));
658 xi.xi_len = sizeof xi;
659 /* XXX should avoid extra copy */
660 bcopy(inp, &xi.xi_inp, sizeof *inp);
662 sotoxsocket(inp->inp_socket, &xi.xi_socket);
663 xi.xi_inp.inp_gencnt = inp->inp_gencnt;
665 error = SYSCTL_OUT(req, &xi, sizeof xi);
671 * Give the user an updated idea of our state.
672 * If the generation differs from what we told
673 * her before, she knows that something happened
674 * while we were processing this request, and it
675 * might be necessary to retry.
677 INP_INFO_RLOCK(&udbinfo);
678 xig.xig_gen = udbinfo.ipi_gencnt;
679 xig.xig_sogen = so_gencnt;
680 xig.xig_count = udbinfo.ipi_count;
681 INP_INFO_RUNLOCK(&udbinfo);
682 error = SYSCTL_OUT(req, &xig, sizeof xig);
684 free(inp_list, M_TEMP);
688 SYSCTL_PROC(_net_inet_udp, UDPCTL_PCBLIST, pcblist, CTLFLAG_RD, 0, 0,
689 udp_pcblist, "S,xinpcb", "List of active UDP sockets");
692 udp_getcred(SYSCTL_HANDLER_ARGS)
695 struct sockaddr_in addrs[2];
699 error = priv_check_cred(req->td->td_ucred, PRIV_NETINET_GETCRED,
703 error = SYSCTL_IN(req, addrs, sizeof(addrs));
706 INP_INFO_RLOCK(&udbinfo);
707 inp = in_pcblookup_hash(&udbinfo, addrs[1].sin_addr, addrs[1].sin_port,
708 addrs[0].sin_addr, addrs[0].sin_port, 1, NULL);
709 if (inp == NULL || inp->inp_socket == NULL) {
713 error = cr_canseesocket(req->td->td_ucred, inp->inp_socket);
716 cru2x(inp->inp_socket->so_cred, &xuc);
718 INP_INFO_RUNLOCK(&udbinfo);
720 error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
724 SYSCTL_PROC(_net_inet_udp, OID_AUTO, getcred,
725 CTLTYPE_OPAQUE|CTLFLAG_RW|CTLFLAG_PRISON, 0, 0,
726 udp_getcred, "S,xucred", "Get the xucred of a UDP connection");
729 udp_output(inp, m, addr, control, td)
730 register struct inpcb *inp;
732 struct sockaddr *addr;
733 struct mbuf *control;
736 register struct udpiphdr *ui;
737 register int len = m->m_pkthdr.len;
738 struct in_addr faddr, laddr;
740 struct sockaddr_in *sin, src;
743 u_short fport, lport;
747 * udp_output() may need to temporarily bind or connect the current
748 * inpcb. As such, we don't know up front what inpcb locks we will
749 * need. Do any work to decide what is needed up front before
752 if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) {
759 src.sin_addr.s_addr = INADDR_ANY;
760 if (control != NULL) {
762 * XXX: Currently, we assume all the optional information
763 * is stored in a single mbuf.
765 if (control->m_next) {
770 for (; control->m_len > 0;
771 control->m_data += CMSG_ALIGN(cm->cmsg_len),
772 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
773 cm = mtod(control, struct cmsghdr *);
774 if (control->m_len < sizeof(*cm) || cm->cmsg_len == 0 ||
775 cm->cmsg_len > control->m_len) {
779 if (cm->cmsg_level != IPPROTO_IP)
782 switch (cm->cmsg_type) {
785 CMSG_LEN(sizeof(struct in_addr))) {
789 bzero(&src, sizeof(src));
790 src.sin_family = AF_INET;
791 src.sin_len = sizeof(src);
792 src.sin_port = inp->inp_lport;
793 src.sin_addr = *(struct in_addr *)CMSG_DATA(cm);
809 if (src.sin_addr.s_addr != INADDR_ANY ||
811 INP_INFO_WLOCK(&udbinfo);
818 mac_create_mbuf_from_inpcb(inp, m);
821 laddr = inp->inp_laddr;
822 lport = inp->inp_lport;
823 if (src.sin_addr.s_addr != INADDR_ANY) {
828 error = in_pcbbind_setup(inp, (struct sockaddr *)&src,
829 &laddr.s_addr, &lport, td->td_ucred);
835 sin = (struct sockaddr_in *)addr;
836 if (jailed(td->td_ucred))
837 prison_remote_ip(td->td_ucred, 0, &sin->sin_addr.s_addr);
838 if (inp->inp_faddr.s_addr != INADDR_ANY) {
842 error = in_pcbconnect_setup(inp, addr, &laddr.s_addr, &lport,
843 &faddr.s_addr, &fport, NULL, td->td_ucred);
847 /* Commit the local port if newly assigned. */
848 if (inp->inp_laddr.s_addr == INADDR_ANY &&
849 inp->inp_lport == 0) {
851 * Remember addr if jailed, to prevent rebinding.
853 if (jailed(td->td_ucred))
854 inp->inp_laddr = laddr;
855 inp->inp_lport = lport;
856 if (in_pcbinshash(inp) != 0) {
861 inp->inp_flags |= INP_ANONPORT;
864 faddr = inp->inp_faddr;
865 fport = inp->inp_fport;
866 if (faddr.s_addr == INADDR_ANY) {
873 * Calculate data length and get a mbuf for UDP, IP, and possible
874 * link-layer headers. Immediate slide the data pointer back forward
875 * since we won't use that space at this layer.
877 M_PREPEND(m, sizeof(struct udpiphdr) + max_linkhdr, M_DONTWAIT);
882 m->m_data += max_linkhdr;
883 m->m_len -= max_linkhdr;
884 m->m_pkthdr.len -= max_linkhdr;
887 * Fill in mbuf with extended UDP header
888 * and addresses and length put into network format.
890 ui = mtod(m, struct udpiphdr *);
891 bzero(ui->ui_x1, sizeof(ui->ui_x1)); /* XXX still needed? */
892 ui->ui_pr = IPPROTO_UDP;
895 ui->ui_sport = lport;
896 ui->ui_dport = fport;
897 ui->ui_ulen = htons((u_short)len + sizeof(struct udphdr));
900 * Set the Don't Fragment bit in the IP header.
902 if (inp->inp_flags & INP_DONTFRAG) {
904 ip = (struct ip *)&ui->ui_i;
909 if (inp->inp_socket->so_options & SO_DONTROUTE)
910 ipflags |= IP_ROUTETOIF;
911 if (inp->inp_socket->so_options & SO_BROADCAST)
912 ipflags |= IP_ALLOWBROADCAST;
913 if (inp->inp_flags & INP_ONESBCAST)
914 ipflags |= IP_SENDONES;
917 * Set up checksum and output datagram.
920 if (inp->inp_flags & INP_ONESBCAST)
921 faddr.s_addr = INADDR_BROADCAST;
922 ui->ui_sum = in_pseudo(ui->ui_src.s_addr, faddr.s_addr,
923 htons((u_short)len + sizeof(struct udphdr) + IPPROTO_UDP));
924 m->m_pkthdr.csum_flags = CSUM_UDP;
925 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
929 ((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len;
930 ((struct ip *)ui)->ip_ttl = inp->inp_ip_ttl; /* XXX */
931 ((struct ip *)ui)->ip_tos = inp->inp_ip_tos; /* XXX */
932 udpstat.udps_opackets++;
935 INP_INFO_WUNLOCK(&udbinfo);
936 error = ip_output(m, inp->inp_options, NULL, ipflags,
937 inp->inp_moptions, inp);
944 INP_INFO_WUNLOCK(&udbinfo);
949 u_long udp_sendspace = 9216; /* really max datagram size */
950 /* 40 1K datagrams */
951 SYSCTL_ULONG(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram, CTLFLAG_RW,
952 &udp_sendspace, 0, "Maximum outgoing UDP datagram size");
954 u_long udp_recvspace = 40 * (1024 +
956 sizeof(struct sockaddr_in6)
958 sizeof(struct sockaddr_in)
961 SYSCTL_ULONG(_net_inet_udp, UDPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
962 &udp_recvspace, 0, "Maximum space for incoming UDP datagrams");
965 udp_abort(struct socket *so)
970 KASSERT(inp != NULL, ("udp_abort: inp == NULL"));
971 INP_INFO_WLOCK(&udbinfo);
973 if (inp->inp_faddr.s_addr != INADDR_ANY) {
974 in_pcbdisconnect(inp);
975 inp->inp_laddr.s_addr = INADDR_ANY;
976 soisdisconnected(so);
979 INP_INFO_WUNLOCK(&udbinfo);
983 udp_attach(struct socket *so, int proto, struct thread *td)
989 KASSERT(inp == NULL, ("udp_attach: inp != NULL"));
990 error = soreserve(so, udp_sendspace, udp_recvspace);
993 INP_INFO_WLOCK(&udbinfo);
994 error = in_pcballoc(so, &udbinfo);
996 INP_INFO_WUNLOCK(&udbinfo);
1000 inp = (struct inpcb *)so->so_pcb;
1001 INP_INFO_WUNLOCK(&udbinfo);
1002 inp->inp_vflag |= INP_IPV4;
1003 inp->inp_ip_ttl = ip_defttl;
1009 udp_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
1014 inp = sotoinpcb(so);
1015 KASSERT(inp != NULL, ("udp_bind: inp == NULL"));
1016 INP_INFO_WLOCK(&udbinfo);
1018 error = in_pcbbind(inp, nam, td->td_ucred);
1020 INP_INFO_WUNLOCK(&udbinfo);
1025 udp_close(struct socket *so)
1029 inp = sotoinpcb(so);
1030 KASSERT(inp != NULL, ("udp_close: inp == NULL"));
1031 INP_INFO_WLOCK(&udbinfo);
1033 if (inp->inp_faddr.s_addr != INADDR_ANY) {
1034 in_pcbdisconnect(inp);
1035 inp->inp_laddr.s_addr = INADDR_ANY;
1036 soisdisconnected(so);
1039 INP_INFO_WUNLOCK(&udbinfo);
1043 udp_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
1047 struct sockaddr_in *sin;
1049 inp = sotoinpcb(so);
1050 KASSERT(inp != NULL, ("udp_connect: inp == NULL"));
1051 INP_INFO_WLOCK(&udbinfo);
1053 if (inp->inp_faddr.s_addr != INADDR_ANY) {
1055 INP_INFO_WUNLOCK(&udbinfo);
1058 sin = (struct sockaddr_in *)nam;
1059 if (jailed(td->td_ucred))
1060 prison_remote_ip(td->td_ucred, 0, &sin->sin_addr.s_addr);
1061 error = in_pcbconnect(inp, nam, td->td_ucred);
1065 INP_INFO_WUNLOCK(&udbinfo);
1070 udp_detach(struct socket *so)
1074 inp = sotoinpcb(so);
1075 KASSERT(inp != NULL, ("udp_detach: inp == NULL"));
1076 KASSERT(inp->inp_faddr.s_addr == INADDR_ANY,
1077 ("udp_detach: not disconnected"));
1078 INP_INFO_WLOCK(&udbinfo);
1082 INP_INFO_WUNLOCK(&udbinfo);
1086 udp_disconnect(struct socket *so)
1090 inp = sotoinpcb(so);
1091 KASSERT(inp != NULL, ("udp_disconnect: inp == NULL"));
1092 INP_INFO_WLOCK(&udbinfo);
1094 if (inp->inp_faddr.s_addr == INADDR_ANY) {
1095 INP_INFO_WUNLOCK(&udbinfo);
1100 in_pcbdisconnect(inp);
1101 inp->inp_laddr.s_addr = INADDR_ANY;
1103 so->so_state &= ~SS_ISCONNECTED; /* XXX */
1106 INP_INFO_WUNLOCK(&udbinfo);
1111 udp_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
1112 struct mbuf *control, struct thread *td)
1116 inp = sotoinpcb(so);
1117 KASSERT(inp != NULL, ("udp_send: inp == NULL"));
1118 return udp_output(inp, m, addr, control, td);
1122 udp_shutdown(struct socket *so)
1126 inp = sotoinpcb(so);
1127 KASSERT(inp != NULL, ("udp_shutdown: inp == NULL"));
1135 * This is the wrapper function for in_setsockaddr. We just pass down
1136 * the pcbinfo for in_setsockaddr to lock. We don't want to do the locking
1137 * here because in_setsockaddr will call malloc and might block.
1140 udp_sockaddr(struct socket *so, struct sockaddr **nam)
1142 return (in_setsockaddr(so, nam, &udbinfo));
1146 * This is the wrapper function for in_setpeeraddr. We just pass down
1147 * the pcbinfo for in_setpeeraddr to lock.
1150 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,