1 /* 2 * Copyright (c) 2005 Jeffrey M. Hsu. All rights reserved. 3 * Copyright (c) 1982, 1986, 1988, 1990, 1993 4 * The Regents of the University of California. All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. All advertising materials mentioning features or use of this software 15 * must display the following acknowledgement: 16 * This product includes software developed by the University of 17 * California, Berkeley and its contributors. 18 * 4. Neither the name of the University nor the names of its contributors 19 * may be used to endorse or promote products derived from this software 20 * without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32 * SUCH DAMAGE. 33 * 34 * @(#)uipc_socket2.c 8.1 (Berkeley) 6/10/93 35 * $FreeBSD: src/sys/kern/uipc_socket2.c,v 1.55.2.17 2002/08/31 19:04:55 dwmalone Exp $ 36 * $DragonFly: src/sys/kern/uipc_socket2.c,v 1.33 2008/09/02 16:17:52 dillon Exp $ 37 */ 38 39 #include "opt_param.h" 40 #include <sys/param.h> 41 #include <sys/systm.h> 42 #include <sys/domain.h> 43 #include <sys/file.h> /* for maxfiles */ 44 #include <sys/kernel.h> 45 #include <sys/proc.h> 46 #include <sys/malloc.h> 47 #include <sys/mbuf.h> 48 #include <sys/protosw.h> 49 #include <sys/resourcevar.h> 50 #include <sys/stat.h> 51 #include <sys/socket.h> 52 #include <sys/socketvar.h> 53 #include <sys/signalvar.h> 54 #include <sys/sysctl.h> 55 #include <sys/aio.h> /* for aio_swake proto */ 56 #include <sys/event.h> 57 58 #include <sys/thread2.h> 59 #include <sys/msgport2.h> 60 61 int maxsockets; 62 63 /* 64 * Primitive routines for operating on sockets and socket buffers 65 */ 66 67 u_long sb_max = SB_MAX; 68 u_long sb_max_adj = 69 SB_MAX * MCLBYTES / (MSIZE + MCLBYTES); /* adjusted sb_max */ 70 71 static u_long sb_efficiency = 8; /* parameter for sbreserve() */ 72 73 /************************************************************************ 74 * signalsockbuf procedures * 75 ************************************************************************/ 76 77 /* 78 * Wait for data to arrive at/drain from a socket buffer. 79 * 80 * NOTE: Caller must generally hold the ssb_lock (client side lock) since 81 * WAIT/WAKEUP only works for one client at a time. 82 * 83 * NOTE: Caller always retries whatever operation it was waiting on. 84 */ 85 int 86 ssb_wait(struct signalsockbuf *ssb) 87 { 88 uint32_t flags; 89 int pflags; 90 int error; 91 92 pflags = (ssb->ssb_flags & SSB_NOINTR) ? 0 : PCATCH; 93 94 for (;;) { 95 flags = ssb->ssb_flags; 96 cpu_ccfence(); 97 98 /* 99 * WAKEUP and WAIT interlock eachother. We can catch the 100 * race by checking to see if WAKEUP has already been set, 101 * and only setting WAIT if WAKEUP is clear. 102 */ 103 if (flags & SSB_WAKEUP) { 104 if (atomic_cmpset_int(&ssb->ssb_flags, flags, 105 flags & ~SSB_WAKEUP)) { 106 error = 0; 107 break; 108 } 109 continue; 110 } 111 112 /* 113 * Only set WAIT if WAKEUP is clear. 114 */ 115 tsleep_interlock(&ssb->ssb_cc, pflags); 116 if (atomic_cmpset_int(&ssb->ssb_flags, flags, 117 flags | SSB_WAIT)) { 118 error = tsleep(&ssb->ssb_cc, pflags | PINTERLOCKED, 119 "sbwait", ssb->ssb_timeo); 120 break; 121 } 122 } 123 return (error); 124 } 125 126 /* 127 * Lock a sockbuf already known to be locked; 128 * return any error returned from sleep (EINTR). 129 */ 130 int 131 _ssb_lock(struct signalsockbuf *ssb) 132 { 133 uint32_t flags; 134 int pflags; 135 int error; 136 137 pflags = (ssb->ssb_flags & SSB_NOINTR) ? 0 : PCATCH; 138 139 for (;;) { 140 flags = ssb->ssb_flags; 141 cpu_ccfence(); 142 if (flags & SSB_LOCK) { 143 tsleep_interlock(&ssb->ssb_flags, pflags); 144 if (atomic_cmpset_int(&ssb->ssb_flags, flags, 145 flags | SSB_WANT)) { 146 error = tsleep(&ssb->ssb_flags, 147 pflags | PINTERLOCKED, 148 "sblock", 0); 149 if (error) 150 break; 151 } 152 } else { 153 if (atomic_cmpset_int(&ssb->ssb_flags, flags, 154 flags | SSB_LOCK)) { 155 error = 0; 156 break; 157 } 158 } 159 } 160 return (error); 161 } 162 163 /* 164 * This does the same for sockbufs. Note that the xsockbuf structure, 165 * since it is always embedded in a socket, does not include a self 166 * pointer nor a length. We make this entry point public in case 167 * some other mechanism needs it. 168 */ 169 void 170 ssbtoxsockbuf(struct signalsockbuf *ssb, struct xsockbuf *xsb) 171 { 172 xsb->sb_cc = ssb->ssb_cc; 173 xsb->sb_hiwat = ssb->ssb_hiwat; 174 xsb->sb_mbcnt = ssb->ssb_mbcnt; 175 xsb->sb_mbmax = ssb->ssb_mbmax; 176 xsb->sb_lowat = ssb->ssb_lowat; 177 xsb->sb_flags = ssb->ssb_flags; 178 xsb->sb_timeo = ssb->ssb_timeo; 179 } 180 181 182 /************************************************************************ 183 * Procedures which manipulate socket state flags, wakeups, etc. * 184 ************************************************************************ 185 * 186 * Normal sequence from the active (originating) side is that 187 * soisconnecting() is called during processing of connect() call, resulting 188 * in an eventual call to soisconnected() if/when the connection is 189 * established. When the connection is torn down soisdisconnecting() is 190 * called during processing of disconnect() call, and soisdisconnected() is 191 * called when the connection to the peer is totally severed. 192 * 193 * The semantics of these routines are such that connectionless protocols 194 * can call soisconnected() and soisdisconnected() only, bypassing the 195 * in-progress calls when setting up a ``connection'' takes no time. 196 * 197 * From the passive side, a socket is created with two queues of sockets: 198 * so_incomp for connections in progress and so_comp for connections 199 * already made and awaiting user acceptance. As a protocol is preparing 200 * incoming connections, it creates a socket structure queued on so_incomp 201 * by calling sonewconn(). When the connection is established, 202 * soisconnected() is called, and transfers the socket structure to so_comp, 203 * making it available to accept(). 204 * 205 * If a socket is closed with sockets on either so_incomp or so_comp, these 206 * sockets are dropped. 207 * 208 * If higher level protocols are implemented in the kernel, the wakeups 209 * done here will sometimes cause software-interrupt process scheduling. 210 */ 211 212 void 213 soisconnecting(struct socket *so) 214 { 215 so->so_state &= ~(SS_ISCONNECTED|SS_ISDISCONNECTING); 216 so->so_state |= SS_ISCONNECTING; 217 } 218 219 void 220 soisconnected(struct socket *so) 221 { 222 struct socket *head = so->so_head; 223 224 so->so_state &= ~(SS_ISCONNECTING|SS_ISDISCONNECTING|SS_ISCONFIRMING); 225 so->so_state |= SS_ISCONNECTED; 226 if (head && (so->so_state & SS_INCOMP)) { 227 if ((so->so_options & SO_ACCEPTFILTER) != 0) { 228 so->so_upcall = head->so_accf->so_accept_filter->accf_callback; 229 so->so_upcallarg = head->so_accf->so_accept_filter_arg; 230 atomic_set_int(&so->so_rcv.ssb_flags, SSB_UPCALL); 231 so->so_options &= ~SO_ACCEPTFILTER; 232 so->so_upcall(so, so->so_upcallarg, 0); 233 return; 234 } 235 TAILQ_REMOVE(&head->so_incomp, so, so_list); 236 head->so_incqlen--; 237 so->so_state &= ~SS_INCOMP; 238 TAILQ_INSERT_TAIL(&head->so_comp, so, so_list); 239 head->so_qlen++; 240 so->so_state |= SS_COMP; 241 sorwakeup(head); 242 wakeup_one(&head->so_timeo); 243 } else { 244 wakeup(&so->so_timeo); 245 sorwakeup(so); 246 sowwakeup(so); 247 } 248 } 249 250 void 251 soisdisconnecting(struct socket *so) 252 { 253 so->so_state &= ~SS_ISCONNECTING; 254 so->so_state |= (SS_ISDISCONNECTING|SS_CANTRCVMORE|SS_CANTSENDMORE); 255 wakeup((caddr_t)&so->so_timeo); 256 sowwakeup(so); 257 sorwakeup(so); 258 } 259 260 void 261 soisdisconnected(struct socket *so) 262 { 263 so->so_state &= ~(SS_ISCONNECTING|SS_ISCONNECTED|SS_ISDISCONNECTING); 264 so->so_state |= (SS_CANTRCVMORE|SS_CANTSENDMORE|SS_ISDISCONNECTED); 265 wakeup((caddr_t)&so->so_timeo); 266 sbdrop(&so->so_snd.sb, so->so_snd.ssb_cc); 267 sowwakeup(so); 268 sorwakeup(so); 269 } 270 271 void 272 soisreconnecting(struct socket *so) 273 { 274 so->so_state &= ~(SS_ISDISCONNECTING|SS_ISDISCONNECTED|SS_CANTRCVMORE| 275 SS_CANTSENDMORE); 276 so->so_state |= SS_ISCONNECTING; 277 } 278 279 void 280 soisreconnected(struct socket *so) 281 { 282 so->so_state &= ~(SS_ISDISCONNECTED|SS_CANTRCVMORE|SS_CANTSENDMORE); 283 soisconnected(so); 284 } 285 286 /* 287 * Set or change the message port a socket receives commands on. 288 * 289 * XXX 290 */ 291 void 292 sosetport(struct socket *so, lwkt_port_t port) 293 { 294 so->so_port = port; 295 } 296 297 /* 298 * When an attempt at a new connection is noted on a socket 299 * which accepts connections, sonewconn is called. If the 300 * connection is possible (subject to space constraints, etc.) 301 * then we allocate a new structure, propoerly linked into the 302 * data structure of the original socket, and return this. 303 * Connstatus may be 0, or SO_ISCONFIRMING, or SO_ISCONNECTED. 304 */ 305 struct socket * 306 sonewconn(struct socket *head, int connstatus) 307 { 308 struct socket *so; 309 struct socket *sp; 310 struct pru_attach_info ai; 311 312 if (head->so_qlen > 3 * head->so_qlimit / 2) 313 return (NULL); 314 so = soalloc(1); 315 if (so == NULL) 316 return (NULL); 317 if ((head->so_options & SO_ACCEPTFILTER) != 0) 318 connstatus = 0; 319 so->so_head = head; 320 so->so_type = head->so_type; 321 so->so_options = head->so_options &~ SO_ACCEPTCONN; 322 so->so_linger = head->so_linger; 323 so->so_state = head->so_state | SS_NOFDREF; 324 so->so_proto = head->so_proto; 325 so->so_cred = crhold(head->so_cred); 326 ai.sb_rlimit = NULL; 327 ai.p_ucred = NULL; 328 ai.fd_rdir = NULL; /* jail code cruft XXX JH */ 329 if (soreserve(so, head->so_snd.ssb_hiwat, head->so_rcv.ssb_hiwat, NULL) || 330 /* Directly call function since we're already at protocol level. */ 331 (*so->so_proto->pr_usrreqs->pru_attach)(so, 0, &ai)) { 332 sodealloc(so); 333 return (NULL); 334 } 335 KKASSERT(so->so_port != NULL); 336 so->so_rcv.ssb_lowat = head->so_rcv.ssb_lowat; 337 so->so_snd.ssb_lowat = head->so_snd.ssb_lowat; 338 so->so_rcv.ssb_timeo = head->so_rcv.ssb_timeo; 339 so->so_snd.ssb_timeo = head->so_snd.ssb_timeo; 340 so->so_rcv.ssb_flags |= head->so_rcv.ssb_flags & 341 (SSB_AUTOSIZE | SSB_AUTOLOWAT); 342 so->so_snd.ssb_flags |= head->so_snd.ssb_flags & 343 (SSB_AUTOSIZE | SSB_AUTOLOWAT); 344 if (connstatus) { 345 TAILQ_INSERT_TAIL(&head->so_comp, so, so_list); 346 so->so_state |= SS_COMP; 347 head->so_qlen++; 348 } else { 349 if (head->so_incqlen > head->so_qlimit) { 350 sp = TAILQ_FIRST(&head->so_incomp); 351 TAILQ_REMOVE(&head->so_incomp, sp, so_list); 352 head->so_incqlen--; 353 sp->so_state &= ~SS_INCOMP; 354 sp->so_head = NULL; 355 soaborta(sp); 356 } 357 TAILQ_INSERT_TAIL(&head->so_incomp, so, so_list); 358 so->so_state |= SS_INCOMP; 359 head->so_incqlen++; 360 } 361 if (connstatus) { 362 sorwakeup(head); 363 wakeup((caddr_t)&head->so_timeo); 364 so->so_state |= connstatus; 365 } 366 return (so); 367 } 368 369 /* 370 * Socantsendmore indicates that no more data will be sent on the 371 * socket; it would normally be applied to a socket when the user 372 * informs the system that no more data is to be sent, by the protocol 373 * code (in case PRU_SHUTDOWN). Socantrcvmore indicates that no more data 374 * will be received, and will normally be applied to the socket by a 375 * protocol when it detects that the peer will send no more data. 376 * Data queued for reading in the socket may yet be read. 377 */ 378 void 379 socantsendmore(struct socket *so) 380 { 381 so->so_state |= SS_CANTSENDMORE; 382 sowwakeup(so); 383 } 384 385 void 386 socantrcvmore(struct socket *so) 387 { 388 so->so_state |= SS_CANTRCVMORE; 389 sorwakeup(so); 390 } 391 392 /* 393 * Wakeup processes waiting on a socket buffer. Do asynchronous notification 394 * via SIGIO if the socket has the SS_ASYNC flag set. 395 * 396 * For users waiting on send/recv try to avoid unnecessary context switch 397 * thrashing. Particularly for senders of large buffers (needs to be 398 * extended to sel and aio? XXX) 399 */ 400 void 401 sowakeup(struct socket *so, struct signalsockbuf *ssb) 402 { 403 struct kqinfo *kqinfo = &ssb->ssb_kq; 404 uint32_t flags; 405 406 /* 407 * Check conditions, set the WAKEUP flag, and clear and signal if 408 * the WAIT flag is found to be set. This interlocks against the 409 * client side. 410 */ 411 for (;;) { 412 flags = ssb->ssb_flags; 413 cpu_ccfence(); 414 415 if ((ssb == &so->so_snd && ssb_space(ssb) >= ssb->ssb_lowat) || 416 (ssb == &so->so_rcv && ssb->ssb_cc >= ssb->ssb_lowat) || 417 (ssb == &so->so_snd && (so->so_state & SS_CANTSENDMORE)) || 418 (ssb == &so->so_rcv && (so->so_state & SS_CANTRCVMORE)) 419 ) { 420 if (atomic_cmpset_int(&ssb->ssb_flags, flags, 421 (flags | SSB_WAKEUP) & ~SSB_WAIT)) { 422 if (flags & SSB_WAIT) 423 wakeup(&ssb->ssb_cc); 424 break; 425 } 426 } else { 427 break; 428 } 429 } 430 431 /* 432 * Misc other events 433 */ 434 if ((so->so_state & SS_ASYNC) && so->so_sigio != NULL) 435 pgsigio(so->so_sigio, SIGIO, 0); 436 if (ssb->ssb_flags & SSB_UPCALL) 437 (*so->so_upcall)(so, so->so_upcallarg, MB_DONTWAIT); 438 if (ssb->ssb_flags & SSB_AIO) 439 aio_swake(so, ssb); 440 KNOTE(&kqinfo->ki_note, 0); 441 if (ssb->ssb_flags & SSB_MEVENT) { 442 struct netmsg_so_notify *msg, *nmsg; 443 444 TAILQ_FOREACH_MUTABLE(msg, &kqinfo->ki_mlist, nm_list, nmsg) { 445 if (msg->nm_predicate(&msg->nm_netmsg)) { 446 TAILQ_REMOVE(&kqinfo->ki_mlist, msg, nm_list); 447 lwkt_replymsg(&msg->nm_netmsg.nm_lmsg, 448 msg->nm_netmsg.nm_lmsg.ms_error); 449 } 450 } 451 if (TAILQ_EMPTY(&ssb->ssb_kq.ki_mlist)) 452 atomic_clear_int(&ssb->ssb_flags, SSB_MEVENT); 453 } 454 } 455 456 /* 457 * Socket buffer (struct signalsockbuf) utility routines. 458 * 459 * Each socket contains two socket buffers: one for sending data and 460 * one for receiving data. Each buffer contains a queue of mbufs, 461 * information about the number of mbufs and amount of data in the 462 * queue, and other fields allowing kevent()/select()/poll() statements 463 * and notification on data availability to be implemented. 464 * 465 * Data stored in a socket buffer is maintained as a list of records. 466 * Each record is a list of mbufs chained together with the m_next 467 * field. Records are chained together with the m_nextpkt field. The upper 468 * level routine soreceive() expects the following conventions to be 469 * observed when placing information in the receive buffer: 470 * 471 * 1. If the protocol requires each message be preceded by the sender's 472 * name, then a record containing that name must be present before 473 * any associated data (mbuf's must be of type MT_SONAME). 474 * 2. If the protocol supports the exchange of ``access rights'' (really 475 * just additional data associated with the message), and there are 476 * ``rights'' to be received, then a record containing this data 477 * should be present (mbuf's must be of type MT_RIGHTS). 478 * 3. If a name or rights record exists, then it must be followed by 479 * a data record, perhaps of zero length. 480 * 481 * Before using a new socket structure it is first necessary to reserve 482 * buffer space to the socket, by calling sbreserve(). This should commit 483 * some of the available buffer space in the system buffer pool for the 484 * socket (currently, it does nothing but enforce limits). The space 485 * should be released by calling ssb_release() when the socket is destroyed. 486 */ 487 int 488 soreserve(struct socket *so, u_long sndcc, u_long rcvcc, struct rlimit *rl) 489 { 490 if (so->so_snd.ssb_lowat == 0) 491 atomic_set_int(&so->so_snd.ssb_flags, SSB_AUTOLOWAT); 492 if (ssb_reserve(&so->so_snd, sndcc, so, rl) == 0) 493 goto bad; 494 if (ssb_reserve(&so->so_rcv, rcvcc, so, rl) == 0) 495 goto bad2; 496 if (so->so_rcv.ssb_lowat == 0) 497 so->so_rcv.ssb_lowat = 1; 498 if (so->so_snd.ssb_lowat == 0) 499 so->so_snd.ssb_lowat = MCLBYTES; 500 if (so->so_snd.ssb_lowat > so->so_snd.ssb_hiwat) 501 so->so_snd.ssb_lowat = so->so_snd.ssb_hiwat; 502 return (0); 503 bad2: 504 ssb_release(&so->so_snd, so); 505 bad: 506 return (ENOBUFS); 507 } 508 509 static int 510 sysctl_handle_sb_max(SYSCTL_HANDLER_ARGS) 511 { 512 int error = 0; 513 u_long old_sb_max = sb_max; 514 515 error = SYSCTL_OUT(req, arg1, sizeof(int)); 516 if (error || !req->newptr) 517 return (error); 518 error = SYSCTL_IN(req, arg1, sizeof(int)); 519 if (error) 520 return (error); 521 if (sb_max < MSIZE + MCLBYTES) { 522 sb_max = old_sb_max; 523 return (EINVAL); 524 } 525 sb_max_adj = (u_quad_t)sb_max * MCLBYTES / (MSIZE + MCLBYTES); 526 return (0); 527 } 528 529 /* 530 * Allot mbufs to a signalsockbuf. 531 * 532 * Attempt to scale mbmax so that mbcnt doesn't become limiting 533 * if buffering efficiency is near the normal case. 534 * 535 * sb_max only applies to user-sockets (where rl != NULL). It does 536 * not apply to kernel sockets or kernel-controlled sockets. Note 537 * that NFS overrides the sockbuf limits created when nfsd creates 538 * a socket. 539 */ 540 int 541 ssb_reserve(struct signalsockbuf *ssb, u_long cc, struct socket *so, 542 struct rlimit *rl) 543 { 544 /* 545 * rl will only be NULL when we're in an interrupt (eg, in tcp_input) 546 * or when called from netgraph (ie, ngd_attach) 547 */ 548 if (rl && cc > sb_max_adj) 549 cc = sb_max_adj; 550 if (!chgsbsize(so->so_cred->cr_uidinfo, &ssb->ssb_hiwat, cc, 551 rl ? rl->rlim_cur : RLIM_INFINITY)) { 552 return (0); 553 } 554 if (rl) 555 ssb->ssb_mbmax = min(cc * sb_efficiency, sb_max); 556 else 557 ssb->ssb_mbmax = cc * sb_efficiency; 558 559 /* 560 * AUTOLOWAT is set on send buffers and prevents large writes 561 * from generating a huge number of context switches. 562 */ 563 if (ssb->ssb_flags & SSB_AUTOLOWAT) { 564 ssb->ssb_lowat = ssb->ssb_hiwat / 2; 565 if (ssb->ssb_lowat < MCLBYTES) 566 ssb->ssb_lowat = MCLBYTES; 567 } 568 if (ssb->ssb_lowat > ssb->ssb_hiwat) 569 ssb->ssb_lowat = ssb->ssb_hiwat; 570 return (1); 571 } 572 573 /* 574 * Free mbufs held by a socket, and reserved mbuf space. 575 */ 576 void 577 ssb_release(struct signalsockbuf *ssb, struct socket *so) 578 { 579 sbflush(&ssb->sb); 580 (void)chgsbsize(so->so_cred->cr_uidinfo, &ssb->ssb_hiwat, 0, 581 RLIM_INFINITY); 582 ssb->ssb_mbmax = 0; 583 } 584 585 /* 586 * Some routines that return EOPNOTSUPP for entry points that are not 587 * supported by a protocol. Fill in as needed. 588 */ 589 int 590 pru_accept_notsupp(struct socket *so, struct sockaddr **nam) 591 { 592 return EOPNOTSUPP; 593 } 594 595 int 596 pru_bind_notsupp(struct socket *so, struct sockaddr *nam, struct thread *td) 597 { 598 return EOPNOTSUPP; 599 } 600 601 int 602 pru_connect_notsupp(struct socket *so, struct sockaddr *nam, struct thread *td) 603 { 604 return EOPNOTSUPP; 605 } 606 607 int 608 pru_connect2_notsupp(struct socket *so1, struct socket *so2) 609 { 610 return EOPNOTSUPP; 611 } 612 613 int 614 pru_control_notsupp(struct socket *so, u_long cmd, caddr_t data, 615 struct ifnet *ifp, struct thread *td) 616 { 617 return EOPNOTSUPP; 618 } 619 620 int 621 pru_disconnect_notsupp(struct socket *so) 622 { 623 return EOPNOTSUPP; 624 } 625 626 int 627 pru_listen_notsupp(struct socket *so, struct thread *td) 628 { 629 return EOPNOTSUPP; 630 } 631 632 int 633 pru_peeraddr_notsupp(struct socket *so, struct sockaddr **nam) 634 { 635 return EOPNOTSUPP; 636 } 637 638 int 639 pru_rcvd_notsupp(struct socket *so, int flags) 640 { 641 return EOPNOTSUPP; 642 } 643 644 int 645 pru_rcvoob_notsupp(struct socket *so, struct mbuf *m, int flags) 646 { 647 return EOPNOTSUPP; 648 } 649 650 int 651 pru_shutdown_notsupp(struct socket *so) 652 { 653 return EOPNOTSUPP; 654 } 655 656 int 657 pru_sockaddr_notsupp(struct socket *so, struct sockaddr **nam) 658 { 659 return EOPNOTSUPP; 660 } 661 662 int 663 pru_sosend_notsupp(struct socket *so, struct sockaddr *addr, struct uio *uio, 664 struct mbuf *top, struct mbuf *control, int flags, 665 struct thread *td) 666 { 667 if (top) 668 m_freem(top); 669 if (control) 670 m_freem(control); 671 return (EOPNOTSUPP); 672 } 673 674 int 675 pru_soreceive_notsupp(struct socket *so, struct sockaddr **paddr, 676 struct uio *uio, struct sockbuf *sio, 677 struct mbuf **controlp, int *flagsp) 678 { 679 return (EOPNOTSUPP); 680 } 681 682 int 683 pru_ctloutput_notsupp(struct socket *so, struct sockopt *sopt) 684 { 685 return (EOPNOTSUPP); 686 } 687 688 /* 689 * This isn't really a ``null'' operation, but it's the default one 690 * and doesn't do anything destructive. 691 */ 692 int 693 pru_sense_null(struct socket *so, struct stat *sb) 694 { 695 sb->st_blksize = so->so_snd.ssb_hiwat; 696 return 0; 697 } 698 699 /* 700 * Make a copy of a sockaddr in a malloced buffer of type M_SONAME. Callers 701 * of this routine assume that it always succeeds, so we have to use a 702 * blockable allocation even though we might be called from a critical thread. 703 */ 704 struct sockaddr * 705 dup_sockaddr(const struct sockaddr *sa) 706 { 707 struct sockaddr *sa2; 708 709 sa2 = kmalloc(sa->sa_len, M_SONAME, M_INTWAIT); 710 bcopy(sa, sa2, sa->sa_len); 711 return (sa2); 712 } 713 714 /* 715 * Create an external-format (``xsocket'') structure using the information 716 * in the kernel-format socket structure pointed to by so. This is done 717 * to reduce the spew of irrelevant information over this interface, 718 * to isolate user code from changes in the kernel structure, and 719 * potentially to provide information-hiding if we decide that 720 * some of this information should be hidden from users. 721 */ 722 void 723 sotoxsocket(struct socket *so, struct xsocket *xso) 724 { 725 xso->xso_len = sizeof *xso; 726 xso->xso_so = so; 727 xso->so_type = so->so_type; 728 xso->so_options = so->so_options; 729 xso->so_linger = so->so_linger; 730 xso->so_state = so->so_state; 731 xso->so_pcb = so->so_pcb; 732 xso->xso_protocol = so->so_proto->pr_protocol; 733 xso->xso_family = so->so_proto->pr_domain->dom_family; 734 xso->so_qlen = so->so_qlen; 735 xso->so_incqlen = so->so_incqlen; 736 xso->so_qlimit = so->so_qlimit; 737 xso->so_timeo = so->so_timeo; 738 xso->so_error = so->so_error; 739 xso->so_pgid = so->so_sigio ? so->so_sigio->sio_pgid : 0; 740 xso->so_oobmark = so->so_oobmark; 741 ssbtoxsockbuf(&so->so_snd, &xso->so_snd); 742 ssbtoxsockbuf(&so->so_rcv, &xso->so_rcv); 743 xso->so_uid = so->so_cred->cr_uid; 744 } 745 746 /* 747 * Here is the definition of some of the basic objects in the kern.ipc 748 * branch of the MIB. 749 */ 750 SYSCTL_NODE(_kern, KERN_IPC, ipc, CTLFLAG_RW, 0, "IPC"); 751 752 /* 753 * This takes the place of kern.maxsockbuf, which moved to kern.ipc. 754 * 755 * NOTE! sb_max only applies to user-created socket buffers. 756 */ 757 static int dummy; 758 SYSCTL_INT(_kern, KERN_DUMMY, dummy, CTLFLAG_RW, &dummy, 0, ""); 759 SYSCTL_OID(_kern_ipc, KIPC_MAXSOCKBUF, maxsockbuf, CTLTYPE_INT|CTLFLAG_RW, 760 &sb_max, 0, sysctl_handle_sb_max, "I", "Maximum socket buffer size"); 761 SYSCTL_INT(_kern_ipc, OID_AUTO, maxsockets, CTLFLAG_RD, 762 &maxsockets, 0, "Maximum number of sockets available"); 763 SYSCTL_INT(_kern_ipc, KIPC_SOCKBUF_WASTE, sockbuf_waste_factor, CTLFLAG_RW, 764 &sb_efficiency, 0, ""); 765 766 /* 767 * Initialize maxsockets 768 */ 769 static void 770 init_maxsockets(void *ignored) 771 { 772 TUNABLE_INT_FETCH("kern.ipc.maxsockets", &maxsockets); 773 maxsockets = imax(maxsockets, imax(maxfiles, nmbclusters)); 774 } 775 SYSINIT(param, SI_BOOT1_TUNABLES, SI_ORDER_ANY, 776 init_maxsockets, NULL); 777 778