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

Cache object: 6e75679e3fccd9d2eb98bc7014137e6c


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