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-2006 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 167895 2007-03-26 08:59:03Z 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> --- 11 unchanged lines hidden (view full) --- 118#include <sys/eventhandler.h> 119#include <sys/poll.h> 120#include <sys/proc.h> 121#include <sys/protosw.h> 122#include <sys/socket.h> 123#include <sys/socketvar.h> 124#include <sys/resourcevar.h> 125#include <sys/signalvar.h> |
126#include <sys/stat.h> |
127#include <sys/sysctl.h> 128#include <sys/uio.h> 129#include <sys/jail.h> 130 131#include <security/mac/mac_framework.h> 132 133#include <vm/uma.h> 134 --- 2454 unchanged lines hidden (view full) --- 2589 2590 SOCKBUF_LOCK(sb); 2591 knlist_add(&sb->sb_sel.si_note, kn, 1); 2592 sb->sb_flags |= SB_KNOTE; 2593 SOCKBUF_UNLOCK(sb); 2594 return (0); 2595} 2596 |
2597/* 2598 * Some routines that return EOPNOTSUPP for entry points that are not 2599 * supported by a protocol. Fill in as needed. 2600 */ 2601int 2602pru_accept_notsupp(struct socket *so, struct sockaddr **nam) 2603{ 2604 return EOPNOTSUPP; 2605} 2606 2607int 2608pru_attach_notsupp(struct socket *so, int proto, struct thread *td) 2609{ 2610 return EOPNOTSUPP; 2611} 2612 2613int 2614pru_bind_notsupp(struct socket *so, struct sockaddr *nam, struct thread *td) 2615{ 2616 return EOPNOTSUPP; 2617} 2618 2619int 2620pru_connect_notsupp(struct socket *so, struct sockaddr *nam, struct thread *td) 2621{ 2622 return EOPNOTSUPP; 2623} 2624 2625int 2626pru_connect2_notsupp(struct socket *so1, struct socket *so2) 2627{ 2628 return EOPNOTSUPP; 2629} 2630 2631int 2632pru_control_notsupp(struct socket *so, u_long cmd, caddr_t data, 2633 struct ifnet *ifp, struct thread *td) 2634{ 2635 return EOPNOTSUPP; 2636} 2637 2638int 2639pru_disconnect_notsupp(struct socket *so) 2640{ 2641 return EOPNOTSUPP; 2642} 2643 2644int 2645pru_listen_notsupp(struct socket *so, int backlog, struct thread *td) 2646{ 2647 return EOPNOTSUPP; 2648} 2649 2650int 2651pru_peeraddr_notsupp(struct socket *so, struct sockaddr **nam) 2652{ 2653 return EOPNOTSUPP; 2654} 2655 2656int 2657pru_rcvd_notsupp(struct socket *so, int flags) 2658{ 2659 return EOPNOTSUPP; 2660} 2661 2662int 2663pru_rcvoob_notsupp(struct socket *so, struct mbuf *m, int flags) 2664{ 2665 return EOPNOTSUPP; 2666} 2667 2668int 2669pru_send_notsupp(struct socket *so, int flags, struct mbuf *m, 2670 struct sockaddr *addr, struct mbuf *control, struct thread *td) 2671{ 2672 return EOPNOTSUPP; 2673} 2674 2675/* 2676 * This isn't really a ``null'' operation, but it's the default one 2677 * and doesn't do anything destructive. 2678 */ 2679int 2680pru_sense_null(struct socket *so, struct stat *sb) 2681{ 2682 sb->st_blksize = so->so_snd.sb_hiwat; 2683 return 0; 2684} 2685 2686int 2687pru_shutdown_notsupp(struct socket *so) 2688{ 2689 return EOPNOTSUPP; 2690} 2691 2692int 2693pru_sockaddr_notsupp(struct socket *so, struct sockaddr **nam) 2694{ 2695 return EOPNOTSUPP; 2696} 2697 2698int 2699pru_sosend_notsupp(struct socket *so, struct sockaddr *addr, struct uio *uio, 2700 struct mbuf *top, struct mbuf *control, int flags, struct thread *td) 2701{ 2702 return EOPNOTSUPP; 2703} 2704 2705int 2706pru_soreceive_notsupp(struct socket *so, struct sockaddr **paddr, 2707 struct uio *uio, struct mbuf **mp0, struct mbuf **controlp, 2708 int *flagsp) 2709{ 2710 return EOPNOTSUPP; 2711} 2712 2713int 2714pru_sopoll_notsupp(struct socket *so, int events, struct ucred *cred, 2715 struct thread *td) 2716{ 2717 return EOPNOTSUPP; 2718} 2719 |
2720static void 2721filt_sordetach(struct knote *kn) 2722{ 2723 struct socket *so = kn->kn_fp->f_data; 2724 2725 SOCKBUF_LOCK(&so->so_rcv); 2726 knlist_remove(&so->so_rcv.sb_sel.si_note, kn, 1); 2727 if (knlist_empty(&so->so_rcv.sb_sel.si_note)) --- 92 unchanged lines hidden (view full) --- 2820 return (error); 2821 2822 if (val < 1 || val > USHRT_MAX) 2823 return (EINVAL); 2824 2825 somaxconn = val; 2826 return (0); 2827} |
2828 2829/* 2830 * Primitive routines for operating on sockets. 2831 */ 2832 2833/* 2834 * Procedures to manipulate state flags of socket 2835 * and do appropriate wakeups. Normal sequence from the 2836 * active (originating) side is that soisconnecting() is 2837 * called during processing of connect() call, 2838 * resulting in an eventual call to soisconnected() if/when the 2839 * connection is established. When the connection is torn down 2840 * soisdisconnecting() is called during processing of disconnect() call, 2841 * and soisdisconnected() is called when the connection to the peer 2842 * is totally severed. The semantics of these routines are such that 2843 * connectionless protocols can call soisconnected() and soisdisconnected() 2844 * only, bypassing the in-progress calls when setting up a ``connection'' 2845 * takes no time. 2846 * 2847 * From the passive side, a socket is created with 2848 * two queues of sockets: so_incomp for connections in progress 2849 * and so_comp for connections already made and awaiting user acceptance. 2850 * As a protocol is preparing incoming connections, it creates a socket 2851 * structure queued on so_incomp by calling sonewconn(). When the connection 2852 * is established, soisconnected() is called, and transfers the 2853 * socket structure to so_comp, making it available to accept(). 2854 * 2855 * If a socket is closed with sockets on either 2856 * so_incomp or so_comp, these sockets are dropped. 2857 * 2858 * If higher level protocols are implemented in 2859 * the kernel, the wakeups done here will sometimes 2860 * cause software-interrupt process scheduling. 2861 */ 2862 2863void 2864soisconnecting(so) 2865 register struct socket *so; 2866{ 2867 2868 SOCK_LOCK(so); 2869 so->so_state &= ~(SS_ISCONNECTED|SS_ISDISCONNECTING); 2870 so->so_state |= SS_ISCONNECTING; 2871 SOCK_UNLOCK(so); 2872} 2873 2874void 2875soisconnected(so) 2876 struct socket *so; 2877{ 2878 struct socket *head; 2879 2880 ACCEPT_LOCK(); 2881 SOCK_LOCK(so); 2882 so->so_state &= ~(SS_ISCONNECTING|SS_ISDISCONNECTING|SS_ISCONFIRMING); 2883 so->so_state |= SS_ISCONNECTED; 2884 head = so->so_head; 2885 if (head != NULL && (so->so_qstate & SQ_INCOMP)) { 2886 if ((so->so_options & SO_ACCEPTFILTER) == 0) { 2887 SOCK_UNLOCK(so); 2888 TAILQ_REMOVE(&head->so_incomp, so, so_list); 2889 head->so_incqlen--; 2890 so->so_qstate &= ~SQ_INCOMP; 2891 TAILQ_INSERT_TAIL(&head->so_comp, so, so_list); 2892 head->so_qlen++; 2893 so->so_qstate |= SQ_COMP; 2894 ACCEPT_UNLOCK(); 2895 sorwakeup(head); 2896 wakeup_one(&head->so_timeo); 2897 } else { 2898 ACCEPT_UNLOCK(); 2899 so->so_upcall = 2900 head->so_accf->so_accept_filter->accf_callback; 2901 so->so_upcallarg = head->so_accf->so_accept_filter_arg; 2902 so->so_rcv.sb_flags |= SB_UPCALL; 2903 so->so_options &= ~SO_ACCEPTFILTER; 2904 SOCK_UNLOCK(so); 2905 so->so_upcall(so, so->so_upcallarg, M_DONTWAIT); 2906 } 2907 return; 2908 } 2909 SOCK_UNLOCK(so); 2910 ACCEPT_UNLOCK(); 2911 wakeup(&so->so_timeo); 2912 sorwakeup(so); 2913 sowwakeup(so); 2914} 2915 2916void 2917soisdisconnecting(so) 2918 register struct socket *so; 2919{ 2920 2921 /* 2922 * Note: This code assumes that SOCK_LOCK(so) and 2923 * SOCKBUF_LOCK(&so->so_rcv) are the same. 2924 */ 2925 SOCKBUF_LOCK(&so->so_rcv); 2926 so->so_state &= ~SS_ISCONNECTING; 2927 so->so_state |= SS_ISDISCONNECTING; 2928 so->so_rcv.sb_state |= SBS_CANTRCVMORE; 2929 sorwakeup_locked(so); 2930 SOCKBUF_LOCK(&so->so_snd); 2931 so->so_snd.sb_state |= SBS_CANTSENDMORE; 2932 sowwakeup_locked(so); 2933 wakeup(&so->so_timeo); 2934} 2935 2936void 2937soisdisconnected(so) 2938 register struct socket *so; 2939{ 2940 2941 /* 2942 * Note: This code assumes that SOCK_LOCK(so) and 2943 * SOCKBUF_LOCK(&so->so_rcv) are the same. 2944 */ 2945 SOCKBUF_LOCK(&so->so_rcv); 2946 so->so_state &= ~(SS_ISCONNECTING|SS_ISCONNECTED|SS_ISDISCONNECTING); 2947 so->so_state |= SS_ISDISCONNECTED; 2948 so->so_rcv.sb_state |= SBS_CANTRCVMORE; 2949 sorwakeup_locked(so); 2950 SOCKBUF_LOCK(&so->so_snd); 2951 so->so_snd.sb_state |= SBS_CANTSENDMORE; 2952 sbdrop_locked(&so->so_snd, so->so_snd.sb_cc); 2953 sowwakeup_locked(so); 2954 wakeup(&so->so_timeo); 2955} 2956 2957/* 2958 * Make a copy of a sockaddr in a malloced buffer of type M_SONAME. 2959 */ 2960struct sockaddr * 2961sodupsockaddr(const struct sockaddr *sa, int mflags) 2962{ 2963 struct sockaddr *sa2; 2964 2965 sa2 = malloc(sa->sa_len, M_SONAME, mflags); 2966 if (sa2) 2967 bcopy(sa, sa2, sa->sa_len); 2968 return sa2; 2969} 2970 2971/* 2972 * Create an external-format (``xsocket'') structure using the information 2973 * in the kernel-format socket structure pointed to by so. This is done 2974 * to reduce the spew of irrelevant information over this interface, 2975 * to isolate user code from changes in the kernel structure, and 2976 * potentially to provide information-hiding if we decide that 2977 * some of this information should be hidden from users. 2978 */ 2979void 2980sotoxsocket(struct socket *so, struct xsocket *xso) 2981{ 2982 xso->xso_len = sizeof *xso; 2983 xso->xso_so = so; 2984 xso->so_type = so->so_type; 2985 xso->so_options = so->so_options; 2986 xso->so_linger = so->so_linger; 2987 xso->so_state = so->so_state; 2988 xso->so_pcb = so->so_pcb; 2989 xso->xso_protocol = so->so_proto->pr_protocol; 2990 xso->xso_family = so->so_proto->pr_domain->dom_family; 2991 xso->so_qlen = so->so_qlen; 2992 xso->so_incqlen = so->so_incqlen; 2993 xso->so_qlimit = so->so_qlimit; 2994 xso->so_timeo = so->so_timeo; 2995 xso->so_error = so->so_error; 2996 xso->so_pgid = so->so_sigio ? so->so_sigio->sio_pgid : 0; 2997 xso->so_oobmark = so->so_oobmark; 2998 sbtoxsockbuf(&so->so_snd, &xso->so_snd); 2999 sbtoxsockbuf(&so->so_rcv, &xso->so_rcv); 3000 xso->so_uid = so->so_cred->cr_uid; 3001} |