[BACK]Return to uipc_socket.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / sys / kern

Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.

Diff for /src/sys/kern/uipc_socket.c between version 1.221 and 1.227

version 1.221, 2014/02/25 18:30:11 version 1.227, 2014/07/24 15:12:03
Line 62 
Line 62 
  *      @(#)uipc_socket.c       8.6 (Berkeley) 5/2/95   *      @(#)uipc_socket.c       8.6 (Berkeley) 5/2/95
  */   */
   
   /*
    * Socket operation routines.
    *
    * These routines are called by the routines in sys_socket.c or from a
    * system process, and implement the semantics of socket operations by
    * switching out to the protocol specific routines.
    */
   
 #include <sys/cdefs.h>  #include <sys/cdefs.h>
 __KERNEL_RCSID(0, "$NetBSD$");  __KERNEL_RCSID(0, "$NetBSD$");
   
Line 103  __KERNEL_RCSID(0, "$NetBSD$");
Line 111  __KERNEL_RCSID(0, "$NetBSD$");
 #include <uvm/uvm_loan.h>  #include <uvm/uvm_loan.h>
 #include <uvm/uvm_page.h>  #include <uvm/uvm_page.h>
   
 MALLOC_DEFINE(M_SOOPTS, "soopts", "socket options");  
 MALLOC_DEFINE(M_SONAME, "soname", "socket name");  MALLOC_DEFINE(M_SONAME, "soname", "socket name");
   
 extern const struct fileops socketops;  extern const struct fileops socketops;
Line 479  soinit1(void)
Line 486  soinit1(void)
 }  }
   
 /*  /*
  * Socket operation routines.   * socreate: create a new socket of the specified type and the protocol.
  * These routines are called by the routines in   *
  * sys_socket.c or from a system process, and   * => Caller may specify another socket for lock sharing (must not be held).
  * implement the semantics of socket operations by   * => Returns the new socket without lock held.
  * switching out to the protocol specific routines.  
  */   */
 /*ARGSUSED*/  
 int  int
 socreate(int dom, struct socket **aso, int type, int proto, struct lwp *l,  socreate(int dom, struct socket **aso, int type, int proto, struct lwp *l,
          struct socket *lockso)           struct socket *lockso)
Line 515  socreate(int dom, struct socket **aso, i
Line 520  socreate(int dom, struct socket **aso, i
                         return EPROTOTYPE;                          return EPROTOTYPE;
                 return EPROTONOSUPPORT;                  return EPROTONOSUPPORT;
         }          }
         if (prp->pr_usrreq == NULL)          if (prp->pr_usrreqs == NULL)
                 return EPROTONOSUPPORT;                  return EPROTONOSUPPORT;
         if (prp->pr_type != type)          if (prp->pr_type != type)
                 return EPROTOTYPE;                  return EPROTOTYPE;
