The usbcom's device was being released too early in the close sequence.
[dragonfly.git] / sys / kern / uipc_socket2.c
CommitLineData
984263bc
MD
1/*
2 * Copyright (c) 1982, 1986, 1988, 1990, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * @(#)uipc_socket2.c 8.1 (Berkeley) 6/10/93
34 * $FreeBSD: src/sys/kern/uipc_socket2.c,v 1.55.2.17 2002/08/31 19:04:55 dwmalone Exp $
74f1caca 35 * $DragonFly: src/sys/kern/uipc_socket2.c,v 1.12 2004/06/02 14:42:57 eirikn Exp $
984263bc
MD
36 */
37
38#include "opt_param.h"
39#include <sys/param.h>
40#include <sys/systm.h>
41#include <sys/domain.h>
42#include <sys/file.h> /* for maxfiles */
43#include <sys/kernel.h>
44#include <sys/proc.h>
45#include <sys/malloc.h>
46#include <sys/mbuf.h>
47#include <sys/protosw.h>
48#include <sys/resourcevar.h>
49#include <sys/stat.h>
50#include <sys/socket.h>
51#include <sys/socketvar.h>
52#include <sys/signalvar.h>
53#include <sys/sysctl.h>
54#include <sys/aio.h> /* for aio_swake proto */
55#include <sys/event.h>
56
c1d0003c
JH
57#include <sys/thread2.h>
58#include <sys/msgport2.h>
59
984263bc
MD
60int maxsockets;
61
62/*
63 * Primitive routines for operating on sockets and socket buffers
64 */
65
66u_long sb_max = SB_MAX;
67u_long sb_max_adj =
68 SB_MAX * MCLBYTES / (MSIZE + MCLBYTES); /* adjusted sb_max */
69
70static u_long sb_efficiency = 8; /* parameter for sbreserve() */
71
72/*
73 * Procedures to manipulate state flags of socket
74 * and do appropriate wakeups. Normal sequence from the
75 * active (originating) side is that soisconnecting() is
76 * called during processing of connect() call,
77 * resulting in an eventual call to soisconnected() if/when the
78 * connection is established. When the connection is torn down
79 * soisdisconnecting() is called during processing of disconnect() call,
80 * and soisdisconnected() is called when the connection to the peer
81 * is totally severed. The semantics of these routines are such that
82 * connectionless protocols can call soisconnected() and soisdisconnected()
83 * only, bypassing the in-progress calls when setting up a ``connection''
84 * takes no time.
85 *
86 * From the passive side, a socket is created with
87 * two queues of sockets: so_incomp for connections in progress
88 * and so_comp for connections already made and awaiting user acceptance.
89 * As a protocol is preparing incoming connections, it creates a socket
90 * structure queued on so_incomp by calling sonewconn(). When the connection
91 * is established, soisconnected() is called, and transfers the
92 * socket structure to so_comp, making it available to accept().
93 *
94 * If a socket is closed with sockets on either
95 * so_incomp or so_comp, these sockets are dropped.
96 *
97 * If higher level protocols are implemented in
98 * the kernel, the wakeups done here will sometimes
99 * cause software-interrupt process scheduling.
100 */
101
102void
103soisconnecting(so)
1fd87d54 104 struct socket *so;
984263bc
MD
105{
106
107 so->so_state &= ~(SS_ISCONNECTED|SS_ISDISCONNECTING);
108 so->so_state |= SS_ISCONNECTING;
109}
110
111void
112soisconnected(so)
113 struct socket *so;
114{
115 struct socket *head = so->so_head;
116
117 so->so_state &= ~(SS_ISCONNECTING|SS_ISDISCONNECTING|SS_ISCONFIRMING);
118 so->so_state |= SS_ISCONNECTED;
119 if (head && (so->so_state & SS_INCOMP)) {
120 if ((so->so_options & SO_ACCEPTFILTER) != 0) {
121 so->so_upcall = head->so_accf->so_accept_filter->accf_callback;
122 so->so_upcallarg = head->so_accf->so_accept_filter_arg;
123 so->so_rcv.sb_flags |= SB_UPCALL;
124 so->so_options &= ~SO_ACCEPTFILTER;
125 so->so_upcall(so, so->so_upcallarg, 0);
126 return;
127 }
128 TAILQ_REMOVE(&head->so_incomp, so, so_list);
129 head->so_incqlen--;
130 so->so_state &= ~SS_INCOMP;
131 TAILQ_INSERT_TAIL(&head->so_comp, so, so_list);
132 head->so_qlen++;
133 so->so_state |= SS_COMP;
134 sorwakeup(head);
135 wakeup_one(&head->so_timeo);
136 } else {
137 wakeup(&so->so_timeo);
138 sorwakeup(so);
139 sowwakeup(so);
140 }
141}
142
143void
144soisdisconnecting(so)
1fd87d54 145 struct socket *so;
984263bc
MD
146{
147
148 so->so_state &= ~SS_ISCONNECTING;
149 so->so_state |= (SS_ISDISCONNECTING|SS_CANTRCVMORE|SS_CANTSENDMORE);
150 wakeup((caddr_t)&so->so_timeo);
151 sowwakeup(so);
152 sorwakeup(so);
153}
154
155void
156soisdisconnected(so)
1fd87d54 157 struct socket *so;
984263bc
MD
158{
159
160 so->so_state &= ~(SS_ISCONNECTING|SS_ISCONNECTED|SS_ISDISCONNECTING);
161 so->so_state |= (SS_CANTRCVMORE|SS_CANTSENDMORE|SS_ISDISCONNECTED);
162 wakeup((caddr_t)&so->so_timeo);
163 sbdrop(&so->so_snd, so->so_snd.sb_cc);
164 sowwakeup(so);
165 sorwakeup(so);
166}
167
168/*
169 * When an attempt at a new connection is noted on a socket
170 * which accepts connections, sonewconn is called. If the
171 * connection is possible (subject to space constraints, etc.)
172 * then we allocate a new structure, propoerly linked into the
173 * data structure of the original socket, and return this.
174 * Connstatus may be 0, or SO_ISCONFIRMING, or SO_ISCONNECTED.
175 */
176struct socket *
dadab5e9 177sonewconn(struct socket *head, int connstatus)
984263bc 178{
1fd87d54 179 struct socket *so;
e4700d00 180 struct pru_attach_info ai;
984263bc
MD
181
182 if (head->so_qlen > 3 * head->so_qlimit / 2)
183 return ((struct socket *)0);
184 so = soalloc(0);
185 if (so == NULL)
186 return ((struct socket *)0);
187 if ((head->so_options & SO_ACCEPTFILTER) != 0)
188 connstatus = 0;
189 so->so_head = head;
190 so->so_type = head->so_type;
191 so->so_options = head->so_options &~ SO_ACCEPTCONN;
192 so->so_linger = head->so_linger;
193 so->so_state = head->so_state | SS_NOFDREF;
194 so->so_proto = head->so_proto;
195 so->so_timeo = head->so_timeo;
dadab5e9 196 so->so_cred = crhold(head->so_cred);
e4700d00
JH
197 ai.sb_rlimit = NULL;
198 ai.p_ucred = NULL;
199 ai.fd_rdir = NULL; /* jail code cruft XXX JH */
200 if (soreserve(so, head->so_snd.sb_hiwat, head->so_rcv.sb_hiwat, NULL) ||
9eeaa8a9 201 /* Directly call function since we're already at protocol level. */
e4700d00 202 (*so->so_proto->pr_usrreqs->pru_attach)(so, 0, &ai)) {
984263bc
MD
203 sodealloc(so);
204 return ((struct socket *)0);
205 }
206
207 if (connstatus) {
208 TAILQ_INSERT_TAIL(&head->so_comp, so, so_list);
209 so->so_state |= SS_COMP;
210 head->so_qlen++;
211 } else {
212 if (head->so_incqlen > head->so_qlimit) {
213 struct socket *sp;
214 sp = TAILQ_FIRST(&head->so_incomp);
215 (void) soabort(sp);
216 }
217 TAILQ_INSERT_TAIL(&head->so_incomp, so, so_list);
218 so->so_state |= SS_INCOMP;
219 head->so_incqlen++;
220 }
221 if (connstatus) {
222 sorwakeup(head);
223 wakeup((caddr_t)&head->so_timeo);
224 so->so_state |= connstatus;
225 }
226 return (so);
227}
228
229/*
230 * Socantsendmore indicates that no more data will be sent on the
231 * socket; it would normally be applied to a socket when the user
232 * informs the system that no more data is to be sent, by the protocol
233 * code (in case PRU_SHUTDOWN). Socantrcvmore indicates that no more data
234 * will be received, and will normally be applied to the socket by a
235 * protocol when it detects that the peer will send no more data.
236 * Data queued for reading in the socket may yet be read.
237 */
238
239void
240socantsendmore(so)
241 struct socket *so;
242{
243
244 so->so_state |= SS_CANTSENDMORE;
245 sowwakeup(so);
246}
247
248void
249socantrcvmore(so)
250 struct socket *so;
251{
252
253 so->so_state |= SS_CANTRCVMORE;
254 sorwakeup(so);
255}
256
257/*
258 * Wait for data to arrive at/drain from a socket buffer.
259 */
260int
261sbwait(sb)
262 struct sockbuf *sb;
263{
264
265 sb->sb_flags |= SB_WAIT;
266 return (tsleep((caddr_t)&sb->sb_cc,
377d4740
MD
267 ((sb->sb_flags & SB_NOINTR) ? 0 : PCATCH),
268 "sbwait",
269 sb->sb_timeo));
984263bc
MD
270}
271
272/*
273 * Lock a sockbuf already known to be locked;
274 * return any error returned from sleep (EINTR).
275 */
276int
277sb_lock(sb)
1fd87d54 278 struct sockbuf *sb;
984263bc
MD
279{
280 int error;
281
282 while (sb->sb_flags & SB_LOCK) {
283 sb->sb_flags |= SB_WANT;
284 error = tsleep((caddr_t)&sb->sb_flags,
377d4740
MD
285 ((sb->sb_flags & SB_NOINTR) ? 0 : PCATCH),
286 "sblock", 0);
984263bc
MD
287 if (error)
288 return (error);
289 }
290 sb->sb_flags |= SB_LOCK;
291 return (0);
292}
293
294/*
b44419cb
MD
295 * Wakeup processes waiting on a socket buffer. Do asynchronous notification
296 * via SIGIO if the socket has the SS_ASYNC flag set.
984263bc
MD
297 */
298void
299sowakeup(so, sb)
1fd87d54
RG
300 struct socket *so;
301 struct sockbuf *sb;
984263bc 302{
c1d0003c
JH
303 struct selinfo *selinfo = &sb->sb_sel;
304
305 selwakeup(selinfo);
984263bc
MD
306 sb->sb_flags &= ~SB_SEL;
307 if (sb->sb_flags & SB_WAIT) {
308 sb->sb_flags &= ~SB_WAIT;
309 wakeup((caddr_t)&sb->sb_cc);
310 }
311 if ((so->so_state & SS_ASYNC) && so->so_sigio != NULL)
312 pgsigio(so->so_sigio, SIGIO, 0);
313 if (sb->sb_flags & SB_UPCALL)
74f1caca 314 (*so->so_upcall)(so, so->so_upcallarg, MB_DONTWAIT);
984263bc
MD
315 if (sb->sb_flags & SB_AIO)
316 aio_swake(so, sb);
c1d0003c
JH
317 KNOTE(&selinfo->si_note, 0);
318 if (sb->sb_flags & SB_MEVENT) {
319 struct netmsg_so_notify *msg, *nmsg;
320
321 TAILQ_FOREACH_MUTABLE(msg, &selinfo->si_mlist, nm_list, nmsg) {
322 if (msg->nm_predicate((struct netmsg *)msg)) {
c1d0003c 323 TAILQ_REMOVE(&selinfo->si_mlist, msg, nm_list);
b44419cb
MD
324 lwkt_replymsg(&msg->nm_lmsg,
325 msg->nm_lmsg.ms_error);
c1d0003c
JH
326 }
327 }
c1d0003c
JH
328 if (TAILQ_EMPTY(&sb->sb_sel.si_mlist))
329 sb->sb_flags &= ~SB_MEVENT;
330 }
984263bc
MD
331}
332
333/*
334 * Socket buffer (struct sockbuf) utility routines.
335 *
336 * Each socket contains two socket buffers: one for sending data and
337 * one for receiving data. Each buffer contains a queue of mbufs,
338 * information about the number of mbufs and amount of data in the
339 * queue, and other fields allowing select() statements and notification
340 * on data availability to be implemented.
341 *
342 * Data stored in a socket buffer is maintained as a list of records.
343 * Each record is a list of mbufs chained together with the m_next
344 * field. Records are chained together with the m_nextpkt field. The upper
345 * level routine soreceive() expects the following conventions to be
346 * observed when placing information in the receive buffer:
347 *
348 * 1. If the protocol requires each message be preceded by the sender's
349 * name, then a record containing that name must be present before
350 * any associated data (mbuf's must be of type MT_SONAME).
351 * 2. If the protocol supports the exchange of ``access rights'' (really
352 * just additional data associated with the message), and there are
353 * ``rights'' to be received, then a record containing this data
354 * should be present (mbuf's must be of type MT_RIGHTS).
355 * 3. If a name or rights record exists, then it must be followed by
356 * a data record, perhaps of zero length.
357 *
358 * Before using a new socket structure it is first necessary to reserve
359 * buffer space to the socket, by calling sbreserve(). This should commit
360 * some of the available buffer space in the system buffer pool for the
361 * socket (currently, it does nothing but enforce limits). The space
362 * should be released by calling sbrelease() when the socket is destroyed.
363 */
364
365int
e4700d00 366soreserve(struct socket *so, u_long sndcc, u_long rcvcc, struct rlimit *rl)
984263bc 367{
e4700d00 368 if (sbreserve(&so->so_snd, sndcc, so, rl) == 0)
984263bc 369 goto bad;
e4700d00 370 if (sbreserve(&so->so_rcv, rcvcc, so, rl) == 0)
984263bc
MD
371 goto bad2;
372 if (so->so_rcv.sb_lowat == 0)
373 so->so_rcv.sb_lowat = 1;
374 if (so->so_snd.sb_lowat == 0)
375 so->so_snd.sb_lowat = MCLBYTES;
376 if (so->so_snd.sb_lowat > so->so_snd.sb_hiwat)
377 so->so_snd.sb_lowat = so->so_snd.sb_hiwat;
378 return (0);
379bad2:
380 sbrelease(&so->so_snd, so);
381bad:
382 return (ENOBUFS);
383}
384
385static int
386sysctl_handle_sb_max(SYSCTL_HANDLER_ARGS)
387{
388 int error = 0;
389 u_long old_sb_max = sb_max;
390
391 error = SYSCTL_OUT(req, arg1, sizeof(int));
392 if (error || !req->newptr)
393 return (error);
394 error = SYSCTL_IN(req, arg1, sizeof(int));
395 if (error)
396 return (error);
397 if (sb_max < MSIZE + MCLBYTES) {
398 sb_max = old_sb_max;
399 return (EINVAL);
400 }
401 sb_max_adj = (u_quad_t)sb_max * MCLBYTES / (MSIZE + MCLBYTES);
402 return (0);
403}
404
405/*
406 * Allot mbufs to a sockbuf.
407 * Attempt to scale mbmax so that mbcnt doesn't become limiting
408 * if buffering efficiency is near the normal case.
409 */
410int
e4700d00 411sbreserve(struct sockbuf *sb, u_long cc, struct socket *so, struct rlimit *rl)
984263bc
MD
412{
413
414 /*
e4700d00
JH
415 * rl will only be NULL when we're in an interrupt (eg, in tcp_input)
416 * or when called from netgraph (ie, ngd_attach)
984263bc
MD
417 */
418 if (cc > sb_max_adj)
419 return (0);
420 if (!chgsbsize(so->so_cred->cr_uidinfo, &sb->sb_hiwat, cc,
e4700d00 421 rl ? rl->rlim_cur : RLIM_INFINITY)) {
984263bc
MD
422 return (0);
423 }
424 sb->sb_mbmax = min(cc * sb_efficiency, sb_max);
425 if (sb->sb_lowat > sb->sb_hiwat)
426 sb->sb_lowat = sb->sb_hiwat;
427 return (1);
428}
429
430/*
431 * Free mbufs held by a socket, and reserved mbuf space.
432 */
433void
434sbrelease(sb, so)
435 struct sockbuf *sb;
436 struct socket *so;
437{
438
439 sbflush(sb);
440 (void)chgsbsize(so->so_cred->cr_uidinfo, &sb->sb_hiwat, 0,
441 RLIM_INFINITY);
442 sb->sb_mbmax = 0;
443}
444
445/*
446 * Routines to add and remove
447 * data from an mbuf queue.
448 *
449 * The routines sbappend() or sbappendrecord() are normally called to
450 * append new mbufs to a socket buffer, after checking that adequate
451 * space is available, comparing the function sbspace() with the amount
452 * of data to be added. sbappendrecord() differs from sbappend() in
453 * that data supplied is treated as the beginning of a new record.
454 * To place a sender's address, optional access rights, and data in a
455 * socket receive buffer, sbappendaddr() should be used. To place
456 * access rights and data in a socket receive buffer, sbappendrights()
457 * should be used. In either case, the new data begins a new record.
458 * Note that unlike sbappend() and sbappendrecord(), these routines check
459 * for the caller that there will be enough space to store the data.
460 * Each fails if there is not enough space, or if it cannot find mbufs
461 * to store additional information in.
462 *
463 * Reliable protocols may use the socket send buffer to hold data
464 * awaiting acknowledgement. Data is normally copied from a socket
465 * send buffer in a protocol with m_copy for output to a peer,
466 * and then removing the data from the socket buffer with sbdrop()
467 * or sbdroprecord() when the data is acknowledged by the peer.
468 */
469
470/*
471 * Append mbuf chain m to the last record in the
472 * socket buffer sb. The additional space associated
473 * the mbuf chain is recorded in sb. Empty mbufs are
474 * discarded and mbufs are compacted where possible.
475 */
476void
477sbappend(sb, m)
478 struct sockbuf *sb;
479 struct mbuf *m;
480{
1fd87d54 481 struct mbuf *n;
984263bc
MD
482
483 if (m == 0)
484 return;
485 n = sb->sb_mb;
486 if (n) {
487 while (n->m_nextpkt)
488 n = n->m_nextpkt;
489 do {
490 if (n->m_flags & M_EOR) {
491 sbappendrecord(sb, m); /* XXXXXX!!!! */
492 return;
493 }
494 } while (n->m_next && (n = n->m_next));
495 }
496 sbcompress(sb, m, n);
497}
498
499#ifdef SOCKBUF_DEBUG
500void
501sbcheck(sb)
1fd87d54 502 struct sockbuf *sb;
984263bc 503{
1fd87d54
RG
504 struct mbuf *m;
505 struct mbuf *n = 0;
506 u_long len = 0, mbcnt = 0;
984263bc
MD
507
508 for (m = sb->sb_mb; m; m = n) {
509 n = m->m_nextpkt;
510 for (; m; m = m->m_next) {
511 len += m->m_len;
512 mbcnt += MSIZE;
513 if (m->m_flags & M_EXT) /*XXX*/ /* pretty sure this is bogus */
514 mbcnt += m->m_ext.ext_size;
515 }
516 }
517 if (len != sb->sb_cc || mbcnt != sb->sb_mbcnt) {
518 printf("cc %ld != %ld || mbcnt %ld != %ld\n", len, sb->sb_cc,
519 mbcnt, sb->sb_mbcnt);
520 panic("sbcheck");
521 }
522}
523#endif
524
525/*
526 * As above, except the mbuf chain
527 * begins a new record.
528 */
529void
530sbappendrecord(sb, m0)
1fd87d54
RG
531 struct sockbuf *sb;
532 struct mbuf *m0;
984263bc 533{
1fd87d54 534 struct mbuf *m;
984263bc
MD
535
536 if (m0 == 0)
537 return;
538 m = sb->sb_mb;
539 if (m)
540 while (m->m_nextpkt)
541 m = m->m_nextpkt;
542 /*
543 * Put the first mbuf on the queue.
544 * Note this permits zero length records.
545 */
546 sballoc(sb, m0);
547 if (m)
548 m->m_nextpkt = m0;
549 else
550 sb->sb_mb = m0;
551 m = m0->m_next;
552 m0->m_next = 0;
553 if (m && (m0->m_flags & M_EOR)) {
554 m0->m_flags &= ~M_EOR;
555 m->m_flags |= M_EOR;
556 }
557 sbcompress(sb, m, m0);
558}
559
560/*
561 * As above except that OOB data
562 * is inserted at the beginning of the sockbuf,
563 * but after any other OOB data.
564 */
565void
566sbinsertoob(sb, m0)
1fd87d54
RG
567 struct sockbuf *sb;
568 struct mbuf *m0;
984263bc 569{
1fd87d54
RG
570 struct mbuf *m;
571 struct mbuf **mp;
984263bc
MD
572
573 if (m0 == 0)
574 return;
575 for (mp = &sb->sb_mb; *mp ; mp = &((*mp)->m_nextpkt)) {
576 m = *mp;
577 again:
578 switch (m->m_type) {
579
580 case MT_OOBDATA:
581 continue; /* WANT next train */
582
583 case MT_CONTROL:
584 m = m->m_next;
585 if (m)
586 goto again; /* inspect THIS train further */
587 }
588 break;
589 }
590 /*
591 * Put the first mbuf on the queue.
592 * Note this permits zero length records.
593 */
594 sballoc(sb, m0);
595 m0->m_nextpkt = *mp;
596 *mp = m0;
597 m = m0->m_next;
598 m0->m_next = 0;
599 if (m && (m0->m_flags & M_EOR)) {
600 m0->m_flags &= ~M_EOR;
601 m->m_flags |= M_EOR;
602 }
603 sbcompress(sb, m, m0);
604}
605
606/*
607 * Append address and data, and optionally, control (ancillary) data
608 * to the receive queue of a socket. If present,
609 * m0 must include a packet header with total length.
610 * Returns 0 if no space in sockbuf or insufficient mbufs.
611 */
612int
613sbappendaddr(sb, asa, m0, control)
1fd87d54 614 struct sockbuf *sb;
984263bc
MD
615 struct sockaddr *asa;
616 struct mbuf *m0, *control;
617{
1fd87d54 618 struct mbuf *m, *n;
984263bc
MD
619 int space = asa->sa_len;
620
a8a35116
HP
621 if (m0 && (m0->m_flags & M_PKTHDR) == 0)
622 panic("sbappendaddr");
623
984263bc
MD
624 if (m0)
625 space += m0->m_pkthdr.len;
626 for (n = control; n; n = n->m_next) {
627 space += n->m_len;
628 if (n->m_next == 0) /* keep pointer to last control buf */
629 break;
630 }
631 if (space > sbspace(sb))
632 return (0);
633 if (asa->sa_len > MLEN)
634 return (0);
74f1caca 635 MGET(m, MB_DONTWAIT, MT_SONAME);
984263bc
MD
636 if (m == 0)
637 return (0);
638 m->m_len = asa->sa_len;
639 bcopy((caddr_t)asa, mtod(m, caddr_t), asa->sa_len);
640 if (n)
641 n->m_next = m0; /* concatenate data to control */
642 else
643 control = m0;
644 m->m_next = control;
645 for (n = m; n; n = n->m_next)
646 sballoc(sb, n);
647 n = sb->sb_mb;
648 if (n) {
649 while (n->m_nextpkt)
650 n = n->m_nextpkt;
651 n->m_nextpkt = m;
652 } else
653 sb->sb_mb = m;
654 return (1);
655}
656
657int
658sbappendcontrol(sb, m0, control)
659 struct sockbuf *sb;
660 struct mbuf *control, *m0;
661{
1fd87d54 662 struct mbuf *m, *n;
984263bc
MD
663 int space = 0;
664
665 if (control == 0)
666 panic("sbappendcontrol");
667 for (m = control; ; m = m->m_next) {
668 space += m->m_len;
669 if (m->m_next == 0)
670 break;
671 }
672 n = m; /* save pointer to last control buffer */
673 for (m = m0; m; m = m->m_next)
674 space += m->m_len;
675 if (space > sbspace(sb))
676 return (0);
677 n->m_next = m0; /* concatenate data to control */
678 for (m = control; m; m = m->m_next)
679 sballoc(sb, m);
680 n = sb->sb_mb;
681 if (n) {
682 while (n->m_nextpkt)
683 n = n->m_nextpkt;
684 n->m_nextpkt = control;
685 } else
686 sb->sb_mb = control;
687 return (1);
688}
689
690/*
691 * Compress mbuf chain m into the socket
692 * buffer sb following mbuf n. If n
693 * is null, the buffer is presumed empty.
694 */
695void
696sbcompress(sb, m, n)
1fd87d54
RG
697 struct sockbuf *sb;
698 struct mbuf *m, *n;
984263bc 699{
1fd87d54
RG
700 int eor = 0;
701 struct mbuf *o;
984263bc
MD
702
703 while (m) {
704 eor |= m->m_flags & M_EOR;
705 if (m->m_len == 0 &&
706 (eor == 0 ||
707 (((o = m->m_next) || (o = n)) &&
708 o->m_type == m->m_type))) {
709 m = m_free(m);
710 continue;
711 }
712 if (n && (n->m_flags & M_EOR) == 0 &&
713 M_WRITABLE(n) &&
714 m->m_len <= MCLBYTES / 4 && /* XXX: Don't copy too much */
715 m->m_len <= M_TRAILINGSPACE(n) &&
716 n->m_type == m->m_type) {
717 bcopy(mtod(m, caddr_t), mtod(n, caddr_t) + n->m_len,
718 (unsigned)m->m_len);
719 n->m_len += m->m_len;
720 sb->sb_cc += m->m_len;
721 m = m_free(m);
722 continue;
723 }
724 if (n)
725 n->m_next = m;
726 else
727 sb->sb_mb = m;
728 sballoc(sb, m);
729 n = m;
730 m->m_flags &= ~M_EOR;
731 m = m->m_next;
732 n->m_next = 0;
733 }
734 if (eor) {
735 if (n)
736 n->m_flags |= eor;
737 else
fc92d4aa 738 printf("semi-panic: sbcompress");
984263bc
MD
739 }
740}
741
742/*
743 * Free all mbufs in a sockbuf.
744 * Check that all resources are reclaimed.
745 */
746void
747sbflush(sb)
1fd87d54 748 struct sockbuf *sb;
984263bc
MD
749{
750
751 if (sb->sb_flags & SB_LOCK)
752 panic("sbflush: locked");
753 while (sb->sb_mbcnt) {
754 /*
755 * Don't call sbdrop(sb, 0) if the leading mbuf is non-empty:
756 * we would loop forever. Panic instead.
757 */
758 if (!sb->sb_cc && (sb->sb_mb == NULL || sb->sb_mb->m_len))
759 break;
760 sbdrop(sb, (int)sb->sb_cc);
761 }
762 if (sb->sb_cc || sb->sb_mb || sb->sb_mbcnt)
763 panic("sbflush: cc %ld || mb %p || mbcnt %ld", sb->sb_cc, (void *)sb->sb_mb, sb->sb_mbcnt);
764}
765
766/*
767 * Drop data from (the front of) a sockbuf.
768 */
769void
770sbdrop(sb, len)
1fd87d54
RG
771 struct sockbuf *sb;
772 int len;
984263bc 773{
1fd87d54 774 struct mbuf *m;
984263bc
MD
775 struct mbuf *next;
776
777 next = (m = sb->sb_mb) ? m->m_nextpkt : 0;
778 while (len > 0) {
779 if (m == 0) {
780 if (next == 0)
781 panic("sbdrop");
782 m = next;
783 next = m->m_nextpkt;
784 continue;
785 }
786 if (m->m_len > len) {
787 m->m_len -= len;
788 m->m_data += len;
789 sb->sb_cc -= len;
790 break;
791 }
792 len -= m->m_len;
793 sbfree(sb, m);
794 m = m_free(m);
795 }
796 while (m && m->m_len == 0) {
797 sbfree(sb, m);
798 m = m_free(m);
799 }
800 if (m) {
801 sb->sb_mb = m;
802 m->m_nextpkt = next;
803 } else
804 sb->sb_mb = next;
805}
806
807/*
808 * Drop a record off the front of a sockbuf
809 * and move the next record to the front.
810 */
811void
812sbdroprecord(sb)
1fd87d54 813 struct sockbuf *sb;
984263bc 814{
1fd87d54 815 struct mbuf *m;
984263bc
MD
816
817 m = sb->sb_mb;
818 if (m) {
819 sb->sb_mb = m->m_nextpkt;
820 do {
821 sbfree(sb, m);
822 m = m_free(m);
823 } while (m);
824 }
825}
826
827/*
828 * Create a "control" mbuf containing the specified data
829 * with the specified type for presentation on a socket buffer.
830 */
831struct mbuf *
832sbcreatecontrol(p, size, type, level)
833 caddr_t p;
1fd87d54 834 int size;
984263bc
MD
835 int type, level;
836{
1fd87d54 837 struct cmsghdr *cp;
984263bc
MD
838 struct mbuf *m;
839
840 if (CMSG_SPACE((u_int)size) > MCLBYTES)
841 return ((struct mbuf *) NULL);
74f1caca 842 if ((m = m_get(MB_DONTWAIT, MT_CONTROL)) == NULL)
984263bc
MD
843 return ((struct mbuf *) NULL);
844 if (CMSG_SPACE((u_int)size) > MLEN) {
74f1caca 845 MCLGET(m, MB_DONTWAIT);
984263bc
MD
846 if ((m->m_flags & M_EXT) == 0) {
847 m_free(m);
848 return ((struct mbuf *) NULL);
849 }
850 }
851 cp = mtod(m, struct cmsghdr *);
852 m->m_len = 0;
853 KASSERT(CMSG_SPACE((u_int)size) <= M_TRAILINGSPACE(m),
854 ("sbcreatecontrol: short mbuf"));
855 if (p != NULL)
856 (void)memcpy(CMSG_DATA(cp), p, size);
857 m->m_len = CMSG_SPACE(size);
858 cp->cmsg_len = CMSG_LEN(size);
859 cp->cmsg_level = level;
860 cp->cmsg_type = type;
861 return (m);
862}
863
864/*
865 * Some routines that return EOPNOTSUPP for entry points that are not
866 * supported by a protocol. Fill in as needed.
867 */
868int
869pru_accept_notsupp(struct socket *so, struct sockaddr **nam)
870{
871 return EOPNOTSUPP;
872}
873
874int
dadab5e9 875pru_connect_notsupp(struct socket *so, struct sockaddr *nam, struct thread *td)
984263bc
MD
876{
877 return EOPNOTSUPP;
878}
879
880int
881pru_connect2_notsupp(struct socket *so1, struct socket *so2)
882{
883 return EOPNOTSUPP;
884}
885
886int
887pru_control_notsupp(struct socket *so, u_long cmd, caddr_t data,
dadab5e9 888 struct ifnet *ifp, struct thread *td)
984263bc
MD
889{
890 return EOPNOTSUPP;
891}
892
893int
dadab5e9 894pru_listen_notsupp(struct socket *so, struct thread *td)
984263bc
MD
895{
896 return EOPNOTSUPP;
897}
898
899int
900pru_rcvd_notsupp(struct socket *so, int flags)
901{
902 return EOPNOTSUPP;
903}
904
905int
906pru_rcvoob_notsupp(struct socket *so, struct mbuf *m, int flags)
907{
908 return EOPNOTSUPP;
909}
910
911/*
912 * This isn't really a ``null'' operation, but it's the default one
913 * and doesn't do anything destructive.
914 */
915int
916pru_sense_null(struct socket *so, struct stat *sb)
917{
918 sb->st_blksize = so->so_snd.sb_hiwat;
919 return 0;
920}
921
922/*
923 * Make a copy of a sockaddr in a malloced buffer of type M_SONAME.
924 */
925struct sockaddr *
926dup_sockaddr(sa, canwait)
927 struct sockaddr *sa;
928 int canwait;
929{
930 struct sockaddr *sa2;
931
932 MALLOC(sa2, struct sockaddr *, sa->sa_len, M_SONAME,
933 canwait ? M_WAITOK : M_NOWAIT);
934 if (sa2)
935 bcopy(sa, sa2, sa->sa_len);
936 return sa2;
937}
938
939/*
940 * Create an external-format (``xsocket'') structure using the information
941 * in the kernel-format socket structure pointed to by so. This is done
942 * to reduce the spew of irrelevant information over this interface,
943 * to isolate user code from changes in the kernel structure, and
944 * potentially to provide information-hiding if we decide that
945 * some of this information should be hidden from users.
946 */
947void
948sotoxsocket(struct socket *so, struct xsocket *xso)
949{
950 xso->xso_len = sizeof *xso;
951 xso->xso_so = so;
952 xso->so_type = so->so_type;
953 xso->so_options = so->so_options;
954 xso->so_linger = so->so_linger;
955 xso->so_state = so->so_state;
956 xso->so_pcb = so->so_pcb;
957 xso->xso_protocol = so->so_proto->pr_protocol;
958 xso->xso_family = so->so_proto->pr_domain->dom_family;
959 xso->so_qlen = so->so_qlen;
960 xso->so_incqlen = so->so_incqlen;
961 xso->so_qlimit = so->so_qlimit;
962 xso->so_timeo = so->so_timeo;
963 xso->so_error = so->so_error;
964 xso->so_pgid = so->so_sigio ? so->so_sigio->sio_pgid : 0;
965 xso->so_oobmark = so->so_oobmark;
966 sbtoxsockbuf(&so->so_snd, &xso->so_snd);
967 sbtoxsockbuf(&so->so_rcv, &xso->so_rcv);
968 xso->so_uid = so->so_cred->cr_uid;
969}
970
971/*
972 * This does the same for sockbufs. Note that the xsockbuf structure,
973 * since it is always embedded in a socket, does not include a self
974 * pointer nor a length. We make this entry point public in case
975 * some other mechanism needs it.
976 */
977void
978sbtoxsockbuf(struct sockbuf *sb, struct xsockbuf *xsb)
979{
980 xsb->sb_cc = sb->sb_cc;
981 xsb->sb_hiwat = sb->sb_hiwat;
982 xsb->sb_mbcnt = sb->sb_mbcnt;
983 xsb->sb_mbmax = sb->sb_mbmax;
984 xsb->sb_lowat = sb->sb_lowat;
985 xsb->sb_flags = sb->sb_flags;
986 xsb->sb_timeo = sb->sb_timeo;
987}
988
989/*
990 * Here is the definition of some of the basic objects in the kern.ipc
991 * branch of the MIB.
992 */
993SYSCTL_NODE(_kern, KERN_IPC, ipc, CTLFLAG_RW, 0, "IPC");
994
995/* This takes the place of kern.maxsockbuf, which moved to kern.ipc. */
996static int dummy;
997SYSCTL_INT(_kern, KERN_DUMMY, dummy, CTLFLAG_RW, &dummy, 0, "");
998SYSCTL_OID(_kern_ipc, KIPC_MAXSOCKBUF, maxsockbuf, CTLTYPE_INT|CTLFLAG_RW,
999 &sb_max, 0, sysctl_handle_sb_max, "I", "Maximum socket buffer size");
1000SYSCTL_INT(_kern_ipc, OID_AUTO, maxsockets, CTLFLAG_RD,
1001 &maxsockets, 0, "Maximum number of sockets avaliable");
1002SYSCTL_INT(_kern_ipc, KIPC_SOCKBUF_WASTE, sockbuf_waste_factor, CTLFLAG_RW,
1003 &sb_efficiency, 0, "");
1004
1005/*
1006 * Initialise maxsockets
1007 */
1008static void init_maxsockets(void *ignored)
1009{
1010 TUNABLE_INT_FETCH("kern.ipc.maxsockets", &maxsockets);
1011 maxsockets = imax(maxsockets, imax(maxfiles, nmbclusters));
1012}
1013SYSINIT(param, SI_SUB_TUNABLES, SI_ORDER_ANY, init_maxsockets, NULL);