socketvar.h revision 130398
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 * $FreeBSD: head/sys/sys/socketvar.h 130398 2004-06-13 02:50:07Z rwatson $ 31 */ 32 33#ifndef _SYS_SOCKETVAR_H_ 34#define _SYS_SOCKETVAR_H_ 35 36#include <sys/queue.h> /* for TAILQ macros */ 37#include <sys/selinfo.h> /* for struct selinfo */ 38#include <sys/_lock.h> 39#include <sys/_mutex.h> 40 41/* 42 * Kernel structure per socket. 43 * Contains send and receive buffer queues, 44 * handle on protocol and pointer to protocol 45 * private data and error information. 46 */ 47typedef u_quad_t so_gen_t; 48 49/*- 50 * Locking key to struct socket: 51 * (a) constant after allocation, no locking required. 52 * (b) locked by SOCK_LOCK(so). 53 * (c) locked by SOCKBUF_LOCK(&so->so_rcv). 54 * (d) locked by SOCKBUF_LOCK(&so->so_snd). 55 * (e) locked by ACCEPT_LOCK(). 56 * (f) not locked since integer reads/writes are atomic. 57 * (g) used only as a sleep/wakeup address, no value. 58 */ 59struct socket { 60 int so_count; /* (b) reference count */ 61 short so_type; /* (a) generic type, see socket.h */ 62 short so_options; /* from socket call, see socket.h */ 63 short so_linger; /* time to linger while closing */ 64 short so_state; /* internal state flags SS_* */ 65 int so_qstate; /* (e) internal state flags SQ_* */ 66 void *so_pcb; /* protocol control block */ 67 struct protosw *so_proto; /* (a) protocol handle */ 68/* 69 * Variables for connection queuing. 70 * Socket where accepts occur is so_head in all subsidiary sockets. 71 * If so_head is 0, socket is not related to an accept. 72 * For head socket so_incomp queues partially completed connections, 73 * while so_comp is a queue of connections ready to be accepted. 74 * If a connection is aborted and it has so_head set, then 75 * it has to be pulled out of either so_incomp or so_comp. 76 * We allow connections to queue up based on current queue lengths 77 * and limit on number of queued connections for this socket. 78 */ 79 struct socket *so_head; /* (e) back pointer to accept socket */ 80 TAILQ_HEAD(, socket) so_incomp; /* (e) queue of partial unaccepted connections */ 81 TAILQ_HEAD(, socket) so_comp; /* (e) queue of complete unaccepted connections */ 82 TAILQ_ENTRY(socket) so_list; /* (e) list of unaccepted connections */ 83 short so_qlen; /* (e) number of unaccepted connections */ 84 short so_incqlen; /* (e) number of unaccepted incomplete 85 connections */ 86 short so_qlimit; /* (e) max number queued connections */ 87 short so_timeo; /* connection timeout */ 88 u_short so_error; /* error affecting connection */ 89 struct sigio *so_sigio; /* [sg] information for async I/O or 90 out of band data (SIGURG) */ 91 u_long so_oobmark; /* chars to oob mark */ 92 TAILQ_HEAD(, aiocblist) so_aiojobq; /* AIO ops waiting on socket */ 93/* 94 * Variables for socket buffering. 95 */ 96 struct sockbuf { 97 struct selinfo sb_sel; /* process selecting read/write */ 98 struct mtx sb_mtx; /* sockbuf lock */ 99#define sb_startzero sb_mb 100 struct mbuf *sb_mb; /* the mbuf chain */ 101 struct mbuf *sb_mbtail; /* the last mbuf in the chain */ 102 struct mbuf *sb_lastrecord; /* first mbuf of last record in 103 * socket buffer */ 104 u_int sb_cc; /* actual chars in buffer */ 105 u_int sb_hiwat; /* max actual char count */ 106 u_int sb_mbcnt; /* chars of mbufs used */ 107 u_int sb_mbmax; /* max chars of mbufs to use */ 108 u_int sb_ctl; /* non-data chars in buffer */ 109 int sb_lowat; /* low water mark */ 110 int sb_timeo; /* timeout for read/write */ 111 short sb_flags; /* flags, see below */ 112 } so_rcv, so_snd; 113#define SB_MAX (256*1024) /* default for max chars in sockbuf */ 114#define SB_LOCK 0x01 /* lock on data queue */ 115#define SB_WANT 0x02 /* someone is waiting to lock */ 116#define SB_WAIT 0x04 /* someone is waiting for data/space */ 117#define SB_SEL 0x08 /* someone is selecting */ 118#define SB_ASYNC 0x10 /* ASYNC I/O, need signals */ 119#define SB_UPCALL 0x20 /* someone wants an upcall */ 120#define SB_NOINTR 0x40 /* operations not interruptible */ 121#define SB_AIO 0x80 /* AIO operations queued */ 122#define SB_KNOTE 0x100 /* kernel note attached */ 123 124 void (*so_upcall)(struct socket *, void *, int); 125 void *so_upcallarg; 126 struct ucred *so_cred; /* user credentials */ 127 struct label *so_label; /* (b) MAC label for socket */ 128 struct label *so_peerlabel; /* (b) cached MAC label for peer */ 129 /* NB: generation count must not be first; easiest to make it last. */ 130 so_gen_t so_gencnt; /* generation count */ 131 void *so_emuldata; /* private data for emulators */ 132 struct so_accf { 133 struct accept_filter *so_accept_filter; 134 void *so_accept_filter_arg; /* saved filter args */ 135 char *so_accept_filter_str; /* saved user args */ 136 } *so_accf; 137}; 138 139#define SB_EMPTY_FIXUP(sb) do { \ 140 if ((sb)->sb_mb == NULL) { \ 141 (sb)->sb_mbtail = NULL; \ 142 (sb)->sb_lastrecord = NULL; \ 143 } \ 144} while (/*CONSTCOND*/0) 145 146/* 147 * Global accept mutex to serialize access to accept queues and 148 * fields associated with multiple sockets. This allows us to 149 * avoid defining a lock order between listen and accept sockets 150 * until such time as it proves to be a good idea. 151 */ 152extern struct mtx accept_mtx; 153#define ACCEPT_LOCK() mtx_lock(&accept_mtx) 154#define ACCEPT_UNLOCK() mtx_unlock(&accept_mtx) 155 156/* 157 * Per-socket buffer mutex used to protect most fields in the socket 158 * buffer. 159 */ 160#define SOCKBUF_MTX(_sb) (&(_sb)->sb_mtx) 161#define SOCKBUF_LOCK_INIT(_sb, _name) \ 162 mtx_init(SOCKBUF_MTX(_sb), _name, NULL, MTX_DEF) 163#define SOCKBUF_LOCK_DESTROY(_sb) mtx_destroy(SOCKBUF_MTX(_sb)) 164#define SOCKBUF_LOCK(_sb) mtx_lock(SOCKBUF_MTX(_sb)) 165#define SOCKBUF_OWNED(_sb) mtx_owned(SOCKBUF_MTX(_sb)) 166#define SOCKBUF_UNLOCK(_sb) mtx_unlock(SOCKBUF_MTX(_sb)) 167#define SOCKBUF_LOCK_ASSERT(_sb) mtx_assert(SOCKBUF_MTX(_sb), MA_OWNED) 168 169/* 170 * Per-socket mutex: we reuse the receive socket buffer mutex for space 171 * efficiency. This decision should probably be revisited as we optimize 172 * locking for the socket code. 173 */ 174#define SOCK_MTX(_so) SOCKBUF_MTX(&(_so)->so_rcv) 175#define SOCK_LOCK(_so) SOCKBUF_LOCK(&(_so)->so_rcv) 176#define SOCK_OWNED(_so) SOCKBUF_OWNED(&(_so)->so_rcv) 177#define SOCK_UNLOCK(_so) SOCKBUF_UNLOCK(&(_so)->so_rcv) 178#define SOCK_LOCK_ASSERT(_so) SOCKBUF_LOCK_ASSERT(&(_so)->so_rcv) 179 180/* 181 * Socket state bits. 182 */ 183#define SS_NOFDREF 0x0001 /* no file table ref any more */ 184#define SS_ISCONNECTED 0x0002 /* socket connected to a peer */ 185#define SS_ISCONNECTING 0x0004 /* in process of connecting to peer */ 186#define SS_ISDISCONNECTING 0x0008 /* in process of disconnecting */ 187#define SS_CANTSENDMORE 0x0010 /* can't send more data to peer */ 188#define SS_CANTRCVMORE 0x0020 /* can't receive more data from peer */ 189#define SS_RCVATMARK 0x0040 /* at mark on input */ 190 191#define SS_NBIO 0x0100 /* non-blocking ops */ 192#define SS_ASYNC 0x0200 /* async i/o notify */ 193#define SS_ISCONFIRMING 0x0400 /* deciding to accept connection req */ 194 195#define SS_ISDISCONNECTED 0x2000 /* socket disconnected from peer */ 196 197/* 198 * Socket state bits stored in so_qstate. 199 */ 200#define SQ_INCOMP 0x0800 /* unaccepted, incomplete connection */ 201#define SQ_COMP 0x1000 /* unaccepted, complete connection */ 202 203/* 204 * Externalized form of struct socket used by the sysctl(3) interface. 205 */ 206struct xsocket { 207 size_t xso_len; /* length of this structure */ 208 struct socket *xso_so; /* makes a convenient handle sometimes */ 209 short so_type; 210 short so_options; 211 short so_linger; 212 short so_state; 213 caddr_t so_pcb; /* another convenient handle */ 214 int xso_protocol; 215 int xso_family; 216 short so_qlen; 217 short so_incqlen; 218 short so_qlimit; 219 short so_timeo; 220 u_short so_error; 221 pid_t so_pgid; 222 u_long so_oobmark; 223 struct xsockbuf { 224 u_int sb_cc; 225 u_int sb_hiwat; 226 u_int sb_mbcnt; 227 u_int sb_mbmax; 228 int sb_lowat; 229 int sb_timeo; 230 short sb_flags; 231 } so_rcv, so_snd; 232 uid_t so_uid; /* XXX */ 233}; 234 235#ifdef _KERNEL 236 237/* 238 * Macros for sockets and socket buffering. 239 */ 240 241/* 242 * Do we need to notify the other side when I/O is possible? 243 */ 244#define sb_notify(sb) (((sb)->sb_flags & (SB_WAIT | SB_SEL | SB_ASYNC | \ 245 SB_UPCALL | SB_AIO | SB_KNOTE)) != 0) 246 247/* 248 * How much space is there in a socket buffer (so->so_snd or so->so_rcv)? 249 * This is problematical if the fields are unsigned, as the space might 250 * still be negative (cc > hiwat or mbcnt > mbmax). Should detect 251 * overflow and return 0. Should use "lmin" but it doesn't exist now. 252 */ 253#define sbspace(sb) \ 254 ((long) imin((int)((sb)->sb_hiwat - (sb)->sb_cc), \ 255 (int)((sb)->sb_mbmax - (sb)->sb_mbcnt))) 256 257/* do we have to send all at once on a socket? */ 258#define sosendallatonce(so) \ 259 ((so)->so_proto->pr_flags & PR_ATOMIC) 260 261/* can we read something from so? */ 262#define soreadable(so) \ 263 ((so)->so_rcv.sb_cc >= (so)->so_rcv.sb_lowat || \ 264 ((so)->so_state & SS_CANTRCVMORE) || \ 265 !TAILQ_EMPTY(&(so)->so_comp) || (so)->so_error) 266 267/* can we write something to so? */ 268#define sowriteable(so) \ 269 ((sbspace(&(so)->so_snd) >= (so)->so_snd.sb_lowat && \ 270 (((so)->so_state&SS_ISCONNECTED) || \ 271 ((so)->so_proto->pr_flags&PR_CONNREQUIRED)==0)) || \ 272 ((so)->so_state & SS_CANTSENDMORE) || \ 273 (so)->so_error) 274 275/* adjust counters in sb reflecting allocation of m */ 276#define sballoc(sb, m) { \ 277 (sb)->sb_cc += (m)->m_len; \ 278 if ((m)->m_type != MT_DATA && (m)->m_type != MT_HEADER && \ 279 (m)->m_type != MT_OOBDATA) \ 280 (sb)->sb_ctl += (m)->m_len; \ 281 (sb)->sb_mbcnt += MSIZE; \ 282 if ((m)->m_flags & M_EXT) \ 283 (sb)->sb_mbcnt += (m)->m_ext.ext_size; \ 284} 285 286/* adjust counters in sb reflecting freeing of m */ 287#define sbfree(sb, m) { \ 288 (sb)->sb_cc -= (m)->m_len; \ 289 if ((m)->m_type != MT_DATA && (m)->m_type != MT_HEADER && \ 290 (m)->m_type != MT_OOBDATA) \ 291 (sb)->sb_ctl -= (m)->m_len; \ 292 (sb)->sb_mbcnt -= MSIZE; \ 293 if ((m)->m_flags & M_EXT) \ 294 (sb)->sb_mbcnt -= (m)->m_ext.ext_size; \ 295} 296 297/* 298 * Set lock on sockbuf sb; sleep if lock is already held. 299 * Unless SB_NOINTR is set on sockbuf, sleep is interruptible. 300 * Returns error without lock if sleep is interrupted. 301 */ 302#define sblock(sb, wf) ((sb)->sb_flags & SB_LOCK ? \ 303 (((wf) == M_WAITOK) ? sb_lock(sb) : EWOULDBLOCK) : \ 304 ((sb)->sb_flags |= SB_LOCK), 0) 305 306/* release lock on sockbuf sb */ 307#define sbunlock(sb) { \ 308 (sb)->sb_flags &= ~SB_LOCK; \ 309 if ((sb)->sb_flags & SB_WANT) { \ 310 (sb)->sb_flags &= ~SB_WANT; \ 311 wakeup(&(sb)->sb_flags); \ 312 } \ 313} 314 315/* 316 * soref()/sorele() ref-count the socket structure. Note that you must 317 * still explicitly close the socket, but the last ref count will free 318 * the structure. 319 */ 320#define soref(so) do { \ 321 SOCK_LOCK_ASSERT(so); \ 322 ++(so)->so_count; \ 323} while (0) 324 325#define sorele(so) do { \ 326 SOCK_LOCK_ASSERT(so); \ 327 if ((so)->so_count <= 0) \ 328 panic("sorele"); \ 329 if (--(so)->so_count == 0) \ 330 sofree(so); \ 331 else \ 332 SOCK_UNLOCK(so); \ 333} while (0) 334 335#define sotryfree(so) do { \ 336 SOCK_LOCK_ASSERT(so); \ 337 if ((so)->so_count == 0) \ 338 sofree(so); \ 339 else \ 340 SOCK_UNLOCK(so); \ 341} while(0) 342 343#define sorwakeup(so) do { \ 344 if (sb_notify(&(so)->so_rcv)) \ 345 sowakeup((so), &(so)->so_rcv); \ 346} while (0) 347 348#define sowwakeup(so) do { \ 349 if (sb_notify(&(so)->so_snd)) \ 350 sowakeup((so), &(so)->so_snd); \ 351} while (0) 352 353/* 354 * Argument structure for sosetopt et seq. This is in the KERNEL 355 * section because it will never be visible to user code. 356 */ 357enum sopt_dir { SOPT_GET, SOPT_SET }; 358struct sockopt { 359 enum sopt_dir sopt_dir; /* is this a get or a set? */ 360 int sopt_level; /* second arg of [gs]etsockopt */ 361 int sopt_name; /* third arg of [gs]etsockopt */ 362 void *sopt_val; /* fourth arg of [gs]etsockopt */ 363 size_t sopt_valsize; /* (almost) fifth arg of [gs]etsockopt */ 364 struct thread *sopt_td; /* calling thread or null if kernel */ 365}; 366 367struct accept_filter { 368 char accf_name[16]; 369 void (*accf_callback) 370 (struct socket *so, void *arg, int waitflag); 371 void * (*accf_create) 372 (struct socket *so, char *arg); 373 void (*accf_destroy) 374 (struct socket *so); 375 SLIST_ENTRY(accept_filter) accf_next; 376}; 377 378#ifdef MALLOC_DECLARE 379MALLOC_DECLARE(M_ACCF); 380MALLOC_DECLARE(M_PCB); 381MALLOC_DECLARE(M_SONAME); 382#endif 383 384extern int maxsockets; 385extern u_long sb_max; 386extern struct uma_zone *socket_zone; 387extern so_gen_t so_gencnt; 388 389struct mbuf; 390struct sockaddr; 391struct ucred; 392struct uio; 393 394/* 395 * From uipc_socket and friends 396 */ 397int sockargs(struct mbuf **mp, caddr_t buf, int buflen, int type); 398int getsockaddr(struct sockaddr **namp, caddr_t uaddr, size_t len); 399void sbappend(struct sockbuf *sb, struct mbuf *m); 400void sbappendstream(struct sockbuf *sb, struct mbuf *m); 401int sbappendaddr(struct sockbuf *sb, const struct sockaddr *asa, 402 struct mbuf *m0, struct mbuf *control); 403int sbappendcontrol(struct sockbuf *sb, struct mbuf *m0, 404 struct mbuf *control); 405void sbappendrecord(struct sockbuf *sb, struct mbuf *m0); 406void sbcheck(struct sockbuf *sb); 407void sbcompress(struct sockbuf *sb, struct mbuf *m, struct mbuf *n); 408struct mbuf * 409 sbcreatecontrol(caddr_t p, int size, int type, int level); 410void sbdrop(struct sockbuf *sb, int len); 411void sbdroprecord(struct sockbuf *sb); 412void sbflush(struct sockbuf *sb); 413void sbinsertoob(struct sockbuf *sb, struct mbuf *m0); 414void sbrelease(struct sockbuf *sb, struct socket *so); 415int sbreserve(struct sockbuf *sb, u_long cc, struct socket *so, 416 struct thread *td); 417void sbtoxsockbuf(struct sockbuf *sb, struct xsockbuf *xsb); 418int sbwait(struct sockbuf *sb); 419int sb_lock(struct sockbuf *sb); 420int soabort(struct socket *so); 421int soaccept(struct socket *so, struct sockaddr **nam); 422struct socket *soalloc(int mflags); 423int socheckuid(struct socket *so, uid_t uid); 424int sobind(struct socket *so, struct sockaddr *nam, struct thread *td); 425void socantrcvmore(struct socket *so); 426void socantsendmore(struct socket *so); 427int soclose(struct socket *so); 428int soconnect(struct socket *so, struct sockaddr *nam, struct thread *td); 429int soconnect2(struct socket *so1, struct socket *so2); 430int socow_setup(struct mbuf *m0, struct uio *uio); 431int socreate(int dom, struct socket **aso, int type, int proto, 432 struct ucred *cred, struct thread *td); 433void sodealloc(struct socket *so); 434int sodisconnect(struct socket *so); 435struct sockaddr *sodupsockaddr(const struct sockaddr *sa, int mflags); 436void sofree(struct socket *so); 437int sogetopt(struct socket *so, struct sockopt *sopt); 438void sohasoutofband(struct socket *so); 439void soisconnected(struct socket *so); 440void soisconnecting(struct socket *so); 441void soisdisconnected(struct socket *so); 442void soisdisconnecting(struct socket *so); 443int solisten(struct socket *so, int backlog, struct thread *td); 444struct socket * 445 sonewconn(struct socket *head, int connstatus); 446int sooptcopyin(struct sockopt *sopt, void *buf, size_t len, size_t minlen); 447int sooptcopyout(struct sockopt *sopt, const void *buf, size_t len); 448 449/* XXX; prepare mbuf for (__FreeBSD__ < 3) routines. */ 450int soopt_getm(struct sockopt *sopt, struct mbuf **mp); 451int soopt_mcopyin(struct sockopt *sopt, struct mbuf *m); 452int soopt_mcopyout(struct sockopt *sopt, struct mbuf *m); 453 454int sopoll(struct socket *so, int events, struct ucred *active_cred, 455 struct thread *td); 456int soreceive(struct socket *so, struct sockaddr **paddr, struct uio *uio, 457 struct mbuf **mp0, struct mbuf **controlp, int *flagsp); 458int soreserve(struct socket *so, u_long sndcc, u_long rcvcc); 459void sorflush(struct socket *so); 460int sosend(struct socket *so, struct sockaddr *addr, struct uio *uio, 461 struct mbuf *top, struct mbuf *control, int flags, 462 struct thread *td); 463int sosetopt(struct socket *so, struct sockopt *sopt); 464int soshutdown(struct socket *so, int how); 465void sotoxsocket(struct socket *so, struct xsocket *xso); 466void sowakeup(struct socket *so, struct sockbuf *sb); 467 468#ifdef SOCKBUF_DEBUG 469void sblastrecordchk(struct sockbuf *, const char *, int); 470#define SBLASTRECORDCHK(sb) sblastrecordchk((sb), __FILE__, __LINE__) 471 472void sblastmbufchk(struct sockbuf *, const char *, int); 473#define SBLASTMBUFCHK(sb) sblastmbufchk((sb), __FILE__, __LINE__) 474#else 475#define SBLASTRECORDCHK(sb) /* nothing */ 476#define SBLASTMBUFCHK(sb) /* nothing */ 477#endif /* SOCKBUF_DEBUG */ 478 479/* 480 * Accept filter functions (duh). 481 */ 482int accept_filt_add(struct accept_filter *filt); 483int accept_filt_del(char *name); 484struct accept_filter *accept_filt_get(char *name); 485#ifdef ACCEPT_FILTER_MOD 486#ifdef SYSCTL_DECL 487SYSCTL_DECL(_net_inet_accf); 488#endif 489int accept_filt_generic_mod_event(module_t mod, int event, void *data); 490#endif 491 492#endif /* _KERNEL */ 493 494#endif /* !_SYS_SOCKETVAR_H_ */ 495