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