1 /* $OpenBSD: socketvar.h,v 1.112 2022/11/26 17:52:35 mvs Exp $ */ 2 /* $NetBSD: socketvar.h,v 1.18 1996/02/09 18:25:38 christos Exp $ */ 3 4 /*- 5 * Copyright (c) 1982, 1986, 1990, 1993 6 * The Regents of the University of California. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 * 32 * @(#)socketvar.h 8.1 (Berkeley) 6/2/93 33 */ 34 35 #ifndef _SYS_SOCKETVAR_H_ 36 #define _SYS_SOCKETVAR_H_ 37 38 #include <sys/selinfo.h> /* for struct selinfo */ 39 #include <sys/queue.h> 40 #include <sys/sigio.h> /* for struct sigio_ref */ 41 #include <sys/task.h> 42 #include <sys/timeout.h> 43 #include <sys/rwlock.h> 44 #include <sys/refcnt.h> 45 46 #ifndef _SOCKLEN_T_DEFINED_ 47 #define _SOCKLEN_T_DEFINED_ 48 typedef __socklen_t socklen_t; /* length type for network syscalls */ 49 #endif 50 51 TAILQ_HEAD(soqhead, socket); 52 53 /* 54 * Kernel structure per socket. 55 * Contains send and receive buffer queues, 56 * handle on protocol and pointer to protocol 57 * private data and error information. 58 */ 59 struct socket { 60 const struct protosw *so_proto; /* protocol handle */ 61 struct rwlock so_lock; /* this socket lock */ 62 struct refcnt so_refcnt; /* references to this socket */ 63 void *so_pcb; /* protocol control block */ 64 u_int so_state; /* internal state flags SS_*, below */ 65 short so_type; /* generic type, see socket.h */ 66 short so_options; /* from socket call, see socket.h */ 67 short so_linger; /* time to linger while closing */ 68 /* 69 * Variables for connection queueing. 70 * Socket where accepts occur is so_head in all subsidiary sockets. 71 * If so_head is 0, socket is not related to an accept. 72 * For head socket so_q0 queues partially completed connections, 73 * while so_q is a queue of connections ready to be accepted. 74 * If a connection is aborted and it has so_head set, then 75 * it has to be pulled out of either so_q0 or so_q. 76 * We allow connections to queue up based on current queue lengths 77 * and limit on number of queued connections for this socket. 78 */ 79 struct socket *so_head; /* back pointer to accept socket */ 80 struct soqhead *so_onq; /* queue (q or q0) that we're on */ 81 struct soqhead so_q0; /* queue of partial connections */ 82 struct soqhead so_q; /* queue of incoming connections */ 83 struct sigio_ref so_sigio; /* async I/O registration */ 84 TAILQ_ENTRY(socket) so_qe; /* our queue entry (q or q0) */ 85 short so_q0len; /* partials on so_q0 */ 86 short so_qlen; /* number of connections on so_q */ 87 short so_qlimit; /* max number queued connections */ 88 u_long so_newconn; /* # of pending sonewconn() threads */ 89 short so_timeo; /* connection timeout */ 90 u_long so_oobmark; /* chars to oob mark */ 91 u_int so_error; /* error affecting connection */ 92 /* 93 * Variables for socket splicing, allocated only when needed. 94 */ 95 struct sosplice { 96 struct socket *ssp_socket; /* send data to drain socket */ 97 struct socket *ssp_soback; /* back ref to source socket */ 98 off_t ssp_len; /* number of bytes spliced */ 99 off_t ssp_max; /* maximum number of bytes */ 100 struct timeval ssp_idletv; /* idle timeout */ 101 struct timeout ssp_idleto; 102 struct task ssp_task; /* task for somove */ 103 } *so_sp; 104 /* 105 * Variables for socket buffering. 106 */ 107 struct sockbuf { 108 /* The following fields are all zeroed on flush. */ 109 #define sb_startzero sb_cc 110 u_long sb_cc; /* actual chars in buffer */ 111 u_long sb_datacc; /* data only chars in buffer */ 112 u_long sb_hiwat; /* max actual char count */ 113 u_long sb_wat; /* default watermark */ 114 u_long sb_mbcnt; /* chars of mbufs used */ 115 u_long sb_mbmax; /* max chars of mbufs to use */ 116 long sb_lowat; /* low water mark */ 117 struct mbuf *sb_mb; /* the mbuf chain */ 118 struct mbuf *sb_mbtail; /* the last mbuf in the chain */ 119 struct mbuf *sb_lastrecord;/* first mbuf of last record in 120 socket buffer */ 121 short sb_flags; /* flags, see below */ 122 /* End area that is zeroed on flush. */ 123 #define sb_endzero sb_flags 124 uint64_t sb_timeo_nsecs;/* timeout for read/write */ 125 struct selinfo sb_sel; /* process selecting read/write */ 126 } so_rcv, so_snd; 127 #define SB_MAX (2*1024*1024) /* default for max chars in sockbuf */ 128 #define SB_LOCK 0x01 /* lock on data queue */ 129 #define SB_WANT 0x02 /* someone is waiting to lock */ 130 #define SB_WAIT 0x04 /* someone is waiting for data/space */ 131 #define SB_ASYNC 0x10 /* ASYNC I/O, need signals */ 132 #define SB_SPLICE 0x20 /* buffer is splice source or drain */ 133 #define SB_NOINTR 0x40 /* operations not interruptible */ 134 135 void (*so_upcall)(struct socket *so, caddr_t arg, int waitf); 136 caddr_t so_upcallarg; /* Arg for above */ 137 uid_t so_euid, so_ruid; /* who opened the socket */ 138 gid_t so_egid, so_rgid; 139 pid_t so_cpid; /* pid of process that opened socket */ 140 }; 141 142 /* 143 * Socket state bits. 144 */ 145 #define SS_NOFDREF 0x001 /* no file table ref any more */ 146 #define SS_ISCONNECTED 0x002 /* socket connected to a peer */ 147 #define SS_ISCONNECTING 0x004 /* in process of connecting to peer */ 148 #define SS_ISDISCONNECTING 0x008 /* in process of disconnecting */ 149 #define SS_CANTSENDMORE 0x010 /* can't send more data to peer */ 150 #define SS_CANTRCVMORE 0x020 /* can't receive more data from peer */ 151 #define SS_RCVATMARK 0x040 /* at mark on input */ 152 #define SS_ISDISCONNECTED 0x800 /* socket disconnected from peer */ 153 154 #define SS_PRIV 0x080 /* privileged for broadcast, raw... */ 155 #define SS_CONNECTOUT 0x1000 /* connect, not accept, at this end */ 156 #define SS_ISSENDING 0x2000 /* hint for lower layer */ 157 #define SS_DNS 0x4000 /* created using SOCK_DNS socket(2) */ 158 #define SS_NEWCONN_WAIT 0x8000 /* waiting sonewconn() relock */ 159 #define SS_YP 0x10000 /* created using ypconnect(2) */ 160 161 #ifdef _KERNEL 162 163 #include <sys/protosw.h> 164 #include <lib/libkern/libkern.h> 165 166 void soassertlocked(struct socket *); 167 168 static inline void 169 soref(struct socket *so) 170 { 171 refcnt_take(&so->so_refcnt); 172 } 173 174 static inline void 175 sorele(struct socket *so) 176 { 177 refcnt_rele_wake(&so->so_refcnt); 178 } 179 180 /* 181 * Macros for sockets and socket buffering. 182 */ 183 184 #define isspliced(so) ((so)->so_sp && (so)->so_sp->ssp_socket) 185 #define issplicedback(so) ((so)->so_sp && (so)->so_sp->ssp_soback) 186 187 /* 188 * Do we need to notify the other side when I/O is possible? 189 */ 190 static inline int 191 sb_notify(struct socket *so, struct sockbuf *sb) 192 { 193 KASSERT(sb == &so->so_rcv || sb == &so->so_snd); 194 soassertlocked(so); 195 return ((sb->sb_flags & (SB_WAIT|SB_ASYNC|SB_SPLICE)) != 0 || 196 !klist_empty(&sb->sb_sel.si_note)); 197 } 198 199 /* 200 * How much space is there in a socket buffer (so->so_snd or so->so_rcv)? 201 * This is problematical if the fields are unsigned, as the space might 202 * still be negative (cc > hiwat or mbcnt > mbmax). Should detect 203 * overflow and return 0. 204 */ 205 static inline long 206 sbspace(struct socket *so, struct sockbuf *sb) 207 { 208 KASSERT(sb == &so->so_rcv || sb == &so->so_snd); 209 soassertlocked(so); 210 return lmin(sb->sb_hiwat - sb->sb_cc, sb->sb_mbmax - sb->sb_mbcnt); 211 } 212 213 /* do we have to send all at once on a socket? */ 214 #define sosendallatonce(so) \ 215 ((so)->so_proto->pr_flags & PR_ATOMIC) 216 217 /* are we sending on this socket? */ 218 #define soissending(so) \ 219 ((so)->so_state & SS_ISSENDING) 220 221 /* can we read something from so? */ 222 static inline int 223 soreadable(struct socket *so) 224 { 225 soassertlocked(so); 226 if (isspliced(so)) 227 return 0; 228 return (so->so_state & SS_CANTRCVMORE) || so->so_qlen || so->so_error || 229 so->so_rcv.sb_cc >= so->so_rcv.sb_lowat; 230 } 231 232 /* can we write something to so? */ 233 static inline int 234 sowriteable(struct socket *so) 235 { 236 soassertlocked(so); 237 return ((sbspace(so, &so->so_snd) >= so->so_snd.sb_lowat && 238 ((so->so_state & SS_ISCONNECTED) || 239 (so->so_proto->pr_flags & PR_CONNREQUIRED)==0)) || 240 (so->so_state & SS_CANTSENDMORE) || so->so_error); 241 } 242 243 /* adjust counters in sb reflecting allocation of m */ 244 static inline void 245 sballoc(struct socket *so, struct sockbuf *sb, struct mbuf *m) 246 { 247 sb->sb_cc += m->m_len; 248 if (m->m_type != MT_CONTROL && m->m_type != MT_SONAME) 249 sb->sb_datacc += m->m_len; 250 sb->sb_mbcnt += MSIZE; 251 if (m->m_flags & M_EXT) 252 sb->sb_mbcnt += m->m_ext.ext_size; 253 } 254 255 /* adjust counters in sb reflecting freeing of m */ 256 static inline void 257 sbfree(struct socket *so, struct sockbuf *sb, struct mbuf *m) 258 { 259 sb->sb_cc -= m->m_len; 260 if (m->m_type != MT_CONTROL && m->m_type != MT_SONAME) 261 sb->sb_datacc -= m->m_len; 262 sb->sb_mbcnt -= MSIZE; 263 if (m->m_flags & M_EXT) 264 sb->sb_mbcnt -= m->m_ext.ext_size; 265 } 266 267 /* 268 * Set lock on sockbuf sb; sleep if lock is already held. 269 * Unless SB_NOINTR is set on sockbuf, sleep is interruptible. 270 * Returns error without lock if sleep is interrupted. 271 */ 272 int sblock(struct socket *, struct sockbuf *, int); 273 274 /* release lock on sockbuf sb */ 275 void sbunlock(struct socket *, struct sockbuf *); 276 277 #define SB_EMPTY_FIXUP(sb) do { \ 278 if ((sb)->sb_mb == NULL) { \ 279 (sb)->sb_mbtail = NULL; \ 280 (sb)->sb_lastrecord = NULL; \ 281 } \ 282 } while (/*CONSTCOND*/0) 283 284 extern u_long sb_max; 285 286 extern struct pool socket_pool; 287 288 struct mbuf; 289 struct sockaddr; 290 struct proc; 291 struct msghdr; 292 struct stat; 293 struct knote; 294 295 /* 296 * File operations on sockets. 297 */ 298 int soo_read(struct file *, struct uio *, int); 299 int soo_write(struct file *, struct uio *, int); 300 int soo_ioctl(struct file *, u_long, caddr_t, struct proc *); 301 int soo_kqfilter(struct file *, struct knote *); 302 int soo_close(struct file *, struct proc *); 303 int soo_stat(struct file *, struct stat *, struct proc *); 304 void sbappend(struct socket *, struct sockbuf *, struct mbuf *); 305 void sbappendstream(struct socket *, struct sockbuf *, struct mbuf *); 306 int sbappendaddr(struct socket *, struct sockbuf *, 307 const struct sockaddr *, struct mbuf *, struct mbuf *); 308 int sbappendcontrol(struct socket *, struct sockbuf *, struct mbuf *, 309 struct mbuf *); 310 void sbappendrecord(struct socket *, struct sockbuf *, struct mbuf *); 311 void sbcompress(struct socket *, struct sockbuf *, struct mbuf *, 312 struct mbuf *); 313 struct mbuf * 314 sbcreatecontrol(const void *, size_t, int, int); 315 void sbdrop(struct socket *, struct sockbuf *, int); 316 void sbdroprecord(struct socket *, struct sockbuf *); 317 void sbflush(struct socket *, struct sockbuf *); 318 void sbrelease(struct socket *, struct sockbuf *); 319 int sbcheckreserve(u_long, u_long); 320 int sbchecklowmem(void); 321 int sbreserve(struct socket *, struct sockbuf *, u_long); 322 int sbwait(struct socket *, struct sockbuf *); 323 void soinit(void); 324 void soabort(struct socket *); 325 int soaccept(struct socket *, struct mbuf *); 326 int sobind(struct socket *, struct mbuf *, struct proc *); 327 void socantrcvmore(struct socket *); 328 void socantsendmore(struct socket *); 329 int soclose(struct socket *, int); 330 int soconnect(struct socket *, struct mbuf *); 331 int soconnect2(struct socket *, struct socket *); 332 int socreate(int, struct socket **, int, int); 333 int sodisconnect(struct socket *); 334 struct socket *soalloc(int); 335 void sofree(struct socket *, int); 336 int sogetopt(struct socket *, int, int, struct mbuf *); 337 void sohasoutofband(struct socket *); 338 void soisconnected(struct socket *); 339 void soisconnecting(struct socket *); 340 void soisdisconnected(struct socket *); 341 void soisdisconnecting(struct socket *); 342 int solisten(struct socket *, int); 343 struct socket *sonewconn(struct socket *, int, int); 344 void soqinsque(struct socket *, struct socket *, int); 345 int soqremque(struct socket *, int); 346 int soreceive(struct socket *, struct mbuf **, struct uio *, 347 struct mbuf **, struct mbuf **, int *, socklen_t); 348 int soreserve(struct socket *, u_long, u_long); 349 int sosend(struct socket *, struct mbuf *, struct uio *, 350 struct mbuf *, struct mbuf *, int); 351 int sosetopt(struct socket *, int, int, struct mbuf *); 352 int soshutdown(struct socket *, int); 353 void sowakeup(struct socket *, struct sockbuf *); 354 void sorwakeup(struct socket *); 355 void sowwakeup(struct socket *); 356 int sockargs(struct mbuf **, const void *, size_t, int); 357 358 int sosleep_nsec(struct socket *, void *, int, const char *, uint64_t); 359 void solock(struct socket *); 360 void solock_shared(struct socket *); 361 int solock_persocket(struct socket *); 362 void solock_pair(struct socket *, struct socket *); 363 void sounlock(struct socket *); 364 void sounlock_shared(struct socket *); 365 366 int sendit(struct proc *, int, struct msghdr *, int, register_t *); 367 int recvit(struct proc *, int, struct msghdr *, caddr_t, register_t *); 368 int doaccept(struct proc *, int, struct sockaddr *, socklen_t *, int, 369 register_t *); 370 371 #ifdef SOCKBUF_DEBUG 372 void sblastrecordchk(struct sockbuf *, const char *); 373 #define SBLASTRECORDCHK(sb, where) sblastrecordchk((sb), (where)) 374 375 void sblastmbufchk(struct sockbuf *, const char *); 376 #define SBLASTMBUFCHK(sb, where) sblastmbufchk((sb), (where)) 377 void sbcheck(struct socket *, struct sockbuf *); 378 #define SBCHECK(so, sb) sbcheck((so), (sb)) 379 #else 380 #define SBLASTRECORDCHK(sb, where) /* nothing */ 381 #define SBLASTMBUFCHK(sb, where) /* nothing */ 382 #define SBCHECK(so, sb) /* nothing */ 383 #endif /* SOCKBUF_DEBUG */ 384 385 #endif /* _KERNEL */ 386 387 #endif /* _SYS_SOCKETVAR_H_ */ 388