2 * Copyright (c) 1982, 1986, 1990, 1993
3 * The Regents of the University of California. 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 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * @(#)sys_socket.c 8.1 (Berkeley) 6/10/93
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/domain.h>
39 #include <sys/filedesc.h>
40 #include <sys/malloc.h>
42 #include <sys/protosw.h>
43 #include <sys/sigio.h>
44 #include <sys/signal.h>
45 #include <sys/signalvar.h>
46 #include <sys/socket.h>
47 #include <sys/socketvar.h>
48 #include <sys/filio.h> /* XXX */
49 #include <sys/sockio.h>
52 #include <sys/ucred.h>
54 #include <sys/unpcb.h>
58 #include <net/if_var.h>
59 #include <net/route.h>
62 #include <netinet/in.h>
63 #include <netinet/in_pcb.h>
65 #include <security/mac/mac_framework.h>
67 static fo_rdwr_t soo_read;
68 static fo_rdwr_t soo_write;
69 static fo_ioctl_t soo_ioctl;
70 static fo_poll_t soo_poll;
71 extern fo_kqfilter_t soo_kqfilter;
72 static fo_stat_t soo_stat;
73 static fo_close_t soo_close;
74 static fo_fill_kinfo_t soo_fill_kinfo;
76 struct fileops socketops = {
78 .fo_write = soo_write,
79 .fo_truncate = invfo_truncate,
80 .fo_ioctl = soo_ioctl,
82 .fo_kqfilter = soo_kqfilter,
84 .fo_close = soo_close,
85 .fo_chmod = invfo_chmod,
86 .fo_chown = invfo_chown,
87 .fo_sendfile = invfo_sendfile,
88 .fo_fill_kinfo = soo_fill_kinfo,
89 .fo_flags = DFLAG_PASSABLE
93 soo_read(struct file *fp, struct uio *uio, struct ucred *active_cred,
94 int flags, struct thread *td)
96 struct socket *so = fp->f_data;
100 error = mac_socket_check_receive(active_cred, so);
104 error = soreceive(so, 0, uio, 0, 0, 0);
109 soo_write(struct file *fp, struct uio *uio, struct ucred *active_cred,
110 int flags, struct thread *td)
112 struct socket *so = fp->f_data;
116 error = mac_socket_check_send(active_cred, so);
120 error = sosend(so, 0, uio, 0, 0, 0, uio->uio_td);
121 if (error == EPIPE && (so->so_options & SO_NOSIGPIPE) == 0) {
122 PROC_LOCK(uio->uio_td->td_proc);
123 tdsignal(uio->uio_td, SIGPIPE);
124 PROC_UNLOCK(uio->uio_td->td_proc);
130 soo_ioctl(struct file *fp, u_long cmd, void *data, struct ucred *active_cred,
133 struct socket *so = fp->f_data;
140 so->so_state |= SS_NBIO;
142 so->so_state &= ~SS_NBIO;
148 * XXXRW: This code separately acquires SOCK_LOCK(so) and
149 * SOCKBUF_LOCK(&so->so_rcv) even though they are the same
150 * mutex to avoid introducing the assumption that they are
155 so->so_state |= SS_ASYNC;
157 SOCKBUF_LOCK(&so->so_rcv);
158 so->so_rcv.sb_flags |= SB_ASYNC;
159 SOCKBUF_UNLOCK(&so->so_rcv);
160 SOCKBUF_LOCK(&so->so_snd);
161 so->so_snd.sb_flags |= SB_ASYNC;
162 SOCKBUF_UNLOCK(&so->so_snd);
165 so->so_state &= ~SS_ASYNC;
167 SOCKBUF_LOCK(&so->so_rcv);
168 so->so_rcv.sb_flags &= ~SB_ASYNC;
169 SOCKBUF_UNLOCK(&so->so_rcv);
170 SOCKBUF_LOCK(&so->so_snd);
171 so->so_snd.sb_flags &= ~SB_ASYNC;
172 SOCKBUF_UNLOCK(&so->so_snd);
178 *(int *)data = so->so_rcv.sb_cc;
183 *(int *)data = so->so_snd.sb_cc;
187 if ((so->so_snd.sb_hiwat < so->so_snd.sb_cc) ||
188 (so->so_snd.sb_mbmax < so->so_snd.sb_mbcnt))
191 *(int *)data = sbspace(&so->so_snd);
195 error = fsetown(*(int *)data, &so->so_sigio);
199 *(int *)data = fgetown(&so->so_sigio);
203 error = fsetown(-(*(int *)data), &so->so_sigio);
207 *(int *)data = -fgetown(&so->so_sigio);
212 *(int *)data = (so->so_rcv.sb_state & SBS_RCVATMARK) != 0;
216 * Interface/routing/protocol specific ioctls: interface and
217 * routing ioctls should have a different entry since a
218 * socket is unnecessary.
220 if (IOCGROUP(cmd) == 'i')
221 error = ifioctl(so, cmd, data, td);
222 else if (IOCGROUP(cmd) == 'r') {
223 CURVNET_SET(so->so_vnet);
224 error = rtioctl_fib(cmd, data, so->so_fibnum);
227 CURVNET_SET(so->so_vnet);
228 error = ((*so->so_proto->pr_usrreqs->pru_control)
229 (so, cmd, data, 0, td));
238 soo_poll(struct file *fp, int events, struct ucred *active_cred,
241 struct socket *so = fp->f_data;
245 error = mac_socket_check_poll(active_cred, so);
249 return (sopoll(so, events, fp->f_cred, td));
253 soo_stat(struct file *fp, struct stat *ub, struct ucred *active_cred,
256 struct socket *so = fp->f_data;
261 bzero((caddr_t)ub, sizeof (*ub));
262 ub->st_mode = S_IFSOCK;
264 error = mac_socket_check_stat(active_cred, so);
269 * If SBS_CANTRCVMORE is set, but there's still data left in the
270 * receive buffer, the socket is still readable.
272 SOCKBUF_LOCK(&so->so_rcv);
273 if ((so->so_rcv.sb_state & SBS_CANTRCVMORE) == 0 ||
274 so->so_rcv.sb_cc != 0)
275 ub->st_mode |= S_IRUSR | S_IRGRP | S_IROTH;
276 ub->st_size = so->so_rcv.sb_cc - so->so_rcv.sb_ctl;
277 SOCKBUF_UNLOCK(&so->so_rcv);
279 if ((so->so_snd.sb_state & SBS_CANTSENDMORE) == 0)
280 ub->st_mode |= S_IWUSR | S_IWGRP | S_IWOTH;
281 ub->st_uid = so->so_cred->cr_uid;
282 ub->st_gid = so->so_cred->cr_gid;
283 return (*so->so_proto->pr_usrreqs->pru_sense)(so, ub);
287 * API socket close on file pointer. We call soclose() to close the socket
288 * (including initiating closing protocols). soclose() will sorele() the
289 * file reference but the actual socket will not go away until the socket's
293 soo_close(struct file *fp, struct thread *td)
299 fp->f_ops = &badfileops;
308 soo_fill_kinfo(struct file *fp, struct kinfo_file *kif, struct filedesc *fdp)
316 kif->kf_type = KF_TYPE_SOCKET;
318 kif->kf_sock_domain = so->so_proto->pr_domain->dom_family;
319 kif->kf_sock_type = so->so_type;
320 kif->kf_sock_protocol = so->so_proto->pr_protocol;
321 kif->kf_un.kf_sock.kf_sock_pcb = (uintptr_t)so->so_pcb;
322 switch (kif->kf_sock_domain) {
325 if (kif->kf_sock_protocol == IPPROTO_TCP) {
326 if (so->so_pcb != NULL) {
327 inpcb = (struct inpcb *)(so->so_pcb);
328 kif->kf_un.kf_sock.kf_sock_inpcb =
329 (uintptr_t)inpcb->inp_ppcb;
334 if (so->so_pcb != NULL) {
335 unpcb = (struct unpcb *)(so->so_pcb);
336 if (unpcb->unp_conn) {
337 kif->kf_un.kf_sock.kf_sock_unpconn =
338 (uintptr_t)unpcb->unp_conn;
339 kif->kf_un.kf_sock.kf_sock_rcv_sb_state =
341 kif->kf_un.kf_sock.kf_sock_snd_sb_state =
347 error = so->so_proto->pr_usrreqs->pru_sockaddr(so, &sa);
348 if (error == 0 && sa->sa_len <= sizeof(kif->kf_sa_local)) {
349 bcopy(sa, &kif->kf_sa_local, sa->sa_len);
352 error = so->so_proto->pr_usrreqs->pru_peeraddr(so, &sa);
353 if (error == 0 && sa->sa_len <= sizeof(kif->kf_sa_peer)) {
354 bcopy(sa, &kif->kf_sa_peer, sa->sa_len);
357 strncpy(kif->kf_path, so->so_proto->pr_domain->dom_name,
358 sizeof(kif->kf_path));