socketvar.h revision 126425
111375Ssherman/*- 211375Ssherman * Copyright (c) 1982, 1986, 1990, 1993 311375Ssherman * The Regents of the University of California. All rights reserved. 411375Ssherman * 511375Ssherman * Redistribution and use in source and binary forms, with or without 611375Ssherman * modification, are permitted provided that the following conditions 711375Ssherman * are met: 811375Ssherman * 1. Redistributions of source code must retain the above copyright 911375Ssherman * notice, this list of conditions and the following disclaimer. 1011375Ssherman * 2. Redistributions in binary form must reproduce the above copyright 1111375Ssherman * notice, this list of conditions and the following disclaimer in the 1211375Ssherman * documentation and/or other materials provided with the distribution. 1311375Ssherman * 3. All advertising materials mentioning features or use of this software 1411375Ssherman * must display the following acknowledgement: 1511375Ssherman * This product includes software developed by the University of 1611375Ssherman * California, Berkeley and its contributors. 1711375Ssherman * 4. Neither the name of the University nor the names of its contributors 1811375Ssherman * may be used to endorse or promote products derived from this software 1911375Ssherman * without specific prior written permission. 2011375Ssherman * 2111375Ssherman * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2211375Ssherman * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2311375Ssherman * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2411375Ssherman * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2511375Ssherman * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2611375Ssherman * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2711375Ssherman * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2811375Ssherman * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2911375Ssherman * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3011375Ssherman * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3111375Ssherman * SUCH DAMAGE. 3211375Ssherman * 3311375Ssherman * @(#)socketvar.h 8.3 (Berkeley) 2/19/95 3411375Ssherman * $FreeBSD: head/sys/sys/socketvar.h 126425 2004-03-01 03:14:23Z rwatson $ 3511375Ssherman */ 3611375Ssherman 3711375Ssherman#ifndef _SYS_SOCKETVAR_H_ 3811375Ssherman#define _SYS_SOCKETVAR_H_ 3911375Ssherman 4011375Ssherman#include <sys/queue.h> /* for TAILQ macros */ 4111375Ssherman#include <sys/selinfo.h> /* for struct selinfo */ 4211375Ssherman 4311375Ssherman/* 4411375Ssherman * Kernel structure per socket. 4511375Ssherman * Contains send and receive buffer queues, 4611375Ssherman * handle on protocol and pointer to protocol 4711375Ssherman * private data and error information. 4811375Ssherman */ 4911375Sshermantypedef u_quad_t so_gen_t; 5011375Ssherman 5111375Ssherman/* 5211375Ssherman * List of locks: 5311375Ssherman * (c) const, inited in either socreate() or sonewconn() 5411375Ssherman * (m) sb_mtx mutex 5511375Ssherman * (mr) so_rcv.sb_mtx mutex 5611375Ssherman * (sg) sigio_lock sx 5711375Ssherman * (sh) sohead_lock sx 5811375Ssherman * 5911375Ssherman * Lock of so_rcv.sb_mtx can duplicate, provided that sohead_lock 6011375Ssherman * is exclusively locked. 6111375Ssherman * 6211375Ssherman * Brackets mean that this data is not protected yet. 6311375Ssherman */ 6411375Sshermanstruct socket { 6511375Ssherman int so_count; /* reference count */ 6611375Ssherman short so_type; /* generic type, see socket.h */ 6711375Ssherman short so_options; /* from socket call, see socket.h */ 6811375Ssherman short so_linger; /* time to linger while closing */ 6911375Ssherman short so_state; /* internal state flags SS_*, below */ 7011375Ssherman void *so_pcb; /* protocol control block */ 7111375Ssherman struct protosw *so_proto; /* protocol handle */ 7211375Ssherman/* 7311375Ssherman * Variables for connection queuing. 7411375Ssherman * Socket where accepts occur is so_head in all subsidiary sockets. 7511375Ssherman * If so_head is 0, socket is not related to an accept. 7611375Ssherman * For head socket so_incomp queues partially completed connections, 7711375Ssherman * while so_comp is a queue of connections ready to be accepted. 7811375Ssherman * If a connection is aborted and it has so_head set, then 7911375Ssherman * it has to be pulled out of either so_incomp or so_comp. 8011375Ssherman * We allow connections to queue up based on current queue lengths 8111375Ssherman * and limit on number of queued connections for this socket. 8211375Ssherman */ 8311375Ssherman struct socket *so_head; /* back pointer to accept socket */ 8411375Ssherman TAILQ_HEAD(, socket) so_incomp; /* queue of partial unaccepted connections */ 8511375Ssherman TAILQ_HEAD(, socket) so_comp; /* queue of complete unaccepted connections */ 8611375Ssherman TAILQ_ENTRY(socket) so_list; /* list of unaccepted connections */ 8711375Ssherman short so_qlen; /* number of unaccepted connections */ 8811375Ssherman short so_incqlen; /* number of unaccepted incomplete 8911375Ssherman connections */ 9011375Ssherman short so_qlimit; /* max number queued connections */ 91 short so_timeo; /* connection timeout */ 92 u_short so_error; /* error affecting connection */ 93 struct sigio *so_sigio; /* [sg] information for async I/O or 94 out of band data (SIGURG) */ 95 u_long so_oobmark; /* chars to oob mark */ 96 TAILQ_HEAD(, aiocblist) so_aiojobq; /* AIO ops waiting on socket */ 97/* 98 * Variables for socket buffering. 99 */ 100 struct sockbuf { 101 struct selinfo sb_sel; /* process selecting read/write */ 102#define sb_startzero sb_mb 103 struct mbuf *sb_mb; /* the mbuf chain */ 104 struct mbuf *sb_mbtail; /* the last mbuf in the chain */ 105 struct mbuf *sb_lastrecord; /* first mbuf of last record in 106 * socket buffer */ 107 u_int sb_cc; /* actual chars in buffer */ 108 u_int sb_hiwat; /* max actual char count */ 109 u_int sb_mbcnt; /* chars of mbufs used */ 110 u_int sb_mbmax; /* max chars of mbufs to use */ 111 u_int sb_ctl; /* non-data chars in buffer */ 112 int sb_lowat; /* low water mark */ 113 int sb_timeo; /* timeout for read/write */ 114 short sb_flags; /* flags, see below */ 115 } so_rcv, so_snd; 116#define SB_MAX (256*1024) /* default for max chars in sockbuf */ 117#define SB_LOCK 0x01 /* lock on data queue */ 118#define SB_WANT 0x02 /* someone is waiting to lock */ 119#define SB_WAIT 0x04 /* someone is waiting for data/space */ 120#define SB_SEL 0x08 /* someone is selecting */ 121#define SB_ASYNC 0x10 /* ASYNC I/O, need signals */ 122#define SB_UPCALL 0x20 /* someone wants an upcall */ 123#define SB_NOINTR 0x40 /* operations not interruptible */ 124#define SB_AIO 0x80 /* AIO operations queued */ 125#define SB_KNOTE 0x100 /* kernel note attached */ 126 127 void (*so_upcall)(struct socket *, void *, int); 128 void *so_upcallarg; 129 struct ucred *so_cred; /* user credentials */ 130 struct label *so_label; /* MAC label for socket */ 131 struct label *so_peerlabel; /* cached MAC label for socket peer */ 132 /* NB: generation count must not be first; easiest to make it last. */ 133 so_gen_t so_gencnt; /* generation count */ 134 void *so_emuldata; /* private data for emulators */ 135 struct so_accf { 136 struct accept_filter *so_accept_filter; 137 void *so_accept_filter_arg; /* saved filter args */ 138 char *so_accept_filter_str; /* saved user args */ 139 } *so_accf; 140}; 141 142#define SB_EMPTY_FIXUP(sb) do { \ 143 if ((sb)->sb_mb == NULL) { \ 144 (sb)->sb_mbtail = NULL; \ 145 (sb)->sb_lastrecord = NULL; \ 146 } \ 147} while (/*CONSTCOND*/0) 148 149/* 150 * Socket state bits. 151 */ 152#define SS_NOFDREF 0x0001 /* no file table ref any more */ 153#define SS_ISCONNECTED 0x0002 /* socket connected to a peer */ 154#define SS_ISCONNECTING 0x0004 /* in process of connecting to peer */ 155#define SS_ISDISCONNECTING 0x0008 /* in process of disconnecting */ 156#define SS_CANTSENDMORE 0x0010 /* can't send more data to peer */ 157#define SS_CANTRCVMORE 0x0020 /* can't receive more data from peer */ 158#define SS_RCVATMARK 0x0040 /* at mark on input */ 159 160#define SS_NBIO 0x0100 /* non-blocking ops */ 161#define SS_ASYNC 0x0200 /* async i/o notify */ 162#define SS_ISCONFIRMING 0x0400 /* deciding to accept connection req */ 163 164#define SS_INCOMP 0x0800 /* unaccepted, incomplete connection */ 165#define SS_COMP 0x1000 /* unaccepted, complete connection */ 166#define SS_ISDISCONNECTED 0x2000 /* socket disconnected from peer */ 167 168/* 169 * Externalized form of struct socket used by the sysctl(3) interface. 170 */ 171struct xsocket { 172 size_t xso_len; /* length of this structure */ 173 struct socket *xso_so; /* makes a convenient handle sometimes */ 174 short so_type; 175 short so_options; 176 short so_linger; 177 short so_state; 178 caddr_t so_pcb; /* another convenient handle */ 179 int xso_protocol; 180 int xso_family; 181 short so_qlen; 182 short so_incqlen; 183 short so_qlimit; 184 short so_timeo; 185 u_short so_error; 186 pid_t so_pgid; 187 u_long so_oobmark; 188 struct xsockbuf { 189 u_int sb_cc; 190 u_int sb_hiwat; 191 u_int sb_mbcnt; 192 u_int sb_mbmax; 193 int sb_lowat; 194 int sb_timeo; 195 short sb_flags; 196 } so_rcv, so_snd; 197 uid_t so_uid; /* XXX */ 198}; 199 200/* 201 * Macros for sockets and socket buffering. 202 */ 203 204/* 205 * Do we need to notify the other side when I/O is possible? 206 */ 207#define sb_notify(sb) (((sb)->sb_flags & (SB_WAIT | SB_SEL | SB_ASYNC | \ 208 SB_UPCALL | SB_AIO | SB_KNOTE)) != 0) 209 210/* 211 * How much space is there in a socket buffer (so->so_snd or so->so_rcv)? 212 * This is problematical if the fields are unsigned, as the space might 213 * still be negative (cc > hiwat or mbcnt > mbmax). Should detect 214 * overflow and return 0. Should use "lmin" but it doesn't exist now. 215 */ 216#define sbspace(sb) \ 217 ((long) imin((int)((sb)->sb_hiwat - (sb)->sb_cc), \ 218 (int)((sb)->sb_mbmax - (sb)->sb_mbcnt))) 219 220/* do we have to send all at once on a socket? */ 221#define sosendallatonce(so) \ 222 ((so)->so_proto->pr_flags & PR_ATOMIC) 223 224/* can we read something from so? */ 225#define soreadable(so) \ 226 ((so)->so_rcv.sb_cc >= (so)->so_rcv.sb_lowat || \ 227 ((so)->so_state & SS_CANTRCVMORE) || \ 228 !TAILQ_EMPTY(&(so)->so_comp) || (so)->so_error) 229 230/* can we write something to so? */ 231#define sowriteable(so) \ 232 ((sbspace(&(so)->so_snd) >= (so)->so_snd.sb_lowat && \ 233 (((so)->so_state&SS_ISCONNECTED) || \ 234 ((so)->so_proto->pr_flags&PR_CONNREQUIRED)==0)) || \ 235 ((so)->so_state & SS_CANTSENDMORE) || \ 236 (so)->so_error) 237 238/* adjust counters in sb reflecting allocation of m */ 239#define sballoc(sb, m) { \ 240 (sb)->sb_cc += (m)->m_len; \ 241 if ((m)->m_type != MT_DATA && (m)->m_type != MT_HEADER && \ 242 (m)->m_type != MT_OOBDATA) \ 243 (sb)->sb_ctl += (m)->m_len; \ 244 (sb)->sb_mbcnt += MSIZE; \ 245 if ((m)->m_flags & M_EXT) \ 246 (sb)->sb_mbcnt += (m)->m_ext.ext_size; \ 247} 248 249/* adjust counters in sb reflecting freeing of m */ 250#define sbfree(sb, m) { \ 251 (sb)->sb_cc -= (m)->m_len; \ 252 if ((m)->m_type != MT_DATA && (m)->m_type != MT_HEADER && \ 253 (m)->m_type != MT_OOBDATA) \ 254 (sb)->sb_ctl -= (m)->m_len; \ 255 (sb)->sb_mbcnt -= MSIZE; \ 256 if ((m)->m_flags & M_EXT) \ 257 (sb)->sb_mbcnt -= (m)->m_ext.ext_size; \ 258} 259 260/* 261 * Set lock on sockbuf sb; sleep if lock is already held. 262 * Unless SB_NOINTR is set on sockbuf, sleep is interruptible. 263 * Returns error without lock if sleep is interrupted. 264 */ 265#define sblock(sb, wf) ((sb)->sb_flags & SB_LOCK ? \ 266 (((wf) == M_WAITOK) ? sb_lock(sb) : EWOULDBLOCK) : \ 267 ((sb)->sb_flags |= SB_LOCK), 0) 268 269/* release lock on sockbuf sb */ 270#define sbunlock(sb) { \ 271 (sb)->sb_flags &= ~SB_LOCK; \ 272 if ((sb)->sb_flags & SB_WANT) { \ 273 (sb)->sb_flags &= ~SB_WANT; \ 274 wakeup(&(sb)->sb_flags); \ 275 } \ 276} 277 278/* 279 * soref()/sorele() ref-count the socket structure. Note that you must 280 * still explicitly close the socket, but the last ref count will free 281 * the structure. 282 */ 283#define soref(so) do { \ 284 ++(so)->so_count; \ 285 } while (0) 286 287#define sorele(so) do { \ 288 if ((so)->so_count <= 0) \ 289 panic("sorele");\ 290 if (--(so)->so_count == 0)\ 291 sofree(so); \ 292 } while (0) 293 294#define sotryfree(so) do { \ 295 if ((so)->so_count == 0) \ 296 sofree(so); \ 297 } while(0) 298 299#define sorwakeup(so) do { \ 300 if (sb_notify(&(so)->so_rcv)) \ 301 sowakeup((so), &(so)->so_rcv); \ 302 } while (0) 303 304#define sowwakeup(so) do { \ 305 if (sb_notify(&(so)->so_snd)) \ 306 sowakeup((so), &(so)->so_snd); \ 307 } while (0) 308 309#ifdef _KERNEL 310 311/* 312 * Argument structure for sosetopt et seq. This is in the KERNEL 313 * section because it will never be visible to user code. 314 */ 315enum sopt_dir { SOPT_GET, SOPT_SET }; 316struct sockopt { 317 enum sopt_dir sopt_dir; /* is this a get or a set? */ 318 int sopt_level; /* second arg of [gs]etsockopt */ 319 int sopt_name; /* third arg of [gs]etsockopt */ 320 void *sopt_val; /* fourth arg of [gs]etsockopt */ 321 size_t sopt_valsize; /* (almost) fifth arg of [gs]etsockopt */ 322 struct thread *sopt_td; /* calling thread or null if kernel */ 323}; 324 325struct accept_filter { 326 char accf_name[16]; 327 void (*accf_callback) 328 (struct socket *so, void *arg, int waitflag); 329 void * (*accf_create) 330 (struct socket *so, char *arg); 331 void (*accf_destroy) 332 (struct socket *so); 333 SLIST_ENTRY(accept_filter) accf_next; 334}; 335 336#ifdef MALLOC_DECLARE 337MALLOC_DECLARE(M_ACCF); 338MALLOC_DECLARE(M_PCB); 339MALLOC_DECLARE(M_SONAME); 340#endif 341 342extern int maxsockets; 343extern u_long sb_max; 344extern struct uma_zone *socket_zone; 345extern so_gen_t so_gencnt; 346 347struct mbuf; 348struct sockaddr; 349struct ucred; 350struct uio; 351 352/* 353 * From uipc_socket and friends 354 */ 355int sockargs(struct mbuf **mp, caddr_t buf, int buflen, int type); 356int getsockaddr(struct sockaddr **namp, caddr_t uaddr, size_t len); 357void sbappend(struct sockbuf *sb, struct mbuf *m); 358void sbappendstream(struct sockbuf *sb, struct mbuf *m); 359int sbappendaddr(struct sockbuf *sb, struct sockaddr *asa, 360 struct mbuf *m0, struct mbuf *control); 361int sbappendcontrol(struct sockbuf *sb, struct mbuf *m0, 362 struct mbuf *control); 363void sbappendrecord(struct sockbuf *sb, struct mbuf *m0); 364void sbcheck(struct sockbuf *sb); 365void sbcompress(struct sockbuf *sb, struct mbuf *m, struct mbuf *n); 366struct mbuf * 367 sbcreatecontrol(caddr_t p, int size, int type, int level); 368void sbdrop(struct sockbuf *sb, int len); 369void sbdroprecord(struct sockbuf *sb); 370void sbflush(struct sockbuf *sb); 371void sbinsertoob(struct sockbuf *sb, struct mbuf *m0); 372void sbrelease(struct sockbuf *sb, struct socket *so); 373int sbreserve(struct sockbuf *sb, u_long cc, struct socket *so, 374 struct thread *td); 375void sbtoxsockbuf(struct sockbuf *sb, struct xsockbuf *xsb); 376int sbwait(struct sockbuf *sb); 377int sb_lock(struct sockbuf *sb); 378int soabort(struct socket *so); 379int soaccept(struct socket *so, struct sockaddr **nam); 380struct socket *soalloc(int mflags); 381int socheckuid(struct socket *so, uid_t uid); 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); 393struct sockaddr *sodupsockaddr(const struct sockaddr *sa, int mflags); 394void sofree(struct socket *so); 395int sogetopt(struct socket *so, struct sockopt *sopt); 396void sohasoutofband(struct socket *so); 397void soisconnected(struct socket *so); 398void soisconnecting(struct socket *so); 399void soisdisconnected(struct socket *so); 400void soisdisconnecting(struct socket *so); 401int solisten(struct socket *so, int backlog, struct thread *td); 402struct socket * 403 sonewconn(struct socket *head, int connstatus); 404int sooptcopyin(struct sockopt *sopt, void *buf, size_t len, size_t minlen); 405int sooptcopyout(struct sockopt *sopt, const void *buf, size_t len); 406 407/* XXX; prepare mbuf for (__FreeBSD__ < 3) routines. */ 408int soopt_getm(struct sockopt *sopt, struct mbuf **mp); 409int soopt_mcopyin(struct sockopt *sopt, struct mbuf *m); 410int soopt_mcopyout(struct sockopt *sopt, struct mbuf *m); 411 412int sopoll(struct socket *so, int events, struct ucred *active_cred, 413 struct thread *td); 414int soreceive(struct socket *so, struct sockaddr **paddr, struct uio *uio, 415 struct mbuf **mp0, struct mbuf **controlp, int *flagsp); 416int soreserve(struct socket *so, u_long sndcc, u_long rcvcc); 417void sorflush(struct socket *so); 418int sosend(struct socket *so, struct sockaddr *addr, struct uio *uio, 419 struct mbuf *top, struct mbuf *control, int flags, 420 struct thread *td); 421int sosetopt(struct socket *so, struct sockopt *sopt); 422int soshutdown(struct socket *so, int how); 423void sotoxsocket(struct socket *so, struct xsocket *xso); 424void sowakeup(struct socket *so, struct sockbuf *sb); 425 426#ifdef SOCKBUF_DEBUG 427void sblastrecordchk(struct sockbuf *, const char *, int); 428#define SBLASTRECORDCHK(sb) sblastrecordchk((sb), __FILE__, __LINE__) 429 430void sblastmbufchk(struct sockbuf *, const char *, int); 431#define SBLASTMBUFCHK(sb) sblastmbufchk((sb), __FILE__, __LINE__) 432#else 433#define SBLASTRECORDCHK(sb) /* nothing */ 434#define SBLASTMBUFCHK(sb) /* nothing */ 435#endif /* SOCKBUF_DEBUG */ 436 437/* 438 * Accept filter functions (duh). 439 */ 440int accept_filt_add(struct accept_filter *filt); 441int accept_filt_del(char *name); 442struct accept_filter *accept_filt_get(char *name); 443#ifdef ACCEPT_FILTER_MOD 444#ifdef SYSCTL_DECL 445SYSCTL_DECL(_net_inet_accf); 446#endif 447int accept_filt_generic_mod_event(module_t mod, int event, void *data); 448#endif 449 450#endif /* _KERNEL */ 451 452#endif /* !_SYS_SOCKETVAR_H_ */ 453