[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.211.2.4 and 1.212

version 1.211.2.4, 2014/08/20 00:04:29 version 1.212, 2012/10/08 19:20:45
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 111  __KERNEL_RCSID(0, "$NetBSD$");
Line 103  __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 423  socket_listener_cb(kauth_cred_t cred, ka
Line 416  socket_listener_cb(kauth_cred_t cred, ka
                 /* Normal users can only drop their own connections. */                  /* Normal users can only drop their own connections. */
                 struct socket *so = (struct socket *)arg1;                  struct socket *so = (struct socket *)arg1;
   
                 if (so->so_cred && proc_uidmatch(cred, so->so_cred) == 0)                  if (proc_uidmatch(cred, so->so_cred))
                         result = KAUTH_RESULT_ALLOW;                          result = KAUTH_RESULT_ALLOW;
   
                 break;                  break;
Line 486  soinit1(void)
Line 479  soinit1(void)
 }  }
   
 /*  /*
  * socreate: create a new socket of the specified type and the protocol.   * Socket operation routines.
  *   * These routines are called by the routines in
  * => Caller may specify another socket for lock sharing (must not be held).   * sys_socket.c or from a system process, and
  * => Returns the new socket without lock held.   * implement the semantics of socket operations by
    * 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 520  socreate(int dom, struct socket **aso, i
Line 515  socreate(int dom, struct socket **aso, i
                         return EPROTOTYPE;                          return EPROTOTYPE;
                 return EPROTONOSUPPORT;                  return EPROTONOSUPPORT;
         }          }
         if (prp->pr_usrreqs == NULL)          if (prp->pr_usrreq == NULL)
                 return EPROTONOSUPPORT;                  return EPROTONOSUPPORT;
         if (prp->pr_type != type)          if (prp->pr_type != type)
                 return EPROTOTYPE;                  return EPROTOTYPE;
Line 538  socreate(int dom, struct socket **aso, i
Line 533  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);
                 mutex_enter(lock);                  mutex_enter(lock);
           } else {
                   /* Lock assigned and taken during PRU_ATTACH. */
         }          }
           error = (*prp->pr_usrreq)(so, PRU_ATTACH, NULL,
         /* Attach the PCB (returns with the socket lock held). */              (struct mbuf *)(long)proto, NULL, l);
         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
  * fsocreate: create a socket and a file descriptor associated with it.   * failure, return non-zero; *fdout will be undefined.
  *  
  * => 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 proto, int *fdout)  fsocreate(int domain, struct socket **sop, int type, int protocol,
       struct lwp *l, int *fdout)
 {  {
         lwp_t *l = curlwp;          struct socket   *so;
         int error, fd, flags;          struct file     *fp;
         struct socket *so;          int             fd, error;
         struct file *fp;          int             flags = type & SOCK_FLAGS_MASK;
   
         if ((error = fd_allocfile(&fp, &fd)) != 0) {          type &= ~SOCK_FLAGS_MASK;
           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);
         type &= ~SOCK_FLAGS_MASK;          if (error != 0) {
         error = socreate(domain, &so, type, proto, l, NULL);  
         if (error) {  
                 fd_abort(curproc, fp, fd);                  fd_abort(curproc, fp, fd);
                 return error;          } else {
         }                  if (sop != NULL)
         if (flags & SOCK_NONBLOCK) {                          *sop = so;
                 so->so_state |= SS_NBIO;                  fp->f_data = so;
         }                  fd_affix(curproc, fp, fd);
         fp->f_data = so;                  *fdout = fd;
         fd_affix(curproc, fp, fd);  
   
         if (sop != NULL) {  
                 *sop = so;  
         }          }
         *fdout = fd;  
         return error;          return error;
 }  }
   
Line 629  sobind(struct socket *so, struct mbuf *n
Line 608  sobind(struct socket *so, struct mbuf *n
         int     error;          int     error;
   
         solock(so);          solock(so);
         error = (*so->so_proto->pr_usrreqs->pr_bind)(so, nam, l);          error = (*so->so_proto->pr_usrreq)(so, PRU_BIND, NULL, nam, NULL, l);
         sounlock(so);          sounlock(so);
         return error;          return error;
 }  }
