uipc_socket.c revision 1.38 1 1.38 perry /* $NetBSD: uipc_socket.c,v 1.38 1998/08/04 04:03:17 perry 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.37 thorpej
55 1.37 thorpej struct pool socket_pool;
56 1.37 thorpej
57 1.37 thorpej void
58 1.37 thorpej soinit()
59 1.37 thorpej {
60 1.37 thorpej
61 1.37 thorpej pool_init(&socket_pool, sizeof(struct socket), 0, 0, 0,
62 1.37 thorpej "sockpl", 0, NULL, NULL, M_SOCKET);
63 1.37 thorpej }
64 1.1 cgd
65 1.1 cgd /*
66 1.1 cgd * Socket operation routines.
67 1.1 cgd * These routines are called by the routines in
68 1.1 cgd * sys_socket.c or from a system process, and
69 1.1 cgd * implement the semantics of socket operations by
70 1.1 cgd * switching out to the protocol specific routines.
71 1.1 cgd */
72 1.1 cgd /*ARGSUSED*/
73 1.3 andrew int
74 1.1 cgd socreate(dom, aso, type, proto)
75 1.11 mycroft int dom;
76 1.1 cgd struct socket **aso;
77 1.1 cgd register int type;
78 1.1 cgd int proto;
79 1.1 cgd {
80 1.1 cgd struct proc *p = curproc; /* XXX */
81 1.1 cgd register struct protosw *prp;
82 1.1 cgd register struct socket *so;
83 1.1 cgd register int error;
84 1.1 cgd
85 1.1 cgd if (proto)
86 1.1 cgd prp = pffindproto(dom, proto, type);
87 1.1 cgd else
88 1.1 cgd prp = pffindtype(dom, type);
89 1.15 mycroft if (prp == 0 || prp->pr_usrreq == 0)
90 1.1 cgd return (EPROTONOSUPPORT);
91 1.1 cgd if (prp->pr_type != type)
92 1.1 cgd return (EPROTOTYPE);
93 1.37 thorpej so = pool_get(&socket_pool, PR_WAITOK);
94 1.38 perry memset((caddr_t)so, 0, sizeof(*so));
95 1.31 thorpej TAILQ_INIT(&so->so_q0);
96 1.31 thorpej TAILQ_INIT(&so->so_q);
97 1.1 cgd so->so_type = type;
98 1.1 cgd so->so_proto = prp;
99 1.33 matt so->so_send = sosend;
100 1.33 matt so->so_receive = soreceive;
101 1.22 mycroft error = (*prp->pr_usrreq)(so, PRU_ATTACH, (struct mbuf *)0,
102 1.22 mycroft (struct mbuf *)(long)proto, (struct mbuf *)0, p);
103 1.1 cgd if (error) {
104 1.1 cgd so->so_state |= SS_NOFDREF;
105 1.1 cgd sofree(so);
106 1.1 cgd return (error);
107 1.1 cgd }
108 1.10 deraadt #ifdef COMPAT_SUNOS
109 1.18 christos {
110 1.18 christos extern struct emul emul_sunos;
111 1.18 christos if (p->p_emul == &emul_sunos && type == SOCK_DGRAM)
112 1.18 christos so->so_options |= SO_BROADCAST;
113 1.18 christos }
114 1.10 deraadt #endif
115 1.1 cgd *aso = so;
116 1.1 cgd return (0);
117 1.1 cgd }
118 1.1 cgd
119 1.3 andrew int
120 1.1 cgd sobind(so, nam)
121 1.1 cgd struct socket *so;
122 1.1 cgd struct mbuf *nam;
123 1.1 cgd {
124 1.22 mycroft struct proc *p = curproc; /* XXX */
125 1.20 mycroft int s = splsoftnet();
126 1.1 cgd int error;
127 1.1 cgd
128 1.22 mycroft error = (*so->so_proto->pr_usrreq)(so, PRU_BIND, (struct mbuf *)0,
129 1.22 mycroft nam, (struct mbuf *)0, p);
130 1.1 cgd splx(s);
131 1.1 cgd return (error);
132 1.1 cgd }
133 1.1 cgd
134 1.3 andrew int
135 1.1 cgd solisten(so, backlog)
136 1.1 cgd register struct socket *so;
137 1.1 cgd int backlog;
138 1.1 cgd {
139 1.20 mycroft int s = splsoftnet(), error;
140 1.1 cgd
141 1.22 mycroft error = (*so->so_proto->pr_usrreq)(so, PRU_LISTEN, (struct mbuf *)0,
142 1.22 mycroft (struct mbuf *)0, (struct mbuf *)0, (struct proc *)0);
143 1.1 cgd if (error) {
144 1.1 cgd splx(s);
145 1.1 cgd return (error);
146 1.1 cgd }
147 1.31 thorpej if (so->so_q.tqh_first == NULL)
148 1.1 cgd so->so_options |= SO_ACCEPTCONN;
149 1.1 cgd if (backlog < 0)
150 1.1 cgd backlog = 0;
151 1.1 cgd so->so_qlimit = min(backlog, SOMAXCONN);
152 1.1 cgd splx(s);
153 1.1 cgd return (0);
154 1.1 cgd }
155 1.1 cgd
156 1.21 christos void
157 1.1 cgd sofree(so)
158 1.1 cgd register struct socket *so;
159 1.1 cgd {
160 1.1 cgd
161 1.1 cgd if (so->so_pcb || (so->so_state & SS_NOFDREF) == 0)
162 1.1 cgd return;
163 1.1 cgd if (so->so_head) {
164 1.1 cgd if (!soqremque(so, 0) && !soqremque(so, 1))
165 1.1 cgd panic("sofree dq");
166 1.1 cgd so->so_head = 0;
167 1.1 cgd }
168 1.1 cgd sbrelease(&so->so_snd);
169 1.1 cgd sorflush(so);
170 1.37 thorpej pool_put(&socket_pool, so);
171 1.1 cgd }
172 1.1 cgd
173 1.1 cgd /*
174 1.1 cgd * Close a socket on last file table reference removal.
175 1.1 cgd * Initiate disconnect if connected.
176 1.1 cgd * Free socket when disconnect complete.
177 1.1 cgd */
178 1.3 andrew int
179 1.1 cgd soclose(so)
180 1.1 cgd register struct socket *so;
181 1.1 cgd {
182 1.20 mycroft int s = splsoftnet(); /* conservative */
183 1.1 cgd int error = 0;
184 1.1 cgd
185 1.1 cgd if (so->so_options & SO_ACCEPTCONN) {
186 1.31 thorpej while (so->so_q0.tqh_first)
187 1.31 thorpej (void) soabort(so->so_q0.tqh_first);
188 1.31 thorpej while (so->so_q.tqh_first)
189 1.31 thorpej (void) soabort(so->so_q.tqh_first);
190 1.1 cgd }
191 1.1 cgd if (so->so_pcb == 0)
192 1.1 cgd goto discard;
193 1.1 cgd if (so->so_state & SS_ISCONNECTED) {
194 1.1 cgd if ((so->so_state & SS_ISDISCONNECTING) == 0) {
195 1.1 cgd error = sodisconnect(so);
196 1.1 cgd if (error)
197 1.1 cgd goto drop;
198 1.1 cgd }
199 1.1 cgd if (so->so_options & SO_LINGER) {
200 1.1 cgd if ((so->so_state & SS_ISDISCONNECTING) &&
201 1.1 cgd (so->so_state & SS_NBIO))
202 1.1 cgd goto drop;
203 1.21 christos while (so->so_state & SS_ISCONNECTED) {
204 1.21 christos error = tsleep((caddr_t)&so->so_timeo,
205 1.21 christos PSOCK | PCATCH, netcls,
206 1.30 thorpej so->so_linger * hz);
207 1.21 christos if (error)
208 1.1 cgd break;
209 1.21 christos }
210 1.1 cgd }
211 1.1 cgd }
212 1.1 cgd drop:
213 1.1 cgd if (so->so_pcb) {
214 1.22 mycroft int error2 = (*so->so_proto->pr_usrreq)(so, PRU_DETACH,
215 1.22 mycroft (struct mbuf *)0, (struct mbuf *)0, (struct mbuf *)0,
216 1.22 mycroft (struct proc *)0);
217 1.1 cgd if (error == 0)
218 1.1 cgd error = error2;
219 1.1 cgd }
220 1.1 cgd discard:
221 1.1 cgd if (so->so_state & SS_NOFDREF)
222 1.1 cgd panic("soclose: NOFDREF");
223 1.1 cgd so->so_state |= SS_NOFDREF;
224 1.1 cgd sofree(so);
225 1.1 cgd splx(s);
226 1.1 cgd return (error);
227 1.1 cgd }
228 1.1 cgd
229 1.1 cgd /*
230 1.20 mycroft * Must be called at splsoftnet...
231 1.1 cgd */
232 1.3 andrew int
233 1.1 cgd soabort(so)
234 1.1 cgd struct socket *so;
235 1.1 cgd {
236 1.1 cgd
237 1.22 mycroft return (*so->so_proto->pr_usrreq)(so, PRU_ABORT, (struct mbuf *)0,
238 1.22 mycroft (struct mbuf *)0, (struct mbuf *)0, (struct proc *)0);
239 1.1 cgd }
240 1.1 cgd
241 1.3 andrew int
242 1.1 cgd soaccept(so, nam)
243 1.1 cgd register struct socket *so;
244 1.1 cgd struct mbuf *nam;
245 1.1 cgd {
246 1.20 mycroft int s = splsoftnet();
247 1.1 cgd int error;
248 1.1 cgd
249 1.1 cgd if ((so->so_state & SS_NOFDREF) == 0)
250 1.1 cgd panic("soaccept: !NOFDREF");
251 1.1 cgd so->so_state &= ~SS_NOFDREF;
252 1.22 mycroft error = (*so->so_proto->pr_usrreq)(so, PRU_ACCEPT, (struct mbuf *)0,
253 1.22 mycroft nam, (struct mbuf *)0, (struct proc *)0);
254 1.1 cgd splx(s);
255 1.1 cgd return (error);
256 1.1 cgd }
257 1.1 cgd
258 1.3 andrew int
259 1.1 cgd soconnect(so, nam)
260 1.1 cgd register struct socket *so;
261 1.1 cgd struct mbuf *nam;
262 1.1 cgd {
263 1.23 mycroft struct proc *p = curproc; /* XXX */
264 1.1 cgd int s;
265 1.1 cgd int error;
266 1.1 cgd
267 1.1 cgd if (so->so_options & SO_ACCEPTCONN)
268 1.1 cgd return (EOPNOTSUPP);
269 1.20 mycroft s = splsoftnet();
270 1.1 cgd /*
271 1.1 cgd * If protocol is connection-based, can only connect once.
272 1.1 cgd * Otherwise, if connected, try to disconnect first.
273 1.1 cgd * This allows user to disconnect by connecting to, e.g.,
274 1.1 cgd * a null address.
275 1.1 cgd */
276 1.1 cgd if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING) &&
277 1.1 cgd ((so->so_proto->pr_flags & PR_CONNREQUIRED) ||
278 1.1 cgd (error = sodisconnect(so))))
279 1.1 cgd error = EISCONN;
280 1.1 cgd else
281 1.1 cgd error = (*so->so_proto->pr_usrreq)(so, PRU_CONNECT,
282 1.23 mycroft (struct mbuf *)0, nam, (struct mbuf *)0, p);
283 1.1 cgd splx(s);
284 1.1 cgd return (error);
285 1.1 cgd }
286 1.1 cgd
287 1.3 andrew int
288 1.1 cgd soconnect2(so1, so2)
289 1.1 cgd register struct socket *so1;
290 1.1 cgd struct socket *so2;
291 1.1 cgd {
292 1.20 mycroft int s = splsoftnet();
293 1.1 cgd int error;
294 1.1 cgd
295 1.22 mycroft error = (*so1->so_proto->pr_usrreq)(so1, PRU_CONNECT2,
296 1.22 mycroft (struct mbuf *)0, (struct mbuf *)so2, (struct mbuf *)0,
297 1.22 mycroft (struct proc *)0);
298 1.1 cgd splx(s);
299 1.1 cgd return (error);
300 1.1 cgd }
301 1.1 cgd
302 1.3 andrew int
303 1.1 cgd sodisconnect(so)
304 1.1 cgd register struct socket *so;
305 1.1 cgd {
306 1.20 mycroft int s = splsoftnet();
307 1.1 cgd int error;
308 1.1 cgd
309 1.1 cgd if ((so->so_state & SS_ISCONNECTED) == 0) {
310 1.1 cgd error = ENOTCONN;
311 1.1 cgd goto bad;
312 1.1 cgd }
313 1.1 cgd if (so->so_state & SS_ISDISCONNECTING) {
314 1.1 cgd error = EALREADY;
315 1.1 cgd goto bad;
316 1.1 cgd }
317 1.22 mycroft error = (*so->so_proto->pr_usrreq)(so, PRU_DISCONNECT,
318 1.22 mycroft (struct mbuf *)0, (struct mbuf *)0, (struct mbuf *)0,
319 1.22 mycroft (struct proc *)0);
320 1.1 cgd bad:
321 1.1 cgd splx(s);
322 1.1 cgd return (error);
323 1.1 cgd }
324 1.1 cgd
325 1.15 mycroft #define SBLOCKWAIT(f) (((f) & MSG_DONTWAIT) ? M_NOWAIT : M_WAITOK)
326 1.1 cgd /*
327 1.1 cgd * Send on a socket.
328 1.1 cgd * If send must go all at once and message is larger than
329 1.1 cgd * send buffering, then hard error.
330 1.1 cgd * Lock against other senders.
331 1.1 cgd * If must go all at once and not enough room now, then
332 1.1 cgd * inform user that this would block and do nothing.
333 1.1 cgd * Otherwise, if nonblocking, send as much as possible.
334 1.1 cgd * The data to be sent is described by "uio" if nonzero,
335 1.1 cgd * otherwise by the mbuf chain "top" (which must be null
336 1.1 cgd * if uio is not). Data provided in mbuf chain must be small
337 1.1 cgd * enough to send all at once.
338 1.1 cgd *
339 1.1 cgd * Returns nonzero on error, timeout or signal; callers
340 1.1 cgd * must check for short counts if EINTR/ERESTART are returned.
341 1.1 cgd * Data and control buffers are freed on return.
342 1.1 cgd */
343 1.3 andrew int
344 1.1 cgd sosend(so, addr, uio, top, control, flags)
345 1.1 cgd register struct socket *so;
346 1.1 cgd struct mbuf *addr;
347 1.1 cgd struct uio *uio;
348 1.1 cgd struct mbuf *top;
349 1.1 cgd struct mbuf *control;
350 1.1 cgd int flags;
351 1.1 cgd {
352 1.15 mycroft struct proc *p = curproc; /* XXX */
353 1.1 cgd struct mbuf **mp;
354 1.1 cgd register struct mbuf *m;
355 1.1 cgd register long space, len, resid;
356 1.1 cgd int clen = 0, error, s, dontroute, mlen;
357 1.1 cgd int atomic = sosendallatonce(so) || top;
358 1.1 cgd
359 1.1 cgd if (uio)
360 1.1 cgd resid = uio->uio_resid;
361 1.1 cgd else
362 1.1 cgd resid = top->m_pkthdr.len;
363 1.7 cgd /*
364 1.7 cgd * In theory resid should be unsigned.
365 1.7 cgd * However, space must be signed, as it might be less than 0
366 1.7 cgd * if we over-committed, and we must use a signed comparison
367 1.7 cgd * of space and resid. On the other hand, a negative resid
368 1.7 cgd * causes us to loop sending 0-length segments to the protocol.
369 1.7 cgd */
370 1.29 mycroft if (resid < 0) {
371 1.29 mycroft error = EINVAL;
372 1.29 mycroft goto out;
373 1.29 mycroft }
374 1.1 cgd dontroute =
375 1.1 cgd (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0 &&
376 1.1 cgd (so->so_proto->pr_flags & PR_ATOMIC);
377 1.12 mycroft p->p_stats->p_ru.ru_msgsnd++;
378 1.1 cgd if (control)
379 1.1 cgd clen = control->m_len;
380 1.1 cgd #define snderr(errno) { error = errno; splx(s); goto release; }
381 1.1 cgd
382 1.1 cgd restart:
383 1.21 christos if ((error = sblock(&so->so_snd, SBLOCKWAIT(flags))) != 0)
384 1.1 cgd goto out;
385 1.1 cgd do {
386 1.20 mycroft s = splsoftnet();
387 1.1 cgd if (so->so_state & SS_CANTSENDMORE)
388 1.1 cgd snderr(EPIPE);
389 1.1 cgd if (so->so_error)
390 1.1 cgd snderr(so->so_error);
391 1.1 cgd if ((so->so_state & SS_ISCONNECTED) == 0) {
392 1.1 cgd if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
393 1.1 cgd if ((so->so_state & SS_ISCONFIRMING) == 0 &&
394 1.1 cgd !(resid == 0 && clen != 0))
395 1.1 cgd snderr(ENOTCONN);
396 1.1 cgd } else if (addr == 0)
397 1.1 cgd snderr(EDESTADDRREQ);
398 1.1 cgd }
399 1.1 cgd space = sbspace(&so->so_snd);
400 1.1 cgd if (flags & MSG_OOB)
401 1.1 cgd space += 1024;
402 1.21 christos if ((atomic && resid > so->so_snd.sb_hiwat) ||
403 1.11 mycroft clen > so->so_snd.sb_hiwat)
404 1.11 mycroft snderr(EMSGSIZE);
405 1.11 mycroft if (space < resid + clen && uio &&
406 1.1 cgd (atomic || space < so->so_snd.sb_lowat || space < clen)) {
407 1.1 cgd if (so->so_state & SS_NBIO)
408 1.1 cgd snderr(EWOULDBLOCK);
409 1.1 cgd sbunlock(&so->so_snd);
410 1.1 cgd error = sbwait(&so->so_snd);
411 1.1 cgd splx(s);
412 1.1 cgd if (error)
413 1.1 cgd goto out;
414 1.1 cgd goto restart;
415 1.1 cgd }
416 1.1 cgd splx(s);
417 1.1 cgd mp = ⊤
418 1.1 cgd space -= clen;
419 1.1 cgd do {
420 1.1 cgd if (uio == NULL) {
421 1.1 cgd /*
422 1.1 cgd * Data is prepackaged in "top".
423 1.1 cgd */
424 1.1 cgd resid = 0;
425 1.1 cgd if (flags & MSG_EOR)
426 1.1 cgd top->m_flags |= M_EOR;
427 1.1 cgd } else do {
428 1.1 cgd if (top == 0) {
429 1.1 cgd MGETHDR(m, M_WAIT, MT_DATA);
430 1.1 cgd mlen = MHLEN;
431 1.1 cgd m->m_pkthdr.len = 0;
432 1.1 cgd m->m_pkthdr.rcvif = (struct ifnet *)0;
433 1.1 cgd } else {
434 1.1 cgd MGET(m, M_WAIT, MT_DATA);
435 1.1 cgd mlen = MLEN;
436 1.1 cgd }
437 1.15 mycroft if (resid >= MINCLSIZE && space >= MCLBYTES) {
438 1.1 cgd MCLGET(m, M_WAIT);
439 1.1 cgd if ((m->m_flags & M_EXT) == 0)
440 1.1 cgd goto nopages;
441 1.1 cgd mlen = MCLBYTES;
442 1.15 mycroft #ifdef MAPPED_MBUFS
443 1.15 mycroft len = min(MCLBYTES, resid);
444 1.15 mycroft #else
445 1.15 mycroft if (atomic && top == 0) {
446 1.15 mycroft len = min(MCLBYTES - max_hdr, resid);
447 1.15 mycroft m->m_data += max_hdr;
448 1.15 mycroft } else
449 1.15 mycroft len = min(MCLBYTES, resid);
450 1.15 mycroft #endif
451 1.25 explorer space -= len;
452 1.1 cgd } else {
453 1.1 cgd nopages:
454 1.1 cgd len = min(min(mlen, resid), space);
455 1.15 mycroft space -= len;
456 1.1 cgd /*
457 1.1 cgd * For datagram protocols, leave room
458 1.1 cgd * for protocol headers in first mbuf.
459 1.1 cgd */
460 1.1 cgd if (atomic && top == 0 && len < mlen)
461 1.1 cgd MH_ALIGN(m, len);
462 1.1 cgd }
463 1.1 cgd error = uiomove(mtod(m, caddr_t), (int)len, uio);
464 1.1 cgd resid = uio->uio_resid;
465 1.1 cgd m->m_len = len;
466 1.1 cgd *mp = m;
467 1.1 cgd top->m_pkthdr.len += len;
468 1.1 cgd if (error)
469 1.1 cgd goto release;
470 1.1 cgd mp = &m->m_next;
471 1.1 cgd if (resid <= 0) {
472 1.1 cgd if (flags & MSG_EOR)
473 1.1 cgd top->m_flags |= M_EOR;
474 1.1 cgd break;
475 1.1 cgd }
476 1.1 cgd } while (space > 0 && atomic);
477 1.1 cgd if (dontroute)
478 1.1 cgd so->so_options |= SO_DONTROUTE;
479 1.20 mycroft s = splsoftnet(); /* XXX */
480 1.22 mycroft error = (*so->so_proto->pr_usrreq)(so,
481 1.24 mycroft (flags & MSG_OOB) ? PRU_SENDOOB : PRU_SEND,
482 1.24 mycroft top, addr, control, p);
483 1.1 cgd splx(s);
484 1.1 cgd if (dontroute)
485 1.1 cgd so->so_options &= ~SO_DONTROUTE;
486 1.1 cgd clen = 0;
487 1.1 cgd control = 0;
488 1.1 cgd top = 0;
489 1.1 cgd mp = ⊤
490 1.1 cgd if (error)
491 1.1 cgd goto release;
492 1.1 cgd } while (resid && space > 0);
493 1.1 cgd } while (resid);
494 1.1 cgd
495 1.1 cgd release:
496 1.1 cgd sbunlock(&so->so_snd);
497 1.1 cgd out:
498 1.1 cgd if (top)
499 1.1 cgd m_freem(top);
500 1.1 cgd if (control)
501 1.1 cgd m_freem(control);
502 1.1 cgd return (error);
503 1.1 cgd }
504 1.1 cgd
505 1.1 cgd /*
506 1.1 cgd * Implement receive operations on a socket.
507 1.1 cgd * We depend on the way that records are added to the sockbuf
508 1.1 cgd * by sbappend*. In particular, each record (mbufs linked through m_next)
509 1.1 cgd * must begin with an address if the protocol so specifies,
510 1.1 cgd * followed by an optional mbuf or mbufs containing ancillary data,
511 1.1 cgd * and then zero or more mbufs of data.
512 1.1 cgd * In order to avoid blocking network interrupts for the entire time here,
513 1.1 cgd * we splx() while doing the actual copy to user space.
514 1.1 cgd * Although the sockbuf is locked, new data may still be appended,
515 1.1 cgd * and thus we must maintain consistency of the sockbuf during that time.
516 1.1 cgd *
517 1.1 cgd * The caller may receive the data as a single mbuf chain by supplying
518 1.1 cgd * an mbuf **mp0 for use in returning the chain. The uio is then used
519 1.1 cgd * only for the count in uio_resid.
520 1.1 cgd */
521 1.3 andrew int
522 1.1 cgd soreceive(so, paddr, uio, mp0, controlp, flagsp)
523 1.1 cgd register struct socket *so;
524 1.1 cgd struct mbuf **paddr;
525 1.1 cgd struct uio *uio;
526 1.1 cgd struct mbuf **mp0;
527 1.1 cgd struct mbuf **controlp;
528 1.1 cgd int *flagsp;
529 1.1 cgd {
530 1.1 cgd register struct mbuf *m, **mp;
531 1.1 cgd register int flags, len, error, s, offset;
532 1.1 cgd struct protosw *pr = so->so_proto;
533 1.1 cgd struct mbuf *nextrecord;
534 1.21 christos int moff, type = 0;
535 1.3 andrew int orig_resid = uio->uio_resid;
536 1.1 cgd
537 1.1 cgd mp = mp0;
538 1.1 cgd if (paddr)
539 1.1 cgd *paddr = 0;
540 1.1 cgd if (controlp)
541 1.1 cgd *controlp = 0;
542 1.1 cgd if (flagsp)
543 1.1 cgd flags = *flagsp &~ MSG_EOR;
544 1.1 cgd else
545 1.1 cgd flags = 0;
546 1.1 cgd if (flags & MSG_OOB) {
547 1.1 cgd m = m_get(M_WAIT, MT_DATA);
548 1.17 cgd error = (*pr->pr_usrreq)(so, PRU_RCVOOB, m,
549 1.22 mycroft (struct mbuf *)(long)(flags & MSG_PEEK), (struct mbuf *)0,
550 1.22 mycroft (struct proc *)0);
551 1.1 cgd if (error)
552 1.1 cgd goto bad;
553 1.1 cgd do {
554 1.1 cgd error = uiomove(mtod(m, caddr_t),
555 1.1 cgd (int) min(uio->uio_resid, m->m_len), uio);
556 1.1 cgd m = m_free(m);
557 1.1 cgd } while (uio->uio_resid && error == 0 && m);
558 1.1 cgd bad:
559 1.1 cgd if (m)
560 1.1 cgd m_freem(m);
561 1.1 cgd return (error);
562 1.1 cgd }
563 1.1 cgd if (mp)
564 1.1 cgd *mp = (struct mbuf *)0;
565 1.1 cgd if (so->so_state & SS_ISCONFIRMING && uio->uio_resid)
566 1.22 mycroft (*pr->pr_usrreq)(so, PRU_RCVD, (struct mbuf *)0,
567 1.22 mycroft (struct mbuf *)0, (struct mbuf *)0, (struct proc *)0);
568 1.1 cgd
569 1.1 cgd restart:
570 1.21 christos if ((error = sblock(&so->so_rcv, SBLOCKWAIT(flags))) != 0)
571 1.1 cgd return (error);
572 1.20 mycroft s = splsoftnet();
573 1.1 cgd
574 1.1 cgd m = so->so_rcv.sb_mb;
575 1.1 cgd /*
576 1.1 cgd * If we have less data than requested, block awaiting more
577 1.1 cgd * (subject to any timeout) if:
578 1.15 mycroft * 1. the current count is less than the low water mark,
579 1.1 cgd * 2. MSG_WAITALL is set, and it is possible to do the entire
580 1.15 mycroft * receive operation at once if we block (resid <= hiwat), or
581 1.15 mycroft * 3. MSG_DONTWAIT is not set.
582 1.1 cgd * If MSG_WAITALL is set but resid is larger than the receive buffer,
583 1.1 cgd * we have to do the receive in sections, and thus risk returning
584 1.1 cgd * a short count if a timeout or signal occurs after we start.
585 1.1 cgd */
586 1.21 christos if (m == 0 || (((flags & MSG_DONTWAIT) == 0 &&
587 1.15 mycroft so->so_rcv.sb_cc < uio->uio_resid) &&
588 1.1 cgd (so->so_rcv.sb_cc < so->so_rcv.sb_lowat ||
589 1.1 cgd ((flags & MSG_WAITALL) && uio->uio_resid <= so->so_rcv.sb_hiwat)) &&
590 1.21 christos m->m_nextpkt == 0 && (pr->pr_flags & PR_ATOMIC) == 0)) {
591 1.1 cgd #ifdef DIAGNOSTIC
592 1.1 cgd if (m == 0 && so->so_rcv.sb_cc)
593 1.1 cgd panic("receive 1");
594 1.1 cgd #endif
595 1.1 cgd if (so->so_error) {
596 1.1 cgd if (m)
597 1.15 mycroft goto dontblock;
598 1.1 cgd error = so->so_error;
599 1.1 cgd if ((flags & MSG_PEEK) == 0)
600 1.1 cgd so->so_error = 0;
601 1.1 cgd goto release;
602 1.1 cgd }
603 1.1 cgd if (so->so_state & SS_CANTRCVMORE) {
604 1.1 cgd if (m)
605 1.15 mycroft goto dontblock;
606 1.1 cgd else
607 1.1 cgd goto release;
608 1.1 cgd }
609 1.1 cgd for (; m; m = m->m_next)
610 1.1 cgd if (m->m_type == MT_OOBDATA || (m->m_flags & M_EOR)) {
611 1.1 cgd m = so->so_rcv.sb_mb;
612 1.1 cgd goto dontblock;
613 1.1 cgd }
614 1.1 cgd if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0 &&
615 1.1 cgd (so->so_proto->pr_flags & PR_CONNREQUIRED)) {
616 1.1 cgd error = ENOTCONN;
617 1.1 cgd goto release;
618 1.1 cgd }
619 1.1 cgd if (uio->uio_resid == 0)
620 1.1 cgd goto release;
621 1.15 mycroft if ((so->so_state & SS_NBIO) || (flags & MSG_DONTWAIT)) {
622 1.1 cgd error = EWOULDBLOCK;
623 1.1 cgd goto release;
624 1.1 cgd }
625 1.1 cgd sbunlock(&so->so_rcv);
626 1.1 cgd error = sbwait(&so->so_rcv);
627 1.1 cgd splx(s);
628 1.1 cgd if (error)
629 1.1 cgd return (error);
630 1.1 cgd goto restart;
631 1.1 cgd }
632 1.1 cgd dontblock:
633 1.15 mycroft #ifdef notyet /* XXXX */
634 1.15 mycroft if (uio->uio_procp)
635 1.15 mycroft uio->uio_procp->p_stats->p_ru.ru_msgrcv++;
636 1.15 mycroft #endif
637 1.1 cgd nextrecord = m->m_nextpkt;
638 1.1 cgd if (pr->pr_flags & PR_ADDR) {
639 1.1 cgd #ifdef DIAGNOSTIC
640 1.1 cgd if (m->m_type != MT_SONAME)
641 1.1 cgd panic("receive 1a");
642 1.1 cgd #endif
643 1.3 andrew orig_resid = 0;
644 1.1 cgd if (flags & MSG_PEEK) {
645 1.1 cgd if (paddr)
646 1.1 cgd *paddr = m_copy(m, 0, m->m_len);
647 1.1 cgd m = m->m_next;
648 1.1 cgd } else {
649 1.1 cgd sbfree(&so->so_rcv, m);
650 1.1 cgd if (paddr) {
651 1.1 cgd *paddr = m;
652 1.1 cgd so->so_rcv.sb_mb = m->m_next;
653 1.1 cgd m->m_next = 0;
654 1.1 cgd m = so->so_rcv.sb_mb;
655 1.1 cgd } else {
656 1.1 cgd MFREE(m, so->so_rcv.sb_mb);
657 1.1 cgd m = so->so_rcv.sb_mb;
658 1.1 cgd }
659 1.1 cgd }
660 1.1 cgd }
661 1.1 cgd while (m && m->m_type == MT_CONTROL && error == 0) {
662 1.1 cgd if (flags & MSG_PEEK) {
663 1.1 cgd if (controlp)
664 1.1 cgd *controlp = m_copy(m, 0, m->m_len);
665 1.1 cgd m = m->m_next;
666 1.1 cgd } else {
667 1.1 cgd sbfree(&so->so_rcv, m);
668 1.1 cgd if (controlp) {
669 1.1 cgd if (pr->pr_domain->dom_externalize &&
670 1.1 cgd mtod(m, struct cmsghdr *)->cmsg_type ==
671 1.1 cgd SCM_RIGHTS)
672 1.1 cgd error = (*pr->pr_domain->dom_externalize)(m);
673 1.1 cgd *controlp = m;
674 1.1 cgd so->so_rcv.sb_mb = m->m_next;
675 1.1 cgd m->m_next = 0;
676 1.1 cgd m = so->so_rcv.sb_mb;
677 1.1 cgd } else {
678 1.1 cgd MFREE(m, so->so_rcv.sb_mb);
679 1.1 cgd m = so->so_rcv.sb_mb;
680 1.1 cgd }
681 1.1 cgd }
682 1.3 andrew if (controlp) {
683 1.3 andrew orig_resid = 0;
684 1.1 cgd controlp = &(*controlp)->m_next;
685 1.3 andrew }
686 1.1 cgd }
687 1.1 cgd if (m) {
688 1.1 cgd if ((flags & MSG_PEEK) == 0)
689 1.1 cgd m->m_nextpkt = nextrecord;
690 1.1 cgd type = m->m_type;
691 1.1 cgd if (type == MT_OOBDATA)
692 1.1 cgd flags |= MSG_OOB;
693 1.1 cgd }
694 1.1 cgd moff = 0;
695 1.1 cgd offset = 0;
696 1.1 cgd while (m && uio->uio_resid > 0 && error == 0) {
697 1.1 cgd if (m->m_type == MT_OOBDATA) {
698 1.1 cgd if (type != MT_OOBDATA)
699 1.1 cgd break;
700 1.1 cgd } else if (type == MT_OOBDATA)
701 1.1 cgd break;
702 1.1 cgd #ifdef DIAGNOSTIC
703 1.1 cgd else if (m->m_type != MT_DATA && m->m_type != MT_HEADER)
704 1.1 cgd panic("receive 3");
705 1.1 cgd #endif
706 1.1 cgd so->so_state &= ~SS_RCVATMARK;
707 1.1 cgd len = uio->uio_resid;
708 1.1 cgd if (so->so_oobmark && len > so->so_oobmark - offset)
709 1.1 cgd len = so->so_oobmark - offset;
710 1.1 cgd if (len > m->m_len - moff)
711 1.1 cgd len = m->m_len - moff;
712 1.1 cgd /*
713 1.1 cgd * If mp is set, just pass back the mbufs.
714 1.1 cgd * Otherwise copy them out via the uio, then free.
715 1.1 cgd * Sockbuf must be consistent here (points to current mbuf,
716 1.1 cgd * it points to next record) when we drop priority;
717 1.1 cgd * we must note any additions to the sockbuf when we
718 1.1 cgd * block interrupts again.
719 1.1 cgd */
720 1.1 cgd if (mp == 0) {
721 1.1 cgd splx(s);
722 1.1 cgd error = uiomove(mtod(m, caddr_t) + moff, (int)len, uio);
723 1.20 mycroft s = splsoftnet();
724 1.1 cgd } else
725 1.1 cgd uio->uio_resid -= len;
726 1.1 cgd if (len == m->m_len - moff) {
727 1.1 cgd if (m->m_flags & M_EOR)
728 1.1 cgd flags |= MSG_EOR;
729 1.1 cgd if (flags & MSG_PEEK) {
730 1.1 cgd m = m->m_next;
731 1.1 cgd moff = 0;
732 1.1 cgd } else {
733 1.1 cgd nextrecord = m->m_nextpkt;
734 1.1 cgd sbfree(&so->so_rcv, m);
735 1.1 cgd if (mp) {
736 1.1 cgd *mp = m;
737 1.1 cgd mp = &m->m_next;
738 1.1 cgd so->so_rcv.sb_mb = m = m->m_next;
739 1.1 cgd *mp = (struct mbuf *)0;
740 1.1 cgd } else {
741 1.1 cgd MFREE(m, so->so_rcv.sb_mb);
742 1.1 cgd m = so->so_rcv.sb_mb;
743 1.1 cgd }
744 1.1 cgd if (m)
745 1.1 cgd m->m_nextpkt = nextrecord;
746 1.1 cgd }
747 1.1 cgd } else {
748 1.1 cgd if (flags & MSG_PEEK)
749 1.1 cgd moff += len;
750 1.1 cgd else {
751 1.1 cgd if (mp)
752 1.1 cgd *mp = m_copym(m, 0, len, M_WAIT);
753 1.1 cgd m->m_data += len;
754 1.1 cgd m->m_len -= len;
755 1.1 cgd so->so_rcv.sb_cc -= len;
756 1.1 cgd }
757 1.1 cgd }
758 1.1 cgd if (so->so_oobmark) {
759 1.1 cgd if ((flags & MSG_PEEK) == 0) {
760 1.1 cgd so->so_oobmark -= len;
761 1.1 cgd if (so->so_oobmark == 0) {
762 1.1 cgd so->so_state |= SS_RCVATMARK;
763 1.1 cgd break;
764 1.1 cgd }
765 1.7 cgd } else {
766 1.1 cgd offset += len;
767 1.7 cgd if (offset == so->so_oobmark)
768 1.7 cgd break;
769 1.7 cgd }
770 1.1 cgd }
771 1.1 cgd if (flags & MSG_EOR)
772 1.1 cgd break;
773 1.1 cgd /*
774 1.1 cgd * If the MSG_WAITALL flag is set (for non-atomic socket),
775 1.1 cgd * we must not quit until "uio->uio_resid == 0" or an error
776 1.1 cgd * termination. If a signal/timeout occurs, return
777 1.1 cgd * with a short count but without error.
778 1.1 cgd * Keep sockbuf locked against other readers.
779 1.1 cgd */
780 1.1 cgd while (flags & MSG_WAITALL && m == 0 && uio->uio_resid > 0 &&
781 1.3 andrew !sosendallatonce(so) && !nextrecord) {
782 1.1 cgd if (so->so_error || so->so_state & SS_CANTRCVMORE)
783 1.1 cgd break;
784 1.1 cgd error = sbwait(&so->so_rcv);
785 1.1 cgd if (error) {
786 1.1 cgd sbunlock(&so->so_rcv);
787 1.1 cgd splx(s);
788 1.1 cgd return (0);
789 1.1 cgd }
790 1.21 christos if ((m = so->so_rcv.sb_mb) != NULL)
791 1.1 cgd nextrecord = m->m_nextpkt;
792 1.1 cgd }
793 1.1 cgd }
794 1.3 andrew
795 1.3 andrew if (m && pr->pr_flags & PR_ATOMIC) {
796 1.3 andrew flags |= MSG_TRUNC;
797 1.3 andrew if ((flags & MSG_PEEK) == 0)
798 1.3 andrew (void) sbdroprecord(&so->so_rcv);
799 1.3 andrew }
800 1.1 cgd if ((flags & MSG_PEEK) == 0) {
801 1.1 cgd if (m == 0)
802 1.1 cgd so->so_rcv.sb_mb = nextrecord;
803 1.1 cgd if (pr->pr_flags & PR_WANTRCVD && so->so_pcb)
804 1.22 mycroft (*pr->pr_usrreq)(so, PRU_RCVD, (struct mbuf *)0,
805 1.22 mycroft (struct mbuf *)(long)flags, (struct mbuf *)0,
806 1.22 mycroft (struct proc *)0);
807 1.1 cgd }
808 1.3 andrew if (orig_resid == uio->uio_resid && orig_resid &&
809 1.3 andrew (flags & MSG_EOR) == 0 && (so->so_state & SS_CANTRCVMORE) == 0) {
810 1.3 andrew sbunlock(&so->so_rcv);
811 1.3 andrew splx(s);
812 1.3 andrew goto restart;
813 1.3 andrew }
814 1.3 andrew
815 1.1 cgd if (flagsp)
816 1.1 cgd *flagsp |= flags;
817 1.1 cgd release:
818 1.1 cgd sbunlock(&so->so_rcv);
819 1.1 cgd splx(s);
820 1.1 cgd return (error);
821 1.1 cgd }
822 1.1 cgd
823 1.14 mycroft int
824 1.1 cgd soshutdown(so, how)
825 1.34 kleink struct socket *so;
826 1.34 kleink int how;
827 1.1 cgd {
828 1.34 kleink struct protosw *pr = so->so_proto;
829 1.34 kleink
830 1.34 kleink if (!(how == SHUT_RD || how == SHUT_WR || how == SHUT_RDWR))
831 1.34 kleink return (EINVAL);
832 1.1 cgd
833 1.34 kleink if (how == SHUT_RD || how == SHUT_RDWR)
834 1.1 cgd sorflush(so);
835 1.34 kleink if (how == SHUT_WR || how == SHUT_RDWR)
836 1.22 mycroft return (*pr->pr_usrreq)(so, PRU_SHUTDOWN, (struct mbuf *)0,
837 1.22 mycroft (struct mbuf *)0, (struct mbuf *)0, (struct proc *)0);
838 1.1 cgd return (0);
839 1.1 cgd }
840 1.1 cgd
841 1.14 mycroft void
842 1.1 cgd sorflush(so)
843 1.1 cgd register struct socket *so;
844 1.1 cgd {
845 1.1 cgd register struct sockbuf *sb = &so->so_rcv;
846 1.1 cgd register struct protosw *pr = so->so_proto;
847 1.1 cgd register int s;
848 1.1 cgd struct sockbuf asb;
849 1.1 cgd
850 1.1 cgd sb->sb_flags |= SB_NOINTR;
851 1.15 mycroft (void) sblock(sb, M_WAITOK);
852 1.1 cgd s = splimp();
853 1.1 cgd socantrcvmore(so);
854 1.1 cgd sbunlock(sb);
855 1.1 cgd asb = *sb;
856 1.38 perry memset((caddr_t)sb, 0, sizeof(*sb));
857 1.1 cgd splx(s);
858 1.1 cgd if (pr->pr_flags & PR_RIGHTS && pr->pr_domain->dom_dispose)
859 1.1 cgd (*pr->pr_domain->dom_dispose)(asb.sb_mb);
860 1.1 cgd sbrelease(&asb);
861 1.1 cgd }
862 1.1 cgd
863 1.14 mycroft int
864 1.1 cgd sosetopt(so, level, optname, m0)
865 1.1 cgd register struct socket *so;
866 1.1 cgd int level, optname;
867 1.1 cgd struct mbuf *m0;
868 1.1 cgd {
869 1.1 cgd int error = 0;
870 1.1 cgd register struct mbuf *m = m0;
871 1.1 cgd
872 1.1 cgd if (level != SOL_SOCKET) {
873 1.1 cgd if (so->so_proto && so->so_proto->pr_ctloutput)
874 1.1 cgd return ((*so->so_proto->pr_ctloutput)
875 1.1 cgd (PRCO_SETOPT, so, level, optname, &m0));
876 1.1 cgd error = ENOPROTOOPT;
877 1.1 cgd } else {
878 1.1 cgd switch (optname) {
879 1.1 cgd
880 1.1 cgd case SO_LINGER:
881 1.36 perry if (m == NULL || m->m_len != sizeof(struct linger)) {
882 1.1 cgd error = EINVAL;
883 1.1 cgd goto bad;
884 1.1 cgd }
885 1.1 cgd so->so_linger = mtod(m, struct linger *)->l_linger;
886 1.1 cgd /* fall thru... */
887 1.1 cgd
888 1.1 cgd case SO_DEBUG:
889 1.1 cgd case SO_KEEPALIVE:
890 1.1 cgd case SO_DONTROUTE:
891 1.1 cgd case SO_USELOOPBACK:
892 1.1 cgd case SO_BROADCAST:
893 1.1 cgd case SO_REUSEADDR:
894 1.15 mycroft case SO_REUSEPORT:
895 1.1 cgd case SO_OOBINLINE:
896 1.26 thorpej case SO_TIMESTAMP:
897 1.36 perry if (m == NULL || m->m_len < sizeof(int)) {
898 1.1 cgd error = EINVAL;
899 1.1 cgd goto bad;
900 1.1 cgd }
901 1.1 cgd if (*mtod(m, int *))
902 1.1 cgd so->so_options |= optname;
903 1.1 cgd else
904 1.1 cgd so->so_options &= ~optname;
905 1.1 cgd break;
906 1.1 cgd
907 1.1 cgd case SO_SNDBUF:
908 1.1 cgd case SO_RCVBUF:
909 1.1 cgd case SO_SNDLOWAT:
910 1.1 cgd case SO_RCVLOWAT:
911 1.28 thorpej {
912 1.28 thorpej int optval;
913 1.28 thorpej
914 1.36 perry if (m == NULL || m->m_len < sizeof(int)) {
915 1.1 cgd error = EINVAL;
916 1.1 cgd goto bad;
917 1.1 cgd }
918 1.28 thorpej
919 1.28 thorpej /*
920 1.28 thorpej * Values < 1 make no sense for any of these
921 1.28 thorpej * options, so disallow them.
922 1.28 thorpej */
923 1.28 thorpej optval = *mtod(m, int *);
924 1.28 thorpej if (optval < 1) {
925 1.28 thorpej error = EINVAL;
926 1.28 thorpej goto bad;
927 1.28 thorpej }
928 1.28 thorpej
929 1.1 cgd switch (optname) {
930 1.1 cgd
931 1.1 cgd case SO_SNDBUF:
932 1.1 cgd case SO_RCVBUF:
933 1.1 cgd if (sbreserve(optname == SO_SNDBUF ?
934 1.1 cgd &so->so_snd : &so->so_rcv,
935 1.28 thorpej (u_long) optval) == 0) {
936 1.1 cgd error = ENOBUFS;
937 1.1 cgd goto bad;
938 1.1 cgd }
939 1.1 cgd break;
940 1.1 cgd
941 1.28 thorpej /*
942 1.28 thorpej * Make sure the low-water is never greater than
943 1.28 thorpej * the high-water.
944 1.28 thorpej */
945 1.1 cgd case SO_SNDLOWAT:
946 1.28 thorpej so->so_snd.sb_lowat =
947 1.28 thorpej (optval > so->so_snd.sb_hiwat) ?
948 1.28 thorpej so->so_snd.sb_hiwat : optval;
949 1.1 cgd break;
950 1.1 cgd case SO_RCVLOWAT:
951 1.28 thorpej so->so_rcv.sb_lowat =
952 1.28 thorpej (optval > so->so_rcv.sb_hiwat) ?
953 1.28 thorpej so->so_rcv.sb_hiwat : optval;
954 1.1 cgd break;
955 1.1 cgd }
956 1.1 cgd break;
957 1.28 thorpej }
958 1.1 cgd
959 1.1 cgd case SO_SNDTIMEO:
960 1.1 cgd case SO_RCVTIMEO:
961 1.1 cgd {
962 1.1 cgd struct timeval *tv;
963 1.1 cgd short val;
964 1.1 cgd
965 1.36 perry if (m == NULL || m->m_len < sizeof(*tv)) {
966 1.1 cgd error = EINVAL;
967 1.1 cgd goto bad;
968 1.1 cgd }
969 1.1 cgd tv = mtod(m, struct timeval *);
970 1.19 cgd if (tv->tv_sec * hz + tv->tv_usec / tick > SHRT_MAX) {
971 1.1 cgd error = EDOM;
972 1.1 cgd goto bad;
973 1.1 cgd }
974 1.1 cgd val = tv->tv_sec * hz + tv->tv_usec / tick;
975 1.1 cgd
976 1.1 cgd switch (optname) {
977 1.1 cgd
978 1.1 cgd case SO_SNDTIMEO:
979 1.1 cgd so->so_snd.sb_timeo = val;
980 1.1 cgd break;
981 1.1 cgd case SO_RCVTIMEO:
982 1.1 cgd so->so_rcv.sb_timeo = val;
983 1.1 cgd break;
984 1.1 cgd }
985 1.1 cgd break;
986 1.1 cgd }
987 1.1 cgd
988 1.1 cgd default:
989 1.1 cgd error = ENOPROTOOPT;
990 1.1 cgd break;
991 1.1 cgd }
992 1.15 mycroft if (error == 0 && so->so_proto && so->so_proto->pr_ctloutput) {
993 1.15 mycroft (void) ((*so->so_proto->pr_ctloutput)
994 1.15 mycroft (PRCO_SETOPT, so, level, optname, &m0));
995 1.15 mycroft m = NULL; /* freed by protocol */
996 1.15 mycroft }
997 1.1 cgd }
998 1.1 cgd bad:
999 1.1 cgd if (m)
1000 1.1 cgd (void) m_free(m);
1001 1.1 cgd return (error);
1002 1.1 cgd }
1003 1.1 cgd
1004 1.14 mycroft int
1005 1.1 cgd sogetopt(so, level, optname, mp)
1006 1.1 cgd register struct socket *so;
1007 1.1 cgd int level, optname;
1008 1.1 cgd struct mbuf **mp;
1009 1.1 cgd {
1010 1.1 cgd register struct mbuf *m;
1011 1.1 cgd
1012 1.1 cgd if (level != SOL_SOCKET) {
1013 1.1 cgd if (so->so_proto && so->so_proto->pr_ctloutput) {
1014 1.1 cgd return ((*so->so_proto->pr_ctloutput)
1015 1.1 cgd (PRCO_GETOPT, so, level, optname, mp));
1016 1.1 cgd } else
1017 1.1 cgd return (ENOPROTOOPT);
1018 1.1 cgd } else {
1019 1.1 cgd m = m_get(M_WAIT, MT_SOOPTS);
1020 1.36 perry m->m_len = sizeof(int);
1021 1.1 cgd
1022 1.1 cgd switch (optname) {
1023 1.1 cgd
1024 1.1 cgd case SO_LINGER:
1025 1.36 perry m->m_len = sizeof(struct linger);
1026 1.1 cgd mtod(m, struct linger *)->l_onoff =
1027 1.1 cgd so->so_options & SO_LINGER;
1028 1.1 cgd mtod(m, struct linger *)->l_linger = so->so_linger;
1029 1.1 cgd break;
1030 1.1 cgd
1031 1.1 cgd case SO_USELOOPBACK:
1032 1.1 cgd case SO_DONTROUTE:
1033 1.1 cgd case SO_DEBUG:
1034 1.1 cgd case SO_KEEPALIVE:
1035 1.1 cgd case SO_REUSEADDR:
1036 1.15 mycroft case SO_REUSEPORT:
1037 1.1 cgd case SO_BROADCAST:
1038 1.1 cgd case SO_OOBINLINE:
1039 1.26 thorpej case SO_TIMESTAMP:
1040 1.1 cgd *mtod(m, int *) = so->so_options & optname;
1041 1.1 cgd break;
1042 1.1 cgd
1043 1.1 cgd case SO_TYPE:
1044 1.1 cgd *mtod(m, int *) = so->so_type;
1045 1.1 cgd break;
1046 1.1 cgd
1047 1.1 cgd case SO_ERROR:
1048 1.1 cgd *mtod(m, int *) = so->so_error;
1049 1.1 cgd so->so_error = 0;
1050 1.1 cgd break;
1051 1.1 cgd
1052 1.1 cgd case SO_SNDBUF:
1053 1.1 cgd *mtod(m, int *) = so->so_snd.sb_hiwat;
1054 1.1 cgd break;
1055 1.1 cgd
1056 1.1 cgd case SO_RCVBUF:
1057 1.1 cgd *mtod(m, int *) = so->so_rcv.sb_hiwat;
1058 1.1 cgd break;
1059 1.1 cgd
1060 1.1 cgd case SO_SNDLOWAT:
1061 1.1 cgd *mtod(m, int *) = so->so_snd.sb_lowat;
1062 1.1 cgd break;
1063 1.1 cgd
1064 1.1 cgd case SO_RCVLOWAT:
1065 1.1 cgd *mtod(m, int *) = so->so_rcv.sb_lowat;
1066 1.1 cgd break;
1067 1.1 cgd
1068 1.1 cgd case SO_SNDTIMEO:
1069 1.1 cgd case SO_RCVTIMEO:
1070 1.1 cgd {
1071 1.1 cgd int val = (optname == SO_SNDTIMEO ?
1072 1.1 cgd so->so_snd.sb_timeo : so->so_rcv.sb_timeo);
1073 1.1 cgd
1074 1.1 cgd m->m_len = sizeof(struct timeval);
1075 1.1 cgd mtod(m, struct timeval *)->tv_sec = val / hz;
1076 1.1 cgd mtod(m, struct timeval *)->tv_usec =
1077 1.27 kleink (val % hz) * tick;
1078 1.1 cgd break;
1079 1.1 cgd }
1080 1.1 cgd
1081 1.1 cgd default:
1082 1.1 cgd (void)m_free(m);
1083 1.1 cgd return (ENOPROTOOPT);
1084 1.1 cgd }
1085 1.1 cgd *mp = m;
1086 1.1 cgd return (0);
1087 1.1 cgd }
1088 1.1 cgd }
1089 1.1 cgd
1090 1.14 mycroft void
1091 1.1 cgd sohasoutofband(so)
1092 1.1 cgd register struct socket *so;
1093 1.1 cgd {
1094 1.1 cgd struct proc *p;
1095 1.1 cgd
1096 1.1 cgd if (so->so_pgid < 0)
1097 1.1 cgd gsignal(-so->so_pgid, SIGURG);
1098 1.1 cgd else if (so->so_pgid > 0 && (p = pfind(so->so_pgid)) != 0)
1099 1.1 cgd psignal(p, SIGURG);
1100 1.2 cgd selwakeup(&so->so_rcv.sb_sel);
1101 1.1 cgd }
1102