uipc_accf.c revision 77544
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: head/sys/kern/uipc_accf.c 77544 2001-05-31 21:56:44Z jesper $
28 */
29
30#define ACCEPT_FILTER_MOD
31
32#include <sys/param.h>
33#include <sys/systm.h>
34#include <sys/domain.h>
35#include <sys/kernel.h>
36#include <sys/malloc.h>
37#include <sys/mbuf.h>
38#include <sys/protosw.h>
39#include <sys/sysctl.h>
40#include <sys/socket.h>
41#include <sys/socketvar.h>
42#include <sys/queue.h>
43
44static SLIST_HEAD(, accept_filter) accept_filtlsthd =
45	SLIST_HEAD_INITIALIZER(&accept_filtlsthd);
46
47MALLOC_DEFINE(M_ACCF, "accf", "accept filter data");
48
49static int unloadable = 0;
50
51SYSCTL_DECL(_net_inet);	/* XXX: some header should do this for me */
52SYSCTL_NODE(_net_inet, OID_AUTO, accf, CTLFLAG_RW, 0, "Accept filters");
53SYSCTL_INT(_net_inet_accf, OID_AUTO, unloadable, CTLFLAG_RW, &unloadable, 0,
54	"Allow unload of accept filters (not recommended)");
55
56/*
57 * must be passed a malloc'd structure so we don't explode if the kld
58 * is unloaded, we leak the struct on deallocation to deal with this,
59 * but if a filter is loaded with the same name as a leaked one we re-use
60 * the entry.
61 */
62int
63accept_filt_add(struct accept_filter *filt)
64{
65	struct accept_filter *p;
66
67	SLIST_FOREACH(p, &accept_filtlsthd, accf_next)
68		if (strcmp(p->accf_name, filt->accf_name) == 0)  {
69			if (p->accf_callback != NULL) {
70				return (EEXIST);
71			} else {
72				p->accf_callback = filt->accf_callback;
73				FREE(filt, M_ACCF);
74				return (0);
75			}
76		}
77
78	if (p == NULL)
79		SLIST_INSERT_HEAD(&accept_filtlsthd, filt, accf_next);
80	return (0);
81}
82
83int
84accept_filt_del(char *name)
85{
86	struct accept_filter *p;
87
88	p = accept_filt_get(name);
89	if (p == NULL)
90		return (ENOENT);
91
92	p->accf_callback = NULL;
93	return (0);
94}
95
96struct accept_filter *
97accept_filt_get(char *name)
98{
99	struct accept_filter *p;
100
101	SLIST_FOREACH(p, &accept_filtlsthd, accf_next)
102		if (strcmp(p->accf_name, name) == 0)
103			return (p);
104
105	return (NULL);
106}
107
108int
109accept_filt_generic_mod_event(module_t mod, int event, void *data)
110{
111	struct accept_filter *p;
112	struct accept_filter *accfp = (struct accept_filter *) data;
113	int	s, error;
114
115	switch (event) {
116	case MOD_LOAD:
117		MALLOC(p, struct accept_filter *, sizeof(*p), M_ACCF, M_WAITOK);
118		bcopy(accfp, p, sizeof(*p));
119		s = splnet();
120		error = accept_filt_add(p);
121		splx(s);
122		break;
123
124	case MOD_UNLOAD:
125		/*
126		 * Do not support unloading yet. we don't keep track of refcounts
127		 * and unloading an accept filter callback and then having it called
128		 * is a bad thing.  A simple fix would be to track the refcount
129		 * in the struct accept_filter.
130		 */
131		if (unloadable != 0) {
132			s = splnet();
133			error = accept_filt_del(accfp->accf_name);
134			splx(s);
135		} else
136			error = EOPNOTSUPP;
137		break;
138
139	case MOD_SHUTDOWN:
140		error = 0;
141		break;
142
143	default:
144		error = EOPNOTSUPP;
145		break;
146	}
147
148	return (error);
149}
150