Line 533  socreate(int dom, struct socket **aso, i
Line 538  socreate(int dom, struct socket **aso, i
         uid = kauth_cred_geteuid(l->l_cred);          uid = kauth_cred_geteuid(l->l_cred);
         so->so_uidinfo = uid_find(uid);          so->so_uidinfo = uid_find(uid);
         so->so_cpid = l->l_proc->p_pid;          so->so_cpid = l->l_proc->p_pid;
         if (lockso != NULL) {  
                 /* Caller wants us to share a lock. */          /*
            * Lock assigned and taken during PCB attach, unless we share
            * the lock with another socket, e.g. socketpair(2) case.
            */
           if (lockso) {
                 lock = lockso->so_lock;                  lock = lockso->so_lock;
                 so->so_lock = lock;                  so->so_lock = lock;
                 mutex_obj_hold(lock);                  mutex_obj_hold(lock);
                 /* XXX Why is this not solock, to match sounlock? */  
                 mutex_enter(lock);                  mutex_enter(lock);
         } else {  
                 /* Lock assigned and taken during PRU_ATTACH. */  
         }          }
         error = (*prp->pr_usrreq)(so, PRU_ATTACH, NULL,  
             (struct mbuf *)(long)proto, NULL, l);          /* Attach the PCB (returns with the socket lock held). */
           error = (*prp->pr_usrreqs->pr_attach)(so, proto);
         KASSERT(solocked(so));          KASSERT(solocked(so));
         if (error != 0) {  
           if (error) {
                   KASSERT(so->so_pcb == NULL);
                 so->so_state |= SS_NOFDREF;                  so->so_state |= SS_NOFDREF;
                 sofree(so);                  sofree(so);
                 return error;                  return error;
         }          }
         so->so_cred = kauth_cred_dup(l->l_cred);          so->so_cred = kauth_cred_dup(l->l_cred);
         sounlock(so);          sounlock(so);
   
         *aso = so;          *aso = so;
         return 0;          return 0;
 }  }
   
 /* On success, write file descriptor to fdout and return zero.  On  /*
  * failure, return non-zero; *fdout will be undefined.   * fsocreate: create a socket and a file descriptor associated with it.
    *
    * => On success, write file descriptor to fdout and return zero.
    * => On failure, return non-zero; *fdout will be undefined.
  */   */
 int  int
 fsocreate(int domain, struct socket **sop, int type, int protocol,  fsocreate(int domain, struct socket **sop, int type, int proto, int *fdout)
     struct lwp *l, int *fdout)  
 {  {
         struct socket   *so;          lwp_t *l = curlwp;
         struct file     *fp;          int error, fd, flags;
         int             fd, error;          struct socket *so;
         int             flags = type & SOCK_FLAGS_MASK;          struct file *fp;
   
         type &= ~SOCK_FLAGS_MASK;          if ((error = fd_allocfile(&fp, &fd)) != 0) {
         if ((error = fd_allocfile(&fp, &fd)) != 0)  
                 return error;                  return error;
           }
           flags = type & SOCK_FLAGS_MASK;
         fd_set_exclose(l, fd, (flags & SOCK_CLOEXEC) != 0);          fd_set_exclose(l, fd, (flags & SOCK_CLOEXEC) != 0);
         fp->f_flag = FREAD|FWRITE|((flags & SOCK_NONBLOCK) ? FNONBLOCK : 0)|          fp->f_flag = FREAD|FWRITE|((flags & SOCK_NONBLOCK) ? FNONBLOCK : 0)|
             ((flags & SOCK_NOSIGPIPE) ? FNOSIGPIPE : 0);              ((flags & SOCK_NOSIGPIPE) ? FNOSIGPIPE : 0);
         fp->f_type = DTYPE_SOCKET;          fp->f_type = DTYPE_SOCKET;
         fp->f_ops = &socketops;          fp->f_ops = &socketops;
         error = socreate(domain, &so, type, protocol, l, NULL);  
         if (error != 0) {          type &= ~SOCK_FLAGS_MASK;
           error = socreate(domain, &so, type, proto, l, NULL);
           if (error) {
                 fd_abort(curproc, fp, fd);                  fd_abort(curproc, fp, fd);
         } else {                  return error;
                 if (sop != NULL)          }
                         *sop = so;          if (flags & SOCK_NONBLOCK) {
                 fp->f_data = so;                  so->so_state |= SS_NBIO;
                 fd_affix(curproc, fp, fd);  
                 *fdout = fd;  
                 if (flags & SOCK_NONBLOCK)  
                         so->so_state |= SS_NBIO;  
         }          }
           fp->f_data = so;
           fd_affix(curproc, fp, fd);
   
           if (sop != NULL) {
                   *sop = so;
           }
           *fdout = fd;
         return error;          return error;
 }  }
   
Line 611  sobind(struct socket *so, struct mbuf *n
Line 629  sobind(struct socket *so, struct mbuf *n
         int     error;          int     error;
   
         solock(so);          solock(so);
         error = (*so->so_proto->pr_usrreq)(so, PRU_BIND, NULL, nam, NULL, l);          error = (*so->so_proto->pr_usrreqs->pr_bind)(so, nam);
         sounlock(so);          sounlock(so);
         return error;          return error;
 }  }
