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.  All rights reserved.
    4  *
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted provided that the following conditions
    7  * are met:
    8  * 1. Redistributions of source code must retain the above copyright
    9  *    notice, this list of conditions and the following disclaimer.
   10  * 2. Redistributions in binary form must reproduce the above copyright
   11  *    notice, this list of conditions and the following disclaimer in the
   12  *    documentation and/or other materials provided with the distribution.
   13  * 3. All advertising materials mentioning features or use of this software
   14  *    must display the following acknowledgement:
   15  *      This product includes software developed by the University of
   16  *      California, Berkeley and its contributors.
   17  * 4. Neither the name of the University nor the names of its contributors
   18  *    may be used to endorse or promote products derived from this software
   19  *    without specific prior written permission.
   20  *
   21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
   22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   31  * SUCH DAMAGE.
   32  *
   33  *      @(#)udp_usrreq.c        8.6 (Berkeley) 5/23/95
   34  * $FreeBSD: releng/5.2/sys/netinet/udp_usrreq.c 122991 2003-11-26 01:40:44Z sam $
   35  */
   36 
   37 #include "opt_ipsec.h"
   38 #include "opt_inet6.h"
   39 #include "opt_mac.h"
   40 
   41 #include <sys/param.h>
   42 #include <sys/systm.h>
   43 #include <sys/domain.h>
   44 #include <sys/jail.h>
   45 #include <sys/kernel.h>
   46 #include <sys/lock.h>
   47 #include <sys/mac.h>
   48 #include <sys/malloc.h>
   49 #include <sys/mbuf.h>
   50 #include <sys/proc.h>
   51 #include <sys/protosw.h>
   52 #include <sys/signalvar.h>
   53 #include <sys/socket.h>
   54 #include <sys/socketvar.h>
   55 #include <sys/sx.h>
   56 #include <sys/sysctl.h>
   57 #include <sys/syslog.h>
   58 
   59 #include <vm/uma.h>
   60 
   61 #include <net/if.h>
   62 #include <net/route.h>
   63 
   64 #include <netinet/in.h>
   65 #include <netinet/in_systm.h>
   66 #include <netinet/in_pcb.h>
   67 #include <netinet/in_var.h>
   68 #include <netinet/ip.h>
   69 #ifdef INET6
   70 #include <netinet/ip6.h>
   71 #endif
   72 #include <netinet/ip_icmp.h>
   73 #include <netinet/icmp_var.h>
   74 #include <netinet/ip_var.h>
   75 #ifdef INET6
   76 #include <netinet6/ip6_var.h>
   77 #endif
   78 #include <netinet/udp.h>
   79 #include <netinet/udp_var.h>
   80 
   81 #ifdef FAST_IPSEC
   82 #include <netipsec/ipsec.h>
   83 #endif /*FAST_IPSEC*/
   84 
   85 #ifdef IPSEC
   86 #include <netinet6/ipsec.h>
   87 #endif /*IPSEC*/
   88 
   89 #include <machine/in_cksum.h>
   90 
   91 /*
   92  * UDP protocol implementation.
   93  * Per RFC 768, August, 1980.
   94  */
   95 #ifndef COMPAT_42
   96 static int      udpcksum = 1;
   97 #else
   98 static int      udpcksum = 0;           /* XXX */
   99 #endif
  100 SYSCTL_INT(_net_inet_udp, UDPCTL_CHECKSUM, checksum, CTLFLAG_RW,
  101                 &udpcksum, 0, "");
  102 
  103 int     log_in_vain = 0;
  104 SYSCTL_INT(_net_inet_udp, OID_AUTO, log_in_vain, CTLFLAG_RW, 
  105     &log_in_vain, 0, "Log all incoming UDP packets");
  106 
  107 static int      blackhole = 0;
  108 SYSCTL_INT(_net_inet_udp, OID_AUTO, blackhole, CTLFLAG_RW,
  109         &blackhole, 0, "Do not send port unreachables for refused connects");
  110 
  111 static int      strict_mcast_mship = 0;
  112 SYSCTL_INT(_net_inet_udp, OID_AUTO, strict_mcast_mship, CTLFLAG_RW,
  113         &strict_mcast_mship, 0, "Only send multicast to member sockets");
  114 
  115 struct  inpcbhead udb;          /* from udp_var.h */
  116 #define udb6    udb  /* for KAME src sync over BSD*'s */
  117 struct  inpcbinfo udbinfo;
  118 
  119 #ifndef UDBHASHSIZE
  120 #define UDBHASHSIZE 16
  121 #endif
  122 
  123 struct  udpstat udpstat;        /* from udp_var.h */
  124 SYSCTL_STRUCT(_net_inet_udp, UDPCTL_STATS, stats, CTLFLAG_RW,
  125     &udpstat, udpstat, "UDP statistics (struct udpstat, netinet/udp_var.h)");
  126 
  127 static struct   sockaddr_in udp_in = { sizeof(udp_in), AF_INET };
  128 #ifdef INET6
  129 struct udp_in6 {
  130         struct sockaddr_in6     uin6_sin;
  131         u_char                  uin6_init_done : 1;
  132 } udp_in6 = {
  133         { sizeof(udp_in6.uin6_sin), AF_INET6 },
  134         0
  135 };
  136 struct udp_ip6 {
  137         struct ip6_hdr          uip6_ip6;
  138         u_char                  uip6_init_done : 1;
  139 } udp_ip6;
  140 #endif /* INET6 */
  141 
  142 static void udp_append(struct inpcb *last, struct ip *ip, struct mbuf *n,
  143                 int off);
  144 #ifdef INET6
  145 static void ip_2_ip6_hdr(struct ip6_hdr *ip6, struct ip *ip);
  146 #endif
  147 
  148 static int udp_detach(struct socket *so);
  149 static  int udp_output(struct inpcb *, struct mbuf *, struct sockaddr *,
  150                 struct mbuf *, struct thread *);
  151 
  152 void
  153 udp_init()
  154 {
  155         INP_INFO_LOCK_INIT(&udbinfo, "udp");
  156         LIST_INIT(&udb);
  157         udbinfo.listhead = &udb;
  158         udbinfo.hashbase = hashinit(UDBHASHSIZE, M_PCB, &udbinfo.hashmask);
  159         udbinfo.porthashbase = hashinit(UDBHASHSIZE, M_PCB,
  160                                         &udbinfo.porthashmask);
  161         udbinfo.ipi_zone = uma_zcreate("udpcb", sizeof(struct inpcb), NULL,
  162             NULL, NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
  163         uma_zone_set_max(udbinfo.ipi_zone, maxsockets);
  164 }
  165 
  166 void
  167 udp_input(m, off)
  168         register struct mbuf *m;
  169         int off;
  170 {
  171         int iphlen = off;
  172         register struct ip *ip;
  173         register struct udphdr *uh;
  174         register struct inpcb *inp;
  175         struct mbuf *opts = 0;
  176         int len;
  177         struct ip save_ip;
  178 
  179         udpstat.udps_ipackets++;
  180 
  181         /*
  182          * Strip IP options, if any; should skip this,
  183          * make available to user, and use on returned packets,
  184          * but we don't yet have a way to check the checksum
  185          * with options still present.
  186          */
  187         if (iphlen > sizeof (struct ip)) {
  188                 ip_stripoptions(m, (struct mbuf *)0);
  189                 iphlen = sizeof(struct ip);
  190         }
  191 
  192         /*
  193          * Get IP and UDP header together in first mbuf.
  194          */
  195         ip = mtod(m, struct ip *);
  196         if (m->m_len < iphlen + sizeof(struct udphdr)) {
  197                 if ((m = m_pullup(m, iphlen + sizeof(struct udphdr))) == 0) {
  198                         udpstat.udps_hdrops++;
  199                         return;
  200                 }
  201                 ip = mtod(m, struct ip *);
  202         }
  203         uh = (struct udphdr *)((caddr_t)ip + iphlen);
  204 
  205         /* destination port of 0 is illegal, based on RFC768. */
  206         if (uh->uh_dport == 0)
  207                 goto badunlocked;
  208 
  209         /*
  210          * Construct sockaddr format source address.
  211          * Stuff source address and datagram in user buffer.
  212          */
  213         udp_in.sin_port = uh->uh_sport;
  214         udp_in.sin_addr = ip->ip_src;
  215 #ifdef INET6
  216         udp_in6.uin6_init_done = udp_ip6.uip6_init_done = 0;
  217 #endif
  218 
  219         /*
  220          * Make mbuf data length reflect UDP length.
  221          * If not enough data to reflect UDP length, drop.
  222          */
  223         len = ntohs((u_short)uh->uh_ulen);
  224         if (ip->ip_len != len) {
  225                 if (len > ip->ip_len || len < sizeof(struct udphdr)) {
  226                         udpstat.udps_badlen++;
  227                         goto badunlocked;
  228                 }
  229                 m_adj(m, len - ip->ip_len);
  230                 /* ip->ip_len = len; */
  231         }
  232         /*
  233          * Save a copy of the IP header in case we want restore it
  234          * for sending an ICMP error message in response.
  235          */
  236         if (!blackhole)
  237                 save_ip = *ip;
  238 
  239         /*
  240          * Checksum extended UDP header and data.
  241          */
  242         if (uh->uh_sum) {
  243                 if (m->m_pkthdr.csum_flags & CSUM_DATA_VALID) {
  244                         if (m->m_pkthdr.csum_flags & CSUM_PSEUDO_HDR)
  245                                 uh->uh_sum = m->m_pkthdr.csum_data;
  246                         else
  247                                 uh->uh_sum = in_pseudo(ip->ip_src.s_addr,
  248                                     ip->ip_dst.s_addr, htonl((u_short)len +
  249                                     m->m_pkthdr.csum_data + IPPROTO_UDP));
  250                         uh->uh_sum ^= 0xffff;
  251                 } else {
  252                         char b[9];
  253                         bcopy(((struct ipovly *)ip)->ih_x1, b, 9);
  254                         bzero(((struct ipovly *)ip)->ih_x1, 9);
  255                         ((struct ipovly *)ip)->ih_len = uh->uh_ulen;
  256                         uh->uh_sum = in_cksum(m, len + sizeof (struct ip));
  257                         bcopy(b, ((struct ipovly *)ip)->ih_x1, 9);
  258                 }
  259                 if (uh->uh_sum) {
  260                         udpstat.udps_badsum++;
  261                         m_freem(m);
  262                         return;
  263                 }
  264         } else
  265                 udpstat.udps_nosum++;
  266 
  267         INP_INFO_RLOCK(&udbinfo);
  268 
  269         if (IN_MULTICAST(ntohl(ip->ip_dst.s_addr)) ||
  270             in_broadcast(ip->ip_dst, m->m_pkthdr.rcvif)) {
  271                 struct inpcb *last;
  272                 /*
  273                  * Deliver a multicast or broadcast datagram to *all* sockets
  274                  * for which the local and remote addresses and ports match
  275                  * those of the incoming datagram.  This allows more than
  276                  * one process to receive multi/broadcasts on the same port.
  277                  * (This really ought to be done for unicast datagrams as
  278                  * well, but that would cause problems with existing
  279                  * applications that open both address-specific sockets and
  280                  * a wildcard socket listening to the same port -- they would
  281                  * end up receiving duplicates of every unicast datagram.
  282                  * Those applications open the multiple sockets to overcome an
  283                  * inadequacy of the UDP socket interface, but for backwards
  284                  * compatibility we avoid the problem here rather than
  285                  * fixing the interface.  Maybe 4.5BSD will remedy this?)
  286                  */
  287 
  288                 /*
  289                  * Locate pcb(s) for datagram.
  290                  * (Algorithm copied from raw_intr().)
  291                  */
  292                 last = NULL;
  293                 LIST_FOREACH(inp, &udb, inp_list) {
  294                         INP_LOCK(inp);
  295                         if (inp->inp_lport != uh->uh_dport) {
  296                 docontinue:
  297                                 INP_UNLOCK(inp);
  298                                 continue;
  299                         }
  300 #ifdef INET6
  301                         if ((inp->inp_vflag & INP_IPV4) == 0)
  302                                 goto docontinue;
  303 #endif
  304                         if (inp->inp_laddr.s_addr != INADDR_ANY) {
  305                                 if (inp->inp_laddr.s_addr != ip->ip_dst.s_addr)
  306                                         goto docontinue;
  307                         }
  308                         if (inp->inp_faddr.s_addr != INADDR_ANY) {
  309                                 if (inp->inp_faddr.s_addr !=
  310                                     ip->ip_src.s_addr ||
  311                                     inp->inp_fport != uh->uh_sport)
  312                                         goto docontinue;
  313                         }
  314 
  315                         /*
  316                          * Check multicast packets to make sure they are only
  317                          * sent to sockets with multicast memberships for the
  318                          * packet's destination address and arrival interface
  319                          */
  320 #define MSHIP(_inp, n) ((_inp)->inp_moptions->imo_membership[(n)])
  321 #define NMSHIPS(_inp) ((_inp)->inp_moptions->imo_num_memberships)
  322                         if (strict_mcast_mship && inp->inp_moptions != NULL) {
  323                                 int mship, foundmship = 0;
  324 
  325                                 for (mship = 0; mship < NMSHIPS(inp); mship++) {
  326                                         if (MSHIP(inp, mship)->inm_addr.s_addr
  327                                             == ip->ip_dst.s_addr &&
  328                                             MSHIP(inp, mship)->inm_ifp
  329                                             == m->m_pkthdr.rcvif) {
  330                                                 foundmship = 1;
  331                                                 break;
  332                                         }
  333                                 }
  334                                 if (foundmship == 0)
  335                                         goto docontinue;
  336                         }
  337 #undef NMSHIPS
  338 #undef MSHIP
  339                         if (last != NULL) {
  340                                 struct mbuf *n;
  341 
  342                                 n = m_copy(m, 0, M_COPYALL);
  343                                 if (n != NULL)
  344                                         udp_append(last, ip, n,
  345                                                    iphlen +
  346                                                    sizeof(struct udphdr));
  347                                 INP_UNLOCK(last);
  348                         }
  349                         last = inp;
  350                         /*
  351                          * Don't look for additional matches if this one does
  352                          * not have either the SO_REUSEPORT or SO_REUSEADDR
  353                          * socket options set.  This heuristic avoids searching
  354                          * through all pcbs in the common case of a non-shared
  355                          * port.  It * assumes that an application will never
  356                          * clear these options after setting them.
  357                          */
  358                         if ((last->inp_socket->so_options&(SO_REUSEPORT|SO_REUSEADDR)) == 0)
  359                                 break;
  360                 }
  361 
  362                 if (last == NULL) {
  363                         /*
  364                          * No matching pcb found; discard datagram.
  365                          * (No need to send an ICMP Port Unreachable
  366                          * for a broadcast or multicast datgram.)
  367                          */
  368                         udpstat.udps_noportbcast++;
  369                         goto badheadlocked;
  370                 }
  371                 INP_INFO_RUNLOCK(&udbinfo);
  372                 udp_append(last, ip, m, iphlen + sizeof(struct udphdr));
  373                 INP_UNLOCK(last);
  374                 return;
  375         }
  376         /*
  377          * Locate pcb for datagram.
  378          */
  379         inp = in_pcblookup_hash(&udbinfo, ip->ip_src, uh->uh_sport,
  380             ip->ip_dst, uh->uh_dport, 1, m->m_pkthdr.rcvif);
  381         if (inp == NULL) {
  382                 if (log_in_vain) {
  383                         char buf[4*sizeof "123"];
  384 
  385                         strcpy(buf, inet_ntoa(ip->ip_dst));
  386                         log(LOG_INFO,
  387                             "Connection attempt to UDP %s:%d from %s:%d\n",
  388                             buf, ntohs(uh->uh_dport), inet_ntoa(ip->ip_src),
  389                             ntohs(uh->uh_sport));
  390                 }
  391                 udpstat.udps_noport++;
  392                 if (m->m_flags & (M_BCAST | M_MCAST)) {
  393                         udpstat.udps_noportbcast++;
  394                         goto badheadlocked;
  395                 }
  396                 if (blackhole)
  397                         goto badheadlocked;
  398                 if (badport_bandlim(BANDLIM_ICMP_UNREACH) < 0)
  399                         goto badheadlocked;
  400                 *ip = save_ip;
  401                 ip->ip_len += iphlen;
  402                 icmp_error(m, ICMP_UNREACH, ICMP_UNREACH_PORT, 0, 0);
  403                 INP_INFO_RUNLOCK(&udbinfo);
  404                 return;
  405         }
  406         INP_LOCK(inp);
  407         INP_INFO_RUNLOCK(&udbinfo);
  408         udp_append(inp, ip, m, iphlen + sizeof(struct udphdr));
  409         INP_UNLOCK(inp);
  410         return;
  411 
  412 badheadlocked:
  413         INP_INFO_RUNLOCK(&udbinfo);
  414         if (inp)
  415                 INP_UNLOCK(inp);
  416 badunlocked:
  417         m_freem(m);
  418         if (opts)
  419                 m_freem(opts);
  420         return;
  421 }
  422 
  423 #ifdef INET6
  424 static void
  425 ip_2_ip6_hdr(ip6, ip)
  426         struct ip6_hdr *ip6;
  427         struct ip *ip;
  428 {
  429         bzero(ip6, sizeof(*ip6));
  430 
  431         ip6->ip6_vfc = IPV6_VERSION;
  432         ip6->ip6_plen = ip->ip_len;
  433         ip6->ip6_nxt = ip->ip_p;
  434         ip6->ip6_hlim = ip->ip_ttl;
  435         ip6->ip6_src.s6_addr32[2] = ip6->ip6_dst.s6_addr32[2] =
  436                 IPV6_ADDR_INT32_SMP;
  437         ip6->ip6_src.s6_addr32[3] = ip->ip_src.s_addr;
  438         ip6->ip6_dst.s6_addr32[3] = ip->ip_dst.s_addr;
  439 }
  440 #endif
  441 
  442 /*
  443  * subroutine of udp_input(), mainly for source code readability.
  444  * caller must properly init udp_ip6 and udp_in6 beforehand.
  445  */
  446 static void
  447 udp_append(last, ip, n, off)
  448         struct inpcb *last;
  449         struct ip *ip;
  450         struct mbuf *n;
  451         int off;
  452 {
  453         struct sockaddr *append_sa;
  454         struct mbuf *opts = 0;
  455 
  456 #ifdef IPSEC
  457         /* check AH/ESP integrity. */
  458         if (ipsec4_in_reject_so(n, last->inp_socket)) {
  459                 ipsecstat.in_polvio++;
  460                 m_freem(n);
  461                 return;
  462         }
  463 #endif /*IPSEC*/
  464 #ifdef FAST_IPSEC
  465         /* check AH/ESP integrity. */
  466         if (ipsec4_in_reject(n, last)) {
  467                 m_freem(n);
  468                 return;
  469         }
  470 #endif /*FAST_IPSEC*/
  471 #ifdef MAC
  472         if (mac_check_inpcb_deliver(last, n) != 0) {
  473                 m_freem(n);
  474                 return;
  475         }
  476 #endif
  477         if (last->inp_flags & INP_CONTROLOPTS ||
  478             last->inp_socket->so_options & SO_TIMESTAMP) {
  479 #ifdef INET6
  480                 if (last->inp_vflag & INP_IPV6) {
  481                         int savedflags;
  482 
  483                         if (udp_ip6.uip6_init_done == 0) {
  484                                 ip_2_ip6_hdr(&udp_ip6.uip6_ip6, ip);
  485                                 udp_ip6.uip6_init_done = 1;
  486                         }
  487                         savedflags = last->inp_flags;
  488                         last->inp_flags &= ~INP_UNMAPPABLEOPTS;
  489                         ip6_savecontrol(last, n, &opts);
  490                         last->inp_flags = savedflags;
  491                 } else
  492 #endif
  493                 ip_savecontrol(last, &opts, ip, n);
  494         }
  495 #ifdef INET6
  496         if (last->inp_vflag & INP_IPV6) {
  497                 if (udp_in6.uin6_init_done == 0) {
  498                         in6_sin_2_v4mapsin6(&udp_in, &udp_in6.uin6_sin);
  499                         udp_in6.uin6_init_done = 1;
  500                 }
  501                 append_sa = (struct sockaddr *)&udp_in6.uin6_sin;
  502         } else
  503 #endif
  504         append_sa = (struct sockaddr *)&udp_in;
  505         m_adj(n, off);
  506         if (sbappendaddr(&last->inp_socket->so_rcv, append_sa, n, opts) == 0) {
  507                 m_freem(n);
  508                 if (opts)
  509                         m_freem(opts);
  510                 udpstat.udps_fullsock++;
  511         } else
  512                 sorwakeup(last->inp_socket);
  513 }
  514 
  515 /*
  516  * Notify a udp user of an asynchronous error;
  517  * just wake up so that he can collect error status.
  518  */
  519 struct inpcb *
  520 udp_notify(inp, errno)
  521         register struct inpcb *inp;
  522         int errno;
  523 {
  524         inp->inp_socket->so_error = errno;
  525         sorwakeup(inp->inp_socket);
  526         sowwakeup(inp->inp_socket);
  527         return inp;
  528 }
  529 
  530 void
  531 udp_ctlinput(cmd, sa, vip)
  532         int cmd;
  533         struct sockaddr *sa;
  534         void *vip;
  535 {
  536         struct ip *ip = vip;
  537         struct udphdr *uh;
  538         struct inpcb *(*notify)(struct inpcb *, int) = udp_notify;
  539         struct in_addr faddr;
  540         struct inpcb *inp;
  541         int s;
  542 
  543         faddr = ((struct sockaddr_in *)sa)->sin_addr;
  544         if (sa->sa_family != AF_INET || faddr.s_addr == INADDR_ANY)
  545                 return;
  546 
  547         /*
  548          * Redirects don't need to be handled up here.
  549          */
  550         if (PRC_IS_REDIRECT(cmd))
  551                 return;
  552         /*
  553          * Hostdead is ugly because it goes linearly through all PCBs.
  554          * XXX: We never get this from ICMP, otherwise it makes an
  555          * excellent DoS attack on machines with many connections.
  556          */
  557         if (cmd == PRC_HOSTDEAD)
  558                 ip = 0;
  559         else if ((unsigned)cmd >= PRC_NCMDS || inetctlerrmap[cmd] == 0)
  560                 return;
  561         if (ip) {
  562                 s = splnet();
  563                 uh = (struct udphdr *)((caddr_t)ip + (ip->ip_hl << 2));
  564                 INP_INFO_RLOCK(&udbinfo);
  565                 inp = in_pcblookup_hash(&udbinfo, faddr, uh->uh_dport,
  566                     ip->ip_src, uh->uh_sport, 0, NULL);
  567                 if (inp != NULL) {
  568                         INP_LOCK(inp);
  569                         if (inp->inp_socket != NULL) {
  570                                 (*notify)(inp, inetctlerrmap[cmd]);
  571                         }
  572                         INP_UNLOCK(inp);
  573                 }
  574                 INP_INFO_RUNLOCK(&udbinfo);
  575                 splx(s);
  576         } else
  577                 in_pcbnotifyall(&udbinfo, faddr, inetctlerrmap[cmd], notify);
  578 }
  579 
  580 static int
  581 udp_pcblist(SYSCTL_HANDLER_ARGS)
  582 {
  583         int error, i, n, s;
  584         struct inpcb *inp, **inp_list;
  585         inp_gen_t gencnt;
  586         struct xinpgen xig;
  587 
  588         /*
  589          * The process of preparing the TCB list is too time-consuming and
  590          * resource-intensive to repeat twice on every request.
  591          */
  592         if (req->oldptr == 0) {
  593                 n = udbinfo.ipi_count;
  594                 req->oldidx = 2 * (sizeof xig)
  595                         + (n + n/8) * sizeof(struct xinpcb);
  596                 return 0;
  597         }
  598 
  599         if (req->newptr != 0)
  600                 return EPERM;
  601 
  602         /*
  603          * OK, now we're committed to doing something.
  604          */
  605         s = splnet();
  606         INP_INFO_RLOCK(&udbinfo);
  607         gencnt = udbinfo.ipi_gencnt;
  608         n = udbinfo.ipi_count;
  609         INP_INFO_RUNLOCK(&udbinfo);
  610         splx(s);
  611 
  612         sysctl_wire_old_buffer(req, 2 * (sizeof xig)
  613                 + n * sizeof(struct xinpcb));
  614 
  615         xig.xig_len = sizeof xig;
  616         xig.xig_count = n;
  617         xig.xig_gen = gencnt;
  618         xig.xig_sogen = so_gencnt;
  619         error = SYSCTL_OUT(req, &xig, sizeof xig);
  620         if (error)
  621                 return error;
  622 
  623         inp_list = malloc(n * sizeof *inp_list, M_TEMP, M_WAITOK);
  624         if (inp_list == 0)
  625                 return ENOMEM;
  626         
  627         s = splnet();
  628         INP_INFO_RLOCK(&udbinfo);
  629         for (inp = LIST_FIRST(udbinfo.listhead), i = 0; inp && i < n;
  630              inp = LIST_NEXT(inp, inp_list)) {
  631                 INP_LOCK(inp);
  632                 if (inp->inp_gencnt <= gencnt &&
  633                     cr_canseesocket(req->td->td_ucred, inp->inp_socket) == 0)
  634                         inp_list[i++] = inp;
  635                 INP_UNLOCK(inp);
  636         }
  637         INP_INFO_RUNLOCK(&udbinfo);
  638         splx(s);
  639         n = i;
  640 
  641         error = 0;
  642         for (i = 0; i < n; i++) {
  643                 inp = inp_list[i];
  644                 if (inp->inp_gencnt <= gencnt) {
  645                         struct xinpcb xi;
  646                         xi.xi_len = sizeof xi;
  647                         /* XXX should avoid extra copy */
  648                         bcopy(inp, &xi.xi_inp, sizeof *inp);
  649                         if (inp->inp_socket)
  650                                 sotoxsocket(inp->inp_socket, &xi.xi_socket);
  651                         xi.xi_inp.inp_gencnt = inp->inp_gencnt;
  652                         error = SYSCTL_OUT(req, &xi, sizeof xi);
  653                 }
  654         }
  655         if (!error) {
  656                 /*
  657                  * Give the user an updated idea of our state.
  658                  * If the generation differs from what we told
  659                  * her before, she knows that something happened
  660                  * while we were processing this request, and it
  661                  * might be necessary to retry.
  662                  */
  663                 s = splnet();
  664                 INP_INFO_RLOCK(&udbinfo);
  665                 xig.xig_gen = udbinfo.ipi_gencnt;
  666                 xig.xig_sogen = so_gencnt;
  667                 xig.xig_count = udbinfo.ipi_count;
  668                 INP_INFO_RUNLOCK(&udbinfo);
  669                 splx(s);
  670                 error = SYSCTL_OUT(req, &xig, sizeof xig);
  671         }
  672         free(inp_list, M_TEMP);
  673         return error;
  674 }
  675 
  676 SYSCTL_PROC(_net_inet_udp, UDPCTL_PCBLIST, pcblist, CTLFLAG_RD, 0, 0,
  677             udp_pcblist, "S,xinpcb", "List of active UDP sockets");
  678 
  679 static int
  680 udp_getcred(SYSCTL_HANDLER_ARGS)
  681 {
  682         struct xucred xuc;
  683         struct sockaddr_in addrs[2];
  684         struct inpcb *inp;
  685         int error, s;
  686 
  687         error = suser_cred(req->td->td_ucred, PRISON_ROOT);
  688         if (error)
  689                 return (error);
  690         error = SYSCTL_IN(req, addrs, sizeof(addrs));
  691         if (error)
  692                 return (error);
  693         s = splnet();
  694         INP_INFO_RLOCK(&udbinfo);
  695         inp = in_pcblookup_hash(&udbinfo, addrs[1].sin_addr, addrs[1].sin_port,
  696                                 addrs[0].sin_addr, addrs[0].sin_port, 1, NULL);
  697         if (inp == NULL || inp->inp_socket == NULL) {
  698                 error = ENOENT;
  699                 goto out;
  700         }
  701         error = cr_canseesocket(req->td->td_ucred, inp->inp_socket);
  702         if (error)
  703                 goto out;
  704         cru2x(inp->inp_socket->so_cred, &xuc);
  705 out:
  706         INP_INFO_RUNLOCK(&udbinfo);
  707         splx(s);
  708         if (error == 0)
  709                 error = SYSCTL_OUT(req, &xuc, sizeof(struct xucred));
  710         return (error);
  711 }
  712 
  713 SYSCTL_PROC(_net_inet_udp, OID_AUTO, getcred,
  714     CTLTYPE_OPAQUE|CTLFLAG_RW|CTLFLAG_PRISON, 0, 0,
  715     udp_getcred, "S,xucred", "Get the xucred of a UDP connection");
  716 
  717 static int
  718 udp_output(inp, m, addr, control, td)
  719         register struct inpcb *inp;
  720         struct mbuf *m;
  721         struct sockaddr *addr;
  722         struct mbuf *control;
  723         struct thread *td;
  724 {
  725         register struct udpiphdr *ui;
  726         register int len = m->m_pkthdr.len;
  727         struct in_addr faddr, laddr;
  728         struct cmsghdr *cm;
  729         struct sockaddr_in *sin, src;
  730         int error = 0;
  731         int ipflags;
  732         u_short fport, lport;
  733 
  734         INP_LOCK_ASSERT(inp);
  735 #ifdef MAC
  736         mac_create_mbuf_from_socket(inp->inp_socket, m);
  737 #endif
  738 
  739         if (len + sizeof(struct udpiphdr) > IP_MAXPACKET) {
  740                 error = EMSGSIZE;
  741                 if (control)
  742                         m_freem(control);
  743                 goto release;
  744         }
  745 
  746         src.sin_addr.s_addr = INADDR_ANY;
  747         if (control != NULL) {
  748                 /*
  749                  * XXX: Currently, we assume all the optional information
  750                  * is stored in a single mbuf.
  751                  */
  752                 if (control->m_next) {
  753                         error = EINVAL;
  754                         m_freem(control);
  755                         goto release;
  756                 }
  757                 for (; control->m_len > 0;
  758                     control->m_data += CMSG_ALIGN(cm->cmsg_len),
  759                     control->m_len -= CMSG_ALIGN(cm->cmsg_len)) {
  760                         cm = mtod(control, struct cmsghdr *);
  761                         if (control->m_len < sizeof(*cm) || cm->cmsg_len == 0 ||
  762                             cm->cmsg_len > control->m_len) {
  763                                 error = EINVAL;
  764                                 break;
  765                         }
  766                         if (cm->cmsg_level != IPPROTO_IP)
  767                                 continue;
  768 
  769                         switch (cm->cmsg_type) {
  770                         case IP_SENDSRCADDR:
  771                                 if (cm->cmsg_len !=
  772                                     CMSG_LEN(sizeof(struct in_addr))) {
  773                                         error = EINVAL;
  774                                         break;
  775                                 }
  776                                 bzero(&src, sizeof(src));
  777                                 src.sin_family = AF_INET;
  778                                 src.sin_len = sizeof(src);
  779                                 src.sin_port = inp->inp_lport;
  780                                 src.sin_addr = *(struct in_addr *)CMSG_DATA(cm);
  781                                 break;
  782                         default:
  783                                 error = ENOPROTOOPT;
  784                                 break;
  785                         }
  786                         if (error)
  787                                 break;
  788                 }
  789                 m_freem(control);
  790         }
  791         if (error)
  792                 goto release;
  793         laddr = inp->inp_laddr;
  794         lport = inp->inp_lport;
  795         if (src.sin_addr.s_addr != INADDR_ANY) {
  796                 if (lport == 0) {
  797                         error = EINVAL;
  798                         goto release;
  799                 }
  800                 error = in_pcbbind_setup(inp, (struct sockaddr *)&src,
  801                     &laddr.s_addr, &lport, td);
  802                 if (error)
  803                         goto release;
  804         }
  805 
  806         if (addr) {
  807                 sin = (struct sockaddr_in *)addr;
  808                 if (td && jailed(td->td_ucred))
  809                         prison_remote_ip(td->td_ucred, 0, &sin->sin_addr.s_addr);
  810                 if (inp->inp_faddr.s_addr != INADDR_ANY) {
  811                         error = EISCONN;
  812                         goto release;
  813                 }
  814                 error = in_pcbconnect_setup(inp, addr, &laddr.s_addr, &lport,
  815                     &faddr.s_addr, &fport, NULL, td);
  816                 if (error)
  817                         goto release;
  818 
  819                 /* Commit the local port if newly assigned. */
  820                 if (inp->inp_laddr.s_addr == INADDR_ANY &&
  821                     inp->inp_lport == 0) {
  822                         inp->inp_lport = lport;
  823                         if (in_pcbinshash(inp) != 0) {
  824                                 inp->inp_lport = 0;
  825                                 error = EAGAIN;
  826                                 goto release;
  827                         }
  828                         inp->inp_flags |= INP_ANONPORT;
  829                 }
  830         } else {
  831                 faddr = inp->inp_faddr;
  832                 fport = inp->inp_fport;
  833                 if (faddr.s_addr == INADDR_ANY) {
  834                         error = ENOTCONN;
  835                         goto release;
  836                 }
  837         }
  838         /*
  839          * Calculate data length and get a mbuf
  840          * for UDP and IP headers.
  841          */
  842         M_PREPEND(m, sizeof(struct udpiphdr), M_DONTWAIT);
  843         if (m == 0) {
  844                 error = ENOBUFS;
  845                 goto release;
  846         }
  847 
  848         /*
  849          * Fill in mbuf with extended UDP header
  850          * and addresses and length put into network format.
  851          */
  852         ui = mtod(m, struct udpiphdr *);
  853         bzero(ui->ui_x1, sizeof(ui->ui_x1));    /* XXX still needed? */
  854         ui->ui_pr = IPPROTO_UDP;
  855         ui->ui_src = laddr;
  856         ui->ui_dst = faddr;
  857         ui->ui_sport = lport;
  858         ui->ui_dport = fport;
  859         ui->ui_ulen = htons((u_short)len + sizeof(struct udphdr));
  860 
  861         ipflags = inp->inp_socket->so_options & (SO_DONTROUTE | SO_BROADCAST);
  862         if (inp->inp_flags & INP_ONESBCAST)
  863                 ipflags |= IP_SENDONES;
  864 
  865         /*
  866          * Set up checksum and output datagram.
  867          */
  868         if (udpcksum) {
  869                 if (inp->inp_flags & INP_ONESBCAST)
  870                         faddr.s_addr = INADDR_BROADCAST;
  871                 ui->ui_sum = in_pseudo(ui->ui_src.s_addr, faddr.s_addr,
  872                     htons((u_short)len + sizeof(struct udphdr) + IPPROTO_UDP));
  873                 m->m_pkthdr.csum_flags = CSUM_UDP;
  874                 m->m_pkthdr.csum_data = offsetof(struct udphdr, uh_sum);
  875         } else {
  876                 ui->ui_sum = 0;
  877         }
  878         ((struct ip *)ui)->ip_len = sizeof (struct udpiphdr) + len;
  879         ((struct ip *)ui)->ip_ttl = inp->inp_ip_ttl;    /* XXX */
  880         ((struct ip *)ui)->ip_tos = inp->inp_ip_tos;    /* XXX */
  881         udpstat.udps_opackets++;
  882 
  883         error = ip_output(m, inp->inp_options, NULL, ipflags,
  884             inp->inp_moptions, inp);
  885         return (error);
  886 
  887 release:
  888         m_freem(m);
  889         return (error);
  890 }
  891 
  892 u_long  udp_sendspace = 9216;           /* really max datagram size */
  893                                         /* 40 1K datagrams */
  894 SYSCTL_INT(_net_inet_udp, UDPCTL_MAXDGRAM, maxdgram, CTLFLAG_RW,
  895     &udp_sendspace, 0, "Maximum outgoing UDP datagram size");
  896 
  897 u_long  udp_recvspace = 40 * (1024 +
  898 #ifdef INET6
  899                                       sizeof(struct sockaddr_in6)
  900 #else
  901                                       sizeof(struct sockaddr_in)
  902 #endif
  903                                       );
  904 SYSCTL_INT(_net_inet_udp, UDPCTL_RECVSPACE, recvspace, CTLFLAG_RW,
  905     &udp_recvspace, 0, "Maximum incoming UDP datagram size");
  906 
  907 static int
  908 udp_abort(struct socket *so)
  909 {
  910         struct inpcb *inp;
  911         int s;
  912 
  913         INP_INFO_WLOCK(&udbinfo);
  914         inp = sotoinpcb(so);
  915         if (inp == 0) {
  916                 INP_INFO_WUNLOCK(&udbinfo);
  917                 return EINVAL;  /* ??? possible? panic instead? */
  918         }
  919         INP_LOCK(inp);
  920         soisdisconnected(so);
  921         s = splnet();
  922         in_pcbdetach(inp);
  923         INP_INFO_WUNLOCK(&udbinfo);
  924         splx(s);
  925         return 0;
  926 }
  927 
  928 static int
  929 udp_attach(struct socket *so, int proto, struct thread *td)
  930 {
  931         struct inpcb *inp;
  932         int s, error;
  933 
  934         INP_INFO_WLOCK(&udbinfo);
  935         inp = sotoinpcb(so);
  936         if (inp != 0) {
  937                 INP_INFO_WUNLOCK(&udbinfo);
  938                 return EINVAL;
  939         }
  940         error = soreserve(so, udp_sendspace, udp_recvspace);
  941         if (error) {
  942                 INP_INFO_WUNLOCK(&udbinfo);
  943                 return error;
  944         }
  945         s = splnet();
  946         error = in_pcballoc(so, &udbinfo, td, "udpinp");
  947         splx(s);
  948         if (error) {
  949                 INP_INFO_WUNLOCK(&udbinfo);
  950                 return error;
  951         }
  952 
  953         inp = (struct inpcb *)so->so_pcb;
  954         INP_LOCK(inp);
  955         INP_INFO_WUNLOCK(&udbinfo);
  956         inp->inp_vflag |= INP_IPV4;
  957         inp->inp_ip_ttl = ip_defttl;
  958         INP_UNLOCK(inp);
  959         return 0;
  960 }
  961 
  962 static int
  963 udp_bind(struct socket *so, struct sockaddr *nam, struct thread *td)
  964 {
  965         struct inpcb *inp;
  966         int s, error;
  967 
  968         INP_INFO_WLOCK(&udbinfo);
  969         inp = sotoinpcb(so);
  970         if (inp == 0) {
  971                 INP_INFO_WUNLOCK(&udbinfo);
  972                 return EINVAL;
  973         }
  974         INP_LOCK(inp);
  975         s = splnet();
  976         error = in_pcbbind(inp, nam, td);
  977         splx(s);
  978         INP_UNLOCK(inp);
  979         INP_INFO_WUNLOCK(&udbinfo);
  980         return error;
  981 }
  982 
  983 static int
  984 udp_connect(struct socket *so, struct sockaddr *nam, struct thread *td)
  985 {
  986         struct inpcb *inp;
  987         int s, error;
  988         struct sockaddr_in *sin;
  989 
  990         INP_INFO_WLOCK(&udbinfo);
  991         inp = sotoinpcb(so);
  992         if (inp == 0) {
  993                 INP_INFO_WUNLOCK(&udbinfo);
  994                 return EINVAL;
  995         }
  996         INP_LOCK(inp);
  997         if (inp->inp_faddr.s_addr != INADDR_ANY) {
  998                 INP_UNLOCK(inp);
  999                 INP_INFO_WUNLOCK(&udbinfo);
 1000                 return EISCONN;
 1001         }
 1002         s = splnet();
 1003         sin = (struct sockaddr_in *)nam;
 1004         if (td && jailed(td->td_ucred))
 1005                 prison_remote_ip(td->td_ucred, 0, &sin->sin_addr.s_addr);
 1006         error = in_pcbconnect(inp, nam, td);
 1007         splx(s);
 1008         if (error == 0)
 1009                 soisconnected(so);
 1010         INP_UNLOCK(inp);
 1011         INP_INFO_WUNLOCK(&udbinfo);
 1012         return error;
 1013 }
 1014 
 1015 static int
 1016 udp_detach(struct socket *so)
 1017 {
 1018         struct inpcb *inp;
 1019         int s;
 1020 
 1021         INP_INFO_WLOCK(&udbinfo);
 1022         inp = sotoinpcb(so);
 1023         if (inp == 0) {
 1024                 INP_INFO_WUNLOCK(&udbinfo);
 1025                 return EINVAL;
 1026         }
 1027         INP_LOCK(inp);
 1028         s = splnet();
 1029         in_pcbdetach(inp);
 1030         INP_INFO_WUNLOCK(&udbinfo);
 1031         splx(s);
 1032         return 0;
 1033 }
 1034 
 1035 static int
 1036 udp_disconnect(struct socket *so)
 1037 {
 1038         struct inpcb *inp;
 1039         int s;
 1040 
 1041         INP_INFO_WLOCK(&udbinfo);
 1042         inp = sotoinpcb(so);
 1043         if (inp == 0) {
 1044                 INP_INFO_WUNLOCK(&udbinfo);
 1045                 return EINVAL;
 1046         }
 1047         INP_LOCK(inp);
 1048         if (inp->inp_faddr.s_addr == INADDR_ANY) {
 1049                 INP_INFO_WUNLOCK(&udbinfo);
 1050                 INP_UNLOCK(inp);
 1051                 return ENOTCONN;
 1052         }
 1053 
 1054         s = splnet();
 1055         in_pcbdisconnect(inp);
 1056         inp->inp_laddr.s_addr = INADDR_ANY;
 1057         INP_UNLOCK(inp);
 1058         INP_INFO_WUNLOCK(&udbinfo);
 1059         splx(s);
 1060         so->so_state &= ~SS_ISCONNECTED;                /* XXX */
 1061         return 0;
 1062 }
 1063 
 1064 static int
 1065 udp_send(struct socket *so, int flags, struct mbuf *m, struct sockaddr *addr,
 1066             struct mbuf *control, struct thread *td)
 1067 {
 1068         struct inpcb *inp;
 1069         int ret;
 1070 
 1071         INP_INFO_WLOCK(&udbinfo);
 1072         inp = sotoinpcb(so);
 1073         if (inp == 0) {
 1074                 INP_INFO_WUNLOCK(&udbinfo);
 1075                 m_freem(m);
 1076                 return EINVAL;
 1077         }
 1078         INP_LOCK(inp);
 1079         ret = udp_output(inp, m, addr, control, td);
 1080         INP_UNLOCK(inp);
 1081         INP_INFO_WUNLOCK(&udbinfo);
 1082         return ret; 
 1083 }
 1084 
 1085 int
 1086 udp_shutdown(struct socket *so)
 1087 {
 1088         struct inpcb *inp;
 1089 
 1090         INP_INFO_RLOCK(&udbinfo);
 1091         inp = sotoinpcb(so);
 1092         if (inp == 0) {
 1093                 INP_INFO_RUNLOCK(&udbinfo);
 1094                 return EINVAL;
 1095         }
 1096         INP_LOCK(inp);
 1097         INP_INFO_RUNLOCK(&udbinfo);
 1098         socantsendmore(so);
 1099         INP_UNLOCK(inp);
 1100         return 0;
 1101 }
 1102 
 1103 /*
 1104  * This is the wrapper function for in_setsockaddr.  We just pass down 
 1105  * the pcbinfo for in_setsockaddr to lock.  We don't want to do the locking 
 1106  * here because in_setsockaddr will call malloc and might block.
 1107  */
 1108 static int
 1109 udp_sockaddr(struct socket *so, struct sockaddr **nam)
 1110 {
 1111         return (in_setsockaddr(so, nam, &udbinfo));
 1112 }
 1113 
 1114 /*
 1115  * This is the wrapper function for in_setpeeraddr.  We just pass down
 1116  * the pcbinfo for in_setpeeraddr to lock.
 1117  */
 1118 static int
 1119 udp_peeraddr(struct socket *so, struct sockaddr **nam)
 1120 {
 1121         return (in_setpeeraddr(so, nam, &udbinfo));
 1122 }
 1123 
 1124 struct pr_usrreqs udp_usrreqs = {
 1125         udp_abort, pru_accept_notsupp, udp_attach, udp_bind, udp_connect, 
 1126         pru_connect2_notsupp, in_control, udp_detach, udp_disconnect, 
 1127         pru_listen_notsupp, udp_peeraddr, pru_rcvd_notsupp, 
 1128         pru_rcvoob_notsupp, udp_send, pru_sense_null, udp_shutdown,
 1129         udp_sockaddr, sosend, soreceive, sopoll, in_pcbsosetlabel
 1130 };

Cache object: c00eefc88c4edc8aa94c5215e28243ea


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