socketvar.h revision 92719
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 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * @(#)socketvar.h 8.3 (Berkeley) 2/19/95 34 * $FreeBSD: head/sys/sys/socketvar.h 92719 2002-03-19 20:18:42Z alfred $ 35 */ 36 37#ifndef _SYS_SOCKETVAR_H_ 38#define _SYS_SOCKETVAR_H_ 39 40#include <sys/queue.h> /* for TAILQ macros */ 41#include <sys/sx.h> /* SX locks */ 42#include <sys/selinfo.h> /* for struct selinfo */ 43#include <vm/vm_zone.h> 44 45/* 46 * Kernel structure per socket. 47 * Contains send and receive buffer queues, 48 * handle on protocol and pointer to protocol 49 * private data and error information. 50 */ 51typedef u_quad_t so_gen_t; 52 53struct accept_filter; 54 55struct socket { 56 vm_zone_t so_zone; /* zone we were allocated from */ 57 int so_count; /* reference count */ 58 short so_type; /* generic type, see socket.h */ 59 short so_options; /* from socket call, see socket.h */ 60 short so_linger; /* time to linger while closing */ 61 short so_state; /* internal state flags SS_*, below */ 62 caddr_t so_pcb; /* protocol control block */ 63 struct protosw *so_proto; /* protocol handle */ 64/* 65 * Variables for connection queuing. 66 * Socket where accepts occur is so_head in all subsidiary sockets. 67 * If so_head is 0, socket is not related to an accept. 68 * For head socket so_incomp queues partially completed connections, 69 * while so_comp is a queue of connections ready to be accepted. 70 * If a connection is aborted and it has so_head set, then 71 * it has to be pulled out of either so_incomp or so_comp. 72 * We allow connections to queue up based on current queue lengths 73 * and limit on number of queued connections for this socket. 74 */ 75 struct socket *so_head; /* back pointer to accept socket */ 76 TAILQ_HEAD(, socket) so_incomp; /* queue of partial unaccepted connections */ 77 TAILQ_HEAD(, socket) so_comp; /* queue of complete unaccepted connections */ 78 TAILQ_ENTRY(socket) so_list; /* list of unaccepted connections */ 79 short so_qlen; /* number of unaccepted connections */ 80 short so_incqlen; /* number of unaccepted incomplete 81 connections */ 82 short so_qlimit; /* max number queued connections */ 83 short so_timeo; /* connection timeout */ 84 u_short so_error; /* error affecting connection */ 85 struct sigio *so_sigio; /* information for async I/O or 86 out of band data (SIGURG) */ 87 u_long so_oobmark; /* chars to oob mark */ 88 TAILQ_HEAD(, aiocblist) so_aiojobq; /* AIO ops waiting on socket */ 89/* 90 * Variables for socket buffering. 91 */ 92 struct sockbuf { 93 u_long sb_cc; /* actual chars in buffer */ 94 u_long sb_hiwat; /* max actual char count */ 95 u_long sb_mbcnt; /* chars of mbufs used */ 96 u_long sb_mbmax; /* max chars of mbufs to use */ 97 long sb_lowat; /* low water mark */ 98 struct mbuf *sb_mb; /* the mbuf chain */ 99 struct selinfo sb_sel; /* process selecting read/write */ 100 short sb_flags; /* flags, see below */ 101 short sb_timeo; /* timeout for read/write */ 102 } so_rcv, so_snd; 103#define SB_MAX (256*1024) /* default for max chars in sockbuf */ 104#define SB_LOCK 0x01 /* lock on data queue */ 105#define SB_WANT 0x02 /* someone is waiting to lock */ 106#define SB_WAIT 0x04 /* someone is waiting for data/space */ 107#define SB_SEL 0x08 /* someone is selecting */ 108#define SB_ASYNC 0x10 /* ASYNC I/O, need signals */ 109#define SB_UPCALL 0x20 /* someone wants an upcall */ 110#define SB_NOINTR 0x40 /* operations not interruptible */ 111#define SB_AIO 0x80 /* AIO operations queued */ 112#define SB_KNOTE 0x100 /* kernel note attached */ 113 114 void (*so_upcall)(struct socket *, void *, int); 115 void *so_upcallarg; 116 struct ucred *so_cred; /* user credentials */ 117 /* NB: generation count must not be first; easiest to make it last. */ 118 so_gen_t so_gencnt; /* generation count */ 119 void *so_emuldata; /* private data for emulators */ 120 struct so_accf { 121 struct accept_filter *so_accept_filter; 122 void *so_accept_filter_arg; /* saved filter args */ 123 char *so_accept_filter_str; /* saved user args */ 124 } *so_accf; 125}; 126 127/* 128 * Socket state bits. 129 */ 130#define SS_NOFDREF 0x0001 /* no file table ref any more */ 131#define SS_ISCONNECTED 0x0002 /* socket connected to a peer */ 132#define SS_ISCONNECTING 0x0004 /* in process of connecting to peer */ 133#define SS_ISDISCONNECTING 0x0008 /* in process of disconnecting */ 134#define SS_CANTSENDMORE 0x0010 /* can't send more data to peer */ 135#define SS_CANTRCVMORE 0x0020 /* can't receive more data from peer */ 136#define SS_RCVATMARK 0x0040 /* at mark on input */ 137 138#define SS_NBIO 0x0100 /* non-blocking ops */ 139#define SS_ASYNC 0x0200 /* async i/o notify */ 140#define SS_ISCONFIRMING 0x0400 /* deciding to accept connection req */ 141 142#define SS_INCOMP 0x0800 /* unaccepted, incomplete connection */ 143#define SS_COMP 0x1000 /* unaccepted, complete connection */ 144#define SS_ISDISCONNECTED 0x2000 /* socket disconnected from peer */ 145 146/* 147 * Externalized form of struct socket used by the sysctl(3) interface. 148 */ 149struct xsocket { 150 size_t xso_len; /* length of this structure */ 151 struct socket *xso_so; /* makes a convenient handle sometimes */ 152 short so_type; 153 short so_options; 154 short so_linger; 155 short so_state; 156 caddr_t so_pcb; /* another convenient handle */ 157 int xso_protocol; 158 int xso_family; 159 short so_qlen; 160 short so_incqlen; 161 short so_qlimit; 162 short so_timeo; 163 u_short so_error; 164 pid_t so_pgid; 165 u_long so_oobmark; 166 struct xsockbuf { 167 u_long sb_cc; 168 u_long sb_hiwat; 169 u_long sb_mbcnt; 170 u_long sb_mbmax; 171 long sb_lowat; 172 short sb_flags; 173 short sb_timeo; 174 } so_rcv, so_snd; 175 uid_t so_uid; /* XXX */ 176}; 177 178/* 179 * Macros for sockets and socket buffering. 180 */ 181 182/* 183 * Do we need to notify the other side when I/O is possible? 184 */ 185#define sb_notify(sb) (((sb)->sb_flags & (SB_WAIT | SB_SEL | SB_ASYNC | \ 186 SB_UPCALL | SB_AIO | SB_KNOTE)) != 0) 187 188/* 189 * How much space is there in a socket buffer (so->so_snd or so->so_rcv)? 190 * This is problematical if the fields are unsigned, as the space might 191 * still be negative (cc > hiwat or mbcnt > mbmax). Should detect 192 * overflow and return 0. Should use "lmin" but it doesn't exist now. 193 */ 194#define sbspace(sb) \ 195 ((long) imin((int)((sb)->sb_hiwat - (sb)->sb_cc), \ 196 (int)((sb)->sb_mbmax - (sb)->sb_mbcnt))) 197 198/* do we have to send all at once on a socket? */ 199#define sosendallatonce(so) \ 200 ((so)->so_proto->pr_flags & PR_ATOMIC) 201 202/* can we read something from so? */ 203#define soreadable(so) \ 204 ((so)->so_rcv.sb_cc >= (so)->so_rcv.sb_lowat || \ 205 ((so)->so_state & SS_CANTRCVMORE) || \ 206 !TAILQ_EMPTY(&(so)->so_comp) || (so)->so_error) 207 208/* can we write something to so? */ 209#define sowriteable(so) \ 210 ((sbspace(&(so)->so_snd) >= (so)->so_snd.sb_lowat && \ 211 (((so)->so_state&SS_ISCONNECTED) || \ 212 ((so)->so_proto->pr_flags&PR_CONNREQUIRED)==0)) || \ 213 ((so)->so_state & SS_CANTSENDMORE) || \ 214 (so)->so_error) 215 216/* adjust counters in sb reflecting allocation of m */ 217#define sballoc(sb, m) { \ 218 (sb)->sb_cc += (m)->m_len; \ 219 (sb)->sb_mbcnt += MSIZE; \ 220 if ((m)->m_flags & M_EXT) \ 221 (sb)->sb_mbcnt += (m)->m_ext.ext_size; \ 222} 223 224/* adjust counters in sb reflecting freeing of m */ 225#define sbfree(sb, m) { \ 226 (sb)->sb_cc -= (m)->m_len; \ 227 (sb)->sb_mbcnt -= MSIZE; \ 228 if ((m)->m_flags & M_EXT) \ 229 (sb)->sb_mbcnt -= (m)->m_ext.ext_size; \ 230} 231 232/* 233 * Set lock on sockbuf sb; sleep if lock is already held. 234 * Unless SB_NOINTR is set on sockbuf, sleep is interruptible. 235 * Returns error without lock if sleep is interrupted. 236 */ 237#define sblock(sb, wf) ((sb)->sb_flags & SB_LOCK ? \ 238 (((wf) == M_WAITOK) ? sb_lock(sb) : EWOULDBLOCK) : \ 239 ((sb)->sb_flags |= SB_LOCK), 0) 240 241/* release lock on sockbuf sb */ 242#define sbunlock(sb) { \ 243 (sb)->sb_flags &= ~SB_LOCK; \ 244 if ((sb)->sb_flags & SB_WANT) { \ 245 (sb)->sb_flags &= ~SB_WANT; \ 246 wakeup((caddr_t)&(sb)->sb_flags); \ 247 } \ 248} 249 250/* 251 * soref()/sorele() ref-count the socket structure. Note that you must 252 * still explicitly close the socket, but the last ref count will free 253 * the structure. 254 */ 255 256#define soref(so) do { \ 257 ++(so)->so_count; \ 258 } while (0) 259 260#define sorele(so) do { \ 261 if ((so)->so_count <= 0) \ 262 panic("sorele");\ 263 if (--(so)->so_count == 0)\ 264 sofree(so); \ 265 } while (0) 266 267#define sotryfree(so) do { \ 268 if ((so)->so_count == 0) \ 269 sofree(so); \ 270 } while(0) 271 272#define sorwakeup(so) do { \ 273 if (sb_notify(&(so)->so_rcv)) \ 274 sowakeup((so), &(so)->so_rcv); \ 275 } while (0) 276 277#define sowwakeup(so) do { \ 278 if (sb_notify(&(so)->so_snd)) \ 279 sowakeup((so), &(so)->so_snd); \ 280 } while (0) 281 282#ifdef _KERNEL 283 284/* 285 * Argument structure for sosetopt et seq. This is in the KERNEL 286 * section because it will never be visible to user code. 287 */ 288enum sopt_dir { SOPT_GET, SOPT_SET }; 289struct sockopt { 290 enum sopt_dir sopt_dir; /* is this a get or a set? */ 291 int sopt_level; /* second arg of [gs]etsockopt */ 292 int sopt_name; /* third arg of [gs]etsockopt */ 293 void *sopt_val; /* fourth arg of [gs]etsockopt */ 294 size_t sopt_valsize; /* (almost) fifth arg of [gs]etsockopt */ 295 struct thread *sopt_td; /* calling thread or null if kernel */ 296}; 297 298struct sf_buf { 299 SLIST_ENTRY(sf_buf) free_list; /* list of free buffer slots */ 300 struct vm_page *m; /* currently mapped page */ 301 vm_offset_t kva; /* va of mapping */ 302}; 303 304struct accept_filter { 305 char accf_name[16]; 306 void (*accf_callback) 307 (struct socket *so, void *arg, int waitflag); 308 void * (*accf_create) 309 (struct socket *so, char *arg); 310 void (*accf_destroy) 311 (struct socket *so); 312 SLIST_ENTRY(accept_filter) accf_next; /* next on the list */ 313}; 314 315#ifdef MALLOC_DECLARE 316MALLOC_DECLARE(M_PCB); 317MALLOC_DECLARE(M_SONAME); 318MALLOC_DECLARE(M_ACCF); 319#endif 320 321extern int maxsockets; 322extern u_long sb_max; 323extern vm_zone_t socket_zone; 324extern so_gen_t so_gencnt; 325 326struct file; 327struct filedesc; 328struct mbuf; 329struct sockaddr; 330struct stat; 331struct ucred; 332struct uio; 333struct knote; 334 335/* 336 * File operations on sockets. 337 */ 338int soo_read(struct file *fp, struct uio *uio, struct ucred *cred, 339 int flags, struct thread *td); 340int soo_write(struct file *fp, struct uio *uio, struct ucred *cred, 341 int flags, struct thread *td); 342int soo_close(struct file *fp, struct thread *td); 343int soo_ioctl(struct file *fp, u_long cmd, caddr_t data, 344 struct thread *td); 345int soo_poll(struct file *fp, int events, struct ucred *cred, 346 struct thread *td); 347int soo_stat(struct file *fp, struct stat *ub, struct thread *td); 348int sokqfilter(struct file *fp, struct knote *kn); 349 350/* 351 * From uipc_socket and friends 352 */ 353struct sockaddr *dup_sockaddr(struct sockaddr *sa, int canwait); 354int sockargs(struct mbuf **mp, caddr_t buf, int buflen, int type); 355int getsockaddr(struct sockaddr **namp, caddr_t uaddr, size_t len); 356void sbappend(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 waitok); 379int sobind(struct socket *so, struct sockaddr *nam, struct thread *td); 380void socantrcvmore(struct socket *so); 381void socantsendmore(struct socket *so); 382int soclose(struct socket *so); 383int soconnect(struct socket *so, struct sockaddr *nam, struct thread *td); 384int soconnect2(struct socket *so1, struct socket *so2); 385int socreate(int dom, struct socket **aso, int type, int proto, 386 struct ucred *cred, struct thread *td); 387int sodisconnect(struct socket *so); 388void sofree(struct socket *so); 389int sogetopt(struct socket *so, struct sockopt *sopt); 390void sohasoutofband(struct socket *so); 391void soisconnected(struct socket *so); 392void soisconnecting(struct socket *so); 393void soisdisconnected(struct socket *so); 394void soisdisconnecting(struct socket *so); 395int solisten(struct socket *so, int backlog, struct thread *td); 396struct socket * 397 sodropablereq(struct socket *head); 398struct socket * 399 sonewconn(struct socket *head, int connstatus); 400int sooptcopyin(struct sockopt *sopt, void *buf, size_t len, 401 size_t minlen); 402int sooptcopyout(struct sockopt *sopt, void *buf, size_t len); 403 404/* XXX; prepare mbuf for (__FreeBSD__ < 3) routines. */ 405int soopt_getm(struct sockopt *sopt, struct mbuf **mp); 406int soopt_mcopyin(struct sockopt *sopt, struct mbuf *m); 407int soopt_mcopyout(struct sockopt *sopt, struct mbuf *m); 408 409int sopoll(struct socket *so, int events, struct ucred *cred, 410 struct thread *td); 411int soreceive(struct socket *so, struct sockaddr **paddr, 412 struct uio *uio, struct mbuf **mp0, 413 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/* accept filter functions */ 425int accept_filt_add(struct accept_filter *filt); 426int accept_filt_del(char *name); 427struct accept_filter * accept_filt_get(char *name); 428#ifdef ACCEPT_FILTER_MOD 429int accept_filt_generic_mod_event(module_t mod, int event, void *data); 430SYSCTL_DECL(_net_inet_accf); 431#endif /* ACCEPT_FILTER_MOD */ 432 433int socheckuid(struct socket *so, uid_t uid); 434int socheckproc(struct socket *so, struct proc *p); 435 436#endif /* _KERNEL */ 437 438#endif /* !_SYS_SOCKETVAR_H_ */ 439