]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/sys/socketvar.h
Make m_length() and m_fixhdr() return unsigned.
[FreeBSD/FreeBSD.git] / sys / sys / socketvar.h
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  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by the University of
16  *      California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *      @(#)socketvar.h 8.3 (Berkeley) 2/19/95
34  * $FreeBSD$
35  */
36
37 #ifndef _SYS_SOCKETVAR_H_
38 #define _SYS_SOCKETVAR_H_
39
40 #include <sys/_label.h>                 /* for struct label */
41 #include <sys/queue.h>                  /* for TAILQ macros */
42 #include <sys/selinfo.h>                /* for struct selinfo */
43
44 /*
45  * Kernel structure per socket.
46  * Contains send and receive buffer queues,
47  * handle on protocol and pointer to protocol
48  * private data and error information.
49  */
50 typedef u_quad_t so_gen_t;
51
52 /*
53  * List of locks:
54  * (c)  const, inited in either socreate() or sonewconn()
55  * (m)  sb_mtx mutex
56  * (mr) so_rcv.sb_mtx mutex
57  * (sg) sigio_lock sx
58  * (sh) sohead_lock sx
59  *
60  * Lock of so_rcv.sb_mtx can duplicate, provided that sohead_lock
61  * is exclusively locked.
62  *
63  * Brackets mean that this data is not protected yet.
64  */
65 struct socket {
66         int     so_count;               /* reference count */
67         short   so_type;                /* generic type, see socket.h */
68         short   so_options;             /* from socket call, see socket.h */
69         short   so_linger;              /* time to linger while closing */
70         short   so_state;               /* internal state flags SS_*, below */
71         void    *so_pcb;                /* protocol control block */
72         struct  protosw *so_proto;      /* protocol handle */
73 /*
74  * Variables for connection queuing.
75  * Socket where accepts occur is so_head in all subsidiary sockets.
76  * If so_head is 0, socket is not related to an accept.
77  * For head socket so_incomp queues partially completed connections,
78  * while so_comp is a queue of connections ready to be accepted.
79  * If a connection is aborted and it has so_head set, then
80  * it has to be pulled out of either so_incomp or so_comp.
81  * We allow connections to queue up based on current queue lengths
82  * and limit on number of queued connections for this socket.
83  */
84         struct  socket *so_head;        /* back pointer to accept socket */
85         TAILQ_HEAD(, socket) so_incomp; /* queue of partial unaccepted connections */
86         TAILQ_HEAD(, socket) so_comp;   /* queue of complete unaccepted connections */
87         TAILQ_ENTRY(socket) so_list;    /* list of unaccepted connections */
88         short   so_qlen;                /* number of unaccepted connections */
89         short   so_incqlen;             /* number of unaccepted incomplete
90                                            connections */
91         short   so_qlimit;              /* max number queued connections */
92         short   so_timeo;               /* connection timeout */
93         u_short so_error;               /* error affecting connection */
94         struct  sigio *so_sigio;        /* [sg] information for async I/O or
95                                            out of band data (SIGURG) */
96         u_long  so_oobmark;             /* chars to oob mark */
97         TAILQ_HEAD(, aiocblist) so_aiojobq; /* AIO ops waiting on socket */
98 /*
99  * Variables for socket buffering.
100  */
101         struct sockbuf {
102                 struct  selinfo sb_sel; /* process selecting read/write */
103                 struct  mbuf *sb_mb;    /* the mbuf chain */
104                 u_int   sb_cc;          /* actual chars in buffer */
105                 u_int   sb_hiwat;       /* max actual char count */
106                 u_int   sb_mbcnt;       /* chars of mbufs used */
107                 u_int   sb_mbmax;       /* max chars of mbufs to use */
108                 int     sb_lowat;       /* low water mark */
109                 int     sb_timeo;       /* timeout for read/write */
110                 short   sb_flags;       /* flags, see below */
111         } so_rcv, so_snd;
112 #define SB_MAX          (256*1024)      /* default for max chars in sockbuf */
113 #define SB_LOCK         0x01            /* lock on data queue */
114 #define SB_WANT         0x02            /* someone is waiting to lock */
115 #define SB_WAIT         0x04            /* someone is waiting for data/space */
116 #define SB_SEL          0x08            /* someone is selecting */
117 #define SB_ASYNC        0x10            /* ASYNC I/O, need signals */
118 #define SB_UPCALL       0x20            /* someone wants an upcall */
119 #define SB_NOINTR       0x40            /* operations not interruptible */
120 #define SB_AIO          0x80            /* AIO operations queued */
121 #define SB_KNOTE        0x100           /* kernel note attached */
122
123         void    (*so_upcall)(struct socket *, void *, int);
124         void    *so_upcallarg;
125         struct  ucred *so_cred;         /* user credentials */
126         struct  label so_label;         /* MAC label for socket */
127         struct  label so_peerlabel;     /* cached MAC label for socket peer */
128         /* NB: generation count must not be first; easiest to make it last. */
129         so_gen_t so_gencnt;             /* generation count */
130         void    *so_emuldata;           /* private data for emulators */
131         struct so_accf {
132                 struct  accept_filter *so_accept_filter;
133                 void    *so_accept_filter_arg;  /* saved filter args */
134                 char    *so_accept_filter_str;  /* saved user args */
135         } *so_accf;
136 };
137
138 /*
139  * Socket state bits.
140  */
141 #define SS_NOFDREF              0x0001  /* no file table ref any more */
142 #define SS_ISCONNECTED          0x0002  /* socket connected to a peer */
143 #define SS_ISCONNECTING         0x0004  /* in process of connecting to peer */
144 #define SS_ISDISCONNECTING      0x0008  /* in process of disconnecting */
145 #define SS_CANTSENDMORE         0x0010  /* can't send more data to peer */
146 #define SS_CANTRCVMORE          0x0020  /* can't receive more data from peer */
147 #define SS_RCVATMARK            0x0040  /* at mark on input */
148
149 #define SS_NBIO                 0x0100  /* non-blocking ops */
150 #define SS_ASYNC                0x0200  /* async i/o notify */
151 #define SS_ISCONFIRMING         0x0400  /* deciding to accept connection req */
152
153 #define SS_INCOMP               0x0800  /* unaccepted, incomplete connection */
154 #define SS_COMP                 0x1000  /* unaccepted, complete connection */
155 #define SS_ISDISCONNECTED       0x2000  /* socket disconnected from peer */
156
157 /*
158  * Externalized form of struct socket used by the sysctl(3) interface.
159  */
160 struct xsocket {
161         size_t  xso_len;        /* length of this structure */
162         struct  socket *xso_so; /* makes a convenient handle sometimes */
163         short   so_type;
164         short   so_options;
165         short   so_linger;
166         short   so_state;
167         caddr_t so_pcb;         /* another convenient handle */
168         int     xso_protocol;
169         int     xso_family;
170         short   so_qlen;
171         short   so_incqlen;
172         short   so_qlimit;
173         short   so_timeo;
174         u_short so_error;
175         pid_t   so_pgid;
176         u_long  so_oobmark;
177         struct xsockbuf {
178                 u_int   sb_cc;
179                 u_int   sb_hiwat;
180                 u_int   sb_mbcnt;
181                 u_int   sb_mbmax;
182                 int     sb_lowat;
183                 int     sb_timeo;
184                 short   sb_flags;
185         } so_rcv, so_snd;
186         uid_t   so_uid;         /* XXX */
187 };
188
189 /*
190  * Macros for sockets and socket buffering.
191  */
192
193 /*
194  * Do we need to notify the other side when I/O is possible?
195  */
196 #define sb_notify(sb)   (((sb)->sb_flags & (SB_WAIT | SB_SEL | SB_ASYNC | \
197     SB_UPCALL | SB_AIO | SB_KNOTE)) != 0)
198
199 /*
200  * How much space is there in a socket buffer (so->so_snd or so->so_rcv)?
201  * This is problematical if the fields are unsigned, as the space might
202  * still be negative (cc > hiwat or mbcnt > mbmax).  Should detect
203  * overflow and return 0.  Should use "lmin" but it doesn't exist now.
204  */
205 #define sbspace(sb) \
206     ((long) imin((int)((sb)->sb_hiwat - (sb)->sb_cc), \
207          (int)((sb)->sb_mbmax - (sb)->sb_mbcnt)))
208
209 /* do we have to send all at once on a socket? */
210 #define sosendallatonce(so) \
211     ((so)->so_proto->pr_flags & PR_ATOMIC)
212
213 /* can we read something from so? */
214 #define soreadable(so) \
215     ((so)->so_rcv.sb_cc >= (so)->so_rcv.sb_lowat || \
216         ((so)->so_state & SS_CANTRCVMORE) || \
217         !TAILQ_EMPTY(&(so)->so_comp) || (so)->so_error)
218
219 /* can we write something to so? */
220 #define sowriteable(so) \
221     ((sbspace(&(so)->so_snd) >= (so)->so_snd.sb_lowat && \
222         (((so)->so_state&SS_ISCONNECTED) || \
223           ((so)->so_proto->pr_flags&PR_CONNREQUIRED)==0)) || \
224      ((so)->so_state & SS_CANTSENDMORE) || \
225      (so)->so_error)
226
227 /* adjust counters in sb reflecting allocation of m */
228 #define sballoc(sb, m) { \
229         (sb)->sb_cc += (m)->m_len; \
230         (sb)->sb_mbcnt += MSIZE; \
231         if ((m)->m_flags & M_EXT) \
232                 (sb)->sb_mbcnt += (m)->m_ext.ext_size; \
233 }
234
235 /* adjust counters in sb reflecting freeing of m */
236 #define sbfree(sb, m) { \
237         (sb)->sb_cc -= (m)->m_len; \
238         (sb)->sb_mbcnt -= MSIZE; \
239         if ((m)->m_flags & M_EXT) \
240                 (sb)->sb_mbcnt -= (m)->m_ext.ext_size; \
241 }
242
243 /*
244  * Set lock on sockbuf sb; sleep if lock is already held.
245  * Unless SB_NOINTR is set on sockbuf, sleep is interruptible.
246  * Returns error without lock if sleep is interrupted.
247  */
248 #define sblock(sb, wf) ((sb)->sb_flags & SB_LOCK ? \
249                 (((wf) == M_WAITOK) ? sb_lock(sb) : EWOULDBLOCK) : \
250                 ((sb)->sb_flags |= SB_LOCK), 0)
251
252 /* release lock on sockbuf sb */
253 #define sbunlock(sb) { \
254         (sb)->sb_flags &= ~SB_LOCK; \
255         if ((sb)->sb_flags & SB_WANT) { \
256                 (sb)->sb_flags &= ~SB_WANT; \
257                 wakeup((caddr_t)&(sb)->sb_flags); \
258         } \
259 }
260
261 /*
262  * soref()/sorele() ref-count the socket structure.  Note that you must
263  * still explicitly close the socket, but the last ref count will free
264  * the structure.
265  */
266 #define soref(so)       do {                    \
267                                 ++(so)->so_count; \
268                         } while (0)
269
270 #define sorele(so)      do {                            \
271                                 if ((so)->so_count <= 0)        \
272                                         panic("sorele");\
273                                 if (--(so)->so_count == 0)\
274                                         sofree(so);     \
275                         } while (0)
276
277 #define sotryfree(so)   do {                            \
278                                 if ((so)->so_count == 0)        \
279                                         sofree(so);     \
280                         } while(0)
281
282 #define sorwakeup(so)   do {                                    \
283                                 if (sb_notify(&(so)->so_rcv))   \
284                                         sowakeup((so), &(so)->so_rcv); \
285                         } while (0)
286
287 #define sowwakeup(so)   do {                                    \
288                                 if (sb_notify(&(so)->so_snd))   \
289                                         sowakeup((so), &(so)->so_snd); \
290                         } while (0)
291
292 #ifdef _KERNEL
293
294 /*
295  * Argument structure for sosetopt et seq.  This is in the KERNEL
296  * section because it will never be visible to user code.
297  */
298 enum sopt_dir { SOPT_GET, SOPT_SET };
299 struct sockopt {
300         enum    sopt_dir sopt_dir; /* is this a get or a set? */
301         int     sopt_level;     /* second arg of [gs]etsockopt */
302         int     sopt_name;      /* third arg of [gs]etsockopt */
303         void   *sopt_val;       /* fourth arg of [gs]etsockopt */
304         size_t  sopt_valsize;   /* (almost) fifth arg of [gs]etsockopt */
305         struct  thread *sopt_td; /* calling thread or null if kernel */
306 };
307
308 struct sf_buf {
309         SLIST_ENTRY(sf_buf) free_list;  /* list of free buffer slots */
310         struct          vm_page *m;     /* currently mapped page */
311         vm_offset_t     kva;            /* va of mapping */
312 };
313
314 struct accept_filter {
315         char    accf_name[16];
316         void    (*accf_callback)
317                 (struct socket *so, void *arg, int waitflag);
318         void *  (*accf_create)
319                 (struct socket *so, char *arg);
320         void    (*accf_destroy)
321                 (struct socket *so);
322         SLIST_ENTRY(accept_filter) accf_next;
323 };
324
325 #ifdef MALLOC_DECLARE
326 MALLOC_DECLARE(M_ACCF);
327 MALLOC_DECLARE(M_PCB);
328 MALLOC_DECLARE(M_SONAME);
329 #endif
330
331 extern int      maxsockets;
332 extern u_long   sb_max;
333 extern struct uma_zone *socket_zone;
334 extern so_gen_t so_gencnt;
335
336 struct file;
337 struct filedesc;
338 struct knote;
339 struct mbuf;
340 struct sockaddr;
341 struct stat;
342 struct ucred;
343 struct uio;
344
345 /*
346  * File operations on sockets.
347  */
348 int     soo_read(struct file *fp, struct uio *uio, struct ucred *active_cred,
349             int flags, struct thread *td);
350 int     soo_write(struct file *fp, struct uio *uio,
351             struct ucred *active_cred, int flags, struct thread *td);
352 int     soo_close(struct file *fp, struct thread *td);
353 int     soo_ioctl(struct file *fp, u_long cmd, void *data,
354             struct ucred *active_cred, struct thread *td);
355 int     soo_poll(struct file *fp, int events, struct ucred *active_cred,
356             struct thread *td);
357 int     soo_stat(struct file *fp, struct stat *ub, struct ucred *active_cred,
358             struct thread *td);
359 int     sokqfilter(struct file *fp, struct knote *kn);
360
361 /*
362  * From uipc_socket and friends
363  */
364 struct  sockaddr *dup_sockaddr(struct sockaddr *sa, int canwait);
365 int     sockargs(struct mbuf **mp, caddr_t buf, int buflen, int type);
366 int     getsockaddr(struct sockaddr **namp, caddr_t uaddr, size_t len);
367 void    sbappend(struct sockbuf *sb, struct mbuf *m);
368 int     sbappendaddr(struct sockbuf *sb, struct sockaddr *asa,
369             struct mbuf *m0, struct mbuf *control);
370 int     sbappendcontrol(struct sockbuf *sb, struct mbuf *m0,
371             struct mbuf *control);
372 void    sbappendrecord(struct sockbuf *sb, struct mbuf *m0);
373 void    sbcheck(struct sockbuf *sb);
374 void    sbcompress(struct sockbuf *sb, struct mbuf *m, struct mbuf *n);
375 struct mbuf *
376         sbcreatecontrol(caddr_t p, int size, int type, int level);
377 void    sbdrop(struct sockbuf *sb, int len);
378 void    sbdroprecord(struct sockbuf *sb);
379 void    sbflush(struct sockbuf *sb);
380 void    sbinsertoob(struct sockbuf *sb, struct mbuf *m0);
381 void    sbrelease(struct sockbuf *sb, struct socket *so);
382 int     sbreserve(struct sockbuf *sb, u_long cc, struct socket *so,
383             struct thread *td);
384 void    sbtoxsockbuf(struct sockbuf *sb, struct xsockbuf *xsb);
385 int     sbwait(struct sockbuf *sb);
386 struct sf_buf *
387         sf_buf_alloc(void);
388 void    sf_buf_free(void *addr, void *args);
389 int     sb_lock(struct sockbuf *sb);
390 int     soabort(struct socket *so);
391 int     soaccept(struct socket *so, struct sockaddr **nam);
392 int     socheckuid(struct socket *so, uid_t uid);
393 struct  socket *soalloc(int waitok);
394 int     sobind(struct socket *so, struct sockaddr *nam, struct thread *td);
395 void    socantrcvmore(struct socket *so);
396 void    socantsendmore(struct socket *so);
397 int     soclose(struct socket *so);
398 int     soconnect(struct socket *so, struct sockaddr *nam, struct thread *td);
399 int     soconnect2(struct socket *so1, struct socket *so2);
400 int     socow_setup(struct mbuf *m0, struct uio *uio);
401 int     socreate(int dom, struct socket **aso, int type, int proto,
402             struct ucred *cred, struct thread *td);
403 int     sodisconnect(struct socket *so);
404 void    sofree(struct socket *so);
405 int     sogetopt(struct socket *so, struct sockopt *sopt);
406 void    sohasoutofband(struct socket *so);
407 void    soisconnected(struct socket *so);
408 void    soisconnecting(struct socket *so);
409 void    soisdisconnected(struct socket *so);
410 void    soisdisconnecting(struct socket *so);
411 int     solisten(struct socket *so, int backlog, struct thread *td);
412 struct socket *
413         sonewconn(struct socket *head, int connstatus);
414 int     sooptcopyin(struct sockopt *sopt, void *buf, size_t len, size_t minlen);
415 int     sooptcopyout(struct sockopt *sopt, void *buf, size_t len);
416
417 /* XXX; prepare mbuf for (__FreeBSD__ < 3) routines. */
418 int     soopt_getm(struct sockopt *sopt, struct mbuf **mp);
419 int     soopt_mcopyin(struct sockopt *sopt, struct mbuf *m);
420 int     soopt_mcopyout(struct sockopt *sopt, struct mbuf *m);
421
422 int     sopoll(struct socket *so, int events, struct ucred *active_cred,
423             struct thread *td);
424 int     soreceive(struct socket *so, struct sockaddr **paddr, struct uio *uio,
425             struct mbuf **mp0, struct mbuf **controlp, int *flagsp);
426 int     soreserve(struct socket *so, u_long sndcc, u_long rcvcc);
427 void    sorflush(struct socket *so);
428 int     sosend(struct socket *so, struct sockaddr *addr, struct uio *uio,
429             struct mbuf *top, struct mbuf *control, int flags,
430             struct thread *td);
431 int     sosetopt(struct socket *so, struct sockopt *sopt);
432 int     soshutdown(struct socket *so, int how);
433 void    sotoxsocket(struct socket *so, struct xsocket *xso);
434 void    sowakeup(struct socket *so, struct sockbuf *sb);
435
436 /*
437  * Accept filter functions (duh).
438  */
439 int     accept_filt_add(struct accept_filter *filt);
440 int     accept_filt_del(char *name);
441 struct  accept_filter *accept_filt_get(char *name);
442 #ifdef ACCEPT_FILTER_MOD
443 #ifdef SYSCTL_DECL
444 SYSCTL_DECL(_net_inet_accf);
445 #endif
446 int     accept_filt_generic_mod_event(module_t mod, int event, void *data);
447 #endif
448
449 #endif /* _KERNEL */
450
451 #endif /* !_SYS_SOCKETVAR_H_ */