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

Cache object: a20b445ad9b054ffda7592a516f340f1


[ 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.