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