Merge branch 'vendor/OPENPAM'
[dragonfly.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: src/sys/sys/socketvar.h,v 1.46.2.10 2003/08/24 08:24:39 hsu Exp $
35  * $DragonFly: src/sys/sys/socketvar.h,v 1.35 2008/08/28 23:15:45 dillon Exp $
36  */
37
38 #ifndef _SYS_SOCKETVAR_H_
39 #define _SYS_SOCKETVAR_H_
40
41 #ifndef _SYS_TYPES_H_
42 #include <sys/types.h>
43 #endif
44 #ifndef _SYS_QUEUE_H_
45 #include <sys/queue.h>                  /* for TAILQ macros */
46 #endif
47 #ifndef _SYS_EVENT_H_
48 #include <sys/event.h>                  /* for struct kqinfo */
49 #endif
50 #ifndef _SYS_THREAD_H_
51 #include <sys/thread.h>                 /* for struct lwkt_token */
52 #endif
53 #ifndef _SYS_SOCKBUF_H_
54 #include <sys/sockbuf.h>
55 #endif
56
57 #if defined(_KERNEL) || defined(_KERNEL_STRUCTURES)
58
59 #ifndef _NET_NETMSG_H_
60 #include <net/netmsg.h>
61 #endif
62
63 struct accept_filter;
64
65 /*
66  * Signaling socket buffers contain additional elements for locking
67  * and signaling conditions.  These are used primarily by sockets.
68  *
69  * WARNING: See partial clearing of fields in kern/uipc_socket.c
70  *          sorflush() and sowflush().
71  */
72 struct signalsockbuf {
73         struct sockbuf sb;
74         struct kqinfo ssb_kq;   /* process selecting read/write */
75         uint32_t ssb_flags;     /* flags, see below (use atomic ops) */
76         u_int   ssb_timeo;      /* timeout for read/write */
77         long    ssb_lowat;      /* low water mark */
78         u_long  ssb_hiwat;      /* high water mark / max actual char count */
79         u_long  ssb_mbmax;      /* max chars of mbufs to use */
80         struct lwkt_token ssb_token; /* frontend/backend serializer */
81 };
82
83 #define ssb_cc          sb.sb_cc        /* commonly used fields */
84 #define ssb_mb          sb.sb_mb        /* commonly used fields */
85 #define ssb_mbcnt       sb.sb_mbcnt     /* commonly used fields */
86
87 #define SSB_LOCK        0x0001          /* lock on data queue */
88 #define SSB_WANT        0x0002          /* someone is waiting to lock */
89 #define SSB_WAIT        0x0004          /* someone is waiting for data/space */
90 #define SSB_ASYNC       0x0010          /* ASYNC I/O, need signals */
91 #define SSB_UPCALL      0x0020          /* someone wants an upcall */
92 #define SSB_NOINTR      0x0040          /* operations not interruptible */
93 /*#define SSB_AIO       0x0080*/        /* AIO operations queued */
94 #define SSB_KNOTE       0x0100          /* kernel note attached */
95 #define SSB_MEVENT      0x0200          /* need message event notification */
96 #define SSB_STOP        0x0400          /* backpressure indicator */
97 #define SSB_AUTOSIZE    0x0800          /* automatically size socket buffer */
98 #define SSB_AUTOLOWAT   0x1000          /* automatically scale lowat */
99 #define SSB_WAKEUP      0x2000          /* wakeup event race */
100
101 #define SSB_CLEAR_MASK  (SSB_ASYNC | SSB_UPCALL | SSB_STOP | \
102                          SSB_AUTOSIZE | SSB_AUTOLOWAT)
103
104 #define SSB_NOTIFY_MASK (SSB_WAIT | SSB_ASYNC | SSB_UPCALL | \
105                          SSB_KNOTE | SSB_MEVENT)
106
107 /*
108  * Per-socket kernel structure.  Contains universal send and receive queues,
109  * protocol control handle, and error information.
110  */
111 struct socket {
112         short   so_type;                /* generic type, see socket.h */
113         short   so_options;             /* from socket call, see socket.h */
114         short   so_linger;              /* time to linger while closing */
115         short   so_state;               /* internal state flags SS_*, below */
116         void    *so_pcb;                /* protocol control block */
117         struct  protosw *so_proto;      /* protocol handle */
118         struct  socket *so_head;        /* back pointer to accept socket */
119         lwkt_port_t so_port;            /* message port */
120
121         /*
122          * These fields are used to manage sockets capable of accepting
123          * new connections.
124          */
125         TAILQ_HEAD(, socket) so_incomp; /* in-progress, incomplete */
126         TAILQ_HEAD(, socket) so_comp;   /* completed but not yet accepted */
127         TAILQ_ENTRY(socket) so_list;    /* list of unaccepted connections */
128         short   so_qlen;                /* so_comp count */
129         short   so_incqlen;             /* so_incomp count */
130         short   so_qlimit;              /* max number queued connections */
131
132         /*
133          * Misc socket support
134          */
135         short   so_timeo;               /* connection timeout */
136         u_short so_error;               /* error affecting connection */
137         struct  sigio *so_sigio;        /* information for async I/O or
138                                            out of band data (SIGURG) */
139         u_long  so_oobmark;             /* chars to oob mark */
140         TAILQ_HEAD(, aiocblist) so_aiojobq; /* AIO ops waiting on socket */
141         struct signalsockbuf so_rcv;
142         struct signalsockbuf so_snd;
143
144         void    (*so_upcall) (struct socket *, void *, int);
145         void    *so_upcallarg;
146         struct  ucred *so_cred;         /* user credentials */
147         /* NB: generation count must not be first; easiest to make it last. */
148         void    *so_emuldata;           /* private data for emulators */
149         int     so_refs;                /* shutdown refs */
150         struct  so_accf { 
151                 struct  accept_filter *so_accept_filter;
152                 void    *so_accept_filter_arg;  /* saved filter args */
153                 char    *so_accept_filter_str;  /* saved user args */
154         } *so_accf;
155
156         struct netmsg_base so_clomsg;
157         struct sockaddr *so_faddr;
158 };
159
160 #endif
161
162 /*
163  * Socket state bits.
164  *
165  * NOTE: The following states are interlocked with so_refs:
166  *
167  *      SS_NOFDREF      so_refs while not set
168  *      (so_pcb)        so_refs while set
169  */
170 #define SS_NOFDREF              0x0001  /* no file table ref any more */
171 #define SS_ISCONNECTED          0x0002  /* socket connected to a peer */
172 #define SS_ISCONNECTING         0x0004  /* in process of connecting to peer */
173 #define SS_ISDISCONNECTING      0x0008  /* in process of disconnecting */
174 #define SS_CANTSENDMORE         0x0010  /* can't send more data to peer */
175 #define SS_CANTRCVMORE          0x0020  /* can't receive more data from peer */
176 #define SS_RCVATMARK            0x0040  /* at mark on input */
177
178 #define SS_ASSERTINPROG         0x0100  /* sonewconn race debugging */
179 #define SS_ASYNC                0x0200  /* async i/o notify */
180 #define SS_ISCONFIRMING         0x0400  /* deciding to accept connection req */
181
182 #define SS_INCOMP               0x0800  /* unaccepted, incomplete connection */
183 #define SS_COMP                 0x1000  /* unaccepted, complete connection */
184 #define SS_ISDISCONNECTED       0x2000  /* socket disconnected from peer */
185
186 /*
187  * Externalized form of struct socket used by the sysctl(3) interface.
188  */
189 struct  xsocket {
190         size_t  xso_len;        /* length of this structure */
191         struct  socket *xso_so; /* makes a convenient handle sometimes */
192         short   so_type;
193         short   so_options;
194         short   so_linger;
195         short   so_state;
196         void    *so_pcb;                /* another convenient handle */
197         int     xso_protocol;
198         int     xso_family;
199         short   so_qlen;
200         short   so_incqlen;
201         short   so_qlimit;
202         short   so_timeo;
203         u_short so_error;
204         pid_t   so_pgid;
205         u_long  so_oobmark;
206         struct  xsockbuf {
207                 u_long  sb_cc;
208                 u_long  sb_hiwat;
209                 u_long  sb_mbcnt;
210                 u_long  sb_mbmax;
211                 long    sb_lowat;
212                 u_int   sb_timeo;
213                 short   sb_flags;
214         } so_rcv, so_snd;
215         uid_t   so_uid;         /* XXX */
216 };
217
218 /*
219  * Macros for sockets and socket buffering.
220  */
221
222 #define sosendallatonce(so) \
223     ((so)->so_proto->pr_flags & PR_ATOMIC)
224
225 /* can we read something from so? */
226 #define soreadable(so) \
227     ((so)->so_rcv.ssb_cc >= (so)->so_rcv.ssb_lowat || \
228         ((so)->so_state & SS_CANTRCVMORE) || \
229         !TAILQ_EMPTY(&(so)->so_comp) || (so)->so_error)
230
231 /* can we write something to so? */
232 #define sowriteable(so) \
233     ((ssb_space(&(so)->so_snd) >= (so)->so_snd.ssb_lowat && \
234         (((so)->so_state&SS_ISCONNECTED) || \
235           ((so)->so_proto->pr_flags&PR_CONNREQUIRED)==0)) || \
236      ((so)->so_state & SS_CANTSENDMORE) || \
237      (so)->so_error)
238
239 /*
240  * Do we need to notify the other side when I/O is possible?
241  *
242  * NOTE: Interlock for ssb_wait/wakeup.  The protocol side will set
243  *       SSB_WAKEUP asynchronously and this can race, so if it isn't
244  *       set we have to go through the full-on notification check.
245  *       If it is set but no waiting ever takes place it simply
246  *       remains set.
247  */
248 #define ssb_notify(ssb)                                 \
249             (((ssb)->ssb_flags & SSB_NOTIFY_MASK) ||    \
250              ((ssb)->ssb_flags & SSB_WAKEUP) == 0)
251
252 /* do we have to send all at once on a socket? */
253
254 #ifdef _KERNEL
255
256 /*
257  * How much space is there in a socket buffer (so->so_snd or so->so_rcv)?
258  * This is problematical if the fields are unsigned, as the space might
259  * still be negative (cc > hiwat or mbcnt > mbmax).  Should detect
260  * overflow and return 0.
261  *
262  * SSB_STOP ignores cc/hiwat and returns 0.  This is used by unix domain
263  * stream sockets to signal backpressure.
264  */
265 static __inline
266 long
267 ssb_space(struct signalsockbuf *ssb)
268 {
269         long bleft;
270         long mleft;
271
272         if (ssb->ssb_flags & SSB_STOP)
273                 return(0);
274         bleft = ssb->ssb_hiwat - ssb->ssb_cc;
275         mleft = ssb->ssb_mbmax - ssb->ssb_mbcnt;
276         return((bleft < mleft) ? bleft : mleft);
277 }
278
279 #endif
280
281 #define ssb_append(ssb, m)                                              \
282         sbappend(&(ssb)->sb, m)
283
284 #define ssb_appendstream(ssb, m)                                        \
285         sbappendstream(&(ssb)->sb, m)
286
287 #define ssb_appendrecord(ssb, m)                                        \
288         sbappendrecord(&(ssb)->sb, m)
289
290 #define ssb_appendaddr(ssb, src, m, control)                            \
291         ((ssb_space(ssb) <= 0) ? 0 : sbappendaddr(&(ssb)->sb, src, m, control))
292
293 #define ssb_appendcontrol(ssb, m, control)                              \
294         ((ssb_space(ssb) <= 0) ? 0 : sbappendcontrol(&(ssb)->sb, m, control))
295
296 #define ssb_insert_knote(ssb, kn) {                                     \
297         knote_insert(&(ssb)->ssb_kq.ki_note, kn);                       \
298         atomic_set_int(&(ssb)->ssb_flags, SSB_KNOTE);                   \
299 }
300
301 #define ssb_remove_knote(ssb, kn) {                                     \
302         knote_remove(&(ssb)->ssb_kq.ki_note, kn);                       \
303         if (SLIST_EMPTY(&(ssb)->ssb_kq.ki_note))                        \
304                 atomic_clear_int(&(ssb)->ssb_flags, SSB_KNOTE);         \
305 }
306
307 #define sorwakeup(so)                                           \
308         do {                                                    \
309                 if (ssb_notify(&(so)->so_rcv))                  \
310                         sowakeup((so), &(so)->so_rcv);          \
311         } while (0)
312
313 #define sowwakeup(so)                                           \
314         do {                                                    \
315                 if (ssb_notify(&(so)->so_snd))                  \
316                         sowakeup((so), &(so)->so_snd);          \
317         } while (0)
318
319 #ifdef _KERNEL
320
321 /*
322  * Argument structure for sosetopt et seq.  This is in the KERNEL
323  * section because it will never be visible to user code.
324  */
325 enum sopt_dir { SOPT_GET, SOPT_SET };
326 struct sockopt {
327         enum    sopt_dir sopt_dir; /* is this a get or a set? */
328         int     sopt_level;     /* second arg of [gs]etsockopt */
329         int     sopt_name;      /* third arg of [gs]etsockopt */
330         void   *sopt_val;       /* fourth arg of [gs]etsockopt */
331         size_t  sopt_valsize;   /* (almost) fifth arg of [gs]etsockopt */
332         struct  thread *sopt_td; /* calling thread or null if kernel */
333 };
334
335 struct accept_filter {
336         char    accf_name[16];
337         void    (*accf_callback)
338                 (struct socket *so, void *arg, int waitflag);
339         void *  (*accf_create)
340                 (struct socket *so, char *arg);
341         void    (*accf_destroy)
342                 (struct socket *so);
343         SLIST_ENTRY(accept_filter) accf_next;   /* next on the list */
344 };
345
346 #ifdef MALLOC_DECLARE
347 MALLOC_DECLARE(M_PCB);
348 MALLOC_DECLARE(M_SONAME);
349 MALLOC_DECLARE(M_ACCF);
350 #endif
351
352 extern int      maxsockets;
353 extern u_long   sb_max;         /* nominal limit */
354 extern u_long   sb_max_adj;     /* actual limit used by sbreserve() */
355
356 struct file;
357 struct filedesc;
358 struct mbuf;
359 struct rlimit;
360 struct sockaddr;
361 struct stat;
362 struct ucred;
363 struct uio;
364 struct knote;
365 struct sysmsg;
366
367 /*
368  * File operations on sockets.
369  */
370 int     soo_read (struct file *fp, struct uio *uio, struct ucred *cred,
371                         int flags);
372 int     soo_write (struct file *fp, struct uio *uio, struct ucred *cred,
373                         int flags);
374 int     soo_close (struct file *fp);
375 int     soo_shutdown (struct file *fp, int how);
376 int     soo_ioctl (struct file *fp, u_long cmd, caddr_t data,
377                         struct ucred *cred, struct sysmsg *msg);
378 int     soo_stat (struct file *fp, struct stat *ub, struct ucred *cred);
379 int     sokqfilter (struct file *fp, struct knote *kn);
380
381 /*
382  * From uipc_socket and friends
383  */
384 struct  sockaddr *dup_sockaddr (const struct sockaddr *sa);
385 int     getsockaddr (struct sockaddr **namp, caddr_t uaddr, size_t len);
386
387 void    ssb_release (struct signalsockbuf *ssb, struct socket *so);
388 int     ssb_reserve (struct signalsockbuf *ssb, u_long cc, struct socket *so,
389                    struct rlimit *rl);
390 void    ssbtoxsockbuf (struct signalsockbuf *sb, struct xsockbuf *xsb);
391 int     ssb_wait (struct signalsockbuf *sb);
392 int     _ssb_lock (struct signalsockbuf *sb);
393
394 void    soabort (struct socket *so);
395 void    soaborta (struct socket *so);
396 void    soabort_oncpu (struct socket *so);
397 int     soaccept (struct socket *so, struct sockaddr **nam);
398 void    soaccept_generic (struct socket *so);
399 struct  socket *soalloc (int waitok);
400 int     sobind (struct socket *so, struct sockaddr *nam, struct thread *td);
401 void    socantrcvmore (struct socket *so);
402 void    socantsendmore (struct socket *so);
403 int     socket_wait (struct socket *so, struct timespec *ts, int *res);
404 int     soclose (struct socket *so, int fflags);
405 int     soconnect (struct socket *so, struct sockaddr *nam, struct thread *td);
406 int     soconnect2 (struct socket *so1, struct socket *so2);
407 int     socreate (int dom, struct socket **aso, int type, int proto,
408             struct thread *td);
409 int     sodisconnect (struct socket *so);
410 void    sofree (struct socket *so);
411 int     sogetopt (struct socket *so, struct sockopt *sopt);
412 void    sohasoutofband (struct socket *so);
413 void    soisconnected (struct socket *so);
414 void    soisconnecting (struct socket *so);
415 void    soisdisconnected (struct socket *so);
416 void    soisdisconnecting (struct socket *so);
417 void    soisreconnected (struct socket *so);
418 void    soisreconnecting (struct socket *so);
419 void    sosetport (struct socket *so, struct lwkt_port *port);
420 int     solisten (struct socket *so, int backlog, struct thread *td);
421 struct socket *sonewconn (struct socket *head, int connstatus);
422 struct socket *sonewconn_faddr (struct socket *head, int connstatus,
423             const struct sockaddr *faddr);
424 int     sooptcopyin (struct sockopt *sopt, void *buf, size_t len,
425                          size_t minlen);
426 int     soopt_to_kbuf (struct sockopt *sopt, void *buf, size_t len,
427                          size_t minlen);
428 int     sooptcopyout (struct sockopt *sopt, const void *buf, size_t len);
429 void    soopt_from_kbuf (struct sockopt *sopt, const void *buf, size_t len);
430
431 /* XXX; prepare mbuf for (__FreeBSD__ < 3) routines. */
432 int     soopt_getm (struct sockopt *sopt, struct mbuf **mp);
433 int     soopt_mcopyin (struct sockopt *sopt, struct mbuf *m);
434 void    soopt_to_mbuf (struct sockopt *sopt, struct mbuf *m);
435 int     soopt_mcopyout (struct sockopt *sopt, struct mbuf *m);
436 int     soopt_from_mbuf (struct sockopt *sopt, struct mbuf *m);
437
438 int     soreceive (struct socket *so, struct sockaddr **paddr,
439                        struct uio *uio, struct sockbuf *sio,
440                        struct mbuf **controlp, int *flagsp);
441 int     soreserve (struct socket *so, u_long sndcc, u_long rcvcc,
442                    struct rlimit *rl);
443 void    sorflush (struct socket *so);
444 int     sosend (struct socket *so, struct sockaddr *addr, struct uio *uio,
445                     struct mbuf *top, struct mbuf *control, int flags,
446                     struct thread *td);
447 int     sosendudp (struct socket *so, struct sockaddr *addr, struct uio *uio,
448                     struct mbuf *top, struct mbuf *control, int flags,
449                     struct thread *td);
450 int     sosendtcp (struct socket *so, struct sockaddr *addr, struct uio *uio,
451                     struct mbuf *top, struct mbuf *control, int flags,
452                     struct thread *td);
453 int     sosetopt (struct socket *so, struct sockopt *sopt);
454 int     soshutdown (struct socket *so, int how);
455 void    sotoxsocket (struct socket *so, struct xsocket *xso);
456 void    sowakeup (struct socket *so, struct signalsockbuf *sb);
457
458 /* accept filter functions */
459 int     accept_filt_add (struct accept_filter *filt);
460 int     accept_filt_del (char *name);
461 struct accept_filter *  accept_filt_get (char *name);
462 #ifdef ACCEPT_FILTER_MOD
463 int accept_filt_generic_mod_event (module_t mod, int event, void *data);
464 SYSCTL_DECL(_net_inet_accf);
465 #endif /* ACCEPT_FILTER_MOD */
466
467 #endif /* _KERNEL */
468
469 #endif /* !_SYS_SOCKETVAR_H_ */