sockin.c revision 1.41 1 1.41 rtr /* $NetBSD: sockin.c,v 1.41 2014/06/22 08:10:19 rtr Exp $ */
2 1.1 pooka
3 1.1 pooka /*
4 1.9 pooka * Copyright (c) 2008, 2009 Antti Kantee. All Rights Reserved.
5 1.1 pooka *
6 1.1 pooka * Redistribution and use in source and binary forms, with or without
7 1.1 pooka * modification, are permitted provided that the following conditions
8 1.1 pooka * are met:
9 1.1 pooka * 1. Redistributions of source code must retain the above copyright
10 1.1 pooka * notice, this list of conditions and the following disclaimer.
11 1.1 pooka * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 pooka * notice, this list of conditions and the following disclaimer in the
13 1.1 pooka * documentation and/or other materials provided with the distribution.
14 1.1 pooka *
15 1.1 pooka * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
16 1.1 pooka * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 1.1 pooka * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 1.1 pooka * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 1.1 pooka * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 1.1 pooka * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 1.1 pooka * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 1.1 pooka * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 1.1 pooka * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 1.1 pooka * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 1.1 pooka * SUCH DAMAGE.
26 1.1 pooka */
27 1.1 pooka
28 1.8 pooka #include <sys/cdefs.h>
29 1.41 rtr __KERNEL_RCSID(0, "$NetBSD: sockin.c,v 1.41 2014/06/22 08:10:19 rtr Exp $");
30 1.8 pooka
31 1.1 pooka #include <sys/param.h>
32 1.1 pooka #include <sys/condvar.h>
33 1.1 pooka #include <sys/domain.h>
34 1.1 pooka #include <sys/kmem.h>
35 1.1 pooka #include <sys/kthread.h>
36 1.1 pooka #include <sys/mbuf.h>
37 1.1 pooka #include <sys/mutex.h>
38 1.34 stacktic #include <sys/once.h>
39 1.1 pooka #include <sys/poll.h>
40 1.1 pooka #include <sys/protosw.h>
41 1.1 pooka #include <sys/queue.h>
42 1.1 pooka #include <sys/socket.h>
43 1.1 pooka #include <sys/socketvar.h>
44 1.1 pooka #include <sys/time.h>
45 1.1 pooka
46 1.21 pooka #include <net/bpf.h>
47 1.21 pooka #include <net/if.h>
48 1.6 pooka #include <net/radix.h>
49 1.6 pooka
50 1.1 pooka #include <netinet/in.h>
51 1.1 pooka #include <netinet/in_systm.h>
52 1.1 pooka #include <netinet/ip.h>
53 1.1 pooka
54 1.1 pooka #include <rump/rumpuser.h>
55 1.1 pooka
56 1.9 pooka #include "rump_private.h"
57 1.36 pooka #include "sockin_user.h"
58 1.9 pooka
59 1.1 pooka /*
60 1.1 pooka * An inet communication domain which uses the socket interface.
61 1.38 pooka * Supports IPv4 & IPv6 UDP/TCP.
62 1.1 pooka */
63 1.1 pooka
64 1.1 pooka DOMAIN_DEFINE(sockindomain);
65 1.31 stacktic DOMAIN_DEFINE(sockin6domain);
66 1.31 stacktic
67 1.34 stacktic static int sockin_do_init(void);
68 1.1 pooka static void sockin_init(void);
69 1.40 rmind static int sockin_attach(struct socket *, int);
70 1.40 rmind static void sockin_detach(struct socket *);
71 1.41 rtr static int sockin_ioctl(struct socket *, struct mbuf *, struct mbuf *,
72 1.41 rtr struct mbuf *control, struct lwp *);
73 1.1 pooka static int sockin_usrreq(struct socket *, int, struct mbuf *,
74 1.1 pooka struct mbuf *, struct mbuf *, struct lwp *);
75 1.7 pooka static int sockin_ctloutput(int op, struct socket *, struct sockopt *);
76 1.1 pooka
77 1.39 rmind static const struct pr_usrreqs sockin_usrreqs = {
78 1.40 rmind .pr_attach = sockin_attach,
79 1.40 rmind .pr_detach = sockin_detach,
80 1.41 rtr .pr_ioctl = sockin_ioctl,
81 1.39 rmind .pr_generic = sockin_usrreq,
82 1.39 rmind };
83 1.39 rmind
84 1.1 pooka const struct protosw sockinsw[] = {
85 1.2 pooka {
86 1.2 pooka .pr_type = SOCK_DGRAM,
87 1.1 pooka .pr_domain = &sockindomain,
88 1.1 pooka .pr_protocol = IPPROTO_UDP,
89 1.1 pooka .pr_flags = PR_ATOMIC|PR_ADDR,
90 1.39 rmind .pr_usrreqs = &sockin_usrreqs,
91 1.7 pooka .pr_ctloutput = sockin_ctloutput,
92 1.2 pooka },
93 1.2 pooka {
94 1.2 pooka .pr_type = SOCK_STREAM,
95 1.2 pooka .pr_domain = &sockindomain,
96 1.2 pooka .pr_protocol = IPPROTO_TCP,
97 1.2 pooka .pr_flags = PR_CONNREQUIRED|PR_WANTRCVD|PR_LISTEN|PR_ABRTACPTDIS,
98 1.39 rmind .pr_usrreqs = &sockin_usrreqs,
99 1.7 pooka .pr_ctloutput = sockin_ctloutput,
100 1.1 pooka }};
101 1.31 stacktic const struct protosw sockin6sw[] = {
102 1.31 stacktic {
103 1.31 stacktic .pr_type = SOCK_DGRAM,
104 1.31 stacktic .pr_domain = &sockin6domain,
105 1.31 stacktic .pr_protocol = IPPROTO_UDP,
106 1.31 stacktic .pr_flags = PR_ATOMIC|PR_ADDR,
107 1.39 rmind .pr_usrreqs = &sockin_usrreqs,
108 1.31 stacktic .pr_ctloutput = sockin_ctloutput,
109 1.31 stacktic },
110 1.31 stacktic {
111 1.31 stacktic .pr_type = SOCK_STREAM,
112 1.31 stacktic .pr_domain = &sockin6domain,
113 1.31 stacktic .pr_protocol = IPPROTO_TCP,
114 1.31 stacktic .pr_flags = PR_CONNREQUIRED|PR_WANTRCVD|PR_LISTEN|PR_ABRTACPTDIS,
115 1.39 rmind .pr_usrreqs = &sockin_usrreqs,
116 1.31 stacktic .pr_ctloutput = sockin_ctloutput,
117 1.31 stacktic }};
118 1.1 pooka
119 1.1 pooka struct domain sockindomain = {
120 1.1 pooka .dom_family = PF_INET,
121 1.1 pooka .dom_name = "socket_inet",
122 1.2 pooka .dom_init = sockin_init,
123 1.1 pooka .dom_externalize = NULL,
124 1.1 pooka .dom_dispose = NULL,
125 1.1 pooka .dom_protosw = sockinsw,
126 1.4 minskim .dom_protoswNPROTOSW = &sockinsw[__arraycount(sockinsw)],
127 1.26 dyoung .dom_rtattach = rt_inithead,
128 1.6 pooka .dom_rtoffset = 32,
129 1.6 pooka .dom_maxrtkey = sizeof(struct sockaddr_in),
130 1.1 pooka .dom_ifattach = NULL,
131 1.1 pooka .dom_ifdetach = NULL,
132 1.1 pooka .dom_ifqueues = { NULL },
133 1.1 pooka .dom_link = { NULL },
134 1.1 pooka .dom_mowner = MOWNER_INIT("",""),
135 1.1 pooka .dom_rtcache = { NULL },
136 1.1 pooka .dom_sockaddr_cmp = NULL
137 1.1 pooka };
138 1.31 stacktic struct domain sockin6domain = {
139 1.31 stacktic .dom_family = PF_INET6,
140 1.31 stacktic .dom_name = "socket_inet6",
141 1.31 stacktic .dom_init = sockin_init,
142 1.31 stacktic .dom_externalize = NULL,
143 1.31 stacktic .dom_dispose = NULL,
144 1.31 stacktic .dom_protosw = sockin6sw,
145 1.31 stacktic .dom_protoswNPROTOSW = &sockin6sw[__arraycount(sockin6sw)],
146 1.31 stacktic .dom_rtattach = rt_inithead,
147 1.31 stacktic .dom_rtoffset = 32,
148 1.31 stacktic .dom_maxrtkey = sizeof(struct sockaddr_in6),
149 1.31 stacktic .dom_ifattach = NULL,
150 1.31 stacktic .dom_ifdetach = NULL,
151 1.31 stacktic .dom_ifqueues = { NULL },
152 1.31 stacktic .dom_link = { NULL },
153 1.31 stacktic .dom_mowner = MOWNER_INIT("",""),
154 1.31 stacktic .dom_rtcache = { NULL },
155 1.31 stacktic .dom_sockaddr_cmp = NULL
156 1.31 stacktic };
157 1.1 pooka
158 1.3 pooka #define SO2S(so) ((intptr_t)(so->so_internal))
159 1.2 pooka #define SOCKIN_SBSIZE 65536
160 1.1 pooka
161 1.5 pooka struct sockin_unit {
162 1.5 pooka struct socket *su_so;
163 1.5 pooka
164 1.5 pooka LIST_ENTRY(sockin_unit) su_entries;
165 1.5 pooka };
166 1.5 pooka static LIST_HEAD(, sockin_unit) su_ent = LIST_HEAD_INITIALIZER(su_ent);
167 1.5 pooka static kmutex_t su_mtx;
168 1.5 pooka static bool rebuild;
169 1.5 pooka static int nsock;
170 1.5 pooka
171 1.21 pooka /* XXX: for the bpf hack */
172 1.21 pooka static struct ifnet sockin_if;
173 1.21 pooka int ifpromisc(struct ifnet *ifp, int pswitch) { return 0; }
174 1.21 pooka
175 1.5 pooka static int
176 1.5 pooka registersock(struct socket *so, int news)
177 1.5 pooka {
178 1.5 pooka struct sockin_unit *su;
179 1.5 pooka
180 1.5 pooka su = kmem_alloc(sizeof(*su), KM_NOSLEEP);
181 1.5 pooka if (!su)
182 1.5 pooka return ENOMEM;
183 1.5 pooka
184 1.5 pooka so->so_internal = (void *)(intptr_t)news;
185 1.5 pooka su->su_so = so;
186 1.5 pooka
187 1.5 pooka mutex_enter(&su_mtx);
188 1.5 pooka LIST_INSERT_HEAD(&su_ent, su, su_entries);
189 1.5 pooka nsock++;
190 1.5 pooka rebuild = true;
191 1.5 pooka mutex_exit(&su_mtx);
192 1.5 pooka
193 1.5 pooka return 0;
194 1.5 pooka }
195 1.5 pooka
196 1.1 pooka static void
197 1.10 pooka removesock(struct socket *so)
198 1.10 pooka {
199 1.10 pooka struct sockin_unit *su_iter;
200 1.10 pooka
201 1.10 pooka mutex_enter(&su_mtx);
202 1.10 pooka LIST_FOREACH(su_iter, &su_ent, su_entries) {
203 1.10 pooka if (su_iter->su_so == so)
204 1.10 pooka break;
205 1.10 pooka }
206 1.10 pooka if (!su_iter)
207 1.10 pooka panic("no such socket");
208 1.10 pooka
209 1.10 pooka LIST_REMOVE(su_iter, su_entries);
210 1.10 pooka nsock--;
211 1.10 pooka rebuild = true;
212 1.10 pooka mutex_exit(&su_mtx);
213 1.10 pooka
214 1.29 pooka rumpuser_close(SO2S(su_iter->su_so));
215 1.10 pooka kmem_free(su_iter, sizeof(*su_iter));
216 1.10 pooka }
217 1.10 pooka
218 1.10 pooka static void
219 1.1 pooka sockin_process(struct socket *so)
220 1.1 pooka {
221 1.31 stacktic struct sockaddr_in6 from;
222 1.1 pooka struct iovec io;
223 1.1 pooka struct msghdr rmsg;
224 1.1 pooka struct mbuf *m;
225 1.30 pooka size_t n, plen;
226 1.1 pooka int error;
227 1.1 pooka
228 1.1 pooka m = m_gethdr(M_WAIT, MT_DATA);
229 1.12 pooka if (so->so_proto->pr_type == SOCK_DGRAM) {
230 1.12 pooka plen = IP_MAXPACKET;
231 1.12 pooka MEXTMALLOC(m, plen, M_DONTWAIT);
232 1.12 pooka } else {
233 1.12 pooka plen = MCLBYTES;
234 1.12 pooka MCLGET(m, M_DONTWAIT);
235 1.12 pooka }
236 1.12 pooka if ((m->m_flags & M_EXT) == 0) {
237 1.12 pooka m_freem(m);
238 1.12 pooka return;
239 1.12 pooka }
240 1.1 pooka
241 1.1 pooka memset(&rmsg, 0, sizeof(rmsg));
242 1.1 pooka io.iov_base = mtod(m, void *);
243 1.1 pooka io.iov_len = plen;
244 1.1 pooka rmsg.msg_iov = &io;
245 1.1 pooka rmsg.msg_iovlen = 1;
246 1.1 pooka rmsg.msg_name = (struct sockaddr *)&from;
247 1.1 pooka rmsg.msg_namelen = sizeof(from);
248 1.1 pooka
249 1.30 pooka error = rumpcomp_sockin_recvmsg(SO2S(so), &rmsg, 0, &n);
250 1.33 pooka if (error || n == 0) {
251 1.1 pooka m_freem(m);
252 1.10 pooka
253 1.10 pooka /* Treat a TCP socket a goner */
254 1.16 pooka if (error != EAGAIN && so->so_proto->pr_type == SOCK_STREAM) {
255 1.11 pooka mutex_enter(softnet_lock);
256 1.10 pooka soisdisconnected(so);
257 1.11 pooka mutex_exit(softnet_lock);
258 1.10 pooka removesock(so);
259 1.10 pooka }
260 1.1 pooka return;
261 1.1 pooka }
262 1.1 pooka m->m_len = m->m_pkthdr.len = n;
263 1.1 pooka
264 1.22 joerg bpf_mtap_af(&sockin_if, AF_UNSPEC, m);
265 1.21 pooka
266 1.11 pooka mutex_enter(softnet_lock);
267 1.2 pooka if (so->so_proto->pr_type == SOCK_DGRAM) {
268 1.2 pooka if (!sbappendaddr(&so->so_rcv, rmsg.msg_name, m, NULL)) {
269 1.2 pooka m_freem(m);
270 1.2 pooka }
271 1.2 pooka } else {
272 1.2 pooka sbappendstream(&so->so_rcv, m);
273 1.1 pooka }
274 1.2 pooka
275 1.1 pooka sorwakeup(so);
276 1.11 pooka mutex_exit(softnet_lock);
277 1.1 pooka }
278 1.1 pooka
279 1.5 pooka static void
280 1.5 pooka sockin_accept(struct socket *so)
281 1.5 pooka {
282 1.5 pooka struct socket *nso;
283 1.31 stacktic struct sockaddr_in6 sin;
284 1.5 pooka int news, error, slen;
285 1.5 pooka
286 1.5 pooka slen = sizeof(sin);
287 1.29 pooka error = rumpcomp_sockin_accept(SO2S(so), (struct sockaddr *)&sin,
288 1.29 pooka &slen, &news);
289 1.29 pooka if (error)
290 1.5 pooka return;
291 1.1 pooka
292 1.11 pooka mutex_enter(softnet_lock);
293 1.35 rmind nso = sonewconn(so, true);
294 1.11 pooka if (nso == NULL)
295 1.5 pooka goto errout;
296 1.5 pooka if (registersock(nso, news) != 0)
297 1.5 pooka goto errout;
298 1.11 pooka mutex_exit(softnet_lock);
299 1.5 pooka return;
300 1.5 pooka
301 1.5 pooka errout:
302 1.29 pooka rumpuser_close(news);
303 1.5 pooka if (nso)
304 1.5 pooka soclose(nso);
305 1.11 pooka mutex_exit(softnet_lock);
306 1.5 pooka }
307 1.1 pooka
308 1.1 pooka #define POLLTIMEOUT 100 /* check for new entries every 100ms */
309 1.1 pooka
310 1.1 pooka /* XXX: doesn't handle socket (kernel) locking properly? */
311 1.1 pooka static void
312 1.1 pooka sockinworker(void *arg)
313 1.1 pooka {
314 1.1 pooka struct pollfd *pfds = NULL, *npfds;
315 1.1 pooka struct sockin_unit *su_iter;
316 1.5 pooka struct socket *so;
317 1.1 pooka int cursock = 0, i, rv, error;
318 1.1 pooka
319 1.1 pooka /*
320 1.1 pooka * Loop reading requests. Check for new sockets periodically
321 1.1 pooka * (could be smarter, but I'm lazy).
322 1.1 pooka */
323 1.1 pooka for (;;) {
324 1.1 pooka if (rebuild) {
325 1.1 pooka npfds = NULL;
326 1.1 pooka mutex_enter(&su_mtx);
327 1.1 pooka if (nsock)
328 1.1 pooka npfds = kmem_alloc(nsock * sizeof(*npfds),
329 1.1 pooka KM_NOSLEEP);
330 1.1 pooka if (npfds || nsock == 0) {
331 1.1 pooka if (pfds)
332 1.1 pooka kmem_free(pfds, cursock*sizeof(*pfds));
333 1.1 pooka pfds = npfds;
334 1.1 pooka cursock = nsock;
335 1.1 pooka rebuild = false;
336 1.1 pooka
337 1.1 pooka i = 0;
338 1.1 pooka LIST_FOREACH(su_iter, &su_ent, su_entries) {
339 1.1 pooka pfds[i].fd = SO2S(su_iter->su_so);
340 1.1 pooka pfds[i].events = POLLIN;
341 1.1 pooka pfds[i].revents = 0;
342 1.1 pooka i++;
343 1.1 pooka }
344 1.1 pooka KASSERT(i == nsock);
345 1.1 pooka }
346 1.1 pooka mutex_exit(&su_mtx);
347 1.1 pooka }
348 1.1 pooka
349 1.1 pooka /* find affected sockets & process */
350 1.29 pooka error = rumpcomp_sockin_poll(pfds, cursock, POLLTIMEOUT, &rv);
351 1.29 pooka for (i = 0; i < cursock && rv > 0 && error == 0; i++) {
352 1.1 pooka if (pfds[i].revents & POLLIN) {
353 1.1 pooka mutex_enter(&su_mtx);
354 1.1 pooka LIST_FOREACH(su_iter, &su_ent, su_entries) {
355 1.1 pooka if (SO2S(su_iter->su_so)==pfds[i].fd) {
356 1.5 pooka so = su_iter->su_so;
357 1.5 pooka mutex_exit(&su_mtx);
358 1.5 pooka if(so->so_options&SO_ACCEPTCONN)
359 1.5 pooka sockin_accept(so);
360 1.5 pooka else
361 1.5 pooka sockin_process(so);
362 1.5 pooka mutex_enter(&su_mtx);
363 1.1 pooka break;
364 1.1 pooka }
365 1.1 pooka }
366 1.1 pooka /* if we can't find it, just wing it */
367 1.1 pooka KASSERT(rebuild || su_iter);
368 1.1 pooka mutex_exit(&su_mtx);
369 1.1 pooka pfds[i].revents = 0;
370 1.1 pooka rv--;
371 1.1 pooka i = -1;
372 1.1 pooka continue;
373 1.1 pooka }
374 1.1 pooka
375 1.1 pooka /* something else? ignore */
376 1.1 pooka if (pfds[i].revents) {
377 1.1 pooka pfds[i].revents = 0;
378 1.1 pooka rv--;
379 1.1 pooka }
380 1.1 pooka }
381 1.1 pooka KASSERT(rv <= 0);
382 1.1 pooka }
383 1.31 stacktic
384 1.1 pooka }
385 1.1 pooka
386 1.34 stacktic static int
387 1.34 stacktic sockin_do_init(void)
388 1.1 pooka {
389 1.1 pooka int rv;
390 1.1 pooka
391 1.9 pooka if (rump_threads) {
392 1.9 pooka if ((rv = kthread_create(PRI_NONE, 0, NULL, sockinworker,
393 1.9 pooka NULL, NULL, "sockwork")) != 0)
394 1.9 pooka panic("sockin_init: could not create worker thread\n");
395 1.9 pooka } else {
396 1.9 pooka printf("sockin_init: no threads => no worker thread\n");
397 1.9 pooka }
398 1.1 pooka mutex_init(&su_mtx, MUTEX_DEFAULT, IPL_NONE);
399 1.21 pooka strlcpy(sockin_if.if_xname, "sockin0", sizeof(sockin_if.if_xname));
400 1.22 joerg bpf_attach(&sockin_if, DLT_NULL, 0);
401 1.34 stacktic return 0;
402 1.34 stacktic }
403 1.34 stacktic
404 1.34 stacktic static void
405 1.34 stacktic sockin_init(void)
406 1.34 stacktic {
407 1.34 stacktic static ONCE_DECL(init);
408 1.34 stacktic
409 1.34 stacktic RUN_ONCE(&init, sockin_do_init);
410 1.1 pooka }
411 1.1 pooka
412 1.1 pooka static int
413 1.40 rmind sockin_attach(struct socket *so, int proto)
414 1.1 pooka {
415 1.40 rmind const int type = so->so_proto->pr_type;
416 1.40 rmind int error, news, family;
417 1.40 rmind
418 1.40 rmind sosetlock(so);
419 1.40 rmind if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
420 1.40 rmind error = soreserve(so, SOCKIN_SBSIZE, SOCKIN_SBSIZE);
421 1.40 rmind if (error)
422 1.40 rmind return error;
423 1.40 rmind }
424 1.40 rmind
425 1.40 rmind family = so->so_proto->pr_domain->dom_family;
426 1.40 rmind KASSERT(family == PF_INET || family == PF_INET6);
427 1.40 rmind error = rumpcomp_sockin_socket(family, type, 0, &news);
428 1.40 rmind if (error)
429 1.40 rmind return error;
430 1.1 pooka
431 1.40 rmind /* For UDP sockets, make sure we can send/recv maximum. */
432 1.40 rmind if (type == SOCK_DGRAM) {
433 1.40 rmind int sbsize = SOCKIN_SBSIZE;
434 1.40 rmind error = rumpcomp_sockin_setsockopt(news,
435 1.40 rmind SOL_SOCKET, SO_SNDBUF,
436 1.40 rmind &sbsize, sizeof(sbsize));
437 1.40 rmind sbsize = SOCKIN_SBSIZE;
438 1.40 rmind error = rumpcomp_sockin_setsockopt(news,
439 1.40 rmind SOL_SOCKET, SO_RCVBUF,
440 1.40 rmind &sbsize, sizeof(sbsize));
441 1.40 rmind }
442 1.1 pooka
443 1.40 rmind if ((error = registersock(so, news)) != 0)
444 1.40 rmind rumpuser_close(news);
445 1.1 pooka
446 1.40 rmind return error;
447 1.40 rmind }
448 1.17 pooka
449 1.40 rmind static void
450 1.40 rmind sockin_detach(struct socket *so)
451 1.40 rmind {
452 1.40 rmind panic("sockin_detach: IMPLEMENT ME\n");
453 1.40 rmind }
454 1.5 pooka
455 1.40 rmind static int
456 1.41 rtr sockin_ioctl(struct socket *so, struct mbuf *m, struct mbuf *nam,
457 1.41 rtr struct mbuf *control, struct lwp *l)
458 1.41 rtr {
459 1.41 rtr return ENOTTY;
460 1.41 rtr }
461 1.41 rtr
462 1.41 rtr static int
463 1.40 rmind sockin_usrreq(struct socket *so, int req, struct mbuf *m, struct mbuf *nam,
464 1.40 rmind struct mbuf *control, struct lwp *l)
465 1.40 rmind {
466 1.40 rmind int error = 0;
467 1.1 pooka
468 1.41 rtr KASSERT(req != PRU_CONTROL);
469 1.41 rtr
470 1.40 rmind switch (req) {
471 1.5 pooka case PRU_ACCEPT:
472 1.5 pooka /* we do all the work in the worker thread */
473 1.5 pooka break;
474 1.5 pooka
475 1.5 pooka case PRU_BIND:
476 1.29 pooka error = rumpcomp_sockin_bind(SO2S(so),
477 1.27 pooka mtod(nam, const struct sockaddr *),
478 1.31 stacktic nam->m_len);
479 1.5 pooka break;
480 1.5 pooka
481 1.1 pooka case PRU_CONNECT:
482 1.29 pooka error = rumpcomp_sockin_connect(SO2S(so),
483 1.31 stacktic mtod(nam, struct sockaddr *), nam->m_len);
484 1.29 pooka if (error == 0)
485 1.10 pooka soisconnected(so);
486 1.1 pooka break;
487 1.1 pooka
488 1.5 pooka case PRU_LISTEN:
489 1.29 pooka error = rumpcomp_sockin_listen(SO2S(so), so->so_qlimit);
490 1.5 pooka break;
491 1.5 pooka
492 1.1 pooka case PRU_SEND:
493 1.1 pooka {
494 1.14 pooka struct sockaddr *saddr;
495 1.1 pooka struct msghdr mhdr;
496 1.19 tron size_t iov_max, i;
497 1.20 tron struct iovec iov_buf[32], *iov;
498 1.1 pooka struct mbuf *m2;
499 1.30 pooka size_t tot, n;
500 1.19 tron int s;
501 1.1 pooka
502 1.22 joerg bpf_mtap_af(&sockin_if, AF_UNSPEC, m);
503 1.21 pooka
504 1.1 pooka memset(&mhdr, 0, sizeof(mhdr));
505 1.1 pooka
506 1.19 tron iov_max = 0;
507 1.19 tron for (m2 = m; m2 != NULL; m2 = m2->m_next) {
508 1.19 tron iov_max++;
509 1.19 tron }
510 1.19 tron
511 1.20 tron if (iov_max <= __arraycount(iov_buf)) {
512 1.20 tron iov = iov_buf;
513 1.20 tron } else {
514 1.20 tron iov = kmem_alloc(sizeof(struct iovec) * iov_max,
515 1.20 tron KM_SLEEP);
516 1.20 tron }
517 1.19 tron
518 1.1 pooka tot = 0;
519 1.19 tron for (i = 0, m2 = m; m2 != NULL; m2 = m2->m_next, i++) {
520 1.1 pooka iov[i].iov_base = m2->m_data;
521 1.1 pooka iov[i].iov_len = m2->m_len;
522 1.1 pooka tot += m2->m_len;
523 1.1 pooka }
524 1.1 pooka mhdr.msg_iov = iov;
525 1.1 pooka mhdr.msg_iovlen = i;
526 1.3 pooka s = SO2S(so);
527 1.2 pooka
528 1.19 tron if (nam != NULL) {
529 1.14 pooka saddr = mtod(nam, struct sockaddr *);
530 1.14 pooka mhdr.msg_name = saddr;
531 1.14 pooka mhdr.msg_namelen = saddr->sa_len;
532 1.14 pooka }
533 1.14 pooka
534 1.30 pooka rumpcomp_sockin_sendmsg(s, &mhdr, 0, &n);
535 1.2 pooka
536 1.20 tron if (iov != iov_buf)
537 1.20 tron kmem_free(iov, sizeof(struct iovec) * iov_max);
538 1.19 tron
539 1.1 pooka m_freem(m);
540 1.1 pooka m_freem(control);
541 1.9 pooka
542 1.1 pooka /* this assumes too many things to list.. buthey, testing */
543 1.9 pooka if (!rump_threads)
544 1.9 pooka sockin_process(so);
545 1.1 pooka }
546 1.1 pooka break;
547 1.1 pooka
548 1.1 pooka case PRU_SHUTDOWN:
549 1.10 pooka removesock(so);
550 1.10 pooka break;
551 1.10 pooka
552 1.10 pooka case PRU_SOCKADDR:
553 1.10 pooka case PRU_PEERADDR:
554 1.1 pooka {
555 1.10 pooka int slen = nam->m_len;
556 1.27 pooka enum rumpcomp_sockin_getnametype which;
557 1.1 pooka
558 1.10 pooka if (req == PRU_SOCKADDR)
559 1.27 pooka which = RUMPCOMP_SOCKIN_SOCKNAME;
560 1.10 pooka else
561 1.27 pooka which = RUMPCOMP_SOCKIN_PEERNAME;
562 1.29 pooka error = rumpcomp_sockin_getname(SO2S(so),
563 1.29 pooka mtod(nam, struct sockaddr *), &slen, which);
564 1.10 pooka if (error == 0)
565 1.10 pooka nam->m_len = slen;
566 1.10 pooka break;
567 1.1 pooka }
568 1.1 pooka
569 1.1 pooka default:
570 1.1 pooka panic("sockin_usrreq: IMPLEMENT ME, req %d not supported", req);
571 1.1 pooka }
572 1.1 pooka
573 1.1 pooka return error;
574 1.1 pooka }
575 1.7 pooka
576 1.7 pooka static int
577 1.7 pooka sockin_ctloutput(int op, struct socket *so, struct sockopt *sopt)
578 1.7 pooka {
579 1.7 pooka
580 1.29 pooka return rumpcomp_sockin_setsockopt(SO2S(so), sopt->sopt_level,
581 1.29 pooka sopt->sopt_name, sopt->sopt_data, sopt->sopt_size);
582 1.7 pooka }
583 1.37 pooka
584 1.37 pooka int sockin_unavailable(void);
585 1.37 pooka int
586 1.37 pooka sockin_unavailable(void)
587 1.37 pooka {
588 1.37 pooka
589 1.37 pooka panic("interface not available in with sockin");
590 1.37 pooka }
591 1.37 pooka __strong_alias(rtrequest,sockin_unavailable);
592 1.37 pooka __strong_alias(ifunit,sockin_unavailable);
593 1.37 pooka __strong_alias(ifreq_setaddr,sockin_unavailable);
594