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 169375 2007-05-08 12:34:14Z yongari $");
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(dom, aso, type, proto, cred, td)
331 int dom;
332 struct socket **aso;
333 int type;
334 int proto;
335 struct ucred *cred;
336 struct thread *td;
337{
338 struct protosw *prp;
339 struct socket *so;
340 int error;
341
342 if (proto)
343 prp = pffindproto(dom, proto, type);
344 else
--- 56 unchanged lines hidden (view full) ---
401 * connections, sonewconn is called. If the connection is possible (subject
402 * to space constraints, etc.) then we allocate a new structure, propoerly
403 * linked into the data structure of the original socket, and return this.
404 * Connstatus may be 0, or SO_ISCONFIRMING, or SO_ISCONNECTED.
405 *
406 * Note: the ref count on the socket is 0 on return.
407 */
408struct socket *
409sonewconn(head, connstatus)
410 register struct socket *head;
411 int connstatus;
412{
413 register struct socket *so;
414 int over;
415
416 ACCEPT_LOCK();
417 over = (head->so_qlen > 3 * head->so_qlimit / 2);
418 ACCEPT_UNLOCK();
419#ifdef REGRESSION
420 if (regression_sonewconn_earlytest && over)
421#else
--- 65 unchanged lines hidden (view full) ---
487 if (connstatus) {
488 sorwakeup(head);
489 wakeup_one(&head->so_timeo);
490 }
491 return (so);
492}
493
494int
495sobind(so, nam, td)
496 struct socket *so;
497 struct sockaddr *nam;
498 struct thread *td;
499{
500
501 return ((*so->so_proto->pr_usrreqs->pru_bind)(so, nam, td));
502}
503
504/*
505 * solisten() transitions a socket from a non-listening state to a listening
506 * state, but can also be used to update the listen queue depth on an
507 * existing listen socket. The protocol will call back into the sockets
508 * layer using solisten_proto_check() and solisten_proto() to check and set
509 * socket-layer listen state. Call backs are used so that the protocol can
510 * acquire both protocol and socket layer locks in whatever order is required
511 * by the protocol.
512 *
513 * Protocol implementors are advised to hold the socket lock across the
514 * socket-layer test and set to avoid races at the socket layer.
515 */
516int
517solisten(so, backlog, td)
518 struct socket *so;
519 int backlog;
520 struct thread *td;
521{
522
523 return ((*so->so_proto->pr_usrreqs->pru_listen)(so, backlog, td));
524}
525
526int
527solisten_proto_check(so)
528 struct socket *so;
529{
530
531 SOCK_LOCK_ASSERT(so);
532
533 if (so->so_state & (SS_ISCONNECTED | SS_ISCONNECTING |
534 SS_ISDISCONNECTING))
535 return (EINVAL);
536 return (0);
537}
538
539void
540solisten_proto(so, backlog)
541 struct socket *so;
542 int backlog;
543{
544
545 SOCK_LOCK_ASSERT(so);
546
547 if (backlog < 0 || backlog > somaxconn)
548 backlog = somaxconn;
549 so->so_qlimit = backlog;
550 so->so_options |= SO_ACCEPTCONN;
--- 15 unchanged lines hidden (view full) ---
566 * - The socket is not in a completed connection queue, so a process has been
567 * notified that it is present. If it is removed, the user process may
568 * block in accept() despite select() saying the socket was ready.
569 *
570 * Otherwise, it will quietly abort so that a future call to sofree(), when
571 * conditions are right, can succeed.
572 */
573void
574sofree(so)
575 struct socket *so;
576{
577 struct protosw *pr = so->so_proto;
578 struct socket *head;
579
580 ACCEPT_LOCK_ASSERT();
581 SOCK_LOCK_ASSERT(so);
582
583 if ((so->so_state & SS_NOFDREF) == 0 || so->so_count != 0 ||
--- 58 unchanged lines hidden (view full) ---
642 * Close a socket on last file table reference removal. Initiate disconnect
643 * if connected. Free socket when disconnect complete.
644 *
645 * This function will sorele() the socket. Note that soclose() may be called
646 * prior to the ref count reaching zero. The actual socket structure will
647 * not be freed until the ref count reaches zero.
648 */
649int
650soclose(so)
651 struct socket *so;
652{
653 int error = 0;
654
655 KASSERT(!(so->so_state & SS_NOFDREF), ("soclose: SS_NOFDREF on enter"));
656
657 funsetown(&so->so_sigio);
658 if (so->so_state & SS_ISCONNECTED) {
659 if ((so->so_state & SS_ISDISCONNECTING) == 0) {
--- 58 unchanged lines hidden (view full) ---
718 * from the listen queue it was on, or races with other threads are risked.
719 *
720 * This interface will call into the protocol code, so must not be called
721 * with any socket locks held. Protocols do call it while holding their own
722 * recursible protocol mutexes, but this is something that should be subject
723 * to review in the future.
724 */
725void
726soabort(so)
727 struct socket *so;
728{
729
730 /*
731 * In as much as is possible, assert that no references to this
732 * socket are held. This is not quite the same as asserting that the
733 * current thread is responsible for arranging for no references, but
734 * is as close as we can get for now.
735 */
--- 6 unchanged lines hidden (view full) ---
742 if (so->so_proto->pr_usrreqs->pru_abort != NULL)
743 (*so->so_proto->pr_usrreqs->pru_abort)(so);
744 ACCEPT_LOCK();
745 SOCK_LOCK(so);
746 sofree(so);
747}
748
749int
750soaccept(so, nam)
751 struct socket *so;
752 struct sockaddr **nam;
753{
754 int error;
755
756 SOCK_LOCK(so);
757 KASSERT((so->so_state & SS_NOFDREF) != 0, ("soaccept: !NOFDREF"));
758 so->so_state &= ~SS_NOFDREF;
759 SOCK_UNLOCK(so);
760 error = (*so->so_proto->pr_usrreqs->pru_accept)(so, nam);
761 return (error);
762}
763
764int
765soconnect(so, nam, td)
766 struct socket *so;
767 struct sockaddr *nam;
768 struct thread *td;
769{
770 int error;
771
772 if (so->so_options & SO_ACCEPTCONN)
773 return (EOPNOTSUPP);
774 /*
775 * If protocol is connection-based, can only connect once.
776 * Otherwise, if connected, try to disconnect first. This allows
--- 11 unchanged lines hidden (view full) ---
788 so->so_error = 0;
789 error = (*so->so_proto->pr_usrreqs->pru_connect)(so, nam, td);
790 }
791
792 return (error);
793}
794
795int
796soconnect2(so1, so2)
797 struct socket *so1;
798 struct socket *so2;
799{
800
801 return ((*so1->so_proto->pr_usrreqs->pru_connect2)(so1, so2));
802}
803
804int
805sodisconnect(so)
806 struct socket *so;
807{
808 int error;
809
810 if ((so->so_state & SS_ISCONNECTED) == 0)
811 return (ENOTCONN);
812 if (so->so_state & SS_ISDISCONNECTING)
813 return (EALREADY);
814 error = (*so->so_proto->pr_usrreqs->pru_disconnect)(so);
--- 126 unchanged lines hidden (view full) ---
941 *retmp = top;
942 return (error);
943}
944#endif /*ZERO_COPY_SOCKETS*/
945
946#define SBLOCKWAIT(f) (((f) & MSG_DONTWAIT) ? M_NOWAIT : M_WAITOK)
947
948int
949sosend_dgram(so, addr, uio, top, control, flags, td)
950 struct socket *so;
951 struct sockaddr *addr;
952 struct uio *uio;
953 struct mbuf *top;
954 struct mbuf *control;
955 int flags;
956 struct thread *td;
957{
958 long space, resid;
959 int clen = 0, error, dontroute;
960#ifdef ZERO_COPY_SOCKETS
961 int atomic = sosendallatonce(so) || top;
962#endif
963
964 KASSERT(so->so_type == SOCK_DGRAM, ("sodgram_send: !SOCK_DGRAM"));
--- 156 unchanged lines hidden (view full) ---
1121 * by the mbuf chain "top" (which must be null if uio is not). Data provided
1122 * in mbuf chain must be small enough to send all at once.
1123 *
1124 * Returns nonzero on error, timeout or signal; callers must check for short
1125 * counts if EINTR/ERESTART are returned. Data and control buffers are freed
1126 * on return.
1127 */
1128int
1129sosend_generic(so, addr, uio, top, control, flags, td)
1130 struct socket *so;
1131 struct sockaddr *addr;
1132 struct uio *uio;
1133 struct mbuf *top;
1134 struct mbuf *control;
1135 int flags;
1136 struct thread *td;
1137{
1138 long space, resid;
1139 int clen = 0, error, dontroute;
1140 int atomic = sosendallatonce(so) || top;
1141
1142 if (uio != NULL)
1143 resid = uio->uio_resid;
1144 else
--- 164 unchanged lines hidden (view full) ---
1309 if (top != NULL)
1310 m_freem(top);
1311 if (control != NULL)
1312 m_freem(control);
1313 return (error);
1314}
1315
1316int
1317sosend(so, addr, uio, top, control, flags, td)
1318 struct socket *so;
1319 struct sockaddr *addr;
1320 struct uio *uio;
1321 struct mbuf *top;
1322 struct mbuf *control;
1323 int flags;
1324 struct thread *td;
1325{
1326
1327 /* XXXRW: Temporary debugging. */
1328 KASSERT(so->so_proto->pr_usrreqs->pru_sosend != sosend,
1329 ("sosend: protocol calls sosend"));
1330
1331 return (so->so_proto->pr_usrreqs->pru_sosend(so, addr, uio, top,
1332 control, flags, td));
1333}
1334
1335/*
1336 * The part of soreceive() that implements reading non-inline out-of-band
1337 * data from a socket. For more complete comments, see soreceive(), from
1338 * which this code originated.
1339 *
1340 * Note that soreceive_rcvoob(), unlike the remainder of soreceive(), is
1341 * unable to return an mbuf chain to the caller.
1342 */
1343static int
1344soreceive_rcvoob(so, uio, flags)
1345 struct socket *so;
1346 struct uio *uio;
1347 int flags;
1348{
1349 struct protosw *pr = so->so_proto;
1350 struct mbuf *m;
1351 int error;
1352
1353 KASSERT(flags & MSG_OOB, ("soreceive_rcvoob: (flags & MSG_OOB) == 0"));
1354
1355 m = m_get(M_TRYWAIT, MT_DATA);
--- 76 unchanged lines hidden (view full) ---
1432 * appended, and thus we must maintain consistency of the sockbuf during that
1433 * time.
1434 *
1435 * The caller may receive the data as a single mbuf chain by supplying an
1436 * mbuf **mp0 for use in returning the chain. The uio is then used only for
1437 * the count in uio_resid.
1438 */
1439int
1440soreceive_generic(so, psa, uio, mp0, controlp, flagsp)
1441 struct socket *so;
1442 struct sockaddr **psa;
1443 struct uio *uio;
1444 struct mbuf **mp0;
1445 struct mbuf **controlp;
1446 int *flagsp;
1447{
1448 struct mbuf *m, **mp;
1449 int flags, len, error, offset;
1450 struct protosw *pr = so->so_proto;
1451 struct mbuf *nextrecord;
1452 int moff, type = 0;
1453 int orig_resid = uio->uio_resid;
1454
--- 435 unchanged lines hidden (view full) ---
1890 if (flagsp != NULL)
1891 *flagsp |= flags;
1892release:
1893 sbunlock(&so->so_rcv);
1894 return (error);
1895}
1896
1897int
1898soreceive(so, psa, uio, mp0, controlp, flagsp)
1899 struct socket *so;
1900 struct sockaddr **psa;
1901 struct uio *uio;
1902 struct mbuf **mp0;
1903 struct mbuf **controlp;
1904 int *flagsp;
1905{
1906
1907 /* XXXRW: Temporary debugging. */
1908 KASSERT(so->so_proto->pr_usrreqs->pru_soreceive != soreceive,
1909 ("soreceive: protocol calls soreceive"));
1910
1911 return (so->so_proto->pr_usrreqs->pru_soreceive(so, psa, uio, mp0,
1912 controlp, flagsp));
1913}
1914
1915int
1916soshutdown(so, how)
1917 struct socket *so;
1918 int how;
1919{
1920 struct protosw *pr = so->so_proto;
1921
1922 if (!(how == SHUT_RD || how == SHUT_WR || how == SHUT_RDWR))
1923 return (EINVAL);
1924
1925 if (how != SHUT_WR)
1926 sorflush(so);
1927 if (how != SHUT_RD)
1928 return ((*pr->pr_usrreqs->pru_shutdown)(so));
1929 return (0);
1930}
1931
1932void
1933sorflush(so)
1934 struct socket *so;
1935{
1936 struct sockbuf *sb = &so->so_rcv;
1937 struct protosw *pr = so->so_proto;
1938 struct sockbuf asb;
1939
1940 /*
1941 * XXXRW: This is quite ugly. Previously, this code made a copy of
1942 * the socket buffer, then zero'd the original to clear the buffer
--- 29 unchanged lines hidden (view full) ---
1972/*
1973 * Perhaps this routine, and sooptcopyout(), below, ought to come in an
1974 * additional variant to handle the case where the option value needs to be
1975 * some kind of integer, but not a specific size. In addition to their use
1976 * here, these functions are also called by the protocol-level pr_ctloutput()
1977 * routines.
1978 */
1979int
1980sooptcopyin(sopt, buf, len, minlen)
1981 struct sockopt *sopt;
1982 void *buf;
1983 size_t len;
1984 size_t minlen;
1985{
1986 size_t valsize;
1987
1988 /*
1989 * If the user gives us more than we wanted, we ignore it, but if we
1990 * don't get the minimum length the caller wants, we return EINVAL.
1991 * On success, sopt->sopt_valsize is set to however much we actually
1992 * retrieved.
--- 26 unchanged lines hidden (view full) ---
2019 sopt.sopt_dir = SOPT_SET;
2020 sopt.sopt_val = optval;
2021 sopt.sopt_valsize = optlen;
2022 sopt.sopt_td = NULL;
2023 return (sosetopt(so, &sopt));
2024}
2025
2026int
2027sosetopt(so, sopt)
2028 struct socket *so;
2029 struct sockopt *sopt;
2030{
2031 int error, optval;
2032 struct linger l;
2033 struct timeval tv;
2034 u_long val;
2035#ifdef MAC
2036 struct mac extmac;
2037#endif
--- 198 unchanged lines hidden (view full) ---
2236 error = copyout(buf, sopt->sopt_val, valsize);
2237 else
2238 bcopy(buf, sopt->sopt_val, valsize);
2239 }
2240 return (error);
2241}
2242
2243int
2244sogetopt(so, sopt)
2245 struct socket *so;
2246 struct sockopt *sopt;
2247{
2248 int error, optval;
2249 struct linger l;
2250 struct timeval tv;
2251#ifdef MAC
2252 struct mac extmac;
2253#endif
2254
--- 247 unchanged lines hidden (view full) ---
2502 return (0);
2503}
2504
2505/*
2506 * sohasoutofband(): protocol notifies socket layer of the arrival of new
2507 * out-of-band data, which will then notify socket consumers.
2508 */
2509void
2510sohasoutofband(so)
2511 struct socket *so;
2512{
2513 if (so->so_sigio != NULL)
2514 pgsigio(&so->so_sigio, SIGURG, 0);
2515 selwakeuppri(&so->so_rcv.sb_sel, PSOCK);
2516}
2517
2518int
2519sopoll(struct socket *so, int events, struct ucred *active_cred,
2520 struct thread *td)
--- 82 unchanged lines hidden (view full) ---
2603
2604/*
2605 * Some routines that return EOPNOTSUPP for entry points that are not
2606 * supported by a protocol. Fill in as needed.
2607 */
2608int
2609pru_accept_notsupp(struct socket *so, struct sockaddr **nam)
2610{
2611 return EOPNOTSUPP;
2612}
2613
2614int
2615pru_attach_notsupp(struct socket *so, int proto, struct thread *td)
2616{
2617 return EOPNOTSUPP;
2618}
2619
2620int
2621pru_bind_notsupp(struct socket *so, struct sockaddr *nam, struct thread *td)
2622{
2623 return EOPNOTSUPP;
2624}
2625
2626int
2627pru_connect_notsupp(struct socket *so, struct sockaddr *nam, struct thread *td)
2628{
2629 return EOPNOTSUPP;
2630}
2631
2632int
2633pru_connect2_notsupp(struct socket *so1, struct socket *so2)
2634{
2635 return EOPNOTSUPP;
2636}
2637
2638int
2639pru_control_notsupp(struct socket *so, u_long cmd, caddr_t data,
2640 struct ifnet *ifp, struct thread *td)
2641{
2642 return EOPNOTSUPP;
2643}
2644
2645int
2646pru_disconnect_notsupp(struct socket *so)
2647{
2648 return EOPNOTSUPP;
2649}
2650
2651int
2652pru_listen_notsupp(struct socket *so, int backlog, struct thread *td)
2653{
2654 return EOPNOTSUPP;
2655}
2656
2657int
2658pru_peeraddr_notsupp(struct socket *so, struct sockaddr **nam)
2659{
2660 return EOPNOTSUPP;
2661}
2662
2663int
2664pru_rcvd_notsupp(struct socket *so, int flags)
2665{
2666 return EOPNOTSUPP;
2667}
2668
2669int
2670pru_rcvoob_notsupp(struct socket *so, struct mbuf *m, int flags)
2671{
2672 return EOPNOTSUPP;
2673}
2674
2675int
2676pru_send_notsupp(struct socket *so, int flags, struct mbuf *m,
2677 struct sockaddr *addr, struct mbuf *control, struct thread *td)
2678{
2679 return EOPNOTSUPP;
2680}
2681
2682/*
2683 * This isn't really a ``null'' operation, but it's the default one and
2684 * doesn't do anything destructive.
2685 */
2686int
2687pru_sense_null(struct socket *so, struct stat *sb)
2688{
2689 sb->st_blksize = so->so_snd.sb_hiwat;
2690 return 0;
2691}
2692
2693int
2694pru_shutdown_notsupp(struct socket *so)
2695{
2696 return EOPNOTSUPP;
2697}
2698
2699int
2700pru_sockaddr_notsupp(struct socket *so, struct sockaddr **nam)
2701{
2702 return EOPNOTSUPP;
2703}
2704
2705int
2706pru_sosend_notsupp(struct socket *so, struct sockaddr *addr, struct uio *uio,
2707 struct mbuf *top, struct mbuf *control, int flags, struct thread *td)
2708{
2709 return EOPNOTSUPP;
2710}
2711
2712int
2713pru_soreceive_notsupp(struct socket *so, struct sockaddr **paddr,
2714 struct uio *uio, struct mbuf **mp0, struct mbuf **controlp,
2715 int *flagsp)
2716{
2717 return EOPNOTSUPP;
2718}
2719
2720int
2721pru_sopoll_notsupp(struct socket *so, int events, struct ucred *cred,
2722 struct thread *td)
2723{
2724 return EOPNOTSUPP;
2725}
2726
2727static void
2728filt_sordetach(struct knote *kn)
2729{
2730 struct socket *so = kn->kn_fp->f_data;
2731
--- 129 unchanged lines hidden (view full) ---
2861 *
2862 * If a socket is closed with sockets on either so_incomp or so_comp, these
2863 * sockets are dropped.
2864 *
2865 * If higher-level protocols are implemented in the kernel, the wakeups done
2866 * here will sometimes cause software-interrupt process scheduling.
2867 */
2868void
2869soisconnecting(so)
2870 register struct socket *so;
2871{
2872
2873 SOCK_LOCK(so);
2874 so->so_state &= ~(SS_ISCONNECTED|SS_ISDISCONNECTING);
2875 so->so_state |= SS_ISCONNECTING;
2876 SOCK_UNLOCK(so);
2877}
2878
2879void
2880soisconnected(so)
2881 struct socket *so;
2882{
2883 struct socket *head;
2884
2885 ACCEPT_LOCK();
2886 SOCK_LOCK(so);
2887 so->so_state &= ~(SS_ISCONNECTING|SS_ISDISCONNECTING|SS_ISCONFIRMING);
2888 so->so_state |= SS_ISCONNECTED;
2889 head = so->so_head;
--- 24 unchanged lines hidden (view full) ---
2914 SOCK_UNLOCK(so);
2915 ACCEPT_UNLOCK();
2916 wakeup(&so->so_timeo);
2917 sorwakeup(so);
2918 sowwakeup(so);
2919}
2920
2921void
2922soisdisconnecting(so)
2923 register struct socket *so;
2924{
2925
2926 /*
2927 * Note: This code assumes that SOCK_LOCK(so) and
2928 * SOCKBUF_LOCK(&so->so_rcv) are the same.
2929 */
2930 SOCKBUF_LOCK(&so->so_rcv);
2931 so->so_state &= ~SS_ISCONNECTING;
2932 so->so_state |= SS_ISDISCONNECTING;
2933 so->so_rcv.sb_state |= SBS_CANTRCVMORE;
2934 sorwakeup_locked(so);
2935 SOCKBUF_LOCK(&so->so_snd);
2936 so->so_snd.sb_state |= SBS_CANTSENDMORE;
2937 sowwakeup_locked(so);
2938 wakeup(&so->so_timeo);
2939}
2940
2941void
2942soisdisconnected(so)
2943 register struct socket *so;
2944{
2945
2946 /*
2947 * Note: This code assumes that SOCK_LOCK(so) and
2948 * SOCKBUF_LOCK(&so->so_rcv) are the same.
2949 */
2950 SOCKBUF_LOCK(&so->so_rcv);
2951 so->so_state &= ~(SS_ISCONNECTING|SS_ISCONNECTED|SS_ISDISCONNECTING);
--- 27 unchanged lines hidden (view full) ---
2979 * reduce the spew of irrelevant information over this interface, to isolate
2980 * user code from changes in the kernel structure, and potentially to provide
2981 * information-hiding if we decide that some of this information should be
2982 * hidden from users.
2983 */
2984void
2985sotoxsocket(struct socket *so, struct xsocket *xso)
2986{
2987 xso->xso_len = sizeof *xso;
2988 xso->xso_so = so;
2989 xso->so_type = so->so_type;
2990 xso->so_options = so->so_options;
2991 xso->so_linger = so->so_linger;
2992 xso->so_state = so->so_state;
2993 xso->so_pcb = so->so_pcb;
2994 xso->xso_protocol = so->so_proto->pr_protocol;
--- 12 unchanged lines hidden ---
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 169375 2007-05-08 12:34:14Z yongari $");
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(dom, aso, type, proto, cred, td)
331 int dom;
332 struct socket **aso;
333 int type;
334 int proto;
335 struct ucred *cred;
336 struct thread *td;
337{
338 struct protosw *prp;
339 struct socket *so;
340 int error;
341
342 if (proto)
343 prp = pffindproto(dom, proto, type);
344 else
--- 56 unchanged lines hidden (view full) ---
401 * connections, sonewconn is called. If the connection is possible (subject
402 * to space constraints, etc.) then we allocate a new structure, propoerly
403 * linked into the data structure of the original socket, and return this.
404 * Connstatus may be 0, or SO_ISCONFIRMING, or SO_ISCONNECTED.
405 *
406 * Note: the ref count on the socket is 0 on return.
407 */
408struct socket *
409sonewconn(head, connstatus)
410 register struct socket *head;
411 int connstatus;
412{
413 register struct socket *so;
414 int over;
415
416 ACCEPT_LOCK();
417 over = (head->so_qlen > 3 * head->so_qlimit / 2);
418 ACCEPT_UNLOCK();
419#ifdef REGRESSION
420 if (regression_sonewconn_earlytest && over)
421#else
--- 65 unchanged lines hidden (view full) ---
487 if (connstatus) {
488 sorwakeup(head);
489 wakeup_one(&head->so_timeo);
490 }
491 return (so);
492}
493
494int
495sobind(so, nam, td)
496 struct socket *so;
497 struct sockaddr *nam;
498 struct thread *td;
499{
500
501 return ((*so->so_proto->pr_usrreqs->pru_bind)(so, nam, td));
502}
503
504/*
505 * solisten() transitions a socket from a non-listening state to a listening
506 * state, but can also be used to update the listen queue depth on an
507 * existing listen socket. The protocol will call back into the sockets
508 * layer using solisten_proto_check() and solisten_proto() to check and set
509 * socket-layer listen state. Call backs are used so that the protocol can
510 * acquire both protocol and socket layer locks in whatever order is required
511 * by the protocol.
512 *
513 * Protocol implementors are advised to hold the socket lock across the
514 * socket-layer test and set to avoid races at the socket layer.
515 */
516int
517solisten(so, backlog, td)
518 struct socket *so;
519 int backlog;
520 struct thread *td;
521{
522
523 return ((*so->so_proto->pr_usrreqs->pru_listen)(so, backlog, td));
524}
525
526int
527solisten_proto_check(so)
528 struct socket *so;
529{
530
531 SOCK_LOCK_ASSERT(so);
532
533 if (so->so_state & (SS_ISCONNECTED | SS_ISCONNECTING |
534 SS_ISDISCONNECTING))
535 return (EINVAL);
536 return (0);
537}
538
539void
540solisten_proto(so, backlog)
541 struct socket *so;
542 int backlog;
543{
544
545 SOCK_LOCK_ASSERT(so);
546
547 if (backlog < 0 || backlog > somaxconn)
548 backlog = somaxconn;
549 so->so_qlimit = backlog;
550 so->so_options |= SO_ACCEPTCONN;
--- 15 unchanged lines hidden (view full) ---
566 * - The socket is not in a completed connection queue, so a process has been
567 * notified that it is present. If it is removed, the user process may
568 * block in accept() despite select() saying the socket was ready.
569 *
570 * Otherwise, it will quietly abort so that a future call to sofree(), when
571 * conditions are right, can succeed.
572 */
573void
574sofree(so)
575 struct socket *so;
576{
577 struct protosw *pr = so->so_proto;
578 struct socket *head;
579
580 ACCEPT_LOCK_ASSERT();
581 SOCK_LOCK_ASSERT(so);
582
583 if ((so->so_state & SS_NOFDREF) == 0 || so->so_count != 0 ||
--- 58 unchanged lines hidden (view full) ---
642 * Close a socket on last file table reference removal. Initiate disconnect
643 * if connected. Free socket when disconnect complete.
644 *
645 * This function will sorele() the socket. Note that soclose() may be called
646 * prior to the ref count reaching zero. The actual socket structure will
647 * not be freed until the ref count reaches zero.
648 */
649int
650soclose(so)
651 struct socket *so;
652{
653 int error = 0;
654
655 KASSERT(!(so->so_state & SS_NOFDREF), ("soclose: SS_NOFDREF on enter"));
656
657 funsetown(&so->so_sigio);
658 if (so->so_state & SS_ISCONNECTED) {
659 if ((so->so_state & SS_ISDISCONNECTING) == 0) {
--- 58 unchanged lines hidden (view full) ---
718 * from the listen queue it was on, or races with other threads are risked.
719 *
720 * This interface will call into the protocol code, so must not be called
721 * with any socket locks held. Protocols do call it while holding their own
722 * recursible protocol mutexes, but this is something that should be subject
723 * to review in the future.
724 */
725void
726soabort(so)
727 struct socket *so;
728{
729
730 /*
731 * In as much as is possible, assert that no references to this
732 * socket are held. This is not quite the same as asserting that the
733 * current thread is responsible for arranging for no references, but
734 * is as close as we can get for now.
735 */
--- 6 unchanged lines hidden (view full) ---
742 if (so->so_proto->pr_usrreqs->pru_abort != NULL)
743 (*so->so_proto->pr_usrreqs->pru_abort)(so);
744 ACCEPT_LOCK();
745 SOCK_LOCK(so);
746 sofree(so);
747}
748
749int
750soaccept(so, nam)
751 struct socket *so;
752 struct sockaddr **nam;
753{
754 int error;
755
756 SOCK_LOCK(so);
757 KASSERT((so->so_state & SS_NOFDREF) != 0, ("soaccept: !NOFDREF"));
758 so->so_state &= ~SS_NOFDREF;
759 SOCK_UNLOCK(so);
760 error = (*so->so_proto->pr_usrreqs->pru_accept)(so, nam);
761 return (error);
762}
763
764int
765soconnect(so, nam, td)
766 struct socket *so;
767 struct sockaddr *nam;
768 struct thread *td;
769{
770 int error;
771
772 if (so->so_options & SO_ACCEPTCONN)
773 return (EOPNOTSUPP);
774 /*
775 * If protocol is connection-based, can only connect once.
776 * Otherwise, if connected, try to disconnect first. This allows
--- 11 unchanged lines hidden (view full) ---
788 so->so_error = 0;
789 error = (*so->so_proto->pr_usrreqs->pru_connect)(so, nam, td);
790 }
791
792 return (error);
793}
794
795int
796soconnect2(so1, so2)
797 struct socket *so1;
798 struct socket *so2;
799{
800
801 return ((*so1->so_proto->pr_usrreqs->pru_connect2)(so1, so2));
802}
803
804int
805sodisconnect(so)
806 struct socket *so;
807{
808 int error;
809
810 if ((so->so_state & SS_ISCONNECTED) == 0)
811 return (ENOTCONN);
812 if (so->so_state & SS_ISDISCONNECTING)
813 return (EALREADY);
814 error = (*so->so_proto->pr_usrreqs->pru_disconnect)(so);
--- 126 unchanged lines hidden (view full) ---
941 *retmp = top;
942 return (error);
943}
944#endif /*ZERO_COPY_SOCKETS*/
945
946#define SBLOCKWAIT(f) (((f) & MSG_DONTWAIT) ? M_NOWAIT : M_WAITOK)
947
948int
949sosend_dgram(so, addr, uio, top, control, flags, td)
950 struct socket *so;
951 struct sockaddr *addr;
952 struct uio *uio;
953 struct mbuf *top;
954 struct mbuf *control;
955 int flags;
956 struct thread *td;
957{
958 long space, resid;
959 int clen = 0, error, dontroute;
960#ifdef ZERO_COPY_SOCKETS
961 int atomic = sosendallatonce(so) || top;
962#endif
963
964 KASSERT(so->so_type == SOCK_DGRAM, ("sodgram_send: !SOCK_DGRAM"));
--- 156 unchanged lines hidden (view full) ---
1121 * by the mbuf chain "top" (which must be null if uio is not). Data provided
1122 * in mbuf chain must be small enough to send all at once.
1123 *
1124 * Returns nonzero on error, timeout or signal; callers must check for short
1125 * counts if EINTR/ERESTART are returned. Data and control buffers are freed
1126 * on return.
1127 */
1128int
1129sosend_generic(so, addr, uio, top, control, flags, td)
1130 struct socket *so;
1131 struct sockaddr *addr;
1132 struct uio *uio;
1133 struct mbuf *top;
1134 struct mbuf *control;
1135 int flags;
1136 struct thread *td;
1137{
1138 long space, resid;
1139 int clen = 0, error, dontroute;
1140 int atomic = sosendallatonce(so) || top;
1141
1142 if (uio != NULL)
1143 resid = uio->uio_resid;
1144 else
--- 164 unchanged lines hidden (view full) ---
1309 if (top != NULL)
1310 m_freem(top);
1311 if (control != NULL)
1312 m_freem(control);
1313 return (error);
1314}
1315
1316int
1317sosend(so, addr, uio, top, control, flags, td)
1318 struct socket *so;
1319 struct sockaddr *addr;
1320 struct uio *uio;
1321 struct mbuf *top;
1322 struct mbuf *control;
1323 int flags;
1324 struct thread *td;
1325{
1326
1327 /* XXXRW: Temporary debugging. */
1328 KASSERT(so->so_proto->pr_usrreqs->pru_sosend != sosend,
1329 ("sosend: protocol calls sosend"));
1330
1331 return (so->so_proto->pr_usrreqs->pru_sosend(so, addr, uio, top,
1332 control, flags, td));
1333}
1334
1335/*
1336 * The part of soreceive() that implements reading non-inline out-of-band
1337 * data from a socket. For more complete comments, see soreceive(), from
1338 * which this code originated.
1339 *
1340 * Note that soreceive_rcvoob(), unlike the remainder of soreceive(), is
1341 * unable to return an mbuf chain to the caller.
1342 */
1343static int
1344soreceive_rcvoob(so, uio, flags)
1345 struct socket *so;
1346 struct uio *uio;
1347 int flags;
1348{
1349 struct protosw *pr = so->so_proto;
1350 struct mbuf *m;
1351 int error;
1352
1353 KASSERT(flags & MSG_OOB, ("soreceive_rcvoob: (flags & MSG_OOB) == 0"));
1354
1355 m = m_get(M_TRYWAIT, MT_DATA);
--- 76 unchanged lines hidden (view full) ---
1432 * appended, and thus we must maintain consistency of the sockbuf during that
1433 * time.
1434 *
1435 * The caller may receive the data as a single mbuf chain by supplying an
1436 * mbuf **mp0 for use in returning the chain. The uio is then used only for
1437 * the count in uio_resid.
1438 */
1439int
1440soreceive_generic(so, psa, uio, mp0, controlp, flagsp)
1441 struct socket *so;
1442 struct sockaddr **psa;
1443 struct uio *uio;
1444 struct mbuf **mp0;
1445 struct mbuf **controlp;
1446 int *flagsp;
1447{
1448 struct mbuf *m, **mp;
1449 int flags, len, error, offset;
1450 struct protosw *pr = so->so_proto;
1451 struct mbuf *nextrecord;
1452 int moff, type = 0;
1453 int orig_resid = uio->uio_resid;
1454
--- 435 unchanged lines hidden (view full) ---
1890 if (flagsp != NULL)
1891 *flagsp |= flags;
1892release:
1893 sbunlock(&so->so_rcv);
1894 return (error);
1895}
1896
1897int
1898soreceive(so, psa, uio, mp0, controlp, flagsp)
1899 struct socket *so;
1900 struct sockaddr **psa;
1901 struct uio *uio;
1902 struct mbuf **mp0;
1903 struct mbuf **controlp;
1904 int *flagsp;
1905{
1906
1907 /* XXXRW: Temporary debugging. */
1908 KASSERT(so->so_proto->pr_usrreqs->pru_soreceive != soreceive,
1909 ("soreceive: protocol calls soreceive"));
1910
1911 return (so->so_proto->pr_usrreqs->pru_soreceive(so, psa, uio, mp0,
1912 controlp, flagsp));
1913}
1914
1915int
1916soshutdown(so, how)
1917 struct socket *so;
1918 int how;
1919{
1920 struct protosw *pr = so->so_proto;
1921
1922 if (!(how == SHUT_RD || how == SHUT_WR || how == SHUT_RDWR))
1923 return (EINVAL);
1924
1925 if (how != SHUT_WR)
1926 sorflush(so);
1927 if (how != SHUT_RD)
1928 return ((*pr->pr_usrreqs->pru_shutdown)(so));
1929 return (0);
1930}
1931
1932void
1933sorflush(so)
1934 struct socket *so;
1935{
1936 struct sockbuf *sb = &so->so_rcv;
1937 struct protosw *pr = so->so_proto;
1938 struct sockbuf asb;
1939
1940 /*
1941 * XXXRW: This is quite ugly. Previously, this code made a copy of
1942 * the socket buffer, then zero'd the original to clear the buffer
--- 29 unchanged lines hidden (view full) ---
1972/*
1973 * Perhaps this routine, and sooptcopyout(), below, ought to come in an
1974 * additional variant to handle the case where the option value needs to be
1975 * some kind of integer, but not a specific size. In addition to their use
1976 * here, these functions are also called by the protocol-level pr_ctloutput()
1977 * routines.
1978 */
1979int
1980sooptcopyin(sopt, buf, len, minlen)
1981 struct sockopt *sopt;
1982 void *buf;
1983 size_t len;
1984 size_t minlen;
1985{
1986 size_t valsize;
1987
1988 /*
1989 * If the user gives us more than we wanted, we ignore it, but if we
1990 * don't get the minimum length the caller wants, we return EINVAL.
1991 * On success, sopt->sopt_valsize is set to however much we actually
1992 * retrieved.
--- 26 unchanged lines hidden (view full) ---
2019 sopt.sopt_dir = SOPT_SET;
2020 sopt.sopt_val = optval;
2021 sopt.sopt_valsize = optlen;
2022 sopt.sopt_td = NULL;
2023 return (sosetopt(so, &sopt));
2024}
2025
2026int
2027sosetopt(so, sopt)
2028 struct socket *so;
2029 struct sockopt *sopt;
2030{
2031 int error, optval;
2032 struct linger l;
2033 struct timeval tv;
2034 u_long val;
2035#ifdef MAC
2036 struct mac extmac;
2037#endif
--- 198 unchanged lines hidden (view full) ---
2236 error = copyout(buf, sopt->sopt_val, valsize);
2237 else
2238 bcopy(buf, sopt->sopt_val, valsize);
2239 }
2240 return (error);
2241}
2242
2243int
2244sogetopt(so, sopt)
2245 struct socket *so;
2246 struct sockopt *sopt;
2247{
2248 int error, optval;
2249 struct linger l;
2250 struct timeval tv;
2251#ifdef MAC
2252 struct mac extmac;
2253#endif
2254
--- 247 unchanged lines hidden (view full) ---
2502 return (0);
2503}
2504
2505/*
2506 * sohasoutofband(): protocol notifies socket layer of the arrival of new
2507 * out-of-band data, which will then notify socket consumers.
2508 */
2509void
2510sohasoutofband(so)
2511 struct socket *so;
2512{
2513 if (so->so_sigio != NULL)
2514 pgsigio(&so->so_sigio, SIGURG, 0);
2515 selwakeuppri(&so->so_rcv.sb_sel, PSOCK);
2516}
2517
2518int
2519sopoll(struct socket *so, int events, struct ucred *active_cred,
2520 struct thread *td)
--- 82 unchanged lines hidden (view full) ---
2603
2604/*
2605 * Some routines that return EOPNOTSUPP for entry points that are not
2606 * supported by a protocol. Fill in as needed.
2607 */
2608int
2609pru_accept_notsupp(struct socket *so, struct sockaddr **nam)
2610{
2611 return EOPNOTSUPP;
2612}
2613
2614int
2615pru_attach_notsupp(struct socket *so, int proto, struct thread *td)
2616{
2617 return EOPNOTSUPP;
2618}
2619
2620int
2621pru_bind_notsupp(struct socket *so, struct sockaddr *nam, struct thread *td)
2622{
2623 return EOPNOTSUPP;
2624}
2625
2626int
2627pru_connect_notsupp(struct socket *so, struct sockaddr *nam, struct thread *td)
2628{
2629 return EOPNOTSUPP;
2630}
2631
2632int
2633pru_connect2_notsupp(struct socket *so1, struct socket *so2)
2634{
2635 return EOPNOTSUPP;
2636}
2637
2638int
2639pru_control_notsupp(struct socket *so, u_long cmd, caddr_t data,
2640 struct ifnet *ifp, struct thread *td)
2641{
2642 return EOPNOTSUPP;
2643}
2644
2645int
2646pru_disconnect_notsupp(struct socket *so)
2647{
2648 return EOPNOTSUPP;
2649}
2650
2651int
2652pru_listen_notsupp(struct socket *so, int backlog, struct thread *td)
2653{
2654 return EOPNOTSUPP;
2655}
2656
2657int
2658pru_peeraddr_notsupp(struct socket *so, struct sockaddr **nam)
2659{
2660 return EOPNOTSUPP;
2661}
2662
2663int
2664pru_rcvd_notsupp(struct socket *so, int flags)
2665{
2666 return EOPNOTSUPP;
2667}
2668
2669int
2670pru_rcvoob_notsupp(struct socket *so, struct mbuf *m, int flags)
2671{
2672 return EOPNOTSUPP;
2673}
2674
2675int
2676pru_send_notsupp(struct socket *so, int flags, struct mbuf *m,
2677 struct sockaddr *addr, struct mbuf *control, struct thread *td)
2678{
2679 return EOPNOTSUPP;
2680}
2681
2682/*
2683 * This isn't really a ``null'' operation, but it's the default one and
2684 * doesn't do anything destructive.
2685 */
2686int
2687pru_sense_null(struct socket *so, struct stat *sb)
2688{
2689 sb->st_blksize = so->so_snd.sb_hiwat;
2690 return 0;
2691}
2692
2693int
2694pru_shutdown_notsupp(struct socket *so)
2695{
2696 return EOPNOTSUPP;
2697}
2698
2699int
2700pru_sockaddr_notsupp(struct socket *so, struct sockaddr **nam)
2701{
2702 return EOPNOTSUPP;
2703}
2704
2705int
2706pru_sosend_notsupp(struct socket *so, struct sockaddr *addr, struct uio *uio,
2707 struct mbuf *top, struct mbuf *control, int flags, struct thread *td)
2708{
2709 return EOPNOTSUPP;
2710}
2711
2712int
2713pru_soreceive_notsupp(struct socket *so, struct sockaddr **paddr,
2714 struct uio *uio, struct mbuf **mp0, struct mbuf **controlp,
2715 int *flagsp)
2716{
2717 return EOPNOTSUPP;
2718}
2719
2720int
2721pru_sopoll_notsupp(struct socket *so, int events, struct ucred *cred,
2722 struct thread *td)
2723{
2724 return EOPNOTSUPP;
2725}
2726
2727static void
2728filt_sordetach(struct knote *kn)
2729{
2730 struct socket *so = kn->kn_fp->f_data;
2731
--- 129 unchanged lines hidden (view full) ---
2861 *
2862 * If a socket is closed with sockets on either so_incomp or so_comp, these
2863 * sockets are dropped.
2864 *
2865 * If higher-level protocols are implemented in the kernel, the wakeups done
2866 * here will sometimes cause software-interrupt process scheduling.
2867 */
2868void
2869soisconnecting(so)
2870 register struct socket *so;
2871{
2872
2873 SOCK_LOCK(so);
2874 so->so_state &= ~(SS_ISCONNECTED|SS_ISDISCONNECTING);
2875 so->so_state |= SS_ISCONNECTING;
2876 SOCK_UNLOCK(so);
2877}
2878
2879void
2880soisconnected(so)
2881 struct socket *so;
2882{
2883 struct socket *head;
2884
2885 ACCEPT_LOCK();
2886 SOCK_LOCK(so);
2887 so->so_state &= ~(SS_ISCONNECTING|SS_ISDISCONNECTING|SS_ISCONFIRMING);
2888 so->so_state |= SS_ISCONNECTED;
2889 head = so->so_head;
--- 24 unchanged lines hidden (view full) ---
2914 SOCK_UNLOCK(so);
2915 ACCEPT_UNLOCK();
2916 wakeup(&so->so_timeo);
2917 sorwakeup(so);
2918 sowwakeup(so);
2919}
2920
2921void
2922soisdisconnecting(so)
2923 register struct socket *so;
2924{
2925
2926 /*
2927 * Note: This code assumes that SOCK_LOCK(so) and
2928 * SOCKBUF_LOCK(&so->so_rcv) are the same.
2929 */
2930 SOCKBUF_LOCK(&so->so_rcv);
2931 so->so_state &= ~SS_ISCONNECTING;
2932 so->so_state |= SS_ISDISCONNECTING;
2933 so->so_rcv.sb_state |= SBS_CANTRCVMORE;
2934 sorwakeup_locked(so);
2935 SOCKBUF_LOCK(&so->so_snd);
2936 so->so_snd.sb_state |= SBS_CANTSENDMORE;
2937 sowwakeup_locked(so);
2938 wakeup(&so->so_timeo);
2939}
2940
2941void
2942soisdisconnected(so)
2943 register struct socket *so;
2944{
2945
2946 /*
2947 * Note: This code assumes that SOCK_LOCK(so) and
2948 * SOCKBUF_LOCK(&so->so_rcv) are the same.
2949 */
2950 SOCKBUF_LOCK(&so->so_rcv);
2951 so->so_state &= ~(SS_ISCONNECTING|SS_ISCONNECTED|SS_ISDISCONNECTING);
--- 27 unchanged lines hidden (view full) ---
2979 * reduce the spew of irrelevant information over this interface, to isolate
2980 * user code from changes in the kernel structure, and potentially to provide
2981 * information-hiding if we decide that some of this information should be
2982 * hidden from users.
2983 */
2984void
2985sotoxsocket(struct socket *so, struct xsocket *xso)
2986{
2987 xso->xso_len = sizeof *xso;
2988 xso->xso_so = so;
2989 xso->so_type = so->so_type;
2990 xso->so_options = so->so_options;
2991 xso->so_linger = so->so_linger;
2992 xso->so_state = so->so_state;
2993 xso->so_pcb = so->so_pcb;
2994 xso->xso_protocol = so->so_proto->pr_protocol;
--- 12 unchanged lines hidden ---