uipc_accf.c revision 77598
165534Salfred/* 265534Salfred * Copyright (c) 2000 Paycounter, Inc. 365534Salfred * Author: Alfred Perlstein <alfred@paycounter.com>, <alfred@FreeBSD.org> 461837Salfred * All rights reserved. 561837Salfred * 661837Salfred * Redistribution and use in source and binary forms, with or without 761837Salfred * modification, are permitted provided that the following conditions 861837Salfred * are met: 961837Salfred * 1. Redistributions of source code must retain the above copyright 1061837Salfred * notice, this list of conditions and the following disclaimer. 1161837Salfred * 2. Redistributions in binary form must reproduce the above copyright 1261837Salfred * notice, this list of conditions and the following disclaimer in the 1361837Salfred * documentation and/or other materials provided with the distribution. 1461837Salfred * 1561837Salfred * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1661837Salfred * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1761837Salfred * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1861837Salfred * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 1961837Salfred * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2061837Salfred * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2161837Salfred * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2261837Salfred * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2361837Salfred * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2461837Salfred * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2561837Salfred * SUCH DAMAGE. 2661837Salfred * 2761837Salfred * $FreeBSD: head/sys/kern/uipc_accf.c 77598 2001-06-01 21:47:34Z jesper $ 2861837Salfred */ 2961837Salfred 3061837Salfred#define ACCEPT_FILTER_MOD 3161837Salfred 3277598Sjesper#include "opt_param.h" 3361837Salfred#include <sys/param.h> 3461837Salfred#include <sys/systm.h> 3561837Salfred#include <sys/domain.h> 3661837Salfred#include <sys/kernel.h> 3761837Salfred#include <sys/malloc.h> 3861837Salfred#include <sys/mbuf.h> 3961837Salfred#include <sys/protosw.h> 4065534Salfred#include <sys/sysctl.h> 4161837Salfred#include <sys/socket.h> 4261837Salfred#include <sys/socketvar.h> 4361837Salfred#include <sys/queue.h> 4461837Salfred 4561837Salfredstatic SLIST_HEAD(, accept_filter) accept_filtlsthd = 4661837Salfred SLIST_HEAD_INITIALIZER(&accept_filtlsthd); 4761837Salfred 4861837SalfredMALLOC_DEFINE(M_ACCF, "accf", "accept filter data"); 4961837Salfred 5065534Salfredstatic int unloadable = 0; 5165534Salfred 5265534SalfredSYSCTL_DECL(_net_inet); /* XXX: some header should do this for me */ 5365534SalfredSYSCTL_NODE(_net_inet, OID_AUTO, accf, CTLFLAG_RW, 0, "Accept filters"); 5465534SalfredSYSCTL_INT(_net_inet_accf, OID_AUTO, unloadable, CTLFLAG_RW, &unloadable, 0, 5565534Salfred "Allow unload of accept filters (not recommended)"); 5665534Salfred 5761837Salfred/* 5861837Salfred * must be passed a malloc'd structure so we don't explode if the kld 5961837Salfred * is unloaded, we leak the struct on deallocation to deal with this, 6061837Salfred * but if a filter is loaded with the same name as a leaked one we re-use 6161837Salfred * the entry. 6261837Salfred */ 6361837Salfredint 6461837Salfredaccept_filt_add(struct accept_filter *filt) 6561837Salfred{ 6661837Salfred struct accept_filter *p; 6761837Salfred 6861837Salfred SLIST_FOREACH(p, &accept_filtlsthd, accf_next) 6961837Salfred if (strcmp(p->accf_name, filt->accf_name) == 0) { 7061837Salfred if (p->accf_callback != NULL) { 7161837Salfred return (EEXIST); 7261837Salfred } else { 7361837Salfred p->accf_callback = filt->accf_callback; 7461837Salfred FREE(filt, M_ACCF); 7561837Salfred return (0); 7661837Salfred } 7761837Salfred } 7861837Salfred 7961837Salfred if (p == NULL) 8061837Salfred SLIST_INSERT_HEAD(&accept_filtlsthd, filt, accf_next); 8161837Salfred return (0); 8261837Salfred} 8361837Salfred 8461837Salfredint 8561837Salfredaccept_filt_del(char *name) 8661837Salfred{ 8761837Salfred struct accept_filter *p; 8861837Salfred 8961837Salfred p = accept_filt_get(name); 9061837Salfred if (p == NULL) 9161837Salfred return (ENOENT); 9261837Salfred 9361837Salfred p->accf_callback = NULL; 9461837Salfred return (0); 9561837Salfred} 9661837Salfred 9761837Salfredstruct accept_filter * 9861837Salfredaccept_filt_get(char *name) 9961837Salfred{ 10061837Salfred struct accept_filter *p; 10161837Salfred 10261837Salfred SLIST_FOREACH(p, &accept_filtlsthd, accf_next) 10361837Salfred if (strcmp(p->accf_name, name) == 0) 10461837Salfred return (p); 10561837Salfred 10661837Salfred return (NULL); 10761837Salfred} 10861837Salfred 10961837Salfredint 11061837Salfredaccept_filt_generic_mod_event(module_t mod, int event, void *data) 11161837Salfred{ 11261837Salfred struct accept_filter *p; 11361837Salfred struct accept_filter *accfp = (struct accept_filter *) data; 11461837Salfred int s, error; 11561837Salfred 11661837Salfred switch (event) { 11761837Salfred case MOD_LOAD: 11861837Salfred MALLOC(p, struct accept_filter *, sizeof(*p), M_ACCF, M_WAITOK); 11961837Salfred bcopy(accfp, p, sizeof(*p)); 12061837Salfred s = splnet(); 12161837Salfred error = accept_filt_add(p); 12261837Salfred splx(s); 12361837Salfred break; 12461837Salfred 12561837Salfred case MOD_UNLOAD: 12663645Salfred /* 12763645Salfred * Do not support unloading yet. we don't keep track of refcounts 12863645Salfred * and unloading an accept filter callback and then having it called 12963645Salfred * is a bad thing. A simple fix would be to track the refcount 13063645Salfred * in the struct accept_filter. 13163645Salfred */ 13265534Salfred if (unloadable != 0) { 13365534Salfred s = splnet(); 13465534Salfred error = accept_filt_del(accfp->accf_name); 13565534Salfred splx(s); 13665534Salfred } else 13765534Salfred error = EOPNOTSUPP; 13861837Salfred break; 13961837Salfred 14061837Salfred case MOD_SHUTDOWN: 14161837Salfred error = 0; 14261837Salfred break; 14361837Salfred 14461837Salfred default: 14561837Salfred error = EOPNOTSUPP; 14661837Salfred break; 14761837Salfred } 14861837Salfred 14961837Salfred return (error); 15061837Salfred} 151