]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/sys/socketvar.h
dts: Update our device tree sources file fomr Linux 4.13
[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. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *      @(#)socketvar.h 8.3 (Berkeley) 2/19/95
30  *
31  * $FreeBSD$
32  */
33
34 #ifndef _SYS_SOCKETVAR_H_
35 #define _SYS_SOCKETVAR_H_
36
37 /*
38  * Socket generation count type.  Also used in xinpcb, xtcpcb, xunpcb.
39  */
40 typedef uint64_t so_gen_t;
41
42 #if defined(_KERNEL) || defined(_WANT_SOCKET)
43 #include <sys/queue.h>                  /* for TAILQ macros */
44 #include <sys/selinfo.h>                /* for struct selinfo */
45 #include <sys/_lock.h>
46 #include <sys/_mutex.h>
47 #include <sys/osd.h>
48 #include <sys/_sx.h>
49 #include <sys/sockbuf.h>
50 #ifdef _KERNEL
51 #include <sys/caprights.h>
52 #include <sys/sockopt.h>
53 #endif
54
55 struct vnet;
56
57 /*
58  * Kernel structure per socket.
59  * Contains send and receive buffer queues,
60  * handle on protocol and pointer to protocol
61  * private data and error information.
62  */
63 typedef int so_upcall_t(struct socket *, void *, int);
64
65 struct socket;
66
67 /*-
68  * Locking key to struct socket:
69  * (a) constant after allocation, no locking required.
70  * (b) locked by SOCK_LOCK(so).
71  * (cr) locked by SOCKBUF_LOCK(&so->so_rcv).
72  * (cs) locked by SOCKBUF_LOCK(&so->so_rcv).
73  * (e) locked by SOLISTEN_LOCK() of corresponding listening socket.
74  * (f) not locked since integer reads/writes are atomic.
75  * (g) used only as a sleep/wakeup address, no value.
76  * (h) locked by global mutex so_global_mtx.
77  */
78 TAILQ_HEAD(accept_queue, socket);
79 struct socket {
80         struct mtx      so_lock;
81         volatile u_int  so_count;       /* (b / refcount) */
82         struct selinfo  so_rdsel;       /* (b/cr) for so_rcv/so_comp */
83         struct selinfo  so_wrsel;       /* (b/cs) for so_snd */
84         short   so_type;                /* (a) generic type, see socket.h */
85         short   so_options;             /* (b) from socket call, see socket.h */
86         short   so_linger;              /* time to linger close(2) */
87         short   so_state;               /* (b) internal state flags SS_* */
88         void    *so_pcb;                /* protocol control block */
89         struct  vnet *so_vnet;          /* (a) network stack instance */
90         struct  protosw *so_proto;      /* (a) protocol handle */
91         short   so_timeo;               /* (g) connection timeout */
92         u_short so_error;               /* (f) error affecting connection */
93         struct  sigio *so_sigio;        /* [sg] information for async I/O or
94                                            out of band data (SIGURG) */
95         struct  ucred *so_cred;         /* (a) user credentials */
96         struct  label *so_label;        /* (b) MAC label for socket */
97         /* NB: generation count must not be first. */
98         so_gen_t so_gencnt;             /* (h) generation count */
99         void    *so_emuldata;           /* (b) private data for emulators */
100         struct  osd     osd;            /* Object Specific extensions */
101         /*
102          * so_fibnum, so_user_cookie and friends can be used to attach
103          * some user-specified metadata to a socket, which then can be
104          * used by the kernel for various actions.
105          * so_user_cookie is used by ipfw/dummynet.
106          */
107         int so_fibnum;          /* routing domain for this socket */
108         uint32_t so_user_cookie;
109
110         int so_ts_clock;        /* type of the clock used for timestamps */
111         uint32_t so_max_pacing_rate;    /* (f) TX rate limit in bytes/s */
112         union {
113                 /* Regular (data flow) socket. */
114                 struct {
115                         /* (cr, cs) Receive and send buffers. */
116                         struct sockbuf          so_rcv, so_snd;
117
118                         /* (e) Our place on accept queue. */
119                         TAILQ_ENTRY(socket)     so_list;
120                         struct socket           *so_listen;     /* (b) */
121                         enum {
122                                 SQ_NONE = 0,
123                                 SQ_INCOMP = 0x0800,     /* on sol_incomp */
124                                 SQ_COMP = 0x1000,       /* on sol_comp */
125                         }                       so_qstate;      /* (b) */
126
127                         /* (b) cached MAC label for peer */
128                         struct  label           *so_peerlabel;
129                         u_long  so_oobmark;     /* chars to oob mark */
130                 };
131                 /*
132                  * Listening socket, where accepts occur, is so_listen in all
133                  * subsidiary sockets.  If so_listen is NULL, socket is not
134                  * related to an accept.  For a listening socket itself
135                  * sol_incomp queues partially completed connections, while
136                  * sol_comp is a queue of connections ready to be accepted.
137                  * If a connection is aborted and it has so_listen set, then
138                  * it has to be pulled out of either sol_incomp or sol_comp.
139                  * We allow connections to queue up based on current queue
140                  * lengths and limit on number of queued connections for this
141                  * socket.
142                  */
143                 struct {
144                         /* (e) queue of partial unaccepted connections */
145                         struct accept_queue     sol_incomp;
146                         /* (e) queue of complete unaccepted connections */
147                         struct accept_queue     sol_comp;
148                         u_int   sol_qlen;    /* (e) sol_comp length */
149                         u_int   sol_incqlen; /* (e) sol_incomp length */
150                         u_int   sol_qlimit;  /* (e) queue limit */
151
152                         /* accept_filter(9) optional data */
153                         struct  accept_filter   *sol_accept_filter;
154                         void    *sol_accept_filter_arg; /* saved filter args */
155                         char    *sol_accept_filter_str; /* saved user args */
156
157                         /* Optional upcall, for kernel socket. */
158                         so_upcall_t     *sol_upcall;    /* (e) */
159                         void            *sol_upcallarg; /* (e) */
160
161                         /* Socket buffer parameters, to be copied to
162                          * dataflow sockets, accepted from this one. */
163                         int             sol_sbrcv_lowat;
164                         int             sol_sbsnd_lowat;
165                         u_int           sol_sbrcv_hiwat;
166                         u_int           sol_sbsnd_hiwat;
167                         short           sol_sbrcv_flags;
168                         short           sol_sbsnd_flags;
169                         sbintime_t      sol_sbrcv_timeo;
170                         sbintime_t      sol_sbsnd_timeo;
171                 };
172         };
173 };
174 #endif  /* defined(_KERNEL) || defined(_WANT_SOCKET) */
175
176 /*
177  * Socket state bits.
178  *
179  * Historically, this bits were all kept in the so_state field.  For
180  * locking reasons, they are now in multiple fields, as they are
181  * locked differently.  so_state maintains basic socket state protected
182  * by the socket lock.  so_qstate holds information about the socket
183  * accept queues.  Each socket buffer also has a state field holding
184  * information relevant to that socket buffer (can't send, rcv).  Many
185  * fields will be read without locks to improve performance and avoid
186  * lock order issues.  However, this approach must be used with caution.
187  */
188 #define SS_NOFDREF              0x0001  /* no file table ref any more */
189 #define SS_ISCONNECTED          0x0002  /* socket connected to a peer */
190 #define SS_ISCONNECTING         0x0004  /* in process of connecting to peer */
191 #define SS_ISDISCONNECTING      0x0008  /* in process of disconnecting */
192 #define SS_NBIO                 0x0100  /* non-blocking ops */
193 #define SS_ASYNC                0x0200  /* async i/o notify */
194 #define SS_ISCONFIRMING         0x0400  /* deciding to accept connection req */
195 #define SS_ISDISCONNECTED       0x2000  /* socket disconnected from peer */
196
197 /*
198  * Protocols can mark a socket as SS_PROTOREF to indicate that, following
199  * pru_detach, they still want the socket to persist, and will free it
200  * themselves when they are done.  Protocols should only ever call sofree()
201  * following setting this flag in pru_detach(), and never otherwise, as
202  * sofree() bypasses socket reference counting.
203  */
204 #define SS_PROTOREF             0x4000  /* strong protocol reference */
205
206 #ifdef _KERNEL
207
208 #define SOCK_MTX(so)            &(so)->so_lock
209 #define SOCK_LOCK(so)           mtx_lock(&(so)->so_lock)
210 #define SOCK_OWNED(so)          mtx_owned(&(so)->so_lock)
211 #define SOCK_UNLOCK(so)         mtx_unlock(&(so)->so_lock)
212 #define SOCK_LOCK_ASSERT(so)    mtx_assert(&(so)->so_lock, MA_OWNED)
213 #define SOCK_UNLOCK_ASSERT(so)  mtx_assert(&(so)->so_lock, MA_NOTOWNED)
214
215 #define SOLISTENING(sol)        (((sol)->so_options & SO_ACCEPTCONN) != 0)
216 #define SOLISTEN_LOCK(sol)      do {                                    \
217         mtx_lock(&(sol)->so_lock);                                      \
218         KASSERT(SOLISTENING(sol),                                       \
219             ("%s: %p not listening", __func__, (sol)));                 \
220 } while (0)
221 #define SOLISTEN_TRYLOCK(sol)   mtx_trylock(&(sol)->so_lock)
222 #define SOLISTEN_UNLOCK(sol)    do {                                    \
223         KASSERT(SOLISTENING(sol),                                       \
224             ("%s: %p not listening", __func__, (sol)));                 \
225         mtx_unlock(&(sol)->so_lock);                                    \
226 } while (0)
227 #define SOLISTEN_LOCK_ASSERT(sol)       do {                            \
228         mtx_assert(&(sol)->so_lock, MA_OWNED);                          \
229         KASSERT(SOLISTENING(sol),                                       \
230             ("%s: %p not listening", __func__, (sol)));                 \
231 } while (0)
232
233 /*
234  * Macros for sockets and socket buffering.
235  */
236
237 /*
238  * Flags to sblock().
239  */
240 #define SBL_WAIT        0x00000001      /* Wait if not immediately available. */
241 #define SBL_NOINTR      0x00000002      /* Force non-interruptible sleep. */
242 #define SBL_VALID       (SBL_WAIT | SBL_NOINTR)
243
244 /*
245  * Do we need to notify the other side when I/O is possible?
246  */
247 #define sb_notify(sb)   (((sb)->sb_flags & (SB_WAIT | SB_SEL | SB_ASYNC | \
248     SB_UPCALL | SB_AIO | SB_KNOTE)) != 0)
249
250 /* do we have to send all at once on a socket? */
251 #define sosendallatonce(so) \
252     ((so)->so_proto->pr_flags & PR_ATOMIC)
253
254 /* can we read something from so? */
255 #define soreadabledata(so) \
256         (sbavail(&(so)->so_rcv) >= (so)->so_rcv.sb_lowat ||  (so)->so_error)
257 #define soreadable(so) \
258         (soreadabledata(so) || ((so)->so_rcv.sb_state & SBS_CANTRCVMORE))
259
260 /* can we write something to so? */
261 #define sowriteable(so) \
262     ((sbspace(&(so)->so_snd) >= (so)->so_snd.sb_lowat && \
263         (((so)->so_state&SS_ISCONNECTED) || \
264           ((so)->so_proto->pr_flags&PR_CONNREQUIRED)==0)) || \
265      ((so)->so_snd.sb_state & SBS_CANTSENDMORE) || \
266      (so)->so_error)
267
268 /*
269  * soref()/sorele() ref-count the socket structure.
270  * soref() may be called without owning socket lock, but in that case a
271  * caller must own something that holds socket, and so_count must be not 0.
272  * Note that you must still explicitly close the socket, but the last ref
273  * count will free the structure.
274  */
275 #define soref(so)       refcount_acquire(&(so)->so_count)
276 #define sorele(so) do {                                                 \
277         SOCK_LOCK_ASSERT(so);                                           \
278         if (refcount_release(&(so)->so_count))                          \
279                 sofree(so);                                             \
280         else                                                            \
281                 SOCK_UNLOCK(so);                                        \
282 } while (0)
283
284 /*
285  * In sorwakeup() and sowwakeup(), acquire the socket buffer lock to
286  * avoid a non-atomic test-and-wakeup.  However, sowakeup is
287  * responsible for releasing the lock if it is called.  We unlock only
288  * if we don't call into sowakeup.  If any code is introduced that
289  * directly invokes the underlying sowakeup() primitives, it must
290  * maintain the same semantics.
291  */
292 #define sorwakeup_locked(so) do {                                       \
293         SOCKBUF_LOCK_ASSERT(&(so)->so_rcv);                             \
294         if (sb_notify(&(so)->so_rcv))                                   \
295                 sowakeup((so), &(so)->so_rcv);                          \
296         else                                                            \
297                 SOCKBUF_UNLOCK(&(so)->so_rcv);                          \
298 } while (0)
299
300 #define sorwakeup(so) do {                                              \
301         SOCKBUF_LOCK(&(so)->so_rcv);                                    \
302         sorwakeup_locked(so);                                           \
303 } while (0)
304
305 #define sowwakeup_locked(so) do {                                       \
306         SOCKBUF_LOCK_ASSERT(&(so)->so_snd);                             \
307         if (sb_notify(&(so)->so_snd))                                   \
308                 sowakeup((so), &(so)->so_snd);                          \
309         else                                                            \
310                 SOCKBUF_UNLOCK(&(so)->so_snd);                          \
311 } while (0)
312
313 #define sowwakeup(so) do {                                              \
314         SOCKBUF_LOCK(&(so)->so_snd);                                    \
315         sowwakeup_locked(so);                                           \
316 } while (0)
317
318 struct accept_filter {
319         char    accf_name[16];
320         int     (*accf_callback)
321                 (struct socket *so, void *arg, int waitflag);
322         void *  (*accf_create)
323                 (struct socket *so, char *arg);
324         void    (*accf_destroy)
325                 (struct socket *so);
326         SLIST_ENTRY(accept_filter) accf_next;
327 };
328
329 #ifdef MALLOC_DECLARE
330 MALLOC_DECLARE(M_ACCF);
331 MALLOC_DECLARE(M_PCB);
332 MALLOC_DECLARE(M_SONAME);
333 #endif
334
335 /*
336  * Socket specific helper hook point identifiers
337  * Do not leave holes in the sequence, hook registration is a loop.
338  */
339 #define HHOOK_SOCKET_OPT                0
340 #define HHOOK_SOCKET_CREATE             1
341 #define HHOOK_SOCKET_RCV                2
342 #define HHOOK_SOCKET_SND                3
343 #define HHOOK_FILT_SOREAD               4
344 #define HHOOK_FILT_SOWRITE              5
345 #define HHOOK_SOCKET_CLOSE              6
346 #define HHOOK_SOCKET_LAST               HHOOK_SOCKET_CLOSE
347
348 struct socket_hhook_data {
349         struct socket   *so;
350         struct mbuf     *m;
351         void            *hctx;          /* hook point specific data*/
352         int             status;
353 };
354
355 extern int      maxsockets;
356 extern u_long   sb_max;
357 extern so_gen_t so_gencnt;
358
359 struct file;
360 struct filecaps;
361 struct filedesc;
362 struct mbuf;
363 struct sockaddr;
364 struct ucred;
365 struct uio;
366
367 /* 'which' values for socket upcalls. */
368 #define SO_RCV          1
369 #define SO_SND          2
370
371 /* Return values for socket upcalls. */
372 #define SU_OK           0
373 #define SU_ISCONNECTED  1
374
375 /*
376  * From uipc_socket and friends
377  */
378 int     getsockaddr(struct sockaddr **namp, caddr_t uaddr, size_t len);
379 int     getsock_cap(struct thread *td, int fd, cap_rights_t *rightsp,
380             struct file **fpp, u_int *fflagp, struct filecaps *havecaps);
381 void    soabort(struct socket *so);
382 int     soaccept(struct socket *so, struct sockaddr **nam);
383 void    soaio_enqueue(struct task *task);
384 void    soaio_rcv(void *context, int pending);
385 void    soaio_snd(void *context, int pending);
386 int     socheckuid(struct socket *so, uid_t uid);
387 int     sobind(struct socket *so, struct sockaddr *nam, struct thread *td);
388 int     sobindat(int fd, struct socket *so, struct sockaddr *nam,
389             struct thread *td);
390 int     soclose(struct socket *so);
391 int     soconnect(struct socket *so, struct sockaddr *nam, struct thread *td);
392 int     soconnectat(int fd, struct socket *so, struct sockaddr *nam,
393             struct thread *td);
394 int     soconnect2(struct socket *so1, struct socket *so2);
395 int     socreate(int dom, struct socket **aso, int type, int proto,
396             struct ucred *cred, struct thread *td);
397 int     sodisconnect(struct socket *so);
398 struct  sockaddr *sodupsockaddr(const struct sockaddr *sa, int mflags);
399 void    sofree(struct socket *so);
400 void    sohasoutofband(struct socket *so);
401 int     solisten(struct socket *so, int backlog, struct thread *td);
402 void    solisten_proto(struct socket *so, int backlog);
403 int     solisten_proto_check(struct socket *so);
404 int     solisten_dequeue(struct socket *, struct socket **, int);
405 struct socket *
406         sonewconn(struct socket *head, int connstatus);
407 struct socket *
408         sopeeloff(struct socket *);
409 int     sopoll(struct socket *so, int events, struct ucred *active_cred,
410             struct thread *td);
411 int     sopoll_generic(struct socket *so, int events,
412             struct ucred *active_cred, struct thread *td);
413 int     soreceive(struct socket *so, struct sockaddr **paddr, struct uio *uio,
414             struct mbuf **mp0, struct mbuf **controlp, int *flagsp);
415 int     soreceive_stream(struct socket *so, struct sockaddr **paddr,
416             struct uio *uio, struct mbuf **mp0, struct mbuf **controlp,
417             int *flagsp);
418 int     soreceive_dgram(struct socket *so, struct sockaddr **paddr,
419             struct uio *uio, struct mbuf **mp0, struct mbuf **controlp,
420             int *flagsp);
421 int     soreceive_generic(struct socket *so, struct sockaddr **paddr,
422             struct uio *uio, struct mbuf **mp0, struct mbuf **controlp,
423             int *flagsp);
424 int     soreserve(struct socket *so, u_long sndcc, u_long rcvcc);
425 void    sorflush(struct socket *so);
426 int     sosend(struct socket *so, struct sockaddr *addr, struct uio *uio,
427             struct mbuf *top, struct mbuf *control, int flags,
428             struct thread *td);
429 int     sosend_dgram(struct socket *so, struct sockaddr *addr,
430             struct uio *uio, struct mbuf *top, struct mbuf *control,
431             int flags, struct thread *td);
432 int     sosend_generic(struct socket *so, struct sockaddr *addr,
433             struct uio *uio, struct mbuf *top, struct mbuf *control,
434             int flags, struct thread *td);
435 int     soshutdown(struct socket *so, int how);
436 void    soupcall_clear(struct socket *, int);
437 void    soupcall_set(struct socket *, int, so_upcall_t, void *);
438 void    solisten_upcall_set(struct socket *, so_upcall_t, void *);
439 void    sowakeup(struct socket *so, struct sockbuf *sb);
440 void    sowakeup_aio(struct socket *so, struct sockbuf *sb);
441 void    solisten_wakeup(struct socket *);
442 int     selsocket(struct socket *so, int events, struct timeval *tv,
443             struct thread *td);
444 void    soisconnected(struct socket *so);
445 void    soisconnecting(struct socket *so);
446 void    soisdisconnected(struct socket *so);
447 void    soisdisconnecting(struct socket *so);
448 void    socantrcvmore(struct socket *so);
449 void    socantrcvmore_locked(struct socket *so);
450 void    socantsendmore(struct socket *so);
451 void    socantsendmore_locked(struct socket *so);
452
453 /*
454  * Accept filter functions (duh).
455  */
456 int     accept_filt_add(struct accept_filter *filt);
457 int     accept_filt_del(char *name);
458 struct  accept_filter *accept_filt_get(char *name);
459 #ifdef ACCEPT_FILTER_MOD
460 #ifdef SYSCTL_DECL
461 SYSCTL_DECL(_net_inet_accf);
462 #endif
463 int     accept_filt_generic_mod_event(module_t mod, int event, void *data);
464 #endif
465
466 #endif /* _KERNEL */
467
468 /*
469  * Structure to export socket from kernel to utilities, via sysctl(3).
470  */
471 struct xsocket {
472         size_t          xso_len;        /* length of this structure */
473         union {
474                 void    *xso_so;        /* kernel address of struct socket */
475                 int64_t ph_so;
476         };
477         union {
478                 void    *so_pcb;        /* kernel address of struct inpcb */
479                 int64_t ph_pcb;
480         };
481         uint64_t        so_oobmark;
482         int64_t         so_spare64[8];
483         int32_t         xso_protocol;
484         int32_t         xso_family;
485         uint32_t        so_qlen;
486         uint32_t        so_incqlen;
487         uint32_t        so_qlimit;
488         pid_t           so_pgid;
489         uid_t           so_uid;
490         int32_t         so_spare32[8];
491         int16_t         so_type;
492         int16_t         so_options;
493         int16_t         so_linger;
494         int16_t         so_state;
495         int16_t         so_timeo;
496         uint16_t        so_error;
497         struct xsockbuf {
498                 uint32_t        sb_cc;
499                 uint32_t        sb_hiwat;
500                 uint32_t        sb_mbcnt;
501                 uint32_t        sb_mcnt;
502                 uint32_t        sb_ccnt;
503                 uint32_t        sb_mbmax;
504                 int32_t         sb_lowat;
505                 int32_t         sb_timeo;
506                 int16_t         sb_flags;
507         } so_rcv, so_snd;
508 };
509
510 #ifdef _KERNEL
511 void    sotoxsocket(struct socket *so, struct xsocket *xso);
512 void    sbtoxsockbuf(struct sockbuf *sb, struct xsockbuf *xsb);
513 #endif
514
515 /*
516  * Socket buffer state bits.  Exported via libprocstat(3).
517  */
518 #define SBS_CANTSENDMORE        0x0010  /* can't send more data to peer */
519 #define SBS_CANTRCVMORE         0x0020  /* can't receive more data from peer */
520 #define SBS_RCVATMARK           0x0040  /* at mark on input */
521
522 #endif /* !_SYS_SOCKETVAR_H_ */