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