socketvar.h revision 215178
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 215178 2010-11-12 13:02:26Z luigi $ 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 58struct socket; 59 60/*- 61 * Locking key to struct socket: 62 * (a) constant after allocation, no locking required. 63 * (b) locked by SOCK_LOCK(so). 64 * (c) locked by SOCKBUF_LOCK(&so->so_rcv). 65 * (d) locked by SOCKBUF_LOCK(&so->so_snd). 66 * (e) locked by ACCEPT_LOCK(). 67 * (f) not locked since integer reads/writes are atomic. 68 * (g) used only as a sleep/wakeup address, no value. 69 * (h) locked by global mutex so_global_mtx. 70 */ 71struct socket { 72 int so_count; /* (b) reference count */ 73 short so_type; /* (a) generic type, see socket.h */ 74 short so_options; /* from socket call, see socket.h */ 75 short so_linger; /* time to linger while closing */ 76 short so_state; /* (b) internal state flags SS_* */ 77 int so_qstate; /* (e) internal state flags SQ_* */ 78 void *so_pcb; /* protocol control block */ 79 struct vnet *so_vnet; /* network stack instance */ 80 struct protosw *so_proto; /* (a) protocol handle */ 81/* 82 * Variables for connection queuing. 83 * Socket where accepts occur is so_head in all subsidiary sockets. 84 * If so_head is 0, socket is not related to an accept. 85 * For head socket so_incomp queues partially completed connections, 86 * while so_comp is a queue of connections ready to be accepted. 87 * If a connection is aborted and it has so_head set, then 88 * it has to be pulled out of either so_incomp or so_comp. 89 * We allow connections to queue up based on current queue lengths 90 * and limit on number of queued connections for this socket. 91 */ 92 struct socket *so_head; /* (e) back pointer to listen socket */ 93 TAILQ_HEAD(, socket) so_incomp; /* (e) queue of partial unaccepted connections */ 94 TAILQ_HEAD(, socket) so_comp; /* (e) queue of complete unaccepted connections */ 95 TAILQ_ENTRY(socket) so_list; /* (e) list of unaccepted connections */ 96 u_short so_qlen; /* (e) number of unaccepted connections */ 97 u_short so_incqlen; /* (e) number of unaccepted incomplete 98 connections */ 99 u_short so_qlimit; /* (e) max number queued connections */ 100 short so_timeo; /* (g) connection timeout */ 101 u_short so_error; /* (f) error affecting connection */ 102 struct sigio *so_sigio; /* [sg] information for async I/O or 103 out of band data (SIGURG) */ 104 u_long so_oobmark; /* (c) chars to oob mark */ 105 TAILQ_HEAD(, aiocblist) so_aiojobq; /* AIO ops waiting on socket */ 106 107 struct sockbuf so_rcv, so_snd; 108 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 /* 121 * so_fibnum, so_user_cookie and friends can be used to attach 122 * some user-specified metadata to a socket, which then can be 123 * used by the kernel for various actions. 124 * so_user_cookie is used by ipfw/dummynet. 125 */ 126 int so_fibnum; /* routing domain for this socket */ 127 uint32_t so_user_cookie; 128}; 129 130/* 131 * Global accept mutex to serialize access to accept queues and 132 * fields associated with multiple sockets. This allows us to 133 * avoid defining a lock order between listen and accept sockets 134 * until such time as it proves to be a good idea. 135 */ 136extern struct mtx accept_mtx; 137#define ACCEPT_LOCK_ASSERT() mtx_assert(&accept_mtx, MA_OWNED) 138#define ACCEPT_UNLOCK_ASSERT() mtx_assert(&accept_mtx, MA_NOTOWNED) 139#define ACCEPT_LOCK() mtx_lock(&accept_mtx) 140#define ACCEPT_UNLOCK() mtx_unlock(&accept_mtx) 141 142/* 143 * Per-socket mutex: we reuse the receive socket buffer mutex for space 144 * efficiency. This decision should probably be revisited as we optimize 145 * locking for the socket code. 146 */ 147#define SOCK_MTX(_so) SOCKBUF_MTX(&(_so)->so_rcv) 148#define SOCK_LOCK(_so) SOCKBUF_LOCK(&(_so)->so_rcv) 149#define SOCK_OWNED(_so) SOCKBUF_OWNED(&(_so)->so_rcv) 150#define SOCK_UNLOCK(_so) SOCKBUF_UNLOCK(&(_so)->so_rcv) 151#define SOCK_LOCK_ASSERT(_so) SOCKBUF_LOCK_ASSERT(&(_so)->so_rcv) 152 153/* 154 * Socket state bits stored in so_qstate. 155 */ 156#define SQ_INCOMP 0x0800 /* unaccepted, incomplete connection */ 157#define SQ_COMP 0x1000 /* unaccepted, complete connection */ 158 159/* 160 * Externalized form of struct socket used by the sysctl(3) interface. 161 */ 162struct xsocket { 163 size_t xso_len; /* length of this structure */ 164 struct socket *xso_so; /* makes a convenient handle sometimes */ 165 short so_type; 166 short so_options; 167 short so_linger; 168 short so_state; 169 caddr_t so_pcb; /* another convenient handle */ 170 int xso_protocol; 171 int xso_family; 172 u_short so_qlen; 173 u_short so_incqlen; 174 u_short so_qlimit; 175 short so_timeo; 176 u_short so_error; 177 pid_t so_pgid; 178 u_long so_oobmark; 179 struct xsockbuf so_rcv, so_snd; 180 uid_t so_uid; /* XXX */ 181}; 182 183#ifdef _KERNEL 184 185/* 186 * Macros for sockets and socket buffering. 187 */ 188 189/* 190 * Flags to sblock(). 191 */ 192#define SBL_WAIT 0x00000001 /* Wait if not immediately available. */ 193#define SBL_NOINTR 0x00000002 /* Force non-interruptible sleep. */ 194#define SBL_VALID (SBL_WAIT | SBL_NOINTR) 195 196/* 197 * Do we need to notify the other side when I/O is possible? 198 */ 199#define sb_notify(sb) (((sb)->sb_flags & (SB_WAIT | SB_SEL | SB_ASYNC | \ 200 SB_UPCALL | SB_AIO | SB_KNOTE)) != 0) 201 202/* do we have to send all at once on a socket? */ 203#define sosendallatonce(so) \ 204 ((so)->so_proto->pr_flags & PR_ATOMIC) 205 206/* can we read something from so? */ 207#define soreadabledata(so) \ 208 ((so)->so_rcv.sb_cc >= (so)->so_rcv.sb_lowat || \ 209 !TAILQ_EMPTY(&(so)->so_comp) || (so)->so_error) 210#define soreadable(so) \ 211 (soreadabledata(so) || ((so)->so_rcv.sb_state & SBS_CANTRCVMORE)) 212 213/* can we write something to so? */ 214#define sowriteable(so) \ 215 ((sbspace(&(so)->so_snd) >= (so)->so_snd.sb_lowat && \ 216 (((so)->so_state&SS_ISCONNECTED) || \ 217 ((so)->so_proto->pr_flags&PR_CONNREQUIRED)==0)) || \ 218 ((so)->so_snd.sb_state & SBS_CANTSENDMORE) || \ 219 (so)->so_error) 220 221/* 222 * soref()/sorele() ref-count the socket structure. Note that you must 223 * still explicitly close the socket, but the last ref count will free 224 * the structure. 225 */ 226#define soref(so) do { \ 227 SOCK_LOCK_ASSERT(so); \ 228 ++(so)->so_count; \ 229} while (0) 230 231#define sorele(so) do { \ 232 ACCEPT_LOCK_ASSERT(); \ 233 SOCK_LOCK_ASSERT(so); \ 234 if ((so)->so_count <= 0) \ 235 panic("sorele"); \ 236 if (--(so)->so_count == 0) \ 237 sofree(so); \ 238 else { \ 239 SOCK_UNLOCK(so); \ 240 ACCEPT_UNLOCK(); \ 241 } \ 242} while (0) 243 244/* 245 * In sorwakeup() and sowwakeup(), acquire the socket buffer lock to 246 * avoid a non-atomic test-and-wakeup. However, sowakeup is 247 * responsible for releasing the lock if it is called. We unlock only 248 * if we don't call into sowakeup. If any code is introduced that 249 * directly invokes the underlying sowakeup() primitives, it must 250 * maintain the same semantics. 251 */ 252#define sorwakeup_locked(so) do { \ 253 SOCKBUF_LOCK_ASSERT(&(so)->so_rcv); \ 254 if (sb_notify(&(so)->so_rcv)) \ 255 sowakeup((so), &(so)->so_rcv); \ 256 else \ 257 SOCKBUF_UNLOCK(&(so)->so_rcv); \ 258} while (0) 259 260#define sorwakeup(so) do { \ 261 SOCKBUF_LOCK(&(so)->so_rcv); \ 262 sorwakeup_locked(so); \ 263} while (0) 264 265#define sowwakeup_locked(so) do { \ 266 SOCKBUF_LOCK_ASSERT(&(so)->so_snd); \ 267 if (sb_notify(&(so)->so_snd)) \ 268 sowakeup((so), &(so)->so_snd); \ 269 else \ 270 SOCKBUF_UNLOCK(&(so)->so_snd); \ 271} while (0) 272 273#define sowwakeup(so) do { \ 274 SOCKBUF_LOCK(&(so)->so_snd); \ 275 sowwakeup_locked(so); \ 276} while (0) 277 278struct accept_filter { 279 char accf_name[16]; 280 int (*accf_callback) 281 (struct socket *so, void *arg, int waitflag); 282 void * (*accf_create) 283 (struct socket *so, char *arg); 284 void (*accf_destroy) 285 (struct socket *so); 286 SLIST_ENTRY(accept_filter) accf_next; 287}; 288 289#ifdef MALLOC_DECLARE 290MALLOC_DECLARE(M_ACCF); 291MALLOC_DECLARE(M_PCB); 292MALLOC_DECLARE(M_SONAME); 293#endif 294 295extern int maxsockets; 296extern u_long sb_max; 297extern struct uma_zone *socket_zone; 298extern so_gen_t so_gencnt; 299 300struct mbuf; 301struct sockaddr; 302struct ucred; 303struct uio; 304 305/* 'which' values for socket upcalls. */ 306#define SO_RCV 1 307#define SO_SND 2 308 309/* Return values for socket upcalls. */ 310#define SU_OK 0 311#define SU_ISCONNECTED 1 312 313/* 314 * From uipc_socket and friends 315 */ 316int sockargs(struct mbuf **mp, caddr_t buf, int buflen, int type); 317int getsockaddr(struct sockaddr **namp, caddr_t uaddr, size_t len); 318void soabort(struct socket *so); 319int soaccept(struct socket *so, struct sockaddr **nam); 320int socheckuid(struct socket *so, uid_t uid); 321int sobind(struct socket *so, struct sockaddr *nam, struct thread *td); 322int soclose(struct socket *so); 323int soconnect(struct socket *so, struct sockaddr *nam, struct thread *td); 324int soconnect2(struct socket *so1, struct socket *so2); 325int socow_setup(struct mbuf *m0, struct uio *uio); 326int socreate(int dom, struct socket **aso, int type, int proto, 327 struct ucred *cred, struct thread *td); 328int sodisconnect(struct socket *so); 329struct sockaddr *sodupsockaddr(const struct sockaddr *sa, int mflags); 330void sofree(struct socket *so); 331void sohasoutofband(struct socket *so); 332int solisten(struct socket *so, int backlog, struct thread *td); 333void solisten_proto(struct socket *so, int backlog); 334int solisten_proto_check(struct socket *so); 335struct socket * 336 sonewconn(struct socket *head, int connstatus); 337 338 339int sopoll(struct socket *so, int events, struct ucred *active_cred, 340 struct thread *td); 341int sopoll_generic(struct socket *so, int events, 342 struct ucred *active_cred, struct thread *td); 343int soreceive(struct socket *so, struct sockaddr **paddr, struct uio *uio, 344 struct mbuf **mp0, struct mbuf **controlp, int *flagsp); 345int soreceive_stream(struct socket *so, struct sockaddr **paddr, 346 struct uio *uio, struct mbuf **mp0, struct mbuf **controlp, 347 int *flagsp); 348int soreceive_dgram(struct socket *so, struct sockaddr **paddr, 349 struct uio *uio, struct mbuf **mp0, struct mbuf **controlp, 350 int *flagsp); 351int soreceive_generic(struct socket *so, struct sockaddr **paddr, 352 struct uio *uio, struct mbuf **mp0, struct mbuf **controlp, 353 int *flagsp); 354int soreserve(struct socket *so, u_long sndcc, u_long rcvcc); 355void sorflush(struct socket *so); 356int sosend(struct socket *so, struct sockaddr *addr, struct uio *uio, 357 struct mbuf *top, struct mbuf *control, int flags, 358 struct thread *td); 359int sosend_dgram(struct socket *so, struct sockaddr *addr, 360 struct uio *uio, struct mbuf *top, struct mbuf *control, 361 int flags, struct thread *td); 362int sosend_generic(struct socket *so, struct sockaddr *addr, 363 struct uio *uio, struct mbuf *top, struct mbuf *control, 364 int flags, struct thread *td); 365int soshutdown(struct socket *so, int how); 366void sotoxsocket(struct socket *so, struct xsocket *xso); 367void soupcall_clear(struct socket *so, int which); 368void soupcall_set(struct socket *so, int which, 369 int (*func)(struct socket *, void *, int), void *arg); 370void sowakeup(struct socket *so, struct sockbuf *sb); 371int selsocket(struct socket *so, int events, struct timeval *tv, 372 struct thread *td); 373 374/* 375 * Accept filter functions (duh). 376 */ 377int accept_filt_add(struct accept_filter *filt); 378int accept_filt_del(char *name); 379struct accept_filter *accept_filt_get(char *name); 380#ifdef ACCEPT_FILTER_MOD 381#ifdef SYSCTL_DECL 382SYSCTL_DECL(_net_inet_accf); 383#endif 384int accept_filt_generic_mod_event(module_t mod, int event, void *data); 385#endif 386 387#endif /* _KERNEL */ 388 389#endif /* !_SYS_SOCKETVAR_H_ */ 390