uipc_socket.c revision 1.56.2.2 1 1.56.2.2 thorpej /* $NetBSD: uipc_socket.c,v 1.56.2.2 2001/09/07 22:01:53 thorpej Exp $ */
2 1.16 cgd
3 1.1 cgd /*
4 1.15 mycroft * Copyright (c) 1982, 1986, 1988, 1990, 1993
5 1.15 mycroft * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.1 cgd * 3. All advertising materials mentioning features or use of this software
16 1.1 cgd * must display the following acknowledgement:
17 1.1 cgd * This product includes software developed by the University of
18 1.1 cgd * California, Berkeley and its contributors.
19 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
20 1.1 cgd * may be used to endorse or promote products derived from this software
21 1.1 cgd * without specific prior written permission.
22 1.1 cgd *
23 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 cgd * SUCH DAMAGE.
34 1.1 cgd *
35 1.32 fvdl * @(#)uipc_socket.c 8.6 (Berkeley) 5/2/95
36 1.1 cgd */
37 1.35 thorpej
38 1.35 thorpej #include "opt_compat_sunos.h"
39 1.1 cgd
40 1.9 mycroft #include <sys/param.h>
41 1.9 mycroft #include <sys/systm.h>
42 1.9 mycroft #include <sys/proc.h>
43 1.9 mycroft #include <sys/file.h>
44 1.9 mycroft #include <sys/malloc.h>
45 1.9 mycroft #include <sys/mbuf.h>
46 1.9 mycroft #include <sys/domain.h>
47 1.9 mycroft #include <sys/kernel.h>
48 1.9 mycroft #include <sys/protosw.h>
49 1.9 mycroft #include <sys/socket.h>
50 1.9 mycroft #include <sys/socketvar.h>
51 1.21 christos #include <sys/signalvar.h>
52 1.9 mycroft #include <sys/resourcevar.h>
53 1.37 thorpej #include <sys/pool.h>
54 1.56.2.1 lukem #include <sys/event.h>
55 1.56.2.1 lukem
56 1.56.2.1 lukem static void filt_sordetach(struct knote *kn);
57 1.56.2.1 lukem static int filt_soread(struct knote *kn, long hint);
58 1.56.2.1 lukem static void filt_sowdetach(struct knote *kn);
59 1.56.2.1 lukem static int filt_sowrite(struct knote *kn, long hint);
60 1.56.2.1 lukem static int filt_solisten(struct knote *kn, long hint);
61 1.56.2.1 lukem
62 1.56.2.2 thorpej static const struct filterops solisten_filtops =
63 1.56.2.1 lukem { 1, NULL, filt_sordetach, filt_solisten };
64 1.56.2.2 thorpej const struct filterops soread_filtops =
65 1.56.2.1 lukem { 1, NULL, filt_sordetach, filt_soread };
66 1.56.2.2 thorpej const struct filterops sowrite_filtops =
67 1.56.2.1 lukem { 1, NULL, filt_sowdetach, filt_sowrite };
68 1.37 thorpej
69 1.54 lukem struct pool socket_pool;
70 1.37 thorpej
71 1.54 lukem extern int somaxconn; /* patchable (XXX sysctl) */
72 1.54 lukem int somaxconn = SOMAXCONN;
73 1.49 jonathan
74 1.37 thorpej void
75 1.54 lukem soinit(void)
76 1.37 thorpej {
77 1.37 thorpej
78 1.37 thorpej pool_init(&socket_pool, sizeof(struct socket), 0, 0, 0,
79 1.37 thorpej "sockpl", 0, NULL, NULL, M_SOCKET);
80 1.37 thorpej }
81 1.1 cgd
82 1.1 cgd /*
83 1.1 cgd * Socket operation routines.
84 1.1 cgd * These routines are called by the routines in
85 1.1 cgd * sys_socket.c or from a system process, and
86 1.1 cgd * implement the semantics of socket operations by
87 1.1 cgd * switching out to the protocol specific routines.
88 1.1 cgd */
89 1.1 cgd /*ARGSUSED*/
90 1.3 andrew int
91 1.54 lukem socreate(int dom, struct socket **aso, int type, int proto)
92 1.1 cgd {
93 1.54 lukem struct proc *p;
94 1.54 lukem struct protosw *prp;
95 1.54 lukem struct socket *so;
96 1.54 lukem int error, s;
97 1.1 cgd
98 1.54 lukem p = curproc; /* XXX */
99 1.1 cgd if (proto)
100 1.1 cgd prp = pffindproto(dom, proto, type);
101 1.1 cgd else
102 1.1 cgd prp = pffindtype(dom, type);
103 1.15 mycroft if (prp == 0 || prp->pr_usrreq == 0)
104 1.1 cgd return (EPROTONOSUPPORT);
105 1.1 cgd if (prp->pr_type != type)
106 1.1 cgd return (EPROTOTYPE);
107 1.39 matt s = splsoftnet();
108 1.37 thorpej so = pool_get(&socket_pool, PR_WAITOK);
109 1.38 perry memset((caddr_t)so, 0, sizeof(*so));
110 1.31 thorpej TAILQ_INIT(&so->so_q0);
111 1.31 thorpej TAILQ_INIT(&so->so_q);
112 1.1 cgd so->so_type = type;
113 1.1 cgd so->so_proto = prp;
114 1.33 matt so->so_send = sosend;
115 1.33 matt so->so_receive = soreceive;
116 1.44 lukem if (p != 0)
117 1.44 lukem so->so_uid = p->p_ucred->cr_uid;
118 1.22 mycroft error = (*prp->pr_usrreq)(so, PRU_ATTACH, (struct mbuf *)0,
119 1.22 mycroft (struct mbuf *)(long)proto, (struct mbuf *)0, p);
120 1.1 cgd if (error) {
121 1.1 cgd so->so_state |= SS_NOFDREF;
122 1.1 cgd sofree(so);
123 1.39 matt splx(s);
124 1.1 cgd return (error);
125 1.1 cgd }
126 1.10 deraadt #ifdef COMPAT_SUNOS
127 1.18 christos {
128 1.18 christos extern struct emul emul_sunos;
129 1.18 christos if (p->p_emul == &emul_sunos && type == SOCK_DGRAM)
130 1.18 christos so->so_options |= SO_BROADCAST;
131 1.18 christos }
132 1.10 deraadt #endif
133 1.39 matt splx(s);
134 1.1 cgd *aso = so;
135 1.1 cgd return (0);
136 1.1 cgd }
137 1.1 cgd
138 1.3 andrew int
139 1.54 lukem sobind(struct socket *so, struct mbuf *nam, struct proc *p)
140 1.1 cgd {
141 1.54 lukem int s, error;
142 1.1 cgd
143 1.54 lukem s = splsoftnet();
144 1.22 mycroft error = (*so->so_proto->pr_usrreq)(so, PRU_BIND, (struct mbuf *)0,
145 1.22 mycroft nam, (struct mbuf *)0, p);
146 1.1 cgd splx(s);
147 1.1 cgd return (error);
148 1.1 cgd }
149 1.1 cgd
150 1.3 andrew int
151 1.54 lukem solisten(struct socket *so, int backlog)
152 1.1 cgd {
153 1.54 lukem int s, error;
154 1.1 cgd
155 1.54 lukem s = splsoftnet();
156 1.22 mycroft error = (*so->so_proto->pr_usrreq)(so, PRU_LISTEN, (struct mbuf *)0,
157 1.22 mycroft (struct mbuf *)0, (struct mbuf *)0, (struct proc *)0);
158 1.1 cgd if (error) {
159 1.1 cgd splx(s);
160 1.1 cgd return (error);
161 1.1 cgd }
162 1.31 thorpej if (so->so_q.tqh_first == NULL)
163 1.1 cgd so->so_options |= SO_ACCEPTCONN;
164 1.1 cgd if (backlog < 0)
165 1.1 cgd backlog = 0;
166 1.49 jonathan so->so_qlimit = min(backlog, somaxconn);
167 1.1 cgd splx(s);
168 1.1 cgd return (0);
169 1.1 cgd }
170 1.1 cgd
171 1.21 christos void
172 1.54 lukem sofree(struct socket *so)
173 1.1 cgd {
174 1.1 cgd
175 1.43 mycroft if (so->so_pcb || (so->so_state & SS_NOFDREF) == 0)
176 1.1 cgd return;
177 1.43 mycroft if (so->so_head) {
178 1.43 mycroft /*
179 1.43 mycroft * We must not decommission a socket that's on the accept(2)
180 1.43 mycroft * queue. If we do, then accept(2) may hang after select(2)
181 1.43 mycroft * indicated that the listening socket was ready.
182 1.43 mycroft */
183 1.43 mycroft if (!soqremque(so, 0))
184 1.43 mycroft return;
185 1.43 mycroft }
186 1.1 cgd sbrelease(&so->so_snd);
187 1.1 cgd sorflush(so);
188 1.37 thorpej pool_put(&socket_pool, so);
189 1.1 cgd }
190 1.1 cgd
191 1.1 cgd /*
192 1.1 cgd * Close a socket on last file table reference removal.
193 1.1 cgd * Initiate disconnect if connected.
194 1.1 cgd * Free socket when disconnect complete.
195 1.1 cgd */
196 1.3 andrew int
197 1.54 lukem soclose(struct socket *so)
198 1.1 cgd {
199 1.54 lukem struct socket *so2;
200 1.54 lukem int s, error;
201 1.1 cgd
202 1.54 lukem error = 0;
203 1.54 lukem s = splsoftnet(); /* conservative */
204 1.1 cgd if (so->so_options & SO_ACCEPTCONN) {
205 1.41 mycroft while ((so2 = so->so_q0.tqh_first) != 0) {
206 1.42 mycroft (void) soqremque(so2, 0);
207 1.41 mycroft (void) soabort(so2);
208 1.41 mycroft }
209 1.41 mycroft while ((so2 = so->so_q.tqh_first) != 0) {
210 1.42 mycroft (void) soqremque(so2, 1);
211 1.41 mycroft (void) soabort(so2);
212 1.41 mycroft }
213 1.1 cgd }
214 1.1 cgd if (so->so_pcb == 0)
215 1.1 cgd goto discard;
216 1.1 cgd if (so->so_state & SS_ISCONNECTED) {
217 1.1 cgd if ((so->so_state & SS_ISDISCONNECTING) == 0) {
218 1.1 cgd error = sodisconnect(so);
219 1.1 cgd if (error)
220 1.1 cgd goto drop;
221 1.1 cgd }
222 1.1 cgd if (so->so_options & SO_LINGER) {
223 1.1 cgd if ((so->so_state & SS_ISDISCONNECTING) &&
224 1.1 cgd (so->so_state & SS_NBIO))
225 1.1 cgd goto drop;
226 1.21 christos while (so->so_state & SS_ISCONNECTED) {
227 1.21 christos error = tsleep((caddr_t)&so->so_timeo,
228 1.21 christos PSOCK | PCATCH, netcls,
229 1.30 thorpej so->so_linger * hz);
230 1.21 christos if (error)
231 1.1 cgd break;
232 1.21 christos }
233 1.1 cgd }
234 1.1 cgd }
235 1.54 lukem drop:
236 1.1 cgd if (so->so_pcb) {
237 1.22 mycroft int error2 = (*so->so_proto->pr_usrreq)(so, PRU_DETACH,
238 1.22 mycroft (struct mbuf *)0, (struct mbuf *)0, (struct mbuf *)0,
239 1.22 mycroft (struct proc *)0);
240 1.1 cgd if (error == 0)
241 1.1 cgd error = error2;
242 1.1 cgd }
243 1.54 lukem discard:
244 1.1 cgd if (so->so_state & SS_NOFDREF)
245 1.1 cgd panic("soclose: NOFDREF");
246 1.1 cgd so->so_state |= SS_NOFDREF;
247 1.1 cgd sofree(so);
248 1.1 cgd splx(s);
249 1.1 cgd return (error);
250 1.1 cgd }
251 1.1 cgd
252 1.1 cgd /*
253 1.20 mycroft * Must be called at splsoftnet...
254 1.1 cgd */
255 1.3 andrew int
256 1.54 lukem soabort(struct socket *so)
257 1.1 cgd {
258 1.1 cgd
259 1.22 mycroft return (*so->so_proto->pr_usrreq)(so, PRU_ABORT, (struct mbuf *)0,
260 1.22 mycroft (struct mbuf *)0, (struct mbuf *)0, (struct proc *)0);
261 1.1 cgd }
262 1.1 cgd
263 1.3 andrew int
264 1.54 lukem soaccept(struct socket *so, struct mbuf *nam)
265 1.1 cgd {
266 1.54 lukem int s, error;
267 1.1 cgd
268 1.54 lukem error = 0;
269 1.54 lukem s = splsoftnet();
270 1.1 cgd if ((so->so_state & SS_NOFDREF) == 0)
271 1.1 cgd panic("soaccept: !NOFDREF");
272 1.1 cgd so->so_state &= ~SS_NOFDREF;
273 1.55 thorpej if ((so->so_state & SS_ISDISCONNECTED) == 0 ||
274 1.55 thorpej (so->so_proto->pr_flags & PR_ABRTACPTDIS) == 0)
275 1.41 mycroft error = (*so->so_proto->pr_usrreq)(so, PRU_ACCEPT,
276 1.41 mycroft (struct mbuf *)0, nam, (struct mbuf *)0, (struct proc *)0);
277 1.41 mycroft else
278 1.53 itojun error = ECONNABORTED;
279 1.52 itojun
280 1.1 cgd splx(s);
281 1.1 cgd return (error);
282 1.1 cgd }
283 1.1 cgd
284 1.3 andrew int
285 1.54 lukem soconnect(struct socket *so, struct mbuf *nam)
286 1.1 cgd {
287 1.54 lukem struct proc *p;
288 1.54 lukem int s, error;
289 1.1 cgd
290 1.54 lukem p = curproc; /* XXX */
291 1.1 cgd if (so->so_options & SO_ACCEPTCONN)
292 1.1 cgd return (EOPNOTSUPP);
293 1.20 mycroft s = splsoftnet();
294 1.1 cgd /*
295 1.1 cgd * If protocol is connection-based, can only connect once.
296 1.1 cgd * Otherwise, if connected, try to disconnect first.
297 1.1 cgd * This allows user to disconnect by connecting to, e.g.,
298 1.1 cgd * a null address.
299 1.1 cgd */
300 1.1 cgd if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING) &&
301 1.1 cgd ((so->so_proto->pr_flags & PR_CONNREQUIRED) ||
302 1.1 cgd (error = sodisconnect(so))))
303 1.1 cgd error = EISCONN;
304 1.1 cgd else
305 1.1 cgd error = (*so->so_proto->pr_usrreq)(so, PRU_CONNECT,
306 1.23 mycroft (struct mbuf *)0, nam, (struct mbuf *)0, p);
307 1.1 cgd splx(s);
308 1.1 cgd return (error);
309 1.1 cgd }
310 1.1 cgd
311 1.3 andrew int
312 1.54 lukem soconnect2(struct socket *so1, struct socket *so2)
313 1.1 cgd {
314 1.54 lukem int s, error;
315 1.1 cgd
316 1.54 lukem s = splsoftnet();
317 1.22 mycroft error = (*so1->so_proto->pr_usrreq)(so1, PRU_CONNECT2,
318 1.22 mycroft (struct mbuf *)0, (struct mbuf *)so2, (struct mbuf *)0,
319 1.22 mycroft (struct proc *)0);
320 1.1 cgd splx(s);
321 1.1 cgd return (error);
322 1.1 cgd }
323 1.1 cgd
324 1.3 andrew int
325 1.54 lukem sodisconnect(struct socket *so)
326 1.1 cgd {
327 1.54 lukem int s, error;
328 1.1 cgd
329 1.54 lukem s = splsoftnet();
330 1.1 cgd if ((so->so_state & SS_ISCONNECTED) == 0) {
331 1.1 cgd error = ENOTCONN;
332 1.1 cgd goto bad;
333 1.1 cgd }
334 1.1 cgd if (so->so_state & SS_ISDISCONNECTING) {
335 1.1 cgd error = EALREADY;
336 1.1 cgd goto bad;
337 1.1 cgd }
338 1.22 mycroft error = (*so->so_proto->pr_usrreq)(so, PRU_DISCONNECT,
339 1.22 mycroft (struct mbuf *)0, (struct mbuf *)0, (struct mbuf *)0,
340 1.22 mycroft (struct proc *)0);
341 1.54 lukem bad:
342 1.1 cgd splx(s);
343 1.1 cgd return (error);
344 1.1 cgd }
345 1.1 cgd
346 1.15 mycroft #define SBLOCKWAIT(f) (((f) & MSG_DONTWAIT) ? M_NOWAIT : M_WAITOK)
347 1.1 cgd /*
348 1.1 cgd * Send on a socket.
349 1.1 cgd * If send must go all at once and message is larger than
350 1.1 cgd * send buffering, then hard error.
351 1.1 cgd * Lock against other senders.
352 1.1 cgd * If must go all at once and not enough room now, then
353 1.1 cgd * inform user that this would block and do nothing.
354 1.1 cgd * Otherwise, if nonblocking, send as much as possible.
355 1.1 cgd * The data to be sent is described by "uio" if nonzero,
356 1.1 cgd * otherwise by the mbuf chain "top" (which must be null
357 1.1 cgd * if uio is not). Data provided in mbuf chain must be small
358 1.1 cgd * enough to send all at once.
359 1.1 cgd *
360 1.1 cgd * Returns nonzero on error, timeout or signal; callers
361 1.1 cgd * must check for short counts if EINTR/ERESTART are returned.
362 1.1 cgd * Data and control buffers are freed on return.
363 1.1 cgd */
364 1.3 andrew int
365 1.54 lukem sosend(struct socket *so, struct mbuf *addr, struct uio *uio, struct mbuf *top,
366 1.54 lukem struct mbuf *control, int flags)
367 1.1 cgd {
368 1.54 lukem struct proc *p;
369 1.54 lukem struct mbuf **mp, *m;
370 1.54 lukem long space, len, resid;
371 1.54 lukem int clen, error, s, dontroute, mlen, atomic;
372 1.54 lukem
373 1.54 lukem p = curproc; /* XXX */
374 1.54 lukem clen = 0;
375 1.54 lukem atomic = sosendallatonce(so) || top;
376 1.1 cgd if (uio)
377 1.1 cgd resid = uio->uio_resid;
378 1.1 cgd else
379 1.1 cgd resid = top->m_pkthdr.len;
380 1.7 cgd /*
381 1.7 cgd * In theory resid should be unsigned.
382 1.7 cgd * However, space must be signed, as it might be less than 0
383 1.7 cgd * if we over-committed, and we must use a signed comparison
384 1.7 cgd * of space and resid. On the other hand, a negative resid
385 1.7 cgd * causes us to loop sending 0-length segments to the protocol.
386 1.7 cgd */
387 1.29 mycroft if (resid < 0) {
388 1.29 mycroft error = EINVAL;
389 1.29 mycroft goto out;
390 1.29 mycroft }
391 1.1 cgd dontroute =
392 1.1 cgd (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0 &&
393 1.1 cgd (so->so_proto->pr_flags & PR_ATOMIC);
394 1.12 mycroft p->p_stats->p_ru.ru_msgsnd++;
395 1.1 cgd if (control)
396 1.1 cgd clen = control->m_len;
397 1.1 cgd #define snderr(errno) { error = errno; splx(s); goto release; }
398 1.1 cgd
399 1.54 lukem restart:
400 1.21 christos if ((error = sblock(&so->so_snd, SBLOCKWAIT(flags))) != 0)
401 1.1 cgd goto out;
402 1.1 cgd do {
403 1.20 mycroft s = splsoftnet();
404 1.1 cgd if (so->so_state & SS_CANTSENDMORE)
405 1.1 cgd snderr(EPIPE);
406 1.48 thorpej if (so->so_error) {
407 1.48 thorpej error = so->so_error;
408 1.48 thorpej so->so_error = 0;
409 1.48 thorpej splx(s);
410 1.48 thorpej goto release;
411 1.48 thorpej }
412 1.1 cgd if ((so->so_state & SS_ISCONNECTED) == 0) {
413 1.1 cgd if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
414 1.1 cgd if ((so->so_state & SS_ISCONFIRMING) == 0 &&
415 1.1 cgd !(resid == 0 && clen != 0))
416 1.1 cgd snderr(ENOTCONN);
417 1.1 cgd } else if (addr == 0)
418 1.1 cgd snderr(EDESTADDRREQ);
419 1.1 cgd }
420 1.1 cgd space = sbspace(&so->so_snd);
421 1.1 cgd if (flags & MSG_OOB)
422 1.1 cgd space += 1024;
423 1.21 christos if ((atomic && resid > so->so_snd.sb_hiwat) ||
424 1.11 mycroft clen > so->so_snd.sb_hiwat)
425 1.11 mycroft snderr(EMSGSIZE);
426 1.11 mycroft if (space < resid + clen && uio &&
427 1.1 cgd (atomic || space < so->so_snd.sb_lowat || space < clen)) {
428 1.1 cgd if (so->so_state & SS_NBIO)
429 1.1 cgd snderr(EWOULDBLOCK);
430 1.1 cgd sbunlock(&so->so_snd);
431 1.1 cgd error = sbwait(&so->so_snd);
432 1.1 cgd splx(s);
433 1.1 cgd if (error)
434 1.1 cgd goto out;
435 1.1 cgd goto restart;
436 1.1 cgd }
437 1.1 cgd splx(s);
438 1.1 cgd mp = ⊤
439 1.1 cgd space -= clen;
440 1.1 cgd do {
441 1.45 tv if (uio == NULL) {
442 1.45 tv /*
443 1.45 tv * Data is prepackaged in "top".
444 1.45 tv */
445 1.45 tv resid = 0;
446 1.45 tv if (flags & MSG_EOR)
447 1.45 tv top->m_flags |= M_EOR;
448 1.45 tv } else do {
449 1.45 tv if (top == 0) {
450 1.45 tv MGETHDR(m, M_WAIT, MT_DATA);
451 1.45 tv mlen = MHLEN;
452 1.45 tv m->m_pkthdr.len = 0;
453 1.45 tv m->m_pkthdr.rcvif = (struct ifnet *)0;
454 1.45 tv } else {
455 1.45 tv MGET(m, M_WAIT, MT_DATA);
456 1.45 tv mlen = MLEN;
457 1.45 tv }
458 1.45 tv if (resid >= MINCLSIZE && space >= MCLBYTES) {
459 1.45 tv MCLGET(m, M_WAIT);
460 1.45 tv if ((m->m_flags & M_EXT) == 0)
461 1.45 tv goto nopages;
462 1.45 tv mlen = MCLBYTES;
463 1.15 mycroft #ifdef MAPPED_MBUFS
464 1.45 tv len = min(MCLBYTES, resid);
465 1.15 mycroft #else
466 1.45 tv if (atomic && top == 0) {
467 1.54 lukem len = min(MCLBYTES - max_hdr,
468 1.54 lukem resid);
469 1.45 tv m->m_data += max_hdr;
470 1.45 tv } else
471 1.45 tv len = min(MCLBYTES, resid);
472 1.15 mycroft #endif
473 1.45 tv space -= len;
474 1.45 tv } else {
475 1.1 cgd nopages:
476 1.45 tv len = min(min(mlen, resid), space);
477 1.45 tv space -= len;
478 1.45 tv /*
479 1.45 tv * For datagram protocols, leave room
480 1.45 tv * for protocol headers in first mbuf.
481 1.45 tv */
482 1.45 tv if (atomic && top == 0 && len < mlen)
483 1.45 tv MH_ALIGN(m, len);
484 1.45 tv }
485 1.54 lukem error = uiomove(mtod(m, caddr_t), (int)len,
486 1.54 lukem uio);
487 1.45 tv resid = uio->uio_resid;
488 1.45 tv m->m_len = len;
489 1.45 tv *mp = m;
490 1.45 tv top->m_pkthdr.len += len;
491 1.45 tv if (error)
492 1.45 tv goto release;
493 1.45 tv mp = &m->m_next;
494 1.45 tv if (resid <= 0) {
495 1.45 tv if (flags & MSG_EOR)
496 1.45 tv top->m_flags |= M_EOR;
497 1.45 tv break;
498 1.45 tv }
499 1.45 tv } while (space > 0 && atomic);
500 1.46 sommerfe
501 1.46 sommerfe s = splsoftnet();
502 1.46 sommerfe
503 1.46 sommerfe if (so->so_state & SS_CANTSENDMORE)
504 1.46 sommerfe snderr(EPIPE);
505 1.45 tv
506 1.45 tv if (dontroute)
507 1.45 tv so->so_options |= SO_DONTROUTE;
508 1.45 tv if (resid > 0)
509 1.45 tv so->so_state |= SS_MORETOCOME;
510 1.46 sommerfe error = (*so->so_proto->pr_usrreq)(so,
511 1.46 sommerfe (flags & MSG_OOB) ? PRU_SENDOOB : PRU_SEND,
512 1.46 sommerfe top, addr, control, p);
513 1.45 tv if (dontroute)
514 1.45 tv so->so_options &= ~SO_DONTROUTE;
515 1.45 tv if (resid > 0)
516 1.45 tv so->so_state &= ~SS_MORETOCOME;
517 1.46 sommerfe splx(s);
518 1.46 sommerfe
519 1.45 tv clen = 0;
520 1.45 tv control = 0;
521 1.45 tv top = 0;
522 1.45 tv mp = ⊤
523 1.1 cgd if (error)
524 1.1 cgd goto release;
525 1.1 cgd } while (resid && space > 0);
526 1.1 cgd } while (resid);
527 1.1 cgd
528 1.54 lukem release:
529 1.1 cgd sbunlock(&so->so_snd);
530 1.54 lukem out:
531 1.1 cgd if (top)
532 1.1 cgd m_freem(top);
533 1.1 cgd if (control)
534 1.1 cgd m_freem(control);
535 1.1 cgd return (error);
536 1.1 cgd }
537 1.1 cgd
538 1.1 cgd /*
539 1.1 cgd * Implement receive operations on a socket.
540 1.1 cgd * We depend on the way that records are added to the sockbuf
541 1.1 cgd * by sbappend*. In particular, each record (mbufs linked through m_next)
542 1.1 cgd * must begin with an address if the protocol so specifies,
543 1.1 cgd * followed by an optional mbuf or mbufs containing ancillary data,
544 1.1 cgd * and then zero or more mbufs of data.
545 1.1 cgd * In order to avoid blocking network interrupts for the entire time here,
546 1.1 cgd * we splx() while doing the actual copy to user space.
547 1.1 cgd * Although the sockbuf is locked, new data may still be appended,
548 1.1 cgd * and thus we must maintain consistency of the sockbuf during that time.
549 1.1 cgd *
550 1.1 cgd * The caller may receive the data as a single mbuf chain by supplying
551 1.1 cgd * an mbuf **mp0 for use in returning the chain. The uio is then used
552 1.1 cgd * only for the count in uio_resid.
553 1.1 cgd */
554 1.3 andrew int
555 1.54 lukem soreceive(struct socket *so, struct mbuf **paddr, struct uio *uio,
556 1.54 lukem struct mbuf **mp0, struct mbuf **controlp, int *flagsp)
557 1.1 cgd {
558 1.54 lukem struct mbuf *m, **mp;
559 1.54 lukem int flags, len, error, s, offset, moff, type, orig_resid;
560 1.54 lukem struct protosw *pr;
561 1.54 lukem struct mbuf *nextrecord;
562 1.1 cgd
563 1.54 lukem pr = so->so_proto;
564 1.1 cgd mp = mp0;
565 1.54 lukem type = 0;
566 1.54 lukem orig_resid = uio->uio_resid;
567 1.1 cgd if (paddr)
568 1.1 cgd *paddr = 0;
569 1.1 cgd if (controlp)
570 1.1 cgd *controlp = 0;
571 1.1 cgd if (flagsp)
572 1.1 cgd flags = *flagsp &~ MSG_EOR;
573 1.1 cgd else
574 1.1 cgd flags = 0;
575 1.1 cgd if (flags & MSG_OOB) {
576 1.1 cgd m = m_get(M_WAIT, MT_DATA);
577 1.17 cgd error = (*pr->pr_usrreq)(so, PRU_RCVOOB, m,
578 1.22 mycroft (struct mbuf *)(long)(flags & MSG_PEEK), (struct mbuf *)0,
579 1.22 mycroft (struct proc *)0);
580 1.1 cgd if (error)
581 1.1 cgd goto bad;
582 1.1 cgd do {
583 1.1 cgd error = uiomove(mtod(m, caddr_t),
584 1.1 cgd (int) min(uio->uio_resid, m->m_len), uio);
585 1.1 cgd m = m_free(m);
586 1.1 cgd } while (uio->uio_resid && error == 0 && m);
587 1.54 lukem bad:
588 1.1 cgd if (m)
589 1.1 cgd m_freem(m);
590 1.1 cgd return (error);
591 1.1 cgd }
592 1.1 cgd if (mp)
593 1.1 cgd *mp = (struct mbuf *)0;
594 1.1 cgd if (so->so_state & SS_ISCONFIRMING && uio->uio_resid)
595 1.22 mycroft (*pr->pr_usrreq)(so, PRU_RCVD, (struct mbuf *)0,
596 1.22 mycroft (struct mbuf *)0, (struct mbuf *)0, (struct proc *)0);
597 1.1 cgd
598 1.54 lukem restart:
599 1.21 christos if ((error = sblock(&so->so_rcv, SBLOCKWAIT(flags))) != 0)
600 1.1 cgd return (error);
601 1.20 mycroft s = splsoftnet();
602 1.1 cgd
603 1.1 cgd m = so->so_rcv.sb_mb;
604 1.1 cgd /*
605 1.1 cgd * If we have less data than requested, block awaiting more
606 1.1 cgd * (subject to any timeout) if:
607 1.15 mycroft * 1. the current count is less than the low water mark,
608 1.1 cgd * 2. MSG_WAITALL is set, and it is possible to do the entire
609 1.15 mycroft * receive operation at once if we block (resid <= hiwat), or
610 1.15 mycroft * 3. MSG_DONTWAIT is not set.
611 1.1 cgd * If MSG_WAITALL is set but resid is larger than the receive buffer,
612 1.1 cgd * we have to do the receive in sections, and thus risk returning
613 1.1 cgd * a short count if a timeout or signal occurs after we start.
614 1.1 cgd */
615 1.21 christos if (m == 0 || (((flags & MSG_DONTWAIT) == 0 &&
616 1.15 mycroft so->so_rcv.sb_cc < uio->uio_resid) &&
617 1.1 cgd (so->so_rcv.sb_cc < so->so_rcv.sb_lowat ||
618 1.1 cgd ((flags & MSG_WAITALL) && uio->uio_resid <= so->so_rcv.sb_hiwat)) &&
619 1.21 christos m->m_nextpkt == 0 && (pr->pr_flags & PR_ATOMIC) == 0)) {
620 1.1 cgd #ifdef DIAGNOSTIC
621 1.1 cgd if (m == 0 && so->so_rcv.sb_cc)
622 1.1 cgd panic("receive 1");
623 1.1 cgd #endif
624 1.1 cgd if (so->so_error) {
625 1.1 cgd if (m)
626 1.15 mycroft goto dontblock;
627 1.1 cgd error = so->so_error;
628 1.1 cgd if ((flags & MSG_PEEK) == 0)
629 1.1 cgd so->so_error = 0;
630 1.1 cgd goto release;
631 1.1 cgd }
632 1.1 cgd if (so->so_state & SS_CANTRCVMORE) {
633 1.1 cgd if (m)
634 1.15 mycroft goto dontblock;
635 1.1 cgd else
636 1.1 cgd goto release;
637 1.1 cgd }
638 1.1 cgd for (; m; m = m->m_next)
639 1.1 cgd if (m->m_type == MT_OOBDATA || (m->m_flags & M_EOR)) {
640 1.1 cgd m = so->so_rcv.sb_mb;
641 1.1 cgd goto dontblock;
642 1.1 cgd }
643 1.1 cgd if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0 &&
644 1.1 cgd (so->so_proto->pr_flags & PR_CONNREQUIRED)) {
645 1.1 cgd error = ENOTCONN;
646 1.1 cgd goto release;
647 1.1 cgd }
648 1.1 cgd if (uio->uio_resid == 0)
649 1.1 cgd goto release;
650 1.15 mycroft if ((so->so_state & SS_NBIO) || (flags & MSG_DONTWAIT)) {
651 1.1 cgd error = EWOULDBLOCK;
652 1.1 cgd goto release;
653 1.1 cgd }
654 1.1 cgd sbunlock(&so->so_rcv);
655 1.1 cgd error = sbwait(&so->so_rcv);
656 1.1 cgd splx(s);
657 1.1 cgd if (error)
658 1.1 cgd return (error);
659 1.1 cgd goto restart;
660 1.1 cgd }
661 1.54 lukem dontblock:
662 1.15 mycroft #ifdef notyet /* XXXX */
663 1.15 mycroft if (uio->uio_procp)
664 1.15 mycroft uio->uio_procp->p_stats->p_ru.ru_msgrcv++;
665 1.15 mycroft #endif
666 1.1 cgd nextrecord = m->m_nextpkt;
667 1.1 cgd if (pr->pr_flags & PR_ADDR) {
668 1.1 cgd #ifdef DIAGNOSTIC
669 1.1 cgd if (m->m_type != MT_SONAME)
670 1.1 cgd panic("receive 1a");
671 1.1 cgd #endif
672 1.3 andrew orig_resid = 0;
673 1.1 cgd if (flags & MSG_PEEK) {
674 1.1 cgd if (paddr)
675 1.1 cgd *paddr = m_copy(m, 0, m->m_len);
676 1.1 cgd m = m->m_next;
677 1.1 cgd } else {
678 1.1 cgd sbfree(&so->so_rcv, m);
679 1.1 cgd if (paddr) {
680 1.1 cgd *paddr = m;
681 1.1 cgd so->so_rcv.sb_mb = m->m_next;
682 1.1 cgd m->m_next = 0;
683 1.1 cgd m = so->so_rcv.sb_mb;
684 1.1 cgd } else {
685 1.1 cgd MFREE(m, so->so_rcv.sb_mb);
686 1.1 cgd m = so->so_rcv.sb_mb;
687 1.1 cgd }
688 1.1 cgd }
689 1.1 cgd }
690 1.1 cgd while (m && m->m_type == MT_CONTROL && error == 0) {
691 1.1 cgd if (flags & MSG_PEEK) {
692 1.1 cgd if (controlp)
693 1.1 cgd *controlp = m_copy(m, 0, m->m_len);
694 1.1 cgd m = m->m_next;
695 1.1 cgd } else {
696 1.1 cgd sbfree(&so->so_rcv, m);
697 1.1 cgd if (controlp) {
698 1.1 cgd if (pr->pr_domain->dom_externalize &&
699 1.1 cgd mtod(m, struct cmsghdr *)->cmsg_type ==
700 1.1 cgd SCM_RIGHTS)
701 1.45 tv error = (*pr->pr_domain->dom_externalize)(m);
702 1.1 cgd *controlp = m;
703 1.1 cgd so->so_rcv.sb_mb = m->m_next;
704 1.1 cgd m->m_next = 0;
705 1.1 cgd m = so->so_rcv.sb_mb;
706 1.1 cgd } else {
707 1.1 cgd MFREE(m, so->so_rcv.sb_mb);
708 1.1 cgd m = so->so_rcv.sb_mb;
709 1.1 cgd }
710 1.1 cgd }
711 1.3 andrew if (controlp) {
712 1.3 andrew orig_resid = 0;
713 1.1 cgd controlp = &(*controlp)->m_next;
714 1.3 andrew }
715 1.1 cgd }
716 1.1 cgd if (m) {
717 1.1 cgd if ((flags & MSG_PEEK) == 0)
718 1.1 cgd m->m_nextpkt = nextrecord;
719 1.1 cgd type = m->m_type;
720 1.1 cgd if (type == MT_OOBDATA)
721 1.1 cgd flags |= MSG_OOB;
722 1.1 cgd }
723 1.1 cgd moff = 0;
724 1.1 cgd offset = 0;
725 1.1 cgd while (m && uio->uio_resid > 0 && error == 0) {
726 1.1 cgd if (m->m_type == MT_OOBDATA) {
727 1.1 cgd if (type != MT_OOBDATA)
728 1.1 cgd break;
729 1.1 cgd } else if (type == MT_OOBDATA)
730 1.1 cgd break;
731 1.1 cgd #ifdef DIAGNOSTIC
732 1.1 cgd else if (m->m_type != MT_DATA && m->m_type != MT_HEADER)
733 1.1 cgd panic("receive 3");
734 1.1 cgd #endif
735 1.1 cgd so->so_state &= ~SS_RCVATMARK;
736 1.1 cgd len = uio->uio_resid;
737 1.1 cgd if (so->so_oobmark && len > so->so_oobmark - offset)
738 1.1 cgd len = so->so_oobmark - offset;
739 1.1 cgd if (len > m->m_len - moff)
740 1.1 cgd len = m->m_len - moff;
741 1.1 cgd /*
742 1.1 cgd * If mp is set, just pass back the mbufs.
743 1.1 cgd * Otherwise copy them out via the uio, then free.
744 1.1 cgd * Sockbuf must be consistent here (points to current mbuf,
745 1.1 cgd * it points to next record) when we drop priority;
746 1.1 cgd * we must note any additions to the sockbuf when we
747 1.1 cgd * block interrupts again.
748 1.1 cgd */
749 1.1 cgd if (mp == 0) {
750 1.1 cgd splx(s);
751 1.1 cgd error = uiomove(mtod(m, caddr_t) + moff, (int)len, uio);
752 1.20 mycroft s = splsoftnet();
753 1.1 cgd } else
754 1.1 cgd uio->uio_resid -= len;
755 1.1 cgd if (len == m->m_len - moff) {
756 1.1 cgd if (m->m_flags & M_EOR)
757 1.1 cgd flags |= MSG_EOR;
758 1.1 cgd if (flags & MSG_PEEK) {
759 1.1 cgd m = m->m_next;
760 1.1 cgd moff = 0;
761 1.1 cgd } else {
762 1.1 cgd nextrecord = m->m_nextpkt;
763 1.1 cgd sbfree(&so->so_rcv, m);
764 1.1 cgd if (mp) {
765 1.1 cgd *mp = m;
766 1.1 cgd mp = &m->m_next;
767 1.1 cgd so->so_rcv.sb_mb = m = m->m_next;
768 1.1 cgd *mp = (struct mbuf *)0;
769 1.1 cgd } else {
770 1.1 cgd MFREE(m, so->so_rcv.sb_mb);
771 1.1 cgd m = so->so_rcv.sb_mb;
772 1.1 cgd }
773 1.1 cgd if (m)
774 1.1 cgd m->m_nextpkt = nextrecord;
775 1.1 cgd }
776 1.1 cgd } else {
777 1.1 cgd if (flags & MSG_PEEK)
778 1.1 cgd moff += len;
779 1.1 cgd else {
780 1.1 cgd if (mp)
781 1.1 cgd *mp = m_copym(m, 0, len, M_WAIT);
782 1.1 cgd m->m_data += len;
783 1.1 cgd m->m_len -= len;
784 1.1 cgd so->so_rcv.sb_cc -= len;
785 1.1 cgd }
786 1.1 cgd }
787 1.1 cgd if (so->so_oobmark) {
788 1.1 cgd if ((flags & MSG_PEEK) == 0) {
789 1.1 cgd so->so_oobmark -= len;
790 1.1 cgd if (so->so_oobmark == 0) {
791 1.1 cgd so->so_state |= SS_RCVATMARK;
792 1.1 cgd break;
793 1.1 cgd }
794 1.7 cgd } else {
795 1.1 cgd offset += len;
796 1.7 cgd if (offset == so->so_oobmark)
797 1.7 cgd break;
798 1.7 cgd }
799 1.1 cgd }
800 1.1 cgd if (flags & MSG_EOR)
801 1.1 cgd break;
802 1.1 cgd /*
803 1.1 cgd * If the MSG_WAITALL flag is set (for non-atomic socket),
804 1.1 cgd * we must not quit until "uio->uio_resid == 0" or an error
805 1.1 cgd * termination. If a signal/timeout occurs, return
806 1.1 cgd * with a short count but without error.
807 1.1 cgd * Keep sockbuf locked against other readers.
808 1.1 cgd */
809 1.1 cgd while (flags & MSG_WAITALL && m == 0 && uio->uio_resid > 0 &&
810 1.3 andrew !sosendallatonce(so) && !nextrecord) {
811 1.1 cgd if (so->so_error || so->so_state & SS_CANTRCVMORE)
812 1.1 cgd break;
813 1.1 cgd error = sbwait(&so->so_rcv);
814 1.1 cgd if (error) {
815 1.1 cgd sbunlock(&so->so_rcv);
816 1.1 cgd splx(s);
817 1.1 cgd return (0);
818 1.1 cgd }
819 1.21 christos if ((m = so->so_rcv.sb_mb) != NULL)
820 1.1 cgd nextrecord = m->m_nextpkt;
821 1.1 cgd }
822 1.1 cgd }
823 1.3 andrew
824 1.3 andrew if (m && pr->pr_flags & PR_ATOMIC) {
825 1.3 andrew flags |= MSG_TRUNC;
826 1.3 andrew if ((flags & MSG_PEEK) == 0)
827 1.3 andrew (void) sbdroprecord(&so->so_rcv);
828 1.3 andrew }
829 1.1 cgd if ((flags & MSG_PEEK) == 0) {
830 1.1 cgd if (m == 0)
831 1.1 cgd so->so_rcv.sb_mb = nextrecord;
832 1.1 cgd if (pr->pr_flags & PR_WANTRCVD && so->so_pcb)
833 1.22 mycroft (*pr->pr_usrreq)(so, PRU_RCVD, (struct mbuf *)0,
834 1.22 mycroft (struct mbuf *)(long)flags, (struct mbuf *)0,
835 1.22 mycroft (struct proc *)0);
836 1.1 cgd }
837 1.3 andrew if (orig_resid == uio->uio_resid && orig_resid &&
838 1.3 andrew (flags & MSG_EOR) == 0 && (so->so_state & SS_CANTRCVMORE) == 0) {
839 1.3 andrew sbunlock(&so->so_rcv);
840 1.3 andrew splx(s);
841 1.3 andrew goto restart;
842 1.3 andrew }
843 1.3 andrew
844 1.1 cgd if (flagsp)
845 1.1 cgd *flagsp |= flags;
846 1.54 lukem release:
847 1.1 cgd sbunlock(&so->so_rcv);
848 1.1 cgd splx(s);
849 1.1 cgd return (error);
850 1.1 cgd }
851 1.1 cgd
852 1.14 mycroft int
853 1.54 lukem soshutdown(struct socket *so, int how)
854 1.1 cgd {
855 1.54 lukem struct protosw *pr;
856 1.34 kleink
857 1.54 lukem pr = so->so_proto;
858 1.34 kleink if (!(how == SHUT_RD || how == SHUT_WR || how == SHUT_RDWR))
859 1.34 kleink return (EINVAL);
860 1.1 cgd
861 1.34 kleink if (how == SHUT_RD || how == SHUT_RDWR)
862 1.1 cgd sorflush(so);
863 1.34 kleink if (how == SHUT_WR || how == SHUT_RDWR)
864 1.22 mycroft return (*pr->pr_usrreq)(so, PRU_SHUTDOWN, (struct mbuf *)0,
865 1.22 mycroft (struct mbuf *)0, (struct mbuf *)0, (struct proc *)0);
866 1.1 cgd return (0);
867 1.1 cgd }
868 1.1 cgd
869 1.14 mycroft void
870 1.54 lukem sorflush(struct socket *so)
871 1.1 cgd {
872 1.54 lukem struct sockbuf *sb, asb;
873 1.54 lukem struct protosw *pr;
874 1.54 lukem int s;
875 1.1 cgd
876 1.54 lukem sb = &so->so_rcv;
877 1.54 lukem pr = so->so_proto;
878 1.1 cgd sb->sb_flags |= SB_NOINTR;
879 1.15 mycroft (void) sblock(sb, M_WAITOK);
880 1.56 thorpej s = splnet();
881 1.1 cgd socantrcvmore(so);
882 1.1 cgd sbunlock(sb);
883 1.1 cgd asb = *sb;
884 1.38 perry memset((caddr_t)sb, 0, sizeof(*sb));
885 1.1 cgd splx(s);
886 1.1 cgd if (pr->pr_flags & PR_RIGHTS && pr->pr_domain->dom_dispose)
887 1.1 cgd (*pr->pr_domain->dom_dispose)(asb.sb_mb);
888 1.1 cgd sbrelease(&asb);
889 1.1 cgd }
890 1.1 cgd
891 1.14 mycroft int
892 1.54 lukem sosetopt(struct socket *so, int level, int optname, struct mbuf *m0)
893 1.1 cgd {
894 1.54 lukem int error;
895 1.54 lukem struct mbuf *m;
896 1.1 cgd
897 1.54 lukem error = 0;
898 1.54 lukem m = m0;
899 1.1 cgd if (level != SOL_SOCKET) {
900 1.1 cgd if (so->so_proto && so->so_proto->pr_ctloutput)
901 1.1 cgd return ((*so->so_proto->pr_ctloutput)
902 1.1 cgd (PRCO_SETOPT, so, level, optname, &m0));
903 1.1 cgd error = ENOPROTOOPT;
904 1.1 cgd } else {
905 1.1 cgd switch (optname) {
906 1.1 cgd
907 1.1 cgd case SO_LINGER:
908 1.36 perry if (m == NULL || m->m_len != sizeof(struct linger)) {
909 1.1 cgd error = EINVAL;
910 1.1 cgd goto bad;
911 1.1 cgd }
912 1.1 cgd so->so_linger = mtod(m, struct linger *)->l_linger;
913 1.1 cgd /* fall thru... */
914 1.1 cgd
915 1.1 cgd case SO_DEBUG:
916 1.1 cgd case SO_KEEPALIVE:
917 1.1 cgd case SO_DONTROUTE:
918 1.1 cgd case SO_USELOOPBACK:
919 1.1 cgd case SO_BROADCAST:
920 1.1 cgd case SO_REUSEADDR:
921 1.15 mycroft case SO_REUSEPORT:
922 1.1 cgd case SO_OOBINLINE:
923 1.26 thorpej case SO_TIMESTAMP:
924 1.36 perry if (m == NULL || m->m_len < sizeof(int)) {
925 1.1 cgd error = EINVAL;
926 1.1 cgd goto bad;
927 1.1 cgd }
928 1.1 cgd if (*mtod(m, int *))
929 1.1 cgd so->so_options |= optname;
930 1.1 cgd else
931 1.1 cgd so->so_options &= ~optname;
932 1.1 cgd break;
933 1.1 cgd
934 1.1 cgd case SO_SNDBUF:
935 1.1 cgd case SO_RCVBUF:
936 1.1 cgd case SO_SNDLOWAT:
937 1.1 cgd case SO_RCVLOWAT:
938 1.28 thorpej {
939 1.28 thorpej int optval;
940 1.28 thorpej
941 1.36 perry if (m == NULL || m->m_len < sizeof(int)) {
942 1.1 cgd error = EINVAL;
943 1.1 cgd goto bad;
944 1.1 cgd }
945 1.28 thorpej
946 1.28 thorpej /*
947 1.28 thorpej * Values < 1 make no sense for any of these
948 1.28 thorpej * options, so disallow them.
949 1.28 thorpej */
950 1.28 thorpej optval = *mtod(m, int *);
951 1.28 thorpej if (optval < 1) {
952 1.28 thorpej error = EINVAL;
953 1.28 thorpej goto bad;
954 1.28 thorpej }
955 1.28 thorpej
956 1.1 cgd switch (optname) {
957 1.1 cgd
958 1.1 cgd case SO_SNDBUF:
959 1.1 cgd case SO_RCVBUF:
960 1.1 cgd if (sbreserve(optname == SO_SNDBUF ?
961 1.1 cgd &so->so_snd : &so->so_rcv,
962 1.28 thorpej (u_long) optval) == 0) {
963 1.1 cgd error = ENOBUFS;
964 1.1 cgd goto bad;
965 1.1 cgd }
966 1.1 cgd break;
967 1.1 cgd
968 1.28 thorpej /*
969 1.28 thorpej * Make sure the low-water is never greater than
970 1.28 thorpej * the high-water.
971 1.28 thorpej */
972 1.1 cgd case SO_SNDLOWAT:
973 1.28 thorpej so->so_snd.sb_lowat =
974 1.28 thorpej (optval > so->so_snd.sb_hiwat) ?
975 1.28 thorpej so->so_snd.sb_hiwat : optval;
976 1.1 cgd break;
977 1.1 cgd case SO_RCVLOWAT:
978 1.28 thorpej so->so_rcv.sb_lowat =
979 1.28 thorpej (optval > so->so_rcv.sb_hiwat) ?
980 1.28 thorpej so->so_rcv.sb_hiwat : optval;
981 1.1 cgd break;
982 1.1 cgd }
983 1.1 cgd break;
984 1.28 thorpej }
985 1.1 cgd
986 1.1 cgd case SO_SNDTIMEO:
987 1.1 cgd case SO_RCVTIMEO:
988 1.1 cgd {
989 1.1 cgd struct timeval *tv;
990 1.1 cgd short val;
991 1.1 cgd
992 1.36 perry if (m == NULL || m->m_len < sizeof(*tv)) {
993 1.1 cgd error = EINVAL;
994 1.1 cgd goto bad;
995 1.1 cgd }
996 1.1 cgd tv = mtod(m, struct timeval *);
997 1.19 cgd if (tv->tv_sec * hz + tv->tv_usec / tick > SHRT_MAX) {
998 1.1 cgd error = EDOM;
999 1.1 cgd goto bad;
1000 1.1 cgd }
1001 1.1 cgd val = tv->tv_sec * hz + tv->tv_usec / tick;
1002 1.1 cgd
1003 1.1 cgd switch (optname) {
1004 1.1 cgd
1005 1.1 cgd case SO_SNDTIMEO:
1006 1.1 cgd so->so_snd.sb_timeo = val;
1007 1.1 cgd break;
1008 1.1 cgd case SO_RCVTIMEO:
1009 1.1 cgd so->so_rcv.sb_timeo = val;
1010 1.1 cgd break;
1011 1.1 cgd }
1012 1.1 cgd break;
1013 1.1 cgd }
1014 1.1 cgd
1015 1.1 cgd default:
1016 1.1 cgd error = ENOPROTOOPT;
1017 1.1 cgd break;
1018 1.1 cgd }
1019 1.15 mycroft if (error == 0 && so->so_proto && so->so_proto->pr_ctloutput) {
1020 1.15 mycroft (void) ((*so->so_proto->pr_ctloutput)
1021 1.15 mycroft (PRCO_SETOPT, so, level, optname, &m0));
1022 1.15 mycroft m = NULL; /* freed by protocol */
1023 1.15 mycroft }
1024 1.1 cgd }
1025 1.54 lukem bad:
1026 1.1 cgd if (m)
1027 1.1 cgd (void) m_free(m);
1028 1.1 cgd return (error);
1029 1.1 cgd }
1030 1.1 cgd
1031 1.14 mycroft int
1032 1.54 lukem sogetopt(struct socket *so, int level, int optname, struct mbuf **mp)
1033 1.1 cgd {
1034 1.54 lukem struct mbuf *m;
1035 1.1 cgd
1036 1.1 cgd if (level != SOL_SOCKET) {
1037 1.1 cgd if (so->so_proto && so->so_proto->pr_ctloutput) {
1038 1.1 cgd return ((*so->so_proto->pr_ctloutput)
1039 1.1 cgd (PRCO_GETOPT, so, level, optname, mp));
1040 1.1 cgd } else
1041 1.1 cgd return (ENOPROTOOPT);
1042 1.1 cgd } else {
1043 1.1 cgd m = m_get(M_WAIT, MT_SOOPTS);
1044 1.36 perry m->m_len = sizeof(int);
1045 1.1 cgd
1046 1.1 cgd switch (optname) {
1047 1.1 cgd
1048 1.1 cgd case SO_LINGER:
1049 1.36 perry m->m_len = sizeof(struct linger);
1050 1.1 cgd mtod(m, struct linger *)->l_onoff =
1051 1.1 cgd so->so_options & SO_LINGER;
1052 1.1 cgd mtod(m, struct linger *)->l_linger = so->so_linger;
1053 1.1 cgd break;
1054 1.1 cgd
1055 1.1 cgd case SO_USELOOPBACK:
1056 1.1 cgd case SO_DONTROUTE:
1057 1.1 cgd case SO_DEBUG:
1058 1.1 cgd case SO_KEEPALIVE:
1059 1.1 cgd case SO_REUSEADDR:
1060 1.15 mycroft case SO_REUSEPORT:
1061 1.1 cgd case SO_BROADCAST:
1062 1.1 cgd case SO_OOBINLINE:
1063 1.26 thorpej case SO_TIMESTAMP:
1064 1.1 cgd *mtod(m, int *) = so->so_options & optname;
1065 1.1 cgd break;
1066 1.1 cgd
1067 1.1 cgd case SO_TYPE:
1068 1.1 cgd *mtod(m, int *) = so->so_type;
1069 1.1 cgd break;
1070 1.1 cgd
1071 1.1 cgd case SO_ERROR:
1072 1.1 cgd *mtod(m, int *) = so->so_error;
1073 1.1 cgd so->so_error = 0;
1074 1.1 cgd break;
1075 1.1 cgd
1076 1.1 cgd case SO_SNDBUF:
1077 1.1 cgd *mtod(m, int *) = so->so_snd.sb_hiwat;
1078 1.1 cgd break;
1079 1.1 cgd
1080 1.1 cgd case SO_RCVBUF:
1081 1.1 cgd *mtod(m, int *) = so->so_rcv.sb_hiwat;
1082 1.1 cgd break;
1083 1.1 cgd
1084 1.1 cgd case SO_SNDLOWAT:
1085 1.1 cgd *mtod(m, int *) = so->so_snd.sb_lowat;
1086 1.1 cgd break;
1087 1.1 cgd
1088 1.1 cgd case SO_RCVLOWAT:
1089 1.1 cgd *mtod(m, int *) = so->so_rcv.sb_lowat;
1090 1.1 cgd break;
1091 1.1 cgd
1092 1.1 cgd case SO_SNDTIMEO:
1093 1.1 cgd case SO_RCVTIMEO:
1094 1.1 cgd {
1095 1.1 cgd int val = (optname == SO_SNDTIMEO ?
1096 1.1 cgd so->so_snd.sb_timeo : so->so_rcv.sb_timeo);
1097 1.1 cgd
1098 1.1 cgd m->m_len = sizeof(struct timeval);
1099 1.1 cgd mtod(m, struct timeval *)->tv_sec = val / hz;
1100 1.1 cgd mtod(m, struct timeval *)->tv_usec =
1101 1.27 kleink (val % hz) * tick;
1102 1.1 cgd break;
1103 1.1 cgd }
1104 1.1 cgd
1105 1.1 cgd default:
1106 1.1 cgd (void)m_free(m);
1107 1.1 cgd return (ENOPROTOOPT);
1108 1.1 cgd }
1109 1.1 cgd *mp = m;
1110 1.1 cgd return (0);
1111 1.1 cgd }
1112 1.1 cgd }
1113 1.1 cgd
1114 1.14 mycroft void
1115 1.54 lukem sohasoutofband(struct socket *so)
1116 1.1 cgd {
1117 1.1 cgd struct proc *p;
1118 1.1 cgd
1119 1.1 cgd if (so->so_pgid < 0)
1120 1.1 cgd gsignal(-so->so_pgid, SIGURG);
1121 1.1 cgd else if (so->so_pgid > 0 && (p = pfind(so->so_pgid)) != 0)
1122 1.1 cgd psignal(p, SIGURG);
1123 1.2 cgd selwakeup(&so->so_rcv.sb_sel);
1124 1.56.2.1 lukem }
1125 1.56.2.1 lukem
1126 1.56.2.1 lukem
1127 1.56.2.1 lukem int
1128 1.56.2.1 lukem soo_kqfilter(struct file *fp, struct knote *kn)
1129 1.56.2.1 lukem {
1130 1.56.2.1 lukem struct socket *so;
1131 1.56.2.1 lukem struct sockbuf *sb;
1132 1.56.2.1 lukem int s;
1133 1.56.2.1 lukem
1134 1.56.2.1 lukem so = (struct socket *)kn->kn_fp->f_data;
1135 1.56.2.1 lukem switch (kn->kn_filter) {
1136 1.56.2.1 lukem case EVFILT_READ:
1137 1.56.2.1 lukem if (so->so_options & SO_ACCEPTCONN)
1138 1.56.2.1 lukem kn->kn_fop = &solisten_filtops;
1139 1.56.2.1 lukem else
1140 1.56.2.1 lukem kn->kn_fop = &soread_filtops;
1141 1.56.2.1 lukem sb = &so->so_rcv;
1142 1.56.2.1 lukem break;
1143 1.56.2.1 lukem case EVFILT_WRITE:
1144 1.56.2.1 lukem kn->kn_fop = &sowrite_filtops;
1145 1.56.2.1 lukem sb = &so->so_snd;
1146 1.56.2.1 lukem break;
1147 1.56.2.1 lukem default:
1148 1.56.2.1 lukem return (1);
1149 1.56.2.1 lukem }
1150 1.56.2.1 lukem s = splnet(); /* XXXLUKEM: maybe splsoftnet() ? */
1151 1.56.2.1 lukem SLIST_INSERT_HEAD(&sb->sb_sel.si_klist, kn, kn_selnext);
1152 1.56.2.1 lukem sb->sb_flags |= SB_KNOTE;
1153 1.56.2.1 lukem splx(s);
1154 1.56.2.1 lukem return (0);
1155 1.56.2.1 lukem }
1156 1.56.2.1 lukem
1157 1.56.2.1 lukem static void
1158 1.56.2.1 lukem filt_sordetach(struct knote *kn)
1159 1.56.2.1 lukem {
1160 1.56.2.1 lukem struct socket *so;
1161 1.56.2.1 lukem int s;
1162 1.56.2.1 lukem
1163 1.56.2.1 lukem so = (struct socket *)kn->kn_fp->f_data;
1164 1.56.2.1 lukem s = splnet(); /* XXXLUKEM: maybe splsoftnet() ? */
1165 1.56.2.1 lukem SLIST_REMOVE(&so->so_rcv.sb_sel.si_klist, kn, knote, kn_selnext);
1166 1.56.2.1 lukem if (SLIST_EMPTY(&so->so_rcv.sb_sel.si_klist))
1167 1.56.2.1 lukem so->so_rcv.sb_flags &= ~SB_KNOTE;
1168 1.56.2.1 lukem splx(s);
1169 1.56.2.1 lukem }
1170 1.56.2.1 lukem
1171 1.56.2.1 lukem /*ARGSUSED*/
1172 1.56.2.1 lukem static int
1173 1.56.2.1 lukem filt_soread(struct knote *kn, long hint)
1174 1.56.2.1 lukem {
1175 1.56.2.1 lukem struct socket *so;
1176 1.56.2.1 lukem
1177 1.56.2.1 lukem so = (struct socket *)kn->kn_fp->f_data;
1178 1.56.2.1 lukem kn->kn_data = so->so_rcv.sb_cc;
1179 1.56.2.1 lukem if (so->so_state & SS_CANTRCVMORE) {
1180 1.56.2.1 lukem kn->kn_flags |= EV_EOF;
1181 1.56.2.1 lukem kn->kn_fflags = so->so_error;
1182 1.56.2.1 lukem return (1);
1183 1.56.2.1 lukem }
1184 1.56.2.1 lukem if (so->so_error) /* temporary udp error */
1185 1.56.2.1 lukem return (1);
1186 1.56.2.1 lukem if (kn->kn_sfflags & NOTE_LOWAT)
1187 1.56.2.1 lukem return (kn->kn_data >= kn->kn_sdata);
1188 1.56.2.1 lukem return (kn->kn_data >= so->so_rcv.sb_lowat);
1189 1.56.2.1 lukem }
1190 1.56.2.1 lukem
1191 1.56.2.1 lukem static void
1192 1.56.2.1 lukem filt_sowdetach(struct knote *kn)
1193 1.56.2.1 lukem {
1194 1.56.2.1 lukem struct socket *so;
1195 1.56.2.1 lukem int s;
1196 1.56.2.1 lukem
1197 1.56.2.1 lukem so = (struct socket *)kn->kn_fp->f_data;
1198 1.56.2.1 lukem s = splnet(); /* XXXLUKEM: maybe splsoftnet() ? */
1199 1.56.2.1 lukem SLIST_REMOVE(&so->so_snd.sb_sel.si_klist, kn, knote, kn_selnext);
1200 1.56.2.1 lukem if (SLIST_EMPTY(&so->so_snd.sb_sel.si_klist))
1201 1.56.2.1 lukem so->so_snd.sb_flags &= ~SB_KNOTE;
1202 1.56.2.1 lukem splx(s);
1203 1.56.2.1 lukem }
1204 1.56.2.1 lukem
1205 1.56.2.1 lukem /*ARGSUSED*/
1206 1.56.2.1 lukem static int
1207 1.56.2.1 lukem filt_sowrite(struct knote *kn, long hint)
1208 1.56.2.1 lukem {
1209 1.56.2.1 lukem struct socket *so;
1210 1.56.2.1 lukem
1211 1.56.2.1 lukem so = (struct socket *)kn->kn_fp->f_data;
1212 1.56.2.1 lukem kn->kn_data = sbspace(&so->so_snd);
1213 1.56.2.1 lukem if (so->so_state & SS_CANTSENDMORE) {
1214 1.56.2.1 lukem kn->kn_flags |= EV_EOF;
1215 1.56.2.1 lukem kn->kn_fflags = so->so_error;
1216 1.56.2.1 lukem return (1);
1217 1.56.2.1 lukem }
1218 1.56.2.1 lukem if (so->so_error) /* temporary udp error */
1219 1.56.2.1 lukem return (1);
1220 1.56.2.1 lukem if (((so->so_state & SS_ISCONNECTED) == 0) &&
1221 1.56.2.1 lukem (so->so_proto->pr_flags & PR_CONNREQUIRED))
1222 1.56.2.1 lukem return (0);
1223 1.56.2.1 lukem if (kn->kn_sfflags & NOTE_LOWAT)
1224 1.56.2.1 lukem return (kn->kn_data >= kn->kn_sdata);
1225 1.56.2.1 lukem return (kn->kn_data >= so->so_snd.sb_lowat);
1226 1.56.2.1 lukem }
1227 1.56.2.1 lukem
1228 1.56.2.1 lukem /*ARGSUSED*/
1229 1.56.2.1 lukem static int
1230 1.56.2.1 lukem filt_solisten(struct knote *kn, long hint)
1231 1.56.2.1 lukem {
1232 1.56.2.1 lukem struct socket *so;
1233 1.56.2.1 lukem
1234 1.56.2.1 lukem so = (struct socket *)kn->kn_fp->f_data;
1235 1.56.2.1 lukem #if 0
1236 1.56.2.1 lukem /*
1237 1.56.2.1 lukem * XXXLUKEM: this was freebsd's code. it appears that they
1238 1.56.2.1 lukem * XXXLUKEM: modified the socket code to store the count
1239 1.56.2.1 lukem * XXXLUKEM: of all connections in so_qlen, and separately
1240 1.56.2.1 lukem * XXXLUKEM: track the number of incompletes in so_incqlen.
1241 1.56.2.1 lukem * XXXLUKEM: as we appear to keep only completed connections
1242 1.56.2.1 lukem * XXXLUKEM: on so_qlen we can just return that.
1243 1.56.2.1 lukem * XXXLUKEM: that said, a socket guru should double check for me :)
1244 1.56.2.1 lukem */
1245 1.56.2.1 lukem kn->kn_data = so->so_qlen - so->so_incqlen;
1246 1.56.2.1 lukem return (! TAILQ_EMPTY(&so->so_comp));
1247 1.56.2.1 lukem #else
1248 1.56.2.1 lukem kn->kn_data = so->so_qlen;
1249 1.56.2.1 lukem return (kn->kn_data > 0);
1250 1.56.2.1 lukem #endif
1251 1.1 cgd }
1252