1 /*	$OpenBSD: uipc_socket2.c,v 1.40 2005/07/18 02:43:27 fgsch Exp $	*/
2 /*	$NetBSD: uipc_socket2.c,v 1.11 1996/02/04 02:17:55 christos Exp $	*/
3 
4 /*
5  * Copyright (c) 1982, 1986, 1988, 1990, 1993
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 3. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  *	@(#)uipc_socket2.c	8.1 (Berkeley) 6/10/93
33  */
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/proc.h>
38 #include <sys/file.h>
39 #include <sys/buf.h>
40 #include <sys/malloc.h>
41 #include <sys/mbuf.h>
42 #include <sys/protosw.h>
43 #include <sys/socket.h>
44 #include <sys/socketvar.h>
45 #include <sys/signalvar.h>
46 #include <sys/event.h>
47 
48 /*
49  * Primitive routines for operating on sockets and socket buffers
50  */
51 
52 /* strings for sleep message: */
53 const char	netcon[] = "netcon";
54 const char	netcls[] = "netcls";
55 const char	netio[] = "netio";
56 const char	netlck[] = "netlck";
57 
58 u_long	sb_max = SB_MAX;		/* patchable */
59 
60 /*
61  * Procedures to manipulate state flags of socket
62  * and do appropriate wakeups.  Normal sequence from the
63  * active (originating) side is that soisconnecting() is
64  * called during processing of connect() call,
65  * resulting in an eventual call to soisconnected() if/when the
66  * connection is established.  When the connection is torn down
67  * soisdisconnecting() is called during processing of disconnect() call,
68  * and soisdisconnected() is called when the connection to the peer
69  * is totally severed.  The semantics of these routines are such that
70  * connectionless protocols can call soisconnected() and soisdisconnected()
71  * only, bypassing the in-progress calls when setting up a ``connection''
72  * takes no time.
73  *
74  * From the passive side, a socket is created with
75  * two queues of sockets: so_q0 for connections in progress
76  * and so_q for connections already made and awaiting user acceptance.
77  * As a protocol is preparing incoming connections, it creates a socket
78  * structure queued on so_q0 by calling sonewconn().  When the connection
79  * is established, soisconnected() is called, and transfers the
80  * socket structure to so_q, making it available to accept().
81  *
82  * If a socket is closed with sockets on either
83  * so_q0 or so_q, these sockets are dropped.
84  *
85  * If higher level protocols are implemented in
86  * the kernel, the wakeups done here will sometimes
87  * cause software-interrupt process scheduling.
88  */
89 
90 void
soisconnecting(so)91 soisconnecting(so)
92 	register struct socket *so;
93 {
94 
95 	so->so_state &= ~(SS_ISCONNECTED|SS_ISDISCONNECTING);
96 	so->so_state |= SS_ISCONNECTING;
97 }
98 
99 void
soisconnected(so)100 soisconnected(so)
101 	register struct socket *so;
102 {
103 	register struct socket *head = so->so_head;
104 
105 	so->so_state &= ~(SS_ISCONNECTING|SS_ISDISCONNECTING|SS_ISCONFIRMING);
106 	so->so_state |= SS_ISCONNECTED;
107 	if (head && soqremque(so, 0)) {
108 		soqinsque(head, so, 1);
109 		sorwakeup(head);
110 		wakeup_one(&head->so_timeo);
111 	} else {
112 		wakeup(&so->so_timeo);
113 		sorwakeup(so);
114 		sowwakeup(so);
115 	}
116 }
117 
118 void
soisdisconnecting(so)119 soisdisconnecting(so)
120 	register struct socket *so;
121 {
122 
123 	so->so_state &= ~SS_ISCONNECTING;
124 	so->so_state |= (SS_ISDISCONNECTING|SS_CANTRCVMORE|SS_CANTSENDMORE);
125 	wakeup(&so->so_timeo);
126 	sowwakeup(so);
127 	sorwakeup(so);
128 }
129 
130 void
soisdisconnected(so)131 soisdisconnected(so)
132 	register struct socket *so;
133 {
134 
135 	so->so_state &= ~(SS_ISCONNECTING|SS_ISCONNECTED|SS_ISDISCONNECTING);
136 	so->so_state |= (SS_CANTRCVMORE|SS_CANTSENDMORE|SS_ISDISCONNECTED);
137 	wakeup(&so->so_timeo);
138 	sowwakeup(so);
139 	sorwakeup(so);
140 }
141 
142 /*
143  * When an attempt at a new connection is noted on a socket
144  * which accepts connections, sonewconn is called.  If the
145  * connection is possible (subject to space constraints, etc.)
146  * then we allocate a new structure, properly linked into the
147  * data structure of the original socket, and return this.
148  * Connstatus may be 0, or SS_ISCONFIRMING, or SS_ISCONNECTED.
149  *
150  * Must be called at splsoftnet()
151  */
152 struct socket *
sonewconn(struct socket * head,int connstatus)153 sonewconn(struct socket *head, int connstatus)
154 {
155 	struct socket *so;
156 	int soqueue = connstatus ? 1 : 0;
157 	extern u_long unpst_sendspace, unpst_recvspace;
158 	u_long snd_sb_hiwat, rcv_sb_hiwat;
159 
160 	splassert(IPL_SOFTNET);
161 
162 	if (mclpool.pr_nout > mclpool.pr_hardlimit * 95 / 100)
163 		return ((struct socket *)0);
164 	if (head->so_qlen + head->so_q0len > head->so_qlimit * 3)
165 		return ((struct socket *)0);
166 	so = pool_get(&socket_pool, PR_NOWAIT);
167 	if (so == NULL)
168 		return ((struct socket *)0);
169 	bzero(so, sizeof(*so));
170 	so->so_type = head->so_type;
171 	so->so_options = head->so_options &~ SO_ACCEPTCONN;
172 	so->so_linger = head->so_linger;
173 	so->so_state = head->so_state | SS_NOFDREF;
174 	so->so_proto = head->so_proto;
175 	so->so_timeo = head->so_timeo;
176 	so->so_pgid = head->so_pgid;
177 	so->so_euid = head->so_euid;
178 	so->so_ruid = head->so_ruid;
179 	so->so_egid = head->so_egid;
180 	so->so_rgid = head->so_rgid;
181 	so->so_siguid = head->so_siguid;
182 	so->so_sigeuid = head->so_sigeuid;
183 
184 	/*
185 	 * If we are tight on mbuf clusters, create the new socket
186 	 * with the minimum.  Sorry, you lose.
187 	 */
188 	snd_sb_hiwat = head->so_snd.sb_hiwat;
189 	if (sbcheckreserve(snd_sb_hiwat, unpst_sendspace))
190 		snd_sb_hiwat = unpst_sendspace;		/* and udp? */
191 	rcv_sb_hiwat = head->so_rcv.sb_hiwat;
192 	if (sbcheckreserve(rcv_sb_hiwat, unpst_recvspace))
193 		rcv_sb_hiwat = unpst_recvspace;		/* and udp? */
194 
195 	(void) soreserve(so, snd_sb_hiwat, rcv_sb_hiwat);
196 	soqinsque(head, so, soqueue);
197 	if ((*so->so_proto->pr_usrreq)(so, PRU_ATTACH,
198 	    (struct mbuf *)0, (struct mbuf *)0, (struct mbuf *)0)) {
199 		(void) soqremque(so, soqueue);
200 		pool_put(&socket_pool, so);
201 		return ((struct socket *)0);
202 	}
203 	if (connstatus) {
204 		sorwakeup(head);
205 		wakeup(&head->so_timeo);
206 		so->so_state |= connstatus;
207 	}
208 	return (so);
209 }
210 
211 void
soqinsque(struct socket * head,struct socket * so,int q)212 soqinsque(struct socket *head, struct socket *so, int q)
213 {
214 
215 #ifdef DIAGNOSTIC
216 	if (so->so_onq != NULL)
217 		panic("soqinsque");
218 #endif
219 
220 	so->so_head = head;
221 	if (q == 0) {
222 		head->so_q0len++;
223 		so->so_onq = &head->so_q0;
224 	} else {
225 		head->so_qlen++;
226 		so->so_onq = &head->so_q;
227 	}
228 	TAILQ_INSERT_TAIL(so->so_onq, so, so_qe);
229 }
230 
231 int
soqremque(struct socket * so,int q)232 soqremque(struct socket *so, int q)
233 {
234 	struct socket *head;
235 
236 	head = so->so_head;
237 	if (q == 0) {
238 		if (so->so_onq != &head->so_q0)
239 			return (0);
240 		head->so_q0len--;
241 	} else {
242 		if (so->so_onq != &head->so_q)
243 			return (0);
244 		head->so_qlen--;
245 	}
246 	TAILQ_REMOVE(so->so_onq, so, so_qe);
247 	so->so_onq = NULL;
248 	so->so_head = NULL;
249 	return (1);
250 }
251 
252 /*
253  * Socantsendmore indicates that no more data will be sent on the
254  * socket; it would normally be applied to a socket when the user
255  * informs the system that no more data is to be sent, by the protocol
256  * code (in case PRU_SHUTDOWN).  Socantrcvmore indicates that no more data
257  * will be received, and will normally be applied to the socket by a
258  * protocol when it detects that the peer will send no more data.
259  * Data queued for reading in the socket may yet be read.
260  */
261 
262 void
socantsendmore(so)263 socantsendmore(so)
264 	struct socket *so;
265 {
266 
267 	so->so_state |= SS_CANTSENDMORE;
268 	sowwakeup(so);
269 }
270 
271 void
socantrcvmore(so)272 socantrcvmore(so)
273 	struct socket *so;
274 {
275 
276 	so->so_state |= SS_CANTRCVMORE;
277 	sorwakeup(so);
278 }
279 
280 /*
281  * Wait for data to arrive at/drain from a socket buffer.
282  */
283 int
sbwait(sb)284 sbwait(sb)
285 	struct sockbuf *sb;
286 {
287 
288 	sb->sb_flags |= SB_WAIT;
289 	return (tsleep(&sb->sb_cc,
290 	    (sb->sb_flags & SB_NOINTR) ? PSOCK : PSOCK | PCATCH, netio,
291 	    sb->sb_timeo));
292 }
293 
294 /*
295  * Lock a sockbuf already known to be locked;
296  * return any error returned from sleep (EINTR).
297  */
298 int
sb_lock(sb)299 sb_lock(sb)
300 	register struct sockbuf *sb;
301 {
302 	int error;
303 
304 	while (sb->sb_flags & SB_LOCK) {
305 		sb->sb_flags |= SB_WANT;
306 		error = tsleep(&sb->sb_flags,
307 		    (sb->sb_flags & SB_NOINTR) ?
308 		    PSOCK : PSOCK|PCATCH, netlck, 0);
309 		if (error)
310 			return (error);
311 	}
312 	sb->sb_flags |= SB_LOCK;
313 	return (0);
314 }
315 
316 /*
317  * Wakeup processes waiting on a socket buffer.
318  * Do asynchronous notification via SIGIO
319  * if the socket has the SS_ASYNC flag set.
320  */
321 void
sowakeup(so,sb)322 sowakeup(so, sb)
323 	register struct socket *so;
324 	register struct sockbuf *sb;
325 {
326 	selwakeup(&sb->sb_sel);
327 	sb->sb_flags &= ~SB_SEL;
328 	if (sb->sb_flags & SB_WAIT) {
329 		sb->sb_flags &= ~SB_WAIT;
330 		wakeup(&sb->sb_cc);
331 	}
332 	if (so->so_state & SS_ASYNC)
333 		csignal(so->so_pgid, SIGIO, so->so_siguid, so->so_sigeuid);
334 	KNOTE(&sb->sb_sel.si_note, 0);
335 }
336 
337 /*
338  * Socket buffer (struct sockbuf) utility routines.
339  *
340  * Each socket contains two socket buffers: one for sending data and
341  * one for receiving data.  Each buffer contains a queue of mbufs,
342  * information about the number of mbufs and amount of data in the
343  * queue, and other fields allowing select() statements and notification
344  * on data availability to be implemented.
345  *
346  * Data stored in a socket buffer is maintained as a list of records.
347  * Each record is a list of mbufs chained together with the m_next
348  * field.  Records are chained together with the m_nextpkt field. The upper
349  * level routine soreceive() expects the following conventions to be
350  * observed when placing information in the receive buffer:
351  *
352  * 1. If the protocol requires each message be preceded by the sender's
353  *    name, then a record containing that name must be present before
354  *    any associated data (mbuf's must be of type MT_SONAME).
355  * 2. If the protocol supports the exchange of ``access rights'' (really
356  *    just additional data associated with the message), and there are
357  *    ``rights'' to be received, then a record containing this data
358  *    should be present (mbuf's must be of type MT_CONTROL).
359  * 3. If a name or rights record exists, then it must be followed by
360  *    a data record, perhaps of zero length.
361  *
362  * Before using a new socket structure it is first necessary to reserve
363  * buffer space to the socket, by calling sbreserve().  This should commit
364  * some of the available buffer space in the system buffer pool for the
365  * socket (currently, it does nothing but enforce limits).  The space
366  * should be released by calling sbrelease() when the socket is destroyed.
367  */
368 
369 int
soreserve(so,sndcc,rcvcc)370 soreserve(so, sndcc, rcvcc)
371 	register struct socket *so;
372 	u_long sndcc, rcvcc;
373 {
374 
375 	if (sbreserve(&so->so_snd, sndcc) == 0)
376 		goto bad;
377 	if (sbreserve(&so->so_rcv, rcvcc) == 0)
378 		goto bad2;
379 	if (so->so_rcv.sb_lowat == 0)
380 		so->so_rcv.sb_lowat = 1;
381 	if (so->so_snd.sb_lowat == 0)
382 		so->so_snd.sb_lowat = MCLBYTES;
383 	if (so->so_snd.sb_lowat > so->so_snd.sb_hiwat)
384 		so->so_snd.sb_lowat = so->so_snd.sb_hiwat;
385 	return (0);
386 bad2:
387 	sbrelease(&so->so_snd);
388 bad:
389 	return (ENOBUFS);
390 }
391 
392 /*
393  * Allot mbufs to a sockbuf.
394  * Attempt to scale mbmax so that mbcnt doesn't become limiting
395  * if buffering efficiency is near the normal case.
396  */
397 int
sbreserve(sb,cc)398 sbreserve(sb, cc)
399 	struct sockbuf *sb;
400 	u_long cc;
401 {
402 
403 	if (cc == 0 || cc > sb_max)
404 		return (0);
405 	sb->sb_hiwat = cc;
406 	sb->sb_mbmax = min(cc * 2, sb_max + (sb_max / MCLBYTES) * MSIZE);
407 	if (sb->sb_lowat > sb->sb_hiwat)
408 		sb->sb_lowat = sb->sb_hiwat;
409 	return (1);
410 }
411 
412 /*
413  * If over 50% of mbuf clusters in use, do not accept any
414  * greater than normal request.
415  */
416 int
sbcheckreserve(u_long cnt,u_long defcnt)417 sbcheckreserve(u_long cnt, u_long defcnt)
418 {
419 	if (cnt > defcnt &&
420 	    mclpool.pr_nout> mclpool.pr_hardlimit / 2)
421 		return (ENOBUFS);
422 	return (0);
423 }
424 
425 /*
426  * Free mbufs held by a socket, and reserved mbuf space.
427  */
428 void
sbrelease(sb)429 sbrelease(sb)
430 	struct sockbuf *sb;
431 {
432 
433 	sbflush(sb);
434 	sb->sb_hiwat = sb->sb_mbmax = 0;
435 }
436 
437 /*
438  * Routines to add and remove
439  * data from an mbuf queue.
440  *
441  * The routines sbappend() or sbappendrecord() are normally called to
442  * append new mbufs to a socket buffer, after checking that adequate
443  * space is available, comparing the function sbspace() with the amount
444  * of data to be added.  sbappendrecord() differs from sbappend() in
445  * that data supplied is treated as the beginning of a new record.
446  * To place a sender's address, optional access rights, and data in a
447  * socket receive buffer, sbappendaddr() should be used.  To place
448  * access rights and data in a socket receive buffer, sbappendrights()
449  * should be used.  In either case, the new data begins a new record.
450  * Note that unlike sbappend() and sbappendrecord(), these routines check
451  * for the caller that there will be enough space to store the data.
452  * Each fails if there is not enough space, or if it cannot find mbufs
453  * to store additional information in.
454  *
455  * Reliable protocols may use the socket send buffer to hold data
456  * awaiting acknowledgement.  Data is normally copied from a socket
457  * send buffer in a protocol with m_copy for output to a peer,
458  * and then removing the data from the socket buffer with sbdrop()
459  * or sbdroprecord() when the data is acknowledged by the peer.
460  */
461 
462 #ifdef SOCKBUF_DEBUG
463 void
sblastrecordchk(struct sockbuf * sb,const char * where)464 sblastrecordchk(struct sockbuf *sb, const char *where)
465 {
466 	struct mbuf *m = sb->sb_mb;
467 
468 	while (m && m->m_nextpkt)
469 		m = m->m_nextpkt;
470 
471 	if (m != sb->sb_lastrecord) {
472 		printf("sblastrecordchk: sb_mb %p sb_lastrecord %p last %p\n",
473 		    sb->sb_mb, sb->sb_lastrecord, m);
474 		printf("packet chain:\n");
475 		for (m = sb->sb_mb; m != NULL; m = m->m_nextpkt)
476 			printf("\t%p\n", m);
477 		panic("sblastrecordchk from %s", where);
478 	}
479 }
480 
481 void
sblastmbufchk(struct sockbuf * sb,const char * where)482 sblastmbufchk(struct sockbuf *sb, const char *where)
483 {
484 	struct mbuf *m = sb->sb_mb;
485 	struct mbuf *n;
486 
487 	while (m && m->m_nextpkt)
488 		m = m->m_nextpkt;
489 
490 	while (m && m->m_next)
491 		m = m->m_next;
492 
493 	if (m != sb->sb_mbtail) {
494 		printf("sblastmbufchk: sb_mb %p sb_mbtail %p last %p\n",
495 		    sb->sb_mb, sb->sb_mbtail, m);
496 		printf("packet tree:\n");
497 		for (m = sb->sb_mb; m != NULL; m = m->m_nextpkt) {
498 			printf("\t");
499 			for (n = m; n != NULL; n = n->m_next)
500 				printf("%p ", n);
501 			printf("\n");
502 		}
503 		panic("sblastmbufchk from %s", where);
504 	}
505 }
506 #endif /* SOCKBUF_DEBUG */
507 
508 #define	SBLINKRECORD(sb, m0)						\
509 do {									\
510 	if ((sb)->sb_lastrecord != NULL)				\
511 		(sb)->sb_lastrecord->m_nextpkt = (m0);			\
512 	else								\
513 		(sb)->sb_mb = (m0);					\
514 	(sb)->sb_lastrecord = (m0);					\
515 } while (/*CONSTCOND*/0)
516 
517 /*
518  * Append mbuf chain m to the last record in the
519  * socket buffer sb.  The additional space associated
520  * the mbuf chain is recorded in sb.  Empty mbufs are
521  * discarded and mbufs are compacted where possible.
522  */
523 void
sbappend(sb,m)524 sbappend(sb, m)
525 	struct sockbuf *sb;
526 	struct mbuf *m;
527 {
528 	register struct mbuf *n;
529 
530 	if (m == NULL)
531 		return;
532 
533 	SBLASTRECORDCHK(sb, "sbappend 1");
534 
535 	if ((n = sb->sb_lastrecord) != NULL) {
536 		/*
537 		 * XXX Would like to simply use sb_mbtail here, but
538 		 * XXX I need to verify that I won't miss an EOR that
539 		 * XXX way.
540 		 */
541 		do {
542 			if (n->m_flags & M_EOR) {
543 				sbappendrecord(sb, m); /* XXXXXX!!!! */
544 				return;
545 			}
546 		} while (n->m_next && (n = n->m_next));
547 	} else {
548 		/*
549 		 * If this is the first record in the socket buffer, it's
550 		 * also the last record.
551 		 */
552 		sb->sb_lastrecord = m;
553 	}
554 	sbcompress(sb, m, n);
555 	SBLASTRECORDCHK(sb, "sbappend 2");
556 }
557 
558 /*
559  * This version of sbappend() should only be used when the caller
560  * absolutely knows that there will never be more than one record
561  * in the socket buffer, that is, a stream protocol (such as TCP).
562  */
563 void
sbappendstream(struct sockbuf * sb,struct mbuf * m)564 sbappendstream(struct sockbuf *sb, struct mbuf *m)
565 {
566 
567 	KDASSERT(m->m_nextpkt == NULL);
568 	KASSERT(sb->sb_mb == sb->sb_lastrecord);
569 
570 	SBLASTMBUFCHK(sb, __func__);
571 
572 	sbcompress(sb, m, sb->sb_mbtail);
573 
574 	sb->sb_lastrecord = sb->sb_mb;
575 	SBLASTRECORDCHK(sb, __func__);
576 }
577 
578 #ifdef SOCKBUF_DEBUG
579 void
sbcheck(struct sockbuf * sb)580 sbcheck(struct sockbuf *sb)
581 {
582 	struct mbuf *m;
583 	u_long len = 0, mbcnt = 0;
584 
585 	for (m = sb->sb_mb; m; m = m->m_next) {
586 		len += m->m_len;
587 		mbcnt += MSIZE;
588 		if (m->m_flags & M_EXT)
589 			mbcnt += m->m_ext.ext_size;
590 		if (m->m_nextpkt)
591 			panic("sbcheck nextpkt");
592 	}
593 	if (len != sb->sb_cc || mbcnt != sb->sb_mbcnt) {
594 		printf("cc %lu != %lu || mbcnt %lu != %lu\n", len, sb->sb_cc,
595 		    mbcnt, sb->sb_mbcnt);
596 		panic("sbcheck");
597 	}
598 }
599 #endif
600 
601 /*
602  * As above, except the mbuf chain
603  * begins a new record.
604  */
605 void
sbappendrecord(struct sockbuf * sb,struct mbuf * m0)606 sbappendrecord(struct sockbuf *sb, struct mbuf *m0)
607 {
608 	struct mbuf *m;
609 
610 	if (m0 == NULL)
611 		return;
612 
613 	/*
614 	 * Put the first mbuf on the queue.
615 	 * Note this permits zero length records.
616 	 */
617 	sballoc(sb, m0);
618 	SBLASTRECORDCHK(sb, "sbappendrecord 1");
619 	SBLINKRECORD(sb, m0);
620 	m = m0->m_next;
621 	m0->m_next = NULL;
622 	if (m && (m0->m_flags & M_EOR)) {
623 		m0->m_flags &= ~M_EOR;
624 		m->m_flags |= M_EOR;
625 	}
626 	sbcompress(sb, m, m0);
627 	SBLASTRECORDCHK(sb, "sbappendrecord 2");
628 }
629 
630 /*
631  * As above except that OOB data
632  * is inserted at the beginning of the sockbuf,
633  * but after any other OOB data.
634  */
635 void
sbinsertoob(struct sockbuf * sb,struct mbuf * m0)636 sbinsertoob(struct sockbuf *sb, struct mbuf *m0)
637 {
638 	struct mbuf *m, **mp;
639 
640 	if (m0 == NULL)
641 		return;
642 
643 	SBLASTRECORDCHK(sb, "sbinsertoob 1");
644 
645 	for (mp = &sb->sb_mb; (m = *mp) != NULL; mp = &((*mp)->m_nextpkt)) {
646 	    again:
647 		switch (m->m_type) {
648 
649 		case MT_OOBDATA:
650 			continue;		/* WANT next train */
651 
652 		case MT_CONTROL:
653 			if ((m = m->m_next) != NULL)
654 				goto again;	/* inspect THIS train further */
655 		}
656 		break;
657 	}
658 	/*
659 	 * Put the first mbuf on the queue.
660 	 * Note this permits zero length records.
661 	 */
662 	sballoc(sb, m0);
663 	m0->m_nextpkt = *mp;
664 	if (*mp == NULL) {
665 		/* m0 is actually the new tail */
666 		sb->sb_lastrecord = m0;
667 	}
668 	*mp = m0;
669 	m = m0->m_next;
670 	m0->m_next = NULL;
671 	if (m && (m0->m_flags & M_EOR)) {
672 		m0->m_flags &= ~M_EOR;
673 		m->m_flags |= M_EOR;
674 	}
675 	sbcompress(sb, m, m0);
676 	SBLASTRECORDCHK(sb, "sbinsertoob 2");
677 }
678 
679 /*
680  * Append address and data, and optionally, control (ancillary) data
681  * to the receive queue of a socket.  If present,
682  * m0 must include a packet header with total length.
683  * Returns 0 if no space in sockbuf or insufficient mbufs.
684  */
685 int
sbappendaddr(struct sockbuf * sb,struct sockaddr * asa,struct mbuf * m0,struct mbuf * control)686 sbappendaddr(struct sockbuf *sb, struct sockaddr *asa, struct mbuf *m0,
687     struct mbuf *control)
688 {
689 	struct mbuf *m, *n, *nlast;
690 	int space = asa->sa_len;
691 
692 	if (m0 && (m0->m_flags & M_PKTHDR) == 0)
693 		panic("sbappendaddr");
694 	if (m0)
695 		space += m0->m_pkthdr.len;
696 	for (n = control; n; n = n->m_next) {
697 		space += n->m_len;
698 		if (n->m_next == NULL)	/* keep pointer to last control buf */
699 			break;
700 	}
701 	if (space > sbspace(sb))
702 		return (0);
703 	if (asa->sa_len > MLEN)
704 		return (0);
705 	MGET(m, M_DONTWAIT, MT_SONAME);
706 	if (m == NULL)
707 		return (0);
708 	m->m_len = asa->sa_len;
709 	bcopy(asa, mtod(m, caddr_t), asa->sa_len);
710 	if (n)
711 		n->m_next = m0;		/* concatenate data to control */
712 	else
713 		control = m0;
714 	m->m_next = control;
715 
716 	SBLASTRECORDCHK(sb, "sbappendaddr 1");
717 
718 	for (n = m; n->m_next != NULL; n = n->m_next)
719 		sballoc(sb, n);
720 	sballoc(sb, n);
721 	nlast = n;
722 	SBLINKRECORD(sb, m);
723 
724 	sb->sb_mbtail = nlast;
725 	SBLASTMBUFCHK(sb, "sbappendaddr");
726 
727 	SBLASTRECORDCHK(sb, "sbappendaddr 2");
728 
729 	return (1);
730 }
731 
732 int
sbappendcontrol(struct sockbuf * sb,struct mbuf * m0,struct mbuf * control)733 sbappendcontrol(struct sockbuf *sb, struct mbuf *m0, struct mbuf *control)
734 {
735 	struct mbuf *m, *mlast, *n;
736 	int space = 0;
737 
738 	if (control == NULL)
739 		panic("sbappendcontrol");
740 	for (m = control; ; m = m->m_next) {
741 		space += m->m_len;
742 		if (m->m_next == NULL)
743 			break;
744 	}
745 	n = m;			/* save pointer to last control buffer */
746 	for (m = m0; m; m = m->m_next)
747 		space += m->m_len;
748 	if (space > sbspace(sb))
749 		return (0);
750 	n->m_next = m0;			/* concatenate data to control */
751 
752 	SBLASTRECORDCHK(sb, "sbappendcontrol 1");
753 
754 	for (m = control; m->m_next != NULL; m = m->m_next)
755 		sballoc(sb, m);
756 	sballoc(sb, m);
757 	mlast = m;
758 	SBLINKRECORD(sb, control);
759 
760 	sb->sb_mbtail = mlast;
761 	SBLASTMBUFCHK(sb, "sbappendcontrol");
762 
763 	SBLASTRECORDCHK(sb, "sbappendcontrol 2");
764 
765 	return (1);
766 }
767 
768 /*
769  * Compress mbuf chain m into the socket
770  * buffer sb following mbuf n.  If n
771  * is null, the buffer is presumed empty.
772  */
773 void
sbcompress(struct sockbuf * sb,struct mbuf * m,struct mbuf * n)774 sbcompress(struct sockbuf *sb, struct mbuf *m, struct mbuf *n)
775 {
776 	int eor = 0;
777 	struct mbuf *o;
778 
779 	while (m) {
780 		eor |= m->m_flags & M_EOR;
781 		if (m->m_len == 0 &&
782 		    (eor == 0 ||
783 		    (((o = m->m_next) || (o = n)) &&
784 		    o->m_type == m->m_type))) {
785 			if (sb->sb_lastrecord == m)
786 				sb->sb_lastrecord = m->m_next;
787 			m = m_free(m);
788 			continue;
789 		}
790 		if (n && (n->m_flags & M_EOR) == 0 &&
791 		    /* M_TRAILINGSPACE() checks buffer writeability */
792 		    m->m_len <= MCLBYTES / 4 && /* XXX Don't copy too much */
793 		    m->m_len <= M_TRAILINGSPACE(n) &&
794 		    n->m_type == m->m_type) {
795 			bcopy(mtod(m, caddr_t), mtod(n, caddr_t) + n->m_len,
796 			    (unsigned)m->m_len);
797 			n->m_len += m->m_len;
798 			sb->sb_cc += m->m_len;
799 			m = m_free(m);
800 			continue;
801 		}
802 		if (n)
803 			n->m_next = m;
804 		else
805 			sb->sb_mb = m;
806 		sb->sb_mbtail = m;
807 		sballoc(sb, m);
808 		n = m;
809 		m->m_flags &= ~M_EOR;
810 		m = m->m_next;
811 		n->m_next = NULL;
812 	}
813 	if (eor) {
814 		if (n)
815 			n->m_flags |= eor;
816 		else
817 			printf("semi-panic: sbcompress");
818 	}
819 	SBLASTMBUFCHK(sb, __func__);
820 }
821 
822 /*
823  * Free all mbufs in a sockbuf.
824  * Check that all resources are reclaimed.
825  */
826 void
sbflush(struct sockbuf * sb)827 sbflush(struct sockbuf *sb)
828 {
829 
830 	KASSERT((sb->sb_flags & SB_LOCK) == 0);
831 
832 	while (sb->sb_mbcnt)
833 		sbdrop(sb, (int)sb->sb_cc);
834 
835 	KASSERT(sb->sb_cc == 0);
836 	KASSERT(sb->sb_mb == NULL);
837 	KASSERT(sb->sb_mbtail == NULL);
838 	KASSERT(sb->sb_lastrecord == NULL);
839 }
840 
841 /*
842  * Drop data from (the front of) a sockbuf.
843  */
844 void
sbdrop(struct sockbuf * sb,int len)845 sbdrop(struct sockbuf *sb, int len)
846 {
847 	struct mbuf *m, *mn;
848 	struct mbuf *next;
849 
850 	next = (m = sb->sb_mb) ? m->m_nextpkt : 0;
851 	while (len > 0) {
852 		if (m == NULL) {
853 			if (next == NULL)
854 				panic("sbdrop");
855 			m = next;
856 			next = m->m_nextpkt;
857 			continue;
858 		}
859 		if (m->m_len > len) {
860 			m->m_len -= len;
861 			m->m_data += len;
862 			sb->sb_cc -= len;
863 			break;
864 		}
865 		len -= m->m_len;
866 		sbfree(sb, m);
867 		MFREE(m, mn);
868 		m = mn;
869 	}
870 	while (m && m->m_len == 0) {
871 		sbfree(sb, m);
872 		MFREE(m, mn);
873 		m = mn;
874 	}
875 	if (m) {
876 		sb->sb_mb = m;
877 		m->m_nextpkt = next;
878 	} else
879 		sb->sb_mb = next;
880 	/*
881 	 * First part is an inline SB_EMPTY_FIXUP().  Second part
882 	 * makes sure sb_lastrecord is up-to-date if we dropped
883 	 * part of the last record.
884 	 */
885 	m = sb->sb_mb;
886 	if (m == NULL) {
887 		sb->sb_mbtail = NULL;
888 		sb->sb_lastrecord = NULL;
889 	} else if (m->m_nextpkt == NULL)
890 		sb->sb_lastrecord = m;
891 }
892 
893 /*
894  * Drop a record off the front of a sockbuf
895  * and move the next record to the front.
896  */
897 void
sbdroprecord(struct sockbuf * sb)898 sbdroprecord(struct sockbuf *sb)
899 {
900 	struct mbuf *m, *mn;
901 
902 	m = sb->sb_mb;
903 	if (m) {
904 		sb->sb_mb = m->m_nextpkt;
905 		do {
906 			sbfree(sb, m);
907 			MFREE(m, mn);
908 		} while ((m = mn) != NULL);
909 	}
910 	SB_EMPTY_FIXUP(sb);
911 }
912 
913 /*
914  * Create a "control" mbuf containing the specified data
915  * with the specified type for presentation on a socket buffer.
916  */
917 struct mbuf *
sbcreatecontrol(p,size,type,level)918 sbcreatecontrol(p, size, type, level)
919 	caddr_t p;
920 	register int size;
921 	int type, level;
922 {
923 	register struct cmsghdr *cp;
924 	struct mbuf *m;
925 
926 	if (CMSG_SPACE(size) > MCLBYTES) {
927 		printf("sbcreatecontrol: message too large %d\n", size);
928 		return NULL;
929 	}
930 
931 	if ((m = m_get(M_DONTWAIT, MT_CONTROL)) == NULL)
932 		return ((struct mbuf *) NULL);
933 	if (CMSG_SPACE(size) > MLEN) {
934 		MCLGET(m, M_DONTWAIT);
935 		if ((m->m_flags & M_EXT) == 0) {
936 			m_free(m);
937 			return NULL;
938 		}
939 	}
940 	cp = mtod(m, struct cmsghdr *);
941 	bcopy(p, CMSG_DATA(cp), size);
942 	m->m_len = CMSG_SPACE(size);
943 	cp->cmsg_len = CMSG_LEN(size);
944 	cp->cmsg_level = level;
945 	cp->cmsg_type = type;
946 	return (m);
947 }
948