1 /* $NetBSD: svr4_net.c,v 1.57 2009/12/09 21:32:58 dsl Exp $ */
4 * Copyright (c) 1994, 2008, 2009 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
33 * Emulate /dev/{udp,tcp,...}
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: svr4_net.c,v 1.57 2009/12/09 21:32:58 dsl Exp $");
41 #include <sys/param.h>
42 #include <sys/kernel.h>
43 #include <sys/systm.h>
45 #include <sys/malloc.h>
46 #include <sys/ioctl.h>
49 #include <sys/filedesc.h>
50 #include <sys/fcntl.h>
51 #include <sys/select.h>
52 #include <sys/socket.h>
53 #include <sys/socketvar.h>
54 #include <sys/protosw.h>
55 #include <sys/domain.h>
57 #include <netinet/in.h>
59 #include <sys/vnode.h>
60 #include <sys/device.h>
62 #include <sys/mount.h>
64 #include <sys/syscallargs.h>
66 #include <compat/svr4/svr4_types.h>
67 #include <compat/svr4/svr4_util.h>
68 #include <compat/svr4/svr4_signal.h>
69 #include <compat/svr4/svr4_lwp.h>
70 #include <compat/svr4/svr4_ucontext.h>
71 #include <compat/svr4/svr4_syscallargs.h>
72 #include <compat/svr4/svr4_ioctl.h>
73 #include <compat/svr4/svr4_stropts.h>
74 #include <compat/svr4/svr4_socket.h>
76 dev_type_open(svr4_netopen
);
78 const struct cdevsw svr4_net_cdevsw
= {
79 svr4_netopen
, noclose
, noread
, nowrite
, noioctl
,
80 nostop
, notty
, nopoll
, nommap
, nokqfilter
, D_OTHER
,
84 * Device minor numbers
95 dev_unix_ord_stream
= 40
98 int svr4_netattach(int);
100 int svr4_soo_close(file_t
*);
102 static const struct fileops svr4_netops
= {
104 .fo_write
= soo_write
,
105 .fo_ioctl
= soo_ioctl
,
106 .fo_fcntl
= soo_fcntl
,
109 .fo_close
= svr4_soo_close
,
110 .fo_kqfilter
= soo_kqfilter
,
111 .fo_restart
= soo_restart
,
116 * Used by new config, but we don't need it.
119 svr4_netattach(int n
)
126 svr4_netopen(dev_t dev
, int flag
, int mode
, struct lwp
*l
)
135 DPRINTF(("netopen("));
137 if (curlwp
->l_dupfd
>= 0) /* XXX */
140 switch (minor(dev
)) {
144 protocol
= IPPROTO_UDP
;
151 protocol
= IPPROTO_TCP
;
159 protocol
= IPPROTO_IP
;
166 protocol
= IPPROTO_ICMP
;
174 DPRINTF(("unix-dgram, "));
177 case dev_unix_stream
:
178 case dev_unix_ord_stream
:
182 DPRINTF(("unix-stream, "));
186 DPRINTF(("%"PRId32
");\n", minor(dev
)));
190 if ((error
= fd_allocfile(&fp
, &fd
)) != 0)
193 if ((error
= socreate(family
, &so
, type
, protocol
, l
, NULL
)) != 0) {
194 DPRINTF(("socreate error %d\n", error
));
195 fd_abort(curproc
, fp
, fd
);
199 error
= fd_clone(fp
, fd
, flag
, &svr4_netops
, so
);
200 fp
->f_type
= DTYPE_SOCKET
;
201 (void)svr4_stream_get(fp
);
209 svr4_soo_close(file_t
*fp
)
211 struct socket
*so
= fp
->f_data
;
213 svr4_delete_socket(curproc
, fp
);
214 free(so
->so_internal
, M_NETADDR
);
215 return soo_close(fp
);
220 svr4_stream_get(file_t
*fp
)
223 struct svr4_strm
*st
;
225 if (fp
== NULL
|| fp
->f_type
!= DTYPE_SOCKET
)
231 return so
->so_internal
;
233 /* Allocate a new one. */
234 fp
->f_ops
= &svr4_netops
;
235 st
= malloc(sizeof(struct svr4_strm
), M_NETADDR
, M_WAITOK
);
236 st
->s_family
= so
->so_proto
->pr_domain
->dom_family
;
240 so
->so_internal
= st
;