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