PageRenderTime 65ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/sys/kern/uipc_socket.c

https://github.com/brauceunix/libuinet
C | 3902 lines | 2830 code | 357 blank | 715 comment | 768 complexity | 567e0eb2b3bb65b01dd0a33ff9a26120 MD5 | raw file
Possible License(s): 0BSD, BSD-2-Clause, MIT
  1. /*-
  2. * Copyright (c) 1982, 1986, 1988, 1990, 1993
  3. * The Regents of the University of California.
  4. * Copyright (c) 2004 The FreeBSD Foundation
  5. * Copyright (c) 2004-2008 Robert N. M. Watson
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in the
  15. * documentation and/or other materials provided with the distribution.
  16. * 4. Neither the name of the University nor the names of its contributors
  17. * may be used to endorse or promote products derived from this software
  18. * without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  21. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  24. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  25. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  26. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  27. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  28. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  29. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30. * SUCH DAMAGE.
  31. *
  32. * @(#)uipc_socket.c 8.3 (Berkeley) 4/15/94
  33. */
  34. /*
  35. * Comments on the socket life cycle:
  36. *
  37. * soalloc() sets of socket layer state for a socket, called only by
  38. * socreate() and sonewconn(). Socket layer private.
  39. *
  40. * sodealloc() tears down socket layer state for a socket, called only by
  41. * sofree() and sonewconn(). Socket layer private.
  42. *
  43. * pru_attach() associates protocol layer state with an allocated socket;
  44. * called only once, may fail, aborting socket allocation. This is called
  45. * from socreate() and sonewconn(). Socket layer private.
  46. *
  47. * pru_detach() disassociates protocol layer state from an attached socket,
  48. * and will be called exactly once for sockets in which pru_attach() has
  49. * been successfully called. If pru_attach() returned an error,
  50. * pru_detach() will not be called. Socket layer private.
  51. *
  52. * pru_abort() and pru_close() notify the protocol layer that the last
  53. * consumer of a socket is starting to tear down the socket, and that the
  54. * protocol should terminate the connection. Historically, pru_abort() also
  55. * detached protocol state from the socket state, but this is no longer the
  56. * case.
  57. *
  58. * socreate() creates a socket and attaches protocol state. This is a public
  59. * interface that may be used by socket layer consumers to create new
  60. * sockets.
  61. *
  62. * sonewconn() creates a socket and attaches protocol state. This is a
  63. * public interface that may be used by protocols to create new sockets when
  64. * a new connection is received and will be available for accept() on a
  65. * listen socket.
  66. *
  67. * soclose() destroys a socket after possibly waiting for it to disconnect.
  68. * This is a public interface that socket consumers should use to close and
  69. * release a socket when done with it.
  70. *
  71. * soabort() destroys a socket without waiting for it to disconnect (used
  72. * only for incoming connections that are already partially or fully
  73. * connected). This is used internally by the socket layer when clearing
  74. * listen socket queues (due to overflow or close on the listen socket), but
  75. * is also a public interface protocols may use to abort connections in
  76. * their incomplete listen queues should they no longer be required. Sockets
  77. * placed in completed connection listen queues should not be aborted for
  78. * reasons described in the comment above the soclose() implementation. This
  79. * is not a general purpose close routine, and except in the specific
  80. * circumstances described here, should not be used.
  81. *
  82. * sofree() will free a socket and its protocol state if all references on
  83. * the socket have been released, and is the public interface to attempt to
  84. * free a socket when a reference is removed. This is a socket layer private
  85. * interface.
  86. *
  87. * NOTE: In addition to socreate() and soclose(), which provide a single
  88. * socket reference to the consumer to be managed as required, there are two
  89. * calls to explicitly manage socket references, soref(), and sorele().
  90. * Currently, these are generally required only when transitioning a socket
  91. * from a listen queue to a file descriptor, in order to prevent garbage
  92. * collection of the socket at an untimely moment. For a number of reasons,
  93. * these interfaces are not preferred, and should be avoided.
  94. *
  95. * NOTE: With regard to VNETs the general rule is that callers do not set
  96. * curvnet. Exceptions to this rule include soabort(), sodisconnect(),
  97. * sofree() (and with that sorele(), sotryfree()), as well as sonewconn()
  98. * and sorflush(), which are usually called from a pre-set VNET context.
  99. * sopoll() currently does not need a VNET context to be set.
  100. */
  101. #include <sys/cdefs.h>
  102. __FBSDID("$FreeBSD: release/9.1.0/sys/kern/uipc_socket.c 233353 2012-03-23 11:26:54Z kib $");
  103. #include "opt_inet.h"
  104. #include "opt_inet6.h"
  105. #include "opt_zero.h"
  106. #include "opt_compat.h"
  107. #include "opt_passiveinet.h"
  108. #include "opt_promiscinet.h"
  109. #include <sys/param.h>
  110. #include <sys/systm.h>
  111. #include <sys/fcntl.h>
  112. #include <sys/limits.h>
  113. #include <sys/lock.h>
  114. #include <sys/mac.h>
  115. #include <sys/malloc.h>
  116. #include <sys/mbuf.h>
  117. #include <sys/mutex.h>
  118. #include <sys/domain.h>
  119. #include <sys/file.h> /* for struct knote */
  120. #include <sys/kernel.h>
  121. #include <sys/event.h>
  122. #include <sys/eventhandler.h>
  123. #include <sys/poll.h>
  124. #include <sys/proc.h>
  125. #include <sys/protosw.h>
  126. #include <sys/socket.h>
  127. #include <sys/socketvar.h>
  128. #include <sys/resourcevar.h>
  129. #include <net/route.h>
  130. #include <sys/signalvar.h>
  131. #include <sys/stat.h>
  132. #include <sys/sx.h>
  133. #include <sys/sysctl.h>
  134. #include <sys/uio.h>
  135. #include <sys/jail.h>
  136. #include <net/vnet.h>
  137. #ifdef MAC
  138. #include <security/mac/mac_framework.h>
  139. #endif /* MAC */
  140. #ifdef PROMISCUOUS_INET
  141. #include <netinet/in_promisc.h>
  142. #endif
  143. #include <vm/uma.h>
  144. #ifdef COMPAT_FREEBSD32
  145. #include <sys/mount.h>
  146. #include <sys/sysent.h>
  147. #include <compat/freebsd32/freebsd32.h>
  148. #endif
  149. static int soreceive_rcvoob(struct socket *so, struct uio *uio,
  150. int flags);
  151. static void filt_sordetach(struct knote *kn);
  152. static int filt_soread(struct knote *kn, long hint);
  153. static void filt_sowdetach(struct knote *kn);
  154. static int filt_sowrite(struct knote *kn, long hint);
  155. static int filt_solisten(struct knote *kn, long hint);
  156. static struct filterops solisten_filtops = {
  157. .f_isfd = 1,
  158. .f_detach = filt_sordetach,
  159. .f_event = filt_solisten,
  160. };
  161. static struct filterops soread_filtops = {
  162. .f_isfd = 1,
  163. .f_detach = filt_sordetach,
  164. .f_event = filt_soread,
  165. };
  166. static struct filterops sowrite_filtops = {
  167. .f_isfd = 1,
  168. .f_detach = filt_sowdetach,
  169. .f_event = filt_sowrite,
  170. };
  171. uma_zone_t socket_zone;
  172. so_gen_t so_gencnt; /* generation count for sockets */
  173. int maxsockets;
  174. MALLOC_DEFINE(M_SONAME, "soname", "socket name");
  175. MALLOC_DEFINE(M_PCB, "pcb", "protocol control block");
  176. #define VNET_SO_ASSERT(so) \
  177. VNET_ASSERT(curvnet != NULL, \
  178. ("%s:%d curvnet is NULL, so=%p", __func__, __LINE__, (so)));
  179. static int somaxconn = SOMAXCONN;
  180. static int sysctl_somaxconn(SYSCTL_HANDLER_ARGS);
  181. /* XXX: we dont have SYSCTL_USHORT */
  182. SYSCTL_PROC(_kern_ipc, KIPC_SOMAXCONN, somaxconn, CTLTYPE_UINT | CTLFLAG_RW,
  183. 0, sizeof(int), sysctl_somaxconn, "I", "Maximum pending socket connection "
  184. "queue size");
  185. static int numopensockets;
  186. SYSCTL_INT(_kern_ipc, OID_AUTO, numopensockets, CTLFLAG_RD,
  187. &numopensockets, 0, "Number of open sockets");
  188. #ifdef ZERO_COPY_SOCKETS
  189. /* These aren't static because they're used in other files. */
  190. int so_zero_copy_send = 1;
  191. int so_zero_copy_receive = 1;
  192. SYSCTL_NODE(_kern_ipc, OID_AUTO, zero_copy, CTLFLAG_RD, 0,
  193. "Zero copy controls");
  194. SYSCTL_INT(_kern_ipc_zero_copy, OID_AUTO, receive, CTLFLAG_RW,
  195. &so_zero_copy_receive, 0, "Enable zero copy receive");
  196. SYSCTL_INT(_kern_ipc_zero_copy, OID_AUTO, send, CTLFLAG_RW,
  197. &so_zero_copy_send, 0, "Enable zero copy send");
  198. #endif /* ZERO_COPY_SOCKETS */
  199. /*
  200. * accept_mtx locks down per-socket fields relating to accept queues. See
  201. * socketvar.h for an annotation of the protected fields of struct socket.
  202. */
  203. struct mtx accept_mtx;
  204. MTX_SYSINIT(accept_mtx, &accept_mtx, "accept", MTX_DEF);
  205. /*
  206. * so_global_mtx protects so_gencnt, numopensockets, and the per-socket
  207. * so_gencnt field.
  208. */
  209. static struct mtx so_global_mtx;
  210. MTX_SYSINIT(so_global_mtx, &so_global_mtx, "so_glabel", MTX_DEF);
  211. /*
  212. * General IPC sysctl name space, used by sockets and a variety of other IPC
  213. * types.
  214. */
  215. SYSCTL_NODE(_kern, KERN_IPC, ipc, CTLFLAG_RW, 0, "IPC");
  216. /*
  217. * Sysctl to get and set the maximum global sockets limit. Notify protocols
  218. * of the change so that they can update their dependent limits as required.
  219. */
  220. static int
  221. sysctl_maxsockets(SYSCTL_HANDLER_ARGS)
  222. {
  223. int error, newmaxsockets;
  224. newmaxsockets = maxsockets;
  225. error = sysctl_handle_int(oidp, &newmaxsockets, 0, req);
  226. if (error == 0 && req->newptr) {
  227. if (newmaxsockets > maxsockets) {
  228. maxsockets = newmaxsockets;
  229. if (maxsockets > ((maxfiles / 4) * 3)) {
  230. maxfiles = (maxsockets * 5) / 4;
  231. maxfilesperproc = (maxfiles * 9) / 10;
  232. }
  233. EVENTHANDLER_INVOKE(maxsockets_change);
  234. } else
  235. error = EINVAL;
  236. }
  237. return (error);
  238. }
  239. SYSCTL_PROC(_kern_ipc, OID_AUTO, maxsockets, CTLTYPE_INT|CTLFLAG_RW,
  240. &maxsockets, 0, sysctl_maxsockets, "IU",
  241. "Maximum number of sockets avaliable");
  242. /*
  243. * Initialise maxsockets. This SYSINIT must be run after
  244. * tunable_mbinit().
  245. */
  246. static void
  247. init_maxsockets(void *ignored)
  248. {
  249. TUNABLE_INT_FETCH("kern.ipc.maxsockets", &maxsockets);
  250. maxsockets = imax(maxsockets, imax(maxfiles, nmbclusters));
  251. }
  252. SYSINIT(param, SI_SUB_TUNABLES, SI_ORDER_ANY, init_maxsockets, NULL);
  253. /*
  254. * Socket operation routines. These routines are called by the routines in
  255. * sys_socket.c or from a system process, and implement the semantics of
  256. * socket operations by switching out to the protocol specific routines.
  257. */
  258. /*
  259. * Get a socket structure from our zone, and initialize it. Note that it
  260. * would probably be better to allocate socket and PCB at the same time, but
  261. * I'm not convinced that all the protocols can be easily modified to do
  262. * this.
  263. *
  264. * soalloc() returns a socket with a ref count of 0.
  265. */
  266. static struct socket *
  267. soalloc(struct vnet *vnet)
  268. {
  269. struct socket *so;
  270. so = uma_zalloc(socket_zone, M_NOWAIT | M_ZERO);
  271. if (so == NULL)
  272. return (NULL);
  273. #ifdef MAC
  274. if (mac_socket_init(so, M_NOWAIT) != 0) {
  275. uma_zfree(socket_zone, so);
  276. return (NULL);
  277. }
  278. #endif
  279. #ifdef PROMISCUOUS_INET
  280. if (in_promisc_socket_init(so, M_NOWAIT) != 0) {
  281. #ifdef MAC
  282. mac_socket_destroy(so);
  283. #endif
  284. uma_zfree(socket_zone, so);
  285. return (NULL);
  286. }
  287. #endif /* PROMISCUOUS_INET */
  288. SOCKBUF_LOCK_INIT(&so->so_snd, "so_snd");
  289. SOCKBUF_LOCK_INIT(&so->so_rcv, "so_rcv");
  290. sx_init(&so->so_snd.sb_sx, "so_snd_sx");
  291. sx_init(&so->so_rcv.sb_sx, "so_rcv_sx");
  292. TAILQ_INIT(&so->so_aiojobq);
  293. mtx_lock(&so_global_mtx);
  294. so->so_gencnt = ++so_gencnt;
  295. ++numopensockets;
  296. #ifdef VIMAGE
  297. VNET_ASSERT(vnet != NULL, ("%s:%d vnet is NULL, so=%p",
  298. __func__, __LINE__, so));
  299. vnet->vnet_sockcnt++;
  300. so->so_vnet = vnet;
  301. #endif
  302. mtx_unlock(&so_global_mtx);
  303. return (so);
  304. }
  305. /*
  306. * Free the storage associated with a socket at the socket layer, tear down
  307. * locks, labels, etc. All protocol state is assumed already to have been
  308. * torn down (and possibly never set up) by the caller.
  309. */
  310. static void
  311. sodealloc(struct socket *so)
  312. {
  313. KASSERT(so->so_count == 0, ("sodealloc(): so_count %d", so->so_count));
  314. KASSERT(so->so_pcb == NULL, ("sodealloc(): so_pcb != NULL"));
  315. mtx_lock(&so_global_mtx);
  316. so->so_gencnt = ++so_gencnt;
  317. --numopensockets; /* Could be below, but faster here. */
  318. #ifdef VIMAGE
  319. VNET_ASSERT(so->so_vnet != NULL, ("%s:%d so_vnet is NULL, so=%p",
  320. __func__, __LINE__, so));
  321. so->so_vnet->vnet_sockcnt--;
  322. #endif
  323. mtx_unlock(&so_global_mtx);
  324. if (so->so_rcv.sb_hiwat)
  325. (void)chgsbsize(so->so_cred->cr_uidinfo,
  326. &so->so_rcv.sb_hiwat, 0, RLIM_INFINITY);
  327. if (so->so_snd.sb_hiwat)
  328. (void)chgsbsize(so->so_cred->cr_uidinfo,
  329. &so->so_snd.sb_hiwat, 0, RLIM_INFINITY);
  330. #ifdef INET
  331. /* remove acccept filter if one is present. */
  332. if (so->so_accf != NULL)
  333. do_setopt_accept_filter(so, NULL);
  334. #endif
  335. #ifdef PROMISCUOUS_INET
  336. in_promisc_socket_destroy(so);
  337. #endif
  338. #ifdef MAC
  339. mac_socket_destroy(so);
  340. #endif
  341. crfree(so->so_cred);
  342. sx_destroy(&so->so_snd.sb_sx);
  343. sx_destroy(&so->so_rcv.sb_sx);
  344. SOCKBUF_LOCK_DESTROY(&so->so_snd);
  345. SOCKBUF_LOCK_DESTROY(&so->so_rcv);
  346. uma_zfree(socket_zone, so);
  347. }
  348. /*
  349. * socreate returns a socket with a ref count of 1. The socket should be
  350. * closed with soclose().
  351. */
  352. int
  353. socreate(int dom, struct socket **aso, int type, int proto,
  354. struct ucred *cred, struct thread *td)
  355. {
  356. struct protosw *prp;
  357. struct socket *so;
  358. int error;
  359. if (proto)
  360. prp = pffindproto(dom, proto, type);
  361. else
  362. prp = pffindtype(dom, type);
  363. if (prp == NULL || prp->pr_usrreqs->pru_attach == NULL ||
  364. prp->pr_usrreqs->pru_attach == pru_attach_notsupp)
  365. return (EPROTONOSUPPORT);
  366. if (prison_check_af(cred, prp->pr_domain->dom_family) != 0)
  367. return (EPROTONOSUPPORT);
  368. if (prp->pr_type != type)
  369. return (EPROTOTYPE);
  370. so = soalloc(CRED_TO_VNET(cred));
  371. if (so == NULL)
  372. return (ENOBUFS);
  373. TAILQ_INIT(&so->so_incomp);
  374. TAILQ_INIT(&so->so_comp);
  375. so->so_type = type;
  376. so->so_cred = crhold(cred);
  377. if ((prp->pr_domain->dom_family == PF_INET) ||
  378. (prp->pr_domain->dom_family == PF_INET6) ||
  379. (prp->pr_domain->dom_family == PF_ROUTE))
  380. so->so_fibnum = td->td_proc->p_fibnum;
  381. else
  382. so->so_fibnum = 0;
  383. so->so_proto = prp;
  384. #ifdef MAC
  385. mac_socket_create(cred, so);
  386. #endif
  387. knlist_init_mtx(&so->so_rcv.sb_sel.si_note, SOCKBUF_MTX(&so->so_rcv));
  388. knlist_init_mtx(&so->so_snd.sb_sel.si_note, SOCKBUF_MTX(&so->so_snd));
  389. so->so_count = 1;
  390. /*
  391. * Auto-sizing of socket buffers is managed by the protocols and
  392. * the appropriate flags must be set in the pru_attach function.
  393. */
  394. CURVNET_SET(so->so_vnet);
  395. error = (*prp->pr_usrreqs->pru_attach)(so, proto, td);
  396. CURVNET_RESTORE();
  397. if (error) {
  398. KASSERT(so->so_count == 1, ("socreate: so_count %d",
  399. so->so_count));
  400. so->so_count = 0;
  401. sodealloc(so);
  402. return (error);
  403. }
  404. *aso = so;
  405. return (0);
  406. }
  407. #ifdef REGRESSION
  408. static int regression_sonewconn_earlytest = 1;
  409. SYSCTL_INT(_regression, OID_AUTO, sonewconn_earlytest, CTLFLAG_RW,
  410. &regression_sonewconn_earlytest, 0, "Perform early sonewconn limit test");
  411. #endif
  412. /*
  413. * When an attempt at a new connection is noted on a socket which accepts
  414. * connections, sonewconn is called. If the connection is possible (subject
  415. * to space constraints, etc.) then we allocate a new structure, properly
  416. * linked into the data structure of the original socket, and return this.
  417. * Connstatus may be 0, or SO_ISCONFIRMING, or SO_ISCONNECTED.
  418. *
  419. * Note: the ref count on the socket is 0 on return.
  420. */
  421. struct socket *
  422. sonewconn(struct socket *head, int connstatus)
  423. {
  424. struct socket *so;
  425. int over;
  426. ACCEPT_LOCK();
  427. over = (head->so_qlen > 3 * head->so_qlimit / 2);
  428. ACCEPT_UNLOCK();
  429. #ifdef REGRESSION
  430. if (regression_sonewconn_earlytest && over)
  431. #else
  432. if (over)
  433. #endif
  434. return (NULL);
  435. VNET_ASSERT(head->so_vnet != NULL, ("%s:%d so_vnet is NULL, head=%p",
  436. __func__, __LINE__, head));
  437. so = soalloc(head->so_vnet);
  438. if (so == NULL)
  439. return (NULL);
  440. if ((head->so_options & SO_ACCEPTFILTER) != 0)
  441. connstatus = 0;
  442. so->so_head = head;
  443. so->so_type = head->so_type;
  444. so->so_options = head->so_options &~ SO_ACCEPTCONN;
  445. so->so_linger = head->so_linger;
  446. so->so_state = head->so_state | SS_NOFDREF;
  447. so->so_fibnum = head->so_fibnum;
  448. so->so_proto = head->so_proto;
  449. so->so_cred = crhold(head->so_cred);
  450. #ifdef MAC
  451. mac_socket_newconn(head, so);
  452. #endif
  453. #ifdef PROMISCUOUS_INET
  454. in_promisc_socket_newconn(head, so);
  455. #endif
  456. knlist_init_mtx(&so->so_rcv.sb_sel.si_note, SOCKBUF_MTX(&so->so_rcv));
  457. knlist_init_mtx(&so->so_snd.sb_sel.si_note, SOCKBUF_MTX(&so->so_snd));
  458. VNET_SO_ASSERT(head);
  459. if (soreserve(so, head->so_snd.sb_hiwat, head->so_rcv.sb_hiwat) ||
  460. (*so->so_proto->pr_usrreqs->pru_attach)(so, 0, NULL)) {
  461. sodealloc(so);
  462. return (NULL);
  463. }
  464. so->so_rcv.sb_lowat = head->so_rcv.sb_lowat;
  465. so->so_snd.sb_lowat = head->so_snd.sb_lowat;
  466. so->so_rcv.sb_timeo = head->so_rcv.sb_timeo;
  467. so->so_snd.sb_timeo = head->so_snd.sb_timeo;
  468. so->so_rcv.sb_flags |= head->so_rcv.sb_flags & SB_AUTOSIZE;
  469. so->so_snd.sb_flags |= head->so_snd.sb_flags & SB_AUTOSIZE;
  470. so->so_state |= connstatus;
  471. ACCEPT_LOCK();
  472. if (connstatus) {
  473. TAILQ_INSERT_TAIL(&head->so_comp, so, so_list);
  474. so->so_qstate |= SQ_COMP;
  475. head->so_qlen++;
  476. } else {
  477. /*
  478. * Keep removing sockets from the head until there's room for
  479. * us to insert on the tail. In pre-locking revisions, this
  480. * was a simple if(), but as we could be racing with other
  481. * threads and soabort() requires dropping locks, we must
  482. * loop waiting for the condition to be true.
  483. */
  484. while (head->so_incqlen > head->so_qlimit) {
  485. struct socket *sp;
  486. sp = TAILQ_FIRST(&head->so_incomp);
  487. TAILQ_REMOVE(&head->so_incomp, sp, so_list);
  488. head->so_incqlen--;
  489. sp->so_qstate &= ~SQ_INCOMP;
  490. sp->so_head = NULL;
  491. ACCEPT_UNLOCK();
  492. soabort(sp);
  493. ACCEPT_LOCK();
  494. }
  495. TAILQ_INSERT_TAIL(&head->so_incomp, so, so_list);
  496. so->so_qstate |= SQ_INCOMP;
  497. head->so_incqlen++;
  498. }
  499. ACCEPT_UNLOCK();
  500. if (connstatus) {
  501. sorwakeup(head);
  502. wakeup_one(&head->so_timeo);
  503. }
  504. return (so);
  505. }
  506. #ifdef PASSIVE_INET
  507. /*
  508. * When a new connection is completed on a listening socket that is
  509. * configured for passive reassembly, sonewconn_passive_client is called to
  510. * create a socket representing the client side of the reassembled
  511. * connection. We allocate a new structure, inherit configuration from the
  512. * listening socket, and return this.
  513. * Connstatus may be 0, or SO_ISCONFIRMING, or SO_ISCONNECTING.
  514. *
  515. * Note: the ref count on the socket is 1 on return.
  516. */
  517. struct socket *
  518. sonewconn_passive_client(struct socket *head, int connstatus)
  519. {
  520. struct socket *so;
  521. VNET_ASSERT(head->so_vnet != NULL, ("%s:%d so_vnet is NULL, head=%p",
  522. __func__, __LINE__, head));
  523. so = soalloc(head->so_vnet);
  524. if (so == NULL)
  525. return (NULL);
  526. so->so_head = NULL; /* just inheriting from head, not otherwise associating */
  527. so->so_type = head->so_type;
  528. so->so_options = head->so_options &~ SO_ACCEPTCONN;
  529. so->so_linger = head->so_linger;
  530. so->so_state = head->so_state | SS_NOFDREF;
  531. so->so_fibnum = head->so_fibnum;
  532. so->so_proto = head->so_proto;
  533. so->so_cred = crhold(head->so_cred);
  534. #ifdef MAC
  535. mac_socket_newconn(head, so);
  536. #endif
  537. #ifdef PROMISCUOUS_INET
  538. in_promisc_socket_newconn(head, so);
  539. #endif
  540. knlist_init_mtx(&so->so_rcv.sb_sel.si_note, SOCKBUF_MTX(&so->so_rcv));
  541. knlist_init_mtx(&so->so_snd.sb_sel.si_note, SOCKBUF_MTX(&so->so_snd));
  542. VNET_SO_ASSERT(head);
  543. if (soreserve(so, head->so_snd.sb_hiwat, head->so_rcv.sb_hiwat) ||
  544. (*so->so_proto->pr_usrreqs->pru_attach)(so, 0, NULL)) {
  545. sodealloc(so);
  546. return (NULL);
  547. }
  548. so->so_rcv.sb_lowat = head->so_rcv.sb_lowat;
  549. so->so_snd.sb_lowat = head->so_snd.sb_lowat;
  550. so->so_rcv.sb_timeo = head->so_rcv.sb_timeo;
  551. so->so_snd.sb_timeo = head->so_snd.sb_timeo;
  552. so->so_rcv.sb_flags |= head->so_rcv.sb_flags & SB_AUTOSIZE;
  553. so->so_snd.sb_flags |= head->so_snd.sb_flags & SB_AUTOSIZE;
  554. so->so_state |= connstatus;
  555. return (so);
  556. }
  557. #endif /* PASSIVE_INET */
  558. int
  559. sobind(struct socket *so, struct sockaddr *nam, struct thread *td)
  560. {
  561. int error;
  562. CURVNET_SET(so->so_vnet);
  563. error = (*so->so_proto->pr_usrreqs->pru_bind)(so, nam, td);
  564. CURVNET_RESTORE();
  565. return error;
  566. }
  567. /*
  568. * solisten() transitions a socket from a non-listening state to a listening
  569. * state, but can also be used to update the listen queue depth on an
  570. * existing listen socket. The protocol will call back into the sockets
  571. * layer using solisten_proto_check() and solisten_proto() to check and set
  572. * socket-layer listen state. Call backs are used so that the protocol can
  573. * acquire both protocol and socket layer locks in whatever order is required
  574. * by the protocol.
  575. *
  576. * Protocol implementors are advised to hold the socket lock across the
  577. * socket-layer test and set to avoid races at the socket layer.
  578. */
  579. int
  580. solisten(struct socket *so, int backlog, struct thread *td)
  581. {
  582. int error;
  583. CURVNET_SET(so->so_vnet);
  584. error = (*so->so_proto->pr_usrreqs->pru_listen)(so, backlog, td);
  585. CURVNET_RESTORE();
  586. return error;
  587. }
  588. int
  589. solisten_proto_check(struct socket *so)
  590. {
  591. SOCK_LOCK_ASSERT(so);
  592. if (so->so_state & (SS_ISCONNECTED | SS_ISCONNECTING |
  593. SS_ISDISCONNECTING))
  594. return (EINVAL);
  595. return (0);
  596. }
  597. void
  598. solisten_proto(struct socket *so, int backlog)
  599. {
  600. SOCK_LOCK_ASSERT(so);
  601. if (backlog < 0 || backlog > somaxconn)
  602. backlog = somaxconn;
  603. so->so_qlimit = backlog;
  604. so->so_options |= SO_ACCEPTCONN;
  605. }
  606. /*
  607. * Evaluate the reference count and named references on a socket; if no
  608. * references remain, free it. This should be called whenever a reference is
  609. * released, such as in sorele(), but also when named reference flags are
  610. * cleared in socket or protocol code.
  611. *
  612. * sofree() will free the socket if:
  613. *
  614. * - There are no outstanding file descriptor references or related consumers
  615. * (so_count == 0).
  616. *
  617. * - The socket has been closed by user space, if ever open (SS_NOFDREF).
  618. *
  619. * - The protocol does not have an outstanding strong reference on the socket
  620. * (SS_PROTOREF).
  621. *
  622. * - The socket is not in a completed connection queue, so a process has been
  623. * notified that it is present. If it is removed, the user process may
  624. * block in accept() despite select() saying the socket was ready.
  625. */
  626. void
  627. sofree(struct socket *so)
  628. {
  629. struct protosw *pr = so->so_proto;
  630. struct socket *head;
  631. ACCEPT_LOCK_ASSERT();
  632. SOCK_LOCK_ASSERT(so);
  633. if ((so->so_state & SS_NOFDREF) == 0 || so->so_count != 0 ||
  634. (so->so_state & SS_PROTOREF) || (so->so_qstate & SQ_COMP)) {
  635. SOCK_UNLOCK(so);
  636. ACCEPT_UNLOCK();
  637. return;
  638. }
  639. head = so->so_head;
  640. if (head != NULL) {
  641. KASSERT((so->so_qstate & SQ_COMP) != 0 ||
  642. (so->so_qstate & SQ_INCOMP) != 0,
  643. ("sofree: so_head != NULL, but neither SQ_COMP nor "
  644. "SQ_INCOMP"));
  645. KASSERT((so->so_qstate & SQ_COMP) == 0 ||
  646. (so->so_qstate & SQ_INCOMP) == 0,
  647. ("sofree: so->so_qstate is SQ_COMP and also SQ_INCOMP"));
  648. TAILQ_REMOVE(&head->so_incomp, so, so_list);
  649. head->so_incqlen--;
  650. so->so_qstate &= ~SQ_INCOMP;
  651. so->so_head = NULL;
  652. }
  653. KASSERT((so->so_qstate & SQ_COMP) == 0 &&
  654. (so->so_qstate & SQ_INCOMP) == 0,
  655. ("sofree: so_head == NULL, but still SQ_COMP(%d) or SQ_INCOMP(%d)",
  656. so->so_qstate & SQ_COMP, so->so_qstate & SQ_INCOMP));
  657. if (so->so_options & SO_ACCEPTCONN) {
  658. KASSERT((TAILQ_EMPTY(&so->so_comp)), ("sofree: so_comp populated"));
  659. KASSERT((TAILQ_EMPTY(&so->so_incomp)), ("sofree: so_comp populated"));
  660. }
  661. SOCK_UNLOCK(so);
  662. ACCEPT_UNLOCK();
  663. VNET_SO_ASSERT(so);
  664. if (pr->pr_flags & PR_RIGHTS && pr->pr_domain->dom_dispose != NULL)
  665. (*pr->pr_domain->dom_dispose)(so->so_rcv.sb_mb);
  666. if (pr->pr_usrreqs->pru_detach != NULL)
  667. (*pr->pr_usrreqs->pru_detach)(so);
  668. /*
  669. * From this point on, we assume that no other references to this
  670. * socket exist anywhere else in the stack. Therefore, no locks need
  671. * to be acquired or held.
  672. *
  673. * We used to do a lot of socket buffer and socket locking here, as
  674. * well as invoke sorflush() and perform wakeups. The direct call to
  675. * dom_dispose() and sbrelease_internal() are an inlining of what was
  676. * necessary from sorflush().
  677. *
  678. * Notice that the socket buffer and kqueue state are torn down
  679. * before calling pru_detach. This means that protocols shold not
  680. * assume they can perform socket wakeups, etc, in their detach code.
  681. */
  682. sbdestroy(&so->so_snd, so);
  683. sbdestroy(&so->so_rcv, so);
  684. seldrain(&so->so_snd.sb_sel);
  685. seldrain(&so->so_rcv.sb_sel);
  686. knlist_destroy(&so->so_rcv.sb_sel.si_note);
  687. knlist_destroy(&so->so_snd.sb_sel.si_note);
  688. sodealloc(so);
  689. }
  690. /*
  691. * Close a socket on last file table reference removal. Initiate disconnect
  692. * if connected. Free socket when disconnect complete.
  693. *
  694. * This function will sorele() the socket. Note that soclose() may be called
  695. * prior to the ref count reaching zero. The actual socket structure will
  696. * not be freed until the ref count reaches zero.
  697. */
  698. int
  699. soclose(struct socket *so)
  700. {
  701. int error = 0;
  702. KASSERT(!(so->so_state & SS_NOFDREF), ("soclose: SS_NOFDREF on enter"));
  703. CURVNET_SET(so->so_vnet);
  704. funsetown(&so->so_sigio);
  705. if (so->so_state & SS_ISCONNECTED) {
  706. if ((so->so_state & SS_ISDISCONNECTING) == 0) {
  707. error = sodisconnect(so);
  708. if (error) {
  709. if (error == ENOTCONN)
  710. error = 0;
  711. goto drop;
  712. }
  713. }
  714. if (so->so_options & SO_LINGER) {
  715. if ((so->so_state & SS_ISDISCONNECTING) &&
  716. (so->so_state & SS_NBIO))
  717. goto drop;
  718. while (so->so_state & SS_ISCONNECTED) {
  719. error = tsleep(&so->so_timeo,
  720. PSOCK | PCATCH, "soclos", so->so_linger * hz);
  721. if (error)
  722. break;
  723. }
  724. }
  725. }
  726. drop:
  727. if (so->so_proto->pr_usrreqs->pru_close != NULL)
  728. (*so->so_proto->pr_usrreqs->pru_close)(so);
  729. if (so->so_options & SO_ACCEPTCONN) {
  730. struct socket *sp;
  731. ACCEPT_LOCK();
  732. while ((sp = TAILQ_FIRST(&so->so_incomp)) != NULL) {
  733. TAILQ_REMOVE(&so->so_incomp, sp, so_list);
  734. so->so_incqlen--;
  735. sp->so_qstate &= ~SQ_INCOMP;
  736. sp->so_head = NULL;
  737. ACCEPT_UNLOCK();
  738. soabort(sp);
  739. ACCEPT_LOCK();
  740. }
  741. while ((sp = TAILQ_FIRST(&so->so_comp)) != NULL) {
  742. TAILQ_REMOVE(&so->so_comp, sp, so_list);
  743. so->so_qlen--;
  744. sp->so_qstate &= ~SQ_COMP;
  745. sp->so_head = NULL;
  746. ACCEPT_UNLOCK();
  747. soabort(sp);
  748. ACCEPT_LOCK();
  749. }
  750. ACCEPT_UNLOCK();
  751. }
  752. ACCEPT_LOCK();
  753. SOCK_LOCK(so);
  754. KASSERT((so->so_state & SS_NOFDREF) == 0, ("soclose: NOFDREF"));
  755. so->so_state |= SS_NOFDREF;
  756. sorele(so);
  757. CURVNET_RESTORE();
  758. return (error);
  759. }
  760. /*
  761. * soabort() is used to abruptly tear down a connection, such as when a
  762. * resource limit is reached (listen queue depth exceeded), or if a listen
  763. * socket is closed while there are sockets waiting to be accepted.
  764. *
  765. * This interface is tricky, because it is called on an unreferenced socket,
  766. * and must be called only by a thread that has actually removed the socket
  767. * from the listen queue it was on, or races with other threads are risked.
  768. *
  769. * This interface will call into the protocol code, so must not be called
  770. * with any socket locks held. Protocols do call it while holding their own
  771. * recursible protocol mutexes, but this is something that should be subject
  772. * to review in the future.
  773. */
  774. void
  775. soabort(struct socket *so)
  776. {
  777. /*
  778. * In as much as is possible, assert that no references to this
  779. * socket are held. This is not quite the same as asserting that the
  780. * current thread is responsible for arranging for no references, but
  781. * is as close as we can get for now.
  782. */
  783. KASSERT(so->so_count == 0, ("soabort: so_count"));
  784. KASSERT((so->so_state & SS_PROTOREF) == 0, ("soabort: SS_PROTOREF"));
  785. KASSERT(so->so_state & SS_NOFDREF, ("soabort: !SS_NOFDREF"));
  786. KASSERT((so->so_state & SQ_COMP) == 0, ("soabort: SQ_COMP"));
  787. KASSERT((so->so_state & SQ_INCOMP) == 0, ("soabort: SQ_INCOMP"));
  788. VNET_SO_ASSERT(so);
  789. if (so->so_proto->pr_usrreqs->pru_abort != NULL)
  790. (*so->so_proto->pr_usrreqs->pru_abort)(so);
  791. ACCEPT_LOCK();
  792. SOCK_LOCK(so);
  793. sofree(so);
  794. }
  795. int
  796. soaccept(struct socket *so, struct sockaddr **nam)
  797. {
  798. int error;
  799. SOCK_LOCK(so);
  800. KASSERT((so->so_state & SS_NOFDREF) != 0, ("soaccept: !NOFDREF"));
  801. so->so_state &= ~SS_NOFDREF;
  802. SOCK_UNLOCK(so);
  803. CURVNET_SET(so->so_vnet);
  804. error = (*so->so_proto->pr_usrreqs->pru_accept)(so, nam);
  805. CURVNET_RESTORE();
  806. return (error);
  807. }
  808. int
  809. soconnect(struct socket *so, struct sockaddr *nam, struct thread *td)
  810. {
  811. int error;
  812. if (so->so_options & SO_ACCEPTCONN)
  813. return (EOPNOTSUPP);
  814. CURVNET_SET(so->so_vnet);
  815. /*
  816. * If protocol is connection-based, can only connect once.
  817. * Otherwise, if connected, try to disconnect first. This allows
  818. * user to disconnect by connecting to, e.g., a null address.
  819. */
  820. if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING) &&
  821. ((so->so_proto->pr_flags & PR_CONNREQUIRED) ||
  822. (error = sodisconnect(so)))) {
  823. error = EISCONN;
  824. } else {
  825. /*
  826. * Prevent accumulated error from previous connection from
  827. * biting us.
  828. */
  829. so->so_error = 0;
  830. error = (*so->so_proto->pr_usrreqs->pru_connect)(so, nam, td);
  831. }
  832. CURVNET_RESTORE();
  833. return (error);
  834. }
  835. int
  836. soconnect2(struct socket *so1, struct socket *so2)
  837. {
  838. int error;
  839. CURVNET_SET(so1->so_vnet);
  840. error = (*so1->so_proto->pr_usrreqs->pru_connect2)(so1, so2);
  841. CURVNET_RESTORE();
  842. return (error);
  843. }
  844. int
  845. sodisconnect(struct socket *so)
  846. {
  847. int error;
  848. if ((so->so_state & SS_ISCONNECTED) == 0)
  849. return (ENOTCONN);
  850. if (so->so_state & SS_ISDISCONNECTING)
  851. return (EALREADY);
  852. VNET_SO_ASSERT(so);
  853. error = (*so->so_proto->pr_usrreqs->pru_disconnect)(so);
  854. return (error);
  855. }
  856. #ifdef ZERO_COPY_SOCKETS
  857. struct so_zerocopy_stats{
  858. int size_ok;
  859. int align_ok;
  860. int found_ifp;
  861. };
  862. struct so_zerocopy_stats so_zerocp_stats = {0,0,0};
  863. #include <netinet/in.h>
  864. #include <net/route.h>
  865. #include <netinet/in_pcb.h>
  866. #include <vm/vm.h>
  867. #include <vm/vm_page.h>
  868. #include <vm/vm_object.h>
  869. /*
  870. * sosend_copyin() is only used if zero copy sockets are enabled. Otherwise
  871. * sosend_dgram() and sosend_generic() use m_uiotombuf().
  872. *
  873. * sosend_copyin() accepts a uio and prepares an mbuf chain holding part or
  874. * all of the data referenced by the uio. If desired, it uses zero-copy.
  875. * *space will be updated to reflect data copied in.
  876. *
  877. * NB: If atomic I/O is requested, the caller must already have checked that
  878. * space can hold resid bytes.
  879. *
  880. * NB: In the event of an error, the caller may need to free the partial
  881. * chain pointed to by *mpp. The contents of both *uio and *space may be
  882. * modified even in the case of an error.
  883. */
  884. static int
  885. sosend_copyin(struct uio *uio, struct mbuf **retmp, int atomic, long *space,
  886. int flags)
  887. {
  888. struct mbuf *m, **mp, *top;
  889. long len;
  890. ssize_t resid;
  891. int error;
  892. #ifdef ZERO_COPY_SOCKETS
  893. int cow_send;
  894. #endif
  895. *retmp = top = NULL;
  896. mp = &top;
  897. len = 0;
  898. resid = uio->uio_resid;
  899. error = 0;
  900. do {
  901. #ifdef ZERO_COPY_SOCKETS
  902. cow_send = 0;
  903. #endif /* ZERO_COPY_SOCKETS */
  904. if (resid >= MINCLSIZE) {
  905. #ifdef ZERO_COPY_SOCKETS
  906. if (top == NULL) {
  907. m = m_gethdr(M_WAITOK, MT_DATA);
  908. m->m_pkthdr.len = 0;
  909. m->m_pkthdr.rcvif = NULL;
  910. } else
  911. m = m_get(M_WAITOK, MT_DATA);
  912. if (so_zero_copy_send &&
  913. resid>=PAGE_SIZE &&
  914. *space>=PAGE_SIZE &&
  915. uio->uio_iov->iov_len>=PAGE_SIZE) {
  916. so_zerocp_stats.size_ok++;
  917. so_zerocp_stats.align_ok++;
  918. cow_send = socow_setup(m, uio);
  919. len = cow_send;
  920. }
  921. if (!cow_send) {
  922. m_clget(m, M_WAITOK);
  923. len = min(min(MCLBYTES, resid), *space);
  924. }
  925. #else /* ZERO_COPY_SOCKETS */
  926. if (top == NULL) {
  927. m = m_getcl(M_WAIT, MT_DATA, M_PKTHDR);
  928. m->m_pkthdr.len = 0;
  929. m->m_pkthdr.rcvif = NULL;
  930. } else
  931. m = m_getcl(M_WAIT, MT_DATA, 0);
  932. len = min(min(MCLBYTES, resid), *space);
  933. #endif /* ZERO_COPY_SOCKETS */
  934. } else {
  935. if (top == NULL) {
  936. m = m_gethdr(M_WAIT, MT_DATA);
  937. m->m_pkthdr.len = 0;
  938. m->m_pkthdr.rcvif = NULL;
  939. len = min(min(MHLEN, resid), *space);
  940. /*
  941. * For datagram protocols, leave room
  942. * for protocol headers in first mbuf.
  943. */
  944. if (atomic && m && len < MHLEN)
  945. MH_ALIGN(m, len);
  946. } else {
  947. m = m_get(M_WAIT, MT_DATA);
  948. len = min(min(MLEN, resid), *space);
  949. }
  950. }
  951. if (m == NULL) {
  952. error = ENOBUFS;
  953. goto out;
  954. }
  955. *space -= len;
  956. #ifdef ZERO_COPY_SOCKETS
  957. if (cow_send)
  958. error = 0;
  959. else
  960. #endif /* ZERO_COPY_SOCKETS */
  961. error = uiomove(mtod(m, void *), (int)len, uio);
  962. resid = uio->uio_resid;
  963. m->m_len = len;
  964. *mp = m;
  965. top->m_pkthdr.len += len;
  966. if (error)
  967. goto out;
  968. mp = &m->m_next;
  969. if (resid <= 0) {
  970. if (flags & MSG_EOR)
  971. top->m_flags |= M_EOR;
  972. break;
  973. }
  974. } while (*space > 0 && atomic);
  975. out:
  976. *retmp = top;
  977. return (error);
  978. }
  979. #endif /*ZERO_COPY_SOCKETS*/
  980. #define SBLOCKWAIT(f) (((f) & MSG_DONTWAIT) ? 0 : SBL_WAIT)
  981. int
  982. sosend_dgram(struct socket *so, struct sockaddr *addr, struct uio *uio,
  983. struct mbuf *top, struct mbuf *control, int flags, struct thread *td)
  984. {
  985. long space;
  986. ssize_t resid;
  987. int clen = 0, error, dontroute;
  988. #ifdef ZERO_COPY_SOCKETS
  989. int atomic = sosendallatonce(so) || top;
  990. #endif
  991. KASSERT(so->so_type == SOCK_DGRAM, ("sodgram_send: !SOCK_DGRAM"));
  992. KASSERT(so->so_proto->pr_flags & PR_ATOMIC,
  993. ("sodgram_send: !PR_ATOMIC"));
  994. if (uio != NULL)
  995. resid = uio->uio_resid;
  996. else
  997. resid = top->m_pkthdr.len;
  998. /*
  999. * In theory resid should be unsigned. However, space must be
  1000. * signed, as it might be less than 0 if we over-committed, and we
  1001. * must use a signed comparison of space and resid. On the other
  1002. * hand, a negative resid causes us to loop sending 0-length
  1003. * segments to the protocol.
  1004. */
  1005. if (resid < 0) {
  1006. error = EINVAL;
  1007. goto out;
  1008. }
  1009. dontroute =
  1010. (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0;
  1011. if (td != NULL)
  1012. td->td_ru.ru_msgsnd++;
  1013. if (control != NULL)
  1014. clen = control->m_len;
  1015. SOCKBUF_LOCK(&so->so_snd);
  1016. if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
  1017. SOCKBUF_UNLOCK(&so->so_snd);
  1018. error = EPIPE;
  1019. goto out;
  1020. }
  1021. if (so->so_error) {
  1022. error = so->so_error;
  1023. so->so_error = 0;
  1024. SOCKBUF_UNLOCK(&so->so_snd);
  1025. goto out;
  1026. }
  1027. if ((so->so_state & SS_ISCONNECTED) == 0) {
  1028. /*
  1029. * `sendto' and `sendmsg' is allowed on a connection-based
  1030. * socket if it supports implied connect. Return ENOTCONN if
  1031. * not connected and no address is supplied.
  1032. */
  1033. if ((so->so_proto->pr_flags & PR_CONNREQUIRED) &&
  1034. (so->so_proto->pr_flags & PR_IMPLOPCL) == 0) {
  1035. if ((so->so_state & SS_ISCONFIRMING) == 0 &&
  1036. !(resid == 0 && clen != 0)) {
  1037. SOCKBUF_UNLOCK(&so->so_snd);
  1038. error = ENOTCONN;
  1039. goto out;
  1040. }
  1041. } else if (addr == NULL) {
  1042. if (so->so_proto->pr_flags & PR_CONNREQUIRED)
  1043. error = ENOTCONN;
  1044. else
  1045. error = EDESTADDRREQ;
  1046. SOCKBUF_UNLOCK(&so->so_snd);
  1047. goto out;
  1048. }
  1049. }
  1050. /*
  1051. * Do we need MSG_OOB support in SOCK_DGRAM? Signs here may be a
  1052. * problem and need fixing.
  1053. */
  1054. space = sbspace(&so->so_snd);
  1055. if (flags & MSG_OOB)
  1056. space += 1024;
  1057. space -= clen;
  1058. SOCKBUF_UNLOCK(&so->so_snd);
  1059. if (resid > space) {
  1060. error = EMSGSIZE;
  1061. goto out;
  1062. }
  1063. if (uio == NULL) {
  1064. resid = 0;
  1065. if (flags & MSG_EOR)
  1066. top->m_flags |= M_EOR;
  1067. } else {
  1068. #ifdef ZERO_COPY_SOCKETS
  1069. error = sosend_copyin(uio, &top, atomic, &space, flags);
  1070. if (error)
  1071. goto out;
  1072. #else
  1073. /*
  1074. * Copy the data from userland into a mbuf chain.
  1075. * If no data is to be copied in, a single empty mbuf
  1076. * is returned.
  1077. */
  1078. top = m_uiotombuf(uio, M_WAITOK, space, max_hdr,
  1079. (M_PKTHDR | ((flags & MSG_EOR) ? M_EOR : 0)));
  1080. if (top == NULL) {
  1081. error = EFAULT; /* only possible error */
  1082. goto out;
  1083. }
  1084. space -= resid - uio->uio_resid;
  1085. #endif
  1086. resid = uio->uio_resid;
  1087. }
  1088. KASSERT(resid == 0, ("sosend_dgram: resid != 0"));
  1089. /*
  1090. * XXXRW: Frobbing SO_DONTROUTE here is even worse without sblock
  1091. * than with.
  1092. */
  1093. if (dontroute) {
  1094. SOCK_LOCK(so);
  1095. so->so_options |= SO_DONTROUTE;
  1096. SOCK_UNLOCK(so);
  1097. }
  1098. /*
  1099. * XXX all the SBS_CANTSENDMORE checks previously done could be out
  1100. * of date. We could have recieved a reset packet in an interrupt or
  1101. * maybe we slept while doing page faults in uiomove() etc. We could
  1102. * probably recheck again inside the locking protection here, but
  1103. * there are probably other places that this also happens. We must
  1104. * rethink this.
  1105. */
  1106. VNET_SO_ASSERT(so);
  1107. error = (*so->so_proto->pr_usrreqs->pru_send)(so,
  1108. (flags & MSG_OOB) ? PRUS_OOB :
  1109. /*
  1110. * If the user set MSG_EOF, the protocol understands this flag and
  1111. * nothing left to send then use PRU_SEND_EOF instead of PRU_SEND.
  1112. */
  1113. ((flags & MSG_EOF) &&
  1114. (so->so_proto->pr_flags & PR_IMPLOPCL) &&
  1115. (resid <= 0)) ?
  1116. PRUS_EOF :
  1117. /* If there is more to send set PRUS_MORETOCOME */
  1118. (resid > 0 && space > 0) ? PRUS_MORETOCOME : 0,
  1119. top, addr, control, td);
  1120. if (dontroute) {
  1121. SOCK_LOCK(so);
  1122. so->so_options &= ~SO_DONTROUTE;
  1123. SOCK_UNLOCK(so);
  1124. }
  1125. clen = 0;
  1126. control = NULL;
  1127. top = NULL;
  1128. out:
  1129. if (top != NULL)
  1130. m_freem(top);
  1131. if (control != NULL)
  1132. m_freem(control);
  1133. return (error);
  1134. }
  1135. /*
  1136. * Send on a socket. If send must go all at once and message is larger than
  1137. * send buffering, then hard error. Lock against other senders. If must go
  1138. * all at once and not enough room now, then inform user that this would
  1139. * block and do nothing. Otherwise, if nonblocking, send as much as
  1140. * possible. The data to be sent is described by "uio" if nonzero, otherwise
  1141. * by the mbuf chain "top" (which must be null if uio is not). Data provided
  1142. * in mbuf chain must be small enough to send all at once.
  1143. *
  1144. * Returns nonzero on error, timeout or signal; callers must check for short
  1145. * counts if EINTR/ERESTART are returned. Data and control buffers are freed
  1146. * on return.
  1147. */
  1148. int
  1149. sosend_generic(struct socket *so, struct sockaddr *addr, struct uio *uio,
  1150. struct mbuf *top, struct mbuf *control, int flags, struct thread *td)
  1151. {
  1152. long space;
  1153. ssize_t resid;
  1154. int clen = 0, error, dontroute;
  1155. int atomic = sosendallatonce(so) || top;
  1156. if (uio != NULL)
  1157. resid = uio->uio_resid;
  1158. else
  1159. resid = top->m_pkthdr.len;
  1160. /*
  1161. * In theory resid should be unsigned. However, space must be
  1162. * signed, as it might be less than 0 if we over-committed, and we
  1163. * must use a signed comparison of space and resid. On the other
  1164. * hand, a negative resid causes us to loop sending 0-length
  1165. * segments to the protocol.
  1166. *
  1167. * Also check to make sure that MSG_EOR isn't used on SOCK_STREAM
  1168. * type sockets since that's an error.
  1169. */
  1170. if (resid < 0 || (so->so_type == SOCK_STREAM && (flags & MSG_EOR))) {
  1171. error = EINVAL;
  1172. goto out;
  1173. }
  1174. dontroute =
  1175. (flags & MSG_DONTROUTE) && (so->so_options & SO_DONTROUTE) == 0 &&
  1176. (so->so_proto->pr_flags & PR_ATOMIC);
  1177. if (td != NULL)
  1178. td->td_ru.ru_msgsnd++;
  1179. if (control != NULL)
  1180. clen = control->m_len;
  1181. error = sblock(&so->so_snd, SBLOCKWAIT(flags));
  1182. if (error)
  1183. goto out;
  1184. restart:
  1185. do {
  1186. SOCKBUF_LOCK(&so->so_snd);
  1187. if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
  1188. SOCKBUF_UNLOCK(&so->so_snd);
  1189. error = EPIPE;
  1190. goto release;
  1191. }
  1192. if (so->so_error) {
  1193. error = so->so_error;
  1194. so->so_error = 0;
  1195. SOCKBUF_UNLOCK(&so->so_snd);
  1196. goto release;
  1197. }
  1198. if ((so->so_state & SS_ISCONNECTED) == 0) {
  1199. /*
  1200. * `sendto' and `sendmsg' is allowed on a connection-
  1201. * based socket if it supports implied connect.
  1202. * Return ENOTCONN if not connected and no address is
  1203. * supplied.
  1204. */
  1205. if ((so->so_proto->pr_flags & PR_CONNREQUIRED) &&
  1206. (so->so_proto->pr_flags & PR_IMPLOPCL) == 0) {
  1207. if ((so->so_state & SS_ISCONFIRMING) == 0 &&
  1208. !(resid == 0 && clen != 0)) {
  1209. SOCKBUF_UNLOCK(&so->so_snd);
  1210. error = ENOTCONN;
  1211. goto release;
  1212. }
  1213. } else if (addr == NULL) {
  1214. SOCKBUF_UNLOCK(&so->so_snd);
  1215. if (so->so_proto->pr_flags & PR_CONNREQUIRED)
  1216. error = ENOTCONN;
  1217. else
  1218. error = EDESTADDRREQ;
  1219. goto release;
  1220. }
  1221. }
  1222. space = sbspace(&so->so_snd);
  1223. if (flags & MSG_OOB)
  1224. space += 1024;
  1225. if ((atomic && resid > so->so_snd.sb_hiwat) ||
  1226. clen > so->so_snd.sb_hiwat) {
  1227. SOCKBUF_UNLOCK(&so->so_snd);
  1228. error = EMSGSIZE;
  1229. goto release;
  1230. }
  1231. if (space < resid + clen &&
  1232. (atomic || space < so->so_snd.sb_lowat || space < clen)) {
  1233. if ((so->so_state & SS_NBIO) || (flags & MSG_NBIO)) {
  1234. if (so->so_upcallprep.soup_send) {
  1235. so->so_upcallprep.soup_send(so,
  1236. so->so_upcallprep.soup_send_arg,
  1237. resid);
  1238. }
  1239. SOCKBUF_UNLOCK(&so->so_snd);
  1240. error = EWOULDBLOCK;
  1241. goto release;
  1242. }
  1243. error = sbwait(&so->so_snd);
  1244. SOCKBUF_UNLOCK(&so->so_snd);
  1245. if (error)
  1246. goto release;
  1247. goto restart;
  1248. }
  1249. SOCKBUF_UNLOCK(&so->so_snd);
  1250. space -= clen;
  1251. do {
  1252. if (uio == NULL) {
  1253. resid = 0;
  1254. if (flags & MSG_EOR)
  1255. top->m_flags |= M_EOR;
  1256. } else {
  1257. #ifdef ZERO_COPY_SOCKETS
  1258. error = sosend_copyin(uio, &top, atomic,
  1259. &space, flags);
  1260. if (error != 0)
  1261. goto release;
  1262. #else
  1263. /*
  1264. * Copy the data from userland into a mbuf
  1265. * chain. If no data is to be copied in,
  1266. * a single empty mbuf is returned.
  1267. */
  1268. top = m_uiotombuf(uio, M_WAITOK, space,
  1269. (atomic ? max_hdr : 0),
  1270. (atomic ? M_PKTHDR : 0) |
  1271. ((flags & MSG_EOR) ? M_EOR : 0));
  1272. if (top == NULL) {
  1273. error = EFAULT; /* only possible error */
  1274. goto release;
  1275. }
  1276. space -= resid - uio->uio_resid;
  1277. #endif
  1278. resid = uio->uio_resid;
  1279. }
  1280. if (dontroute) {
  1281. SOCK_LOCK(so);
  1282. so->so_options |= SO_DONTROUTE;
  1283. SOCK_UNLOCK(so);
  1284. }
  1285. /*
  1286. * XXX all the SBS_CANTSENDMORE checks previously
  1287. * done could be out of date. We could have recieved
  1288. * a reset packet in an interrupt or maybe we slept
  1289. * while doing page faults in uiomove() etc. We
  1290. * could probably recheck again inside the locking
  1291. * protection here, but there are probably other
  1292. * places that this also happens. We must rethink
  1293. * this.
  1294. */
  1295. VNET_SO_ASSERT(so);
  1296. error = (*so->so_proto->pr_usrreqs->pru_send)(so,
  1297. (flags & MSG_OOB) ? PRUS_OOB :
  1298. /*
  1299. * If the user set MSG_EOF, the protocol understands
  1300. * this flag and nothing left to send then use
  1301. * PRU_SEND_EOF instead of PRU_SEND.
  1302. */
  1303. ((flags & MSG_EOF) &&
  1304. (so->so_proto->pr_flags & PR_IMPLOPCL) &&
  1305. (resid <= 0)) ?
  1306. PRUS_EOF :
  1307. /* If there is more to send set PRUS_MORETOCOME. */
  1308. (resid > 0 && space > 0) ? PRUS_MORETOCOME : 0,
  1309. top, addr, control, td);
  1310. if (dontroute) {
  1311. SOCK_LOCK(so);
  1312. so->so_options &= ~SO_DONTROUTE;
  1313. SOCK_UNLOCK(so);
  1314. }
  1315. clen = 0;
  1316. control = NULL;
  1317. top = NULL;
  1318. if (error)
  1319. goto release;
  1320. } while (resid && space > 0);
  1321. } while (resid);
  1322. release:
  1323. sbunlock(&so->so_snd);
  1324. out:
  1325. if (top != NULL)
  1326. m_freem(top);
  1327. if (control != NULL)
  1328. m_freem(control);
  1329. return (error);
  1330. }
  1331. int
  1332. sosend(struct socket *so, struct sockaddr *addr, struct uio *uio,
  1333. struct mbuf *top, struct mbuf *control, int flags, struct thread *td)
  1334. {
  1335. int error;
  1336. CURVNET_SET(so->so_vnet);
  1337. error = so->so_proto->pr_usrreqs->pru_sosend(so, addr, uio, top,
  1338. control, flags, td);
  1339. CURVNET_RESTORE();
  1340. return (error);
  1341. }
  1342. /*
  1343. * The part of soreceive() that implements reading non-inline out-of-band
  1344. * data from a socket. For more complete comments, see soreceive(), from
  1345. * which this code originated.
  1346. *
  1347. * Note that soreceive_rcvoob(), unlike the remainder of soreceive(), is
  1348. * unable to return an mbuf chain to the caller.
  1349. */
  1350. static int
  1351. soreceive_rcvoob(struct socket *so, struct uio *uio, int flags)
  1352. {
  1353. struct protosw *pr = so->so_proto;
  1354. struct mbuf *m;
  1355. int error;
  1356. KASSERT(flags & MSG_OOB, ("soreceive_rcvoob: (flags & MSG_OOB) == 0"));
  1357. VNET_SO_ASSERT(so);
  1358. m = m_get(M_WAIT, MT_DATA);
  1359. error = (*pr->pr_usrreqs->pru_rcvoob)(so, m, flags & MSG_PEEK);
  1360. if (error)
  1361. goto bad;
  1362. do {
  1363. #ifdef ZERO_COPY_SOCKETS
  1364. if (so_zero_copy_receive) {
  1365. int disposable;
  1366. if ((m->m_flags & M_EXT)
  1367. && (m->m_ext.ext_type == EXT_DISPOSABLE))
  1368. disposable = 1;
  1369. else
  1370. disposable = 0;
  1371. error = uiomoveco(mtod(m, void *),
  1372. min(uio->uio_resid, m->m_len),
  1373. uio, disposable);
  1374. } else
  1375. #endif /* ZERO_COPY_SOCKETS */
  1376. error = uiomove(mtod(m, void *),
  1377. (int) min(uio->uio_resid, m->m_len), uio);
  1378. m = m_free(m);
  1379. } while (uio->uio_resid && error == 0 && m);
  1380. bad:
  1381. if (m != NULL)
  1382. m_freem(m);
  1383. return (error);
  1384. }
  1385. /*
  1386. * Following replacement or removal of the first mbuf on the first mbuf chain
  1387. * of a socket buffer, push necessary state changes back into the socket
  1388. * buffer so that other consumers see the values consistently. 'nextrecord'
  1389. * is the callers locally stored value of the original value of
  1390. * sb->sb_mb->m_nextpkt which must be restored when the lead mbuf changes.
  1391. * NOTE: 'nextrecord' may be NULL.
  1392. */
  1393. static __inline void
  1394. sockbuf_pushsync(struct sockbuf *sb, struct mbuf *nextrecord)
  1395. {
  1396. SOCKBUF_LOCK_ASSERT(sb);
  1397. /*
  1398. * First, update for the new value of nextrecord. If necessary, make
  1399. * it the first record.
  1400. */
  1401. if (sb->sb_mb != NULL)
  1402. sb->sb_mb->m_nextpkt = nextrecord;
  1403. else
  1404. sb->sb_mb = nextrecord;
  1405. /*
  1406. * Now update any dependent socket buffer fields to reflect the new
  1407. * state. This is an expanded inline of SB_EMPTY_FIXUP(), with the
  1408. * addition of a second clause that takes care of the case where
  1409. * sb_mb has been updated, but remains the last record.
  1410. */
  1411. if (sb->sb_mb == NULL) {
  1412. sb->sb_mbtail = NULL;
  1413. sb->sb_lastrecord = NULL;
  1414. } else if (sb->sb_mb->m_nextpkt == NULL)
  1415. sb->sb_lastrecord = sb->sb_mb;
  1416. }
  1417. /*
  1418. * Implement receive operations on a socket. We depend on the way that
  1419. * records are added to the sockbuf by sbappend. In particular, each record
  1420. * (mbufs linked through m_next) must begin with an address if the protocol
  1421. * so specifies, followed by an optional mbuf or mbufs containing ancillary
  1422. * data, and then zero or more mbufs of data. In order to allow parallelism
  1423. * between network receive and copying to user space, as well as avoid
  1424. * sleeping with a mutex held, we release the socket buffer mutex during the
  1425. * user space copy. Although the sockbuf is locked, new data may still be
  1426. * appended, and thus we must maintain consistency of the sockbuf during that
  1427. * time.
  1428. *
  1429. * The caller may receive the data as a single mbuf chain by supplying an
  1430. * mbuf **mp0 for use in returning the chain. The uio is then used only for
  1431. * the count in uio_resid.
  1432. */
  1433. int
  1434. soreceive_generic(struct socket *so, struct sockaddr **psa, struct uio *uio,
  1435. struct mbuf **mp0, struct mbuf **controlp, int *flagsp)
  1436. {
  1437. struct mbuf *m, **mp;
  1438. int flags, error, offset;
  1439. ssize_t len;
  1440. struct protosw *pr = so->so_proto;
  1441. struct mbuf *nextrecord;
  1442. int moff, type = 0, last_m_flags, hole_break = 0;
  1443. ssize_t orig_resid = uio->uio_resid;
  1444. mp = mp0;
  1445. if (psa != NULL)
  1446. *psa = NULL;
  1447. if (controlp != NULL)
  1448. *controlp = NULL;
  1449. if (flagsp != NULL) {
  1450. hole_break = *flagsp & MSG_HOLE_BREAK;
  1451. *flagsp &= ~MSG_HOLE_BREAK;
  1452. flags = *flagsp &~ MSG_EOR;
  1453. } else
  1454. flags = 0;
  1455. if (flags & MSG_OOB)
  1456. return (soreceive_rcvoob(so, uio, flags));
  1457. if (mp != NULL)
  1458. *mp = NULL;
  1459. if ((pr->pr_flags & PR_WANTRCVD) && (so->so_state & SS_ISCONFIRMING)
  1460. && uio->uio_resid) {
  1461. VNET_SO_ASSERT(so);
  1462. (*pr->pr_usrreqs->pru_rcvd)(so, 0);
  1463. }
  1464. error = sblock(&so->so_rcv, SBLOCKWAIT(flags));
  1465. if (error)
  1466. return (error);
  1467. restart:
  1468. SOCKBUF_LOCK(&so->so_rcv);
  1469. m = so->so_rcv.sb_mb;
  1470. /*
  1471. * If we have less data than requested, block awaiting more (subject
  1472. * to any timeout) if:
  1473. * 1. the current count is less than the low water mark, or
  1474. * 2. MSG_WAITALL is set, and it is possible to do the entire
  1475. * receive operation at once if we block (resid <= hiwat).
  1476. * 3. MSG_DONTWAIT is not set
  1477. * If MSG_WAITALL is set but resid is larger than the receive buffer,
  1478. * we have to do the receive in sections, and thus risk returning a
  1479. * short count if a timeout or signal occurs after we start.
  1480. */
  1481. if (m == NULL || (((flags & MSG_DONTWAIT) == 0 &&
  1482. so->so_rcv.sb_cc < uio->uio_resid) &&
  1483. (so->so_rcv.sb_cc < so->so_rcv.sb_lowat ||
  1484. ((flags & MSG_WAITALL) && uio->uio_resid <= so->so_rcv.sb_hiwat)) &&
  1485. m->m_nextpkt == NULL && (pr->pr_flags & PR_ATOMIC) == 0)) {
  1486. KASSERT(m != NULL || !so->so_rcv.sb_cc,
  1487. ("receive: m == %p so->so_rcv.sb_cc == %u",
  1488. m, so->so_rcv.sb_cc));
  1489. if (so->so_error) {
  1490. if (m != NULL)
  1491. goto dontblock;
  1492. error = so->so_error;
  1493. if ((flags & MSG_PEEK) == 0)
  1494. so->so_error = 0;
  1495. SOCKBUF_UNLOCK(&so->so_rcv);
  1496. goto release;
  1497. }
  1498. SOCKBUF_LOCK_ASSERT(&so->so_rcv);
  1499. if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
  1500. if (m == NULL) {
  1501. SOCKBUF_UNLOCK(&so->so_rcv);
  1502. goto release;
  1503. } else
  1504. goto dontblock;
  1505. }
  1506. for (; m != NULL; m = m->m_next)
  1507. if (m->m_type == MT_OOBDATA || (m->m_flags & M_EOR)) {
  1508. m = so->so_rcv.sb_mb;
  1509. goto dontblock;
  1510. }
  1511. if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0 &&
  1512. (so->so_proto->pr_flags & PR_CONNREQUIRED)) {
  1513. SOCKBUF_UNLOCK(&so->so_rcv);
  1514. error = ENOTCONN;
  1515. goto release;
  1516. }
  1517. if (uio->uio_resid == 0) {
  1518. SOCKBUF_UNLOCK(&so->so_rcv);
  1519. goto release;
  1520. }
  1521. if ((so->so_state & SS_NBIO) ||
  1522. (flags & (MSG_DONTWAIT|MSG_NBIO))) {
  1523. if (so->so_upcallprep.soup_receive != NULL) {
  1524. so->so_upcallprep.soup_receive(so,
  1525. so->so_upcallprep.soup_receive_arg,
  1526. orig_resid - uio->uio_resid, uio->uio_resid);
  1527. }
  1528. SOCKBUF_UNLOCK(&so->so_rcv);
  1529. error = EWOULDBLOCK;
  1530. goto release;
  1531. }
  1532. SBLASTRECORDCHK(&so->so_rcv);
  1533. SBLASTMBUFCHK(&so->so_rcv);
  1534. error = sbwait(&so->so_rcv);
  1535. SOCKBUF_UNLOCK(&so->so_rcv);
  1536. if (error)
  1537. goto release;
  1538. goto restart;
  1539. }
  1540. dontblock:
  1541. /*
  1542. * From this point onward, we maintain 'nextrecord' as a cache of the
  1543. * pointer to the next record in the socket buffer. We must keep the
  1544. * various socket buffer pointers and local stack versions of the
  1545. * pointers in sync, pushing out modifications before dropping the
  1546. * socket buffer mutex, and re-reading them when picking it up.
  1547. *
  1548. * Otherwise, we will race with the network stack appending new data
  1549. * or records onto the socket buffer by using inconsistent/stale
  1550. * versions of the field, possibly resulting in socket buffer
  1551. * corruption.
  1552. *
  1553. * By holding the high-level sblock(), we prevent simultaneous
  1554. * readers from pulling off the front of the socket buffer.
  1555. */
  1556. SOCKBUF_LOCK_ASSERT(&so->so_rcv);
  1557. if (uio->uio_td)
  1558. uio->uio_td->td_ru.ru_msgrcv++;
  1559. KASSERT(m == so->so_rcv.sb_mb, ("soreceive: m != so->so_rcv.sb_mb"));
  1560. SBLASTRECORDCHK(&so->so_rcv);
  1561. SBLASTMBUFCHK(&so->so_rcv);
  1562. nextrecord = m->m_nextpkt;
  1563. if (pr->pr_flags & PR_ADDR) {
  1564. KASSERT(m->m_type == MT_SONAME,
  1565. ("m->m_type == %d", m->m_type));
  1566. orig_resid = 0;
  1567. if (psa != NULL)
  1568. *psa = sodupsockaddr(mtod(m, struct sockaddr *),
  1569. M_NOWAIT);
  1570. if (flags & MSG_PEEK) {
  1571. m = m->m_next;
  1572. } else {
  1573. sbfree(&so->so_rcv, m);
  1574. so->so_rcv.sb_mb = m_free(m);
  1575. m = so->so_rcv.sb_mb;
  1576. sockbuf_pushsync(&so->so_rcv, nextrecord);
  1577. }
  1578. }
  1579. /*
  1580. * Process one or more MT_CONTROL mbufs present before any data mbufs
  1581. * in the first mbuf chain on the socket buffer. If MSG_PEEK, we
  1582. * just copy the data; if !MSG_PEEK, we call into the protocol to
  1583. * perform externalization (or freeing if controlp == NULL).
  1584. */
  1585. if (m != NULL && m->m_type == MT_CONTROL) {
  1586. struct mbuf *cm = NULL, *cmn;
  1587. struct mbuf **cme = &cm;
  1588. do {
  1589. if (flags & MSG_PEEK) {
  1590. if (controlp != NULL) {
  1591. *controlp = m_copy(m, 0, m->m_len);
  1592. controlp = &(*controlp)->m_next;
  1593. }
  1594. m = m->m_next;
  1595. } else {
  1596. sbfree(&so->so_rcv, m);
  1597. so->so_rcv.sb_mb = m->m_next;
  1598. m->m_next = NULL;
  1599. *cme = m;
  1600. cme = &(*cme)->m_next;
  1601. m = so->so_rcv.sb_mb;
  1602. }
  1603. } while (m != NULL && m->m_type == MT_CONTROL);
  1604. if ((flags & MSG_PEEK) == 0)
  1605. sockbuf_pushsync(&so->so_rcv, nextrecord);
  1606. while (cm != NULL) {
  1607. cmn = cm->m_next;
  1608. cm->m_next = NULL;
  1609. if (pr->pr_domain->dom_externalize != NULL) {
  1610. SOCKBUF_UNLOCK(&so->so_rcv);
  1611. VNET_SO_ASSERT(so);
  1612. error = (*pr->pr_domain->dom_externalize)
  1613. (cm, controlp);
  1614. SOCKBUF_LOCK(&so->so_rcv);
  1615. } else if (controlp != NULL)
  1616. *controlp = cm;
  1617. else
  1618. m_freem(cm);
  1619. if (controlp != NULL) {
  1620. orig_resid = 0;
  1621. while (*controlp != NULL)
  1622. controlp = &(*controlp)->m_next;
  1623. }
  1624. cm = cmn;
  1625. }
  1626. if (m != NULL)
  1627. nextrecord = so->so_rcv.sb_mb->m_nextpkt;
  1628. else
  1629. nextrecord = so->so_rcv.sb_mb;
  1630. orig_resid = 0;
  1631. }
  1632. if (m != NULL) {
  1633. if ((flags & MSG_PEEK) == 0) {
  1634. KASSERT(m->m_nextpkt == nextrecord,
  1635. ("soreceive: post-control, nextrecord !sync"));
  1636. if (nextrecord == NULL) {
  1637. KASSERT(so->so_rcv.sb_mb == m,
  1638. ("soreceive: post-control, sb_mb!=m"));
  1639. KASSERT(so->so_rcv.sb_lastrecord == m,
  1640. ("soreceive: post-control, lastrecord!=m"));
  1641. }
  1642. }
  1643. type = m->m_type;
  1644. if (type == MT_OOBDATA)
  1645. flags |= MSG_OOB;
  1646. last_m_flags = m->m_flags;
  1647. if (hole_break && (m->m_flags & M_HOLE))
  1648. flags |= MSG_HOLE_BREAK;
  1649. } else {
  1650. if ((flags & MSG_PEEK) == 0) {
  1651. KASSERT(so->so_rcv.sb_mb == nextrecord,
  1652. ("soreceive: sb_mb != nextrecord"));
  1653. if (so->so_rcv.sb_mb == NULL) {
  1654. KASSERT(so->so_rcv.sb_lastrecord == NULL,
  1655. ("soreceive: sb_lastercord != NULL"));
  1656. }
  1657. }
  1658. }
  1659. SOCKBUF_LOCK_ASSERT(&so->so_rcv);
  1660. SBLASTRECORDCHK(&so->so_rcv);
  1661. SBLASTMBUFCHK(&so->so_rcv);
  1662. /*
  1663. * Now continue to read any data mbufs off of the head of the socket
  1664. * buffer until the read request is satisfied. Note that 'type' is
  1665. * used to store the type of any mbuf reads that have happened so far
  1666. * such that soreceive() can stop reading if the type changes, which
  1667. * causes soreceive() to return only one of regular data and inline
  1668. * out-of-band data in a single socket receive operation.
  1669. */
  1670. moff = 0;
  1671. offset = 0;
  1672. while (m != NULL && uio->uio_resid > 0 && error == 0) {
  1673. /*
  1674. * If the type of mbuf has changed since the last mbuf
  1675. * examined ('type'), end the receive operation.
  1676. */
  1677. SOCKBUF_LOCK_ASSERT(&so->so_rcv);
  1678. if (hole_break &&
  1679. ((m->m_flags ^ last_m_flags) & M_HOLE))
  1680. break;
  1681. last_m_flags = m->m_flags;
  1682. if (m->m_type == MT_OOBDATA) {
  1683. if (type != MT_OOBDATA)
  1684. break;
  1685. } else if (type == MT_OOBDATA)
  1686. break;
  1687. else
  1688. KASSERT(m->m_type == MT_DATA,
  1689. ("m->m_type == %d", m->m_type));
  1690. so->so_rcv.sb_state &= ~SBS_RCVATMARK;
  1691. len = uio->uio_resid;
  1692. if (so->so_oobmark && len > so->so_oobmark - offset)
  1693. len = so->so_oobmark - offset;
  1694. if (len > m->m_len - moff)
  1695. len = m->m_len - moff;
  1696. /*
  1697. * If mp is set, just pass back the mbufs. Otherwise copy
  1698. * them out via the uio, then free. Sockbuf must be
  1699. * consistent here (points to current mbuf, it points to next
  1700. * record) when we drop priority; we must note any additions
  1701. * to the sockbuf when we block interrupts again.
  1702. */
  1703. if (mp == NULL) {
  1704. SOCKBUF_LOCK_ASSERT(&so->so_rcv);
  1705. SBLASTRECORDCHK(&so->so_rcv);
  1706. SBLASTMBUFCHK(&so->so_rcv);
  1707. SOCKBUF_UNLOCK(&so->so_rcv);
  1708. #ifdef ZERO_COPY_SOCKETS
  1709. if (so_zero_copy_receive) {
  1710. int disposable;
  1711. if ((m->m_flags & M_EXT)
  1712. && (m->m_ext.ext_type == EXT_DISPOSABLE))
  1713. disposable = 1;
  1714. else
  1715. disposable = 0;
  1716. error = uiomoveco(mtod(m, char *) + moff,
  1717. (int)len, uio,
  1718. disposable);
  1719. } else
  1720. #endif /* ZERO_COPY_SOCKETS */
  1721. if (m->m_flags & M_HOLE)
  1722. error = uiofill(0, (int)len, uio);
  1723. else
  1724. error = uiomove(mtod(m, char *) + moff, (int)len, uio);
  1725. SOCKBUF_LOCK(&so->so_rcv);
  1726. if (error) {
  1727. /*
  1728. * The MT_SONAME mbuf has already been removed
  1729. * from the record, so it is necessary to
  1730. * remove the data mbufs, if any, to preserve
  1731. * the invariant in the case of PR_ADDR that
  1732. * requires MT_SONAME mbufs at the head of
  1733. * each record.
  1734. */
  1735. if (m && pr->pr_flags & PR_ATOMIC &&
  1736. ((flags & MSG_PEEK) == 0))
  1737. (void)sbdroprecord_locked(&so->so_rcv);
  1738. SOCKBUF_UNLOCK(&so->so_rcv);
  1739. goto release;
  1740. }
  1741. } else
  1742. uio->uio_resid -= len;
  1743. SOCKBUF_LOCK_ASSERT(&so->so_rcv);
  1744. if (len == m->m_len - moff) {
  1745. if (m->m_flags & M_EOR)
  1746. flags |= MSG_EOR;
  1747. if (flags & MSG_PEEK) {
  1748. m = m->m_next;
  1749. moff = 0;
  1750. } else {
  1751. nextrecord = m->m_nextpkt;
  1752. sbfree(&so->so_rcv, m);
  1753. if (mp != NULL) {
  1754. *mp = m;
  1755. mp = &m->m_next;
  1756. so->so_rcv.sb_mb = m = m->m_next;
  1757. *mp = NULL;
  1758. } else {
  1759. so->so_rcv.sb_mb = m_free(m);
  1760. m = so->so_rcv.sb_mb;
  1761. }
  1762. sockbuf_pushsync(&so->so_rcv, nextrecord);
  1763. SBLASTRECORDCHK(&so->so_rcv);
  1764. SBLASTMBUFCHK(&so->so_rcv);
  1765. }
  1766. } else {
  1767. if (flags & MSG_PEEK)
  1768. moff += len;
  1769. else {
  1770. if (mp != NULL) {
  1771. int copy_flag;
  1772. if (flags & MSG_DONTWAIT)
  1773. copy_flag = M_DONTWAIT;
  1774. else
  1775. copy_flag = M_WAIT;
  1776. if (copy_flag == M_WAIT)
  1777. SOCKBUF_UNLOCK(&so->so_rcv);
  1778. *mp = m_copym2(m, 0, len, copy_flag,
  1779. hole_break);
  1780. if (copy_flag == M_WAIT)
  1781. SOCKBUF_LOCK(&so->so_rcv);
  1782. if (*mp == NULL) {
  1783. /*
  1784. * m_copym() couldn't
  1785. * allocate an mbuf. Adjust
  1786. * uio_resid back (it was
  1787. * adjusted down by len
  1788. * bytes, which we didn't end
  1789. * up "copying" over).
  1790. */
  1791. uio->uio_resid += len;
  1792. break;
  1793. }
  1794. }
  1795. if ((m->m_flags & M_HOLE) == 0)
  1796. m->m_data += len;
  1797. m->m_len -= len;
  1798. so->so_rcv.sb_cc -= len;
  1799. }
  1800. }
  1801. SOCKBUF_LOCK_ASSERT(&so->so_rcv);
  1802. if (so->so_oobmark) {
  1803. if ((flags & MSG_PEEK) == 0) {
  1804. so->so_oobmark -= len;
  1805. if (so->so_oobmark == 0) {
  1806. so->so_rcv.sb_state |= SBS_RCVATMARK;
  1807. break;
  1808. }
  1809. } else {
  1810. offset += len;
  1811. if (offset == so->so_oobmark)
  1812. break;
  1813. }
  1814. }
  1815. if (flags & MSG_EOR)
  1816. break;
  1817. /*
  1818. * If the MSG_WAITALL flag is set (for non-atomic socket), we
  1819. * must not quit until "uio->uio_resid == 0" or an error
  1820. * termination. If a signal/timeout occurs, return with a
  1821. * short count but without error. Keep sockbuf locked
  1822. * against other readers.
  1823. */
  1824. while (flags & MSG_WAITALL && m == NULL && uio->uio_resid > 0 &&
  1825. !sosendallatonce(so) && nextrecord == NULL) {
  1826. SOCKBUF_LOCK_ASSERT(&so->so_rcv);
  1827. if (so->so_error || so->so_rcv.sb_state & SBS_CANTRCVMORE)
  1828. break;
  1829. /*
  1830. * Notify the protocol that some data has been
  1831. * drained before blocking.
  1832. */
  1833. if (pr->pr_flags & PR_WANTRCVD) {
  1834. SOCKBUF_UNLOCK(&so->so_rcv);
  1835. VNET_SO_ASSERT(so);
  1836. (*pr->pr_usrreqs->pru_rcvd)(so, flags);
  1837. SOCKBUF_LOCK(&so->so_rcv);
  1838. }
  1839. SBLASTRECORDCHK(&so->so_rcv);
  1840. SBLASTMBUFCHK(&so->so_rcv);
  1841. /*
  1842. * We could receive some data while was notifying
  1843. * the protocol. Skip blocking in this case.
  1844. */
  1845. if (so->so_rcv.sb_mb == NULL) {
  1846. error = sbwait(&so->so_rcv);
  1847. if (error) {
  1848. SOCKBUF_UNLOCK(&so->so_rcv);
  1849. goto release;
  1850. }
  1851. }
  1852. m = so->so_rcv.sb_mb;
  1853. if (m != NULL)
  1854. nextrecord = m->m_nextpkt;
  1855. }
  1856. }
  1857. SOCKBUF_LOCK_ASSERT(&so->so_rcv);
  1858. if (m != NULL && pr->pr_flags & PR_ATOMIC) {
  1859. flags |= MSG_TRUNC;
  1860. if ((flags & MSG_PEEK) == 0)
  1861. (void) sbdroprecord_locked(&so->so_rcv);
  1862. }
  1863. if ((flags & MSG_PEEK) == 0) {
  1864. if (m == NULL) {
  1865. /*
  1866. * First part is an inline SB_EMPTY_FIXUP(). Second
  1867. * part makes sure sb_lastrecord is up-to-date if
  1868. * there is still data in the socket buffer.
  1869. */
  1870. so->so_rcv.sb_mb = nextrecord;
  1871. if (so->so_rcv.sb_mb == NULL) {
  1872. so->so_rcv.sb_mbtail = NULL;
  1873. so->so_rcv.sb_lastrecord = NULL;
  1874. } else if (nextrecord->m_nextpkt == NULL)
  1875. so->so_rcv.sb_lastrecord = nextrecord;
  1876. if (uio->uio_resid > 0 && orig_resid != uio->uio_resid
  1877. && !sosendallatonce(so) && nextrecord == NULL) {
  1878. if (so->so_upcallprep.soup_receive != NULL) {
  1879. so->so_upcallprep.soup_receive(so,
  1880. so->so_upcallprep.soup_receive_arg,
  1881. orig_resid - uio->uio_resid, uio->uio_resid);
  1882. }
  1883. }
  1884. }
  1885. SBLASTRECORDCHK(&so->so_rcv);
  1886. SBLASTMBUFCHK(&so->so_rcv);
  1887. /*
  1888. * If soreceive() is being done from the socket callback,
  1889. * then don't need to generate ACK to peer to update window,
  1890. * since ACK will be generated on return to TCP.
  1891. */
  1892. if (!(flags & MSG_SOCALLBCK) &&
  1893. (pr->pr_flags & PR_WANTRCVD)) {
  1894. SOCKBUF_UNLOCK(&so->so_rcv);
  1895. VNET_SO_ASSERT(so);
  1896. (*pr->pr_usrreqs->pru_rcvd)(so, flags);
  1897. SOCKBUF_LOCK(&so->so_rcv);
  1898. }
  1899. }
  1900. SOCKBUF_LOCK_ASSERT(&so->so_rcv);
  1901. if (orig_resid == uio->uio_resid && orig_resid &&
  1902. (flags & MSG_EOR) == 0 && (so->so_rcv.sb_state & SBS_CANTRCVMORE) == 0) {
  1903. SOCKBUF_UNLOCK(&so->so_rcv);
  1904. goto restart;
  1905. }
  1906. SOCKBUF_UNLOCK(&so->so_rcv);
  1907. if (flagsp != NULL)
  1908. *flagsp |= flags;
  1909. release:
  1910. sbunlock(&so->so_rcv);
  1911. return (error);
  1912. }
  1913. /*
  1914. * Optimized version of soreceive() for stream (TCP) sockets.
  1915. */
  1916. int
  1917. soreceive_stream(struct socket *so, struct sockaddr **psa, struct uio *uio,
  1918. struct mbuf **mp0, struct mbuf **controlp, int *flagsp)
  1919. {
  1920. int len = 0, error = 0, flags, oresid, hole_break = 0;
  1921. struct sockbuf *sb;
  1922. struct mbuf *m, *n = NULL;
  1923. /* We only do stream sockets. */
  1924. if (so->so_type != SOCK_STREAM)
  1925. return (EINVAL);
  1926. if (psa != NULL)
  1927. *psa = NULL;
  1928. if (controlp != NULL)
  1929. return (EINVAL);
  1930. if (flagsp != NULL) {
  1931. hole_break = *flagsp & MSG_HOLE_BREAK;
  1932. *flagsp &= ~MSG_HOLE_BREAK;
  1933. flags = *flagsp &~ MSG_EOR;
  1934. } else
  1935. flags = 0;
  1936. if (flags & MSG_OOB)
  1937. return (soreceive_rcvoob(so, uio, flags));
  1938. if (mp0 != NULL)
  1939. *mp0 = NULL;
  1940. sb = &so->so_rcv;
  1941. /* Prevent other readers from entering the socket. */
  1942. error = sblock(sb, SBLOCKWAIT(flags));
  1943. if (error)
  1944. goto out;
  1945. SOCKBUF_LOCK(sb);
  1946. /* Easy one, no space to copyout anything. */
  1947. if (uio->uio_resid == 0) {
  1948. error = EINVAL;
  1949. goto out;
  1950. }
  1951. oresid = uio->uio_resid;
  1952. /* We will never ever get anything unless we are or were connected. */
  1953. if (!(so->so_state & (SS_ISCONNECTED|SS_ISDISCONNECTED))) {
  1954. error = ENOTCONN;
  1955. goto out;
  1956. }
  1957. restart:
  1958. SOCKBUF_LOCK_ASSERT(&so->so_rcv);
  1959. /* Abort if socket has reported problems. */
  1960. if (so->so_error) {
  1961. if (sb->sb_cc > 0)
  1962. goto deliver;
  1963. if (oresid > uio->uio_resid)
  1964. goto out;
  1965. error = so->so_error;
  1966. if (!(flags & MSG_PEEK))
  1967. so->so_error = 0;
  1968. goto out;
  1969. }
  1970. /* Door is closed. Deliver what is left, if any. */
  1971. if (sb->sb_state & SBS_CANTRCVMORE) {
  1972. if (sb->sb_cc > 0)
  1973. goto deliver;
  1974. else
  1975. goto out;
  1976. }
  1977. /* Socket buffer is empty and we shall not block. */
  1978. if (sb->sb_cc == 0 &&
  1979. ((so->so_state & SS_NBIO) || (flags & (MSG_DONTWAIT|MSG_NBIO)))) {
  1980. error = EAGAIN;
  1981. goto out;
  1982. }
  1983. /* Socket buffer got some data that we shall deliver now. */
  1984. if (sb->sb_cc > 0 && !(flags & MSG_WAITALL) &&
  1985. ((sb->sb_flags & SS_NBIO) ||
  1986. (flags & (MSG_DONTWAIT|MSG_NBIO)) ||
  1987. sb->sb_cc >= sb->sb_lowat ||
  1988. sb->sb_cc >= uio->uio_resid ||
  1989. sb->sb_cc >= sb->sb_hiwat) ) {
  1990. goto deliver;
  1991. }
  1992. /* On MSG_WAITALL we must wait until all data or error arrives. */
  1993. if ((flags & MSG_WAITALL) &&
  1994. (sb->sb_cc >= uio->uio_resid || sb->sb_cc >= sb->sb_lowat))
  1995. goto deliver;
  1996. /*
  1997. * Wait and block until (more) data comes in.
  1998. * NB: Drops the sockbuf lock during wait.
  1999. */
  2000. error = sbwait(sb);
  2001. if (error)
  2002. goto out;
  2003. goto restart;
  2004. deliver:
  2005. SOCKBUF_LOCK_ASSERT(&so->so_rcv);
  2006. KASSERT(sb->sb_cc > 0, ("%s: sockbuf empty", __func__));
  2007. KASSERT(sb->sb_mb != NULL, ("%s: sb_mb == NULL", __func__));
  2008. /* Statistics. */
  2009. if (uio->uio_td)
  2010. uio->uio_td->td_ru.ru_msgrcv++;
  2011. /* Fill uio until full or current end of socket buffer is reached. */
  2012. len = min(uio->uio_resid, sb->sb_cc);
  2013. if (hole_break && (sb->sb_mb->m_flags & M_HOLE))
  2014. flags |= MSG_HOLE_BREAK;
  2015. if (mp0 != NULL) {
  2016. /* Dequeue as many mbufs as possible. */
  2017. if (!(flags & MSG_PEEK) && len >= sb->sb_mb->m_len) {
  2018. for (*mp0 = m = sb->sb_mb;
  2019. m != NULL && m->m_len <= len;
  2020. m = m->m_next) {
  2021. len -= m->m_len;
  2022. uio->uio_resid -= m->m_len;
  2023. sbfree(sb, m);
  2024. n = m;
  2025. }
  2026. sb->sb_mb = m;
  2027. if (sb->sb_mb == NULL)
  2028. SB_EMPTY_FIXUP(sb);
  2029. n->m_next = NULL;
  2030. }
  2031. /* Copy the remainder. */
  2032. if (len > 0) {
  2033. KASSERT(sb->sb_mb != NULL,
  2034. ("%s: len > 0 && sb->sb_mb empty", __func__));
  2035. m = m_copym2(sb->sb_mb, 0, len, M_DONTWAIT, hole_break);
  2036. if (m == NULL)
  2037. len = 0; /* Don't flush data from sockbuf. */
  2038. else
  2039. uio->uio_resid -= m->m_len;
  2040. if (*mp0 != NULL)
  2041. n->m_next = m;
  2042. else
  2043. *mp0 = m;
  2044. if (*mp0 == NULL) {
  2045. error = ENOBUFS;
  2046. goto out;
  2047. }
  2048. }
  2049. } else {
  2050. /* NB: Must unlock socket buffer as uiomove may sleep. */
  2051. SOCKBUF_UNLOCK(sb);
  2052. error = m_mbuftouio(uio, sb->sb_mb, len, hole_break);
  2053. SOCKBUF_LOCK(sb);
  2054. if (error)
  2055. goto out;
  2056. }
  2057. SBLASTRECORDCHK(sb);
  2058. SBLASTMBUFCHK(sb);
  2059. /*
  2060. * Remove the delivered data from the socket buffer unless we
  2061. * were only peeking.
  2062. */
  2063. if (!(flags & MSG_PEEK)) {
  2064. if (len > 0)
  2065. sbdrop_locked(sb, len);
  2066. /* Notify protocol that we drained some data. */
  2067. if ((so->so_proto->pr_flags & PR_WANTRCVD) &&
  2068. (((flags & MSG_WAITALL) && uio->uio_resid > 0) ||
  2069. !(flags & MSG_SOCALLBCK))) {
  2070. SOCKBUF_UNLOCK(sb);
  2071. VNET_SO_ASSERT(so);
  2072. (*so->so_proto->pr_usrreqs->pru_rcvd)(so, flags);
  2073. SOCKBUF_LOCK(sb);
  2074. }
  2075. }
  2076. /*
  2077. * For MSG_WAITALL we may have to loop again and wait for
  2078. * more data to come in.
  2079. */
  2080. if ((flags & MSG_WAITALL) && uio->uio_resid > 0)
  2081. goto restart;
  2082. out:
  2083. if (flagsp != NULL)
  2084. *flagsp |= flags;
  2085. SOCKBUF_LOCK_ASSERT(sb);
  2086. SBLASTRECORDCHK(sb);
  2087. SBLASTMBUFCHK(sb);
  2088. SOCKBUF_UNLOCK(sb);
  2089. sbunlock(sb);
  2090. return (error);
  2091. }
  2092. /*
  2093. * Optimized version of soreceive() for simple datagram cases from userspace.
  2094. * Unlike in the stream case, we're able to drop a datagram if copyout()
  2095. * fails, and because we handle datagrams atomically, we don't need to use a
  2096. * sleep lock to prevent I/O interlacing.
  2097. */
  2098. int
  2099. soreceive_dgram(struct socket *so, struct sockaddr **psa, struct uio *uio,
  2100. struct mbuf **mp0, struct mbuf **controlp, int *flagsp)
  2101. {
  2102. struct mbuf *m, *m2;
  2103. int flags, error;
  2104. ssize_t len;
  2105. struct protosw *pr = so->so_proto;
  2106. struct mbuf *nextrecord;
  2107. if (psa != NULL)
  2108. *psa = NULL;
  2109. if (controlp != NULL)
  2110. *controlp = NULL;
  2111. if (flagsp != NULL)
  2112. flags = *flagsp &~ MSG_EOR;
  2113. else
  2114. flags = 0;
  2115. /*
  2116. * For any complicated cases, fall back to the full
  2117. * soreceive_generic().
  2118. */
  2119. if (mp0 != NULL || (flags & MSG_PEEK) || (flags & MSG_OOB))
  2120. return (soreceive_generic(so, psa, uio, mp0, controlp,
  2121. flagsp));
  2122. /*
  2123. * Enforce restrictions on use.
  2124. */
  2125. KASSERT((pr->pr_flags & PR_WANTRCVD) == 0,
  2126. ("soreceive_dgram: wantrcvd"));
  2127. KASSERT(pr->pr_flags & PR_ATOMIC, ("soreceive_dgram: !atomic"));
  2128. KASSERT((so->so_rcv.sb_state & SBS_RCVATMARK) == 0,
  2129. ("soreceive_dgram: SBS_RCVATMARK"));
  2130. KASSERT((so->so_proto->pr_flags & PR_CONNREQUIRED) == 0,
  2131. ("soreceive_dgram: P_CONNREQUIRED"));
  2132. /*
  2133. * Loop blocking while waiting for a datagram.
  2134. */
  2135. SOCKBUF_LOCK(&so->so_rcv);
  2136. while ((m = so->so_rcv.sb_mb) == NULL) {
  2137. KASSERT(so->so_rcv.sb_cc == 0,
  2138. ("soreceive_dgram: sb_mb NULL but sb_cc %u",
  2139. so->so_rcv.sb_cc));
  2140. if (so->so_error) {
  2141. error = so->so_error;
  2142. so->so_error = 0;
  2143. SOCKBUF_UNLOCK(&so->so_rcv);
  2144. return (error);
  2145. }
  2146. if (so->so_rcv.sb_state & SBS_CANTRCVMORE ||
  2147. uio->uio_resid == 0) {
  2148. SOCKBUF_UNLOCK(&so->so_rcv);
  2149. return (0);
  2150. }
  2151. if ((so->so_state & SS_NBIO) ||
  2152. (flags & (MSG_DONTWAIT|MSG_NBIO))) {
  2153. SOCKBUF_UNLOCK(&so->so_rcv);
  2154. return (EWOULDBLOCK);
  2155. }
  2156. SBLASTRECORDCHK(&so->so_rcv);
  2157. SBLASTMBUFCHK(&so->so_rcv);
  2158. error = sbwait(&so->so_rcv);
  2159. if (error) {
  2160. SOCKBUF_UNLOCK(&so->so_rcv);
  2161. return (error);
  2162. }
  2163. }
  2164. SOCKBUF_LOCK_ASSERT(&so->so_rcv);
  2165. if (uio->uio_td)
  2166. uio->uio_td->td_ru.ru_msgrcv++;
  2167. SBLASTRECORDCHK(&so->so_rcv);
  2168. SBLASTMBUFCHK(&so->so_rcv);
  2169. nextrecord = m->m_nextpkt;
  2170. if (nextrecord == NULL) {
  2171. KASSERT(so->so_rcv.sb_lastrecord == m,
  2172. ("soreceive_dgram: lastrecord != m"));
  2173. }
  2174. KASSERT(so->so_rcv.sb_mb->m_nextpkt == nextrecord,
  2175. ("soreceive_dgram: m_nextpkt != nextrecord"));
  2176. /*
  2177. * Pull 'm' and its chain off the front of the packet queue.
  2178. */
  2179. so->so_rcv.sb_mb = NULL;
  2180. sockbuf_pushsync(&so->so_rcv, nextrecord);
  2181. /*
  2182. * Walk 'm's chain and free that many bytes from the socket buffer.
  2183. */
  2184. for (m2 = m; m2 != NULL; m2 = m2->m_next)
  2185. sbfree(&so->so_rcv, m2);
  2186. /*
  2187. * Do a few last checks before we let go of the lock.
  2188. */
  2189. SBLASTRECORDCHK(&so->so_rcv);
  2190. SBLASTMBUFCHK(&so->so_rcv);
  2191. SOCKBUF_UNLOCK(&so->so_rcv);
  2192. if (pr->pr_flags & PR_ADDR) {
  2193. KASSERT(m->m_type == MT_SONAME,
  2194. ("m->m_type == %d", m->m_type));
  2195. if (psa != NULL)
  2196. *psa = sodupsockaddr(mtod(m, struct sockaddr *),
  2197. M_NOWAIT);
  2198. m = m_free(m);
  2199. }
  2200. if (m == NULL) {
  2201. /* XXXRW: Can this happen? */
  2202. return (0);
  2203. }
  2204. /*
  2205. * Packet to copyout() is now in 'm' and it is disconnected from the
  2206. * queue.
  2207. *
  2208. * Process one or more MT_CONTROL mbufs present before any data mbufs
  2209. * in the first mbuf chain on the socket buffer. We call into the
  2210. * protocol to perform externalization (or freeing if controlp ==
  2211. * NULL).
  2212. */
  2213. if (m->m_type == MT_CONTROL) {
  2214. struct mbuf *cm = NULL, *cmn;
  2215. struct mbuf **cme = &cm;
  2216. do {
  2217. m2 = m->m_next;
  2218. m->m_next = NULL;
  2219. *cme = m;
  2220. cme = &(*cme)->m_next;
  2221. m = m2;
  2222. } while (m != NULL && m->m_type == MT_CONTROL);
  2223. while (cm != NULL) {
  2224. cmn = cm->m_next;
  2225. cm->m_next = NULL;
  2226. if (pr->pr_domain->dom_externalize != NULL) {
  2227. error = (*pr->pr_domain->dom_externalize)
  2228. (cm, controlp);
  2229. } else if (controlp != NULL)
  2230. *controlp = cm;
  2231. else
  2232. m_freem(cm);
  2233. if (controlp != NULL) {
  2234. while (*controlp != NULL)
  2235. controlp = &(*controlp)->m_next;
  2236. }
  2237. cm = cmn;
  2238. }
  2239. }
  2240. KASSERT(m->m_type == MT_DATA, ("soreceive_dgram: !data"));
  2241. while (m != NULL && uio->uio_resid > 0) {
  2242. len = uio->uio_resid;
  2243. if (len > m->m_len)
  2244. len = m->m_len;
  2245. error = uiomove(mtod(m, char *), (int)len, uio);
  2246. if (error) {
  2247. m_freem(m);
  2248. return (error);
  2249. }
  2250. if (len == m->m_len)
  2251. m = m_free(m);
  2252. else {
  2253. m->m_data += len;
  2254. m->m_len -= len;
  2255. }
  2256. }
  2257. if (m != NULL)
  2258. flags |= MSG_TRUNC;
  2259. m_freem(m);
  2260. if (flagsp != NULL)
  2261. *flagsp |= flags;
  2262. return (0);
  2263. }
  2264. int
  2265. soreceive(struct socket *so, struct sockaddr **psa, struct uio *uio,
  2266. struct mbuf **mp0, struct mbuf **controlp, int *flagsp)
  2267. {
  2268. int error;
  2269. CURVNET_SET(so->so_vnet);
  2270. error = (so->so_proto->pr_usrreqs->pru_soreceive(so, psa, uio, mp0,
  2271. controlp, flagsp));
  2272. CURVNET_RESTORE();
  2273. return (error);
  2274. }
  2275. int
  2276. soshutdown(struct socket *so, int how)
  2277. {
  2278. struct protosw *pr = so->so_proto;
  2279. int error;
  2280. if (!(how == SHUT_RD || how == SHUT_WR || how == SHUT_RDWR))
  2281. return (EINVAL);
  2282. CURVNET_SET(so->so_vnet);
  2283. if (pr->pr_usrreqs->pru_flush != NULL) {
  2284. (*pr->pr_usrreqs->pru_flush)(so, how);
  2285. }
  2286. if (how != SHUT_WR)
  2287. sorflush(so);
  2288. if (how != SHUT_RD) {
  2289. error = (*pr->pr_usrreqs->pru_shutdown)(so);
  2290. CURVNET_RESTORE();
  2291. return (error);
  2292. }
  2293. CURVNET_RESTORE();
  2294. return (0);
  2295. }
  2296. void
  2297. sorflush(struct socket *so)
  2298. {
  2299. struct sockbuf *sb = &so->so_rcv;
  2300. struct protosw *pr = so->so_proto;
  2301. struct sockbuf asb;
  2302. VNET_SO_ASSERT(so);
  2303. /*
  2304. * In order to avoid calling dom_dispose with the socket buffer mutex
  2305. * held, and in order to generally avoid holding the lock for a long
  2306. * time, we make a copy of the socket buffer and clear the original
  2307. * (except locks, state). The new socket buffer copy won't have
  2308. * initialized locks so we can only call routines that won't use or
  2309. * assert those locks.
  2310. *
  2311. * Dislodge threads currently blocked in receive and wait to acquire
  2312. * a lock against other simultaneous readers before clearing the
  2313. * socket buffer. Don't let our acquire be interrupted by a signal
  2314. * despite any existing socket disposition on interruptable waiting.
  2315. */
  2316. socantrcvmore(so);
  2317. (void) sblock(sb, SBL_WAIT | SBL_NOINTR);
  2318. /*
  2319. * Invalidate/clear most of the sockbuf structure, but leave selinfo
  2320. * and mutex data unchanged.
  2321. */
  2322. SOCKBUF_LOCK(sb);
  2323. bzero(&asb, offsetof(struct sockbuf, sb_startzero));
  2324. bcopy(&sb->sb_startzero, &asb.sb_startzero,
  2325. sizeof(*sb) - offsetof(struct sockbuf, sb_startzero));
  2326. bzero(&sb->sb_startzero,
  2327. sizeof(*sb) - offsetof(struct sockbuf, sb_startzero));
  2328. SOCKBUF_UNLOCK(sb);
  2329. sbunlock(sb);
  2330. /*
  2331. * Dispose of special rights and flush the socket buffer. Don't call
  2332. * any unsafe routines (that rely on locks being initialized) on asb.
  2333. */
  2334. if (pr->pr_flags & PR_RIGHTS && pr->pr_domain->dom_dispose != NULL)
  2335. (*pr->pr_domain->dom_dispose)(asb.sb_mb);
  2336. sbrelease_internal(&asb, so);
  2337. }
  2338. /*
  2339. * Perhaps this routine, and sooptcopyout(), below, ought to come in an
  2340. * additional variant to handle the case where the option value needs to be
  2341. * some kind of integer, but not a specific size. In addition to their use
  2342. * here, these functions are also called by the protocol-level pr_ctloutput()
  2343. * routines.
  2344. */
  2345. int
  2346. sooptcopyin(struct sockopt *sopt, void *buf, size_t len, size_t minlen)
  2347. {
  2348. size_t valsize;
  2349. /*
  2350. * If the user gives us more than we wanted, we ignore it, but if we
  2351. * don't get the minimum length the caller wants, we return EINVAL.
  2352. * On success, sopt->sopt_valsize is set to however much we actually
  2353. * retrieved.
  2354. */
  2355. if ((valsize = sopt->sopt_valsize) < minlen)
  2356. return EINVAL;
  2357. if (valsize > len)
  2358. sopt->sopt_valsize = valsize = len;
  2359. if (sopt->sopt_td != NULL)
  2360. return (copyin(sopt->sopt_val, buf, valsize));
  2361. bcopy(sopt->sopt_val, buf, valsize);
  2362. return (0);
  2363. }
  2364. /*
  2365. * Kernel version of setsockopt(2).
  2366. *
  2367. * XXX: optlen is size_t, not socklen_t
  2368. */
  2369. int
  2370. so_setsockopt(struct socket *so, int level, int optname, void *optval,
  2371. size_t optlen)
  2372. {
  2373. struct sockopt sopt;
  2374. sopt.sopt_level = level;
  2375. sopt.sopt_name = optname;
  2376. sopt.sopt_dir = SOPT_SET;
  2377. sopt.sopt_val = optval;
  2378. sopt.sopt_valsize = optlen;
  2379. sopt.sopt_td = NULL;
  2380. return (sosetopt(so, &sopt));
  2381. }
  2382. int
  2383. sosetopt(struct socket *so, struct sockopt *sopt)
  2384. {
  2385. int error, optval;
  2386. struct linger l;
  2387. struct timeval tv;
  2388. u_long val;
  2389. uint32_t val32;
  2390. #ifdef MAC
  2391. struct mac extmac;
  2392. #endif
  2393. #ifdef PROMISCUOUS_INET
  2394. struct in_l2info l2info;
  2395. #endif
  2396. CURVNET_SET(so->so_vnet);
  2397. error = 0;
  2398. if (sopt->sopt_level != SOL_SOCKET) {
  2399. if (so->so_proto->pr_ctloutput != NULL) {
  2400. error = (*so->so_proto->pr_ctloutput)(so, sopt);
  2401. CURVNET_RESTORE();
  2402. return (error);
  2403. }
  2404. error = ENOPROTOOPT;
  2405. } else {
  2406. switch (sopt->sopt_name) {
  2407. #ifdef INET
  2408. case SO_ACCEPTFILTER:
  2409. error = do_setopt_accept_filter(so, sopt);
  2410. if (error)
  2411. goto bad;
  2412. break;
  2413. #endif
  2414. case SO_LINGER:
  2415. error = sooptcopyin(sopt, &l, sizeof l, sizeof l);
  2416. if (error)
  2417. goto bad;
  2418. SOCK_LOCK(so);
  2419. so->so_linger = l.l_linger;
  2420. if (l.l_onoff)
  2421. so->so_options |= SO_LINGER;
  2422. else
  2423. so->so_options &= ~SO_LINGER;
  2424. SOCK_UNLOCK(so);
  2425. break;
  2426. case SO_DEBUG:
  2427. case SO_KEEPALIVE:
  2428. case SO_DONTROUTE:
  2429. case SO_USELOOPBACK:
  2430. case SO_BROADCAST:
  2431. case SO_REUSEADDR:
  2432. case SO_REUSEPORT:
  2433. case SO_OOBINLINE:
  2434. case SO_TIMESTAMP:
  2435. case SO_BINTIME:
  2436. case SO_NOSIGPIPE:
  2437. case SO_NO_DDP:
  2438. case SO_NO_OFFLOAD:
  2439. error = sooptcopyin(sopt, &optval, sizeof optval,
  2440. sizeof optval);
  2441. if (error)
  2442. goto bad;
  2443. SOCK_LOCK(so);
  2444. if (optval)
  2445. so->so_options |= sopt->sopt_name;
  2446. else
  2447. so->so_options &= ~sopt->sopt_name;
  2448. SOCK_UNLOCK(so);
  2449. break;
  2450. case SO_SETFIB:
  2451. error = sooptcopyin(sopt, &optval, sizeof optval,
  2452. sizeof optval);
  2453. if (optval < 0 || optval >= rt_numfibs) {
  2454. error = EINVAL;
  2455. goto bad;
  2456. }
  2457. if (((so->so_proto->pr_domain->dom_family == PF_INET) ||
  2458. (so->so_proto->pr_domain->dom_family == PF_INET6) ||
  2459. (so->so_proto->pr_domain->dom_family == PF_ROUTE))) {
  2460. so->so_fibnum = optval;
  2461. /* Note: ignore error */
  2462. if (so->so_proto->pr_ctloutput)
  2463. (*so->so_proto->pr_ctloutput)(so, sopt);
  2464. } else {
  2465. so->so_fibnum = 0;
  2466. }
  2467. break;
  2468. case SO_USER_COOKIE:
  2469. error = sooptcopyin(sopt, &val32, sizeof val32,
  2470. sizeof val32);
  2471. if (error)
  2472. goto bad;
  2473. so->so_user_cookie = val32;
  2474. break;
  2475. case SO_L2INFO:
  2476. #ifdef PROMISCUOUS_INET
  2477. error = sooptcopyin(sopt, &l2info, sizeof l2info,
  2478. sizeof l2info);
  2479. if (error)
  2480. goto bad;
  2481. if ((so->so_proto->pr_domain->dom_family == PF_INET) ||
  2482. (so->so_proto->pr_domain->dom_family == PF_INET6)) {
  2483. if (l2info.inl2i_tagstack.inl2t_cnt >
  2484. IN_L2INFO_MAX_TAGS) {
  2485. error = EINVAL;
  2486. goto bad;
  2487. }
  2488. SOCK_LOCK(so);
  2489. in_promisc_l2info_copy(so->so_l2info, &l2info);
  2490. /* Note: ignore error */
  2491. if (so->so_proto->pr_ctloutput)
  2492. (*so->so_proto->pr_ctloutput)(so, sopt);
  2493. SOCK_UNLOCK(so);
  2494. } else {
  2495. error = ENOPROTOOPT;
  2496. }
  2497. #else
  2498. error = EOPNOTSUPP;
  2499. #endif /* PROMISCUOUS_INET */
  2500. break;
  2501. case SO_PASSIVE:
  2502. case SO_PROMISC:
  2503. #if defined(PROMISCUOUS_INET) || defined(PASSIVE_INET)
  2504. error = sooptcopyin(sopt, &optval, sizeof optval,
  2505. sizeof optval);
  2506. if (error) {
  2507. goto bad;
  2508. }
  2509. SOCK_LOCK(so);
  2510. if (optval)
  2511. so->so_options |= sopt->sopt_name;
  2512. else
  2513. so->so_options &= ~sopt->sopt_name;
  2514. SOCK_UNLOCK(so);
  2515. if ((so->so_proto->pr_domain->dom_family == PF_INET) ||
  2516. (so->so_proto->pr_domain->dom_family == PF_INET6)) {
  2517. /* Note: ignore error */
  2518. if (so->so_proto->pr_ctloutput)
  2519. (*so->so_proto->pr_ctloutput)(so, sopt);
  2520. }
  2521. #else
  2522. error = EOPNOTSUPP;
  2523. #endif
  2524. break;
  2525. case SO_SNDBUF:
  2526. case SO_RCVBUF:
  2527. case SO_SNDLOWAT:
  2528. case SO_RCVLOWAT:
  2529. error = sooptcopyin(sopt, &optval, sizeof optval,
  2530. sizeof optval);
  2531. if (error)
  2532. goto bad;
  2533. /*
  2534. * Values < 1 make no sense for any of these options,
  2535. * so disallow them.
  2536. */
  2537. if (optval < 1) {
  2538. error = EINVAL;
  2539. goto bad;
  2540. }
  2541. switch (sopt->sopt_name) {
  2542. case SO_SNDBUF:
  2543. case SO_RCVBUF:
  2544. if (sbreserve(sopt->sopt_name == SO_SNDBUF ?
  2545. &so->so_snd : &so->so_rcv, (u_long)optval,
  2546. so, curthread) == 0) {
  2547. error = ENOBUFS;
  2548. goto bad;
  2549. }
  2550. (sopt->sopt_name == SO_SNDBUF ? &so->so_snd :
  2551. &so->so_rcv)->sb_flags &= ~SB_AUTOSIZE;
  2552. break;
  2553. /*
  2554. * Make sure the low-water is never greater than the
  2555. * high-water.
  2556. */
  2557. case SO_SNDLOWAT:
  2558. SOCKBUF_LOCK(&so->so_snd);
  2559. so->so_snd.sb_lowat =
  2560. (optval > so->so_snd.sb_hiwat) ?
  2561. so->so_snd.sb_hiwat : optval;
  2562. SOCKBUF_UNLOCK(&so->so_snd);
  2563. break;
  2564. case SO_RCVLOWAT:
  2565. SOCKBUF_LOCK(&so->so_rcv);
  2566. so->so_rcv.sb_lowat =
  2567. (optval > so->so_rcv.sb_hiwat) ?
  2568. so->so_rcv.sb_hiwat : optval;
  2569. SOCKBUF_UNLOCK(&so->so_rcv);
  2570. break;
  2571. }
  2572. break;
  2573. case SO_SNDTIMEO:
  2574. case SO_RCVTIMEO:
  2575. #ifdef COMPAT_FREEBSD32
  2576. if (SV_CURPROC_FLAG(SV_ILP32)) {
  2577. struct timeval32 tv32;
  2578. error = sooptcopyin(sopt, &tv32, sizeof tv32,
  2579. sizeof tv32);
  2580. CP(tv32, tv, tv_sec);
  2581. CP(tv32, tv, tv_usec);
  2582. } else
  2583. #endif
  2584. error = sooptcopyin(sopt, &tv, sizeof tv,
  2585. sizeof tv);
  2586. if (error)
  2587. goto bad;
  2588. /* assert(hz > 0); */
  2589. if (tv.tv_sec < 0 || tv.tv_sec > INT_MAX / hz ||
  2590. tv.tv_usec < 0 || tv.tv_usec >= 1000000) {
  2591. error = EDOM;
  2592. goto bad;
  2593. }
  2594. /* assert(tick > 0); */
  2595. /* assert(ULONG_MAX - INT_MAX >= 1000000); */
  2596. val = (u_long)(tv.tv_sec * hz) + tv.tv_usec / tick;
  2597. if (val > INT_MAX) {
  2598. error = EDOM;
  2599. goto bad;
  2600. }
  2601. if (val == 0 && tv.tv_usec != 0)
  2602. val = 1;
  2603. switch (sopt->sopt_name) {
  2604. case SO_SNDTIMEO:
  2605. so->so_snd.sb_timeo = val;
  2606. break;
  2607. case SO_RCVTIMEO:
  2608. so->so_rcv.sb_timeo = val;
  2609. break;
  2610. }
  2611. break;
  2612. case SO_LABEL:
  2613. #ifdef MAC
  2614. error = sooptcopyin(sopt, &extmac, sizeof extmac,
  2615. sizeof extmac);
  2616. if (error)
  2617. goto bad;
  2618. error = mac_setsockopt_label(sopt->sopt_td->td_ucred,
  2619. so, &extmac);
  2620. #else
  2621. error = EOPNOTSUPP;
  2622. #endif
  2623. break;
  2624. default:
  2625. error = ENOPROTOOPT;
  2626. break;
  2627. }
  2628. if (error == 0 && so->so_proto->pr_ctloutput != NULL)
  2629. (void)(*so->so_proto->pr_ctloutput)(so, sopt);
  2630. }
  2631. bad:
  2632. CURVNET_RESTORE();
  2633. return (error);
  2634. }
  2635. /*
  2636. * Helper routine for getsockopt.
  2637. */
  2638. int
  2639. sooptcopyout(struct sockopt *sopt, const void *buf, size_t len)
  2640. {
  2641. int error;
  2642. size_t valsize;
  2643. error = 0;
  2644. /*
  2645. * Documented get behavior is that we always return a value, possibly
  2646. * truncated to fit in the user's buffer. Traditional behavior is
  2647. * that we always tell the user precisely how much we copied, rather
  2648. * than something useful like the total amount we had available for
  2649. * her. Note that this interface is not idempotent; the entire
  2650. * answer must generated ahead of time.
  2651. */
  2652. valsize = min(len, sopt->sopt_valsize);
  2653. sopt->sopt_valsize = valsize;
  2654. if (sopt->sopt_val != NULL) {
  2655. if (sopt->sopt_td != NULL)
  2656. error = copyout(buf, sopt->sopt_val, valsize);
  2657. else
  2658. bcopy(buf, sopt->sopt_val, valsize);
  2659. }
  2660. return (error);
  2661. }
  2662. /*
  2663. * Kernel version of getsockopt(2).
  2664. *
  2665. * XXX: optlen is size_t, not socklen_t
  2666. */
  2667. int
  2668. so_getsockopt(struct socket *so, int level, int optname, void *optval,
  2669. size_t *optlen)
  2670. {
  2671. struct sockopt sopt;
  2672. int error;
  2673. sopt.sopt_level = level;
  2674. sopt.sopt_name = optname;
  2675. sopt.sopt_dir = SOPT_GET;
  2676. sopt.sopt_val = optval;
  2677. sopt.sopt_valsize = *optlen;
  2678. sopt.sopt_td = NULL;
  2679. error = sogetopt(so, &sopt);
  2680. *optlen = sopt.sopt_valsize;
  2681. return (error);
  2682. }
  2683. int
  2684. sogetopt(struct socket *so, struct sockopt *sopt)
  2685. {
  2686. int error, optval;
  2687. struct linger l;
  2688. struct timeval tv;
  2689. #ifdef MAC
  2690. struct mac extmac;
  2691. #endif
  2692. #ifdef PROMISCUOUS_INET
  2693. struct in_l2info l2info;
  2694. #endif
  2695. CURVNET_SET(so->so_vnet);
  2696. error = 0;
  2697. if (sopt->sopt_level != SOL_SOCKET) {
  2698. if (so->so_proto->pr_ctloutput != NULL)
  2699. error = (*so->so_proto->pr_ctloutput)(so, sopt);
  2700. else
  2701. error = ENOPROTOOPT;
  2702. CURVNET_RESTORE();
  2703. return (error);
  2704. } else {
  2705. switch (sopt->sopt_name) {
  2706. #ifdef INET
  2707. case SO_ACCEPTFILTER:
  2708. error = do_getopt_accept_filter(so, sopt);
  2709. break;
  2710. #endif
  2711. case SO_LINGER:
  2712. SOCK_LOCK(so);
  2713. l.l_onoff = so->so_options & SO_LINGER;
  2714. l.l_linger = so->so_linger;
  2715. SOCK_UNLOCK(so);
  2716. error = sooptcopyout(sopt, &l, sizeof l);
  2717. break;
  2718. case SO_USELOOPBACK:
  2719. case SO_DONTROUTE:
  2720. case SO_DEBUG:
  2721. case SO_KEEPALIVE:
  2722. case SO_REUSEADDR:
  2723. case SO_REUSEPORT:
  2724. case SO_BROADCAST:
  2725. case SO_OOBINLINE:
  2726. case SO_ACCEPTCONN:
  2727. case SO_TIMESTAMP:
  2728. case SO_BINTIME:
  2729. case SO_NOSIGPIPE:
  2730. optval = so->so_options & sopt->sopt_name;
  2731. integer:
  2732. error = sooptcopyout(sopt, &optval, sizeof optval);
  2733. break;
  2734. case SO_TYPE:
  2735. optval = so->so_type;
  2736. goto integer;
  2737. case SO_PROTOCOL:
  2738. optval = so->so_proto->pr_protocol;
  2739. goto integer;
  2740. case SO_ERROR:
  2741. SOCK_LOCK(so);
  2742. optval = so->so_error;
  2743. so->so_error = 0;
  2744. SOCK_UNLOCK(so);
  2745. goto integer;
  2746. case SO_L2INFO:
  2747. #ifdef PROMISCUOUS_INET
  2748. if ((so->so_proto->pr_domain->dom_family == PF_INET) ||
  2749. (so->so_proto->pr_domain->dom_family == PF_INET6)) {
  2750. SOCK_LOCK(so);
  2751. in_promisc_l2info_copy(&l2info, so->so_l2info);
  2752. SOCK_UNLOCK(so);
  2753. error = sooptcopyout(sopt, &l2info, sizeof l2info);
  2754. } else {
  2755. error = ENOPROTOOPT;
  2756. }
  2757. #else
  2758. error = EOPNOTSUPP;
  2759. #endif
  2760. break;
  2761. case SO_SNDBUF:
  2762. optval = so->so_snd.sb_hiwat;
  2763. goto integer;
  2764. case SO_RCVBUF:
  2765. optval = so->so_rcv.sb_hiwat;
  2766. goto integer;
  2767. case SO_SNDLOWAT:
  2768. optval = so->so_snd.sb_lowat;
  2769. goto integer;
  2770. case SO_RCVLOWAT:
  2771. optval = so->so_rcv.sb_lowat;
  2772. goto integer;
  2773. case SO_SNDTIMEO:
  2774. case SO_RCVTIMEO:
  2775. optval = (sopt->sopt_name == SO_SNDTIMEO ?
  2776. so->so_snd.sb_timeo : so->so_rcv.sb_timeo);
  2777. tv.tv_sec = optval / hz;
  2778. tv.tv_usec = (optval % hz) * tick;
  2779. #ifdef COMPAT_FREEBSD32
  2780. if (SV_CURPROC_FLAG(SV_ILP32)) {
  2781. struct timeval32 tv32;
  2782. CP(tv, tv32, tv_sec);
  2783. CP(tv, tv32, tv_usec);
  2784. error = sooptcopyout(sopt, &tv32, sizeof tv32);
  2785. } else
  2786. #endif
  2787. error = sooptcopyout(sopt, &tv, sizeof tv);
  2788. break;
  2789. case SO_LABEL:
  2790. #ifdef MAC
  2791. error = sooptcopyin(sopt, &extmac, sizeof(extmac),
  2792. sizeof(extmac));
  2793. if (error)
  2794. goto bad;
  2795. error = mac_getsockopt_label(sopt->sopt_td->td_ucred,
  2796. so, &extmac);
  2797. if (error)
  2798. goto bad;
  2799. error = sooptcopyout(sopt, &extmac, sizeof extmac);
  2800. #else
  2801. error = EOPNOTSUPP;
  2802. #endif
  2803. break;
  2804. case SO_PEERLABEL:
  2805. #ifdef MAC
  2806. error = sooptcopyin(sopt, &extmac, sizeof(extmac),
  2807. sizeof(extmac));
  2808. if (error)
  2809. goto bad;
  2810. error = mac_getsockopt_peerlabel(
  2811. sopt->sopt_td->td_ucred, so, &extmac);
  2812. if (error)
  2813. goto bad;
  2814. error = sooptcopyout(sopt, &extmac, sizeof extmac);
  2815. #else
  2816. error = EOPNOTSUPP;
  2817. #endif
  2818. break;
  2819. case SO_LISTENQLIMIT:
  2820. optval = so->so_qlimit;
  2821. goto integer;
  2822. case SO_LISTENQLEN:
  2823. optval = so->so_qlen;
  2824. goto integer;
  2825. case SO_LISTENINCQLEN:
  2826. optval = so->so_incqlen;
  2827. goto integer;
  2828. default:
  2829. error = ENOPROTOOPT;
  2830. break;
  2831. }
  2832. }
  2833. #ifdef MAC
  2834. bad:
  2835. #endif
  2836. CURVNET_RESTORE();
  2837. return (error);
  2838. }
  2839. /* XXX; prepare mbuf for (__FreeBSD__ < 3) routines. */
  2840. int
  2841. soopt_getm(struct sockopt *sopt, struct mbuf **mp)
  2842. {
  2843. struct mbuf *m, *m_prev;
  2844. int sopt_size = sopt->sopt_valsize;
  2845. MGET(m, sopt->sopt_td ? M_WAIT : M_DONTWAIT, MT_DATA);
  2846. if (m == NULL)
  2847. return ENOBUFS;
  2848. if (sopt_size > MLEN) {
  2849. MCLGET(m, sopt->sopt_td ? M_WAIT : M_DONTWAIT);
  2850. if ((m->m_flags & M_EXT) == 0) {
  2851. m_free(m);
  2852. return ENOBUFS;
  2853. }
  2854. m->m_len = min(MCLBYTES, sopt_size);
  2855. } else {
  2856. m->m_len = min(MLEN, sopt_size);
  2857. }
  2858. sopt_size -= m->m_len;
  2859. *mp = m;
  2860. m_prev = m;
  2861. while (sopt_size) {
  2862. MGET(m, sopt->sopt_td ? M_WAIT : M_DONTWAIT, MT_DATA);
  2863. if (m == NULL) {
  2864. m_freem(*mp);
  2865. return ENOBUFS;
  2866. }
  2867. if (sopt_size > MLEN) {
  2868. MCLGET(m, sopt->sopt_td != NULL ? M_WAIT :
  2869. M_DONTWAIT);
  2870. if ((m->m_flags & M_EXT) == 0) {
  2871. m_freem(m);
  2872. m_freem(*mp);
  2873. return ENOBUFS;
  2874. }
  2875. m->m_len = min(MCLBYTES, sopt_size);
  2876. } else {
  2877. m->m_len = min(MLEN, sopt_size);
  2878. }
  2879. sopt_size -= m->m_len;
  2880. m_prev->m_next = m;
  2881. m_prev = m;
  2882. }
  2883. return (0);
  2884. }
  2885. /* XXX; copyin sopt data into mbuf chain for (__FreeBSD__ < 3) routines. */
  2886. int
  2887. soopt_mcopyin(struct sockopt *sopt, struct mbuf *m)
  2888. {
  2889. struct mbuf *m0 = m;
  2890. if (sopt->sopt_val == NULL)
  2891. return (0);
  2892. while (m != NULL && sopt->sopt_valsize >= m->m_len) {
  2893. if (sopt->sopt_td != NULL) {
  2894. int error;
  2895. error = copyin(sopt->sopt_val, mtod(m, char *),
  2896. m->m_len);
  2897. if (error != 0) {
  2898. m_freem(m0);
  2899. return(error);
  2900. }
  2901. } else
  2902. bcopy(sopt->sopt_val, mtod(m, char *), m->m_len);
  2903. sopt->sopt_valsize -= m->m_len;
  2904. sopt->sopt_val = (char *)sopt->sopt_val + m->m_len;
  2905. m = m->m_next;
  2906. }
  2907. if (m != NULL) /* should be allocated enoughly at ip6_sooptmcopyin() */
  2908. panic("ip6_sooptmcopyin");
  2909. return (0);
  2910. }
  2911. /* XXX; copyout mbuf chain data into soopt for (__FreeBSD__ < 3) routines. */
  2912. int
  2913. soopt_mcopyout(struct sockopt *sopt, struct mbuf *m)
  2914. {
  2915. struct mbuf *m0 = m;
  2916. size_t valsize = 0;
  2917. if (sopt->sopt_val == NULL)
  2918. return (0);
  2919. while (m != NULL && sopt->sopt_valsize >= m->m_len) {
  2920. if (sopt->sopt_td != NULL) {
  2921. int error;
  2922. error = copyout(mtod(m, char *), sopt->sopt_val,
  2923. m->m_len);
  2924. if (error != 0) {
  2925. m_freem(m0);
  2926. return(error);
  2927. }
  2928. } else
  2929. bcopy(mtod(m, char *), sopt->sopt_val, m->m_len);
  2930. sopt->sopt_valsize -= m->m_len;
  2931. sopt->sopt_val = (char *)sopt->sopt_val + m->m_len;
  2932. valsize += m->m_len;
  2933. m = m->m_next;
  2934. }
  2935. if (m != NULL) {
  2936. /* enough soopt buffer should be given from user-land */
  2937. m_freem(m0);
  2938. return(EINVAL);
  2939. }
  2940. sopt->sopt_valsize = valsize;
  2941. return (0);
  2942. }
  2943. /*
  2944. * sohasoutofband(): protocol notifies socket layer of the arrival of new
  2945. * out-of-band data, which will then notify socket consumers.
  2946. */
  2947. void
  2948. sohasoutofband(struct socket *so)
  2949. {
  2950. if (so->so_sigio != NULL)
  2951. pgsigio(&so->so_sigio, SIGURG, 0);
  2952. selwakeuppri(&so->so_rcv.sb_sel, PSOCK);
  2953. }
  2954. int
  2955. sopoll(struct socket *so, int events, struct ucred *active_cred,
  2956. struct thread *td)
  2957. {
  2958. /*
  2959. * We do not need to set or assert curvnet as long as everyone uses
  2960. * sopoll_generic().
  2961. */
  2962. return (so->so_proto->pr_usrreqs->pru_sopoll(so, events, active_cred,
  2963. td));
  2964. }
  2965. int
  2966. sopoll_generic(struct socket *so, int events, struct ucred *active_cred,
  2967. struct thread *td)
  2968. {
  2969. int revents = 0;
  2970. SOCKBUF_LOCK(&so->so_snd);
  2971. SOCKBUF_LOCK(&so->so_rcv);
  2972. if (events & (POLLIN | POLLRDNORM))
  2973. if (soreadabledata(so))
  2974. revents |= events & (POLLIN | POLLRDNORM);
  2975. if (events & (POLLOUT | POLLWRNORM))
  2976. if (sowriteable(so))
  2977. revents |= events & (POLLOUT | POLLWRNORM);
  2978. if (events & (POLLPRI | POLLRDBAND))
  2979. if (so->so_oobmark || (so->so_rcv.sb_state & SBS_RCVATMARK))
  2980. revents |= events & (POLLPRI | POLLRDBAND);
  2981. if ((events & POLLINIGNEOF) == 0) {
  2982. if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
  2983. revents |= events & (POLLIN | POLLRDNORM);
  2984. if (so->so_snd.sb_state & SBS_CANTSENDMORE)
  2985. revents |= POLLHUP;
  2986. }
  2987. }
  2988. if (revents == 0) {
  2989. if (events & (POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND)) {
  2990. selrecord(td, &so->so_rcv.sb_sel);
  2991. so->so_rcv.sb_flags |= SB_SEL;
  2992. }
  2993. if (events & (POLLOUT | POLLWRNORM)) {
  2994. selrecord(td, &so->so_snd.sb_sel);
  2995. so->so_snd.sb_flags |= SB_SEL;
  2996. }
  2997. }
  2998. SOCKBUF_UNLOCK(&so->so_rcv);
  2999. SOCKBUF_UNLOCK(&so->so_snd);
  3000. return (revents);
  3001. }
  3002. int
  3003. soo_kqfilter(struct file *fp, struct knote *kn)
  3004. {
  3005. struct socket *so = kn->kn_fp->f_data;
  3006. struct sockbuf *sb;
  3007. switch (kn->kn_filter) {
  3008. case EVFILT_READ:
  3009. if (so->so_options & SO_ACCEPTCONN)
  3010. kn->kn_fop = &solisten_filtops;
  3011. else
  3012. kn->kn_fop = &soread_filtops;
  3013. sb = &so->so_rcv;
  3014. break;
  3015. case EVFILT_WRITE:
  3016. kn->kn_fop = &sowrite_filtops;
  3017. sb = &so->so_snd;
  3018. break;
  3019. default:
  3020. return (EINVAL);
  3021. }
  3022. SOCKBUF_LOCK(sb);
  3023. knlist_add(&sb->sb_sel.si_note, kn, 1);
  3024. sb->sb_flags |= SB_KNOTE;
  3025. SOCKBUF_UNLOCK(sb);
  3026. return (0);
  3027. }
  3028. /*
  3029. * Some routines that return EOPNOTSUPP for entry points that are not
  3030. * supported by a protocol. Fill in as needed.
  3031. */
  3032. int
  3033. pru_accept_notsupp(struct socket *so, struct sockaddr **nam)
  3034. {
  3035. return EOPNOTSUPP;
  3036. }
  3037. int
  3038. pru_attach_notsupp(struct socket *so, int proto, struct thread *td)
  3039. {
  3040. return EOPNOTSUPP;
  3041. }
  3042. int
  3043. pru_bind_notsupp(struct socket *so, struct sockaddr *nam, struct thread *td)
  3044. {
  3045. return EOPNOTSUPP;
  3046. }
  3047. int
  3048. pru_connect_notsupp(struct socket *so, struct sockaddr *nam, struct thread *td)
  3049. {
  3050. return EOPNOTSUPP;
  3051. }
  3052. int
  3053. pru_connect2_notsupp(struct socket *so1, struct socket *so2)
  3054. {
  3055. return EOPNOTSUPP;
  3056. }
  3057. int
  3058. pru_control_notsupp(struct socket *so, u_long cmd, caddr_t data,
  3059. struct ifnet *ifp, struct thread *td)
  3060. {
  3061. return EOPNOTSUPP;
  3062. }
  3063. int
  3064. pru_disconnect_notsupp(struct socket *so)
  3065. {
  3066. return EOPNOTSUPP;
  3067. }
  3068. int
  3069. pru_listen_notsupp(struct socket *so, int backlog, struct thread *td)
  3070. {
  3071. return EOPNOTSUPP;
  3072. }
  3073. int
  3074. pru_peeraddr_notsupp(struct socket *so, struct sockaddr **nam)
  3075. {
  3076. return EOPNOTSUPP;
  3077. }
  3078. int
  3079. pru_rcvd_notsupp(struct socket *so, int flags)
  3080. {
  3081. return EOPNOTSUPP;
  3082. }
  3083. int
  3084. pru_rcvoob_notsupp(struct socket *so, struct mbuf *m, int flags)
  3085. {
  3086. return EOPNOTSUPP;
  3087. }
  3088. int
  3089. pru_send_notsupp(struct socket *so, int flags, struct mbuf *m,
  3090. struct sockaddr *addr, struct mbuf *control, struct thread *td)
  3091. {
  3092. return EOPNOTSUPP;
  3093. }
  3094. /*
  3095. * This isn't really a ``null'' operation, but it's the default one and
  3096. * doesn't do anything destructive.
  3097. */
  3098. int
  3099. pru_sense_null(struct socket *so, struct stat *sb)
  3100. {
  3101. sb->st_blksize = so->so_snd.sb_hiwat;
  3102. return 0;
  3103. }
  3104. int
  3105. pru_shutdown_notsupp(struct socket *so)
  3106. {
  3107. return EOPNOTSUPP;
  3108. }
  3109. int
  3110. pru_sockaddr_notsupp(struct socket *so, struct sockaddr **nam)
  3111. {
  3112. return EOPNOTSUPP;
  3113. }
  3114. int
  3115. pru_sosend_notsupp(struct socket *so, struct sockaddr *addr, struct uio *uio,
  3116. struct mbuf *top, struct mbuf *control, int flags, struct thread *td)
  3117. {
  3118. return EOPNOTSUPP;
  3119. }
  3120. int
  3121. pru_soreceive_notsupp(struct socket *so, struct sockaddr **paddr,
  3122. struct uio *uio, struct mbuf **mp0, struct mbuf **controlp, int *flagsp)
  3123. {
  3124. return EOPNOTSUPP;
  3125. }
  3126. int
  3127. pru_sopoll_notsupp(struct socket *so, int events, struct ucred *cred,
  3128. struct thread *td)
  3129. {
  3130. return EOPNOTSUPP;
  3131. }
  3132. static void
  3133. filt_sordetach(struct knote *kn)
  3134. {
  3135. struct socket *so = kn->kn_fp->f_data;
  3136. SOCKBUF_LOCK(&so->so_rcv);
  3137. knlist_remove(&so->so_rcv.sb_sel.si_note, kn, 1);
  3138. if (knlist_empty(&so->so_rcv.sb_sel.si_note))
  3139. so->so_rcv.sb_flags &= ~SB_KNOTE;
  3140. SOCKBUF_UNLOCK(&so->so_rcv);
  3141. }
  3142. /*ARGSUSED*/
  3143. static int
  3144. filt_soread(struct knote *kn, long hint)
  3145. {
  3146. struct socket *so;
  3147. so = kn->kn_fp->f_data;
  3148. SOCKBUF_LOCK_ASSERT(&so->so_rcv);
  3149. kn->kn_data = so->so_rcv.sb_cc - so->so_rcv.sb_ctl;
  3150. if (so->so_rcv.sb_state & SBS_CANTRCVMORE) {
  3151. kn->kn_flags |= EV_EOF;
  3152. kn->kn_fflags = so->so_error;
  3153. return (1);
  3154. } else if (so->so_error) /* temporary udp error */
  3155. return (1);
  3156. else if (kn->kn_sfflags & NOTE_LOWAT)
  3157. return (kn->kn_data >= kn->kn_sdata);
  3158. else
  3159. return (so->so_rcv.sb_cc >= so->so_rcv.sb_lowat);
  3160. }
  3161. static void
  3162. filt_sowdetach(struct knote *kn)
  3163. {
  3164. struct socket *so = kn->kn_fp->f_data;
  3165. SOCKBUF_LOCK(&so->so_snd);
  3166. knlist_remove(&so->so_snd.sb_sel.si_note, kn, 1);
  3167. if (knlist_empty(&so->so_snd.sb_sel.si_note))
  3168. so->so_snd.sb_flags &= ~SB_KNOTE;
  3169. SOCKBUF_UNLOCK(&so->so_snd);
  3170. }
  3171. /*ARGSUSED*/
  3172. static int
  3173. filt_sowrite(struct knote *kn, long hint)
  3174. {
  3175. struct socket *so;
  3176. so = kn->kn_fp->f_data;
  3177. SOCKBUF_LOCK_ASSERT(&so->so_snd);
  3178. kn->kn_data = sbspace(&so->so_snd);
  3179. if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
  3180. kn->kn_flags |= EV_EOF;
  3181. kn->kn_fflags = so->so_error;
  3182. return (1);
  3183. } else if (so->so_error) /* temporary udp error */
  3184. return (1);
  3185. else if (((so->so_state & SS_ISCONNECTED) == 0) &&
  3186. (so->so_proto->pr_flags & PR_CONNREQUIRED))
  3187. return (0);
  3188. else if (kn->kn_sfflags & NOTE_LOWAT)
  3189. return (kn->kn_data >= kn->kn_sdata);
  3190. else
  3191. return (kn->kn_data >= so->so_snd.sb_lowat);
  3192. }
  3193. /*ARGSUSED*/
  3194. static int
  3195. filt_solisten(struct knote *kn, long hint)
  3196. {
  3197. struct socket *so = kn->kn_fp->f_data;
  3198. kn->kn_data = so->so_qlen;
  3199. return (! TAILQ_EMPTY(&so->so_comp));
  3200. }
  3201. int
  3202. socheckuid(struct socket *so, uid_t uid)
  3203. {
  3204. if (so == NULL)
  3205. return (EPERM);
  3206. if (so->so_cred->cr_uid != uid)
  3207. return (EPERM);
  3208. return (0);
  3209. }
  3210. static int
  3211. sysctl_somaxconn(SYSCTL_HANDLER_ARGS)
  3212. {
  3213. int error;
  3214. int val;
  3215. val = somaxconn;
  3216. error = sysctl_handle_int(oidp, &val, 0, req);
  3217. if (error || !req->newptr )
  3218. return (error);
  3219. if (val < 1 || val > USHRT_MAX)
  3220. return (EINVAL);
  3221. somaxconn = val;
  3222. return (0);
  3223. }
  3224. /*
  3225. * These functions are used by protocols to notify the socket layer (and its
  3226. * consumers) of state changes in the sockets driven by protocol-side events.
  3227. */
  3228. /*
  3229. * Procedures to manipulate state flags of socket and do appropriate wakeups.
  3230. *
  3231. * Normal sequence from the active (originating) side is that
  3232. * soisconnecting() is called during processing of connect() call, resulting
  3233. * in an eventual call to soisconnected() if/when the connection is
  3234. * established. When the connection is torn down soisdisconnecting() is
  3235. * called during processing of disconnect() call, and soisdisconnected() is
  3236. * called when the connection to the peer is totally severed. The semantics
  3237. * of these routines are such that connectionless protocols can call
  3238. * soisconnected() and soisdisconnected() only, bypassing the in-progress
  3239. * calls when setting up a ``connection'' takes no time.
  3240. *
  3241. * From the passive side, a socket is created with two queues of sockets:
  3242. * so_incomp for connections in progress and so_comp for connections already
  3243. * made and awaiting user acceptance. As a protocol is preparing incoming
  3244. * connections, it creates a socket structure queued on so_incomp by calling
  3245. * sonewconn(). When the connection is established, soisconnected() is
  3246. * called, and transfers the socket structure to so_comp, making it available
  3247. * to accept().
  3248. *
  3249. * If a socket is closed with sockets on either so_incomp or so_comp, these
  3250. * sockets are dropped.
  3251. *
  3252. * If higher-level protocols are implemented in the kernel, the wakeups done
  3253. * here will sometimes cause software-interrupt process scheduling.
  3254. */
  3255. void
  3256. soisconnecting(struct socket *so)
  3257. {
  3258. SOCK_LOCK(so);
  3259. so->so_state &= ~(SS_ISCONNECTED|SS_ISDISCONNECTING);
  3260. so->so_state |= SS_ISCONNECTING;
  3261. SOCK_UNLOCK(so);
  3262. }
  3263. void
  3264. soisconnected(struct socket *so)
  3265. {
  3266. struct socket *head;
  3267. int ret;
  3268. restart:
  3269. ACCEPT_LOCK();
  3270. SOCK_LOCK(so);
  3271. so->so_state &= ~(SS_ISCONNECTING|SS_ISDISCONNECTING|SS_ISCONFIRMING);
  3272. so->so_state |= SS_ISCONNECTED;
  3273. head = so->so_head;
  3274. if (head != NULL && (so->so_qstate & SQ_INCOMP)) {
  3275. if ((so->so_options & SO_ACCEPTFILTER) == 0) {
  3276. SOCK_UNLOCK(so);
  3277. TAILQ_REMOVE(&head->so_incomp, so, so_list);
  3278. head->so_incqlen--;
  3279. so->so_qstate &= ~SQ_INCOMP;
  3280. TAILQ_INSERT_TAIL(&head->so_comp, so, so_list);
  3281. head->so_qlen++;
  3282. so->so_qstate |= SQ_COMP;
  3283. ACCEPT_UNLOCK();
  3284. sorwakeup(head);
  3285. wakeup_one(&head->so_timeo);
  3286. } else {
  3287. ACCEPT_UNLOCK();
  3288. soupcall_set(so, SO_RCV,
  3289. head->so_accf->so_accept_filter->accf_callback,
  3290. head->so_accf->so_accept_filter_arg);
  3291. so->so_options &= ~SO_ACCEPTFILTER;
  3292. ret = head->so_accf->so_accept_filter->accf_callback(so,
  3293. head->so_accf->so_accept_filter_arg, M_DONTWAIT);
  3294. if (ret == SU_ISCONNECTED)
  3295. soupcall_clear(so, SO_RCV);
  3296. SOCK_UNLOCK(so);
  3297. if (ret == SU_ISCONNECTED)
  3298. goto restart;
  3299. }
  3300. return;
  3301. }
  3302. SOCK_UNLOCK(so);
  3303. ACCEPT_UNLOCK();
  3304. wakeup(&so->so_timeo);
  3305. sorwakeup(so);
  3306. sowwakeup(so);
  3307. }
  3308. void
  3309. soisdisconnecting(struct socket *so)
  3310. {
  3311. /*
  3312. * Note: This code assumes that SOCK_LOCK(so) and
  3313. * SOCKBUF_LOCK(&so->so_rcv) are the same.
  3314. */
  3315. SOCKBUF_LOCK(&so->so_rcv);
  3316. so->so_state &= ~SS_ISCONNECTING;
  3317. so->so_state |= SS_ISDISCONNECTING;
  3318. so->so_rcv.sb_state |= SBS_CANTRCVMORE;
  3319. sorwakeup_locked(so);
  3320. SOCKBUF_LOCK(&so->so_snd);
  3321. so->so_snd.sb_state |= SBS_CANTSENDMORE;
  3322. sowwakeup_locked(so);
  3323. wakeup(&so->so_timeo);
  3324. }
  3325. void
  3326. soisdisconnected(struct socket *so)
  3327. {
  3328. /*
  3329. * Note: This code assumes that SOCK_LOCK(so) and
  3330. * SOCKBUF_LOCK(&so->so_rcv) are the same.
  3331. */
  3332. SOCKBUF_LOCK(&so->so_rcv);
  3333. so->so_state &= ~(SS_ISCONNECTING|SS_ISCONNECTED|SS_ISDISCONNECTING);
  3334. so->so_state |= SS_ISDISCONNECTED;
  3335. so->so_rcv.sb_state |= SBS_CANTRCVMORE;
  3336. sorwakeup_locked(so);
  3337. SOCKBUF_LOCK(&so->so_snd);
  3338. so->so_snd.sb_state |= SBS_CANTSENDMORE;
  3339. sbdrop_locked(&so->so_snd, so->so_snd.sb_cc);
  3340. sowwakeup_locked(so);
  3341. wakeup(&so->so_timeo);
  3342. }
  3343. /*
  3344. * Make a copy of a sockaddr in a malloced buffer of type M_SONAME.
  3345. */
  3346. struct sockaddr *
  3347. sodupsockaddr(const struct sockaddr *sa, int mflags)
  3348. {
  3349. struct sockaddr *sa2;
  3350. sa2 = malloc(sa->sa_len, M_SONAME, mflags);
  3351. if (sa2)
  3352. bcopy(sa, sa2, sa->sa_len);
  3353. return sa2;
  3354. }
  3355. /*
  3356. * Register per-socket buffer upcalls.
  3357. */
  3358. void
  3359. soupcall_set(struct socket *so, int which,
  3360. int (*func)(struct socket *, void *, int), void *arg)
  3361. {
  3362. struct sockbuf *sb;
  3363. switch (which) {
  3364. case SO_RCV:
  3365. sb = &so->so_rcv;
  3366. break;
  3367. case SO_SND:
  3368. sb = &so->so_snd;
  3369. break;
  3370. default:
  3371. panic("soupcall_set: bad which");
  3372. }
  3373. SOCKBUF_LOCK_ASSERT(sb);
  3374. #if 0
  3375. /* XXX: accf_http actually wants to do this on purpose. */
  3376. KASSERT(sb->sb_upcall == NULL, ("soupcall_set: overwriting upcall"));
  3377. #endif
  3378. sb->sb_upcall = func;
  3379. sb->sb_upcallarg = arg;
  3380. sb->sb_flags |= SB_UPCALL;
  3381. }
  3382. void
  3383. soupcall_clear(struct socket *so, int which)
  3384. {
  3385. struct sockbuf *sb;
  3386. switch (which) {
  3387. case SO_RCV:
  3388. sb = &so->so_rcv;
  3389. break;
  3390. case SO_SND:
  3391. sb = &so->so_snd;
  3392. break;
  3393. default:
  3394. panic("soupcall_clear: bad which");
  3395. }
  3396. SOCKBUF_LOCK_ASSERT(sb);
  3397. KASSERT(sb->sb_upcall != NULL, ("soupcall_clear: no upcall to clear"));
  3398. sb->sb_upcall = NULL;
  3399. sb->sb_upcallarg = NULL;
  3400. sb->sb_flags &= ~SB_UPCALL;
  3401. }
  3402. int
  3403. souserctx_alloc(struct socket *so)
  3404. {
  3405. int result;
  3406. SOCK_LOCK(so);
  3407. if (SOMAXUSERCTX == so->so_user_ctx_count)
  3408. result = -1;
  3409. else
  3410. result = so->so_user_ctx_count++;
  3411. SOCK_UNLOCK(so);
  3412. return (result);
  3413. }
  3414. /*
  3415. * Create an external-format (``xsocket'') structure using the information in
  3416. * the kernel-format socket structure pointed to by so. This is done to
  3417. * reduce the spew of irrelevant information over this interface, to isolate
  3418. * user code from changes in the kernel structure, and potentially to provide
  3419. * information-hiding if we decide that some of this information should be
  3420. * hidden from users.
  3421. */
  3422. void
  3423. sotoxsocket(struct socket *so, struct xsocket *xso)
  3424. {
  3425. xso->xso_len = sizeof *xso;
  3426. xso->xso_so = so;
  3427. xso->so_type = so->so_type;
  3428. xso->so_options = so->so_options;
  3429. xso->so_linger = so->so_linger;
  3430. xso->so_state = so->so_state;
  3431. xso->so_pcb = so->so_pcb;
  3432. xso->xso_protocol = so->so_proto->pr_protocol;
  3433. xso->xso_family = so->so_proto->pr_domain->dom_family;
  3434. xso->so_qlen = so->so_qlen;
  3435. xso->so_incqlen = so->so_incqlen;
  3436. xso->so_qlimit = so->so_qlimit;
  3437. xso->so_timeo = so->so_timeo;
  3438. xso->so_error = so->so_error;
  3439. xso->so_pgid = so->so_sigio ? so->so_sigio->sio_pgid : 0;
  3440. xso->so_oobmark = so->so_oobmark;
  3441. sbtoxsockbuf(&so->so_snd, &xso->so_snd);
  3442. sbtoxsockbuf(&so->so_rcv, &xso->so_rcv);
  3443. xso->so_uid = so->so_cred->cr_uid;
  3444. }
  3445. /*
  3446. * Socket accessor functions to provide external consumers with
  3447. * a safe interface to socket state
  3448. *
  3449. */
  3450. void
  3451. so_listeners_apply_all(struct socket *so, void (*func)(struct socket *, void *), void *arg)
  3452. {
  3453. TAILQ_FOREACH(so, &so->so_comp, so_list)
  3454. func(so, arg);
  3455. }
  3456. struct sockbuf *
  3457. so_sockbuf_rcv(struct socket *so)
  3458. {
  3459. return (&so->so_rcv);
  3460. }
  3461. struct sockbuf *
  3462. so_sockbuf_snd(struct socket *so)
  3463. {
  3464. return (&so->so_snd);
  3465. }
  3466. int
  3467. so_state_get(const struct socket *so)
  3468. {
  3469. return (so->so_state);
  3470. }
  3471. void
  3472. so_state_set(struct socket *so, int val)
  3473. {
  3474. so->so_state = val;
  3475. }
  3476. int
  3477. so_options_get(const struct socket *so)
  3478. {
  3479. return (so->so_options);
  3480. }
  3481. void
  3482. so_options_set(struct socket *so, int val)
  3483. {
  3484. so->so_options = val;
  3485. }
  3486. int
  3487. so_error_get(const struct socket *so)
  3488. {
  3489. return (so->so_error);
  3490. }
  3491. void
  3492. so_error_set(struct socket *so, int val)
  3493. {
  3494. so->so_error = val;
  3495. }
  3496. int
  3497. so_linger_get(const struct socket *so)
  3498. {
  3499. return (so->so_linger);
  3500. }
  3501. void
  3502. so_linger_set(struct socket *so, int val)
  3503. {
  3504. so->so_linger = val;
  3505. }
  3506. struct protosw *
  3507. so_protosw_get(const struct socket *so)
  3508. {
  3509. return (so->so_proto);
  3510. }
  3511. void
  3512. so_protosw_set(struct socket *so, struct protosw *val)
  3513. {
  3514. so->so_proto = val;
  3515. }
  3516. void
  3517. so_sorwakeup(struct socket *so)
  3518. {
  3519. sorwakeup(so);
  3520. }
  3521. void
  3522. so_sowwakeup(struct socket *so)
  3523. {
  3524. sowwakeup(so);
  3525. }
  3526. void
  3527. so_sorwakeup_locked(struct socket *so)
  3528. {
  3529. sorwakeup_locked(so);
  3530. }
  3531. void
  3532. so_sowwakeup_locked(struct socket *so)
  3533. {
  3534. sowwakeup_locked(so);
  3535. }
  3536. void
  3537. so_lock(struct socket *so)
  3538. {
  3539. SOCK_LOCK(so);
  3540. }
  3541. void
  3542. so_unlock(struct socket *so)
  3543. {
  3544. SOCK_UNLOCK(so);
  3545. }