socketvar.h revision 118453
11541Srgrimes/*- 21541Srgrimes * Copyright (c) 1982, 1986, 1990, 1993 31541Srgrimes * The Regents of the University of California. All rights reserved. 41541Srgrimes * 51541Srgrimes * Redistribution and use in source and binary forms, with or without 61541Srgrimes * modification, are permitted provided that the following conditions 71541Srgrimes * are met: 81541Srgrimes * 1. Redistributions of source code must retain the above copyright 91541Srgrimes * notice, this list of conditions and the following disclaimer. 101541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 111541Srgrimes * notice, this list of conditions and the following disclaimer in the 121541Srgrimes * documentation and/or other materials provided with the distribution. 131541Srgrimes * 3. All advertising materials mentioning features or use of this software 141541Srgrimes * must display the following acknowledgement: 151541Srgrimes * This product includes software developed by the University of 161541Srgrimes * California, Berkeley and its contributors. 171541Srgrimes * 4. Neither the name of the University nor the names of its contributors 181541Srgrimes * may be used to endorse or promote products derived from this software 191541Srgrimes * without specific prior written permission. 201541Srgrimes * 211541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 221541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 231541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 241541Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 251541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 261541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 271541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 281541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 291541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 301541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 311541Srgrimes * SUCH DAMAGE. 321541Srgrimes * 3314487Shsu * @(#)socketvar.h 8.3 (Berkeley) 2/19/95 3450477Speter * $FreeBSD: head/sys/sys/socketvar.h 118453 2003-08-05 00:27:54Z hsu $ 351541Srgrimes */ 361541Srgrimes 371541Srgrimes#ifndef _SYS_SOCKETVAR_H_ 381541Srgrimes#define _SYS_SOCKETVAR_H_ 391541Srgrimes 4014487Shsu#include <sys/_label.h> /* for struct label */ 4114487Shsu#include <sys/queue.h> /* for TAILQ macros */ 421541Srgrimes#include <sys/selinfo.h> /* for struct selinfo */ 431541Srgrimes 441541Srgrimes/* 451541Srgrimes * Kernel structure per socket. 461541Srgrimes * Contains send and receive buffer queues, 471541Srgrimes * handle on protocol and pointer to protocol 481541Srgrimes * private data and error information. 491541Srgrimes */ 501541Srgrimestypedef u_quad_t so_gen_t; 5125775Speter 5225775Speter/* 531541Srgrimes * List of locks: 5418397Snate * (c) const, inited in either socreate() or sonewconn() 5518397Snate * (m) sb_mtx mutex 561541Srgrimes * (mr) so_rcv.sb_mtx mutex 5725775Speter * (sg) sigio_lock sx 581541Srgrimes * (sh) sohead_lock sx 5941827Sdes * 6041827Sdes * Lock of so_rcv.sb_mtx can duplicate, provided that sohead_lock 6141827Sdes * is exclusively locked. 6241827Sdes * 6341827Sdes * Brackets mean that this data is not protected yet. 6441827Sdes */ 6541827Sdesstruct socket { 6641827Sdes int so_count; /* reference count */ 6741827Sdes short so_type; /* generic type, see socket.h */ 6841827Sdes short so_options; /* from socket call, see socket.h */ 691541Srgrimes short so_linger; /* time to linger while closing */ 701541Srgrimes short so_state; /* internal state flags SS_*, below */ 711541Srgrimes void *so_pcb; /* protocol control block */ 721541Srgrimes struct protosw *so_proto; /* protocol handle */ 731541Srgrimes/* 741541Srgrimes * Variables for connection queuing. 751541Srgrimes * Socket where accepts occur is so_head in all subsidiary sockets. 761541Srgrimes * If so_head is 0, socket is not related to an accept. 771541Srgrimes * For head socket so_incomp queues partially completed connections, 781541Srgrimes * while so_comp is a queue of connections ready to be accepted. 791541Srgrimes * If a connection is aborted and it has so_head set, then 801541Srgrimes * it has to be pulled out of either so_incomp or so_comp. 811541Srgrimes * We allow connections to queue up based on current queue lengths 8233690Sphk * and limit on number of queued connections for this socket. 8334901Sphk */ 8434901Sphk struct socket *so_head; /* back pointer to accept socket */ 8533690Sphk TAILQ_HEAD(, socket) so_incomp; /* queue of partial unaccepted connections */ 8634901Sphk TAILQ_HEAD(, socket) so_comp; /* queue of complete unaccepted connections */ 8734901Sphk TAILQ_ENTRY(socket) so_list; /* list of unaccepted connections */ 8834901Sphk short so_qlen; /* number of unaccepted connections */ 8934901Sphk short so_incqlen; /* number of unaccepted incomplete 9034901Sphk connections */ 9133690Sphk short so_qlimit; /* max number queued connections */ 9234901Sphk short so_timeo; /* connection timeout */ 9333690Sphk u_short so_error; /* error affecting connection */ 9434901Sphk struct sigio *so_sigio; /* [sg] information for async I/O or 9533690Sphk out of band data (SIGURG) */ 9634901Sphk u_long so_oobmark; /* chars to oob mark */ 9733690Sphk TAILQ_HEAD(, aiocblist) so_aiojobq; /* AIO ops waiting on socket */ 9833690Sphk/* 9933690Sphk * Variables for socket buffering. 10033690Sphk */ 10133690Sphk struct sockbuf { 10233690Sphk struct selinfo sb_sel; /* process selecting read/write */ 10333690Sphk#define sb_startzero sb_mb 10433690Sphk struct mbuf *sb_mb; /* the mbuf chain */ 10534901Sphk u_int sb_cc; /* actual chars in buffer */ 10634901Sphk u_int sb_hiwat; /* max actual char count */ 10734901Sphk u_int sb_mbcnt; /* chars of mbufs used */ 10833690Sphk u_int sb_mbmax; /* max chars of mbufs to use */ 10933690Sphk u_int sb_ctl; /* non-data chars in buffer */ 11034901Sphk int sb_lowat; /* low water mark */ 11133690Sphk int sb_timeo; /* timeout for read/write */ 11234901Sphk short sb_flags; /* flags, see below */ 11334901Sphk } so_rcv, so_snd; 11433690Sphk#define SB_MAX (256*1024) /* default for max chars in sockbuf */ 11533690Sphk#define SB_LOCK 0x01 /* lock on data queue */ 11633690Sphk#define SB_WANT 0x02 /* someone is waiting to lock */ 11734901Sphk#define SB_WAIT 0x04 /* someone is waiting for data/space */ 11834901Sphk#define SB_SEL 0x08 /* someone is selecting */ 11934901Sphk#define SB_ASYNC 0x10 /* ASYNC I/O, need signals */ 12034901Sphk#define SB_UPCALL 0x20 /* someone wants an upcall */ 12134901Sphk#define SB_NOINTR 0x40 /* operations not interruptible */ 12234901Sphk#define SB_AIO 0x80 /* AIO operations queued */ 12334901Sphk#define SB_KNOTE 0x100 /* kernel note attached */ 12448887Sbde 12548887Sbde void (*so_upcall)(struct socket *, void *, int); 12634901Sphk void *so_upcallarg; 12748887Sbde struct ucred *so_cred; /* user credentials */ 12848887Sbde struct label so_label; /* MAC label for socket */ 12948887Sbde struct label so_peerlabel; /* cached MAC label for socket peer */ 13034901Sphk /* NB: generation count must not be first; easiest to make it last. */ 13148887Sbde so_gen_t so_gencnt; /* generation count */ 13233690Sphk void *so_emuldata; /* private data for emulators */ 13333690Sphk struct so_accf { 13433690Sphk struct accept_filter *so_accept_filter; 13536719Sphk void *so_accept_filter_arg; /* saved filter args */ 13636741Sphk char *so_accept_filter_str; /* saved user args */ 13733690Sphk } *so_accf; 13833690Sphk}; 13934901Sphk 14036810Sphk/* 14136810Sphk * Socket state bits. 14236810Sphk */ 14336810Sphk#define SS_NOFDREF 0x0001 /* no file table ref any more */ 14436810Sphk#define SS_ISCONNECTED 0x0002 /* socket connected to a peer */ 14536810Sphk#define SS_ISCONNECTING 0x0004 /* in process of connecting to peer */ 14634901Sphk#define SS_ISDISCONNECTING 0x0008 /* in process of disconnecting */ 14744574Sphk#define SS_CANTSENDMORE 0x0010 /* can't send more data to peer */ 14836810Sphk#define SS_CANTRCVMORE 0x0020 /* can't receive more data from peer */ 14936810Sphk#define SS_RCVATMARK 0x0040 /* at mark on input */ 15036810Sphk 15136810Sphk#define SS_NBIO 0x0100 /* non-blocking ops */ 15236810Sphk#define SS_ASYNC 0x0200 /* async i/o notify */ 15336810Sphk#define SS_ISCONFIRMING 0x0400 /* deciding to accept connection req */ 15436810Sphk 15536810Sphk#define SS_INCOMP 0x0800 /* unaccepted, incomplete connection */ 15636810Sphk#define SS_COMP 0x1000 /* unaccepted, complete connection */ 15748887Sbde#define SS_ISDISCONNECTED 0x2000 /* socket disconnected from peer */ 15836810Sphk 15936810Sphk/* 16033690Sphk * Externalized form of struct socket used by the sysctl(3) interface. 16133690Sphk */ 16255205Speterstruct xsocket { 16335058Sphk size_t xso_len; /* length of this structure */ 16435029Sphk struct socket *xso_so; /* makes a convenient handle sometimes */ 16535401Seivind short so_type; 16635029Sphk short so_options; 16735029Sphk short so_linger; 16835029Sphk short so_state; 16935029Sphk caddr_t so_pcb; /* another convenient handle */ 17035029Sphk int xso_protocol; 17135058Sphk int xso_family; 17235029Sphk short so_qlen; 17335058Sphk short so_incqlen; 17435058Sphk short so_qlimit; 17535029Sphk short so_timeo; 17635029Sphk u_short so_error; 17735029Sphk pid_t so_pgid; 17835029Sphk u_long so_oobmark; 17935029Sphk struct xsockbuf { 18035058Sphk u_int sb_cc; 18135029Sphk u_int sb_hiwat; 18235058Sphk u_int sb_mbcnt; 18335058Sphk u_int sb_mbmax; 18435029Sphk int sb_lowat; 18535029Sphk int sb_timeo; 18635029Sphk short sb_flags; 18735029Sphk } so_rcv, so_snd; 18835029Sphk uid_t so_uid; /* XXX */ 18935058Sphk}; 1901541Srgrimes 19135058Sphk/* 19235058Sphk * Macros for sockets and socket buffering. 19335058Sphk */ 19435029Sphk 19535029Sphk/* 19635029Sphk * Do we need to notify the other side when I/O is possible? 19735029Sphk */ 19835058Sphk#define sb_notify(sb) (((sb)->sb_flags & (SB_WAIT | SB_SEL | SB_ASYNC | \ 19935058Sphk SB_UPCALL | SB_AIO | SB_KNOTE)) != 0) 20035058Sphk 20155205Speter/* 20235029Sphk * How much space is there in a socket buffer (so->so_snd or so->so_rcv)? 20355205Speter * This is problematical if the fields are unsigned, as the space might 20435058Sphk * still be negative (cc > hiwat or mbcnt > mbmax). Should detect 20535401Seivind * overflow and return 0. Should use "lmin" but it doesn't exist now. 2061541Srgrimes */ 20735058Sphk#define sbspace(sb) \ 2081541Srgrimes ((long) imin((int)((sb)->sb_hiwat - (sb)->sb_cc), \ 2091541Srgrimes (int)((sb)->sb_mbmax - (sb)->sb_mbcnt))) 2101541Srgrimes 21121099Speter/* do we have to send all at once on a socket? */ 21221099Speter#define sosendallatonce(so) \ 21321099Speter ((so)->so_proto->pr_flags & PR_ATOMIC) 21421099Speter 21521099Speter/* can we read something from so? */ 21621099Speter#define soreadable(so) \ 21721099Speter ((so)->so_rcv.sb_cc >= (so)->so_rcv.sb_lowat || \ 21821099Speter ((so)->so_state & SS_CANTRCVMORE) || \ 21921099Speter !TAILQ_EMPTY(&(so)->so_comp) || (so)->so_error) 22021099Speter 22121099Speter/* can we write something to so? */ 22221099Speter#define sowriteable(so) \ 22321099Speter ((sbspace(&(so)->so_snd) >= (so)->so_snd.sb_lowat && \ 22421099Speter (((so)->so_state&SS_ISCONNECTED) || \ 22521099Speter ((so)->so_proto->pr_flags&PR_CONNREQUIRED)==0)) || \ 22621099Speter ((so)->so_state & SS_CANTSENDMORE) || \ 22721099Speter (so)->so_error) 22821099Speter 22921099Speter/* adjust counters in sb reflecting allocation of m */ 2301541Srgrimes#define sballoc(sb, m) { \ 2311541Srgrimes (sb)->sb_cc += (m)->m_len; \ 2321541Srgrimes if ((m)->m_type != MT_DATA && (m)->m_type != MT_HEADER && \ 2331541Srgrimes (m)->m_type != MT_OOBDATA) \ 2341541Srgrimes (sb)->sb_ctl += (m)->m_len; \ 2351541Srgrimes (sb)->sb_mbcnt += MSIZE; \ 2361541Srgrimes if ((m)->m_flags & M_EXT) \ 2371541Srgrimes (sb)->sb_mbcnt += (m)->m_ext.ext_size; \ 2381541Srgrimes} 2391541Srgrimes 2401541Srgrimes/* adjust counters in sb reflecting freeing of m */ 2411541Srgrimes#define sbfree(sb, m) { \ 2421541Srgrimes (sb)->sb_cc -= (m)->m_len; \ 2431541Srgrimes if ((m)->m_type != MT_DATA && (m)->m_type != MT_HEADER && \ 2441541Srgrimes (m)->m_type != MT_OOBDATA) \ 2451541Srgrimes (sb)->sb_ctl -= (m)->m_len; \ 2461541Srgrimes (sb)->sb_mbcnt -= MSIZE; \ 2471541Srgrimes if ((m)->m_flags & M_EXT) \ 2481541Srgrimes (sb)->sb_mbcnt -= (m)->m_ext.ext_size; \ 2491541Srgrimes} 25026897Sjhay 2511541Srgrimes/* 2521541Srgrimes * Set lock on sockbuf sb; sleep if lock is already held. 2531541Srgrimes * Unless SB_NOINTR is set on sockbuf, sleep is interruptible. 2541541Srgrimes * Returns error without lock if sleep is interrupted. 25534030Sdufault */ 25634030Sdufault#define sblock(sb, wf) ((sb)->sb_flags & SB_LOCK ? \ 25734030Sdufault (((wf) == M_WAITOK) ? sb_lock(sb) : EWOULDBLOCK) : \ 25825578Speter ((sb)->sb_flags |= SB_LOCK), 0) 25934030Sdufault 26025578Speter/* release lock on sockbuf sb */ 26125578Speter#define sbunlock(sb) { \ 26225578Speter (sb)->sb_flags &= ~SB_LOCK; \ 26325578Speter if ((sb)->sb_flags & SB_WANT) { \ 26434030Sdufault (sb)->sb_flags &= ~SB_WANT; \ 26525578Speter wakeup(&(sb)->sb_flags); \ 26634030Sdufault } \ 26725578Speter} 26855205Speter 26933690Sphk/* 27034961Sphk * soref()/sorele() ref-count the socket structure. Note that you must 27133690Sphk * still explicitly close the socket, but the last ref count will free 27236119Sphk * the structure. 27334901Sphk */ 27436119Sphk#define soref(so) do { \ 27534901Sphk ++(so)->so_count; \ 27634901Sphk } while (0) 27735029Sphk 27814487Shsu#define sorele(so) do { \ 27936119Sphk if ((so)->so_count <= 0) \ 28033690Sphk panic("sorele");\ 28136119Sphk if (--(so)->so_count == 0)\ 28233690Sphk sofree(so); \ 28334901Sphk } while (0) 2843484Sphk 2853484Sphk#define sotryfree(so) do { \ 28634961Sphk if ((so)->so_count == 0) \ 28746054Sphk sofree(so); \ 28855205Speter } while(0) 2891541Srgrimes 2901541Srgrimes#define sorwakeup(so) do { \ 2911541Srgrimes if (sb_notify(&(so)->so_rcv)) \ 2921541Srgrimes sowakeup((so), &(so)->so_rcv); \ 2931541Srgrimes } while (0) 2941541Srgrimes 29555045Sbde#define sowwakeup(so) do { \ 2961541Srgrimes if (sb_notify(&(so)->so_snd)) \ 2971541Srgrimes sowakeup((so), &(so)->so_snd); \ 29855045Sbde } while (0) 2991541Srgrimes 3001541Srgrimes#ifdef _KERNEL 3011541Srgrimes 3021541Srgrimes/* 3031541Srgrimes * Argument structure for sosetopt et seq. This is in the KERNEL 30455205Speter * section because it will never be visible to user code. 3051541Srgrimes */ 3061541Srgrimesenum sopt_dir { SOPT_GET, SOPT_SET }; 307struct sockopt { 308 enum sopt_dir sopt_dir; /* is this a get or a set? */ 309 int sopt_level; /* second arg of [gs]etsockopt */ 310 int sopt_name; /* third arg of [gs]etsockopt */ 311 void *sopt_val; /* fourth arg of [gs]etsockopt */ 312 size_t sopt_valsize; /* (almost) fifth arg of [gs]etsockopt */ 313 struct thread *sopt_td; /* calling thread or null if kernel */ 314}; 315 316struct sf_buf { 317 SLIST_ENTRY(sf_buf) free_list; /* list of free buffer slots */ 318 struct vm_page *m; /* currently mapped page */ 319 vm_offset_t kva; /* va of mapping */ 320}; 321 322struct accept_filter { 323 char accf_name[16]; 324 void (*accf_callback) 325 (struct socket *so, void *arg, int waitflag); 326 void * (*accf_create) 327 (struct socket *so, char *arg); 328 void (*accf_destroy) 329 (struct socket *so); 330 SLIST_ENTRY(accept_filter) accf_next; 331}; 332 333#ifdef MALLOC_DECLARE 334MALLOC_DECLARE(M_ACCF); 335MALLOC_DECLARE(M_PCB); 336MALLOC_DECLARE(M_SONAME); 337#endif 338 339extern int maxsockets; 340extern u_long sb_max; 341extern struct uma_zone *socket_zone; 342extern so_gen_t so_gencnt; 343 344struct mbuf; 345struct sockaddr; 346struct ucred; 347struct uio; 348 349/* 350 * From uipc_socket and friends 351 */ 352struct sockaddr *dup_sockaddr(struct sockaddr *sa, int canwait); 353int sockargs(struct mbuf **mp, caddr_t buf, int buflen, int type); 354int getsockaddr(struct sockaddr **namp, caddr_t uaddr, size_t len); 355void sbappend(struct sockbuf *sb, struct mbuf *m); 356int sbappendaddr(struct sockbuf *sb, struct sockaddr *asa, 357 struct mbuf *m0, struct mbuf *control); 358int sbappendcontrol(struct sockbuf *sb, struct mbuf *m0, 359 struct mbuf *control); 360void sbappendrecord(struct sockbuf *sb, struct mbuf *m0); 361void sbcheck(struct sockbuf *sb); 362void sbcompress(struct sockbuf *sb, struct mbuf *m, struct mbuf *n); 363struct mbuf * 364 sbcreatecontrol(caddr_t p, int size, int type, int level); 365void sbdrop(struct sockbuf *sb, int len); 366void sbdroprecord(struct sockbuf *sb); 367void sbflush(struct sockbuf *sb); 368void sbinsertoob(struct sockbuf *sb, struct mbuf *m0); 369void sbrelease(struct sockbuf *sb, struct socket *so); 370int sbreserve(struct sockbuf *sb, u_long cc, struct socket *so, 371 struct thread *td); 372void sbtoxsockbuf(struct sockbuf *sb, struct xsockbuf *xsb); 373int sbwait(struct sockbuf *sb); 374struct sf_buf * 375 sf_buf_alloc(struct vm_page *m); 376void sf_buf_free(void *addr, void *args); 377int sb_lock(struct sockbuf *sb); 378int soabort(struct socket *so); 379int soaccept(struct socket *so, struct sockaddr **nam); 380int socheckuid(struct socket *so, uid_t uid); 381struct socket *soalloc(int waitok); 382int sobind(struct socket *so, struct sockaddr *nam, struct thread *td); 383void socantrcvmore(struct socket *so); 384void socantsendmore(struct socket *so); 385int soclose(struct socket *so); 386int soconnect(struct socket *so, struct sockaddr *nam, struct thread *td); 387int soconnect2(struct socket *so1, struct socket *so2); 388int socow_setup(struct mbuf *m0, struct uio *uio); 389int socreate(int dom, struct socket **aso, int type, int proto, 390 struct ucred *cred, struct thread *td); 391void sodealloc(struct socket *so); 392int sodisconnect(struct socket *so); 393void sofree(struct socket *so); 394int sogetopt(struct socket *so, struct sockopt *sopt); 395void sohasoutofband(struct socket *so); 396void soisconnected(struct socket *so); 397void soisconnecting(struct socket *so); 398void soisdisconnected(struct socket *so); 399void soisdisconnecting(struct socket *so); 400int solisten(struct socket *so, int backlog, struct thread *td); 401struct socket * 402 sonewconn(struct socket *head, int connstatus); 403int sooptcopyin(struct sockopt *sopt, void *buf, size_t len, size_t minlen); 404int sooptcopyout(struct sockopt *sopt, const void *buf, size_t len); 405 406/* XXX; prepare mbuf for (__FreeBSD__ < 3) routines. */ 407int soopt_getm(struct sockopt *sopt, struct mbuf **mp); 408int soopt_mcopyin(struct sockopt *sopt, struct mbuf *m); 409int soopt_mcopyout(struct sockopt *sopt, struct mbuf *m); 410 411int sopoll(struct socket *so, int events, struct ucred *active_cred, 412 struct thread *td); 413int soreceive(struct socket *so, struct sockaddr **paddr, struct uio *uio, 414 struct mbuf **mp0, struct mbuf **controlp, int *flagsp); 415int soreserve(struct socket *so, u_long sndcc, u_long rcvcc); 416void sorflush(struct socket *so); 417int sosend(struct socket *so, struct sockaddr *addr, struct uio *uio, 418 struct mbuf *top, struct mbuf *control, int flags, 419 struct thread *td); 420int sosetopt(struct socket *so, struct sockopt *sopt); 421int soshutdown(struct socket *so, int how); 422void sotoxsocket(struct socket *so, struct xsocket *xso); 423void sowakeup(struct socket *so, struct sockbuf *sb); 424 425/* 426 * Accept filter functions (duh). 427 */ 428int accept_filt_add(struct accept_filter *filt); 429int accept_filt_del(char *name); 430struct accept_filter *accept_filt_get(char *name); 431#ifdef ACCEPT_FILTER_MOD 432#ifdef SYSCTL_DECL 433SYSCTL_DECL(_net_inet_accf); 434#endif 435int accept_filt_generic_mod_event(module_t mod, int event, void *data); 436#endif 437 438#endif /* _KERNEL */ 439 440#endif /* !_SYS_SOCKETVAR_H_ */ 441