• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  */
30 
31 /* __Userspace__ version of <sys/socketvar.h> goes here.*/
32 
33 #ifndef _USER_SOCKETVAR_H_
34 #define _USER_SOCKETVAR_H_
35 
36 #if defined(__Userspace_os_Darwin)
37 #include <sys/types.h>
38 #include <unistd.h>
39 #endif
40 
41 /* #include <sys/selinfo.h> */ /*__Userspace__ alternative?*/	/* for struct selinfo */
42 /* #include <sys/_lock.h>  was 0 byte file */
43 /* #include <sys/_mutex.h> was 0 byte file */
44 /* #include <sys/_sx.h> */ /*__Userspace__ alternative?*/
45 #if !defined(__Userspace_os_DragonFly) && !defined(__Userspace_os_FreeBSD) && !defined(__Userspace_os_NetBSD) && !defined(__Userspace_os_Windows) && !defined(__Userspace_os_NaCl)
46 #include <sys/uio.h>
47 #endif
48 #define SOCK_MAXADDRLEN 255
49 #if !defined(MSG_NOTIFICATION)
50 #define MSG_NOTIFICATION 0x2000         /* SCTP notification */
51 #endif
52 #define SCTP_SO_LINGER     0x0001
53 #define SCTP_SO_ACCEPTCONN 0x0002
54 #define SS_CANTRCVMORE 0x020
55 #define SS_CANTSENDMORE 0x010
56 
57 #if defined(__Userspace_os_Darwin) || defined(__Userspace_os_DragonFly) || defined(__Userspace_os_FreeBSD) || defined(__Userspace_os_OpenBSD) || defined (__Userspace_os_Windows) || defined(__Userspace_os_NaCl)
58 #define UIO_MAXIOV 1024
59 #define ERESTART (-1)
60 #endif
61 
62 #if !defined(__Userspace_os_Darwin) && !defined(__Userspace_os_NetBSD) && !defined(__Userspace_os_OpenBSD)
63 enum	uio_rw { UIO_READ, UIO_WRITE };
64 #endif
65 
66 #if !defined(__Userspace_os_NetBSD) && !defined(__Userspace_os_OpenBSD)
67 /* Segment flag values. */
68 enum uio_seg {
69 	UIO_USERSPACE,		/* from user data space */
70 	UIO_SYSSPACE		/* from system space */
71 };
72 #endif
73 
74 struct proc {
75 	int stub; /* struct proc is a dummy for __Userspace__ */
76 };
77 
78 MALLOC_DECLARE(M_ACCF);
79 MALLOC_DECLARE(M_PCB);
80 MALLOC_DECLARE(M_SONAME);
81 
82 /* __Userspace__ Are these all the fields we need?
83  * Removing struct thread *uio_td;    owner field
84 */
85 struct uio {
86 	struct	iovec *uio_iov;		/* scatter/gather list */
87 	int		uio_iovcnt;		/* length of scatter/gather list */
88 	off_t	uio_offset;		/* offset in target object */
89 	ssize_t 	uio_resid;		/* remaining bytes to process */
90 	enum	uio_seg uio_segflg;	/* address space */
91 	enum	uio_rw uio_rw;		/* operation */
92 };
93 
94 
95 /* __Userspace__ */
96 
97 /*
98  * Kernel structure per socket.
99  * Contains send and receive buffer queues,
100  * handle on protocol and pointer to protocol
101  * private data and error information.
102  */
103 #if defined (__Userspace_os_Windows)
104 #define AF_ROUTE  17
105 #if !defined(__MINGW32__)
106 typedef __int32 pid_t;
107 #endif
108 typedef unsigned __int32 uid_t;
109 enum sigType {
110 	SIGNAL = 0,
111 	BROADCAST = 1,
112 	MAX_EVENTS = 2
113 };
114 #endif
115 
116 /*-
117  * Locking key to struct socket:
118  * (a) constant after allocation, no locking required.
119  * (b) locked by SOCK_LOCK(so).
120  * (c) locked by SOCKBUF_LOCK(&so->so_rcv).
121  * (d) locked by SOCKBUF_LOCK(&so->so_snd).
122  * (e) locked by ACCEPT_LOCK().
123  * (f) not locked since integer reads/writes are atomic.
124  * (g) used only as a sleep/wakeup address, no value.
125  * (h) locked by global mutex so_global_mtx.
126  */
127 struct socket {
128 	int	so_count;		/* (b) reference count */
129 	short	so_type;		/* (a) generic type, see socket.h */
130 	short	so_options;		/* from socket call, see socket.h */
131 	short	so_linger;		/* time to linger while closing */
132 	short	so_state;		/* (b) internal state flags SS_* */
133 	int	so_qstate;		/* (e) internal state flags SQ_* */
134 	void	*so_pcb;		/* protocol control block */
135 	int	so_dom;
136 /*
137  * Variables for connection queuing.
138  * Socket where accepts occur is so_head in all subsidiary sockets.
139  * If so_head is 0, socket is not related to an accept.
140  * For head socket so_incomp queues partially completed connections,
141  * while so_comp is a queue of connections ready to be accepted.
142  * If a connection is aborted and it has so_head set, then
143  * it has to be pulled out of either so_incomp or so_comp.
144  * We allow connections to queue up based on current queue lengths
145  * and limit on number of queued connections for this socket.
146  */
147 	struct	socket *so_head;	/* (e) back pointer to listen socket */
148 	TAILQ_HEAD(, socket) so_incomp;	/* (e) queue of partial unaccepted connections */
149 	TAILQ_HEAD(, socket) so_comp;	/* (e) queue of complete unaccepted connections */
150 	TAILQ_ENTRY(socket) so_list;	/* (e) list of unaccepted connections */
151 	u_short	so_qlen;		/* (e) number of unaccepted connections */
152 	u_short	so_incqlen;		/* (e) number of unaccepted incomplete
153 					   connections */
154 	u_short	so_qlimit;		/* (e) max number queued connections */
155 	short	so_timeo;		/* (g) connection timeout */
156 	userland_cond_t timeo_cond;      /* timeo_cond condition variable being used in wakeup */
157 
158 	u_short	so_error;		/* (f) error affecting connection */
159 	struct	sigio *so_sigio;	/* [sg] information for async I/O or
160 					   out of band data (SIGURG) */
161 	u_long	so_oobmark;		/* (c) chars to oob mark */
162 	TAILQ_HEAD(, aiocblist) so_aiojobq; /* AIO ops waiting on socket */
163 /*
164  * Variables for socket buffering.
165  */
166 	struct sockbuf {
167 		/* __Userspace__ Many of these fields may
168 		 * not be required for the sctp stack.
169 		 * Commenting out the following.
170 		 * Including pthread mutex and condition variable to be
171 		 * used by sbwait, sorwakeup and sowwakeup.
172 		*/
173 		/* struct	selinfo sb_sel;*/ /* process selecting read/write */
174 		/* struct	mtx sb_mtx;*/	/* sockbuf lock */
175 		/* struct	sx sb_sx;*/	/* prevent I/O interlacing */
176 		userland_cond_t sb_cond; /* sockbuf condition variable */
177 		userland_mutex_t sb_mtx; /* sockbuf lock associated with sb_cond */
178 		short	sb_state;	/* (c/d) socket state on sockbuf */
179 #define	sb_startzero	sb_mb
180 		struct	mbuf *sb_mb;	/* (c/d) the mbuf chain */
181 		struct	mbuf *sb_mbtail; /* (c/d) the last mbuf in the chain */
182 		struct	mbuf *sb_lastrecord;	/* (c/d) first mbuf of last
183 						 * record in socket buffer */
184 		struct	mbuf *sb_sndptr; /* (c/d) pointer into mbuf chain */
185 		u_int	sb_sndptroff;	/* (c/d) byte offset of ptr into chain */
186 		u_int	sb_cc;		/* (c/d) actual chars in buffer */
187 		u_int	sb_hiwat;	/* (c/d) max actual char count */
188 		u_int	sb_mbcnt;	/* (c/d) chars of mbufs used */
189 		u_int	sb_mbmax;	/* (c/d) max chars of mbufs to use */
190 		u_int	sb_ctl;		/* (c/d) non-data chars in buffer */
191 		int	sb_lowat;	/* (c/d) low water mark */
192 		int	sb_timeo;	/* (c/d) timeout for read/write */
193 		short	sb_flags;	/* (c/d) flags, see below */
194 	} so_rcv, so_snd;
195 /*
196  * Constants for sb_flags field of struct sockbuf.
197  */
198 #define	SB_MAX		(256*1024)	/* default for max chars in sockbuf */
199 #define SB_RAW          (64*1024*2)    /*Aligning so->so_rcv.sb_hiwat with the receive buffer size of raw socket*/
200 /*
201  * Constants for sb_flags field of struct sockbuf.
202  */
203 #define	SB_WAIT		0x04		/* someone is waiting for data/space */
204 #define	SB_SEL		0x08		/* someone is selecting */
205 #define	SB_ASYNC	0x10		/* ASYNC I/O, need signals */
206 #define	SB_UPCALL	0x20		/* someone wants an upcall */
207 #define	SB_NOINTR	0x40		/* operations not interruptible */
208 #define	SB_AIO		0x80		/* AIO operations queued */
209 #define	SB_KNOTE	0x100		/* kernel note attached */
210 #define	SB_AUTOSIZE	0x800		/* automatically size socket buffer */
211 
212 	void	(*so_upcall)(struct socket *, void *, int);
213 	void	*so_upcallarg;
214 	struct	ucred *so_cred;		/* (a) user credentials */
215 	struct	label *so_label;	/* (b) MAC label for socket */
216 	struct	label *so_peerlabel;	/* (b) cached MAC label for peer */
217 	/* NB: generation count must not be first. */
218 	uint32_t so_gencnt;		/* (h) generation count */
219 	void	*so_emuldata;		/* (b) private data for emulators */
220  	struct so_accf {
221 		struct	accept_filter *so_accept_filter;
222 		void	*so_accept_filter_arg;	/* saved filter args */
223 		char	*so_accept_filter_str;	/* saved user args */
224 	} *so_accf;
225 };
226 
227 #define SB_EMPTY_FIXUP(sb) do {						\
228 	if ((sb)->sb_mb == NULL) {					\
229 		(sb)->sb_mbtail = NULL;					\
230 		(sb)->sb_lastrecord = NULL;				\
231 	}								\
232 } while (/*CONSTCOND*/0)
233 
234 /*
235  * Global accept mutex to serialize access to accept queues and
236  * fields associated with multiple sockets.  This allows us to
237  * avoid defining a lock order between listen and accept sockets
238  * until such time as it proves to be a good idea.
239  */
240 #if defined(__Userspace_os_Windows)
241 extern userland_mutex_t accept_mtx;
242 extern userland_cond_t accept_cond;
243 #define ACCEPT_LOCK_ASSERT()
244 #define	ACCEPT_LOCK() do { \
245 	EnterCriticalSection(&accept_mtx); \
246 } while (0)
247 #define	ACCEPT_UNLOCK()	do { \
248 	LeaveCriticalSection(&accept_mtx); \
249 } while (0)
250 #define	ACCEPT_UNLOCK_ASSERT()
251 #else
252 extern userland_mutex_t accept_mtx;
253 
254 extern userland_cond_t accept_cond;
255 #ifdef INVARIANTS
256 #define	ACCEPT_LOCK()	KASSERT(pthread_mutex_lock(&accept_mtx) == 0, ("%s: accept_mtx already locked", __func__))
257 #define	ACCEPT_UNLOCK()	KASSERT(pthread_mutex_unlock(&accept_mtx) == 0, ("%s: accept_mtx not locked", __func__))
258 #else
259 #define	ACCEPT_LOCK()   (void)pthread_mutex_lock(&accept_mtx)
260 #define	ACCEPT_UNLOCK() (void)pthread_mutex_unlock(&accept_mtx)
261 #endif
262 #define	ACCEPT_LOCK_ASSERT() \
263           KASSERT(pthread_mutex_trylock(&accept_mtx) == EBUSY, ("%s: accept_mtx not locked", __func__))
264 #define	ACCEPT_UNLOCK_ASSERT() do {                                                               \
265 	  KASSERT(pthread_mutex_trylock(&accept_mtx) == 0, ("%s: accept_mtx  locked", __func__)); \
266 	  (void)pthread_mutex_unlock(&accept_mtx);                                                \
267         } while (0)
268 #endif
269 
270 /*
271  * Per-socket buffer mutex used to protect most fields in the socket
272  * buffer.
273  */
274 #define	SOCKBUF_MTX(_sb) (&(_sb)->sb_mtx)
275 #if defined (__Userspace_os_Windows)
276 #define SOCKBUF_LOCK_INIT(_sb, _name) \
277 	InitializeCriticalSection(SOCKBUF_MTX(_sb))
278 #define SOCKBUF_LOCK_DESTROY(_sb) DeleteCriticalSection(SOCKBUF_MTX(_sb))
279 #define SOCKBUF_COND_INIT(_sb) InitializeConditionVariable((&(_sb)->sb_cond))
280 #define SOCKBUF_COND_DESTROY(_sb) DeleteConditionVariable((&(_sb)->sb_cond))
281 #define SOCK_COND_INIT(_so) InitializeConditionVariable((&(_so)->timeo_cond))
282 #define SOCK_COND_DESTROY(_so) DeleteConditionVariable((&(_so)->timeo_cond))
283 #define SOCK_COND(_so) (&(_so)->timeo_cond)
284 #else
285 #ifdef INVARIANTS
286 #define SOCKBUF_LOCK_INIT(_sb, _name) do {                                 \
287 	pthread_mutexattr_t mutex_attr;                                    \
288 	                                                                   \
289 	pthread_mutexattr_init(&mutex_attr);                               \
290 	pthread_mutexattr_settype(&mutex_attr, PTHREAD_MUTEX_ERRORCHECK);  \
291 	pthread_mutex_init(SOCKBUF_MTX(_sb), &mutex_attr);                 \
292 	pthread_mutexattr_destroy(&mutex_attr);                            \
293 } while (0)
294 #else
295 #define SOCKBUF_LOCK_INIT(_sb, _name) \
296 	pthread_mutex_init(SOCKBUF_MTX(_sb), NULL)
297 #endif
298 #define SOCKBUF_LOCK_DESTROY(_sb) pthread_mutex_destroy(SOCKBUF_MTX(_sb))
299 #define SOCKBUF_COND_INIT(_sb) pthread_cond_init((&(_sb)->sb_cond), NULL)
300 #define SOCKBUF_COND_DESTROY(_sb) pthread_cond_destroy((&(_sb)->sb_cond))
301 #define SOCK_COND_INIT(_so) pthread_cond_init((&(_so)->timeo_cond), NULL)
302 #define SOCK_COND_DESTROY(_so) pthread_cond_destroy((&(_so)->timeo_cond))
303 #define SOCK_COND(_so) (&(_so)->timeo_cond)
304 #endif
305 /*__Userspace__ SOCKBUF_LOCK(_sb) is now defined in netinet/sctp_process_lock.h */
306 
307 /* #define	SOCKBUF_OWNED(_sb)		mtx_owned(SOCKBUF_MTX(_sb)) unused */
308 /*__Userspace__ SOCKBUF_UNLOCK(_sb) is now defined in netinet/sctp_process_lock.h */
309 
310 /*__Userspace__ SOCKBUF_LOCK_ASSERT(_sb) is now defined in netinet/sctp_process_lock.h */
311 
312 /* #define	SOCKBUF_UNLOCK_ASSERT(_sb)	mtx_assert(SOCKBUF_MTX(_sb), MA_NOTOWNED)   unused */
313 
314 /*
315  * Per-socket mutex: we reuse the receive socket buffer mutex for space
316  * efficiency.  This decision should probably be revisited as we optimize
317  * locking for the socket code.
318  */
319 #define	SOCK_MTX(_so)			SOCKBUF_MTX(&(_so)->so_rcv)
320 /*__Userspace__ SOCK_LOCK(_so) is now defined in netinet/sctp_process_lock.h */
321 
322 /* #define	SOCK_OWNED(_so)			SOCKBUF_OWNED(&(_so)->so_rcv) unused */
323 /*__Userspace__ SOCK_UNLOCK(_so) is now defined in netinet/sctp_process_lock.h */
324 
325 #define	SOCK_LOCK_ASSERT(_so)		SOCKBUF_LOCK_ASSERT(&(_so)->so_rcv)
326 
327 /*
328  * Socket state bits.
329  *
330  * Historically, this bits were all kept in the so_state field.  For
331  * locking reasons, they are now in multiple fields, as they are
332  * locked differently.  so_state maintains basic socket state protected
333  * by the socket lock.  so_qstate holds information about the socket
334  * accept queues.  Each socket buffer also has a state field holding
335  * information relevant to that socket buffer (can't send, rcv).  Many
336  * fields will be read without locks to improve performance and avoid
337  * lock order issues.  However, this approach must be used with caution.
338  */
339 #define	SS_NOFDREF		0x0001	/* no file table ref any more */
340 #define	SS_ISCONNECTED		0x0002	/* socket connected to a peer */
341 #define	SS_ISCONNECTING		0x0004	/* in process of connecting to peer */
342 #define	SS_ISDISCONNECTING	0x0008	/* in process of disconnecting */
343 #define	SS_NBIO			0x0100	/* non-blocking ops */
344 #define	SS_ASYNC		0x0200	/* async i/o notify */
345 #define	SS_ISCONFIRMING		0x0400	/* deciding to accept connection req */
346 #define	SS_ISDISCONNECTED	0x2000	/* socket disconnected from peer */
347 /*
348  * Protocols can mark a socket as SS_PROTOREF to indicate that, following
349  * pru_detach, they still want the socket to persist, and will free it
350  * themselves when they are done.  Protocols should only ever call sofree()
351  * following setting this flag in pru_detach(), and never otherwise, as
352  * sofree() bypasses socket reference counting.
353  */
354 #define	SS_PROTOREF		0x4000	/* strong protocol reference */
355 
356 /*
357  * Socket state bits now stored in the socket buffer state field.
358  */
359 #define	SBS_CANTSENDMORE	0x0010	/* can't send more data to peer */
360 #define	SBS_CANTRCVMORE		0x0020	/* can't receive more data from peer */
361 #define	SBS_RCVATMARK		0x0040	/* at mark on input */
362 
363 /*
364  * Socket state bits stored in so_qstate.
365  */
366 #define	SQ_INCOMP		0x0800	/* unaccepted, incomplete connection */
367 #define	SQ_COMP			0x1000	/* unaccepted, complete connection */
368 
369 /*
370  * Socket event flags
371  */
372 #define SCTP_EVENT_READ		0x0001	/* socket is readable */
373 #define SCTP_EVENT_WRITE	0x0002	/* socket is writeable */
374 #define SCTP_EVENT_ERROR	0x0004	/* socket has an error state */
375 
376 /*
377  * Externalized form of struct socket used by the sysctl(3) interface.
378  */
379 struct xsocket {
380 	size_t	xso_len;	/* length of this structure */
381 	struct	socket *xso_so;	/* makes a convenient handle sometimes */
382 	short	so_type;
383 	short	so_options;
384 	short	so_linger;
385 	short	so_state;
386 	caddr_t	so_pcb;		/* another convenient handle */
387 	int	xso_protocol;
388 	int	xso_family;
389 	u_short	so_qlen;
390 	u_short	so_incqlen;
391 	u_short	so_qlimit;
392 	short	so_timeo;
393 	u_short	so_error;
394 	pid_t	so_pgid;
395 	u_long	so_oobmark;
396 	struct xsockbuf {
397 		u_int	sb_cc;
398 		u_int	sb_hiwat;
399 		u_int	sb_mbcnt;
400 		u_int	sb_mbmax;
401 		int	sb_lowat;
402 		int	sb_timeo;
403 		short	sb_flags;
404 	} so_rcv, so_snd;
405 	uid_t	so_uid;		/* XXX */
406 };
407 
408 #if defined(_KERNEL)
409 
410 
411 /*
412  * Macros for sockets and socket buffering.
413  */
414 
415 /*
416  * Do we need to notify the other side when I/O is possible?
417  */
418 #define	sb_notify(sb)	(((sb)->sb_flags & (SB_WAIT | SB_SEL | SB_ASYNC | \
419     SB_UPCALL | SB_AIO | SB_KNOTE)) != 0)
420 
421 /*
422  * How much space is there in a socket buffer (so->so_snd or so->so_rcv)?
423  * This is problematical if the fields are unsigned, as the space might
424  * still be negative (cc > hiwat or mbcnt > mbmax).  Should detect
425  * overflow and return 0.  Should use "lmin" but it doesn't exist now.
426  */
427 #define	sbspace(sb) \
428     ((long) imin((int)((sb)->sb_hiwat - (sb)->sb_cc), \
429 	 (int)((sb)->sb_mbmax - (sb)->sb_mbcnt)))
430 
431 /* do we have to send all at once on a socket? */
432 #define	sosendallatonce(so) \
433     ((so)->so_proto->pr_flags & PR_ATOMIC)
434 
435 /* can we read something from so? */
436 #define	soreadable(so) \
437     ((so)->so_rcv.sb_cc >= (so)->so_rcv.sb_lowat || \
438 	((so)->so_rcv.sb_state & SBS_CANTRCVMORE) || \
439 	!TAILQ_EMPTY(&(so)->so_comp) || (so)->so_error)
440 
441 /* can we write something to so? */
442 #define	sowriteable(so) \
443     ((sbspace(&(so)->so_snd) >= (so)->so_snd.sb_lowat && \
444 	(((so)->so_state&SS_ISCONNECTED) || \
445 	  ((so)->so_proto->pr_flags&PR_CONNREQUIRED)==0)) || \
446      ((so)->so_snd.sb_state & SBS_CANTSENDMORE) || \
447      (so)->so_error)
448 
449 /* adjust counters in sb reflecting allocation of m */
450 #define	sballoc(sb, m) { \
451 	(sb)->sb_cc += (m)->m_len; \
452 	if ((m)->m_type != MT_DATA && (m)->m_type != MT_OOBDATA) \
453 		(sb)->sb_ctl += (m)->m_len; \
454 	(sb)->sb_mbcnt += MSIZE; \
455 	if ((m)->m_flags & M_EXT) \
456 		(sb)->sb_mbcnt += (m)->m_ext.ext_size; \
457 }
458 
459 /* adjust counters in sb reflecting freeing of m */
460 #define	sbfree(sb, m) { \
461 	(sb)->sb_cc -= (m)->m_len; \
462 	if ((m)->m_type != MT_DATA && (m)->m_type != MT_OOBDATA) \
463 		(sb)->sb_ctl -= (m)->m_len; \
464 	(sb)->sb_mbcnt -= MSIZE; \
465 	if ((m)->m_flags & M_EXT) \
466 		(sb)->sb_mbcnt -= (m)->m_ext.ext_size; \
467 	if ((sb)->sb_sndptr == (m)) { \
468 		(sb)->sb_sndptr = NULL; \
469 		(sb)->sb_sndptroff = 0; \
470 	} \
471 	if ((sb)->sb_sndptroff != 0) \
472 		(sb)->sb_sndptroff -= (m)->m_len; \
473 }
474 
475 /*
476  * soref()/sorele() ref-count the socket structure.  Note that you must
477  * still explicitly close the socket, but the last ref count will free
478  * the structure.
479  */
480 #define	soref(so) do {							\
481 	SOCK_LOCK_ASSERT(so);						\
482 	++(so)->so_count;						\
483 } while (0)
484 
485 #define	sorele(so) do {							\
486 	ACCEPT_LOCK_ASSERT();						\
487 	SOCK_LOCK_ASSERT(so);						\
488 	KASSERT((so)->so_count > 0, ("sorele"));			\
489 	if (--(so)->so_count == 0)					\
490 		sofree(so);						\
491 	else {								\
492 		SOCK_UNLOCK(so);					\
493 		ACCEPT_UNLOCK();					\
494 	}								\
495 } while (0)
496 
497 #define	sotryfree(so) do {						\
498 	ACCEPT_LOCK_ASSERT();						\
499 	SOCK_LOCK_ASSERT(so);						\
500 	if ((so)->so_count == 0)					\
501 		sofree(so);						\
502 	else {								\
503 		SOCK_UNLOCK(so);					\
504 		ACCEPT_UNLOCK();					\
505 	}								\
506 } while(0)
507 
508 /*
509  * In sorwakeup() and sowwakeup(), acquire the socket buffer lock to
510  * avoid a non-atomic test-and-wakeup.  However, sowakeup is
511  * responsible for releasing the lock if it is called.  We unlock only
512  * if we don't call into sowakeup.  If any code is introduced that
513  * directly invokes the underlying sowakeup() primitives, it must
514  * maintain the same semantics.
515  */
516 #define	sorwakeup_locked(so) do {					\
517 	SOCKBUF_LOCK_ASSERT(&(so)->so_rcv);				\
518 	if (sb_notify(&(so)->so_rcv))					\
519 		sowakeup((so), &(so)->so_rcv);	 			\
520 	else								\
521 		SOCKBUF_UNLOCK(&(so)->so_rcv);				\
522 } while (0)
523 
524 #define	sorwakeup(so) do {						\
525 	SOCKBUF_LOCK(&(so)->so_rcv);					\
526 	sorwakeup_locked(so);						\
527 } while (0)
528 
529 #define	sowwakeup_locked(so) do {					\
530 	SOCKBUF_LOCK_ASSERT(&(so)->so_snd);				\
531 	if (sb_notify(&(so)->so_snd))					\
532 		sowakeup((so), &(so)->so_snd); 				\
533 	else								\
534 		SOCKBUF_UNLOCK(&(so)->so_snd);				\
535 } while (0)
536 
537 #define	sowwakeup(so) do {						\
538 	SOCKBUF_LOCK(&(so)->so_snd);					\
539 	sowwakeup_locked(so);						\
540 } while (0)
541 
542 /*
543  * Argument structure for sosetopt et seq.  This is in the KERNEL
544  * section because it will never be visible to user code.
545  */
546 enum sopt_dir { SOPT_GET, SOPT_SET };
547 struct sockopt {
548 	enum	sopt_dir sopt_dir; /* is this a get or a set? */
549 	int	sopt_level;	/* second arg of [gs]etsockopt */
550 	int	sopt_name;	/* third arg of [gs]etsockopt */
551 	void   *sopt_val;	/* fourth arg of [gs]etsockopt */
552 	size_t	sopt_valsize;	/* (almost) fifth arg of [gs]etsockopt */
553 	struct	thread *sopt_td; /* calling thread or null if kernel */
554 };
555 
556 struct accept_filter {
557 	char	accf_name[16];
558 	void	(*accf_callback)
559 		(struct socket *so, void *arg, int waitflag);
560 	void *	(*accf_create)
561 		(struct socket *so, char *arg);
562 	void	(*accf_destroy)
563 		(struct socket *so);
564 	SLIST_ENTRY(accept_filter) accf_next;
565 };
566 
567 extern int	maxsockets;
568 extern u_long	sb_max;
569 extern struct uma_zone *socket_zone;
570 extern so_gen_t so_gencnt;
571 
572 struct mbuf;
573 struct sockaddr;
574 struct ucred;
575 struct uio;
576 
577 /*
578  * From uipc_socket and friends
579  */
580 int	do_getopt_accept_filter(struct socket *so, struct sockopt *sopt);
581 int	do_setopt_accept_filter(struct socket *so, struct sockopt *sopt);
582 int	so_setsockopt(struct socket *so, int level, int optname,
583 	    void *optval, size_t optlen);
584 int	sockargs(struct mbuf **mp, caddr_t buf, int buflen, int type);
585 int	getsockaddr(struct sockaddr **namp, caddr_t uaddr, size_t len);
586 void	sbappend(struct sockbuf *sb, struct mbuf *m);
587 void	sbappend_locked(struct sockbuf *sb, struct mbuf *m);
588 void	sbappendstream(struct sockbuf *sb, struct mbuf *m);
589 void	sbappendstream_locked(struct sockbuf *sb, struct mbuf *m);
590 int	sbappendaddr(struct sockbuf *sb, const struct sockaddr *asa,
591 	    struct mbuf *m0, struct mbuf *control);
592 int	sbappendaddr_locked(struct sockbuf *sb, const struct sockaddr *asa,
593 	    struct mbuf *m0, struct mbuf *control);
594 int	sbappendcontrol(struct sockbuf *sb, struct mbuf *m0,
595 	    struct mbuf *control);
596 int	sbappendcontrol_locked(struct sockbuf *sb, struct mbuf *m0,
597 	    struct mbuf *control);
598 void	sbappendrecord(struct sockbuf *sb, struct mbuf *m0);
599 void	sbappendrecord_locked(struct sockbuf *sb, struct mbuf *m0);
600 void	sbcheck(struct sockbuf *sb);
601 void	sbcompress(struct sockbuf *sb, struct mbuf *m, struct mbuf *n);
602 struct mbuf *
603 	sbcreatecontrol(caddr_t p, int size, int type, int level);
604 void	sbdestroy(struct sockbuf *sb, struct socket *so);
605 void	sbdrop(struct sockbuf *sb, int len);
606 void	sbdrop_locked(struct sockbuf *sb, int len);
607 void	sbdroprecord(struct sockbuf *sb);
608 void	sbdroprecord_locked(struct sockbuf *sb);
609 void	sbflush(struct sockbuf *sb);
610 void	sbflush_locked(struct sockbuf *sb);
611 void	sbrelease(struct sockbuf *sb, struct socket *so);
612 void	sbrelease_locked(struct sockbuf *sb, struct socket *so);
613 int	sbreserve(struct sockbuf *sb, u_long cc, struct socket *so,
614 	    struct thread *td);
615 int	sbreserve_locked(struct sockbuf *sb, u_long cc, struct socket *so,
616 	    struct thread *td);
617 struct mbuf *
618 	sbsndptr(struct sockbuf *sb, u_int off, u_int len, u_int *moff);
619 void	sbtoxsockbuf(struct sockbuf *sb, struct xsockbuf *xsb);
620 int	sbwait(struct sockbuf *sb);
621 int	sblock(struct sockbuf *sb, int flags);
622 void	sbunlock(struct sockbuf *sb);
623 void	soabort(struct socket *so);
624 int	soaccept(struct socket *so, struct sockaddr **nam);
625 int	socheckuid(struct socket *so, uid_t uid);
626 int	sobind(struct socket *so, struct sockaddr *nam, struct thread *td);
627 void	socantrcvmore(struct socket *so);
628 void	socantrcvmore_locked(struct socket *so);
629 void	socantsendmore(struct socket *so);
630 void	socantsendmore_locked(struct socket *so);
631 int	soclose(struct socket *so);
632 int	soconnect(struct socket *so, struct sockaddr *nam, struct thread *td);
633 int	soconnect2(struct socket *so1, struct socket *so2);
634 int	socow_setup(struct mbuf *m0, struct uio *uio);
635 int	socreate(int dom, struct socket **aso, int type, int proto,
636 	    struct ucred *cred, struct thread *td);
637 int	sodisconnect(struct socket *so);
638 struct	sockaddr *sodupsockaddr(const struct sockaddr *sa, int mflags);
639 void	sofree(struct socket *so);
640 int	sogetopt(struct socket *so, struct sockopt *sopt);
641 void	sohasoutofband(struct socket *so);
642 void	soisconnected(struct socket *so);
643 void	soisconnecting(struct socket *so);
644 void	soisdisconnected(struct socket *so);
645 void	soisdisconnecting(struct socket *so);
646 int	solisten(struct socket *so, int backlog, struct thread *td);
647 void	solisten_proto(struct socket *so, int backlog);
648 int	solisten_proto_check(struct socket *so);
649 struct socket *
650 	sonewconn(struct socket *head, int connstatus);
651 int	sooptcopyin(struct sockopt *sopt, void *buf, size_t len, size_t minlen);
652 int	sooptcopyout(struct sockopt *sopt, const void *buf, size_t len);
653 
654 /* XXX; prepare mbuf for (__FreeBSD__ < 3) routines. */
655 int	soopt_getm(struct sockopt *sopt, struct mbuf **mp);
656 int	soopt_mcopyin(struct sockopt *sopt, struct mbuf *m);
657 int	soopt_mcopyout(struct sockopt *sopt, struct mbuf *m);
658 
659 int	sopoll(struct socket *so, int events, struct ucred *active_cred,
660 	    struct thread *td);
661 int	sopoll_generic(struct socket *so, int events,
662 	    struct ucred *active_cred, struct thread *td);
663 int	soreceive(struct socket *so, struct sockaddr **paddr, struct uio *uio,
664 	    struct mbuf **mp0, struct mbuf **controlp, int *flagsp);
665 int	soreceive_generic(struct socket *so, struct sockaddr **paddr,
666 	    struct uio *uio, struct mbuf **mp0, struct mbuf **controlp,
667 	    int *flagsp);
668 int	soreserve(struct socket *so, u_long sndcc, u_long rcvcc);
669 void	sorflush(struct socket *so);
670 int	sosend(struct socket *so, struct sockaddr *addr, struct uio *uio,
671 	    struct mbuf *top, struct mbuf *control, int flags,
672 	    struct thread *td);
673 int	sosend_dgram(struct socket *so, struct sockaddr *addr,
674 	    struct uio *uio, struct mbuf *top, struct mbuf *control,
675 	    int flags, struct thread *td);
676 int	sosend_generic(struct socket *so, struct sockaddr *addr,
677 	    struct uio *uio, struct mbuf *top, struct mbuf *control,
678 	    int flags, struct thread *td);
679 int	sosetopt(struct socket *so, struct sockopt *sopt);
680 int	soshutdown(struct socket *so, int how);
681 void	sotoxsocket(struct socket *so, struct xsocket *xso);
682 void	sowakeup(struct socket *so, struct sockbuf *sb);
683 
684 #ifdef SOCKBUF_DEBUG
685 void	sblastrecordchk(struct sockbuf *, const char *, int);
686 #define	SBLASTRECORDCHK(sb)	sblastrecordchk((sb), __FILE__, __LINE__)
687 
688 void	sblastmbufchk(struct sockbuf *, const char *, int);
689 #define	SBLASTMBUFCHK(sb)	sblastmbufchk((sb), __FILE__, __LINE__)
690 #else
691 #define	SBLASTRECORDCHK(sb)      /* nothing */
692 #define	SBLASTMBUFCHK(sb)        /* nothing */
693 #endif /* SOCKBUF_DEBUG */
694 
695 /*
696  * Accept filter functions (duh).
697  */
698 int	accept_filt_add(struct accept_filter *filt);
699 int	accept_filt_del(char *name);
700 struct	accept_filter *accept_filt_get(char *name);
701 #ifdef ACCEPT_FILTER_MOD
702 #ifdef SYSCTL_DECL
703 SYSCTL_DECL(_net_inet_accf);
704 #endif
705 int	accept_filt_generic_mod_event(module_t mod, int event, void *data);
706 #endif
707 
708 #endif /* _KERNEL */
709 
710 
711 /*-------------------------------------------------------------*/
712 /*-------------------------------------------------------------*/
713 /*                   __Userspace__                             */
714 /*-------------------------------------------------------------*/
715 /*-------------------------------------------------------------*/
716 /* this new __Userspace__ section is to copy portions of the _KERNEL block
717  *  above into, avoiding having to port the entire thing at once...
718  *  For function prototypes, the full bodies are in user_socket.c .
719  */
720 #if defined(__Userspace__)
721 
722 /* ---------------------------------------------------------- */
723 /* --- function prototypes (implemented in user_socket.c) --- */
724 /* ---------------------------------------------------------- */
725 void	soisconnecting(struct socket *so);
726 void	soisdisconnecting(struct socket *so);
727 void	soisconnected(struct socket *so);
728 struct socket * sonewconn(struct socket *head, int connstatus);
729 void	socantrcvmore(struct socket *so);
730 void	socantsendmore(struct socket *so);
731 void	sofree(struct socket *so);
732 
733 
734 
735 /* -------------- */
736 /* --- macros --- */
737 /* -------------- */
738 
739 #define	soref(so) do {							\
740 	SOCK_LOCK_ASSERT(so);						\
741 	++(so)->so_count;						\
742 } while (0)
743 
744 #define	sorele(so) do {							\
745 	ACCEPT_LOCK_ASSERT();						\
746 	SOCK_LOCK_ASSERT(so);						\
747 	KASSERT((so)->so_count > 0, ("sorele"));			\
748 	if (--(so)->so_count == 0)					\
749 		sofree(so);						\
750 	else {								\
751 		SOCK_UNLOCK(so);					\
752 		ACCEPT_UNLOCK();					\
753 	}								\
754 } while (0)
755 
756 
757 /* replacing imin with min (user_environment.h) */
758 #define	sbspace(sb) \
759     ((long) min((int)((sb)->sb_hiwat - (sb)->sb_cc), \
760 	 (int)((sb)->sb_mbmax - (sb)->sb_mbcnt)))
761 
762 /* do we have to send all at once on a socket? */
763 #define	sosendallatonce(so) \
764     ((so)->so_proto->pr_flags & PR_ATOMIC)
765 
766 /* can we read something from so? */
767 #define	soreadable(so) \
768     ((int)((so)->so_rcv.sb_cc) >= (so)->so_rcv.sb_lowat || \
769 	((so)->so_rcv.sb_state & SBS_CANTRCVMORE) || \
770 	!TAILQ_EMPTY(&(so)->so_comp) || (so)->so_error)
771 
772 #if 0  /*  original */
773 #define PR_CONNREQUIRED 0x04  /* from sys/protosw.h "needed" for sowriteable */
774 #define	sowriteable(so) \
775     ((sbspace(&(so)->so_snd) >= (so)->so_snd.sb_lowat && \
776 	(((so)->so_state&SS_ISCONNECTED) || \
777 	  ((so)->so_proto->pr_flags&PR_CONNREQUIRED)==0)) || \
778      ((so)->so_snd.sb_state & SBS_CANTSENDMORE) || \
779      (so)->so_error)
780 #else  /* line with PR_CONNREQUIRED removed */
781 /* can we write something to so? */
782 #define	sowriteable(so) \
783     ((sbspace(&(so)->so_snd) >= (so)->so_snd.sb_lowat && \
784       (((so)->so_state&SS_ISCONNECTED))) ||              \
785      ((so)->so_snd.sb_state & SBS_CANTSENDMORE) || \
786      (so)->so_error)
787 #endif
788 
789 extern void solisten_proto(struct socket *so, int backlog);
790 extern int solisten_proto_check(struct socket *so);
791 extern int sctp_listen(struct socket *so, int backlog, struct proc *p);
792 extern void socantrcvmore_locked(struct socket *so);
793 extern int sctp_bind(struct socket *so, struct sockaddr *addr);
794 extern int sctp6_bind(struct socket *so, struct sockaddr *addr, void *proc);
795 #if defined(__Userspace__)
796 extern int sctpconn_bind(struct socket *so, struct sockaddr *addr);
797 #endif
798 extern int sctp_accept(struct socket *so, struct sockaddr **addr);
799 extern int sctp_attach(struct socket *so, int proto, uint32_t vrf_id);
800 extern int sctp6_attach(struct socket *so, int proto, uint32_t vrf_id);
801 extern int sctp_abort(struct socket *so);
802 extern int sctp6_abort(struct socket *so);
803 extern void sctp_close(struct socket *so);
804 extern int soaccept(struct socket *so, struct sockaddr **nam);
805 extern int solisten(struct socket *so, int backlog);
806 extern int  soreserve(struct socket *so, u_long sndcc, u_long rcvcc);
807 extern void sowakeup(struct socket *so, struct sockbuf *sb);
808 extern void wakeup(void *ident, struct socket *so); /*__Userspace__ */
809 extern int uiomove(void *cp, int n, struct uio *uio);
810 extern int sbwait(struct sockbuf *sb);
811 extern int sodisconnect(struct socket *so);
812 extern int soconnect(struct socket *so, struct sockaddr *nam);
813 extern int sctp_disconnect(struct socket *so);
814 extern int sctp_connect(struct socket *so, struct sockaddr *addr);
815 extern int sctp6_connect(struct socket *so, struct sockaddr *addr);
816 #if defined(__Userspace__)
817 extern int sctpconn_connect(struct socket *so, struct sockaddr *addr);
818 #endif
819 extern void sctp_finish(void);
820 
821 /* ------------------------------------------------ */
822 /* -----  macros copied from above ---- */
823 /* ------------------------------------------------ */
824 
825 /*
826  * Do we need to notify the other side when I/O is possible?
827  */
828 #define	sb_notify(sb)	(((sb)->sb_flags & (SB_WAIT | SB_SEL | SB_ASYNC | \
829     SB_UPCALL | SB_AIO | SB_KNOTE)) != 0)
830 
831 
832 /*
833  * In sorwakeup() and sowwakeup(), acquire the socket buffer lock to
834  * avoid a non-atomic test-and-wakeup.  However, sowakeup is
835  * responsible for releasing the lock if it is called.  We unlock only
836  * if we don't call into sowakeup.  If any code is introduced that
837  * directly invokes the underlying sowakeup() primitives, it must
838  * maintain the same semantics.
839  */
840 #define	sorwakeup_locked(so) do {					\
841 	SOCKBUF_LOCK_ASSERT(&(so)->so_rcv);				\
842 	if (sb_notify(&(so)->so_rcv))					\
843 		sowakeup((so), &(so)->so_rcv);	 			\
844 	else								\
845 		SOCKBUF_UNLOCK(&(so)->so_rcv);				\
846 } while (0)
847 
848 #define	sorwakeup(so) do {						\
849 	SOCKBUF_LOCK(&(so)->so_rcv);					\
850 	sorwakeup_locked(so);						\
851 } while (0)
852 
853 #define	sowwakeup_locked(so) do {					\
854 	SOCKBUF_LOCK_ASSERT(&(so)->so_snd);				\
855 	if (sb_notify(&(so)->so_snd))					\
856 		sowakeup((so), &(so)->so_snd); 				\
857 	else								\
858 		SOCKBUF_UNLOCK(&(so)->so_snd);				\
859 } while (0)
860 
861 #define	sowwakeup(so) do {						\
862 	SOCKBUF_LOCK(&(so)->so_snd);					\
863 	sowwakeup_locked(so);						\
864 } while (0)
865 
866 
867 
868 #endif /* __Userspace__ */
869 
870 #endif /* !_SYS_SOCKETVAR_H_ */
871