Home | History | Annotate | Line # | Download | only in sys
      1 /*	$NetBSD: socketvar.h,v 1.171 2025/04/07 21:02:19 andvar Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Andrew Doran.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*-
     33  * Copyright (c) 1982, 1986, 1990, 1993
     34  *	The Regents of the University of California.  All rights reserved.
     35  *
     36  * Redistribution and use in source and binary forms, with or without
     37  * modification, are permitted provided that the following conditions
     38  * are met:
     39  * 1. Redistributions of source code must retain the above copyright
     40  *    notice, this list of conditions and the following disclaimer.
     41  * 2. Redistributions in binary form must reproduce the above copyright
     42  *    notice, this list of conditions and the following disclaimer in the
     43  *    documentation and/or other materials provided with the distribution.
     44  * 3. Neither the name of the University nor the names of its contributors
     45  *    may be used to endorse or promote products derived from this software
     46  *    without specific prior written permission.
     47  *
     48  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     49  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     50  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     51  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     52  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     53  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     54  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     55  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     56  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     57  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     58  * SUCH DAMAGE.
     59  *
     60  *	@(#)socketvar.h	8.3 (Berkeley) 2/19/95
     61  */
     62 
     63 #ifndef _SYS_SOCKETVAR_H_
     64 #define	_SYS_SOCKETVAR_H_
     65 
     66 #include <sys/select.h>
     67 #include <sys/selinfo.h>		/* for struct selinfo */
     68 #include <sys/queue.h>
     69 #include <sys/mutex.h>
     70 #include <sys/condvar.h>
     71 
     72 #if !defined(_KERNEL)
     73 struct uio;
     74 struct lwp;
     75 struct uidinfo;
     76 #else
     77 #include <sys/atomic.h>
     78 #include <sys/uidinfo.h>
     79 #endif
     80 
     81 TAILQ_HEAD(soqhead, socket);
     82 
     83 /*
     84  * Variables for socket buffering.
     85  */
     86 struct sockbuf {
     87 	struct selinfo sb_sel;		/* process selecting read/write */
     88 	struct mowner *sb_mowner;	/* who owns data for this sockbuf */
     89 	struct socket *sb_so;		/* back pointer to socket */
     90 	kcondvar_t sb_cv;		/* notifier */
     91 	/* When re-zeroing this struct, we zero from sb_startzero to the end */
     92 #define	sb_startzero	sb_cc
     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 	u_long	sb_lowat;		/* low water mark */
     98 	struct mbuf *sb_mb;		/* the mbuf chain */
     99 	struct mbuf *sb_mbtail;		/* the last mbuf in the chain */
    100 	struct mbuf *sb_lastrecord;	/* first mbuf of last record in
    101 					   socket buffer */
    102 	int	sb_flags;		/* flags, see below */
    103 	int	sb_timeo;		/* timeout for read/write */
    104 	u_long	sb_overflowed;		/* # of drops due to full buffer */
    105 };
    106 
    107 #ifndef SB_MAX
    108 #define	SB_MAX		(256*1024)	/* default for max chars in sockbuf */
    109 #endif
    110 
    111 #define	SB_LOCK		0x01		/* lock on data queue */
    112 #define	SB_NOTIFY	0x04		/* someone is waiting for data/space */
    113 #define	SB_ASYNC	0x10		/* ASYNC I/O, need signals */
    114 #define	SB_UPCALL	0x20		/* someone wants an upcall */
    115 #define	SB_NOINTR	0x40		/* operations not interruptible */
    116 #define	SB_KNOTE	0x100		/* kernel note attached */
    117 #define	SB_AUTOSIZE	0x800		/* automatically size socket buffer */
    118 
    119 /*
    120  * Kernel structure per socket.
    121  * Contains send and receive buffer queues,
    122  * handle on protocol and pointer to protocol
    123  * private data and error information.
    124  */
    125 struct so_accf {
    126 	struct accept_filter	*so_accept_filter;
    127 	void	*so_accept_filter_arg;	/* saved filter args */
    128 	char	*so_accept_filter_str;	/* saved user args */
    129 };
    130 
    131 struct sockaddr;
    132 
    133 struct socket {
    134 	kmutex_t * volatile so_lock;	/* pointer to lock on structure */
    135 	kcondvar_t	so_cv;		/* notifier */
    136 	short		so_type;	/* generic type, see socket.h */
    137 	short		so_options;	/* from socket call, see socket.h */
    138 	u_short		so_linger;	/* time to linger while closing */
    139 	short		so_state;	/* internal state flags SS_*, below */
    140 	int		so_unused;	/* used to be so_nbio */
    141 	void		*so_pcb;	/* protocol control block */
    142 	const struct protosw *so_proto;	/* protocol handle */
    143 /*
    144  * Variables for connection queueing.
    145  * Socket where accepts occur is so_head in all subsidiary sockets.
    146  * If so_head is 0, socket is not related to an accept.
    147  * For head socket so_q0 queues partially completed connections,
    148  * while so_q is a queue of connections ready to be accepted.
    149  * If a connection is aborted and it has so_head set, then
    150  * it has to be pulled out of either so_q0 or so_q.
    151  * We allow connections to queue up based on current queue lengths
    152  * and limit on number of queued connections for this socket.
    153  */
    154 	struct socket	*so_head;	/* back pointer to accept socket */
    155 	struct soqhead	*so_onq;	/* queue (q or q0) that we're on */
    156 	struct soqhead	so_q0;		/* queue of partial connections */
    157 	struct soqhead	so_q;		/* queue of incoming connections */
    158 	TAILQ_ENTRY(socket) so_qe;	/* our queue entry (q or q0) */
    159 	short		so_q0len;	/* partials on so_q0 */
    160 	short		so_qlen;	/* number of connections on so_q */
    161 	short		so_qlimit;	/* max number queued connections */
    162 	short		so_timeo;	/* connection timeout */
    163 	u_short		so_error;	/* error affecting connection */
    164 	u_short		so_rerror;	/* error affecting receiving */
    165 	u_short		so_aborting;	/* references from soabort() */
    166 	pid_t		so_pgid;	/* pgid for signals */
    167 	u_long		so_oobmark;	/* chars to oob mark */
    168 	struct sockbuf	so_snd;		/* send buffer */
    169 	struct sockbuf	so_rcv;		/* receive buffer */
    170 
    171 	void		*so_internal;	/* Space for svr4 stream data */
    172 	void		(*so_upcall) (struct socket *, void *, int, int);
    173 	void *		so_upcallarg;	/* Arg for above */
    174 	int		(*so_send) (struct socket *, struct sockaddr *,
    175 					struct uio *, struct mbuf *,
    176 					struct mbuf *, int, struct lwp *);
    177 	int		(*so_receive) (struct socket *,
    178 					struct mbuf **,
    179 					struct uio *, struct mbuf **,
    180 					struct mbuf **, int *);
    181 	struct mowner	*so_mowner;	/* who owns mbufs for this socket */
    182 	struct uidinfo	*so_uidinfo;	/* who opened the socket */
    183 	gid_t		so_egid;	/* creator effective gid */
    184 	pid_t		so_cpid;	/* creator pid */
    185 	struct so_accf	*so_accf;
    186 	kauth_cred_t	so_cred;	/* socket credentials */
    187 };
    188 
    189 /*
    190  * Socket state bits.
    191  */
    192 #define	SS_NOFDREF		0x001	/* no file table ref any more */
    193 #define	SS_ISCONNECTED		0x002	/* socket connected to a peer */
    194 #define	SS_ISCONNECTING		0x004	/* in process of connecting to peer */
    195 #define	SS_ISDISCONNECTING	0x008	/* in process of disconnecting */
    196 #define	SS_CANTSENDMORE		0x010	/* can't send more data to peer */
    197 #define	SS_CANTRCVMORE		0x020	/* can't receive more data from peer */
    198 #define	SS_RCVATMARK		0x040	/* at mark on input */
    199 #define	SS_ISABORTING		0x080	/* aborting fd references - close() */
    200 #define	SS_RESTARTSYS		0x100	/* restart blocked system calls */
    201 #define	SS_POLLRDBAND		0x200	/* poll should return POLLRDBAND */
    202 #define	SS_MORETOCOME		0x400	/*
    203 					 * hint from sosend to lower layer;
    204 					 * more data coming
    205 					 */
    206 #define	SS_ISDISCONNECTED	0x800	/* socket disconnected from peer */
    207 #define	SS_ISAPIPE 		0x1000	/* socket is implementing a pipe */
    208 #define	SS_NBIO			0x2000	/* socket is in non blocking I/O */
    209 
    210 #ifdef _KERNEL
    211 
    212 struct accept_filter {
    213 	char	accf_name[16];
    214 	void	(*accf_callback)
    215 		(struct socket *, void *, int, int);
    216 	void *	(*accf_create)
    217 		(struct socket *, char *);
    218 	void	(*accf_destroy)
    219 		(struct socket *);
    220 	LIST_ENTRY(accept_filter) accf_next;
    221 	u_int	accf_refcnt;
    222 };
    223 
    224 struct sockopt {
    225 	int		sopt_level;		/* option level */
    226 	int		sopt_name;		/* option name */
    227 	size_t		sopt_size;		/* data length */
    228 	size_t		sopt_retsize;		/* returned data length */
    229 	void *		sopt_data;		/* data pointer */
    230 	uint8_t		sopt_buf[sizeof(int)];	/* internal storage */
    231 };
    232 
    233 #define	SB_EMPTY_FIXUP(sb)						\
    234 do {									\
    235 	KASSERT(solocked((sb)->sb_so));					\
    236 	if ((sb)->sb_mb == NULL) {					\
    237 		(sb)->sb_mbtail = NULL;					\
    238 		(sb)->sb_lastrecord = NULL;				\
    239 	}								\
    240 } while (0)
    241 
    242 extern u_long		sb_max;
    243 extern int		somaxkva;
    244 extern int		sock_loan_thresh;
    245 extern kmutex_t		*softnet_lock;
    246 
    247 struct mbuf;
    248 struct lwp;
    249 struct msghdr;
    250 struct stat;
    251 struct knote;
    252 struct sockaddr_big;
    253 enum uio_seg;
    254 
    255 /* 0x400 is SO_OTIMESTAMP */
    256 #define SOOPT_TIMESTAMP(o)     ((o) & (SO_TIMESTAMP | 0x400))
    257 
    258 /*
    259  * File operations on sockets.
    260  */
    261 int	soo_read(file_t *, off_t *, struct uio *, kauth_cred_t, int);
    262 int	soo_write(file_t *, off_t *, struct uio *, kauth_cred_t, int);
    263 int	soo_fcntl(file_t *, u_int cmd, void *);
    264 int	soo_ioctl(file_t *, u_long cmd, void *);
    265 int	soo_poll(file_t *, int);
    266 int	soo_kqfilter(file_t *, struct knote *);
    267 int 	soo_close(file_t *);
    268 int	soo_stat(file_t *, struct stat *);
    269 void	soo_restart(file_t *);
    270 void	sbappend(struct sockbuf *, struct mbuf *);
    271 void	sbappendstream(struct sockbuf *, struct mbuf *);
    272 int	sbappendaddr(struct sockbuf *, const struct sockaddr *, struct mbuf *,
    273 	    struct mbuf *);
    274 int	sbappendaddrchain(struct sockbuf *, const struct sockaddr *,
    275 	     struct mbuf *, int);
    276 int	sbappendcontrol(struct sockbuf *, struct mbuf *, struct mbuf *);
    277 void	sbappendrecord(struct sockbuf *, struct mbuf *);
    278 void	sbcheck(struct sockbuf *);
    279 void	sbcompress(struct sockbuf *, struct mbuf *, struct mbuf *);
    280 struct mbuf *
    281 	sbcreatecontrol(void *, int, int, int);
    282 struct mbuf *
    283 	sbcreatecontrol1(void **, int, int, int, int);
    284 struct mbuf **
    285 	sbsavetimestamp(int, struct mbuf **);
    286 void	sbdrop(struct sockbuf *, int);
    287 void	sbdroprecord(struct sockbuf *);
    288 void	sbflush(struct sockbuf *);
    289 void	sbinsertoob(struct sockbuf *, struct mbuf *);
    290 void	sbrelease(struct sockbuf *, struct socket *);
    291 int	sbreserve(struct sockbuf *, u_long, struct socket *);
    292 int	sbwait(struct sockbuf *);
    293 int	sb_max_set(u_long);
    294 void	soinit(void);
    295 void	soinit1(void);
    296 void	soinit2(void);
    297 int	soabort(struct socket *);
    298 int	soaccept(struct socket *, struct sockaddr *);
    299 int	sofamily(const struct socket *);
    300 int	sobind(struct socket *, struct sockaddr *, struct lwp *);
    301 void	socantrcvmore(struct socket *);
    302 void	socantsendmore(struct socket *);
    303 void	soroverflow(struct socket *);
    304 int	soclose(struct socket *);
    305 int	soconnect(struct socket *, struct sockaddr *, struct lwp *);
    306 int	soconnect2(struct socket *, struct socket *);
    307 int	socreate(int, struct socket **, int, int, struct lwp *,
    308 		 struct socket *);
    309 int	fsocreate(int, struct socket **, int, int, int *, file_t **,
    310 		struct socket *);
    311 int	sodisconnect(struct socket *);
    312 void	sofree(struct socket *);
    313 int	sogetopt(struct socket *, struct sockopt *);
    314 void	sohasoutofband(struct socket *);
    315 void	soisconnected(struct socket *);
    316 void	soisconnecting(struct socket *);
    317 void	soisdisconnected(struct socket *);
    318 void	soisdisconnecting(struct socket *);
    319 int	solisten(struct socket *, int, struct lwp *);
    320 struct socket *
    321 	sonewconn(struct socket *, bool);
    322 void	soqinsque(struct socket *, struct socket *, int);
    323 bool	soqremque(struct socket *, int);
    324 int	soreceive(struct socket *, struct mbuf **, struct uio *,
    325 	    struct mbuf **, struct mbuf **, int *);
    326 int	soreserve(struct socket *, u_long, u_long);
    327 void	sorflush(struct socket *);
    328 int	sosend(struct socket *, struct sockaddr *, struct uio *,
    329 	    struct mbuf *, struct mbuf *, int, struct lwp *);
    330 int	sosetopt(struct socket *, struct sockopt *);
    331 int	so_setsockopt(struct lwp *, struct socket *, int, int, const void *, size_t);
    332 int	soshutdown(struct socket *, int);
    333 void	sorestart(struct socket *);
    334 void	sowakeup(struct socket *, struct sockbuf *, int);
    335 int	sockargs(struct mbuf **, const void *, size_t, enum uio_seg, int);
    336 int	sopoll(struct socket *, int);
    337 struct	socket *soget(bool);
    338 void	soput(struct socket *);
    339 bool	solocked(const struct socket *);
    340 bool	solocked2(const struct socket *, const struct socket *);
    341 int	sblock(struct sockbuf *, int);
    342 void	sbunlock(struct sockbuf *);
    343 int	sowait(struct socket *, bool, int);
    344 void	solockretry(struct socket *, kmutex_t *);
    345 void	sosetlock(struct socket *);
    346 void	solockreset(struct socket *, kmutex_t *);
    347 
    348 void	sockopt_init(struct sockopt *, int, int, size_t);
    349 void	sockopt_destroy(struct sockopt *);
    350 int	sockopt_set(struct sockopt *, const void *, size_t);
    351 int	sockopt_setint(struct sockopt *, int);
    352 int	sockopt_get(const struct sockopt *, void *, size_t);
    353 int	sockopt_getint(const struct sockopt *, int *);
    354 int	sockopt_setmbuf(struct sockopt *, struct mbuf *);
    355 struct mbuf *sockopt_getmbuf(const struct sockopt *);
    356 
    357 int	copyout_sockname(struct sockaddr *, unsigned int *, int, struct mbuf *);
    358 int	copyout_sockname_sb(struct sockaddr *, unsigned int *,
    359     int , struct sockaddr_big *);
    360 int	copyout_msg_control(struct lwp *, struct msghdr *, struct mbuf *);
    361 void	free_control_mbuf(struct lwp *, struct mbuf *, struct mbuf *);
    362 
    363 int	do_sys_getpeername(int, struct sockaddr *);
    364 int	do_sys_getsockname(int, struct sockaddr *);
    365 
    366 int	do_sys_sendmsg(struct lwp *, int, struct msghdr *, int, register_t *);
    367 int	do_sys_sendmsg_so(struct lwp *, int, struct socket *, file_t *,
    368 	    struct msghdr *, int, register_t *);
    369 
    370 int	do_sys_recvmsg(struct lwp *, int, struct msghdr *,
    371 	    struct mbuf **, struct mbuf **, register_t *);
    372 int	do_sys_recvmsg_so(struct lwp *, int, struct socket *,
    373 	    struct msghdr *mp, struct mbuf **, struct mbuf **, register_t *);
    374 
    375 int	do_sys_bind(struct lwp *, int, struct sockaddr *);
    376 int	do_sys_connect(struct lwp *, int, struct sockaddr *);
    377 int	do_sys_accept(struct lwp *, int, struct sockaddr *, register_t *,
    378 	    const sigset_t *, int, int);
    379 
    380 int	do_sys_peeloff(struct socket *, void *);
    381 /*
    382  * Inline functions for sockets and socket buffering.
    383  */
    384 
    385 #include <sys/protosw.h>
    386 #include <sys/mbuf.h>
    387 
    388 /*
    389  * Do we need to notify the other side when I/O is possible?
    390  */
    391 static __inline int
    392 sb_notify(struct sockbuf *sb)
    393 {
    394 
    395 	KASSERT(solocked(sb->sb_so));
    396 
    397 	return sb->sb_flags & (SB_NOTIFY | SB_ASYNC | SB_UPCALL | SB_KNOTE);
    398 }
    399 
    400 /*
    401  * How much space is there in a socket buffer (so->so_snd or so->so_rcv)?
    402  * Since the fields are unsigned, detect overflow and return 0.
    403  */
    404 static __inline u_long
    405 sbspace(const struct sockbuf *sb)
    406 {
    407 
    408 	KASSERT(solocked(sb->sb_so));
    409 	if (sb->sb_hiwat <= sb->sb_cc || sb->sb_mbmax <= sb->sb_mbcnt)
    410 		return 0;
    411 	return lmin(sb->sb_hiwat - sb->sb_cc, sb->sb_mbmax - sb->sb_mbcnt);
    412 }
    413 
    414 static __inline u_long
    415 sbspace_oob(const struct sockbuf *sb)
    416 {
    417 	u_long hiwat = sb->sb_hiwat;
    418 
    419 	if (hiwat < ULONG_MAX - 1024)
    420 		hiwat += 1024;
    421 
    422 	KASSERT(solocked(sb->sb_so));
    423 
    424 	if (hiwat <= sb->sb_cc || sb->sb_mbmax <= sb->sb_mbcnt)
    425 		return 0;
    426 	return lmin(hiwat - sb->sb_cc, sb->sb_mbmax - sb->sb_mbcnt);
    427 }
    428 
    429 /*
    430  * How much socket buffer space has been used?
    431  */
    432 static __inline u_long
    433 sbused(const struct sockbuf *sb)
    434 {
    435 
    436 	KASSERT(solocked(sb->sb_so));
    437 	return sb->sb_cc;
    438 }
    439 
    440 /* do we have to send all at once on a socket? */
    441 static __inline int
    442 sosendallatonce(const struct socket *so)
    443 {
    444 
    445 	return so->so_proto->pr_flags & PR_ATOMIC;
    446 }
    447 
    448 /* can we read something from so? */
    449 static __inline int
    450 soreadable(const struct socket *so)
    451 {
    452 
    453 	KASSERT(solocked(so));
    454 
    455 	return so->so_rcv.sb_cc >= so->so_rcv.sb_lowat ||
    456 	    (so->so_state & SS_CANTRCVMORE) != 0 ||
    457 	    so->so_qlen != 0 || so->so_error != 0 || so->so_rerror != 0;
    458 }
    459 
    460 /* can we write something to so? */
    461 static __inline int
    462 sowritable(const struct socket *so)
    463 {
    464 
    465 	KASSERT(solocked(so));
    466 
    467 	return (sbspace(&so->so_snd) >= so->so_snd.sb_lowat &&
    468 	    ((so->so_state & SS_ISCONNECTED) != 0 ||
    469 	    (so->so_proto->pr_flags & PR_CONNREQUIRED) == 0)) ||
    470 	    (so->so_state & SS_CANTSENDMORE) != 0 ||
    471 	    so->so_error != 0;
    472 }
    473 
    474 /* adjust counters in sb reflecting allocation of m */
    475 static __inline void
    476 sballoc(struct sockbuf *sb, struct mbuf *m)
    477 {
    478 
    479 	KASSERT(solocked(sb->sb_so));
    480 
    481 	sb->sb_cc += m->m_len;
    482 	sb->sb_mbcnt += MSIZE;
    483 	if (m->m_flags & M_EXT)
    484 		sb->sb_mbcnt += m->m_ext.ext_size;
    485 }
    486 
    487 /* adjust counters in sb reflecting freeing of m */
    488 static __inline void
    489 sbfree(struct sockbuf *sb, struct mbuf *m)
    490 {
    491 
    492 	KASSERT(solocked(sb->sb_so));
    493 
    494 	sb->sb_cc -= m->m_len;
    495 	sb->sb_mbcnt -= MSIZE;
    496 	if (m->m_flags & M_EXT)
    497 		sb->sb_mbcnt -= m->m_ext.ext_size;
    498 }
    499 
    500 static __inline void
    501 sorwakeup(struct socket *so)
    502 {
    503 
    504 	KASSERT(solocked(so));
    505 
    506 	if (sb_notify(&so->so_rcv))
    507 		sowakeup(so, &so->so_rcv, POLL_IN);
    508 }
    509 
    510 static __inline void
    511 sowwakeup(struct socket *so)
    512 {
    513 
    514 	KASSERT(solocked(so));
    515 
    516 	if (sb_notify(&so->so_snd))
    517 		sowakeup(so, &so->so_snd, POLL_OUT);
    518 }
    519 
    520 static __inline void
    521 solock(struct socket *so)
    522 {
    523 	kmutex_t *lock;
    524 
    525 	lock = atomic_load_consume(&so->so_lock);
    526 	mutex_enter(lock);
    527 	if (__predict_false(lock != atomic_load_relaxed(&so->so_lock)))
    528 		solockretry(so, lock);
    529 }
    530 
    531 static __inline void
    532 sounlock(struct socket *so)
    533 {
    534 
    535 	mutex_exit(so->so_lock);
    536 }
    537 
    538 #ifdef SOCKBUF_DEBUG
    539 /*
    540  * SBLASTRECORDCHK: check sb->sb_lastrecord is maintained correctly.
    541  * SBLASTMBUFCHK: check sb->sb_mbtail is maintained correctly.
    542  *
    543  * => panic if the socket buffer is inconsistent.
    544  * => 'where' is used for a panic message.
    545  */
    546 void	sblastrecordchk(struct sockbuf *, const char *);
    547 #define	SBLASTRECORDCHK(sb, where)	sblastrecordchk((sb), (where))
    548 
    549 void	sblastmbufchk(struct sockbuf *, const char *);
    550 #define	SBLASTMBUFCHK(sb, where)	sblastmbufchk((sb), (where))
    551 #define	SBCHECK(sb)			sbcheck(sb)
    552 #else
    553 #define	SBLASTRECORDCHK(sb, where)	/* nothing */
    554 #define	SBLASTMBUFCHK(sb, where)	/* nothing */
    555 #define	SBCHECK(sb)			/* nothing */
    556 #endif /* SOCKBUF_DEBUG */
    557 
    558 /* sosend loan */
    559 vaddr_t	sokvaalloc(vaddr_t, vsize_t, struct socket *);
    560 void	sokvafree(vaddr_t, vsize_t);
    561 void	soloanfree(struct mbuf *, void *, size_t, void *);
    562 
    563 /*
    564  * Values for socket-buffer-append priority argument to sbappendaddrchain().
    565  * The following flags are reserved for future implementation:
    566  *
    567  *  SB_PRIO_NONE:  honour normal socket-buffer limits.
    568  *
    569  *  SB_PRIO_ONESHOT_OVERFLOW:  if the socket has any space,
    570  *	deliver the entire chain. Intended for large requests
    571  *      that should be delivered in their entirety, or not at all.
    572  *
    573  * SB_PRIO_OVERDRAFT:  allow a small (2*MLEN) overflow, over and
    574  *	above normal socket limits. Intended messages indicating
    575  *      buffer overflow in earlier normal/lower-priority messages .
    576  *
    577  * SB_PRIO_BESTEFFORT: Ignore  limits entirely.  Intended only for
    578  * 	kernel-generated messages to specially-marked sockets which
    579  *	require "reliable" delivery, nd where the source socket/protocol
    580  *	message generator enforce some hard limit (but possibly well
    581  *	above kern.sbmax). It is entirely up to the in-kernel source to
    582  *	avoid complete mbuf exhaustion or DoS scenarios.
    583  */
    584 #define SB_PRIO_NONE 	 	0
    585 #define SB_PRIO_ONESHOT_OVERFLOW 1
    586 #define SB_PRIO_OVERDRAFT	2
    587 #define SB_PRIO_BESTEFFORT	3
    588 
    589 /*
    590  * Accept filter functions (duh).
    591  */
    592 int	accept_filt_getopt(struct socket *, struct sockopt *);
    593 int	accept_filt_setopt(struct socket *, const struct sockopt *);
    594 int	accept_filt_clear(struct socket *);
    595 int	accept_filt_add(struct accept_filter *);
    596 int	accept_filt_del(struct accept_filter *);
    597 struct	accept_filter *accept_filt_get(char *);
    598 #ifdef ACCEPT_FILTER_MOD
    599 #ifdef SYSCTL_DECL
    600 SYSCTL_DECL(_net_inet_accf);
    601 #endif
    602 void	accept_filter_init(void);
    603 #endif
    604 #ifdef DDB
    605 int sofindproc(struct socket *so, int all, void (*pr)(const char *, ...));
    606 void socket_print(const char *modif, void (*pr)(const char *, ...));
    607 #endif
    608 
    609 #endif /* _KERNEL */
    610 
    611 #endif /* !_SYS_SOCKETVAR_H_ */
    612