1 /* 2 * Copyright (c) 2000 Paycounter, Inc. 3 * Author: Alfred Perlstein <alfred@paycounter.com>, <alfred@FreeBSD.org> 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 * 27 * $FreeBSD: src/sys/kern/uipc_accf.c,v 1.2.2.2 2000/09/20 21:19:21 ps Exp $ 28 * $DragonFly: src/sys/kern/uipc_accf.c,v 1.3 2005/06/06 15:02:28 dillon Exp $ 29 */ 30 31 #define ACCEPT_FILTER_MOD 32 33 #include "opt_param.h" 34 #include <sys/param.h> 35 #include <sys/systm.h> 36 #include <sys/domain.h> 37 #include <sys/kernel.h> 38 #include <sys/proc.h> 39 #include <sys/malloc.h> 40 #include <sys/mbuf.h> 41 #include <sys/protosw.h> 42 #include <sys/sysctl.h> 43 #include <sys/socket.h> 44 #include <sys/socketvar.h> 45 #include <sys/queue.h> 46 #include <sys/thread2.h> 47 48 static SLIST_HEAD(, accept_filter) accept_filtlsthd = 49 SLIST_HEAD_INITIALIZER(&accept_filtlsthd); 50 51 MALLOC_DEFINE(M_ACCF, "accf", "accept filter data"); 52 53 static int unloadable = 0; 54 55 SYSCTL_DECL(_net_inet); /* XXX: some header should do this for me */ 56 SYSCTL_NODE(_net_inet, OID_AUTO, accf, CTLFLAG_RW, 0, "Accept filters"); 57 SYSCTL_INT(_net_inet_accf, OID_AUTO, unloadable, CTLFLAG_RW, &unloadable, 0, 58 "Allow unload of accept filters (not recommended)"); 59 60 /* 61 * must be passed a malloc'd structure so we don't explode if the kld 62 * is unloaded, we leak the struct on deallocation to deal with this, 63 * but if a filter is loaded with the same name as a leaked one we re-use 64 * the entry. 65 */ 66 int 67 accept_filt_add(struct accept_filter *filt) 68 { 69 struct accept_filter *p; 70 71 SLIST_FOREACH(p, &accept_filtlsthd, accf_next) 72 if (strcmp(p->accf_name, filt->accf_name) == 0) { 73 if (p->accf_callback != NULL) { 74 return (EEXIST); 75 } else { 76 p->accf_callback = filt->accf_callback; 77 FREE(filt, M_ACCF); 78 return (0); 79 } 80 } 81 82 if (p == NULL) 83 SLIST_INSERT_HEAD(&accept_filtlsthd, filt, accf_next); 84 return (0); 85 } 86 87 int 88 accept_filt_del(char *name) 89 { 90 struct accept_filter *p; 91 92 p = accept_filt_get(name); 93 if (p == NULL) 94 return (ENOENT); 95 96 p->accf_callback = NULL; 97 return (0); 98 } 99 100 struct accept_filter * 101 accept_filt_get(char *name) 102 { 103 struct accept_filter *p; 104 105 SLIST_FOREACH(p, &accept_filtlsthd, accf_next) 106 if (strcmp(p->accf_name, name) == 0) 107 return (p); 108 109 return (NULL); 110 } 111 112 int 113 accept_filt_generic_mod_event(module_t mod, int event, void *data) 114 { 115 struct accept_filter *p; 116 struct accept_filter *accfp = (struct accept_filter *) data; 117 int error; 118 119 switch (event) { 120 case MOD_LOAD: 121 MALLOC(p, struct accept_filter *, sizeof(*p), M_ACCF, M_WAITOK); 122 bcopy(accfp, p, sizeof(*p)); 123 crit_enter(); 124 error = accept_filt_add(p); 125 crit_exit(); 126 break; 127 128 case MOD_UNLOAD: 129 /* 130 * Do not support unloading yet. we don't keep track of refcounts 131 * and unloading an accept filter callback and then having it called 132 * is a bad thing. A simple fix would be to track the refcount 133 * in the struct accept_filter. 134 */ 135 if (unloadable != 0) { 136 crit_enter(); 137 error = accept_filt_del(accfp->accf_name); 138 crit_exit(); 139 } else 140 error = EOPNOTSUPP; 141 break; 142 143 case MOD_SHUTDOWN: 144 error = 0; 145 break; 146 147 default: 148 error = EOPNOTSUPP; 149 break; 150 } 151 152 return (error); 153 } 154