uipc_socket.c 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904
  1. /* $OpenBSD: uipc_socket.c,v 1.141 2015/07/08 07:21:50 mpi Exp $ */
  2. /* $NetBSD: uipc_socket.c,v 1.21 1996/02/04 02:17:52 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_socket.c 8.3 (Berkeley) 4/15/94
  32. */
  33. #include <sys/param.h>
  34. #include <sys/systm.h>
  35. #include <sys/proc.h>
  36. #include <sys/file.h>
  37. #include <sys/filedesc.h>
  38. #include <sys/malloc.h>
  39. #include <sys/mbuf.h>
  40. #include <sys/domain.h>
  41. #include <sys/kernel.h>
  42. #include <sys/event.h>
  43. #include <sys/protosw.h>
  44. #include <sys/socket.h>
  45. #include <sys/unpcb.h>
  46. #include <sys/socketvar.h>
  47. #include <sys/signalvar.h>
  48. #include <net/if.h>
  49. #include <sys/pool.h>
  50. void sbsync(struct sockbuf *, struct mbuf *);
  51. int sosplice(struct socket *, int, off_t, struct timeval *);
  52. void sounsplice(struct socket *, struct socket *, int);
  53. void soidle(void *);
  54. int somove(struct socket *, int);
  55. void filt_sordetach(struct knote *kn);
  56. int filt_soread(struct knote *kn, long hint);
  57. void filt_sowdetach(struct knote *kn);
  58. int filt_sowrite(struct knote *kn, long hint);
  59. int filt_solisten(struct knote *kn, long hint);
  60. struct filterops solisten_filtops =
  61. { 1, NULL, filt_sordetach, filt_solisten };
  62. struct filterops soread_filtops =
  63. { 1, NULL, filt_sordetach, filt_soread };
  64. struct filterops sowrite_filtops =
  65. { 1, NULL, filt_sowdetach, filt_sowrite };
  66. #ifndef SOMINCONN
  67. #define SOMINCONN 80
  68. #endif /* SOMINCONN */
  69. int somaxconn = SOMAXCONN;
  70. int sominconn = SOMINCONN;
  71. struct pool socket_pool;
  72. #ifdef SOCKET_SPLICE
  73. struct pool sosplice_pool;
  74. #endif
  75. void
  76. soinit(void)
  77. {
  78. pool_init(&socket_pool, sizeof(struct socket), 0, 0, 0, "sockpl", NULL);
  79. #ifdef SOCKET_SPLICE
  80. pool_init(&sosplice_pool, sizeof(struct sosplice), 0, 0, 0, "sosppl",
  81. NULL);
  82. #endif
  83. }
  84. /*
  85. * Socket operation routines.
  86. * These routines are called by the routines in
  87. * sys_socket.c or from a system process, and
  88. * implement the semantics of socket operations by
  89. * switching out to the protocol specific routines.
  90. */
  91. /*ARGSUSED*/
  92. int
  93. socreate(int dom, struct socket **aso, int type, int proto)
  94. {
  95. struct proc *p = curproc; /* XXX */
  96. struct protosw *prp;
  97. struct socket *so;
  98. int error, s;
  99. if (proto)
  100. prp = pffindproto(dom, proto, type);
  101. else
  102. prp = pffindtype(dom, type);
  103. if (prp == NULL || prp->pr_usrreq == 0)
  104. return (EPROTONOSUPPORT);
  105. if (prp->pr_type != type)
  106. return (EPROTOTYPE);
  107. s = splsoftnet();
  108. so = pool_get(&socket_pool, PR_WAITOK | PR_ZERO);
  109. TAILQ_INIT(&so->so_q0);
  110. TAILQ_INIT(&so->so_q);
  111. so->so_type = type;
  112. if (suser(p, 0) == 0)
  113. so->so_state = SS_PRIV;
  114. so->so_ruid = p->p_ucred->cr_ruid;
  115. so->so_euid = p->p_ucred->cr_uid;
  116. so->so_rgid = p->p_ucred->cr_rgid;
  117. so->so_egid = p->p_ucred->cr_gid;
  118. so->so_cpid = p->p_p->ps_pid;
  119. so->so_proto = prp;
  120. error = (*prp->pr_usrreq)(so, PRU_ATTACH, NULL,
  121. (struct mbuf *)(long)proto, NULL, p);
  122. if (error) {
  123. so->so_state |= SS_NOFDREF;
  124. sofree(so);
  125. splx(s);
  126. return (error);
  127. }
  128. splx(s);
  129. *aso = so;
  130. return (0);
  131. }
  132. int
  133. sobind(struct socket *so, struct mbuf *nam, struct proc *p)
  134. {
  135. int s = splsoftnet();
  136. int error;
  137. error = (*so->so_proto->pr_usrreq)(so, PRU_BIND, NULL, nam, NULL, p);
  138. splx(s);
  139. return (error);
  140. }
  141. int
  142. solisten(struct socket *so, int backlog)
  143. {
  144. int s, error;
  145. if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING|SS_ISDISCONNECTING))
  146. return (EOPNOTSUPP);
  147. #ifdef SOCKET_SPLICE
  148. if (isspliced(so) || issplicedback(so))
  149. return (EOPNOTSUPP);
  150. #endif /* SOCKET_SPLICE */
  151. s = splsoftnet();
  152. error = (*so->so_proto->pr_usrreq)(so, PRU_LISTEN, NULL, NULL, NULL,
  153. curproc);
  154. if (error) {
  155. splx(s);
  156. return (error);
  157. }
  158. if (TAILQ_FIRST(&so->so_q) == NULL)
  159. so->so_options |= SO_ACCEPTCONN;
  160. if (backlog < 0 || backlog > somaxconn)
  161. backlog = somaxconn;
  162. if (backlog < sominconn)
  163. backlog = sominconn;
  164. so->so_qlimit = backlog;
  165. splx(s);
  166. return (0);
  167. }
  168. /*
  169. * Must be called at splsoftnet()
  170. */
  171. void
  172. sofree(struct socket *so)
  173. {
  174. splsoftassert(IPL_SOFTNET);
  175. if (so->so_pcb || (so->so_state & SS_NOFDREF) == 0)
  176. return;
  177. if (so->so_head) {
  178. /*
  179. * We must not decommission a socket that's on the accept(2)
  180. * queue. If we do, then accept(2) may hang after select(2)
  181. * indicated that the listening socket was ready.
  182. */
  183. if (!soqremque(so, 0))
  184. return;
  185. }
  186. #ifdef SOCKET_SPLICE
  187. if (so->so_sp) {
  188. if (issplicedback(so))
  189. sounsplice(so->so_sp->ssp_soback, so,
  190. so->so_sp->ssp_soback != so);
  191. if (isspliced(so))
  192. sounsplice(so, so->so_sp->ssp_socket, 0);
  193. pool_put(&sosplice_pool, so->so_sp);
  194. so->so_sp = NULL;
  195. }
  196. #endif /* SOCKET_SPLICE */
  197. sbrelease(&so->so_snd);
  198. sorflush(so);
  199. pool_put(&socket_pool, so);
  200. }
  201. /*
  202. * Close a socket on last file table reference removal.
  203. * Initiate disconnect if connected.
  204. * Free socket when disconnect complete.
  205. */
  206. int
  207. soclose(struct socket *so)
  208. {
  209. struct socket *so2;
  210. int s = splsoftnet(); /* conservative */
  211. int error = 0;
  212. if (so->so_options & SO_ACCEPTCONN) {
  213. while ((so2 = TAILQ_FIRST(&so->so_q0)) != NULL) {
  214. (void) soqremque(so2, 0);
  215. (void) soabort(so2);
  216. }
  217. while ((so2 = TAILQ_FIRST(&so->so_q)) != NULL) {
  218. (void) soqremque(so2, 1);
  219. (void) soabort(so2);
  220. }
  221. }
  222. if (so->so_pcb == 0)
  223. goto discard;
  224. if (so->so_state & SS_ISCONNECTED) {
  225. if ((so->so_state & SS_ISDISCONNECTING) == 0) {
  226. error = sodisconnect(so);
  227. if (error)
  228. goto drop;
  229. }
  230. if (so->so_options & SO_LINGER) {
  231. if ((so->so_state & SS_ISDISCONNECTING) &&
  232. (so->so_state & SS_NBIO))
  233. goto drop;
  234. while (so->so_state & SS_ISCONNECTED) {
  235. error = tsleep(&so->so_timeo,
  236. PSOCK | PCATCH, "netcls",
  237. so->so_linger * hz);
  238. if (error)
  239. break;
  240. }
  241. }
  242. }
  243. drop:
  244. if (so->so_pcb) {
  245. int error2 = (*so->so_proto->pr_usrreq)(so, PRU_DETACH, NULL,
  246. NULL, NULL, curproc);
  247. if (error == 0)
  248. error = error2;
  249. }
  250. discard:
  251. if (so->so_state & SS_NOFDREF)
  252. panic("soclose: NOFDREF");
  253. so->so_state |= SS_NOFDREF;
  254. sofree(so);
  255. splx(s);
  256. return (error);
  257. }
  258. /*
  259. * Must be called at splsoftnet.
  260. */
  261. int
  262. soabort(struct socket *so)
  263. {
  264. splsoftassert(IPL_SOFTNET);
  265. return (*so->so_proto->pr_usrreq)(so, PRU_ABORT, NULL, NULL, NULL,
  266. curproc);
  267. }
  268. int
  269. soaccept(struct socket *so, struct mbuf *nam)
  270. {
  271. int s = splsoftnet();
  272. int error = 0;
  273. if ((so->so_state & SS_NOFDREF) == 0)
  274. panic("soaccept: !NOFDREF");
  275. so->so_state &= ~SS_NOFDREF;
  276. if ((so->so_state & SS_ISDISCONNECTED) == 0 ||
  277. (so->so_proto->pr_flags & PR_ABRTACPTDIS) == 0)
  278. error = (*so->so_proto->pr_usrreq)(so, PRU_ACCEPT, NULL,
  279. nam, NULL, curproc);
  280. else
  281. error = ECONNABORTED;
  282. splx(s);
  283. return (error);
  284. }
  285. int
  286. soconnect(struct socket *so, struct mbuf *nam)
  287. {
  288. int s;
  289. int error;
  290. if (so->so_options & SO_ACCEPTCONN)
  291. return (EOPNOTSUPP);
  292. s = splsoftnet();
  293. /*
  294. * If protocol is connection-based, can only connect once.
  295. * Otherwise, if connected, try to disconnect first.
  296. * This allows user to disconnect by connecting to, e.g.,
  297. * a null address.
  298. */
  299. if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING) &&
  300. ((so->so_proto->pr_flags & PR_CONNREQUIRED) ||
  301. (error = sodisconnect(so))))
  302. error = EISCONN;
  303. else
  304. error = (*so->so_proto->pr_usrreq)(so, PRU_CONNECT,
  305. NULL, nam, NULL, curproc);
  306. splx(s);
  307. return (error);
  308. }
  309. int
  310. soconnect2(struct socket *so1, struct socket *so2)
  311. {
  312. int s = splsoftnet();
  313. int error;
  314. error = (*so1->so_proto->pr_usrreq)(so1, PRU_CONNECT2, NULL,
  315. (struct mbuf *)so2, NULL, curproc);
  316. splx(s);
  317. return (error);
  318. }
  319. int
  320. sodisconnect(struct socket *so)
  321. {
  322. int s = splsoftnet();
  323. int error;
  324. if ((so->so_state & SS_ISCONNECTED) == 0) {
  325. error = ENOTCONN;
  326. goto bad;
  327. }
  328. if (so->so_state & SS_ISDISCONNECTING) {
  329. error = EALREADY;
  330. goto bad;
  331. }
  332. error = (*so->so_proto->pr_usrreq)(so, PRU_DISCONNECT, NULL, NULL,
  333. NULL, curproc);
  334. bad:
  335. splx(s);
  336. return (error);
  337. }
  338. #define SBLOCKWAIT(f) (((f) & MSG_DONTWAIT) ? M_NOWAIT : M_WAITOK)
  339. /*
  340. * Send on a socket.
  341. * If send must go all at once and message is larger than
  342. * send buffering, then hard error.
  343. * Lock against other senders.
  344. * If must go all at once and not enough room now, then
  345. * inform user that this would block and do nothing.
  346. * Otherwise, if nonblocking, send as much as possible.
  347. * The data to be sent is described by "uio" if nonzero,
  348. * otherwise by the mbuf chain "top" (which must be null
  349. * if uio is not). Data provided in mbuf chain must be small
  350. * enough to send all at once.
  351. *
  352. * Returns nonzero on error, timeout or signal; callers
  353. * must check for short counts if EINTR/ERESTART are returned.
  354. * Data and control buffers are freed on return.
  355. */
  356. int
  357. sosend(struct socket *so, struct mbuf *addr, struct uio *uio, struct mbuf *top,
  358. struct mbuf *control, int flags)
  359. {
  360. struct mbuf **mp;
  361. struct mbuf *m;
  362. long space, len, mlen, clen = 0;
  363. quad_t resid;
  364. int error, s;
  365. int atomic = sosendallatonce(so) || top;
  366. if (uio)
  367. resid = uio->uio_resid;
  368. else
  369. resid = top->m_pkthdr.len;
  370. /*
  371. * In theory resid should be unsigned (since uio->uio_resid is).
  372. * However, space must be signed, as it might be less than 0
  373. * if we over-committed, and we must use a signed comparison
  374. * of space and resid. On the other hand, a negative resid
  375. * causes us to loop sending 0-length segments to the protocol.
  376. * MSG_EOR on a SOCK_STREAM socket is also invalid.
  377. */
  378. if (resid < 0 ||
  379. (so->so_type == SOCK_STREAM && (flags & MSG_EOR))) {
  380. error = EINVAL;
  381. goto out;
  382. }
  383. if (uio && uio->uio_procp)
  384. uio->uio_procp->p_ru.ru_msgsnd++;
  385. if (control) {
  386. clen = control->m_len;
  387. /* reserve extra space for AF_LOCAL's internalize */
  388. if (so->so_proto->pr_domain->dom_family == AF_LOCAL &&
  389. clen >= CMSG_ALIGN(sizeof(struct cmsghdr)) &&
  390. mtod(control, struct cmsghdr *)->cmsg_type == SCM_RIGHTS)
  391. clen = CMSG_SPACE(
  392. (clen - CMSG_ALIGN(sizeof(struct cmsghdr))) *
  393. (sizeof(struct file *) / sizeof(int)));
  394. }
  395. #define snderr(errno) { error = errno; splx(s); goto release; }
  396. restart:
  397. if ((error = sblock(&so->so_snd, SBLOCKWAIT(flags))) != 0)
  398. goto out;
  399. so->so_state |= SS_ISSENDING;
  400. do {
  401. s = splsoftnet();
  402. if (so->so_state & SS_CANTSENDMORE)
  403. snderr(EPIPE);
  404. if (so->so_error) {
  405. error = so->so_error;
  406. so->so_error = 0;
  407. splx(s);
  408. goto release;
  409. }
  410. if ((so->so_state & SS_ISCONNECTED) == 0) {
  411. if (so->so_proto->pr_flags & PR_CONNREQUIRED) {
  412. if (!(resid == 0 && clen != 0))
  413. snderr(ENOTCONN);
  414. } else if (addr == 0)
  415. snderr(EDESTADDRREQ);
  416. }
  417. space = sbspace(&so->so_snd);
  418. if (flags & MSG_OOB)
  419. space += 1024;
  420. if ((atomic && resid > so->so_snd.sb_hiwat) ||
  421. (so->so_proto->pr_domain->dom_family != AF_LOCAL &&
  422. clen > so->so_snd.sb_hiwat))
  423. snderr(EMSGSIZE);
  424. if (space < resid + clen &&
  425. (atomic || space < so->so_snd.sb_lowat || space < clen)) {
  426. if ((so->so_state & SS_NBIO) || (flags & MSG_DONTWAIT))
  427. snderr(EWOULDBLOCK);
  428. sbunlock(&so->so_snd);
  429. error = sbwait(&so->so_snd);
  430. so->so_state &= ~SS_ISSENDING;
  431. splx(s);
  432. if (error)
  433. goto out;
  434. goto restart;
  435. }
  436. splx(s);
  437. mp = &top;
  438. space -= clen;
  439. do {
  440. if (uio == NULL) {
  441. /*
  442. * Data is prepackaged in "top".
  443. */
  444. resid = 0;
  445. if (flags & MSG_EOR)
  446. top->m_flags |= M_EOR;
  447. } else do {
  448. if (top == 0) {
  449. MGETHDR(m, M_WAIT, MT_DATA);
  450. mlen = MHLEN;
  451. m->m_pkthdr.len = 0;
  452. m->m_pkthdr.ph_ifidx = 0;
  453. } else {
  454. MGET(m, M_WAIT, MT_DATA);
  455. mlen = MLEN;
  456. }
  457. if (resid >= MINCLSIZE && space >= MCLBYTES) {
  458. MCLGET(m, M_NOWAIT);
  459. if ((m->m_flags & M_EXT) == 0)
  460. goto nopages;
  461. if (atomic && top == 0) {
  462. len = lmin(MCLBYTES - max_hdr,
  463. resid);
  464. m->m_data += max_hdr;
  465. } else
  466. len = lmin(MCLBYTES, resid);
  467. space -= len;
  468. } else {
  469. nopages:
  470. len = lmin(lmin(mlen, resid), space);
  471. space -= len;
  472. /*
  473. * For datagram protocols, leave room
  474. * for protocol headers in first mbuf.
  475. */
  476. if (atomic && top == 0 && len < mlen)
  477. MH_ALIGN(m, len);
  478. }
  479. error = uiomovei(mtod(m, caddr_t), (int)len,
  480. uio);
  481. resid = uio->uio_resid;
  482. m->m_len = len;
  483. *mp = m;
  484. top->m_pkthdr.len += len;
  485. if (error)
  486. goto release;
  487. mp = &m->m_next;
  488. if (resid <= 0) {
  489. if (flags & MSG_EOR)
  490. top->m_flags |= M_EOR;
  491. break;
  492. }
  493. } while (space > 0 && atomic);
  494. s = splsoftnet(); /* XXX */
  495. if (resid <= 0)
  496. so->so_state &= ~SS_ISSENDING;
  497. error = (*so->so_proto->pr_usrreq)(so,
  498. (flags & MSG_OOB) ? PRU_SENDOOB : PRU_SEND,
  499. top, addr, control, curproc);
  500. splx(s);
  501. clen = 0;
  502. control = 0;
  503. top = 0;
  504. mp = &top;
  505. if (error)
  506. goto release;
  507. } while (resid && space > 0);
  508. } while (resid);
  509. release:
  510. so->so_state &= ~SS_ISSENDING;
  511. sbunlock(&so->so_snd);
  512. out:
  513. if (top)
  514. m_freem(top);
  515. if (control)
  516. m_freem(control);
  517. return (error);
  518. }
  519. /*
  520. * Following replacement or removal of the first mbuf on the first
  521. * mbuf chain of a socket buffer, push necessary state changes back
  522. * into the socket buffer so that other consumers see the values
  523. * consistently. 'nextrecord' is the callers locally stored value of
  524. * the original value of sb->sb_mb->m_nextpkt which must be restored
  525. * when the lead mbuf changes. NOTE: 'nextrecord' may be NULL.
  526. */
  527. void
  528. sbsync(struct sockbuf *sb, struct mbuf *nextrecord)
  529. {
  530. /*
  531. * First, update for the new value of nextrecord. If necessary,
  532. * make it the first record.
  533. */
  534. if (sb->sb_mb != NULL)
  535. sb->sb_mb->m_nextpkt = nextrecord;
  536. else
  537. sb->sb_mb = nextrecord;
  538. /*
  539. * Now update any dependent socket buffer fields to reflect
  540. * the new state. This is an inline of SB_EMPTY_FIXUP, with
  541. * the addition of a second clause that takes care of the
  542. * case where sb_mb has been updated, but remains the last
  543. * record.
  544. */
  545. if (sb->sb_mb == NULL) {
  546. sb->sb_mbtail = NULL;
  547. sb->sb_lastrecord = NULL;
  548. } else if (sb->sb_mb->m_nextpkt == NULL)
  549. sb->sb_lastrecord = sb->sb_mb;
  550. }
  551. /*
  552. * Implement receive operations on a socket.
  553. * We depend on the way that records are added to the sockbuf
  554. * by sbappend*. In particular, each record (mbufs linked through m_next)
  555. * must begin with an address if the protocol so specifies,
  556. * followed by an optional mbuf or mbufs containing ancillary data,
  557. * and then zero or more mbufs of data.
  558. * In order to avoid blocking network interrupts for the entire time here,
  559. * we splx() while doing the actual copy to user space.
  560. * Although the sockbuf is locked, new data may still be appended,
  561. * and thus we must maintain consistency of the sockbuf during that time.
  562. *
  563. * The caller may receive the data as a single mbuf chain by supplying
  564. * an mbuf **mp0 for use in returning the chain. The uio is then used
  565. * only for the count in uio_resid.
  566. */
  567. int
  568. soreceive(struct socket *so, struct mbuf **paddr, struct uio *uio,
  569. struct mbuf **mp0, struct mbuf **controlp, int *flagsp,
  570. socklen_t controllen)
  571. {
  572. struct mbuf *m, **mp;
  573. struct mbuf *cm;
  574. int flags, len, error, s, offset;
  575. struct protosw *pr = so->so_proto;
  576. struct mbuf *nextrecord;
  577. int moff, type = 0;
  578. size_t orig_resid = uio->uio_resid;
  579. int uio_error = 0;
  580. int resid;
  581. mp = mp0;
  582. if (paddr)
  583. *paddr = 0;
  584. if (controlp)
  585. *controlp = 0;
  586. if (flagsp)
  587. flags = *flagsp &~ MSG_EOR;
  588. else
  589. flags = 0;
  590. if (so->so_state & SS_NBIO)
  591. flags |= MSG_DONTWAIT;
  592. if (flags & MSG_OOB) {
  593. m = m_get(M_WAIT, MT_DATA);
  594. error = (*pr->pr_usrreq)(so, PRU_RCVOOB, m,
  595. (struct mbuf *)(long)(flags & MSG_PEEK), NULL, curproc);
  596. if (error)
  597. goto bad;
  598. do {
  599. error = uiomovei(mtod(m, caddr_t),
  600. (int) min(uio->uio_resid, m->m_len), uio);
  601. m = m_free(m);
  602. } while (uio->uio_resid && error == 0 && m);
  603. bad:
  604. if (m)
  605. m_freem(m);
  606. return (error);
  607. }
  608. if (mp)
  609. *mp = NULL;
  610. restart:
  611. if ((error = sblock(&so->so_rcv, SBLOCKWAIT(flags))) != 0)
  612. return (error);
  613. s = splsoftnet();
  614. m = so->so_rcv.sb_mb;
  615. #ifdef SOCKET_SPLICE
  616. if (isspliced(so))
  617. m = NULL;
  618. #endif /* SOCKET_SPLICE */
  619. /*
  620. * If we have less data than requested, block awaiting more
  621. * (subject to any timeout) if:
  622. * 1. the current count is less than the low water mark,
  623. * 2. MSG_WAITALL is set, and it is possible to do the entire
  624. * receive operation at once if we block (resid <= hiwat), or
  625. * 3. MSG_DONTWAIT is not set.
  626. * If MSG_WAITALL is set but resid is larger than the receive buffer,
  627. * we have to do the receive in sections, and thus risk returning
  628. * a short count if a timeout or signal occurs after we start.
  629. */
  630. if (m == NULL || (((flags & MSG_DONTWAIT) == 0 &&
  631. so->so_rcv.sb_cc < uio->uio_resid) &&
  632. (so->so_rcv.sb_cc < so->so_rcv.sb_lowat ||
  633. ((flags & MSG_WAITALL) && uio->uio_resid <= so->so_rcv.sb_hiwat)) &&
  634. m->m_nextpkt == NULL && (pr->pr_flags & PR_ATOMIC) == 0)) {
  635. #ifdef DIAGNOSTIC
  636. if (m == NULL && so->so_rcv.sb_cc)
  637. #ifdef SOCKET_SPLICE
  638. if (!isspliced(so))
  639. #endif /* SOCKET_SPLICE */
  640. panic("receive 1");
  641. #endif
  642. if (so->so_error) {
  643. if (m)
  644. goto dontblock;
  645. error = so->so_error;
  646. if ((flags & MSG_PEEK) == 0)
  647. so->so_error = 0;
  648. goto release;
  649. }
  650. if (so->so_state & SS_CANTRCVMORE) {
  651. if (m)
  652. goto dontblock;
  653. else if (so->so_rcv.sb_cc == 0)
  654. goto release;
  655. }
  656. for (; m; m = m->m_next)
  657. if (m->m_type == MT_OOBDATA || (m->m_flags & M_EOR)) {
  658. m = so->so_rcv.sb_mb;
  659. goto dontblock;
  660. }
  661. if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0 &&
  662. (so->so_proto->pr_flags & PR_CONNREQUIRED)) {
  663. error = ENOTCONN;
  664. goto release;
  665. }
  666. if (uio->uio_resid == 0 && controlp == NULL)
  667. goto release;
  668. if ((so->so_state & SS_NBIO) || (flags & MSG_DONTWAIT)) {
  669. error = EWOULDBLOCK;
  670. goto release;
  671. }
  672. SBLASTRECORDCHK(&so->so_rcv, "soreceive sbwait 1");
  673. SBLASTMBUFCHK(&so->so_rcv, "soreceive sbwait 1");
  674. sbunlock(&so->so_rcv);
  675. error = sbwait(&so->so_rcv);
  676. splx(s);
  677. if (error)
  678. return (error);
  679. goto restart;
  680. }
  681. dontblock:
  682. /*
  683. * On entry here, m points to the first record of the socket buffer.
  684. * From this point onward, we maintain 'nextrecord' as a cache of the
  685. * pointer to the next record in the socket buffer. We must keep the
  686. * various socket buffer pointers and local stack versions of the
  687. * pointers in sync, pushing out modifications before operations that
  688. * may sleep, and re-reading them afterwards.
  689. *
  690. * Otherwise, we will race with the network stack appending new data
  691. * or records onto the socket buffer by using inconsistent/stale
  692. * versions of the field, possibly resulting in socket buffer
  693. * corruption.
  694. */
  695. if (uio->uio_procp)
  696. uio->uio_procp->p_ru.ru_msgrcv++;
  697. KASSERT(m == so->so_rcv.sb_mb);
  698. SBLASTRECORDCHK(&so->so_rcv, "soreceive 1");
  699. SBLASTMBUFCHK(&so->so_rcv, "soreceive 1");
  700. nextrecord = m->m_nextpkt;
  701. if (pr->pr_flags & PR_ADDR) {
  702. #ifdef DIAGNOSTIC
  703. if (m->m_type != MT_SONAME)
  704. panic("receive 1a");
  705. #endif
  706. orig_resid = 0;
  707. if (flags & MSG_PEEK) {
  708. if (paddr)
  709. *paddr = m_copym(m, 0, m->m_len, M_NOWAIT);
  710. m = m->m_next;
  711. } else {
  712. sbfree(&so->so_rcv, m);
  713. if (paddr) {
  714. *paddr = m;
  715. so->so_rcv.sb_mb = m->m_next;
  716. m->m_next = 0;
  717. m = so->so_rcv.sb_mb;
  718. } else {
  719. so->so_rcv.sb_mb = m_free(m);
  720. m = so->so_rcv.sb_mb;
  721. }
  722. sbsync(&so->so_rcv, nextrecord);
  723. }
  724. }
  725. while (m && m->m_type == MT_CONTROL && error == 0) {
  726. if (flags & MSG_PEEK) {
  727. if (controlp)
  728. *controlp = m_copym(m, 0, m->m_len, M_NOWAIT);
  729. m = m->m_next;
  730. } else {
  731. sbfree(&so->so_rcv, m);
  732. so->so_rcv.sb_mb = m->m_next;
  733. m->m_nextpkt = m->m_next = NULL;
  734. cm = m;
  735. m = so->so_rcv.sb_mb;
  736. sbsync(&so->so_rcv, nextrecord);
  737. if (controlp) {
  738. if (pr->pr_domain->dom_externalize &&
  739. mtod(cm, struct cmsghdr *)->cmsg_type ==
  740. SCM_RIGHTS)
  741. error = (*pr->pr_domain->dom_externalize)(cm,
  742. controllen, flags);
  743. *controlp = cm;
  744. } else {
  745. /*
  746. * Dispose of any SCM_RIGHTS message that went
  747. * through the read path rather than recv.
  748. */
  749. if (pr->pr_domain->dom_dispose &&
  750. mtod(cm, struct cmsghdr *)->cmsg_type == SCM_RIGHTS)
  751. pr->pr_domain->dom_dispose(cm);
  752. m_free(cm);
  753. }
  754. }
  755. if (m != NULL)
  756. nextrecord = so->so_rcv.sb_mb->m_nextpkt;
  757. else
  758. nextrecord = so->so_rcv.sb_mb;
  759. if (controlp) {
  760. orig_resid = 0;
  761. controlp = &(*controlp)->m_next;
  762. }
  763. }
  764. /* If m is non-NULL, we have some data to read. */
  765. if (m) {
  766. type = m->m_type;
  767. if (type == MT_OOBDATA)
  768. flags |= MSG_OOB;
  769. if (m->m_flags & M_BCAST)
  770. flags |= MSG_BCAST;
  771. if (m->m_flags & M_MCAST)
  772. flags |= MSG_MCAST;
  773. }
  774. SBLASTRECORDCHK(&so->so_rcv, "soreceive 2");
  775. SBLASTMBUFCHK(&so->so_rcv, "soreceive 2");
  776. moff = 0;
  777. offset = 0;
  778. while (m && uio->uio_resid > 0 && error == 0) {
  779. if (m->m_type == MT_OOBDATA) {
  780. if (type != MT_OOBDATA)
  781. break;
  782. } else if (type == MT_OOBDATA)
  783. break;
  784. #ifdef DIAGNOSTIC
  785. else if (m->m_type != MT_DATA && m->m_type != MT_HEADER)
  786. panic("receive 3");
  787. #endif
  788. so->so_state &= ~SS_RCVATMARK;
  789. len = uio->uio_resid;
  790. if (so->so_oobmark && len > so->so_oobmark - offset)
  791. len = so->so_oobmark - offset;
  792. if (len > m->m_len - moff)
  793. len = m->m_len - moff;
  794. /*
  795. * If mp is set, just pass back the mbufs.
  796. * Otherwise copy them out via the uio, then free.
  797. * Sockbuf must be consistent here (points to current mbuf,
  798. * it points to next record) when we drop priority;
  799. * we must note any additions to the sockbuf when we
  800. * block interrupts again.
  801. */
  802. if (mp == NULL && uio_error == 0) {
  803. SBLASTRECORDCHK(&so->so_rcv, "soreceive uiomove");
  804. SBLASTMBUFCHK(&so->so_rcv, "soreceive uiomove");
  805. resid = uio->uio_resid;
  806. splx(s);
  807. uio_error = uiomovei(mtod(m, caddr_t) + moff, len, uio);
  808. s = splsoftnet();
  809. if (uio_error)
  810. uio->uio_resid = resid - len;
  811. } else
  812. uio->uio_resid -= len;
  813. if (len == m->m_len - moff) {
  814. if (m->m_flags & M_EOR)
  815. flags |= MSG_EOR;
  816. if (flags & MSG_PEEK) {
  817. m = m->m_next;
  818. moff = 0;
  819. } else {
  820. nextrecord = m->m_nextpkt;
  821. sbfree(&so->so_rcv, m);
  822. if (mp) {
  823. *mp = m;
  824. mp = &m->m_next;
  825. so->so_rcv.sb_mb = m = m->m_next;
  826. *mp = NULL;
  827. } else {
  828. so->so_rcv.sb_mb = m_free(m);
  829. m = so->so_rcv.sb_mb;
  830. }
  831. /*
  832. * If m != NULL, we also know that
  833. * so->so_rcv.sb_mb != NULL.
  834. */
  835. KASSERT(so->so_rcv.sb_mb == m);
  836. if (m) {
  837. m->m_nextpkt = nextrecord;
  838. if (nextrecord == NULL)
  839. so->so_rcv.sb_lastrecord = m;
  840. } else {
  841. so->so_rcv.sb_mb = nextrecord;
  842. SB_EMPTY_FIXUP(&so->so_rcv);
  843. }
  844. SBLASTRECORDCHK(&so->so_rcv, "soreceive 3");
  845. SBLASTMBUFCHK(&so->so_rcv, "soreceive 3");
  846. }
  847. } else {
  848. if (flags & MSG_PEEK)
  849. moff += len;
  850. else {
  851. if (mp)
  852. *mp = m_copym(m, 0, len, M_WAIT);
  853. m->m_data += len;
  854. m->m_len -= len;
  855. so->so_rcv.sb_cc -= len;
  856. so->so_rcv.sb_datacc -= len;
  857. }
  858. }
  859. if (so->so_oobmark) {
  860. if ((flags & MSG_PEEK) == 0) {
  861. so->so_oobmark -= len;
  862. if (so->so_oobmark == 0) {
  863. so->so_state |= SS_RCVATMARK;
  864. break;
  865. }
  866. } else {
  867. offset += len;
  868. if (offset == so->so_oobmark)
  869. break;
  870. }
  871. }
  872. if (flags & MSG_EOR)
  873. break;
  874. /*
  875. * If the MSG_WAITALL flag is set (for non-atomic socket),
  876. * we must not quit until "uio->uio_resid == 0" or an error
  877. * termination. If a signal/timeout occurs, return
  878. * with a short count but without error.
  879. * Keep sockbuf locked against other readers.
  880. */
  881. while (flags & MSG_WAITALL && m == NULL && uio->uio_resid > 0 &&
  882. !sosendallatonce(so) && !nextrecord) {
  883. if (so->so_error || so->so_state & SS_CANTRCVMORE)
  884. break;
  885. SBLASTRECORDCHK(&so->so_rcv, "soreceive sbwait 2");
  886. SBLASTMBUFCHK(&so->so_rcv, "soreceive sbwait 2");
  887. error = sbwait(&so->so_rcv);
  888. if (error) {
  889. sbunlock(&so->so_rcv);
  890. splx(s);
  891. return (0);
  892. }
  893. if ((m = so->so_rcv.sb_mb) != NULL)
  894. nextrecord = m->m_nextpkt;
  895. }
  896. }
  897. if (m && pr->pr_flags & PR_ATOMIC) {
  898. flags |= MSG_TRUNC;
  899. if ((flags & MSG_PEEK) == 0)
  900. (void) sbdroprecord(&so->so_rcv);
  901. }
  902. if ((flags & MSG_PEEK) == 0) {
  903. if (m == NULL) {
  904. /*
  905. * First part is an inline SB_EMPTY_FIXUP(). Second
  906. * part makes sure sb_lastrecord is up-to-date if
  907. * there is still data in the socket buffer.
  908. */
  909. so->so_rcv.sb_mb = nextrecord;
  910. if (so->so_rcv.sb_mb == NULL) {
  911. so->so_rcv.sb_mbtail = NULL;
  912. so->so_rcv.sb_lastrecord = NULL;
  913. } else if (nextrecord->m_nextpkt == NULL)
  914. so->so_rcv.sb_lastrecord = nextrecord;
  915. }
  916. SBLASTRECORDCHK(&so->so_rcv, "soreceive 4");
  917. SBLASTMBUFCHK(&so->so_rcv, "soreceive 4");
  918. if (pr->pr_flags & PR_WANTRCVD && so->so_pcb)
  919. (*pr->pr_usrreq)(so, PRU_RCVD, NULL,
  920. (struct mbuf *)(long)flags, NULL, curproc);
  921. }
  922. if (orig_resid == uio->uio_resid && orig_resid &&
  923. (flags & MSG_EOR) == 0 && (so->so_state & SS_CANTRCVMORE) == 0) {
  924. sbunlock(&so->so_rcv);
  925. splx(s);
  926. goto restart;
  927. }
  928. if (uio_error)
  929. error = uio_error;
  930. if (flagsp)
  931. *flagsp |= flags;
  932. release:
  933. sbunlock(&so->so_rcv);
  934. splx(s);
  935. return (error);
  936. }
  937. int
  938. soshutdown(struct socket *so, int how)
  939. {
  940. struct protosw *pr = so->so_proto;
  941. switch (how) {
  942. case SHUT_RD:
  943. case SHUT_RDWR:
  944. sorflush(so);
  945. if (how == SHUT_RD)
  946. return (0);
  947. /* FALLTHROUGH */
  948. case SHUT_WR:
  949. return (*pr->pr_usrreq)(so, PRU_SHUTDOWN, NULL, NULL, NULL,
  950. curproc);
  951. default:
  952. return (EINVAL);
  953. }
  954. }
  955. void
  956. sorflush(struct socket *so)
  957. {
  958. struct sockbuf *sb = &so->so_rcv;
  959. struct protosw *pr = so->so_proto;
  960. int s;
  961. struct sockbuf asb;
  962. sb->sb_flags |= SB_NOINTR;
  963. (void) sblock(sb, M_WAITOK);
  964. s = splnet();
  965. socantrcvmore(so);
  966. sbunlock(sb);
  967. asb = *sb;
  968. memset(sb, 0, sizeof (*sb));
  969. /* XXX - the memset stomps all over so_rcv */
  970. if (asb.sb_flags & SB_KNOTE) {
  971. sb->sb_sel.si_note = asb.sb_sel.si_note;
  972. sb->sb_flags = SB_KNOTE;
  973. }
  974. splx(s);
  975. if (pr->pr_flags & PR_RIGHTS && pr->pr_domain->dom_dispose)
  976. (*pr->pr_domain->dom_dispose)(asb.sb_mb);
  977. sbrelease(&asb);
  978. }
  979. #ifdef SOCKET_SPLICE
  980. #define so_splicelen so_sp->ssp_len
  981. #define so_splicemax so_sp->ssp_max
  982. #define so_idletv so_sp->ssp_idletv
  983. #define so_idleto so_sp->ssp_idleto
  984. int
  985. sosplice(struct socket *so, int fd, off_t max, struct timeval *tv)
  986. {
  987. struct file *fp;
  988. struct socket *sosp;
  989. int s, error = 0;
  990. if ((so->so_proto->pr_flags & PR_SPLICE) == 0)
  991. return (EPROTONOSUPPORT);
  992. if (so->so_options & SO_ACCEPTCONN)
  993. return (EOPNOTSUPP);
  994. if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0 &&
  995. (so->so_proto->pr_flags & PR_CONNREQUIRED))
  996. return (ENOTCONN);
  997. if (so->so_sp == NULL)
  998. so->so_sp = pool_get(&sosplice_pool, PR_WAITOK | PR_ZERO);
  999. /* If no fd is given, unsplice by removing existing link. */
  1000. if (fd < 0) {
  1001. /* Lock receive buffer. */
  1002. if ((error = sblock(&so->so_rcv,
  1003. (so->so_state & SS_NBIO) ? M_NOWAIT : M_WAITOK)) != 0)
  1004. return (error);
  1005. s = splsoftnet();
  1006. if (so->so_sp->ssp_socket)
  1007. sounsplice(so, so->so_sp->ssp_socket, 1);
  1008. splx(s);
  1009. sbunlock(&so->so_rcv);
  1010. return (0);
  1011. }
  1012. if (max && max < 0)
  1013. return (EINVAL);
  1014. if (tv && (tv->tv_sec < 0 || tv->tv_usec < 0))
  1015. return (EINVAL);
  1016. /* Find sosp, the drain socket where data will be spliced into. */
  1017. if ((error = getsock(curproc, fd, &fp)) != 0)
  1018. return (error);
  1019. sosp = fp->f_data;
  1020. if (sosp->so_sp == NULL)
  1021. sosp->so_sp = pool_get(&sosplice_pool, PR_WAITOK | PR_ZERO);
  1022. /* Lock both receive and send buffer. */
  1023. if ((error = sblock(&so->so_rcv,
  1024. (so->so_state & SS_NBIO) ? M_NOWAIT : M_WAITOK)) != 0) {
  1025. FRELE(fp, curproc);
  1026. return (error);
  1027. }
  1028. if ((error = sblock(&sosp->so_snd, M_WAITOK)) != 0) {
  1029. sbunlock(&so->so_rcv);
  1030. FRELE(fp, curproc);
  1031. return (error);
  1032. }
  1033. s = splsoftnet();
  1034. if (so->so_sp->ssp_socket || sosp->so_sp->ssp_soback) {
  1035. error = EBUSY;
  1036. goto release;
  1037. }
  1038. if (sosp->so_proto->pr_usrreq != so->so_proto->pr_usrreq) {
  1039. error = EPROTONOSUPPORT;
  1040. goto release;
  1041. }
  1042. if (sosp->so_options & SO_ACCEPTCONN) {
  1043. error = EOPNOTSUPP;
  1044. goto release;
  1045. }
  1046. if ((sosp->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0) {
  1047. error = ENOTCONN;
  1048. goto release;
  1049. }
  1050. /* Splice so and sosp together. */
  1051. so->so_sp->ssp_socket = sosp;
  1052. sosp->so_sp->ssp_soback = so;
  1053. so->so_splicelen = 0;
  1054. so->so_splicemax = max;
  1055. if (tv)
  1056. so->so_idletv = *tv;
  1057. else
  1058. timerclear(&so->so_idletv);
  1059. timeout_set(&so->so_idleto, soidle, so);
  1060. /*
  1061. * To prevent softnet interrupt from calling somove() while
  1062. * we sleep, the socket buffers are not marked as spliced yet.
  1063. */
  1064. if (somove(so, M_WAIT)) {
  1065. so->so_rcv.sb_flagsintr |= SB_SPLICE;
  1066. sosp->so_snd.sb_flagsintr |= SB_SPLICE;
  1067. }
  1068. release:
  1069. splx(s);
  1070. sbunlock(&sosp->so_snd);
  1071. sbunlock(&so->so_rcv);
  1072. FRELE(fp, curproc);
  1073. return (error);
  1074. }
  1075. void
  1076. sounsplice(struct socket *so, struct socket *sosp, int wakeup)
  1077. {
  1078. splsoftassert(IPL_SOFTNET);
  1079. timeout_del(&so->so_idleto);
  1080. sosp->so_snd.sb_flagsintr &= ~SB_SPLICE;
  1081. so->so_rcv.sb_flagsintr &= ~SB_SPLICE;
  1082. so->so_sp->ssp_socket = sosp->so_sp->ssp_soback = NULL;
  1083. if (wakeup && soreadable(so))
  1084. sorwakeup(so);
  1085. }
  1086. void
  1087. soidle(void *arg)
  1088. {
  1089. struct socket *so = arg;
  1090. int s;
  1091. s = splsoftnet();
  1092. if (so->so_rcv.sb_flagsintr & SB_SPLICE) {
  1093. so->so_error = ETIMEDOUT;
  1094. sounsplice(so, so->so_sp->ssp_socket, 1);
  1095. }
  1096. splx(s);
  1097. }
  1098. /*
  1099. * Move data from receive buffer of spliced source socket to send
  1100. * buffer of drain socket. Try to move as much as possible in one
  1101. * big chunk. It is a TCP only implementation.
  1102. * Return value 0 means splicing has been finished, 1 continue.
  1103. */
  1104. int
  1105. somove(struct socket *so, int wait)
  1106. {
  1107. struct socket *sosp = so->so_sp->ssp_socket;
  1108. struct mbuf *m, **mp, *nextrecord;
  1109. u_long len, off, oobmark;
  1110. long space;
  1111. int error = 0, maxreached = 0;
  1112. short state;
  1113. splsoftassert(IPL_SOFTNET);
  1114. nextpkt:
  1115. if (so->so_error) {
  1116. error = so->so_error;
  1117. goto release;
  1118. }
  1119. if (sosp->so_state & SS_CANTSENDMORE) {
  1120. error = EPIPE;
  1121. goto release;
  1122. }
  1123. if (sosp->so_error && sosp->so_error != ETIMEDOUT &&
  1124. sosp->so_error != EFBIG) {
  1125. error = sosp->so_error;
  1126. goto release;
  1127. }
  1128. if ((sosp->so_state & SS_ISCONNECTED) == 0)
  1129. goto release;
  1130. /* Calculate how many bytes can be copied now. */
  1131. len = so->so_rcv.sb_datacc;
  1132. if (so->so_splicemax) {
  1133. KASSERT(so->so_splicelen < so->so_splicemax);
  1134. if (so->so_splicemax <= so->so_splicelen + len) {
  1135. len = so->so_splicemax - so->so_splicelen;
  1136. maxreached = 1;
  1137. }
  1138. }
  1139. space = sbspace(&sosp->so_snd);
  1140. if (so->so_oobmark && so->so_oobmark < len &&
  1141. so->so_oobmark < space + 1024)
  1142. space += 1024;
  1143. if (space <= 0) {
  1144. maxreached = 0;
  1145. goto release;
  1146. }
  1147. if (space < len) {
  1148. maxreached = 0;
  1149. if (space < sosp->so_snd.sb_lowat)
  1150. goto release;
  1151. len = space;
  1152. }
  1153. sosp->so_state |= SS_ISSENDING;
  1154. SBLASTRECORDCHK(&so->so_rcv, "somove 1");
  1155. SBLASTMBUFCHK(&so->so_rcv, "somove 1");
  1156. m = so->so_rcv.sb_mb;
  1157. if (m == NULL)
  1158. goto release;
  1159. nextrecord = m->m_nextpkt;
  1160. /* Drop address and control information not used with splicing. */
  1161. if (so->so_proto->pr_flags & PR_ADDR) {
  1162. #ifdef DIAGNOSTIC
  1163. if (m->m_type != MT_SONAME)
  1164. panic("somove soname");
  1165. #endif
  1166. m = m->m_next;
  1167. }
  1168. while (m && m->m_type == MT_CONTROL)
  1169. m = m->m_next;
  1170. if (m == NULL) {
  1171. sbdroprecord(&so->so_rcv);
  1172. if (so->so_proto->pr_flags & PR_WANTRCVD && so->so_pcb)
  1173. (so->so_proto->pr_usrreq)(so, PRU_RCVD, NULL,
  1174. (struct mbuf *)0L, NULL, NULL);
  1175. goto nextpkt;
  1176. }
  1177. if (so->so_proto->pr_flags & PR_ATOMIC) {
  1178. if ((m->m_flags & M_PKTHDR) == 0)
  1179. panic("somove pkthdr");
  1180. if (sosp->so_snd.sb_hiwat < m->m_pkthdr.len) {
  1181. error = EMSGSIZE;
  1182. goto release;
  1183. }
  1184. if (len < m->m_pkthdr.len)
  1185. goto release;
  1186. if (m->m_pkthdr.len < len) {
  1187. maxreached = 0;
  1188. len = m->m_pkthdr.len;
  1189. }
  1190. /*
  1191. * Throw away the name mbuf after it has been assured
  1192. * that the whole first record can be processed.
  1193. */
  1194. m = so->so_rcv.sb_mb;
  1195. sbfree(&so->so_rcv, m);
  1196. so->so_rcv.sb_mb = m_free(m);
  1197. sbsync(&so->so_rcv, nextrecord);
  1198. }
  1199. /*
  1200. * Throw away the control mbufs after it has been assured
  1201. * that the whole first record can be processed.
  1202. */
  1203. m = so->so_rcv.sb_mb;
  1204. while (m && m->m_type == MT_CONTROL) {
  1205. sbfree(&so->so_rcv, m);
  1206. so->so_rcv.sb_mb = m_free(m);
  1207. m = so->so_rcv.sb_mb;
  1208. sbsync(&so->so_rcv, nextrecord);
  1209. }
  1210. SBLASTRECORDCHK(&so->so_rcv, "somove 2");
  1211. SBLASTMBUFCHK(&so->so_rcv, "somove 2");
  1212. /* Take at most len mbufs out of receive buffer. */
  1213. for (off = 0, mp = &m; off <= len && *mp;
  1214. off += (*mp)->m_len, mp = &(*mp)->m_next) {
  1215. u_long size = len - off;
  1216. #ifdef DIAGNOSTIC
  1217. if ((*mp)->m_type != MT_DATA && (*mp)->m_type != MT_HEADER)
  1218. panic("somove type");
  1219. #endif
  1220. if ((*mp)->m_len > size) {
  1221. if (!maxreached || (*mp = m_copym(
  1222. so->so_rcv.sb_mb, 0, size, wait)) == NULL) {
  1223. len -= size;
  1224. break;
  1225. }
  1226. so->so_rcv.sb_mb->m_data += size;
  1227. so->so_rcv.sb_mb->m_len -= size;
  1228. so->so_rcv.sb_cc -= size;
  1229. so->so_rcv.sb_datacc -= size;
  1230. } else {
  1231. *mp = so->so_rcv.sb_mb;
  1232. sbfree(&so->so_rcv, *mp);
  1233. so->so_rcv.sb_mb = (*mp)->m_next;
  1234. sbsync(&so->so_rcv, nextrecord);
  1235. }
  1236. }
  1237. *mp = NULL;
  1238. SBLASTRECORDCHK(&so->so_rcv, "somove 3");
  1239. SBLASTMBUFCHK(&so->so_rcv, "somove 3");
  1240. SBCHECK(&so->so_rcv);
  1241. if (m == NULL)
  1242. goto release;
  1243. m->m_nextpkt = NULL;
  1244. if (m->m_flags & M_PKTHDR) {
  1245. m_tag_delete_chain(m);
  1246. memset(&m->m_pkthdr, 0, sizeof(m->m_pkthdr));
  1247. m->m_pkthdr.len = len;
  1248. m->m_pkthdr.pf.prio = IFQ_DEFPRIO;
  1249. }
  1250. /* Send window update to source peer as receive buffer has changed. */
  1251. if (so->so_proto->pr_flags & PR_WANTRCVD && so->so_pcb)
  1252. (so->so_proto->pr_usrreq)(so, PRU_RCVD, NULL,
  1253. NULL, NULL, NULL);
  1254. /* Receive buffer did shrink by len bytes, adjust oob. */
  1255. state = so->so_state;
  1256. so->so_state &= ~SS_RCVATMARK;
  1257. oobmark = so->so_oobmark;
  1258. so->so_oobmark = oobmark > len ? oobmark - len : 0;
  1259. if (oobmark) {
  1260. if (oobmark == len)
  1261. so->so_state |= SS_RCVATMARK;
  1262. if (oobmark >= len)
  1263. oobmark = 0;
  1264. }
  1265. /*
  1266. * Handle oob data. If any malloc fails, ignore error.
  1267. * TCP urgent data is not very reliable anyway.
  1268. */
  1269. while (((state & SS_RCVATMARK) || oobmark) &&
  1270. (so->so_options & SO_OOBINLINE)) {
  1271. struct mbuf *o = NULL;
  1272. if (state & SS_RCVATMARK) {
  1273. o = m_get(wait, MT_DATA);
  1274. state &= ~SS_RCVATMARK;
  1275. } else if (oobmark) {
  1276. o = m_split(m, oobmark, wait);
  1277. if (o) {
  1278. error = (*sosp->so_proto->pr_usrreq)(sosp,
  1279. PRU_SEND, m, NULL, NULL, NULL);
  1280. if (error) {
  1281. if (sosp->so_state & SS_CANTSENDMORE)
  1282. error = EPIPE;
  1283. m_freem(o);
  1284. goto release;
  1285. }
  1286. len -= oobmark;
  1287. so->so_splicelen += oobmark;
  1288. m = o;
  1289. o = m_get(wait, MT_DATA);
  1290. }
  1291. oobmark = 0;
  1292. }
  1293. if (o) {
  1294. o->m_len = 1;
  1295. *mtod(o, caddr_t) = *mtod(m, caddr_t);
  1296. error = (*sosp->so_proto->pr_usrreq)(sosp, PRU_SENDOOB,
  1297. o, NULL, NULL, NULL);
  1298. if (error) {
  1299. if (sosp->so_state & SS_CANTSENDMORE)
  1300. error = EPIPE;
  1301. m_freem(m);
  1302. goto release;
  1303. }
  1304. len -= 1;
  1305. so->so_splicelen += 1;
  1306. if (oobmark) {
  1307. oobmark -= 1;
  1308. if (oobmark == 0)
  1309. state |= SS_RCVATMARK;
  1310. }
  1311. m_adj(m, 1);
  1312. }
  1313. }
  1314. /* Append all remaining data to drain socket. */
  1315. if (so->so_rcv.sb_cc == 0 || maxreached)
  1316. sosp->so_state &= ~SS_ISSENDING;
  1317. error = (*sosp->so_proto->pr_usrreq)(sosp, PRU_SEND, m, NULL, NULL,
  1318. NULL);
  1319. if (error) {
  1320. if (sosp->so_state & SS_CANTSENDMORE)
  1321. error = EPIPE;
  1322. goto release;
  1323. }
  1324. so->so_splicelen += len;
  1325. /* Move several packets if possible. */
  1326. if (!maxreached && nextrecord)
  1327. goto nextpkt;
  1328. release:
  1329. sosp->so_state &= ~SS_ISSENDING;
  1330. if (!error && maxreached && so->so_splicemax == so->so_splicelen)
  1331. error = EFBIG;
  1332. if (error)
  1333. so->so_error = error;
  1334. if (((so->so_state & SS_CANTRCVMORE) && so->so_rcv.sb_cc == 0) ||
  1335. (sosp->so_state & SS_CANTSENDMORE) || maxreached || error) {
  1336. sounsplice(so, sosp, 1);
  1337. return (0);
  1338. }
  1339. if (timerisset(&so->so_idletv))
  1340. timeout_add_tv(&so->so_idleto, &so->so_idletv);
  1341. return (1);
  1342. }
  1343. #undef so_splicelen
  1344. #undef so_splicemax
  1345. #undef so_idletv
  1346. #undef so_idleto
  1347. #endif /* SOCKET_SPLICE */
  1348. void
  1349. sorwakeup(struct socket *so)
  1350. {
  1351. #ifdef SOCKET_SPLICE
  1352. if (so->so_rcv.sb_flagsintr & SB_SPLICE)
  1353. (void) somove(so, M_DONTWAIT);
  1354. if (isspliced(so))
  1355. return;
  1356. #endif
  1357. sowakeup(so, &so->so_rcv);
  1358. if (so->so_upcall)
  1359. (*(so->so_upcall))(so, so->so_upcallarg, M_DONTWAIT);
  1360. }
  1361. void
  1362. sowwakeup(struct socket *so)
  1363. {
  1364. #ifdef SOCKET_SPLICE
  1365. if (so->so_snd.sb_flagsintr & SB_SPLICE)
  1366. (void) somove(so->so_sp->ssp_soback, M_DONTWAIT);
  1367. #endif
  1368. sowakeup(so, &so->so_snd);
  1369. }
  1370. int
  1371. sosetopt(struct socket *so, int level, int optname, struct mbuf *m0)
  1372. {
  1373. int error = 0;
  1374. struct mbuf *m = m0;
  1375. if (level != SOL_SOCKET) {
  1376. if (so->so_proto && so->so_proto->pr_ctloutput)
  1377. return ((*so->so_proto->pr_ctloutput)
  1378. (PRCO_SETOPT, so, level, optname, &m0));
  1379. error = ENOPROTOOPT;
  1380. } else {
  1381. switch (optname) {
  1382. case SO_BINDANY:
  1383. if ((error = suser(curproc, 0)) != 0) /* XXX */
  1384. goto bad;
  1385. break;
  1386. }
  1387. switch (optname) {
  1388. case SO_LINGER:
  1389. if (m == NULL || m->m_len != sizeof (struct linger) ||
  1390. mtod(m, struct linger *)->l_linger < 0 ||
  1391. mtod(m, struct linger *)->l_linger > SHRT_MAX) {
  1392. error = EINVAL;
  1393. goto bad;
  1394. }
  1395. so->so_linger = mtod(m, struct linger *)->l_linger;
  1396. /* FALLTHROUGH */
  1397. case SO_BINDANY:
  1398. case SO_DEBUG:
  1399. case SO_KEEPALIVE:
  1400. case SO_USELOOPBACK:
  1401. case SO_BROADCAST:
  1402. case SO_REUSEADDR:
  1403. case SO_REUSEPORT:
  1404. case SO_OOBINLINE:
  1405. case SO_TIMESTAMP:
  1406. if (m == NULL || m->m_len < sizeof (int)) {
  1407. error = EINVAL;
  1408. goto bad;
  1409. }
  1410. if (*mtod(m, int *))
  1411. so->so_options |= optname;
  1412. else
  1413. so->so_options &= ~optname;
  1414. break;
  1415. case SO_DONTROUTE:
  1416. if (m == NULL || m->m_len < sizeof (int)) {
  1417. error = EINVAL;
  1418. goto bad;
  1419. }
  1420. if (*mtod(m, int *))
  1421. error = EOPNOTSUPP;
  1422. break;
  1423. case SO_SNDBUF:
  1424. case SO_RCVBUF:
  1425. case SO_SNDLOWAT:
  1426. case SO_RCVLOWAT:
  1427. {
  1428. u_long cnt;
  1429. if (m == NULL || m->m_len < sizeof (int)) {
  1430. error = EINVAL;
  1431. goto bad;
  1432. }
  1433. cnt = *mtod(m, int *);
  1434. if ((long)cnt <= 0)
  1435. cnt = 1;
  1436. switch (optname) {
  1437. case SO_SNDBUF:
  1438. if (so->so_state & SS_CANTSENDMORE) {
  1439. error = EINVAL;
  1440. goto bad;
  1441. }
  1442. if (sbcheckreserve(cnt, so->so_snd.sb_wat) ||
  1443. sbreserve(&so->so_snd, cnt)) {
  1444. error = ENOBUFS;
  1445. goto bad;
  1446. }
  1447. so->so_snd.sb_wat = cnt;
  1448. break;
  1449. case SO_RCVBUF:
  1450. if (so->so_state & SS_CANTRCVMORE) {
  1451. error = EINVAL;
  1452. goto bad;
  1453. }
  1454. if (sbcheckreserve(cnt, so->so_rcv.sb_wat) ||
  1455. sbreserve(&so->so_rcv, cnt)) {
  1456. error = ENOBUFS;
  1457. goto bad;
  1458. }
  1459. so->so_rcv.sb_wat = cnt;
  1460. break;
  1461. case SO_SNDLOWAT:
  1462. so->so_snd.sb_lowat =
  1463. (cnt > so->so_snd.sb_hiwat) ?
  1464. so->so_snd.sb_hiwat : cnt;
  1465. break;
  1466. case SO_RCVLOWAT:
  1467. so->so_rcv.sb_lowat =
  1468. (cnt > so->so_rcv.sb_hiwat) ?
  1469. so->so_rcv.sb_hiwat : cnt;
  1470. break;
  1471. }
  1472. break;
  1473. }
  1474. case SO_SNDTIMEO:
  1475. case SO_RCVTIMEO:
  1476. {
  1477. struct timeval tv;
  1478. int val;
  1479. if (m == NULL || m->m_len < sizeof (tv)) {
  1480. error = EINVAL;
  1481. goto bad;
  1482. }
  1483. memcpy(&tv, mtod(m, struct timeval *), sizeof tv);
  1484. val = tvtohz(&tv);
  1485. if (val > USHRT_MAX) {
  1486. error = EDOM;
  1487. goto bad;
  1488. }
  1489. switch (optname) {
  1490. case SO_SNDTIMEO:
  1491. so->so_snd.sb_timeo = val;
  1492. break;
  1493. case SO_RCVTIMEO:
  1494. so->so_rcv.sb_timeo = val;
  1495. break;
  1496. }
  1497. break;
  1498. }
  1499. case SO_RTABLE:
  1500. if (so->so_proto && so->so_proto->pr_domain &&
  1501. so->so_proto->pr_domain->dom_protosw &&
  1502. so->so_proto->pr_ctloutput) {
  1503. struct domain *dom = so->so_proto->pr_domain;
  1504. level = dom->dom_protosw->pr_protocol;
  1505. return ((*so->so_proto->pr_ctloutput)
  1506. (PRCO_SETOPT, so, level, optname, &m0));
  1507. }
  1508. error = ENOPROTOOPT;
  1509. break;
  1510. #ifdef SOCKET_SPLICE
  1511. case SO_SPLICE:
  1512. if (m == NULL) {
  1513. error = sosplice(so, -1, 0, NULL);
  1514. } else if (m->m_len < sizeof(int)) {
  1515. error = EINVAL;
  1516. goto bad;
  1517. } else if (m->m_len < sizeof(struct splice)) {
  1518. error = sosplice(so, *mtod(m, int *), 0, NULL);
  1519. } else {
  1520. error = sosplice(so,
  1521. mtod(m, struct splice *)->sp_fd,
  1522. mtod(m, struct splice *)->sp_max,
  1523. &mtod(m, struct splice *)->sp_idle);
  1524. }
  1525. break;
  1526. #endif /* SOCKET_SPLICE */
  1527. default:
  1528. error = ENOPROTOOPT;
  1529. break;
  1530. }
  1531. if (error == 0 && so->so_proto && so->so_proto->pr_ctloutput) {
  1532. (void) ((*so->so_proto->pr_ctloutput)
  1533. (PRCO_SETOPT, so, level, optname, &m0));
  1534. m = NULL; /* freed by protocol */
  1535. }
  1536. }
  1537. bad:
  1538. if (m)
  1539. (void) m_free(m);
  1540. return (error);
  1541. }
  1542. int
  1543. sogetopt(struct socket *so, int level, int optname, struct mbuf **mp)
  1544. {
  1545. struct mbuf *m;
  1546. if (level != SOL_SOCKET) {
  1547. if (so->so_proto && so->so_proto->pr_ctloutput) {
  1548. return ((*so->so_proto->pr_ctloutput)
  1549. (PRCO_GETOPT, so, level, optname, mp));
  1550. } else
  1551. return (ENOPROTOOPT);
  1552. } else {
  1553. m = m_get(M_WAIT, MT_SOOPTS);
  1554. m->m_len = sizeof (int);
  1555. switch (optname) {
  1556. case SO_LINGER:
  1557. m->m_len = sizeof (struct linger);
  1558. mtod(m, struct linger *)->l_onoff =
  1559. so->so_options & SO_LINGER;
  1560. mtod(m, struct linger *)->l_linger = so->so_linger;
  1561. break;
  1562. case SO_BINDANY:
  1563. case SO_USELOOPBACK:
  1564. case SO_DEBUG:
  1565. case SO_KEEPALIVE:
  1566. case SO_REUSEADDR:
  1567. case SO_REUSEPORT:
  1568. case SO_BROADCAST:
  1569. case SO_OOBINLINE:
  1570. case SO_TIMESTAMP:
  1571. *mtod(m, int *) = so->so_options & optname;
  1572. break;
  1573. case SO_DONTROUTE:
  1574. *mtod(m, int *) = 0;
  1575. break;
  1576. case SO_TYPE:
  1577. *mtod(m, int *) = so->so_type;
  1578. break;
  1579. case SO_ERROR:
  1580. *mtod(m, int *) = so->so_error;
  1581. so->so_error = 0;
  1582. break;
  1583. case SO_SNDBUF:
  1584. *mtod(m, int *) = so->so_snd.sb_hiwat;
  1585. break;
  1586. case SO_RCVBUF:
  1587. *mtod(m, int *) = so->so_rcv.sb_hiwat;
  1588. break;
  1589. case SO_SNDLOWAT:
  1590. *mtod(m, int *) = so->so_snd.sb_lowat;
  1591. break;
  1592. case SO_RCVLOWAT:
  1593. *mtod(m, int *) = so->so_rcv.sb_lowat;
  1594. break;
  1595. case SO_SNDTIMEO:
  1596. case SO_RCVTIMEO:
  1597. {
  1598. struct timeval tv;
  1599. int val = (optname == SO_SNDTIMEO ?
  1600. so->so_snd.sb_timeo : so->so_rcv.sb_timeo);
  1601. m->m_len = sizeof(struct timeval);
  1602. memset(&tv, 0, sizeof(tv));
  1603. tv.tv_sec = val / hz;
  1604. tv.tv_usec = (val % hz) * tick;
  1605. memcpy(mtod(m, struct timeval *), &tv, sizeof tv);
  1606. break;
  1607. }
  1608. case SO_RTABLE:
  1609. (void)m_free(m);
  1610. if (so->so_proto && so->so_proto->pr_domain &&
  1611. so->so_proto->pr_domain->dom_protosw &&
  1612. so->so_proto->pr_ctloutput) {
  1613. struct domain *dom = so->so_proto->pr_domain;
  1614. level = dom->dom_protosw->pr_protocol;
  1615. return ((*so->so_proto->pr_ctloutput)
  1616. (PRCO_GETOPT, so, level, optname, mp));
  1617. }
  1618. return (ENOPROTOOPT);
  1619. break;
  1620. #ifdef SOCKET_SPLICE
  1621. case SO_SPLICE:
  1622. {
  1623. off_t len;
  1624. int s = splsoftnet();
  1625. m->m_len = sizeof(off_t);
  1626. len = so->so_sp ? so->so_sp->ssp_len : 0;
  1627. memcpy(mtod(m, off_t *), &len, sizeof(off_t));
  1628. splx(s);
  1629. break;
  1630. }
  1631. #endif /* SOCKET_SPLICE */
  1632. case SO_PEERCRED:
  1633. if (so->so_proto->pr_protocol == AF_UNIX) {
  1634. struct unpcb *unp = sotounpcb(so);
  1635. if (unp->unp_flags & UNP_FEIDS) {
  1636. m->m_len = sizeof(unp->unp_connid);
  1637. memcpy(mtod(m, caddr_t),
  1638. &(unp->unp_connid), m->m_len);
  1639. break;
  1640. }
  1641. (void)m_free(m);
  1642. return (ENOTCONN);
  1643. }
  1644. (void)m_free(m);
  1645. return (EOPNOTSUPP);
  1646. break;
  1647. default:
  1648. (void)m_free(m);
  1649. return (ENOPROTOOPT);
  1650. }
  1651. *mp = m;
  1652. return (0);
  1653. }
  1654. }
  1655. void
  1656. sohasoutofband(struct socket *so)
  1657. {
  1658. csignal(so->so_pgid, SIGURG, so->so_siguid, so->so_sigeuid);
  1659. selwakeup(&so->so_rcv.sb_sel);
  1660. }
  1661. int
  1662. soo_kqfilter(struct file *fp, struct knote *kn)
  1663. {
  1664. struct socket *so = kn->kn_fp->f_data;
  1665. struct sockbuf *sb;
  1666. int s;
  1667. switch (kn->kn_filter) {
  1668. case EVFILT_READ:
  1669. if (so->so_options & SO_ACCEPTCONN)
  1670. kn->kn_fop = &solisten_filtops;
  1671. else
  1672. kn->kn_fop = &soread_filtops;
  1673. sb = &so->so_rcv;
  1674. break;
  1675. case EVFILT_WRITE:
  1676. kn->kn_fop = &sowrite_filtops;
  1677. sb = &so->so_snd;
  1678. break;
  1679. default:
  1680. return (EINVAL);
  1681. }
  1682. s = splnet();
  1683. SLIST_INSERT_HEAD(&sb->sb_sel.si_note, kn, kn_selnext);
  1684. sb->sb_flags |= SB_KNOTE;
  1685. splx(s);
  1686. return (0);
  1687. }
  1688. void
  1689. filt_sordetach(struct knote *kn)
  1690. {
  1691. struct socket *so = kn->kn_fp->f_data;
  1692. int s = splnet();
  1693. SLIST_REMOVE(&so->so_rcv.sb_sel.si_note, kn, knote, kn_selnext);
  1694. if (SLIST_EMPTY(&so->so_rcv.sb_sel.si_note))
  1695. so->so_rcv.sb_flags &= ~SB_KNOTE;
  1696. splx(s);
  1697. }
  1698. /*ARGSUSED*/
  1699. int
  1700. filt_soread(struct knote *kn, long hint)
  1701. {
  1702. struct socket *so = kn->kn_fp->f_data;
  1703. kn->kn_data = so->so_rcv.sb_cc;
  1704. #ifdef SOCKET_SPLICE
  1705. if (isspliced(so))
  1706. return (0);
  1707. #endif /* SOCKET_SPLICE */
  1708. if (so->so_state & SS_CANTRCVMORE) {
  1709. kn->kn_flags |= EV_EOF;
  1710. kn->kn_fflags = so->so_error;
  1711. return (1);
  1712. }
  1713. if (so->so_error) /* temporary udp error */
  1714. return (1);
  1715. if (kn->kn_sfflags & NOTE_LOWAT)
  1716. return (kn->kn_data >= kn->kn_sdata);
  1717. return (kn->kn_data >= so->so_rcv.sb_lowat);
  1718. }
  1719. void
  1720. filt_sowdetach(struct knote *kn)
  1721. {
  1722. struct socket *so = kn->kn_fp->f_data;
  1723. int s = splnet();
  1724. SLIST_REMOVE(&so->so_snd.sb_sel.si_note, kn, knote, kn_selnext);
  1725. if (SLIST_EMPTY(&so->so_snd.sb_sel.si_note))
  1726. so->so_snd.sb_flags &= ~SB_KNOTE;
  1727. splx(s);
  1728. }
  1729. /*ARGSUSED*/
  1730. int
  1731. filt_sowrite(struct knote *kn, long hint)
  1732. {
  1733. struct socket *so = kn->kn_fp->f_data;
  1734. kn->kn_data = sbspace(&so->so_snd);
  1735. if (so->so_state & SS_CANTSENDMORE) {
  1736. kn->kn_flags |= EV_EOF;
  1737. kn->kn_fflags = so->so_error;
  1738. return (1);
  1739. }
  1740. if (so->so_error) /* temporary udp error */
  1741. return (1);
  1742. if (((so->so_state & SS_ISCONNECTED) == 0) &&
  1743. (so->so_proto->pr_flags & PR_CONNREQUIRED))
  1744. return (0);
  1745. if (kn->kn_sfflags & NOTE_LOWAT)
  1746. return (kn->kn_data >= kn->kn_sdata);
  1747. return (kn->kn_data >= so->so_snd.sb_lowat);
  1748. }
  1749. /*ARGSUSED*/
  1750. int
  1751. filt_solisten(struct knote *kn, long hint)
  1752. {
  1753. struct socket *so = kn->kn_fp->f_data;
  1754. kn->kn_data = so->so_qlen;
  1755. return (so->so_qlen != 0);
  1756. }