]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - packages/net/bsd_tcpip/v2_0/src/sys/kern/uipc_socket.c
Merge branch 'master' of git+ssh://git.kernelconcepts.de/karo-tx-redboot
[karo-tx-redboot.git] / packages / net / bsd_tcpip / v2_0 / src / sys / kern / uipc_socket.c
1 //==========================================================================
2 //
3 //      src/sys/kern/uipc_socket.c
4 //
5 //==========================================================================
6 //####BSDCOPYRIGHTBEGIN####
7 //
8 // -------------------------------------------
9 //
10 // Portions of this software may have been derived from OpenBSD, 
11 // FreeBSD or other sources, and are covered by the appropriate
12 // copyright disclaimers included herein.
13 //
14 // Portions created by Red Hat are
15 // Copyright (C) 2002 Red Hat, Inc. All Rights Reserved.
16 //
17 // -------------------------------------------
18 //
19 //####BSDCOPYRIGHTEND####
20 //==========================================================================
21
22 /*
23  * Copyright (c) 1982, 1986, 1988, 1990, 1993
24  *      The Regents of the University of California.  All rights reserved.
25  *
26  * Redistribution and use in source and binary forms, with or without
27  * modification, are permitted provided that the following conditions
28  * are met:
29  * 1. Redistributions of source code must retain the above copyright
30  *    notice, this list of conditions and the following disclaimer.
31  * 2. Redistributions in binary form must reproduce the above copyright
32  *    notice, this list of conditions and the following disclaimer in the
33  *    documentation and/or other materials provided with the distribution.
34  * 3. All advertising materials mentioning features or use of this software
35  *    must display the following acknowledgement:
36  *      This product includes software developed by the University of
37  *      California, Berkeley and its contributors.
38  * 4. Neither the name of the University nor the names of its contributors
39  *    may be used to endorse or promote products derived from this software
40  *    without specific prior written permission.
41  *
42  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
43  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
44  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
45  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
46  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
47  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
48  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
50  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
51  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52  * SUCH DAMAGE.
53  *
54  *      @(#)uipc_socket.c       8.3 (Berkeley) 4/15/94
55  * $FreeBSD: src/sys/kern/uipc_socket.c,v 1.68.2.16 2001/06/14 20:46:06 ume Exp $
56  */
57
58 #include <sys/param.h>
59 #include <sys/malloc.h>
60 #include <sys/mbuf.h>
61 #include <sys/domain.h>
62 #include <sys/malloc.h>
63 #include <sys/protosw.h>
64 #include <sys/socket.h>
65 #include <sys/socketvar.h>
66
67 #include <cyg/io/file.h>
68 #include <sys/time.h>
69
70 #ifdef INET
71 static int       do_setopt_accept_filter(struct socket *so, struct sockopt *sopt);
72 #endif /* INET */
73
74 #if 0  // FILT
75 static void     filt_sordetach(struct knote *kn);
76 static int      filt_soread(struct knote *kn, long hint);
77 static void     filt_sowdetach(struct knote *kn);
78 static int      filt_sowrite(struct knote *kn, long hint);
79 static int      filt_solisten(struct knote *kn, long hint);
80
81 static struct filterops solisten_filtops = 
82         { 1, NULL, filt_sordetach, filt_solisten };
83 static struct filterops soread_filtops =
84         { 1, NULL, filt_sordetach, filt_soread };
85 static struct filterops sowrite_filtops = 
86         { 1, NULL, filt_sowdetach, filt_sowrite };
87 #endif
88
89 struct  vm_zone *socket_zone;
90 so_gen_t        so_gencnt;      /* generation count for sockets */
91
92 static int somaxconn = SOMAXCONN;
93
94 /*
95  * Socket operation routines.
96  * These routines are called by the routines in
97  * sys_socket.c or from a system process, and
98  * implement the semantics of socket operations by
99  * switching out to the protocol specific routines.
100  */
101
102 /*
103  * Get a socket structure from our zone, and initialize it.
104  * 'waitok' has been implemented for eCos, with [currently] some
105  * rather fixed strategy - it will retry some number of times (10)
106  * after at most 2 minutes.  This seems sufficient for sockets which
107  * are tied up in the TCP close process.
108  */
109 struct socket *
110 soalloc(int waitok)
111 {
112     struct socket *so = NULL;
113     int maxtries = waitok ? 10 : 1;
114
115     while (maxtries-- > 0) {
116         so = zalloci(socket_zone);
117         if (so) {
118             /* XXX race condition for reentrant kernel */
119             bzero(so, sizeof *so);
120             so->so_gencnt = ++so_gencnt;
121             so->so_zone = socket_zone;
122             TAILQ_INIT(&so->so_aiojobq);
123             return so;
124         }
125         if (waitok) {
126             diag_printf("DEBUG: Out of sockets - waiting\n");
127             tsleep(socket_zone, PVM|PCATCH, "soalloc", 120*100);
128             diag_printf("DEBUG: ... retry sockets\n");
129         }
130     }
131     return so;
132 }
133
134 int
135 socreate(dom, aso, type, proto, p)
136         int dom;
137         struct socket **aso;
138         register int type;
139         int proto;
140         struct proc *p;
141 {
142         register struct protosw *prp;
143         register struct socket *so;
144         register int error;
145
146         if (proto)
147                 prp = pffindproto(dom, proto, type);
148         else
149                 prp = pffindtype(dom, type);
150
151         if (prp == 0 || prp->pr_usrreqs->pru_attach == 0)
152                 return (EPROTONOSUPPORT);
153
154         if (prp->pr_type != type)
155                 return (EPROTOTYPE);
156         so = soalloc(p != 0);
157         if (so == 0) {
158                 return (ENOBUFS);
159         }
160
161         TAILQ_INIT(&so->so_incomp);
162         TAILQ_INIT(&so->so_comp);
163         so->so_type = type;
164         so->so_proto = prp;
165         error = (*prp->pr_usrreqs->pru_attach)(so, proto, p);
166         if (error) {
167                 so->so_state |= SS_NOFDREF;
168                 sofree(so);
169                 return (error);
170         }
171         *aso = so;
172         return (0);
173 }
174
175 int
176 sobind(so, nam, p)
177         struct socket *so;
178         struct sockaddr *nam;
179         struct proc *p;
180 {
181         int s = splnet();
182         int error;
183
184         error = (*so->so_proto->pr_usrreqs->pru_bind)(so, nam, p);
185         splx(s);
186         return (error);
187 }
188
189 void
190 sodealloc(so)
191         struct socket *so;
192         
193 {
194         vm_zone_t zone;
195   
196         so->so_gencnt = ++so_gencnt;
197 #ifdef INET
198         if (so->so_accf != NULL) {
199                 if (so->so_accf->so_accept_filter != NULL && 
200                         so->so_accf->so_accept_filter->accf_destroy != NULL) {
201                         so->so_accf->so_accept_filter->accf_destroy(so);
202                 }
203                 if (so->so_accf->so_accept_filter_str != NULL)
204                         FREE(so->so_accf->so_accept_filter_str, M_ACCF);
205                 FREE(so->so_accf, M_ACCF);
206         }
207 #endif /* INET */
208         zone = so->so_zone;
209         zfreei(zone, so);
210         wakeup(zone);
211 }
212
213 int
214 solisten(so, backlog, p)
215         register struct socket *so;
216         int backlog;
217         struct proc *p;
218 {
219         int s, error;
220
221         s = splnet();
222         error = (*so->so_proto->pr_usrreqs->pru_listen)(so, p);
223         if (error) {
224                 splx(s);
225                 return (error);
226         }
227         if (TAILQ_EMPTY(&so->so_comp))
228                 so->so_options |= SO_ACCEPTCONN;
229         if (backlog < 0 || backlog > somaxconn)
230                 backlog = somaxconn;
231         so->so_qlimit = backlog;
232         splx(s);
233         return (0);
234 }
235
236 void
237 sofree(so)
238         register struct socket *so;
239 {
240         struct socket *head = so->so_head;
241
242         if (so->so_pcb || (so->so_state & SS_NOFDREF) == 0)
243                 return;
244         if (head != NULL) {
245                 if (so->so_state & SS_INCOMP) {
246                         TAILQ_REMOVE(&head->so_incomp, so, so_list);
247                         head->so_incqlen--;
248                 } else if (so->so_state & SS_COMP) {
249                         /*
250                          * We must not decommission a socket that's
251                          * on the accept(2) queue.  If we do, then
252                          * accept(2) may hang after select(2) indicated
253                          * that the listening socket was ready.
254                          */
255                         return;
256                 } else {
257                         panic("sofree: not queued");
258                 }
259                 head->so_qlen--;
260                 so->so_state &= ~SS_INCOMP;
261                 so->so_head = NULL;
262         }
263         sbrelease(&so->so_snd, so);
264         sorflush(so);
265         sodealloc(so);
266 }
267
268 /*
269  * Close a socket on last file table reference removal.
270  * Initiate disconnect if connected.
271  * Free socket when disconnect complete.
272  */
273 int
274 soclose(so)
275         register struct socket *so;
276 {
277         int s = splnet();               /* conservative */
278         int error = 0;
279
280         if (so->so_options & SO_ACCEPTCONN) {
281                 struct socket *sp, *sonext;
282
283                 sp = TAILQ_FIRST(&so->so_incomp);
284                 for (; sp != NULL; sp = sonext) {
285                         sonext = TAILQ_NEXT(sp, so_list);
286                         (void) soabort(sp);
287                 }
288                 for (sp = TAILQ_FIRST(&so->so_comp); sp != NULL; sp = sonext) {
289                         sonext = TAILQ_NEXT(sp, so_list);
290                         /* Dequeue from so_comp since sofree() won't do it */
291                         TAILQ_REMOVE(&so->so_comp, sp, so_list);
292                         so->so_qlen--;
293                         sp->so_state &= ~SS_COMP;
294                         sp->so_head = NULL;
295                         (void) soabort(sp);
296                 }
297         }
298         if (so->so_pcb == 0)
299                 goto discard;
300         if (so->so_state & SS_ISCONNECTED) {
301                 if ((so->so_state & SS_ISDISCONNECTING) == 0) {
302                         error = sodisconnect(so);
303                         if (error)
304                                 goto drop;
305                 }
306                 if (so->so_options & SO_LINGER) {
307                         if ((so->so_state & SS_ISDISCONNECTING) &&
308                             (so->so_state & SS_NBIO))
309                                 goto drop;
310                         while (so->so_state & SS_ISCONNECTED) {
311                                 error = tsleep((caddr_t)&so->so_timeo,
312                                     PSOCK | PCATCH, "soclos", so->so_linger * hz);
313                                 if (error)
314                                         break;
315                         }
316                 }
317         }
318 drop:
319         if (so->so_pcb) {
320                 int error2 = (*so->so_proto->pr_usrreqs->pru_detach)(so);
321                 if (error == 0)
322                         error = error2;
323         }
324 discard:
325         if (so->so_state & SS_NOFDREF)
326                 panic("soclose: NOFDREF");
327         so->so_state |= SS_NOFDREF;
328         sofree(so);
329         splx(s);
330         return (error);
331 }
332
333 /*
334  * Must be called at splnet...
335  */
336 int
337 soabort(so)
338         struct socket *so;
339 {
340         int error;
341
342         error = (*so->so_proto->pr_usrreqs->pru_abort)(so);
343         if (error) {
344                 sofree(so);
345                 return error;
346         }
347         return (0);
348 }
349
350 int
351 soaccept(so, nam)
352         register struct socket *so;
353         struct sockaddr **nam;
354 {
355         int s = splnet();
356         int error;
357
358         if ((so->so_state & SS_NOFDREF) == 0)
359                 panic("soaccept: !NOFDREF");
360         so->so_state &= ~SS_NOFDREF;
361         error = (*so->so_proto->pr_usrreqs->pru_accept)(so, nam);
362         splx(s);
363         return (error);
364 }
365
366 int
367 soconnect(so, nam, p)
368         register struct socket *so;
369         struct sockaddr *nam;
370         struct proc *p;
371 {
372         int s;
373         int error;
374
375         if (so->so_options & SO_ACCEPTCONN)
376                 return (EOPNOTSUPP);
377         s = splnet();
378         /*
379          * If protocol is connection-based, can only connect once.
380          * Otherwise, if connected, try to disconnect first.
381          * This allows user to disconnect by connecting to, e.g.,
382          * a null address.
383          */
384         if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING) &&
385             ((so->so_proto->pr_flags & PR_CONNREQUIRED) ||
386             (error = sodisconnect(so))))
387                 error = EISCONN;
388         else
389                 error = (*so->so_proto->pr_usrreqs->pru_connect)(so, nam, p);
390         splx(s);
391         return (error);
392 }
393
394 int
395 soconnect2(so1, so2)
396         register struct socket *so1;
397         struct socket *so2;
398 {
399         int s = splnet();
400         int error;
401
402         error = (*so1->so_proto->pr_usrreqs->pru_connect2)(so1, so2);
403         splx(s);
404         return (error);
405 }
406
407 int
408 sodisconnect(so)
409         register struct socket *so;
410 {
411         int s = splnet();
412         int error;
413
414         if ((so->so_state & SS_ISCONNECTED) == 0) {
415                 error = ENOTCONN;
416                 goto bad;
417         }
418         if (so->so_state & SS_ISDISCONNECTING) {
419                 error = EALREADY;
420                 goto bad;
421         }
422         error = (*so->so_proto->pr_usrreqs->pru_disconnect)(so);
423 bad:
424         splx(s);
425         return (error);
426 }
427
428 #define SBLOCKWAIT(f)   (((f) & MSG_DONTWAIT) ? M_NOWAIT : M_WAITOK)
429 /*
430  * Send on a socket.
431  * If send must go all at once and message is larger than
432  * send buffering, then hard error.
433  * Lock against other senders.
434  * If must go all at once and not enough room now, then
435  * inform user that this would block and do nothing.
436  * Otherwise, if nonblocking, send as much as possible.
437  * The data to be sent is described by "uio" if nonzero,
438  * otherwise by the mbuf chain "top" (which must be null
439  * if uio is not).  Data provided in mbuf chain must be small
440  * enough to send all at once.
441  *
442  * Returns nonzero on error, timeout or signal; callers
443  * must check for short counts if EINTR/ERESTART are returned.
444  * Data and control buffers are freed on return.
445  */
446 int
447 sosend(so, addr, uio, top, control, flags, p)
448         register struct socket *so;
449         struct sockaddr *addr;
450         struct uio *uio;
451         struct mbuf *top;
452         struct mbuf *control;
453         int flags;
454         struct proc *p;
455 {
456         struct mbuf **mp;
457         register struct mbuf *m;
458         register long space, len, resid;
459         int clen = 0, error, s, dontroute, mlen;
460         int atomic = sosendallatonce(so) || top;
461
462         if (uio)
463                 resid = uio->uio_resid;
464         else
465                 resid = top->m_pkthdr.len;
466         /*
467          * In theory resid should be unsigned.
468          * However, space must be signed, as it might be less than 0
469          * if we over-committed, and we must use a signed comparison
470          * of space and resid.  On the other hand, a negative resid
471          * causes us to loop sending 0-length segments to the protocol.
472          *
473          * Also check to make sure that MSG_EOR isn't used on SOCK_STREAM
474          * type sockets since that's an error.
475          */
476         if (resid < 0 || (so->so_type == SOCK_STREAM && (flags & MSG_EOR))) {
477                 error = EINVAL;
478                 goto out;
479         }
480
481         dontroute =
482             (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0 &&
483             (so->so_proto->pr_flags & PR_ATOMIC);
484         if (control)
485                 clen = control->m_len;
486 #define snderr(errno)   { error = errno; splx(s); goto release; }
487
488 restart:
489         error = sblock(&so->so_snd, SBLOCKWAIT(flags));
490         if (error)
491                 goto out;
492         do {
493                 s = splnet();
494                 if (so->so_state & SS_CANTSENDMORE)
495                         snderr(EPIPE);
496                 if (so->so_error) {
497                         error = so->so_error;
498                         so->so_error = 0;
499                         splx(s);
500                         goto release;
501                 }
502                 if ((so->so_state & SS_ISCONNECTED) == 0) {
503                         /*
504                          * `sendto' and `sendmsg' is allowed on a connection-
505                          * based socket if it supports implied connect.
506                          * Return ENOTCONN if not connected and no address is
507                          * supplied.
508                          */
509                         if ((so->so_proto->pr_flags & PR_CONNREQUIRED) &&
510                             (so->so_proto->pr_flags & PR_IMPLOPCL) == 0) {
511                                 if ((so->so_state & SS_ISCONFIRMING) == 0 &&
512                                     !(resid == 0 && clen != 0))
513                                         snderr(ENOTCONN);
514                         } else if (addr == 0)
515                             snderr(so->so_proto->pr_flags & PR_CONNREQUIRED ?
516                                    ENOTCONN : EDESTADDRREQ);
517                 }
518                 space = sbspace(&so->so_snd);
519                 if (flags & MSG_OOB)
520                         space += 1024;
521                 if ((atomic && resid > so->so_snd.sb_hiwat) ||
522                     clen > so->so_snd.sb_hiwat)
523                         snderr(EMSGSIZE);
524                 if (space < resid + clen && uio &&
525                     (atomic || space < so->so_snd.sb_lowat || space < clen)) {
526                         if (so->so_state & SS_NBIO)
527                                 snderr(EWOULDBLOCK);
528                         sbunlock(&so->so_snd);
529                         error = sbwait(&so->so_snd);
530                         splx(s);
531                         if (error)
532                                 goto out;
533                         goto restart;
534                 }
535                 splx(s);
536                 mp = &top;
537                 space -= clen;
538                 do {
539                     if (uio == NULL) {
540                         /*
541                          * Data is prepackaged in "top".
542                          */
543                         resid = 0;
544                         if (flags & MSG_EOR)
545                                 top->m_flags |= M_EOR;
546                     } else do {
547                         if (top == 0) {
548                                 MGETHDR(m, M_WAIT, MT_DATA);
549                                 if (m == NULL) {
550                                         error = ENOBUFS;
551                                         goto release;
552                                 }
553                                 mlen = MHLEN;
554                                 m->m_pkthdr.len = 0;
555                                 m->m_pkthdr.rcvif = (struct ifnet *)0;
556                         } else {
557                                 MGET(m, M_WAIT, MT_DATA);
558                                 if (m == NULL) {
559                                         error = ENOBUFS;
560                                         goto release;
561                                 }
562                                 mlen = MLEN;
563                         }
564                         if (resid >= MINCLSIZE) {
565                                 MCLGET(m, M_WAIT);
566                                 if ((m->m_flags & M_EXT) == 0)
567                                         goto nopages;
568                                 mlen = MCLBYTES;
569                                 len = min(min(mlen, resid), space);
570                         } else {
571 nopages:
572                                 len = min(min(mlen, resid), space);
573                                 /*
574                                  * For datagram protocols, leave room
575                                  * for protocol headers in first mbuf.
576                                  */
577                                 if (atomic && top == 0 && len < mlen)
578                                         MH_ALIGN(m, len);
579                         }
580                         space -= len;
581                         error = uiomove(mtod(m, caddr_t), (int)len, uio);
582                         resid = uio->uio_resid;
583                         m->m_len = len;
584                         *mp = m;
585                         top->m_pkthdr.len += len;
586                         if (error)
587                                 goto release;
588                         mp = &m->m_next;
589                         if (resid <= 0) {
590                                 if (flags & MSG_EOR)
591                                         top->m_flags |= M_EOR;
592                                 break;
593                         }
594                     } while (space > 0 && atomic);
595                     if (dontroute)
596                             so->so_options |= SO_DONTROUTE;
597                     s = splnet();                               /* XXX */
598                     /*
599                      * XXX all the SS_CANTSENDMORE checks previously
600                      * done could be out of date.  We could have recieved
601                      * a reset packet in an interrupt or maybe we slept
602                      * while doing page faults in uiomove() etc. We could
603                      * probably recheck again inside the splnet() protection
604                      * here, but there are probably other places that this
605                      * also happens.  We must rethink this.
606                      */
607                     error = (*so->so_proto->pr_usrreqs->pru_send)(so,
608                         (flags & MSG_OOB) ? PRUS_OOB :
609                         /*
610                          * If the user set MSG_EOF, the protocol
611                          * understands this flag and nothing left to
612                          * send then use PRU_SEND_EOF instead of PRU_SEND.
613                          */
614                         ((flags & MSG_EOF) &&
615                          (so->so_proto->pr_flags & PR_IMPLOPCL) &&
616                          (resid <= 0)) ?
617                                 PRUS_EOF :
618                         /* If there is more to send set PRUS_MORETOCOME */
619                         (resid > 0 && space > 0) ? PRUS_MORETOCOME : 0,
620                         top, addr, control, p);
621                     splx(s);
622                     if (dontroute)
623                             so->so_options &= ~SO_DONTROUTE;
624                     clen = 0;
625                     control = 0;
626                     top = 0;
627                     mp = &top;
628                     if (error)
629                         goto release;
630                 } while (resid && space > 0);
631         } while (resid);
632
633 release:
634         sbunlock(&so->so_snd);
635 out:
636         if (top)
637                 m_freem(top);
638         if (control)
639                 m_freem(control);
640         return (error);
641 }
642
643 /*
644  * Implement receive operations on a socket.
645  * We depend on the way that records are added to the sockbuf
646  * by sbappend*.  In particular, each record (mbufs linked through m_next)
647  * must begin with an address if the protocol so specifies,
648  * followed by an optional mbuf or mbufs containing ancillary data,
649  * and then zero or more mbufs of data.
650  * In order to avoid blocking network interrupts for the entire time here,
651  * we splx() while doing the actual copy to user space.
652  * Although the sockbuf is locked, new data may still be appended,
653  * and thus we must maintain consistency of the sockbuf during that time.
654  *
655  * The caller may receive the data as a single mbuf chain by supplying
656  * an mbuf **mp0 for use in returning the chain.  The uio is then used
657  * only for the count in uio_resid.
658  */
659 int
660 soreceive(so, psa, uio, mp0, controlp, flagsp)
661         register struct socket *so;
662         struct sockaddr **psa;
663         struct uio *uio;
664         struct mbuf **mp0;
665         struct mbuf **controlp;
666         int *flagsp;
667 {
668         register struct mbuf *m, **mp;
669         register int flags, len, error, s, offset;
670         struct protosw *pr = so->so_proto;
671         struct mbuf *nextrecord;
672         int moff, type = 0;
673         int orig_resid = uio->uio_resid;
674
675         mp = mp0;
676         if (psa)
677                 *psa = 0;
678         if (controlp)
679                 *controlp = 0;
680         if (flagsp)
681                 flags = *flagsp &~ MSG_EOR;
682         else
683                 flags = 0;
684         if (flags & MSG_OOB) {
685                 m = m_get(M_WAIT, MT_DATA);
686                 if (m == NULL) {
687                         return (ENOBUFS);
688                 }
689                 error = (*pr->pr_usrreqs->pru_rcvoob)(so, m, flags & MSG_PEEK);
690                 if (error)
691                         goto bad;
692                 do {
693                         error = uiomove(mtod(m, caddr_t),
694                             (int) min(uio->uio_resid, m->m_len), uio);
695                         m = m_free(m);
696                 } while (uio->uio_resid && error == 0 && m);
697 bad:
698                 if (m)
699                         m_freem(m);
700                 return (error);
701         }
702         if (mp)
703                 *mp = (struct mbuf *)0;
704         if (so->so_state & SS_ISCONFIRMING && uio->uio_resid)
705                 (*pr->pr_usrreqs->pru_rcvd)(so, 0);
706
707 restart:
708         error = sblock(&so->so_rcv, SBLOCKWAIT(flags));
709         if (error)
710                 return (error);
711         s = splnet();
712
713         m = so->so_rcv.sb_mb;
714         /*
715          * If we have less data than requested, block awaiting more
716          * (subject to any timeout) if:
717          *   1. the current count is less than the low water mark, or
718          *   2. MSG_WAITALL is set, and it is possible to do the entire
719          *      receive operation at once if we block (resid <= hiwat).
720          *   3. MSG_DONTWAIT is not set
721          * If MSG_WAITALL is set but resid is larger than the receive buffer,
722          * we have to do the receive in sections, and thus risk returning
723          * a short count if a timeout or signal occurs after we start.
724          */
725         if (m == 0 || (((flags & MSG_DONTWAIT) == 0 &&
726             so->so_rcv.sb_cc < uio->uio_resid) &&
727             (so->so_rcv.sb_cc < so->so_rcv.sb_lowat ||
728             ((flags & MSG_WAITALL) && uio->uio_resid <= so->so_rcv.sb_hiwat)) &&
729             m->m_nextpkt == 0 && (pr->pr_flags & PR_ATOMIC) == 0)) {
730                 if (so->so_error) {
731                         if (m)
732                                 goto dontblock;
733                         error = so->so_error;
734                         if ((flags & MSG_PEEK) == 0)
735                                 so->so_error = 0;
736                         goto release;
737                 }
738                 if (so->so_state & SS_CANTRCVMORE) {
739                         if (m)
740                                 goto dontblock;
741                         else
742                                 goto release;
743                 }
744                 for (; m; m = m->m_next)
745                         if (m->m_type == MT_OOBDATA  || (m->m_flags & M_EOR)) {
746                                 m = so->so_rcv.sb_mb;
747                                 goto dontblock;
748                         }
749                 if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0 &&
750                     (so->so_proto->pr_flags & PR_CONNREQUIRED)) {
751                         error = ENOTCONN;
752                         goto release;
753                 }
754                 if (uio->uio_resid == 0)
755                         goto release;
756                 if ((so->so_state & SS_NBIO) || (flags & MSG_DONTWAIT)) {
757                         error = EWOULDBLOCK;
758                         goto release;
759                 }
760                 sbunlock(&so->so_rcv);
761                 error = sbwait(&so->so_rcv);
762                 splx(s);
763                 if (error)
764                         return (error);
765                 goto restart;
766         }
767 dontblock:
768         nextrecord = m->m_nextpkt;
769         if (pr->pr_flags & PR_ADDR) {
770                 orig_resid = 0;
771                 if (psa)
772                         *psa = dup_sockaddr(mtod(m, struct sockaddr *),
773                                             mp0 == 0);
774                 if (flags & MSG_PEEK) {
775                         m = m->m_next;
776                 } else {
777                         sbfree(&so->so_rcv, m);
778                         MFREE(m, so->so_rcv.sb_mb);
779                         m = so->so_rcv.sb_mb;
780                 }
781         }
782         while (m && m->m_type == MT_CONTROL && error == 0) {
783                 if (flags & MSG_PEEK) {
784                         if (controlp)
785                                 *controlp = m_copy(m, 0, m->m_len);
786                         m = m->m_next;
787                 } else {
788                         sbfree(&so->so_rcv, m);
789                         if (controlp) {
790                                 if (pr->pr_domain->dom_externalize &&
791                                     mtod(m, struct cmsghdr *)->cmsg_type ==
792                                     SCM_RIGHTS)
793                                    error = (*pr->pr_domain->dom_externalize)(m);
794                                 *controlp = m;
795                                 so->so_rcv.sb_mb = m->m_next;
796                                 m->m_next = 0;
797                                 m = so->so_rcv.sb_mb;
798                         } else {
799                                 MFREE(m, so->so_rcv.sb_mb);
800                                 m = so->so_rcv.sb_mb;
801                         }
802                 }
803                 if (controlp) {
804                         orig_resid = 0;
805                         controlp = &(*controlp)->m_next;
806                 }
807         }
808         if (m) {
809                 if ((flags & MSG_PEEK) == 0)
810                         m->m_nextpkt = nextrecord;
811                 type = m->m_type;
812                 if (type == MT_OOBDATA)
813                         flags |= MSG_OOB;
814         }
815         moff = 0;
816         offset = 0;
817         while (m && uio->uio_resid > 0 && error == 0) {
818                 if (m->m_type == MT_OOBDATA) {
819                         if (type != MT_OOBDATA)
820                                 break;
821                 } else if (type == MT_OOBDATA)
822                         break;
823                 so->so_state &= ~SS_RCVATMARK;
824                 len = uio->uio_resid;
825                 if (so->so_oobmark && len > so->so_oobmark - offset)
826                         len = so->so_oobmark - offset;
827                 if (len > m->m_len - moff)
828                         len = m->m_len - moff;
829                 /*
830                  * If mp is set, just pass back the mbufs.
831                  * Otherwise copy them out via the uio, then free.
832                  * Sockbuf must be consistent here (points to current mbuf,
833                  * it points to next record) when we drop priority;
834                  * we must note any additions to the sockbuf when we
835                  * block interrupts again.
836                  */
837                 if (mp == 0) {
838                         splx(s);
839                         error = uiomove(mtod(m, caddr_t) + moff, (int)len, uio);
840                         s = splnet();
841                         if (error)
842                                 goto release;
843                 } else
844                         uio->uio_resid -= len;
845                 if (len == m->m_len - moff) {
846                         if (m->m_flags & M_EOR)
847                                 flags |= MSG_EOR;
848                         if (flags & MSG_PEEK) {
849                                 m = m->m_next;
850                                 moff = 0;
851                         } else {
852                                 nextrecord = m->m_nextpkt;
853                                 sbfree(&so->so_rcv, m);
854                                 if (mp) {
855                                         *mp = m;
856                                         mp = &m->m_next;
857                                         so->so_rcv.sb_mb = m = m->m_next;
858                                         *mp = (struct mbuf *)0;
859                                 } else {
860                                         MFREE(m, so->so_rcv.sb_mb);
861                                         m = so->so_rcv.sb_mb;
862                                 }
863                                 if (m)
864                                         m->m_nextpkt = nextrecord;
865                         }
866                 } else {
867                         if (flags & MSG_PEEK)
868                                 moff += len;
869                         else {
870                                 if (mp)
871                                         *mp = m_copym(m, 0, len, M_WAIT);
872                                 m->m_data += len;
873                                 m->m_len -= len;
874                                 so->so_rcv.sb_cc -= len;
875                         }
876                 }
877                 if (so->so_oobmark) {
878                         if ((flags & MSG_PEEK) == 0) {
879                                 so->so_oobmark -= len;
880                                 if (so->so_oobmark == 0) {
881                                         so->so_state |= SS_RCVATMARK;
882                                         break;
883                                 }
884                         } else {
885                                 offset += len;
886                                 if (offset == so->so_oobmark)
887                                         break;
888                         }
889                 }
890                 if (flags & MSG_EOR)
891                         break;
892                 /*
893                  * If the MSG_WAITALL flag is set (for non-atomic socket),
894                  * we must not quit until "uio->uio_resid == 0" or an error
895                  * termination.  If a signal/timeout occurs, return
896                  * with a short count but without error.
897                  * Keep sockbuf locked against other readers.
898                  */
899                 while (flags & MSG_WAITALL && m == 0 && uio->uio_resid > 0 &&
900                     !sosendallatonce(so) && !nextrecord) {
901                         if (so->so_error || so->so_state & SS_CANTRCVMORE)
902                                 break;
903                         error = sbwait(&so->so_rcv);
904                         if (error) {
905                                 sbunlock(&so->so_rcv);
906                                 splx(s);
907                                 return (0);
908                         }
909                         m = so->so_rcv.sb_mb;
910                         if (m)
911                                 nextrecord = m->m_nextpkt;
912                 }
913         }
914
915         if (m && pr->pr_flags & PR_ATOMIC) {
916                 flags |= MSG_TRUNC;
917                 if ((flags & MSG_PEEK) == 0)
918                         (void) sbdroprecord(&so->so_rcv);
919         }
920         if ((flags & MSG_PEEK) == 0) {
921                 if (m == 0)
922                         so->so_rcv.sb_mb = nextrecord;
923                 if (pr->pr_flags & PR_WANTRCVD && so->so_pcb)
924                         (*pr->pr_usrreqs->pru_rcvd)(so, flags);
925         }
926         if (orig_resid == uio->uio_resid && orig_resid &&
927             (flags & MSG_EOR) == 0 && (so->so_state & SS_CANTRCVMORE) == 0) {
928                 sbunlock(&so->so_rcv);
929                 splx(s);
930                 goto restart;
931         }
932
933         if (flagsp)
934                 *flagsp |= flags;
935 release:
936         sbunlock(&so->so_rcv);
937         splx(s);
938         return (error);
939 }
940
941 int
942 soshutdown(so, how)
943         register struct socket *so;
944         register int how;
945 {
946         register struct protosw *pr = so->so_proto;
947
948         if (!(how == SHUT_RD || how == SHUT_WR || how == SHUT_RDWR))
949                 return (EINVAL);
950
951         if (how != SHUT_WR)
952                 sorflush(so);
953         if (how != SHUT_RD)
954                 return ((*pr->pr_usrreqs->pru_shutdown)(so));
955         return (0);
956 }
957
958 void
959 sorflush(so)
960         register struct socket *so;
961 {
962         register struct sockbuf *sb = &so->so_rcv;
963         register struct protosw *pr = so->so_proto;
964         register int s;
965         struct sockbuf asb;
966
967         sb->sb_flags |= SB_NOINTR;
968         (void) sblock(sb, M_WAITOK);
969         s = splimp();
970         socantrcvmore(so);
971         sbunlock(sb);
972         asb = *sb;
973         bzero((caddr_t)sb, sizeof (*sb));
974         splx(s);
975         if (pr->pr_flags & PR_RIGHTS && pr->pr_domain->dom_dispose)
976                 (*pr->pr_domain->dom_dispose)(asb.sb_mb);
977         sbrelease(&asb, so);
978 }
979
980 #ifdef INET
981 static int
982 do_setopt_accept_filter(so, sopt)
983         struct  socket *so;
984         struct  sockopt *sopt;
985 {
986         struct accept_filter_arg        *afap = NULL;
987         struct accept_filter    *afp;
988         struct so_accf  *af = so->so_accf;
989         int     error = 0;
990
991         /* do not set/remove accept filters on non listen sockets */
992         if ((so->so_options & SO_ACCEPTCONN) == 0) {
993                 error = EINVAL;
994                 goto out;
995         }
996
997         /* removing the filter */
998         if (sopt == NULL) {
999                 if (af != NULL) {
1000                         if (af->so_accept_filter != NULL && 
1001                                 af->so_accept_filter->accf_destroy != NULL) {
1002                                 af->so_accept_filter->accf_destroy(so);
1003                         }
1004                         if (af->so_accept_filter_str != NULL) {
1005                                 FREE(af->so_accept_filter_str, M_ACCF);
1006                         }
1007                         FREE(af, M_ACCF);
1008                         so->so_accf = NULL;
1009                 }
1010                 so->so_options &= ~SO_ACCEPTFILTER;
1011                 return (0);
1012         }
1013         /* adding a filter */
1014         /* must remove previous filter first */
1015         if (af != NULL) {
1016                 error = EINVAL;
1017                 goto out;
1018         }
1019         /* don't put large objects on the kernel stack */
1020         MALLOC(afap, struct accept_filter_arg *, sizeof(*afap), M_TEMP, M_WAITOK);
1021         error = sooptcopyin(sopt, afap, sizeof *afap, sizeof *afap);
1022         afap->af_name[sizeof(afap->af_name)-1] = '\0';
1023         afap->af_arg[sizeof(afap->af_arg)-1] = '\0';
1024         if (error)
1025                 goto out;
1026         afp = accept_filt_get(afap->af_name);
1027         if (afp == NULL) {
1028                 error = ENOENT;
1029                 goto out;
1030         }
1031         MALLOC(af, struct so_accf *, sizeof(*af), M_ACCF, M_WAITOK);
1032         bzero(af, sizeof(*af));
1033         if (afp->accf_create != NULL) {
1034                 if (afap->af_name[0] != '\0') {
1035                         int len = strlen(afap->af_name) + 1;
1036
1037                         MALLOC(af->so_accept_filter_str, char *, len, M_ACCF, M_WAITOK);
1038                         strcpy(af->so_accept_filter_str, afap->af_name);
1039                 }
1040                 af->so_accept_filter_arg = afp->accf_create(so, afap->af_arg);
1041                 if (af->so_accept_filter_arg == NULL) {
1042                         FREE(af->so_accept_filter_str, M_ACCF);
1043                         FREE(af, M_ACCF);
1044                         so->so_accf = NULL;
1045                         error = EINVAL;
1046                         goto out;
1047                 }
1048         }
1049         af->so_accept_filter = afp;
1050         so->so_accf = af;
1051         so->so_options |= SO_ACCEPTFILTER;
1052 out:
1053         if (afap != NULL)
1054                 FREE(afap, M_TEMP);
1055         return (error);
1056 }
1057 #endif /* INET */
1058
1059 /*
1060  * Perhaps this routine, and sooptcopyout(), below, ought to come in
1061  * an additional variant to handle the case where the option value needs
1062  * to be some kind of integer, but not a specific size.
1063  * In addition to their use here, these functions are also called by the
1064  * protocol-level pr_ctloutput() routines.
1065  */
1066 int
1067 sooptcopyin(sopt, buf, len, minlen)
1068         struct  sockopt *sopt;
1069         void    *buf;
1070         size_t  len;
1071         size_t  minlen;
1072 {
1073         size_t  valsize;
1074
1075         /*
1076          * If the user gives us more than we wanted, we ignore it,
1077          * but if we don't get the minimum length the caller
1078          * wants, we return EINVAL.  On success, sopt->sopt_valsize
1079          * is set to however much we actually retrieved.
1080          */
1081         if ((valsize = sopt->sopt_valsize) < minlen)
1082                 return EINVAL;
1083         if (valsize > len)
1084                 sopt->sopt_valsize = valsize = len;
1085
1086         if (sopt->sopt_p != 0)
1087                 return (copyin(sopt->sopt_val, buf, valsize));
1088
1089         bcopy(sopt->sopt_val, buf, valsize);
1090         return 0;
1091 }
1092
1093 int
1094 sosetopt(so, sopt)
1095         struct socket *so;
1096         struct sockopt *sopt;
1097 {
1098         int     error, optval;
1099         struct  linger l;
1100         struct  timeval tv;
1101         u_long  val;
1102
1103         error = 0;
1104         if (sopt->sopt_level != SOL_SOCKET) {
1105                 if (so->so_proto && so->so_proto->pr_ctloutput)
1106                         return ((*so->so_proto->pr_ctloutput)
1107                                   (so, sopt));
1108                 error = ENOPROTOOPT;
1109         } else {
1110                 switch (sopt->sopt_name) {
1111 #ifdef INET
1112                 case SO_ACCEPTFILTER:
1113                         error = do_setopt_accept_filter(so, sopt);
1114                         if (error)
1115                                 goto bad;
1116                         break;
1117 #endif /* INET */
1118                 case SO_LINGER:
1119                         error = sooptcopyin(sopt, &l, sizeof l, sizeof l);
1120                         if (error)
1121                                 goto bad;
1122
1123                         so->so_linger = l.l_linger;
1124                         if (l.l_onoff)
1125                                 so->so_options |= SO_LINGER;
1126                         else
1127                                 so->so_options &= ~SO_LINGER;
1128                         break;
1129
1130                 case SO_DEBUG:
1131                 case SO_KEEPALIVE:
1132                 case SO_DONTROUTE:
1133                 case SO_USELOOPBACK:
1134                 case SO_BROADCAST:
1135                 case SO_REUSEADDR:
1136                 case SO_REUSEPORT:
1137                 case SO_OOBINLINE:
1138                 case SO_TIMESTAMP:
1139                         error = sooptcopyin(sopt, &optval, sizeof optval,
1140                                             sizeof optval);
1141                         if (error)
1142                                 goto bad;
1143                         if (optval)
1144                                 so->so_options |= sopt->sopt_name;
1145                         else
1146                                 so->so_options &= ~sopt->sopt_name;
1147                         break;
1148
1149                 case SO_SNDBUF:
1150                 case SO_RCVBUF:
1151                 case SO_SNDLOWAT:
1152                 case SO_RCVLOWAT:
1153                         error = sooptcopyin(sopt, &optval, sizeof optval,
1154                                             sizeof optval);
1155                         if (error)
1156                                 goto bad;
1157
1158                         /*
1159                          * Values < 1 make no sense for any of these
1160                          * options, so disallow them.
1161                          */
1162                         if (optval < 1) {
1163                                 error = EINVAL;
1164                                 goto bad;
1165                         }
1166
1167                         switch (sopt->sopt_name) {
1168                         case SO_SNDBUF:
1169                         case SO_RCVBUF:
1170                                 if (sbreserve(sopt->sopt_name == SO_SNDBUF ?
1171                                     &so->so_snd : &so->so_rcv, (u_long)optval,
1172                                     so, curproc) == 0) {
1173                                         error = ENOBUFS;
1174                                         goto bad;
1175                                 }
1176                                 break;
1177
1178                         /*
1179                          * Make sure the low-water is never greater than
1180                          * the high-water.
1181                          */
1182                         case SO_SNDLOWAT:
1183                                 so->so_snd.sb_lowat =
1184                                     (optval > so->so_snd.sb_hiwat) ?
1185                                     so->so_snd.sb_hiwat : optval;
1186                                 break;
1187                         case SO_RCVLOWAT:
1188                                 so->so_rcv.sb_lowat =
1189                                     (optval > so->so_rcv.sb_hiwat) ?
1190                                     so->so_rcv.sb_hiwat : optval;
1191                                 break;
1192                         }
1193                         break;
1194
1195                 case SO_SNDTIMEO:
1196                 case SO_RCVTIMEO:
1197                         error = sooptcopyin(sopt, &tv, sizeof tv,
1198                                             sizeof tv);
1199                         if (error)
1200                                 goto bad;
1201
1202                         /* assert(hz > 0); */
1203                         if (tv.tv_sec < 0 || tv.tv_sec > SHRT_MAX / hz ||
1204                             tv.tv_usec < 0 || tv.tv_usec >= 1000000) {
1205                                 error = EDOM;
1206                                 goto bad;
1207                         }
1208                         /* assert(tick > 0); */
1209                         /* assert(ULONG_MAX - SHRT_MAX >= 1000000); */
1210                         val = (u_long)(tv.tv_sec * hz) + tv.tv_usec / tick;
1211                         if (val > SHRT_MAX) {
1212                                 error = EDOM;
1213                                 goto bad;
1214                         }
1215
1216                         switch (sopt->sopt_name) {
1217                         case SO_SNDTIMEO:
1218                                 so->so_snd.sb_timeo = val;
1219                                 break;
1220                         case SO_RCVTIMEO:
1221                                 so->so_rcv.sb_timeo = val;
1222                                 break;
1223                         }
1224                         break;
1225                 default:
1226                         error = ENOPROTOOPT;
1227                         break;
1228                 }
1229                 if (error == 0 && so->so_proto && so->so_proto->pr_ctloutput) {
1230                         (void) ((*so->so_proto->pr_ctloutput)
1231                                   (so, sopt));
1232                 }
1233         }
1234 bad:
1235         return (error);
1236 }
1237
1238 /* Helper routine for getsockopt */
1239 int
1240 sooptcopyout(sopt, buf, len)
1241         struct  sockopt *sopt;
1242         void    *buf;
1243         size_t  len;
1244 {
1245         int     error;
1246         size_t  valsize;
1247
1248         error = 0;
1249
1250         /*
1251          * Documented get behavior is that we always return a value,
1252          * possibly truncated to fit in the user's buffer.
1253          * Traditional behavior is that we always tell the user
1254          * precisely how much we copied, rather than something useful
1255          * like the total amount we had available for her.
1256          * Note that this interface is not idempotent; the entire answer must
1257          * generated ahead of time.
1258          */
1259         valsize = min(len, sopt->sopt_valsize);
1260         sopt->sopt_valsize = valsize;
1261         if (sopt->sopt_val != 0) {
1262                 if (sopt->sopt_p != 0)
1263                         error = copyout(buf, sopt->sopt_val, valsize);
1264                 else
1265                         bcopy(buf, sopt->sopt_val, valsize);
1266         }
1267         return error;
1268 }
1269
1270 int
1271 sogetopt(so, sopt)
1272         struct socket *so;
1273         struct sockopt *sopt;
1274 {
1275         int     error, optval;
1276         struct  linger l;
1277         struct  timeval tv;
1278         struct accept_filter_arg *afap;
1279
1280         error = 0;
1281         if (sopt->sopt_level != SOL_SOCKET) {
1282                 if (so->so_proto && so->so_proto->pr_ctloutput) {
1283                         return ((*so->so_proto->pr_ctloutput)
1284                                   (so, sopt));
1285                 } else
1286                         return (ENOPROTOOPT);
1287         } else {
1288                 switch (sopt->sopt_name) {
1289 #ifdef INET
1290                 case SO_ACCEPTFILTER:
1291                         if ((so->so_options & SO_ACCEPTCONN) == 0)
1292                                 return (EINVAL);
1293                         MALLOC(afap, struct accept_filter_arg *, sizeof(*afap),
1294                                 M_TEMP, M_WAITOK);
1295                         bzero(afap, sizeof(*afap));
1296                         if ((so->so_options & SO_ACCEPTFILTER) != 0) {
1297                                 strcpy(afap->af_name, so->so_accf->so_accept_filter->accf_name);
1298                                 if (so->so_accf->so_accept_filter_str != NULL)
1299                                         strcpy(afap->af_arg, so->so_accf->so_accept_filter_str);
1300                         }
1301                         error = sooptcopyout(sopt, afap, sizeof(*afap));
1302                         FREE(afap, M_TEMP);
1303                         break;
1304 #endif /* INET */
1305                         
1306                 case SO_LINGER:
1307                         l.l_onoff = so->so_options & SO_LINGER;
1308                         l.l_linger = so->so_linger;
1309                         error = sooptcopyout(sopt, &l, sizeof l);
1310                         break;
1311
1312                 case SO_USELOOPBACK:
1313                 case SO_DONTROUTE:
1314                 case SO_DEBUG:
1315                 case SO_KEEPALIVE:
1316                 case SO_REUSEADDR:
1317                 case SO_REUSEPORT:
1318                 case SO_BROADCAST:
1319                 case SO_OOBINLINE:
1320                 case SO_TIMESTAMP:
1321                         optval = so->so_options & sopt->sopt_name;
1322 integer:
1323                         error = sooptcopyout(sopt, &optval, sizeof optval);
1324                         break;
1325
1326                 case SO_TYPE:
1327                         optval = so->so_type;
1328                         goto integer;
1329
1330                 case SO_ERROR:
1331                         optval = so->so_error;
1332                         so->so_error = 0;
1333                         goto integer;
1334
1335                 case SO_SNDBUF:
1336                         optval = so->so_snd.sb_hiwat;
1337                         goto integer;
1338
1339                 case SO_RCVBUF:
1340                         optval = so->so_rcv.sb_hiwat;
1341                         goto integer;
1342
1343                 case SO_SNDLOWAT:
1344                         optval = so->so_snd.sb_lowat;
1345                         goto integer;
1346
1347                 case SO_RCVLOWAT:
1348                         optval = so->so_rcv.sb_lowat;
1349                         goto integer;
1350
1351                 case SO_SNDTIMEO:
1352                 case SO_RCVTIMEO:
1353                         optval = (sopt->sopt_name == SO_SNDTIMEO ?
1354                                   so->so_snd.sb_timeo : so->so_rcv.sb_timeo);
1355
1356                         tv.tv_sec = optval / hz;
1357                         tv.tv_usec = (optval % hz) * tick;
1358                         error = sooptcopyout(sopt, &tv, sizeof tv);
1359                         break;                  
1360
1361                 default:
1362                         error = ENOPROTOOPT;
1363                         break;
1364                 }
1365                 return (error);
1366         }
1367 }
1368
1369 /* XXX; prepare mbuf for (__FreeBSD__ < 3) routines. */
1370 int
1371 soopt_getm(struct sockopt *sopt, struct mbuf **mp)
1372 {
1373         struct mbuf *m, *m_prev;
1374         int sopt_size = sopt->sopt_valsize;
1375
1376         MGET(m, sopt->sopt_p ? M_WAIT : M_DONTWAIT, MT_DATA);
1377         if (m == 0) {
1378                 return ENOBUFS;
1379         }
1380         if (sopt_size > MLEN) {
1381                 MCLGET(m, sopt->sopt_p ? M_WAIT : M_DONTWAIT);
1382                 if ((m->m_flags & M_EXT) == 0) {
1383                         m_free(m);
1384                         return ENOBUFS;
1385                 }
1386                 m->m_len = min(MCLBYTES, sopt_size);
1387         } else {
1388                 m->m_len = min(MLEN, sopt_size);
1389         }
1390         sopt_size -= m->m_len;
1391         *mp = m;
1392         m_prev = m;
1393
1394         while (sopt_size) {
1395                 MGET(m, sopt->sopt_p ? M_WAIT : M_DONTWAIT, MT_DATA);
1396                 if (m == 0) {
1397                         m_freem(*mp);
1398                         return ENOBUFS;
1399                 }
1400                 if (sopt_size > MLEN) {
1401                         MCLGET(m, sopt->sopt_p ? M_WAIT : M_DONTWAIT);
1402                         if ((m->m_flags & M_EXT) == 0) {
1403                                 m_freem(*mp);
1404                                 return ENOBUFS;
1405                         }
1406                         m->m_len = min(MCLBYTES, sopt_size);
1407                 } else {
1408                         m->m_len = min(MLEN, sopt_size);
1409                 }
1410                 sopt_size -= m->m_len;
1411                 m_prev->m_next = m;
1412                 m_prev = m;
1413         }
1414         return 0;
1415 }
1416
1417 /* XXX; copyin sopt data into mbuf chain for (__FreeBSD__ < 3) routines. */
1418 int
1419 soopt_mcopyin(struct sockopt *sopt, struct mbuf *m)
1420 {
1421         struct mbuf *m0 = m;
1422
1423         if (sopt->sopt_val == NULL)
1424                 return 0;
1425         while (m != NULL && sopt->sopt_valsize >= m->m_len) {
1426                 if (sopt->sopt_p != NULL) {
1427                         int error;
1428
1429                         error = copyin(sopt->sopt_val, mtod(m, char *),
1430                                        m->m_len);
1431                         if (error != 0) {
1432                                 m_freem(m0);
1433                                 return(error);
1434                         }
1435                 } else
1436                         bcopy(sopt->sopt_val, mtod(m, char *), m->m_len);
1437                 sopt->sopt_valsize -= m->m_len;
1438                 sopt->sopt_val = (char *)sopt->sopt_val + m->m_len;
1439                 m = m->m_next;
1440         }
1441         if (m != NULL) /* should be allocated enoughly at ip6_sooptmcopyin() */
1442                 panic("ip6_sooptmcopyin");
1443         return 0;
1444 }
1445
1446 /* XXX; copyout mbuf chain data into soopt for (__FreeBSD__ < 3) routines. */
1447 int
1448 soopt_mcopyout(struct sockopt *sopt, struct mbuf *m)
1449 {
1450         struct mbuf *m0 = m;
1451         size_t valsize = 0;
1452
1453         if (sopt->sopt_val == NULL)
1454                 return 0;
1455         while (m != NULL && sopt->sopt_valsize >= m->m_len) {
1456                 if (sopt->sopt_p != NULL) {
1457                         int error;
1458
1459                         error = copyout(mtod(m, char *), sopt->sopt_val,
1460                                        m->m_len);
1461                         if (error != 0) {
1462                                 m_freem(m0);
1463                                 return(error);
1464                         }
1465                 } else
1466                         bcopy(mtod(m, char *), sopt->sopt_val, m->m_len);
1467                sopt->sopt_valsize -= m->m_len;
1468                sopt->sopt_val = (char *)sopt->sopt_val + m->m_len;
1469                valsize += m->m_len;
1470                m = m->m_next;
1471         }
1472         if (m != NULL) {
1473                 /* enough soopt buffer should be given from user-land */
1474                 m_freem(m0);
1475                 return(EINVAL);
1476         }
1477         sopt->sopt_valsize = valsize;
1478         return 0;
1479 }
1480
1481 void
1482 sohasoutofband(so)
1483         register struct socket *so;
1484 {
1485         selwakeup(&so->so_rcv.sb_sel);
1486 }
1487
1488 int
1489 sopoll(struct socket *so, int events, struct ucred *cred, struct proc *p)
1490 {
1491     panic("%s\n", __FUNCTION__);
1492     return 0;
1493 #if 0 // POLL
1494         int revents = 0;
1495         int s = splnet();
1496
1497         if (events & (POLLIN | POLLRDNORM))
1498                 if (soreadable(so))
1499                         revents |= events & (POLLIN | POLLRDNORM);
1500
1501         if (events & (POLLOUT | POLLWRNORM))
1502                 if (sowriteable(so))
1503                         revents |= events & (POLLOUT | POLLWRNORM);
1504
1505         if (events & (POLLPRI | POLLRDBAND))
1506                 if (so->so_oobmark || (so->so_state & SS_RCVATMARK))
1507                         revents |= events & (POLLPRI | POLLRDBAND);
1508
1509         if (revents == 0) {
1510                 if (events & (POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND)) {
1511                         selrecord(p, &so->so_rcv.sb_sel);
1512                         so->so_rcv.sb_flags |= SB_SEL;
1513                 }
1514
1515                 if (events & (POLLOUT | POLLWRNORM)) {
1516                         selrecord(p, &so->so_snd.sb_sel);
1517                         so->so_snd.sb_flags |= SB_SEL;
1518                 }
1519         }
1520
1521         splx(s);
1522         return (revents);
1523 #endif // POLL
1524 }
1525