1 /* $NetBSD: socketvar.h,v 1.51 2002/05/02 17:55:52 thorpej Exp $ */ 2 3 /*- 4 * Copyright (c) 1982, 1986, 1990, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 * 35 * @(#)socketvar.h 8.3 (Berkeley) 2/19/95 36 */ 37 38 #ifndef _SYS_SOCKETVAR_H_ 39 #define _SYS_SOCKETVAR_H_ 40 41 #include <sys/select.h> /* for struct selinfo */ 42 #include <sys/queue.h> 43 44 #if !defined(_KERNEL) || defined(LKM) 45 struct uio; 46 #endif 47 48 TAILQ_HEAD(soqhead, socket); 49 50 /* 51 * Kernel structure per socket. 52 * Contains send and receive buffer queues, 53 * handle on protocol and pointer to protocol 54 * private data and error information. 55 */ 56 struct socket { 57 short so_type; /* generic type, see socket.h */ 58 short so_options; /* from socket call, see socket.h */ 59 short so_linger; /* time to linger while closing */ 60 short so_state; /* internal state flags SS_*, below */ 61 void *so_pcb; /* protocol control block */ 62 struct protosw *so_proto; /* protocol handle */ 63 /* 64 * Variables for connection queueing. 65 * Socket where accepts occur is so_head in all subsidiary sockets. 66 * If so_head is 0, socket is not related to an accept. 67 * For head socket so_q0 queues partially completed connections, 68 * while so_q is a queue of connections ready to be accepted. 69 * If a connection is aborted and it has so_head set, then 70 * it has to be pulled out of either so_q0 or so_q. 71 * We allow connections to queue up based on current queue lengths 72 * and limit on number of queued connections for this socket. 73 */ 74 struct socket *so_head; /* back pointer to accept socket */ 75 struct soqhead *so_onq; /* queue (q or q0) that we're on */ 76 struct soqhead so_q0; /* queue of partial connections */ 77 struct soqhead so_q; /* queue of incoming connections */ 78 TAILQ_ENTRY(socket) so_qe; /* our queue entry (q or q0) */ 79 short so_q0len; /* partials on so_q0 */ 80 short so_qlen; /* number of connections on so_q */ 81 short so_qlimit; /* max number queued connections */ 82 short so_timeo; /* connection timeout */ 83 u_short so_error; /* error affecting connection */ 84 pid_t so_pgid; /* pgid for signals */ 85 u_long so_oobmark; /* chars to oob mark */ 86 /* 87 * Variables for socket buffering. 88 */ 89 struct sockbuf { 90 u_long sb_cc; /* actual chars in buffer */ 91 u_long sb_hiwat; /* max actual char count */ 92 u_long sb_mbcnt; /* chars of mbufs used */ 93 u_long sb_mbmax; /* max chars of mbufs to use */ 94 long sb_lowat; /* low water mark */ 95 struct mbuf *sb_mb; /* the mbuf chain */ 96 struct selinfo sb_sel; /* process selecting read/write */ 97 short sb_flags; /* flags, see below */ 98 short sb_timeo; /* timeout for read/write */ 99 } so_rcv, so_snd; 100 101 #ifndef SB_MAX 102 #define SB_MAX (256*1024) /* default for max chars in sockbuf */ 103 #endif 104 105 #define SB_LOCK 0x01 /* lock on data queue */ 106 #define SB_WANT 0x02 /* someone is waiting to lock */ 107 #define SB_WAIT 0x04 /* someone is waiting for data/space */ 108 #define SB_SEL 0x08 /* someone is selecting */ 109 #define SB_ASYNC 0x10 /* ASYNC I/O, need signals */ 110 #define SB_UPCALL 0x20 /* someone wants an upcall */ 111 #define SB_NOINTR 0x40 /* operations not interruptible */ 112 113 void *so_internal; /* Space for svr4 stream data */ 114 void (*so_upcall) __P((struct socket *so, caddr_t arg, 115 int waitf)); 116 caddr_t so_upcallarg; /* Arg for above */ 117 int (*so_send) __P((struct socket *so, struct mbuf *addr, 118 struct uio *uio, struct mbuf *top, 119 struct mbuf *control, int flags)); 120 int (*so_receive) __P((struct socket *so, 121 struct mbuf **paddr, 122 struct uio *uio, struct mbuf **mp0, 123 struct mbuf **controlp, int *flagsp)); 124 uid_t so_uid; /* who opened the socket */ 125 struct mbuf *so_pendfree; /* loaned-page mbufs w/ frees pending */ 126 }; 127 128 /* 129 * Socket state bits. 130 */ 131 #define SS_NOFDREF 0x001 /* no file table ref any more */ 132 #define SS_ISCONNECTED 0x002 /* socket connected to a peer */ 133 #define SS_ISCONNECTING 0x004 /* in process of connecting to peer */ 134 #define SS_ISDISCONNECTING 0x008 /* in process of disconnecting */ 135 #define SS_CANTSENDMORE 0x010 /* can't send more data to peer */ 136 #define SS_CANTRCVMORE 0x020 /* can't receive more data from peer */ 137 #define SS_RCVATMARK 0x040 /* at mark on input */ 138 #define SS_ISDISCONNECTED 0x800 /* socket disconnected from peer */ 139 140 #define SS_NBIO 0x080 /* non-blocking ops */ 141 #define SS_ASYNC 0x100 /* async i/o notify */ 142 #define SS_ISCONFIRMING 0x200 /* deciding to accept connection req */ 143 #define SS_MORETOCOME 0x400 /* 144 * hint from sosend to lower layer; 145 * more data coming 146 */ 147 #define SS_ISAPIPE 0x800 /* socket is implementing a pipe */ 148 149 150 /* 151 * Macros for sockets and socket buffering. 152 */ 153 154 /* 155 * Do we need to notify the other side when I/O is possible? 156 */ 157 #define sb_notify(sb) (((sb)->sb_flags & \ 158 (SB_WAIT | SB_SEL | SB_ASYNC | SB_UPCALL)) != 0) 159 160 /* 161 * How much space is there in a socket buffer (so->so_snd or so->so_rcv)? 162 * This is problematical if the fields are unsigned, as the space might 163 * still be negative (cc > hiwat or mbcnt > mbmax). Should detect 164 * overflow and return 0. 165 */ 166 #define sbspace(sb) \ 167 (lmin((sb)->sb_hiwat - (sb)->sb_cc, (sb)->sb_mbmax - (sb)->sb_mbcnt)) 168 169 /* do we have to send all at once on a socket? */ 170 #define sosendallatonce(so) \ 171 ((so)->so_proto->pr_flags & PR_ATOMIC) 172 173 /* can we read something from so? */ 174 #define soreadable(so) \ 175 ((so)->so_rcv.sb_cc >= (so)->so_rcv.sb_lowat || \ 176 ((so)->so_state & SS_CANTRCVMORE) || \ 177 (so)->so_qlen || (so)->so_error) 178 179 /* can we write something to so? */ 180 #define sowriteable(so) \ 181 ((sbspace(&(so)->so_snd) >= (so)->so_snd.sb_lowat && \ 182 (((so)->so_state&SS_ISCONNECTED) || \ 183 ((so)->so_proto->pr_flags&PR_CONNREQUIRED)==0)) || \ 184 ((so)->so_state & SS_CANTSENDMORE) || \ 185 (so)->so_error) 186 187 /* adjust counters in sb reflecting allocation of m */ 188 #define sballoc(sb, m) \ 189 do { \ 190 (sb)->sb_cc += (m)->m_len; \ 191 (sb)->sb_mbcnt += MSIZE; \ 192 if ((m)->m_flags & M_EXT) \ 193 (sb)->sb_mbcnt += (m)->m_ext.ext_size; \ 194 } while (/* CONSTCOND */ 0) 195 196 /* adjust counters in sb reflecting freeing of m */ 197 #define sbfree(sb, m) \ 198 do { \ 199 (sb)->sb_cc -= (m)->m_len; \ 200 (sb)->sb_mbcnt -= MSIZE; \ 201 if ((m)->m_flags & M_EXT) \ 202 (sb)->sb_mbcnt -= (m)->m_ext.ext_size; \ 203 } while (/* CONSTCOND */ 0) 204 205 /* 206 * Set lock on sockbuf sb; sleep if lock is already held. 207 * Unless SB_NOINTR is set on sockbuf, sleep is interruptible. 208 * Returns error without lock if sleep is interrupted. 209 */ 210 #define sblock(sb, wf) \ 211 ((sb)->sb_flags & SB_LOCK ? \ 212 (((wf) == M_WAITOK) ? sb_lock(sb) : EWOULDBLOCK) : \ 213 ((sb)->sb_flags |= SB_LOCK), 0) 214 215 /* release lock on sockbuf sb */ 216 #define sbunlock(sb) \ 217 do { \ 218 (sb)->sb_flags &= ~SB_LOCK; \ 219 if ((sb)->sb_flags & SB_WANT) { \ 220 (sb)->sb_flags &= ~SB_WANT; \ 221 wakeup((caddr_t)&(sb)->sb_flags); \ 222 } \ 223 } while (/* CONSTCOND */ 0) 224 225 #define sorwakeup(so) \ 226 do { \ 227 if (sb_notify(&(so)->so_rcv)) \ 228 sowakeup((so), &(so)->so_rcv); \ 229 } while (/* CONSTCOND */ 0) 230 231 #define sowwakeup(so) \ 232 do { \ 233 if (sb_notify(&(so)->so_snd)) \ 234 sowakeup((so), &(so)->so_snd); \ 235 } while (/* CONSTCOND */ 0) 236 237 #ifdef _KERNEL 238 extern u_long sb_max; 239 /* to catch callers missing new second argument to sonewconn: */ 240 #define sonewconn(head, connstatus) sonewconn1((head), (connstatus)) 241 242 /* strings for sleep message: */ 243 extern const char netio[], netcon[], netcls[]; 244 245 extern struct pool socket_pool; 246 247 struct mbuf; 248 struct sockaddr; 249 struct proc; 250 struct msghdr; 251 struct stat; 252 253 /* 254 * File operations on sockets. 255 */ 256 int soo_read(struct file *fp, off_t *offset, struct uio *uio, 257 struct ucred *cred, int flags); 258 int soo_write(struct file *fp, off_t *offset, struct uio *uio, 259 struct ucred *cred, int flags); 260 int soo_fcntl(struct file *fp, u_int cmd, caddr_t data, struct proc *p); 261 int soo_ioctl(struct file *fp, u_long cmd, caddr_t data, struct proc *p); 262 int soo_poll(struct file *fp, int events, struct proc *p); 263 int soo_close(struct file *fp, struct proc *p); 264 int soo_stat(struct file *fp, struct stat *ub, struct proc *p); 265 int uipc_usrreq(struct socket *, int , struct mbuf *, 266 struct mbuf *, struct mbuf *, struct proc *); 267 int uipc_ctloutput(int, struct socket *, int, int, struct mbuf **); 268 void sbappend(struct sockbuf *sb, struct mbuf *m); 269 int sbappendaddr(struct sockbuf *sb, struct sockaddr *asa, 270 struct mbuf *m0, struct mbuf *control); 271 int sbappendcontrol(struct sockbuf *sb, struct mbuf *m0, 272 struct mbuf *control); 273 void sbappendrecord(struct sockbuf *sb, struct mbuf *m0); 274 void sbcheck(struct sockbuf *sb); 275 void sbcompress(struct sockbuf *sb, struct mbuf *m, struct mbuf *n); 276 struct mbuf * 277 sbcreatecontrol(caddr_t p, int size, int type, int level); 278 void sbdrop(struct sockbuf *sb, int len); 279 void sbdroprecord(struct sockbuf *sb); 280 void sbflush(struct sockbuf *sb); 281 void sbinsertoob(struct sockbuf *sb, struct mbuf *m0); 282 void sbrelease(struct sockbuf *sb); 283 int sbreserve(struct sockbuf *sb, u_long cc); 284 int sbwait(struct sockbuf *sb); 285 int sb_lock(struct sockbuf *sb); 286 void soinit(void); 287 int soabort(struct socket *so); 288 int soaccept(struct socket *so, struct mbuf *nam); 289 int sobind(struct socket *so, struct mbuf *nam, struct proc *); 290 void socantrcvmore(struct socket *so); 291 void socantsendmore(struct socket *so); 292 int soclose(struct socket *so); 293 int soconnect(struct socket *so, struct mbuf *nam); 294 int soconnect2(struct socket *so1, struct socket *so2); 295 int socreate(int dom, struct socket **aso, int type, int proto); 296 int sodisconnect(struct socket *so); 297 void sofree(struct socket *so); 298 int sogetopt(struct socket *so, int level, int optname, struct mbuf **mp); 299 void sohasoutofband(struct socket *so); 300 void soisconnected(struct socket *so); 301 void soisconnecting(struct socket *so); 302 void soisdisconnected(struct socket *so); 303 void soisdisconnecting(struct socket *so); 304 int solisten(struct socket *so, int backlog); 305 struct socket * 306 sonewconn1(struct socket *head, int connstatus); 307 void soqinsque(struct socket *head, struct socket *so, int q); 308 int soqremque(struct socket *so, int q); 309 int soreceive(struct socket *so, struct mbuf **paddr, struct uio *uio, 310 struct mbuf **mp0, struct mbuf **controlp, int *flagsp); 311 int soreserve(struct socket *so, u_long sndcc, u_long rcvcc); 312 void sorflush(struct socket *so); 313 int sosend(struct socket *so, struct mbuf *addr, struct uio *uio, 314 struct mbuf *top, struct mbuf *control, int flags); 315 int sosetopt(struct socket *so, int level, int optname, struct mbuf *m0); 316 int soshutdown(struct socket *so, int how); 317 void sowakeup(struct socket *so, struct sockbuf *sb); 318 int sockargs(struct mbuf **, const void *, size_t, int); 319 320 int sendit(struct proc *, int, struct msghdr *, int, register_t *); 321 int recvit(struct proc *, int, struct msghdr *, caddr_t, register_t *); 322 323 #endif /* _KERNEL */ 324 325 #endif /* !_SYS_SOCKETVAR_H_ */ 326