uipc_domain.c revision 137386
1/*
2 * Copyright (c) 1982, 1986, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 *    may be used to endorse or promote products derived from this software
15 *    without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 *	@(#)uipc_domain.c	8.2 (Berkeley) 10/18/93
30 */
31
32#include <sys/cdefs.h>
33__FBSDID("$FreeBSD: head/sys/kern/uipc_domain.c 137386 2004-11-08 14:44:54Z phk $");
34
35#include <sys/param.h>
36#include <sys/socket.h>
37#include <sys/protosw.h>
38#include <sys/domain.h>
39#include <sys/mbuf.h>
40#include <sys/kernel.h>
41#include <sys/lock.h>
42#include <sys/mutex.h>
43#include <sys/socketvar.h>
44#include <sys/systm.h>
45#include <vm/uma.h>
46
47/*
48 * System initialization
49 *
50 * Note: domain initialization takes place on a per domain basis
51 * as a result of traversing a SYSINIT linker set.  Most likely,
52 * each domain would want to call DOMAIN_SET(9) itself, which
53 * would cause the domain to be added just after domaininit()
54 * is called during startup.
55 *
56 * See DOMAIN_SET(9) for details on its use.
57 */
58
59static void domaininit(void *);
60SYSINIT(domain, SI_SUB_PROTO_DOMAIN, SI_ORDER_FIRST, domaininit, NULL)
61
62static struct callout pffast_callout;
63static struct callout pfslow_callout;
64
65static void	pffasttimo(void *);
66static void	pfslowtimo(void *);
67
68struct domain *domains;		/* registered protocol domains */
69struct mtx dom_mtx;		/* domain list lock */
70MTX_SYSINIT(domain, &dom_mtx, "domain list", MTX_DEF);
71
72/*
73 * Dummy protocol specific user requests function pointer array.
74 * All functions return EOPNOTSUPP.
75 */
76struct pr_usrreqs nousrreqs = {
77	.pru_abort =		pru_abort_notsupp,
78	.pru_accept =		pru_accept_notsupp,
79	.pru_attach =		pru_attach_notsupp,
80	.pru_bind =		pru_bind_notsupp,
81	.pru_connect =		pru_connect_notsupp,
82	.pru_connect2 =		pru_connect2_notsupp,
83	.pru_control =		pru_control_notsupp,
84	.pru_detach =		pru_detach_notsupp,
85	.pru_disconnect	=	pru_disconnect_notsupp,
86	.pru_listen =		pru_listen_notsupp,
87	.pru_peeraddr =		pru_peeraddr_notsupp,
88	.pru_rcvd =		pru_rcvd_notsupp,
89	.pru_rcvoob =		pru_rcvoob_notsupp,
90	.pru_send =		pru_send_notsupp,
91	.pru_sense =		pru_sense_null,
92	.pru_shutdown =		pru_shutdown_notsupp,
93	.pru_sockaddr =		pru_sockaddr_notsupp,
94	.pru_sosend =		pru_sosend_notsupp,
95	.pru_soreceive =	pru_soreceive_notsupp,
96	.pru_sopoll =		pru_sopoll_notsupp,
97	.pru_sosetlabel =	pru_sosetlabel_null
98};
99
100/*
101 * Add a new protocol domain to the list of supported domains
102 * Note: you cant unload it again because a socket may be using it.
103 * XXX can't fail at this time.
104 */
105static void
106net_init_domain(struct domain *dp)
107{
108	struct protosw *pr;
109	struct pr_usrreqs *pu;
110
111	if (dp->dom_init)
112		(*dp->dom_init)();
113	for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++){
114		pu = pr->pr_usrreqs;
115		KASSERT(pu != NULL,
116		    ("domaininit: %ssw[%d] has no usrreqs!",
117		    dp->dom_name, (int)(pr - dp->dom_protosw)));
118#define DEFAULT(foo, bar)	if ((foo) == NULL)  (foo) = (bar)
119		DEFAULT(pu->pru_accept, pru_accept_notsupp);
120		DEFAULT(pu->pru_connect, pru_connect_notsupp);
121		DEFAULT(pu->pru_connect2, pru_connect2_notsupp);
122		DEFAULT(pu->pru_control, pru_control_notsupp);
123		DEFAULT(pu->pru_listen, pru_listen_notsupp);
124		DEFAULT(pu->pru_rcvd, pru_rcvd_notsupp);
125		DEFAULT(pu->pru_rcvoob, pru_rcvoob_notsupp);
126		DEFAULT(pu->pru_sense, pru_sense_null);
127		DEFAULT(pu->pru_sosend, sosend);
128		DEFAULT(pu->pru_soreceive, soreceive);
129		DEFAULT(pu->pru_sopoll, sopoll);
130		DEFAULT(pu->pru_sosetlabel, pru_sosetlabel_null);
131#undef DEFAULT
132
133		if (pr->pr_init)
134			(*pr->pr_init)();
135	}
136	/*
137	 * update global information about maximums
138	 */
139	max_hdr = max_linkhdr + max_protohdr;
140	max_datalen = MHLEN - max_hdr;
141}
142
143/*
144 * Add a new protocol domain to the list of supported domains
145 * Note: you cant unload it again because a socket may be using it.
146 * XXX can't fail at this time.
147 */
148void
149net_add_domain(void *data)
150{
151	struct domain *dp;
152
153	dp = (struct domain *)data;
154	mtx_lock(&dom_mtx);
155	dp->dom_next = domains;
156	domains = dp;
157	mtx_unlock(&dom_mtx);
158	net_init_domain(dp);
159}
160
161/* ARGSUSED*/
162static void
163domaininit(void *dummy)
164{
165	/*
166	 * Before we do any setup, make sure to initialize the
167	 * zone allocator we get struct sockets from.
168	 */
169
170	socket_zone = uma_zcreate("socket", sizeof(struct socket), NULL, NULL,
171	    NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
172	uma_zone_set_max(socket_zone, maxsockets);
173
174	if (max_linkhdr < 16)		/* XXX */
175		max_linkhdr = 16;
176
177	if (debug_mpsafenet) {
178		callout_init(&pffast_callout, CALLOUT_MPSAFE);
179		callout_init(&pfslow_callout, CALLOUT_MPSAFE);
180	} else {
181		callout_init(&pffast_callout, 0);
182		callout_init(&pfslow_callout, 0);
183	}
184
185	callout_reset(&pffast_callout, 1, pffasttimo, NULL);
186	callout_reset(&pfslow_callout, 1, pfslowtimo, NULL);
187}
188
189
190struct protosw *
191pffindtype(family, type)
192	int family;
193	int type;
194{
195	register struct domain *dp;
196	register struct protosw *pr;
197
198	for (dp = domains; dp; dp = dp->dom_next)
199		if (dp->dom_family == family)
200			goto found;
201	return (0);
202found:
203	for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
204		if (pr->pr_type && pr->pr_type == type)
205			return (pr);
206	return (0);
207}
208
209struct protosw *
210pffindproto(family, protocol, type)
211	int family;
212	int protocol;
213	int type;
214{
215	register struct domain *dp;
216	register struct protosw *pr;
217	struct protosw *maybe = 0;
218
219	if (family == 0)
220		return (0);
221	for (dp = domains; dp; dp = dp->dom_next)
222		if (dp->dom_family == family)
223			goto found;
224	return (0);
225found:
226	for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
227		if ((pr->pr_protocol == protocol) && (pr->pr_type == type))
228			return (pr);
229
230		if (type == SOCK_RAW && pr->pr_type == SOCK_RAW &&
231		    pr->pr_protocol == 0 && maybe == (struct protosw *)0)
232			maybe = pr;
233	}
234	return (maybe);
235}
236
237/*
238 * The caller must make sure that the new protocol is fully set up and ready to
239 * accept requests before it is registered.
240 */
241int
242pf_proto_register(family, npr)
243	int family;
244	struct protosw *npr;
245{
246	struct domain *dp;
247	struct protosw *pr, *fpr;
248
249	/* Sanity checks. */
250	if (family == 0)
251		return (EPFNOSUPPORT);
252	if (npr->pr_type == 0)
253		return (EPROTOTYPE);
254	if (npr->pr_protocol == 0)
255		return (EPROTONOSUPPORT);
256	if (npr->pr_usrreqs == NULL)
257		return (ENXIO);
258
259	/* Try to find the specified domain based on the family. */
260	for (dp = domains; dp; dp = dp->dom_next)
261		if (dp->dom_family == family)
262			goto found;
263	return (EPFNOSUPPORT);
264
265found:
266	/* Initialize backpointer to struct domain. */
267	npr->pr_domain = dp;
268	fpr = NULL;
269
270	/*
271	 * Protect us against races when two protocol registrations for
272	 * the same protocol happen at the same time.
273	 */
274	mtx_lock(&Giant);
275
276	/* The new protocol must not yet exist. */
277	for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
278		if ((pr->pr_type == npr->pr_type) &&
279		    (pr->pr_protocol == npr->pr_protocol)) {
280			mtx_unlock(&Giant);
281			return (EEXIST);	/* XXX: Check only protocol? */
282		}
283		/* While here, remember the first free spacer. */
284		if ((fpr == NULL) && (pr->pr_protocol == PROTO_SPACER))
285			fpr = pr;
286	}
287
288	/* If no free spacer is found we can't add the new protocol. */
289	if (fpr == NULL) {
290		mtx_unlock(&Giant);
291		return (ENOMEM);
292	}
293
294	/* Copy the new struct protosw over the spacer. */
295	bcopy(npr, fpr, sizeof(*fpr));
296
297	/* Job is done, no more protection required. */
298	mtx_unlock(&Giant);
299
300	/* Initialize and activate the protocol. */
301	if (fpr->pr_init)
302		(fpr->pr_init)();
303
304	return (0);
305}
306
307/*
308 * The caller must make sure the protocol and its functions correctly shut down
309 * all sockets and release all locks and memory references.
310 */
311int
312pf_proto_unregister(family, protocol, type)
313	int family;
314	int protocol;
315	int type;
316{
317	struct domain *dp;
318	struct protosw *pr, *dpr;
319
320	/* Sanity checks. */
321	if (family == 0)
322		return (EPFNOSUPPORT);
323	if (protocol == 0)
324		return (EPROTONOSUPPORT);
325	if (type == 0)
326		return (EPROTOTYPE);
327
328	/* Try to find the specified domain based on the family type. */
329	for (dp = domains; dp; dp = dp->dom_next)
330		if (dp->dom_family == family)
331			goto found;
332	return (EPFNOSUPPORT);
333
334found:
335	dpr = NULL;
336
337	/* Lock out everyone else while we are manipulating the protosw. */
338	mtx_lock(&Giant);
339
340	/* The protocol must exist and only once. */
341	for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
342		if ((pr->pr_type == type) && (pr->pr_protocol == protocol)) {
343			if (dpr != NULL) {
344				mtx_unlock(&Giant);
345				return (EMLINK);   /* Should not happen! */
346			} else
347				dpr = pr;
348		}
349	}
350
351	/* Protocol does not exist. */
352	if (dpr == NULL) {
353		mtx_unlock(&Giant);
354		return (EPROTONOSUPPORT);
355	}
356
357	/* De-orbit the protocol and make the slot available again. */
358	dpr->pr_type = 0;
359	dpr->pr_domain = dp;
360	dpr->pr_protocol = PROTO_SPACER;
361	dpr->pr_flags = 0;
362	dpr->pr_input = NULL;
363	dpr->pr_output = NULL;
364	dpr->pr_ctlinput = NULL;
365	dpr->pr_ctloutput = NULL;
366	dpr->pr_ousrreq = NULL;
367	dpr->pr_init = NULL;
368	dpr->pr_fasttimo = NULL;
369	dpr->pr_slowtimo = NULL;
370	dpr->pr_drain = NULL;
371	dpr->pr_usrreqs = &nousrreqs;
372
373	/* Job is done, not more protection required. */
374	mtx_unlock(&Giant);
375
376	return (0);
377}
378
379void
380pfctlinput(cmd, sa)
381	int cmd;
382	struct sockaddr *sa;
383{
384	register struct domain *dp;
385	register struct protosw *pr;
386
387	for (dp = domains; dp; dp = dp->dom_next)
388		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
389			if (pr->pr_ctlinput)
390				(*pr->pr_ctlinput)(cmd, sa, (void *)0);
391}
392
393void
394pfctlinput2(cmd, sa, ctlparam)
395	int cmd;
396	struct sockaddr *sa;
397	void *ctlparam;
398{
399	struct domain *dp;
400	struct protosw *pr;
401
402	if (!sa)
403		return;
404	for (dp = domains; dp; dp = dp->dom_next) {
405		/*
406		 * the check must be made by xx_ctlinput() anyways, to
407		 * make sure we use data item pointed to by ctlparam in
408		 * correct way.  the following check is made just for safety.
409		 */
410		if (dp->dom_family != sa->sa_family)
411			continue;
412
413		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
414			if (pr->pr_ctlinput)
415				(*pr->pr_ctlinput)(cmd, sa, ctlparam);
416	}
417}
418
419static void
420pfslowtimo(arg)
421	void *arg;
422{
423	register struct domain *dp;
424	register struct protosw *pr;
425
426	NET_ASSERT_GIANT();
427
428	for (dp = domains; dp; dp = dp->dom_next)
429		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
430			if (pr->pr_slowtimo)
431				(*pr->pr_slowtimo)();
432	callout_reset(&pfslow_callout, hz/2, pfslowtimo, NULL);
433}
434
435static void
436pffasttimo(arg)
437	void *arg;
438{
439	register struct domain *dp;
440	register struct protosw *pr;
441
442	NET_ASSERT_GIANT();
443
444	for (dp = domains; dp; dp = dp->dom_next)
445		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
446			if (pr->pr_fasttimo)
447				(*pr->pr_fasttimo)();
448	callout_reset(&pffast_callout, hz/5, pffasttimo, NULL);
449}
450