Line 642  solisten(struct socket *so, int backlog,
Line 621  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_usrreqs->pr_listen)(so, l);          error = (*so->so_proto->pr_usrreq)(so, PRU_LISTEN, NULL,
               NULL, NULL, l);
         if (error != 0) {          if (error != 0) {
                 sounlock(so);                  sounlock(so);
                 return error;                  return error;
Line 702  sofree(struct socket *so)
Line 682  sofree(struct socket *so)
 }  }
   
 /*  /*
  * soclose: close a socket on last file table reference removal.   * Close a socket on last file table reference removal.
  * Initiate disconnect if connected.  Free socket when disconnect complete.   * Initiate disconnect if connected.
    * Free socket when disconnect complete.
  */   */
 int  int
 soclose(struct socket *so)  soclose(struct socket *so)
 {  {
         struct socket *so2;          struct socket   *so2;
         int error = 0;          int             error;
           int             error2;
   
           error = 0;
         solock(so);          solock(so);
         if (so->so_options & SO_ACCEPTCONN) {          if (so->so_options & SO_ACCEPTCONN) {
                 for (;;) {                  for (;;) {
Line 733  soclose(struct socket *so)
Line 716  soclose(struct socket *so)
                         break;                          break;
                 }                  }
         }          }
         if (so->so_pcb == NULL)          if (so->so_pcb == 0)
                 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 754  soclose(struct socket *so)
Line 737  soclose(struct socket *so)
         }          }
  drop:   drop:
         if (so->so_pcb) {          if (so->so_pcb) {
                 KASSERT(solocked(so));                  error2 = (*so->so_proto->pr_usrreq)(so, PRU_DETACH,
                 (*so->so_proto->pr_usrreqs->pr_detach)(so);                      NULL, NULL, NULL, NULL);
                   if (error == 0)
                           error = error2;
         }          }
  discard:   discard:
         KASSERT((so->so_state & SS_NOFDREF) == 0);          if (so->so_state & SS_NOFDREF)
                   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 778  soabort(struct socket *so)
Line 764  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_usrreqs->pr_abort)(so);          error = (*so->so_proto->pr_usrreq)(so, 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 791  soabort(struct socket *so)
Line 778  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_usrreqs->pr_accept)(so, nam);                  error = (*so->so_proto->pr_usrreq)(so, PRU_ACCEPT,
                       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 826  soconnect(struct socket *so, struct mbuf
Line 816  soconnect(struct socket *so, struct mbuf
             (error = sodisconnect(so))))              (error = sodisconnect(so))))
                 error = EISCONN;                  error = EISCONN;
         else          else
                 error = (*so->so_proto->pr_usrreqs->pr_connect)(so, nam, l);                  error = (*so->so_proto->pr_usrreq)(so, 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));
   
         return (*so1->so_proto->pr_usrreqs->pr_connect2)(so1, so2);          error = (*so1->so_proto->pr_usrreq)(so1, PRU_CONNECT2,
               NULL, (struct mbuf *)so2, NULL, NULL);
           return (error);
 }  }
   
 int  int
Line 851  sodisconnect(struct socket *so)
Line 845  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_usrreqs->pr_disconnect)(so);                  error = (*so->so_proto->pr_usrreq)(so, PRU_DISCONNECT,
                       NULL, NULL, NULL, NULL);
         }          }
         return (error);          return (error);
 }  }
