xref: /dragonfly/sys/kern/uipc_domain.c (revision bc3d4063)
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  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *	This product includes software developed by the University of
16  *	California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *	@(#)uipc_domain.c	8.2 (Berkeley) 10/18/93
34  * $FreeBSD: src/sys/kern/uipc_domain.c,v 1.22.2.1 2001/07/03 11:01:37 ume Exp $
35  * $DragonFly: src/sys/kern/uipc_domain.c,v 1.12 2008/08/15 17:37:29 nth Exp $
36  */
37 
38 #include <sys/param.h>
39 #include <sys/socket.h>
40 #include <sys/protosw.h>
41 #include <sys/domain.h>
42 #include <sys/mbuf.h>
43 #include <sys/kernel.h>
44 #include <sys/socketvar.h>
45 #include <sys/systm.h>
46 #include <vm/vm_zone.h>
47 
48 #include <sys/thread2.h>
49 
50 /*
51  * System initialization
52  *
53  * Note: domain initialization wants to take place on a per domain basis
54  * as a result of traversing a linker set.  Most likely, each domain
55  * want to call a registration function rather than being handled here
56  * in domaininit().  Probably this will look like:
57  *
58  * SYSINIT(unique, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, domain_add, xxx)
59  *
60  * Where 'xxx' is replaced by the address of a parameter struct to be
61  * passed to the doamin_add() function.
62  */
63 
64 static void domaininit (void *);
65 SYSINIT(domain, SI_SUB_PROTO_DOMAIN, SI_ORDER_FIRST, domaininit, NULL)
66 
67 static void	pffasttimo (void *);
68 static void	pfslowtimo (void *);
69 
70 struct domainlist domains;
71 
72 static struct callout pffasttimo_ch;
73 static struct callout pfslowtimo_ch;
74 
75 /*
76  * Add a new protocol domain to the list of supported domains
77  * Note: you cant unload it again because a socket may be using it.
78  * XXX can't fail at this time.
79  */
80 static void
81 net_init_domain(struct domain *dp)
82 {
83 	struct protosw *pr;
84 
85 	crit_enter();
86 	if (dp->dom_init)
87 		(*dp->dom_init)();
88 	for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
89 		if (pr->pr_usrreqs == NULL)
90 			panic("domaininit: %ssw[%d] has no usrreqs!",
91 			      dp->dom_name, pr - dp->dom_protosw);
92 		if (pr->pr_init)
93 			(*pr->pr_init)();
94 	}
95 	/*
96 	 * update global information about maximums
97 	 */
98 	max_hdr = max_linkhdr + max_protohdr;
99 	max_datalen = MHLEN - max_hdr;
100 	crit_exit();
101 }
102 
103 /*
104  * Add a new protocol domain to the list of supported domains
105  * Note: you cant unload it again because a socket may be using it.
106  * XXX can't fail at this time.
107  */
108 void
109 net_add_domain(void *data)
110 {
111 	struct domain *dp = data;
112 
113 	crit_enter();
114 	SLIST_INSERT_HEAD(&domains, dp, dom_next);
115 	crit_exit();
116 	net_init_domain(dp);
117 }
118 
119 /* ARGSUSED*/
120 static void
121 domaininit(void *dummy)
122 {
123 	if (max_linkhdr < 16)		/* XXX */
124 		max_linkhdr = 16;
125 
126 	callout_init(&pffasttimo_ch);
127 	callout_init(&pfslowtimo_ch);
128 	callout_reset(&pffasttimo_ch, 1, pffasttimo, NULL);
129 	callout_reset(&pfslowtimo_ch, 1, pfslowtimo, NULL);
130 }
131 
132 
133 struct protosw *
134 pffindtype(int family, int type)
135 {
136 	struct domain *dp;
137 	struct protosw *pr;
138 
139 	SLIST_FOREACH(dp, &domains, dom_next)
140 		if (dp->dom_family == family)
141 			goto found;
142 	return (NULL);
143 
144 found:
145 	for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
146 		if (pr->pr_type && pr->pr_type == type)
147 			return (pr);
148 	return (NULL);
149 }
150 
151 struct protosw *
152 pffindproto(int family, int protocol, int type)
153 {
154 	struct domain *dp;
155 	struct protosw *pr;
156 	struct protosw *maybe = NULL;
157 
158 	if (family == 0)
159 		return (NULL);
160 	SLIST_FOREACH(dp, &domains, dom_next)
161 		if (dp->dom_family == family)
162 			goto found;
163 	return (NULL);
164 
165 found:
166 	for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
167 		if ((pr->pr_protocol == protocol) && (pr->pr_type == type))
168 			return (pr);
169 
170 		if (type == SOCK_RAW && pr->pr_type == SOCK_RAW &&
171 		    pr->pr_protocol == 0 && maybe == (struct protosw *)NULL)
172 			maybe = pr;
173 	}
174 	return (maybe);
175 }
176 
177 void
178 kpfctlinput(int cmd, struct sockaddr *sa)
179 {
180 	struct domain *dp;
181 	struct protosw *pr;
182 
183 	SLIST_FOREACH(dp, &domains, dom_next)
184 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
185 			if (pr->pr_ctlinput)
186 				(*pr->pr_ctlinput)(cmd, sa, NULL);
187 }
188 
189 void
190 kpfctlinput2(int cmd, struct sockaddr *sa, void *ctlparam)
191 {
192 	struct domain *dp;
193 	struct protosw *pr;
194 
195 	if (!sa)
196 		return;
197 	SLIST_FOREACH(dp, &domains, dom_next) {
198 		/*
199 		 * the check must be made by xx_ctlinput() anyways, to
200 		 * make sure we use data item pointed to by ctlparam in
201 		 * correct way.  the following check is made just for safety.
202 		 */
203 		if (dp->dom_family != sa->sa_family)
204 			continue;
205 
206 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
207 			if (pr->pr_ctlinput)
208 				(*pr->pr_ctlinput)(cmd, sa, ctlparam);
209 	}
210 }
211 
212 static void
213 pfslowtimo(void *arg)
214 {
215 	struct domain *dp;
216 	struct protosw *pr;
217 
218 	SLIST_FOREACH(dp, &domains, dom_next)
219 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
220 			if (pr->pr_slowtimo)
221 				(*pr->pr_slowtimo)();
222 	callout_reset(&pfslowtimo_ch, hz / 2, pfslowtimo, NULL);
223 }
224 
225 static void
226 pffasttimo(void *arg)
227 {
228 	struct domain *dp;
229 	struct protosw *pr;
230 
231 	SLIST_FOREACH(dp, &domains, dom_next)
232 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
233 			if (pr->pr_fasttimo)
234 				(*pr->pr_fasttimo)();
235 	callout_reset(&pffasttimo_ch, hz / 5, pffasttimo, NULL);
236 }
237