socketvar.h revision 191688
1/*- 2 * Copyright (c) 1982, 1986, 1990, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 4. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * @(#)socketvar.h 8.3 (Berkeley) 2/19/95 30 * 31 * $FreeBSD: head/sys/sys/socketvar.h 191688 2009-04-30 13:36:26Z zec $ 32 */ 33 34#ifndef _SYS_SOCKETVAR_H_ 35#define _SYS_SOCKETVAR_H_ 36 37#include <sys/queue.h> /* for TAILQ macros */ 38#include <sys/selinfo.h> /* for struct selinfo */ 39#include <sys/_lock.h> 40#include <sys/_mutex.h> 41#include <sys/_sx.h> 42#include <sys/sockbuf.h> 43#include <sys/sockstate.h> 44#ifdef _KERNEL 45#include <sys/sockopt.h> 46#endif 47 48struct vnet; 49 50/* 51 * Kernel structure per socket. 52 * Contains send and receive buffer queues, 53 * handle on protocol and pointer to protocol 54 * private data and error information. 55 */ 56typedef u_quad_t so_gen_t; 57 58/*- 59 * Locking key to struct socket: 60 * (a) constant after allocation, no locking required. 61 * (b) locked by SOCK_LOCK(so). 62 * (c) locked by SOCKBUF_LOCK(&so->so_rcv). 63 * (d) locked by SOCKBUF_LOCK(&so->so_snd). 64 * (e) locked by ACCEPT_LOCK(). 65 * (f) not locked since integer reads/writes are atomic. 66 * (g) used only as a sleep/wakeup address, no value. 67 * (h) locked by global mutex so_global_mtx. 68 */ 69struct socket { 70 int so_count; /* (b) reference count */ 71 short so_type; /* (a) generic type, see socket.h */ 72 short so_options; /* from socket call, see socket.h */ 73 short so_linger; /* time to linger while closing */ 74 short so_state; /* (b) internal state flags SS_* */ 75 int so_qstate; /* (e) internal state flags SQ_* */ 76 void *so_pcb; /* protocol control block */ 77 struct vnet *so_vnet; /* network stack instance */ 78 struct protosw *so_proto; /* (a) protocol handle */ 79/* 80 * Variables for connection queuing. 81 * Socket where accepts occur is so_head in all subsidiary sockets. 82 * If so_head is 0, socket is not related to an accept. 83 * For head socket so_incomp queues partially completed connections, 84 * while so_comp is a queue of connections ready to be accepted. 85 * If a connection is aborted and it has so_head set, then 86 * it has to be pulled out of either so_incomp or so_comp. 87 * We allow connections to queue up based on current queue lengths 88 * and limit on number of queued connections for this socket. 89 */ 90 struct socket *so_head; /* (e) back pointer to listen socket */ 91 TAILQ_HEAD(, socket) so_incomp; /* (e) queue of partial unaccepted connections */ 92 TAILQ_HEAD(, socket) so_comp; /* (e) queue of complete unaccepted connections */ 93 TAILQ_ENTRY(socket) so_list; /* (e) list of unaccepted connections */ 94 u_short so_qlen; /* (e) number of unaccepted connections */ 95 u_short so_incqlen; /* (e) number of unaccepted incomplete 96 connections */ 97 u_short so_qlimit; /* (e) max number queued connections */ 98 short so_timeo; /* (g) connection timeout */ 99 u_short so_error; /* (f) error affecting connection */ 100 struct sigio *so_sigio; /* [sg] information for async I/O or 101 out of band data (SIGURG) */ 102 u_long so_oobmark; /* (c) chars to oob mark */ 103 TAILQ_HEAD(, aiocblist) so_aiojobq; /* AIO ops waiting on socket */ 104 105 struct sockbuf so_rcv, so_snd; 106 107 void (*so_upcall)(struct socket *, void *, int); 108 void *so_upcallarg; 109 struct ucred *so_cred; /* (a) user credentials */ 110 struct label *so_label; /* (b) MAC label for socket */ 111 struct label *so_peerlabel; /* (b) cached MAC label for peer */ 112 /* NB: generation count must not be first. */ 113 so_gen_t so_gencnt; /* (h) generation count */ 114 void *so_emuldata; /* (b) private data for emulators */ 115 struct so_accf { 116 struct accept_filter *so_accept_filter; 117 void *so_accept_filter_arg; /* saved filter args */ 118 char *so_accept_filter_str; /* saved user args */ 119 } *so_accf; 120 int so_fibnum; /* routing domain for this socket */ 121}; 122 123/* 124 * Global accept mutex to serialize access to accept queues and 125 * fields associated with multiple sockets. This allows us to 126 * avoid defining a lock order between listen and accept sockets 127 * until such time as it proves to be a good idea. 128 */ 129extern struct mtx accept_mtx; 130#define ACCEPT_LOCK_ASSERT() mtx_assert(&accept_mtx, MA_OWNED) 131#define ACCEPT_UNLOCK_ASSERT() mtx_assert(&accept_mtx, MA_NOTOWNED) 132#define ACCEPT_LOCK() mtx_lock(&accept_mtx) 133#define ACCEPT_UNLOCK() mtx_unlock(&accept_mtx) 134 135/* 136 * Per-socket mutex: we reuse the receive socket buffer mutex for space 137 * efficiency. This decision should probably be revisited as we optimize 138 * locking for the socket code. 139 */ 140#define SOCK_MTX(_so) SOCKBUF_MTX(&(_so)->so_rcv) 141#define SOCK_LOCK(_so) SOCKBUF_LOCK(&(_so)->so_rcv) 142#define SOCK_OWNED(_so) SOCKBUF_OWNED(&(_so)->so_rcv) 143#define SOCK_UNLOCK(_so) SOCKBUF_UNLOCK(&(_so)->so_rcv) 144#define SOCK_LOCK_ASSERT(_so) SOCKBUF_LOCK_ASSERT(&(_so)->so_rcv) 145 146/* 147 * Socket state bits stored in so_qstate. 148 */ 149#define SQ_INCOMP 0x0800 /* unaccepted, incomplete connection */ 150#define SQ_COMP 0x1000 /* unaccepted, complete connection */ 151 152/* 153 * Externalized form of struct socket used by the sysctl(3) interface. 154 */ 155struct xsocket { 156 size_t xso_len; /* length of this structure */ 157 struct socket *xso_so; /* makes a convenient handle sometimes */ 158 short so_type; 159 short so_options; 160 short so_linger; 161 short so_state; 162 caddr_t so_pcb; /* another convenient handle */ 163 int xso_protocol; 164 int xso_family; 165 u_short so_qlen; 166 u_short so_incqlen; 167 u_short so_qlimit; 168 short so_timeo; 169 u_short so_error; 170 pid_t so_pgid; 171 u_long so_oobmark; 172 struct xsockbuf so_rcv, so_snd; 173 uid_t so_uid; /* XXX */ 174}; 175 176#ifdef _KERNEL 177 178/* 179 * Macros for sockets and socket buffering. 180 */ 181 182/* 183 * Flags to sblock(). 184 */ 185#define SBL_WAIT 0x00000001 /* Wait if not immediately available. */ 186#define SBL_NOINTR 0x00000002 /* Force non-interruptible sleep. */ 187#define SBL_VALID (SBL_WAIT | SBL_NOINTR) 188 189/* 190 * Do we need to notify the other side when I/O is possible? 191 */ 192#define sb_notify(sb) (((sb)->sb_flags & (SB_WAIT | SB_SEL | SB_ASYNC | \ 193 SB_UPCALL | SB_AIO | SB_KNOTE)) != 0) 194 195/* do we have to send all at once on a socket? */ 196#define sosendallatonce(so) \ 197 ((so)->so_proto->pr_flags & PR_ATOMIC) 198 199/* can we read something from so? */ 200#define soreadable(so) \ 201 ((so)->so_rcv.sb_cc >= (so)->so_rcv.sb_lowat || \ 202 ((so)->so_rcv.sb_state & SBS_CANTRCVMORE) || \ 203 !TAILQ_EMPTY(&(so)->so_comp) || (so)->so_error) 204 205/* can we write something to so? */ 206#define sowriteable(so) \ 207 ((sbspace(&(so)->so_snd) >= (so)->so_snd.sb_lowat && \ 208 (((so)->so_state&SS_ISCONNECTED) || \ 209 ((so)->so_proto->pr_flags&PR_CONNREQUIRED)==0)) || \ 210 ((so)->so_snd.sb_state & SBS_CANTSENDMORE) || \ 211 (so)->so_error) 212 213/* 214 * soref()/sorele() ref-count the socket structure. Note that you must 215 * still explicitly close the socket, but the last ref count will free 216 * the structure. 217 */ 218#define soref(so) do { \ 219 SOCK_LOCK_ASSERT(so); \ 220 ++(so)->so_count; \ 221} while (0) 222 223#define sorele(so) do { \ 224 ACCEPT_LOCK_ASSERT(); \ 225 SOCK_LOCK_ASSERT(so); \ 226 if ((so)->so_count <= 0) \ 227 panic("sorele"); \ 228 if (--(so)->so_count == 0) \ 229 sofree(so); \ 230 else { \ 231 SOCK_UNLOCK(so); \ 232 ACCEPT_UNLOCK(); \ 233 } \ 234} while (0) 235 236#define sotryfree(so) do { \ 237 ACCEPT_LOCK_ASSERT(); \ 238 SOCK_LOCK_ASSERT(so); \ 239 if ((so)->so_count == 0) \ 240 sofree(so); \ 241 else { \ 242 SOCK_UNLOCK(so); \ 243 ACCEPT_UNLOCK(); \ 244 } \ 245} while(0) 246 247/* 248 * In sorwakeup() and sowwakeup(), acquire the socket buffer lock to 249 * avoid a non-atomic test-and-wakeup. However, sowakeup is 250 * responsible for releasing the lock if it is called. We unlock only 251 * if we don't call into sowakeup. If any code is introduced that 252 * directly invokes the underlying sowakeup() primitives, it must 253 * maintain the same semantics. 254 */ 255#define sorwakeup_locked(so) do { \ 256 SOCKBUF_LOCK_ASSERT(&(so)->so_rcv); \ 257 if (sb_notify(&(so)->so_rcv)) \ 258 sowakeup((so), &(so)->so_rcv); \ 259 else \ 260 SOCKBUF_UNLOCK(&(so)->so_rcv); \ 261} while (0) 262 263#define sorwakeup(so) do { \ 264 SOCKBUF_LOCK(&(so)->so_rcv); \ 265 sorwakeup_locked(so); \ 266} while (0) 267 268#define sowwakeup_locked(so) do { \ 269 SOCKBUF_LOCK_ASSERT(&(so)->so_snd); \ 270 if (sb_notify(&(so)->so_snd)) \ 271 sowakeup((so), &(so)->so_snd); \ 272 else \ 273 SOCKBUF_UNLOCK(&(so)->so_snd); \ 274} while (0) 275 276#define sowwakeup(so) do { \ 277 SOCKBUF_LOCK(&(so)->so_snd); \ 278 sowwakeup_locked(so); \ 279} while (0) 280 281struct accept_filter { 282 char accf_name[16]; 283 void (*accf_callback) 284 (struct socket *so, void *arg, int waitflag); 285 void * (*accf_create) 286 (struct socket *so, char *arg); 287 void (*accf_destroy) 288 (struct socket *so); 289 SLIST_ENTRY(accept_filter) accf_next; 290}; 291 292#ifdef MALLOC_DECLARE 293MALLOC_DECLARE(M_ACCF); 294MALLOC_DECLARE(M_PCB); 295MALLOC_DECLARE(M_SONAME); 296#endif 297 298extern int maxsockets; 299extern u_long sb_max; 300extern struct uma_zone *socket_zone; 301extern so_gen_t so_gencnt; 302 303struct mbuf; 304struct sockaddr; 305struct ucred; 306struct uio; 307 308/* 309 * From uipc_socket and friends 310 */ 311int sockargs(struct mbuf **mp, caddr_t buf, int buflen, int type); 312int getsockaddr(struct sockaddr **namp, caddr_t uaddr, size_t len); 313void soabort(struct socket *so); 314int soaccept(struct socket *so, struct sockaddr **nam); 315int socheckuid(struct socket *so, uid_t uid); 316int sobind(struct socket *so, struct sockaddr *nam, struct thread *td); 317int soclose(struct socket *so); 318int soconnect(struct socket *so, struct sockaddr *nam, struct thread *td); 319int soconnect2(struct socket *so1, struct socket *so2); 320int socow_setup(struct mbuf *m0, struct uio *uio); 321int socreate(int dom, struct socket **aso, int type, int proto, 322 struct ucred *cred, struct thread *td); 323int sodisconnect(struct socket *so); 324struct sockaddr *sodupsockaddr(const struct sockaddr *sa, int mflags); 325void sofree(struct socket *so); 326void sohasoutofband(struct socket *so); 327int solisten(struct socket *so, int backlog, struct thread *td); 328void solisten_proto(struct socket *so, int backlog); 329int solisten_proto_check(struct socket *so); 330struct socket * 331 sonewconn(struct socket *head, int connstatus); 332 333 334int sopoll(struct socket *so, int events, struct ucred *active_cred, 335 struct thread *td); 336int sopoll_generic(struct socket *so, int events, 337 struct ucred *active_cred, struct thread *td); 338int soreceive(struct socket *so, struct sockaddr **paddr, struct uio *uio, 339 struct mbuf **mp0, struct mbuf **controlp, int *flagsp); 340int soreceive_dgram(struct socket *so, struct sockaddr **paddr, 341 struct uio *uio, struct mbuf **mp0, struct mbuf **controlp, 342 int *flagsp); 343int soreceive_generic(struct socket *so, struct sockaddr **paddr, 344 struct uio *uio, struct mbuf **mp0, struct mbuf **controlp, 345 int *flagsp); 346int soreserve(struct socket *so, u_long sndcc, u_long rcvcc); 347void sorflush(struct socket *so); 348int sosend(struct socket *so, struct sockaddr *addr, struct uio *uio, 349 struct mbuf *top, struct mbuf *control, int flags, 350 struct thread *td); 351int sosend_dgram(struct socket *so, struct sockaddr *addr, 352 struct uio *uio, struct mbuf *top, struct mbuf *control, 353 int flags, struct thread *td); 354int sosend_generic(struct socket *so, struct sockaddr *addr, 355 struct uio *uio, struct mbuf *top, struct mbuf *control, 356 int flags, struct thread *td); 357int soshutdown(struct socket *so, int how); 358void sotoxsocket(struct socket *so, struct xsocket *xso); 359void sowakeup(struct socket *so, struct sockbuf *sb); 360int selsocket(struct socket *so, int events, struct timeval *tv, 361 struct thread *td); 362 363/* 364 * Accept filter functions (duh). 365 */ 366int accept_filt_add(struct accept_filter *filt); 367int accept_filt_del(char *name); 368struct accept_filter *accept_filt_get(char *name); 369#ifdef ACCEPT_FILTER_MOD 370#ifdef SYSCTL_DECL 371SYSCTL_DECL(_net_inet_accf); 372#endif 373int accept_filt_generic_mod_event(module_t mod, int event, void *data); 374#endif 375 376#endif /* _KERNEL */ 377 378#endif /* !_SYS_SOCKETVAR_H_ */ 379