1 /* $FreeBSD: src/sys/contrib/pf/net/pf_subr.c,v 1.1 2004/06/16 23:24:00 mlaier Exp $ */ 2 /* from $OpenBSD: kern_subr.c,v 1.26 2003/10/31 11:10:41 markus Exp $ */ 3 /* $NetBSD: kern_subr.c,v 1.15 1996/04/09 17:21:56 ragge Exp $ */ 4 /* $DragonFly: src/sys/net/pf/pf_subr.c,v 1.1 2004/09/19 22:32:47 joerg Exp $ */ 5 6 /* 7 * Copyright (c) 2004 The DragonFly Project. All rights reserved. 8 * 9 * Copyright (c) 1982, 1986, 1991, 1993 10 * The Regents of the University of California. All rights reserved. 11 * (c) UNIX System Laboratories, Inc. 12 * All or some portions of this file are derived from material licensed 13 * to the University of California by American Telephone and Telegraph 14 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 15 * the permission of UNIX System Laboratories, Inc. 16 * 17 * Redistribution and use in source and binary forms, with or without 18 * modification, are permitted provided that the following conditions 19 * are met: 20 * 1. Redistributions of source code must retain the above copyright 21 * notice, this list of conditions and the following disclaimer. 22 * 2. Redistributions in binary form must reproduce the above copyright 23 * notice, this list of conditions and the following disclaimer in the 24 * documentation and/or other materials provided with the distribution. 25 * 3. Neither the name of the University nor the names of its contributors 26 * may be used to endorse or promote products derived from this software 27 * without specific prior written permission. 28 * 29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 32 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 39 * SUCH DAMAGE. 40 * 41 * @(#)kern_subr.c 8.3 (Berkeley) 1/21/94 42 */ 43 44 #include <sys/param.h> 45 #include <sys/systm.h> 46 #include <sys/socket.h> 47 #include <sys/socketvar.h> 48 #include <sys/proc.h> 49 #include <sys/malloc.h> 50 #include <sys/queue.h> 51 #include <sys/kernel.h> 52 #include <sys/resourcevar.h> 53 #include <vm/vm_zone.h> 54 55 #include <net/if.h> 56 57 #include <netinet/in.h> 58 #include <netinet/in_var.h> 59 60 #include <net/pf/pfvar.h> 61 62 /* 63 * This implements additional functions used by pf which can not be ported 64 * easyly. At this point it boils down to mostly the Net/OpenBSD hook 65 * implementation. 66 * 67 * BEWARE: this is not locked! Required locking is done by the caller. 68 */ 69 70 void * 71 hook_establish(struct hook_desc_head *head, int tail, void (*fn)(void *), 72 void *arg) 73 { 74 struct hook_desc *hdp; 75 76 hdp = (struct hook_desc *)malloc(sizeof (*hdp), M_DEVBUF, M_NOWAIT); 77 if (hdp == NULL) 78 return (NULL); 79 80 hdp->hd_fn = fn; 81 hdp->hd_arg = arg; 82 if (tail) 83 TAILQ_INSERT_TAIL(head, hdp, hd_list); 84 else 85 TAILQ_INSERT_HEAD(head, hdp, hd_list); 86 87 return (hdp); 88 } 89 90 void 91 hook_disestablish(struct hook_desc_head *head, void *vhook) 92 { 93 struct hook_desc *hdp; 94 95 #ifdef DIAGNOSTIC 96 for (hdp = TAILQ_FIRST(head); hdp != NULL; 97 hdp = TAILQ_NEXT(hdp, hd_list)) 98 if (hdp == vhook) 99 break; 100 if (hdp == NULL) 101 panic("hook_disestablish: hook not established"); 102 #endif 103 hdp = vhook; 104 TAILQ_REMOVE(head, hdp, hd_list); 105 free(hdp, M_DEVBUF); 106 } 107 108 /* 109 * Run hooks. Startup hooks are invoked right after scheduler_start but 110 * before root is mounted. Shutdown hooks are invoked immediately before the 111 * system is halted or rebooted, i.e. after file systems unmounted, 112 * after crash dump done, etc. 113 */ 114 void 115 dohooks(struct hook_desc_head *head, int flags) 116 { 117 struct hook_desc *hdp; 118 119 if ((flags & HOOK_REMOVE) == 0) { 120 TAILQ_FOREACH(hdp, head, hd_list) { 121 (*hdp->hd_fn)(hdp->hd_arg); 122 } 123 } else { 124 while ((hdp = TAILQ_FIRST(head)) != NULL) { 125 TAILQ_REMOVE(head, hdp, hd_list); 126 (*hdp->hd_fn)(hdp->hd_arg); 127 if ((flags & HOOK_FREE) != 0) 128 free(hdp, M_DEVBUF); 129 } 130 } 131 } 132