The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/netinet/udp_usrreq.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * Copyright (c) 1982, 1986, 1988, 1990, 1993, 1995
    3  *      The Regents of the University of California.
    4  * Copyright (c) 2008 Robert N. M. Watson
    5  * All rights reserved.
    6  *
    7  * Redistribution and use in source and binary forms, with or without
    8  * modification, are permitted provided that the following conditions
    9  * are met:
   10  * 1. Redistributions of source code must retain the above copyright
   11  *    notice, this list of conditions and the following disclaimer.
   12  * 2. Redistributions in binary form must reproduce the above copyright
   13  *    notice, this list of conditions and the following disclaimer in the
   14  *    documentation and/or other materials provided with the distribution.
   15  * 4. Neither the name of the University nor the names of its contributors
   16  *    may be used to endorse or promote products derived from this software
   17  *    without specific prior written permission.
   18  *
   19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   29  * SUCH DAMAGE.
   30  *
   31  *      @(#)udp_usrreq.c        8.6 (Berkeley) 5/23/95
   32  */
   33 
   34 #include <sys/cdefs.h>
   35 __FBSDID("$FreeBSD$");
   36 
   37 #include "opt_ipfw.h"
   38 #include "opt_inet6.h"
   39 #include "opt_ipsec.h"
   40 #include "opt_mac.h"
   41 
   42 #include <sys/param.h>
   43 #include <sys/domain.h>
   44 #include <sys/eventhandler.h>
   45 #include <sys/jail.h>
   46 #include <sys/kernel.h>
   47 #include <sys/lock.h>
   48 #include <sys/malloc.h>
   49 #include <sys/mbuf.h>
   50 #include <sys/priv.h>
   51 #include <sys/proc.h>
   52 #include <sys/protosw.h>
   53 #include <sys/signalvar.h>
   54 #include <sys/socket.h>
   55 #include <sys/socketvar.h>
   56 #include <sys/sx.h>
   57 #include <sys/sysctl.h>
   58 #include <sys/syslog.h>
   59 #include <sys/systm.h>
   60 
   61 #include <vm/uma.h>
   62 
   63 #include <net/if.h>
   64 #include <net/route.h>
   65 
   66 #include <netinet/in.h>
   67 #include <netinet/in_pcb.h>
   68 #include <netinet/in_systm.h>
   69 #include <netinet/in_var.h>
   70 #include <netinet/ip.h>
   71 #ifdef INET6
   72 #include <netinet/ip6.h>
   73 #endif
   74 #include <netinet/ip_icmp.h>
   75 #include <netinet/icmp_var.h>
   76 #include <netinet/ip_var.h>
   77 #include <netinet/ip_options.h>
   78 #ifdef INET6
   79 #include <netinet6/ip6_var.h>
   80 #endif
   81 #include <netinet/udp.h>
   82 #include <netinet/udp_var.h>
   83 #ifdef INET6
   84 #include <netinet6/udp6_var.h>
   85 #endif
   86 
   87 #ifdef IPSEC
   88 #include <netipsec/ipsec.h>
   89 #endif
   90 
   91 #include <machine/in_cksum.h>
   92 
   93 #include <security/mac/mac_framework.h>
   94 
   95 /*
   96  * UDP protocol implementation.
   97  * Per RFC 768, August, 1980.
   98  */
   99 
  100 /*
  101  * BSD 4.2 defaulted the udp checksum to be off.  Turning off udp checksums
  102  * removes the only data integrity mechanism for packets and malformed
  103  * packets that would otherwise be discarded due to bad checksums, and may
  104  * cause problems (especially for NFS data blocks).
  105  */
  106 static int      udp_cksum = 1;
  107 SYSCTL_INT(_net_inet_udp, UDPCTL_CHECKSUM, checksum, CTLFLAG_RW, &udp_cksum,
  108     0, "");
  109 
  110 int     udp_log_in_vain = 0;
  111 SYSCTL_INT(_net_inet_udp, OID_AUTO, log_in_vain, CTLFLAG_RW,
  112     &udp_log_in_vain, 0, "Log all incoming UDP packets");
  113 
  114 int     udp_blackhole = 0;
  115 SYSCTL_INT(_net_inet_udp, OID_AUTO, blackhole, CTLFLAG_RW, &udp_blackhole, 0,
  116     "Do not send port unreachables for refused connects");
  117 
  118 u_long  udp_sendspace = 9216;           /* really max datagram size */
  119                                         /* 40 1K datagrams */
  120 SYSCTL_ULONG(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram, CTLFLAG_RW,
  121     &udp_sendspace, 0, "Maximum outgoing UDP datagram size");
  122 
  123 u_long  udp_recvspace = 40 * (1024 +
  124 #ifdef INET6
  125                                       sizeof(struct sockaddr_in6)
  126 #else
  127                                       sizeof(struct sockaddr_in)
  128 #endif
  129                                       );
  130 
  131 SYSCTL_ULONG(_net_inet_udp, UDPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
  132     &udp_recvspace, 0, "Maximum space for incoming UDP datagrams");
  133 
  134 static int udp_soreceive_dgram;
  135 SYSCTL_INT(_net_inet_udp, OID_AUTO, soreceive_dgram_enabled,
  136     CTLFLAG_RD | CTLFLAG_TUN, &udp_soreceive_dgram, 0,
  137     "Use experimental optimized datagram receive");
  138 
  139 struct inpcbhead        udb;            /* from udp_var.h */
  140 struct inpcbinfo        udbinfo;
  141 
  142 #ifndef UDBHASHSIZE
  143 #define UDBHASHSIZE     128
  144 #endif
  145 
  146 struct udpstat  udpstat;        /* from udp_var.h */
  147 SYSCTL_STRUCT(_net_inet_udp, UDPCTL_STATS, stats, CTLFLAG_RW, &udpstat,
  148     udpstat, "UDP statistics (struct udpstat, netinet/udp_var.h)");
  149 
  150 static void     udp_detach(struct socket *so);
  151 static int      udp_output(struct inpcb *, struct mbuf *, struct sockaddr *,
  152                     struct mbuf *, struct thread *);
  153 
  154 static void
  155 udp_zone_change(void *tag)
  156 {
  157 
  158         uma_zone_set_max(udbinfo.ipi_zone, maxsockets);
  159 }
  160 
  161 static int
  162 udp_inpcb_init(void *mem, int size, int flags)
  163 {
  164         struct inpcb *inp;
  165 
  166         inp = mem;
  167         INP_LOCK_INIT(inp, "inp", "udpinp");
  168         return (0);
  169 }
  170 
  171 void
  172 udp_init(void)
  173 {
  174 
  175         INP_INFO_LOCK_INIT(&udbinfo, "udp");
  176         LIST_INIT(&udb);
  177         udbinfo.ipi_listhead = &udb;
  178         udbinfo.ipi_hashbase = hashinit(UDBHASHSIZE, M_PCB,
  179             &udbinfo.ipi_hashmask);
  180         udbinfo.ipi_porthashbase = hashinit(UDBHASHSIZE, M_PCB,
  181             &udbinfo.ipi_porthashmask);
  182         udbinfo.ipi_zone = uma_zcreate("udpcb", sizeof(struct inpcb), NULL,
  183             NULL, udp_inpcb_init, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
  184         uma_zone_set_max(udbinfo.ipi_zone, maxsockets);
  185         EVENTHANDLER_REGISTER(maxsockets_change, udp_zone_change, NULL,
  186             EVENTHANDLER_PRI_ANY);
  187         TUNABLE_INT_FETCH("net.inet.udp.soreceive_dgram_enabled",
  188             &udp_soreceive_dgram);
  189         if (udp_soreceive_dgram) {
  190                 udp_usrreqs.pru_soreceive = soreceive_dgram;
  191 #ifdef INET6
  192                 udp6_usrreqs.pru_soreceive = soreceive_dgram;
  193 #endif
  194         }
  195 }
  196 
  197 /*
  198  * Subroutine of udp_input(), which appends the provided mbuf chain to the
  199  * passed pcb/socket.  The caller must provide a sockaddr_in via udp_in that
  200  * contains the source address.  If the socket ends up being an IPv6 socket,
  201  * udp_append() will convert to a sockaddr_in6 before passing the address
  202  * into the socket code.
  203  */
  204 static void
  205 udp_append(struct inpcb *inp, struct ip *ip, struct mbuf *n, int off,
  206     struct sockaddr_in *udp_in)
  207 {
  208         struct sockaddr *append_sa;
  209         struct socket *so;
  210         struct mbuf *opts = 0;
  211 #ifdef INET6
  212         struct sockaddr_in6 udp_in6;
  213 #endif
  214 
  215         INP_RLOCK_ASSERT(inp);
  216 
  217 #ifdef IPSEC
  218         /* Check AH/ESP integrity. */
  219         if (ipsec4_in_reject(n, inp)) {
  220                 m_freem(n);
  221                 ipsec4stat.in_polvio++;
  222                 return;
  223         }
  224 #endif /* IPSEC */
  225 #ifdef MAC
  226         if (mac_check_inpcb_deliver(inp, n) != 0) {
  227                 m_freem(n);
  228                 return;
  229         }
  230 #endif
  231         if (inp->inp_flags & INP_CONTROLOPTS ||
  232             inp->inp_socket->so_options & (SO_TIMESTAMP | SO_BINTIME)) {
  233 #ifdef INET6
  234                 if (inp->inp_vflag & INP_IPV6)
  235                         (void)ip6_savecontrol_v4(inp, n, &opts, NULL);
  236                 else
  237 #endif
  238                         ip_savecontrol(inp, &opts, ip, n);
  239         }
  240 #ifdef INET6
  241         if (inp->inp_vflag & INP_IPV6) {
  242                 bzero(&udp_in6, sizeof(udp_in6));
  243                 udp_in6.sin6_len = sizeof(udp_in6);
  244                 udp_in6.sin6_family = AF_INET6;
  245                 in6_sin_2_v4mapsin6(udp_in, &udp_in6);
  246                 append_sa = (struct sockaddr *)&udp_in6;
  247         } else
  248 #endif
  249                 append_sa = (struct sockaddr *)udp_in;
  250         m_adj(n, off);
  251 
  252         so = inp->inp_socket;
  253         SOCKBUF_LOCK(&so->so_rcv);
  254         if (sbappendaddr_locked(&so->so_rcv, append_sa, n, opts) == 0) {
  255                 SOCKBUF_UNLOCK(&so->so_rcv);
  256                 m_freem(n);
  257                 if (opts)
  258                         m_freem(opts);
  259                 udpstat.udps_fullsock++;
  260         } else
  261                 sorwakeup_locked(so);
  262 }
  263 
  264 void
  265 udp_input(struct mbuf *m, int off)
  266 {
  267         int iphlen = off;
  268         struct ip *ip;
  269         struct udphdr *uh;
  270         struct ifnet *ifp;
  271         struct inpcb *inp;
  272         int len;
  273         struct ip save_ip;
  274         struct sockaddr_in udp_in;
  275 #ifdef IPFIREWALL_FORWARD
  276         struct m_tag *fwd_tag;
  277 #endif
  278 
  279         ifp = m->m_pkthdr.rcvif;
  280         udpstat.udps_ipackets++;
  281 
  282         /*
  283          * Strip IP options, if any; should skip this, make available to
  284          * user, and use on returned packets, but we don't yet have a way to
  285          * check the checksum with options still present.
  286          */
  287         if (iphlen > sizeof (struct ip)) {
  288                 ip_stripoptions(m, (struct mbuf *)0);
  289                 iphlen = sizeof(struct ip);
  290         }
  291 
  292         /*
  293          * Get IP and UDP header together in first mbuf.
  294          */
  295         ip = mtod(m, struct ip *);
  296         if (m->m_len < iphlen + sizeof(struct udphdr)) {
  297                 if ((m = m_pullup(m, iphlen + sizeof(struct udphdr))) == 0) {
  298                         udpstat.udps_hdrops++;
  299                         return;
  300                 }
  301                 ip = mtod(m, struct ip *);
  302         }
  303         uh = (struct udphdr *)((caddr_t)ip + iphlen);
  304 
  305         /*
  306          * Destination port of 0 is illegal, based on RFC768.
  307          */
  308         if (uh->uh_dport == 0)
  309                 goto badunlocked;
  310 
  311         /*
  312          * Construct sockaddr format source address.  Stuff source address
  313          * and datagram in user buffer.
  314          */
  315         bzero(&udp_in, sizeof(udp_in));
  316         udp_in.sin_len = sizeof(udp_in);
  317         udp_in.sin_family = AF_INET;
  318         udp_in.sin_port = uh->uh_sport;
  319         udp_in.sin_addr = ip->ip_src;
  320 
  321         /*
  322          * Make mbuf data length reflect UDP length.  If not enough data to
  323          * reflect UDP length, drop.
  324          */
  325         len = ntohs((u_short)uh->uh_ulen);
  326         if (ip->ip_len != len) {
  327                 if (len > ip->ip_len || len < sizeof(struct udphdr)) {
  328                         udpstat.udps_badlen++;
  329                         goto badunlocked;
  330                 }
  331                 m_adj(m, len - ip->ip_len);
  332                 /* ip->ip_len = len; */
  333         }
  334 
  335         /*
  336          * Save a copy of the IP header in case we want restore it for
  337          * sending an ICMP error message in response.
  338          */
  339         if (!udp_blackhole)
  340                 save_ip = *ip;
  341         else
  342                 memset(&save_ip, 0, sizeof(save_ip));
  343 
  344         /*
  345          * Checksum extended UDP header and data.
  346          */
  347         if (uh->uh_sum) {
  348                 u_short uh_sum;
  349 
  350                 if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
  351                         if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
  352                                 uh_sum = m->m_pkthdr.csum_data;
  353                         else
  354                                 uh_sum = in_pseudo(ip->ip_src.s_addr,
  355                                     ip->ip_dst.s_addr, htonl((u_short)len +
  356                                     m->m_pkthdr.csum_data + IPPROTO_UDP));
  357                         uh_sum ^= 0xffff;
  358                 } else {
  359                         char b[9];
  360 
  361                         bcopy(((struct ipovly *)ip)->ih_x1, b, 9);
  362                         bzero(((struct ipovly *)ip)->ih_x1, 9);
  363                         ((struct ipovly *)ip)->ih_len = uh->uh_ulen;
  364                         uh_sum = in_cksum(m, len + sizeof (struct ip));
  365                         bcopy(b, ((struct ipovly *)ip)->ih_x1, 9);
  366                 }
  367                 if (uh_sum) {
  368                         udpstat.udps_badsum++;
  369                         m_freem(m);
  370                         return;
  371                 }
  372         } else
  373                 udpstat.udps_nosum++;
  374 
  375 #ifdef IPFIREWALL_FORWARD
  376         /*
  377          * Grab info from PACKET_TAG_IPFORWARD tag prepended to the chain.
  378          */
  379         fwd_tag = m_tag_find(m, PACKET_TAG_IPFORWARD, NULL);
  380         if (fwd_tag != NULL) {
  381                 struct sockaddr_in *next_hop;
  382 
  383                 /*
  384                  * Do the hack.
  385                  */
  386                 next_hop = (struct sockaddr_in *)(fwd_tag + 1);
  387                 ip->ip_dst = next_hop->sin_addr;
  388                 uh->uh_dport = ntohs(next_hop->sin_port);
  389 
  390                 /*
  391                  * Remove the tag from the packet.  We don't need it anymore.
  392                  */
  393                 m_tag_delete(m, fwd_tag);
  394         }
  395 #endif
  396 
  397         INP_INFO_RLOCK(&udbinfo);
  398         if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
  399             in_broadcast(ip->ip_dst, ifp)) {
  400                 struct inpcb *last;
  401                 struct ip_moptions *imo;
  402 
  403                 last = NULL;
  404                 LIST_FOREACH(inp, &udb, inp_list) {
  405                         if (inp->inp_lport != uh->uh_dport)
  406                                 continue;
  407 #ifdef INET6
  408                         if ((inp->inp_vflag & INP_IPV4) == 0)
  409                                 continue;
  410 #endif
  411                         if (inp->inp_laddr.s_addr != INADDR_ANY &&
  412                             inp->inp_laddr.s_addr != ip->ip_dst.s_addr)
  413                                 continue;
  414                         if (inp->inp_faddr.s_addr != INADDR_ANY &&
  415                             inp->inp_faddr.s_addr != ip->ip_src.s_addr)
  416                                 continue;
  417                         /*
  418                          * XXX: Do not check source port of incoming datagram
  419                          * unless inp_connect() has been called to bind the
  420                          * fport part of the 4-tuple; the source could be
  421                          * trying to talk to us with an ephemeral port.
  422                          */
  423                         if (inp->inp_fport != 0 &&
  424                             inp->inp_fport != uh->uh_sport)
  425                                 continue;
  426 
  427                         INP_RLOCK(inp);
  428 
  429                         /*
  430                          * Handle socket delivery policy for any-source
  431                          * and source-specific multicast. [RFC3678]
  432                          */
  433                         imo = inp->inp_moptions;
  434                         if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) &&
  435                             imo != NULL) {
  436                                 struct sockaddr_in       sin;
  437                                 struct in_msource       *ims;
  438                                 int                      blocked, mode;
  439                                 size_t                   idx;
  440 
  441                                 bzero(&sin, sizeof(struct sockaddr_in));
  442                                 sin.sin_len = sizeof(struct sockaddr_in);
  443                                 sin.sin_family = AF_INET;
  444                                 sin.sin_addr = ip->ip_dst;
  445 
  446                                 blocked = 0;
  447                                 idx = imo_match_group(imo, ifp,
  448                                     (struct sockaddr *)&sin);
  449                                 if (idx == -1) {
  450                                         /*
  451                                          * No group membership for this socket.
  452                                          * Do not bump udps_noportbcast, as
  453                                          * this will happen further down.
  454                                          */
  455                                         blocked++;
  456                                 } else {
  457                                         /*
  458                                          * Check for a multicast source filter
  459                                          * entry on this socket for this group.
  460                                          * MCAST_EXCLUDE is the default
  461                                          * behaviour.  It means default accept;
  462                                          * entries, if present, denote sources
  463                                          * to be excluded from delivery.
  464                                          */
  465                                         ims = imo_match_source(imo, idx,
  466                                             (struct sockaddr *)&udp_in);
  467                                         mode = imo->imo_mfilters[idx].imf_fmode;
  468                                         if ((ims != NULL &&
  469                                              mode == MCAST_EXCLUDE) ||
  470                                             (ims == NULL &&
  471                                              mode == MCAST_INCLUDE)) {
  472 #ifdef DIAGNOSTIC
  473                                                 if (bootverbose) {
  474                                                         printf("%s: blocked by"
  475                                                             " source filter\n",
  476                                                             __func__);
  477                                                 }
  478 #endif
  479                                                 udpstat.udps_filtermcast++;
  480                                                 blocked++;
  481                                         }
  482                                 }
  483                                 if (blocked != 0) {
  484                                         INP_RUNLOCK(inp);
  485                                         continue;
  486                                 }
  487                         }
  488                         if (last != NULL) {
  489                                 struct mbuf *n;
  490 
  491                                 n = m_copy(m, 0, M_COPYALL);
  492                                 if (n != NULL)
  493                                         udp_append(last, ip, n, iphlen +
  494                                             sizeof(struct udphdr), &udp_in);
  495                                 INP_RUNLOCK(last);
  496                         }
  497                         last = inp;
  498                         /*
  499                          * Don't look for additional matches if this one does
  500                          * not have either the SO_REUSEPORT or SO_REUSEADDR
  501                          * socket options set.  This heuristic avoids
  502                          * searching through all pcbs in the common case of a
  503                          * non-shared port.  It assumes that an application
  504                          * will never clear these options after setting them.
  505                          */
  506                         if ((last->inp_socket->so_options &
  507                             (SO_REUSEPORT|SO_REUSEADDR)) == 0)
  508                                 break;
  509                 }
  510 
  511                 if (last == NULL) {
  512                         /*
  513                          * No matching pcb found; discard datagram.  (No need
  514                          * to send an ICMP Port Unreachable for a broadcast
  515                          * or multicast datgram.)
  516                          */
  517                         udpstat.udps_noportbcast++;
  518                         goto badheadlocked;
  519                 }
  520                 udp_append(last, ip, m, iphlen + sizeof(struct udphdr),
  521                     &udp_in);
  522                 INP_RUNLOCK(last);
  523                 INP_INFO_RUNLOCK(&udbinfo);
  524                 return;
  525         }
  526 
  527         /*
  528          * Locate pcb for datagram.
  529          */
  530         inp = in_pcblookup_hash(&udbinfo, ip->ip_src, uh->uh_sport,
  531             ip->ip_dst, uh->uh_dport, 1, ifp);
  532         if (inp == NULL) {
  533                 if (udp_log_in_vain) {
  534                         char buf[4*sizeof "123"];
  535 
  536                         strcpy(buf, inet_ntoa(ip->ip_dst));
  537                         log(LOG_INFO,
  538                             "Connection attempt to UDP %s:%d from %s:%d\n",
  539                             buf, ntohs(uh->uh_dport), inet_ntoa(ip->ip_src),
  540                             ntohs(uh->uh_sport));
  541                 }
  542                 udpstat.udps_noport++;
  543                 if (m->m_flags & (M_BCAST | M_MCAST)) {
  544                         udpstat.udps_noportbcast++;
  545                         goto badheadlocked;
  546                 }
  547                 if (udp_blackhole)
  548                         goto badheadlocked;
  549                 if (badport_bandlim(BANDLIM_ICMP_UNREACH) < 0)
  550                         goto badheadlocked;
  551                 *ip = save_ip;
  552                 ip->ip_len += iphlen;
  553                 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
  554                 INP_INFO_RUNLOCK(&udbinfo);
  555                 return;
  556         }
  557 
  558         /*
  559          * Check the minimum TTL for socket.
  560          */
  561         INP_RLOCK(inp);
  562         INP_INFO_RUNLOCK(&udbinfo);
  563         if (inp->inp_ip_minttl && inp->inp_ip_minttl > ip->ip_ttl) {
  564                 INP_RUNLOCK(inp);
  565                 goto badunlocked;
  566         }
  567         udp_append(inp, ip, m, iphlen + sizeof(struct udphdr), &udp_in);
  568         INP_RUNLOCK(inp);
  569         return;
  570 
  571 badheadlocked:
  572         if (inp)
  573                 INP_RUNLOCK(inp);
  574         INP_INFO_RUNLOCK(&udbinfo);
  575 badunlocked:
  576         m_freem(m);
  577 }
  578 
  579 /*
  580  * Notify a udp user of an asynchronous error; just wake up so that they can
  581  * collect error status.
  582  */
  583 struct inpcb *
  584 udp_notify(struct inpcb *inp, int errno)
  585 {
  586 
  587         /*
  588          * While udp_ctlinput() always calls udp_notify() with a read lock
  589          * when invoking it directly, in_pcbnotifyall() currently uses write
  590          * locks due to sharing code with TCP.  For now, accept either a read
  591          * or a write lock, but a read lock is sufficient.
  592          */
  593         INP_LOCK_ASSERT(inp);
  594 
  595         inp->inp_socket->so_error = errno;
  596         sorwakeup(inp->inp_socket);
  597         sowwakeup(inp->inp_socket);
  598         return (inp);
  599 }
  600 
  601 void
  602 udp_ctlinput(int cmd, struct sockaddr *sa, void *vip)
  603 {
  604         struct ip *ip = vip;
  605         struct udphdr *uh;
  606         struct in_addr faddr;
  607         struct inpcb *inp;
  608 
  609         faddr = ((struct sockaddr_in *)sa)->sin_addr;
  610         if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY)
  611                 return;
  612 
  613         /*
  614          * Redirects don't need to be handled up here.
  615          */
  616         if (PRC_IS_REDIRECT(cmd))
  617                 return;
  618 
  619         /*
  620          * Hostdead is ugly because it goes linearly through all PCBs.
  621          *
  622          * XXX: We never get this from ICMP, otherwise it makes an excellent
  623          * DoS attack on machines with many connections.
  624          */
  625         if (cmd == PRC_HOSTDEAD)
  626                 ip = NULL;
  627         else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0)
  628                 return;
  629         if (ip != NULL) {
  630                 uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
  631                 INP_INFO_RLOCK(&udbinfo);
  632                 inp = in_pcblookup_hash(&udbinfo, faddr, uh->uh_dport,
  633                     ip->ip_src, uh->uh_sport, 0, NULL);
  634                 if (inp != NULL) {
  635                         INP_RLOCK(inp);
  636                         if (inp->inp_socket != NULL) {
  637                                 udp_notify(inp, inetctlerrmap[cmd]);
  638                         }
  639                         INP_RUNLOCK(inp);
  640                 }
  641                 INP_INFO_RUNLOCK(&udbinfo);
  642         } else
  643                 in_pcbnotifyall(&udbinfo, faddr, inetctlerrmap[cmd],
  644                     udp_notify);
  645 }
  646 
  647 static int
  648 udp_pcblist(SYSCTL_HANDLER_ARGS)
  649 {
  650         int error, i, n;
  651         struct inpcb *inp, **inp_list;
  652         inp_gen_t gencnt;
  653         struct xinpgen xig;
  654 
  655         /*
  656          * The process of preparing the PCB list is too time-consuming and
  657          * resource-intensive to repeat twice on every request.
  658          */
  659         if (req->oldptr == 0) {
  660                 n = udbinfo.ipi_count;
  661                 req->oldidx = 2 * (sizeof xig)
  662                         + (n + n/8) * sizeof(struct xinpcb);
  663                 return (0);
  664         }
  665 
  666         if (req->newptr != 0)
  667                 return (EPERM);
  668 
  669         /*
  670          * OK, now we're committed to doing something.
  671          */
  672         INP_INFO_RLOCK(&udbinfo);
  673         gencnt = udbinfo.ipi_gencnt;
  674         n = udbinfo.ipi_count;
  675         INP_INFO_RUNLOCK(&udbinfo);
  676 
  677         error = sysctl_wire_old_buffer(req, 2 * (sizeof xig)
  678                 + n * sizeof(struct xinpcb));
  679         if (error != 0)
  680                 return (error);
  681 
  682         xig.xig_len = sizeof xig;
  683         xig.xig_count = n;
  684         xig.xig_gen = gencnt;
  685         xig.xig_sogen = so_gencnt;
  686         error = SYSCTL_OUT(req, &xig, sizeof xig);
  687         if (error)
  688                 return (error);
  689 
  690         inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK);
  691         if (inp_list == 0)
  692                 return (ENOMEM);
  693 
  694         INP_INFO_RLOCK(&udbinfo);
  695         for (inp = LIST_FIRST(udbinfo.ipi_listhead), i = 0; inp && i < n;
  696              inp = LIST_NEXT(inp, inp_list)) {
  697                 INP_RLOCK(inp);
  698                 if (inp->inp_gencnt <= gencnt &&
  699                     cr_canseeinpcb(req->td->td_ucred, inp) == 0)
  700                         inp_list[i++] = inp;
  701                 INP_RUNLOCK(inp);
  702         }
  703         INP_INFO_RUNLOCK(&udbinfo);
  704         n = i;
  705 
  706         error = 0;
  707         for (i = 0; i < n; i++) {
  708                 inp = inp_list[i];
  709                 INP_RLOCK(inp);
  710                 if (inp->inp_gencnt <= gencnt) {
  711                         struct xinpcb xi;
  712                         bzero(&xi, sizeof(xi));
  713                         xi.xi_len = sizeof xi;
  714                         /* XXX should avoid extra copy */
  715                         bcopy(inp, &xi.xi_inp, sizeof *inp);
  716                         if (inp->inp_socket)
  717                                 sotoxsocket(inp->inp_socket, &xi.xi_socket);
  718                         xi.xi_inp.inp_gencnt = inp->inp_gencnt;
  719                         INP_RUNLOCK(inp);
  720                         error = SYSCTL_OUT(req, &xi, sizeof xi);
  721                 } else
  722                         INP_RUNLOCK(inp);
  723         }
  724         if (!error) {
  725                 /*
  726                  * Give the user an updated idea of our state.  If the
  727                  * generation differs from what we told her before, she knows
  728                  * that something happened while we were processing this
  729                  * request, and it might be necessary to retry.
  730                  */
  731                 INP_INFO_RLOCK(&udbinfo);
  732                 xig.xig_gen = udbinfo.ipi_gencnt;
  733                 xig.xig_sogen = so_gencnt;
  734                 xig.xig_count = udbinfo.ipi_count;
  735                 INP_INFO_RUNLOCK(&udbinfo);
  736                 error = SYSCTL_OUT(req, &xig, sizeof xig);
  737         }
  738         free(inp_list, M_TEMP);
  739         return (error);
  740 }
  741 
  742 SYSCTL_PROC(_net_inet_udp, UDPCTL_PCBLIST, pcblist, CTLFLAG_RD, 0, 0,
  743     udp_pcblist, "S,xinpcb", "List of active UDP sockets");
  744 
  745 static int
  746 udp_getcred(SYSCTL_HANDLER_ARGS)
  747 {
  748         struct xucred xuc;
  749         struct sockaddr_in addrs[2];
  750         struct inpcb *inp;
  751         int error;
  752 
  753         error = priv_check(req->td, PRIV_NETINET_GETCRED);
  754         if (error)
  755                 return (error);
  756         error = SYSCTL_IN(req, addrs, sizeof(addrs));
  757         if (error)
  758                 return (error);
  759         INP_INFO_RLOCK(&udbinfo);
  760         inp = in_pcblookup_hash(&udbinfo, addrs[1].sin_addr, addrs[1].sin_port,
  761                                 addrs[0].sin_addr, addrs[0].sin_port, 1, NULL);
  762         if (inp != NULL) {
  763                 INP_RLOCK(inp);
  764                 INP_INFO_RUNLOCK(&udbinfo);
  765                 if (inp->inp_socket == NULL)
  766                         error = ENOENT;
  767                 if (error == 0)
  768                         error = cr_canseeinpcb(req->td->td_ucred, inp);
  769                 if (error == 0)
  770                         cru2x(inp->inp_cred, &xuc);
  771                 INP_RUNLOCK(inp);
  772         } else {
  773                 INP_INFO_RUNLOCK(&udbinfo);
  774                 error = ENOENT;
  775         }
  776         if (error == 0)
  777                 error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
  778         return (error);
  779 }
  780 
  781 SYSCTL_PROC(_net_inet_udp, OID_AUTO, getcred,
  782     CTLTYPE_OPAQUE|CTLFLAG_RW|CTLFLAG_PRISON, 0, 0,
  783     udp_getcred, "S,xucred", "Get the xucred of a UDP connection");
  784 
  785 static int
  786 udp_output(struct inpcb *inp, struct mbuf *m, struct sockaddr *addr,
  787     struct mbuf *control, struct thread *td)
  788 {
  789         struct udpiphdr *ui;
  790         int len = m->m_pkthdr.len;
  791         struct in_addr faddr, laddr;
  792         struct cmsghdr *cm;
  793         struct sockaddr_in *sin, src;
  794         int error = 0;
  795         int ipflags;
  796         u_short fport, lport;
  797         int unlock_udbinfo;
  798 
  799         /*
  800          * udp_output() may need to temporarily bind or connect the current
  801          * inpcb.  As such, we don't know up front whether we will need the
  802          * pcbinfo lock or not.  Do any work to decide what is needed up
  803          * front before acquiring any locks.
  804          */
  805         if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) {
  806                 if (control)
  807                         m_freem(control);
  808                 m_freem(m);
  809                 return (EMSGSIZE);
  810         }
  811 
  812         src.sin_family = 0;
  813         if (control != NULL) {
  814                 /*
  815                  * XXX: Currently, we assume all the optional information is
  816                  * stored in a single mbuf.
  817                  */
  818                 if (control->m_next) {
  819                         m_freem(control);
  820                         m_freem(m);
  821                         return (EINVAL);
  822                 }
  823                 for (; control->m_len > 0;
  824                     control->m_data += CMSG_ALIGN(cm->cmsg_len),
  825                     control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
  826                         cm = mtod(control, struct cmsghdr *);
  827                         if (control->m_len < sizeof(*cm) || cm->cmsg_len == 0
  828                             || cm->cmsg_len > control->m_len) {
  829                                 error = EINVAL;
  830                                 break;
  831                         }
  832                         if (cm->cmsg_level != IPPROTO_IP)
  833                                 continue;
  834 
  835                         switch (cm->cmsg_type) {
  836                         case IP_SENDSRCADDR:
  837                                 if (cm->cmsg_len !=
  838                                     CMSG_LEN(sizeof(struct in_addr))) {
  839                                         error = EINVAL;
  840                                         break;
  841                                 }
  842                                 bzero(&src, sizeof(src));
  843                                 src.sin_family = AF_INET;
  844                                 src.sin_len = sizeof(src);
  845                                 src.sin_port = inp->inp_lport;
  846                                 src.sin_addr =
  847                                     *(struct in_addr *)CMSG_DATA(cm);
  848                                 break;
  849 
  850                         default:
  851                                 error = ENOPROTOOPT;
  852                                 break;
  853                         }
  854                         if (error)
  855                                 break;
  856                 }
  857                 m_freem(control);
  858         }
  859         if (error) {
  860                 m_freem(m);
  861                 return (error);
  862         }
  863 
  864         /*
  865          * Depending on whether or not the application has bound or connected
  866          * the socket, we may have to do varying levels of work.  The optimal
  867          * case is for a connected UDP socket, as a global lock isn't
  868          * required at all.
  869          *
  870          * In order to decide which we need, we require stability of the
  871          * inpcb binding, which we ensure by acquiring a read lock on the
  872          * inpcb.  This doesn't strictly follow the lock order, so we play
  873          * the trylock and retry game; note that we may end up with more
  874          * conservative locks than required the second time around, so later
  875          * assertions have to accept that.  Further analysis of the number of
  876          * misses under contention is required.
  877          */
  878         sin = (struct sockaddr_in *)addr;
  879         INP_RLOCK(inp);
  880         if (sin != NULL &&
  881             (inp->inp_laddr.s_addr == INADDR_ANY && inp->inp_lport == 0)) {
  882                 INP_RUNLOCK(inp);
  883                 INP_INFO_WLOCK(&udbinfo);
  884                 INP_WLOCK(inp);
  885                 unlock_udbinfo = 2;
  886         } else if ((sin != NULL && (
  887             (sin->sin_addr.s_addr == INADDR_ANY) ||
  888             (sin->sin_addr.s_addr == INADDR_BROADCAST) ||
  889             (inp->inp_laddr.s_addr == INADDR_ANY) ||
  890             (inp->inp_lport == 0))) ||
  891             (src.sin_family == AF_INET)) {
  892                 if (!INP_INFO_TRY_RLOCK(&udbinfo)) {
  893                         INP_RUNLOCK(inp);
  894                         INP_INFO_RLOCK(&udbinfo);
  895                         INP_RLOCK(inp);
  896                 }
  897                 unlock_udbinfo = 1;
  898         } else
  899                 unlock_udbinfo = 0;
  900 
  901         /*
  902          * If the IP_SENDSRCADDR control message was specified, override the
  903          * source address for this datagram.  Its use is invalidated if the
  904          * address thus specified is incomplete or clobbers other inpcbs.
  905          */
  906         laddr = inp->inp_laddr;
  907         lport = inp->inp_lport;
  908         if (src.sin_family == AF_INET) {
  909                 INP_INFO_LOCK_ASSERT(&udbinfo);
  910                 if ((lport == 0) ||
  911                     (laddr.s_addr == INADDR_ANY &&
  912                      src.sin_addr.s_addr == INADDR_ANY)) {
  913                         error = EINVAL;
  914                         goto release;
  915                 }
  916                 error = in_pcbbind_setup(inp, (struct sockaddr *)&src,
  917                     &laddr.s_addr, &lport, td->td_ucred);
  918                 if (error)
  919                         goto release;
  920         }
  921 
  922         /*
  923          * If a UDP socket has been connected, then a local address/port will
  924          * have been selected and bound.
  925          *
  926          * If a UDP socket has not been connected to, then an explicit
  927          * destination address must be used, in which case a local
  928          * address/port may not have been selected and bound.
  929          */
  930         if (sin != NULL) {
  931                 INP_LOCK_ASSERT(inp);
  932                 if (inp->inp_faddr.s_addr != INADDR_ANY) {
  933                         error = EISCONN;
  934                         goto release;
  935                 }
  936 
  937                 /*
  938                  * Jail may rewrite the destination address, so let it do
  939                  * that before we use it.
  940                  */
  941                 if (jailed(td->td_ucred))
  942                         prison_remote_ip(td->td_ucred, 0,
  943                             &sin->sin_addr.s_addr);
  944 
  945                 /*
  946                  * If a local address or port hasn't yet been selected, or if
  947                  * the destination address needs to be rewritten due to using
  948                  * a special INADDR_ constant, invoke in_pcbconnect_setup()
  949                  * to do the heavy lifting.  Once a port is selected, we
  950                  * commit the binding back to the socket; we also commit the
  951                  * binding of the address if in jail.
  952                  *
  953                  * If we already have a valid binding and we're not
  954                  * requesting a destination address rewrite, use a fast path.
  955                  */
  956                 if (inp->inp_laddr.s_addr == INADDR_ANY ||
  957                     inp->inp_lport == 0 ||
  958                     sin->sin_addr.s_addr == INADDR_ANY ||
  959                     sin->sin_addr.s_addr == INADDR_BROADCAST) {
  960                         INP_INFO_LOCK_ASSERT(&udbinfo);
  961                         error = in_pcbconnect_setup(inp, addr, &laddr.s_addr,
  962                             &lport, &faddr.s_addr, &fport, NULL,
  963                             td->td_ucred);
  964                         if (error)
  965                                 goto release;
  966 
  967                         /*
  968                          * XXXRW: Why not commit the port if the address is
  969                          * !INADDR_ANY?
  970                          */
  971                         /* Commit the local port if newly assigned. */
  972                         if (inp->inp_laddr.s_addr == INADDR_ANY &&
  973                             inp->inp_lport == 0) {
  974                                 INP_INFO_WLOCK_ASSERT(&udbinfo);
  975                                 INP_WLOCK_ASSERT(inp);
  976                                 /*
  977                                  * Remember addr if jailed, to prevent
  978                                  * rebinding.
  979                                  */
  980                                 if (jailed(td->td_ucred))
  981                                         inp->inp_laddr = laddr;
  982                                 inp->inp_lport = lport;
  983                                 if (in_pcbinshash(inp) != 0) {
  984                                         inp->inp_lport = 0;
  985                                         error = EAGAIN;
  986                                         goto release;
  987                                 }
  988                                 inp->inp_flags |= INP_ANONPORT;
  989                         }
  990                 } else {
  991                         faddr = sin->sin_addr;
  992                         fport = sin->sin_port;
  993                 }
  994         } else {
  995                 INP_LOCK_ASSERT(inp);
  996                 faddr = inp->inp_faddr;
  997                 fport = inp->inp_fport;
  998                 if (faddr.s_addr == INADDR_ANY) {
  999                         error = ENOTCONN;
 1000                         goto release;
 1001                 }
 1002         }
 1003 
 1004         /*
 1005          * Calculate data length and get a mbuf for UDP, IP, and possible
 1006          * link-layer headers.  Immediate slide the data pointer back forward
 1007          * since we won't use that space at this layer.
 1008          */
 1009         M_PREPEND(m, sizeof(struct udpiphdr) + max_linkhdr, M_DONTWAIT);
 1010         if (m == NULL) {
 1011                 error = ENOBUFS;
 1012                 goto release;
 1013         }
 1014         m->m_data += max_linkhdr;
 1015         m->m_len -= max_linkhdr;
 1016         m->m_pkthdr.len -= max_linkhdr;
 1017 
 1018         /*
 1019          * Fill in mbuf with extended UDP header and addresses and length put
 1020          * into network format.
 1021          */
 1022         ui = mtod(m, struct udpiphdr *);
 1023         bzero(ui->ui_x1, sizeof(ui->ui_x1));    /* XXX still needed? */
 1024         ui->ui_pr = IPPROTO_UDP;
 1025         ui->ui_src = laddr;
 1026         ui->ui_dst = faddr;
 1027         ui->ui_sport = lport;
 1028         ui->ui_dport = fport;
 1029         ui->ui_ulen = htons((u_short)len + sizeof(struct udphdr));
 1030 
 1031         /*
 1032          * Set the Don't Fragment bit in the IP header.
 1033          */
 1034         if (inp->inp_flags & INP_DONTFRAG) {
 1035                 struct ip *ip;
 1036 
 1037                 ip = (struct ip *)&ui->ui_i;
 1038                 ip->ip_off |= IP_DF;
 1039         }
 1040 
 1041         ipflags = 0;
 1042         if (inp->inp_socket->so_options & SO_DONTROUTE)
 1043                 ipflags |= IP_ROUTETOIF;
 1044         if (inp->inp_socket->so_options & SO_BROADCAST)
 1045                 ipflags |= IP_ALLOWBROADCAST;
 1046         if (inp->inp_flags & INP_ONESBCAST)
 1047                 ipflags |= IP_SENDONES;
 1048 
 1049 #ifdef MAC
 1050         mac_create_mbuf_from_inpcb(inp, m);
 1051 #endif
 1052 
 1053         /*
 1054          * Set up checksum and output datagram.
 1055          */
 1056         if (udp_cksum) {
 1057                 if (inp->inp_flags & INP_ONESBCAST)
 1058                         faddr.s_addr = INADDR_BROADCAST;
 1059                 ui->ui_sum = in_pseudo(ui->ui_src.s_addr, faddr.s_addr,
 1060                     htons((u_short)len + sizeof(struct udphdr) + IPPROTO_UDP));
 1061                 m->m_pkthdr.csum_flags = CSUM_UDP;
 1062                 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
 1063         } else
 1064                 ui->ui_sum = 0;
 1065         ((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len;
 1066         ((struct ip *)ui)->ip_ttl = inp->inp_ip_ttl;    /* XXX */
 1067         ((struct ip *)ui)->ip_tos = inp->inp_ip_tos;    /* XXX */
 1068         udpstat.udps_opackets++;
 1069 
 1070         if (unlock_udbinfo == 2)
 1071                 INP_INFO_WUNLOCK(&udbinfo);
 1072         else if (unlock_udbinfo == 1)
 1073                 INP_INFO_RUNLOCK(&udbinfo);
 1074         error = ip_output(m, inp->inp_options, NULL, ipflags,
 1075             inp->inp_moptions, inp);
 1076         if (unlock_udbinfo == 2)
 1077                 INP_WUNLOCK(inp);
 1078         else
 1079                 INP_RUNLOCK(inp);
 1080         return (error);
 1081 
 1082 release:
 1083         if (unlock_udbinfo == 2) {
 1084                 INP_WUNLOCK(inp);
 1085                 INP_INFO_WUNLOCK(&udbinfo);
 1086         } else if (unlock_udbinfo == 1) {
 1087                 INP_RUNLOCK(inp);
 1088                 INP_INFO_RUNLOCK(&udbinfo);
 1089         } else
 1090                 INP_RUNLOCK(inp);
 1091         m_freem(m);
 1092         return (error);
 1093 }
 1094 
 1095 static void
 1096 udp_abort(struct socket *so)
 1097 {
 1098         struct inpcb *inp;
 1099 
 1100         inp = sotoinpcb(so);
 1101         KASSERT(inp != NULL, ("udp_abort: inp == NULL"));
 1102         INP_INFO_WLOCK(&udbinfo);
 1103         INP_WLOCK(inp);
 1104         if (inp->inp_faddr.s_addr != INADDR_ANY) {
 1105                 in_pcbdisconnect(inp);
 1106                 inp->inp_laddr.s_addr = INADDR_ANY;
 1107                 soisdisconnected(so);
 1108         }
 1109         INP_WUNLOCK(inp);
 1110         INP_INFO_WUNLOCK(&udbinfo);
 1111 }
 1112 
 1113 static int
 1114 udp_attach(struct socket *so, int proto, struct thread *td)
 1115 {
 1116         struct inpcb *inp;
 1117         int error;
 1118 
 1119         inp = sotoinpcb(so);
 1120         KASSERT(inp == NULL, ("udp_attach: inp != NULL"));
 1121         error = soreserve(so, udp_sendspace, udp_recvspace);
 1122         if (error)
 1123                 return (error);
 1124         INP_INFO_WLOCK(&udbinfo);
 1125         error = in_pcballoc(so, &udbinfo);
 1126         if (error) {
 1127                 INP_INFO_WUNLOCK(&udbinfo);
 1128                 return (error);
 1129         }
 1130 
 1131         inp = (struct inpcb *)so->so_pcb;
 1132         INP_INFO_WUNLOCK(&udbinfo);
 1133         inp->inp_vflag |= INP_IPV4;
 1134         inp->inp_ip_ttl = ip_defttl;
 1135         INP_WUNLOCK(inp);
 1136         return (0);
 1137 }
 1138 
 1139 static int
 1140 udp_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
 1141 {
 1142         struct inpcb *inp;
 1143         int error;
 1144 
 1145         inp = sotoinpcb(so);
 1146         KASSERT(inp != NULL, ("udp_bind: inp == NULL"));
 1147         INP_INFO_WLOCK(&udbinfo);
 1148         INP_WLOCK(inp);
 1149         error = in_pcbbind(inp, nam, td->td_ucred);
 1150         INP_WUNLOCK(inp);
 1151         INP_INFO_WUNLOCK(&udbinfo);
 1152         return (error);
 1153 }
 1154 
 1155 static void
 1156 udp_close(struct socket *so)
 1157 {
 1158         struct inpcb *inp;
 1159 
 1160         inp = sotoinpcb(so);
 1161         KASSERT(inp != NULL, ("udp_close: inp == NULL"));
 1162         INP_INFO_WLOCK(&udbinfo);
 1163         INP_WLOCK(inp);
 1164         if (inp->inp_faddr.s_addr != INADDR_ANY) {
 1165                 in_pcbdisconnect(inp);
 1166                 inp->inp_laddr.s_addr = INADDR_ANY;
 1167                 soisdisconnected(so);
 1168         }
 1169         INP_WUNLOCK(inp);
 1170         INP_INFO_WUNLOCK(&udbinfo);
 1171 }
 1172 
 1173 static int
 1174 udp_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
 1175 {
 1176         struct inpcb *inp;
 1177         int error;
 1178         struct sockaddr_in *sin;
 1179 
 1180         inp = sotoinpcb(so);
 1181         KASSERT(inp != NULL, ("udp_connect: inp == NULL"));
 1182         INP_INFO_WLOCK(&udbinfo);
 1183         INP_WLOCK(inp);
 1184         if (inp->inp_faddr.s_addr != INADDR_ANY) {
 1185                 INP_WUNLOCK(inp);
 1186                 INP_INFO_WUNLOCK(&udbinfo);
 1187                 return (EISCONN);
 1188         }
 1189         sin = (struct sockaddr_in *)nam;
 1190         if (jailed(td->td_ucred))
 1191                 prison_remote_ip(td->td_ucred, 0, &sin->sin_addr.s_addr);
 1192         error = in_pcbconnect(inp, nam, td->td_ucred);
 1193         if (error == 0)
 1194                 soisconnected(so);
 1195         INP_WUNLOCK(inp);
 1196         INP_INFO_WUNLOCK(&udbinfo);
 1197         return (error);
 1198 }
 1199 
 1200 static void
 1201 udp_detach(struct socket *so)
 1202 {
 1203         struct inpcb *inp;
 1204 
 1205         inp = sotoinpcb(so);
 1206         KASSERT(inp != NULL, ("udp_detach: inp == NULL"));
 1207         KASSERT(inp->inp_faddr.s_addr == INADDR_ANY,
 1208             ("udp_detach: not disconnected"));
 1209         INP_INFO_WLOCK(&udbinfo);
 1210         INP_WLOCK(inp);
 1211         in_pcbdetach(inp);
 1212         in_pcbfree(inp);
 1213         INP_INFO_WUNLOCK(&udbinfo);
 1214 }
 1215 
 1216 static int
 1217 udp_disconnect(struct socket *so)
 1218 {
 1219         struct inpcb *inp;
 1220 
 1221         inp = sotoinpcb(so);
 1222         KASSERT(inp != NULL, ("udp_disconnect: inp == NULL"));
 1223         INP_INFO_WLOCK(&udbinfo);
 1224         INP_WLOCK(inp);
 1225         if (inp->inp_faddr.s_addr == INADDR_ANY) {
 1226                 INP_WUNLOCK(inp);
 1227                 INP_INFO_WUNLOCK(&udbinfo);
 1228                 return (ENOTCONN);
 1229         }
 1230 
 1231         in_pcbdisconnect(inp);
 1232         inp->inp_laddr.s_addr = INADDR_ANY;
 1233         SOCK_LOCK(so);
 1234         so->so_state &= ~SS_ISCONNECTED;                /* XXX */
 1235         SOCK_UNLOCK(so);
 1236         INP_WUNLOCK(inp);
 1237         INP_INFO_WUNLOCK(&udbinfo);
 1238         return (0);
 1239 }
 1240 
 1241 static int
 1242 udp_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
 1243     struct mbuf *control, struct thread *td)
 1244 {
 1245         struct inpcb *inp;
 1246 
 1247         inp = sotoinpcb(so);
 1248         KASSERT(inp != NULL, ("udp_send: inp == NULL"));
 1249         return (udp_output(inp, m, addr, control, td));
 1250 }
 1251 
 1252 int
 1253 udp_shutdown(struct socket *so)
 1254 {
 1255         struct inpcb *inp;
 1256 
 1257         inp = sotoinpcb(so);
 1258         KASSERT(inp != NULL, ("udp_shutdown: inp == NULL"));
 1259         INP_WLOCK(inp);
 1260         socantsendmore(so);
 1261         INP_WUNLOCK(inp);
 1262         return (0);
 1263 }
 1264 
 1265 struct pr_usrreqs udp_usrreqs = {
 1266         .pru_abort =            udp_abort,
 1267         .pru_attach =           udp_attach,
 1268         .pru_bind =             udp_bind,
 1269         .pru_connect =          udp_connect,
 1270         .pru_control =          in_control,
 1271         .pru_detach =           udp_detach,
 1272         .pru_disconnect =       udp_disconnect,
 1273         .pru_peeraddr =         in_getpeeraddr,
 1274         .pru_send =             udp_send,
 1275         .pru_sosend =           sosend_dgram,
 1276         .pru_shutdown =         udp_shutdown,
 1277         .pru_sockaddr =         in_getsockaddr,
 1278         .pru_sosetlabel =       in_pcbsosetlabel,
 1279         .pru_close =            udp_close,
 1280 };

Cache object: e0fde8365644423f26b57de542447019


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.