socketvar.h revision 129958
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 129958 2004-06-01 19:33:06Z 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; /* 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; /* back pointer to accept socket */ 78 TAILQ_HEAD(, socket) so_incomp; /* queue of partial unaccepted connections */ 79 TAILQ_HEAD(, socket) so_comp; /* queue of complete unaccepted connections */ 80 TAILQ_ENTRY(socket) so_list; /* list of unaccepted connections */ 81 short so_qlen; /* number of unaccepted connections */ 82 short so_incqlen; /* number of unaccepted incomplete 83 connections */ 84 short so_qlimit; /* 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 * Socket state bits. 145 */ 146#define SS_NOFDREF 0x0001 /* no file table ref any more */ 147#define SS_ISCONNECTED 0x0002 /* socket connected to a peer */ 148#define SS_ISCONNECTING 0x0004 /* in process of connecting to peer */ 149#define SS_ISDISCONNECTING 0x0008 /* in process of disconnecting */ 150#define SS_CANTSENDMORE 0x0010 /* can't send more data to peer */ 151#define SS_CANTRCVMORE 0x0020 /* can't receive more data from peer */ 152#define SS_RCVATMARK 0x0040 /* at mark on input */ 153 154#define SS_NBIO 0x0100 /* non-blocking ops */ 155#define SS_ASYNC 0x0200 /* async i/o notify */ 156#define SS_ISCONFIRMING 0x0400 /* deciding to accept connection req */ 157 158#define SS_ISDISCONNECTED 0x2000 /* socket disconnected from peer */ 159 160/* 161 * Socket state bits stored in so_qstate. 162 */ 163#define SQ_INCOMP 0x0800 /* unaccepted, incomplete connection */ 164#define SQ_COMP 0x1000 /* unaccepted, complete connection */ 165 166/* 167 * Externalized form of struct socket used by the sysctl(3) interface. 168 */ 169struct xsocket { 170 size_t xso_len; /* length of this structure */ 171 struct socket *xso_so; /* makes a convenient handle sometimes */ 172 short so_type; 173 short so_options; 174 short so_linger; 175 short so_state; 176 caddr_t so_pcb; /* another convenient handle */ 177 int xso_protocol; 178 int xso_family; 179 short so_qlen; 180 short so_incqlen; 181 short so_qlimit; 182 short so_timeo; 183 u_short so_error; 184 pid_t so_pgid; 185 u_long so_oobmark; 186 struct xsockbuf { 187 u_int sb_cc; 188 u_int sb_hiwat; 189 u_int sb_mbcnt; 190 u_int sb_mbmax; 191 int sb_lowat; 192 int sb_timeo; 193 short sb_flags; 194 } so_rcv, so_snd; 195 uid_t so_uid; /* XXX */ 196}; 197 198/* 199 * Macros for sockets and socket buffering. 200 */ 201 202/* 203 * Do we need to notify the other side when I/O is possible? 204 */ 205#define sb_notify(sb) (((sb)->sb_flags & (SB_WAIT | SB_SEL | SB_ASYNC | \ 206 SB_UPCALL | SB_AIO | SB_KNOTE)) != 0) 207 208/* 209 * How much space is there in a socket buffer (so->so_snd or so->so_rcv)? 210 * This is problematical if the fields are unsigned, as the space might 211 * still be negative (cc > hiwat or mbcnt > mbmax). Should detect 212 * overflow and return 0. Should use "lmin" but it doesn't exist now. 213 */ 214#define sbspace(sb) \ 215 ((long) imin((int)((sb)->sb_hiwat - (sb)->sb_cc), \ 216 (int)((sb)->sb_mbmax - (sb)->sb_mbcnt))) 217 218/* do we have to send all at once on a socket? */ 219#define sosendallatonce(so) \ 220 ((so)->so_proto->pr_flags & PR_ATOMIC) 221 222/* can we read something from so? */ 223#define soreadable(so) \ 224 ((so)->so_rcv.sb_cc >= (so)->so_rcv.sb_lowat || \ 225 ((so)->so_state & SS_CANTRCVMORE) || \ 226 !TAILQ_EMPTY(&(so)->so_comp) || (so)->so_error) 227 228/* can we write something to so? */ 229#define sowriteable(so) \ 230 ((sbspace(&(so)->so_snd) >= (so)->so_snd.sb_lowat && \ 231 (((so)->so_state&SS_ISCONNECTED) || \ 232 ((so)->so_proto->pr_flags&PR_CONNREQUIRED)==0)) || \ 233 ((so)->so_state & SS_CANTSENDMORE) || \ 234 (so)->so_error) 235 236/* adjust counters in sb reflecting allocation of m */ 237#define sballoc(sb, m) { \ 238 (sb)->sb_cc += (m)->m_len; \ 239 if ((m)->m_type != MT_DATA && (m)->m_type != MT_HEADER && \ 240 (m)->m_type != MT_OOBDATA) \ 241 (sb)->sb_ctl += (m)->m_len; \ 242 (sb)->sb_mbcnt += MSIZE; \ 243 if ((m)->m_flags & M_EXT) \ 244 (sb)->sb_mbcnt += (m)->m_ext.ext_size; \ 245} 246 247/* adjust counters in sb reflecting freeing of m */ 248#define sbfree(sb, m) { \ 249 (sb)->sb_cc -= (m)->m_len; \ 250 if ((m)->m_type != MT_DATA && (m)->m_type != MT_HEADER && \ 251 (m)->m_type != MT_OOBDATA) \ 252 (sb)->sb_ctl -= (m)->m_len; \ 253 (sb)->sb_mbcnt -= MSIZE; \ 254 if ((m)->m_flags & M_EXT) \ 255 (sb)->sb_mbcnt -= (m)->m_ext.ext_size; \ 256} 257 258/* 259 * Set lock on sockbuf sb; sleep if lock is already held. 260 * Unless SB_NOINTR is set on sockbuf, sleep is interruptible. 261 * Returns error without lock if sleep is interrupted. 262 */ 263#define sblock(sb, wf) ((sb)->sb_flags & SB_LOCK ? \ 264 (((wf) == M_WAITOK) ? sb_lock(sb) : EWOULDBLOCK) : \ 265 ((sb)->sb_flags |= SB_LOCK), 0) 266 267/* release lock on sockbuf sb */ 268#define sbunlock(sb) { \ 269 (sb)->sb_flags &= ~SB_LOCK; \ 270 if ((sb)->sb_flags & SB_WANT) { \ 271 (sb)->sb_flags &= ~SB_WANT; \ 272 wakeup(&(sb)->sb_flags); \ 273 } \ 274} 275 276/* 277 * soref()/sorele() ref-count the socket structure. Note that you must 278 * still explicitly close the socket, but the last ref count will free 279 * the structure. 280 */ 281#define soref(so) do { \ 282 ++(so)->so_count; \ 283 } while (0) 284 285#define sorele(so) do { \ 286 if ((so)->so_count <= 0) \ 287 panic("sorele");\ 288 if (--(so)->so_count == 0)\ 289 sofree(so); \ 290 } while (0) 291 292#define sotryfree(so) do { \ 293 if ((so)->so_count == 0) \ 294 sofree(so); \ 295 } while(0) 296 297#define sorwakeup(so) do { \ 298 if (sb_notify(&(so)->so_rcv)) \ 299 sowakeup((so), &(so)->so_rcv); \ 300 } while (0) 301 302#define sowwakeup(so) do { \ 303 if (sb_notify(&(so)->so_snd)) \ 304 sowakeup((so), &(so)->so_snd); \ 305 } while (0) 306 307#ifdef _KERNEL 308 309/* 310 * Argument structure for sosetopt et seq. This is in the KERNEL 311 * section because it will never be visible to user code. 312 */ 313enum sopt_dir { SOPT_GET, SOPT_SET }; 314struct sockopt { 315 enum sopt_dir sopt_dir; /* is this a get or a set? */ 316 int sopt_level; /* second arg of [gs]etsockopt */ 317 int sopt_name; /* third arg of [gs]etsockopt */ 318 void *sopt_val; /* fourth arg of [gs]etsockopt */ 319 size_t sopt_valsize; /* (almost) fifth arg of [gs]etsockopt */ 320 struct thread *sopt_td; /* calling thread or null if kernel */ 321}; 322 323struct accept_filter { 324 char accf_name[16]; 325 void (*accf_callback) 326 (struct socket *so, void *arg, int waitflag); 327 void * (*accf_create) 328 (struct socket *so, char *arg); 329 void (*accf_destroy) 330 (struct socket *so); 331 SLIST_ENTRY(accept_filter) accf_next; 332}; 333 334#ifdef MALLOC_DECLARE 335MALLOC_DECLARE(M_ACCF); 336MALLOC_DECLARE(M_PCB); 337MALLOC_DECLARE(M_SONAME); 338#endif 339 340extern int maxsockets; 341extern u_long sb_max; 342extern struct uma_zone *socket_zone; 343extern so_gen_t so_gencnt; 344 345struct mbuf; 346struct sockaddr; 347struct ucred; 348struct uio; 349 350/* 351 * From uipc_socket and friends 352 */ 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); 356void sbappendstream(struct sockbuf *sb, struct mbuf *m); 357int sbappendaddr(struct sockbuf *sb, struct sockaddr *asa, 358 struct mbuf *m0, struct mbuf *control); 359int sbappendcontrol(struct sockbuf *sb, struct mbuf *m0, 360 struct mbuf *control); 361void sbappendrecord(struct sockbuf *sb, struct mbuf *m0); 362void sbcheck(struct sockbuf *sb); 363void sbcompress(struct sockbuf *sb, struct mbuf *m, struct mbuf *n); 364struct mbuf * 365 sbcreatecontrol(caddr_t p, int size, int type, int level); 366void sbdrop(struct sockbuf *sb, int len); 367void sbdroprecord(struct sockbuf *sb); 368void sbflush(struct sockbuf *sb); 369void sbinsertoob(struct sockbuf *sb, struct mbuf *m0); 370void sbrelease(struct sockbuf *sb, struct socket *so); 371int sbreserve(struct sockbuf *sb, u_long cc, struct socket *so, 372 struct thread *td); 373void sbtoxsockbuf(struct sockbuf *sb, struct xsockbuf *xsb); 374int sbwait(struct sockbuf *sb); 375int sb_lock(struct sockbuf *sb); 376int soabort(struct socket *so); 377int soaccept(struct socket *so, struct sockaddr **nam); 378struct socket *soalloc(int mflags); 379int socheckuid(struct socket *so, uid_t uid); 380int sobind(struct socket *so, struct sockaddr *nam, struct thread *td); 381void socantrcvmore(struct socket *so); 382void socantsendmore(struct socket *so); 383int soclose(struct socket *so); 384int soconnect(struct socket *so, struct sockaddr *nam, struct thread *td); 385int soconnect2(struct socket *so1, struct socket *so2); 386int socow_setup(struct mbuf *m0, struct uio *uio); 387int socreate(int dom, struct socket **aso, int type, int proto, 388 struct ucred *cred, struct thread *td); 389void sodealloc(struct socket *so); 390int sodisconnect(struct socket *so); 391struct sockaddr *sodupsockaddr(const struct sockaddr *sa, int mflags); 392void sofree(struct socket *so); 393int sogetopt(struct socket *so, struct sockopt *sopt); 394void sohasoutofband(struct socket *so); 395void soisconnected(struct socket *so); 396void soisconnecting(struct socket *so); 397void soisdisconnected(struct socket *so); 398void soisdisconnecting(struct socket *so); 399int solisten(struct socket *so, int backlog, struct thread *td); 400struct socket * 401 sonewconn(struct socket *head, int connstatus); 402int sooptcopyin(struct sockopt *sopt, void *buf, size_t len, size_t minlen); 403int sooptcopyout(struct sockopt *sopt, const void *buf, size_t len); 404 405/* XXX; prepare mbuf for (__FreeBSD__ < 3) routines. */ 406int soopt_getm(struct sockopt *sopt, struct mbuf **mp); 407int soopt_mcopyin(struct sockopt *sopt, struct mbuf *m); 408int soopt_mcopyout(struct sockopt *sopt, struct mbuf *m); 409 410int sopoll(struct socket *so, int events, struct ucred *active_cred, 411 struct thread *td); 412int soreceive(struct socket *so, struct sockaddr **paddr, struct uio *uio, 413 struct mbuf **mp0, struct mbuf **controlp, int *flagsp); 414int soreserve(struct socket *so, u_long sndcc, u_long rcvcc); 415void sorflush(struct socket *so); 416int sosend(struct socket *so, struct sockaddr *addr, struct uio *uio, 417 struct mbuf *top, struct mbuf *control, int flags, 418 struct thread *td); 419int sosetopt(struct socket *so, struct sockopt *sopt); 420int soshutdown(struct socket *so, int how); 421void sotoxsocket(struct socket *so, struct xsocket *xso); 422void sowakeup(struct socket *so, struct sockbuf *sb); 423 424#ifdef SOCKBUF_DEBUG 425void sblastrecordchk(struct sockbuf *, const char *, int); 426#define SBLASTRECORDCHK(sb) sblastrecordchk((sb), __FILE__, __LINE__) 427 428void sblastmbufchk(struct sockbuf *, const char *, int); 429#define SBLASTMBUFCHK(sb) sblastmbufchk((sb), __FILE__, __LINE__) 430#else 431#define SBLASTRECORDCHK(sb) /* nothing */ 432#define SBLASTMBUFCHK(sb) /* nothing */ 433#endif /* SOCKBUF_DEBUG */ 434 435/* 436 * Accept filter functions (duh). 437 */ 438int accept_filt_add(struct accept_filter *filt); 439int accept_filt_del(char *name); 440struct accept_filter *accept_filt_get(char *name); 441#ifdef ACCEPT_FILTER_MOD 442#ifdef SYSCTL_DECL 443SYSCTL_DECL(_net_inet_accf); 444#endif 445int accept_filt_generic_mod_event(module_t mod, int event, void *data); 446#endif 447 448#endif /* _KERNEL */ 449 450#endif /* !_SYS_SOCKETVAR_H_ */ 451