sockin.c revision 1.53 1 1.53 rtr /* $NetBSD: sockin.c,v 1.53 2014/07/30 10:04:26 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.53 rtr __KERNEL_RCSID(0, "$NetBSD: sockin.c,v 1.53 2014/07/30 10:04:26 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.48 rtr static int sockin_accept(struct socket *, struct mbuf *);
72 1.51 rtr static int sockin_bind(struct socket *, struct mbuf *);
73 1.51 rtr static int sockin_listen(struct socket *);
74 1.53 rtr static int sockin_connect(struct socket *, struct mbuf *);
75 1.42 rtr static int sockin_ioctl(struct socket *, u_long, void *, struct ifnet *);
76 1.43 rtr static int sockin_stat(struct socket *, struct stat *);
77 1.47 rtr static int sockin_peeraddr(struct socket *, struct mbuf *);
78 1.47 rtr static int sockin_sockaddr(struct socket *, struct mbuf *);
79 1.50 rtr static int sockin_recvoob(struct socket *, struct mbuf *, int);
80 1.50 rtr static int sockin_sendoob(struct socket *, struct mbuf *, struct mbuf *);
81 1.1 pooka static int sockin_usrreq(struct socket *, int, struct mbuf *,
82 1.1 pooka struct mbuf *, struct mbuf *, struct lwp *);
83 1.7 pooka static int sockin_ctloutput(int op, struct socket *, struct sockopt *);
84 1.1 pooka
85 1.39 rmind static const struct pr_usrreqs sockin_usrreqs = {
86 1.40 rmind .pr_attach = sockin_attach,
87 1.40 rmind .pr_detach = sockin_detach,
88 1.48 rtr .pr_accept = sockin_accept,
89 1.51 rtr .pr_bind = sockin_bind,
90 1.51 rtr .pr_listen = sockin_listen,
91 1.53 rtr .pr_connect = sockin_connect,
92 1.41 rtr .pr_ioctl = sockin_ioctl,
93 1.43 rtr .pr_stat = sockin_stat,
94 1.47 rtr .pr_peeraddr = sockin_peeraddr,
95 1.47 rtr .pr_sockaddr = sockin_sockaddr,
96 1.50 rtr .pr_recvoob = sockin_recvoob,
97 1.50 rtr .pr_sendoob = sockin_sendoob,
98 1.39 rmind .pr_generic = sockin_usrreq,
99 1.39 rmind };
100 1.39 rmind
101 1.1 pooka const struct protosw sockinsw[] = {
102 1.2 pooka {
103 1.2 pooka .pr_type = SOCK_DGRAM,
104 1.1 pooka .pr_domain = &sockindomain,
105 1.1 pooka .pr_protocol = IPPROTO_UDP,
106 1.1 pooka .pr_flags = PR_ATOMIC|PR_ADDR,
107 1.39 rmind .pr_usrreqs = &sockin_usrreqs,
108 1.7 pooka .pr_ctloutput = sockin_ctloutput,
109 1.2 pooka },
110 1.2 pooka {
111 1.2 pooka .pr_type = SOCK_STREAM,
112 1.2 pooka .pr_domain = &sockindomain,
113 1.2 pooka .pr_protocol = IPPROTO_TCP,
114 1.2 pooka .pr_flags = PR_CONNREQUIRED|PR_WANTRCVD|PR_LISTEN|PR_ABRTACPTDIS,
115 1.39 rmind .pr_usrreqs = &sockin_usrreqs,
116 1.7 pooka .pr_ctloutput = sockin_ctloutput,
117 1.1 pooka }};
118 1.31 stacktic const struct protosw sockin6sw[] = {
119 1.31 stacktic {
120 1.31 stacktic .pr_type = SOCK_DGRAM,
121 1.31 stacktic .pr_domain = &sockin6domain,
122 1.31 stacktic .pr_protocol = IPPROTO_UDP,
123 1.31 stacktic .pr_flags = PR_ATOMIC|PR_ADDR,
124 1.39 rmind .pr_usrreqs = &sockin_usrreqs,
125 1.31 stacktic .pr_ctloutput = sockin_ctloutput,
126 1.31 stacktic },
127 1.31 stacktic {
128 1.31 stacktic .pr_type = SOCK_STREAM,
129 1.31 stacktic .pr_domain = &sockin6domain,
130 1.31 stacktic .pr_protocol = IPPROTO_TCP,
131 1.31 stacktic .pr_flags = PR_CONNREQUIRED|PR_WANTRCVD|PR_LISTEN|PR_ABRTACPTDIS,
132 1.39 rmind .pr_usrreqs = &sockin_usrreqs,
133 1.31 stacktic .pr_ctloutput = sockin_ctloutput,
134 1.31 stacktic }};
135 1.1 pooka
136 1.1 pooka struct domain sockindomain = {
137 1.1 pooka .dom_family = PF_INET,
138 1.1 pooka .dom_name = "socket_inet",
139 1.2 pooka .dom_init = sockin_init,
140 1.1 pooka .dom_externalize = NULL,
141 1.1 pooka .dom_dispose = NULL,
142 1.1 pooka .dom_protosw = sockinsw,
143 1.4 minskim .dom_protoswNPROTOSW = &sockinsw[__arraycount(sockinsw)],
144 1.26 dyoung .dom_rtattach = rt_inithead,
145 1.6 pooka .dom_rtoffset = 32,
146 1.6 pooka .dom_maxrtkey = sizeof(struct sockaddr_in),
147 1.1 pooka .dom_ifattach = NULL,
148 1.1 pooka .dom_ifdetach = NULL,
149 1.1 pooka .dom_ifqueues = { NULL },
150 1.1 pooka .dom_link = { NULL },
151 1.1 pooka .dom_mowner = MOWNER_INIT("",""),
152 1.1 pooka .dom_rtcache = { NULL },
153 1.1 pooka .dom_sockaddr_cmp = NULL
154 1.1 pooka };
155 1.31 stacktic struct domain sockin6domain = {
156 1.31 stacktic .dom_family = PF_INET6,
157 1.31 stacktic .dom_name = "socket_inet6",
158 1.31 stacktic .dom_init = sockin_init,
159 1.31 stacktic .dom_externalize = NULL,
160 1.31 stacktic .dom_dispose = NULL,
161 1.31 stacktic .dom_protosw = sockin6sw,
162 1.31 stacktic .dom_protoswNPROTOSW = &sockin6sw[__arraycount(sockin6sw)],
163 1.31 stacktic .dom_rtattach = rt_inithead,
164 1.31 stacktic .dom_rtoffset = 32,
165 1.31 stacktic .dom_maxrtkey = sizeof(struct sockaddr_in6),
166 1.31 stacktic .dom_ifattach = NULL,
167 1.31 stacktic .dom_ifdetach = NULL,
168 1.31 stacktic .dom_ifqueues = { NULL },
169 1.31 stacktic .dom_link = { NULL },
170 1.31 stacktic .dom_mowner = MOWNER_INIT("",""),
171 1.31 stacktic .dom_rtcache = { NULL },
172 1.31 stacktic .dom_sockaddr_cmp = NULL
173 1.31 stacktic };
174 1.1 pooka
175 1.3 pooka #define SO2S(so) ((intptr_t)(so->so_internal))
176 1.2 pooka #define SOCKIN_SBSIZE 65536
177 1.1 pooka
178 1.5 pooka struct sockin_unit {
179 1.5 pooka struct socket *su_so;
180 1.5 pooka
181 1.5 pooka LIST_ENTRY(sockin_unit) su_entries;
182 1.5 pooka };
183 1.5 pooka static LIST_HEAD(, sockin_unit) su_ent = LIST_HEAD_INITIALIZER(su_ent);
184 1.5 pooka static kmutex_t su_mtx;
185 1.5 pooka static bool rebuild;
186 1.5 pooka static int nsock;
187 1.5 pooka
188 1.21 pooka /* XXX: for the bpf hack */
189 1.21 pooka static struct ifnet sockin_if;
190 1.21 pooka int ifpromisc(struct ifnet *ifp, int pswitch) { return 0; }
191 1.21 pooka
192 1.5 pooka static int
193 1.5 pooka registersock(struct socket *so, int news)
194 1.5 pooka {
195 1.5 pooka struct sockin_unit *su;
196 1.5 pooka
197 1.5 pooka su = kmem_alloc(sizeof(*su), KM_NOSLEEP);
198 1.5 pooka if (!su)
199 1.5 pooka return ENOMEM;
200 1.5 pooka
201 1.5 pooka so->so_internal = (void *)(intptr_t)news;
202 1.5 pooka su->su_so = so;
203 1.5 pooka
204 1.5 pooka mutex_enter(&su_mtx);
205 1.5 pooka LIST_INSERT_HEAD(&su_ent, su, su_entries);
206 1.5 pooka nsock++;
207 1.5 pooka rebuild = true;
208 1.5 pooka mutex_exit(&su_mtx);
209 1.5 pooka
210 1.5 pooka return 0;
211 1.5 pooka }
212 1.5 pooka
213 1.1 pooka static void
214 1.10 pooka removesock(struct socket *so)
215 1.10 pooka {
216 1.10 pooka struct sockin_unit *su_iter;
217 1.10 pooka
218 1.10 pooka mutex_enter(&su_mtx);
219 1.10 pooka LIST_FOREACH(su_iter, &su_ent, su_entries) {
220 1.10 pooka if (su_iter->su_so == so)
221 1.10 pooka break;
222 1.10 pooka }
223 1.10 pooka if (!su_iter)
224 1.10 pooka panic("no such socket");
225 1.10 pooka
226 1.10 pooka LIST_REMOVE(su_iter, su_entries);
227 1.10 pooka nsock--;
228 1.10 pooka rebuild = true;
229 1.10 pooka mutex_exit(&su_mtx);
230 1.10 pooka
231 1.29 pooka rumpuser_close(SO2S(su_iter->su_so));
232 1.10 pooka kmem_free(su_iter, sizeof(*su_iter));
233 1.10 pooka }
234 1.10 pooka
235 1.10 pooka static void
236 1.1 pooka sockin_process(struct socket *so)
237 1.1 pooka {
238 1.31 stacktic struct sockaddr_in6 from;
239 1.1 pooka struct iovec io;
240 1.1 pooka struct msghdr rmsg;
241 1.1 pooka struct mbuf *m;
242 1.30 pooka size_t n, plen;
243 1.1 pooka int error;
244 1.1 pooka
245 1.1 pooka m = m_gethdr(M_WAIT, MT_DATA);
246 1.12 pooka if (so->so_proto->pr_type == SOCK_DGRAM) {
247 1.12 pooka plen = IP_MAXPACKET;
248 1.12 pooka MEXTMALLOC(m, plen, M_DONTWAIT);
249 1.12 pooka } else {
250 1.12 pooka plen = MCLBYTES;
251 1.12 pooka MCLGET(m, M_DONTWAIT);
252 1.12 pooka }
253 1.12 pooka if ((m->m_flags & M_EXT) == 0) {
254 1.12 pooka m_freem(m);
255 1.12 pooka return;
256 1.12 pooka }
257 1.1 pooka
258 1.1 pooka memset(&rmsg, 0, sizeof(rmsg));
259 1.1 pooka io.iov_base = mtod(m, void *);
260 1.1 pooka io.iov_len = plen;
261 1.1 pooka rmsg.msg_iov = &io;
262 1.1 pooka rmsg.msg_iovlen = 1;
263 1.1 pooka rmsg.msg_name = (struct sockaddr *)&from;
264 1.1 pooka rmsg.msg_namelen = sizeof(from);
265 1.1 pooka
266 1.30 pooka error = rumpcomp_sockin_recvmsg(SO2S(so), &rmsg, 0, &n);
267 1.33 pooka if (error || n == 0) {
268 1.1 pooka m_freem(m);
269 1.10 pooka
270 1.10 pooka /* Treat a TCP socket a goner */
271 1.16 pooka if (error != EAGAIN && so->so_proto->pr_type == SOCK_STREAM) {
272 1.11 pooka mutex_enter(softnet_lock);
273 1.10 pooka soisdisconnected(so);
274 1.11 pooka mutex_exit(softnet_lock);
275 1.10 pooka removesock(so);
276 1.10 pooka }
277 1.1 pooka return;
278 1.1 pooka }
279 1.1 pooka m->m_len = m->m_pkthdr.len = n;
280 1.1 pooka
281 1.22 joerg bpf_mtap_af(&sockin_if, AF_UNSPEC, m);
282 1.21 pooka
283 1.11 pooka mutex_enter(softnet_lock);
284 1.2 pooka if (so->so_proto->pr_type == SOCK_DGRAM) {
285 1.2 pooka if (!sbappendaddr(&so->so_rcv, rmsg.msg_name, m, NULL)) {
286 1.2 pooka m_freem(m);
287 1.2 pooka }
288 1.2 pooka } else {
289 1.2 pooka sbappendstream(&so->so_rcv, m);
290 1.1 pooka }
291 1.2 pooka
292 1.1 pooka sorwakeup(so);
293 1.11 pooka mutex_exit(softnet_lock);
294 1.1 pooka }
295 1.1 pooka
296 1.5 pooka static void
297 1.49 rtr sockin_waccept(struct socket *so)
298 1.5 pooka {
299 1.5 pooka struct socket *nso;
300 1.31 stacktic struct sockaddr_in6 sin;
301 1.5 pooka int news, error, slen;
302 1.5 pooka
303 1.5 pooka slen = sizeof(sin);
304 1.29 pooka error = rumpcomp_sockin_accept(SO2S(so), (struct sockaddr *)&sin,
305 1.29 pooka &slen, &news);
306 1.29 pooka if (error)
307 1.5 pooka return;
308 1.1 pooka
309 1.11 pooka mutex_enter(softnet_lock);
310 1.35 rmind nso = sonewconn(so, true);
311 1.11 pooka if (nso == NULL)
312 1.5 pooka goto errout;
313 1.5 pooka if (registersock(nso, news) != 0)
314 1.5 pooka goto errout;
315 1.11 pooka mutex_exit(softnet_lock);
316 1.5 pooka return;
317 1.5 pooka
318 1.5 pooka errout:
319 1.29 pooka rumpuser_close(news);
320 1.5 pooka if (nso)
321 1.5 pooka soclose(nso);
322 1.11 pooka mutex_exit(softnet_lock);
323 1.5 pooka }
324 1.1 pooka
325 1.1 pooka #define POLLTIMEOUT 100 /* check for new entries every 100ms */
326 1.1 pooka
327 1.1 pooka /* XXX: doesn't handle socket (kernel) locking properly? */
328 1.1 pooka static void
329 1.1 pooka sockinworker(void *arg)
330 1.1 pooka {
331 1.1 pooka struct pollfd *pfds = NULL, *npfds;
332 1.1 pooka struct sockin_unit *su_iter;
333 1.5 pooka struct socket *so;
334 1.1 pooka int cursock = 0, i, rv, error;
335 1.1 pooka
336 1.1 pooka /*
337 1.1 pooka * Loop reading requests. Check for new sockets periodically
338 1.1 pooka * (could be smarter, but I'm lazy).
339 1.1 pooka */
340 1.1 pooka for (;;) {
341 1.1 pooka if (rebuild) {
342 1.1 pooka npfds = NULL;
343 1.1 pooka mutex_enter(&su_mtx);
344 1.1 pooka if (nsock)
345 1.1 pooka npfds = kmem_alloc(nsock * sizeof(*npfds),
346 1.1 pooka KM_NOSLEEP);
347 1.1 pooka if (npfds || nsock == 0) {
348 1.1 pooka if (pfds)
349 1.1 pooka kmem_free(pfds, cursock*sizeof(*pfds));
350 1.1 pooka pfds = npfds;
351 1.1 pooka cursock = nsock;
352 1.1 pooka rebuild = false;
353 1.1 pooka
354 1.1 pooka i = 0;
355 1.1 pooka LIST_FOREACH(su_iter, &su_ent, su_entries) {
356 1.1 pooka pfds[i].fd = SO2S(su_iter->su_so);
357 1.1 pooka pfds[i].events = POLLIN;
358 1.1 pooka pfds[i].revents = 0;
359 1.1 pooka i++;
360 1.1 pooka }
361 1.1 pooka KASSERT(i == nsock);
362 1.1 pooka }
363 1.1 pooka mutex_exit(&su_mtx);
364 1.1 pooka }
365 1.1 pooka
366 1.1 pooka /* find affected sockets & process */
367 1.29 pooka error = rumpcomp_sockin_poll(pfds, cursock, POLLTIMEOUT, &rv);
368 1.29 pooka for (i = 0; i < cursock && rv > 0 && error == 0; i++) {
369 1.1 pooka if (pfds[i].revents & POLLIN) {
370 1.1 pooka mutex_enter(&su_mtx);
371 1.1 pooka LIST_FOREACH(su_iter, &su_ent, su_entries) {
372 1.1 pooka if (SO2S(su_iter->su_so)==pfds[i].fd) {
373 1.5 pooka so = su_iter->su_so;
374 1.5 pooka mutex_exit(&su_mtx);
375 1.5 pooka if(so->so_options&SO_ACCEPTCONN)
376 1.49 rtr sockin_waccept(so);
377 1.5 pooka else
378 1.5 pooka sockin_process(so);
379 1.5 pooka mutex_enter(&su_mtx);
380 1.1 pooka break;
381 1.1 pooka }
382 1.1 pooka }
383 1.1 pooka /* if we can't find it, just wing it */
384 1.1 pooka KASSERT(rebuild || su_iter);
385 1.1 pooka mutex_exit(&su_mtx);
386 1.1 pooka pfds[i].revents = 0;
387 1.1 pooka rv--;
388 1.1 pooka i = -1;
389 1.1 pooka continue;
390 1.1 pooka }
391 1.1 pooka
392 1.1 pooka /* something else? ignore */
393 1.1 pooka if (pfds[i].revents) {
394 1.1 pooka pfds[i].revents = 0;
395 1.1 pooka rv--;
396 1.1 pooka }
397 1.1 pooka }
398 1.1 pooka KASSERT(rv <= 0);
399 1.1 pooka }
400 1.31 stacktic
401 1.1 pooka }
402 1.1 pooka
403 1.34 stacktic static int
404 1.34 stacktic sockin_do_init(void)
405 1.1 pooka {
406 1.1 pooka int rv;
407 1.1 pooka
408 1.9 pooka if (rump_threads) {
409 1.9 pooka if ((rv = kthread_create(PRI_NONE, 0, NULL, sockinworker,
410 1.9 pooka NULL, NULL, "sockwork")) != 0)
411 1.9 pooka panic("sockin_init: could not create worker thread\n");
412 1.9 pooka } else {
413 1.9 pooka printf("sockin_init: no threads => no worker thread\n");
414 1.9 pooka }
415 1.1 pooka mutex_init(&su_mtx, MUTEX_DEFAULT, IPL_NONE);
416 1.21 pooka strlcpy(sockin_if.if_xname, "sockin0", sizeof(sockin_if.if_xname));
417 1.22 joerg bpf_attach(&sockin_if, DLT_NULL, 0);
418 1.34 stacktic return 0;
419 1.34 stacktic }
420 1.34 stacktic
421 1.34 stacktic static void
422 1.34 stacktic sockin_init(void)
423 1.34 stacktic {
424 1.34 stacktic static ONCE_DECL(init);
425 1.34 stacktic
426 1.34 stacktic RUN_ONCE(&init, sockin_do_init);
427 1.1 pooka }
428 1.1 pooka
429 1.1 pooka static int
430 1.40 rmind sockin_attach(struct socket *so, int proto)
431 1.1 pooka {
432 1.40 rmind const int type = so->so_proto->pr_type;
433 1.40 rmind int error, news, family;
434 1.40 rmind
435 1.40 rmind sosetlock(so);
436 1.40 rmind if (so->so_snd.sb_hiwat == 0 || so->so_rcv.sb_hiwat == 0) {
437 1.40 rmind error = soreserve(so, SOCKIN_SBSIZE, SOCKIN_SBSIZE);
438 1.40 rmind if (error)
439 1.40 rmind return error;
440 1.40 rmind }
441 1.40 rmind
442 1.40 rmind family = so->so_proto->pr_domain->dom_family;
443 1.40 rmind KASSERT(family == PF_INET || family == PF_INET6);
444 1.40 rmind error = rumpcomp_sockin_socket(family, type, 0, &news);
445 1.40 rmind if (error)
446 1.40 rmind return error;
447 1.1 pooka
448 1.40 rmind /* For UDP sockets, make sure we can send/recv maximum. */
449 1.40 rmind if (type == SOCK_DGRAM) {
450 1.40 rmind int sbsize = SOCKIN_SBSIZE;
451 1.40 rmind error = rumpcomp_sockin_setsockopt(news,
452 1.40 rmind SOL_SOCKET, SO_SNDBUF,
453 1.40 rmind &sbsize, sizeof(sbsize));
454 1.40 rmind sbsize = SOCKIN_SBSIZE;
455 1.40 rmind error = rumpcomp_sockin_setsockopt(news,
456 1.40 rmind SOL_SOCKET, SO_RCVBUF,
457 1.40 rmind &sbsize, sizeof(sbsize));
458 1.40 rmind }
459 1.1 pooka
460 1.40 rmind if ((error = registersock(so, news)) != 0)
461 1.40 rmind rumpuser_close(news);
462 1.1 pooka
463 1.40 rmind return error;
464 1.40 rmind }
465 1.17 pooka
466 1.40 rmind static void
467 1.40 rmind sockin_detach(struct socket *so)
468 1.40 rmind {
469 1.40 rmind panic("sockin_detach: IMPLEMENT ME\n");
470 1.40 rmind }
471 1.5 pooka
472 1.40 rmind static int
473 1.48 rtr sockin_accept(struct socket *so, struct mbuf *nam)
474 1.48 rtr {
475 1.48 rtr KASSERT(solocked(so));
476 1.48 rtr
477 1.48 rtr /* we do all the work in the worker thread */
478 1.48 rtr return 0;
479 1.48 rtr }
480 1.48 rtr
481 1.48 rtr static int
482 1.51 rtr sockin_bind(struct socket *so, struct mbuf *nam)
483 1.51 rtr {
484 1.51 rtr KASSERT(solocked(so));
485 1.51 rtr KASSERT(nam != NULL);
486 1.51 rtr
487 1.51 rtr return rumpcomp_sockin_bind(SO2S(so),
488 1.51 rtr mtod(nam, const struct sockaddr *),
489 1.51 rtr nam->m_len);
490 1.51 rtr }
491 1.51 rtr
492 1.51 rtr static int
493 1.51 rtr sockin_listen(struct socket *so)
494 1.51 rtr {
495 1.51 rtr KASSERT(solocked(so));
496 1.51 rtr
497 1.51 rtr return rumpcomp_sockin_listen(SO2S(so), so->so_qlimit);
498 1.51 rtr }
499 1.51 rtr
500 1.51 rtr static int
501 1.53 rtr sockin_connect(struct socket *so, struct mbuf *nam)
502 1.53 rtr {
503 1.53 rtr int error = 0;
504 1.53 rtr
505 1.53 rtr KASSERT(solocked(so));
506 1.53 rtr KASSERT(nam != NULL);
507 1.53 rtr
508 1.53 rtr error = rumpcomp_sockin_connect(SO2S(so),
509 1.53 rtr mtod(nam, struct sockaddr *), nam->m_len);
510 1.53 rtr if (error == 0)
511 1.53 rtr soisconnected(so);
512 1.53 rtr
513 1.53 rtr return error;
514 1.53 rtr }
515 1.53 rtr
516 1.53 rtr
517 1.53 rtr static int
518 1.42 rtr sockin_ioctl(struct socket *so, u_long cmd, void *nam, struct ifnet *ifp)
519 1.41 rtr {
520 1.41 rtr return ENOTTY;
521 1.41 rtr }
522 1.41 rtr
523 1.41 rtr static int
524 1.43 rtr sockin_stat(struct socket *so, struct stat *ub)
525 1.43 rtr {
526 1.46 rtr KASSERT(solocked(so));
527 1.46 rtr
528 1.45 rtr return 0;
529 1.43 rtr }
530 1.43 rtr
531 1.43 rtr static int
532 1.47 rtr sockin_peeraddr(struct socket *so, struct mbuf *nam)
533 1.47 rtr {
534 1.47 rtr KASSERT(solocked(so));
535 1.47 rtr
536 1.47 rtr int error = 0;
537 1.47 rtr int slen = nam->m_len;
538 1.47 rtr
539 1.47 rtr error = rumpcomp_sockin_getname(SO2S(so),
540 1.47 rtr mtod(nam, struct sockaddr *), &slen, RUMPCOMP_SOCKIN_PEERNAME);
541 1.47 rtr if (error == 0)
542 1.47 rtr nam->m_len = slen;
543 1.47 rtr return error;
544 1.47 rtr }
545 1.47 rtr
546 1.47 rtr static int
547 1.47 rtr sockin_sockaddr(struct socket *so, struct mbuf *nam)
548 1.47 rtr {
549 1.47 rtr KASSERT(solocked(so));
550 1.47 rtr
551 1.47 rtr int error = 0;
552 1.47 rtr int slen = nam->m_len;
553 1.47 rtr
554 1.47 rtr error = rumpcomp_sockin_getname(SO2S(so),
555 1.47 rtr mtod(nam, struct sockaddr *), &slen, RUMPCOMP_SOCKIN_SOCKNAME);
556 1.47 rtr if (error == 0)
557 1.47 rtr nam->m_len = slen;
558 1.47 rtr return error;
559 1.47 rtr }
560 1.47 rtr
561 1.47 rtr static int
562 1.50 rtr sockin_recvoob(struct socket *so, struct mbuf *m, int flags)
563 1.50 rtr {
564 1.50 rtr panic("sockin_recvoob: IMPLEMENT ME, recvoob not supported");
565 1.50 rtr }
566 1.50 rtr
567 1.50 rtr static int
568 1.50 rtr sockin_sendoob(struct socket *so, struct mbuf *m, struct mbuf *control)
569 1.50 rtr {
570 1.50 rtr panic("sockin_sendoob: IMPLEMENT ME, sendoob not supported");
571 1.50 rtr }
572 1.50 rtr
573 1.50 rtr static int
574 1.40 rmind sockin_usrreq(struct socket *so, int req, struct mbuf *m, struct mbuf *nam,
575 1.40 rmind struct mbuf *control, struct lwp *l)
576 1.40 rmind {
577 1.40 rmind int error = 0;
578 1.1 pooka
579 1.48 rtr KASSERT(req != PRU_ACCEPT);
580 1.52 rtr KASSERT(req != PRU_BIND);
581 1.52 rtr KASSERT(req != PRU_LISTEN);
582 1.53 rtr KASSERT(req != PRU_CONNECT);
583 1.41 rtr KASSERT(req != PRU_CONTROL);
584 1.43 rtr KASSERT(req != PRU_SENSE);
585 1.47 rtr KASSERT(req != PRU_PEERADDR);
586 1.47 rtr KASSERT(req != PRU_SOCKADDR);
587 1.50 rtr KASSERT(req != PRU_RCVOOB);
588 1.50 rtr KASSERT(req != PRU_SENDOOB);
589 1.41 rtr
590 1.40 rmind switch (req) {
591 1.1 pooka case PRU_SEND:
592 1.1 pooka {
593 1.14 pooka struct sockaddr *saddr;
594 1.1 pooka struct msghdr mhdr;
595 1.19 tron size_t iov_max, i;
596 1.20 tron struct iovec iov_buf[32], *iov;
597 1.1 pooka struct mbuf *m2;
598 1.30 pooka size_t tot, n;
599 1.19 tron int s;
600 1.1 pooka
601 1.22 joerg bpf_mtap_af(&sockin_if, AF_UNSPEC, m);
602 1.21 pooka
603 1.1 pooka memset(&mhdr, 0, sizeof(mhdr));
604 1.1 pooka
605 1.19 tron iov_max = 0;
606 1.19 tron for (m2 = m; m2 != NULL; m2 = m2->m_next) {
607 1.19 tron iov_max++;
608 1.19 tron }
609 1.19 tron
610 1.20 tron if (iov_max <= __arraycount(iov_buf)) {
611 1.20 tron iov = iov_buf;
612 1.20 tron } else {
613 1.20 tron iov = kmem_alloc(sizeof(struct iovec) * iov_max,
614 1.20 tron KM_SLEEP);
615 1.20 tron }
616 1.19 tron
617 1.1 pooka tot = 0;
618 1.19 tron for (i = 0, m2 = m; m2 != NULL; m2 = m2->m_next, i++) {
619 1.1 pooka iov[i].iov_base = m2->m_data;
620 1.1 pooka iov[i].iov_len = m2->m_len;
621 1.1 pooka tot += m2->m_len;
622 1.1 pooka }
623 1.1 pooka mhdr.msg_iov = iov;
624 1.1 pooka mhdr.msg_iovlen = i;
625 1.3 pooka s = SO2S(so);
626 1.2 pooka
627 1.19 tron if (nam != NULL) {
628 1.14 pooka saddr = mtod(nam, struct sockaddr *);
629 1.14 pooka mhdr.msg_name = saddr;
630 1.14 pooka mhdr.msg_namelen = saddr->sa_len;
631 1.14 pooka }
632 1.14 pooka
633 1.30 pooka rumpcomp_sockin_sendmsg(s, &mhdr, 0, &n);
634 1.2 pooka
635 1.20 tron if (iov != iov_buf)
636 1.20 tron kmem_free(iov, sizeof(struct iovec) * iov_max);
637 1.19 tron
638 1.1 pooka m_freem(m);
639 1.1 pooka m_freem(control);
640 1.9 pooka
641 1.1 pooka /* this assumes too many things to list.. buthey, testing */
642 1.9 pooka if (!rump_threads)
643 1.9 pooka sockin_process(so);
644 1.1 pooka }
645 1.1 pooka break;
646 1.1 pooka
647 1.1 pooka case PRU_SHUTDOWN:
648 1.10 pooka removesock(so);
649 1.10 pooka break;
650 1.10 pooka
651 1.1 pooka default:
652 1.1 pooka panic("sockin_usrreq: IMPLEMENT ME, req %d not supported", req);
653 1.1 pooka }
654 1.1 pooka
655 1.1 pooka return error;
656 1.1 pooka }
657 1.7 pooka
658 1.7 pooka static int
659 1.7 pooka sockin_ctloutput(int op, struct socket *so, struct sockopt *sopt)
660 1.7 pooka {
661 1.7 pooka
662 1.29 pooka return rumpcomp_sockin_setsockopt(SO2S(so), sopt->sopt_level,
663 1.29 pooka sopt->sopt_name, sopt->sopt_data, sopt->sopt_size);
664 1.7 pooka }
665 1.37 pooka
666 1.37 pooka int sockin_unavailable(void);
667 1.37 pooka int
668 1.37 pooka sockin_unavailable(void)
669 1.37 pooka {
670 1.37 pooka
671 1.37 pooka panic("interface not available in with sockin");
672 1.37 pooka }
673 1.37 pooka __strong_alias(rtrequest,sockin_unavailable);
674 1.37 pooka __strong_alias(ifunit,sockin_unavailable);
675 1.37 pooka __strong_alias(ifreq_setaddr,sockin_unavailable);
676