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 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#)udp_usrreq.c 8.6 (Berkeley) 5/23/95
37 #include "opt_ipsec.h"
38 #include "opt_inet6.h"
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/domain.h>
45 #include <sys/kernel.h>
48 #include <sys/malloc.h>
51 #include <sys/protosw.h>
52 #include <sys/signalvar.h>
53 #include <sys/socket.h>
54 #include <sys/socketvar.h>
56 #include <sys/sysctl.h>
57 #include <sys/syslog.h>
62 #include <net/route.h>
64 #include <netinet/in.h>
65 #include <netinet/in_systm.h>
66 #include <netinet/in_pcb.h>
67 #include <netinet/in_var.h>
68 #include <netinet/ip.h>
70 #include <netinet/ip6.h>
72 #include <netinet/ip_icmp.h>
73 #include <netinet/icmp_var.h>
74 #include <netinet/ip_var.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>
92 * UDP protocol implementation.
93 * Per RFC 768, August, 1980.
96 static int udpcksum = 1;
98 static int udpcksum = 0; /* XXX */
100 SYSCTL_INT(_net_inet_udp, UDPCTL_CHECKSUM, checksum, CTLFLAG_RW,
104 SYSCTL_INT(_net_inet_udp, OID_AUTO, log_in_vain, CTLFLAG_RW,
105 &log_in_vain, 0, "Log all incoming UDP packets");
107 static int blackhole = 0;
108 SYSCTL_INT(_net_inet_udp, OID_AUTO, blackhole, CTLFLAG_RW,
109 &blackhole, 0, "Do not send port unreachables for refused connects");
111 struct inpcbhead udb; /* from udp_var.h */
112 #define udb6 udb /* for KAME src sync over BSD*'s */
113 struct inpcbinfo udbinfo;
116 #define UDBHASHSIZE 16
119 struct udpstat udpstat; /* from udp_var.h */
120 SYSCTL_STRUCT(_net_inet_udp, UDPCTL_STATS, stats, CTLFLAG_RW,
121 &udpstat, udpstat, "UDP statistics (struct udpstat, netinet/udp_var.h)");
123 static struct sockaddr_in udp_in = { sizeof(udp_in), AF_INET };
126 struct sockaddr_in6 uin6_sin;
127 u_char uin6_init_done : 1;
129 { sizeof(udp_in6.uin6_sin), AF_INET6 },
133 struct ip6_hdr uip6_ip6;
134 u_char uip6_init_done : 1;
138 static void udp_append(struct inpcb *last, struct ip *ip, struct mbuf *n,
141 static void ip_2_ip6_hdr(struct ip6_hdr *ip6, struct ip *ip);
144 static int udp_detach(struct socket *so);
145 static int udp_output(struct inpcb *, struct mbuf *, struct sockaddr *,
146 struct mbuf *, struct thread *);
151 INP_INFO_LOCK_INIT(&udbinfo, "udp");
153 udbinfo.listhead = &udb;
154 udbinfo.hashbase = hashinit(UDBHASHSIZE, M_PCB, &udbinfo.hashmask);
155 udbinfo.porthashbase = hashinit(UDBHASHSIZE, M_PCB,
156 &udbinfo.porthashmask);
157 udbinfo.ipi_zone = uma_zcreate("udpcb", sizeof(struct inpcb), NULL,
158 NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
159 uma_zone_set_max(udbinfo.ipi_zone, maxsockets);
164 register struct mbuf *m;
168 register struct ip *ip;
169 register struct udphdr *uh;
170 register struct inpcb *inp;
171 struct mbuf *opts = 0;
175 udpstat.udps_ipackets++;
178 * Strip IP options, if any; should skip this,
179 * make available to user, and use on returned packets,
180 * but we don't yet have a way to check the checksum
181 * with options still present.
183 if (iphlen > sizeof (struct ip)) {
184 ip_stripoptions(m, (struct mbuf *)0);
185 iphlen = sizeof(struct ip);
189 * Get IP and UDP header together in first mbuf.
191 ip = mtod(m, struct ip *);
192 if (m->m_len < iphlen + sizeof(struct udphdr)) {
193 if ((m = m_pullup(m, iphlen + sizeof(struct udphdr))) == 0) {
194 udpstat.udps_hdrops++;
197 ip = mtod(m, struct ip *);
199 uh = (struct udphdr *)((caddr_t)ip + iphlen);
201 /* destination port of 0 is illegal, based on RFC768. */
202 if (uh->uh_dport == 0)
206 * Construct sockaddr format source address.
207 * Stuff source address and datagram in user buffer.
209 udp_in.sin_port = uh->uh_sport;
210 udp_in.sin_addr = ip->ip_src;
212 udp_in6.uin6_init_done = udp_ip6.uip6_init_done = 0;
216 * Make mbuf data length reflect UDP length.
217 * If not enough data to reflect UDP length, drop.
219 len = ntohs((u_short)uh->uh_ulen);
220 if (ip->ip_len != len) {
221 if (len > ip->ip_len || len < sizeof(struct udphdr)) {
222 udpstat.udps_badlen++;
225 m_adj(m, len - ip->ip_len);
226 /* ip->ip_len = len; */
229 * Save a copy of the IP header in case we want restore it
230 * for sending an ICMP error message in response.
236 * Checksum extended UDP header and data.
239 if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
240 if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
241 uh->uh_sum = m->m_pkthdr.csum_data;
243 uh->uh_sum = in_pseudo(ip->ip_src.s_addr,
244 ip->ip_dst.s_addr, htonl((u_short)len +
245 m->m_pkthdr.csum_data + IPPROTO_UDP));
246 uh->uh_sum ^= 0xffff;
249 bcopy(((struct ipovly *)ip)->ih_x1, b, 9);
250 bzero(((struct ipovly *)ip)->ih_x1, 9);
251 ((struct ipovly *)ip)->ih_len = uh->uh_ulen;
252 uh->uh_sum = in_cksum(m, len + sizeof (struct ip));
253 bcopy(b, ((struct ipovly *)ip)->ih_x1, 9);
256 udpstat.udps_badsum++;
261 udpstat.udps_nosum++;
263 INP_INFO_RLOCK(&udbinfo);
265 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
266 in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) {
269 * Deliver a multicast or broadcast datagram to *all* sockets
270 * for which the local and remote addresses and ports match
271 * those of the incoming datagram. This allows more than
272 * one process to receive multi/broadcasts on the same port.
273 * (This really ought to be done for unicast datagrams as
274 * well, but that would cause problems with existing
275 * applications that open both address-specific sockets and
276 * a wildcard socket listening to the same port -- they would
277 * end up receiving duplicates of every unicast datagram.
278 * Those applications open the multiple sockets to overcome an
279 * inadequacy of the UDP socket interface, but for backwards
280 * compatibility we avoid the problem here rather than
281 * fixing the interface. Maybe 4.5BSD will remedy this?)
285 * Locate pcb(s) for datagram.
286 * (Algorithm copied from raw_intr().)
289 LIST_FOREACH(inp, &udb, inp_list) {
291 if (inp->inp_lport != uh->uh_dport) {
297 if ((inp->inp_vflag & INP_IPV4) == 0)
300 if (inp->inp_laddr.s_addr != INADDR_ANY) {
301 if (inp->inp_laddr.s_addr != ip->ip_dst.s_addr)
304 if (inp->inp_faddr.s_addr != INADDR_ANY) {
305 if (inp->inp_faddr.s_addr !=
307 inp->inp_fport != uh->uh_sport)
314 n = m_copy(m, 0, M_COPYALL);
316 udp_append(last, ip, n,
318 sizeof(struct udphdr));
323 * Don't look for additional matches if this one does
324 * not have either the SO_REUSEPORT or SO_REUSEADDR
325 * socket options set. This heuristic avoids searching
326 * through all pcbs in the common case of a non-shared
327 * port. It * assumes that an application will never
328 * clear these options after setting them.
330 if ((last->inp_socket->so_options&(SO_REUSEPORT|SO_REUSEADDR)) == 0)
336 * No matching pcb found; discard datagram.
337 * (No need to send an ICMP Port Unreachable
338 * for a broadcast or multicast datgram.)
340 udpstat.udps_noportbcast++;
343 INP_INFO_RUNLOCK(&udbinfo);
344 udp_append(last, ip, m, iphlen + sizeof(struct udphdr));
349 * Locate pcb for datagram.
351 inp = in_pcblookup_hash(&udbinfo, ip->ip_src, uh->uh_sport,
352 ip->ip_dst, uh->uh_dport, 1, m->m_pkthdr.rcvif);
355 char buf[4*sizeof "123"];
357 strcpy(buf, inet_ntoa(ip->ip_dst));
359 "Connection attempt to UDP %s:%d from %s:%d\n",
360 buf, ntohs(uh->uh_dport), inet_ntoa(ip->ip_src),
361 ntohs(uh->uh_sport));
363 udpstat.udps_noport++;
364 if (m->m_flags & (M_BCAST | M_MCAST)) {
365 udpstat.udps_noportbcast++;
370 if (badport_bandlim(BANDLIM_ICMP_UNREACH) < 0)
373 ip->ip_len += iphlen;
374 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
375 INP_INFO_RUNLOCK(&udbinfo);
379 INP_INFO_RUNLOCK(&udbinfo);
380 udp_append(inp, ip, m, iphlen + sizeof(struct udphdr));
385 INP_INFO_RUNLOCK(&udbinfo);
397 ip_2_ip6_hdr(ip6, ip)
401 bzero(ip6, sizeof(*ip6));
403 ip6->ip6_vfc = IPV6_VERSION;
404 ip6->ip6_plen = ip->ip_len;
405 ip6->ip6_nxt = ip->ip_p;
406 ip6->ip6_hlim = ip->ip_ttl;
407 ip6->ip6_src.s6_addr32[2] = ip6->ip6_dst.s6_addr32[2] =
409 ip6->ip6_src.s6_addr32[3] = ip->ip_src.s_addr;
410 ip6->ip6_dst.s6_addr32[3] = ip->ip_dst.s_addr;
415 * subroutine of udp_input(), mainly for source code readability.
416 * caller must properly init udp_ip6 and udp_in6 beforehand.
419 udp_append(last, ip, n, off)
425 struct sockaddr *append_sa;
426 struct mbuf *opts = 0;
429 /* check AH/ESP integrity. */
430 if (ipsec4_in_reject_so(n, last->inp_socket)) {
431 ipsecstat.in_polvio++;
437 /* check AH/ESP integrity. */
438 if (ipsec4_in_reject(n, last)) {
442 #endif /*FAST_IPSEC*/
444 if (mac_check_socket_deliver(last->inp_socket, n) != 0) {
449 if (last->inp_flags & INP_CONTROLOPTS ||
450 last->inp_socket->so_options & SO_TIMESTAMP) {
452 if (last->inp_vflag & INP_IPV6) {
455 if (udp_ip6.uip6_init_done == 0) {
456 ip_2_ip6_hdr(&udp_ip6.uip6_ip6, ip);
457 udp_ip6.uip6_init_done = 1;
459 savedflags = last->inp_flags;
460 last->inp_flags &= ~INP_UNMAPPABLEOPTS;
461 ip6_savecontrol(last, &opts, &udp_ip6.uip6_ip6, n);
462 last->inp_flags = savedflags;
465 ip_savecontrol(last, &opts, ip, n);
468 if (last->inp_vflag & INP_IPV6) {
469 if (udp_in6.uin6_init_done == 0) {
470 in6_sin_2_v4mapsin6(&udp_in, &udp_in6.uin6_sin);
471 udp_in6.uin6_init_done = 1;
473 append_sa = (struct sockaddr *)&udp_in6.uin6_sin;
476 append_sa = (struct sockaddr *)&udp_in;
478 if (sbappendaddr(&last->inp_socket->so_rcv, append_sa, n, opts) == 0) {
482 udpstat.udps_fullsock++;
484 sorwakeup(last->inp_socket);
488 * Notify a udp user of an asynchronous error;
489 * just wake up so that he can collect error status.
492 udp_notify(inp, errno)
493 register struct inpcb *inp;
496 inp->inp_socket->so_error = errno;
497 sorwakeup(inp->inp_socket);
498 sowwakeup(inp->inp_socket);
503 udp_ctlinput(cmd, sa, vip)
510 struct inpcb *(*notify)(struct inpcb *, int) = udp_notify;
511 struct in_addr faddr;
515 faddr = ((struct sockaddr_in *)sa)->sin_addr;
516 if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY)
519 if (PRC_IS_REDIRECT(cmd)) {
521 notify = in_rtchange;
522 } else if (cmd == PRC_HOSTDEAD)
524 else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0)
528 uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
529 INP_INFO_RLOCK(&udbinfo);
530 inp = in_pcblookup_hash(&udbinfo, faddr, uh->uh_dport,
531 ip->ip_src, uh->uh_sport, 0, NULL);
534 if (inp->inp_socket != NULL) {
535 (*notify)(inp, inetctlerrmap[cmd]);
539 INP_INFO_RUNLOCK(&udbinfo);
542 in_pcbnotifyall(&udbinfo, faddr, inetctlerrmap[cmd], notify);
546 udp_pcblist(SYSCTL_HANDLER_ARGS)
549 struct inpcb *inp, **inp_list;
554 * The process of preparing the TCB list is too time-consuming and
555 * resource-intensive to repeat twice on every request.
557 if (req->oldptr == 0) {
558 n = udbinfo.ipi_count;
559 req->oldidx = 2 * (sizeof xig)
560 + (n + n/8) * sizeof(struct xinpcb);
564 if (req->newptr != 0)
568 * OK, now we're committed to doing something.
571 INP_INFO_RLOCK(&udbinfo);
572 gencnt = udbinfo.ipi_gencnt;
573 n = udbinfo.ipi_count;
574 INP_INFO_RUNLOCK(&udbinfo);
577 sysctl_wire_old_buffer(req, 2 * (sizeof xig)
578 + n * sizeof(struct xinpcb));
580 xig.xig_len = sizeof xig;
582 xig.xig_gen = gencnt;
583 xig.xig_sogen = so_gencnt;
584 error = SYSCTL_OUT(req, &xig, sizeof xig);
588 inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK);
593 INP_INFO_RLOCK(&udbinfo);
594 for (inp = LIST_FIRST(udbinfo.listhead), i = 0; inp && i < n;
595 inp = LIST_NEXT(inp, inp_list)) {
597 if (inp->inp_gencnt <= gencnt &&
598 cr_canseesocket(req->td->td_ucred, inp->inp_socket) == 0)
602 INP_INFO_RUNLOCK(&udbinfo);
607 for (i = 0; i < n; i++) {
609 if (inp->inp_gencnt <= gencnt) {
611 xi.xi_len = sizeof xi;
612 /* XXX should avoid extra copy */
613 bcopy(inp, &xi.xi_inp, sizeof *inp);
615 sotoxsocket(inp->inp_socket, &xi.xi_socket);
616 xi.xi_inp.inp_gencnt = inp->inp_gencnt;
617 error = SYSCTL_OUT(req, &xi, sizeof xi);
622 * Give the user an updated idea of our state.
623 * If the generation differs from what we told
624 * her before, she knows that something happened
625 * while we were processing this request, and it
626 * might be necessary to retry.
629 INP_INFO_RLOCK(&udbinfo);
630 xig.xig_gen = udbinfo.ipi_gencnt;
631 xig.xig_sogen = so_gencnt;
632 xig.xig_count = udbinfo.ipi_count;
633 INP_INFO_RUNLOCK(&udbinfo);
635 error = SYSCTL_OUT(req, &xig, sizeof xig);
637 free(inp_list, M_TEMP);
641 SYSCTL_PROC(_net_inet_udp, UDPCTL_PCBLIST, pcblist, CTLFLAG_RD, 0, 0,
642 udp_pcblist, "S,xinpcb", "List of active UDP sockets");
645 udp_getcred(SYSCTL_HANDLER_ARGS)
648 struct sockaddr_in addrs[2];
652 error = suser_cred(req->td->td_ucred, PRISON_ROOT);
655 error = SYSCTL_IN(req, addrs, sizeof(addrs));
659 INP_INFO_RLOCK(&udbinfo);
660 inp = in_pcblookup_hash(&udbinfo, addrs[1].sin_addr, addrs[1].sin_port,
661 addrs[0].sin_addr, addrs[0].sin_port, 1, NULL);
662 if (inp == NULL || inp->inp_socket == NULL) {
666 error = cr_canseesocket(req->td->td_ucred, inp->inp_socket);
669 cru2x(inp->inp_socket->so_cred, &xuc);
671 INP_INFO_RUNLOCK(&udbinfo);
674 error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
678 SYSCTL_PROC(_net_inet_udp, OID_AUTO, getcred,
679 CTLTYPE_OPAQUE|CTLFLAG_RW|CTLFLAG_PRISON, 0, 0,
680 udp_getcred, "S,xucred", "Get the xucred of a UDP connection");
683 udp_output(inp, m, addr, control, td)
684 register struct inpcb *inp;
686 struct sockaddr *addr;
687 struct mbuf *control;
690 register struct udpiphdr *ui;
691 register int len = m->m_pkthdr.len;
692 struct in_addr faddr, laddr;
694 struct sockaddr_in *sin, src;
696 u_short fport, lport;
699 mac_create_mbuf_from_socket(inp->inp_socket, m);
702 if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) {
709 src.sin_addr.s_addr = INADDR_ANY;
710 if (control != NULL) {
712 * XXX: Currently, we assume all the optional information
713 * is stored in a single mbuf.
715 if (control->m_next) {
720 for (; control->m_len > 0;
721 control->m_data += CMSG_ALIGN(cm->cmsg_len),
722 control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
723 cm = mtod(control, struct cmsghdr *);
724 if (control->m_len < sizeof(*cm) || cm->cmsg_len == 0 ||
725 cm->cmsg_len > control->m_len) {
729 if (cm->cmsg_level != IPPROTO_IP)
732 switch (cm->cmsg_type) {
735 CMSG_LEN(sizeof(struct in_addr))) {
739 bzero(&src, sizeof(src));
740 src.sin_family = AF_INET;
741 src.sin_len = sizeof(src);
742 src.sin_port = inp->inp_lport;
743 src.sin_addr = *(struct in_addr *)CMSG_DATA(cm);
756 laddr = inp->inp_laddr;
757 lport = inp->inp_lport;
758 if (src.sin_addr.s_addr != INADDR_ANY) {
763 error = in_pcbbind_setup(inp, (struct sockaddr *)&src,
764 &laddr.s_addr, &lport, td);
770 sin = (struct sockaddr_in *)addr;
771 if (td && jailed(td->td_ucred))
772 prison_remote_ip(td->td_ucred, 0, &sin->sin_addr.s_addr);
773 if (inp->inp_faddr.s_addr != INADDR_ANY) {
777 error = in_pcbconnect_setup(inp, addr, &laddr.s_addr, &lport,
778 &faddr.s_addr, &fport, NULL, td);
782 /* Commit the local port if newly assigned. */
783 if (inp->inp_laddr.s_addr == INADDR_ANY &&
784 inp->inp_lport == 0) {
785 inp->inp_lport = lport;
786 if (in_pcbinshash(inp) != 0) {
791 inp->inp_flags |= INP_ANONPORT;
794 faddr = inp->inp_faddr;
795 fport = inp->inp_fport;
796 if (faddr.s_addr == INADDR_ANY) {
802 * Calculate data length and get a mbuf
803 * for UDP and IP headers.
805 M_PREPEND(m, sizeof(struct udpiphdr), M_DONTWAIT);
812 * Fill in mbuf with extended UDP header
813 * and addresses and length put into network format.
815 ui = mtod(m, struct udpiphdr *);
816 bzero(ui->ui_x1, sizeof(ui->ui_x1)); /* XXX still needed? */
817 ui->ui_pr = IPPROTO_UDP;
820 ui->ui_sport = lport;
821 ui->ui_dport = fport;
822 ui->ui_ulen = htons((u_short)len + sizeof(struct udphdr));
825 * Set up checksum and output datagram.
828 ui->ui_sum = in_pseudo(ui->ui_src.s_addr, ui->ui_dst.s_addr,
829 htons((u_short)len + sizeof(struct udphdr) + IPPROTO_UDP));
830 m->m_pkthdr.csum_flags = CSUM_UDP;
831 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
835 ((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len;
836 ((struct ip *)ui)->ip_ttl = inp->inp_ip_ttl; /* XXX */
837 ((struct ip *)ui)->ip_tos = inp->inp_ip_tos; /* XXX */
838 udpstat.udps_opackets++;
840 error = ip_output(m, inp->inp_options, &inp->inp_route,
841 (inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST)),
842 inp->inp_moptions, inp);
850 u_long udp_sendspace = 9216; /* really max datagram size */
851 /* 40 1K datagrams */
852 SYSCTL_INT(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram, CTLFLAG_RW,
853 &udp_sendspace, 0, "Maximum outgoing UDP datagram size");
855 u_long udp_recvspace = 40 * (1024 +
857 sizeof(struct sockaddr_in6)
859 sizeof(struct sockaddr_in)
862 SYSCTL_INT(_net_inet_udp, UDPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
863 &udp_recvspace, 0, "Maximum incoming UDP datagram size");
866 udp_abort(struct socket *so)
871 INP_INFO_WLOCK(&udbinfo);
874 INP_INFO_WUNLOCK(&udbinfo);
875 return EINVAL; /* ??? possible? panic instead? */
878 soisdisconnected(so);
881 INP_INFO_WUNLOCK(&udbinfo);
887 udp_attach(struct socket *so, int proto, struct thread *td)
892 INP_INFO_WLOCK(&udbinfo);
895 INP_INFO_WUNLOCK(&udbinfo);
898 error = soreserve(so, udp_sendspace, udp_recvspace);
900 INP_INFO_WUNLOCK(&udbinfo);
904 error = in_pcballoc(so, &udbinfo, td);
909 inp = (struct inpcb *)so->so_pcb;
911 INP_INFO_WUNLOCK(&udbinfo);
912 inp->inp_vflag |= INP_IPV4;
913 inp->inp_ip_ttl = ip_defttl;
919 udp_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
924 INP_INFO_WLOCK(&udbinfo);
927 INP_INFO_WUNLOCK(&udbinfo);
932 error = in_pcbbind(inp, nam, td);
935 INP_INFO_WUNLOCK(&udbinfo);
940 udp_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
944 struct sockaddr_in *sin;
946 INP_INFO_WLOCK(&udbinfo);
949 INP_INFO_WUNLOCK(&udbinfo);
953 if (inp->inp_faddr.s_addr != INADDR_ANY) {
955 INP_INFO_WUNLOCK(&udbinfo);
959 sin = (struct sockaddr_in *)nam;
960 if (td && jailed(td->td_ucred))
961 prison_remote_ip(td->td_ucred, 0, &sin->sin_addr.s_addr);
962 error = in_pcbconnect(inp, nam, td);
967 INP_INFO_WUNLOCK(&udbinfo);
972 udp_detach(struct socket *so)
977 INP_INFO_WLOCK(&udbinfo);
980 INP_INFO_WUNLOCK(&udbinfo);
986 INP_INFO_WUNLOCK(&udbinfo);
992 udp_disconnect(struct socket *so)
997 INP_INFO_WLOCK(&udbinfo);
1000 INP_INFO_WUNLOCK(&udbinfo);
1004 if (inp->inp_faddr.s_addr == INADDR_ANY) {
1005 INP_INFO_WUNLOCK(&udbinfo);
1011 in_pcbdisconnect(inp);
1012 inp->inp_laddr.s_addr = INADDR_ANY;
1014 INP_INFO_WUNLOCK(&udbinfo);
1016 so->so_state &= ~SS_ISCONNECTED; /* XXX */
1021 udp_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
1022 struct mbuf *control, struct thread *td)
1027 INP_INFO_WLOCK(&udbinfo);
1028 inp = sotoinpcb(so);
1030 INP_INFO_WUNLOCK(&udbinfo);
1035 ret = udp_output(inp, m, addr, control, td);
1037 INP_INFO_WUNLOCK(&udbinfo);
1042 udp_shutdown(struct socket *so)
1046 INP_INFO_RLOCK(&udbinfo);
1047 inp = sotoinpcb(so);
1049 INP_INFO_RUNLOCK(&udbinfo);
1053 INP_INFO_RUNLOCK(&udbinfo);
1060 * This is the wrapper function for in_setsockaddr. We just pass down
1061 * the pcbinfo for in_setsockaddr to lock. We don't want to do the locking
1062 * here because in_setsockaddr will call malloc and might block.
1065 udp_sockaddr(struct socket *so, struct sockaddr **nam)
1067 return (in_setsockaddr(so, nam, &udbinfo));
1071 * This is the wrapper function for in_setpeeraddr. We just pass down
1072 * the pcbinfo for in_setpeeraddr to lock.
1075 udp_peeraddr(struct socket *so, struct sockaddr **nam)
1077 return (in_setpeeraddr(so, nam, &udbinfo));
1080 struct pr_usrreqs udp_usrreqs = {
1081 udp_abort, pru_accept_notsupp, udp_attach, udp_bind, udp_connect,
1082 pru_connect2_notsupp, in_control, udp_detach, udp_disconnect,
1083 pru_listen_notsupp, udp_peeraddr, pru_rcvd_notsupp,
1084 pru_rcvoob_notsupp, udp_send, pru_sense_null, udp_shutdown,
1085 udp_sockaddr, sosend, soreceive, sopoll