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-2007 Robert N. M. Watson 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without --- 81 unchanged lines hidden (view full) --- 90 * calls to explicitly manage socket references, soref(), and sorele(). 91 * Currently, these are generally required only when transitioning a socket 92 * from a listen queue to a file descriptor, in order to prevent garbage 93 * collection of the socket at an untimely moment. For a number of reasons, 94 * these interfaces are not preferred, and should be avoided. 95 */ 96 97#include <sys/cdefs.h> |
98__FBSDID("$FreeBSD: head/sys/kern/uipc_socket.c 169624 2007-05-16 20:41:08Z rwatson $"); |
99 100#include "opt_inet.h" 101#include "opt_mac.h" 102#include "opt_zero.h" 103#include "opt_compat.h" 104 105#include <sys/param.h> 106#include <sys/systm.h> --- 215 unchanged lines hidden (view full) --- 322 uma_zfree(socket_zone, so); 323} 324 325/* 326 * socreate returns a socket with a ref count of 1. The socket should be 327 * closed with soclose(). 328 */ 329int |
330socreate(int dom, struct socket **aso, int type, int proto, 331 struct ucred *cred, struct thread *td) |
332{ 333 struct protosw *prp; 334 struct socket *so; 335 int error; 336 337 if (proto) 338 prp = pffindproto(dom, proto, type); 339 else --- 56 unchanged lines hidden (view full) --- 396 * connections, sonewconn is called. If the connection is possible (subject 397 * to space constraints, etc.) then we allocate a new structure, propoerly 398 * linked into the data structure of the original socket, and return this. 399 * Connstatus may be 0, or SO_ISCONFIRMING, or SO_ISCONNECTED. 400 * 401 * Note: the ref count on the socket is 0 on return. 402 */ 403struct socket * |
404sonewconn(struct socket *head, int connstatus) |
405{ |
406 struct socket *so; |
407 int over; 408 409 ACCEPT_LOCK(); 410 over = (head->so_qlen > 3 * head->so_qlimit / 2); 411 ACCEPT_UNLOCK(); 412#ifdef REGRESSION 413 if (regression_sonewconn_earlytest && over) 414#else --- 65 unchanged lines hidden (view full) --- 480 if (connstatus) { 481 sorwakeup(head); 482 wakeup_one(&head->so_timeo); 483 } 484 return (so); 485} 486 487int |
488sobind(struct socket *so, struct sockaddr *nam, struct thread *td) |
489{ 490 491 return ((*so->so_proto->pr_usrreqs->pru_bind)(so, nam, td)); 492} 493 494/* 495 * solisten() transitions a socket from a non-listening state to a listening 496 * state, but can also be used to update the listen queue depth on an 497 * existing listen socket. The protocol will call back into the sockets 498 * layer using solisten_proto_check() and solisten_proto() to check and set 499 * socket-layer listen state. Call backs are used so that the protocol can 500 * acquire both protocol and socket layer locks in whatever order is required 501 * by the protocol. 502 * 503 * Protocol implementors are advised to hold the socket lock across the 504 * socket-layer test and set to avoid races at the socket layer. 505 */ 506int |
507solisten(struct socket *so, int backlog, struct thread *td) |
508{ 509 510 return ((*so->so_proto->pr_usrreqs->pru_listen)(so, backlog, td)); 511} 512 513int |
514solisten_proto_check(struct socket *so) |
515{ 516 517 SOCK_LOCK_ASSERT(so); 518 519 if (so->so_state & (SS_ISCONNECTED | SS_ISCONNECTING | 520 SS_ISDISCONNECTING)) 521 return (EINVAL); 522 return (0); 523} 524 525void |
526solisten_proto(struct socket *so, int backlog) |
527{ 528 529 SOCK_LOCK_ASSERT(so); 530 531 if (backlog < 0 || backlog > somaxconn) 532 backlog = somaxconn; 533 so->so_qlimit = backlog; 534 so->so_options |= SO_ACCEPTCONN; --- 15 unchanged lines hidden (view full) --- 550 * - The socket is not in a completed connection queue, so a process has been 551 * notified that it is present. If it is removed, the user process may 552 * block in accept() despite select() saying the socket was ready. 553 * 554 * Otherwise, it will quietly abort so that a future call to sofree(), when 555 * conditions are right, can succeed. 556 */ 557void |
558sofree(struct socket *so) |
559{ 560 struct protosw *pr = so->so_proto; 561 struct socket *head; 562 563 ACCEPT_LOCK_ASSERT(); 564 SOCK_LOCK_ASSERT(so); 565 566 if ((so->so_state & SS_NOFDREF) == 0 || so->so_count != 0 || --- 58 unchanged lines hidden (view full) --- 625 * Close a socket on last file table reference removal. Initiate disconnect 626 * if connected. Free socket when disconnect complete. 627 * 628 * This function will sorele() the socket. Note that soclose() may be called 629 * prior to the ref count reaching zero. The actual socket structure will 630 * not be freed until the ref count reaches zero. 631 */ 632int |
633soclose(struct socket *so) |
634{ 635 int error = 0; 636 637 KASSERT(!(so->so_state & SS_NOFDREF), ("soclose: SS_NOFDREF on enter")); 638 639 funsetown(&so->so_sigio); 640 if (so->so_state & SS_ISCONNECTED) { 641 if ((so->so_state & SS_ISDISCONNECTING) == 0) { --- 58 unchanged lines hidden (view full) --- 700 * from the listen queue it was on, or races with other threads are risked. 701 * 702 * This interface will call into the protocol code, so must not be called 703 * with any socket locks held. Protocols do call it while holding their own 704 * recursible protocol mutexes, but this is something that should be subject 705 * to review in the future. 706 */ 707void |
708soabort(struct socket *so) |
709{ 710 711 /* 712 * In as much as is possible, assert that no references to this 713 * socket are held. This is not quite the same as asserting that the 714 * current thread is responsible for arranging for no references, but 715 * is as close as we can get for now. 716 */ --- 6 unchanged lines hidden (view full) --- 723 if (so->so_proto->pr_usrreqs->pru_abort != NULL) 724 (*so->so_proto->pr_usrreqs->pru_abort)(so); 725 ACCEPT_LOCK(); 726 SOCK_LOCK(so); 727 sofree(so); 728} 729 730int |
731soaccept(struct socket *so, struct sockaddr **nam) |
732{ 733 int error; 734 735 SOCK_LOCK(so); 736 KASSERT((so->so_state & SS_NOFDREF) != 0, ("soaccept: !NOFDREF")); 737 so->so_state &= ~SS_NOFDREF; 738 SOCK_UNLOCK(so); 739 error = (*so->so_proto->pr_usrreqs->pru_accept)(so, nam); 740 return (error); 741} 742 743int |
744soconnect(struct socket *so, struct sockaddr *nam, struct thread *td) |
745{ 746 int error; 747 748 if (so->so_options & SO_ACCEPTCONN) 749 return (EOPNOTSUPP); 750 /* 751 * If protocol is connection-based, can only connect once. 752 * Otherwise, if connected, try to disconnect first. This allows --- 11 unchanged lines hidden (view full) --- 764 so->so_error = 0; 765 error = (*so->so_proto->pr_usrreqs->pru_connect)(so, nam, td); 766 } 767 768 return (error); 769} 770 771int |
772soconnect2(struct socket *so1, struct socket *so2) |
773{ 774 775 return ((*so1->so_proto->pr_usrreqs->pru_connect2)(so1, so2)); 776} 777 778int |
779sodisconnect(struct socket *so) |
780{ 781 int error; 782 783 if ((so->so_state & SS_ISCONNECTED) == 0) 784 return (ENOTCONN); 785 if (so->so_state & SS_ISDISCONNECTING) 786 return (EALREADY); 787 error = (*so->so_proto->pr_usrreqs->pru_disconnect)(so); --- 126 unchanged lines hidden (view full) --- 914 *retmp = top; 915 return (error); 916} 917#endif /*ZERO_COPY_SOCKETS*/ 918 919#define SBLOCKWAIT(f) (((f) & MSG_DONTWAIT) ? M_NOWAIT : M_WAITOK) 920 921int |
922sosend_dgram(struct socket *so, struct sockaddr *addr, struct uio *uio, 923 struct mbuf *top, struct mbuf *control, int flags, struct thread *td) |
924{ 925 long space, resid; 926 int clen = 0, error, dontroute; 927#ifdef ZERO_COPY_SOCKETS 928 int atomic = sosendallatonce(so) || top; 929#endif 930 931 KASSERT(so->so_type == SOCK_DGRAM, ("sodgram_send: !SOCK_DGRAM")); --- 156 unchanged lines hidden (view full) --- 1088 * by the mbuf chain "top" (which must be null if uio is not). Data provided 1089 * in mbuf chain must be small enough to send all at once. 1090 * 1091 * Returns nonzero on error, timeout or signal; callers must check for short 1092 * counts if EINTR/ERESTART are returned. Data and control buffers are freed 1093 * on return. 1094 */ 1095int |
1096sosend_generic(struct socket *so, struct sockaddr *addr, struct uio *uio, 1097 struct mbuf *top, struct mbuf *control, int flags, struct thread *td) |
1098{ 1099 long space, resid; 1100 int clen = 0, error, dontroute; 1101 int atomic = sosendallatonce(so) || top; 1102 1103 if (uio != NULL) 1104 resid = uio->uio_resid; 1105 else --- 164 unchanged lines hidden (view full) --- 1270 if (top != NULL) 1271 m_freem(top); 1272 if (control != NULL) 1273 m_freem(control); 1274 return (error); 1275} 1276 1277int |
1278sosend(struct socket *so, struct sockaddr *addr, struct uio *uio, 1279 struct mbuf *top, struct mbuf *control, int flags, struct thread *td) |
1280{ 1281 1282 /* XXXRW: Temporary debugging. */ 1283 KASSERT(so->so_proto->pr_usrreqs->pru_sosend != sosend, 1284 ("sosend: protocol calls sosend")); 1285 1286 return (so->so_proto->pr_usrreqs->pru_sosend(so, addr, uio, top, 1287 control, flags, td)); 1288} 1289 1290/* 1291 * The part of soreceive() that implements reading non-inline out-of-band 1292 * data from a socket. For more complete comments, see soreceive(), from 1293 * which this code originated. 1294 * 1295 * Note that soreceive_rcvoob(), unlike the remainder of soreceive(), is 1296 * unable to return an mbuf chain to the caller. 1297 */ 1298static int |
1299soreceive_rcvoob(struct socket *so, struct uio *uio, int flags) |
1300{ 1301 struct protosw *pr = so->so_proto; 1302 struct mbuf *m; 1303 int error; 1304 1305 KASSERT(flags & MSG_OOB, ("soreceive_rcvoob: (flags & MSG_OOB) == 0")); 1306 1307 m = m_get(M_TRYWAIT, MT_DATA); --- 76 unchanged lines hidden (view full) --- 1384 * appended, and thus we must maintain consistency of the sockbuf during that 1385 * time. 1386 * 1387 * The caller may receive the data as a single mbuf chain by supplying an 1388 * mbuf **mp0 for use in returning the chain. The uio is then used only for 1389 * the count in uio_resid. 1390 */ 1391int |
1392soreceive_generic(struct socket *so, struct sockaddr **psa, struct uio *uio, 1393 struct mbuf **mp0, struct mbuf **controlp, int *flagsp) |
1394{ 1395 struct mbuf *m, **mp; 1396 int flags, len, error, offset; 1397 struct protosw *pr = so->so_proto; 1398 struct mbuf *nextrecord; 1399 int moff, type = 0; 1400 int orig_resid = uio->uio_resid; 1401 --- 435 unchanged lines hidden (view full) --- 1837 if (flagsp != NULL) 1838 *flagsp |= flags; 1839release: 1840 sbunlock(&so->so_rcv); 1841 return (error); 1842} 1843 1844int |
1845soreceive(struct socket *so, struct sockaddr **psa, struct uio *uio, 1846 struct mbuf **mp0, struct mbuf **controlp, int *flagsp) |
1847{ 1848 1849 /* XXXRW: Temporary debugging. */ 1850 KASSERT(so->so_proto->pr_usrreqs->pru_soreceive != soreceive, 1851 ("soreceive: protocol calls soreceive")); 1852 1853 return (so->so_proto->pr_usrreqs->pru_soreceive(so, psa, uio, mp0, 1854 controlp, flagsp)); 1855} 1856 1857int |
1858soshutdown(struct socket *so, int how) |
1859{ 1860 struct protosw *pr = so->so_proto; 1861 1862 if (!(how == SHUT_RD || how == SHUT_WR || how == SHUT_RDWR)) 1863 return (EINVAL); 1864 1865 if (how != SHUT_WR) 1866 sorflush(so); 1867 if (how != SHUT_RD) 1868 return ((*pr->pr_usrreqs->pru_shutdown)(so)); 1869 return (0); 1870} 1871 1872void |
1873sorflush(struct socket *so) |
1874{ 1875 struct sockbuf *sb = &so->so_rcv; 1876 struct protosw *pr = so->so_proto; 1877 struct sockbuf asb; 1878 1879 /* 1880 * XXXRW: This is quite ugly. Previously, this code made a copy of 1881 * the socket buffer, then zero'd the original to clear the buffer --- 29 unchanged lines hidden (view full) --- 1911/* 1912 * Perhaps this routine, and sooptcopyout(), below, ought to come in an 1913 * additional variant to handle the case where the option value needs to be 1914 * some kind of integer, but not a specific size. In addition to their use 1915 * here, these functions are also called by the protocol-level pr_ctloutput() 1916 * routines. 1917 */ 1918int |
1919sooptcopyin(struct sockopt *sopt, void *buf, size_t len, size_t minlen) |
1920{ 1921 size_t valsize; 1922 1923 /* 1924 * If the user gives us more than we wanted, we ignore it, but if we 1925 * don't get the minimum length the caller wants, we return EINVAL. 1926 * On success, sopt->sopt_valsize is set to however much we actually 1927 * retrieved. --- 26 unchanged lines hidden (view full) --- 1954 sopt.sopt_dir = SOPT_SET; 1955 sopt.sopt_val = optval; 1956 sopt.sopt_valsize = optlen; 1957 sopt.sopt_td = NULL; 1958 return (sosetopt(so, &sopt)); 1959} 1960 1961int |
1962sosetopt(struct socket *so, struct sockopt *sopt) |
1963{ 1964 int error, optval; 1965 struct linger l; 1966 struct timeval tv; 1967 u_long val; 1968#ifdef MAC 1969 struct mac extmac; 1970#endif --- 198 unchanged lines hidden (view full) --- 2169 error = copyout(buf, sopt->sopt_val, valsize); 2170 else 2171 bcopy(buf, sopt->sopt_val, valsize); 2172 } 2173 return (error); 2174} 2175 2176int |
2177sogetopt(struct socket *so, struct sockopt *sopt) |
2178{ 2179 int error, optval; 2180 struct linger l; 2181 struct timeval tv; 2182#ifdef MAC 2183 struct mac extmac; 2184#endif 2185 --- 247 unchanged lines hidden (view full) --- 2433 return (0); 2434} 2435 2436/* 2437 * sohasoutofband(): protocol notifies socket layer of the arrival of new 2438 * out-of-band data, which will then notify socket consumers. 2439 */ 2440void |
2441sohasoutofband(struct socket *so) |
2442{ |
2443 |
2444 if (so->so_sigio != NULL) 2445 pgsigio(&so->so_sigio, SIGURG, 0); 2446 selwakeuppri(&so->so_rcv.sb_sel, PSOCK); 2447} 2448 2449int 2450sopoll(struct socket *so, int events, struct ucred *active_cred, 2451 struct thread *td) --- 82 unchanged lines hidden (view full) --- 2534 2535/* 2536 * Some routines that return EOPNOTSUPP for entry points that are not 2537 * supported by a protocol. Fill in as needed. 2538 */ 2539int 2540pru_accept_notsupp(struct socket *so, struct sockaddr **nam) 2541{ |
2542 |
2543 return EOPNOTSUPP; 2544} 2545 2546int 2547pru_attach_notsupp(struct socket *so, int proto, struct thread *td) 2548{ |
2549 |
2550 return EOPNOTSUPP; 2551} 2552 2553int 2554pru_bind_notsupp(struct socket *so, struct sockaddr *nam, struct thread *td) 2555{ |
2556 |
2557 return EOPNOTSUPP; 2558} 2559 2560int 2561pru_connect_notsupp(struct socket *so, struct sockaddr *nam, struct thread *td) 2562{ |
2563 |
2564 return EOPNOTSUPP; 2565} 2566 2567int 2568pru_connect2_notsupp(struct socket *so1, struct socket *so2) 2569{ |
2570 |
2571 return EOPNOTSUPP; 2572} 2573 2574int 2575pru_control_notsupp(struct socket *so, u_long cmd, caddr_t data, |
2576 struct ifnet *ifp, struct thread *td) |
2577{ |
2578 |
2579 return EOPNOTSUPP; 2580} 2581 2582int 2583pru_disconnect_notsupp(struct socket *so) 2584{ |
2585 |
2586 return EOPNOTSUPP; 2587} 2588 2589int 2590pru_listen_notsupp(struct socket *so, int backlog, struct thread *td) 2591{ |
2592 |
2593 return EOPNOTSUPP; 2594} 2595 2596int 2597pru_peeraddr_notsupp(struct socket *so, struct sockaddr **nam) 2598{ |
2599 |
2600 return EOPNOTSUPP; 2601} 2602 2603int 2604pru_rcvd_notsupp(struct socket *so, int flags) 2605{ |
2606 |
2607 return EOPNOTSUPP; 2608} 2609 2610int 2611pru_rcvoob_notsupp(struct socket *so, struct mbuf *m, int flags) 2612{ |
2613 |
2614 return EOPNOTSUPP; 2615} 2616 2617int 2618pru_send_notsupp(struct socket *so, int flags, struct mbuf *m, |
2619 struct sockaddr *addr, struct mbuf *control, struct thread *td) |
2620{ |
2621 |
2622 return EOPNOTSUPP; 2623} 2624 2625/* 2626 * This isn't really a ``null'' operation, but it's the default one and 2627 * doesn't do anything destructive. 2628 */ 2629int 2630pru_sense_null(struct socket *so, struct stat *sb) 2631{ |
2632 |
2633 sb->st_blksize = so->so_snd.sb_hiwat; 2634 return 0; 2635} 2636 2637int 2638pru_shutdown_notsupp(struct socket *so) 2639{ |
2640 |
2641 return EOPNOTSUPP; 2642} 2643 2644int 2645pru_sockaddr_notsupp(struct socket *so, struct sockaddr **nam) 2646{ |
2647 |
2648 return EOPNOTSUPP; 2649} 2650 2651int 2652pru_sosend_notsupp(struct socket *so, struct sockaddr *addr, struct uio *uio, |
2653 struct mbuf *top, struct mbuf *control, int flags, struct thread *td) |
2654{ |
2655 |
2656 return EOPNOTSUPP; 2657} 2658 2659int 2660pru_soreceive_notsupp(struct socket *so, struct sockaddr **paddr, |
2661 struct uio *uio, struct mbuf **mp0, struct mbuf **controlp, int *flagsp) |
2662{ |
2663 |
2664 return EOPNOTSUPP; 2665} 2666 2667int 2668pru_sopoll_notsupp(struct socket *so, int events, struct ucred *cred, |
2669 struct thread *td) |
2670{ |
2671 |
2672 return EOPNOTSUPP; 2673} 2674 2675static void 2676filt_sordetach(struct knote *kn) 2677{ 2678 struct socket *so = kn->kn_fp->f_data; 2679 --- 129 unchanged lines hidden (view full) --- 2809 * 2810 * If a socket is closed with sockets on either so_incomp or so_comp, these 2811 * sockets are dropped. 2812 * 2813 * If higher-level protocols are implemented in the kernel, the wakeups done 2814 * here will sometimes cause software-interrupt process scheduling. 2815 */ 2816void |
2817soisconnecting(struct socket *so) |
2818{ 2819 2820 SOCK_LOCK(so); 2821 so->so_state &= ~(SS_ISCONNECTED|SS_ISDISCONNECTING); 2822 so->so_state |= SS_ISCONNECTING; 2823 SOCK_UNLOCK(so); 2824} 2825 2826void |
2827soisconnected(struct socket *so) |
2828{ 2829 struct socket *head; 2830 2831 ACCEPT_LOCK(); 2832 SOCK_LOCK(so); 2833 so->so_state &= ~(SS_ISCONNECTING|SS_ISDISCONNECTING|SS_ISCONFIRMING); 2834 so->so_state |= SS_ISCONNECTED; 2835 head = so->so_head; --- 24 unchanged lines hidden (view full) --- 2860 SOCK_UNLOCK(so); 2861 ACCEPT_UNLOCK(); 2862 wakeup(&so->so_timeo); 2863 sorwakeup(so); 2864 sowwakeup(so); 2865} 2866 2867void |
2868soisdisconnecting(struct socket *so) |
2869{ 2870 2871 /* 2872 * Note: This code assumes that SOCK_LOCK(so) and 2873 * SOCKBUF_LOCK(&so->so_rcv) are the same. 2874 */ 2875 SOCKBUF_LOCK(&so->so_rcv); 2876 so->so_state &= ~SS_ISCONNECTING; 2877 so->so_state |= SS_ISDISCONNECTING; 2878 so->so_rcv.sb_state |= SBS_CANTRCVMORE; 2879 sorwakeup_locked(so); 2880 SOCKBUF_LOCK(&so->so_snd); 2881 so->so_snd.sb_state |= SBS_CANTSENDMORE; 2882 sowwakeup_locked(so); 2883 wakeup(&so->so_timeo); 2884} 2885 2886void |
2887soisdisconnected(struct socket *so) |
2888{ 2889 2890 /* 2891 * Note: This code assumes that SOCK_LOCK(so) and 2892 * SOCKBUF_LOCK(&so->so_rcv) are the same. 2893 */ 2894 SOCKBUF_LOCK(&so->so_rcv); 2895 so->so_state &= ~(SS_ISCONNECTING|SS_ISCONNECTED|SS_ISDISCONNECTING); --- 27 unchanged lines hidden (view full) --- 2923 * reduce the spew of irrelevant information over this interface, to isolate 2924 * user code from changes in the kernel structure, and potentially to provide 2925 * information-hiding if we decide that some of this information should be 2926 * hidden from users. 2927 */ 2928void 2929sotoxsocket(struct socket *so, struct xsocket *xso) 2930{ |
2931 |
2932 xso->xso_len = sizeof *xso; 2933 xso->xso_so = so; 2934 xso->so_type = so->so_type; 2935 xso->so_options = so->so_options; 2936 xso->so_linger = so->so_linger; 2937 xso->so_state = so->so_state; 2938 xso->so_pcb = so->so_pcb; 2939 xso->xso_protocol = so->so_proto->pr_protocol; --- 12 unchanged lines hidden --- |