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