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