xref: /openbsd-src/sys/kern/sys_socket.c (revision 62a742911104f98b9185b2c6b6007d9b1c36396c)
1 /*	$OpenBSD: sys_socket.c,v 1.3 1997/08/31 20:42:23 deraadt Exp $	*/
2 /*	$NetBSD: sys_socket.c,v 1.13 1995/08/12 23:59:09 mycroft Exp $	*/
3 
4 /*
5  * Copyright (c) 1982, 1986, 1990, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. All advertising materials mentioning features or use of this software
17  *    must display the following acknowledgement:
18  *	This product includes software developed by the University of
19  *	California, Berkeley and its contributors.
20  * 4. Neither the name of the University nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  *	@(#)sys_socket.c	8.1 (Berkeley) 6/10/93
37  */
38 
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/file.h>
42 #include <sys/proc.h>
43 #include <sys/mbuf.h>
44 #include <sys/protosw.h>
45 #include <sys/socket.h>
46 #include <sys/socketvar.h>
47 #include <sys/ioctl.h>
48 #include <sys/stat.h>
49 
50 #include <net/if.h>
51 #include <net/route.h>
52 
53 struct	fileops socketops =
54     { soo_read, soo_write, soo_ioctl, soo_select, soo_close };
55 
56 /* ARGSUSED */
57 int
58 soo_read(fp, uio, cred)
59 	struct file *fp;
60 	struct uio *uio;
61 	struct ucred *cred;
62 {
63 
64 	return (soreceive((struct socket *)fp->f_data, (struct mbuf **)0,
65 		uio, (struct mbuf **)0, (struct mbuf **)0, (int *)0));
66 }
67 
68 /* ARGSUSED */
69 int
70 soo_write(fp, uio, cred)
71 	struct file *fp;
72 	struct uio *uio;
73 	struct ucred *cred;
74 {
75 
76 	return (sosend((struct socket *)fp->f_data, (struct mbuf *)0,
77 		uio, (struct mbuf *)0, (struct mbuf *)0, 0));
78 }
79 
80 int
81 soo_ioctl(fp, cmd, data, p)
82 	struct file *fp;
83 	u_long cmd;
84 	register caddr_t data;
85 	struct proc *p;
86 {
87 	register struct socket *so = (struct socket *)fp->f_data;
88 
89 	switch (cmd) {
90 
91 	case FIONBIO:
92 		if (*(int *)data)
93 			so->so_state |= SS_NBIO;
94 		else
95 			so->so_state &= ~SS_NBIO;
96 		return (0);
97 
98 	case FIOASYNC:
99 		if (*(int *)data) {
100 			so->so_state |= SS_ASYNC;
101 			so->so_rcv.sb_flags |= SB_ASYNC;
102 			so->so_snd.sb_flags |= SB_ASYNC;
103 		} else {
104 			so->so_state &= ~SS_ASYNC;
105 			so->so_rcv.sb_flags &= ~SB_ASYNC;
106 			so->so_snd.sb_flags &= ~SB_ASYNC;
107 		}
108 		return (0);
109 
110 	case FIONREAD:
111 		*(int *)data = so->so_rcv.sb_cc;
112 		return (0);
113 
114 	case SIOCSPGRP:
115 		so->so_pgid = *(int *)data;
116 		so->so_siguid = p->p_cred->p_ruid;
117 		so->so_sigeuid = p->p_ucred->cr_uid;
118 		return (0);
119 
120 	case SIOCGPGRP:
121 		*(int *)data = so->so_pgid;
122 		return (0);
123 
124 	case SIOCATMARK:
125 		*(int *)data = (so->so_state&SS_RCVATMARK) != 0;
126 		return (0);
127 	}
128 	/*
129 	 * Interface/routing/protocol specific ioctls:
130 	 * interface and routing ioctls should have a
131 	 * different entry since a socket's unnecessary
132 	 */
133 	if (IOCGROUP(cmd) == 'i')
134 		return (ifioctl(so, cmd, data, p));
135 	if (IOCGROUP(cmd) == 'r')
136 		return (rtioctl(cmd, data, p));
137 	return ((*so->so_proto->pr_usrreq)(so, PRU_CONTROL,
138 	    (struct mbuf *)cmd, (struct mbuf *)data, (struct mbuf *)0));
139 }
140 
141 int
142 soo_select(fp, which, p)
143 	struct file *fp;
144 	int which;
145 	struct proc *p;
146 {
147 	register struct socket *so = (struct socket *)fp->f_data;
148 	register int s = splsoftnet();
149 
150 	switch (which) {
151 
152 	case FREAD:
153 		if (soreadable(so)) {
154 			splx(s);
155 			return (1);
156 		}
157 		selrecord(p, &so->so_rcv.sb_sel);
158 		so->so_rcv.sb_flags |= SB_SEL;
159 		break;
160 
161 	case FWRITE:
162 		if (sowriteable(so)) {
163 			splx(s);
164 			return (1);
165 		}
166 		selrecord(p, &so->so_snd.sb_sel);
167 		so->so_snd.sb_flags |= SB_SEL;
168 		break;
169 
170 	case 0:
171 		if (so->so_oobmark || (so->so_state & SS_RCVATMARK)) {
172 			splx(s);
173 			return (1);
174 		}
175 		selrecord(p, &so->so_rcv.sb_sel);
176 		so->so_rcv.sb_flags |= SB_SEL;
177 		break;
178 	}
179 	splx(s);
180 	return (0);
181 }
182 
183 int
184 soo_stat(so, ub)
185 	register struct socket *so;
186 	register struct stat *ub;
187 {
188 
189 	bzero((caddr_t)ub, sizeof (*ub));
190 	ub->st_mode = S_IFSOCK;
191 	return ((*so->so_proto->pr_usrreq)(so, PRU_SENSE,
192 	    (struct mbuf *)ub, (struct mbuf *)0,
193 	    (struct mbuf *)0));
194 }
195 
196 /* ARGSUSED */
197 int
198 soo_close(fp, p)
199 	struct file *fp;
200 	struct proc *p;
201 {
202 	int error = 0;
203 
204 	if (fp->f_data)
205 		error = soclose((struct socket *)fp->f_data);
206 	fp->f_data = 0;
207 	return (error);
208 }
209