FreeBSD/Linux Kernel Cross Reference
sys/sys/socketvar.h
1 /* $OpenBSD: socketvar.h,v 1.43 2008/11/07 17:31:24 deraadt 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 #include <sys/selinfo.h> /* for struct selinfo */
36 #include <sys/queue.h>
37
38 TAILQ_HEAD(soqhead, socket);
39
40 /*
41 * Kernel structure per socket.
42 * Contains send and receive buffer queues,
43 * handle on protocol and pointer to protocol
44 * private data and error information.
45 */
46 struct socket {
47 short so_type; /* generic type, see socket.h */
48 short so_options; /* from socket call, see socket.h */
49 short so_linger; /* time to linger while closing */
50 short so_state; /* internal state flags SS_*, below */
51 void *so_pcb; /* protocol control block */
52 struct protosw *so_proto; /* protocol handle */
53 /*
54 * Variables for connection queueing.
55 * Socket where accepts occur is so_head in all subsidiary sockets.
56 * If so_head is 0, socket is not related to an accept.
57 * For head socket so_q0 queues partially completed connections,
58 * while so_q is a queue of connections ready to be accepted.
59 * If a connection is aborted and it has so_head set, then
60 * it has to be pulled out of either so_q0 or so_q.
61 * We allow connections to queue up based on current queue lengths
62 * and limit on number of queued connections for this socket.
63 */
64 struct socket *so_head; /* back pointer to accept socket */
65 struct soqhead *so_onq; /* queue (q or q0) that we're on */
66 struct soqhead so_q0; /* queue of partial connections */
67 struct soqhead so_q; /* queue of incoming connections */
68 TAILQ_ENTRY(socket) so_qe; /* our queue entry (q or q0) */
69 short so_q0len; /* partials on so_q0 */
70 short so_qlen; /* number of connections on so_q */
71 short so_qlimit; /* max number queued connections */
72 short so_timeo; /* connection timeout */
73 u_short so_error; /* error affecting connection */
74 pid_t so_pgid; /* pgid for signals */
75 uid_t so_siguid; /* uid of process who set so_pgid */
76 uid_t so_sigeuid; /* euid of process who set so_pgid */
77 u_long so_oobmark; /* chars to oob mark */
78 /*
79 * Variables for socket buffering.
80 */
81 struct sockbuf {
82 u_long sb_cc; /* actual chars in buffer */
83 u_long sb_datacc; /* data only chars in buffer */
84 u_long sb_hiwat; /* max actual char count */
85 u_long sb_mbcnt; /* chars of mbufs used */
86 u_long sb_mbmax; /* max chars of mbufs to use */
87 long sb_lowat; /* low water mark */
88 struct mbuf *sb_mb; /* the mbuf chain */
89 struct mbuf *sb_mbtail; /* the last mbuf in the chain */
90 struct mbuf *sb_lastrecord;/* first mbuf of last record in
91 socket buffer */
92 struct selinfo sb_sel; /* process selecting read/write */
93 short sb_flags; /* flags, see below */
94 u_short sb_timeo; /* timeout for read/write */
95 } so_rcv, so_snd;
96 #define SB_MAX (256*1024) /* default for max chars in sockbuf */
97 #define SB_LOCK 0x01 /* lock on data queue */
98 #define SB_WANT 0x02 /* someone is waiting to lock */
99 #define SB_WAIT 0x04 /* someone is waiting for data/space */
100 #define SB_SEL 0x08 /* someone is selecting */
101 #define SB_ASYNC 0x10 /* ASYNC I/O, need signals */
102 #define SB_NOINTR 0x40 /* operations not interruptible */
103 #define SB_KNOTE 0x80 /* kernel note attached */
104
105 void *so_internal; /* Space for svr4 stream data */
106 void (*so_upcall)(struct socket *so, caddr_t arg, int waitf);
107 caddr_t so_upcallarg; /* Arg for above */
108 uid_t so_euid, so_ruid; /* who opened the socket */
109 gid_t so_egid, so_rgid;
110 pid_t so_cpid; /* pid of process that opened socket */
111 };
112
113 #define SB_EMPTY_FIXUP(sb) do { \
114 if ((sb)->sb_mb == NULL) { \
115 (sb)->sb_mbtail = NULL; \
116 (sb)->sb_lastrecord = NULL; \
117 } \
118 } while (/*CONSTCOND*/0)
119
120 /*
121 * Socket state bits.
122 */
123 #define SS_NOFDREF 0x001 /* no file table ref any more */
124 #define SS_ISCONNECTED 0x002 /* socket connected to a peer */
125 #define SS_ISCONNECTING 0x004 /* in process of connecting to peer */
126 #define SS_ISDISCONNECTING 0x008 /* in process of disconnecting */
127 #define SS_CANTSENDMORE 0x010 /* can't send more data to peer */
128 #define SS_CANTRCVMORE 0x020 /* can't receive more data from peer */
129 #define SS_RCVATMARK 0x040 /* at mark on input */
130 #define SS_ISDISCONNECTED 0x800 /* socket disconnected from peer */
131
132 #define SS_PRIV 0x080 /* privileged for broadcast, raw... */
133 #define SS_NBIO 0x100 /* non-blocking ops */
134 #define SS_ASYNC 0x200 /* async i/o notify */
135 #define SS_ISCONFIRMING 0x400 /* deciding to accept connection req */
136 #define SS_CONNECTOUT 0x1000 /* connect, not accept, at this end */
137 #define SS_ISSENDING 0x2000 /* hint for lower layer */
138
139 /*
140 * Macros for sockets and socket buffering.
141 */
142
143 /*
144 * Do we need to notify the other side when I/O is possible?
145 */
146 #define sb_notify(sb) (((sb)->sb_flags & (SB_WAIT|SB_SEL|SB_ASYNC| \
147 SB_KNOTE)) != 0)
148
149 /*
150 * How much space is there in a socket buffer (so->so_snd or so->so_rcv)?
151 * This is problematical if the fields are unsigned, as the space might
152 * still be negative (cc > hiwat or mbcnt > mbmax). Should detect
153 * overflow and return 0.
154 */
155 #define sbspace(sb) \
156 lmin((sb)->sb_hiwat - (sb)->sb_cc, (sb)->sb_mbmax - (sb)->sb_mbcnt)
157
158 /* do we have to send all at once on a socket? */
159 #define sosendallatonce(so) \
160 ((so)->so_proto->pr_flags & PR_ATOMIC)
161
162 /* are we sending on this socket? */
163 #define soissending(so) \
164 ((so)->so_state & SS_ISSENDING)
165
166 /* can we read something from so? */
167 #define soreadable(so) \
168 ((so)->so_rcv.sb_cc >= (so)->so_rcv.sb_lowat || \
169 ((so)->so_state & SS_CANTRCVMORE) || \
170 (so)->so_qlen || (so)->so_error)
171
172 /* can we write something to so? */
173 #define sowriteable(so) \
174 ((sbspace(&(so)->so_snd) >= (so)->so_snd.sb_lowat && \
175 (((so)->so_state&SS_ISCONNECTED) || \
176 ((so)->so_proto->pr_flags&PR_CONNREQUIRED)==0)) || \
177 ((so)->so_state & SS_CANTSENDMORE) || (so)->so_error)
178
179 /* adjust counters in sb reflecting allocation of m */
180 #define sballoc(sb, m) do { \
181 (sb)->sb_cc += (m)->m_len; \
182 if ((m)->m_type != MT_CONTROL && (m)->m_type != MT_SONAME) \
183 (sb)->sb_datacc += (m)->m_len; \
184 (sb)->sb_mbcnt += MSIZE; \
185 if ((m)->m_flags & M_EXT) \
186 (sb)->sb_mbcnt += (m)->m_ext.ext_size; \
187 } while (/* CONSTCOND */ 0)
188
189 /* adjust counters in sb reflecting freeing of m */
190 #define sbfree(sb, m) do { \
191 (sb)->sb_cc -= (m)->m_len; \
192 if ((m)->m_type != MT_CONTROL && (m)->m_type != MT_SONAME) \
193 (sb)->sb_datacc -= (m)->m_len; \
194 (sb)->sb_mbcnt -= MSIZE; \
195 if ((m)->m_flags & M_EXT) \
196 (sb)->sb_mbcnt -= (m)->m_ext.ext_size; \
197 } while (/* CONSTCOND */ 0)
198
199 /*
200 * Set lock on sockbuf sb; sleep if lock is already held.
201 * Unless SB_NOINTR is set on sockbuf, sleep is interruptible.
202 * Returns error without lock if sleep is interrupted.
203 */
204 #define sblock(sb, wf) ((sb)->sb_flags & SB_LOCK ? \
205 (((wf) == M_WAITOK) ? sb_lock(sb) : EWOULDBLOCK) : \
206 ((sb)->sb_flags |= SB_LOCK, 0))
207
208 /* release lock on sockbuf sb */
209 #define sbunlock(sb) do { \
210 (sb)->sb_flags &= ~SB_LOCK; \
211 if ((sb)->sb_flags & SB_WANT) { \
212 (sb)->sb_flags &= ~SB_WANT; \
213 wakeup((caddr_t)&(sb)->sb_flags); \
214 } \
215 } while (/* CONSTCOND */ 0)
216
217 #define sorwakeup(so) do { \
218 sowakeup((so), &(so)->so_rcv); \
219 if ((so)->so_upcall) \
220 (*((so)->so_upcall))((so), (so)->so_upcallarg, \
221 M_DONTWAIT); \
222 } while (/* CONSTCOND */ 0)
223
224 #define sowwakeup(so) sowakeup((so), &(so)->so_snd)
225
226 #ifdef _KERNEL
227 extern u_long sb_max;
228 struct socket *sonewconn(struct socket *head, int connstatus);
229
230 /* strings for sleep message: */
231 extern const char netio[], netcon[], netcls[];
232
233 extern struct pool socket_pool;
234
235 struct mbuf;
236 struct sockaddr;
237 struct proc;
238 struct msghdr;
239 struct stat;
240 struct knote;
241
242 /*
243 * File operations on sockets.
244 */
245 int soo_read(struct file *fp, off_t *, struct uio *uio,
246 struct ucred *cred);
247 int soo_write(struct file *fp, off_t *, struct uio *uio,
248 struct ucred *cred);
249 int soo_ioctl(struct file *fp, u_long cmd, caddr_t data,
250 struct proc *p);
251 int soo_poll(struct file *fp, int events, struct proc *p);
252 int soo_kqfilter(struct file *fp, struct knote *kn);
253 int soo_close(struct file *fp, struct proc *p);
254 int soo_stat(struct file *, struct stat *, struct proc *);
255 int uipc_usrreq(struct socket *, int , struct mbuf *,
256 struct mbuf *, struct mbuf *, struct proc *);
257 void sbappend(struct sockbuf *sb, struct mbuf *m);
258 void sbappendstream(struct sockbuf *sb, struct mbuf *m);
259 int sbappendaddr(struct sockbuf *sb, struct sockaddr *asa,
260 struct mbuf *m0, struct mbuf *control);
261 int sbappendcontrol(struct sockbuf *sb, struct mbuf *m0,
262 struct mbuf *control);
263 void sbappendrecord(struct sockbuf *sb, struct mbuf *m0);
264 void sbcheck(struct sockbuf *sb);
265 void sbcompress(struct sockbuf *sb, struct mbuf *m, struct mbuf *n);
266 struct mbuf *
267 sbcreatecontrol(caddr_t p, int size, int type, int level);
268 void sbdrop(struct sockbuf *sb, int len);
269 void sbdroprecord(struct sockbuf *sb);
270 void sbflush(struct sockbuf *sb);
271 void sbinsertoob(struct sockbuf *sb, struct mbuf *m0);
272 void sbrelease(struct sockbuf *sb);
273 int sbcheckreserve(u_long cnt, u_long defcnt);
274 int sbreserve(struct sockbuf *sb, u_long cc);
275 int sbwait(struct sockbuf *sb);
276 int sb_lock(struct sockbuf *sb);
277 void soinit(void);
278 int soabort(struct socket *so);
279 int soaccept(struct socket *so, struct mbuf *nam);
280 int sobind(struct socket *so, struct mbuf *nam, struct proc *p);
281 void socantrcvmore(struct socket *so);
282 void socantsendmore(struct socket *so);
283 int soclose(struct socket *so);
284 int soconnect(struct socket *so, struct mbuf *nam);
285 int soconnect2(struct socket *so1, struct socket *so2);
286 int socreate(int dom, struct socket **aso, int type, int proto);
287 int sodisconnect(struct socket *so);
288 void sofree(struct socket *so);
289 int sogetopt(struct socket *so, int level, int optname,
290 struct mbuf **mp);
291 void sohasoutofband(struct socket *so);
292 void soisconnected(struct socket *so);
293 void soisconnecting(struct socket *so);
294 void soisdisconnected(struct socket *so);
295 void soisdisconnecting(struct socket *so);
296 int solisten(struct socket *so, int backlog);
297 struct socket *sonewconn(struct socket *head, int connstatus);
298 void soqinsque(struct socket *head, struct socket *so, int q);
299 int soqremque(struct socket *so, int q);
300 int soreceive(struct socket *so, struct mbuf **paddr, struct uio *uio,
301 struct mbuf **mp0, struct mbuf **controlp, int *flagsp);
302 int soreserve(struct socket *so, u_long sndcc, u_long rcvcc);
303 void sorflush(struct socket *so);
304 int sosend(struct socket *so, struct mbuf *addr, struct uio *uio,
305 struct mbuf *top, struct mbuf *control, int flags);
306 int sosetopt(struct socket *so, int level, int optname,
307 struct mbuf *m0);
308 int soshutdown(struct socket *so, int how);
309 void sowakeup(struct socket *so, struct sockbuf *sb);
310 int sockargs(struct mbuf **, const void *, size_t, int);
311
312 int sendit(struct proc *, int, struct msghdr *, int, register_t *);
313 int recvit(struct proc *, int, struct msghdr *, caddr_t,
314 register_t *);
315
316 #ifdef SOCKBUF_DEBUG
317 void sblastrecordchk(struct sockbuf *, const char *);
318 #define SBLASTRECORDCHK(sb, where) sblastrecordchk((sb), (where))
319
320 void sblastmbufchk(struct sockbuf *, const char *);
321 #define SBLASTMBUFCHK(sb, where) sblastmbufchk((sb), (where))
322 #else
323 #define SBLASTRECORDCHK(sb, where) /* nothing */
324 #define SBLASTMBUFCHK(sb, where) /* nothing */
325 #endif /* SOCKBUF_DEBUG */
326
327 #endif /* _KERNEL */
Cache object: 0349ac36d96911bd34c21f6a78c7f03c
|