Home | History | Annotate | Line # | Download | only in kern
uipc_accf.c revision 1.14
      1 /*	$NetBSD: uipc_accf.c,v 1.14 2024/12/06 18:36:31 riastradh 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 (at) paycounter.com>, <alfred (at) 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.14 2024/12/06 18:36:31 riastradh Exp $");
     62 
     63 #define ACCEPT_FILTER_MOD
     64 
     65 #include <sys/param.h>
     66 #include <sys/types.h>
     67 
     68 #include <sys/atomic.h>
     69 #include <sys/domain.h>
     70 #include <sys/kernel.h>
     71 #include <sys/kmem.h>
     72 #include <sys/lock.h>
     73 #include <sys/mbuf.h>
     74 #include <sys/module.h>
     75 #include <sys/once.h>
     76 #include <sys/protosw.h>
     77 #include <sys/queue.h>
     78 #include <sys/rwlock.h>
     79 #include <sys/socket.h>
     80 #include <sys/socketvar.h>
     81 #include <sys/sysctl.h>
     82 #include <sys/systm.h>
     83 
     84 static krwlock_t accept_filter_lock;
     85 
     86 static LIST_HEAD(, accept_filter) accept_filtlsthd =
     87     LIST_HEAD_INITIALIZER(&accept_filtlsthd);
     88 
     89 /*
     90  * Names of Accept filter sysctl objects
     91  */
     92 static struct sysctllog *ctllog;
     93 static void
     94 sysctl_net_inet_accf_setup(void)
     95 {
     96 
     97 	sysctl_createv(&ctllog, 0, NULL, NULL,
     98 		       CTLFLAG_PERMANENT,
     99 		       CTLTYPE_NODE, "inet", NULL,
    100 		       NULL, 0, NULL, 0,
    101 		       CTL_NET, PF_INET, CTL_EOL);
    102 	sysctl_createv(&ctllog, 0, NULL, NULL,
    103 		       CTLFLAG_PERMANENT,
    104 		       CTLTYPE_NODE, "accf",
    105 		       SYSCTL_DESCR("Accept filters"),
    106 		       NULL, 0, NULL, 0,
    107 		       CTL_NET, PF_INET, SO_ACCEPTFILTER, CTL_EOL);
    108 }
    109 
    110 int
    111 accept_filt_add(struct accept_filter *filt)
    112 {
    113 	struct accept_filter *p;
    114 
    115 	accept_filter_init();
    116 
    117 	rw_enter(&accept_filter_lock, RW_WRITER);
    118 	LIST_FOREACH(p, &accept_filtlsthd, accf_next) {
    119 		if (strcmp(p->accf_name, filt->accf_name) == 0)  {
    120 			rw_exit(&accept_filter_lock);
    121 			return EEXIST;
    122 		}
    123 	}
    124 	LIST_INSERT_HEAD(&accept_filtlsthd, filt, accf_next);
    125 	rw_exit(&accept_filter_lock);
    126 
    127 	return 0;
    128 }
    129 
    130 int
    131 accept_filt_del(struct accept_filter *p)
    132 {
    133 
    134 	rw_enter(&accept_filter_lock, RW_WRITER);
    135 	if (p->accf_refcnt != 0) {
    136 		rw_exit(&accept_filter_lock);
    137 		return EBUSY;
    138 	}
    139 	LIST_REMOVE(p, accf_next);
    140 	rw_exit(&accept_filter_lock);
    141 
    142 	return 0;
    143 }
    144 
    145 struct accept_filter *
    146 accept_filt_get(char *name)
    147 {
    148 	struct accept_filter *p;
    149 	char buf[32];
    150 	u_int gen;
    151 
    152 	do {
    153 		rw_enter(&accept_filter_lock, RW_READER);
    154 		LIST_FOREACH(p, &accept_filtlsthd, accf_next) {
    155 			if (strcmp(p->accf_name, name) == 0) {
    156 				atomic_inc_uint(&p->accf_refcnt);
    157 				break;
    158 			}
    159 		}
    160 		rw_exit(&accept_filter_lock);
    161 		if (p != NULL) {
    162 			break;
    163 		}
    164 		/* Try to autoload a module to satisfy the request. */
    165 		strcpy(buf, "accf_");
    166 		strlcat(buf, name, sizeof(buf));
    167 		gen = module_gen;
    168 		(void)module_autoload(buf, MODULE_CLASS_ANY);
    169 	} while (gen != module_gen);
    170 
    171 	return p;
    172 }
    173 
    174 /*
    175  * Accept filter initialization routine.
    176  * This should be called only once.
    177  */
    178 
    179 static int
    180 accept_filter_init0(void)
    181 {
    182 
    183 	rw_init(&accept_filter_lock);
    184 	sysctl_net_inet_accf_setup();
    185 
    186 	return 0;
    187 }
    188 
    189 /*
    190  * Initialization routine: This can also be replaced with
    191  * accept_filt_generic_mod_event for attaching new accept filter.
    192  */
    193 
    194 void
    195 accept_filter_init(void)
    196 {
    197 	static ONCE_DECL(accept_filter_init_once);
    198 
    199 	RUN_ONCE(&accept_filter_init_once, accept_filter_init0);
    200 }
    201 
    202 int
    203 accept_filt_getopt(struct socket *so, struct sockopt *sopt)
    204 {
    205 	struct accept_filter_arg afa;
    206 	int error;
    207 
    208 	KASSERT(solocked(so));
    209 
    210 	if ((so->so_options & SO_ACCEPTCONN) == 0) {
    211 		error = EINVAL;
    212 		goto out;
    213 	}
    214 	if ((so->so_options & SO_ACCEPTFILTER) == 0) {
    215 		error = EINVAL;
    216 		goto out;
    217 	}
    218 
    219 	memset(&afa, 0, sizeof(afa));
    220 	strcpy(afa.af_name, so->so_accf->so_accept_filter->accf_name);
    221 	if (so->so_accf->so_accept_filter_str != NULL)
    222 		strcpy(afa.af_arg, so->so_accf->so_accept_filter_str);
    223 	error = sockopt_set(sopt, &afa, sizeof(afa));
    224 out:
    225 	return error;
    226 }
    227 
    228 /*
    229  * Simple delete case, with socket locked.
    230  */
    231 int
    232 accept_filt_clear(struct socket *so)
    233 {
    234 	struct accept_filter_arg afa;
    235 	struct accept_filter *afp;
    236 	struct socket *so2, *next;
    237 	struct so_accf *af;
    238 
    239 	KASSERT(solocked(so));
    240 
    241 	if ((so->so_options & SO_ACCEPTCONN) == 0) {
    242 		return EINVAL;
    243 	}
    244 	if (so->so_accf != NULL) {
    245 		/* Break in-flight processing. */
    246 		for (so2 = TAILQ_FIRST(&so->so_q0); so2 != NULL; so2 = next) {
    247 			next = TAILQ_NEXT(so2, so_qe);
    248 			if (so2->so_upcall == NULL) {
    249 				continue;
    250 			}
    251 			so2->so_upcall = NULL;
    252 			so2->so_upcallarg = NULL;
    253 			so2->so_options &= ~SO_ACCEPTFILTER;
    254 			so2->so_rcv.sb_flags &= ~SB_UPCALL;
    255 			soisconnected(so2);
    256 		}
    257 		af = so->so_accf;
    258 		afp = af->so_accept_filter;
    259 		if (afp != NULL && afp->accf_destroy != NULL) {
    260 			(*afp->accf_destroy)(so);
    261 		}
    262 		if (af->so_accept_filter_str != NULL) {
    263 			kmem_free(af->so_accept_filter_str,
    264 			    sizeof(afa.af_name));
    265 		}
    266 		kmem_free(af, sizeof(*af));
    267 		so->so_accf = NULL;
    268 		atomic_dec_uint(&afp->accf_refcnt);
    269 	}
    270 	so->so_options &= ~SO_ACCEPTFILTER;
    271 	return 0;
    272 }
    273 
    274 /*
    275  * setsockopt() for accept filters.  Called with the socket unlocked,
    276  * will always return it locked.
    277  */
    278 int
    279 accept_filt_setopt(struct socket *so, const struct sockopt *sopt)
    280 {
    281 	struct accept_filter_arg afa;
    282 	struct accept_filter *afp;
    283 	struct so_accf *newaf;
    284 	int error;
    285 
    286 	accept_filter_init();
    287 
    288 	if (sopt == NULL || sopt->sopt_size == 0) {
    289 		solock(so);
    290 		return accept_filt_clear(so);
    291 	}
    292 
    293 	/*
    294 	 * Pre-allocate any memory we may need later to avoid blocking at
    295 	 * untimely moments.  This does not optimize for invalid arguments.
    296 	 */
    297 	error = sockopt_get(sopt, &afa, sizeof(afa));
    298 	if (error) {
    299 		solock(so);
    300 		return error;
    301 	}
    302 	afa.af_name[sizeof(afa.af_name)-1] = '\0';
    303 	afa.af_arg[sizeof(afa.af_arg)-1] = '\0';
    304 	afp = accept_filt_get(afa.af_name);
    305 	if (afp == NULL) {
    306 		solock(so);
    307 		return ENOENT;
    308 	}
    309 	/*
    310 	 * Allocate the new accept filter instance storage.  We may
    311 	 * have to free it again later if we fail to attach it.  If
    312 	 * attached properly, 'newaf' is NULLed to avoid a free()
    313 	 * while in use.
    314 	 */
    315 	newaf = kmem_zalloc(sizeof(*newaf), KM_SLEEP);
    316 	if (afp->accf_create != NULL && afa.af_name[0] != '\0') {
    317 		/*
    318 		 * FreeBSD did a variable-size allocation here
    319 		 * with the actual string length from afa.af_name
    320 		 * but it is so short, why bother tracking it?
    321 		 * XXX as others have noted, this is an API mistake;
    322 		 * XXX accept_filter_arg should have a mandatory namelen.
    323 		 * XXX (but it's a bit too late to fix that now)
    324 		 */
    325 		newaf->so_accept_filter_str =
    326 		    kmem_alloc(sizeof(afa.af_name), KM_SLEEP);
    327 		strcpy(newaf->so_accept_filter_str, afa.af_name);
    328 	}
    329 
    330 	/*
    331 	 * Require a listen socket; don't try to replace an existing filter
    332 	 * without first removing it.
    333 	 */
    334 	solock(so);
    335 	if ((so->so_options & SO_ACCEPTCONN) == 0 || so->so_accf != NULL) {
    336 		error = EINVAL;
    337 		goto out;
    338 	}
    339 
    340 	/*
    341 	 * Invoke the accf_create() method of the filter if required.  The
    342 	 * socket lock is held over this call, so create methods for filters
    343 	 * shouldn't block.
    344 	 */
    345 	if (afp->accf_create != NULL) {
    346 		newaf->so_accept_filter_arg =
    347 		    (*afp->accf_create)(so, afa.af_arg);
    348 		if (newaf->so_accept_filter_arg == NULL) {
    349 			error = EINVAL;
    350 			goto out;
    351 		}
    352 	}
    353 	newaf->so_accept_filter = afp;
    354 	so->so_accf = newaf;
    355 	so->so_options |= SO_ACCEPTFILTER;
    356 	newaf = NULL;
    357 out:
    358 	if (newaf != NULL) {
    359 		if (newaf->so_accept_filter_str != NULL)
    360 			kmem_free(newaf->so_accept_filter_str,
    361 			    sizeof(afa.af_name));
    362 		kmem_free(newaf, sizeof(*newaf));
    363 		atomic_dec_uint(&afp->accf_refcnt);
    364 	}
    365 	return error;
    366 }
    367