Line 879  sosend(struct socket *so, struct mbuf *a
Line 874  sosend(struct socket *so, struct mbuf *a
         struct mbuf *control, int flags, struct lwp *l)          struct mbuf *control, int flags, struct lwp *l)
 {  {
         struct mbuf     **mp, *m;          struct mbuf     **mp, *m;
           struct proc     *p;
         long            space, len, resid, clen, mlen;          long            space, len, resid, clen, mlen;
         int             error, s, dontroute, atomic;          int             error, s, dontroute, atomic;
         short           wakeup_state = 0;          short           wakeup_state = 0;
   
           p = l->l_proc;
         clen = 0;          clen = 0;
   
         /*          /*
Line 929  sosend(struct socket *so, struct mbuf *a
Line 926  sosend(struct socket *so, struct mbuf *a
                 }                  }
                 if ((so->so_state & SS_ISCONNECTED) == 0) {                  if ((so->so_state & SS_ISCONNECTED) == 0) {
                         if (so->so_proto->pr_flags & PR_CONNREQUIRED) {                          if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
                                 if (resid || clen == 0) {                                  if ((so->so_state & SS_ISCONFIRMING) == 0 &&
                                       !(resid == 0 && clen != 0)) {
                                         error = ENOTCONN;                                          error = ENOTCONN;
                                         goto release;                                          goto release;
                                 }                                  }
Line 1047  sosend(struct socket *so, struct mbuf *a
Line 1045  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;
                         if (flags & MSG_OOB)                          error = (*so->so_proto->pr_usrreq)(so,
                                 error = (*so->so_proto->pr_usrreqs->pr_sendoob)(so,                              (flags & MSG_OOB) ? PRU_SENDOOB : PRU_SEND,
                                     top, control);                              top, addr, control, curlwp);
                         else  
                                 error = (*so->so_proto->pr_usrreqs->pr_send)(so,  
                                     top, addr, control, l);  
                         if (dontroute)                          if (dontroute)
                                 so->so_options &= ~SO_DONTROUTE;                                  so->so_options &= ~SO_DONTROUTE;
                         if (resid > 0)                          if (resid > 0)
Line 1164  soreceive(struct socket *so, struct mbuf
Line 1159  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_usrreqs->pr_recvoob)(so, m, flags & MSG_PEEK);                  error = (*pr->pr_usrreq)(so, PRU_RCVOOB, m,
                       (struct mbuf *)(long)(flags & MSG_PEEK), NULL, l);
                 sounlock(so);                  sounlock(so);
                 if (error)                  if (error)
                         goto bad;                          goto bad;
Line 1188  soreceive(struct socket *so, struct mbuf
Line 1184  soreceive(struct socket *so, struct mbuf
          */           */
         s = splsoftnet();          s = splsoftnet();
         solock(so);          solock(so);
           if (so->so_state & SS_ISCONFIRMING && uio->uio_resid)
                   (*pr->pr_usrreq)(so, PRU_RCVD, NULL, NULL, NULL, l);
   
  restart:   restart:
         if ((error = sblock(&so->so_rcv, SBLOCKWAIT(flags))) != 0) {          if ((error = sblock(&so->so_rcv, SBLOCKWAIT(flags))) != 0) {
                 sounlock(so);                  sounlock(so);
Line 1531  soreceive(struct socket *so, struct mbuf
Line 1530  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_usrreqs->pr_rcvd)(so, flags, l);                                  (*pr->pr_usrreq)(so, PRU_RCVD,
                                       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");
                         if (wakeup_state & SS_RESTARTSYS)                          if (wakeup_state & SS_RESTARTSYS)
Line 1572  soreceive(struct socket *so, struct mbuf
Line 1572  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_usrreqs->pr_rcvd)(so, flags, l);                          (*pr->pr_usrreq)(so, PRU_RCVD, NULL,
                               (struct mbuf *)(long)flags, NULL, l);
         }          }
         if (orig_resid == uio->uio_resid && orig_resid &&          if (orig_resid == uio->uio_resid && orig_resid &&
             (flags & MSG_EOR) == 0 && (so->so_state & SS_CANTRCVMORE) == 0) {              (flags & MSG_EOR) == 0 && (so->so_state & SS_CANTRCVMORE) == 0) {
Line 1606  soshutdown(struct socket *so, int how)
Line 1607  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_usrreqs->pr_shutdown)(so);                  error = (*pr->pr_usrreq)(so, PRU_SHUTDOWN, NULL,
                       NULL, NULL, NULL);
   
         return error;          return error;
 }  }
Line 1665  sorflush(struct socket *so)
Line 1667  sorflush(struct socket *so)
 static int  static int
 sosetopt1(struct socket *so, const struct sockopt *sopt)  sosetopt1(struct socket *so, const struct sockopt *sopt)
 {  {
         int error = EINVAL, opt;          int error = EINVAL, optval, opt;
         int optval = 0; /* XXX: gcc */  
         struct linger l;          struct linger l;
         struct timeval tv;          struct timeval tv;
   
Line 1908  sogetopt1(struct socket *so, struct sock
Line 1909  sogetopt1(struct socket *so, struct sock
 #ifdef SO_OTIMESTAMP  #ifdef SO_OTIMESTAMP
         case SO_OTIMESTAMP:          case SO_OTIMESTAMP:
 #endif  #endif
         case SO_ACCEPTCONN:  
                 error = sockopt_setint(sopt, (so->so_options & opt) ? 1 : 0);                  error = sockopt_setint(sopt, (so->so_options & opt) ? 1 : 0);
                 break;                  break;
   
Line 2426  sysctl_kern_socket_setup(void)
Line 2426  sysctl_kern_socket_setup(void)
 {  {
   
         KASSERT(socket_sysctllog == NULL);          KASSERT(socket_sysctllog == NULL);
           sysctl_createv(&socket_sysctllog, 0, NULL, NULL,
                          CTLFLAG_PERMANENT,
                          CTLTYPE_NODE, "kern", NULL,
                          NULL, 0, NULL, 0,
                          CTL_KERN, CTL_EOL);
   
         sysctl_createv(&socket_sysctllog, 0, NULL, NULL,          sysctl_createv(&socket_sysctllog, 0, NULL, NULL,
                        CTLFLAG_PERMANENT|CTLFLAG_READWRITE,                         CTLFLAG_PERMANENT|CTLFLAG_READWRITE,

Legend:
Removed from v.1.211.2.4  
changed lines
  Added in v.1.212

CVSweb <webmaster@jp.NetBSD.org>