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/uipc_accf.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 /*      $NetBSD: uipc_accf.c,v 1.6 2008/10/15 08:25:28 ad Exp $ */
    2 
    3 /*-
    4  * Copyright (c) 2008 The NetBSD Foundation, Inc.
    5  * All rights reserved.
    6  *
    7  * This code is derived from software developed for The NetBSD Foundation
    8  * by Andrew Doran.
    9  *
   10  * Redistribution and use in source and binary forms, with or without
   11  * modification, are permitted provided that the following conditions
   12  * are met:
   13  * 1. Redistributions of source code must retain the above copyright
   14  *    notice, this list of conditions and the following disclaimer.
   15  * 2. Redistributions in binary form must reproduce the above copyright
   16  *    notice, this list of conditions and the following disclaimer in the
   17  *    documentation and/or other materials provided with the distribution.
   18  *
   19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
   20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
   21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
   22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
   23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
   24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
   25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
   26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
   27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   29  * POSSIBILITY OF SUCH DAMAGE.
   30  */
   31 
   32 /*-
   33  * Copyright (c) 2000 Paycounter, Inc.
   34  * Copyright (c) 2005 Robert N. M. Watson
   35  * Author: Alfred Perlstein <alfred@paycounter.com>, <alfred@FreeBSD.org>
   36  * All rights reserved.
   37  *
   38  * Redistribution and use in source and binary forms, with or without
   39  * modification, are permitted provided that the following conditions
   40  * are met:
   41  * 1. Redistributions of source code must retain the above copyright
   42  *    notice, this list of conditions and the following disclaimer.
   43  * 2. Redistributions in binary form must reproduce the above copyright
   44  *    notice, this list of conditions and the following disclaimer in the
   45  *    documentation and/or other materials provided with the distribution.
   46  *
   47  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   48  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   49  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   50  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   51  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   52  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   53  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   54  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   55  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   56  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   57  * SUCH DAMAGE.
   58  */
   59 
   60 #include <sys/cdefs.h>
   61 __KERNEL_RCSID(0, "$NetBSD: uipc_accf.c,v 1.6 2008/10/15 08:25:28 ad Exp $");
   62 
   63 #define ACCEPT_FILTER_MOD
   64 
   65 #include <sys/param.h>
   66 #include <sys/systm.h>
   67 #include <sys/domain.h>
   68 #include <sys/kernel.h>
   69 #include <sys/lock.h>
   70 #include <sys/kmem.h>
   71 #include <sys/mbuf.h>
   72 #include <sys/lkm.h>
   73 #include <sys/rwlock.h>
   74 #include <sys/protosw.h>
   75 #include <sys/sysctl.h>
   76 #include <sys/socket.h>
   77 #include <sys/socketvar.h>
   78 #include <sys/queue.h>
   79 #include <sys/once.h>
   80 #include <sys/atomic.h>
   81 
   82 static krwlock_t accept_filter_lock;
   83 
   84 static LIST_HEAD(, accept_filter) accept_filtlsthd =
   85     LIST_HEAD_INITIALIZER(&accept_filtlsthd);
   86 
   87 /*
   88  * Names of Accept filter sysctl objects
   89  */
   90 SYSCTL_SETUP(sysctl_net_inet_accf_setup, "sysctl net.inet.accf subtree setup")
   91 {
   92 
   93         sysctl_createv(clog, 0, NULL, NULL,
   94                        CTLFLAG_PERMANENT,
   95                        CTLTYPE_NODE, "net", NULL,
   96                        NULL, 0, NULL, 0,
   97                        CTL_NET, CTL_EOL);
   98         sysctl_createv(clog, 0, NULL, NULL,
   99                        CTLFLAG_PERMANENT,
  100                        CTLTYPE_NODE, "inet", NULL,
  101                        NULL, 0, NULL, 0,
  102                        CTL_NET, PF_INET, CTL_EOL);
  103         sysctl_createv(clog, 0, NULL, NULL,
  104                        CTLFLAG_PERMANENT,
  105                        CTLTYPE_NODE, "accf",
  106                        SYSCTL_DESCR("Accept filters"),
  107                        NULL, 0, NULL, 0,
  108                        CTL_NET, PF_INET, SO_ACCEPTFILTER, CTL_EOL);
  109 }
  110 
  111 int
  112 accept_filt_add(struct accept_filter *filt)
  113 {
  114         struct accept_filter *p;
  115 
  116         rw_enter(&accept_filter_lock, RW_WRITER);
  117         LIST_FOREACH(p, &accept_filtlsthd, accf_next) {
  118                 if (strcmp(p->accf_name, filt->accf_name) == 0)  {
  119                         rw_exit(&accept_filter_lock);
  120                         return EEXIST;
  121                 }
  122         }                               
  123         LIST_INSERT_HEAD(&accept_filtlsthd, filt, accf_next);
  124         rw_exit(&accept_filter_lock);
  125 
  126         return 0;
  127 }
  128 
  129 int
  130 accept_filt_del(struct accept_filter *p)
  131 {
  132 
  133         rw_enter(&accept_filter_lock, RW_WRITER);
  134         if (p->accf_refcnt != 0) {
  135                 rw_exit(&accept_filter_lock);
  136                 return EBUSY;
  137         }
  138         LIST_REMOVE(p, accf_next);
  139         rw_exit(&accept_filter_lock);
  140 
  141         return 0;
  142 }
  143 
  144 struct accept_filter *
  145 accept_filt_get(char *name)
  146 {
  147         struct accept_filter *p;
  148 
  149         rw_enter(&accept_filter_lock, RW_READER);
  150         LIST_FOREACH(p, &accept_filtlsthd, accf_next) {
  151                 if (strcmp(p->accf_name, name) == 0) {
  152                         atomic_inc_uint(&p->accf_refcnt);
  153                         break;
  154                 }
  155         }
  156         rw_exit(&accept_filter_lock);
  157 
  158         return p;
  159 }
  160 
  161 /*
  162  * Accept filter initialization routine.
  163  * This should be called only once.
  164  */
  165 
  166 static int
  167 accept_filter_init0(void)
  168 {
  169 
  170         rw_init(&accept_filter_lock);
  171 
  172         return 0;
  173 }
  174 
  175 /*
  176  * Initialization routine: This can also be replaced with 
  177  * accept_filt_generic_mod_event for attaching new accept filter.
  178  */
  179 
  180 void
  181 accept_filter_init(void)
  182 {
  183         static ONCE_DECL(accept_filter_init_once);
  184 
  185         RUN_ONCE(&accept_filter_init_once, accept_filter_init0);
  186 }
  187 
  188 int
  189 accept_filt_generic_mod_event(struct lkm_table *lkmtp, int event, void *data)
  190 {
  191         struct accept_filter *accfp = (struct accept_filter *) data;
  192         int error;
  193 
  194         switch (event) {
  195         case LKM_E_LOAD:
  196                 accept_filter_init();
  197                 error = accept_filt_add(accfp);
  198                 break;
  199 
  200         case LKM_E_UNLOAD:
  201                 error = accept_filt_del(accfp);
  202                 break;
  203 
  204         case LKM_E_STAT:
  205                 error = 0;
  206                 break;
  207 
  208         default:
  209                 error = EOPNOTSUPP;
  210                 break;
  211         }
  212 
  213         return error;
  214 }
  215 
  216 int
  217 accept_filt_getopt(struct socket *so, struct sockopt *sopt)
  218 {
  219         struct accept_filter_arg afa;
  220         int error;
  221 
  222         KASSERT(solocked(so));
  223 
  224         if ((so->so_options & SO_ACCEPTCONN) == 0) {
  225                 error = EINVAL;
  226                 goto out;
  227         }
  228         if ((so->so_options & SO_ACCEPTFILTER) == 0) {
  229                 error = EINVAL;
  230                 goto out;
  231         }
  232 
  233         memset(&afa, 0, sizeof(afa));
  234         strcpy(afa.af_name, so->so_accf->so_accept_filter->accf_name);
  235         if (so->so_accf->so_accept_filter_str != NULL)
  236                 strcpy(afa.af_arg, so->so_accf->so_accept_filter_str);
  237         error = sockopt_set(sopt, &afa, sizeof(afa));
  238 out:
  239         return error;
  240 }
  241 
  242 /*
  243  * Simple delete case, with socket locked.
  244  */
  245 int
  246 accept_filt_clear(struct socket *so)
  247 {
  248         struct accept_filter_arg afa;
  249         struct accept_filter *afp;
  250         struct socket *so2, *next;
  251         struct so_accf *af;
  252 
  253         KASSERT(solocked(so));
  254 
  255         if ((so->so_options & SO_ACCEPTCONN) == 0) {
  256                 return EINVAL;
  257         }
  258         if (so->so_accf != NULL) {
  259                 /* Break in-flight processing. */
  260                 for (so2 = TAILQ_FIRST(&so->so_q0); so2 != NULL; so2 = next) {
  261                         next = TAILQ_NEXT(so2, so_qe);
  262                         if (so2->so_upcall == NULL) {
  263                                 continue;
  264                         }
  265                         so2->so_upcall = NULL;
  266                         so2->so_upcallarg = NULL;
  267                         so2->so_options &= ~SO_ACCEPTFILTER;
  268                         so2->so_rcv.sb_flags &= ~SB_UPCALL;
  269                         soisconnected(so2);
  270                 }
  271                 af = so->so_accf;
  272                 afp = af->so_accept_filter;
  273                 if (afp != NULL && afp->accf_destroy != NULL) {
  274                         (*afp->accf_destroy)(so);
  275                 }
  276                 if (af->so_accept_filter_str != NULL) {
  277                         kmem_free(af->so_accept_filter_str,
  278                             sizeof(afa.af_name));
  279                 }
  280                 kmem_free(af, sizeof(*af));
  281                 so->so_accf = NULL;
  282                 atomic_dec_uint(&afp->accf_refcnt);
  283         }
  284         so->so_options &= ~SO_ACCEPTFILTER;
  285         return 0;
  286 }
  287 
  288 /*
  289  * setsockopt() for accept filters.  Called with the socket unlocked,
  290  * will always return it locked.
  291  */
  292 int
  293 accept_filt_setopt(struct socket *so, const struct sockopt *sopt)
  294 {
  295         struct accept_filter_arg afa;
  296         struct accept_filter *afp;
  297         struct so_accf *newaf;
  298         int error;
  299 
  300         if (sopt == NULL || sopt->sopt_size == 0) {
  301                 solock(so);
  302                 return accept_filt_clear(so);
  303         }
  304 
  305         /*
  306          * Pre-allocate any memory we may need later to avoid blocking at
  307          * untimely moments.  This does not optimize for invalid arguments.
  308          */
  309         error = sockopt_get(sopt, &afa, sizeof(afa));
  310         if (error) {
  311                 solock(so);
  312                 return error;
  313         }
  314         afa.af_name[sizeof(afa.af_name)-1] = '\0';
  315         afa.af_arg[sizeof(afa.af_arg)-1] = '\0';
  316         afp = accept_filt_get(afa.af_name);
  317         if (afp == NULL) {
  318                 solock(so);
  319                 return ENOENT;
  320         }
  321         /*
  322          * Allocate the new accept filter instance storage.  We may
  323          * have to free it again later if we fail to attach it.  If
  324          * attached properly, 'newaf' is NULLed to avoid a free()
  325          * while in use.
  326          */
  327         newaf = kmem_zalloc(sizeof(*newaf), KM_SLEEP);
  328         if (afp->accf_create != NULL && afa.af_name[0] != '\0') {
  329                 /*
  330                  * FreeBSD did a variable-size allocation here
  331                  * with the actual string length from afa.af_name
  332                  * but it is so short, why bother tracking it?
  333                  * XXX as others have noted, this is an API mistake;
  334                  * XXX accept_filter_arg should have a mandatory namelen.
  335                  * XXX (but it's a bit too late to fix that now)
  336                  */
  337                 newaf->so_accept_filter_str =
  338                     kmem_alloc(sizeof(afa.af_name), KM_SLEEP);
  339                 strcpy(newaf->so_accept_filter_str, afa.af_name);
  340         }
  341 
  342         /*
  343          * Require a listen socket; don't try to replace an existing filter
  344          * without first removing it.
  345          */
  346         solock(so);
  347         if ((so->so_options & SO_ACCEPTCONN) == 0 || so->so_accf != NULL) {
  348                 error = EINVAL;
  349                 goto out;
  350         }
  351 
  352         /*
  353          * Invoke the accf_create() method of the filter if required.  The
  354          * socket lock is held over this call, so create methods for filters
  355          * shouldn't block.
  356          */
  357         if (afp->accf_create != NULL) {
  358                 newaf->so_accept_filter_arg =
  359                     (*afp->accf_create)(so, afa.af_arg);
  360                 if (newaf->so_accept_filter_arg == NULL) {
  361                         error = EINVAL;
  362                         goto out;
  363                 }
  364         }
  365         newaf->so_accept_filter = afp;
  366         so->so_accf = newaf;
  367         so->so_options |= SO_ACCEPTFILTER;
  368         newaf = NULL;
  369 out:
  370         if (newaf != NULL) {
  371                 if (newaf->so_accept_filter_str != NULL)
  372                         kmem_free(newaf->so_accept_filter_str,
  373                             sizeof(afa.af_name));
  374                 kmem_free(newaf, sizeof(*newaf));
  375                 atomic_dec_uint(&afp->accf_refcnt);
  376         }
  377         return error;
  378 }

Cache object: 92f0e5f66622cbc3e8c854f88e18e755


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