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.16.2.5 1999/09/05 08:15:34 peter Exp $
35 */
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/proc.h>
41 #include <sys/file.h>
42 #include <sys/buf.h>
43 #include <sys/malloc.h>
44 #include <sys/mbuf.h>
45 #include <sys/protosw.h>
46 #include <sys/stat.h>
47 #include <sys/socket.h>
48 #include <sys/socketvar.h>
49 #include <sys/signalvar.h>
50 #include <sys/sysctl.h>
51
52 /*
53 * Primitive routines for operating on sockets and socket buffers
54 */
55
56 u_long sb_max = SB_MAX; /* XXX should be static */
57 SYSCTL_INT(_kern, KERN_MAXSOCKBUF, maxsockbuf, CTLFLAG_RW, &sb_max, 0, "")
58
59 static u_long sb_efficiency = 8; /* parameter for sbreserve() */
60 SYSCTL_INT(_kern, OID_AUTO, sockbuf_waste_factor, CTLFLAG_RW, &sb_efficiency,
61 0, "");
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(so)
95 register struct socket *so;
96 {
97
98 so->so_state &= ~(SS_ISCONNECTED|SS_ISDISCONNECTING);
99 so->so_state |= SS_ISCONNECTING;
100 }
101
102 void
103 soisconnected(so)
104 register struct socket *so;
105 {
106 register struct socket *head = so->so_head;
107
108 so->so_state &= ~(SS_ISCONNECTING|SS_ISDISCONNECTING|SS_ISCONFIRMING);
109 so->so_state |= SS_ISCONNECTED;
110 if (head && (so->so_state & SS_INCOMP)) {
111 TAILQ_REMOVE(&head->so_incomp, so, so_list);
112 head->so_incqlen--;
113 so->so_state &= ~SS_INCOMP;
114 TAILQ_INSERT_TAIL(&head->so_comp, so, so_list);
115 so->so_state |= SS_COMP;
116 sorwakeup(head);
117 wakeup_one(&head->so_timeo);
118 } else {
119 wakeup(&so->so_timeo);
120 sorwakeup(so);
121 sowwakeup(so);
122 }
123 }
124
125 void
126 soisdisconnecting(so)
127 register struct socket *so;
128 {
129
130 so->so_state &= ~SS_ISCONNECTING;
131 so->so_state |= (SS_ISDISCONNECTING|SS_CANTRCVMORE|SS_CANTSENDMORE);
132 wakeup((caddr_t)&so->so_timeo);
133 sowwakeup(so);
134 sorwakeup(so);
135 }
136
137 void
138 soisdisconnected(so)
139 register struct socket *so;
140 {
141
142 so->so_state &= ~(SS_ISCONNECTING|SS_ISCONNECTED|SS_ISDISCONNECTING);
143 so->so_state |= (SS_CANTRCVMORE|SS_CANTSENDMORE);
144 wakeup((caddr_t)&so->so_timeo);
145 sowwakeup(so);
146 sorwakeup(so);
147 }
148
149 /*
150 * Return a random connection that hasn't been serviced yet and
151 * is eligible for discard. There is a one in qlen chance that
152 * we will return a null, saying that there are no dropable
153 * requests. In this case, the protocol specific code should drop
154 * the new request. This insures fairness.
155 *
156 * This may be used in conjunction with protocol specific queue
157 * congestion routines.
158 */
159 struct socket *
160 sodropablereq(head)
161 register struct socket *head;
162 {
163 register struct socket *so;
164 unsigned int i, j, qlen;
165
166 static int rnd;
167 static long old_mono_secs;
168 static unsigned int cur_cnt, old_cnt;
169
170 if ((i = (mono_time.tv_sec - old_mono_secs)) != 0) {
171 old_mono_secs = mono_time.tv_sec;
172 old_cnt = cur_cnt / i;
173 cur_cnt = 0;
174 }
175
176 so = TAILQ_FIRST(&head->so_incomp);
177 if (!so)
178 return (so);
179
180 qlen = head->so_incqlen;
181 if (++cur_cnt > qlen || old_cnt > qlen) {
182 rnd = (314159 * rnd + 66329) & 0xffff;
183 j = ((qlen + 1) * rnd) >> 16;
184
185 while (j-- && so)
186 so = TAILQ_NEXT(so, so_list);
187 }
188
189 return (so);
190 }
191
192 /*
193 * When an attempt at a new connection is noted on a socket
194 * which accepts connections, sonewconn is called. If the
195 * connection is possible (subject to space constraints, etc.)
196 * then we allocate a new structure, propoerly linked into the
197 * data structure of the original socket, and return this.
198 * Connstatus may be 0, or SO_ISCONFIRMING, or SO_ISCONNECTED.
199 *
200 * Currently, sonewconn() is defined as sonewconn1() in socketvar.h
201 * to catch calls that are missing the (new) second parameter.
202 */
203 struct socket *
204 sonewconn1(head, connstatus)
205 register struct socket *head;
206 int connstatus;
207 {
208 register struct socket *so;
209
210 if (head->so_qlen > 3 * head->so_qlimit / 2)
211 return ((struct socket *)0);
212 MALLOC(so, struct socket *, sizeof(*so), M_SOCKET, M_DONTWAIT);
213 if (so == NULL)
214 return ((struct socket *)0);
215 bzero((caddr_t)so, sizeof(*so));
216 so->so_head = head;
217 so->so_type = head->so_type;
218 so->so_options = head->so_options &~ SO_ACCEPTCONN;
219 so->so_linger = head->so_linger;
220 so->so_state = head->so_state | SS_NOFDREF;
221 so->so_proto = head->so_proto;
222 so->so_timeo = head->so_timeo;
223 so->so_pgid = head->so_pgid;
224 so->so_uid = head->so_uid;
225 (void) soreserve(so, head->so_snd.sb_hiwat, head->so_rcv.sb_hiwat);
226 if (connstatus) {
227 TAILQ_INSERT_TAIL(&head->so_comp, so, so_list);
228 so->so_state |= SS_COMP;
229 } else {
230 TAILQ_INSERT_TAIL(&head->so_incomp, so, so_list);
231 so->so_state |= SS_INCOMP;
232 head->so_incqlen++;
233 }
234 head->so_qlen++;
235 if ((*so->so_proto->pr_usrreqs->pru_attach)(so, 0)) {
236 if (so->so_state & SS_COMP) {
237 TAILQ_REMOVE(&head->so_comp, so, so_list);
238 } else {
239 TAILQ_REMOVE(&head->so_incomp, so, so_list);
240 head->so_incqlen--;
241 }
242 head->so_qlen--;
243 (void) free((caddr_t)so, M_SOCKET);
244 return ((struct socket *)0);
245 }
246 if (connstatus) {
247 sorwakeup(head);
248 wakeup((caddr_t)&head->so_timeo);
249 so->so_state |= connstatus;
250 }
251 return (so);
252 }
253
254 /*
255 * Socantsendmore indicates that no more data will be sent on the
256 * socket; it would normally be applied to a socket when the user
257 * informs the system that no more data is to be sent, by the protocol
258 * code (in case PRU_SHUTDOWN). Socantrcvmore indicates that no more data
259 * will be received, and will normally be applied to the socket by a
260 * protocol when it detects that the peer will send no more data.
261 * Data queued for reading in the socket may yet be read.
262 */
263
264 void
265 socantsendmore(so)
266 struct socket *so;
267 {
268
269 so->so_state |= SS_CANTSENDMORE;
270 sowwakeup(so);
271 }
272
273 void
274 socantrcvmore(so)
275 struct socket *so;
276 {
277
278 so->so_state |= SS_CANTRCVMORE;
279 sorwakeup(so);
280 }
281
282 /*
283 * Wait for data to arrive at/drain from a socket buffer.
284 */
285 int
286 sbwait(sb)
287 struct sockbuf *sb;
288 {
289
290 sb->sb_flags |= SB_WAIT;
291 return (tsleep((caddr_t)&sb->sb_cc,
292 (sb->sb_flags & SB_NOINTR) ? PSOCK : PSOCK | PCATCH, "sbwait",
293 sb->sb_timeo));
294 }
295
296 /*
297 * Lock a sockbuf already known to be locked;
298 * return any error returned from sleep (EINTR).
299 */
300 int
301 sb_lock(sb)
302 register struct sockbuf *sb;
303 {
304 int error;
305
306 while (sb->sb_flags & SB_LOCK) {
307 sb->sb_flags |= SB_WANT;
308 error = tsleep((caddr_t)&sb->sb_flags,
309 (sb->sb_flags & SB_NOINTR) ? PSOCK : PSOCK|PCATCH,
310 "sblock", 0);
311 if (error)
312 return (error);
313 }
314 sb->sb_flags |= SB_LOCK;
315 return (0);
316 }
317
318 /*
319 * Wakeup processes waiting on a socket buffer.
320 * Do asynchronous notification via SIGIO
321 * if the socket has the SS_ASYNC flag set.
322 */
323 void
324 sowakeup(so, sb)
325 register struct socket *so;
326 register struct sockbuf *sb;
327 {
328 struct proc *p;
329
330 selwakeup(&sb->sb_sel);
331 sb->sb_flags &= ~SB_SEL;
332 if (sb->sb_flags & SB_WAIT) {
333 sb->sb_flags &= ~SB_WAIT;
334 wakeup((caddr_t)&sb->sb_cc);
335 }
336 if (so->so_state & SS_ASYNC) {
337 if (so->so_pgid < 0)
338 gsignal(-so->so_pgid, SIGIO);
339 else if (so->so_pgid > 0 && (p = pfind(so->so_pgid)) != 0)
340 psignal(p, SIGIO);
341 }
342 }
343
344 /*
345 * Socket buffer (struct sockbuf) utility routines.
346 *
347 * Each socket contains two socket buffers: one for sending data and
348 * one for receiving data. Each buffer contains a queue of mbufs,
349 * information about the number of mbufs and amount of data in the
350 * queue, and other fields allowing select() statements and notification
351 * on data availability to be implemented.
352 *
353 * Data stored in a socket buffer is maintained as a list of records.
354 * Each record is a list of mbufs chained together with the m_next
355 * field. Records are chained together with the m_nextpkt field. The upper
356 * level routine soreceive() expects the following conventions to be
357 * observed when placing information in the receive buffer:
358 *
359 * 1. If the protocol requires each message be preceded by the sender's
360 * name, then a record containing that name must be present before
361 * any associated data (mbuf's must be of type MT_SONAME).
362 * 2. If the protocol supports the exchange of ``access rights'' (really
363 * just additional data associated with the message), and there are
364 * ``rights'' to be received, then a record containing this data
365 * should be present (mbuf's must be of type MT_RIGHTS).
366 * 3. If a name or rights record exists, then it must be followed by
367 * a data record, perhaps of zero length.
368 *
369 * Before using a new socket structure it is first necessary to reserve
370 * buffer space to the socket, by calling sbreserve(). This should commit
371 * some of the available buffer space in the system buffer pool for the
372 * socket (currently, it does nothing but enforce limits). The space
373 * should be released by calling sbrelease() when the socket is destroyed.
374 */
375
376 int
377 soreserve(so, sndcc, rcvcc)
378 register struct socket *so;
379 u_long sndcc, rcvcc;
380 {
381
382 if (sbreserve(&so->so_snd, sndcc) == 0)
383 goto bad;
384 if (sbreserve(&so->so_rcv, rcvcc) == 0)
385 goto bad2;
386 if (so->so_rcv.sb_lowat == 0)
387 so->so_rcv.sb_lowat = 1;
388 if (so->so_snd.sb_lowat == 0)
389 so->so_snd.sb_lowat = MCLBYTES;
390 if (so->so_snd.sb_lowat > so->so_snd.sb_hiwat)
391 so->so_snd.sb_lowat = so->so_snd.sb_hiwat;
392 return (0);
393 bad2:
394 sbrelease(&so->so_snd);
395 bad:
396 return (ENOBUFS);
397 }
398
399 /*
400 * Allot mbufs to a sockbuf.
401 * Attempt to scale mbmax so that mbcnt doesn't become limiting
402 * if buffering efficiency is near the normal case.
403 */
404 int
405 sbreserve(sb, cc)
406 struct sockbuf *sb;
407 u_long cc;
408 {
409
410 if (cc > sb_max * MCLBYTES / (MSIZE + MCLBYTES))
411 return (0);
412 sb->sb_hiwat = cc;
413 sb->sb_mbmax = min(cc * sb_efficiency, sb_max);
414 if (sb->sb_lowat > sb->sb_hiwat)
415 sb->sb_lowat = sb->sb_hiwat;
416 return (1);
417 }
418
419 /*
420 * Free mbufs held by a socket, and reserved mbuf space.
421 */
422 void
423 sbrelease(sb)
424 struct sockbuf *sb;
425 {
426
427 sbflush(sb);
428 sb->sb_hiwat = sb->sb_mbmax = 0;
429 }
430
431 /*
432 * Routines to add and remove
433 * data from an mbuf queue.
434 *
435 * The routines sbappend() or sbappendrecord() are normally called to
436 * append new mbufs to a socket buffer, after checking that adequate
437 * space is available, comparing the function sbspace() with the amount
438 * of data to be added. sbappendrecord() differs from sbappend() in
439 * that data supplied is treated as the beginning of a new record.
440 * To place a sender's address, optional access rights, and data in a
441 * socket receive buffer, sbappendaddr() should be used. To place
442 * access rights and data in a socket receive buffer, sbappendrights()
443 * should be used. In either case, the new data begins a new record.
444 * Note that unlike sbappend() and sbappendrecord(), these routines check
445 * for the caller that there will be enough space to store the data.
446 * Each fails if there is not enough space, or if it cannot find mbufs
447 * to store additional information in.
448 *
449 * Reliable protocols may use the socket send buffer to hold data
450 * awaiting acknowledgement. Data is normally copied from a socket
451 * send buffer in a protocol with m_copy for output to a peer,
452 * and then removing the data from the socket buffer with sbdrop()
453 * or sbdroprecord() when the data is acknowledged by the peer.
454 */
455
456 /*
457 * Append mbuf chain m to the last record in the
458 * socket buffer sb. The additional space associated
459 * the mbuf chain is recorded in sb. Empty mbufs are
460 * discarded and mbufs are compacted where possible.
461 */
462 void
463 sbappend(sb, m)
464 struct sockbuf *sb;
465 struct mbuf *m;
466 {
467 register struct mbuf *n;
468
469 if (m == 0)
470 return;
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 sbappendrecord(sb, m); /* XXXXXX!!!! */
478 return;
479 }
480 } while (n->m_next && (n = n->m_next));
481 }
482 sbcompress(sb, m, n);
483 }
484
485 #ifdef SOCKBUF_DEBUG
486 void
487 sbcheck(sb)
488 register struct sockbuf *sb;
489 {
490 register struct mbuf *m;
491 register int len = 0, mbcnt = 0;
492
493 for (m = sb->sb_mb; m; m = m->m_next) {
494 len += m->m_len;
495 mbcnt += MSIZE;
496 if (m->m_flags & M_EXT) /*XXX*/ /* pretty sure this is bogus */
497 mbcnt += m->m_ext.ext_size;
498 if (m->m_nextpkt)
499 panic("sbcheck nextpkt");
500 }
501 if (len != sb->sb_cc || mbcnt != sb->sb_mbcnt) {
502 printf("cc %d != %d || mbcnt %d != %d\n", len, sb->sb_cc,
503 mbcnt, sb->sb_mbcnt);
504 panic("sbcheck");
505 }
506 }
507 #endif
508
509 /*
510 * As above, except the mbuf chain
511 * begins a new record.
512 */
513 void
514 sbappendrecord(sb, m0)
515 register struct sockbuf *sb;
516 register struct mbuf *m0;
517 {
518 register struct mbuf *m;
519
520 if (m0 == 0)
521 return;
522 m = sb->sb_mb;
523 if (m)
524 while (m->m_nextpkt)
525 m = m->m_nextpkt;
526 /*
527 * Put the first mbuf on the queue.
528 * Note this permits zero length records.
529 */
530 sballoc(sb, m0);
531 if (m)
532 m->m_nextpkt = m0;
533 else
534 sb->sb_mb = m0;
535 m = m0->m_next;
536 m0->m_next = 0;
537 if (m && (m0->m_flags & M_EOR)) {
538 m0->m_flags &= ~M_EOR;
539 m->m_flags |= M_EOR;
540 }
541 sbcompress(sb, m, m0);
542 }
543
544 /*
545 * As above except that OOB data
546 * is inserted at the beginning of the sockbuf,
547 * but after any other OOB data.
548 */
549 void
550 sbinsertoob(sb, m0)
551 register struct sockbuf *sb;
552 register struct mbuf *m0;
553 {
554 register struct mbuf *m;
555 register struct mbuf **mp;
556
557 if (m0 == 0)
558 return;
559 for (mp = &sb->sb_mb; *mp ; mp = &((*mp)->m_nextpkt)) {
560 m = *mp;
561 again:
562 switch (m->m_type) {
563
564 case MT_OOBDATA:
565 continue; /* WANT next train */
566
567 case MT_CONTROL:
568 m = m->m_next;
569 if (m)
570 goto again; /* inspect THIS train further */
571 }
572 break;
573 }
574 /*
575 * Put the first mbuf on the queue.
576 * Note this permits zero length records.
577 */
578 sballoc(sb, m0);
579 m0->m_nextpkt = *mp;
580 *mp = m0;
581 m = m0->m_next;
582 m0->m_next = 0;
583 if (m && (m0->m_flags & M_EOR)) {
584 m0->m_flags &= ~M_EOR;
585 m->m_flags |= M_EOR;
586 }
587 sbcompress(sb, m, m0);
588 }
589
590 /*
591 * Append address and data, and optionally, control (ancillary) data
592 * to the receive queue of a socket. If present,
593 * m0 must include a packet header with total length.
594 * Returns 0 if no space in sockbuf or insufficient mbufs.
595 */
596 int
597 sbappendaddr(sb, asa, m0, control)
598 register struct sockbuf *sb;
599 struct sockaddr *asa;
600 struct mbuf *m0, *control;
601 {
602 register struct mbuf *m, *n;
603 int space = asa->sa_len;
604
605 if (m0 && (m0->m_flags & M_PKTHDR) == 0)
606 panic("sbappendaddr");
607 if (m0)
608 space += m0->m_pkthdr.len;
609 for (n = control; n; n = n->m_next) {
610 space += n->m_len;
611 if (n->m_next == 0) /* keep pointer to last control buf */
612 break;
613 }
614 if (space > sbspace(sb))
615 return (0);
616 if (asa->sa_len > MLEN)
617 return (0);
618 MGET(m, M_DONTWAIT, MT_SONAME);
619 if (m == 0)
620 return (0);
621 m->m_len = asa->sa_len;
622 bcopy((caddr_t)asa, mtod(m, caddr_t), asa->sa_len);
623 if (n)
624 n->m_next = m0; /* concatenate data to control */
625 else
626 control = m0;
627 m->m_next = control;
628 for (n = m; n; n = n->m_next)
629 sballoc(sb, n);
630 n = sb->sb_mb;
631 if (n) {
632 while (n->m_nextpkt)
633 n = n->m_nextpkt;
634 n->m_nextpkt = m;
635 } else
636 sb->sb_mb = m;
637 return (1);
638 }
639
640 int
641 sbappendcontrol(sb, m0, control)
642 struct sockbuf *sb;
643 struct mbuf *control, *m0;
644 {
645 register struct mbuf *m, *n;
646 int space = 0;
647
648 if (control == 0)
649 panic("sbappendcontrol");
650 for (m = control; ; m = m->m_next) {
651 space += m->m_len;
652 if (m->m_next == 0)
653 break;
654 }
655 n = m; /* save pointer to last control buffer */
656 for (m = m0; m; m = m->m_next)
657 space += m->m_len;
658 if (space > sbspace(sb))
659 return (0);
660 n->m_next = m0; /* concatenate data to control */
661 for (m = control; m; m = m->m_next)
662 sballoc(sb, m);
663 n = sb->sb_mb;
664 if (n) {
665 while (n->m_nextpkt)
666 n = n->m_nextpkt;
667 n->m_nextpkt = control;
668 } else
669 sb->sb_mb = control;
670 return (1);
671 }
672
673 /*
674 * Compress mbuf chain m into the socket
675 * buffer sb following mbuf n. If n
676 * is null, the buffer is presumed empty.
677 */
678 void
679 sbcompress(sb, m, n)
680 register struct sockbuf *sb;
681 register struct mbuf *m, *n;
682 {
683 register int eor = 0;
684 register struct mbuf *o;
685
686 while (m) {
687 eor |= m->m_flags & M_EOR;
688 if (m->m_len == 0 &&
689 (eor == 0 ||
690 (((o = m->m_next) || (o = n)) &&
691 o->m_type == m->m_type))) {
692 m = m_free(m);
693 continue;
694 }
695 if (n && (n->m_flags & (M_EXT | M_EOR)) == 0 &&
696 (n->m_data + n->m_len + m->m_len) < &n->m_dat[MLEN] &&
697 n->m_type == m->m_type) {
698 bcopy(mtod(m, caddr_t), mtod(n, caddr_t) + n->m_len,
699 (unsigned)m->m_len);
700 n->m_len += m->m_len;
701 sb->sb_cc += m->m_len;
702 m = m_free(m);
703 continue;
704 }
705 if (n)
706 n->m_next = m;
707 else
708 sb->sb_mb = m;
709 sballoc(sb, m);
710 n = m;
711 m->m_flags &= ~M_EOR;
712 m = m->m_next;
713 n->m_next = 0;
714 }
715 if (eor) {
716 if (n)
717 n->m_flags |= eor;
718 else
719 printf("semi-panic: sbcompress\n");
720 }
721 }
722
723 /*
724 * Free all mbufs in a sockbuf.
725 * Check that all resources are reclaimed.
726 */
727 void
728 sbflush(sb)
729 register struct sockbuf *sb;
730 {
731
732 if (sb->sb_flags & SB_LOCK)
733 panic("sbflush");
734 while (sb->sb_mbcnt)
735 sbdrop(sb, (int)sb->sb_cc);
736 if (sb->sb_cc || sb->sb_mb)
737 panic("sbflush 2");
738 }
739
740 /*
741 * Drop data from (the front of) a sockbuf.
742 */
743 void
744 sbdrop(sb, len)
745 register struct sockbuf *sb;
746 register int len;
747 {
748 register struct mbuf *m, *mn;
749 struct mbuf *next;
750
751 next = (m = sb->sb_mb) ? m->m_nextpkt : 0;
752 while (len > 0) {
753 if (m == 0) {
754 if (next == 0)
755 panic("sbdrop");
756 m = next;
757 next = m->m_nextpkt;
758 continue;
759 }
760 if (m->m_len > len) {
761 m->m_len -= len;
762 m->m_data += len;
763 sb->sb_cc -= len;
764 break;
765 }
766 len -= m->m_len;
767 sbfree(sb, m);
768 MFREE(m, mn);
769 m = mn;
770 }
771 while (m && m->m_len == 0) {
772 sbfree(sb, m);
773 MFREE(m, mn);
774 m = mn;
775 }
776 if (m) {
777 sb->sb_mb = m;
778 m->m_nextpkt = next;
779 } else
780 sb->sb_mb = next;
781 }
782
783 /*
784 * Drop a record off the front of a sockbuf
785 * and move the next record to the front.
786 */
787 void
788 sbdroprecord(sb)
789 register struct sockbuf *sb;
790 {
791 register struct mbuf *m, *mn;
792
793 m = sb->sb_mb;
794 if (m) {
795 sb->sb_mb = m->m_nextpkt;
796 do {
797 sbfree(sb, m);
798 MFREE(m, mn);
799 m = mn;
800 } while (m);
801 }
802 }
803
804 /*
805 * Create a "control" mbuf containing the specified data
806 * with the specified type for presentation on a socket buffer.
807 */
808 struct mbuf *
809 sbcreatecontrol(p, size, type, level)
810 caddr_t p;
811 register int size;
812 int type, level;
813 {
814 register struct cmsghdr *cp;
815 struct mbuf *m;
816
817 if ((m = m_get(M_DONTWAIT, MT_CONTROL)) == NULL)
818 return ((struct mbuf *) NULL);
819 cp = mtod(m, struct cmsghdr *);
820 /* XXX check size? */
821 (void)memcpy(CMSG_DATA(cp), p, size);
822 size += sizeof(*cp);
823 m->m_len = size;
824 cp->cmsg_len = size;
825 cp->cmsg_level = level;
826 cp->cmsg_type = type;
827 return (m);
828 }
829
830 #ifdef PRU_OLDSTYLE
831 /*
832 * The following routines mediate between the old-style `pr_usrreq'
833 * protocol implementations and the new-style `struct pr_usrreqs'
834 * calling convention.
835 */
836
837 /* syntactic sugar */
838 #define nomb (struct mbuf *)0
839
840 static int
841 old_abort(struct socket *so)
842 {
843 return so->so_proto->pr_ousrreq(so, PRU_ABORT, nomb, nomb, nomb);
844 }
845
846 static int
847 old_accept(struct socket *so, struct mbuf *nam)
848 {
849 return so->so_proto->pr_ousrreq(so, PRU_ACCEPT, nomb, nam, nomb);
850 }
851
852 static int
853 old_attach(struct socket *so, int proto)
854 {
855 return so->so_proto->pr_ousrreq(so, PRU_ATTACH, nomb,
856 (struct mbuf *)proto, /* XXX */
857 nomb);
858 }
859
860 static int
861 old_bind(struct socket *so, struct mbuf *nam)
862 {
863 return so->so_proto->pr_ousrreq(so, PRU_BIND, nomb, nam, nomb);
864 }
865
866 static int
867 old_connect(struct socket *so, struct mbuf *nam)
868 {
869 return so->so_proto->pr_ousrreq(so, PRU_CONNECT, nomb, nam, nomb);
870 }
871
872 static int
873 old_connect2(struct socket *so1, struct socket *so2)
874 {
875 return so1->so_proto->pr_ousrreq(so1, PRU_CONNECT2, nomb,
876 (struct mbuf *)so2, nomb);
877 }
878
879 static int
880 old_control(struct socket *so, int cmd, caddr_t data, struct ifnet *ifp)
881 {
882 return so->so_proto->pr_ousrreq(so, PRU_CONTROL, (struct mbuf *)cmd,
883 (struct mbuf *)data,
884 (struct mbuf *)ifp);
885 }
886
887 static int
888 old_detach(struct socket *so)
889 {
890 return so->so_proto->pr_ousrreq(so, PRU_DETACH, nomb, nomb, nomb);
891 }
892
893 static int
894 old_disconnect(struct socket *so)
895 {
896 return so->so_proto->pr_ousrreq(so, PRU_DISCONNECT, nomb, nomb, nomb);
897 }
898
899 static int
900 old_listen(struct socket *so)
901 {
902 return so->so_proto->pr_ousrreq(so, PRU_LISTEN, nomb, nomb, nomb);
903 }
904
905 static int
906 old_peeraddr(struct socket *so, struct mbuf *nam)
907 {
908 return so->so_proto->pr_ousrreq(so, PRU_PEERADDR, nomb, nam, nomb);
909 }
910
911 static int
912 old_rcvd(struct socket *so, int flags)
913 {
914 return so->so_proto->pr_ousrreq(so, PRU_RCVD, nomb,
915 (struct mbuf *)flags, /* XXX */
916 nomb);
917 }
918
919 static int
920 old_rcvoob(struct socket *so, struct mbuf *m, int flags)
921 {
922 return so->so_proto->pr_ousrreq(so, PRU_RCVOOB, m,
923 (struct mbuf *)flags, /* XXX */
924 nomb);
925 }
926
927 static int
928 old_send(struct socket *so, int flags, struct mbuf *m, struct mbuf *addr,
929 struct mbuf *control)
930 {
931 int req;
932
933 if (flags & PRUS_OOB) {
934 req = PRU_SENDOOB;
935 } else if(flags & PRUS_EOF) {
936 req = PRU_SEND_EOF;
937 } else {
938 req = PRU_SEND;
939 }
940 return so->so_proto->pr_ousrreq(so, req, m, addr, control);
941 }
942
943 static int
944 old_sense(struct socket *so, struct stat *sb)
945 {
946 return so->so_proto->pr_ousrreq(so, PRU_SENSE, (struct mbuf *)sb,
947 nomb, nomb);
948 }
949
950 static int
951 old_shutdown(struct socket *so)
952 {
953 return so->so_proto->pr_ousrreq(so, PRU_SHUTDOWN, nomb, nomb, nomb);
954 }
955
956 static int
957 old_sockaddr(struct socket *so, struct mbuf *nam)
958 {
959 return so->so_proto->pr_ousrreq(so, PRU_SOCKADDR, nomb, nam, nomb);
960 }
961
962 struct pr_usrreqs pru_oldstyle = {
963 old_abort, old_accept, old_attach, old_bind, old_connect,
964 old_connect2, old_control, old_detach, old_disconnect,
965 old_listen, old_peeraddr, old_rcvd, old_rcvoob, old_send,
966 old_sense, old_shutdown, old_sockaddr
967 };
968
969 #endif /* PRU_OLDSTYLE */
970
971 /*
972 * Some routines that return EOPNOTSUPP for entry points that are not
973 * supported by a protocol. Fill in as needed.
974 */
975 int
976 pru_accept_notsupp(struct socket *so, struct mbuf *nam)
977 {
978 return EOPNOTSUPP;
979 }
980
981 int
982 pru_connect2_notsupp(struct socket *so1, struct socket *so2)
983 {
984 return EOPNOTSUPP;
985 }
986
987 int
988 pru_control_notsupp(struct socket *so, int cmd, caddr_t data,
989 struct ifnet *ifp)
990 {
991 return EOPNOTSUPP;
992 }
993
994 int
995 pru_listen_notsupp(struct socket *so)
996 {
997 return EOPNOTSUPP;
998 }
999
1000 int
1001 pru_rcvd_notsupp(struct socket *so, int flags)
1002 {
1003 return EOPNOTSUPP;
1004 }
1005
1006 int
1007 pru_rcvoob_notsupp(struct socket *so, struct mbuf *m, int flags)
1008 {
1009 return EOPNOTSUPP;
1010 }
1011
1012 /*
1013 * This isn't really a ``null'' operation, but it's the default one
1014 * and doesn't do anything destructive.
1015 */
1016 int
1017 pru_sense_null(struct socket *so, struct stat *sb)
1018 {
1019 sb->st_blksize = so->so_snd.sb_hiwat;
1020 return 0;
1021 }
1022
Cache object: eed02acac2bce390c2d6c46848994290
|