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