1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
|
/*-
* SPDX-License-Identifier: BSD-3-Clause
*
* Copyright (c) 1982, 1986, 1993
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/param.h>
#include <sys/socket.h>
#include <sys/protosw.h>
#include <sys/domain.h>
#include <sys/eventhandler.h>
#include <sys/epoch.h>
#include <sys/mbuf.h>
#include <sys/kernel.h>
#include <sys/lock.h>
#include <sys/mutex.h>
#include <sys/rmlock.h>
#include <sys/socketvar.h>
#include <sys/systm.h>
#include <machine/atomic.h>
#include <net/vnet.h>
struct domainhead domains = SLIST_HEAD_INITIALIZER(&domains);
int domain_init_status = 1;
static struct mtx dom_mtx; /* domain list lock */
MTX_SYSINIT(domain, &dom_mtx, "domain list", MTX_DEF);
static int
pr_accept_notsupp(struct socket *so, struct sockaddr *sa)
{
return (EOPNOTSUPP);
}
static int
pr_bind_notsupp(struct socket *so, struct sockaddr *nam, struct thread *td)
{
return (EOPNOTSUPP);
}
static int
pr_bindat_notsupp(int fd, struct socket *so, struct sockaddr *nam,
struct thread *td)
{
return (EOPNOTSUPP);
}
static int
pr_connect_notsupp(struct socket *so, struct sockaddr *nam, struct thread *td)
{
return (EOPNOTSUPP);
}
static int
pr_connectat_notsupp(int fd, struct socket *so, struct sockaddr *nam,
struct thread *td)
{
return (EOPNOTSUPP);
}
static int
pr_connect2_notsupp(struct socket *so1, struct socket *so2)
{
return (EOPNOTSUPP);
}
static int
pr_control_notsupp(struct socket *so, u_long cmd, void *data,
struct ifnet *ifp, struct thread *td)
{
return (EOPNOTSUPP);
}
static int
pr_ctloutput_notsupp(struct socket *so, struct sockopt *sopt)
{
return (ENOPROTOOPT);
}
static int
pr_disconnect_notsupp(struct socket *so)
{
return (EOPNOTSUPP);
}
int
pr_listen_notsupp(struct socket *so, int backlog, struct thread *td)
{
return (EOPNOTSUPP);
}
static int
pr_peeraddr_notsupp(struct socket *so, struct sockaddr *nam)
{
return (EOPNOTSUPP);
}
static int
pr_rcvd_notsupp(struct socket *so, int flags)
{
return (EOPNOTSUPP);
}
static int
pr_rcvoob_notsupp(struct socket *so, struct mbuf *m, int flags)
{
return (EOPNOTSUPP);
}
static int
pr_send_notsupp(struct socket *so, int flags, struct mbuf *m,
struct sockaddr *addr, struct mbuf *control, struct thread *td)
{
if (control != NULL)
m_freem(control);
if ((flags & PRUS_NOTREADY) == 0)
m_freem(m);
return (EOPNOTSUPP);
}
static int
pr_sendfile_wait_notsupp(struct socket *so, off_t need, int *space)
{
return (EOPNOTSUPP);
}
static int
pr_ready_notsupp(struct socket *so, struct mbuf *m, int count)
{
return (EOPNOTSUPP);
}
static int
pr_shutdown_notsupp(struct socket *so, enum shutdown_how how)
{
return (EOPNOTSUPP);
}
static int
pr_sockaddr_notsupp(struct socket *so, struct sockaddr *nam)
{
return (EOPNOTSUPP);
}
static void
pr_init(struct domain *dom, struct protosw *pr)
{
KASSERT(pr->pr_attach != NULL,
("%s: protocol doesn't have pr_attach", __func__));
pr->pr_domain = dom;
#define DEFAULT(foo, bar) if (pr->foo == NULL) pr->foo = bar
DEFAULT(pr_sosend, sosend_generic);
DEFAULT(pr_soreceive, soreceive_generic);
DEFAULT(pr_sopoll, sopoll_generic);
DEFAULT(pr_setsbopt, sbsetopt);
DEFAULT(pr_aio_queue, soaio_queue_generic);
DEFAULT(pr_kqfilter, sokqfilter_generic);
#define NOTSUPP(foo) if (pr->foo == NULL) pr->foo = foo ## _notsupp
NOTSUPP(pr_accept);
NOTSUPP(pr_bind);
NOTSUPP(pr_bindat);
NOTSUPP(pr_connect);
NOTSUPP(pr_connect2);
NOTSUPP(pr_connectat);
NOTSUPP(pr_control);
NOTSUPP(pr_ctloutput);
NOTSUPP(pr_disconnect);
NOTSUPP(pr_listen);
NOTSUPP(pr_peeraddr);
NOTSUPP(pr_rcvd);
NOTSUPP(pr_rcvoob);
NOTSUPP(pr_send);
NOTSUPP(pr_sendfile_wait);
NOTSUPP(pr_shutdown);
NOTSUPP(pr_sockaddr);
NOTSUPP(pr_ready);
}
/*
* Add a new protocol domain to the list of supported domains
* Note: you can't unload it again because a socket may be using it.
* XXX can't fail at this time.
*/
void
domain_add(struct domain *dp)
{
struct protosw *pr;
MPASS(IS_DEFAULT_VNET(curvnet));
if (dp->dom_probe != NULL && (*dp->dom_probe)() != 0)
return;
for (int i = 0; i < dp->dom_nprotosw; i++)
if ((pr = dp->dom_protosw[i]) != NULL)
pr_init(dp, pr);
mtx_lock(&dom_mtx);
#ifdef INVARIANTS
struct domain *tmp;
SLIST_FOREACH(tmp, &domains, dom_next)
MPASS(tmp->dom_family != dp->dom_family);
#endif
SLIST_INSERT_HEAD(&domains, dp, dom_next);
mtx_unlock(&dom_mtx);
}
void
domain_remove(struct domain *dp)
{
if ((dp->dom_flags & DOMF_UNLOADABLE) == 0)
return;
mtx_lock(&dom_mtx);
SLIST_REMOVE(&domains, dp, domain, dom_next);
mtx_unlock(&dom_mtx);
}
static void
domainfinalize(void *dummy)
{
mtx_lock(&dom_mtx);
KASSERT(domain_init_status == 1, ("domainfinalize called too late!"));
domain_init_status = 2;
mtx_unlock(&dom_mtx);
}
SYSINIT(domainfin, SI_SUB_PROTO_IFATTACHDOMAIN, SI_ORDER_FIRST, domainfinalize,
NULL);
struct domain *
pffinddomain(int family)
{
struct domain *dp;
SLIST_FOREACH(dp, &domains, dom_next)
if (dp->dom_family == family)
return (dp);
return (NULL);
}
struct protosw *
pffindproto(int family, int type, int proto)
{
struct domain *dp;
struct protosw *pr;
dp = pffinddomain(family);
if (dp == NULL)
return (NULL);
for (int i = 0; i < dp->dom_nprotosw; i++)
if ((pr = dp->dom_protosw[i]) != NULL && pr->pr_type == type &&
(pr->pr_protocol == 0 || proto == 0 ||
pr->pr_protocol == proto))
return (pr);
return (NULL);
}
/*
* The caller must make sure that the new protocol is fully set up and ready to
* accept requests before it is registered.
*/
int
protosw_register(struct domain *dp, struct protosw *npr)
{
struct protosw **prp;
MPASS(dp);
MPASS(npr && npr->pr_type > 0 && npr->pr_protocol > 0);
prp = NULL;
/*
* Protect us against races when two protocol registrations for
* the same protocol happen at the same time.
*/
mtx_lock(&dom_mtx);
for (int i = 0; i < dp->dom_nprotosw; i++) {
if (dp->dom_protosw[i] == NULL) {
/* Remember the first free spacer. */
if (prp == NULL)
prp = &dp->dom_protosw[i];
} else {
/*
* The new protocol must not yet exist.
* XXXAO: Check only protocol?
* XXXGL: Maybe assert that it doesn't exist?
*/
if ((dp->dom_protosw[i]->pr_type == npr->pr_type) &&
(dp->dom_protosw[i]->pr_protocol ==
npr->pr_protocol)) {
mtx_unlock(&dom_mtx);
return (EEXIST);
}
}
}
/* If no free spacer is found we can't add the new protocol. */
if (prp == NULL) {
mtx_unlock(&dom_mtx);
return (ENOMEM);
}
pr_init(dp, npr);
*prp = npr;
mtx_unlock(&dom_mtx);
return (0);
}
/*
* The caller must make sure the protocol and its functions correctly shut down
* all sockets and release all locks and memory references.
*/
int
protosw_unregister(struct protosw *pr)
{
struct domain *dp;
struct protosw **prp;
dp = pr->pr_domain;
prp = NULL;
mtx_lock(&dom_mtx);
/* The protocol must exist and only once. */
for (int i = 0; i < dp->dom_nprotosw; i++) {
if (dp->dom_protosw[i] == pr) {
KASSERT(prp == NULL,
("%s: domain %p protocol %p registered twice\n",
__func__, dp, pr));
prp = &dp->dom_protosw[i];
}
}
/* Protocol does not exist. XXXGL: assert that it does? */
if (prp == NULL) {
mtx_unlock(&dom_mtx);
return (EPROTONOSUPPORT);
}
/* De-orbit the protocol and make the slot available again. */
*prp = NULL;
mtx_unlock(&dom_mtx);
return (0);
}
|