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.2 2006/09/05 00:55:47 dillon Exp $ */
7 * Copyright (c) 2004 The DragonFly Project. All rights reserved.
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.
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
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.
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
41 * @(#)kern_subr.c 8.3 (Berkeley) 1/21/94
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/socket.h>
47 #include <sys/socketvar.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>
57 #include <netinet/in.h>
58 #include <netinet/in_var.h>
60 #include <net/pf/pfvar.h>
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
67 * BEWARE: this is not locked! Required locking is done by the caller.
71 hook_establish(struct hook_desc_head *head, int tail, void (*fn)(void *),
74 struct hook_desc *hdp;
76 hdp = (struct hook_desc *)kmalloc(sizeof (*hdp), M_DEVBUF, M_NOWAIT);
83 TAILQ_INSERT_TAIL(head, hdp, hd_list);
85 TAILQ_INSERT_HEAD(head, hdp, hd_list);
91 hook_disestablish(struct hook_desc_head *head, void *vhook)
93 struct hook_desc *hdp;
96 for (hdp = TAILQ_FIRST(head); hdp != NULL;
97 hdp = TAILQ_NEXT(hdp, hd_list))
101 panic("hook_disestablish: hook not established");
104 TAILQ_REMOVE(head, hdp, hd_list);
105 kfree(hdp, M_DEVBUF);
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.
115 dohooks(struct hook_desc_head *head, int flags)
117 struct hook_desc *hdp;
119 if ((flags & HOOK_REMOVE) == 0) {
120 TAILQ_FOREACH(hdp, head, hd_list) {
121 (*hdp->hd_fn)(hdp->hd_arg);
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 kfree(hdp, M_DEVBUF);