Deleted Added
full compact
uipc_domain.c (160619) uipc_domain.c (169624)
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 * 4. 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 */
31
32#include <sys/cdefs.h>
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 * 4. 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 */
31
32#include <sys/cdefs.h>
33__FBSDID("$FreeBSD: head/sys/kern/uipc_domain.c 160619 2006-07-24 15:20:08Z rwatson $");
33__FBSDID("$FreeBSD: head/sys/kern/uipc_domain.c 169624 2007-05-16 20:41:08Z rwatson $");
34
35#include <sys/param.h>
36#include <sys/socket.h>
37#include <sys/protosw.h>
38#include <sys/domain.h>
39#include <sys/eventhandler.h>
40#include <sys/mbuf.h>
41#include <sys/kernel.h>
42#include <sys/lock.h>
43#include <sys/mutex.h>
44#include <sys/socketvar.h>
45#include <sys/systm.h>
46#include <vm/uma.h>
47
48/*
49 * System initialization
50 *
51 * Note: domain initialization takes place on a per domain basis
52 * as a result of traversing a SYSINIT linker set. Most likely,
53 * each domain would want to call DOMAIN_SET(9) itself, which
54 * would cause the domain to be added just after domaininit()
55 * is called during startup.
56 *
57 * See DOMAIN_SET(9) for details on its use.
58 */
59
60static void domaininit(void *);
61SYSINIT(domain, SI_SUB_PROTO_DOMAIN, SI_ORDER_FIRST, domaininit, NULL)
62
63static void domainfinalize(void *);
64SYSINIT(domainfin, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_FIRST, domainfinalize,
65 NULL)
66
67static struct callout pffast_callout;
68static struct callout pfslow_callout;
69
70static void pffasttimo(void *);
71static void pfslowtimo(void *);
72
73struct domain *domains; /* registered protocol domains */
74int domain_init_status = 0;
75struct mtx dom_mtx; /* domain list lock */
76MTX_SYSINIT(domain, &dom_mtx, "domain list", MTX_DEF);
77
78/*
79 * Dummy protocol specific user requests function pointer array.
80 * All functions return EOPNOTSUPP.
81 */
82struct pr_usrreqs nousrreqs = {
83 .pru_accept = pru_accept_notsupp,
84 .pru_attach = pru_attach_notsupp,
85 .pru_bind = pru_bind_notsupp,
86 .pru_connect = pru_connect_notsupp,
87 .pru_connect2 = pru_connect2_notsupp,
88 .pru_control = pru_control_notsupp,
89 .pru_disconnect = pru_disconnect_notsupp,
90 .pru_listen = pru_listen_notsupp,
91 .pru_peeraddr = pru_peeraddr_notsupp,
92 .pru_rcvd = pru_rcvd_notsupp,
93 .pru_rcvoob = pru_rcvoob_notsupp,
94 .pru_send = pru_send_notsupp,
95 .pru_sense = pru_sense_null,
96 .pru_shutdown = pru_shutdown_notsupp,
97 .pru_sockaddr = pru_sockaddr_notsupp,
98 .pru_sosend = pru_sosend_notsupp,
99 .pru_soreceive = pru_soreceive_notsupp,
100 .pru_sopoll = pru_sopoll_notsupp,
101};
102
103static void
104protosw_init(struct protosw *pr)
105{
106 struct pr_usrreqs *pu;
107
108 pu = pr->pr_usrreqs;
109 KASSERT(pu != NULL, ("protosw_init: %ssw[%d] has no usrreqs!",
110 pr->pr_domain->dom_name,
111 (int)(pr - pr->pr_domain->dom_protosw)));
112
113#define DEFAULT(foo, bar) if ((foo) == NULL) (foo) = (bar)
114 DEFAULT(pu->pru_accept, pru_accept_notsupp);
115 DEFAULT(pu->pru_connect, pru_connect_notsupp);
116 DEFAULT(pu->pru_connect2, pru_connect2_notsupp);
117 DEFAULT(pu->pru_control, pru_control_notsupp);
118 DEFAULT(pu->pru_listen, pru_listen_notsupp);
119 DEFAULT(pu->pru_rcvd, pru_rcvd_notsupp);
120 DEFAULT(pu->pru_rcvoob, pru_rcvoob_notsupp);
121 DEFAULT(pu->pru_sense, pru_sense_null);
122 DEFAULT(pu->pru_sosend, sosend_generic);
123 DEFAULT(pu->pru_soreceive, soreceive_generic);
124 DEFAULT(pu->pru_sopoll, sopoll_generic);
125#undef DEFAULT
126 if (pr->pr_init)
127 (*pr->pr_init)();
128}
129
130/*
131 * Add a new protocol domain to the list of supported domains
132 * Note: you cant unload it again because a socket may be using it.
133 * XXX can't fail at this time.
134 */
135static void
136net_init_domain(struct domain *dp)
137{
138 struct protosw *pr;
139
140 if (dp->dom_init)
141 (*dp->dom_init)();
142 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
143 protosw_init(pr);
144 /*
145 * update global information about maximums
146 */
147 max_hdr = max_linkhdr + max_protohdr;
148 max_datalen = MHLEN - max_hdr;
149 if (max_datalen < 1)
150 panic("%s: max_datalen < 1", __func__);
151}
152
153/*
154 * Add a new protocol domain to the list of supported domains
155 * Note: you cant unload it again because a socket may be using it.
156 * XXX can't fail at this time.
157 */
158void
159net_add_domain(void *data)
160{
161 struct domain *dp;
162
163 dp = (struct domain *)data;
164 mtx_lock(&dom_mtx);
165 dp->dom_next = domains;
166 domains = dp;
167
168 KASSERT(domain_init_status >= 1,
169 ("attempt to net_add_domain(%s) before domaininit()",
170 dp->dom_name));
171#ifndef INVARIANTS
172 if (domain_init_status < 1)
173 printf("WARNING: attempt to net_add_domain(%s) before "
174 "domaininit()\n", dp->dom_name);
175#endif
176#ifdef notyet
177 KASSERT(domain_init_status < 2,
178 ("attempt to net_add_domain(%s) after domainfinalize()",
179 dp->dom_name));
180#else
181 if (domain_init_status >= 2)
182 printf("WARNING: attempt to net_add_domain(%s) after "
183 "domainfinalize()\n", dp->dom_name);
184#endif
185 mtx_unlock(&dom_mtx);
186 net_init_domain(dp);
187}
188
189static void
190socket_zone_change(void *tag)
191{
192
193 uma_zone_set_max(socket_zone, maxsockets);
194}
195
196/* ARGSUSED*/
197static void
198domaininit(void *dummy)
199{
34
35#include <sys/param.h>
36#include <sys/socket.h>
37#include <sys/protosw.h>
38#include <sys/domain.h>
39#include <sys/eventhandler.h>
40#include <sys/mbuf.h>
41#include <sys/kernel.h>
42#include <sys/lock.h>
43#include <sys/mutex.h>
44#include <sys/socketvar.h>
45#include <sys/systm.h>
46#include <vm/uma.h>
47
48/*
49 * System initialization
50 *
51 * Note: domain initialization takes place on a per domain basis
52 * as a result of traversing a SYSINIT linker set. Most likely,
53 * each domain would want to call DOMAIN_SET(9) itself, which
54 * would cause the domain to be added just after domaininit()
55 * is called during startup.
56 *
57 * See DOMAIN_SET(9) for details on its use.
58 */
59
60static void domaininit(void *);
61SYSINIT(domain, SI_SUB_PROTO_DOMAIN, SI_ORDER_FIRST, domaininit, NULL)
62
63static void domainfinalize(void *);
64SYSINIT(domainfin, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_FIRST, domainfinalize,
65 NULL)
66
67static struct callout pffast_callout;
68static struct callout pfslow_callout;
69
70static void pffasttimo(void *);
71static void pfslowtimo(void *);
72
73struct domain *domains; /* registered protocol domains */
74int domain_init_status = 0;
75struct mtx dom_mtx; /* domain list lock */
76MTX_SYSINIT(domain, &dom_mtx, "domain list", MTX_DEF);
77
78/*
79 * Dummy protocol specific user requests function pointer array.
80 * All functions return EOPNOTSUPP.
81 */
82struct pr_usrreqs nousrreqs = {
83 .pru_accept = pru_accept_notsupp,
84 .pru_attach = pru_attach_notsupp,
85 .pru_bind = pru_bind_notsupp,
86 .pru_connect = pru_connect_notsupp,
87 .pru_connect2 = pru_connect2_notsupp,
88 .pru_control = pru_control_notsupp,
89 .pru_disconnect = pru_disconnect_notsupp,
90 .pru_listen = pru_listen_notsupp,
91 .pru_peeraddr = pru_peeraddr_notsupp,
92 .pru_rcvd = pru_rcvd_notsupp,
93 .pru_rcvoob = pru_rcvoob_notsupp,
94 .pru_send = pru_send_notsupp,
95 .pru_sense = pru_sense_null,
96 .pru_shutdown = pru_shutdown_notsupp,
97 .pru_sockaddr = pru_sockaddr_notsupp,
98 .pru_sosend = pru_sosend_notsupp,
99 .pru_soreceive = pru_soreceive_notsupp,
100 .pru_sopoll = pru_sopoll_notsupp,
101};
102
103static void
104protosw_init(struct protosw *pr)
105{
106 struct pr_usrreqs *pu;
107
108 pu = pr->pr_usrreqs;
109 KASSERT(pu != NULL, ("protosw_init: %ssw[%d] has no usrreqs!",
110 pr->pr_domain->dom_name,
111 (int)(pr - pr->pr_domain->dom_protosw)));
112
113#define DEFAULT(foo, bar) if ((foo) == NULL) (foo) = (bar)
114 DEFAULT(pu->pru_accept, pru_accept_notsupp);
115 DEFAULT(pu->pru_connect, pru_connect_notsupp);
116 DEFAULT(pu->pru_connect2, pru_connect2_notsupp);
117 DEFAULT(pu->pru_control, pru_control_notsupp);
118 DEFAULT(pu->pru_listen, pru_listen_notsupp);
119 DEFAULT(pu->pru_rcvd, pru_rcvd_notsupp);
120 DEFAULT(pu->pru_rcvoob, pru_rcvoob_notsupp);
121 DEFAULT(pu->pru_sense, pru_sense_null);
122 DEFAULT(pu->pru_sosend, sosend_generic);
123 DEFAULT(pu->pru_soreceive, soreceive_generic);
124 DEFAULT(pu->pru_sopoll, sopoll_generic);
125#undef DEFAULT
126 if (pr->pr_init)
127 (*pr->pr_init)();
128}
129
130/*
131 * Add a new protocol domain to the list of supported domains
132 * Note: you cant unload it again because a socket may be using it.
133 * XXX can't fail at this time.
134 */
135static void
136net_init_domain(struct domain *dp)
137{
138 struct protosw *pr;
139
140 if (dp->dom_init)
141 (*dp->dom_init)();
142 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
143 protosw_init(pr);
144 /*
145 * update global information about maximums
146 */
147 max_hdr = max_linkhdr + max_protohdr;
148 max_datalen = MHLEN - max_hdr;
149 if (max_datalen < 1)
150 panic("%s: max_datalen < 1", __func__);
151}
152
153/*
154 * Add a new protocol domain to the list of supported domains
155 * Note: you cant unload it again because a socket may be using it.
156 * XXX can't fail at this time.
157 */
158void
159net_add_domain(void *data)
160{
161 struct domain *dp;
162
163 dp = (struct domain *)data;
164 mtx_lock(&dom_mtx);
165 dp->dom_next = domains;
166 domains = dp;
167
168 KASSERT(domain_init_status >= 1,
169 ("attempt to net_add_domain(%s) before domaininit()",
170 dp->dom_name));
171#ifndef INVARIANTS
172 if (domain_init_status < 1)
173 printf("WARNING: attempt to net_add_domain(%s) before "
174 "domaininit()\n", dp->dom_name);
175#endif
176#ifdef notyet
177 KASSERT(domain_init_status < 2,
178 ("attempt to net_add_domain(%s) after domainfinalize()",
179 dp->dom_name));
180#else
181 if (domain_init_status >= 2)
182 printf("WARNING: attempt to net_add_domain(%s) after "
183 "domainfinalize()\n", dp->dom_name);
184#endif
185 mtx_unlock(&dom_mtx);
186 net_init_domain(dp);
187}
188
189static void
190socket_zone_change(void *tag)
191{
192
193 uma_zone_set_max(socket_zone, maxsockets);
194}
195
196/* ARGSUSED*/
197static void
198domaininit(void *dummy)
199{
200
200 /*
201 * Before we do any setup, make sure to initialize the
202 * zone allocator we get struct sockets from.
203 */
201 /*
202 * Before we do any setup, make sure to initialize the
203 * zone allocator we get struct sockets from.
204 */
204
205 socket_zone = uma_zcreate("socket", sizeof(struct socket), NULL, NULL,
206 NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
207 uma_zone_set_max(socket_zone, maxsockets);
208 EVENTHANDLER_REGISTER(maxsockets_change, socket_zone_change, NULL,
209 EVENTHANDLER_PRI_FIRST);
210
211 if (max_linkhdr < 16) /* XXX */
212 max_linkhdr = 16;
213
214 if (debug_mpsafenet) {
215 callout_init(&pffast_callout, CALLOUT_MPSAFE);
216 callout_init(&pfslow_callout, CALLOUT_MPSAFE);
217 } else {
218 callout_init(&pffast_callout, 0);
219 callout_init(&pfslow_callout, 0);
220 }
221
222 mtx_lock(&dom_mtx);
223 KASSERT(domain_init_status == 0, ("domaininit called too late!"));
224 domain_init_status = 1;
225 mtx_unlock(&dom_mtx);
226}
227
228/* ARGSUSED*/
229static void
230domainfinalize(void *dummy)
231{
205 socket_zone = uma_zcreate("socket", sizeof(struct socket), NULL, NULL,
206 NULL, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
207 uma_zone_set_max(socket_zone, maxsockets);
208 EVENTHANDLER_REGISTER(maxsockets_change, socket_zone_change, NULL,
209 EVENTHANDLER_PRI_FIRST);
210
211 if (max_linkhdr < 16) /* XXX */
212 max_linkhdr = 16;
213
214 if (debug_mpsafenet) {
215 callout_init(&pffast_callout, CALLOUT_MPSAFE);
216 callout_init(&pfslow_callout, CALLOUT_MPSAFE);
217 } else {
218 callout_init(&pffast_callout, 0);
219 callout_init(&pfslow_callout, 0);
220 }
221
222 mtx_lock(&dom_mtx);
223 KASSERT(domain_init_status == 0, ("domaininit called too late!"));
224 domain_init_status = 1;
225 mtx_unlock(&dom_mtx);
226}
227
228/* ARGSUSED*/
229static void
230domainfinalize(void *dummy)
231{
232
232 mtx_lock(&dom_mtx);
233 KASSERT(domain_init_status == 1, ("domainfinalize called too late!"));
234 domain_init_status = 2;
235 mtx_unlock(&dom_mtx);
236
237 callout_reset(&pffast_callout, 1, pffasttimo, NULL);
238 callout_reset(&pfslow_callout, 1, pfslowtimo, NULL);
239}
240
241struct protosw *
233 mtx_lock(&dom_mtx);
234 KASSERT(domain_init_status == 1, ("domainfinalize called too late!"));
235 domain_init_status = 2;
236 mtx_unlock(&dom_mtx);
237
238 callout_reset(&pffast_callout, 1, pffasttimo, NULL);
239 callout_reset(&pfslow_callout, 1, pfslowtimo, NULL);
240}
241
242struct protosw *
242pffindtype(family, type)
243 int family;
244 int type;
243pffindtype(int family, int type)
245{
244{
246 register struct domain *dp;
247 register struct protosw *pr;
245 struct domain *dp;
246 struct protosw *pr;
248
249 for (dp = domains; dp; dp = dp->dom_next)
250 if (dp->dom_family == family)
251 goto found;
252 return (0);
253found:
254 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
255 if (pr->pr_type && pr->pr_type == type)
256 return (pr);
257 return (0);
258}
259
260struct protosw *
247
248 for (dp = domains; dp; dp = dp->dom_next)
249 if (dp->dom_family == family)
250 goto found;
251 return (0);
252found:
253 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
254 if (pr->pr_type && pr->pr_type == type)
255 return (pr);
256 return (0);
257}
258
259struct protosw *
261pffindproto(family, protocol, type)
262 int family;
263 int protocol;
264 int type;
260pffindproto(int family, int protocol, int type)
265{
261{
266 register struct domain *dp;
267 register struct protosw *pr;
262 struct domain *dp;
263 struct protosw *pr;
268 struct protosw *maybe = 0;
269
270 if (family == 0)
271 return (0);
272 for (dp = domains; dp; dp = dp->dom_next)
273 if (dp->dom_family == family)
274 goto found;
275 return (0);
276found:
277 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
278 if ((pr->pr_protocol == protocol) && (pr->pr_type == type))
279 return (pr);
280
281 if (type == SOCK_RAW && pr->pr_type == SOCK_RAW &&
282 pr->pr_protocol == 0 && maybe == (struct protosw *)0)
283 maybe = pr;
284 }
285 return (maybe);
286}
287
288/*
289 * The caller must make sure that the new protocol is fully set up and ready to
290 * accept requests before it is registered.
291 */
292int
264 struct protosw *maybe = 0;
265
266 if (family == 0)
267 return (0);
268 for (dp = domains; dp; dp = dp->dom_next)
269 if (dp->dom_family == family)
270 goto found;
271 return (0);
272found:
273 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
274 if ((pr->pr_protocol == protocol) && (pr->pr_type == type))
275 return (pr);
276
277 if (type == SOCK_RAW && pr->pr_type == SOCK_RAW &&
278 pr->pr_protocol == 0 && maybe == (struct protosw *)0)
279 maybe = pr;
280 }
281 return (maybe);
282}
283
284/*
285 * The caller must make sure that the new protocol is fully set up and ready to
286 * accept requests before it is registered.
287 */
288int
293pf_proto_register(family, npr)
294 int family;
295 struct protosw *npr;
289pf_proto_register(int family, struct protosw *npr)
296{
297 struct domain *dp;
298 struct protosw *pr, *fpr;
299
300 /* Sanity checks. */
301 if (family == 0)
302 return (EPFNOSUPPORT);
303 if (npr->pr_type == 0)
304 return (EPROTOTYPE);
305 if (npr->pr_protocol == 0)
306 return (EPROTONOSUPPORT);
307 if (npr->pr_usrreqs == NULL)
308 return (ENXIO);
309
310 /* Try to find the specified domain based on the family. */
311 for (dp = domains; dp; dp = dp->dom_next)
312 if (dp->dom_family == family)
313 goto found;
314 return (EPFNOSUPPORT);
315
316found:
317 /* Initialize backpointer to struct domain. */
318 npr->pr_domain = dp;
319 fpr = NULL;
320
321 /*
322 * Protect us against races when two protocol registrations for
323 * the same protocol happen at the same time.
324 */
325 mtx_lock(&Giant);
326
327 /* The new protocol must not yet exist. */
328 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
329 if ((pr->pr_type == npr->pr_type) &&
330 (pr->pr_protocol == npr->pr_protocol)) {
331 mtx_unlock(&Giant);
332 return (EEXIST); /* XXX: Check only protocol? */
333 }
334 /* While here, remember the first free spacer. */
335 if ((fpr == NULL) && (pr->pr_protocol == PROTO_SPACER))
336 fpr = pr;
337 }
338
339 /* If no free spacer is found we can't add the new protocol. */
340 if (fpr == NULL) {
341 mtx_unlock(&Giant);
342 return (ENOMEM);
343 }
344
345 /* Copy the new struct protosw over the spacer. */
346 bcopy(npr, fpr, sizeof(*fpr));
347
348 /* Job is done, no more protection required. */
349 mtx_unlock(&Giant);
350
351 /* Initialize and activate the protocol. */
352 protosw_init(fpr);
353
354 return (0);
355}
356
357/*
358 * The caller must make sure the protocol and its functions correctly shut down
359 * all sockets and release all locks and memory references.
360 */
361int
290{
291 struct domain *dp;
292 struct protosw *pr, *fpr;
293
294 /* Sanity checks. */
295 if (family == 0)
296 return (EPFNOSUPPORT);
297 if (npr->pr_type == 0)
298 return (EPROTOTYPE);
299 if (npr->pr_protocol == 0)
300 return (EPROTONOSUPPORT);
301 if (npr->pr_usrreqs == NULL)
302 return (ENXIO);
303
304 /* Try to find the specified domain based on the family. */
305 for (dp = domains; dp; dp = dp->dom_next)
306 if (dp->dom_family == family)
307 goto found;
308 return (EPFNOSUPPORT);
309
310found:
311 /* Initialize backpointer to struct domain. */
312 npr->pr_domain = dp;
313 fpr = NULL;
314
315 /*
316 * Protect us against races when two protocol registrations for
317 * the same protocol happen at the same time.
318 */
319 mtx_lock(&Giant);
320
321 /* The new protocol must not yet exist. */
322 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
323 if ((pr->pr_type == npr->pr_type) &&
324 (pr->pr_protocol == npr->pr_protocol)) {
325 mtx_unlock(&Giant);
326 return (EEXIST); /* XXX: Check only protocol? */
327 }
328 /* While here, remember the first free spacer. */
329 if ((fpr == NULL) && (pr->pr_protocol == PROTO_SPACER))
330 fpr = pr;
331 }
332
333 /* If no free spacer is found we can't add the new protocol. */
334 if (fpr == NULL) {
335 mtx_unlock(&Giant);
336 return (ENOMEM);
337 }
338
339 /* Copy the new struct protosw over the spacer. */
340 bcopy(npr, fpr, sizeof(*fpr));
341
342 /* Job is done, no more protection required. */
343 mtx_unlock(&Giant);
344
345 /* Initialize and activate the protocol. */
346 protosw_init(fpr);
347
348 return (0);
349}
350
351/*
352 * The caller must make sure the protocol and its functions correctly shut down
353 * all sockets and release all locks and memory references.
354 */
355int
362pf_proto_unregister(family, protocol, type)
363 int family;
364 int protocol;
365 int type;
356pf_proto_unregister(int family, int protocol, int type)
366{
367 struct domain *dp;
368 struct protosw *pr, *dpr;
369
370 /* Sanity checks. */
371 if (family == 0)
372 return (EPFNOSUPPORT);
373 if (protocol == 0)
374 return (EPROTONOSUPPORT);
375 if (type == 0)
376 return (EPROTOTYPE);
377
378 /* Try to find the specified domain based on the family type. */
379 for (dp = domains; dp; dp = dp->dom_next)
380 if (dp->dom_family == family)
381 goto found;
382 return (EPFNOSUPPORT);
383
384found:
385 dpr = NULL;
386
387 /* Lock out everyone else while we are manipulating the protosw. */
388 mtx_lock(&Giant);
389
390 /* The protocol must exist and only once. */
391 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
392 if ((pr->pr_type == type) && (pr->pr_protocol == protocol)) {
393 if (dpr != NULL) {
394 mtx_unlock(&Giant);
395 return (EMLINK); /* Should not happen! */
396 } else
397 dpr = pr;
398 }
399 }
400
401 /* Protocol does not exist. */
402 if (dpr == NULL) {
403 mtx_unlock(&Giant);
404 return (EPROTONOSUPPORT);
405 }
406
407 /* De-orbit the protocol and make the slot available again. */
408 dpr->pr_type = 0;
409 dpr->pr_domain = dp;
410 dpr->pr_protocol = PROTO_SPACER;
411 dpr->pr_flags = 0;
412 dpr->pr_input = NULL;
413 dpr->pr_output = NULL;
414 dpr->pr_ctlinput = NULL;
415 dpr->pr_ctloutput = NULL;
416 dpr->pr_ousrreq = NULL;
417 dpr->pr_init = NULL;
418 dpr->pr_fasttimo = NULL;
419 dpr->pr_slowtimo = NULL;
420 dpr->pr_drain = NULL;
421 dpr->pr_usrreqs = &nousrreqs;
422
423 /* Job is done, not more protection required. */
424 mtx_unlock(&Giant);
425
426 return (0);
427}
428
429void
357{
358 struct domain *dp;
359 struct protosw *pr, *dpr;
360
361 /* Sanity checks. */
362 if (family == 0)
363 return (EPFNOSUPPORT);
364 if (protocol == 0)
365 return (EPROTONOSUPPORT);
366 if (type == 0)
367 return (EPROTOTYPE);
368
369 /* Try to find the specified domain based on the family type. */
370 for (dp = domains; dp; dp = dp->dom_next)
371 if (dp->dom_family == family)
372 goto found;
373 return (EPFNOSUPPORT);
374
375found:
376 dpr = NULL;
377
378 /* Lock out everyone else while we are manipulating the protosw. */
379 mtx_lock(&Giant);
380
381 /* The protocol must exist and only once. */
382 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++) {
383 if ((pr->pr_type == type) && (pr->pr_protocol == protocol)) {
384 if (dpr != NULL) {
385 mtx_unlock(&Giant);
386 return (EMLINK); /* Should not happen! */
387 } else
388 dpr = pr;
389 }
390 }
391
392 /* Protocol does not exist. */
393 if (dpr == NULL) {
394 mtx_unlock(&Giant);
395 return (EPROTONOSUPPORT);
396 }
397
398 /* De-orbit the protocol and make the slot available again. */
399 dpr->pr_type = 0;
400 dpr->pr_domain = dp;
401 dpr->pr_protocol = PROTO_SPACER;
402 dpr->pr_flags = 0;
403 dpr->pr_input = NULL;
404 dpr->pr_output = NULL;
405 dpr->pr_ctlinput = NULL;
406 dpr->pr_ctloutput = NULL;
407 dpr->pr_ousrreq = NULL;
408 dpr->pr_init = NULL;
409 dpr->pr_fasttimo = NULL;
410 dpr->pr_slowtimo = NULL;
411 dpr->pr_drain = NULL;
412 dpr->pr_usrreqs = &nousrreqs;
413
414 /* Job is done, not more protection required. */
415 mtx_unlock(&Giant);
416
417 return (0);
418}
419
420void
430pfctlinput(cmd, sa)
431 int cmd;
432 struct sockaddr *sa;
421pfctlinput(int cmd, struct sockaddr *sa)
433{
422{
434 register struct domain *dp;
435 register struct protosw *pr;
423 struct domain *dp;
424 struct protosw *pr;
436
437 for (dp = domains; dp; dp = dp->dom_next)
438 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
439 if (pr->pr_ctlinput)
440 (*pr->pr_ctlinput)(cmd, sa, (void *)0);
441}
442
443void
425
426 for (dp = domains; dp; dp = dp->dom_next)
427 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
428 if (pr->pr_ctlinput)
429 (*pr->pr_ctlinput)(cmd, sa, (void *)0);
430}
431
432void
444pfctlinput2(cmd, sa, ctlparam)
445 int cmd;
446 struct sockaddr *sa;
447 void *ctlparam;
433pfctlinput2(int cmd, struct sockaddr *sa, void *ctlparam)
448{
449 struct domain *dp;
450 struct protosw *pr;
451
452 if (!sa)
453 return;
454 for (dp = domains; dp; dp = dp->dom_next) {
455 /*
456 * the check must be made by xx_ctlinput() anyways, to
457 * make sure we use data item pointed to by ctlparam in
458 * correct way. the following check is made just for safety.
459 */
460 if (dp->dom_family != sa->sa_family)
461 continue;
462
463 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
464 if (pr->pr_ctlinput)
465 (*pr->pr_ctlinput)(cmd, sa, ctlparam);
466 }
467}
468
469static void
434{
435 struct domain *dp;
436 struct protosw *pr;
437
438 if (!sa)
439 return;
440 for (dp = domains; dp; dp = dp->dom_next) {
441 /*
442 * the check must be made by xx_ctlinput() anyways, to
443 * make sure we use data item pointed to by ctlparam in
444 * correct way. the following check is made just for safety.
445 */
446 if (dp->dom_family != sa->sa_family)
447 continue;
448
449 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
450 if (pr->pr_ctlinput)
451 (*pr->pr_ctlinput)(cmd, sa, ctlparam);
452 }
453}
454
455static void
470pfslowtimo(arg)
471 void *arg;
456pfslowtimo(void *arg)
472{
457{
473 register struct domain *dp;
474 register struct protosw *pr;
458 struct domain *dp;
459 struct protosw *pr;
475
476 NET_ASSERT_GIANT();
477
478 for (dp = domains; dp; dp = dp->dom_next)
479 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
480 if (pr->pr_slowtimo)
481 (*pr->pr_slowtimo)();
482 callout_reset(&pfslow_callout, hz/2, pfslowtimo, NULL);
483}
484
485static void
460
461 NET_ASSERT_GIANT();
462
463 for (dp = domains; dp; dp = dp->dom_next)
464 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
465 if (pr->pr_slowtimo)
466 (*pr->pr_slowtimo)();
467 callout_reset(&pfslow_callout, hz/2, pfslowtimo, NULL);
468}
469
470static void
486pffasttimo(arg)
487 void *arg;
471pffasttimo(void *arg)
488{
472{
489 register struct domain *dp;
490 register struct protosw *pr;
473 struct domain *dp;
474 struct protosw *pr;
491
492 NET_ASSERT_GIANT();
493
494 for (dp = domains; dp; dp = dp->dom_next)
495 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
496 if (pr->pr_fasttimo)
497 (*pr->pr_fasttimo)();
498 callout_reset(&pffast_callout, hz/5, pffasttimo, NULL);
499}
475
476 NET_ASSERT_GIANT();
477
478 for (dp = domains; dp; dp = dp->dom_next)
479 for (pr = dp->dom_protosw; pr < dp->dom_protoswNPROTOSW; pr++)
480 if (pr->pr_fasttimo)
481 (*pr->pr_fasttimo)();
482 callout_reset(&pffast_callout, hz/5, pffasttimo, NULL);
483}