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