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/kern/sys_socket.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, 1990, 1993
    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  *      @(#)sys_socket.c        8.1 (Berkeley) 6/10/93
   30  */
   31 
   32 #include <sys/cdefs.h>
   33 __FBSDID("$FreeBSD: releng/7.4/sys/kern/sys_socket.c 210086 2010-07-14 21:52:23Z jhb $");
   34 
   35 #include "opt_mac.h"
   36 
   37 #include <sys/param.h>
   38 #include <sys/systm.h>
   39 #include <sys/file.h>
   40 #include <sys/filedesc.h>
   41 #include <sys/proc.h>
   42 #include <sys/protosw.h>
   43 #include <sys/sigio.h>
   44 #include <sys/signal.h>
   45 #include <sys/signalvar.h>
   46 #include <sys/socket.h>
   47 #include <sys/socketvar.h>
   48 #include <sys/filio.h>                  /* XXX */
   49 #include <sys/sockio.h>
   50 #include <sys/stat.h>
   51 #include <sys/uio.h>
   52 #include <sys/ucred.h>
   53 
   54 #include <net/if.h>
   55 #include <net/route.h>
   56 
   57 #include <security/mac/mac_framework.h>
   58 
   59 struct fileops  socketops = {
   60         .fo_read = soo_read,
   61         .fo_write = soo_write,
   62         .fo_ioctl = soo_ioctl,
   63         .fo_poll = soo_poll,
   64         .fo_kqfilter = soo_kqfilter,
   65         .fo_stat = soo_stat,
   66         .fo_close = soo_close,
   67         .fo_flags = DFLAG_PASSABLE
   68 };
   69 
   70 /* ARGSUSED */
   71 int
   72 soo_read(struct file *fp, struct uio *uio, struct ucred *active_cred,
   73     int flags, struct thread *td)
   74 {
   75         struct socket *so = fp->f_data;
   76 #ifdef MAC
   77         int error;
   78 
   79         SOCK_LOCK(so);
   80         error = mac_check_socket_receive(active_cred, so);
   81         SOCK_UNLOCK(so);
   82         if (error)
   83                 return (error);
   84 #endif
   85         return (soreceive(so, 0, uio, 0, 0, 0));
   86 }
   87 
   88 /* ARGSUSED */
   89 int
   90 soo_write(struct file *fp, struct uio *uio, struct ucred *active_cred,
   91     int flags, struct thread *td)
   92 {
   93         struct socket *so = fp->f_data;
   94         int error;
   95 
   96 #ifdef MAC
   97         SOCK_LOCK(so);
   98         error = mac_check_socket_send(active_cred, so);
   99         SOCK_UNLOCK(so);
  100         if (error)
  101                 return (error);
  102 #endif
  103         error = sosend(so, 0, uio, 0, 0, 0, uio->uio_td);
  104         if (error == EPIPE && (so->so_options & SO_NOSIGPIPE) == 0) {
  105                 PROC_LOCK(uio->uio_td->td_proc);
  106                 tdsignal(uio->uio_td->td_proc, uio->uio_td, SIGPIPE, NULL);
  107                 PROC_UNLOCK(uio->uio_td->td_proc);
  108         }
  109         return (error);
  110 }
  111 
  112 int
  113 soo_ioctl(struct file *fp, u_long cmd, void *data, struct ucred *active_cred,
  114     struct thread *td)
  115 {
  116         struct socket *so = fp->f_data;
  117         int error = 0;
  118 
  119         switch (cmd) {
  120         case FIONBIO:
  121                 SOCK_LOCK(so);
  122                 if (*(int *)data)
  123                         so->so_state |= SS_NBIO;
  124                 else
  125                         so->so_state &= ~SS_NBIO;
  126                 SOCK_UNLOCK(so);
  127                 break;
  128 
  129         case FIOASYNC:
  130                 /*
  131                  * XXXRW: This code separately acquires SOCK_LOCK(so) and
  132                  * SOCKBUF_LOCK(&so->so_rcv) even though they are the same
  133                  * mutex to avoid introducing the assumption that they are
  134                  * the same.
  135                  */
  136                 if (*(int *)data) {
  137                         SOCK_LOCK(so);
  138                         so->so_state |= SS_ASYNC;
  139                         SOCK_UNLOCK(so);
  140                         SOCKBUF_LOCK(&so->so_rcv);
  141                         so->so_rcv.sb_flags |= SB_ASYNC;
  142                         SOCKBUF_UNLOCK(&so->so_rcv);
  143                         SOCKBUF_LOCK(&so->so_snd);
  144                         so->so_snd.sb_flags |= SB_ASYNC;
  145                         SOCKBUF_UNLOCK(&so->so_snd);
  146                 } else {
  147                         SOCK_LOCK(so);
  148                         so->so_state &= ~SS_ASYNC;
  149                         SOCK_UNLOCK(so);
  150                         SOCKBUF_LOCK(&so->so_rcv);
  151                         so->so_rcv.sb_flags &= ~SB_ASYNC;
  152                         SOCKBUF_UNLOCK(&so->so_rcv);
  153                         SOCKBUF_LOCK(&so->so_snd);
  154                         so->so_snd.sb_flags &= ~SB_ASYNC;
  155                         SOCKBUF_UNLOCK(&so->so_snd);
  156                 }
  157                 break;
  158 
  159         case FIONREAD:
  160                 /* Unlocked read. */
  161                 *(int *)data = so->so_rcv.sb_cc;
  162                 break;
  163 
  164         case FIONWRITE:
  165                 /* Unlocked read. */
  166                 *(int *)data = so->so_snd.sb_cc;
  167                 break;
  168 
  169         case FIONSPACE:
  170                 if ((so->so_snd.sb_hiwat < so->so_snd.sb_cc) ||
  171                     (so->so_snd.sb_mbmax < so->so_snd.sb_mbcnt))
  172                         *(int *)data = 0;
  173                 else
  174                         *(int *)data = sbspace(&so->so_snd);
  175                 break;
  176 
  177         case FIOSETOWN:
  178                 error = fsetown(*(int *)data, &so->so_sigio);
  179                 break;
  180 
  181         case FIOGETOWN:
  182                 *(int *)data = fgetown(&so->so_sigio);
  183                 break;
  184 
  185         case SIOCSPGRP:
  186                 error = fsetown(-(*(int *)data), &so->so_sigio);
  187                 break;
  188 
  189         case SIOCGPGRP:
  190                 *(int *)data = -fgetown(&so->so_sigio);
  191                 break;
  192 
  193         case SIOCATMARK:
  194                 /* Unlocked read. */
  195                 *(int *)data = (so->so_rcv.sb_state & SBS_RCVATMARK) != 0;
  196                 break;
  197         default:
  198                 /*
  199                  * Interface/routing/protocol specific ioctls: interface and
  200                  * routing ioctls should have a different entry since a
  201                  * socket is unnecessary.
  202                  */
  203                 if (IOCGROUP(cmd) == 'i')
  204                         error = ifioctl(so, cmd, data, td);
  205                 else if (IOCGROUP(cmd) == 'r')
  206                         error = rtioctl_fib(cmd, data, so->so_fibnum);
  207                 else
  208                         error = ((*so->so_proto->pr_usrreqs->pru_control)
  209                             (so, cmd, data, 0, td));
  210                 break;
  211         }
  212         return (error);
  213 }
  214 
  215 int
  216 soo_poll(struct file *fp, int events, struct ucred *active_cred,
  217     struct thread *td)
  218 {
  219         struct socket *so = fp->f_data;
  220 #ifdef MAC
  221         int error;
  222 
  223         SOCK_LOCK(so);
  224         error = mac_check_socket_poll(active_cred, so);
  225         SOCK_UNLOCK(so);
  226         if (error)
  227                 return (error);
  228 #endif
  229         return (sopoll(so, events, fp->f_cred, td));
  230 }
  231 
  232 int
  233 soo_stat(struct file *fp, struct stat *ub, struct ucred *active_cred,
  234     struct thread *td)
  235 {
  236         struct socket *so = fp->f_data;
  237 #ifdef MAC
  238         int error;
  239 #endif
  240 
  241         bzero((caddr_t)ub, sizeof (*ub));
  242         ub->st_mode = S_IFSOCK;
  243 #ifdef MAC
  244         SOCK_LOCK(so);
  245         error = mac_check_socket_stat(active_cred, so);
  246         SOCK_UNLOCK(so);
  247         if (error)
  248                 return (error);
  249 #endif
  250         /*
  251          * If SBS_CANTRCVMORE is set, but there's still data left in the
  252          * receive buffer, the socket is still readable.
  253          */
  254         SOCKBUF_LOCK(&so->so_rcv);
  255         if ((so->so_rcv.sb_state & SBS_CANTRCVMORE) == 0 ||
  256             so->so_rcv.sb_cc != 0)
  257                 ub->st_mode |= S_IRUSR | S_IRGRP | S_IROTH;
  258         ub->st_size = so->so_rcv.sb_cc - so->so_rcv.sb_ctl;
  259         SOCKBUF_UNLOCK(&so->so_rcv);
  260         /* Unlocked read. */
  261         if ((so->so_snd.sb_state & SBS_CANTSENDMORE) == 0)
  262                 ub->st_mode |= S_IWUSR | S_IWGRP | S_IWOTH;
  263         ub->st_uid = so->so_cred->cr_uid;
  264         ub->st_gid = so->so_cred->cr_gid;
  265         return (*so->so_proto->pr_usrreqs->pru_sense)(so, ub);
  266 }
  267 
  268 /*
  269  * API socket close on file pointer.  We call soclose() to close the socket
  270  * (including initiating closing protocols).  soclose() will sorele() the
  271  * file reference but the actual socket will not go away until the socket's
  272  * ref count hits 0.
  273  */
  274 /* ARGSUSED */
  275 int
  276 soo_close(struct file *fp, struct thread *td)
  277 {
  278         int error = 0;
  279         struct socket *so;
  280 
  281         so = fp->f_data;
  282         fp->f_ops = &badfileops;
  283         fp->f_data = NULL;
  284 
  285         if (so)
  286                 error = soclose(so);
  287         return (error);
  288 }

Cache object: 4ceb9897c499b492448f1217aa29f27b


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