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"
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/domain.h>
44 #include <sys/kernel.h>
46 #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>
74 #include <netinet6/ip6_var.h>
76 #include <netinet/udp.h>
77 #include <netinet/udp_var.h>
80 #include <netinet6/ipsec.h>
83 #include <machine/in_cksum.h>
86 * UDP protocol implementation.
87 * Per RFC 768, August, 1980.
90 static int udpcksum = 1;
92 static int udpcksum = 0; /* XXX */
94 SYSCTL_INT(_net_inet_udp, UDPCTL_CHECKSUM, checksum, CTLFLAG_RW,
98 SYSCTL_INT(_net_inet_udp, OID_AUTO, log_in_vain, CTLFLAG_RW,
99 &log_in_vain, 0, "Log all incoming UDP packets");
101 static int blackhole = 0;
102 SYSCTL_INT(_net_inet_udp, OID_AUTO, blackhole, CTLFLAG_RW,
103 &blackhole, 0, "Do not send port unreachables for refused connects");
105 struct inpcbhead udb; /* from udp_var.h */
106 #define udb6 udb /* for KAME src sync over BSD*'s */
107 struct inpcbinfo udbinfo;
110 #define UDBHASHSIZE 16
113 struct udpstat udpstat; /* from udp_var.h */
114 SYSCTL_STRUCT(_net_inet_udp, UDPCTL_STATS, stats, CTLFLAG_RW,
115 &udpstat, udpstat, "UDP statistics (struct udpstat, netinet/udp_var.h)");
117 static struct sockaddr_in udp_in = { sizeof(udp_in), AF_INET };
120 struct sockaddr_in6 uin6_sin;
121 u_char uin6_init_done : 1;
123 { sizeof(udp_in6.uin6_sin), AF_INET6 },
127 struct ip6_hdr uip6_ip6;
128 u_char uip6_init_done : 1;
132 static void udp_append(struct inpcb *last, struct ip *ip, struct mbuf *n,
135 static void ip_2_ip6_hdr(struct ip6_hdr *ip6, struct ip *ip);
138 static int udp_detach(struct socket *so);
139 static int udp_output(struct inpcb *, struct mbuf *, struct sockaddr *,
140 struct mbuf *, struct thread *);
145 INP_INFO_LOCK_INIT(&udbinfo, "udp");
147 udbinfo.listhead = &udb;
148 udbinfo.hashbase = hashinit(UDBHASHSIZE, M_PCB, &udbinfo.hashmask);
149 udbinfo.porthashbase = hashinit(UDBHASHSIZE, M_PCB,
150 &udbinfo.porthashmask);
151 udbinfo.ipi_zone = uma_zcreate("udpcb", sizeof(struct inpcb), NULL,
152 NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
153 uma_zone_set_max(udbinfo.ipi_zone, maxsockets);
158 register struct mbuf *m;
162 register struct ip *ip;
163 register struct udphdr *uh;
164 register struct inpcb *inp;
165 struct mbuf *opts = 0;
168 struct sockaddr *append_sa;
170 udpstat.udps_ipackets++;
173 * Strip IP options, if any; should skip this,
174 * make available to user, and use on returned packets,
175 * but we don't yet have a way to check the checksum
176 * with options still present.
178 if (iphlen > sizeof (struct ip)) {
179 ip_stripoptions(m, (struct mbuf *)0);
180 iphlen = sizeof(struct ip);
184 * Get IP and UDP header together in first mbuf.
186 ip = mtod(m, struct ip *);
187 if (m->m_len < iphlen + sizeof(struct udphdr)) {
188 if ((m = m_pullup(m, iphlen + sizeof(struct udphdr))) == 0) {
189 udpstat.udps_hdrops++;
192 ip = mtod(m, struct ip *);
194 uh = (struct udphdr *)((caddr_t)ip + iphlen);
196 /* destination port of 0 is illegal, based on RFC768. */
197 if (uh->uh_dport == 0)
201 * Make mbuf data length reflect UDP length.
202 * If not enough data to reflect UDP length, drop.
204 len = ntohs((u_short)uh->uh_ulen);
205 if (ip->ip_len != len) {
206 if (len > ip->ip_len || len < sizeof(struct udphdr)) {
207 udpstat.udps_badlen++;
210 m_adj(m, len - ip->ip_len);
211 /* ip->ip_len = len; */
214 * Save a copy of the IP header in case we want restore it
215 * for sending an ICMP error message in response.
221 * Checksum extended UDP header and data.
224 if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
225 if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
226 uh->uh_sum = m->m_pkthdr.csum_data;
228 uh->uh_sum = in_pseudo(ip->ip_src.s_addr,
229 ip->ip_dst.s_addr, htonl((u_short)len +
230 m->m_pkthdr.csum_data + IPPROTO_UDP));
231 uh->uh_sum ^= 0xffff;
234 bcopy(((struct ipovly *)ip)->ih_x1, b, 9);
235 bzero(((struct ipovly *)ip)->ih_x1, 9);
236 ((struct ipovly *)ip)->ih_len = uh->uh_ulen;
237 uh->uh_sum = in_cksum(m, len + sizeof (struct ip));
238 bcopy(b, ((struct ipovly *)ip)->ih_x1, 9);
241 udpstat.udps_badsum++;
246 udpstat.udps_nosum++;
248 INP_INFO_RLOCK(&udbinfo);
250 if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
251 in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) {
254 * Deliver a multicast or broadcast datagram to *all* sockets
255 * for which the local and remote addresses and ports match
256 * those of the incoming datagram. This allows more than
257 * one process to receive multi/broadcasts on the same port.
258 * (This really ought to be done for unicast datagrams as
259 * well, but that would cause problems with existing
260 * applications that open both address-specific sockets and
261 * a wildcard socket listening to the same port -- they would
262 * end up receiving duplicates of every unicast datagram.
263 * Those applications open the multiple sockets to overcome an
264 * inadequacy of the UDP socket interface, but for backwards
265 * compatibility we avoid the problem here rather than
266 * fixing the interface. Maybe 4.5BSD will remedy this?)
270 * Construct sockaddr format source address.
272 udp_in.sin_port = uh->uh_sport;
273 udp_in.sin_addr = ip->ip_src;
275 * Locate pcb(s) for datagram.
276 * (Algorithm copied from raw_intr().)
280 udp_in6.uin6_init_done = udp_ip6.uip6_init_done = 0;
282 LIST_FOREACH(inp, &udb, inp_list) {
284 if (inp->inp_lport != uh->uh_dport) {
290 if ((inp->inp_vflag & INP_IPV4) == 0)
293 if (inp->inp_laddr.s_addr != INADDR_ANY) {
294 if (inp->inp_laddr.s_addr != ip->ip_dst.s_addr)
297 if (inp->inp_faddr.s_addr != INADDR_ANY) {
298 if (inp->inp_faddr.s_addr !=
300 inp->inp_fport != uh->uh_sport)
308 /* check AH/ESP integrity. */
309 if (ipsec4_in_reject_so(m, last->inp_socket))
310 ipsecstat.in_polvio++;
311 /* do not inject data to pcb */
314 if ((n = m_copy(m, 0, M_COPYALL)) != NULL)
315 udp_append(last, ip, n,
317 sizeof(struct udphdr));
322 * Don't look for additional matches if this one does
323 * not have either the SO_REUSEPORT or SO_REUSEADDR
324 * socket options set. This heuristic avoids searching
325 * through all pcbs in the common case of a non-shared
326 * port. It * assumes that an application will never
327 * clear these options after setting them.
329 if ((last->inp_socket->so_options&(SO_REUSEPORT|SO_REUSEADDR)) == 0)
335 * No matching pcb found; discard datagram.
336 * (No need to send an ICMP Port Unreachable
337 * for a broadcast or multicast datgram.)
339 udpstat.udps_noportbcast++;
343 /* check AH/ESP integrity. */
344 if (ipsec4_in_reject_so(m, last->inp_socket)) {
345 ipsecstat.in_polvio++;
350 INP_INFO_RUNLOCK(&udbinfo);
351 udp_append(last, ip, m, iphlen + sizeof(struct udphdr));
355 * Locate pcb for datagram.
357 inp = in_pcblookup_hash(&udbinfo, ip->ip_src, uh->uh_sport,
358 ip->ip_dst, uh->uh_dport, 1, m->m_pkthdr.rcvif);
361 char buf[4*sizeof "123"];
363 strcpy(buf, inet_ntoa(ip->ip_dst));
365 "Connection attempt to UDP %s:%d from %s:%d\n",
366 buf, ntohs(uh->uh_dport), inet_ntoa(ip->ip_src),
367 ntohs(uh->uh_sport));
369 udpstat.udps_noport++;
370 if (m->m_flags & (M_BCAST | M_MCAST)) {
371 udpstat.udps_noportbcast++;
374 if (badport_bandlim(BANDLIM_ICMP_UNREACH) < 0)
379 ip->ip_len += iphlen;
380 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
381 INP_INFO_RUNLOCK(&udbinfo);
385 INP_INFO_RUNLOCK(&udbinfo);
387 if (ipsec4_in_reject_so(m, inp->inp_socket)) {
388 ipsecstat.in_polvio++;
394 * Construct sockaddr format source address.
395 * Stuff source address and datagram in user buffer.
397 udp_in.sin_port = uh->uh_sport;
398 udp_in.sin_addr = ip->ip_src;
399 if (inp->inp_flags & INP_CONTROLOPTS
400 || inp->inp_socket->so_options & SO_TIMESTAMP) {
402 if (inp->inp_vflag & INP_IPV6) {
405 ip_2_ip6_hdr(&udp_ip6.uip6_ip6, ip);
406 savedflags = inp->inp_flags;
407 inp->inp_flags &= ~INP_UNMAPPABLEOPTS;
408 ip6_savecontrol(inp, &opts, &udp_ip6.uip6_ip6, m);
409 inp->inp_flags = savedflags;
412 ip_savecontrol(inp, &opts, ip, m);
414 m_adj(m, iphlen + sizeof(struct udphdr));
416 if (inp->inp_vflag & INP_IPV6) {
417 in6_sin_2_v4mapsin6(&udp_in, &udp_in6.uin6_sin);
418 append_sa = (struct sockaddr *)&udp_in6;
421 append_sa = (struct sockaddr *)&udp_in;
422 if (sbappendaddr(&inp->inp_socket->so_rcv, append_sa, m, opts) == 0) {
423 udpstat.udps_fullsock++;
426 sorwakeup(inp->inp_socket);
431 INP_INFO_RUNLOCK(&udbinfo);
444 ip_2_ip6_hdr(ip6, ip)
448 bzero(ip6, sizeof(*ip6));
450 ip6->ip6_vfc = IPV6_VERSION;
451 ip6->ip6_plen = ip->ip_len;
452 ip6->ip6_nxt = ip->ip_p;
453 ip6->ip6_hlim = ip->ip_ttl;
454 ip6->ip6_src.s6_addr32[2] = ip6->ip6_dst.s6_addr32[2] =
456 ip6->ip6_src.s6_addr32[3] = ip->ip_src.s_addr;
457 ip6->ip6_dst.s6_addr32[3] = ip->ip_dst.s_addr;
462 * subroutine of udp_input(), mainly for source code readability.
463 * caller must properly init udp_ip6 and udp_in6 beforehand.
466 udp_append(last, ip, n, off)
472 struct sockaddr *append_sa;
473 struct mbuf *opts = 0;
475 if (last->inp_flags & INP_CONTROLOPTS ||
476 last->inp_socket->so_options & SO_TIMESTAMP) {
478 if (last->inp_vflag & INP_IPV6) {
481 if (udp_ip6.uip6_init_done == 0) {
482 ip_2_ip6_hdr(&udp_ip6.uip6_ip6, ip);
483 udp_ip6.uip6_init_done = 1;
485 savedflags = last->inp_flags;
486 last->inp_flags &= ~INP_UNMAPPABLEOPTS;
487 ip6_savecontrol(last, &opts, &udp_ip6.uip6_ip6, n);
488 last->inp_flags = savedflags;
491 ip_savecontrol(last, &opts, ip, n);
494 if (last->inp_vflag & INP_IPV6) {
495 if (udp_in6.uin6_init_done == 0) {
496 in6_sin_2_v4mapsin6(&udp_in, &udp_in6.uin6_sin);
497 udp_in6.uin6_init_done = 1;
499 append_sa = (struct sockaddr *)&udp_in6.uin6_sin;
502 append_sa = (struct sockaddr *)&udp_in;
504 if (sbappendaddr(&last->inp_socket->so_rcv, append_sa, n, opts) == 0) {
508 udpstat.udps_fullsock++;
510 sorwakeup(last->inp_socket);
514 * Notify a udp user of an asynchronous error;
515 * just wake up so that he can collect error status.
518 udp_notify(inp, errno)
519 register struct inpcb *inp;
522 inp->inp_socket->so_error = errno;
523 sorwakeup(inp->inp_socket);
524 sowwakeup(inp->inp_socket);
529 udp_ctlinput(cmd, sa, vip)
536 struct inpcb *(*notify)(struct inpcb *, int) = udp_notify;
537 struct in_addr faddr;
541 faddr = ((struct sockaddr_in *)sa)->sin_addr;
542 if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY)
545 if (PRC_IS_REDIRECT(cmd)) {
547 notify = in_rtchange;
548 } else if (cmd == PRC_HOSTDEAD)
550 else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0)
554 uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
555 INP_INFO_RLOCK(&udbinfo);
556 inp = in_pcblookup_hash(&udbinfo, faddr, uh->uh_dport,
557 ip->ip_src, uh->uh_sport, 0, NULL);
560 if(inp->inp_socket != NULL) {
561 (*notify)(inp, inetctlerrmap[cmd]);
565 INP_INFO_RUNLOCK(&udbinfo);
568 in_pcbnotifyall(&udbinfo, faddr, inetctlerrmap[cmd], notify);
572 udp_pcblist(SYSCTL_HANDLER_ARGS)
575 struct inpcb *inp, **inp_list;
580 * The process of preparing the TCB list is too time-consuming and
581 * resource-intensive to repeat twice on every request.
583 if (req->oldptr == 0) {
584 n = udbinfo.ipi_count;
585 req->oldidx = 2 * (sizeof xig)
586 + (n + n/8) * sizeof(struct xinpcb);
590 if (req->newptr != 0)
594 * OK, now we're committed to doing something.
597 gencnt = udbinfo.ipi_gencnt;
598 n = udbinfo.ipi_count;
601 sysctl_wire_old_buffer(req, 2 * (sizeof xig)
602 + n * sizeof(struct xinpcb));
604 xig.xig_len = sizeof xig;
606 xig.xig_gen = gencnt;
607 xig.xig_sogen = so_gencnt;
608 error = SYSCTL_OUT(req, &xig, sizeof xig);
612 inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK);
617 INP_INFO_RLOCK(&udbinfo);
618 for (inp = LIST_FIRST(udbinfo.listhead), i = 0; inp && i < n;
619 inp = LIST_NEXT(inp, inp_list)) {
621 if (inp->inp_gencnt <= gencnt &&
622 cr_canseesocket(req->td->td_ucred, inp->inp_socket) == 0)
626 INP_INFO_RUNLOCK(&udbinfo);
631 for (i = 0; i < n; i++) {
634 if (inp->inp_gencnt <= gencnt) {
636 xi.xi_len = sizeof xi;
637 /* XXX should avoid extra copy */
638 bcopy(inp, &xi.xi_inp, sizeof *inp);
640 sotoxsocket(inp->inp_socket, &xi.xi_socket);
641 error = SYSCTL_OUT(req, &xi, sizeof xi);
647 * Give the user an updated idea of our state.
648 * If the generation differs from what we told
649 * her before, she knows that something happened
650 * while we were processing this request, and it
651 * might be necessary to retry.
654 INP_INFO_RLOCK(&udbinfo);
655 xig.xig_gen = udbinfo.ipi_gencnt;
656 xig.xig_sogen = so_gencnt;
657 xig.xig_count = udbinfo.ipi_count;
658 INP_INFO_RUNLOCK(&udbinfo);
660 error = SYSCTL_OUT(req, &xig, sizeof xig);
662 free(inp_list, M_TEMP);
666 SYSCTL_PROC(_net_inet_udp, UDPCTL_PCBLIST, pcblist, CTLFLAG_RD, 0, 0,
667 udp_pcblist, "S,xinpcb", "List of active UDP sockets");
670 udp_getcred(SYSCTL_HANDLER_ARGS)
673 struct sockaddr_in addrs[2];
677 error = suser_cred(req->td->td_ucred, PRISON_ROOT);
680 error = SYSCTL_IN(req, addrs, sizeof(addrs));
684 INP_INFO_RLOCK(&udbinfo);
685 inp = in_pcblookup_hash(&udbinfo, addrs[1].sin_addr, addrs[1].sin_port,
686 addrs[0].sin_addr, addrs[0].sin_port, 1, NULL);
687 if (inp == NULL || inp->inp_socket == NULL) {
691 error = cr_canseesocket(req->td->td_ucred, inp->inp_socket);
694 cru2x(inp->inp_socket->so_cred, &xuc);
696 INP_INFO_RUNLOCK(&udbinfo);
699 error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
703 SYSCTL_PROC(_net_inet_udp, OID_AUTO, getcred,
704 CTLTYPE_OPAQUE|CTLFLAG_RW|CTLFLAG_PRISON, 0, 0,
705 udp_getcred, "S,xucred", "Get the xucred of a UDP connection");
708 udp_output(inp, m, addr, control, td)
709 register struct inpcb *inp;
711 struct sockaddr *addr;
712 struct mbuf *control;
715 register struct udpiphdr *ui;
716 register int len = m->m_pkthdr.len;
717 struct in_addr laddr;
718 struct sockaddr_in *sin;
719 int s = 0, error = 0;
722 m_freem(control); /* XXX */
724 if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) {
730 sin = (struct sockaddr_in *)addr;
731 if (td && jailed(td->td_ucred))
732 prison_remote_ip(td->td_ucred, 0, &sin->sin_addr.s_addr);
733 laddr = inp->inp_laddr;
734 if (inp->inp_faddr.s_addr != INADDR_ANY) {
739 * Must block input while temporarily connected.
742 error = in_pcbconnect(inp, addr, td);
748 if (inp->inp_faddr.s_addr == INADDR_ANY) {
754 * Calculate data length and get a mbuf
755 * for UDP and IP headers.
757 M_PREPEND(m, sizeof(struct udpiphdr), M_DONTWAIT);
766 * Fill in mbuf with extended UDP header
767 * and addresses and length put into network format.
769 ui = mtod(m, struct udpiphdr *);
770 bzero(ui->ui_x1, sizeof(ui->ui_x1)); /* XXX still needed? */
771 ui->ui_pr = IPPROTO_UDP;
772 ui->ui_src = inp->inp_laddr;
773 ui->ui_dst = inp->inp_faddr;
774 ui->ui_sport = inp->inp_lport;
775 ui->ui_dport = inp->inp_fport;
776 ui->ui_ulen = htons((u_short)len + sizeof(struct udphdr));
779 * Set up checksum and output datagram.
782 ui->ui_sum = in_pseudo(ui->ui_src.s_addr, ui->ui_dst.s_addr,
783 htons((u_short)len + sizeof(struct udphdr) + IPPROTO_UDP));
784 m->m_pkthdr.csum_flags = CSUM_UDP;
785 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
789 ((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len;
790 ((struct ip *)ui)->ip_ttl = inp->inp_ip_ttl; /* XXX */
791 ((struct ip *)ui)->ip_tos = inp->inp_ip_tos; /* XXX */
792 udpstat.udps_opackets++;
795 if (ipsec_setsocket(m, inp->inp_socket) != 0) {
800 error = ip_output(m, inp->inp_options, &inp->inp_route,
801 (inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST)),
805 in_pcbdisconnect(inp);
806 inp->inp_laddr = laddr; /* XXX rehash? */
816 u_long udp_sendspace = 9216; /* really max datagram size */
817 /* 40 1K datagrams */
818 SYSCTL_INT(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram, CTLFLAG_RW,
819 &udp_sendspace, 0, "Maximum outgoing UDP datagram size");
821 u_long udp_recvspace = 40 * (1024 +
823 sizeof(struct sockaddr_in6)
825 sizeof(struct sockaddr_in)
828 SYSCTL_INT(_net_inet_udp, UDPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
829 &udp_recvspace, 0, "Maximum incoming UDP datagram size");
832 udp_abort(struct socket *so)
837 INP_INFO_WLOCK(&udbinfo);
840 INP_INFO_WUNLOCK(&udbinfo);
841 return EINVAL; /* ??? possible? panic instead? */
844 soisdisconnected(so);
847 INP_INFO_WUNLOCK(&udbinfo);
853 udp_attach(struct socket *so, int proto, struct thread *td)
858 INP_INFO_WLOCK(&udbinfo);
861 INP_INFO_WUNLOCK(&udbinfo);
864 error = soreserve(so, udp_sendspace, udp_recvspace);
866 INP_INFO_WUNLOCK(&udbinfo);
870 error = in_pcballoc(so, &udbinfo, td);
875 inp = (struct inpcb *)so->so_pcb;
877 INP_INFO_WUNLOCK(&udbinfo);
878 inp->inp_vflag |= INP_IPV4;
879 inp->inp_ip_ttl = ip_defttl;
885 udp_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
890 INP_INFO_WLOCK(&udbinfo);
893 INP_INFO_WUNLOCK(&udbinfo);
898 error = in_pcbbind(inp, nam, td);
901 INP_INFO_WUNLOCK(&udbinfo);
906 udp_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
910 struct sockaddr_in *sin;
912 INP_INFO_WLOCK(&udbinfo);
915 INP_INFO_WUNLOCK(&udbinfo);
919 if (inp->inp_faddr.s_addr != INADDR_ANY) {
921 INP_INFO_WUNLOCK(&udbinfo);
925 sin = (struct sockaddr_in *)nam;
926 if (td && jailed(td->td_ucred))
927 prison_remote_ip(td->td_ucred, 0, &sin->sin_addr.s_addr);
928 error = in_pcbconnect(inp, nam, td);
933 INP_INFO_WUNLOCK(&udbinfo);
938 udp_detach(struct socket *so)
943 INP_INFO_WLOCK(&udbinfo);
946 INP_INFO_WUNLOCK(&udbinfo);
952 INP_INFO_WUNLOCK(&udbinfo);
958 udp_disconnect(struct socket *so)
963 INP_INFO_WLOCK(&udbinfo);
966 INP_INFO_WUNLOCK(&udbinfo);
970 if (inp->inp_faddr.s_addr == INADDR_ANY) {
971 INP_INFO_WUNLOCK(&udbinfo);
977 in_pcbdisconnect(inp);
978 inp->inp_laddr.s_addr = INADDR_ANY;
980 INP_INFO_WUNLOCK(&udbinfo);
982 so->so_state &= ~SS_ISCONNECTED; /* XXX */
987 udp_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
988 struct mbuf *control, struct thread *td)
993 INP_INFO_WLOCK(&udbinfo);
996 INP_INFO_WUNLOCK(&udbinfo);
1001 ret = udp_output(inp, m, addr, control, td);
1003 INP_INFO_WUNLOCK(&udbinfo);
1008 udp_shutdown(struct socket *so)
1012 INP_INFO_RLOCK(&udbinfo);
1013 inp = sotoinpcb(so);
1015 INP_INFO_RUNLOCK(&udbinfo);
1019 INP_INFO_RUNLOCK(&udbinfo);
1026 * This is the wrapper function for in_setsockaddr. We just pass down
1027 * the pcbinfo for in_setsockaddr to lock. We don't want to do the locking
1028 * here because in_setsockaddr will call malloc and might block.
1031 udp_sockaddr(struct socket *so, struct sockaddr **nam)
1033 return (in_setsockaddr(so, nam, &udbinfo));
1037 * This is the wrapper function for in_setpeeraddr. We just pass down
1038 * the pcbinfo for in_setpeeraddr to lock.
1041 udp_peeraddr(struct socket *so, struct sockaddr **nam)
1043 return (in_setpeeraddr(so, nam, &udbinfo));
1046 struct pr_usrreqs udp_usrreqs = {
1047 udp_abort, pru_accept_notsupp, udp_attach, udp_bind, udp_connect,
1048 pru_connect2_notsupp, in_control, udp_detach, udp_disconnect,
1049 pru_listen_notsupp, udp_peeraddr, pru_rcvd_notsupp,
1050 pru_rcvoob_notsupp, udp_send, pru_sense_null, udp_shutdown,
1051 udp_sockaddr, sosend, soreceive, sopoll