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