FreeBSD/Linux Kernel Cross Reference
sys/sys/socketvar.h
1 /* $NetBSD: socketvar.h,v 1.69.2.1 2004/05/30 07:02:37 tron 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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 * @(#)socketvar.h 8.3 (Berkeley) 2/19/95
32 */
33
34 #ifndef _SYS_SOCKETVAR_H_
35 #define _SYS_SOCKETVAR_H_
36
37 #include <sys/select.h> /* for struct selinfo */
38 #include <sys/queue.h>
39
40 #if !defined(_KERNEL) || defined(LKM)
41 struct uio;
42 #endif
43
44 TAILQ_HEAD(soqhead, socket);
45
46 /*
47 * Variables for socket buffering.
48 */
49 struct sockbuf {
50 struct selinfo sb_sel; /* process selecting read/write */
51 struct mowner *sb_mowner; /* who owns data for this sockbuf */
52 /* When re-zeroing this struct, we zero from sb_startzero to the end */
53 #define sb_startzero sb_cc
54 u_long sb_cc; /* actual chars in buffer */
55 u_long sb_hiwat; /* max actual char count */
56 u_long sb_mbcnt; /* chars of mbufs used */
57 u_long sb_mbmax; /* max chars of mbufs to use */
58 long sb_lowat; /* low water mark */
59 struct mbuf *sb_mb; /* the mbuf chain */
60 struct mbuf *sb_mbtail; /* the last mbuf in the chain */
61 struct mbuf *sb_lastrecord; /* first mbuf of last record in
62 socket buffer */
63 short sb_flags; /* flags, see below */
64 short sb_timeo; /* timeout for read/write */
65 };
66
67 #ifndef SB_MAX
68 #define SB_MAX (256*1024) /* default for max chars in sockbuf */
69 #endif
70
71 #define SB_LOCK 0x01 /* lock on data queue */
72 #define SB_WANT 0x02 /* someone is waiting to lock */
73 #define SB_WAIT 0x04 /* someone is waiting for data/space */
74 #define SB_SEL 0x08 /* someone is selecting */
75 #define SB_ASYNC 0x10 /* ASYNC I/O, need signals */
76 #define SB_UPCALL 0x20 /* someone wants an upcall */
77 #define SB_NOINTR 0x40 /* operations not interruptible */
78 /* XXXLUKEM: 0x80 left for FreeBSD's SB_AIO */
79 #define SB_KNOTE 0x100 /* kernel note attached */
80
81 /*
82 * Kernel structure per socket.
83 * Contains send and receive buffer queues,
84 * handle on protocol and pointer to protocol
85 * private data and error information.
86 */
87 struct socket {
88 short so_type; /* generic type, see socket.h */
89 short so_options; /* from socket call, see socket.h */
90 short so_linger; /* time to linger while closing */
91 short so_state; /* internal state flags SS_*, below */
92 void *so_pcb; /* protocol control block */
93 struct protosw *so_proto; /* protocol handle */
94 /*
95 * Variables for connection queueing.
96 * Socket where accepts occur is so_head in all subsidiary sockets.
97 * If so_head is 0, socket is not related to an accept.
98 * For head socket so_q0 queues partially completed connections,
99 * while so_q is a queue of connections ready to be accepted.
100 * If a connection is aborted and it has so_head set, then
101 * it has to be pulled out of either so_q0 or so_q.
102 * We allow connections to queue up based on current queue lengths
103 * and limit on number of queued connections for this socket.
104 */
105 struct socket *so_head; /* back pointer to accept socket */
106 struct soqhead *so_onq; /* queue (q or q0) that we're on */
107 struct soqhead so_q0; /* queue of partial connections */
108 struct soqhead so_q; /* queue of incoming connections */
109 TAILQ_ENTRY(socket) so_qe; /* our queue entry (q or q0) */
110 short so_q0len; /* partials on so_q0 */
111 short so_qlen; /* number of connections on so_q */
112 short so_qlimit; /* max number queued connections */
113 short so_timeo; /* connection timeout */
114 u_short so_error; /* error affecting connection */
115 pid_t so_pgid; /* pgid for signals */
116 u_long so_oobmark; /* chars to oob mark */
117 struct sockbuf so_snd; /* send buffer */
118 struct sockbuf so_rcv; /* receive buffer */
119
120 void *so_internal; /* Space for svr4 stream data */
121 void (*so_upcall) __P((struct socket *, caddr_t, int));
122 caddr_t so_upcallarg; /* Arg for above */
123 int (*so_send) __P((struct socket *, struct mbuf *,
124 struct uio *, struct mbuf *,
125 struct mbuf *, int));
126 int (*so_receive) __P((struct socket *,
127 struct mbuf **,
128 struct uio *, struct mbuf **,
129 struct mbuf **, int *));
130 struct mowner *so_mowner; /* who owns mbufs for this socket */
131 uid_t so_uid; /* who opened the socket */
132 };
133
134 #define SB_EMPTY_FIXUP(sb) \
135 do { \
136 if ((sb)->sb_mb == NULL) { \
137 (sb)->sb_mbtail = NULL; \
138 (sb)->sb_lastrecord = NULL; \
139 } \
140 } while (/*CONSTCOND*/0)
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_NBIO 0x080 /* non-blocking ops */
155 #define SS_ASYNC 0x100 /* async i/o notify */
156 #define SS_ISCONFIRMING 0x200 /* deciding to accept connection req */
157 #define SS_MORETOCOME 0x400 /*
158 * hint from sosend to lower layer;
159 * more data coming
160 */
161 #define SS_ISAPIPE 0x800 /* socket is implementing a pipe */
162
163
164 /*
165 * Macros for sockets and socket buffering.
166 */
167
168 /*
169 * Do we need to notify the other side when I/O is possible?
170 */
171 #define sb_notify(sb) (((sb)->sb_flags & \
172 (SB_WAIT | SB_SEL | SB_ASYNC | SB_UPCALL | SB_KNOTE)) != 0)
173
174 /*
175 * How much space is there in a socket buffer (so->so_snd or so->so_rcv)?
176 * This is problematical if the fields are unsigned, as the space might
177 * still be negative (cc > hiwat or mbcnt > mbmax). Should detect
178 * overflow and return 0.
179 */
180 #define sbspace(sb) \
181 (lmin((sb)->sb_hiwat - (sb)->sb_cc, (sb)->sb_mbmax - (sb)->sb_mbcnt))
182
183 /* do we have to send all at once on a socket? */
184 #define sosendallatonce(so) \
185 ((so)->so_proto->pr_flags & PR_ATOMIC)
186
187 /* can we read something from so? */
188 #define soreadable(so) \
189 ((so)->so_rcv.sb_cc >= (so)->so_rcv.sb_lowat || \
190 ((so)->so_state & SS_CANTRCVMORE) || \
191 (so)->so_qlen || (so)->so_error)
192
193 /* can we write something to so? */
194 #define sowritable(so) \
195 ((sbspace(&(so)->so_snd) >= (so)->so_snd.sb_lowat && \
196 (((so)->so_state&SS_ISCONNECTED) || \
197 ((so)->so_proto->pr_flags&PR_CONNREQUIRED)==0)) || \
198 ((so)->so_state & SS_CANTSENDMORE) || \
199 (so)->so_error)
200
201 /* adjust counters in sb reflecting allocation of m */
202 #define sballoc(sb, m) \
203 do { \
204 (sb)->sb_cc += (m)->m_len; \
205 (sb)->sb_mbcnt += MSIZE; \
206 if ((m)->m_flags & M_EXT) \
207 (sb)->sb_mbcnt += (m)->m_ext.ext_size; \
208 } while (/* CONSTCOND */ 0)
209
210 /* adjust counters in sb reflecting freeing of m */
211 #define sbfree(sb, m) \
212 do { \
213 (sb)->sb_cc -= (m)->m_len; \
214 (sb)->sb_mbcnt -= MSIZE; \
215 if ((m)->m_flags & M_EXT) \
216 (sb)->sb_mbcnt -= (m)->m_ext.ext_size; \
217 } while (/* CONSTCOND */ 0)
218
219 /*
220 * Set lock on sockbuf sb; sleep if lock is already held.
221 * Unless SB_NOINTR is set on sockbuf, sleep is interruptible.
222 * Returns error without lock if sleep is interrupted.
223 */
224 #define sblock(sb, wf) \
225 ((sb)->sb_flags & SB_LOCK ? \
226 (((wf) == M_WAITOK) ? sb_lock(sb) : EWOULDBLOCK) : \
227 ((sb)->sb_flags |= SB_LOCK), 0)
228
229 /* release lock on sockbuf sb */
230 #define sbunlock(sb) \
231 do { \
232 (sb)->sb_flags &= ~SB_LOCK; \
233 if ((sb)->sb_flags & SB_WANT) { \
234 (sb)->sb_flags &= ~SB_WANT; \
235 wakeup((caddr_t)&(sb)->sb_flags); \
236 } \
237 } while (/* CONSTCOND */ 0)
238
239 #define sorwakeup(so) \
240 do { \
241 if (sb_notify(&(so)->so_rcv)) \
242 sowakeup((so), &(so)->so_rcv, POLL_IN); \
243 } while (/* CONSTCOND */ 0)
244
245 #define sowwakeup(so) \
246 do { \
247 if (sb_notify(&(so)->so_snd)) \
248 sowakeup((so), &(so)->so_snd, POLL_OUT); \
249 } while (/* CONSTCOND */ 0)
250
251 #ifdef _KERNEL
252 extern u_long sb_max;
253 extern int somaxkva;
254 /* to catch callers missing new second argument to sonewconn: */
255 #define sonewconn(head, connstatus) sonewconn1((head), (connstatus))
256
257 /* strings for sleep message: */
258 extern const char netio[], netcon[], netcls[];
259
260 extern struct pool socket_pool;
261
262 struct mbuf;
263 struct sockaddr;
264 struct proc;
265 struct msghdr;
266 struct stat;
267 struct knote;
268
269 /*
270 * File operations on sockets.
271 */
272 int soo_read(struct file *, off_t *, struct uio *, struct ucred *, int);
273 int soo_write(struct file *, off_t *, struct uio *, struct ucred *, int);
274 int soo_fcntl(struct file *, u_int cmd, void *, struct proc *);
275 int soo_ioctl(struct file *, u_long cmd, void *, struct proc *);
276 int soo_poll(struct file *, int, struct proc *);
277 int soo_kqfilter(struct file *, struct knote *);
278 int soo_close(struct file *, struct proc *);
279 int soo_stat(struct file *, struct stat *, struct proc *);
280 int uipc_usrreq(struct socket *, int, struct mbuf *,
281 struct mbuf *, struct mbuf *, struct proc *);
282 int uipc_ctloutput(int, struct socket *, int, int, struct mbuf **);
283 void sbappend(struct sockbuf *, struct mbuf *);
284 void sbappendstream(struct sockbuf *, struct mbuf *);
285 int sbappendaddr(struct sockbuf *, struct sockaddr *, struct mbuf *,
286 struct mbuf *);
287 int sbappendaddrchain(struct sockbuf *, const struct sockaddr *,
288 struct mbuf *, int);
289 int sbappendcontrol(struct sockbuf *, struct mbuf *, struct mbuf *);
290 void sbappendrecord(struct sockbuf *, struct mbuf *);
291 void sbcheck(struct sockbuf *);
292 void sbcompress(struct sockbuf *, struct mbuf *, struct mbuf *);
293 struct mbuf *
294 sbcreatecontrol(caddr_t, int, int, int);
295 void sbdrop(struct sockbuf *, int);
296 void sbdroprecord(struct sockbuf *);
297 void sbflush(struct sockbuf *);
298 void sbinsertoob(struct sockbuf *, struct mbuf *);
299 void sbrelease(struct sockbuf *);
300 int sbreserve(struct sockbuf *, u_long);
301 int sbwait(struct sockbuf *);
302 int sb_lock(struct sockbuf *);
303 int sb_max_set(u_long);
304 void soinit(void);
305 int soabort(struct socket *);
306 int soaccept(struct socket *, struct mbuf *);
307 int sobind(struct socket *, struct mbuf *, struct proc *);
308 void socantrcvmore(struct socket *);
309 void socantsendmore(struct socket *);
310 int soclose(struct socket *);
311 int soconnect(struct socket *, struct mbuf *);
312 int soconnect2(struct socket *, struct socket *);
313 int socreate(int, struct socket **, int, int);
314 int sodisconnect(struct socket *);
315 void sofree(struct socket *);
316 int sogetopt(struct socket *, int, int, struct mbuf **);
317 void sohasoutofband(struct socket *);
318 void soisconnected(struct socket *);
319 void soisconnecting(struct socket *);
320 void soisdisconnected(struct socket *);
321 void soisdisconnecting(struct socket *);
322 int solisten(struct socket *, int);
323 struct socket *
324 sonewconn1(struct socket *, int);
325 void soqinsque(struct socket *, struct socket *, int);
326 int soqremque(struct socket *, int);
327 int soreceive(struct socket *, struct mbuf **, struct uio *,
328 struct mbuf **, struct mbuf **, int *);
329 int soreserve(struct socket *, u_long, u_long);
330 void sorflush(struct socket *);
331 int sosend(struct socket *, struct mbuf *, struct uio *,
332 struct mbuf *, struct mbuf *, int);
333 int sosetopt(struct socket *, int, int, struct mbuf *);
334 int soshutdown(struct socket *, int);
335 void sowakeup(struct socket *, struct sockbuf *, int);
336 int sockargs(struct mbuf **, const void *, size_t, int);
337
338 int sendit(struct proc *, int, struct msghdr *, int, register_t *);
339 int recvit(struct proc *, int, struct msghdr *, caddr_t, register_t *);
340
341 #ifdef SOCKBUF_DEBUG
342 void sblastrecordchk(struct sockbuf *, const char *);
343 #define SBLASTRECORDCHK(sb, where) sblastrecordchk((sb), (where))
344
345 void sblastmbufchk(struct sockbuf *, const char *);
346 #define SBLASTMBUFCHK(sb, where) sblastmbufchk((sb), (where))
347 #else
348 #define SBLASTRECORDCHK(sb, where) /* nothing */
349 #define SBLASTMBUFCHK(sb, where) /* nothing */
350 #endif /* SOCKBUF_DEBUG */
351
352 /* sosend loan */
353 vaddr_t sokvaalloc(vsize_t, struct socket *);
354 void sokvafree(vaddr_t, vsize_t);
355 void soloanfree(struct mbuf *, caddr_t, size_t, void *);
356
357 /*
358 * Values for socket-buffer-append priority argument to sbappendaddrchain().
359 * The following flags are reserved for future implementation:
360 *
361 * SB_PRIO_NONE: honour normal socket-buffer limits.
362 *
363 * SB_PRIO_ONESHOT_OVERFLOW: if the socket has any space,
364 * deliver the entire chain. Intended for large requests
365 * that should be delivered in their entirety, or not at all.
366 *
367 * SB_PRIO_OVERDRAFT: allow a small (2*MLEN) overflow, over and
368 * aboce normal socket limits. Intended messages indicating
369 * buffer overflow in earlier normal/lower-priority messages .
370 *
371 * SB_PRIO_BESTEFFORT: Ignore limits entirely. Intended only for
372 * kernel-generated messages to specially-marked scokets which
373 * require "reliable" delivery, nd where the source socket/protocol
374 * message generator enforce some hard limit (but possibly well
375 * above kern.sbmax). It is entirely up to the in-kernel source to
376 * avoid complete mbuf exhaustion or DoS scenarios.
377 */
378 #define SB_PRIO_NONE 0
379 #define SB_PRIO_ONESHOT_OVERFLOW 1
380 #define SB_PRIO_OVERDRAFT 2
381 #define SB_PRIO_BESTEFFORT 3
382
383 #endif /* _KERNEL */
384
385 #endif /* !_SYS_SOCKETVAR_H_ */
Cache object: 4b71ee3ca0637e1e586b54a842fdc7c5
|