Disallow writes to filesystems mounted read-only via NULLFS. In this case
[dragonfly.git] / sys / kern / uipc_domain.c
CommitLineData
984263bc
MD
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 $
e43a034f 35 * $DragonFly: src/sys/kern/uipc_domain.c,v 1.9 2005/06/06 15:02:28 dillon Exp $
984263bc
MD
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
e43a034f
MD
48#include <sys/thread2.h>
49
984263bc
MD
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
402ed7e1 64static void domaininit (void *);
984263bc
MD
65SYSINIT(domain, SI_SUB_PROTO_DOMAIN, SI_ORDER_FIRST, domaininit, NULL)
66
402ed7e1
RG
67static void pffasttimo (void *);
68static void pfslowtimo (void *);
984263bc 69
9c70fe43 70struct domainlist domains;
984263bc 71
a330847e
JS
72static struct callout pffasttimo_ch;
73static struct callout pfslowtimo_ch;
74
984263bc
MD
75/*
76 * Add a new protocol domain to the list of supported domains
9c70fe43 77 * Note: you cant unload it again because a socket may be using it.
984263bc
MD
78 * XXX can't fail at this time.
79 */
80static void
81net_init_domain(struct domain *dp)
82{
1fd87d54 83 struct protosw *pr;
984263bc 84
e43a034f 85 crit_enter();
984263bc
MD
86 if (dp->dom_init)
87 (*dp->dom_init)();
517e1666 88 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
5fe66e68 89 if (pr->pr_usrreqs == NULL)
984263bc 90 panic("domaininit: %ssw[%d] has no usrreqs!",
5fe66e68 91 dp->dom_name, pr - dp->dom_protosw);
984263bc
MD
92 if (pr->pr_init)
93 (*pr->pr_init)();
94 }
95 /*
9c70fe43 96 * update global information about maximums
984263bc
MD
97 */
98 max_hdr = max_linkhdr + max_protohdr;
99 max_datalen = MHLEN - max_hdr;
e43a034f 100 crit_exit();
984263bc
MD
101}
102
103/*
104 * Add a new protocol domain to the list of supported domains
9c70fe43 105 * Note: you cant unload it again because a socket may be using it.
984263bc
MD
106 * XXX can't fail at this time.
107 */
108void
109net_add_domain(void *data)
110{
9c70fe43 111 struct domain *dp = data;
984263bc 112
e43a034f 113 crit_enter();
9c70fe43 114 SLIST_INSERT_HEAD(&domains, dp, dom_next);
e43a034f 115 crit_exit();
984263bc
MD
116 net_init_domain(dp);
117}
118
119/* ARGSUSED*/
120static void
121domaininit(void *dummy)
122{
123 /*
124 * Before we do any setup, make sure to initialize the
125 * zone allocator we get struct sockets from. The obvious
126 * maximum number of sockets is `maxfiles', but it is possible
127 * to have a socket without an open file (e.g., a connection waiting
128 * to be accept(2)ed). Rather than think up and define a
129 * better value, we just use nmbclusters, since that's what people
130 * are told to increase first when the network runs out of memory.
131 * Perhaps we should have two pools, one of unlimited size
132 * for use during socreate(), and one ZONE_INTERRUPT pool for
133 * use in sonewconn().
134 */
135 socket_zone = zinit("socket", sizeof(struct socket), maxsockets,
136 ZONE_INTERRUPT, 0);
137
138 if (max_linkhdr < 16) /* XXX */
139 max_linkhdr = 16;
140
a330847e
JS
141 callout_init(&pffasttimo_ch);
142 callout_init(&pfslowtimo_ch);
143 callout_reset(&pffasttimo_ch, 1, pffasttimo, NULL);
144 callout_reset(&pfslowtimo_ch, 1, pfslowtimo, NULL);
984263bc
MD
145}
146
147
148struct protosw *
5fe66e68 149pffindtype(int family, int type)
984263bc 150{
1fd87d54
RG
151 struct domain *dp;
152 struct protosw *pr;
984263bc 153
9c70fe43 154 SLIST_FOREACH(dp, &domains, dom_next)
984263bc
MD
155 if (dp->dom_family == family)
156 goto found;
5fe66e68
JH
157 return (NULL);
158
984263bc
MD
159found:
160 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
161 if (pr->pr_type && pr->pr_type == type)
162 return (pr);
5fe66e68 163 return (NULL);
984263bc
MD
164}
165
166struct protosw *
5fe66e68 167pffindproto(int family, int protocol, int type)
984263bc 168{
1fd87d54
RG
169 struct domain *dp;
170 struct protosw *pr;
5fe66e68 171 struct protosw *maybe = NULL;
984263bc
MD
172
173 if (family == 0)
5fe66e68 174 return (NULL);
9c70fe43 175 SLIST_FOREACH(dp, &domains, dom_next)
984263bc
MD
176 if (dp->dom_family == family)
177 goto found;
5fe66e68
JH
178 return (NULL);
179
984263bc
MD
180found:
181 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
182 if ((pr->pr_protocol == protocol) && (pr->pr_type == type))
183 return (pr);
184
185 if (type == SOCK_RAW && pr->pr_type == SOCK_RAW &&
5fe66e68 186 pr->pr_protocol == 0 && maybe == (struct protosw *)NULL)
984263bc
MD
187 maybe = pr;
188 }
189 return (maybe);
190}
191
192void
5fe66e68 193pfctlinput(int cmd, struct sockaddr *sa)
984263bc 194{
1fd87d54
RG
195 struct domain *dp;
196 struct protosw *pr;
984263bc 197
9c70fe43 198 SLIST_FOREACH(dp, &domains, dom_next)
984263bc
MD
199 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
200 if (pr->pr_ctlinput)
5fe66e68 201 (*pr->pr_ctlinput)(cmd, sa, (void *)NULL);
984263bc
MD
202}
203
204void
5fe66e68 205pfctlinput2(int cmd, struct sockaddr *sa, void *ctlparam)
984263bc
MD
206{
207 struct domain *dp;
208 struct protosw *pr;
209
210 if (!sa)
211 return;
9c70fe43 212 SLIST_FOREACH(dp, &domains, dom_next) {
984263bc
MD
213 /*
214 * the check must be made by xx_ctlinput() anyways, to
215 * make sure we use data item pointed to by ctlparam in
216 * correct way. the following check is made just for safety.
217 */
218 if (dp->dom_family != sa->sa_family)
219 continue;
220
221 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
222 if (pr->pr_ctlinput)
223 (*pr->pr_ctlinput)(cmd, sa, ctlparam);
224 }
225}
226
227static void
5fe66e68 228pfslowtimo(void *arg)
984263bc 229{
1fd87d54
RG
230 struct domain *dp;
231 struct protosw *pr;
984263bc 232
9c70fe43 233 SLIST_FOREACH(dp, &domains, dom_next)
984263bc
MD
234 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
235 if (pr->pr_slowtimo)
236 (*pr->pr_slowtimo)();
a330847e 237 callout_reset(&pfslowtimo_ch, hz / 2, pfslowtimo, NULL);
984263bc
MD
238}
239
240static void
5fe66e68 241pffasttimo(void *arg)
984263bc 242{
1fd87d54
RG
243 struct domain *dp;
244 struct protosw *pr;
984263bc 245
9c70fe43 246 SLIST_FOREACH(dp, &domains, dom_next)
984263bc
MD
247 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
248 if (pr->pr_fasttimo)
249 (*pr->pr_fasttimo)();
a330847e 250 callout_reset(&pffasttimo_ch, hz / 5, pffasttimo, NULL);
984263bc 251}