uipc_socket2.c 23 KB

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