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