xref: /dragonfly/sys/kern/uipc_domain.c (revision 0db87cb7)
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. 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  * $FreeBSD: src/sys/kern/uipc_domain.c,v 1.22.2.1 2001/07/03 11:01:37 ume Exp $
31  */
32 
33 #include <sys/param.h>
34 #include <sys/socket.h>
35 #include <sys/protosw.h>
36 #include <sys/domain.h>
37 #include <sys/mbuf.h>
38 #include <sys/kernel.h>
39 #include <sys/socketvar.h>
40 #include <sys/socketops.h>
41 #include <sys/systm.h>
42 #include <vm/vm_zone.h>
43 
44 #include <sys/thread2.h>
45 
46 /*
47  * System initialization
48  *
49  * Note: domain initialization wants to take place on a per domain basis
50  * as a result of traversing a linker set.  Most likely, each domain
51  * want to call a registration function rather than being handled here
52  * in domaininit().  Probably this will look like:
53  *
54  * SYSINIT(unique, SI_SUB_PROTO_DOMAIN, SI_ORDER_ANY, domain_add, xxx);
55  *
56  * Where 'xxx' is replaced by the address of a parameter struct to be
57  * passed to the doamin_add() function.
58  */
59 
60 static void domaininit (void *);
61 SYSINIT(domain, SI_SUB_PROTO_DOMAIN, SI_ORDER_FIRST, domaininit, NULL);
62 
63 static void	pffasttimo (void *);
64 static void	pfslowtimo (void *);
65 
66 struct domainlist domains;
67 
68 static struct callout pffasttimo_ch;
69 static struct callout pfslowtimo_ch;
70 
71 /*
72  * Add a new protocol domain to the list of supported domains
73  * Note: you cant unload it again because a socket may be using it.
74  * XXX can't fail at this time.
75  */
76 #define PR_NOTSUPP(pr, label)		\
77 	if (pr->pr_ ## label == NULL)	\
78 		pr->pr_ ## label = pr_generic_notsupp;
79 
80 #define PRU_NOTSUPP(pu, label)		\
81 	if (pu->pru_ ## label == NULL)	\
82 		pu->pru_ ## label = pr_generic_notsupp;
83 
84 static void
85 net_init_domain(struct domain *dp)
86 {
87 	struct protosw *pr;
88 	struct pr_usrreqs *pu;
89 
90 	crit_enter();
91 
92 	if (dp->dom_init)
93 		(*dp->dom_init)();
94 
95 	for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
96 		pu = pr->pr_usrreqs;
97 		if (pu == NULL) {
98 			panic("domaininit: %ssw[%ld] has no usrreqs!",
99 			      dp->dom_name, (long)(pr - dp->dom_protosw));
100 		}
101 		PR_NOTSUPP(pr, ctloutput);
102 		PRU_NOTSUPP(pu, accept);
103 		PRU_NOTSUPP(pu, bind);
104 		PRU_NOTSUPP(pu, connect);
105 		PRU_NOTSUPP(pu, connect2);
106 		PRU_NOTSUPP(pu, control);
107 		PRU_NOTSUPP(pu, disconnect);
108 		PRU_NOTSUPP(pu, listen);
109 		PRU_NOTSUPP(pu, peeraddr);
110 		PRU_NOTSUPP(pu, rcvd);
111 		PRU_NOTSUPP(pu, rcvoob);
112 		PRU_NOTSUPP(pu, shutdown);
113 		PRU_NOTSUPP(pu, sockaddr);
114 
115 		if (pu->pru_sense == NULL)
116 			pu->pru_sense = pru_sense_null;
117 		if (pu->pru_sosend == NULL)
118 			pu->pru_sosend = pru_sosend_notsupp;
119 		if (pu->pru_soreceive == NULL)
120 			pu->pru_soreceive = pru_soreceive_notsupp;
121 
122 		if (pr->pr_init)
123 			(*pr->pr_init)();
124 	}
125 	/*
126 	 * update global information about maximums
127 	 */
128 	max_hdr = max_linkhdr + max_protohdr;
129 	max_datalen = MHLEN - max_hdr;
130 	crit_exit();
131 }
132 
133 /*
134  * Add a new protocol domain to the list of supported domains
135  * Note: you cant unload it again because a socket may be using it.
136  * XXX can't fail at this time.
137  */
138 void
139 net_add_domain(void *data)
140 {
141 	struct domain *dp = data;
142 
143 	crit_enter();
144 	SLIST_INSERT_HEAD(&domains, dp, dom_next);
145 	crit_exit();
146 	net_init_domain(dp);
147 }
148 
149 /* ARGSUSED*/
150 static void
151 domaininit(void *dummy)
152 {
153 	if (max_linkhdr < 20)		/* XXX */
154 		max_linkhdr = 20;
155 
156 	callout_init_mp(&pffasttimo_ch);
157 	callout_init_mp(&pfslowtimo_ch);
158 	callout_reset(&pffasttimo_ch, 1, pffasttimo, NULL);
159 	callout_reset(&pfslowtimo_ch, 1, pfslowtimo, NULL);
160 }
161 
162 
163 struct protosw *
164 pffindtype(int family, int type)
165 {
166 	struct domain *dp;
167 	struct protosw *pr;
168 
169 	SLIST_FOREACH(dp, &domains, dom_next)
170 		if (dp->dom_family == family)
171 			goto found;
172 	return (NULL);
173 
174 found:
175 	for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
176 		if (pr->pr_type && pr->pr_type == type)
177 			return (pr);
178 	return (NULL);
179 }
180 
181 struct protosw *
182 pffindproto(int family, int protocol, int type)
183 {
184 	struct domain *dp;
185 	struct protosw *pr;
186 	struct protosw *maybe = NULL;
187 
188 	if (family == 0)
189 		return (NULL);
190 	SLIST_FOREACH(dp, &domains, dom_next)
191 		if (dp->dom_family == family)
192 			goto found;
193 	return (NULL);
194 
195 found:
196 	for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
197 		if ((pr->pr_protocol == protocol) && (pr->pr_type == type))
198 			return (pr);
199 
200 		if (type == SOCK_RAW && pr->pr_type == SOCK_RAW &&
201 		    pr->pr_protocol == 0 && maybe == NULL)
202 			maybe = pr;
203 	}
204 	return (maybe);
205 }
206 
207 void
208 kpfctlinput(int cmd, struct sockaddr *sa)
209 {
210 	struct domain *dp;
211 	struct protosw *pr;
212 
213 	SLIST_FOREACH(dp, &domains, dom_next) {
214 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
215 			so_pr_ctlinput(pr, cmd, sa, NULL);
216 	}
217 }
218 
219 void
220 kpfctlinput_direct(int cmd, struct sockaddr *sa)
221 {
222 	struct domain *dp;
223 	struct protosw *pr;
224 
225 	SLIST_FOREACH(dp, &domains, dom_next) {
226 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
227 			so_pr_ctlinput_direct(pr, cmd, sa, NULL);
228 	}
229 }
230 
231 void
232 kpfctlinput2(int cmd, struct sockaddr *sa, void *ctlparam)
233 {
234 	struct domain *dp;
235 	struct protosw *pr;
236 
237 	if (!sa)
238 		return;
239 	SLIST_FOREACH(dp, &domains, dom_next) {
240 		/*
241 		 * the check must be made by xx_ctlinput() anyways, to
242 		 * make sure we use data item pointed to by ctlparam in
243 		 * correct way.  the following check is made just for safety.
244 		 */
245 		if (dp->dom_family != sa->sa_family)
246 			continue;
247 
248 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
249 			so_pr_ctlinput(pr, cmd, sa, ctlparam);
250 	}
251 }
252 
253 static void
254 pfslowtimo(void *arg)
255 {
256 	struct domain *dp;
257 	struct protosw *pr;
258 
259 	SLIST_FOREACH(dp, &domains, dom_next) {
260 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
261 			if (pr->pr_slowtimo)
262 				(*pr->pr_slowtimo)();
263 	}
264 	callout_reset(&pfslowtimo_ch, hz / 2, pfslowtimo, NULL);
265 }
266 
267 static void
268 pffasttimo(void *arg)
269 {
270 	struct domain *dp;
271 	struct protosw *pr;
272 
273 	SLIST_FOREACH(dp, &domains, dom_next) {
274 		for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
275 			if (pr->pr_fasttimo)
276 				(*pr->pr_fasttimo)();
277 	}
278 	callout_reset(&pffasttimo_ch, hz / 5, pffasttimo, NULL);
279 }
280