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