Line 624  solisten(struct socket *so, int backlog,
Line 642  solisten(struct socket *so, int backlog,
         solock(so);          solock(so);
         if ((so->so_state & (SS_ISCONNECTED | SS_ISCONNECTING |          if ((so->so_state & (SS_ISCONNECTED | SS_ISCONNECTING |
             SS_ISDISCONNECTING)) != 0) {              SS_ISDISCONNECTING)) != 0) {
                 sounlock(so);                  sounlock(so);
                 return (EINVAL);                  return EINVAL;
         }          }
         error = (*so->so_proto->pr_usrreq)(so, PRU_LISTEN, NULL,          error = (*so->so_proto->pr_usrreqs->pr_listen)(so);
             NULL, NULL, l);  
         if (error != 0) {          if (error != 0) {
                 sounlock(so);                  sounlock(so);
                 return error;                  return error;
Line 685  sofree(struct socket *so)
Line 702  sofree(struct socket *so)
 }  }
   
 /*  /*
  * Close a socket on last file table reference removal.   * soclose: close a socket on last file table reference removal.
  * Initiate disconnect if connected.   * Initiate disconnect if connected.  Free socket when disconnect complete.
  * Free socket when disconnect complete.  
  */   */
 int  int
 soclose(struct socket *so)  soclose(struct socket *so)
 {  {
         struct socket   *so2;          struct socket *so2;
         int             error;          int error = 0;
         int             error2;  
   
         error = 0;  
         solock(so);          solock(so);
         if (so->so_options & SO_ACCEPTCONN) {          if (so->so_options & SO_ACCEPTCONN) {
                 for (;;) {                  for (;;) {
Line 719  soclose(struct socket *so)
Line 733  soclose(struct socket *so)
                         break;                          break;
                 }                  }
         }          }
         if (so->so_pcb == 0)          if (so->so_pcb == NULL)
                 goto discard;                  goto discard;
         if (so->so_state & SS_ISCONNECTED) {          if (so->so_state & SS_ISCONNECTED) {
                 if ((so->so_state & SS_ISDISCONNECTING) == 0) {                  if ((so->so_state & SS_ISDISCONNECTING) == 0) {
Line 740  soclose(struct socket *so)
Line 754  soclose(struct socket *so)
         }          }
  drop:   drop:
         if (so->so_pcb) {          if (so->so_pcb) {
                 error2 = (*so->so_proto->pr_usrreq)(so, PRU_DETACH,                  KASSERT(solocked(so));
                     NULL, NULL, NULL, NULL);                  (*so->so_proto->pr_usrreqs->pr_detach)(so);
                 if (error == 0)  
                         error = error2;  
         }          }
  discard:   discard:
         if (so->so_state & SS_NOFDREF)          KASSERT((so->so_state & SS_NOFDREF) == 0);
                 panic("soclose: NOFDREF");  
         kauth_cred_free(so->so_cred);          kauth_cred_free(so->so_cred);
         so->so_state |= SS_NOFDREF;          so->so_state |= SS_NOFDREF;
         sofree(so);          sofree(so);
         return (error);          return error;
 }  }
   
 /*  /*
Line 767  soabort(struct socket *so)
Line 778  soabort(struct socket *so)
         KASSERT(so->so_head == NULL);          KASSERT(so->so_head == NULL);
   
         so->so_aborting++;              /* XXX */          so->so_aborting++;              /* XXX */
         error = (*so->so_proto->pr_usrreq)(so, PRU_ABORT, NULL,          error = (*so->so_proto->pr_usrreqs->pr_generic)(so,
             NULL, NULL, NULL);              PRU_ABORT, NULL, NULL, NULL, NULL);
         refs = --so->so_aborting;       /* XXX */          refs = --so->so_aborting;       /* XXX */
         if (error || (refs == 0)) {          if (error || (refs == 0)) {
                 sofree(so);                  sofree(so);
Line 781  soabort(struct socket *so)
Line 792  soabort(struct socket *so)
 int  int
 soaccept(struct socket *so, struct mbuf *nam)  soaccept(struct socket *so, struct mbuf *nam)
 {  {
         int     error;          int error;
   
         KASSERT(solocked(so));          KASSERT(solocked(so));
           KASSERT((so->so_state & SS_NOFDREF) != 0);
   
         error = 0;  
         if ((so->so_state & SS_NOFDREF) == 0)  
                 panic("soaccept: !NOFDREF");  
         so->so_state &= ~SS_NOFDREF;          so->so_state &= ~SS_NOFDREF;
         if ((so->so_state & SS_ISDISCONNECTED) == 0 ||          if ((so->so_state & SS_ISDISCONNECTED) == 0 ||
             (so->so_proto->pr_flags & PR_ABRTACPTDIS) == 0)              (so->so_proto->pr_flags & PR_ABRTACPTDIS) == 0)
                 error = (*so->so_proto->pr_usrreq)(so, PRU_ACCEPT,                  error = (*so->so_proto->pr_usrreqs->pr_accept)(so, nam);
                     NULL, nam, NULL, NULL);  
         else          else
                 error = ECONNABORTED;                  error = ECONNABORTED;
   
         return (error);          return error;
 }  }
   
 int  int
 soconnect(struct socket *so, struct mbuf *nam, struct lwp *l)  soconnect(struct socket *so, struct mbuf *nam, struct lwp *l)
 {  {
         int             error;          int error;
   
         KASSERT(solocked(so));          KASSERT(solocked(so));
   
         if (so->so_options & SO_ACCEPTCONN)          if (so->so_options & SO_ACCEPTCONN)
                 return (EOPNOTSUPP);                  return EOPNOTSUPP;
         /*          /*
          * If protocol is connection-based, can only connect once.           * If protocol is connection-based, can only connect once.
          * Otherwise, if connected, try to disconnect first.           * Otherwise, if connected, try to disconnect first.
Line 819  soconnect(struct socket *so, struct mbuf
Line 827  soconnect(struct socket *so, struct mbuf
             (error = sodisconnect(so))))              (error = sodisconnect(so))))
                 error = EISCONN;                  error = EISCONN;
         else          else
                 error = (*so->so_proto->pr_usrreq)(so, PRU_CONNECT,                  error = (*so->so_proto->pr_usrreqs->pr_generic)(so,
                     NULL, nam, NULL, l);                      PRU_CONNECT, NULL, nam, NULL, l);
         return (error);  
           return error;
 }  }
   
 int  int
 soconnect2(struct socket *so1, struct socket *so2)  soconnect2(struct socket *so1, struct socket *so2)
 {  {
         int     error;  
   
         KASSERT(solocked2(so1, so2));          KASSERT(solocked2(so1, so2));
   
         error = (*so1->so_proto->pr_usrreq)(so1, PRU_CONNECT2,          return (*so1->so_proto->pr_usrreqs->pr_generic)(so1,
             NULL, (struct mbuf *)so2, NULL, NULL);              PRU_CONNECT2, NULL, (struct mbuf *)so2, NULL, NULL);
         return (error);  
 }  }
   
 int  int
Line 848  sodisconnect(struct socket *so)
Line 854  sodisconnect(struct socket *so)
         } else if (so->so_state & SS_ISDISCONNECTING) {          } else if (so->so_state & SS_ISDISCONNECTING) {
                 error = EALREADY;                  error = EALREADY;
         } else {          } else {
                 error = (*so->so_proto->pr_usrreq)(so, PRU_DISCONNECT,                  error = (*so->so_proto->pr_usrreqs->pr_generic)(so,
                     NULL, NULL, NULL, NULL);                      PRU_DISCONNECT, NULL, NULL, NULL, NULL);
         }          }
         return (error);          return (error);
 }  }
Line 1045  sosend(struct socket *so, struct mbuf *a
Line 1051  sosend(struct socket *so, struct mbuf *a
                                 so->so_options |= SO_DONTROUTE;                                  so->so_options |= SO_DONTROUTE;
                         if (resid > 0)                          if (resid > 0)
                                 so->so_state |= SS_MORETOCOME;                                  so->so_state |= SS_MORETOCOME;
                         error = (*so->so_proto->pr_usrreq)(so,                          if (flags & MSG_OOB)
                             (flags & MSG_OOB) ? PRU_SENDOOB : PRU_SEND,                                  error = (*so->so_proto->pr_usrreqs->pr_sendoob)(so,
                             top, addr, control, curlwp);                                      top, control);
                           else
                                   error = (*so->so_proto->pr_usrreqs->pr_generic)(so,
                                       PRU_SEND, top, addr, control, curlwp);
                         if (dontroute)                          if (dontroute)
                                 so->so_options &= ~SO_DONTROUTE;                                  so->so_options &= ~SO_DONTROUTE;
                         if (resid > 0)                          if (resid > 0)
Line 1159  soreceive(struct socket *so, struct mbuf
Line 1168  soreceive(struct socket *so, struct mbuf
         if (flags & MSG_OOB) {          if (flags & MSG_OOB) {
                 m = m_get(M_WAIT, MT_DATA);                  m = m_get(M_WAIT, MT_DATA);
                 solock(so);                  solock(so);
                 error = (*pr->pr_usrreq)(so, PRU_RCVOOB, m,                  error = (*pr->pr_usrreqs->pr_recvoob)(so, m, flags & MSG_PEEK);
                     (struct mbuf *)(long)(flags & MSG_PEEK), NULL, l);  
                 sounlock(so);                  sounlock(so);
                 if (error)                  if (error)
                         goto bad;                          goto bad;
Line 1527  soreceive(struct socket *so, struct mbuf
Line 1535  soreceive(struct socket *so, struct mbuf
                          * get it filled again.                           * get it filled again.
                          */                           */
                         if ((pr->pr_flags & PR_WANTRCVD) && so->so_pcb)                          if ((pr->pr_flags & PR_WANTRCVD) && so->so_pcb)
                                 (*pr->pr_usrreq)(so, PRU_RCVD,                                  (*pr->pr_usrreqs->pr_generic)(so, PRU_RCVD,
                                     NULL, (struct mbuf *)(long)flags, NULL, l);                                      NULL, (struct mbuf *)(long)flags, NULL, l);
                         SBLASTRECORDCHK(&so->so_rcv, "soreceive sbwait 2");                          SBLASTRECORDCHK(&so->so_rcv, "soreceive sbwait 2");
                         SBLASTMBUFCHK(&so->so_rcv, "soreceive sbwait 2");                          SBLASTMBUFCHK(&so->so_rcv, "soreceive sbwait 2");
Line 1569  soreceive(struct socket *so, struct mbuf
Line 1577  soreceive(struct socket *so, struct mbuf
                 SBLASTRECORDCHK(&so->so_rcv, "soreceive 4");                  SBLASTRECORDCHK(&so->so_rcv, "soreceive 4");
                 SBLASTMBUFCHK(&so->so_rcv, "soreceive 4");                  SBLASTMBUFCHK(&so->so_rcv, "soreceive 4");
                 if (pr->pr_flags & PR_WANTRCVD && so->so_pcb)                  if (pr->pr_flags & PR_WANTRCVD && so->so_pcb)
                         (*pr->pr_usrreq)(so, PRU_RCVD, NULL,                          (*pr->pr_usrreqs->pr_generic)(so, PRU_RCVD, NULL,
                             (struct mbuf *)(long)flags, NULL, l);                              (struct mbuf *)(long)flags, NULL, l);
         }          }
         if (orig_resid == uio->uio_resid && orig_resid &&          if (orig_resid == uio->uio_resid && orig_resid &&
Line 1604  soshutdown(struct socket *so, int how)
Line 1612  soshutdown(struct socket *so, int how)
                 error = 0;                  error = 0;
         }          }
         if (how == SHUT_WR || how == SHUT_RDWR)          if (how == SHUT_WR || how == SHUT_RDWR)
                 error = (*pr->pr_usrreq)(so, PRU_SHUTDOWN, NULL,                  error = (*pr->pr_usrreqs->pr_generic)(so,
                     NULL, NULL, NULL);                      PRU_SHUTDOWN, NULL, NULL, NULL, NULL);
   
         return error;          return error;
 }  }

Legend:
Removed from v.1.221  
changed lines
  Added in v.1.227

CVSweb <webmaster@jp.NetBSD.org>