[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.160.2.2 and 1.173

version 1.160.2.2, 2009/05/04 08:13:49 version 1.173, 2008/10/10 19:49:49
Line 1 
Line 1 
 /*      $NetBSD$        */  /*      $NetBSD$        */
   
 /*-  /*-
  * Copyright (c) 2002, 2007, 2008, 2009 The NetBSD Foundation, Inc.   * Copyright (c) 2002, 2007, 2008 The NetBSD Foundation, Inc.
  * All rights reserved.   * All rights reserved.
  *   *
  * This code is derived from software contributed to The NetBSD Foundation   * This code is derived from software contributed to The NetBSD Foundation
  * by Jason R. Thorpe of Wasabi Systems, Inc, and by Andrew Doran.   * by Jason R. Thorpe of Wasabi Systems, Inc.
  *   *
  * Redistribution and use in source and binary forms, with or without   * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions   * modification, are permitted provided that the following conditions
Line 65 
Line 65 
 #include <sys/cdefs.h>  #include <sys/cdefs.h>
 __KERNEL_RCSID(0, "$NetBSD$");  __KERNEL_RCSID(0, "$NetBSD$");
   
 #include "opt_compat_netbsd.h"  #include "opt_inet.h"
 #include "opt_sock_counters.h"  #include "opt_sock_counters.h"
 #include "opt_sosend_loan.h"  #include "opt_sosend_loan.h"
 #include "opt_mbuftrace.h"  #include "opt_mbuftrace.h"
Line 86  __KERNEL_RCSID(0, "$NetBSD$");
Line 86  __KERNEL_RCSID(0, "$NetBSD$");
 #include <sys/socketvar.h>  #include <sys/socketvar.h>
 #include <sys/signalvar.h>  #include <sys/signalvar.h>
 #include <sys/resourcevar.h>  #include <sys/resourcevar.h>
 #include <sys/uidinfo.h>  
 #include <sys/event.h>  #include <sys/event.h>
 #include <sys/poll.h>  #include <sys/poll.h>
 #include <sys/kauth.h>  #include <sys/kauth.h>
 #include <sys/mutex.h>  #include <sys/mutex.h>
 #include <sys/condvar.h>  #include <sys/condvar.h>
   
 #ifdef COMPAT_50  
 #include <compat/sys/time.h>  
 #include <compat/sys/socket.h>  
 #endif  
   
 #include <uvm/uvm.h>  #include <uvm/uvm.h>
   
 MALLOC_DEFINE(M_SOOPTS, "soopts", "socket options");  MALLOC_DEFINE(M_SOOPTS, "soopts", "socket options");
Line 156  static kcondvar_t socurkva_cv;
Line 150  static kcondvar_t socurkva_cv;
 static size_t sodopendfree(void);  static size_t sodopendfree(void);
 static size_t sodopendfreel(void);  static size_t sodopendfreel(void);
   
 static void sysctl_kern_somaxkva_setup(void);  
 static struct sysctllog *socket_sysctllog;  
   
 static vsize_t  static vsize_t
 sokvareserve(struct socket *so, vsize_t len)  sokvareserve(struct socket *so, vsize_t len)
 {  {
Line 432  void
Line 423  void
 soinit(void)  soinit(void)
 {  {
   
         sysctl_kern_somaxkva_setup();  
   
         mutex_init(&so_pendfree_lock, MUTEX_DEFAULT, IPL_VM);          mutex_init(&so_pendfree_lock, MUTEX_DEFAULT, IPL_VM);
         softnet_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);          softnet_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_NONE);
         cv_init(&socurkva_cv, "sokva");          cv_init(&socurkva_cv, "sokva");
Line 625  sofree(struct socket *so)
Line 614  sofree(struct socket *so)
         KASSERT(!cv_has_waiters(&so->so_snd.sb_cv));          KASSERT(!cv_has_waiters(&so->so_snd.sb_cv));
         sorflush(so);          sorflush(so);
         refs = so->so_aborting; /* XXX */          refs = so->so_aborting; /* XXX */
         /* Remove acccept filter if one is present. */  #ifdef INET
           /* remove acccept filter if one is present. */
         if (so->so_accf != NULL)          if (so->so_accf != NULL)
                 (void)accept_filt_clear(so);                  do_setopt_accept_filter(so, NULL);
   #endif
         sounlock(so);          sounlock(so);
         if (refs == 0)          /* XXX */          if (refs == 0)          /* XXX */
                 soput(so);                  soput(so);
Line 680  soclose(struct socket *so)
Line 671  soclose(struct socket *so)
                         if ((so->so_state & SS_ISDISCONNECTING) && so->so_nbio)                          if ((so->so_state & SS_ISDISCONNECTING) && so->so_nbio)
                                 goto drop;                                  goto drop;
                         while (so->so_state & SS_ISCONNECTED) {                          while (so->so_state & SS_ISCONNECTED) {
                                 error = sowait(so, true, so->so_linger * hz);                                  error = sowait(so, so->so_linger * hz);
                                 if (error)                                  if (error)
                                         break;                                          break;
                         }                          }
Line 1547  soshutdown(struct socket *so, int how)
Line 1538  soshutdown(struct socket *so, int how)
         return error;          return error;
 }  }
   
 int  
 sodrain(struct socket *so)  
 {  
         int error;  
   
         solock(so);  
         so->so_state |= SS_ISDRAINING;  
         cv_broadcast(&so->so_cv);  
         error = soshutdown(so, SHUT_RDWR);  
         sounlock(so);  
   
         return error;  
 }  
   
 void  void
 sorflush(struct socket *so)  sorflush(struct socket *so)
 {  {
Line 1596  sorflush(struct socket *so)
Line 1573  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, optval, opt;          int error, optval;
         struct linger l;          struct linger l;
         struct timeval tv;          struct timeval tv;
   
         switch ((opt = sopt->sopt_name)) {          switch (sopt->sopt_name) {
   
   #ifdef INET
         case SO_ACCEPTFILTER:          case SO_ACCEPTFILTER:
                 error = accept_filt_setopt(so, sopt);                  error = do_setopt_accept_filter(so, sopt);
                 KASSERT(solocked(so));                  if (error)
                           return error;
                 break;                  break;
   #endif
   
         case SO_LINGER:          case SO_LINGER:
                 error = sockopt_get(sopt, &l, sizeof(l));                  error = sockopt_get(sopt, &l, sizeof(l));
                 solock(so);  
                 if (error)                  if (error)
                         break;                          return (error);
   
                 if (l.l_linger < 0 || l.l_linger > USHRT_MAX ||                  if (l.l_linger < 0 || l.l_linger > USHRT_MAX ||
                     l.l_linger > (INT_MAX / hz)) {                      l.l_linger > (INT_MAX / hz))
                         error = EDOM;                          return EDOM;
                         break;  
                 }  
                 so->so_linger = l.l_linger;                  so->so_linger = l.l_linger;
                 if (l.l_onoff)                  if (l.l_onoff)
                         so->so_options |= SO_LINGER;                          so->so_options |= SO_LINGER;
                 else                  else
                         so->so_options &= ~SO_LINGER;                          so->so_options &= ~SO_LINGER;
                 break;  
                   break;
   
         case SO_DEBUG:          case SO_DEBUG:
         case SO_KEEPALIVE:          case SO_KEEPALIVE:
Line 1633  sosetopt1(struct socket *so, const struc
Line 1612  sosetopt1(struct socket *so, const struc
         case SO_REUSEPORT:          case SO_REUSEPORT:
         case SO_OOBINLINE:          case SO_OOBINLINE:
         case SO_TIMESTAMP:          case SO_TIMESTAMP:
 #ifdef SO_OTIMESTAMP  
         case SO_OTIMESTAMP:  
 #endif  
                 error = sockopt_getint(sopt, &optval);                  error = sockopt_getint(sopt, &optval);
                 solock(so);  
                 if (error)                  if (error)
                         break;                          return (error);
   
                 if (optval)                  if (optval)
                         so->so_options |= opt;                          so->so_options |= sopt->sopt_name;
                 else                  else
                         so->so_options &= ~opt;                          so->so_options &= ~sopt->sopt_name;
                 break;                  break;
   
         case SO_SNDBUF:          case SO_SNDBUF:
Line 1651  sosetopt1(struct socket *so, const struc
Line 1627  sosetopt1(struct socket *so, const struc
         case SO_SNDLOWAT:          case SO_SNDLOWAT:
         case SO_RCVLOWAT:          case SO_RCVLOWAT:
                 error = sockopt_getint(sopt, &optval);                  error = sockopt_getint(sopt, &optval);
                 solock(so);  
                 if (error)                  if (error)
                         break;                          return (error);
   
                 /*                  /*
                  * Values < 1 make no sense for any of these                   * Values < 1 make no sense for any of these
                  * options, so disallow them.                   * options, so disallow them.
                  */                   */
                 if (optval < 1) {                  if (optval < 1)
                         error = EINVAL;                          return EINVAL;
                         break;  
                 }  
   
                 switch (opt) {                  switch (sopt->sopt_name) {
                 case SO_SNDBUF:                  case SO_SNDBUF:
                         if (sbreserve(&so->so_snd, (u_long)optval, so) == 0) {                          if (sbreserve(&so->so_snd, (u_long)optval, so) == 0)
                                 error = ENOBUFS;                                  return ENOBUFS;
                                 break;  
                         }  
                         so->so_snd.sb_flags &= ~SB_AUTOSIZE;                          so->so_snd.sb_flags &= ~SB_AUTOSIZE;
                         break;                          break;
   
                 case SO_RCVBUF:                  case SO_RCVBUF:
                         if (sbreserve(&so->so_rcv, (u_long)optval, so) == 0) {                          if (sbreserve(&so->so_rcv, (u_long)optval, so) == 0)
                                 error = ENOBUFS;                                  return ENOBUFS;
                                 break;  
                         }  
                         so->so_rcv.sb_flags &= ~SB_AUTOSIZE;                          so->so_rcv.sb_flags &= ~SB_AUTOSIZE;
                         break;                          break;
   
Line 1701  sosetopt1(struct socket *so, const struc
Line 1672  sosetopt1(struct socket *so, const struc
                 }                  }
                 break;                  break;
   
 #ifdef COMPAT_50  
         case SO_OSNDTIMEO:  
         case SO_ORCVTIMEO: {  
                 struct timeval50 otv;  
                 error = sockopt_get(sopt, &otv, sizeof(otv));  
                 if (error) {  
                         solock(so);  
                         break;  
                 }  
                 timeval50_to_timeval(&otv, &tv);  
                 opt = opt == SO_OSNDTIMEO ? SO_SNDTIMEO : SO_RCVTIMEO;  
                 error = 0;  
                 /*FALLTHROUGH*/  
         }  
 #endif /* COMPAT_50 */  
   
         case SO_SNDTIMEO:          case SO_SNDTIMEO:
         case SO_RCVTIMEO:          case SO_RCVTIMEO:
                   error = sockopt_get(sopt, &tv, sizeof(tv));
                 if (error)                  if (error)
                         error = sockopt_get(sopt, &tv, sizeof(tv));                          return (error);
                 solock(so);  
                 if (error)  
                         break;  
   
                 if (tv.tv_sec > (INT_MAX - tv.tv_usec / tick) / hz) {                  if (tv.tv_sec > (INT_MAX - tv.tv_usec / tick) / hz)
                         error = EDOM;                          return EDOM;
                         break;  
                 }  
   
                 optval = tv.tv_sec * hz + tv.tv_usec / tick;                  optval = tv.tv_sec * hz + tv.tv_usec / tick;
                 if (optval == 0 && tv.tv_usec != 0)                  if (optval == 0 && tv.tv_usec != 0)
                         optval = 1;                          optval = 1;
   
                 switch (opt) {                  switch (sopt->sopt_name) {
                 case SO_SNDTIMEO:                  case SO_SNDTIMEO:
                         so->so_snd.sb_timeo = optval;                          so->so_snd.sb_timeo = optval;
                         break;                          break;
Line 1745  sosetopt1(struct socket *so, const struc
Line 1696  sosetopt1(struct socket *so, const struc
                 break;                  break;
   
         default:          default:
                 solock(so);                  return ENOPROTOOPT;
                 error = ENOPROTOOPT;  
                 break;  
         }          }
         KASSERT(solocked(so));          return 0;
         return error;  
 }  }
   
 int  int
Line 1758  sosetopt(struct socket *so, struct socko
Line 1706  sosetopt(struct socket *so, struct socko
 {  {
         int error, prerr;          int error, prerr;
   
         if (sopt->sopt_level == SOL_SOCKET) {          solock(so);
           if (sopt->sopt_level == SOL_SOCKET)
                 error = sosetopt1(so, sopt);                  error = sosetopt1(so, sopt);
                 KASSERT(solocked(so));          else
         } else {  
                 error = ENOPROTOOPT;                  error = ENOPROTOOPT;
                 solock(so);  
         }  
   
         if ((error == 0 || error == ENOPROTOOPT) &&          if ((error == 0 || error == ENOPROTOOPT) &&
             so->so_proto != NULL && so->so_proto->pr_ctloutput != NULL) {              so->so_proto != NULL && so->so_proto->pr_ctloutput != NULL) {
Line 1807  so_setsockopt(struct lwp *l, struct sock
Line 1753  so_setsockopt(struct lwp *l, struct sock
 static int  static int
 sogetopt1(struct socket *so, struct sockopt *sopt)  sogetopt1(struct socket *so, struct sockopt *sopt)
 {  {
         int error, optval, opt;          int error, optval;
         struct linger l;          struct linger l;
         struct timeval tv;          struct timeval tv;
   
         switch ((opt = sopt->sopt_name)) {          switch (sopt->sopt_name) {
   
   #ifdef INET
         case SO_ACCEPTFILTER:          case SO_ACCEPTFILTER:
                 error = accept_filt_getopt(so, sopt);                  error = do_getopt_accept_filter(so, sopt);
                 break;                  break;
   #endif
   
         case SO_LINGER:          case SO_LINGER:
                 l.l_onoff = (so->so_options & SO_LINGER) ? 1 : 0;                  l.l_onoff = (so->so_options & SO_LINGER) ? 1 : 0;
Line 1833  sogetopt1(struct socket *so, struct sock
Line 1781  sogetopt1(struct socket *so, struct sock
         case SO_BROADCAST:          case SO_BROADCAST:
         case SO_OOBINLINE:          case SO_OOBINLINE:
         case SO_TIMESTAMP:          case SO_TIMESTAMP:
 #ifdef SO_OTIMESTAMP                  error = sockopt_setint(sopt,
         case SO_OTIMESTAMP:                      (so->so_options & sopt->sopt_name) ? 1 : 0);
 #endif  
                 error = sockopt_setint(sopt, (so->so_options & opt) ? 1 : 0);  
                 break;                  break;
   
         case SO_TYPE:          case SO_TYPE:
Line 1864  sogetopt1(struct socket *so, struct sock
Line 1810  sogetopt1(struct socket *so, struct sock
                 error = sockopt_setint(sopt, so->so_rcv.sb_lowat);                  error = sockopt_setint(sopt, so->so_rcv.sb_lowat);
                 break;                  break;
   
 #ifdef COMPAT_50  
         case SO_OSNDTIMEO:  
         case SO_ORCVTIMEO: {  
                 struct timeval50 otv;  
   
                 optval = (opt == SO_OSNDTIMEO ?  
                      so->so_snd.sb_timeo : so->so_rcv.sb_timeo);  
   
                 otv.tv_sec = optval / hz;  
                 otv.tv_usec = (optval % hz) * tick;  
   
                 error = sockopt_set(sopt, &otv, sizeof(otv));  
                 break;  
         }  
 #endif /* COMPAT_50 */  
   
         case SO_SNDTIMEO:          case SO_SNDTIMEO:
         case SO_RCVTIMEO:          case SO_RCVTIMEO:
                 optval = (opt == SO_SNDTIMEO ?                  optval = (sopt->sopt_name == SO_SNDTIMEO ?
                      so->so_snd.sb_timeo : so->so_rcv.sb_timeo);                       so->so_snd.sb_timeo : so->so_rcv.sb_timeo);
   
                 tv.tv_sec = optval / hz;                  tv.tv_sec = optval / hz;
Line 1926  sogetopt(struct socket *so, struct socko
Line 1856  sogetopt(struct socket *so, struct socko
  * alloc sockopt data buffer buffer   * alloc sockopt data buffer buffer
  *      - will be released at destroy   *      - will be released at destroy
  */   */
 static int  static void
 sockopt_alloc(struct sockopt *sopt, size_t len, km_flag_t kmflag)  sockopt_alloc(struct sockopt *sopt, size_t len)
 {  {
   
         KASSERT(sopt->sopt_size == 0);          KASSERT(sopt->sopt_size == 0);
   
         if (len > sizeof(sopt->sopt_buf)) {          if (len > sizeof(sopt->sopt_buf))
                 sopt->sopt_data = kmem_zalloc(len, kmflag);                  sopt->sopt_data = kmem_zalloc(len, KM_SLEEP);
                 if (sopt->sopt_data == NULL)          else
                         return ENOMEM;  
         } else  
                 sopt->sopt_data = sopt->sopt_buf;                  sopt->sopt_data = sopt->sopt_buf;
   
         sopt->sopt_size = len;          sopt->sopt_size = len;
         return 0;  
 }  }
   
 /*  /*
  * initialise sockopt storage   * initialise sockopt storage
  *      - MAY sleep during allocation  
  */   */
 void  void
 sockopt_init(struct sockopt *sopt, int level, int name, size_t size)  sockopt_init(struct sockopt *sopt, int level, int name, size_t size)
Line 1955  sockopt_init(struct sockopt *sopt, int l
Line 1881  sockopt_init(struct sockopt *sopt, int l
   
         sopt->sopt_level = level;          sopt->sopt_level = level;
         sopt->sopt_name = name;          sopt->sopt_name = name;
         (void)sockopt_alloc(sopt, size, KM_SLEEP);          sockopt_alloc(sopt, size);
 }  }
   
 /*  /*
Line 1975  sockopt_destroy(struct sockopt *sopt)
Line 1901  sockopt_destroy(struct sockopt *sopt)
 /*  /*
  * set sockopt value   * set sockopt value
  *      - value is copied into sockopt   *      - value is copied into sockopt
  *      - memory is allocated when necessary, will not sleep   *      - memory is allocated when necessary
  */   */
 int  int
 sockopt_set(struct sockopt *sopt, const void *buf, size_t len)  sockopt_set(struct sockopt *sopt, const void *buf, size_t len)
 {  {
         int error;  
   
         if (sopt->sopt_size == 0) {          if (sopt->sopt_size == 0)
                 error = sockopt_alloc(sopt, len, KM_NOSLEEP);                  sockopt_alloc(sopt, len);
                 if (error)  
                         return error;  
         }  
   
         KASSERT(sopt->sopt_size == len);          KASSERT(sopt->sopt_size == len);
         memcpy(sopt->sopt_data, buf, len);          memcpy(sopt->sopt_data, buf, len);
Line 2032  sockopt_getint(const struct sockopt *sop
Line 1954  sockopt_getint(const struct sockopt *sop
  * set sockopt value from mbuf   * set sockopt value from mbuf
  *      - ONLY for legacy code   *      - ONLY for legacy code
  *      - mbuf is released by sockopt   *      - mbuf is released by sockopt
  *      - will not sleep  
  */   */
 int  int
 sockopt_setmbuf(struct sockopt *sopt, struct mbuf *m)  sockopt_setmbuf(struct sockopt *sopt, struct mbuf *m)
 {  {
         size_t len;          size_t len;
         int error;  
   
         len = m_length(m);          len = m_length(m);
   
         if (sopt->sopt_size == 0) {          if (sopt->sopt_size == 0)
                 error = sockopt_alloc(sopt, len, KM_NOSLEEP);                  sockopt_alloc(sopt, len);
                 if (error)  
                         return error;  
         }  
   
         KASSERT(sopt->sopt_size == len);          KASSERT(sopt->sopt_size == len);
         m_copydata(m, 0, len, sopt->sopt_data);          m_copydata(m, 0, len, sopt->sopt_data);
Line 2059  sockopt_setmbuf(struct sockopt *sopt, st
Line 1976  sockopt_setmbuf(struct sockopt *sopt, st
  * get sockopt value into mbuf   * get sockopt value into mbuf
  *      - ONLY for legacy code   *      - ONLY for legacy code
  *      - mbuf to be released by the caller   *      - mbuf to be released by the caller
  *      - will not sleep  
  */   */
 struct mbuf *  struct mbuf *
 sockopt_getmbuf(const struct sockopt *sopt)  sockopt_getmbuf(const struct sockopt *sopt)
 {  {
         struct mbuf *m;          struct mbuf *m;
   
         if (sopt->sopt_size > MCLBYTES)          m = m_get(M_WAIT, MT_SOOPTS);
                 return NULL;  
   
         m = m_get(M_DONTWAIT, MT_SOOPTS);  
         if (m == NULL)          if (m == NULL)
                 return NULL;                  return NULL;
   
         if (sopt->sopt_size > MLEN) {          m->m_len = MLEN;
                 MCLGET(m, M_DONTWAIT);          m_copyback(m, 0, sopt->sopt_size, sopt->sopt_data);
                 if ((m->m_flags & M_EXT) == 0) {          if (m_length(m) != max(sopt->sopt_size, MLEN)) {
                         m_free(m);                  m_freem(m);
                         return NULL;                  return NULL;
                 }  
         }          }
           m->m_len = min(sopt->sopt_size, MLEN);
         memcpy(mtod(m, void *), sopt->sopt_data, sopt->sopt_size);  
         m->m_len = sopt->sopt_size;  
   
         return m;          return m;
 }  }
Line 2092  sohasoutofband(struct socket *so)
Line 2002  sohasoutofband(struct socket *so)
 {  {
   
         fownsignal(so->so_pgid, SIGURG, POLL_PRI, POLLPRI|POLLRDBAND, so);          fownsignal(so->so_pgid, SIGURG, POLL_PRI, POLLPRI|POLLRDBAND, so);
         selnotify(&so->so_rcv.sb_sel, POLLPRI | POLLRDBAND, NOTE_SUBMIT);          selnotify(&so->so_rcv.sb_sel, POLLPRI | POLLRDBAND, 0);
 }  }
   
 static void  static void
Line 2323  sysctl_kern_somaxkva(SYSCTLFN_ARGS)
Line 2233  sysctl_kern_somaxkva(SYSCTLFN_ARGS)
         return (error);          return (error);
 }  }
   
 static void  SYSCTL_SETUP(sysctl_kern_somaxkva_setup, "sysctl kern.somaxkva setup")
 sysctl_kern_somaxkva_setup(void)  
 {  {
   
         KASSERT(socket_sysctllog == NULL);          sysctl_createv(clog, 0, NULL, NULL,
         sysctl_createv(&socket_sysctllog, 0, NULL, NULL,  
                        CTLFLAG_PERMANENT,                         CTLFLAG_PERMANENT,
                        CTLTYPE_NODE, "kern", NULL,                         CTLTYPE_NODE, "kern", NULL,
                        NULL, 0, NULL, 0,                         NULL, 0, NULL, 0,
                        CTL_KERN, CTL_EOL);                         CTL_KERN, CTL_EOL);
   
         sysctl_createv(&socket_sysctllog, 0, NULL, NULL,          sysctl_createv(clog, 0, NULL, NULL,
                        CTLFLAG_PERMANENT|CTLFLAG_READWRITE,                         CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
                        CTLTYPE_INT, "somaxkva",                         CTLTYPE_INT, "somaxkva",
                        SYSCTL_DESCR("Maximum amount of kernel memory to be "                         SYSCTL_DESCR("Maximum amount of kernel memory to be "

Legend:
Removed from v.1.160.2.2  
changed lines
  Added in v.1.173

CVSweb <webmaster@jp.NetBSD.org>