2 * Copyright (c) 1998 Mark Newton. All rights reserved.
3 * Copyright (c) 1994, 1996 Christos Zoulas. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Christos Zoulas.
16 * 4. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * Pretend that we have streams...
35 * ToDo: The state machine for getmsg needs re-thinking
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
41 #include "opt_compat.h"
42 #include "opt_ktrace.h"
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/capsicum.h>
47 #include <sys/fcntl.h>
48 #include <sys/filedesc.h>
49 #include <sys/filio.h>
51 #include <sys/malloc.h>
52 #include <sys/file.h> /* Must come after sys/malloc.h */
54 #include <sys/mutex.h>
56 #include <sys/protosw.h>
57 #include <sys/signal.h>
58 #include <sys/signalvar.h>
59 #include <sys/socket.h>
60 #include <sys/socketvar.h>
62 #include <sys/syscallsubr.h>
63 #include <sys/sysproto.h>
65 #include <sys/ktrace.h> /* Must come after sys/uio.h */
68 #include <netinet/in.h>
70 #include <compat/svr4/svr4.h>
71 #include <compat/svr4/svr4_types.h>
72 #include <compat/svr4/svr4_util.h>
73 #include <compat/svr4/svr4_signal.h>
74 #include <compat/svr4/svr4_proto.h>
75 #include <compat/svr4/svr4_stropts.h>
76 #include <compat/svr4/svr4_timod.h>
77 #include <compat/svr4/svr4_sockmod.h>
78 #include <compat/svr4/svr4_ioctl.h>
79 #include <compat/svr4/svr4_socket.h>
82 static int clean_pipe(struct thread *, char *);
83 static void getparm(struct file *, struct svr4_si_sockparms *);
84 static int svr4_do_putmsg(struct thread *, struct svr4_sys_putmsg_args *,
86 static int svr4_do_getmsg(struct thread *, struct svr4_sys_getmsg_args *,
89 /* Address Conversions */
90 static void sockaddr_to_netaddr_in(struct svr4_strmcmd *,
91 const struct sockaddr_in *);
92 static void sockaddr_to_netaddr_un(struct svr4_strmcmd *,
93 const struct sockaddr_un *);
94 static void netaddr_to_sockaddr_in(struct sockaddr_in *,
95 const struct svr4_strmcmd *);
96 static void netaddr_to_sockaddr_un(struct sockaddr_un *,
97 const struct svr4_strmcmd *);
100 static int i_nread(struct file *, struct thread *, register_t *, int,
102 static int i_fdinsert(struct file *, struct thread *, register_t *, int,
104 static int i_str(struct file *, struct thread *, register_t *, int,
106 static int i_setsig(struct file *, struct thread *, register_t *, int,
108 static int i_getsig(struct file *, struct thread *, register_t *, int,
110 static int _i_bind_rsvd(struct file *, struct thread *, register_t *, int,
112 static int _i_rele_rsvd(struct file *, struct thread *, register_t *, int,
115 /* i_str sockmod calls */
116 static int sockmod(struct file *, int, struct svr4_strioctl *,
118 static int si_listen(struct file *, int, struct svr4_strioctl *,
120 static int si_ogetudata(struct file *, int, struct svr4_strioctl *,
122 static int si_sockparams(struct file *, int, struct svr4_strioctl *,
124 static int si_shutdown (struct file *, int, struct svr4_strioctl *,
126 static int si_getudata(struct file *, int, struct svr4_strioctl *,
129 /* i_str timod calls */
130 static int timod(struct file *, int, struct svr4_strioctl *, struct thread *);
131 static int ti_getinfo(struct file *, int, struct svr4_strioctl *,
133 static int ti_bind(struct file *, int, struct svr4_strioctl *, struct thread *);
136 static void bufprint(u_char *, size_t);
137 static int show_ioc(const char *, struct svr4_strioctl *);
138 static int show_strbuf(struct svr4_strbuf *);
139 static void show_msg(const char *, int, struct svr4_strbuf *,
140 struct svr4_strbuf *, int);
150 for (i = 0; i < len; i++) {
151 uprintf("%x ", buf[i]);
152 if (i && (i % 16) == 0)
160 struct svr4_strioctl *ioc;
171 ptr = (u_char *) malloc(len, M_TEMP, M_WAITOK);
172 if ((error = copyin(ioc->buf, ptr, len)) != 0) {
173 free((char *) ptr, M_TEMP);
178 uprintf("%s cmd = %ld, timeout = %d, len = %d, buf = %p { ",
179 str, ioc->cmd, ioc->timeout, ioc->len, ioc->buf);
187 free((char *) ptr, M_TEMP);
194 struct svr4_strbuf *str;
198 int maxlen = str->maxlen;
211 ptr = (u_char *) malloc(len, M_TEMP, M_WAITOK);
213 if ((error = copyin(str->buf, ptr, len)) != 0) {
214 free((char *) ptr, M_TEMP);
219 uprintf(", { %d, %d, %p=[ ", str->maxlen, str->len, str->buf);
227 free((char *) ptr, M_TEMP);
234 show_msg(str, fd, ctl, dat, flags)
237 struct svr4_strbuf *ctl;
238 struct svr4_strbuf *dat;
241 struct svr4_strbuf buf;
244 uprintf("%s(%d", str, fd);
246 if ((error = copyin(ctl, &buf, sizeof(buf))) != 0)
254 if ((error = copyin(dat, &buf, sizeof(buf))) != 0)
261 uprintf(", %x);\n", flags);
264 #endif /* DEBUG_SVR4 */
267 * We are faced with an interesting situation. On svr4 unix sockets
268 * are really pipes. But we really have sockets, and we might as
269 * well use them. At the point where svr4 calls TI_BIND, it has
270 * already created a named pipe for the socket using mknod(2).
271 * We need to create a socket with the same name when we bind,
272 * so we need to remove the pipe before, otherwise we'll get address
273 * already in use. So we *carefully* remove the pipe, to avoid
274 * using this as a random file removal tool. We use system calls
275 * to avoid code duplication.
285 error = kern_statat(td, AT_SYMLINK_NOFOLLOW, AT_FDCWD, path,
286 UIO_SYSSPACE, &st, NULL);
289 * Make sure we are dealing with a mode 0 named pipe.
291 if ((st.st_mode & S_IFMT) != S_IFIFO)
294 if ((st.st_mode & ALLPERMS) != 0)
297 error = kern_unlinkat(td, AT_FDCWD, path, UIO_SYSSPACE, 0);
299 DPRINTF(("clean_pipe: unlink failed %d\n", error));
305 sockaddr_to_netaddr_in(sc, sain)
306 struct svr4_strmcmd *sc;
307 const struct sockaddr_in *sain;
309 struct svr4_netaddr_in *na;
310 na = SVR4_ADDROF(sc);
312 na->family = sain->sin_family;
313 na->port = sain->sin_port;
314 na->addr = sain->sin_addr.s_addr;
315 DPRINTF(("sockaddr_in -> netaddr %d %d %lx\n", na->family, na->port,
321 sockaddr_to_netaddr_un(sc, saun)
322 struct svr4_strmcmd *sc;
323 const struct sockaddr_un *saun;
325 struct svr4_netaddr_un *na;
326 char *dst, *edst = ((char *) sc) + sc->offs + sizeof(na->family) + 1 -
330 na = SVR4_ADDROF(sc);
331 na->family = saun->sun_family;
332 for (src = saun->sun_path, dst = na->path; (*dst++ = *src++) != '\0'; )
335 DPRINTF(("sockaddr_un -> netaddr %d %s\n", na->family, na->path));
340 netaddr_to_sockaddr_in(sain, sc)
341 struct sockaddr_in *sain;
342 const struct svr4_strmcmd *sc;
344 const struct svr4_netaddr_in *na;
347 na = SVR4_C_ADDROF(sc);
348 memset(sain, 0, sizeof(*sain));
349 sain->sin_len = sizeof(*sain);
350 sain->sin_family = na->family;
351 sain->sin_port = na->port;
352 sain->sin_addr.s_addr = na->addr;
353 DPRINTF(("netaddr -> sockaddr_in %d %d %x\n", sain->sin_family,
354 sain->sin_port, sain->sin_addr.s_addr));
359 netaddr_to_sockaddr_un(saun, sc)
360 struct sockaddr_un *saun;
361 const struct svr4_strmcmd *sc;
363 const struct svr4_netaddr_un *na;
364 char *dst, *edst = &saun->sun_path[sizeof(saun->sun_path) - 1];
367 na = SVR4_C_ADDROF(sc);
368 memset(saun, 0, sizeof(*saun));
369 saun->sun_family = na->family;
370 for (src = na->path, dst = saun->sun_path; (*dst++ = *src++) != '\0'; )
373 saun->sun_len = dst - saun->sun_path;
374 DPRINTF(("netaddr -> sockaddr_un %d %s\n", saun->sun_family,
382 struct svr4_si_sockparms *pa;
384 struct svr4_strm *st;
387 st = svr4_stream_get(fp);
393 pa->family = st->s_family;
395 switch (so->so_type) {
397 pa->type = SVR4_T_CLTS;
398 pa->protocol = IPPROTO_UDP;
399 DPRINTF(("getparm(dgram)\n"));
403 pa->type = SVR4_T_COTS; /* What about T_COTS_ORD? XXX */
404 pa->protocol = IPPROTO_IP;
405 DPRINTF(("getparm(stream)\n"));
409 pa->type = SVR4_T_CLTS;
410 pa->protocol = IPPROTO_RAW;
411 DPRINTF(("getparm(raw)\n"));
417 DPRINTF(("getparm(type %d?)\n", so->so_type));
424 si_ogetudata(fp, fd, ioc, td)
427 struct svr4_strioctl *ioc;
431 struct svr4_si_oudata ud;
432 struct svr4_si_sockparms pa;
434 if (ioc->len != sizeof(ud) && ioc->len != sizeof(ud) - sizeof(int)) {
435 DPRINTF(("SI_OGETUDATA: Wrong size %d != %d\n",
436 sizeof(ud), ioc->len));
440 if ((error = copyin(ioc->buf, &ud, sizeof(ud))) != 0)
448 ud.addrsize = sizeof(struct svr4_sockaddr_in);
449 if (pa.type == SVR4_SOCK_STREAM)
462 DPRINTF(("SI_OGETUDATA: Unsupported address family %d\n",
467 /* I have no idea what these should be! */
471 ud.servtype = pa.type;
476 return copyout(&ud, ioc->buf, ioc->len);
481 si_sockparams(fp, fd, ioc, td)
484 struct svr4_strioctl *ioc;
487 struct svr4_si_sockparms pa;
490 return copyout(&pa, ioc->buf, sizeof(pa));
495 si_listen(fp, fd, ioc, td)
498 struct svr4_strioctl *ioc;
502 struct svr4_strm *st = svr4_stream_get(fp);
503 struct svr4_strmcmd lst;
504 struct listen_args la;
509 if (ioc->len < 0 || ioc->len > sizeof(lst))
512 if ((error = copyin(ioc->buf, &lst, ioc->len)) != 0)
515 if (lst.cmd != SVR4_TI_OLD_BIND_REQUEST) {
516 DPRINTF(("si_listen: bad request %ld\n", lst.cmd));
521 * We are making assumptions again...
524 DPRINTF(("SI_LISTEN: fileno %d backlog = %d\n", fd, 5));
527 if ((error = sys_listen(td, &la)) != 0) {
528 DPRINTF(("SI_LISTEN: listen failed %d\n", error));
532 st->s_cmd = SVR4_TI__ACCEPT_WAIT;
533 lst.cmd = SVR4_TI_BIND_REPLY;
535 switch (st->s_family) {
537 /* XXX: Fill the length here */
542 lst.pad[28] = 0x00000000; /* magic again */
543 lst.pad[29] = 0x00000800; /* magic again */
544 lst.pad[30] = 0x80001400; /* magic again */
548 DPRINTF(("SI_LISTEN: Unsupported address family %d\n",
554 if ((error = copyout(&lst, ioc->buf, ioc->len)) != 0)
562 si_getudata(fp, fd, ioc, td)
565 struct svr4_strioctl *ioc;
569 struct svr4_si_udata ud;
571 if (sizeof(ud) != ioc->len) {
572 DPRINTF(("SI_GETUDATA: Wrong size %d != %d\n",
573 sizeof(ud), ioc->len));
577 if ((error = copyin(ioc->buf, &ud, sizeof(ud))) != 0)
580 getparm(fp, &ud.sockparms);
582 switch (ud.sockparms.family) {
584 DPRINTF(("getudata_inet\n"));
587 ud.addrsize = sizeof(struct svr4_sockaddr_in);
588 if (ud.sockparms.type == SVR4_SOCK_STREAM)
596 DPRINTF(("getudata_local\n"));
605 DPRINTF(("SI_GETUDATA: Unsupported address family %d\n",
606 ud.sockparms.family));
611 ud.servtype = ud.sockparms.type;
612 DPRINTF(("ud.servtype = %d\n", ud.servtype));
616 return copyout(&ud, ioc->buf, sizeof(ud));
621 si_shutdown(fp, fd, ioc, td)
624 struct svr4_strioctl *ioc;
628 struct shutdown_args ap;
630 if (ioc->len != sizeof(ap.how)) {
631 DPRINTF(("SI_SHUTDOWN: Wrong size %d != %d\n",
632 sizeof(ap.how), ioc->len));
636 if ((error = copyin(ioc->buf, &ap.how, ioc->len)) != 0)
641 return sys_shutdown(td, &ap);
646 sockmod(fp, fd, ioc, td)
649 struct svr4_strioctl *ioc;
653 case SVR4_SI_OGETUDATA:
654 DPRINTF(("SI_OGETUDATA\n"));
655 return si_ogetudata(fp, fd, ioc, td);
657 case SVR4_SI_SHUTDOWN:
658 DPRINTF(("SI_SHUTDOWN\n"));
659 return si_shutdown(fp, fd, ioc, td);
662 DPRINTF(("SI_LISTEN\n"));
663 return si_listen(fp, fd, ioc, td);
665 case SVR4_SI_SETMYNAME:
666 DPRINTF(("SI_SETMYNAME\n"));
669 case SVR4_SI_SETPEERNAME:
670 DPRINTF(("SI_SETPEERNAME\n"));
673 case SVR4_SI_GETINTRANSIT:
674 DPRINTF(("SI_GETINTRANSIT\n"));
677 case SVR4_SI_TCL_LINK:
678 DPRINTF(("SI_TCL_LINK\n"));
681 case SVR4_SI_TCL_UNLINK:
682 DPRINTF(("SI_TCL_UNLINK\n"));
685 case SVR4_SI_SOCKPARAMS:
686 DPRINTF(("SI_SOCKPARAMS\n"));
687 return si_sockparams(fp, fd, ioc, td);
689 case SVR4_SI_GETUDATA:
690 DPRINTF(("SI_GETUDATA\n"));
691 return si_getudata(fp, fd, ioc, td);
694 DPRINTF(("Unknown sockmod ioctl %lx\n", ioc->cmd));
702 ti_getinfo(fp, fd, ioc, td)
705 struct svr4_strioctl *ioc;
709 struct svr4_infocmd info;
711 memset(&info, 0, sizeof(info));
713 if (ioc->len < 0 || ioc->len > sizeof(info))
716 if ((error = copyin(ioc->buf, &info, ioc->len)) != 0)
719 if (info.cmd != SVR4_TI_INFO_REQUEST)
722 info.cmd = SVR4_TI_INFO_REPLY;
734 ioc->len = sizeof(info);
735 if ((error = copyout(&info, ioc->buf, ioc->len)) != 0)
743 ti_bind(fp, fd, ioc, td)
746 struct svr4_strioctl *ioc;
750 struct svr4_strm *st = svr4_stream_get(fp);
751 struct sockaddr_in sain;
752 struct sockaddr_un saun;
753 struct sockaddr *skp;
755 struct svr4_strmcmd bnd;
758 DPRINTF(("ti_bind: bad file descriptor\n"));
762 if (ioc->len < 0 || ioc->len > sizeof(bnd))
765 if ((error = copyin(ioc->buf, &bnd, ioc->len)) != 0)
768 if (bnd.cmd != SVR4_TI_OLD_BIND_REQUEST) {
769 DPRINTF(("ti_bind: bad request %ld\n", bnd.cmd));
773 switch (st->s_family) {
775 skp = (struct sockaddr *)&sain;
776 sasize = sizeof(sain);
781 netaddr_to_sockaddr_in(&sain, &bnd);
783 DPRINTF(("TI_BIND: fam %d, port %d, addr %x\n",
784 sain.sin_family, sain.sin_port,
785 sain.sin_addr.s_addr));
789 skp = (struct sockaddr *)&saun;
790 sasize = sizeof(saun);
794 netaddr_to_sockaddr_un(&saun, &bnd);
796 if (saun.sun_path[0] == '\0')
799 DPRINTF(("TI_BIND: fam %d, path %s\n",
800 saun.sun_family, saun.sun_path));
802 if ((error = clean_pipe(td, saun.sun_path)) != 0)
805 bnd.pad[28] = 0x00001000; /* magic again */
809 DPRINTF(("TI_BIND: Unsupported address family %d\n",
814 DPRINTF(("TI_BIND: fileno %d\n", fd));
816 if ((error = kern_bindat(td, AT_FDCWD, fd, skp)) != 0) {
817 DPRINTF(("TI_BIND: bind failed %d\n", error));
823 memset(&bnd, 0, sizeof(bnd));
824 bnd.len = sasize + 4;
825 bnd.offs = 0x10; /* XXX */
828 bnd.cmd = SVR4_TI_BIND_REPLY;
830 if ((error = copyout(&bnd, ioc->buf, ioc->len)) != 0)
838 timod(fp, fd, ioc, td)
841 struct svr4_strioctl *ioc;
845 case SVR4_TI_GETINFO:
846 DPRINTF(("TI_GETINFO\n"));
847 return ti_getinfo(fp, fd, ioc, td);
849 case SVR4_TI_OPTMGMT:
850 DPRINTF(("TI_OPTMGMT\n"));
854 DPRINTF(("TI_BIND\n"));
855 return ti_bind(fp, fd, ioc, td);
858 DPRINTF(("TI_UNBIND\n"));
862 DPRINTF(("Unknown timod ioctl %lx\n", ioc->cmd));
869 svr4_stream_ti_ioctl(fp, td, retval, fd, cmd, dat)
877 struct svr4_strbuf skb, *sub = (struct svr4_strbuf *) dat;
878 struct svr4_strm *st = svr4_stream_get(fp);
881 socklen_t sasize, oldsasize;
882 struct svr4_strmcmd sc;
884 DPRINTF(("svr4_stream_ti_ioctl\n"));
891 if ((error = copyin(sub, &skb, sizeof(skb))) != 0) {
892 DPRINTF(("ti_ioctl: error copying in strbuf\n"));
896 switch (st->s_family) {
898 sasize = sizeof(struct sockaddr_in);
902 sasize = sizeof(struct sockaddr_un);
906 DPRINTF(("ti_ioctl: Unsupported address family %d\n",
913 case SVR4_TI_GETMYNAME:
914 DPRINTF(("TI_GETMYNAME\n"));
916 error = kern_getsockname(td, fd, &sa, &sasize);
918 DPRINTF(("ti_ioctl: getsockname error\n"));
924 case SVR4_TI_GETPEERNAME:
925 DPRINTF(("TI_GETPEERNAME\n"));
927 error = kern_getpeername(td, fd, &sa, &sasize);
929 DPRINTF(("ti_ioctl: getpeername error\n"));
935 case SVR4_TI_SETMYNAME:
936 DPRINTF(("TI_SETMYNAME\n"));
939 case SVR4_TI_SETPEERNAME:
940 DPRINTF(("TI_SETPEERNAME\n"));
943 DPRINTF(("ti_ioctl: Unknown ioctl %lx\n", cmd));
947 if (sasize < 0 || sasize > oldsasize) {
952 switch (st->s_family) {
954 sockaddr_to_netaddr_in(&sc, (struct sockaddr_in *)sa);
959 sockaddr_to_netaddr_un(&sc, (struct sockaddr_un *)sa);
960 skb.len = sasize + 4;
969 if ((error = copyout(SVR4_ADDROF(&sc), skb.buf, sasize)) != 0) {
970 DPRINTF(("ti_ioctl: error copying out socket data\n"));
975 if ((error = copyout(&skb, sub, sizeof(skb))) != 0) {
976 DPRINTF(("ti_ioctl: error copying out strbuf\n"));
987 i_nread(fp, td, retval, fd, cmd, dat)
999 * We are supposed to return the message length in nread, and the
1000 * number of messages in retval. We don't have the notion of number
1001 * of stream messages, so we just find out if we have any bytes waiting
1002 * for us, and if we do, then we assume that we have at least one
1003 * message waiting for us.
1005 if ((error = fo_ioctl(fp, FIONREAD, (caddr_t) &nread, td->td_ucred,
1014 return copyout(&nread, dat, sizeof(nread));
1018 i_fdinsert(fp, td, retval, fd, cmd, dat)
1027 * Major hack again here. We assume that we are using this to
1028 * implement accept(2). If that is the case, we have already
1029 * called accept, and we have stored the file descriptor in
1030 * afd. We find the file descriptor that the code wants to use
1031 * in fd insert, and then we dup2() our accepted file descriptor
1035 struct svr4_strm *st = svr4_stream_get(fp);
1036 struct svr4_strfdinsert fdi;
1037 struct dup2_args d2p;
1040 DPRINTF(("fdinsert: bad file type\n"));
1045 if (st->s_afd == -1) {
1046 DPRINTF(("fdinsert: accept fd not found\n"));
1051 if ((error = copyin(dat, &fdi, sizeof(fdi))) != 0) {
1052 DPRINTF(("fdinsert: copyin failed %d\n", error));
1057 d2p.from = st->s_afd;
1060 if ((error = sys_dup2(td, &d2p)) != 0) {
1061 DPRINTF(("fdinsert: dup2(%d, %d) failed %d\n",
1062 st->s_afd, fdi.fd, error));
1067 if ((error = kern_close(td, st->s_afd)) != 0) {
1068 DPRINTF(("fdinsert: close(%d) failed %d\n",
1083 _i_bind_rsvd(fp, td, retval, fd, cmd, dat)
1091 struct mkfifo_args ap;
1094 * This is a supposed to be a kernel and library only ioctl.
1095 * It gets called before ti_bind, when we have a unix
1096 * socket, to physically create the socket transport and
1097 * ``reserve'' it. I don't know how this get reserved inside
1098 * the kernel, but we are going to create it nevertheless.
1103 return sys_mkfifo(td, &ap);
1107 _i_rele_rsvd(fp, td, retval, fd, cmd, dat)
1115 struct unlink_args ap;
1118 * This is a supposed to be a kernel and library only ioctl.
1119 * I guess it is supposed to release the socket.
1123 return sys_unlink(td, &ap);
1127 i_str(fp, td, retval, fd, cmd, dat)
1136 struct svr4_strioctl ioc;
1138 if ((error = copyin(dat, &ioc, sizeof(ioc))) != 0)
1142 if ((error = show_ioc(">", &ioc)) != 0)
1144 #endif /* DEBUG_SVR4 */
1146 switch (ioc.cmd & 0xff00) {
1148 if ((error = sockmod(fp, fd, &ioc, td)) != 0)
1153 if ((error = timod(fp, fd, &ioc, td)) != 0)
1158 DPRINTF(("Unimplemented module %c %ld\n",
1159 (char) (cmd >> 8), cmd & 0xff));
1164 if ((error = show_ioc("<", &ioc)) != 0)
1166 #endif /* DEBUG_SVR4 */
1167 return copyout(&ioc, dat, sizeof(ioc));
1171 i_setsig(fp, td, retval, fd, cmd, dat)
1180 * This is the best we can do for now; we cannot generate
1181 * signals only for specific events so the signal mask gets
1182 * ignored; we save it just to pass it to a possible I_GETSIG...
1184 * We alse have to fix the O_ASYNC fcntl bit, so the
1185 * process will get SIGPOLLs.
1188 register_t oflags, flags;
1189 struct svr4_strm *st = svr4_stream_get(fp);
1192 DPRINTF(("i_setsig: bad file descriptor\n"));
1195 /* get old status flags */
1196 error = kern_fcntl(td, fd, F_GETFL, 0);
1200 oflags = td->td_retval[0];
1202 /* update the flags */
1207 flags = oflags | O_ASYNC;
1208 if ((error = copyin(dat, &mask, sizeof(mask))) != 0) {
1209 DPRINTF(("i_setsig: bad eventmask pointer\n"));
1212 if (mask & SVR4_S_ALLMASK) {
1213 DPRINTF(("i_setsig: bad eventmask data %x\n", mask));
1216 st->s_eventmask = mask;
1219 flags = oflags & ~O_ASYNC;
1220 st->s_eventmask = 0;
1224 /* set the new flags, if changed */
1225 if (flags != oflags) {
1226 error = kern_fcntl(td, fd, F_SETFL, flags);
1229 flags = td->td_retval[0];
1232 /* set up SIGIO receiver if needed */
1234 return (kern_fcntl(td, fd, F_SETOWN, td->td_proc->p_pid));
1239 i_getsig(fp, td, retval, fd, cmd, dat)
1247 int error, eventmask;
1250 struct svr4_strm *st = svr4_stream_get(fp);
1253 DPRINTF(("i_getsig: bad file descriptor\n"));
1257 eventmask = st->s_eventmask;
1259 if ((error = copyout(&eventmask, dat,
1260 sizeof(eventmask))) != 0) {
1261 DPRINTF(("i_getsig: bad eventmask pointer\n"));
1269 svr4_stream_ioctl(fp, td, retval, fd, cmd, dat)
1280 * All the following stuff assumes "sockmod" is pushed...
1284 DPRINTF(("I_NREAD\n"));
1285 return i_nread(fp, td, retval, fd, cmd, dat);
1288 DPRINTF(("I_PUSH %p\n", dat));
1289 #if defined(DEBUG_SVR4)
1290 show_strbuf((struct svr4_strbuf *)dat);
1295 DPRINTF(("I_POP\n"));
1299 DPRINTF(("I_LOOK\n"));
1303 DPRINTF(("I_FLUSH\n"));
1307 DPRINTF(("I_SRDOPT\n"));
1311 DPRINTF(("I_GRDOPT\n"));
1315 DPRINTF(("I_STR\n"));
1316 return i_str(fp, td, retval, fd, cmd, dat);
1319 DPRINTF(("I_SETSIG\n"));
1320 return i_setsig(fp, td, retval, fd, cmd, dat);
1323 DPRINTF(("I_GETSIG\n"));
1324 return i_getsig(fp, td, retval, fd, cmd, dat);
1327 DPRINTF(("I_FIND\n"));
1329 * Here we are not pushing modules really, we just
1330 * pretend all are present
1336 DPRINTF(("I_LINK\n"));
1340 DPRINTF(("I_UNLINK\n"));
1343 case SVR4_I_ERECVFD:
1344 DPRINTF(("I_ERECVFD\n"));
1348 DPRINTF(("I_PEEK\n"));
1351 case SVR4_I_FDINSERT:
1352 DPRINTF(("I_FDINSERT\n"));
1353 return i_fdinsert(fp, td, retval, fd, cmd, dat);
1356 DPRINTF(("I_SENDFD\n"));
1360 DPRINTF(("I_RECVFD\n"));
1364 DPRINTF(("I_SWROPT\n"));
1368 DPRINTF(("I_GWROPT\n"));
1372 DPRINTF(("I_LIST\n"));
1376 DPRINTF(("I_PLINK\n"));
1379 case SVR4_I_PUNLINK:
1380 DPRINTF(("I_PUNLINK\n"));
1384 DPRINTF(("I_SETEV\n"));
1388 DPRINTF(("I_GETEV\n"));
1392 DPRINTF(("I_STREV\n"));
1395 case SVR4_I_UNSTREV:
1396 DPRINTF(("I_UNSTREV\n"));
1399 case SVR4_I_FLUSHBAND:
1400 DPRINTF(("I_FLUSHBAND\n"));
1404 DPRINTF(("I_CKBAND\n"));
1407 case SVR4_I_GETBAND:
1408 DPRINTF(("I_GETBANK\n"));
1412 DPRINTF(("I_ATMARK\n"));
1415 case SVR4_I_SETCLTIME:
1416 DPRINTF(("I_SETCLTIME\n"));
1419 case SVR4_I_GETCLTIME:
1420 DPRINTF(("I_GETCLTIME\n"));
1424 DPRINTF(("I_CANPUT\n"));
1427 case SVR4__I_BIND_RSVD:
1428 DPRINTF(("_I_BIND_RSVD\n"));
1429 return _i_bind_rsvd(fp, td, retval, fd, cmd, dat);
1431 case SVR4__I_RELE_RSVD:
1432 DPRINTF(("_I_RELE_RSVD\n"));
1433 return _i_rele_rsvd(fp, td, retval, fd, cmd, dat);
1436 DPRINTF(("unimpl cmd = %lx\n", cmd));
1446 svr4_sys_putmsg(td, uap)
1448 struct svr4_sys_putmsg_args *uap;
1450 cap_rights_t rights;
1454 error = fget(td, uap->fd, cap_rights_init(&rights, CAP_SEND), &fp);
1457 uprintf("putmsg: bad fp\n");
1461 error = svr4_do_putmsg(td, uap, fp);
1467 svr4_do_putmsg(td, uap, fp)
1469 struct svr4_sys_putmsg_args *uap;
1472 struct svr4_strbuf dat, ctl;
1473 struct svr4_strmcmd sc;
1474 struct sockaddr_in sain;
1475 struct sockaddr_un saun;
1476 struct sockaddr *sa;
1477 int sasize, *retval;
1478 struct svr4_strm *st;
1481 retval = td->td_retval;
1484 show_msg(">putmsg", uap->fd, uap->ctl,
1485 uap->dat, uap->flags);
1486 #endif /* DEBUG_SVR4 */
1488 if (uap->ctl != NULL) {
1489 if ((error = copyin(uap->ctl, &ctl, sizeof(ctl))) != 0) {
1491 uprintf("putmsg: copyin(): %d\n", error);
1499 if (uap->dat != NULL) {
1500 if ((error = copyin(uap->dat, &dat, sizeof(dat))) != 0) {
1502 uprintf("putmsg: copyin(): %d (2)\n", error);
1511 * Only for sockets for now.
1513 if ((st = svr4_stream_get(fp)) == NULL) {
1514 DPRINTF(("putmsg: bad file type\n"));
1518 if (ctl.len < 0 || ctl.len > sizeof(sc)) {
1519 DPRINTF(("putmsg: Bad control size %d != %d\n", ctl.len,
1520 sizeof(struct svr4_strmcmd)));
1524 if ((error = copyin(ctl.buf, &sc, ctl.len)) != 0)
1527 switch (st->s_family) {
1529 if (sc.len != sizeof(sain)) {
1530 if (sc.cmd == SVR4_TI_DATA_REQUEST) {
1531 struct write_args wa;
1533 /* Solaris seems to use sc.cmd = 3 to
1534 * send "expedited" data. telnet uses
1535 * this for options processing, sending EOF,
1536 * etc. I'm sure other things use it too.
1537 * I don't have any documentation
1538 * on it, so I'm making a guess that this
1539 * is how it works. newton@atdot.dotat.org XXX
1541 DPRINTF(("sending expedited data ??\n"));
1545 return sys_write(td, &wa);
1547 DPRINTF(("putmsg: Invalid inet length %ld\n", sc.len));
1550 netaddr_to_sockaddr_in(&sain, &sc);
1551 sa = (struct sockaddr *)&sain;
1552 sasize = sizeof(sain);
1553 if (sain.sin_family != st->s_family)
1559 /* We are doing an accept; succeed */
1560 DPRINTF(("putmsg: Do nothing\n"));
1565 /* Maybe we've been given a device/inode pair */
1566 dev_t *dev = SVR4_ADDROF(&sc);
1567 ino_t *ino = (ino_t *) &dev[1];
1568 if (svr4_find_socket(td, fp, *dev, *ino, &saun) != 0) {
1569 /* I guess we have it by name */
1570 netaddr_to_sockaddr_un(&saun, &sc);
1572 sa = (struct sockaddr *)&saun;
1573 sasize = sizeof(saun);
1578 DPRINTF(("putmsg: Unsupported address family %d\n",
1587 case SVR4_TI_CONNECT_REQUEST: /* connect */
1590 return (kern_connectat(td, AT_FDCWD, uap->fd, sa));
1593 case SVR4_TI_SENDTO_REQUEST: /* sendto */
1599 msg.msg_namelen = sasize;
1600 msg.msg_iov = &aiov;
1602 msg.msg_control = 0;
1604 aiov.iov_base = dat.buf;
1605 aiov.iov_len = dat.len;
1606 error = kern_sendit(td, uap->fd, &msg, uap->flags,
1607 NULL, UIO_USERSPACE);
1608 DPRINTF(("sendto_request error: %d\n", error));
1614 DPRINTF(("putmsg: Unimplemented command %lx\n", sc.cmd));
1620 svr4_sys_getmsg(td, uap)
1622 struct svr4_sys_getmsg_args *uap;
1624 cap_rights_t rights;
1628 error = fget(td, uap->fd, cap_rights_init(&rights, CAP_RECV), &fp);
1631 uprintf("getmsg: bad fp\n");
1635 error = svr4_do_getmsg(td, uap, fp);
1641 svr4_do_getmsg(td, uap, fp)
1643 struct svr4_sys_getmsg_args *uap;
1646 struct svr4_strbuf dat, ctl;
1647 struct svr4_strmcmd sc;
1651 struct sockaddr_in sain;
1652 struct sockaddr_un saun;
1653 struct sockaddr *sa;
1655 struct svr4_strm *st;
1659 retval = td->td_retval;
1663 memset(&sc, 0, sizeof(sc));
1666 show_msg(">getmsg", uap->fd, uap->ctl,
1668 #endif /* DEBUG_SVR4 */
1670 if (uap->ctl != NULL) {
1671 if ((error = copyin(uap->ctl, &ctl, sizeof(ctl))) != 0)
1681 if (uap->dat != NULL) {
1682 if ((error = copyin(uap->dat, &dat, sizeof(dat))) != 0)
1691 * Only for sockets for now.
1693 if ((st = svr4_stream_get(fp)) == NULL) {
1694 DPRINTF(("getmsg: bad file type\n"));
1698 if (ctl.maxlen == -1 || dat.maxlen == -1) {
1699 DPRINTF(("getmsg: Cannot handle -1 maxlen (yet)\n"));
1703 switch (st->s_family) {
1705 sasize = sizeof(sain);
1709 sasize = sizeof(saun);
1713 DPRINTF(("getmsg: Unsupported address family %d\n",
1719 switch (st->s_cmd) {
1720 case SVR4_TI_CONNECT_REQUEST:
1721 DPRINTF(("getmsg: TI_CONNECT_REQUEST\n"));
1723 * We do the connect in one step, so the putmsg should
1724 * have gotten the error.
1726 sc.cmd = SVR4_TI_OK_REPLY;
1735 case SVR4_TI_OK_REPLY:
1736 DPRINTF(("getmsg: TI_OK_REPLY\n"));
1738 * We are immediately after a connect reply, so we send
1739 * a connect verification.
1742 error = kern_getpeername(td, uap->fd, &sa, &sasize);
1745 DPRINTF(("getmsg: getpeername failed %d\n", error));
1749 sc.cmd = SVR4_TI_CONNECT_REPLY;
1753 sc.pad[2] = 0x04000402;
1755 switch (st->s_family) {
1758 sockaddr_to_netaddr_in(&sc, (struct sockaddr_in *)sa);
1762 sc.len = sasize + 4;
1763 sockaddr_to_netaddr_un(&sc, (struct sockaddr_un *)sa);
1779 case SVR4_TI__ACCEPT_OK:
1780 DPRINTF(("getmsg: TI__ACCEPT_OK\n"));
1782 * We do the connect in one step, so the putmsg should
1783 * have gotten the error.
1785 sc.cmd = SVR4_TI_OK_REPLY;
1791 st->s_cmd = SVR4_TI__ACCEPT_WAIT;
1794 case SVR4_TI__ACCEPT_WAIT:
1795 DPRINTF(("getmsg: TI__ACCEPT_WAIT\n"));
1797 * We are after a listen, so we try to accept...
1800 error = kern_accept(td, uap->fd, &sa, &sasize, &afp);
1803 DPRINTF(("getmsg: accept failed %d\n", error));
1807 st->s_afd = *retval;
1809 DPRINTF(("getmsg: Accept fd = %d\n", st->s_afd));
1811 sc.cmd = SVR4_TI_ACCEPT_REPLY;
1815 switch (st->s_family) {
1818 sockaddr_to_netaddr_in(&sc, (struct sockaddr_in *)&sa);
1824 sc.pad[1] = 0x00010000;
1825 sc.pad[2] = 0xf6bcdaa0; /* I don't know what that is */
1826 sc.pad[3] = 0x00010000;
1828 sc.len = sasize + 4;
1832 fdclose(td, afp, st->s_afd);
1843 st->s_cmd = SVR4_TI__ACCEPT_OK;
1846 case SVR4_TI_SENDTO_REQUEST:
1847 DPRINTF(("getmsg: TI_SENDTO_REQUEST\n"));
1848 if (ctl.maxlen > 36 && ctl.len < 36)
1851 if (ctl.len > sizeof(sc))
1852 ctl.len = sizeof(sc);
1854 if ((error = copyin(ctl.buf, &sc, ctl.len)) != 0) {
1859 switch (st->s_family) {
1861 sa = (struct sockaddr *)&sain;
1862 sockaddr_to_netaddr_in(&sc, &sain);
1866 sa = (struct sockaddr *)&saun;
1867 sockaddr_to_netaddr_un(&sc, &saun);
1876 msg.msg_namelen = sasize;
1877 msg.msg_iov = &aiov;
1879 msg.msg_control = 0;
1880 aiov.iov_base = dat.buf;
1881 aiov.iov_len = dat.maxlen;
1884 error = kern_recvit(td, uap->fd, &msg, UIO_SYSSPACE, NULL);
1888 DPRINTF(("getmsg: recvit failed %d\n", error));
1892 sc.cmd = SVR4_TI_RECVFROM_IND;
1894 switch (st->s_family) {
1897 sockaddr_to_netaddr_in(&sc, &sain);
1901 sc.len = sasize + 4;
1902 sockaddr_to_netaddr_un(&sc, &saun);
1917 if (st->s_cmd == SVR4_TI_CONNECT_REQUEST) {
1918 struct read_args ra;
1920 /* More weirdness: Again, I can't find documentation
1921 * to back this up, but when a process does a generic
1922 * "getmsg()" call it seems that the command field is
1923 * zero and the length of the data area is zero. I
1924 * think processes expect getmsg() to fill in dat.len
1925 * after reading at most dat.maxlen octets from the
1926 * stream. Since we're using sockets I can let
1927 * read() look after it and frob return values
1928 * appropriately (or inappropriately :-)
1929 * -- newton@atdot.dotat.org XXX
1933 ra.nbyte = dat.maxlen;
1934 if ((error = sys_read(td, &ra)) != 0) {
1940 st->s_cmd = SVR4_TI_SENDTO_REQUEST;
1944 DPRINTF(("getmsg: Unknown state %x\n", st->s_cmd));
1949 if (ctl.len > sizeof(sc))
1950 ctl.len = sizeof(sc);
1952 error = copyout(&sc, ctl.buf, ctl.len);
1955 error = copyout(&ctl, uap->ctl, sizeof(ctl));
1960 error = copyout(&dat, uap->dat, sizeof(dat));
1963 if (uap->flags) { /* XXX: Need translation */
1965 error = copyout(&fl, uap->flags, sizeof(fl));
1970 fdclose(td, afp, st->s_afd);
1984 show_msg("<getmsg", uap->fd, uap->ctl,
1986 #endif /* DEBUG_SVR4 */
1990 int svr4_sys_send(td, uap)
1992 struct svr4_sys_send_args *uap;
1994 struct sendto_args sta;
1999 sta.flags = uap->flags;
2003 return (sys_sendto(td, &sta));
2006 int svr4_sys_recv(td, uap)
2008 struct svr4_sys_recv_args *uap;
2010 struct recvfrom_args rfa;
2015 rfa.flags = uap->flags;
2017 rfa.fromlenaddr = NULL;
2019 return (sys_recvfrom(td, &rfa));
2023 * XXX This isn't necessary, but it's handy for inserting debug code into
2024 * sendto(). Let's leave it here for now...
2027 svr4_sys_sendto(td, uap)
2029 struct svr4_sys_sendto_args *uap;
2031 struct sendto_args sa;
2036 sa.flags = uap->flags;
2037 sa.to = (caddr_t)uap->to;
2038 sa.tolen = uap->tolen;
2040 DPRINTF(("calling sendto()\n"));
2041 return sys_sendto(td, &sa);