uipc_accf.c revision 77598
1129864Sstefanf/* 2129864Sstefanf * Copyright (c) 2000 Paycounter, Inc. 3129864Sstefanf * Author: Alfred Perlstein <alfred@paycounter.com>, <alfred@FreeBSD.org> 4129864Sstefanf * All rights reserved. 5129864Sstefanf * 6129864Sstefanf * Redistribution and use in source and binary forms, with or without 7129864Sstefanf * modification, are permitted provided that the following conditions 8129864Sstefanf * are met: 9129864Sstefanf * 1. Redistributions of source code must retain the above copyright 10129864Sstefanf * notice, this list of conditions and the following disclaimer. 11129864Sstefanf * 2. Redistributions in binary form must reproduce the above copyright 12129864Sstefanf * notice, this list of conditions and the following disclaimer in the 13129864Sstefanf * documentation and/or other materials provided with the distribution. 14129864Sstefanf * 15129864Sstefanf * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16129864Sstefanf * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17129864Sstefanf * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18129864Sstefanf * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19129864Sstefanf * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20129864Sstefanf * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21129864Sstefanf * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22129864Sstefanf * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23129864Sstefanf * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24129864Sstefanf * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25129864Sstefanf * SUCH DAMAGE. 26129864Sstefanf * 27129864Sstefanf * $FreeBSD: head/sys/kern/uipc_accf.c 77598 2001-06-01 21:47:34Z jesper $ 28129864Sstefanf */ 29129864Sstefanf 30189803Sdas#define ACCEPT_FILTER_MOD 31129864Sstefanf 32129864Sstefanf#include "opt_param.h" 33129864Sstefanf#include <sys/param.h> 34129864Sstefanf#include <sys/systm.h> 35189803Sdas#include <sys/domain.h> 36181374Sdas#include <sys/kernel.h> 37189803Sdas#include <sys/malloc.h> 38129864Sstefanf#include <sys/mbuf.h> 39#include <sys/protosw.h> 40#include <sys/sysctl.h> 41#include <sys/socket.h> 42#include <sys/socketvar.h> 43#include <sys/queue.h> 44 45static SLIST_HEAD(, accept_filter) accept_filtlsthd = 46 SLIST_HEAD_INITIALIZER(&accept_filtlsthd); 47 48MALLOC_DEFINE(M_ACCF, "accf", "accept filter data"); 49 50static int unloadable = 0; 51 52SYSCTL_DECL(_net_inet); /* XXX: some header should do this for me */ 53SYSCTL_NODE(_net_inet, OID_AUTO, accf, CTLFLAG_RW, 0, "Accept filters"); 54SYSCTL_INT(_net_inet_accf, OID_AUTO, unloadable, CTLFLAG_RW, &unloadable, 0, 55 "Allow unload of accept filters (not recommended)"); 56 57/* 58 * must be passed a malloc'd structure so we don't explode if the kld 59 * is unloaded, we leak the struct on deallocation to deal with this, 60 * but if a filter is loaded with the same name as a leaked one we re-use 61 * the entry. 62 */ 63int 64accept_filt_add(struct accept_filter *filt) 65{ 66 struct accept_filter *p; 67 68 SLIST_FOREACH(p, &accept_filtlsthd, accf_next) 69 if (strcmp(p->accf_name, filt->accf_name) == 0) { 70 if (p->accf_callback != NULL) { 71 return (EEXIST); 72 } else { 73 p->accf_callback = filt->accf_callback; 74 FREE(filt, M_ACCF); 75 return (0); 76 } 77 } 78 79 if (p == NULL) 80 SLIST_INSERT_HEAD(&accept_filtlsthd, filt, accf_next); 81 return (0); 82} 83 84int 85accept_filt_del(char *name) 86{ 87 struct accept_filter *p; 88 89 p = accept_filt_get(name); 90 if (p == NULL) 91 return (ENOENT); 92 93 p->accf_callback = NULL; 94 return (0); 95} 96 97struct accept_filter * 98accept_filt_get(char *name) 99{ 100 struct accept_filter *p; 101 102 SLIST_FOREACH(p, &accept_filtlsthd, accf_next) 103 if (strcmp(p->accf_name, name) == 0) 104 return (p); 105 106 return (NULL); 107} 108 109int 110accept_filt_generic_mod_event(module_t mod, int event, void *data) 111{ 112 struct accept_filter *p; 113 struct accept_filter *accfp = (struct accept_filter *) data; 114 int s, error; 115 116 switch (event) { 117 case MOD_LOAD: 118 MALLOC(p, struct accept_filter *, sizeof(*p), M_ACCF, M_WAITOK); 119 bcopy(accfp, p, sizeof(*p)); 120 s = splnet(); 121 error = accept_filt_add(p); 122 splx(s); 123 break; 124 125 case MOD_UNLOAD: 126 /* 127 * Do not support unloading yet. we don't keep track of refcounts 128 * and unloading an accept filter callback and then having it called 129 * is a bad thing. A simple fix would be to track the refcount 130 * in the struct accept_filter. 131 */ 132 if (unloadable != 0) { 133 s = splnet(); 134 error = accept_filt_del(accfp->accf_name); 135 splx(s); 136 } else 137 error = EOPNOTSUPP; 138 break; 139 140 case MOD_SHUTDOWN: 141 error = 0; 142 break; 143 144 default: 145 error = EOPNOTSUPP; 146 break; 147 } 148 149 return (error); 150} 151