1 /* $OpenBSD: socketvar.h,v 1.40 2007/07/05 09:04:04 dim 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 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) \
114 do { \
115 if ((sb)->sb_mb == NULL) { \
116 (sb)->sb_mbtail = NULL; \
117 (sb)->sb_lastrecord = NULL; \
118 } \
119 } while (/*CONSTCOND*/0)
120
121 /*
122 * Socket state bits.
123 */
124 #define SS_NOFDREF 0x001 /* no file table ref any more */
125 #define SS_ISCONNECTED 0x002 /* socket connected to a peer */
126 #define SS_ISCONNECTING 0x004 /* in process of connecting to peer */
127 #define SS_ISDISCONNECTING 0x008 /* in process of disconnecting */
128 #define SS_CANTSENDMORE 0x010 /* can't send more data to peer */
129 #define SS_CANTRCVMORE 0x020 /* can't receive more data from peer */
130 #define SS_RCVATMARK 0x040 /* at mark on input */
131 #define SS_ISDISCONNECTED 0x800 /* socket disconnected from peer */
132
133 #define SS_PRIV 0x080 /* privileged for broadcast, raw... */
134 #define SS_NBIO 0x100 /* non-blocking ops */
135 #define SS_ASYNC 0x200 /* async i/o notify */
136 #define SS_ISCONFIRMING 0x400 /* deciding to accept connection req */
137 #define SS_CONNECTOUT 0x1000 /* connect, not accept, at this end */
138 #define SS_ISSENDING 0x2000 /* hint for lower layer */
139
140 /*
141 * Macros for sockets and socket buffering.
142 */
143
144 /*
145 * Do we need to notify the other side when I/O is possible?
146 */
147 #define sb_notify(sb) (((sb)->sb_flags & (SB_WAIT|SB_SEL|SB_ASYNC| \
148 SB_KNOTE)) != 0)
149
150 /*
151 * How much space is there in a socket buffer (so->so_snd or so->so_rcv)?
152 * This is problematical if the fields are unsigned, as the space might
153 * still be negative (cc > hiwat or mbcnt > mbmax). Should detect
154 * overflow and return 0.
155 */
156 #define sbspace(sb) \
157 lmin((sb)->sb_hiwat - (sb)->sb_cc, (sb)->sb_mbmax - (sb)->sb_mbcnt)
158
159 /* do we have to send all at once on a socket? */
160 #define sosendallatonce(so) \
161 ((so)->so_proto->pr_flags & PR_ATOMIC)
162
163 /* are we sending on this socket? */
164 #define soissending(so) \
165 ((so)->so_state & SS_ISSENDING)
166
167 /* can we read something from so? */
168 #define soreadable(so) \
169 ((so)->so_rcv.sb_cc >= (so)->so_rcv.sb_lowat || \
170 ((so)->so_state & SS_CANTRCVMORE) || \
171 (so)->so_qlen || (so)->so_error)
172
173 /* can we write something to so? */
174 #define sowriteable(so) \
175 ((sbspace(&(so)->so_snd) >= (so)->so_snd.sb_lowat && \
176 (((so)->so_state&SS_ISCONNECTED) || \
177 ((so)->so_proto->pr_flags&PR_CONNREQUIRED)==0)) || \
178 ((so)->so_state & SS_CANTSENDMORE) || (so)->so_error)
179
180 /* adjust counters in sb reflecting allocation of m */
181 #define sballoc(sb, m) { \
182 (sb)->sb_cc += (m)->m_len; \
183 if ((m)->m_type != MT_CONTROL && (m)->m_type != MT_SONAME) \
184 (sb)->sb_datacc += (m)->m_len; \
185 (sb)->sb_mbcnt += MSIZE; \
186 if ((m)->m_flags & M_EXT) \
187 (sb)->sb_mbcnt += (m)->m_ext.ext_size; \
188 }
189
190 /* adjust counters in sb reflecting freeing of m */
191 #define sbfree(sb, m) { \
192 (sb)->sb_cc -= (m)->m_len; \
193 if ((m)->m_type != MT_CONTROL && (m)->m_type != MT_SONAME) \
194 (sb)->sb_datacc -= (m)->m_len; \
195 (sb)->sb_mbcnt -= MSIZE; \
196 if ((m)->m_flags & M_EXT) \
197 (sb)->sb_mbcnt -= (m)->m_ext.ext_size; \
198 }
199
200 /*
201 * Set lock on sockbuf sb; sleep if lock is already held.
202 * Unless SB_NOINTR is set on sockbuf, sleep is interruptible.
203 * Returns error without lock if sleep is interrupted.
204 */
205 #define sblock(sb, wf) ((sb)->sb_flags & SB_LOCK ? \
206 (((wf) == M_WAITOK) ? sb_lock(sb) : EWOULDBLOCK) : \
207 ((sb)->sb_flags |= SB_LOCK, 0))
208
209 /* release lock on sockbuf sb */
210 #define sbunlock(sb) { \
211 (sb)->sb_flags &= ~SB_LOCK; \
212 if ((sb)->sb_flags & SB_WANT) { \
213 (sb)->sb_flags &= ~SB_WANT; \
214 wakeup((caddr_t)&(sb)->sb_flags); \
215 } \
216 }
217
218 #define sorwakeup(so) { sowakeup((so), &(so)->so_rcv); \
219 if ((so)->so_upcall) \
220 (*((so)->so_upcall))((so), (so)->so_upcallarg, M_DONTWAIT); \
221 }
222
223 #define sowwakeup(so) sowakeup((so), &(so)->so_snd)
224
225 #ifdef _KERNEL
226 extern u_long sb_max;
227 struct socket *sonewconn(struct socket *head, int connstatus);
228
229 /* strings for sleep message: */
230 extern const char netio[], netcon[], netcls[];
231
232 extern struct pool socket_pool;
233
234 struct mbuf;
235 struct sockaddr;
236 struct proc;
237 struct msghdr;
238 struct stat;
239 struct knote;
240
241 /*
242 * File operations on sockets.
243 */
244 int soo_read(struct file *fp, off_t *, struct uio *uio,
245 struct ucred *cred);
246 int soo_write(struct file *fp, off_t *, struct uio *uio,
247 struct ucred *cred);
248 int soo_ioctl(struct file *fp, u_long cmd, caddr_t data,
249 struct proc *p);
250 int soo_poll(struct file *fp, int events, struct proc *p);
251 int soo_kqfilter(struct file *fp, struct knote *kn);
252 int soo_close(struct file *fp, struct proc *p);
253 int soo_stat(struct file *, struct stat *, struct proc *);
254 int uipc_usrreq(struct socket *, int , struct mbuf *,
255 struct mbuf *, struct mbuf *);
256 void sbappend(struct sockbuf *sb, struct mbuf *m);
257 void sbappendstream(struct sockbuf *sb, struct mbuf *m);
258 int sbappendaddr(struct sockbuf *sb, struct sockaddr *asa,
259 struct mbuf *m0, struct mbuf *control);
260 int sbappendcontrol(struct sockbuf *sb, struct mbuf *m0,
261 struct mbuf *control);
262 void sbappendrecord(struct sockbuf *sb, struct mbuf *m0);
263 void sbcheck(struct sockbuf *sb);
264 void sbcompress(struct sockbuf *sb, struct mbuf *m, struct mbuf *n);
265 struct mbuf *
266 sbcreatecontrol(caddr_t p, int size, int type, int level);
267 void sbdrop(struct sockbuf *sb, int len);
268 void sbdroprecord(struct sockbuf *sb);
269 void sbflush(struct sockbuf *sb);
270 void sbinsertoob(struct sockbuf *sb, struct mbuf *m0);
271 void sbrelease(struct sockbuf *sb);
272 int sbcheckreserve(u_long cnt, u_long defcnt);
273 int sbreserve(struct sockbuf *sb, u_long cc);
274 int sbwait(struct sockbuf *sb);
275 int sb_lock(struct sockbuf *sb);
276 void soinit(void);
277 int soabort(struct socket *so);
278 int soaccept(struct socket *so, struct mbuf *nam);
279 int sobind(struct socket *so, struct mbuf *nam);
280 void socantrcvmore(struct socket *so);
281 void socantsendmore(struct socket *so);
282 int soclose(struct socket *so);
283 int soconnect(struct socket *so, struct mbuf *nam);
284 int soconnect2(struct socket *so1, struct socket *so2);
285 int socreate(int dom, struct socket **aso, int type, int proto);
286 int sodisconnect(struct socket *so);
287 void sofree(struct socket *so);
288 int sogetopt(struct socket *so, int level, int optname,
289 struct mbuf **mp);
290 void sohasoutofband(struct socket *so);
291 void soisconnected(struct socket *so);
292 void soisconnecting(struct socket *so);
293 void soisdisconnected(struct socket *so);
294 void soisdisconnecting(struct socket *so);
295 int solisten(struct socket *so, int backlog);
296 struct socket *sonewconn(struct socket *head, int connstatus);
297 void soqinsque(struct socket *head, struct socket *so, int q);
298 int soqremque(struct socket *so, int q);
299 int soreceive(struct socket *so, struct mbuf **paddr, struct uio *uio,
300 struct mbuf **mp0, struct mbuf **controlp, int *flagsp);
301 int soreserve(struct socket *so, u_long sndcc, u_long rcvcc);
302 void sorflush(struct socket *so);
303 int sosend(struct socket *so, struct mbuf *addr, struct uio *uio,
304 struct mbuf *top, struct mbuf *control, int flags);
305 int sosetopt(struct socket *so, int level, int optname,
306 struct mbuf *m0);
307 int soshutdown(struct socket *so, int how);
308 void sowakeup(struct socket *so, struct sockbuf *sb);
309 int sockargs(struct mbuf **, const void *, size_t, int);
310
311 int sendit(struct proc *, int, struct msghdr *, int, register_t *);
312 int recvit(struct proc *, int, struct msghdr *, caddr_t,
313 register_t *);
314
315 #ifdef SOCKBUF_DEBUG
316 void sblastrecordchk(struct sockbuf *, const char *);
317 #define SBLASTRECORDCHK(sb, where) sblastrecordchk((sb), (where))
318
319 void sblastmbufchk(struct sockbuf *, const char *);
320 #define SBLASTMBUFCHK(sb, where) sblastmbufchk((sb), (where))
321 #else
322 #define SBLASTRECORDCHK(sb, where) /* nothing */
323 #define SBLASTMBUFCHK(sb, where) /* nothing */
324 #endif /* SOCKBUF_DEBUG */
325
326 #endif /* _KERNEL */