xref: /freebsd/sys/net/pfil.c (revision 0b70e3e78b0279c66be06dea27bcdaf5eadf663d)
1e9c7bebfSDarren Reed /*	$FreeBSD$ */
2134ea224SSam Leffler /*	$NetBSD: pfil.c,v 1.20 2001/11/12 23:49:46 lukem Exp $	*/
3e9c7bebfSDarren Reed 
4c398230bSWarner Losh /*-
5fe267a55SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
6fe267a55SPedro F. Giffuni  *
7b252313fSGleb Smirnoff  * Copyright (c) 2019 Gleb Smirnoff <glebius@FreeBSD.org>
8e9c7bebfSDarren Reed  * Copyright (c) 1996 Matthew R. Green
9e9c7bebfSDarren Reed  * All rights reserved.
10e9c7bebfSDarren Reed  *
11e9c7bebfSDarren Reed  * Redistribution and use in source and binary forms, with or without
12e9c7bebfSDarren Reed  * modification, are permitted provided that the following conditions
13e9c7bebfSDarren Reed  * are met:
14e9c7bebfSDarren Reed  * 1. Redistributions of source code must retain the above copyright
15e9c7bebfSDarren Reed  *    notice, this list of conditions and the following disclaimer.
16e9c7bebfSDarren Reed  * 2. Redistributions in binary form must reproduce the above copyright
17e9c7bebfSDarren Reed  *    notice, this list of conditions and the following disclaimer in the
18e9c7bebfSDarren Reed  *    documentation and/or other materials provided with the distribution.
19e9c7bebfSDarren Reed  * 3. The name of the author may not be used to endorse or promote products
20e9c7bebfSDarren Reed  *    derived from this software without specific prior written permission.
21e9c7bebfSDarren Reed  *
22e9c7bebfSDarren Reed  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23e9c7bebfSDarren Reed  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24e9c7bebfSDarren Reed  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25e9c7bebfSDarren Reed  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26e9c7bebfSDarren Reed  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27e9c7bebfSDarren Reed  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28e9c7bebfSDarren Reed  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29e9c7bebfSDarren Reed  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30e9c7bebfSDarren Reed  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31e9c7bebfSDarren Reed  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32e9c7bebfSDarren Reed  * SUCH DAMAGE.
33e9c7bebfSDarren Reed  */
34e9c7bebfSDarren Reed 
35e9c7bebfSDarren Reed #include <sys/param.h>
36b252313fSGleb Smirnoff #include <sys/conf.h>
37134ea224SSam Leffler #include <sys/kernel.h>
38b252313fSGleb Smirnoff #include <sys/epoch.h>
39e9c7bebfSDarren Reed #include <sys/errno.h>
40604afec4SChristian S.J. Peron #include <sys/lock.h>
41e9c7bebfSDarren Reed #include <sys/malloc.h>
42e9c7bebfSDarren Reed #include <sys/socket.h>
43e9c7bebfSDarren Reed #include <sys/socketvar.h>
44e9c7bebfSDarren Reed #include <sys/systm.h>
45134ea224SSam Leffler #include <sys/lock.h>
46134ea224SSam Leffler #include <sys/mutex.h>
47134ea224SSam Leffler #include <sys/proc.h>
48e9c7bebfSDarren Reed #include <sys/queue.h>
493ca1c423SGleb Smirnoff #include <sys/ucred.h>
503ca1c423SGleb Smirnoff #include <sys/jail.h>
51e9c7bebfSDarren Reed 
52e9c7bebfSDarren Reed #include <net/if.h>
5376039bc8SGleb Smirnoff #include <net/if_var.h>
54e9c7bebfSDarren Reed #include <net/pfil.h>
55e9c7bebfSDarren Reed 
56b252313fSGleb Smirnoff static MALLOC_DEFINE(M_PFIL, "pfil", "pfil(9) packet filter hooks");
57e9c7bebfSDarren Reed 
58b252313fSGleb Smirnoff static int pfil_ioctl(struct cdev *, u_long, caddr_t, int, struct thread *);
59b252313fSGleb Smirnoff static struct cdevsw pfil_cdevsw = {
60b252313fSGleb Smirnoff 	.d_ioctl =	pfil_ioctl,
61b252313fSGleb Smirnoff 	.d_name =	PFILDEV,
62b252313fSGleb Smirnoff 	.d_version =	D_VERSION,
63b252313fSGleb Smirnoff };
64b252313fSGleb Smirnoff static struct cdev *pfil_dev;
65134ea224SSam Leffler 
66b252313fSGleb Smirnoff static struct mtx pfil_lock;
67b252313fSGleb Smirnoff MTX_SYSINIT(pfil_mtxinit, &pfil_lock, "pfil(9) lock", MTX_DEF);
68b252313fSGleb Smirnoff #define	PFIL_LOCK()	mtx_lock(&pfil_lock)
69b252313fSGleb Smirnoff #define	PFIL_UNLOCK()	mtx_unlock(&pfil_lock)
70b252313fSGleb Smirnoff #define	PFIL_LOCK_ASSERT()	mtx_assert(&pfil_lock, MA_OWNED)
71b252313fSGleb Smirnoff 
72b252313fSGleb Smirnoff struct pfil_hook {
73b252313fSGleb Smirnoff 	pfil_func_t	 hook_func;
74b252313fSGleb Smirnoff 	void		*hook_ruleset;
75b252313fSGleb Smirnoff 	int		 hook_flags;
76b252313fSGleb Smirnoff 	int		 hook_links;
77b252313fSGleb Smirnoff 	enum pfil_types	 hook_type;
78b252313fSGleb Smirnoff 	const char	*hook_modname;
79b252313fSGleb Smirnoff 	const char	*hook_rulname;
80b252313fSGleb Smirnoff 	LIST_ENTRY(pfil_hook) hook_list;
81b252313fSGleb Smirnoff };
82b252313fSGleb Smirnoff 
83b252313fSGleb Smirnoff struct pfil_link {
84b252313fSGleb Smirnoff 	CK_STAILQ_ENTRY(pfil_link) link_chain;
85b252313fSGleb Smirnoff 	pfil_func_t		 link_func;
86b252313fSGleb Smirnoff 	void			*link_ruleset;
87b252313fSGleb Smirnoff 	int			 link_flags;
88b252313fSGleb Smirnoff 	struct pfil_hook	*link_hook;
89b252313fSGleb Smirnoff 	struct epoch_context	 link_epoch_ctx;
90b252313fSGleb Smirnoff };
91b252313fSGleb Smirnoff 
92b252313fSGleb Smirnoff typedef CK_STAILQ_HEAD(pfil_chain, pfil_link)	pfil_chain_t;
93b252313fSGleb Smirnoff struct pfil_head {
94b252313fSGleb Smirnoff 	int		 head_nhooksin;
95b252313fSGleb Smirnoff 	int		 head_nhooksout;
96b252313fSGleb Smirnoff 	pfil_chain_t	 head_in;
97b252313fSGleb Smirnoff 	pfil_chain_t	 head_out;
98b252313fSGleb Smirnoff 	int		 head_flags;
99b252313fSGleb Smirnoff 	enum pfil_types	 head_type;
100b252313fSGleb Smirnoff 	LIST_ENTRY(pfil_head) head_list;
101b252313fSGleb Smirnoff 	const char	*head_name;
102b252313fSGleb Smirnoff };
103134ea224SSam Leffler 
1040b4b0b0fSJulian Elischer LIST_HEAD(pfilheadhead, pfil_head);
105b252313fSGleb Smirnoff VNET_DEFINE_STATIC(struct pfilheadhead, pfil_head_list) =
106b252313fSGleb Smirnoff     LIST_HEAD_INITIALIZER(pfil_head_list);
1070b4b0b0fSJulian Elischer #define	V_pfil_head_list	VNET(pfil_head_list)
108134ea224SSam Leffler 
109b252313fSGleb Smirnoff LIST_HEAD(pfilhookhead, pfil_hook);
110b252313fSGleb Smirnoff VNET_DEFINE_STATIC(struct pfilhookhead, pfil_hook_list) =
111b252313fSGleb Smirnoff     LIST_HEAD_INITIALIZER(pfil_hook_list);
112b252313fSGleb Smirnoff #define	V_pfil_hook_list	VNET(pfil_hook_list)
113af48c203SAndrey V. Elsukov 
114b252313fSGleb Smirnoff static struct pfil_link *pfil_link_remove(pfil_chain_t *, pfil_hook_t );
115b252313fSGleb Smirnoff static void pfil_link_free(epoch_context_t);
116af48c203SAndrey V. Elsukov 
117c9341022SGleb Smirnoff int
118c9341022SGleb Smirnoff pfil_realloc(pfil_packet_t *p, int flags, struct ifnet *ifp)
119c9341022SGleb Smirnoff {
120c9341022SGleb Smirnoff 	struct mbuf *m;
121c9341022SGleb Smirnoff 
122c9341022SGleb Smirnoff 	MPASS(flags & PFIL_MEMPTR);
123c9341022SGleb Smirnoff 
124c9341022SGleb Smirnoff 	if ((m = m_devget(p->mem, PFIL_LENGTH(flags), 0, ifp, NULL)) == NULL)
125c9341022SGleb Smirnoff 		return (ENOMEM);
126c9341022SGleb Smirnoff 	*p = pfil_packet_align(*p);
127c9341022SGleb Smirnoff 	*p->m = m;
128c9341022SGleb Smirnoff 
129c9341022SGleb Smirnoff 	return (0);
130c9341022SGleb Smirnoff }
131c9341022SGleb Smirnoff 
132b252313fSGleb Smirnoff static __noinline int
133c9341022SGleb Smirnoff pfil_fake_mbuf(pfil_func_t func, pfil_packet_t *p, struct ifnet *ifp, int flags,
134b252313fSGleb Smirnoff     void *ruleset, struct inpcb *inp)
135b252313fSGleb Smirnoff {
136b252313fSGleb Smirnoff 	struct mbuf m, *mp;
137b252313fSGleb Smirnoff 	pfil_return_t rv;
138b252313fSGleb Smirnoff 
139b252313fSGleb Smirnoff 	(void)m_init(&m, M_NOWAIT, MT_DATA, M_NOFREE | M_PKTHDR);
140c9341022SGleb Smirnoff 	m_extadd(&m, p->mem, PFIL_LENGTH(flags), NULL, NULL, NULL, 0,
141c9341022SGleb Smirnoff 	    EXT_RXRING);
142b252313fSGleb Smirnoff 	m.m_len = m.m_pkthdr.len = PFIL_LENGTH(flags);
143b252313fSGleb Smirnoff 	mp = &m;
144b252313fSGleb Smirnoff 	flags &= ~(PFIL_MEMPTR | PFIL_LENMASK);
145b252313fSGleb Smirnoff 
146b252313fSGleb Smirnoff 	rv = func(&mp, ifp, flags, ruleset, inp);
147b252313fSGleb Smirnoff 	if (rv == PFIL_PASS && mp != &m) {
148b252313fSGleb Smirnoff 		/*
149b252313fSGleb Smirnoff 		 * Firewalls that need pfil_fake_mbuf() most likely don't
150c9341022SGleb Smirnoff 		 * know they need return PFIL_REALLOCED.
151b252313fSGleb Smirnoff 		 */
152b252313fSGleb Smirnoff 		rv = PFIL_REALLOCED;
153c9341022SGleb Smirnoff 		*p = pfil_packet_align(*p);
154c9341022SGleb Smirnoff 		*p->m = mp;
155b252313fSGleb Smirnoff 	}
156b252313fSGleb Smirnoff 
157b252313fSGleb Smirnoff 	return (rv);
158b252313fSGleb Smirnoff }
159af48c203SAndrey V. Elsukov 
160134ea224SSam Leffler /*
1618da01399SAndre Oppermann  * pfil_run_hooks() runs the specified packet filter hook chain.
162134ea224SSam Leffler  */
163134ea224SSam Leffler int
164b252313fSGleb Smirnoff pfil_run_hooks(struct pfil_head *head, pfil_packet_t p, struct ifnet *ifp,
165b252313fSGleb Smirnoff     int flags, struct inpcb *inp)
166134ea224SSam Leffler {
167b252313fSGleb Smirnoff 	pfil_chain_t *pch;
168b252313fSGleb Smirnoff 	struct pfil_link *link;
169b9fdb4b3SGleb Smirnoff 	pfil_return_t rv;
170b9fdb4b3SGleb Smirnoff 	bool realloc = false;
171134ea224SSam Leffler 
172e87ad0abSGleb Smirnoff 	NET_EPOCH_ASSERT();
173e87ad0abSGleb Smirnoff 
174b252313fSGleb Smirnoff 	if (PFIL_DIR(flags) == PFIL_IN)
175b252313fSGleb Smirnoff 		pch = &head->head_in;
176b252313fSGleb Smirnoff 	else if (__predict_true(PFIL_DIR(flags) == PFIL_OUT))
177b252313fSGleb Smirnoff 		pch = &head->head_out;
178887c60fcSAndre Oppermann 	else
179b252313fSGleb Smirnoff 		panic("%s: bogus flags %d", __func__, flags);
180b252313fSGleb Smirnoff 
181b252313fSGleb Smirnoff 	rv = PFIL_PASS;
182b252313fSGleb Smirnoff 	CK_STAILQ_FOREACH(link, pch, link_chain) {
183b252313fSGleb Smirnoff 		if ((flags & PFIL_MEMPTR) && !(link->link_flags & PFIL_MEMPTR))
184c9341022SGleb Smirnoff 			rv = pfil_fake_mbuf(link->link_func, &p, ifp, flags,
185c9341022SGleb Smirnoff 			    link->link_ruleset, inp);
186b252313fSGleb Smirnoff 		else
187b9fdb4b3SGleb Smirnoff 			rv = (*link->link_func)(p, ifp, flags,
188b252313fSGleb Smirnoff 			    link->link_ruleset, inp);
189b9fdb4b3SGleb Smirnoff 		if (rv == PFIL_DROPPED || rv == PFIL_CONSUMED)
190b252313fSGleb Smirnoff 			break;
191b9fdb4b3SGleb Smirnoff 		else if (rv == PFIL_REALLOCED) {
192b252313fSGleb Smirnoff 			flags &= ~(PFIL_MEMPTR | PFIL_LENMASK);
193b9fdb4b3SGleb Smirnoff 			realloc = true;
194887c60fcSAndre Oppermann 		}
1954dab1a18SAlexander V. Chernikov 	}
196b9fdb4b3SGleb Smirnoff 	if (realloc && rv == PFIL_PASS)
197b9fdb4b3SGleb Smirnoff 		rv = PFIL_REALLOCED;
198b9fdb4b3SGleb Smirnoff 	return (rv);
1994dab1a18SAlexander V. Chernikov }
2008da01399SAndre Oppermann 
201*0b70e3e7SMateusz Guzik static __always_inline int
202*0b70e3e7SMateusz Guzik pfil_mbuf_common(pfil_chain_t *pch, pfil_packet_t p, struct ifnet *ifp,
203*0b70e3e7SMateusz Guzik     int flags, struct inpcb *inp)
204*0b70e3e7SMateusz Guzik {
205*0b70e3e7SMateusz Guzik 	struct pfil_link *link;
206*0b70e3e7SMateusz Guzik 	pfil_return_t rv;
207*0b70e3e7SMateusz Guzik 
208*0b70e3e7SMateusz Guzik 	NET_EPOCH_ASSERT();
209*0b70e3e7SMateusz Guzik 	KASSERT(flags == PFIL_IN || flags == PFIL_OUT,
210*0b70e3e7SMateusz Guzik 	    ("%s: unsupported flags %d", __func__, flags));
211*0b70e3e7SMateusz Guzik 
212*0b70e3e7SMateusz Guzik 	rv = PFIL_PASS;
213*0b70e3e7SMateusz Guzik 	CK_STAILQ_FOREACH(link, pch, link_chain) {
214*0b70e3e7SMateusz Guzik 		rv = (*link->link_func)(p, ifp, flags, link->link_ruleset, inp);
215*0b70e3e7SMateusz Guzik 		if (rv == PFIL_DROPPED || rv == PFIL_CONSUMED)
216*0b70e3e7SMateusz Guzik 			break;
217*0b70e3e7SMateusz Guzik 	}
218*0b70e3e7SMateusz Guzik 	return (rv);
219*0b70e3e7SMateusz Guzik }
220*0b70e3e7SMateusz Guzik 
221*0b70e3e7SMateusz Guzik int
222*0b70e3e7SMateusz Guzik pfil_mbuf_in(struct pfil_head *head, pfil_packet_t p, struct ifnet *ifp,
223*0b70e3e7SMateusz Guzik    struct inpcb *inp)
224*0b70e3e7SMateusz Guzik {
225*0b70e3e7SMateusz Guzik 
226*0b70e3e7SMateusz Guzik 	return (pfil_mbuf_common(&head->head_in, p, ifp, PFIL_IN, inp));
227*0b70e3e7SMateusz Guzik }
228*0b70e3e7SMateusz Guzik 
229*0b70e3e7SMateusz Guzik int
230*0b70e3e7SMateusz Guzik pfil_mbuf_out(struct pfil_head *head, pfil_packet_t p, struct ifnet *ifp,
231*0b70e3e7SMateusz Guzik     struct inpcb *inp)
232*0b70e3e7SMateusz Guzik {
233*0b70e3e7SMateusz Guzik 
234*0b70e3e7SMateusz Guzik 	return (pfil_mbuf_common(&head->head_out, p, ifp, PFIL_OUT, inp));
235*0b70e3e7SMateusz Guzik }
236*0b70e3e7SMateusz Guzik 
2374dab1a18SAlexander V. Chernikov /*
238cee81198SRobert Watson  * pfil_head_register() registers a pfil_head with the packet filter hook
239cee81198SRobert Watson  * mechanism.
240134ea224SSam Leffler  */
241b252313fSGleb Smirnoff pfil_head_t
242b252313fSGleb Smirnoff pfil_head_register(struct pfil_head_args *pa)
243134ea224SSam Leffler {
244b252313fSGleb Smirnoff 	struct pfil_head *head, *list;
245134ea224SSam Leffler 
246b252313fSGleb Smirnoff 	MPASS(pa->pa_version == PFIL_VERSION);
247b252313fSGleb Smirnoff 
248b252313fSGleb Smirnoff 	head = malloc(sizeof(struct pfil_head), M_PFIL, M_WAITOK);
249b252313fSGleb Smirnoff 
250b252313fSGleb Smirnoff 	head->head_nhooksin = head->head_nhooksout = 0;
251b252313fSGleb Smirnoff 	head->head_flags = pa->pa_flags;
252b252313fSGleb Smirnoff 	head->head_type = pa->pa_type;
253b252313fSGleb Smirnoff 	head->head_name = pa->pa_headname;
254b252313fSGleb Smirnoff 	CK_STAILQ_INIT(&head->head_in);
255b252313fSGleb Smirnoff 	CK_STAILQ_INIT(&head->head_out);
256b252313fSGleb Smirnoff 
257b252313fSGleb Smirnoff 	PFIL_LOCK();
258b252313fSGleb Smirnoff 	LIST_FOREACH(list, &V_pfil_head_list, head_list)
259b252313fSGleb Smirnoff 		if (strcmp(pa->pa_headname, list->head_name) == 0) {
260b252313fSGleb Smirnoff 			printf("pfil: duplicate head \"%s\"\n",
261b252313fSGleb Smirnoff 			    pa->pa_headname);
262134ea224SSam Leffler 		}
263b252313fSGleb Smirnoff 	LIST_INSERT_HEAD(&V_pfil_head_list, head, head_list);
264b252313fSGleb Smirnoff 	PFIL_UNLOCK();
265b252313fSGleb Smirnoff 
266b252313fSGleb Smirnoff 	return (head);
267134ea224SSam Leffler }
268134ea224SSam Leffler 
269134ea224SSam Leffler /*
270d2c205d5SRobert Watson  * pfil_head_unregister() removes a pfil_head from the packet filter hook
271d2c205d5SRobert Watson  * mechanism.  The producer of the hook promises that all outstanding
272d2c205d5SRobert Watson  * invocations of the hook have completed before it unregisters the hook.
273134ea224SSam Leffler  */
274b252313fSGleb Smirnoff void
275b252313fSGleb Smirnoff pfil_head_unregister(pfil_head_t ph)
276134ea224SSam Leffler {
277b252313fSGleb Smirnoff 	struct pfil_link *link, *next;
278134ea224SSam Leffler 
279b252313fSGleb Smirnoff 	PFIL_LOCK();
280b252313fSGleb Smirnoff 	LIST_REMOVE(ph, head_list);
281b252313fSGleb Smirnoff 
282b252313fSGleb Smirnoff 	CK_STAILQ_FOREACH_SAFE(link, &ph->head_in, link_chain, next) {
283b252313fSGleb Smirnoff 		link->link_hook->hook_links--;
284b252313fSGleb Smirnoff 		free(link, M_PFIL);
285b252313fSGleb Smirnoff 	}
286b252313fSGleb Smirnoff 	CK_STAILQ_FOREACH_SAFE(link, &ph->head_out, link_chain, next) {
287b252313fSGleb Smirnoff 		link->link_hook->hook_links--;
288b252313fSGleb Smirnoff 		free(link, M_PFIL);
289b252313fSGleb Smirnoff 	}
290b252313fSGleb Smirnoff 	PFIL_UNLOCK();
291134ea224SSam Leffler }
292134ea224SSam Leffler 
293b252313fSGleb Smirnoff pfil_hook_t
294b252313fSGleb Smirnoff pfil_add_hook(struct pfil_hook_args *pa)
295134ea224SSam Leffler {
296b252313fSGleb Smirnoff 	struct pfil_hook *hook, *list;
297134ea224SSam Leffler 
298b252313fSGleb Smirnoff 	MPASS(pa->pa_version == PFIL_VERSION);
299b252313fSGleb Smirnoff 
300b252313fSGleb Smirnoff 	hook = malloc(sizeof(struct pfil_hook), M_PFIL, M_WAITOK | M_ZERO);
301b252313fSGleb Smirnoff 	hook->hook_func = pa->pa_func;
302b252313fSGleb Smirnoff 	hook->hook_ruleset = pa->pa_ruleset;
303b252313fSGleb Smirnoff 	hook->hook_flags = pa->pa_flags;
304b252313fSGleb Smirnoff 	hook->hook_type = pa->pa_type;
305b252313fSGleb Smirnoff 	hook->hook_modname = pa->pa_modname;
306b252313fSGleb Smirnoff 	hook->hook_rulname = pa->pa_rulname;
307b252313fSGleb Smirnoff 
308b252313fSGleb Smirnoff 	PFIL_LOCK();
309b252313fSGleb Smirnoff 	LIST_FOREACH(list, &V_pfil_hook_list, hook_list)
310b252313fSGleb Smirnoff 		if (strcmp(pa->pa_modname, list->hook_modname) == 0 &&
311b252313fSGleb Smirnoff 		    strcmp(pa->pa_rulname, list->hook_rulname) == 0) {
312b252313fSGleb Smirnoff 			printf("pfil: duplicate hook \"%s:%s\"\n",
313b252313fSGleb Smirnoff 			    pa->pa_modname, pa->pa_rulname);
314e9c7bebfSDarren Reed 		}
315b252313fSGleb Smirnoff 	LIST_INSERT_HEAD(&V_pfil_hook_list, hook, hook_list);
316b252313fSGleb Smirnoff 	PFIL_UNLOCK();
317e9c7bebfSDarren Reed 
318b252313fSGleb Smirnoff 	return (hook);
319effaab88SKristof Provost }
320effaab88SKristof Provost 
321effaab88SKristof Provost static int
322b252313fSGleb Smirnoff pfil_unlink(struct pfil_link_args *pa, pfil_head_t head, pfil_hook_t hook)
323effaab88SKristof Provost {
324b252313fSGleb Smirnoff 	struct pfil_link *in, *out;
325e9c7bebfSDarren Reed 
326b252313fSGleb Smirnoff 	PFIL_LOCK_ASSERT();
327e9c7bebfSDarren Reed 
328b252313fSGleb Smirnoff 	if (pa->pa_flags & PFIL_IN) {
329b252313fSGleb Smirnoff 		in = pfil_link_remove(&head->head_in, hook);
330b252313fSGleb Smirnoff 		if (in != NULL) {
331b252313fSGleb Smirnoff 			head->head_nhooksin--;
332b252313fSGleb Smirnoff 			hook->hook_links--;
333effaab88SKristof Provost 		}
334b252313fSGleb Smirnoff 	} else
335b252313fSGleb Smirnoff 		in = NULL;
336b252313fSGleb Smirnoff 	if (pa->pa_flags & PFIL_OUT) {
337b252313fSGleb Smirnoff 		out = pfil_link_remove(&head->head_out, hook);
338b252313fSGleb Smirnoff 		if (out != NULL) {
339b252313fSGleb Smirnoff 			head->head_nhooksout--;
340b252313fSGleb Smirnoff 			hook->hook_links--;
341604afec4SChristian S.J. Peron 		}
342b252313fSGleb Smirnoff 	} else
343b252313fSGleb Smirnoff 		out = NULL;
344b252313fSGleb Smirnoff 	PFIL_UNLOCK();
345e9c7bebfSDarren Reed 
346b252313fSGleb Smirnoff 	if (in != NULL)
347e87ad0abSGleb Smirnoff 		NET_EPOCH_CALL(pfil_link_free, &in->link_epoch_ctx);
348b252313fSGleb Smirnoff 	if (out != NULL)
349e87ad0abSGleb Smirnoff 		NET_EPOCH_CALL(pfil_link_free, &out->link_epoch_ctx);
350134ea224SSam Leffler 
351b252313fSGleb Smirnoff 	if (in == NULL && out == NULL)
352b252313fSGleb Smirnoff 		return (ENOENT);
353134ea224SSam Leffler 	else
354cee81198SRobert Watson 		return (0);
355134ea224SSam Leffler }
356134ea224SSam Leffler 
357b252313fSGleb Smirnoff int
358b252313fSGleb Smirnoff pfil_link(struct pfil_link_args *pa)
359b252313fSGleb Smirnoff {
360b252313fSGleb Smirnoff 	struct pfil_link *in, *out, *link;
361b252313fSGleb Smirnoff 	struct pfil_head *head;
362b252313fSGleb Smirnoff 	struct pfil_hook *hook;
363b252313fSGleb Smirnoff 	int error;
364b252313fSGleb Smirnoff 
365b252313fSGleb Smirnoff 	MPASS(pa->pa_version == PFIL_VERSION);
366b252313fSGleb Smirnoff 
367b252313fSGleb Smirnoff 	if ((pa->pa_flags & (PFIL_IN | PFIL_UNLINK)) == PFIL_IN)
368b252313fSGleb Smirnoff 		in = malloc(sizeof(*in), M_PFIL, M_WAITOK | M_ZERO);
369b252313fSGleb Smirnoff 	else
370b252313fSGleb Smirnoff 		in = NULL;
371b252313fSGleb Smirnoff 	if ((pa->pa_flags & (PFIL_OUT | PFIL_UNLINK)) == PFIL_OUT)
372b252313fSGleb Smirnoff 		out = malloc(sizeof(*out), M_PFIL, M_WAITOK | M_ZERO);
373b252313fSGleb Smirnoff 	else
374b252313fSGleb Smirnoff 		out = NULL;
375b252313fSGleb Smirnoff 
376b252313fSGleb Smirnoff 	PFIL_LOCK();
377b252313fSGleb Smirnoff 	if (pa->pa_flags & PFIL_HEADPTR)
378b252313fSGleb Smirnoff 		head = pa->pa_head;
379b252313fSGleb Smirnoff 	else
380b252313fSGleb Smirnoff 		LIST_FOREACH(head, &V_pfil_head_list, head_list)
381b252313fSGleb Smirnoff 			if (strcmp(pa->pa_headname, head->head_name) == 0)
382b252313fSGleb Smirnoff 				break;
383b252313fSGleb Smirnoff 	if (pa->pa_flags & PFIL_HOOKPTR)
384b252313fSGleb Smirnoff 		hook = pa->pa_hook;
385b252313fSGleb Smirnoff 	else
386b252313fSGleb Smirnoff 		LIST_FOREACH(hook, &V_pfil_hook_list, hook_list)
387b252313fSGleb Smirnoff 			if (strcmp(pa->pa_modname, hook->hook_modname) == 0 &&
388b252313fSGleb Smirnoff 			    strcmp(pa->pa_rulname, hook->hook_rulname) == 0)
389b252313fSGleb Smirnoff 				break;
390b252313fSGleb Smirnoff 	if (head == NULL || hook == NULL) {
391b252313fSGleb Smirnoff 		error = ENOENT;
392b252313fSGleb Smirnoff 		goto fail;
393b252313fSGleb Smirnoff 	}
394b252313fSGleb Smirnoff 
395b252313fSGleb Smirnoff 	if (pa->pa_flags & PFIL_UNLINK)
396b252313fSGleb Smirnoff 		return (pfil_unlink(pa, head, hook));
397b252313fSGleb Smirnoff 
398b252313fSGleb Smirnoff 	if (head->head_type != hook->hook_type ||
399b252313fSGleb Smirnoff 	    ((hook->hook_flags & pa->pa_flags) & ~head->head_flags)) {
400b252313fSGleb Smirnoff 		error = EINVAL;
401b252313fSGleb Smirnoff 		goto fail;
402b252313fSGleb Smirnoff 	}
403b252313fSGleb Smirnoff 
404b252313fSGleb Smirnoff 	if (pa->pa_flags & PFIL_IN)
405b252313fSGleb Smirnoff 		CK_STAILQ_FOREACH(link, &head->head_in, link_chain)
406b252313fSGleb Smirnoff 			if (link->link_hook == hook) {
407b252313fSGleb Smirnoff 				error = EEXIST;
408b252313fSGleb Smirnoff 				goto fail;
409b252313fSGleb Smirnoff 			}
410b252313fSGleb Smirnoff 	if (pa->pa_flags & PFIL_OUT)
411b252313fSGleb Smirnoff 		CK_STAILQ_FOREACH(link, &head->head_out, link_chain)
412b252313fSGleb Smirnoff 			if (link->link_hook == hook) {
413b252313fSGleb Smirnoff 				error = EEXIST;
414b252313fSGleb Smirnoff 				goto fail;
415b252313fSGleb Smirnoff 			}
416b252313fSGleb Smirnoff 
417b252313fSGleb Smirnoff 	if (pa->pa_flags & PFIL_IN) {
418b252313fSGleb Smirnoff 		in->link_hook = hook;
419b252313fSGleb Smirnoff 		in->link_func = hook->hook_func;
420b252313fSGleb Smirnoff 		in->link_flags = hook->hook_flags;
421b252313fSGleb Smirnoff 		in->link_ruleset = hook->hook_ruleset;
422b252313fSGleb Smirnoff 		if (pa->pa_flags & PFIL_APPEND)
423b252313fSGleb Smirnoff 			CK_STAILQ_INSERT_TAIL(&head->head_in, in, link_chain);
424b252313fSGleb Smirnoff 		else
425b252313fSGleb Smirnoff 			CK_STAILQ_INSERT_HEAD(&head->head_in, in, link_chain);
426b252313fSGleb Smirnoff 		hook->hook_links++;
427b252313fSGleb Smirnoff 		head->head_nhooksin++;
428b252313fSGleb Smirnoff 	}
429b252313fSGleb Smirnoff 	if (pa->pa_flags & PFIL_OUT) {
430b252313fSGleb Smirnoff 		out->link_hook = hook;
431b252313fSGleb Smirnoff 		out->link_func = hook->hook_func;
432b252313fSGleb Smirnoff 		out->link_flags = hook->hook_flags;
433b252313fSGleb Smirnoff 		out->link_ruleset = hook->hook_ruleset;
434b252313fSGleb Smirnoff 		if (pa->pa_flags & PFIL_APPEND)
435b252313fSGleb Smirnoff 			CK_STAILQ_INSERT_HEAD(&head->head_out, out, link_chain);
436b252313fSGleb Smirnoff 		else
437b252313fSGleb Smirnoff 			CK_STAILQ_INSERT_TAIL(&head->head_out, out, link_chain);
438b252313fSGleb Smirnoff 		hook->hook_links++;
439b252313fSGleb Smirnoff 		head->head_nhooksout++;
440b252313fSGleb Smirnoff 	}
441b252313fSGleb Smirnoff 	PFIL_UNLOCK();
442b252313fSGleb Smirnoff 
443b252313fSGleb Smirnoff 	return (0);
444b252313fSGleb Smirnoff 
445b252313fSGleb Smirnoff fail:
446b252313fSGleb Smirnoff 	PFIL_UNLOCK();
447b252313fSGleb Smirnoff 	free(in, M_PFIL);
448b252313fSGleb Smirnoff 	free(out, M_PFIL);
449b252313fSGleb Smirnoff 	return (error);
450b252313fSGleb Smirnoff }
451b252313fSGleb Smirnoff 
452b252313fSGleb Smirnoff static void
453b252313fSGleb Smirnoff pfil_link_free(epoch_context_t ctx)
454b252313fSGleb Smirnoff {
455b252313fSGleb Smirnoff 	struct pfil_link *link;
456b252313fSGleb Smirnoff 
457b252313fSGleb Smirnoff 	link = __containerof(ctx, struct pfil_link, link_epoch_ctx);
458b252313fSGleb Smirnoff 	free(link, M_PFIL);
459b252313fSGleb Smirnoff }
460b252313fSGleb Smirnoff 
461b252313fSGleb Smirnoff /*
462b252313fSGleb Smirnoff  * pfil_remove_hook removes a filter from all filtering points.
463b252313fSGleb Smirnoff  */
464b252313fSGleb Smirnoff void
465b252313fSGleb Smirnoff pfil_remove_hook(pfil_hook_t hook)
466b252313fSGleb Smirnoff {
467b252313fSGleb Smirnoff 	struct pfil_head *head;
468b252313fSGleb Smirnoff 	struct pfil_link *in, *out;
469b252313fSGleb Smirnoff 
470b252313fSGleb Smirnoff 	PFIL_LOCK();
471b252313fSGleb Smirnoff 	LIST_FOREACH(head, &V_pfil_head_list, head_list) {
472b252313fSGleb Smirnoff retry:
473b252313fSGleb Smirnoff 		in = pfil_link_remove(&head->head_in, hook);
474b252313fSGleb Smirnoff 		if (in != NULL) {
475b252313fSGleb Smirnoff 			head->head_nhooksin--;
476b252313fSGleb Smirnoff 			hook->hook_links--;
477e87ad0abSGleb Smirnoff 			NET_EPOCH_CALL(pfil_link_free, &in->link_epoch_ctx);
478b252313fSGleb Smirnoff 		}
479b252313fSGleb Smirnoff 		out = pfil_link_remove(&head->head_out, hook);
480b252313fSGleb Smirnoff 		if (out != NULL) {
481b252313fSGleb Smirnoff 			head->head_nhooksout--;
482b252313fSGleb Smirnoff 			hook->hook_links--;
483e87ad0abSGleb Smirnoff 			NET_EPOCH_CALL(pfil_link_free, &out->link_epoch_ctx);
484b252313fSGleb Smirnoff 		}
485b252313fSGleb Smirnoff 		if (in != NULL || out != NULL)
486b252313fSGleb Smirnoff 			/* What if some stupid admin put same filter twice? */
487b252313fSGleb Smirnoff 			goto retry;
488b252313fSGleb Smirnoff 	}
489b252313fSGleb Smirnoff 	LIST_REMOVE(hook, hook_list);
490b252313fSGleb Smirnoff 	PFIL_UNLOCK();
491b252313fSGleb Smirnoff 	MPASS(hook->hook_links == 0);
492b252313fSGleb Smirnoff 	free(hook, M_PFIL);
493b252313fSGleb Smirnoff }
494b252313fSGleb Smirnoff 
495e9c7bebfSDarren Reed /*
4968da01399SAndre Oppermann  * Internal: Remove a pfil hook from a hook chain.
497e9c7bebfSDarren Reed  */
498b252313fSGleb Smirnoff static struct pfil_link *
499b252313fSGleb Smirnoff pfil_link_remove(pfil_chain_t *chain, pfil_hook_t hook)
500e9c7bebfSDarren Reed {
501b252313fSGleb Smirnoff 	struct pfil_link *link;
502e9c7bebfSDarren Reed 
503b252313fSGleb Smirnoff 	PFIL_LOCK_ASSERT();
504b252313fSGleb Smirnoff 
505b252313fSGleb Smirnoff 	CK_STAILQ_FOREACH(link, chain, link_chain)
506b252313fSGleb Smirnoff 		if (link->link_hook == hook) {
507b252313fSGleb Smirnoff 			CK_STAILQ_REMOVE(chain, link, pfil_link, link_chain);
508b252313fSGleb Smirnoff 			return (link);
509e9c7bebfSDarren Reed 		}
5100b4b0b0fSJulian Elischer 
511b252313fSGleb Smirnoff 	return (NULL);
512b252313fSGleb Smirnoff }
513b252313fSGleb Smirnoff 
514b9dbac48SBjoern A. Zeeb static void
515b252313fSGleb Smirnoff pfil_init(const void *unused __unused)
5160b4b0b0fSJulian Elischer {
517b252313fSGleb Smirnoff 	struct make_dev_args args;
518c681cce9SMateusz Guzik 	int error __diagused;
519cee81198SRobert Watson 
520b252313fSGleb Smirnoff 	make_dev_args_init(&args);
521b252313fSGleb Smirnoff 	args.mda_flags = MAKEDEV_WAITOK | MAKEDEV_CHECKNAME;
522b252313fSGleb Smirnoff 	args.mda_devsw = &pfil_cdevsw;
523b252313fSGleb Smirnoff 	args.mda_uid = UID_ROOT;
524b252313fSGleb Smirnoff 	args.mda_gid = GID_WHEEL;
525b252313fSGleb Smirnoff 	args.mda_mode = 0600;
526b252313fSGleb Smirnoff 	error = make_dev_s(&args, &pfil_dev, PFILDEV);
527b252313fSGleb Smirnoff 	KASSERT(error == 0, ("%s: failed to create dev: %d", __func__, error));
5280b4b0b0fSJulian Elischer }
529cee81198SRobert Watson /*
53089856f7eSBjoern A. Zeeb  * Make sure the pfil bits are first before any possible subsystem which
53189856f7eSBjoern A. Zeeb  * might piggyback on the SI_SUB_PROTO_PFIL.
5320b4b0b0fSJulian Elischer  */
533b252313fSGleb Smirnoff SYSINIT(pfil_init, SI_SUB_PROTO_PFIL, SI_ORDER_FIRST, pfil_init, NULL);
5340b4b0b0fSJulian Elischer 
5350b4b0b0fSJulian Elischer /*
536b252313fSGleb Smirnoff  * User control interface.
5370b4b0b0fSJulian Elischer  */
538b252313fSGleb Smirnoff static int pfilioc_listheads(struct pfilioc_list *);
539b252313fSGleb Smirnoff static int pfilioc_listhooks(struct pfilioc_list *);
540b252313fSGleb Smirnoff static int pfilioc_link(struct pfilioc_link *);
541b252313fSGleb Smirnoff 
542b252313fSGleb Smirnoff static int
543b252313fSGleb Smirnoff pfil_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flags,
544b252313fSGleb Smirnoff     struct thread *td)
545b252313fSGleb Smirnoff {
546b252313fSGleb Smirnoff 	int error;
547b252313fSGleb Smirnoff 
5483ca1c423SGleb Smirnoff 	CURVNET_SET(TD_TO_VNET(td));
549b252313fSGleb Smirnoff 	error = 0;
550b252313fSGleb Smirnoff 	switch (cmd) {
551b252313fSGleb Smirnoff 	case PFILIOC_LISTHEADS:
552b252313fSGleb Smirnoff 		error = pfilioc_listheads((struct pfilioc_list *)addr);
553b252313fSGleb Smirnoff 		break;
554b252313fSGleb Smirnoff 	case PFILIOC_LISTHOOKS:
555b252313fSGleb Smirnoff 		error = pfilioc_listhooks((struct pfilioc_list *)addr);
556b252313fSGleb Smirnoff 		break;
557b252313fSGleb Smirnoff 	case PFILIOC_LINK:
558b252313fSGleb Smirnoff 		error = pfilioc_link((struct pfilioc_link *)addr);
559b252313fSGleb Smirnoff 		break;
560b252313fSGleb Smirnoff 	default:
5613ca1c423SGleb Smirnoff 		error = EINVAL;
5623ca1c423SGleb Smirnoff 		break;
563b252313fSGleb Smirnoff 	}
5643ca1c423SGleb Smirnoff 	CURVNET_RESTORE();
565b252313fSGleb Smirnoff 	return (error);
566b252313fSGleb Smirnoff }
567b252313fSGleb Smirnoff 
568b252313fSGleb Smirnoff static int
569b252313fSGleb Smirnoff pfilioc_listheads(struct pfilioc_list *req)
570b252313fSGleb Smirnoff {
571b252313fSGleb Smirnoff 	struct pfil_head *head;
572b252313fSGleb Smirnoff 	struct pfil_link *link;
573b252313fSGleb Smirnoff 	struct pfilioc_head *iohead;
574b252313fSGleb Smirnoff 	struct pfilioc_hook *iohook;
575b252313fSGleb Smirnoff 	u_int nheads, nhooks, hd, hk;
576b252313fSGleb Smirnoff 	int error;
577b252313fSGleb Smirnoff 
578b252313fSGleb Smirnoff 	PFIL_LOCK();
579b252313fSGleb Smirnoff restart:
580b252313fSGleb Smirnoff 	nheads = nhooks = 0;
581b252313fSGleb Smirnoff 	LIST_FOREACH(head, &V_pfil_head_list, head_list) {
582b252313fSGleb Smirnoff 		nheads++;
583b252313fSGleb Smirnoff 		nhooks += head->head_nhooksin + head->head_nhooksout;
584b252313fSGleb Smirnoff 	}
585b252313fSGleb Smirnoff 	PFIL_UNLOCK();
586b252313fSGleb Smirnoff 
587b252313fSGleb Smirnoff 	if (req->pio_nheads < nheads || req->pio_nhooks < nhooks) {
588b252313fSGleb Smirnoff 		req->pio_nheads = nheads;
589b252313fSGleb Smirnoff 		req->pio_nhooks = nhooks;
590b252313fSGleb Smirnoff 		return (0);
591b252313fSGleb Smirnoff 	}
592b252313fSGleb Smirnoff 
593b252313fSGleb Smirnoff 	iohead = malloc(sizeof(*iohead) * nheads, M_TEMP, M_WAITOK);
594b252313fSGleb Smirnoff 	iohook = malloc(sizeof(*iohook) * nhooks, M_TEMP, M_WAITOK);
595b252313fSGleb Smirnoff 
596b252313fSGleb Smirnoff 	hd = hk = 0;
597b252313fSGleb Smirnoff 	PFIL_LOCK();
598b252313fSGleb Smirnoff 	LIST_FOREACH(head, &V_pfil_head_list, head_list) {
599b252313fSGleb Smirnoff 		if (hd + 1 > nheads ||
600b252313fSGleb Smirnoff 		    hk + head->head_nhooksin + head->head_nhooksout > nhooks) {
601b252313fSGleb Smirnoff 			/* Configuration changed during malloc(). */
602b252313fSGleb Smirnoff 			free(iohead, M_TEMP);
603b252313fSGleb Smirnoff 			free(iohook, M_TEMP);
604b252313fSGleb Smirnoff 			goto restart;
605b252313fSGleb Smirnoff 		}
606b252313fSGleb Smirnoff 		strlcpy(iohead[hd].pio_name, head->head_name,
607b252313fSGleb Smirnoff 			sizeof(iohead[0].pio_name));
608b252313fSGleb Smirnoff 		iohead[hd].pio_nhooksin = head->head_nhooksin;
609b252313fSGleb Smirnoff 		iohead[hd].pio_nhooksout = head->head_nhooksout;
610b252313fSGleb Smirnoff 		iohead[hd].pio_type = head->head_type;
611b252313fSGleb Smirnoff 		CK_STAILQ_FOREACH(link, &head->head_in, link_chain) {
612b252313fSGleb Smirnoff 			strlcpy(iohook[hk].pio_module,
613b252313fSGleb Smirnoff 			    link->link_hook->hook_modname,
614b252313fSGleb Smirnoff 			    sizeof(iohook[0].pio_module));
615b252313fSGleb Smirnoff 			strlcpy(iohook[hk].pio_ruleset,
616b252313fSGleb Smirnoff 			    link->link_hook->hook_rulname,
617b252313fSGleb Smirnoff 			    sizeof(iohook[0].pio_ruleset));
618b252313fSGleb Smirnoff 			hk++;
619b252313fSGleb Smirnoff 		}
620b252313fSGleb Smirnoff 		CK_STAILQ_FOREACH(link, &head->head_out, link_chain) {
621b252313fSGleb Smirnoff 			strlcpy(iohook[hk].pio_module,
622b252313fSGleb Smirnoff 			    link->link_hook->hook_modname,
623b252313fSGleb Smirnoff 			    sizeof(iohook[0].pio_module));
624b252313fSGleb Smirnoff 			strlcpy(iohook[hk].pio_ruleset,
625b252313fSGleb Smirnoff 			    link->link_hook->hook_rulname,
626b252313fSGleb Smirnoff 			    sizeof(iohook[0].pio_ruleset));
627b252313fSGleb Smirnoff 			hk++;
628b252313fSGleb Smirnoff 		}
629b252313fSGleb Smirnoff 		hd++;
630b252313fSGleb Smirnoff 	}
631b252313fSGleb Smirnoff 	PFIL_UNLOCK();
632b252313fSGleb Smirnoff 
633b252313fSGleb Smirnoff 	error = copyout(iohead, req->pio_heads,
634b252313fSGleb Smirnoff 	    sizeof(*iohead) * min(hd, req->pio_nheads));
635b252313fSGleb Smirnoff 	if (error == 0)
636b252313fSGleb Smirnoff 		error = copyout(iohook, req->pio_hooks,
637b252313fSGleb Smirnoff 		    sizeof(*iohook) * min(req->pio_nhooks, hk));
638b252313fSGleb Smirnoff 
639b252313fSGleb Smirnoff 	req->pio_nheads = hd;
640b252313fSGleb Smirnoff 	req->pio_nhooks = hk;
641b252313fSGleb Smirnoff 
642b252313fSGleb Smirnoff 	free(iohead, M_TEMP);
643b252313fSGleb Smirnoff 	free(iohook, M_TEMP);
644b252313fSGleb Smirnoff 
645b252313fSGleb Smirnoff 	return (error);
646b252313fSGleb Smirnoff }
647b252313fSGleb Smirnoff 
648b252313fSGleb Smirnoff static int
649b252313fSGleb Smirnoff pfilioc_listhooks(struct pfilioc_list *req)
650b252313fSGleb Smirnoff {
651b252313fSGleb Smirnoff 	struct pfil_hook *hook;
652b252313fSGleb Smirnoff 	struct pfilioc_hook *iohook;
653b252313fSGleb Smirnoff 	u_int nhooks, hk;
654b252313fSGleb Smirnoff 	int error;
655b252313fSGleb Smirnoff 
656b252313fSGleb Smirnoff 	PFIL_LOCK();
657b252313fSGleb Smirnoff restart:
658b252313fSGleb Smirnoff 	nhooks = 0;
659b252313fSGleb Smirnoff 	LIST_FOREACH(hook, &V_pfil_hook_list, hook_list)
660b252313fSGleb Smirnoff 		nhooks++;
661b252313fSGleb Smirnoff 	PFIL_UNLOCK();
662b252313fSGleb Smirnoff 
663b252313fSGleb Smirnoff 	if (req->pio_nhooks < nhooks) {
664b252313fSGleb Smirnoff 		req->pio_nhooks = nhooks;
665b252313fSGleb Smirnoff 		return (0);
666b252313fSGleb Smirnoff 	}
667b252313fSGleb Smirnoff 
668b252313fSGleb Smirnoff 	iohook = malloc(sizeof(*iohook) * nhooks, M_TEMP, M_WAITOK);
669b252313fSGleb Smirnoff 
670b252313fSGleb Smirnoff 	hk = 0;
671b252313fSGleb Smirnoff 	PFIL_LOCK();
672b252313fSGleb Smirnoff 	LIST_FOREACH(hook, &V_pfil_hook_list, hook_list) {
673b252313fSGleb Smirnoff 		if (hk + 1 > nhooks) {
674b252313fSGleb Smirnoff 			/* Configuration changed during malloc(). */
675b252313fSGleb Smirnoff 			free(iohook, M_TEMP);
676b252313fSGleb Smirnoff 			goto restart;
677b252313fSGleb Smirnoff 		}
678b252313fSGleb Smirnoff 		strlcpy(iohook[hk].pio_module, hook->hook_modname,
679b252313fSGleb Smirnoff 		    sizeof(iohook[0].pio_module));
680b252313fSGleb Smirnoff 		strlcpy(iohook[hk].pio_ruleset, hook->hook_rulname,
681b252313fSGleb Smirnoff 		    sizeof(iohook[0].pio_ruleset));
682b252313fSGleb Smirnoff 		iohook[hk].pio_type = hook->hook_type;
683b252313fSGleb Smirnoff 		iohook[hk].pio_flags = hook->hook_flags;
684b252313fSGleb Smirnoff 		hk++;
685b252313fSGleb Smirnoff 	}
686b252313fSGleb Smirnoff 	PFIL_UNLOCK();
687b252313fSGleb Smirnoff 
688b252313fSGleb Smirnoff 	error = copyout(iohook, req->pio_hooks,
689b252313fSGleb Smirnoff 	    sizeof(*iohook) * min(req->pio_nhooks, hk));
690b252313fSGleb Smirnoff 	req->pio_nhooks = hk;
691b252313fSGleb Smirnoff 	free(iohook, M_TEMP);
692b252313fSGleb Smirnoff 
693b252313fSGleb Smirnoff 	return (error);
694b252313fSGleb Smirnoff }
695b252313fSGleb Smirnoff 
696b252313fSGleb Smirnoff static int
697b252313fSGleb Smirnoff pfilioc_link(struct pfilioc_link *req)
698b252313fSGleb Smirnoff {
699b252313fSGleb Smirnoff 	struct pfil_link_args args;
700b252313fSGleb Smirnoff 
701b252313fSGleb Smirnoff 	if (req->pio_flags & ~(PFIL_IN | PFIL_OUT | PFIL_UNLINK | PFIL_APPEND))
702b252313fSGleb Smirnoff 		return (EINVAL);
703b252313fSGleb Smirnoff 
704b252313fSGleb Smirnoff 	args.pa_version = PFIL_VERSION;
705b252313fSGleb Smirnoff 	args.pa_flags = req->pio_flags;
706b252313fSGleb Smirnoff 	args.pa_headname = req->pio_name;
707b252313fSGleb Smirnoff 	args.pa_modname = req->pio_module;
708b252313fSGleb Smirnoff 	args.pa_rulname = req->pio_ruleset;
709b252313fSGleb Smirnoff 
710b252313fSGleb Smirnoff 	return (pfil_link(&args));
711b252313fSGleb Smirnoff }
712